Mercurial > hg > wm
comparison Meerwald/wm_dugad_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 "wm.h" | |
2 #include "dwt.h" | |
3 #include "dwt_util.h" | |
4 #include "pgm.h" | |
5 | |
6 char *progname; | |
7 | |
8 void usage(void) { | |
9 fprintf(stderr, "usage: %s [-a n] [-e n] [-f n] [-F file] [-h] [-l n] [-n n] [-o file] [-v n] [-t n] -s file file\n\n", progname); | |
10 fprintf(stderr, "\t-a n\t\talpha 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-l n\t\tdecomposition levels\n"); | |
16 fprintf(stderr, "\t-n n\t\twatermark length\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-t n\t\tdetection threshold\n"); | |
20 fprintf(stderr, "\t-v n\t\tverbosity level\n"); | |
21 exit(0); | |
22 } | |
23 | |
24 void wm_subband(Image s, double *w, int n, double t2, int *m, double *z, double *v) { | |
25 int i; | |
26 | |
27 *m = 0; | |
28 *z = 0.0; | |
29 *v = 0.0; | |
30 for (i = 0; i < s->width * s->height; i++) | |
31 if (s->data[i] > t2) { | |
32 (*z) += (s->data[i] * w[i % n]); | |
33 (*v) += fabs(s->data[i]); | |
34 (*m)++; | |
35 } | |
36 } | |
37 | |
38 int main(int argc, char *argv[]) { | |
39 | |
40 FILE *in = stdin; | |
41 FILE *out = stdout; | |
42 FILE *sig = NULL; | |
43 | |
44 gray **input_image; | |
45 | |
46 char signature_name[MAXPATHLEN]; | |
47 char output_name[MAXPATHLEN] = "(stdout)"; | |
48 char input_name[MAXPATHLEN] = "(stdin)"; | |
49 | |
50 int c, w; | |
51 int i; | |
52 int n = 0; | |
53 int method = -1; | |
54 int levels = 0; | |
55 int filter = 0; | |
56 char filter_name[MAXPATHLEN] = ""; | |
57 | |
58 double alpha = 0.0; | |
59 double t2 = 0.0; | |
60 | |
61 int in_rows, in_cols, in_format; | |
62 gray in_maxval; | |
63 int rows, cols; | |
64 int row, col; | |
65 | |
66 double *watermark; | |
67 | |
68 Image_tree dwts, s; | |
69 | |
70 int verbose = 0; | |
71 | |
72 progname = argv[0]; | |
73 | |
74 pgm_init(&argc, argv); wm_init2(); | |
75 | |
76 while ((c = getopt(argc, argv, "a:e:f:F:h?l:n:o:s:v:t")) != EOF) { | |
77 switch (c) { | |
78 case 'a': | |
79 alpha = atof(optarg); | |
80 if (alpha <= 0.0) { | |
81 fprintf(stderr, "%s: alpha factor %f out of range\n", progname, alpha); | |
82 exit(1); | |
83 } | |
84 break; | |
85 case 'e': | |
86 method = atoi(optarg); | |
87 if (method < 0) { | |
88 fprintf(stderr, "%s: wavelet filtering method %d out of range\n", progname, method); | |
89 exit(1); | |
90 } | |
91 break; | |
92 case 'f': | |
93 filter = atoi(optarg); | |
94 if (filter <= 0) { | |
95 fprintf(stderr, "%s: filter number %d out of range\n", progname, filter); | |
96 exit(1); | |
97 } | |
98 break; | |
99 case 'F': | |
100 strcpy(filter_name, optarg); | |
101 break; | |
102 case 'h': | |
103 case '?': | |
104 usage(); | |
105 break; | |
106 case 'l': | |
107 levels = atoi(optarg); | |
108 if (levels <= 0) { | |
109 fprintf(stderr, "%s: decomposition level %d out of range\n", levels); | |
110 exit(1); | |
111 } | |
112 break; | |
113 | |
114 case 'n': | |
115 n = atoi(optarg); | |
116 if (n < 1 || n > 32000) { | |
117 fprintf(stderr, "%s: watermark length %d out of range\n", progname, n); | |
118 exit(1); | |
119 } | |
120 break; | |
121 case 'o': | |
122 if ((out = fopen(optarg, "w")) == NULL) { | |
123 fprintf(stderr, "%s: unable to open output file %s\n", progname, optarg); | |
124 exit(1); | |
125 } | |
126 strcpy(output_name, optarg); | |
127 break; | |
128 case 's': | |
129 if ((sig = fopen(optarg, "r")) == NULL) { | |
130 fprintf(stderr, "%s: unable to open signature file %s\n", progname, optarg); | |
131 exit(1); | |
132 } | |
133 strcpy(signature_name, optarg); | |
134 break; | |
135 case 't': | |
136 t2 = atof(optarg); | |
137 if (t2 <= 0.0) { | |
138 fprintf(stderr, "%s: detection threshold %f out of range\n", progname, t2); | |
139 exit(1); | |
140 } | |
141 break; | |
142 case 'v': | |
143 verbose = atoi(optarg); | |
144 if (verbose < 0) { | |
145 fprintf(stderr, "%s: verbosity level %d out of range\n", progname, verbose); | |
146 exit(1); | |
147 } | |
148 break; | |
149 } | |
150 } | |
151 | |
152 argc -= optind; | |
153 argv += optind; | |
154 | |
155 if (argc > 1) { | |
156 usage(); | |
157 exit(1); | |
158 } | |
159 | |
160 if (argc == 1 && *argv[0] != '-') | |
161 if ((in = fopen(argv[0], "rb")) == NULL) { | |
162 fprintf(stderr, "%s: unable to open input file %s\n", progname, argv[0]); | |
163 exit(1); | |
164 } | |
165 else | |
166 strcpy(input_name, argv[0]); | |
167 | |
168 if (sig) { | |
169 char line[32]; | |
170 fgets(line, sizeof(line), sig); | |
171 if (strspn(line, "DGSG") >= 4) { | |
172 fscanf(sig, "%d\n", &n); | |
173 if (levels == 0) | |
174 fscanf(sig, "%d\n", &levels); | |
175 else | |
176 fscanf(sig, "%*d\n"); | |
177 if (alpha == 0.0) | |
178 fscanf(sig, "%lf\n", &alpha); | |
179 else | |
180 fscanf(sig, "%*lf\n"); | |
181 fscanf(sig, "%*lf\n"); | |
182 if (t2 == 0.0) | |
183 fscanf(sig, "%lf\n", &t2); | |
184 else | |
185 fscanf(sig, "%*lf\n"); | |
186 if (method < 0) | |
187 fscanf(sig, "%d\n", &method); | |
188 else | |
189 fscanf(sig, "%*d\n"); | |
190 if (filter == 0) | |
191 fscanf(sig, "%d\n", &filter); | |
192 else | |
193 fscanf(sig, "%*d\n"); | |
194 if (!strcmp(filter_name, "")) | |
195 fscanf(sig, "%[^\n\r]\n", &filter_name); | |
196 else | |
197 fscanf(sig, "%*[^\n\r]\n"); | |
198 } | |
199 else { | |
200 fprintf(stderr, "%s: invalid signature file %s\n", progname, signature_name); | |
201 exit(1); | |
202 } | |
203 } | |
204 else { | |
205 fprintf(stderr, "%s: signature file not specified, use -s file option\n", progname); | |
206 exit(1); | |
207 } | |
208 | |
209 watermark = malloc(n * sizeof(double)); | |
210 for (i = 0; i < n; i++) | |
211 fscanf(sig, "%lf\n", &watermark[i]); | |
212 fclose(sig); | |
213 | |
214 pgm_readpgminit(in, &in_cols, &in_rows, &in_maxval, &in_format); | |
215 | |
216 cols = in_cols; | |
217 rows = in_rows; | |
218 | |
219 input_image = pgm_allocarray(in_cols, in_rows); | |
220 | |
221 for (row = 0; row < in_rows; row++) { | |
222 pgm_readpgmrow(in, input_image[row], in_cols, in_maxval, in_format); | |
223 } | |
224 | |
225 fclose(in); | |
226 | |
227 init_dwt(cols, rows, filter_name, filter, levels, method); | |
228 #ifdef POLLEN_STUFF | |
229 #include "pollen_stuff.xxx" | |
230 #endif | |
231 #ifdef PARAM_STUFF | |
232 #include "param_stuff.xxx" | |
233 #endif | |
234 | |
235 dwts = fdwt(input_image); | |
236 | |
237 fprintf(out, "DGWM\n"); | |
238 fprintf(out, "%d\n", levels); | |
239 fprintf(out, "%f\n", alpha); | |
240 | |
241 for (i = 0, s = dwts; i < levels; i++, s = s->coarse) { | |
242 int m; | |
243 double z, v; | |
244 | |
245 wm_subband(s->horizontal->image, watermark, n, t2, &m, &z, &v); | |
246 fprintf(out, "%d %f %f\n", m, z, v); | |
247 wm_subband(s->vertical->image, watermark, n, t2, &m, &z, &v); | |
248 fprintf(out, "%d %f %f\n", m, z, v); | |
249 wm_subband(s->diagonal->image, watermark, n, t2, &m, &z, &v); | |
250 fprintf(out, "%d %f %f\n", m, z, v); | |
251 } | |
252 | |
253 fclose(out); | |
254 | |
255 free(watermark); | |
256 | |
257 pgm_freearray(input_image, rows); | |
258 | |
259 exit(0); | |
260 } |