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