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

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