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

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