remove deprecated v1 style function opj_decode and opj_decode_with_info
[openjpeg.git] / libopenjpeg / dwt.c
index 69f385ea4671a4d9b618ab13715789125627160b..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;
 
@@ -123,6 +123,16 @@ static void dwt_decode_tile(opj_tcd_tilecomp_t* tilec, int i, DWT1DFN fn);
 #endif
 static opj_bool dwt_decode_tile(opj_tcd_tilecomp_t* tilec, OPJ_UINT32 i, DWT1DFN fn);
 
+/**
+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);
+
 
 /*@}*/
 
@@ -379,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. */
@@ -395,6 +494,13 @@ opj_bool dwt_decode(opj_tcd_tilecomp_t* tilec, OPJ_UINT32 numres) {
        return dwt_decode_tile(tilec, numres, &dwt_decode_1);
 }
 
+/* <summary>                            */
+/* Inverse 5-3 wavelet transform in 2-D. */
+/* </summary>                           */
+opj_bool dwt_decode_v2(opj_tcd_tilecomp_v2_t* tilec, OPJ_UINT32 numres) {
+       return dwt_decode_tile_v2(tilec, numres, &dwt_decode_1);
+}
+
 
 /* <summary>                          */
 /* Get gain of 5-3 wavelet transform. */
@@ -407,6 +513,17 @@ int dwt_getgain(int orient) {
        return 2;
 }
 
+/* <summary>                          */
+/* Get gain of 5-3 wavelet transform. */
+/* </summary>                         */
+OPJ_UINT32 dwt_getgain_v2(OPJ_UINT32 orient) {
+       if (orient == 0)
+               return 0;
+       if (orient == 1 || orient == 2)
+               return 1;
+       return 2;
+}
+
 /* <summary>                */
 /* Get norm of 5-3 wavelet. */
 /* </summary>               */
@@ -470,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. */
@@ -479,6 +603,14 @@ int dwt_getgain_real(int orient) {
        return 0;
 }
 
+/* <summary>                          */
+/* Get gain of 9-7 wavelet transform. */
+/* </summary>                         */
+OPJ_UINT32 dwt_getgain_real_v2(OPJ_UINT32 orient) {
+       (void)orient;
+       return 0;
+}
+
 /* <summary>                */
 /* Get norm of 9-7 wavelet. */
 /* </summary>               */
@@ -540,6 +672,22 @@ static OPJ_UINT32 dwt_max_resolution(opj_tcd_resolution_t* restrict r, OPJ_UINT3
        return mr ;
 }
 
+/* <summary>                             */
+/* Determine maximum computed resolution level for inverse wavelet transform */
+/* </summary>                            */
+static OPJ_UINT32 dwt_max_resolution_v2(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 ;
+}
+
 #ifdef OPJ_V1
 /* <summary>                            */
 /* Inverse wavelet transform in 2-D.     */
@@ -654,12 +802,73 @@ static opj_bool dwt_decode_tile(opj_tcd_tilecomp_t* tilec, OPJ_UINT32 numres, DW
        return OPJ_TRUE;
 }
 
+/* <summary>                            */
+/* Inverse wavelet transform in 2-D.     */
+/* </summary>                           */
+static opj_bool dwt_decode_tile_v2(opj_tcd_tilecomp_v2_t* tilec, OPJ_UINT32 numres, DWT1DFN dwt_1D) {
+       dwt_t h;
+       dwt_t v;
+
+       opj_tcd_resolution_v2_t* tr = tilec->resolutions;
+
+       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 */
+
+       OPJ_UINT32 w = tilec->x1 - tilec->x0;
+
+       h.mem = (OPJ_INT32*)
+       opj_aligned_malloc(dwt_max_resolution_v2(tr, numres) * sizeof(OPJ_INT32));
+       if
+               (! h.mem)
+       {
+               return OPJ_FALSE;
+       }
+
+       v.mem = h.mem;
+
+       while( --numres) {
+               OPJ_INT32 * restrict tiledp = tilec->data;
+               OPJ_UINT32 j;
+
+               ++tr;
+               h.sn = rw;
+               v.sn = rh;
+
+               rw = tr->x1 - tr->x0;
+               rh = tr->y1 - tr->y0;
+
+               h.dn = rw - h.sn;
+               h.cas = tr->x0 % 2;
+
+               for(j = 0; j < rh; ++j) {
+                       dwt_interleave_h(&h, &tiledp[j*w]);
+                       (dwt_1D)(&h);
+                       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){
+                       OPJ_UINT32 k;
+                       dwt_interleave_v(&v, &tiledp[j], w);
+                       (dwt_1D)(&v);
+                       for(k = 0; k < rh; ++k) {
+                               tiledp[k * w + j] = v.mem[k];
+                       }
+               }
+       }
+       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;
+
        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;
@@ -671,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;
@@ -694,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));
        }
 }
 
@@ -859,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;
@@ -927,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));
@@ -937,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));
@@ -949,3 +1163,100 @@ opj_bool dwt_decode_real(opj_tcd_tilecomp_t* restrict tilec, int numres){
        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((dwt_max_resolution_v2(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;
+                       v4dwt_interleave_h(&h, aj, w, bufsize);
+                       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) {
+                       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 > 3; j -= 4){
+                       OPJ_INT32 k;
+
+                       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(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);
+       return OPJ_TRUE;
+}