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