Skip to content

Commit

Permalink
libgui|Image: Serializing based on a file extension format hint
Browse files Browse the repository at this point in the history
  • Loading branch information
skyjake committed Sep 1, 2019
1 parent ee064b6 commit 4fe8da4
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 2 deletions.
1 change: 1 addition & 0 deletions doomsday/libs/gui/include/de/graphics/image.h
Expand Up @@ -248,6 +248,7 @@ class LIBGUI_PUBLIC Image : public ISerializable

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

// Implements ISerializable.
void operator >> (Writer &to) const;
Expand Down
30 changes: 28 additions & 2 deletions doomsday/libs/gui/src/graphics/image.cpp
Expand Up @@ -34,6 +34,8 @@

namespace de {

static constexpr int JPEG_QUALITY = 85;

#define IMAGE_ASSERT_EDITABLE(d) DE_ASSERT(d->format == RGBA_8888)

namespace internal {
Expand Down Expand Up @@ -1005,7 +1007,7 @@ void Image::save(const NativePath &path) const
}
else if (!ext.compareWithoutCase(".jpg"))
{
stbi_write_jpg(path.c_str(), width(), height(), comp, bits(), 85);
stbi_write_jpg(path.c_str(), width(), height(), comp, bits(), JPEG_QUALITY);
}
else if (!ext.compareWithoutCase(".tga"))
{
Expand Down Expand Up @@ -1037,7 +1039,7 @@ Block Image::serialize(SerializationFormat format) const
break;

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

case Targa:
Expand All @@ -1051,6 +1053,30 @@ Block Image::serialize(SerializationFormat format) const
return data;
}

Block Image::serialize(const char *formatHint) const
{
struct Hint {
const char *ext;
SerializationFormat sformat;
};
static const Hint hints[] = {
{ ".png", Png },
{ ".jpg", Jpeg },
{ ".jpeg", Jpeg },
{ ".bmp", Bmp },
{ ".tga", Targa },
};

for (const auto &hint : hints)
{
if (!iCmpStrCase(formatHint, hint.ext))
{
return serialize(hint.sformat);
}
}
return serialize(Png);
}

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

0 comments on commit 4fe8da4

Please sign in to comment.