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