0
|
1 #include "wm.h"
|
|
2 #include "signature.h"
|
|
3 #include "dwt.h"
|
|
4 #include "pgm.h"
|
|
5
|
|
6 char *progname;
|
|
7
|
|
8 // watermarking transformation, set median pixel to quantization boundary
|
|
9 double wm_transform(double alpha, double f1, double f2, double f3, int x) {
|
|
10 double s = alpha * (fabs(f3) - fabs(f1)) / 2.0;
|
|
11 double l = x ? (f1 + s) : f1;
|
|
12 double f2new;
|
|
13
|
|
14 while ((l + 2 * s) < f2) l += 2 * s;
|
|
15 f2new = ((f2 - l) < (l + 2 * s - f2)) ? l : (l + 2 * s);
|
|
16
|
|
17 return f2new;
|
|
18 }
|
|
19
|
|
20 void usage(void) {
|
|
21 fprintf(stderr, "usage: %s [-a n] [-e n] [-f n] [-F n] [-h] [-l n] [-o file] [-v n] -s file file\n\n", progname);
|
|
22 fprintf(stderr, "\t-a n\t\tembedding strength (default 0.05)\n");
|
|
23 fprintf(stderr, "\t-e n\t\twavelet filtering method\n");
|
|
24 fprintf(stderr, "\t-f n\t\tfilter number\n");
|
|
25 fprintf(stderr, "\t-F file\t\tfilter definition file\n");
|
|
26 fprintf(stderr, "\t-l n\t\tembedding level\n");
|
|
27 fprintf(stderr, "\t-h\t\tprint usage\n");
|
|
28 fprintf(stderr, "\t-o file\t\toutput (watermarked) file\n");
|
|
29 fprintf(stderr, "\t-s file\t\tsignature to embed in input image\n");
|
|
30 fprintf(stderr, "\t-v n\t\tverbosity level\n");
|
|
31 exit(0);
|
|
32 }
|
|
33
|
|
34 int main(int argc, char *argv[]) {
|
|
35
|
|
36 FILE *in = stdin;
|
|
37 FILE *out = stdout;
|
|
38 FILE *sig = NULL;
|
|
39
|
|
40 char output_name[MAXPATHLEN] = "(stdout)";
|
|
41 char input_name[MAXPATHLEN] = "(stdin)";
|
|
42 char signature_name[MAXPATHLEN];
|
|
43
|
|
44 int c, n;
|
|
45 int row, col;
|
|
46
|
|
47 double alpha = 0.0;
|
|
48 int filter = 0;
|
|
49 int method = -1;
|
|
50 int level = 0;
|
|
51 char filter_name[MAXPATHLEN] = "";
|
|
52
|
|
53 int seed;
|
|
54 int verbose = 0;
|
|
55
|
|
56 gray **image;
|
|
57 Image_tree dwts, p;
|
|
58
|
|
59 gray maxval;
|
|
60 int rows, cols, colors, format;
|
|
61
|
|
62 progname = argv[0];
|
|
63
|
|
64 pgm_init(&argc, argv); wm_init();
|
|
65
|
|
66 while ((c = getopt(argc, argv, "a:e:f:F:h?l:o:s:v:")) != EOF) {
|
|
67 switch (c) {
|
|
68 case 'e':
|
|
69 method = atoi(optarg);
|
|
70 if (method < 0) {
|
|
71 fprintf(stderr, "%s: wavelet filtering method %d out of range\n", progname, method);
|
|
72 exit(1);
|
|
73 }
|
|
74 break;
|
|
75 case 'f':
|
|
76 filter = atoi(optarg);
|
|
77 if (filter <= 0) {
|
|
78 fprintf(stderr, "%s: filter number %d out of range\n", progname, filter);
|
|
79 exit(1);
|
|
80 }
|
|
81 break;
|
|
82 case 'F':
|
|
83 strcpy(filter_name, optarg);
|
|
84 break;
|
|
85 case 'h':
|
|
86 case '?':
|
|
87 usage();
|
|
88 break;
|
|
89 case 'l':
|
|
90 level = atoi(optarg);
|
|
91 if (level < 1) {
|
|
92 fprintf(stderr, "%s: embedding level out of range\n", progname);
|
|
93 exit(1);
|
|
94 }
|
|
95 break;
|
|
96 case 'o':
|
|
97 if ((out = fopen(optarg, "wb")) == NULL) {
|
|
98 fprintf(stderr, "%s: unable to open output file %s\n", progname, optarg);
|
|
99 exit(1);
|
|
100 }
|
|
101 strcpy(output_name, optarg);
|
|
102 break;
|
|
103 case 'a':
|
|
104 alpha = atof(optarg);
|
|
105 if (alpha <= 0.0) {
|
|
106 fprintf(stderr, "%s: embedding strength factor %f out of range\n", progname, alpha);
|
|
107 exit(1);
|
|
108 }
|
|
109 break;
|
|
110 case 's':
|
|
111 if ((sig = fopen(optarg, "r")) == NULL) {
|
|
112 fprintf(stderr, "%s: unable to open signature file %s\n", progname, optarg);
|
|
113 exit(1);
|
|
114 }
|
|
115 strcpy(signature_name, optarg);
|
|
116 break;
|
|
117 case 'v':
|
|
118 verbose = atoi(optarg);
|
|
119 if (verbose < 0) {
|
|
120 fprintf(stderr, "%s: verbosity level %d out of range\n", progname, verbose);
|
|
121 exit(1);
|
|
122 }
|
|
123 break;
|
|
124 }
|
|
125 }
|
|
126
|
|
127 argc -= optind;
|
|
128 argv += optind;
|
|
129
|
|
130 if (argc > 1) {
|
|
131 usage();
|
|
132 exit(1);
|
|
133 }
|
|
134
|
|
135 if (argc == 1 && *argv[0] != '-')
|
|
136 if ((in = fopen(argv[0], "rb")) == NULL) {
|
|
137 fprintf(stderr, "%s: unable to open input file %s\n", progname, argv[0]);
|
|
138 exit(1);
|
|
139 }
|
|
140 else
|
|
141 strcpy(input_name, argv[0]);
|
|
142
|
|
143 if (sig) {
|
|
144 char line[32];
|
|
145 fgets(line, sizeof(line), sig);
|
|
146 if (strspn(line, "XESG") >= 4) {
|
|
147 fscanf(sig, "%d\n", &nbit_signature);
|
|
148 if (alpha == 0.0)
|
|
149 fscanf(sig, "%lf\n", &alpha);
|
|
150 else
|
|
151 fscanf(sig, "%*lf\n");
|
|
152 if (method < 0)
|
|
153 fscanf(sig, "%d\n", &method);
|
|
154 else
|
|
155 fscanf(sig, "%*d\n");
|
|
156 if (filter == 0)
|
|
157 fscanf(sig, "%d\n", &filter);
|
|
158 else
|
|
159 fscanf(sig, "%*d\n");
|
|
160 if (!strcmp(filter_name, ""))
|
|
161 fscanf(sig, "%[^\n\r]\n", &filter_name);
|
|
162 else
|
|
163 fscanf(sig, "%*[^\n\r]\n");
|
|
164 if (level == 0)
|
|
165 fscanf(sig, "%d\n", &level);
|
|
166 else
|
|
167 fscanf(sig, "%*d\n");
|
|
168 fscanf(sig, "%d\n", &seed);
|
|
169 srandom(seed);
|
|
170 n_signature = NBITSTOBYTES(nbit_signature);
|
|
171 fread(signature, sizeof(char), n_signature, sig);
|
|
172 fscanf(sig, "\n");
|
|
173 }
|
|
174 else {
|
|
175 fprintf(stderr, "%s: invalid signature file %s\n", progname, signature_name);
|
|
176 exit(1);
|
|
177 }
|
|
178 fclose(sig);
|
|
179 }
|
|
180 else {
|
|
181 fprintf(stderr, "%s: signature file not specified, use -s file option\n", progname);
|
|
182 exit(1);
|
|
183 }
|
|
184
|
|
185 pgm_readpgminit(in, &cols, &rows, &maxval, &format);
|
|
186
|
|
187 if (verbose > 0)
|
|
188 fprintf(stderr, "%s: embedding %d bits with strength %f in\n"
|
|
189 " %d x %d host image, decomposition level %d\n",
|
|
190 progname, nbit_signature, alpha, cols, rows, level);
|
|
191
|
|
192 image = pgm_allocarray(cols, rows);
|
|
193
|
|
194 for (row = 0; row < rows; row++)
|
|
195 pgm_readpgmrow(in, image[row], cols, maxval, format);
|
|
196
|
|
197 fclose(in);
|
|
198
|
|
199 // decomposition of image
|
|
200 init_dwt(cols, rows, filter_name, filter, level, method);
|
|
201 #ifdef POLLEN_STUFF
|
3
|
202 #include "pollen_stuff.c"
|
0
|
203 #endif
|
|
204 #ifdef PARAM_STUFF
|
3
|
205 #include "param_stuff.c"
|
0
|
206 #endif
|
|
207
|
|
208 dwts = fdwt(image);
|
|
209
|
|
210 p = dwts;
|
|
211
|
|
212 // consider each resolution level
|
|
213 while (p->level < level)
|
|
214 // descend one level
|
|
215 p = p->coarse;
|
|
216
|
|
217 // repeat binary watermark by sliding a 3-pixel window of approximation image
|
|
218 n = 0;
|
|
219 for (row = 0; row < p->image->height; row++) {
|
|
220 for (col = 0; col < p->image->width - 3; col += 3) {
|
|
221 double b1, b2, b3;
|
|
222 double *f1 = &b1, *f2 = &b2, *f3 = &b3;
|
|
223 double f2new;
|
|
224
|
|
225 // get all three approximation pixels in window
|
|
226 b1 = get_pixel(p->image, col + 0, row);
|
|
227 b2 = get_pixel(p->image, col + 1, row);
|
|
228 b3 = get_pixel(p->image, col + 2, row);
|
|
229
|
|
230 // bring selected pixels in ascending order
|
|
231 #define SWAP(A, B) {double *t = A; A = B; B = t;}
|
|
232 if (*f1 > *f2) SWAP(f1, f2);
|
|
233 if (*f2 > *f3) SWAP(f2, f3);
|
|
234 if (*f1 > *f2) SWAP(f1, f2);
|
|
235
|
|
236 // apply watermarking transformation (modify median pixel)
|
|
237 f2new = wm_transform(alpha, *f1, *f2, *f3, get_signature_bit(n % nbit_signature));
|
|
238
|
|
239 if (verbose > 1)
|
|
240 fprintf(stderr, "%s: embedding #%d (= %d) at (%d/%d); %f < %f -> %f < %f\n",
|
|
241 progname, n, get_signature_bit(n % nbit_signature), col, row, *f1, *f2, f2new, *f3);
|
|
242
|
|
243 *f2 = f2new;
|
|
244
|
|
245 // write modified pixel
|
|
246 set_pixel(p->image, col + 0, row, b1);
|
|
247 set_pixel(p->image, col + 1, row, b2);
|
|
248 set_pixel(p->image, col + 2, row, b3);
|
|
249
|
|
250 n++;
|
|
251 }
|
|
252 }
|
|
253
|
|
254
|
|
255 // skip over some pixels
|
|
256
|
|
257 idwt(dwts, image);
|
|
258
|
|
259 pgm_writepgminit(out, cols, rows, maxval, 0);
|
|
260
|
|
261 for (row = 0; row < rows; row++)
|
|
262 pgm_writepgmrow(out, image[row], cols, maxval, 0);
|
|
263
|
|
264 fclose(out);
|
|
265
|
|
266 pgm_freearray(image, rows);
|
|
267
|
|
268 exit(0);
|
|
269 }
|