comparison Meerwald/wm_kim_d.c @ 0:be303a3f5ea8

import
author Peter Meerwald <pmeerw@cosy.sbg.ac.at>
date Sun, 12 Aug 2007 13:14:34 +0200
parents
children acb6967ee76d
comparison
equal deleted inserted replaced
-1:000000000000 0:be303a3f5ea8
1 #include "wm.h"
2 #include "dwt.h"
3 #include "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 i;
71 int n = 0;
72 int method = -1;
73 int filter = 0;
74 char filter_name[MAXPATHLEN] = "";
75
76 int level = 0, levels;
77 double alpha_detail = 0.0;
78 double alpha_approx = 0.0;
79
80 int in_rows, in_cols, in_format;
81 gray in_maxval;
82 int orig_rows, orig_cols, orig_format;
83 gray orig_maxval;
84 int rows, cols;
85 int row, col;
86
87 double *watermark;
88
89 Image_tree input_dwts, orig_dwts, p, q;
90
91 int verbose = 0;
92
93 progname = argv[0];
94
95 pgm_init(&argc, argv); wm_init2();
96
97 while ((c = getopt(argc, argv, "a:A:e:f:F:h?i:l:o:s:v:")) != EOF) {
98 switch (c) {
99 case 'a':
100 alpha_detail = atof(optarg);
101 if (alpha_detail <= 0.0) {
102 fprintf(stderr, "%s: alpha factor %f out of range\n", progname, alpha_detail);
103 exit(1);
104 }
105 break;
106 case 'A':
107 alpha_approx = atof(optarg);
108 if (alpha_approx <= 0.0) {
109 fprintf(stderr, "%s: alpha factor %f out of range\n", progname, alpha_approx);
110 exit(1);
111 }
112 break;
113 case 'e':
114 method = atoi(optarg);
115 if (method < 0) {
116 fprintf(stderr, "%s: wavelet filtering method %d out of range\n", progname, method);
117 exit(1);
118 }
119 break;
120 case 'f':
121 filter = atoi(optarg);
122 if (filter <= 0) {
123 fprintf(stderr, "%s: filter number %d out of range\n", progname, filter);
124 exit(1);
125 }
126 break;
127 case 'F':
128 strcpy(filter_name, optarg);
129 break;
130 case 'h':
131 case '?':
132 usage();
133 break;
134 case 'i':
135 if ((orig = fopen(optarg, "rb")) == NULL) {
136 fprintf(stderr, "%s: unable to open original image file %s\n", progname, optarg);
137 exit(1);
138 }
139 strcpy(orig_name, optarg);
140 break;
141 case 'l':
142 level = atoi(optarg);
143 if (level <= 0) {
144 fprintf(stderr, "%s: decomposition level %d out of range\n", progname, level);
145 exit(1);
146 }
147 break;
148 case 'o':
149 if ((out = fopen(optarg, "w")) == NULL) {
150 fprintf(stderr, "%s: unable to open output file %s\n", progname, optarg);
151 exit(1);
152 }
153 strcpy(output_name, optarg);
154 break;
155 case 's':
156 if ((sig = fopen(optarg, "r")) == NULL) {
157 fprintf(stderr, "%s: unable to open signature file %s\n", progname, optarg);
158 exit(1);
159 }
160 strcpy(signature_name, optarg);
161 break;
162 case 'v':
163 verbose = atoi(optarg);
164 if (verbose < 0) {
165 fprintf(stderr, "%s: verbosity level %d out of range\n", progname, verbose);
166 exit(1);
167 }
168 break;
169 }
170 }
171
172 argc -= optind;
173 argv += optind;
174
175 if (argc > 1) {
176 usage();
177 exit(1);
178 }
179
180 if (argc == 1 && *argv[0] != '-')
181 if ((in = fopen(argv[0], "rb")) == NULL) {
182 fprintf(stderr, "%s: unable to open input file %s\n", progname, argv[0]);
183 exit(1);
184 }
185 else
186 strcpy(input_name, argv[0]);
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, "%*lf\n");
202 if (alpha_approx == 0.0)
203 fscanf(sig, "%lf\n", &alpha_approx);
204 else
205 fscanf(sig, "%*lf\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.xxx"
266 #endif
267 #ifdef PARAM_STUFF
268 #include "param_stuff.xxx"
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 }

Repositories maintained by Peter Meerwald, pmeerw@pmeerw.net.