comparison Meerwald-dir/gen_bruyn_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_bruyn_sig.c@4987db85cfae
children
comparison
equal deleted inserted replaced
23:71dd4b96221b 24:9f20bce6184e
1 #include "wm.h"
2 #include "signature.h"
3 #include "bruyn_common.h"
4
5 char *progname;
6
7 void usage(void) {
8 fprintf(stderr, "usage: %s [-b n] [-k] [-n n] [-o file] [-pP n] [-q n] [-s file] [-S n] [-tT n] file\n\n", progname);
9 fprintf(stderr, "\t-b n\t\tblock size (default 8)\n");
10 fprintf(stderr, "\t-h\t\tprint usage\n");
11 fprintf(stderr, "\t-k\t\tdisable block skipping\n");
12 fprintf(stderr, "\t-n n\t\twatermark bit length\n");
13 fprintf(stderr, "\t-o file\t\toutput file\n");
14 fprintf(stderr, "\t-p n\t\tpattern type for zone 1 (default 1, 1.." NPATTERN_USAGE ")\n");
15 fprintf(stderr, "\t-P n\t\tpattern type for zone 2 (default 2, 1.." NPATTERN_USAGE ")\n");
16 fprintf(stderr, "\t-q n\t\tsignature strength (default 7.0)\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-t n\t\tthreshold for noise (default " THRESHOLD_NOISE_USAGE ")\n");
20 fprintf(stderr, "\t-T n\t\tthreshold for slope (default " THRESHOLD_SLOPE_USAGE ")\n");
21 exit(0);
22 }
23
24 int main(int argc, char *argv[]) {
25 FILE *in = stdin;
26 FILE *out = stdout;
27 FILE *sig = NULL;
28
29 char output_name[MAXPATHLEN] = "(stdout)";
30 char input_name[MAXPATHLEN] = "(stdin)";
31 char signature_name[MAXPATHLEN];
32
33 int c;
34 int i;
35 int b = 8;
36 int n = 0;
37 int nb;
38 int s = 0;
39 int p1 = 1;
40 int p2 = 2;
41 double t1 = THRESHOLD_NOISE;
42 double t2 = THRESHOLD_SLOPE;
43 double q = 7.0;
44 int skipping = 0;
45
46 progname = argv[0];
47 wm_init();
48
49 while ((c = getopt(argc, argv, "b:h?n:o:p:P:q:s:S:t:T:k")) != EOF) {
50 switch (c) {
51 case 'b':
52 b = atoi(optarg);
53 if (b <= 0) {
54 fprintf(stderr, "%s: block size %d out of range\n", progname, b);
55 exit(1);
56 }
57 break;
58 case 'k':
59 skipping = 1;
60 break;
61 case 'h':
62 case '?':
63 usage();
64 break;
65 case 'n':
66 n = atoi(optarg);
67 if (n < 1 || n > 1000) {
68 fprintf(stderr, "%s: watermark length %d out of range\n", progname, n);
69 exit(1);
70 }
71 break;
72 case 'o':
73 if ((out = fopen(optarg, "wb")) == NULL) {
74 fprintf(stderr, "%s: unable to open output file %s\n", progname, optarg);
75 exit(1);
76 }
77 strcpy(output_name, optarg);
78 break;
79 case 'p':
80 p1 = atoi(optarg);
81 if (p1 <= 0 || p1 > NPATTERN) {
82 fprintf(stderr, "%s: pattern type out of range\n", progname);
83 exit(1);
84 }
85 break;
86 case 'P':
87 p2 = atoi(optarg);
88 if (p2 <= 0 || p2 > NPATTERN) {
89 fprintf(stderr, "%s: pattern type out of range\n", progname);
90 exit(1);
91 }
92 break;
93 case 'q':
94 q = atof(optarg);
95 if (q <= 0.0) {
96 fprintf(stderr, "%s: signature strength factor %f out of range\n", progname, q);
97 exit(1);
98 }
99 break;
100 case 't':
101 t1 = atof(optarg);
102 if (t1 <= 0) {
103 fprintf(stderr, "%s: noise threshold %f out of range\n", progname, t1);
104 }
105 break;
106 case 'T':
107 t2 = atof(optarg);
108 if (t2 <= 0) {
109 fprintf(stderr, "%s: slope threshold %f out of range\n", progname, t2);
110 }
111 break;
112 case 's':
113 if ((sig = fopen(optarg, "r")) == NULL) {
114 fprintf(stderr, "%s: unable to open signature file %s\n", progname, optarg);
115 exit(1);
116 }
117 strcpy(signature_name, optarg);
118 break;
119 case 'S':
120 s = atoi(optarg);
121 break;
122 }
123 }
124
125 argc -= optind;
126 argv += optind;
127
128 if (argc > 1) {
129 usage();
130 exit(1);
131 }
132
133 if (b % 2 > 0 || b <= 2) {
134 fprintf(stderr, "%s: block size has to be even and greater than 2\n", progname);
135 exit(1);
136 }
137
138 if (argc == 1 && *argv[0] != '-') {
139 if ((in = fopen(argv[0], "rb")) == NULL) {
140 fprintf(stderr, "%s: unable to open input file %s\n", progname, argv[0]);
141 exit(1);
142 }
143 else
144 strcpy(input_name, argv[0]);
145 }
146
147 // read signature file and set options
148 // command line options override signature file options
149 if (sig) {
150 char line[128];
151 fgets(line, sizeof(line), sig);
152 if (strspn(line, "BRSG") >= 4) {
153 if (n == 0)
154 fscanf(sig, "%d\n", &n);
155 else
156 fscanf(sig, "%*d\n");
157 if (skipping == 0)
158 fscanf(sig, "%d\n", &skipping);
159 else
160 fscanf(sig, "%*d\n");
161 if (p1 == 0)
162 fscanf(sig, "%d\n", &p1);
163 else
164 fscanf(sig, "%*d\n");
165 if (p2 == 0)
166 fscanf(sig, "%d\n", &p2);
167 else
168 fscanf(sig, "%*d\n");
169 if (q == 0.0)
170 fscanf(sig, "%lf\n", &q);
171 else
172 fscanf(sig, "%*f\n");
173 if (t1 == 0.0)
174 fscanf(sig, "%lf\n", &t1);
175 else
176 fscanf(sig, "%*f\n");
177 if (t2 == 0.0)
178 fscanf(sig, "%lf\n", &t2);
179 else
180 fscanf(sig, "%*f\n");
181 if (b == 0)
182 fscanf(sig, "%d\n", &b);
183 else
184 fscanf(sig, "%*d\n");
185 }
186 else {
187 fprintf(stderr, "%s: invalid signature file %s\n", progname, signature_name);
188 exit(1);
189 }
190 fclose(sig);
191 }
192
193 if (s)
194 srandom(s);
195 else
196 srandom(time(NULL) * getpid());
197
198 if (n > 0) {
199 nb = fread(signature, sizeof(char), i = NBITSTOBYTES(n), in);
200 if (nb < i) {
201 fprintf(stderr, "%s: failed to read all %d signature bits from %s\n", progname, n, input_name);
202 exit(1);
203 }
204 }
205 else {
206 if (fscanf(in, "%128[^\n\r]", signature) == EOF) {
207 fprintf(stderr, "%s: failed to read signature bits from %s\n", progname, input_name);
208 exit(1);
209 }
210 nb = strlen(signature);
211 n = NBYTESTOBITS(nb);
212 fprintf(stderr, "%s: got %d signature bits\n", progname, n);
213 }
214
215
216 fprintf(out, "BRSG\n");
217 fprintf(out, "%d\n", n);
218 fprintf(out, "%d\n", skipping);
219 fprintf(out, "%d\n", p1);
220 fprintf(out, "%d\n", p2);
221 fprintf(out, "%f\n", q);
222 fprintf(out, "%f\n", t1);
223 fprintf(out, "%f\n", t2);
224 fprintf(out, "%d\n", b);
225 fprintf(out, "%ld\n", random());
226 fwrite(signature, sizeof(char), nb, out);
227 fprintf(out, "\n");
228
229 fclose(out);
230
231 exit(0);
232 }

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