Mercurial > hg > wm
comparison Meerwald/gen_kund2_sig.c @ 3:acb6967ee76d
update to 0.5
author | Peter Meerwald <pmeerw@cosy.sbg.ac.at> |
---|---|
date | Tue, 14 Aug 2007 21:11:21 +0200 |
parents | |
children | f83ef905a63d |
comparison
equal
deleted
inserted
replaced
2:b92f06d9a967 | 3:acb6967ee76d |
---|---|
1 #include "wm.h" | |
2 #include "signature.h" | |
3 | |
4 char *progname; | |
5 | |
6 void usage(void) { | |
7 fprintf(stderr, "usage: %s [-b n] [-e n] [-f n] [-F file] [-l n] [-n n] [-o file] [-q n] [-s file] [-S n] [-v n] file\n\n", progname); | |
8 fprintf(stderr, "\t-h\t\tprint usage\n"); | |
9 fprintf(stderr, "\t-b n\t\tblock size (default 64)\n"); | |
10 fprintf(stderr, "\t-e n\t\twavelet filtering method (default 2)\n"); | |
11 fprintf(stderr, "\t-f n\t\tfilter number (default 1)\n"); | |
12 fprintf(stderr, "\t-F file\t\tfilter definition file (default 'filter.dat')\n"); | |
13 fprintf(stderr, "\t-l n\t\tembedding level (default 1)\n"); | |
14 fprintf(stderr, "\t-n n\t\twatermark bit length\n"); | |
15 fprintf(stderr, "\t-o file\t\toutput file\n"); | |
16 fprintf(stderr, "\t-q n\t\tsignature strength (default 4)\n"); | |
17 fprintf(stderr, "\t-s file\t\tuse signature file's embedding information\n"); | |
18 fprintf(stderr, "\t-S n\t\tseed\n"); | |
19 fprintf(stderr, "\t-v n\t\tverbosity level\n"); | |
20 exit(0); | |
21 } | |
22 | |
23 int main(int argc, char *argv[]) { | |
24 FILE *in = stdin; | |
25 FILE *out = stdout; | |
26 FILE *sig = NULL; | |
27 | |
28 char output_name[MAXPATHLEN] = "(stdout)"; | |
29 char input_name[MAXPATHLEN] = "(stdin)"; | |
30 char signature_name[MAXPATHLEN]; | |
31 char *binstr; | |
32 | |
33 int verbose = 0; | |
34 int c; | |
35 int i; | |
36 int l = 3; | |
37 int n = 0; | |
38 int s = 0; | |
39 int q = 4; | |
40 int e = 2; | |
41 int f = 1; | |
42 int blocksize = 64; | |
43 char F[MAXPATHLEN] = "filter.dat"; | |
44 | |
45 progname = argv[0]; | |
46 | |
47 wm_init(); | |
48 | |
49 while ((c = getopt(argc, argv, "b:e:f:F:h?l:n:o:q:s:S:v:")) != EOF) { | |
50 switch (c) { | |
51 case 'b': | |
52 blocksize = atoi(optarg); | |
53 if (blocksize < 0) | |
54 fprintf(stderr, "%s: block size %d out of range\n", progname, blocksize); | |
55 break; | |
56 case 'e': | |
57 e = atoi(optarg); | |
58 if (e < 0) { | |
59 fprintf(stderr, "%s: wavelet filtering method %d out of range\n", progname, e); | |
60 } | |
61 break; | |
62 case 'f': | |
63 f = atoi(optarg); | |
64 if (f <= 0) { | |
65 fprintf(stderr, "%s: filter number %d out of range\n", progname, f); | |
66 exit(1); | |
67 } | |
68 break; | |
69 case 'F': | |
70 strcpy(F, optarg); | |
71 break; | |
72 case 'h': | |
73 case '?': | |
74 usage(); | |
75 break; | |
76 case 'l': | |
77 l = atoi(optarg); | |
78 if (l < 1) { | |
79 fprintf(stderr, "%s: embedding level out of range\n", progname); | |
80 exit(1); | |
81 } | |
82 break; | |
83 case 'n': | |
84 n = atoi(optarg); | |
85 if (n < 1 || n > 1000) { | |
86 fprintf(stderr, "%s: watermark length %d out of range\n", progname, n); | |
87 exit(1); | |
88 } | |
89 break; | |
90 case 'o': | |
91 if ((out = fopen(optarg, "wb")) == NULL) { | |
92 fprintf(stderr, "%s: unable to open output file %s\n", progname, optarg); | |
93 exit(1); | |
94 } | |
95 strcpy(output_name, optarg); | |
96 break; | |
97 case 'q': | |
98 q = atoi(optarg); | |
99 if (q < 1) { | |
100 fprintf(stderr, "%s: signature strength %d out of range\n", progname, q); | |
101 exit(1); | |
102 } | |
103 break; | |
104 case 's': | |
105 if ((sig = fopen(optarg, "r")) == NULL) { | |
106 fprintf(stderr, "%s: unable to open signature file %s\n", progname, optarg); | |
107 exit(1); | |
108 } | |
109 strcpy(signature_name, optarg); | |
110 break; | |
111 case 'S': | |
112 s = atoi(optarg); | |
113 break; | |
114 case 'v': | |
115 verbose = atoi(optarg); | |
116 if (verbose < 0) { | |
117 fprintf(stderr, "%s: verbosity level %d out of range", progname, verbose); | |
118 exit(1); | |
119 } | |
120 break; | |
121 } | |
122 } | |
123 | |
124 argc -= optind; | |
125 argv += optind; | |
126 | |
127 if (argc > 1) { | |
128 usage(); | |
129 exit(1); | |
130 } | |
131 | |
132 if (argc == 1 && *argv[0] != '-') | |
133 if ((in = fopen(argv[0], "rb")) == NULL) { | |
134 fprintf(stderr, "%s: unable to open input file %s\n", progname, argv[0]); | |
135 exit(1); | |
136 } | |
137 else | |
138 strcpy(input_name, argv[0]); | |
139 | |
140 if (sig) { | |
141 char line[32]; | |
142 fgets(line, sizeof(line), sig); | |
143 if (strspn(line, "KD2SG") >= 5) { | |
144 if (n == 0) | |
145 fscanf(sig, "%d\n", &n); | |
146 else | |
147 fscanf(sig, "%*d\n"); | |
148 if (q == 0) | |
149 fscanf(sig, "%d\n", &q); | |
150 else | |
151 fscanf(sig, "%*d\n"); | |
152 if (blocksize == 0) | |
153 fscanf(sig, "%d\n", &blocksize); | |
154 else | |
155 fscanf(sig, "%*d\n"); | |
156 if (e < 0) | |
157 fscanf(sig, "%d\n", &e); | |
158 else | |
159 fscanf(sig, "%*d\n"); | |
160 if (f == 0) | |
161 fscanf(sig, "%d\n", &f); | |
162 else | |
163 fscanf(sig, "%*d\n"); | |
164 if (!strcmp(F, "")) | |
165 fscanf(sig, "%[^\n\r]\n", &F); | |
166 else | |
167 fscanf(sig, "%*[^\n\r]\n"); | |
168 if (l == 0) | |
169 fscanf(sig, "%d\n", &l); | |
170 else | |
171 fscanf(sig, "%*d\n"); | |
172 } | |
173 else { | |
174 fprintf(stderr, "%s: invalid signature file %s\n", progname, signature_name); | |
175 exit(1); | |
176 } | |
177 close(sig); | |
178 } | |
179 | |
180 if (s) | |
181 srandom(s); | |
182 else | |
183 srandom(time(NULL) * getpid()); | |
184 | |
185 if (n > 0) { | |
186 n_signature = fread(signature, sizeof(char), i = NBITSTOBYTES(n), in); | |
187 nbit_signature = NBYTESTOBITS(n_signature); | |
188 if (n_signature < i) { | |
189 fprintf(stderr, "%s: failed to read all %d signature bits from %s\n", progname, n, input_name); | |
190 exit(1); | |
191 } | |
192 } | |
193 else { | |
194 if (fscanf(in, "%128[^\n\r]", signature) == EOF) { | |
195 fprintf(stderr, "%s: failed to read signature bits from %s\n", progname, input_name); | |
196 exit(1); | |
197 } | |
198 n_signature = strlen(signature); | |
199 nbit_signature = NBYTESTOBITS(n_signature); | |
200 fprintf(stderr, "%s: got %d signature bits\n", progname, nbit_signature); | |
201 } | |
202 | |
203 fprintf(out, "KD2SG\n"); | |
204 fprintf(out, "%d\n", nbit_signature); | |
205 fprintf(out, "%d\n", q); | |
206 fprintf(out, "%d\n", blocksize); | |
207 fprintf(out, "%d\n", e); | |
208 fprintf(out, "%d\n", f); | |
209 fprintf(out, "%s\n", F); | |
210 fprintf(out, "%d\n", l); | |
211 fprintf(out, "%d\n", random()); | |
212 binstr = malloc((nbit_signature + 1) * sizeof(char)); | |
213 sig_to_binstr(binstr); | |
214 fprintf(out, "%s\n", binstr); | |
215 free(binstr); | |
216 | |
217 fclose(out); | |
218 | |
219 exit(0); | |
220 } |