Mercurial > hg > wm
comparison Meerwald/wm_koch_d.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 "signature.h" | |
| 4 #include "coord.h" | |
| 5 #include "pgm.h" | |
| 6 | |
| 7 char *progname; | |
| 8 | |
| 9 void usage(void) { | |
| 10 fprintf(stderr, "usage: %s [-h] [-l n] [-o file] [-q n] [-v n] -s file file\n\n", progname); | |
| 11 fprintf(stderr, "\t-h\t\tprint usage\n"); | |
| 12 fprintf(stderr, "\t-l n\t\tsignature robustness factor\n"); | |
| 13 fprintf(stderr, "\t-o file\t\textracted signature file\n"); | |
| 14 fprintf(stderr, "\t-q n\t\tquantization (JPEG quality) factor\n"); | |
| 15 fprintf(stderr, "\t-s file\t\toriginal signature file\n"); | |
| 16 fprintf(stderr, "\t-v n\t\tverbosity level\n"); | |
| 17 exit(0); | |
| 18 } | |
| 19 | |
| 20 int main(int argc, char *argv[]) { | |
| 21 | |
| 22 FILE *in = stdin; | |
| 23 FILE *out = stdout; | |
| 24 FILE *sig = NULL; | |
| 25 | |
| 26 gray **image; | |
| 27 struct coords *coords; | |
| 28 | |
| 29 char signature_name[MAXPATHLEN]; | |
| 30 char input_name[MAXPATHLEN] = "(stdin)"; | |
| 31 char output_name[MAXPATHLEN] = "(stdout)"; | |
| 32 | |
| 33 int i; | |
| 34 int j; | |
| 35 int c; | |
| 36 int n; | |
| 37 int x; | |
| 38 int y; | |
| 39 | |
| 40 int rows, cols, format; | |
| 41 gray maxval; | |
| 42 int row, col; | |
| 43 | |
| 44 int seed; | |
| 45 int verbose = 0; | |
| 46 | |
| 47 double quality = 0.0; | |
| 48 int quantization = 0; | |
| 49 | |
| 50 double **dcts; | |
| 51 | |
| 52 progname = argv[0]; | |
| 53 | |
| 54 pgm_init(&argc, argv); wm_init(); | |
| 55 | |
| 56 while ((c = getopt(argc, argv, "h?i:l:o:q:s:v:")) != EOF) { | |
| 57 switch (c) { | |
| 58 case 'h': | |
| 59 case '?': | |
| 60 usage(); | |
| 61 break; | |
| 62 case 'l': | |
| 63 quality = atof(optarg); | |
| 64 if (quality <= 0.0) { | |
| 65 fprintf(stderr, "%s: signature strength factor %f out of range\n", progname, quality); | |
| 66 exit(1); | |
| 67 } | |
| 68 break; | |
| 69 case 'o': | |
| 70 if ((out = fopen(optarg, "wb")) == NULL) { | |
| 71 fprintf(stderr, "%s: unable to open output file %s\n", progname, optarg); | |
| 72 exit(1); | |
| 73 } | |
| 74 strcpy(output_name, optarg); | |
| 75 break; | |
| 76 case 'q': | |
| 77 quantization = atoi(optarg); | |
| 78 if (quantization <= 0 || quantization > 100) { | |
| 79 fprintf(stderr, "%s: quantization factor %d out of range\n", progname, quantization); | |
| 80 exit(1); | |
| 81 } | |
| 82 break; | |
| 83 case 's': | |
| 84 if ((sig = fopen(optarg, "r")) == NULL) { | |
| 85 fprintf(stderr, "%s: unable to open signature file %s\n", progname, optarg); | |
| 86 exit(1); | |
| 87 } | |
| 88 strcpy(signature_name, optarg); | |
| 89 break; | |
| 90 case 'v': | |
| 91 verbose = atoi(optarg); | |
| 92 if (verbose < 0) { | |
| 93 fprintf(stderr, "%s: verbosity level %d out of range\n", progname, verbose); | |
| 94 exit(1); | |
| 95 } | |
| 96 break; | |
| 97 } | |
| 98 } | |
| 99 | |
| 100 argc -= optind; | |
| 101 argv += optind; | |
| 102 | |
| 103 if (argc > 1) { | |
| 104 usage(); | |
| 105 exit(1); | |
| 106 } | |
| 107 | |
| 108 if (argc == 1 && *argv[0] != '-') | |
| 109 if ((in = fopen(argv[0], "rb")) == NULL) { | |
| 110 fprintf(stderr, "%s: unable to open input file %s\n", progname, argv[0]); | |
| 111 exit(1); | |
| 112 } | |
| 113 else | |
| 114 strcpy(input_name, argv[0]); | |
| 115 | |
| 116 if (sig) { | |
| 117 char line[128]; | |
| 118 fgets(line, sizeof(line), sig); | |
| 119 if (strspn(line, "KCSG") >= 4) { | |
| 120 fscanf(sig, "%d\n", &nbit_signature); | |
| 121 n_signature = NBITSTOBYTES(nbit_signature); | |
| 122 if (quality == 0.0) | |
| 123 fscanf(sig, "%lf\n", &quality); | |
| 124 else | |
| 125 fscanf(sig, "%*lf\n"); | |
| 126 if (quantization == 0) | |
| 127 fscanf(sig, "%d\n", &quantization); | |
| 128 else | |
| 129 fscanf(sig, "%*d\n"); | |
| 130 fscanf(sig, "%d\n", &seed); | |
| 131 srandom(seed); | |
| 132 fread(signature, sizeof(char), n_signature, sig); | |
| 133 init_signature_bits(); | |
| 134 fscanf(sig, "\n"); | |
| 135 } | |
| 136 else { | |
| 137 fprintf(stderr, "%s: invalid signature file %s\n", progname, signature_name); | |
| 138 exit(1); | |
| 139 } | |
| 140 fclose(sig); | |
| 141 } | |
| 142 else { | |
| 143 fprintf(stderr, "%s: signature file not specified, use -s file option\n", progname); | |
| 144 exit(1); | |
| 145 } | |
| 146 | |
| 147 pgm_readpgminit(in, &cols, &rows, &maxval, &format); | |
| 148 | |
| 149 if (cols % NJPEG) { | |
| 150 fprintf(stderr, "%s: image width %d not a multiple of %d\n", progname, cols, NJPEG); | |
| 151 exit(1); | |
| 152 } | |
| 153 | |
| 154 if (rows % NJPEG) { | |
| 155 fprintf(stderr, "%s: image height %d not a multiple of %d\n", progname, rows, NJPEG); | |
| 156 exit(1); | |
| 157 } | |
| 158 | |
| 159 if ((rows * cols) / (NJPEG * NJPEG) < nbit_signature) { | |
| 160 fprintf(stderr, "%s: image too small to extract %d bits of signature\n", progname, nbit_signature); | |
| 161 exit(1); | |
| 162 } | |
| 163 | |
| 164 init_dct_8x8(); | |
| 165 init_quantum_JPEG_lumin(quantization); | |
| 166 | |
| 167 dcts = alloc_coeffs_8x8(); | |
| 168 | |
| 169 if ((coords = alloc_coords(nbit_signature)) == NULL) { | |
| 170 fprintf(stderr, "%s: unable to allocate memory\n", progname); | |
| 171 exit(1); | |
| 172 } | |
| 173 | |
| 174 image = pgm_allocarray(cols, rows); | |
| 175 | |
| 176 for (row = 0; row < rows; row++) | |
| 177 pgm_readpgmrow(in, image[row], cols, maxval, format); | |
| 178 | |
| 179 fclose(in); | |
| 180 | |
| 181 n = 0; | |
| 182 while (n < nbit_signature) { | |
| 183 int xb; | |
| 184 int yb; | |
| 185 int c1, c2; | |
| 186 double v1, v2; | |
| 187 double q = 1.0; | |
| 188 | |
| 189 do { | |
| 190 xb = random() % (cols / NJPEG); | |
| 191 yb = random() % (rows / NJPEG); | |
| 192 } while (add_coord(coords, xb, yb) < 0); | |
| 193 | |
| 194 fdct_block_8x8(image, xb * NJPEG, yb * NJPEG, dcts); | |
| 195 | |
| 196 do { | |
| 197 c1 = (random() % (NJPEG * NJPEG - 2)) + 1; | |
| 198 c2 = (random() % (NJPEG * NJPEG - 2)) + 1; | |
| 199 } while (c1 == c2 || !is_middle_frequency_coeff_8x8(c1) || !is_middle_frequency_coeff_8x8(c2)); | |
| 200 | |
| 201 quantize_8x8(dcts); | |
| 202 | |
| 203 if (verbose >= 1) | |
| 204 fprintf(stderr, "%d: quantized DCT block (x %d/y %d), extracting (x %d/y %d), (x %d/y %d) ", n, xb * NJPEG, yb * NJPEG, c1 % NJPEG, c1 / NJPEG, c2 % NJPEG, c2 / NJPEG); | |
| 205 | |
| 206 v1 = dcts[c1 / NJPEG][c1 % NJPEG]; | |
| 207 v2 = dcts[c2 / NJPEG][c2 % NJPEG]; | |
| 208 | |
| 209 if (fabs(v1) > fabs(v2)) { | |
| 210 set_signature_bit(n, 1); | |
| 211 if (verbose >= 1) | |
| 212 fprintf(stderr, "HIGH\n"); | |
| 213 } | |
| 214 else { | |
| 215 set_signature_bit(n, 0); | |
| 216 if (verbose >= 1) | |
| 217 fprintf(stderr, "LOW\n"); | |
| 218 } | |
| 219 | |
| 220 if (verbose >= 2) | |
| 221 print_coeffs_8x8(dcts); | |
| 222 | |
| 223 n++; | |
| 224 } | |
| 225 | |
| 226 fprintf(out, "KCWM\n"); | |
| 227 fprintf(out, "%d\n", nbit_signature); | |
| 228 fwrite(signature, sizeof(char), n_signature, out); | |
| 229 fprintf(out, "\n"); | |
| 230 | |
| 231 fclose(out); | |
| 232 | |
| 233 free_coeffs(dcts); | |
| 234 | |
| 235 pbm_freearray(image, rows); | |
| 236 | |
| 237 exit(0); | |
| 238 } |
