Mercurial > hg > wm
annotate Meerwald/wm_koch_e.c @ 17:824d192e5614
bug fix; mse minimization still missing
| author | pmeerw@pan |
|---|---|
| date | Thu, 29 Jan 2009 18:28:05 +0100 |
| parents | 4987db85cfae |
| children | 3bdb67e76858 |
| rev | line source |
|---|---|
| 0 | 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 c; | |
| 31 int n; | |
| 32 | |
| 33 int seed; | |
| 34 int verbose = 0; | |
| 35 | |
| 36 int rows, cols, format; | |
| 37 gray maxval; | |
| 8 | 38 int row; |
| 0 | 39 |
| 40 int quantization = 0; | |
| 41 double quality = 0.0; | |
| 42 | |
| 43 struct coords *coords; | |
| 44 | |
| 45 gray **image; | |
| 46 double **dcts; | |
| 47 | |
| 48 progname = argv[0]; | |
| 49 | |
| 50 pgm_init(&argc, argv); wm_init(); | |
| 51 | |
| 52 while ((c = getopt(argc, argv, "h?i:l:o:q:s:v:")) != EOF) { | |
| 53 switch (c) { | |
| 54 case 'h': | |
| 55 case '?': | |
| 56 usage(); | |
| 57 break; | |
| 58 case 'l': | |
| 59 quality = atof(optarg); | |
| 60 if (quality <= 0.0) { | |
| 61 fprintf(stderr, "%s: signature strength factor %f out of range\n", progname, quality); | |
| 62 exit(1); | |
| 63 } | |
| 64 break; | |
| 65 case 'o': | |
| 66 if ((out = fopen(optarg, "wb")) == NULL) { | |
| 67 fprintf(stderr, "%s: unable to open output file %s\n", progname, optarg); | |
| 68 exit(1); | |
| 69 } | |
| 70 strcpy(output_name, optarg); | |
| 71 break; | |
| 72 case 'q': | |
| 73 quantization = atoi(optarg); | |
| 74 if (quantization <= 0 || quantization > 100) { | |
| 75 fprintf(stderr, "%s: quantization factor %d out of range\n", progname, quantization); | |
| 76 exit(1); | |
| 77 } | |
| 78 break; | |
| 79 case 's': | |
| 80 if ((sig = fopen(optarg, "r")) == NULL) { | |
| 81 fprintf(stderr, "%s: unable to open signature file %s\n", progname, optarg); | |
| 82 exit(1); | |
| 83 } | |
| 84 strcpy(signature_name, optarg); | |
| 85 break; | |
| 86 case 'v': | |
| 87 verbose = atoi(optarg); | |
| 88 if (verbose < 0) { | |
| 89 fprintf(stderr, "%s: verbosity level %d out of range\n", progname, verbose); | |
| 90 exit(1); | |
| 91 } | |
| 92 break; | |
| 93 } | |
| 94 } | |
| 95 | |
| 96 argc -= optind; | |
| 97 argv += optind; | |
| 98 | |
| 99 if (argc > 1) { | |
| 100 usage(); | |
| 101 exit(1); | |
| 102 } | |
| 103 | |
| 8 | 104 if (argc == 1 && *argv[0] != '-') { |
| 0 | 105 if ((in = fopen(argv[0], "rb")) == NULL) { |
| 106 fprintf(stderr, "%s: unable to open input file %s\n", progname, argv[0]); | |
| 107 exit(1); | |
| 108 } | |
| 109 else | |
| 110 strcpy(input_name, argv[0]); | |
| 8 | 111 } |
| 0 | 112 |
| 113 if (sig) { | |
| 114 char line[128]; | |
| 115 fgets(line, sizeof(line), sig); | |
| 116 if (strspn(line, "KCSG") >= 4) { | |
| 117 fscanf(sig, "%d\n", &nbit_signature); | |
| 118 if (quality == 0.0) | |
| 119 fscanf(sig, "%lf\n", &quality); | |
| 120 else | |
|
16
4987db85cfae
fix another scanf() warning
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
8
diff
changeset
|
121 fscanf(sig, "%*f\n"); |
| 0 | 122 if (quantization == 0) |
| 123 fscanf(sig, "%d\n", &quantization); | |
| 124 else | |
| 125 fscanf(sig, "%*d\n"); | |
| 126 fscanf(sig, "%d\n", &seed); | |
| 127 n_signature = NBITSTOBYTES(nbit_signature); | |
| 128 fread(signature, sizeof(char), n_signature, sig); | |
| 129 fscanf(sig, "\n"); | |
| 130 srandom(seed); | |
| 131 } | |
| 132 else { | |
| 133 fprintf(stderr, "%s: invalid signature file %s\n", progname, signature_name); | |
| 134 exit(1); | |
| 135 } | |
| 136 fclose(sig); | |
| 137 } | |
| 138 else { | |
| 139 fprintf(stderr, "%s: signature file not specified, use -s file option\n", progname); | |
| 140 exit(1); | |
| 141 } | |
| 142 | |
| 143 pgm_readpgminit(in, &cols, &rows, &maxval, &format); | |
| 144 | |
| 145 if (cols % NJPEG) { | |
| 146 fprintf(stderr, "%s: image width %d not a multiple of %d\n", progname, cols, NJPEG); | |
| 147 exit(1); | |
| 148 } | |
| 149 | |
| 150 if (rows % NJPEG) { | |
| 151 fprintf(stderr, "%s: image height %d not a multiple of %d\n", progname, rows, NJPEG); | |
| 152 exit(1); | |
| 153 } | |
| 154 | |
| 155 if ((cols * rows) / (NJPEG * NJPEG) < nbit_signature) { | |
| 156 fprintf(stderr, "%s: image not large enough to embed %d bits of signature\n", progname, nbit_signature); | |
| 157 exit(1); | |
| 158 } | |
| 159 | |
| 160 init_dct_8x8(); | |
| 161 init_quantum_JPEG_lumin(quantization); | |
| 162 | |
| 163 dcts = alloc_coeffs_8x8(); | |
| 164 | |
| 165 if ((coords = alloc_coords(nbit_signature)) == NULL) { | |
| 166 fprintf(stderr, "%s: unable to allocate memory\n", progname); | |
| 167 exit(1); | |
| 168 } | |
| 169 | |
| 170 image = pgm_allocarray(cols, rows); | |
| 171 | |
| 172 for (row = 0; row < rows; row++) | |
| 173 pgm_readpgmrow(in, image[row], cols, maxval, format); | |
| 174 | |
| 175 fclose(in); | |
| 176 | |
| 177 // embedding signature bits by modifying two coefficient relationship, | |
| 178 // one bit for each block | |
| 179 n = 0; | |
| 180 while (n < nbit_signature) { | |
| 181 int xb; | |
| 182 int yb; | |
| 183 int c1, c2; | |
| 184 double v1, v2; | |
| 185 double w1, w2; | |
| 186 double diff, abs_diff; | |
| 187 | |
| 188 // randomly select a block, check to get distinct blocks | |
| 189 // (don't watermark a block twice) | |
| 190 do { | |
| 191 xb = random() % (cols / NJPEG); | |
| 192 yb = random() % (rows / NJPEG); | |
| 193 } while (add_coord(coords, xb, yb) < 0); | |
| 194 | |
| 195 // do the forward 8x8 DCT of that block | |
| 196 fdct_block_8x8(image, xb * NJPEG, yb * NJPEG, dcts); | |
| 197 | |
| 198 // randomly select two distinct coefficients from block | |
| 199 // only accept coefficients in the middle frequency range | |
| 200 do { | |
| 201 c1 = (random() % (NJPEG * NJPEG - 2)) + 1; | |
| 202 c2 = (random() % (NJPEG * NJPEG - 2)) + 1; | |
| 203 } while (c1 == c2 || !is_middle_frequency_coeff_8x8(c1) || !is_middle_frequency_coeff_8x8(c2)); | |
| 204 | |
| 205 // quantize block according to quantization quality parameter | |
| 206 quantize_8x8(dcts); | |
| 207 | |
| 208 if (verbose > 0) | |
| 209 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"); | |
| 210 if (verbose > 5) | |
| 211 print_coeffs_8x8(dcts); | |
| 212 | |
| 213 v1 = dcts[c1 / NJPEG][c1 % NJPEG]; | |
| 214 v2 = dcts[c2 / NJPEG][c2 % NJPEG]; | |
| 215 | |
| 216 diff = fabs(v1) - fabs(v2); | |
| 217 abs_diff = (fabs(diff) + quality) / 2.0; | |
| 218 | |
| 219 // modify coefficient's relationship to embed signature bit | |
| 220 // using mean square error to minimize error | |
| 221 if (get_signature_bit(n)) { | |
| 222 if (diff < quality) { | |
| 223 // we have to impose the relationship, does not occur naturally | |
| 224 w1 = (v1 > 0.0) ? (v1 + abs_diff) : (v1 - abs_diff); | |
| 17 | 225 w2 = v2; |
| 0 | 226 } |
| 227 else { | |
| 228 w1 = v1; | |
| 229 w2 = v2; | |
| 230 } | |
| 231 } | |
| 232 else { | |
| 233 if (diff > -quality) { | |
| 234 // force the relationship | |
| 17 | 235 w1 = v1; |
| 0 | 236 w2 = (v2 > 0.0) ? (v2 + abs_diff) : (v2 - abs_diff); |
| 237 } | |
| 238 else { | |
| 239 w1 = v1; | |
| 240 w2 = v2; | |
| 241 } | |
| 242 } | |
| 243 | |
| 244 if (verbose > 1) | |
| 245 fprintf(stderr, " %f -> %f, %f -> %f\n", v1, w1, v2, w2); | |
| 246 | |
| 247 // put the changed coefficients back to black | |
| 248 dcts[c1 / NJPEG][c1 % NJPEG] = w1; | |
| 249 dcts[c2 / NJPEG][c2 % NJPEG] = w2; | |
| 250 | |
| 251 // the obvious :-) | |
| 252 dequantize_8x8(dcts); | |
| 253 | |
| 254 // do the inverse DCT on the modified 8x8 block | |
| 255 idct_block_8x8(dcts, image, xb * NJPEG, yb * NJPEG); | |
| 256 | |
| 257 n++; | |
| 258 } | |
| 259 | |
| 260 free_coeffs(dcts); | |
| 261 | |
| 262 pgm_writepgminit(out, cols, rows, maxval, 0); | |
| 263 for (row = 0; row < rows; row++) | |
| 264 pgm_writepgmrow(out, image[row], cols, maxval, 0); | |
| 265 | |
| 266 fclose(out); | |
| 267 | |
| 268 pgm_freearray(image, rows); | |
| 269 | |
| 270 exit(0); | |
| 271 } |
