0
|
1 #include "wm.h"
|
|
2 #include "pgm.h"
|
|
3
|
|
4 char *progname;
|
|
5
|
|
6 void usage(void) {
|
3
|
7 fprintf(stderr, "usage: %s [-C] [-h] [-m n] [-o file] [-pP] -i file file\n\n", progname);
|
0
|
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 exit(0);
|
|
16 }
|
|
17
|
|
18 int main(int argc, char *argv[]) {
|
|
19
|
|
20 FILE *in = stdin;
|
|
21 FILE *out = stdout;
|
|
22 FILE *orig = NULL;
|
|
23
|
|
24 gray **input_image;
|
|
25 gray **orig_image;
|
|
26
|
|
27 char output_name[MAXPATHLEN] = "(stdout)";
|
|
28 char input_name[MAXPATHLEN] = "(stdin)";
|
|
29 char orig_name[MAXPATHLEN];
|
|
30
|
|
31 int in_cols, in_rows, in_format;
|
|
32 gray in_maxval;
|
|
33 int orig_cols, orig_rows, orig_format;
|
|
34 gray orig_maxval;
|
|
35 int cols, rows, format;
|
|
36 gray maxval;
|
|
37 int col, row;
|
|
38
|
|
39 int i;
|
|
40 int c;
|
|
41
|
|
42 int m = 0;
|
|
43 int min, max;
|
|
44
|
|
45 int print_psnr = 0;
|
|
46 int print_psnr_only = 0;
|
|
47 int print_psnr_value_only = 0;
|
|
48 double error = 0.0;
|
|
49
|
|
50 progname = argv[0];
|
|
51
|
|
52 pgm_init(&argc, argv); wm_init();
|
|
53
|
3
|
54 while ((c = getopt(argc, argv, "h?i:m:o:pPC")) != EOF) {
|
0
|
55 switch (c) {
|
|
56 case 'h':
|
|
57 case '?':
|
|
58 usage();
|
|
59 break;
|
|
60 case 'i':
|
|
61 if ((orig = fopen(optarg, "rb")) == NULL) {
|
|
62 fprintf(stderr, "%s: unable to open original image file %s\n", progname, optarg);
|
|
63 exit(1);
|
|
64 }
|
|
65 strcpy(orig_name, optarg);
|
|
66 break;
|
|
67 case 'm':
|
|
68 m = atoi(optarg);
|
|
69 if (m <= 0) {
|
|
70 fprintf(stderr, "%s: multiplication factor %d out of range\n", progname, m);
|
|
71 exit(1);
|
|
72 }
|
|
73 break;
|
|
74 case 'o':
|
|
75 if ((out = fopen(optarg, "wb")) == NULL) {
|
|
76 fprintf(stderr, "%s: unable to open output file %s\n", progname, optarg);
|
|
77 exit(1);
|
|
78 }
|
|
79 strcpy(output_name, optarg);
|
|
80 break;
|
|
81 case 'p':
|
|
82 print_psnr = 1;
|
|
83 break;
|
|
84 case 'P':
|
|
85 print_psnr_only = 1;
|
|
86 break;
|
|
87 case 'C':
|
|
88 print_psnr_value_only = 1;
|
|
89 print_psnr_only = 1;
|
|
90 break;
|
|
91 }
|
|
92 }
|
|
93
|
|
94 argc -= optind;
|
|
95 argv += optind;
|
|
96
|
|
97 if (argc > 1) {
|
|
98 usage();
|
|
99 exit(1);
|
|
100 }
|
|
101
|
|
102 if (argc == 1 && *argv[0] != '-')
|
|
103 if ((in = fopen(argv[0], "rb")) == NULL) {
|
|
104 fprintf(stderr, "%s: unable to open input file %s\n", progname, argv[0]);
|
|
105 exit(1);
|
|
106 }
|
|
107 else
|
|
108 strcpy(input_name, argv[0]);
|
|
109
|
|
110 if (!orig) {
|
|
111 fprintf(stderr, "%s: original image file not specified, use -i file option\n", progname);
|
|
112 exit(1);
|
|
113 }
|
|
114
|
|
115 pgm_readpgminit(in, &in_cols, &in_rows, &in_maxval, &in_format);
|
|
116
|
3
|
117 pgm_readpgminit(orig, &orig_cols, &orig_rows, &orig_maxval, &orig_format);
|
0
|
118
|
3
|
119 if (in_cols != orig_cols || in_rows != orig_rows) {
|
|
120 fprintf(stderr, "%s: input image %s does not match dimensions of original image %s\n", progname, input_name, orig_name);
|
|
121 exit(1);
|
0
|
122 }
|
|
123
|
|
124 cols = in_cols;
|
|
125 rows = in_rows;
|
|
126 format = in_format;
|
|
127 maxval = in_maxval;
|
|
128
|
|
129 input_image = pgm_allocarray(cols, rows);
|
|
130 orig_image = pgm_allocarray(cols, rows);
|
|
131
|
|
132 for (row = 0; row < rows; row++) {
|
|
133 pgm_readpgmrow(in, input_image[row], cols, in_maxval, in_format);
|
3
|
134 pgm_readpgmrow(orig, orig_image[row], cols, orig_maxval, orig_format);
|
0
|
135 }
|
|
136
|
|
137 fclose(in);
|
3
|
138 fclose(orig);
|
0
|
139
|
3
|
140 min = max = abs(input_image[0][0] - orig_image[0][0]);
|
0
|
141
|
|
142 for (row = 0; row < rows; row++) {
|
|
143 gray *pi = input_image[row];
|
|
144 gray *po = orig_image[row];
|
|
145
|
|
146 for (col = 0; col < cols; col++) {
|
|
147 int diff = abs(*pi - *po);
|
|
148 error += sqr(diff);
|
|
149 if (diff < min) min = diff;
|
|
150 if (diff > max) max = diff;
|
|
151
|
3
|
152 pi++;
|
|
153 po++;
|
0
|
154 }
|
|
155 }
|
|
156
|
|
157 for (row = 0; row < rows; row++) {
|
|
158 gray *pi = input_image[row];
|
|
159 gray *po = orig_image[row];
|
|
160
|
|
161 for (col = 0; col < cols; col++) {
|
|
162 int diff = abs(*pi - *po);
|
3
|
163 if (m > 0)
|
|
164 *pi = PIXELRANGE(diff * m);
|
|
165 else
|
|
166 *pi = PIXELRANGE((double) (diff - min) / (double) (max - min) * maxval);
|
0
|
167
|
|
168 pi++;
|
|
169 po++;
|
|
170 }
|
|
171 }
|
|
172
|
|
173 if (!print_psnr_only) {
|
|
174 pgm_writepgminit(out, cols, rows, maxval, 0);
|
|
175 for (row = 0; row < rows; row++)
|
|
176 pgm_writepgmrow(out, input_image[row], cols, maxval, 0);
|
|
177
|
|
178 fclose(out);
|
|
179 }
|
|
180
|
|
181 pgm_freearray(input_image, rows);
|
|
182 pgm_freearray(orig_image, rows);
|
|
183
|
|
184 if (print_psnr || print_psnr_only) {
|
3
|
185 double mse = error / (double) (cols * rows);
|
0
|
186 double rmse = sqrt(mse);
|
3
|
187 double psnr = 20.0 * log(255.0 / rmse) / log(10.0);
|
0
|
188 FILE *print = print_psnr_only ? out : stderr;
|
|
189 if (!print_psnr_value_only) {
|
|
190 if (mse > 0.0)
|
|
191 fprintf(print, "PSNR: %lf dB\n", psnr);
|
|
192 else
|
|
193 fprintf(print, "PSNR: inf\n");
|
|
194 fprintf(print, "RMSE: %lf\n", rmse);
|
|
195 fprintf(print, "MSE: %lf\n", mse);
|
|
196 fprintf(print, "dmin, dmax: %d, %d\n", min, max);
|
|
197 }
|
|
198 else
|
|
199 fprintf(print, "%lf\n", mse > 0.0 ? psnr : 100.0);
|
|
200 }
|
|
201
|
|
202 exit(0);
|
|
203 }
|