From 0be19c55633250b57c626cad49dabdc54bb477ca Mon Sep 17 00:00:00 2001 From: giraffedata Date: Sun, 6 Nov 2022 19:08:28 +0000 Subject: cleanup git-svn-id: http://svn.code.sf.net/p/netpbm/code/trunk@4455 9d0c8265-081b-0410-96cb-a4ca84ce46f8 --- converter/other/jpeg2000/libjasper/jpc/jpc_enc.c | 2230 +++++++++++----------- 1 file changed, 1112 insertions(+), 1118 deletions(-) (limited to 'converter/other/jpeg2000') diff --git a/converter/other/jpeg2000/libjasper/jpc/jpc_enc.c b/converter/other/jpeg2000/libjasper/jpc/jpc_enc.c index 9af25771..e317c3f5 100644 --- a/converter/other/jpeg2000/libjasper/jpc/jpc_enc.c +++ b/converter/other/jpeg2000/libjasper/jpc/jpc_enc.c @@ -77,17 +77,8 @@ void jpc_enc_dump(jpc_enc_t *enc); \*****************************************************************************/ void quantize(jas_matrix_t *data, jpc_fix_t stepsize); -static int jpc_enc_encodemainhdr(jpc_enc_t *enc); -static int jpc_enc_encodemainbody(jpc_enc_t *enc); -int jpc_enc_encodetiledata(jpc_enc_t *enc); jpc_enc_t *jpc_enc_create(jpc_enc_cp_t *cp, jas_stream_t *out, jas_image_t *image); void jpc_enc_destroy(jpc_enc_t *enc); -static int jpc_enc_encodemainhdr(jpc_enc_t *enc); -static int jpc_enc_encodemainbody(jpc_enc_t *enc); -int jpc_enc_encodetiledata(jpc_enc_t *enc); -int setins(int numvalues, jpc_flt_t *values, jpc_flt_t value); -static jpc_enc_cp_t *cp_create(char *optstr, jas_image_t *image); -void jpc_enc_cp_destroy(jpc_enc_cp_t *cp); @@ -227,67 +218,9 @@ trace(const char * const fmt, ...) { -int -jpc_encode(jas_image_t *image, jas_stream_t *out, char *optstr) { - - jpc_enc_t *enc; - jpc_enc_cp_t *cp; - - enc = 0; - cp = 0; - - jpc_initluts(); - - if (!(cp = cp_create(optstr, image))) { - fprintf(stderr, "invalid JP encoder options\n"); - goto error; - } - - if (!(enc = jpc_enc_create(cp, out, image))) { - goto error; - } - cp = 0; - - /* Encode the main header. */ - if (jpc_enc_encodemainhdr(enc)) { - goto error; - } - - /* Encode the main body. This constitutes most of the encoding work. */ - if (jpc_enc_encodemainbody(enc)) { - goto error; - } - - /* Write EOC marker segment. */ - if (!(enc->mrk = jpc_ms_create(JPC_MS_EOC))) { - goto error; - } - if (jpc_putms(enc->out, enc->cstate, enc->mrk)) { - fprintf(stderr, "cannot write EOI marker\n"); - goto error; - } - jpc_ms_destroy(enc->mrk); - enc->mrk = 0; - - if (jas_stream_flush(enc->out)) { - goto error; - } - - jpc_enc_destroy(enc); - - return 0; - -error: - if (cp) { - jpc_enc_cp_destroy(cp); - } - if (enc) { - jpc_enc_destroy(enc); - } - return -1; -} - - +/*****************************************************************************\ +* Option parsing code. +\*****************************************************************************/ static void ratestrtosize(const char * const s, @@ -311,9 +244,19 @@ ratestrtosize(const char * const s, -/*****************************************************************************\ -* Option parsing code. -\*****************************************************************************/ +static void +cp_destroy(jpc_enc_cp_t *cp) { + + if (cp->ccps) { + if (cp->tcp.ilyrrates) { + jas_free(cp->tcp.ilyrrates); + } + jas_free(cp->ccps); + } + jas_free(cp); +} + + static jpc_enc_cp_t * cp_create(char *optstr, jas_image_t *image) { @@ -749,96 +692,15 @@ error: jas_tvparser_destroy(tvp); } if (cp) { - jpc_enc_cp_destroy(cp); - } - return 0; -} - - - -void -jpc_enc_cp_destroy(jpc_enc_cp_t *cp) { - - if (cp->ccps) { - if (cp->tcp.ilyrrates) { - jas_free(cp->tcp.ilyrrates); - } - jas_free(cp->ccps); - } - jas_free(cp); -} - - - -/*****************************************************************************\ -* Encoder constructor and destructor. -\*****************************************************************************/ - -jpc_enc_t * -jpc_enc_create(jpc_enc_cp_t *cp, jas_stream_t *out, jas_image_t *image) { - - jpc_enc_t *enc; - - enc = 0; - - if (!(enc = jas_malloc(sizeof(jpc_enc_t)))) { - goto error; - } - - enc->image = image; - enc->out = out; - enc->cp = cp; - enc->cstate = 0; - enc->tmpstream = 0; - enc->mrk = 0; - enc->curtile = 0; - - if (!(enc->cstate = jpc_cstate_create())) { - goto error; - } - enc->len = 0; - enc->mainbodysize = 0; - - return enc; - -error: - - if (enc) { - jpc_enc_destroy(enc); + cp_destroy(cp); } return 0; } -void -jpc_enc_destroy(jpc_enc_t *enc) { - - /* The image object (i.e., enc->image) and output stream object - (i.e., enc->out) are created outside of the encoder. - Therefore, they must not be destroyed here. - */ - - if (enc->curtile) { - jpc_enc_tile_destroy(enc->curtile); - } - if (enc->cp) { - jpc_enc_cp_destroy(enc->cp); - } - if (enc->cstate) { - jpc_cstate_destroy(enc->cstate); - } - if (enc->tmpstream) { - jas_stream_close(enc->tmpstream); - } - - jas_free(enc); -} - - - static int -jpc_enc_encodemainhdr(jpc_enc_t *enc) { +encodemainhdr(jpc_enc_t *enc) { uint_fast32_t const maintlrlen = 2; @@ -1065,48 +927,6 @@ jpc_enc_encodemainhdr(jpc_enc_t *enc) { -int -jpc_enc_encodetiledata(jpc_enc_t *enc) { - - assert(enc->tmpstream); - if (jpc_enc_encpkts(enc, enc->tmpstream)) { - return -1; - } - return 0; -} - - - -void -quantize(jas_matrix_t *data, jpc_fix_t stepsize) { - - int i; - int j; - jpc_fix_t t; - - if (stepsize == jpc_inttofix(1)) { - return; - } - - for (i = 0; i < jas_matrix_numrows(data); ++i) { - for (j = 0; j < jas_matrix_numcols(data); ++j) { - t = jas_matrix_get(data, i, j); - -{ - if (t < 0) { - t = jpc_fix_neg(jpc_fix_div(jpc_fix_neg(t), stepsize)); - } else { - t = jpc_fix_div(t, stepsize); - } -} - - jas_matrix_set(data, i, j, t); - } - } -} - - - static void calcrdslopes(jpc_enc_cblk_t *cblk) { @@ -1653,1147 +1473,1321 @@ performTier2Coding(jpc_enc_t * const encP, -/*****************************************************************************\ -* Tile constructors and destructors. -\*****************************************************************************/ - -jpc_enc_tile_t * -jpc_enc_tile_create(jpc_enc_cp_t *cp, jas_image_t *image, int tileno) { - - jpc_enc_tile_t *tile; - uint_fast32_t htileno; - uint_fast32_t vtileno; - uint_fast16_t lyrno; - uint_fast16_t cmptno; - jpc_enc_tcmpt_t *tcmpt; +static int +encodetiledata(jpc_enc_t *enc) { - if (!(tile = jas_malloc(sizeof(jpc_enc_tile_t)))) { - goto error; + assert(enc->tmpstream); + if (jpc_enc_encpkts(enc, enc->tmpstream)) { + return -1; } + return 0; +} - /* Initialize a few members used in error recovery. */ - tile->tcmpts = 0; - tile->lyrsizes = 0; - tile->numtcmpts = cp->numcmpts; - tile->pi = 0; - - tile->tileno = tileno; - htileno = tileno % cp->numhtiles; - vtileno = tileno / cp->numhtiles; - - /* Calculate the coordinates of the top-left and bottom-right - corners of the tile. */ - tile->tlx = JAS_MAX(cp->tilegrdoffx + htileno * cp->tilewidth, - cp->imgareatlx); - tile->tly = JAS_MAX(cp->tilegrdoffy + vtileno * cp->tileheight, - cp->imgareatly); - tile->brx = JAS_MIN(cp->tilegrdoffx + (htileno + 1) * cp->tilewidth, - cp->refgrdwidth); - tile->bry = JAS_MIN(cp->tilegrdoffy + (vtileno + 1) * cp->tileheight, - cp->refgrdheight); - /* Initialize some tile coding parameters. */ - tile->intmode = cp->tcp.intmode; - tile->csty = cp->tcp.csty; - tile->prg = cp->tcp.prg; - tile->mctid = cp->tcp.mctid; - tile->numlyrs = cp->tcp.numlyrs; - if (!(tile->lyrsizes = jas_malloc(tile->numlyrs * - sizeof(uint_fast32_t)))) { - goto error; - } - for (lyrno = 0; lyrno < tile->numlyrs; ++lyrno) { - tile->lyrsizes[lyrno] = 0; - } +static int +encodemainbody(jpc_enc_t *enc) { - /* Allocate an array for the per-tile-component information. */ - if (!(tile->tcmpts = jas_malloc(cp->numcmpts * sizeof(jpc_enc_tcmpt_t)))) { - goto error; - } - /* Initialize a few members critical for error recovery. */ - for (cmptno = 0, tcmpt = tile->tcmpts; cmptno < cp->numcmpts; - ++cmptno, ++tcmpt) { - tcmpt->rlvls = 0; - tcmpt->tsfb = 0; - tcmpt->data = 0; - } - /* Initialize the per-tile-component information. */ - for (cmptno = 0, tcmpt = tile->tcmpts; cmptno < cp->numcmpts; - ++cmptno, ++tcmpt) { - if (!tcmpt_create(tcmpt, cp, image, tile)) { - goto error; + int tileno; + int i; + jpc_sot_t *sot; + jpc_enc_tcmpt_t *comp; + jpc_enc_tcmpt_t *endcomps; + jpc_enc_band_t *band; + jpc_enc_band_t *endbands; + jpc_enc_rlvl_t *lvl; + int rlvlno; + jpc_qcc_t *qcc; + jpc_cod_t *cod; + int adjust; + int j; + int absbandno; + long tilehdrlen; + long tilelen; + jpc_enc_tile_t *tile; + jpc_enc_cp_t *cp; + double rho; + uint_fast16_t cmptno; + int samestepsizes; + jpc_enc_ccp_t *ccps; + jpc_enc_tccp_t *tccp; + int bandno; + uint_fast32_t x; + uint_fast32_t y; + int mingbits; + int actualnumbps; + jpc_fix_t mxmag; + jpc_fix_t mag; + int numgbits; + const char * error; + int rc; + + cp = enc->cp; + + for (tileno = 0; tileno < cp->numtiles; ++tileno) { + enc->curtile = jpc_enc_tile_create(enc->cp, enc->image, tileno); + if (!enc->curtile) + abort(); + + tile = enc->curtile; + + if (jas_getdbglevel() >= 10) { + jpc_enc_dump(enc); } - } - /* Initialize the synthesis weights for the MCT. */ - switch (tile->mctid) { - case JPC_MCT_RCT: - tile->tcmpts[0].synweight = jpc_dbltofix(sqrt(3.0)); - tile->tcmpts[1].synweight = jpc_dbltofix(sqrt(0.6875)); - tile->tcmpts[2].synweight = jpc_dbltofix(sqrt(0.6875)); - break; - case JPC_MCT_ICT: - tile->tcmpts[0].synweight = jpc_dbltofix(sqrt(3.0000)); - tile->tcmpts[1].synweight = jpc_dbltofix(sqrt(3.2584)); - tile->tcmpts[2].synweight = jpc_dbltofix(sqrt(2.4755)); - break; - default: - case JPC_MCT_NONE: - for (cmptno = 0, tcmpt = tile->tcmpts; cmptno < cp->numcmpts; - ++cmptno, ++tcmpt) { - tcmpt->synweight = JPC_FIX_ONE; + endcomps = &tile->tcmpts[tile->numtcmpts]; + for (cmptno = 0, comp = tile->tcmpts; + cmptno < tile->numtcmpts; + ++cmptno, ++comp) { + if (!cp->ccps[cmptno].sgnd) { + adjust = 1 << (cp->ccps[cmptno].prec - 1); + for (i = 0; i < jas_matrix_numrows(comp->data); ++i) { + for (j = 0; j < jas_matrix_numcols(comp->data); ++j) { + *jas_matrix_getref(comp->data, i, j) -= adjust; + } + } + } } - break; - } - if (!(tile->pi = jpc_enc_pi_create(cp, tile))) { - goto error; - } + if (!tile->intmode) { + endcomps = &tile->tcmpts[tile->numtcmpts]; + for (comp = tile->tcmpts; comp != endcomps; ++comp) { + jas_matrix_asl(comp->data, JPC_FIX_FRACBITS); + } + } - return tile; + switch (tile->mctid) { + case JPC_MCT_RCT: + assert(jas_image_numcmpts(enc->image) == 3); + jpc_rct(tile->tcmpts[0].data, tile->tcmpts[1].data, + tile->tcmpts[2].data); + break; + case JPC_MCT_ICT: + assert(jas_image_numcmpts(enc->image) == 3); + jpc_ict(tile->tcmpts[0].data, tile->tcmpts[1].data, + tile->tcmpts[2].data); + break; + default: + break; + } -error: + for (i = 0; i < jas_image_numcmpts(enc->image); ++i) { + comp = &tile->tcmpts[i]; + jpc_tsfb_analyze(comp->tsfb, + ((comp->qmfbid == JPC_COX_RFT) ? + JPC_TSFB_RITIMODE : 0), comp->data); - if (tile) { - jpc_enc_tile_destroy(tile); - } - return 0; -} + } + endcomps = &tile->tcmpts[tile->numtcmpts]; + for (cmptno = 0, comp = tile->tcmpts; + comp != endcomps; + ++cmptno, ++comp) { + mingbits = 0; + absbandno = 0; + /* All bands must have a corresponding quantizer step size, + even if they contain no samples and are never coded. */ + /* Some bands may not be hit by the loop below, so we must + initialize all of the step sizes to a sane value. */ + memset(comp->stepsizes, 0, sizeof(comp->stepsizes)); + for (rlvlno = 0, lvl = comp->rlvls; + rlvlno < comp->numrlvls; + ++rlvlno, ++lvl) { + if (!lvl->bands) { + absbandno += rlvlno ? 3 : 1; + continue; + } + endbands = &lvl->bands[lvl->numbands]; + for (band = lvl->bands; band != endbands; ++band) { + if (!band->data) { + ++absbandno; + continue; + } + actualnumbps = 0; + mxmag = 0; + for (y = 0; y < jas_matrix_numrows(band->data); ++y) { + for (x = 0; x < jas_matrix_numcols(band->data); ++x) { + mag = abs(jas_matrix_get(band->data, y, x)); + if (mag > mxmag) { + mxmag = mag; + } + } + } + if (tile->intmode) { + actualnumbps = + jpc_firstone(mxmag) + 1; + } else { + actualnumbps = + jpc_firstone(mxmag) + 1 - JPC_FIX_FRACBITS; + } + numgbits = actualnumbps - (cp->ccps[cmptno].prec - 1 + + band->analgain); + if (numgbits > mingbits) { + mingbits = numgbits; + } + if (!tile->intmode) { + band->absstepsize = + jpc_fix_div( + jpc_inttofix(1 << (band->analgain + 1)), + band->synweight); + } else { + band->absstepsize = jpc_inttofix(1); + } + band->stepsize = jpc_abstorelstepsize( + band->absstepsize, cp->ccps[cmptno].prec + + band->analgain); + band->numbps = cp->tccp.numgbits + + JPC_QCX_GETEXPN(band->stepsize) - 1; + if ((!tile->intmode) && band->data) { + quantize(band->data, band->absstepsize); + } -void -jpc_enc_tile_destroy(jpc_enc_tile_t *tile) { + comp->stepsizes[absbandno] = band->stepsize; + ++absbandno; + } + } - jpc_enc_tcmpt_t *tcmpt; - uint_fast16_t cmptno; + assert(JPC_FIX_FRACBITS >= JPC_NUMEXTRABITS); + if (!tile->intmode) { + jas_matrix_divpow2(comp->data, + JPC_FIX_FRACBITS - JPC_NUMEXTRABITS); + } else { + jas_matrix_asl(comp->data, JPC_NUMEXTRABITS); + } + } - if (tile->tcmpts) { - for (cmptno = 0, tcmpt = tile->tcmpts; cmptno < - tile->numtcmpts; ++cmptno, ++tcmpt) { - tcmpt_destroy(tcmpt); + if (mingbits > cp->tccp.numgbits) { + fprintf(stderr, "error: too few guard bits (need at least %d)\n", + mingbits); + return -1; } - jas_free(tile->tcmpts); - } - if (tile->lyrsizes) { - jas_free(tile->lyrsizes); - } - if (tile->pi) { - jpc_pi_destroy(tile->pi); - } - jas_free(tile); -} + enc->tmpstream = jas_stream_memopen(0, 0); + if (!enc->tmpstream) { + fprintf(stderr, "cannot open tmp file\n"); + return -1; + } + /* Write the tile header. */ + enc->mrk = jpc_ms_create(JPC_MS_SOT); + if (!enc->mrk) + return -1; + sot = &enc->mrk->parms.sot; + sot->len = 0; + sot->tileno = tileno; + sot->partno = 0; + sot->numparts = 1; + if (jpc_putms(enc->tmpstream, enc->cstate, enc->mrk)) { + fprintf(stderr, "cannot write SOT marker\n"); + return -1; + } + jpc_ms_destroy(enc->mrk); + enc->mrk = 0; -static jpc_enc_tcmpt_t * -tcmpt_create(jpc_enc_tcmpt_t * const tcmpt, - jpc_enc_cp_t * const cp, - jas_image_t * const image, - jpc_enc_tile_t * const tile) { +/************************************************************************/ +/************************************************************************/ +/************************************************************************/ - uint_fast16_t cmptno; - uint_fast16_t rlvlno; - jpc_enc_rlvl_t *rlvl; - uint_fast32_t tlx; - uint_fast32_t tly; - uint_fast32_t brx; - uint_fast32_t bry; - uint_fast32_t cmpttlx; - uint_fast32_t cmpttly; - jpc_enc_ccp_t *ccp; - jpc_tsfb_band_t bandinfos[JPC_MAXBANDS]; + tccp = &cp->tccp; + for (cmptno = 0; cmptno < cp->numcmpts; ++cmptno) { + comp = &tile->tcmpts[cmptno]; + if (comp->numrlvls != tccp->maxrlvls) { + if (!(enc->mrk = jpc_ms_create(JPC_MS_COD))) { + return -1; + } + /* XXX = this is not really correct. we are using comp #0's + precint sizes and other characteristics */ + comp = &tile->tcmpts[0]; + cod = &enc->mrk->parms.cod; + cod->compparms.csty = 0; + cod->compparms.numdlvls = comp->numrlvls - 1; + cod->prg = tile->prg; + cod->numlyrs = tile->numlyrs; + cod->compparms.cblkwidthval = + JPC_COX_CBLKSIZEEXPN(comp->cblkwidthexpn); + cod->compparms.cblkheightval = + JPC_COX_CBLKSIZEEXPN(comp->cblkheightexpn); + cod->compparms.cblksty = comp->cblksty; + cod->compparms.qmfbid = comp->qmfbid; + cod->mctrans = (tile->mctid != JPC_MCT_NONE); + for (i = 0; i < comp->numrlvls; ++i) { + cod->compparms.rlvls[i].parwidthval = + comp->rlvls[i].prcwidthexpn; + cod->compparms.rlvls[i].parheightval = + comp->rlvls[i].prcheightexpn; + } + if (jpc_putms(enc->tmpstream, enc->cstate, enc->mrk)) { + return -1; + } + jpc_ms_destroy(enc->mrk); + enc->mrk = 0; + } + } - tcmpt->tile = tile; - tcmpt->tsfb = 0; - tcmpt->data = 0; - tcmpt->rlvls = 0; + for (cmptno = 0, comp = tile->tcmpts; + cmptno < cp->numcmpts; + ++cmptno, ++comp) { + ccps = &cp->ccps[cmptno]; + if (ccps->numstepsizes == comp->numstepsizes) { + samestepsizes = 1; + for (bandno = 0; bandno < ccps->numstepsizes; ++bandno) { + if (ccps->stepsizes[bandno] != comp->stepsizes[bandno]) { + samestepsizes = 0; + break; + } + } + } else { + samestepsizes = 0; + } + if (!samestepsizes) { + if (!(enc->mrk = jpc_ms_create(JPC_MS_QCC))) { + return -1; + } + qcc = &enc->mrk->parms.qcc; + qcc->compno = cmptno; + qcc->compparms.numguard = cp->tccp.numgbits; + qcc->compparms.qntsty = (comp->qmfbid == JPC_COX_INS) ? + JPC_QCX_SEQNT : JPC_QCX_NOQNT; + qcc->compparms.numstepsizes = comp->numstepsizes; + qcc->compparms.stepsizes = comp->stepsizes; + if (jpc_putms(enc->tmpstream, enc->cstate, enc->mrk)) { + return -1; + } + qcc->compparms.stepsizes = 0; + jpc_ms_destroy(enc->mrk); + enc->mrk = 0; + } + } + + /* Write a SOD marker to indicate the end of the tile header. */ + if (!(enc->mrk = jpc_ms_create(JPC_MS_SOD))) { + return -1; + } + if (jpc_putms(enc->tmpstream, enc->cstate, enc->mrk)) { + fprintf(stderr, "cannot write SOD marker\n"); + return -1; + } + jpc_ms_destroy(enc->mrk); + enc->mrk = 0; + tilehdrlen = jas_stream_getrwcount(enc->tmpstream); + +/************************************************************************/ +/************************************************************************/ +/************************************************************************/ + + rc = jpc_enc_enccblks(enc); + if (rc != 0) { + fprintf(stderr, "jpc_enc_enccblks() failed, called from " + "'encodemainbody'\n"); + return -1; + } + + cp = enc->cp; + rho = (double) (tile->brx - tile->tlx) * (tile->bry - tile->tly) / + ((cp->refgrdwidth - cp->imgareatlx) * (cp->refgrdheight - + cp->imgareatly)); + tile->rawsize = cp->rawsize * rho; + + computeLayerSizes(enc, tile, cp, rho, tilehdrlen, &error); + + if (!error) { + int rc; + performTier2Coding(enc, tile->numlyrs, tile->lyrsizes, &error); + + rc = encodetiledata(enc); + if (rc != 0) + pm_asprintf(&error, "encodetiledata() failed\n"); + } + + if (error) { + fprintf(stderr, "%s\n", error); + pm_strfree(error); + return -1; + } + + tilelen = jas_stream_tell(enc->tmpstream); + + if (jas_stream_seek(enc->tmpstream, 6, SEEK_SET) < 0) { + return -1; + } + jpc_putuint32(enc->tmpstream, tilelen); + + if (jas_stream_seek(enc->tmpstream, 0, SEEK_SET) < 0) { + return -1; + } + if (jpc_putdata(enc->out, enc->tmpstream, -1)) { + return -1; + } + enc->len += tilelen; + + jas_stream_close(enc->tmpstream); + enc->tmpstream = 0; + + jpc_enc_tile_destroy(enc->curtile); + enc->curtile = 0; + + } + + return 0; +} + + + +int +jpc_encode(jas_image_t *image, jas_stream_t *out, char *optstr) { - /* Deduce the component number. */ - cmptno = tcmpt - tile->tcmpts; + jpc_enc_t *enc; + jpc_enc_cp_t *cp; - ccp = &cp->ccps[cmptno]; + enc = 0; + cp = 0; - /* Compute the coordinates of the top-left and bottom-right - corners of this tile-component. */ - tlx = JPC_CEILDIV(tile->tlx, ccp->sampgrdstepx); - tly = JPC_CEILDIV(tile->tly, ccp->sampgrdstepy); - brx = JPC_CEILDIV(tile->brx, ccp->sampgrdstepx); - bry = JPC_CEILDIV(tile->bry, ccp->sampgrdstepy); + jpc_initluts(); - /* Create a sequence to hold the tile-component sample data. */ - tcmpt->data = jas_seq2d_create(tlx, tly, brx, bry); - if (!tcmpt->data) + if (!(cp = cp_create(optstr, image))) { + fprintf(stderr, "invalid JP encoder options\n"); goto error; + } - /* Get the image data associated with this tile-component. */ - cmpttlx = JPC_CEILDIV(cp->imgareatlx, ccp->sampgrdstepx); - cmpttly = JPC_CEILDIV(cp->imgareatly, ccp->sampgrdstepy); - if (jas_image_readcmpt(image, cmptno, tlx - cmpttlx, tly - cmpttly, - brx - tlx, bry - tly, tcmpt->data)) { + if (!(enc = jpc_enc_create(cp, out, image))) { goto error; } + cp = 0; - tcmpt->synweight = 0; - tcmpt->qmfbid = cp->tccp.qmfbid; - tcmpt->numrlvls = cp->tccp.maxrlvls; - tcmpt->numbands = 3 * tcmpt->numrlvls - 2; - - tcmpt->tsfb = jpc_cod_gettsfb(tcmpt->qmfbid, tcmpt->numrlvls - 1); - - if (!tcmpt->tsfb) + /* Encode the main header. */ + if (encodemainhdr(enc)) { goto error; - - for (rlvlno = 0; rlvlno < tcmpt->numrlvls; ++rlvlno) { - tcmpt->prcwidthexpns[rlvlno] = cp->tccp.prcwidthexpns[rlvlno]; - tcmpt->prcheightexpns[rlvlno] = cp->tccp.prcheightexpns[rlvlno]; } - tcmpt->cblkwidthexpn = cp->tccp.cblkwidthexpn; - tcmpt->cblkheightexpn = cp->tccp.cblkheightexpn; - tcmpt->cblksty = cp->tccp.cblksty; - tcmpt->csty = cp->tccp.csty; - - tcmpt->numstepsizes = tcmpt->numbands; - assert(tcmpt->numstepsizes <= JPC_MAXBANDS); - memset(tcmpt->stepsizes, 0, - sizeof(tcmpt->numstepsizes * sizeof(uint_fast16_t))); - - /* Retrieve information about the various bands. */ - jpc_tsfb_getbands(tcmpt->tsfb, jas_seq2d_xstart(tcmpt->data), - jas_seq2d_ystart(tcmpt->data), - jas_seq2d_xend(tcmpt->data), - jas_seq2d_yend(tcmpt->data), bandinfos); - tcmpt->rlvls = jas_malloc(tcmpt->numrlvls * sizeof(jpc_enc_rlvl_t)); - if (!tcmpt->rlvls) + /* Encode the main body. This constitutes most of the encoding work. */ + if (encodemainbody(enc)) { goto error; + } - for (rlvlno = 0, rlvl = tcmpt->rlvls; rlvlno < tcmpt->numrlvls; - ++rlvlno, ++rlvl) { - rlvl->bands = 0; - rlvl->tcmpt = tcmpt; + /* Write EOC marker segment. */ + if (!(enc->mrk = jpc_ms_create(JPC_MS_EOC))) { + goto error; } - for (rlvlno = 0, rlvl = tcmpt->rlvls; rlvlno < tcmpt->numrlvls; - ++rlvlno, ++rlvl) { - if (!rlvl_create(rlvl, cp, tcmpt, bandinfos)) { - goto error; - } + if (jpc_putms(enc->out, enc->cstate, enc->mrk)) { + fprintf(stderr, "cannot write EOI marker\n"); + goto error; } + jpc_ms_destroy(enc->mrk); + enc->mrk = 0; - return tcmpt; + if (jas_stream_flush(enc->out)) { + goto error; + } -error: + jpc_enc_destroy(enc); - tcmpt_destroy(tcmpt); return 0; +error: + if (cp) { + cp_destroy(cp); + } + if (enc) { + jpc_enc_destroy(enc); + } + return -1; } -static void -tcmpt_destroy(jpc_enc_tcmpt_t *tcmpt) { +/*****************************************************************************\ +* Encoder constructor and destructor. +\*****************************************************************************/ - jpc_enc_rlvl_t *rlvl; - uint_fast16_t rlvlno; +jpc_enc_t * +jpc_enc_create(jpc_enc_cp_t *cp, jas_stream_t *out, jas_image_t *image) { - if (tcmpt->rlvls) { - for (rlvlno = 0, rlvl = tcmpt->rlvls; rlvlno < tcmpt->numrlvls; - ++rlvlno, ++rlvl) { - rlvl_destroy(rlvl); - } - jas_free(tcmpt->rlvls); - } + jpc_enc_t *enc; - if (tcmpt->data) { - jas_seq2d_destroy(tcmpt->data); + enc = 0; + + if (!(enc = jas_malloc(sizeof(jpc_enc_t)))) { + goto error; } - if (tcmpt->tsfb) { - jpc_tsfb_destroy(tcmpt->tsfb); + + enc->image = image; + enc->out = out; + enc->cp = cp; + enc->cstate = 0; + enc->tmpstream = 0; + enc->mrk = 0; + enc->curtile = 0; + + if (!(enc->cstate = jpc_cstate_create())) { + goto error; } -} + enc->len = 0; + enc->mainbodysize = 0; + return enc; +error: -static jpc_enc_rlvl_t * -rlvl_create(jpc_enc_rlvl_t *rlvl, jpc_enc_cp_t *cp, - jpc_enc_tcmpt_t *tcmpt, jpc_tsfb_band_t *bandinfos) { + if (enc) { + jpc_enc_destroy(enc); + } + return 0; +} - uint_fast16_t rlvlno; - uint_fast32_t tlprctlx; - uint_fast32_t tlprctly; - uint_fast32_t brprcbrx; - uint_fast32_t brprcbry; - uint_fast16_t bandno; - jpc_enc_band_t *band; - /* Deduce the resolution level. */ - rlvlno = rlvl - tcmpt->rlvls; - /* Initialize members required for error recovery. */ - rlvl->bands = 0; - rlvl->tcmpt = tcmpt; +void +jpc_enc_destroy(jpc_enc_t *enc) { - /* Compute the coordinates of the top-left and bottom-right - corners of the tile-component at this resolution. */ - rlvl->tlx = - JPC_CEILDIVPOW2(jas_seq2d_xstart(tcmpt->data), tcmpt->numrlvls - - 1 - rlvlno); - rlvl->tly = - JPC_CEILDIVPOW2(jas_seq2d_ystart(tcmpt->data), tcmpt->numrlvls - - 1 - rlvlno); - rlvl->brx = - JPC_CEILDIVPOW2(jas_seq2d_xend(tcmpt->data), tcmpt->numrlvls - - 1 - rlvlno); - rlvl->bry = - JPC_CEILDIVPOW2(jas_seq2d_yend(tcmpt->data), tcmpt->numrlvls - - 1 - rlvlno); + /* The image object (i.e., enc->image) and output stream object + (i.e., enc->out) are created outside of the encoder. + Therefore, they must not be destroyed here. + */ - if (rlvl->tlx >= rlvl->brx || rlvl->tly >= rlvl->bry) { - rlvl->numhprcs = 0; - rlvl->numvprcs = 0; - rlvl->numprcs = 0; - return rlvl; + if (enc->curtile) { + jpc_enc_tile_destroy(enc->curtile); } - - rlvl->numbands = (!rlvlno) ? 1 : 3; - rlvl->prcwidthexpn = cp->tccp.prcwidthexpns[rlvlno]; - rlvl->prcheightexpn = cp->tccp.prcheightexpns[rlvlno]; - if (!rlvlno) { - rlvl->cbgwidthexpn = rlvl->prcwidthexpn; - rlvl->cbgheightexpn = rlvl->prcheightexpn; - } else { - rlvl->cbgwidthexpn = rlvl->prcwidthexpn - 1; - rlvl->cbgheightexpn = rlvl->prcheightexpn - 1; + if (enc->cp) { + cp_destroy(enc->cp); } - rlvl->cblkwidthexpn = - JAS_MIN(cp->tccp.cblkwidthexpn, rlvl->cbgwidthexpn); - rlvl->cblkheightexpn = - JAS_MIN(cp->tccp.cblkheightexpn, rlvl->cbgheightexpn); - - /* Compute the number of precincts. */ - tlprctlx = JPC_FLOORTOMULTPOW2(rlvl->tlx, rlvl->prcwidthexpn); - tlprctly = JPC_FLOORTOMULTPOW2(rlvl->tly, rlvl->prcheightexpn); - brprcbrx = JPC_CEILTOMULTPOW2(rlvl->brx, rlvl->prcwidthexpn); - brprcbry = JPC_CEILTOMULTPOW2(rlvl->bry, rlvl->prcheightexpn); - rlvl->numhprcs = JPC_FLOORDIVPOW2(brprcbrx - tlprctlx, rlvl->prcwidthexpn); - rlvl->numvprcs = - JPC_FLOORDIVPOW2(brprcbry - tlprctly, rlvl->prcheightexpn); - rlvl->numprcs = rlvl->numhprcs * rlvl->numvprcs; - - rlvl->bands = jas_malloc(rlvl->numbands * sizeof(jpc_enc_band_t)); - if (!rlvl->bands) - goto error; - - for (bandno = 0, band = rlvl->bands; bandno < rlvl->numbands; - ++bandno, ++band) { - band->prcs = 0; - band->data = 0; - band->rlvl = rlvl; + if (enc->cstate) { + jpc_cstate_destroy(enc->cstate); } - for (bandno = 0, band = rlvl->bands; bandno < rlvl->numbands; - ++bandno, ++band) { - if (!band_create(band, cp, rlvl, bandinfos)) { - goto error; - } + if (enc->tmpstream) { + jas_stream_close(enc->tmpstream); } - return rlvl; -error: - - rlvl_destroy(rlvl); - return 0; + jas_free(enc); } -static void -rlvl_destroy(jpc_enc_rlvl_t *rlvl) { +void +quantize(jas_matrix_t *data, jpc_fix_t stepsize) { - jpc_enc_band_t *band; - uint_fast16_t bandno; + int i; + int j; + jpc_fix_t t; - if (rlvl->bands) { - for (bandno = 0, band = rlvl->bands; bandno < rlvl->numbands; - ++bandno, ++band) { - band_destroy(band); + if (stepsize == jpc_inttofix(1)) { + return; + } + + for (i = 0; i < jas_matrix_numrows(data); ++i) { + for (j = 0; j < jas_matrix_numcols(data); ++j) { + t = jas_matrix_get(data, i, j); + +{ + if (t < 0) { + t = jpc_fix_neg(jpc_fix_div(jpc_fix_neg(t), stepsize)); + } else { + t = jpc_fix_div(t, stepsize); + } +} + + jas_matrix_set(data, i, j, t); } - jas_free(rlvl->bands); } } -static jpc_enc_band_t * -band_create(jpc_enc_band_t *band, jpc_enc_cp_t *cp, - jpc_enc_rlvl_t *rlvl, jpc_tsfb_band_t *bandinfos) { +/*****************************************************************************\ +* Tile constructors and destructors. +\*****************************************************************************/ - uint_fast16_t bandno; - uint_fast16_t gblbandno; - uint_fast16_t rlvlno; - jpc_tsfb_band_t *bandinfo; +jpc_enc_tile_t * +jpc_enc_tile_create(jpc_enc_cp_t *cp, jas_image_t *image, int tileno) { + + jpc_enc_tile_t *tile; + uint_fast32_t htileno; + uint_fast32_t vtileno; + uint_fast16_t lyrno; + uint_fast16_t cmptno; jpc_enc_tcmpt_t *tcmpt; - uint_fast32_t prcno; - jpc_enc_prc_t *prc; - tcmpt = rlvl->tcmpt; - band->data = 0; - band->prcs = 0; - band->rlvl = rlvl; + if (!(tile = jas_malloc(sizeof(jpc_enc_tile_t)))) { + goto error; + } - /* Deduce the resolution level and band number. */ - rlvlno = rlvl - rlvl->tcmpt->rlvls; - bandno = band - rlvl->bands; - gblbandno = (!rlvlno) ? 0 : (3 * (rlvlno - 1) + bandno + 1); + /* Initialize a few members used in error recovery. */ + tile->tcmpts = 0; + tile->lyrsizes = 0; + tile->numtcmpts = cp->numcmpts; + tile->pi = 0; - bandinfo = &bandinfos[gblbandno]; + tile->tileno = tileno; + htileno = tileno % cp->numhtiles; + vtileno = tileno / cp->numhtiles; - if (bandinfo->xstart != bandinfo->xend && - bandinfo->ystart != bandinfo->yend) { - band->data = jas_seq2d_create(0, 0, 0, 0); - if (!band->data) - goto error; - jas_seq2d_bindsub(band->data, tcmpt->data, bandinfo->locxstart, - bandinfo->locystart, bandinfo->locxend, - bandinfo->locyend); - jas_seq2d_setshift(band->data, bandinfo->xstart, bandinfo->ystart); + /* Calculate the coordinates of the top-left and bottom-right + corners of the tile. */ + tile->tlx = JAS_MAX(cp->tilegrdoffx + htileno * cp->tilewidth, + cp->imgareatlx); + tile->tly = JAS_MAX(cp->tilegrdoffy + vtileno * cp->tileheight, + cp->imgareatly); + tile->brx = JAS_MIN(cp->tilegrdoffx + (htileno + 1) * cp->tilewidth, + cp->refgrdwidth); + tile->bry = JAS_MIN(cp->tilegrdoffy + (vtileno + 1) * cp->tileheight, + cp->refgrdheight); + + /* Initialize some tile coding parameters. */ + tile->intmode = cp->tcp.intmode; + tile->csty = cp->tcp.csty; + tile->prg = cp->tcp.prg; + tile->mctid = cp->tcp.mctid; + + tile->numlyrs = cp->tcp.numlyrs; + if (!(tile->lyrsizes = jas_malloc(tile->numlyrs * + sizeof(uint_fast32_t)))) { + goto error; + } + for (lyrno = 0; lyrno < tile->numlyrs; ++lyrno) { + tile->lyrsizes[lyrno] = 0; } - band->orient = bandinfo->orient; - band->analgain = JPC_NOMINALGAIN(cp->tccp.qmfbid, tcmpt->numrlvls, rlvlno, - band->orient); - band->numbps = 0; - band->absstepsize = 0; - band->stepsize = 0; - band->synweight = bandinfo->synenergywt; - if (band->data) { - band->prcs = jas_malloc(rlvl->numprcs * sizeof(jpc_enc_prc_t)); - if (!band->prcs) + /* Allocate an array for the per-tile-component information. */ + if (!(tile->tcmpts = jas_malloc(cp->numcmpts * sizeof(jpc_enc_tcmpt_t)))) { + goto error; + } + /* Initialize a few members critical for error recovery. */ + for (cmptno = 0, tcmpt = tile->tcmpts; cmptno < cp->numcmpts; + ++cmptno, ++tcmpt) { + tcmpt->rlvls = 0; + tcmpt->tsfb = 0; + tcmpt->data = 0; + } + /* Initialize the per-tile-component information. */ + for (cmptno = 0, tcmpt = tile->tcmpts; cmptno < cp->numcmpts; + ++cmptno, ++tcmpt) { + if (!tcmpt_create(tcmpt, cp, image, tile)) { goto error; - for (prcno = 0, prc = band->prcs; - prcno < rlvl->numprcs; - ++prcno, ++prc) { - prc->cblks = 0; - prc->incltree = 0; - prc->nlibtree = 0; - prc->savincltree = 0; - prc->savnlibtree = 0; - prc->band = band; } - for (prcno = 0, prc = band->prcs; - prcno < rlvl->numprcs; - ++prcno, ++prc) { - if (!prc_create(prc, cp, band)) { - goto error; - } + } + + /* Initialize the synthesis weights for the MCT. */ + switch (tile->mctid) { + case JPC_MCT_RCT: + tile->tcmpts[0].synweight = jpc_dbltofix(sqrt(3.0)); + tile->tcmpts[1].synweight = jpc_dbltofix(sqrt(0.6875)); + tile->tcmpts[2].synweight = jpc_dbltofix(sqrt(0.6875)); + break; + case JPC_MCT_ICT: + tile->tcmpts[0].synweight = jpc_dbltofix(sqrt(3.0000)); + tile->tcmpts[1].synweight = jpc_dbltofix(sqrt(3.2584)); + tile->tcmpts[2].synweight = jpc_dbltofix(sqrt(2.4755)); + break; + default: + case JPC_MCT_NONE: + for (cmptno = 0, tcmpt = tile->tcmpts; cmptno < cp->numcmpts; + ++cmptno, ++tcmpt) { + tcmpt->synweight = JPC_FIX_ONE; } + break; } - return band; + if (!(tile->pi = jpc_enc_pi_create(cp, tile))) { + goto error; + } + + return tile; error: - band_destroy(band); + + if (tile) { + jpc_enc_tile_destroy(tile); + } return 0; } -static void -band_destroy(jpc_enc_band_t *band) { +void +jpc_enc_tile_destroy(jpc_enc_tile_t *tile) { - jpc_enc_prc_t *prc; - jpc_enc_rlvl_t *rlvl; - uint_fast32_t prcno; + jpc_enc_tcmpt_t *tcmpt; + uint_fast16_t cmptno; - if (band->prcs) { - rlvl = band->rlvl; - for (prcno = 0, prc = band->prcs; prcno < rlvl->numprcs; - ++prcno, ++prc) { - prc_destroy(prc); + if (tile->tcmpts) { + for (cmptno = 0, tcmpt = tile->tcmpts; cmptno < + tile->numtcmpts; ++cmptno, ++tcmpt) { + tcmpt_destroy(tcmpt); } - jas_free(band->prcs); + jas_free(tile->tcmpts); } - if (band->data) { - jas_seq2d_destroy(band->data); + if (tile->lyrsizes) { + jas_free(tile->lyrsizes); } + if (tile->pi) { + jpc_pi_destroy(tile->pi); + } + jas_free(tile); } -static jpc_enc_prc_t * -prc_create(jpc_enc_prc_t *prc, jpc_enc_cp_t *cp, jpc_enc_band_t *band) { +static jpc_enc_tcmpt_t * +tcmpt_create(jpc_enc_tcmpt_t * const tcmpt, + jpc_enc_cp_t * const cp, + jas_image_t * const image, + jpc_enc_tile_t * const tile) { - uint_fast32_t prcno; - uint_fast32_t prcxind; - uint_fast32_t prcyind; - uint_fast32_t cbgtlx; - uint_fast32_t cbgtly; - uint_fast32_t tlprctlx; - uint_fast32_t tlprctly; - uint_fast32_t tlcbgtlx; - uint_fast32_t tlcbgtly; + uint_fast16_t cmptno; uint_fast16_t rlvlno; jpc_enc_rlvl_t *rlvl; - uint_fast32_t tlcblktlx; - uint_fast32_t tlcblktly; - uint_fast32_t brcblkbrx; - uint_fast32_t brcblkbry; - uint_fast32_t cblkno; - jpc_enc_cblk_t *cblk; - jpc_enc_tcmpt_t *tcmpt; + uint_fast32_t tlx; + uint_fast32_t tly; + uint_fast32_t brx; + uint_fast32_t bry; + uint_fast32_t cmpttlx; + uint_fast32_t cmpttly; + jpc_enc_ccp_t *ccp; + jpc_tsfb_band_t bandinfos[JPC_MAXBANDS]; - prc->cblks = 0; - prc->incltree = 0; - prc->savincltree = 0; - prc->nlibtree = 0; - prc->savnlibtree = 0; + tcmpt->tile = tile; + tcmpt->tsfb = 0; + tcmpt->data = 0; + tcmpt->rlvls = 0; - rlvl = band->rlvl; - tcmpt = rlvl->tcmpt; - rlvlno = rlvl - tcmpt->rlvls; - prcno = prc - band->prcs; - prcxind = prcno % rlvl->numhprcs; - prcyind = prcno / rlvl->numhprcs; - prc->band = band; + /* Deduce the component number. */ + cmptno = tcmpt - tile->tcmpts; - tlprctlx = JPC_FLOORTOMULTPOW2(rlvl->tlx, rlvl->prcwidthexpn); - tlprctly = JPC_FLOORTOMULTPOW2(rlvl->tly, rlvl->prcheightexpn); + ccp = &cp->ccps[cmptno]; - if (!rlvlno) { - tlcbgtlx = tlprctlx; - tlcbgtly = tlprctly; - } else { - tlcbgtlx = JPC_CEILDIVPOW2(tlprctlx, 1); - tlcbgtly = JPC_CEILDIVPOW2(tlprctly, 1); + /* Compute the coordinates of the top-left and bottom-right + corners of this tile-component. */ + tlx = JPC_CEILDIV(tile->tlx, ccp->sampgrdstepx); + tly = JPC_CEILDIV(tile->tly, ccp->sampgrdstepy); + brx = JPC_CEILDIV(tile->brx, ccp->sampgrdstepx); + bry = JPC_CEILDIV(tile->bry, ccp->sampgrdstepy); + + /* Create a sequence to hold the tile-component sample data. */ + tcmpt->data = jas_seq2d_create(tlx, tly, brx, bry); + if (!tcmpt->data) + goto error; + + /* Get the image data associated with this tile-component. */ + cmpttlx = JPC_CEILDIV(cp->imgareatlx, ccp->sampgrdstepx); + cmpttly = JPC_CEILDIV(cp->imgareatly, ccp->sampgrdstepy); + if (jas_image_readcmpt(image, cmptno, tlx - cmpttlx, tly - cmpttly, + brx - tlx, bry - tly, tcmpt->data)) { + goto error; } - /* Compute the coordinates of the top-left and bottom-right corners of the - precinct. - */ - cbgtlx = tlcbgtlx + (prcxind << rlvl->cbgwidthexpn); - cbgtly = tlcbgtly + (prcyind << rlvl->cbgheightexpn); - prc->tlx = JAS_MAX(jas_seq2d_xstart(band->data), cbgtlx); - prc->tly = JAS_MAX(jas_seq2d_ystart(band->data), cbgtly); - prc->brx = JAS_MIN(jas_seq2d_xend(band->data), cbgtlx + - (1 << rlvl->cbgwidthexpn)); - prc->bry = JAS_MIN(jas_seq2d_yend(band->data), cbgtly + - (1 << rlvl->cbgheightexpn)); + tcmpt->synweight = 0; + tcmpt->qmfbid = cp->tccp.qmfbid; + tcmpt->numrlvls = cp->tccp.maxrlvls; + tcmpt->numbands = 3 * tcmpt->numrlvls - 2; + + tcmpt->tsfb = jpc_cod_gettsfb(tcmpt->qmfbid, tcmpt->numrlvls - 1); - if (prc->tlx < prc->brx && prc->tly < prc->bry) { - /* The precinct contains at least one code block. */ + if (!tcmpt->tsfb) + goto error; - tlcblktlx = JPC_FLOORTOMULTPOW2(prc->tlx, rlvl->cblkwidthexpn); - tlcblktly = JPC_FLOORTOMULTPOW2(prc->tly, rlvl->cblkheightexpn); - brcblkbrx = JPC_CEILTOMULTPOW2(prc->brx, rlvl->cblkwidthexpn); - brcblkbry = JPC_CEILTOMULTPOW2(prc->bry, rlvl->cblkheightexpn); - prc->numhcblks = JPC_FLOORDIVPOW2(brcblkbrx - tlcblktlx, - rlvl->cblkwidthexpn); - prc->numvcblks = JPC_FLOORDIVPOW2(brcblkbry - tlcblktly, - rlvl->cblkheightexpn); - prc->numcblks = prc->numhcblks * prc->numvcblks; + for (rlvlno = 0; rlvlno < tcmpt->numrlvls; ++rlvlno) { + tcmpt->prcwidthexpns[rlvlno] = cp->tccp.prcwidthexpns[rlvlno]; + tcmpt->prcheightexpns[rlvlno] = cp->tccp.prcheightexpns[rlvlno]; + } + tcmpt->cblkwidthexpn = cp->tccp.cblkwidthexpn; + tcmpt->cblkheightexpn = cp->tccp.cblkheightexpn; + tcmpt->cblksty = cp->tccp.cblksty; + tcmpt->csty = cp->tccp.csty; - if (!(prc->incltree = jpc_tagtree_create(prc->numhcblks, - prc->numvcblks))) { - goto error; - } - if (!(prc->nlibtree = jpc_tagtree_create(prc->numhcblks, - prc->numvcblks))) { - goto error; - } - if (!(prc->savincltree = jpc_tagtree_create(prc->numhcblks, - prc->numvcblks))) { - goto error; - } - if (!(prc->savnlibtree = jpc_tagtree_create(prc->numhcblks, - prc->numvcblks))) { - goto error; - } + tcmpt->numstepsizes = tcmpt->numbands; + assert(tcmpt->numstepsizes <= JPC_MAXBANDS); + memset(tcmpt->stepsizes, 0, + sizeof(tcmpt->numstepsizes * sizeof(uint_fast16_t))); - prc->cblks = jas_malloc(prc->numcblks * sizeof(jpc_enc_cblk_t)); + /* Retrieve information about the various bands. */ + jpc_tsfb_getbands(tcmpt->tsfb, jas_seq2d_xstart(tcmpt->data), + jas_seq2d_ystart(tcmpt->data), + jas_seq2d_xend(tcmpt->data), + jas_seq2d_yend(tcmpt->data), bandinfos); - if (!prc->cblks) + tcmpt->rlvls = jas_malloc(tcmpt->numrlvls * sizeof(jpc_enc_rlvl_t)); + if (!tcmpt->rlvls) + goto error; + + for (rlvlno = 0, rlvl = tcmpt->rlvls; rlvlno < tcmpt->numrlvls; + ++rlvlno, ++rlvl) { + rlvl->bands = 0; + rlvl->tcmpt = tcmpt; + } + for (rlvlno = 0, rlvl = tcmpt->rlvls; rlvlno < tcmpt->numrlvls; + ++rlvlno, ++rlvl) { + if (!rlvl_create(rlvl, cp, tcmpt, bandinfos)) { goto error; - for (cblkno = 0, cblk = prc->cblks; - cblkno < prc->numcblks; - ++cblkno, ++cblk) { - cblk->passes = 0; - cblk->stream = 0; - cblk->mqenc = 0; - cblk->data = 0; - cblk->flags = 0; - cblk->prc = prc; - } - for (cblkno = 0, cblk = prc->cblks; - cblkno < prc->numcblks; - ++cblkno, ++cblk) { - if (!cblk_create(cblk, cp, prc)) { - goto error; - } } - } else { - /* The precinct does not contain any code blocks. */ - prc->tlx = prc->brx; - prc->tly = prc->bry; - prc->numcblks = 0; - prc->numhcblks = 0; - prc->numvcblks = 0; - prc->cblks = 0; - prc->incltree = 0; - prc->nlibtree = 0; - prc->savincltree = 0; - prc->savnlibtree = 0; } - return prc; + return tcmpt; error: - prc_destroy(prc); + + tcmpt_destroy(tcmpt); return 0; + } static void -prc_destroy(jpc_enc_prc_t *prc) { +tcmpt_destroy(jpc_enc_tcmpt_t *tcmpt) { - jpc_enc_cblk_t *cblk; - uint_fast32_t cblkno; + jpc_enc_rlvl_t *rlvl; + uint_fast16_t rlvlno; - if (prc->cblks) { - for (cblkno = 0, cblk = prc->cblks; cblkno < prc->numcblks; - ++cblkno, ++cblk) { - cblk_destroy(cblk); + if (tcmpt->rlvls) { + for (rlvlno = 0, rlvl = tcmpt->rlvls; rlvlno < tcmpt->numrlvls; + ++rlvlno, ++rlvl) { + rlvl_destroy(rlvl); } - jas_free(prc->cblks); - } - if (prc->incltree) { - jpc_tagtree_destroy(prc->incltree); - } - if (prc->nlibtree) { - jpc_tagtree_destroy(prc->nlibtree); + jas_free(tcmpt->rlvls); } - if (prc->savincltree) { - jpc_tagtree_destroy(prc->savincltree); + + if (tcmpt->data) { + jas_seq2d_destroy(tcmpt->data); } - if (prc->savnlibtree) { - jpc_tagtree_destroy(prc->savnlibtree); + if (tcmpt->tsfb) { + jpc_tsfb_destroy(tcmpt->tsfb); } } -static jpc_enc_cblk_t * -cblk_create(jpc_enc_cblk_t *cblk, jpc_enc_cp_t *cp, jpc_enc_prc_t *prc) { +static jpc_enc_rlvl_t * +rlvl_create(jpc_enc_rlvl_t *rlvl, jpc_enc_cp_t *cp, + jpc_enc_tcmpt_t *tcmpt, jpc_tsfb_band_t *bandinfos) { + uint_fast16_t rlvlno; + uint_fast32_t tlprctlx; + uint_fast32_t tlprctly; + uint_fast32_t brprcbrx; + uint_fast32_t brprcbry; + uint_fast16_t bandno; jpc_enc_band_t *band; - uint_fast32_t cblktlx; - uint_fast32_t cblktly; - uint_fast32_t cblkbrx; - uint_fast32_t cblkbry; - jpc_enc_rlvl_t *rlvl; - uint_fast32_t cblkxind; - uint_fast32_t cblkyind; - uint_fast32_t cblkno; - uint_fast32_t tlcblktlx; - uint_fast32_t tlcblktly; - cblkno = cblk - prc->cblks; - cblkxind = cblkno % prc->numhcblks; - cblkyind = cblkno / prc->numhcblks; - rlvl = prc->band->rlvl; - cblk->prc = prc; + /* Deduce the resolution level. */ + rlvlno = rlvl - tcmpt->rlvls; - cblk->numpasses = 0; - cblk->passes = 0; - cblk->numencpasses = 0; - cblk->numimsbs = 0; - cblk->numlenbits = 0; - cblk->stream = 0; - cblk->mqenc = 0; - cblk->flags = 0; - cblk->numbps = 0; - cblk->curpass = 0; - cblk->data = 0; - cblk->savedcurpass = 0; - cblk->savednumlenbits = 0; - cblk->savednumencpasses = 0; + /* Initialize members required for error recovery. */ + rlvl->bands = 0; + rlvl->tcmpt = tcmpt; - band = prc->band; - tlcblktlx = JPC_FLOORTOMULTPOW2(prc->tlx, rlvl->cblkwidthexpn); - tlcblktly = JPC_FLOORTOMULTPOW2(prc->tly, rlvl->cblkheightexpn); - cblktlx = - JAS_MAX(tlcblktlx + (cblkxind << rlvl->cblkwidthexpn), prc->tlx); - cblktly = - JAS_MAX(tlcblktly + (cblkyind << rlvl->cblkheightexpn), prc->tly); - cblkbrx = - JAS_MIN(tlcblktlx + ((cblkxind + 1) << rlvl->cblkwidthexpn), prc->brx); - cblkbry = - JAS_MIN(tlcblktly + ((cblkyind + 1) << rlvl->cblkheightexpn), - prc->bry); + /* Compute the coordinates of the top-left and bottom-right + corners of the tile-component at this resolution. */ + rlvl->tlx = + JPC_CEILDIVPOW2(jas_seq2d_xstart(tcmpt->data), tcmpt->numrlvls - + 1 - rlvlno); + rlvl->tly = + JPC_CEILDIVPOW2(jas_seq2d_ystart(tcmpt->data), tcmpt->numrlvls - + 1 - rlvlno); + rlvl->brx = + JPC_CEILDIVPOW2(jas_seq2d_xend(tcmpt->data), tcmpt->numrlvls - + 1 - rlvlno); + rlvl->bry = + JPC_CEILDIVPOW2(jas_seq2d_yend(tcmpt->data), tcmpt->numrlvls - + 1 - rlvlno); - assert(cblktlx < cblkbrx && cblktly < cblkbry); - cblk->data = jas_seq2d_create(0, 0, 0, 0); - if (!cblk->data) + if (rlvl->tlx >= rlvl->brx || rlvl->tly >= rlvl->bry) { + rlvl->numhprcs = 0; + rlvl->numvprcs = 0; + rlvl->numprcs = 0; + return rlvl; + } + + rlvl->numbands = (!rlvlno) ? 1 : 3; + rlvl->prcwidthexpn = cp->tccp.prcwidthexpns[rlvlno]; + rlvl->prcheightexpn = cp->tccp.prcheightexpns[rlvlno]; + if (!rlvlno) { + rlvl->cbgwidthexpn = rlvl->prcwidthexpn; + rlvl->cbgheightexpn = rlvl->prcheightexpn; + } else { + rlvl->cbgwidthexpn = rlvl->prcwidthexpn - 1; + rlvl->cbgheightexpn = rlvl->prcheightexpn - 1; + } + rlvl->cblkwidthexpn = + JAS_MIN(cp->tccp.cblkwidthexpn, rlvl->cbgwidthexpn); + rlvl->cblkheightexpn = + JAS_MIN(cp->tccp.cblkheightexpn, rlvl->cbgheightexpn); + + /* Compute the number of precincts. */ + tlprctlx = JPC_FLOORTOMULTPOW2(rlvl->tlx, rlvl->prcwidthexpn); + tlprctly = JPC_FLOORTOMULTPOW2(rlvl->tly, rlvl->prcheightexpn); + brprcbrx = JPC_CEILTOMULTPOW2(rlvl->brx, rlvl->prcwidthexpn); + brprcbry = JPC_CEILTOMULTPOW2(rlvl->bry, rlvl->prcheightexpn); + rlvl->numhprcs = JPC_FLOORDIVPOW2(brprcbrx - tlprctlx, rlvl->prcwidthexpn); + rlvl->numvprcs = + JPC_FLOORDIVPOW2(brprcbry - tlprctly, rlvl->prcheightexpn); + rlvl->numprcs = rlvl->numhprcs * rlvl->numvprcs; + + rlvl->bands = jas_malloc(rlvl->numbands * sizeof(jpc_enc_band_t)); + if (!rlvl->bands) goto error; - jas_seq2d_bindsub(cblk->data, band->data, - cblktlx, cblktly, cblkbrx, cblkbry); - return cblk; + for (bandno = 0, band = rlvl->bands; bandno < rlvl->numbands; + ++bandno, ++band) { + band->prcs = 0; + band->data = 0; + band->rlvl = rlvl; + } + for (bandno = 0, band = rlvl->bands; bandno < rlvl->numbands; + ++bandno, ++band) { + if (!band_create(band, cp, rlvl, bandinfos)) { + goto error; + } + } + return rlvl; error: - cblk_destroy(cblk); + + rlvl_destroy(rlvl); return 0; } static void -cblk_destroy(jpc_enc_cblk_t *cblk) { +rlvl_destroy(jpc_enc_rlvl_t *rlvl) { - uint_fast16_t passno; - jpc_enc_pass_t *pass; - if (cblk->passes) { - for (passno = 0, pass = cblk->passes; passno < cblk->numpasses; - ++passno, ++pass) { - pass_destroy(pass); + jpc_enc_band_t *band; + uint_fast16_t bandno; + + if (rlvl->bands) { + for (bandno = 0, band = rlvl->bands; bandno < rlvl->numbands; + ++bandno, ++band) { + band_destroy(band); } - jas_free(cblk->passes); - } - if (cblk->stream) { - jas_stream_close(cblk->stream); - } - if (cblk->mqenc) { - jpc_mqenc_destroy(cblk->mqenc); - } - if (cblk->data) { - jas_seq2d_destroy(cblk->data); - } - if (cblk->flags) { - jas_seq2d_destroy(cblk->flags); + jas_free(rlvl->bands); } } -static void -pass_destroy(jpc_enc_pass_t *pass) { +static jpc_enc_band_t * +band_create(jpc_enc_band_t *band, jpc_enc_cp_t *cp, + jpc_enc_rlvl_t *rlvl, jpc_tsfb_band_t *bandinfos) { - /* XXX - need to free resources here */ + uint_fast16_t bandno; + uint_fast16_t gblbandno; + uint_fast16_t rlvlno; + jpc_tsfb_band_t *bandinfo; + jpc_enc_tcmpt_t *tcmpt; + uint_fast32_t prcno; + jpc_enc_prc_t *prc; + + tcmpt = rlvl->tcmpt; + band->data = 0; + band->prcs = 0; + band->rlvl = rlvl; + + /* Deduce the resolution level and band number. */ + rlvlno = rlvl - rlvl->tcmpt->rlvls; + bandno = band - rlvl->bands; + gblbandno = (!rlvlno) ? 0 : (3 * (rlvlno - 1) + bandno + 1); + + bandinfo = &bandinfos[gblbandno]; + + if (bandinfo->xstart != bandinfo->xend && + bandinfo->ystart != bandinfo->yend) { + band->data = jas_seq2d_create(0, 0, 0, 0); + if (!band->data) + goto error; + jas_seq2d_bindsub(band->data, tcmpt->data, bandinfo->locxstart, + bandinfo->locystart, bandinfo->locxend, + bandinfo->locyend); + jas_seq2d_setshift(band->data, bandinfo->xstart, bandinfo->ystart); + } + band->orient = bandinfo->orient; + band->analgain = JPC_NOMINALGAIN(cp->tccp.qmfbid, tcmpt->numrlvls, rlvlno, + band->orient); + band->numbps = 0; + band->absstepsize = 0; + band->stepsize = 0; + band->synweight = bandinfo->synenergywt; + + if (band->data) { + band->prcs = jas_malloc(rlvl->numprcs * sizeof(jpc_enc_prc_t)); + if (!band->prcs) + goto error; + for (prcno = 0, prc = band->prcs; + prcno < rlvl->numprcs; + ++prcno, ++prc) { + prc->cblks = 0; + prc->incltree = 0; + prc->nlibtree = 0; + prc->savincltree = 0; + prc->savnlibtree = 0; + prc->band = band; + } + for (prcno = 0, prc = band->prcs; + prcno < rlvl->numprcs; + ++prcno, ++prc) { + if (!prc_create(prc, cp, band)) { + goto error; + } + } + } + + return band; + +error: + band_destroy(band); + return 0; } -void -jpc_enc_dump(jpc_enc_t *enc) { +static void +band_destroy(jpc_enc_band_t *band) { - jpc_enc_tile_t *tile; - jpc_enc_tcmpt_t *tcmpt; - jpc_enc_rlvl_t *rlvl; - jpc_enc_band_t *band; jpc_enc_prc_t *prc; - jpc_enc_cblk_t *cblk; - uint_fast16_t cmptno; - uint_fast16_t rlvlno; - uint_fast16_t bandno; + jpc_enc_rlvl_t *rlvl; uint_fast32_t prcno; - uint_fast32_t cblkno; - - tile = enc->curtile; - for (cmptno = 0, tcmpt = tile->tcmpts; - cmptno < tile->numtcmpts; - ++cmptno, ++tcmpt) { - fprintf(stderr, " tcmpt %5d %5d %5d %5d\n", - (int)jas_seq2d_xstart(tcmpt->data), - (int)jas_seq2d_ystart(tcmpt->data), - (int)jas_seq2d_xend(tcmpt->data), - (int)jas_seq2d_yend(tcmpt->data)); - for (rlvlno = 0, rlvl = tcmpt->rlvls; - rlvlno < tcmpt->numrlvls; - ++rlvlno, ++rlvl) { - fprintf(stderr, " rlvl %5d %5d %5d %5d\n", - (int)rlvl->tlx, (int)rlvl->tly, - (int)rlvl->brx, (int)rlvl->bry); - for (bandno = 0, band = rlvl->bands; bandno < rlvl->numbands; - ++bandno, ++band) { - if (!band->data) { - continue; - } - fprintf(stderr, " band %5d %5d %5d %5d\n", - (int)jas_seq2d_xstart(band->data), - (int)jas_seq2d_ystart(band->data), - (int)jas_seq2d_xend(band->data), - (int)jas_seq2d_yend(band->data)); - for (prcno = 0, prc = band->prcs; - prcno < rlvl->numprcs; - ++prcno, ++prc) { - fprintf(stderr, " prc %5d %5d %5d %5d (%5d %5d)\n", - (int)prc->tlx, (int)prc->tly, - (int)prc->brx, (int)prc->bry, - (int)(prc->brx - prc->tlx), - (int)(prc->bry - prc->tly)); - if (!prc->cblks) { - continue; - } - for (cblkno = 0, cblk = prc->cblks; cblkno < prc->numcblks; - ++cblkno, ++cblk) { - fprintf(stderr, " cblk %5d %5d %5d %5d\n", - (int)jas_seq2d_xstart(cblk->data), - (int)jas_seq2d_ystart(cblk->data), - (int)jas_seq2d_xend(cblk->data), - (int)jas_seq2d_yend(cblk->data)); - } - } - } + if (band->prcs) { + rlvl = band->rlvl; + for (prcno = 0, prc = band->prcs; prcno < rlvl->numprcs; + ++prcno, ++prc) { + prc_destroy(prc); } + jas_free(band->prcs); + } + if (band->data) { + jas_seq2d_destroy(band->data); } } -static int -jpc_enc_encodemainbody(jpc_enc_t *enc) { +static jpc_enc_prc_t * +prc_create(jpc_enc_prc_t *prc, jpc_enc_cp_t *cp, jpc_enc_band_t *band) { - int tileno; - int i; - jpc_sot_t *sot; - jpc_enc_tcmpt_t *comp; - jpc_enc_tcmpt_t *endcomps; - jpc_enc_band_t *band; - jpc_enc_band_t *endbands; - jpc_enc_rlvl_t *lvl; - int rlvlno; - jpc_qcc_t *qcc; - jpc_cod_t *cod; - int adjust; - int j; - int absbandno; - long tilehdrlen; - long tilelen; - jpc_enc_tile_t *tile; - jpc_enc_cp_t *cp; - double rho; - uint_fast16_t cmptno; - int samestepsizes; - jpc_enc_ccp_t *ccps; - jpc_enc_tccp_t *tccp; - int bandno; - uint_fast32_t x; - uint_fast32_t y; - int mingbits; - int actualnumbps; - jpc_fix_t mxmag; - jpc_fix_t mag; - int numgbits; - const char * error; + uint_fast32_t prcno; + uint_fast32_t prcxind; + uint_fast32_t prcyind; + uint_fast32_t cbgtlx; + uint_fast32_t cbgtly; + uint_fast32_t tlprctlx; + uint_fast32_t tlprctly; + uint_fast32_t tlcbgtlx; + uint_fast32_t tlcbgtly; + uint_fast16_t rlvlno; + jpc_enc_rlvl_t *rlvl; + uint_fast32_t tlcblktlx; + uint_fast32_t tlcblktly; + uint_fast32_t brcblkbrx; + uint_fast32_t brcblkbry; + uint_fast32_t cblkno; + jpc_enc_cblk_t *cblk; + jpc_enc_tcmpt_t *tcmpt; + + prc->cblks = 0; + prc->incltree = 0; + prc->savincltree = 0; + prc->nlibtree = 0; + prc->savnlibtree = 0; + + rlvl = band->rlvl; + tcmpt = rlvl->tcmpt; + rlvlno = rlvl - tcmpt->rlvls; + prcno = prc - band->prcs; + prcxind = prcno % rlvl->numhprcs; + prcyind = prcno / rlvl->numhprcs; + prc->band = band; + + tlprctlx = JPC_FLOORTOMULTPOW2(rlvl->tlx, rlvl->prcwidthexpn); + tlprctly = JPC_FLOORTOMULTPOW2(rlvl->tly, rlvl->prcheightexpn); + + if (!rlvlno) { + tlcbgtlx = tlprctlx; + tlcbgtly = tlprctly; + } else { + tlcbgtlx = JPC_CEILDIVPOW2(tlprctlx, 1); + tlcbgtly = JPC_CEILDIVPOW2(tlprctly, 1); + } - cp = enc->cp; + /* Compute the coordinates of the top-left and bottom-right corners of the + precinct. + */ + cbgtlx = tlcbgtlx + (prcxind << rlvl->cbgwidthexpn); + cbgtly = tlcbgtly + (prcyind << rlvl->cbgheightexpn); + prc->tlx = JAS_MAX(jas_seq2d_xstart(band->data), cbgtlx); + prc->tly = JAS_MAX(jas_seq2d_ystart(band->data), cbgtly); + prc->brx = JAS_MIN(jas_seq2d_xend(band->data), cbgtlx + + (1 << rlvl->cbgwidthexpn)); + prc->bry = JAS_MIN(jas_seq2d_yend(band->data), cbgtly + + (1 << rlvl->cbgheightexpn)); - for (tileno = 0; tileno < cp->numtiles; ++tileno) { - enc->curtile = jpc_enc_tile_create(enc->cp, enc->image, tileno); - if (!enc->curtile) - abort(); + if (prc->tlx < prc->brx && prc->tly < prc->bry) { + /* The precinct contains at least one code block. */ - tile = enc->curtile; + tlcblktlx = JPC_FLOORTOMULTPOW2(prc->tlx, rlvl->cblkwidthexpn); + tlcblktly = JPC_FLOORTOMULTPOW2(prc->tly, rlvl->cblkheightexpn); + brcblkbrx = JPC_CEILTOMULTPOW2(prc->brx, rlvl->cblkwidthexpn); + brcblkbry = JPC_CEILTOMULTPOW2(prc->bry, rlvl->cblkheightexpn); + prc->numhcblks = JPC_FLOORDIVPOW2(brcblkbrx - tlcblktlx, + rlvl->cblkwidthexpn); + prc->numvcblks = JPC_FLOORDIVPOW2(brcblkbry - tlcblktly, + rlvl->cblkheightexpn); + prc->numcblks = prc->numhcblks * prc->numvcblks; - if (jas_getdbglevel() >= 10) { - jpc_enc_dump(enc); + if (!(prc->incltree = jpc_tagtree_create(prc->numhcblks, + prc->numvcblks))) { + goto error; } - - endcomps = &tile->tcmpts[tile->numtcmpts]; - for (cmptno = 0, comp = tile->tcmpts; - cmptno < tile->numtcmpts; - ++cmptno, ++comp) { - if (!cp->ccps[cmptno].sgnd) { - adjust = 1 << (cp->ccps[cmptno].prec - 1); - for (i = 0; i < jas_matrix_numrows(comp->data); ++i) { - for (j = 0; j < jas_matrix_numcols(comp->data); ++j) { - *jas_matrix_getref(comp->data, i, j) -= adjust; - } - } - } + if (!(prc->nlibtree = jpc_tagtree_create(prc->numhcblks, + prc->numvcblks))) { + goto error; + } + if (!(prc->savincltree = jpc_tagtree_create(prc->numhcblks, + prc->numvcblks))) { + goto error; + } + if (!(prc->savnlibtree = jpc_tagtree_create(prc->numhcblks, + prc->numvcblks))) { + goto error; } - if (!tile->intmode) { - endcomps = &tile->tcmpts[tile->numtcmpts]; - for (comp = tile->tcmpts; comp != endcomps; ++comp) { - jas_matrix_asl(comp->data, JPC_FIX_FRACBITS); + prc->cblks = jas_malloc(prc->numcblks * sizeof(jpc_enc_cblk_t)); + + if (!prc->cblks) + goto error; + for (cblkno = 0, cblk = prc->cblks; + cblkno < prc->numcblks; + ++cblkno, ++cblk) { + cblk->passes = 0; + cblk->stream = 0; + cblk->mqenc = 0; + cblk->data = 0; + cblk->flags = 0; + cblk->prc = prc; + } + for (cblkno = 0, cblk = prc->cblks; + cblkno < prc->numcblks; + ++cblkno, ++cblk) { + if (!cblk_create(cblk, cp, prc)) { + goto error; } } + } else { + /* The precinct does not contain any code blocks. */ + prc->tlx = prc->brx; + prc->tly = prc->bry; + prc->numcblks = 0; + prc->numhcblks = 0; + prc->numvcblks = 0; + prc->cblks = 0; + prc->incltree = 0; + prc->nlibtree = 0; + prc->savincltree = 0; + prc->savnlibtree = 0; + } - switch (tile->mctid) { - case JPC_MCT_RCT: - assert(jas_image_numcmpts(enc->image) == 3); - jpc_rct(tile->tcmpts[0].data, tile->tcmpts[1].data, - tile->tcmpts[2].data); - break; - case JPC_MCT_ICT: - assert(jas_image_numcmpts(enc->image) == 3); - jpc_ict(tile->tcmpts[0].data, tile->tcmpts[1].data, - tile->tcmpts[2].data); - break; - default: - break; - } + return prc; - for (i = 0; i < jas_image_numcmpts(enc->image); ++i) { - comp = &tile->tcmpts[i]; - jpc_tsfb_analyze(comp->tsfb, - ((comp->qmfbid == JPC_COX_RFT) ? - JPC_TSFB_RITIMODE : 0), comp->data); +error: + prc_destroy(prc); + return 0; +} - } - endcomps = &tile->tcmpts[tile->numtcmpts]; - for (cmptno = 0, comp = tile->tcmpts; - comp != endcomps; - ++cmptno, ++comp) { - mingbits = 0; - absbandno = 0; - /* All bands must have a corresponding quantizer step size, - even if they contain no samples and are never coded. */ - /* Some bands may not be hit by the loop below, so we must - initialize all of the step sizes to a sane value. */ - memset(comp->stepsizes, 0, sizeof(comp->stepsizes)); - for (rlvlno = 0, lvl = comp->rlvls; - rlvlno < comp->numrlvls; - ++rlvlno, ++lvl) { - if (!lvl->bands) { - absbandno += rlvlno ? 3 : 1; - continue; - } - endbands = &lvl->bands[lvl->numbands]; - for (band = lvl->bands; band != endbands; ++band) { - if (!band->data) { - ++absbandno; - continue; - } - actualnumbps = 0; - mxmag = 0; - for (y = 0; y < jas_matrix_numrows(band->data); ++y) { - for (x = 0; x < jas_matrix_numcols(band->data); ++x) { - mag = abs(jas_matrix_get(band->data, y, x)); - if (mag > mxmag) { - mxmag = mag; - } - } - } - if (tile->intmode) { - actualnumbps = - jpc_firstone(mxmag) + 1; - } else { - actualnumbps = - jpc_firstone(mxmag) + 1 - JPC_FIX_FRACBITS; - } - numgbits = actualnumbps - (cp->ccps[cmptno].prec - 1 + - band->analgain); - if (numgbits > mingbits) { - mingbits = numgbits; - } - if (!tile->intmode) { - band->absstepsize = - jpc_fix_div( - jpc_inttofix(1 << (band->analgain + 1)), - band->synweight); - } else { - band->absstepsize = jpc_inttofix(1); - } - band->stepsize = jpc_abstorelstepsize( - band->absstepsize, cp->ccps[cmptno].prec + - band->analgain); - band->numbps = cp->tccp.numgbits + - JPC_QCX_GETEXPN(band->stepsize) - 1; - if ((!tile->intmode) && band->data) { - quantize(band->data, band->absstepsize); - } +static void +prc_destroy(jpc_enc_prc_t *prc) { - comp->stepsizes[absbandno] = band->stepsize; - ++absbandno; - } - } + jpc_enc_cblk_t *cblk; + uint_fast32_t cblkno; - assert(JPC_FIX_FRACBITS >= JPC_NUMEXTRABITS); - if (!tile->intmode) { - jas_matrix_divpow2(comp->data, - JPC_FIX_FRACBITS - JPC_NUMEXTRABITS); - } else { - jas_matrix_asl(comp->data, JPC_NUMEXTRABITS); - } + if (prc->cblks) { + for (cblkno = 0, cblk = prc->cblks; cblkno < prc->numcblks; + ++cblkno, ++cblk) { + cblk_destroy(cblk); } + jas_free(prc->cblks); + } + if (prc->incltree) { + jpc_tagtree_destroy(prc->incltree); + } + if (prc->nlibtree) { + jpc_tagtree_destroy(prc->nlibtree); + } + if (prc->savincltree) { + jpc_tagtree_destroy(prc->savincltree); + } + if (prc->savnlibtree) { + jpc_tagtree_destroy(prc->savnlibtree); + } +} - if (mingbits > cp->tccp.numgbits) { - fprintf(stderr, "error: too few guard bits (need at least %d)\n", - mingbits); - return -1; - } - enc->tmpstream = jas_stream_memopen(0, 0); - if (!enc->tmpstream) { - fprintf(stderr, "cannot open tmp file\n"); - return -1; - } - /* Write the tile header. */ - enc->mrk = jpc_ms_create(JPC_MS_SOT); - if (!enc->mrk) - return -1; - sot = &enc->mrk->parms.sot; - sot->len = 0; - sot->tileno = tileno; - sot->partno = 0; - sot->numparts = 1; - if (jpc_putms(enc->tmpstream, enc->cstate, enc->mrk)) { - fprintf(stderr, "cannot write SOT marker\n"); - return -1; - } - jpc_ms_destroy(enc->mrk); - enc->mrk = 0; +static jpc_enc_cblk_t * +cblk_create(jpc_enc_cblk_t *cblk, jpc_enc_cp_t *cp, jpc_enc_prc_t *prc) { + + jpc_enc_band_t *band; + uint_fast32_t cblktlx; + uint_fast32_t cblktly; + uint_fast32_t cblkbrx; + uint_fast32_t cblkbry; + jpc_enc_rlvl_t *rlvl; + uint_fast32_t cblkxind; + uint_fast32_t cblkyind; + uint_fast32_t cblkno; + uint_fast32_t tlcblktlx; + uint_fast32_t tlcblktly; + + cblkno = cblk - prc->cblks; + cblkxind = cblkno % prc->numhcblks; + cblkyind = cblkno / prc->numhcblks; + rlvl = prc->band->rlvl; + cblk->prc = prc; + + cblk->numpasses = 0; + cblk->passes = 0; + cblk->numencpasses = 0; + cblk->numimsbs = 0; + cblk->numlenbits = 0; + cblk->stream = 0; + cblk->mqenc = 0; + cblk->flags = 0; + cblk->numbps = 0; + cblk->curpass = 0; + cblk->data = 0; + cblk->savedcurpass = 0; + cblk->savednumlenbits = 0; + cblk->savednumencpasses = 0; + + band = prc->band; + tlcblktlx = JPC_FLOORTOMULTPOW2(prc->tlx, rlvl->cblkwidthexpn); + tlcblktly = JPC_FLOORTOMULTPOW2(prc->tly, rlvl->cblkheightexpn); + cblktlx = + JAS_MAX(tlcblktlx + (cblkxind << rlvl->cblkwidthexpn), prc->tlx); + cblktly = + JAS_MAX(tlcblktly + (cblkyind << rlvl->cblkheightexpn), prc->tly); + cblkbrx = + JAS_MIN(tlcblktlx + ((cblkxind + 1) << rlvl->cblkwidthexpn), prc->brx); + cblkbry = + JAS_MIN(tlcblktly + ((cblkyind + 1) << rlvl->cblkheightexpn), + prc->bry); + + assert(cblktlx < cblkbrx && cblktly < cblkbry); + cblk->data = jas_seq2d_create(0, 0, 0, 0); + if (!cblk->data) + goto error; + jas_seq2d_bindsub(cblk->data, band->data, + cblktlx, cblktly, cblkbrx, cblkbry); -/************************************************************************/ -/************************************************************************/ -/************************************************************************/ + return cblk; - tccp = &cp->tccp; - for (cmptno = 0; cmptno < cp->numcmpts; ++cmptno) { - comp = &tile->tcmpts[cmptno]; - if (comp->numrlvls != tccp->maxrlvls) { - if (!(enc->mrk = jpc_ms_create(JPC_MS_COD))) { - return -1; - } - /* XXX = this is not really correct. we are using comp #0's - precint sizes and other characteristics */ - comp = &tile->tcmpts[0]; - cod = &enc->mrk->parms.cod; - cod->compparms.csty = 0; - cod->compparms.numdlvls = comp->numrlvls - 1; - cod->prg = tile->prg; - cod->numlyrs = tile->numlyrs; - cod->compparms.cblkwidthval = - JPC_COX_CBLKSIZEEXPN(comp->cblkwidthexpn); - cod->compparms.cblkheightval = - JPC_COX_CBLKSIZEEXPN(comp->cblkheightexpn); - cod->compparms.cblksty = comp->cblksty; - cod->compparms.qmfbid = comp->qmfbid; - cod->mctrans = (tile->mctid != JPC_MCT_NONE); - for (i = 0; i < comp->numrlvls; ++i) { - cod->compparms.rlvls[i].parwidthval = - comp->rlvls[i].prcwidthexpn; - cod->compparms.rlvls[i].parheightval = - comp->rlvls[i].prcheightexpn; - } - if (jpc_putms(enc->tmpstream, enc->cstate, enc->mrk)) { - return -1; - } - jpc_ms_destroy(enc->mrk); - enc->mrk = 0; - } - } +error: + cblk_destroy(cblk); + return 0; +} - for (cmptno = 0, comp = tile->tcmpts; - cmptno < cp->numcmpts; - ++cmptno, ++comp) { - ccps = &cp->ccps[cmptno]; - if (ccps->numstepsizes == comp->numstepsizes) { - samestepsizes = 1; - for (bandno = 0; bandno < ccps->numstepsizes; ++bandno) { - if (ccps->stepsizes[bandno] != comp->stepsizes[bandno]) { - samestepsizes = 0; - break; - } - } - } else { - samestepsizes = 0; - } - if (!samestepsizes) { - if (!(enc->mrk = jpc_ms_create(JPC_MS_QCC))) { - return -1; - } - qcc = &enc->mrk->parms.qcc; - qcc->compno = cmptno; - qcc->compparms.numguard = cp->tccp.numgbits; - qcc->compparms.qntsty = (comp->qmfbid == JPC_COX_INS) ? - JPC_QCX_SEQNT : JPC_QCX_NOQNT; - qcc->compparms.numstepsizes = comp->numstepsizes; - qcc->compparms.stepsizes = comp->stepsizes; - if (jpc_putms(enc->tmpstream, enc->cstate, enc->mrk)) { - return -1; - } - qcc->compparms.stepsizes = 0; - jpc_ms_destroy(enc->mrk); - enc->mrk = 0; - } - } - /* Write a SOD marker to indicate the end of the tile header. */ - if (!(enc->mrk = jpc_ms_create(JPC_MS_SOD))) { - return -1; - } - if (jpc_putms(enc->tmpstream, enc->cstate, enc->mrk)) { - fprintf(stderr, "cannot write SOD marker\n"); - return -1; - } - jpc_ms_destroy(enc->mrk); - enc->mrk = 0; - tilehdrlen = jas_stream_getrwcount(enc->tmpstream); -/************************************************************************/ -/************************************************************************/ -/************************************************************************/ +static void +cblk_destroy(jpc_enc_cblk_t *cblk) { - if (jpc_enc_enccblks(enc)) { - abort(); - return -1; + uint_fast16_t passno; + jpc_enc_pass_t *pass; + if (cblk->passes) { + for (passno = 0, pass = cblk->passes; passno < cblk->numpasses; + ++passno, ++pass) { + pass_destroy(pass); } + jas_free(cblk->passes); + } + if (cblk->stream) { + jas_stream_close(cblk->stream); + } + if (cblk->mqenc) { + jpc_mqenc_destroy(cblk->mqenc); + } + if (cblk->data) { + jas_seq2d_destroy(cblk->data); + } + if (cblk->flags) { + jas_seq2d_destroy(cblk->flags); + } +} - cp = enc->cp; - rho = (double) (tile->brx - tile->tlx) * (tile->bry - tile->tly) / - ((cp->refgrdwidth - cp->imgareatlx) * (cp->refgrdheight - - cp->imgareatly)); - tile->rawsize = cp->rawsize * rho; - - computeLayerSizes(enc, tile, cp, rho, tilehdrlen, &error); - if (!error) { - int rc; - performTier2Coding(enc, tile->numlyrs, tile->lyrsizes, &error); - rc = jpc_enc_encodetiledata(enc); - if (rc != 0) - pm_asprintf(&error, "jpc_enc_encodetiledata() failed\n"); - } +static void +pass_destroy(jpc_enc_pass_t *pass) { - if (error) { - fprintf(stderr, "%s\n", error); - pm_strfree(error); - return -1; - } + /* XXX - need to free resources here */ +} - tilelen = jas_stream_tell(enc->tmpstream); - if (jas_stream_seek(enc->tmpstream, 6, SEEK_SET) < 0) { - return -1; - } - jpc_putuint32(enc->tmpstream, tilelen); - if (jas_stream_seek(enc->tmpstream, 0, SEEK_SET) < 0) { - return -1; - } - if (jpc_putdata(enc->out, enc->tmpstream, -1)) { - return -1; - } - enc->len += tilelen; +void +jpc_enc_dump(jpc_enc_t *enc) { - jas_stream_close(enc->tmpstream); - enc->tmpstream = 0; + jpc_enc_tile_t *tile; + jpc_enc_tcmpt_t *tcmpt; + jpc_enc_rlvl_t *rlvl; + jpc_enc_band_t *band; + jpc_enc_prc_t *prc; + jpc_enc_cblk_t *cblk; + uint_fast16_t cmptno; + uint_fast16_t rlvlno; + uint_fast16_t bandno; + uint_fast32_t prcno; + uint_fast32_t cblkno; - jpc_enc_tile_destroy(enc->curtile); - enc->curtile = 0; + tile = enc->curtile; + for (cmptno = 0, tcmpt = tile->tcmpts; + cmptno < tile->numtcmpts; + ++cmptno, ++tcmpt) { + fprintf(stderr, " tcmpt %5d %5d %5d %5d\n", + (int)jas_seq2d_xstart(tcmpt->data), + (int)jas_seq2d_ystart(tcmpt->data), + (int)jas_seq2d_xend(tcmpt->data), + (int)jas_seq2d_yend(tcmpt->data)); + for (rlvlno = 0, rlvl = tcmpt->rlvls; + rlvlno < tcmpt->numrlvls; + ++rlvlno, ++rlvl) { + fprintf(stderr, " rlvl %5d %5d %5d %5d\n", + (int)rlvl->tlx, (int)rlvl->tly, + (int)rlvl->brx, (int)rlvl->bry); + for (bandno = 0, band = rlvl->bands; bandno < rlvl->numbands; + ++bandno, ++band) { + if (!band->data) { + continue; + } + fprintf(stderr, " band %5d %5d %5d %5d\n", + (int)jas_seq2d_xstart(band->data), + (int)jas_seq2d_ystart(band->data), + (int)jas_seq2d_xend(band->data), + (int)jas_seq2d_yend(band->data)); + for (prcno = 0, prc = band->prcs; + prcno < rlvl->numprcs; + ++prcno, ++prc) { + fprintf(stderr, " prc %5d %5d %5d %5d (%5d %5d)\n", + (int)prc->tlx, (int)prc->tly, + (int)prc->brx, (int)prc->bry, + (int)(prc->brx - prc->tlx), + (int)(prc->bry - prc->tly)); + if (!prc->cblks) { + continue; + } + for (cblkno = 0, cblk = prc->cblks; cblkno < prc->numcblks; + ++cblkno, ++cblk) { + fprintf(stderr, " cblk %5d %5d %5d %5d\n", + (int)jas_seq2d_xstart(cblk->data), + (int)jas_seq2d_ystart(cblk->data), + (int)jas_seq2d_xend(cblk->data), + (int)jas_seq2d_yend(cblk->data)); + } + } + } + } } - - return 0; } -- cgit 1.4.1