Mercurial > hg > wm
annotate Meerwald/wm_xie2_d.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 // 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; | |
8 | 68 int rows, cols, format; |
3 | 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 | |
8 | 146 if (argc == 1 && *argv[0] != '-') { |
3 | 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]); | |
8 | 153 } |
3 | 154 |
155 if (sig) { | |
156 char line[32]; | |
157 fgets(line, sizeof(line), sig); | |
158 if (strspn(line, "XE2SG") >= 5) { | |
159 fscanf(sig, "%d\n", &nbit_signature); | |
160 if (alpha == 0.0) | |
161 fscanf(sig, "%lf\n", &alpha); | |
162 else | |
16
4987db85cfae
fix another scanf() warning
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
15
diff
changeset
|
163 fscanf(sig, "%*f\n"); |
3 | 164 if (method < 0) |
165 fscanf(sig, "%d\n", &method); | |
166 else | |
167 fscanf(sig, "%*d\n"); | |
168 if (filter == 0) | |
169 fscanf(sig, "%d\n", &filter); | |
170 else | |
171 fscanf(sig, "%*d\n"); | |
172 if (!strcmp(filter_name, "")) | |
15 | 173 fscanf(sig, "%[^\n\r]\n", filter_name); |
3 | 174 else |
175 fscanf(sig, "%*[^\n\r]\n"); | |
176 if (quant == 0) | |
177 fscanf(sig, "%d\n", &quant); | |
178 else | |
179 fscanf(sig, "%*d\n"); | |
180 if (level == 0) | |
181 fscanf(sig, "%d\n", &level); | |
182 else | |
183 fscanf(sig, "%*d\n"); | |
184 fscanf(sig, "%d\n", &seed); | |
185 srandom(seed); | |
186 n_signature = NBITSTOBYTES(nbit_signature); | |
187 fread(signature, sizeof(char), n_signature, sig); | |
188 fscanf(sig, "\n"); | |
189 } | |
190 else { | |
191 fprintf(stderr, "%s: invalid signature file %s\n", progname, signature_name); | |
192 exit(1); | |
193 } | |
194 fclose(sig); | |
195 } | |
196 else { | |
197 fprintf(stderr, "%s: signature file not specified, use -s file option\n", progname); | |
198 exit(1); | |
199 } | |
200 | |
201 pgm_readpgminit(in, &cols, &rows, &maxval, &format); | |
202 if (verbose > 0) | |
203 fprintf(stderr, "%s: embedding %d bits with strength %d in\n" | |
204 " %d x %d host image, decomposition level %d\n", | |
205 progname, nbit_signature, quant, cols, rows, level); | |
206 | |
207 image = pgm_allocarray(cols, rows); | |
208 for (row = 0; row < rows; row++) | |
209 pgm_readpgmrow(in, image[row], cols, maxval, format); | |
210 fclose(in); | |
211 | |
212 // decomposition of image | |
213 init_dwt(cols, rows, filter_name, filter, level, method); | |
214 #ifdef POLLEN_STUFF | |
215 #include "pollen_stuff.c" | |
216 #endif | |
217 #ifdef PARAM_STUFF | |
218 #include "param_stuff.c" | |
219 #endif | |
220 | |
221 dwts = fdwt(image); | |
222 | |
223 p = dwts; | |
224 | |
225 // consider each resolution level | |
226 while (p->level < level) | |
227 // descend one level | |
228 p = p->coarse; | |
229 | |
230 // repeat binary watermark by sliding a 3-pixel window of approximation image | |
231 n = 0; | |
232 for (row = 0; row < p->image->height; row++) { | |
233 col = 0; | |
234 while (col < p->image->width - 3) { | |
235 double b1, b2, b3; | |
236 double *f1 = &b1, *f2 = &b2, *f3 = &b3; | |
237 | |
238 // get all three approximation pixels in window | |
239 b1 = get_pixel(p->image, col + 0, row); | |
240 b2 = get_pixel(p->image, col + 1, row); | |
241 b3 = get_pixel(p->image, col + 2, row); | |
242 | |
243 // bring selected pixels in ascending order | |
244 #define SWAP(A, B) {double *t = A; A = B; B = t;} | |
245 if (*f1 > *f2) SWAP(f1, f2); | |
246 if (*f2 > *f3) SWAP(f2, f3); | |
247 if (*f1 > *f2) SWAP(f1, f2); | |
248 | |
249 set_signature_bit(n, wm_transform(quant, *f1, *f2, *f3)); | |
250 | |
251 if (verbose > 1) | |
252 fprintf(stderr, "%s: extracting #%d (= %d) at (%d/%d); %f < %f < %f\n", | |
253 progname, n, get_signature_bit(n % nbit_signature), col, row, *f1, *f2, *f3); | |
254 | |
255 n++; | |
256 col += 3; | |
257 col += (2.0 / alpha); | |
258 } | |
259 } | |
260 | |
261 fprintf(out, "XE2WM\n"); | |
262 fprintf(out, "%d\n", n); | |
263 fwrite(signature, sizeof(char), NBITSTOBYTES(n), out); | |
264 fprintf(out, "\n"); | |
265 fclose(out); | |
266 | |
267 pgm_freearray(image, rows); | |
268 | |
269 exit(0); | |
270 } |