Mercurial > hg > wm
comparison Meerwald/wm_zhu_e.c @ 0:be303a3f5ea8
import
author | Peter Meerwald <pmeerw@cosy.sbg.ac.at> |
---|---|
date | Sun, 12 Aug 2007 13:14:34 +0200 |
parents | |
children | acb6967ee76d |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:be303a3f5ea8 |
---|---|
1 #include "wm.h" | |
2 #include "dwt.h" | |
3 #include "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, col; | |
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, colors, 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 if (sig) { | |
137 char line[32]; | |
138 fgets(line, sizeof(line), sig); | |
139 if (strspn(line, "ZHSG") >= 4) { | |
140 fscanf(sig, "%d\n", &n); | |
141 if (alpha == 0.0) | |
142 fscanf(sig, "%lf\n", &alpha); | |
143 else | |
144 fscanf(sig, "%*lf\n"); | |
145 if (level == 0) | |
146 fscanf(sig, "%d\n", &level); | |
147 else | |
148 fscanf(sig, "%*d\n"); | |
149 if (method < 0) | |
150 fscanf(sig, "%d\n", &method); | |
151 else | |
152 fscanf(sig, "%*d\n"); | |
153 if (filter == 0) | |
154 fscanf(sig, "%d\n", &filter); | |
155 else | |
156 fscanf(sig, "%*d\n"); | |
157 if (!strcmp(filter_name, "")) | |
158 fscanf(sig, "%[^\n\r]\n", &filter_name); | |
159 else | |
160 fscanf(sig, "%*[^\n\r]\n"); | |
161 } | |
162 else { | |
163 fprintf(stderr, "%s: invalid signature file %s\n", progname, signature_name); | |
164 exit(1); | |
165 } | |
166 } | |
167 else { | |
168 fprintf(stderr, "%s: signature file not specified, use -s file option\n", progname); | |
169 exit(1); | |
170 } | |
171 | |
172 watermark = malloc(n * sizeof(double)); | |
173 for (i = 0; i < n; i++) | |
174 fscanf(sig, "%lf\n", &watermark[i]); | |
175 fclose(sig); | |
176 | |
177 pgm_readpgminit(in, &cols, &rows, &maxval, &format); | |
178 image = pgm_allocarray(cols, rows); | |
179 for (row = 0; row < rows; row++) | |
180 pgm_readpgmrow(in, image[row], cols, maxval, format); | |
181 fclose(in); | |
182 | |
183 // complete decomposition | |
184 levels = find_deepest_level(cols, rows) - 1; | |
185 if (level > levels) { | |
186 fprintf(stderr, "%s: decomposition level %d not possible (max. %d), image size is %d x %d\n", progname, level, levels, cols, rows); | |
187 exit(1); | |
188 } | |
189 | |
190 // wavelet transform | |
191 init_dwt(cols, rows, filter_name, filter, level, method); | |
192 #ifdef POLLEN_STUFF | |
193 #include "pollen_stuff.xxx" | |
194 #endif | |
195 #ifdef PARAM_STUFF | |
196 #include "param_stuff.xxx" | |
197 #endif | |
198 | |
199 dwts = fdwt(image); | |
200 | |
201 p = dwts; | |
202 while (p->coarse) { | |
203 double *collected_coeffs, *largest; | |
204 int subband_size = p->horizontal->image->size; | |
205 int maxselect = MIN(3 * subband_size, n + 1); | |
206 double threshold; | |
207 | |
208 // allocate memory for coefficient vector | |
209 collected_coeffs = malloc(3 * subband_size * sizeof(double)); | |
210 if (!collected_coeffs) { | |
211 fprintf(stderr, "%s: malloc() failed\n", progname); | |
212 exit(1); | |
213 } | |
214 | |
215 // collect coefficients from all subbands of one level into one vector | |
216 for (i = 0; i < subband_size; i++) { | |
217 collected_coeffs[3 * i + 0] = p->horizontal->image->data[i]; | |
218 collected_coeffs[3 * i + 1] = p->vertical->image->data[i]; | |
219 collected_coeffs[3 * i + 2] = p->diagonal->image->data[i]; | |
220 } | |
221 | |
222 // allocate memory for largest coefficients | |
223 largest = malloc(maxselect * sizeof(double)); | |
224 if (!largest) { | |
225 fprintf(stderr, "%s: malloc() failed\n", progname); | |
226 exit(1); | |
227 } | |
228 | |
229 // select largest coefficients (involves sorting) | |
230 select_largest_coeffs(collected_coeffs, 3 * subband_size, maxselect, largest); | |
231 // threshold is the smallest of the largest coefficients | |
232 threshold = largest[0]; | |
233 free(largest); | |
234 free(collected_coeffs); | |
235 | |
236 w = 0; | |
237 for (i = 0; i < subband_size && w < n; i++) { | |
238 if (p->horizontal->image->data[i] > threshold) | |
239 p->horizontal->image->data[i] *= (1.0 + alpha * watermark[w++]); | |
240 if (p->vertical->image->data[i] > threshold) | |
241 p->vertical->image->data[i] *= (1.0 + alpha * watermark[w++]); | |
242 if (p->diagonal->image->data[i] > threshold) | |
243 p->diagonal->image->data[i] *= (1.0 + alpha * watermark[w++]); | |
244 } | |
245 | |
246 p = p->coarse; | |
247 } | |
248 | |
249 free(watermark); | |
250 idwt(dwts, image); | |
251 | |
252 pgm_writepgminit(out, cols, rows, maxval, 0); | |
253 for (row = 0; row < rows; row++) | |
254 pgm_writepgmrow(out, image[row], cols, maxval, 0); | |
255 fclose(out); | |
256 | |
257 pgm_freearray(image, rows); | |
258 | |
259 exit(0); | |
260 } |