Add expand option to PngRead

This commit is contained in:
Jeroen D Stout 2017-10-03 23:18:38 +02:00 committed by Michał Janiszewski
parent 7089ec80fc
commit 25015fb15d
2 changed files with 22 additions and 7 deletions

View File

@ -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)

View File

@ -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