Mercurial > hg > wm
comparison Meerwald/cmp_frid2_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 #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 int correlation_only = 0; | |
27 | |
28 int c, i; | |
29 int corr1 = 0, match1 = 0; | |
30 int corr2 = 0, match2 = 0; | |
31 int verbose = 0; | |
32 char filter_name[MAXPATHLEN] = ""; | |
33 | |
34 int seed; | |
35 char line[32]; | |
36 | |
37 progname = argv[0]; | |
38 | |
39 while ((c = getopt(argc, argv, "h?Co:s:v:")) != EOF) { | |
40 switch (c) { | |
41 case 'h': | |
42 case '?': | |
43 usage(); | |
44 break; | |
45 case 'C': | |
46 correlation_only = 1; | |
47 break; | |
48 case 'o': | |
49 if ((out = fopen(optarg, "w")) == NULL) { | |
50 fprintf(stderr, "%s: unable to open output file %s\n", progname, optarg); | |
51 exit(1); | |
52 } | |
53 strcpy(output_name, optarg); | |
54 break; | |
55 case 's': | |
56 if ((sig = fopen(optarg, "r")) == NULL) { | |
57 fprintf(stderr, "%s: unable to open signature file %s\n", progname, optarg); | |
58 exit(1); | |
59 } | |
60 strcpy(signature_name, optarg); | |
61 break; | |
62 case 'v': | |
63 verbose = atoi(optarg); | |
64 if (verbose < 0) { | |
65 fprintf(stderr, "%s: verbosity level %d out of range\n", progname, verbose); | |
66 exit(1); | |
67 } | |
68 break; | |
69 } | |
70 } | |
71 | |
72 argc -= optind; | |
73 argv += optind; | |
74 | |
75 if (argc > 1) { | |
76 usage(); | |
77 exit(1); | |
78 } | |
79 | |
80 if (argc == 1 && *argv[0] != '-') | |
81 if ((in = fopen(argv[0], "r")) == NULL) { | |
82 fprintf(stderr, "%s: unable to open input file %s\n", progname, argv[0]); | |
83 exit(1); | |
84 } | |
85 else | |
86 strcpy(input_name, argv[0]); | |
87 | |
88 if (sig) { | |
89 fgets(line, sizeof(line), sig); | |
90 if (strspn(line, "FR2SG") >= 5) { | |
91 fscanf(sig, "%d\n", &nbit_signature); | |
92 fscanf(sig, "%*lf\n"); | |
93 fscanf(sig, "%*lf\n"); | |
94 fscanf(sig, "%*d\n"); | |
95 n_signature = NBITSTOBYTES(nbit_signature); | |
96 fread(signature, sizeof(char), n_signature, sig); | |
97 fscanf(sig, "\n"); | |
98 } | |
99 else { | |
100 fprintf(stderr, "%s: invalid signature file %s\n", progname, signature_name); | |
101 exit(1); | |
102 } | |
103 close(sig); | |
104 } | |
105 else { | |
106 fprintf(stderr, "%s: original signature file not specified, use -s file option\n", progname); | |
107 exit(1); | |
108 } | |
109 | |
110 fgets(line, sizeof(line), in); | |
111 if (strspn(line, "FR2WM") >= 5) { | |
112 fscanf(in, "%d\n", &nbit_signature1); | |
113 n_signature1 = NBITSTOBYTES(nbit_signature1); | |
114 fread(signature1, sizeof(char), n_signature1, in); | |
115 // fscanf(in, "\n"); | |
116 fscanf(in, "%d\n", &nbit_signature2); | |
117 n_signature2 = NBITSTOBYTES(nbit_signature2); | |
118 fread(signature2, sizeof(char), n_signature2, in); | |
119 fscanf(in, "\n"); | |
120 } | |
121 else { | |
122 fprintf(stderr, "%s: invalid watermark file %s\n", progname, input_name); | |
123 exit(1); | |
124 } | |
125 | |
126 if (verbose > 0) { | |
127 fprintf(stderr, "signature length: %d\n", nbit_signature); | |
128 fprintf(stderr, "watermark length (low. freq.): %d\n", nbit_signature1); | |
129 fprintf(stderr, "watermark length (med. freq.): %d\n", nbit_signature2); | |
130 } | |
131 | |
132 for (i = 0; i < nbit_signature; i++) { | |
133 if (get_signature_bit(i) == get_signature1_bit(i)) | |
134 corr1++, match1++; | |
135 else | |
136 corr1--; | |
137 if (get_signature_bit(i) == get_signature2_bit(i)) | |
138 corr2++, match2++; | |
139 else | |
140 corr2--; | |
141 } | |
142 | |
143 if (correlation_only) | |
144 fprintf(out, "%lf\n", (double) (corr1 + corr2) / (nbit_signature1 + nbit_signature2)); | |
145 else { | |
146 fprintf(out, "bit matches (low freq.): %d/%d\n", match1, nbit_signature1); | |
147 fprintf(out, "correlation (low. freq.): %lf\n", (double) corr1 / nbit_signature1); | |
148 fprintf(out, "bit matches (med. freq.): %d/%d\n", match2, nbit_signature2); | |
149 fprintf(out, "correlation (med. freq.): %lf\n", (double) corr2 / nbit_signature2); | |
150 fprintf(out, "total correlation: %lf\n", (double) (corr1 + corr2) / (nbit_signature1 + nbit_signature2)); | |
151 } | |
152 | |
153 exit(0); | |
154 } |