Mercurial > hg > wm
comparison Meerwald/cmp_corvi_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; | |
26 int sig_n, in_n; | |
27 double s1, s2, s3; | |
28 char line[32]; | |
29 int matches; | |
30 | |
31 int verbose = 0; | |
32 int correlation_only = 0; | |
33 | |
34 progname = argv[0]; | |
35 | |
36 while ((c = getopt(argc, argv, "h?o:s:v:C")) != EOF) { | |
37 switch (c) { | |
38 case 'h': | |
39 case '?': | |
40 usage(); | |
41 break; | |
42 case 'o': | |
43 if ((out = fopen(optarg, "w")) == NULL) { | |
44 fprintf(stderr, "%s: unable to open output file %s\n", progname, optarg); | |
45 exit(1); | |
46 } | |
47 strcpy(output_name, optarg); | |
48 break; | |
49 case 's': | |
50 if ((sig = fopen(optarg, "r")) == NULL) { | |
51 fprintf(stderr, "%s: unable to open signature file %s\n", progname, optarg); | |
52 exit(1); | |
53 } | |
54 strcpy(signature_name, optarg); | |
55 break; | |
56 case 'v': | |
57 verbose = atoi(optarg); | |
58 if (verbose < 0) { | |
59 fprintf(stderr, "%s: verbosity level %d out of range\n", progname, verbose); | |
60 exit(1); | |
61 } | |
62 break; | |
63 case 'C': | |
64 correlation_only = 1; | |
65 break; | |
66 } | |
67 } | |
68 | |
69 argc -= optind; | |
70 argv += optind; | |
71 | |
72 if (argc > 1) { | |
73 usage(); | |
74 exit(1); | |
75 } | |
76 | |
77 if (argc == 1 && *argv[0] != '-') | |
78 if ((in = fopen(argv[0], "r")) == NULL) { | |
79 fprintf(stderr, "%s: unable to open input file %s\n", progname, argv[0]); | |
80 exit(1); | |
81 } | |
82 else | |
83 strcpy(input_name, argv[0]); | |
84 | |
85 if (!sig) { | |
86 fprintf(stderr, "%s: original signature file not specified, use -s file option\n", progname); | |
87 exit(1); | |
88 } | |
89 | |
90 fgets(line, sizeof(line), sig); | |
91 if (strspn(line, "CVSG") < 4) { | |
92 fprintf(stderr, "%s: original signature file %s invalid\n", progname, signature_name); | |
93 exit(1); | |
94 } | |
95 | |
96 fgets(line, sizeof(line), in); | |
97 if (strspn(line, "CVWM") < 4) { | |
98 fprintf(stderr, "%s: signature file %s invalid\n", progname, input_name); | |
99 exit(1); | |
100 } | |
101 | |
102 fscanf(sig, "%d\n", &sig_n); | |
103 fscanf(in, "%d\n", &in_n); | |
104 if (sig_n != in_n) { | |
105 fprintf(stderr, "%s: watermark length mismatch (original %d, input %d)\n", progname, sig_n, in_n); | |
106 exit(1); | |
107 } | |
108 if (sig_n <= 0 || sig_n > 1000) { | |
109 fprintf(stderr, "%s: invalid original watermark length %d\n", progname, sig_n); | |
110 exit(1); | |
111 } | |
112 if (in_n <= 0 || in_n > 1000) { | |
113 fprintf(stderr, "%s: invalid watermark length %d\n", progname, in_n); | |
114 exit(1); | |
115 } | |
116 | |
117 fscanf(sig, "%*lf\n"); | |
118 fscanf(sig, "%*d\n"); | |
119 fscanf(sig, "%*d\n"); | |
120 fscanf(sig, "%*[^\n\r]\n"); | |
121 | |
122 /* | |
123 * normalized correlation | |
124 * Craver, S., "Can Invisible Watermarks Resolve Rightful Ownership?", IBM Research Report, 1996, p. 5 | |
125 */ | |
126 | |
127 s1 = s2 = s3 = 0.0; | |
128 matches = 0; | |
129 while (in_n > 0) { | |
130 double sig_x, in_x; | |
131 | |
132 fscanf(sig, "%lf\n", &sig_x); | |
133 fscanf(in, "%lf\n", &in_x); | |
134 | |
135 matches += SIGN(sig_x * 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 if (!correlation_only) { | |
149 fprintf(out, "%s: correlation %f, hamming distance %f\n", progname, s1 / sqrt(s2 * s3), (double) matches / sig_n); | |
150 } | |
151 else | |
152 fprintf(out, "%f\n", (double) matches / sig_n); | |
153 | |
154 fclose(sig); | |
155 fclose(out); | |
156 fclose(in); | |
157 | |
158 exit(0); | |
159 } |