comparison Meerwald-dir/cmp_koch_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_koch_sig.c@3bdb67e76858
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 c, i;
27 double quality = 0.0;
28 int quantization = 0;
29 int corr = 0, match = 0;
30 int verbose = 0;
31
32 int correlation_only = 0;
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
89 if (sig) {
90 fgets(line, sizeof(line), sig);
91 if (strspn(line, "KCSG") >= 4) {
92 fscanf(sig, "%d\n", &nbit_signature1);
93 fscanf(sig, "%lf\n", &quality);
94 fscanf(sig, "%d\n", &quantization);
95 fscanf(sig, "%d\n", &seed);
96 srandom(seed);
97 n_signature1 = NBITSTOBYTES(nbit_signature1);
98 fread(signature1, sizeof(char), n_signature1, sig);
99 fscanf(sig, "\n");
100 }
101 else {
102 fprintf(stderr, "%s: invalid signature file %s\n", progname, signature_name);
103 exit(1);
104 }
105 fclose(sig);
106 }
107 else {
108 fprintf(stderr, "%s: original signature file not specified, use -s file option\n", progname);
109 exit(1);
110 }
111
112 fgets(line, sizeof(line), in);
113 if (strspn(line, "KCWM") >= 4) {
114 fscanf(in, "%d\n", &nbit_signature2);
115 n_signature2 = NBITSTOBYTES(nbit_signature2);
116 fread(signature2, sizeof(char), n_signature2, in);
117 fscanf(in, "\n");
118 }
119 else {
120 fprintf(stderr, "%s: invalid watermark file %s\n", progname, input_name);
121 exit(1);
122 }
123
124 if (verbose > 0) {
125 fprintf(stderr, "signature length: %d\n", nbit_signature1);
126 fprintf(stderr, "watermark length: %d\n", nbit_signature2);
127 }
128
129 for (i = 0; i < nbit_signature2; i++)
130 if (get_signature1_bit(i % nbit_signature1) == get_signature2_bit(i))
131 corr++, match++;
132 else
133 corr--;
134
135 if (verbose > 1) {
136 for (i = 0; i < nbit_signature2; i++)
137 fprintf(stderr, "%d", get_signature1_bit(i % nbit_signature1));
138 fprintf(stderr, "\n");
139 for (i = 0; i < nbit_signature2; i++)
140 fprintf(stderr, "%d", get_signature2_bit(i));
141 fprintf(stderr, "\n");
142 }
143
144 if (correlation_only)
145 fprintf(out, "%lf\n", (double) corr / nbit_signature2);
146 else {
147 fprintf(out, "bit matches: %d/%d\n", match, nbit_signature2);
148 fprintf(out, "correlation: %lf\n", (double) corr / nbit_signature2);
149 }
150
151 exit(0);
152 }

Repositories maintained by Peter Meerwald, pmeerw@pmeerw.net.