Pillow
98 строк · 3.1 Кб
1/* PILusm, a gaussian blur and unsharp masking library for PIL
2By Kevin Cazabon, copyright 2003
3kevin_cazabon@hotmail.com
4kevin@cazabon.com */
5
6/* Originally released under LGPL. Graciously donated to PIL
7for distribution under the standard PIL license in 2009." */
8
9#include "Imaging.h"10
11typedef UINT8 pixel[4];12
13static inline UINT814clip8(int in) {15if (in >= 255) {16return 255;17}18if (in <= 0) {19return 0;20}21return (UINT8)in;22}
23
24Imaging
25ImagingUnsharpMask(26Imaging imOut, Imaging imIn, float radius, int percent, int threshold27) {28ImagingSectionCookie cookie;29Imaging result;30
31int x, y, diff;32
33pixel *lineIn = NULL;34pixel *lineOut = NULL;35UINT8 *lineIn8 = NULL;36UINT8 *lineOut8 = NULL;37
38/* First, do a gaussian blur on the image, putting results in imOut39temporarily. All format checks are in gaussian blur. */
40result = ImagingGaussianBlur(imOut, imIn, radius, radius, 3);41if (!result) {42return NULL;43}44
45/* Now, go through each pixel, compare "normal" pixel to blurred46pixel. If the difference is more than threshold values, apply
47the OPPOSITE correction to the amount of blur, multiplied by
48percent. */
49
50ImagingSectionEnter(&cookie);51
52for (y = 0; y < imIn->ysize; y++) {53if (imIn->image8) {54lineIn8 = imIn->image8[y];55lineOut8 = imOut->image8[y];56for (x = 0; x < imIn->xsize; x++) {57/* compare in/out pixels, apply sharpening */58diff = lineIn8[x] - lineOut8[x];59if (abs(diff) > threshold) {60/* add the diff*percent to the original pixel */61lineOut8[x] = clip8(lineIn8[x] + diff * percent / 100);62} else {63/* new pixel is the same as imIn */64lineOut8[x] = lineIn8[x];65}66}67} else {68lineIn = (pixel *)imIn->image32[y];69lineOut = (pixel *)imOut->image32[y];70for (x = 0; x < imIn->xsize; x++) {71/* compare in/out pixels, apply sharpening */72diff = lineIn[x][0] - lineOut[x][0];73lineOut[x][0] = abs(diff) > threshold74? clip8(lineIn[x][0] + diff * percent / 100)75: lineIn[x][0];76
77diff = lineIn[x][1] - lineOut[x][1];78lineOut[x][1] = abs(diff) > threshold79? clip8(lineIn[x][1] + diff * percent / 100)80: lineIn[x][1];81
82diff = lineIn[x][2] - lineOut[x][2];83lineOut[x][2] = abs(diff) > threshold84? clip8(lineIn[x][2] + diff * percent / 100)85: lineIn[x][2];86
87diff = lineIn[x][3] - lineOut[x][3];88lineOut[x][3] = abs(diff) > threshold89? clip8(lineIn[x][3] + diff * percent / 100)90: lineIn[x][3];91}92}93}94
95ImagingSectionLeave(&cookie);96
97return imOut;98}
99