tests: fix build on os/x
[schroedinger.git] / testsuite / wavelet_2d.c
blob62cd533bd2df305cb7c91921fb7000744d9fca46
2 #ifdef HAVE_CONFIG_H
3 #include "config.h"
4 #endif
6 #include <schroedinger/schro.h>
7 #include <schroedinger/schrowavelet.h>
8 #include <schroedinger/schroorc.h>
10 #include <stdio.h>
11 #include <string.h>
12 #include <stdlib.h>
14 #include "common.h"
16 int filtershift[] = { 1, 1, 1, 0, 1, 0, 1 };
18 int fail = 0;
19 int verbose = 1;
21 void iwt_ref(SchroFrameData *p, int filter);
22 void iiwt_ref(SchroFrameData *p, int filter);
23 void iwt_test(SchroFrameData *p, int filter);
24 void iiwt_test(SchroFrameData *p, int filter);
27 void
28 fwd_test (int filter, int width, int height)
30 int i;
31 SchroFrame *test;
32 SchroFrame *ref;
33 SchroFrame *orig;
34 SchroFrameData *fd_test;
35 SchroFrameData *fd_ref;
36 SchroFrameData *fd_orig;
37 char name[TEST_PATTERN_NAME_SIZE] = { 0 };
39 test = schro_frame_new_and_alloc (NULL, SCHRO_FRAME_FORMAT_S16_444,
40 width, height);
41 fd_test = test->components + 0;
42 orig = schro_frame_new_and_alloc (NULL, SCHRO_FRAME_FORMAT_S16_444,
43 width, height);
44 fd_orig = orig->components + 0;
45 ref = schro_frame_new_and_alloc (NULL, SCHRO_FRAME_FORMAT_S16_444,
46 width, height);
47 fd_ref = ref->components + 0;
49 for(i=0;i<test_pattern_get_n_generators();i++){
50 test_pattern_generate (fd_orig, name, i);
51 printf(" forward test \"%s\":\n", name);
52 fflush(stdout);
54 schro_frame_convert (ref, orig);
55 schro_frame_convert (test, orig);
56 iwt_ref(fd_ref,filter);
57 iwt_test(fd_test,filter);
58 if (!frame_data_compare(fd_test, fd_ref)) {
59 printf(" failed\n");
60 if (verbose) frame_data_dump_full (fd_test, fd_ref, fd_orig);
61 fail = TRUE;
64 schro_frame_unref (orig);
65 schro_frame_unref (test);
66 schro_frame_unref (ref);
69 void
70 inv_test (int filter, int width, int height)
72 int i;
73 SchroFrame *test;
74 SchroFrame *orig;
75 SchroFrame *ref;
76 SchroFrameData *fd_test;
77 SchroFrameData *fd_orig;
78 SchroFrameData *fd_ref;
79 char name[TEST_PATTERN_NAME_SIZE] = { 0 };
81 orig = schro_frame_new_and_alloc (NULL, SCHRO_FRAME_FORMAT_S16_444,
82 width, height);
83 fd_orig = orig->components + 0;
84 test = schro_frame_new_and_alloc (NULL, SCHRO_FRAME_FORMAT_S16_444,
85 width, height);
86 fd_test = test->components + 0;
87 ref = schro_frame_new_and_alloc (NULL, SCHRO_FRAME_FORMAT_S16_444,
88 width, height);
89 fd_ref = ref->components + 0;
91 for(i=0;i<test_pattern_get_n_generators();i++){
92 test_pattern_generate (fd_orig, name, i);
93 printf(" reverse test \"%s\":\n", name);
94 fflush(stdout);
96 iwt_ref(fd_orig,filter);
97 schro_frame_convert (test, orig);
98 schro_frame_convert (ref, orig);
99 iiwt_ref(fd_ref,filter);
100 iiwt_test(fd_test,filter);
101 if (!frame_data_compare(fd_test, fd_ref)) {
102 printf(" failed\n");
103 if (verbose) frame_data_dump_full (fd_test, fd_ref, fd_orig);
104 fail = TRUE;
107 schro_frame_unref (orig);
108 schro_frame_unref (test);
109 schro_frame_unref (ref);
112 void
113 fwd_random_test (int filter, int width, int height)
115 SchroFrame *test;
116 SchroFrame *orig;
117 SchroFrame *ref;
118 SchroFrameData *fd_orig;
119 SchroFrameData *fd_test;
120 SchroFrameData *fd_ref;
121 char name[TEST_PATTERN_NAME_SIZE] = { 0 };
123 orig = schro_frame_new_and_alloc (NULL, SCHRO_FRAME_FORMAT_S16_444,
124 width, height);
125 fd_orig = orig->components + 0;
126 test = schro_frame_new_and_alloc (NULL, SCHRO_FRAME_FORMAT_S16_444,
127 width, height);
128 fd_test = test->components + 0;
129 ref = schro_frame_new_and_alloc (NULL, SCHRO_FRAME_FORMAT_S16_444,
130 width, height);
131 fd_ref = ref->components + 0;
133 test_pattern_generate (fd_orig, name, 0);
134 printf(" forward test \"%s\":\n", name);
135 fflush(stdout);
137 schro_frame_convert (ref, orig);
138 schro_frame_convert (test, orig);
139 iwt_ref(fd_ref,filter);
140 iwt_test(fd_test,filter);
141 if (!frame_data_compare(fd_test, fd_ref)) {
142 printf(" failed\n");
143 if (verbose) frame_data_dump_full (fd_test, fd_ref, fd_orig);
144 fail = TRUE;
147 schro_frame_unref (orig);
148 schro_frame_unref (test);
149 schro_frame_unref (ref);
152 void
153 inv_random_test (int filter, int width, int height)
155 SchroFrame *test;
156 SchroFrame *orig;
157 SchroFrame *ref;
158 SchroFrameData *fd_test;
159 SchroFrameData *fd_orig;
160 SchroFrameData *fd_ref;
161 char name[TEST_PATTERN_NAME_SIZE] = { 0 };
163 orig = schro_frame_new_and_alloc (NULL, SCHRO_FRAME_FORMAT_S16_444,
164 width, height);
165 fd_orig = orig->components + 0;
166 test = schro_frame_new_and_alloc (NULL, SCHRO_FRAME_FORMAT_S16_444,
167 width, height);
168 fd_test = test->components + 0;
169 ref = schro_frame_new_and_alloc (NULL, SCHRO_FRAME_FORMAT_S16_444,
170 width, height);
171 fd_ref = ref->components + 0;
173 test_pattern_generate (fd_orig, name, 0);
174 printf(" reverse test \"%s\":\n", name);
175 fflush(stdout);
177 iwt_ref(fd_orig,filter);
178 schro_frame_convert (test, orig);
179 schro_frame_convert (ref, orig);
180 iiwt_ref(fd_ref,filter);
181 iiwt_test(fd_test,filter);
182 if (!frame_data_compare(fd_test, fd_ref)) {
183 printf(" failed\n");
184 if (verbose) frame_data_dump_full (fd_test, fd_ref, fd_orig);
185 fail = TRUE;
187 schro_frame_unref (orig);
188 schro_frame_unref (test);
189 schro_frame_unref (ref);
192 void
193 fwd_random_test_s32 (int filter, int width, int height)
195 SchroFrame *test;
196 SchroFrame *orig;
197 SchroFrame *ref;
198 SchroFrameData *fd_orig;
199 SchroFrameData *fd_test;
200 SchroFrameData *fd_ref;
201 char name[TEST_PATTERN_NAME_SIZE] = { 0 };
203 orig = schro_frame_new_and_alloc (NULL, SCHRO_FRAME_FORMAT_S16_444,
204 width, height);
205 fd_orig = orig->components + 0;
206 test = schro_frame_new_and_alloc (NULL, SCHRO_FRAME_FORMAT_S32_444,
207 width, height);
208 fd_test = test->components + 0;
209 ref = schro_frame_new_and_alloc (NULL, SCHRO_FRAME_FORMAT_S16_444,
210 width, height);
211 fd_ref = ref->components + 0;
213 test_pattern_generate (fd_orig, name, 0);
214 printf(" forward test 32-bit \"%s\":\n", name);
215 fflush(stdout);
217 schro_frame_convert (ref, orig);
218 schro_frame_convert (test, orig);
219 iwt_ref(fd_ref,filter);
220 iwt_test(fd_test,filter);
221 if (!frame_data_compare(fd_test, fd_ref)) {
222 printf(" failed\n");
223 if (verbose) frame_data_dump_full (fd_test, fd_ref, fd_orig);
224 fail = TRUE;
227 schro_frame_unref (orig);
228 schro_frame_unref (test);
229 schro_frame_unref (ref);
232 void
233 inv_random_test_s32 (int filter, int width, int height)
235 SchroFrame *test;
236 SchroFrame *orig;
237 SchroFrame *ref;
238 SchroFrameData *fd_test;
239 SchroFrameData *fd_orig;
240 SchroFrameData *fd_ref;
241 char name[TEST_PATTERN_NAME_SIZE] = { 0 };
243 orig = schro_frame_new_and_alloc (NULL, SCHRO_FRAME_FORMAT_S16_444,
244 width, height);
245 fd_orig = orig->components + 0;
246 test = schro_frame_new_and_alloc (NULL, SCHRO_FRAME_FORMAT_S32_444,
247 width, height);
248 fd_test = test->components + 0;
249 ref = schro_frame_new_and_alloc (NULL, SCHRO_FRAME_FORMAT_S16_444,
250 width, height);
251 fd_ref = ref->components + 0;
253 test_pattern_generate (fd_orig, name, 0);
254 printf(" reverse test 32-bit \"%s\":\n", name);
255 fflush(stdout);
257 iwt_ref(fd_orig,filter);
258 schro_frame_convert (test, orig);
259 schro_frame_convert (ref, orig);
260 iiwt_ref(fd_ref,filter);
261 iiwt_test(fd_test,filter);
262 if (!frame_data_compare(fd_test, fd_ref)) {
263 printf(" failed\n");
264 if (verbose) frame_data_dump_full (fd_test, fd_ref, fd_orig);
265 fail = TRUE;
267 schro_frame_unref (orig);
268 schro_frame_unref (test);
269 schro_frame_unref (ref);
274 main (int argc, char *argv[])
276 int filter;
277 int width;
278 int height;
280 schro_init();
282 for(filter=0;filter<=SCHRO_WAVELET_DAUBECHIES_9_7;filter++){
283 printf("Filter %d:\n", filter);
284 fwd_test(filter, 20, 20);
285 inv_test(filter, 20, 20);
288 for(width = 2; width <= 40; width+=2) {
289 for(height = 2; height <= 40; height+=2) {
290 printf("Size %dx%d:\n", width, height);
291 for(filter=0;filter<=SCHRO_WAVELET_DAUBECHIES_9_7;filter++){
292 printf(" filter %d:\n", filter);
293 fwd_random_test(filter, width, height);
294 inv_random_test(filter, width, height);
295 fwd_random_test_s32(filter, width, height);
296 inv_random_test_s32(filter, width, height);
301 return fail;
304 void
305 rshift (SchroFrameData *p, int n)
307 int i;
308 int j;
309 int16_t *data;
311 if (n==0) return;
312 for(j=0;j<p->height;j++){
313 data = OFFSET(p->data,j*p->stride);
314 for(i=0;i<p->width;i++){
315 data[i] >>= n;
320 void
321 lshift (SchroFrameData *p, int n)
323 int i;
324 int j;
325 int16_t *data;
327 if (n==0) return;
328 for(j=0;j<p->height;j++){
329 data = OFFSET(p->data,j*p->stride);
330 for(i=0;i<p->width;i++){
331 data[i] <<= n;
336 void
337 copy (int16_t *d, int ds, int16_t *s, int ss, int n)
339 int i;
340 int16_t *xd, *xs;
341 for(i=0;i<n;i++){
342 xd = OFFSET(d,ds * i);
343 xs = OFFSET(s,ss * i);
344 *xd = *xs;
348 void iwt_ref(SchroFrameData *p, int filter)
350 int16_t tmp1[100], *hi;
351 int16_t tmp2[100], *lo;
352 int16_t tmp3[100], *tmpbuf;
353 int16_t *data;
354 int i;
356 hi = tmp1 + 4;
357 lo = tmp2 + 4;
358 tmpbuf = tmp3 + 8;
360 lshift(p, filtershift[filter]);
362 for(i=0;i<p->height;i++){
363 data = OFFSET(p->data,i*p->stride);
364 copy(tmpbuf, sizeof(int16_t), data, sizeof(int16_t), p->width);
365 split (tmpbuf, p->width, filter);
366 orc_deinterleave2_s16 (lo, hi, tmpbuf, p->width/2);
367 copy(data, sizeof(int16_t), lo, sizeof(int16_t), p->width/2);
368 copy(data + p->width/2, sizeof(int16_t), hi, sizeof(int16_t), p->width/2);
371 for(i=0;i<p->width;i++){
372 data = OFFSET(p->data,i*sizeof(int16_t));
373 copy(tmpbuf, sizeof(int16_t), data, p->stride, p->height);
374 split (tmpbuf, p->height, filter);
375 copy(data, p->stride, tmpbuf, sizeof(int16_t), p->height);
380 void iiwt_ref(SchroFrameData *p, int filter)
382 int16_t tmp1[100], *hi;
383 int16_t tmp2[100], *lo;
384 int16_t tmp3[100], *tmpbuf;
385 int16_t *data;
386 int i;
388 hi = tmp1 + 4;
389 lo = tmp2 + 4;
390 tmpbuf = tmp3 + 8;
392 for(i=0;i<p->width;i++){
393 data = OFFSET(p->data,i*sizeof(int16_t));
394 copy(tmpbuf, sizeof(int16_t), data, p->stride, p->height);
395 synth (tmpbuf, p->height, filter);
396 copy(data, p->stride, tmpbuf, sizeof(int16_t), p->height);
399 for(i=0;i<p->height;i++){
400 data = OFFSET(p->data,i*p->stride);
401 copy(hi, sizeof(int16_t), data, sizeof(int16_t), p->width/2);
402 copy(lo, sizeof(int16_t), data + p->width/2, sizeof(int16_t), p->width/2);
403 orc_interleave2_s16 (tmpbuf, hi, lo, p->width/2);
404 synth (tmpbuf, p->width, filter);
405 copy(data, sizeof(int16_t), tmpbuf, sizeof(int16_t), p->width);
408 rshift(p, filtershift[filter]);
411 void iwt_test(SchroFrameData *p, int filter)
413 int16_t *tmp;
415 tmp = malloc(((p->width+8)*2)*sizeof(int32_t));
417 schro_wavelet_transform_2d (p, filter, tmp);
419 free(tmp);
422 void iiwt_test(SchroFrameData *p, int filter)
424 int16_t *tmp;
426 tmp = malloc(((p->width+8)*2)*sizeof(int32_t));
428 schro_wavelet_inverse_transform_2d (p, p, filter, tmp);
430 free(tmp);