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 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;
|
8
|
34 int row;
|
0
|
35
|
|
36 int n;
|
|
37
|
|
38 double alpha = 0.0;
|
|
39 double beta = 0.0;
|
|
40
|
|
41 int filter = 0;
|
|
42 int method = -1;
|
3
|
43 int level = 0;
|
0
|
44 char filter_name[MAXPATHLEN] = "";
|
|
45
|
|
46 int verbose = 0;
|
|
47
|
|
48 gray **image;
|
|
49 Image_tree dwts;
|
|
50
|
|
51 gray maxval;
|
8
|
52 int rows, cols, format;
|
0
|
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
|
8
|
129 if (argc == 1 && *argv[0] != '-') {
|
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]);
|
8
|
136 }
|
|
137
|
0
|
138 if (sig) {
|
|
139 char line[32];
|
|
140 fgets(line, sizeof(line), sig);
|
|
141 if (strspn(line, "WGSG") >= 4) {
|
|
142 fscanf(sig, "%d\n", &n);
|
|
143 if (alpha == 0.0)
|
|
144 fscanf(sig, "%lf\n", &alpha);
|
|
145 else
|
|
146 fscanf(sig, "%*lf\n");
|
|
147 if (beta == 0.0)
|
|
148 fscanf(sig, "%lf\n", &beta);
|
|
149 else
|
|
150 fscanf(sig, "%*lf\n");
|
|
151 if (method < 0)
|
|
152 fscanf(sig, "%d\n", &method);
|
|
153 else
|
|
154 fscanf(sig, "%*d\n");
|
|
155 if (filter == 0)
|
|
156 fscanf(sig, "%d\n", &filter);
|
|
157 else
|
|
158 fscanf(sig, "%*d\n");
|
|
159 if (!strcmp(filter_name, ""))
|
|
160 fscanf(sig, "%[^\n\r]\n", &filter_name);
|
|
161 else
|
|
162 fscanf(sig, "%*[^\n\r]\n");
|
|
163 }
|
|
164 else {
|
|
165 fprintf(stderr, "%s: invalid signature file %s\n", progname, signature_name);
|
|
166 exit(1);
|
|
167 }
|
|
168 }
|
|
169 else {
|
|
170 fprintf(stderr, "%s: signature file not specified, use -s file option\n", progname);
|
|
171 exit(1);
|
|
172 }
|
|
173
|
|
174 watermark = malloc(n * sizeof(double));
|
|
175 for (i = 0; i < n; i++)
|
|
176 fscanf(sig, "%lf\n", &watermark[i]);
|
|
177 fclose(sig);
|
|
178
|
|
179 pgm_readpgminit(in, &cols, &rows, &maxval, &format);
|
|
180
|
|
181 image = pgm_allocarray(cols, rows);
|
|
182
|
|
183 for (row = 0; row < rows; row++)
|
|
184 pgm_readpgmrow(in, image[row], cols, maxval, format);
|
|
185
|
|
186 fclose(in);
|
|
187
|
|
188 // complete decomposition
|
3
|
189 level = find_deepest_level(cols, rows) - 1;
|
0
|
190
|
|
191 // wavelet transform
|
3
|
192 init_dwt(cols, rows, filter_name, filter, level, method);
|
0
|
193 #ifdef POLLEN_STUFF
|
3
|
194 #include "pollen_stuff.c"
|
0
|
195 #endif
|
|
196 #ifdef PARAM_STUFF
|
3
|
197 #include "param_stuff.c"
|
0
|
198 #endif
|
|
199
|
|
200 dwts = fdwt(image);
|
|
201
|
|
202 // build tree for subband selection, calculate subband thresholds
|
|
203 init_subbands(dwts);
|
|
204 set_subbands_type_beta(HORIZONTAL, beta);
|
|
205 set_subbands_type_beta(VERTICAL, beta);
|
|
206 calc_subbands_threshold();
|
|
207
|
|
208 w = 0;
|
|
209 while (w < n) {
|
|
210 Subband_data s;
|
|
211
|
|
212 // select subband with max. threshold
|
|
213 s = select_subband();
|
|
214 if (verbose > 1)
|
|
215 fprintf(stderr, "%s: selected subband %s%d, T=%lf, beta=%lf\n", progname, subband_name(s->type), s->level, s->T, s->beta);
|
|
216
|
|
217 // watermark significant coefficients and set them selected
|
|
218 // check is entire signature has been embedded
|
|
219 c = select_subband_coeff(s);
|
|
220 do {
|
|
221 double p;
|
|
222 if (c < 0)
|
|
223 // no more significant coefficients in subband
|
|
224 break;
|
|
225
|
|
226 p = get_subband_coeff(s, c);
|
|
227 if (p < s->Cmax) {
|
|
228 if (verbose > 2)
|
|
229 fprintf(stderr, "%s: embedding sig. coeff. #%d (= %lf)\n into %s%d coeff. #%d\n",
|
|
230 progname, w, watermark[w], subband_name(s->type), s->level, c);
|
|
231
|
|
232 p = p + alpha * s->beta * s->T * watermark[w];
|
|
233 set_subband_coeff(s, c, p);
|
|
234 w++;
|
|
235 }
|
|
236 mark_subband_coeff(s, c);
|
|
237
|
|
238 // select next significant coefficient
|
|
239 c = select_subband_coeff_from(s, c);
|
|
240 } while (w < n);
|
|
241
|
|
242 // update subband threshold
|
|
243 s->T /= 2.0;
|
|
244
|
|
245 }
|
|
246
|
|
247 free_subbands();
|
|
248
|
|
249 free(watermark);
|
|
250
|
|
251 idwt(dwts, image);
|
|
252
|
|
253 pgm_writepgminit(out, cols, rows, maxval, 0);
|
|
254
|
|
255 for (row = 0; row < rows; row++)
|
|
256 pgm_writepgmrow(out, image[row], cols, maxval, 0);
|
|
257
|
|
258 fclose(out);
|
|
259
|
|
260 pgm_freearray(image, rows);
|
|
261
|
|
262 exit(0);
|
|
263 }
|