From 4d847f42235e06098cc80e40085ec47af75a21b0 Mon Sep 17 00:00:00 2001 From: Jaime Passos Date: Sat, 10 Oct 2020 14:09:59 -0300 Subject: [PATCH 1/2] Compare the PNG's palette with the game's palette instead of assuming they are the same --- src/r_picformats.c | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/src/r_picformats.c b/src/r_picformats.c index 95fe23aeb..feec2abf4 100644 --- a/src/r_picformats.c +++ b/src/r_picformats.c @@ -887,8 +887,24 @@ static png_bytep *PNG_Read( // matches the color count of SRB2's palette: 256 colors. if (png_get_PLTE(png_ptr, png_info_ptr, &palette, &palette_size)) { - if (palette_size == 256) + if (palette_size == 256 && pMasterPalette) + { + png_colorp pal = palette; + INT32 i; + usepal = true; + + for (i = 0; i < 256; i++) + { + UINT32 rgb = R_PutRgbaRGBA(pal->red, pal->green, pal->blue, 0xFF); + if (rgb != pMasterPalette[i].rgba) + { + usepal = false; + break; + } + pal++; + } + } } // If any of the tRNS colors have an alpha lower than 0xFF, and that From 06c0932ab4d3f1984d25ef3de886ec9ad94e2897 Mon Sep 17 00:00:00 2001 From: Jaime Passos Date: Sat, 10 Oct 2020 14:12:22 -0300 Subject: [PATCH 2/2] Only check the tRNS (trans) chunk if the image is still palettized --- src/r_picformats.c | 25 ++++++++++++++----------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/src/r_picformats.c b/src/r_picformats.c index feec2abf4..8d7cf37f2 100644 --- a/src/r_picformats.c +++ b/src/r_picformats.c @@ -909,20 +909,23 @@ static png_bytep *PNG_Read( // If any of the tRNS colors have an alpha lower than 0xFF, and that // color is present on the image, the palette flag is disabled. - png_get_tRNS(png_ptr, png_info_ptr, &trans, &trans_num, &trans_values); - - if (trans && trans_num == 256) + if (usepal) { - int i; - for (i = 0; i < trans_num; i++) + png_get_tRNS(png_ptr, png_info_ptr, &trans, &trans_num, &trans_values); + + if (trans && trans_num == 256) { - // libpng will transform this image into RGB even if - // the transparent index does not exist in the image, - // and there is no way around that. - if (trans[i] < 0xFF) + INT32 i; + for (i = 0; i < trans_num; i++) { - usepal = false; - break; + // libpng will transform this image into RGB even if + // the transparent index does not exist in the image, + // and there is no way around that. + if (trans[i] < 0xFF) + { + usepal = false; + break; + } } } }