Mercurial > hg > wm
comparison Meerwald-dir/wm_xie_e.c @ 24:9f20bce6184e v0.7
move directories, support netpbm 11
author | Peter Meerwald-Stadler <pmeerw@pmeerw.net> |
---|---|
date | Fri, 20 Dec 2024 13:08:59 +0100 |
parents | Meerwald/wm_xie_e.c@4987db85cfae |
children |
comparison
equal
deleted
inserted
replaced
23:71dd4b96221b | 24:9f20bce6184e |
---|---|
1 #include "wm.h" | |
2 #include "signature.h" | |
3 #include "dwt.h" | |
4 #include "netpbm/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, 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 | |
144 if (sig) { | |
145 char line[32]; | |
146 fgets(line, sizeof(line), sig); | |
147 if (strspn(line, "XESG") >= 4) { | |
148 fscanf(sig, "%d\n", &nbit_signature); | |
149 if (alpha == 0.0) | |
150 fscanf(sig, "%lf\n", &alpha); | |
151 else | |
152 fscanf(sig, "%*f\n"); | |
153 if (method < 0) | |
154 fscanf(sig, "%d\n", &method); | |
155 else | |
156 fscanf(sig, "%*d\n"); | |
157 if (filter == 0) | |
158 fscanf(sig, "%d\n", &filter); | |
159 else | |
160 fscanf(sig, "%*d\n"); | |
161 if (!strcmp(filter_name, "")) | |
162 fscanf(sig, "%[^\n\r]\n", filter_name); | |
163 else | |
164 fscanf(sig, "%*[^\n\r]\n"); | |
165 if (level == 0) | |
166 fscanf(sig, "%d\n", &level); | |
167 else | |
168 fscanf(sig, "%*d\n"); | |
169 fscanf(sig, "%d\n", &seed); | |
170 srandom(seed); | |
171 n_signature = NBITSTOBYTES(nbit_signature); | |
172 fread(signature, sizeof(char), n_signature, sig); | |
173 fscanf(sig, "\n"); | |
174 } | |
175 else { | |
176 fprintf(stderr, "%s: invalid signature file %s\n", progname, signature_name); | |
177 exit(1); | |
178 } | |
179 fclose(sig); | |
180 } | |
181 else { | |
182 fprintf(stderr, "%s: signature file not specified, use -s file option\n", progname); | |
183 exit(1); | |
184 } | |
185 | |
186 pgm_readpgminit(in, &cols, &rows, &maxval, &format); | |
187 | |
188 if (verbose > 0) | |
189 fprintf(stderr, "%s: embedding %d bits with strength %f in\n" | |
190 " %d x %d host image, decomposition level %d\n", | |
191 progname, nbit_signature, alpha, cols, rows, level); | |
192 | |
193 image = pgm_allocarray(cols, rows); | |
194 | |
195 for (row = 0; row < rows; row++) | |
196 pgm_readpgmrow(in, image[row], cols, maxval, format); | |
197 | |
198 fclose(in); | |
199 | |
200 // decomposition of image | |
201 init_dwt(cols, rows, filter_name, filter, level, method); | |
202 #ifdef POLLEN_STUFF | |
203 #include "pollen_stuff.c" | |
204 #endif | |
205 #ifdef PARAM_STUFF | |
206 #include "param_stuff.c" | |
207 #endif | |
208 | |
209 dwts = fdwt(image); | |
210 | |
211 p = dwts; | |
212 | |
213 // consider each resolution level | |
214 while (p->level < level) | |
215 // descend one level | |
216 p = p->coarse; | |
217 | |
218 // repeat binary watermark by sliding a 3-pixel window of approximation image | |
219 n = 0; | |
220 for (row = 0; row < p->image->height; row++) { | |
221 for (col = 0; col < p->image->width - 3; col += 3) { | |
222 double b1, b2, b3; | |
223 double *f1 = &b1, *f2 = &b2, *f3 = &b3; | |
224 double f2new; | |
225 | |
226 // get all three approximation pixels in window | |
227 b1 = get_pixel(p->image, col + 0, row); | |
228 b2 = get_pixel(p->image, col + 1, row); | |
229 b3 = get_pixel(p->image, col + 2, row); | |
230 | |
231 // bring selected pixels in ascending order | |
232 #define SWAP(A, B) {double *t = A; A = B; B = t;} | |
233 if (*f1 > *f2) SWAP(f1, f2); | |
234 if (*f2 > *f3) SWAP(f2, f3); | |
235 if (*f1 > *f2) SWAP(f1, f2); | |
236 | |
237 // apply watermarking transformation (modify median pixel) | |
238 f2new = wm_transform(alpha, *f1, *f2, *f3, get_signature_bit(n % nbit_signature)); | |
239 | |
240 if (verbose > 1) | |
241 fprintf(stderr, "%s: embedding #%d (= %d) at (%d/%d); %f < %f -> %f < %f\n", | |
242 progname, n, get_signature_bit(n % nbit_signature), col, row, *f1, *f2, f2new, *f3); | |
243 | |
244 *f2 = f2new; | |
245 | |
246 // write modified pixel | |
247 set_pixel(p->image, col + 0, row, b1); | |
248 set_pixel(p->image, col + 1, row, b2); | |
249 set_pixel(p->image, col + 2, row, b3); | |
250 | |
251 n++; | |
252 } | |
253 } | |
254 | |
255 | |
256 // skip over some pixels | |
257 | |
258 idwt(dwts, image); | |
259 | |
260 pgm_writepgminit(out, cols, rows, maxval, 0); | |
261 | |
262 for (row = 0; row < rows; row++) | |
263 pgm_writepgmrow(out, image[row], cols, maxval, 0); | |
264 | |
265 fclose(out); | |
266 | |
267 pgm_freearray(image, rows); | |
268 | |
269 exit(0); | |
270 } |