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 detect\n");
|
|
18 fprintf(stderr, "\t-o file\t\textracted signature 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\tembedded signature\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
|
|
49 char signature_name[MAXPATHLEN];
|
|
50 char input_name[MAXPATHLEN] = "(stdin)";
|
|
51 char output_name[MAXPATHLEN] = "(stdout)";
|
|
52
|
|
53 double quality = 0.0;
|
|
54 double threshold_noise = 0.0;
|
|
55 double threshold_slope = 0.0;
|
|
56 int pattern1 = 0;
|
|
57 int pattern2 = 0;
|
|
58 int blocksize = 0;
|
|
59 int seed;
|
|
60
|
|
61 int verbose = 0;
|
|
62 int skipping = 0;
|
|
63
|
|
64 struct coords *coords;
|
|
65
|
|
66 progname = argv[0];
|
|
67
|
|
68 pgm_init(&argc, argv); wm_init2();
|
|
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 'h':
|
|
74 case '?':
|
|
75 usage();
|
|
76 break;
|
|
77 case 'k':
|
|
78 skipping = 1;
|
|
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 init_signature_bits();
|
|
203 fscanf(sig, "\n");
|
|
204 }
|
|
205 else {
|
|
206 fprintf(stderr, "%s: invalid signature file %s\n", progname, signature_name);
|
|
207 exit(1);
|
|
208 }
|
|
209 fclose(sig);
|
|
210 }
|
|
211 else {
|
|
212 fprintf(stderr, "%s: signature file not specified, use -s file option\n", progname);
|
|
213 exit(1);
|
|
214 }
|
|
215
|
|
216 if (pattern1 <= 0 || pattern2 <= 0 || pattern1 > NPATTERN || pattern2 > NPATTERN) {
|
|
217 fprintf(stderr, "%s: invalid pattern type specified\n");
|
|
218 exit(1);
|
|
219 }
|
|
220
|
|
221 // read dimensions of input image file
|
|
222 pgm_readpgminit(in, &cols, &rows, &maxval, &format);
|
|
223
|
|
224 // see if we can extract all signature bits
|
|
225 // we want at least half of the blocks untouched
|
|
226 if (((rows / blocksize) * (cols / blocksize)) < nbit_signature / 2) {
|
|
227 fprintf(stderr, "%s: image not large enough to contain %d bits of signature\n", progname, nbit_signature);
|
|
228 exit(1);
|
|
229 }
|
|
230 n_block = blocksize * blocksize;
|
|
231
|
|
232 // allocate structure to remember which blocks we already touched,
|
|
233 // allow plenty of room to skip over blocks
|
|
234 if ((coords = alloc_coords(nbit_signature * 2)) == NULL) {
|
|
235 fprintf(stderr, "%s: unable to allocate memory\n", progname);
|
|
236 exit(1);
|
|
237 }
|
|
238
|
|
239 // read in input image file
|
|
240 image = pgm_allocarray(cols, rows);
|
|
241 for (row = 0; row < rows; row++)
|
|
242 pgm_readpgmrow(in, image[row], cols, maxval, format);
|
|
243
|
|
244 fclose(in);
|
|
245
|
|
246 row = 0;
|
|
247 col = 0;
|
|
248
|
|
249 // allocate memory for one block
|
|
250 block = alloc_grays(blocksize, blocksize);
|
|
251
|
|
252 // allocate memory for zone classification
|
|
253 zone = alloc_grays(blocksize, blocksize);
|
|
254
|
|
255 // allocate memory for category classification
|
|
256 category1 = alloc_grays(blocksize, blocksize);
|
|
257 category2 = alloc_grays(blocksize, blocksize);
|
|
258
|
|
259 // set up category classification array according to
|
|
260 // pattern type parameter
|
|
261 for (i = 0; i < blocksize; i++)
|
|
262 for (j = 0; j < blocksize; j++) {
|
|
263 category1[j][i] = lookup_pattern(pattern1, i, j);
|
|
264 category2[j][i] = lookup_pattern(pattern2, i, j);
|
|
265 }
|
|
266
|
|
267 // allocate memory for slope calculation
|
|
268 slope = malloc(sizeof(double) * n_block);
|
|
269
|
|
270 fprintf(out, "BRWM\n");
|
|
271 fprintf(out, "%d\n", nbit_signature);
|
|
272
|
|
273 // extract all the signature bits, one by one
|
|
274 n = 0;
|
|
275 while (n < nbit_signature) {
|
|
276 int xb;
|
|
277 int yb;
|
|
278 int blocktype;
|
|
279 double smax;
|
|
280 int alpha, beta_minus, beta_plus;
|
|
281 double mean_1A, mean_1B, mean_2A, mean_2B, mean_1, mean_2;
|
|
282 double mean__1A, mean__1B, mean__2A, mean__2B;
|
|
283 int n_1A, n_1B, n_2A, n_2B, n_1, n_2;
|
|
284 double sigma, sigma_1, sigma_2;
|
|
285 int zone1_ok, zone2_ok;
|
|
286
|
|
287 // find an unused block randomly, depending on seed
|
|
288 do {
|
|
289 xb = random() % (cols / blocksize);
|
|
290 yb = random() % (rows / blocksize);
|
|
291 } while (add_coord(coords, xb, yb) < 0);
|
|
292
|
|
293 // copy image block
|
|
294 copy_grays_to_block(block, image, xb * blocksize, yb * blocksize, blocksize, blocksize);
|
|
295
|
|
296 if (verbose > 0)
|
|
297 fprintf(stderr, "detecting bit #%d in block at (%d/%d)\n", n, xb * blocksize, yb * blocksize);
|
|
298
|
|
299 // sort luminance values in block to represent increasing function F
|
|
300 sort_grays(block[0], n_block);
|
|
301
|
|
302 // calculate slopes of F and determine smax, the max. slope of F
|
|
303 // the index where smax occures is called alpha
|
|
304 alpha = 0;
|
|
305 smax = 0.0;
|
|
306 for (i = 0; i < n_block - 1; i++) {
|
|
307 slope[i] = block[0][i + 1] - block[0][i];
|
|
308 if (slope[i] > smax) {
|
|
309 smax = slope[i];
|
|
310 alpha = i;
|
|
311 }
|
|
312 }
|
|
313 slope[n_block - 1] = 0;
|
|
314
|
|
315 // block type classification
|
|
316 blocktype = BLOCKTYPE_UNKNOWN;
|
|
317
|
|
318 if (smax < threshold_noise) {
|
|
319 // block has noise contrast
|
|
320 beta_minus = beta_plus = alpha;
|
|
321 blocktype = BLOCKTYPE_NOISE;
|
|
322 }
|
|
323 else {
|
|
324 // block has progressive or hard contrast, let's find out...
|
|
325
|
|
326 beta_minus = alpha - 1;
|
|
327 while (beta_minus >= 0 && smax - slope[beta_minus] <= threshold_slope)
|
|
328 beta_minus--;
|
|
329
|
|
330 beta_plus = alpha + 1;
|
|
331 while (beta_plus < n_block && smax - slope[beta_plus] <= threshold_slope)
|
|
332 beta_plus++;
|
|
333
|
|
334 if (beta_minus + 1 == alpha && beta_plus - 1 == alpha)
|
|
335 blocktype = BLOCKTYPE_HARD;
|
|
336 else
|
|
337 blocktype = BLOCKTYPE_PROGRESSIVE;
|
|
338 }
|
|
339
|
|
340 if (verbose > 1) {
|
|
341 fprintf(stderr, "blocktype: %d\n", blocktype);
|
|
342 fprintf(stderr, "Smax = %lf, alpha = %d, beta- = %d, beta+ = %d\n", smax, alpha, beta_minus, beta_plus);
|
|
343 }
|
|
344
|
|
345 // block pixel classification
|
|
346 for (i = 0; i < blocksize; i++)
|
|
347 for (j = 0; j < blocksize; j++) {
|
|
348 gray pixel = image[yb * blocksize + j][xb * blocksize + i];
|
|
349 zone[j][i] = ZONE_VOID;
|
|
350 switch (blocktype) {
|
|
351 case BLOCKTYPE_PROGRESSIVE:
|
|
352 case BLOCKTYPE_HARD:
|
|
353 if (pixel < block[0][beta_minus])
|
|
354 zone[j][i] = ZONE_1;
|
|
355 else if (pixel > block[0][beta_plus])
|
|
356 zone[j][i] = ZONE_2;
|
|
357 break;
|
|
358 case BLOCKTYPE_NOISE:
|
|
359 if (pixel < block[0][n_block / 2])
|
|
360 zone[j][i] = ZONE_1;
|
|
361 else if (pixel > block[0][n_block / 2])
|
|
362 zone[j][i] = ZONE_2;
|
|
363 break;
|
|
364 default:
|
|
365 fprintf(stderr, "%s: invalid block type\n", progname);
|
|
366 break;
|
|
367 }
|
|
368 }
|
|
369
|
|
370 // calculate mean values for zone/categories
|
|
371 mean_1A = mean_1B = mean_2A = mean_2B = mean_1 = mean_2 = 0.0;
|
|
372 n_1A = n_1B = n_2A = n_2B = n_1 = n_2 = 0;
|
|
373 for (i = 0; i < blocksize; i++)
|
|
374 for (j = 0; j < blocksize; j++) {
|
|
375 gray pixel = image[yb * blocksize + j][xb * blocksize + i];
|
|
376 int pixel_zone = zone[j][i];
|
|
377 int pixel_category = CATEGORY_VOID;
|
|
378 if (pixel_zone == ZONE_1)
|
|
379 pixel_category = category1[j][i];
|
|
380 else if (pixel_zone == ZONE_2)
|
|
381 pixel_category = category2[j][i];
|
|
382
|
|
383 switch (pixel_zone | pixel_category) {
|
|
384 case CLASSIFICATION_1A:
|
|
385 n_1++;
|
|
386 n_1A++;
|
|
387 mean_1A += pixel;
|
|
388 mean_1 += pixel;
|
|
389 break;
|
|
390 case CLASSIFICATION_1B:
|
|
391 n_1++;
|
|
392 n_1B++;
|
|
393 mean_1B += pixel;
|
|
394 mean_1 += pixel;
|
|
395 break;
|
|
396 case CLASSIFICATION_2A:
|
|
397 n_2++;
|
|
398 n_2A++;
|
|
399 mean_2A += pixel;
|
|
400 mean_2 += pixel;
|
|
401 break;
|
|
402 case CLASSIFICATION_2B:
|
|
403 n_2++;
|
|
404 n_2B++;
|
|
405 mean_2B += pixel;
|
|
406 mean_2 += pixel;
|
|
407 break;
|
|
408 }
|
|
409 }
|
|
410
|
|
411 if (n_1 && n_1A && n_1B) {
|
|
412 mean_1 /= (double) n_1;
|
|
413 mean_1A /= (double) n_1A;
|
|
414 mean_1B /= (double) n_1B;
|
|
415 zone1_ok = 1;
|
|
416 }
|
|
417 else {
|
|
418 mean_1 = mean_1A = mean_1B = 0.0;
|
|
419 zone1_ok = 0;
|
|
420 if (verbose > 0)
|
|
421 fprintf(stderr, "zone 1 unusable\n");
|
|
422 }
|
|
423
|
|
424 if (n_2 && n_2A && n_2B) {
|
|
425 mean_2 /= (double) n_2;
|
|
426 mean_2A /= (double) n_2A;
|
|
427 mean_2B /= (double) n_2B;
|
|
428 zone2_ok = 1;
|
|
429 }
|
|
430 else {
|
|
431 mean_2 = mean_2A = mean_2B = 0.0;
|
|
432 zone2_ok = 0;
|
|
433 if (verbose > 0)
|
|
434 fprintf(stderr, "zone 2 unusable\n");
|
|
435 }
|
|
436
|
|
437 // bit extraction
|
|
438 if (zone1_ok && zone2_ok) {
|
|
439 sigma_1 = mean_1A - mean_1B;
|
|
440 sigma_2 = mean_2A - mean_2B;
|
|
441
|
|
442 if (verbose > 2) {
|
|
443 fprintf(stderr, "m_1A = %lf, m_1B = %lf\n", mean_1A, mean_1B);
|
|
444 fprintf(stderr, "m_2A = %lf, m_2B = %lf\n", mean_2A, mean_2B);
|
|
445 fprintf(stderr, "sigma1 = %lf, sigma2 = %lf\n", sigma_1, sigma_2);
|
|
446 }
|
|
447
|
|
448 #define EPSILON 0.001
|
|
449 if (fabs(sigma_1 * sigma_2) < EPSILON) {
|
|
450 // case 3
|
|
451 sigma = MAX(fabs(sigma_1), fabs(sigma_2));
|
|
452 set_signature_bit(n, sigma > 0.0);
|
|
453 if (verbose > 0)
|
|
454 fprintf(stderr, "case 3, bit #%d = %d\n", n, sigma > 0.0);
|
|
455 }
|
|
456 else if (sigma_1 * sigma_2 > 0.0) {
|
|
457 // case 1
|
|
458 set_signature_bit(n, sigma_1 > 0.0);
|
|
459 if (verbose > 0)
|
|
460 fprintf(stderr, "case 1, bit #%d = %d\n", n, sigma_1 > 0.0);
|
|
461 }
|
|
462 else if (sigma_1 * sigma_2 < 0.0) {
|
|
463 // case 2
|
|
464 sigma = (double) (n_1A + n_1B) * sigma_1 + (double) (n_2A + n_2B) * sigma_2;
|
|
465 set_signature_bit(n, sigma > 0.0);
|
|
466 if (verbose > 0)
|
|
467 fprintf(stderr, "case 2, bit #%d = %d\n", n, sigma > 0.0);
|
|
468 }
|
|
469 }
|
|
470 else if (zone1_ok) {
|
|
471 set_signature_bit(n, mean_1A > mean_1B);
|
|
472 if (verbose > 0)
|
|
473 fprintf(stderr, "case 4, bit #%d = %d\n", n, mean_1A > mean_1B);
|
|
474 }
|
|
475 else if (zone2_ok) {
|
|
476 set_signature_bit(n, mean_2A > mean_2B);
|
|
477 if (verbose > 0)
|
|
478 fprintf(stderr, "case 5, bit #%d = %d\n", n, mean_2A > mean_2B);
|
|
479 }
|
|
480 else {
|
|
481 // pathological case - can it ever happen?
|
|
482 if (verbose > 0)
|
|
483 fprintf(stderr, "block skipped\n");
|
|
484 if (!skipping) continue;
|
|
485 }
|
|
486
|
|
487 n++;
|
|
488 }
|
|
489
|
|
490 free_grays(category2);
|
|
491 free_grays(category1);
|
|
492 free_grays(zone);
|
|
493 free_grays(block);
|
|
494
|
|
495 // write extracted signature
|
|
496
|
|
497 fwrite(signature, sizeof(char), n_signature, out);
|
|
498 fclose(out);
|
|
499
|
|
500 pgm_freearray(image, rows);
|
|
501
|
|
502 exit(0);
|
|
503 }
|