Mercurial > hg > wm
annotate Meerwald/cmp_wang_sig.c @ 20:bd669312f068
suppress warnings, fix link errors
author | Peter Meerwald-Stadler <pmeerw@pmeerw.net> |
---|---|
date | Sat, 28 Jan 2023 23:54:58 +0100 |
parents | f83ef905a63d |
children |
rev | line source |
---|---|
0 | 1 #include "wm.h" |
2 | |
3 char *progname; | |
4 | |
5 void usage(void) { | |
20
bd669312f068
suppress warnings, fix link errors
Peter Meerwald-Stadler <pmeerw@pmeerw.net>
parents:
8
diff
changeset
|
6 fprintf(stderr, "usage: %s [-h] [-o file] [-v n] -s file file\n\n", progname); |
0 | 7 fprintf(stderr, "\t-h\t\tprint usage\n"); |
8 fprintf(stderr, "\t-o file\t\toutput file\n"); | |
9 fprintf(stderr, "\t-v n\t\tverbosity level\n"); | |
10 fprintf(stderr, "\t-s file\t\toriginal signature file\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, sig_b; | |
27 int sig_e, sig_f; | |
28 double s1, s2, s3; | |
29 char line[32]; | |
30 | |
31 int verbose = 0; | |
32 | |
33 progname = argv[0]; | |
34 | |
20
bd669312f068
suppress warnings, fix link errors
Peter Meerwald-Stadler <pmeerw@pmeerw.net>
parents:
8
diff
changeset
|
35 while ((c = getopt(argc, argv, "h?o:s:v:")) != EOF) { |
0 | 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 | |
8 | 73 if (argc == 1 && *argv[0] != '-') { |
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]); | |
8 | 80 } |
20
bd669312f068
suppress warnings, fix link errors
Peter Meerwald-Stadler <pmeerw@pmeerw.net>
parents:
8
diff
changeset
|
81 |
0 | 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, "WGSG") < 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, "WGWM") < 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 != sig_n) { | |
110 fprintf(stderr, "%s: invalid watermark length %d, does not match signature length\n", progname, in_n); | |
111 exit(1); | |
112 } | |
113 | |
114 fscanf(sig, "%lf\n", &sig_a); | |
115 fscanf(sig, "%lf\n", &sig_b); | |
116 fscanf(sig, "%d\n", &sig_e); | |
117 fscanf(sig, "%d\n", &sig_f); | |
118 fscanf(sig, "%*[^\n\r]\n"); | |
119 | |
120 /* | |
121 * normalized correlation | |
122 * Craver, S., "Can Invisible Watermarks Resolve Rightful Ownership?", IBM Research Report, 1996, p. 5 | |
123 */ | |
124 | |
125 s1 = s2 = s3 = 0.0; | |
126 while (in_n > 0) { | |
127 double sig_x, in_x; | |
128 | |
129 fscanf(sig, "%lf\n", &sig_x); | |
130 fscanf(in, "%lf\n", &in_x); | |
131 | |
132 if (verbose >= 1) { | |
133 fprintf(stderr, "orig %f input %f\n", sig_x, in_x); | |
134 } | |
135 | |
136 s1 += sig_x * in_x; | |
137 s2 += in_x * in_x; | |
138 s3 += sig_x * sig_x; | |
139 | |
140 in_n--; | |
141 } | |
142 | |
143 fprintf(out, "%f\n", s1 / sqrt(s2 * s3)); | |
144 | |
145 fclose(sig); | |
146 fclose(out); | |
147 fclose(in); | |
148 | |
149 exit(0); | |
150 } |