Mercurial > hg > wm
annotate Meerwald/wm_corvi_d.c @ 21:1c4ccd635a68 v0.6
ignore *.swp
author | Peter Meerwald-Stadler <pmeerw@pmeerw.net> |
---|---|
date | Sat, 28 Jan 2023 23:57:40 +0100 |
parents | 4987db85cfae |
children |
rev | line source |
---|---|
0 | 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 n = 0; | |
38 int method = -1; | |
39 int filter = 0; | |
40 char filter_name[MAXPATHLEN] = ""; | |
41 | |
3 | 42 int level = 0; |
0 | 43 double alpha = 0.0; |
44 | |
45 int in_rows, in_cols, in_format; | |
46 gray in_maxval; | |
47 int orig_rows, orig_cols, orig_format; | |
48 gray orig_maxval; | |
49 int rows, cols; | |
50 int row, col; | |
51 | |
52 Image_tree input_dwts; | |
53 Image_tree orig_dwts; | |
54 | |
55 int verbose = 0; | |
56 | |
57 progname = argv[0]; | |
58 | |
59 pgm_init(&argc, argv); wm_init2(); | |
60 | |
61 while ((c = getopt(argc, argv, "a:e:f:F:h?i:n:o:s:v:")) != EOF) { | |
62 switch (c) { | |
63 case 'a': | |
64 alpha = atof(optarg); | |
65 if (alpha <= 0.0) { | |
66 fprintf(stderr, "%s: alpha factor %f out of range\n", progname, alpha); | |
67 exit(1); | |
68 } | |
69 break; | |
70 case 'e': | |
71 method = atoi(optarg); | |
72 if (method < 0) { | |
73 fprintf(stderr, "%s: wavelet filtering method %d out of range\n", progname, method); | |
74 exit(1); | |
75 } | |
76 break; | |
77 case 'f': | |
78 filter = atoi(optarg); | |
79 if (filter <= 0) { | |
80 fprintf(stderr, "%s: filter number %d out of range\n", progname, filter); | |
81 exit(1); | |
82 } | |
83 break; | |
84 case 'F': | |
85 strcpy(filter_name, optarg); | |
86 break; | |
87 case 'h': | |
88 case '?': | |
89 usage(); | |
90 break; | |
91 case 'i': | |
92 if ((orig = fopen(optarg, "rb")) == NULL) { | |
93 fprintf(stderr, "%s: unable to open original image file %s\n", progname, optarg); | |
94 exit(1); | |
95 } | |
96 strcpy(orig_name, optarg); | |
97 break; | |
98 case 'n': | |
99 n = atoi(optarg); | |
100 if (n < 1 || n > 1000) { | |
101 fprintf(stderr, "%s: watermark length %d out of range\n", progname, n); | |
102 exit(1); | |
103 } | |
104 break; | |
105 case 'o': | |
106 if ((out = fopen(optarg, "w")) == NULL) { | |
107 fprintf(stderr, "%s: unable to open output file %s\n", progname, optarg); | |
108 exit(1); | |
109 } | |
110 strcpy(output_name, optarg); | |
111 break; | |
112 case 's': | |
113 if ((sig = fopen(optarg, "r")) == NULL) { | |
114 fprintf(stderr, "%s: unable to open signature file %s\n", progname, optarg); | |
115 exit(1); | |
116 } | |
117 strcpy(signature_name, optarg); | |
118 break; | |
119 case 'v': | |
120 verbose = atoi(optarg); | |
121 if (verbose < 0) { | |
122 fprintf(stderr, "%s: verbosity level %d out of range\n", progname, verbose); | |
123 exit(1); | |
124 } | |
125 break; | |
126 } | |
127 } | |
128 | |
129 argc -= optind; | |
130 argv += optind; | |
131 | |
132 if (argc > 1) { | |
133 usage(); | |
134 exit(1); | |
135 } | |
136 | |
8 | 137 if (argc == 1 && *argv[0] != '-') { |
0 | 138 if ((in = fopen(argv[0], "rb")) == NULL) { |
139 fprintf(stderr, "%s: unable to open input file %s\n", progname, argv[0]); | |
140 exit(1); | |
141 } | |
142 else | |
143 strcpy(input_name, argv[0]); | |
8 | 144 } |
145 | |
0 | 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 | |
16
4987db85cfae
fix another scanf() warning
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
15
diff
changeset
|
159 fscanf(sig, "%*f\n"); |
0 | 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, "")) | |
15 | 169 fscanf(sig, "%[^\n\r]\n", filter_name); |
0 | 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 | |
3 | 224 #include "pollen_stuff.c" |
0 | 225 #endif |
226 #ifdef PARAM_STUFF | |
3 | 227 #include "param_stuff.c" |
0 | 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 } |