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