comparison Meerwald/gen_frid2_sig.c @ 0:be303a3f5ea8

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

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