0
|
1 #include "wm.h"
|
|
2 #include "dwt.h"
|
|
3 #include "pgm.h"
|
|
4
|
|
5 char *progname;
|
|
6
|
|
7 void usage(void) {
|
|
8 fprintf(stderr, "usage: %s [-a n] [-e n] [-f n] [-F n] [-h] [-o file] [-v n] -s file file\n\n", progname);
|
|
9 fprintf(stderr, "\t-a n\t\talpha factor/embedding strength\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-o file\t\toutput (watermarked) file\n");
|
|
15 fprintf(stderr, "\t-s file\t\tsignature to embed in input image\n");
|
|
16 fprintf(stderr, "\t-v n\t\tverbosity level\n");
|
|
17 exit(0);
|
|
18 }
|
|
19
|
|
20 int main(int argc, char *argv[]) {
|
|
21
|
|
22 FILE *in = stdin;
|
|
23 FILE *out = stdout;
|
|
24 FILE *sig = NULL;
|
|
25
|
|
26 char output_name[MAXPATHLEN] = "(stdout)";
|
|
27 char input_name[MAXPATHLEN] = "(stdin)";
|
|
28 char signature_name[MAXPATHLEN];
|
|
29
|
|
30 int c;
|
|
31 int row, col;
|
|
32
|
|
33 int n;
|
|
34
|
|
35 double alpha = 0.0;
|
|
36
|
|
37 int filter = 0;
|
|
38 int method = -1;
|
3
|
39 int level = 0;
|
0
|
40 char filter_name[MAXPATHLEN] = "";
|
|
41
|
|
42 int verbose = 0;
|
|
43
|
|
44 gray **image;
|
|
45 Image_tree dwts;
|
|
46
|
|
47 gray maxval;
|
|
48 int rows, cols, colors, format;
|
|
49
|
|
50 progname = argv[0];
|
|
51
|
|
52 pgm_init(&argc, argv); wm_init();
|
|
53
|
|
54 while ((c = getopt(argc, argv, "a:e:f:F:h?o:s:v:")) != EOF) {
|
|
55 switch (c) {
|
|
56 case 'a':
|
|
57 alpha = atof(optarg);
|
|
58 if (alpha <= 0.0) {
|
|
59 fprintf(stderr, "%s: alpha factor %f out of range\n", progname, alpha);
|
|
60 exit(1);
|
|
61 }
|
|
62 break;
|
|
63 case 'e':
|
|
64 method = atoi(optarg);
|
|
65 if (method < 0) {
|
|
66 fprintf(stderr, "%s: wavelet filtering method %d out of range\n", progname, method);
|
|
67 exit(1);
|
|
68 }
|
|
69 break;
|
|
70 case 'f':
|
|
71 filter = atoi(optarg);
|
|
72 if (filter <= 0) {
|
|
73 fprintf(stderr, "%s: filter number %d out of range\n", progname, filter);
|
|
74 exit(1);
|
|
75 }
|
|
76 break;
|
|
77 case 'F':
|
|
78 strcpy(filter_name, optarg);
|
|
79 break;
|
|
80 case 'h':
|
|
81 case '?':
|
|
82 usage();
|
|
83 break;
|
|
84 case 'o':
|
|
85 if ((out = fopen(optarg, "wb")) == NULL) {
|
|
86 fprintf(stderr, "%s: unable to open output file %s\n", progname, optarg);
|
|
87 exit(1);
|
|
88 }
|
|
89 strcpy(output_name, optarg);
|
|
90 break;
|
|
91 case 's':
|
|
92 if ((sig = fopen(optarg, "r")) == NULL) {
|
|
93 fprintf(stderr, "%s: unable to open signature file %s\n", progname, optarg);
|
|
94 exit(1);
|
|
95 }
|
|
96 strcpy(signature_name, optarg);
|
|
97 break;
|
|
98 case 'v':
|
|
99 verbose = atoi(optarg);
|
|
100 if (verbose < 0) {
|
|
101 fprintf(stderr, "%s: verbosity level %d out of range\n", progname, verbose);
|
|
102 exit(1);
|
|
103 }
|
|
104 break;
|
|
105 }
|
|
106 }
|
|
107
|
|
108 argc -= optind;
|
|
109 argv += optind;
|
|
110
|
|
111 if (argc > 1) {
|
|
112 usage();
|
|
113 exit(1);
|
|
114 }
|
|
115
|
|
116 if (argc == 1 && *argv[0] != '-')
|
|
117 if ((in = fopen(argv[0], "rb")) == NULL) {
|
|
118 fprintf(stderr, "%s: unable to open input file %s\n", progname, argv[0]);
|
|
119 exit(1);
|
|
120 }
|
|
121 else
|
|
122 strcpy(input_name, argv[0]);
|
|
123
|
|
124 if (sig) {
|
|
125 char line[32];
|
|
126 fgets(line, sizeof(line), sig);
|
|
127 if (strspn(line, "CVSG") >= 4) {
|
|
128 fscanf(sig, "%d\n", &n);
|
|
129 if (alpha == 0.0)
|
|
130 fscanf(sig, "%lf\n", &alpha);
|
|
131 else
|
|
132 fscanf(sig, "%*lf\n");
|
|
133 if (method < 0)
|
|
134 fscanf(sig, "%d\n", &method);
|
|
135 else
|
|
136 fscanf(sig, "%*d\n");
|
|
137 if (filter == 0)
|
|
138 fscanf(sig, "%d\n", &filter);
|
|
139 else
|
|
140 fscanf(sig, "%*d\n");
|
|
141 if (!strcmp(filter_name, ""))
|
|
142 fscanf(sig, "%[^\n\r]\n", &filter_name);
|
|
143 else
|
|
144 fscanf(sig, "%*[^\n\r]\n");
|
|
145 }
|
|
146 else {
|
|
147 fprintf(stderr, "%s: invalid signature file %s\n", progname, signature_name);
|
|
148 exit(1);
|
|
149 }
|
|
150 }
|
|
151 else {
|
|
152 fprintf(stderr, "%s: signature file not specified, use -s file option\n", progname);
|
|
153 exit(1);
|
|
154 }
|
|
155
|
|
156 pgm_readpgminit(in, &cols, &rows, &maxval, &format);
|
|
157
|
|
158 image = pgm_allocarray(cols, rows);
|
|
159
|
|
160 for (row = 0; row < rows; row++)
|
|
161 pgm_readpgmrow(in, image[row], cols, maxval, format);
|
|
162
|
|
163 fclose(in);
|
|
164
|
|
165 level = 0;
|
|
166 row = rows;
|
|
167 col = cols;
|
|
168 while (n < row * col / 4.0 && row >= 2 && col >= 2) {
|
|
169 row /= 2;
|
|
170 col /= 2;
|
|
171 level++;
|
|
172 }
|
|
173
|
|
174 if (verbose >= 2) {
|
|
175 fprintf(stderr, "%s: embedding into coarse image (x %d/y %d) at level %d\n", progname, col, row, level);
|
|
176 }
|
|
177
|
|
178 init_dwt(cols, rows, filter_name, filter, level, method);
|
|
179 #ifdef POLLEN_STUFF
|
3
|
180 #include "pollen_stuff.c"
|
0
|
181 #endif
|
|
182 #ifdef PARAM_STUFF
|
3
|
183 #include "param_stuff.c"
|
0
|
184 #endif
|
|
185
|
|
186 dwts = fdwt(image);
|
|
187
|
|
188 {
|
|
189 Image_tree p = dwts;
|
|
190 Image img;
|
|
191 double med;
|
|
192
|
|
193 while (!p->image)
|
|
194 p = p->coarse;
|
|
195
|
|
196 img = p->image;
|
|
197
|
|
198 med = 0.0;
|
|
199 for (row = 0; row < img->height; row++)
|
|
200 for (col = 0; col < img->width; col++)
|
|
201 med += get_pixel(img, col, row);
|
|
202
|
|
203 med /= (double) (img->height * img->width);
|
|
204
|
|
205 row = 0;
|
|
206 col = 0;
|
|
207 while (n > 0) {
|
|
208 double pix;
|
|
209 double g;
|
|
210
|
|
211 fscanf(sig, "%lf\n", &g);
|
|
212
|
|
213 pix = get_pixel(img, col, row);
|
|
214 pix = med + (pix - med) * (1.0 + alpha * g);
|
|
215 set_pixel(img, col, row, pix);
|
|
216
|
|
217 if (++col == img->width) { col = 0; row++; }
|
|
218 n--;
|
|
219 }
|
|
220 }
|
|
221
|
|
222 fclose(sig);
|
|
223
|
|
224 idwt(dwts, image);
|
|
225
|
|
226 pgm_writepgminit(out, cols, rows, maxval, 0);
|
|
227
|
|
228 for (row = 0; row < rows; row++)
|
|
229 pgm_writepgmrow(out, image[row], cols, maxval, 0);
|
|
230
|
|
231 fclose(out);
|
|
232
|
|
233 pgm_freearray(image, rows);
|
|
234
|
|
235 exit(0);
|
|
236 }
|