Mercurial > hg > wm
annotate Meerwald/gen_cox_sig.c @ 21:1c4ccd635a68 v0.6
ignore *.swp
author | Peter Meerwald-Stadler <pmeerw@pmeerw.net> |
---|---|
date | Sat, 28 Jan 2023 23:57:40 +0100 |
parents | 4987db85cfae |
children |
rev | line source |
---|---|
0 | 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 n = 100; | |
27 int s = 0; | |
28 double a = 0.3; | |
29 double m = 0.0; | |
30 double d = 1.0; | |
31 | |
32 progname = argv[0]; | |
33 | |
34 while ((c = getopt(argc, argv, "a:d:h?m:n:o:s:S:")) != EOF) { | |
35 switch (c) { | |
36 case 'a': | |
37 a = atof(optarg); | |
38 if (a <= 0.0) { | |
39 fprintf(stderr, "%s: alpha factor %f out of range\n", progname, a); | |
40 exit(1); | |
41 } | |
42 break; | |
43 case 'd': | |
44 d = atof(optarg); | |
45 if (d <= 0.0) { | |
46 fprintf(stderr, "%s: deviation %f out of range\n", progname, d); | |
47 exit(1); | |
48 } | |
49 break; | |
50 case 'h': | |
51 case '?': | |
52 usage(); | |
53 break; | |
54 case 'm': | |
55 m = atof(optarg); | |
56 break; | |
57 case 'n': | |
58 n = atoi(optarg); | |
59 if (n < 1 || n > 32000) { | |
60 fprintf(stderr, "%s: watermark length %d out of range\n", progname, n); | |
61 exit(1); | |
62 } | |
63 break; | |
64 case 'o': | |
65 if ((out = fopen(optarg, "w")) == NULL) { | |
66 fprintf(stderr, "%s: unable to open output file %s\n", progname, optarg); | |
67 exit(1); | |
68 } | |
69 strcpy(output_name, optarg); | |
70 break; | |
71 case 's': | |
72 if ((sig = fopen(optarg, "r")) == NULL) { | |
73 fprintf(stderr, "%s: unable to open signature file %s\n", progname, optarg); | |
74 exit(1); | |
75 } | |
76 strcpy(signature_name, optarg); | |
77 break; | |
78 case 'S': | |
79 s = atoi(optarg); | |
80 break; | |
81 } | |
82 } | |
83 | |
84 argc -= optind; | |
85 argv += optind; | |
86 | |
87 if (argc > 0) { | |
88 usage(); | |
89 exit(1); | |
90 } | |
91 | |
92 if (sig) { | |
93 char line[32]; | |
94 fgets(line, sizeof(line), sig); | |
95 if (strspn(line, "CXSG") >= 4) { | |
96 if (n == 0) | |
97 fscanf(sig, "%d\n", &n); | |
98 else | |
99 fscanf(sig, "%*d\n"); | |
100 if (a == 0.0) | |
101 fscanf(sig, "%lf\n", &a); | |
102 else | |
16
4987db85cfae
fix another scanf() warning
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
8
diff
changeset
|
103 fscanf(sig, "%*f\n"); |
0 | 104 if (m == 0.0) |
105 fscanf(sig, "%lf\n", &m); | |
106 else | |
16
4987db85cfae
fix another scanf() warning
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
8
diff
changeset
|
107 fscanf(sig, "%*f\n"); |
0 | 108 if (d == 0.0) |
109 fscanf(sig, "%lf\n", &d); | |
110 else | |
16
4987db85cfae
fix another scanf() warning
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
8
diff
changeset
|
111 fscanf(sig, "%*f\n"); |
0 | 112 } |
113 else { | |
114 fprintf(stderr, "%s: invalid signature file %s\n", progname, signature_name); | |
115 exit(1); | |
116 } | |
8 | 117 fclose(sig); |
0 | 118 } |
119 | |
120 if (s) | |
121 srandom(s); | |
122 else | |
123 srandom(time(NULL) * getpid()); | |
124 | |
125 fprintf(out, "CXSG\n"); | |
126 fprintf(out, "%d\n", n); | |
127 fprintf(out, "%f\n", a); | |
128 fprintf(out, "%f\n", m); | |
129 fprintf(out, "%f\n", d); | |
130 | |
131 n >>= 1; | |
132 while (n > 0) { | |
133 double x; | |
134 double x1, x2; | |
135 | |
136 /* | |
137 * Algorithm P (Polar method for normal deviates), | |
138 * Knuth, D., "The Art of Computer Programming", Vol. 2, 3rd Edition, p. 122 | |
139 */ | |
140 do { | |
141 x1 = 2.0 * ((random() & RAND_MAX) / ((double) RAND_MAX + 1.0)) - 1.0; | |
142 x2 = 2.0 * ((random() & RAND_MAX) / ((double) RAND_MAX + 1.0)) - 1.0; | |
143 x = x1 * x1 + x2 * x2; | |
144 } while (x >= 1.0); | |
145 x1 *= sqrt((-2.0) * log(x) / x); | |
146 x2 *= sqrt((-2.0) * log(x) / x); | |
147 | |
148 fprintf(out, "%f\n", m + d * x1); | |
149 fprintf(out, "%f\n", m + d * x2); | |
150 | |
151 n--; | |
152 } | |
153 | |
154 fclose(out); | |
155 | |
156 exit(0); | |
157 } |