comparison Meerwald/wm_wang_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 "wang_common.h"
4 #include "dwt_util.h"
5 #include "pgm.h"
6
7 char *progname;
8
9 void usage(void) {
10 fprintf(stderr, "usage: %s [-a n] [-b n] [-e n] [-f n] [-F n] [-h] [-o file] [-v n] -s file file\n\n", progname);
11 fprintf(stderr, "\t-a n\t\talpha factor/embedding strength\n");
12 fprintf(stderr, "\t-b n\t\tsubband weighting factor\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-o file\t\toutput (watermarked) file\n");
18 fprintf(stderr, "\t-s file\t\tsignature to embed in input image\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 char output_name[MAXPATHLEN] = "(stdout)";
30 char input_name[MAXPATHLEN] = "(stdin)";
31 char signature_name[MAXPATHLEN];
32
33 int i, c, w;
34 int row, col;
35
36 int n;
37
38 double alpha = 0.0;
39 double beta = 0.0;
40
41 int filter = 0;
42 int method = -1;
43 int levels;
44 char filter_name[MAXPATHLEN] = "";
45
46 int verbose = 0;
47
48 gray **image;
49 Image_tree dwts;
50
51 gray maxval;
52 int rows, cols, colors, format;
53
54 double *watermark;
55
56 progname = argv[0];
57
58 pgm_init(&argc, argv); wm_init();
59
60 while ((c = getopt(argc, argv, "a:b:e:f:F:h?o:s:v:")) != EOF) {
61 switch (c) {
62 case 'a':
63 alpha = atof(optarg);
64 if (alpha <= 0.0) {
65 fprintf(stderr, "%s: alpha factor %f out of range\n", progname, alpha);
66 exit(1);
67 }
68 break;
69 case 'b':
70 beta = atof(optarg);
71 if (beta <= 0.0) {
72 fprintf(stderr, "%s: beta factor %f out of range\n", progname, beta);
73 exit(1);
74 }
75 break;
76 case 'e':
77 method = atoi(optarg);
78 if (method < 0) {
79 fprintf(stderr, "%s: wavelet filtering method %d out of range\n", progname, method);
80 exit(1);
81 }
82 break;
83 case 'f':
84 filter = atoi(optarg);
85 if (filter <= 0) {
86 fprintf(stderr, "%s: filter number %d out of range\n", progname, filter);
87 exit(1);
88 }
89 break;
90 case 'F':
91 strcpy(filter_name, optarg);
92 break;
93 case 'h':
94 case '?':
95 usage();
96 break;
97 case 'o':
98 if ((out = fopen(optarg, "wb")) == NULL) {
99 fprintf(stderr, "%s: unable to open output file %s\n", progname, optarg);
100 exit(1);
101 }
102 strcpy(output_name, optarg);
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
129 if (argc == 1 && *argv[0] != '-')
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]);
136
137 if (sig) {
138 char line[32];
139 fgets(line, sizeof(line), sig);
140 if (strspn(line, "WGSG") >= 4) {
141 fscanf(sig, "%d\n", &n);
142 if (alpha == 0.0)
143 fscanf(sig, "%lf\n", &alpha);
144 else
145 fscanf(sig, "%*lf\n");
146 if (beta == 0.0)
147 fscanf(sig, "%lf\n", &beta);
148 else
149 fscanf(sig, "%*lf\n");
150 if (method < 0)
151 fscanf(sig, "%d\n", &method);
152 else
153 fscanf(sig, "%*d\n");
154 if (filter == 0)
155 fscanf(sig, "%d\n", &filter);
156 else
157 fscanf(sig, "%*d\n");
158 if (!strcmp(filter_name, ""))
159 fscanf(sig, "%[^\n\r]\n", &filter_name);
160 else
161 fscanf(sig, "%*[^\n\r]\n");
162 }
163 else {
164 fprintf(stderr, "%s: invalid signature file %s\n", progname, signature_name);
165 exit(1);
166 }
167 }
168 else {
169 fprintf(stderr, "%s: signature file not specified, use -s file option\n", progname);
170 exit(1);
171 }
172
173 watermark = malloc(n * sizeof(double));
174 for (i = 0; i < n; i++)
175 fscanf(sig, "%lf\n", &watermark[i]);
176 fclose(sig);
177
178 pgm_readpgminit(in, &cols, &rows, &maxval, &format);
179
180 image = pgm_allocarray(cols, rows);
181
182 for (row = 0; row < rows; row++)
183 pgm_readpgmrow(in, image[row], cols, maxval, format);
184
185 fclose(in);
186
187 // complete decomposition
188 levels = find_deepest_level(cols, rows) - 1;
189
190 // wavelet transform
191 init_dwt(cols, rows, filter_name, filter, levels, method);
192 #ifdef POLLEN_STUFF
193 #include "pollen_stuff.xxx"
194 #endif
195 #ifdef PARAM_STUFF
196 #include "param_stuff.xxx"
197 #endif
198
199 dwts = fdwt(image);
200
201 // build tree for subband selection, calculate subband thresholds
202 init_subbands(dwts);
203 set_subbands_type_beta(HORIZONTAL, beta);
204 set_subbands_type_beta(VERTICAL, beta);
205 calc_subbands_threshold();
206
207 w = 0;
208 while (w < n) {
209 Subband_data s;
210
211 // select subband with max. threshold
212 s = select_subband();
213 if (verbose > 1)
214 fprintf(stderr, "%s: selected subband %s%d, T=%lf, beta=%lf\n", progname, subband_name(s->type), s->level, s->T, s->beta);
215
216 // watermark significant coefficients and set them selected
217 // check is entire signature has been embedded
218 c = select_subband_coeff(s);
219 do {
220 double p;
221 if (c < 0)
222 // no more significant coefficients in subband
223 break;
224
225 p = get_subband_coeff(s, c);
226 if (p < s->Cmax) {
227 if (verbose > 2)
228 fprintf(stderr, "%s: embedding sig. coeff. #%d (= %lf)\n into %s%d coeff. #%d\n",
229 progname, w, watermark[w], subband_name(s->type), s->level, c);
230
231 p = p + alpha * s->beta * s->T * watermark[w];
232 set_subband_coeff(s, c, p);
233 w++;
234 }
235 mark_subband_coeff(s, c);
236
237 // select next significant coefficient
238 c = select_subband_coeff_from(s, c);
239 } while (w < n);
240
241 // update subband threshold
242 s->T /= 2.0;
243
244 }
245
246 free_subbands();
247
248 free(watermark);
249
250 idwt(dwts, image);
251
252 pgm_writepgminit(out, cols, rows, maxval, 0);
253
254 for (row = 0; row < rows; row++)
255 pgm_writepgmrow(out, image[row], cols, maxval, 0);
256
257 fclose(out);
258
259 pgm_freearray(image, rows);
260
261 exit(0);
262 }

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