comparison Meerwald-dir/wm_zhu_e.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_e.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 n] [-h] [-l n] [-o file] [-v n] -s file file\n\n", progname);
10 fprintf(stderr, "\t-a n\t\talpha factor/embedding strength\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-l n\t\tdecomposition level\n");
16 fprintf(stderr, "\t-o file\t\toutput (watermarked) file\n");
17 fprintf(stderr, "\t-s file\t\tsignature to embed in input image\n");
18 fprintf(stderr, "\t-v n\t\tverbosity level\n");
19 exit(0);
20 }
21
22 int main(int argc, char *argv[]) {
23
24 FILE *in = stdin;
25 FILE *out = stdout;
26 FILE *sig = NULL;
27
28 char output_name[MAXPATHLEN] = "(stdout)";
29 char input_name[MAXPATHLEN] = "(stdin)";
30 char signature_name[MAXPATHLEN];
31
32 int i, c, w;
33 int row;
34
35 int n;
36
37 double alpha = 0.0;
38 int level = 0;
39
40 int filter = 0;
41 int method = -1;
42 int levels;
43 char filter_name[MAXPATHLEN] = "";
44
45 int verbose = 0;
46
47 gray **image;
48 Image_tree p, dwts;
49
50 gray maxval;
51 int rows, cols, format;
52
53 double *watermark;
54
55 progname = argv[0];
56
57 pgm_init(&argc, argv); wm_init();
58
59 while ((c = getopt(argc, argv, "a:e:f:F:h?o:l:s:v:")) != EOF) {
60 switch (c) {
61 case 'a':
62 alpha = atof(optarg);
63 if (alpha <= 0.0) {
64 fprintf(stderr, "%s: alpha factor %f out of range\n", progname, alpha);
65 exit(1);
66 }
67 break;
68 case 'l':
69 level = atoi(optarg);
70 if (level <= 0) {
71 fprintf(stderr, "%s: decomposition level %d out of range\n", progname, level);
72 exit(1);
73 }
74 break;
75 case 'e':
76 method = atoi(optarg);
77 if (method < 0) {
78 fprintf(stderr, "%s: wavelet filtering method %d out of range\n", progname, method);
79 exit(1);
80 }
81 break;
82 case 'f':
83 filter = atoi(optarg);
84 if (filter <= 0) {
85 fprintf(stderr, "%s: filter number %d out of range\n", progname, filter);
86 exit(1);
87 }
88 break;
89 case 'F':
90 strcpy(filter_name, optarg);
91 break;
92 case 'h':
93 case '?':
94 usage();
95 break;
96 case 'o':
97 if ((out = fopen(optarg, "wb")) == NULL) {
98 fprintf(stderr, "%s: unable to open output file %s\n", progname, optarg);
99 exit(1);
100 }
101 strcpy(output_name, optarg);
102 break;
103 case 's':
104 if ((sig = fopen(optarg, "r")) == NULL) {
105 fprintf(stderr, "%s: unable to open signature file %s\n", progname, optarg);
106 exit(1);
107 }
108 strcpy(signature_name, optarg);
109 break;
110 case 'v':
111 verbose = atoi(optarg);
112 if (verbose < 0) {
113 fprintf(stderr, "%s: verbosity level %d out of range\n", progname, verbose);
114 exit(1);
115 }
116 break;
117 }
118 }
119
120 argc -= optind;
121 argv += optind;
122
123 if (argc > 1) {
124 usage();
125 exit(1);
126 }
127
128 if (argc == 1 && *argv[0] != '-') {
129 if ((in = fopen(argv[0], "rb")) == NULL) {
130 fprintf(stderr, "%s: unable to open input file %s\n", progname, argv[0]);
131 exit(1);
132 }
133 else
134 strcpy(input_name, argv[0]);
135 }
136
137 if (sig) {
138 char line[32];
139 fgets(line, sizeof(line), sig);
140 if (strspn(line, "ZHSG") >= 4) {
141 fscanf(sig, "%d\n", &n);
142 if (alpha == 0.0)
143 fscanf(sig, "%lf\n", &alpha);
144 else
145 fscanf(sig, "%*f\n");
146 if (level == 0)
147 fscanf(sig, "%d\n", &level);
148 else
149 fscanf(sig, "%*d\n");
150 if (method < 0)
151 fscanf(sig, "%d\n", &method);
152 else
153 fscanf(sig, "%*d\n");
154 if (filter == 0)
155 fscanf(sig, "%d\n", &filter);
156 else
157 fscanf(sig, "%*d\n");
158 if (!strcmp(filter_name, ""))
159 fscanf(sig, "%[^\n\r]\n", filter_name);
160 else
161 fscanf(sig, "%*[^\n\r]\n");
162 }
163 else {
164 fprintf(stderr, "%s: invalid signature file %s\n", progname, signature_name);
165 exit(1);
166 }
167 }
168 else {
169 fprintf(stderr, "%s: signature file not specified, use -s file option\n", progname);
170 exit(1);
171 }
172
173 watermark = malloc(n * sizeof(double));
174 for (i = 0; i < n; i++)
175 fscanf(sig, "%lf\n", &watermark[i]);
176 fclose(sig);
177
178 pgm_readpgminit(in, &cols, &rows, &maxval, &format);
179 image = pgm_allocarray(cols, rows);
180 for (row = 0; row < rows; row++)
181 pgm_readpgmrow(in, image[row], cols, maxval, format);
182 fclose(in);
183
184 // complete decomposition
185 levels = find_deepest_level(cols, rows) - 1;
186 if (level > levels) {
187 fprintf(stderr, "%s: decomposition level %d not possible (max. %d), image size is %d x %d\n", progname, level, levels, cols, rows);
188 exit(1);
189 }
190
191 // wavelet transform
192 init_dwt(cols, rows, filter_name, filter, level, method);
193 #ifdef POLLEN_STUFF
194 #include "pollen_stuff.c"
195 #endif
196 #ifdef PARAM_STUFF
197 #include "param_stuff.c"
198 #endif
199
200 dwts = fdwt(image);
201
202 p = dwts;
203 while (p->coarse) {
204 double *collected_coeffs, *largest;
205 int subband_size = p->horizontal->image->size;
206 int maxselect = MIN(3 * subband_size, n + 1);
207 double threshold;
208
209 // allocate memory for coefficient vector
210 collected_coeffs = malloc(3 * subband_size * sizeof(double));
211 if (!collected_coeffs) {
212 fprintf(stderr, "%s: malloc() failed\n", progname);
213 exit(1);
214 }
215
216 // collect coefficients from all subbands of one level into one vector
217 for (i = 0; i < subband_size; i++) {
218 collected_coeffs[3 * i + 0] = p->horizontal->image->data[i];
219 collected_coeffs[3 * i + 1] = p->vertical->image->data[i];
220 collected_coeffs[3 * i + 2] = p->diagonal->image->data[i];
221 }
222
223 // allocate memory for largest coefficients
224 largest = malloc(maxselect * sizeof(double));
225 if (!largest) {
226 fprintf(stderr, "%s: malloc() failed\n", progname);
227 exit(1);
228 }
229
230 // select largest coefficients (involves sorting)
231 select_largest_coeffs(collected_coeffs, 3 * subband_size, maxselect, largest);
232 // threshold is the smallest of the largest coefficients
233 threshold = largest[0];
234 free(largest);
235 free(collected_coeffs);
236
237 w = 0;
238 for (i = 0; i < subband_size && w < n; i++) {
239 if (p->horizontal->image->data[i] > threshold)
240 p->horizontal->image->data[i] *= (1.0 + alpha * watermark[w++]);
241 if (p->vertical->image->data[i] > threshold)
242 p->vertical->image->data[i] *= (1.0 + alpha * watermark[w++]);
243 if (p->diagonal->image->data[i] > threshold)
244 p->diagonal->image->data[i] *= (1.0 + alpha * watermark[w++]);
245 }
246
247 p = p->coarse;
248 }
249
250 free(watermark);
251 idwt(dwts, image);
252
253 pgm_writepgminit(out, cols, rows, maxval, 0);
254 for (row = 0; row < rows; row++)
255 pgm_writepgmrow(out, image[row], cols, maxval, 0);
256 fclose(out);
257
258 pgm_freearray(image, rows);
259
260 exit(0);
261 }

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