2025-09-23 21:57:08 +02:00
|
|
|
#include "PixelariumImageViewDefault.hpp"
|
2025-09-13 14:49:59 +02:00
|
|
|
|
|
|
|
|
#include <format>
|
|
|
|
|
|
2025-09-22 23:13:28 +02:00
|
|
|
#include "imaging/IPixelariumImage.hpp"
|
2025-09-13 14:49:59 +02:00
|
|
|
#include "imgui.h"
|
|
|
|
|
|
|
|
|
|
/// @brief Checks if the dimensions of two ImVec2 vectors have changed significantly.
|
|
|
|
|
/// @param ref_rect The reference ImVec2 vector.
|
|
|
|
|
/// @param new_rect The new ImVec2 vector to compare against.
|
|
|
|
|
/// @return True if the absolute difference between the y-coordinates is greater than 5 or the x-coordinates are
|
|
|
|
|
/// different; otherwise, false.
|
|
|
|
|
static bool dim_changed_p(const ImVec2& ref_rect, const ImVec2& new_rect)
|
|
|
|
|
{
|
|
|
|
|
if (std::abs(ref_rect.y - new_rect.y) > 5 || std::abs(ref_rect.x - new_rect.x))
|
|
|
|
|
{
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// @brief Calculates dimensions to maintain aspect ratio.
|
|
|
|
|
/// @param img The input image.
|
|
|
|
|
/// @param curr_dim The current dimensions.
|
|
|
|
|
/// @return The calculated dimensions maintaining aspect ratio.
|
2025-09-22 23:13:28 +02:00
|
|
|
ImVec2 aspect_const_dimensions(const ImVec2& raw_dim, const ImVec2& curr_dim)
|
2025-09-13 14:49:59 +02:00
|
|
|
{
|
2025-09-22 23:13:28 +02:00
|
|
|
const auto w_fact = (curr_dim.x / raw_dim.x);
|
|
|
|
|
const auto h_fact = (curr_dim.y / raw_dim.y);
|
2025-09-13 14:49:59 +02:00
|
|
|
|
|
|
|
|
const auto fact = w_fact < h_fact ? w_fact : h_fact;
|
|
|
|
|
|
2025-09-22 23:13:28 +02:00
|
|
|
return ImVec2(raw_dim.x * fact, raw_dim.y * fact);
|
2025-09-13 14:49:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// @brief Displays the image in an ImGui window.
|
|
|
|
|
///
|
|
|
|
|
/// If the image is null, empty, or has an empty name, the function returns immediately. Otherwise, it creates an ImGui
|
|
|
|
|
/// window with a horizontal scrollbar and menu bar. The image is rendered using the CvMatRender object, resizing it to
|
|
|
|
|
/// fit the available window space. The raw and rendered dimensions are displayed below the image.
|
2025-09-23 21:57:08 +02:00
|
|
|
void pixelarium::render::PixelariumImageViewDefault::ShowImage()
|
2025-09-13 14:49:59 +02:00
|
|
|
{
|
2025-09-23 21:57:08 +02:00
|
|
|
if (!this->cached_image_ || this->is_dirty_)
|
2025-09-22 23:13:28 +02:00
|
|
|
{
|
|
|
|
|
this->cached_image_ = this->img_->TryGetImage();
|
2025-09-23 21:57:08 +02:00
|
|
|
this->is_dirty_ = false;
|
2025-09-22 23:13:28 +02:00
|
|
|
}
|
|
|
|
|
|
2025-09-23 21:57:08 +02:00
|
|
|
if (this->img_->Empty() || this->img_->type_ == imaging::ImageFileType::UNKNOWN || !cached_image_ || this->img_->Name().empty())
|
2025-09-13 14:49:59 +02:00
|
|
|
{
|
|
|
|
|
// do nothing
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-22 23:13:28 +02:00
|
|
|
ImGui::Begin(this->img_->Name().c_str(), &this->open_p, ImGuiWindowFlags_HorizontalScrollbar | ImGuiWindowFlags_MenuBar);
|
2025-09-13 14:49:59 +02:00
|
|
|
|
|
|
|
|
this->curr_dim_ = ImGui::GetContentRegionAvail();
|
|
|
|
|
auto new_dim = ImGui::GetContentRegionAvail();
|
|
|
|
|
auto texture =
|
|
|
|
|
dim_changed_p(this->curr_dim_, new_dim)
|
|
|
|
|
? this->render_.Render(static_cast<size_t>(this->curr_dim_.x), static_cast<size_t>(this->curr_dim_.y))
|
|
|
|
|
: this->render_.Render();
|
|
|
|
|
|
|
|
|
|
this->curr_dim_ = new_dim;
|
|
|
|
|
|
2025-09-23 21:57:08 +02:00
|
|
|
ImVec2 dim(cached_image_->cols, cached_image_->rows);
|
2025-09-13 14:49:59 +02:00
|
|
|
|
|
|
|
|
ImGui::Image(reinterpret_cast<ImTextureID>(reinterpret_cast<void*>(texture)),
|
2025-09-22 23:13:28 +02:00
|
|
|
aspect_const_dimensions(dim, new_dim));
|
2025-09-13 14:49:59 +02:00
|
|
|
|
|
|
|
|
ImGui::Separator();
|
|
|
|
|
ImGui::Text("%s", std::format(" Raw Dimensions W : {}, H: {}", dim.x, dim.y).c_str());
|
|
|
|
|
ImGui::Text("%s", std::format("Render Dimensions W : {}, H: {}", curr_dim_.x, curr_dim_.y).c_str());
|
|
|
|
|
|
|
|
|
|
ImGui::End();
|
|
|
|
|
}
|