Files

75 lines
2.3 KiB
C++
Raw Permalink Normal View History

2025-09-13 14:49:59 +02:00
#include "ImageViewFactory.hpp"
2025-09-26 21:09:51 +02:00
#include <format>
2025-09-13 14:49:59 +02:00
#include <memory>
2025-09-26 21:09:51 +02:00
#include "IPixelariumImage.hpp"
2026-01-23 23:00:35 +00:00
#include "IPixelariumImageView.hpp"
#include "PixelariumImageFactory.hpp"
2026-01-23 23:00:35 +00:00
#include "PixelariumImageViewCzi.hpp"
#include "PixelariumImageViewDefault.hpp"
2025-09-13 14:49:59 +02:00
/// @brief Creates a PixelariumImageView from a resource image.
/// @param image_id The ID of the image resource to render.
2025-09-26 21:09:51 +02:00
/// @return A unique pointer to the PixelariumImageView, or nullptr if the image resource is not found or is empty. The
/// image data is copied.
2026-01-23 23:00:35 +00:00
std::unique_ptr<pixelarium::application::IPixelariumImageView> pixelarium::application::ImageViewFactory::RenderImage(
2025-09-26 21:09:51 +02:00
resources::ResourceKey image_id)
2025-09-13 14:49:59 +02:00
{
2025-10-12 21:47:17 +02:00
using ImageType = imaging::ImageFileType;
2025-09-22 23:13:28 +02:00
auto res{this->image_pool_.GetResource(image_id)};
2025-09-13 14:49:59 +02:00
const auto img{res.lock()};
2025-09-23 21:57:08 +02:00
if (img == nullptr)
2025-09-13 14:49:59 +02:00
{
2025-09-22 23:13:28 +02:00
return {};
}
if (img->Empty())
{
return {};
2025-09-13 14:49:59 +02:00
}
2025-09-23 21:57:08 +02:00
auto type = imaging::ExtensionToType(img->Uri().extension().string());
2025-10-12 21:47:17 +02:00
if (img->Uri().empty())
{
log_.Info(std::format("{}: empty Uri for {}.", __PRETTY_FUNCTION__, img->Name()));
type = ImageType::kMemory;
}
2025-09-23 21:57:08 +02:00
switch (type)
{
2025-10-12 21:47:17 +02:00
case ImageType::kUnknown:
case ImageType::kAbstract:
case ImageType::kPng:
case ImageType::kJpg:
case ImageType::kTiff:
case ImageType::kMemory:
2025-09-26 21:09:51 +02:00
log_.Info(std::format("{}: Creating a Default View", __PRETTY_FUNCTION__));
2025-09-23 21:57:08 +02:00
// beware: here we copy the actual image resource over to the new image
try
{
auto view{std::make_unique<PixelariumImageViewDefault>(img)};
return view;
}
catch (const std::exception& ex)
{
log_.Error(std::format("{}: Creating view failed: {}", __PRETTY_FUNCTION__, ex.what()));
}
2025-10-12 21:47:17 +02:00
case ImageType::kCzi:
2025-09-26 21:09:51 +02:00
log_.Info(std::format("{}: Creating a CZI View", __PRETTY_FUNCTION__));
try
{
auto view{std::make_unique<PixelariumImageViewCzi>(img, log_)};
return view;
}
catch (const std::exception& ex)
{
log_.Error(std::format("{}: Creating view failed: {}", __PRETTY_FUNCTION__, ex.what()));
}
2025-09-23 21:57:08 +02:00
default:
return {};
}
2025-09-13 14:49:59 +02:00
}