* Quantization bug fixed when using 9x7 DWT (comment keyword : quantizbug1)
authorAntonin Descampe <antonin@gmail.com>
Tue, 13 Jul 2004 09:17:17 +0000 (09:17 +0000)
committerAntonin Descampe <antonin@gmail.com>
Tue, 13 Jul 2004 09:17:17 +0000 (09:17 +0000)
* Multiplication bug fixed when dividing by 8192 (comment keyword : multbug1)

libopenjpeg/dwt.c
libopenjpeg/fix.c
libopenjpeg/t1.c
libopenjpeg/tcd.c
libopenjpeg/tcd.h

index 712d68baa8e6aca7df6b62b5799c336d5af2060f..92d006ad39dc953ff8a07be91e2a0f8a0e43258b 100644 (file)
@@ -67,7 +67,7 @@ static int *b = NULL;
 static int lastSizeOfB = 0;
 
 /* <summary>       */
-/* Claning memory. */
+/* Cleaning memory. */
 /* </summary>      */
 
 void dwt_clean()
index 53a8a8cbc96f43bd160e9cbdd2abb2e584083c5f..77ca6d630f9ec354a298c3c5820221adf21aae39 100644 (file)
@@ -25,6 +25,7 @@
  */
 
 #include "fix.h"
+#include <math.h>   //Add Antonin : multbug1
 
 #ifdef WIN32
 #define int64 __int64
 /*
  * Multiply two fixed-precision rational numbers.
  */
+
+//int fix_mul(int a, int b)
+//{
+//  return (int) ((int64) a * (int64) b >> 13);
+//}
+
+//Mod Antonin : multbug1
 int fix_mul(int a, int b)
 {
-  return (int) ((int64) a * (int64) b >> 13);
+  double tmp= (double) ((int64) a * (int64) b);
+  int64 v = (int64) ((fabs(tmp/8192.0)>=floor(fabs(tmp/8192.0))+0.5)?fabs(tmp/8192.0)+1.0:fabs(tmp/8192.0));
+  v = (tmp<0)?-v:v;
+  return (int) v;
 }
+//doM
+
index deb6b98a8637c1614a845bbee3ea761449179629..b1b22469f9ee79fc6d7321a85d59740c95697275 100644 (file)
@@ -92,7 +92,7 @@ static int t1_lut_nmsedec_sig0[1 << T1_NMSEDEC_BITS];
 static int t1_lut_nmsedec_ref[1 << T1_NMSEDEC_BITS];
 static int t1_lut_nmsedec_ref0[1 << T1_NMSEDEC_BITS];
 
-static int t1_data[T1_MAXCBLKH][T1_MAXCBLKH];
+static int t1_data[T1_MAXCBLKH][T1_MAXCBLKW];
 static int t1_flags[T1_MAXCBLKH + 2][T1_MAXCBLKH + 2];
 
 int t1_getctxno_zc(int f, int orient)
@@ -187,6 +187,7 @@ void t1_enc_sigpass_step(int *fp, int *dp, int orient, int bpno, int one,
   }
 }
 
+
 void t1_dec_sigpass_step(int *fp, int *dp, int orient, int oneplushalf,
                         char type, int vsc)
 {
@@ -247,7 +248,9 @@ void t1_dec_sigpass(int w, int h, int bpno, int orient, char type,
        vsc = ((cblksty & J2K_CCP_CBLKSTY_VSC)
               && (j == k + 3 || j == h - 1)) ? 1 : 0;
        t1_dec_sigpass_step(&t1_flags[1 + j][1 + i],
-                           &t1_data[j][i], orient, oneplushalf,
+                           &t1_data[j][i], 
+                           orient, 
+                           oneplushalf,
                            type, vsc);
       }
     }
@@ -275,6 +278,7 @@ void t1_enc_refpass_step(int *fp, int *dp, int bpno, int one, int *nmsedec,
   }
 }
 
+
 void t1_dec_refpass_step(int *fp, int *dp, int poshalf, int neghalf,
                         char type, int vsc)
 {
@@ -326,7 +330,10 @@ void t1_dec_refpass(int w, int h, int bpno, char type, int cblksty)
        vsc = ((cblksty & J2K_CCP_CBLKSTY_VSC)
               && (j == k + 3 || j == h - 1)) ? 1 : 0;
        t1_dec_refpass_step(&t1_flags[1 + j][1 + i],
-                           &t1_data[j][i], poshalf, neghalf, type, vsc);
+                           &t1_data[j][i], 
+                           poshalf, 
+                           neghalf, 
+                           type, vsc);
       }
     }
   }
