comparison Meerwald/wm_xie2_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 // inverse watermarking transformation, extract embedded bit, check quantization boundaries
9 double wm_transform(int quant, double f1, double f2, double f3) {
10 double s = (fabs(f3) - fabs(f1)) / (double) quant;
11 double l = f1;
12 int x;
13
14 x = 0;
15 while (l < f2) {
16 l += s;
17 x++;
18 }
19
20 if (fabs(l - s - f2) < fabs(l-f2))
21 return (x+1) % 2;
22 else
23 return (x) % 2;
24 }
25
26 void usage(void) {
27 fprintf(stderr, "usage: %s [-a n] [-e n] [-f n] [-F n] [-h] [-l n] [-o file] [-v n] -s file file\n\n", progname);
28 fprintf(stderr, "\t-a n\t\tembedding strength (default 0.05)\n");
29 fprintf(stderr, "\t-e n\t\twavelet filtering method\n");
30 fprintf(stderr, "\t-f n\t\tfilter number\n");
31 fprintf(stderr, "\t-F file\t\tfilter definition file\n");
32 fprintf(stderr, "\t-l n\t\tembedding level\n");
33 fprintf(stderr, "\t-h\t\tprint usage\n");
34 fprintf(stderr, "\t-o file\t\textracted signature file\n");
35 fprintf(stderr, "\t-q n\t\tquantization level\n");
36 fprintf(stderr, "\t-s file\t\tsignature to embed in input image\n");
37 fprintf(stderr, "\t-v n\t\tverbosity level\n");
38 exit(0);
39 }
40
41 int main(int argc, char *argv[]) {
42
43 FILE *in = stdin;
44 FILE *out = stdout;
45 FILE *sig = NULL;
46
47 char output_name[MAXPATHLEN] = "(stdout)";
48 char input_name[MAXPATHLEN] = "(stdin)";
49 char signature_name[MAXPATHLEN];
50
51 int c, n;
52 int row, col;
53
54 double alpha = 0.0;
55 int quant = 0;
56 int filter = 0;
57 int method = -1;
58 int level = 0;
59 char filter_name[MAXPATHLEN] = "";
60
61 int seed;
62 int verbose = 0;
63
64 gray **image;
65 Image_tree dwts, p;
66
67 gray maxval;
68 int rows, cols, colors, format;
69
70 progname = argv[0];
71
72 pgm_init(&argc, argv); wm_init2();
73
74 while ((c = getopt(argc, argv, "a:e:f:F:h?l:o:s:v:q:")) != EOF) {
75 switch (c) {
76 case 'e':
77 method = atoi(optarg);
78 if (method < 0) {
79 fprintf(stderr, "%s: wavelet filtering method %d out of range\n", progname, method);
80 exit(1);
81 }
82 break;
83 case 'f':
84 filter = atoi(optarg);
85 if (filter <= 0) {
86 fprintf(stderr, "%s: filter number %d out of range\n", progname, filter);
87 exit(1);
88 }
89 break;
90 case 'F':
91 strcpy(filter_name, optarg);
92 break;
93 case 'h':
94 case '?':
95 usage();
96 break;
97 case 'l':
98 level = atoi(optarg);
99 if (level < 1) {
100 fprintf(stderr, "%s: embedding level out of range\n", progname);
101 exit(1);
102 }
103 break;
104 case 'o':
105 if ((out = fopen(optarg, "wb")) == NULL) {
106 fprintf(stderr, "%s: unable to open output file %s\n", progname, optarg);
107 exit(1);
108 }
109 strcpy(output_name, optarg);
110 break;
111 case 'q':
112 quant = atoi(optarg);
113 break;
114 case 'a':
115 alpha = atof(optarg);
116 if (alpha <= 0.0) {
117 fprintf(stderr, "%s: embedding strength factor %f out of range\n", progname, alpha);
118 exit(1);
119 }
120 break;
121 case 's':
122 if ((sig = fopen(optarg, "r")) == NULL) {
123 fprintf(stderr, "%s: unable to open signature file %s\n", progname, optarg);
124 exit(1);
125 }
126 strcpy(signature_name, optarg);
127 break;
128 case 'v':
129 verbose = atoi(optarg);
130 if (verbose < 0) {
131 fprintf(stderr, "%s: verbosity level %d out of range\n", progname, verbose);
132 exit(1);
133 }
134 break;
135 }
136 }
137
138 argc -= optind;
139 argv += optind;
140
141 if (argc > 1) {
142 usage();
143 exit(1);
144 }
145
146 if (argc == 1 && *argv[0] != '-')
147 if ((in = fopen(argv[0], "rb")) == NULL) {
148 fprintf(stderr, "%s: unable to open input file %s\n", progname, argv[0]);
149 exit(1);
150 }
151 else
152 strcpy(input_name, argv[0]);
153
154 if (sig) {
155 char line[32];
156 fgets(line, sizeof(line), sig);
157 if (strspn(line, "XE2SG") >= 5) {
158 fscanf(sig, "%d\n", &nbit_signature);
159 if (alpha == 0.0)
160 fscanf(sig, "%lf\n", &alpha);
161 else
162 fscanf(sig, "%*lf\n");
163 if (method < 0)
164 fscanf(sig, "%d\n", &method);
165 else
166 fscanf(sig, "%*d\n");
167 if (filter == 0)
168 fscanf(sig, "%d\n", &filter);
169 else
170 fscanf(sig, "%*d\n");
171 if (!strcmp(filter_name, ""))
172 fscanf(sig, "%[^\n\r]\n", &filter_name);
173 else
174 fscanf(sig, "%*[^\n\r]\n");
175 if (quant == 0)
176 fscanf(sig, "%d\n", &quant);
177 else
178 fscanf(sig, "%*d\n");
179 if (level == 0)
180 fscanf(sig, "%d\n", &level);
181 else
182 fscanf(sig, "%*d\n");
183 fscanf(sig, "%d\n", &seed);
184 srandom(seed);
185 n_signature = NBITSTOBYTES(nbit_signature);
186 fread(signature, sizeof(char), n_signature, sig);
187 fscanf(sig, "\n");
188 }
189 else {
190 fprintf(stderr, "%s: invalid signature file %s\n", progname, signature_name);
191 exit(1);
192 }
193 fclose(sig);
194 }
195 else {
196 fprintf(stderr, "%s: signature file not specified, use -s file option\n", progname);
197 exit(1);
198 }
199
200 pgm_readpgminit(in, &cols, &rows, &maxval, &format);
201 if (verbose > 0)
202 fprintf(stderr, "%s: embedding %d bits with strength %d in\n"
203 " %d x %d host image, decomposition level %d\n",
204 progname, nbit_signature, quant, cols, rows, level);
205
206 image = pgm_allocarray(cols, rows);
207 for (row = 0; row < rows; row++)
208 pgm_readpgmrow(in, image[row], cols, maxval, format);
209 fclose(in);
210
211 // decomposition of image
212 init_dwt(cols, rows, filter_name, filter, level, method);
213 #ifdef POLLEN_STUFF
214 #include "pollen_stuff.c"
215 #endif
216 #ifdef PARAM_STUFF
217 #include "param_stuff.c"
218 #endif
219
220 dwts = fdwt(image);
221
222 p = dwts;
223
224 // consider each resolution level
225 while (p->level < level)
226 // descend one level
227 p = p->coarse;
228
229 // repeat binary watermark by sliding a 3-pixel window of approximation image
230 n = 0;
231 for (row = 0; row < p->image->height; row++) {
232 col = 0;
233 while (col < p->image->width - 3) {
234 double b1, b2, b3;
235 double *f1 = &b1, *f2 = &b2, *f3 = &b3;
236
237 // get all three approximation pixels in window
238 b1 = get_pixel(p->image, col + 0, row);
239 b2 = get_pixel(p->image, col + 1, row);
240 b3 = get_pixel(p->image, col + 2, row);
241
242 // bring selected pixels in ascending order
243 #define SWAP(A, B) {double *t = A; A = B; B = t;}
244 if (*f1 > *f2) SWAP(f1, f2);
245 if (*f2 > *f3) SWAP(f2, f3);
246 if (*f1 > *f2) SWAP(f1, f2);
247
248 set_signature_bit(n, wm_transform(quant, *f1, *f2, *f3));
249
250 if (verbose > 1)
251 fprintf(stderr, "%s: extracting #%d (= %d) at (%d/%d); %f < %f < %f\n",
252 progname, n, get_signature_bit(n % nbit_signature), col, row, *f1, *f2, *f3);
253
254 n++;
255 col += 3;
256 col += (2.0 / alpha);
257 }
258 }
259
260 fprintf(out, "XE2WM\n");
261 fprintf(out, "%d\n", n);
262 fwrite(signature, sizeof(char), NBITSTOBYTES(n), out);
263 fprintf(out, "\n");
264 fclose(out);
265
266 pgm_freearray(image, rows);
267
268 exit(0);
269 }

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