From 49b3f566b141ea71d39bab30d78079d5bb15a072 Mon Sep 17 00:00:00 2001 From: Rangi Date: Sun, 13 Sep 2026 16:09:49 -0400 Subject: [PATCH] Fix handling of PNG files with RGB or RGBA color type that have a PLTE chunk --- src/gfx/pal_sorting.cpp | 8 +++++++- test/gfx/rgb_with_plte.out.2bpp | Bin 0 -> 16 bytes test/gfx/rgb_with_plte.out.pal | Bin 0 -> 8 bytes test/gfx/rgb_with_plte.png | Bin 0 -> 99 bytes 4 files changed, 7 insertions(+), 1 deletion(-) create mode 100644 test/gfx/rgb_with_plte.out.2bpp create mode 100644 test/gfx/rgb_with_plte.out.pal create mode 100644 test/gfx/rgb_with_plte.png diff --git a/src/gfx/pal_sorting.cpp b/src/gfx/pal_sorting.cpp index 56712fd9..45918340 100644 --- a/src/gfx/pal_sorting.cpp +++ b/src/gfx/pal_sorting.cpp @@ -33,7 +33,13 @@ void sortIndexed(std::vector &palettes, std::vector const &embPal return true; } } - unreachable_(); // LCOV_EXCL_LINE + // A PNG image using PNG_COLOR_TYPE_RGB (2) or PNG_COLOR_TYPE_RGBA (6) can still + // contain a PLTE chunk. From the PNG spec: "If present, it provides a suggested set of + // from 1 to 256 colors to which the truecolor image can be quantized if the viewer + // cannot display truecolor directly." This means that an input image may have pixels + // with colors that are not in the PLTE chunk, so iterating through `embPal` may finish + // without returning. In that case, we compare `lhs` and `rhs` directly. + return lhs < rhs; }); } } diff --git a/test/gfx/rgb_with_plte.out.2bpp b/test/gfx/rgb_with_plte.out.2bpp new file mode 100644 index 0000000000000000000000000000000000000000..147b4e9a89dc9952bee4dcaed4ce42bafe673e8c GIT binary patch literal 16 NcmWG#Sj7;EgaIO$1N{I1 literal 0 HcmV?d00001 diff --git a/test/gfx/rgb_with_plte.out.pal b/test/gfx/rgb_with_plte.out.pal new file mode 100644 index 0000000000000000000000000000000000000000..98c4fa004b03f7cf8041064c99cf1891a15cdeaf GIT binary patch literal 8 Ncmb1Vc)