Mercurial > hg > wm
comparison Meerwald-dir/gen_corvi_sig.c @ 24:9f20bce6184e v0.7
move directories, support netpbm 11
| author | Peter Meerwald-Stadler <pmeerw@pmeerw.net> |
|---|---|
| date | Fri, 20 Dec 2024 13:08:59 +0100 |
| parents | Meerwald/gen_corvi_sig.c@f83ef905a63d |
| children |
comparison
equal
deleted
inserted
replaced
| 23:71dd4b96221b | 24:9f20bce6184e |
|---|---|
| 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] [-m n] [-n n] [-o file] [-s n]\n\n", progname); | |
| 7 fprintf(stderr, "\t-a n\t\talpha factor (default 0.1)\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-m n \t\tmean value (default 0.0)\n"); | |
| 14 fprintf(stderr, "\t-n n\t\twatermark length (default 1000)\n"); | |
| 15 fprintf(stderr, "\t-o file\t\toutput file\n"); | |
| 16 fprintf(stderr, "\t-s n\t\tseed\n"); | |
| 17 exit(0); | |
| 18 } | |
| 19 | |
| 20 int main(int argc, char *argv[]) { | |
| 21 FILE *out = stdout; | |
| 22 | |
| 23 char output_name[MAXPATHLEN] = "(stdout)"; | |
| 24 | |
| 25 int c; | |
| 26 int n = 1000; | |
| 27 int s = 0; | |
| 28 int e = 2; | |
| 29 int f = 1; | |
| 30 char F[MAXPATHLEN] = "filter.dat"; | |
| 31 double a = 0.1; | |
| 32 double m = 0.0; | |
| 33 double d = 1.0; | |
| 34 | |
| 35 progname = argv[0]; | |
| 36 | |
| 37 while ((c = getopt(argc, argv, "a:d:e:f:F:h?m:n:o:s:")) != EOF) { | |
| 38 switch (c) { | |
| 39 case 'a': | |
| 40 a = atof(optarg); | |
| 41 if (a <= 0.0) { | |
| 42 fprintf(stderr, "%s: alpha factor %f out of range\n", progname, a); | |
| 43 exit(1); | |
| 44 } | |
| 45 break; | |
| 46 case 'd': | |
| 47 d = atof(optarg); | |
| 48 if (d <= 0.0) { | |
| 49 fprintf(stderr, "%s: deviation %f out of range\n", progname, d); | |
| 50 exit(1); | |
| 51 } | |
| 52 break; | |
| 53 case 'e': | |
| 54 e = atoi(optarg); | |
| 55 if (e < 0) { | |
| 56 fprintf(stderr, "%s: wavelet filtering method %d out of range\n", progname, e); | |
| 57 } | |
| 58 break; | |
| 59 case 'f': | |
| 60 f = atoi(optarg); | |
| 61 if (f <= 0) { | |
| 62 fprintf(stderr, "%s: filter number %d out of range\n", progname, f); | |
| 63 exit(1); | |
| 64 } | |
| 65 break; | |
| 66 case 'F': | |
| 67 strcpy(F, optarg); | |
| 68 break; | |
| 69 case 'h': | |
| 70 case '?': | |
| 71 usage(); | |
| 72 break; | |
| 73 case 'm': | |
| 74 m = atof(optarg); | |
| 75 break; | |
| 76 case 'n': | |
| 77 n = atoi(optarg); | |
| 78 if (n < 1 || n > 32000) { | |
| 79 fprintf(stderr, "%s: watermark length %d out of range\n", progname, n); | |
| 80 exit(1); | |
| 81 } | |
| 82 break; | |
| 83 case 'o': | |
| 84 if ((out = fopen(optarg, "w")) == NULL) { | |
| 85 fprintf(stderr, "%s: unable to open output file %s\n", progname, optarg); | |
| 86 exit(1); | |
| 87 } | |
| 88 strcpy(output_name, optarg); | |
| 89 break; | |
| 90 case 's': | |
| 91 s = atoi(optarg); | |
| 92 break; | |
| 93 } | |
| 94 } | |
| 95 | |
| 96 argc -= optind; | |
| 97 argv += optind; | |
| 98 | |
| 99 if (argc > 0) { | |
| 100 usage(); | |
| 101 exit(1); | |
| 102 } | |
| 103 | |
| 104 if (s) | |
| 105 srandom(s); | |
| 106 else | |
| 107 srandom(time(NULL) * getpid()); | |
| 108 | |
| 109 fprintf(out, "CVSG\n"); | |
| 110 fprintf(out, "%d\n", n); | |
| 111 fprintf(out, "%f\n", a); | |
| 112 fprintf(out, "%d\n", e); | |
| 113 fprintf(out, "%d\n", f); | |
| 114 fprintf(out, "%s\n", F); | |
| 115 | |
| 116 n >>= 1; | |
| 117 while (n > 0) { | |
| 118 double x; | |
| 119 double x1, x2; | |
| 120 | |
| 121 /* | |
| 122 * Algorithm P (Polar method for normal deviates), | |
| 123 * Knuth, D., "The Art of Computer Programming", Vol. 2, 3rd Edition, p. 122 | |
| 124 */ | |
| 125 do { | |
| 126 x1 = 2.0 * ((random() & RAND_MAX) / ((double) RAND_MAX + 1.0)) - 1.0; | |
| 127 x2 = 2.0 * ((random() & RAND_MAX) / ((double) RAND_MAX + 1.0)) - 1.0; | |
| 128 x = x1 * x1 + x2 * x2; | |
| 129 } while (x >= 1.0); | |
| 130 x1 *= sqrt((-2.0) * log(x) / x); | |
| 131 x2 *= sqrt((-2.0) * log(x) / x); | |
| 132 | |
| 133 fprintf(out, "%f\n", m + d * x1); | |
| 134 fprintf(out, "%f\n", m + d * x2); | |
| 135 | |
| 136 n--; | |
| 137 } | |
| 138 | |
| 139 fclose(out); | |
| 140 | |
| 141 exit(0); | |
| 142 } |
