comparison Meerwald/cmp_xie_sig.c @ 0:be303a3f5ea8

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

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