0
|
1 #include "wm.h"
|
|
2 #include "signature.h"
|
|
3
|
|
4 char *progname;
|
|
5
|
|
6 void usage(void) {
|
|
7 fprintf(stderr, "usage: %s [-l n] [-n n] [-o file] [-q n] [-s file] [-S n] file\n\n", progname);
|
|
8 fprintf(stderr, "\t-h\t\tprint usage\n");
|
|
9 fprintf(stderr, "\t-l n\t\tsignature strength factor (default 5.0)\n");
|
|
10 fprintf(stderr, "\t-n n\t\twatermark bit length\n");
|
|
11 fprintf(stderr, "\t-o file\t\toutput file\n");
|
|
12 fprintf(stderr, "\t-q n\t\tquantization (JPEG quality) factor (default 90)\n");
|
|
13 fprintf(stderr, "\t-s file\t\tuse signature file's embedding information\n");
|
|
14 fprintf(stderr, "\t-S n\t\tseed\n");
|
|
15 exit(0);
|
|
16 }
|
|
17
|
|
18 int main(int argc, char *argv[]) {
|
|
19 FILE *in = stdin;
|
|
20 FILE *out = stdout;
|
|
21 FILE *sig = NULL;
|
|
22
|
|
23 char output_name[MAXPATHLEN] = "(stdout)";
|
|
24 char input_name[MAXPATHLEN] = "(stdin)";
|
|
25 char signature_name[MAXPATHLEN];
|
|
26
|
|
27 int c;
|
|
28 int i;
|
|
29 int n = 0;
|
|
30 int nb;
|
|
31 int s = 0;
|
|
32 int q = 90;
|
|
33 double l = 5.0;
|
|
34
|
|
35 progname = argv[0]; wm_init();
|
|
36
|
|
37 while ((c = getopt(argc, argv, "h?l:n:o:q:s:S:")) != EOF) {
|
|
38 switch (c) {
|
|
39 case 'h':
|
|
40 case '?':
|
|
41 usage();
|
|
42 break;
|
|
43 case 'l':
|
|
44 l = atof(optarg);
|
|
45 if (l <= 0.0) {
|
|
46 fprintf(stderr, "%s: signature strength factor %f out of range\n", progname, l);
|
|
47 exit(1);
|
|
48 }
|
|
49 break;
|
|
50 case 'n':
|
|
51 n = atoi(optarg);
|
|
52 if (n < 1 || n > 1000) {
|
|
53 fprintf(stderr, "%s: watermark length %d out of range\n", progname, n);
|
|
54 exit(1);
|
|
55 }
|
|
56 break;
|
|
57 case 'o':
|
|
58 if ((out = fopen(optarg, "wb")) == NULL) {
|
|
59 fprintf(stderr, "%s: unable to open output file %s\n", progname, optarg);
|
|
60 exit(1);
|
|
61 }
|
|
62 strcpy(output_name, optarg);
|
|
63 break;
|
|
64 case 'q':
|
|
65 q = atoi(optarg);
|
|
66 if (q <= 0 || q > 100) {
|
|
67 fprintf(stderr, "%s: quantization factor %f out of range\n", progname, q);
|
|
68 exit(1);
|
|
69 }
|
|
70 break;
|
|
71 case 's':
|
|
72 if ((sig = fopen(optarg, "r")) == NULL) {
|
|
73 fprintf(stderr, "%s: unable to open signature file %s\n", progname, optarg);
|
|
74 exit(1);
|
|
75 }
|
|
76 strcpy(signature_name, optarg);
|
|
77 break;
|
|
78 case 'S':
|
|
79 s = atoi(optarg);
|
|
80 break;
|
|
81 }
|
|
82 }
|
|
83
|
|
84 argc -= optind;
|
|
85 argv += optind;
|
|
86
|
|
87 if (argc > 1) {
|
|
88 usage();
|
|
89 exit(1);
|
|
90 }
|
|
91
|
|
92 if (argc == 1 && *argv[0] != '-')
|
|
93 if ((in = fopen(argv[0], "rb")) == NULL) {
|
|
94 fprintf(stderr, "%s: unable to open input file %s\n", progname, argv[0]);
|
|
95 exit(1);
|
|
96 }
|
|
97 else
|
|
98 strcpy(input_name, argv[0]);
|
|
99
|
|
100 if (s)
|
|
101 srandom(s);
|
|
102 else
|
|
103 srandom(time(NULL) * getpid());
|
|
104
|
|
105 if (sig) {
|
|
106 char line[128];
|
|
107 fgets(line, sizeof(line), sig);
|
|
108 if (strspn(line, "KCSG") >= 4) {
|
|
109 if (n == 0)
|
|
110 fscanf(sig, "%d\n", &n);
|
|
111 else
|
|
112 fscanf(sig, "%*d\n");
|
|
113 if (l == 0.0)
|
|
114 fscanf(sig, "%lf\n", &l);
|
|
115 else
|
|
116 fscanf(sig, "%*lf\n");
|
|
117 if (q == 0)
|
|
118 fscanf(sig, "%d\n", &q);
|
|
119 else
|
|
120 fscanf(sig, "%*d\n");
|
|
121 }
|
|
122 else {
|
|
123 fprintf(stderr, "%s: invalid signature file %s\n", progname, signature_name);
|
|
124 exit(1);
|
|
125 }
|
|
126 fclose(sig);
|
|
127 }
|
|
128
|
|
129 if (n > 0) {
|
|
130 nb = fread(signature, sizeof(char), i = NBITSTOBYTES(n), in);
|
|
131 if (nb < i) {
|
|
132 fprintf(stderr, "%s: failed to read all %d signature bits from %s\n", progname, n, input_name);
|
|
133 exit(1);
|
|
134 }
|
|
135 }
|
|
136 else {
|
|
137 if (fscanf(in, "%128[^\n\r]", signature) == EOF) {
|
|
138 fprintf(stderr, "%s: failed to read signature bits from %s\n", progname, input_name);
|
|
139 exit(1);
|
|
140 }
|
|
141 nb = strlen(signature);
|
|
142 n = NBYTESTOBITS(nb);
|
|
143 fprintf(stderr, "%s: got %d signature bits\n", progname, n);
|
|
144 }
|
|
145
|
|
146 fprintf(out, "KCSG\n");
|
|
147 fprintf(out, "%d\n", n);
|
|
148 fprintf(out, "%f\n", l);
|
|
149 fprintf(out, "%d\n", q);
|
|
150 fprintf(out, "%d\n", random());
|
|
151 fwrite(signature, sizeof(char), nb, out);
|
|
152 fprintf(out, "\n");
|
|
153
|
|
154 fclose(out);
|
|
155
|
|
156 exit(0);
|
|
157 }
|