0
|
1 #include "wm.h"
|
|
2 #include "signature.h"
|
|
3
|
|
4 char *progname;
|
|
5
|
|
6 void usage(void) {
|
|
7 fprintf(stderr, "usage: %s [-h] [-C] [-o file] [-v n] -s file file\n\n", progname);
|
|
8 fprintf(stderr, "\t-C\t\toutput correlation only\n");
|
|
9 fprintf(stderr, "\t-h\t\tprint usage\n");
|
|
10 fprintf(stderr, "\t-o file\t\toutput file\n");
|
|
11 fprintf(stderr, "\t-s file\t\toriginal signature file\n");
|
|
12 fprintf(stderr, "\t-v n\t\tverbosity level\n");
|
|
13 exit(0);
|
|
14 }
|
|
15
|
|
16 int main(int argc, char *argv[]) {
|
|
17
|
|
18 FILE *in = stdin;
|
|
19 FILE *out = stdout;
|
|
20 FILE *sig = NULL;
|
|
21
|
|
22 char signature_name[MAXPATHLEN];
|
|
23 char output_name[MAXPATHLEN] = "(stdout)";
|
|
24 char input_name[MAXPATHLEN] = "(stdin)";
|
|
25
|
|
26 int c, i;
|
|
27 double quality = 0.0;
|
|
28 int quantization = 0;
|
|
29 int corr = 0, match = 0;
|
|
30 int verbose = 0;
|
|
31
|
|
32 int correlation_only = 0;
|
|
33
|
|
34 int seed;
|
|
35 char line[32];
|
|
36
|
|
37 progname = argv[0];
|
|
38
|
|
39 while ((c = getopt(argc, argv, "h?Co:s:v:")) != EOF) {
|
|
40 switch (c) {
|
|
41 case 'h':
|
|
42 case '?':
|
|
43 usage();
|
|
44 break;
|
|
45 case 'C':
|
|
46 correlation_only = 1;
|
|
47 break;
|
|
48 case 'o':
|
|
49 if ((out = fopen(optarg, "w")) == NULL) {
|
|
50 fprintf(stderr, "%s: unable to open output file %s\n", progname, optarg);
|
|
51 exit(1);
|
|
52 }
|
|
53 strcpy(output_name, optarg);
|
|
54 break;
|
|
55 case 's':
|
|
56 if ((sig = fopen(optarg, "r")) == NULL) {
|
|
57 fprintf(stderr, "%s: unable to open signature file %s\n", progname, optarg);
|
|
58 exit(1);
|
|
59 }
|
|
60 strcpy(signature_name, optarg);
|
|
61 break;
|
|
62 case 'v':
|
|
63 verbose = atoi(optarg);
|
|
64 if (verbose < 0) {
|
|
65 fprintf(stderr, "%s: verbosity level %d out of range\n", progname, verbose);
|
|
66 exit(1);
|
|
67 }
|
|
68 break;
|
|
69 }
|
|
70 }
|
|
71
|
|
72 argc -= optind;
|
|
73 argv += optind;
|
|
74
|
|
75 if (argc > 1) {
|
|
76 usage();
|
|
77 exit(1);
|
|
78 }
|
|
79
|
|
80 if (argc == 1 && *argv[0] != '-')
|
|
81 if ((in = fopen(argv[0], "r")) == NULL) {
|
|
82 fprintf(stderr, "%s: unable to open input file %s\n", progname, argv[0]);
|
|
83 exit(1);
|
|
84 }
|
|
85 else
|
|
86 strcpy(input_name, argv[0]);
|
|
87
|
|
88 if (sig) {
|
|
89 fgets(line, sizeof(line), sig);
|
|
90 if (strspn(line, "KCSG") >= 4) {
|
|
91 fscanf(sig, "%d\n", &nbit_signature1);
|
|
92 fscanf(sig, "%lf\n", &quality);
|
|
93 fscanf(sig, "%d\n", &quantization);
|
|
94 fscanf(sig, "%d\n", &seed);
|
|
95 srandom(seed);
|
|
96 n_signature1 = NBITSTOBYTES(nbit_signature1);
|
|
97 fread(signature1, sizeof(char), n_signature1, sig);
|
|
98 fscanf(sig, "\n");
|
|
99 }
|
|
100 else {
|
|
101 fprintf(stderr, "%s: invalid signature file %s\n", progname, signature_name);
|
|
102 exit(1);
|
|
103 }
|
|
104 close(sig);
|
|
105 }
|
|
106 else {
|
|
107 fprintf(stderr, "%s: original signature file not specified, use -s file option\n", progname);
|
|
108 exit(1);
|
|
109 }
|
|
110
|
|
111 fgets(line, sizeof(line), in);
|
|
112 if (strspn(line, "KCWM") >= 4) {
|
|
113 fscanf(in, "%d\n", &nbit_signature2);
|
|
114 n_signature2 = NBITSTOBYTES(nbit_signature2);
|
|
115 fread(signature2, sizeof(char), n_signature2, in);
|
|
116 fscanf(in, "\n");
|
|
117 }
|
|
118 else {
|
|
119 fprintf(stderr, "%s: invalid watermark file %s\n", progname, input_name);
|
|
120 exit(1);
|
|
121 }
|
|
122
|
|
123 if (verbose > 0) {
|
|
124 fprintf(stderr, "signature length: %d\n", nbit_signature1);
|
|
125 fprintf(stderr, "watermark length: %d\n", nbit_signature2);
|
|
126 }
|
|
127
|
|
128 for (i = 0; i < nbit_signature2; i++)
|
|
129 if (get_signature1_bit(i % nbit_signature1) == get_signature2_bit(i))
|
|
130 corr++, match++;
|
|
131 else
|
|
132 corr--;
|
|
133
|
|
134 if (correlation_only)
|
|
135 fprintf(out, "%lf\n", (double) corr / nbit_signature2);
|
|
136 else {
|
|
137 fprintf(out, "bit matches: %d/%d\n", match, nbit_signature2);
|
|
138 fprintf(out, "correlation: %lf\n", (double) corr / nbit_signature2);
|
|
139 }
|
|
140
|
|
141 exit(0);
|
|
142 }
|