* 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:
m-aXimilian
2026-01-23 23:00:35 +00:00
committed by Maximilian Kueffner
parent e3e161ce52
commit b37814204f
52 changed files with 712 additions and 207 deletions
+1 -1
View File
@@ -1,6 +1,6 @@
cmake_minimum_required(VERSION 3.23) cmake_minimum_required(VERSION 3.23)
project(pixelarium VERSION 0.0.10) project(pixelarium VERSION 0.0.11)
set(CMAKE_VERBOSE_MAKEFILE ON) set(CMAKE_VERBOSE_MAKEFILE ON)
set(CMAKE_CXX_STANDARD 23) set(CMAKE_CXX_STANDARD 23)
+14 -13
View File
@@ -1,16 +1,17 @@
# Version History # Version History
| Version | Description | | Version | Description |
|:-------:|:------------------------------------------------------------------------------------------------------------| |:--------:|:------------------------------------------------------------------------------------------------------------|
| 0.0.10| Adds Tiff-support, in-memory images, and advances usage example "custom_1" | | 0.0.11 | Miscellaneous refactoring |
| 0.0.9 | Improve documentation, add example for `DefaultApp` override semantics | | 0.0.10 | Adds Tiff-support, in-memory images, and advances usage example "custom_1" |
| 0.0.8 | Init example projects | | 0.0.9 | Improve documentation, add example for `DefaultApp` override semantics |
| 0.0.7 | Refactors image gallery logic from `DefaultApp` into a separate module | | 0.0.8 | Init example projects |
| 0.0.6 | Added documentation-only option `PIXELARIUM_BUILD_DOCS_ONLY`, libCZI upgrade to main branch CI improvements | | 0.0.7 | Refactors image gallery logic from `DefaultApp` into a separate module |
| 0.0.5 | Compile for C++23 and code style adaptions | | 0.0.6 | Added documentation-only option `PIXELARIUM_BUILD_DOCS_ONLY`, libCZI upgrade to main branch CI improvements |
| 0.0.4 | Fix MSVC build, some cosmetics, explicit initial window size for images | | 0.0.5 | Compile for C++23 and code style adaptions |
| 0.0.3 | Fetch subblocks based on the dimension selection sliders | | 0.0.4 | Fix MSVC build, some cosmetics, explicit initial window size for images |
| 0.0.2 | Add Dimension selector sliders to CZI image view | | 0.0.3 | Fetch subblocks based on the dimension selection sliders |
| 0.0.1 | Initiate CZI image view | | 0.0.2 | Add Dimension selector sliders to CZI image view |
| 0.0.0 | Initial version | | 0.0.1 | Initiate CZI image view |
| 0.0.0 | Initial version |
-1
View File
@@ -7,7 +7,6 @@ add_executable(${CUSTOM_0_NAME} ${SRC})
target_link_libraries(${CUSTOM_0_NAME} target_link_libraries(${CUSTOM_0_NAME}
PRIVATE pixelariumimagelib PRIVATE pixelariumimagelib
PRIVATE pixelariumrenderlib
PRIVATE pixelariumutilslib PRIVATE pixelariumutilslib
PRIVATE pixelariumresourcelib PRIVATE pixelariumresourcelib
PRIVATE pixelariumapplicationlib) PRIVATE pixelariumapplicationlib)
+169 -3
View File
@@ -1,7 +1,16 @@
#include <opencv2/core/hal/interface.h>
#include <cstdint>
#include <filesystem>
#include <format>
#include <fstream>
#include <memory> #include <memory>
#include <string> #include <string>
#include "DefaultApp.hpp" #include "DefaultApp.hpp"
#include "imgui.h"
#include "impl/PixelariumMem.hpp"
#include "portable-file-dialogs.h"
#include "resources/resource.hpp" #include "resources/resource.hpp"
#include "utilities/ILog.hpp" #include "utilities/ILog.hpp"
#include "utilities/SpdLogger.hpp" #include "utilities/SpdLogger.hpp"
@@ -20,9 +29,165 @@ unique_ptr<Log> logger{
make_unique<utils::log::SpdLogger>(string(getenv("HOME")) + "/.cache/pixelarium/simple_app.log", "default")}; make_unique<utils::log::SpdLogger>(string(getenv("HOME")) + "/.cache/pixelarium/simple_app.log", "default")};
#endif #endif
// instantiate an image pool for the application
resources::ImageResourcePool image_pool;
constexpr auto ToCVPixelType(size_t depth, size_t chans) -> int
{
int tp{};
switch (depth)
{
case 8:
tp = CV_8U;
break;
case 16:
tp = CV_16U;
break;
default:
return -1;
}
if (chans > 1)
{
return CV_MAKETYPE(tp, chans);
}
return tp;
}
struct StatusReport
{
const std::function<void(const std::string&)> report_status;
const std::function<void()> reset_status;
};
class BinaryReader
{
private:
filesystem::path bin_file{};
vector<std::byte> buffer{};
uintmax_t file_size;
// struct __attribute__((packed)) ParsedImage // gcc and clang only
#pragma pack(push, 1)
struct ParsedImage
{
uint8_t depth;
uint8_t channels;
uint16_t width;
uint16_t height;
void* data;
};
#pragma pack(pop)
auto RegisterImage(const ParsedImage& img, string& name, const StatusReport& report) -> void
{
if (img.width == 0 || img.height == 0 || img.channels == 0 || img.depth == 0 || !img.data)
{
report.reset_status();
report.report_status(
format("Parsing {} failed: Dimensions (w: {}, h: {}, d: {}, c: {}) cannot be parsed! The provided "
"bin-file is probably corrupted.",
name, img.width, img.height, img.depth, img.channels));
return;
}
auto tmp_mat =
cv::Mat(img.height, img.width, ToCVPixelType(img.depth, img.channels), const_cast<void*>(img.data));
// not cloning is a dangling reference once the externally managed data pointer is freed
auto mat{tmp_mat.clone()};
image_pool.SetResource(make_unique<imaging::PixelariumMem>(mat, name.c_str(), *logger));
}
auto ReadFile(const filesystem::path& file, const StatusReport& report) -> ParsedImage
{
uint8_t depth{};
uint8_t channels{};
uint16_t width{};
uint16_t height{};
uint64_t pixel_count{};
if (!filesystem::exists(file)) return {};
auto sz = filesystem::file_size(file);
// header layout of binary file
// | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 |
// | ^ | ^ | ^ | ^ | pixel count in byte |
// | |______ |__ |
// pixel depth | | height in px
// | width in px
// channel count
constexpr auto header_size{14};
bool can_read = sz >= header_size;
if (!can_read) return {};
if (!buffer.empty())
{
buffer.clear();
}
// buffer = static_cast<char*>(malloc(sz));
ifstream inp_stream(file, ios::binary);
if (inp_stream)
{
inp_stream.read(reinterpret_cast<char*>(&width), sizeof(width));
inp_stream.read(reinterpret_cast<char*>(&height), sizeof(height));
inp_stream.read(reinterpret_cast<char*>(&depth), sizeof(depth));
inp_stream.read(reinterpret_cast<char*>(&channels), sizeof(channels));
inp_stream.read(reinterpret_cast<char*>(&pixel_count), sizeof(pixel_count));
logger->Info(format("{}(): Pixel count {}", __FUNCTION__, pixel_count));
if (pixel_count <= sz - header_size)
{
buffer.resize(pixel_count);
inp_stream.read(reinterpret_cast<char*>(buffer.data()), pixel_count);
}
}
logger->Info(format("{}: Parsed image with width: {}, height: {}, depth: {}, channels: {}", __PRETTY_FUNCTION__,
width, height, depth, channels));
report.report_status(
format("Parsed image with width: {}, height: {}, depth: {}, channels: {}", width, height, depth, channels));
return {.depth = depth, .channels = channels, .width = width, .height = height, .data = buffer.data()};
}
public:
auto Present(const StatusReport& report) -> void
{
using namespace ImGui;
SetNextWindowSize({256, 124});
Begin("Load Binary File");
if (Button("Load File"))
{
auto res{pfd::open_file("Load Inputs", pfd::path::home(), {"Bin Files", "*.bin"}).result()};
if (!res.empty()) bin_file = filesystem::path(res.at(0));
}
if (filesystem::exists(bin_file))
{
file_size = filesystem::file_size(bin_file);
Text("File: %s (%lu)", bin_file.filename().c_str(), file_size);
if (Button("Parse File"))
{
auto buff = ReadFile(bin_file, report);
auto name = bin_file.filename().string();
RegisterImage(buff, name, report);
}
}
End();
}
};
// create a custom app inheriting from the library's default app // create a custom app inheriting from the library's default app
class MyApp : public application::DefaultApp class MyApp : public application::DefaultApp
{ {
private:
BinaryReader bin_read;
public: public:
MyApp(const Log& log, Pool& pool) : application::DefaultApp(log, pool) {} MyApp(const Log& log, Pool& pool) : application::DefaultApp(log, pool) {}
@@ -37,9 +202,6 @@ int main()
// some initial log message // some initial log message
logger->Info(std::format("{}: Starting Application {}", __FUNCTION__, "Pixelarium")); logger->Info(std::format("{}: Starting Application {}", __FUNCTION__, "Pixelarium"));
// instantiate an image pool for the application
resources::ImageResourcePool image_pool;
// create a custom application, inject its dependencies and start it // create a custom application, inject its dependencies and start it
auto app{MyApp(*logger, image_pool)}; auto app{MyApp(*logger, image_pool)};
@@ -50,4 +212,8 @@ void MyApp::Run()
{ {
this->gallery_.RenderGallery(); this->gallery_.RenderGallery();
this->gallery_.RenderImages(); this->gallery_.RenderImages();
const auto reporter =
StatusReport{.report_status = [this](const std::string& msg) { this->SetStatusTimed(msg, 5); },
.reset_status = [this]() { this->ResetStatus(); }};
bin_read.Present(reporter);
} }
-1
View File
@@ -7,7 +7,6 @@ add_executable(${CUSTOM_1_NAME} ${SRC})
target_link_libraries(${CUSTOM_1_NAME} target_link_libraries(${CUSTOM_1_NAME}
PRIVATE pixelariumimagelib PRIVATE pixelariumimagelib
PRIVATE pixelariumrenderlib
PRIVATE pixelariumutilslib PRIVATE pixelariumutilslib
PRIVATE pixelariumresourcelib PRIVATE pixelariumresourcelib
PRIVATE pixelariumapplicationlib) PRIVATE pixelariumapplicationlib)
+5 -4
View File
@@ -52,7 +52,7 @@ class Selector
if (ImGui::BeginCombo("Select first image", preview_0_.c_str())) if (ImGui::BeginCombo("Select first image", preview_0_.c_str()))
{ {
pool_.Enumerate( pool_.Enumerate(
[&](resources::ResourceKey key, size_t idx, const imaging::IPixelariumImage& img) -> void [&](resources::ResourceKey key, size_t idx, const imaging::IPixelariumImage<cv::Mat>& img) -> void
{ {
const bool is_selected = static_cast<int>(idx) == selected_idx_0; const bool is_selected = static_cast<int>(idx) == selected_idx_0;
if (ImGui::Selectable(img.Name().c_str(), is_selected)) if (ImGui::Selectable(img.Name().c_str(), is_selected))
@@ -74,7 +74,7 @@ class Selector
if (ImGui::BeginCombo("Select second image", preview_1_.c_str())) if (ImGui::BeginCombo("Select second image", preview_1_.c_str()))
{ {
pool_.Enumerate( pool_.Enumerate(
[&](resources::ResourceKey key, size_t idx, const imaging::IPixelariumImage& img) -> void [&](resources::ResourceKey key, size_t idx, const imaging::IPixelariumImage<cv::Mat>& img) -> void
{ {
const bool is_selected = static_cast<int>(idx) == selected_idx_1; const bool is_selected = static_cast<int>(idx) == selected_idx_1;
if (ImGui::Selectable(img.Name().c_str(), is_selected)) if (ImGui::Selectable(img.Name().c_str(), is_selected))
@@ -100,11 +100,12 @@ class Selector
auto img1 = pool_.GetResource(selected_key_1); auto img1 = pool_.GetResource(selected_key_1);
auto img_mat1 = img1.lock()->TryGetImage(); auto img_mat1 = img1.lock()->TryGetImage();
if (img_mat0 == nullptr || img_mat1 == nullptr || img_mat0->empty() || img_mat1->empty()) return; if (!img_mat0.has_value() || !img_mat1.has_value() || img_mat0.value().empty() || img_mat1.value().empty())
return;
if (img_mat0->size != img_mat1->size) return; if (img_mat0->size != img_mat1->size) return;
cv::multiply(*img_mat0, *img_mat1, *img_mat0); cv::multiply(img_mat0.value(), img_mat1.value(), img_mat0.value());
std::string name{std::format("Multiply_{}", idx_)}; std::string name{std::format("Multiply_{}", idx_)};
pool_.SetResource(std::make_unique<imaging::PixelariumMem>(*img_mat0, name, *logger)); pool_.SetResource(std::make_unique<imaging::PixelariumMem>(*img_mat0, name, *logger));
-1
View File
@@ -7,7 +7,6 @@ add_executable(${SIMPLE_NAME} ${SRC})
target_link_libraries(${SIMPLE_NAME} target_link_libraries(${SIMPLE_NAME}
PRIVATE pixelariumimagelib PRIVATE pixelariumimagelib
PRIVATE pixelariumrenderlib
PRIVATE pixelariumutilslib PRIVATE pixelariumutilslib
PRIVATE pixelariumresourcelib PRIVATE pixelariumresourcelib
PRIVATE pixelariumapplicationlib) PRIVATE pixelariumapplicationlib)
-1
View File
@@ -1,5 +1,4 @@
add_subdirectory(utilities) add_subdirectory(utilities)
add_subdirectory(imaging) add_subdirectory(imaging)
add_subdirectory(rendering)
add_subdirectory(resources) add_subdirectory(resources)
add_subdirectory(app) add_subdirectory(app)
+73
View File
@@ -4,6 +4,56 @@
#include "imgui.h" #include "imgui.h"
#include "imgui_impl_glfw.h" #include "imgui_impl_glfw.h"
#include "imgui_impl_opengl3.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. /// @brief GLFW error callback function.
/// @param error The error code. /// @param error The error code.
@@ -193,6 +243,18 @@ void pixelarium::application::AppGLFW::MenuBar()
ImGui::EndMainMenuBar(); 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. /// @brief Allows the user to select the log level via a combo box.
@@ -213,3 +275,14 @@ void pixelarium::application::AppGLFW::LogLevelSelect()
ImGui::EndCombo(); 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
View File
@@ -2,9 +2,8 @@
#include <GLFW/glfw3.h> #include <GLFW/glfw3.h>
#include <memory> #include <format>
#include "imgui.h"
#include "utilities/ILog.hpp" #include "utilities/ILog.hpp"
namespace pixelarium::application namespace pixelarium::application
@@ -19,6 +18,21 @@ class AppGLFW
/// @brief Start the main render loop /// @brief Start the main render loop
void Start() { this->RunLoop(); } 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: protected:
/// @brief Function implementing the first column of the menu bar (e.g. "Menu") /// @brief Function implementing the first column of the menu bar (e.g. "Menu")
virtual void MenuBarOptionsColumn1() {} virtual void MenuBarOptionsColumn1() {}
@@ -47,5 +61,7 @@ class AppGLFW
void LogLevelSelect(); void LogLevelSelect();
int log_level_{0}; int log_level_{0};
GLFWwindow* window = nullptr; GLFWwindow* window = nullptr;
bool show_status_{false};
std::string status_message_{};
}; };
} // namespace pixelarium::application } // namespace pixelarium::application
+19 -3
View File
@@ -16,15 +16,30 @@ set(APPLIBSRC
${imgui_DIR}/backends/imgui_impl_opengl3.cpp ${imgui_DIR}/backends/imgui_impl_opengl3.cpp
${imgui_DIR}/backends/imgui_impl_glfw.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) set(APPLIBNAME pixelariumapplicationlib)
add_library(${APPLIBNAME} add_library(${APPLIBNAME}
STATIC ${APPLIBSRC}) STATIC ${APPLIBSRC} ${RENDERSRC})
target_link_libraries(${APPLIBNAME} target_link_libraries(${APPLIBNAME}
PRIVATE pixelariumutilslib PRIVATE pixelariumutilslib
PRIVATE pixelariumimagelib PRIVATE pixelariumimagelib)
PRIVATE pixelariumrenderlib)
# This needs to be public to let the consumer know about it. # This needs to be public to let the consumer know about it.
if(WIN32) if(WIN32)
@@ -47,6 +62,7 @@ target_include_directories(${APPLIBNAME}
PRIVATE ${CMAKE_BINARY_DIR} PRIVATE ${CMAKE_BINARY_DIR}
PRIVATE ${PROJECT_SOURCE_DIR}/lib PRIVATE ${PROJECT_SOURCE_DIR}/lib
PRIVATE ${PROJECT_SOURCE_DIR}/lib/imaging PRIVATE ${PROJECT_SOURCE_DIR}/lib/imaging
PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/rendering
PUBLIC ${pfd_DIR} PUBLIC ${pfd_DIR}
PUBLIC ${imgui_DIR} PUBLIC ${imgui_DIR}
PUBLIC ${imgui_DIR}/backends) PUBLIC ${imgui_DIR}/backends)
+2 -2
View File
@@ -25,7 +25,7 @@ void DefaultApp::MenuBarOptionsColumn2()
{ {
if (ImGui::MenuItem(LOADIMAGE)) if (ImGui::MenuItem(LOADIMAGE))
{ {
this->LoadImage(); this->LoadImageDialogue();
} }
ImGui::EndMenu(); ImGui::EndMenu();
@@ -40,7 +40,7 @@ void DefaultApp::Run()
this->gallery_.RenderImages(); 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()}; auto res{pfd::open_file("Load Inputs", pfd::path::home(), {"All Files", "*"}, pfd::opt::multiselect).result()};
for (auto& p : res) for (auto& p : res)
+2 -2
View File
@@ -19,7 +19,7 @@ class DefaultApp : public AppGLFW
DefaultApp(const utils::log::ILog& log, pixelarium::resources::ImageResourcePool& pool) DefaultApp(const utils::log::ILog& log, pixelarium::resources::ImageResourcePool& pool)
: application::AppGLFW(log), pool_(pool), gallery_(log, pool) : application::AppGLFW(log), pool_(pool), gallery_(log, pool)
{ {
gallery_.SetLoadFunction([&]() -> void { this->LoadImage(); }); gallery_.SetLoadFunction([&]() -> void { this->LoadImageDialogue(); });
} }
protected: protected:
@@ -32,7 +32,7 @@ class DefaultApp : public AppGLFW
application::PixelariumImageGallery gallery_; application::PixelariumImageGallery gallery_;
protected: protected:
void LoadImage(); void LoadImageDialogue();
private: private:
bool image_listp_{true}; bool image_listp_{true};
+2 -2
View File
@@ -23,7 +23,7 @@ void PixelariumImageGallery::RenderGallery()
if (ImGui::BeginListBox("Image List", ImVec2(200, 400))) if (ImGui::BeginListBox("Image List", ImVec2(200, 400)))
{ {
pool_.EnumerateResources( 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; const bool is_selected = selected_index == idx;
if (ImGui::Selectable(std::format("{}", img.Name()).c_str(), is_selected)) if (ImGui::Selectable(std::format("{}", img.Name()).c_str(), is_selected))
@@ -74,7 +74,7 @@ void PixelariumImageGallery::RenderGallery()
void PixelariumImageGallery::RenderImages() void PixelariumImageGallery::RenderImages()
{ {
this->render_manager_->Enumerate( this->render_manager_->Enumerate(
[&](resources::ResourceKey key, render::RenderImageStateWrapper& render_state) [&](resources::ResourceKey key, application::RenderImageStateWrapper& render_state)
{ {
render_state.view->ShowImage(); render_state.view->ShowImage();
+5 -4
View File
@@ -3,13 +3,14 @@
#include "rendering/RenderImageManager.hpp" #include "rendering/RenderImageManager.hpp"
#include "resources/resource.hpp" #include "resources/resource.hpp"
#include "utilities/ILog.hpp" #include "utilities/ILog.hpp"
namespace pixelarium::application namespace pixelarium::application
{ {
/// @brief Defines a concept for a gallery type /// @brief Defines a concept for a gallery type
/// @tparam P The resource pool type of the gallery concept /// @tparam P The resource pool type of the gallery concept
template <typename P> template <typename P, class D>
concept GalleryT = requires(P& p) { static_cast<resources::IResourcePool<P>&>(p); }; concept GalleryT = requires(P& p) { static_cast<resources::IResourcePool<P, D>&>(p); };
/// @brief Interface for a Pixelarium gallery. /// @brief Interface for a Pixelarium gallery.
/// ///
@@ -33,7 +34,7 @@ class PixelariumImageGallery : IPixelariumGallery<resources::ImageResourcePool>
public: public:
PixelariumImageGallery(const Log& log, resources::ImageResourcePool& pool) 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_{}; std::function<void()> load_image_{};
Pool& pool_; Pool& pool_;
const Log& log_; const Log& log_;
std::unique_ptr<render::RenderImageManager> render_manager_; std::unique_ptr<application::RenderImageManager> render_manager_;
bool image_listp_{true}; bool image_listp_{true};
bool auto_show_selectd_image_{true}; bool auto_show_selectd_image_{true};
size_t selected_image_{0}; size_t selected_image_{0};
+3
View File
@@ -17,6 +17,9 @@
#define LOADIMAGE "Load Image" #define LOADIMAGE "Load Image"
#define REMOVEIMAGE "Remove Image" #define REMOVEIMAGE "Remove Image"
#define CLEARALL "Clear All" #define CLEARALL "Clear All"
#define SAVEAS "Save As..."
// clang-format on // clang-format on
inline constexpr std::array<std::string_view, 5> LOGLEVELS = {"Trace", "Debug", "Info", "Warning", "Error"}; inline constexpr std::array<std::string_view, 5> LOGLEVELS = {"Trace", "Debug", "Info", "Warning", "Error"};
inline constexpr auto kInitialWindowWidth {700.0f};
@@ -9,7 +9,7 @@ using namespace pixelarium::imaging;
/// @brief Constructor for the CvMatRender class. /// @brief Constructor for the CvMatRender class.
/// @param img A shared pointer to the PixelariumImage to be rendered. /// @param img A shared pointer to the PixelariumImage to be rendered.
pixelarium::render::CvMatRender::CvMatRender(const cv::Mat& img) : base_(img), texture_(0) pixelarium::application::CvMatRender::CvMatRender(const cv::Mat& img) : base_(img), texture_(0)
{ {
// storing a copy of the to-be-rendered image // storing a copy of the to-be-rendered image
// because it will be resized and filtered eventually which we absolutely // because it will be resized and filtered eventually which we absolutely
@@ -20,7 +20,7 @@ pixelarium::render::CvMatRender::CvMatRender(const cv::Mat& img) : base_(img), t
/// @brief Destructor for the CvMatRender class. /// @brief Destructor for the CvMatRender class.
/// Deallocates the OpenGL texture if it exists. /// Deallocates the OpenGL texture if it exists.
pixelarium::render::CvMatRender::~CvMatRender() pixelarium::application::CvMatRender::~CvMatRender()
{ {
if (texture_) if (texture_)
{ {
@@ -32,7 +32,7 @@ pixelarium::render::CvMatRender::~CvMatRender()
/// @brief Uploads the current image data to an OpenGL texture. /// @brief Uploads the current image data to an OpenGL texture.
/// @return The ID of the uploaded 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. /// @throws std::runtime_error if the image data is empty or if there is an OpenGL error.
GLuint pixelarium::render::CvMatRender::uploadTexture() GLuint pixelarium::application::CvMatRender::uploadTexture()
{ {
if (img_.empty()) if (img_.empty())
{ {
@@ -89,12 +89,12 @@ GLuint pixelarium::render::CvMatRender::uploadTexture()
/// @brief Renders the image by uploading it as a texture. /// @brief Renders the image by uploading it as a texture.
/// @return The ID of the OpenGL texture. /// @return The ID of the OpenGL texture.
GLuint pixelarium::render::CvMatRender::Render() { return this->uploadTexture(); } GLuint pixelarium::application::CvMatRender::Render() { return this->uploadTexture(); }
/// @brief Renders the image with a specified scaling factor. /// @brief Renders the image with a specified scaling factor.
/// @param factor The scaling factor for resizing the image. /// @param factor The scaling factor for resizing the image.
/// @return The ID of the OpenGL texture. /// @return The ID of the OpenGL texture.
GLuint pixelarium::render::CvMatRender::Render(float factor) GLuint pixelarium::application::CvMatRender::Render(float factor)
{ {
cv::resize(this->base_, this->img_, cv::Size(0, 0), factor, factor, cv::INTER_LINEAR_EXACT); cv::resize(this->base_, this->img_, cv::Size(0, 0), factor, factor, cv::INTER_LINEAR_EXACT);
@@ -105,7 +105,7 @@ GLuint pixelarium::render::CvMatRender::Render(float factor)
/// @param width The maximum width of the rendered image. /// @param width The maximum width of the rendered image.
/// @param height The maximum height of the rendered image. /// @param height The maximum height of the rendered image.
/// @return The ID of the OpenGL texture. /// @return The ID of the OpenGL texture.
GLuint pixelarium::render::CvMatRender::Render(size_t width, size_t height) GLuint pixelarium::application::CvMatRender::Render(size_t width, size_t height)
{ {
const auto sz{this->base_.size()}; const auto sz{this->base_.size()};
@@ -116,7 +116,7 @@ GLuint pixelarium::render::CvMatRender::Render(size_t width, size_t height)
return this->Render(factor); return this->Render(factor);
} }
void pixelarium::render::CvMatRender::ResetRenderImage() void pixelarium::application::CvMatRender::ResetRenderImage()
{ {
// we copy here // we copy here
this->img_ = this->base_.clone(); this->img_ = this->base_.clone();
@@ -15,7 +15,7 @@
#include <opencv2/core/mat.hpp> #include <opencv2/core/mat.hpp>
// clang-format on // clang-format on
namespace pixelarium::render namespace pixelarium::application
{ {
/// @brief Renders cv::Mat bitmaps as OpenGL textures. /// @brief Renders cv::Mat bitmaps as OpenGL textures.
class CvMatRender class CvMatRender
@@ -49,4 +49,4 @@ class CvMatRender
GLuint uploadTexture(); GLuint uploadTexture();
}; };
} // namespace pixelarium::render } // 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();
}
}
@@ -5,7 +5,7 @@
#include "imaging/IPixelariumImage.hpp" #include "imaging/IPixelariumImage.hpp"
#include "imgui.h" #include "imgui.h"
namespace pixelarium::render namespace pixelarium::application
{ {
/// @brief An interface defining the contract on views to dedicated implementations of IPixelariumImage /// @brief An interface defining the contract on views to dedicated implementations of IPixelariumImage
class IPixelariumImageView class IPixelariumImageView
@@ -18,15 +18,23 @@ class IPixelariumImageView
public: public:
virtual const bool* GetStatus() const noexcept { return &this->open_p; } virtual const bool* GetStatus() const noexcept { return &this->open_p; }
virtual void ForceUpdate() noexcept { this->is_dirty_ = true; } 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) virtual void SetInitialSize(float width = 700.0f, float height = 700.0f)
{ {
ImGui::SetNextWindowSize({width, height}); ImGui::SetNextWindowSize({width, height});
} }
protected: protected:
std::shared_ptr<imaging::IPixelariumImage> img_{}; virtual void ImageViewMenuBar();
std::unique_ptr<cv::Mat> cached_image_{};
protected:
std::shared_ptr<imaging::IPixelariumImageCvMat> img_{};
cv::Mat cached_image_{};
bool open_p{true}; bool open_p{true};
bool is_dirty_{true}; bool is_dirty_{true};
bool first_render_{true};
}; };
} // namespace pixelarium::render } // namespace pixelarium::application
@@ -3,17 +3,17 @@
#include <format> #include <format>
#include <memory> #include <memory>
#include "IPixelariumImageView.hpp"
#include "PixelariumImageViewCzi.hpp"
#include "PixelariumImageViewDefault.hpp"
#include "imaging/IPixelariumImage.hpp" #include "imaging/IPixelariumImage.hpp"
#include "imaging/PixelariumImageFactory.hpp" #include "imaging/PixelariumImageFactory.hpp"
#include "rendering/IPixelariumImageView.hpp"
#include "rendering/PixelariumImageViewCzi.hpp"
#include "rendering/PixelariumImageViewDefault.hpp"
/// @brief Creates a PixelariumImageView from a resource image. /// @brief Creates a PixelariumImageView from a resource image.
/// @param image_id The ID of the image resource to render. /// @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 /// @return A unique pointer to the PixelariumImageView, or nullptr if the image resource is not found or is empty. The
/// image data is copied. /// image data is copied.
std::unique_ptr<pixelarium::render::IPixelariumImageView> pixelarium::render::ImageViewFactory::RenderImage( std::unique_ptr<pixelarium::application::IPixelariumImageView> pixelarium::application::ImageViewFactory::RenderImage(
resources::ResourceKey image_id) resources::ResourceKey image_id)
{ {
using ImageType = imaging::ImageFileType; using ImageType = imaging::ImageFileType;
@@ -1,15 +1,15 @@
#pragma once #pragma once
#include "PixelariumImageViewDefault.hpp" #include "IPixelariumImageView.hpp"
#include "rendering/IPixelariumImageView.hpp"
#include "resources/resource.hpp" #include "resources/resource.hpp"
#include "utilities/ILog.hpp" #include "utilities/ILog.hpp"
namespace pixelarium::render
namespace pixelarium::application
{ {
/// @brief Factory for instantiating matching views to different implementations of IPixelariumImage. /// @brief Factory for instantiating matching views to different implementations of IPixelariumImage.
class ImageViewFactory class ImageViewFactory
{ {
using Image = imaging::IPixelariumImage; using Image = imaging::IPixelariumImageCvMat;
using Pool = resources::ImageResourcePool; using Pool = resources::ImageResourcePool;
using Log = utils::log::ILog; using Log = utils::log::ILog;
@@ -22,4 +22,4 @@ class ImageViewFactory
Pool& image_pool_; Pool& image_pool_;
const Log& log_; const Log& log_;
}; };
} // namespace pixelarium::render } // namespace pixelarium::application
@@ -3,13 +3,29 @@
#include <format> #include <format>
#include <memory> #include <memory>
#include "CvMatRender.hpp"
#include "RenderHelpers.hpp" #include "RenderHelpers.hpp"
#include "imaging/IPixelariumImage.hpp" #include "imaging/IPixelariumImage.hpp"
#include "imaging/impl/PixelariumCzi.hpp" #include "imaging/impl/PixelariumCzi.hpp"
#include "imgui.h" #include "imgui.h"
#include "rendering/CvMatRender.hpp"
pixelarium::render::PixelariumImageViewCzi::PixelariumImageViewCzi(std::shared_ptr<Image> img, const Log& log) 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())) : log_(log), render_(std::make_unique<CvMatRender>(*img->TryGetImage()))
{ {
img_ = img; img_ = img;
@@ -23,7 +39,7 @@ pixelarium::render::PixelariumImageViewCzi::PixelariumImageViewCzi(std::shared_p
return true; return true;
}); });
this->SetInitialSize(); // this->SetInitialSize();
log_.Info(std::format("{}: dimension map size: {}", __PRETTY_FUNCTION__, dimension_map_.size())); log_.Info(std::format("{}: dimension map size: {}", __PRETTY_FUNCTION__, dimension_map_.size()));
} }
@@ -32,35 +48,36 @@ pixelarium::render::PixelariumImageViewCzi::PixelariumImageViewCzi(std::shared_p
/// If the image is null, empty, or has an empty name, the function returns immediately. Otherwise, it creates an ImGui /// 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 /// 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. /// fit the available window space. The raw and rendered dimensions are displayed below the image.
void pixelarium::render::PixelariumImageViewCzi::ShowImage() void pixelarium::application::PixelariumImageViewCzi::ShowImage()
{ {
auto czi_img = std::static_pointer_cast<imaging::PixelariumCzi>(this->img_); auto czi_img = std::static_pointer_cast<imaging::PixelariumCzi>(this->img_);
if (!czi_img) return; if (!czi_img) return;
if (!this->cached_image_ || this->is_dirty_) RefreshCachedImage();
{
log_.Info(std::format("{}: refreshing image.", __PRETTY_FUNCTION__));
imaging::CziParams params;
params.dimension_map = this->dimension_map_;
this->cached_image_ = this->img_->TryGetImage(params);
// 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;
}
if (czi_img->Empty() || this->img_->type_ == imaging::ImageFileType::kUnknown || !cached_image_ || if (czi_img->Empty() || this->img_->type_ == imaging::ImageFileType::kUnknown || cached_image_.empty() ||
czi_img->Name().empty()) czi_img->Name().empty())
{ {
// do nothing // do nothing
return; 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, ImGui::Begin(this->img_->Name().c_str(), &this->open_p,
ImGuiWindowFlags_HorizontalScrollbar | ImGuiWindowFlags_MenuBar); ImGuiWindowFlags_HorizontalScrollbar | ImGuiWindowFlags_MenuBar);
ImageViewMenuBar();
this->curr_dim_ = ImGui::GetContentRegionAvail(); this->curr_dim_ = ImGui::GetContentRegionAvail();
auto new_dim = ImGui::GetContentRegionAvail(); auto new_dim = ImGui::GetContentRegionAvail();
auto texture = auto texture =
@@ -70,7 +87,7 @@ void pixelarium::render::PixelariumImageViewCzi::ShowImage()
this->curr_dim_ = new_dim; this->curr_dim_ = new_dim;
ImVec2 dim(cached_image_->cols, cached_image_->rows); ImVec2 dim(cached_image_.cols, cached_image_.rows);
ImGui::Image(reinterpret_cast<ImTextureID>(reinterpret_cast<void*>(texture)), ImGui::Image(reinterpret_cast<ImTextureID>(reinterpret_cast<void*>(texture)),
aspect_const_dimensions(dim, new_dim)); aspect_const_dimensions(dim, new_dim));
@@ -3,19 +3,19 @@
#include <memory> #include <memory>
#include <unordered_map> #include <unordered_map>
#include "CvMatRender.hpp"
#include "IPixelariumImageView.hpp"
#include "imaging/IPixelariumImage.hpp"
#include "imgui.h" #include "imgui.h"
#include "libCZI_DimCoordinate.h" #include "libCZI_DimCoordinate.h"
#include "rendering/CvMatRender.hpp"
#include "rendering/IPixelariumImageView.hpp"
#include "utilities/ILog.hpp" #include "utilities/ILog.hpp"
namespace pixelarium::render namespace pixelarium::application
{ {
/// @brief A CZI-specific implementation of IPixelariumImageView. /// @brief A CZI-specific implementation of IPixelariumImageView.
class PixelariumImageViewCzi : public IPixelariumImageView class PixelariumImageViewCzi : public IPixelariumImageView
{ {
using Image = imaging::IPixelariumImage; using Image = imaging::IPixelariumImageCvMat;
using Render = render::CvMatRender;
using Log = utils::log::ILog; using Log = utils::log::ILog;
public: public:
@@ -34,5 +34,8 @@ class PixelariumImageViewCzi : public IPixelariumImageView
const Log& log_; const Log& log_;
std::unordered_map<libCZI::DimensionIndex, int> dimension_map_; std::unordered_map<libCZI::DimensionIndex, int> dimension_map_;
std::unique_ptr<CvMatRender> render_; std::unique_ptr<CvMatRender> render_;
private:
void RefreshCachedImage();
}; };
} // namespace pixelarium::render } // namespace pixelarium::application
@@ -3,31 +3,50 @@
#include <format> #include <format>
#include "RenderHelpers.hpp" #include "RenderHelpers.hpp"
#include "app_resources_default.h"
#include "imaging/IPixelariumImage.hpp" #include "imaging/IPixelariumImage.hpp"
#include "imgui.h" #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. /// @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 /// 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 /// 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. /// fit the available window space. The raw and rendered dimensions are displayed below the image.
void pixelarium::render::PixelariumImageViewDefault::ShowImage() void pixelarium::application::PixelariumImageViewDefault::ShowImage()
{ {
if (!this->cached_image_ || this->is_dirty_) RefreshCachedImage();
{
this->cached_image_ = this->img_->TryGetImage();
this->is_dirty_ = false;
}
if (this->img_->Empty() || this->img_->type_ == imaging::ImageFileType::kUnknown || !cached_image_ || if (this->img_->Empty() || this->img_->type_ == imaging::ImageFileType::kUnknown || this->cached_image_.empty() ||
this->img_->Name().empty()) this->img_->Name().empty())
{ {
// do nothing // do nothing
return; 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, ImGui::Begin(this->img_->Name().c_str(), &this->open_p,
ImGuiWindowFlags_HorizontalScrollbar | ImGuiWindowFlags_MenuBar); ImGuiWindowFlags_HorizontalScrollbar | ImGuiWindowFlags_MenuBar);
ImageViewMenuBar();
this->curr_dim_ = ImGui::GetContentRegionAvail(); this->curr_dim_ = ImGui::GetContentRegionAvail();
auto new_dim = ImGui::GetContentRegionAvail(); auto new_dim = ImGui::GetContentRegionAvail();
auto texture = auto texture =
@@ -37,7 +56,7 @@ void pixelarium::render::PixelariumImageViewDefault::ShowImage()
this->curr_dim_ = new_dim; this->curr_dim_ = new_dim;
ImVec2 dim(cached_image_->cols, cached_image_->rows); ImVec2 dim(cached_image_.cols, cached_image_.rows);
ImGui::Image(reinterpret_cast<ImTextureID>(reinterpret_cast<void*>(texture)), ImGui::Image(reinterpret_cast<ImTextureID>(reinterpret_cast<void*>(texture)),
aspect_const_dimensions(dim, new_dim)); aspect_const_dimensions(dim, new_dim));
@@ -2,25 +2,24 @@
#include <memory> #include <memory>
#include "CvMatRender.hpp"
#include "IPixelariumImageView.hpp"
#include "imaging/IPixelariumImage.hpp" #include "imaging/IPixelariumImage.hpp"
#include "imgui.h" #include "imgui.h"
#include "rendering/CvMatRender.hpp"
#include "rendering/IPixelariumImageView.hpp"
namespace pixelarium::render namespace pixelarium::application
{ {
/// @brief A default implementation of IPixelariumImageView. /// @brief A default implementation of IPixelariumImageView.
/// This is sufficient for single dimension images like png or jpg. /// This is sufficient for single dimension images like png or jpg.
class PixelariumImageViewDefault : public IPixelariumImageView class PixelariumImageViewDefault : public IPixelariumImageView
{ {
using Image = imaging::IPixelariumImage; using Image = imaging::IPixelariumImageCvMat;
using Render = render::CvMatRender;
public: public:
explicit PixelariumImageViewDefault(std::shared_ptr<Image> img) : render_(*img->TryGetImage()) explicit PixelariumImageViewDefault(std::shared_ptr<Image> img) : render_(*img->TryGetImage())
{ {
img_ = img; img_ = img;
this->SetInitialSize(); // this->SetInitialSize();
} }
PixelariumImageViewDefault() = delete; PixelariumImageViewDefault() = delete;
PixelariumImageViewDefault(PixelariumImageViewDefault&) = delete; PixelariumImageViewDefault(PixelariumImageViewDefault&) = delete;
@@ -34,5 +33,8 @@ class PixelariumImageViewDefault : public IPixelariumImageView
private: private:
ImVec2 curr_dim_{}; ImVec2 curr_dim_{};
CvMatRender render_; CvMatRender render_;
private:
void RefreshCachedImage();
}; };
} // namespace pixelarium::render } // namespace pixelarium::application
@@ -1,12 +1,13 @@
#include "RenderHelpers.hpp" #include "RenderHelpers.hpp"
#include <cstdlib> #include <cstdlib>
/// @brief Checks if the dimensions of two ImVec2 vectors have changed significantly. /// @brief Checks if the dimensions of two ImVec2 vectors have changed significantly.
/// @param ref_rect The reference ImVec2 vector. /// @param ref_rect The reference ImVec2 vector.
/// @param new_rect The new ImVec2 vector to compare against. /// @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 /// @return True if the absolute difference between the y-coordinates is greater than 5 or the x-coordinates are
/// different; otherwise, false. /// different; otherwise, false.
bool pixelarium::render::dim_changed_p(const ImVec2& ref_rect, const ImVec2& new_rect) 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)) if (std::abs(ref_rect.y - new_rect.y) > 5 || std::abs(ref_rect.x - new_rect.x))
{ {
@@ -20,7 +21,7 @@ bool pixelarium::render::dim_changed_p(const ImVec2& ref_rect, const ImVec2& new
/// @param img The input image. /// @param img The input image.
/// @param curr_dim The current dimensions. /// @param curr_dim The current dimensions.
/// @return The calculated dimensions maintaining aspect ratio. /// @return The calculated dimensions maintaining aspect ratio.
ImVec2 pixelarium::render::aspect_const_dimensions(const ImVec2& raw_dim, const ImVec2& curr_dim) 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 w_fact = (curr_dim.x / raw_dim.x);
const auto h_fact = (curr_dim.y / raw_dim.y); const auto h_fact = (curr_dim.y / raw_dim.y);
@@ -2,9 +2,9 @@
#include "imgui.h" #include "imgui.h"
namespace pixelarium::render namespace pixelarium::application
{ {
bool dim_changed_p(const ImVec2& ref_rect, const ImVec2& new_rect); bool dim_changed_p(const ImVec2& ref_rect, const ImVec2& new_rect);
ImVec2 aspect_const_dimensions(const ImVec2& raw_dim, const ImVec2& curr_dim); ImVec2 aspect_const_dimensions(const ImVec2& raw_dim, const ImVec2& curr_dim);
}; // namespace pixelarium::render }; // namespace pixelarium::application
@@ -7,7 +7,7 @@ using namespace std;
/// @brief Updates the collection of rendered images by removing images marked for deletion. /// @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. /// 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. /// It does not acquire the mutex to avoid deadlocks with the `Remove` function.
void pixelarium::render::RenderImageManager::UpdateCollection() void pixelarium::application::RenderImageManager::UpdateCollection()
{ {
for (const auto& key : keys_to_delete_) for (const auto& key : keys_to_delete_)
{ {
@@ -19,7 +19,7 @@ void pixelarium::render::RenderImageManager::UpdateCollection()
/// @brief Marks a resource for deletion. /// @brief Marks a resource for deletion.
/// @param key The ID of the resource to mark for deletion. /// @param key The ID of the resource to mark for deletion.
void pixelarium::render::RenderImageManager::MarkForDeletion(resources::ResourceKey key) void pixelarium::application::RenderImageManager::MarkForDeletion(resources::ResourceKey key)
{ {
this->log_.Debug(std::format("{} marking key: \"{}\" for deletion.", __PRETTY_FUNCTION__, key)); this->log_.Debug(std::format("{} marking key: \"{}\" for deletion.", __PRETTY_FUNCTION__, key));
lock_guard<mutex> guard(this->mut_); lock_guard<mutex> guard(this->mut_);
@@ -29,7 +29,7 @@ void pixelarium::render::RenderImageManager::MarkForDeletion(resources::Resource
/// @brief Removes a render image from the manager. /// @brief Removes a render image from the manager.
/// @param key The key of the render image to remove. /// @param key The key of the render image to remove.
/// @return True if the render image was removed, false otherwise. /// @return True if the render image was removed, false otherwise.
bool pixelarium::render::RenderImageManager::Remove(resources::ResourceKey key) noexcept bool pixelarium::application::RenderImageManager::Remove(resources::ResourceKey key) noexcept
{ {
bool remove_state{false}; bool remove_state{false};
this->log_.Debug(std::format("{} removing key: \"{}\" from renderlist.", __PRETTY_FUNCTION__, key)); this->log_.Debug(std::format("{} removing key: \"{}\" from renderlist.", __PRETTY_FUNCTION__, key));
@@ -44,7 +44,7 @@ bool pixelarium::render::RenderImageManager::Remove(resources::ResourceKey key)
/// @brief Adds a resource to the render image map. /// @brief Adds a resource to the render image map.
/// @param key The ID of the resource to add. /// @param key The ID of the resource to add.
/// @return void. No exception is thrown. /// @return void. No exception is thrown.
void pixelarium::render::RenderImageManager::Add(resources::ResourceKey key) noexcept void pixelarium::application::RenderImageManager::Add(resources::ResourceKey key) noexcept
{ {
// we don't want to add what's already there // we don't want to add what's already there
// or empty render images // or empty render images
@@ -77,4 +77,4 @@ void pixelarium::render::RenderImageManager::Add(resources::ResourceKey key) noe
/// @brief Clears all render images from the manager. /// @brief Clears all render images from the manager.
/// @note This function is noexcept. /// @note This function is noexcept.
void pixelarium::render::RenderImageManager::Clear() noexcept { this->render_image_map_.clear(); } void pixelarium::application::RenderImageManager::Clear() noexcept { this->render_image_map_.clear(); }
@@ -4,14 +4,14 @@
#include <unordered_map> #include <unordered_map>
#include <unordered_set> #include <unordered_set>
#include "IPixelariumImageView.hpp"
#include "ImageViewFactory.hpp" #include "ImageViewFactory.hpp"
#include "rendering/IPixelariumImageView.hpp"
#include "resources/resource.hpp" #include "resources/resource.hpp"
#include "utilities/ILog.hpp" #include "utilities/ILog.hpp"
// This is intended as an additional abstraction // This is intended as an additional abstraction
// aggregating views that should be rendered (or not) // aggregating views that should be rendered (or not)
namespace pixelarium::render namespace pixelarium::application
{ {
/// @brief Instead of directly using the view, we /// @brief Instead of directly using the view, we
/// proxy it through a wrapper. This allows for arbitrary additional data /// proxy it through a wrapper. This allows for arbitrary additional data
@@ -71,4 +71,4 @@ class RenderImageManager
const utils::log::ILog& log_; const utils::log::ILog& log_;
}; };
} // namespace pixelarium::render } // namespace pixelarium::application
+1
View File
@@ -7,6 +7,7 @@ message(STATUS "OpenCV_LIBs from: " ${OpenCV_LIBS})
set(IMAGELIBSRC set(IMAGELIBSRC
IPixelariumImage.hpp IPixelariumImage.hpp
IPixelariumImage.cpp
PixelariumImageFactory.hpp PixelariumImageFactory.hpp
PixelariumImageFactory.cpp PixelariumImageFactory.cpp
impl/PixelariumJpg.hpp impl/PixelariumJpg.hpp
+15
View File
@@ -0,0 +1,15 @@
#include "IPixelariumImage.hpp"
#include <opencv2/imgcodecs.hpp>
bool pixelarium::imaging::IPixelariumImageCvMat::SaveImage(const std::string& uri)
{
auto current_image = this->TryGetImage();
if (!current_image.has_value())
{
return false;
}
return cv::imwrite(uri, current_image.value());
}
+60 -4
View File
@@ -2,7 +2,6 @@
#include <filesystem> #include <filesystem>
#include <functional> #include <functional>
#include <memory>
#include <opencv2/core/mat.hpp> #include <opencv2/core/mat.hpp>
#include <string> #include <string>
@@ -38,6 +37,7 @@ struct IImageQuery
/// @brief This aims to be a generic image abstraction /// @brief This aims to be a generic image abstraction
/// meant for codec specific implementation. /// meant for codec specific implementation.
template <class Data>
class IPixelariumImage class IPixelariumImage
{ {
public: public:
@@ -47,19 +47,19 @@ class IPixelariumImage
/// @return A unique pointer to a Mat object containing the image data, /// @return A unique pointer to a Mat object containing the image data,
/// or nullptr if the image is not found or cannot be retrieved. /// or nullptr if the image is not found or cannot be retrieved.
/// May throw exceptions for multidimensional images. /// May throw exceptions for multidimensional images.
virtual std::unique_ptr<cv::Mat> TryGetImage() = 0; virtual std::optional<Data> TryGetImage() = 0;
/// @brief Attempts to retrieve the image. /// @brief Attempts to retrieve the image.
/// @param query The query object defining the images to retrieve. /// @param query The query object defining the images to retrieve.
/// @return A unique pointer to a Mat object containing the image data, /// @return A unique pointer to a Mat object containing the image data,
/// or nullptr if the image is not found or cannot be retrieved. /// or nullptr if the image is not found or cannot be retrieved.
virtual std::unique_ptr<cv::Mat> TryGetImage(const IImageQuery& query) = 0; virtual std::optional<Data> TryGetImage(const IImageQuery& query) = 0;
/// @brief Attempts to retrieve a collection of images based on a query. /// @brief Attempts to retrieve a collection of images based on a query.
/// @param query The query object defining the images to retrieve. /// @param query The query object defining the images to retrieve.
/// @return A vector of unique pointers to cv::Mat objects. Each element is an image. /// @return A vector of unique pointers to cv::Mat objects. Each element is an image.
/// Returns an empty vector if no images are found or if an error occurs. /// Returns an empty vector if no images are found or if an error occurs.
virtual std::vector<std::unique_ptr<cv::Mat>> TryGetImages(const IImageQuery& query) = 0; virtual std::vector<std::optional<Data>> TryGetImages(const IImageQuery& query) = 0;
/// @brief Checks if the image is empty. /// @brief Checks if the image is empty.
/// @return true if the image is empty, false otherwise. /// @return true if the image is empty, false otherwise.
@@ -92,4 +92,60 @@ class IPixelariumImage
protected: protected:
std::filesystem::path uri_; std::filesystem::path uri_;
}; };
class IPixelariumImageCvMat : public IPixelariumImage<cv::Mat>
{
public:
virtual ~IPixelariumImageCvMat() = default;
/// @brief Attempts to retrieve the image.
/// @return A unique pointer to a Mat object containing the image data,
/// or nullptr if the image is not found or cannot be retrieved.
/// May throw exceptions for multidimensional images.
virtual std::optional<cv::Mat> TryGetImage() = 0;
/// @brief Attempts to retrieve the image.
/// @param query The query object defining the images to retrieve.
/// @return A unique pointer to a Mat object containing the image data,
/// or nullptr if the image is not found or cannot be retrieved.
virtual std::optional<cv::Mat> TryGetImage(const IImageQuery& query) = 0;
/// @brief Attempts to retrieve a collection of images based on a query.
/// @param query The query object defining the images to retrieve.
/// @return A vector of unique pointers to cv::Mat objects. Each element is an image.
/// Returns an empty vector if no images are found or if an error occurs.
virtual std::vector<std::optional<cv::Mat>> TryGetImages(const IImageQuery& query) = 0;
/// @brief Checks if the image is empty.
/// @return true if the image is empty, false otherwise.
virtual bool Empty() const noexcept = 0;
/// @brief Saves an image to the resource identifier given by uri.
/// @return true if saving was successful, false otherwise.
virtual bool SaveImage(const std::string& uri);
// default implemented
public:
/// @brief Gets the resource identifier as a file path.
/// @return @c std::filesystem::path of the underlying resource.
virtual std::filesystem::path Uri() const noexcept { return this->uri_; }
/// @brief Gets the resource name.
/// @note Implementations of IPixelariumImage that live in memory
/// should override this to get something meaningful as the name
/// cannot be fetched from the resource uri in that case.
/// @return The name of the underlying resource.
virtual std::string Name() const noexcept
{
if (!this->uri_.empty())
{
return this->uri_.filename().string();
}
return {};
}
public:
const static ImageFileType type_{ImageFileType::kAbstract};
};
} // namespace pixelarium::imaging } // namespace pixelarium::imaging
+2 -1
View File
@@ -3,13 +3,14 @@
#include <cctype> #include <cctype>
#include <memory> #include <memory>
#include "imaging/IPixelariumImage.hpp"
#include "imaging/impl/PixelariumMem.hpp" #include "imaging/impl/PixelariumMem.hpp"
#include "impl/PixelariumCzi.hpp" #include "impl/PixelariumCzi.hpp"
#include "impl/PixelariumJpg.hpp" #include "impl/PixelariumJpg.hpp"
#include "impl/PixelariumPng.hpp" #include "impl/PixelariumPng.hpp"
#include "impl/PixelariumTiff.hpp" #include "impl/PixelariumTiff.hpp"
/*static*/ std::unique_ptr<pixelarium::imaging::IPixelariumImage> /*static*/ std::unique_ptr<pixelarium::imaging::IPixelariumImageCvMat>
pixelarium::imaging::PixelariumImageFactory::CreateImage(const std::string& uri, const Log& log) pixelarium::imaging::PixelariumImageFactory::CreateImage(const std::string& uri, const Log& log)
{ {
const auto res{std::filesystem::path(uri)}; const auto res{std::filesystem::path(uri)};
+1 -1
View File
@@ -37,6 +37,6 @@ class PixelariumImageFactory
using Log = utils::log::ILog; using Log = utils::log::ILog;
public: public:
static std::unique_ptr<IPixelariumImage> CreateImage(const std::string& uri, const Log& log); static std::unique_ptr<IPixelariumImageCvMat> CreateImage(const std::string& uri, const Log& log);
}; };
} // namespace pixelarium::imaging } // namespace pixelarium::imaging
+10 -8
View File
@@ -4,6 +4,8 @@
#include <filesystem> #include <filesystem>
#include <format> #include <format>
#include <memory> #include <memory>
#include <opencv2/imgcodecs.hpp>
#include <optional>
#include <stdexcept> #include <stdexcept>
#include <utility> #include <utility>
@@ -48,8 +50,8 @@ constexpr int try_get_index_match(const pixelarium::imaging::CziParams& params,
return index; return index;
} }
std::unique_ptr<cv::Mat> CZISubBlockToCvMat(std::shared_ptr<libCZI::IBitmapData> bitmap, libCZI::PixelType pixeltype, std::optional<cv::Mat> CZISubBlockToCvMat(std::shared_ptr<libCZI::IBitmapData> bitmap, libCZI::PixelType pixeltype,
const pixelarium::utils::log::ILog& log) const pixelarium::utils::log::ILog& log)
{ {
size_t pixel_size{0}; size_t pixel_size{0};
int target_type; int target_type;
@@ -94,21 +96,21 @@ std::unique_ptr<cv::Mat> CZISubBlockToCvMat(std::shared_ptr<libCZI::IBitmapData>
break; break;
} }
if (pixel_size < 0) return nullptr; if (pixel_size < 0) return std::nullopt;
log.Info(std::format("{}: source pixel type {}, target cv pixel type {}, pixel size {}", __PRETTY_FUNCTION__, log.Info(std::format("{}: source pixel type {}, target cv pixel type {}, pixel size {}", __PRETTY_FUNCTION__,
pixel_pair.first, pixel_pair.second, pixel_size)); pixel_pair.first, pixel_pair.second, pixel_size));
size_t height{bitmap->GetHeight()}; size_t height{bitmap->GetHeight()};
size_t width{bitmap->GetWidth()}; size_t width{bitmap->GetWidth()};
auto fill_mat = std::make_unique<cv::Mat>(height, width, target_type); auto fill_mat = cv::Mat(height, width, target_type);
auto bitmap_info = bitmap->Lock(); auto bitmap_info = bitmap->Lock();
for (size_t h{0}; h < height; ++h) for (size_t h{0}; h < height; ++h)
{ {
unsigned char* source_row = ((unsigned char*)bitmap_info.ptrDataRoi) + bitmap_info.stride * h; unsigned char* source_row = ((unsigned char*)bitmap_info.ptrDataRoi) + bitmap_info.stride * h;
unsigned char* target_row = fill_mat->ptr(h); unsigned char* target_row = fill_mat.ptr(h);
for (size_t w{0}; w < width; ++w) for (size_t w{0}; w < width; ++w)
{ {
@@ -139,7 +141,7 @@ std::unique_ptr<cv::Mat> CZISubBlockToCvMat(std::shared_ptr<libCZI::IBitmapData>
return fill_mat; return fill_mat;
} }
std::unique_ptr<cv::Mat> pixelarium::imaging::PixelariumCzi::SubblockToCvMat(int index) std::optional<cv::Mat> pixelarium::imaging::PixelariumCzi::SubblockToCvMat(int index)
{ {
log_.Info(std::format("{}: constructing bitmap with index {}", __PRETTY_FUNCTION__, index)); log_.Info(std::format("{}: constructing bitmap with index {}", __PRETTY_FUNCTION__, index));
auto block = this->czi_reader_->ReadSubBlock(index); auto block = this->czi_reader_->ReadSubBlock(index);
@@ -170,9 +172,9 @@ pixelarium::imaging::PixelariumCzi::PixelariumCzi(const std::string& uri, const
}); });
} }
std::unique_ptr<cv::Mat> pixelarium::imaging::PixelariumCzi::TryGetImage() { return SubblockToCvMat(0); } std::optional<cv::Mat> pixelarium::imaging::PixelariumCzi::TryGetImage() { return SubblockToCvMat(0); }
std::unique_ptr<cv::Mat> pixelarium::imaging::PixelariumCzi::TryGetImage(const IImageQuery& query) std::optional<cv::Mat> pixelarium::imaging::PixelariumCzi::TryGetImage(const IImageQuery& query)
{ {
const auto czi_query = static_cast<const CziParams&>(query); const auto czi_query = static_cast<const CziParams&>(query);
int index = try_get_index_match(czi_query, *this->czi_reader_); int index = try_get_index_match(czi_query, *this->czi_reader_);
+6 -5
View File
@@ -18,7 +18,7 @@ struct CziParams : public IImageQuery
}; };
/// @brief Implements support for .czi-images in the realm of IPixelariumImage /// @brief Implements support for .czi-images in the realm of IPixelariumImage
class PixelariumCzi : public IPixelariumImage class PixelariumCzi : public IPixelariumImageCvMat
{ {
using Log = pixelarium::utils::log::ILog; using Log = pixelarium::utils::log::ILog;
@@ -31,16 +31,17 @@ class PixelariumCzi : public IPixelariumImage
// IPixelariumImage member implementations // IPixelariumImage member implementations
public: public:
std::unique_ptr<cv::Mat> TryGetImage() override; std::optional<cv::Mat> TryGetImage() override;
std::unique_ptr<cv::Mat> TryGetImage(const IImageQuery&) override; std::optional<cv::Mat> TryGetImage(const IImageQuery&) override;
std::vector<std::unique_ptr<cv::Mat>> TryGetImages(const IImageQuery&) override std::vector<std::optional<cv::Mat>> TryGetImages(const IImageQuery&) override
{ {
// ToDo: proper error // ToDo: proper error
throw std::runtime_error("Not implemented."); throw std::runtime_error("Not implemented.");
} }
public:
bool Empty() const noexcept override { return this->is_empty_; } bool Empty() const noexcept override { return this->is_empty_; }
const libCZI::SubBlockStatistics& GetStatistics() const { return this->image_statistics_; } const libCZI::SubBlockStatistics& GetStatistics() const { return this->image_statistics_; }
@@ -49,7 +50,7 @@ class PixelariumCzi : public IPixelariumImage
const static ImageFileType type_{ImageFileType::kCzi}; const static ImageFileType type_{ImageFileType::kCzi};
private: private:
std::unique_ptr<cv::Mat> SubblockToCvMat(int index); std::optional<cv::Mat> SubblockToCvMat(int index);
private: private:
// this should be set by each image getter // this should be set by each image getter
+2 -4
View File
@@ -1,7 +1,5 @@
#include "PixelariumJpg.hpp" #include "PixelariumJpg.hpp"
#include <filesystem>
#include <memory>
#include <opencv2/imgcodecs.hpp> #include <opencv2/imgcodecs.hpp>
#include <string> #include <string>
@@ -16,11 +14,11 @@ pixelarium::imaging::PixelariumJpg::PixelariumJpg(const std::string& uri)
this->uri_ = std::filesystem::path(uri); this->uri_ = std::filesystem::path(uri);
} }
std::unique_ptr<cv::Mat> pixelarium::imaging::PixelariumJpg::TryGetImage() std::optional<cv::Mat> pixelarium::imaging::PixelariumJpg::TryGetImage()
{ {
try try
{ {
auto img = std::make_unique<cv::Mat>(cv::imread(this->uri_.string())); auto img = cv::Mat(cv::imread(this->uri_.string()));
this->is_empty_ = false; this->is_empty_ = false;
+5 -4
View File
@@ -8,27 +8,28 @@
namespace pixelarium::imaging namespace pixelarium::imaging
{ {
/// @brief Implements support for .jpg-images in the realm of IPixelariumImage /// @brief Implements support for .jpg-images in the realm of IPixelariumImage
class PixelariumJpg : public IPixelariumImage class PixelariumJpg : public IPixelariumImageCvMat
{ {
public: public:
explicit PixelariumJpg(const std::string& url); explicit PixelariumJpg(const std::string& url);
// IPixelariumImage member implementations // IPixelariumImage member implementations
public: public:
std::unique_ptr<cv::Mat> TryGetImage() override; std::optional<cv::Mat> TryGetImage() override;
std::unique_ptr<cv::Mat> TryGetImage(const IImageQuery&) override std::optional<cv::Mat> TryGetImage(const IImageQuery&) override
{ {
// ToDo: proper error // ToDo: proper error
throw std::runtime_error("Not possible with jpg."); throw std::runtime_error("Not possible with jpg.");
} }
std::vector<std::unique_ptr<cv::Mat>> TryGetImages(const IImageQuery&) override std::vector<std::optional<cv::Mat>> TryGetImages(const IImageQuery&) override
{ {
// ToDo: proper error // ToDo: proper error
throw std::runtime_error("Not possible with jpg."); throw std::runtime_error("Not possible with jpg.");
} }
public:
bool Empty() const noexcept override { return this->is_empty_; } bool Empty() const noexcept override { return this->is_empty_; }
public: public:
+2 -2
View File
@@ -12,7 +12,7 @@ pixelarium::imaging::PixelariumMem::PixelariumMem(const cv::Mat& img, const std:
this->uri_ = std::filesystem::path(); this->uri_ = std::filesystem::path();
} }
std::unique_ptr<cv::Mat> pixelarium::imaging::PixelariumMem::TryGetImage() std::optional<cv::Mat> pixelarium::imaging::PixelariumMem::TryGetImage()
{ {
// ToDo: this craving for a revision of the whole concept: // ToDo: this craving for a revision of the whole concept:
// the interface requires a unique_ptr here. This concept was designed to "create an in-memory image on demand" sort // the interface requires a unique_ptr here. This concept was designed to "create an in-memory image on demand" sort
@@ -22,5 +22,5 @@ std::unique_ptr<cv::Mat> pixelarium::imaging::PixelariumMem::TryGetImage()
// So, returning a unique_ptr from it in the following semantic essentially calls the // So, returning a unique_ptr from it in the following semantic essentially calls the
// copy constructor of cv::Mat. This is potentially not "super bad", but at least it requires attention at some // copy constructor of cv::Mat. This is potentially not "super bad", but at least it requires attention at some
// point. // point.
return std::make_unique<cv::Mat>(this->img_); return this->img_;
} }
+4 -4
View File
@@ -9,7 +9,7 @@
namespace pixelarium::imaging namespace pixelarium::imaging
{ {
/// @brief Implements support for in-memory images in the realm of IPixelariumImage /// @brief Implements support for in-memory images in the realm of IPixelariumImage
class PixelariumMem : public IPixelariumImage class PixelariumMem : public IPixelariumImageCvMat
{ {
using Log = pixelarium::utils::log::ILog; using Log = pixelarium::utils::log::ILog;
@@ -18,11 +18,11 @@ class PixelariumMem : public IPixelariumImage
// IPixelariumImage member implementations // IPixelariumImage member implementations
public: public:
std::unique_ptr<cv::Mat> TryGetImage() override; std::optional<cv::Mat> TryGetImage() override;
std::unique_ptr<cv::Mat> TryGetImage(const IImageQuery&) override { throw std::runtime_error("Not implemented."); } std::optional<cv::Mat> TryGetImage(const IImageQuery&) override { throw std::runtime_error("Not implemented."); }
std::vector<std::unique_ptr<cv::Mat>> TryGetImages(const IImageQuery&) override std::vector<std::optional<cv::Mat>> TryGetImages(const IImageQuery&) override
{ {
throw std::runtime_error("Not implemented."); throw std::runtime_error("Not implemented.");
} }
+2 -2
View File
@@ -16,11 +16,11 @@ pixelarium::imaging::PixelariumPng::PixelariumPng(const std::string& uri)
this->uri_ = std::filesystem::path(uri); this->uri_ = std::filesystem::path(uri);
} }
std::unique_ptr<cv::Mat> pixelarium::imaging::PixelariumPng::TryGetImage() std::optional<cv::Mat> pixelarium::imaging::PixelariumPng::TryGetImage()
{ {
try try
{ {
auto img = std::make_unique<cv::Mat>(cv::imread(this->uri_.string())); auto img = cv::Mat(cv::imread(this->uri_.string()));
this->is_empty_ = false; this->is_empty_ = false;
+4 -4
View File
@@ -8,22 +8,22 @@
namespace pixelarium::imaging namespace pixelarium::imaging
{ {
/// @brief Implements support for .png-images in the realm of IPixelariumImage /// @brief Implements support for .png-images in the realm of IPixelariumImage
class PixelariumPng : public IPixelariumImage class PixelariumPng : public IPixelariumImageCvMat
{ {
public: public:
explicit PixelariumPng(const std::string& url); explicit PixelariumPng(const std::string& url);
// IPixelariumImage member implementations // IPixelariumImage member implementations
public: public:
std::unique_ptr<cv::Mat> TryGetImage() override; std::optional<cv::Mat> TryGetImage() override;
std::unique_ptr<cv::Mat> TryGetImage(const IImageQuery&) override std::optional<cv::Mat> TryGetImage(const IImageQuery&) override
{ {
// ToDo: proper error // ToDo: proper error
throw std::runtime_error("Not possible with png."); throw std::runtime_error("Not possible with png.");
} }
std::vector<std::unique_ptr<cv::Mat>> TryGetImages(const IImageQuery&) override std::vector<std::optional<cv::Mat>> TryGetImages(const IImageQuery&) override
{ {
// ToDo: proper error // ToDo: proper error
throw std::runtime_error("Not possible with png."); throw std::runtime_error("Not possible with png.");
+2 -2
View File
@@ -16,11 +16,11 @@ pixelarium::imaging::PixelariumTiff::PixelariumTiff(const std::string& uri, cons
this->uri_ = std::filesystem::path(uri); this->uri_ = std::filesystem::path(uri);
} }
std::unique_ptr<cv::Mat> pixelarium::imaging::PixelariumTiff::TryGetImage() std::optional<cv::Mat> pixelarium::imaging::PixelariumTiff::TryGetImage()
{ {
try try
{ {
auto img = std::make_unique<cv::Mat>(cv::imread(this->uri_.string())); auto img = cv::Mat(cv::imread(this->uri_.string()));
this->is_empty_ = false; this->is_empty_ = false;
+4 -4
View File
@@ -9,7 +9,7 @@
namespace pixelarium::imaging namespace pixelarium::imaging
{ {
/// @brief Implements support for .tiff-images in the realm of IPixelariumImage /// @brief Implements support for .tiff-images in the realm of IPixelariumImage
class PixelariumTiff : public IPixelariumImage class PixelariumTiff : public IPixelariumImageCvMat
{ {
using Log = pixelarium::utils::log::ILog; using Log = pixelarium::utils::log::ILog;
@@ -18,15 +18,15 @@ class PixelariumTiff : public IPixelariumImage
// IPixelariumImage member implementations // IPixelariumImage member implementations
public: public:
std::unique_ptr<cv::Mat> TryGetImage() override; std::optional<cv::Mat> TryGetImage() override;
std::unique_ptr<cv::Mat> TryGetImage(const IImageQuery&) override std::optional<cv::Mat> TryGetImage(const IImageQuery&) override
{ {
// ToDo: proper error // ToDo: proper error
throw std::runtime_error("Not possible with tiff."); throw std::runtime_error("Not possible with tiff.");
} }
std::vector<std::unique_ptr<cv::Mat>> TryGetImages(const IImageQuery&) override std::vector<std::optional<cv::Mat>> TryGetImages(const IImageQuery&) override
{ {
// ToDo: proper error // ToDo: proper error
throw std::runtime_error("Not possible with tiff."); throw std::runtime_error("Not possible with tiff.");
-26
View File
@@ -1,26 +0,0 @@
set(RENDERLIBNAME pixelariumrenderlib)
set(RENDERLIBSRC
RenderHelpers.hpp
RenderHelpers.cpp
CvMatRender.hpp
CvMatRender.cpp
RenderImageManager.hpp
RenderImageManager.cpp
IPixelariumImageView.hpp
PixelariumImageViewDefault.hpp
PixelariumImageViewDefault.cpp
PixelariumImageViewCzi.hpp
PixelariumImageViewCzi.cpp
ImageViewFactory.hpp
ImageViewFactory.cpp)
add_library(${RENDERLIBNAME} STATIC
${RENDERLIBSRC})
target_link_libraries(${RENDERLIBNAME}
PRIVATE pixelariumimagelib)
target_include_directories(${RENDERLIBNAME}
PRIVATE ${CMAKE_SOURCE_DIR}/lib
PRIVATE ${imgui_DIR})
+7 -6
View File
@@ -5,7 +5,9 @@
#include <functional> #include <functional>
#include <mutex> #include <mutex>
using pixelarium::imaging::IPixelariumImage; #include "imaging/IPixelariumImage.hpp"
using Image = pixelarium::imaging::IPixelariumImageCvMat;
using namespace std; using namespace std;
namespace namespace
@@ -21,7 +23,7 @@ size_t GenerateId() { return id_.fetch_add(1, memory_order_relaxed); }
/// @brief Retrieves a resource from the pool. /// @brief Retrieves a resource from the pool.
/// @param id The ID of the resource to retrieve. /// @param id The ID of the resource to retrieve.
/// @return A pointer to the resource if found, otherwise an empty optional. /// @return A pointer to the resource if found, otherwise an empty optional.
std::weak_ptr<IPixelariumImage> pixelarium::resources::ImageResourcePool::GetResource(ResourceKey id) const std::weak_ptr<Image> pixelarium::resources::ImageResourcePool::GetResource(ResourceKey id) const
{ {
auto search{this->resources_.find(id)}; auto search{this->resources_.find(id)};
if (search == this->resources_.end()) return {}; if (search == this->resources_.end()) return {};
@@ -32,7 +34,7 @@ std::weak_ptr<IPixelariumImage> pixelarium::resources::ImageResourcePool::GetRes
/// @brief Sets a resource in the pool. /// @brief Sets a resource in the pool.
/// @param res A unique pointer to the resource to set. /// @param res A unique pointer to the resource to set.
/// @return The ID of the new resource. /// @return The ID of the new resource.
size_t pixelarium::resources::ImageResourcePool::SetResource(unique_ptr<IPixelariumImage> res) size_t pixelarium::resources::ImageResourcePool::SetResource(unique_ptr<Image> res)
{ {
if (res == nullptr) if (res == nullptr)
{ {
@@ -52,8 +54,7 @@ size_t pixelarium::resources::ImageResourcePool::SetResource(unique_ptr<IPixelar
/// @param id The ID of the resource to update. /// @param id The ID of the resource to update.
/// @param res A unique pointer to the new resource. /// @param res A unique pointer to the new resource.
/// @return True if the resource was updated, false otherwise. /// @return True if the resource was updated, false otherwise.
bool pixelarium::resources::ImageResourcePool::ModifyResource(ResourceKey id, bool pixelarium::resources::ImageResourcePool::ModifyResource(ResourceKey id, std::unique_ptr<Image> res)
std::unique_ptr<imaging::IPixelariumImage> res)
{ {
auto search{this->resources_.find(id)}; auto search{this->resources_.find(id)};
if (search == this->resources_.end()) return false; if (search == this->resources_.end()) return false;
@@ -80,7 +81,7 @@ bool pixelarium::resources::ImageResourcePool::DeleteResource(ResourceKey id)
/// @param func A function to call for each resource. The function should accept the resource ID and a const reference /// @param func A function to call for each resource. The function should accept the resource ID and a const reference
/// to a PixelariumImage. /// to a PixelariumImage.
void pixelarium::resources::ImageResourcePool::EnumerateResources( void pixelarium::resources::ImageResourcePool::EnumerateResources(
const std::function<void(ResourceKey, size_t, const imaging::IPixelariumImage&)>& func) const std::function<void(ResourceKey, size_t, const imaging::IPixelariumImage<cv::Mat>&)>& func)
{ {
size_t idx{0}; size_t idx{0};
for (const auto& e : this->resources_) for (const auto& e : this->resources_)
+9 -9
View File
@@ -38,7 +38,7 @@ concept ResT = requires(R& r) { static_cast<IResource&>(r); };
/// @brief Defines an interface for a resource pool /// @brief Defines an interface for a resource pool
/// @tparam ResT defines the resource type that is accepted by the pool /// @tparam ResT defines the resource type that is accepted by the pool
template <typename ResT> template <typename ResT, class Data>
class IResourcePool class IResourcePool
{ {
public: public:
@@ -48,7 +48,7 @@ class IResourcePool
virtual bool ModifyResource(ResourceKey id, std::unique_ptr<ResT> res) = 0; virtual bool ModifyResource(ResourceKey id, std::unique_ptr<ResT> res) = 0;
virtual bool DeleteResource(ResourceKey id) = 0; virtual bool DeleteResource(ResourceKey id) = 0;
virtual void EnumerateResources( virtual void EnumerateResources(
const std::function<void(ResourceKey, size_t, const imaging::IPixelariumImage&)>& func) = 0; const std::function<void(ResourceKey, size_t, const imaging::IPixelariumImage<Data>&)>& func) = 0;
virtual size_t GetTotalSize() const = 0; virtual size_t GetTotalSize() const = 0;
virtual void Clear() = 0; virtual void Clear() = 0;
}; };
@@ -58,7 +58,7 @@ class IResourcePool
// reside with the =ResourcePool=! // reside with the =ResourcePool=!
// In fact, the intention is, that there is no way back once the =ResourcePool= took ownership of an object. // In fact, the intention is, that there is no way back once the =ResourcePool= took ownership of an object.
// Callers can get references, but no ownership. A caller might delete a resource though. // Callers can get references, but no ownership. A caller might delete a resource though.
class ImageResourcePool : public IResourcePool<imaging::IPixelariumImage> class ImageResourcePool : public IResourcePool<imaging::IPixelariumImageCvMat, cv::Mat>
{ {
public: public:
ImageResourcePool() = default; ImageResourcePool() = default;
@@ -68,17 +68,17 @@ class ImageResourcePool : public IResourcePool<imaging::IPixelariumImage>
ImageResourcePool& operator=(ImageResourcePool&) = delete; ImageResourcePool& operator=(ImageResourcePool&) = delete;
ImageResourcePool& operator=(ImageResourcePool&&) = delete; ImageResourcePool& operator=(ImageResourcePool&&) = delete;
std::weak_ptr<imaging::IPixelariumImage> GetResource(ResourceKey id) const override; std::weak_ptr<imaging::IPixelariumImageCvMat> GetResource(ResourceKey id) const override;
ResourceKey SetResource(std::unique_ptr<imaging::IPixelariumImage> res) override; ResourceKey SetResource(std::unique_ptr<imaging::IPixelariumImageCvMat> res) override;
bool ModifyResource(ResourceKey id, std::unique_ptr<imaging::IPixelariumImage> res) override; bool ModifyResource(ResourceKey id, std::unique_ptr<imaging::IPixelariumImageCvMat> res) override;
bool DeleteResource(ResourceKey id) override; bool DeleteResource(ResourceKey id) override;
void Clear() override { this->resources_.clear(); } void Clear() override { this->resources_.clear(); }
void EnumerateResources( void EnumerateResources(
const std::function<void(ResourceKey, size_t, const imaging::IPixelariumImage&)>& func) override; const std::function<void(ResourceKey, size_t, const imaging::IPixelariumImage<cv::Mat>&)>& func) override;
template <typename Callable> template <typename Callable>
requires std::invocable<Callable, ResourceKey, size_t, const imaging::IPixelariumImage&> requires std::invocable<Callable, ResourceKey, size_t, const imaging::IPixelariumImageCvMat&>
void Enumerate(Callable&& func) const void Enumerate(Callable&& func) const
{ {
size_t idx{0}; size_t idx{0};
@@ -91,7 +91,7 @@ class ImageResourcePool : public IResourcePool<imaging::IPixelariumImage>
size_t GetTotalSize() const override { return resources_.size(); } size_t GetTotalSize() const override { return resources_.size(); }
private: private:
std::unordered_map<size_t, std::shared_ptr<imaging::IPixelariumImage>> resources_; std::unordered_map<size_t, std::shared_ptr<imaging::IPixelariumImageCvMat>> resources_;
std::mutex mut_; std::mutex mut_;
}; };
} // namespace pixelarium::resources } // namespace pixelarium::resources
+3 -1
View File
@@ -3,7 +3,9 @@ set(UTILSLIBNAME pixelariumutilslib)
set(UTILSLIBSRC set(UTILSLIBSRC
ILog.hpp ILog.hpp
SpdLogger.hpp SpdLogger.hpp
SpdLogger.cpp) SpdLogger.cpp
simple_thread_pool.hpp
simple_thread_pool.cpp)
add_library(${UTILSLIBNAME} STATIC ${UTILSLIBSRC}) add_library(${UTILSLIBNAME} STATIC ${UTILSLIBSRC})
+47
View File
@@ -0,0 +1,47 @@
#include "simple_thread_pool.hpp"
#include <functional>
#include <mutex>
using namespace pixelarium::utils;
simple_thread_pool::simple_thread_pool(size_t num_threads)
{
for (size_t i{0}; i < num_threads; ++i)
{
workers_.emplace_back(
[this]()
{
while (true)
{
std::function<void()> job;
{
std::unique_lock<std::mutex> lck(thread_mutex_);
cv_.wait(lck, [this]() -> bool { return shutdown_ || !task_queue_.empty(); });
if (shutdown_ && task_queue_.empty()) return;
job = std::move(task_queue_.front());
task_queue_.pop();
}
job();
}
});
}
}
simple_thread_pool::~simple_thread_pool()
{
{
std::unique_lock<std::mutex> lck(thread_mutex_);
shutdown_ = true;
}
cv_.notify_all();
for (auto& th : workers_)
{
th.join();
}
}
+56
View File
@@ -0,0 +1,56 @@
#pragma once
#include <condition_variable>
#include <functional>
#include <mutex>
#include <queue>
#include <thread>
#include <vector>
namespace pixelarium::utils
{
class simple_thread_pool
{
public:
explicit simple_thread_pool(size_t);
simple_thread_pool(simple_thread_pool&) = delete;
simple_thread_pool(const simple_thread_pool&) = delete;
simple_thread_pool(simple_thread_pool&&) = delete;
simple_thread_pool& operator=(simple_thread_pool&) = delete;
simple_thread_pool& operator=(simple_thread_pool&&) = delete;
~simple_thread_pool();
template <typename Callable>
requires std::invocable<Callable>
static auto run_asynch(Callable&& fun) -> void
{
simple_thread_pool::Global().enqueue(std::forward<Callable>(fun));
}
public:
template <typename Callable>
requires std::invocable<Callable>
auto enqueue(Callable&& fun) -> void
{
{
std::unique_lock<std::mutex> lck(thread_mutex_);
task_queue_.emplace(std::forward<Callable>(fun));
}
cv_.notify_one();
}
private:
static auto Global() -> simple_thread_pool&
{
const auto kThreadCount{std::thread::hardware_concurrency() * 2};
static simple_thread_pool global_instance(kThreadCount == 0 ? 5 : kThreadCount);
return global_instance;
}
std::vector<std::thread> workers_;
std::condition_variable cv_;
std::mutex thread_mutex_;
std::queue<std::function<void()>> task_queue_;
bool shutdown_{false};
};
} // namespace pixelarium::utils
+8 -7
View File
@@ -2,20 +2,21 @@
#include <algorithm> #include <algorithm>
#include "imaging/IPixelariumImage.hpp"
#include "resources/resource.hpp" #include "resources/resource.hpp"
namespace namespace
{ {
// A Mock implementation for tests requiring _any_ instance of a IPixelariumImage // A Mock implementation for tests requiring _any_ instance of a IPixelariumImage
class DummyImage : public pixelarium::imaging::IPixelariumImage class DummyImage : public pixelarium::imaging::IPixelariumImageCvMat
{ {
public: public:
std::unique_ptr<cv::Mat> TryGetImage() override { return {}; } std::optional<cv::Mat> TryGetImage() override { return {}; }
std::unique_ptr<cv::Mat> TryGetImage(const pixelarium::imaging::IImageQuery&) override { return {}; } std::optional<cv::Mat> TryGetImage(const pixelarium::imaging::IImageQuery&) override { return {}; }
std::vector<std::unique_ptr<cv::Mat>> TryGetImages(const pixelarium::imaging::IImageQuery&) override { return {}; } std::vector<std::optional<cv::Mat>> TryGetImages(const pixelarium::imaging::IImageQuery&) override { return {}; }
std::string Name() const noexcept override { return {}; } std::string Name() const noexcept override { return {}; }
@@ -43,7 +44,7 @@ TEST(ImageResourcePoolTest, SetWrappedRawPointerGet)
{ {
ImageResourcePool pool; ImageResourcePool pool;
auto img = new DummyImage(); auto img = new DummyImage();
auto id = pool.SetResource(std::unique_ptr<pixelarium::imaging::IPixelariumImage>(img)); auto id = pool.SetResource(std::unique_ptr<pixelarium::imaging::IPixelariumImageCvMat>(img));
auto res = pool.GetResource(id); auto res = pool.GetResource(id);
auto res_img = res.lock(); auto res_img = res.lock();
EXPECT_NE(res_img, nullptr); EXPECT_NE(res_img, nullptr);
@@ -95,7 +96,7 @@ TEST(ImageResourcePoolTest, EnumerateResources)
auto id2 = pool.SetResource(std::make_unique<DummyImage>()); auto id2 = pool.SetResource(std::make_unique<DummyImage>());
std::vector<size_t> found_ids{}; std::vector<size_t> found_ids{};
pool.EnumerateResources([&found_ids](size_t id, size_t, const pixelarium::imaging::IPixelariumImage&) pool.EnumerateResources([&found_ids](size_t id, size_t, const pixelarium::imaging::IPixelariumImage<cv::Mat>&)
{ found_ids.push_back(id); }); { found_ids.push_back(id); });
EXPECT_EQ(found_ids.size(), 2); EXPECT_EQ(found_ids.size(), 2);
@@ -110,7 +111,7 @@ TEST(ImageResourcePoolTest, TemplatedEnumerate)
auto id2 = pool.SetResource(std::make_unique<DummyImage>()); auto id2 = pool.SetResource(std::make_unique<DummyImage>());
std::vector<size_t> found_ids{}; std::vector<size_t> found_ids{};
pool.Enumerate([&found_ids](size_t id, size_t, const pixelarium::imaging::IPixelariumImage&) pool.Enumerate([&found_ids](size_t id, size_t, const pixelarium::imaging::IPixelariumImage<cv::Mat>&)
{ found_ids.push_back(id); }); { found_ids.push_back(id); });
EXPECT_EQ(found_ids.size(), 2); EXPECT_EQ(found_ids.size(), 2);