remove deprecated v1 style function opj_decode and opj_decode_with_info
[openjpeg.git] / libopenjpeg / dwt.c
index bb40fd02679c790449e7f94ca4297820e554ccd0..13a3001b31aa7fc2c3d6b640cb0d8e54b8d69de5 100644 (file)
@@ -64,12 +64,12 @@ typedef struct v4dwt_local {
        int             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 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 float K      = 1.230174105f; //  10078
+static const float K      = 1.230174105f; /*  10078 */
 /* FIXME: What is this constant? */
 static const float c13318 = 1.625732422f;
 
@@ -128,6 +128,12 @@ Inverse wavelet transform in 2-D.
 */
 static opj_bool dwt_decode_tile_v2(opj_tcd_tilecomp_v2_t* tilec, OPJ_UINT32 i, DWT1DFN fn);
 
+static opj_bool dwt_encode_procedure(  opj_tcd_tilecomp_v2_t * tilec,
+                                                                               void (*p_function)(OPJ_INT32 *, OPJ_INT32,OPJ_INT32,OPJ_INT32) );
+
+static OPJ_UINT32 dwt_max_resolution_v2(opj_tcd_resolution_v2_t* restrict r, OPJ_UINT32 i);
+
+
 /*@}*/
 
 /*@}*/
@@ -383,6 +389,95 @@ void dwt_encode(opj_tcd_tilecomp_t * tilec) {
        }
 }
 
+/* <summary>                            */
+/* Forward 5-3 wavelet transform in 2-D. */
+/* </summary>                           */
+INLINE opj_bool 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;
+
+       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 = dwt_max_resolution_v2( 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;
+               for (j = 0; j < rw; ++j) {
+                       aj = a + j;
+                       for (k = 0; k < rh; ++k) {
+                               bj[k] = aj[k*w];
+                       }
+
+                       (*p_function) (bj, dn, sn, cas_col);
+
+                       dwt_deinterleave_v(bj, aj, dn, sn, w, cas_col);
+               }
+
+               sn = rw1;
+               dn = rw - rw1;
+
+               for (j = 0; j < rh; j++) {
+                       aj = a + j * w;
+                       for (k = 0; k < rw; k++)  bj[k] = aj[k];
+                       (*p_function) (bj, dn, sn, cas_row);
+                       dwt_deinterleave_h(bj, aj, dn, sn, cas_row);
+               }
+
+               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 dwt_encode_v2(opj_tcd_tilecomp_v2_t * tilec)
+{
+       return dwt_encode_procedure(tilec,dwt_encode_1);
+}
+
 #ifdef OPJ_V1
 /* <summary>                            */
 /* Inverse 5-3 wavelet transform in 2-D. */
@@ -492,6 +587,13 @@ void dwt_encode_real(opj_tcd_tilecomp_t * tilec) {
        }
 }
 
+/* <summary>                             */
+/* Forward 9-7 wavelet transform in 2-D. */
+/* </summary>                            */
+opj_bool dwt_encode_real_v2(opj_tcd_tilecomp_v2_t * tilec)
+{
+       return dwt_encode_procedure(tilec,dwt_encode_1_real);
+}
 
 /* <summary>                          */
 /* Get gain of 9-7 wavelet transform. */
@@ -764,8 +866,9 @@ static void v4dwt_interleave_h(v4dwt_t* restrict w, float* restrict a, int x, in
        float* restrict bi = (float*) (w->wavelet + w->cas);
        int count = w->sn;
        int 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;
@@ -777,22 +880,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){
+                               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]; /* This one*/
+                       }
                }
+
                bi = (float*) (w->wavelet + 1 - w->cas);
                a += w->sn;
                size -= w->sn;
@@ -800,16 +905,19 @@ 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){
+static void v4dwt_interleave_v(v4dwt_t* restrict v , float* restrict a , int x, int nb_elts_read){
        v4* restrict bi = v->wavelet + v->cas;
        int 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(float));
        }
+
        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(float));
        }
 }
 
@@ -965,11 +1073,11 @@ static void v4dwt_decode(v4dwt_t* restrict dwt){
 }
 
 
-// KEEP TRUNK VERSION + return type of v2 because rev557
+/* KEEP TRUNK VERSION + return type of v2 because rev557 */
 /* <summary>                             */
 /* Inverse 9-7 wavelet transform in 2-D. */
 /* </summary>                            */
-// V1 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;
@@ -1033,7 +1141,7 @@ opj_bool 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_interleave_v(&v, aj, w, 4);
                        v4dwt_decode(&v);
                                for(k = 0; k < rh; ++k){
                                        memcpy(&aj[k*w], &v.wavelet[k], 4 * sizeof(float));
@@ -1043,7 +1151,7 @@ opj_bool 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_interleave_v(&v, aj, w, j);
                        v4dwt_decode(&v);
                                for(k = 0; k < rh; ++k){
                                        memcpy(&aj[k*w], &v.wavelet[k], j * sizeof(float));
@@ -1087,58 +1195,66 @@ opj_bool dwt_decode_real_v2(opj_tcd_tilecomp_v2_t* restrict tilec, OPJ_UINT32 nu
                rh = res->y1 - res->y0; /* height of the resolution level computed */
 
                h.dn = rw - h.sn;
-               h.cas = res->x0 & 1;
+               h.cas = res->x0 % 2;
 
-               for(j = rh; j > 0; j -= 4) {
+               for(j = rh; j > 3; j -= 4) {
+                       OPJ_INT32 k;
                        v4dwt_interleave_h(&h, aj, w, bufsize);
                        v4dwt_decode(&h);
 
-                       if(j >= 4){
-                               OPJ_INT32 k = rw;
-
-                               while (--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];
-                               }
-                       }
-                       else {
-                               OPJ_INT32 k = rw;
-
-                               while (--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];
-                                       }
-                               }
+                       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) {
+                       int k;
+                       j = rh & 0x03;
+                       v4dwt_interleave_h(&h, aj, w, bufsize);
+                       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 > 0; j -= 4){
-                       v4dwt_interleave_v(&v, aj, w);
+               for(j = rw; j > 3; j -= 4){
+                       OPJ_INT32 k;
+
+                       v4dwt_interleave_v(&v, aj, w, 4);
                        v4dwt_decode(&v);
-                       if(j >= 4){
-                               OPJ_UINT32 k;
-                               for(k = 0; k < rh; ++k){
-                                       memcpy(&aj[k*w], &v.wavelet[k], 4 * sizeof(OPJ_FLOAT32));
-                               }
-                       }else{
-                               OPJ_UINT32 k;
-                               for(k = 0; k < rh; ++k){
-                                       memcpy(&aj[k*w], &v.wavelet[k], j * sizeof(OPJ_FLOAT32));
-                               }
+
+                       for(k = 0; k < rh; ++k){
+                               memcpy(&aj[k*w], &v.wavelet[k], 4 * sizeof(OPJ_FLOAT32));
                        }
                        aj += 4;
                }
+
+               if (rw & 0x03){
+                       OPJ_INT32 k;
+
+                       j = rw & 0x03;
+
+                       v4dwt_interleave_v(&v, aj, w, j);
+                       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);