add Canvas.decode static method

This commit is contained in:
MihailRis
2025-11-19 19:21:09 +03:00
parent d714e6943a
commit 2095e82f1a
3 changed files with 55 additions and 8 deletions
+32 -8
View File
@@ -1,3 +1,4 @@
#define VC_ENABLE_REFLECTION
#include "imageio.hpp"
#include <functional>
@@ -7,28 +8,34 @@
#include "io/io.hpp"
#include "png.hpp"
using namespace imageio;
using image_reader =
std::function<std::unique_ptr<ImageData>(const ubyte*, size_t)>;
using image_writer = std::function<void(const std::string&, const ImageData*)>;
static std::unordered_map<std::string, image_reader> readers {
{".png", png::load_image},
static std::unordered_map<ImageFileFormat, image_reader> readers {
{ImageFileFormat::PNG, png::load_image},
};
static std::unordered_map<std::string, image_writer> writers {
{".png", png::write_image},
static std::unordered_map<ImageFileFormat, image_writer> writers {
{ImageFileFormat::PNG, png::write_image},
};
bool imageio::is_read_supported(const std::string& extension) {
return readers.find(extension) != readers.end();
return extension == ".png";
}
bool imageio::is_write_supported(const std::string& extension) {
return writers.find(extension) != writers.end();
return extension == ".png";
}
std::unique_ptr<ImageData> imageio::read(const io::path& file) {
auto found = readers.find(file.extension());
ImageFileFormat format;
if (!ImageFileFormatMeta.getItem(file.extension().substr(1), format)) {
throw std::runtime_error("unsupported image format");
}
auto found = readers.find(format);
if (found == readers.end()) {
throw std::runtime_error(
"file format is not supported (read): " + file.string()
@@ -44,8 +51,25 @@ std::unique_ptr<ImageData> imageio::read(const io::path& file) {
}
}
std::unique_ptr<ImageData> imageio::decode(
ImageFileFormat format, util::span<ubyte> src
) {
auto found = readers.find(format);
try {
return std::unique_ptr<ImageData>(found->second(src.data(), src.size()));
} catch (const std::runtime_error& err) {
throw std::runtime_error(
"could not to decode image: " + std::string(err.what())
);
}
}
void imageio::write(const io::path& file, const ImageData* image) {
auto found = writers.find(file.extension());
ImageFileFormat format;
if (!ImageFileFormatMeta.getItem(file.extension().substr(1), format)) {
throw std::runtime_error("unsupported image format");
}
auto found = writers.find(format);
if (found == writers.end()) {
throw std::runtime_error(
"file format is not supported (write): " + file.string()
+3
View File
@@ -6,6 +6,8 @@
#include "io/fwd.hpp"
#include "util/Buffer.hpp"
#include "util/EnumMetadata.hpp"
#include "util/span.hpp"
#include "typedefs.hpp"
class ImageData;
@@ -25,5 +27,6 @@ namespace imageio {
std::unique_ptr<ImageData> read(const io::path& file);
void write(const io::path& file, const ImageData* image);
std::unique_ptr<ImageData> decode(ImageFileFormat format, util::span<ubyte> src);
util::Buffer<unsigned char> encode(ImageFileFormat format, const ImageData& image);
}