comparison Meerwald/wm_frid2_e.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 "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 int n;
34 double normalization = 1024.0;
35 int seed, format;
36
37 double alpha = 0.0;
38 double gamma = 0.0;
39
40 gray **image;
41 double **coeffs;
42
43 double mean, derivation;
44 double mult_factor;
45
46 int rows, cols;
47 int verbose = 0;
48 gray maxval;
49
50 progname = argv[0];
51
52 pgm_init(&argc, argv); wm_init();
53
54 while ((c = getopt(argc, argv, "a:g:n:h?o:s:v:")) != EOF) {
55 switch (c) {
56 case 'a':
57 alpha = atof(optarg);
58 if (alpha <= 0.0) {
59 fprintf(stderr, "%s: alpha factor %f out of range\n", progname, alpha);
60 exit(1);
61 }
62 break;
63 case 'g':
64 gamma = atof(optarg);
65 if (gamma <= 0.0) {
66 fprintf(stderr, "%s: gamma factor %f out of range\n", progname, alpha);
67 exit(1);
68 }
69 break;
70 case 'n':
71 normalization = atof(optarg);
72 if (normalization < 0) {
73 fprintf(stderr, "%s: normalisation factor %f out of range\n", progname, normalization);
74 exit(1);
75 }
76 break;
77 case 'h':
78 case '?':
79 usage();
80 break;
81 case 'o':
82 if ((out = fopen(optarg, "wb")) == NULL) {
83 fprintf(stderr, "%s: unable to open output file %s\n", progname, optarg);
84 exit(1);
85 }
86 strcpy(output_name, optarg);
87 break;
88 case 's':
89 if ((sig = fopen(optarg, "r")) == NULL) {
90 fprintf(stderr, "%s: unable to open signature file %s\n", progname, optarg);
91 exit(1);
92 }
93 strcpy(signature_name, optarg);
94 break;
95 case 'v':
96 verbose = atoi(optarg);
97 if (verbose < 0) {
98 fprintf(stderr, "%s: verbosity level %d out of range\n", progname, verbose);
99 exit(1);
100 }
101 break;
102 }
103 }
104
105 argc -= optind;
106 argv += optind;
107
108 if (argc > 1) {
109 usage();
110 exit(1);
111 }
112
113 if (argc == 1 && *argv[0] != '-')
114 if ((in = fopen(argv[0], "rb")) == NULL) {
115 fprintf(stderr, "%s: unable to open input file %s\n", progname, argv[0]);
116 exit(1);
117 }
118 else
119 strcpy(input_name, argv[0]);
120
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 }

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