Mercurial > hg > wm
comparison Meerwald/wm_bruyn_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 "signature.h" | |
3 #include "coord.h" | |
4 #include "gray.h" | |
5 #include "sort.h" | |
6 #include "bruyn_common.h" | |
7 #include "pgm.h" | |
8 | |
9 char *progname; | |
10 | |
11 // prints out program's parameters | |
12 void usage(void) { | |
13 fprintf(stderr, "usage: %s [-b n] [-h] [-k] [-n n] [-o file] [-pP n] [-q n] [-tT n] [-v n] -s file file\n", progname); | |
14 fprintf(stderr, "\t-b n\t\tblock size\n"); | |
15 fprintf(stderr, "\t-h\t\tprint usage\n"); | |
16 fprintf(stderr, "\t-k\t\tdisable block skipping\n"); | |
17 fprintf(stderr, "\t-n n\t\tnumber of signature bits to embed\n"); | |
18 fprintf(stderr, "\t-o file\t\toutput (watermarked) file\n"); | |
19 fprintf(stderr, "\t-p n\t\tpattern type for zone 1\n"); | |
20 fprintf(stderr, "\t-P n\t\tpattern type for zone 2\n"); | |
21 fprintf(stderr, "\t-q n\t\tsignature strength\n"); | |
22 fprintf(stderr, "\t-s file\t\tsignature to embed in input image\n"); | |
23 fprintf(stderr, "\t-t n\t\tthreshold for noise\n"); | |
24 fprintf(stderr, "\t-T n\t\tthreshold for slope\n"); | |
25 fprintf(stderr, "\t-v n\t\tverbosity level\n"); | |
26 exit(0); | |
27 } | |
28 | |
29 int main(int argc, char *argv[]) { | |
30 FILE *in = stdin; | |
31 FILE *out = stdout; | |
32 FILE *sig = NULL; | |
33 | |
34 gray** image; | |
35 gray **block; | |
36 gray **zone; | |
37 gray **category1, **category2; | |
38 gray maxval; | |
39 double *slope; | |
40 int rows, cols, colors, format; | |
41 int c; | |
42 int i, j; | |
43 int r; | |
44 int n; | |
45 int col, row; | |
46 int bwidth, bheight; | |
47 int n_block; | |
48 int skipping = 0; | |
49 | |
50 char signature_name[MAXPATHLEN]; | |
51 char input_name[MAXPATHLEN] = "(stdin)"; | |
52 char output_name[MAXPATHLEN] = "(stdout)"; | |
53 | |
54 double quality = 0.0; | |
55 double threshold_noise = 0.0; | |
56 double threshold_slope = 0.0; | |
57 int pattern1 = 0; | |
58 int pattern2 = 0; | |
59 int blocksize = 0; | |
60 int seed; | |
61 | |
62 int verbose = 0; | |
63 | |
64 struct coords *coords; | |
65 | |
66 progname = argv[0]; | |
67 | |
68 pgm_init(&argc, argv); wm_init(); | |
69 | |
70 // parse command line and set options | |
71 while ((c = getopt(argc, argv, "b:h?n:o:p:P:q:s:t:T:v:k")) != EOF) { | |
72 switch (c) { | |
73 case 'k': | |
74 skipping = 1; | |
75 break; | |
76 case 'h': | |
77 case '?': | |
78 usage(); | |
79 break; | |
80 case 'n': | |
81 nbit_signature = atoi(optarg); | |
82 if (nbit_signature <= 0 || nbit_signature > NBITSIGNATURE) { | |
83 fprintf(stderr, "%s: invalid signature length %d\n", progname, nbit_signature); | |
84 exit(1); | |
85 } | |
86 break; | |
87 case 'o': | |
88 if ((out = fopen(optarg, "wb")) == NULL) { | |
89 fprintf(stderr, "%s: unable to open output file %s\n", progname, optarg); | |
90 exit(1); | |
91 } | |
92 strcpy(output_name, optarg); | |
93 break; | |
94 case 'p': | |
95 pattern1 = atoi(optarg); | |
96 if (pattern1 <= 0 || pattern1 > NPATTERN) { | |
97 fprintf(stderr, "%s: pattern type out of range\n", progname); | |
98 exit(1); | |
99 } | |
100 break; | |
101 case 'P': | |
102 pattern2 = atoi(optarg); | |
103 if (pattern2 <= 0 || pattern2 > 3) { | |
104 fprintf(stderr, "%s: pattern type out of range\n", progname); | |
105 exit(1); | |
106 } | |
107 break; | |
108 case 'q': | |
109 quality = atof(optarg); | |
110 if (quality <= 0) { | |
111 fprintf(stderr, "%s: quality factor %f out of range\n", progname, quality); | |
112 } | |
113 break; | |
114 case 's': | |
115 if ((sig = fopen(optarg, "r")) == NULL) { | |
116 fprintf(stderr, "%s: unable to open signature file %s\n", progname, optarg); | |
117 exit(1); | |
118 } | |
119 strcpy(signature_name, optarg); | |
120 break; | |
121 case 't': | |
122 threshold_noise = atof(optarg); | |
123 if (threshold_noise <= 0) { | |
124 fprintf(stderr, "%s: noise threshold %f out of range\n", progname, threshold_noise); | |
125 } | |
126 break; | |
127 case 'T': | |
128 threshold_slope = atof(optarg); | |
129 if (threshold_slope <= 0) { | |
130 fprintf(stderr, "%s: slope threshold %f out of range\n", progname, threshold_slope); | |
131 } | |
132 break; | |
133 case 'v': | |
134 verbose = atoi(optarg); | |
135 if (verbose < 0) { | |
136 fprintf(stderr, "%s: verbosity level %d out of range\n",progname, verbose); | |
137 exit(1); | |
138 } | |
139 break; | |
140 } | |
141 } | |
142 | |
143 argc -= optind; | |
144 argv += optind; | |
145 | |
146 if (argc > 1) { | |
147 usage(); | |
148 exit(1); | |
149 } | |
150 | |
151 // open input image file or read from stdin | |
152 if (argc == 1 && *argv[0] != '-') | |
153 if ((in = fopen(argv[0], "rb")) == NULL) { | |
154 fprintf(stderr, "%s: unable to open input file %s\n", progname, argv[0]); | |
155 exit(1); | |
156 } | |
157 else | |
158 strcpy(input_name, argv[0]); | |
159 | |
160 // read signature file and set options | |
161 // command line options override signature file options | |
162 if (sig) { | |
163 char line[128]; | |
164 fgets(line, sizeof(line), sig); | |
165 if (strspn(line, "BRSG") >= 4) { | |
166 if (nbit_signature == 0) | |
167 fscanf(sig, "%d\n", &nbit_signature); | |
168 else | |
169 fscanf(sig, "%*d\n"); | |
170 if (skipping == 0) | |
171 fscanf(sig, "%d\n", &skipping); | |
172 else | |
173 fscanf(sig, "%*d\n"); | |
174 if (pattern1 == 0) | |
175 fscanf(sig, "%d\n", &pattern1); | |
176 else | |
177 fscanf(sig, "%*d\n"); | |
178 if (pattern2 == 0) | |
179 fscanf(sig, "%d\n", &pattern2); | |
180 else | |
181 fscanf(sig, "%*d\n"); | |
182 if (quality == 0.0) | |
183 fscanf(sig, "%lf\n", &quality); | |
184 else | |
185 fscanf(sig, "%*lf\n"); | |
186 if (threshold_noise == 0.0) | |
187 fscanf(sig, "%lf\n", &threshold_noise); | |
188 else | |
189 fscanf(sig, "%*lf\n"); | |
190 if (threshold_slope == 0.0) | |
191 fscanf(sig, "%lf\n", &threshold_slope); | |
192 else | |
193 fscanf(sig, "%*lf\n"); | |
194 if (blocksize == 0) | |
195 fscanf(sig, "%d\n", &blocksize); | |
196 else | |
197 fscanf(sig, "%*d\n"); | |
198 fscanf(sig, "%d\n", &seed); | |
199 srandom(seed); | |
200 n_signature = NBITSTOBYTES(nbit_signature); | |
201 fread(signature, sizeof(char), n_signature, sig); | |
202 fscanf(sig, "\n"); | |
203 } | |
204 else { | |
205 fprintf(stderr, "%s: invalid signature file %s\n", progname, signature_name); | |
206 exit(1); | |
207 } | |
208 fclose(sig); | |
209 } | |
210 else { | |
211 fprintf(stderr, "%s: signature file not specified, use -s file option\n", progname); | |
212 exit(1); | |
213 } | |
214 | |
215 if (pattern1 <= 0 || pattern2 <= 0 || pattern1 > NPATTERN || pattern2 > NPATTERN) { | |
216 fprintf(stderr, "%s: invalid pattern type specified\n"); | |
217 exit(1); | |
218 } | |
219 | |
220 // read dimensions of input image file | |
221 pgm_readpgminit(in, &cols, &rows, &maxval, &format); | |
222 | |
223 // see if we can embed all signature bits | |
224 // we want at least half of the blocks untouched | |
225 if (((rows / blocksize) * (cols / blocksize)) < nbit_signature / 2) { | |
226 fprintf(stderr, "%s: image not large enough to embed %d bits of signature\n", progname, nbit_signature); | |
227 exit(1); | |
228 } | |
229 n_block = blocksize * blocksize; | |
230 | |
231 // allocate structure to remember which blocks we already touched, | |
232 // allow plenty of room to skip over blocks | |
233 if ((coords = alloc_coords(nbit_signature * 2)) == NULL) { | |
234 fprintf(stderr, "%s: unable to allocate memory\n", progname); | |
235 exit(1); | |
236 } | |
237 | |
238 // read in input image file | |
239 image = pgm_allocarray(cols, rows); | |
240 for (row = 0; row < rows; row++) | |
241 pgm_readpgmrow(in, image[row], cols, maxval, format); | |
242 | |
243 fclose(in); | |
244 | |
245 row = 0; | |
246 col = 0; | |
247 | |
248 // allocate memory for one block | |
249 block = alloc_grays(blocksize, blocksize); | |
250 | |
251 // allocate memory for zone classification | |
252 zone = alloc_grays(blocksize, blocksize); | |
253 | |
254 // allocate memory for category classification | |
255 category1 = alloc_grays(blocksize, blocksize); | |
256 category2 = alloc_grays(blocksize, blocksize); | |
257 | |
258 // set up category classification array according to | |
259 // pattern type parameter | |
260 for (i = 0; i < blocksize; i++) | |
261 for (j = 0; j < blocksize; j++) { | |
262 category1[j][i] = lookup_pattern(pattern1, i, j); | |
263 category2[j][i] = lookup_pattern(pattern2, i, j); | |
264 } | |
265 | |
266 // allocate memory for slope calculation | |
267 slope = malloc(sizeof(double) * n_block); | |
268 | |
269 // embed all the signature bits, one by one | |
270 n = 0; | |
271 while (n < nbit_signature) { | |
272 int xb; | |
273 int yb; | |
274 int blocktype; | |
275 double smax; | |
276 int alpha, beta_minus, beta_plus; | |
277 double mean_1A, mean_1B, mean_2A, mean_2B, mean_1, mean_2; | |
278 double mean__1A, mean__1B, mean__2A, mean__2B; | |
279 int n_1A, n_1B, n_2A, n_2B, n_1, n_2; | |
280 int var_1A, var_1B, var_2A, var_2B; | |
281 int zone1_ok, zone2_ok; | |
282 | |
283 // find an unused block randomly, depending on seed | |
284 do { | |
285 xb = random() % (cols / blocksize); | |
286 yb = random() % (rows / blocksize); | |
287 } while (add_coord(coords, xb, yb) < 0); | |
288 | |
289 // copy image block | |
290 copy_grays_to_block(block, image, xb * blocksize, yb * blocksize, blocksize, blocksize); | |
291 | |
292 if (verbose > 0) | |
293 fprintf(stderr, "embedding bit #%d (= %d) in block at (%d/%d)\n", n, get_signature_bit(n), xb * blocksize, yb * blocksize); | |
294 if (verbose > 8) { | |
295 print_grays(image, xb * blocksize, yb * blocksize, blocksize, blocksize); | |
296 fprintf(stderr, "\n"); | |
297 } | |
298 | |
299 // sort luminance values in block to represent increasing function F | |
300 sort_grays(block[0], n_block); | |
301 | |
302 if (verbose > 8) { | |
303 print_grays(block, 0, 0, blocksize, blocksize); | |
304 fprintf(stderr, "\n"); | |
305 } | |
306 | |
307 // calculate slopes of F and determine smax, the max. slope of F | |
308 // the index where smax occures is called alpha | |
309 alpha = 0; | |
310 smax = 0.0; | |
311 for (i = 0; i < n_block - 1; i++) { | |
312 slope[i] = block[0][i + 1] - block[0][i]; | |
313 if (slope[i] > smax) { | |
314 smax = slope[i]; | |
315 alpha = i; | |
316 } | |
317 } | |
318 slope[n_block - 1] = 0; | |
319 | |
320 // block type classification | |
321 blocktype = BLOCKTYPE_UNKNOWN; | |
322 | |
323 if (smax < threshold_noise) { | |
324 // block has noise contrast | |
325 | |
326 blocktype = BLOCKTYPE_NOISE; | |
327 beta_minus = beta_plus = alpha; | |
328 } | |
329 else { | |
330 // block has progressive or hard contrast, let's find out... | |
331 | |
332 beta_minus = alpha - 1; | |
333 while (beta_minus >= 0 && smax - slope[beta_minus] <= threshold_slope) | |
334 beta_minus--; | |
335 | |
336 beta_plus = alpha + 1; | |
337 while (beta_plus < n_block && smax - slope[beta_plus] <= threshold_slope) | |
338 beta_plus++; | |
339 | |
340 if (beta_minus + 1 == alpha && beta_plus - 1 == alpha) | |
341 blocktype = BLOCKTYPE_HARD; | |
342 else | |
343 blocktype = BLOCKTYPE_PROGRESSIVE; | |
344 } | |
345 | |
346 if (verbose > 1) { | |
347 fprintf(stderr, "blocktype: %d\n", blocktype); | |
348 fprintf(stderr, "Smax = %lf, alpha = %d, beta- = %d, beta+ = %d\n", smax, alpha, beta_minus, beta_plus); | |
349 } | |
350 | |
351 // block pixel classification | |
352 for (i = 0; i < blocksize; i++) | |
353 for (j = 0; j < blocksize; j++) { | |
354 gray pixel = image[yb * blocksize + j][xb * blocksize + i]; | |
355 zone[j][i] = ZONE_VOID; | |
356 switch (blocktype) { | |
357 case BLOCKTYPE_PROGRESSIVE: | |
358 case BLOCKTYPE_HARD: | |
359 if (pixel < block[0][beta_minus]) | |
360 zone[j][i] = ZONE_1; | |
361 else if (pixel > block[0][beta_plus]) | |
362 zone[j][i] = ZONE_2; | |
363 break; | |
364 case BLOCKTYPE_NOISE: | |
365 if (pixel < block[0][n_block / 2]) | |
366 zone[j][i] = ZONE_1; | |
367 else if (pixel > block[0][n_block / 2]) | |
368 zone[j][i] = ZONE_2; | |
369 break; | |
370 default: | |
371 fprintf(stderr, "%s: invalid block type\n", progname); | |
372 break; | |
373 } | |
374 } | |
375 | |
376 if (verbose > 8) { | |
377 print_grays(zone, 0, 0, blocksize, blocksize); | |
378 fprintf(stderr, "\n"); | |
379 } | |
380 | |
381 // calculate mean values for zone/categories | |
382 mean_1A = mean_1B = mean_2A = mean_2B = mean_1 = mean_2 = 0.0; | |
383 n_1A = n_1B = n_2A = n_2B = n_1 = n_2 = 0; | |
384 for (i = 0; i < blocksize; i++) | |
385 for (j = 0; j < blocksize; j++) { | |
386 gray pixel = image[yb * blocksize + j][xb * blocksize + i]; | |
387 int pixel_zone = zone[j][i]; | |
388 int pixel_category = CATEGORY_VOID; | |
389 if (pixel_zone == ZONE_1) | |
390 pixel_category = category1[j][i]; | |
391 else if (pixel_zone == ZONE_2) | |
392 pixel_category = category2[j][i]; | |
393 | |
394 switch (pixel_zone | pixel_category) { | |
395 case CLASSIFICATION_1A: | |
396 n_1++; | |
397 n_1A++; | |
398 mean_1A += pixel; | |
399 mean_1 += pixel; | |
400 break; | |
401 case CLASSIFICATION_1B: | |
402 n_1++; | |
403 n_1B++; | |
404 mean_1B += pixel; | |
405 mean_1 += pixel; | |
406 break; | |
407 case CLASSIFICATION_2A: | |
408 n_2++; | |
409 n_2A++; | |
410 mean_2A += pixel; | |
411 mean_2 += pixel; | |
412 break; | |
413 case CLASSIFICATION_2B: | |
414 n_2++; | |
415 n_2B++; | |
416 mean_2B += pixel; | |
417 mean_2 += pixel; | |
418 break; | |
419 } | |
420 } | |
421 | |
422 if (n_1 && n_1A && n_1B) { | |
423 mean_1 /= (double) n_1; | |
424 mean_1A /= (double) n_1A; | |
425 mean_1B /= (double) n_1B; | |
426 zone1_ok = 1; | |
427 } | |
428 else { | |
429 mean_1 = mean_1A = mean_1B = 0.0; | |
430 zone1_ok = 0; | |
431 if (verbose > 0) | |
432 fprintf(stderr, "zone 1 unusable\n"); | |
433 } | |
434 | |
435 if (n_2 && n_2A && n_2B) { | |
436 mean_2 /= (double) n_2; | |
437 mean_2A /= (double) n_2A; | |
438 mean_2B /= (double) n_2B; | |
439 zone2_ok = 1; | |
440 } | |
441 else { | |
442 mean_2 = mean_2A = mean_2B = 0.0; | |
443 zone2_ok = 0; | |
444 if (verbose > 0) | |
445 fprintf(stderr, "zone 2 unusable\n"); | |
446 } | |
447 | |
448 if (!skipping && !zone1_ok && !zone2_ok) { | |
449 // pathological case - can it ever happen? | |
450 if (verbose > 0) | |
451 fprintf(stderr, "block skipped\n"); | |
452 continue; | |
453 } | |
454 | |
455 if (verbose > 2) { | |
456 fprintf(stderr, "m_1 = %lf, m_1A = %lf, m_1B = %lf\n", mean_1, mean_1A, mean_1B); | |
457 fprintf(stderr, "m_2 = %lf, m_2A = %lf, m_2B = %lf\n", mean_2, mean_2A, mean_2B); | |
458 } | |
459 | |
460 // calculate new mean values required by embedding rule | |
461 if (get_signature_bit(n)) { | |
462 if (zone1_ok) { | |
463 mean__1A = (mean_1 * (double) (n_1A + n_1B) + (double) n_1B * quality) / (double) (n_1A + n_1B); | |
464 mean__1B = mean__1A - quality; | |
465 } | |
466 if (zone2_ok) { | |
467 mean__2A = (mean_2 * (double) (n_2A + n_2B) + (double) n_2B * quality) / (double) (n_2A + n_2B); | |
468 mean__2B = mean__2A - quality; | |
469 } | |
470 } | |
471 else { | |
472 if (zone1_ok) { | |
473 mean__1A = (mean_1 * (double) (n_1A + n_1B) - (double) n_1B * quality) / (double) (n_1A + n_1B); | |
474 mean__1B = mean__1A + quality; | |
475 } | |
476 if (zone2_ok) { | |
477 mean__2A = (mean_2 * (double) (n_2A + n_2B) - (double) n_2B * quality) / (double) (n_2A + n_2B); | |
478 mean__2B = mean__2A + quality; | |
479 } | |
480 } | |
481 | |
482 // calculate luminance variations | |
483 if (zone1_ok) { | |
484 var_1A = rint(mean__1A - mean_1A); | |
485 var_1B = rint(mean__1B - mean_1B); | |
486 } | |
487 else var_1A = var_1B = 0; | |
488 | |
489 if (zone2_ok) { | |
490 var_2A = rint(mean__2A - mean_2A); | |
491 var_2B = rint(mean__2B - mean_2B); | |
492 } | |
493 else var_2A = var_2B = 0; | |
494 | |
495 if (verbose > 2) { | |
496 if (zone1_ok) | |
497 fprintf(stderr, "m*_1A = %lf, m*_1B = %lf\n", mean__1A, mean__1B); | |
498 if (zone2_ok) | |
499 fprintf(stderr, "m*_2A = %lf, m*_2B = %lf\n", mean__2A, mean__2B); | |
500 fprintf(stderr, "var %d %d %d %d\n", var_1A, var_1B, var_2A, var_2B); | |
501 } | |
502 | |
503 // apply luminance variations to image pixels | |
504 for (i = 0; i < blocksize; i++) | |
505 for (j = 0; j < blocksize; j++) { | |
506 int pixel = image[yb * blocksize + j][xb * blocksize + i]; | |
507 int pixel_zone = zone[j][i]; | |
508 int pixel_category = CATEGORY_VOID; | |
509 if (pixel_zone == ZONE_1) | |
510 pixel_category = category1[j][i]; | |
511 else if (pixel_zone == ZONE_2) | |
512 pixel_category = category2[j][i]; | |
513 | |
514 switch (pixel_zone | pixel_category) { | |
515 case CLASSIFICATION_1A: | |
516 pixel = GRAYRANGE(pixel + var_1A); | |
517 break; | |
518 case CLASSIFICATION_1B: | |
519 pixel = GRAYRANGE(pixel + var_1B); | |
520 break; | |
521 case CLASSIFICATION_2A: | |
522 pixel = GRAYRANGE(pixel + var_2A); | |
523 break; | |
524 case CLASSIFICATION_2B: | |
525 pixel = GRAYRANGE(pixel + var_2B); | |
526 break; | |
527 } | |
528 image[yb * blocksize + j][xb * blocksize + i] = pixel; | |
529 } | |
530 | |
531 n++; | |
532 } | |
533 | |
534 free_grays(category2); | |
535 free_grays(category1); | |
536 free_grays(zone); | |
537 free_grays(block); | |
538 | |
539 // write output image dimensions to output file | |
540 pgm_writepgminit(out, cols, rows, maxval, 0); | |
541 | |
542 // write output image | |
543 for (row = 0; row < rows; row++) | |
544 pgm_writepgmrow(out, image[row], cols, maxval, 0); | |
545 | |
546 fclose(out); | |
547 | |
548 pgm_freearray(image, rows); | |
549 | |
550 exit(0); | |
551 } |