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 no_orig = 0;
|
|
44 int c;
|
|
45
|
|
46 double error = 0.0;
|
|
47 int print_psnr = 0;
|
|
48 int print_psnr_only = 0;
|
|
49 int m = 16;
|
|
50
|
|
51 progname = argv[0];
|
|
52
|
|
53 pgm_init(&argc, argv); wm_init();
|
|
54
|
|
55 while ((c = getopt(argc, argv, "h?i:m:o:pP")) != EOF) {
|
|
56 switch (c) {
|
|
57 case 'h':
|
|
58 case '?':
|
|
59 usage();
|
|
60 break;
|
|
61 case 'i':
|
|
62 if (!strcmp(optarg, "-")) {
|
|
63 no_orig = 1;
|
|
64 strcpy(orig_name, "(zero)");
|
|
65 }
|
|
66 else {
|
|
67 if ((orig = fopen(optarg, "rb")) == NULL) {
|
|
68 fprintf(stderr, "%s: unable to open original image file %s\n", progname, optarg);
|
|
69 exit(1);
|
|
70 }
|
|
71 strcpy(orig_name, optarg);
|
|
72 }
|
|
73 break;
|
|
74 case 'm':
|
|
75 m = atoi(optarg);
|
|
76 if (m <= 0) {
|
|
77 fprintf(stderr, "%s: multiplication factor %d out of range\n", progname, m);
|
|
78 exit(1);
|
|
79 }
|
|
80 break;
|
|
81 case 'o':
|
|
82 if ((out = fopen(optarg, "wb")) == NULL) {
|
|
83 fprintf(stderr, "%s: unable to open output file %s\n", progname, optarg);
|
|
84 exit(1);
|
|
85 }
|
|
86 strcpy(output_name, optarg);
|
|
87 break;
|
|
88 case 'p':
|
|
89 print_psnr = 1;
|
|
90 break;
|
|
91 case 'P':
|
|
92 print_psnr_only = 1;
|
|
93 break;
|
|
94 }
|
|
95 }
|
|
96
|
|
97 argc -= optind;
|
|
98 argv += optind;
|
|
99
|
|
100 if (argc > 1) {
|
|
101 usage();
|
|
102 exit(1);
|
|
103 }
|
|
104
|
8
|
105 if (argc == 1 && *argv[0] != '-') {
|
0
|
106 if ((in = fopen(argv[0], "rb")) == NULL) {
|
|
107 fprintf(stderr, "%s: unable to open input file %s\n", progname, argv[0]);
|
|
108 exit(1);
|
|
109 }
|
|
110 else
|
|
111 strcpy(input_name, argv[0]);
|
8
|
112 }
|
|
113
|
0
|
114 if (!orig && !no_orig) {
|
|
115 fprintf(stderr, "%s: original image file not specified, using zero image\n", progname);
|
|
116 strcpy(orig_name, "(zero)");
|
|
117 no_orig = 1;
|
|
118 }
|
|
119
|
|
120 pgm_readpgminit(in, &in_cols, &in_rows, &in_maxval, &in_format);
|
|
121
|
|
122 if (!no_orig) {
|
|
123 pgm_readpgminit(orig, &orig_cols, &orig_rows, &orig_maxval, &orig_format);
|
|
124
|
|
125 if (in_cols != orig_cols || in_rows != orig_rows) {
|
|
126 fprintf(stderr, "%s: input image %s does not match dimensions of original image %s\n", progname, input_name, orig_name);
|
|
127 exit(1);
|
|
128 }
|
|
129 }
|
|
130
|
|
131 cols = in_cols;
|
|
132 rows = in_rows;
|
|
133 format = in_format;
|
|
134 maxval = in_maxval;
|
|
135
|
|
136 input_image = pgm_allocarray(cols, rows);
|
|
137 orig_image = pgm_allocarray(cols, rows);
|
|
138
|
|
139 init_dct_NxN(cols, rows);
|
|
140 input_dcts = alloc_coeffs(cols, rows);
|
|
141 orig_dcts = alloc_coeffs(cols, rows);
|
|
142 output = alloc_grays(cols, rows);
|
|
143
|
|
144 if (no_orig) {
|
|
145 for (row = 0; row < rows; row++) {
|
|
146 pgm_readpgmrow(in, input_image[row], cols, maxval, format);
|
|
147 bzero(orig_image[row], sizeof(gray) * cols);
|
|
148 }
|
|
149 }
|
|
150 else {
|
|
151 for (row = 0; row < rows; row++) {
|
|
152 pgm_readpgmrow(in, input_image[row], cols, maxval, format);
|
|
153 pgm_readpgmrow(orig, orig_image[row], cols, maxval, format);
|
|
154 }
|
|
155 }
|
|
156
|
|
157 fclose(in);
|
|
158 if (!no_orig)
|
|
159 fclose(orig);
|
|
160
|
|
161 fdct_NxN(input_image, input_dcts);
|
|
162 fdct_NxN(orig_image, orig_dcts);
|
|
163
|
|
164 for (row = 0; row < rows; row++)
|
|
165 for (col = 0; col < cols; col++) {
|
|
166 error += sqr(input_dcts[row][col] - orig_dcts[row][col]);
|
|
167 output[row][col] = PIXELRANGE(fabs(input_dcts[row][col] - orig_dcts[row][col]) * m);
|
|
168 }
|
|
169
|
|
170 if (!print_psnr_only) {
|
|
171 pgm_writepgminit(out, cols, rows, maxval, 0);
|
|
172 for (row = 0; row < rows; row++)
|
|
173 pgm_writepgmrow(out, output[row], cols, maxval, 0);
|
|
174
|
|
175 fclose(out);
|
|
176 }
|
|
177
|
|
178 pgm_freearray(input_image, rows);
|
|
179 pgm_freearray(orig_image, rows);
|
|
180 free_coeffs(input_dcts);
|
|
181 free_coeffs(orig_dcts);
|
|
182 free_grays(output);
|
|
183
|
|
184 if (print_psnr || print_psnr_only) {
|
|
185 double mse = error / (double) (cols * rows);
|
|
186 double rmse = sqrt(mse);
|
|
187 double psnr = 20.0 * log(255.0 / rmse) / log(10.0);
|
|
188 FILE *print = print_psnr_only ? out : stderr;
|
|
189 if (mse > 0.0)
|
|
190 fprintf(print, "PSNR: %lf dB\n", psnr);
|
|
191 else
|
|
192 fprintf(print, "PSNR: inf\n");
|
|
193 fprintf(print, "RMS: %lf\n", rmse);
|
|
194 fprintf(print, "MSE: %lf\n", mse);
|
|
195 }
|
|
196
|
|
197 exit(0);
|
|
198 }
|