0
|
1 /* Watermarking program - Subband DCT Transform based */
|
|
2 /* Module : Casting */
|
|
3 /* Author : Vassilis Fotopoulos */
|
|
4 /* Date : 25/4/2000 */
|
|
5 /* Revision : 7.0 */
|
|
6 /* Developed at : ELLAB */
|
|
7 /* Electronics Laboratory */
|
|
8 /* Department of Physics */
|
|
9 /* University of Patras - GREECE */
|
|
10 /* Copyleft (c) 1999 */
|
|
11 /*------------------------------------------------------*/
|
|
12 /* pseudorandom noise generator's code is */
|
|
13 /* taken from "Numerical Recipes in C" */
|
|
14 /* FCT implementation from University of Bath */
|
|
15 /*------------------------------------------------------*/
|
|
16 #include <stdio.h>
|
|
17 #include <stdlib.h>
|
|
18 #include <string.h>
|
|
19 #include <math.h>
|
|
20 #include <float.h>
|
|
21 #include <getopt.h>
|
|
22 #include "common.h"
|
|
23
|
|
24 void add_hor_add_ver(double **in, int N, double **out);
|
|
25 void add_hor_sub_ver(double **in, int N, double **out);
|
|
26 void sub_hor_add_ver(double **in, int N, double **out);
|
|
27 void sub_hor_sub_ver(double **in, int N, double **out);
|
|
28 void band_synthesis(double **ll, double **lh, double **hl, double **hh, int N, double **s);
|
|
29 void watermark(double **i, int N, long key, long int L, long int M, double a);
|
|
30
|
|
31 double cu[1024];
|
|
32 double cv[1024];
|
|
33 int height, width;
|
|
34
|
|
35 //--------------------------------------------------------
|
|
36 void add_hor_add_ver(double **in, int N, double **out)
|
|
37 {
|
|
38 double **temp;
|
|
39 int r, c;
|
|
40 temp = dmatrix(N, N);
|
|
41 for (r = 0; r < N; r++)
|
|
42 for (c = 0; c < N / 2; c++)
|
|
43 temp[r][c] = (in[r][2 * c] + in[r][2 * c + 1]) / 2;
|
|
44 for (c = 0; c < N / 2; c++)
|
|
45 for (r = 0; r < N / 2; r++)
|
|
46 out[r][c] = (temp[2 * r][c] + temp[2 * r + 1][c]) / 2;
|
|
47 freematrix_d(temp, N);
|
|
48 }
|
|
49 //--------------------------------------------------------
|
|
50 void add_hor_sub_ver(double **in, int N, double **out)
|
|
51 {
|
|
52 double **temp;
|
|
53 int r, c;
|
|
54 temp = dmatrix(N, N);
|
|
55 for (r = 0; r < N; r++)
|
|
56 for (c = 0; c < N / 2; c++)
|
|
57 temp[r][c] = (in[r][2 * c] + in[r][2 * c + 1]) / 2;
|
|
58 for (c = 0; c < N / 2; c++)
|
|
59 for (r = 0; r < N / 2; r++)
|
|
60 out[r][c] = (temp[2 * r][c] - temp[2 * r + 1][c]) / 2;
|
|
61 freematrix_d(temp, N);
|
|
62 }
|
|
63 //--------------------------------------------------------
|
|
64 void sub_hor_add_ver(double **in, int N, double **out)
|
|
65 {
|
|
66 double **temp;
|
|
67 int r, c;
|
|
68 temp = dmatrix(N, N);
|
|
69 for (r = 0; r < N; r++)
|
|
70 for (c = 0; c < N / 2; c++)
|
|
71 temp[r][c] = (in[r][2 * c] - in[r][2 * c + 1]) / 2;
|
|
72 for (c = 0; c < N / 2; c++)
|
|
73 for (r = 0; r < N / 2; r++)
|
|
74 out[r][c] = (temp[2 * r][c] + temp[2 * r + 1][c]) / 2;
|
|
75 freematrix_d(temp, N);
|
|
76 }
|
|
77 //--------------------------------------------------------
|
|
78 void sub_hor_sub_ver(double **in, int N, double **out)
|
|
79 {
|
|
80 double **temp;
|
|
81 int r, c;
|
|
82 temp = dmatrix(N, N);
|
|
83 for (r = 0; r < N; r++)
|
|
84 for (c = 0; c < N / 2; c++)
|
|
85 temp[r][c] = (in[r][2 * c] - in[r][2 * c + 1]) / 2;
|
|
86 for (c = 0; c < N / 2; c++)
|
|
87 for (r = 0; r < N / 2; r++)
|
|
88 out[r][c] = (temp[2 * r][c] - temp[2 * r + 1][c]) / 2;
|
|
89 freematrix_d(temp, N);
|
|
90 }
|
|
91 //--------------------------------------------------------
|
|
92 void band_synthesis(double **ll, double **lh, double **hl, double **hh, int N, double **s)
|
|
93 {
|
|
94 int r, c;
|
|
95 double b1, b2, b3, b4;
|
|
96 for (r = 0; r < N; r++)
|
|
97 for (c = 0; c < N; c++) {
|
|
98 b1 = ll[r][c] + lh[r][c] + hl[r][c] + hh[r][c]; //Reconstruct each
|
|
99 b2 = ll[r][c] + lh[r][c] - hl[r][c] - hh[r][c]; //of the pixels
|
|
100 b3 = ll[r][c] - lh[r][c] + hl[r][c] - hh[r][c];
|
|
101 b4 = ll[r][c] - lh[r][c] - hl[r][c] + hh[r][c];
|
|
102 b1 = (b1 > 255.0) ? 255.0 : b1; //Check for positive...
|
|
103 b1 = (b1 < 0.0) ? 0.0 : b1; //or negative core!
|
|
104 b2 = (b2 > 255.0) ? 255.0 : b2;
|
|
105 b2 = (b2 < 0.0) ? 0.0 : b2;
|
|
106 b3 = (b3 > 255.0) ? 255.0 : b3;
|
|
107 b3 = (b3 < 0.0) ? 0.0 : b3;
|
|
108 b4 = (b4 > 255.0) ? 255.0 : b4;
|
|
109 b4 = (b4 < 0.0) ? 0.0 : b4;
|
|
110 s[2*r][2*c] = b1; //Put them back in
|
|
111 s[2*r][2*c + 1] = b2; //the right position
|
|
112 s[2*r + 1][2*c] = b3;
|
|
113 s[2*r + 1][2*c + 1] = b4;
|
|
114 }
|
|
115 }
|
|
116
|
|
117 void watermark(double **i, int N, long key, long int L, long int M, double a)
|
|
118 {
|
|
119 int row, col, count;
|
|
120 long int elem, temp, seed;
|
|
121 double *v;
|
|
122 v = dvector(N * N);
|
|
123 put_matrix_2_vector(i, v, N);
|
|
124 fct2d(v, N, N);
|
|
125 seed = key;
|
|
126 count = 0;
|
|
127 elem = 0;
|
|
128 row = 2;
|
|
129 col = -1;
|
|
130 do {
|
|
131 do {
|
|
132 row--;
|
|
133 col++;
|
|
134 elem++;
|
|
135 if (col < N) {
|
|
136 if (elem > M) {
|
|
137 temp = row * N + col;
|
|
138 v[temp] += a * fabs(v[temp]) * gasdev(&seed);
|
|
139 count++;
|
|
140 }
|
|
141 }
|
|
142 } while (row > 0);
|
|
143 row = 2 + col;
|
|
144 col = -1;
|
|
145 } while (count < L);
|
|
146 ifct2d(v, N, N);
|
|
147 put_vector_2_matrix(v, i, N);
|
|
148 free(v);
|
|
149 }
|
|
150
|
|
151 int main(int argc, char* argv[])
|
|
152 {
|
|
153 double **i;
|
|
154 double **o;
|
|
155 FILE *in;
|
|
156 FILE *out;
|
|
157 int N;
|
|
158 int c;
|
|
159 long int M1, L1, M2, L2;
|
|
160 int **image_i;
|
|
161 double **ll, **lh, **hl, **hh;
|
|
162 double a_ll = 0.1, a_other = 0.2;
|
|
163 int wm_length_1 = 10000, wm_length_ll = 10000, coeff_start_1 = 3000,
|
|
164 coeff_start_ll = 3000, wm_key = 123;
|
|
165
|
|
166 pgm_init(&argc, argv); wm_init();
|
|
167
|
|
168 while ((c = getopt(argc, argv, "a:b:t:m:s:l:k:")) != EOF) {
|
|
169 switch (c) {
|
|
170 case 'a':
|
|
171 a_ll = atof(optarg);
|
|
172 break;
|
|
173 case 'b':
|
|
174 a_other = atof(optarg);
|
|
175 break;
|
|
176 case 't':
|
|
177 coeff_start_1 = atoi(optarg);
|
|
178 break;
|
|
179 case 'm':
|
|
180 wm_length_1 = atoi(optarg);
|
|
181 break;
|
|
182 case 's':
|
|
183 coeff_start_ll = atoi(optarg);
|
|
184 break;
|
|
185 case 'l':
|
|
186 wm_length_ll = atoi(optarg);
|
|
187 break;
|
|
188 case 'k':
|
|
189 wm_key = atoi(optarg);
|
|
190 break;
|
|
191 }
|
|
192 }
|
|
193 argc -= optind;
|
|
194 argv += optind;
|
|
195
|
|
196 in = stdin;
|
|
197 out = stdout;
|
|
198 open_image(in, &width, &height);
|
|
199 image_i = imatrix(height, width);
|
|
200 load_image(image_i, in, width, height);
|
|
201 if (height == width)
|
|
202 N = height;
|
|
203 else {
|
|
204 fprintf(stderr, "Cannot Proccess non-square images!\n");
|
|
205 exit( -11);
|
|
206 }
|
|
207 // starting coeff. for 1st level decomp.
|
|
208 M1 = coeff_start_1;
|
|
209 // number of coeffs to alter
|
|
210 L1 = wm_length_1;
|
|
211
|
|
212 // now the LL band
|
|
213 M2 = coeff_start_ll;
|
|
214 L2 = wm_length_ll;
|
|
215
|
|
216 i = dmatrix(N, N);
|
|
217 o = dmatrix(N, N);
|
|
218 ll = dmatrix(N / 2, N / 2);
|
|
219 lh = dmatrix(N / 2, N / 2);
|
|
220 hl = dmatrix(N / 2, N / 2);
|
|
221 hh = dmatrix(N / 2, N / 2);
|
|
222
|
|
223 matrix_i2d(image_i, i, N);
|
|
224
|
|
225 //---------------------1o decomposition-------------------
|
|
226 add_hor_add_ver(i, N, ll);
|
|
227 add_hor_sub_ver(i, N, lh);
|
|
228 sub_hor_add_ver(i, N, hl);
|
|
229 sub_hor_sub_ver(i, N, hh);
|
|
230 //---------------------Watermark medium frequency bands---
|
|
231 watermark(lh, N / 2, wm_key, L1, M1, a_other);
|
|
232 watermark(hl, N / 2, wm_key, L1, M1, a_other);
|
|
233 watermark(hh, N / 2, wm_key, L1, M1, a_other);
|
|
234 watermark(ll, N / 2, wm_key, L2, M2, a_ll);
|
|
235 //---------------------Synthesis stage--------------------
|
|
236 band_synthesis(ll, lh, hl, hh, N / 2, o);
|
|
237 //--------------------------------------------------------
|
|
238 matrix_d2i(o, image_i, N);
|
|
239 save_image(image_i, out, width, height);
|
|
240
|
|
241 fclose(in);
|
|
242 fclose(out);
|
|
243 }
|
|
244
|
|
245
|
|
246
|
|
247
|