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