satdump/src-core/common/widgets/waterfall_plot.cpp

66 lines
1.9 KiB
C++
Raw Normal View History

2022-04-28 13:38:07 +02:00
#include "waterfall_plot.h"
#include "imgui/imgui_internal.h"
#include <string>
#include "imgui/imgui_image.h"
#include "resources.h"
namespace widgets
{
WaterfallPlot::WaterfallPlot(float *v, int size, int lines)
{
values = v;
2022-11-29 00:01:26 +01:00
fft_max_size = fft_size = size;
2022-04-28 13:38:07 +02:00
fft_lines = lines;
}
WaterfallPlot::~WaterfallPlot()
{
if (!first_run)
delete[] raw_img_buffer;
}
2022-05-08 13:01:05 +02:00
void WaterfallPlot::draw(ImVec2 size, bool active)
2022-04-28 13:38:07 +02:00
{
2022-11-29 00:01:26 +01:00
work_mtx.lock();
2022-05-08 13:01:05 +02:00
if (active || first_run)
2022-04-28 13:38:07 +02:00
{
2022-05-08 13:01:05 +02:00
if (first_run)
{
texture_id = makeImageTexture();
first_run = false;
raw_img_buffer = new uint32_t[fft_size * fft_lines];
memset(raw_img_buffer, 0, sizeof(uint32_t) * fft_size * fft_lines);
2022-11-29 20:15:06 +01:00
if (palette.size() != resolution)
set_palette(colormaps::loadMap(resources::getResourcePath("waterfall/classic.json")), false);
2022-05-08 13:01:05 +02:00
}
2022-04-28 13:38:07 +02:00
2022-12-05 12:22:45 +01:00
memmove(&raw_img_buffer[fft_size * 1], &raw_img_buffer[fft_size * 0], fft_size * (fft_lines - 1) * sizeof(uint32_t));
2022-04-28 13:38:07 +02:00
2022-05-08 13:01:05 +02:00
for (int i = 0; i < fft_size; i++)
{
int v = ((values[i] - scale_min) / fabs(scale_max - scale_min)) * resolution;
2022-04-28 13:38:07 +02:00
2022-05-08 13:01:05 +02:00
if (v < 0)
v = 0;
2022-10-27 09:54:52 +02:00
if (v >= resolution)
v = resolution - 1;
2022-04-28 13:38:07 +02:00
2022-05-08 13:01:05 +02:00
raw_img_buffer[i] = palette[v];
}
2022-04-28 13:38:07 +02:00
2022-05-08 13:01:05 +02:00
updateImageTexture(texture_id, raw_img_buffer, fft_size, fft_lines);
}
2022-04-28 13:38:07 +02:00
ImGui::Image((void *)(intptr_t)texture_id, size);
2022-11-29 00:01:26 +01:00
work_mtx.unlock();
2022-04-28 13:38:07 +02:00
}
2022-11-29 20:15:06 +01:00
void WaterfallPlot::set_palette(colormaps::Map pa, bool mtx)
{
if (mtx)
work_mtx.lock();
palette = colormaps::generatePalette(pa, resolution);
if (mtx)
work_mtx.unlock();
}
2022-04-28 13:38:07 +02:00
}