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 | * convolution_template.h: convolution templates |
---|
17 | * required macros: |
---|
18 | * FUNC1 standard function name |
---|
19 | * FUNC2 separable function name |
---|
20 | * PIXEL pixel type |
---|
21 | * GRAY 1 (grayscale) or 0 (classic RGBA) |
---|
22 | */ |
---|
23 | |
---|
24 | static pipi_image_t *FUNC1(pipi_image_t *src, int m, int n, double mat[]) |
---|
25 | { |
---|
26 | pipi_image_t *dst; |
---|
27 | pipi_pixels_t *srcp, *dstp; |
---|
28 | PIXEL *srcdata, *dstdata; |
---|
29 | int x, y, i, j, w, h; |
---|
30 | |
---|
31 | w = src->w; |
---|
32 | h = src->h; |
---|
33 | |
---|
34 | srcp = GRAY ? pipi_getpixels(src, PIPI_PIXELS_Y_F) |
---|
35 | : pipi_getpixels(src, PIPI_PIXELS_RGBA_F); |
---|
36 | srcdata = (PIXEL *)srcp->pixels; |
---|
37 | |
---|
38 | dst = pipi_new(w, h); |
---|
39 | dstp = GRAY ? pipi_getpixels(dst, PIPI_PIXELS_Y_F) |
---|
40 | : pipi_getpixels(dst, PIPI_PIXELS_RGBA_F); |
---|
41 | dstdata = (PIXEL *)dstp->pixels; |
---|
42 | |
---|
43 | for(y = 0; y < h; y++) |
---|
44 | { |
---|
45 | for(x = 0; x < w; x++) |
---|
46 | { |
---|
47 | if(GRAY) |
---|
48 | { |
---|
49 | double Y = 0.; |
---|
50 | int x2, y2; |
---|
51 | |
---|
52 | for(j = 0; j < n; j++) |
---|
53 | { |
---|
54 | y2 = y + j - n / 2; |
---|
55 | if(y2 < 0) y2 = 0; |
---|
56 | else if(y2 >= h) y2 = h - 1; |
---|
57 | |
---|
58 | for(i = 0; i < m; i++) |
---|
59 | { |
---|
60 | x2 = x + i - m / 2; |
---|
61 | if(x2 < 0) x2 = 0; |
---|
62 | else if(x2 >= w) x2 = w - 1; |
---|
63 | |
---|
64 | Y += mat[j * m + i] * srcdata[y2 * w + x2]; |
---|
65 | } |
---|
66 | } |
---|
67 | |
---|
68 | dstdata[y * w + x] = Y; |
---|
69 | } |
---|
70 | else |
---|
71 | { |
---|
72 | double R = 0., G = 0., B = 0.; |
---|
73 | int x2, y2, off = 4 * (y * w + x); |
---|
74 | |
---|
75 | for(j = 0; j < n; j++) |
---|
76 | { |
---|
77 | y2 = y + j - n / 2; |
---|
78 | if(y2 < 0) y2 = 0; |
---|
79 | else if(y2 >= h) y2 = h - 1; |
---|
80 | |
---|
81 | for(i = 0; i < m; i++) |
---|
82 | { |
---|
83 | double f = mat[j * m + i]; |
---|
84 | |
---|
85 | x2 = x + i - m / 2; |
---|
86 | if(x2 < 0) x2 = 0; |
---|
87 | else if(x2 >= w) x2 = w - 1; |
---|
88 | |
---|
89 | R += f * srcdata[(y2 * w + x2) * 4]; |
---|
90 | G += f * srcdata[(y2 * w + x2) * 4 + 1]; |
---|
91 | B += f * srcdata[(y2 * w + x2) * 4 + 2]; |
---|
92 | } |
---|
93 | } |
---|
94 | |
---|
95 | dstdata[off] = R; |
---|
96 | dstdata[off + 1] = G; |
---|
97 | dstdata[off + 2] = B; |
---|
98 | } |
---|
99 | } |
---|
100 | } |
---|
101 | |
---|
102 | return dst; |
---|
103 | } |
---|
104 | |
---|
105 | static pipi_image_t *FUNC2(pipi_image_t *src, |
---|
106 | int m, double hvec[], int n, double vvec[]) |
---|
107 | { |
---|
108 | pipi_image_t *dst; |
---|
109 | pipi_pixels_t *srcp, *dstp; |
---|
110 | PIXEL *srcdata, *dstdata; |
---|
111 | double *buffer; |
---|
112 | int x, y, i, j, w, h; |
---|
113 | |
---|
114 | w = src->w; |
---|
115 | h = src->h; |
---|
116 | |
---|
117 | srcp = GRAY ? pipi_getpixels(src, PIPI_PIXELS_Y_F) |
---|
118 | : pipi_getpixels(src, PIPI_PIXELS_RGBA_F); |
---|
119 | srcdata = (PIXEL *)srcp->pixels; |
---|
120 | |
---|
121 | dst = pipi_new(w, h); |
---|
122 | dstp = GRAY ? pipi_getpixels(dst, PIPI_PIXELS_Y_F) |
---|
123 | : pipi_getpixels(dst, PIPI_PIXELS_RGBA_F); |
---|
124 | dstdata = (PIXEL *)dstp->pixels; |
---|
125 | |
---|
126 | buffer = malloc(w * h * (GRAY ? 1 : 4) * sizeof(double)); |
---|
127 | |
---|
128 | for(y = 0; y < h; y++) |
---|
129 | { |
---|
130 | for(x = 0; x < w; x++) |
---|
131 | { |
---|
132 | if(GRAY) |
---|
133 | { |
---|
134 | double Y = 0.; |
---|
135 | int x2; |
---|
136 | |
---|
137 | for(i = 0; i < m; i++) |
---|
138 | { |
---|
139 | x2 = x + i - m / 2; |
---|
140 | if(x2 < 0) x2 = 0; |
---|
141 | else if(x2 >= w) x2 = w - 1; |
---|
142 | |
---|
143 | Y += hvec[i] * srcdata[y * w + x2]; |
---|
144 | } |
---|
145 | |
---|
146 | buffer[y * w + x] = Y; |
---|
147 | } |
---|
148 | else |
---|
149 | { |
---|
150 | double R = 0., G = 0., B = 0.; |
---|
151 | int x2, off = 4 * (y * w + x); |
---|
152 | |
---|
153 | for(i = 0; i < m; i++) |
---|
154 | { |
---|
155 | double f = hvec[i]; |
---|
156 | |
---|
157 | x2 = x + i - m / 2; |
---|
158 | if(x2 < 0) x2 = 0; |
---|
159 | else if(x2 >= w) x2 = w - 1; |
---|
160 | |
---|
161 | R += f * srcdata[(y * w + x2) * 4]; |
---|
162 | G += f * srcdata[(y * w + x2) * 4 + 1]; |
---|
163 | B += f * srcdata[(y * w + x2) * 4 + 2]; |
---|
164 | } |
---|
165 | |
---|
166 | buffer[off] = R; |
---|
167 | buffer[off + 1] = G; |
---|
168 | buffer[off + 2] = B; |
---|
169 | } |
---|
170 | } |
---|
171 | } |
---|
172 | |
---|
173 | for(y = 0; y < h; y++) |
---|
174 | { |
---|
175 | for(x = 0; x < w; x++) |
---|
176 | { |
---|
177 | if(GRAY) |
---|
178 | { |
---|
179 | double Y = 0.; |
---|
180 | int y2; |
---|
181 | |
---|
182 | for(j = 0; j < n; j++) |
---|
183 | { |
---|
184 | y2 = y + j - n / 2; |
---|
185 | if(y2 < 0) y2 = 0; |
---|
186 | else if(y2 >= h) y2 = h - 1; |
---|
187 | |
---|
188 | Y += vvec[j] * buffer[y2 * w + x]; |
---|
189 | } |
---|
190 | |
---|
191 | dstdata[y * w + x] = Y; |
---|
192 | } |
---|
193 | else |
---|
194 | { |
---|
195 | double R = 0., G = 0., B = 0.; |
---|
196 | int y2, off = 4 * (y * w + x); |
---|
197 | |
---|
198 | for(j = 0; j < n; j++) |
---|
199 | { |
---|
200 | double f = vvec[j]; |
---|
201 | |
---|
202 | y2 = y + j - n / 2; |
---|
203 | if(y2 < 0) y2 = 0; |
---|
204 | else if(y2 >= h) y2 = h - 1; |
---|
205 | |
---|
206 | R += f * buffer[(y2 * w + x) * 4]; |
---|
207 | G += f * buffer[(y2 * w + x) * 4 + 1]; |
---|
208 | B += f * buffer[(y2 * w + x) * 4 + 2]; |
---|
209 | } |
---|
210 | |
---|
211 | dstdata[off] = R; |
---|
212 | dstdata[off + 1] = G; |
---|
213 | dstdata[off + 2] = B; |
---|
214 | } |
---|
215 | } |
---|
216 | } |
---|
217 | |
---|
218 | free(buffer); |
---|
219 | |
---|
220 | return dst; |
---|
221 | } |
---|
222 | |
---|