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