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
|
8
|
53 int i, c;
|
|
54 int row;
|
0
|
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;
|
8
|
72 int rows, cols, format;
|
0
|
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
|
8
|
149 if (argc == 1 && *argv[0] != '-') {
|
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]);
|
8
|
156 }
|
|
157
|
0
|
158 if (sig) {
|
|
159 char line[32];
|
|
160 fgets(line, sizeof(line), sig);
|
|
161 if (strspn(line, "XASG") >= 4) {
|
|
162 fscanf(sig, "%d\n", &n);
|
|
163 if (alpha == 0.0)
|
|
164 fscanf(sig, "%lf\n", &alpha);
|
|
165 else
|
|
166 fscanf(sig, "%*lf\n");
|
|
167 if (level == 0)
|
|
168 fscanf(sig, "%d\n", &level);
|
|
169 else
|
|
170 fscanf(sig, "%*d\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 image = pgm_allocarray(cols, rows);
|
|
201 for (row = 0; row < rows; row++)
|
|
202 pgm_readpgmrow(in, image[row], cols, maxval, format);
|
|
203 fclose(in);
|
|
204
|
|
205 // complete decomposition
|
|
206 levels = find_deepest_level(cols, rows) - 1;
|
|
207 if (level > levels) {
|
|
208 fprintf(stderr, "%s: decomposition level %d not possible (max. %d), image size is %d x %d\n", progname, level, levels, cols, rows);
|
|
209 exit(1);
|
|
210 }
|
|
211
|
|
212 // wavelet transform
|
|
213 init_dwt(cols, rows, filter_name, filter, level, method);
|
|
214 #ifdef POLLEN_STUFF
|
3
|
215 #include "pollen_stuff.c"
|
0
|
216 #endif
|
|
217 #ifdef PARAM_STUFF
|
3
|
218 #include "param_stuff.c"
|
0
|
219 #endif
|
|
220
|
|
221 dwts = fdwt(image);
|
|
222
|
|
223 p = dwts;
|
|
224 while (p->coarse) {
|
|
225
|
|
226 mark_subband(p->horizontal, HORIZONTAL, alpha, watermark, n, verbose);
|
|
227 mark_subband(p->vertical, VERTICAL, alpha, watermark, n, verbose);
|
|
228 mark_subband(p->diagonal, DIAGONAL, alpha, watermark, n, verbose);
|
|
229
|
|
230 p = p->coarse;
|
|
231 }
|
|
232
|
|
233 free(watermark);
|
|
234 idwt(dwts, image);
|
|
235
|
|
236 pgm_writepgminit(out, cols, rows, maxval, 0);
|
|
237 for (row = 0; row < rows; row++)
|
|
238 pgm_writepgmrow(out, image[row], cols, maxval, 0);
|
|
239 fclose(out);
|
|
240
|
|
241 pgm_freearray(image, rows);
|
|
242
|
|
243 exit(0);
|
|
244 }
|