Mercurial > hg > wm
annotate Meerwald/cmp_ppm.c @ 13:cbecc570129d
cleanup warnings
author | Peter Meerwald <pmeerw@cosy.sbg.ac.at> |
---|---|
date | Mon, 30 Jun 2008 15:43:16 +0200 |
parents | a5249f7d45f7 |
children | bd669312f068 |
rev | line source |
---|---|
10 | 1 #include "wm.h" |
2 #include "ppm.h" | |
3 | |
4 #define LUMINANCE 1 | |
5 #define RED 2 | |
6 #define GREEN 4 | |
7 #define BLUE 8 | |
8 | |
9 char *progname; | |
10 | |
11 void usage(void) { | |
12 fprintf(stderr, "usage: %s -c name [-C] [-h] [-m n] [-o file] [-pP] -i file file\n\n", progname); | |
13 fprintf(stderr, "\t-c name\t\tcolor component (default luminance)\n"); | |
11
a5249f7d45f7
improve cmp_ppm usage help
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
10
diff
changeset
|
14 fprintf(stderr, "\t\t\teg. red, green, blue, luminance\n"); |
10 | 15 fprintf(stderr, "\t-C\t\tprint PSNR value only\n"); |
16 fprintf(stderr, "\t-h\t\tprint usage\n"); | |
17 fprintf(stderr, "\t-i file\t\toriginal image file\n"); | |
18 fprintf(stderr, "\t-m n\t\tmultiplication factor (default 16)\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 exit(0); | |
23 } | |
24 | |
25 int main(int argc, char *argv[]) { | |
26 | |
27 FILE *in = stdin; | |
28 FILE *out = stdout; | |
29 FILE *orig = NULL; | |
30 | |
31 pixel **input_image; | |
32 pixel **orig_image; | |
33 | |
34 char output_name[MAXPATHLEN] = "(stdout)"; | |
35 char input_name[MAXPATHLEN] = "(stdin)"; | |
36 char orig_name[MAXPATHLEN]; | |
37 | |
38 int in_cols, in_rows, in_format; | |
39 pixval in_maxval; | |
40 int orig_cols, orig_rows, orig_format; | |
41 pixval orig_maxval; | |
42 int cols, rows, format; | |
43 pixval maxval; | |
44 int col, row; | |
45 | |
46 int c; | |
47 | |
48 double error = 0.0; | |
49 int print_psnr = 0; | |
50 int print_psnr_only = 0; | |
51 int print_psnr_value_only = 0; | |
52 | |
53 int m = 16; | |
54 int component = 0; | |
55 int component_default = LUMINANCE; | |
56 int min, max; | |
57 | |
58 progname = argv[0]; | |
59 | |
60 ppm_init(&argc, argv); | |
61 | |
62 #ifdef __EMX__ | |
63 _fsetmode(in, "b"); | |
64 _fsetmode(out, "b"); | |
65 #endif | |
66 | |
67 while ((c = getopt(argc, argv, "Cc:h?i:m:o:pP")) != EOF) { | |
68 switch (c) { | |
69 case 'C': | |
70 print_psnr_value_only = 1; | |
71 print_psnr_only = 1; | |
72 break; | |
73 case 'c': | |
74 if (!strcasecmp(optarg, "RED") || toupper(*optarg) == 'R') | |
75 component |= RED; | |
76 else if (!strcasecmp(optarg, "GREEN") || toupper(*optarg) == 'G') | |
77 component |= GREEN; | |
78 else if (!strcasecmp(optarg, "BLUE") || toupper(*optarg) == 'B') | |
79 component |= BLUE; | |
80 else if (!strcasecmp(optarg, "LUMINANCE") || toupper(*optarg) == 'L') | |
81 component |= LUMINANCE; | |
82 else | |
83 fprintf(stderr, "%s: unknown color component %s\n", progname, optarg); | |
84 break; | |
85 case 'h': | |
86 case '?': | |
87 usage(); | |
88 break; | |
89 case 'i': | |
90 if ((orig = fopen(optarg, "rb")) == NULL) { | |
91 fprintf(stderr, "%s: unable to open original image file %s\n", progname, optarg); | |
92 exit(1); | |
93 } | |
94 strcpy(orig_name, optarg); | |
95 break; | |
96 case 'm': | |
97 m = atoi(optarg); | |
98 if (m <= 0) { | |
99 fprintf(stderr, "%s: multiplication factor %d out of range\n", progname, m); | |
100 exit(1); | |
101 } | |
102 break; | |
103 case 'o': | |
104 if ((out = fopen(optarg, "wb")) == NULL) { | |
105 fprintf(stderr, "%s: unable to open output file %s\n", progname, optarg); | |
106 exit(1); | |
107 } | |
108 strcpy(output_name, optarg); | |
109 break; | |
110 case 'p': | |
111 print_psnr = 1; | |
112 break; | |
113 case 'P': | |
114 print_psnr_only = 1; | |
115 break; | |
116 } | |
117 } | |
118 | |
119 argc -= optind; | |
120 argv += optind; | |
121 | |
122 if (argc > 1) { | |
123 usage(); | |
124 exit(1); | |
125 } | |
126 | |
127 if (argc == 1 && *argv[0] != '-') { | |
128 if ((in = fopen(argv[0], "rb")) == NULL) { | |
129 fprintf(stderr, "%s: unable to open input file %s\n", progname, argv[0]); | |
130 exit(1); | |
131 } | |
132 else | |
133 strcpy(input_name, argv[0]); | |
134 } | |
135 | |
136 if (!orig) { | |
137 fprintf(stderr, "%s: original image file not specified, use -i file option\n", progname); | |
138 exit(1); | |
139 } | |
140 | |
141 if (!component) { | |
142 if (component_default) | |
143 component = component_default; | |
144 else { | |
145 fprintf(stderr, "%s: color component(s) to compare not specified, use -c name option\n", progname); | |
146 exit(1); | |
147 } | |
148 } | |
149 | |
150 if (component & LUMINANCE && component & (RED | GREEN | BLUE)) { | |
151 fprintf(stderr, "%s: unable to compare luminance AND color component\n", progname); | |
152 exit(1); | |
153 } | |
154 | |
155 ppm_readppminit(in, &in_cols, &in_rows, &in_maxval, &in_format); | |
156 | |
157 ppm_readppminit(orig, &orig_cols, &orig_rows, &orig_maxval, &orig_format); | |
158 | |
159 if (in_cols != orig_cols || in_rows != orig_rows) { | |
160 fprintf(stderr, "%s: input image %s does not match dimensions of original image %s\n", progname, input_name, orig_name); | |
161 exit(1); | |
162 } | |
163 | |
164 cols = in_cols; | |
165 rows = in_rows; | |
166 format = in_format; | |
167 maxval = in_maxval; | |
168 | |
169 input_image = ppm_allocarray(cols, rows); | |
170 | |
171 for (row = 0; row < rows; row++) | |
172 ppm_readppmrow(in, input_image[row], cols, in_maxval, in_format); | |
173 | |
174 fclose(in); | |
175 | |
176 orig_image = ppm_allocarray(cols, rows); | |
177 | |
178 for (row = 0; row < rows; row++) | |
179 ppm_readppmrow(orig, orig_image[row], cols, orig_maxval, orig_format); | |
180 | |
181 fclose(orig); | |
182 | |
183 if (component & LUMINANCE) | |
184 min = max = abs(PPM_LUMIN(input_image[0][0]) - PPM_LUMIN(orig_image[0][0])); | |
185 else { | |
186 if (component & RED) | |
187 min = max = abs(PPM_GETR(input_image[0][0]) - PPM_GETR(orig_image[0][0])); | |
188 else if (component & GREEN) | |
189 min = max = abs(PPM_GETG(input_image[0][0]) - PPM_GETG(orig_image[0][0])); | |
190 else if (component & BLUE) | |
191 min = max = abs(PPM_GETB(input_image[0][0]) - PPM_GETB(orig_image[0][0])); | |
192 else | |
193 min = max = 0; | |
194 } | |
195 | |
196 for (row = 0; row < rows; row++) { | |
197 pixel *pi = input_image[row]; | |
198 pixel *po = orig_image[row]; | |
199 | |
200 for (col = 0; col < cols; col++) { | |
201 int diff=0; | |
202 | |
203 if (component & LUMINANCE) { | |
204 pixval l; | |
205 diff = abs(PPM_LUMIN(*pi) - PPM_LUMIN(*po)); | |
206 error += sqr(PPM_LUMIN(*pi) - PPM_LUMIN(*po)); | |
207 l = PIXELRANGE(ROUND(abs(PPM_LUMIN(*pi) - PPM_LUMIN(*po)) * m)); | |
208 PPM_ASSIGN(*pi, l, l, l); | |
209 } | |
210 else { | |
211 if (component & RED) { | |
212 diff = abs(PPM_GETR(*pi) - PPM_GETR(*po)); | |
213 error += sqr(PPM_GETR(*pi) - PPM_GETR(*po)); | |
214 PPM_PUTR(*pi, PIXELRANGE(abs(PPM_GETR(*pi) - PPM_GETR(*po)) * m)); | |
215 } | |
216 else | |
217 PPM_PUTR(*pi, 0); | |
218 if (component & GREEN) { | |
219 diff = abs(PPM_GETG(*pi) - PPM_GETG(*po)); | |
220 error += sqr(PPM_GETG(*pi) - PPM_GETG(*po)); | |
221 PPM_PUTG(*pi, PIXELRANGE(abs(PPM_GETG(*pi) - PPM_GETG(*po)) * m)); | |
222 } | |
223 else | |
224 PPM_PUTG(*pi, 0); | |
225 if (component & BLUE) { | |
226 diff = abs(PPM_GETB(*pi) - PPM_GETB(*po)); | |
227 error += sqr(PPM_GETB(*pi) - PPM_GETB(*po)); | |
228 PPM_PUTB(*pi, PIXELRANGE(abs(PPM_GETB(*pi) - PPM_GETB(*po)) * m)); | |
229 } | |
230 else | |
231 PPM_PUTB(*pi, 0); | |
232 } | |
233 | |
234 if (diff < min) min = diff; | |
235 if (diff > max) max = diff; | |
236 pi++; | |
237 po++; | |
238 } | |
239 } | |
240 | |
241 if (!print_psnr_only) { | |
242 ppm_writeppminit(out, cols, rows, maxval, 0); | |
243 for (row = 0; row < rows; row++) | |
244 ppm_writeppmrow(out, input_image[row], cols, maxval, 0); | |
245 | |
246 fclose(out); | |
247 } | |
248 | |
249 ppm_freearray(input_image, rows); | |
250 ppm_freearray(orig_image, rows); | |
251 | |
252 if (print_psnr || print_psnr_only) { | |
253 double mse = error / (double) (cols * rows); | |
254 double rmse = sqrt(mse); | |
255 double psnr = 20.0 * log(255.0 / rmse) / log(10.0); | |
256 FILE *print = print_psnr_only ? out : stderr; | |
257 if (!print_psnr_value_only) { | |
258 if (mse > 0.0) | |
259 fprintf(print, "PSNR: %lf dB\n", psnr); | |
260 else | |
261 fprintf(print, "PSNR: inf\n"); | |
262 fprintf(print, "RMS: %lf\n", rmse); | |
263 fprintf(print, "MSE: %lf\n", mse); | |
264 fprintf(print, "dmin, dmax: %d, %d\n", min, max); | |
265 } | |
266 else | |
267 fprintf(print, "%lf\n", mse > 0.0 ? psnr : 100.0); | |
268 } | |
269 | |
270 exit(0); | |
271 } |