Mercurial > hg > wm
annotate Meerwald/wm_bruyn_e.c @ 16:4987db85cfae
fix another scanf() warning
author | Peter Meerwald <pmeerw@cosy.sbg.ac.at> |
---|---|
date | Mon, 30 Jun 2008 21:03:40 +0200 |
parents | f83ef905a63d |
children | bd669312f068 |
rev | line source |
---|---|
0 | 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; | |
8 | 40 int rows, cols, format; |
0 | 41 int c; |
42 int i, j; | |
43 int n; | |
44 int col, row; | |
45 int n_block; | |
46 int skipping = 0; | |
47 | |
48 char signature_name[MAXPATHLEN]; | |
49 char input_name[MAXPATHLEN] = "(stdin)"; | |
50 char output_name[MAXPATHLEN] = "(stdout)"; | |
51 | |
52 double quality = 0.0; | |
53 double threshold_noise = 0.0; | |
54 double threshold_slope = 0.0; | |
55 int pattern1 = 0; | |
56 int pattern2 = 0; | |
57 int blocksize = 0; | |
58 int seed; | |
59 | |
60 int verbose = 0; | |
61 | |
62 struct coords *coords; | |
63 | |
64 progname = argv[0]; | |
65 | |
66 pgm_init(&argc, argv); wm_init(); | |
67 | |
68 // parse command line and set options | |
69 while ((c = getopt(argc, argv, "b:h?n:o:p:P:q:s:t:T:v:k")) != EOF) { | |
70 switch (c) { | |
71 case 'k': | |
72 skipping = 1; | |
73 break; | |
74 case 'h': | |
75 case '?': | |
76 usage(); | |
77 break; | |
78 case 'n': | |
79 nbit_signature = atoi(optarg); | |
80 if (nbit_signature <= 0 || nbit_signature > NBITSIGNATURE) { | |
81 fprintf(stderr, "%s: invalid signature length %d\n", progname, nbit_signature); | |
82 exit(1); | |
83 } | |
84 break; | |
85 case 'o': | |
86 if ((out = fopen(optarg, "wb")) == NULL) { | |
87 fprintf(stderr, "%s: unable to open output file %s\n", progname, optarg); | |
88 exit(1); | |
89 } | |
90 strcpy(output_name, optarg); | |
91 break; | |
92 case 'p': | |
93 pattern1 = atoi(optarg); | |
94 if (pattern1 <= 0 || pattern1 > NPATTERN) { | |
95 fprintf(stderr, "%s: pattern type out of range\n", progname); | |
96 exit(1); | |
97 } | |
98 break; | |
99 case 'P': | |
100 pattern2 = atoi(optarg); | |
101 if (pattern2 <= 0 || pattern2 > 3) { | |
102 fprintf(stderr, "%s: pattern type out of range\n", progname); | |
103 exit(1); | |
104 } | |
105 break; | |
106 case 'q': | |
107 quality = atof(optarg); | |
108 if (quality <= 0) { | |
109 fprintf(stderr, "%s: quality factor %f out of range\n", progname, quality); | |
110 } | |
111 break; | |
112 case 's': | |
113 if ((sig = fopen(optarg, "r")) == NULL) { | |
114 fprintf(stderr, "%s: unable to open signature file %s\n", progname, optarg); | |
115 exit(1); | |
116 } | |
117 strcpy(signature_name, optarg); | |
118 break; | |
119 case 't': | |
120 threshold_noise = atof(optarg); | |
121 if (threshold_noise <= 0) { | |
122 fprintf(stderr, "%s: noise threshold %f out of range\n", progname, threshold_noise); | |
123 } | |
124 break; | |
125 case 'T': | |
126 threshold_slope = atof(optarg); | |
127 if (threshold_slope <= 0) { | |
128 fprintf(stderr, "%s: slope threshold %f out of range\n", progname, threshold_slope); | |
129 } | |
130 break; | |
131 case 'v': | |
132 verbose = atoi(optarg); | |
133 if (verbose < 0) { | |
134 fprintf(stderr, "%s: verbosity level %d out of range\n",progname, verbose); | |
135 exit(1); | |
136 } | |
137 break; | |
138 } | |
139 } | |
140 | |
141 argc -= optind; | |
142 argv += optind; | |
143 | |
144 if (argc > 1) { | |
145 usage(); | |
146 exit(1); | |
147 } | |
148 | |
149 // open input image file or read from stdin | |
8 | 150 if (argc == 1 && *argv[0] != '-') { |
0 | 151 if ((in = fopen(argv[0], "rb")) == NULL) { |
152 fprintf(stderr, "%s: unable to open input file %s\n", progname, argv[0]); | |
153 exit(1); | |
154 } | |
155 else | |
156 strcpy(input_name, argv[0]); | |
8 | 157 } |
158 | |
0 | 159 // read signature file and set options |
160 // command line options override signature file options | |
161 if (sig) { | |
162 char line[128]; | |
163 fgets(line, sizeof(line), sig); | |
164 if (strspn(line, "BRSG") >= 4) { | |
165 if (nbit_signature == 0) | |
166 fscanf(sig, "%d\n", &nbit_signature); | |
167 else | |
168 fscanf(sig, "%*d\n"); | |
169 if (skipping == 0) | |
170 fscanf(sig, "%d\n", &skipping); | |
171 else | |
172 fscanf(sig, "%*d\n"); | |
173 if (pattern1 == 0) | |
174 fscanf(sig, "%d\n", &pattern1); | |
175 else | |
176 fscanf(sig, "%*d\n"); | |
177 if (pattern2 == 0) | |
178 fscanf(sig, "%d\n", &pattern2); | |
179 else | |
180 fscanf(sig, "%*d\n"); | |
181 if (quality == 0.0) | |
182 fscanf(sig, "%lf\n", &quality); | |
183 else | |
16
4987db85cfae
fix another scanf() warning
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
8
diff
changeset
|
184 fscanf(sig, "%*f\n"); |
0 | 185 if (threshold_noise == 0.0) |
186 fscanf(sig, "%lf\n", &threshold_noise); | |
187 else | |
16
4987db85cfae
fix another scanf() warning
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
8
diff
changeset
|
188 fscanf(sig, "%*f\n"); |
0 | 189 if (threshold_slope == 0.0) |
190 fscanf(sig, "%lf\n", &threshold_slope); | |
191 else | |
16
4987db85cfae
fix another scanf() warning
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
8
diff
changeset
|
192 fscanf(sig, "%*f\n"); |
0 | 193 if (blocksize == 0) |
194 fscanf(sig, "%d\n", &blocksize); | |
195 else | |
196 fscanf(sig, "%*d\n"); | |
197 fscanf(sig, "%d\n", &seed); | |
198 srandom(seed); | |
199 n_signature = NBITSTOBYTES(nbit_signature); | |
200 fread(signature, sizeof(char), n_signature, sig); | |
201 fscanf(sig, "\n"); | |
202 } | |
203 else { | |
204 fprintf(stderr, "%s: invalid signature file %s\n", progname, signature_name); | |
205 exit(1); | |
206 } | |
207 fclose(sig); | |
208 } | |
209 else { | |
210 fprintf(stderr, "%s: signature file not specified, use -s file option\n", progname); | |
211 exit(1); | |
212 } | |
213 | |
214 if (pattern1 <= 0 || pattern2 <= 0 || pattern1 > NPATTERN || pattern2 > NPATTERN) { | |
8 | 215 fprintf(stderr, "%s: invalid pattern type specified\n", progname); |
0 | 216 exit(1); |
217 } | |
218 | |
219 // read dimensions of input image file | |
220 pgm_readpgminit(in, &cols, &rows, &maxval, &format); | |
221 | |
222 // see if we can embed all signature bits | |
223 // we want at least half of the blocks untouched | |
224 if (((rows / blocksize) * (cols / blocksize)) < nbit_signature / 2) { | |
225 fprintf(stderr, "%s: image not large enough to embed %d bits of signature\n", progname, nbit_signature); | |
226 exit(1); | |
227 } | |
228 n_block = blocksize * blocksize; | |
229 | |
230 // allocate structure to remember which blocks we already touched, | |
231 // allow plenty of room to skip over blocks | |
8 | 232 if ((coords = alloc_coords(nbit_signature * 16)) == NULL) { |
0 | 233 fprintf(stderr, "%s: unable to allocate memory\n", progname); |
234 exit(1); | |
235 } | |
236 | |
237 // read in input image file | |
238 image = pgm_allocarray(cols, rows); | |
239 for (row = 0; row < rows; row++) | |
240 pgm_readpgmrow(in, image[row], cols, maxval, format); | |
241 | |
242 fclose(in); | |
243 | |
244 row = 0; | |
245 col = 0; | |
246 | |
247 // allocate memory for one block | |
248 block = alloc_grays(blocksize, blocksize); | |
249 | |
250 // allocate memory for zone classification | |
251 zone = alloc_grays(blocksize, blocksize); | |
252 | |
253 // allocate memory for category classification | |
254 category1 = alloc_grays(blocksize, blocksize); | |
255 category2 = alloc_grays(blocksize, blocksize); | |
256 | |
257 // set up category classification array according to | |
258 // pattern type parameter | |
259 for (i = 0; i < blocksize; i++) | |
260 for (j = 0; j < blocksize; j++) { | |
261 category1[j][i] = lookup_pattern(pattern1, i, j); | |
262 category2[j][i] = lookup_pattern(pattern2, i, j); | |
263 } | |
264 | |
265 // allocate memory for slope calculation | |
266 slope = malloc(sizeof(double) * n_block); | |
267 | |
268 // embed all the signature bits, one by one | |
269 n = 0; | |
270 while (n < nbit_signature) { | |
271 int xb; | |
272 int yb; | |
273 int blocktype; | |
274 double smax; | |
275 int alpha, beta_minus, beta_plus; | |
276 double mean_1A, mean_1B, mean_2A, mean_2B, mean_1, mean_2; | |
277 double mean__1A, mean__1B, mean__2A, mean__2B; | |
278 int n_1A, n_1B, n_2A, n_2B, n_1, n_2; | |
279 int var_1A, var_1B, var_2A, var_2B; | |
280 int zone1_ok, zone2_ok; | |
281 | |
282 // find an unused block randomly, depending on seed | |
283 do { | |
284 xb = random() % (cols / blocksize); | |
285 yb = random() % (rows / blocksize); | |
286 } while (add_coord(coords, xb, yb) < 0); | |
287 | |
288 // copy image block | |
289 copy_grays_to_block(block, image, xb * blocksize, yb * blocksize, blocksize, blocksize); | |
290 | |
291 if (verbose > 0) | |
292 fprintf(stderr, "embedding bit #%d (= %d) in block at (%d/%d)\n", n, get_signature_bit(n), xb * blocksize, yb * blocksize); | |
293 if (verbose > 8) { | |
294 print_grays(image, xb * blocksize, yb * blocksize, blocksize, blocksize); | |
295 fprintf(stderr, "\n"); | |
296 } | |
297 | |
298 // sort luminance values in block to represent increasing function F | |
299 sort_grays(block[0], n_block); | |
300 | |
301 if (verbose > 8) { | |
302 print_grays(block, 0, 0, blocksize, blocksize); | |
303 fprintf(stderr, "\n"); | |
304 } | |
305 | |
306 // calculate slopes of F and determine smax, the max. slope of F | |
307 // the index where smax occures is called alpha | |
308 alpha = 0; | |
309 smax = 0.0; | |
310 for (i = 0; i < n_block - 1; i++) { | |
311 slope[i] = block[0][i + 1] - block[0][i]; | |
312 if (slope[i] > smax) { | |
313 smax = slope[i]; | |
314 alpha = i; | |
315 } | |
316 } | |
317 slope[n_block - 1] = 0; | |
318 | |
319 // block type classification | |
320 blocktype = BLOCKTYPE_UNKNOWN; | |
321 | |
322 if (smax < threshold_noise) { | |
323 // block has noise contrast | |
324 | |
325 blocktype = BLOCKTYPE_NOISE; | |
326 beta_minus = beta_plus = alpha; | |
327 } | |
328 else { | |
329 // block has progressive or hard contrast, let's find out... | |
330 | |
331 beta_minus = alpha - 1; | |
332 while (beta_minus >= 0 && smax - slope[beta_minus] <= threshold_slope) | |
333 beta_minus--; | |
334 | |
335 beta_plus = alpha + 1; | |
336 while (beta_plus < n_block && smax - slope[beta_plus] <= threshold_slope) | |
337 beta_plus++; | |
338 | |
339 if (beta_minus + 1 == alpha && beta_plus - 1 == alpha) | |
340 blocktype = BLOCKTYPE_HARD; | |
341 else | |
342 blocktype = BLOCKTYPE_PROGRESSIVE; | |
343 } | |
344 | |
345 if (verbose > 1) { | |
346 fprintf(stderr, "blocktype: %d\n", blocktype); | |
347 fprintf(stderr, "Smax = %lf, alpha = %d, beta- = %d, beta+ = %d\n", smax, alpha, beta_minus, beta_plus); | |
348 } | |
349 | |
350 // block pixel classification | |
351 for (i = 0; i < blocksize; i++) | |
352 for (j = 0; j < blocksize; j++) { | |
353 gray pixel = image[yb * blocksize + j][xb * blocksize + i]; | |
354 zone[j][i] = ZONE_VOID; | |
355 switch (blocktype) { | |
356 case BLOCKTYPE_PROGRESSIVE: | |
357 case BLOCKTYPE_HARD: | |
358 if (pixel < block[0][beta_minus]) | |
359 zone[j][i] = ZONE_1; | |
360 else if (pixel > block[0][beta_plus]) | |
361 zone[j][i] = ZONE_2; | |
362 break; | |
363 case BLOCKTYPE_NOISE: | |
364 if (pixel < block[0][n_block / 2]) | |
365 zone[j][i] = ZONE_1; | |
366 else if (pixel > block[0][n_block / 2]) | |
367 zone[j][i] = ZONE_2; | |
368 break; | |
369 default: | |
370 fprintf(stderr, "%s: invalid block type\n", progname); | |
371 break; | |
372 } | |
373 } | |
374 | |
375 if (verbose > 8) { | |
376 print_grays(zone, 0, 0, blocksize, blocksize); | |
377 fprintf(stderr, "\n"); | |
378 } | |
379 | |
380 // calculate mean values for zone/categories | |
381 mean_1A = mean_1B = mean_2A = mean_2B = mean_1 = mean_2 = 0.0; | |
8 | 382 mean__1A = mean__1B = mean__2A = mean__2B = 0.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 } |