double msen(jas_matrix_t *x, jas_matrix_t *y, int n) { double s; double d; int i; int j; s = 0.0; for (i = 0; i < jas_matrix_numrows(x); i++) { for (j = 0; j < jas_matrix_numcols(x); j++) { d = jas_matrix_get(y, i, j) - jas_matrix_get(x, i, j); if (n == 1) { s += fabs(d); } else if (n == 2) { s += d * d; } else { abort(); } } } return s / ((double) jas_matrix_numrows(x) * jas_matrix_numcols(x)); }
double pae(jas_matrix_t *x, jas_matrix_t *y) { double s; double d; int i; int j; s = 0.0; for (i = 0; i < jas_matrix_numrows(x); i++) { for (j = 0; j < jas_matrix_numcols(x); j++) { d = abs(jas_matrix_get(y, i, j) - jas_matrix_get(x, i, j)); if (d > s) { s = d; } } } return s; }
static int jpc_encclnpass(jpc_mqenc_t *mqenc, int bitpos, int orient, int vcausalflag, int segsymflag, jas_matrix_t *flags, jas_matrix_t *data, int term, long *nmsedec) { int i; int j; int k; int vscanlen; int v; int runlen; jpc_fix_t *fp; int width; int height; jpc_fix_t *dp; int one; int frowstep; int drowstep; int fstripestep; int dstripestep; jpc_fix_t *fstripestart; jpc_fix_t *dstripestart; jpc_fix_t *fvscanstart; jpc_fix_t *dvscanstart; *nmsedec = 0; width = jas_matrix_numcols(data); height = jas_matrix_numrows(data); frowstep = jas_matrix_rowstep(flags); drowstep = jas_matrix_rowstep(data); fstripestep = frowstep << 2; dstripestep = drowstep << 2; one = 1 << (bitpos + JPC_NUMEXTRABITS); fstripestart = jas_matrix_getref(flags, 1, 1); dstripestart = jas_matrix_getref(data, 0, 0); for (i = height; i > 0; i -= 4, fstripestart += fstripestep, dstripestart += dstripestep) { fvscanstart = fstripestart; dvscanstart = dstripestart; vscanlen = JAS_MIN(i, 4); for (j = width; j > 0; --j, ++fvscanstart, ++dvscanstart) { fp = fvscanstart; if (vscanlen >= 4 && !((*fp) & (JPC_SIG | JPC_VISIT | JPC_OTHSIGMSK)) && (fp += frowstep, !((*fp) & (JPC_SIG | JPC_VISIT | JPC_OTHSIGMSK))) && (fp += frowstep, !((*fp) & (JPC_SIG | JPC_VISIT | JPC_OTHSIGMSK))) && (fp += frowstep, !((*fp) & (JPC_SIG | JPC_VISIT | JPC_OTHSIGMSK)))) { dp = dvscanstart; for (k = 0; k < vscanlen; ++k) { v = (abs(*dp) & one) ? 1 : 0; if (v) { break; } dp += drowstep; } runlen = k; if (runlen >= 4) { jpc_mqenc_setcurctx(mqenc, JPC_AGGCTXNO); jpc_mqenc_putbit(mqenc, 0); continue; } jpc_mqenc_setcurctx(mqenc, JPC_AGGCTXNO); jpc_mqenc_putbit(mqenc, 1); jpc_mqenc_setcurctx(mqenc, JPC_UCTXNO); jpc_mqenc_putbit(mqenc, runlen >> 1); jpc_mqenc_putbit(mqenc, runlen & 1); fp = fvscanstart + frowstep * runlen; dp = dvscanstart + drowstep * runlen; k = vscanlen - runlen; switch (runlen) { case 0: goto clnpass_partial0; break; case 1: goto clnpass_partial1; break; case 2: goto clnpass_partial2; break; case 3: goto clnpass_partial3; break; } } else { runlen = 0; fp = fvscanstart; dp = dvscanstart; k = vscanlen; goto clnpass_full0; } clnpass_step(fp, frowstep, dp, bitpos, one, orient, nmsedec, mqenc, clnpass_full0:, clnpass_partial0:, vcausalflag); if (--k <= 0) { continue; } fp += frowstep; dp += drowstep; clnpass_step(fp, frowstep, dp, bitpos, one, orient, nmsedec, mqenc, ;, clnpass_partial1:, 0);
static int jpc_encrawrefpass(jpc_bitstream_t *out, int bitpos, int vcausalflag, jas_matrix_t *flags, jas_matrix_t *data, int term, long *nmsedec) { int i; int j; int k; int one; int vscanlen; int width; int height; int frowstep; int drowstep; int fstripestep; int dstripestep; jpc_fix_t *fstripestart; jpc_fix_t *dstripestart; jpc_fix_t *fvscanstart; jpc_fix_t *dvscanstart; jpc_fix_t *dp; jpc_fix_t *fp; *nmsedec = 0; width = jas_matrix_numcols(data); height = jas_matrix_numrows(data); frowstep = jas_matrix_rowstep(flags); drowstep = jas_matrix_rowstep(data); fstripestep = frowstep << 2; dstripestep = drowstep << 2; one = 1 << (bitpos + JPC_NUMEXTRABITS); fstripestart = jas_matrix_getref(flags, 1, 1); dstripestart = jas_matrix_getref(data, 0, 0); for (i = height; i > 0; i -= 4, fstripestart += fstripestep, dstripestart += dstripestep) { fvscanstart = fstripestart; dvscanstart = dstripestart; vscanlen = JAS_MIN(i, 4); for (j = width; j > 0; --j, ++fvscanstart, ++dvscanstart) { fp = fvscanstart; dp = dvscanstart; k = vscanlen; rawrefpass_step(fp, dp, bitpos, one, nmsedec, out, vcausalflag); if (--k <= 0) { continue; } fp += frowstep; dp += drowstep; rawrefpass_step(fp, dp, bitpos, one, nmsedec, out, vcausalflag); if (--k <= 0) { continue; } fp += frowstep; dp += drowstep; rawrefpass_step(fp, dp, bitpos, one, nmsedec, out, vcausalflag); if (--k <= 0) { continue; } fp += frowstep; dp += drowstep; rawrefpass_step(fp, dp, bitpos, one, nmsedec, out, vcausalflag); } } if (term) { jpc_bitstream_outalign(out, 0x2a); } return 0; }
/* Encode a single code block. */ int jpc_enc_enccblk(jpc_enc_t *enc, jas_stream_t *out, jpc_enc_tcmpt_t *tcmpt, jpc_enc_band_t *band, jpc_enc_cblk_t *cblk) { jpc_enc_pass_t *pass; jpc_enc_pass_t *endpasses; int bitpos; int n; int adjust; int ret; int passtype; int t; jpc_bitstream_t *bout; jpc_enc_pass_t *termpass; jpc_enc_rlvl_t *rlvl; int vcausal; int segsym; int termmode; int c; bout = 0; rlvl = band->rlvl; cblk->stream = jas_stream_memopen(0, 0); assert(cblk->stream); cblk->mqenc = jpc_mqenc_create(JPC_NUMCTXS, cblk->stream); assert(cblk->mqenc); jpc_mqenc_setctxs(cblk->mqenc, JPC_NUMCTXS, jpc_mqctxs); cblk->numpasses = (cblk->numbps > 0) ? (3 * cblk->numbps - 2) : 0; if (cblk->numpasses > 0) { cblk->passes = jas_malloc(cblk->numpasses * sizeof(jpc_enc_pass_t)); assert(cblk->passes); } else { cblk->passes = 0; } endpasses = &cblk->passes[cblk->numpasses]; for (pass = cblk->passes; pass != endpasses; ++pass) { pass->start = 0; pass->end = 0; pass->term = JPC_ISTERMINATED(pass - cblk->passes, 0, cblk->numpasses, (tcmpt->cblksty & JPC_COX_TERMALL) != 0, (tcmpt->cblksty & JPC_COX_LAZY) != 0); pass->type = JPC_SEGTYPE(pass - cblk->passes, 0, (tcmpt->cblksty & JPC_COX_LAZY) != 0); pass->lyrno = -1; if (pass == endpasses - 1) { assert(pass->term == 1); pass->term = 1; } } cblk->flags = jas_matrix_create(jas_matrix_numrows(cblk->data) + 2, jas_matrix_numcols(cblk->data) + 2); assert(cblk->flags); bitpos = cblk->numbps - 1; pass = cblk->passes; n = cblk->numpasses; while (--n >= 0) { if (pass->type == JPC_SEG_MQ) { /* NOP */ } else { assert(pass->type == JPC_SEG_RAW); if (!bout) { bout = jpc_bitstream_sopen(cblk->stream, "w"); assert(bout); } } #if 1 passtype = (pass - cblk->passes + 2) % 3; #else passtype = JPC_PASSTYPE(pass - cblk->passes + 2); #endif pass->start = jas_stream_tell(cblk->stream); #if 0 assert(jas_stream_tell(cblk->stream) == jas_stream_getrwcount(cblk->stream)); #endif assert(bitpos >= 0); vcausal = (tcmpt->cblksty & JPC_COX_VSC) != 0; segsym = (tcmpt->cblksty & JPC_COX_SEGSYM) != 0; if (pass->term) { termmode = ((tcmpt->cblksty & JPC_COX_PTERM) ? JPC_MQENC_PTERM : JPC_MQENC_DEFTERM) + 1; } else { termmode = 0; } switch (passtype) { case JPC_SIGPASS: ret = (pass->type == JPC_SEG_MQ) ? jpc_encsigpass(cblk->mqenc, bitpos, band->orient, vcausal, cblk->flags, cblk->data, termmode, &pass->nmsedec) : jpc_encrawsigpass(bout, bitpos, vcausal, cblk->flags, cblk->data, termmode, &pass->nmsedec); break; case JPC_REFPASS: ret = (pass->type == JPC_SEG_MQ) ? jpc_encrefpass(cblk->mqenc, bitpos, vcausal, cblk->flags, cblk->data, termmode, &pass->nmsedec) : jpc_encrawrefpass(bout, bitpos, vcausal, cblk->flags, cblk->data, termmode, &pass->nmsedec); break; case JPC_CLNPASS: assert(pass->type == JPC_SEG_MQ); ret = jpc_encclnpass(cblk->mqenc, bitpos, band->orient, vcausal, segsym, cblk->flags, cblk->data, termmode, &pass->nmsedec); break; default: assert(0); break; } if (pass->type == JPC_SEG_MQ) { if (pass->term) { jpc_mqenc_init(cblk->mqenc); } jpc_mqenc_getstate(cblk->mqenc, &pass->mqencstate); pass->end = jas_stream_tell(cblk->stream); if (tcmpt->cblksty & JPC_COX_RESET) { jpc_mqenc_setctxs(cblk->mqenc, JPC_NUMCTXS, jpc_mqctxs); } } else { if (pass->term) { if (jpc_bitstream_pending(bout)) { jpc_bitstream_outalign(bout, 0x2a); } jpc_bitstream_close(bout); bout = 0; pass->end = jas_stream_tell(cblk->stream); } else { pass->end = jas_stream_tell(cblk->stream) + jpc_bitstream_pending(bout); /* NOTE - This will not work. need to adjust by # of pending output bytes */ } } #if 0 /* XXX - This assertion fails sometimes when various coding modes are used. This seems to be harmless, but why does it happen at all? */ assert(jas_stream_tell(cblk->stream) == jas_stream_getrwcount(cblk->stream)); #endif pass->wmsedec = jpc_fixtodbl(band->rlvl->tcmpt->synweight) * jpc_fixtodbl(band->rlvl->tcmpt->synweight) * jpc_fixtodbl(band->synweight) * jpc_fixtodbl(band->synweight) * jpc_fixtodbl(band->absstepsize) * jpc_fixtodbl(band->absstepsize) * ((double) (1 << bitpos)) * ((double)(1 << bitpos)) * jpc_fixtodbl(pass->nmsedec); pass->cumwmsedec = pass->wmsedec; if (pass != cblk->passes) { pass->cumwmsedec += pass[-1].cumwmsedec; } if (passtype == JPC_CLNPASS) { --bitpos; } ++pass; } #if 0 dump_passes(cblk->passes, cblk->numpasses, cblk); #endif n = 0; endpasses = &cblk->passes[cblk->numpasses]; for (pass = cblk->passes; pass != endpasses; ++pass) { if (pass->start < n) { pass->start = n; } if (pass->end < n) { pass->end = n; } if (!pass->term) { termpass = pass; while (termpass - pass < cblk->numpasses && !termpass->term) { ++termpass; } if (pass->type == JPC_SEG_MQ) { t = (pass->mqencstate.lastbyte == 0xff) ? 1 : 0; if (pass->mqencstate.ctreg >= 5) { adjust = 4 + t; } else { adjust = 5 + t; } pass->end += adjust; } if (pass->end > termpass->end) { pass->end = termpass->end; } if ((c = getthebyte(cblk->stream, pass->end - 1)) == EOF) { abort(); } if (c == 0xff) { ++pass->end; } n = JAS_MAX(n, pass->end); } else { n = JAS_MAX(n, pass->end); } } #if 0 dump_passes(cblk->passes, cblk->numpasses, cblk); #endif if (bout) { jpc_bitstream_close(bout); } return 0; }
/* Encode all of the code blocks associated with the current tile. */ int jpc_enc_enccblks(jpc_enc_t *enc) { jpc_enc_tcmpt_t *tcmpt; jpc_enc_tcmpt_t *endcomps; jpc_enc_rlvl_t *lvl; jpc_enc_rlvl_t *endlvls; jpc_enc_band_t *band; jpc_enc_band_t *endbands; jpc_enc_cblk_t *cblk; jpc_enc_cblk_t *endcblks; int i; int j; int mx; int bmx; int v; jpc_enc_tile_t *tile; uint_fast32_t prcno; jpc_enc_prc_t *prc; tile = enc->curtile; endcomps = &tile->tcmpts[tile->numtcmpts]; for (tcmpt = tile->tcmpts; tcmpt != endcomps; ++tcmpt) { endlvls = &tcmpt->rlvls[tcmpt->numrlvls]; for (lvl = tcmpt->rlvls; lvl != endlvls; ++lvl) { if (!lvl->bands) { continue; } endbands = &lvl->bands[lvl->numbands]; for (band = lvl->bands; band != endbands; ++band) { if (!band->data) { continue; } for (prcno = 0, prc = band->prcs; prcno < lvl->numprcs; ++prcno, ++prc) { if (!prc->cblks) { continue; } bmx = 0; endcblks = &prc->cblks[prc->numcblks]; for (cblk = prc->cblks; cblk != endcblks; ++cblk) { mx = 0; for (i = 0; i < jas_matrix_numrows(cblk->data); ++i) { for (j = 0; j < jas_matrix_numcols(cblk->data); ++j) { v = abs(jas_matrix_get(cblk->data, i, j)); if (v > mx) { mx = v; } } } if (mx > bmx) { bmx = mx; } cblk->numbps = JAS_MAX(jpc_firstone(mx) + 1 - JPC_NUMEXTRABITS, 0); } for (cblk = prc->cblks; cblk != endcblks; ++cblk) { cblk->numimsbs = band->numbps - cblk->numbps; assert(cblk->numimsbs >= 0); } for (cblk = prc->cblks; cblk != endcblks; ++cblk) { if (jpc_enc_enccblk(enc, cblk->stream, tcmpt, band, cblk)) { return -1; } } } } } } return 0; }
/* Encode all of the code blocks associated with the current tile. */ int jpc_enc_enccblks(jpc_enc_t *enc) { jpc_enc_tcmpt_t *tcmpt; jpc_enc_tcmpt_t *endcomps; jpc_enc_rlvl_t *lvl; jpc_enc_rlvl_t *endlvls; jpc_enc_band_t *band; jpc_enc_band_t *endbands; jpc_enc_cblk_t *endcblks; int k; int bmx; jpc_enc_tile_t *tile; uint_fast32_t prcno; jpc_enc_prc_t *prc; tile = enc->curtile; endcomps = &tile->tcmpts[tile->numtcmpts]; for (tcmpt = tile->tcmpts; tcmpt != endcomps; ++tcmpt) { endlvls = &tcmpt->rlvls[tcmpt->numrlvls]; for (lvl = tcmpt->rlvls; lvl != endlvls; ++lvl) { if (!lvl->bands) { continue; } endbands = &lvl->bands[lvl->numbands]; for (band = lvl->bands; band != endbands; ++band) { if (!band->data) { continue; } for (prcno = 0, prc = band->prcs; prcno < lvl->numprcs; ++prcno, ++prc) { int error = 0; if (!prc->cblks) { continue; } bmx = 0; endcblks = &prc->cblks[prc->numcblks]; #pragma omp parallel for reduction(|:error) for (k=0; k<prc->numcblks; ++k) { //OMP: for (cblk = prc->cblks; cblk != endcblks; ++cblk) { int v, mx, i, j; jpc_enc_cblk_t *cblk = &(prc->cblks[k]); //printf("Thread %d working on k %d (numcblks = %d)\n", omp_get_thread_num(), k, prc->numcblks); mx = 0; for (i = 0; i < jas_matrix_numrows(cblk->data); ++i) { for (j = 0; j < jas_matrix_numcols(cblk->data); ++j) { v = abs(jas_matrix_get(cblk->data, i, j)); if (v > mx) { mx = v; } } } /* if (mx > bmx) { bmx = mx; } */ cblk->numbps = JAS_MAX(jpc_firstone(mx) + 1 - JPC_NUMEXTRABITS, 0); //OMP: } //OMP: for (cblk = prc->cblks; cblk != endcblks; ++cblk) { cblk->numimsbs = band->numbps - cblk->numbps; assert(cblk->numimsbs >= 0); //OMP: } //OMP: for (cblk = prc->cblks; cblk != endcblks; ++cblk) { if (jpc_enc_enccblk(enc, cblk->stream, tcmpt, band, cblk)) { error |= 1; //OMP: return -1; } } //printf("Exiting\n"); if(error) return -1; } } } } return 0; }
static int dec_clnpass(jpc_dec_t *dec, register jpc_mqdec_t *mqdec, int bitpos, int orient, int vcausalflag, int segsymflag, jas_matrix_t *flags, jas_matrix_t *data) { int i; int j; int k; int vscanlen; int v; int half; int runlen; int f; int width; int height; int one; int oneplushalf; jpc_fix_t *fp; int frowstep; int fstripestep; jpc_fix_t *fstripestart; jpc_fix_t *fvscanstart; jpc_fix_t *dp; int drowstep; int dstripestep; jpc_fix_t *dstripestart; jpc_fix_t *dvscanstart; /* Avoid compiler warning about unused parameters. */ dec = 0; one = 1 << bitpos; half = one >> 1; oneplushalf = one | half; width = jas_matrix_numcols(data); height = jas_matrix_numrows(data); frowstep = jas_matrix_rowstep(flags); drowstep = jas_matrix_rowstep(data); fstripestep = frowstep << 2; dstripestep = drowstep << 2; fstripestart = jas_matrix_getref(flags, 1, 1); dstripestart = jas_matrix_getref(data, 0, 0); for (i = 0; i < height; i += 4, fstripestart += fstripestep, dstripestart += dstripestep) { fvscanstart = fstripestart; dvscanstart = dstripestart; vscanlen = JAS_MIN(4, height - i); for (j = width; j > 0; --j, ++fvscanstart, ++dvscanstart) { fp = fvscanstart; if (vscanlen >= 4 && (!((*fp) & (JPC_SIG | JPC_VISIT | JPC_OTHSIGMSK))) && (fp += frowstep, !((*fp) & (JPC_SIG | JPC_VISIT | JPC_OTHSIGMSK))) && (fp += frowstep, !((*fp) & (JPC_SIG | JPC_VISIT | JPC_OTHSIGMSK))) && (fp += frowstep, !((*fp) & (JPC_SIG | JPC_VISIT | JPC_OTHSIGMSK)))) { jpc_mqdec_setcurctx(mqdec, JPC_AGGCTXNO); JPC_T1D_GETBIT(mqdec, v, "CLN", "AGG"); if (!v) { continue; } jpc_mqdec_setcurctx(mqdec, JPC_UCTXNO); JPC_T1D_GETBITNOSKEW(mqdec, v, "CLN", "RL"); runlen = v; JPC_T1D_GETBITNOSKEW(mqdec, v, "CLN", "RL"); runlen = (runlen << 1) | v; f = *(fp = fvscanstart + frowstep * runlen); dp = dvscanstart + drowstep * runlen; k = vscanlen - runlen; switch (runlen) { case 0: goto clnpass_partial0; break; case 1: goto clnpass_partial1; break; case 2: goto clnpass_partial2; break; case 3: goto clnpass_partial3; break; } } else { f = *(fp = fvscanstart); dp = dvscanstart; k = vscanlen; goto clnpass_full0; } /* Process first sample in vertical scan. */ jpc_clnpass_step(f, fp, frowstep, dp, oneplushalf, orient, mqdec, clnpass_full0:, clnpass_partial0:, vcausalflag); if (--k <= 0) { continue; } fp += frowstep; dp += drowstep; /* Process second sample in vertical scan. */ f = *fp; jpc_clnpass_step(f, fp, frowstep, dp, oneplushalf, orient, mqdec, ;, clnpass_partial1:, 0); if (--k <= 0) { continue; } fp += frowstep; dp += drowstep; /* Process third sample in vertical scan. */ f = *fp; jpc_clnpass_step(f, fp, frowstep, dp, oneplushalf, orient, mqdec, ;, clnpass_partial2:, 0); if (--k <= 0) { continue; } fp += frowstep; dp += drowstep; /* Process fourth sample in vertical scan. */ f = *fp; jpc_clnpass_step(f, fp, frowstep, dp, oneplushalf, orient, mqdec, ;, clnpass_partial3:, 0);
static int dec_rawrefpass(jpc_dec_t *dec, jpc_bitstream_t *in, int bitpos, int vcausalflag, jas_matrix_t *flags, jas_matrix_t *data) { int i; int j; int k; int vscanlen; int width; int height; int one; int poshalf; int neghalf; jpc_fix_t *fp; int frowstep; int fstripestep; jpc_fix_t *fstripestart; jpc_fix_t *fvscanstart; jpc_fix_t *dp; int drowstep; int dstripestep; jpc_fix_t *dstripestart; jpc_fix_t *dvscanstart; /* Avoid compiler warning about unused parameters. */ dec = 0; vcausalflag = 0; width = jas_matrix_numcols(data); height = jas_matrix_numrows(data); frowstep = jas_matrix_rowstep(flags); drowstep = jas_matrix_rowstep(data); fstripestep = frowstep << 2; dstripestep = drowstep << 2; one = 1 << bitpos; poshalf = one >> 1; neghalf = (bitpos > 0) ? (-poshalf) : (-1); fstripestart = jas_matrix_getref(flags, 1, 1); dstripestart = jas_matrix_getref(data, 0, 0); for (i = height; i > 0; i -= 4, fstripestart += fstripestep, dstripestart += dstripestep) { fvscanstart = fstripestart; dvscanstart = dstripestart; vscanlen = JAS_MIN(i, 4); for (j = width; j > 0; --j, ++fvscanstart, ++dvscanstart) { fp = fvscanstart; dp = dvscanstart; k = vscanlen; /* Process first sample in vertical scan. */ jpc_rawrefpass_step(fp, dp, poshalf, neghalf, in, vcausalflag); if (--k <= 0) { continue; } fp += frowstep; dp += drowstep; /* Process second sample in vertical scan. */ jpc_rawrefpass_step(fp, dp, poshalf, neghalf, in, 0); if (--k <= 0) { continue; } fp += frowstep; dp += drowstep; /* Process third sample in vertical scan. */ jpc_rawrefpass_step(fp, dp, poshalf, neghalf, in, 0); if (--k <= 0) { continue; } fp += frowstep; dp += drowstep; /* Process fourth sample in vertical scan. */ jpc_rawrefpass_step(fp, dp, poshalf, neghalf, in, 0); } } return 0; }
static int jpc_dec_decodecblk(jpc_dec_t *dec, jpc_dec_tile_t *tile, jpc_dec_tcomp_t *tcomp, jpc_dec_band_t *band, jpc_dec_cblk_t *cblk, int dopartial, int maxlyrs) { jpc_dec_seg_t *seg; int i; int bpno; int passtype; int ret; int compno; int filldata; int fillmask; jpc_dec_ccp_t *ccp; compno = tcomp - tile->tcomps; if (!cblk->flags) { /* Note: matrix is assumed to be zeroed */ if (!(cblk->flags = jas_matrix_create(jas_matrix_numrows(cblk->data) + 2, jas_matrix_numcols(cblk->data) + 2))) { return -1; } } seg = cblk->segs.head; while (seg && (seg != cblk->curseg || dopartial) && (maxlyrs < 0 || seg->lyrno < maxlyrs)) { assert(seg->numpasses >= seg->maxpasses || dopartial); assert(seg->stream); jas_stream_rewind(seg->stream); jas_stream_setrwcount(seg->stream, 0); if (seg->type == JPC_SEG_MQ) { if (!cblk->mqdec) { if (!(cblk->mqdec = jpc_mqdec_create(JPC_NUMCTXS, 0))) { return -1; } jpc_mqdec_setctxs(cblk->mqdec, JPC_NUMCTXS, jpc_mqctxs); } jpc_mqdec_setinput(cblk->mqdec, seg->stream); jpc_mqdec_init(cblk->mqdec); } else { assert(seg->type == JPC_SEG_RAW); if (!cblk->nulldec) { if (!(cblk->nulldec = jpc_bitstream_sopen(seg->stream, "r"))) { assert(0); } } } for (i = 0; i < seg->numpasses; ++i) { if (cblk->numimsbs > band->numbps) { ccp = &tile->cp->ccps[compno]; if (ccp->roishift <= 0) { jas_eprintf("warning: corrupt code stream\n"); } else { if (cblk->numimsbs < ccp->roishift - band->numbps) { jas_eprintf("warning: corrupt code stream\n"); } } } bpno = band->roishift + band->numbps - 1 - (cblk->numimsbs + (seg->passno + i - cblk->firstpassno + 2) / 3); if (bpno < 0) { goto premature_exit; } #if 1 passtype = (seg->passno + i + 2) % 3; #else passtype = JPC_PASSTYPE(seg->passno + i + 2); #endif assert(bpno >= 0 && bpno < 31); switch (passtype) { case JPC_SIGPASS: ret = (seg->type == JPC_SEG_MQ) ? dec_sigpass(dec, cblk->mqdec, bpno, band->orient, (tile->cp->ccps[compno].cblkctx & JPC_COX_VSC) != 0, cblk->flags, cblk->data) : dec_rawsigpass(dec, cblk->nulldec, bpno, (tile->cp->ccps[compno].cblkctx & JPC_COX_VSC) != 0, cblk->flags, cblk->data); break; case JPC_REFPASS: ret = (seg->type == JPC_SEG_MQ) ? dec_refpass(dec, cblk->mqdec, bpno, (tile->cp->ccps[compno].cblkctx & JPC_COX_VSC) != 0, cblk->flags, cblk->data) : dec_rawrefpass(dec, cblk->nulldec, bpno, (tile->cp->ccps[compno].cblkctx & JPC_COX_VSC) != 0, cblk->flags, cblk->data); break; case JPC_CLNPASS: assert(seg->type == JPC_SEG_MQ); ret = dec_clnpass(dec, cblk->mqdec, bpno, band->orient, (tile->cp->ccps[compno].cblkctx & JPC_COX_VSC) != 0, (tile->cp->ccps[compno].cblkctx & JPC_COX_SEGSYM) != 0, cblk->flags, cblk->data); break; default: ret = -1; break; } /* Do we need to reset after each coding pass? */ if (tile->cp->ccps[compno].cblkctx & JPC_COX_RESET) { jpc_mqdec_setctxs(cblk->mqdec, JPC_NUMCTXS, jpc_mqctxs); } if (ret) { jas_eprintf("coding pass failed passtype=%d segtype=%d\n", passtype, seg->type); return -1; } } if (seg->type == JPC_SEG_MQ) { /* Note: dont destroy mq decoder because context info will be lost */ } else { assert(seg->type == JPC_SEG_RAW); if (tile->cp->ccps[compno].cblkctx & JPC_COX_PTERM) { fillmask = 0x7f; filldata = 0x2a; } else { fillmask = 0; filldata = 0; } if ((ret = jpc_bitstream_inalign(cblk->nulldec, fillmask, filldata)) < 0) { return -1; } else if (ret > 0) { jas_eprintf("warning: bad termination pattern detected\n"); } jpc_bitstream_close(cblk->nulldec); cblk->nulldec = 0; } cblk->curseg = seg->next; jpc_seglist_remove(&cblk->segs, seg); jpc_seg_destroy(seg); seg = cblk->curseg; } assert(dopartial ? (!cblk->curseg) : 1); premature_exit: return 0; }
jas_image_t *makediffimage(jas_matrix_t *origdata, jas_matrix_t *recondata) { jas_image_t *diffimage; jas_matrix_t *diffdata[3]; int width; int height; int i; int j; int k; jas_image_cmptparm_t compparms[3]; jas_seqent_t a; jas_seqent_t b; width = jas_matrix_numcols(origdata); height = jas_matrix_numrows(origdata); for (i = 0; i < 3; ++i) { compparms[i].tlx = 0; compparms[i].tly = 0; compparms[i].hstep = 1; compparms[i].vstep = 1; compparms[i].width = width; compparms[i].height = height; compparms[i].prec = 8; compparms[i].sgnd = jas_false; } if (!(diffimage = jas_image_create(3, compparms, JAS_CLRSPC_SRGB))) { abort(); } for (i = 0; i < 3; ++i) { if (!(diffdata[i] = jas_matrix_create(height, width))) { jas_eprintf("internal error\n"); return 0; } } for (j = 0; j < height; ++j) { for (k = 0; k < width; ++k) { a = jas_matrix_get(origdata, j, k); b = jas_matrix_get(recondata, j, k); if (a > b) { jas_matrix_set(diffdata[0], j, k, 255); jas_matrix_set(diffdata[1], j, k, 0); jas_matrix_set(diffdata[2], j, k, 0); } else if (a < b) { jas_matrix_set(diffdata[0], j, k, 0); jas_matrix_set(diffdata[1], j, k, 255); jas_matrix_set(diffdata[2], j, k, 0); } else { jas_matrix_set(diffdata[0], j, k, a); jas_matrix_set(diffdata[1], j, k, a); jas_matrix_set(diffdata[2], j, k, a); } } } for (i = 0; i < 3; ++i) { if (jas_image_writecmpt(diffimage, i, 0, 0, width, height, diffdata[i])) { return 0; } } return diffimage; }
static int dec_rawsigpass(jpc_dec_t *dec, jpc_bitstream_t *in, int bitpos, int vcausalflag, jas_matrix_t *flags, jas_matrix_t *data) { int i; int j; int k; int one; int half; int oneplushalf; int vscanlen; int width; int height; jpc_fix_t *fp; int frowstep; int fstripestep; jpc_fix_t *fstripestart; jpc_fix_t *fvscanstart; jpc_fix_t *dp; int drowstep; int dstripestep; jpc_fix_t *dstripestart; jpc_fix_t *dvscanstart; width = jas_matrix_numcols(data); height = jas_matrix_numrows(data); frowstep = jas_matrix_rowstep(flags); drowstep = jas_matrix_rowstep(data); fstripestep = frowstep << 2; dstripestep = drowstep << 2; one = 1 << bitpos; half = one >> 1; oneplushalf = one | half; fstripestart = jas_matrix_getref(flags, 1, 1); dstripestart = jas_matrix_getref(data, 0, 0); for (i = height; i > 0; i -= 4, fstripestart += fstripestep, dstripestart += dstripestep) { fvscanstart = fstripestart; dvscanstart = dstripestart; vscanlen = JAS_MIN(i, 4); for (j = width; j > 0; --j, ++fvscanstart, ++dvscanstart) { fp = fvscanstart; dp = dvscanstart; k = vscanlen; /* Process first sample in vertical scan. */ jpc_rawsigpass_step(fp, frowstep, dp, oneplushalf, in, vcausalflag); if (--k <= 0) { continue; } fp += frowstep; dp += drowstep; /* Process second sample in vertical scan. */ jpc_rawsigpass_step(fp, frowstep, dp, oneplushalf, in, 0); if (--k <= 0) { continue; } fp += frowstep; dp += drowstep; /* Process third sample in vertical scan. */ jpc_rawsigpass_step(fp, frowstep, dp, oneplushalf, in, 0); if (--k <= 0) { continue; } fp += frowstep; dp += drowstep; /* Process fourth sample in vertical scan. */ jpc_rawsigpass_step(fp, frowstep, dp, oneplushalf, in, 0); } } return 0; }