From b6638d8155d28628c1f089592e525785bff6ed1a Mon Sep 17 00:00:00 2001 From: Alexander Date: Mon, 7 Aug 2017 01:11:05 +0300 Subject: [PATCH 01/17] divide coefficients before applying --- _imaging.c | 8 ++++++-- libImaging/Filter.c | 6 +++--- libImaging/Imaging.h | 2 +- 3 files changed, 10 insertions(+), 6 deletions(-) diff --git a/_imaging.c b/_imaging.c index 7b3380b4093..ff38a19b6a3 100644 --- a/_imaging.c +++ b/_imaging.c @@ -836,7 +836,7 @@ _filter(ImagingObject* self, PyObject* args) Py_ssize_t kernelsize; FLOAT32* kerneldata; - int xsize, ysize; + int xsize, ysize, i; float divisor, offset; PyObject* kernel = NULL; if (!PyArg_ParseTuple(args, "(ii)ffO", &xsize, &ysize, @@ -852,8 +852,12 @@ _filter(ImagingObject* self, PyObject* args) return ImagingError_ValueError("bad kernel size"); } + for (i = 0; i < kernelsize; ++i) { + kerneldata[i] /= divisor; + } + imOut = PyImagingNew( - ImagingFilter(self->image, xsize, ysize, kerneldata, offset, divisor) + ImagingFilter(self->image, xsize, ysize, kerneldata, offset) ); free(kerneldata); diff --git a/libImaging/Filter.c b/libImaging/Filter.c index 4c8b8158771..50946c8ceb0 100644 --- a/libImaging/Filter.c +++ b/libImaging/Filter.c @@ -73,7 +73,7 @@ ImagingExpand(Imaging imIn, int xmargin, int ymargin, int mode) Imaging ImagingFilter(Imaging im, int xsize, int ysize, const FLOAT32* kernel, - FLOAT32 offset, FLOAT32 divisor) + FLOAT32 offset) { Imaging imOut; int x, y; @@ -138,7 +138,7 @@ ImagingFilter(Imaging im, int xsize, int ysize, const FLOAT32* kernel, for (y = 1; y < im->ysize-1; y++) { imOut->image[y][0] = im->image8[y][0]; for (x = 1; x < im->xsize-1; x++) { - sum = KERNEL3x3(im->image8, kernel, 1) / divisor + offset; + sum = KERNEL3x3(im->image8, kernel, 1) + offset; if (sum <= 0) imOut->image8[y][x] = 0; else if (sum >= 255) @@ -159,7 +159,7 @@ ImagingFilter(Imaging im, int xsize, int ysize, const FLOAT32* kernel, for (x = 0; x < 2; x++) imOut->image8[y][x] = im->image8[y][x]; for (; x < im->xsize-2; x++) { - sum = KERNEL5x5(im->image8, kernel, 1) / divisor + offset; + sum = KERNEL5x5(im->image8, kernel, 1) + offset; if (sum <= 0) imOut->image8[y][x] = 0; else if (sum >= 255) diff --git a/libImaging/Imaging.h b/libImaging/Imaging.h index 99fff7f6713..81dc39c60aa 100644 --- a/libImaging/Imaging.h +++ b/libImaging/Imaging.h @@ -261,7 +261,7 @@ extern Imaging ImagingFillLinearGradient(const char* mode); extern Imaging ImagingFillRadialGradient(const char* mode); extern Imaging ImagingFilter( Imaging im, int xsize, int ysize, const FLOAT32* kernel, - FLOAT32 offset, FLOAT32 divisor); + FLOAT32 offset); extern Imaging ImagingFlipLeftRight(Imaging imOut, Imaging imIn); extern Imaging ImagingFlipTopBottom(Imaging imOut, Imaging imIn); extern Imaging ImagingGaussianBlur(Imaging imOut, Imaging imIn, float radius, From f71b3a8ed7ddea48e832672edd05d45469a77558 Mon Sep 17 00:00:00 2001 From: Alexander Date: Sat, 12 Aug 2017 22:12:59 +0300 Subject: [PATCH 02/17] clip8 function --- libImaging/Filter.c | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/libImaging/Filter.c b/libImaging/Filter.c index 50946c8ceb0..288f1e461d6 100644 --- a/libImaging/Filter.c +++ b/libImaging/Filter.c @@ -26,6 +26,16 @@ #include "Imaging.h" + +static inline UINT8 clip8(float in) +{ + if (in <= 0.0) + return 0; + if (in >= 255.0) + return 255; + return (UINT8) in; +} + Imaging ImagingExpand(Imaging imIn, int xmargin, int ymargin, int mode) { @@ -134,17 +144,12 @@ ImagingFilter(Imaging im, int xsize, int ysize, const FLOAT32* kernel, if (xsize == 3) { /* 3x3 kernel. */ for (x = 0; x < im->xsize; x++) - imOut->image[0][x] = im->image8[0][x]; + imOut->image8[0][x] = im->image8[0][x]; for (y = 1; y < im->ysize-1; y++) { - imOut->image[y][0] = im->image8[y][0]; + imOut->image8[y][0] = im->image8[y][0]; for (x = 1; x < im->xsize-1; x++) { sum = KERNEL3x3(im->image8, kernel, 1) + offset; - if (sum <= 0) - imOut->image8[y][x] = 0; - else if (sum >= 255) - imOut->image8[y][x] = 255; - else - imOut->image8[y][x] = (UINT8) sum; + imOut->image8[y][x] = clip8(sum); } imOut->image8[y][x] = im->image8[y][x]; } @@ -160,12 +165,7 @@ ImagingFilter(Imaging im, int xsize, int ysize, const FLOAT32* kernel, imOut->image8[y][x] = im->image8[y][x]; for (; x < im->xsize-2; x++) { sum = KERNEL5x5(im->image8, kernel, 1) + offset; - if (sum <= 0) - imOut->image8[y][x] = 0; - else if (sum >= 255) - imOut->image8[y][x] = 255; - else - imOut->image8[y][x] = (UINT8) sum; + imOut->image8[y][x] = clip8(sum); } for (; x < im->xsize; x++) imOut->image8[y][x] = im->image8[y][x]; From 5353d28e35845658c8fa97ee3f8e85673153eb89 Mon Sep 17 00:00:00 2001 From: Alexander Date: Sat, 12 Aug 2017 22:18:24 +0300 Subject: [PATCH 03/17] copy first and last lines --- libImaging/Filter.c | 18 +++++++----------- 1 file changed, 7 insertions(+), 11 deletions(-) diff --git a/libImaging/Filter.c b/libImaging/Filter.c index 288f1e461d6..7b17dd1b897 100644 --- a/libImaging/Filter.c +++ b/libImaging/Filter.c @@ -143,8 +143,7 @@ ImagingFilter(Imaging im, int xsize, int ysize, const FLOAT32* kernel, if (xsize == 3) { /* 3x3 kernel. */ - for (x = 0; x < im->xsize; x++) - imOut->image8[0][x] = im->image8[0][x]; + memcpy(imOut->image[0], im->image[0], im->linesize); for (y = 1; y < im->ysize-1; y++) { imOut->image8[y][0] = im->image8[y][0]; for (x = 1; x < im->xsize-1; x++) { @@ -153,14 +152,12 @@ ImagingFilter(Imaging im, int xsize, int ysize, const FLOAT32* kernel, } imOut->image8[y][x] = im->image8[y][x]; } - for (x = 0; x < im->xsize; x++) - imOut->image8[y][x] = im->image8[y][x]; + memcpy(imOut->image[y], im->image[y], im->linesize); } else { /* 5x5 kernel. */ - for (y = 0; y < 2; y++) - for (x = 0; x < im->xsize; x++) - imOut->image8[y][x] = im->image8[y][x]; - for (; y < im->ysize-2; y++) { + memcpy(imOut->image[0], im->image[0], im->linesize); + memcpy(imOut->image[1], im->image[1], im->linesize); + for (y = 2; y < im->ysize-2; y++) { for (x = 0; x < 2; x++) imOut->image8[y][x] = im->image8[y][x]; for (; x < im->xsize-2; x++) { @@ -170,9 +167,8 @@ ImagingFilter(Imaging im, int xsize, int ysize, const FLOAT32* kernel, for (; x < im->xsize; x++) imOut->image8[y][x] = im->image8[y][x]; } - for (; y < im->ysize; y++) - for (x = 0; x < im->xsize; x++) - imOut->image8[y][x] = im->image8[y][x]; + memcpy(imOut->image[y], im->image[y], im->linesize); + memcpy(imOut->image[y+1], im->image[y+1], im->linesize); } return imOut; } From 38eab1a43f58607fd45c4c878e073feb14d40c00 Mon Sep 17 00:00:00 2001 From: Alexander Date: Sat, 12 Aug 2017 23:07:08 +0300 Subject: [PATCH 04/17] do 3x3 filtering in separate function --- libImaging/Filter.c | 61 +++++++++++++++++++++++++++------------------ 1 file changed, 37 insertions(+), 24 deletions(-) diff --git a/libImaging/Filter.c b/libImaging/Filter.c index 7b17dd1b897..651fa9c1525 100644 --- a/libImaging/Filter.c +++ b/libImaging/Filter.c @@ -81,13 +81,47 @@ ImagingExpand(Imaging imIn, int xmargin, int ymargin, int mode) return imOut; } + +void +ImagingFilter3x3(Imaging imOut, Imaging im, const float* kernel, + float offset) +{ +#define KERNEL3x3(in_1, in, in1, kernel, d) ( \ + (UINT8) in1[x-d] * kernel[0] + \ + (UINT8) in1[x] * kernel[1] + \ + (UINT8) in1[x+d] * kernel[2] + \ + (UINT8) in0[x-d] * kernel[3] + \ + (UINT8) in0[x] * kernel[4] + \ + (UINT8) in0[x+d] * kernel[5] + \ + (UINT8) in_1[x-d] * kernel[6] + \ + (UINT8) in_1[x] * kernel[7] + \ + (UINT8) in_1[x+d] * kernel[8]) + + int x, y; + + memcpy(imOut->image[0], im->image[0], im->linesize); + for (y = 1; y < im->ysize-1; y++) { + UINT8* in_1 = (UINT8*) im->image[y-1]; + UINT8* in0 = (UINT8*) im->image[y]; + UINT8* in1 = (UINT8*) im->image[y+1]; + UINT8* out = (UINT8*) imOut->image[y]; + + out[0] = in0[0]; + for (x = 1; x < im->xsize-1; x++) { + float sum = KERNEL3x3(in_1, in, in1, kernel, 1) + offset; + out[x] = clip8(sum); + } + out[x] = in0[x]; + } + memcpy(imOut->image[y], im->image[y], im->linesize); +} + Imaging ImagingFilter(Imaging im, int xsize, int ysize, const FLOAT32* kernel, FLOAT32 offset) { Imaging imOut; int x, y; - FLOAT32 sum; if (!im || strcmp(im->mode, "L") != 0) return (Imaging) ImagingError_ModeError(); @@ -102,18 +136,6 @@ ImagingFilter(Imaging im, int xsize, int ysize, const FLOAT32* kernel, if (!imOut) return NULL; - /* brute force kernel implementations */ -#define KERNEL3x3(image, kernel, d) ( \ - (int) image[y+1][x-d] * kernel[0] + \ - (int) image[y+1][x] * kernel[1] + \ - (int) image[y+1][x+d] * kernel[2] + \ - (int) image[y][x-d] * kernel[3] + \ - (int) image[y][x] * kernel[4] + \ - (int) image[y][x+d] * kernel[5] + \ - (int) image[y-1][x-d] * kernel[6] + \ - (int) image[y-1][x] * kernel[7] + \ - (int) image[y-1][x+d] * kernel[8]) - #define KERNEL5x5(image, kernel, d) ( \ (int) image[y+2][x-d-d] * kernel[0] + \ (int) image[y+2][x-d] * kernel[1] + \ @@ -143,16 +165,7 @@ ImagingFilter(Imaging im, int xsize, int ysize, const FLOAT32* kernel, if (xsize == 3) { /* 3x3 kernel. */ - memcpy(imOut->image[0], im->image[0], im->linesize); - for (y = 1; y < im->ysize-1; y++) { - imOut->image8[y][0] = im->image8[y][0]; - for (x = 1; x < im->xsize-1; x++) { - sum = KERNEL3x3(im->image8, kernel, 1) + offset; - imOut->image8[y][x] = clip8(sum); - } - imOut->image8[y][x] = im->image8[y][x]; - } - memcpy(imOut->image[y], im->image[y], im->linesize); + ImagingFilter3x3(imOut, im, kernel, offset); } else { /* 5x5 kernel. */ memcpy(imOut->image[0], im->image[0], im->linesize); @@ -161,7 +174,7 @@ ImagingFilter(Imaging im, int xsize, int ysize, const FLOAT32* kernel, for (x = 0; x < 2; x++) imOut->image8[y][x] = im->image8[y][x]; for (; x < im->xsize-2; x++) { - sum = KERNEL5x5(im->image8, kernel, 1) + offset; + float sum = KERNEL5x5(im->image8, kernel, 1) + offset; imOut->image8[y][x] = clip8(sum); } for (; x < im->xsize; x++) From b11bba108e6a2e74a87c27c3592d40fe0eac8cf5 Mon Sep 17 00:00:00 2001 From: Alexander Date: Sun, 13 Aug 2017 00:03:50 +0300 Subject: [PATCH 05/17] accept multiband images in filter (noop) --- PIL/Image.py | 4 +++- libImaging/Filter.c | 53 +++++++++++++++++++++++++-------------------- 2 files changed, 33 insertions(+), 24 deletions(-) diff --git a/PIL/Image.py b/PIL/Image.py index 49c9f6ab204..6a28f7cda94 100644 --- a/PIL/Image.py +++ b/PIL/Image.py @@ -1112,7 +1112,9 @@ def filter(self, filter): raise TypeError("filter argument should be ImageFilter.Filter " + "instance or class") - if self.im.bands == 1: + multiband = getattr(filter, 'is_multiband', False) + + if self.im.bands == 1 or multiband: return self._new(filter.filter(self.im)) # fix to handle multiband images since _imaging doesn't ims = [] diff --git a/libImaging/Filter.c b/libImaging/Filter.c index 86657358854..43e25c196ca 100644 --- a/libImaging/Filter.c +++ b/libImaging/Filter.c @@ -100,21 +100,26 @@ ImagingFilter3x3(Imaging imOut, Imaging im, const float* kernel, (UINT8) in_1[x] * kernel[7] + \ (UINT8) in_1[x+d] * kernel[8]) - int x, y; + int x, y = 0; memcpy(imOut->image[0], im->image[0], im->linesize); - for (y = 1; y < im->ysize-1; y++) { - UINT8* in_1 = (UINT8*) im->image[y-1]; - UINT8* in0 = (UINT8*) im->image[y]; - UINT8* in1 = (UINT8*) im->image[y+1]; - UINT8* out = (UINT8*) imOut->image[y]; - - out[0] = in0[0]; - for (x = 1; x < im->xsize-1; x++) { - float sum = KERNEL3x3(in_1, in, in1, kernel, 1) + offset; - out[x] = clip8(sum); - } - out[x] = in0[x]; + if (im->bands == 1) { + for (y = 1; y < im->ysize-1; y++) { + UINT8* in_1 = (UINT8*) im->image[y-1]; + UINT8* in0 = (UINT8*) im->image[y]; + UINT8* in1 = (UINT8*) im->image[y+1]; + UINT8* out = (UINT8*) imOut->image[y]; + + out[0] = in0[0]; + for (x = 1; x < im->xsize-1; x++) { + out[x] = clip8(KERNEL3x3(in_1, in, in1, kernel, 1) + offset); + } + out[x] = in0[x]; + } + } else if (im->bands == 3) { + printf("%s\n", "hi there"); + for (y = 1; y < im->ysize-1; y++) { + } } memcpy(imOut->image[y], im->image[y], im->linesize); } @@ -124,10 +129,10 @@ ImagingFilter(Imaging im, int xsize, int ysize, const FLOAT32* kernel, FLOAT32 offset) { Imaging imOut; - int x, y; + int x, y = 0; ImagingSectionCookie cookie; - if (!im || strcmp(im->mode, "L") != 0) + if ( ! im || im->type != IMAGING_TYPE_UINT8) return (Imaging) ImagingError_ModeError(); if (im->xsize < xsize || im->ysize < ysize) @@ -178,15 +183,17 @@ ImagingFilter(Imaging im, int xsize, int ysize, const FLOAT32* kernel, /* 5x5 kernel. */ memcpy(imOut->image[0], im->image[0], im->linesize); memcpy(imOut->image[1], im->image[1], im->linesize); - for (y = 2; y < im->ysize-2; y++) { - for (x = 0; x < 2; x++) - imOut->image8[y][x] = im->image8[y][x]; - for (; x < im->xsize-2; x++) { - float sum = KERNEL5x5(im->image8, kernel, 1) + offset; - imOut->image8[y][x] = clip8(sum); + if (im->bands == 1) { + for (y = 2; y < im->ysize-2; y++) { + for (x = 0; x < 2; x++) + imOut->image8[y][x] = im->image8[y][x]; + for (; x < im->xsize-2; x++) { + float sum = KERNEL5x5(im->image8, kernel, 1) + offset; + imOut->image8[y][x] = clip8(sum); + } + for (; x < im->xsize; x++) + imOut->image8[y][x] = im->image8[y][x]; } - for (; x < im->xsize; x++) - imOut->image8[y][x] = im->image8[y][x]; } memcpy(imOut->image[y], im->image[y], im->linesize); memcpy(imOut->image[y+1], im->image[y+1], im->linesize); From f3a33da9f3f541209bbf1701a8d3537c70f6e9fe Mon Sep 17 00:00:00 2001 From: Alexander Date: Sun, 13 Aug 2017 00:15:57 +0300 Subject: [PATCH 06/17] 3x3 kernel --- libImaging/Filter.c | 51 ++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 46 insertions(+), 5 deletions(-) diff --git a/libImaging/Filter.c b/libImaging/Filter.c index 43e25c196ca..3196add0d6a 100644 --- a/libImaging/Filter.c +++ b/libImaging/Filter.c @@ -27,6 +27,13 @@ #include "Imaging.h" +#ifdef WORDS_BIGENDIAN + #define MAKE_UINT32(u0, u1, u2, u3) (u3 | (u2<<8) | (u1<<16) | (u0<<24)) +#else + #define MAKE_UINT32(u0, u1, u2, u3) (u0 | (u1<<8) | (u2<<16) | (u3<<24)) +#endif + + static inline UINT8 clip8(float in) { if (in <= 0.0) @@ -89,7 +96,7 @@ void ImagingFilter3x3(Imaging imOut, Imaging im, const float* kernel, float offset) { -#define KERNEL3x3(in_1, in, in1, kernel, d) ( \ +#define KERNEL3x3(in_1, in, in1, x, kernel, d) ( \ (UINT8) in1[x-d] * kernel[0] + \ (UINT8) in1[x] * kernel[1] + \ (UINT8) in1[x+d] * kernel[2] + \ @@ -100,7 +107,7 @@ ImagingFilter3x3(Imaging imOut, Imaging im, const float* kernel, (UINT8) in_1[x] * kernel[7] + \ (UINT8) in_1[x+d] * kernel[8]) - int x, y = 0; + int x = 0, y = 0; memcpy(imOut->image[0], im->image[0], im->linesize); if (im->bands == 1) { @@ -112,13 +119,47 @@ ImagingFilter3x3(Imaging imOut, Imaging im, const float* kernel, out[0] = in0[0]; for (x = 1; x < im->xsize-1; x++) { - out[x] = clip8(KERNEL3x3(in_1, in, in1, kernel, 1) + offset); + float ss = KERNEL3x3(in_1, in, in1, x, kernel, 1); + out[x] = clip8(ss + offset); } out[x] = in0[x]; } - } else if (im->bands == 3) { - printf("%s\n", "hi there"); + } else { for (y = 1; y < im->ysize-1; y++) { + UINT8* in_1 = (UINT8*) im->image[y-1]; + UINT8* in0 = (UINT8*) im->image[y]; + UINT8* in1 = (UINT8*) im->image[y+1]; + UINT32* out = (UINT32*) imOut->image[y]; + + out[0] = ((UINT32*) in0)[0]; + if (im->bands == 2) { + for (x = 1; x < im->xsize-1; x++) { + float ss0 = KERNEL3x3(in_1, in, in1, x*4+0, kernel, 4); + float ss3 = KERNEL3x3(in_1, in, in1, x*4+3, kernel, 4); + out[x] = MAKE_UINT32( + clip8(ss0 + offset), 0, 0, clip8(ss3 + offset)); + } + } else if (im->bands == 3) { + for (x = 1; x < im->xsize-1; x++) { + float ss0 = KERNEL3x3(in_1, in, in1, x*4+0, kernel, 4); + float ss1 = KERNEL3x3(in_1, in, in1, x*4+1, kernel, 4); + float ss2 = KERNEL3x3(in_1, in, in1, x*4+2, kernel, 4); + out[x] = MAKE_UINT32( + clip8(ss0 + offset), clip8(ss1 + offset), + clip8(ss2 + offset), 0); + } + } else if (im->bands == 4) { + for (x = 1; x < im->xsize-1; x++) { + float ss0 = KERNEL3x3(in_1, in, in1, x*4+0, kernel, 4); + float ss1 = KERNEL3x3(in_1, in, in1, x*4+1, kernel, 4); + float ss2 = KERNEL3x3(in_1, in, in1, x*4+2, kernel, 4); + float ss3 = KERNEL3x3(in_1, in, in1, x*4+3, kernel, 4); + out[x] = MAKE_UINT32( + clip8(ss0 + offset), clip8(ss1 + offset), + clip8(ss2 + offset), clip8(ss3 + offset)); + } + } + out[x] = ((UINT32*) in0)[x]; } } memcpy(imOut->image[y], im->image[y], im->linesize); From 8d4681ff67083c5343a2cdd5e0156edd656dd757 Mon Sep 17 00:00:00 2001 From: Alexander Date: Sun, 13 Aug 2017 00:33:52 +0300 Subject: [PATCH 07/17] 5x5 kernel --- libImaging/Filter.c | 167 +++++++++++++++++++++++++++++--------------- 1 file changed, 112 insertions(+), 55 deletions(-) diff --git a/libImaging/Filter.c b/libImaging/Filter.c index 3196add0d6a..fd723275e06 100644 --- a/libImaging/Filter.c +++ b/libImaging/Filter.c @@ -96,7 +96,7 @@ void ImagingFilter3x3(Imaging imOut, Imaging im, const float* kernel, float offset) { -#define KERNEL3x3(in_1, in, in1, x, kernel, d) ( \ +#define KERNEL3x3(in_1, in0, in1, x, kernel, d) ( \ (UINT8) in1[x-d] * kernel[0] + \ (UINT8) in1[x] * kernel[1] + \ (UINT8) in1[x+d] * kernel[2] + \ @@ -119,7 +119,7 @@ ImagingFilter3x3(Imaging imOut, Imaging im, const float* kernel, out[0] = in0[0]; for (x = 1; x < im->xsize-1; x++) { - float ss = KERNEL3x3(in_1, in, in1, x, kernel, 1); + float ss = KERNEL3x3(in_1, in0, in1, x, kernel, 1); out[x] = clip8(ss + offset); } out[x] = in0[x]; @@ -134,26 +134,26 @@ ImagingFilter3x3(Imaging imOut, Imaging im, const float* kernel, out[0] = ((UINT32*) in0)[0]; if (im->bands == 2) { for (x = 1; x < im->xsize-1; x++) { - float ss0 = KERNEL3x3(in_1, in, in1, x*4+0, kernel, 4); - float ss3 = KERNEL3x3(in_1, in, in1, x*4+3, kernel, 4); + float ss0 = KERNEL3x3(in_1, in0, in1, x*4+0, kernel, 4); + float ss3 = KERNEL3x3(in_1, in0, in1, x*4+3, kernel, 4); out[x] = MAKE_UINT32( clip8(ss0 + offset), 0, 0, clip8(ss3 + offset)); } } else if (im->bands == 3) { for (x = 1; x < im->xsize-1; x++) { - float ss0 = KERNEL3x3(in_1, in, in1, x*4+0, kernel, 4); - float ss1 = KERNEL3x3(in_1, in, in1, x*4+1, kernel, 4); - float ss2 = KERNEL3x3(in_1, in, in1, x*4+2, kernel, 4); + float ss0 = KERNEL3x3(in_1, in0, in1, x*4+0, kernel, 4); + float ss1 = KERNEL3x3(in_1, in0, in1, x*4+1, kernel, 4); + float ss2 = KERNEL3x3(in_1, in0, in1, x*4+2, kernel, 4); out[x] = MAKE_UINT32( clip8(ss0 + offset), clip8(ss1 + offset), clip8(ss2 + offset), 0); } } else if (im->bands == 4) { for (x = 1; x < im->xsize-1; x++) { - float ss0 = KERNEL3x3(in_1, in, in1, x*4+0, kernel, 4); - float ss1 = KERNEL3x3(in_1, in, in1, x*4+1, kernel, 4); - float ss2 = KERNEL3x3(in_1, in, in1, x*4+2, kernel, 4); - float ss3 = KERNEL3x3(in_1, in, in1, x*4+3, kernel, 4); + float ss0 = KERNEL3x3(in_1, in0, in1, x*4+0, kernel, 4); + float ss1 = KERNEL3x3(in_1, in0, in1, x*4+1, kernel, 4); + float ss2 = KERNEL3x3(in_1, in0, in1, x*4+2, kernel, 4); + float ss3 = KERNEL3x3(in_1, in0, in1, x*4+3, kernel, 4); out[x] = MAKE_UINT32( clip8(ss0 + offset), clip8(ss1 + offset), clip8(ss2 + offset), clip8(ss3 + offset)); @@ -165,12 +165,111 @@ ImagingFilter3x3(Imaging imOut, Imaging im, const float* kernel, memcpy(imOut->image[y], im->image[y], im->linesize); } + +void +ImagingFilter5x5(Imaging imOut, Imaging im, const float* kernel, + float offset) +{ +#define KERNEL5x5(in_2, in_1, in0, in1, in2, x, kernel, d) ( \ + (UINT8) in2[x-d-d] * kernel[0] + \ + (UINT8) in2[x-d] * kernel[1] + \ + (UINT8) in2[x] * kernel[2] + \ + (UINT8) in2[x+d] * kernel[3] + \ + (UINT8) in2[x+d+d] * kernel[4] + \ + (UINT8) in1[x-d-d] * kernel[5] + \ + (UINT8) in1[x-d] * kernel[6] + \ + (UINT8) in1[x] * kernel[7] + \ + (UINT8) in1[x+d] * kernel[8] + \ + (UINT8) in1[x+d+d] * kernel[9] + \ + (UINT8) in0[x-d-d] * kernel[10] + \ + (UINT8) in0[x-d] * kernel[11] + \ + (UINT8) in0[x] * kernel[12] + \ + (UINT8) in0[x+d] * kernel[13] + \ + (UINT8) in0[x+d+d] * kernel[14] + \ + (UINT8) in_1[x-d-d] * kernel[15] + \ + (UINT8) in_1[x-d] * kernel[16] + \ + (UINT8) in_1[x] * kernel[17] + \ + (UINT8) in_1[x+d] * kernel[18] + \ + (UINT8) in_1[x+d+d] * kernel[19] + \ + (UINT8) in_2[x-d-d] * kernel[20] + \ + (UINT8) in_2[x-d] * kernel[21] + \ + (UINT8) in_2[x] * kernel[22] + \ + (UINT8) in_2[x+d] * kernel[23] + \ + (UINT8) in_2[x+d+d] * kernel[24]) + + int x = 0, y = 0; + + memcpy(imOut->image[0], im->image[0], im->linesize); + memcpy(imOut->image[1], im->image[1], im->linesize); + if (im->bands == 1) { + for (y = 2; y < im->ysize-2; y++) { + UINT8* in_2 = (UINT8*) im->image[y-2]; + UINT8* in_1 = (UINT8*) im->image[y-1]; + UINT8* in0 = (UINT8*) im->image[y]; + UINT8* in1 = (UINT8*) im->image[y+1]; + UINT8* in2 = (UINT8*) im->image[y+2]; + UINT8* out = (UINT8*) imOut->image[y]; + + out[0] = in0[0]; + out[1] = in0[1]; + for (x = 2; x < im->xsize-2; x++) { + float ss = KERNEL5x5(in_2, in_1, in0, in1, in2, x, kernel, 1); + out[x] = clip8(ss + offset); + } + out[x+0] = in0[x+0]; + out[x+1] = in0[x+1]; + } + } else { + for (y = 2; y < im->ysize-2; y++) { + UINT8* in_2 = (UINT8*) im->image[y-2]; + UINT8* in_1 = (UINT8*) im->image[y-1]; + UINT8* in0 = (UINT8*) im->image[y]; + UINT8* in1 = (UINT8*) im->image[y+1]; + UINT8* in2 = (UINT8*) im->image[y+2]; + UINT32* out = (UINT32*) imOut->image[y]; + + out[0] = ((UINT32*) in0)[0]; + out[1] = ((UINT32*) in0)[1]; + if (im->bands == 2) { + for (x = 2; x < im->xsize-2; x++) { + float ss0 = KERNEL5x5(in_2, in_1, in0, in1, in2, x*4+0, kernel, 4); + float ss3 = KERNEL5x5(in_2, in_1, in0, in1, in2, x*4+3, kernel, 4); + out[x] = MAKE_UINT32( + clip8(ss0 + offset), 0, 0, clip8(ss3 + offset)); + } + } else if (im->bands == 3) { + for (x = 2; x < im->xsize-2; x++) { + float ss0 = KERNEL5x5(in_2, in_1, in0, in1, in2, x*4+0, kernel, 4); + float ss1 = KERNEL5x5(in_2, in_1, in0, in1, in2, x*4+1, kernel, 4); + float ss2 = KERNEL5x5(in_2, in_1, in0, in1, in2, x*4+2, kernel, 4); + out[x] = MAKE_UINT32( + clip8(ss0 + offset), clip8(ss1 + offset), + clip8(ss2 + offset), 0); + } + } else if (im->bands == 4) { + for (x = 2; x < im->xsize-2; x++) { + float ss0 = KERNEL5x5(in_2, in_1, in0, in1, in2, x*4+0, kernel, 4); + float ss1 = KERNEL5x5(in_2, in_1, in0, in1, in2, x*4+1, kernel, 4); + float ss2 = KERNEL5x5(in_2, in_1, in0, in1, in2, x*4+2, kernel, 4); + float ss3 = KERNEL5x5(in_2, in_1, in0, in1, in2, x*4+3, kernel, 4); + out[x] = MAKE_UINT32( + clip8(ss0 + offset), clip8(ss1 + offset), + clip8(ss2 + offset), clip8(ss3 + offset)); + } + } + out[x] = ((UINT32*) in0)[x]; + out[x+1] = ((UINT32*) in0)[x+1]; + } + } + memcpy(imOut->image[y], im->image[y], im->linesize); + memcpy(imOut->image[y+1], im->image[y+1], im->linesize); +} + Imaging ImagingFilter(Imaging im, int xsize, int ysize, const FLOAT32* kernel, FLOAT32 offset) { Imaging imOut; - int x, y = 0; ImagingSectionCookie cookie; if ( ! im || im->type != IMAGING_TYPE_UINT8) @@ -189,55 +288,13 @@ ImagingFilter(Imaging im, int xsize, int ysize, const FLOAT32* kernel, // Add one time for rounding offset += 0.5; -#define KERNEL5x5(image, kernel, d) ( \ - (int) image[y+2][x-d-d] * kernel[0] + \ - (int) image[y+2][x-d] * kernel[1] + \ - (int) image[y+2][x] * kernel[2] + \ - (int) image[y+2][x+d] * kernel[3] + \ - (int) image[y+2][x+d+d] * kernel[4] + \ - (int) image[y+1][x-d-d] * kernel[5] + \ - (int) image[y+1][x-d] * kernel[6] + \ - (int) image[y+1][x] * kernel[7] + \ - (int) image[y+1][x+d] * kernel[8] + \ - (int) image[y+1][x+d+d] * kernel[9] + \ - (int) image[y][x-d-d] * kernel[10] + \ - (int) image[y][x-d] * kernel[11] + \ - (int) image[y][x] * kernel[12] + \ - (int) image[y][x+d] * kernel[13] + \ - (int) image[y][x+d+d] * kernel[14] + \ - (int) image[y-1][x-d-d] * kernel[15] + \ - (int) image[y-1][x-d] * kernel[16] + \ - (int) image[y-1][x] * kernel[17] + \ - (int) image[y-1][x+d] * kernel[18] + \ - (int) image[y-1][x+d+d] * kernel[19] + \ - (int) image[y-2][x-d-d] * kernel[20] + \ - (int) image[y-2][x-d] * kernel[21] + \ - (int) image[y-2][x] * kernel[22] + \ - (int) image[y-2][x+d] * kernel[23] + \ - (int) image[y-2][x+d+d] * kernel[24]) - ImagingSectionEnter(&cookie); if (xsize == 3) { /* 3x3 kernel. */ ImagingFilter3x3(imOut, im, kernel, offset); } else { /* 5x5 kernel. */ - memcpy(imOut->image[0], im->image[0], im->linesize); - memcpy(imOut->image[1], im->image[1], im->linesize); - if (im->bands == 1) { - for (y = 2; y < im->ysize-2; y++) { - for (x = 0; x < 2; x++) - imOut->image8[y][x] = im->image8[y][x]; - for (; x < im->xsize-2; x++) { - float sum = KERNEL5x5(im->image8, kernel, 1) + offset; - imOut->image8[y][x] = clip8(sum); - } - for (; x < im->xsize; x++) - imOut->image8[y][x] = im->image8[y][x]; - } - } - memcpy(imOut->image[y], im->image[y], im->linesize); - memcpy(imOut->image[y+1], im->image[y+1], im->linesize); + ImagingFilter5x5(imOut, im, kernel, offset); } ImagingSectionLeave(&cookie); return imOut; From b1c1a22401395b0b3c56f763a96cf4b56192f231 Mon Sep 17 00:00:00 2001 From: Alexander Date: Sun, 13 Aug 2017 00:37:06 +0300 Subject: [PATCH 08/17] turn on multiband filters for kernels and gaussian --- PIL/ImageFilter.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/PIL/ImageFilter.py b/PIL/ImageFilter.py index baa168aa751..77d626ce8d8 100644 --- a/PIL/ImageFilter.py +++ b/PIL/ImageFilter.py @@ -39,6 +39,7 @@ class Kernel(Filter): :param offset: Offset. If given, this value is added to the result, after it has been divided by the scale factor. """ + is_multiband = True def __init__(self, size, kernel, scale=None, offset=0): if scale is None: @@ -148,6 +149,7 @@ class GaussianBlur(Filter): :param radius: Blur radius. """ name = "GaussianBlur" + is_multiband = True def __init__(self, radius=2): self.radius = radius @@ -171,6 +173,7 @@ class UnsharpMask(Filter): """ name = "UnsharpMask" + is_multiband = True def __init__(self, radius=2, percent=150, threshold=3): self.radius = radius From b10b134b41f02ad5c3a2b115de99b83aeab6b5d7 Mon Sep 17 00:00:00 2001 From: Alexander Date: Sun, 13 Aug 2017 16:01:01 +0300 Subject: [PATCH 09/17] i2f fix --- libImaging/Filter.c | 209 +++++++++++++++++++++++++++++--------------- 1 file changed, 139 insertions(+), 70 deletions(-) diff --git a/libImaging/Filter.c b/libImaging/Filter.c index fd723275e06..5e51ab7718e 100644 --- a/libImaging/Filter.c +++ b/libImaging/Filter.c @@ -92,20 +92,35 @@ ImagingExpand(Imaging imIn, int xmargin, int ymargin, int mode) } +#ifdef __GNUC__ + #define GCC_VERSION (__GNUC__ * 10000 \ + + __GNUC_MINOR__ * 100 \ + + __GNUC_PATCHLEVEL__) +#endif + +/* This is work around bug in GCC prior 4.9 in 64 bit mode. + GCC generates code with partial dependency which 3 times slower. + See: http://stackoverflow.com/a/26588074/253146 */ +#if defined(__x86_64__) && defined(__SSE__) && ! defined(__NO_INLINE__) && \ + ! defined(__clang__) && defined(GCC_VERSION) && (GCC_VERSION < 40900) +static float __attribute__((always_inline)) i2f(int v) { + float x; + __asm__("xorps %0, %0; cvtsi2ss %1, %0" : "=X"(x) : "r"(v) ); + return x; +} +#else +static float inline i2f(int v) { return (float) v; } +#endif + + void ImagingFilter3x3(Imaging imOut, Imaging im, const float* kernel, float offset) { -#define KERNEL3x3(in_1, in0, in1, x, kernel, d) ( \ - (UINT8) in1[x-d] * kernel[0] + \ - (UINT8) in1[x] * kernel[1] + \ - (UINT8) in1[x+d] * kernel[2] + \ - (UINT8) in0[x-d] * kernel[3] + \ - (UINT8) in0[x] * kernel[4] + \ - (UINT8) in0[x+d] * kernel[5] + \ - (UINT8) in_1[x-d] * kernel[6] + \ - (UINT8) in_1[x] * kernel[7] + \ - (UINT8) in_1[x+d] * kernel[8]) +#define KERNEL1x3(in0, x, kernel, d) ( \ + i2f((UINT8) in0[x-d]) * (kernel)[0] + \ + i2f((UINT8) in0[x]) * (kernel)[1] + \ + i2f((UINT8) in0[x+d]) * (kernel)[2]) int x = 0, y = 0; @@ -119,8 +134,11 @@ ImagingFilter3x3(Imaging imOut, Imaging im, const float* kernel, out[0] = in0[0]; for (x = 1; x < im->xsize-1; x++) { - float ss = KERNEL3x3(in_1, in0, in1, x, kernel, 1); - out[x] = clip8(ss + offset); + float ss = offset; + ss += KERNEL1x3(in1, x, &kernel[0], 1); + ss += KERNEL1x3(in0, x, &kernel[3], 1); + ss += KERNEL1x3(in_1, x, &kernel[6], 1); + out[x] = clip8(ss); } out[x] = in0[x]; } @@ -134,29 +152,53 @@ ImagingFilter3x3(Imaging imOut, Imaging im, const float* kernel, out[0] = ((UINT32*) in0)[0]; if (im->bands == 2) { for (x = 1; x < im->xsize-1; x++) { - float ss0 = KERNEL3x3(in_1, in0, in1, x*4+0, kernel, 4); - float ss3 = KERNEL3x3(in_1, in0, in1, x*4+3, kernel, 4); - out[x] = MAKE_UINT32( - clip8(ss0 + offset), 0, 0, clip8(ss3 + offset)); + float ss0 = offset; + float ss3 = offset; + ss0 += KERNEL1x3(in1, x*4+0, &kernel[0], 4); + ss3 += KERNEL1x3(in1, x*4+3, &kernel[0], 4); + ss0 += KERNEL1x3(in0, x*4+0, &kernel[3], 4); + ss3 += KERNEL1x3(in0, x*4+3, &kernel[3], 4); + ss0 += KERNEL1x3(in_1, x*4+0, &kernel[6], 4); + ss3 += KERNEL1x3(in_1, x*4+3, &kernel[6], 4); + out[x] = MAKE_UINT32(clip8(ss0), 0, 0, clip8(ss3)); } } else if (im->bands == 3) { for (x = 1; x < im->xsize-1; x++) { - float ss0 = KERNEL3x3(in_1, in0, in1, x*4+0, kernel, 4); - float ss1 = KERNEL3x3(in_1, in0, in1, x*4+1, kernel, 4); - float ss2 = KERNEL3x3(in_1, in0, in1, x*4+2, kernel, 4); + float ss0 = offset; + float ss1 = offset; + float ss2 = offset; + ss0 += KERNEL1x3(in1, x*4+0, &kernel[0], 4); + ss1 += KERNEL1x3(in1, x*4+1, &kernel[0], 4); + ss2 += KERNEL1x3(in1, x*4+2, &kernel[0], 4); + ss0 += KERNEL1x3(in0, x*4+0, &kernel[3], 4); + ss1 += KERNEL1x3(in0, x*4+1, &kernel[3], 4); + ss2 += KERNEL1x3(in0, x*4+2, &kernel[3], 4); + ss0 += KERNEL1x3(in_1, x*4+0, &kernel[6], 4); + ss1 += KERNEL1x3(in_1, x*4+1, &kernel[6], 4); + ss2 += KERNEL1x3(in_1, x*4+2, &kernel[6], 4); out[x] = MAKE_UINT32( - clip8(ss0 + offset), clip8(ss1 + offset), - clip8(ss2 + offset), 0); + clip8(ss0), clip8(ss1), clip8(ss2), 0); } } else if (im->bands == 4) { for (x = 1; x < im->xsize-1; x++) { - float ss0 = KERNEL3x3(in_1, in0, in1, x*4+0, kernel, 4); - float ss1 = KERNEL3x3(in_1, in0, in1, x*4+1, kernel, 4); - float ss2 = KERNEL3x3(in_1, in0, in1, x*4+2, kernel, 4); - float ss3 = KERNEL3x3(in_1, in0, in1, x*4+3, kernel, 4); + float ss0 = offset; + float ss1 = offset; + float ss2 = offset; + float ss3 = offset; + ss0 += KERNEL1x3(in1, x*4+0, &kernel[0], 4); + ss1 += KERNEL1x3(in1, x*4+1, &kernel[0], 4); + ss2 += KERNEL1x3(in1, x*4+2, &kernel[0], 4); + ss3 += KERNEL1x3(in1, x*4+3, &kernel[0], 4); + ss0 += KERNEL1x3(in0, x*4+0, &kernel[3], 4); + ss1 += KERNEL1x3(in0, x*4+1, &kernel[3], 4); + ss2 += KERNEL1x3(in0, x*4+2, &kernel[3], 4); + ss3 += KERNEL1x3(in0, x*4+3, &kernel[3], 4); + ss0 += KERNEL1x3(in_1, x*4+0, &kernel[6], 4); + ss1 += KERNEL1x3(in_1, x*4+1, &kernel[6], 4); + ss2 += KERNEL1x3(in_1, x*4+2, &kernel[6], 4); + ss3 += KERNEL1x3(in_1, x*4+3, &kernel[6], 4); out[x] = MAKE_UINT32( - clip8(ss0 + offset), clip8(ss1 + offset), - clip8(ss2 + offset), clip8(ss3 + offset)); + clip8(ss0), clip8(ss1), clip8(ss2), clip8(ss3)); } } out[x] = ((UINT32*) in0)[x]; @@ -170,32 +212,12 @@ void ImagingFilter5x5(Imaging imOut, Imaging im, const float* kernel, float offset) { -#define KERNEL5x5(in_2, in_1, in0, in1, in2, x, kernel, d) ( \ - (UINT8) in2[x-d-d] * kernel[0] + \ - (UINT8) in2[x-d] * kernel[1] + \ - (UINT8) in2[x] * kernel[2] + \ - (UINT8) in2[x+d] * kernel[3] + \ - (UINT8) in2[x+d+d] * kernel[4] + \ - (UINT8) in1[x-d-d] * kernel[5] + \ - (UINT8) in1[x-d] * kernel[6] + \ - (UINT8) in1[x] * kernel[7] + \ - (UINT8) in1[x+d] * kernel[8] + \ - (UINT8) in1[x+d+d] * kernel[9] + \ - (UINT8) in0[x-d-d] * kernel[10] + \ - (UINT8) in0[x-d] * kernel[11] + \ - (UINT8) in0[x] * kernel[12] + \ - (UINT8) in0[x+d] * kernel[13] + \ - (UINT8) in0[x+d+d] * kernel[14] + \ - (UINT8) in_1[x-d-d] * kernel[15] + \ - (UINT8) in_1[x-d] * kernel[16] + \ - (UINT8) in_1[x] * kernel[17] + \ - (UINT8) in_1[x+d] * kernel[18] + \ - (UINT8) in_1[x+d+d] * kernel[19] + \ - (UINT8) in_2[x-d-d] * kernel[20] + \ - (UINT8) in_2[x-d] * kernel[21] + \ - (UINT8) in_2[x] * kernel[22] + \ - (UINT8) in_2[x+d] * kernel[23] + \ - (UINT8) in_2[x+d+d] * kernel[24]) +#define KERNEL1x5(in0, x, kernel, d) ( \ + i2f((UINT8) in0[x-d-d]) * (kernel)[0] + \ + i2f((UINT8) in0[x-d]) * (kernel)[1] + \ + i2f((UINT8) in0[x]) * (kernel)[2] + \ + i2f((UINT8) in0[x+d]) * (kernel)[3] + \ + i2f((UINT8) in0[x+d+d]) * (kernel)[4]) int x = 0, y = 0; @@ -213,8 +235,13 @@ ImagingFilter5x5(Imaging imOut, Imaging im, const float* kernel, out[0] = in0[0]; out[1] = in0[1]; for (x = 2; x < im->xsize-2; x++) { - float ss = KERNEL5x5(in_2, in_1, in0, in1, in2, x, kernel, 1); - out[x] = clip8(ss + offset); + float ss = offset; + ss += KERNEL1x5(in2, x, &kernel[0], 1); + ss += KERNEL1x5(in1, x, &kernel[5], 1); + ss += KERNEL1x5(in0, x, &kernel[10], 1); + ss += KERNEL1x5(in_1, x, &kernel[15], 1); + ss += KERNEL1x5(in_2, x, &kernel[20], 1); + out[x] = clip8(ss); } out[x+0] = in0[x+0]; out[x+1] = in0[x+1]; @@ -232,29 +259,71 @@ ImagingFilter5x5(Imaging imOut, Imaging im, const float* kernel, out[1] = ((UINT32*) in0)[1]; if (im->bands == 2) { for (x = 2; x < im->xsize-2; x++) { - float ss0 = KERNEL5x5(in_2, in_1, in0, in1, in2, x*4+0, kernel, 4); - float ss3 = KERNEL5x5(in_2, in_1, in0, in1, in2, x*4+3, kernel, 4); - out[x] = MAKE_UINT32( - clip8(ss0 + offset), 0, 0, clip8(ss3 + offset)); + float ss0 = offset; + float ss3 = offset; + ss0 += KERNEL1x5(in2, x*4+0, &kernel[0], 4); + ss3 += KERNEL1x5(in2, x*4+3, &kernel[0], 4); + ss0 += KERNEL1x5(in1, x*4+0, &kernel[5], 4); + ss3 += KERNEL1x5(in1, x*4+3, &kernel[5], 4); + ss0 += KERNEL1x5(in0, x*4+0, &kernel[10], 4); + ss3 += KERNEL1x5(in0, x*4+3, &kernel[10], 4); + ss0 += KERNEL1x5(in_1, x*4+0, &kernel[15], 4); + ss3 += KERNEL1x5(in_1, x*4+3, &kernel[15], 4); + ss0 += KERNEL1x5(in_2, x*4+0, &kernel[20], 4); + ss3 += KERNEL1x5(in_2, x*4+3, &kernel[20], 4); + out[x] = MAKE_UINT32(clip8(ss0), 0, 0, clip8(ss3)); } } else if (im->bands == 3) { for (x = 2; x < im->xsize-2; x++) { - float ss0 = KERNEL5x5(in_2, in_1, in0, in1, in2, x*4+0, kernel, 4); - float ss1 = KERNEL5x5(in_2, in_1, in0, in1, in2, x*4+1, kernel, 4); - float ss2 = KERNEL5x5(in_2, in_1, in0, in1, in2, x*4+2, kernel, 4); + float ss0 = offset; + float ss1 = offset; + float ss2 = offset; + ss0 += KERNEL1x5(in2, x*4+0, &kernel[0], 4); + ss1 += KERNEL1x5(in2, x*4+1, &kernel[0], 4); + ss2 += KERNEL1x5(in2, x*4+2, &kernel[0], 4); + ss0 += KERNEL1x5(in1, x*4+0, &kernel[5], 4); + ss1 += KERNEL1x5(in1, x*4+1, &kernel[5], 4); + ss2 += KERNEL1x5(in1, x*4+2, &kernel[5], 4); + ss0 += KERNEL1x5(in0, x*4+0, &kernel[10], 4); + ss1 += KERNEL1x5(in0, x*4+1, &kernel[10], 4); + ss2 += KERNEL1x5(in0, x*4+2, &kernel[10], 4); + ss0 += KERNEL1x5(in_1, x*4+0, &kernel[15], 4); + ss1 += KERNEL1x5(in_1, x*4+1, &kernel[15], 4); + ss2 += KERNEL1x5(in_1, x*4+2, &kernel[15], 4); + ss0 += KERNEL1x5(in_2, x*4+0, &kernel[20], 4); + ss1 += KERNEL1x5(in_2, x*4+1, &kernel[20], 4); + ss2 += KERNEL1x5(in_2, x*4+2, &kernel[20], 4); out[x] = MAKE_UINT32( - clip8(ss0 + offset), clip8(ss1 + offset), - clip8(ss2 + offset), 0); + clip8(ss0), clip8(ss1), clip8(ss2), 0); } } else if (im->bands == 4) { for (x = 2; x < im->xsize-2; x++) { - float ss0 = KERNEL5x5(in_2, in_1, in0, in1, in2, x*4+0, kernel, 4); - float ss1 = KERNEL5x5(in_2, in_1, in0, in1, in2, x*4+1, kernel, 4); - float ss2 = KERNEL5x5(in_2, in_1, in0, in1, in2, x*4+2, kernel, 4); - float ss3 = KERNEL5x5(in_2, in_1, in0, in1, in2, x*4+3, kernel, 4); + float ss0 = offset; + float ss1 = offset; + float ss2 = offset; + float ss3 = offset; + ss0 += KERNEL1x5(in2, x*4+0, &kernel[0], 4); + ss1 += KERNEL1x5(in2, x*4+1, &kernel[0], 4); + ss2 += KERNEL1x5(in2, x*4+2, &kernel[0], 4); + ss3 += KERNEL1x5(in2, x*4+3, &kernel[0], 4); + ss0 += KERNEL1x5(in1, x*4+0, &kernel[5], 4); + ss1 += KERNEL1x5(in1, x*4+1, &kernel[5], 4); + ss2 += KERNEL1x5(in1, x*4+2, &kernel[5], 4); + ss3 += KERNEL1x5(in1, x*4+3, &kernel[5], 4); + ss0 += KERNEL1x5(in0, x*4+0, &kernel[10], 4); + ss1 += KERNEL1x5(in0, x*4+1, &kernel[10], 4); + ss2 += KERNEL1x5(in0, x*4+2, &kernel[10], 4); + ss3 += KERNEL1x5(in0, x*4+3, &kernel[10], 4); + ss0 += KERNEL1x5(in_1, x*4+0, &kernel[15], 4); + ss1 += KERNEL1x5(in_1, x*4+1, &kernel[15], 4); + ss2 += KERNEL1x5(in_1, x*4+2, &kernel[15], 4); + ss3 += KERNEL1x5(in_1, x*4+3, &kernel[15], 4); + ss0 += KERNEL1x5(in_2, x*4+0, &kernel[20], 4); + ss1 += KERNEL1x5(in_2, x*4+1, &kernel[20], 4); + ss2 += KERNEL1x5(in_2, x*4+2, &kernel[20], 4); + ss3 += KERNEL1x5(in_2, x*4+3, &kernel[20], 4); out[x] = MAKE_UINT32( - clip8(ss0 + offset), clip8(ss1 + offset), - clip8(ss2 + offset), clip8(ss3 + offset)); + clip8(ss0), clip8(ss1), clip8(ss2), clip8(ss3)); } } out[x] = ((UINT32*) in0)[x]; From 5dbc71b69b9ad8827a72ab125a238a0c915d85b6 Mon Sep 17 00:00:00 2001 From: Alexander Date: Sun, 13 Aug 2017 16:27:54 +0300 Subject: [PATCH 10/17] already defined --- libImaging/Filter.c | 6 ------ 1 file changed, 6 deletions(-) diff --git a/libImaging/Filter.c b/libImaging/Filter.c index 5e51ab7718e..604a00c4907 100644 --- a/libImaging/Filter.c +++ b/libImaging/Filter.c @@ -92,12 +92,6 @@ ImagingExpand(Imaging imIn, int xmargin, int ymargin, int mode) } -#ifdef __GNUC__ - #define GCC_VERSION (__GNUC__ * 10000 \ - + __GNUC_MINOR__ * 100 \ - + __GNUC_PATCHLEVEL__) -#endif - /* This is work around bug in GCC prior 4.9 in 64 bit mode. GCC generates code with partial dependency which 3 times slower. See: http://stackoverflow.com/a/26588074/253146 */ From 3949fc812a7d516137ac211e928d1d7f61e1fc09 Mon Sep 17 00:00:00 2001 From: Alexander Date: Sun, 13 Aug 2017 16:33:36 +0300 Subject: [PATCH 11/17] avoid warning: always_inline function might not be inlinable --- libImaging/Filter.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libImaging/Filter.c b/libImaging/Filter.c index 604a00c4907..eb3293a2383 100644 --- a/libImaging/Filter.c +++ b/libImaging/Filter.c @@ -97,7 +97,7 @@ ImagingExpand(Imaging imIn, int xmargin, int ymargin, int mode) See: http://stackoverflow.com/a/26588074/253146 */ #if defined(__x86_64__) && defined(__SSE__) && ! defined(__NO_INLINE__) && \ ! defined(__clang__) && defined(GCC_VERSION) && (GCC_VERSION < 40900) -static float __attribute__((always_inline)) i2f(int v) { +static float __attribute__((always_inline)) inline i2f(int v) { float x; __asm__("xorps %0, %0; cvtsi2ss %1, %0" : "=X"(x) : "r"(v) ); return x; From c5865f9abef983a8a3c0713f5900136ed13ea66d Mon Sep 17 00:00:00 2001 From: Alexander Date: Sun, 13 Aug 2017 17:39:22 +0300 Subject: [PATCH 12/17] it's implementation detail (SSE rounds automatically) --- libImaging/Filter.c | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/libImaging/Filter.c b/libImaging/Filter.c index eb3293a2383..2798e13fbde 100644 --- a/libImaging/Filter.c +++ b/libImaging/Filter.c @@ -120,6 +120,8 @@ ImagingFilter3x3(Imaging imOut, Imaging im, const float* kernel, memcpy(imOut->image[0], im->image[0], im->linesize); if (im->bands == 1) { + // Add one time for rounding + offset += 0.5; for (y = 1; y < im->ysize-1; y++) { UINT8* in_1 = (UINT8*) im->image[y-1]; UINT8* in0 = (UINT8*) im->image[y]; @@ -137,6 +139,8 @@ ImagingFilter3x3(Imaging imOut, Imaging im, const float* kernel, out[x] = in0[x]; } } else { + // Add one time for rounding + offset += 0.5; for (y = 1; y < im->ysize-1; y++) { UINT8* in_1 = (UINT8*) im->image[y-1]; UINT8* in0 = (UINT8*) im->image[y]; @@ -218,6 +222,8 @@ ImagingFilter5x5(Imaging imOut, Imaging im, const float* kernel, memcpy(imOut->image[0], im->image[0], im->linesize); memcpy(imOut->image[1], im->image[1], im->linesize); if (im->bands == 1) { + // Add one time for rounding + offset += 0.5; for (y = 2; y < im->ysize-2; y++) { UINT8* in_2 = (UINT8*) im->image[y-2]; UINT8* in_1 = (UINT8*) im->image[y-1]; @@ -241,6 +247,8 @@ ImagingFilter5x5(Imaging imOut, Imaging im, const float* kernel, out[x+1] = in0[x+1]; } } else { + // Add one time for rounding + offset += 0.5; for (y = 2; y < im->ysize-2; y++) { UINT8* in_2 = (UINT8*) im->image[y-2]; UINT8* in_1 = (UINT8*) im->image[y-1]; @@ -348,9 +356,6 @@ ImagingFilter(Imaging im, int xsize, int ysize, const FLOAT32* kernel, if (!imOut) return NULL; - // Add one time for rounding - offset += 0.5; - ImagingSectionEnter(&cookie); if (xsize == 3) { /* 3x3 kernel. */ From b31c74a9cc5e4160e1982d797755478776d98aeb Mon Sep 17 00:00:00 2001 From: Alexander Date: Tue, 15 Aug 2017 07:45:03 +0300 Subject: [PATCH 13/17] Tests for all bands and combinations --- Tests/test_image_filter.py | 50 ++++++++++++++++++++++++-------------- 1 file changed, 32 insertions(+), 18 deletions(-) diff --git a/Tests/test_image_filter.py b/Tests/test_image_filter.py index c8a397e7ea5..88aae419e66 100644 --- a/Tests/test_image_filter.py +++ b/Tests/test_image_filter.py @@ -95,26 +95,40 @@ def test_rankfilter_properties(self): self.assertEqual(rankfilter.rank, 2) def test_consistency_3x3(self): - im = Image.open("Tests/images/hopper.bmp") - emboss = im.filter(ImageFilter.Kernel((3, 3), - (-1, -1, 0, - -1, 0, 1, - 0, 1, 1), .3)) - - self.assert_image_equal( - emboss, Image.open("Tests/images/hopper_emboss.bmp")) + source = Image.open("Tests/images/hopper.bmp") + reference = Image.open("Tests/images/hopper_emboss.bmp") + kernel = ImageFilter.Kernel((3, 3), + (-1, -1, 0, + -1, 0, 1, + 0, 1, 1), .3) + source = source.split() * 2 + reference = reference.split() * 2 + + for mode in ['L', 'LA', 'RGB', 'CMYK']: + print mode + self.assert_image_equal( + Image.merge(mode, source[:len(mode)]).filter(kernel), + Image.merge(mode, reference[:len(mode)]), + ) def test_consistency_5x5(self): - im = Image.open("Tests/images/hopper.bmp") - emboss = im.filter(ImageFilter.Kernel((5, 5), - (-1, -1, -1, -1, 0, - -1, -1, -1, 0, 1, - -1, -1, 0, 1, 1, - -1, 0, 1, 1, 1, - 0, 1, 1, 1, 1), 0.3)) - - self.assert_image_equal( - emboss, Image.open("Tests/images/hopper_emboss_more.bmp")) + source = Image.open("Tests/images/hopper.bmp") + reference = Image.open("Tests/images/hopper_emboss_more.bmp") + kernel = ImageFilter.Kernel((5, 5), + (-1, -1, -1, -1, 0, + -1, -1, -1, 0, 1, + -1, -1, 0, 1, 1, + -1, 0, 1, 1, 1, + 0, 1, 1, 1, 1), 0.3) + source = source.split() * 2 + reference = reference.split() * 2 + + for mode in ['L', 'LA', 'RGB', 'CMYK']: + print mode + self.assert_image_equal( + Image.merge(mode, source[:len(mode)]).filter(kernel), + Image.merge(mode, reference[:len(mode)]), + ) if __name__ == '__main__': From b8e104e743e33a8fe9ecfe5c170e02747006e7de Mon Sep 17 00:00:00 2001 From: Alexander Date: Tue, 15 Aug 2017 07:52:02 +0300 Subject: [PATCH 14/17] oops --- Tests/test_image_filter.py | 1 - 1 file changed, 1 deletion(-) diff --git a/Tests/test_image_filter.py b/Tests/test_image_filter.py index 88aae419e66..0352d47af6d 100644 --- a/Tests/test_image_filter.py +++ b/Tests/test_image_filter.py @@ -124,7 +124,6 @@ def test_consistency_5x5(self): reference = reference.split() * 2 for mode in ['L', 'LA', 'RGB', 'CMYK']: - print mode self.assert_image_equal( Image.merge(mode, source[:len(mode)]).filter(kernel), Image.merge(mode, reference[:len(mode)]), From e842919f37e166a3d95eae026facb2ac67d1a47a Mon Sep 17 00:00:00 2001 From: Alexander Date: Tue, 15 Aug 2017 07:53:31 +0300 Subject: [PATCH 15/17] oops 2 --- Tests/test_image_filter.py | 1 - 1 file changed, 1 deletion(-) diff --git a/Tests/test_image_filter.py b/Tests/test_image_filter.py index 0352d47af6d..d235fee5aa4 100644 --- a/Tests/test_image_filter.py +++ b/Tests/test_image_filter.py @@ -105,7 +105,6 @@ def test_consistency_3x3(self): reference = reference.split() * 2 for mode in ['L', 'LA', 'RGB', 'CMYK']: - print mode self.assert_image_equal( Image.merge(mode, source[:len(mode)]).filter(kernel), Image.merge(mode, reference[:len(mode)]), From a941747f140e93b8db41bed0f809ff051662525b Mon Sep 17 00:00:00 2001 From: Alexander Date: Thu, 17 Aug 2017 02:56:10 +0300 Subject: [PATCH 16/17] use ImagingNewDirty --- libImaging/Filter.c | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/libImaging/Filter.c b/libImaging/Filter.c index 2798e13fbde..aab9cdce7a7 100644 --- a/libImaging/Filter.c +++ b/libImaging/Filter.c @@ -53,9 +53,8 @@ ImagingExpand(Imaging imIn, int xmargin, int ymargin, int mode) if (xmargin < 0 && ymargin < 0) return (Imaging) ImagingError_ValueError("bad kernel size"); - imOut = ImagingNew( - imIn->mode, imIn->xsize+2*xmargin, imIn->ysize+2*ymargin - ); + imOut = ImagingNewDirty( + imIn->mode, imIn->xsize+2*xmargin, imIn->ysize+2*ymargin); if (!imOut) return NULL; @@ -352,7 +351,7 @@ ImagingFilter(Imaging im, int xsize, int ysize, const FLOAT32* kernel, if ((xsize != 3 && xsize != 5) || xsize != ysize) return (Imaging) ImagingError_ValueError("bad kernel size"); - imOut = ImagingNew(im->mode, im->xsize, im->ysize); + imOut = ImagingNewDirty(im->mode, im->xsize, im->ysize); if (!imOut) return NULL; From 98ee46827c00c375d45bcee0aa35cf4d0fc49e4e Mon Sep 17 00:00:00 2001 From: Alexander Date: Sun, 10 Sep 2017 12:59:51 +0300 Subject: [PATCH 17/17] MultibandFilter --- PIL/Image.py | 7 ++++--- PIL/ImageFilter.py | 13 +++++++------ 2 files changed, 11 insertions(+), 9 deletions(-) diff --git a/PIL/Image.py b/PIL/Image.py index d3a21a898a4..58fb7c508c0 100644 --- a/PIL/Image.py +++ b/PIL/Image.py @@ -1105,6 +1105,8 @@ def filter(self, filter): :param filter: Filter kernel. :returns: An :py:class:`~PIL.Image.Image` object. """ + from . import ImageFilter + self.load() if isinstance(filter, collections.Callable): @@ -1113,11 +1115,10 @@ def filter(self, filter): raise TypeError("filter argument should be ImageFilter.Filter " + "instance or class") - multiband = getattr(filter, 'is_multiband', False) - + multiband = isinstance(filter, ImageFilter.MultibandFilter) if self.im.bands == 1 or multiband: return self._new(filter.filter(self.im)) - # fix to handle multiband images since _imaging doesn't + ims = [] for c in range(self.im.bands): ims.append(self._new(filter.filter(self.im.getband(c)))) diff --git a/PIL/ImageFilter.py b/PIL/ImageFilter.py index 77d626ce8d8..c89225484b0 100644 --- a/PIL/ImageFilter.py +++ b/PIL/ImageFilter.py @@ -22,7 +22,11 @@ class Filter(object): pass -class Kernel(Filter): +class MultibandFilter(Filter): + pass + + +class Kernel(MultibandFilter): """ Create a convolution kernel. The current version only supports 3x3 and 5x5 integer and floating point kernels. @@ -39,7 +43,6 @@ class Kernel(Filter): :param offset: Offset. If given, this value is added to the result, after it has been divided by the scale factor. """ - is_multiband = True def __init__(self, size, kernel, scale=None, offset=0): if scale is None: @@ -143,13 +146,12 @@ def filter(self, image): return image.modefilter(self.size) -class GaussianBlur(Filter): +class GaussianBlur(MultibandFilter): """Gaussian blur filter. :param radius: Blur radius. """ name = "GaussianBlur" - is_multiband = True def __init__(self, radius=2): self.radius = radius @@ -158,7 +160,7 @@ def filter(self, image): return image.gaussian_blur(self.radius) -class UnsharpMask(Filter): +class UnsharpMask(MultibandFilter): """Unsharp mask filter. See Wikipedia's entry on `digital unsharp masking`_ for an explanation of @@ -173,7 +175,6 @@ class UnsharpMask(Filter): """ name = "UnsharpMask" - is_multiband = True def __init__(self, radius=2, percent=150, threshold=3): self.radius = radius