comparison Meerwald/gen_cox_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] [-d 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.3)\n");
8 fprintf(stderr, "\t-d n\t\tdeviation (default 1.0)\n");
9 fprintf(stderr, "\t-h\t\tprint usage\n");
10 fprintf(stderr, "\t-m n \t\tmean value (default 0.0)\n");
11 fprintf(stderr, "\t-n n\t\twatermark length (default 100)\n");
12 fprintf(stderr, "\t-o file\t\toutput file\n");
13 fprintf(stderr, "\t-s file\t\tuse signature file's embedding information\n");
14 fprintf(stderr, "\t-S n\t\tseed\n");
15 exit(0);
16 }
17
18 int main(int argc, char *argv[]) {
19 FILE *out = stdout;
20 FILE *sig = NULL;
21
22 char output_name[MAXPATHLEN] = "(stdout)";
23 char signature_name[MAXPATHLEN];
24
25 int c;
26 int i;
27 int n = 100;
28 int s = 0;
29 double a = 0.3;
30 double m = 0.0;
31 double d = 1.0;
32
33 progname = argv[0];
34
35 while ((c = getopt(argc, argv, "a:d:h?m:n:o:s:S:")) != EOF) {
36 switch (c) {
37 case 'a':
38 a = atof(optarg);
39 if (a <= 0.0) {
40 fprintf(stderr, "%s: alpha factor %f out of range\n", progname, a);
41 exit(1);
42 }
43 break;
44 case 'd':
45 d = atof(optarg);
46 if (d <= 0.0) {
47 fprintf(stderr, "%s: deviation %f out of range\n", progname, d);
48 exit(1);
49 }
50 break;
51 case 'h':
52 case '?':
53 usage();
54 break;
55 case 'm':
56 m = atof(optarg);
57 break;
58 case 'n':
59 n = atoi(optarg);
60 if (n < 1 || n > 32000) {
61 fprintf(stderr, "%s: watermark length %d out of range\n", progname, n);
62 exit(1);
63 }
64 break;
65 case 'o':
66 if ((out = fopen(optarg, "w")) == NULL) {
67 fprintf(stderr, "%s: unable to open output file %s\n", progname, optarg);
68 exit(1);
69 }
70 strcpy(output_name, optarg);
71 break;
72 case 's':
73 if ((sig = fopen(optarg, "r")) == NULL) {
74 fprintf(stderr, "%s: unable to open signature file %s\n", progname, optarg);
75 exit(1);
76 }
77 strcpy(signature_name, optarg);
78 break;
79 case 'S':
80 s = atoi(optarg);
81 break;
82 }
83 }
84
85 argc -= optind;
86 argv += optind;
87
88 if (argc > 0) {
89 usage();
90 exit(1);
91 }
92
93 if (sig) {
94 char line[32];
95 fgets(line, sizeof(line), sig);
96 if (strspn(line, "CXSG") >= 4) {
97 if (n == 0)
98 fscanf(sig, "%d\n", &n);
99 else
100 fscanf(sig, "%*d\n");
101 if (a == 0.0)
102 fscanf(sig, "%lf\n", &a);
103 else
104 fscanf(sig, "%*lf\n");
105 if (m == 0.0)
106 fscanf(sig, "%lf\n", &m);
107 else
108 fscanf(sig, "%*lf\n");
109 if (d == 0.0)
110 fscanf(sig, "%lf\n", &d);
111 else
112 fscanf(sig, "%*lf\n");
113 }
114 else {
115 fprintf(stderr, "%s: invalid signature file %s\n", progname, signature_name);
116 exit(1);
117 }
118 close(sig);
119 }
120
121 if (s)
122 srandom(s);
123 else
124 srandom(time(NULL) * getpid());
125
126 fprintf(out, "CXSG\n");
127 fprintf(out, "%d\n", n);
128 fprintf(out, "%f\n", a);
129 fprintf(out, "%f\n", m);
130 fprintf(out, "%f\n", d);
131
132 n >>= 1;
133 while (n > 0) {
134 double x;
135 double x1, x2;
136
137 /*
138 * Algorithm P (Polar method for normal deviates),
139 * Knuth, D., "The Art of Computer Programming", Vol. 2, 3rd Edition, p. 122
140 */
141 do {
142 x1 = 2.0 * ((random() & RAND_MAX) / ((double) RAND_MAX + 1.0)) - 1.0;
143 x2 = 2.0 * ((random() & RAND_MAX) / ((double) RAND_MAX + 1.0)) - 1.0;
144 x = x1 * x1 + x2 * x2;
145 } while (x >= 1.0);
146 x1 *= sqrt((-2.0) * log(x) / x);
147 x2 *= sqrt((-2.0) * log(x) / x);
148
149 fprintf(out, "%f\n", m + d * x1);
150 fprintf(out, "%f\n", m + d * x2);
151
152 n--;
153 }
154
155 fclose(out);
156
157 exit(0);
158 }

Repositories maintained by Peter Meerwald, pmeerw@pmeerw.net.