@@ -358,6 +365,7 @@ void t1_enc_clnpass_step(int *fp, int *dp, int orient, int bpno, int one,
   *fp &= ~T1_VISIT;
 }
 
+
 void t1_dec_clnpass_step(int *fp, int *dp, int orient, int oneplushalf,
                         int partial, int vsc)
 {
@@ -495,7 +503,9 @@ void t1_dec_clnpass(int w, int h, int bpno, int orient, int cblksty)
        vsc = ((cblksty & J2K_CCP_CBLKSTY_VSC)
               && (j == k + 3 || j == h - 1)) ? 1 : 0;
        t1_dec_clnpass_step(&t1_flags[1 + j][1 + i],
-                           &t1_data[j][i], orient, oneplushalf,
+                           &t1_data[j][i], 
+                           orient, 
+                           oneplushalf,
                            agg && (j == k + runlen), vsc);
       }
     }
@@ -692,6 +702,8 @@ void t1_decode_cblk(tcd_cblk_t * cblk, int orient, int roishift,
       mqc_init_dec(seg->data, seg->len);
     /* dda */
 
+    if (bpno==0) cblk->lastbp=1;  // Add Antonin : quantizbug1
+
     for (passno = 0; passno < seg->numpasses; passno++) {
       switch (passtype) {
       case 0:
@@ -865,13 +877,18 @@ void t1_decode_cblks(tcd_tile_t * tile, j2k_tcp_t * tcp)
                for (i = 0; i < cblk->x1 - cblk->x0; i++) {
                  if (t1_data[j][i] == 0) {
                    tilec->data[x + i +
-                               (y + j) * (tilec->x1 - tilec->x0)] = 0;
+                     (y + j) * (tilec->x1 - tilec->x0)] = 0;
                  } else {
+                   // Add antonin : quantizbug1
+                   t1_data[j][i]<<=1;
+                   //if (cblk->lastbp) 
+                   t1_data[j][i]+=t1_data[j][i]>0?1:-1;
+                   // ddA
                    tilec->data[x + i +
                                (y + j) * (tilec->x1 -
                                           tilec->
                                           x0)] =
-                     fix_mul(t1_data[j][i] << 13, band->stepsize);
+                     fix_mul(t1_data[j][i] << 12, band->stepsize); //Mod Antonin : quantizbug1 (before : << 13)
                  }
                }
              }
index fb778347797212c0649c069c1477e8a034e14036..09e31ecede3ba109983b8a1d33d88f948698ef2a 100644 (file)
@@ -774,6 +774,8 @@ void tcd_init(j2k_image_t * img, j2k_cp_t * cp)
              cblk->y0 = int_max(cblkystart, prc->y0);
              cblk->x1 = int_min(cblkxend, prc->x1);
              cblk->y1 = int_min(cblkyend, prc->y1);
+
+             cblk->lastbp = 0; // Add Antonin : quantizbug1
            }
          }
        }
@@ -1523,10 +1525,15 @@ int tcd_decode_tile(unsigned char *src, int len, int tileno)
       for (i = res->x0; i < res->x1; i++) {
 
        int v;
+       double tmp= (double) tilec->data[i - res->x0 + (j - res->y0) * tw];
        if (tcd_tcp->tccps[compno].qmfbid == 1) {
-         v = tilec->data[i - res->x0 + (j - res->y0) * tw];
+         v = (int) tmp;
        } else {
-         v = tilec->data[i - res->x0 + (j - res->y0) * tw] >> 13;
+         //v = (int) tmp >> 13;
+         //Mod antonin : multbug1
+         v = (int) ((fabs(tmp/8192.0)>=floor(fabs(tmp/8192.0))+0.5)?fabs(tmp/8192.0)+1.0:fabs(tmp/8192.0));
+         v = (tmp<0)?-v:v;
+         //doM
        }
        v += adjust;
 
index 236a3a6b05cb3df6366950897933076a38d1d2f0..6e64717da3405ffccd98d2c76369b1dea4a00bf7 100644 (file)
@@ -57,6 +57,7 @@ typedef struct {
 typedef struct {
   int x0, y0, x1, y1;          /* dimension of the code-blocks : left upper corner (x0, y0) right low corner (x1,y1) */
   int numbps;
+  int lastbp;                  /* Add antonin : quantizbug1 */
   int numlenbits;
   int len;                     /* length */
   int numpasses;               /* number of pass already done for the code-blocks */