comparison Meerwald-dir/wm_kund3_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_kund3_e.c@ad1d224896c5
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 void usage(void) {
9 fprintf(stderr, "usage: %s [-e n] [-f n] [-F n] [-h] [-l n] [-o file] [-q n] [-v n] -s file file\n\n", progname);
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-l n\t\tembedding level\n");
14 fprintf(stderr, "\t-h\t\tprint usage\n");
15 fprintf(stderr, "\t-o file\t\toutput (watermarked) file\n");
16 fprintf(stderr, "\t-q n\t\tsignature strength (default 4)\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 int main(int argc, char *argv[]) {
23
24 FILE *in = stdin;
25 FILE *out = stdout;
26 FILE *sig = NULL;
27
28 char output_name[MAXPATHLEN] = "(stdout)";
29 char input_name[MAXPATHLEN] = "(stdin)";
30 char signature_name[MAXPATHLEN];
31
32 char *binstr;
33
34 int r, c, n;
35 int row, col;
36
37 int quality = 0;
38
39 int filter = 0;
40 int method = -1;
41 int level = 0;
42 char filter_name[MAXPATHLEN] = "";
43
44 int seed;
45 int verbose = 0;
46
47 gray **image;
48 Image_tree dwts, p;
49
50 gray maxval;
51 int rows, cols, format;
52
53 progname = argv[0];
54
55 pgm_init(&argc, argv);
56 wm_init();
57
58 while ((c = getopt(argc, argv, "e:f:F:h?l:o:q:s:v:")) != EOF) {
59 switch (c) {
60 case 'e':
61 method = atoi(optarg);
62 if (method < 0) {
63 fprintf(stderr, "%s: wavelet filtering method %d out of range\n", progname, method);
64 exit(1);
65 }
66 break;
67 case 'f':
68 filter = atoi(optarg);
69 if (filter <= 0) {
70 fprintf(stderr, "%s: filter number %d out of range\n", progname, filter);
71 exit(1);
72 }
73 break;
74 case 'F':
75 strcpy(filter_name, optarg);
76 break;
77 case 'h':
78 case '?':
79 usage();
80 break;
81 case 'l':
82 level = atoi(optarg);
83 if (level < 1) {
84 fprintf(stderr, "%s: embedding level out of range\n", progname);
85 exit(1);
86 }
87 break;
88 case 'o':
89 if ((out = fopen(optarg, "wb")) == NULL) {
90 fprintf(stderr, "%s: unable to open output file %s\n", progname, optarg);
91 exit(1);
92 }
93 strcpy(output_name, optarg);
94 break;
95 case 'q':
96 quality = atoi(optarg);
97 if (quality < 1) {
98 fprintf(stderr, "%s: quality factor %d out of range\n", progname, quality);
99 exit(1);
100 }
101 break;
102 case 's':
103 if ((sig = fopen(optarg, "r")) == NULL) {
104 fprintf(stderr, "%s: unable to open signature file %s\n", progname, optarg);
105 exit(1);
106 }
107 strcpy(signature_name, optarg);
108 break;
109 case 'v':
110 verbose = atoi(optarg);
111 if (verbose < 0) {
112 fprintf(stderr, "%s: verbosity level %d out of range\n", progname, verbose);
113 exit(1);
114 }
115 break;
116 }
117 }
118
119 argc -= optind;
120 argv += optind;
121
122 if (argc > 1) {
123 usage();
124 exit(1);
125 }
126
127 if (argc == 1 && *argv[0] != '-') {
128 if ((in = fopen(argv[0], "rb")) == NULL) {
129 fprintf(stderr, "%s: unable to open input file %s\n", progname, argv[0]);
130 exit(1);
131 }
132 else
133 strcpy(input_name, argv[0]);
134 }
135
136 if (sig) {
137 char line[32];
138 fgets(line, sizeof(line), sig);
139 if (strspn(line, "KD3SG") >= 5) {
140 fscanf(sig, "%d\n", &nbit_signature);
141 if (quality == 0)
142 fscanf(sig, "%d\n", &quality);
143 else
144 fscanf(sig, "%*d\n");
145 if (method < 0)
146 fscanf(sig, "%d\n", &method);
147 else
148 fscanf(sig, "%*d\n");
149 if (filter == 0)
150 fscanf(sig, "%d\n", &filter);
151 else
152 fscanf(sig, "%*d\n");
153 if (!strcmp(filter_name, ""))
154 fscanf(sig, "%[^\n\r]\n", filter_name);
155 else
156 fscanf(sig, "%*[^\n\r]\n");
157 if (level == 0)
158 fscanf(sig, "%d\n", &level);
159 else
160 fscanf(sig, "%*d\n");
161 fscanf(sig, "%d\n", &seed);
162 srandom(seed);
163 n_signature = NBITSTOBYTES(nbit_signature);
164 binstr = malloc((nbit_signature + 1) * sizeof(char));
165 fscanf(sig, "%[01]\n", binstr);
166 binstr_to_sig(binstr);
167 free(binstr);
168 }
169 else {
170 fprintf(stderr, "%s: invalid signature file %s\n", progname, signature_name);
171 exit(1);
172 }
173 fclose(sig);
174 }
175 else {
176 fprintf(stderr, "%s: signature file not specified, use -s file option\n", progname);
177 exit(1);
178 }
179
180 pgm_readpgminit(in, &cols, &rows, &maxval, &format);
181
182 if (verbose > 0)
183 fprintf(stderr, "%s: embedding %d bits with quality %d in\n"
184 " %d x %d host image, decomposition level %d\n",
185 progname, nbit_signature, quality, cols, rows, level);
186
187 image = pgm_allocarray(cols, rows);
188
189 for (row = 0; row < rows; row++)
190 pgm_readpgmrow(in, image[row], cols, maxval, format);
191
192 fclose(in);
193
194 // decomposition of image
195 init_dwt(cols, rows, filter_name, filter, level, method);
196 #ifdef POLLEN_STUFF
197 #include "pollen_stuff.c"
198 #endif
199 #ifdef PARAM_STUFF
200 #include "param_stuff.c"
201 #endif
202
203 dwts = fdwt(image);
204
205 p = dwts;
206
207 // consider each resolution level
208 while (p->coarse->level < level)
209 // descend one level
210 p = p->coarse;
211
212 // start to embed signature from beginning at each level
213 // get width and height of detail images at current level
214 {
215 int lwidth = p->vertical->image->width;
216 int lheight = p->vertical->image->height;
217
218 if (verbose > 1)
219 fprintf(stderr, "%s: embedding at level %d now, size %d x %d\n", progname,
220 p->coarse->level, lwidth, lheight);
221
222 #define STEP 3
223 n = 0;
224 // consider each coefficient at resolution level
225 for (row = 0; row < lwidth - STEP; row += STEP)
226 for (col = 0; col < lheight - STEP; col += STEP) {
227 double h, v, d;
228 double *f1 = &h, *f2 = &v, *f3 = &d;
229 double delta;
230
231 // key-dependant coefficient selection
232 r = row + 1+random() % (STEP-2);
233 c = col + 1+random() % (STEP-2);
234
235 // get coefficient values, one from each detail image
236 h = get_pixel(p->horizontal->image, c, r);
237 v = get_pixel(p->vertical->image, c, r);
238 d = get_pixel(p->diagonal->image, c, r);
239
240 // order pointer to coefficient values such that f1 <= f2 <= f3
241 #define SWAP(A, B) {double *t = A; A = B; B = t;}
242 if (*f1 > *f2) SWAP(f1, f2);
243 if (*f2 > *f3) SWAP(f2, f3);
244 if (*f1 > *f2) SWAP(f1, f2);
245
246 if (verbose > 2)
247 fprintf(stderr, "%s: embedding bit #%d (= %d) at (%d/%d),\n",
248 progname, n, get_signature_bit(n % nbit_signature), c, r);
249 if (verbose > 5)
250 fprintf(stderr, " h=%lf, v=%lf, d=%lf\n", h, v, d);
251 if (verbose > 2)
252 fprintf(stderr, " f1=%lf, f2=%lf", *f1, *f2);
253
254 #define MIN_DIFF 20.0
255 if (*f3 - *f1 < MIN_DIFF) {
256 double adj = (MIN_DIFF - (*f3 - *f1)) / 2.0;
257 *f1 -= adj;
258 *f3 += adj;
259 }
260
261 // calculate delta, the width of the bins
262 delta = (*f3 - *f1) / (double) (2 * quality - 1);
263
264 // set middle coefficient to closest appropriate bin,
265 // according to watermark bit
266 if (quality == 1)
267 *f2 = get_signature_bit(n % nbit_signature) ? *f3 : *f1;
268 else {
269 double l = get_signature_bit(n % nbit_signature) ? *f1 + delta : *f1;
270 while ((l + 2 * delta) < *f2) l += 2 * delta;
271 *f2 = (*f2 - l) < (l + 2 * delta - *f2) ? l : l + 2 * delta;
272 }
273
274 if (verbose > 2)
275 fprintf(stderr, " -> %lf, f3=%lf\n", *f2, *f3);
276
277 // write pixels, one of them modified
278 set_pixel(p->horizontal->image, c, r, h);
279 set_pixel(p->vertical->image, c, r, v);
280 set_pixel(p->diagonal->image, c, r, d);
281
282 n++;
283
284 }
285 }
286
287 idwt(dwts, image);
288
289 pgm_writepgminit(out, cols, rows, maxval, 0);
290
291 for (row = 0; row < rows; row++)
292 pgm_writepgmrow(out, image[row], cols, maxval, 0);
293
294 fclose(out);
295
296 pgm_freearray(image, rows);
297
298 exit(0);
299 }

Repositories maintained by Peter Meerwald, pmeerw@pmeerw.net.