From b11bba108e6a2e74a87c27c3592d40fe0eac8cf5 Mon Sep 17 00:00:00 2001 From: Alexander Date: Sun, 13 Aug 2017 00:03:50 +0300 Subject: [PATCH] accept multiband images in filter (noop) --- PIL/Image.py | 4 +++- libImaging/Filter.c | 51 ++++++++++++++++++++++++++------------------- 2 files changed, 32 insertions(+), 23 deletions(-) diff --git a/PIL/Image.py b/PIL/Image.py index 49c9f6ab2..6a28f7cda 100644 --- a/PIL/Image.py +++ b/PIL/Image.py @@ -1112,7 +1112,9 @@ class Image(object): 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 866573588..43e25c196 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]; + 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++) { - float sum = KERNEL3x3(in_1, in, in1, kernel, 1) + offset; - out[x] = clip8(sum); - } - out[x] = in0[x]; + 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);