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

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