Skip to content

Commit

Permalink
libgui|Image: Serializing an image
Browse files Browse the repository at this point in the history
  • Loading branch information
skyjake committed Sep 1, 2019
1 parent 4ad459a commit ee064b6
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 2 deletions.
6 changes: 4 additions & 2 deletions doomsday/libs/gui/include/de/graphics/image.h
Expand Up @@ -48,7 +48,6 @@ class LIBGUI_PUBLIC Image : public ISerializable
*/
enum Format {
Unknown = -1,
// UseQImageFormat = 0, ///< May not be GL friendly.

Luminance_8 = 1,
LuminanceAlpha_88 = 2,
Expand Down Expand Up @@ -81,6 +80,8 @@ class LIBGUI_PUBLIC Image : public ISerializable
RGBA_32ui = 29,
};

enum SerializationFormat { Png, Jpeg, Targa, Bmp };

typedef Vec2ui Size;
typedef Vec4ub Color;
typedef Vector4<duint16> Color16;
Expand Down Expand Up @@ -245,7 +246,8 @@ class LIBGUI_PUBLIC Image : public ISerializable
Image rgbSwapped() const;
Image flipped() const;

void save(const NativePath &path) const;
void save(const NativePath &path) const;
Block serialize(SerializationFormat format) const;

// Implements ISerializable.
void operator >> (Writer &to) const;
Expand Down
30 changes: 30 additions & 0 deletions doomsday/libs/gui/src/graphics/image.cpp
Expand Up @@ -1021,6 +1021,36 @@ void Image::save(const NativePath &path) const
}
}

static void dataWriter(void *context, void *data, int size)
{
reinterpret_cast<Block *>(context)->append(data, size);
}

Block Image::serialize(SerializationFormat format) const
{
Block data;
const int comp = bytesPerPixel() / 4;
switch (format)
{
case Png:
stbi_write_png_to_func(dataWriter, &data, width(), height(), comp, bits(), stride());
break;

case Jpeg:
stbi_write_jpg_to_func(dataWriter, &data, width(), height(), comp, bits(), 85);
break;

case Targa:
stbi_write_tga_to_func(dataWriter, &data, width(), height(), comp, bits());
break;

case Bmp:
stbi_write_bmp_to_func(dataWriter, &data, width(), height(), comp, bits());
break;
}
return data;
}

void Image::operator>>(Writer &to) const
{
to << duint8(d->format);
Expand Down

0 comments on commit ee064b6

Please sign in to comment.