Mercurial > hg > wm
comparison Meerwald/cmp_dct.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 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 | |
| 105 if (argc == 1 && *argv[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]); | |
| 112 | |
| 113 if (!orig && !no_orig) { | |
| 114 fprintf(stderr, "%s: original image file not specified, using zero image\n", progname); | |
| 115 strcpy(orig_name, "(zero)"); | |
| 116 no_orig = 1; | |
| 117 } | |
| 118 | |
| 119 pgm_readpgminit(in, &in_cols, &in_rows, &in_maxval, &in_format); | |
| 120 | |
| 121 if (!no_orig) { | |
| 122 pgm_readpgminit(orig, &orig_cols, &orig_rows, &orig_maxval, &orig_format); | |
| 123 | |
| 124 if (in_cols != orig_cols || in_rows != orig_rows) { | |
| 125 fprintf(stderr, "%s: input image %s does not match dimensions of original image %s\n", progname, input_name, orig_name); | |
| 126 exit(1); | |
| 127 } | |
| 128 } | |
| 129 | |
| 130 cols = in_cols; | |
| 131 rows = in_rows; | |
| 132 format = in_format; | |
| 133 maxval = in_maxval; | |
| 134 | |
| 135 input_image = pgm_allocarray(cols, rows); | |
| 136 orig_image = pgm_allocarray(cols, rows); | |
| 137 | |
| 138 init_dct_NxN(cols, rows); | |
| 139 input_dcts = alloc_coeffs(cols, rows); | |
| 140 orig_dcts = alloc_coeffs(cols, rows); | |
| 141 output = alloc_grays(cols, rows); | |
| 142 | |
| 143 if (no_orig) { | |
| 144 for (row = 0; row < rows; row++) { | |
| 145 pgm_readpgmrow(in, input_image[row], cols, maxval, format); | |
| 146 bzero(orig_image[row], sizeof(gray) * cols); | |
| 147 } | |
| 148 } | |
| 149 else { | |
| 150 for (row = 0; row < rows; row++) { | |
| 151 pgm_readpgmrow(in, input_image[row], cols, maxval, format); | |
| 152 pgm_readpgmrow(orig, orig_image[row], cols, maxval, format); | |
| 153 } | |
| 154 } | |
| 155 | |
| 156 fclose(in); | |
| 157 if (!no_orig) | |
| 158 fclose(orig); | |
| 159 | |
| 160 fdct_NxN(input_image, input_dcts); | |
| 161 fdct_NxN(orig_image, orig_dcts); | |
| 162 | |
| 163 for (row = 0; row < rows; row++) | |
| 164 for (col = 0; col < cols; col++) { | |
| 165 error += sqr(input_dcts[row][col] - orig_dcts[row][col]); | |
| 166 output[row][col] = PIXELRANGE(fabs(input_dcts[row][col] - orig_dcts[row][col]) * m); | |
| 167 } | |
| 168 | |
| 169 if (!print_psnr_only) { | |
| 170 pgm_writepgminit(out, cols, rows, maxval, 0); | |
| 171 for (row = 0; row < rows; row++) | |
| 172 pgm_writepgmrow(out, output[row], cols, maxval, 0); | |
| 173 | |
| 174 fclose(out); | |
| 175 } | |
| 176 | |
| 177 pgm_freearray(input_image, rows); | |
| 178 pgm_freearray(orig_image, rows); | |
| 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 } |
