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