Mercurial > hg > wm
comparison Meerwald/cmp_kim_sig.c @ 0:be303a3f5ea8
import
author | Peter Meerwald <pmeerw@cosy.sbg.ac.at> |
---|---|
date | Sun, 12 Aug 2007 13:14:34 +0200 |
parents | |
children | f83ef905a63d |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:be303a3f5ea8 |
---|---|
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, i, j, n; | |
26 int in_level; | |
27 double *input_watermark, *orig_watermark; | |
28 int sig_n, in_n; | |
29 double sig_a; | |
30 double sig_A; | |
31 int sig_l; | |
32 int sig_e, sig_f; | |
33 double s1, s2, s3; | |
34 double correlation; | |
35 char line[32]; | |
36 | |
37 int verbose = 0; | |
38 int correlation_only = 0; | |
39 | |
40 progname = argv[0]; | |
41 | |
42 while ((c = getopt(argc, argv, "h?Co:s:v:")) != EOF) { | |
43 switch (c) { | |
44 case 'h': | |
45 case '?': | |
46 usage(); | |
47 break; | |
48 case 'C': | |
49 correlation_only = 1; | |
50 break; | |
51 case 'o': | |
52 if ((out = fopen(optarg, "w")) == NULL) { | |
53 fprintf(stderr, "%s: unable to open output file %s\n", progname, optarg); | |
54 exit(1); | |
55 } | |
56 strcpy(output_name, optarg); | |
57 break; | |
58 case 's': | |
59 if ((sig = fopen(optarg, "r")) == NULL) { | |
60 fprintf(stderr, "%s: unable to open signature file %s\n", progname, optarg); | |
61 exit(1); | |
62 } | |
63 strcpy(signature_name, optarg); | |
64 break; | |
65 case 'v': | |
66 verbose = atoi(optarg); | |
67 if (verbose < 0) { | |
68 fprintf(stderr, "%s: verbosity level %d out of range\n", progname, verbose); | |
69 exit(1); | |
70 } | |
71 break; | |
72 } | |
73 } | |
74 | |
75 argc -= optind; | |
76 argv += optind; | |
77 | |
78 if (argc > 1) { | |
79 usage(); | |
80 exit(1); | |
81 } | |
82 | |
83 if (argc == 1 && *argv[0] != '-') | |
84 if ((in = fopen(argv[0], "r")) == NULL) { | |
85 fprintf(stderr, "%s: unable to open input file %s\n", progname, argv[0]); | |
86 exit(1); | |
87 } | |
88 else | |
89 strcpy(input_name, argv[0]); | |
90 | |
91 if (!sig) { | |
92 fprintf(stderr, "%s: original signature file not specified, use -s file option\n", progname); | |
93 exit(1); | |
94 } | |
95 | |
96 fgets(line, sizeof(line), sig); | |
97 if (strspn(line, "KISG") < 4) { | |
98 fprintf(stderr, "%s: original signature file %s invalid\n", progname, signature_name); | |
99 exit(1); | |
100 } | |
101 | |
102 fgets(line, sizeof(line), in); | |
103 if (strspn(line, "KIWM") < 4) { | |
104 fprintf(stderr, "%s: watermark file %s invalid\n", progname, input_name); | |
105 exit(1); | |
106 } | |
107 | |
108 fscanf(sig, "%d\n", &sig_n); | |
109 fscanf(in, "%d\n", &in_n); | |
110 | |
111 if (sig_n <= 0 || sig_n > 32000) { | |
112 fprintf(stderr, "%s: invalid original watermark length %d\n", progname, sig_n); | |
113 exit(1); | |
114 } | |
115 | |
116 fscanf(sig, "%lf\n", &sig_a); | |
117 fscanf(sig, "%lf\n", &sig_A); | |
118 fscanf(sig, "%d\n", &sig_l); | |
119 fscanf(sig, "%d\n", &sig_e); | |
120 fscanf(sig, "%d\n", &sig_f); | |
121 fscanf(sig, "%*[^\n\r]\n"); | |
122 | |
123 orig_watermark = malloc(sig_n * sizeof(double)); | |
124 for (i = 0; i < sig_n; i++) | |
125 fscanf(sig, "%lf\n", &orig_watermark[i]); | |
126 fclose(sig); | |
127 | |
128 fscanf(in, "%d\n", &in_level); | |
129 input_watermark = malloc(in_n * sizeof(double)); | |
130 for (i = 0; i < in_n; i++) | |
131 fscanf(in, "%lf\n", &input_watermark[i]); | |
132 fclose(in); | |
133 | |
134 /* | |
135 * normalized correlation | |
136 * Craver, S., "Can Invisible Watermarks Resolve Rightful Ownership?", IBM Research Report, 1996, p. 5 | |
137 */ | |
138 | |
139 s1 = s2 = s3 = 0.0; | |
140 for (i = 0; i < in_n; i++) { | |
141 double in_x, sig_x; | |
142 | |
143 in_x = input_watermark[i]; | |
144 sig_x = orig_watermark[i % sig_n]; | |
145 | |
146 s1 += sig_x * in_x; | |
147 s2 += in_x * in_x; | |
148 s3 += sig_x * sig_x; | |
149 } | |
150 | |
151 correlation = s1 / sqrt(s2 * s3); | |
152 | |
153 if (!correlation_only) | |
154 fprintf(out, "%s: correlation: %f\n", progname, correlation); | |
155 else | |
156 fprintf(out, "%f\n", correlation); | |
157 | |
158 fclose(out); | |
159 | |
160 free(orig_watermark); | |
161 free(input_watermark); | |
162 | |
163 exit(0); | |
164 } |