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
This commit is contained in:
UnknownShadow200 2023-04-22 20:51:24 +10:00
parent 3e08cfc533
commit 03ccdbbc5c

View File

@ -429,8 +429,9 @@ cc_result Png_Decode(struct Bitmap* bmp, struct Stream* stream) {
res = Stream_Read(stream, tmp, dataSize); res = Stream_Read(stream, tmp, dataSize);
if (res) return res; if (res) return res;
/* RGB is 16 bits big endian, ignore least significant 8 bits */ /* RGB is always two bytes */
trnsCol = BitmapCol_Make(tmp[0], tmp[0], tmp[0], 0); /* 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) { } else if (col == PNG_COLOR_INDEXED) {
if (dataSize > PNG_PALETTE) return PNG_ERR_TRANS_COUNT; if (dataSize > PNG_PALETTE) return PNG_ERR_TRANS_COUNT;
res = Stream_Read(stream, tmp, dataSize); 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); res = Stream_Read(stream, tmp, dataSize);
if (res) return res; if (res) return res;
/* R,G,B is 16 bits big endian, ignore least significant 8 bits */ /* R,G,B are always two bytes */
trnsCol = BitmapCol_Make(tmp[0], tmp[2], tmp[4], 0); /* TODO is this right for 16 bits per channel images? */
trnsCol = BitmapCol_Make(tmp[1], tmp[3], tmp[5], 0);
} else { } else {
return PNG_ERR_TRANS_INVALID; return PNG_ERR_TRANS_INVALID;
} }