Mercurial > hg > wm
comparison Meerwald-dir/wm_kim_d.c @ 24:9f20bce6184e v0.7
move directories, support netpbm 11
author | Peter Meerwald-Stadler <pmeerw@pmeerw.net> |
---|---|
date | Fri, 20 Dec 2024 13:08:59 +0100 |
parents | Meerwald/wm_kim_d.c@4987db85cfae |
children |
comparison
equal
deleted
inserted
replaced
23:71dd4b96221b | 24:9f20bce6184e |
---|---|
1 #include "wm.h" | |
2 #include "dwt.h" | |
3 #include "netpbm/pgm.h" | |
4 #include "dwt_util.h" | |
5 #include "kim_common.h" | |
6 | |
7 char *progname; | |
8 | |
9 void usage(void) { | |
10 fprintf(stderr, "usage: %s [-a n] [-A n] [-e n] [-f n] [-F file] [-h] [-l n] [-o file] [-v n] -s file -i file file\n\n", progname); | |
11 fprintf(stderr, "\t-a n\t\talpha factor for detail subband\n"); | |
12 fprintf(stderr, "\t-A n\t\talpha factor for approximation image\n"); | |
13 fprintf(stderr, "\t-e n\t\twavelet filtering method\n"); | |
14 fprintf(stderr, "\t-f n\t\tfilter number\n"); | |
15 fprintf(stderr, "\t-F file\t\tfilter definition file\n"); | |
16 fprintf(stderr, "\t-h\t\tprint usage\n"); | |
17 fprintf(stderr, "\t-i file\t\toriginal image file\n"); | |
18 fprintf(stderr, "\t-l n\t\tdecomposition level\n"); | |
19 fprintf(stderr, "\t-o file\t\tfile for extracted watermark\n"); | |
20 fprintf(stderr, "\t-s file\t\toriginal signature file\n"); | |
21 fprintf(stderr, "\t-v n\t\tverbosity level\n"); | |
22 exit(0); | |
23 } | |
24 | |
25 int extract_subband(Image_tree s, Image_tree t, int name, double alpha, double watermark[], double threshold, int w, int n, int verbose) { | |
26 int i, j; | |
27 | |
28 for (i = 5; i < s->image->height-5; i++) | |
29 for (j = 5; j < s->image->width-5; j++) { | |
30 double orig_coeff, input_coeff; | |
31 | |
32 orig_coeff = get_pixel(s->image, i, j); | |
33 input_coeff = get_pixel(t->image, i, j); | |
34 if (fabs(orig_coeff) > threshold) { | |
35 watermark[w++] = (input_coeff - orig_coeff) / (alpha * orig_coeff); | |
36 } | |
37 } | |
38 | |
39 if (verbose > 5) | |
40 fprintf(stderr, "%s: extracted %s%d, size %d x %d; %d coeffs. total\n", | |
41 progname, subband_name(name), s->level, s->image->width, s->image->height, w); | |
42 | |
43 return w; | |
44 } | |
45 | |
46 void write_mark(FILE *out, double watermark[], int n) { | |
47 int i; | |
48 | |
49 fprintf(out, "%d\n", n); | |
50 for (i = 0; i < n; i++) | |
51 fprintf(out, "%f\n", watermark[i]); | |
52 } | |
53 | |
54 int main(int argc, char *argv[]) { | |
55 | |
56 FILE *in = stdin; | |
57 FILE *out = stdout; | |
58 FILE *orig = NULL; | |
59 FILE *sig = NULL; | |
60 | |
61 gray **input_image; | |
62 gray **orig_image; | |
63 | |
64 char signature_name[MAXPATHLEN]; | |
65 char output_name[MAXPATHLEN] = "(stdout)"; | |
66 char input_name[MAXPATHLEN] = "(stdin)"; | |
67 char orig_name[MAXPATHLEN]; | |
68 | |
69 int c, w; | |
70 int n = 0; | |
71 int method = -1; | |
72 int filter = 0; | |
73 char filter_name[MAXPATHLEN] = ""; | |
74 | |
75 int level = 0, levels; | |
76 double alpha_detail = 0.0; | |
77 double alpha_approx = 0.0; | |
78 | |
79 int in_rows, in_cols, in_format; | |
80 gray in_maxval; | |
81 int orig_rows, orig_cols, orig_format; | |
82 gray orig_maxval; | |
83 int rows, cols; | |
84 int row; | |
85 | |
86 double *watermark; | |
87 | |
88 Image_tree input_dwts, orig_dwts, p, q; | |
89 | |
90 int verbose = 0; | |
91 | |
92 progname = argv[0]; | |
93 | |
94 pgm_init(&argc, argv); wm_init2(); | |
95 | |
96 while ((c = getopt(argc, argv, "a:A:e:f:F:h?i:l:o:s:v:")) != EOF) { | |
97 switch (c) { | |
98 case 'a': | |
99 alpha_detail = atof(optarg); | |
100 if (alpha_detail <= 0.0) { | |
101 fprintf(stderr, "%s: alpha factor %f out of range\n", progname, alpha_detail); | |
102 exit(1); | |
103 } | |
104 break; | |
105 case 'A': | |
106 alpha_approx = atof(optarg); | |
107 if (alpha_approx <= 0.0) { | |
108 fprintf(stderr, "%s: alpha factor %f out of range\n", progname, alpha_approx); | |
109 exit(1); | |
110 } | |
111 break; | |
112 case 'e': | |
113 method = atoi(optarg); | |
114 if (method < 0) { | |
115 fprintf(stderr, "%s: wavelet filtering method %d out of range\n", progname, method); | |
116 exit(1); | |
117 } | |
118 break; | |
119 case 'f': | |
120 filter = atoi(optarg); | |
121 if (filter <= 0) { | |
122 fprintf(stderr, "%s: filter number %d out of range\n", progname, filter); | |
123 exit(1); | |
124 } | |
125 break; | |
126 case 'F': | |
127 strcpy(filter_name, optarg); | |
128 break; | |
129 case 'h': | |
130 case '?': | |
131 usage(); | |
132 break; | |
133 case 'i': | |
134 if ((orig = fopen(optarg, "rb")) == NULL) { | |
135 fprintf(stderr, "%s: unable to open original image file %s\n", progname, optarg); | |
136 exit(1); | |
137 } | |
138 strcpy(orig_name, optarg); | |
139 break; | |
140 case 'l': | |
141 level = atoi(optarg); | |
142 if (level <= 0) { | |
143 fprintf(stderr, "%s: decomposition level %d out of range\n", progname, level); | |
144 exit(1); | |
145 } | |
146 break; | |
147 case 'o': | |
148 if ((out = fopen(optarg, "w")) == NULL) { | |
149 fprintf(stderr, "%s: unable to open output file %s\n", progname, optarg); | |
150 exit(1); | |
151 } | |
152 strcpy(output_name, optarg); | |
153 break; | |
154 case 's': | |
155 if ((sig = fopen(optarg, "r")) == NULL) { | |
156 fprintf(stderr, "%s: unable to open signature file %s\n", progname, optarg); | |
157 exit(1); | |
158 } | |
159 strcpy(signature_name, optarg); | |
160 break; | |
161 case 'v': | |
162 verbose = atoi(optarg); | |
163 if (verbose < 0) { | |
164 fprintf(stderr, "%s: verbosity level %d out of range\n", progname, verbose); | |
165 exit(1); | |
166 } | |
167 break; | |
168 } | |
169 } | |
170 | |
171 argc -= optind; | |
172 argv += optind; | |
173 | |
174 if (argc > 1) { | |
175 usage(); | |
176 exit(1); | |
177 } | |
178 | |
179 if (argc == 1 && *argv[0] != '-') { | |
180 if ((in = fopen(argv[0], "rb")) == NULL) { | |
181 fprintf(stderr, "%s: unable to open input file %s\n", progname, argv[0]); | |
182 exit(1); | |
183 } | |
184 else | |
185 strcpy(input_name, argv[0]); | |
186 } | |
187 | |
188 if (!orig) { | |
189 fprintf(stderr, "%s: original image file not specified, use -i file option\n", progname); | |
190 exit(1); | |
191 } | |
192 | |
193 if (sig) { | |
194 char line[32]; | |
195 fgets(line, sizeof(line), sig); | |
196 if (strspn(line, "KISG") >= 4) { | |
197 fscanf(sig, "%d\n", &n); | |
198 if (alpha_detail == 0.0) | |
199 fscanf(sig, "%lf\n", &alpha_detail); | |
200 else | |
201 fscanf(sig, "%*f\n"); | |
202 if (alpha_approx == 0.0) | |
203 fscanf(sig, "%lf\n", &alpha_approx); | |
204 else | |
205 fscanf(sig, "%*f\n"); | |
206 if (level == 0) | |
207 fscanf(sig, "%d\n", &level); | |
208 else | |
209 fscanf(sig, "%*d\n"); | |
210 if (method < 0) | |
211 fscanf(sig, "%d\n", &method); | |
212 else | |
213 fscanf(sig, "%*d\n"); | |
214 if (filter == 0) | |
215 fscanf(sig, "%d\n", &filter); | |
216 else | |
217 fscanf(sig, "%*d\n"); | |
218 if (!strcmp(filter_name, "")) | |
219 fscanf(sig, "%[^\n\r]\n", filter_name); | |
220 else | |
221 fscanf(sig, "%*[^\n\r]\n"); | |
222 } | |
223 else { | |
224 fprintf(stderr, "%s: invalid signature file %s\n", progname, signature_name); | |
225 exit(1); | |
226 } | |
227 fclose(sig); | |
228 } | |
229 else { | |
230 fprintf(stderr, "%s: signature file not specified, use -s file option\n", progname); | |
231 exit(1); | |
232 } | |
233 | |
234 pgm_readpgminit(in, &in_cols, &in_rows, &in_maxval, &in_format); | |
235 pgm_readpgminit(orig, &orig_cols, &orig_rows, &orig_maxval, &orig_format); | |
236 | |
237 if (in_cols != orig_cols || in_rows != orig_rows) { | |
238 fprintf(stderr, "%s: input image %s does not match dimensions of original image %s\n", progname, input_name, orig_name); | |
239 exit(1); | |
240 } | |
241 | |
242 cols = in_cols; | |
243 rows = in_rows; | |
244 | |
245 input_image = pgm_allocarray(in_cols, in_rows); | |
246 orig_image = pgm_allocarray(orig_cols, orig_rows); | |
247 | |
248 for (row = 0; row < in_rows; row++) { | |
249 pgm_readpgmrow(in, input_image[row], in_cols, in_maxval, in_format); | |
250 pgm_readpgmrow(orig, orig_image[row], orig_cols, orig_maxval, orig_format); | |
251 } | |
252 | |
253 fclose(in); | |
254 fclose(orig); | |
255 | |
256 // complete decomposition | |
257 levels = find_deepest_level(cols, rows) - 1; | |
258 if (level > levels) { | |
259 fprintf(stderr, "%s: decomposition level %d not possible (max. %d), image size is %d x %d\n", progname, level, levels, cols, rows); | |
260 exit(1); | |
261 } | |
262 | |
263 init_dwt(cols, rows, filter_name, filter, level, method); | |
264 #ifdef POLLEN_STUFF | |
265 #include "pollen_stuff.c" | |
266 #endif | |
267 #ifdef PARAM_STUFF | |
268 #include "param_stuff.c" | |
269 #endif | |
270 | |
271 input_dwts = fdwt(input_image); | |
272 orig_dwts = fdwt(orig_image); | |
273 | |
274 watermark = malloc((rows * cols) * sizeof(double)); | |
275 if (!watermark) { | |
276 fprintf(stderr, "%s: malloc() failed\n\n", progname); | |
277 exit(1); | |
278 } | |
279 | |
280 p = input_dwts; | |
281 q = orig_dwts; | |
282 w = 0; | |
283 while (p->coarse && q->coarse) { | |
284 int current_level; | |
285 double threshold; | |
286 double max_coeff; | |
287 double alpha; | |
288 | |
289 // get current decomposition level number | |
290 current_level = q->horizontal->level; | |
291 | |
292 // find largest absolute coefficient in detail subbands of current decomposition level | |
293 max_coeff = find_level_largest_coeff(q, verbose); | |
294 | |
295 // calculate significance threshold for current decomposition level | |
296 threshold = calc_level_threshold(max_coeff, verbose); | |
297 | |
298 // calculate embedding strength alpha for current decomposition level | |
299 alpha = calc_level_alpha_detail(alpha_detail, level, current_level, verbose); | |
300 | |
301 if (verbose > 1) | |
302 fprintf(stderr, "%s: level %d, threshold %f, alpha %f\n", progname, current_level, threshold, alpha); | |
303 | |
304 w = extract_subband(q->horizontal, p->horizontal, HORIZONTAL, alpha, watermark, threshold, w, n, verbose); | |
305 w = extract_subband(q->vertical, p->vertical, VERTICAL, alpha, watermark, threshold, w, n, verbose); | |
306 w = extract_subband(q->diagonal, p->diagonal, DIAGONAL, alpha, watermark, threshold, w, n, verbose); | |
307 | |
308 p = p->coarse; | |
309 q = q->coarse; | |
310 } | |
311 | |
312 // extract watermark from approximation image using calculated significance threshold and embedding strength | |
313 w = extract_subband(q, p, COARSE, alpha_approx, watermark, calc_level_threshold(find_subband_largest_coeff(q, COARSE, verbose), verbose), w, n, verbose); | |
314 | |
315 fprintf(out, "KIWM\n"); | |
316 write_mark(out, watermark, w); | |
317 | |
318 fclose(out); | |
319 | |
320 free(watermark); | |
321 | |
322 pgm_freearray(input_image, rows); | |
323 pgm_freearray(orig_image, rows); | |
324 | |
325 exit(0); | |
326 } |