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