comparison Meerwald-dir/gen_kund_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_kund_sig.c@acb6967ee76d
children
comparison
equal deleted inserted replaced
23:71dd4b96221b 24:9f20bce6184e
1 #include "wm.h"
2 #include "signature.h"
3
4 char *progname;
5
6 void usage(void) {
7 fprintf(stderr, "usage: %s [-e n] [-f n] [-F file] [-l n] [-n n] [-o file] [-q n] [-s n] file\n\n", progname);
8 fprintf(stderr, "\t-h\t\tprint usage\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-l n\t\tembedding level (default 1)\n");
13 fprintf(stderr, "\t-n n\t\twatermark bit length\n");
14 fprintf(stderr, "\t-o file\t\toutput file\n");
15 fprintf(stderr, "\t-q n\t\tsignature strength (default 1.0)\n");
16 fprintf(stderr, "\t-s n\t\tseed\n");
17 exit(0);
18 }
19
20 int main(int argc, char *argv[]) {
21 FILE *in = stdin;
22 FILE *out = stdout;
23
24 char output_name[MAXPATHLEN] = "(stdout)";
25 char input_name[MAXPATHLEN] = "(stdin)";
26
27 int c;
28 int i;
29 int l = 1;
30 int n = 0, nb;
31 int s = 0;
32 double q = 1.0;
33 int e = 2;
34 int f = 1;
35 char F[MAXPATHLEN] = "filter.dat";
36
37 progname = argv[0];
38
39 #ifdef __EMX__
40 _fsetmode(in, "b");
41 _fsetmode(out, "b");
42 #endif
43
44 while ((c = getopt(argc, argv, "e:f:F:h?l:n:o:q:s:")) != EOF) {
45 switch (c) {
46 case 'e':
47 e = atoi(optarg);
48 if (e < 0) {
49 fprintf(stderr, "%s: wavelet filtering method %d out of range\n", progname, e);
50 }
51 break;
52 case 'f':
53 f = atoi(optarg);
54 if (f <= 0) {
55 fprintf(stderr, "%s: filter number %d out of range\n", progname, f);
56 exit(1);
57 }
58 break;
59 case 'F':
60 strcpy(F, optarg);
61 break;
62 case 'h':
63 case '?':
64 usage();
65 break;
66 case 'l':
67 l = atoi(optarg);
68 if (l < 1) {
69 fprintf(stderr, "%s: embedding level out of range\n", progname);
70 exit(1);
71 }
72 break;
73 case 'n':
74 n = atoi(optarg);
75 if (n < 1 || n > 1000) {
76 fprintf(stderr, "%s: watermark length %d out of range\n", progname, n);
77 exit(1);
78 }
79 if (n % 4 != 0 || (int) sqrt(n / 4) * (int) sqrt(n / 4) != n) {
80 fprintf(stderr, "%s: watermark length not divisible by 4 or not a square number\n", progname);
81 exit(1);
82 }
83 break;
84 case 'o':
85 if ((out = fopen(optarg, "wb")) == NULL) {
86 fprintf(stderr, "%s: unable to open output file %s\n", progname, optarg);
87 exit(1);
88 }
89 strcpy(output_name, optarg);
90 break;
91 case 'q':
92 q = atof(optarg);
93 if (q <= 0.0) {
94 fprintf(stderr, "%s: signature strength factor %f out of range\n", progname, q);
95 exit(1);
96 }
97 break;
98 case 's':
99 s = atoi(optarg);
100 break;
101 }
102 }
103
104 argc -= optind;
105 argv += optind;
106
107 if (argc > 0) {
108 usage();
109 exit(1);
110 }
111
112 if (argc == 1 && *argv[0] != '-')
113 if ((in = fopen(argv[0], "rb")) == NULL) {
114 fprintf(stderr, "%s: unable to open input file %s\n", progname, argv[0]);
115 exit(1);
116 }
117 else
118 strcpy(input_name, argv[0]);
119
120 if (s)
121 srandom(s);
122 else
123 srandom(time(NULL) * getpid());
124
125 if (n > 0) {
126 nb = fread(signature, sizeof(char), i = NBITSTOBYTES(n), in);
127 if (nb < i) {
128 fprintf(stderr, "%s: failed to read all %d signature bits from %s\n", progname, n, input_name);
129 exit(1);
130 }
131 }
132 else {
133 int n_square;
134 if (fscanf(in, "%128[^\n\r]", signature) == EOF) {
135 fprintf(stderr, "%s: failed to read signature bits from %s\n", progname, input_name);
136 exit(1);
137 }
138 nb = strlen(signature);
139 n = NBYTESTOBITS(nb);
140 n_square = (int) sqrt(n) * (int) sqrt(n);
141 fprintf(stderr, "%s: got %d signature bits, truncated to %d\n", progname, n, n_square);
142 n = n_square;
143 nb = NBITSTOBYTES(n);
144 if (n < 1) {
145 fprintf(stderr, "%s: watermark length %d out of range\n", progname, n);
146 exit(1);
147 }
148 }
149
150 fprintf(out, "KDSG\n");
151 fprintf(out, "%d\n", n);
152 fprintf(out, "%d\n", e);
153 fprintf(out, "%d\n", f);
154 fprintf(out, "%s\n", F);
155 fprintf(out, "%d\n", l);
156 fprintf(out, "%f\n", q);
157 fprintf(out, "%d\n", random());
158 fwrite(signature, sizeof(char), nb, out);
159 fprintf(out, "\n");
160
161 fclose(out);
162
163 exit(0);
164 }

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