Enhance image type support (#18)

* adds tiff support

* doc update

* adds memory-based =IPixelariumImage= implementation

* add usage example for custom user control

* enhance

* clang-format

fix readme

fix docs
This commit is contained in:
m-aXimilian
2025-10-12 21:47:17 +02:00
committed by Maximilian Kueffner
parent 356f966d01
commit e3e161ce52
16 changed files with 385 additions and 15 deletions
+4
View File
@@ -15,6 +15,10 @@ set(IMAGELIBSRC
impl/PixelariumPng.cpp
impl/PixelariumCzi.hpp
impl/PixelariumCzi.cpp
impl/PixelariumTiff.hpp
impl/PixelariumTiff.cpp
impl/PixelariumMem.hpp
impl/PixelariumMem.cpp
)
set(IMAGELIBLIBNAME pixelariumimagelib)
+4
View File
@@ -23,6 +23,10 @@ enum class ImageFileType
kJpg = 2,
/// @brief Represents a CZI image file.
kCzi = 3,
/// @brief Represents a TIFF image file.
kTiff = 4,
/// @brief Represents an in-memory image.
kMemory = 5,
};
/// @brief An abstract interface to define a semantic query
+8 -5
View File
@@ -3,9 +3,11 @@
#include <cctype>
#include <memory>
#include "imaging/impl/PixelariumMem.hpp"
#include "impl/PixelariumCzi.hpp"
#include "impl/PixelariumJpg.hpp"
#include "impl/PixelariumPng.hpp"
#include "impl/PixelariumTiff.hpp"
/*static*/ std::unique_ptr<pixelarium::imaging::IPixelariumImage>
pixelarium::imaging::PixelariumImageFactory::CreateImage(const std::string& uri, const Log& log)
@@ -17,18 +19,19 @@ pixelarium::imaging::PixelariumImageFactory::CreateImage(const std::string& uri,
{
case ImageFileType::kUnknown:
return {};
break;
case ImageFileType::kAbstract:
return {};
break;
case ImageFileType::kPng:
return std::make_unique<PixelariumPng>(uri);
break;
case ImageFileType::kJpg:
return std::make_unique<PixelariumJpg>(uri);
break;
case ImageFileType::kCzi:
return std::make_unique<PixelariumCzi>(uri, log);
break;
case ImageFileType::kTiff:
return std::make_unique<PixelariumTiff>(uri, log);
case ImageFileType::kMemory:
return std::make_unique<PixelariumMem>(cv::Mat(), uri, log);
default:
return {};
}
}
+4
View File
@@ -23,6 +23,10 @@ constexpr pixelarium::imaging::ImageFileType ExtensionToType(const std::string&
{
return pixelarium::imaging::ImageFileType::kCzi;
}
if (lower_ext == ".tiff" || lower_ext == ".tif")
{
return pixelarium::imaging::ImageFileType::kTiff;
}
return pixelarium::imaging::ImageFileType::kUnknown;
}
+26
View File
@@ -0,0 +1,26 @@
#include "PixelariumMem.hpp"
#include <filesystem>
#include <memory>
#include <opencv2/imgcodecs.hpp>
#include <string>
pixelarium::imaging::PixelariumMem::PixelariumMem(const cv::Mat& img, const std::string& name, const Log& log)
: img_(img), log_(log), name_(name)
{
this->is_empty_ = false;
this->uri_ = std::filesystem::path();
}
std::unique_ptr<cv::Mat> pixelarium::imaging::PixelariumMem::TryGetImage()
{
// 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
// of.
// I.e., it only makes sense for the file types that do not already manage a cv::Mat in memory.
// PixelariumMem is meant for exactly this in-memory management of a cv::Mat though.
// 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
// point.
return std::make_unique<cv::Mat>(this->img_);
}
+47
View File
@@ -0,0 +1,47 @@
#pragma once
#include <stdexcept>
#include <string>
#include "../IPixelariumImage.hpp"
#include "utilities/ILog.hpp"
namespace pixelarium::imaging
{
/// @brief Implements support for in-memory images in the realm of IPixelariumImage
class PixelariumMem : public IPixelariumImage
{
using Log = pixelarium::utils::log::ILog;
public:
explicit PixelariumMem(const cv::Mat& img, const std::string& name, const Log& log);
// IPixelariumImage member implementations
public:
std::unique_ptr<cv::Mat> TryGetImage() override;
std::unique_ptr<cv::Mat> TryGetImage(const IImageQuery&) override { throw std::runtime_error("Not implemented."); }
std::vector<std::unique_ptr<cv::Mat>> TryGetImages(const IImageQuery&) override
{
throw std::runtime_error("Not implemented.");
}
void SetImage(const cv::Mat& img) { this->img_ = img; }
std::string Name() const noexcept override { return this->name_; }
bool Empty() const noexcept override { return this->is_empty_; }
public:
const static ImageFileType type_{ImageFileType::kMemory};
private:
// this should be set by each image getter
// after a new cv::Mat could be instantiated
bool is_empty_{true};
cv::Mat img_;
const Log& log_;
std::string name_;
};
} // namespace pixelarium::imaging
+34
View File
@@ -0,0 +1,34 @@
#include "PixelariumTiff.hpp"
#include <filesystem>
#include <memory>
#include <opencv2/imgcodecs.hpp>
#include <string>
pixelarium::imaging::PixelariumTiff::PixelariumTiff(const std::string& uri, const Log& log) : log_(log)
{
if (!std::filesystem::exists(uri))
{
throw std::runtime_error("Render file not found.");
}
this->is_empty_ = false;
this->uri_ = std::filesystem::path(uri);
}
std::unique_ptr<cv::Mat> pixelarium::imaging::PixelariumTiff::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 {};
}
}
+46
View File
@@ -0,0 +1,46 @@
#pragma once
#include <stdexcept>
#include <string>
#include "../IPixelariumImage.hpp"
#include "utilities/ILog.hpp"
namespace pixelarium::imaging
{
/// @brief Implements support for .tiff-images in the realm of IPixelariumImage
class PixelariumTiff : public IPixelariumImage
{
using Log = pixelarium::utils::log::ILog;
public:
explicit PixelariumTiff(const std::string& uri, const Log& log);
// IPixelariumImage member implementations
public:
std::unique_ptr<cv::Mat> TryGetImage() override;
std::unique_ptr<cv::Mat> TryGetImage(const IImageQuery&) override
{
// ToDo: proper error
throw std::runtime_error("Not possible with tiff.");
}
std::vector<std::unique_ptr<cv::Mat>> TryGetImages(const IImageQuery&) override
{
// ToDo: proper error
throw std::runtime_error("Not possible with tiff.");
}
bool Empty() const noexcept override { return this->is_empty_; }
public:
const static ImageFileType type_{ImageFileType::kTiff};
private:
// this should be set by each image getter
// after a new cv::Mat could be instantiated
bool is_empty_{true};
const Log& log_;
};
} // namespace pixelarium::imaging