2 * Copyright (c) 2002-2007, Communications and Remote Sensing Laboratory, Universite catholique de Louvain (UCL), Belgium
3 * Copyright (c) 2002-2007, Professor Benoit Macq
4 * Copyright (c) 2001-2003, David Janssens
5 * Copyright (c) 2002-2003, Yannick Verschueren
6 * Copyright (c) 2003-2007, Francois-Olivier Devaux and Antonin Descampe
7 * Copyright (c) 2005, Herve Drolon, FreeImage Team
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
20 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
23 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
33 #include "opj_includes.h"
35 /** @defgroup DWT DWT - Implementation of a discrete wavelet transform */
38 #define WS(i) v->mem[(i)*2]
39 #define WD(i) v->mem[(1+(i)*2)]
41 /** @name Local data structures */
44 typedef struct dwt_local{
54 Virtual function type for wavelet transform in 1-D
56 typedef void (*DWT1DFN)(dwt_t* v);
58 /** @name Local static functions */
62 Forward lazy transform (horizontal)
64 static void dwt_deinterleave_h(int *a, int *b, int dn, int sn, int cas);
66 Forward lazy transform (vertical)
68 static void dwt_deinterleave_v(int *a, int *b, int dn, int sn, int x, int cas);
70 Inverse lazy transform (horizontal)
72 static void dwt_interleave_h(dwt_t* h, int *a);
74 Inverse lazy transform (vertical)
76 static void dwt_interleave_v(dwt_t* v, int *a, int x);
78 Forward 5-3 wavelet tranform in 1-D
80 static void dwt_encode_1(int *a, int dn, int sn, int cas);
82 Inverse 5-3 wavelet tranform in 1-D
84 static void dwt_decode_1(dwt_t *v);
86 Forward 9-7 wavelet transform in 1-D
88 static void dwt_encode_1_real(int *a, int dn, int sn, int cas);
90 Inverse 9-7 wavelet transform in 1-D
92 static void dwt_decode_1_real(dwt_t *v);
96 static void dwt_encode_stepsize(int stepsize, int numbps, opj_stepsize_t *bandno_stepsize);
98 Inverse wavelet tranform in 2-D.
100 static void dwt_decode_tile(opj_tcd_tilecomp_t * tilec, int stop , DWT1DFN fn);
106 #define S(i) a[(i)*2]
107 #define D(i) a[(1+(i)*2)]
108 #define S_(i) ((i)<0?S(0):((i)>=sn?S(sn-1):S(i)))
109 #define D_(i) ((i)<0?D(0):((i)>=dn?D(dn-1):D(i)))
111 #define SS_(i) ((i)<0?S(0):((i)>=dn?S(dn-1):S(i)))
112 #define DD_(i) ((i)<0?D(0):((i)>=sn?D(sn-1):D(i)))
115 /* This table contains the norms of the 5-3 wavelets for different bands. */
117 static const double dwt_norms[4][10] = {
118 {1.000, 1.500, 2.750, 5.375, 10.68, 21.34, 42.67, 85.33, 170.7, 341.3},
119 {1.038, 1.592, 2.919, 5.703, 11.33, 22.64, 45.25, 90.48, 180.9},
120 {1.038, 1.592, 2.919, 5.703, 11.33, 22.64, 45.25, 90.48, 180.9},
121 {.7186, .9218, 1.586, 3.043, 6.019, 12.01, 24.00, 47.97, 95.93}
125 /* This table contains the norms of the 9-7 wavelets for different bands. */
127 static const double dwt_norms_real[4][10] = {
128 {1.000, 1.965, 4.177, 8.403, 16.90, 33.84, 67.69, 135.3, 270.6, 540.9},
129 {2.022, 3.989, 8.355, 17.04, 34.27, 68.63, 137.3, 274.6, 549.0},
130 {2.022, 3.989, 8.355, 17.04, 34.27, 68.63, 137.3, 274.6, 549.0},
131 {2.080, 3.865, 8.307, 17.18, 34.71, 69.59, 139.3, 278.6, 557.2}
135 ==========================================================
137 ==========================================================
141 /* Forward lazy transform (horizontal). */
143 static void dwt_deinterleave_h(int *a, int *b, int dn, int sn, int cas) {
145 for (i=0; i<sn; i++) b[i]=a[2*i+cas];
146 for (i=0; i<dn; i++) b[sn+i]=a[(2*i+1-cas)];
150 /* Forward lazy transform (vertical). */
152 static void dwt_deinterleave_v(int *a, int *b, int dn, int sn, int x, int cas) {
154 for (i=0; i<sn; i++) b[i*x]=a[2*i+cas];
155 for (i=0; i<dn; i++) b[(sn+i)*x]=a[(2*i+1-cas)];
159 /* Inverse lazy transform (horizontal). */
161 static void dwt_interleave_h(dwt_t* h, int *a) {
163 int *bi = h->mem + h->cas;
170 bi = h->mem + 1 - h->cas;
179 /* Inverse lazy transform (vertical). */
181 static void dwt_interleave_v(dwt_t* v, int *a, int x) {
183 int *bi = v->mem + v->cas;
190 ai = a + (v->sn * x);
191 bi = v->mem + 1 - v->cas;
202 /* Forward 5-3 wavelet tranform in 1-D. */
204 static void dwt_encode_1(int *a, int dn, int sn, int cas) {
208 if ((dn > 0) || (sn > 1)) { /* NEW : CASE ONE ELEMENT */
209 for (i = 0; i < dn; i++) D(i) -= (S_(i) + S_(i + 1)) >> 1;
210 for (i = 0; i < sn; i++) S(i) += (D_(i - 1) + D_(i) + 2) >> 2;
213 if (!sn && dn == 1) /* NEW : CASE ONE ELEMENT */
216 for (i = 0; i < dn; i++) S(i) -= (DD_(i) + DD_(i - 1)) >> 1;
217 for (i = 0; i < sn; i++) D(i) += (SS_(i) + SS_(i + 1) + 2) >> 2;
223 /* Inverse 5-3 wavelet tranform in 1-D. */
225 static void dwt_decode_1_(int *a, int dn, int sn, int cas) {
229 if ((dn > 0) || (sn > 1)) { /* NEW : CASE ONE ELEMENT */
230 for (i = 0; i < sn; i++) S(i) -= (D_(i - 1) + D_(i) + 2) >> 2;
231 for (i = 0; i < dn; i++) D(i) += (S_(i) + S_(i + 1)) >> 1;
234 if (!sn && dn == 1) /* NEW : CASE ONE ELEMENT */
237 for (i = 0; i < sn; i++) D(i) -= (SS_(i) + SS_(i + 1) + 2) >> 2;
238 for (i = 0; i < dn; i++) S(i) += (DD_(i) + DD_(i - 1)) >> 1;
244 /* Inverse 5-3 wavelet tranform in 1-D. */
246 static void dwt_decode_1(dwt_t *v) {
247 dwt_decode_1_(v->mem, v->dn, v->sn, v->cas);
251 /* Forward 9-7 wavelet transform in 1-D. */
253 static void dwt_encode_1_real(int *a, int dn, int sn, int cas) {
256 if ((dn > 0) || (sn > 1)) { /* NEW : CASE ONE ELEMENT */
257 for (i = 0; i < dn; i++)
258 D(i) -= fix_mul(S_(i) + S_(i + 1), 12993);
259 for (i = 0; i < sn; i++)
260 S(i) -= fix_mul(D_(i - 1) + D_(i), 434);
261 for (i = 0; i < dn; i++)
262 D(i) += fix_mul(S_(i) + S_(i + 1), 7233);
263 for (i = 0; i < sn; i++)
264 S(i) += fix_mul(D_(i - 1) + D_(i), 3633);
265 for (i = 0; i < dn; i++)
266 D(i) = fix_mul(D(i), 5038); /*5038 */
267 for (i = 0; i < sn; i++)
268 S(i) = fix_mul(S(i), 6659); /*6660 */
271 if ((sn > 0) || (dn > 1)) { /* NEW : CASE ONE ELEMENT */
272 for (i = 0; i < dn; i++)
273 S(i) -= fix_mul(DD_(i) + DD_(i - 1), 12993);
274 for (i = 0; i < sn; i++)
275 D(i) -= fix_mul(SS_(i) + SS_(i + 1), 434);
276 for (i = 0; i < dn; i++)
277 S(i) += fix_mul(DD_(i) + DD_(i - 1), 7233);
278 for (i = 0; i < sn; i++)
279 D(i) += fix_mul(SS_(i) + SS_(i + 1), 3633);
280 for (i = 0; i < dn; i++)
281 S(i) = fix_mul(S(i), 5038); /*5038 */
282 for (i = 0; i < sn; i++)
283 D(i) = fix_mul(D(i), 6659); /*6660 */
288 static void dwt_decode_sm(dwt_t* v, int k, int n, int x) {
289 int m = k > n ? n : k;
290 int l = v->mem[1]; //D(0);
293 for (i = 0; i < m; i++) {
295 WS(i) -= fix_mul( ( l = WD(i) ) + j , x);
298 l = fix_mul( l + l , x );
304 static void dwt_decode_sp(dwt_t* v, int k, int n, int x) {
305 int m = k > n ? n : k;
306 int l = v->mem[1]; //D(0);
309 for (i = 0; i < m; i++) {
311 WS(i) += fix_mul( ( l = WD(i) ) + j , x);
314 l = fix_mul( l + l , x );
320 static void dwt_decode_dm(dwt_t* v, int k, int n, int x) {
321 int m = k >= n ? n-1 : k;
322 int l = v->mem[0]; //S(0);
325 for (i = 0; i < m; i++) {
327 WD(i) -= fix_mul( ( l = WS(i+1) ) + j , x);
330 l = fix_mul( l + l , x );
336 static void dwt_decode_dp(dwt_t* v, int k, int n, int x) {
337 int m = k >= n ? n-1 : k;
338 int l = v->mem[0]; //S(0);
341 for (i = 0; i < m; i++) {
343 WD(i) += fix_mul( ( l = WS(i+1) ) + j , x);
347 l = fix_mul( l + l , x );
355 /* Inverse 9-7 wavelet transform in 1-D. */
357 static void dwt_decode_1_real(dwt_t* v) {
360 if ((v->dn > 0) || (v->sn > 1)) { /* NEW : CASE ONE ELEMENT */
361 for (i = 0; i < v->sn; i++)
362 WS(i) = fix_mul(WS(i), 10078); /* 10076 */
363 for (i = 0; i < v->dn; i++)
364 WD(i) = fix_mul(WD(i), 13318); /* 13320 */
365 dwt_decode_sm(v, v->sn, v->dn, 3633);
366 dwt_decode_dm(v, v->dn, v->sn, 7233);
367 dwt_decode_sp(v, v->sn, v->dn, 434);
368 dwt_decode_dp(v, v->dn, v->sn, 12994);
371 if ((v->sn > 0) || (v->dn > 1)) { /* NEW : CASE ONE ELEMENT */
372 for (i = 0; i < v->sn; i++)
373 WD(i) = fix_mul(WD(i), 10078); /* 10076 */
374 for (i = 0; i < v->dn; i++)
375 WS(i) = fix_mul(WS(i), 13318); /* 13320 */
376 dwt_decode_dm(v, v->sn, v->dn, 3633);
377 dwt_decode_sm(v, v->dn, v->sn, 7233);
378 dwt_decode_dp(v, v->sn, v->dn, 434);
379 dwt_decode_sp(v, v->dn, v->sn, 12994);
384 static void dwt_encode_stepsize(int stepsize, int numbps, opj_stepsize_t *bandno_stepsize) {
386 p = int_floorlog2(stepsize) - 13;
387 n = 11 - int_floorlog2(stepsize);
388 bandno_stepsize->mant = (n < 0 ? stepsize >> -n : stepsize << n) & 0x7ff;
389 bandno_stepsize->expn = numbps - p;
393 ==========================================================
395 ==========================================================
399 /* Forward 5-3 wavelet tranform in 2-D. */
401 void dwt_encode(opj_tcd_tilecomp_t * tilec) {
408 w = tilec->x1-tilec->x0;
409 l = tilec->numresolutions-1;
412 for (i = 0; i < l; i++) {
413 int rw; /* width of the resolution level computed */
414 int rh; /* heigth of the resolution level computed */
415 int rw1; /* width of the resolution level once lower than computed one */
416 int rh1; /* height of the resolution level once lower than computed one */
417 int cas_col; /* 0 = non inversion on horizontal filtering 1 = inversion between low-pass and high-pass filtering */
418 int cas_row; /* 0 = non inversion on vertical filtering 1 = inversion between low-pass and high-pass filtering */
421 rw = tilec->resolutions[l - i].x1 - tilec->resolutions[l - i].x0;
422 rh = tilec->resolutions[l - i].y1 - tilec->resolutions[l - i].y0;
423 rw1= tilec->resolutions[l - i - 1].x1 - tilec->resolutions[l - i - 1].x0;
424 rh1= tilec->resolutions[l - i - 1].y1 - tilec->resolutions[l - i - 1].y0;
426 cas_row = tilec->resolutions[l - i].x0 % 2;
427 cas_col = tilec->resolutions[l - i].y0 % 2;
431 bj = (int*)opj_malloc(rh * sizeof(int));
432 for (j = 0; j < rw; j++) {
434 for (k = 0; k < rh; k++) bj[k] = aj[k*w];
435 dwt_encode_1(bj, dn, sn, cas_col);
436 dwt_deinterleave_v(bj, aj, dn, sn, w, cas_col);
442 bj = (int*)opj_malloc(rw * sizeof(int));
443 for (j = 0; j < rh; j++) {
445 for (k = 0; k < rw; k++) bj[k] = aj[k];
446 dwt_encode_1(bj, dn, sn, cas_row);
447 dwt_deinterleave_h(bj, aj, dn, sn, cas_row);
455 /* Inverse 5-3 wavelet tranform in 2-D. */
457 void dwt_decode(opj_tcd_tilecomp_t * tilec, int stop) {
458 dwt_decode_tile(tilec, stop, &dwt_decode_1);
463 /* Get gain of 5-3 wavelet transform. */
465 int dwt_getgain(int orient) {
468 if (orient == 1 || orient == 2)
474 /* Get norm of 5-3 wavelet. */
476 double dwt_getnorm(int level, int orient) {
477 return dwt_norms[orient][level];
481 /* Forward 9-7 wavelet transform in 2-D. */
484 void dwt_encode_real(opj_tcd_tilecomp_t * tilec) {
491 w = tilec->x1-tilec->x0;
492 l = tilec->numresolutions-1;
495 for (i = 0; i < l; i++) {
496 int rw; /* width of the resolution level computed */
497 int rh; /* heigth of the resolution level computed */
498 int rw1; /* width of the resolution level once lower than computed one */
499 int rh1; /* height of the resolution level once lower than computed one */
500 int cas_col; /* 0 = non inversion on horizontal filtering 1 = inversion between low-pass and high-pass filtering */
501 int cas_row; /* 0 = non inversion on vertical filtering 1 = inversion between low-pass and high-pass filtering */
504 rw = tilec->resolutions[l - i].x1 - tilec->resolutions[l - i].x0;
505 rh = tilec->resolutions[l - i].y1 - tilec->resolutions[l - i].y0;
506 rw1= tilec->resolutions[l - i - 1].x1 - tilec->resolutions[l - i - 1].x0;
507 rh1= tilec->resolutions[l - i - 1].y1 - tilec->resolutions[l - i - 1].y0;
509 cas_row = tilec->resolutions[l - i].x0 % 2;
510 cas_col = tilec->resolutions[l - i].y0 % 2;
514 bj = (int*)opj_malloc(rh * sizeof(int));
515 for (j = 0; j < rw; j++) {
517 for (k = 0; k < rh; k++) bj[k] = aj[k*w];
518 dwt_encode_1_real(bj, dn, sn, cas_col);
519 dwt_deinterleave_v(bj, aj, dn, sn, w, cas_col);
525 bj = (int*)opj_malloc(rw * sizeof(int));
526 for (j = 0; j < rh; j++) {
528 for (k = 0; k < rw; k++) bj[k] = aj[k];
529 dwt_encode_1_real(bj, dn, sn, cas_row);
530 dwt_deinterleave_h(bj, aj, dn, sn, cas_row);
538 /* Inverse 9-7 wavelet transform in 2-D. */
540 void dwt_decode_real(opj_tcd_tilecomp_t * tilec, int stop) {
541 dwt_decode_tile(tilec, stop, dwt_decode_1_real);
546 /* Get gain of 9-7 wavelet transform. */
548 int dwt_getgain_real(int orient) {
554 /* Get norm of 9-7 wavelet. */
556 double dwt_getnorm_real(int level, int orient) {
557 return dwt_norms_real[orient][level];
560 void dwt_calc_explicit_stepsizes(opj_tccp_t * tccp, int prec) {
561 int numbands, bandno;
562 numbands = 3 * tccp->numresolutions - 2;
563 for (bandno = 0; bandno < numbands; bandno++) {
565 int resno, level, orient, gain;
567 resno = (bandno == 0) ? 0 : ((bandno - 1) / 3 + 1);
568 orient = (bandno == 0) ? 0 : ((bandno - 1) % 3 + 1);
569 level = tccp->numresolutions - 1 - resno;
570 gain = (tccp->qmfbid == 0) ? 0 : ((orient == 0) ? 0 : (((orient == 1) || (orient == 2)) ? 1 : 2));
571 if (tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) {
574 double norm = dwt_norms_real[orient][level];
575 stepsize = (1 << (gain)) / norm;
577 dwt_encode_stepsize((int) floor(stepsize * 8192.0), prec + gain, &tccp->stepsizes[bandno]);
583 /* Determine maximum computed resolution level for inverse wavelet transform */
585 static int dwt_decode_max_resolution(opj_tcd_resolution_t* r, int i) {
590 if( mr < ( w = r->x1 - r->x0 ) )
592 if( mr < ( w = r->y1 - r->y0 ) )
600 /* Inverse wavelet tranform in 2-D. */
602 static void dwt_decode_tile(opj_tcd_tilecomp_t * tilec, int stop, DWT1DFN dwt_1D) {
603 opj_tcd_resolution_t* tr;
609 int rw; /* width of the resolution level computed */
610 int rh; /* heigth of the resolution level computed */
614 if( 1 > ( i = tilec->numresolutions - stop ) )
617 tr = tilec->resolutions;
619 w = tilec->x1-tilec->x0;
622 m = (int*)opj_malloc(sizeof(int) * (dwt_decode_max_resolution(tr, i)+5));
623 h.mem = v.mem = (int*)( (unsigned)m + 16 - ( (unsigned)m % 16 ) ) ;
625 rw = tr->x1 - tr->x0;
626 rh = tr->y1 - tr->y0;
632 h.dn = ( rw = tr->x1 - tr->x0 ) - h.sn;
633 v.dn = ( rh = tr->y1 - tr->y0 ) - v.sn;
641 dwt_interleave_h(&h, aj);
652 dwt_interleave_v(&v, aj, w);
656 aj[k * w] = v.mem[k];