1 | /* |
---|
2 | * libpipi Proper image processing implementation 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 | #include "common.h" |
---|
21 | |
---|
22 | #include <stdlib.h> |
---|
23 | #include <stdio.h> |
---|
24 | #include <string.h> |
---|
25 | #include <math.h> |
---|
26 | |
---|
27 | #include "pipi.h" |
---|
28 | #include "pipi_internals.h" |
---|
29 | |
---|
30 | /* Any standard deviation below this value will be rounded up, in order |
---|
31 | * to avoid ridiculously low values. exp(-1/(2*0.2*0.2)) is < 10^-5 so |
---|
32 | * there is little chance that any value below 0.2 will be useful. */ |
---|
33 | #define BLUR_EPSILON 0.2 |
---|
34 | |
---|
35 | pipi_image_t *pipi_gaussian_blur(pipi_image_t *src, float radius) |
---|
36 | { |
---|
37 | return pipi_gaussian_blur_ext(src, radius, radius, 0.0, 0.0, 0.0); |
---|
38 | } |
---|
39 | |
---|
40 | pipi_image_t *pipi_gaussian_blur_ext(pipi_image_t *src, float rx, float ry, |
---|
41 | float angle, float dx, float dy) |
---|
42 | { |
---|
43 | pipi_image_t *ret; |
---|
44 | double *kernel; |
---|
45 | double Kx, Ky, t = 0.0, sint, cost, bbx, bby; |
---|
46 | int i, j, krx, kry, m, n; |
---|
47 | |
---|
48 | if(rx < BLUR_EPSILON) rx = BLUR_EPSILON; |
---|
49 | if(ry < BLUR_EPSILON) ry = BLUR_EPSILON; |
---|
50 | |
---|
51 | sint = sin(angle * M_PI / 180.); |
---|
52 | cost = cos(angle * M_PI / 180.); |
---|
53 | |
---|
54 | /* Compute the final ellipse's bounding box */ |
---|
55 | bbx = sqrt(rx * rx * cost * cost + ry * ry * sint * sint); |
---|
56 | bby = sqrt(ry * ry * cost * cost + rx * rx * sint * sint); |
---|
57 | |
---|
58 | /* FIXME: the kernel becomes far too big with large values of dx, because |
---|
59 | * we grow both left and right. Fix the growing direction. */ |
---|
60 | krx = (int)(3. * bbx + .99999 + ceil(abs(dx))); |
---|
61 | m = 2 * krx + 1; |
---|
62 | Kx = -1. / (2. * rx * rx); |
---|
63 | |
---|
64 | kry = (int)(3. * bby + .99999 + ceil(abs(dy))); |
---|
65 | n = 2 * kry + 1; |
---|
66 | Ky = -1. / (2. * ry * ry); |
---|
67 | |
---|
68 | kernel = malloc(m * n * sizeof(double)); |
---|
69 | |
---|
70 | for(j = -kry; j <= kry; j++) |
---|
71 | { |
---|
72 | for(i = -krx; i <= krx; i++) |
---|
73 | { |
---|
74 | /* FIXME: this level of interpolation sucks. We should |
---|
75 | * interpolate on the full NxN grid for better quality. */ |
---|
76 | static double const samples[] = |
---|
77 | { |
---|
78 | .0, .0, 1, |
---|
79 | -.40, -.40, 0.8, |
---|
80 | -.30, .0, 0.9, |
---|
81 | -.40, .40, 0.8, |
---|
82 | .0, .30, 0.9, |
---|
83 | .40, .40, 0.8, |
---|
84 | .30, .0, 0.9, |
---|
85 | .40, -.40, 0.8, |
---|
86 | .0, -.30, 0.9, |
---|
87 | }; |
---|
88 | double u, v, ex, ey, d = 0.; |
---|
89 | unsigned int k; |
---|
90 | |
---|
91 | for(k = 0; k < sizeof(samples) / sizeof(*samples) / 3; k++) |
---|
92 | { |
---|
93 | u = ((double)i + samples[k * 3]) * cost |
---|
94 | - ((double)j + samples[k * 3 + 1]) * sint + dx; |
---|
95 | v = ((double)i + samples[k * 3]) * sint |
---|
96 | + ((double)j + samples[k * 3 + 1]) * cost + dy; |
---|
97 | ex = Kx * u * u; |
---|
98 | ey = Ky * v * v; |
---|
99 | d += samples[k * 3 + 2] * exp(ex + ey); |
---|
100 | } |
---|
101 | |
---|
102 | kernel[(j + kry) * m + i + krx] = d; |
---|
103 | t += d; |
---|
104 | } |
---|
105 | } |
---|
106 | |
---|
107 | for(j = 0; j < n; j++) |
---|
108 | for(i = 0; i < m; i++) |
---|
109 | kernel[j * m + i] /= t; |
---|
110 | |
---|
111 | ret = pipi_convolution(src, m, n, kernel); |
---|
112 | |
---|
113 | free(kernel); |
---|
114 | |
---|
115 | return ret; |
---|
116 | } |
---|
117 | |
---|
118 | pipi_image_t *pipi_box_blur(pipi_image_t *src, int size) |
---|
119 | { |
---|
120 | return pipi_box_blur_ext(src, size, size); |
---|
121 | } |
---|
122 | |
---|
123 | /* FIXME: split this into templates for wrap-around and proper gray support */ |
---|
124 | pipi_image_t *pipi_box_blur_ext(pipi_image_t *src, int m, int n) |
---|
125 | { |
---|
126 | pipi_image_t *dst; |
---|
127 | pipi_pixels_t *srcp, *dstp; |
---|
128 | float *srcdata, *dstdata; |
---|
129 | double *acc; |
---|
130 | int x, y, w, h, i, j, size, gray; |
---|
131 | |
---|
132 | w = src->w; |
---|
133 | h = src->h; |
---|
134 | size = (2 * m + 1) * (2 * n + 1); |
---|
135 | |
---|
136 | gray = (src->last_modified == PIPI_PIXELS_Y_F); |
---|
137 | |
---|
138 | srcp = gray ? pipi_getpixels(src, PIPI_PIXELS_Y_F) |
---|
139 | : pipi_getpixels(src, PIPI_PIXELS_RGBA_F); |
---|
140 | srcdata = (float *)srcp->pixels; |
---|
141 | |
---|
142 | dst = pipi_new(w, h); |
---|
143 | dstp = gray ? pipi_getpixels(dst, PIPI_PIXELS_Y_F) |
---|
144 | : pipi_getpixels(dst, PIPI_PIXELS_RGBA_F); |
---|
145 | dstdata = (float *)dstp->pixels; |
---|
146 | |
---|
147 | acc = malloc(w * (gray ? 1 : 4) * sizeof(double)); |
---|
148 | |
---|
149 | /* Step 1: fill the accumulator */ |
---|
150 | for(x = 0; x < w; x++) |
---|
151 | { |
---|
152 | if(gray) |
---|
153 | { |
---|
154 | double t = 0.; |
---|
155 | |
---|
156 | for(j = -n; j <= n; j++) |
---|
157 | { |
---|
158 | int j2 = (j < 0) ? h - 1 - ((-j - 1) % h) : j % h; |
---|
159 | t += srcdata[j2 * w + x]; |
---|
160 | } |
---|
161 | |
---|
162 | acc[x] = t; |
---|
163 | } |
---|
164 | else |
---|
165 | { |
---|
166 | double r = 0., g = 0., b = 0., a = 0.; |
---|
167 | |
---|
168 | for(j = -n; j <= n; j++) |
---|
169 | { |
---|
170 | int j2 = (j < 0) ? h - 1 - ((-j - 1) % h) : j % h; |
---|
171 | r += srcdata[4 * (j2 * w + x)]; |
---|
172 | g += srcdata[4 * (j2 * w + x) + 1]; |
---|
173 | b += srcdata[4 * (j2 * w + x) + 2]; |
---|
174 | a += srcdata[4 * (j2 * w + x) + 3]; |
---|
175 | } |
---|
176 | |
---|
177 | acc[4 * x] = r; |
---|
178 | acc[4 * x + 1] = g; |
---|
179 | acc[4 * x + 2] = b; |
---|
180 | acc[4 * x + 3] = a; |
---|
181 | } |
---|
182 | } |
---|
183 | |
---|
184 | /* Step 2: blur the image, line by line */ |
---|
185 | for(y = 0; y < h; y++) |
---|
186 | { |
---|
187 | double r = 0., g = 0., b = 0., a = 0.; |
---|
188 | double t = 0.; |
---|
189 | |
---|
190 | /* 2.1: compute the first pixel */ |
---|
191 | if(gray) |
---|
192 | { |
---|
193 | for(i = -m; i <= m; i++) |
---|
194 | { |
---|
195 | int i2 = (i < 0) ? w - 1 - ((-i - 1) % w) : i % w; |
---|
196 | t += acc[i2]; |
---|
197 | } |
---|
198 | } |
---|
199 | else |
---|
200 | { |
---|
201 | for(i = -m; i <= m; i++) |
---|
202 | { |
---|
203 | int i2 = (i < 0) ? w - 1 - ((-i - 1) % w) : i % w; |
---|
204 | r += acc[4 * i2]; |
---|
205 | g += acc[4 * i2 + 1]; |
---|
206 | b += acc[4 * i2 + 2]; |
---|
207 | a += acc[4 * i2 + 3]; |
---|
208 | } |
---|
209 | } |
---|
210 | |
---|
211 | /* 2.2: iterate on the whole line */ |
---|
212 | for(x = 0; x < w; x++) |
---|
213 | { |
---|
214 | int u, u2, v, v2; |
---|
215 | |
---|
216 | if(gray) |
---|
217 | { |
---|
218 | dstdata[y * w + x] = t / size; |
---|
219 | } |
---|
220 | else |
---|
221 | { |
---|
222 | dstdata[4 * (y * w + x)] = r / size; |
---|
223 | dstdata[4 * (y * w + x) + 1] = g / size; |
---|
224 | dstdata[4 * (y * w + x) + 2] = b / size; |
---|
225 | dstdata[4 * (y * w + x) + 3] = a / size; |
---|
226 | } |
---|
227 | |
---|
228 | u = x - m; |
---|
229 | u2 = (u < 0) ? w - 1 - ((-u - 1) % w) : u % w; |
---|
230 | v = x + m + 1; |
---|
231 | v2 = (v < 0) ? w - 1 - ((-v - 1) % w) : v % w; |
---|
232 | if(gray) |
---|
233 | { |
---|
234 | t = t - acc[u2] + acc[v2]; |
---|
235 | } |
---|
236 | else |
---|
237 | { |
---|
238 | r = r - acc[4 * u2] + acc[4 * v2]; |
---|
239 | g = g - acc[4 * u2 + 1] + acc[4 * v2 + 1]; |
---|
240 | b = b - acc[4 * u2 + 2] + acc[4 * v2 + 2]; |
---|
241 | a = a - acc[4 * u2 + 3] + acc[4 * v2 + 3]; |
---|
242 | } |
---|
243 | } |
---|
244 | |
---|
245 | /* 2.3: update the accumulator */ |
---|
246 | for(x = 0; x < w; x++) |
---|
247 | { |
---|
248 | int u, u2, v, v2; |
---|
249 | |
---|
250 | u = y - n; |
---|
251 | u2 = (u < 0) ? w - 1 - ((-u - 1) % w) : u % w; |
---|
252 | v = y + n + 1; |
---|
253 | v2 = (v < 0) ? w - 1 - ((-v - 1) % w) : v % w; |
---|
254 | if(gray) |
---|
255 | { |
---|
256 | acc[x] += srcdata[v2 * w + x] - srcdata[u2 * w + x]; |
---|
257 | } |
---|
258 | else |
---|
259 | { |
---|
260 | int uoff = 4 * (u2 * w + x); |
---|
261 | int voff = 4 * (v2 * w + x); |
---|
262 | |
---|
263 | acc[4 * x] += srcdata[voff] - srcdata[uoff]; |
---|
264 | acc[4 * x + 1] += srcdata[voff + 1] - srcdata[uoff + 1]; |
---|
265 | acc[4 * x + 2] += srcdata[voff + 2] - srcdata[uoff + 2]; |
---|
266 | acc[4 * x + 3] += srcdata[voff + 3] - srcdata[uoff + 3]; |
---|
267 | } |
---|
268 | } |
---|
269 | } |
---|
270 | |
---|
271 | free(acc); |
---|
272 | |
---|
273 | return dst; |
---|
274 | } |
---|
275 | |
---|