1 | /* |
---|
2 | * libpipi Pathetic image processing interface library |
---|
3 | * Copyright (c) 2004-2008 Sam Hocevar <sam@zoy.org> |
---|
4 | * All Rights Reserved |
---|
5 | * |
---|
6 | * $Id$ |
---|
7 | * |
---|
8 | * This library is free software. It comes without any warranty, to |
---|
9 | * the extent permitted by applicable law. You can redistribute it |
---|
10 | * and/or modify it under the terms of the Do What The Fuck You Want |
---|
11 | * To Public License, Version 2, as published by Sam Hocevar. See |
---|
12 | * http://sam.zoy.org/wtfpl/COPYING for more details. |
---|
13 | */ |
---|
14 | |
---|
15 | /* |
---|
16 | * blur.c: blur functions |
---|
17 | */ |
---|
18 | |
---|
19 | #include "config.h" |
---|
20 | |
---|
21 | #include <stdlib.h> |
---|
22 | #include <stdio.h> |
---|
23 | #include <string.h> |
---|
24 | #include <math.h> |
---|
25 | #ifndef M_PI |
---|
26 | # define M_PI 3.14159265358979323846 |
---|
27 | #endif |
---|
28 | |
---|
29 | #include "pipi.h" |
---|
30 | #include "pipi_internals.h" |
---|
31 | |
---|
32 | #if !defined TEMPLATE_FILE /* This file uses the template system */ |
---|
33 | #define TEMPLATE_FLAGS SET_FLAG_GRAY | SET_FLAG_WRAP |
---|
34 | #define TEMPLATE_FILE "filter/blur.c" |
---|
35 | #include "pipi_template.h" |
---|
36 | |
---|
37 | /* Any standard deviation below this value will be rounded up, in order |
---|
38 | * to avoid ridiculously low values. exp(-1/(2*0.2*0.2)) is < 10^-5 so |
---|
39 | * there is little chance that any value below 0.2 will be useful. */ |
---|
40 | #define BLUR_EPSILON 0.2 |
---|
41 | |
---|
42 | pipi_image_t *pipi_gaussian_blur(pipi_image_t *src, float radius) |
---|
43 | { |
---|
44 | return pipi_gaussian_blur_ext(src, radius, radius, 0.0, 0.0, 0.0); |
---|
45 | } |
---|
46 | |
---|
47 | pipi_image_t *pipi_gaussian_blur_ext(pipi_image_t *src, float rx, float ry, |
---|
48 | float angle, float dx, float dy) |
---|
49 | { |
---|
50 | pipi_image_t *ret; |
---|
51 | double *kernel; |
---|
52 | double Kx, Ky, t = 0.0, sint, cost, bbx, bby; |
---|
53 | int i, j, krx, kry, m, n; |
---|
54 | |
---|
55 | if(rx < BLUR_EPSILON) rx = BLUR_EPSILON; |
---|
56 | if(ry < BLUR_EPSILON) ry = BLUR_EPSILON; |
---|
57 | |
---|
58 | sint = sin(angle * M_PI / 180.); |
---|
59 | cost = cos(angle * M_PI / 180.); |
---|
60 | |
---|
61 | /* Compute the final ellipse's bounding box */ |
---|
62 | bbx = sqrt(rx * rx * cost * cost + ry * ry * sint * sint); |
---|
63 | bby = sqrt(ry * ry * cost * cost + rx * rx * sint * sint); |
---|
64 | |
---|
65 | /* FIXME: the kernel becomes far too big with large values of dx, because |
---|
66 | * we grow both left and right. Fix the growing direction. */ |
---|
67 | krx = (int)(3. * bbx + .99999 + ceil(abs(dx))); |
---|
68 | m = 2 * krx + 1; |
---|
69 | Kx = -1. / (2. * rx * rx); |
---|
70 | |
---|
71 | kry = (int)(3. * bby + .99999 + ceil(abs(dy))); |
---|
72 | n = 2 * kry + 1; |
---|
73 | Ky = -1. / (2. * ry * ry); |
---|
74 | |
---|
75 | kernel = malloc(m * n * sizeof(double)); |
---|
76 | |
---|
77 | for(j = -kry; j <= kry; j++) |
---|
78 | { |
---|
79 | for(i = -krx; i <= krx; i++) |
---|
80 | { |
---|
81 | /* FIXME: this level of interpolation sucks. We should |
---|
82 | * interpolate on the full NxN grid for better quality. */ |
---|
83 | static double const samples[] = |
---|
84 | { |
---|
85 | .0, .0, 1, |
---|
86 | -.40, -.40, 0.8, |
---|
87 | -.30, .0, 0.9, |
---|
88 | -.40, .40, 0.8, |
---|
89 | .0, .30, 0.9, |
---|
90 | .40, .40, 0.8, |
---|
91 | .30, .0, 0.9, |
---|
92 | .40, -.40, 0.8, |
---|
93 | .0, -.30, 0.9, |
---|
94 | }; |
---|
95 | double u, v, ex, ey, d = 0.; |
---|
96 | unsigned int k; |
---|
97 | |
---|
98 | for(k = 0; k < sizeof(samples) / sizeof(*samples) / 3; k++) |
---|
99 | { |
---|
100 | u = ((double)i + samples[k * 3]) * cost |
---|
101 | - ((double)j + samples[k * 3 + 1]) * sint + dx; |
---|
102 | v = ((double)i + samples[k * 3]) * sint |
---|
103 | + ((double)j + samples[k * 3 + 1]) * cost + dy; |
---|
104 | ex = Kx * u * u; |
---|
105 | ey = Ky * v * v; |
---|
106 | d += samples[k * 3 + 2] * exp(ex + ey); |
---|
107 | |
---|
108 | /* Do not interpolate if this is a standard gaussian. */ |
---|
109 | if(!dx && !dy && !angle) |
---|
110 | break; |
---|
111 | } |
---|
112 | |
---|
113 | kernel[(j + kry) * m + i + krx] = d; |
---|
114 | t += d; |
---|
115 | } |
---|
116 | } |
---|
117 | |
---|
118 | for(j = 0; j < n; j++) |
---|
119 | for(i = 0; i < m; i++) |
---|
120 | kernel[j * m + i] /= t; |
---|
121 | |
---|
122 | ret = pipi_convolution(src, m, n, kernel); |
---|
123 | |
---|
124 | free(kernel); |
---|
125 | |
---|
126 | return ret; |
---|
127 | } |
---|
128 | |
---|
129 | pipi_image_t *pipi_box_blur(pipi_image_t *src, int size) |
---|
130 | { |
---|
131 | return pipi_box_blur_ext(src, size, size); |
---|
132 | } |
---|
133 | |
---|
134 | pipi_image_t *pipi_box_blur_ext(pipi_image_t *src, int m, int n) |
---|
135 | { |
---|
136 | if(src->wrap) |
---|
137 | { |
---|
138 | if(src->last_modified == PIPI_PIXELS_Y_F32) |
---|
139 | return boxblur_gray_wrap(src, m, n); |
---|
140 | |
---|
141 | return boxblur_wrap(src, m, n); |
---|
142 | } |
---|
143 | else |
---|
144 | { |
---|
145 | if(src->last_modified == PIPI_PIXELS_Y_F32) |
---|
146 | return boxblur_gray(src, m, n); |
---|
147 | |
---|
148 | return boxblur(src, m, n); |
---|
149 | } |
---|
150 | } |
---|
151 | |
---|
152 | #else /* XXX: the following functions use the template system */ |
---|
153 | |
---|
154 | static pipi_image_t *T(boxblur)(pipi_image_t *src, int m, int n) |
---|
155 | { |
---|
156 | pipi_image_t *dst; |
---|
157 | pipi_pixels_t *srcp, *dstp; |
---|
158 | float *srcdata, *dstdata; |
---|
159 | double *acc; |
---|
160 | int x, y, w, h, i, j, i2, j2, size; |
---|
161 | |
---|
162 | w = src->w; |
---|
163 | h = src->h; |
---|
164 | size = (2 * m + 1) * (2 * n + 1); |
---|
165 | |
---|
166 | srcp = FLAG_GRAY ? pipi_get_pixels(src, PIPI_PIXELS_Y_F32) |
---|
167 | : pipi_get_pixels(src, PIPI_PIXELS_RGBA_F32); |
---|
168 | srcdata = (float *)srcp->pixels; |
---|
169 | |
---|
170 | dst = pipi_new(w, h); |
---|
171 | dstp = FLAG_GRAY ? pipi_get_pixels(dst, PIPI_PIXELS_Y_F32) |
---|
172 | : pipi_get_pixels(dst, PIPI_PIXELS_RGBA_F32); |
---|
173 | dstdata = (float *)dstp->pixels; |
---|
174 | |
---|
175 | acc = malloc(w * (FLAG_GRAY ? 1 : 4) * sizeof(double)); |
---|
176 | |
---|
177 | /* Step 1: fill the accumulator */ |
---|
178 | for(x = 0; x < w; x++) |
---|
179 | { |
---|
180 | double r = 0., g = 0., b = 0., a = 0.; |
---|
181 | double t = 0.; |
---|
182 | |
---|
183 | for(j = -n; j <= n; j++) |
---|
184 | { |
---|
185 | if(FLAG_WRAP) |
---|
186 | j2 = (j < 0) ? h - 1 - ((-j - 1) % h) : j % h; |
---|
187 | else |
---|
188 | j2 = (j < 0) ? 0 : (j >= h) ? h - 1 : j; |
---|
189 | |
---|
190 | if(FLAG_GRAY) |
---|
191 | t += srcdata[j2 * w + x]; |
---|
192 | else |
---|
193 | { |
---|
194 | r += srcdata[4 * (j2 * w + x)]; |
---|
195 | g += srcdata[4 * (j2 * w + x) + 1]; |
---|
196 | b += srcdata[4 * (j2 * w + x) + 2]; |
---|
197 | a += srcdata[4 * (j2 * w + x) + 3]; |
---|
198 | } |
---|
199 | } |
---|
200 | |
---|
201 | if(FLAG_GRAY) |
---|
202 | acc[x] = t; |
---|
203 | else |
---|
204 | { |
---|
205 | acc[4 * x] = r; |
---|
206 | acc[4 * x + 1] = g; |
---|
207 | acc[4 * x + 2] = b; |
---|
208 | acc[4 * x + 3] = a; |
---|
209 | } |
---|
210 | } |
---|
211 | |
---|
212 | /* Step 2: blur the image, line by line */ |
---|
213 | for(y = 0; y < h; y++) |
---|
214 | { |
---|
215 | double r = 0., g = 0., b = 0., a = 0.; |
---|
216 | double t = 0.; |
---|
217 | |
---|
218 | /* 2.1: compute the first pixel */ |
---|
219 | for(i = -m; i <= m; i++) |
---|
220 | { |
---|
221 | if(FLAG_WRAP) |
---|
222 | i2 = (i < 0) ? w - 1 - ((-i - 1) % w) : i % w; |
---|
223 | else |
---|
224 | i2 = (i < 0) ? 0 : (i >= w) ? w - 1 : i; |
---|
225 | |
---|
226 | if(FLAG_GRAY) |
---|
227 | t += acc[i2]; |
---|
228 | else |
---|
229 | { |
---|
230 | r += acc[4 * i2]; |
---|
231 | g += acc[4 * i2 + 1]; |
---|
232 | b += acc[4 * i2 + 2]; |
---|
233 | a += acc[4 * i2 + 3]; |
---|
234 | } |
---|
235 | } |
---|
236 | |
---|
237 | /* 2.2: iterate on the whole line */ |
---|
238 | for(x = 0; x < w; x++) |
---|
239 | { |
---|
240 | int u, u2, v, v2; |
---|
241 | |
---|
242 | if(FLAG_GRAY) |
---|
243 | { |
---|
244 | dstdata[y * w + x] = t / size; |
---|
245 | } |
---|
246 | else |
---|
247 | { |
---|
248 | dstdata[4 * (y * w + x)] = r / size; |
---|
249 | dstdata[4 * (y * w + x) + 1] = g / size; |
---|
250 | dstdata[4 * (y * w + x) + 2] = b / size; |
---|
251 | dstdata[4 * (y * w + x) + 3] = a / size; |
---|
252 | } |
---|
253 | |
---|
254 | u = x - m; |
---|
255 | if(FLAG_WRAP) |
---|
256 | u2 = (u < 0) ? w - 1 - ((-u - 1) % w) : u % w; |
---|
257 | else |
---|
258 | u2 = (u < 0) ? 0 : (u >= w) ? w - 1 : u; |
---|
259 | v = x + m + 1; |
---|
260 | if(FLAG_WRAP) |
---|
261 | v2 = (v < 0) ? w - 1 - ((-v - 1) % w) : v % w; |
---|
262 | else |
---|
263 | v2 = (v < 0) ? 0 : (v >= w) ? w - 1 : v; |
---|
264 | if(FLAG_GRAY) |
---|
265 | { |
---|
266 | t = t - acc[u2] + acc[v2]; |
---|
267 | } |
---|
268 | else |
---|
269 | { |
---|
270 | r = r - acc[4 * u2] + acc[4 * v2]; |
---|
271 | g = g - acc[4 * u2 + 1] + acc[4 * v2 + 1]; |
---|
272 | b = b - acc[4 * u2 + 2] + acc[4 * v2 + 2]; |
---|
273 | a = a - acc[4 * u2 + 3] + acc[4 * v2 + 3]; |
---|
274 | } |
---|
275 | } |
---|
276 | |
---|
277 | /* 2.3: update the accumulator */ |
---|
278 | for(x = 0; x < w; x++) |
---|
279 | { |
---|
280 | int u, u2, v, v2; |
---|
281 | |
---|
282 | u = y - n; |
---|
283 | if(FLAG_WRAP) |
---|
284 | u2 = (u < 0) ? w - 1 - ((-u - 1) % w) : u % w; |
---|
285 | else |
---|
286 | u2 = (u < 0) ? 0 : (u >= w) ? w - 1 : u; |
---|
287 | v = y + n + 1; |
---|
288 | if(FLAG_WRAP) |
---|
289 | v2 = (v < 0) ? w - 1 - ((-v - 1) % w) : v % w; |
---|
290 | else |
---|
291 | v2 = (v < 0) ? 0 : (v >= w) ? w - 1 : v; |
---|
292 | if(FLAG_GRAY) |
---|
293 | { |
---|
294 | acc[x] += srcdata[v2 * w + x] - srcdata[u2 * w + x]; |
---|
295 | } |
---|
296 | else |
---|
297 | { |
---|
298 | int uoff = 4 * (u2 * w + x); |
---|
299 | int voff = 4 * (v2 * w + x); |
---|
300 | |
---|
301 | acc[4 * x] += srcdata[voff] - srcdata[uoff]; |
---|
302 | acc[4 * x + 1] += srcdata[voff + 1] - srcdata[uoff + 1]; |
---|
303 | acc[4 * x + 2] += srcdata[voff + 2] - srcdata[uoff + 2]; |
---|
304 | acc[4 * x + 3] += srcdata[voff + 3] - srcdata[uoff + 3]; |
---|
305 | } |
---|
306 | } |
---|
307 | } |
---|
308 | |
---|
309 | free(acc); |
---|
310 | |
---|
311 | return dst; |
---|
312 | } |
---|
313 | |
---|
314 | #endif |
---|
315 | |
---|