comparison Meerwald-dir/wm_corvi_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_corvi_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
5 char *progname;
6
7 void usage(void) {
8 fprintf(stderr, "usage: %s [-a n] [-e n] [-f n] [-F n] [-h] [-o file] [-v n] -s file file\n\n", progname);
9 fprintf(stderr, "\t-a n\t\talpha factor/embedding strength\n");
10 fprintf(stderr, "\t-e n\t\twavelet filtering method\n");
11 fprintf(stderr, "\t-f n\t\tfilter number\n");
12 fprintf(stderr, "\t-F file\t\tfilter definition file\n");
13 fprintf(stderr, "\t-h\t\tprint usage\n");
14 fprintf(stderr, "\t-o file\t\toutput (watermarked) file\n");
15 fprintf(stderr, "\t-s file\t\tsignature to embed in input image\n");
16 fprintf(stderr, "\t-v n\t\tverbosity level\n");
17 exit(0);
18 }
19
20 int main(int argc, char *argv[]) {
21
22 FILE *in = stdin;
23 FILE *out = stdout;
24 FILE *sig = NULL;
25
26 char output_name[MAXPATHLEN] = "(stdout)";
27 char input_name[MAXPATHLEN] = "(stdin)";
28 char signature_name[MAXPATHLEN];
29
30 int c;
31 int row, col;
32
33 int n;
34
35 double alpha = 0.0;
36
37 int filter = 0;
38 int method = -1;
39 int level = 0;
40 char filter_name[MAXPATHLEN] = "";
41
42 int verbose = 0;
43
44 gray **image;
45 Image_tree dwts;
46
47 gray maxval;
48 int rows, cols, format;
49
50 progname = argv[0];
51
52 pgm_init(&argc, argv); wm_init();
53
54 while ((c = getopt(argc, argv, "a:e:f:F:h?o:s:v:")) != EOF) {
55 switch (c) {
56 case 'a':
57 alpha = atof(optarg);
58 if (alpha <= 0.0) {
59 fprintf(stderr, "%s: alpha factor %f out of range\n", progname, alpha);
60 exit(1);
61 }
62 break;
63 case 'e':
64 method = atoi(optarg);
65 if (method < 0) {
66 fprintf(stderr, "%s: wavelet filtering method %d out of range\n", progname, method);
67 exit(1);
68 }
69 break;
70 case 'f':
71 filter = atoi(optarg);
72 if (filter <= 0) {
73 fprintf(stderr, "%s: filter number %d out of range\n", progname, filter);
74 exit(1);
75 }
76 break;
77 case 'F':
78 strcpy(filter_name, optarg);
79 break;
80 case 'h':
81 case '?':
82 usage();
83 break;
84 case 'o':
85 if ((out = fopen(optarg, "wb")) == NULL) {
86 fprintf(stderr, "%s: unable to open output file %s\n", progname, optarg);
87 exit(1);
88 }
89 strcpy(output_name, optarg);
90 break;
91 case 's':
92 if ((sig = fopen(optarg, "r")) == NULL) {
93 fprintf(stderr, "%s: unable to open signature file %s\n", progname, optarg);
94 exit(1);
95 }
96 strcpy(signature_name, optarg);
97 break;
98 case 'v':
99 verbose = atoi(optarg);
100 if (verbose < 0) {
101 fprintf(stderr, "%s: verbosity level %d out of range\n", progname, verbose);
102 exit(1);
103 }
104 break;
105 }
106 }
107
108 argc -= optind;
109 argv += optind;
110
111 if (argc > 1) {
112 usage();
113 exit(1);
114 }
115
116 if (argc == 1 && *argv[0] != '-') {
117 if ((in = fopen(argv[0], "rb")) == NULL) {
118 fprintf(stderr, "%s: unable to open input file %s\n", progname, argv[0]);
119 exit(1);
120 }
121 else
122 strcpy(input_name, argv[0]);
123 }
124
125 if (sig) {
126 char line[32];
127 fgets(line, sizeof(line), sig);
128 if (strspn(line, "CVSG") >= 4) {
129 fscanf(sig, "%d\n", &n);
130 if (alpha == 0.0)
131 fscanf(sig, "%lf\n", &alpha);
132 else
133 fscanf(sig, "%*f\n");
134 if (method < 0)
135 fscanf(sig, "%d\n", &method);
136 else
137 fscanf(sig, "%*d\n");
138 if (filter == 0)
139 fscanf(sig, "%d\n", &filter);
140 else
141 fscanf(sig, "%*d\n");
142 if (!strcmp(filter_name, ""))
143 fscanf(sig, "%[^\n\r]\n", filter_name);
144 else
145 fscanf(sig, "%*[^\n\r]\n");
146 }
147 else {
148 fprintf(stderr, "%s: invalid signature file %s\n", progname, signature_name);
149 exit(1);
150 }
151 }
152 else {
153 fprintf(stderr, "%s: signature file not specified, use -s file option\n", progname);
154 exit(1);
155 }
156
157 pgm_readpgminit(in, &cols, &rows, &maxval, &format);
158
159 image = pgm_allocarray(cols, rows);
160
161 for (row = 0; row < rows; row++)
162 pgm_readpgmrow(in, image[row], cols, maxval, format);
163
164 fclose(in);
165
166 level = 0;
167 row = rows;
168 col = cols;
169 while (n < row * col / 4.0 && row >= 2 && col >= 2) {
170 row /= 2;
171 col /= 2;
172 level++;
173 }
174
175 if (verbose >= 2) {
176 fprintf(stderr, "%s: embedding into coarse image (x %d/y %d) at level %d\n", progname, col, row, level);
177 }
178
179 init_dwt(cols, rows, filter_name, filter, level, method);
180 #ifdef POLLEN_STUFF
181 #include "pollen_stuff.c"
182 #endif
183 #ifdef PARAM_STUFF
184 #include "param_stuff.c"
185 #endif
186
187 dwts = fdwt(image);
188
189 {
190 Image_tree p = dwts;
191 Image img;
192 double med;
193
194 while (!p->image)
195 p = p->coarse;
196
197 img = p->image;
198
199 med = 0.0;
200 for (row = 0; row < img->height; row++)
201 for (col = 0; col < img->width; col++)
202 med += get_pixel(img, col, row);
203
204 med /= (double) (img->height * img->width);
205
206 row = 0;
207 col = 0;
208 while (n > 0) {
209 double pix;
210 double g;
211
212 fscanf(sig, "%lf\n", &g);
213
214 pix = get_pixel(img, col, row);
215 pix = med + (pix - med) * (1.0 + alpha * g);
216 set_pixel(img, col, row, pix);
217
218 if (++col == img->width) { col = 0; row++; }
219 n--;
220 }
221 }
222
223 fclose(sig);
224
225 idwt(dwts, image);
226
227 pgm_writepgminit(out, cols, rows, maxval, 0);
228
229 for (row = 0; row < rows; row++)
230 pgm_writepgmrow(out, image[row], cols, maxval, 0);
231
232 fclose(out);
233
234 pgm_freearray(image, rows);
235
236 exit(0);
237 }

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