Mercurial > hg > wm
comparison Meerwald/wm_koch_e.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", 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\toutput (watermarked) file\n"); | |
| 14 fprintf(stderr, "\t-q n\t\tquantization (JPEG quality) factor\n"); | |
| 15 fprintf(stderr, "\t-s file\t\tsignature to embed in input image\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 char signature_name[MAXPATHLEN]; | |
| 27 char input_name[MAXPATHLEN] = "(stdin)"; | |
| 28 char output_name[MAXPATHLEN] = "(stdout)"; | |
| 29 | |
| 30 int i; | |
| 31 int j; | |
| 32 int c; | |
| 33 int n; | |
| 34 int x; | |
| 35 int y; | |
| 36 | |
| 37 int seed; | |
| 38 int verbose = 0; | |
| 39 | |
| 40 int rows, cols, format; | |
| 41 gray maxval; | |
| 42 int row, col; | |
| 43 | |
| 44 int quantization = 0; | |
| 45 double quality = 0.0; | |
| 46 | |
| 47 struct coords *coords; | |
| 48 | |
| 49 gray **image; | |
| 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 if (quality == 0.0) | |
| 122 fscanf(sig, "%lf\n", &quality); | |
| 123 else | |
| 124 fscanf(sig, "%*lf\n"); | |
| 125 if (quantization == 0) | |
| 126 fscanf(sig, "%d\n", &quantization); | |
| 127 else | |
| 128 fscanf(sig, "%*d\n"); | |
| 129 fscanf(sig, "%d\n", &seed); | |
| 130 n_signature = NBITSTOBYTES(nbit_signature); | |
| 131 fread(signature, sizeof(char), n_signature, sig); | |
| 132 fscanf(sig, "\n"); | |
| 133 srandom(seed); | |
| 134 } | |
| 135 else { | |
| 136 fprintf(stderr, "%s: invalid signature file %s\n", progname, signature_name); | |
| 137 exit(1); | |
| 138 } | |
| 139 fclose(sig); | |
| 140 } | |
| 141 else { | |
| 142 fprintf(stderr, "%s: signature file not specified, use -s file option\n", progname); | |
| 143 exit(1); | |
| 144 } | |
| 145 | |
| 146 pgm_readpgminit(in, &cols, &rows, &maxval, &format); | |
| 147 | |
| 148 if (cols % NJPEG) { | |
| 149 fprintf(stderr, "%s: image width %d not a multiple of %d\n", progname, cols, NJPEG); | |
| 150 exit(1); | |
| 151 } | |
| 152 | |
| 153 if (rows % NJPEG) { | |
| 154 fprintf(stderr, "%s: image height %d not a multiple of %d\n", progname, rows, NJPEG); | |
| 155 exit(1); | |
| 156 } | |
| 157 | |
| 158 if ((cols * rows) / (NJPEG * NJPEG) < nbit_signature) { | |
| 159 fprintf(stderr, "%s: image not large enough to embed %d bits of signature\n", progname, nbit_signature); | |
| 160 exit(1); | |
| 161 } | |
| 162 | |
| 163 init_dct_8x8(); | |
| 164 init_quantum_JPEG_lumin(quantization); | |
| 165 | |
| 166 dcts = alloc_coeffs_8x8(); | |
| 167 | |
| 168 if ((coords = alloc_coords(nbit_signature)) == NULL) { | |
| 169 fprintf(stderr, "%s: unable to allocate memory\n", progname); | |
| 170 exit(1); | |
| 171 } | |
| 172 | |
| 173 image = pgm_allocarray(cols, rows); | |
| 174 | |
| 175 for (row = 0; row < rows; row++) | |
| 176 pgm_readpgmrow(in, image[row], cols, maxval, format); | |
| 177 | |
| 178 fclose(in); | |
| 179 | |
| 180 // embedding signature bits by modifying two coefficient relationship, | |
| 181 // one bit for each block | |
| 182 n = 0; | |
| 183 while (n < nbit_signature) { | |
| 184 int xb; | |
| 185 int yb; | |
| 186 int c1, c2; | |
| 187 double v1, v2; | |
| 188 double w1, w2; | |
| 189 double diff, abs_diff; | |
| 190 | |
| 191 // randomly select a block, check to get distinct blocks | |
| 192 // (don't watermark a block twice) | |
| 193 do { | |
| 194 xb = random() % (cols / NJPEG); | |
| 195 yb = random() % (rows / NJPEG); | |
| 196 } while (add_coord(coords, xb, yb) < 0); | |
| 197 | |
| 198 // do the forward 8x8 DCT of that block | |
| 199 fdct_block_8x8(image, xb * NJPEG, yb * NJPEG, dcts); | |
| 200 | |
| 201 // randomly select two distinct coefficients from block | |
| 202 // only accept coefficients in the middle frequency range | |
| 203 do { | |
| 204 c1 = (random() % (NJPEG * NJPEG - 2)) + 1; | |
| 205 c2 = (random() % (NJPEG * NJPEG - 2)) + 1; | |
| 206 } while (c1 == c2 || !is_middle_frequency_coeff_8x8(c1) || !is_middle_frequency_coeff_8x8(c2)); | |
| 207 | |
| 208 // quantize block according to quantization quality parameter | |
| 209 quantize_8x8(dcts); | |
| 210 | |
| 211 if (verbose > 0) | |
| 212 fprintf(stderr, "%d: quantized DCT block (x %d/y %d), modifying (x %d/y %d), (x %d/y %d) for %s\n", n, xb * NJPEG, yb * NJPEG, c1 % NJPEG, c1 / NJPEG, c2 % NJPEG, c2 / NJPEG, get_signature_bit(n) ? "HIGH" : "LOW"); | |
| 213 if (verbose > 5) | |
| 214 print_coeffs_8x8(dcts); | |
| 215 | |
| 216 v1 = dcts[c1 / NJPEG][c1 % NJPEG]; | |
| 217 v2 = dcts[c2 / NJPEG][c2 % NJPEG]; | |
| 218 | |
| 219 diff = fabs(v1) - fabs(v2); | |
| 220 abs_diff = (fabs(diff) + quality) / 2.0; | |
| 221 | |
| 222 // modify coefficient's relationship to embed signature bit | |
| 223 // using mean square error to minimize error | |
| 224 if (get_signature_bit(n)) { | |
| 225 if (diff < quality) { | |
| 226 // we have to impose the relationship, does not occur naturally | |
| 227 w1 = (v1 > 0.0) ? (v1 + abs_diff) : (v1 - abs_diff); | |
| 228 w2 = (v2 > 0.0) ? (v2 - abs_diff) : (v2 + abs_diff); | |
| 229 } | |
| 230 else { | |
| 231 w1 = v1; | |
| 232 w2 = v2; | |
| 233 } | |
| 234 } | |
| 235 else { | |
| 236 if (diff > -quality) { | |
| 237 // force the relationship | |
| 238 w1 = (v1 > 0.0) ? (v1 - abs_diff) : (v1 + abs_diff); | |
| 239 w2 = (v2 > 0.0) ? (v2 + abs_diff) : (v2 - abs_diff); | |
| 240 } | |
| 241 else { | |
| 242 w1 = v1; | |
| 243 w2 = v2; | |
| 244 } | |
| 245 } | |
| 246 | |
| 247 if (verbose > 1) | |
| 248 fprintf(stderr, " %f -> %f, %f -> %f\n", v1, w1, v2, w2); | |
| 249 | |
| 250 // put the changed coefficients back to black | |
| 251 dcts[c1 / NJPEG][c1 % NJPEG] = w1; | |
| 252 dcts[c2 / NJPEG][c2 % NJPEG] = w2; | |
| 253 | |
| 254 // the obvious :-) | |
| 255 dequantize_8x8(dcts); | |
| 256 | |
| 257 // do the inverse DCT on the modified 8x8 block | |
| 258 idct_block_8x8(dcts, image, xb * NJPEG, yb * NJPEG); | |
| 259 | |
| 260 n++; | |
| 261 } | |
| 262 | |
| 263 free_coeffs(dcts); | |
| 264 | |
| 265 pgm_writepgminit(out, cols, rows, maxval, 0); | |
| 266 for (row = 0; row < rows; row++) | |
| 267 pgm_writepgmrow(out, image[row], cols, maxval, 0); | |
| 268 | |
| 269 fclose(out); | |
| 270 | |
| 271 pgm_freearray(image, rows); | |
| 272 | |
| 273 exit(0); | |
| 274 } |
