Mercurial > hg > wm
annotate Meerwald/wm_bruyn_e.c @ 20:bd669312f068
suppress warnings, fix link errors
author | Peter Meerwald-Stadler <pmeerw@pmeerw.net> |
---|---|
date | Sat, 28 Jan 2023 23:54:58 +0100 |
parents | 4987db85cfae |
children |
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; | |
20
bd669312f068
suppress warnings, fix link errors
Peter Meerwald-Stadler <pmeerw@pmeerw.net>
parents:
16
diff
changeset
|
44 int row; |
0 | 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) { | |
20
bd669312f068
suppress warnings, fix link errors
Peter Meerwald-Stadler <pmeerw@pmeerw.net>
parents:
16
diff
changeset
|
71 case 'k': |
0 | 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); | |
20
bd669312f068
suppress warnings, fix link errors
Peter Meerwald-Stadler <pmeerw@pmeerw.net>
parents:
16
diff
changeset
|
199 fread(signature, sizeof(char), NBITSTOBYTES(nbit_signature), sig); |
0 | 200 fscanf(sig, "\n"); |
201 } | |
202 else { | |
203 fprintf(stderr, "%s: invalid signature file %s\n", progname, signature_name); | |
204 exit(1); | |
205 } | |
206 fclose(sig); | |
207 } | |
208 else { | |
209 fprintf(stderr, "%s: signature file not specified, use -s file option\n", progname); | |
210 exit(1); | |
211 } | |
212 | |
213 if (pattern1 <= 0 || pattern2 <= 0 || pattern1 > NPATTERN || pattern2 > NPATTERN) { | |
8 | 214 fprintf(stderr, "%s: invalid pattern type specified\n", progname); |
0 | 215 exit(1); |
216 } | |
217 | |
218 // read dimensions of input image file | |
219 pgm_readpgminit(in, &cols, &rows, &maxval, &format); | |
220 | |
221 // see if we can embed all signature bits | |
222 // we want at least half of the blocks untouched | |
223 if (((rows / blocksize) * (cols / blocksize)) < nbit_signature / 2) { | |
224 fprintf(stderr, "%s: image not large enough to embed %d bits of signature\n", progname, nbit_signature); | |
225 exit(1); | |
226 } | |
227 n_block = blocksize * blocksize; | |
228 | |
229 // allocate structure to remember which blocks we already touched, | |
230 // allow plenty of room to skip over blocks | |
8 | 231 if ((coords = alloc_coords(nbit_signature * 16)) == NULL) { |
0 | 232 fprintf(stderr, "%s: unable to allocate memory\n", progname); |
233 exit(1); | |
234 } | |
235 | |
236 // read in input image file | |
237 image = pgm_allocarray(cols, rows); | |
238 for (row = 0; row < rows; row++) | |
239 pgm_readpgmrow(in, image[row], cols, maxval, format); | |
240 | |
241 fclose(in); | |
242 | |
243 row = 0; | |
244 | |
245 // allocate memory for one block | |
246 block = alloc_grays(blocksize, blocksize); | |
247 | |
248 // allocate memory for zone classification | |
249 zone = alloc_grays(blocksize, blocksize); | |
250 | |
251 // allocate memory for category classification | |
252 category1 = alloc_grays(blocksize, blocksize); | |
253 category2 = alloc_grays(blocksize, blocksize); | |
254 | |
20
bd669312f068
suppress warnings, fix link errors
Peter Meerwald-Stadler <pmeerw@pmeerw.net>
parents:
16
diff
changeset
|
255 // set up category classification array according to |
0 | 256 // pattern type parameter |
257 for (i = 0; i < blocksize; i++) | |
258 for (j = 0; j < blocksize; j++) { | |
259 category1[j][i] = lookup_pattern(pattern1, i, j); | |
260 category2[j][i] = lookup_pattern(pattern2, i, j); | |
261 } | |
262 | |
263 // allocate memory for slope calculation | |
264 slope = malloc(sizeof(double) * n_block); | |
265 | |
266 // embed all the signature bits, one by one | |
267 n = 0; | |
268 while (n < nbit_signature) { | |
269 int xb; | |
270 int yb; | |
271 int blocktype; | |
272 double smax; | |
273 int alpha, beta_minus, beta_plus; | |
274 double mean_1A, mean_1B, mean_2A, mean_2B, mean_1, mean_2; | |
275 double mean__1A, mean__1B, mean__2A, mean__2B; | |
276 int n_1A, n_1B, n_2A, n_2B, n_1, n_2; | |
277 int var_1A, var_1B, var_2A, var_2B; | |
278 int zone1_ok, zone2_ok; | |
279 | |
280 // find an unused block randomly, depending on seed | |
281 do { | |
282 xb = random() % (cols / blocksize); | |
283 yb = random() % (rows / blocksize); | |
284 } while (add_coord(coords, xb, yb) < 0); | |
285 | |
286 // copy image block | |
20
bd669312f068
suppress warnings, fix link errors
Peter Meerwald-Stadler <pmeerw@pmeerw.net>
parents:
16
diff
changeset
|
287 copy_grays_to_block(block, image, xb * blocksize, yb * blocksize, blocksize, blocksize); |
0 | 288 |
289 if (verbose > 0) | |
290 fprintf(stderr, "embedding bit #%d (= %d) in block at (%d/%d)\n", n, get_signature_bit(n), xb * blocksize, yb * blocksize); | |
291 if (verbose > 8) { | |
292 print_grays(image, xb * blocksize, yb * blocksize, blocksize, blocksize); | |
293 fprintf(stderr, "\n"); | |
294 } | |
295 | |
296 // sort luminance values in block to represent increasing function F | |
297 sort_grays(block[0], n_block); | |
298 | |
299 if (verbose > 8) { | |
300 print_grays(block, 0, 0, blocksize, blocksize); | |
301 fprintf(stderr, "\n"); | |
302 } | |
303 | |
304 // calculate slopes of F and determine smax, the max. slope of F | |
305 // the index where smax occures is called alpha | |
306 alpha = 0; | |
307 smax = 0.0; | |
308 for (i = 0; i < n_block - 1; i++) { | |
309 slope[i] = block[0][i + 1] - block[0][i]; | |
310 if (slope[i] > smax) { | |
311 smax = slope[i]; | |
312 alpha = i; | |
313 } | |
314 } | |
315 slope[n_block - 1] = 0; | |
316 | |
317 // block type classification | |
318 blocktype = BLOCKTYPE_UNKNOWN; | |
319 | |
320 if (smax < threshold_noise) { | |
321 // block has noise contrast | |
322 | |
323 blocktype = BLOCKTYPE_NOISE; | |
324 beta_minus = beta_plus = alpha; | |
325 } | |
326 else { | |
327 // block has progressive or hard contrast, let's find out... | |
328 | |
329 beta_minus = alpha - 1; | |
330 while (beta_minus >= 0 && smax - slope[beta_minus] <= threshold_slope) | |
331 beta_minus--; | |
332 | |
333 beta_plus = alpha + 1; | |
334 while (beta_plus < n_block && smax - slope[beta_plus] <= threshold_slope) | |
335 beta_plus++; | |
336 | |
337 if (beta_minus + 1 == alpha && beta_plus - 1 == alpha) | |
338 blocktype = BLOCKTYPE_HARD; | |
339 else | |
340 blocktype = BLOCKTYPE_PROGRESSIVE; | |
341 } | |
342 | |
343 if (verbose > 1) { | |
344 fprintf(stderr, "blocktype: %d\n", blocktype); | |
345 fprintf(stderr, "Smax = %lf, alpha = %d, beta- = %d, beta+ = %d\n", smax, alpha, beta_minus, beta_plus); | |
346 } | |
347 | |
348 // block pixel classification | |
349 for (i = 0; i < blocksize; i++) | |
350 for (j = 0; j < blocksize; j++) { | |
351 gray pixel = image[yb * blocksize + j][xb * blocksize + i]; | |
352 zone[j][i] = ZONE_VOID; | |
353 switch (blocktype) { | |
354 case BLOCKTYPE_PROGRESSIVE: | |
355 case BLOCKTYPE_HARD: | |
356 if (pixel < block[0][beta_minus]) | |
357 zone[j][i] = ZONE_1; | |
358 else if (pixel > block[0][beta_plus]) | |
359 zone[j][i] = ZONE_2; | |
360 break; | |
361 case BLOCKTYPE_NOISE: | |
362 if (pixel < block[0][n_block / 2]) | |
363 zone[j][i] = ZONE_1; | |
364 else if (pixel > block[0][n_block / 2]) | |
365 zone[j][i] = ZONE_2; | |
366 break; | |
367 default: | |
368 fprintf(stderr, "%s: invalid block type\n", progname); | |
369 break; | |
370 } | |
371 } | |
372 | |
373 if (verbose > 8) { | |
374 print_grays(zone, 0, 0, blocksize, blocksize); | |
375 fprintf(stderr, "\n"); | |
376 } | |
377 | |
378 // calculate mean values for zone/categories | |
379 mean_1A = mean_1B = mean_2A = mean_2B = mean_1 = mean_2 = 0.0; | |
8 | 380 mean__1A = mean__1B = mean__2A = mean__2B = 0.0; |
0 | 381 n_1A = n_1B = n_2A = n_2B = n_1 = n_2 = 0; |
382 for (i = 0; i < blocksize; i++) | |
383 for (j = 0; j < blocksize; j++) { | |
384 gray pixel = image[yb * blocksize + j][xb * blocksize + i]; | |
385 int pixel_zone = zone[j][i]; | |
386 int pixel_category = CATEGORY_VOID; | |
387 if (pixel_zone == ZONE_1) | |
388 pixel_category = category1[j][i]; | |
389 else if (pixel_zone == ZONE_2) | |
390 pixel_category = category2[j][i]; | |
391 | |
392 switch (pixel_zone | pixel_category) { | |
393 case CLASSIFICATION_1A: | |
394 n_1++; | |
395 n_1A++; | |
396 mean_1A += pixel; | |
397 mean_1 += pixel; | |
398 break; | |
399 case CLASSIFICATION_1B: | |
400 n_1++; | |
401 n_1B++; | |
402 mean_1B += pixel; | |
403 mean_1 += pixel; | |
404 break; | |
405 case CLASSIFICATION_2A: | |
406 n_2++; | |
407 n_2A++; | |
408 mean_2A += pixel; | |
409 mean_2 += pixel; | |
410 break; | |
411 case CLASSIFICATION_2B: | |
412 n_2++; | |
413 n_2B++; | |
414 mean_2B += pixel; | |
415 mean_2 += pixel; | |
416 break; | |
417 } | |
418 } | |
419 | |
420 if (n_1 && n_1A && n_1B) { | |
421 mean_1 /= (double) n_1; | |
422 mean_1A /= (double) n_1A; | |
423 mean_1B /= (double) n_1B; | |
424 zone1_ok = 1; | |
425 } | |
426 else { | |
427 mean_1 = mean_1A = mean_1B = 0.0; | |
428 zone1_ok = 0; | |
429 if (verbose > 0) | |
430 fprintf(stderr, "zone 1 unusable\n"); | |
431 } | |
432 | |
433 if (n_2 && n_2A && n_2B) { | |
434 mean_2 /= (double) n_2; | |
435 mean_2A /= (double) n_2A; | |
436 mean_2B /= (double) n_2B; | |
437 zone2_ok = 1; | |
438 } | |
439 else { | |
440 mean_2 = mean_2A = mean_2B = 0.0; | |
441 zone2_ok = 0; | |
442 if (verbose > 0) | |
443 fprintf(stderr, "zone 2 unusable\n"); | |
444 } | |
445 | |
446 if (!skipping && !zone1_ok && !zone2_ok) { | |
447 // pathological case - can it ever happen? | |
448 if (verbose > 0) | |
449 fprintf(stderr, "block skipped\n"); | |
450 continue; | |
451 } | |
452 | |
453 if (verbose > 2) { | |
454 fprintf(stderr, "m_1 = %lf, m_1A = %lf, m_1B = %lf\n", mean_1, mean_1A, mean_1B); | |
455 fprintf(stderr, "m_2 = %lf, m_2A = %lf, m_2B = %lf\n", mean_2, mean_2A, mean_2B); | |
456 } | |
457 | |
458 // calculate new mean values required by embedding rule | |
459 if (get_signature_bit(n)) { | |
460 if (zone1_ok) { | |
461 mean__1A = (mean_1 * (double) (n_1A + n_1B) + (double) n_1B * quality) / (double) (n_1A + n_1B); | |
462 mean__1B = mean__1A - quality; | |
463 } | |
464 if (zone2_ok) { | |
465 mean__2A = (mean_2 * (double) (n_2A + n_2B) + (double) n_2B * quality) / (double) (n_2A + n_2B); | |
466 mean__2B = mean__2A - quality; | |
467 } | |
468 } | |
469 else { | |
470 if (zone1_ok) { | |
471 mean__1A = (mean_1 * (double) (n_1A + n_1B) - (double) n_1B * quality) / (double) (n_1A + n_1B); | |
472 mean__1B = mean__1A + quality; | |
473 } | |
474 if (zone2_ok) { | |
475 mean__2A = (mean_2 * (double) (n_2A + n_2B) - (double) n_2B * quality) / (double) (n_2A + n_2B); | |
476 mean__2B = mean__2A + quality; | |
477 } | |
478 } | |
479 | |
480 // calculate luminance variations | |
481 if (zone1_ok) { | |
482 var_1A = rint(mean__1A - mean_1A); | |
483 var_1B = rint(mean__1B - mean_1B); | |
484 } | |
485 else var_1A = var_1B = 0; | |
486 | |
487 if (zone2_ok) { | |
488 var_2A = rint(mean__2A - mean_2A); | |
489 var_2B = rint(mean__2B - mean_2B); | |
490 } | |
491 else var_2A = var_2B = 0; | |
492 | |
493 if (verbose > 2) { | |
494 if (zone1_ok) | |
495 fprintf(stderr, "m*_1A = %lf, m*_1B = %lf\n", mean__1A, mean__1B); | |
496 if (zone2_ok) | |
497 fprintf(stderr, "m*_2A = %lf, m*_2B = %lf\n", mean__2A, mean__2B); | |
498 fprintf(stderr, "var %d %d %d %d\n", var_1A, var_1B, var_2A, var_2B); | |
499 } | |
500 | |
501 // apply luminance variations to image pixels | |
502 for (i = 0; i < blocksize; i++) | |
503 for (j = 0; j < blocksize; j++) { | |
504 int pixel = image[yb * blocksize + j][xb * blocksize + i]; | |
505 int pixel_zone = zone[j][i]; | |
506 int pixel_category = CATEGORY_VOID; | |
507 if (pixel_zone == ZONE_1) | |
508 pixel_category = category1[j][i]; | |
509 else if (pixel_zone == ZONE_2) | |
510 pixel_category = category2[j][i]; | |
511 | |
512 switch (pixel_zone | pixel_category) { | |
513 case CLASSIFICATION_1A: | |
514 pixel = GRAYRANGE(pixel + var_1A); | |
515 break; | |
516 case CLASSIFICATION_1B: | |
517 pixel = GRAYRANGE(pixel + var_1B); | |
518 break; | |
519 case CLASSIFICATION_2A: | |
520 pixel = GRAYRANGE(pixel + var_2A); | |
521 break; | |
522 case CLASSIFICATION_2B: | |
523 pixel = GRAYRANGE(pixel + var_2B); | |
524 break; | |
525 } | |
526 image[yb * blocksize + j][xb * blocksize + i] = pixel; | |
527 } | |
528 | |
529 n++; | |
530 } | |
531 | |
532 free_grays(category2); | |
533 free_grays(category1); | |
534 free_grays(zone); | |
535 free_grays(block); | |
536 | |
537 // write output image dimensions to output file | |
538 pgm_writepgminit(out, cols, rows, maxval, 0); | |
539 | |
540 // write output image | |
541 for (row = 0; row < rows; row++) | |
542 pgm_writepgmrow(out, image[row], cols, maxval, 0); | |
543 | |
544 fclose(out); | |
545 | |
546 pgm_freearray(image, rows); | |
547 | |
548 exit(0); | |
549 } |