comparison Meerwald-dir/wm_zhu_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_zhu_d.c@4987db85cfae
children
comparison
equal deleted inserted replaced
23:71dd4b96221b 24:9f20bce6184e
1 #include "wm.h"
2 #include "dwt.h"
3 #include "netpbm/pgm.h"
4 #include "sort.h"
5
6 char *progname;
7
8 void usage(void) {
9 fprintf(stderr, "usage: %s [-a n] [-e n] [-f n] [-F file] [-h] [-l n] [-o file] [-v n] -s file -i file file\n\n", progname);
10 fprintf(stderr, "\t-a n\t\talpha factor\n");
11 fprintf(stderr, "\t-b n\t\tbeta factor\n");
12 fprintf(stderr, "\t-e n\t\twavelet filtering method\n");
13 fprintf(stderr, "\t-f n\t\tfilter number\n");
14 fprintf(stderr, "\t-F file\t\tfilter definition file\n");
15 fprintf(stderr, "\t-h\t\tprint usage\n");
16 fprintf(stderr, "\t-i file\t\toriginal image file\n");
17 fprintf(stderr, "\t-l n\t\tdecomposition level\n");
18 fprintf(stderr, "\t-o file\t\tfile for extracted watermark\n");
19 fprintf(stderr, "\t-s file\t\toriginal signature file\n");
20 fprintf(stderr, "\t-v n\t\tverbosity level\n");
21 exit(0);
22 }
23
24 void write_mark(FILE *out, double watermark[], int n) {
25 int i;
26
27 fprintf(out, "%d\n", n);
28 for (i = 0; i < n; i++)
29 fprintf(out, "%f\n", watermark[i]);
30 }
31
32 int main(int argc, char *argv[]) {
33
34 FILE *in = stdin;
35 FILE *out = stdout;
36 FILE *orig = NULL;
37 FILE *sig = NULL;
38
39 gray **input_image;
40 gray **orig_image;
41
42 char signature_name[MAXPATHLEN];
43 char output_name[MAXPATHLEN] = "(stdout)";
44 char input_name[MAXPATHLEN] = "(stdin)";
45 char orig_name[MAXPATHLEN];
46
47 int c, w;
48 int i;
49 int n = 0;
50 int method = -1;
51 int filter = 0;
52 char filter_name[MAXPATHLEN] = "";
53
54 int level = 0, levels;
55 double alpha = 0.0;
56
57 int in_rows, in_cols, in_format;
58 gray in_maxval;
59 int orig_rows, orig_cols, orig_format;
60 gray orig_maxval;
61 int rows, cols;
62 int row;
63
64 double *watermark;
65
66 Image_tree input_dwts, orig_dwts, p, q;
67
68 int verbose = 0;
69
70 progname = argv[0];
71
72 pgm_init(&argc, argv); wm_init2();
73
74 while ((c = getopt(argc, argv, "a:e:f:F:h?i:l:o:s:v:")) != EOF) {
75 switch (c) {
76 case 'a':
77 alpha = atof(optarg);
78 if (alpha <= 0.0) {
79 fprintf(stderr, "%s: alpha factor %f out of range\n", progname, alpha);
80 exit(1);
81 }
82 break;
83 case 'e':
84 method = atoi(optarg);
85 if (method < 0) {
86 fprintf(stderr, "%s: wavelet filtering method %d out of range\n", progname, method);
87 exit(1);
88 }
89 break;
90 case 'f':
91 filter = atoi(optarg);
92 if (filter <= 0) {
93 fprintf(stderr, "%s: filter number %d out of range\n", progname, filter);
94 exit(1);
95 }
96 break;
97 case 'F':
98 strcpy(filter_name, optarg);
99 break;
100 case 'h':
101 case '?':
102 usage();
103 break;
104 case 'i':
105 if ((orig = fopen(optarg, "rb")) == NULL) {
106 fprintf(stderr, "%s: unable to open original image file %s\n", progname, optarg);
107 exit(1);
108 }
109 strcpy(orig_name, optarg);
110 break;
111 case 'l':
112 level = atoi(optarg);
113 if (level <= 0) {
114 fprintf(stderr, "%s: decomposition level %d out of range\n", progname, level);
115 exit(1);
116 }
117 break;
118 case 'o':
119 if ((out = fopen(optarg, "w")) == NULL) {
120 fprintf(stderr, "%s: unable to open output file %s\n", progname, optarg);
121 exit(1);
122 }
123 strcpy(output_name, optarg);
124 break;
125 case 's':
126 if ((sig = fopen(optarg, "r")) == NULL) {
127 fprintf(stderr, "%s: unable to open signature file %s\n", progname, optarg);
128 exit(1);
129 }
130 strcpy(signature_name, optarg);
131 break;
132 case 'v':
133 verbose = atoi(optarg);
134 if (verbose < 0) {
135 fprintf(stderr, "%s: verbosity level %d out of range\n", progname, verbose);
136 exit(1);
137 }
138 break;
139 }
140 }
141
142 argc -= optind;
143 argv += optind;
144
145 if (argc > 1) {
146 usage();
147 exit(1);
148 }
149
150 if (argc == 1 && *argv[0] != '-') {
151 if ((in = fopen(argv[0], "rb")) == NULL) {
152 fprintf(stderr, "%s: unable to open input file %s\n", progname, argv[0]);
153 exit(1);
154 }
155 else
156 strcpy(input_name, argv[0]);
157 }
158
159 if (!orig) {
160 fprintf(stderr, "%s: original image file not specified, use -i file option\n", progname);
161 exit(1);
162 }
163
164 if (sig) {
165 char line[32];
166 fgets(line, sizeof(line), sig);
167 if (strspn(line, "ZHSG") >= 4) {
168 fscanf(sig, "%d\n", &n);
169 if (alpha == 0.0)
170 fscanf(sig, "%lf\n", &alpha);
171 else
172 fscanf(sig, "%*f\n");
173 if (level == 0)
174 fscanf(sig, "%d\n", &level);
175 else
176 fscanf(sig, "%*d\n");
177 if (method < 0)
178 fscanf(sig, "%d\n", &method);
179 else
180 fscanf(sig, "%*d\n");
181 if (filter == 0)
182 fscanf(sig, "%d\n", &filter);
183 else
184 fscanf(sig, "%*d\n");
185 if (!strcmp(filter_name, ""))
186 fscanf(sig, "%[^\n\r]\n", filter_name);
187 else
188 fscanf(sig, "%*[^\n\r]\n");
189 }
190 else {
191 fprintf(stderr, "%s: invalid signature file %s\n", progname, signature_name);
192 exit(1);
193 }
194 fclose(sig);
195 }
196 else {
197 fprintf(stderr, "%s: signature file not specified, use -s file option\n", progname);
198 exit(1);
199 }
200
201 pgm_readpgminit(in, &in_cols, &in_rows, &in_maxval, &in_format);
202 pgm_readpgminit(orig, &orig_cols, &orig_rows, &orig_maxval, &orig_format);
203
204 if (in_cols != orig_cols || in_rows != orig_rows) {
205 fprintf(stderr, "%s: input image %s does not match dimensions of original image %s\n", progname, input_name, orig_name);
206 exit(1);
207 }
208
209 cols = in_cols;
210 rows = in_rows;
211
212 input_image = pgm_allocarray(in_cols, in_rows);
213 orig_image = pgm_allocarray(orig_cols, orig_rows);
214
215 for (row = 0; row < in_rows; row++) {
216 pgm_readpgmrow(in, input_image[row], in_cols, in_maxval, in_format);
217 pgm_readpgmrow(orig, orig_image[row], orig_cols, orig_maxval, orig_format);
218 }
219
220 fclose(in);
221 fclose(orig);
222
223 // complete decomposition
224 levels = find_deepest_level(cols, rows) - 1;
225 if (level > levels) {
226 fprintf(stderr, "%s: decomposition level %d not possible (max. %d), image size is %d x %d\n", progname, level, levels, cols, rows);
227 exit(1);
228 }
229
230 init_dwt(cols, rows, filter_name, filter, levels, 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 watermark = malloc(n * sizeof(double));
242 if (!watermark) {
243 fprintf(stderr, "%s: malloc() failed\n\n", progname);
244 exit(1);
245 }
246
247 fprintf(out, "ZHWM\n");
248 fprintf(out, "%d\n", n);
249 fprintf(out, "%d\n", level);
250
251 p = input_dwts;
252 q = orig_dwts;
253 while (p->coarse && q->coarse) {
254 double *collected_coeffs, *largest;
255 int subband_size = q->horizontal->image->size;
256 int maxselect = MIN(3 * subband_size, n + 1);
257 double threshold;
258
259 collected_coeffs = malloc(3 * subband_size * sizeof(double));
260 if (!collected_coeffs) {
261 fprintf(stderr, "%s: malloc() failed\n", progname);
262 exit(1);
263 }
264
265 for (i = 0; i < subband_size; i++) {
266 collected_coeffs[3 * i + 0] = q->horizontal->image->data[i];
267 collected_coeffs[3 * i + 1] = q->vertical->image->data[i];
268 collected_coeffs[3 * i + 2] = q->diagonal->image->data[i];
269 }
270
271 largest = malloc(maxselect * sizeof(double));
272 if (!largest) {
273 fprintf(stderr, "%s: malloc() failed\n", progname);
274 exit(1);
275 }
276
277 select_largest_coeffs(collected_coeffs, 3 * subband_size, maxselect, largest);
278 threshold = largest[0];
279 free(largest);
280 free(collected_coeffs);
281
282 w = 0;
283 for (i = 0; i < subband_size && w < n; i++) {
284 if (q->horizontal->image->data[i] > threshold)
285 watermark[w++] = (p->horizontal->image->data[i] / q->horizontal->image->data[i] - 1.0) / alpha;
286 if (q->vertical->image->data[i] > threshold)
287 watermark[w++] = (p->vertical->image->data[i] / q->vertical->image->data[i] - 1.0) / alpha;
288 if (q->diagonal->image->data[i] > threshold)
289 watermark[w++] = (p->diagonal->image->data[i] / q->diagonal->image->data[i] - 1.0) / alpha;
290 }
291
292 write_mark(out, watermark, w);
293
294 p = p->coarse;
295 q = q->coarse;
296 }
297
298 fclose(out);
299
300 free(watermark);
301
302 pgm_freearray(input_image, rows);
303 pgm_freearray(orig_image, rows);
304
305 exit(0);
306 }

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