annotate Meerwald/wm_koch_e.c @ 1:647ab0f05aae v0.4

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

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