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:
committed by
Maximilian Kueffner
parent
356f966d01
commit
e3e161ce52
@@ -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_);
|
||||
}
|
||||
@@ -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
|
||||
@@ -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 {};
|
||||
}
|
||||
}
|
||||
@@ -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
|
||||
Reference in New Issue
Block a user