From c59529d28d5c543b11a0842cf40ef8ad98c437c8 Mon Sep 17 00:00:00 2001 From: Alexander Date: Fri, 1 Sep 2017 08:37:05 +0300 Subject: [PATCH 1/3] use PRECISION_BITS constant in ImagingAlphaComposite use UINT32 instead of UINT16 (a bit faster on x86) --- libImaging/AlphaComposite.c | 28 +++++++++++----------------- 1 file changed, 11 insertions(+), 17 deletions(-) diff --git a/libImaging/AlphaComposite.c b/libImaging/AlphaComposite.c index 00d28f956..81b621123 100644 --- a/libImaging/AlphaComposite.c +++ b/libImaging/AlphaComposite.c @@ -11,6 +11,7 @@ #include "Imaging.h" +#define PRECISION_BITS 7 typedef struct { @@ -49,13 +50,11 @@ ImagingAlphaComposite(Imaging imDst, Imaging imSrc) ImagingCopyInfo(imOut, imDst); for (y = 0; y < imDst->ysize; y++) { - rgba8* dst = (rgba8*) imDst->image[y]; rgba8* src = (rgba8*) imSrc->image[y]; rgba8* out = (rgba8*) imOut->image[y]; for (x = 0; x < imDst->xsize; x ++) { - if (src->a == 0) { // Copy 4 bytes at once. *out = *dst; @@ -64,25 +63,20 @@ ImagingAlphaComposite(Imaging imDst, Imaging imSrc) // Each variable has extra meaningful bits. // Divisions are rounded. - // This code uses trick from Paste.c: - // (a + (2 << (n-1)) - 1) / ((2 << n)-1) - // almost equivalent to: - // tmp = a + (2 << (n-1)), ((tmp >> n) + tmp) >> n - UINT32 tmpr, tmpg, tmpb; - UINT16 blend = dst->a * (255 - src->a); - UINT16 outa255 = src->a * 255 + blend; + UINT32 blend = dst->a * (255 - src->a); + UINT32 outa255 = src->a * 255 + blend; // There we use 7 bits for precision. // We could use more, but we go beyond 32 bits. - UINT16 coef1 = src->a * 255 * 255 * 128 / outa255; - UINT16 coef2 = 255 * 128 - coef1; + UINT32 coef1 = src->a * 255 * 255 * (1<r * coef1 + dst->r * coef2 + (0x80 << 7); - out->r = SHIFTFORDIV255(tmpr) >> 7; - tmpg = src->g * coef1 + dst->g * coef2 + (0x80 << 7); - out->g = SHIFTFORDIV255(tmpg) >> 7; - tmpb = src->b * coef1 + dst->b * coef2 + (0x80 << 7); - out->b = SHIFTFORDIV255(tmpb) >> 7; + tmpr = src->r * coef1 + dst->r * coef2; + tmpg = src->g * coef1 + dst->g * coef2; + tmpb = src->b * coef1 + dst->b * coef2; + out->r = SHIFTFORDIV255(tmpr + (0x80<> PRECISION_BITS; + out->g = SHIFTFORDIV255(tmpg + (0x80<> PRECISION_BITS; + out->b = SHIFTFORDIV255(tmpb + (0x80<> PRECISION_BITS; out->a = SHIFTFORDIV255(outa255 + 0x80); } From d4b2fa7348740edd0ea806c135904d94b4694e23 Mon Sep 17 00:00:00 2001 From: Alexander Date: Fri, 1 Sep 2017 13:32:37 +0300 Subject: [PATCH 2/3] Fix erroneous condition which was never true --- PIL/Image.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/PIL/Image.py b/PIL/Image.py index d9660dce7..5293cdf08 100644 --- a/PIL/Image.py +++ b/PIL/Image.py @@ -1426,7 +1426,7 @@ class Image(object): box = dest + (dest[0] + overlay.width, dest[1] + overlay.height) # destination image. don't copy if we're using the whole image. - if dest == (0,0) + self.size: + if box == (0,0) + self.size: background = self else: background = self.crop(box) From 62ece989dc2fcd3536f1f6fa5b9561402634131b Mon Sep 17 00:00:00 2001 From: Alexander Date: Fri, 1 Sep 2017 13:36:25 +0300 Subject: [PATCH 3/3] Accept lists as well --- PIL/Image.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/PIL/Image.py b/PIL/Image.py index 5293cdf08..864c2bde5 100644 --- a/PIL/Image.py +++ b/PIL/Image.py @@ -1400,9 +1400,9 @@ class Image(object): Performance Note: Not currently implemented in-place in the core layer. """ - if not isinstance(source, tuple): + if not isinstance(source, (list, tuple)): raise ValueError("Source must be a tuple") - if not isinstance(dest, tuple): + if not isinstance(dest, (list, tuple)): raise ValueError("Destination must be a tuple") if not len(source) in (2, 4): raise ValueError("Source must be a 2 or 4-tuple")