Skip to content

Commit 035e141

Browse files
committed
WritePng: Support transparent PNG images
Now matches exactly what Maniacs is saving
1 parent 818aee8 commit 035e141

3 files changed

Lines changed: 12 additions & 4 deletions

File tree

src/bitmap.cpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,11 +184,19 @@ bool Bitmap::WritePNG(std::ostream& os) const {
184184
auto format = PIXMAN_b8g8r8;
185185
#endif
186186

187+
if (GetTransparent()) {
188+
#ifdef WORDS_BIGENDIAN
189+
format = PIXMAN_r8g8b8a8;
190+
#else
191+
format = PIXMAN_a8b8g8r8;
192+
#endif
193+
}
194+
187195
auto dst = PixmanImagePtr{pixman_image_create_bits(format, width, height, &data.front(), stride)};
188196
pixman_image_composite32(PIXMAN_OP_SRC, bitmap.get(), NULL, dst.get(),
189197
0, 0, 0, 0, 0, 0, width, height);
190198

191-
return ImagePNG::Write(os, width, height, &data.front());
199+
return ImagePNG::Write(os, width, height, &data.front(), GetTransparent());
192200
}
193201

194202
size_t Bitmap::GetSize() const {

src/image_png.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,7 @@ static void flush_stream(png_structp out_ptr) {
253253
reinterpret_cast<Filesystem_Stream::OutputStream*>(png_get_io_ptr(out_ptr))->flush();
254254
}
255255

256-
bool ImagePNG::Write(std::ostream& os, uint32_t width, uint32_t height, uint32_t* data) {
256+
bool ImagePNG::Write(std::ostream& os, uint32_t width, uint32_t height, uint32_t* data, bool transparent) {
257257
png_structp write = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
258258
if (!write) {
259259
Output::Warning("Bitmap::WritePNG: error in png_create_write");
@@ -282,7 +282,7 @@ bool ImagePNG::Write(std::ostream& os, uint32_t width, uint32_t height, uint32_t
282282
png_set_write_fn(write, &os, &write_data, &flush_stream);
283283

284284
png_set_IHDR(write, info, width, height, 8,
285-
PNG_COLOR_TYPE_RGB, PNG_INTERLACE_NONE,
285+
transparent ? PNG_COLOR_TYPE_RGBA : PNG_COLOR_TYPE_RGB, PNG_INTERLACE_NONE,
286286
PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
287287
png_write_info(write, info);
288288
png_write_image(write, ptrs);

src/image_png.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
namespace ImagePNG {
2626
bool Read(const void* buffer, bool transparent, ImageOut& output);
2727
bool Read(Filesystem_Stream::InputStream& is, bool transparent, ImageOut& output);
28-
bool Write(std::ostream& os, uint32_t width, uint32_t height, uint32_t* data);
28+
bool Write(std::ostream& os, uint32_t width, uint32_t height, uint32_t* data, bool transparent);
2929
}
3030

3131
#endif

0 commit comments

Comments
 (0)