Gerneric image codec abstraction init (#6)

* remove raw pointer from resource manager

* towards generic images

* 💅 and pin libCZI module

* remove raw pointer from resource manager

* towards generic images

* fix rendering

* fix rendering

* fix unit tests

* fix pipeline

* fix gcc build

* re-enable tests

* add czi impl

* remove resource button

* refactor user code app to being a "default app"

* ui resources

* missing lib?

* init czi render support

* typos
This commit is contained in:
m-aXimilian
2025-09-22 23:13:28 +02:00
committed by Maximilian Kueffner
parent bce12b0bb4
commit 0be064bb8e
31 changed files with 670 additions and 221 deletions
+6 -1
View File
@@ -4,6 +4,7 @@ configure_file(${CMAKE_CURRENT_SOURCE_DIR}/app_resources_default.h.in
set(APPLIBSRC
AppGLFW.cpp
DefaultApp.cpp
${imgui_DIR}/imgui.cpp
${imgui_DIR}/imgui_demo.cpp
${imgui_DIR}/imgui_draw.cpp
@@ -18,7 +19,9 @@ add_library(${APPLIBNAME}
STATIC ${APPLIBSRC})
target_link_libraries(${APPLIBNAME}
PRIVATE pixelariumutilslib)
PRIVATE pixelariumutilslib
PRIVATE pixelariumimagelib
PRIVATE pixelariumrenderlib)
# This needs to be public to let the consumer know about it.
if(WIN32)
@@ -40,5 +43,7 @@ endif()
target_include_directories(${APPLIBNAME}
PRIVATE ${CMAKE_BINARY_DIR}
PRIVATE ${PROJECT_SOURCE_DIR}/lib
PRIVATE ${PROJECT_SOURCE_DIR}/lib/imaging
PUBLIC ${pfd_DIR}
PUBLIC ${imgui_DIR}
PUBLIC ${imgui_DIR}/backends)
+139
View File
@@ -0,0 +1,139 @@
#include "DefaultApp.hpp"
#include <cstddef>
#include <format>
#include <memory>
#include "PixelariumImageFactory.hpp"
#include "app_resources_default.h"
#include "imgui.h"
#include "portable-file-dialogs.h"
#include "rendering/RenderImageManager.hpp"
#include "resources/resource.hpp"
#include "utilities/ILog.hpp"
using namespace pixelarium::imaging;
void pixelarium::ui::DefaultApp::MenuBarOptionsColumn1()
{
ImGui::MenuItem(SHOWIMGUIDEMOS, NULL, &this->demop_);
ImGui::MenuItem(SHOWIMAGEGALLERY, NULL, &this->image_listp_);
}
void pixelarium::ui::DefaultApp::MenuBarOptionsColumn2()
{
if (ImGui::BeginMenu(FILEMENUNAME))
{
if (ImGui::MenuItem(LOADIMAGE))
{
this->LoadImage();
}
ImGui::EndMenu();
}
}
void pixelarium::ui::DefaultApp::Run()
{
if (demop_) ImGui::ShowDemoWindow(&this->demop_);
if (image_listp_) this->ImageGalleryRender();
this->RenderImages();
}
void pixelarium::ui::DefaultApp::RenderImages()
{
this->render_manager_->Enumerate(
[&](resources::ResourceKey key, render::RenderImageStateWrapper& render_state)
{
render_state.view->ShowImage();
if (!*render_state.view->GetStatus())
{
this->render_manager_->MarkForDeletion(key);
}
});
}
void pixelarium::ui::DefaultApp::ImageGalleryRender()
{
ImGui::SetNextWindowSize(ImVec2(300, 500));
ImGui::Begin(SHOWIMAGEGALLERY, &this->image_listp_);
// this updates the render collection
// essentially deleting render views that were
// marked for deletion
this->render_manager_->UpdateCollection();
static size_t selected_index{0};
int highlight_index{-1};
if (ImGui::BeginListBox("Image List", ImVec2(200, 400)))
{
pool_.EnumerateResources(
[&](size_t id, size_t idx, const imaging::IPixelariumImage& img) -> void
{
const bool is_selected = selected_index == idx;
if (ImGui::Selectable(std::format("{}", img.Name()).c_str(), is_selected))
{
selected_index = idx;
this->selected_image_ = id;
}
if (highlight_index && ImGui::IsItemHovered()) highlight_index = idx;
if (is_selected) ImGui::SetItemDefaultFocus();
});
ImGui::EndListBox();
}
ImGui::Checkbox(AUTOSHOWSELECTED, &this->auto_show_selectd_image_);
ImGui::SameLine(); // put the button next to the checkbox
// note that the button will only show when the checkbox is toggled off
// this is intended behavior as the selected image will render automatically
// when the checkbox is toggled on
if (this->auto_show_selectd_image_ || ImGui::Button(OPENIMAGE))
{
// Try add the selected index to the manager
this->render_manager_->Add(this->selected_image_);
}
if (ImGui::Button(LOADIMAGE))
{
this->LoadImage();
}
if (ImGui::Button(REMOVEIMAGE))
{
this->render_manager_->MarkForDeletion(this->selected_image_);
this->pool_.DeleteResource(this->selected_image_);
}
ImGui::SameLine();
if (ImGui::Button(CLEARALL))
{
this->render_manager_->Clear();
this->pool_.Clear();
}
ImGui::End(); // end gallery window
}
void pixelarium::ui::DefaultApp::LoadImage()
{
size_t last_id{};
auto res{pfd::open_file("Load Inputs", pfd::path::home(), {"All Files", "*"}, pfd::opt::multiselect).result()};
for (auto& p : res)
{
this->logger_.Debug(std::format("{}: Creating image {}", __FUNCTION__, p));
try
{
pool_.SetResource(PixelariumImageFactory::CreateImage(p));
}
catch (pixelarium::resources::empty_resource_exception& e)
{
logger_.Error(std::format("{}: Failed to load file '{}' with '{}'", __PRETTY_FUNCTION__, p, e.what()));
}
}
}
+43
View File
@@ -0,0 +1,43 @@
#pragma once
#include <cstddef>
#include <memory>
#include "AppGLFW.hpp"
#include "imgui.h"
#include "rendering/RenderImageManager.hpp"
#include "resources/resource.hpp"
#include "utilities/ILog.hpp"
namespace pixelarium::ui
{
class DefaultApp : public application::AppGLFW
{
public:
DefaultApp(const utils::log::ILog& log, pixelarium::resources::ImageResourcePool& pool)
: application::AppGLFW(log),
pool_(pool),
render_manager_(std::make_unique<render::RenderImageManager>(pool, log))
{
}
protected:
void MenuBarOptionsColumn1() override;
void MenuBarOptionsColumn2() override;
void Run() override;
private:
void LoadImage();
void ImageGalleryRender();
void RenderImages();
private:
resources::ImageResourcePool& pool_;
std::unique_ptr<render::RenderImageManager> render_manager_;
bool image_listp_{true};
bool auto_show_selectd_image_{true};
bool demop_{false};
ImVec2 curr_dim_;
size_t selected_image_{0};
};
} // namespace pixelarium::ui
+12 -4
View File
@@ -5,9 +5,17 @@
#cmakedefine PIXELARIUM_TITLE "@PIXELARIUM_TITLE@"
#define MAINMENUNAME "Menu"
#define FILEMENUNAME "File"
#define LOGLEVELSELECT "Log Level"
// clang-format off
#define MAINMENUNAME "Menu"
#define FILEMENUNAME "File"
#define LOGLEVELSELECT "Log Level"
#define SHOWIMGUIDEMOS "ImGui Demos"
#define SHOWIMAGEGALLERY "Image Gallery"
#define AUTOSHOWSELECTED "Auto View"
#define OPENIMAGE "Open"
#define LOADIMAGE "Load Image"
#define REMOVEIMAGE "Remove Image"
#define CLEARALL "Clear All"
// clang-format on
inline constexpr std::array<std::string_view, 5> LOGLEVELS = {"Trace", "Debug", "Info", "Warning", "Error"};
+6 -1
View File
@@ -6,7 +6,12 @@ message(STATUS "Found opencv: " ${OpenCV_INCLUDE_DIRS})
message(STATUS "OpenCV_LIBs from: " ${OpenCV_LIBS})
set(IMAGELIBSRC
PixelariumImage.cpp)
IPixelariumImage.hpp
PixelariumImageFactory.cpp
impl/PixelariumJpg.cpp
impl/PixelariumPng.cpp
impl/PixelariumCzi.cpp
)
set(IMAGELIBLIBNAME pixelariumimagelib)
+55
View File
@@ -0,0 +1,55 @@
#pragma once
#include <filesystem>
#include <functional>
#include <memory>
#include <opencv2/core/mat.hpp>
#include <optional>
#include <string>
namespace pixelarium::imaging
{
using ImageQueryFunctor = std::function<void(const std::string&, void*, int*)>;
enum class ImageFileType
{
UNKNOWN = -10,
ABSTRACT = 0,
PNG = 1,
JPG = 2,
CZI = 3,
};
/// @brief An abstract interface to define a semantic query
/// for a multi-dimensional image.
struct IImageQuery
{
virtual ~IImageQuery() = default;
};
/// @brief This aims to be a generic image abstraction
/// meant for codec specific implementation.
class IPixelariumImage
{
public:
virtual ~IPixelariumImage() = default;
// this will have to throw or something for multidimensional images
virtual std::optional<std::unique_ptr<cv::Mat>> TryGetImage() = 0;
virtual std::optional<std::unique_ptr<cv::Mat>> TryGetImage(const IImageQuery&) = 0;
virtual std::string Name() const noexcept = 0;
virtual bool Empty() const noexcept = 0;
virtual std::filesystem::path Uri() const noexcept = 0;
public:
const static ImageFileType type_{ImageFileType::ABSTRACT};
protected:
std::filesystem::path uri_;
};
} // namespace pixelarium::imaging
-18
View File
@@ -1,18 +0,0 @@
#include "PixelariumImage.hpp"
#include <filesystem>
#include <format>
#include <memory>
#include <opencv2/imgcodecs.hpp>
#include <stdexcept>
pixelarium::imaging::PixelariumImage::PixelariumImage(const std::string& uri)
{
if (!std::filesystem::exists(uri))
{
throw std::runtime_error(std::format("File not {} found", uri));
}
this->uri_ = std::filesystem::path(uri);
this->img_ = std::make_unique<cv::Mat>(cv::imread(uri));
}
-85
View File
@@ -1,85 +0,0 @@
#pragma once
#include <filesystem>
#include <functional>
#include <memory>
#include <opencv2/core/mat.hpp>
#include <string>
namespace pixelarium::imaging
{
using AccessorFunctor = std::function<void(const std::string&, void*, int*)>;
enum class ImageFileType
{
ABSRACT = 0,
PNG = 1,
JPG = 2,
CZI = 3,
};
/// @brief This aims to be a generic image abstraction
/// meant for codec specific implementation.
/// Todo: the above implies that most of the below implementations don't make sense for this class (c.f. cv::Mat
/// generation et.al.)
class PixelariumImage
{
public:
// get back the defaults
// this means, that there has to be and API option to set
// a resource which should trigger some sort of action
// after setting
explicit PixelariumImage(const std::string& uri);
PixelariumImage() = default;
PixelariumImage(const PixelariumImage& other)
{
// be ware!!
// we make a copy of the image data here!
img_ = std::make_unique<cv::Mat>(*other.img_);
uri_ = other.uri_;
};
PixelariumImage(PixelariumImage&& other) noexcept : img_(std::move(other.img_)) {}
PixelariumImage& operator=(const PixelariumImage& other) = delete;
PixelariumImage& operator=(PixelariumImage&& other) noexcept
{
if (this != &other)
{
img_ = std::move(other.img_);
uri_ = other.uri_;
}
return *this;
}
// this should probably vanish as it makes no sense
// for multidimensional images (more than one frame)
// -> we need some sort of accessor functionality
~PixelariumImage() = default;
const cv::Mat& GetImage() const { return *this->img_.get(); }
const std::string Name() const noexcept
{
if (!this->uri_.empty())
{
return this->uri_.filename().string();
}
return {};
}
bool Empty() const noexcept { return this->img_->empty(); }
static ImageFileType Type() { return PixelariumImage::type_; }
protected:
std::unique_ptr<cv::Mat> img_;
std::filesystem::path uri_;
static ImageFileType type_;
};
} // namespace pixelarium::imaging
+56
View File
@@ -0,0 +1,56 @@
#include "PixelariumImageFactory.hpp"
#include <algorithm>
#include <cctype>
#include <memory>
#include "impl/PixelariumCzi.hpp"
#include "impl/PixelariumJpg.hpp"
#include "impl/PixelariumPng.hpp"
constexpr pixelarium::imaging::ImageFileType ExtensionToType(const std::string& extension)
{
std::string lower_ext{extension};
std::ranges::transform(extension, lower_ext.begin(), [](const char c) -> char { return std::tolower(c); });
if (lower_ext == ".jpg" || lower_ext == ".jpeg")
{
return pixelarium::imaging::ImageFileType::JPG;
}
if (lower_ext == ".png")
{
return pixelarium::imaging::ImageFileType::PNG;
}
if (lower_ext == ".czi")
{
return pixelarium::imaging::ImageFileType::CZI;
}
return pixelarium::imaging::ImageFileType::UNKNOWN;
}
/*static*/ std::unique_ptr<pixelarium::imaging::IPixelariumImage>
pixelarium::imaging::PixelariumImageFactory::CreateImage(const std::string& uri)
{
const auto res{std::filesystem::path(uri)};
const auto target_type{ExtensionToType(res.extension().string())};
switch (target_type)
{
case ImageFileType::UNKNOWN:
return {};
break;
case ImageFileType::ABSTRACT:
return {};
break;
case ImageFileType::PNG:
return std::make_unique<PixelariumPng>(uri);
break;
case ImageFileType::JPG:
return std::make_unique<PixelariumJpg>(uri);
break;
case ImageFileType::CZI:
return std::make_unique<PixelariumCzi>(uri);
break;
}
}
+13
View File
@@ -0,0 +1,13 @@
#pragma once
#include <memory>
#include "IPixelariumImage.hpp"
namespace pixelarium::imaging
{
class PixelariumImageFactory
{
public:
static std::unique_ptr<IPixelariumImage> CreateImage(const std::string& uri);
};
} // namespace pixelarium::imaging
+105
View File
@@ -0,0 +1,105 @@
#include "PixelariumCzi.hpp"
#include <filesystem>
#include <stdexcept>
#include "libCZI.h"
std::unique_ptr<cv::Mat> CZISubBlockToCvMat(std::shared_ptr<libCZI::IBitmapData> bitmap, libCZI::PixelType pixeltype)
{
size_t pixel_size{0};
int target_type;
switch (pixeltype)
{
case libCZI::PixelType::Gray8:
pixel_size = 1;
target_type = CV_8U;
break;
case libCZI::PixelType::Gray16:
pixel_size = 2;
target_type = CV_16U;
break;
case libCZI::PixelType::Bgr24:
pixel_size = 3;
target_type = CV_8UC3;
break;
case libCZI::PixelType::Bgra32:
target_type = CV_8UC4;
case libCZI::PixelType::Gray32:
target_type = CV_32S;
case libCZI::PixelType::Gray32Float:
target_type = CV_32F;
pixel_size = 4;
break;
// case libCZI::PixelType::Gray64ComplexFloat:
// case libCZI::PixelType::Gray64Float:
// target_type =
// pixel_size = 8;
// break;
default:
pixel_size = -1;
break;
}
if (pixel_size < 0) return nullptr;
size_t height{bitmap->GetHeight()};
size_t width{bitmap->GetWidth()};
auto fill_mat = std::make_unique<cv::Mat>(height, width, target_type);
auto bitmap_info = bitmap->Lock();
for (size_t h{0}; h < height; ++h)
{
unsigned char* source_row = ((unsigned char*)bitmap_info.ptrDataRoi) + bitmap_info.stride * h;
unsigned char* target_row = fill_mat->ptr(h);
for (size_t w{0}; w < width; ++w)
{
switch (pixel_size)
{
case 1:
target_row[w] = source_row[w];
break;
case 2:
reinterpret_cast<unsigned short*>(target_row)[w] = reinterpret_cast<unsigned short*>(source_row)[w];
break;
case 3:
target_row[3 * w] = source_row[3 * w];
target_row[3 * w + 1] = source_row[3 * w + 1];
target_row[3 * w + 2] = source_row[3 * w + 2];
break;
default:
throw std::runtime_error("Unknown pixel type requested!");
break;
}
}
}
bitmap->Unlock();
return fill_mat;
}
pixelarium::imaging::PixelariumCzi::PixelariumCzi(const std::string& uri)
{
if (!std::filesystem::exists(uri))
{
throw std::runtime_error("Render file not found.");
}
this->is_empty_ = false;
this->uri_ = std::filesystem::path(uri);
}
std::optional<std::unique_ptr<cv::Mat>> pixelarium::imaging::PixelariumCzi::TryGetImage()
{
auto stream = libCZI::CreateStreamFromFile(this->uri_.wstring().c_str());
auto cziReader = libCZI::CreateCZIReader();
cziReader->Open(stream);
auto block = cziReader->ReadSubBlock(0);
auto bitmap = block->CreateBitmap();
auto res = CZISubBlockToCvMat(bitmap, block->GetSubBlockInfo().pixelType);
return res;
}
+46
View File
@@ -0,0 +1,46 @@
#pragma once
#include <string>
#include "../IPixelariumImage.hpp"
namespace pixelarium::imaging
{
class PixelariumCzi : public IPixelariumImage
{
public:
explicit PixelariumCzi(const std::string& uri);
// IPixelariumImage member implementations
public:
std::optional<std::unique_ptr<cv::Mat>> TryGetImage() override;
std::optional<std::unique_ptr<cv::Mat>> TryGetImage(const IImageQuery&) override
{
// ToDo: proper error
throw std::runtime_error("Not implemented.");
}
std::string Name() const noexcept override
{
if (!this->uri_.empty())
{
return this->uri_.filename().string();
}
return {};
}
std::filesystem::path Uri() const noexcept override { return this->uri_.string(); }
bool Empty() const noexcept override { return this->is_empty_; }
public:
const static ImageFileType type_{ImageFileType::CZI};
private:
// this should be set by each image getter
// after a new cv::Mat could be instantiated
bool is_empty_{true};
};
} // namespace pixelarium::imaging
+34
View File
@@ -0,0 +1,34 @@
#include "PixelariumJpg.hpp"
#include <filesystem>
#include <memory>
#include <opencv2/imgcodecs.hpp>
#include <string>
pixelarium::imaging::PixelariumJpg::PixelariumJpg(const std::string& uri)
{
if (!std::filesystem::exists(uri))
{
throw std::runtime_error("Render file not found.");
}
this->is_empty_ = false;
this->uri_ = std::filesystem::path(uri);
}
std::optional<std::unique_ptr<cv::Mat>> pixelarium::imaging::PixelariumJpg::TryGetImage()
{
try
{
auto img = std::make_unique<cv::Mat>(cv::imread(this->uri_.string()));
this->is_empty_ = false;
return img;
}
catch (const std::exception& e)
{
this->is_empty_ = true;
return {};
}
}
+50
View File
@@ -0,0 +1,50 @@
#pragma once
#include <stdexcept>
#include <string>
#include "../IPixelariumImage.hpp"
namespace pixelarium::imaging
{
class PixelariumJpg : public IPixelariumImage
{
public:
explicit PixelariumJpg(const std::string& url);
// IPixelariumImage member implementations
public:
std::optional<std::unique_ptr<cv::Mat>> TryGetImage() override;
std::optional<std::unique_ptr<cv::Mat>> TryGetImage(const IImageQuery&) override
{
// ToDo: proper error
throw std::runtime_error("Not possible with jpg.");
}
std::string Name() const noexcept override
{
if (!this->uri_.empty())
{
return this->uri_.filename().string();
}
return {};
}
std::filesystem::path Uri() const noexcept override
{
return this->uri_.string();
}
bool Empty() const noexcept override { return this->is_empty_; }
public:
const static ImageFileType type_{ImageFileType::JPG};
private:
// this should be set by each image getter
// after a new cv::Mat could be instantiated
bool is_empty_{true};
};
} // namespace pixelarium::imaging
+34
View File
@@ -0,0 +1,34 @@
#include "PixelariumPng.hpp"
#include <filesystem>
#include <memory>
#include <opencv2/imgcodecs.hpp>
#include <string>
pixelarium::imaging::PixelariumPng::PixelariumPng(const std::string& uri)
{
if (!std::filesystem::exists(uri))
{
throw std::runtime_error("Render file not found.");
}
this->is_empty_ = false;
this->uri_ = std::filesystem::path(uri);
}
std::optional<std::unique_ptr<cv::Mat>> pixelarium::imaging::PixelariumPng::TryGetImage()
{
try
{
auto img = std::make_unique<cv::Mat>(cv::imread(this->uri_.string()));
this->is_empty_ = false;
return img;
}
catch (const std::exception& e)
{
this->is_empty_ = true;
return {};
}
}
+50
View File
@@ -0,0 +1,50 @@
#pragma once
#include <stdexcept>
#include <string>
#include "../IPixelariumImage.hpp"
namespace pixelarium::imaging
{
class PixelariumPng : public IPixelariumImage
{
public:
explicit PixelariumPng(const std::string& url);
// IPixelariumImage member implementations
public:
std::optional<std::unique_ptr<cv::Mat>> TryGetImage() override;
std::optional<std::unique_ptr<cv::Mat>> TryGetImage(const IImageQuery&) override
{
// ToDo: proper error
throw std::runtime_error("Not possible with png.");
}
std::string Name() const noexcept override
{
if (!this->uri_.empty())
{
return this->uri_.filename().string();
}
return {};
}
std::filesystem::path Uri() const noexcept override
{
return this->uri_.string();
}
bool Empty() const noexcept override { return this->is_empty_; }
public:
const static ImageFileType type_{ImageFileType::PNG};
private:
// this should be set by each image getter
// after a new cv::Mat could be instantiated
bool is_empty_{true};
};
} // namespace pixelarium::imaging
+39 -5
View File
@@ -5,13 +5,13 @@
#include <opencv2/imgproc.hpp>
#include <stdexcept>
#include "imaging/PixelariumImage.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::render::CvMatRender::CvMatRender(const std::shared_ptr<PixelariumImage>& img) : base_(img), texture_(0)
pixelarium::render::CvMatRender::CvMatRender(std::shared_ptr<pixelarium::imaging::IPixelariumImage>& 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
@@ -33,7 +33,7 @@ pixelarium::render::CvMatRender::~CvMatRender()
/// @brief Resets the render image with a new PixelariumImage.
/// @param img A shared pointer to the new PixelariumImage.
void pixelarium::render::CvMatRender::ResetRenderImage(const std::shared_ptr<pixelarium::imaging::PixelariumImage>& img)
void pixelarium::render::CvMatRender::ResetRenderImage(std::shared_ptr<pixelarium::imaging::IPixelariumImage>& img)
{
this->base_ = img;
this->ResetRenderImage();
@@ -97,7 +97,11 @@ GLuint pixelarium::render::CvMatRender::Render() { return this->uploadTexture();
/// @return The ID of the OpenGL texture.
GLuint pixelarium::render::CvMatRender::Render(float factor)
{
cv::resize(this->base_->GetImage(), this->img_, cv::Size(0, 0), factor, factor, cv::INTER_LINEAR_EXACT);
auto res_val {this->base_->TryGetImage()};
if (res_val.has_value())
{
cv::resize(*res_val.value(), this->img_, cv::Size(0, 0), factor, factor, cv::INTER_LINEAR_EXACT);
}
return this->uploadTexture();
}
@@ -108,7 +112,14 @@ GLuint pixelarium::render::CvMatRender::Render(float factor)
/// @return The ID of the OpenGL texture.
GLuint pixelarium::render::CvMatRender::Render(size_t width, size_t height)
{
const auto sz{this->base_->GetImage().size()};
auto res_val {this->base_->TryGetImage()};
if (!res_val.has_value())
{
return this->Render(1.0f);
}
const auto sz{res_val.value()->size()};
const auto get_factor = [](auto opt1, auto opt2) -> float { return opt1 < opt2 ? opt1 : opt2; };
@@ -116,3 +127,26 @@ GLuint pixelarium::render::CvMatRender::Render(size_t width, size_t height)
return this->Render(factor);
}
void pixelarium::render::CvMatRender::ResetRenderImage()
{
if (this->base_ == nullptr)
{
return;
}
auto root_res = this->base_->TryGetImage();
if (!root_res.has_value())
{
return;
}
if (root_res.value() == nullptr)
{
return;
}
// we copy here
this->img_ = (*root_res.value()).clone();
}
+5 -5
View File
@@ -13,7 +13,7 @@
#endif
#include <GLFW/glfw3.h> // Will drag system OpenGL headers
#endif
#include "imaging/PixelariumImage.hpp"
#include "imaging/IPixelariumImage.hpp"
// clang-format on
namespace pixelarium::render
@@ -33,18 +33,18 @@ class CvMatRender
CvMatRender& operator=(CvMatRender&) = default;
CvMatRender& operator=(CvMatRender&& other) = default;
~CvMatRender();
explicit CvMatRender(const std::shared_ptr<pixelarium::imaging::PixelariumImage>& img);
explicit CvMatRender(std::shared_ptr<pixelarium::imaging::IPixelariumImage>& img);
public:
GLuint Render();
GLuint Render(float factor);
GLuint Render(size_t width, size_t height);
void ResetRenderImage() { this->img_ = this->base_->GetImage().clone(); }
void ResetRenderImage(const std::shared_ptr<pixelarium::imaging::PixelariumImage>& img);
void ResetRenderImage();
void ResetRenderImage(std::shared_ptr<pixelarium::imaging::IPixelariumImage>& img);
private:
cv::Mat img_;
std::shared_ptr<pixelarium::imaging::PixelariumImage> base_;
std::shared_ptr<pixelarium::imaging::IPixelariumImage> base_;
GLuint texture_;
GLuint uploadTexture();
+11 -4
View File
@@ -9,14 +9,21 @@
std::unique_ptr<pixelarium::render::PixelariumImageView> pixelarium::render::ImageViewFactory::RenderImage(
size_t image_id)
{
auto img{this->image_pool_.GetResource(image_id)};
auto res{this->image_pool_.GetResource(image_id)};
if (!img.has_value() || img.value()->Empty())
if (!res.has_value())
{
return nullptr;
return {};
}
auto img {res.value().lock()};
if (img->Empty())
{
return {};
}
// beware: here we copy the actual image resource over to the new image
return std::make_unique<PixelariumImageView>(std::make_shared<Image>(*img.value()));
return std::make_unique<PixelariumImageView>(img);
}
+1 -2
View File
@@ -1,13 +1,12 @@
#pragma once
#include "PixelariumImageView.hpp"
#include "imaging/PixelariumImage.hpp"
#include "resources/resource.hpp"
namespace pixelarium::render
{
class ImageViewFactory
{
using Image = imaging::PixelariumImage;
using Image = imaging::IPixelariumImage;
using Pool = resources::ImageResourcePool;
public:
+14 -8
View File
@@ -2,6 +2,7 @@
#include <format>
#include "imaging/IPixelariumImage.hpp"
#include "imgui.h"
/// @brief Checks if the dimensions of two ImVec2 vectors have changed significantly.
@@ -23,14 +24,14 @@ static bool dim_changed_p(const ImVec2& ref_rect, const ImVec2& new_rect)
/// @param img The input image.
/// @param curr_dim The current dimensions.
/// @return The calculated dimensions maintaining aspect ratio.
ImVec2 aspect_const_dimensions(const pixelarium::imaging::PixelariumImage& img, const ImVec2& curr_dim)
ImVec2 aspect_const_dimensions(const ImVec2& raw_dim, const ImVec2& curr_dim)
{
const auto w_fact = (static_cast<float>(curr_dim.x) / img.GetImage().cols);
const auto h_fact = (static_cast<float>(curr_dim.y) / img.GetImage().rows);
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(img.GetImage().cols * fact, img.GetImage().rows * fact);
return ImVec2(raw_dim.x * fact, raw_dim.y * fact);
}
/// @brief Displays the image in an ImGui window.
@@ -40,13 +41,18 @@ ImVec2 aspect_const_dimensions(const pixelarium::imaging::PixelariumImage& img,
/// fit the available window space. The raw and rendered dimensions are displayed below the image.
void pixelarium::render::PixelariumImageView::ShowImage()
{
if (this->img_ == nullptr || this->img_->Empty() || this->img_->Name().empty())
if (!this->cached_image_.has_value())
{
this->cached_image_ = this->img_->TryGetImage();
}
if (this->img_->Empty() || this->img_->type_ == imaging::ImageFileType::UNKNOWN || !cached_image_.has_value() || this->img_->Name().empty())
{
// do nothing
return;
}
ImGui::Begin(img_->Name().c_str(), &this->open_p, ImGuiWindowFlags_HorizontalScrollbar | ImGuiWindowFlags_MenuBar);
ImGui::Begin(this->img_->Name().c_str(), &this->open_p, ImGuiWindowFlags_HorizontalScrollbar | ImGuiWindowFlags_MenuBar);
this->curr_dim_ = ImGui::GetContentRegionAvail();
auto new_dim = ImGui::GetContentRegionAvail();
@@ -57,10 +63,10 @@ void pixelarium::render::PixelariumImageView::ShowImage()
this->curr_dim_ = new_dim;
ImVec2 dim(this->img_->GetImage().cols, this->img_->GetImage().rows);
ImVec2 dim(cached_image_.value()->cols, cached_image_.value()->rows);
ImGui::Image(reinterpret_cast<ImTextureID>(reinterpret_cast<void*>(texture)),
aspect_const_dimensions(*this->img_, new_dim));
aspect_const_dimensions(dim, new_dim));
ImGui::Separator();
ImGui::Text("%s", std::format(" Raw Dimensions W : {}, H: {}", dim.x, dim.y).c_str());
+5 -4
View File
@@ -2,7 +2,7 @@
#include <memory>
#include "imaging/PixelariumImage.hpp"
#include "imaging/IPixelariumImage.hpp"
#include "imgui.h"
#include "rendering/CvMatRender.hpp"
@@ -10,11 +10,11 @@ namespace pixelarium::render
{
class PixelariumImageView
{
using Image = imaging::PixelariumImage;
using Image = imaging::IPixelariumImage;
using Render = render::CvMatRender;
public:
explicit PixelariumImageView(const std::shared_ptr<Image>& img) : img_(img) { render_ = Render(img_); }
explicit PixelariumImageView(std::shared_ptr<Image> img) : img_(img) { render_ = Render(img_); }
PixelariumImageView() = delete;
PixelariumImageView(PixelariumImageView&) = delete;
PixelariumImageView(const PixelariumImageView&) = delete;
@@ -27,7 +27,8 @@ class PixelariumImageView
void ShowImage();
private:
const std::shared_ptr<Image> img_;
std::shared_ptr<Image> img_;
std::optional<std::unique_ptr<cv::Mat>> cached_image_ {};
Render render_;
bool open_p{true};
ImVec2 curr_dim_{};
+11 -6
View File
@@ -6,7 +6,7 @@
#include <mutex>
#include <optional>
using pixelarium::imaging::PixelariumImage;
using pixelarium::imaging::IPixelariumImage;
using namespace std;
namespace
@@ -22,19 +22,24 @@ size_t GenerateId() { return id_.fetch_add(1, memory_order_relaxed); }
/// @brief Retrieves a resource from the pool.
/// @param id The ID of the resource to retrieve.
/// @return A pointer to the resource if found, otherwise an empty optional.
std::optional<const PixelariumImage*> pixelarium::resources::ImageResourcePool::GetResource(ResourceKey id) const
std::optional<std::weak_ptr<IPixelariumImage>> pixelarium::resources::ImageResourcePool::GetResource(ResourceKey id) const
{
auto search{this->resources_.find(id)};
if (search == this->resources_.end()) return std::nullopt;
return search->second.get();
return search->second;
}
/// @brief Sets a resource in the pool.
/// @param res A unique pointer to the resource to set.
/// @return The ID of the new resource.
size_t pixelarium::resources::ImageResourcePool::SetResource(unique_ptr<PixelariumImage> res)
size_t pixelarium::resources::ImageResourcePool::SetResource(unique_ptr<IPixelariumImage> res)
{
if (res == nullptr)
{
throw empty_resource_exception();
}
auto key{::GenerateId()};
{
std::lock_guard<std::mutex> guard(this->mut_);
@@ -49,7 +54,7 @@ size_t pixelarium::resources::ImageResourcePool::SetResource(unique_ptr<Pixelari
/// @param res A unique pointer to the new resource.
/// @return True if the resource was updated, false otherwise.
bool pixelarium::resources::ImageResourcePool::ModifyResource(ResourceKey id,
std::unique_ptr<imaging::PixelariumImage> res)
std::unique_ptr<imaging::IPixelariumImage> res)
{
auto search{this->resources_.find(id)};
if (search == this->resources_.end()) return false;
@@ -76,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
/// to a PixelariumImage.
void pixelarium::resources::ImageResourcePool::EnumerateResources(
const std::function<void(ResourceKey, size_t, const imaging::PixelariumImage&)>& func)
const std::function<void(ResourceKey, size_t, const imaging:: IPixelariumImage&)>& func)
{
size_t idx{0};
for (const auto& e : this->resources_)
+26 -11
View File
@@ -7,11 +7,22 @@
#include <optional>
#include <unordered_map>
#include "imaging/PixelariumImage.hpp"
#include "imaging/IPixelariumImage.hpp"
namespace pixelarium::resources
{
using ResourceKey = size_t;
using ResourceKey = size_t;
struct empty_resource_exception : public std::exception
{
empty_resource_exception() {};
empty_resource_exception(const char* msg) : message_(msg) {};
const char* what() { return message_; }
private:
const char* message_ = "Empty Resource";
};
struct IResource
{
virtual ~IResource() = default;
@@ -25,12 +36,14 @@ class IResourcePool
{
public:
virtual ~IResourcePool() = default;
virtual std::optional<const ResT*> GetResource(size_t id) const = 0;
virtual std::optional<std::weak_ptr<ResT>> GetResource(size_t id) const = 0;
virtual ResourceKey SetResource(std::unique_ptr<ResT> res) = 0;
virtual bool ModifyResource(ResourceKey id, std::unique_ptr<ResT> res) = 0;
virtual bool DeleteResource(ResourceKey id) = 0;
virtual void EnumerateResources(const std::function<void(ResourceKey, size_t, const imaging::PixelariumImage&)>& func) = 0;
virtual void EnumerateResources(
const std::function<void(ResourceKey, size_t, const imaging::IPixelariumImage&)>& func) = 0;
virtual size_t GetTotalSize() const = 0;
virtual void Clear() = 0;
};
// Now with the =GetResource= method, I do not want to transfer ownership to the caller of that method. The ownership
@@ -38,7 +51,7 @@ class IResourcePool
// reside with the =ResourcePool=!
// 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.
class ImageResourcePool : public IResourcePool<imaging::PixelariumImage>
class ImageResourcePool : public IResourcePool<imaging::IPixelariumImage>
{
public:
ImageResourcePool() = default;
@@ -48,15 +61,17 @@ class ImageResourcePool : public IResourcePool<imaging::PixelariumImage>
ImageResourcePool& operator=(ImageResourcePool&) = delete;
ImageResourcePool& operator=(ImageResourcePool&&) = delete;
std::optional<const imaging::PixelariumImage*> GetResource(ResourceKey id) const override;
ResourceKey SetResource(std::unique_ptr<imaging::PixelariumImage> res) override;
bool ModifyResource(ResourceKey id, std::unique_ptr<imaging::PixelariumImage> res) override;
std::optional<std::weak_ptr<imaging::IPixelariumImage>> GetResource(ResourceKey id) const override;
ResourceKey SetResource(std::unique_ptr<imaging::IPixelariumImage> res) override;
bool ModifyResource(ResourceKey id, std::unique_ptr<imaging::IPixelariumImage> res) override;
bool DeleteResource(ResourceKey id) override;
void Clear() override { this->resources_.clear(); }
void EnumerateResources(const std::function<void(ResourceKey, size_t, const imaging::PixelariumImage&)>& func) override;
void EnumerateResources(
const std::function<void(ResourceKey, size_t, const imaging::IPixelariumImage&)>& func) override;
template <typename Callable>
requires std::invocable<Callable, ResourceKey, size_t, const imaging::PixelariumImage&>
requires std::invocable<Callable, ResourceKey, size_t, const imaging::IPixelariumImage&>
void Enumerate(Callable&& func) const
{
size_t idx{0};
@@ -69,7 +84,7 @@ class ImageResourcePool : public IResourcePool<imaging::PixelariumImage>
size_t GetTotalSize() const override { return resources_.size(); }
private:
std::unordered_map<size_t, std::unique_ptr<imaging::PixelariumImage>> resources_;
std::unordered_map<size_t, std::shared_ptr<imaging::IPixelariumImage>> resources_;
std::mutex mut_;
};
} // namespace pixelarium::resources