Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit e88490b

Browse files
committed
Precision improvement. ≈60% performance lost
1 parent c39a51b commit e88490b

1 file changed

Lines changed: 13 additions & 16 deletions

File tree

libImaging/AlphaComposite.c

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -50,35 +50,32 @@ ImagingAlphaComposite(Imaging imDst, Imaging imSrc)
5050

5151
for (y = 0; y < imDst->ysize; y++) {
5252

53-
rgba8* pdst = (rgba8*) imDst->image[y];
54-
rgba8* psrc = (rgba8*) imSrc->image[y];
55-
rgba8* pout = (rgba8*) imOut->image[y];
53+
rgba8* dst = (rgba8*) imDst->image[y];
54+
rgba8* src = (rgba8*) imSrc->image[y];
55+
rgba8* out = (rgba8*) imOut->image[y];
5656

5757
for (x = 0; x < imDst->xsize; x ++) {
58-
rgba8 src = psrc[x];
5958

60-
if (src.a == 0) {
59+
if (src->a == 0) {
6160
// Copy 4 bytes at once.
62-
pout[x] = pdst[x];
61+
*out = *dst;
6362
} else {
64-
rgba8 dst = pdst[x];
65-
rgba8* out = &pout[x];
66-
6763
// Integer implementation with increased precision.
6864
// Each variable has extra meaningful bits.
6965
// Divisions are rounded.
7066

71-
UINT16 blend = dst.a * (255 - src.a); // 16 bit max
72-
UINT16 outa = (src.a << 4) + ((blend + 0x8) >> 4); // 12
73-
UINT16 coef1 = (src.a << 16) / outa; // 12
67+
UINT16 blend = dst->a * (255 - src->a); // 16 bit max
68+
UINT16 outa = (src->a << 4) + ((blend << 4) + 127) / 255; // 12
69+
UINT16 coef1 = ((src->a * 255) << 8) / outa; // 12
7470
UINT16 coef2 = (blend << 8) / outa; // 12
7571

76-
out->r = (src.r * coef1 + dst.r * coef2 + 0x800) >> 12;
77-
out->g = (src.g * coef1 + dst.g * coef2 + 0x800) >> 12;
78-
out->b = (src.b * coef1 + dst.b * coef2 + 0x800) >> 12;
79-
out->a = (outa + 0x8) >> 4;
72+
out->r = ((src->r * coef1 + dst->r * coef2 + 0x7ff) / 255) >> 4;
73+
out->g = ((src->g * coef1 + dst->g * coef2 + 0x7ff) / 255) >> 4;
74+
out->b = ((src->b * coef1 + dst->b * coef2 + 0x7ff) / 255) >> 4;
75+
out->a = (outa + 0x7) >> 4;
8076
}
8177

78+
dst++; src++; out++;
8279
}
8380

8481
}

0 commit comments

Comments
 (0)