add image class serialization

This commit is contained in:
2019-09-03 08:08:31 +02:00
parent 486d2a4eeb
commit 6639414e54
3 changed files with 156 additions and 10 deletions

View File

@@ -1,6 +1,5 @@
#pragma once
#include "binary_stream.h"
#include "image.h"
#include "util.h"
#include "log.h"
@@ -631,25 +630,81 @@ public:
{
}
};
/*
struct ImagePNG : public Type
{
using native_type = Image;
Image data;
bool read(BinaryStreamReader& r) override
{
Descriptor d;
if (d.class_id != "image_png")
return false;
r >> d;
d.value<Integer>("width", data.width);
d.value<Integer>("height", data.height);
d.value<Integer>("comp", data.comp);
data.file_base = wstr2str(d.value<Serializer::String>("file_base"));
data.file_name = wstr2str(d.value<Serializer::String>("file_name"));
data.file_ext = wstr2str(d.value<Serializer::String>("file_ext"));
auto img_raw = d.get<RawData>("data");
int png_width, png_height, png_comp;
data.m_data = std::unique_ptr<uint8_t[]>(stbi_load_from_memory(
img_raw->data.data(), img_raw->data.size(), &png_width, &png_height, &png_comp, 4));
return true;
}
void write(BinaryStreamWriter& w) const override
{
Descriptor d;
d.class_id = "image_png";
d.name = L"Image class";
d.props["width"] = std::make_shared<Integer>(data.width);
d.props["height"] = std::make_shared<Integer>(data.height);
d.props["comp"] = std::make_shared<Integer>(data.comp);
d.props["file_base"] = std::make_shared<Serializer::String>(data.file_base);
d.props["file_name"] = std::make_shared<Serializer::String>(data.file_name);
d.props["file_ext"] = std::make_shared<Serializer::String>(data.file_ext);
// really ugly way to compress the png and store it with a lambda
stbi_write_png_to_func([](void* context, void* data, int size) {
Descriptor& d = *static_cast<Descriptor*>(context);
d.props["data"] = std::make_shared<RawData>(std::vector<uint8_t>((uint8_t*)data, (uint8_t*)data + size));
}, &d, data.width, data.height, data.comp, data.m_data.get(), 0);
w << d;
}
};
*/
struct VMArray : public Type
{
struct ImageData
{
std::unique_ptr<uint8_t[]> data;
size_t size = 0;
int width = 0;
int height = 0;
int comp = 0;
};
uint32_t version; // = 3
Rectangle rect;
std::vector<Channel> channels;
VMArray() = default;
VMArray(uint32_t version, const Rectangle& rect) : version(version), rect(rect) { }
std::shared_ptr<Image> image(bool grayscale, bool invert) const
ImageData image(bool grayscale, bool invert) const
{
int nc = (int)channels.size();
auto pixels = (channels[0].depth >> 3) * rect.area();
if (nc == 1 || nc >= 3)
{
auto img = std::make_shared<Image>();
img->comp = 4;
img->width = rect.width();
img->height = rect.height();
img->m_data = std::make_unique<uint8_t[]>(pixels * 4);
auto out = reinterpret_cast<glm::u8vec4*>(img->m_data.get());
ImageData img;
img.comp = 4;
img.width = rect.width();
img.height = rect.height();
img.data = std::make_unique<uint8_t[]>(pixels * 4);
auto out = reinterpret_cast<glm::u8vec4*>(img.data.get());
if (grayscale)
{
auto const& raw = channels[0].data;
@@ -690,7 +745,7 @@ public:
{
LOG("Error image with %ld channels\n", channels.size());
}
return nullptr;
return {};
}
virtual bool read(BinaryStreamReader& r) override
{