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
|
|
78 if (argc == 1 && *argv[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]);
|
|
85
|
|
86 if (!sig) {
|
|
87 fprintf(stderr, "%s: original signature file not specified, use -s file option\n", progname);
|
|
88 exit(1);
|
|
89 }
|
|
90
|
|
91 fgets(line, sizeof(line), sig);
|
|
92 if (strspn(line, "WGSG") < 4) {
|
|
93 fprintf(stderr, "%s: original signature file %s invalid\n", progname, signature_name);
|
|
94 exit(1);
|
|
95 }
|
|
96
|
|
97 fgets(line, sizeof(line), in);
|
|
98 if (strspn(line, "WGWM") < 4) {
|
|
99 fprintf(stderr, "%s: watermark file %s invalid\n", progname, input_name);
|
|
100 exit(1);
|
|
101 }
|
|
102
|
|
103 fscanf(sig, "%d\n", &sig_n);
|
|
104 fscanf(in, "%d\n", &in_n);
|
|
105 if (sig_n != in_n) {
|
|
106 fprintf(stderr, "%s: watermark length mismatch (original %d, input %d)\n", progname, sig_n, in_n);
|
|
107 exit(1);
|
|
108 }
|
|
109 if (sig_n <= 0 || sig_n > 32000) {
|
|
110 fprintf(stderr, "%s: invalid original watermark length %d\n", progname, sig_n);
|
|
111 exit(1);
|
|
112 }
|
|
113 if (in_n != sig_n) {
|
|
114 fprintf(stderr, "%s: invalid watermark length %d, does not match signature length\n", progname, in_n);
|
|
115 exit(1);
|
|
116 }
|
|
117
|
|
118 fscanf(sig, "%lf\n", &sig_a);
|
|
119 fscanf(sig, "%lf\n", &sig_b);
|
|
120 fscanf(sig, "%d\n", &sig_e);
|
|
121 fscanf(sig, "%d\n", &sig_f);
|
|
122 fscanf(sig, "%*[^\n\r]\n");
|
|
123
|
|
124 /*
|
|
125 * normalized correlation
|
|
126 * Craver, S., "Can Invisible Watermarks Resolve Rightful Ownership?", IBM Research Report, 1996, p. 5
|
|
127 */
|
|
128
|
|
129 s1 = s2 = s3 = 0.0;
|
|
130 while (in_n > 0) {
|
|
131 double sig_x, in_x;
|
|
132
|
|
133 fscanf(sig, "%lf\n", &sig_x);
|
|
134 fscanf(in, "%lf\n", &in_x);
|
|
135
|
|
136 if (verbose >= 1) {
|
|
137 fprintf(stderr, "orig %f input %f\n", sig_x, in_x);
|
|
138 }
|
|
139
|
|
140 s1 += sig_x * in_x;
|
|
141 s2 += in_x * in_x;
|
|
142 s3 += sig_x * sig_x;
|
|
143
|
|
144 in_n--;
|
|
145 }
|
|
146
|
|
147 fprintf(out, "%f\n", s1 / sqrt(s2 * s3));
|
|
148
|
|
149 fclose(sig);
|
|
150 fclose(out);
|
|
151 fclose(in);
|
|
152
|
|
153 exit(0);
|
|
154 }
|