annotate Meerwald/wm_koch_e.c @ 17:824d192e5614

bug fix; mse minimization still missing
author pmeerw@pan
date Thu, 29 Jan 2009 18:28:05 +0100
parents 4987db85cfae
children 3bdb67e76858
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
1 #include "wm.h"
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
2 #include "dct.h"
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
3 #include "signature.h"
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
4 #include "coord.h"
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
5 #include "pgm.h"
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
6
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
7 char *progname;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
8
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
9 void usage(void) {
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
10 fprintf(stderr, "usage: %s [-h] [-l n] [-o file] [-q n] [-v n] -s file file\n", progname);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
11 fprintf(stderr, "\t-h\t\tprint usage\n");
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
12 fprintf(stderr, "\t-l n\t\tsignature robustness factor\n");
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
13 fprintf(stderr, "\t-o file\t\toutput (watermarked) file\n");
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
14 fprintf(stderr, "\t-q n\t\tquantization (JPEG quality) factor\n");
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
15 fprintf(stderr, "\t-s file\t\tsignature to embed in input image\n");
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
16 fprintf(stderr, "\t-v n\t\tverbosity level\n");
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
17 exit(0);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
18 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
19
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
20 int main(int argc, char *argv[]) {
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
21
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
22 FILE *in = stdin;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
23 FILE *out = stdout;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
24 FILE *sig = NULL;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
25
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
26 char signature_name[MAXPATHLEN];
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
27 char input_name[MAXPATHLEN] = "(stdin)";
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
28 char output_name[MAXPATHLEN] = "(stdout)";
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
29
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
30 int c;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
31 int n;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
32
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
33 int seed;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
34 int verbose = 0;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
35
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
36 int rows, cols, format;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
37 gray maxval;
8
f83ef905a63d fixing many warnings
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 0
diff changeset
38 int row;
0
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
39
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
40 int quantization = 0;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
41 double quality = 0.0;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
42
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
43 struct coords *coords;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
44
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
45 gray **image;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
46 double **dcts;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
47
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
48 progname = argv[0];
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
49
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
50 pgm_init(&argc, argv); wm_init();
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
51
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
52 while ((c = getopt(argc, argv, "h?i:l:o:q:s:v:")) != EOF) {
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
53 switch (c) {
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
54 case 'h':
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
55 case '?':
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
56 usage();
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
57 break;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
58 case 'l':
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
59 quality = atof(optarg);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
60 if (quality <= 0.0) {
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
61 fprintf(stderr, "%s: signature strength factor %f out of range\n", progname, quality);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
62 exit(1);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
63 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
64 break;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
65 case 'o':
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
66 if ((out = fopen(optarg, "wb")) == NULL) {
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
67 fprintf(stderr, "%s: unable to open output file %s\n", progname, optarg);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
68 exit(1);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
69 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
70 strcpy(output_name, optarg);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
71 break;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
72 case 'q':
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
73 quantization = atoi(optarg);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
74 if (quantization <= 0 || quantization > 100) {
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
75 fprintf(stderr, "%s: quantization factor %d out of range\n", progname, quantization);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
76 exit(1);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
77 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
78 break;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
79 case 's':
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
80 if ((sig = fopen(optarg, "r")) == NULL) {
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
81 fprintf(stderr, "%s: unable to open signature file %s\n", progname, optarg);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
82 exit(1);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
83 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
84 strcpy(signature_name, optarg);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
85 break;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
86 case 'v':
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
87 verbose = atoi(optarg);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
88 if (verbose < 0) {
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
89 fprintf(stderr, "%s: verbosity level %d out of range\n", progname, verbose);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
90 exit(1);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
91 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
92 break;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
93 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
94 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
95
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
96 argc -= optind;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
97 argv += optind;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
98
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
99 if (argc > 1) {
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
100 usage();
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
101 exit(1);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
102 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
103
8
f83ef905a63d fixing many warnings
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 0
diff changeset
104 if (argc == 1 && *argv[0] != '-') {
0
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
105 if ((in = fopen(argv[0], "rb")) == NULL) {
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
106 fprintf(stderr, "%s: unable to open input file %s\n", progname, argv[0]);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
107 exit(1);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
108 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
109 else
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
110 strcpy(input_name, argv[0]);
8
f83ef905a63d fixing many warnings
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 0
diff changeset
111 }
0
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
112
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
113 if (sig) {
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
114 char line[128];
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
115 fgets(line, sizeof(line), sig);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
116 if (strspn(line, "KCSG") >= 4) {
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
117 fscanf(sig, "%d\n", &nbit_signature);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
118 if (quality == 0.0)
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
119 fscanf(sig, "%lf\n", &quality);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
120 else
16
4987db85cfae fix another scanf() warning
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 8
diff changeset
121 fscanf(sig, "%*f\n");
0
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
122 if (quantization == 0)
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
123 fscanf(sig, "%d\n", &quantization);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
124 else
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
125 fscanf(sig, "%*d\n");
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
126 fscanf(sig, "%d\n", &seed);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
127 n_signature = NBITSTOBYTES(nbit_signature);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
128 fread(signature, sizeof(char), n_signature, sig);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
129 fscanf(sig, "\n");
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
130 srandom(seed);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
131 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
132 else {
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
133 fprintf(stderr, "%s: invalid signature file %s\n", progname, signature_name);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
134 exit(1);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
135 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
136 fclose(sig);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
137 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
138 else {
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
139 fprintf(stderr, "%s: signature file not specified, use -s file option\n", progname);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
140 exit(1);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
141 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
142
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
143 pgm_readpgminit(in, &cols, &rows, &maxval, &format);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
144
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
145 if (cols % NJPEG) {
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
146 fprintf(stderr, "%s: image width %d not a multiple of %d\n", progname, cols, NJPEG);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
147 exit(1);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
148 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
149
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
150 if (rows % NJPEG) {
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
151 fprintf(stderr, "%s: image height %d not a multiple of %d\n", progname, rows, NJPEG);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
152 exit(1);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
153 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
154
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
155 if ((cols * rows) / (NJPEG * NJPEG) < nbit_signature) {
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
156 fprintf(stderr, "%s: image not large enough to embed %d bits of signature\n", progname, nbit_signature);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
157 exit(1);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
158 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
159
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
160 init_dct_8x8();
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
161 init_quantum_JPEG_lumin(quantization);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
162
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
163 dcts = alloc_coeffs_8x8();
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
164
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
165 if ((coords = alloc_coords(nbit_signature)) == NULL) {
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
166 fprintf(stderr, "%s: unable to allocate memory\n", progname);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
167 exit(1);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
168 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
169
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
170 image = pgm_allocarray(cols, rows);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
171
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
172 for (row = 0; row < rows; row++)
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
173 pgm_readpgmrow(in, image[row], cols, maxval, format);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
174
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
175 fclose(in);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
176
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
177 // embedding signature bits by modifying two coefficient relationship,
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
178 // one bit for each block
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
179 n = 0;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
180 while (n < nbit_signature) {
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
181 int xb;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
182 int yb;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
183 int c1, c2;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
184 double v1, v2;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
185 double w1, w2;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
186 double diff, abs_diff;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
187
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
188 // randomly select a block, check to get distinct blocks
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
189 // (don't watermark a block twice)
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
190 do {
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
191 xb = random() % (cols / NJPEG);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
192 yb = random() % (rows / NJPEG);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
193 } while (add_coord(coords, xb, yb) < 0);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
194
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
195 // do the forward 8x8 DCT of that block
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
196 fdct_block_8x8(image, xb * NJPEG, yb * NJPEG, dcts);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
197
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
198 // randomly select two distinct coefficients from block
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
199 // only accept coefficients in the middle frequency range
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
200 do {
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
201 c1 = (random() % (NJPEG * NJPEG - 2)) + 1;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
202 c2 = (random() % (NJPEG * NJPEG - 2)) + 1;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
203 } while (c1 == c2 || !is_middle_frequency_coeff_8x8(c1) || !is_middle_frequency_coeff_8x8(c2));
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
204
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
205 // quantize block according to quantization quality parameter
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
206 quantize_8x8(dcts);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
207
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
208 if (verbose > 0)
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
209 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");
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
210 if (verbose > 5)
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
211 print_coeffs_8x8(dcts);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
212
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
213 v1 = dcts[c1 / NJPEG][c1 % NJPEG];
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
214 v2 = dcts[c2 / NJPEG][c2 % NJPEG];
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
215
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
216 diff = fabs(v1) - fabs(v2);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
217 abs_diff = (fabs(diff) + quality) / 2.0;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
218
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
219 // modify coefficient's relationship to embed signature bit
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
220 // using mean square error to minimize error
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
221 if (get_signature_bit(n)) {
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
222 if (diff < quality) {
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
223 // we have to impose the relationship, does not occur naturally
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
224 w1 = (v1 > 0.0) ? (v1 + abs_diff) : (v1 - abs_diff);
17
824d192e5614 bug fix; mse minimization still missing
pmeerw@pan
parents: 16
diff changeset
225 w2 = v2;
0
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
226 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
227 else {
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
228 w1 = v1;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
229 w2 = v2;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
230 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
231 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
232 else {
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
233 if (diff > -quality) {
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
234 // force the relationship
17
824d192e5614 bug fix; mse minimization still missing
pmeerw@pan
parents: 16
diff changeset
235 w1 = v1;
0
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
236 w2 = (v2 > 0.0) ? (v2 + abs_diff) : (v2 - abs_diff);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
237 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
238 else {
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
239 w1 = v1;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
240 w2 = v2;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
241 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
242 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
243
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
244 if (verbose > 1)
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
245 fprintf(stderr, " %f -> %f, %f -> %f\n", v1, w1, v2, w2);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
246
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
247 // put the changed coefficients back to black
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
248 dcts[c1 / NJPEG][c1 % NJPEG] = w1;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
249 dcts[c2 / NJPEG][c2 % NJPEG] = w2;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
250
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
251 // the obvious :-)
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
252 dequantize_8x8(dcts);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
253
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
254 // do the inverse DCT on the modified 8x8 block
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
255 idct_block_8x8(dcts, image, xb * NJPEG, yb * NJPEG);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
256
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
257 n++;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
258 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
259
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
260 free_coeffs(dcts);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
261
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
262 pgm_writepgminit(out, cols, rows, maxval, 0);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
263 for (row = 0; row < rows; row++)
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
264 pgm_writepgmrow(out, image[row], cols, maxval, 0);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
265
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
266 fclose(out);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
267
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
268 pgm_freearray(image, rows);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
269
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
270 exit(0);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
271 }

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