Mercurial > hg > wm
annotate Meerwald/wm_kund2_d.c @ 23:71dd4b96221b
fix some compiler warning
author | Peter Meerwald-Stadler <pmeerw@pmeerw.net> |
---|---|
date | Fri, 20 Dec 2024 12:53:41 +0100 |
parents | bd669312f068 |
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 void usage(void) { | |
9 fprintf(stderr, "usage: %s [-e n] [-f n] [-F n] [-h] [-l n] [-o file] [-q n] [-s file] [-v n] file\n\n", progname); | |
10 fprintf(stderr, "\t-a n\t\toverall embedding strength\n"); | |
11 fprintf(stderr, "\t-e n\t\twavelet filtering method\n"); | |
12 fprintf(stderr, "\t-f n\t\tfilter number\n"); | |
13 fprintf(stderr, "\t-F file\t\tfilter definition file\n"); | |
14 fprintf(stderr, "\t-h\t\tprint usage\n"); | |
15 fprintf(stderr, "\t-l n\t\tembedding level\n"); | |
16 fprintf(stderr, "\t-o file\t\textracted signature file\n"); | |
17 fprintf(stderr, "\t-q n\t\tsignature strength\n"); | |
18 fprintf(stderr, "\t-s file\t\toriginal signature file\n"); | |
19 fprintf(stderr, "\t-v n\t\tverbosity level\n"); | |
20 exit(0); | |
21 } | |
22 | |
23 int main(int argc, char *argv[]) { | |
24 | |
25 FILE *in = stdin; | |
26 FILE *out = stdout; | |
27 FILE *sig = NULL; | |
28 | |
29 gray **input_image; | |
30 | |
31 char signature_name[MAXPATHLEN]; | |
32 char output_name[MAXPATHLEN] = "(stdout)"; | |
33 char input_name[MAXPATHLEN] = "(stdin)"; | |
34 | |
35 int r, c; | |
36 int quality = 0; | |
37 int blocksize = 0; | |
38 int seed = 0; | |
39 int n = 0; | |
40 int method = -1; | |
41 int filter = 0; | |
42 char filter_name[MAXPATHLEN] = ""; | |
43 char *binstr; | |
44 | |
45 int level = 0; | |
46 double alpha = 0.0; | |
47 | |
48 int in_rows, in_cols, in_format; | |
49 gray in_maxval; | |
50 int rows, cols; | |
51 int row, col; | |
52 | |
53 Image_tree dwts, p; | |
54 | |
55 int verbose = 0; | |
56 | |
57 progname = argv[0]; | |
58 | |
59 pgm_init(&argc, argv); | |
60 wm_init(); | |
61 | |
62 while ((c = getopt(argc, argv, "a:e:f:F:h?l:o:q:s:v:")) != EOF) { | |
63 switch (c) { | |
64 case 'a': | |
65 alpha = atof(optarg); | |
66 break; | |
67 case 'e': | |
68 method = atoi(optarg); | |
69 if (method < 0) { | |
70 fprintf(stderr, "%s: wavelet filtering method %d out of range\n", progname, method); | |
71 exit(1); | |
72 } | |
73 break; | |
74 case 'f': | |
75 filter = atoi(optarg); | |
76 if (filter <= 0) { | |
77 fprintf(stderr, "%s: filter number %d out of range\n", progname, filter); | |
78 exit(1); | |
79 } | |
80 break; | |
81 case 'F': | |
82 strcpy(filter_name, optarg); | |
83 break; | |
84 case 'h': | |
85 case '?': | |
86 usage(); | |
87 break; | |
88 case 'l': | |
89 level = atoi(optarg); | |
90 if (level < 1) { | |
91 fprintf(stderr, "%s: embedding level out of range\n", progname); | |
92 exit(1); | |
93 } | |
94 break; | |
95 case 'o': | |
96 if ((out = fopen(optarg, "w")) == NULL) { | |
97 fprintf(stderr, "%s: unable to open output file %s\n", progname, optarg); | |
98 exit(1); | |
99 } | |
100 strcpy(output_name, optarg); | |
101 break; | |
102 case 'q': | |
103 quality = atoi(optarg); | |
104 if (quality < 1) { | |
105 fprintf(stderr, "%s: quality level %d out of range\n", progname, quality); | |
106 exit(1); | |
107 } | |
108 break; | |
109 case 's': | |
110 if ((sig = fopen(optarg, "r")) == NULL) { | |
111 fprintf(stderr, "%s: unable to open signature file %s\n", progname, optarg); | |
112 exit(1); | |
113 } | |
114 strcpy(signature_name, optarg); | |
115 break; | |
116 case 'v': | |
117 verbose = atoi(optarg); | |
118 if (verbose < 0) { | |
119 fprintf(stderr, "%s: verbosity level %d out of range\n", progname, verbose); | |
120 exit(1); | |
121 } | |
122 break; | |
123 } | |
124 } | |
125 | |
126 argc -= optind; | |
127 argv += optind; | |
128 | |
129 if (argc > 1) { | |
130 usage(); | |
131 exit(1); | |
132 } | |
133 | |
8 | 134 if (argc == 1 && *argv[0] != '-') { |
3 | 135 if ((in = fopen(argv[0], "rb")) == NULL) { |
136 fprintf(stderr, "%s: unable to open input file %s\n", progname, argv[0]); | |
137 exit(1); | |
138 } | |
139 else | |
140 strcpy(input_name, argv[0]); | |
8 | 141 } |
142 | |
3 | 143 if (sig) { |
144 char line[1024]; | |
145 fgets(line, sizeof(line), sig); | |
146 if (strspn(line, "KD2SG") >= 5) { | |
147 fscanf(sig, "%d\n", &nbit_signature); | |
148 if (quality == 0) | |
149 fscanf(sig, "%d\n", &quality); | |
150 else | |
151 fscanf(sig, "%*d\n"); | |
152 if (blocksize == 0) | |
153 fscanf(sig, "%d\n", &blocksize); | |
154 else | |
155 fscanf(sig, "%*d\n"); | |
156 if (method < 0) | |
157 fscanf(sig, "%d\n", &method); | |
158 else | |
159 fscanf(sig, "%*d\n"); | |
160 if (filter == 0) | |
161 fscanf(sig, "%d\n", &filter); | |
162 else | |
163 fscanf(sig, "%*d\n"); | |
164 if (!strcmp(filter_name, "")) | |
15 | 165 fscanf(sig, "%[^\n\r]\n", filter_name); |
3 | 166 else |
167 fscanf(sig, "%*[^\n\r]\n"); | |
168 if (level == 0) | |
169 fscanf(sig, "%d\n", &level); | |
170 else | |
171 fscanf(sig, "%*d\n"); | |
172 fscanf(sig, "%d\n", &seed); | |
173 srandom(seed); | |
174 nbit_signature2 = nbit_signature; | |
175 n_signature = n_signature2 = NBITSTOBYTES(nbit_signature2); | |
176 binstr = malloc((nbit_signature2 + 1) * sizeof(char)); | |
177 fscanf(sig, "%[01]\n", binstr); | |
178 binstr_to_sig2(binstr); | |
179 free(binstr); | |
180 init_signature_bits(); | |
181 } | |
182 else { | |
183 fprintf(stderr, "%s: invalid signature file %s\n", progname, signature_name); | |
184 exit(1); | |
185 } | |
186 fclose(sig); | |
187 } | |
188 else { | |
189 fprintf(stderr, "%s: signature file not specified, use -s file option\n", progname); | |
190 exit(1); | |
191 } | |
192 | |
193 pgm_readpgminit(in, &in_cols, &in_rows, &in_maxval, &in_format); | |
194 | |
195 cols = in_cols; | |
196 rows = in_rows; | |
197 | |
198 if (verbose > 0) | |
199 fprintf(stderr, "%s: extracting %d bits with quality %d from\n" | |
200 " %d x %d host image, decomposition level %d\n", | |
201 progname, nbit_signature, quality, cols, rows, level); | |
202 | |
203 input_image = pgm_allocarray(in_cols, in_rows); | |
204 | |
205 for (row = 0; row < in_rows; row++) | |
206 pgm_readpgmrow(in, input_image[row], in_cols, in_maxval, in_format); | |
207 | |
208 fclose(in); | |
209 | |
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 fprintf(out, "KD2WM\n"); | |
219 fprintf(out, "%d\n", nbit_signature); | |
220 | |
221 dwts = fdwt(input_image); | |
222 | |
223 | |
224 p = dwts; | |
225 | |
226 // consider each resolution level | |
227 while (p->coarse->level < level) { | |
228 int lwidth = p->vertical->image->width; | |
229 int lheight = p->vertical->image->height; | |
230 int bx, by; | |
231 | |
232 for (bx = 0; bx < lwidth; bx += blocksize) { | |
233 for (by = 0; by < lheight; by += blocksize) { | |
234 int bw = MIN(bx + blocksize, lwidth); | |
235 int bh = MIN(by + blocksize, lheight); | |
236 int STEP; | |
237 | |
238 // start to extracting watermark from beginning at each level | |
239 // get width and height of detail images at current level | |
20
bd669312f068
suppress warnings, fix link errors
Peter Meerwald-Stadler <pmeerw@pmeerw.net>
parents:
15
diff
changeset
|
240 |
3 | 241 if (verbose > 1) |
242 fprintf(stderr, "%s: extracting at level %d now, size %d x %d\n", | |
243 progname, p->coarse->level, lwidth, lheight); | |
244 | |
245 STEP = ROUND(alpha * (-15.555) + 10.777); | |
246 n = 0; | |
247 // consider each coefficient at resolution level | |
248 for (row = by; row < bh - STEP; row += STEP) | |
249 for (col = bx; col < bw - STEP; col += STEP) { | |
250 double h, v, d; | |
251 double *f1 = &h, *f2 = &v, *f3 = &d; | |
252 double delta; | |
253 | |
254 // key-dependant coefficient selection | |
255 r = row + 1 + random() % (STEP-2); | |
256 c = col + 1 + random() % (STEP-2); | |
257 | |
258 // get coefficient values, one from each detail image | |
259 h = get_pixel(p->horizontal->image, c, r); | |
260 v = get_pixel(p->vertical->image, c, r); | |
261 d = get_pixel(p->diagonal->image, c, r); | |
262 | |
263 // order pointer to coefficient values such that f1 <= f2 <= f3 | |
264 #define SWAP(A, B) {double *t = A; A = B; B = t;} | |
265 if (*f1 > *f2) SWAP(f1, f2); | |
266 if (*f2 > *f3) SWAP(f2, f3); | |
267 if (*f1 > *f2) SWAP(f1, f2); | |
268 | |
269 // calculate delta, the width of the bins | |
270 delta = (*f3 - *f1) / (double) (2 * quality - 1); | |
271 | |
272 // set middle coefficient to closest appropriate bin, | |
273 // according to watermark bit | |
274 if (quality == 1) | |
275 set_signature_bit(n, (*f3 - *f2) < (*f2 - *f1)); | |
276 else { | |
277 double l = *f1; | |
278 int i = 0; | |
279 while ((l + delta) < *f2) { | |
280 l += delta; | |
281 i++; | |
282 } | |
283 if (i % 2) | |
284 set_signature_bit(n, (l + delta - *f2) > (*f2 - l)); | |
285 else | |
286 set_signature_bit(n, (l + delta - *f2) < (*f2 - l)); | |
287 } | |
288 | |
289 if (verbose > 2) | |
290 fprintf(stderr, "%s: extracted bit #%d (= %d =? %d) at (%d/%d),\n" | |
291 " f1=%lf, f2=%lf, f3=%lf\n", progname, n, | |
292 get_signature_bit(n), get_signature2_bit(n), | |
293 c, r, *f1, *f2, *f3); | |
294 n++; | |
295 } | |
296 | |
297 nbit_signature = n; | |
298 binstr = malloc(sizeof(char) * (nbit_signature + 1)); | |
299 sig_to_binstr(binstr); | |
300 fprintf(out, "%d\n", nbit_signature); | |
301 fprintf(out, "%s\n", binstr); | |
302 free(binstr); | |
303 | |
304 } | |
305 } | |
306 // descend one level | |
307 p = p->coarse; | |
308 } | |
309 | |
310 fclose(out); | |
311 | |
312 pgm_freearray(input_image, rows); | |
313 | |
314 exit(0); | |
315 } |