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