Pillow

Форк
0
/
UnsharpMask.c 
98 строк · 3.1 Кб
1
/* PILusm, a gaussian blur and unsharp masking library for PIL
2
   By Kevin Cazabon, copyright 2003
3
   kevin_cazabon@hotmail.com
4
   kevin@cazabon.com */
5

6
/* Originally released under LGPL.  Graciously donated to PIL
7
   for distribution under the standard PIL license in 2009." */
8

9
#include "Imaging.h"
10

11
typedef UINT8 pixel[4];
12

13
static inline UINT8
14
clip8(int in) {
15
    if (in >= 255) {
16
        return 255;
17
    }
18
    if (in <= 0) {
19
        return 0;
20
    }
21
    return (UINT8)in;
22
}
23

24
Imaging
25
ImagingUnsharpMask(
26
    Imaging imOut, Imaging imIn, float radius, int percent, int threshold
27
) {
28
    ImagingSectionCookie cookie;
29
    Imaging result;
30

31
    int x, y, diff;
32

33
    pixel *lineIn = NULL;
34
    pixel *lineOut = NULL;
35
    UINT8 *lineIn8 = NULL;
36
    UINT8 *lineOut8 = NULL;
37

38
    /* First, do a gaussian blur on the image, putting results in imOut
39
       temporarily. All format checks are in gaussian blur. */
40
    result = ImagingGaussianBlur(imOut, imIn, radius, radius, 3);
41
    if (!result) {
42
        return NULL;
43
    }
44

45
    /* Now, go through each pixel, compare "normal" pixel to blurred
46
       pixel. If the difference is more than threshold values, apply
47
       the OPPOSITE correction to the amount of blur, multiplied by
48
       percent. */
49

50
    ImagingSectionEnter(&cookie);
51

52
    for (y = 0; y < imIn->ysize; y++) {
53
        if (imIn->image8) {
54
            lineIn8 = imIn->image8[y];
55
            lineOut8 = imOut->image8[y];
56
            for (x = 0; x < imIn->xsize; x++) {
57
                /* compare in/out pixels, apply sharpening */
58
                diff = lineIn8[x] - lineOut8[x];
59
                if (abs(diff) > threshold) {
60
                    /* add the diff*percent to the original pixel */
61
                    lineOut8[x] = clip8(lineIn8[x] + diff * percent / 100);
62
                } else {
63
                    /* new pixel is the same as imIn */
64
                    lineOut8[x] = lineIn8[x];
65
                }
66
            }
67
        } else {
68
            lineIn = (pixel *)imIn->image32[y];
69
            lineOut = (pixel *)imOut->image32[y];
70
            for (x = 0; x < imIn->xsize; x++) {
71
                /* compare in/out pixels, apply sharpening */
72
                diff = lineIn[x][0] - lineOut[x][0];
73
                lineOut[x][0] = abs(diff) > threshold
74
                                    ? clip8(lineIn[x][0] + diff * percent / 100)
75
                                    : lineIn[x][0];
76

77
                diff = lineIn[x][1] - lineOut[x][1];
78
                lineOut[x][1] = abs(diff) > threshold
79
                                    ? clip8(lineIn[x][1] + diff * percent / 100)
80
                                    : lineIn[x][1];
81

82
                diff = lineIn[x][2] - lineOut[x][2];
83
                lineOut[x][2] = abs(diff) > threshold
84
                                    ? clip8(lineIn[x][2] + diff * percent / 100)
85
                                    : lineIn[x][2];
86

87
                diff = lineIn[x][3] - lineOut[x][3];
88
                lineOut[x][3] = abs(diff) > threshold
89
                                    ? clip8(lineIn[x][3] + diff * percent / 100)
90
                                    : lineIn[x][3];
91
            }
92
        }
93
    }
94

95
    ImagingSectionLeave(&cookie);
96

97
    return imOut;
98
}
99

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.