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