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

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