From 4b9517a148bcd6f47cc2f20a83355093811cc04b Mon Sep 17 00:00:00 2001 From: Vasile Vilvoiu Date: Thu, 21 Oct 2021 23:12:06 +0300 Subject: 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. --- src/live.cpp | 10 ++++++---- src/live.hpp | 7 ++++--- src/renderer.cpp | 21 ++++++++++++++------- src/renderer.hpp | 2 +- src/specgram.cpp | 4 ++-- 5 files changed, 27 insertions(+), 17 deletions(-) (limited to 'src') 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 #include -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 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(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 live = nullptr; if (conf.IsLive()) { - live = std::make_unique(conf, *color_map, *value_map); + live = std::make_unique(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(); -- cgit v1.2.3