0
|
1 #include "wm.h"
|
|
2
|
|
3 char *progname;
|
|
4
|
|
5 void usage(void) {
|
|
6 fprintf(stderr, "usage: %s [-h] [-C] [-o file] [-v n] -s file file\n\n", progname);
|
|
7 fprintf(stderr, "\t-C\t\toutput correlation only\n");
|
|
8 fprintf(stderr, "\t-h\t\tprint usage\n");
|
|
9 fprintf(stderr, "\t-o file\t\toutput file\n");
|
|
10 fprintf(stderr, "\t-v n\t\tverbosity level\n");
|
|
11 fprintf(stderr, "\t-s file\t\toriginal signature file\n");
|
|
12 exit(0);
|
|
13 }
|
|
14
|
|
15 int main(int argc, char *argv[]) {
|
|
16
|
|
17 FILE *in = stdin;
|
|
18 FILE *out = stdout;
|
|
19 FILE *sig = NULL;
|
|
20
|
|
21 char signature_name[MAXPATHLEN];
|
|
22 char output_name[MAXPATHLEN] = "(stdout)";
|
|
23 char input_name[MAXPATHLEN] = "(stdin)";
|
|
24
|
|
25 int c;
|
|
26 int sig_n, in_n;
|
|
27 double sig_a, sig_b;
|
|
28 int sig_e, sig_f;
|
|
29 double s1, s2, s3;
|
|
30 char line[32];
|
|
31
|
|
32 int verbose = 0;
|
|
33 int correlation_only = 0;
|
|
34
|
|
35 progname = argv[0];
|
|
36
|
|
37 while ((c = getopt(argc, argv, "h?Co:s:v:")) != EOF) {
|
|
38 switch (c) {
|
|
39 case 'h':
|
|
40 case '?':
|
|
41 usage();
|
|
42 break;
|
|
43 case 'C':
|
|
44 correlation_only = 1;
|
|
45 break;
|
|
46 case 'o':
|
|
47 if ((out = fopen(optarg, "w")) == NULL) {
|
|
48 fprintf(stderr, "%s: unable to open output file %s\n", progname, optarg);
|
|
49 exit(1);
|
|
50 }
|
|
51 strcpy(output_name, optarg);
|
|
52 break;
|
|
53 case 's':
|
|
54 if ((sig = fopen(optarg, "r")) == NULL) {
|
|
55 fprintf(stderr, "%s: unable to open signature file %s\n", progname, optarg);
|
|
56 exit(1);
|
|
57 }
|
|
58 strcpy(signature_name, optarg);
|
|
59 break;
|
|
60 case 'v':
|
|
61 verbose = atoi(optarg);
|
|
62 if (verbose < 0) {
|
|
63 fprintf(stderr, "%s: verbosity level %d out of range\n", progname, verbose);
|
|
64 exit(1);
|
|
65 }
|
|
66 break;
|
|
67 }
|
|
68 }
|
|
69
|
|
70 argc -= optind;
|
|
71 argv += optind;
|
|
72
|
|
73 if (argc > 1) {
|
|
74 usage();
|
|
75 exit(1);
|
|
76 }
|
|
77
|
8
|
78 if (argc == 1 && *argv[0] != '-') {
|
0
|
79 if ((in = fopen(argv[0], "r")) == NULL) {
|
|
80 fprintf(stderr, "%s: unable to open input file %s\n", progname, argv[0]);
|
|
81 exit(1);
|
|
82 }
|
|
83 else
|
|
84 strcpy(input_name, argv[0]);
|
8
|
85 }
|
|
86
|
0
|
87 if (!sig) {
|
|
88 fprintf(stderr, "%s: original signature file not specified, use -s file option\n", progname);
|
|
89 exit(1);
|
|
90 }
|
|
91
|
|
92 fgets(line, sizeof(line), sig);
|
|
93 if (strspn(line, "WGSG") < 4) {
|
|
94 fprintf(stderr, "%s: original signature file %s invalid\n", progname, signature_name);
|
|
95 exit(1);
|
|
96 }
|
|
97
|
|
98 fgets(line, sizeof(line), in);
|
|
99 if (strspn(line, "WGWM") < 4) {
|
|
100 fprintf(stderr, "%s: watermark file %s invalid\n", progname, input_name);
|
|
101 exit(1);
|
|
102 }
|
|
103
|
|
104 fscanf(sig, "%d\n", &sig_n);
|
|
105 fscanf(in, "%d\n", &in_n);
|
|
106 if (sig_n != in_n) {
|
|
107 fprintf(stderr, "%s: watermark length mismatch (original %d, input %d)\n", progname, sig_n, in_n);
|
|
108 exit(1);
|
|
109 }
|
|
110 if (sig_n <= 0 || sig_n > 32000) {
|
|
111 fprintf(stderr, "%s: invalid original watermark length %d\n", progname, sig_n);
|
|
112 exit(1);
|
|
113 }
|
|
114 if (in_n != sig_n) {
|
|
115 fprintf(stderr, "%s: invalid watermark length %d, does not match signature length\n", progname, in_n);
|
|
116 exit(1);
|
|
117 }
|
|
118
|
|
119 fscanf(sig, "%lf\n", &sig_a);
|
|
120 fscanf(sig, "%lf\n", &sig_b);
|
|
121 fscanf(sig, "%d\n", &sig_e);
|
|
122 fscanf(sig, "%d\n", &sig_f);
|
|
123 fscanf(sig, "%*[^\n\r]\n");
|
|
124
|
|
125 /*
|
|
126 * normalized correlation
|
|
127 * Craver, S., "Can Invisible Watermarks Resolve Rightful Ownership?", IBM Research Report, 1996, p. 5
|
|
128 */
|
|
129
|
|
130 s1 = s2 = s3 = 0.0;
|
|
131 while (in_n > 0) {
|
|
132 double sig_x, in_x;
|
|
133
|
|
134 fscanf(sig, "%lf\n", &sig_x);
|
|
135 fscanf(in, "%lf\n", &in_x);
|
|
136
|
|
137 if (verbose >= 1) {
|
|
138 fprintf(stderr, "orig %f input %f\n", sig_x, in_x);
|
|
139 }
|
|
140
|
|
141 s1 += sig_x * in_x;
|
|
142 s2 += in_x * in_x;
|
|
143 s3 += sig_x * sig_x;
|
|
144
|
|
145 in_n--;
|
|
146 }
|
|
147
|
|
148 fprintf(out, "%f\n", s1 / sqrt(s2 * s3));
|
|
149
|
|
150 fclose(sig);
|
|
151 fclose(out);
|
|
152 fclose(in);
|
|
153
|
|
154 exit(0);
|
|
155 }
|