annotate Meerwald/wm_xie_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
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
1 #include "wm.h"
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
2 #include "signature.h"
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
3 #include "dwt.h"
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
4 #include "pgm.h"
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
5
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
6 char *progname;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
7
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
8 // watermarking transformation, set median pixel to quantization boundary
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
9 double wm_transform(double alpha, double f1, double f2, double f3, int x) {
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
10 double s = alpha * (fabs(f3) - fabs(f1)) / 2.0;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
11 double l = x ? (f1 + s) : f1;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
12 double f2new;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
13
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
14 while ((l + 2 * s) < f2) l += 2 * s;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
15 f2new = ((f2 - l) < (l + 2 * s - f2)) ? l : (l + 2 * s);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
16
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
17 return f2new;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
18 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
19
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
20 void usage(void) {
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
21 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);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
22 fprintf(stderr, "\t-a n\t\tembedding strength (default 0.05)\n");
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
23 fprintf(stderr, "\t-e n\t\twavelet filtering method\n");
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
24 fprintf(stderr, "\t-f n\t\tfilter number\n");
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
25 fprintf(stderr, "\t-F file\t\tfilter definition file\n");
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
26 fprintf(stderr, "\t-l n\t\tembedding level\n");
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
27 fprintf(stderr, "\t-h\t\tprint usage\n");
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
28 fprintf(stderr, "\t-o file\t\toutput (watermarked) file\n");
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
29 fprintf(stderr, "\t-s file\t\tsignature to embed in input image\n");
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
30 fprintf(stderr, "\t-v n\t\tverbosity level\n");
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
31 exit(0);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
32 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
33
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
34 int main(int argc, char *argv[]) {
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
35
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
36 FILE *in = stdin;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
37 FILE *out = stdout;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
38 FILE *sig = NULL;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
39
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
40 char output_name[MAXPATHLEN] = "(stdout)";
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
41 char input_name[MAXPATHLEN] = "(stdin)";
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
42 char signature_name[MAXPATHLEN];
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
43
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
44 int c, n;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
45 int row, col;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
46
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
47 double alpha = 0.0;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
48 int filter = 0;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
49 int method = -1;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
50 int level = 0;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
51 char filter_name[MAXPATHLEN] = "";
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
52
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
53 int seed;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
54 int verbose = 0;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
55
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
56 gray **image;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
57 Image_tree dwts, p;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
58
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
59 gray maxval;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
60 int rows, cols, colors, format;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
61
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
62 progname = argv[0];
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
63
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
64 pgm_init(&argc, argv); wm_init();
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
65
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
66 while ((c = getopt(argc, argv, "a:e:f:F:h?l:o:s:v:")) != EOF) {
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
67 switch (c) {
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
68 case 'e':
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
69 method = atoi(optarg);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
70 if (method < 0) {
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
71 fprintf(stderr, "%s: wavelet filtering method %d out of range\n", progname, method);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
72 exit(1);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
73 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
74 break;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
75 case 'f':
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
76 filter = atoi(optarg);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
77 if (filter <= 0) {
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
78 fprintf(stderr, "%s: filter number %d out of range\n", progname, filter);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
79 exit(1);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
80 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
81 break;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
82 case 'F':
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
83 strcpy(filter_name, optarg);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
84 break;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
85 case 'h':
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
86 case '?':
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
87 usage();
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
88 break;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
89 case 'l':
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
90 level = atoi(optarg);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
91 if (level < 1) {
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
92 fprintf(stderr, "%s: embedding level out of range\n", progname);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
93 exit(1);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
94 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
95 break;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
96 case 'o':
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
97 if ((out = fopen(optarg, "wb")) == NULL) {
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
98 fprintf(stderr, "%s: unable to open output file %s\n", progname, optarg);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
99 exit(1);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
100 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
101 strcpy(output_name, optarg);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
102 break;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
103 case 'a':
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
104 alpha = atof(optarg);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
105 if (alpha <= 0.0) {
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
106 fprintf(stderr, "%s: embedding strength factor %f out of range\n", progname, alpha);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
107 exit(1);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
108 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
109 break;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
110 case 's':
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
111 if ((sig = fopen(optarg, "r")) == NULL) {
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
112 fprintf(stderr, "%s: unable to open signature file %s\n", progname, optarg);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
113 exit(1);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
114 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
115 strcpy(signature_name, optarg);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
116 break;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
117 case 'v':
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
118 verbose = atoi(optarg);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
119 if (verbose < 0) {
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
120 fprintf(stderr, "%s: verbosity level %d out of range\n", progname, verbose);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
121 exit(1);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
122 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
123 break;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
124 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
125 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
126
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
127 argc -= optind;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
128 argv += optind;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
129
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
130 if (argc > 1) {
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
131 usage();
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
132 exit(1);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
133 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
134
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
135 if (argc == 1 && *argv[0] != '-')
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
136 if ((in = fopen(argv[0], "rb")) == NULL) {
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
137 fprintf(stderr, "%s: unable to open input file %s\n", progname, argv[0]);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
138 exit(1);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
139 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
140 else
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
141 strcpy(input_name, argv[0]);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
142
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
143 if (sig) {
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
144 char line[32];
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
145 fgets(line, sizeof(line), sig);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
146 if (strspn(line, "XESG") >= 4) {
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
147 fscanf(sig, "%d\n", &nbit_signature);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
148 if (alpha == 0.0)
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
149 fscanf(sig, "%lf\n", &alpha);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
150 else
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
151 fscanf(sig, "%*lf\n");
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
152 if (method < 0)
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
153 fscanf(sig, "%d\n", &method);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
154 else
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
155 fscanf(sig, "%*d\n");
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
156 if (filter == 0)
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
157 fscanf(sig, "%d\n", &filter);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
158 else
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
159 fscanf(sig, "%*d\n");
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
160 if (!strcmp(filter_name, ""))
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
161 fscanf(sig, "%[^\n\r]\n", &filter_name);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
162 else
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
163 fscanf(sig, "%*[^\n\r]\n");
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
164 if (level == 0)
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
165 fscanf(sig, "%d\n", &level);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
166 else
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
167 fscanf(sig, "%*d\n");
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
168 fscanf(sig, "%d\n", &seed);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
169 srandom(seed);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
170 n_signature = NBITSTOBYTES(nbit_signature);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
171 fread(signature, sizeof(char), n_signature, sig);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
172 fscanf(sig, "\n");
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
173 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
174 else {
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
175 fprintf(stderr, "%s: invalid signature file %s\n", progname, signature_name);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
176 exit(1);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
177 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
178 fclose(sig);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
179 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
180 else {
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
181 fprintf(stderr, "%s: signature file not specified, use -s file option\n", progname);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
182 exit(1);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
183 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
184
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
185 pgm_readpgminit(in, &cols, &rows, &maxval, &format);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
186
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
187 if (verbose > 0)
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
188 fprintf(stderr, "%s: embedding %d bits with strength %f in\n"
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
189 " %d x %d host image, decomposition level %d\n",
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
190 progname, nbit_signature, alpha, cols, rows, level);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
191
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
192 image = pgm_allocarray(cols, rows);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
193
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
194 for (row = 0; row < rows; row++)
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
195 pgm_readpgmrow(in, image[row], cols, maxval, format);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
196
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
197 fclose(in);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
198
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
199 // decomposition of image
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
200 init_dwt(cols, rows, filter_name, filter, level, method);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
201 #ifdef POLLEN_STUFF
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
202 #include "pollen_stuff.xxx"
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
203 #endif
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
204 #ifdef PARAM_STUFF
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
205 #include "param_stuff.xxx"
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
206 #endif
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
207
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
208 dwts = fdwt(image);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
209
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
210 p = dwts;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
211
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
212 // consider each resolution level
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
213 while (p->level < level)
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
214 // descend one level
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
215 p = p->coarse;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
216
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
217 // repeat binary watermark by sliding a 3-pixel window of approximation image
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
218 n = 0;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
219 for (row = 0; row < p->image->height; row++) {
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
220 for (col = 0; col < p->image->width - 3; col += 3) {
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
221 double b1, b2, b3;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
222 double *f1 = &b1, *f2 = &b2, *f3 = &b3;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
223 double f2new;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
224
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
225 // get all three approximation pixels in window
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
226 b1 = get_pixel(p->image, col + 0, row);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
227 b2 = get_pixel(p->image, col + 1, row);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
228 b3 = get_pixel(p->image, col + 2, row);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
229
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
230 // bring selected pixels in ascending order
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
231 #define SWAP(A, B) {double *t = A; A = B; B = t;}
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
232 if (*f1 > *f2) SWAP(f1, f2);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
233 if (*f2 > *f3) SWAP(f2, f3);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
234 if (*f1 > *f2) SWAP(f1, f2);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
235
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
236 // apply watermarking transformation (modify median pixel)
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
237 f2new = wm_transform(alpha, *f1, *f2, *f3, get_signature_bit(n % nbit_signature));
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
238
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
239 if (verbose > 1)
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
240 fprintf(stderr, "%s: embedding #%d (= %d) at (%d/%d); %f < %f -> %f < %f\n",
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
241 progname, n, get_signature_bit(n % nbit_signature), col, row, *f1, *f2, f2new, *f3);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
242
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
243 *f2 = f2new;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
244
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
245 // write modified pixel
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
246 set_pixel(p->image, col + 0, row, b1);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
247 set_pixel(p->image, col + 1, row, b2);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
248 set_pixel(p->image, col + 2, row, b3);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
249
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
250 n++;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
251 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
252 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
253
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
254
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
255 // skip over some pixels
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
256
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
257 idwt(dwts, image);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
258
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
259 pgm_writepgminit(out, cols, rows, maxval, 0);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
260
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
261 for (row = 0; row < rows; row++)
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
262 pgm_writepgmrow(out, image[row], cols, maxval, 0);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
263
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
264 fclose(out);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
265
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
266 pgm_freearray(image, rows);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
267
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
268 exit(0);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
269 }

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