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] [-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;
|
15
|
53 int rows, cols, format;
|
3
|
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
|
8
|
132 if (argc == 1 && *argv[0] != '-') {
|
3
|
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]);
|
8
|
139 }
|
|
140
|
3
|
141 if (sig) {
|
|
142 char line[1024];
|
|
143 fgets(line, sizeof(line), sig);
|
|
144 if (strspn(line, "KD2SG") >= 5) {
|
|
145 fscanf(sig, "%d\n", &nbit_signature);
|
|
146 if (quality == 0)
|
|
147 fscanf(sig, "%d\n", &quality);
|
|
148 else
|
|
149 fscanf(sig, "%*d\n");
|
|
150 if (blocksize == 0)
|
|
151 fscanf(sig, "%d\n", &blocksize);
|
|
152 else
|
|
153 fscanf(sig, "%*d\n");
|
|
154 if (method < 0)
|
|
155 fscanf(sig, "%d\n", &method);
|
|
156 else
|
|
157 fscanf(sig, "%*d\n");
|
|
158 if (filter == 0)
|
|
159 fscanf(sig, "%d\n", &filter);
|
|
160 else
|
|
161 fscanf(sig, "%*d\n");
|
|
162 if (!strcmp(filter_name, ""))
|
15
|
163 fscanf(sig, "%[^\n\r]\n", filter_name);
|
3
|
164 else
|
|
165 fscanf(sig, "%*[^\n\r]\n");
|
|
166 if (level == 0)
|
|
167 fscanf(sig, "%d\n", &level);
|
|
168 else
|
|
169 fscanf(sig, "%*d\n");
|
|
170 fscanf(sig, "%d\n", &seed);
|
|
171 srandom(seed);
|
|
172 n_signature = NBITSTOBYTES(nbit_signature);
|
|
173 binstr = malloc((nbit_signature + 1) * sizeof(char));
|
|
174 fscanf(sig, "%[01]\n", binstr);
|
|
175 binstr_to_sig(binstr);
|
|
176 free(binstr);
|
|
177 }
|
|
178 else {
|
|
179 fprintf(stderr, "%s: invalid signature file %s\n", progname, signature_name);
|
|
180 exit(1);
|
|
181 }
|
|
182 fclose(sig);
|
|
183 }
|
|
184 else {
|
|
185 fprintf(stderr, "%s: signature file not specified, use -s file option\n", progname);
|
|
186 exit(1);
|
|
187 }
|
|
188
|
|
189 pgm_readpgminit(in, &cols, &rows, &maxval, &format);
|
|
190
|
|
191 if (verbose > 0)
|
|
192 fprintf(stderr, "%s: embedding %d bits with quality %d in\n"
|
|
193 " %d x %d host image, up to decomposition level %d\n",
|
|
194 progname, nbit_signature, quality, cols, rows, level);
|
|
195
|
|
196 image = pgm_allocarray(cols, rows);
|
|
197
|
|
198 for (row = 0; row < rows; row++)
|
|
199 pgm_readpgmrow(in, image[row], cols, maxval, format);
|
|
200
|
|
201 fclose(in);
|
|
202
|
|
203 // decomposition of image
|
|
204 init_dwt(cols, rows, filter_name, filter, level, method);
|
|
205 #ifdef POLLEN_STUFF
|
|
206 #include "pollen_stuff.c"
|
|
207 #endif
|
|
208 #ifdef PARAM_STUFF
|
|
209 #include "param_stuff.c"
|
|
210 #endif
|
|
211
|
|
212 dwts = fdwt(image);
|
|
213
|
|
214 p = dwts;
|
|
215
|
|
216 // consider each resolution level
|
|
217 while (p->coarse->level < level) {
|
|
218 int lwidth = p->vertical->image->width;
|
|
219 int lheight = p->vertical->image->height;
|
|
220 int l = p->vertical->level;
|
|
221 int bx, by;
|
|
222 int nblock;
|
|
223 int bits_per_level;
|
|
224
|
|
225 nblock = 0;
|
|
226 bits_per_level = 0;
|
|
227 for (bx = 0; bx < lwidth; bx += blocksize) {
|
|
228 for (by = 0; by < lheight; by += blocksize) {
|
|
229 int bw = MIN(bx + blocksize, lwidth);
|
|
230 int bh = MIN(by + blocksize, lheight);
|
|
231 int STEP;
|
|
232 float MIN_DIFF;
|
|
233
|
|
234 // start to embed signature from beginning at each level
|
|
235 // get width and height of detail images at current level
|
|
236
|
|
237
|
|
238 if (verbose > 1)
|
|
239 fprintf(stderr, "%s: embedding at level %d now, block %d, size %d x %d at %d, %d\n",
|
|
240 progname, l, nblock, bw, bh, bx, by);
|
|
241
|
|
242 MIN_DIFF = ROUND(120.0 * alpha);
|
|
243 STEP = ROUND(alpha * (-15.555) + 10.777);
|
|
244 // fprintf(stderr, "XXX %d %f\n", STEP, MIN_DIFF);
|
|
245
|
|
246 n = 0;
|
|
247 // consider each coefficient at resolution level
|
|
248 for (row = by; row < bh - STEP; row += STEP)
|
|
249 for (col = bx; col < bw - STEP; col += STEP) {
|
|
250 double h, v, d;
|
|
251 double *f1 = &h, *f2 = &v, *f3 = &d;
|
|
252 double delta;
|
|
253
|
|
254 // key-dependant coefficient selection
|
|
255 r = row + 1+random() % (STEP-2);
|
|
256 c = col + 1+random() % (STEP-2);
|
|
257
|
|
258 // get coefficient values, one from each detail image
|
|
259 h = get_pixel(p->horizontal->image, c, r);
|
|
260 v = get_pixel(p->vertical->image, c, r);
|
|
261 d = get_pixel(p->diagonal->image, c, r);
|
|
262
|
|
263 // order pointer to coefficient values such that f1 <= f2 <= f3
|
|
264 #define SWAP(A, B) {double *t = A; A = B; B = t;}
|
|
265 if (*f1 > *f2) SWAP(f1, f2);
|
|
266 if (*f2 > *f3) SWAP(f2, f3);
|
|
267 if (*f1 > *f2) SWAP(f1, f2);
|
|
268
|
|
269 if (verbose > 2)
|
|
270 fprintf(stderr, "%s: embedding bit #%d (= %d) at (%d/%d),\n",
|
|
271 progname, n, get_signature_bit(n % nbit_signature), c, r);
|
|
272 if (verbose > 5)
|
|
273 fprintf(stderr, " h=%lf, v=%lf, d=%lf\n", h, v, d);
|
|
274 if (verbose > 2)
|
|
275 fprintf(stderr, " f1=%lf, f2=%lf", *f1, *f2);
|
|
276
|
|
277 if ((*f3 - *f1) < MIN_DIFF) {
|
|
278 double adj = (MIN_DIFF - (*f3 - *f1)) / 2.0;
|
|
279 *f1 -= adj;
|
|
280 *f3 += adj;
|
|
281 }
|
|
282
|
|
283 // calculate delta, the width of the bins
|
|
284 delta = (*f3 - *f1) / (double) (2 * quality - 1);
|
|
285
|
|
286 // set middle coefficient to closest appropriate bin,
|
|
287 // according to watermark bit
|
|
288 bits_per_level++;
|
|
289 if (quality == 1)
|
|
290 *f2 = get_signature_bit(n % nbit_signature) ? *f3 : *f1;
|
|
291 else {
|
|
292 double l = get_signature_bit(n % nbit_signature) ? *f1 + delta : *f1;
|
|
293 while ((l + 2 * delta) < *f2) l += 2 * delta;
|
|
294 *f2 = (*f2 - l) < (l + 2 * delta - *f2) ? l : l + 2 * delta;
|
|
295 }
|
|
296
|
|
297 if (verbose > 2)
|
|
298 fprintf(stderr, " -> %lf, f3=%lf\n", *f2, *f3);
|
|
299
|
|
300 // write pixels, one of them modified
|
|
301 set_pixel(p->horizontal->image, c, r, h);
|
|
302 set_pixel(p->vertical->image, c, r, v);
|
|
303 set_pixel(p->diagonal->image, c, r, d);
|
|
304
|
|
305 n++;
|
|
306
|
|
307 }
|
|
308 }
|
|
309 }
|
|
310
|
|
311 if (verbose > 1)
|
|
312 fprintf(stderr, "%s: embedded %d bits at level %d\n",
|
|
313 progname, bits_per_level, l);
|
|
314
|
|
315 // descend one level
|
|
316 p = p->coarse;
|
|
317 }
|
|
318
|
|
319 idwt(dwts, image);
|
|
320
|
|
321 pgm_writepgminit(out, cols, rows, maxval, 0);
|
|
322
|
|
323 for (row = 0; row < rows; row++)
|
|
324 pgm_writepgmrow(out, image[row], cols, maxval, 0);
|
|
325
|
|
326 fclose(out);
|
|
327
|
|
328 pgm_freearray(image, rows);
|
|
329
|
|
330 exit(0);
|
|
331 }
|