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