Pillow/libImaging/Filter.c

246 lines
8.0 KiB
C
Raw Normal View History

2010-07-31 06:52:47 +04:00
/*
* The Python Imaging Library
* $Id$
*
* apply convolution kernel to image
*
* history:
* 1995-11-26 fl Created, supports 3x3 kernels
* 1995-11-27 fl Added 5x5 kernels, copy border
* 1999-07-26 fl Eliminated a few compiler warnings
* 2002-06-09 fl Moved kernel definitions to Python
* 2002-06-11 fl Support floating point kernels
* 2003-09-15 fl Added ImagingExpand helper
*
* Copyright (c) Secret Labs AB 1997-2002. All rights reserved.
* Copyright (c) Fredrik Lundh 1995.
*
* See the README file for information on usage and redistribution.
*/
/*
* FIXME: Support RGB and RGBA/CMYK modes as well
* FIXME: Expand image border (current version leaves border as is)
* FIXME: Implement image processing gradient filters
*/
#include "Imaging.h"
2017-08-12 22:12:59 +03:00
2017-08-13 00:15:57 +03:00
#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
2017-08-12 22:12:59 +03:00
static inline UINT8 clip8(float in)
{
if (in <= 0.0)
return 0;
if (in >= 255.0)
return 255;
return (UINT8) in;
}
2010-07-31 06:52:47 +04:00
Imaging
ImagingExpand(Imaging imIn, int xmargin, int ymargin, int mode)
{
Imaging imOut;
int x, y;
2017-08-11 15:17:47 +03:00
ImagingSectionCookie cookie;
2010-07-31 06:52:47 +04:00
if (xmargin < 0 && ymargin < 0)
2017-08-07 00:20:35 +03:00
return (Imaging) ImagingError_ValueError("bad kernel size");
2010-07-31 06:52:47 +04:00
imOut = ImagingNew(
imIn->mode, imIn->xsize+2*xmargin, imIn->ysize+2*ymargin
);
if (!imOut)
2017-08-07 00:20:35 +03:00
return NULL;
2010-07-31 06:52:47 +04:00
#define EXPAND_LINE(type, image, yin, yout) {\
for (x = 0; x < xmargin; x++)\
imOut->image[yout][x] = imIn->image[yin][0];\
for (x = 0; x < imIn->xsize; x++)\
imOut->image[yout][x+xmargin] = imIn->image[yin][x];\
for (x = 0; x < xmargin; x++)\
imOut->image[yout][xmargin+imIn->xsize+x] =\
imIn->image[yin][imIn->xsize-1];\
}
#define EXPAND(type, image) {\
for (y = 0; y < ymargin; y++)\
EXPAND_LINE(type, image, 0, y);\
for (y = 0; y < imIn->ysize; y++)\
EXPAND_LINE(type, image, y, y+ymargin);\
for (y = 0; y < ymargin; y++)\
EXPAND_LINE(type, image, imIn->ysize-1, ymargin+imIn->ysize+y);\
}
2017-08-11 15:17:47 +03:00
ImagingSectionEnter(&cookie);
2010-07-31 06:52:47 +04:00
if (imIn->image8) {
2017-08-07 00:20:35 +03:00
EXPAND(UINT8, image8);
2010-07-31 06:52:47 +04:00
} else {
2017-08-07 00:20:35 +03:00
EXPAND(INT32, image32);
2010-07-31 06:52:47 +04:00
}
2017-08-11 15:17:47 +03:00
ImagingSectionLeave(&cookie);
2010-07-31 06:52:47 +04:00
ImagingCopyInfo(imOut, imIn);
return imOut;
}
2017-08-12 23:07:08 +03:00
void
ImagingFilter3x3(Imaging imOut, Imaging im, const float* kernel,
float offset)
{
2017-08-13 00:15:57 +03:00
#define KERNEL3x3(in_1, in, in1, x, kernel, d) ( \
2017-08-12 23:07:08 +03:00
(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])
2017-08-13 00:15:57 +03:00
int x = 0, y = 0;
2017-08-12 23:07:08 +03:00
memcpy(imOut->image[0], im->image[0], im->linesize);
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++) {
2017-08-13 00:15:57 +03:00
float ss = KERNEL3x3(in_1, in, in1, x, kernel, 1);
out[x] = clip8(ss + offset);
}
out[x] = in0[x];
}
2017-08-13 00:15:57 +03:00
} else {
for (y = 1; y < im->ysize-1; y++) {
2017-08-13 00:15:57 +03:00
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];
}
2017-08-12 23:07:08 +03:00
}
memcpy(imOut->image[y], im->image[y], im->linesize);
}
2010-07-31 06:52:47 +04:00
Imaging
ImagingFilter(Imaging im, int xsize, int ysize, const FLOAT32* kernel,
2017-08-07 01:11:05 +03:00
FLOAT32 offset)
2010-07-31 06:52:47 +04:00
{
Imaging imOut;
int x, y = 0;
2017-08-11 15:17:47 +03:00
ImagingSectionCookie cookie;
2010-07-31 06:52:47 +04:00
if ( ! im || im->type != IMAGING_TYPE_UINT8)
2017-08-07 00:20:35 +03:00
return (Imaging) ImagingError_ModeError();
2010-07-31 06:52:47 +04:00
if (im->xsize < xsize || im->ysize < ysize)
return ImagingCopy(im);
if ((xsize != 3 && xsize != 5) || xsize != ysize)
2017-08-07 00:20:35 +03:00
return (Imaging) ImagingError_ValueError("bad kernel size");
2010-07-31 06:52:47 +04:00
imOut = ImagingNew(im->mode, im->xsize, im->ysize);
if (!imOut)
2017-08-07 00:20:35 +03:00
return NULL;
2010-07-31 06:52:47 +04:00
2017-08-12 23:22:59 +03:00
// Add one time for rounding
offset += 0.5;
2017-08-07 00:20:35 +03:00
#define KERNEL5x5(image, kernel, d) ( \
2010-07-31 06:52:47 +04:00
(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])
2017-08-11 15:17:47 +03:00
ImagingSectionEnter(&cookie);
2010-07-31 06:52:47 +04:00
if (xsize == 3) {
2017-08-07 00:20:35 +03:00
/* 3x3 kernel. */
2017-08-12 23:07:08 +03:00
ImagingFilter3x3(imOut, im, kernel, offset);
2010-07-31 06:52:47 +04:00
} else {
2017-08-07 00:20:35 +03:00
/* 5x5 kernel. */
2017-08-12 22:18:24 +03:00
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];
2017-08-07 00:20:35 +03:00
}
}
2017-08-12 22:18:24 +03:00
memcpy(imOut->image[y], im->image[y], im->linesize);
memcpy(imOut->image[y+1], im->image[y+1], im->linesize);
2010-07-31 06:52:47 +04:00
}
2017-08-11 15:17:47 +03:00
ImagingSectionLeave(&cookie);
2010-07-31 06:52:47 +04:00
return imOut;
}