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