Mercurial > hg > wm
comparison Meerwald-dir/gen_kund2_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_kund2_sig.c@ad1d224896c5 |
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 [-b n] [-e n] [-f n] [-F file] [-l n] [-n n] [-o file] [-q n] [-s file] [-S n] [-v n] file\n\n", progname); | |
8 fprintf(stderr, "\t-h\t\tprint usage\n"); | |
9 fprintf(stderr, "\t-b n\t\tblock size (default 64)\n"); | |
10 fprintf(stderr, "\t-e n\t\twavelet filtering method (default 2)\n"); | |
11 fprintf(stderr, "\t-f n\t\tfilter number (default 1)\n"); | |
12 fprintf(stderr, "\t-F file\t\tfilter definition file (default 'filter.dat')\n"); | |
13 fprintf(stderr, "\t-l n\t\tembedding level (default 1)\n"); | |
14 fprintf(stderr, "\t-n n\t\twatermark bit length\n"); | |
15 fprintf(stderr, "\t-o file\t\toutput file\n"); | |
16 fprintf(stderr, "\t-q n\t\tsignature strength (default 4)\n"); | |
17 fprintf(stderr, "\t-s file\t\tuse signature file's embedding information\n"); | |
18 fprintf(stderr, "\t-S n\t\tseed\n"); | |
19 fprintf(stderr, "\t-v n\t\tverbosity level\n"); | |
20 exit(0); | |
21 } | |
22 | |
23 int main(int argc, char *argv[]) { | |
24 FILE *in = stdin; | |
25 FILE *out = stdout; | |
26 FILE *sig = NULL; | |
27 | |
28 char output_name[MAXPATHLEN] = "(stdout)"; | |
29 char input_name[MAXPATHLEN] = "(stdin)"; | |
30 char signature_name[MAXPATHLEN]; | |
31 char *binstr; | |
32 | |
33 int verbose = 0; | |
34 int c; | |
35 int i; | |
36 int l = 3; | |
37 int n = 0; | |
38 int s = 0; | |
39 int q = 4; | |
40 int e = 2; | |
41 int f = 1; | |
42 int blocksize = 64; | |
43 char F[MAXPATHLEN] = "filter.dat"; | |
44 | |
45 progname = argv[0]; | |
46 | |
47 wm_init(); | |
48 | |
49 while ((c = getopt(argc, argv, "b:e:f:F:h?l:n:o:q:s:S:v:")) != EOF) { | |
50 switch (c) { | |
51 case 'b': | |
52 blocksize = atoi(optarg); | |
53 if (blocksize < 0) | |
54 fprintf(stderr, "%s: block size %d out of range\n", progname, blocksize); | |
55 break; | |
56 case 'e': | |
57 e = atoi(optarg); | |
58 if (e < 0) { | |
59 fprintf(stderr, "%s: wavelet filtering method %d out of range\n", progname, e); | |
60 } | |
61 break; | |
62 case 'f': | |
63 f = atoi(optarg); | |
64 if (f <= 0) { | |
65 fprintf(stderr, "%s: filter number %d out of range\n", progname, f); | |
66 exit(1); | |
67 } | |
68 break; | |
69 case 'F': | |
70 strcpy(F, optarg); | |
71 break; | |
72 case 'h': | |
73 case '?': | |
74 usage(); | |
75 break; | |
76 case 'l': | |
77 l = atoi(optarg); | |
78 if (l < 1) { | |
79 fprintf(stderr, "%s: embedding level out of range\n", progname); | |
80 exit(1); | |
81 } | |
82 break; | |
83 case 'n': | |
84 n = atoi(optarg); | |
85 if (n < 1 || n > 1000) { | |
86 fprintf(stderr, "%s: watermark length %d out of range\n", progname, n); | |
87 exit(1); | |
88 } | |
89 break; | |
90 case 'o': | |
91 if ((out = fopen(optarg, "wb")) == NULL) { | |
92 fprintf(stderr, "%s: unable to open output file %s\n", progname, optarg); | |
93 exit(1); | |
94 } | |
95 strcpy(output_name, optarg); | |
96 break; | |
97 case 'q': | |
98 q = atoi(optarg); | |
99 if (q < 1) { | |
100 fprintf(stderr, "%s: signature strength %d out of range\n", progname, q); | |
101 exit(1); | |
102 } | |
103 break; | |
104 case 's': | |
105 if ((sig = fopen(optarg, "r")) == NULL) { | |
106 fprintf(stderr, "%s: unable to open signature file %s\n", progname, optarg); | |
107 exit(1); | |
108 } | |
109 strcpy(signature_name, optarg); | |
110 break; | |
111 case 'S': | |
112 s = atoi(optarg); | |
113 break; | |
114 case 'v': | |
115 verbose = atoi(optarg); | |
116 if (verbose < 0) { | |
117 fprintf(stderr, "%s: verbosity level %d out of range", progname, verbose); | |
118 exit(1); | |
119 } | |
120 break; | |
121 } | |
122 } | |
123 | |
124 argc -= optind; | |
125 argv += optind; | |
126 | |
127 if (argc > 1) { | |
128 usage(); | |
129 exit(1); | |
130 } | |
131 | |
132 if (argc == 1 && *argv[0] != '-') { | |
133 if ((in = fopen(argv[0], "rb")) == NULL) { | |
134 fprintf(stderr, "%s: unable to open input file %s\n", progname, argv[0]); | |
135 exit(1); | |
136 } | |
137 else | |
138 strcpy(input_name, argv[0]); | |
139 } | |
140 | |
141 if (sig) { | |
142 char line[32]; | |
143 fgets(line, sizeof(line), sig); | |
144 if (strspn(line, "KD2SG") >= 5) { | |
145 if (n == 0) | |
146 fscanf(sig, "%d\n", &n); | |
147 else | |
148 fscanf(sig, "%*d\n"); | |
149 if (q == 0) | |
150 fscanf(sig, "%d\n", &q); | |
151 else | |
152 fscanf(sig, "%*d\n"); | |
153 if (blocksize == 0) | |
154 fscanf(sig, "%d\n", &blocksize); | |
155 else | |
156 fscanf(sig, "%*d\n"); | |
157 if (e < 0) | |
158 fscanf(sig, "%d\n", &e); | |
159 else | |
160 fscanf(sig, "%*d\n"); | |
161 if (f == 0) | |
162 fscanf(sig, "%d\n", &f); | |
163 else | |
164 fscanf(sig, "%*d\n"); | |
165 if (!strcmp(F, "")) | |
166 fscanf(sig, "%[^\n\r]\n", F); | |
167 else | |
168 fscanf(sig, "%*[^\n\r]\n"); | |
169 if (l == 0) | |
170 fscanf(sig, "%d\n", &l); | |
171 else | |
172 fscanf(sig, "%*d\n"); | |
173 } | |
174 else { | |
175 fprintf(stderr, "%s: invalid signature file %s\n", progname, signature_name); | |
176 exit(1); | |
177 } | |
178 fclose(sig); | |
179 } | |
180 | |
181 if (s) | |
182 srandom(s); | |
183 else | |
184 srandom(time(NULL) * getpid()); | |
185 | |
186 if (n > 0) { | |
187 n_signature = fread(signature, sizeof(char), i = NBITSTOBYTES(n), in); | |
188 nbit_signature = NBYTESTOBITS(n_signature); | |
189 if (n_signature < i) { | |
190 fprintf(stderr, "%s: failed to read all %d signature bits from %s\n", progname, n, input_name); | |
191 exit(1); | |
192 } | |
193 } | |
194 else { | |
195 if (fscanf(in, "%128[^\n\r]", signature) == EOF) { | |
196 fprintf(stderr, "%s: failed to read signature bits from %s\n", progname, input_name); | |
197 exit(1); | |
198 } | |
199 n_signature = strlen(signature); | |
200 nbit_signature = NBYTESTOBITS(n_signature); | |
201 fprintf(stderr, "%s: got %d signature bits\n", progname, nbit_signature); | |
202 } | |
203 | |
204 fprintf(out, "KD2SG\n"); | |
205 fprintf(out, "%d\n", nbit_signature); | |
206 fprintf(out, "%d\n", q); | |
207 fprintf(out, "%d\n", blocksize); | |
208 fprintf(out, "%d\n", e); | |
209 fprintf(out, "%d\n", f); | |
210 fprintf(out, "%s\n", F); | |
211 fprintf(out, "%d\n", l); | |
212 fprintf(out, "%ld\n", random()); | |
213 binstr = malloc((nbit_signature + 1) * sizeof(char)); | |
214 sig_to_binstr(binstr); | |
215 fprintf(out, "%s\n", binstr); | |
216 free(binstr); | |
217 | |
218 fclose(out); | |
219 | |
220 exit(0); | |
221 } |