comparison Meerwald-dir/wm_koch_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_koch_e.c@3bdb67e76858
children
comparison
equal deleted inserted replaced
23:71dd4b96221b 24:9f20bce6184e
1 #include "wm.h"
2 #include "dct.h"
3 #include "signature.h"
4 #include "coord.h"
5 #include "gray.h"
6 #include "netpbm/pgm.h"
7
8 char *progname;
9
10 double sign(double x) {
11 if (x >= 0.0) return 1.0;
12 else return -1.0;
13 }
14
15 double try_modif(gray **image_block, double **dcts, int c1, int c2, double w1, double w2) {
16 int i, j;
17 gray **altered_block;
18 double **altered_dcts;
19 double sum;
20
21 altered_block = alloc_grays_8x8();
22 altered_dcts = alloc_coeffs_8x8();
23
24 for (i = 0; i < 8; i++) {
25 memcpy(altered_dcts[i], dcts[i], sizeof(double) * 8);
26 }
27
28 // put the changed coefficients back to black
29 altered_dcts[c1 / NJPEG][c1 % NJPEG] = w1;
30 altered_dcts[c2 / NJPEG][c2 % NJPEG] = w2;
31
32 dequantize_8x8(altered_dcts);
33
34 idct_block_8x8(altered_dcts, altered_block, 0, 0);
35
36 // compute MSE
37 sum = 0.0;
38 for (i = 0; i < 8; i++) {
39 for (j = 0; j < 8; j++) {
40 double ib = image_block[i][j];
41 double ab = altered_block[i][j];
42 sum += (ib - ab) * (ib - ab);
43 }
44 }
45 sum /= 64.0;
46
47 free(altered_block);
48 free(altered_dcts);
49
50 return sum;
51 }
52
53 void usage(void) {
54 fprintf(stderr, "usage: %s [-h] [-l n] [-o file] [-q n] [-v n] -s file file\n", progname);
55 fprintf(stderr, "\t-h\t\tprint usage\n");
56 fprintf(stderr, "\t-l n\t\tsignature robustness factor\n");
57 fprintf(stderr, "\t-o file\t\toutput (watermarked) file\n");
58 fprintf(stderr, "\t-q n\t\tquantization (JPEG quality) factor\n");
59 fprintf(stderr, "\t-s file\t\tsignature to embed in input image\n");
60 fprintf(stderr, "\t-v n\t\tverbosity level\n");
61 exit(0);
62 }
63
64 int main(int argc, char *argv[]) {
65
66 FILE *in = stdin;
67 FILE *out = stdout;
68 FILE *sig = NULL;
69
70 char signature_name[MAXPATHLEN];
71 char input_name[MAXPATHLEN] = "(stdin)";
72 char output_name[MAXPATHLEN] = "(stdout)";
73
74 int c;
75 int n;
76
77 int seed;
78 int verbose = 0;
79
80 int rows, cols, format;
81 gray maxval;
82 int row;
83
84 int quantization = 0;
85 double quality = 0.0;
86
87 struct coords *coords;
88
89 gray **image;
90 double **dcts;
91 gray **image_block;
92
93 progname = argv[0];
94
95 pgm_init(&argc, argv); wm_init();
96
97 while ((c = getopt(argc, argv, "h?i:l:o:q:s:v:")) != EOF) {
98 switch (c) {
99 case 'h':
100 case '?':
101 usage();
102 break;
103 case 'l':
104 quality = atof(optarg);
105 if (quality <= 0.0) {
106 fprintf(stderr, "%s: signature strength factor %f out of range\n", progname, quality);
107 exit(1);
108 }
109 break;
110 case 'o':
111 if ((out = fopen(optarg, "wb")) == NULL) {
112 fprintf(stderr, "%s: unable to open output file %s\n", progname, optarg);
113 exit(1);
114 }
115 strcpy(output_name, optarg);
116 break;
117 case 'q':
118 quantization = atoi(optarg);
119 if (quantization <= 0 || quantization > 100) {
120 fprintf(stderr, "%s: quantization factor %d out of range\n", progname, quantization);
121 exit(1);
122 }
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
158 if (sig) {
159 char line[128];
160 fgets(line, sizeof(line), sig);
161 if (strspn(line, "KCSG") >= 4) {
162 fscanf(sig, "%d\n", &nbit_signature);
163 if (quality == 0.0)
164 fscanf(sig, "%lf\n", &quality);
165 else
166 fscanf(sig, "%*f\n");
167 if (quantization == 0)
168 fscanf(sig, "%d\n", &quantization);
169 else
170 fscanf(sig, "%*d\n");
171 fscanf(sig, "%d\n", &seed);
172 n_signature = NBITSTOBYTES(nbit_signature);
173 fread(signature, sizeof(char), n_signature, sig);
174 fscanf(sig, "\n");
175 srandom(seed);
176 }
177 else {
178 fprintf(stderr, "%s: invalid signature file %s\n", progname, signature_name);
179 exit(1);
180 }
181 fclose(sig);
182 }
183 else {
184 fprintf(stderr, "%s: signature file not specified, use -s file option\n", progname);
185 exit(1);
186 }
187
188 pgm_readpgminit(in, &cols, &rows, &maxval, &format);
189
190 if (cols % NJPEG) {
191 fprintf(stderr, "%s: image width %d not a multiple of %d\n", progname, cols, NJPEG);
192 exit(1);
193 }
194
195 if (rows % NJPEG) {
196 fprintf(stderr, "%s: image height %d not a multiple of %d\n", progname, rows, NJPEG);
197 exit(1);
198 }
199
200 if ((cols * rows) / (NJPEG * NJPEG) < nbit_signature) {
201 fprintf(stderr, "%s: image not large enough to embed %d bits of signature\n", progname, nbit_signature);
202 exit(1);
203 }
204
205 init_dct_8x8();
206 init_quantum_JPEG_lumin(quantization);
207
208 dcts = alloc_coeffs_8x8();
209 image_block = alloc_grays_8x8();
210
211 if ((coords = alloc_coords(nbit_signature)) == NULL) {
212 fprintf(stderr, "%s: unable to allocate memory\n", progname);
213 exit(1);
214 }
215
216 image = pgm_allocarray(cols, rows);
217
218 for (row = 0; row < rows; row++)
219 pgm_readpgmrow(in, image[row], cols, maxval, format);
220
221 fclose(in);
222
223 // embedding signature bits by modifying two coefficient relationship,
224 // one bit for each block
225 n = 0;
226 while (n < nbit_signature) {
227 int xb;
228 int yb;
229 int c1, c2;
230 double v1, v2;
231 double w1, w2;
232 double best_w1, best_w2;
233 double diff;
234 double mod;
235 double try;
236 double best_mse;
237 int no_mse_opt = 0;
238
239 // randomly select a block, check to get distinct blocks
240 // (don't watermark a block twice)
241 do {
242 xb = random() % (cols / NJPEG);
243 yb = random() % (rows / NJPEG);
244 } while (add_coord(coords, xb, yb) < 0);
245
246 // do the forward 8x8 DCT of that block
247 fdct_block_8x8(image, xb * NJPEG, yb * NJPEG, dcts);
248
249 copy_grays_to_block(image_block, image, xb*NJPEG, yb*NJPEG, NJPEG, NJPEG);
250
251 // randomly select two distinct coefficients from block
252 // only accept coefficients in the middle frequency range
253 do {
254 c1 = (random() % (NJPEG * NJPEG - 2)) + 1;
255 c2 = (random() % (NJPEG * NJPEG - 2)) + 1;
256 } while (c1 == c2 || !is_middle_frequency_coeff_8x8(c1) || !is_middle_frequency_coeff_8x8(c2));
257
258 // quantize block according to quantization quality parameter
259 quantize_8x8(dcts);
260
261 if (verbose > 0)
262 fprintf(stderr, "%d: quantized DCT block (x %d/y %d), modifying (x %d/y %d), (x %d/y %d) for %s\n", n, xb * NJPEG, yb * NJPEG, c1 % NJPEG, c1 / NJPEG, c2 % NJPEG, c2 / NJPEG, get_signature_bit(n) ? "HIGH" : "LOW");
263 if (verbose > 5)
264 print_coeffs_8x8(dcts);
265
266 v1 = dcts[c1 / NJPEG][c1 % NJPEG];
267 v2 = dcts[c2 / NJPEG][c2 % NJPEG];
268
269 best_w1 = DBL_MAX, best_w2 = DBL_MAX;
270 try = 0.0;
271 best_mse = DBL_MAX;
272
273 diff = fabs(v1) - fabs(v2);
274
275 if (get_signature_bit(n))
276 mod = fabs(quality - ( fabs(v1) - fabs(v2) ));
277 else
278 mod = fabs(quality - (fabs(v2) - fabs(v1)));
279
280 if (verbose > 2)
281 fprintf(stderr, "%d / %d: %.2f %.2f %.2f | %d\n", xb, yb, diff, v1, v2, get_signature_bit(n));
282
283 while (try <= mod) {
284 w1 = v1;
285 w2 = v2;
286
287 // modify coefficient's relationship to embed signature bit
288 // using mean square error to minimize error
289 if (get_signature_bit(n)) {
290 if (diff < quality) {
291 // we have to impose the relationship, does not occur naturally
292 w1 = sign(v1)*(fabs(v1) + mod - try);
293 w2 = sign(v2)*(fabs(v2) - try);
294 }
295 }
296 else {
297 if (diff > -quality) {
298 // force the relationship
299 w2 = sign(v2)*(fabs(v2) + mod - try);
300 w1 = sign(v1)*(fabs(v1) - try);
301 }
302 }
303
304 double mse = try_modif(image_block, dcts, c1, c2, w1, w2);
305 if (mse < best_mse) {
306 best_w1 = w1;
307 best_w2 = w2;
308 best_mse = mse;
309 }
310
311 if (verbose > 2)
312 fprintf(stderr, "%d / %d: MSE %.2f %.2f; %.2f: %.2f %.2f\n", xb, yb, mse, best_mse, try, w1, w2);
313
314 if (fabs(mse) == 1e-3)
315 break;
316
317 if (fabs(fabs(w1) - fabs(w2) + quality) > 1e-3)
318 break;
319
320 if (no_mse_opt)
321 break;
322
323 try += 0.05;
324 }
325
326 if (verbose > 1)
327 fprintf(stderr, " %f -> %f, %f -> %f\n", v1, best_w1, v2, best_w2);
328
329 // put the changed coefficients back to black
330 dcts[c1 / NJPEG][c1 % NJPEG] = best_w1;
331 dcts[c2 / NJPEG][c2 % NJPEG] = best_w2;
332
333 // the obvious :-)
334 dequantize_8x8(dcts);
335
336 // do the inverse DCT on the modified 8x8 block
337 idct_block_8x8(dcts, image, xb * NJPEG, yb * NJPEG);
338
339 n++;
340 }
341
342 free_grays(image_block);
343 free_coeffs(dcts);
344
345 pgm_writepgminit(out, cols, rows, maxval, 0);
346 for (row = 0; row < rows; row++)
347 pgm_writepgmrow(out, image[row], cols, maxval, 0);
348
349 fclose(out);
350
351 pgm_freearray(image, rows);
352
353 exit(0);
354 }

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