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