Subversion Repositories Kolibri OS

Rev

Go to most recent revision | Blame | Last modification | View Log | RSS feed

  1. /*
  2.  * Copyright (c) 2001-2003, David Janssens
  3.  * Copyright (c) 2002-2003, Yannick Verschueren
  4.  * Copyright (c) 2003-2005, Francois Devaux and Antonin Descampe
  5.  * Copyright (c) 2005, Hervé Drolon, FreeImage Team
  6.  * Copyright (c) 2002-2005, Communications and remote sensing Laboratory, Universite catholique de Louvain, Belgium
  7.  * Copyright (c) 2006, Mónica Díez, LPI-UVA, Spain
  8.  * All rights reserved.
  9.  *
  10.  * Redistribution and use in source and binary forms, with or without
  11.  * modification, are permitted provided that the following conditions
  12.  * are met:
  13.  * 1. Redistributions of source code must retain the above copyright
  14.  *    notice, this list of conditions and the following disclaimer.
  15.  * 2. Redistributions in binary form must reproduce the above copyright
  16.  *    notice, this list of conditions and the following disclaimer in the
  17.  *    documentation and/or other materials provided with the distribution.
  18.  *
  19.  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
  20.  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  21.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  22.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  23.  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  24.  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  25.  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  26.  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  27.  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  28.  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  29.  * POSSIBILITY OF SUCH DAMAGE.
  30.  */
  31.  
  32. #include "opj_includes.h"
  33.  
  34. void tcd_dump(FILE *fd, opj_tcd_t *tcd, opj_tcd_volume_t * vol) {
  35.         int tileno, compno, resno, bandno, precno, cblkno;
  36.  
  37.         fprintf(fd, "volume {\n");
  38.         fprintf(fd, "  tw=%d, th=%d, tl=%d, x0=%d x1=%d y0=%d y1=%d z0=%d z1=%d\n",
  39.                 vol->tw, vol->th, vol->tl, tcd->volume->x0, tcd->volume->x1, tcd->volume->y0, tcd->volume->y1, tcd->volume->z0, tcd->volume->z1);
  40.  
  41.         for (tileno = 0; tileno < vol->th * vol->tw * vol->tl; tileno++) {
  42.                 opj_tcd_tile_t *tile = &tcd->tcd_volume->tiles[tileno];
  43.                 fprintf(fd, "  tile {\n");
  44.                 fprintf(fd, "    x0=%d, y0=%d, z0=%d, x1=%d, y1=%d, z1=%d, numcomps=%d\n",
  45.                         tile->x0, tile->y0, tile->z0, tile->x1, tile->y1, tile->z1, tile->numcomps);
  46.                 for (compno = 0; compno < tile->numcomps; compno++) {
  47.                         opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
  48.                         fprintf(fd, "    tilecomp %d {\n",compno);
  49.                         fprintf(fd,     "     x0=%d, y0=%d, z0=%d, x1=%d, y1=%d, z1=%d, numresx=%d, numresy=%d, numresz=%d\n",
  50.                                 tilec->x0, tilec->y0, tilec->z0, tilec->x1, tilec->y1, tilec->z1, tilec->numresolution[0], tilec->numresolution[1], tilec->numresolution[2]);
  51.                         for (resno = 0; resno < tilec->numresolution[0]; resno++) {
  52.                                 opj_tcd_resolution_t *res = &tilec->resolutions[resno];
  53.                                 fprintf(fd, "     res %d{\n",resno);
  54.                                 fprintf(fd,"      x0=%d, y0=%d, z0=%d, x1=%d, y1=%d, z1=%d, pw=%d, ph=%d, pl=%d, numbands=%d\n",
  55.                                         res->x0, res->y0, res->z0, res->x1, res->y1, res->z1, res->prctno[0], res->prctno[1], res->prctno[2], res->numbands);
  56.                                 for (bandno = 0; bandno < res->numbands; bandno++) {
  57.                                         opj_tcd_band_t *band = &res->bands[bandno];
  58.                                         fprintf(fd, "       band %d{\n", bandno);
  59.                                         fprintf(fd,     "                x0=%d, y0=%d, z0=%d, x1=%d, y1=%d, z1=%d, stepsize=%f, numbps=%d\n",
  60.                                                 band->x0, band->y0, band->z0, band->x1, band->y1, band->z1, band->stepsize, band->numbps);
  61.                                         for (precno = 0; precno < (res->prctno[0] * res->prctno[1] * res->prctno[2]); precno++) {
  62.                                                 opj_tcd_precinct_t *prec = &band->precincts[precno];
  63.                                                 fprintf(fd, "             prec %d{\n",precno);
  64.                                                 fprintf(fd,     "                  x0=%d, y0=%d, z0=%d, x1=%d, y1=%d, z1=%d, cw=%d, ch=%d, cl=%d,\n",
  65.                                                         prec->x0, prec->y0, prec->z0, prec->x1, prec->y1, prec->z1, prec->cblkno[0], prec->cblkno[1], prec->cblkno[2]);
  66.                                                 for (cblkno = 0; cblkno < (prec->cblkno[0] * prec->cblkno[1] * prec->cblkno[2]); cblkno++) {
  67.                                                         opj_tcd_cblk_t *cblk = &prec->cblks[cblkno];
  68.                                                         fprintf(fd, "               cblk %d{\n",cblkno);
  69.                                                         fprintf(fd,     "                    x0=%d, y0=%d, z0=%d, x1=%d, y1=%d, z1=%d\n", cblk->x0, cblk->y0, cblk->z0, cblk->x1, cblk->y1, cblk->z1);
  70.                                                         fprintf(fd, "            }\n");
  71.                                                 }
  72.                                                 fprintf(fd, "          }\n");
  73.                                         }
  74.                                         fprintf(fd, "        }\n");
  75.                                 }
  76.                                 fprintf(fd, "      }\n");
  77.                         }
  78.                         fprintf(fd, "    }\n");
  79.                 }
  80.                 fprintf(fd, "  }\n");
  81.         }
  82.         fprintf(fd, "}\n");
  83. }
  84.  
  85. void tilec_dump(FILE *fd, opj_tcd_tilecomp_t *tilec) {
  86.  
  87.         int i=0,k;
  88.         int datalen;
  89.         int *a;
  90.  
  91.         fprintf(fd, "    tilecomp{\n");
  92.         fprintf(fd,     "     x0=%d, y0=%d, z0=%d, x1=%d, y1=%d, z1=%d, numresx=%d, numresy=%d, numresz=%d\n",
  93.                 tilec->x0, tilec->y0, tilec->z0, tilec->x1, tilec->y1, tilec->z1, tilec->numresolution[0], tilec->numresolution[1], tilec->numresolution[2]);
  94.         fprintf(fd, "     data {\n");
  95.         datalen = (tilec->z1 - tilec->z0) * (tilec->y1 - tilec->y0) * (tilec->x1 - tilec->x0);
  96.         a = tilec->data;
  97.         for (k = 0; k < datalen; k++) {
  98.                 if (!(k % tilec->x1)){
  99.                         fprintf(fd, "\n");
  100.                 }
  101.                 if (!(k % (tilec->y1 * tilec->x1))){
  102.                         fprintf(fd, "Slice %d\n",i++);
  103.                 }
  104.                 fprintf(fd," %d",a[k]);
  105.                
  106.                
  107.         }                      
  108.         fprintf(fd, "     }\n");
  109.         /*i=0;
  110.         fprintf(fd, "Slice %d\n");
  111.         if (tilec->prediction->prederr) {
  112.                 fprintf(fd, "     prederror {\n");
  113.                 a = tilec->prediction->prederr;
  114.                 for (k = 0; k < datalen; k++) {
  115.                         fprintf(fd," %d",*(a++));
  116.                         if (!(k % (tilec->y1 - tilec->y0) * (tilec->x1 - tilec->x0))){
  117.                                 fprintf(fd, "\n");fprintf(fd, "Slice %d\n",i++);
  118.                         }
  119.                         if (!(k % (tilec->x1 - tilec->x0))){
  120.                                 fprintf(fd, "\n");
  121.                         }
  122.                 }
  123.         }
  124.         fprintf(fd, "     }\n");*/
  125.         fprintf(fd, "}\n");
  126. }
  127.  
  128. /* ----------------------------------------------------------------------- */
  129.  
  130. /**
  131. Create a new TCD handle
  132. */
  133. opj_tcd_t* tcd_create(opj_common_ptr cinfo) {
  134.         /* create the tcd structure */
  135.         opj_tcd_t *tcd = (opj_tcd_t*)opj_malloc(sizeof(opj_tcd_t));
  136.         if(!tcd) return NULL;
  137.         tcd->cinfo = cinfo;
  138.         tcd->tcd_volume = (opj_tcd_volume_t*)opj_malloc(sizeof(opj_tcd_volume_t));
  139.         if(!tcd->tcd_volume) {
  140.                 opj_free(tcd);
  141.                 return NULL;
  142.         }
  143.  
  144.         return tcd;
  145. }
  146.  
  147. /**
  148. Destroy a previously created TCD handle
  149. */
  150. void tcd_destroy(opj_tcd_t *tcd) {
  151.         if(tcd) {
  152.                 opj_free(tcd->tcd_volume);
  153.                 opj_free(tcd);
  154.         }
  155. }
  156.  
  157. /* ----------------------------------------------------------------------- */
  158. void tcd_malloc_encode(opj_tcd_t *tcd, opj_volume_t * volume, opj_cp_t * cp, int curtileno) {
  159.         int compno, resno, bandno, precno, cblkno, i, j;//, k;
  160.  
  161.         opj_tcd_tile_t          *tile = NULL;           /* pointer to tcd->tile */
  162.         opj_tcd_tilecomp_t      *tilec = NULL;          /* pointer to tcd->tilec */
  163.         opj_tcd_resolution_t    *res = NULL;            /* pointer to tcd->res */
  164.         opj_tcd_band_t          *band = NULL;           /* pointer to tcd->band */
  165.         opj_tcd_precinct_t      *prc = NULL;            /* pointer to tcd->prc */
  166.         opj_tcd_cblk_t          *cblk = NULL;           /* pointer to tcd->cblk */
  167.         opj_tcp_t               *tcp = &cp->tcps[curtileno];
  168.         int p,q,r;
  169.  
  170.         tcd->volume = volume;
  171.         tcd->cp = cp;
  172.         tcd->tcd_volume->tw = cp->tw;
  173.         tcd->tcd_volume->th = cp->th;
  174.         tcd->tcd_volume->tl = cp->tl;
  175.         tcd->tcd_volume->tiles = (opj_tcd_tile_t *) opj_malloc(sizeof(opj_tcd_tile_t));
  176.         tcd->tile = tcd->tcd_volume->tiles;
  177.         tile = tcd->tile;
  178.        
  179.  
  180.         /* p61 ISO/IEC IS15444-1 : 2002 */
  181.         /* curtileno --> raster scanned index of tiles */
  182.         /* p,q,r --> matricial index of tiles */
  183.         p = curtileno % cp->tw;
  184.         q = curtileno / cp->tw;
  185.         r = curtileno / (cp->tw * cp->th); /* extension to 3-D */
  186.  
  187.         /* 4 borders of the tile rescale on the volume if necessary (B.3)*/
  188.         tile->x0 = int_max(cp->tx0 + p * cp->tdx, volume->x0);
  189.         tile->y0 = int_max(cp->ty0 + q * cp->tdy, volume->y0);
  190.         tile->z0 = int_max(cp->tz0 + r * cp->tdz, volume->z0);
  191.         tile->x1 = int_min(cp->tx0 + (p + 1) * cp->tdx, volume->x1);
  192.         tile->y1 = int_min(cp->ty0 + (q + 1) * cp->tdy, volume->y1);
  193.         tile->z1 = int_min(cp->tz0 + (r + 1) * cp->tdz, volume->z1);
  194.         tile->numcomps = volume->numcomps;
  195.  
  196.         /* Modification of the RATE >> */
  197.         for (j = 0; j < tcp->numlayers; j++) {
  198.                 if (tcp->rates[j] <= 1) {
  199.                         tcp->rates[j] = 0;
  200.                 } else {
  201.                         float num = (float) (tile->numcomps * (tile->x1 - tile->x0) * (tile->y1 - tile->y0) * (tile->z1 - tile->z0) * volume->comps[0].prec);
  202.                         float den = (float) (8 * volume->comps[0].dx * volume->comps[0].dy * volume->comps[0].dz);
  203.                         den = tcp->rates[j] * den;
  204.                         tcp->rates[j] = (num + den - 1) / den;
  205.                 }
  206.                 /*tcp->rates[j] = tcp->rates[j] ? int_ceildiv(
  207.                         tile->numcomps * (tile->x1 - tile->x0) * (tile->y1 - tile->y0) * (tile->z1 - tile->z0) * volume->comps[0].prec,
  208.             (tcp->rates[j] * 8 * volume->comps[0].dx * volume->comps[0].dy * volume->comps[0].dz)) : 0;*/
  209.                 if (tcp->rates[j]) {
  210.                         if (j && tcp->rates[j] < tcp->rates[j - 1] + 10) {
  211.                                 tcp->rates[j] = tcp->rates[j - 1] + 20;
  212.                         } else if (!j && tcp->rates[j] < 30){
  213.                                 tcp->rates[j] = 30;
  214.                         }
  215.                 }
  216.         }
  217.         /* << Modification of the RATE */
  218.  
  219.         tile->comps = (opj_tcd_tilecomp_t *) opj_malloc(volume->numcomps * sizeof(opj_tcd_tilecomp_t));
  220.         for (compno = 0; compno < tile->numcomps; compno++) {
  221.                 opj_tccp_t *tccp = &tcp->tccps[compno];
  222.                 int res_max;
  223.                 int prevnumbands = 0;
  224.  
  225.                 /* opj_tcd_tilecomp_t *tilec=&tile->comps[compno]; */
  226.                 tcd->tilec = &tile->comps[compno];
  227.                 tilec = tcd->tilec;
  228.  
  229.                 /* border of each tile component (global) (B.3) */
  230.                 tilec->x0 = int_ceildiv(tile->x0, volume->comps[compno].dx);
  231.                 tilec->y0 = int_ceildiv(tile->y0, volume->comps[compno].dy);
  232.                 tilec->z0 = int_ceildiv(tile->z0, volume->comps[compno].dz);
  233.                 tilec->x1 = int_ceildiv(tile->x1, volume->comps[compno].dx);
  234.                 tilec->y1 = int_ceildiv(tile->y1, volume->comps[compno].dy);
  235.                 tilec->z1 = int_ceildiv(tile->z1, volume->comps[compno].dz);
  236.  
  237.                 tilec->data = (int *) opj_malloc((tilec->x1 - tilec->x0) * (tilec->y1 - tilec->y0) * (tilec->z1 - tilec->z0) * sizeof(int));
  238.                
  239.                 res_max = 0;
  240.                 for (i = 0;i < 3; i++){
  241.                         tilec->numresolution[i] = tccp->numresolution[i];
  242.                         //Greater of 3 resolutions contains all information
  243.                         res_max = (tilec->numresolution[i] > res_max) ? tilec->numresolution[i] : res_max;
  244.                 }
  245.                
  246.  
  247.                 tilec->resolutions = (opj_tcd_resolution_t *) opj_malloc(res_max * sizeof(opj_tcd_resolution_t));
  248.                 for (resno = 0; resno < res_max; resno++) {
  249.                        
  250.                         int pdx, pdy, pdz;
  251.                         int tlprcxstart, tlprcystart, tlprczstart;
  252.                         int brprcxend, brprcyend, brprczend;
  253.                         int tlcbgxstart, tlcbgystart, tlcbgzstart;
  254.                         int brcbgxend, brcbgyend, brcbgzend;
  255.                         int cbgwidthexpn, cbgheightexpn, cbglengthexpn;
  256.                         int cblkwidthexpn, cblkheightexpn, cblklengthexpn;
  257.  
  258.                         int diff = tccp->numresolution[0] - tccp->numresolution[2];
  259.                         int levelnox = tilec->numresolution[0] - 1 - resno;
  260.                         int levelnoy = tilec->numresolution[1] - 1 - resno;
  261.                         int levelnoz = tilec->numresolution[2] - 1 - ((resno <= diff) ? 0 : (resno - diff));
  262.                                 if (levelnoz < 0) levelnoz = 0;
  263.  
  264.                         /* opj_tcd_resolution_t *res=&tilec->resolutions[resno]; */
  265.                         tcd->res = &tilec->resolutions[resno];
  266.                         res = tcd->res;
  267.                        
  268.                         /* border for each resolution level (global) (B.14)*/
  269.                         res->x0 = int_ceildivpow2(tilec->x0, levelnox);
  270.                         res->y0 = int_ceildivpow2(tilec->y0, levelnoy);
  271.                         res->z0 = int_ceildivpow2(tilec->z0, levelnoz);
  272.                         res->x1 = int_ceildivpow2(tilec->x1, levelnox);
  273.                         res->y1 = int_ceildivpow2(tilec->y1, levelnoy);
  274.                         res->z1 = int_ceildivpow2(tilec->z1, levelnoz);
  275.                         //if (res->z1 < 0)fprintf(stdout,"Res: %d       %d/%d --> %d\n",resno,tilec->z1, levelnoz, int_ceildivpow2(tilec->z1, levelnoz));
  276.  
  277.                         res->numbands = (resno == 0) ? 1 : (resno <= diff) ? 3 : 7; /* --> 3D */
  278.  
  279.                         /* p. 30, table A-13, ISO/IEC IS154444-1 : 2002 */
  280.                         if (tccp->csty & J3D_CCP_CSTY_PRT) {
  281.                                 pdx = tccp->prctsiz[0][resno];
  282.                                 pdy = tccp->prctsiz[1][resno];
  283.                                 pdz = tccp->prctsiz[2][resno];
  284.                         } else {
  285.                                 pdx = 15;
  286.                                 pdy = 15;
  287.                                 pdz = 15;
  288.                         }
  289.                        
  290.                         /* p. 66, B.16, ISO/IEC IS15444-1 : 2002  */
  291.                         tlprcxstart = int_floordivpow2(res->x0, pdx) << pdx;
  292.                         tlprcystart = int_floordivpow2(res->y0, pdy) << pdy;
  293.                         tlprczstart = int_floordivpow2(res->z0, pdz) << pdz;
  294.                         brprcxend = int_ceildivpow2(res->x1, pdx) << pdx;
  295.                         brprcyend = int_ceildivpow2(res->y1, pdy) << pdy;
  296.                         brprczend = int_ceildivpow2(res->z1, pdz) << pdz;
  297.                        
  298.                         res->prctno[0] = (brprcxend - tlprcxstart) >> pdx;
  299.                         res->prctno[1] = (brprcyend - tlprcystart) >> pdy;
  300.                         res->prctno[2] = (brprczend - tlprczstart) >> pdz;
  301.                                 if (res->prctno[2] == 0) res->prctno[2] = 1;
  302.                                
  303.                         /* p. 67, B.17 & B.18, ISO/IEC IS15444-1 : 2002  */
  304.                         if (resno == 0) {
  305.                                 tlcbgxstart = tlprcxstart;
  306.                                 tlcbgystart = tlprcystart;
  307.                                 tlcbgzstart = tlprczstart;
  308.                                 brcbgxend = brprcxend;
  309.                                 brcbgyend = brprcyend;
  310.                                 brcbgzend = brprczend;
  311.                                 cbgwidthexpn = pdx;
  312.                                 cbgheightexpn = pdy;
  313.                                 cbglengthexpn = pdz;
  314.                         } else {
  315.                                 tlcbgxstart = int_ceildivpow2(tlprcxstart, 1);
  316.                                 tlcbgystart = int_ceildivpow2(tlprcystart, 1);
  317.                                 tlcbgzstart = int_ceildivpow2(tlprczstart, 1);
  318.                                 brcbgxend = int_ceildivpow2(brprcxend, 1);
  319.                                 brcbgyend = int_ceildivpow2(brprcyend, 1);
  320.                                 brcbgzend = int_ceildivpow2(brprczend, 1);
  321.                                 cbgwidthexpn = pdx - 1;
  322.                                 cbgheightexpn = pdy - 1;
  323.                                 cbglengthexpn = pdz - 1;
  324.                         }
  325.                        
  326.                         cblkwidthexpn = int_min(tccp->cblk[0], cbgwidthexpn); //6
  327.                         cblkheightexpn = int_min(tccp->cblk[1], cbgheightexpn); //6
  328.                         cblklengthexpn = int_min(tccp->cblk[2], cbglengthexpn); //6
  329.                        
  330.                         res->bands = (opj_tcd_band_t *) opj_malloc(res->numbands * sizeof(opj_tcd_band_t));
  331.                         for (bandno = 0; bandno < res->numbands; bandno++) {
  332.                                 int x0b, y0b, z0b, i;
  333.                                 int gain, numbps;
  334.                                 opj_stepsize_t *ss = NULL;
  335.  
  336.                                 tcd->band = &res->bands[bandno];
  337.                                 band = tcd->band;
  338.  
  339.                                 band->bandno = (resno == 0) ? 0 : bandno + 1;
  340.                                 /* Bandno:      0 - LLL         2 - LHL
  341.                                                         1 - HLL         3 - HHL
  342.                                                         4 - LLH         6 - LHH
  343.                                                         5 - HLH         7 - HHH         */
  344.                                 x0b = (band->bandno == 1) || (band->bandno == 3) || (band->bandno == 5 ) || (band->bandno == 7 ) ? 1 : 0;
  345.                                 y0b = (band->bandno == 2) || (band->bandno == 3) || (band->bandno == 6 ) || (band->bandno == 7 ) ? 1 : 0;
  346.                                 z0b = (band->bandno == 4) || (band->bandno == 5) || (band->bandno == 6 ) || (band->bandno == 7 ) ? 1 : 0;
  347.                                
  348.                                 /* p. 65, B.15, ISO/IEC IS15444-1 : 2002  */
  349.                                 if (band->bandno == 0) {
  350.                                         /* band border (global) */
  351.                                         band->x0 = int_ceildivpow2(tilec->x0, levelnox);
  352.                                         band->y0 = int_ceildivpow2(tilec->y0, levelnoy);
  353.                                         band->z0 = int_ceildivpow2(tilec->z0, levelnoz);
  354.                                         band->x1 = int_ceildivpow2(tilec->x1, levelnox);
  355.                                         band->y1 = int_ceildivpow2(tilec->y1, levelnoy);
  356.                                         band->z1 = int_ceildivpow2(tilec->z1, levelnoz);
  357.                                 } else {
  358.                                         band->x0 = int_ceildivpow2(tilec->x0 - (1 << levelnox) * x0b, levelnox + 1);
  359.                                         band->y0 = int_ceildivpow2(tilec->y0 - (1 << levelnoy) * y0b, levelnoy + 1);
  360.                                         band->z0 = int_ceildivpow2(tilec->z0 - (1 << levelnoz) * z0b, (resno <= diff) ? levelnoz : levelnoz + 1);
  361.                                         band->x1 = int_ceildivpow2(tilec->x1 - (1 << levelnox) * x0b, levelnox + 1);
  362.                                         band->y1 = int_ceildivpow2(tilec->y1 - (1 << levelnoy) * y0b, levelnoy + 1);
  363.                                         band->z1 = int_ceildivpow2(tilec->z1 - (1 << levelnoz) * z0b, (resno <= diff) ? levelnoz : levelnoz + 1);
  364.                                 }
  365.                                
  366.                                 ss = &tccp->stepsizes[(resno == 0) ? 0 : (prevnumbands + bandno + 1)];
  367.                                 if (bandno == (res->numbands - 1))
  368.                                         prevnumbands += (resno == 0) ? 0 : res->numbands;
  369.                                 gain = dwt_getgain(band->bandno,tccp->reversible);                                     
  370.                                 numbps = volume->comps[compno].prec + gain;
  371.                                 band->stepsize = (float)((1.0 + ss->mant / 2048.0) * pow(2.0, numbps - ss->expn));
  372.                                 band->numbps = ss->expn + tccp->numgbits - 1;   /* WHY -1 ? */
  373.                                
  374.                                 band->precincts = (opj_tcd_precinct_t *) opj_malloc((res->prctno[0] * res->prctno[1] * res->prctno[2]) * sizeof(opj_tcd_precinct_t));
  375.                                
  376.                                 for (i = 0; i < (res->prctno[0] * res->prctno[1] * res->prctno[2]); i++) {
  377.                                         band->precincts[i].imsbtree = NULL;
  378.                                         band->precincts[i].incltree = NULL;
  379.                                 }
  380.  
  381.                                 for (precno = 0; precno < (res->prctno[0] * res->prctno[1] * res->prctno[2]); precno++) {
  382.                                         int tlcblkxstart, tlcblkystart, tlcblkzstart, brcblkxend, brcblkyend, brcblkzend;
  383.                                         int cbgxstart, cbgystart, cbgzstart, cbgxend, cbgyend, cbgzend;
  384.  
  385.                                         cbgxstart = tlcbgxstart + (precno % res->prctno[0]) * (1 << cbgwidthexpn);
  386.                                         cbgystart = tlcbgystart + ((precno % (res->prctno[0] * res->prctno[1])) / res->prctno[0]) * (1 << cbgheightexpn);
  387.                                         cbgzstart = tlcbgzstart + (precno / (res->prctno[0] * res->prctno[1])) * (1 << cbglengthexpn);
  388.                                         cbgxend = cbgxstart + (1 << cbgwidthexpn);
  389.                                         cbgyend = cbgystart + (1 << cbgheightexpn);
  390.                                         cbgzend = cbgzstart + (1 << cbglengthexpn);
  391.                                        
  392.                                         tcd->prc = &band->precincts[precno];
  393.                                         prc = tcd->prc;
  394.  
  395.                                         /* precinct size (global) */
  396.                                         prc->x0 = int_max(cbgxstart, band->x0);
  397.                                         prc->y0 = int_max(cbgystart, band->y0);
  398.                                         prc->z0 = int_max(cbgzstart, band->z0);
  399.                                         prc->x1 = int_min(cbgxend, band->x1);
  400.                                         prc->y1 = int_min(cbgyend, band->y1);
  401.                                         prc->z1 = int_min(cbgzend, band->z1);
  402.                                        
  403.                                         tlcblkxstart = int_floordivpow2(prc->x0, cblkwidthexpn) << cblkwidthexpn;
  404.                                         tlcblkystart = int_floordivpow2(prc->y0, cblkheightexpn) << cblkheightexpn;
  405.                                         tlcblkzstart = int_floordivpow2(prc->z0, cblklengthexpn) << cblklengthexpn;
  406.                                         brcblkxend = int_ceildivpow2(prc->x1, cblkwidthexpn) << cblkwidthexpn;
  407.                                         brcblkyend = int_ceildivpow2(prc->y1, cblkheightexpn) << cblkheightexpn;
  408.                                         brcblkzend = int_ceildivpow2(prc->z1, cblklengthexpn) << cblklengthexpn;
  409.                                         prc->cblkno[0] = (brcblkxend - tlcblkxstart) >> cblkwidthexpn;
  410.                                         prc->cblkno[1] = (brcblkyend - tlcblkystart) >> cblkheightexpn;
  411.                                         prc->cblkno[2] = (brcblkzend - tlcblkzstart) >> cblklengthexpn;
  412.                                         prc->cblkno[2] = (prc->cblkno[2] == 0) ? 1 : prc->cblkno[2];
  413.  
  414.                                         prc->cblks = (opj_tcd_cblk_t *) opj_malloc((prc->cblkno[0] * prc->cblkno[1] * prc->cblkno[2]) * sizeof(opj_tcd_cblk_t));
  415.                                         prc->incltree = tgt_create(prc->cblkno[0], prc->cblkno[1], prc->cblkno[2]);
  416.                                         prc->imsbtree = tgt_create(prc->cblkno[0], prc->cblkno[1], prc->cblkno[2]);
  417.                                         //tgt_tree_dump(stdout,prc->incltree);
  418.                                         for (cblkno = 0; cblkno < (prc->cblkno[0] * prc->cblkno[1] * prc->cblkno[2]); cblkno++) {
  419.                                                 int cblkxstart = tlcblkxstart + (cblkno % prc->cblkno[0]) * (1 << cblkwidthexpn);
  420.                                                 int cblkystart = tlcblkystart + ((cblkno % (prc->cblkno[0] * prc->cblkno[1])) / prc->cblkno[0]) * (1 << cblkheightexpn);
  421.                                                 int cblkzstart = tlcblkzstart + (cblkno / (prc->cblkno[0] * prc->cblkno[1])) * (1 << cblklengthexpn);
  422.                                                 int cblkxend = cblkxstart + (1 << cblkwidthexpn);
  423.                                                 int cblkyend = cblkystart + (1 << cblkheightexpn);
  424.                                                 int cblkzend = cblkzstart + (1 << cblklengthexpn);
  425.                                                 int prec = ((tilec->bpp > 16) ? 3 : ((tilec->bpp > 8) ? 2 : 1));
  426.                                                
  427.                                                 tcd->cblk = &prc->cblks[cblkno];
  428.                                                 cblk = tcd->cblk;
  429.  
  430.                                                 /* code-block size (global) */
  431.                                                 cblk->x0 = int_max(cblkxstart, prc->x0);
  432.                                                 cblk->y0 = int_max(cblkystart, prc->y0);
  433.                                                 cblk->z0 = int_max(cblkzstart, prc->z0);
  434.                                                 cblk->x1 = int_min(cblkxend, prc->x1);
  435.                                                 cblk->y1 = int_min(cblkyend, prc->y1);
  436.                                                 cblk->z1 = int_min(cblkzend, prc->z1);
  437.                                         }                                      
  438.                                 }
  439.                         }
  440.                 }
  441.         }
  442.         //tcd_dump(stdout, tcd, tcd->tcd_volume);
  443.  
  444. }
  445. void tcd_init_encode(opj_tcd_t *tcd, opj_volume_t * volume, opj_cp_t * cp, int curtileno) {
  446.         int compno, resno, bandno, precno, cblkno;
  447.         int j, p, q, r;
  448.  
  449.         opj_tcd_tile_t          *tile = NULL;           /* pointer to tcd->tile */
  450.         opj_tcd_tilecomp_t      *tilec = NULL;          /* pointer to tcd->tilec */
  451.         opj_tcd_resolution_t    *res = NULL;    /* pointer to tcd->res */
  452.         opj_tcd_band_t          *band = NULL;           /* pointer to tcd->band */
  453.         opj_tcd_precinct_t      *prc = NULL;            /* pointer to tcd->prc */
  454.         opj_tcd_cblk_t          *cblk = NULL;           /* pointer to tcd->cblk */
  455.         opj_tcp_t *tcp = &cp->tcps[curtileno];
  456.  
  457.         tcd->tile = tcd->tcd_volume->tiles;
  458.         tile = tcd->tile;
  459.  
  460.         /* p61 ISO/IEC IS15444-1 : 2002 */
  461.         /* curtileno --> raster scanned index of tiles */
  462.         /* p,q,r --> matricial index of tiles */
  463.         p = curtileno % cp->tw;
  464.         q = curtileno / cp->tw;
  465.         r = curtileno / (cp->tw * cp->th); /* extension to 3-D */
  466.        
  467.         /* 4 borders of the tile rescale on the volume if necessary (B.3)*/
  468.         tile->x0 = int_max(cp->tx0 + p * cp->tdx, volume->x0);
  469.         tile->y0 = int_max(cp->ty0 + q * cp->tdy, volume->y0);
  470.         tile->z0 = int_max(cp->tz0 + r * cp->tdz, volume->z0);
  471.         tile->x1 = int_min(cp->tx0 + (p + 1) * cp->tdx, volume->x1);
  472.         tile->y1 = int_min(cp->ty0 + (q + 1) * cp->tdy, volume->y1);
  473.         tile->z1 = int_min(cp->tz0 + (r + 1) * cp->tdz, volume->z1);
  474.         tile->numcomps = volume->numcomps;
  475.  
  476.         /* Modification of the RATE >> */
  477.         for (j = 0; j < tcp->numlayers; j++) {
  478.                 if (tcp->rates[j] <= 1) {
  479.                         tcp->rates[j] = 0;
  480.                 } else {
  481.                         float num = (float) (tile->numcomps * (tile->x1 - tile->x0) * (tile->y1 - tile->y0) * (tile->z1 - tile->z0) * volume->comps[0].prec);
  482.                         float den = (float) (8 * volume->comps[0].dx * volume->comps[0].dy * volume->comps[0].dz);
  483.                         den = tcp->rates[j] * den;
  484.                         tcp->rates[j] = (num + den - 1) / den;
  485.                 }
  486.                 /*tcp->rates[j] = tcp->rates[j] ? int_ceildiv(
  487.                         tile->numcomps * (tile->x1 - tile->x0) * (tile->y1 - tile->y0) * (tile->z1 - tile->z0) * volume->comps[0].prec,
  488.             (tcp->rates[j] * 8 * volume->comps[0].dx * volume->comps[0].dy * volume->comps[0].dz)) : 0;*/
  489.                 if (tcp->rates[j]) {
  490.                         if (j && tcp->rates[j] < tcp->rates[j - 1] + 10) {
  491.                                 tcp->rates[j] = tcp->rates[j - 1] + 20;
  492.                         } else if (!j && tcp->rates[j] < 30){
  493.                                 tcp->rates[j] = 30;
  494.                         }
  495.                 }
  496.         }
  497.         /* << Modification of the RATE */
  498.  
  499.         for (compno = 0; compno < tile->numcomps; compno++) {
  500.                 opj_tccp_t *tccp = &tcp->tccps[compno];
  501.                 int res_max, i;
  502.                 int prevnumbands = 0;
  503.  
  504.                 /* opj_tcd_tilecomp_t *tilec=&tile->comps[compno]; */
  505.                 tcd->tilec = &tile->comps[compno];
  506.                 tilec = tcd->tilec;
  507.  
  508.                 /* border of each tile component (global) (B.3) */
  509.                 tilec->x0 = int_ceildiv(tile->x0, volume->comps[compno].dx);
  510.                 tilec->y0 = int_ceildiv(tile->y0, volume->comps[compno].dy);
  511.                 tilec->z0 = int_ceildiv(tile->z0, volume->comps[compno].dz);
  512.                 tilec->x1 = int_ceildiv(tile->x1, volume->comps[compno].dx);
  513.                 tilec->y1 = int_ceildiv(tile->y1, volume->comps[compno].dy);
  514.                 tilec->z1 = int_ceildiv(tile->z1, volume->comps[compno].dz);
  515.  
  516.                 tilec->data = (int *) opj_malloc((tilec->x1 - tilec->x0) * (tilec->y1 - tilec->y0) * (tilec->z1 - tilec->z0) * sizeof(int));
  517.                
  518.                 res_max = 0;
  519.                 for (i = 0;i < 3; i++){
  520.                         tilec->numresolution[i] = tccp->numresolution[i];
  521.                         //Greater of 3 resolutions contains all information
  522.                         res_max = (tilec->numresolution[i] > res_max) ? tilec->numresolution[i] : res_max;
  523.                 }
  524.  
  525.                 tilec->resolutions = (opj_tcd_resolution_t *) opj_malloc(res_max * sizeof(opj_tcd_resolution_t));
  526.                 for (resno = 0; resno < res_max; resno++) {
  527.                         int pdx, pdy, pdz;
  528.                         int tlprcxstart, tlprcystart, tlprczstart, brprcxend, brprcyend, brprczend;
  529.                         int tlcbgxstart, tlcbgystart, tlcbgzstart, brcbgxend, brcbgyend, brcbgzend;
  530.                         int cbgwidthexpn, cbgheightexpn, cbglengthexpn;
  531.                         int cblkwidthexpn, cblkheightexpn, cblklengthexpn;
  532.                        
  533.                         int levelnox = tilec->numresolution[0] - 1 - resno;
  534.                         int levelnoy = tilec->numresolution[1] - 1 - resno;
  535.                         int diff = tccp->numresolution[0] - tccp->numresolution[2];
  536.                         int levelnoz = tilec->numresolution[2] - 1 - ((resno <= diff) ? 0 : (resno - diff));
  537.                                 if (levelnoz < 0) levelnoz = 0;
  538.  
  539.                         tcd->res = &tilec->resolutions[resno];
  540.                         res = tcd->res;
  541.                        
  542.                         /* border for each resolution level (global) (B.14)*/
  543.                         res->x0 = int_ceildivpow2(tilec->x0, levelnox);
  544.                         res->y0 = int_ceildivpow2(tilec->y0, levelnoy);
  545.                         res->z0 = int_ceildivpow2(tilec->z0, levelnoz);
  546.                         res->x1 = int_ceildivpow2(tilec->x1, levelnox);
  547.                         res->y1 = int_ceildivpow2(tilec->y1, levelnoy);
  548.                         res->z1 = int_ceildivpow2(tilec->z1, levelnoz);
  549.  
  550.                         // res->numbands = resno == 0 ? 1 : 3; /* --> 2D */
  551.                         res->numbands = (resno == 0) ? 1 : (resno <= diff) ? 3 : 7; /* --> 3D */
  552.  
  553.                         /* p. 30, table A-13, ISO/IEC IS154444-1 : 2002 */                     
  554.                         if (tccp->csty & J3D_CCP_CSTY_PRT) {
  555.                                 pdx = tccp->prctsiz[0][resno];
  556.                                 pdy = tccp->prctsiz[1][resno];
  557.                                 pdz = tccp->prctsiz[2][resno];
  558.                         } else {
  559.                                 pdx = 15;
  560.                                 pdy = 15;
  561.                                 pdz = 15;
  562.                         }
  563.                         /* p. 66, B.16, ISO/IEC IS15444-1 : 2002  */
  564.                         tlprcxstart = int_floordivpow2(res->x0, pdx) << pdx;
  565.                         tlprcystart = int_floordivpow2(res->y0, pdy) << pdy;
  566.                         tlprczstart = int_floordivpow2(res->z0, pdz) << pdz;
  567.                         brprcxend = int_ceildivpow2(res->x1, pdx) << pdx;
  568.                         brprcyend = int_ceildivpow2(res->y1, pdy) << pdy;
  569.                         brprczend = int_ceildivpow2(res->z1, pdz) << pdz;
  570.                        
  571.                         res->prctno[0] = (brprcxend - tlprcxstart) >> pdx;
  572.                         res->prctno[1] = (brprcyend - tlprcystart) >> pdy;
  573.                         res->prctno[2] = (brprczend - tlprczstart) >> pdz;
  574.                         if (res->prctno[2] == 0) res->prctno[2] = 1;
  575.  
  576.                         /* p. 67, B.17 & B.18, ISO/IEC IS15444-1 : 2002  */
  577.                         if (resno == 0) {
  578.                                 tlcbgxstart = tlprcxstart;
  579.                                 tlcbgystart = tlprcystart;
  580.                                 tlcbgzstart = tlprczstart;
  581.                                 brcbgxend = brprcxend;
  582.                                 brcbgyend = brprcyend;
  583.                                 brcbgzend = brprczend;
  584.                                 cbgwidthexpn = pdx;
  585.                                 cbgheightexpn = pdy;
  586.                                 cbglengthexpn = pdz;
  587.                         } else {
  588.                                 tlcbgxstart = int_ceildivpow2(tlprcxstart, 1);
  589.                                 tlcbgystart = int_ceildivpow2(tlprcystart, 1);
  590.                                 tlcbgzstart = int_ceildivpow2(tlprczstart, 1);
  591.                                 brcbgxend = int_ceildivpow2(brprcxend, 1);
  592.                                 brcbgyend = int_ceildivpow2(brprcyend, 1);
  593.                                 brcbgzend = int_ceildivpow2(brprczend, 1);
  594.                                 cbgwidthexpn = pdx - 1;
  595.                                 cbgheightexpn = pdy - 1;
  596.                                 cbglengthexpn = pdz - 1;
  597.                         }
  598.                        
  599.                         cblkwidthexpn = int_min(tccp->cblk[0], cbgwidthexpn);
  600.                         cblkheightexpn = int_min(tccp->cblk[1], cbgheightexpn);
  601.                         cblklengthexpn = int_min(tccp->cblk[2], cbglengthexpn);
  602.                        
  603.                         res->bands = (opj_tcd_band_t *) opj_malloc(res->numbands * sizeof(opj_tcd_band_t));
  604.                         for (bandno = 0; bandno < res->numbands; bandno++) {
  605.                                 int x0b, y0b, z0b;
  606.                                 int gain, numbps;
  607.                                 opj_stepsize_t *ss = NULL;
  608.  
  609.                                 tcd->band = &res->bands[bandno];
  610.                                 band = tcd->band;
  611.  
  612.                                 band->bandno = resno == 0 ? 0 : bandno + 1;
  613.                                 /* Bandno:      0 - LLL         2 - LHL
  614.                                                         1 - HLL         3 - HHL
  615.                                                         4 - LLH         6 - LHH
  616.                                                         5 - HLH         7 - HHH         */
  617.                                 x0b = (band->bandno == 1) || (band->bandno == 3) || (band->bandno == 5 ) || (band->bandno == 7 ) ? 1 : 0;
  618.                                 y0b = (band->bandno == 2) || (band->bandno == 3) || (band->bandno == 6 ) || (band->bandno == 7 ) ? 1 : 0;
  619.                                 z0b = (band->bandno == 4) || (band->bandno == 5) || (band->bandno == 6 ) || (band->bandno == 7 ) ? 1 : 0;
  620.                                
  621.                                 /* p. 65, B.15, ISO/IEC IS15444-1 : 2002  */
  622.                                 if (band->bandno == 0) {
  623.                                         /* band border (global) */
  624.                                         band->x0 = int_ceildivpow2(tilec->x0, levelnox);
  625.                                         band->y0 = int_ceildivpow2(tilec->y0, levelnoy);
  626.                                         band->z0 = int_ceildivpow2(tilec->z0, levelnoz);
  627.                                         band->x1 = int_ceildivpow2(tilec->x1, levelnox);
  628.                                         band->y1 = int_ceildivpow2(tilec->y1, levelnoy);
  629.                                         band->z1 = int_ceildivpow2(tilec->z1, levelnoz);
  630.                                 } else {
  631.                                         band->x0 = int_ceildivpow2(tilec->x0 - (1 << levelnox) * x0b, levelnox + 1);
  632.                                         band->y0 = int_ceildivpow2(tilec->y0 - (1 << levelnoy) * y0b, levelnoy + 1);
  633.                                         band->z0 = int_ceildivpow2(tilec->z0 - (1 << levelnoz) * z0b, (resno <= diff) ? levelnoz : levelnoz + 1);
  634.                                         band->x1 = int_ceildivpow2(tilec->x1 - (1 << levelnox) * x0b, levelnox + 1);
  635.                                         band->y1 = int_ceildivpow2(tilec->y1 - (1 << levelnoy) * y0b, levelnoy + 1);
  636.                                         band->z1 = int_ceildivpow2(tilec->z1 - (1 << levelnoz) * z0b, (resno <= diff) ? levelnoz : levelnoz + 1);
  637.                                 }
  638.                                                                
  639.                                 ss = &tccp->stepsizes[(resno == 0) ? 0 : (prevnumbands + bandno + 1)];
  640.                                 if (bandno == (res->numbands - 1))
  641.                                         prevnumbands += (resno == 0) ? 0 : res->numbands;
  642.                                 gain = dwt_getgain(band->bandno,tccp->reversible);                                     
  643.                                 numbps = volume->comps[compno].prec + gain;
  644.                                
  645.                                 band->stepsize = (float)((1.0 + ss->mant / 2048.0) * pow(2.0, numbps - ss->expn));
  646.                                 band->numbps = ss->expn + tccp->numgbits - 1;   /* WHY -1 ? */
  647.                                
  648.                                 for (precno = 0; precno < res->prctno[0] * res->prctno[1] * res->prctno[2]; precno++) {
  649.                                         int tlcblkxstart, tlcblkystart, tlcblkzstart, brcblkxend, brcblkyend, brcblkzend;
  650.  
  651.                                         int cbgxstart = tlcbgxstart + (precno % res->prctno[0]) * (1 << cbgwidthexpn);
  652.                                         int cbgystart = tlcbgystart + ((precno / (res->prctno[0] * res->prctno[1])) / res->prctno[0]) * (1 << cbgheightexpn);
  653.                                         int cbgzstart = tlcbgzstart + (precno / (res->prctno[0] * res->prctno[1])) * (1 << cbglengthexpn);
  654.                                         int cbgxend = cbgxstart + (1 << cbgwidthexpn);
  655.                                         int cbgyend = cbgystart + (1 << cbgheightexpn);
  656.                                         int cbgzend = cbgzstart + (1 << cbglengthexpn);
  657.  
  658.                                         /* opj_tcd_precinct_t *prc=&band->precincts[precno]; */
  659.                                         tcd->prc = &band->precincts[precno];
  660.                                         prc = tcd->prc;
  661.  
  662.                                         /* precinct size (global) */
  663.                                         prc->x0 = int_max(cbgxstart, band->x0);
  664.                                         prc->y0 = int_max(cbgystart, band->y0);
  665.                                         prc->z0 = int_max(cbgzstart, band->z0);
  666.                                         prc->x1 = int_min(cbgxend, band->x1);
  667.                                         prc->y1 = int_min(cbgyend, band->y1);
  668.                                         prc->z1 = int_min(cbgzend, band->z1);
  669.  
  670.                                         tlcblkxstart = int_floordivpow2(prc->x0, cblkwidthexpn) << cblkwidthexpn;
  671.                                         tlcblkystart = int_floordivpow2(prc->y0, cblkheightexpn) << cblkheightexpn;
  672.                                         tlcblkzstart = int_floordivpow2(prc->z0, cblklengthexpn) << cblklengthexpn;
  673.                                         brcblkxend = int_ceildivpow2(prc->x1, cblkwidthexpn) << cblkwidthexpn;
  674.                                         brcblkyend = int_ceildivpow2(prc->y1, cblkheightexpn) << cblkheightexpn;
  675.                                         brcblkzend = int_ceildivpow2(prc->z1, cblklengthexpn) << cblklengthexpn;
  676.                                         prc->cblkno[0] = (brcblkxend - tlcblkxstart) >> cblkwidthexpn;
  677.                                         prc->cblkno[1] = (brcblkyend - tlcblkystart) >> cblkheightexpn;
  678.                                         prc->cblkno[2] = (brcblkzend - tlcblkzstart) >> cblklengthexpn;
  679.                                         prc->cblkno[2] = (prc->cblkno[2] == 0) ? 1 : prc->cblkno[2];
  680.  
  681.                                         opj_free(prc->cblks);
  682.                                         prc->cblks = (opj_tcd_cblk_t *) opj_malloc((prc->cblkno[0] * prc->cblkno[1] * prc->cblkno[2]) * sizeof(opj_tcd_cblk_t));
  683.                                         prc->incltree = tgt_create(prc->cblkno[0], prc->cblkno[1], prc->cblkno[2]);
  684.                                         prc->imsbtree = tgt_create(prc->cblkno[0], prc->cblkno[1], prc->cblkno[2]);
  685.  
  686.                                         for (cblkno = 0; cblkno < (prc->cblkno[0] * prc->cblkno[1] * prc->cblkno[2]); cblkno++) {
  687.                                                         int cblkxstart = tlcblkxstart + (cblkno % prc->cblkno[0]) * (1 << cblkwidthexpn);
  688.                                                         int cblkystart = tlcblkystart + ((cblkno % (prc->cblkno[0] * prc->cblkno[1])) / prc->cblkno[0]) * (1 << cblkheightexpn);
  689.                                                         int cblkzstart = tlcblkzstart + (cblkno / (prc->cblkno[0] * prc->cblkno[1])) * (1 << cblklengthexpn);
  690.                                                         int cblkxend = cblkxstart + (1 << cblkwidthexpn);
  691.                                                         int cblkyend = cblkystart + (1 << cblkheightexpn);
  692.                                                         int cblkzend = cblkzstart + (1 << cblklengthexpn);
  693.                                                         int prec = ((tilec->bpp > 16) ? 3 : ((tilec->bpp > 8) ? 2 : 1));
  694.  
  695.                                                         tcd->cblk = &prc->cblks[cblkno];
  696.                                                         cblk = tcd->cblk;
  697.  
  698.                                                         /* code-block size (global) */
  699.                                                         cblk->x0 = int_max(cblkxstart, prc->x0);
  700.                                                         cblk->y0 = int_max(cblkystart, prc->y0);
  701.                                                         cblk->z0 = int_max(cblkzstart, prc->z0);
  702.                                                         cblk->x1 = int_min(cblkxend, prc->x1);
  703.                                                         cblk->y1 = int_min(cblkyend, prc->y1);
  704.                                                         cblk->z1 = int_min(cblkzend, prc->z1);
  705.                                         }
  706.                                 } /* precno */
  707.                         } /* bandno */
  708.                 } /* resno */
  709.         } /* compno */
  710.         //tcd_dump(stdout, tcd, tcd->tcd_volume);
  711. }
  712.  
  713.  
  714. void tcd_free_encode(opj_tcd_t *tcd) {
  715.         int tileno, compno, resno, bandno, precno;
  716.  
  717.         opj_tcd_tile_t *tile = NULL;            /* pointer to tcd->tile         */
  718. //      opj_tcd_slice_t *slice = NULL;          /* pointer to tcd->slice */
  719.         opj_tcd_tilecomp_t *tilec = NULL;       /* pointer to tcd->tilec        */
  720.         opj_tcd_resolution_t *res = NULL;       /* pointer to tcd->res          */
  721.         opj_tcd_band_t *band = NULL;            /* pointer to tcd->band         */
  722.         opj_tcd_precinct_t *prc = NULL;         /* pointer to tcd->prc          */
  723.  
  724.         for (tileno = 0; tileno < 1; tileno++) {
  725.                 tcd->tile = tcd->tcd_volume->tiles;
  726.                 tile = tcd->tile;
  727.  
  728.                 for (compno = 0; compno < tile->numcomps; compno++) {
  729.                         tcd->tilec = &tile->comps[compno];
  730.                         tilec = tcd->tilec;
  731.  
  732.                         for (resno = 0; resno < tilec->numresolution[0]; resno++) {
  733.                                 tcd->res = &tilec->resolutions[resno];
  734.                                 res = tcd->res;
  735.  
  736.                                 for (bandno = 0; bandno < res->numbands; bandno++) {
  737.                                         tcd->band = &res->bands[bandno];
  738.                                         band = tcd->band;
  739.  
  740.                                         for (precno = 0; precno < res->prctno[0] * res->prctno[1] * res->prctno[2]; precno++) {
  741.                                                 tcd->prc = &band->precincts[precno];
  742.                                                 prc = tcd->prc;
  743.  
  744.                                                 if (prc->incltree != NULL) {
  745.                             tgt_destroy(prc->incltree);
  746.                             prc->incltree = NULL;
  747.                                                 }
  748.                                                 if (prc->imsbtree != NULL) {
  749.                             tgt_destroy(prc->imsbtree);
  750.                             prc->imsbtree = NULL;
  751.                                                 }
  752.                                                 opj_free(prc->cblks);
  753.                                                 prc->cblks = NULL;
  754.                                         } /* for (precno */
  755.                                         opj_free(band->precincts);
  756.                                         band->precincts = NULL;
  757.                                 } /* for (bandno */
  758.                         } /* for (resno */
  759.                         opj_free(tilec->resolutions);
  760.                         tilec->resolutions = NULL;
  761.                 } /* for (compno */
  762.                 opj_free(tile->comps);
  763.                 tile->comps = NULL;
  764.         } /* for (tileno */
  765.         opj_free(tcd->tcd_volume->tiles);
  766.         tcd->tcd_volume->tiles = NULL;
  767. }
  768.  
  769. /* ----------------------------------------------------------------------- */
  770. void tcd_malloc_decode(opj_tcd_t *tcd, opj_volume_t * volume, opj_cp_t * cp) {
  771.         int tileno, compno, resno, bandno, precno, cblkno, res_max,
  772.                 i, j, p, q, r;
  773.         unsigned int x0 = 0, y0 = 0, z0 = 0,
  774.                 x1 = 0, y1 = 0, z1 = 0,
  775.                 w, h, l;
  776.  
  777.         tcd->volume = volume;
  778.         tcd->cp = cp;
  779.         tcd->tcd_volume->tw = cp->tw;
  780.         tcd->tcd_volume->th = cp->th;
  781.         tcd->tcd_volume->tl = cp->tl;
  782.         tcd->tcd_volume->tiles = (opj_tcd_tile_t *) opj_malloc(cp->tw * cp->th * cp->tl * sizeof(opj_tcd_tile_t));
  783.        
  784.         for (i = 0; i < cp->tileno_size; i++) {
  785.                 opj_tcp_t *tcp = &(cp->tcps[cp->tileno[i]]);
  786.                 opj_tcd_tile_t *tile = &(tcd->tcd_volume->tiles[cp->tileno[i]]);
  787.        
  788.                 /* p61 ISO/IEC IS15444-1 : 2002 */
  789.                 /* curtileno --> raster scanned index of tiles */
  790.                 /* p,q,r --> matricial index of tiles */
  791.                 tileno = cp->tileno[i];
  792.                 p = tileno % cp->tw;   
  793.                 q = tileno / cp->tw;   
  794.                 r = tileno / (cp->tw * cp->th); /* extension to 3-D */
  795.  
  796.                 /* 4 borders of the tile rescale on the volume if necessary (B.3)*/
  797.                 tile->x0 = int_max(cp->tx0 + p * cp->tdx, volume->x0);
  798.                 tile->y0 = int_max(cp->ty0 + q * cp->tdy, volume->y0);
  799.                 tile->z0 = int_max(cp->tz0 + r * cp->tdz, volume->z0);
  800.                 tile->x1 = int_min(cp->tx0 + (p + 1) * cp->tdx, volume->x1);
  801.                 tile->y1 = int_min(cp->ty0 + (q + 1) * cp->tdy, volume->y1);
  802.                 tile->z1 = int_min(cp->tz0 + (r + 1) * cp->tdz, volume->z1);
  803.                 tile->numcomps = volume->numcomps;             
  804.                
  805.                 tile->comps = (opj_tcd_tilecomp_t *) opj_malloc(volume->numcomps * sizeof(opj_tcd_tilecomp_t));
  806.                 for (compno = 0; compno < tile->numcomps; compno++) {
  807.                         opj_tccp_t *tccp = &tcp->tccps[compno];
  808.                         opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
  809.                         int prevnumbands = 0;
  810.  
  811.                         /* border of each tile component (global) */
  812.                         tilec->x0 = int_ceildiv(tile->x0, volume->comps[compno].dx);
  813.                         tilec->y0 = int_ceildiv(tile->y0, volume->comps[compno].dy);
  814.                         tilec->z0 = int_ceildiv(tile->z0, volume->comps[compno].dz);
  815.                         tilec->x1 = int_ceildiv(tile->x1, volume->comps[compno].dx);
  816.                         tilec->y1 = int_ceildiv(tile->y1, volume->comps[compno].dy);
  817.                         tilec->z1 = int_ceildiv(tile->z1, volume->comps[compno].dz);
  818.                        
  819.                         tilec->data = (int *) opj_malloc((tilec->x1 - tilec->x0) * (tilec->y1 - tilec->y0) * (tilec->z1 - tilec->z0) * sizeof(int));
  820.  
  821.                         res_max = 0;
  822.                         for (i = 0;i < 3; i++){
  823.                                 tilec->numresolution[i] = tccp->numresolution[i];
  824.                                 //Greater of 3 resolutions contains all information
  825.                                 res_max = (tilec->numresolution[i] > res_max) ? tilec->numresolution[i] : res_max;
  826.                         }
  827.  
  828.                         tilec->resolutions = (opj_tcd_resolution_t *) opj_malloc(res_max * sizeof(opj_tcd_resolution_t));
  829.  
  830.                         for (resno = 0; resno < res_max; resno++) {
  831.                                 opj_tcd_resolution_t *res = &tilec->resolutions[resno];
  832.                                 int pdx, pdy, pdz;
  833.                                 int tlprcxstart, tlprcystart, tlprczstart, brprcxend, brprcyend, brprczend;
  834.                                 int tlcbgxstart, tlcbgystart, tlcbgzstart, brcbgxend, brcbgyend, brcbgzend;
  835.                                 int cbgwidthexpn, cbgheightexpn, cbglengthexpn;
  836.                                 int cblkwidthexpn, cblkheightexpn, cblklengthexpn;
  837.                                 int levelnox = tilec->numresolution[0] - 1 - resno;
  838.                                 int levelnoy = tilec->numresolution[1] - 1 - resno;
  839.                                 int diff = tccp->numresolution[0] - tccp->numresolution[2];
  840.                                 int levelnoz = tilec->numresolution[2] - 1 - ((resno <= diff) ? 0 : (resno - diff));
  841.                                         if (levelnoz < 0) levelnoz = 0;
  842.  
  843.                                 /* border for each resolution level (global) */
  844.                                 res->x0 = int_ceildivpow2(tilec->x0, levelnox);
  845.                                 res->y0 = int_ceildivpow2(tilec->y0, levelnoy);
  846.                                 res->z0 = int_ceildivpow2(tilec->z0, levelnoz);
  847.                                 res->x1 = int_ceildivpow2(tilec->x1, levelnox);
  848.                                 res->y1 = int_ceildivpow2(tilec->y1, levelnoy);
  849.                                 res->z1 = int_ceildivpow2(tilec->z1, levelnoz);
  850.                                 res->numbands = (resno == 0) ? 1 : (resno <= diff) ? 3 : 7; /* --> 3D */
  851.                                
  852.                                 /* p. 30, table A-13, ISO/IEC IS154444-1 : 2002 */
  853.                                 if (tccp->csty & J3D_CCP_CSTY_PRT) {
  854.                                         pdx = tccp->prctsiz[0][resno];
  855.                                         pdy = tccp->prctsiz[1][resno];
  856.                                         pdz = tccp->prctsiz[2][resno];
  857.                                 } else {
  858.                                         pdx = 15;
  859.                                         pdy = 15;
  860.                                         pdz = 15;
  861.                                 }
  862.                                
  863.                                 /* p. 66, B.16, ISO/IEC IS15444-1 : 2002  */
  864.                                 tlprcxstart = int_floordivpow2(res->x0, pdx) << pdx;
  865.                                 tlprcystart = int_floordivpow2(res->y0, pdy) << pdy;
  866.                                 tlprczstart = int_floordivpow2(res->z0, pdz) << pdz;
  867.                                 brprcxend = int_ceildivpow2(res->x1, pdx) << pdx;
  868.                                 brprcyend = int_ceildivpow2(res->y1, pdy) << pdy;
  869.                                 brprczend = int_ceildivpow2(res->z1, pdz) << pdz;
  870.                                
  871.                                 res->prctno[0] = (brprcxend - tlprcxstart) >> pdx;
  872.                                 res->prctno[1] = (brprcyend - tlprcystart) >> pdy;
  873.                                 res->prctno[2] = (brprczend - tlprczstart) >> pdz;
  874.                                
  875.                                 /* p. 67, B.17 & B.18, ISO/IEC IS15444-1 : 2002  */
  876.                                 if (resno == 0) {
  877.                                         tlcbgxstart = tlprcxstart;//0
  878.                                         tlcbgystart = tlprcystart;
  879.                                         tlcbgzstart = tlprczstart;
  880.                                         brcbgxend = brprcxend;//1
  881.                                         brcbgyend = brprcyend;
  882.                                         brcbgzend = brprczend;
  883.                                         cbgwidthexpn = pdx; //15
  884.                                         cbgheightexpn = pdy;
  885.                                         cbglengthexpn = pdz;
  886.                                 } else {
  887.                                         tlcbgxstart = int_ceildivpow2(tlprcxstart, 1);
  888.                                         tlcbgystart = int_ceildivpow2(tlprcystart, 1);
  889.                                         tlcbgzstart = int_ceildivpow2(tlprczstart, 1);
  890.                                         brcbgxend = int_ceildivpow2(brprcxend, 1);
  891.                                         brcbgyend = int_ceildivpow2(brprcyend, 1);
  892.                                         brcbgzend = int_ceildivpow2(brprczend, 1);
  893.                                         cbgwidthexpn = pdx - 1;
  894.                                         cbgheightexpn = pdy - 1;
  895.                                         cbglengthexpn = pdz - 1;
  896.                                 }
  897.                                
  898.                                 cblkwidthexpn = int_min(tccp->cblk[0], cbgwidthexpn); //6
  899.                                 cblkheightexpn = int_min(tccp->cblk[1], cbgheightexpn); //6
  900.                                 cblklengthexpn = int_min(tccp->cblk[2], cbglengthexpn); //6
  901.  
  902.                                 res->bands = (opj_tcd_band_t *) opj_malloc(res->numbands * sizeof(opj_tcd_band_t));
  903.                                 for (bandno = 0; bandno < res->numbands; bandno++) {
  904.                                         int x0b, y0b, z0b;
  905.                                         int gain, numbps;
  906.                                         opj_stepsize_t *ss = NULL;
  907.  
  908.                                         opj_tcd_band_t *band = &res->bands[bandno];
  909.                                         band->bandno = resno == 0 ? 0 : bandno + 1;
  910.                                         /* Bandno:      0 - LLL         2 - LHL
  911.                                                                 1 - HLL         3 - HHL
  912.                                                                 4 - LLH         6 - LHH
  913.                                                                 5 - HLH         7 - HHH         */
  914.                                         x0b = (band->bandno == 1) || (band->bandno == 3) || (band->bandno == 5 ) || (band->bandno == 7 ) ? 1 : 0;
  915.                                         y0b = (band->bandno == 2) || (band->bandno == 3) || (band->bandno == 6 ) || (band->bandno == 7 ) ? 1 : 0;
  916.                                         z0b = (band->bandno == 4) || (band->bandno == 5) || (band->bandno == 6 ) || (band->bandno == 7 ) ? 1 : 0;
  917.                                        
  918.                                         /* p. 65, B.15, ISO/IEC IS15444-1 : 2002  */
  919.                                         if (band->bandno == 0) {
  920.                                                 /* band border (global) */
  921.                                                 band->x0 = int_ceildivpow2(tilec->x0, levelnox);
  922.                                                 band->y0 = int_ceildivpow2(tilec->y0, levelnoy);
  923.                                                 band->z0 = int_ceildivpow2(tilec->z0, levelnoz);
  924.                                                 band->x1 = int_ceildivpow2(tilec->x1, levelnox);
  925.                                                 band->y1 = int_ceildivpow2(tilec->y1, levelnoy);
  926.                                                 band->z1 = int_ceildivpow2(tilec->z1, levelnoz);
  927.                                         } else {
  928.                                                 band->x0 = int_ceildivpow2(tilec->x0 - (1 << levelnox) * x0b, levelnox + 1);
  929.                                                 band->y0 = int_ceildivpow2(tilec->y0 - (1 << levelnoy) * y0b, levelnoy + 1);
  930.                                                 band->z0 = int_ceildivpow2(tilec->z0 - (1 << levelnoz) * z0b, (resno <= diff) ? levelnoz : levelnoz + 1);
  931.                                                 band->x1 = int_ceildivpow2(tilec->x1 - (1 << levelnox) * x0b, levelnox + 1);
  932.                                                 band->y1 = int_ceildivpow2(tilec->y1 - (1 << levelnoy) * y0b, levelnoy + 1);
  933.                                                 band->z1 = int_ceildivpow2(tilec->z1 - (1 << levelnoz) * z0b, (resno <= diff) ? levelnoz : levelnoz + 1);
  934.                                         }      
  935.  
  936.                                         ss = &tccp->stepsizes[(resno == 0) ? 0 : (prevnumbands + bandno + 1)];
  937.                                         if (bandno == (res->numbands - 1))
  938.                                                 prevnumbands += (resno == 0) ? 0 : res->numbands;
  939.                                         gain = dwt_getgain(band->bandno,tccp->reversible);                                     
  940.                                         numbps = volume->comps[compno].prec + gain;
  941.  
  942.                                         band->stepsize = (float)((1.0 + ss->mant / 2048.0) * pow(2.0, numbps - ss->expn));
  943.                                         band->numbps = ss->expn + tccp->numgbits - 1;   /* WHY -1 ? */
  944.                                        
  945.                                         band->precincts = (opj_tcd_precinct_t *) opj_malloc(res->prctno[0] * res->prctno[1] * res->prctno[2] * sizeof(opj_tcd_precinct_t));
  946.                                        
  947.                                         for (precno = 0; precno < res->prctno[0] * res->prctno[1] * res->prctno[2]; precno++) {
  948.                                                 int tlcblkxstart, tlcblkystart, tlcblkzstart, brcblkxend, brcblkyend, brcblkzend;
  949.  
  950.                                                 int cbgxstart = tlcbgxstart + (precno % res->prctno[0]) * (1 << cbgwidthexpn);
  951.                                                 int cbgystart = tlcbgystart + (precno / res->prctno[0]) * (1 << cbgheightexpn);
  952.                                                 int cbgzstart = tlcbgzstart + (precno / (res->prctno[0] * res->prctno[1])) * (1 << cbglengthexpn);
  953.                                                 int cbgxend = cbgxstart + (1 << cbgwidthexpn);
  954.                                                 int cbgyend = cbgystart + (1 << cbgheightexpn);
  955.                                                 int cbgzend = cbgzstart + (1 << cbglengthexpn);
  956.  
  957.                                                 opj_tcd_precinct_t *prc = &band->precincts[precno];
  958.                                                 /* precinct size (global) */
  959.                                                 prc->x0 = int_max(cbgxstart, band->x0);
  960.                                                 prc->y0 = int_max(cbgystart, band->y0);
  961.                                                 prc->z0 = int_max(cbgzstart, band->z0);
  962.                                                 prc->x1 = int_min(cbgxend, band->x1);
  963.                                                 prc->y1 = int_min(cbgyend, band->y1);
  964.                                                 prc->z1 = int_min(cbgzend, band->z1);
  965.  
  966.                                                 tlcblkxstart = int_floordivpow2(prc->x0, cblkwidthexpn) << cblkwidthexpn;
  967.                                                 tlcblkystart = int_floordivpow2(prc->y0, cblkheightexpn) << cblkheightexpn;
  968.                                                 tlcblkzstart = int_floordivpow2(prc->z0, cblklengthexpn) << cblklengthexpn;
  969.                                                 brcblkxend = int_ceildivpow2(prc->x1, cblkwidthexpn) << cblkwidthexpn;
  970.                                                 brcblkyend = int_ceildivpow2(prc->y1, cblkheightexpn) << cblkheightexpn;
  971.                                                 brcblkzend = int_ceildivpow2(prc->z1, cblklengthexpn) << cblklengthexpn;
  972.                                                 prc->cblkno[0] = (brcblkxend - tlcblkxstart) >> cblkwidthexpn;
  973.                                                 prc->cblkno[1] = (brcblkyend - tlcblkystart) >> cblkheightexpn;
  974.                                                 prc->cblkno[2] = (brcblkzend - tlcblkzstart) >> cblklengthexpn;
  975.                                                 prc->cblkno[2] = (prc->cblkno[2] == 0) ? 1 : prc->cblkno[2];
  976.  
  977.                                                 prc->cblks = (opj_tcd_cblk_t *) opj_malloc((prc->cblkno[0] * prc->cblkno[1] * prc->cblkno[2]) * sizeof(opj_tcd_cblk_t));
  978.                                                 prc->incltree = tgt_create(prc->cblkno[0], prc->cblkno[1], prc->cblkno[2]);
  979.                                                 prc->imsbtree = tgt_create(prc->cblkno[0], prc->cblkno[1], prc->cblkno[2]);
  980.                                                
  981.                                                 for (cblkno = 0; cblkno < prc->cblkno[0] * prc->cblkno[1] * prc->cblkno[2]; cblkno++) {
  982.                                                         int cblkxstart = tlcblkxstart + (cblkno % prc->cblkno[0]) * (1 << cblkwidthexpn);
  983.                                                         int cblkystart = tlcblkystart + ((cblkno % (prc->cblkno[0] * prc->cblkno[1])) / prc->cblkno[0]) * (1 << cblkheightexpn);
  984.                                                         int cblkzstart = tlcblkzstart + (cblkno / (prc->cblkno[0] * prc->cblkno[1])) * (1 << cblklengthexpn);
  985.                                                         int cblkxend = cblkxstart + (1 << cblkwidthexpn);
  986.                                                         int cblkyend = cblkystart + (1 << cblkheightexpn);
  987.                                                         int cblkzend = cblkzstart + (1 << cblklengthexpn);
  988.                                                         int prec = ((tilec->bpp > 16) ? 3 : ((tilec->bpp > 8) ? 2 : 1));
  989.                                                         /* code-block size (global) */
  990.                                                         opj_tcd_cblk_t *cblk = &prc->cblks[cblkno];
  991.                                                        
  992.                                                         /* code-block size (global) */
  993.                                                         cblk->x0 = int_max(cblkxstart, prc->x0);
  994.                                                         cblk->y0 = int_max(cblkystart, prc->y0);
  995.                                                         cblk->z0 = int_max(cblkzstart, prc->z0);
  996.                                                         cblk->x1 = int_min(cblkxend, prc->x1);
  997.                                                         cblk->y1 = int_min(cblkyend, prc->y1);
  998.                                                         cblk->z1 = int_min(cblkzend, prc->z1);
  999.                                                 }
  1000.                                         } /* precno */
  1001.                                 } /* bandno */
  1002.                         } /* resno */
  1003.                 } /* compno */
  1004.         } /* i = 0..cp->tileno_size */
  1005.  
  1006.         //tcd_dump(stdout, tcd, tcd->tcd_volume);
  1007.  
  1008.         /*
  1009.         Allocate place to store the decoded data = final volume
  1010.         Place limited by the tile really present in the codestream
  1011.         */
  1012.        
  1013.         for (i = 0; i < volume->numcomps; i++) {
  1014.                 for (j = 0; j < cp->tileno_size; j++) {
  1015.                         tileno = cp->tileno[j];
  1016.                         x0 = (j == 0) ? tcd->tcd_volume->tiles[tileno].comps[i].x0 : int_min(x0,(unsigned int) tcd->tcd_volume->tiles[tileno].comps[i].x0);
  1017.                         y0 = (j == 0) ? tcd->tcd_volume->tiles[tileno].comps[i].y0 : int_min(y0,(unsigned int) tcd->tcd_volume->tiles[tileno].comps[i].y0);
  1018.                         z0 = (j == 0) ? tcd->tcd_volume->tiles[tileno].comps[i].z0 : int_min(z0,(unsigned int) tcd->tcd_volume->tiles[tileno].comps[i].z0);
  1019.                         x1 = (j == 0) ? tcd->tcd_volume->tiles[tileno].comps[i].x1 : int_max(x1,(unsigned int) tcd->tcd_volume->tiles[tileno].comps[i].x1);
  1020.                         y1 = (j == 0) ? tcd->tcd_volume->tiles[tileno].comps[i].y1 : int_max(y1,(unsigned int) tcd->tcd_volume->tiles[tileno].comps[i].y1);
  1021.                         z1 = (j == 0) ? tcd->tcd_volume->tiles[tileno].comps[i].z1 : int_max(z1,(unsigned int) tcd->tcd_volume->tiles[tileno].comps[i].z1);
  1022.                 }
  1023.                
  1024.                 w = x1 - x0;
  1025.                 h = y1 - y0;
  1026.                 l = z1 - z0;
  1027.                
  1028.                 volume->comps[i].data = (int *) opj_malloc(w * h * l * sizeof(int));
  1029.                 volume->comps[i].w = w;
  1030.                 volume->comps[i].h = h;
  1031.                 volume->comps[i].l = l;
  1032.                 volume->comps[i].x0 = x0;
  1033.                 volume->comps[i].y0 = y0;
  1034.                 volume->comps[i].z0 = z0;
  1035.                 volume->comps[i].bigendian = cp->bigendian;
  1036.         }
  1037. }
  1038.  
  1039. void tcd_free_decode(opj_tcd_t *tcd) {
  1040.         int tileno,compno,resno,bandno,precno;
  1041.  
  1042.         opj_tcd_volume_t *tcd_volume = tcd->tcd_volume;
  1043.        
  1044.         for (tileno = 0; tileno < tcd_volume->tw * tcd_volume->th * tcd_volume->tl; tileno++) {
  1045.                 opj_tcd_tile_t *tile = &tcd_volume->tiles[tileno];
  1046.                 for (compno = 0; compno < tile->numcomps; compno++) {
  1047.                         opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
  1048.                         for (resno = 0; resno < tilec->numresolution[0]; resno++) {
  1049.                                 opj_tcd_resolution_t *res = &tilec->resolutions[resno];
  1050.                                 for (bandno = 0; bandno < res->numbands; bandno++) {
  1051.                                         opj_tcd_band_t *band = &res->bands[bandno];
  1052.                                         for (precno = 0; precno < res->prctno[1] * res->prctno[0] * res->prctno[2]; precno++) {
  1053.                                                 opj_tcd_precinct_t *prec = &band->precincts[precno];
  1054.                                                 if (prec->cblks != NULL) opj_free(prec->cblks);
  1055.                                                 if (prec->imsbtree != NULL) tgt_destroy(prec->imsbtree);
  1056.                         if (prec->incltree != NULL) tgt_destroy(prec->incltree);
  1057.                                                 /*for (treeno = 0; treeno < prec->numtrees; treeno++){
  1058.                             if (prec->imsbtree[treeno] != NULL) tgt_destroy(prec->imsbtree[treeno]);
  1059.                             if (prec->incltree[treeno] != NULL) tgt_destroy(prec->incltree[treeno]);
  1060.                                                 }*/
  1061.                                         }
  1062.                                         if (band->precincts != NULL) opj_free(band->precincts);
  1063.                                 }
  1064.                         }
  1065.                         if (tilec->resolutions != NULL) opj_free(tilec->resolutions);
  1066.                 }
  1067.                 if (tile->comps != NULL) opj_free(tile->comps);
  1068.         }
  1069.  
  1070.         if (tcd_volume->tiles != NULL) opj_free(tcd_volume->tiles);
  1071. }
  1072.  
  1073.  
  1074.  
  1075. /* ----------------------------------------------------------------------- */
  1076. void tcd_makelayer_fixed(opj_tcd_t *tcd, int layno, int final) {
  1077.         int compno, resno, bandno, precno, cblkno;
  1078.         int value;                      /*, matrice[tcd_tcp->numlayers][tcd_tile->comps[0].numresolution[0]][3]; */
  1079.         int matrice[10][10][3];
  1080.         int i, j, k;
  1081.  
  1082.         opj_cp_t *cp = tcd->cp;
  1083.         opj_tcd_tile_t *tcd_tile = tcd->tcd_tile;
  1084.         opj_tcp_t *tcd_tcp = tcd->tcp;
  1085.  
  1086.         /*matrice=(int*)opj_malloc(tcd_tcp->numlayers*tcd_tile->comps[0].numresolution[0]*3*sizeof(int)); */
  1087.        
  1088.         for (compno = 0; compno < tcd_tile->numcomps; compno++) {
  1089.                 opj_tcd_tilecomp_t *tilec = &tcd_tile->comps[compno];
  1090.                 for (i = 0; i < tcd_tcp->numlayers; i++) {
  1091.                         for (j = 0; j < tilec->numresolution[0]; j++) {
  1092.                                 for (k = 0; k < 3; k++) {
  1093.                                         matrice[i][j][k] =
  1094.                                                 (int) (cp->matrice[i * tilec->numresolution[0] * 3 + j * 3 + k]
  1095.                                                 * (float) (tcd->volume->comps[compno].prec / 16.0));
  1096.                                 }
  1097.                         }
  1098.                 }
  1099.        
  1100.                 for (resno = 0; resno < tilec->numresolution[0]; resno++) {
  1101.                         opj_tcd_resolution_t *res = &tilec->resolutions[resno];
  1102.                         for (bandno = 0; bandno < res->numbands; bandno++) {
  1103.                                 opj_tcd_band_t *band = &res->bands[bandno];
  1104.                                 for (precno = 0; precno < res->prctno[0] * res->prctno[1] * res->prctno[2]; precno++) {
  1105.                                         opj_tcd_precinct_t *prc = &band->precincts[precno];
  1106.                                         for (cblkno = 0; cblkno < prc->cblkno[0] * prc->cblkno[1] * prc->cblkno[2]; cblkno++) {
  1107.                                                 opj_tcd_cblk_t *cblk = &prc->cblks[cblkno];
  1108.                                                 opj_tcd_layer_t *layer = &cblk->layers[layno];
  1109.                                                 int n;
  1110.                                                 int imsb = tcd->volume->comps[compno].prec - cblk->numbps;      /* number of bit-plan equal to zero */
  1111.                                                 /* Correction of the matrix of coefficient to include the IMSB information */
  1112.                                                 if (layno == 0) {
  1113.                                                         value = matrice[layno][resno][bandno];
  1114.                                                         if (imsb >= value) {
  1115.                                                                 value = 0;
  1116.                                                         } else {
  1117.                                                                 value -= imsb;
  1118.                                                         }
  1119.                                                 } else {
  1120.                                                         value = matrice[layno][resno][bandno] - matrice[layno - 1][resno][bandno];
  1121.                                                         if (imsb >= matrice[layno - 1][resno][bandno]) {
  1122.                                                                 value -= (imsb - matrice[layno - 1][resno][bandno]);
  1123.                                                                 if (value < 0) {
  1124.                                                                         value = 0;
  1125.                                                                 }
  1126.                                                         }
  1127.                                                 }
  1128.                                                
  1129.                                                 if (layno == 0) {
  1130.                                                         cblk->numpassesinlayers = 0;
  1131.                                                 }
  1132.                                                
  1133.                                                 n = cblk->numpassesinlayers;
  1134.                                                 if (cblk->numpassesinlayers == 0) {
  1135.                                                         if (value != 0) {
  1136.                                                                 n = 3 * value - 2 + cblk->numpassesinlayers;
  1137.                                                         } else {
  1138.                                                                 n = cblk->numpassesinlayers;
  1139.                                                         }
  1140.                                                 } else {
  1141.                                                         n = 3 * value + cblk->numpassesinlayers;
  1142.                                                 }
  1143.                                                
  1144.                                                 layer->numpasses = n - cblk->numpassesinlayers;
  1145.                                                
  1146.                                                 if (!layer->numpasses)
  1147.                                                         continue;
  1148.                                                
  1149.                                                 if (cblk->numpassesinlayers == 0) {
  1150.                                                         layer->len = cblk->passes[n - 1].rate;
  1151.                                                         layer->data = cblk->data;
  1152.                                                 } else {
  1153.                                                         layer->len = cblk->passes[n - 1].rate - cblk->passes[cblk->numpassesinlayers - 1].rate;
  1154.                                                         layer->data = cblk->data + cblk->passes[cblk->numpassesinlayers - 1].rate;
  1155.                                                 }
  1156.                                                 if (final)
  1157.                                                         cblk->numpassesinlayers = n;
  1158.                                         }
  1159.                                 }
  1160.                         }
  1161.                 }
  1162.         }
  1163. }
  1164.  
  1165. void tcd_rateallocate_fixed(opj_tcd_t *tcd) {
  1166.         int layno;
  1167.         for (layno = 0; layno < tcd->tcp->numlayers; layno++) {
  1168.                 tcd_makelayer_fixed(tcd, layno, 1);
  1169.         }
  1170. }
  1171.  
  1172. void tcd_makelayer(opj_tcd_t *tcd, int layno, double thresh, int final) {
  1173.         int compno, resno, bandno, precno, cblkno, passno;
  1174.        
  1175.         opj_tcd_tile_t *tcd_tile = tcd->tcd_tile;
  1176.  
  1177.         tcd_tile->distolayer[layno] = 0;        /* fixed_quality */
  1178.        
  1179.         for (compno = 0; compno < tcd_tile->numcomps; compno++) {
  1180.                 opj_tcd_tilecomp_t *tilec = &tcd_tile->comps[compno];
  1181.                 for (resno = 0; resno < tilec->numresolution[0]; resno++) {
  1182.                         opj_tcd_resolution_t *res = &tilec->resolutions[resno];
  1183.                         for (bandno = 0; bandno < res->numbands; bandno++) {
  1184.                                 opj_tcd_band_t *band = &res->bands[bandno];
  1185.                                 for (precno = 0; precno < res->prctno[0] * res->prctno[1] * res->prctno[2]; precno++) {
  1186.                                         opj_tcd_precinct_t *prc = &band->precincts[precno];
  1187.                                         for (cblkno = 0; cblkno < prc->cblkno[0] * prc->cblkno[1] * prc->cblkno[2]; cblkno++) {
  1188.                                                 opj_tcd_cblk_t *cblk = &prc->cblks[cblkno];
  1189.                                                 opj_tcd_layer_t *layer = &cblk->layers[layno];
  1190.                                                
  1191.                                                 int n;
  1192.                                                 if (layno == 0) {
  1193.                                                         cblk->numpassesinlayers = 0;
  1194.                                                 }
  1195.                                                 n = cblk->numpassesinlayers;
  1196.                                                 for (passno = cblk->numpassesinlayers; passno < cblk->totalpasses; passno++) {
  1197.                                                         int dr;
  1198.                                                         double dd;
  1199.                                                         opj_tcd_pass_t *pass = &cblk->passes[passno];
  1200.                                                         if (n == 0) {
  1201.                                                                 dr = pass->rate;
  1202.                                                                 dd = pass->distortiondec;
  1203.                                                         } else {
  1204.                                                                 dr = pass->rate - cblk->passes[n - 1].rate;
  1205.                                                                 dd = pass->distortiondec - cblk->passes[n - 1].distortiondec;
  1206.                                                         }
  1207.                                                         if (!dr) {
  1208.                                                                 if (dd)
  1209.                                                                         n = passno + 1;
  1210.                                                                 continue;
  1211.                                                         }
  1212.                                                         if (dd / dr >= thresh){
  1213.                                                                 n = passno + 1;
  1214.                                                         }
  1215.                                                 }
  1216.                                                 layer->numpasses = n - cblk->numpassesinlayers;
  1217.                                                
  1218.                                                 if (!layer->numpasses) {
  1219.                                                         layer->disto = 0;
  1220.                                                         continue;
  1221.                                                 }
  1222.                                                 if (cblk->numpassesinlayers == 0) {
  1223.                                                         layer->len = cblk->passes[n - 1].rate;
  1224.                                                         layer->data = cblk->data;
  1225.                                                         layer->disto = cblk->passes[n - 1].distortiondec;
  1226.                                                 } else {
  1227.                                                         layer->len = cblk->passes[n - 1].rate - cblk->passes[cblk->numpassesinlayers - 1].rate;
  1228.                                                         layer->data = cblk->data + cblk->passes[cblk->numpassesinlayers - 1].rate;
  1229.                                                         layer->disto = cblk->passes[n - 1].distortiondec - cblk->passes[cblk->numpassesinlayers - 1].distortiondec;
  1230.                                                 }
  1231.                                                
  1232.                                                 tcd_tile->distolayer[layno] += layer->disto;    /* fixed_quality */
  1233.                                                
  1234.                                                 if (final)
  1235.                                                         cblk->numpassesinlayers = n;
  1236.  
  1237.                                         //      fprintf(stdout,"MakeLayer : %d %f %d %d \n",layer->len, layer->disto, layer->numpasses, n);
  1238.                                         }
  1239.                                 }
  1240.                         }
  1241.                 }
  1242.         }
  1243. }
  1244.  
  1245. bool tcd_rateallocate(opj_tcd_t *tcd, unsigned char *dest, int len, opj_volume_info_t * volume_info) {
  1246.         int compno, resno, bandno, precno, cblkno, passno, layno;
  1247.         double min, max;
  1248.         double cumdisto[100];   /* fixed_quality */
  1249.         const double K = 1;             /* 1.1; // fixed_quality */
  1250.         double maxSE = 0;
  1251.  
  1252.         opj_cp_t *cp = tcd->cp;
  1253.         opj_tcd_tile_t *tcd_tile = tcd->tcd_tile;
  1254.         opj_tcp_t *tcd_tcp = tcd->tcp;
  1255.  
  1256.         min = DBL_MAX;
  1257.         max = 0;
  1258.        
  1259.         tcd_tile->nbpix = 0;            /* fixed_quality */
  1260.        
  1261.         for (compno = 0; compno < tcd_tile->numcomps; compno++) {
  1262.                 opj_tcd_tilecomp_t *tilec = &tcd_tile->comps[compno];
  1263.                 tilec->nbpix = 0;
  1264.                 for (resno = 0; resno < tilec->numresolution[0]; resno++) {
  1265.                         opj_tcd_resolution_t *res = &tilec->resolutions[resno];
  1266.                         for (bandno = 0; bandno < res->numbands; bandno++) {
  1267.                                 opj_tcd_band_t *band = &res->bands[bandno];
  1268.                                 for (precno = 0; precno < res->prctno[0] * res->prctno[1] * res->prctno[2]; precno++) {
  1269.                                         opj_tcd_precinct_t *prc = &band->precincts[precno];
  1270.                                         for (cblkno = 0; cblkno < prc->cblkno[0] * prc->cblkno[1] * prc->cblkno[2]; cblkno++) {
  1271.                                                 opj_tcd_cblk_t *cblk = &prc->cblks[cblkno];
  1272.                                                 for (passno = 0; passno < cblk->totalpasses; passno++) {
  1273.                                                         opj_tcd_pass_t *pass = &cblk->passes[passno];
  1274.                                                         int dr;
  1275.                                                         double dd, rdslope;
  1276.                                                         if (passno == 0) {
  1277.                                                                 dr = pass->rate;
  1278.                                                                 dd = pass->distortiondec;
  1279.                                                         } else {
  1280.                                                                 dr = pass->rate - cblk->passes[passno - 1].rate;
  1281.                                                                 dd = pass->distortiondec - cblk->passes[passno - 1].distortiondec;
  1282.                                                         }
  1283.                                                         if (dr == 0) {
  1284.                                                                 continue;
  1285.                                                         }
  1286.                                                         rdslope = dd / dr;
  1287.                                                         if (rdslope < min) {
  1288.                                                                 min = rdslope;
  1289.                                                         }
  1290.                                                         if (rdslope > max) {
  1291.                                                                 max = rdslope;
  1292.                                                         }
  1293.  
  1294.                                                 } /* passno */
  1295.                                                
  1296.                                                 /* fixed_quality */
  1297.                                                 tcd_tile->nbpix += ((cblk->x1 - cblk->x0) * (cblk->y1 - cblk->y0) * (cblk->z1 - cblk->z0));
  1298.                         tilec->nbpix += ((cblk->x1 - cblk->x0) * (cblk->y1 - cblk->y0) * (cblk->z1 - cblk->z0));
  1299.                                         } /* cbklno */
  1300.                                 } /* precno */
  1301.                         } /* bandno */
  1302.                 } /* resno */
  1303.                
  1304.                 maxSE += (((double)(1 << tcd->volume->comps[compno].prec) - 1.0)
  1305.                         * ((double)(1 << tcd->volume->comps[compno].prec) -1.0))
  1306.                         * ((double)(tilec->nbpix));
  1307.         } /* compno */
  1308.        
  1309.         /* add antonin index */
  1310.         if(volume_info && volume_info->index_on) {
  1311.                 opj_tile_info_t *info_TL = &volume_info->tile[tcd->tcd_tileno];
  1312.                 info_TL->nbpix = tcd_tile->nbpix;
  1313.                 info_TL->distotile = tcd_tile->distotile;
  1314.                 info_TL->thresh = (double *) opj_malloc(tcd_tcp->numlayers * sizeof(double));
  1315.         }
  1316.         /* dda */
  1317.        
  1318.         for (layno = 0; layno < tcd_tcp->numlayers; layno++) {
  1319.                 double lo = min;
  1320.                 double hi = max;
  1321.                 int success = 0;
  1322.                 int maxlen = tcd_tcp->rates[layno] ? int_min(((int) tcd_tcp->rates[layno]), len) : len;
  1323.                 double goodthresh;
  1324.                 double distotarget;             /* fixed_quality */
  1325.                 int i = 0;
  1326.                
  1327.         /* fixed_quality */
  1328.                 distotarget = tcd_tile->distotile - ((K * maxSE) / pow((float)10, tcd_tcp->distoratio[layno] / 10));
  1329.        
  1330.                 if ((tcd_tcp->rates[layno]) || (cp->disto_alloc==0)) {
  1331.                         opj_t2_t *t2 = t2_create(tcd->cinfo, tcd->volume, cp);
  1332.                         int oldl = 0, oldoldl = 0;
  1333.                         for (i = 0; i < 128; i++) {
  1334.                                 double thresh = (lo + hi) / 2;
  1335.                                 int l = 0;
  1336.                                 double distoachieved = 0;       /* fixed_quality -q */
  1337.                        
  1338.                                 tcd_makelayer(tcd, layno, thresh, 0);
  1339.                
  1340.                                 if (cp->fixed_quality) {        /* fixed_quality -q */
  1341.                                         distoachieved = (layno == 0) ? tcd_tile->distolayer[0] : cumdisto[layno - 1] + tcd_tile->distolayer[layno];
  1342.                                         if (distoachieved < distotarget) {
  1343.                                                 hi = thresh;
  1344.                                                 continue;
  1345.                                         }
  1346.                                         lo = thresh;
  1347.                                 } else {                /* disto_alloc -r, fixed_alloc -f */
  1348.                                         l = t2_encode_packets(t2, tcd->tcd_tileno, tcd_tile, layno + 1, dest, maxlen, volume_info);
  1349.                                         //fprintf(stdout, "layno %d i %d len=%d max=%d \n",layno,i,l,maxlen);
  1350.                                         if (l == -999) {
  1351.                                                 lo = thresh;
  1352.                                                 continue;
  1353.                                         } else if (l == oldl && oldl == oldoldl && tcd_tile->distolayer[layno] > 0.0 && i>32)
  1354.                                                 break;
  1355.                                         hi = thresh;
  1356.                                         oldoldl = oldl;
  1357.                                         oldl = l;
  1358.                                 }
  1359.                                 success = 1;
  1360.                                 goodthresh = thresh;
  1361.                         }
  1362.                         t2_destroy(t2);
  1363.                 } else {
  1364.                         success = 1;
  1365.                         goodthresh = min;
  1366.                 }
  1367.                 if (!success) {
  1368.                         return false;
  1369.                 }
  1370.                
  1371.                 if(volume_info && volume_info->index_on) {      /* Threshold for Marcela Index */
  1372.                         volume_info->tile[tcd->tcd_tileno].thresh[layno] = goodthresh;
  1373.                 }
  1374.                 tcd_makelayer(tcd, layno, goodthresh, 1);
  1375.                
  1376.                 /* fixed_quality */
  1377.                 cumdisto[layno] = (layno == 0) ? tcd_tile->distolayer[0] : cumdisto[layno - 1] + tcd_tile->distolayer[layno];  
  1378.         }
  1379.  
  1380.         return true;
  1381. }
  1382.  
  1383. /* ----------------------------------------------------------------------- */
  1384. int tcd_encode_tile(opj_tcd_t *tcd, int tileno, unsigned char *dest, int len, opj_volume_info_t * volume_info) {
  1385.         int compno;
  1386.         int l, i, npck = 0;
  1387.         double encoding_time;
  1388.        
  1389.         opj_tcd_tile_t  *tile = NULL;
  1390.         opj_tcp_t               *tcd_tcp = NULL;
  1391.         opj_cp_t                *cp = NULL;
  1392.  
  1393.         opj_tcp_t               *tcp = &tcd->cp->tcps[0];
  1394.         opj_tccp_t              *tccp = &tcp->tccps[0];
  1395.         opj_volume_t    *volume = tcd->volume;
  1396.         opj_t2_t                *t2 = NULL;             /* T2 component */
  1397.  
  1398.         tcd->tcd_tileno = tileno;                       /* current encoded/decoded tile */
  1399.        
  1400.         tcd->tcd_tile = tcd->tcd_volume->tiles; /* tile information */
  1401.         tile = tcd->tcd_tile;
  1402.        
  1403.         tcd->tcp = &tcd->cp->tcps[tileno];      /* coding/decoding params of tileno */ 
  1404.         tcd_tcp = tcd->tcp;
  1405.        
  1406.         cp = tcd->cp;           /* coding parameters */
  1407.  
  1408.         /* INDEX >> */
  1409.         if(volume_info && volume_info->index_on) {
  1410.                 opj_tcd_tilecomp_t *tilec_idx = &tile->comps[0];        /* based on component 0 */
  1411.                 for (i = 0; i < tilec_idx->numresolution[0]; i++) {
  1412.                         opj_tcd_resolution_t *res_idx = &tilec_idx->resolutions[i];
  1413.  
  1414.                         volume_info->tile[tileno].prctno[0][i] = res_idx->prctno[0];
  1415.                         volume_info->tile[tileno].prctno[1][i] = res_idx->prctno[1];
  1416.                         volume_info->tile[tileno].prctno[2][i] = res_idx->prctno[2];
  1417.  
  1418.                         npck += res_idx->prctno[0] * res_idx->prctno[1] * res_idx->prctno[2];
  1419.  
  1420.                         volume_info->tile[tileno].prctsiz[0][i] = tccp->prctsiz[0][i];
  1421.                         volume_info->tile[tileno].prctsiz[1][i] = tccp->prctsiz[1][i];
  1422.                         volume_info->tile[tileno].prctsiz[2][i] = tccp->prctsiz[2][i];
  1423.                 }
  1424.                 volume_info->tile[tileno].packet = (opj_packet_info_t *) opj_malloc(volume_info->comp * volume_info->layer * npck * sizeof(opj_packet_info_t));
  1425.         }
  1426.         /* << INDEX */
  1427.        
  1428.         /*---------------TILE-------------------*/
  1429.         encoding_time = opj_clock();    /* time needed to encode a tile */
  1430.        
  1431.         for (compno = 0; compno < tile->numcomps; compno++) {
  1432.                 int x, y, z;
  1433.                 opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
  1434.                
  1435.                 int adjust;
  1436.                 int offset_x = int_ceildiv(volume->x0, volume->comps[compno].dx); //ceil(x0 / subsampling_dx)
  1437.                 int offset_y = int_ceildiv(volume->y0, volume->comps[compno].dy);
  1438.                 int offset_z = int_ceildiv(volume->z0, volume->comps[compno].dz);
  1439.                
  1440.                 int tw = tilec->x1 - tilec->x0;
  1441.                 int w = int_ceildiv(volume->x1 - volume->x0, volume->comps[compno].dx);
  1442.                 int th = tilec->y1 - tilec->y0;
  1443.                 int h = int_ceildiv(volume->y1 - volume->y0, volume->comps[compno].dy);
  1444.                 int tl = tilec->z1 - tilec->z0;
  1445.                 int l = int_ceildiv(volume->z1 - volume->z0, volume->comps[compno].dz);
  1446.  
  1447.                
  1448.                
  1449.                 /* extract tile data from volume.comps[0].data to tile.comps[0].data */
  1450.                 //fprintf(stdout,"[INFO] Extract tile data\n");
  1451.                 if (tcd->cp->transform_format == TRF_3D_RLS || tcd->cp->transform_format == TRF_3D_LSE) {
  1452.                         adjust = 0;
  1453.                 } else {
  1454.             adjust = volume->comps[compno].sgnd ? 0 : 1 << (volume->comps[compno].prec - 1); //sign=='+' --> 2^(prec-1)
  1455.                         if (volume->comps[compno].dcoffset != 0){
  1456.                                 adjust += volume->comps[compno].dcoffset;
  1457.                                 fprintf(stdout,"[INFO] DC Offset applied: DCO = %d -> adjust = %d\n",volume->comps[compno].dcoffset,adjust);
  1458.                         }
  1459.                 }              
  1460.  
  1461.                 if (tcd_tcp->tccps[compno].reversible == 1) { //IF perfect reconstruction (DWT.5-3)
  1462.                         for (z = tilec->z0; z < tilec->z1; z++) {
  1463.                                 for (y = tilec->y0; y < tilec->y1; y++) {
  1464.                                         /* start of the src tile scanline */
  1465.                                         int *data = &volume->comps[compno].data[(tilec->x0 - offset_x) + (y - offset_y) * w + (z - offset_z) * w * h];
  1466.                                         /* start of the dst tile scanline */
  1467.                                         int *tile_data = &tilec->data[(y - tilec->y0) * tw + (z - tilec->z0) * tw * th];
  1468.                                         for (x = tilec->x0; x < tilec->x1; x++) {
  1469.                                                 *tile_data++ = *data++ - adjust;
  1470.                                         }
  1471.                                 }
  1472.                         }
  1473.                 } else if (tcd_tcp->tccps[compno].reversible == 0) { //IF not (DWT.9-7)
  1474.                         for (z = tilec->z0; z < tilec->z1; z++) {
  1475.                                 for (y = tilec->y0; y < tilec->y1; y++) {
  1476.                                         /* start of the src tile scanline */
  1477.                                         int *data = &volume->comps[compno].data[(tilec->x0 - offset_x) + (y - offset_y) * w + (z - offset_z) * w * h];
  1478.                                         /* start of the dst tile scanline */
  1479.                                         int *tile_data = &tilec->data[(y - tilec->y0) * tw + (z - tilec->z0) * tw * th];
  1480.                                         for (x = tilec->x0; x < tilec->x1; x++) {
  1481.                                                 *tile_data++ = (*data++ - adjust) << 13;
  1482.                                         }
  1483.                                 }
  1484.                         }
  1485.                 }
  1486.        
  1487.         }
  1488.  
  1489.         /*----------------MCT-------------------*/
  1490.         if (tcd_tcp->mct) {
  1491.                 int samples = (tile->comps[0].x1 - tile->comps[0].x0) * (tile->comps[0].y1 - tile->comps[0].y0) * (tile->comps[0].z1 - tile->comps[0].z0);
  1492.                 fprintf(stdout,"[INFO] Tcd_encode_tile: mct\n");
  1493.                 if (tcd_tcp->tccps[0].reversible == 0) {
  1494.                         mct_encode_real(tile->comps[0].data, tile->comps[1].data, tile->comps[2].data, samples);
  1495.                 } else {
  1496.                         mct_encode(tile->comps[0].data, tile->comps[1].data, tile->comps[2].data, samples);
  1497.                 }
  1498.         }
  1499.         /*----------------TRANSFORM---------------------------------*/
  1500.         fprintf(stdout,"[INFO] Tcd_encode_tile: Transform\n");
  1501.         for (compno = 0; compno < tile->numcomps; compno++) {
  1502.                 opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
  1503.                 dwt_encode(tilec, tcd_tcp->tccps[compno].dwtid);
  1504.         }
  1505.  
  1506.         /*-------------------ENTROPY CODING-----------------------------*/
  1507.         fprintf(stdout,"[INFO] Tcd_encode_tile: Entropy coding\n");
  1508.         if ((cp->encoding_format == ENCOD_2EB)||(cp->encoding_format == ENCOD_3EB))
  1509.         {
  1510.                 if (cp->encoding_format == ENCOD_2EB) {
  1511.                         opj_t1_t *t1 = NULL;
  1512.                         t1 = t1_create(tcd->cinfo);
  1513.                         t1_encode_cblks(t1, tile, tcd_tcp);
  1514.                         t1_destroy(t1);
  1515.                 } else if (cp->encoding_format == ENCOD_3EB) {
  1516.                         opj_t1_3d_t *t1 = NULL;        
  1517.                         t1 = t1_3d_create(tcd->cinfo);
  1518.                         t1_3d_encode_cblks(t1, tile, tcd_tcp);
  1519.                         t1_3d_destroy(t1);     
  1520.                 }
  1521.                 /*-----------RATE-ALLOCATE------------------*/
  1522.                 /* INDEX */
  1523.                 if(volume_info) {
  1524.                         volume_info->index_write = 0;
  1525.                 }
  1526.                 if (cp->disto_alloc || cp->fixed_quality) {    
  1527.                         fprintf(stdout,"[INFO] Tcd_encode_tile: Rate-allocate\n");
  1528.                         tcd_rateallocate(tcd, dest, len, volume_info);                  /* Normal Rate/distortion allocation */
  1529.                 } else {/* fixed_alloc */
  1530.             fprintf(stdout,"[INFO] Tcd_encode_tile: Rate-allocate fixed\n");
  1531.             tcd_rateallocate_fixed(tcd);                                                        /* Fixed layer allocation */
  1532.                 }
  1533.  
  1534.                 /*--------------TIER2------------------*/
  1535.                 /* INDEX */
  1536.                 if(volume_info) {
  1537.                         volume_info->index_write = 1;
  1538.                 }
  1539.                 fprintf(stdout,"[INFO] Tcd_encode_tile: Tier - 2\n");
  1540.         t2 = t2_create(tcd->cinfo, volume, cp);
  1541.                 l = t2_encode_packets(t2, tileno, tile, tcd_tcp->numlayers, dest, len, volume_info);
  1542.         t2_destroy(t2);
  1543.         } else if ((cp->encoding_format == ENCOD_2GR)||(cp->encoding_format == ENCOD_3GR)) {
  1544.                 /*if(volume_info) {
  1545.                         volume_info->index_write = 1;
  1546.                 }
  1547.                 gr = golomb_create(tcd->cinfo, volume, cp);
  1548.                 l = golomb_encode(gr, tileno, tile, dest, len, volume_info);
  1549.                 golomb_destroy(gr);*/
  1550.         }
  1551.  
  1552.        
  1553.         /*---------------CLEAN-------------------*/
  1554.         fprintf(stdout,"[INFO] Tcd_encode_tile: %d bytes coded\n",l);
  1555.         encoding_time = opj_clock() - encoding_time;
  1556.         opj_event_msg(tcd->cinfo, EVT_INFO, "- tile encoded in %f s\n", encoding_time);
  1557.        
  1558.         /* cleaning memory */
  1559.         for (compno = 0; compno < tile->numcomps; compno++) {
  1560.                 tcd->tilec = &tile->comps[compno];
  1561.                 opj_free(tcd->tilec->data);
  1562.         }
  1563.        
  1564.         if (l == -999){
  1565.                 fprintf(stdout,"[ERROR] Unable to perform T2 tier. Return -999.\n");
  1566.                 return 0;
  1567.         }
  1568.  
  1569.         return l;
  1570. }
  1571.  
  1572.  
  1573. bool tcd_decode_tile(opj_tcd_t *tcd, unsigned char *src, int len, int tileno) {
  1574.         int l, i;
  1575.         int compno, eof = 0;
  1576.         double tile_time, t1_time, dwt_time;
  1577.  
  1578.         opj_tcd_tile_t *tile = NULL;
  1579.         opj_t2_t *t2 = NULL;            /* T2 component */
  1580.        
  1581.         tcd->tcd_tileno = tileno;
  1582.         tcd->tcd_tile = &(tcd->tcd_volume->tiles[tileno]);
  1583.         tcd->tcp = &(tcd->cp->tcps[tileno]);
  1584.         tile = tcd->tcd_tile;
  1585.        
  1586.         tile_time = opj_clock();        /* time needed to decode a tile */
  1587.         opj_event_msg(tcd->cinfo, EVT_INFO, "tile %d / %d\n", tileno + 1, tcd->cp->tw * tcd->cp->th * tcd->cp->tl);
  1588.  
  1589.         if ((tcd->cp->encoding_format == ENCOD_2EB) || (tcd->cp->encoding_format == ENCOD_3EB)) {
  1590.                 /*--------------TIER2------------------*/
  1591.                 t2 = t2_create(tcd->cinfo, tcd->volume, tcd->cp);
  1592.                 l = t2_decode_packets(t2, src, len, tileno, tile);
  1593.                 t2_destroy(t2);
  1594.                 opj_event_msg(tcd->cinfo, EVT_INFO, "Tcd_decode_tile: %d bytes decoded\n",l);
  1595.                
  1596.                 if (l == -999) {
  1597.                         eof = 1;
  1598.                         opj_event_msg(tcd->cinfo, EVT_ERROR, "Tcd_decode_tile: incomplete bistream\n");
  1599.                 }
  1600.        
  1601.                 /*------------------TIER1-----------------*/
  1602.                 opj_event_msg(tcd->cinfo, EVT_INFO, "Tcd_decode_tile: Entropy decoding %d \n",tcd->cp->encoding_format);
  1603.                 t1_time = opj_clock();  /* time needed to decode a tile */
  1604.                 if (tcd->cp->encoding_format == ENCOD_2EB) {
  1605.                         opj_t1_t *t1 = NULL;            /* T1 component */
  1606.                         t1 = t1_create(tcd->cinfo);
  1607.                         t1_decode_cblks(t1, tile, tcd->tcp);
  1608.                         t1_destroy(t1);
  1609.                 }else if (tcd->cp->encoding_format == ENCOD_3EB) {
  1610.                         opj_t1_3d_t *t1 = NULL;         /* T1 component */
  1611.                         t1 = t1_3d_create(tcd->cinfo);
  1612.                         t1_3d_decode_cblks(t1, tile, tcd->tcp);
  1613.                         t1_3d_destroy(t1);
  1614.                 }
  1615.  
  1616.                 t1_time = opj_clock() - t1_time;
  1617.                 #ifdef VERBOSE
  1618.                                 opj_event_msg(tcd->cinfo, EVT_INFO, "- tier-1 took %f s\n", t1_time);
  1619.                 #endif
  1620.         } else if ((tcd->cp->encoding_format == ENCOD_2GR)||(tcd->cp->encoding_format == ENCOD_3GR)) {
  1621.                 opj_event_msg(tcd->cinfo, EVT_INFO, "Tcd_decode_tile: Entropy decoding -- Does nothing :-D\n");
  1622.                 /*
  1623.                 gr = golomb_create(tcd->cinfo, tcd->volume, tcd->cp);
  1624.                 l = golomb_decode(gr, tileno, tile, src, len);
  1625.                 golomb_destroy(gr);
  1626.                 if (l == -999) {
  1627.                         eof = 1;
  1628.                         opj_event_msg(tcd->cinfo, EVT_ERROR, "Tcd_decode_tile: incomplete bistream\n");
  1629.                 }
  1630.                 */
  1631.         }
  1632.  
  1633.         /*----------------DWT---------------------*/
  1634.         fprintf(stdout,"[INFO] Tcd_decode_tile: Inverse DWT\n");
  1635.         dwt_time = opj_clock(); /* time needed to decode a tile */
  1636.         for (compno = 0; compno < tile->numcomps; compno++) {
  1637.                 opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
  1638.                 int stops[3], dwtid[3];
  1639.        
  1640.                 for (i = 0; i < 3; i++) {
  1641.                         if (tcd->cp->reduce[i] != 0)
  1642.                                 tcd->volume->comps[compno].resno_decoded[i] = tile->comps[compno].numresolution[i] - tcd->cp->reduce[i] - 1;
  1643.                         stops[i] = tilec->numresolution[i] - 1 - tcd->volume->comps[compno].resno_decoded[i];
  1644.                         if (stops[i] < 0) stops[i]=0;
  1645.                         dwtid[i] = tcd->cp->tcps->tccps[compno].dwtid[i];
  1646.                 }
  1647.                
  1648.                 dwt_decode(tilec, stops, dwtid);
  1649.  
  1650.                 for (i = 0; i < 3; i++) {
  1651.                         if (tile->comps[compno].numresolution[i] > 0) {
  1652.                                 tcd->volume->comps[compno].factor[i] = tile->comps[compno].numresolution[i] - (tcd->volume->comps[compno].resno_decoded[i] + 1);
  1653.                                 if ( (tcd->volume->comps[compno].factor[i]) < 0 )
  1654.                                         tcd->volume->comps[compno].factor[i] = 0;
  1655.                         }
  1656.                 }
  1657.         }
  1658.         dwt_time = opj_clock() - dwt_time;
  1659.         #ifdef VERBOSE
  1660.                         opj_event_msg(tcd->cinfo, EVT_INFO, "- dwt took %f s\n", dwt_time);
  1661.         #endif
  1662.  
  1663.         /*----------------MCT-------------------*/
  1664.        
  1665.         if (tcd->tcp->mct) {
  1666.                 if (tcd->tcp->tccps[0].reversible == 1) {
  1667.                         mct_decode(tile->comps[0].data, tile->comps[1].data, tile->comps[2].data,
  1668.                                 (tile->comps[0].x1 - tile->comps[0].x0) * (tile->comps[0].y1 - tile->comps[0].y0) * (tile->comps[0].z1 - tile->comps[0].z0));
  1669.                 } else {
  1670.                         mct_decode_real(tile->comps[0].data, tile->comps[1].data, tile->comps[2].data,
  1671.                                 (tile->comps[0].x1 - tile->comps[0].x0) * (tile->comps[0].y1 - tile->comps[0].y0)* (tile->comps[0].z1 - tile->comps[0].z0));
  1672.                 }
  1673.         }
  1674.        
  1675.         /*---------------TILE-------------------*/
  1676.        
  1677.         for (compno = 0; compno < tile->numcomps; compno++) {
  1678.                 opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
  1679.                 opj_tcd_resolution_t *res =     &tilec->resolutions[tcd->volume->comps[compno].resno_decoded[0]];
  1680.                 int adjust;
  1681.                 int minval = tcd->volume->comps[compno].sgnd ? -(1 << (tcd->volume->comps[compno].prec - 1)) : 0;
  1682.                 int maxval = tcd->volume->comps[compno].sgnd ? (1 << (tcd->volume->comps[compno].prec - 1)) - 1 : (1 << tcd->volume->comps[compno].prec) - 1;
  1683.                
  1684.                 int tw = tilec->x1 - tilec->x0;
  1685.                 int w = tcd->volume->comps[compno].w;
  1686.                 int th = tilec->y1 - tilec->y0;
  1687.                 int h = tcd->volume->comps[compno].h;
  1688.  
  1689.                 int i, j, k;
  1690.                 int offset_x = int_ceildivpow2(tcd->volume->comps[compno].x0, tcd->volume->comps[compno].factor[0]);
  1691.                 int offset_y = int_ceildivpow2(tcd->volume->comps[compno].y0, tcd->volume->comps[compno].factor[1]);
  1692.                 int offset_z = int_ceildivpow2(tcd->volume->comps[compno].z0, tcd->volume->comps[compno].factor[2]);
  1693.                
  1694.                 if (tcd->cp->transform_format == TRF_3D_RLS || tcd->cp->transform_format == TRF_3D_LSE) {
  1695.                         adjust = 0;
  1696.                 } else {
  1697.             adjust = tcd->volume->comps[compno].sgnd ? 0 : 1 << (tcd->volume->comps[compno].prec - 1); //sign=='+' --> 2^(prec-1)
  1698.                         if (tcd->volume->comps[compno].dcoffset != 0){
  1699.                                 adjust += tcd->volume->comps[compno].dcoffset;
  1700.                                 fprintf(stdout,"[INFO] DC Offset applied: DCO = %d -> adjust = %d\n",tcd->volume->comps[compno].dcoffset,adjust);
  1701.                         }
  1702.                 }
  1703.  
  1704.                 for (k = res->z0; k < res->z1; k++) {
  1705.                         for (j = res->y0; j < res->y1; j++) {
  1706.                                 for (i = res->x0; i < res->x1; i++) {
  1707.                                         int v;
  1708.                                         float tmp = (float)((tilec->data[i - res->x0 + (j - res->y0) * tw + (k - res->z0) * tw * th]) / 8192.0);
  1709.  
  1710.                                         if (tcd->tcp->tccps[compno].reversible == 1) {
  1711.                                                 v = tilec->data[i - res->x0 + (j - res->y0) * tw + (k - res->z0) * tw * th];
  1712.                                         } else {
  1713.                                                 int tmp2 = ((int) (floor(fabs(tmp)))) + ((int) floor(fabs(tmp*2))%2);
  1714.                                                 v = ((tmp < 0) ? -tmp2:tmp2);
  1715.                                         }
  1716.                                         v += adjust;
  1717.                                        
  1718.                                         tcd->volume->comps[compno].data[(i - offset_x) + (j - offset_y) * w + (k - offset_z) * w * h] = int_clamp(v, minval, maxval);
  1719.                                 }
  1720.                         }
  1721.                 }
  1722.         }
  1723.        
  1724.         tile_time = opj_clock() - tile_time;    /* time needed to decode a tile */
  1725.         opj_event_msg(tcd->cinfo, EVT_INFO, "- tile decoded in %f s\n", tile_time);
  1726.                
  1727.         for (compno = 0; compno < tile->numcomps; compno++) {
  1728.                 opj_free(tcd->tcd_volume->tiles[tileno].comps[compno].data);
  1729.                 tcd->tcd_volume->tiles[tileno].comps[compno].data = NULL;
  1730.         }
  1731.        
  1732.         if (eof) {
  1733.                 return false;
  1734.         }
  1735.        
  1736.         return true;
  1737. }
  1738.  
  1739.