+
+/* ----------------------------------------------------------------------- */
+
+opj_t1_t* t1_create(opj_common_ptr cinfo) {
+ opj_t1_t *t1 = (opj_t1_t*)opj_malloc(sizeof(opj_t1_t));
+ if(t1) {
+ t1->cinfo = cinfo;
+ /* create MQC and RAW handles */
+ t1->mqc = mqc_create();
+ t1->raw = raw_create();
+ /* initialize the look-up tables of the Tier-1 coder/decoder */
+ t1_init_luts(t1);
+ }
+ return t1;
+}
+
+void t1_destroy(opj_t1_t *t1) {
+ if(t1) {
+ /* destroy MQC and RAW handles */
+ mqc_destroy(t1->mqc);
+ raw_destroy(t1->raw);
+ opj_free(t1);
+ }
+}
+
+void t1_encode_cblks(opj_t1_t *t1, opj_tcd_tile_t *tile, opj_tcp_t *tcp) {
+ int compno, resno, bandno, precno, cblkno;
+ int x, y, i, j, orient;
+
+ tile->distotile = 0; /* fixed_quality */
+
+ for (compno = 0; compno < tile->numcomps; compno++) {
+ opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
+
+ for (resno = 0; resno < tilec->numresolutions; resno++) {
+ opj_tcd_resolution_t *res = &tilec->resolutions[resno];
+
+ for (bandno = 0; bandno < res->numbands; bandno++) {
+ opj_tcd_band_t *band = &res->bands[bandno];
+
+ for (precno = 0; precno < res->pw * res->ph; precno++) {
+ opj_tcd_precinct_t *prc = &band->precincts[precno];
+
+ for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
+ opj_tcd_cblk_t *cblk = &prc->cblks[cblkno];
+
+ if (band->bandno == 0) {
+ x = cblk->x0 - band->x0;
+ y = cblk->y0 - band->y0;
+ } else if (band->bandno == 1) {
+ opj_tcd_resolution_t *pres = &tilec->resolutions[resno - 1];
+ x = pres->x1 - pres->x0 + cblk->x0 - band->x0;
+ y = cblk->y0 - band->y0;
+ } else if (band->bandno == 2) {
+ opj_tcd_resolution_t *pres = &tilec->resolutions[resno - 1];
+ x = cblk->x0 - band->x0;
+ y = pres->y1 - pres->y0 + cblk->y0 - band->y0;
+ } else { /* if (band->bandno == 3) */
+ opj_tcd_resolution_t *pres = &tilec->resolutions[resno - 1];
+ x = pres->x1 - pres->x0 + cblk->x0 - band->x0;
+ y = pres->y1 - pres->y0 + cblk->y0 - band->y0;
+ }
+
+ if (tcp->tccps[compno].qmfbid == 1) {
+ for (j = 0; j < cblk->y1 - cblk->y0; j++) {
+ for (i = 0; i < cblk->x1 - cblk->x0; i++) {
+ t1->data[j][i] =
+ tilec->data[(x + i) + (y + j) * (tilec->x1 - tilec->x0)] << T1_NMSEDEC_FRACBITS;
+ }
+ }
+ } 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++) {
+ t1->data[j][i] =
+ fix_mul(
+ tilec->data[x + i + (y + j) * (tilec->x1 - tilec->x0)],
+ 8192 * 8192 / ((int) floor(band->stepsize * 8192))) >> (13 - T1_NMSEDEC_FRACBITS);
+ }
+ }
+ }
+ orient = band->bandno; /* FIXME */
+ if (orient == 2) {
+ orient = 1;
+ } else if (orient == 1) {
+ orient = 2;
+ }
+
+ t1_encode_cblk(t1, cblk, orient, compno, tilec->numresolutions - 1 - resno, tcp->tccps[compno].qmfbid, band->stepsize, tcp->tccps[compno].cblksty, tile->numcomps, tile);
+
+ } /* cblkno */
+ } /* precno */
+ } /* bandno */
+ } /* resno */
+ } /* compno */
+}
+
+void t1_decode_cblks(opj_t1_t *t1, opj_tcd_tile_t *tile, opj_tcp_t *tcp) {
+ int compno, resno, bandno, precno, cblkno;
+
+ for (compno = 0; compno < tile->numcomps; compno++) {
+ opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
+
+ for (resno = 0; resno < tilec->numresolutions; resno++) {
+ opj_tcd_resolution_t *res = &tilec->resolutions[resno];
+
+ for (bandno = 0; bandno < res->numbands; bandno++) {
+ opj_tcd_band_t *band = &res->bands[bandno];
+
+ for (precno = 0; precno < res->pw * res->ph; precno++) {
+ opj_tcd_precinct_t *prc = &band->precincts[precno];
+
+ for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
+ int x, y, i, j, orient;
+ opj_tcd_cblk_t *cblk = &prc->cblks[cblkno];
+
+ orient = band->bandno; /* FIXME */
+ if (orient == 2) {
+ orient = 1;
+ } else if (orient == 1) {
+ orient = 2;
+ }
+
+ t1_decode_cblk(t1, cblk, orient, tcp->tccps[compno].roishift, tcp->tccps[compno].cblksty);
+
+ if (band->bandno == 0) {
+ x = cblk->x0 - band->x0;
+ y = cblk->y0 - band->y0;
+ } else if (band->bandno == 1) {
+ opj_tcd_resolution_t *pres = &tilec->resolutions[resno - 1];
+ x = pres->x1 - pres->x0 + cblk->x0 - band->x0;
+ y = cblk->y0 - band->y0;
+ } else if (band->bandno == 2) {
+ opj_tcd_resolution_t *pres = &tilec->resolutions[resno - 1];
+ x = cblk->x0 - band->x0;
+ y = pres->y1 - pres->y0 + cblk->y0 - band->y0;
+ } else { /* if (band->bandno == 3) */
+ opj_tcd_resolution_t *pres = &tilec->resolutions[resno - 1];
+ x = pres->x1 - pres->x0 + cblk->x0 - band->x0;
+ y = pres->y1 - pres->y0 + cblk->y0 - band->y0;
+ }
+
+ if (tcp->tccps[compno].roishift) {
+ int thresh, val, mag;
+ thresh = 1 << tcp->tccps[compno].roishift;
+ for (j = 0; j < cblk->y1 - cblk->y0; j++) {
+ for (i = 0; i < cblk->x1 - cblk->x0; i++) {
+ val = t1->data[j][i];
+ mag = int_abs(val);
+ if (mag >= thresh) {
+ mag >>= tcp->tccps[compno].roishift;
+ t1->data[j][i] = val < 0 ? -mag : mag;
+ }
+ }
+ }
+ }
+
+ if (tcp->tccps[compno].qmfbid == 1) {
+ for (j = 0; j < cblk->y1 - cblk->y0; j++) {
+ for (i = 0; i < cblk->x1 - cblk->x0; 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)+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++) {
+ double tmp = (double)(t1->data[j][i] * band->stepsize * 4096.0);
+ if (t1->data[j][i] >> 1 == 0) {
+ tilec->data[x + i + (y + j) * (tilec->x1 - tilec->x0)] = 0;
+ } else {
+ int 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);
+ }
+ }
+ }
+ }
+ } /* cblkno */
+ } /* precno */
+ } /* bandno */
+ } /* resno */
+ } /* compno */
+}
+