0
|
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
|
8
|
100 if (argc == 1 && *argv[0] != '-') {
|
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]);
|
8
|
107 }
|
|
108
|
0
|
109 if (!orig) {
|
|
110 fprintf(stderr, "%s: original image file not specified, use -i file option\n", progname);
|
|
111 exit(1);
|
|
112 }
|
|
113
|
|
114 pgm_readpgminit(in, &in_cols, &in_rows, &in_maxval, &in_format);
|
|
115
|
|
116 pgm_readpgminit(orig, &orig_cols, &orig_rows, &orig_maxval, &orig_format);
|
|
117
|
|
118 if (in_cols != orig_cols || in_rows != orig_rows) {
|
|
119 fprintf(stderr, "%s: input image %s does not match dimensions of original image %s\n", progname, input_name, orig_name);
|
|
120 exit(1);
|
|
121 }
|
|
122
|
8
|
123 cols = in_cols;
|
|
124 rows = in_rows;
|
|
125 format = in_format;
|
|
126 maxval = in_maxval;
|
|
127
|
0
|
128 if (cols % NJPEG) {
|
|
129 fprintf(stderr, "%s: image width %d not a multiple of %d\n", progname, cols, NJPEG);
|
|
130 exit(1);
|
|
131 }
|
|
132
|
|
133 if (rows % NJPEG) {
|
|
134 fprintf(stderr, "%s: image height %d not a multiple of %d\n", progname, rows, NJPEG);
|
|
135 exit(1);
|
|
136 }
|
|
137
|
|
138 input_image = pgm_allocarray(cols, NJPEG);
|
|
139 orig_image = pgm_allocarray(cols, NJPEG);
|
|
140
|
|
141 init_dct_8x8();
|
|
142 input_dcts = alloc_coeffs_8x8();
|
|
143 orig_dcts = alloc_coeffs_8x8();
|
|
144 output = alloc_grays(cols, NJPEG);
|
|
145
|
|
146 if (!print_psnr_only)
|
|
147 pgm_writepgminit(out, cols, rows, maxval, 0);
|
|
148
|
|
149 for (row = 0; row < rows; row += NJPEG) {
|
|
150
|
|
151 for (i = 0; i < NJPEG; i++) {
|
|
152 pgm_readpgmrow(in, input_image[row % NJPEG], cols, maxval, format);
|
|
153 pgm_readpgmrow(orig, orig_image[row % NJPEG], cols, maxval, format);
|
|
154 }
|
|
155
|
|
156 for (col = 0; col < cols; col += NJPEG) {
|
|
157 fdct_block_8x8(input_image, col, 0, input_dcts);
|
|
158 fdct_block_8x8(orig_image, col, 0, orig_dcts);
|
|
159
|
|
160 for (i = 0; i < NJPEG; i++)
|
|
161 for (j = 0; j < NJPEG; j++)
|
|
162 error += sqr(input_dcts[j][i + cols] - orig_dcts[j][i + cols]);
|
|
163 output[j][i + col] = PIXELRANGE(fabs(input_dcts[j][i + cols] - orig_dcts[j][i + cols]) * m);
|
|
164 }
|
|
165
|
|
166 if (!print_psnr_only) {
|
|
167 for (i = 0; i < NJPEG; i++)
|
|
168 pgm_writepgmrow(out, output[i], cols, maxval, 0);
|
|
169 }
|
|
170
|
|
171 }
|
|
172 fclose(in);
|
|
173 fclose(orig);
|
|
174 if (!print_psnr_only)
|
|
175 fclose(out);
|
|
176
|
|
177 pgm_freearray(input_image, NJPEG);
|
|
178 pgm_freearray(orig_image, NJPEG);
|
|
179 free_coeffs(input_dcts);
|
|
180 free_coeffs(orig_dcts);
|
|
181 free_grays(output);
|
|
182
|
|
183 if (print_psnr || print_psnr_only) {
|
|
184 double mse = error / (double) (cols * rows);
|
|
185 double rmse = sqrt(mse);
|
|
186 double psnr = 20.0 * log(255.0 / rmse) / log(10.0);
|
|
187 FILE *print = print_psnr_only ? out : stderr;
|
|
188 if (mse > 0.0)
|
|
189 fprintf(print, "PSNR: %lf dB\n", psnr);
|
|
190 else
|
|
191 fprintf(print, "PSNR: inf\n");
|
|
192 fprintf(print, "RMS: %lf\n", rmse);
|
|
193 fprintf(print, "MSE: %lf\n", mse);
|
|
194 }
|
|
195
|
|
196 exit(0);
|
|
197 }
|