Mercurial > hg > wm
comparison Meerwald/wm_kund3_d.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 #include "dwt.h" | |
4 #include "pgm.h" | |
5 | |
6 char *progname; | |
7 | |
8 void usage(void) { | |
9 fprintf(stderr, "usage: %s [-e n] [-f n] [-F n] [-h] [-l n] [-o file] [-q n] [-s file] [-v n] file\n\n", progname); | |
10 fprintf(stderr, "\t-e n\t\twavelet filtering method\n"); | |
11 fprintf(stderr, "\t-f n\t\tfilter number\n"); | |
12 fprintf(stderr, "\t-F file\t\tfilter definition file\n"); | |
13 fprintf(stderr, "\t-h\t\tprint usage\n"); | |
14 fprintf(stderr, "\t-l n\t\tembedding level\n"); | |
15 fprintf(stderr, "\t-o file\t\textracted signature file\n"); | |
16 fprintf(stderr, "\t-q n\t\tsignature strength\n"); | |
17 fprintf(stderr, "\t-s file\t\toriginal signature file\n"); | |
18 fprintf(stderr, "\t-v n\t\tverbosity level\n"); | |
19 exit(0); | |
20 } | |
21 | |
22 int main(int argc, char *argv[]) { | |
23 | |
24 FILE *in = stdin; | |
25 FILE *out = stdout; | |
26 FILE *sig = NULL; | |
27 | |
28 gray **input_image; | |
29 | |
30 char signature_name[MAXPATHLEN]; | |
31 char output_name[MAXPATHLEN] = "(stdout)"; | |
32 char input_name[MAXPATHLEN] = "(stdin)"; | |
33 | |
34 int r, c; | |
35 int i; | |
36 int quality = 0; | |
37 int seed = 0; | |
38 int n = 0; | |
39 int method = -1; | |
40 int filter = 0; | |
41 char filter_name[MAXPATHLEN] = ""; | |
42 | |
43 char *binstr; | |
44 | |
45 int level = 0; | |
46 double alpha = 0.0; | |
47 | |
48 int in_rows, in_cols, in_format; | |
49 gray in_maxval; | |
50 int rows, cols; | |
51 int row, col; | |
52 | |
53 Image_tree dwts, p; | |
54 | |
55 int verbose = 0; | |
56 | |
57 progname = argv[0]; | |
58 | |
59 pgm_init(&argc, argv); | |
60 wm_init(); | |
61 | |
62 while ((c = getopt(argc, argv, "e:f:F:h?l:o:q:s:v:")) != EOF) { | |
63 switch (c) { | |
64 case 'e': | |
65 method = atoi(optarg); | |
66 if (method < 0) { | |
67 fprintf(stderr, "%s: wavelet filtering method %d out of range\n", progname, method); | |
68 exit(1); | |
69 } | |
70 break; | |
71 case 'f': | |
72 filter = atoi(optarg); | |
73 if (filter <= 0) { | |
74 fprintf(stderr, "%s: filter number %d out of range\n", progname, filter); | |
75 exit(1); | |
76 } | |
77 break; | |
78 case 'F': | |
79 strcpy(filter_name, optarg); | |
80 break; | |
81 case 'h': | |
82 case '?': | |
83 usage(); | |
84 break; | |
85 case 'l': | |
86 level = atoi(optarg); | |
87 if (level < 1) { | |
88 fprintf(stderr, "%s: embedding level out of range\n", progname); | |
89 exit(1); | |
90 } | |
91 break; | |
92 case 'o': | |
93 if ((out = fopen(optarg, "w")) == NULL) { | |
94 fprintf(stderr, "%s: unable to open output file %s\n", progname, optarg); | |
95 exit(1); | |
96 } | |
97 strcpy(output_name, optarg); | |
98 break; | |
99 case 'q': | |
100 quality = atoi(optarg); | |
101 if (quality < 1) { | |
102 fprintf(stderr, "%s: quality level %d out of range\n", progname, quality); | |
103 exit(1); | |
104 } | |
105 break; | |
106 case 's': | |
107 if ((sig = fopen(optarg, "r")) == NULL) { | |
108 fprintf(stderr, "%s: unable to open signature file %s\n", progname, optarg); | |
109 exit(1); | |
110 } | |
111 strcpy(signature_name, optarg); | |
112 break; | |
113 case 'v': | |
114 verbose = atoi(optarg); | |
115 if (verbose < 0) { | |
116 fprintf(stderr, "%s: verbosity level %d out of range\n", progname, verbose); | |
117 exit(1); | |
118 } | |
119 break; | |
120 } | |
121 } | |
122 | |
123 argc -= optind; | |
124 argv += optind; | |
125 | |
126 if (argc > 1) { | |
127 usage(); | |
128 exit(1); | |
129 } | |
130 | |
131 if (argc == 1 && *argv[0] != '-') | |
132 if ((in = fopen(argv[0], "rb")) == NULL) { | |
133 fprintf(stderr, "%s: unable to open input file %s\n", progname, argv[0]); | |
134 exit(1); | |
135 } | |
136 else | |
137 strcpy(input_name, argv[0]); | |
138 | |
139 if (sig) { | |
140 char line[32]; | |
141 fgets(line, sizeof(line), sig); | |
142 if (strspn(line, "KD3SG") >= 5) { | |
143 fscanf(sig, "%d\n", &nbit_signature); | |
144 if (quality == 0) | |
145 fscanf(sig, "%d\n", &quality); | |
146 else | |
147 fscanf(sig, "%*d\n"); | |
148 if (method < 0) | |
149 fscanf(sig, "%d\n", &method); | |
150 else | |
151 fscanf(sig, "%*d\n"); | |
152 if (filter == 0) | |
153 fscanf(sig, "%d\n", &filter); | |
154 else | |
155 fscanf(sig, "%*d\n"); | |
156 if (!strcmp(filter_name, "")) | |
157 fscanf(sig, "%[^\n\r]\n", &filter_name); | |
158 else | |
159 fscanf(sig, "%*[^\n\r]\n"); | |
160 if (level == 0) | |
161 fscanf(sig, "%d\n", &level); | |
162 else | |
163 fscanf(sig, "%*d\n"); | |
164 fscanf(sig, "%d\n", &seed); | |
165 srandom(seed); | |
166 nbit_signature2 = nbit_signature; | |
167 n_signature = n_signature2 = NBITSTOBYTES(nbit_signature2); | |
168 binstr = malloc((nbit_signature2 + 1) * sizeof(char)); | |
169 fscanf(sig, "%[01]\n", binstr); | |
170 binstr_to_sig2(binstr); | |
171 free(binstr); | |
172 init_signature_bits(); | |
173 } | |
174 else { | |
175 fprintf(stderr, "%s: invalid signature file %s\n", progname, signature_name); | |
176 exit(1); | |
177 } | |
178 fclose(sig); | |
179 } | |
180 else { | |
181 fprintf(stderr, "%s: signature file not specified, use -s file option\n", progname); | |
182 exit(1); | |
183 } | |
184 | |
185 pgm_readpgminit(in, &in_cols, &in_rows, &in_maxval, &in_format); | |
186 | |
187 cols = in_cols; | |
188 rows = in_rows; | |
189 | |
190 if (verbose > 0) | |
191 fprintf(stderr, "%s: extracting %d bits with quality %d from\n" | |
192 " %d x %d host image, decomposition level %d\n", | |
193 progname, nbit_signature, quality, cols, rows, level); | |
194 | |
195 input_image = pgm_allocarray(in_cols, in_rows); | |
196 | |
197 for (row = 0; row < in_rows; row++) | |
198 pgm_readpgmrow(in, input_image[row], in_cols, in_maxval, in_format); | |
199 | |
200 fclose(in); | |
201 | |
202 init_dwt(cols, rows, filter_name, filter, level, method); | |
203 /*#ifdef POLLEN_STUFF | |
204 #include "pollen_stuff.c" | |
205 #endif | |
206 #ifdef PARAM_STUFF | |
207 #include "param_stuff.c" | |
208 #endif*/ | |
209 | |
210 dwts = fdwt(input_image); | |
211 | |
212 p = dwts; | |
213 | |
214 #define STEP 3 | |
215 | |
216 // consider each resolution level | |
217 while (p->coarse->level < level) | |
218 // descend one level | |
219 p = p->coarse; | |
220 | |
221 // start to extracting watermark from beginning at each level | |
222 { | |
223 // get width and height of detail images at current level | |
224 int lwidth = p->vertical->image->width; | |
225 int lheight = p->vertical->image->height; | |
226 | |
227 if (verbose > 1) | |
228 fprintf(stderr, "%s: extracting at level %d now, size %d x %d\n", | |
229 progname, p->coarse->level, lwidth, lheight); | |
230 | |
231 // consider each coefficient at resolution level | |
232 for (row = 0; row < lwidth - STEP; row += STEP) | |
233 for (col = 0; col < lheight - STEP; col += STEP) { | |
234 double h, v, d; | |
235 double *f1 = &h, *f2 = &v, *f3 = &d; | |
236 double delta; | |
237 | |
238 // key-dependant coefficient selection | |
239 r = row + 1 + random() % (STEP-2); | |
240 c = col + 1 + random() % (STEP-2); | |
241 | |
242 // get coefficient values, one from each detail image | |
243 h = get_pixel(p->horizontal->image, c, r); | |
244 v = get_pixel(p->vertical->image, c, r); | |
245 d = get_pixel(p->diagonal->image, c, r); | |
246 | |
247 // order pointer to coefficient values such that f1 <= f2 <= f3 | |
248 #define SWAP(A, B) {double *t = A; A = B; B = t;} | |
249 if (*f1 > *f2) SWAP(f1, f2); | |
250 if (*f2 > *f3) SWAP(f2, f3); | |
251 if (*f1 > *f2) SWAP(f1, f2); | |
252 | |
253 // calculate delta, the width of the bins | |
254 delta = (*f3 - *f1) / (double) (2 * quality - 1); | |
255 | |
256 // set middle coefficient to closest appropriate bin, | |
257 // according to watermark bit | |
258 if (quality == 1) | |
259 set_signature_bit(n, (*f3 - *f2) < (*f2 - *f1)); | |
260 else { | |
261 double l = *f1; | |
262 int i = 0; | |
263 while ((l + delta) < *f2) { | |
264 l += delta; | |
265 i++; | |
266 } | |
267 if (i % 2) | |
268 set_signature_bit(n, (l + delta - *f2) > (*f2 - l)); | |
269 else | |
270 set_signature_bit(n, (l + delta - *f2) < (*f2 - l)); | |
271 } | |
272 | |
273 if (verbose > 2) | |
274 fprintf(stderr, "%s: extracted bit #%d (= %d =? %d) at (%d/%d),\n" | |
275 " f1=%lf, f2=%lf, f3=%lf\n", progname, n, | |
276 get_signature_bit(n), get_signature2_bit(n), | |
277 c, r, *f1, *f2, *f3); | |
278 n++; | |
279 } | |
280 } | |
281 | |
282 fprintf(out, "KD3WM\n"); | |
283 fprintf(out, "%d\n", n); | |
284 nbit_signature = n; | |
285 binstr = malloc(sizeof(char) * (nbit_signature + 1)); | |
286 sig_to_binstr(binstr); | |
287 fprintf(out, "%s\n", binstr); | |
288 free(binstr); | |
289 fclose(out); | |
290 | |
291 pgm_freearray(input_image, rows); | |
292 | |
293 exit(0); | |
294 } |