annotate Meerwald/cmp_pgm.c @ 1:647ab0f05aae v0.4

drop, not needed
author Peter Meerwald <pmeerw@cosy.sbg.ac.at>
date Tue, 14 Aug 2007 19:58:25 +0200
parents be303a3f5ea8
children acb6967ee76d
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
1 #include "wm.h"
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
2 #include "pgm.h"
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
3
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
4 char *progname;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
5
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
6 void usage(void) {
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
7 fprintf(stderr, "usage: %s [-C] [-h] [-m n] [-o file] [-pP] [-r file] -i file file\n\n", progname);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
8 fprintf(stderr, "\t-C\t\tprint PSNR value only\n");
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
9 fprintf(stderr, "\t-h\t\tprint usage\n");
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
10 fprintf(stderr, "\t-i file\t\toriginal image file\n");
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
11 fprintf(stderr, "\t-m n\t\tmultiplication factor (default auto)\n");
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
12 fprintf(stderr, "\t-o file\t\toutput file for difference image\n");
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
13 fprintf(stderr, "\t-p\t\tprint PSNR, RMS and MSE\n");
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
14 fprintf(stderr, "\t-P\t\tonly print PSNR, RMS and MSE, no difference image\n");
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
15 fprintf(stderr, "\t-r file\t\tROI (region-of-interest) mask (default none)\n");
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
16 exit(0);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
17 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
18
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
19 int main(int argc, char *argv[]) {
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
20
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
21 FILE *in = stdin;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
22 FILE *out = stdout;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
23 FILE *orig = NULL;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
24 FILE *roi = NULL;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
25
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
26 gray **input_image;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
27 gray **orig_image;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
28 gray **roi_image;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
29
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
30 char output_name[MAXPATHLEN] = "(stdout)";
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
31 char input_name[MAXPATHLEN] = "(stdin)";
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
32 char orig_name[MAXPATHLEN];
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
33 char roi_name[MAXPATHLEN];
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
34
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
35 int in_cols, in_rows, in_format;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
36 gray in_maxval;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
37 int roi_cols, roi_rows, roi_format;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
38 gray roi_maxval;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
39 int orig_cols, orig_rows, orig_format;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
40 gray orig_maxval;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
41 int cols, rows, format;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
42 gray maxval;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
43 int col, row;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
44
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
45 int roisize = 0;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
46
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
47 int i;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
48 int c;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
49
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
50 int m = 0;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
51 int min, max;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
52
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
53 int print_psnr = 0;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
54 int print_psnr_only = 0;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
55 int print_psnr_value_only = 0;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
56 double error = 0.0;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
57
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
58 progname = argv[0];
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
59
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
60 pgm_init(&argc, argv); wm_init();
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
61
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
62 while ((c = getopt(argc, argv, "h?i:m:o:r:pPC")) != EOF) {
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
63 switch (c) {
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
64 case 'h':
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
65 case '?':
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
66 usage();
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
67 break;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
68 case 'i':
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
69 if ((orig = fopen(optarg, "rb")) == NULL) {
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
70 fprintf(stderr, "%s: unable to open original image file %s\n", progname, optarg);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
71 exit(1);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
72 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
73 strcpy(orig_name, optarg);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
74 break;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
75 case 'r':
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
76 if ((roi = fopen(optarg, "rb")) == NULL) {
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
77 fprintf(stderr, "%s: unable to open ROI image file %s\n", progname, optarg);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
78 exit(1);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
79 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
80 strcpy(roi_name, optarg);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
81 break;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
82 case 'm':
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
83 m = atoi(optarg);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
84 if (m <= 0) {
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
85 fprintf(stderr, "%s: multiplication factor %d out of range\n", progname, m);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
86 exit(1);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
87 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
88 break;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
89 case 'o':
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
90 if ((out = fopen(optarg, "wb")) == NULL) {
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
91 fprintf(stderr, "%s: unable to open output file %s\n", progname, optarg);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
92 exit(1);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
93 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
94 strcpy(output_name, optarg);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
95 break;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
96 case 'p':
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
97 print_psnr = 1;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
98 break;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
99 case 'P':
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
100 print_psnr_only = 1;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
101 break;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
102 case 'C':
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
103 print_psnr_value_only = 1;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
104 print_psnr_only = 1;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
105 break;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
106 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
107 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
108
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
109 argc -= optind;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
110 argv += optind;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
111
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
112 if (argc > 1) {
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
113 usage();
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
114 exit(1);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
115 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
116
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
117 if (argc == 1 && *argv[0] != '-')
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
118 if ((in = fopen(argv[0], "rb")) == NULL) {
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
119 fprintf(stderr, "%s: unable to open input file %s\n", progname, argv[0]);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
120 exit(1);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
121 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
122 else
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
123 strcpy(input_name, argv[0]);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
124
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
125 if (!orig) {
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
126 fprintf(stderr, "%s: original image file not specified, use -i file option\n", progname);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
127 exit(1);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
128 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
129
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
130 pgm_readpgminit(in, &in_cols, &in_rows, &in_maxval, &in_format);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
131
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
132 if (orig) {
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
133 pgm_readpgminit(orig, &orig_cols, &orig_rows, &orig_maxval, &orig_format);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
134
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
135 if (in_cols != orig_cols || in_rows != orig_rows) {
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
136 fprintf(stderr, "%s: input image %s does not match dimensions of original image %s\n", progname, input_name, orig_name);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
137 exit(1);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
138 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
139 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
140
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
141 if (roi) {
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
142 pgm_readpgminit(roi, &roi_cols, &roi_rows, &roi_maxval, &roi_format);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
143
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
144 if (in_cols != roi_cols || in_rows != roi_rows) {
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
145 fprintf(stderr, "%s: input image %s does not match dimensions of ROI image %s\n", progname, input_name, roi_name);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
146 exit(1);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
147 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
148 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
149
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
150 cols = in_cols;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
151 rows = in_rows;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
152 format = in_format;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
153 maxval = in_maxval;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
154
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
155 input_image = pgm_allocarray(cols, rows);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
156 orig_image = pgm_allocarray(cols, rows);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
157 roi_image = pgm_allocarray(cols, rows);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
158
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
159 for (row = 0; row < rows; row++) {
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
160 pgm_readpgmrow(in, input_image[row], cols, in_maxval, in_format);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
161 if (orig)
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
162 pgm_readpgmrow(orig, orig_image[row], cols, orig_maxval, orig_format);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
163 else
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
164 memset(orig_image[row], cols, 0);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
165 if (roi)
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
166 pgm_readpgmrow(roi, roi_image[row], cols, roi_maxval, roi_format);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
167 else
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
168 memset(roi_image[row], cols, PGM_MAXMAXVAL);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
169 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
170
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
171 fclose(in);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
172 if (orig) fclose(orig);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
173 if (roi) fclose(roi);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
174
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
175 max = 0;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
176 min = PGM_MAXMAXVAL;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
177
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
178 for (row = 0; row < rows; row++) {
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
179 gray *pi = input_image[row];
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
180 gray *po = orig_image[row];
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
181 gray *pr = roi_image[row];
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
182
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
183 for (col = 0; col < cols; col++) {
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
184 int diff = abs(*pi - *po);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
185 int inroi = (!roi || *pr > 0);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
186
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
187 pi++;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
188 po++;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
189 pr++;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
190
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
191 if (roi && !inroi)
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
192 continue;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
193
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
194 roisize++;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
195
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
196 error += sqr(diff);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
197 if (diff < min) min = diff;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
198 if (diff > max) max = diff;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
199
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
200 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
201 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
202
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
203 for (row = 0; row < rows; row++) {
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
204 gray *pi = input_image[row];
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
205 gray *po = orig_image[row];
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
206 gray *pr = roi_image[row];
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
207
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
208 for (col = 0; col < cols; col++) {
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
209 int diff = abs(*pi - *po);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
210 int inroi = (!roi || *pr > 0);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
211
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
212 if (!inroi) {
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
213 *pi = 0;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
214 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
215 else {
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
216 if (m > 0)
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
217 *pi = PIXELRANGE(diff * m);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
218 else
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
219 *pi = PIXELRANGE((double) (diff - min) / (double) (max - min) * maxval);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
220 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
221
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
222 pi++;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
223 po++;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
224 pr++;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
225
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
226 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
227 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
228
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
229 if (!print_psnr_only) {
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
230 pgm_writepgminit(out, cols, rows, maxval, 0);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
231 for (row = 0; row < rows; row++)
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
232 pgm_writepgmrow(out, input_image[row], cols, maxval, 0);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
233
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
234 fclose(out);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
235 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
236
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
237 pgm_freearray(input_image, rows);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
238 pgm_freearray(orig_image, rows);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
239 pgm_freearray(roi_image, rows);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
240
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
241 if (print_psnr || print_psnr_only) {
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
242 double mse = (roisize) ? error / (double) roisize : 0;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
243 double rmse = sqrt(mse);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
244 double psnr = (rmse) ? 20.0 * log(255.0 / rmse) / log(10.0) : 0;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
245 FILE *print = print_psnr_only ? out : stderr;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
246 if (!print_psnr_value_only) {
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
247 if (roi)
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
248 fprintf(print, "ROI size: %d\n", roisize);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
249 if (mse > 0.0)
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
250 fprintf(print, "PSNR: %lf dB\n", psnr);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
251 else
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
252 fprintf(print, "PSNR: inf\n");
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
253 fprintf(print, "RMSE: %lf\n", rmse);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
254 fprintf(print, "MSE: %lf\n", mse);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
255 fprintf(print, "dmin, dmax: %d, %d\n", min, max);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
256 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
257 else
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
258 fprintf(print, "%lf\n", mse > 0.0 ? psnr : 100.0);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
259 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
260
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
261 exit(0);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
262 }

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