update j2k_lib with new opj type
[openjpeg.git] / libopenjpeg / dwt.c
index 3436b103123399b583249edb35809f52a1b7b9b9..94586eb53046d304693f8c2e0b413d4bee851528 100644 (file)
 /*@{*/
 
 typedef struct dwt_local {
-       int* mem;
-       int dn;
-       int sn;
-       int cas;
+       OPJ_INT32* mem;
+       OPJ_INT32 dn;
+       OPJ_INT32 sn;
+       OPJ_INT32 cas;
 } dwt_t;
 
 typedef union {
-       float   f[4];
+       OPJ_FLOAT32     f[4];
 } v4;
 
 typedef struct v4dwt_local {
        v4*     wavelet ;
-       int             dn ;
-       int             sn ;
-       int             cas ;
+       OPJ_INT32               dn ;
+       OPJ_INT32               sn ;
+       OPJ_INT32               cas ;
 } v4dwt_t ;
 
-static const float dwt_alpha =  1.586134342f; //  12994
-static const float dwt_beta  =  0.052980118f; //    434
-static const float dwt_gamma = -0.882911075f; //  -7233
-static const float dwt_delta = -0.443506852f; //  -3633
+static const OPJ_FLOAT32 opj_dwt_alpha =  1.586134342f; /*  12994 */
+static const OPJ_FLOAT32 opj_dwt_beta  =  0.052980118f; /*    434 */
+static const OPJ_FLOAT32 opj_dwt_gamma = -0.882911075f; /*  -7233 */
+static const OPJ_FLOAT32 opj_dwt_delta = -0.443506852f; /*  -3633 */
 
-static const float K      = 1.230174105f; //  10078
-/* FIXME: What is this constant? */
-static const float c13318 = 1.625732422f;
+static const OPJ_FLOAT32 opj_K      = 1.230174105f; /*  10078 */
+static const OPJ_FLOAT32 opj_c13318 = 1.625732422f;
 
 /*@}*/
 
@@ -86,39 +85,64 @@ typedef void (*DWT1DFN)(dwt_t* v);
 /**
 Forward lazy transform (horizontal)
 */
-static void dwt_deinterleave_h(int *a, int *b, int dn, int sn, int cas);
+static void opj_dwt_deinterleave_h(OPJ_INT32 *a, OPJ_INT32 *b, OPJ_INT32 dn, OPJ_INT32 sn, OPJ_INT32 cas);
 /**
 Forward lazy transform (vertical)
 */
-static void dwt_deinterleave_v(int *a, int *b, int dn, int sn, int x, int cas);
+static void opj_dwt_deinterleave_v(OPJ_INT32 *a, OPJ_INT32 *b, OPJ_INT32 dn, OPJ_INT32 sn, OPJ_INT32 x, OPJ_INT32 cas);
 /**
 Inverse lazy transform (horizontal)
 */
-static void dwt_interleave_h(dwt_t* h, int *a);
+static void opj_dwt_interleave_h(dwt_t* h, OPJ_INT32 *a);
 /**
 Inverse lazy transform (vertical)
 */
-static void dwt_interleave_v(dwt_t* v, int *a, int x);
+static void opj_dwt_interleave_v(dwt_t* v, OPJ_INT32 *a, OPJ_INT32 x);
 /**
 Forward 5-3 wavelet transform in 1-D
 */
-static void dwt_encode_1(int *a, int dn, int sn, int cas);
+static void opj_dwt_encode_1(OPJ_INT32 *a, OPJ_INT32 dn, OPJ_INT32 sn, OPJ_INT32 cas);
 /**
 Inverse 5-3 wavelet transform in 1-D
 */
-static void dwt_decode_1(dwt_t *v);
+static void opj_dwt_decode_1(dwt_t *v);
+static void opj_dwt_decode_1_(OPJ_INT32 *a, OPJ_INT32 dn, OPJ_INT32 sn, OPJ_INT32 cas);
 /**
 Forward 9-7 wavelet transform in 1-D
 */
-static void dwt_encode_1_real(int *a, int dn, int sn, int cas);
+static void opj_dwt_encode_1_real(OPJ_INT32 *a, OPJ_INT32 dn, OPJ_INT32 sn, OPJ_INT32 cas);
 /**
 Explicit calculation of the Quantization Stepsizes 
 */
-static void dwt_encode_stepsize(int stepsize, int numbps, opj_stepsize_t *bandno_stepsize);
+static void opj_dwt_encode_stepsize(OPJ_INT32 stepsize, OPJ_INT32 numbps, opj_stepsize_t *bandno_stepsize);
 /**
 Inverse wavelet transform in 2-D.
 */
-static void dwt_decode_tile(opj_tcd_tilecomp_t* tilec, int i, DWT1DFN fn);
+static opj_bool opj_dwt_decode_tile(opj_tcd_tilecomp_v2_t* tilec, OPJ_UINT32 i, DWT1DFN fn);
+
+static opj_bool opj_dwt_encode_procedure(      opj_tcd_tilecomp_v2_t * tilec,
+                                                                                   void (*p_function)(OPJ_INT32 *, OPJ_INT32,OPJ_INT32,OPJ_INT32) );
+
+static OPJ_UINT32 opj_dwt_max_resolution(opj_tcd_resolution_v2_t* restrict r, OPJ_UINT32 i);
+
+/* <summary>                             */
+/* Inverse 9-7 wavelet transform in 1-D. */
+/* </summary>                            */
+static void opj_v4dwt_decode(v4dwt_t* restrict dwt);
+
+static void opj_v4dwt_interleave_h(v4dwt_t* restrict w, OPJ_FLOAT32* restrict a, OPJ_INT32 x, OPJ_INT32 size);
+
+static void opj_v4dwt_interleave_v(v4dwt_t* restrict v , OPJ_FLOAT32* restrict a , OPJ_INT32 x, OPJ_INT32 nb_elts_read);
+
+#ifdef __SSE__
+static void opj_v4dwt_decode_step1_sse(v4* w, OPJ_INT32 count, const __m128 c);
+
+static void opj_v4dwt_decode_step2_sse(v4* l, v4* w, OPJ_INT32 k, OPJ_INT32 m, __m128 c);
+#endif
+
+static void opj_v4dwt_decode_step1(v4* w, OPJ_INT32 count, const OPJ_FLOAT32 c);
+static void opj_v4dwt_decode_step2(v4* l, v4* w, OPJ_INT32 k, OPJ_INT32 m, OPJ_FLOAT32 c);
+
 
 /*@}*/
 
@@ -135,7 +159,7 @@ static void dwt_decode_tile(opj_tcd_tilecomp_t* tilec, int i, DWT1DFN fn);
 /* <summary>                                                              */
 /* This table contains the norms of the 5-3 wavelets for different bands. */
 /* </summary>                                                             */
