0
|
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 file] [-h] [-n n] [-o file] [-v n] -s file -i file file\n\n", progname);
|
|
11 fprintf(stderr, "\t-a n\t\talpha factor\n");
|
|
12 fprintf(stderr, "\t-b n\t\tbeta 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-i file\t\toriginal image file\n");
|
|
18 fprintf(stderr, "\t-n n\t\twatermark length\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 main(int argc, char *argv[]) {
|
|
26
|
|
27 FILE *in = stdin;
|
|
28 FILE *out = stdout;
|
|
29 FILE *orig = NULL;
|
|
30 FILE *sig = NULL;
|
|
31
|
|
32 gray **input_image;
|
|
33 gray **orig_image;
|
|
34
|
|
35 char signature_name[MAXPATHLEN];
|
|
36 char output_name[MAXPATHLEN] = "(stdout)";
|
|
37 char input_name[MAXPATHLEN] = "(stdin)";
|
|
38 char orig_name[MAXPATHLEN];
|
|
39
|
|
40 int c, w;
|
|
41 int i;
|
|
42 int n = 0;
|
|
43 int method = -1;
|
|
44 int filter = 0;
|
|
45 char filter_name[MAXPATHLEN] = "";
|
|
46
|
|
47 int level, levels;
|
|
48 double alpha = 0.0;
|
|
49 double beta = 0.0;
|
|
50
|
|
51 int in_rows, in_cols, in_format;
|
|
52 gray in_maxval;
|
|
53 int orig_rows, orig_cols, orig_format;
|
|
54 gray orig_maxval;
|
|
55 int rows, cols;
|
|
56 int row, col;
|
|
57
|
|
58 double *watermark;
|
|
59
|
|
60 Image_tree input_dwts;
|
|
61 Image_tree orig_dwts;
|
|
62
|
|
63 int verbose = 0;
|
|
64
|
|
65 progname = argv[0];
|
|
66
|
|
67 pgm_init(&argc, argv); wm_init2();
|
|
68
|
|
69 while ((c = getopt(argc, argv, "a:b:e:f:F:h?i:n:o:s:v:")) != EOF) {
|
|
70 switch (c) {
|
|
71 case 'a':
|
|
72 alpha = atof(optarg);
|
|
73 if (alpha <= 0.0) {
|
|
74 fprintf(stderr, "%s: alpha factor %f out of range\n", progname, alpha);
|
|
75 exit(1);
|
|
76 }
|
|
77 break;
|
|
78 case 'b':
|
|
79 beta = atof(optarg);
|
|
80 if (beta <= 0.0) {
|
|
81 fprintf(stderr, "%s: beta factor %f out of range\n", progname, alpha);
|
|
82 exit(1);
|
|
83 }
|
|
84 break;
|
|
85 case 'e':
|
|
86 method = atoi(optarg);
|
|
87 if (method < 0) {
|
|
88 fprintf(stderr, "%s: wavelet filtering method %d out of range\n", progname, method);
|
|
89 exit(1);
|
|
90 }
|
|
91 break;
|
|
92 case 'f':
|
|
93 filter = atoi(optarg);
|
|
94 if (filter <= 0) {
|
|
95 fprintf(stderr, "%s: filter number %d out of range\n", progname, filter);
|
|
96 exit(1);
|
|
97 }
|
|
98 break;
|
|
99 case 'F':
|
|
100 strcpy(filter_name, optarg);
|
|
101 break;
|
|
102 case 'h':
|
|
103 case '?':
|
|
104 usage();
|
|
105 break;
|
|
106 case 'i':
|
|
107 if ((orig = fopen(optarg, "rb")) == NULL) {
|
|
108 fprintf(stderr, "%s: unable to open original image file %s\n", progname, optarg);
|
|
109 exit(1);
|
|
110 }
|
|
111 strcpy(orig_name, optarg);
|
|
112 break;
|
|
113 case 'n':
|
|
114 n = atoi(optarg);
|
|
115 if (n < 1 || n > 32000) {
|
|
116 fprintf(stderr, "%s: watermark length %d out of range\n", progname, n);
|
|
117 exit(1);
|
|
118 }
|
|
119 break;
|
|
120 case 'o':
|
|
121 if ((out = fopen(optarg, "w")) == NULL) {
|
|
122 fprintf(stderr, "%s: unable to open output file %s\n", progname, optarg);
|
|
123 exit(1);
|
|
124 }
|
|
125 strcpy(output_name, optarg);
|
|
126 break;
|
|
127 case 's':
|
|
128 if ((sig = fopen(optarg, "r")) == NULL) {
|
|
129 fprintf(stderr, "%s: unable to open signature file %s\n", progname, optarg);
|
|
130 exit(1);
|
|
131 }
|
|
132 strcpy(signature_name, optarg);
|
|
133 break;
|
|
134 case 'v':
|
|
135 verbose = atoi(optarg);
|
|
136 if (verbose < 0) {
|
|
137 fprintf(stderr, "%s: verbosity level %d out of range\n", progname, verbose);
|
|
138 exit(1);
|
|
139 }
|
|
140 break;
|
|
141 }
|
|
142 }
|
|
143
|
|
144 argc -= optind;
|
|
145 argv += optind;
|
|
146
|
|
147 if (argc > 1) {
|
|
148 usage();
|
|
149 exit(1);
|
|
150 }
|
|
151
|
|
152 if (argc == 1 && *argv[0] != '-')
|
|
153 if ((in = fopen(argv[0], "rb")) == NULL) {
|
|
154 fprintf(stderr, "%s: unable to open input file %s\n", progname, argv[0]);
|
|
155 exit(1);
|
|
156 }
|
|
157 else
|
|
158 strcpy(input_name, argv[0]);
|
|
159
|
|
160 if (!orig) {
|
|
161 fprintf(stderr, "%s: original image file not specified, use -i file option\n", progname);
|
|
162 exit(1);
|
|
163 }
|
|
164
|
|
165 if (sig) {
|
|
166 char line[32];
|
|
167 fgets(line, sizeof(line), sig);
|
|
168 if (strspn(line, "WGSG") >= 4) {
|
|
169 fscanf(sig, "%d\n", &n);
|
|
170 if (alpha == 0.0)
|
|
171 fscanf(sig, "%lf\n", &alpha);
|
|
172 else
|
|
173 fscanf(sig, "%*lf\n");
|
|
174 if (beta == 0.0)
|
|
175 fscanf(sig, "%lf\n", &beta);
|
|
176 else
|
|
177 fscanf(sig, "%*lf\n");
|
|
178 if (method < 0)
|
|
179 fscanf(sig, "%d\n", &method);
|
|
180 else
|
|
181 fscanf(sig, "%*d\n");
|
|
182 if (filter == 0)
|
|
183 fscanf(sig, "%d\n", &filter);
|
|
184 else
|
|
185 fscanf(sig, "%*d\n");
|
|
186 if (!strcmp(filter_name, ""))
|
|
187 fscanf(sig, "%[^\n\r]\n", &filter_name);
|
|
188 else
|
|
189 fscanf(sig, "%*[^\n\r]\n");
|
|
190 }
|
|
191 else {
|
|
192 fprintf(stderr, "%s: invalid signature file %s\n", progname, signature_name);
|
|
193 exit(1);
|
|
194 }
|
|
195 fclose(sig);
|
|
196 }
|
|
197 else {
|
|
198 fprintf(stderr, "%s: signature file not specified, use -s file option\n", progname);
|
|
199 exit(1);
|
|
200 }
|
|
201
|
|
202 watermark = malloc(n * sizeof(double));
|
|
203
|
|
204 pgm_readpgminit(in, &in_cols, &in_rows, &in_maxval, &in_format);
|
|
205 pgm_readpgminit(orig, &orig_cols, &orig_rows, &orig_maxval, &orig_format);
|
|
206
|
|
207 if (in_cols != orig_cols || in_rows != orig_rows) {
|
|
208 fprintf(stderr, "%s: input image %s does not match dimensions of original image %s\n", progname, input_name, orig_name);
|
|
209 exit(1);
|
|
210 }
|
|
211
|
|
212 cols = in_cols;
|
|
213 rows = in_rows;
|
|
214
|
|
215 input_image = pgm_allocarray(in_cols, in_rows);
|
|
216
|
|
217 orig_image = pgm_allocarray(orig_cols, orig_rows);
|
|
218
|
|
219 for (row = 0; row < in_rows; row++) {
|
|
220 pgm_readpgmrow(in, input_image[row], in_cols, in_maxval, in_format);
|
|
221 pgm_readpgmrow(orig, orig_image[row], orig_cols, orig_maxval, orig_format);
|
|
222 }
|
|
223
|
|
224 fclose(in);
|
|
225 fclose(orig);
|
|
226
|
|
227 // complete decomposition
|
|
228 levels = find_deepest_level(cols, rows) - 1;
|
|
229
|
|
230 init_dwt(cols, rows, filter_name, filter, levels, method);
|
|
231 #ifdef POLLEN_STUFF
|
|
232 #include "pollen_stuff.xxx"
|
|
233 #endif
|
|
234 #ifdef PARAM_STUFF
|
|
235 #include "param_stuff.xxx"
|
|
236 #endif
|
|
237
|
|
238 input_dwts = fdwt(input_image);
|
|
239 orig_dwts = fdwt(orig_image);
|
|
240
|
|
241 // build tree for subband selection, calculate subband thresholds
|
|
242 init_subbands(orig_dwts);
|
|
243 set_subbands_type_beta(HORIZONTAL, beta);
|
|
244 set_subbands_type_beta(VERTICAL, beta);
|
|
245 calc_subbands_threshold();
|
|
246
|
|
247 fprintf(out, "WGWM\n");
|
|
248 fprintf(out, "%d\n", n);
|
|
249
|
|
250 w = 0;
|
|
251 while (w < n) {
|
|
252 Subband_data s;
|
|
253
|
|
254 // select subband with max. threshold
|
|
255 s = select_subband();
|
|
256 if (verbose > 1)
|
|
257 fprintf(stderr, "%s: selected subband %s%d, T=%lf, beta=%lf\n", progname, subband_name(s->type), s->level, s->T, s->beta);
|
|
258
|
|
259 // watermark significant coefficients and set them selected
|
|
260 // check is entire signature has been embedded
|
|
261 c = select_subband_coeff(s);
|
|
262 do {
|
|
263 Pixel p;
|
|
264 Pixel q;
|
|
265 if (c < 0)
|
|
266 // no more significant coefficients in subband
|
|
267 break;
|
|
268
|
|
269 p = get_subband_coeff(s, c);
|
|
270 if (p < s->Cmax) {
|
|
271 q = get_dwt_coeff(input_dwts, s->level, s->type, c);
|
|
272 watermark[w] = (q - p) / (alpha * s->beta * s->T);
|
|
273 fprintf(out, "%lf\n", watermark[w]);
|
|
274
|
|
275 if (verbose > 2)
|
|
276 fprintf(stderr, "%s: detected sig. coeff. #%d (= %lf)\n from %s%d coeff. #%d\n",
|
|
277 progname, w, watermark[w], subband_name(s->type), s->level, c);
|
|
278
|
|
279 w++;
|
|
280 }
|
|
281 mark_subband_coeff(s, c);
|
|
282
|
|
283 // select next significant coefficient
|
|
284 c = select_subband_coeff_from(s, c);
|
|
285 } while (w < n);
|
|
286
|
|
287 // update subband threshold
|
|
288 s->T /= 2.0;
|
|
289
|
|
290 }
|
|
291
|
|
292 fclose(out);
|
|
293
|
|
294 free_subbands();
|
|
295
|
|
296 free(watermark);
|
|
297
|
|
298 pgm_freearray(input_image, rows);
|
|
299 pgm_freearray(orig_image, rows);
|
|
300
|
|
301 exit(0);
|
|
302 }
|