comparison Meerwald/cmp_dwt.c @ 0:be303a3f5ea8

import
author Peter Meerwald <pmeerw@cosy.sbg.ac.at>
date Sun, 12 Aug 2007 13:14:34 +0200
parents
children f83ef905a63d
comparison
equal deleted inserted replaced
-1:000000000000 0:be303a3f5ea8
1 #include "wm.h"
2 #include "dwt.h"
3 #include "coeff.h"
4 #include "gray.h"
5 #include "pgm.h"
6 #include "dwt_util.h"
7
8 char *progname;
9
10 void usage(void) {
11 fprintf(stderr, "usage: %s [-e n] [-f n] [-F file] [-h] [-l n] [-o file] [-pP] [-s name,..] -i file file\n\n", progname);
12 fprintf(stderr, "\t-e n\t\twavelet filtering method (default: 2)\n");
13 fprintf(stderr, "\t-f n\t\tfilter number (default: 2)\n");
14 fprintf(stderr, "\t-F file\t\tfilter definition file (default: filter.dat\n");
15 fprintf(stderr, "\t-h\t\tprint usage\n");
16 fprintf(stderr, "\t-i file\t\toriginal image file\n");
17 fprintf(stderr, "\t-l n\t\tmax. decomposition level (default: 0 = max)\n");
18 fprintf(stderr, "\t-m n\t\tmultiplication factor (default: 0 = auto)\n");
19 fprintf(stderr, "\t-o file\t\toutput file\n");
20 fprintf(stderr, "\t-p\t\tprint PSNR, RMS and MSE\n");
21 fprintf(stderr, "\t-P\t\tonly print PSNR, RMS and MSE, no difference image\n");
22 fprintf(stderr, "\t-q\t\tprint PSNR, RMS and MSE per subband\n");
23 fprintf(stderr, "\t-s name,...\tsubband/level (default: all subbands)\n");
24 fprintf(stderr, "\t-v n\t\tverbosity level\n");
25 exit(0);
26 }
27
28 void process_subband_var(gray **output, int cols, int rows, Image_tree p, Image_tree q, int type, double min, double max, gray maxval) {
29 int i;
30 int col, row, startcol, startrow;
31
32 if (!p || !q) return;
33
34 calc_subband_location(cols, rows, type, p->level, &startcol, &startrow);
35
36 for (row = 0; row < p->image->height; row++)
37 for (col = 0; col < p->image->width; col++) {
38 double diff = fabs(get_pixel(p->image, col, row) - get_pixel(q->image, col, row));
39 output[startrow + row][startcol + col] = PIXELRANGE((double) (diff - min) / (double) (max - min) * maxval);
40 }
41 }
42
43 void process_subband_fixed(gray **output, int cols, int rows, Image_tree p, Image_tree q, int type, double m) {
44 int i;
45 int col, row, startcol, startrow;
46
47 if (!p || !q) return;
48
49 calc_subband_location(cols, rows, type, p->level, &startcol, &startrow);
50
51 for (row = 0; row < p->image->height; row++)
52 for (col = 0; col < p->image->width; col++) {
53 double diff = fabs(get_pixel(p->image, col, row) - get_pixel(q->image, col, row));
54 output[startrow + row][startcol + col] = PIXELRANGE(diff * m);
55 }
56 }
57
58 void print_subband_psnr(int type, int level, double error, int cols, int rows, double min, double max, FILE *print) {
59 double mse = error / (double) (cols * rows);
60 double rmse = sqrt(mse);
61 double psnr = 20.0 * log(255.0 / rmse) / log(10.0);
62 int startcol, startrow;
63
64 calc_subband_location(cols << level, rows << level, type, level, &startcol, &startrow);
65 fprintf(print, "%s%d (%d x %d) at %d x %d\n", subband_name(type), level, cols, rows, startcol, startrow);
66 if (mse > 0.0)
67 fprintf(print, " PSNR: %lf dB\n", psnr);
68 else
69 fprintf(print, " PSNR: inf\n");
70 fprintf(print, " RMS: %lf\n", rmse);
71 fprintf(print, " MSE: %lf\n", mse);
72 fprintf(print, " dmin, dmax: %lf, %lf\n", min, max);
73 }
74
75 int main(int argc, char *argv[]) {
76 FILE *in = stdin;
77 FILE *out = stdout;
78 FILE *orig = NULL;
79 FILE *print;
80
81 gray **input_image;
82 gray **orig_image;
83 Image_tree input_dwts, p;
84 Image_tree orig_dwts, q;
85 gray **output;
86
87 char output_name[MAXPATHLEN] = "(stdout)";
88 char input_name[MAXPATHLEN] = "(stdin)";
89 char orig_name[MAXPATHLEN];
90 char *subband_list = NULL;
91
92 int in_cols, in_rows, in_format;
93 gray in_maxval;
94 int orig_cols, orig_rows, orig_format;
95 gray orig_maxval;
96 int cols, rows, format;
97 gray maxval;
98 int col, row;
99
100 int no_orig = 0;
101 int m = 0;
102 int c;
103 int maxlevel = 0;
104
105 int filter = 1;
106 int method = 2;
107 int levels;
108 char filter_name[MAXPATHLEN] = "filter.dat";
109
110 double error = 0.0;
111 int print_psnr = 0;
112 int print_psnr_subband = 0;
113 int print_psnr_only = 0;
114
115 double min, max;
116
117 int verbose = 0;
118
119 progname = argv[0];
120
121 pgm_init(&argc, argv); wm_init();
122
123 while ((c = getopt(argc, argv, "e:f:F:h?i:l:m:o:pPqs:v:")) != EOF) {
124 switch (c) {
125 case 'h':
126 case '?':
127 usage();
128 break;
129 case 'e':
130 method = atoi(optarg);
131 if (method < 0) {
132 fprintf(stderr, "%s: wavelet filtering method %d out of range\n", progname, method);
133 exit(1);
134 }
135 break;
136 case 'f':
137 filter = atoi(optarg);
138 if (filter <= 0) {
139 fprintf(stderr, "%s: filter number %d out of range\n", progname, filter);
140 exit(1);
141 }
142 break;
143 case 'F':
144 strcpy(filter_name, optarg);
145 break;
146 case 'i':
147 if (!strcmp(optarg, "-")) {
148 no_orig = 1;
149 strcpy(orig_name, "(zero)");
150 }
151 else {
152 if ((orig = fopen(optarg, "rb")) == NULL) {
153 fprintf(stderr, "%s: unable to open original image file %s\n", progname, optarg);
154 exit(1);
155 }
156 strcpy(orig_name, optarg);
157 }
158 break;
159 case 'l':
160 maxlevel = atoi(optarg);
161 if (maxlevel < 0) {
162 fprintf(stderr, "%s: decomposition level %d out of range\n", progname, maxlevel);
163 exit(1);
164 }
165 break;
166 case 'm':
167 m = atoi(optarg);
168 if (m < -1) {
169 fprintf(stderr, "%s: multiplication factor %d out of range\n", progname, m);
170 exit(1);
171 }
172 break;
173 case 'o':
174 if ((out = fopen(optarg, "wb")) == NULL) {
175 fprintf(stderr, "%s: unable to open output file %s\n", progname, optarg);
176 exit(1);
177 }
178 strcpy(output_name, optarg);
179 break;
180 case 'p':
181 print_psnr = 1;
182 break;
183 case 'P':
184 print_psnr_only = 1;
185 break;
186 case 'q':
187 print_psnr = 1;
188 print_psnr_subband = 1;
189 case 's':
190 subband_list = optarg;
191 break;
192 case 'v':
193 verbose = atoi(optarg);
194 if (verbose < 0) {
195 fprintf(stderr, "%s: verbosity level %d out of range\n", progname, verbose);
196 exit(1);
197 }
198 break;
199
200 }
201 }
202
203 argc -= optind;
204 argv += optind;
205
206 if (argc > 1) {
207 usage();
208 exit(1);
209 }
210
211 print = print_psnr_only ? out : stderr;
212
213 if (argc == 1 && *argv[0] != '-')
214 if ((in = fopen(argv[0], "rb")) == NULL) {
215 fprintf(stderr, "%s: unable to open input file %s\n", progname, argv[0]);
216 exit(1);
217 }
218 else
219 strcpy(input_name, argv[0]);
220
221 if (!orig && !no_orig) {
222 fprintf(stderr, "%s: original image file not specified, using zero image\n", progname);
223 strcpy(orig_name, "(zero)");
224 no_orig = 1;
225 }
226
227 pgm_readpgminit(in, &in_cols, &in_rows, &in_maxval, &in_format);
228
229 if (!no_orig) {
230 pgm_readpgminit(orig, &orig_cols, &orig_rows, &orig_maxval, &orig_format);
231 if (in_cols != orig_cols || in_rows != orig_rows) {
232 fprintf(stderr, "%s: input image %s does not match dimensions of original image %s\n", progname, input_name, orig_name);
233 exit(1);
234 }
235 }
236
237 cols = in_cols;
238 rows = in_rows;
239 format = in_format;
240 maxval = in_maxval;
241
242 input_image = pgm_allocarray(cols, rows);
243 orig_image = pgm_allocarray(cols, rows);
244
245 output = alloc_grays(cols, rows);
246
247 if (no_orig) {
248 for (row = 0; row < rows; row++) {
249 pgm_readpgmrow(in, input_image[row], cols, maxval, format);
250 bzero(orig_image[row], sizeof(gray) * cols);
251 }
252 }
253 else {
254 for (row = 0; row < rows; row++) {
255 pgm_readpgmrow(in, input_image[row], in_cols, in_maxval, in_format);
256 pgm_readpgmrow(orig, orig_image[row], orig_cols, orig_maxval, orig_format);
257 }
258 }
259
260 fclose(in);
261 if (!no_orig)
262 fclose(orig);
263
264 // complete decomposition
265 levels = find_deepest_level(cols, rows) - 1;
266 if (!maxlevel) maxlevel = levels;
267 if (maxlevel > levels) {
268 fprintf(stderr, "%s: decomposition level %d not possible (max. %d), image size is %d x %d\n", progname, maxlevel, levels, cols, rows);
269 exit(1);
270 }
271
272 init_dwt(cols, rows, filter_name, filter, maxlevel, method);
273
274 input_dwts = fdwt(input_image);
275 orig_dwts = fdwt(orig_image);
276
277 p = input_dwts;
278 q = orig_dwts;
279 min = 10000000.0;
280 max = 0.0;
281 error = 0.0;
282 while (p->coarse && q->coarse) {
283 double localmin, localmax, localerror;
284
285 if (subband_in_list(subband_list, HORIZONTAL, p->horizontal->level)) {
286 calc_subband(p->horizontal, q->horizontal, HORIZONTAL, &localmin, &localmax, &localerror);
287 if (m == -1) process_subband_var(output, cols, rows, p->horizontal, q->horizontal, HORIZONTAL, localmin, localmax, maxval);
288 if (localmin < min) min = localmin;
289 if (localmax > max) max = localmax;
290 error += localerror;
291 if (print_psnr_subband)
292 print_subband_psnr(HORIZONTAL, p->horizontal->level, error, p->horizontal->image->width, p->horizontal->image->height, localmin, localmax, print);
293 }
294 else if (verbose > 5)
295 fprintf(stderr, "%s: subband %s%d skipped\n", progname, subband_name(HORIZONTAL), p->horizontal->level);
296
297 if (subband_in_list(subband_list, VERTICAL, p->vertical->level)) {
298 calc_subband(p->vertical, q->vertical, VERTICAL, &localmin, &localmax, &localerror);
299 if (m == -1) process_subband_var(output, cols, rows, p->vertical, q->vertical, VERTICAL, localmin, localmax, maxval);
300 if (localmin < min) min = localmin;
301 if (localmax > max) max = localmax;
302 error += localerror;
303 if (print_psnr_subband)
304 print_subband_psnr(VERTICAL, p->vertical->level, error, p->vertical->image->width, p->vertical->image->height, localmin, localmax, print);
305 }
306 else if (verbose > 5)
307 fprintf(stderr, "%s: subband %s%d skipped\n", progname, subband_name(VERTICAL), p->vertical->level);
308
309 if (subband_in_list(subband_list, DIAGONAL, p->diagonal->level)) {
310 calc_subband(p->diagonal, q->diagonal, DIAGONAL, &localmin, &localmax, &localerror);
311 if (m == -1) process_subband_var(output, cols, rows, p->diagonal, q->diagonal, DIAGONAL, localmin, localmax, maxval);
312 if (localmin < min) min = localmin;
313 if (localmax > max) max = localmax;
314 error += localerror;
315 if (print_psnr_subband)
316 print_subband_psnr(DIAGONAL, p->vertical->level, error, p->diagonal->image->width, p->diagonal->image->height, localmin, localmax, print);
317 }
318 else if (verbose > 5)
319 fprintf(stderr, "%s: subband %s%d skipped\n", progname, subband_name(DIAGONAL), p->diagonal->level);
320
321 p = p->coarse;
322 q = q->coarse;
323
324 if (!p->coarse) {
325 if (subband_in_list(subband_list, COARSE, p->level)) {
326 calc_subband(p, q, COARSE, &localmin, &localmax, &localerror);
327 if (m == -1) process_subband_var(output, cols, rows, p, q, COARSE, localmin, localmax, maxval);
328 if (localmin < min) min = localmin;
329 if (localmax > max) max = localmax;
330 error += localerror;
331 if (print_psnr_subband)
332 print_subband_psnr(COARSE, p->level, error, p->image->width, p->image->height, localmin, localmax, print);
333 }
334 else if (verbose > 5)
335 fprintf(stderr, "%s: subband %s%d skipped\n", progname, subband_name(COARSE), p->level);
336 }
337 }
338
339 p = input_dwts;
340 q = orig_dwts;
341 while (p->coarse && q->coarse) {
342 if (m > 0) {
343 process_subband_fixed(output, cols, rows, p->horizontal, q->horizontal, HORIZONTAL, m);
344 process_subband_fixed(output, cols, rows, p->vertical, q->vertical, VERTICAL, m);
345 process_subband_fixed(output, cols, rows, p->diagonal, q->diagonal, DIAGONAL, m);
346 }
347 else if (m == 0) {
348 process_subband_var(output, cols, rows, p->horizontal, q->horizontal, HORIZONTAL, min, max, maxval);
349 process_subband_var(output, cols, rows, p->vertical, q->vertical, VERTICAL, min, max, maxval);
350 process_subband_var(output, cols, rows, p->diagonal, q->diagonal, DIAGONAL, min, max, maxval);
351 }
352
353 p = p->coarse;
354 q = q->coarse;
355
356 if (!p->coarse) {
357 if (m > 0)
358 process_subband_fixed(output, cols, rows, p, q, COARSE, m);
359 else if (m == 0)
360 process_subband_var(output, cols, rows, p, q, COARSE, min, max, maxval);
361 }
362 }
363
364 if (!print_psnr_only) {
365 pgm_writepgminit(out, cols, rows, maxval, 0);
366 for (row = 0; row < rows; row++)
367 pgm_writepgmrow(out, output[row], cols, maxval, 0);
368
369 fclose(out);
370 }
371
372 pgm_freearray(input_image, rows);
373 pgm_freearray(orig_image, rows);
374 free_grays(output);
375
376 if (print_psnr || print_psnr_only) {
377 double mse = error / (double) (cols * rows);
378 double rmse = sqrt(mse);
379 double psnr = 20.0 * log(255.0 / rmse) / log(10.0);
380 if (mse > 0.0)
381 fprintf(print, "PSNR: %lf dB\n", psnr);
382 else
383 fprintf(print, "PSNR: inf\n");
384 fprintf(print, "RMS: %lf\n", rmse);
385 fprintf(print, "MSE: %lf\n", mse);
386 fprintf(print, "dmin, dmax: %lf, %lf\n", min, max);
387 }
388
389 exit(0);
390 }

Repositories maintained by Peter Meerwald, pmeerw@pmeerw.net.