Pillow
413 строк · 17.3 Кб
1/*
2* The Python Imaging Library
3* $Id$
4*
5* apply convolution kernel to image
6*
7* history:
8* 1995-11-26 fl Created, supports 3x3 kernels
9* 1995-11-27 fl Added 5x5 kernels, copy border
10* 1999-07-26 fl Eliminated a few compiler warnings
11* 2002-06-09 fl Moved kernel definitions to Python
12* 2002-06-11 fl Support floating point kernels
13* 2003-09-15 fl Added ImagingExpand helper
14*
15* Copyright (c) Secret Labs AB 1997-2002. All rights reserved.
16* Copyright (c) Fredrik Lundh 1995.
17*
18* See the README file for information on usage and redistribution.
19*/
20
21/*
22* FIXME: Support RGB and RGBA/CMYK modes as well
23* FIXME: Expand image border (current version leaves border as is)
24* FIXME: Implement image processing gradient filters
25*/
26
27#include "Imaging.h"
28
29static inline UINT8
30clip8(float in) {
31if (in <= 0.0) {
32return 0;
33}
34if (in >= 255.0) {
35return 255;
36}
37return (UINT8)in;
38}
39
40static inline INT32
41clip32(float in) {
42if (in <= 0.0) {
43return 0;
44}
45if (in >= pow(2, 31) - 1) {
46return pow(2, 31) - 1;
47}
48return (INT32)in;
49}
50
51Imaging
52ImagingExpand(Imaging imIn, int xmargin, int ymargin) {
53Imaging imOut;
54int x, y;
55ImagingSectionCookie cookie;
56
57if (xmargin < 0 && ymargin < 0) {
58return (Imaging)ImagingError_ValueError("bad kernel size");
59}
60
61imOut = ImagingNewDirty(
62imIn->mode, imIn->xsize + 2 * xmargin, imIn->ysize + 2 * ymargin
63);
64if (!imOut) {
65return NULL;
66}
67
68#define EXPAND_LINE(type, image, yin, yout) \
69{ \
70for (x = 0; x < xmargin; x++) { \
71imOut->image[yout][x] = imIn->image[yin][0]; \
72} \
73for (x = 0; x < imIn->xsize; x++) { \
74imOut->image[yout][x + xmargin] = imIn->image[yin][x]; \
75} \
76for (x = 0; x < xmargin; x++) { \
77imOut->image[yout][xmargin + imIn->xsize + x] = \
78imIn->image[yin][imIn->xsize - 1]; \
79} \
80}
81
82#define EXPAND(type, image) \
83{ \
84for (y = 0; y < ymargin; y++) { \
85EXPAND_LINE(type, image, 0, y); \
86} \
87for (y = 0; y < imIn->ysize; y++) { \
88EXPAND_LINE(type, image, y, y + ymargin); \
89} \
90for (y = 0; y < ymargin; y++) { \
91EXPAND_LINE(type, image, imIn->ysize - 1, ymargin + imIn->ysize + y); \
92} \
93}
94
95ImagingSectionEnter(&cookie);
96if (imIn->image8) {
97EXPAND(UINT8, image8);
98} else {
99EXPAND(INT32, image32);
100}
101ImagingSectionLeave(&cookie);
102
103ImagingCopyPalette(imOut, imIn);
104
105return imOut;
106}
107
108void
109ImagingFilter3x3(Imaging imOut, Imaging im, const float *kernel, float offset) {
110#define KERNEL1x3(in0, x, kernel, d) \
111(_i2f(in0[x - d]) * (kernel)[0] + _i2f(in0[x]) * (kernel)[1] + \
112_i2f(in0[x + d]) * (kernel)[2])
113
114int x = 0, y = 0;
115
116memcpy(imOut->image[0], im->image[0], im->linesize);
117if (im->bands == 1) {
118// Add one time for rounding
119offset += 0.5;
120if (im->type == IMAGING_TYPE_INT32) {
121for (y = 1; y < im->ysize - 1; y++) {
122INT32 *in_1 = (INT32 *)im->image[y - 1];
123INT32 *in0 = (INT32 *)im->image[y];
124INT32 *in1 = (INT32 *)im->image[y + 1];
125INT32 *out = (INT32 *)imOut->image[y];
126
127out[0] = in0[0];
128for (x = 1; x < im->xsize - 1; x++) {
129float ss = offset;
130ss += KERNEL1x3(in1, x, &kernel[0], 1);
131ss += KERNEL1x3(in0, x, &kernel[3], 1);
132ss += KERNEL1x3(in_1, x, &kernel[6], 1);
133out[x] = clip32(ss);
134}
135out[x] = in0[x];
136}
137} else {
138for (y = 1; y < im->ysize - 1; y++) {
139UINT8 *in_1 = (UINT8 *)im->image[y - 1];
140UINT8 *in0 = (UINT8 *)im->image[y];
141UINT8 *in1 = (UINT8 *)im->image[y + 1];
142UINT8 *out = (UINT8 *)imOut->image[y];
143
144out[0] = in0[0];
145for (x = 1; x < im->xsize - 1; x++) {
146float ss = offset;
147ss += KERNEL1x3(in1, x, &kernel[0], 1);
148ss += KERNEL1x3(in0, x, &kernel[3], 1);
149ss += KERNEL1x3(in_1, x, &kernel[6], 1);
150out[x] = clip8(ss);
151}
152out[x] = in0[x];
153}
154}
155} else {
156// Add one time for rounding
157offset += 0.5;
158for (y = 1; y < im->ysize - 1; y++) {
159UINT8 *in_1 = (UINT8 *)im->image[y - 1];
160UINT8 *in0 = (UINT8 *)im->image[y];
161UINT8 *in1 = (UINT8 *)im->image[y + 1];
162UINT8 *out = (UINT8 *)imOut->image[y];
163
164memcpy(out, in0, sizeof(UINT32));
165if (im->bands == 2) {
166for (x = 1; x < im->xsize - 1; x++) {
167float ss0 = offset;
168float ss3 = offset;
169UINT32 v;
170ss0 += KERNEL1x3(in1, x * 4 + 0, &kernel[0], 4);
171ss3 += KERNEL1x3(in1, x * 4 + 3, &kernel[0], 4);
172ss0 += KERNEL1x3(in0, x * 4 + 0, &kernel[3], 4);
173ss3 += KERNEL1x3(in0, x * 4 + 3, &kernel[3], 4);
174ss0 += KERNEL1x3(in_1, x * 4 + 0, &kernel[6], 4);
175ss3 += KERNEL1x3(in_1, x * 4 + 3, &kernel[6], 4);
176v = MAKE_UINT32(clip8(ss0), 0, 0, clip8(ss3));
177memcpy(out + x * sizeof(v), &v, sizeof(v));
178}
179} else if (im->bands == 3) {
180for (x = 1; x < im->xsize - 1; x++) {
181float ss0 = offset;
182float ss1 = offset;
183float ss2 = offset;
184UINT32 v;
185ss0 += KERNEL1x3(in1, x * 4 + 0, &kernel[0], 4);
186ss1 += KERNEL1x3(in1, x * 4 + 1, &kernel[0], 4);
187ss2 += KERNEL1x3(in1, x * 4 + 2, &kernel[0], 4);
188ss0 += KERNEL1x3(in0, x * 4 + 0, &kernel[3], 4);
189ss1 += KERNEL1x3(in0, x * 4 + 1, &kernel[3], 4);
190ss2 += KERNEL1x3(in0, x * 4 + 2, &kernel[3], 4);
191ss0 += KERNEL1x3(in_1, x * 4 + 0, &kernel[6], 4);
192ss1 += KERNEL1x3(in_1, x * 4 + 1, &kernel[6], 4);
193ss2 += KERNEL1x3(in_1, x * 4 + 2, &kernel[6], 4);
194v = MAKE_UINT32(clip8(ss0), clip8(ss1), clip8(ss2), 0);
195memcpy(out + x * sizeof(v), &v, sizeof(v));
196}
197} else if (im->bands == 4) {
198for (x = 1; x < im->xsize - 1; x++) {
199float ss0 = offset;
200float ss1 = offset;
201float ss2 = offset;
202float ss3 = offset;
203UINT32 v;
204ss0 += KERNEL1x3(in1, x * 4 + 0, &kernel[0], 4);
205ss1 += KERNEL1x3(in1, x * 4 + 1, &kernel[0], 4);
206ss2 += KERNEL1x3(in1, x * 4 + 2, &kernel[0], 4);
207ss3 += KERNEL1x3(in1, x * 4 + 3, &kernel[0], 4);
208ss0 += KERNEL1x3(in0, x * 4 + 0, &kernel[3], 4);
209ss1 += KERNEL1x3(in0, x * 4 + 1, &kernel[3], 4);
210ss2 += KERNEL1x3(in0, x * 4 + 2, &kernel[3], 4);
211ss3 += KERNEL1x3(in0, x * 4 + 3, &kernel[3], 4);
212ss0 += KERNEL1x3(in_1, x * 4 + 0, &kernel[6], 4);
213ss1 += KERNEL1x3(in_1, x * 4 + 1, &kernel[6], 4);
214ss2 += KERNEL1x3(in_1, x * 4 + 2, &kernel[6], 4);
215ss3 += KERNEL1x3(in_1, x * 4 + 3, &kernel[6], 4);
216v = MAKE_UINT32(clip8(ss0), clip8(ss1), clip8(ss2), clip8(ss3));
217memcpy(out + x * sizeof(v), &v, sizeof(v));
218}
219}
220memcpy(out + x * sizeof(UINT32), in0 + x * sizeof(UINT32), sizeof(UINT32));
221}
222}
223memcpy(imOut->image[y], im->image[y], im->linesize);
224}
225
226void
227ImagingFilter5x5(Imaging imOut, Imaging im, const float *kernel, float offset) {
228#define KERNEL1x5(in0, x, kernel, d) \
229(_i2f(in0[x - d - d]) * (kernel)[0] + _i2f(in0[x - d]) * (kernel)[1] + \
230_i2f(in0[x]) * (kernel)[2] + _i2f(in0[x + d]) * (kernel)[3] + \
231_i2f(in0[x + d + d]) * (kernel)[4])
232
233int x = 0, y = 0;
234
235memcpy(imOut->image[0], im->image[0], im->linesize);
236memcpy(imOut->image[1], im->image[1], im->linesize);
237if (im->bands == 1) {
238// Add one time for rounding
239offset += 0.5;
240if (im->type == IMAGING_TYPE_INT32) {
241for (y = 2; y < im->ysize - 2; y++) {
242INT32 *in_2 = (INT32 *)im->image[y - 2];
243INT32 *in_1 = (INT32 *)im->image[y - 1];
244INT32 *in0 = (INT32 *)im->image[y];
245INT32 *in1 = (INT32 *)im->image[y + 1];
246INT32 *in2 = (INT32 *)im->image[y + 2];
247INT32 *out = (INT32 *)imOut->image[y];
248
249out[0] = in0[0];
250out[1] = in0[1];
251for (x = 2; x < im->xsize - 2; x++) {
252float ss = offset;
253ss += KERNEL1x5(in2, x, &kernel[0], 1);
254ss += KERNEL1x5(in1, x, &kernel[5], 1);
255ss += KERNEL1x5(in0, x, &kernel[10], 1);
256ss += KERNEL1x5(in_1, x, &kernel[15], 1);
257ss += KERNEL1x5(in_2, x, &kernel[20], 1);
258out[x] = clip32(ss);
259}
260out[x + 0] = in0[x + 0];
261out[x + 1] = in0[x + 1];
262}
263} else {
264for (y = 2; y < im->ysize - 2; y++) {
265UINT8 *in_2 = (UINT8 *)im->image[y - 2];
266UINT8 *in_1 = (UINT8 *)im->image[y - 1];
267UINT8 *in0 = (UINT8 *)im->image[y];
268UINT8 *in1 = (UINT8 *)im->image[y + 1];
269UINT8 *in2 = (UINT8 *)im->image[y + 2];
270UINT8 *out = (UINT8 *)imOut->image[y];
271
272out[0] = in0[0];
273out[1] = in0[1];
274for (x = 2; x < im->xsize - 2; x++) {
275float ss = offset;
276ss += KERNEL1x5(in2, x, &kernel[0], 1);
277ss += KERNEL1x5(in1, x, &kernel[5], 1);
278ss += KERNEL1x5(in0, x, &kernel[10], 1);
279ss += KERNEL1x5(in_1, x, &kernel[15], 1);
280ss += KERNEL1x5(in_2, x, &kernel[20], 1);
281out[x] = clip8(ss);
282}
283out[x + 0] = in0[x + 0];
284out[x + 1] = in0[x + 1];
285}
286}
287} else {
288// Add one time for rounding
289offset += 0.5;
290for (y = 2; y < im->ysize - 2; y++) {
291UINT8 *in_2 = (UINT8 *)im->image[y - 2];
292UINT8 *in_1 = (UINT8 *)im->image[y - 1];
293UINT8 *in0 = (UINT8 *)im->image[y];
294UINT8 *in1 = (UINT8 *)im->image[y + 1];
295UINT8 *in2 = (UINT8 *)im->image[y + 2];
296UINT8 *out = (UINT8 *)imOut->image[y];
297
298memcpy(out, in0, sizeof(UINT32) * 2);
299if (im->bands == 2) {
300for (x = 2; x < im->xsize - 2; x++) {
301float ss0 = offset;
302float ss3 = offset;
303UINT32 v;
304ss0 += KERNEL1x5(in2, x * 4 + 0, &kernel[0], 4);
305ss3 += KERNEL1x5(in2, x * 4 + 3, &kernel[0], 4);
306ss0 += KERNEL1x5(in1, x * 4 + 0, &kernel[5], 4);
307ss3 += KERNEL1x5(in1, x * 4 + 3, &kernel[5], 4);
308ss0 += KERNEL1x5(in0, x * 4 + 0, &kernel[10], 4);
309ss3 += KERNEL1x5(in0, x * 4 + 3, &kernel[10], 4);
310ss0 += KERNEL1x5(in_1, x * 4 + 0, &kernel[15], 4);
311ss3 += KERNEL1x5(in_1, x * 4 + 3, &kernel[15], 4);
312ss0 += KERNEL1x5(in_2, x * 4 + 0, &kernel[20], 4);
313ss3 += KERNEL1x5(in_2, x * 4 + 3, &kernel[20], 4);
314v = MAKE_UINT32(clip8(ss0), 0, 0, clip8(ss3));
315memcpy(out + x * sizeof(v), &v, sizeof(v));
316}
317} else if (im->bands == 3) {
318for (x = 2; x < im->xsize - 2; x++) {
319float ss0 = offset;
320float ss1 = offset;
321float ss2 = offset;
322UINT32 v;
323ss0 += KERNEL1x5(in2, x * 4 + 0, &kernel[0], 4);
324ss1 += KERNEL1x5(in2, x * 4 + 1, &kernel[0], 4);
325ss2 += KERNEL1x5(in2, x * 4 + 2, &kernel[0], 4);
326ss0 += KERNEL1x5(in1, x * 4 + 0, &kernel[5], 4);
327ss1 += KERNEL1x5(in1, x * 4 + 1, &kernel[5], 4);
328ss2 += KERNEL1x5(in1, x * 4 + 2, &kernel[5], 4);
329ss0 += KERNEL1x5(in0, x * 4 + 0, &kernel[10], 4);
330ss1 += KERNEL1x5(in0, x * 4 + 1, &kernel[10], 4);
331ss2 += KERNEL1x5(in0, x * 4 + 2, &kernel[10], 4);
332ss0 += KERNEL1x5(in_1, x * 4 + 0, &kernel[15], 4);
333ss1 += KERNEL1x5(in_1, x * 4 + 1, &kernel[15], 4);
334ss2 += KERNEL1x5(in_1, x * 4 + 2, &kernel[15], 4);
335ss0 += KERNEL1x5(in_2, x * 4 + 0, &kernel[20], 4);
336ss1 += KERNEL1x5(in_2, x * 4 + 1, &kernel[20], 4);
337ss2 += KERNEL1x5(in_2, x * 4 + 2, &kernel[20], 4);
338v = MAKE_UINT32(clip8(ss0), clip8(ss1), clip8(ss2), 0);
339memcpy(out + x * sizeof(v), &v, sizeof(v));
340}
341} else if (im->bands == 4) {
342for (x = 2; x < im->xsize - 2; x++) {
343float ss0 = offset;
344float ss1 = offset;
345float ss2 = offset;
346float ss3 = offset;
347UINT32 v;
348ss0 += KERNEL1x5(in2, x * 4 + 0, &kernel[0], 4);
349ss1 += KERNEL1x5(in2, x * 4 + 1, &kernel[0], 4);
350ss2 += KERNEL1x5(in2, x * 4 + 2, &kernel[0], 4);
351ss3 += KERNEL1x5(in2, x * 4 + 3, &kernel[0], 4);
352ss0 += KERNEL1x5(in1, x * 4 + 0, &kernel[5], 4);
353ss1 += KERNEL1x5(in1, x * 4 + 1, &kernel[5], 4);
354ss2 += KERNEL1x5(in1, x * 4 + 2, &kernel[5], 4);
355ss3 += KERNEL1x5(in1, x * 4 + 3, &kernel[5], 4);
356ss0 += KERNEL1x5(in0, x * 4 + 0, &kernel[10], 4);
357ss1 += KERNEL1x5(in0, x * 4 + 1, &kernel[10], 4);
358ss2 += KERNEL1x5(in0, x * 4 + 2, &kernel[10], 4);
359ss3 += KERNEL1x5(in0, x * 4 + 3, &kernel[10], 4);
360ss0 += KERNEL1x5(in_1, x * 4 + 0, &kernel[15], 4);
361ss1 += KERNEL1x5(in_1, x * 4 + 1, &kernel[15], 4);
362ss2 += KERNEL1x5(in_1, x * 4 + 2, &kernel[15], 4);
363ss3 += KERNEL1x5(in_1, x * 4 + 3, &kernel[15], 4);
364ss0 += KERNEL1x5(in_2, x * 4 + 0, &kernel[20], 4);
365ss1 += KERNEL1x5(in_2, x * 4 + 1, &kernel[20], 4);
366ss2 += KERNEL1x5(in_2, x * 4 + 2, &kernel[20], 4);
367ss3 += KERNEL1x5(in_2, x * 4 + 3, &kernel[20], 4);
368v = MAKE_UINT32(clip8(ss0), clip8(ss1), clip8(ss2), clip8(ss3));
369memcpy(out + x * sizeof(v), &v, sizeof(v));
370}
371}
372memcpy(
373out + x * sizeof(UINT32), in0 + x * sizeof(UINT32), sizeof(UINT32) * 2
374);
375}
376}
377memcpy(imOut->image[y], im->image[y], im->linesize);
378memcpy(imOut->image[y + 1], im->image[y + 1], im->linesize);
379}
380
381Imaging
382ImagingFilter(Imaging im, int xsize, int ysize, const FLOAT32 *kernel, FLOAT32 offset) {
383Imaging imOut;
384ImagingSectionCookie cookie;
385
386if (im->type != IMAGING_TYPE_UINT8 && im->type != IMAGING_TYPE_INT32) {
387return (Imaging)ImagingError_ModeError();
388}
389
390if (im->xsize < xsize || im->ysize < ysize) {
391return ImagingCopy(im);
392}
393
394if ((xsize != 3 && xsize != 5) || xsize != ysize) {
395return (Imaging)ImagingError_ValueError("bad kernel size");
396}
397
398imOut = ImagingNewDirty(im->mode, im->xsize, im->ysize);
399if (!imOut) {
400return NULL;
401}
402
403ImagingSectionEnter(&cookie);
404if (xsize == 3) {
405/* 3x3 kernel. */
406ImagingFilter3x3(imOut, im, kernel, offset);
407} else {
408/* 5x5 kernel. */
409ImagingFilter5x5(imOut, im, kernel, offset);
410}
411ImagingSectionLeave(&cookie);
412return imOut;
413}
414