diff --git a/src/openrct2/Imaging.cpp b/src/openrct2/Imaging.cpp index 161ed8eb32..c0c2210a5e 100644 --- a/src/openrct2/Imaging.cpp +++ b/src/openrct2/Imaging.cpp @@ -32,7 +32,7 @@ namespace Imaging static void PngWarning(png_structp png_ptr, const char * b); static void PngError(png_structp png_ptr, const char * b); - bool PngRead(uint8 * * pixels, uint32 * width, uint32 * height, const utf8 * path) + bool PngRead(uint8 * * pixels, uint32 * width, uint32 * height, bool expand, const utf8 * path) { png_structp png_ptr; png_infop info_ptr; @@ -68,8 +68,13 @@ namespace Imaging png_set_read_fn(png_ptr, &fs, PngReadData); png_set_sig_bytes(png_ptr, sig_read); - // To simplify the reading process, convert 4-16 bit data to 24-32 bit data - png_read_png(png_ptr, info_ptr, PNG_TRANSFORM_STRIP_16 | PNG_TRANSFORM_PACKING | PNG_TRANSFORM_EXPAND | PNG_TRANSFORM_GRAY_TO_RGB, nullptr); + uint32 readFlags = PNG_TRANSFORM_STRIP_16 | PNG_TRANSFORM_PACKING; + if (expand) + { + // If we expand the resulting image always be full RGBA + readFlags |= PNG_TRANSFORM_GRAY_TO_RGB | PNG_TRANSFORM_EXPAND; + } + png_read_png(png_ptr, info_ptr, readFlags, nullptr); // Read header png_uint_32 pngWidth, pngHeight; @@ -97,6 +102,16 @@ namespace Imaging } } } + else if (bitDepth == 8 && !expand) + { + // 8-bit paletted + Guard::Assert(rowBytes == pngWidth, GUARD_LINE); + for (png_uint_32 i = 0; i < pngHeight; i++) + { + Memory::Copy(dst, rowPointers[i], rowBytes); + dst += rowBytes; + } + } else { // 32-bit PNG (with alpha) @@ -287,9 +302,9 @@ namespace Imaging extern "C" { - bool image_io_png_read(uint8 * * pixels, uint32 * width, uint32 * height, const utf8 * path) + bool image_io_png_read(uint8 * * pixels, uint32 * width, uint32 * height, bool expand, const utf8 * path) { - return Imaging::PngRead(pixels, width, height, path); + return Imaging::PngRead(pixels, width, height, expand, path); } bool image_io_png_write(const rct_drawpixelinfo * dpi, const rct_palette * palette, const utf8 * path) diff --git a/src/openrct2/Imaging.h b/src/openrct2/Imaging.h index 68a9d79d9b..716d999aea 100644 --- a/src/openrct2/Imaging.h +++ b/src/openrct2/Imaging.h @@ -24,7 +24,7 @@ namespace Imaging { - bool PngRead(uint8 * * pixels, uint32 * width, uint32 * height, const utf8 * path); + bool PngRead(uint8 * * pixels, uint32 * width, uint32 * height, bool expand, const utf8 * path); bool PngWrite(const rct_drawpixelinfo * dpi, const rct_palette * palette, const utf8 * path); bool PngWrite32bpp(sint32 width, sint32 height, const void * pixels, const utf8 * path); } @@ -35,7 +35,7 @@ namespace Imaging extern "C" { #endif - bool image_io_png_read(uint8 * * pixels, uint32 * width, uint32 * height, const utf8 * path); + bool image_io_png_read(uint8 * * pixels, uint32 * width, uint32 * height, bool expand, const utf8 * path); bool image_io_png_write(const rct_drawpixelinfo * dpi, const rct_palette * palette, const utf8 * path); bool image_io_png_write_32bpp(sint32 width, sint32 height, const void * pixels, const utf8 * path); #ifdef __cplusplus