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
|
8
|
123 if (argc == 1 && *argv[0] != '-') {
|
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]);
|
8
|
130 }
|
0
|
131
|
|
132 if (sig) {
|
|
133 char line[32];
|
|
134 fgets(line, sizeof(line), sig);
|
|
135 if (strspn(line, "XESG") >= 4) {
|
|
136 if (n == 0)
|
|
137 fscanf(sig, "%d\n", &n);
|
|
138 else
|
|
139 fscanf(sig, "%*d\n");
|
|
140 if (a == 0.0)
|
|
141 fscanf(sig, "%lf\n", &a);
|
|
142 else
|
|
143 fscanf(sig, "%*lf\n");
|
|
144 if (e < 0)
|
|
145 fscanf(sig, "%d\n", &e);
|
|
146 else
|
|
147 fscanf(sig, "%*d\n");
|
|
148 if (f == 0)
|
|
149 fscanf(sig, "%d\n", &f);
|
|
150 else
|
|
151 fscanf(sig, "%*d\n");
|
|
152 if (!strcmp(F, ""))
|
|
153 fscanf(sig, "%[^\n\r]\n", &F);
|
|
154 else
|
|
155 fscanf(sig, "%*[^\n\r]\n");
|
|
156 if (l == 0)
|
|
157 fscanf(sig, "%d\n", &l);
|
|
158 else
|
|
159 fscanf(sig, "%*d\n");
|
|
160 }
|
|
161 else {
|
|
162 fprintf(stderr, "%s: invalid signature file %s\n", progname, signature_name);
|
|
163 exit(1);
|
|
164 }
|
8
|
165 fclose(sig);
|
0
|
166 }
|
|
167
|
|
168 if (s)
|
|
169 srandom(s);
|
|
170 else
|
|
171 srandom(time(NULL) * getpid());
|
|
172
|
|
173 if (n > 0) {
|
|
174 nb = fread(signature, sizeof(char), i = NBITSTOBYTES(n), in);
|
|
175 if (nb < i) {
|
|
176 fprintf(stderr, "%s: failed to read all %d signature bits from %s\n", progname, n, input_name);
|
|
177 exit(1);
|
|
178 }
|
|
179 }
|
|
180 else {
|
|
181 if (fscanf(in, "%128[^\n\r]", signature) == EOF) {
|
|
182 fprintf(stderr, "%s: failed to read signature bits from %s\n", progname, input_name);
|
|
183 exit(1);
|
|
184 }
|
|
185 nb = strlen(signature);
|
|
186 n = NBYTESTOBITS(nb);
|
|
187 fprintf(stderr, "%s: got %d signature bits\n", progname, n);
|
|
188 }
|
|
189
|
|
190 fprintf(out, "XESG\n");
|
|
191 fprintf(out, "%d\n", n);
|
|
192 fprintf(out, "%f\n", a);
|
|
193 fprintf(out, "%d\n", e);
|
|
194 fprintf(out, "%d\n", f);
|
|
195 fprintf(out, "%s\n", F);
|
|
196 fprintf(out, "%d\n", l);
|
8
|
197 fprintf(out, "%ld\n", random());
|
0
|
198 fwrite(signature, sizeof(char), nb, out);
|
|
199 fprintf(out, "\n");
|
|
200
|
|
201 fclose(out);
|
|
202
|
|
203 exit(0);
|
|
204 }
|