0
|
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
|
8
|
50 int c;
|
0
|
51 int i;
|
|
52 int n = 0;
|
|
53 int method = -1;
|
3
|
54 int level = 0;
|
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;
|
8
|
64 int row;
|
0
|
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':
|
3
|
107 level = atoi(optarg);
|
|
108 if (level <= 0) {
|
8
|
109 fprintf(stderr, "%s: decomposition level %d out of range\n", progname, level);
|
0
|
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
|
8
|
160 if (argc == 1 && *argv[0] != '-') {
|
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]);
|
8
|
167 }
|
|
168
|
0
|
169 if (sig) {
|
|
170 char line[32];
|
|
171 fgets(line, sizeof(line), sig);
|
|
172 if (strspn(line, "DGSG") >= 4) {
|
|
173 fscanf(sig, "%d\n", &n);
|
3
|
174 if (level == 0)
|
|
175 fscanf(sig, "%d\n", &level);
|
0
|
176 else
|
|
177 fscanf(sig, "%*d\n");
|
|
178 if (alpha == 0.0)
|
|
179 fscanf(sig, "%lf\n", &alpha);
|
|
180 else
|
|
181 fscanf(sig, "%*lf\n");
|
|
182 fscanf(sig, "%*lf\n");
|
|
183 if (t2 == 0.0)
|
|
184 fscanf(sig, "%lf\n", &t2);
|
|
185 else
|
|
186 fscanf(sig, "%*lf\n");
|
|
187 if (method < 0)
|
|
188 fscanf(sig, "%d\n", &method);
|
|
189 else
|
|
190 fscanf(sig, "%*d\n");
|
|
191 if (filter == 0)
|
|
192 fscanf(sig, "%d\n", &filter);
|
|
193 else
|
|
194 fscanf(sig, "%*d\n");
|
|
195 if (!strcmp(filter_name, ""))
|
15
|
196 fscanf(sig, "%[^\n\r]\n", filter_name);
|
0
|
197 else
|
|
198 fscanf(sig, "%*[^\n\r]\n");
|
|
199 }
|
|
200 else {
|
|
201 fprintf(stderr, "%s: invalid signature file %s\n", progname, signature_name);
|
|
202 exit(1);
|
|
203 }
|
|
204 }
|
|
205 else {
|
|
206 fprintf(stderr, "%s: signature file not specified, use -s file option\n", progname);
|
|
207 exit(1);
|
|
208 }
|
|
209
|
|
210 watermark = malloc(n * sizeof(double));
|
|
211 for (i = 0; i < n; i++)
|
|
212 fscanf(sig, "%lf\n", &watermark[i]);
|
|
213 fclose(sig);
|
|
214
|
|
215 pgm_readpgminit(in, &in_cols, &in_rows, &in_maxval, &in_format);
|
|
216
|
|
217 cols = in_cols;
|
|
218 rows = in_rows;
|
|
219
|
|
220 input_image = pgm_allocarray(in_cols, in_rows);
|
|
221
|
|
222 for (row = 0; row < in_rows; row++) {
|
|
223 pgm_readpgmrow(in, input_image[row], in_cols, in_maxval, in_format);
|
|
224 }
|
|
225
|
|
226 fclose(in);
|
|
227
|
3
|
228 init_dwt(cols, rows, filter_name, filter, level, method);
|
0
|
229 #ifdef POLLEN_STUFF
|
3
|
230 #include "pollen_stuff.c"
|
0
|
231 #endif
|
|
232 #ifdef PARAM_STUFF
|
3
|
233 #include "param_stuff.c"
|
0
|
234 #endif
|
|
235
|
|
236 dwts = fdwt(input_image);
|
|
237
|
|
238 fprintf(out, "DGWM\n");
|
3
|
239 fprintf(out, "%d\n", level);
|
0
|
240 fprintf(out, "%f\n", alpha);
|
|
241
|
3
|
242 for (i = 0, s = dwts; i < level; i++, s = s->coarse) {
|
0
|
243 int m;
|
|
244 double z, v;
|
|
245
|
|
246 wm_subband(s->horizontal->image, watermark, n, t2, &m, &z, &v);
|
|
247 fprintf(out, "%d %f %f\n", m, z, v);
|
|
248 wm_subband(s->vertical->image, watermark, n, t2, &m, &z, &v);
|
|
249 fprintf(out, "%d %f %f\n", m, z, v);
|
|
250 wm_subband(s->diagonal->image, watermark, n, t2, &m, &z, &v);
|
|
251 fprintf(out, "%d %f %f\n", m, z, v);
|
|
252 }
|
|
253
|
|
254 fclose(out);
|
|
255
|
|
256 free(watermark);
|
|
257
|
|
258 pgm_freearray(input_image, rows);
|
|
259
|
|
260 exit(0);
|
|
261 }
|