Mercurial > hg > wm
comparison Meerwald-dir/cmp_kund2_sig.c @ 24:9f20bce6184e v0.7
move directories, support netpbm 11
author | Peter Meerwald-Stadler <pmeerw@pmeerw.net> |
---|---|
date | Fri, 20 Dec 2024 13:08:59 +0100 |
parents | Meerwald/cmp_kund2_sig.c@71dd4b96221b |
children |
comparison
equal
deleted
inserted
replaced
23:71dd4b96221b | 24:9f20bce6184e |
---|---|
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 | |
96 if (sig) { | |
97 fgets(line, sizeof(line), sig); | |
98 if (strspn(line, "KD2SG") >= 5) { | |
99 fscanf(sig, "%d\n", &nbit_signature1); | |
100 fscanf(sig, "%d\n", &quality); | |
101 fscanf(sig, "%d\n", &blocksize); | |
102 fscanf(sig, "%d\n", &method); | |
103 fscanf(sig, "%d\n", &filter); | |
104 fscanf(sig, "%[^\n\r]\n", filter_name); | |
105 fscanf(sig, "%d\n", &level); | |
106 fscanf(sig, "%d\n", &seed); | |
107 srandom(seed); | |
108 n_signature1 = NBITSTOBYTES(nbit_signature1); | |
109 | |
110 binstr = malloc((nbit_signature1 + 1) * sizeof(char)); | |
111 fscanf(sig, "%[01]\n", binstr); | |
112 binstr_to_sig1(binstr); | |
113 free(binstr); | |
114 } | |
115 else { | |
116 fprintf(stderr, "%s: invalid signature file %s\n", progname, signature_name); | |
117 exit(1); | |
118 } | |
119 fclose(sig); | |
120 } | |
121 else { | |
122 fprintf(stderr, "%s: original signature file not specified, use -s file option\n", progname); | |
123 exit(1); | |
124 } | |
125 | |
126 fgets(line, sizeof(line), in); | |
127 if (strspn(line, "KD2WM") >= 5) { | |
128 int max_nbit_signature; | |
129 int min_nbit_signature = -1; | |
130 double pe[100]; | |
131 double pe_sum; | |
132 double alpha[100]; | |
133 char *w[100]; | |
134 | |
135 fscanf(in, "%d\n", &max_nbit_signature); | |
136 | |
137 k = 0; | |
138 while (!feof(in) && k < 100) { | |
139 int e; | |
140 | |
141 fscanf(in, "%d\n", &nbit_signature2); | |
142 w[k] = binstr = malloc(sizeof(char) * (nbit_signature2 + 1)); | |
143 fscanf(in, "%[01]\n", binstr); | |
144 | |
145 binstr_to_sig2(binstr); | |
146 | |
147 if (nbit_signature2 < min_nbit_signature || min_nbit_signature == -1) | |
148 min_nbit_signature = nbit_signature2; | |
149 e = 0; | |
150 for (i = 0; i < nbit_signature2; i += 2) { | |
151 if (get_signature1_bit(i % nbit_signature1) != get_signature2_bit(i)) | |
152 e++; | |
153 } | |
154 if (e > 0) | |
155 pe[k++] = log( (1 - (e / (double) nbit_signature2)) / (e / (double) nbit_signature2)); | |
156 else | |
157 pe[k++] = 0; | |
158 } | |
159 | |
160 pe_sum = 0.0; | |
161 for (i = 0; i < k; i++) { | |
162 // fprintf(stderr, "XXX pe[%d] = %f\n", i, pe[i]); | |
163 pe_sum += pe[i]; | |
164 } | |
165 | |
166 for (i = 0; i < k; i++) { | |
167 if (pe_sum != 0) | |
168 alpha[i] = pe[i] / pe_sum; | |
169 else | |
170 alpha[i] = 1.0; | |
171 } | |
172 | |
173 nbit_signature = min_nbit_signature; | |
174 for (i = 0; i < min_nbit_signature; i++) { | |
175 double s = 0.0; | |
176 int j; | |
177 | |
178 for (j = 0; j < k; j++) { | |
179 int bit; | |
180 //fprintf(stderr, "XXX %d %d\n", i, j); | |
181 binstr_to_sig2(w[j]); | |
182 bit = get_signature2_bit(i) ? 1 : -1; | |
183 s += alpha[j] * bit; | |
184 } | |
185 // fprintf(stderr, "YYY %d %f\n", i, s); | |
186 set_signature_bit(i, s > 0 ? 1 : 0); | |
187 } | |
188 } | |
189 else { | |
190 fprintf(stderr, "%s: invalid watermark file %s\n", progname, input_name); | |
191 exit(1); | |
192 } | |
193 | |
194 if (verbose > 0) { | |
195 fprintf(stderr, "signature length: %d\n", nbit_signature1); | |
196 fprintf(stderr, "watermark length: %d\n", nbit_signature); | |
197 } | |
198 | |
199 for (i = 0; i < nbit_signature; i++) | |
200 if (get_signature1_bit(i % nbit_signature1) == get_signature2_bit(i)) | |
201 corr++, match++; | |
202 else | |
203 corr--; | |
204 | |
205 if (correlation_only) | |
206 fprintf(out, "%lf\n", (double) corr / nbit_signature2); | |
207 else { | |
208 fprintf(stderr, "redundant blocks: %d\n", k); | |
209 fprintf(out, "bit matches: %d/%d\n", match, nbit_signature2); | |
210 fprintf(out, "correlation: %lf\n", (double) corr / nbit_signature2); | |
211 } | |
212 | |
213 exit(0); | |
214 } |