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