0
|
1 #include "wm.h"
|
|
2 #include "dct.h"
|
|
3 #include "pgm.h"
|
|
4 #include "signature.h"
|
|
5 #include "frid2_common.h"
|
|
6
|
|
7 char *progname;
|
|
8
|
|
9 void usage(void) {
|
|
10 fprintf(stderr, "usage: %s [-a n] [-g n] [-n n] [-h] [-o file] [-v n] -s file file\n\n", progname);
|
|
11 fprintf(stderr, "\t-a n\t\talpha factor low freq. embedding strength\n");
|
|
12 fprintf(stderr, "\t-g n\t\tgamma factor med freq. embedding strength\n");
|
|
13 fprintf(stderr, "\t-h\t\tprint usage\n");
|
|
14 fprintf(stderr, "\t-n n\t\tnormalisation factor (default 1024.0)\n");
|
|
15 fprintf(stderr, "\t-o file\t\toutput (watermarked) file\n");
|
|
16 fprintf(stderr, "\t-s file\t\tsignature to embed in input image\n");
|
|
17 fprintf(stderr, "\t-v n\t\tverbosity level\n");
|
|
18 exit(0);
|
|
19 }
|
|
20
|
|
21 int main(int argc, char *argv[]) {
|
|
22
|
|
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 c;
|
|
32 int row, col;
|
|
33 double normalization = 1024.0;
|
|
34 int seed, format;
|
|
35
|
|
36 double alpha = 0.0;
|
|
37 double gamma = 0.0;
|
|
38
|
|
39 gray **image;
|
|
40 double **coeffs;
|
|
41
|
|
42 double mean, derivation;
|
|
43 double mult_factor;
|
|
44
|
|
45 int rows, cols;
|
|
46 int verbose = 0;
|
|
47 gray maxval;
|
|
48
|
|
49 progname = argv[0];
|
|
50
|
|
51 pgm_init(&argc, argv); wm_init();
|
|
52
|
|
53 while ((c = getopt(argc, argv, "a:g:n:h?o:s:v:")) != EOF) {
|
|
54 switch (c) {
|
|
55 case 'a':
|
|
56 alpha = atof(optarg);
|
|
57 if (alpha <= 0.0) {
|
|
58 fprintf(stderr, "%s: alpha factor %f out of range\n", progname, alpha);
|
|
59 exit(1);
|
|
60 }
|
|
61 break;
|
|
62 case 'g':
|
|
63 gamma = atof(optarg);
|
|
64 if (gamma <= 0.0) {
|
|
65 fprintf(stderr, "%s: gamma factor %f out of range\n", progname, alpha);
|
|
66 exit(1);
|
|
67 }
|
|
68 break;
|
|
69 case 'n':
|
|
70 normalization = atof(optarg);
|
|
71 if (normalization < 0) {
|
|
72 fprintf(stderr, "%s: normalisation factor %f out of range\n", progname, normalization);
|
|
73 exit(1);
|
|
74 }
|
|
75 break;
|
|
76 case 'h':
|
|
77 case '?':
|
|
78 usage();
|
|
79 break;
|
|
80 case 'o':
|
|
81 if ((out = fopen(optarg, "wb")) == NULL) {
|
|
82 fprintf(stderr, "%s: unable to open output file %s\n", progname, optarg);
|
|
83 exit(1);
|
|
84 }
|
|
85 strcpy(output_name, optarg);
|
|
86 break;
|
|
87 case 's':
|
|
88 if ((sig = fopen(optarg, "r")) == NULL) {
|
|
89 fprintf(stderr, "%s: unable to open signature file %s\n", progname, optarg);
|
|
90 exit(1);
|
|
91 }
|
|
92 strcpy(signature_name, optarg);
|
|
93 break;
|
|
94 case 'v':
|
|
95 verbose = atoi(optarg);
|
|
96 if (verbose < 0) {
|
|
97 fprintf(stderr, "%s: verbosity level %d out of range\n", progname, verbose);
|
|
98 exit(1);
|
|
99 }
|
|
100 break;
|
|
101 }
|
|
102 }
|
|
103
|
|
104 argc -= optind;
|
|
105 argv += optind;
|
|
106
|
|
107 if (argc > 1) {
|
|
108 usage();
|
|
109 exit(1);
|
|
110 }
|
|
111
|
8
|
112 if (argc == 1 && *argv[0] != '-') {
|
0
|
113 if ((in = fopen(argv[0], "rb")) == NULL) {
|
|
114 fprintf(stderr, "%s: unable to open input file %s\n", progname, argv[0]);
|
|
115 exit(1);
|
|
116 }
|
|
117 else
|
|
118 strcpy(input_name, argv[0]);
|
8
|
119 }
|
|
120
|
0
|
121 if (sig) {
|
|
122 char line[32];
|
|
123 fgets(line, sizeof(line), sig);
|
|
124 if (strspn(line, "FR2SG") >= 5) {
|
|
125 fscanf(sig, "%d\n", &nbit_signature);
|
|
126 if (alpha == 0.0)
|
|
127 fscanf(sig, "%lf\n", &alpha);
|
|
128 else
|
|
129 fscanf(sig, "%*lf\n");
|
|
130 if (gamma == 0.0)
|
|
131 fscanf(sig, "%lf\n", &gamma);
|
|
132 else
|
|
133 fscanf(sig, "%*lf\n");
|
|
134
|
|
135 fscanf(sig, "%d\n", &seed);
|
|
136 n_signature = NBITSTOBYTES(nbit_signature);
|
|
137 fread(signature, sizeof(char), n_signature, sig);
|
|
138 fscanf(sig, "\n");
|
|
139 srandom(seed);
|
|
140 }
|
|
141 else {
|
|
142 fprintf(stderr, "%s: invalid signature file %s\n", progname, signature_name);
|
|
143 exit(1);
|
|
144 }
|
|
145 fclose(sig);
|
|
146 }
|
|
147 else {
|
|
148 fprintf(stderr, "%s: signature file not specified, use -s file option\n", progname);
|
|
149 exit(1);
|
|
150 }
|
|
151
|
|
152 pgm_readpgminit(in, &cols, &rows, &maxval, &format);
|
|
153
|
|
154 if (rows == cols)
|
|
155 init_dct_NxN(cols, rows);
|
|
156 else
|
|
157 init_dct_NxM(cols, rows);
|
|
158
|
|
159 image = pgm_allocarray(cols, rows);
|
|
160 for (row = 0; row < rows; row++)
|
|
161 pgm_readpgmrow(in, image[row], cols, maxval, format);
|
|
162 fclose(in);
|
|
163
|
|
164 // calculate mean value
|
|
165 mean = 0.0;
|
|
166 for (row = 0; row < rows; row++)
|
|
167 for (col = 0; col < cols; col++)
|
|
168 mean += image[row][col];
|
|
169
|
|
170 mean /= cols * rows;
|
|
171
|
|
172 // calculate derivation
|
|
173 derivation = 0.0;
|
|
174 for (row = 0; row < rows; row++)
|
|
175 for (col = 0; col < cols; col++)
|
|
176 derivation += sqr(image[row][col] - mean);
|
|
177
|
|
178 derivation = sqrt(derivation / (cols * rows - 1));
|
|
179 mult_factor = normalization / (sqrt(cols * rows) * derivation);
|
|
180
|
|
181 if (verbose > 5)
|
|
182 fprintf(stderr, "%s: mean %f, derivation %f, mult_factor %f\n", progname, mean, derivation, mult_factor);
|
|
183
|
|
184 // normalize image
|
|
185 coeffs = alloc_coeffs(cols, rows);
|
|
186 for (row = 0; row < rows; row++)
|
|
187 for (col = 0; col < cols; col++)
|
|
188 coeffs[row][col] = (image[row][col] - mean) * mult_factor;
|
|
189
|
|
190 if (cols == rows)
|
|
191 fdct_inplace_NxN(coeffs);
|
|
192 // else
|
|
193 // fdct_NxM(image, dcts);
|
|
194
|
|
195 embed_low_freq(coeffs, cols, rows, alpha, verbose);
|
|
196 embed_med_freq(coeffs, cols, rows, gamma, seed, verbose);
|
|
197
|
|
198 if (cols == rows)
|
|
199 idct_inplace_NxN(coeffs);
|
|
200 // else
|
|
201 // idct_NxM(dcts, image);
|
|
202
|
|
203 for (row = 0; row < rows; row++)
|
|
204 for (col = 0; col < cols; col++)
|
|
205 image[row][col] = PIXELRANGE(coeffs[row][col] / mult_factor + mean + 0.5);
|
|
206
|
|
207 free_coeffs(coeffs);
|
|
208
|
|
209 pgm_writepgminit(out, cols, rows, maxval, 0);
|
|
210 for (row = 0; row < rows; row++)
|
|
211 pgm_writepgmrow(out, image[row], cols, maxval, 0);
|
|
212
|
|
213 fclose(out);
|
|
214
|
|
215 pgm_freearray(image, rows);
|
|
216
|
|
217 exit(0);
|
|
218 }
|