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

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