Mercurial > hg > wm
annotate Meerwald/cmp_ppm.c @ 22:d8551fb39a5e default tip
Added tag v0.6 for changeset 1c4ccd635a68
author | Peter Meerwald-Stadler <pmeerw@pmeerw.net> |
---|---|
date | Sat, 28 Jan 2023 23:57:51 +0100 |
parents | bd669312f068 |
children |
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; | |
20
bd669312f068
suppress warnings, fix link errors
Peter Meerwald-Stadler <pmeerw@pmeerw.net>
parents:
11
diff
changeset
|
42 int cols, rows; |
10 | 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 maxval = in_maxval; | |
167 | |
168 input_image = ppm_allocarray(cols, rows); | |
169 | |
170 for (row = 0; row < rows; row++) | |
171 ppm_readppmrow(in, input_image[row], cols, in_maxval, in_format); | |
172 | |
173 fclose(in); | |
174 | |
175 orig_image = ppm_allocarray(cols, rows); | |
176 | |
177 for (row = 0; row < rows; row++) | |
178 ppm_readppmrow(orig, orig_image[row], cols, orig_maxval, orig_format); | |
179 | |
180 fclose(orig); | |
181 | |
182 if (component & LUMINANCE) | |
183 min = max = abs(PPM_LUMIN(input_image[0][0]) - PPM_LUMIN(orig_image[0][0])); | |
184 else { | |
185 if (component & RED) | |
186 min = max = abs(PPM_GETR(input_image[0][0]) - PPM_GETR(orig_image[0][0])); | |
187 else if (component & GREEN) | |
188 min = max = abs(PPM_GETG(input_image[0][0]) - PPM_GETG(orig_image[0][0])); | |
189 else if (component & BLUE) | |
190 min = max = abs(PPM_GETB(input_image[0][0]) - PPM_GETB(orig_image[0][0])); | |
191 else | |
192 min = max = 0; | |
193 } | |
194 | |
195 for (row = 0; row < rows; row++) { | |
196 pixel *pi = input_image[row]; | |
197 pixel *po = orig_image[row]; | |
198 | |
199 for (col = 0; col < cols; col++) { | |
200 int diff=0; | |
201 | |
202 if (component & LUMINANCE) { | |
203 pixval l; | |
204 diff = abs(PPM_LUMIN(*pi) - PPM_LUMIN(*po)); | |
205 error += sqr(PPM_LUMIN(*pi) - PPM_LUMIN(*po)); | |
206 l = PIXELRANGE(ROUND(abs(PPM_LUMIN(*pi) - PPM_LUMIN(*po)) * m)); | |
207 PPM_ASSIGN(*pi, l, l, l); | |
208 } | |
209 else { | |
210 if (component & RED) { | |
211 diff = abs(PPM_GETR(*pi) - PPM_GETR(*po)); | |
212 error += sqr(PPM_GETR(*pi) - PPM_GETR(*po)); | |
213 PPM_PUTR(*pi, PIXELRANGE(abs(PPM_GETR(*pi) - PPM_GETR(*po)) * m)); | |
214 } | |
215 else | |
216 PPM_PUTR(*pi, 0); | |
217 if (component & GREEN) { | |
218 diff = abs(PPM_GETG(*pi) - PPM_GETG(*po)); | |
219 error += sqr(PPM_GETG(*pi) - PPM_GETG(*po)); | |
220 PPM_PUTG(*pi, PIXELRANGE(abs(PPM_GETG(*pi) - PPM_GETG(*po)) * m)); | |
221 } | |
222 else | |
223 PPM_PUTG(*pi, 0); | |
224 if (component & BLUE) { | |
225 diff = abs(PPM_GETB(*pi) - PPM_GETB(*po)); | |
226 error += sqr(PPM_GETB(*pi) - PPM_GETB(*po)); | |
227 PPM_PUTB(*pi, PIXELRANGE(abs(PPM_GETB(*pi) - PPM_GETB(*po)) * m)); | |
228 } | |
229 else | |
230 PPM_PUTB(*pi, 0); | |
231 } | |
232 | |
233 if (diff < min) min = diff; | |
234 if (diff > max) max = diff; | |
235 pi++; | |
236 po++; | |
237 } | |
238 } | |
239 | |
240 if (!print_psnr_only) { | |
241 ppm_writeppminit(out, cols, rows, maxval, 0); | |
242 for (row = 0; row < rows; row++) | |
243 ppm_writeppmrow(out, input_image[row], cols, maxval, 0); | |
244 | |
245 fclose(out); | |
246 } | |
247 | |
248 ppm_freearray(input_image, rows); | |
249 ppm_freearray(orig_image, rows); | |
250 | |
251 if (print_psnr || print_psnr_only) { | |
252 double mse = error / (double) (cols * rows); | |
253 double rmse = sqrt(mse); | |
254 double psnr = 20.0 * log(255.0 / rmse) / log(10.0); | |
255 FILE *print = print_psnr_only ? out : stderr; | |
256 if (!print_psnr_value_only) { | |
257 if (mse > 0.0) | |
258 fprintf(print, "PSNR: %lf dB\n", psnr); | |
259 else | |
260 fprintf(print, "PSNR: inf\n"); | |
261 fprintf(print, "RMS: %lf\n", rmse); | |
262 fprintf(print, "MSE: %lf\n", mse); | |
263 fprintf(print, "dmin, dmax: %d, %d\n", min, max); | |
264 } | |
265 else | |
266 fprintf(print, "%lf\n", mse > 0.0 ? psnr : 100.0); | |
267 } | |
268 | |
269 exit(0); | |
270 } |