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