comparison Meerwald/gen_bruyn_sig.c @ 0:be303a3f5ea8

import
author Peter Meerwald <pmeerw@cosy.sbg.ac.at>
date Sun, 12 Aug 2007 13:14:34 +0200
parents
children f83ef905a63d
comparison
equal deleted inserted replaced
-1:000000000000 0:be303a3f5ea8
1 #include "wm.h"
2 #include "signature.h"
3 #include "bruyn_common.h"
4
5 char *progname;
6
7 void usage(void) {
8 fprintf(stderr, "usage: %s [-b n] [-k] [-n n] [-o file] [-pP n] [-q n] [-s file] [-S n] [-tT n] file\n\n", progname);
9 fprintf(stderr, "\t-b n\t\tblock size (default 8)\n");
10 fprintf(stderr, "\t-h\t\tprint usage\n");
11 fprintf(stderr, "\t-k\t\tdisable block skipping\n");
12 fprintf(stderr, "\t-n n\t\twatermark bit length\n");
13 fprintf(stderr, "\t-o file\t\toutput file\n");
14 fprintf(stderr, "\t-p n\t\tpattern type for zone 1 (default 1, 1.." NPATTERN_USAGE ")\n");
15 fprintf(stderr, "\t-P n\t\tpattern type for zone 2 (default 2, 1.." NPATTERN_USAGE ")\n");
16 fprintf(stderr, "\t-q n\t\tsignature strength (default 7.0)\n");
17 fprintf(stderr, "\t-s file\t\tuse signature file's embedding information\n");
18 fprintf(stderr, "\t-S n\t\tseed\n");
19 fprintf(stderr, "\t-t n\t\tthreshold for noise (default " THRESHOLD_NOISE_USAGE ")\n");
20 fprintf(stderr, "\t-T n\t\tthreshold for slope (default " THRESHOLD_SLOPE_USAGE ")\n");
21 exit(0);
22 }
23
24 int main(int argc, char *argv[]) {
25 FILE *in = stdin;
26 FILE *out = stdout;
27 FILE *sig = NULL;
28
29 char output_name[MAXPATHLEN] = "(stdout)";
30 char input_name[MAXPATHLEN] = "(stdin)";
31 char signature_name[MAXPATHLEN];
32
33 int c;
34 int i;
35 int b = 8;
36 int n = 0;
37 int nb;
38 int s = 0;
39 int p1 = 1;
40 int p2 = 2;
41 double t1 = THRESHOLD_NOISE;
42 double t2 = THRESHOLD_SLOPE;
43 double q = 7.0;
44 int skipping = 0;
45
46 progname = argv[0];
47 wm_init();
48
49 while ((c = getopt(argc, argv, "b:h?n:o:p:P:q:s:S:t:T:k")) != EOF) {
50 switch (c) {
51 case 'b':
52 b = atoi(optarg);
53 if (b <= 0) {
54 fprintf(stderr, "%s: block size %d out of range\n", progname, b);
55 exit(1);
56 }
57 break;
58 case 'k':
59 skipping = 1;
60 break;
61 case 'h':
62 case '?':
63 usage();
64 break;
65 case 'n':
66 n = atoi(optarg);
67 if (n < 1 || n > 1000) {
68 fprintf(stderr, "%s: watermark length %d out of range\n", progname, n);
69 exit(1);
70 }
71 break;
72 case 'o':
73 if ((out = fopen(optarg, "wb")) == NULL) {
74 fprintf(stderr, "%s: unable to open output file %s\n", progname, optarg);
75 exit(1);
76 }
77 strcpy(output_name, optarg);
78 break;
79 case 'p':
80 p1 = atoi(optarg);
81 if (p1 <= 0 || p1 > NPATTERN) {
82 fprintf(stderr, "%s: pattern type out of range\n", progname);
83 exit(1);
84 }
85 break;
86 case 'P':
87 p2 = atoi(optarg);
88 if (p2 <= 0 || p2 > NPATTERN) {
89 fprintf(stderr, "%s: pattern type out of range\n", progname);
90 exit(1);
91 }
92 break;
93 case 'q':
94 q = atof(optarg);
95 if (q <= 0.0) {
96 fprintf(stderr, "%s: signature strength factor %f out of range\n", progname, q);
97 exit(1);
98 }
99 break;
100 case 't':
101 t1 = atof(optarg);
102 if (t1 <= 0) {
103 fprintf(stderr, "%s: noise threshold %f out of range\n", progname, t1);
104 }
105 break;
106 case 'T':
107 t2 = atof(optarg);
108 if (t2 <= 0) {
109 fprintf(stderr, "%s: slope threshold %f out of range\n", progname, t2);
110 }
111 break;
112 case 's':
113 if ((sig = fopen(optarg, "r")) == NULL) {
114 fprintf(stderr, "%s: unable to open signature file %s\n", progname, optarg);
115 exit(1);
116 }
117 strcpy(signature_name, optarg);
118 break;
119 case 'S':
120 s = atoi(optarg);
121 break;
122 }
123 }
124
125 argc -= optind;
126 argv += optind;
127
128 if (argc > 1) {
129 usage();
130 exit(1);
131 }
132
133 if (b % 2 > 0 || b <= 2) {
134 fprintf(stderr, "%s: block size has to be even and greater than 2\n");
135 exit(1);
136 }
137
138 if (argc == 1 && *argv[0] != '-')
139 if ((in = fopen(argv[0], "rb")) == NULL) {
140 fprintf(stderr, "%s: unable to open input file %s\n", progname, argv[0]);
141 exit(1);
142 }
143 else
144 strcpy(input_name, argv[0]);
145
146 // read signature file and set options
147 // command line options override signature file options
148 if (sig) {
149 char line[128];
150 fgets(line, sizeof(line), sig);
151 if (strspn(line, "BRSG") >= 4) {
152 if (n == 0)
153 fscanf(sig, "%d\n", &n);
154 else
155 fscanf(sig, "%*d\n");
156 if (skipping == 0)
157 fscanf(sig, "%d\n", &skipping);
158 else
159 fscanf(sig, "%*d\n");
160 if (p1 == 0)
161 fscanf(sig, "%d\n", &p1);
162 else
163 fscanf(sig, "%*d\n");
164 if (p2 == 0)
165 fscanf(sig, "%d\n", &p2);
166 else
167 fscanf(sig, "%*d\n");
168 if (q == 0.0)
169 fscanf(sig, "%lf\n", &q);
170 else
171 fscanf(sig, "%*lf\n");
172 if (t1 == 0.0)
173 fscanf(sig, "%lf\n", &t1);
174 else
175 fscanf(sig, "%*lf\n");
176 if (t2 == 0.0)
177 fscanf(sig, "%lf\n", &t2);
178 else
179 fscanf(sig, "%*lf\n");
180 if (b == 0)
181 fscanf(sig, "%d\n", &b);
182 else
183 fscanf(sig, "%*d\n");
184 }
185 else {
186 fprintf(stderr, "%s: invalid signature file %s\n", progname, signature_name);
187 exit(1);
188 }
189 fclose(sig);
190 }
191
192 if (s)
193 srandom(s);
194 else
195 srandom(time(NULL) * getpid());
196
197 if (n > 0) {
198 nb = fread(signature, sizeof(char), i = NBITSTOBYTES(n), in);
199 if (nb < i) {
200 fprintf(stderr, "%s: failed to read all %d signature bits from %s\n", progname, n, input_name);
201 exit(1);
202 }
203 }
204 else {
205 if (fscanf(in, "%128[^\n\r]", signature) == EOF) {
206 fprintf(stderr, "%s: failed to read signature bits from %s\n", progname, input_name);
207 exit(1);
208 }
209 nb = strlen(signature);
210 n = NBYTESTOBITS(nb);
211 fprintf(stderr, "%s: got %d signature bits\n", progname, n);
212 }
213
214
215 fprintf(out, "BRSG\n");
216 fprintf(out, "%d\n", n);
217 fprintf(out, "%d\n", skipping);
218 fprintf(out, "%d\n", p1);
219 fprintf(out, "%d\n", p2);
220 fprintf(out, "%f\n", q);
221 fprintf(out, "%f\n", t1);
222 fprintf(out, "%f\n", t2);
223 fprintf(out, "%d\n", b);
224 fprintf(out, "%d\n", random());
225 fwrite(signature, sizeof(char), nb, out);
226 fprintf(out, "\n");
227
228 fclose(out);
229
230 exit(0);
231 }

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