comparison Meerwald/cmp_ppm.c @ 10:aa7b4c3a7313

add cmp_ppm
author Peter Meerwald <pmeerw@cosy.sbg.ac.at>
date Thu, 01 May 2008 18:15:14 +0200
parents
children a5249f7d45f7
comparison
equal deleted inserted replaced
9:e496e3b113b6 10:aa7b4c3a7313
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");
14 fprintf(stderr, "\t-C\t\tprint PSNR value only\n");
15 fprintf(stderr, "\t-h\t\tprint usage\n");
16 fprintf(stderr, "\t-i file\t\toriginal image file\n");
17 fprintf(stderr, "\t-m n\t\tmultiplication factor (default 16)\n");
18 fprintf(stderr, "\t-o file\t\toutput file\n");
19 fprintf(stderr, "\t-p\t\tprint PSNR, RMS and MSE\n");
20 fprintf(stderr, "\t-P\t\tonly print PSNR, RMS and MSE, no difference image\n");
21 exit(0);
22 }
23
24 int main(int argc, char *argv[]) {
25
26 FILE *in = stdin;
27 FILE *out = stdout;
28 FILE *orig = NULL;
29
30 pixel **input_image;
31 pixel **orig_image;
32
33 char output_name[MAXPATHLEN] = "(stdout)";
34 char input_name[MAXPATHLEN] = "(stdin)";
35 char orig_name[MAXPATHLEN];
36
37 int in_cols, in_rows, in_format;
38 pixval in_maxval;
39 int orig_cols, orig_rows, orig_format;
40 pixval orig_maxval;
41 int cols, rows, format;
42 pixval maxval;
43 int col, row;
44
45 int c;
46
47 double error = 0.0;
48 int print_psnr = 0;
49 int print_psnr_only = 0;
50 int print_psnr_value_only = 0;
51
52 int m = 16;
53 int component = 0;
54 int component_default = LUMINANCE;
55 int min, max;
56
57 progname = argv[0];
58
59 ppm_init(&argc, argv);
60
61 #ifdef __EMX__
62 _fsetmode(in, "b");
63 _fsetmode(out, "b");
64 #endif
65
66 while ((c = getopt(argc, argv, "Cc:h?i:m:o:pP")) != EOF) {
67 switch (c) {
68 case 'C':
69 print_psnr_value_only = 1;
70 print_psnr_only = 1;
71 break;
72 case 'c':
73 if (!strcasecmp(optarg, "RED") || toupper(*optarg) == 'R')
74 component |= RED;
75 else if (!strcasecmp(optarg, "GREEN") || toupper(*optarg) == 'G')
76 component |= GREEN;
77 else if (!strcasecmp(optarg, "BLUE") || toupper(*optarg) == 'B')
78 component |= BLUE;
79 else if (!strcasecmp(optarg, "LUMINANCE") || toupper(*optarg) == 'L')
80 component |= LUMINANCE;
81 else
82 fprintf(stderr, "%s: unknown color component %s\n", progname, optarg);
83 break;
84 case 'h':
85 case '?':
86 usage();
87 break;
88 case 'i':
89 if ((orig = fopen(optarg, "rb")) == NULL) {
90 fprintf(stderr, "%s: unable to open original image file %s\n", progname, optarg);
91 exit(1);
92 }
93 strcpy(orig_name, optarg);
94 break;
95 case 'm':
96 m = atoi(optarg);
97 if (m <= 0) {
98 fprintf(stderr, "%s: multiplication factor %d out of range\n", progname, m);
99 exit(1);
100 }
101 break;
102 case 'o':
103 if ((out = fopen(optarg, "wb")) == NULL) {
104 fprintf(stderr, "%s: unable to open output file %s\n", progname, optarg);
105 exit(1);
106 }
107 strcpy(output_name, optarg);
108 break;
109 case 'p':
110 print_psnr = 1;
111 break;
112 case 'P':
113 print_psnr_only = 1;
114 break;
115 }
116 }
117
118 argc -= optind;
119 argv += optind;
120
121 if (argc > 1) {
122 usage();
123 exit(1);
124 }
125
126 if (argc == 1 && *argv[0] != '-') {
127 if ((in = fopen(argv[0], "rb")) == NULL) {
128 fprintf(stderr, "%s: unable to open input file %s\n", progname, argv[0]);
129 exit(1);
130 }
131 else
132 strcpy(input_name, argv[0]);
133 }
134
135 if (!orig) {
136 fprintf(stderr, "%s: original image file not specified, use -i file option\n", progname);
137 exit(1);
138 }
139
140 if (!component) {
141 if (component_default)
142 component = component_default;
143 else {
144 fprintf(stderr, "%s: color component(s) to compare not specified, use -c name option\n", progname);
145 exit(1);
146 }
147 }
148
149 if (component & LUMINANCE && component & (RED | GREEN | BLUE)) {
150 fprintf(stderr, "%s: unable to compare luminance AND color component\n", progname);
151 exit(1);
152 }
153
154 ppm_readppminit(in, &in_cols, &in_rows, &in_maxval, &in_format);
155
156 ppm_readppminit(orig, &orig_cols, &orig_rows, &orig_maxval, &orig_format);
157
158 if (in_cols != orig_cols || in_rows != orig_rows) {
159 fprintf(stderr, "%s: input image %s does not match dimensions of original image %s\n", progname, input_name, orig_name);
160 exit(1);
161 }
162
163 cols = in_cols;
164 rows = in_rows;
165 format = in_format;
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 }

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