comparison Meerwald-dir/wm_wang_d.c @ 24:9f20bce6184e v0.7

move directories, support netpbm 11
author Peter Meerwald-Stadler <pmeerw@pmeerw.net>
date Fri, 20 Dec 2024 13:08:59 +0100
parents Meerwald/wm_wang_d.c@4987db85cfae
children
comparison
equal deleted inserted replaced
23:71dd4b96221b 24:9f20bce6184e
1 #include "wm.h"
2 #include "dwt.h"
3 #include "wang_common.h"
4 #include "dwt_util.h"
5 #include "netpbm/pgm.h"
6
7 char *progname;
8
9 void usage(void) {
10 fprintf(stderr, "usage: %s [-a n] [-b n] [-e n] [-f n] [-F file] [-h] [-n n] [-o file] [-v n] -s file -i file file\n\n", progname);
11 fprintf(stderr, "\t-a n\t\talpha factor\n");
12 fprintf(stderr, "\t-b n\t\tbeta factor\n");
13 fprintf(stderr, "\t-e n\t\twavelet filtering method\n");
14 fprintf(stderr, "\t-f n\t\tfilter number\n");
15 fprintf(stderr, "\t-F file\t\tfilter definition file\n");
16 fprintf(stderr, "\t-h\t\tprint usage\n");
17 fprintf(stderr, "\t-i file\t\toriginal image file\n");
18 fprintf(stderr, "\t-n n\t\twatermark length\n");
19 fprintf(stderr, "\t-o file\t\tfile for extracted watermark\n");
20 fprintf(stderr, "\t-s file\t\toriginal signature file\n");
21 fprintf(stderr, "\t-v n\t\tverbosity level\n");
22 exit(0);
23 }
24
25 int main(int argc, char *argv[]) {
26
27 FILE *in = stdin;
28 FILE *out = stdout;
29 FILE *orig = NULL;
30 FILE *sig = NULL;
31
32 gray **input_image;
33 gray **orig_image;
34
35 char signature_name[MAXPATHLEN];
36 char output_name[MAXPATHLEN] = "(stdout)";
37 char input_name[MAXPATHLEN] = "(stdin)";
38 char orig_name[MAXPATHLEN];
39
40 int c, w;
41 int n = 0;
42 int method = -1;
43 int filter = 0;
44 char filter_name[MAXPATHLEN] = "";
45
46 int level = 0;
47 double alpha = 0.0;
48 double beta = 0.0;
49
50 int in_rows, in_cols, in_format;
51 gray in_maxval;
52 int orig_rows, orig_cols, orig_format;
53 gray orig_maxval;
54 int rows, cols;
55 int row;
56
57 double *watermark;
58
59 Image_tree input_dwts;
60 Image_tree orig_dwts;
61
62 int verbose = 0;
63
64 progname = argv[0];
65
66 pgm_init(&argc, argv); wm_init2();
67
68 while ((c = getopt(argc, argv, "a:b:e:f:F:h?i:n:o:s:v:")) != EOF) {
69 switch (c) {
70 case 'a':
71 alpha = atof(optarg);
72 if (alpha <= 0.0) {
73 fprintf(stderr, "%s: alpha factor %f out of range\n", progname, alpha);
74 exit(1);
75 }
76 break;
77 case 'b':
78 beta = atof(optarg);
79 if (beta <= 0.0) {
80 fprintf(stderr, "%s: beta factor %f out of range\n", progname, alpha);
81 exit(1);
82 }
83 break;
84 case 'e':
85 method = atoi(optarg);
86 if (method < 0) {
87 fprintf(stderr, "%s: wavelet filtering method %d out of range\n", progname, method);
88 exit(1);
89 }
90 break;
91 case 'f':
92 filter = atoi(optarg);
93 if (filter <= 0) {
94 fprintf(stderr, "%s: filter number %d out of range\n", progname, filter);
95 exit(1);
96 }
97 break;
98 case 'F':
99 strcpy(filter_name, optarg);
100 break;
101 case 'h':
102 case '?':
103 usage();
104 break;
105 case 'i':
106 if ((orig = fopen(optarg, "rb")) == NULL) {
107 fprintf(stderr, "%s: unable to open original image file %s\n", progname, optarg);
108 exit(1);
109 }
110 strcpy(orig_name, optarg);
111 break;
112 case 'n':
113 n = atoi(optarg);
114 if (n < 1 || n > 32000) {
115 fprintf(stderr, "%s: watermark length %d out of range\n", progname, n);
116 exit(1);
117 }
118 break;
119 case 'o':
120 if ((out = fopen(optarg, "w")) == NULL) {
121 fprintf(stderr, "%s: unable to open output file %s\n", progname, optarg);
122 exit(1);
123 }
124 strcpy(output_name, optarg);
125 break;
126 case 's':
127 if ((sig = fopen(optarg, "r")) == NULL) {
128 fprintf(stderr, "%s: unable to open signature file %s\n", progname, optarg);
129 exit(1);
130 }
131 strcpy(signature_name, optarg);
132 break;
133 case 'v':
134 verbose = atoi(optarg);
135 if (verbose < 0) {
136 fprintf(stderr, "%s: verbosity level %d out of range\n", progname, verbose);
137 exit(1);
138 }
139 break;
140 }
141 }
142
143 argc -= optind;
144 argv += optind;
145
146 if (argc > 1) {
147 usage();
148 exit(1);
149 }
150
151 if (argc == 1 && *argv[0] != '-') {
152 if ((in = fopen(argv[0], "rb")) == NULL) {
153 fprintf(stderr, "%s: unable to open input file %s\n", progname, argv[0]);
154 exit(1);
155 }
156 else
157 strcpy(input_name, argv[0]);
158 }
159
160 if (!orig) {
161 fprintf(stderr, "%s: original image file not specified, use -i file option\n", progname);
162 exit(1);
163 }
164
165 if (sig) {
166 char line[32];
167 fgets(line, sizeof(line), sig);
168 if (strspn(line, "WGSG") >= 4) {
169 fscanf(sig, "%d\n", &n);
170 if (alpha == 0.0)
171 fscanf(sig, "%lf\n", &alpha);
172 else
173 fscanf(sig, "%*f\n");
174 if (beta == 0.0)
175 fscanf(sig, "%lf\n", &beta);
176 else
177 fscanf(sig, "%*f\n");
178 if (method < 0)
179 fscanf(sig, "%d\n", &method);
180 else
181 fscanf(sig, "%*d\n");
182 if (filter == 0)
183 fscanf(sig, "%d\n", &filter);
184 else
185 fscanf(sig, "%*d\n");
186 if (!strcmp(filter_name, ""))
187 fscanf(sig, "%[^\n\r]\n", filter_name);
188 else
189 fscanf(sig, "%*[^\n\r]\n");
190 }
191 else {
192 fprintf(stderr, "%s: invalid signature file %s\n", progname, signature_name);
193 exit(1);
194 }
195 fclose(sig);
196 }
197 else {
198 fprintf(stderr, "%s: signature file not specified, use -s file option\n", progname);
199 exit(1);
200 }
201
202 watermark = malloc(n * sizeof(double));
203
204 pgm_readpgminit(in, &in_cols, &in_rows, &in_maxval, &in_format);
205 pgm_readpgminit(orig, &orig_cols, &orig_rows, &orig_maxval, &orig_format);
206
207 if (in_cols != orig_cols || in_rows != orig_rows) {
208 fprintf(stderr, "%s: input image %s does not match dimensions of original image %s\n", progname, input_name, orig_name);
209 exit(1);
210 }
211
212 cols = in_cols;
213 rows = in_rows;
214
215 input_image = pgm_allocarray(in_cols, in_rows);
216
217 orig_image = pgm_allocarray(orig_cols, orig_rows);
218
219 for (row = 0; row < in_rows; row++) {
220 pgm_readpgmrow(in, input_image[row], in_cols, in_maxval, in_format);
221 pgm_readpgmrow(orig, orig_image[row], orig_cols, orig_maxval, orig_format);
222 }
223
224 fclose(in);
225 fclose(orig);
226
227 // complete decomposition
228 level = find_deepest_level(cols, rows) - 1;
229
230 init_dwt(cols, rows, filter_name, filter, level, method);
231 #ifdef POLLEN_STUFF
232 #include "pollen_stuff.c"
233 #endif
234 #ifdef PARAM_STUFF
235 #include "param_stuff.c"
236 #endif
237
238 input_dwts = fdwt(input_image);
239 orig_dwts = fdwt(orig_image);
240
241 // build tree for subband selection, calculate subband thresholds
242 init_subbands(orig_dwts);
243 set_subbands_type_beta(HORIZONTAL, beta);
244 set_subbands_type_beta(VERTICAL, beta);
245 calc_subbands_threshold();
246
247 fprintf(out, "WGWM\n");
248 fprintf(out, "%d\n", n);
249
250 w = 0;
251 while (w < n) {
252 Subband_data s;
253
254 // select subband with max. threshold
255 s = select_subband();
256 if (verbose > 1)
257 fprintf(stderr, "%s: selected subband %s%d, T=%lf, beta=%lf\n", progname, subband_name(s->type), s->level, s->T, s->beta);
258
259 // watermark significant coefficients and set them selected
260 // check is entire signature has been embedded
261 c = select_subband_coeff(s);
262 do {
263 Pixel p;
264 Pixel q;
265 if (c < 0)
266 // no more significant coefficients in subband
267 break;
268
269 p = get_subband_coeff(s, c);
270 if (p < s->Cmax) {
271 q = get_dwt_coeff(input_dwts, s->level, s->type, c);
272 watermark[w] = (q - p) / (alpha * s->beta * s->T);
273 fprintf(out, "%lf\n", watermark[w]);
274
275 if (verbose > 2)
276 fprintf(stderr, "%s: detected sig. coeff. #%d (= %lf)\n from %s%d coeff. #%d\n",
277 progname, w, watermark[w], subband_name(s->type), s->level, c);
278
279 w++;
280 }
281 mark_subband_coeff(s, c);
282
283 // select next significant coefficient
284 c = select_subband_coeff_from(s, c);
285 } while (w < n);
286
287 // update subband threshold
288 s->T /= 2.0;
289
290 }
291
292 fclose(out);
293
294 free_subbands();
295
296 free(watermark);
297
298 pgm_freearray(input_image, rows);
299 pgm_freearray(orig_image, rows);
300
301 exit(0);
302 }

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