Mercurial > hg > wm
annotate Meerwald/wm_koch_d.c @ 18:3bdb67e76858
mse opt.
author | Peter Meerwald <pmeerw@cosy.sbg.ac.at> |
---|---|
date | Fri, 30 Jan 2009 12:46:49 +0100 |
parents | 4987db85cfae |
children |
rev | line source |
---|---|
0 | 1 #include "wm.h" |
2 #include "dct.h" | |
3 #include "signature.h" | |
4 #include "coord.h" | |
5 #include "pgm.h" | |
6 | |
7 char *progname; | |
8 | |
9 void usage(void) { | |
10 fprintf(stderr, "usage: %s [-h] [-l n] [-o file] [-q n] [-v n] -s file file\n\n", progname); | |
11 fprintf(stderr, "\t-h\t\tprint usage\n"); | |
12 fprintf(stderr, "\t-l n\t\tsignature robustness factor\n"); | |
13 fprintf(stderr, "\t-o file\t\textracted signature file\n"); | |
14 fprintf(stderr, "\t-q n\t\tquantization (JPEG quality) factor\n"); | |
15 fprintf(stderr, "\t-s file\t\toriginal signature file\n"); | |
16 fprintf(stderr, "\t-v n\t\tverbosity level\n"); | |
17 exit(0); | |
18 } | |
19 | |
20 int main(int argc, char *argv[]) { | |
21 | |
22 FILE *in = stdin; | |
23 FILE *out = stdout; | |
24 FILE *sig = NULL; | |
25 | |
26 gray **image; | |
27 struct coords *coords; | |
28 | |
29 char signature_name[MAXPATHLEN]; | |
30 char input_name[MAXPATHLEN] = "(stdin)"; | |
31 char output_name[MAXPATHLEN] = "(stdout)"; | |
32 | |
33 int c; | |
34 int n; | |
35 | |
36 int rows, cols, format; | |
37 gray maxval; | |
8 | 38 int row; |
0 | 39 |
40 int seed; | |
41 int verbose = 0; | |
42 | |
43 double quality = 0.0; | |
44 int quantization = 0; | |
45 | |
46 double **dcts; | |
47 | |
48 progname = argv[0]; | |
49 | |
50 pgm_init(&argc, argv); wm_init(); | |
51 | |
52 while ((c = getopt(argc, argv, "h?i:l:o:q:s:v:")) != EOF) { | |
53 switch (c) { | |
54 case 'h': | |
55 case '?': | |
56 usage(); | |
57 break; | |
58 case 'l': | |
59 quality = atof(optarg); | |
60 if (quality <= 0.0) { | |
61 fprintf(stderr, "%s: signature strength factor %f out of range\n", progname, quality); | |
62 exit(1); | |
63 } | |
64 break; | |
65 case 'o': | |
66 if ((out = fopen(optarg, "wb")) == NULL) { | |
67 fprintf(stderr, "%s: unable to open output file %s\n", progname, optarg); | |
68 exit(1); | |
69 } | |
70 strcpy(output_name, optarg); | |
71 break; | |
72 case 'q': | |
73 quantization = atoi(optarg); | |
74 if (quantization <= 0 || quantization > 100) { | |
75 fprintf(stderr, "%s: quantization factor %d out of range\n", progname, quantization); | |
76 exit(1); | |
77 } | |
78 break; | |
79 case 's': | |
80 if ((sig = fopen(optarg, "r")) == NULL) { | |
81 fprintf(stderr, "%s: unable to open signature file %s\n", progname, optarg); | |
82 exit(1); | |
83 } | |
84 strcpy(signature_name, optarg); | |
85 break; | |
86 case 'v': | |
87 verbose = atoi(optarg); | |
88 if (verbose < 0) { | |
89 fprintf(stderr, "%s: verbosity level %d out of range\n", progname, verbose); | |
90 exit(1); | |
91 } | |
92 break; | |
93 } | |
94 } | |
95 | |
96 argc -= optind; | |
97 argv += optind; | |
98 | |
99 if (argc > 1) { | |
100 usage(); | |
101 exit(1); | |
102 } | |
103 | |
8 | 104 if (argc == 1 && *argv[0] != '-') { |
0 | 105 if ((in = fopen(argv[0], "rb")) == NULL) { |
106 fprintf(stderr, "%s: unable to open input file %s\n", progname, argv[0]); | |
107 exit(1); | |
108 } | |
109 else | |
110 strcpy(input_name, argv[0]); | |
8 | 111 } |
112 | |
0 | 113 if (sig) { |
114 char line[128]; | |
115 fgets(line, sizeof(line), sig); | |
116 if (strspn(line, "KCSG") >= 4) { | |
117 fscanf(sig, "%d\n", &nbit_signature); | |
118 n_signature = NBITSTOBYTES(nbit_signature); | |
119 if (quality == 0.0) | |
120 fscanf(sig, "%lf\n", &quality); | |
121 else | |
16
4987db85cfae
fix another scanf() warning
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
8
diff
changeset
|
122 fscanf(sig, "%*f\n"); |
0 | 123 if (quantization == 0) |
124 fscanf(sig, "%d\n", &quantization); | |
125 else | |
126 fscanf(sig, "%*d\n"); | |
127 fscanf(sig, "%d\n", &seed); | |
128 srandom(seed); | |
129 fread(signature, sizeof(char), n_signature, sig); | |
130 init_signature_bits(); | |
131 fscanf(sig, "\n"); | |
132 } | |
133 else { | |
134 fprintf(stderr, "%s: invalid signature file %s\n", progname, signature_name); | |
135 exit(1); | |
136 } | |
137 fclose(sig); | |
138 } | |
139 else { | |
140 fprintf(stderr, "%s: signature file not specified, use -s file option\n", progname); | |
141 exit(1); | |
142 } | |
143 | |
144 pgm_readpgminit(in, &cols, &rows, &maxval, &format); | |
145 | |
146 if (cols % NJPEG) { | |
147 fprintf(stderr, "%s: image width %d not a multiple of %d\n", progname, cols, NJPEG); | |
148 exit(1); | |
149 } | |
150 | |
151 if (rows % NJPEG) { | |
152 fprintf(stderr, "%s: image height %d not a multiple of %d\n", progname, rows, NJPEG); | |
153 exit(1); | |
154 } | |
155 | |
156 if ((rows * cols) / (NJPEG * NJPEG) < nbit_signature) { | |
157 fprintf(stderr, "%s: image too small to extract %d bits of signature\n", progname, nbit_signature); | |
158 exit(1); | |
159 } | |
160 | |
161 init_dct_8x8(); | |
162 init_quantum_JPEG_lumin(quantization); | |
163 | |
164 dcts = alloc_coeffs_8x8(); | |
165 | |
166 if ((coords = alloc_coords(nbit_signature)) == NULL) { | |
167 fprintf(stderr, "%s: unable to allocate memory\n", progname); | |
168 exit(1); | |
169 } | |
170 | |
171 image = pgm_allocarray(cols, rows); | |
172 | |
173 for (row = 0; row < rows; row++) | |
174 pgm_readpgmrow(in, image[row], cols, maxval, format); | |
175 | |
176 fclose(in); | |
177 | |
178 n = 0; | |
179 while (n < nbit_signature) { | |
180 int xb; | |
181 int yb; | |
182 int c1, c2; | |
183 double v1, v2; | |
184 | |
185 do { | |
186 xb = random() % (cols / NJPEG); | |
187 yb = random() % (rows / NJPEG); | |
188 } while (add_coord(coords, xb, yb) < 0); | |
189 | |
190 fdct_block_8x8(image, xb * NJPEG, yb * NJPEG, dcts); | |
191 | |
192 do { | |
193 c1 = (random() % (NJPEG * NJPEG - 2)) + 1; | |
194 c2 = (random() % (NJPEG * NJPEG - 2)) + 1; | |
195 } while (c1 == c2 || !is_middle_frequency_coeff_8x8(c1) || !is_middle_frequency_coeff_8x8(c2)); | |
196 | |
197 quantize_8x8(dcts); | |
198 | |
199 if (verbose >= 1) | |
200 fprintf(stderr, "%d: quantized DCT block (x %d/y %d), extracting (x %d/y %d), (x %d/y %d) ", n, xb * NJPEG, yb * NJPEG, c1 % NJPEG, c1 / NJPEG, c2 % NJPEG, c2 / NJPEG); | |
201 | |
202 v1 = dcts[c1 / NJPEG][c1 % NJPEG]; | |
203 v2 = dcts[c2 / NJPEG][c2 % NJPEG]; | |
204 | |
205 if (fabs(v1) > fabs(v2)) { | |
206 set_signature_bit(n, 1); | |
207 if (verbose >= 1) | |
208 fprintf(stderr, "HIGH\n"); | |
209 } | |
210 else { | |
211 set_signature_bit(n, 0); | |
212 if (verbose >= 1) | |
213 fprintf(stderr, "LOW\n"); | |
214 } | |
215 | |
216 if (verbose >= 2) | |
217 print_coeffs_8x8(dcts); | |
218 | |
219 n++; | |
220 } | |
221 | |
222 fprintf(out, "KCWM\n"); | |
223 fprintf(out, "%d\n", nbit_signature); | |
224 fwrite(signature, sizeof(char), n_signature, out); | |
225 fprintf(out, "\n"); | |
226 | |
227 fclose(out); | |
228 | |
229 free_coeffs(dcts); | |
230 | |
231 pbm_freearray(image, rows); | |
232 | |
233 exit(0); | |
234 } |