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