satdump/src-interface/viewer2/handler/image/image_handler.cpp

182 lines
6.2 KiB
C++
Raw Normal View History

2025-01-01 16:16:02 +01:00
#include "image_handler.h"
#include "imgui/imgui_stdlib.h"
#include "core/style.h"
2025-01-02 17:04:32 +01:00
#include "common/image/io.h" // TODOREWORK
#include "common/image/processing.h"
2025-01-02 19:07:13 +01:00
#include "nlohmann/json_utils.h"
2025-01-01 16:16:02 +01:00
namespace satdump
{
namespace viewer
{
ImageHandler::ImageHandler()
{
}
ImageHandler::~ImageHandler()
{
}
void ImageHandler::drawMenu()
{
2025-01-02 17:04:32 +01:00
bool needs_to_be_disabled = is_processing;
2025-01-01 16:16:02 +01:00
if (ImGui::CollapsingHeader("Image"))
{
2025-01-02 17:04:32 +01:00
bool needs_to_update = false;
if (needs_to_be_disabled)
style::beginDisabled();
needs_to_update |= ImGui::Checkbox("Equalize", &equalize_img);
needs_to_update |= ImGui::Checkbox("Individual Equalize", &equalize_perchannel_img);
needs_to_update |= ImGui::Checkbox("White Balance", &white_balance_img);
needs_to_update |= ImGui::Checkbox("Normalize", &normalize_img);
needs_to_update |= ImGui::Checkbox("Median Blur", &median_blur_img);
2025-01-05 19:57:26 +01:00
needs_to_update |= ImGui::Checkbox("Rotate 180", &rotate180_image);
2025-01-02 17:04:32 +01:00
if (needs_to_be_disabled)
style::endDisabled();
2025-01-03 16:26:39 +01:00
if (image_calib_valid)
{
ImGui::Text("Calibration Unit %s", image_calib.unit.c_str());
ImGui::Text("Calibration Min %f", image_calib.min);
ImGui::Text("Calibration Max %f", image_calib.max);
}
2025-01-02 17:04:32 +01:00
if (needs_to_update)
asyncProcess();
}
if (imgview_needs_update)
{
image_view.update(get_current_img());
imgview_needs_update = false;
2025-01-03 16:26:39 +01:00
image_view.mouseCallback = [this](int x, int y)
{
auto &img = get_current_img();
ImGui::BeginTooltip(); // TODOREWORK
additionalMouseCallback(x, y);
ImGui::Text("Raw : %d", img.get(0, x, y));
if (image_calib_valid && image.channels() == 1)
{
double val = image_calib.getVal(img.getf(0, x, y));
ImGui::Text("Unit : %f %s", val, image_calib.unit.c_str());
}
2025-01-17 18:12:23 +01:00
if (image_proj_valid)
{
geodetic::geodetic_coords_t pos;
if (image_proj.inverse(x, y, pos))
{
ImGui::Text("Lat : Invalid!");
ImGui::Text("Lon : Invalid!");
}
else
{
ImGui::Text("Lat : %f", pos.lat);
ImGui::Text("Lon : %f", pos.lon);
}
}
2025-01-03 16:26:39 +01:00
ImGui::EndTooltip();
};
2025-01-02 17:04:32 +01:00
}
}
void ImageHandler::drawMenuBar()
{
if (ImGui::MenuItem("Save Image"))
{
logger->warn("Saving image to /tmp/out.png!");
image::save_img(get_current_img(), "/tmp/out.png");
2025-01-01 16:16:02 +01:00
}
}
void ImageHandler::drawContents(ImVec2 win_size)
{
image_view.draw(win_size);
}
2025-01-02 17:04:32 +01:00
2025-01-02 19:07:13 +01:00
void ImageHandler::setConfig(nlohmann::json p)
{
2025-01-02 21:30:41 +01:00
equalize_img = getValueOrDefault(p["equalize"], false);
2025-01-02 22:37:59 +01:00
equalize_perchannel_img = getValueOrDefault(p["individual_equalize"], false);
2025-01-02 21:30:41 +01:00
white_balance_img = getValueOrDefault(p["white_balance"], false);
normalize_img = getValueOrDefault(p["normalize"], false);
median_blur_img = getValueOrDefault(p["median_blur"], false);
2025-01-05 19:57:26 +01:00
rotate180_image = getValueOrDefault(p["rotate180_image"], false);
2025-01-02 19:07:13 +01:00
}
nlohmann::json ImageHandler::getConfig()
{
nlohmann::json p;
p["equalize"] = equalize_img;
2025-01-02 22:37:59 +01:00
p["individual_equalize"] = equalize_perchannel_img;
2025-01-02 19:07:13 +01:00
p["white_balance"] = white_balance_img;
p["normalize"] = normalize_img;
p["median_blur"] = median_blur_img;
2025-01-05 19:57:26 +01:00
p["rotate180_image"] = rotate180_image;
2025-01-02 19:07:13 +01:00
return p;
}
2025-01-03 16:26:39 +01:00
void ImageHandler::updateImage(image::Image &img) // TODOREWORK
{
2025-01-17 18:12:23 +01:00
image::set_metadata(image, {});
2025-01-03 16:26:39 +01:00
image = img;
process();
}
2025-01-02 17:04:32 +01:00
void ImageHandler::do_process()
{
bool image_needs_processing = equalize_img |
equalize_perchannel_img |
white_balance_img |
normalize_img |
2025-01-05 19:57:26 +01:00
median_blur_img |
2025-01-16 10:21:40 +01:00
rotate180_image;
2025-01-02 17:04:32 +01:00
if (image_needs_processing)
{
curr_image = image;
if (equalize_img)
image::equalize(curr_image, false);
if (equalize_perchannel_img)
image::equalize(curr_image, true);
if (white_balance_img)
image::white_balance(curr_image);
if (normalize_img)
image::normalize(curr_image);
if (median_blur_img)
image::median_blur(curr_image);
2025-01-05 19:57:26 +01:00
if (rotate180_image)
curr_image.mirror(true, true);
2025-01-02 17:04:32 +01:00
}
else
curr_image.clear();
has_second_image = image_needs_processing;
imgview_needs_update = true;
2025-01-03 16:26:39 +01:00
2025-01-17 18:12:23 +01:00
image_proj_valid = false;
if (image::has_metadata_proj_cfg(image))
2025-01-03 16:26:39 +01:00
{
2025-01-17 18:12:23 +01:00
image_proj = image::get_metadata_proj_cfg(image);
image_proj.init(0, 1);
image_proj_valid = true;
2025-01-03 16:26:39 +01:00
}
image_calib_valid = false;
2025-01-17 18:12:23 +01:00
if (image::has_metadata_calib_cfg(image))
2025-01-03 16:26:39 +01:00
{
2025-01-17 18:12:23 +01:00
image_calib = image::get_metadata_calib_cfg(image);
2025-01-03 16:26:39 +01:00
image_calib_valid = true;
}
2025-01-02 17:04:32 +01:00
}
2025-01-01 16:16:02 +01:00
}
}