comparison Meerwald/wm_kim_e.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 n] [-h] [-l n] [-o file] [-v n] -s file file\n\n", progname);
11 fprintf(stderr, "\t-a n\t\talpha factor/embedding strength for detail subbands\n");
12 fprintf(stderr, "\t-a n\t\talpha factor/embedding strength for approximation subband\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-l n\t\tdecomposition level\n");
18 fprintf(stderr, "\t-o file\t\toutput (watermarked) file\n");
19 fprintf(stderr, "\t-s file\t\tsignature to embed in input image\n");
20 fprintf(stderr, "\t-v n\t\tverbosity level\n");
21 exit(0);
22 }
23
24 int mark_subband(Image_tree s, int name, double alpha, double watermark[], double threshold, int w, int n, int verbose) {
25 int i, j;
26
27 for (i = 5; i < s->image->height-5; i++)
28 for (j = 5; j < s->image->width-5; j++) {
29 double coeff, newcoeff;
30
31 coeff = get_pixel(s->image, i, j);
32 if (fabs(coeff) > threshold) {
33 newcoeff = coeff + alpha * coeff * watermark[w % n];
34 set_pixel(s->image, i, j, newcoeff);
35
36 if (verbose >= 9)
37 fprintf(stderr, "%s: (%d/%d) %f: %f -> %f\n", progname, j, i, watermark[w % n], coeff, newcoeff);
38
39 w++;
40 }
41 }
42
43 if (verbose > 5)
44 fprintf(stderr, "%s: watermarking %s%d, size %d x %d; embedded %d coeffs. total\n",
45 progname, subband_name(name), s->level, s->image->width, s->image->height, w);
46
47 return w;
48 }
49
50 int main(int argc, char *argv[]) {
51
52 FILE *in = stdin;
53 FILE *out = stdout;
54 FILE *sig = NULL;
55
56 char output_name[MAXPATHLEN] = "(stdout)";
57 char input_name[MAXPATHLEN] = "(stdin)";
58 char signature_name[MAXPATHLEN];
59
60 int i, c, w;
61 int row, col;
62
63 int n;
64
65 double alpha_detail = 0.0;
66 double alpha_approx = 0.0;
67 int level = 0;
68
69 int filter = 0;
70 int method = -1;
71 int levels;
72 char filter_name[MAXPATHLEN] = "";
73
74 int verbose = 0;
75
76 gray **image;
77 Image_tree p, dwts;
78
79 gray maxval;
80 int rows, cols, colors, format;
81
82 double *watermark;
83
84 progname = argv[0];
85
86 pgm_init(&argc, argv); wm_init();
87
88 while ((c = getopt(argc, argv, "a:A:e:f:F:h?o:l:s:v:")) != EOF) {
89 switch (c) {
90 case 'a':
91 alpha_detail = atof(optarg);
92 if (alpha_detail <= 0.0) {
93 fprintf(stderr, "%s: alpha factor %f out of range\n", progname, alpha_detail);
94 exit(1);
95 }
96 break;
97 case 'A':
98 alpha_approx = atof(optarg);
99 if (alpha_approx <= 0.0) {
100 fprintf(stderr, "%s: alpha factor %f out of range\n", progname, alpha_approx);
101 exit(1);
102 }
103 break;
104 case 'l':
105 level = atoi(optarg);
106 if (level <= 0) {
107 fprintf(stderr, "%s: decomposition level %d out of range\n", progname, level);
108 exit(1);
109 }
110 break;
111 case 'e':
112 method = atoi(optarg);
113 if (method < 0) {
114 fprintf(stderr, "%s: wavelet filtering method %d out of range\n", progname, method);
115 exit(1);
116 }
117 break;
118 case 'f':
119 filter = atoi(optarg);
120 if (filter <= 0) {
121 fprintf(stderr, "%s: filter number %d out of range\n", progname, filter);
122 exit(1);
123 }
124 break;
125 case 'F':
126 strcpy(filter_name, optarg);
127 break;
128 case 'h':
129 case '?':
130 usage();
131 break;
132 case 'o':
133 if ((out = fopen(optarg, "wb")) == NULL) {
134 fprintf(stderr, "%s: unable to open output file %s\n", progname, optarg);
135 exit(1);
136 }
137 strcpy(output_name, optarg);
138 break;
139 case 's':
140 if ((sig = fopen(optarg, "r")) == NULL) {
141 fprintf(stderr, "%s: unable to open signature file %s\n", progname, optarg);
142 exit(1);
143 }
144 strcpy(signature_name, optarg);
145 break;
146 case 'v':
147 verbose = atoi(optarg);
148 if (verbose < 0) {
149 fprintf(stderr, "%s: verbosity level %d out of range\n", progname, verbose);
150 exit(1);
151 }
152 break;
153 }
154 }
155
156 argc -= optind;
157 argv += optind;
158
159 if (argc > 1) {
160 usage();
161 exit(1);
162 }
163
164 if (argc == 1 && *argv[0] != '-')
165 if ((in = fopen(argv[0], "rb")) == NULL) {
166 fprintf(stderr, "%s: unable to open input file %s\n", progname, argv[0]);
167 exit(1);
168 }
169 else
170 strcpy(input_name, argv[0]);
171
172 if (sig) {
173 char line[32];
174 fgets(line, sizeof(line), sig);
175 if (strspn(line, "KISG") >= 4) {
176 fscanf(sig, "%d\n", &n);
177 if (alpha_detail == 0.0)
178 fscanf(sig, "%lf\n", &alpha_detail);
179 else
180 fscanf(sig, "%*lf\n");
181 if (alpha_approx == 0.0)
182 fscanf(sig, "%lf\n", &alpha_approx);
183 else
184 fscanf(sig, "%*lf\n");
185 if (level == 0)
186 fscanf(sig, "%d\n", &level);
187 else
188 fscanf(sig, "%*d\n");
189 if (method < 0)
190 fscanf(sig, "%d\n", &method);
191 else
192 fscanf(sig, "%*d\n");
193 if (filter == 0)
194 fscanf(sig, "%d\n", &filter);
195 else
196 fscanf(sig, "%*d\n");
197 if (!strcmp(filter_name, ""))
198 fscanf(sig, "%[^\n\r]\n", &filter_name);
199 else
200 fscanf(sig, "%*[^\n\r]\n");
201 }
202 else {
203 fprintf(stderr, "%s: invalid signature file %s\n", progname, signature_name);
204 exit(1);
205 }
206 }
207 else {
208 fprintf(stderr, "%s: signature file not specified, use -s file option\n", progname);
209 exit(1);
210 }
211
212 watermark = malloc(n * sizeof(double));
213 for (i = 0; i < n; i++)
214 fscanf(sig, "%lf\n", &watermark[i]);
215 fclose(sig);
216
217 pgm_readpgminit(in, &cols, &rows, &maxval, &format);
218 image = pgm_allocarray(cols, rows);
219 for (row = 0; row < rows; row++)
220 pgm_readpgmrow(in, image[row], cols, maxval, format);
221 fclose(in);
222
223 // complete decomposition
224 levels = find_deepest_level(cols, rows) - 1;
225 if (level > levels) {
226 fprintf(stderr, "%s: decomposition level %d not possible (max. %d), image size is %d x %d\n", progname, level, levels, cols, rows);
227 exit(1);
228 }
229
230 // wavelet transform
231 init_dwt(cols, rows, filter_name, filter, level, method);
232 #ifdef POLLEN_STUFF
233 #include "pollen_stuff.xxx"
234 #endif
235 #ifdef PARAM_STUFF
236 #include "param_stuff.xxx"
237 #endif
238
239 dwts = fdwt(image);
240
241 p = dwts;
242 w = 0;
243
244 // process each decomposition level
245 while (p->coarse) {
246 int current_level;
247 double threshold;
248 double max_coeff;
249 double alpha;
250
251 // get current decomposition level number
252 current_level = p->horizontal->level;
253
254 // find largest absolute coefficient in detail subbands of current decomposition level
255 max_coeff = find_level_largest_coeff(p, verbose);
256
257 // calculate significance threshold for current decomposition level
258 threshold = calc_level_threshold(max_coeff, verbose);
259
260 // calculate embedding strength alpha for current decomposition level
261 alpha = calc_level_alpha_detail(alpha_detail, level, current_level, verbose);
262
263 if (verbose > 1)
264 fprintf(stderr, "%s: level %d, threshold %f, alpha %f\n", progname, current_level, threshold, alpha);
265
266 // embed watermark sequence into detail subbands of current decomposition level
267 w = mark_subband(p->horizontal, HORIZONTAL, alpha, watermark, threshold, w, n, verbose);
268 w = mark_subband(p->vertical, VERTICAL, alpha, watermark, threshold, w, n, verbose);
269 w = mark_subband(p->diagonal, DIAGONAL, alpha, watermark, threshold, w, n, verbose);
270
271 p = p->coarse;
272 }
273
274 // mark approximation image using calculated significance threshold and embedding strength
275 w = mark_subband(p, COARSE, alpha_approx, watermark, calc_level_threshold(find_subband_largest_coeff(p, COARSE, verbose), verbose), w, n, verbose);
276
277 free(watermark);
278 idwt(dwts, image);
279
280 pgm_writepgminit(out, cols, rows, maxval, 0);
281 for (row = 0; row < rows; row++)
282 pgm_writepgmrow(out, image[row], cols, maxval, 0);
283 fclose(out);
284
285 pgm_freearray(image, rows);
286
287 exit(0);
288 }

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