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