0
|
1 #include "wm.h"
|
|
2 #include "dwt.h"
|
|
3
|
|
4 char filter_file[MAXPATHLEN] = "";
|
|
5 AllFilters dwt_allfilters;
|
|
6 FilterGH *dwt_filters = NULL;
|
|
7 int dwt_method;
|
|
8 int dwt_cols;
|
|
9 int dwt_rows;
|
|
10 int dwt_levels;
|
|
11 int dwt_filter;
|
|
12
|
|
13 void init_dwt(int cols, int rows, const char *filter_name, int filter, int level, int method) {
|
|
14 int i;
|
|
15
|
|
16 if (strcmp(filter_file, filter_name)) {
|
|
17 if (filter_name)
|
|
18 strcpy(filter_file, filter_name);
|
|
19 else
|
|
20 strcpy(filter_file, "filter.dat");
|
3
|
21
|
0
|
22 /* memory leak here - there is no function unload_filters() */
|
|
23 dwt_allfilters = load_filters(filter_file);
|
3
|
24
|
0
|
25 if (!dwt_allfilters) {
|
|
26 fprintf(stderr, "init_dwt(): unable to open filter definition file %s\n", filter_file);
|
|
27 return;
|
|
28 }
|
|
29 }
|
|
30
|
|
31 #ifdef DEBUG
|
|
32 if (level <= 0 || level > rint(log(MIN(cols, rows))/log(2.0)) - 2) {
|
|
33 fprintf(stderr, "init_dwt(): level parameter does not match image width/height\n");
|
|
34 return;
|
|
35 }
|
|
36 #endif
|
|
37
|
|
38 if (dwt_filters && level != dwt_levels) {
|
|
39 free(dwt_filters);
|
|
40 dwt_filters = NULL;
|
|
41 }
|
|
42
|
|
43 dwt_levels = level;
|
|
44
|
|
45 if (!dwt_filters)
|
|
46 dwt_filters = calloc(level + 1, sizeof(FilterGH));
|
|
47
|
|
48 for (i = 0; i < level + 1; i++)
|
|
49 dwt_filters[i] = (dwt_allfilters->filter)[filter];
|
|
50
|
|
51 dwt_filter = filter;
|
|
52 dwt_method = method;
|
|
53 dwt_cols = cols;
|
|
54 dwt_rows = rows;
|
|
55 }
|
|
56
|
|
57 Image_tree fdwt(gray **pixels) {
|
|
58 Image image;
|
|
59 Image_tree tree;
|
|
60 int i, j;
|
|
61
|
|
62 image = new_image(dwt_cols, dwt_rows);
|
|
63
|
|
64 for (i = 0; i < dwt_rows; i++)
|
|
65 for (j = 0; j < dwt_cols; j++)
|
|
66 set_pixel(image, j, i, pixels[i][j]);
|
|
67
|
|
68 tree = wavelettransform(image, dwt_levels, dwt_filters, dwt_method);
|
|
69 free_image(image);
|
|
70
|
|
71 return tree;
|
|
72 }
|
|
73
|
|
74 Image_tree fdwt_wp(gray **pixels) {
|
|
75 Image image;
|
|
76 Image_tree tree;
|
|
77 int i, j;
|
|
78
|
|
79 image = new_image(dwt_cols, dwt_rows);
|
|
80
|
|
81 for (i = 0; i < dwt_rows; i++)
|
|
82 for (j = 0; j < dwt_cols; j++)
|
|
83 set_pixel(image, j, i, pixels[i][j]);
|
|
84
|
|
85 tree = wavelettransform_wp(image, dwt_levels, dwt_filters, dwt_method);
|
|
86 free_image(image);
|
|
87
|
|
88 return tree;
|
|
89 }
|
|
90
|
|
91 void idwt(Image_tree dwts, gray **pixels) {
|
|
92 Image image;
|
|
93 int i, j;
|
|
94
|
|
95 image = inv_transform(dwts, dwt_filters, dwt_method + 1);
|
|
96
|
|
97 for (i = 0; i < dwt_rows; i++)
|
|
98 for (j = 0; j < dwt_cols; j++)
|
|
99 pixels[i][j] = PIXELRANGE((int) (get_pixel(image, j, i) + 0.5));
|
|
100
|
|
101 free_image(image);
|
|
102 }
|
|
103
|
|
104 void idwt_wp(Image_tree dwts, gray **pixels) {
|
|
105 Image image;
|
|
106 int i, j;
|
|
107
|
|
108 image = inv_transform(dwts, dwt_filters, dwt_method + 1);
|
|
109
|
|
110 for (i = 0; i < dwt_rows; i++)
|
|
111 for (j = 0; j < dwt_cols; j++)
|
|
112 pixels[i][j] = PIXELRANGE((int) (get_pixel(image, j, i) + 0.5));
|
|
113
|
|
114 free_image(image);
|
|
115 }
|
|
116
|
|
117 int gen_pollen_filter(double *filter, double alpha, double beta, int which) {
|
|
118 int i, j, k, filterlength;
|
|
119 double tf[6];
|
|
120
|
|
121 /* parameter alpha, beta have to be in range -Pi .. Pi */
|
|
122 if (alpha < -M_PI || alpha >= M_PI) {
|
|
123 fprintf(stderr, "alpha %f out of range\n", alpha);
|
|
124 return -1;
|
|
125 }
|
|
126
|
|
127 if (beta < -M_PI || beta >= M_PI) {
|
|
128 fprintf(stderr, "beta %f out of range\n", beta);
|
|
129 return -1;
|
|
130 }
|
|
131
|
|
132 /* generate Pollen filter coefficients, see http://www.dfw.net/~cody for details */
|
|
133 tf[0] = ((1.0 + cos(alpha) + sin(alpha)) * (1.0 - cos(beta) - sin(beta)) + 2.0 * sin(beta) * cos(alpha)) / 4.0;
|
|
134 tf[1] = ((1.0 - cos(alpha) + sin(alpha)) * (1.0 + cos(beta) - sin(beta)) - 2.0 * sin(beta) * cos(alpha)) / 4.0;
|
|
135 tf[2] = (1.0 + cos(alpha - beta) + sin(alpha - beta)) / 2.0;
|
|
136 tf[3] = (1.0 + cos(alpha - beta) - sin(alpha - beta)) / 2.0;
|
|
137 tf[4] = 1.0 - tf[0] - tf[2];
|
|
138 tf[5] = 1.0 - tf[1] - tf[3];
|
|
139
|
|
140 /* set close-to-zero filter coefficients to zero */
|
|
141 for (i = 0; i < 6; i++)
|
|
142 if (fabs(tf[i]) < 1.0e-15) tf[i] = 0.0;
|
|
143
|
|
144 /* find the first non-zero wavelet coefficient */
|
|
145 i = 0;
|
|
146 while (tf[i] == 0.0) i++;
|
|
147
|
|
148 /* find the last non-zero wavelet coefficient */
|
|
149 j = 5;
|
|
150 while (tf[j] == 0.0) j--;
|
|
151
|
|
152 filterlength = j - i + 1;
|
|
153 for (k = 0; k < filterlength; k++)
|
|
154 switch (which) {
|
|
155 case FILTERH:
|
|
156 filter[k] = tf[j--] / 2.0;
|
|
157 break;
|
|
158 case FILTERG:
|
|
159 filter[k] = (double) (((i & 0x01) * 2) - 1) * tf[i++] / 2.0;
|
|
160 break;
|
|
161 case FILTERHi:
|
|
162 filter[k] = tf[j--];
|
|
163 break;
|
|
164 case FILTERGi:
|
|
165 filter[k] = (double) (((i & 0x01) * 2) - 1) * tf[i++];
|
|
166 break;
|
|
167 default:
|
|
168 return -1;
|
|
169 }
|
|
170
|
|
171 while (k < 6)
|
|
172 filter[k++] = 0.0;
|
|
173
|
|
174 return filterlength;
|
|
175 }
|
|
176
|
|
177 void dwt_pollen_filter(double alpha, double beta) {
|
|
178 FilterGH filter;
|
|
179 int i;
|
|
180
|
|
181 filter = malloc(sizeof(struct FilterGHStruct));
|
|
182 #ifdef DEBUG
|
|
183 if (!filter) {
|
|
184 fprintf(stderr, "dwt_pollen_filter(): malloc failed()\n");
|
|
185 return;
|
|
186 }
|
|
187 #endif
|
|
188
|
|
189 filter->type = FTOther;
|
|
190 filter->name = "pollen";
|
|
191
|
|
192 filter->g = new_filter(6);
|
|
193 filter->g->type = FTSymm;
|
|
194 filter->g->hipass = 1;
|
|
195 filter->g->len = gen_pollen_filter(filter->g->data, alpha, beta, FILTERG);
|
|
196 filter->g->start = -filter->g->len / 2;
|
|
197 filter->g->end = filter->g->len / 2 - 1;
|
|
198
|
|
199 filter->h = new_filter(6);
|
|
200 filter->h->type = FTSymm;
|
|
201 filter->h->hipass = 0;
|
|
202 filter->h->len = gen_pollen_filter(filter->h->data, alpha, beta, FILTERH);
|
|
203 filter->h->start = -filter->h->len / 2;
|
|
204 filter->h->end = filter->h->len / 2 - 1;
|
|
205
|
|
206 filter->gi = new_filter(6);
|
|
207 filter->gi->type = FTSymm;
|
|
208 filter->gi->hipass = 1;
|
|
209 filter->gi->len = gen_pollen_filter(filter->gi->data, alpha, beta, FILTERGi);
|
|
210 filter->gi->start = -filter->gi->len / 2;
|
|
211 filter->gi->end = filter->gi->len / 2 - 1;
|
|
212
|
|
213 filter->hi = new_filter(6);
|
|
214 filter->hi->type = FTSymm;
|
|
215 filter->hi->hipass = 0;
|
|
216 filter->hi->len = gen_pollen_filter(filter->hi->data, alpha, beta, FILTERHi);
|
|
217 filter->hi->start = -filter->hi->len / 2;
|
|
218 filter->hi->end = filter->hi->len / 2 - 1;
|
|
219
|
|
220 #ifdef DEBUG
|
|
221 if (dwt_levels <= 0) {
|
|
222 fprintf(stderr, "dwt_pollen_filter(): level invalid - set to zero\n");
|
|
223 return;
|
|
224 }
|
|
225 #endif
|
|
226
|
|
227 #ifdef DEBUG
|
|
228 if (!dwt_filters) {
|
|
229 fprintf(stderr, "dwt_pollen_filter(): wm_dwt not initialized, call init_dwt() first\n");
|
|
230 return;
|
|
231 }
|
|
232 #endif
|
|
233
|
|
234 for (i = 0; i < dwt_levels + 1; i++)
|
|
235 dwt_filters[i] = filter;
|
|
236 }
|
|
237
|
|
238 int gen_param_filter(double *filter, int n, double alpha[], int which) {
|
|
239 int i, j, k, filterlength;
|
|
240 double *tf, *t;
|
|
241
|
|
242 tf = malloc(2 * (n + 1) * sizeof(double));
|
|
243 t = malloc(2 * (n + 1) * sizeof(double));
|
|
244 if (!tf) {
|
|
245 fprintf(stderr, "gen_param_filter(): malloc() failed\n");
|
|
246 return -1;
|
|
247 }
|
|
248
|
|
249 tf[0] = 1.0 / sqrt(2.0);
|
|
250 tf[1] = 1.0 / sqrt(2.0);
|
|
251
|
|
252 for (k = 0; k < n; k++) {
|
|
253 for (i = 0; i < 2 * (k + 2); i++) {
|
|
254
|
|
255 #define H(X) (((X) < 0 || (X) >= 2 * (k + 1)) ? 0.0 : tf[X])
|
|
256
|
|
257 t[i] = 0.5 * (H(i - 2) + H(i) +
|
|
258 cos(alpha[k]) * (H(i - 2) - H(i)) +
|
|
259 (i & 1 ? -1.0 : 1.0) * sin(alpha[k]) * (H(2 * (k + 2) - i - 1) - H(2 * (k + 2) - i - 3)));
|
|
260 }
|
|
261 for (i = 0; i < 2 * (k + 2); i++) tf[i] = t[i];
|
|
262 }
|
|
263
|
|
264 /* set close-to-zero filter coefficients to zero */
|
|
265 for (i = 0; i < 2 * (n + 1) ; i++)
|
|
266 if (fabs(tf[i]) < 1.0e-15) tf[i] = 0.0;
|
|
267
|
|
268 /* find the first non-zero wavelet coefficient */
|
|
269 i = 0;
|
|
270 while (tf[i] == 0.0) i++;
|
|
271
|
|
272 /* find the last non-zero wavelet coefficient */
|
|
273 j = 2 * (n + 1) - 1;
|
|
274 while (tf[j] == 0.0) j--;
|
|
275
|
|
276 filterlength = j - i + 1;
|
|
277 for (k = 0; k < filterlength; k++)
|
|
278 switch (which) {
|
|
279 case FILTERG:
|
|
280 case FILTERGi:
|
8
|
281 filter[k] = (double) ((((i+1) & 0x01) * 2) - 1) * tf[i++];
|
0
|
282 break;
|
|
283 case FILTERH:
|
|
284 case FILTERHi:
|
|
285 filter[k] = tf[j--];
|
|
286 break;
|
|
287 default:
|
|
288 return -1;
|
|
289 }
|
|
290
|
|
291 while (k < 2 * (n + 1))
|
|
292 filter[k++] = 0.0;
|
|
293
|
|
294 return filterlength;
|
|
295 }
|
|
296
|
3
|
297 void dwt_param_filter(double alpha[], int param_len[]) {
|
0
|
298 FilterGH filter;
|
|
299 int i;
|
3
|
300 int param_len_sum = 0;
|
0
|
301
|
|
302 #ifdef DEBUG
|
|
303 if (dwt_levels <= 0) {
|
3
|
304 fprintf(stderr, "dwt_param_filter(): level invalid - set to zero\n");
|
0
|
305 return;
|
|
306 }
|
|
307 #endif
|
|
308
|
|
309 #ifdef DEBUG
|
|
310 if (!dwt_filters) {
|
3
|
311 fprintf(stderr, "dwt_param_filter(): wm_dwt not initialized, call init_dwt() first\n");
|
0
|
312 return;
|
|
313 }
|
|
314 #endif
|
|
315
|
3
|
316
|
|
317 for (i = 0; i < dwt_levels + 1; i++) {
|
|
318
|
|
319 filter = malloc(sizeof(struct FilterGHStruct));
|
|
320 #ifdef DEBUG
|
|
321 if (!filter) {
|
|
322 fprintf(stderr, "dwt_param_filter(): malloc failed()\n");
|
|
323 return;
|
|
324 }
|
|
325 #endif
|
|
326
|
|
327 filter->type = FTOrtho;
|
|
328 filter->name = "param";
|
|
329
|
|
330 filter->g = new_filter(2 * (param_len[i] + 1));
|
|
331 filter->g->type = FTSymm;
|
|
332 filter->g->hipass = 1;
|
|
333 filter->g->len = gen_param_filter(filter->g->data,
|
|
334 param_len[i], &alpha[param_len_sum],
|
|
335 FILTERG);
|
|
336 filter->g->start = -filter->g->len / 2;
|
|
337 filter->g->end = filter->g->len / 2 - 1;
|
|
338
|
|
339 filter->h = new_filter(2 * (param_len[i] + 1));
|
|
340 filter->h->type = FTSymm;
|
|
341 filter->h->hipass = 0;
|
|
342 filter->h->len = gen_param_filter(filter->h->data,
|
|
343 param_len[i], &alpha[param_len_sum],
|
|
344 FILTERH);
|
|
345 filter->h->start = -filter->h->len / 2;
|
|
346 filter->h->end = filter->h->len / 2 - 1;
|
|
347
|
|
348 filter->gi = 0;
|
|
349 filter->hi = 0;
|
|
350
|
0
|
351 dwt_filters[i] = filter;
|
3
|
352
|
|
353 param_len_sum += param_len[i];
|
|
354 }
|
0
|
355 }
|
|
356
|
|
357 void done_dwt() {
|
|
358 }
|