From fdec387b4a8fdd8d8fb0e59e66922af835b4a5a6 Mon Sep 17 00:00:00 2001 From: Andrew Murray Date: Sat, 11 Dec 2021 16:07:45 +1100 Subject: [PATCH] Fixed palette index for zeroed color in FASTOCTREE quantize --- Tests/test_image_quantize.py | 17 +++++++++++++++++ src/libImaging/QuantOctree.c | 2 +- 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/Tests/test_image_quantize.py b/Tests/test_image_quantize.py index bd9db362c..8a7b9b311 100644 --- a/Tests/test_image_quantize.py +++ b/Tests/test_image_quantize.py @@ -85,3 +85,20 @@ def test_transparent_colors_equal(): converted = im.quantize() converted_px = converted.load() assert converted_px[0, 0] == converted_px[0, 1] + + +@pytest.mark.parametrize( + "method, color", + ( + (Image.MEDIANCUT, (0, 0, 0)), + (Image.MAXCOVERAGE, (0, 0, 0)), + (Image.FASTOCTREE, (0, 0, 0)), + (Image.FASTOCTREE, (0, 0, 0, 0)), + ), +) +def test_palette(method, color): + im = Image.new("RGBA" if len(color) == 4 else "RGB", (1, 1), color) + + converted = im.quantize(method=method) + converted_px = converted.load() + assert converted_px[0, 0] == converted.palette.colors[color] diff --git a/src/libImaging/QuantOctree.c b/src/libImaging/QuantOctree.c index b8d4d1d7c..5e79bce35 100644 --- a/src/libImaging/QuantOctree.c +++ b/src/libImaging/QuantOctree.c @@ -317,7 +317,7 @@ void add_lookup_buckets(ColorCube cube, ColorBucket palette, long nColors, long offset) { long i; Pixel p; - for (i = offset; i < offset + nColors; i++) { + for (i = offset + nColors - 1; i >= offset; i--) { avg_color_from_color_bucket(&palette[i], &p); set_lookup_value(cube, &p, i); }