This commit is contained in:
Arnaud Billon 2026-01-28 01:05:02 +01:00 committed by GitHub
commit 97a3ff5371
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 34 additions and 53 deletions

Binary file not shown.

Before

Width:  |  Height:  |  Size: 17 KiB

After

Width:  |  Height:  |  Size: 17 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 20 KiB

After

Width:  |  Height:  |  Size: 20 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 17 KiB

After

Width:  |  Height:  |  Size: 17 KiB

View File

@ -11,7 +11,7 @@ def test_entropy() -> None:
assert round(abs(entropy("L") - 7.063008716585465), 7) == 0
assert round(abs(entropy("I") - 7.063008716585465), 7) == 0
assert round(abs(entropy("F") - 7.063008716585465), 7) == 0
assert round(abs(entropy("P") - 5.082506854662517), 7) == 0
assert round(abs(entropy("P") - 5.085630558679487), 7) == 0
assert round(abs(entropy("RGB") - 8.821286587714319), 7) == 0
assert round(abs(entropy("RGBA") - 7.42724306524488), 7) == 0
assert round(abs(entropy("CMYK") - 7.4272430652448795), 7) == 0

View File

@ -14,7 +14,7 @@ def test_histogram() -> None:
assert histogram("La") == (512, 0, 16384)
assert histogram("I") == (256, 0, 662)
assert histogram("F") == (256, 0, 662)
assert histogram("P") == (256, 0, 1551)
assert histogram("P") == (256, 0, 1557)
assert histogram("PA") == (512, 0, 16384)
assert histogram("RGB") == (768, 4, 675)
assert histogram("RGBA") == (1024, 0, 16384)

View File

@ -1246,19 +1246,18 @@ topalette(
/* Map each pixel to the nearest palette entry */
ImagingSectionEnter(&cookie);
for (y = 0; y < imIn->ysize; y++) {
int r, r0, r1, r2;
int g, g0, g1, g2;
int b, b0, b1, b2;
int r, r0, r1;
int g, g0, g1;
int b, b0, b1;
UINT8 *in = (UINT8 *)imIn->image[y];
UINT8 *out = alpha ? (UINT8 *)imOut->image32[y] : imOut->image8[y];
int *e = errors;
r = r0 = r1 = 0;
g = g0 = g1 = 0;
b = b0 = b1 = b2 = 0;
b = b0 = b1 = 0;
for (x = 0; x < imIn->xsize; x++, in += 4) {
int d2;
INT16 *cache;
r = CLIP8(in[0] + (r + e[3 + 0]) / 16);
@ -1281,38 +1280,28 @@ topalette(
g -= (int)palette->palette[cache[0] * 4 + 1];
b -= (int)palette->palette[cache[0] * 4 + 2];
/* propagate errors (don't ask ;-) */
r2 = r;
d2 = r + r;
r += d2;
e[0] = r + r0;
r += d2;
r0 = r + r1;
r1 = r2;
r += d2;
g2 = g;
d2 = g + g;
g += d2;
e[1] = g + g0;
g += d2;
g0 = g + g1;
g1 = g2;
g += d2;
b2 = b;
d2 = b + b;
b += d2;
e[2] = b + b0;
b += d2;
b0 = b + b1;
b1 = b2;
b += d2;
/* propagate errors */
e[0] = 3 * r + r0;
r0 = 5 * r + r1;
r1 = r;
r = 7 * r;
e[1] = 3 * g + g0;
g0 = 5 * g + g1;
g1 = g;
g = 7 * g;
e[2] = 3 * b + b0;
b0 = 5 * b + b1;
b1 = b;
b = 7 * b;
e += 3;
}
e[0] = b0;
e[1] = b1;
e[2] = b2;
e[0] = r0;
e[1] = g0;
e[2] = b0;
}
ImagingSectionLeave(&cookie);
free(errors);
@ -1385,7 +1374,7 @@ tobilevel(Imaging imOut, Imaging imIn) {
/* map each pixel to black or white, using error diffusion */
ImagingSectionEnter(&cookie);
for (y = 0; y < imIn->ysize; y++) {
int l, l0, l1, l2, d2;
int l, l0, l1;
UINT8 *in = (UINT8 *)imIn->image[y];
UINT8 *out = imOut->image8[y];
@ -1398,14 +1387,10 @@ tobilevel(Imaging imOut, Imaging imIn) {
/* propagate errors */
l -= (int)out[x];
l2 = l;
d2 = l + l;
l += d2;
errors[x] = l + l0;
l += d2;
l0 = l + l1;
l1 = l2;
l += d2;
errors[x] = 3 * l + l0;
l0 = 5 * l + l1;
l1 = l;
l = 7 * l;
}
errors[x] = l0;
@ -1416,7 +1401,7 @@ tobilevel(Imaging imOut, Imaging imIn) {
/* map each pixel to black or white, using error diffusion */
ImagingSectionEnter(&cookie);
for (y = 0; y < imIn->ysize; y++) {
int l, l0, l1, l2, d2;
int l, l0, l1;
UINT8 *in = (UINT8 *)imIn->image[y];
UINT8 *out = imOut->image8[y];
@ -1429,14 +1414,10 @@ tobilevel(Imaging imOut, Imaging imIn) {
/* propagate errors */
l -= (int)out[x];
l2 = l;
d2 = l + l;
l += d2;
errors[x] = l + l0;
l += d2;
l0 = l + l1;
l1 = l2;
l += d2;
errors[x] = 3 * l + l0;
l0 = 5 * l + l1;
l1 = l;
l = 7 * l;
}
errors[x] = l0;