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