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"
|
|
|
|
|
|
|
|
|
2014-11-22 01:00:04 +03:00
|
|
|
typedef UINT8 pixel[4];
|
|
|
|
|
|
|
|
|
|
|
|
static inline UINT8 clip8(int in)
|
2014-10-12 15:13:37 +04:00
|
|
|
{
|
2014-11-22 01:00:04 +03:00
|
|
|
if (in >= 255)
|
|
|
|
return 255;
|
|
|
|
if (in <= 0)
|
|
|
|
return 0;
|
|
|
|
return (UINT8) in;
|
2014-10-12 15:13:37 +04:00
|
|
|
}
|
|
|
|
|
2014-10-25 16:36:41 +04:00
|
|
|
|
2010-07-31 06:52:47 +04:00
|
|
|
Imaging
|
2014-10-25 17:16:14 +04:00
|
|
|
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;
|
|
|
|
|
2014-11-22 01:00:04 +03:00
|
|
|
int x, y, diff;
|
2010-07-31 06:52:47 +04:00
|
|
|
|
2014-11-22 01:00:04 +03:00
|
|
|
pixel *lineIn = NULL;
|
|
|
|
pixel *lineOut = NULL;
|
2010-07-31 06:52:47 +04:00
|
|
|
UINT8 *lineIn8 = NULL;
|
|
|
|
UINT8 *lineOut8 = NULL;
|
|
|
|
|
2014-11-22 01:00:04 +03:00
|
|
|
/* First, do a gaussian blur on the image, putting results in imOut
|
|
|
|
temporarily. All format checks are in gaussian blur. */
|
2014-10-25 17:16:14 +04:00
|
|
|
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
|
|
|
|
2014-11-22 01:00:04 +03: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);
|
|
|
|
|
2014-10-25 17:16:14 +04:00
|
|
|
for (y = 0; y < imIn->ysize; y++) {
|
2014-11-22 01:00:04 +03:00
|
|
|
if (imIn->image8)
|
|
|
|
{
|
2014-10-25 17:16:14 +04:00
|
|
|
lineIn8 = imIn->image8[y];
|
2014-10-06 20:55:59 +04:00
|
|
|
lineOut8 = imOut->image8[y];
|
2014-11-22 01:00:04 +03:00
|
|
|
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 */
|
2014-11-22 01:00:04 +03:00
|
|
|
lineOut8[x] = clip8(lineIn8[x] + diff * percent / 100);
|
2014-10-06 20:55:59 +04:00
|
|
|
} else {
|
2014-11-22 01:00:04 +03:00
|
|
|
/* new pixel is the same as imIn */
|
|
|
|
lineOut8[x] = lineIn8[x];
|
2014-10-06 20:55:59 +04:00
|
|
|
}
|
|
|
|
}
|
2014-11-22 01:00:04 +03: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;
|
|
|
|
}
|