add canvas:encode method
This commit is contained in:
parent
dda823ac24
commit
d714e6943a
@ -53,3 +53,14 @@ void imageio::write(const io::path& file, const ImageData* image) {
|
||||
}
|
||||
return found->second(io::resolve(file).u8string(), image);
|
||||
}
|
||||
|
||||
util::Buffer<unsigned char> imageio::encode(
|
||||
ImageFileFormat format, const ImageData& image
|
||||
) {
|
||||
switch (format) {
|
||||
case ImageFileFormat::PNG:
|
||||
return png::encode_image(image);
|
||||
default:
|
||||
throw std::runtime_error("file format is not supported for encoding");
|
||||
}
|
||||
}
|
||||
|
||||
@ -4,10 +4,20 @@
|
||||
#include <string>
|
||||
|
||||
#include "io/fwd.hpp"
|
||||
#include "util/Buffer.hpp"
|
||||
#include "util/EnumMetadata.hpp"
|
||||
|
||||
class ImageData;
|
||||
|
||||
namespace imageio {
|
||||
enum class ImageFileFormat {
|
||||
PNG
|
||||
};
|
||||
|
||||
VC_ENUM_METADATA(ImageFileFormat)
|
||||
{"png", ImageFileFormat::PNG},
|
||||
VC_ENUM_END
|
||||
|
||||
inline const std::string PNG = ".png";
|
||||
|
||||
bool is_read_supported(const std::string& extension);
|
||||
@ -15,4 +25,5 @@ namespace imageio {
|
||||
|
||||
std::unique_ptr<ImageData> read(const io::path& file);
|
||||
void write(const io::path& file, const ImageData* image);
|
||||
util::Buffer<unsigned char> encode(ImageFileFormat format, const ImageData& image);
|
||||
}
|
||||
|
||||
@ -11,6 +11,60 @@
|
||||
|
||||
static debug::Logger logger("png-coder");
|
||||
|
||||
static util::Buffer<ubyte> write_to_memory(uint width, uint height, const ubyte* data, bool alpha) {
|
||||
uint pixsize = alpha ? 4 : 3;
|
||||
|
||||
std::vector<ubyte> buffer;
|
||||
png_structp png_ptr = png_create_write_struct(
|
||||
PNG_LIBPNG_VER_STRING, nullptr, nullptr, nullptr
|
||||
);
|
||||
png_infop info_ptr = png_create_info_struct(png_ptr);
|
||||
|
||||
png_set_write_fn(
|
||||
png_ptr,
|
||||
&buffer,
|
||||
[](png_structp pngPtr, png_bytep data, png_size_t length) {
|
||||
auto& buf = *reinterpret_cast<std::vector<ubyte>*>(png_get_io_ptr(pngPtr));
|
||||
buf.insert(
|
||||
buf.end(),
|
||||
reinterpret_cast<ubyte*>(data),
|
||||
reinterpret_cast<ubyte*>(data) + length
|
||||
);
|
||||
},
|
||||
nullptr
|
||||
);
|
||||
|
||||
png_set_IHDR(
|
||||
png_ptr,
|
||||
info_ptr,
|
||||
width,
|
||||
height,
|
||||
8,
|
||||
alpha ? PNG_COLOR_TYPE_RGBA : PNG_COLOR_TYPE_RGB,
|
||||
PNG_INTERLACE_NONE,
|
||||
PNG_COMPRESSION_TYPE_BASE,
|
||||
PNG_FILTER_TYPE_BASE
|
||||
);
|
||||
|
||||
png_write_info(png_ptr, info_ptr);
|
||||
|
||||
auto row = std::make_unique<png_byte[]>(pixsize * width);
|
||||
for (uint y = 0; y < height; y++) {
|
||||
for (uint x = 0; x < width; x++) {
|
||||
for (uint i = 0; i < pixsize; i++) {
|
||||
row[x * pixsize + i] =
|
||||
(png_byte)data[(y * width + x) * pixsize + i];
|
||||
}
|
||||
}
|
||||
png_write_row(png_ptr, row.get());
|
||||
}
|
||||
|
||||
png_write_end(png_ptr, nullptr);
|
||||
png_free_data(png_ptr, info_ptr, PNG_FREE_ALL, -1);
|
||||
png_destroy_write_struct(&png_ptr, &info_ptr);
|
||||
return util::Buffer<ubyte>(buffer.data(), buffer.size());
|
||||
}
|
||||
|
||||
// returns 0 if all-right, 1 otherwise
|
||||
static int png_write(
|
||||
const char* filename, uint width, uint height, const ubyte* data, bool alpha
|
||||
@ -230,3 +284,13 @@ void png::write_image(const std::string& filename, const ImageData* image) {
|
||||
image->getFormat() == ImageFormat::rgba8888
|
||||
);
|
||||
}
|
||||
|
||||
util::Buffer<ubyte> png::encode_image(const ImageData& image) {
|
||||
auto format = image.getFormat();
|
||||
return write_to_memory(
|
||||
image.getWidth(),
|
||||
image.getHeight(),
|
||||
image.getData(),
|
||||
format == ImageFormat::rgba8888
|
||||
);
|
||||
}
|
||||
|
||||
@ -4,6 +4,7 @@
|
||||
#include <string>
|
||||
|
||||
#include "typedefs.hpp"
|
||||
#include "util/Buffer.hpp"
|
||||
|
||||
class Texture;
|
||||
class ImageData;
|
||||
@ -11,6 +12,7 @@ class ImageData;
|
||||
namespace png {
|
||||
std::unique_ptr<ImageData> load_image(const ubyte* bytes, size_t size);
|
||||
void write_image(const std::string& filename, const ImageData* image);
|
||||
util::Buffer<ubyte> encode_image(const ImageData& image);
|
||||
std::unique_ptr<Texture> load_texture(const ubyte* bytes, size_t size);
|
||||
std::unique_ptr<Texture> load_texture(const std::string& filename);
|
||||
}
|
||||
|
||||
@ -1,8 +1,10 @@
|
||||
#define VC_ENABLE_REFLECTION
|
||||
#include "lua_type_canvas.hpp"
|
||||
|
||||
#include "graphics/core/ImageData.hpp"
|
||||
#include "graphics/core/Texture.hpp"
|
||||
#include "logic/scripting/lua/lua_util.hpp"
|
||||
#include "coders/imageio.hpp"
|
||||
#include "engine/Engine.hpp"
|
||||
#include "assets/Assets.hpp"
|
||||
|
||||
@ -284,6 +286,23 @@ static int l_sub(State* L) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int l_encode(State* L) {
|
||||
auto canvas = touserdata<LuaCanvas>(L, 1);
|
||||
if (canvas == nullptr) {
|
||||
return 0;
|
||||
}
|
||||
auto format = imageio::ImageFileFormat::PNG;
|
||||
if (lua::isstring(L, 2)) {
|
||||
auto name = lua::require_string(L, 2);
|
||||
if (!imageio::ImageFileFormatMeta.getItem(name, format)) {
|
||||
throw std::runtime_error("unsupported image file format");
|
||||
}
|
||||
}
|
||||
|
||||
auto buffer = imageio::encode(format, canvas->getData());
|
||||
return lua::create_bytearray(L, buffer.data(), buffer.size());
|
||||
}
|
||||
|
||||
static std::unordered_map<std::string, lua_CFunction> methods {
|
||||
{"at", lua::wrap<l_at>},
|
||||
{"set", lua::wrap<l_set>},
|
||||
@ -296,6 +315,7 @@ static std::unordered_map<std::string, lua_CFunction> methods {
|
||||
{"mul", lua::wrap<l_mul>},
|
||||
{"add", lua::wrap<l_add>},
|
||||
{"sub", lua::wrap<l_sub>},
|
||||
{"encode", lua::wrap<l_encode>},
|
||||
{"_set_data", lua::wrap<l_set_data>},
|
||||
};
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user