diff options
| author | Vasile Vilvoiu <vasi@vilvoiu.ro> | 2021-10-21 23:12:06 +0300 |
|---|---|---|
| committer | Vasile Vilvoiu <vasi@vilvoiu.ro> | 2021-10-21 23:12:06 +0300 |
| commit | 4b9517a148bcd6f47cc2f20a83355093811cc04b (patch) | |
| tree | af982328464a716d6ec98d7fb6c542ce15bbef88 | |
| parent | e18e8eee0e04e936abee1afdf86b8d29e13ec3a7 (diff) | |
Renderer does not cache colromap and valuemap.
* Renderer build colormap from configuration.
* Renderer builds (temporary) valuemap from configuration.
* LiveOutput no longer needs cmap/vmap parameters.
| -rw-r--r-- | src/live.cpp | 10 | ||||
| -rw-r--r-- | src/live.hpp | 7 | ||||
| -rw-r--r-- | src/renderer.cpp | 21 | ||||
| -rw-r--r-- | src/renderer.hpp | 2 | ||||
| -rw-r--r-- | src/specgram.cpp | 4 | ||||
| -rw-r--r-- | test/test-renderer.cpp | 10 |
6 files changed, 32 insertions, 22 deletions
diff --git a/src/live.cpp b/src/live.cpp index 0120bd7..a59f828 100644 --- a/src/live.cpp +++ b/src/live.cpp @@ -9,8 +9,10 @@ #include <cassert> #include <cstring> -LiveOutput::LiveOutput(const Configuration& conf, const ColorMap& cmap, const ValueMap& vmap) - : configuration_(conf), renderer_(conf.GetForLive(), cmap, vmap, conf.GetCount()) +LiveOutput::LiveOutput(const Configuration& conf) + : width_(conf.GetWidth()) + , is_horizontal_(conf.IsHorizontal()) + , renderer_(conf.GetForLive(), conf.GetCount()) { auto width = conf.IsHorizontal() ? renderer_.GetHeight() : renderer_.GetWidth(); auto height = conf.IsHorizontal() ? renderer_.GetWidth() : renderer_.GetHeight(); @@ -34,7 +36,7 @@ std::vector<uint8_t> LiveOutput::AddWindow(const RealWindow& win_values) { auto window = this->renderer_.RenderLiveFFT(win_values); - std::size_t wlen_bytes = this->configuration_.GetWidth() * 4; + std::size_t wlen_bytes = this->width_ * 4; if (window.size() != wlen_bytes) { throw std::runtime_error("input window size differs from live window size"); } @@ -74,7 +76,7 @@ LiveOutput::Render() /* draw renderer output to window */ sf::Texture canvas_texture = this->renderer_.GetCanvas(); sf::Sprite canvas_sprite(canvas_texture); - if (this->configuration_.IsHorizontal()) { + if (this->is_horizontal_) { canvas_sprite.setRotation(-90.0f); canvas_sprite.setPosition(0.0f, canvas_texture.getSize().x); } diff --git a/src/live.hpp b/src/live.hpp index 33f3b3d..399d856 100644 --- a/src/live.hpp +++ b/src/live.hpp @@ -18,7 +18,8 @@ class LiveOutput { private: /* configuration */ - const Configuration configuration_; + std::size_t width_; + bool is_horizontal_; Renderer renderer_; /* live window */ @@ -38,7 +39,7 @@ public: * @param cmap Color map instance to use. * @param vmap Value map instance to use. */ - LiveOutput(const Configuration& conf, const ColorMap& cmap, const ValueMap& vmap); + LiveOutput(const Configuration& conf); /** * Add a FFT window to the history and render it. @@ -59,4 +60,4 @@ public: void Render(); }; -#endif
\ No newline at end of file +#endif diff --git a/src/renderer.cpp b/src/renderer.cpp index 00dedfe..b4f5a1e 100644 --- a/src/renderer.cpp +++ b/src/renderer.cpp @@ -48,11 +48,14 @@ Renderer::ValueToShortString(double value, int scale, const std::string& unit) return ss.str(); } -Renderer::Renderer(const Configuration& conf, const ColorMap& cmap, const ValueMap& vmap, std::size_t fft_count) - : configuration_(conf), fft_count_(fft_count), color_map_(cmap.Copy()) +Renderer::Renderer(const Configuration& conf, std::size_t fft_count) + : configuration_(conf) + , fft_count_(fft_count) + , color_map_(ColorMap::Build(conf.GetColorMap(), conf.GetBackgroundColor(), + conf.GetColorMapCustomColor())) { if (color_map_ == nullptr) { - throw std::runtime_error("failed to copy color map"); + throw std::runtime_error("failed to build colormap"); } if (fft_count == 0) { throw std::runtime_error("positive number of FFT windows required by renderer"); @@ -71,10 +74,14 @@ Renderer::Renderer(const Configuration& conf, const ColorMap& cmap, const ValueM Renderer::GetNiceTicks(0.0f, (double)fft_count * this->configuration_.GetAverageCount() * this->configuration_.GetFFTStride() / this->configuration_.GetRate(), "s", fft_count, 30, !this->configuration_.IsHorizontal()); - auto legend_ticks = Renderer::GetNiceTicks(vmap.GetLowerBound(), vmap.GetUpperBound(), - vmap.GetUnit(), this->configuration_.GetWidth(), 50, + auto vmap = ValueMap::Build(conf.GetScaleType(), + conf.GetScaleLowerBound(), + conf.GetScaleUpperBound(), + conf.GetScaleUnit()); + auto legend_ticks = Renderer::GetNiceTicks(vmap->GetLowerBound(), vmap->GetUpperBound(), + vmap->GetUnit(), this->configuration_.GetWidth(), 50, this->configuration_.IsHorizontal()); - this->live_ticks_ = Renderer::GetNiceTicks(vmap.GetLowerBound(), vmap.GetUpperBound(), + this->live_ticks_ = Renderer::GetNiceTicks(vmap->GetLowerBound(), vmap->GetUpperBound(), "", this->configuration_.GetLiveFFTHeight(), 20, !this->configuration_.IsHorizontal()); /* no unit, keep it short */ @@ -233,7 +240,7 @@ Renderer::Renderer(const Configuration& conf, const ColorMap& cmap, const ValueM this->canvas_.draw(legend_box, this->legend_transform_); /* legend gradient */ - auto memory = cmap.Gradient(this->configuration_.GetWidth()); + auto memory = color_map_->Gradient(this->configuration_.GetWidth()); sf::Texture tex; tex.create(this->configuration_.GetWidth(), 1); tex.update(reinterpret_cast<const uint8_t *>(memory.data())); diff --git a/src/renderer.hpp b/src/renderer.hpp index b0abf91..9bb69ca 100644 --- a/src/renderer.hpp +++ b/src/renderer.hpp @@ -110,7 +110,7 @@ protected: /* for all intents and purposes this should be private, but we want t public: Renderer() = delete; - Renderer(const Configuration& conf, const ColorMap& cmap, const ValueMap& vmap, std::size_t fft_count); + Renderer(const Configuration& conf, std::size_t fft_count); /** * Render the spectrogram area. diff --git a/src/specgram.cpp b/src/specgram.cpp index 0e4560c..0cd9635 100644 --- a/src/specgram.cpp +++ b/src/specgram.cpp @@ -169,7 +169,7 @@ main(int argc, char** argv) /* create live window */ std::unique_ptr<LiveOutput> live = nullptr; if (conf.IsLive()) { - live = std::make_unique<LiveOutput>(conf, *color_map, *value_map); + live = std::make_unique<LiveOutput>(conf); live->Render(); /* render empty window */ } @@ -324,7 +324,7 @@ main(int argc, char** argv) /* save file */ if (have_output) { - Renderer file_renderer(conf, *color_map, *value_map, history.size()); + Renderer file_renderer(conf, history.size()); file_renderer.RenderFFTArea(history); auto image = file_renderer.GetCanvas().copyToImage(); diff --git a/test/test-renderer.cpp b/test/test-renderer.cpp index be8d49c..2fd6df9 100644 --- a/test/test-renderer.cpp +++ b/test/test-renderer.cpp @@ -13,8 +13,8 @@ class ExposedRenderer : public Renderer public: using AxisTick = Renderer::AxisTick; - ExposedRenderer(const Configuration& conf, const ColorMap& cmap, const ValueMap& vmap, std::size_t fft_count) - : Renderer(conf, cmap, vmap, fft_count) { }; + ExposedRenderer(const Configuration& conf, std::size_t fft_count) + : Renderer(conf, fft_count) { }; static std::string EValueToShortString(double value, int scale, const std::string& unit) { @@ -148,9 +148,9 @@ TEST(TestRenderer, GetNiceTicks) EXPECT_FALSE(exit); /* renderer */ - EXPECT_THROW_MATCH(ExposedRenderer(conf.GetForLive(), JetColorMap(), DecibelValueMap(-120.0, 0.0, "FS"), 0), + EXPECT_THROW_MATCH(ExposedRenderer(conf.GetForLive(), 0), std::runtime_error, "positive number of FFT windows required by renderer"); - ExposedRenderer renderer(conf.GetForLive(), JetColorMap(), DecibelValueMap(-120.0, 0.0, "FS"), 128); + ExposedRenderer renderer(conf.GetForLive(), 128); EXPECT_THROW_MATCH(renderer.EGetNiceTicks(1.0, 1.0, "", 100, 30, false), std::runtime_error, "minimum and maximum values are not in order"); @@ -194,4 +194,4 @@ TEST(TestRenderer, GetNiceTicks) EXPECT_EQ(std::get<1>(*it), std::get<1>(test.ticks[i])); } } -}
\ No newline at end of file +} |
