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

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