comparison Meerwald-dir/cmp_corvi_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_corvi_sig.c@4987db85cfae
children
comparison
equal deleted inserted replaced
23:71dd4b96221b 24:9f20bce6184e
1 #include "wm.h"
2
3 char *progname;
4
5 void usage(void) {
6 fprintf(stderr, "usage: %s [-h] [-C] [-o file] [-v n] -s file file\n\n", progname);
7 fprintf(stderr, "\t-C\t\toutput correlation only\n");
8 fprintf(stderr, "\t-h\t\tprint usage\n");
9 fprintf(stderr, "\t-o file\t\toutput file\n");
10 fprintf(stderr, "\t-v n\t\tverbosity level\n");
11 fprintf(stderr, "\t-s file\t\toriginal signature file\n");
12 exit(0);
13 }
14
15 int main(int argc, char *argv[]) {
16
17 FILE *in = stdin;
18 FILE *out = stdout;
19 FILE *sig = NULL;
20
21 char signature_name[MAXPATHLEN];
22 char output_name[MAXPATHLEN] = "(stdout)";
23 char input_name[MAXPATHLEN] = "(stdin)";
24
25 int c;
26 int sig_n, in_n;
27 double s1, s2, s3;
28 char line[32];
29 int matches;
30
31 int verbose = 0;
32 int correlation_only = 0;
33
34 progname = argv[0];
35
36 while ((c = getopt(argc, argv, "h?o:s:v:C")) != EOF) {
37 switch (c) {
38 case 'h':
39 case '?':
40 usage();
41 break;
42 case 'o':
43 if ((out = fopen(optarg, "w")) == NULL) {
44 fprintf(stderr, "%s: unable to open output file %s\n", progname, optarg);
45 exit(1);
46 }
47 strcpy(output_name, optarg);
48 break;
49 case 's':
50 if ((sig = fopen(optarg, "r")) == NULL) {
51 fprintf(stderr, "%s: unable to open signature file %s\n", progname, optarg);
52 exit(1);
53 }
54 strcpy(signature_name, optarg);
55 break;
56 case 'v':
57 verbose = atoi(optarg);
58 if (verbose < 0) {
59 fprintf(stderr, "%s: verbosity level %d out of range\n", progname, verbose);
60 exit(1);
61 }
62 break;
63 case 'C':
64 correlation_only = 1;
65 break;
66 }
67 }
68
69 argc -= optind;
70 argv += optind;
71
72 if (argc > 1) {
73 usage();
74 exit(1);
75 }
76
77 if (argc == 1 && *argv[0] != '-') {
78 if ((in = fopen(argv[0], "r")) == NULL) {
79 fprintf(stderr, "%s: unable to open input file %s\n", progname, argv[0]);
80 exit(1);
81 }
82 else
83 strcpy(input_name, argv[0]);
84 }
85
86 if (!sig) {
87 fprintf(stderr, "%s: original signature file not specified, use -s file option\n", progname);
88 exit(1);
89 }
90
91 fgets(line, sizeof(line), sig);
92 if (strspn(line, "CVSG") < 4) {
93 fprintf(stderr, "%s: original signature file %s invalid\n", progname, signature_name);
94 exit(1);
95 }
96
97 fgets(line, sizeof(line), in);
98 if (strspn(line, "CVWM") < 4) {
99 fprintf(stderr, "%s: signature file %s invalid\n", progname, input_name);
100 exit(1);
101 }
102
103 fscanf(sig, "%d\n", &sig_n);
104 fscanf(in, "%d\n", &in_n);
105 if (sig_n != in_n) {
106 fprintf(stderr, "%s: watermark length mismatch (original %d, input %d)\n", progname, sig_n, in_n);
107 exit(1);
108 }
109 if (sig_n <= 0 || sig_n > 1000) {
110 fprintf(stderr, "%s: invalid original watermark length %d\n", progname, sig_n);
111 exit(1);
112 }
113 if (in_n <= 0 || in_n > 1000) {
114 fprintf(stderr, "%s: invalid watermark length %d\n", progname, in_n);
115 exit(1);
116 }
117
118 fscanf(sig, "%*f\n");
119 fscanf(sig, "%*d\n");
120 fscanf(sig, "%*d\n");
121 fscanf(sig, "%*[^\n\r]\n");
122
123 /*
124 * normalized correlation
125 * Craver, S., "Can Invisible Watermarks Resolve Rightful Ownership?", IBM Research Report, 1996, p. 5
126 */
127
128 s1 = s2 = s3 = 0.0;
129 matches = 0;
130 while (in_n > 0) {
131 double sig_x, in_x;
132
133 fscanf(sig, "%lf\n", &sig_x);
134 fscanf(in, "%lf\n", &in_x);
135
136 matches += SIGN(sig_x * in_x);
137
138 if (verbose >= 1) {
139 fprintf(stderr, "orig %f input %f\n", sig_x, in_x);
140 }
141
142 s1 += sig_x * in_x;
143 s2 += in_x * in_x;
144 s3 += sig_x * sig_x;
145
146 in_n--;
147 }
148
149 if (!correlation_only) {
150 fprintf(out, "%s: correlation %f, hamming distance %f\n", progname, s1 / sqrt(s2 * s3), (double) matches / sig_n);
151 }
152 else
153 fprintf(out, "%f\n", (double) matches / sig_n);
154
155 fclose(sig);
156 fclose(out);
157 fclose(in);
158
159 exit(0);
160 }

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