Mercurial > hg > wm
comparison Meerwald/gen_kim_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 | |
3 char *progname; | |
4 | |
5 void usage(void) { | |
6 fprintf(stderr, "usage: %s [-a n] [-A n] [-d n] [-e n] [-f n] [-F file] [-l n] [-m n] [-n n] [-o file] [-s file] [-S n]\n\n", progname); | |
7 fprintf(stderr, "\t-a n\t\talpha factor for detail subbands (default 0.1)\n"); | |
8 fprintf(stderr, "\t-A n\t\talpha factor for approximation subband (default 0.02)\n"); | |
9 fprintf(stderr, "\t-d n\t\tdeviation (default 1.0)\n"); | |
10 fprintf(stderr, "\t-e n\t\twavelet filtering method (default 2)\n"); | |
11 fprintf(stderr, "\t-f n\t\tfilter number (default 1)\n"); | |
12 fprintf(stderr, "\t-F file\t\tfilter definition file (default 'filter.dat')\n"); | |
13 fprintf(stderr, "\t-h\t\tprint usage\n"); | |
14 fprintf(stderr, "\t-l n\t\tdecomposition level (default 4)\n"); | |
15 fprintf(stderr, "\t-m n \t\tmean value (default 0.0)\n"); | |
16 fprintf(stderr, "\t-n n\t\twatermark length (default 1000)\n"); | |
17 fprintf(stderr, "\t-o file\t\toutput file\n"); | |
18 fprintf(stderr, "\t-s file\t\tuse signature file's embedding information\n"); | |
19 fprintf(stderr, "\t-S n\t\tseed\n"); | |
20 exit(0); | |
21 } | |
22 | |
23 int main(int argc, char *argv[]) { | |
24 FILE *out = stdout; | |
25 FILE *sig = NULL; | |
26 | |
27 char output_name[MAXPATHLEN] = "(stdout)"; | |
28 char signature_name[MAXPATHLEN]; | |
29 | |
30 int c; | |
31 int i; | |
32 int n = 1000; | |
33 int s = 0; | |
34 int e = 2; | |
35 int f = 1; | |
36 char F[MAXPATHLEN] = "filter.dat"; | |
37 double a = 0.1; | |
38 double A = 0.02; | |
39 int l = 4; | |
40 double m = 0.0; | |
41 double d = 1.0; | |
42 | |
43 progname = argv[0]; | |
44 | |
45 while ((c = getopt(argc, argv, "a:A:l:d:e:f:F:h?m:n:o:s:S:")) != EOF) { | |
46 switch (c) { | |
47 case 'a': | |
48 a = atof(optarg); | |
49 if (a <= 0.0) { | |
50 fprintf(stderr, "%s: alpha factor %f out of range\n", progname, a); | |
51 exit(1); | |
52 } | |
53 break; | |
54 case 'A': | |
55 A = atof(optarg); | |
56 if (A <= 0.0) { | |
57 fprintf(stderr, "%s: alpha factor %f out of range\n", progname, a); | |
58 exit(1); | |
59 } | |
60 break; | |
61 case 'l': | |
62 l = atoi(optarg); | |
63 if (l <= 0) { | |
64 fprintf(stderr, "%s: decomposition level %d out of range\n", progname, l); | |
65 exit(1); | |
66 } | |
67 break; | |
68 case 'd': | |
69 d = atof(optarg); | |
70 if (d <= 0.0) { | |
71 fprintf(stderr, "%s: deviation %f out of range\n", progname, d); | |
72 exit(1); | |
73 } | |
74 break; | |
75 case 'e': | |
76 e = atoi(optarg); | |
77 if (e < 0) { | |
78 fprintf(stderr, "%s: wavelet filtering method %d out of range\n", progname, e); | |
79 } | |
80 break; | |
81 case 'f': | |
82 f = atoi(optarg); | |
83 if (f <= 0) { | |
84 fprintf(stderr, "%s: filter number %d out of range\n", progname, f); | |
85 exit(1); | |
86 } | |
87 break; | |
88 case 'F': | |
89 strcpy(F, optarg); | |
90 break; | |
91 case 'h': | |
92 case '?': | |
93 usage(); | |
94 break; | |
95 case 'm': | |
96 m = atof(optarg); | |
97 break; | |
98 case 'n': | |
99 n = atoi(optarg); | |
100 if (n < 1 || n > 32000) { | |
101 fprintf(stderr, "%s: watermark length %d out of range\n", progname, n); | |
102 exit(1); | |
103 } | |
104 break; | |
105 case 'o': | |
106 if ((out = fopen(optarg, "w")) == NULL) { | |
107 fprintf(stderr, "%s: unable to open output file %s\n", progname, optarg); | |
108 exit(1); | |
109 } | |
110 strcpy(output_name, optarg); | |
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 > 0) { | |
129 usage(); | |
130 exit(1); | |
131 } | |
132 | |
133 if (sig) { | |
134 char line[32]; | |
135 fgets(line, sizeof(line), sig); | |
136 if (strspn(line, "KISG") >= 4) { | |
137 if (n == 0) | |
138 fscanf(sig, "%d\n", &n); | |
139 else | |
140 fscanf(sig, "%*d\n"); | |
141 if (a == 0.0) | |
142 fscanf(sig, "%lf\n", &a); | |
143 else | |
144 fscanf(sig, "%*lf\n"); | |
145 if (A == 0.0) | |
146 fscanf(sig, "%lf\n", &A); | |
147 else | |
148 fscanf(sig, "%*lf\n"); | |
149 if (l == 0) | |
150 fscanf(sig, "%d\n", &l); | |
151 else | |
152 fscanf(sig, "%*d\n"); | |
153 if (e < 0) | |
154 fscanf(sig, "%d\n", &e); | |
155 else | |
156 fscanf(sig, "%*d\n"); | |
157 if (f == 0) | |
158 fscanf(sig, "%d\n", &f); | |
159 else | |
160 fscanf(sig, "%*d\n"); | |
161 if (!strcmp(F, "")) | |
162 fscanf(sig, "%[^\n\r]\n", &F); | |
163 else | |
164 fscanf(sig, "%*[^\n\r]\n"); | |
165 } | |
166 else { | |
167 fprintf(stderr, "%s: invalid signature file %s\n", progname, signature_name); | |
168 exit(1); | |
169 } | |
170 } | |
171 | |
172 if (s) | |
173 srandom(s); | |
174 else | |
175 srandom(time(NULL) * getpid()); | |
176 | |
177 fprintf(out, "KISG\n"); | |
178 fprintf(out, "%d\n", n); | |
179 fprintf(out, "%f\n", a); | |
180 fprintf(out, "%f\n", A); | |
181 fprintf(out, "%d\n", l); | |
182 fprintf(out, "%d\n", e); | |
183 fprintf(out, "%d\n", f); | |
184 fprintf(out, "%s\n", F); | |
185 | |
186 n >>= 1; | |
187 while (n > 0) { | |
188 double x; | |
189 double x1, x2; | |
190 | |
191 /* | |
192 * Algorithm P (Polar method for normal deviates), | |
193 * Knuth, D., "The Art of Computer Programming", Vol. 2, 3rd Edition, p. 122 | |
194 */ | |
195 do { | |
196 x1 = 2.0 * ((random() & RAND_MAX) / ((double) RAND_MAX + 1.0)) - 1.0; | |
197 x2 = 2.0 * ((random() & RAND_MAX) / ((double) RAND_MAX + 1.0)) - 1.0; | |
198 x = x1 * x1 + x2 * x2; | |
199 } while (x >= 1.0); | |
200 x1 *= sqrt((-2.0) * log(x) / x); | |
201 x2 *= sqrt((-2.0) * log(x) / x); | |
202 | |
203 fprintf(out, "%f\n", m + d * x1); | |
204 fprintf(out, "%f\n", m + d * x2); | |
205 | |
206 n--; | |
207 } | |
208 | |
209 fclose(out); | |
210 | |
211 exit(0); | |
212 } |