From 7ee36c3a4c37d9c8cee30ac19fd5532a3863285b Mon Sep 17 00:00:00 2001 From: Antonin Descampe Date: Thu, 27 Oct 2005 07:49:29 +0000 Subject: [PATCH] 1) quantization stepsizes stored as float instead of shifted integers -> fixes a pb of precision when using very small stepsizes. 2) bug fixed when decoding until bitplane 0 -> r-value (1/2) added to the coefficient. --- codec/image_to_j2k.c | 15 ++++++++------- libopenjpeg/t1.c | 44 +++++++++++++++---------------------------- libopenjpeg/tcd.c | 45 +++++++++++++------------------------------- libopenjpeg/tcd.h | 3 +-- 4 files changed, 37 insertions(+), 70 deletions(-) diff --git a/codec/image_to_j2k.c b/codec/image_to_j2k.c index 9be55998..b73941fa 100644 --- a/codec/image_to_j2k.c +++ b/codec/image_to_j2k.c @@ -250,12 +250,13 @@ int floorlog2(int a) return l; } -void encode_stepsize(int stepsize, int numbps, int *expn, int *mant) +void encode_stepsize(float stepsize, int numbps, int *expn, int *mant) { - int p, n; - p = floorlog2(stepsize) - 13; - n = 11 - floorlog2(stepsize); - *mant = (n < 0 ? stepsize >> -n : stepsize << n) & 0x7ff; + int p, n, stepsizeTMP; + stepsizeTMP=(int) floor(stepsize * 8192.0); + p = floorlog2(stepsizeTMP) - 13; + n = 11 - floorlog2(stepsizeTMP); + *mant = (n < 0 ? stepsizeTMP >> -n : stepsizeTMP << n) & 0x7ff; *expn = numbps - p; } @@ -264,7 +265,7 @@ void calc_explicit_stepsizes(j2k_tccp_t * tccp, int prec) int numbands, bandno; numbands = 3 * tccp->numresolutions - 2; for (bandno = 0; bandno < numbands; bandno++) { - double stepsize; + float stepsize; int resno, level, orient, gain; resno = bandno == 0 ? 0 : (bandno - 1) / 3 + 1; @@ -280,7 +281,7 @@ void calc_explicit_stepsizes(j2k_tccp_t * tccp, int prec) double norm = dwt_norms_97[orient][level]; stepsize = (1 << (gain + 1)) / norm; } - encode_stepsize((int) floor(stepsize * 8192.0), prec + gain, + encode_stepsize(stepsize, prec + gain, &tccp->stepsizes[bandno].expn, &tccp->stepsizes[bandno].mant); } diff --git a/libopenjpeg/t1.c b/libopenjpeg/t1.c index d4a4e440..17f2b4f3 100644 --- a/libopenjpeg/t1.c +++ b/libopenjpeg/t1.c @@ -535,7 +535,7 @@ void t1_dec_clnpass(int w, int h, int bpno, int orient, int cblksty) } } /* VSC and BYPASS by Antonin */ -double t1_getwmsedec(int nmsedec, int compno, int level, int orient, int bpno, int qmfbid, double stepsize, int numcomps) //mod fixed_quality +double t1_getwmsedec(int nmsedec, int compno, int level, int orient, int bpno, int qmfbid, float stepsize, int numcomps) //mod fixed_quality { double w1, w2, wmsedec; if (qmfbid == 1) { @@ -550,7 +550,7 @@ double t1_getwmsedec(int nmsedec, int compno, int level, int orient, int bpno, i return wmsedec; } -void t1_encode_cblk(tcd_cblk_t * cblk, int orient, int compno, int level, int qmfbid, double stepsize, int cblksty, int numcomps, tcd_tile_t * tile) //mod fixed_quality +void t1_encode_cblk(tcd_cblk_t * cblk, int orient, int compno, int level, int qmfbid, float stepsize, int cblksty, int numcomps, tcd_tile_t * tile) //mod fixed_quality { int i, j; int w, h; @@ -723,19 +723,17 @@ void t1_decode_cblk(tcd_cblk_t * cblk, int orient, int roishift, else 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: - t1_dec_sigpass(w, h, bpno, orient, type, cblksty); + t1_dec_sigpass(w, h, bpno+1, orient, type, cblksty); break; case 1: - t1_dec_refpass(w, h, bpno, type, cblksty); + t1_dec_refpass(w, h, bpno+1, type, cblksty); break; case 2: - t1_dec_clnpass(w, h, bpno, orient, cblksty); + t1_dec_clnpass(w, h, bpno+1, orient, cblksty); break; } @@ -819,7 +817,7 @@ void t1_encode_cblks(tcd_tile_t * tile, j2k_tcp_t * tcp) tilec-> x0)], 8192 * 8192 / - band->stepsize) >> (13 - T1_NMSEDEC_FRACBITS); + ((int) floor(band->stepsize * 8192))) >> (13 - T1_NMSEDEC_FRACBITS); } } } @@ -896,34 +894,22 @@ void t1_decode_cblks(tcd_tile_t * tile, j2k_tcp_t * tcp) if (tcp->tccps[compno].qmfbid == 1) { for (j = 0; j < cblk->y1 - cblk->y0; j++) { for (i = 0; i < cblk->x1 - cblk->x0; i++) { - tilec->data[x + i + - (y + j) * (tilec->x1 - - tilec->x0)] = t1_data[j][i]; + int tmp=t1_data[j][i]; + if (tmp>>1==0) tilec->data[x + i + (y + j) * (tilec->x1 - tilec->x0)] = 0; + else tilec->data[x + i + (y + j) * (tilec->x1 - tilec->x0)] = tmp<0?((tmp>>1) | 0x80000000)+1:(tmp>>1); } } } else { /* if (tcp->tccps[compno].qmfbid == 0) */ for (j = 0; j < cblk->y1 - cblk->y0; j++) { 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; + float tmp=t1_data[j][i] * band->stepsize * 4096.0; + int tmp2; + if (t1_data[j][i]>>1 == 0) { + tilec->data[x + i + (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] << 12, band->stepsize); //Mod Antonin : quantizbug1 (before : << 13) + tmp2=((int) (floor(fabs(tmp)))) + ((int) floor(fabs(tmp*2))%2); + tilec->data[x + i + (y + j) * (tilec->x1 - tilec->x0)] = ((tmp<0)?-tmp2:tmp2); } } } diff --git a/libopenjpeg/tcd.c b/libopenjpeg/tcd.c index 3e433209..95711ab7 100644 --- a/libopenjpeg/tcd.c +++ b/libopenjpeg/tcd.c @@ -87,7 +87,7 @@ void tcd_dump(tcd_image_t * img, int curtileno) tcd_band_t *band = &res->bands[bandno]; fprintf(stdout, " band {\n"); fprintf(stdout, - " x0=%d, y0=%d, x1=%d, y1=%d, stepsize=%d, numbps=%d\n", + " x0=%d, y0=%d, x1=%d, y1=%d, stepsize=%f, numbps=%d\n", band->x0, band->y0, band->x1, band->y1, band->stepsize, band->numbps); for (precno = 0; precno < res->pw * res->ph; precno++) { @@ -270,9 +270,7 @@ void tcd_malloc_encode(j2k_image_t * img, j2k_cp_t * cp, int curtileno) tccp->qmfbid == 0 ? dwt_getgain_real(band->bandno) : dwt_getgain(band->bandno); numbps = img->comps[compno].prec + gain; - band->stepsize = - (int) floor((1.0 + ss->mant / 2048.0) * - pow(2.0, numbps - ss->expn) * 8192.0); + band->stepsize = (1.0 + ss->mant / 2048.0) * pow(2.0, numbps - ss->expn); band->numbps = ss->expn + tccp->numgbits - 1; /* WHY -1 ? */ band->precincts = @@ -514,9 +512,7 @@ void tcd_init_encode(j2k_image_t * img, j2k_cp_t * cp, int curtileno) tccp->qmfbid == 0 ? dwt_getgain_real(band->bandno) : dwt_getgain(band->bandno); numbps = img->comps[compno].prec + gain; - band->stepsize = - (int) floor((1.0 + ss->mant / 2048.0) * - pow(2.0, numbps - ss->expn) * 8192.0); + band->stepsize = (1.0 + ss->mant / 2048.0) * pow(2.0, numbps - ss->expn); band->numbps = ss->expn + tccp->numgbits - 1; /* WHY -1 ? */ for (precno = 0; precno < res->pw * res->ph; precno++) { @@ -718,13 +714,11 @@ void tcd_init(j2k_image_t * img, j2k_cp_t * cp) ss = &tccp->stepsizes[resno == 0 ? 0 : 3 * (resno - 1) + bandno + 1]; - gain = + gain = tccp->qmfbid == 0 ? dwt_getgain_real(band->bandno) : dwt_getgain(band->bandno); numbps = img->comps[compno].prec + gain; - band->stepsize = - (int) floor((1.0 + ss->mant / 2048.0) * - pow(2.0, numbps - ss->expn) * 8192.0); + band->stepsize = (1.0 + ss->mant / 2048.0) * pow(2.0, numbps - ss->expn); band->numbps = ss->expn + tccp->numgbits - 1; /* WHY -1 ? */ band->precincts = @@ -777,10 +771,6 @@ 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 } } } @@ -1571,25 +1561,16 @@ 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]; + float tmp = (tilec->data[i - res->x0 + (j - res->y0) * tw])/8192.0; + int tmp2; + if (tcd_tcp->tccps[compno].qmfbid == 1) { - v = (int) tmp; + v = tilec->data[i - res->x0 + (j - res->y0) * tw]; } else { - - //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 - } + tmp2=((int) (floor(fabs(tmp)))) + ((int) floor(fabs(tmp*2))%2); + v = ((tmp<0)?-tmp2:tmp2); + } + v += adjust; tcd_img->comps[compno].data[(i - offset_x) + diff --git a/libopenjpeg/tcd.h b/libopenjpeg/tcd.h index 3201243d..714d5ea4 100644 --- a/libopenjpeg/tcd.h +++ b/libopenjpeg/tcd.h @@ -57,7 +57,6 @@ 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 */ @@ -84,7 +83,7 @@ typedef struct { int bandno; tcd_precinct_t *precincts; /* precinct information */ int numbps; - int stepsize; + float stepsize; } tcd_band_t; typedef struct {