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
@@ -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