-static const double dwt_norms[4][10] = {
+static const OPJ_FLOAT64 opj_dwt_norms[4][10] = {
        {1.000, 1.500, 2.750, 5.375, 10.68, 21.34, 42.67, 85.33, 170.7, 341.3},
        {1.038, 1.592, 2.919, 5.703, 11.33, 22.64, 45.25, 90.48, 180.9},
        {1.038, 1.592, 2.919, 5.703, 11.33, 22.64, 45.25, 90.48, 180.9},
@@ -145,7 +169,7 @@ static const double dwt_norms[4][10] = {
 /* <summary>                                                              */
 /* This table contains the norms of the 9-7 wavelets for different bands. */
 /* </summary>                                                             */
-static const double dwt_norms_real[4][10] = {
+static const OPJ_FLOAT64 opj_dwt_norms_real[4][10] = {
        {1.000, 1.965, 4.177, 8.403, 16.90, 33.84, 67.69, 135.3, 270.6, 540.9},
        {2.022, 3.989, 8.355, 17.04, 34.27, 68.63, 137.3, 274.6, 549.0},
        {2.022, 3.989, 8.355, 17.04, 34.27, 68.63, 137.3, 274.6, 549.0},
@@ -161,28 +185,57 @@ static const double dwt_norms_real[4][10] = {
 /* <summary>                                    */
 /* Forward lazy transform (horizontal).  */
 /* </summary>                            */ 
-static void dwt_deinterleave_h(int *a, int *b, int dn, int sn, int cas) {
-       int i;
-    for (i=0; i<sn; i++) b[i]=a[2*i+cas];
-    for (i=0; i<dn; i++) b[sn+i]=a[(2*i+1-cas)];
+void opj_dwt_deinterleave_h(OPJ_INT32 *a, OPJ_INT32 *b, OPJ_INT32 dn, OPJ_INT32 sn, OPJ_INT32 cas) {
+       OPJ_INT32 i;
+       OPJ_INT32 * l_dest = b;
+       OPJ_INT32 * l_src = a+cas;
+
+    for (i=0; i<sn; ++i) {
+               *l_dest++ = *l_src;
+               l_src += 2;
+       }
+       
+    l_dest = b + sn;
+       l_src = a + 1 - cas;
+
+    for        (i=0; i<dn; ++i)  {
+               *l_dest++=*l_src;
+               l_src += 2;
+       }
 }
 
 /* <summary>                             */  
 /* Forward lazy transform (vertical).    */
 /* </summary>                            */ 
-static void dwt_deinterleave_v(int *a, int *b, int dn, int sn, int x, int cas) {
-    int i;
-    for (i=0; i<sn; i++) b[i*x]=a[2*i+cas];
-    for (i=0; i<dn; i++) b[(sn+i)*x]=a[(2*i+1-cas)];
+void opj_dwt_deinterleave_v(OPJ_INT32 *a, OPJ_INT32 *b, OPJ_INT32 dn, OPJ_INT32 sn, OPJ_INT32 x, OPJ_INT32 cas) {
+    OPJ_INT32 i = sn;
+       OPJ_INT32 * l_dest = b;
+       OPJ_INT32 * l_src = a+cas;
+
+    while (i--) {
+               *l_dest = *l_src;
+               l_dest += x;
+               l_src += 2;
+               } /* b[i*x]=a[2*i+cas]; */
+
+       l_dest = b + sn * x;
+       l_src = a + 1 - cas;
+       
+       i = dn;
+    while (i--) {
+               *l_dest = *l_src;
+               l_dest += x;
+               l_src += 2;
+        } /*b[(sn+i)*x]=a[(2*i+1-cas)];*/
 }
 
 /* <summary>                             */
 /* Inverse lazy transform (horizontal).  */
 /* </summary>                            */
-static void dwt_interleave_h(dwt_t* h, int *a) {
-    int *ai = a;
-    int *bi = h->mem + h->cas;
-    int  i     = h->sn;
+void opj_dwt_interleave_h(dwt_t* h, OPJ_INT32 *a) {
+    OPJ_INT32 *ai = a;
+    OPJ_INT32 *bi = h->mem + h->cas;
+    OPJ_INT32  i       = h->sn;
     while( i-- ) {
       *bi = *(ai++);
          bi += 2;
@@ -199,10 +252,10 @@ static void dwt_interleave_h(dwt_t* h, int *a) {
 /* <summary>                             */  
 /* Inverse lazy transform (vertical).    */
 /* </summary>                            */ 
-static void dwt_interleave_v(dwt_t* v, int *a, int x) {
-    int *ai = a;
-    int *bi = v->mem + v->cas;
-    int  i = v->sn;
+void opj_dwt_interleave_v(dwt_t* v, OPJ_INT32 *a, OPJ_INT32 x) {
+    OPJ_INT32 *ai = a;
+    OPJ_INT32 *bi = v->mem + v->cas;
+    OPJ_INT32  i = v->sn;
     while( i-- ) {
       *bi = *ai;
          bi += 2;
@@ -222,8 +275,8 @@ static void dwt_interleave_v(dwt_t* v, int *a, int x) {
 /* <summary>                            */
 /* Forward 5-3 wavelet transform in 1-D. */
 /* </summary>                           */
-static void dwt_encode_1(int *a, int dn, int sn, int cas) {
-       int i;
+void opj_dwt_encode_1(OPJ_INT32 *a, OPJ_INT32 dn, OPJ_INT32 sn, OPJ_INT32 cas) {
+       OPJ_INT32 i;
        
        if (!cas) {
                if ((dn > 0) || (sn > 1)) {     /* NEW :  CASE ONE ELEMENT */
@@ -243,8 +296,8 @@ static void dwt_encode_1(int *a, int dn, int sn, int cas) {
 /* <summary>                            */
 /* Inverse 5-3 wavelet transform in 1-D. */
 /* </summary>                           */ 
-static void dwt_decode_1_(int *a, int dn, int sn, int cas) {
-       int i;
+void opj_dwt_decode_1_(OPJ_INT32 *a, OPJ_INT32 dn, OPJ_INT32 sn, OPJ_INT32 cas) {
+       OPJ_INT32 i;
        
        if (!cas) {
                if ((dn > 0) || (sn > 1)) { /* NEW :  CASE ONE ELEMENT */
@@ -264,15 +317,15 @@ static void dwt_decode_1_(int *a, int dn, int sn, int cas) {
 /* <summary>                            */
 /* Inverse 5-3 wavelet transform in 1-D. */
 /* </summary>                           */ 
-static void dwt_decode_1(dwt_t *v) {
-       dwt_decode_1_(v->mem, v->dn, v->sn, v->cas);
+void opj_dwt_decode_1(dwt_t *v) {
+       opj_dwt_decode_1_(v->mem, v->dn, v->sn, v->cas);
 }
 
 /* <summary>                             */
 /* Forward 9-7 wavelet transform in 1-D. */
 /* </summary>                            */
-static void dwt_encode_1_real(int *a, int dn, int sn, int cas) {
-       int i;
+void opj_dwt_encode_1_real(OPJ_INT32 *a, OPJ_INT32 dn, OPJ_INT32 sn, OPJ_INT32 cas) {
+       OPJ_INT32 i;
        if (!cas) {
                if ((dn > 0) || (sn > 1)) {     /* NEW :  CASE ONE ELEMENT */
                        for (i = 0; i < dn; i++)
@@ -306,8 +359,8 @@ static void dwt_encode_1_real(int *a, int dn, int sn, int cas) {
        }
 }
 
-static void dwt_encode_stepsize(int stepsize, int numbps, opj_stepsize_t *bandno_stepsize) {
-       int p, n;
+void opj_dwt_encode_stepsize(OPJ_INT32 stepsize, OPJ_INT32 numbps, opj_stepsize_t *bandno_stepsize) {
+       OPJ_INT32 p, n;
        p = int_floorlog2(stepsize) - 13;
        n = 11 - int_floorlog2(stepsize);
        bandno_stepsize->mant = (n < 0 ? stepsize >> -n : stepsize << n) & 0x7ff;
@@ -320,74 +373,108 @@ static void dwt_encode_stepsize(int stepsize, int numbps, opj_stepsize_t *bandno
 ==========================================================
 */
 
+
 /* <summary>                            */
 /* Forward 5-3 wavelet transform in 2-D. */
 /* </summary>                           */
-void dwt_encode(opj_tcd_tilecomp_t * tilec) {
-       int i, j, k;
-       int *a = NULL;
-       int *aj = NULL;
-       int *bj = NULL;
-       int w, l;
-       
+INLINE opj_bool opj_dwt_encode_procedure(opj_tcd_tilecomp_v2_t * tilec,void (*p_function)(OPJ_INT32 *, OPJ_INT32,OPJ_INT32,OPJ_INT32) )
+{
+       OPJ_INT32 i, j, k;
+       OPJ_INT32 *a = 00;
+       OPJ_INT32 *aj = 00;
+       OPJ_INT32 *bj = 00;
+       OPJ_INT32 w, l;
+
+       OPJ_INT32 rw;                   /* width of the resolution level computed   */
+       OPJ_INT32 rh;                   /* height of the resolution level computed  */
+       OPJ_INT32 l_data_size;
+
+       opj_tcd_resolution_v2_t * l_cur_res = 0;
+       opj_tcd_resolution_v2_t * l_last_res = 0;
+
        w = tilec->x1-tilec->x0;
        l = tilec->numresolutions-1;
        a = tilec->data;
-       
-       for (i = 0; i < l; i++) {
-               int rw;                 /* width of the resolution level computed                                                           */
-               int rh;                 /* height of the resolution level computed                                                          */
-               int rw1;                /* width of the resolution level once lower than computed one                                       */
-               int rh1;                /* height of the resolution level once lower than computed one                                      */
-               int cas_col;    /* 0 = non inversion on horizontal filtering 1 = inversion between low-pass and high-pass filtering */
-               int cas_row;    /* 0 = non inversion on vertical filtering 1 = inversion between low-pass and high-pass filtering   */
-               int dn, sn;
-               
-               rw = tilec->resolutions[l - i].x1 - tilec->resolutions[l - i].x0;
-               rh = tilec->resolutions[l - i].y1 - tilec->resolutions[l - i].y0;
-               rw1= tilec->resolutions[l - i - 1].x1 - tilec->resolutions[l - i - 1].x0;
-               rh1= tilec->resolutions[l - i - 1].y1 - tilec->resolutions[l - i - 1].y0;
-               
-               cas_row = tilec->resolutions[l - i].x0 % 2;
-               cas_col = tilec->resolutions[l - i].y0 % 2;
-        
+
+       l_cur_res = tilec->resolutions + l;
+       l_last_res = l_cur_res - 1;
+
+       rw = l_cur_res->x1 - l_cur_res->x0;
+       rh = l_cur_res->y1 - l_cur_res->y0;
+
+       l_data_size = opj_dwt_max_resolution( tilec->resolutions,tilec->numresolutions) * sizeof(OPJ_INT32);
+       bj = (OPJ_INT32*)opj_malloc(l_data_size);
+       if (! bj) {
+               return OPJ_FALSE;
+       }
+       i = l;
+
+       while (i--) {
+               OPJ_INT32 rw1;          /* width of the resolution level once lower than computed one                                       */
+               OPJ_INT32 rh1;          /* height of the resolution level once lower than computed one                                      */
+               OPJ_INT32 cas_col;      /* 0 = non inversion on horizontal filtering 1 = inversion between low-pass and high-pass filtering */
+               OPJ_INT32 cas_row;      /* 0 = non inversion on vertical filtering 1 = inversion between low-pass and high-pass filtering   */
+               OPJ_INT32 dn, sn;
+
+               rw  = l_cur_res->x1 - l_cur_res->x0;
+               rh  = l_cur_res->y1 - l_cur_res->y0;
+               rw1 = l_last_res->x1 - l_last_res->x0;
+               rh1 = l_last_res->y1 - l_last_res->y0;
+
+               cas_row = l_cur_res->x0 & 1;
+               cas_col = l_cur_res->y0 & 1;
+
                sn = rh1;
                dn = rh - rh1;
-               bj = (int*)opj_malloc(rh * sizeof(int));
-               for (j = 0; j < rw; j++) {
+               for (j = 0; j < rw; ++j) {
                        aj = a + j;
-                       for (k = 0; k < rh; k++)  bj[k] = aj[k*w];
-                       dwt_encode_1(bj, dn, sn, cas_col);
-                       dwt_deinterleave_v(bj, aj, dn, sn, w, cas_col);
+                       for (k = 0; k < rh; ++k) {
+                               bj[k] = aj[k*w];
+                       }
+
+                       (*p_function) (bj, dn, sn, cas_col);
+
+                       opj_dwt_deinterleave_v(bj, aj, dn, sn, w, cas_col);
                }
-               opj_free(bj);
-               
+
                sn = rw1;
                dn = rw - rw1;
-               bj = (int*)opj_malloc(rw * sizeof(int));
+
                for (j = 0; j < rh; j++) {
                        aj = a + j * w;
                        for (k = 0; k < rw; k++)  bj[k] = aj[k];
-                       dwt_encode_1(bj, dn, sn, cas_row);
-                       dwt_deinterleave_h(bj, aj, dn, sn, cas_row);
+                       (*p_function) (bj, dn, sn, cas_row);
+                       opj_dwt_deinterleave_h(bj, aj, dn, sn, cas_row);
                }
-               opj_free(bj);
+
+               l_cur_res = l_last_res;
+
+               --l_last_res;
        }
+
+       opj_free(bj);
+       return OPJ_TRUE;
 }
 
+/* Forward 5-3 wavelet transform in 2-D. */
+/* </summary>                           */
+opj_bool opj_dwt_encode(opj_tcd_tilecomp_v2_t * tilec)
+{
+       return opj_dwt_encode_procedure(tilec,opj_dwt_encode_1);
+}
 
 /* <summary>                            */
 /* Inverse 5-3 wavelet transform in 2-D. */
 /* </summary>                           */
-void dwt_decode(opj_tcd_tilecomp_t* tilec, int numres) {
-       dwt_decode_tile(tilec, numres, &dwt_decode_1);
+opj_bool opj_dwt_decode(opj_tcd_tilecomp_v2_t* tilec, OPJ_UINT32 numres) {
+       return opj_dwt_decode_tile(tilec, numres, &opj_dwt_decode_1);
 }
 
 
 /* <summary>                          */
 /* Get gain of 5-3 wavelet transform. */
 /* </summary>                         */
-int dwt_getgain(int orient) {
+OPJ_UINT32 opj_dwt_getgain(OPJ_UINT32 orient) {
        if (orient == 0)
                return 0;
        if (orient == 1 || orient == 2)
@@ -399,70 +486,28 @@ int dwt_getgain(int orient) {
 /* Get norm of 5-3 wavelet. */
 /* </summary>               */
 double dwt_getnorm(int level, int orient) {
-       return dwt_norms[orient][level];
+       return opj_dwt_norms[orient][level];
+}
+
+/* <summary>                */
+/* Get norm of 5-3 wavelet. */
+/* </summary>               */
+OPJ_FLOAT64 opj_dwt_getnorm(OPJ_UINT32 level, OPJ_UINT32 orient) {
+       return opj_dwt_norms[orient][level];
 }
 
 /* <summary>                             */
 /* Forward 9-7 wavelet transform in 2-D. */
 /* </summary>                            */
-
-void dwt_encode_real(opj_tcd_tilecomp_t * tilec) {
-       int i, j, k;
-       int *a = NULL;
-       int *aj = NULL;
-       int *bj = NULL;
-       int w, l;
-       
-       w = tilec->x1-tilec->x0;
-       l = tilec->numresolutions-1;
-       a = tilec->data;
-       
-       for (i = 0; i < l; i++) {
-               int rw;                 /* width of the resolution level computed                                                     */
-               int rh;                 /* height of the resolution level computed                                                    */
-               int rw1;                /* width of the resolution level once lower than computed one                                 */
-               int rh1;                /* height of the resolution level once lower than computed one                                */
-               int cas_col;    /* 0 = non inversion on horizontal filtering 1 = inversion between low-pass and high-pass filtering */
-               int cas_row;    /* 0 = non inversion on vertical filtering 1 = inversion between low-pass and high-pass filtering   */
-               int dn, sn;
-               
-               rw = tilec->resolutions[l - i].x1 - tilec->resolutions[l - i].x0;
-               rh = tilec->resolutions[l - i].y1 - tilec->resolutions[l - i].y0;
-               rw1= tilec->resolutions[l - i - 1].x1 - tilec->resolutions[l - i - 1].x0;
-               rh1= tilec->resolutions[l - i - 1].y1 - tilec->resolutions[l - i - 1].y0;
-               
-               cas_row = tilec->resolutions[l - i].x0 % 2;
-               cas_col = tilec->resolutions[l - i].y0 % 2;
-               
-               sn = rh1;
-               dn = rh - rh1;
-               bj = (int*)opj_malloc(rh * sizeof(int));
-               for (j = 0; j < rw; j++) {
-                       aj = a + j;
-                       for (k = 0; k < rh; k++)  bj[k] = aj[k*w];
-                       dwt_encode_1_real(bj, dn, sn, cas_col);
-                       dwt_deinterleave_v(bj, aj, dn, sn, w, cas_col);
-               }
-               opj_free(bj);
-               
-               sn = rw1;
-               dn = rw - rw1;
-               bj = (int*)opj_malloc(rw * sizeof(int));
-               for (j = 0; j < rh; j++) {
-                       aj = a + j * w;
-                       for (k = 0; k < rw; k++)  bj[k] = aj[k];
-                       dwt_encode_1_real(bj, dn, sn, cas_row);
-                       dwt_deinterleave_h(bj, aj, dn, sn, cas_row);
-               }
-               opj_free(bj);
-       }
+opj_bool opj_dwt_encode_real(opj_tcd_tilecomp_v2_t * tilec)
+{
+       return opj_dwt_encode_procedure(tilec,opj_dwt_encode_1_real);
 }
 
-
 /* <summary>                          */
 /* Get gain of 9-7 wavelet transform. */
 /* </summary>                         */
-int dwt_getgain_real(int orient) {
+OPJ_UINT32 opj_dwt_getgain_real(OPJ_UINT32 orient) {
        (void)orient;
        return 0;
 }
@@ -471,15 +516,22 @@ int dwt_getgain_real(int orient) {
 /* Get norm of 9-7 wavelet. */
 /* </summary>               */
 double dwt_getnorm_real(int level, int orient) {
-       return dwt_norms_real[orient][level];
+       return opj_dwt_norms_real[orient][level];
+}
+
+/* <summary>                */
+/* Get norm of 9-7 wavelet. */
+/* </summary>               */
+OPJ_FLOAT64 opj_dwt_getnorm_real(OPJ_UINT32 level, OPJ_UINT32 orient) {
+       return opj_dwt_norms_real[orient][level];
 }
 
-void dwt_calc_explicit_stepsizes(opj_tccp_t * tccp, int prec) {
-       int numbands, bandno;
+void opj_dwt_calc_explicit_stepsizes(opj_tccp_t * tccp, OPJ_UINT32 prec) {
+       OPJ_UINT32 numbands, bandno;
        numbands = 3 * tccp->numresolutions - 2;
        for (bandno = 0; bandno < numbands; bandno++) {
-               double stepsize;
-               int resno, level, orient, gain;
+               OPJ_FLOAT64 stepsize;
+               OPJ_UINT32 resno, level, orient, gain;
 
                resno = (bandno == 0) ? 0 : ((bandno - 1) / 3 + 1);
                orient = (bandno == 0) ? 0 : ((bandno - 1) % 3 + 1);
@@ -488,14 +540,14 @@ void dwt_calc_explicit_stepsizes(opj_tccp_t * tccp, int prec) {
                if (tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) {
                        stepsize = 1.0;
                } else {
-                       double norm = dwt_norms_real[orient][level];
+                       OPJ_FLOAT64 norm = opj_dwt_norms_real[orient][level];
                        stepsize = (1 << (gain)) / norm;
                }
-               dwt_encode_stepsize((int) floor(stepsize * 8192.0), prec + gain, &tccp->stepsizes[bandno]);
+               opj_dwt_encode_stepsize((OPJ_INT32) floor(stepsize * 8192.0), prec + gain, &tccp->stepsizes[bandno]);
        }
 }
 
-
+#ifdef OPJ_V1
 /* <summary>                             */
 /* Determine maximum computed resolution level for inverse wavelet transform */
 /* </summary>                            */
@@ -511,28 +563,66 @@ static int dwt_decode_max_resolution(opj_tcd_resolution_t* restrict r, int i) {
        }
        return mr ;
 }
+#endif
+/* <summary>                             */
+/* Determine maximum computed resolution level for inverse wavelet transform */
+/* </summary>                            */
+static OPJ_UINT32 dwt_max_resolution(opj_tcd_resolution_t* restrict r, OPJ_UINT32 i) {
+       OPJ_UINT32 mr   = 0;
+       OPJ_UINT32 w;
+       while( --i ) {
+               ++r;
+               if( mr < ( w = r->x1 - r->x0 ) )
+                       mr = w ;
+               if( mr < ( w = r->y1 - r->y0 ) )
+                       mr = w ;
+       }
+       return mr ;
+}
 
+/* <summary>                             */
+/* Determine maximum computed resolution level for inverse wavelet transform */
+/* </summary>                            */
+OPJ_UINT32 opj_dwt_max_resolution(opj_tcd_resolution_v2_t* restrict r, OPJ_UINT32 i) {
+       OPJ_UINT32 mr   = 0;
+       OPJ_UINT32 w;
+       while( --i ) {
+               ++r;
+               if( mr < ( w = r->x1 - r->x0 ) )
+                       mr = w ;
+               if( mr < ( w = r->y1 - r->y0 ) )
+                       mr = w ;
+       }
+       return mr ;
+}
 
 /* <summary>                            */
 /* Inverse wavelet transform in 2-D.     */
 /* </summary>                           */
-static void dwt_decode_tile(opj_tcd_tilecomp_t* tilec, int numres, DWT1DFN dwt_1D) {
+opj_bool opj_dwt_decode_tile(opj_tcd_tilecomp_v2_t* tilec, OPJ_UINT32 numres, DWT1DFN dwt_1D) {
        dwt_t h;
        dwt_t v;
 
-       opj_tcd_resolution_t* tr = tilec->resolutions;
+       opj_tcd_resolution_v2_t* tr = tilec->resolutions;
 
-       int rw = tr->x1 - tr->x0;       /* width of the resolution level computed */
-       int rh = tr->y1 - tr->y0;       /* height of the resolution level computed */
+       OPJ_UINT32 rw = tr->x1 - tr->x0;        /* width of the resolution level computed */
+       OPJ_UINT32 rh = tr->y1 - tr->y0;        /* height of the resolution level computed */
 
-       int w = tilec->x1 - tilec->x0;
+       OPJ_UINT32 w = tilec->x1 - tilec->x0;
+
+       h.mem = (OPJ_INT32*)
+       opj_aligned_malloc(opj_dwt_max_resolution(tr, numres) * sizeof(OPJ_INT32));
+       if
+               (! h.mem)
+       {
+               return OPJ_FALSE;
+       }
 
-       h.mem = opj_aligned_malloc(dwt_decode_max_resolution(tr, numres) * sizeof(int));
        v.mem = h.mem;
 
        while( --numres) {
-               int * restrict tiledp = tilec->data;
-               int j;
+               OPJ_INT32 * restrict tiledp = tilec->data;
+               OPJ_UINT32 j;
 
                ++tr;
                h.sn = rw;
@@ -545,17 +635,17 @@ static void dwt_decode_tile(opj_tcd_tilecomp_t* tilec, int numres, DWT1DFN dwt_1
                h.cas = tr->x0 % 2;
 
                for(j = 0; j < rh; ++j) {
-                       dwt_interleave_h(&h, &tiledp[j*w]);
+                       opj_dwt_interleave_h(&h, &tiledp[j*w]);
                        (dwt_1D)(&h);
-                       memcpy(&tiledp[j*w], h.mem, rw * sizeof(int));
+                       memcpy(&tiledp[j*w], h.mem, rw * sizeof(OPJ_INT32));
                }
 
                v.dn = rh - v.sn;
                v.cas = tr->y0 % 2;
 
                for(j = 0; j < rw; ++j){
-                       int k;
-                       dwt_interleave_v(&v, &tiledp[j], w);
+                       OPJ_UINT32 k;
+                       opj_dwt_interleave_v(&v, &tiledp[j], w);
                        (dwt_1D)(&v);
                        for(k = 0; k < rh; ++k) {
                                tiledp[k * w + j] = v.mem[k];
@@ -563,17 +653,19 @@ static void dwt_decode_tile(opj_tcd_tilecomp_t* tilec, int numres, DWT1DFN dwt_1
                }
        }
        opj_aligned_free(h.mem);
+       return OPJ_TRUE;
 }
 
-static void v4dwt_interleave_h(v4dwt_t* restrict w, float* restrict a, int x, int size){
-       float* restrict bi = (float*) (w->wavelet + w->cas);
-       int count = w->sn;
-       int i, k;
+void opj_v4dwt_interleave_h(v4dwt_t* restrict w, OPJ_FLOAT32* restrict a, OPJ_INT32 x, OPJ_INT32 size){
+       OPJ_FLOAT32* restrict bi = (OPJ_FLOAT32*) (w->wavelet + w->cas);
+       OPJ_INT32 count = w->sn;
+       OPJ_INT32 i, k;
+
        for(k = 0; k < 2; ++k){
-               if (count + 3 * x < size && ((size_t) a & 0x0f) == 0 && ((size_t) bi & 0x0f) == 0 && (x & 0x0f) == 0) {
+               if ( count + 3 * x < size && ((size_t) a & 0x0f) == 0 && ((size_t) bi & 0x0f) == 0 && (x & 0x0f) == 0 ) {
                        /* Fast code path */
                        for(i = 0; i < count; ++i){
-                               int j = i;
+                               OPJ_INT32 j = i;
                                bi[i*8    ] = a[j];
                                j += x;
                                bi[i*8 + 1] = a[j];
@@ -582,22 +674,24 @@ static void v4dwt_interleave_h(v4dwt_t* restrict w, float* restrict a, int x, in
                                j += x;
                                bi[i*8 + 3] = a[j];
                        }
-               } else {
-                       /* Slow code path */
-               for(i = 0; i < count; ++i){
-                       int j = i;
-                       bi[i*8    ] = a[j];
-                       j += x;
-                       if(j > size) continue;
-                       bi[i*8 + 1] = a[j];
-                       j += x;
-                       if(j > size) continue;
-                       bi[i*8 + 2] = a[j];
-                       j += x;
-                       if(j > size) continue;
-                       bi[i*8 + 3] = a[j];
                }
+               else {
+                       /* Slow code path */
+                       for(i = 0; i < count; ++i){
+                               OPJ_INT32 j = i;
+                               bi[i*8    ] = a[j];
+                               j += x;
+                               if(j >= size) continue;
+                               bi[i*8 + 1] = a[j];
+                               j += x;
+                               if(j >= size) continue;
+                               bi[i*8 + 2] = a[j];
+                               j += x;
+                               if(j >= size) continue;
+                               bi[i*8 + 3] = a[j]; /* This one*/
+                       }
                }
+
                bi = (float*) (w->wavelet + 1 - w->cas);
                a += w->sn;
                size -= w->sn;
@@ -605,24 +699,27 @@ static void v4dwt_interleave_h(v4dwt_t* restrict w, float* restrict a, int x, in
        }
 }
 
-static void v4dwt_interleave_v(v4dwt_t* restrict v , float* restrict a , int x){
+void opj_v4dwt_interleave_v(v4dwt_t* restrict v , OPJ_FLOAT32* restrict a , OPJ_INT32 x, OPJ_INT32 nb_elts_read){
        v4* restrict bi = v->wavelet + v->cas;
-       int i;
+       OPJ_INT32 i;
+
        for(i = 0; i < v->sn; ++i){
-               memcpy(&bi[i*2], &a[i*x], 4 * sizeof(float));
+               memcpy(&bi[i*2], &a[i*x], nb_elts_read * sizeof(OPJ_FLOAT32));
        }
+
        a += v->sn * x;
        bi = v->wavelet + 1 - v->cas;
+
        for(i = 0; i < v->dn; ++i){
-               memcpy(&bi[i*2], &a[i*x], 4 * sizeof(float));
+               memcpy(&bi[i*2], &a[i*x], nb_elts_read * sizeof(OPJ_FLOAT32));
        }
 }
 
 #ifdef __SSE__
 
-static void v4dwt_decode_step1_sse(v4* w, int count, const __m128 c){
+void opj_v4dwt_decode_step1_sse(v4* w, int count, const __m128 c){
        __m128* restrict vw = (__m128*) w;
-       int i;
+       OPJ_INT32 i;
        /* 4x unrolled loop */
        for(i = 0; i < count >> 2; ++i){
                *vw = _mm_mul_ps(*vw, c);
@@ -641,7 +738,7 @@ static void v4dwt_decode_step1_sse(v4* w, int count, const __m128 c){
        }
 }
 
-static void v4dwt_decode_step2_sse(v4* l, v4* w, int k, int m, __m128 c){
+void opj_v4dwt_decode_step2_sse(v4* l, v4* w, int k, int m, __m128 c){
        __m128* restrict vl = (__m128*) l;
        __m128* restrict vw = (__m128*) w;
        int i;
@@ -669,14 +766,15 @@ static void v4dwt_decode_step2_sse(v4* l, v4* w, int k, int m, __m128 c){
 
 #else
 
-static void v4dwt_decode_step1(v4* w, int count, const float c){
-       float* restrict fw = (float*) w;
-       int i;
+void opj_v4dwt_decode_step1(v4* w, OPJ_INT32 count, const OPJ_FLOAT32 c)
+{
+       OPJ_FLOAT32* restrict fw = (OPJ_FLOAT32*) w;
+       OPJ_INT32 i;
        for(i = 0; i < count; ++i){
-               float tmp1 = fw[i*8    ];
-               float tmp2 = fw[i*8 + 1];
-               float tmp3 = fw[i*8 + 2];
-               float tmp4 = fw[i*8 + 3];
+               OPJ_FLOAT32 tmp1 = fw[i*8    ];
+               OPJ_FLOAT32 tmp2 = fw[i*8 + 1];
+               OPJ_FLOAT32 tmp3 = fw[i*8 + 2];
+               OPJ_FLOAT32 tmp4 = fw[i*8 + 3];
                fw[i*8    ] = tmp1 * c;
                fw[i*8 + 1] = tmp2 * c;
                fw[i*8 + 2] = tmp3 * c;
@@ -684,23 +782,24 @@ static void v4dwt_decode_step1(v4* w, int count, const float c){
        }
 }
 
-static void v4dwt_decode_step2(v4* l, v4* w, int k, int m, float c){
-       float* restrict fl = (float*) l;
-       float* restrict fw = (float*) w;
+void opj_v4dwt_decode_step2(v4* l, v4* w, OPJ_INT32 k, OPJ_INT32 m, OPJ_FLOAT32 c)
+{
+       OPJ_FLOAT32* restrict fl = (OPJ_FLOAT32*) l;
+       OPJ_FLOAT32* restrict fw = (OPJ_FLOAT32*) w;
        int i;
        for(i = 0; i < m; ++i){
-               float tmp1_1 = fl[0];
-               float tmp1_2 = fl[1];
-               float tmp1_3 = fl[2];
-               float tmp1_4 = fl[3];
-               float tmp2_1 = fw[-4];
-               float tmp2_2 = fw[-3];
-               float tmp2_3 = fw[-2];
-               float tmp2_4 = fw[-1];
-               float tmp3_1 = fw[0];
-               float tmp3_2 = fw[1];
-               float tmp3_3 = fw[2];
-               float tmp3_4 = fw[3];
+               OPJ_FLOAT32 tmp1_1 = fl[0];
+               OPJ_FLOAT32 tmp1_2 = fl[1];
+               OPJ_FLOAT32 tmp1_3 = fl[2];
+               OPJ_FLOAT32 tmp1_4 = fl[3];
+               OPJ_FLOAT32 tmp2_1 = fw[-4];
+               OPJ_FLOAT32 tmp2_2 = fw[-3];
+               OPJ_FLOAT32 tmp2_3 = fw[-2];
+               OPJ_FLOAT32 tmp2_4 = fw[-1];
+               OPJ_FLOAT32 tmp3_1 = fw[0];
+               OPJ_FLOAT32 tmp3_2 = fw[1];
+               OPJ_FLOAT32 tmp3_3 = fw[2];
+               OPJ_FLOAT32 tmp3_4 = fw[3];
                fw[-4] = tmp2_1 + ((tmp1_1 + tmp3_1) * c);
                fw[-3] = tmp2_2 + ((tmp1_2 + tmp3_2) * c);
                fw[-2] = tmp2_3 + ((tmp1_3 + tmp3_3) * c);
@@ -709,20 +808,20 @@ static void v4dwt_decode_step2(v4* l, v4* w, int k, int m, float c){
                fw += 8;
        }
        if(m < k){
-               float c1;
-               float c2;
-               float c3;
-               float c4;
+               OPJ_FLOAT32 c1;
+               OPJ_FLOAT32 c2;
+               OPJ_FLOAT32 c3;
+               OPJ_FLOAT32 c4;
                c += c;
                c1 = fl[0] * c;
                c2 = fl[1] * c;
                c3 = fl[2] * c;
                c4 = fl[3] * c;
                for(; m < k; ++m){
-                       float tmp1 = fw[-4];
-                       float tmp2 = fw[-3];
-                       float tmp3 = fw[-2];
-                       float tmp4 = fw[-1];
+                       OPJ_FLOAT32 tmp1 = fw[-4];
+                       OPJ_FLOAT32 tmp2 = fw[-3];
+                       OPJ_FLOAT32 tmp3 = fw[-2];
+                       OPJ_FLOAT32 tmp4 = fw[-1];
                        fw[-4] = tmp1 + c1;
                        fw[-3] = tmp2 + c2;
                        fw[-2] = tmp3 + c3;
@@ -737,7 +836,8 @@ static void v4dwt_decode_step2(v4* l, v4* w, int k, int m, float c){
 /* <summary>                             */
 /* Inverse 9-7 wavelet transform in 1-D. */
 /* </summary>                            */
-static void v4dwt_decode(v4dwt_t* restrict dwt){
+void opj_v4dwt_decode(v4dwt_t* restrict dwt)
+{
        int a, b;
        if(dwt->cas == 0) {
                if(!((dwt->dn > 0) || (dwt->sn > 1))){
@@ -753,26 +853,30 @@ static void v4dwt_decode(v4dwt_t* restrict dwt){
                b = 0;
        }
 #ifdef __SSE__
-       v4dwt_decode_step1_sse(dwt->wavelet+a, dwt->sn, _mm_set1_ps(K));
-       v4dwt_decode_step1_sse(dwt->wavelet+b, dwt->dn, _mm_set1_ps(c13318));
-       v4dwt_decode_step2_sse(dwt->wavelet+b, dwt->wavelet+a+1, dwt->sn, int_min(dwt->sn, dwt->dn-a), _mm_set1_ps(dwt_delta));
-       v4dwt_decode_step2_sse(dwt->wavelet+a, dwt->wavelet+b+1, dwt->dn, int_min(dwt->dn, dwt->sn-b), _mm_set1_ps(dwt_gamma));
-       v4dwt_decode_step2_sse(dwt->wavelet+b, dwt->wavelet+a+1, dwt->sn, int_min(dwt->sn, dwt->dn-a), _mm_set1_ps(dwt_beta));
-       v4dwt_decode_step2_sse(dwt->wavelet+a, dwt->wavelet+b+1, dwt->dn, int_min(dwt->dn, dwt->sn-b), _mm_set1_ps(dwt_alpha));
+       opj_v4dwt_decode_step1_sse(dwt->wavelet+a, dwt->sn, _mm_set1_ps(opj_K));
+       opj_v4dwt_decode_step1_sse(dwt->wavelet+b, dwt->dn, _mm_set1_ps(opj_c13318));
+       opj_v4dwt_decode_step2_sse(dwt->wavelet+b, dwt->wavelet+a+1, dwt->sn, int_min(dwt->sn, dwt->dn-a), _mm_set1_ps(opj_dwt_delta));
+       opj_v4dwt_decode_step2_sse(dwt->wavelet+a, dwt->wavelet+b+1, dwt->dn, int_min(dwt->dn, dwt->sn-b), _mm_set1_ps(opj_dwt_gamma));
+       opj_v4dwt_decode_step2_sse(dwt->wavelet+b, dwt->wavelet+a+1, dwt->sn, int_min(dwt->sn, dwt->dn-a), _mm_set1_ps(opj_dwt_beta));
+       opj_v4dwt_decode_step2_sse(dwt->wavelet+a, dwt->wavelet+b+1, dwt->dn, int_min(dwt->dn, dwt->sn-b), _mm_set1_ps(opj_dwt_alpha));
 #else
-       v4dwt_decode_step1(dwt->wavelet+a, dwt->sn, K);
-       v4dwt_decode_step1(dwt->wavelet+b, dwt->dn, c13318);
-       v4dwt_decode_step2(dwt->wavelet+b, dwt->wavelet+a+1, dwt->sn, int_min(dwt->sn, dwt->dn-a), dwt_delta);
-       v4dwt_decode_step2(dwt->wavelet+a, dwt->wavelet+b+1, dwt->dn, int_min(dwt->dn, dwt->sn-b), dwt_gamma);
-       v4dwt_decode_step2(dwt->wavelet+b, dwt->wavelet+a+1, dwt->sn, int_min(dwt->sn, dwt->dn-a), dwt_beta);
-       v4dwt_decode_step2(dwt->wavelet+a, dwt->wavelet+b+1, dwt->dn, int_min(dwt->dn, dwt->sn-b), dwt_alpha);
+       opj_v4dwt_decode_step1(dwt->wavelet+a, dwt->sn, opj_K);
+       opj_v4dwt_decode_step1(dwt->wavelet+b, dwt->dn, opj_c13318);
+       opj_v4dwt_decode_step2(dwt->wavelet+b, dwt->wavelet+a+1, dwt->sn, int_min(dwt->sn, dwt->dn-a), opj_dwt_delta);
+       opj_v4dwt_decode_step2(dwt->wavelet+a, dwt->wavelet+b+1, dwt->dn, int_min(dwt->dn, dwt->sn-b), opj_dwt_gamma);
+       opj_v4dwt_decode_step2(dwt->wavelet+b, dwt->wavelet+a+1, dwt->sn, int_min(dwt->sn, dwt->dn-a), opj_dwt_beta);
+       opj_v4dwt_decode_step2(dwt->wavelet+a, dwt->wavelet+b+1, dwt->dn, int_min(dwt->dn, dwt->sn-b), opj_dwt_alpha);
 #endif
 }
 
+
+/* KEEP TRUNK VERSION + return type of v2 because rev557 */
 /* <summary>                             */
 /* Inverse 9-7 wavelet transform in 2-D. */
 /* </summary>                            */
-void dwt_decode_real(opj_tcd_tilecomp_t* restrict tilec, int numres){
+/* V1 void dwt_decode_real(opj_tcd_tilecomp_t* restrict tilec, int numres){ */
+opj_bool dwt_decode_real(opj_tcd_tilecomp_t* restrict tilec, int numres)
+{
        v4dwt_t h;
        v4dwt_t v;
 
@@ -783,7 +887,7 @@ void dwt_decode_real(opj_tcd_tilecomp_t* restrict tilec, int numres){
 
        int w = tilec->x1 - tilec->x0;
 
-       h.wavelet = (v4*) opj_aligned_malloc((dwt_decode_max_resolution(res, numres)+5) * sizeof(v4));
+       h.wavelet = (v4*) opj_aligned_malloc((dwt_max_resolution(res, numres)+5) * sizeof(v4));
        v.wavelet = h.wavelet;
 
        while( --numres) {
@@ -804,8 +908,8 @@ void dwt_decode_real(opj_tcd_tilecomp_t* restrict tilec, int numres){
 
                for(j = rh; j > 3; j -= 4){
                        int k;
-                       v4dwt_interleave_h(&h, aj, w, bufsize);
-                       v4dwt_decode(&h);
+                       opj_v4dwt_interleave_h(&h, aj, w, bufsize);
+                       opj_v4dwt_decode(&h);
                                for(k = rw; --k >= 0;){
                                        aj[k    ] = h.wavelet[k].f[0];
                                        aj[k+w  ] = h.wavelet[k].f[1];
@@ -818,8 +922,8 @@ void dwt_decode_real(opj_tcd_tilecomp_t* restrict tilec, int numres){
                if (rh & 0x03) {
                                int k;
                        j = rh & 0x03;
-                       v4dwt_interleave_h(&h, aj, w, bufsize);
-                       v4dwt_decode(&h);
+                       opj_v4dwt_interleave_h(&h, aj, w, bufsize);
+                       opj_v4dwt_decode(&h);
                                for(k = rw; --k >= 0;){
                                        switch(j) {
                                                case 3: aj[k+w*2] = h.wavelet[k].f[2];
@@ -835,8 +939,8 @@ void dwt_decode_real(opj_tcd_tilecomp_t* restrict tilec, int numres){
                aj = (float*) tilec->data;
                for(j = rw; j > 3; j -= 4){
                        int k;
-                       v4dwt_interleave_v(&v, aj, w);
-                       v4dwt_decode(&v);
+                       opj_v4dwt_interleave_v(&v, aj, w, 4);
+                       opj_v4dwt_decode(&v);
                                for(k = 0; k < rh; ++k){
                                        memcpy(&aj[k*w], &v.wavelet[k], 4 * sizeof(float));
                                }
@@ -845,8 +949,8 @@ void dwt_decode_real(opj_tcd_tilecomp_t* restrict tilec, int numres){
                if (rw & 0x03){
                                int k;
                        j = rw & 0x03;
-                       v4dwt_interleave_v(&v, aj, w);
-                       v4dwt_decode(&v);
+                       opj_v4dwt_interleave_v(&v, aj, w, j);
+                       opj_v4dwt_decode(&v);
                                for(k = 0; k < rh; ++k){
                                        memcpy(&aj[k*w], &v.wavelet[k], j * sizeof(float));
                                }
@@ -854,5 +958,104 @@ void dwt_decode_real(opj_tcd_tilecomp_t* restrict tilec, int numres){
        }
 
        opj_aligned_free(h.wavelet);
+       return OPJ_TRUE;
 }
 
+
+/* <summary>                             */
+/* Inverse 9-7 wavelet transform in 2-D. */
+/* </summary>                            */
+opj_bool dwt_decode_real_v2(opj_tcd_tilecomp_v2_t* restrict tilec, OPJ_UINT32 numres)
+{
+       v4dwt_t h;
+       v4dwt_t v;
+
+       opj_tcd_resolution_v2_t* res = tilec->resolutions;
+
+       OPJ_UINT32 rw = res->x1 - res->x0;      /* width of the resolution level computed */
+       OPJ_UINT32 rh = res->y1 - res->y0;      /* height of the resolution level computed */
+
+       OPJ_UINT32 w = tilec->x1 - tilec->x0;
+
+       h.wavelet = (v4*) opj_aligned_malloc((opj_dwt_max_resolution(res, numres)+5) * sizeof(v4));
+       v.wavelet = h.wavelet;
+
+       while( --numres) {
+               OPJ_FLOAT32 * restrict aj = (OPJ_FLOAT32*) tilec->data;
+               OPJ_UINT32 bufsize = (tilec->x1 - tilec->x0) * (tilec->y1 - tilec->y0);
+               OPJ_INT32 j;
+
+               h.sn = rw;
+               v.sn = rh;
+
+               ++res;
+
+               rw = res->x1 - res->x0; /* width of the resolution level computed */
+               rh = res->y1 - res->y0; /* height of the resolution level computed */
+
+               h.dn = rw - h.sn;
+               h.cas = res->x0 % 2;
+
+               for(j = rh; j > 3; j -= 4) {
+                       OPJ_INT32 k;
+                       opj_v4dwt_interleave_h(&h, aj, w, bufsize);
+                       opj_v4dwt_decode(&h);
+
+                       for(k = rw; --k >= 0;){
+                               aj[k    ] = h.wavelet[k].f[0];
+                               aj[k+w  ] = h.wavelet[k].f[1];
+                               aj[k+w*2] = h.wavelet[k].f[2];
+                               aj[k+w*3] = h.wavelet[k].f[3];
+                       }
+
+                       aj += w*4;
+                       bufsize -= w*4;
+               }
+
+               if (rh & 0x03) {
+                       OPJ_INT32 k;
+                       j = rh & 0x03;
+                       opj_v4dwt_interleave_h(&h, aj, w, bufsize);
+                       opj_v4dwt_decode(&h);
+                       for(k = rw; --k >= 0;){
+                               switch(j) {
+                                       case 3: aj[k+w*2] = h.wavelet[k].f[2];
+                                       case 2: aj[k+w  ] = h.wavelet[k].f[1];
+                                       case 1: aj[k    ] = h.wavelet[k].f[0];
+                               }
+                       }
+               }
+
+               v.dn = rh - v.sn;
+               v.cas = res->y0 % 2;
+
+               aj = (OPJ_FLOAT32*) tilec->data;
+               for(j = rw; j > 3; j -= 4){
+                       OPJ_UINT32 k;
+
+                       opj_v4dwt_interleave_v(&v, aj, w, 4);
+                       opj_v4dwt_decode(&v);
+
+                       for(k = 0; k < rh; ++k){
+                               memcpy(&aj[k*w], &v.wavelet[k], 4 * sizeof(OPJ_FLOAT32));
+                       }
+                       aj += 4;
+               }
+
+               if (rw & 0x03){
+                       OPJ_UINT32 k;
+
+                       j = rw & 0x03;
+
+                       opj_v4dwt_interleave_v(&v, aj, w, j);
+                       opj_v4dwt_decode(&v);
+
+                       for(k = 0; k < rh; ++k){
+                               memcpy(&aj[k*w], &v.wavelet[k], j * sizeof(OPJ_FLOAT32));
+                       }
+               }
+       }
+
+       opj_aligned_free(h.wavelet);
+       return OPJ_TRUE;
+}