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