Misc (#19)
* get image returns optional<Mat> instead of unique_ptr<Mat> * introduce complexity * rename image load function * add example for a basic reader for binary image files * fix windows build? * add a status bar underneath the menu bar * use status bar in custom_0 example app * clang formats * packing w/ pragma push * add "Save As" functions to image views * resize over reserve * rm local override uri * add simple thread pool * rename to simple_thread_pool * get rid of useless renderlib * document version inc * extract hardcoded values to in-header * clang format patch * clone registered image in custom_0 * document binary-file header * minor fixes * clang format * rm unused render cmake
This commit is contained in:
committed by
Maximilian Kueffner
parent
e3e161ce52
commit
b37814204f
@@ -4,6 +4,56 @@
|
||||
#include "imgui.h"
|
||||
#include "imgui_impl_glfw.h"
|
||||
#include "imgui_impl_opengl3.h"
|
||||
#include "imgui_internal.h"
|
||||
#include "utilities/simple_thread_pool.hpp"
|
||||
|
||||
// see https://github.com/ocornut/imgui/issues/3518
|
||||
bool PixelBeginStatusBar()
|
||||
{
|
||||
using namespace ImGui;
|
||||
ImGuiContext& g = *GImGui;
|
||||
ImGuiViewportP* viewport = (ImGuiViewportP*)(void*)GetMainViewport();
|
||||
|
||||
SetCurrentViewport(NULL, viewport);
|
||||
|
||||
g.NextWindowData.MenuBarOffsetMinVal = ImVec2(
|
||||
g.Style.DisplaySafeAreaPadding.x, ImMax(g.Style.DisplaySafeAreaPadding.y - g.Style.FramePadding.y, 0.0f));
|
||||
ImGuiWindowFlags window_flags =
|
||||
ImGuiWindowFlags_NoScrollbar | ImGuiWindowFlags_NoSavedSettings | ImGuiWindowFlags_MenuBar;
|
||||
float height = GetFrameHeight();
|
||||
bool is_open = BeginViewportSideBar("##MainStatusBar", viewport, ImGuiDir_Up, height, window_flags);
|
||||
g.NextWindowData.MenuBarOffsetMinVal = ImVec2(0.0f, 0.0f);
|
||||
if (!is_open)
|
||||
{
|
||||
End();
|
||||
return false;
|
||||
}
|
||||
|
||||
g.CurrentWindow->Flags &= ~ImGuiWindowFlags_NoSavedSettings;
|
||||
BeginMenuBar();
|
||||
return is_open;
|
||||
}
|
||||
|
||||
void PixelEndStatusBar()
|
||||
{
|
||||
using namespace ImGui;
|
||||
ImGuiContext& g = *GImGui;
|
||||
if (!g.CurrentWindow->DC.MenuBarAppending)
|
||||
{
|
||||
IM_ASSERT_USER_ERROR(0, "Calling EndMainMenuBar() not from a menu-bar!");
|
||||
return;
|
||||
}
|
||||
|
||||
EndMenuBar();
|
||||
g.CurrentWindow->Flags |= ImGuiWindowFlags_NoSavedSettings;
|
||||
|
||||
if (g.CurrentWindow == g.NavWindow && g.NavLayer == ImGuiNavLayer_Main && !g.NavAnyRequest && g.ActiveId == 0)
|
||||
FocusTopMostWindowUnderOne(
|
||||
g.NavWindow, NULL, NULL,
|
||||
ImGuiFocusRequestFlags_UnlessBelowModal | ImGuiFocusRequestFlags_RestoreFocusedChild);
|
||||
|
||||
End();
|
||||
}
|
||||
|
||||
/// @brief GLFW error callback function.
|
||||
/// @param error The error code.
|
||||
@@ -193,6 +243,18 @@ void pixelarium::application::AppGLFW::MenuBar()
|
||||
|
||||
ImGui::EndMainMenuBar();
|
||||
}
|
||||
|
||||
if (show_status_ && PixelBeginStatusBar())
|
||||
{
|
||||
if (ImGui::Button("Ok", {20, 20}))
|
||||
{
|
||||
show_status_ = false;
|
||||
status_message_.clear();
|
||||
}
|
||||
|
||||
ImGui::Text("%s", status_message_.c_str());
|
||||
PixelEndStatusBar();
|
||||
}
|
||||
}
|
||||
|
||||
/// @brief Allows the user to select the log level via a combo box.
|
||||
@@ -213,3 +275,14 @@ void pixelarium::application::AppGLFW::LogLevelSelect()
|
||||
ImGui::EndCombo();
|
||||
}
|
||||
}
|
||||
|
||||
void pixelarium::application::AppGLFW::SetStatusTimed(const std::string& status, size_t seconds)
|
||||
{
|
||||
SetStatus(status);
|
||||
utils::simple_thread_pool::run_asynch(
|
||||
[this, seconds]()
|
||||
{
|
||||
std::this_thread::sleep_for(std::chrono::seconds(seconds));
|
||||
ResetStatus();
|
||||
});
|
||||
}
|
||||
|
||||
+18
-2
@@ -2,9 +2,8 @@
|
||||
|
||||
#include <GLFW/glfw3.h>
|
||||
|
||||
#include <memory>
|
||||
#include <format>
|
||||
|
||||
#include "imgui.h"
|
||||
#include "utilities/ILog.hpp"
|
||||
|
||||
namespace pixelarium::application
|
||||
@@ -19,6 +18,21 @@ class AppGLFW
|
||||
/// @brief Start the main render loop
|
||||
void Start() { this->RunLoop(); }
|
||||
|
||||
void SetStatusTimed(const std::string& status, size_t second);
|
||||
|
||||
void SetStatus(const std::string& status)
|
||||
{
|
||||
logger_.Info(std::format("{}(): {}", __PRETTY_FUNCTION__, status));
|
||||
status_message_ = status;
|
||||
show_status_ = true;
|
||||
}
|
||||
|
||||
void ResetStatus()
|
||||
{
|
||||
status_message_.clear();
|
||||
show_status_ = false;
|
||||
}
|
||||
|
||||
protected:
|
||||
/// @brief Function implementing the first column of the menu bar (e.g. "Menu")
|
||||
virtual void MenuBarOptionsColumn1() {}
|
||||
@@ -47,5 +61,7 @@ class AppGLFW
|
||||
void LogLevelSelect();
|
||||
int log_level_{0};
|
||||
GLFWwindow* window = nullptr;
|
||||
bool show_status_{false};
|
||||
std::string status_message_{};
|
||||
};
|
||||
} // namespace pixelarium::application
|
||||
|
||||
+19
-3
@@ -16,15 +16,30 @@ set(APPLIBSRC
|
||||
${imgui_DIR}/backends/imgui_impl_opengl3.cpp
|
||||
${imgui_DIR}/backends/imgui_impl_glfw.cpp)
|
||||
|
||||
set(RENDERSRC
|
||||
rendering/RenderHelpers.hpp
|
||||
rendering/RenderHelpers.cpp
|
||||
rendering/CvMatRender.hpp
|
||||
rendering/CvMatRender.cpp
|
||||
rendering/RenderImageManager.hpp
|
||||
rendering/RenderImageManager.cpp
|
||||
rendering/IPixelariumImageView.hpp
|
||||
rendering/IPixelariumImageView.cpp
|
||||
rendering/PixelariumImageViewDefault.hpp
|
||||
rendering/PixelariumImageViewDefault.cpp
|
||||
rendering/PixelariumImageViewCzi.hpp
|
||||
rendering/PixelariumImageViewCzi.cpp
|
||||
rendering/ImageViewFactory.hpp
|
||||
rendering/ImageViewFactory.cpp)
|
||||
|
||||
set(APPLIBNAME pixelariumapplicationlib)
|
||||
|
||||
add_library(${APPLIBNAME}
|
||||
STATIC ${APPLIBSRC})
|
||||
STATIC ${APPLIBSRC} ${RENDERSRC})
|
||||
|
||||
target_link_libraries(${APPLIBNAME}
|
||||
PRIVATE pixelariumutilslib
|
||||
PRIVATE pixelariumimagelib
|
||||
PRIVATE pixelariumrenderlib)
|
||||
PRIVATE pixelariumimagelib)
|
||||
|
||||
# This needs to be public to let the consumer know about it.
|
||||
if(WIN32)
|
||||
@@ -47,6 +62,7 @@ target_include_directories(${APPLIBNAME}
|
||||
PRIVATE ${CMAKE_BINARY_DIR}
|
||||
PRIVATE ${PROJECT_SOURCE_DIR}/lib
|
||||
PRIVATE ${PROJECT_SOURCE_DIR}/lib/imaging
|
||||
PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/rendering
|
||||
PUBLIC ${pfd_DIR}
|
||||
PUBLIC ${imgui_DIR}
|
||||
PUBLIC ${imgui_DIR}/backends)
|
||||
|
||||
@@ -25,7 +25,7 @@ void DefaultApp::MenuBarOptionsColumn2()
|
||||
{
|
||||
if (ImGui::MenuItem(LOADIMAGE))
|
||||
{
|
||||
this->LoadImage();
|
||||
this->LoadImageDialogue();
|
||||
}
|
||||
|
||||
ImGui::EndMenu();
|
||||
@@ -40,7 +40,7 @@ void DefaultApp::Run()
|
||||
this->gallery_.RenderImages();
|
||||
}
|
||||
|
||||
void DefaultApp::LoadImage()
|
||||
void DefaultApp::LoadImageDialogue()
|
||||
{
|
||||
auto res{pfd::open_file("Load Inputs", pfd::path::home(), {"All Files", "*"}, pfd::opt::multiselect).result()};
|
||||
for (auto& p : res)
|
||||
|
||||
@@ -19,7 +19,7 @@ class DefaultApp : public AppGLFW
|
||||
DefaultApp(const utils::log::ILog& log, pixelarium::resources::ImageResourcePool& pool)
|
||||
: application::AppGLFW(log), pool_(pool), gallery_(log, pool)
|
||||
{
|
||||
gallery_.SetLoadFunction([&]() -> void { this->LoadImage(); });
|
||||
gallery_.SetLoadFunction([&]() -> void { this->LoadImageDialogue(); });
|
||||
}
|
||||
|
||||
protected:
|
||||
@@ -32,7 +32,7 @@ class DefaultApp : public AppGLFW
|
||||
application::PixelariumImageGallery gallery_;
|
||||
|
||||
protected:
|
||||
void LoadImage();
|
||||
void LoadImageDialogue();
|
||||
|
||||
private:
|
||||
bool image_listp_{true};
|
||||
|
||||
@@ -23,7 +23,7 @@ void PixelariumImageGallery::RenderGallery()
|
||||
if (ImGui::BeginListBox("Image List", ImVec2(200, 400)))
|
||||
{
|
||||
pool_.EnumerateResources(
|
||||
[&](size_t id, size_t idx, const imaging::IPixelariumImage& img) -> void
|
||||
[&](size_t id, size_t idx, const imaging::IPixelariumImage<cv::Mat>& img) -> void
|
||||
{
|
||||
const bool is_selected = selected_index == idx;
|
||||
if (ImGui::Selectable(std::format("{}", img.Name()).c_str(), is_selected))
|
||||
@@ -74,7 +74,7 @@ void PixelariumImageGallery::RenderGallery()
|
||||
void PixelariumImageGallery::RenderImages()
|
||||
{
|
||||
this->render_manager_->Enumerate(
|
||||
[&](resources::ResourceKey key, render::RenderImageStateWrapper& render_state)
|
||||
[&](resources::ResourceKey key, application::RenderImageStateWrapper& render_state)
|
||||
{
|
||||
render_state.view->ShowImage();
|
||||
|
||||
|
||||
@@ -3,13 +3,14 @@
|
||||
#include "rendering/RenderImageManager.hpp"
|
||||
#include "resources/resource.hpp"
|
||||
#include "utilities/ILog.hpp"
|
||||
|
||||
namespace pixelarium::application
|
||||
{
|
||||
|
||||
/// @brief Defines a concept for a gallery type
|
||||
/// @tparam P The resource pool type of the gallery concept
|
||||
template <typename P>
|
||||
concept GalleryT = requires(P& p) { static_cast<resources::IResourcePool<P>&>(p); };
|
||||
template <typename P, class D>
|
||||
concept GalleryT = requires(P& p) { static_cast<resources::IResourcePool<P, D>&>(p); };
|
||||
|
||||
/// @brief Interface for a Pixelarium gallery.
|
||||
///
|
||||
@@ -33,7 +34,7 @@ class PixelariumImageGallery : IPixelariumGallery<resources::ImageResourcePool>
|
||||
|
||||
public:
|
||||
PixelariumImageGallery(const Log& log, resources::ImageResourcePool& pool)
|
||||
: pool_{pool}, log_{log}, render_manager_(std::make_unique<render::RenderImageManager>(pool, log))
|
||||
: pool_{pool}, log_{log}, render_manager_(std::make_unique<application::RenderImageManager>(pool, log))
|
||||
{
|
||||
}
|
||||
|
||||
@@ -49,7 +50,7 @@ class PixelariumImageGallery : IPixelariumGallery<resources::ImageResourcePool>
|
||||
std::function<void()> load_image_{};
|
||||
Pool& pool_;
|
||||
const Log& log_;
|
||||
std::unique_ptr<render::RenderImageManager> render_manager_;
|
||||
std::unique_ptr<application::RenderImageManager> render_manager_;
|
||||
bool image_listp_{true};
|
||||
bool auto_show_selectd_image_{true};
|
||||
size_t selected_image_{0};
|
||||
|
||||
@@ -17,6 +17,9 @@
|
||||
#define LOADIMAGE "Load Image"
|
||||
#define REMOVEIMAGE "Remove Image"
|
||||
#define CLEARALL "Clear All"
|
||||
#define SAVEAS "Save As..."
|
||||
// clang-format on
|
||||
|
||||
inline constexpr std::array<std::string_view, 5> LOGLEVELS = {"Trace", "Debug", "Info", "Warning", "Error"};
|
||||
|
||||
inline constexpr auto kInitialWindowWidth {700.0f};
|
||||
|
||||
@@ -0,0 +1,123 @@
|
||||
#include "CvMatRender.hpp"
|
||||
|
||||
#include <opencv2/core/mat.hpp>
|
||||
#include <opencv2/imgproc.hpp>
|
||||
|
||||
#include "imaging/IPixelariumImage.hpp"
|
||||
|
||||
using namespace pixelarium::imaging;
|
||||
|
||||
/// @brief Constructor for the CvMatRender class.
|
||||
/// @param img A shared pointer to the PixelariumImage to be rendered.
|
||||
pixelarium::application::CvMatRender::CvMatRender(const cv::Mat& img) : base_(img), texture_(0)
|
||||
{
|
||||
// storing a copy of the to-be-rendered image
|
||||
// because it will be resized and filtered eventually which we absolutely
|
||||
// must not do on the original image
|
||||
this->ResetRenderImage();
|
||||
cv::cvtColor(this->img_, this->img_, cv::COLOR_BGR2RGBA);
|
||||
}
|
||||
|
||||
/// @brief Destructor for the CvMatRender class.
|
||||
/// Deallocates the OpenGL texture if it exists.
|
||||
pixelarium::application::CvMatRender::~CvMatRender()
|
||||
{
|
||||
if (texture_)
|
||||
{
|
||||
glDeleteTextures(1, &texture_);
|
||||
texture_ = 0;
|
||||
}
|
||||
}
|
||||
|
||||
/// @brief Uploads the current image data to an OpenGL texture.
|
||||
/// @return The ID of the uploaded OpenGL texture.
|
||||
/// @throws std::runtime_error if the image data is empty or if there is an OpenGL error.
|
||||
GLuint pixelarium::application::CvMatRender::uploadTexture()
|
||||
{
|
||||
if (img_.empty())
|
||||
{
|
||||
throw std::runtime_error("Image data is empty.");
|
||||
}
|
||||
|
||||
if (!this->texture_)
|
||||
{
|
||||
glGenTextures(1, &this->texture_);
|
||||
if (this->texture_ == 0)
|
||||
{
|
||||
throw std::runtime_error("Failed to generate OpenGL texture.");
|
||||
}
|
||||
}
|
||||
|
||||
glBindTexture(GL_TEXTURE_2D, this->texture_);
|
||||
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
||||
|
||||
const int width = img_.cols;
|
||||
const int height = img_.rows;
|
||||
// see
|
||||
// https://stackoverflow.com/questions/10167534/how-to-find-out-what-type-of-a-mat-object-is-with-mattype-in-opencv
|
||||
// for pixel a pixel type table
|
||||
switch (img_.type())
|
||||
{
|
||||
case CV_8U:
|
||||
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_BYTE, img_.data);
|
||||
break;
|
||||
case CV_16U:
|
||||
case CV_16UC3:
|
||||
case CV_16UC4:
|
||||
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_SHORT, img_.data);
|
||||
break;
|
||||
case CV_32F:
|
||||
case CV_32FC4:
|
||||
glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT, width, height, 0, GL_DEPTH_COMPONENT, GL_FLOAT,
|
||||
img_.data);
|
||||
break;
|
||||
default:
|
||||
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, img_.cols, img_.rows, 0, GL_RGBA, GL_UNSIGNED_BYTE, img_.data);
|
||||
break;
|
||||
}
|
||||
|
||||
GLenum error = glGetError();
|
||||
if (error != GL_NO_ERROR)
|
||||
{
|
||||
throw std::runtime_error("OpenGL error during texture upload: " + std::to_string(error));
|
||||
}
|
||||
|
||||
return this->texture_;
|
||||
}
|
||||
|
||||
/// @brief Renders the image by uploading it as a texture.
|
||||
/// @return The ID of the OpenGL texture.
|
||||
GLuint pixelarium::application::CvMatRender::Render() { return this->uploadTexture(); }
|
||||
|
||||
/// @brief Renders the image with a specified scaling factor.
|
||||
/// @param factor The scaling factor for resizing the image.
|
||||
/// @return The ID of the OpenGL texture.
|
||||
GLuint pixelarium::application::CvMatRender::Render(float factor)
|
||||
{
|
||||
cv::resize(this->base_, this->img_, cv::Size(0, 0), factor, factor, cv::INTER_LINEAR_EXACT);
|
||||
|
||||
return this->uploadTexture();
|
||||
}
|
||||
|
||||
/// @brief Renders the image to fit within the specified width and height.
|
||||
/// @param width The maximum width of the rendered image.
|
||||
/// @param height The maximum height of the rendered image.
|
||||
/// @return The ID of the OpenGL texture.
|
||||
GLuint pixelarium::application::CvMatRender::Render(size_t width, size_t height)
|
||||
{
|
||||
const auto sz{this->base_.size()};
|
||||
|
||||
const auto get_factor = [](auto opt1, auto opt2) -> float { return opt1 < opt2 ? opt1 : opt2; };
|
||||
|
||||
auto factor = get_factor(width / static_cast<float>(sz.width), height / static_cast<float>(sz.height));
|
||||
|
||||
return this->Render(factor);
|
||||
}
|
||||
|
||||
void pixelarium::application::CvMatRender::ResetRenderImage()
|
||||
{
|
||||
// we copy here
|
||||
this->img_ = this->base_.clone();
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
#pragma once
|
||||
// windows.h must come before GL/GL.h here.
|
||||
// clang format would change this, effectively rendering the build broken.
|
||||
// clang-format off
|
||||
#ifdef _WIN32
|
||||
#include <windows.h>
|
||||
#include <GL/GL.h>
|
||||
#else
|
||||
#define GL_SILENCE_DEPRECATION
|
||||
#if defined(IMGUI_IMPL_OPENGL_ES2)
|
||||
#include <GLES2/gl2.h>
|
||||
#endif
|
||||
#include <GLFW/glfw3.h> // Will drag system OpenGL headers
|
||||
#endif
|
||||
#include <opencv2/core/mat.hpp>
|
||||
// clang-format on
|
||||
|
||||
namespace pixelarium::application
|
||||
{
|
||||
/// @brief Renders cv::Mat bitmaps as OpenGL textures.
|
||||
class CvMatRender
|
||||
{
|
||||
public:
|
||||
// we want the default constructor for the time being
|
||||
// (although it does not make much sense and should
|
||||
// get removed in the future)
|
||||
// as the using AppGLFW constructs it empty as a member
|
||||
// when coming to life.
|
||||
// CvMatRender() = default;
|
||||
CvMatRender(CvMatRender&) = delete;
|
||||
CvMatRender(const CvMatRender&) = delete;
|
||||
CvMatRender(CvMatRender&&) = delete;
|
||||
CvMatRender& operator=(CvMatRender&) = delete;
|
||||
CvMatRender& operator=(CvMatRender&& other) = delete;
|
||||
~CvMatRender();
|
||||
explicit CvMatRender(const cv::Mat& img);
|
||||
|
||||
public:
|
||||
GLuint Render();
|
||||
GLuint Render(float factor);
|
||||
GLuint Render(size_t width, size_t height);
|
||||
void ResetRenderImage();
|
||||
|
||||
private:
|
||||
cv::Mat img_;
|
||||
const cv::Mat& base_;
|
||||
GLuint texture_;
|
||||
|
||||
GLuint uploadTexture();
|
||||
};
|
||||
|
||||
} // namespace pixelarium::application
|
||||
@@ -0,0 +1,26 @@
|
||||
#include "IPixelariumImageView.hpp"
|
||||
|
||||
#include <opencv2/imgcodecs.hpp>
|
||||
|
||||
#include "app_resources_default.h"
|
||||
#include "portable-file-dialogs.h"
|
||||
|
||||
auto pixelarium::application::IPixelariumImageView::ImageViewMenuBar() -> void
|
||||
{
|
||||
if (ImGui::BeginMenuBar())
|
||||
{
|
||||
if (ImGui::MenuItem(SAVEAS))
|
||||
{
|
||||
auto dest = pfd::save_file("Save File", ".", {"Image Files", "*.png *.jpg *.jpeg *.tiff"},
|
||||
pfd::opt::force_overwrite)
|
||||
.result();
|
||||
if (!dest.empty())
|
||||
{
|
||||
// this->img_->SaveImage(dest);
|
||||
cv::imwrite(dest, cached_image_);
|
||||
}
|
||||
}
|
||||
|
||||
ImGui::EndMenuBar();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
#pragma once
|
||||
|
||||
#include <memory>
|
||||
|
||||
#include "imaging/IPixelariumImage.hpp"
|
||||
#include "imgui.h"
|
||||
|
||||
namespace pixelarium::application
|
||||
{
|
||||
/// @brief An interface defining the contract on views to dedicated implementations of IPixelariumImage
|
||||
class IPixelariumImageView
|
||||
{
|
||||
public:
|
||||
virtual ~IPixelariumImageView() = default;
|
||||
virtual void ShowImage() = 0;
|
||||
|
||||
// default implemented
|
||||
public:
|
||||
virtual const bool* GetStatus() const noexcept { return &this->open_p; }
|
||||
virtual void ForceUpdate() noexcept { this->is_dirty_ = true; }
|
||||
|
||||
// this must be called immediately before a "ImGui::Begin" context
|
||||
// as it will affect the next window and result in undeterministic effects
|
||||
// when called "out of sync"
|
||||
virtual void SetInitialSize(float width = 700.0f, float height = 700.0f)
|
||||
{
|
||||
ImGui::SetNextWindowSize({width, height});
|
||||
}
|
||||
|
||||
protected:
|
||||
virtual void ImageViewMenuBar();
|
||||
|
||||
protected:
|
||||
std::shared_ptr<imaging::IPixelariumImageCvMat> img_{};
|
||||
cv::Mat cached_image_{};
|
||||
bool open_p{true};
|
||||
bool is_dirty_{true};
|
||||
bool first_render_{true};
|
||||
};
|
||||
} // namespace pixelarium::application
|
||||
@@ -0,0 +1,59 @@
|
||||
#include "ImageViewFactory.hpp"
|
||||
|
||||
#include <format>
|
||||
#include <memory>
|
||||
|
||||
#include "IPixelariumImageView.hpp"
|
||||
#include "PixelariumImageViewCzi.hpp"
|
||||
#include "PixelariumImageViewDefault.hpp"
|
||||
#include "imaging/IPixelariumImage.hpp"
|
||||
#include "imaging/PixelariumImageFactory.hpp"
|
||||
|
||||
/// @brief Creates a PixelariumImageView from a resource image.
|
||||
/// @param image_id The ID of the image resource to render.
|
||||
/// @return A unique pointer to the PixelariumImageView, or nullptr if the image resource is not found or is empty. The
|
||||
/// image data is copied.
|
||||
std::unique_ptr<pixelarium::application::IPixelariumImageView> pixelarium::application::ImageViewFactory::RenderImage(
|
||||
resources::ResourceKey image_id)
|
||||
{
|
||||
using ImageType = imaging::ImageFileType;
|
||||
auto res{this->image_pool_.GetResource(image_id)};
|
||||
|
||||
auto img{res.lock()};
|
||||
|
||||
if (img == nullptr)
|
||||
{
|
||||
return {};
|
||||
}
|
||||
|
||||
if (img->Empty())
|
||||
{
|
||||
return {};
|
||||
}
|
||||
|
||||
auto type = imaging::ExtensionToType(img->Uri().extension().string());
|
||||
if (img->Uri().empty())
|
||||
{
|
||||
log_.Info(std::format("{}: empty Uri for {}.", __PRETTY_FUNCTION__, img->Name()));
|
||||
type = ImageType::kMemory;
|
||||
}
|
||||
|
||||
switch (type)
|
||||
{
|
||||
case ImageType::kUnknown:
|
||||
case ImageType::kAbstract:
|
||||
case ImageType::kPng:
|
||||
case ImageType::kJpg:
|
||||
case ImageType::kTiff:
|
||||
case ImageType::kMemory:
|
||||
log_.Info(std::format("{}: Creating a Default View", __PRETTY_FUNCTION__));
|
||||
// beware: here we copy the actual image resource over to the new image
|
||||
return std::make_unique<PixelariumImageViewDefault>(img);
|
||||
case ImageType::kCzi:
|
||||
log_.Info(std::format("{}: Creating a CZI View", __PRETTY_FUNCTION__));
|
||||
// beware: here we copy the actual image resource over to the new image
|
||||
return std::make_unique<PixelariumImageViewCzi>(img, log_);
|
||||
default:
|
||||
return {};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
#pragma once
|
||||
|
||||
#include "IPixelariumImageView.hpp"
|
||||
#include "resources/resource.hpp"
|
||||
#include "utilities/ILog.hpp"
|
||||
|
||||
namespace pixelarium::application
|
||||
{
|
||||
/// @brief Factory for instantiating matching views to different implementations of IPixelariumImage.
|
||||
class ImageViewFactory
|
||||
{
|
||||
using Image = imaging::IPixelariumImageCvMat;
|
||||
using Pool = resources::ImageResourcePool;
|
||||
using Log = utils::log::ILog;
|
||||
|
||||
public:
|
||||
explicit ImageViewFactory(Pool& pool, const Log& log) : image_pool_(pool), log_(log) {}
|
||||
|
||||
std::unique_ptr<IPixelariumImageView> RenderImage(resources::ResourceKey id);
|
||||
|
||||
private:
|
||||
Pool& image_pool_;
|
||||
const Log& log_;
|
||||
};
|
||||
} // namespace pixelarium::application
|
||||
@@ -0,0 +1,120 @@
|
||||
#include "PixelariumImageViewCzi.hpp"
|
||||
|
||||
#include <format>
|
||||
#include <memory>
|
||||
|
||||
#include "CvMatRender.hpp"
|
||||
#include "RenderHelpers.hpp"
|
||||
#include "imaging/IPixelariumImage.hpp"
|
||||
#include "imaging/impl/PixelariumCzi.hpp"
|
||||
#include "imgui.h"
|
||||
|
||||
void pixelarium::application::PixelariumImageViewCzi::RefreshCachedImage()
|
||||
{
|
||||
if (this->cached_image_.empty() || this->is_dirty_)
|
||||
{
|
||||
log_.Info(std::format("{}: refreshing image.", __PRETTY_FUNCTION__));
|
||||
imaging::CziParams params;
|
||||
params.dimension_map = this->dimension_map_;
|
||||
this->cached_image_ = this->img_->TryGetImage(params).value_or(cv::Mat{});
|
||||
// Resetting the image while the renderer is possibly accessing the
|
||||
// image at the same time is not a good idea. Therefore, we simply create
|
||||
// a new renderer here.
|
||||
this->render_ = std::make_unique<CvMatRender>(this->cached_image_);
|
||||
this->is_dirty_ = false;
|
||||
}
|
||||
}
|
||||
|
||||
pixelarium::application::PixelariumImageViewCzi::PixelariumImageViewCzi(std::shared_ptr<Image> img, const Log& log)
|
||||
: log_(log), render_(std::make_unique<CvMatRender>(*img->TryGetImage()))
|
||||
{
|
||||
img_ = img;
|
||||
auto czi_img = std::static_pointer_cast<imaging::PixelariumCzi>(this->img_);
|
||||
|
||||
auto stats = czi_img->GetStatistics();
|
||||
stats.dimBounds.EnumValidDimensions(
|
||||
[&](libCZI::DimensionIndex dim, int start, int) -> bool
|
||||
{
|
||||
this->dimension_map_[dim] = start;
|
||||
return true;
|
||||
});
|
||||
|
||||
// this->SetInitialSize();
|
||||
log_.Info(std::format("{}: dimension map size: {}", __PRETTY_FUNCTION__, dimension_map_.size()));
|
||||
}
|
||||
|
||||
/// @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.
|
||||
void pixelarium::application::PixelariumImageViewCzi::ShowImage()
|
||||
{
|
||||
auto czi_img = std::static_pointer_cast<imaging::PixelariumCzi>(this->img_);
|
||||
|
||||
if (!czi_img) return;
|
||||
|
||||
RefreshCachedImage();
|
||||
|
||||
if (czi_img->Empty() || this->img_->type_ == imaging::ImageFileType::kUnknown || cached_image_.empty() ||
|
||||
czi_img->Name().empty())
|
||||
{
|
||||
// do nothing
|
||||
return;
|
||||
}
|
||||
|
||||
if (first_render_)
|
||||
{
|
||||
first_render_ = false;
|
||||
constexpr auto initial_width{700.0f};
|
||||
const auto cached_width{cached_image_.cols};
|
||||
const auto cached_heigth{cached_image_.rows};
|
||||
const auto ratio{static_cast<float>(cached_heigth) / cached_width};
|
||||
SetInitialSize(initial_width, (initial_width * ratio + 100));
|
||||
}
|
||||
|
||||
ImGui::Begin(this->img_->Name().c_str(), &this->open_p,
|
||||
ImGuiWindowFlags_HorizontalScrollbar | ImGuiWindowFlags_MenuBar);
|
||||
|
||||
ImageViewMenuBar();
|
||||
|
||||
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;
|
||||
|
||||
ImVec2 dim(cached_image_.cols, cached_image_.rows);
|
||||
|
||||
ImGui::Image(reinterpret_cast<ImTextureID>(reinterpret_cast<void*>(texture)),
|
||||
aspect_const_dimensions(dim, new_dim));
|
||||
|
||||
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::Text("Dimensions");
|
||||
ImGui::Separator();
|
||||
if (ImGui::Button("Update"))
|
||||
{
|
||||
this->ForceUpdate();
|
||||
}
|
||||
|
||||
auto stats = czi_img->GetStatistics();
|
||||
stats.dimBounds.EnumValidDimensions(
|
||||
[&](libCZI::DimensionIndex dim, int start, int size) -> bool
|
||||
{
|
||||
auto dim_char = libCZI::Utils::DimensionToChar(dim);
|
||||
if (ImGui::SliderInt(std::format("{}({}-{})", dim_char, start, size).c_str(), &dimension_map_[dim], start,
|
||||
size - 1))
|
||||
{
|
||||
this->ForceUpdate();
|
||||
}
|
||||
|
||||
return true;
|
||||
});
|
||||
|
||||
ImGui::End();
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
#pragma once
|
||||
|
||||
#include <memory>
|
||||
#include <unordered_map>
|
||||
|
||||
#include "CvMatRender.hpp"
|
||||
#include "IPixelariumImageView.hpp"
|
||||
#include "imaging/IPixelariumImage.hpp"
|
||||
#include "imgui.h"
|
||||
#include "libCZI_DimCoordinate.h"
|
||||
#include "utilities/ILog.hpp"
|
||||
|
||||
namespace pixelarium::application
|
||||
{
|
||||
/// @brief A CZI-specific implementation of IPixelariumImageView.
|
||||
class PixelariumImageViewCzi : public IPixelariumImageView
|
||||
{
|
||||
using Image = imaging::IPixelariumImageCvMat;
|
||||
using Log = utils::log::ILog;
|
||||
|
||||
public:
|
||||
explicit PixelariumImageViewCzi(std::shared_ptr<Image> img, const Log& log);
|
||||
PixelariumImageViewCzi() = delete;
|
||||
PixelariumImageViewCzi(PixelariumImageViewCzi&) = delete;
|
||||
PixelariumImageViewCzi(const PixelariumImageViewCzi&) = delete;
|
||||
PixelariumImageViewCzi(PixelariumImageViewCzi&&) = delete;
|
||||
PixelariumImageViewCzi& operator=(PixelariumImageViewCzi&) = delete;
|
||||
PixelariumImageViewCzi& operator=(PixelariumImageViewCzi&&) = delete;
|
||||
|
||||
void ShowImage() override;
|
||||
|
||||
private:
|
||||
ImVec2 curr_dim_{};
|
||||
const Log& log_;
|
||||
std::unordered_map<libCZI::DimensionIndex, int> dimension_map_;
|
||||
std::unique_ptr<CvMatRender> render_;
|
||||
|
||||
private:
|
||||
void RefreshCachedImage();
|
||||
};
|
||||
} // namespace pixelarium::application
|
||||
@@ -0,0 +1,69 @@
|
||||
#include "PixelariumImageViewDefault.hpp"
|
||||
|
||||
#include <format>
|
||||
|
||||
#include "RenderHelpers.hpp"
|
||||
#include "app_resources_default.h"
|
||||
#include "imaging/IPixelariumImage.hpp"
|
||||
#include "imgui.h"
|
||||
|
||||
void pixelarium::application::PixelariumImageViewDefault::RefreshCachedImage()
|
||||
{
|
||||
if (this->cached_image_.empty() || this->is_dirty_)
|
||||
{
|
||||
this->cached_image_ = this->img_->TryGetImage().value_or(cv::Mat{});
|
||||
this->is_dirty_ = false;
|
||||
}
|
||||
}
|
||||
|
||||
/// @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.
|
||||
void pixelarium::application::PixelariumImageViewDefault::ShowImage()
|
||||
{
|
||||
RefreshCachedImage();
|
||||
|
||||
if (this->img_->Empty() || this->img_->type_ == imaging::ImageFileType::kUnknown || this->cached_image_.empty() ||
|
||||
this->img_->Name().empty())
|
||||
{
|
||||
// do nothing
|
||||
return;
|
||||
}
|
||||
|
||||
if (first_render_)
|
||||
{
|
||||
first_render_ = false;
|
||||
|
||||
const auto cached_width{cached_image_.cols};
|
||||
const auto cached_heigth{cached_image_.rows};
|
||||
const auto ratio{static_cast<float>(cached_heigth) / cached_width};
|
||||
SetInitialSize(kInitialWindowWidth, (kInitialWindowWidth * ratio + 100));
|
||||
}
|
||||
|
||||
ImGui::Begin(this->img_->Name().c_str(), &this->open_p,
|
||||
ImGuiWindowFlags_HorizontalScrollbar | ImGuiWindowFlags_MenuBar);
|
||||
|
||||
ImageViewMenuBar();
|
||||
|
||||
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;
|
||||
|
||||
ImVec2 dim(cached_image_.cols, cached_image_.rows);
|
||||
|
||||
ImGui::Image(reinterpret_cast<ImTextureID>(reinterpret_cast<void*>(texture)),
|
||||
aspect_const_dimensions(dim, new_dim));
|
||||
|
||||
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();
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
#pragma once
|
||||
|
||||
#include <memory>
|
||||
|
||||
#include "CvMatRender.hpp"
|
||||
#include "IPixelariumImageView.hpp"
|
||||
#include "imaging/IPixelariumImage.hpp"
|
||||
#include "imgui.h"
|
||||
|
||||
namespace pixelarium::application
|
||||
{
|
||||
/// @brief A default implementation of IPixelariumImageView.
|
||||
/// This is sufficient for single dimension images like png or jpg.
|
||||
class PixelariumImageViewDefault : public IPixelariumImageView
|
||||
{
|
||||
using Image = imaging::IPixelariumImageCvMat;
|
||||
|
||||
public:
|
||||
explicit PixelariumImageViewDefault(std::shared_ptr<Image> img) : render_(*img->TryGetImage())
|
||||
{
|
||||
img_ = img;
|
||||
// this->SetInitialSize();
|
||||
}
|
||||
PixelariumImageViewDefault() = delete;
|
||||
PixelariumImageViewDefault(PixelariumImageViewDefault&) = delete;
|
||||
PixelariumImageViewDefault(const PixelariumImageViewDefault&) = delete;
|
||||
PixelariumImageViewDefault(PixelariumImageViewDefault&&) = delete;
|
||||
PixelariumImageViewDefault& operator=(PixelariumImageViewDefault&) = delete;
|
||||
PixelariumImageViewDefault& operator=(PixelariumImageViewDefault&&) = delete;
|
||||
|
||||
void ShowImage() override;
|
||||
|
||||
private:
|
||||
ImVec2 curr_dim_{};
|
||||
CvMatRender render_;
|
||||
|
||||
private:
|
||||
void RefreshCachedImage();
|
||||
};
|
||||
} // namespace pixelarium::application
|
||||
@@ -0,0 +1,32 @@
|
||||
#include "RenderHelpers.hpp"
|
||||
|
||||
#include <cstdlib>
|
||||
|
||||
/// @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.
|
||||
bool pixelarium::application::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.
|
||||
ImVec2 pixelarium::application::aspect_const_dimensions(const ImVec2& raw_dim, const ImVec2& curr_dim)
|
||||
{
|
||||
const auto w_fact = (curr_dim.x / raw_dim.x);
|
||||
const auto h_fact = (curr_dim.y / raw_dim.y);
|
||||
|
||||
const auto fact = w_fact < h_fact ? w_fact : h_fact;
|
||||
|
||||
return ImVec2(raw_dim.x * fact, raw_dim.y * fact);
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
#pragma once
|
||||
|
||||
#include "imgui.h"
|
||||
|
||||
namespace pixelarium::application
|
||||
{
|
||||
bool dim_changed_p(const ImVec2& ref_rect, const ImVec2& new_rect);
|
||||
|
||||
ImVec2 aspect_const_dimensions(const ImVec2& raw_dim, const ImVec2& curr_dim);
|
||||
}; // namespace pixelarium::application
|
||||
@@ -0,0 +1,80 @@
|
||||
#include "RenderImageManager.hpp"
|
||||
|
||||
#include <format>
|
||||
|
||||
using namespace std;
|
||||
|
||||
/// @brief Updates the collection of rendered images by removing images marked for deletion.
|
||||
/// This function iterates through the \c keys_to_delete_ list and removes the corresponding images from the collection.
|
||||
/// It does not acquire the mutex to avoid deadlocks with the `Remove` function.
|
||||
void pixelarium::application::RenderImageManager::UpdateCollection()
|
||||
{
|
||||
for (const auto& key : keys_to_delete_)
|
||||
{
|
||||
this->Remove(key);
|
||||
}
|
||||
|
||||
keys_to_delete_.clear();
|
||||
}
|
||||
|
||||
/// @brief Marks a resource for deletion.
|
||||
/// @param key The ID of the resource to mark for deletion.
|
||||
void pixelarium::application::RenderImageManager::MarkForDeletion(resources::ResourceKey key)
|
||||
{
|
||||
this->log_.Debug(std::format("{} marking key: \"{}\" for deletion.", __PRETTY_FUNCTION__, key));
|
||||
lock_guard<mutex> guard(this->mut_);
|
||||
keys_to_delete_.insert(key);
|
||||
}
|
||||
|
||||
/// @brief Removes a render image from the manager.
|
||||
/// @param key The key of the render image to remove.
|
||||
/// @return True if the render image was removed, false otherwise.
|
||||
bool pixelarium::application::RenderImageManager::Remove(resources::ResourceKey key) noexcept
|
||||
{
|
||||
bool remove_state{false};
|
||||
this->log_.Debug(std::format("{} removing key: \"{}\" from renderlist.", __PRETTY_FUNCTION__, key));
|
||||
{
|
||||
lock_guard<mutex> guard(this->mut_);
|
||||
remove_state = this->render_image_map_.erase(key) == 1;
|
||||
}
|
||||
|
||||
return remove_state;
|
||||
}
|
||||
|
||||
/// @brief Adds a resource to the render image map.
|
||||
/// @param key The ID of the resource to add.
|
||||
/// @return void. No exception is thrown.
|
||||
void pixelarium::application::RenderImageManager::Add(resources::ResourceKey key) noexcept
|
||||
{
|
||||
// we don't want to add what's already there
|
||||
// or empty render images
|
||||
if (this->render_image_map_.contains(key))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
auto current_view = this->view_factory_->RenderImage(key);
|
||||
if (current_view == nullptr)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
this->log_.Debug(std::format("{}: adding key: \"{}\" to renderlist.", __PRETTY_FUNCTION__, key));
|
||||
|
||||
lock_guard<mutex> guard(this->mut_);
|
||||
// clang-format off
|
||||
this->render_image_map_.insert(
|
||||
{
|
||||
key,
|
||||
RenderImageStateWrapper
|
||||
{
|
||||
.view{ std::move(current_view) },
|
||||
}
|
||||
}
|
||||
);
|
||||
// clang-format on
|
||||
}
|
||||
|
||||
/// @brief Clears all render images from the manager.
|
||||
/// @note This function is noexcept.
|
||||
void pixelarium::application::RenderImageManager::Clear() noexcept { this->render_image_map_.clear(); }
|
||||
@@ -0,0 +1,74 @@
|
||||
#pragma once
|
||||
#include <memory>
|
||||
#include <mutex>
|
||||
#include <unordered_map>
|
||||
#include <unordered_set>
|
||||
|
||||
#include "IPixelariumImageView.hpp"
|
||||
#include "ImageViewFactory.hpp"
|
||||
#include "resources/resource.hpp"
|
||||
#include "utilities/ILog.hpp"
|
||||
|
||||
// This is intended as an additional abstraction
|
||||
// aggregating views that should be rendered (or not)
|
||||
namespace pixelarium::application
|
||||
{
|
||||
/// @brief Instead of directly using the view, we
|
||||
/// proxy it through a wrapper. This allows for arbitrary additional data
|
||||
/// to be added in future
|
||||
struct RenderImageStateWrapper
|
||||
{
|
||||
std::unique_ptr<IPixelariumImageView> view;
|
||||
const bool* show_state;
|
||||
};
|
||||
|
||||
/// @brief Manage instances of IPixelariumImageView.
|
||||
///
|
||||
/// This class is used to keep track of what must be rendered.
|
||||
/// It manages a set of IPixelariumImageView instances that can be traversed
|
||||
/// via its Enumerate() function.
|
||||
/// Views that shall not be rendered anymore should be marked for deletion
|
||||
/// with MarkForDeletion()
|
||||
class RenderImageManager
|
||||
{
|
||||
using Pool = resources::ImageResourcePool;
|
||||
|
||||
public:
|
||||
explicit RenderImageManager(Pool& pool, const utils::log::ILog& log)
|
||||
: view_factory_(std::make_unique<ImageViewFactory>(pool, log)), log_(log)
|
||||
{
|
||||
}
|
||||
|
||||
void Clear() noexcept;
|
||||
|
||||
void Add(resources::ResourceKey key) noexcept;
|
||||
|
||||
bool Remove(resources::ResourceKey key) noexcept;
|
||||
|
||||
// can't be const because func mutates the state
|
||||
template <typename Callable>
|
||||
requires std::invocable<Callable, resources::ResourceKey, RenderImageStateWrapper&>
|
||||
void Enumerate(Callable&& func)
|
||||
{
|
||||
for (auto& [key, val] : this->render_image_map_)
|
||||
{
|
||||
if (val.view != nullptr)
|
||||
{
|
||||
func(key, val);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void MarkForDeletion(resources::ResourceKey key);
|
||||
|
||||
void UpdateCollection();
|
||||
|
||||
private:
|
||||
std::unordered_map<resources::ResourceKey, RenderImageStateWrapper> render_image_map_;
|
||||
std::unique_ptr<ImageViewFactory> view_factory_;
|
||||
std::mutex mut_;
|
||||
std::unordered_set<resources::ResourceKey> keys_to_delete_;
|
||||
|
||||
const utils::log::ILog& log_;
|
||||
};
|
||||
} // namespace pixelarium::application
|
||||
Reference in New Issue
Block a user