From 03ccdbbc5c7527cd5c032d3626ef85487f2f7d88 Mon Sep 17 00:00:00 2001 From: UnknownShadow200 Date: Sat, 22 Apr 2023 20:51:24 +1000 Subject: [PATCH] Fix rare issue where some PNGs were decoded with the wrong transparency (Thanks Goodly) If the bitmap used a colour type of 2 (RGB) or 0 (GRAYSCALE), and had a tRNS chunk to indicate to treat one specific RGB colour as transparent, and that specific colour wasn't block, the decoder would still incorrectly treat black as the transparent colour instead of the actual colour specified --- src/Bitmap.c | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/Bitmap.c b/src/Bitmap.c index 1c7883b34..8f3db313e 100644 --- a/src/Bitmap.c +++ b/src/Bitmap.c @@ -429,8 +429,9 @@ cc_result Png_Decode(struct Bitmap* bmp, struct Stream* stream) { res = Stream_Read(stream, tmp, dataSize); if (res) return res; - /* RGB is 16 bits big endian, ignore least significant 8 bits */ - trnsCol = BitmapCol_Make(tmp[0], tmp[0], tmp[0], 0); + /* RGB is always two bytes */ + /* TODO is this right for 16 bits per channel images? */ + trnsCol = BitmapCol_Make(tmp[1], tmp[1], tmp[1], 0); } else if (col == PNG_COLOR_INDEXED) { if (dataSize > PNG_PALETTE) return PNG_ERR_TRANS_COUNT; res = Stream_Read(stream, tmp, dataSize); @@ -446,8 +447,9 @@ cc_result Png_Decode(struct Bitmap* bmp, struct Stream* stream) { res = Stream_Read(stream, tmp, dataSize); if (res) return res; - /* R,G,B is 16 bits big endian, ignore least significant 8 bits */ - trnsCol = BitmapCol_Make(tmp[0], tmp[2], tmp[4], 0); + /* R,G,B are always two bytes */ + /* TODO is this right for 16 bits per channel images? */ + trnsCol = BitmapCol_Make(tmp[1], tmp[3], tmp[5], 0); } else { return PNG_ERR_TRANS_INVALID; }