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 | * convolution.c: generic convolution 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 | |
---|
26 | #include "pipi.h" |
---|
27 | #include "pipi_internals.h" |
---|
28 | |
---|
29 | #if !defined TEMPLATE_FILE /* This file uses the template system */ |
---|
30 | #define TEMPLATE_FLAGS SET_FLAG_GRAY | SET_FLAG_WRAP |
---|
31 | #define TEMPLATE_FILE "filter/convolution.c" |
---|
32 | #include "pipi_template.h" |
---|
33 | |
---|
34 | pipi_image_t *pipi_convolution(pipi_image_t *src, int m, int n, double mat[]) |
---|
35 | { |
---|
36 | pipi_image_t *ret; |
---|
37 | double tmp; |
---|
38 | double *hvec, *vvec; |
---|
39 | int i, j, besti = -1, bestj = -1; |
---|
40 | |
---|
41 | /* Find the cell with the largest value */ |
---|
42 | tmp = 0.0; |
---|
43 | for(i = 0; i < m * n; i++) |
---|
44 | if(mat[i] * mat[i] > tmp) |
---|
45 | { |
---|
46 | tmp = mat[i] * mat[i]; |
---|
47 | besti = i % m; |
---|
48 | bestj = i / m; |
---|
49 | } |
---|
50 | |
---|
51 | /* If the kernel is empty, return an empty picture */ |
---|
52 | if(tmp == 0.0) |
---|
53 | return pipi_new(src->w, src->h); |
---|
54 | |
---|
55 | /* Check whether the matrix rank is 1 */ |
---|
56 | for(j = 0; j < n; j++) |
---|
57 | { |
---|
58 | if(j == bestj) |
---|
59 | continue; |
---|
60 | |
---|
61 | for(i = 0; i < m; i++) |
---|
62 | { |
---|
63 | double p, q; |
---|
64 | |
---|
65 | if(i == besti) |
---|
66 | continue; |
---|
67 | |
---|
68 | p = mat[j * m + i] * mat[bestj * m + besti]; |
---|
69 | q = mat[bestj * m + i] * mat[j * m + besti]; |
---|
70 | |
---|
71 | if(fabs(p - q) > 0.0001 * 0.0001) |
---|
72 | { |
---|
73 | if(src->last_modified == PIPI_PIXELS_Y_F32) |
---|
74 | { |
---|
75 | if(src->wrap) |
---|
76 | return conv_gray_wrap(src, m, n, mat); |
---|
77 | return conv_gray(src, m, n, mat); |
---|
78 | } |
---|
79 | else |
---|
80 | { |
---|
81 | if(src->wrap) |
---|
82 | return conv_wrap(src, m, n, mat); |
---|
83 | return conv(src, m, n, mat); |
---|
84 | } |
---|
85 | } |
---|
86 | } |
---|
87 | } |
---|
88 | |
---|
89 | /* Matrix rank is 1! Separate the filter */ |
---|
90 | hvec = malloc(m * sizeof(double)); |
---|
91 | vvec = malloc(n * sizeof(double)); |
---|
92 | |
---|
93 | tmp = sqrt(fabs(mat[bestj * m + besti])); |
---|
94 | for(i = 0; i < m; i++) |
---|
95 | hvec[i] = mat[bestj * m + i] / tmp; |
---|
96 | for(j = 0; j < n; j++) |
---|
97 | vvec[j] = mat[j * m + besti] / tmp; |
---|
98 | |
---|
99 | if(src->last_modified == PIPI_PIXELS_Y_F32) |
---|
100 | ret = src->wrap ? sepconv_gray_wrap(src, m, hvec, n, vvec) |
---|
101 | : sepconv_gray(src, m, hvec, n, vvec); |
---|
102 | else |
---|
103 | ret = src->wrap ? sepconv_wrap(src, m, hvec, n, vvec) |
---|
104 | : sepconv(src, m, hvec, n, vvec); |
---|
105 | |
---|
106 | free(hvec); |
---|
107 | free(vvec); |
---|
108 | |
---|
109 | return ret; |
---|
110 | } |
---|
111 | |
---|
112 | #else /* XXX: the following functions use the template system */ |
---|
113 | |
---|
114 | static pipi_image_t *T(conv)(pipi_image_t *src, int m, int n, double mat[]) |
---|
115 | { |
---|
116 | pipi_image_t *dst; |
---|
117 | pipi_pixels_t *srcp, *dstp; |
---|
118 | float *srcdata, *dstdata; |
---|
119 | int x, y, i, j, w, h; |
---|
120 | |
---|
121 | w = src->w; |
---|
122 | h = src->h; |
---|
123 | |
---|
124 | srcp = FLAG_GRAY ? pipi_get_pixels(src, PIPI_PIXELS_Y_F32) |
---|
125 | : pipi_get_pixels(src, PIPI_PIXELS_RGBA_F32); |
---|
126 | srcdata = (float *)srcp->pixels; |
---|
127 | |
---|
128 | dst = pipi_new(w, h); |
---|
129 | dstp = FLAG_GRAY ? pipi_get_pixels(dst, PIPI_PIXELS_Y_F32) |
---|
130 | : pipi_get_pixels(dst, PIPI_PIXELS_RGBA_F32); |
---|
131 | dstdata = (float *)dstp->pixels; |
---|
132 | |
---|
133 | for(y = 0; y < h; y++) |
---|
134 | { |
---|
135 | for(x = 0; x < w; x++) |
---|
136 | { |
---|
137 | double R = 0., G = 0., B = 0., A = 0.; |
---|
138 | double Y = 0.; |
---|
139 | int x2, y2, off = 4 * (y * w + x); |
---|
140 | |
---|
141 | for(j = 0; j < n; j++) |
---|
142 | { |
---|
143 | y2 = y + j - n / 2; |
---|
144 | if(y2 < 0) y2 = FLAG_WRAP ? h - 1 - ((-y2 - 1) % h) : 0; |
---|
145 | else if(y2 >= h) y2 = FLAG_WRAP ? y2 % h : h - 1; |
---|
146 | |
---|
147 | for(i = 0; i < m; i++) |
---|
148 | { |
---|
149 | double f = mat[j * m + i]; |
---|
150 | |
---|
151 | x2 = x + i - m / 2; |
---|
152 | if(x2 < 0) x2 = FLAG_WRAP ? w - 1 - ((-x2 - 1) % w) : 0; |
---|
153 | else if(x2 >= w) x2 = FLAG_WRAP ? x2 % w : w - 1; |
---|
154 | |
---|
155 | if(FLAG_GRAY) |
---|
156 | Y += f * srcdata[y2 * w + x2]; |
---|
157 | else |
---|
158 | { |
---|
159 | R += f * srcdata[(y2 * w + x2) * 4]; |
---|
160 | G += f * srcdata[(y2 * w + x2) * 4 + 1]; |
---|
161 | B += f * srcdata[(y2 * w + x2) * 4 + 2]; |
---|
162 | A += f * srcdata[(y2 * w + x2) * 4 + 3]; |
---|
163 | } |
---|
164 | } |
---|
165 | } |
---|
166 | |
---|
167 | if(FLAG_GRAY) |
---|
168 | dstdata[y * w + x] = Y < 0.0 ? 0.0 : Y > 1.0 ? 1.0 : Y; |
---|
169 | else |
---|
170 | { |
---|
171 | dstdata[off] = R < 0.0 ? 0.0 : R > 1.0 ? 1.0 : R; |
---|
172 | dstdata[off + 1] = G < 0.0 ? 0.0 : G > 1.0 ? 1.0 : G; |
---|
173 | dstdata[off + 2] = B < 0.0 ? 0.0 : B > 1.0 ? 1.0 : B; |
---|
174 | dstdata[off + 3] = A < 0.0 ? 0.0 : A > 1.0 ? 1.0 : A; |
---|
175 | } |
---|
176 | } |
---|
177 | } |
---|
178 | |
---|
179 | return dst; |
---|
180 | } |
---|
181 | |
---|
182 | static pipi_image_t *T(sepconv)(pipi_image_t *src, |
---|
183 | int m, double hvec[], int n, double vvec[]) |
---|
184 | { |
---|
185 | pipi_image_t *dst; |
---|
186 | pipi_pixels_t *srcp, *dstp; |
---|
187 | float *srcdata, *dstdata; |
---|
188 | double *buffer; |
---|
189 | int x, y, i, j, w, h; |
---|
190 | |
---|
191 | w = src->w; |
---|
192 | h = src->h; |
---|
193 | |
---|
194 | srcp = FLAG_GRAY ? pipi_get_pixels(src, PIPI_PIXELS_Y_F32) |
---|
195 | : pipi_get_pixels(src, PIPI_PIXELS_RGBA_F32); |
---|
196 | srcdata = (float *)srcp->pixels; |
---|
197 | |
---|
198 | dst = pipi_new(w, h); |
---|
199 | dstp = FLAG_GRAY ? pipi_get_pixels(dst, PIPI_PIXELS_Y_F32) |
---|
200 | : pipi_get_pixels(dst, PIPI_PIXELS_RGBA_F32); |
---|
201 | dstdata = (float *)dstp->pixels; |
---|
202 | |
---|
203 | buffer = malloc(w * h * (FLAG_GRAY ? 1 : 4) * sizeof(double)); |
---|
204 | |
---|
205 | for(y = 0; y < h; y++) |
---|
206 | { |
---|
207 | for(x = 0; x < w; x++) |
---|
208 | { |
---|
209 | double R = 0., G = 0., B = 0., A = 0.; |
---|
210 | double Y = 0.; |
---|
211 | int x2, off = 4 * (y * w + x); |
---|
212 | |
---|
213 | for(i = 0; i < m; i++) |
---|
214 | { |
---|
215 | double f = hvec[i]; |
---|
216 | |
---|
217 | x2 = x + i - m / 2; |
---|
218 | if(x2 < 0) x2 = FLAG_WRAP ? w - 1 - ((-x2 - 1) % w) : 0; |
---|
219 | else if(x2 >= w) x2 = FLAG_WRAP ? x2 % w : w - 1; |
---|
220 | |
---|
221 | if(FLAG_GRAY) |
---|
222 | Y += f * srcdata[y * w + x2]; |
---|
223 | else |
---|
224 | { |
---|
225 | R += f * srcdata[(y * w + x2) * 4]; |
---|
226 | G += f * srcdata[(y * w + x2) * 4 + 1]; |
---|
227 | B += f * srcdata[(y * w + x2) * 4 + 2]; |
---|
228 | A += f * srcdata[(y * w + x2) * 4 + 3]; |
---|
229 | } |
---|
230 | } |
---|
231 | |
---|
232 | if(FLAG_GRAY) |
---|
233 | buffer[y * w + x] = Y; |
---|
234 | else |
---|
235 | { |
---|
236 | buffer[off] = R; |
---|
237 | buffer[off + 1] = G; |
---|
238 | buffer[off + 2] = B; |
---|
239 | buffer[off + 3] = A; |
---|
240 | } |
---|
241 | } |
---|
242 | } |
---|
243 | |
---|
244 | for(y = 0; y < h; y++) |
---|
245 | { |
---|
246 | for(x = 0; x < w; x++) |
---|
247 | { |
---|
248 | double R = 0., G = 0., B = 0., A = 0.; |
---|
249 | double Y = 0.; |
---|
250 | int y2, off = 4 * (y * w + x); |
---|
251 | |
---|
252 | for(j = 0; j < n; j++) |
---|
253 | { |
---|
254 | double f = vvec[j]; |
---|
255 | |
---|
256 | y2 = y + j - n / 2; |
---|
257 | if(y2 < 0) y2 = FLAG_WRAP ? h - 1 - ((-y2 - 1) % h) : 0; |
---|
258 | else if(y2 >= h) y2 = FLAG_WRAP ? y2 % h : h - 1; |
---|
259 | |
---|
260 | if(FLAG_GRAY) |
---|
261 | Y += f * buffer[y2 * w + x]; |
---|
262 | else |
---|
263 | { |
---|
264 | R += f * buffer[(y2 * w + x) * 4]; |
---|
265 | G += f * buffer[(y2 * w + x) * 4 + 1]; |
---|
266 | B += f * buffer[(y2 * w + x) * 4 + 2]; |
---|
267 | A += f * buffer[(y2 * w + x) * 4 + 3]; |
---|
268 | } |
---|
269 | } |
---|
270 | |
---|
271 | if(FLAG_GRAY) |
---|
272 | dstdata[y * w + x] = Y < 0.0 ? 0.0 : Y > 1.0 ? 1.0 : Y; |
---|
273 | else |
---|
274 | { |
---|
275 | dstdata[off] = R < 0.0 ? 0.0 : R > 1.0 ? 1.0 : R; |
---|
276 | dstdata[off + 1] = G < 0.0 ? 0.0 : G > 1.0 ? 1.0 : G; |
---|
277 | dstdata[off + 2] = B < 0.0 ? 0.0 : B > 1.0 ? 1.0 : B; |
---|
278 | dstdata[off + 3] = A < 0.0 ? 0.0 : A > 1.0 ? 1.0 : A; |
---|
279 | } |
---|
280 | } |
---|
281 | } |
---|
282 | |
---|
283 | free(buffer); |
---|
284 | |
---|
285 | return dst; |
---|
286 | } |
---|
287 | |
---|
288 | #endif |
---|
289 | |
---|