Mercurial > hg > wm
annotate Meerwald/wm_xie2_e.c @ 22:d8551fb39a5e default tip
Added tag v0.6 for changeset 1c4ccd635a68
author | Peter Meerwald-Stadler <pmeerw@pmeerw.net> |
---|---|
date | Sat, 28 Jan 2023 23:57:51 +0100 |
parents | 4987db85cfae |
children |
rev | line source |
---|---|
3 | 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; | |
8 | 62 int rows, cols, format; |
3 | 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 | |
8 | 140 if (argc == 1 && *argv[0] != '-') { |
3 | 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]); | |
8 | 147 } |
3 | 148 |
149 if (sig) { | |
150 char line[32]; | |
151 fgets(line, sizeof(line), sig); | |
152 if (strspn(line, "XE2SG") >= 5) { | |
153 fscanf(sig, "%d\n", &nbit_signature); | |
154 if (alpha == 0.0) | |
155 fscanf(sig, "%lf\n", &alpha); | |
156 else | |
16
4987db85cfae
fix another scanf() warning
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
15
diff
changeset
|
157 fscanf(sig, "%*f\n"); |
3 | 158 if (method < 0) |
159 fscanf(sig, "%d\n", &method); | |
160 else | |
161 fscanf(sig, "%*d\n"); | |
162 if (filter == 0) | |
163 fscanf(sig, "%d\n", &filter); | |
164 else | |
165 fscanf(sig, "%*d\n"); | |
166 if (!strcmp(filter_name, "")) | |
15 | 167 fscanf(sig, "%[^\n\r]\n", filter_name); |
3 | 168 else |
169 fscanf(sig, "%*[^\n\r]\n"); | |
170 if (quant == 0) | |
171 fscanf(sig, "%d\n", &quant); | |
172 else | |
173 fscanf(sig, "%*d\n"); | |
174 if (level == 0) | |
175 fscanf(sig, "%d\n", &level); | |
176 else | |
177 fscanf(sig, "%*d\n"); | |
178 fscanf(sig, "%d\n", &seed); | |
179 srandom(seed); | |
180 n_signature = NBITSTOBYTES(nbit_signature); | |
181 fread(signature, sizeof(char), n_signature, sig); | |
182 fscanf(sig, "\n"); | |
183 } | |
184 else { | |
185 fprintf(stderr, "%s: invalid signature file %s\n", progname, signature_name); | |
186 exit(1); | |
187 } | |
188 fclose(sig); | |
189 } | |
190 else { | |
191 fprintf(stderr, "%s: signature file not specified, use -s file option\n", progname); | |
192 exit(1); | |
193 } | |
194 | |
195 pgm_readpgminit(in, &cols, &rows, &maxval, &format); | |
196 | |
197 if (verbose > 0) | |
198 fprintf(stderr, "%s: embedding %d bits with strength %d in\n" | |
199 " %d x %d host image, decomposition level %d\n", | |
200 progname, nbit_signature, quant, cols, rows, level); | |
201 | |
202 image = pgm_allocarray(cols, rows); | |
203 | |
204 for (row = 0; row < rows; row++) | |
205 pgm_readpgmrow(in, image[row], cols, maxval, format); | |
206 | |
207 fclose(in); | |
208 | |
209 // decomposition of image | |
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 dwts = fdwt(image); | |
219 | |
220 p = dwts; | |
221 | |
222 // consider each resolution level | |
223 while (p->level < level) | |
224 // descend one level | |
225 p = p->coarse; | |
226 | |
227 // repeat binary watermark by sliding a 3-pixel window of approximation image | |
228 n = 0; | |
229 for (row = 0; row < p->image->height; row++) { | |
230 col = 0; | |
231 while (col < p->image->width - 3) { | |
232 double MIN_DIFF = (20.0 * alpha); | |
233 double b1, b2, b3; | |
234 double *f1 = &b1, *f2 = &b2, *f3 = &b3; | |
235 double f2new; | |
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 if ((*f3 - *f1) < MIN_DIFF) { | |
249 double adj = (MIN_DIFF - (*f3 - *f1)) / 2.0; | |
250 *f1 -= adj; | |
251 *f3 += adj; | |
252 } | |
253 | |
254 // apply watermarking transformation (modify median pixel) | |
255 f2new = wm_transform(quant, *f1, *f2, *f3, get_signature_bit(n % nbit_signature)); | |
256 | |
257 if (verbose > 1) | |
258 fprintf(stderr, "%s: embedding #%d (= %d) at (%d/%d); %f < %f -> %f < %f\n", | |
259 progname, n, get_signature_bit(n % nbit_signature), col, row, *f1, *f2, f2new, *f3); | |
260 | |
261 *f2 = f2new; | |
262 | |
263 // write modified pixel | |
264 set_pixel(p->image, col + 0, row, b1); | |
265 set_pixel(p->image, col + 1, row, b2); | |
266 set_pixel(p->image, col + 2, row, b3); | |
267 n++; | |
268 col += 3; | |
269 col+= (2.0 / alpha); | |
270 } | |
271 } | |
272 | |
273 | |
274 // skip over some pixels | |
275 | |
276 idwt(dwts, image); | |
277 | |
278 pgm_writepgminit(out, cols, rows, maxval, 0); | |
279 | |
280 for (row = 0; row < rows; row++) | |
281 pgm_writepgmrow(out, image[row], cols, maxval, 0); | |
282 | |
283 fclose(out); | |
284 | |
285 pgm_freearray(image, rows); | |
286 | |
287 exit(0); | |
288 } |