comparison Meerwald/cmp_dct8x8.c @ 0:be303a3f5ea8

import
author Peter Meerwald <pmeerw@cosy.sbg.ac.at>
date Sun, 12 Aug 2007 13:14:34 +0200
parents
children f83ef905a63d
comparison
equal deleted inserted replaced
-1:000000000000 0:be303a3f5ea8
1 #include "wm.h"
2 #include "dct.h"
3 #include "gray.h"
4 #include "pgm.h"
5
6 char *progname;
7
8 void usage(void) {
9 fprintf(stderr, "usage: %s [-h] [-m n] [-o file] [-pP] -i file file\n\n", progname);
10 fprintf(stderr, "\t-h\t\tprint usage\n");
11 fprintf(stderr, "\t-i file\t\toriginal image file\n");
12 fprintf(stderr, "\t-m n\t\tmultiplication factor (default 16)\n");
13 fprintf(stderr, "\t-o file\t\toutput file\n");
14 fprintf(stderr, "\t-p\t\tprint PSNR, RMS and MSE\n");
15 fprintf(stderr, "\t-P\t\tonly print PSNR, RMS and MSE, no difference image\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
25 gray **input_image;
26 gray **orig_image;
27 double **input_dcts;
28 double **orig_dcts;
29 gray **output;
30
31 char output_name[MAXPATHLEN] = "(stdout)";
32 char input_name[MAXPATHLEN] = "(stdin)";
33 char orig_name[MAXPATHLEN];
34
35 int in_cols, in_rows, in_format;
36 gray in_maxval;
37 int orig_cols, orig_rows, orig_format;
38 gray orig_maxval;
39 int cols, rows, format;
40 gray maxval;
41 int col, row;
42
43 int i, j;
44 int c;
45
46 int m = 16;
47
48 int print_psnr = 0;
49 int print_psnr_only = 0;
50 double error = 0.0;
51
52 progname = argv[0];
53
54 pgm_init(&argc, argv); wm_init();
55
56 while ((c = getopt(argc, argv, "h?i:m:o:pP")) != EOF) {
57 switch (c) {
58 case 'h':
59 case '?':
60 usage();
61 break;
62 case 'i':
63 if ((orig = fopen(optarg, "rb")) == NULL) {
64 fprintf(stderr, "%s: unable to open original image file %s\n", progname, optarg);
65 exit(1);
66 }
67 strcpy(orig_name, optarg);
68 break;
69 case 'm':
70 m = atoi(optarg);
71 if (m <= 0) {
72 fprintf(stderr, "%s: multiplication factor %d out of range\n", progname, m);
73 exit(1);
74 }
75 break;
76 case 'o':
77 if ((out = fopen(optarg, "w")) == NULL) {
78 fprintf(stderr, "%s: unable to open output file %s\n", progname, optarg);
79 exit(1);
80 }
81 strcpy(output_name, optarg);
82 break;
83 case 'p':
84 print_psnr = 1;
85 break;
86 case 'P':
87 print_psnr_only = 1;
88 break;
89 }
90 }
91
92 argc -= optind;
93 argv += optind;
94
95 if (argc > 1) {
96 usage();
97 exit(1);
98 }
99
100 if (argc == 1 && *argv[0] != '-')
101 if ((in = fopen(argv[0], "rb")) == NULL) {
102 fprintf(stderr, "%s: unable to open input file %s\n", progname, argv[0]);
103 exit(1);
104 }
105 else
106 strcpy(input_name, argv[0]);
107
108 if (!orig) {
109 fprintf(stderr, "%s: original image file not specified, use -i file option\n", progname);
110 exit(1);
111 }
112
113 pgm_readpgminit(in, &in_cols, &in_rows, &in_maxval, &in_format);
114
115 pgm_readpgminit(orig, &orig_cols, &orig_rows, &orig_maxval, &orig_format);
116
117 if (in_cols != orig_cols || in_rows != orig_rows) {
118 fprintf(stderr, "%s: input image %s does not match dimensions of original image %s\n", progname, input_name, orig_name);
119 exit(1);
120 }
121
122 if (cols % NJPEG) {
123 fprintf(stderr, "%s: image width %d not a multiple of %d\n", progname, cols, NJPEG);
124 exit(1);
125 }
126
127 if (rows % NJPEG) {
128 fprintf(stderr, "%s: image height %d not a multiple of %d\n", progname, rows, NJPEG);
129 exit(1);
130 }
131
132 cols = in_cols;
133 rows = in_rows;
134 format = in_format;
135 maxval = in_maxval;
136
137 input_image = pgm_allocarray(cols, NJPEG);
138 orig_image = pgm_allocarray(cols, NJPEG);
139
140 init_dct_8x8();
141 input_dcts = alloc_coeffs_8x8();
142 orig_dcts = alloc_coeffs_8x8();
143 output = alloc_grays(cols, NJPEG);
144
145 if (!print_psnr_only)
146 pgm_writepgminit(out, cols, rows, maxval, 0);
147
148 for (row = 0; row < rows; row += NJPEG) {
149
150 for (i = 0; i < NJPEG; i++) {
151 pgm_readpgmrow(in, input_image[row % NJPEG], cols, maxval, format);
152 pgm_readpgmrow(orig, orig_image[row % NJPEG], cols, maxval, format);
153 }
154
155 for (col = 0; col < cols; col += NJPEG) {
156 fdct_block_8x8(input_image, col, 0, input_dcts);
157 fdct_block_8x8(orig_image, col, 0, orig_dcts);
158
159 for (i = 0; i < NJPEG; i++)
160 for (j = 0; j < NJPEG; j++)
161 error += sqr(input_dcts[j][i + cols] - orig_dcts[j][i + cols]);
162 output[j][i + col] = PIXELRANGE(fabs(input_dcts[j][i + cols] - orig_dcts[j][i + cols]) * m);
163 }
164
165 if (!print_psnr_only) {
166 for (i = 0; i < NJPEG; i++)
167 pgm_writepgmrow(out, output[i], cols, maxval, 0);
168 }
169
170 }
171 fclose(in);
172 fclose(orig);
173 if (!print_psnr_only)
174 fclose(out);
175
176 pgm_freearray(input_image, NJPEG);
177 pgm_freearray(orig_image, NJPEG);
178 free_coeffs(input_dcts);
179 free_coeffs(orig_dcts);
180 free_grays(output);
181
182 if (print_psnr || print_psnr_only) {
183 double mse = error / (double) (cols * rows);
184 double rmse = sqrt(mse);
185 double psnr = 20.0 * log(255.0 / rmse) / log(10.0);
186 FILE *print = print_psnr_only ? out : stderr;
187 if (mse > 0.0)
188 fprintf(print, "PSNR: %lf dB\n", psnr);
189 else
190 fprintf(print, "PSNR: inf\n");
191 fprintf(print, "RMS: %lf\n", rmse);
192 fprintf(print, "MSE: %lf\n", mse);
193 }
194
195 exit(0);
196 }

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