3
|
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 quality = 0;
|
|
37 int blocksize = 0;
|
|
38 int seed = 0;
|
|
39 int n = 0;
|
|
40 int method = -1;
|
|
41 int filter = 0;
|
|
42 char filter_name[MAXPATHLEN] = "";
|
|
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, "a:e:f:F:h?l:o:q:s:v:")) != EOF) {
|
|
63 switch (c) {
|
|
64 case 'a':
|
|
65 alpha = atof(optarg);
|
|
66 break;
|
|
67 case 'e':
|
|
68 method = atoi(optarg);
|
|
69 if (method < 0) {
|
|
70 fprintf(stderr, "%s: wavelet filtering method %d out of range\n", progname, method);
|
|
71 exit(1);
|
|
72 }
|
|
73 break;
|
|
74 case 'f':
|
|
75 filter = atoi(optarg);
|
|
76 if (filter <= 0) {
|
|
77 fprintf(stderr, "%s: filter number %d out of range\n", progname, filter);
|
|
78 exit(1);
|
|
79 }
|
|
80 break;
|
|
81 case 'F':
|
|
82 strcpy(filter_name, optarg);
|
|
83 break;
|
|
84 case 'h':
|
|
85 case '?':
|
|
86 usage();
|
|
87 break;
|
|
88 case 'l':
|
|
89 level = atoi(optarg);
|
|
90 if (level < 1) {
|
|
91 fprintf(stderr, "%s: embedding level out of range\n", progname);
|
|
92 exit(1);
|
|
93 }
|
|
94 break;
|
|
95 case 'o':
|
|
96 if ((out = fopen(optarg, "w")) == NULL) {
|
|
97 fprintf(stderr, "%s: unable to open output file %s\n", progname, optarg);
|
|
98 exit(1);
|
|
99 }
|
|
100 strcpy(output_name, optarg);
|
|
101 break;
|
|
102 case 'q':
|
|
103 quality = atoi(optarg);
|
|
104 if (quality < 1) {
|
|
105 fprintf(stderr, "%s: quality level %d out of range\n", progname, quality);
|
|
106 exit(1);
|
|
107 }
|
|
108 break;
|
|
109 case 's':
|
|
110 if ((sig = fopen(optarg, "r")) == NULL) {
|
|
111 fprintf(stderr, "%s: unable to open signature file %s\n", progname, optarg);
|
|
112 exit(1);
|
|
113 }
|
|
114 strcpy(signature_name, optarg);
|
|
115 break;
|
|
116 case 'v':
|
|
117 verbose = atoi(optarg);
|
|
118 if (verbose < 0) {
|
|
119 fprintf(stderr, "%s: verbosity level %d out of range\n", progname, verbose);
|
|
120 exit(1);
|
|
121 }
|
|
122 break;
|
|
123 }
|
|
124 }
|
|
125
|
|
126 argc -= optind;
|
|
127 argv += optind;
|
|
128
|
|
129 if (argc > 1) {
|
|
130 usage();
|
|
131 exit(1);
|
|
132 }
|
|
133
|
8
|
134 if (argc == 1 && *argv[0] != '-') {
|
3
|
135 if ((in = fopen(argv[0], "rb")) == NULL) {
|
|
136 fprintf(stderr, "%s: unable to open input file %s\n", progname, argv[0]);
|
|
137 exit(1);
|
|
138 }
|
|
139 else
|
|
140 strcpy(input_name, argv[0]);
|
8
|
141 }
|
|
142
|
3
|
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 bx, by;
|
|
231 int nblock;
|
|
232
|
|
233 nblock = 0;
|
|
234 for (bx = 0; bx < lwidth; bx += blocksize) {
|
|
235 for (by = 0; by < lheight; by += blocksize) {
|
|
236 int bw = MIN(bx + blocksize, lwidth);
|
|
237 int bh = MIN(by + blocksize, lheight);
|
|
238 int STEP;
|
|
239
|
|
240 // start to extracting watermark from beginning at each level
|
|
241 // get width and height of detail images at current level
|
|
242
|
|
243 if (verbose > 1)
|
|
244 fprintf(stderr, "%s: extracting at level %d now, size %d x %d\n",
|
|
245 progname, p->coarse->level, lwidth, lheight);
|
|
246
|
|
247 STEP = ROUND(alpha * (-15.555) + 10.777);
|
|
248 n = 0;
|
|
249 // consider each coefficient at resolution level
|
|
250 for (row = by; row < bh - STEP; row += STEP)
|
|
251 for (col = bx; col < bw - STEP; col += STEP) {
|
|
252 double h, v, d;
|
|
253 double *f1 = &h, *f2 = &v, *f3 = &d;
|
|
254 double delta;
|
|
255
|
|
256 // key-dependant coefficient selection
|
|
257 r = row + 1 + random() % (STEP-2);
|
|
258 c = col + 1 + random() % (STEP-2);
|
|
259
|
|
260 // get coefficient values, one from each detail image
|
|
261 h = get_pixel(p->horizontal->image, c, r);
|
|
262 v = get_pixel(p->vertical->image, c, r);
|
|
263 d = get_pixel(p->diagonal->image, c, r);
|
|
264
|
|
265 // order pointer to coefficient values such that f1 <= f2 <= f3
|
|
266 #define SWAP(A, B) {double *t = A; A = B; B = t;}
|
|
267 if (*f1 > *f2) SWAP(f1, f2);
|
|
268 if (*f2 > *f3) SWAP(f2, f3);
|
|
269 if (*f1 > *f2) SWAP(f1, f2);
|
|
270
|
|
271 // calculate delta, the width of the bins
|
|
272 delta = (*f3 - *f1) / (double) (2 * quality - 1);
|
|
273
|
|
274 // set middle coefficient to closest appropriate bin,
|
|
275 // according to watermark bit
|
|
276 if (quality == 1)
|
|
277 set_signature_bit(n, (*f3 - *f2) < (*f2 - *f1));
|
|
278 else {
|
|
279 double l = *f1;
|
|
280 int i = 0;
|
|
281 while ((l + delta) < *f2) {
|
|
282 l += delta;
|
|
283 i++;
|
|
284 }
|
|
285 if (i % 2)
|
|
286 set_signature_bit(n, (l + delta - *f2) > (*f2 - l));
|
|
287 else
|
|
288 set_signature_bit(n, (l + delta - *f2) < (*f2 - l));
|
|
289 }
|
|
290
|
|
291 if (verbose > 2)
|
|
292 fprintf(stderr, "%s: extracted bit #%d (= %d =? %d) at (%d/%d),\n"
|
|
293 " f1=%lf, f2=%lf, f3=%lf\n", progname, n,
|
|
294 get_signature_bit(n), get_signature2_bit(n),
|
|
295 c, r, *f1, *f2, *f3);
|
|
296 n++;
|
|
297 }
|
|
298
|
|
299 nbit_signature = n;
|
|
300 binstr = malloc(sizeof(char) * (nbit_signature + 1));
|
|
301 sig_to_binstr(binstr);
|
|
302 fprintf(out, "%d\n", nbit_signature);
|
|
303 fprintf(out, "%s\n", binstr);
|
|
304 free(binstr);
|
|
305
|
|
306 }
|
|
307 }
|
|
308 // descend one level
|
|
309 p = p->coarse;
|
|
310 }
|
|
311
|
|
312 fclose(out);
|
|
313
|
|
314 pgm_freearray(input_image, rows);
|
|
315
|
|
316 exit(0);
|
|
317 }
|