2 * Copyright (c) 2001-2002, David Janssens
3 * Copyright (c) 2002-2004, Yannick Verschueren
4 * Copyright (c) 2002-2004, Communications and remote sensing Laboratory, Universite catholique de Louvain, Belgium
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
37 #define S(i) a[x*(i)*2]
38 #define D(i) a[x*(1+(i)*2)]
39 #define S_(i) ((i)<0?S(0):((i)>=sn?S(sn-1):S(i)))
40 #define D_(i) ((i)<0?D(0):((i)>=dn?D(dn-1):D(i)))
42 #define SS_(i) ((i)<0?S(0):((i)>=dn?S(dn-1):S(i)))
43 #define DD_(i) ((i)<0?D(0):((i)>=sn?D(sn-1):D(i)))
46 /* This table contains the norms of the 5-3 wavelets for different bands. */
48 double dwt_norms[4][10] = {
49 {1.000, 1.500, 2.750, 5.375, 10.68, 21.34, 42.67, 85.33, 170.7, 341.3},
50 {1.038, 1.592, 2.919, 5.703, 11.33, 22.64, 45.25, 90.48, 180.9},
51 {1.038, 1.592, 2.919, 5.703, 11.33, 22.64, 45.25, 90.48, 180.9},
52 {.7186, .9218, 1.586, 3.043, 6.019, 12.01, 24.00, 47.97, 95.93}
56 /* This table contains the norms of the 9-7 wavelets for different bands. */
58 double dwt_norms_real[4][10] = {
59 {1.000, 1.965, 4.177, 8.403, 16.90, 33.84, 67.69, 135.3, 270.6, 540.9},
60 {2.022, 3.989, 8.355, 17.04, 34.27, 68.63, 137.3, 274.6, 549.0},
61 {2.022, 3.989, 8.355, 17.04, 34.27, 68.63, 137.3, 274.6, 549.0},
62 {2.080, 3.865, 8.307, 17.18, 34.71, 69.59, 139.3, 278.6, 557.2}
67 static int lastSizeOfB = 0;
70 /* Cleaning memory. */
85 /* Forward lazy transform. */
87 void dwt_deinterleave(int *a, int n, int x, int res, int cas)
92 if (lastSizeOfB != n) {
95 b = (int *) malloc(n * sizeof(int));
100 for (i = 0; i < sn; i++)
101 b[i] = a[(2 * i + 1) * x];
102 for (i = 0; i < dn; i++)
103 b[sn + i] = a[2 * i * x];
105 for (i = 0; i < sn; i++)
107 for (i = 0; i < dn; i++)
108 b[sn + i] = a[(2 * i + 1) * x];
110 for (i = 0; i < n; i++)
115 /* Inverse lazy transform. */
117 void dwt_interleave(int *a, int n, int x, int res, int cas)
123 if (lastSizeOfB != n) {
126 b = (int *) malloc(n * sizeof(int));
131 for (i = 0; i < sn; i++)
132 b[2 * i + 1] = a[i * x];
133 for (i = 0; i < dn; i++)
134 b[2 * i] = a[(sn + i) * x];
136 for (i = 0; i < sn; i++)
138 for (i = 0; i < dn; i++)
139 b[2 * i + 1] = a[(sn + i) * x];
141 for (i = 0; i < n; i++)
146 /* Forward 5-3 wavelet tranform in 1-D. */
148 void dwt_encode_1(int *a, int n, int x, int res, int cas)
155 if (!sn && dn == 1) /* NEW : CASE ONE ELEMENT */
158 for (i = 0; i < dn; i++)
159 S(i) -= (DD_(i) + DD_(i - 1)) >> 1;
160 for (i = 0; i < sn; i++)
161 D(i) += (SS_(i) + SS_(i + 1) + 2) >> 2;
164 if ((dn > 0) || (sn > 1)) { /* NEW : CASE ONE ELEMENT */
165 for (i = 0; i < dn; i++)
166 D(i) -= (S_(i) + S_(i + 1)) >> 1;
167 for (i = 0; i < sn; i++)
168 S(i) += (D_(i - 1) + D_(i) + 2) >> 2;
171 dwt_deinterleave(a, n, x, res, cas);
175 /* Inverse 5-3 wavelet tranform in 1-D. */
177 void dwt_decode_1(int *a, int n, int x, int res, int cas)
183 dwt_interleave(a, n, x, res, cas);
185 if (!sn && dn == 1) /* NEW : CASE ONE ELEMENT */
188 for (i = 0; i < sn; i++)
189 D(i) -= (SS_(i) + SS_(i + 1) + 2) >> 2;
190 for (i = 0; i < dn; i++)
191 S(i) += (DD_(i) + DD_(i - 1)) >> 1;
194 if ((dn > 0) || (sn > 1)) { /* NEW : CASE ONE ELEMENT */
195 for (i = 0; i < sn; i++)
196 S(i) -= (D_(i - 1) + D_(i) + 2) >> 2;
197 for (i = 0; i < dn; i++)
198 D(i) += (S_(i) + S_(i + 1)) >> 1;
204 /* Forward 5-3 wavelet tranform in 2-D. */
206 void dwt_encode(int *a, int w, int h, tcd_tilecomp_t * tilec, int l)
209 int rw; /* width of the resolution level computed */
210 int rh; /* heigth of the resolution level computed */
211 int rw1; /* width of the resolution level once lower than computed one */
212 int rh1; /* height of the resolution level once lower than computed one */
214 for (i = 0; i < l; i++) {
215 int cas_col = 0; /* 0 = non inversion on horizontal filtering 1 = inversion between low-pass and high-pass filtering */
216 int cas_row = 0; /* 0 = non inversion on vertical filtering 1 = inversion between low-pass and high-pass filtering */
217 rw = tilec->resolutions[l - i].x1 - tilec->resolutions[l - i].x0;
218 rh = tilec->resolutions[l - i].y1 - tilec->resolutions[l - i].y0;
220 tilec->resolutions[l - i - 1].x1 - tilec->resolutions[l - i - 1].x0;
222 tilec->resolutions[l - i - 1].y1 - tilec->resolutions[l - i - 1].y0;
224 cas_row = tilec->resolutions[l - i].x0 % 2;
225 cas_col = tilec->resolutions[l - i].y0 % 2;
227 for (j = 0; j < rw; j++)
228 dwt_encode_1(a + j, rh, w, rh1, cas_col);
229 for (j = 0; j < rh; j++)
230 dwt_encode_1(a + j * w, rw, 1, rw1, cas_row);
237 /* Inverse 5-3 wavelet tranform in 2-D. */
239 void dwt_decode(int *a, int w, int h, tcd_tilecomp_t * tilec, int l,
243 int rw; /* width of the resolution level computed */
244 int rh; /* heigth of the resolution level computed */
245 int rw1; /* width of the resolution level once lower than computed one */
246 int rh1; /* height of the resolution level once lower than computed one */
248 for (i = l - 1; i >= stop; i--) {
249 int cas_col = 0; /* 0 = non inversion on horizontal filtering 1 = inversion between low-pass and high-pass filtering */
250 int cas_row = 0; /* 0 = non inversion on vertical filtering 1 = inversion between low-pass and high-pass filtering */
252 rw = tilec->resolutions[l - i].x1 - tilec->resolutions[l - i].x0;
253 rh = tilec->resolutions[l - i].y1 - tilec->resolutions[l - i].y0;
255 tilec->resolutions[l - i - 1].x1 - tilec->resolutions[l - i - 1].x0;
257 tilec->resolutions[l - i - 1].y1 - tilec->resolutions[l - i - 1].y0;
259 cas_row = tilec->resolutions[l - i].x0 % 2;
260 cas_col = tilec->resolutions[l - i].y0 % 2;
262 for (j = 0; j < rh; j++)
263 dwt_decode_1(a + j * w, rw, 1, rw1, cas_row);
264 for (j = 0; j < rw; j++)
265 dwt_decode_1(a + j, rh, w, rh1, cas_col);
271 /* Get gain of 5-3 wavelet transform. */
273 int dwt_getgain(int orient)
277 if (orient == 1 || orient == 2)
283 /* Get norm of 5-3 wavelet. */
285 double dwt_getnorm(int level, int orient)
287 return dwt_norms[orient][level];
291 /* Forward 9-7 wavelet transform in 1-D. */
293 void dwt_encode_1_real(int *a, int n, int x, int res, int cas)
300 if ((sn > 0) || (dn > 1)) { /* NEW : CASE ONE ELEMENT */
301 for (i = 0; i < dn; i++)
302 S(i) -= fix_mul(DD_(i) + DD_(i - 1), 12993);
303 for (i = 0; i < sn; i++)
304 D(i) -= fix_mul(SS_(i) + SS_(i + 1), 434);
305 for (i = 0; i < dn; i++)
306 S(i) += fix_mul(DD_(i) + DD_(i - 1), 7233);
307 for (i = 0; i < sn; i++)
308 D(i) += fix_mul(SS_(i) + SS_(i + 1), 3633);
309 for (i = 0; i < dn; i++)
310 S(i) = fix_mul(S(i), 5038); /*5038 */
311 for (i = 0; i < sn; i++)
312 D(i) = fix_mul(D(i), 6659); /*6660 */
315 if ((dn > 0) || (sn > 1)) { /* NEW : CASE ONE ELEMENT */
316 for (i = 0; i < dn; i++)
317 D(i) -= fix_mul(S_(i) + S_(i + 1), 12993);
318 for (i = 0; i < sn; i++)
319 S(i) -= fix_mul(D_(i - 1) + D_(i), 434);
320 for (i = 0; i < dn; i++)
321 D(i) += fix_mul(S_(i) + S_(i + 1), 7233);
322 for (i = 0; i < sn; i++)
323 S(i) += fix_mul(D_(i - 1) + D_(i), 3633);
324 for (i = 0; i < dn; i++)
325 D(i) = fix_mul(D(i), 5038); /*5038 */
326 for (i = 0; i < sn; i++)
327 S(i) = fix_mul(S(i), 6659); /*6660 */
330 dwt_deinterleave(a, n, x, res, cas);
334 /* Inverse 9-7 wavelet transform in 1-D. */
336 void dwt_decode_1_real(int *a, int n, int x, int res, int cas)
341 dwt_interleave(a, n, x, res, cas);
343 if ((sn > 0) || (dn > 1)) { /* NEW : CASE ONE ELEMENT */
344 for (i = 0; i < sn; i++)
345 D(i) = fix_mul(D(i), 10078); /* 10076 */
346 for (i = 0; i < dn; i++)
347 S(i) = fix_mul(S(i), 13318); /* 13320 */
348 for (i = 0; i < sn; i++)
349 D(i) -= fix_mul(SS_(i) + SS_(i + 1), 3633);
350 for (i = 0; i < dn; i++)
351 S(i) -= fix_mul(DD_(i) + DD_(i - 1), 7233);
352 for (i = 0; i < sn; i++)
353 D(i) += fix_mul(SS_(i) + SS_(i + 1), 434);
354 for (i = 0; i < dn; i++)
355 S(i) += fix_mul(DD_(i) + DD_(i - 1), 12993);
358 if ((dn > 0) || (sn > 1)) { /* NEW : CASE ONE ELEMENT */
359 for (i = 0; i < sn; i++)
360 S(i) = fix_mul(S(i), 10078); /* 10076 */
361 for (i = 0; i < dn; i++)
362 D(i) = fix_mul(D(i), 13318); /* 13320 */
363 for (i = 0; i < sn; i++)
364 S(i) -= fix_mul(D_(i - 1) + D_(i), 3633);
365 for (i = 0; i < dn; i++)
366 D(i) -= fix_mul(S_(i) + S_(i + 1), 7233);
367 for (i = 0; i < sn; i++)
368 S(i) += fix_mul(D_(i - 1) + D_(i), 434);
369 for (i = 0; i < dn; i++)
370 D(i) += fix_mul(S_(i) + S_(i + 1), 12993);
376 /* Forward 9-7 wavelet transform in 2-D. */
379 void dwt_encode_real(int *a, int w, int h, tcd_tilecomp_t * tilec, int l)
382 int rw; /* width of the resolution level computed */
383 int rh; /* heigth of the resolution level computed */
384 int rw1; /* width of the resolution level once lower than computed one */
385 int rh1; /* height of the resolution level once lower than computed one */
387 for (i = 0; i < l; i++) {
388 int cas_col = 0; /* 0 = non inversion on horizontal filtering 1 = inversion between low-pass and high-pass filtering */
389 int cas_row = 0; /* 0 = non inversion on vertical filtering 1 = inversion between low-pass and high-pass filtering */
390 rw = tilec->resolutions[l - i].x1 - tilec->resolutions[l - i].x0;
391 rh = tilec->resolutions[l - i].y1 - tilec->resolutions[l - i].y0;
393 tilec->resolutions[l - i - 1].x1 - tilec->resolutions[l - i - 1].x0;
395 tilec->resolutions[l - i - 1].y1 - tilec->resolutions[l - i - 1].y0;
397 cas_row = tilec->resolutions[l - i].x0 % 2;
398 cas_col = tilec->resolutions[l - i].y0 % 2;
400 for (j = 0; j < rw; j++)
401 dwt_encode_1_real(a + j, rh, w, rh1, cas_col);
402 for (j = 0; j < rh; j++)
403 dwt_encode_1_real(a + j * w, rw, 1, rw1, cas_row);
408 /* Inverse 9-7 wavelet transform in 2-D. */
410 void dwt_decode_real(int *a, int w, int h, tcd_tilecomp_t * tilec, int l,
414 int rw; /* width of the resolution level computed */
415 int rh; /* heigth of the resolution level computed */
416 int rw1; /* width of the resolution level once lower than computed one */
417 int rh1; /* height of the resolution level once lower than computed one */
419 for (i = l - 1; i >= stop; i--) {
420 int cas_col = 0; /* 0 = non inversion on horizontal filtering 1 = inversion between low-pass and high-pass filtering */
421 int cas_row = 0; /* 0 = non inversion on vertical filtering 1 = inversion between low-pass and high-pass filtering */
423 rw = tilec->resolutions[l - i].x1 - tilec->resolutions[l - i].x0;
424 rh = tilec->resolutions[l - i].y1 - tilec->resolutions[l - i].y0;
426 tilec->resolutions[l - i - 1].x1 - tilec->resolutions[l - i - 1].x0;
428 tilec->resolutions[l - i - 1].y1 - tilec->resolutions[l - i - 1].y0;
430 cas_row = tilec->resolutions[l - i].x0 % 2;
431 cas_col = tilec->resolutions[l - i].y0 % 2;
433 for (j = 0; j < rh; j++)
434 dwt_decode_1_real(a + j * w, rw, 1, rw1, cas_row);
435 for (j = 0; j < rw; j++)
436 dwt_decode_1_real(a + j, rh, w, rh1, cas_col);
441 /* Get gain of 9-7 wavelet transform. */
443 int dwt_getgain_real(int orient)
449 /* Get norm of 9-7 wavelet. */
451 double dwt_getnorm_real(int level, int orient)
453 return dwt_norms_real[orient][level];