add Canvas.decode static method
This commit is contained in:
parent
d714e6943a
commit
2095e82f1a
@ -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()
|
||||
|
||||
@ -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);
|
||||
}
|
||||
|
||||
@ -374,6 +374,23 @@ static int l_meta_meta_call(lua::State* L) {
|
||||
);
|
||||
}
|
||||
|
||||
static int l_canvas_decode(lua::State* L) {
|
||||
auto bytes = bytearray_as_string(L, 1);
|
||||
auto formatName = require_lstring(L, 2);
|
||||
imageio::ImageFileFormat format;
|
||||
if (!imageio::ImageFileFormatMeta.getItem(formatName, format)) {
|
||||
throw std::runtime_error("unsupported image format");
|
||||
}
|
||||
return newuserdata<LuaCanvas>(
|
||||
L,
|
||||
nullptr,
|
||||
imageio::decode(
|
||||
format,
|
||||
{reinterpret_cast<const unsigned char*>(bytes.data()), bytes.size()}
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
int LuaCanvas::createMetatable(State* L) {
|
||||
createtable(L, 0, 3);
|
||||
pushcfunction(L, lua::wrap<l_meta_index>);
|
||||
@ -385,5 +402,8 @@ int LuaCanvas::createMetatable(State* L) {
|
||||
pushcfunction(L, lua::wrap<l_meta_meta_call>);
|
||||
setfield(L, "__call");
|
||||
setmetatable(L);
|
||||
|
||||
pushcfunction(L, lua::wrap<l_canvas_decode>);
|
||||
setfield(L, "decode");
|
||||
return 1;
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user