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
5 * Copyright (c) 2005, Reiner Wahler
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
18 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
21 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 * POSSIBILITY OF SUCH DAMAGE.
32 * This is a modified version of the openjpeg dwt.c file.
33 * Average speed improvement compared to the original file (measured on
34 * my own machine, a P4 running at 3.0 GHz):
35 * 5x3 wavelets about 2 times faster
36 * 9x7 wavelets about 3 times faster
37 * for both, encoding and decoding.
39 * The better performance is caused by doing the 1-dimensional DWT
40 * within a temporary buffer where the data can be accessed sequential
41 * for both directions, horizontal and vertical. The 2d vertical DWT was
42 * the major bottleneck in the former version.
44 * I have also removed the "Add Patrick" part because it is not longer
48 * -Ive (aka Reiner Wahler)
49 * mail: ive@lilysoft.com
62 #define D(i) a[(1+(i)*2)]
63 #define S_(i) ((i)<0?S(0):((i)>=sn?S(sn-1):S(i)))
64 #define D_(i) ((i)<0?D(0):((i)>=dn?D(dn-1):D(i)))
66 #define SS_(i) ((i)<0?S(0):((i)>=dn?S(dn-1):S(i)))
67 #define DD_(i) ((i)<0?D(0):((i)>=sn?D(sn-1):D(i)))
70 /* This table contains the norms of the 5-3 wavelets for different bands. */
72 double dwt_norms[4][10] = {
73 {1.000, 1.500, 2.750, 5.375, 10.68, 21.34, 42.67, 85.33, 170.7, 341.3},
74 {1.038, 1.592, 2.919, 5.703, 11.33, 22.64, 45.25, 90.48, 180.9},
75 {1.038, 1.592, 2.919, 5.703, 11.33, 22.64, 45.25, 90.48, 180.9},
76 {.7186, .9218, 1.586, 3.043, 6.019, 12.01, 24.00, 47.97, 95.93}
80 /* This table contains the norms of the 9-7 wavelets for different bands. */
82 double dwt_norms_real[4][10] = {
83 {1.000, 1.965, 4.177, 8.403, 16.90, 33.84, 67.69, 135.3, 270.6, 540.9},
84 {2.022, 3.989, 8.355, 17.04, 34.27, 68.63, 137.3, 274.6, 549.0},
85 {2.022, 3.989, 8.355, 17.04, 34.27, 68.63, 137.3, 274.6, 549.0},
86 {2.080, 3.865, 8.307, 17.18, 34.71, 69.59, 139.3, 278.6, 557.2}
91 /* Forward lazy transform (horizontal). */
93 void dwt_deinterleave_h(int *a, int *b, int dn, int sn, int cas) {
95 for (i=0; i<sn; i++) b[i]=a[2*i+cas];
96 for (i=0; i<dn; i++) b[sn+i]=a[(2*i+1-cas)];
100 /* Forward lazy transform (vertical). */
102 void dwt_deinterleave_v(int *a, int *b, int dn, int sn, int x, int cas) {
104 for (i=0; i<sn; i++) b[i*x]=a[2*i+cas];
105 for (i=0; i<dn; i++) b[(sn+i)*x]=a[(2*i+1-cas)];
109 /* Inverse lazy transform (horizontal). */
111 void dwt_interleave_h(int *a, int *b, int dn, int sn, int cas) {
113 // for (i=0; i<sn; i++) b[2*i+cas]=a[i];
114 // for (i=0; i<dn; i++) b[2*i+1-cas]=a[(sn+i)];
119 for (i=0; i<sn; i++) {
120 *bi = *ai; bi+=2; ai++;
124 for (i=0; i<dn; i++) {
125 *bi = *ai; bi+=2; ai++;
130 /* Inverse lazy transform (vertical). */
132 void dwt_interleave_v(int *a, int *b, int dn, int sn, int x, int cas) {
134 // for (i=0; i<sn; i++) b[2*i+cas]=a[i*x];
135 // for (i=0; i<dn; i++) b[2*i+1-cas]=a[(sn+i)*x];
140 for (i=0; i<sn; i++) {
141 *bi = *ai; bi+=2; ai+=x;
145 for (i=0; i<dn; i++) {
146 *bi = *ai; bi+=2; ai+=x;
152 /* Forward 5-3 wavelet tranform in 1-D. */
154 void dwt_encode_1(int *a, int dn, int sn, int cas)
159 if ((dn > 0) || (sn > 1)) { /* NEW : CASE ONE ELEMENT */
160 for (i = 0; i < dn; i++) D(i) -= (S_(i) + S_(i + 1)) >> 1;
161 for (i = 0; i < sn; i++) S(i) += (D_(i - 1) + D_(i) + 2) >> 2;
164 if (!sn && dn == 1) /* NEW : CASE ONE ELEMENT */
167 for (i = 0; i < dn; i++) S(i) -= (DD_(i) + DD_(i - 1)) >> 1;
168 for (i = 0; i < sn; i++) D(i) += (SS_(i) + SS_(i + 1) + 2) >> 2;
175 /* Inverse 5-3 wavelet tranform in 1-D. */
177 void dwt_decode_1(int *a, int dn, int sn, int cas)
182 if ((dn > 0) || (sn > 1)) { /* NEW : CASE ONE ELEMENT */
183 for (i = 0; i < sn; i++) S(i) -= (D_(i - 1) + D_(i) + 2) >> 2;
184 for (i = 0; i < dn; i++) D(i) += (S_(i) + S_(i + 1)) >> 1;
187 if (!sn && dn == 1) /* NEW : CASE ONE ELEMENT */
190 for (i = 0; i < sn; i++) D(i) -= (SS_(i) + SS_(i + 1) + 2) >> 2;
191 for (i = 0; i < dn; i++) S(i) += (DD_(i) + DD_(i - 1)) >> 1;
198 /* Forward 5-3 wavelet tranform in 2-D. */
200 void dwt_encode(tcd_tilecomp_t * tilec)
208 w = tilec->x1-tilec->x0;
209 l = tilec->numresolutions-1;
212 for (i = 0; i < l; i++) {
213 int rw; /* width of the resolution level computed */
214 int rh; /* heigth of the resolution level computed */
215 int rw1; /* width of the resolution level once lower than computed one */
216 int rh1; /* height of the resolution level once lower than computed one */
217 int cas_col; /* 0 = non inversion on horizontal filtering 1 = inversion between low-pass and high-pass filtering */
218 int cas_row; /* 0 = non inversion on vertical filtering 1 = inversion between low-pass and high-pass filtering */
221 rw = tilec->resolutions[l - i].x1 - tilec->resolutions[l - i].x0;
222 rh = tilec->resolutions[l - i].y1 - tilec->resolutions[l - i].y0;
223 rw1= tilec->resolutions[l - i - 1].x1 - tilec->resolutions[l - i - 1].x0;
224 rh1= tilec->resolutions[l - i - 1].y1 - tilec->resolutions[l - i - 1].y0;
226 cas_row = tilec->resolutions[l - i].x0 % 2;
227 cas_col = tilec->resolutions[l - i].y0 % 2;
232 bj=(int*)malloc(rh*sizeof(int));
233 for (j=0; j<rw; j++) {
235 for (k=0; k<rh; k++) bj[k]=aj[k*w];
236 dwt_encode_1(bj, dn, sn, cas_col);
237 dwt_deinterleave_v(bj, aj, dn, sn, w, cas_col);
243 bj=(int*)malloc(rw*sizeof(int));
244 for (j=0; j<rh; j++) {
246 for (k=0; k<rw; k++) bj[k]=aj[k];
247 dwt_encode_1(bj, dn, sn, cas_row);
248 dwt_deinterleave_h(bj, aj, dn, sn, cas_row);
256 /* Inverse 5-3 wavelet tranform in 2-D. */
258 void dwt_decode(tcd_tilecomp_t * tilec, int stop)
266 w = tilec->x1-tilec->x0;
267 l = tilec->numresolutions-1;
270 for (i = l - 1; i >= stop; i--) {
271 int rw; /* width of the resolution level computed */
272 int rh; /* heigth of the resolution level computed */
273 int rw1; /* width of the resolution level once lower than computed one */
274 int rh1; /* height of the resolution level once lower than computed one */
275 int cas_col; /* 0 = non inversion on horizontal filtering 1 = inversion between low-pass and high-pass filtering */
276 int cas_row; /* 0 = non inversion on vertical filtering 1 = inversion between low-pass and high-pass filtering */
279 rw = tilec->resolutions[l - i].x1 - tilec->resolutions[l - i].x0;
280 rh = tilec->resolutions[l - i].y1 - tilec->resolutions[l - i].y0;
281 rw1= tilec->resolutions[l - i - 1].x1 - tilec->resolutions[l - i - 1].x0;
282 rh1= tilec->resolutions[l - i - 1].y1 - tilec->resolutions[l - i - 1].y0;
284 cas_row = tilec->resolutions[l - i].x0 % 2;
285 cas_col = tilec->resolutions[l - i].y0 % 2;
289 bj=(int*)malloc(rw*sizeof(int));
290 for (j = 0; j < rh; j++) {
292 dwt_interleave_h(aj, bj, dn, sn, cas_row);
293 dwt_decode_1(bj, dn, sn, cas_row);
294 for (k = 0; k < rw; k++) aj[k] = bj[k];
300 bj=(int*)malloc(rh*sizeof(int));
301 for (j = 0; j < rw; j++) {
303 dwt_interleave_v(aj, bj, dn, sn, w, cas_col);
304 dwt_decode_1(bj, dn, sn, cas_col);
305 for (k = 0; k < rh; k++) aj[k * w] = bj[k];
314 /* Get gain of 5-3 wavelet transform. */
316 int dwt_getgain(int orient)
320 if (orient == 1 || orient == 2)
326 /* Get norm of 5-3 wavelet. */
328 double dwt_getnorm(int level, int orient)
330 return dwt_norms[orient][level];
334 /* Forward 9-7 wavelet transform in 1-D. */
336 void dwt_encode_1_real(int *a, int dn, int sn, int cas)
340 if ((dn > 0) || (sn > 1)) { /* NEW : CASE ONE ELEMENT */
341 for (i = 0; i < dn; i++)
342 D(i) -= fix_mul(S_(i) + S_(i + 1), 12993);
343 for (i = 0; i < sn; i++)
344 S(i) -= fix_mul(D_(i - 1) + D_(i), 434);
345 for (i = 0; i < dn; i++)
346 D(i) += fix_mul(S_(i) + S_(i + 1), 7233);
347 for (i = 0; i < sn; i++)
348 S(i) += fix_mul(D_(i - 1) + D_(i), 3633);
349 for (i = 0; i < dn; i++)
350 D(i) = fix_mul(D(i), 5038); /*5038 */
351 for (i = 0; i < sn; i++)
352 S(i) = fix_mul(S(i), 6659); /*6660 */
355 if ((sn > 0) || (dn > 1)) { /* NEW : CASE ONE ELEMENT */
356 for (i = 0; i < dn; i++)
357 S(i) -= fix_mul(DD_(i) + DD_(i - 1), 12993);
358 for (i = 0; i < sn; i++)
359 D(i) -= fix_mul(SS_(i) + SS_(i + 1), 434);
360 for (i = 0; i < dn; i++)
361 S(i) += fix_mul(DD_(i) + DD_(i - 1), 7233);
362 for (i = 0; i < sn; i++)
363 D(i) += fix_mul(SS_(i) + SS_(i + 1), 3633);
364 for (i = 0; i < dn; i++)
365 S(i) = fix_mul(S(i), 5038); /*5038 */
366 for (i = 0; i < sn; i++)
367 D(i) = fix_mul(D(i), 6659); /*6660 */
373 /* Inverse 9-7 wavelet transform in 1-D. */
375 void dwt_decode_1_real(int *a, int dn, int sn, int cas)
379 if ((dn > 0) || (sn > 1)) { /* NEW : CASE ONE ELEMENT */
380 for (i = 0; i < sn; i++)
381 S(i) = fix_mul(S(i), 10078); /* 10076 */
382 for (i = 0; i < dn; i++)
383 D(i) = fix_mul(D(i), 13318); /* 13320 */
384 for (i = 0; i < sn; i++)
385 S(i) -= fix_mul(D_(i - 1) + D_(i), 3633);
386 for (i = 0; i < dn; i++)
387 D(i) -= fix_mul(S_(i) + S_(i + 1), 7233);
388 for (i = 0; i < sn; i++)
389 S(i) += fix_mul(D_(i - 1) + D_(i), 434);
390 for (i = 0; i < dn; i++)
391 D(i) += fix_mul(S_(i) + S_(i + 1), 12994); /* 12993 */
394 if ((sn > 0) || (dn > 1)) { /* NEW : CASE ONE ELEMENT */
395 for (i = 0; i < sn; i++)
396 D(i) = fix_mul(D(i), 10078); /* 10076 */
397 for (i = 0; i < dn; i++)
398 S(i) = fix_mul(S(i), 13318); /* 13320 */
399 for (i = 0; i < sn; i++)
400 D(i) -= fix_mul(SS_(i) + SS_(i + 1), 3633);
401 for (i = 0; i < dn; i++)
402 S(i) -= fix_mul(DD_(i) + DD_(i - 1), 7233);
403 for (i = 0; i < sn; i++)
404 D(i) += fix_mul(SS_(i) + SS_(i + 1), 434);
405 for (i = 0; i < dn; i++)
406 S(i) += fix_mul(DD_(i) + DD_(i - 1), 12994); /* 12993 */
412 /* Forward 9-7 wavelet transform in 2-D. */
415 void dwt_encode_real(tcd_tilecomp_t * tilec)
423 w = tilec->x1-tilec->x0;
424 l = tilec->numresolutions-1;
427 for (i = 0; i < l; i++) {
428 int rw; /* width of the resolution level computed */
429 int rh; /* heigth of the resolution level computed */
430 int rw1; /* width of the resolution level once lower than computed one */
431 int rh1; /* height of the resolution level once lower than computed one */
432 int cas_col; /* 0 = non inversion on horizontal filtering 1 = inversion between low-pass and high-pass filtering */
433 int cas_row; /* 0 = non inversion on vertical filtering 1 = inversion between low-pass and high-pass filtering */
436 rw = tilec->resolutions[l - i].x1 - tilec->resolutions[l - i].x0;
437 rh = tilec->resolutions[l - i].y1 - tilec->resolutions[l - i].y0;
438 rw1= tilec->resolutions[l - i - 1].x1 - tilec->resolutions[l - i - 1].x0;
439 rh1= tilec->resolutions[l - i - 1].y1 - tilec->resolutions[l - i - 1].y0;
441 cas_row = tilec->resolutions[l - i].x0 % 2;
442 cas_col = tilec->resolutions[l - i].y0 % 2;
446 bj=(int*)malloc(rh*sizeof(int));
447 for (j = 0; j < rw; j++) {
449 for (k = 0; k < rh; k++) bj[k] = aj[k*w];
450 dwt_encode_1_real(bj, dn, sn, cas_col);
451 dwt_deinterleave_v(bj, aj, dn, sn, w, cas_col);
457 bj=(int*)malloc(rw*sizeof(int));
458 for (j = 0; j < rh; j++) {
460 for (k = 0; k < rw; k++) bj[k] = aj[k];
461 dwt_encode_1_real(bj, dn, sn, cas_row);
462 dwt_deinterleave_h(bj, aj, dn, sn, cas_row);
470 /* Inverse 9-7 wavelet transform in 2-D. */
472 void dwt_decode_real(tcd_tilecomp_t * tilec, int stop)
481 w = tilec->x1-tilec->x0;
482 l = tilec->numresolutions-1;
485 for (i = l-1; i >= stop; i--) {
486 int rw; /* width of the resolution level computed */
487 int rh; /* heigth of the resolution level computed */
488 int rw1; /* width of the resolution level once lower than computed one */
489 int rh1; /* height of the resolution level once lower than computed one */
490 int cas_col; /* 0 = non inversion on horizontal filtering 1 = inversion between low-pass and high-pass filtering */
491 int cas_row; /* 0 = non inversion on vertical filtering 1 = inversion between low-pass and high-pass filtering */
494 rw = tilec->resolutions[l - i].x1 - tilec->resolutions[l - i].x0;
495 rh = tilec->resolutions[l - i].y1 - tilec->resolutions[l - i].y0;
496 rw1= tilec->resolutions[l - i - 1].x1 - tilec->resolutions[l - i - 1].x0;
497 rh1= tilec->resolutions[l - i - 1].y1 - tilec->resolutions[l - i - 1].y0;
499 cas_col = tilec->resolutions[l - i].x0 % 2; /* 0 = non inversion on horizontal filtering 1 = inversion between low-pass and high-pass filtering */
500 cas_row = tilec->resolutions[l - i].y0 % 2; /* 0 = non inversion on vertical filtering 1 = inversion between low-pass and high-pass filtering */
504 bj = (int*)malloc(rw * sizeof(int));
505 for (j = 0; j < rh; j++) {
507 dwt_interleave_h(aj, bj, dn, sn, cas_col);
508 dwt_decode_1_real(bj, dn, sn, cas_col);
509 for (k = 0; k < rw; k++) aj[k] = bj[k];
515 bj = (int*)malloc(rh * sizeof(int));
516 for (j=0; j<rw; j++) {
518 dwt_interleave_v(aj, bj, dn, sn, w, cas_row);
519 dwt_decode_1_real(bj, dn, sn, cas_row);
520 for (k = 0; k < rh; k++) aj[k * w] = bj[k];
528 /* Get gain of 9-7 wavelet transform. */
530 int dwt_getgain_real(int orient)
536 /* Get norm of 9-7 wavelet. */
538 double dwt_getnorm_real(int level, int orient)
540 return dwt_norms_real[orient][level];