Mercurial > hg > wm
annotate Meerwald/gen_dugad_sig.c @ 16:4987db85cfae
fix another scanf() warning
author | Peter Meerwald <pmeerw@cosy.sbg.ac.at> |
---|---|
date | Mon, 30 Jun 2008 21:03:40 +0200 |
parents | ad1d224896c5 |
children |
rev | line source |
---|---|
0 | 1 #include "wm.h" |
2 | |
3 char *progname; | |
4 | |
5 void usage(void) { | |
6 fprintf(stderr, "usage: %s [-a n] [-e n] [-f n] [-F file] [-l n] [-n n] [-o file] [-s file] [-S n] [-t n] [-T n]\n\n", progname); | |
7 fprintf(stderr, "\t-a n\t\talpha factor (default 0.2)\n"); | |
8 fprintf(stderr, "\t-d n\t\tdeviation (default 1.0)\n"); | |
9 fprintf(stderr, "\t-e n\t\twavelet filtering method (default 2)\n"); | |
10 fprintf(stderr, "\t-f n\t\tfilter number (default 1)\n"); | |
11 fprintf(stderr, "\t-F file\t\tfilter definition file (default 'filter.dat')\n"); | |
12 fprintf(stderr, "\t-h\t\tprint usage\n"); | |
13 fprintf(stderr, "\t-l n\t\tdecomposition levels (default 3)\n"); | |
14 fprintf(stderr, "\t-m n \t\tmean value (default 0.0)\n"); | |
15 fprintf(stderr, "\t-n n\t\twatermark length (default 1000)\n"); | |
16 fprintf(stderr, "\t-o file\t\toutput file\n"); | |
17 fprintf(stderr, "\t-s file\t\tsignature to embed in input image\n"); | |
18 fprintf(stderr, "\t-S n\t\tseed\n"); | |
19 fprintf(stderr, "\t-t n\t\tcasting threshold (default 40.0)\n"); | |
20 fprintf(stderr, "\t-T n\t\tdetection threshold (default 50.0)\n"); | |
21 exit(0); | |
22 } | |
23 | |
24 int main(int argc, char *argv[]) { | |
25 FILE *out = stdout; | |
26 FILE *sig = NULL; | |
27 | |
28 char output_name[MAXPATHLEN] = "(stdout)"; | |
29 char signature_name[MAXPATHLEN]; | |
30 | |
31 int c; | |
32 int n = 1000; | |
33 int s = 0; | |
34 int e = 2; | |
35 int f = 1; | |
36 int l = 3; | |
37 char F[MAXPATHLEN] = "filter.dat"; | |
38 double a = 0.2; | |
39 double t1 = 40.0; | |
40 double t2 = 50.0; | |
41 double m = 0.0; | |
42 double d = 1.0; | |
43 | |
44 progname = argv[0]; | |
45 | |
46 while ((c = getopt(argc, argv, "a:b:d:e:f:F:h?l:m:n:o:s:S:t:T:")) != EOF) { | |
47 switch (c) { | |
48 case 'a': | |
49 a = atof(optarg); | |
50 if (a <= 0.0) { | |
51 fprintf(stderr, "%s: alpha factor %f out of range\n", progname, a); | |
52 exit(1); | |
53 } | |
54 break; | |
55 case 'd': | |
56 d = atof(optarg); | |
57 if (d <= 0.0) { | |
58 fprintf(stderr, "%s: deviation %f out of range\n", progname, d); | |
59 exit(1); | |
60 } | |
61 break; | |
62 | |
63 case 'e': | |
64 e = atoi(optarg); | |
65 if (e < 0) { | |
66 fprintf(stderr, "%s: wavelet filtering method %d out of range\n", progname, e); | |
67 } | |
68 break; | |
69 case 'f': | |
70 f = atoi(optarg); | |
71 if (f <= 0) { | |
72 fprintf(stderr, "%s: filter number %d out of range\n", progname, f); | |
73 exit(1); | |
74 } | |
75 break; | |
76 case 'F': | |
77 strcpy(F, optarg); | |
78 break; | |
79 case 'h': | |
80 case '?': | |
81 usage(); | |
82 break; | |
83 case 'l': | |
84 l = atoi(optarg); | |
85 if (l <= 0) { | |
8 | 86 fprintf(stderr, "%s: decomposition level %d out of range\n", progname, l); |
0 | 87 exit(1); |
88 } | |
89 break; | |
90 case 'm': | |
91 m = atof(optarg); | |
92 break; | |
93 case 'n': | |
94 n = atoi(optarg); | |
95 if (n < 1 || n > 32000) { | |
96 fprintf(stderr, "%s: watermark length %d out of range\n", progname, n); | |
97 exit(1); | |
98 } | |
99 break; | |
100 case 'o': | |
101 if ((out = fopen(optarg, "w")) == NULL) { | |
102 fprintf(stderr, "%s: unable to open output file %s\n", progname, optarg); | |
103 exit(1); | |
104 } | |
105 strcpy(output_name, optarg); | |
106 break; | |
107 case 's': | |
108 if ((sig = fopen(optarg, "r")) == NULL) { | |
109 fprintf(stderr, "%s: unable to open signature file %s\n", progname, optarg); | |
110 exit(1); | |
111 } | |
112 strcpy(signature_name, optarg); | |
113 break; | |
114 case 'S': | |
115 s = atoi(optarg); | |
116 break; | |
117 case 't': | |
118 t1 = atof(optarg); | |
119 if (t1 <= 0.0) { | |
120 fprintf(stderr, "%s: casting threshold %f out of range\n", progname, t1); | |
121 exit(1); | |
122 } | |
123 break; | |
124 case 'T': | |
125 t2 = atof(optarg); | |
126 if (t2 <= 0.0) { | |
127 fprintf(stderr, "%s: detection threshold %f out of range\n", progname, t2); | |
128 exit(1); | |
129 } | |
130 break; | |
131 } | |
132 } | |
133 | |
134 argc -= optind; | |
135 argv += optind; | |
136 | |
137 if (argc > 0) { | |
138 usage(); | |
139 exit(1); | |
140 } | |
141 | |
142 if (sig) { | |
143 char line[32]; | |
144 fgets(line, sizeof(line), sig); | |
145 if (strspn(line, "DGSG") >= 4) { | |
146 if (n == 0) | |
147 fscanf(sig, "%d\n", &n); | |
148 else | |
149 fscanf(sig, "%*d\n"); | |
150 if (l == 0) | |
151 fscanf(sig, "%d\n", &l); | |
152 else | |
153 fscanf(sig, "%*d\n"); | |
154 if (a == 0.0) | |
155 fscanf(sig, "%lf\n", &a); | |
156 else | |
16
4987db85cfae
fix another scanf() warning
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
15
diff
changeset
|
157 fscanf(sig, "%*f\n"); |
0 | 158 if (t1 == 0.0) |
159 fscanf(sig, "%lf\n", &t1); | |
160 else | |
16
4987db85cfae
fix another scanf() warning
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
15
diff
changeset
|
161 fscanf(sig, "%*f\n"); |
0 | 162 if (t2 == 0.0) |
163 fscanf(sig, "%lf\n", &t2); | |
164 else | |
16
4987db85cfae
fix another scanf() warning
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
15
diff
changeset
|
165 fscanf(sig, "%*f\n"); |
0 | 166 if (e < 0) |
167 fscanf(sig, "%d\n", &e); | |
168 else | |
169 fscanf(sig, "%*d\n"); | |
170 if (f == 0) | |
171 fscanf(sig, "%d\n", &f); | |
172 else | |
173 fscanf(sig, "%*d\n"); | |
174 if (!strcmp(F, "")) | |
15 | 175 fscanf(sig, "%[^\n\r]\n", F); |
0 | 176 else |
177 fscanf(sig, "%*[^\n\r]\n"); | |
178 } | |
179 else { | |
180 fprintf(stderr, "%s: invalid signature file %s\n", progname, signature_name); | |
181 exit(1); | |
182 } | |
183 } | |
184 | |
185 if (s) | |
186 srandom(s); | |
187 else | |
188 srandom(time(NULL) * getpid()); | |
189 | |
190 fprintf(out, "DGSG\n"); | |
191 fprintf(out, "%d\n", n); | |
192 fprintf(out, "%d\n", l); | |
193 fprintf(out, "%f\n", a); | |
194 fprintf(out, "%f\n", t1); | |
195 fprintf(out, "%f\n", t2); | |
196 fprintf(out, "%d\n", e); | |
197 fprintf(out, "%d\n", f); | |
198 fprintf(out, "%s\n", F); | |
199 | |
200 n >>= 1; | |
201 while (n > 0) { | |
202 double x; | |
203 double x1, x2; | |
204 | |
205 /* | |
206 * Algorithm P (Polar method for normal deviates), | |
207 * Knuth, D., "The Art of Computer Programming", Vol. 2, 3rd Edition, p. 122 | |
208 */ | |
209 do { | |
210 x1 = 2.0 * ((random() & RAND_MAX) / ((double) RAND_MAX + 1.0)) - 1.0; | |
211 x2 = 2.0 * ((random() & RAND_MAX) / ((double) RAND_MAX + 1.0)) - 1.0; | |
212 x = x1 * x1 + x2 * x2; | |
213 } while (x >= 1.0); | |
214 x1 *= sqrt((-2.0) * log(x) / x); | |
215 x2 *= sqrt((-2.0) * log(x) / x); | |
216 | |
217 fprintf(out, "%f\n", m + d * x1); | |
218 fprintf(out, "%f\n", m + d * x2); | |
219 | |
220 n--; | |
221 } | |
222 | |
223 fclose(out); | |
224 | |
225 exit(0); | |
226 } |