Skip to content

Commit 9efd8a1

Browse files
committed
Improve Dither code readability
1 parent 6d4f2b5 commit 9efd8a1

1 file changed

Lines changed: 29 additions & 48 deletions

File tree

src/libImaging/Convert.c

Lines changed: 29 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1246,19 +1246,18 @@ topalette(
12461246
/* Map each pixel to the nearest palette entry */
12471247
ImagingSectionEnter(&cookie);
12481248
for (y = 0; y < imIn->ysize; y++) {
1249-
int r, r0, r1, r2;
1250-
int g, g0, g1, g2;
1251-
int b, b0, b1, b2;
1249+
int r, r0, r1;
1250+
int g, g0, g1;
1251+
int b, b0, b1;
12521252
UINT8 *in = (UINT8 *)imIn->image[y];
12531253
UINT8 *out = alpha ? (UINT8 *)imOut->image32[y] : imOut->image8[y];
12541254
int *e = errors;
12551255

12561256
r = r0 = r1 = 0;
12571257
g = g0 = g1 = 0;
1258-
b = b0 = b1 = b2 = 0;
1258+
b = b0 = b1 = 0;
12591259

12601260
for (x = 0; x < imIn->xsize; x++, in += 4) {
1261-
int d2;
12621261
INT16 *cache;
12631262

12641263
r = CLIP8(in[0] + (r + e[3 + 0]) / 16);
@@ -1281,31 +1280,21 @@ topalette(
12811280
g -= (int)palette->palette[cache[0] * 4 + 1];
12821281
b -= (int)palette->palette[cache[0] * 4 + 2];
12831282

1284-
/* propagate errors (don't ask ;-) */
1285-
r2 = r;
1286-
d2 = r + r;
1287-
r += d2;
1288-
e[0] = r + r0;
1289-
r += d2;
1290-
r0 = r + r1;
1291-
r1 = r2;
1292-
r += d2;
1293-
g2 = g;
1294-
d2 = g + g;
1295-
g += d2;
1296-
e[1] = g + g0;
1297-
g += d2;
1298-
g0 = g + g1;
1299-
g1 = g2;
1300-
g += d2;
1301-
b2 = b;
1302-
d2 = b + b;
1303-
b += d2;
1304-
e[2] = b + b0;
1305-
b += d2;
1306-
b0 = b + b1;
1307-
b1 = b2;
1308-
b += d2;
1283+
/* propagate errors */
1284+
e[0] = 3*r + r0;
1285+
r0 = 5*r + r1;
1286+
r1 = r;
1287+
r = 7*r;
1288+
1289+
e[1] = 3*g + g0;
1290+
g0 = 5*g + g1;
1291+
g1 = g;
1292+
g = 7*g;
1293+
1294+
e[2] = 3*b + b0;
1295+
b0 = 5*b + b1;
1296+
b1 = b;
1297+
b = 7*b;
13091298

13101299
e += 3;
13111300
}
@@ -1385,7 +1374,7 @@ tobilevel(Imaging imOut, Imaging imIn) {
13851374
/* map each pixel to black or white, using error diffusion */
13861375
ImagingSectionEnter(&cookie);
13871376
for (y = 0; y < imIn->ysize; y++) {
1388-
int l, l0, l1, l2, d2;
1377+
int l, l0, l1;
13891378
UINT8 *in = (UINT8 *)imIn->image[y];
13901379
UINT8 *out = imOut->image8[y];
13911380

@@ -1398,14 +1387,10 @@ tobilevel(Imaging imOut, Imaging imIn) {
13981387

13991388
/* propagate errors */
14001389
l -= (int)out[x];
1401-
l2 = l;
1402-
d2 = l + l;
1403-
l += d2;
1404-
errors[x] = l + l0;
1405-
l += d2;
1406-
l0 = l + l1;
1407-
l1 = l2;
1408-
l += d2;
1390+
errors[x] = 3*l + l0;
1391+
l0 = 5*l + l1;
1392+
l1 = l;
1393+
l = 7*l;
14091394
}
14101395

14111396
errors[x] = l0;
@@ -1416,7 +1401,7 @@ tobilevel(Imaging imOut, Imaging imIn) {
14161401
/* map each pixel to black or white, using error diffusion */
14171402
ImagingSectionEnter(&cookie);
14181403
for (y = 0; y < imIn->ysize; y++) {
1419-
int l, l0, l1, l2, d2;
1404+
int l, l0, l1;
14201405
UINT8 *in = (UINT8 *)imIn->image[y];
14211406
UINT8 *out = imOut->image8[y];
14221407

@@ -1429,14 +1414,10 @@ tobilevel(Imaging imOut, Imaging imIn) {
14291414

14301415
/* propagate errors */
14311416
l -= (int)out[x];
1432-
l2 = l;
1433-
d2 = l + l;
1434-
l += d2;
1435-
errors[x] = l + l0;
1436-
l += d2;
1437-
l0 = l + l1;
1438-
l1 = l2;
1439-
l += d2;
1417+
errors[x] = 3*l + l0;
1418+
l0 = 5*l + l1;
1419+
l1 = l;
1420+
l = 7*l;
14401421
}
14411422

14421423
errors[x] = l0;

0 commit comments

Comments
 (0)