comparison Meerwald/cmp_kund2_sig.c @ 3:acb6967ee76d

update to 0.5
author Peter Meerwald <pmeerw@cosy.sbg.ac.at>
date Tue, 14 Aug 2007 21:11:21 +0200
parents
children f83ef905a63d
comparison
equal deleted inserted replaced
2:b92f06d9a967 3:acb6967ee76d
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 char *binstr;
27
28 int correlation_only = 0;
29
30 int c, i;
31 int quality = 0;
32 int blocksize = 0;
33 int corr = 0, match = 0;
34 int verbose = 0;
35 int filter = 0;
36 int method = 0;
37 int level = 0;
38 char filter_name[MAXPATHLEN] = "";
39 int k;
40
41 int seed;
42 char line[32];
43
44 progname = argv[0];
45
46 while ((c = getopt(argc, argv, "h?Co:s:v:")) != EOF) {
47 switch (c) {
48 case 'h':
49 case '?':
50 usage();
51 break;
52 case 'C':
53 correlation_only = 1;
54 break;
55 case 'o':
56 if ((out = fopen(optarg, "w")) == NULL) {
57 fprintf(stderr, "%s: unable to open output file %s\n", progname, optarg);
58 exit(1);
59 }
60 strcpy(output_name, optarg);
61 break;
62 case 's':
63 if ((sig = fopen(optarg, "r")) == NULL) {
64 fprintf(stderr, "%s: unable to open signature file %s\n", progname, optarg);
65 exit(1);
66 }
67 strcpy(signature_name, optarg);
68 break;
69 case 'v':
70 verbose = atoi(optarg);
71 if (verbose < 0) {
72 fprintf(stderr, "%s: verbosity level %d out of range\n", progname, verbose);
73 exit(1);
74 }
75 break;
76 }
77 }
78
79 argc -= optind;
80 argv += optind;
81
82 if (argc > 1) {
83 usage();
84 exit(1);
85 }
86
87 if (argc == 1 && *argv[0] != '-')
88 if ((in = fopen(argv[0], "r")) == NULL) {
89 fprintf(stderr, "%s: unable to open input file %s\n", progname, argv[0]);
90 exit(1);
91 }
92 else
93 strcpy(input_name, argv[0]);
94
95 if (sig) {
96 fgets(line, sizeof(line), sig);
97 if (strspn(line, "KD2SG") >= 5) {
98 fscanf(sig, "%d\n", &nbit_signature1);
99 fscanf(sig, "%d\n", &quality);
100 fscanf(sig, "%d\n", &blocksize);
101 fscanf(sig, "%d\n", &method);
102 fscanf(sig, "%d\n", &filter);
103 fscanf(sig, "%[^\n\r]\n", &filter_name);
104 fscanf(sig, "%d\n", &level);
105 fscanf(sig, "%d\n", &seed);
106 srandom(seed);
107 n_signature1 = NBITSTOBYTES(nbit_signature1);
108
109 binstr = malloc((nbit_signature1 + 1) * sizeof(char));
110 fscanf(sig, "%[01]\n", binstr);
111 binstr_to_sig1(binstr);
112 free(binstr);
113 }
114 else {
115 fprintf(stderr, "%s: invalid signature file %s\n", progname, signature_name);
116 exit(1);
117 }
118 close(sig);
119 }
120 else {
121 fprintf(stderr, "%s: original signature file not specified, use -s file option\n", progname);
122 exit(1);
123 }
124
125 fgets(line, sizeof(line), in);
126 if (strspn(line, "KD2WM") >= 5) {
127 int max_nbit_signature;
128 int min_nbit_signature = -1;
129 double pe[100];
130 double pe_sum;
131 double alpha[100];
132 char *w[100];
133
134 fscanf(in, "%d\n", &max_nbit_signature);
135
136 k = 0;
137 while (!feof(in) && k < 100) {
138 int e;
139
140 fscanf(in, "%d\n", &nbit_signature2);
141 w[k] = binstr = malloc(sizeof(char) * (nbit_signature2 + 1));
142 fscanf(in, "%[01]\n", binstr);
143
144 binstr_to_sig2(binstr);
145
146 if (nbit_signature2 < min_nbit_signature || min_nbit_signature == -1)
147 min_nbit_signature = nbit_signature2;
148 e = 0;
149 for (i = 0; i < nbit_signature2; i += 2) {
150 if (get_signature1_bit(i % nbit_signature1) != get_signature2_bit(i))
151 e++;
152 }
153 if (e > 0)
154 pe[k++] = log( (1 - (e / (double) nbit_signature2)) / (e / (double) nbit_signature2));
155 else
156 pe[k++] = 0;
157 }
158
159 pe_sum = 0.0;
160 for (i = 0; i < k; i++) {
161 // fprintf(stderr, "XXX pe[%d] = %f\n", i, pe[i]);
162 pe_sum += pe[i];
163 }
164
165 for (i = 0; i < k; i++) {
166 if (pe_sum != 0)
167 alpha[i] = pe[i] / pe_sum;
168 else
169 alpha[i] = 1.0;
170 }
171
172 nbit_signature = min_nbit_signature;
173 for (i = 0; i < min_nbit_signature; i++) {
174 double s = 0.0;
175 int j;
176
177 for (j = 0; j < k; j++) {
178 int bit;
179 //fprintf(stderr, "XXX %d %d\n", i, j);
180 binstr_to_sig2(w[j]);
181 bit = get_signature2_bit(i) ? 1 : -1;
182 s += alpha[j] * bit;
183 }
184 // fprintf(stderr, "YYY %d %f\n", i, s);
185 set_signature_bit(i, s > 0 ? 1 : 0);
186 }
187
188 free(binstr);
189 }
190 else {
191 fprintf(stderr, "%s: invalid watermark file %s\n", progname, input_name);
192 exit(1);
193 }
194
195 if (verbose > 0) {
196 fprintf(stderr, "signature length: %d\n", nbit_signature1);
197 fprintf(stderr, "watermark length: %d\n", nbit_signature);
198 }
199
200 for (i = 0; i < nbit_signature; i++)
201 if (get_signature1_bit(i % nbit_signature1) == get_signature2_bit(i))
202 corr++, match++;
203 else
204 corr--;
205
206 if (correlation_only)
207 fprintf(out, "%lf\n", (double) corr / nbit_signature2);
208 else {
209 fprintf(stderr, "redundant blocks: %d\n", k);
210 fprintf(out, "bit matches: %d/%d\n", match, nbit_signature2);
211 fprintf(out, "correlation: %lf\n", (double) corr / nbit_signature2);
212 }
213
214 exit(0);
215 }

Repositories maintained by Peter Meerwald, pmeerw@pmeerw.net.