Mercurial > hg > wm
annotate Meerwald/wm_kim_e.c @ 20:bd669312f068
suppress warnings, fix link errors
author | Peter Meerwald-Stadler <pmeerw@pmeerw.net> |
---|---|
date | Sat, 28 Jan 2023 23:54:58 +0100 |
parents | 4987db85cfae |
children |
rev | line source |
---|---|
0 | 1 #include "wm.h" |
2 #include "dwt.h" | |
3 #include "pgm.h" | |
4 #include "dwt_util.h" | |
5 #include "kim_common.h" | |
6 | |
7 char *progname; | |
8 | |
9 void usage(void) { | |
10 fprintf(stderr, "usage: %s [-a n] [-A n] [-e n] [-f n] [-F n] [-h] [-l n] [-o file] [-v n] -s file file\n\n", progname); | |
11 fprintf(stderr, "\t-a n\t\talpha factor/embedding strength for detail subbands\n"); | |
12 fprintf(stderr, "\t-a n\t\talpha factor/embedding strength for approximation subband\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-l n\t\tdecomposition level\n"); | |
18 fprintf(stderr, "\t-o file\t\toutput (watermarked) file\n"); | |
19 fprintf(stderr, "\t-s file\t\tsignature to embed in input image\n"); | |
20 fprintf(stderr, "\t-v n\t\tverbosity level\n"); | |
21 exit(0); | |
22 } | |
23 | |
24 int mark_subband(Image_tree s, int name, double alpha, double watermark[], double threshold, int w, int n, int verbose) { | |
25 int i, j; | |
26 | |
27 for (i = 5; i < s->image->height-5; i++) | |
28 for (j = 5; j < s->image->width-5; j++) { | |
29 double coeff, newcoeff; | |
30 | |
31 coeff = get_pixel(s->image, i, j); | |
32 if (fabs(coeff) > threshold) { | |
33 newcoeff = coeff + alpha * coeff * watermark[w % n]; | |
34 set_pixel(s->image, i, j, newcoeff); | |
35 | |
36 if (verbose >= 9) | |
37 fprintf(stderr, "%s: (%d/%d) %f: %f -> %f\n", progname, j, i, watermark[w % n], coeff, newcoeff); | |
38 | |
39 w++; | |
40 } | |
41 } | |
42 | |
43 if (verbose > 5) | |
44 fprintf(stderr, "%s: watermarking %s%d, size %d x %d; embedded %d coeffs. total\n", | |
45 progname, subband_name(name), s->level, s->image->width, s->image->height, w); | |
46 | |
47 return w; | |
48 } | |
49 | |
50 int main(int argc, char *argv[]) { | |
51 | |
52 FILE *in = stdin; | |
53 FILE *out = stdout; | |
54 FILE *sig = NULL; | |
55 | |
56 char output_name[MAXPATHLEN] = "(stdout)"; | |
57 char input_name[MAXPATHLEN] = "(stdin)"; | |
58 char signature_name[MAXPATHLEN]; | |
59 | |
60 int i, c, w; | |
8 | 61 int row; |
0 | 62 |
63 int n; | |
64 | |
65 double alpha_detail = 0.0; | |
66 double alpha_approx = 0.0; | |
67 int level = 0; | |
68 | |
69 int filter = 0; | |
70 int method = -1; | |
71 int levels; | |
72 char filter_name[MAXPATHLEN] = ""; | |
73 | |
74 int verbose = 0; | |
75 | |
76 gray **image; | |
77 Image_tree p, dwts; | |
78 | |
79 gray maxval; | |
8 | 80 int rows, cols, format; |
0 | 81 |
82 double *watermark; | |
83 | |
84 progname = argv[0]; | |
85 | |
86 pgm_init(&argc, argv); wm_init(); | |
87 | |
88 while ((c = getopt(argc, argv, "a:A:e:f:F:h?o:l:s:v:")) != EOF) { | |
89 switch (c) { | |
90 case 'a': | |
91 alpha_detail = atof(optarg); | |
92 if (alpha_detail <= 0.0) { | |
93 fprintf(stderr, "%s: alpha factor %f out of range\n", progname, alpha_detail); | |
94 exit(1); | |
95 } | |
96 break; | |
97 case 'A': | |
98 alpha_approx = atof(optarg); | |
99 if (alpha_approx <= 0.0) { | |
100 fprintf(stderr, "%s: alpha factor %f out of range\n", progname, alpha_approx); | |
101 exit(1); | |
102 } | |
103 break; | |
104 case 'l': | |
105 level = atoi(optarg); | |
106 if (level <= 0) { | |
107 fprintf(stderr, "%s: decomposition level %d out of range\n", progname, level); | |
108 exit(1); | |
109 } | |
110 break; | |
111 case 'e': | |
112 method = atoi(optarg); | |
113 if (method < 0) { | |
114 fprintf(stderr, "%s: wavelet filtering method %d out of range\n", progname, method); | |
115 exit(1); | |
116 } | |
117 break; | |
118 case 'f': | |
119 filter = atoi(optarg); | |
120 if (filter <= 0) { | |
121 fprintf(stderr, "%s: filter number %d out of range\n", progname, filter); | |
122 exit(1); | |
123 } | |
124 break; | |
125 case 'F': | |
126 strcpy(filter_name, optarg); | |
127 break; | |
128 case 'h': | |
129 case '?': | |
130 usage(); | |
131 break; | |
132 case 'o': | |
133 if ((out = fopen(optarg, "wb")) == NULL) { | |
134 fprintf(stderr, "%s: unable to open output file %s\n", progname, optarg); | |
135 exit(1); | |
136 } | |
137 strcpy(output_name, optarg); | |
138 break; | |
139 case 's': | |
140 if ((sig = fopen(optarg, "r")) == NULL) { | |
141 fprintf(stderr, "%s: unable to open signature file %s\n", progname, optarg); | |
142 exit(1); | |
143 } | |
144 strcpy(signature_name, optarg); | |
145 break; | |
146 case 'v': | |
147 verbose = atoi(optarg); | |
148 if (verbose < 0) { | |
149 fprintf(stderr, "%s: verbosity level %d out of range\n", progname, verbose); | |
150 exit(1); | |
151 } | |
152 break; | |
153 } | |
154 } | |
155 | |
156 argc -= optind; | |
157 argv += optind; | |
158 | |
159 if (argc > 1) { | |
160 usage(); | |
161 exit(1); | |
162 } | |
163 | |
8 | 164 if (argc == 1 && *argv[0] != '-') { |
0 | 165 if ((in = fopen(argv[0], "rb")) == NULL) { |
166 fprintf(stderr, "%s: unable to open input file %s\n", progname, argv[0]); | |
167 exit(1); | |
168 } | |
169 else | |
170 strcpy(input_name, argv[0]); | |
8 | 171 } |
172 | |
0 | 173 if (sig) { |
174 char line[32]; | |
175 fgets(line, sizeof(line), sig); | |
176 if (strspn(line, "KISG") >= 4) { | |
177 fscanf(sig, "%d\n", &n); | |
178 if (alpha_detail == 0.0) | |
179 fscanf(sig, "%lf\n", &alpha_detail); | |
180 else | |
16
4987db85cfae
fix another scanf() warning
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
15
diff
changeset
|
181 fscanf(sig, "%*f\n"); |
0 | 182 if (alpha_approx == 0.0) |
183 fscanf(sig, "%lf\n", &alpha_approx); | |
184 else | |
16
4987db85cfae
fix another scanf() warning
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
15
diff
changeset
|
185 fscanf(sig, "%*f\n"); |
0 | 186 if (level == 0) |
187 fscanf(sig, "%d\n", &level); | |
188 else | |
189 fscanf(sig, "%*d\n"); | |
190 if (method < 0) | |
191 fscanf(sig, "%d\n", &method); | |
192 else | |
193 fscanf(sig, "%*d\n"); | |
194 if (filter == 0) | |
195 fscanf(sig, "%d\n", &filter); | |
196 else | |
197 fscanf(sig, "%*d\n"); | |
198 if (!strcmp(filter_name, "")) | |
15 | 199 fscanf(sig, "%[^\n\r]\n", filter_name); |
0 | 200 else |
201 fscanf(sig, "%*[^\n\r]\n"); | |
202 } | |
203 else { | |
204 fprintf(stderr, "%s: invalid signature file %s\n", progname, signature_name); | |
205 exit(1); | |
206 } | |
207 } | |
208 else { | |
209 fprintf(stderr, "%s: signature file not specified, use -s file option\n", progname); | |
210 exit(1); | |
211 } | |
212 | |
213 watermark = malloc(n * sizeof(double)); | |
214 for (i = 0; i < n; i++) | |
215 fscanf(sig, "%lf\n", &watermark[i]); | |
216 fclose(sig); | |
217 | |
218 pgm_readpgminit(in, &cols, &rows, &maxval, &format); | |
219 image = pgm_allocarray(cols, rows); | |
220 for (row = 0; row < rows; row++) | |
221 pgm_readpgmrow(in, image[row], cols, maxval, format); | |
222 fclose(in); | |
223 | |
224 // complete decomposition | |
225 levels = find_deepest_level(cols, rows) - 1; | |
226 if (level > levels) { | |
227 fprintf(stderr, "%s: decomposition level %d not possible (max. %d), image size is %d x %d\n", progname, level, levels, cols, rows); | |
228 exit(1); | |
229 } | |
230 | |
231 // wavelet transform | |
232 init_dwt(cols, rows, filter_name, filter, level, method); | |
233 #ifdef POLLEN_STUFF | |
3 | 234 #include "pollen_stuff.c" |
0 | 235 #endif |
236 #ifdef PARAM_STUFF | |
3 | 237 #include "param_stuff.c" |
0 | 238 #endif |
239 | |
240 dwts = fdwt(image); | |
241 | |
242 p = dwts; | |
243 w = 0; | |
244 | |
245 // process each decomposition level | |
246 while (p->coarse) { | |
247 int current_level; | |
248 double threshold; | |
249 double max_coeff; | |
250 double alpha; | |
251 | |
252 // get current decomposition level number | |
253 current_level = p->horizontal->level; | |
254 | |
255 // find largest absolute coefficient in detail subbands of current decomposition level | |
256 max_coeff = find_level_largest_coeff(p, verbose); | |
257 | |
258 // calculate significance threshold for current decomposition level | |
259 threshold = calc_level_threshold(max_coeff, verbose); | |
260 | |
261 // calculate embedding strength alpha for current decomposition level | |
262 alpha = calc_level_alpha_detail(alpha_detail, level, current_level, verbose); | |
263 | |
264 if (verbose > 1) | |
265 fprintf(stderr, "%s: level %d, threshold %f, alpha %f\n", progname, current_level, threshold, alpha); | |
266 | |
267 // embed watermark sequence into detail subbands of current decomposition level | |
268 w = mark_subband(p->horizontal, HORIZONTAL, alpha, watermark, threshold, w, n, verbose); | |
269 w = mark_subband(p->vertical, VERTICAL, alpha, watermark, threshold, w, n, verbose); | |
270 w = mark_subband(p->diagonal, DIAGONAL, alpha, watermark, threshold, w, n, verbose); | |
271 | |
272 p = p->coarse; | |
273 } | |
274 | |
275 // mark approximation image using calculated significance threshold and embedding strength | |
276 w = mark_subband(p, COARSE, alpha_approx, watermark, calc_level_threshold(find_subband_largest_coeff(p, COARSE, verbose), verbose), w, n, verbose); | |
277 | |
278 free(watermark); | |
279 idwt(dwts, image); | |
280 | |
281 pgm_writepgminit(out, cols, rows, maxval, 0); | |
282 for (row = 0; row < rows; row++) | |
283 pgm_writepgmrow(out, image[row], cols, maxval, 0); | |
284 fclose(out); | |
285 | |
286 pgm_freearray(image, rows); | |
287 | |
288 exit(0); | |
289 } |