Pillow/libImaging/UnsharpMask.c

97 lines
2.9 KiB
C
Raw Normal View History

2010-07-31 06:52:47 +04:00
/* PILusm, a gaussian blur and unsharp masking library for PIL
By Kevin Cazabon, copyright 2003
kevin_cazabon@hotmail.com
kevin@cazabon.com */
/* Originally released under LGPL. Graciously donated to PIL
for distribution under the standard PIL license in 2009." */
#include "Python.h"
#include "Imaging.h"
typedef UINT8 pixel[4];
static inline UINT8 clip8(int in)
{
if (in >= 255)
return 255;
if (in <= 0)
return 0;
return (UINT8) in;
}
2010-07-31 06:52:47 +04:00
Imaging
ImagingUnsharpMask(Imaging imOut, Imaging imIn, float radius, int percent,
2014-10-06 20:55:59 +04:00
int threshold)
2010-07-31 06:52:47 +04:00
{
ImagingSectionCookie cookie;
Imaging result;
int x, y, diff;
2010-07-31 06:52:47 +04:00
pixel *lineIn = NULL;
pixel *lineOut = NULL;
2010-07-31 06:52:47 +04:00
UINT8 *lineIn8 = NULL;
UINT8 *lineOut8 = NULL;
/* First, do a gaussian blur on the image, putting results in imOut
temporarily. All format checks are in gaussian blur. */
result = ImagingGaussianBlur(imOut, imIn, radius, 3);
2010-07-31 06:52:47 +04:00
if (!result)
2014-10-06 20:55:59 +04:00
return NULL;
2010-07-31 06:52:47 +04:00
/* Now, go through each pixel, compare "normal" pixel to blurred
pixel. If the difference is more than threshold values, apply
2010-07-31 06:52:47 +04:00
the OPPOSITE correction to the amount of blur, multiplied by
percent. */
ImagingSectionEnter(&cookie);
for (y = 0; y < imIn->ysize; y++) {
if (imIn->image8)
{
lineIn8 = imIn->image8[y];
2014-10-06 20:55:59 +04:00
lineOut8 = imOut->image8[y];
for (x = 0; x < imIn->xsize; x++) {
/* compare in/out pixels, apply sharpening */
diff = lineIn8[x] - lineOut8[x];
2014-10-06 20:55:59 +04:00
if (abs(diff) > threshold) {
/* add the diff*percent to the original pixel */
lineOut8[x] = clip8(lineIn8[x] + diff * percent / 100);
2014-10-06 20:55:59 +04:00
} else {
/* new pixel is the same as imIn */
lineOut8[x] = lineIn8[x];
2014-10-06 20:55:59 +04:00
}
}
} else {
lineIn = (pixel *)imIn->image32[y];
lineOut = (pixel *)imOut->image32[y];
for (x = 0; x < imIn->xsize; x++) {
/* compare in/out pixels, apply sharpening */
diff = lineIn[x][0] - lineOut[x][0];
lineOut[x][0] = abs(diff) > threshold ?
clip8(lineIn[x][0] + diff * percent / 100) : lineIn[x][0];
diff = lineIn[x][1] - lineOut[x][1];
lineOut[x][1] = abs(diff) > threshold ?
clip8(lineIn[x][1] + diff * percent / 100) : lineIn[x][1];
diff = lineIn[x][2] - lineOut[x][2];
lineOut[x][2] = abs(diff) > threshold ?
clip8(lineIn[x][2] + diff * percent / 100) : lineIn[x][2];
diff = lineIn[x][3] - lineOut[x][3];
lineOut[x][3] = abs(diff) > threshold ?
clip8(lineIn[x][3] + diff * percent / 100) : lineIn[x][3];
2014-10-06 20:55:59 +04:00
}
}
2010-07-31 06:52:47 +04:00
}
ImagingSectionLeave(&cookie);
return imOut;
}