Skip to content

Commit 6dd5b2e

Browse files
authored
Merge pull request #6377 from btrekkie/fix-round-to-palette
Fixed bug with rounding pixels to palette
2 parents b00b509 + b7dc310 commit 6dd5b2e

File tree

8 files changed

+22
-6
lines changed

8 files changed

+22
-6
lines changed

Tests/images/palette_negative.png

-86 Bytes
Loading

Tests/images/palette_sepia.png

25 Bytes
Loading

Tests/images/palette_wedge.png

-81 Bytes
Loading

Tests/test_image_entropy.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ def entropy(mode):
99
assert round(abs(entropy("L") - 7.063008716585465), 7) == 0
1010
assert round(abs(entropy("I") - 7.063008716585465), 7) == 0
1111
assert round(abs(entropy("F") - 7.063008716585465), 7) == 0
12-
assert round(abs(entropy("P") - 5.0530452472519745), 7) == 0
12+
assert round(abs(entropy("P") - 5.082506854662517), 7) == 0
1313
assert round(abs(entropy("RGB") - 8.821286587714319), 7) == 0
1414
assert round(abs(entropy("RGBA") - 7.42724306524488), 7) == 0
1515
assert round(abs(entropy("CMYK") - 7.4272430652448795), 7) == 0

Tests/test_image_getcolors.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ def getcolors(mode, limit=None):
1616
assert getcolors("L") == 255
1717
assert getcolors("I") == 255
1818
assert getcolors("F") == 255
19-
assert getcolors("P") == 90 # fixed palette
19+
assert getcolors("P") == 96 # fixed palette
2020
assert getcolors("RGB") is None
2121
assert getcolors("RGBA") is None
2222
assert getcolors("CMYK") is None

Tests/test_image_histogram.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ def histogram(mode):
1010
assert histogram("L") == (256, 0, 662)
1111
assert histogram("I") == (256, 0, 662)
1212
assert histogram("F") == (256, 0, 662)
13-
assert histogram("P") == (256, 0, 1871)
13+
assert histogram("P") == (256, 0, 1551)
1414
assert histogram("RGB") == (768, 4, 675)
1515
assert histogram("RGBA") == (1024, 0, 16384)
1616
assert histogram("CMYK") == (1024, 0, 16384)

Tests/test_image_quantize.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,22 @@ def test_quantize_no_dither():
6565
assert converted.palette.palette == palette.palette.palette
6666

6767

68+
def test_quantize_no_dither2():
69+
im = Image.new("RGB", (9, 1))
70+
im.putdata(list((p,) * 3 for p in range(0, 36, 4)))
71+
72+
palette = Image.new("P", (1, 1))
73+
data = (0, 0, 0, 32, 32, 32)
74+
palette.putpalette(data)
75+
quantized = im.quantize(dither=Image.Dither.NONE, palette=palette)
76+
77+
assert tuple(quantized.palette.palette) == data
78+
79+
px = quantized.load()
80+
for x in range(9):
81+
assert px[x, 0] == (0 if x < 5 else 1)
82+
83+
6884
def test_quantize_dither_diff():
6985
image = hopper()
7086
with Image.open("Tests/images/caption_6_33_22.png") as palette:

src/libImaging/Palette.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -200,15 +200,15 @@ ImagingPaletteCacheUpdate(ImagingPalette palette, int r, int g, int b) {
200200

201201
/* Find min and max distances to any point in the box */
202202
r = palette->palette[i * 4 + 0];
203-
tmin = (r < r0) ? RDIST(r, r1) : (r > r1) ? RDIST(r, r0) : 0;
203+
tmin = (r < r0) ? RDIST(r, r0) : (r > r1) ? RDIST(r, r1) : 0;
204204
tmax = (r <= rc) ? RDIST(r, r1) : RDIST(r, r0);
205205

206206
g = palette->palette[i * 4 + 1];
207-
tmin += (g < g0) ? GDIST(g, g1) : (g > g1) ? GDIST(g, g0) : 0;
207+
tmin += (g < g0) ? GDIST(g, g0) : (g > g1) ? GDIST(g, g1) : 0;
208208
tmax += (g <= gc) ? GDIST(g, g1) : GDIST(g, g0);
209209

210210
b = palette->palette[i * 4 + 2];
211-
tmin += (b < b0) ? BDIST(b, b1) : (b > b1) ? BDIST(b, b0) : 0;
211+
tmin += (b < b0) ? BDIST(b, b0) : (b > b1) ? BDIST(b, b1) : 0;
212212
tmax += (b <= bc) ? BDIST(b, b1) : BDIST(b, b0);
213213

214214
dmin[i] = tmin;

0 commit comments

Comments
 (0)