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