95cfd93e05740aed6f8c2f7178e147179001ae5a
[openjpeg.git] / libopenjpeg / tcd.c
1 /*
2  * Copyright (c) 2002-2007, Communications and Remote Sensing Laboratory, Universite catholique de Louvain (UCL), Belgium
3  * Copyright (c) 2002-2007, Professor Benoit Macq
4  * Copyright (c) 2001-2003, David Janssens
5  * Copyright (c) 2002-2003, Yannick Verschueren
6  * Copyright (c) 2003-2007, Francois-Olivier Devaux and Antonin Descampe
7  * Copyright (c) 2005, Herve Drolon, FreeImage Team
8  * Copyright (c) 2006-2007, Parvatha Elangovan
9  * All rights reserved.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
21  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
24  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30  * POSSIBILITY OF SUCH DAMAGE.
31  */
32
33 #include "opj_includes.h"
34
35 void tcd_dump(FILE *fd, opj_tcd_t *tcd, opj_tcd_image_t * img) {
36         int tileno, compno, resno, bandno, precno;//, cblkno;
37
38         fprintf(fd, "image {\n");
39         fprintf(fd, "  tw=%d, th=%d x0=%d x1=%d y0=%d y1=%d\n", 
40                 img->tw, img->th, tcd->image->x0, tcd->image->x1, tcd->image->y0, tcd->image->y1);
41
42         for (tileno = 0; tileno < img->th * img->tw; tileno++) {
43                 opj_tcd_tile_t *tile = &tcd->tcd_image->tiles[tileno];
44                 fprintf(fd, "  tile {\n");
45                 fprintf(fd, "    x0=%d, y0=%d, x1=%d, y1=%d, numcomps=%d\n",
46                         tile->x0, tile->y0, tile->x1, tile->y1, tile->numcomps);
47                 for (compno = 0; compno < tile->numcomps; compno++) {
48                         opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
49                         fprintf(fd, "    tilec {\n");
50                         fprintf(fd,
51                                 "      x0=%d, y0=%d, x1=%d, y1=%d, numresolutions=%d\n",
52                                 tilec->x0, tilec->y0, tilec->x1, tilec->y1, tilec->numresolutions);
53                         for (resno = 0; resno < tilec->numresolutions; resno++) {
54                                 opj_tcd_resolution_t *res = &tilec->resolutions[resno];
55                                 fprintf(fd, "\n   res {\n");
56                                 fprintf(fd,
57                                         "          x0=%d, y0=%d, x1=%d, y1=%d, pw=%d, ph=%d, numbands=%d\n",
58                                         res->x0, res->y0, res->x1, res->y1, res->pw, res->ph, res->numbands);
59                                 for (bandno = 0; bandno < res->numbands; bandno++) {
60                                         opj_tcd_band_t *band = &res->bands[bandno];
61                                         fprintf(fd, "        band {\n");
62                                         fprintf(fd,
63                                                 "          x0=%d, y0=%d, x1=%d, y1=%d, stepsize=%f, numbps=%d\n",
64                                                 band->x0, band->y0, band->x1, band->y1, band->stepsize, band->numbps);
65                                         for (precno = 0; precno < res->pw * res->ph; precno++) {
66                                                 opj_tcd_precinct_t *prec = &band->precincts[precno];
67                                                 fprintf(fd, "          prec {\n");
68                                                 fprintf(fd,
69                                                         "            x0=%d, y0=%d, x1=%d, y1=%d, cw=%d, ch=%d\n",
70                                                         prec->x0, prec->y0, prec->x1, prec->y1, prec->cw, prec->ch);
71                                                 /*
72                                                 for (cblkno = 0; cblkno < prec->cw * prec->ch; cblkno++) {
73                                                         opj_tcd_cblk_t *cblk = &prec->cblks[cblkno];
74                                                         fprintf(fd, "            cblk {\n");
75                                                         fprintf(fd,
76                                                                 "              x0=%d, y0=%d, x1=%d, y1=%d\n",
77                                                                 cblk->x0, cblk->y0, cblk->x1, cblk->y1);
78                                                         fprintf(fd, "            }\n");
79                                                 }
80                                                 */
81                                                 fprintf(fd, "          }\n");
82                                         }
83                                         fprintf(fd, "        }\n");
84                                 }
85                                 fprintf(fd, "      }\n");
86                         }
87                         fprintf(fd, "    }\n");
88                 }
89                 fprintf(fd, "  }\n");
90         }
91         fprintf(fd, "}\n");
92 }
93
94 /* ----------------------------------------------------------------------- */
95
96 /**
97 Create a new TCD handle
98 */
99 opj_tcd_t* tcd_create(opj_common_ptr cinfo) {
100         /* create the tcd structure */
101         opj_tcd_t *tcd = (opj_tcd_t*)opj_malloc(sizeof(opj_tcd_t));
102         if(!tcd) return NULL;
103         tcd->cinfo = cinfo;
104         tcd->tcd_image = (opj_tcd_image_t*)opj_malloc(sizeof(opj_tcd_image_t));
105         if(!tcd->tcd_image) {
106                 opj_free(tcd);
107                 return NULL;
108         }
109
110         return tcd;
111 }
112
113 /**
114 Destroy a previously created TCD handle
115 */
116 void tcd_destroy(opj_tcd_t *tcd) {
117         if(tcd) {
118                 opj_free(tcd->tcd_image);
119                 opj_free(tcd);
120         }
121 }
122
123 /* ----------------------------------------------------------------------- */
124
125 void tcd_malloc_encode(opj_tcd_t *tcd, opj_image_t * image, opj_cp_t * cp, int curtileno) {
126         int tileno, compno, resno, bandno, precno, cblkno;
127
128         tcd->image = image;
129         tcd->cp = cp;
130         tcd->tcd_image->tw = cp->tw;
131         tcd->tcd_image->th = cp->th;
132         tcd->tcd_image->tiles = (opj_tcd_tile_t *) opj_malloc(sizeof(opj_tcd_tile_t));
133         
134         for (tileno = 0; tileno < 1; tileno++) {
135                 opj_tcp_t *tcp = &cp->tcps[curtileno];
136                 int j;
137
138                 /* cfr p59 ISO/IEC FDIS15444-1 : 2000 (18 august 2000) */
139                 int p = curtileno % cp->tw;     /* si numerotation matricielle .. */
140                 int q = curtileno / cp->tw;     /* .. coordonnees de la tile (q,p) q pour ligne et p pour colonne */
141
142                 /* opj_tcd_tile_t *tile=&tcd->tcd_image->tiles[tileno]; */
143                 opj_tcd_tile_t *tile = tcd->tcd_image->tiles;
144
145                 /* 4 borders of the tile rescale on the image if necessary */
146                 tile->x0 = int_max(cp->tx0 + p * cp->tdx, image->x0);
147                 tile->y0 = int_max(cp->ty0 + q * cp->tdy, image->y0);
148                 tile->x1 = int_min(cp->tx0 + (p + 1) * cp->tdx, image->x1);
149                 tile->y1 = int_min(cp->ty0 + (q + 1) * cp->tdy, image->y1);
150                 tile->numcomps = image->numcomps;
151                 /* tile->PPT=image->PPT;  */
152
153                 /* Modification of the RATE >> */
154                 for (j = 0; j < tcp->numlayers; j++) {
155                         tcp->rates[j] = tcp->rates[j] ? 
156                                 cp->tp_on ? 
157                                         (((float) (tile->numcomps 
158                                         * (tile->x1 - tile->x0) 
159                                         * (tile->y1 - tile->y0)
160                                         * image->comps[0].prec))
161                                         /(tcp->rates[j] * 8 * image->comps[0].dx * image->comps[0].dy)) - (((tcd->cur_totnum_tp - 1) * 14 )/ tcp->numlayers)
162                                         :
163                                 ((float) (tile->numcomps 
164                                         * (tile->x1 - tile->x0) 
165                                         * (tile->y1 - tile->y0) 
166                                         * image->comps[0].prec))/ 
167                                         (tcp->rates[j] * 8 * image->comps[0].dx * image->comps[0].dy)
168                                         : 0;
169
170                         if (tcp->rates[j]) {
171                                 if (j && tcp->rates[j] < tcp->rates[j - 1] + 10) {
172                                         tcp->rates[j] = tcp->rates[j - 1] + 20;
173                                 } else {
174                                         if (!j && tcp->rates[j] < 30)
175                                                 tcp->rates[j] = 30;
176                                 }
177                                 
178                                 if(j == (tcp->numlayers-1)){
179                                         tcp->rates[j] = tcp->rates[j]- 2;
180                                 }
181                         }
182                 }
183                 /* << Modification of the RATE */
184                 
185                 tile->comps = (opj_tcd_tilecomp_t *) opj_malloc(image->numcomps * sizeof(opj_tcd_tilecomp_t));
186                 for (compno = 0; compno < tile->numcomps; compno++) {
187                         opj_tccp_t *tccp = &tcp->tccps[compno];
188
189                         opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
190
191                         /* border of each tile component (global) */
192                         tilec->x0 = int_ceildiv(tile->x0, image->comps[compno].dx);
193                         tilec->y0 = int_ceildiv(tile->y0, image->comps[compno].dy);
194                         tilec->x1 = int_ceildiv(tile->x1, image->comps[compno].dx);
195                         tilec->y1 = int_ceildiv(tile->y1, image->comps[compno].dy);
196                         
197                         tilec->data = (int *) opj_aligned_malloc((tilec->x1 - tilec->x0) * (tilec->y1 - tilec->y0) * sizeof(int));
198                         tilec->numresolutions = tccp->numresolutions;
199
200                         tilec->resolutions = (opj_tcd_resolution_t *) opj_malloc(tilec->numresolutions * sizeof(opj_tcd_resolution_t));
201                         
202                         for (resno = 0; resno < tilec->numresolutions; resno++) {
203                                 int pdx, pdy;
204                                 int levelno = tilec->numresolutions - 1 - resno;
205                                 int tlprcxstart, tlprcystart, brprcxend, brprcyend;
206                                 int tlcbgxstart, tlcbgystart, brcbgxend, brcbgyend;
207                                 int cbgwidthexpn, cbgheightexpn;
208                                 int cblkwidthexpn, cblkheightexpn;
209
210                                 opj_tcd_resolution_t *res = &tilec->resolutions[resno];
211                                 
212                                 /* border for each resolution level (global) */
213                                 res->x0 = int_ceildivpow2(tilec->x0, levelno);
214                                 res->y0 = int_ceildivpow2(tilec->y0, levelno);
215                                 res->x1 = int_ceildivpow2(tilec->x1, levelno);
216                                 res->y1 = int_ceildivpow2(tilec->y1, levelno);
217                                 
218                                 res->numbands = resno == 0 ? 1 : 3;
219                                 /* p. 35, table A-23, ISO/IEC FDIS154444-1 : 2000 (18 august 2000) */
220                                 if (tccp->csty & J2K_CCP_CSTY_PRT) {
221                                         pdx = tccp->prcw[resno];
222                                         pdy = tccp->prch[resno];
223                                 } else {
224                                         pdx = 15;
225                                         pdy = 15;
226                                 }
227                                 /* p. 64, B.6, ISO/IEC FDIS15444-1 : 2000 (18 august 2000)  */
228                                 tlprcxstart = int_floordivpow2(res->x0, pdx) << pdx;
229                                 tlprcystart = int_floordivpow2(res->y0, pdy) << pdy;
230                                 
231                                 brprcxend = int_ceildivpow2(res->x1, pdx) << pdx;
232                                 brprcyend = int_ceildivpow2(res->y1, pdy) << pdy;
233                                 
234                                 res->pw = (brprcxend - tlprcxstart) >> pdx;
235                                 res->ph = (brprcyend - tlprcystart) >> pdy;
236                                 
237                                 if (resno == 0) {
238                                         tlcbgxstart = tlprcxstart;
239                                         tlcbgystart = tlprcystart;
240                                         brcbgxend = brprcxend;
241                                         brcbgyend = brprcyend;
242                                         cbgwidthexpn = pdx;
243                                         cbgheightexpn = pdy;
244                                 } else {
245                                         tlcbgxstart = int_ceildivpow2(tlprcxstart, 1);
246                                         tlcbgystart = int_ceildivpow2(tlprcystart, 1);
247                                         brcbgxend = int_ceildivpow2(brprcxend, 1);
248                                         brcbgyend = int_ceildivpow2(brprcyend, 1);
249                                         cbgwidthexpn = pdx - 1;
250                                         cbgheightexpn = pdy - 1;
251                                 }
252                                 
253                                 cblkwidthexpn = int_min(tccp->cblkw, cbgwidthexpn);
254                                 cblkheightexpn = int_min(tccp->cblkh, cbgheightexpn);
255                                 
256                                 for (bandno = 0; bandno < res->numbands; bandno++) {
257                                         int x0b, y0b, i;
258                                         int gain, numbps;
259                                         opj_stepsize_t *ss = NULL;
260
261                                         opj_tcd_band_t *band = &res->bands[bandno];
262
263                                         band->bandno = resno == 0 ? 0 : bandno + 1;
264                                         x0b = (band->bandno == 1) || (band->bandno == 3) ? 1 : 0;
265                                         y0b = (band->bandno == 2) || (band->bandno == 3) ? 1 : 0;
266                                         
267                                         if (band->bandno == 0) {
268                                                 /* band border (global) */
269                                                 band->x0 = int_ceildivpow2(tilec->x0, levelno);
270                                                 band->y0 = int_ceildivpow2(tilec->y0, levelno);
271                                                 band->x1 = int_ceildivpow2(tilec->x1, levelno);
272                                                 band->y1 = int_ceildivpow2(tilec->y1, levelno);
273                                         } else {
274                                                 /* band border (global) */
275                                                 band->x0 = int_ceildivpow2(tilec->x0 - (1 << levelno) * x0b, levelno + 1);
276                                                 band->y0 = int_ceildivpow2(tilec->y0 - (1 << levelno) * y0b, levelno + 1);
277                                                 band->x1 = int_ceildivpow2(tilec->x1 - (1 << levelno) * x0b, levelno + 1);
278                                                 band->y1 = int_ceildivpow2(tilec->y1 - (1 << levelno) * y0b, levelno + 1);
279                                         }
280                                         
281                                         ss = &tccp->stepsizes[resno == 0 ? 0 : 3 * (resno - 1) + bandno + 1];
282                                         gain = tccp->qmfbid == 0 ? dwt_getgain_real(band->bandno) : dwt_getgain(band->bandno);                                  
283                                         numbps = image->comps[compno].prec + gain;
284                                         
285                                         band->stepsize = (float)((1.0 + ss->mant / 2048.0) * pow(2.0, numbps - ss->expn));
286                                         band->numbps = ss->expn + tccp->numgbits - 1;   /* WHY -1 ? */
287                                         
288                                         band->precincts = (opj_tcd_precinct_t *) opj_malloc(3 * res->pw * res->ph * sizeof(opj_tcd_precinct_t));
289                                         
290                                         for (i = 0; i < res->pw * res->ph * 3; i++) {
291                                                 band->precincts[i].imsbtree = NULL;
292                                                 band->precincts[i].incltree = NULL;
293                                         }
294                                         
295                                         for (precno = 0; precno < res->pw * res->ph; precno++) {
296                                                 int tlcblkxstart, tlcblkystart, brcblkxend, brcblkyend;
297
298                                                 int cbgxstart = tlcbgxstart + (precno % res->pw) * (1 << cbgwidthexpn);
299                                                 int cbgystart = tlcbgystart + (precno / res->pw) * (1 << cbgheightexpn);
300                                                 int cbgxend = cbgxstart + (1 << cbgwidthexpn);
301                                                 int cbgyend = cbgystart + (1 << cbgheightexpn);
302
303                                                 opj_tcd_precinct_t *prc = &band->precincts[precno];
304
305                                                 /* precinct size (global) */
306                                                 prc->x0 = int_max(cbgxstart, band->x0);
307                                                 prc->y0 = int_max(cbgystart, band->y0);
308                                                 prc->x1 = int_min(cbgxend, band->x1);
309                                                 prc->y1 = int_min(cbgyend, band->y1);
310
311                                                 tlcblkxstart = int_floordivpow2(prc->x0, cblkwidthexpn) << cblkwidthexpn;
312                                                 tlcblkystart = int_floordivpow2(prc->y0, cblkheightexpn) << cblkheightexpn;
313                                                 brcblkxend = int_ceildivpow2(prc->x1, cblkwidthexpn) << cblkwidthexpn;
314                                                 brcblkyend = int_ceildivpow2(prc->y1, cblkheightexpn) << cblkheightexpn;
315                                                 prc->cw = (brcblkxend - tlcblkxstart) >> cblkwidthexpn;
316                                                 prc->ch = (brcblkyend - tlcblkystart) >> cblkheightexpn;
317
318                                                 prc->cblks.enc = (opj_tcd_cblk_enc_t*) opj_calloc((prc->cw * prc->ch), sizeof(opj_tcd_cblk_enc_t));
319                                                 prc->incltree = tgt_create(prc->cw, prc->ch);
320                                                 prc->imsbtree = tgt_create(prc->cw, prc->ch);
321                                                 
322                                                 for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
323                                                         int cblkxstart = tlcblkxstart + (cblkno % prc->cw) * (1 << cblkwidthexpn);
324                                                         int cblkystart = tlcblkystart + (cblkno / prc->cw) * (1 << cblkheightexpn);
325                                                         int cblkxend = cblkxstart + (1 << cblkwidthexpn);
326                                                         int cblkyend = cblkystart + (1 << cblkheightexpn);
327                                                         
328                                                         opj_tcd_cblk_enc_t* cblk = &prc->cblks.enc[cblkno];
329
330                                                         /* code-block size (global) */
331                                                         cblk->x0 = int_max(cblkxstart, prc->x0);
332                                                         cblk->y0 = int_max(cblkystart, prc->y0);
333                                                         cblk->x1 = int_min(cblkxend, prc->x1);
334                                                         cblk->y1 = int_min(cblkyend, prc->y1);
335                                                         cblk->data = (unsigned char*) opj_calloc(8192+2, sizeof(unsigned char));
336                                                         /* FIXME: mqc_init_enc and mqc_byteout underrun the buffer if we don't do this. Why? */
337                                                         cblk->data += 2;
338                                                         cblk->layers = (opj_tcd_layer_t*) opj_calloc(100, sizeof(opj_tcd_layer_t));
339                                                         cblk->passes = (opj_tcd_pass_t*) opj_calloc(100, sizeof(opj_tcd_pass_t));
340                                                 }
341                                         }
342                                 }
343                         }
344                 }
345         }
346         
347         /* tcd_dump(stdout, tcd, &tcd->tcd_image); */
348 }
349
350 void tcd_free_encode(opj_tcd_t *tcd) {
351         int tileno, compno, resno, bandno, precno, cblkno;
352
353         for (tileno = 0; tileno < 1; tileno++) {
354                 opj_tcd_tile_t *tile = tcd->tcd_image->tiles;
355
356                 for (compno = 0; compno < tile->numcomps; compno++) {
357                         opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
358
359                         for (resno = 0; resno < tilec->numresolutions; resno++) {
360                                 opj_tcd_resolution_t *res = &tilec->resolutions[resno];
361
362                                 for (bandno = 0; bandno < res->numbands; bandno++) {
363                                         opj_tcd_band_t *band = &res->bands[bandno];
364
365                                         for (precno = 0; precno < res->pw * res->ph; precno++) {
366                                                 opj_tcd_precinct_t *prc = &band->precincts[precno];
367
368                                                 if (prc->incltree != NULL) {
369                                                         tgt_destroy(prc->incltree);
370                                                         prc->incltree = NULL;
371                                                 }
372                                                 if (prc->imsbtree != NULL) {
373                                                         tgt_destroy(prc->imsbtree);     
374                                                         prc->imsbtree = NULL;
375                                                 }
376                                                 for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
377                                                         opj_free(prc->cblks.enc[cblkno].data - 2);
378                                                         opj_free(prc->cblks.enc[cblkno].layers);
379                                                         opj_free(prc->cblks.enc[cblkno].passes);
380                                                 }
381                                                 opj_free(prc->cblks.enc);
382                                         } /* for (precno */
383                                         opj_free(band->precincts);
384                                         band->precincts = NULL;
385                                 } /* for (bandno */
386                         } /* for (resno */
387                         opj_free(tilec->resolutions);
388                         tilec->resolutions = NULL;
389                 } /* for (compno */
390                 opj_free(tile->comps);
391                 tile->comps = NULL;
392         } /* for (tileno */
393         opj_free(tcd->tcd_image->tiles);
394         tcd->tcd_image->tiles = NULL;
395 }
396
397 void tcd_init_encode(opj_tcd_t *tcd, opj_image_t * image, opj_cp_t * cp, int curtileno) {
398         int tileno, compno, resno, bandno, precno, cblkno;
399
400         for (tileno = 0; tileno < 1; tileno++) {
401                 opj_tcp_t *tcp = &cp->tcps[curtileno];
402                 int j;
403                 /* cfr p59 ISO/IEC FDIS15444-1 : 2000 (18 august 2000) */
404                 int p = curtileno % cp->tw;
405                 int q = curtileno / cp->tw;
406
407                 opj_tcd_tile_t *tile = tcd->tcd_image->tiles;
408                 
409                 /* 4 borders of the tile rescale on the image if necessary */
410                 tile->x0 = int_max(cp->tx0 + p * cp->tdx, image->x0);
411                 tile->y0 = int_max(cp->ty0 + q * cp->tdy, image->y0);
412                 tile->x1 = int_min(cp->tx0 + (p + 1) * cp->tdx, image->x1);
413                 tile->y1 = int_min(cp->ty0 + (q + 1) * cp->tdy, image->y1);
414                 
415                 tile->numcomps = image->numcomps;
416                 /* tile->PPT=image->PPT; */
417
418                 /* Modification of the RATE >> */
419                 for (j = 0; j < tcp->numlayers; j++) {
420                         tcp->rates[j] = tcp->rates[j] ? 
421                                                 ((float) (tile->numcomps 
422                                                                 * (tile->x1 - tile->x0) 
423                                                                 * (tile->y1 - tile->y0) 
424                                                                 * image->comps[0].prec))/ 
425                                                 (tcp->rates[j] * 8 * image->comps[0].dx * image->comps[0].dy) 
426                                                 : 0;
427
428                         if (tcp->rates[j]) {
429                                 if (j && tcp->rates[j] < tcp->rates[j - 1] + 10) {
430                                         tcp->rates[j] = tcp->rates[j - 1] + 20;
431                                 } else {
432                                         if (!j && tcp->rates[j] < 30)
433                                                 tcp->rates[j] = 30;
434                                 }
435                         }
436                 }
437                 /* << Modification of the RATE */
438
439                 /* tile->comps=(opj_tcd_tilecomp_t*)opj_realloc(tile->comps,image->numcomps*sizeof(opj_tcd_tilecomp_t)); */
440                 for (compno = 0; compno < tile->numcomps; compno++) {
441                         opj_tccp_t *tccp = &tcp->tccps[compno];
442                         
443                         opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
444
445                         /* border of each tile component (global) */
446                         tilec->x0 = int_ceildiv(tile->x0, image->comps[compno].dx);
447                         tilec->y0 = int_ceildiv(tile->y0, image->comps[compno].dy);
448                         tilec->x1 = int_ceildiv(tile->x1, image->comps[compno].dx);
449                         tilec->y1 = int_ceildiv(tile->y1, image->comps[compno].dy);
450                         
451                         tilec->data = (int *) opj_aligned_malloc((tilec->x1 - tilec->x0) * (tilec->y1 - tilec->y0) * sizeof(int));
452                         tilec->numresolutions = tccp->numresolutions;
453                         /* tilec->resolutions=(opj_tcd_resolution_t*)opj_realloc(tilec->resolutions,tilec->numresolutions*sizeof(opj_tcd_resolution_t)); */
454                         for (resno = 0; resno < tilec->numresolutions; resno++) {
455                                 int pdx, pdy;
456
457                                 int levelno = tilec->numresolutions - 1 - resno;
458                                 int tlprcxstart, tlprcystart, brprcxend, brprcyend;
459                                 int tlcbgxstart, tlcbgystart, brcbgxend, brcbgyend;
460                                 int cbgwidthexpn, cbgheightexpn;
461                                 int cblkwidthexpn, cblkheightexpn;
462                                 
463                                 opj_tcd_resolution_t *res = &tilec->resolutions[resno];
464
465                                 /* border for each resolution level (global) */
466                                 res->x0 = int_ceildivpow2(tilec->x0, levelno);
467                                 res->y0 = int_ceildivpow2(tilec->y0, levelno);
468                                 res->x1 = int_ceildivpow2(tilec->x1, levelno);
469                                 res->y1 = int_ceildivpow2(tilec->y1, levelno);  
470                                 res->numbands = resno == 0 ? 1 : 3;
471
472                                 /* p. 35, table A-23, ISO/IEC FDIS154444-1 : 2000 (18 august 2000) */
473                                 if (tccp->csty & J2K_CCP_CSTY_PRT) {
474                                         pdx = tccp->prcw[resno];
475                                         pdy = tccp->prch[resno];
476                                 } else {
477                                         pdx = 15;
478                                         pdy = 15;
479                                 }
480                                 /* p. 64, B.6, ISO/IEC FDIS15444-1 : 2000 (18 august 2000)  */
481                                 tlprcxstart = int_floordivpow2(res->x0, pdx) << pdx;
482                                 tlprcystart = int_floordivpow2(res->y0, pdy) << pdy;
483                                 brprcxend = int_ceildivpow2(res->x1, pdx) << pdx;
484                                 brprcyend = int_ceildivpow2(res->y1, pdy) << pdy;
485                                 
486                                 res->pw = (brprcxend - tlprcxstart) >> pdx;
487                                 res->ph = (brprcyend - tlprcystart) >> pdy;
488                                 
489                                 if (resno == 0) {
490                                         tlcbgxstart = tlprcxstart;
491                                         tlcbgystart = tlprcystart;
492                                         brcbgxend = brprcxend;
493                                         brcbgyend = brprcyend;
494                                         cbgwidthexpn = pdx;
495                                         cbgheightexpn = pdy;
496                                 } else {
497                                         tlcbgxstart = int_ceildivpow2(tlprcxstart, 1);
498                                         tlcbgystart = int_ceildivpow2(tlprcystart, 1);
499                                         brcbgxend = int_ceildivpow2(brprcxend, 1);
500                                         brcbgyend = int_ceildivpow2(brprcyend, 1);
501                                         cbgwidthexpn = pdx - 1;
502                                         cbgheightexpn = pdy - 1;
503                                 }
504                                 
505                                 cblkwidthexpn = int_min(tccp->cblkw, cbgwidthexpn);
506                                 cblkheightexpn = int_min(tccp->cblkh, cbgheightexpn);
507                                 
508                                 for (bandno = 0; bandno < res->numbands; bandno++) {
509                                         int x0b, y0b;
510                                         int gain, numbps;
511                                         opj_stepsize_t *ss = NULL;
512
513                                         opj_tcd_band_t *band = &res->bands[bandno];
514
515                                         band->bandno = resno == 0 ? 0 : bandno + 1;
516                                         x0b = (band->bandno == 1) || (band->bandno == 3) ? 1 : 0;
517                                         y0b = (band->bandno == 2) || (band->bandno == 3) ? 1 : 0;
518                                         
519                                         if (band->bandno == 0) {
520                                                 /* band border */
521                                                 band->x0 = int_ceildivpow2(tilec->x0, levelno);
522                                                 band->y0 = int_ceildivpow2(tilec->y0, levelno);
523                                                 band->x1 = int_ceildivpow2(tilec->x1, levelno);
524                                                 band->y1 = int_ceildivpow2(tilec->y1, levelno);
525                                         } else {
526                                                 band->x0 = int_ceildivpow2(tilec->x0 - (1 << levelno) * x0b, levelno + 1);
527                                                 band->y0 = int_ceildivpow2(tilec->y0 - (1 << levelno) * y0b, levelno + 1);
528                                                 band->x1 = int_ceildivpow2(tilec->x1 - (1 << levelno) * x0b, levelno + 1);
529                                                 band->y1 = int_ceildivpow2(tilec->y1 - (1 << levelno) * y0b, levelno + 1);
530                                         }
531                                         
532                                         ss = &tccp->stepsizes[resno == 0 ? 0 : 3 * (resno - 1) + bandno + 1];
533                                         gain = tccp->qmfbid == 0 ? dwt_getgain_real(band->bandno) : dwt_getgain(band->bandno);
534                                         numbps = image->comps[compno].prec + gain;
535                                         band->stepsize = (float)((1.0 + ss->mant / 2048.0) * pow(2.0, numbps - ss->expn));
536                                         band->numbps = ss->expn + tccp->numgbits - 1;   /* WHY -1 ? */
537                                         
538                                         for (precno = 0; precno < res->pw * res->ph; precno++) {
539                                                 int tlcblkxstart, tlcblkystart, brcblkxend, brcblkyend;
540
541                                                 int cbgxstart = tlcbgxstart + (precno % res->pw) * (1 << cbgwidthexpn);
542                                                 int cbgystart = tlcbgystart + (precno / res->pw) * (1 << cbgheightexpn);
543                                                 int cbgxend = cbgxstart + (1 << cbgwidthexpn);
544                                                 int cbgyend = cbgystart + (1 << cbgheightexpn);
545                                                 
546                                                 opj_tcd_precinct_t *prc = &band->precincts[precno];
547
548                                                 /* precinct size (global) */
549                                                 prc->x0 = int_max(cbgxstart, band->x0);
550                                                 prc->y0 = int_max(cbgystart, band->y0);
551                                                 prc->x1 = int_min(cbgxend, band->x1);
552                                                 prc->y1 = int_min(cbgyend, band->y1);
553
554                                                 tlcblkxstart = int_floordivpow2(prc->x0, cblkwidthexpn) << cblkwidthexpn;
555                                                 tlcblkystart = int_floordivpow2(prc->y0, cblkheightexpn) << cblkheightexpn;
556                                                 brcblkxend = int_ceildivpow2(prc->x1, cblkwidthexpn) << cblkwidthexpn;
557                                                 brcblkyend = int_ceildivpow2(prc->y1, cblkheightexpn) << cblkheightexpn;
558                                                 prc->cw = (brcblkxend - tlcblkxstart) >> cblkwidthexpn;
559                                                 prc->ch = (brcblkyend - tlcblkystart) >> cblkheightexpn;
560
561                                                 opj_free(prc->cblks.enc);
562                                                 prc->cblks.enc = (opj_tcd_cblk_enc_t*) opj_calloc(prc->cw * prc->ch, sizeof(opj_tcd_cblk_enc_t));
563
564                                                 if (prc->incltree != NULL) {
565                                                         tgt_destroy(prc->incltree);
566                                                 }
567                                                 if (prc->imsbtree != NULL) {
568                                                         tgt_destroy(prc->imsbtree);
569                                                 }
570                                                 
571                                                 prc->incltree = tgt_create(prc->cw, prc->ch);
572                                                 prc->imsbtree = tgt_create(prc->cw, prc->ch);
573
574                                                 for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
575                                                         int cblkxstart = tlcblkxstart + (cblkno % prc->cw) * (1 << cblkwidthexpn);
576                                                         int cblkystart = tlcblkystart + (cblkno / prc->cw) * (1 << cblkheightexpn);
577                                                         int cblkxend = cblkxstart + (1 << cblkwidthexpn);
578                                                         int cblkyend = cblkystart + (1 << cblkheightexpn);
579
580                                                         opj_tcd_cblk_enc_t* cblk = &prc->cblks.enc[cblkno];
581
582                                                         /* code-block size (global) */
583                                                         cblk->x0 = int_max(cblkxstart, prc->x0);
584                                                         cblk->y0 = int_max(cblkystart, prc->y0);
585                                                         cblk->x1 = int_min(cblkxend, prc->x1);
586                                                         cblk->y1 = int_min(cblkyend, prc->y1);
587                                                         cblk->data = (unsigned char*) opj_calloc(8192, sizeof(unsigned char));
588                                                         cblk->layers = (opj_tcd_layer_t*) opj_calloc(100, sizeof(opj_tcd_layer_t));
589                                                         cblk->passes = (opj_tcd_pass_t*) opj_calloc(100, sizeof(opj_tcd_pass_t));
590                                                 }
591                                         } /* precno */
592                                 } /* bandno */
593                         } /* resno */
594                 } /* compno */
595         } /* tileno */
596
597         /* tcd_dump(stdout, tcd, &tcd->tcd_image); */
598 }
599
600 void tcd_malloc_decode(opj_tcd_t *tcd, opj_image_t * image, opj_cp_t * cp) {
601         int i, j, tileno, p, q;
602         unsigned int x0 = 0, y0 = 0, x1 = 0, y1 = 0, w, h;
603
604         tcd->image = image;
605         tcd->tcd_image->tw = cp->tw;
606         tcd->tcd_image->th = cp->th;
607         tcd->tcd_image->tiles = (opj_tcd_tile_t *) opj_malloc(cp->tw * cp->th * sizeof(opj_tcd_tile_t));
608
609         /* 
610         Allocate place to store the decoded data = final image
611         Place limited by the tile really present in the codestream 
612         */
613
614         for (j = 0; j < cp->tileno_size; j++) {
615                 opj_tcd_tile_t *tile;
616                 
617                 tileno = cp->tileno[j];         
618                 tile = &(tcd->tcd_image->tiles[cp->tileno[tileno]]);            
619                 tile->numcomps = image->numcomps;
620                 tile->comps = (opj_tcd_tilecomp_t*) opj_calloc(image->numcomps, sizeof(opj_tcd_tilecomp_t));
621         }
622
623         for (i = 0; i < image->numcomps; i++) {
624                 for (j = 0; j < cp->tileno_size; j++) {
625                         opj_tcd_tile_t *tile;
626                         opj_tcd_tilecomp_t *tilec;
627                         
628                         /* cfr p59 ISO/IEC FDIS15444-1 : 2000 (18 august 2000) */
629                         
630                         tileno = cp->tileno[j];
631                         
632                         tile = &(tcd->tcd_image->tiles[cp->tileno[tileno]]);
633                         tilec = &tile->comps[i];
634                         
635                         p = tileno % cp->tw;    /* si numerotation matricielle .. */
636                         q = tileno / cp->tw;    /* .. coordonnees de la tile (q,p) q pour ligne et p pour colonne */
637                         
638                         /* 4 borders of the tile rescale on the image if necessary */
639                         tile->x0 = int_max(cp->tx0 + p * cp->tdx, image->x0);
640                         tile->y0 = int_max(cp->ty0 + q * cp->tdy, image->y0);
641                         tile->x1 = int_min(cp->tx0 + (p + 1) * cp->tdx, image->x1);
642                         tile->y1 = int_min(cp->ty0 + (q + 1) * cp->tdy, image->y1);
643
644                         tilec->x0 = int_ceildiv(tile->x0, image->comps[i].dx);
645                         tilec->y0 = int_ceildiv(tile->y0, image->comps[i].dy);
646                         tilec->x1 = int_ceildiv(tile->x1, image->comps[i].dx);
647                         tilec->y1 = int_ceildiv(tile->y1, image->comps[i].dy);
648
649                         x0 = j == 0 ? tilec->x0 : int_min(x0, (unsigned int) tilec->x0);
650                         y0 = j == 0 ? tilec->y0 : int_min(y0,   (unsigned int) tilec->x0);
651                         x1 = j == 0 ? tilec->x1 : int_max(x1,   (unsigned int) tilec->x1);
652                         y1 = j == 0 ? tilec->y1 : int_max(y1,   (unsigned int) tilec->y1);
653                 }
654
655                 w = int_ceildivpow2(x1 - x0, image->comps[i].factor);
656                 h = int_ceildivpow2(y1 - y0, image->comps[i].factor);
657
658                 image->comps[i].w = w;
659                 image->comps[i].h = h;
660                 image->comps[i].x0 = x0;
661                 image->comps[i].y0 = y0;
662         }
663 }
664
665 void tcd_malloc_decode_tile(opj_tcd_t *tcd, opj_image_t * image, opj_cp_t * cp, int tileno, opj_codestream_info_t *cstr_info) {
666         int compno, resno, bandno, precno, cblkno;
667         opj_tcp_t *tcp;
668         opj_tcd_tile_t *tile;
669
670         tcd->cp = cp;
671         
672         tcp = &(cp->tcps[cp->tileno[tileno]]);
673         tile = &(tcd->tcd_image->tiles[cp->tileno[tileno]]);
674         
675         tileno = cp->tileno[tileno];
676         
677         for (compno = 0; compno < tile->numcomps; compno++) {
678                 opj_tccp_t *tccp = &tcp->tccps[compno];
679                 opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
680                 
681                 /* border of each tile component (global) */
682                 tilec->x0 = int_ceildiv(tile->x0, image->comps[compno].dx);
683                 tilec->y0 = int_ceildiv(tile->y0, image->comps[compno].dy);
684                 tilec->x1 = int_ceildiv(tile->x1, image->comps[compno].dx);
685                 tilec->y1 = int_ceildiv(tile->y1, image->comps[compno].dy);
686
687                 tilec->numresolutions = tccp->numresolutions;
688                 tilec->resolutions = (opj_tcd_resolution_t *) opj_malloc(tilec->numresolutions * sizeof(opj_tcd_resolution_t));
689                 
690                 for (resno = 0; resno < tilec->numresolutions; resno++) {
691                         int pdx, pdy;
692                         int levelno = tilec->numresolutions - 1 - resno;
693                         int tlprcxstart, tlprcystart, brprcxend, brprcyend;
694                         int tlcbgxstart, tlcbgystart, brcbgxend, brcbgyend;
695                         int cbgwidthexpn, cbgheightexpn;
696                         int cblkwidthexpn, cblkheightexpn;
697                         
698                         opj_tcd_resolution_t *res = &tilec->resolutions[resno];
699                         
700                         /* border for each resolution level (global) */
701                         res->x0 = int_ceildivpow2(tilec->x0, levelno);
702                         res->y0 = int_ceildivpow2(tilec->y0, levelno);
703                         res->x1 = int_ceildivpow2(tilec->x1, levelno);
704                         res->y1 = int_ceildivpow2(tilec->y1, levelno);
705                         res->numbands = resno == 0 ? 1 : 3;
706                         
707                         /* p. 35, table A-23, ISO/IEC FDIS154444-1 : 2000 (18 august 2000) */
708                         if (tccp->csty & J2K_CCP_CSTY_PRT) {
709                                 pdx = tccp->prcw[resno];
710                                 pdy = tccp->prch[resno];
711                         } else {
712                                 pdx = 15;
713                                 pdy = 15;
714                         }                       
715                         
716                         /* p. 64, B.6, ISO/IEC FDIS15444-1 : 2000 (18 august 2000)  */
717                         tlprcxstart = int_floordivpow2(res->x0, pdx) << pdx;
718                         tlprcystart = int_floordivpow2(res->y0, pdy) << pdy;
719                         brprcxend = int_ceildivpow2(res->x1, pdx) << pdx;
720                         brprcyend = int_ceildivpow2(res->y1, pdy) << pdy;
721                         
722                         res->pw = (res->x0 == res->x1) ? 0 : ((brprcxend - tlprcxstart) >> pdx);
723                         res->ph = (res->y0 == res->y1) ? 0 : ((brprcyend - tlprcystart) >> pdy);
724                         
725                         if (resno == 0) {
726                                 tlcbgxstart = tlprcxstart;
727                                 tlcbgystart = tlprcystart;
728                                 brcbgxend = brprcxend;
729                                 brcbgyend = brprcyend;
730                                 cbgwidthexpn = pdx;
731                                 cbgheightexpn = pdy;
732                         } else {
733                                 tlcbgxstart = int_ceildivpow2(tlprcxstart, 1);
734                                 tlcbgystart = int_ceildivpow2(tlprcystart, 1);
735                                 brcbgxend = int_ceildivpow2(brprcxend, 1);
736                                 brcbgyend = int_ceildivpow2(brprcyend, 1);
737                                 cbgwidthexpn = pdx - 1;
738                                 cbgheightexpn = pdy - 1;
739                         }
740                         
741                         cblkwidthexpn = int_min(tccp->cblkw, cbgwidthexpn);
742                         cblkheightexpn = int_min(tccp->cblkh, cbgheightexpn);
743                         
744                         for (bandno = 0; bandno < res->numbands; bandno++) {
745                                 int x0b, y0b;
746                                 int gain, numbps;
747                                 opj_stepsize_t *ss = NULL;
748                                 
749                                 opj_tcd_band_t *band = &res->bands[bandno];
750                                 band->bandno = resno == 0 ? 0 : bandno + 1;
751                                 x0b = (band->bandno == 1) || (band->bandno == 3) ? 1 : 0;
752                                 y0b = (band->bandno == 2) || (band->bandno == 3) ? 1 : 0;
753                                 
754                                 if (band->bandno == 0) {
755                                         /* band border (global) */
756                                         band->x0 = int_ceildivpow2(tilec->x0, levelno);
757                                         band->y0 = int_ceildivpow2(tilec->y0, levelno);
758                                         band->x1 = int_ceildivpow2(tilec->x1, levelno);
759                                         band->y1 = int_ceildivpow2(tilec->y1, levelno);
760                                 } else {
761                                         /* band border (global) */
762                                         band->x0 = int_ceildivpow2(tilec->x0 - (1 << levelno) * x0b, levelno + 1);
763                                         band->y0 = int_ceildivpow2(tilec->y0 - (1 << levelno) * y0b, levelno + 1);
764                                         band->x1 = int_ceildivpow2(tilec->x1 - (1 << levelno) * x0b, levelno + 1);
765                                         band->y1 = int_ceildivpow2(tilec->y1 - (1 << levelno) * y0b, levelno + 1);
766                                 }
767                                 
768                                 ss = &tccp->stepsizes[resno == 0 ? 0 : 3 * (resno - 1) + bandno + 1];
769                                 gain = tccp->qmfbid == 0 ? dwt_getgain_real(band->bandno) : dwt_getgain(band->bandno);
770                                 numbps = image->comps[compno].prec + gain;
771                                 band->stepsize = (float)(((1.0 + ss->mant / 2048.0) * pow(2.0, numbps - ss->expn)) * 0.5);
772                                 band->numbps = ss->expn + tccp->numgbits - 1;   /* WHY -1 ? */
773                                 
774                                 band->precincts = (opj_tcd_precinct_t *) opj_malloc(res->pw * res->ph * sizeof(opj_tcd_precinct_t));
775                                 
776                                 for (precno = 0; precno < res->pw * res->ph; precno++) {
777                                         int tlcblkxstart, tlcblkystart, brcblkxend, brcblkyend;
778                                         int cbgxstart = tlcbgxstart + (precno % res->pw) * (1 << cbgwidthexpn);
779                                         int cbgystart = tlcbgystart + (precno / res->pw) * (1 << cbgheightexpn);
780                                         int cbgxend = cbgxstart + (1 << cbgwidthexpn);
781                                         int cbgyend = cbgystart + (1 << cbgheightexpn);
782                                         
783                                         opj_tcd_precinct_t *prc = &band->precincts[precno];
784                                         /* precinct size (global) */
785                                         prc->x0 = int_max(cbgxstart, band->x0);
786                                         prc->y0 = int_max(cbgystart, band->y0);
787                                         prc->x1 = int_min(cbgxend, band->x1);
788                                         prc->y1 = int_min(cbgyend, band->y1);
789                                         
790                                         tlcblkxstart = int_floordivpow2(prc->x0, cblkwidthexpn) << cblkwidthexpn;
791                                         tlcblkystart = int_floordivpow2(prc->y0, cblkheightexpn) << cblkheightexpn;
792                                         brcblkxend = int_ceildivpow2(prc->x1, cblkwidthexpn) << cblkwidthexpn;
793                                         brcblkyend = int_ceildivpow2(prc->y1, cblkheightexpn) << cblkheightexpn;
794                                         prc->cw = (brcblkxend - tlcblkxstart) >> cblkwidthexpn;
795                                         prc->ch = (brcblkyend - tlcblkystart) >> cblkheightexpn;
796
797                                         prc->cblks.dec = (opj_tcd_cblk_dec_t*) opj_malloc(prc->cw * prc->ch * sizeof(opj_tcd_cblk_dec_t));
798
799                                         prc->incltree = tgt_create(prc->cw, prc->ch);
800                                         prc->imsbtree = tgt_create(prc->cw, prc->ch);
801                                         
802                                         for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
803                                                 int cblkxstart = tlcblkxstart + (cblkno % prc->cw) * (1 << cblkwidthexpn);
804                                                 int cblkystart = tlcblkystart + (cblkno / prc->cw) * (1 << cblkheightexpn);
805                                                 int cblkxend = cblkxstart + (1 << cblkwidthexpn);
806                                                 int cblkyend = cblkystart + (1 << cblkheightexpn);                                      
807
808                                                 opj_tcd_cblk_dec_t* cblk = &prc->cblks.dec[cblkno];
809                                                 cblk->data = NULL;
810                                                 cblk->segs = NULL;
811                                                 /* code-block size (global) */
812                                                 cblk->x0 = int_max(cblkxstart, prc->x0);
813                                                 cblk->y0 = int_max(cblkystart, prc->y0);
814                                                 cblk->x1 = int_min(cblkxend, prc->x1);
815                                                 cblk->y1 = int_min(cblkyend, prc->y1);
816                                                 cblk->numsegs = 0;
817                                         }
818                                 } /* precno */
819                         } /* bandno */
820                 } /* resno */
821         } /* compno */
822         /* tcd_dump(stdout, tcd, &tcd->tcd_image); */
823 }
824
825 void tcd_makelayer_fixed(opj_tcd_t *tcd, int layno, int final) {
826         int compno, resno, bandno, precno, cblkno;
827         int value;                      /*, matrice[tcd_tcp->numlayers][tcd_tile->comps[0].numresolutions][3]; */
828         int matrice[10][10][3];
829         int i, j, k;
830
831         opj_cp_t *cp = tcd->cp;
832         opj_tcd_tile_t *tcd_tile = tcd->tcd_tile;
833         opj_tcp_t *tcd_tcp = tcd->tcp;
834
835         /*matrice=(int*)opj_malloc(tcd_tcp->numlayers*tcd_tile->comps[0].numresolutions*3*sizeof(int)); */
836         
837         for (compno = 0; compno < tcd_tile->numcomps; compno++) {
838                 opj_tcd_tilecomp_t *tilec = &tcd_tile->comps[compno];
839                 for (i = 0; i < tcd_tcp->numlayers; i++) {
840                         for (j = 0; j < tilec->numresolutions; j++) {
841                                 for (k = 0; k < 3; k++) {
842                                         matrice[i][j][k] =
843                                                 (int) (cp->matrice[i * tilec->numresolutions * 3 + j * 3 + k] 
844                                                 * (float) (tcd->image->comps[compno].prec / 16.0));
845                                 }
846                         }
847                 }
848         
849                 for (resno = 0; resno < tilec->numresolutions; resno++) {
850                         opj_tcd_resolution_t *res = &tilec->resolutions[resno];
851                         for (bandno = 0; bandno < res->numbands; bandno++) {
852                                 opj_tcd_band_t *band = &res->bands[bandno];
853                                 for (precno = 0; precno < res->pw * res->ph; precno++) {
854                                         opj_tcd_precinct_t *prc = &band->precincts[precno];
855                                         for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
856                                                 opj_tcd_cblk_enc_t *cblk = &prc->cblks.enc[cblkno];
857                                                 opj_tcd_layer_t *layer = &cblk->layers[layno];
858                                                 int n;
859                                                 int imsb = tcd->image->comps[compno].prec - cblk->numbps;       /* number of bit-plan equal to zero */
860                                                 /* Correction of the matrix of coefficient to include the IMSB information */
861                                                 if (layno == 0) {
862                                                         value = matrice[layno][resno][bandno];
863                                                         if (imsb >= value) {
864                                                                 value = 0;
865                                                         } else {
866                                                                 value -= imsb;
867                                                         }
868                                                 } else {
869                                                         value = matrice[layno][resno][bandno] - matrice[layno - 1][resno][bandno];
870                                                         if (imsb >= matrice[layno - 1][resno][bandno]) {
871                                                                 value -= (imsb - matrice[layno - 1][resno][bandno]);
872                                                                 if (value < 0) {
873                                                                         value = 0;
874                                                                 }
875                                                         }
876                                                 }
877                                                 
878                                                 if (layno == 0) {
879                                                         cblk->numpassesinlayers = 0;
880                                                 }
881                                                 
882                                                 n = cblk->numpassesinlayers;
883                                                 if (cblk->numpassesinlayers == 0) {
884                                                         if (value != 0) {
885                                                                 n = 3 * value - 2 + cblk->numpassesinlayers;
886                                                         } else {
887                                                                 n = cblk->numpassesinlayers;
888                                                         }
889                                                 } else {
890                                                         n = 3 * value + cblk->numpassesinlayers;
891                                                 }
892                                                 
893                                                 layer->numpasses = n - cblk->numpassesinlayers;
894                                                 
895                                                 if (!layer->numpasses)
896                                                         continue;
897                                                 
898                                                 if (cblk->numpassesinlayers == 0) {
899                                                         layer->len = cblk->passes[n - 1].rate;
900                                                         layer->data = cblk->data;
901                                                 } else {
902                                                         layer->len = cblk->passes[n - 1].rate - cblk->passes[cblk->numpassesinlayers - 1].rate;
903                                                         layer->data = cblk->data + cblk->passes[cblk->numpassesinlayers - 1].rate;
904                                                 }
905                                                 if (final)
906                                                         cblk->numpassesinlayers = n;
907                                         }
908                                 }
909                         }
910                 }
911         }
912 }
913
914 void tcd_rateallocate_fixed(opj_tcd_t *tcd) {
915         int layno;
916         for (layno = 0; layno < tcd->tcp->numlayers; layno++) {
917                 tcd_makelayer_fixed(tcd, layno, 1);
918         }
919 }
920
921 void tcd_makelayer(opj_tcd_t *tcd, int layno, double thresh, int final) {
922         int compno, resno, bandno, precno, cblkno, passno;
923         
924         opj_tcd_tile_t *tcd_tile = tcd->tcd_tile;
925
926         tcd_tile->distolayer[layno] = 0;        /* fixed_quality */
927         
928         for (compno = 0; compno < tcd_tile->numcomps; compno++) {
929                 opj_tcd_tilecomp_t *tilec = &tcd_tile->comps[compno];
930                 for (resno = 0; resno < tilec->numresolutions; resno++) {
931                         opj_tcd_resolution_t *res = &tilec->resolutions[resno];
932                         for (bandno = 0; bandno < res->numbands; bandno++) {
933                                 opj_tcd_band_t *band = &res->bands[bandno];
934                                 for (precno = 0; precno < res->pw * res->ph; precno++) {
935                                         opj_tcd_precinct_t *prc = &band->precincts[precno];
936                                         for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
937                                                 opj_tcd_cblk_enc_t *cblk = &prc->cblks.enc[cblkno];
938                                                 opj_tcd_layer_t *layer = &cblk->layers[layno];
939                                                 
940                                                 int n;
941                                                 if (layno == 0) {
942                                                         cblk->numpassesinlayers = 0;
943                                                 }
944                                                 n = cblk->numpassesinlayers;
945                                                 for (passno = cblk->numpassesinlayers; passno < cblk->totalpasses; passno++) {
946                                                         int dr;
947                                                         double dd;
948                                                         opj_tcd_pass_t *pass = &cblk->passes[passno];
949                                                         if (n == 0) {
950                                                                 dr = pass->rate;
951                                                                 dd = pass->distortiondec;
952                                                         } else {
953                                                                 dr = pass->rate - cblk->passes[n - 1].rate;
954                                                                 dd = pass->distortiondec - cblk->passes[n - 1].distortiondec;
955                                                         }
956                                                         if (!dr) {
957                                                                 if (dd != 0)
958                                                                         n = passno + 1;
959                                                                 continue;
960                                                         }
961                                                         if (dd / dr >= thresh)
962                                                                 n = passno + 1;
963                                                 }
964                                                 layer->numpasses = n - cblk->numpassesinlayers;
965                                                 
966                                                 if (!layer->numpasses) {
967                                                         layer->disto = 0;
968                                                         continue;
969                                                 }
970                                                 if (cblk->numpassesinlayers == 0) {
971                                                         layer->len = cblk->passes[n - 1].rate;
972                                                         layer->data = cblk->data;
973                                                         layer->disto = cblk->passes[n - 1].distortiondec;
974                                                 } else {
975                                                         layer->len = cblk->passes[n - 1].rate - cblk->passes[cblk->numpassesinlayers - 1].rate;
976                                                         layer->data = cblk->data + cblk->passes[cblk->numpassesinlayers - 1].rate;
977                                                         layer->disto = cblk->passes[n - 1].distortiondec - cblk->passes[cblk->numpassesinlayers - 1].distortiondec;
978                                                 }
979                                                 
980                                                 tcd_tile->distolayer[layno] += layer->disto;    /* fixed_quality */
981                                                 
982                                                 if (final)
983                                                         cblk->numpassesinlayers = n;
984                                         }
985                                 }
986                         }
987                 }
988         }
989 }
990
991 bool tcd_rateallocate(opj_tcd_t *tcd, unsigned char *dest, int len, opj_codestream_info_t *cstr_info) {
992         int compno, resno, bandno, precno, cblkno, passno, layno;
993         double min, max;
994         double cumdisto[100];   /* fixed_quality */
995         const double K = 1;             /* 1.1; fixed_quality */
996         double maxSE = 0;
997
998         opj_cp_t *cp = tcd->cp;
999         opj_tcd_tile_t *tcd_tile = tcd->tcd_tile;
1000         opj_tcp_t *tcd_tcp = tcd->tcp;
1001
1002         min = DBL_MAX;
1003         max = 0;
1004         
1005         tcd_tile->numpix = 0;           /* fixed_quality */
1006         
1007         for (compno = 0; compno < tcd_tile->numcomps; compno++) {
1008                 opj_tcd_tilecomp_t *tilec = &tcd_tile->comps[compno];
1009                 tilec->numpix = 0;
1010
1011                 for (resno = 0; resno < tilec->numresolutions; resno++) {
1012                         opj_tcd_resolution_t *res = &tilec->resolutions[resno];
1013
1014                         for (bandno = 0; bandno < res->numbands; bandno++) {
1015                                 opj_tcd_band_t *band = &res->bands[bandno];
1016
1017                                 for (precno = 0; precno < res->pw * res->ph; precno++) {
1018                                         opj_tcd_precinct_t *prc = &band->precincts[precno];
1019
1020                                         for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
1021                                                 opj_tcd_cblk_enc_t *cblk = &prc->cblks.enc[cblkno];
1022
1023                                                 for (passno = 0; passno < cblk->totalpasses; passno++) {
1024                                                         opj_tcd_pass_t *pass = &cblk->passes[passno];
1025                                                         int dr;
1026                                                         double dd, rdslope;
1027                                                         if (passno == 0) {
1028                                                                 dr = pass->rate;
1029                                                                 dd = pass->distortiondec;
1030                                                         } else {
1031                                                                 dr = pass->rate - cblk->passes[passno - 1].rate;
1032                                                                 dd = pass->distortiondec - cblk->passes[passno - 1].distortiondec;
1033                                                         }
1034                                                         if (dr == 0) {
1035                                                                 continue;
1036                                                         }
1037                                                         rdslope = dd / dr;
1038                                                         if (rdslope < min) {
1039                                                                 min = rdslope;
1040                                                         }
1041                                                         if (rdslope > max) {
1042                                                                 max = rdslope;
1043                                                         }
1044                                                 } /* passno */
1045                                                 
1046                                                 /* fixed_quality */
1047                                                 tcd_tile->numpix += ((cblk->x1 - cblk->x0) * (cblk->y1 - cblk->y0));
1048                                                 tilec->numpix += ((cblk->x1 - cblk->x0) * (cblk->y1 - cblk->y0));
1049                                         } /* cbklno */
1050                                 } /* precno */
1051                         } /* bandno */
1052                 } /* resno */
1053                 
1054                 maxSE += (((double)(1 << tcd->image->comps[compno].prec) - 1.0) 
1055                         * ((double)(1 << tcd->image->comps[compno].prec) -1.0)) 
1056                         * ((double)(tilec->numpix));
1057         } /* compno */
1058         
1059         /* index file */
1060         if(cstr_info) {
1061                 opj_tile_info_t *tile_info = &cstr_info->tile[tcd->tcd_tileno];
1062                 tile_info->numpix = tcd_tile->numpix;
1063                 tile_info->distotile = tcd_tile->distotile;
1064                 tile_info->thresh = (double *) opj_malloc(tcd_tcp->numlayers * sizeof(double));
1065         }
1066         
1067         for (layno = 0; layno < tcd_tcp->numlayers; layno++) {
1068                 double lo = min;
1069                 double hi = max;
1070                 int success = 0;
1071                 int maxlen = tcd_tcp->rates[layno] ? int_min(((int) ceil(tcd_tcp->rates[layno])), len) : len;
1072                 double goodthresh = 0;
1073                 double stable_thresh = 0;
1074                 int i;
1075                 double distotarget;             /* fixed_quality */
1076                 
1077                 /* fixed_quality */
1078                 distotarget = tcd_tile->distotile - ((K * maxSE) / pow((float)10, tcd_tcp->distoratio[layno] / 10));
1079         
1080                 /* Don't try to find an optimal threshold but rather take everything not included yet, if
1081                   -r xx,yy,zz,0   (disto_alloc == 1 and rates == 0)
1082                   -q xx,yy,zz,0   (fixed_quality == 1 and distoratio == 0)
1083                   ==> possible to have some lossy layers and the last layer for sure lossless */
1084                 if ( ((cp->disto_alloc==1) && (tcd_tcp->rates[layno]>0)) || ((cp->fixed_quality==1) && (tcd_tcp->distoratio[layno]>0))) {
1085                         opj_t2_t *t2 = t2_create(tcd->cinfo, tcd->image, cp);
1086                         double thresh = 0;
1087
1088                         for (i = 0; i < 32; i++) {
1089                                 int l = 0;
1090                                 double distoachieved = 0;       /* fixed_quality */
1091                                 thresh = (lo + hi) / 2;
1092                                 
1093                                 tcd_makelayer(tcd, layno, thresh, 0);
1094                                 
1095                                 if (cp->fixed_quality) {        /* fixed_quality */
1096                                         if(cp->cinema){
1097                                                 l = t2_encode_packets(t2,tcd->tcd_tileno, tcd_tile, layno + 1, dest, maxlen, cstr_info,tcd->cur_tp_num,tcd->tp_pos,tcd->cur_pino,THRESH_CALC);
1098                                                 if (l == -999) {
1099                                                         lo = thresh;
1100                                                         continue;
1101                                                 }else{
1102                         distoachieved = layno == 0 ? 
1103                                                         tcd_tile->distolayer[0] : cumdisto[layno - 1] + tcd_tile->distolayer[layno];
1104                                                         if (distoachieved < distotarget) {
1105                                                                 hi=thresh; 
1106                                                                 stable_thresh = thresh;
1107                                                                 continue;
1108                                                         }else{
1109                                                                 lo=thresh;
1110                                                         }
1111                                                 }
1112                                         }else{
1113                                                 distoachieved = (layno == 0) ? 
1114                                                         tcd_tile->distolayer[0] : (cumdisto[layno - 1] + tcd_tile->distolayer[layno]);
1115                                                 if (distoachieved < distotarget) {
1116                                                         hi = thresh;
1117                                                         stable_thresh = thresh;
1118                                                         continue;
1119                                                 }
1120                                                 lo = thresh;
1121                                         }
1122                                 } else {
1123                                         l = t2_encode_packets(t2, tcd->tcd_tileno, tcd_tile, layno + 1, dest, maxlen, cstr_info,tcd->cur_tp_num,tcd->tp_pos,tcd->cur_pino,THRESH_CALC);
1124                                         /* TODO: what to do with l ??? seek / tell ??? */
1125                                         /* opj_event_msg(tcd->cinfo, EVT_INFO, "rate alloc: len=%d, max=%d\n", l, maxlen); */
1126                                         if (l == -999) {
1127                                                 lo = thresh;
1128                                                 continue;
1129                                         }
1130                                         hi = thresh;
1131                                         stable_thresh = thresh;
1132                                 }
1133                         }
1134                         success = 1;
1135                         goodthresh = stable_thresh == 0? thresh : stable_thresh;
1136                         t2_destroy(t2);
1137                 } else {
1138                         success = 1;
1139                         goodthresh = min;
1140                 }
1141                 
1142                 if (!success) {
1143                         return false;
1144                 }
1145                 
1146                 if(cstr_info) { /* Threshold for Marcela Index */
1147                         cstr_info->tile[tcd->tcd_tileno].thresh[layno] = goodthresh;
1148                 }
1149                 tcd_makelayer(tcd, layno, goodthresh, 1);
1150         
1151                 /* fixed_quality */
1152                 cumdisto[layno] = (layno == 0) ? tcd_tile->distolayer[0] : (cumdisto[layno - 1] + tcd_tile->distolayer[layno]); 
1153         }
1154
1155         return true;
1156 }
1157
1158 int tcd_encode_tile(opj_tcd_t *tcd, int tileno, unsigned char *dest, int len, opj_codestream_info_t *cstr_info) {
1159         int compno;
1160         int l, i, numpacks = 0;
1161         opj_tcd_tile_t *tile = NULL;
1162         opj_tcp_t *tcd_tcp = NULL;
1163         opj_cp_t *cp = NULL;
1164
1165         opj_tcp_t *tcp = &tcd->cp->tcps[0];
1166         opj_tccp_t *tccp = &tcp->tccps[0];
1167         opj_image_t *image = tcd->image;
1168         
1169         opj_t1_t *t1 = NULL;            /* T1 component */
1170         opj_t2_t *t2 = NULL;            /* T2 component */
1171
1172         tcd->tcd_tileno = tileno;
1173         tcd->tcd_tile = tcd->tcd_image->tiles;
1174         tcd->tcp = &tcd->cp->tcps[tileno];
1175
1176         tile = tcd->tcd_tile;
1177         tcd_tcp = tcd->tcp;
1178         cp = tcd->cp;
1179
1180         if(tcd->cur_tp_num == 0){
1181                 tcd->encoding_time = opj_clock();       /* time needed to encode a tile */
1182                 /* INDEX >> "Precinct_nb_X et Precinct_nb_Y" */
1183                 if(cstr_info) {
1184                         opj_tcd_tilecomp_t *tilec_idx = &tile->comps[0];        /* based on component 0 */
1185                         for (i = 0; i < tilec_idx->numresolutions; i++) {
1186                                 opj_tcd_resolution_t *res_idx = &tilec_idx->resolutions[i];
1187                                 
1188                                 cstr_info->tile[tileno].pw[i] = res_idx->pw;
1189                                 cstr_info->tile[tileno].ph[i] = res_idx->ph;
1190                                 
1191                                 numpacks += res_idx->pw * res_idx->ph;
1192                                 
1193                                 cstr_info->tile[tileno].pdx[i] = tccp->prcw[i];
1194                                 cstr_info->tile[tileno].pdy[i] = tccp->prch[i];
1195                         }
1196                         cstr_info->tile[tileno].packet = (opj_packet_info_t*) opj_calloc(cstr_info->numcomps * cstr_info->numlayers * numpacks, sizeof(opj_packet_info_t));
1197                 }
1198                 /* << INDEX */
1199                 
1200                 /*---------------TILE-------------------*/
1201                 
1202                 for (compno = 0; compno < tile->numcomps; compno++) {
1203                         int x, y;
1204                         
1205                         int adjust = image->comps[compno].sgnd ? 0 : 1 << (image->comps[compno].prec - 1);
1206                         int offset_x = int_ceildiv(image->x0, image->comps[compno].dx);
1207                         int offset_y = int_ceildiv(image->y0, image->comps[compno].dy);
1208                         
1209                         opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
1210                         int tw = tilec->x1 - tilec->x0;
1211                         int w = int_ceildiv(image->x1 - image->x0, image->comps[compno].dx);
1212                         
1213                         /* extract tile data */
1214                         
1215                         if (tcd_tcp->tccps[compno].qmfbid == 1) {
1216                                 for (y = tilec->y0; y < tilec->y1; y++) {
1217                                         /* start of the src tile scanline */
1218                                         int *data = &image->comps[compno].data[(tilec->x0 - offset_x) + (y - offset_y) * w];
1219                                         /* start of the dst tile scanline */
1220                                         int *tile_data = &tilec->data[(y - tilec->y0) * tw];
1221                                         for (x = tilec->x0; x < tilec->x1; x++) {
1222                                                 *tile_data++ = *data++ - adjust;
1223                                         }
1224                                 }
1225                         } else if (tcd_tcp->tccps[compno].qmfbid == 0) {
1226                                 for (y = tilec->y0; y < tilec->y1; y++) {
1227                                         /* start of the src tile scanline */
1228                                         int *data = &image->comps[compno].data[(tilec->x0 - offset_x) + (y - offset_y) * w];
1229                                         /* start of the dst tile scanline */
1230                                         int *tile_data = &tilec->data[(y - tilec->y0) * tw];
1231                                         for (x = tilec->x0; x < tilec->x1; x++) {
1232                                                 *tile_data++ = (*data++ - adjust) << 11;
1233                                         }
1234                                         
1235                                 }
1236                         }
1237                 }
1238                 
1239                 /*----------------MCT-------------------*/
1240                 if (tcd_tcp->mct) {
1241                         int samples = (tile->comps[0].x1 - tile->comps[0].x0) * (tile->comps[0].y1 - tile->comps[0].y0);
1242                         if (tcd_tcp->tccps[0].qmfbid == 0) {
1243                                 mct_encode_real(tile->comps[0].data, tile->comps[1].data, tile->comps[2].data, samples);
1244                         } else {
1245                                 mct_encode(tile->comps[0].data, tile->comps[1].data, tile->comps[2].data, samples);
1246                         }
1247                 }
1248                 
1249                 /*----------------DWT---------------------*/
1250                 
1251                 for (compno = 0; compno < tile->numcomps; compno++) {
1252                         opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
1253                         if (tcd_tcp->tccps[compno].qmfbid == 1) {
1254                                 dwt_encode(tilec);
1255                         } else if (tcd_tcp->tccps[compno].qmfbid == 0) {
1256                                 dwt_encode_real(tilec);
1257                         }
1258                 }
1259                 
1260                 /*------------------TIER1-----------------*/
1261                 t1 = t1_create(tcd->cinfo);
1262                 t1_encode_cblks(t1, tile, tcd_tcp);
1263                 t1_destroy(t1);
1264                 
1265                 /*-----------RATE-ALLOCATE------------------*/
1266                 
1267                 /* INDEX */
1268                 if(cstr_info) {
1269                         cstr_info->index_write = 0;
1270                 }
1271                 if (cp->disto_alloc || cp->fixed_quality) {     /* fixed_quality */
1272                         /* Normal Rate/distortion allocation */
1273                         tcd_rateallocate(tcd, dest, len, cstr_info);
1274                 } else {
1275                         /* Fixed layer allocation */
1276                         tcd_rateallocate_fixed(tcd);
1277                 }
1278         }
1279         /*--------------TIER2------------------*/
1280
1281         /* INDEX */
1282         if(cstr_info) {
1283                 cstr_info->index_write = 1;
1284         }
1285
1286         t2 = t2_create(tcd->cinfo, image, cp);
1287         l = t2_encode_packets(t2,tileno, tile, tcd_tcp->numlayers, dest, len, cstr_info,tcd->tp_num,tcd->tp_pos,tcd->cur_pino,FINAL_PASS);
1288         t2_destroy(t2);
1289         
1290         /*---------------CLEAN-------------------*/
1291
1292         
1293         if(tcd->cur_tp_num == tcd->cur_totnum_tp - 1){
1294                 tcd->encoding_time = opj_clock() - tcd->encoding_time;
1295                 opj_event_msg(tcd->cinfo, EVT_INFO, "- tile encoded in %f s\n", tcd->encoding_time);
1296
1297                 /* cleaning memory */
1298                 for (compno = 0; compno < tile->numcomps; compno++) {
1299                         opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
1300                         opj_aligned_free(tilec->data);
1301                 }
1302         }
1303
1304         return l;
1305 }
1306
1307 bool tcd_decode_tile(opj_tcd_t *tcd, unsigned char *src, int len, int tileno, opj_codestream_info_t *cstr_info) {
1308         int l;
1309         int compno;
1310         int eof = 0;
1311         double tile_time, t1_time, dwt_time;
1312         opj_tcd_tile_t *tile = NULL;
1313
1314         opj_t1_t *t1 = NULL;            /* T1 component */
1315         opj_t2_t *t2 = NULL;            /* T2 component */
1316         
1317         tcd->tcd_tileno = tileno;
1318         tcd->tcd_tile = &(tcd->tcd_image->tiles[tileno]);
1319         tcd->tcp = &(tcd->cp->tcps[tileno]);
1320         tile = tcd->tcd_tile;
1321         
1322         tile_time = opj_clock();        /* time needed to decode a tile */
1323         opj_event_msg(tcd->cinfo, EVT_INFO, "tile %d of %d\n", tileno + 1, tcd->cp->tw * tcd->cp->th);
1324
1325         /* INDEX >>  */
1326         if(cstr_info) {
1327                 int resno, compno, numprec = 0;
1328                 for (compno = 0; compno < cstr_info->numcomps; compno++) {
1329                         opj_tcp_t *tcp = &tcd->cp->tcps[0];
1330                         opj_tccp_t *tccp = &tcp->tccps[compno];
1331                         opj_tcd_tilecomp_t *tilec_idx = &tile->comps[compno];   
1332                         for (resno = 0; resno < tilec_idx->numresolutions; resno++) {
1333                                 opj_tcd_resolution_t *res_idx = &tilec_idx->resolutions[resno];
1334                                 cstr_info->tile[tileno].pw[resno] = res_idx->pw;
1335                                 cstr_info->tile[tileno].ph[resno] = res_idx->ph;
1336                                 numprec += res_idx->pw * res_idx->ph;
1337                                 if (tccp->csty & J2K_CP_CSTY_PRT) {
1338                                         cstr_info->tile[tileno].pdx[resno] = tccp->prcw[resno];
1339                                         cstr_info->tile[tileno].pdy[resno] = tccp->prch[resno];
1340                                 }
1341                                 else {
1342                                         cstr_info->tile[tileno].pdx[resno] = 15;
1343                                         cstr_info->tile[tileno].pdx[resno] = 15;
1344                                 }
1345                         }
1346                 }
1347                 cstr_info->tile[tileno].packet = (opj_packet_info_t *) opj_malloc(cstr_info->numlayers * numprec * sizeof(opj_packet_info_t));
1348                 cstr_info->packno = 0;
1349         }
1350         /* << INDEX */
1351         
1352         /*--------------TIER2------------------*/
1353         
1354         t2 = t2_create(tcd->cinfo, tcd->image, tcd->cp);
1355         l = t2_decode_packets(t2, src, len, tileno, tile, cstr_info);
1356         t2_destroy(t2);
1357
1358         if (l == -999) {
1359                 eof = 1;
1360                 opj_event_msg(tcd->cinfo, EVT_ERROR, "tcd_decode: incomplete bistream\n");
1361         }
1362         
1363         /*------------------TIER1-----------------*/
1364         
1365         t1_time = opj_clock();  /* time needed to decode a tile */
1366         t1 = t1_create(tcd->cinfo);
1367         for (compno = 0; compno < tile->numcomps; ++compno) {
1368                 opj_tcd_tilecomp_t* tilec = &tile->comps[compno];
1369                 /* The +3 is headroom required by the vectorized DWT */
1370                 tilec->data = (int*) opj_aligned_malloc((((tilec->x1 - tilec->x0) * (tilec->y1 - tilec->y0))+3) * sizeof(int));
1371                 t1_decode_cblks(t1, tilec, &tcd->tcp->tccps[compno]);
1372         }
1373         t1_destroy(t1);
1374         t1_time = opj_clock() - t1_time;
1375         opj_event_msg(tcd->cinfo, EVT_INFO, "- tiers-1 took %f s\n", t1_time);
1376         
1377         /*----------------DWT---------------------*/
1378
1379         dwt_time = opj_clock(); /* time needed to decode a tile */
1380         for (compno = 0; compno < tile->numcomps; compno++) {
1381                 opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
1382                 int numres2decode;
1383
1384                 if (tcd->cp->reduce != 0) {
1385                         tcd->image->comps[compno].resno_decoded =
1386                                 tile->comps[compno].numresolutions - tcd->cp->reduce - 1;
1387                         if (tcd->image->comps[compno].resno_decoded < 0) {
1388                                 opj_event_msg(tcd->cinfo, EVT_ERROR, "Error decoding tile. The number of resolutions to remove is higher than the number "
1389                                         "of resolutions in the original codestream\nModify the cp_reduce parameter.\n");
1390                                 return false;
1391                         }
1392                 }
1393
1394                 numres2decode = tcd->image->comps[compno].resno_decoded + 1;
1395                 if(numres2decode > 0){
1396                         if (tcd->tcp->tccps[compno].qmfbid == 1) {
1397                                 dwt_decode(tilec, numres2decode);
1398                         } else {
1399                                 dwt_decode_real(tilec, numres2decode);
1400                         }
1401                 }
1402         }
1403         dwt_time = opj_clock() - dwt_time;
1404         opj_event_msg(tcd->cinfo, EVT_INFO, "- dwt took %f s\n", dwt_time);
1405
1406         /*----------------MCT-------------------*/
1407
1408         if (tcd->tcp->mct) {
1409                 int n = (tile->comps[0].x1 - tile->comps[0].x0) * (tile->comps[0].y1 - tile->comps[0].y0);
1410                 if (tcd->tcp->tccps[0].qmfbid == 1) {
1411                         mct_decode(
1412                                         tile->comps[0].data,
1413                                         tile->comps[1].data,
1414                                         tile->comps[2].data, 
1415                                         n);
1416                 } else {
1417                         mct_decode_real(
1418                                         (float*)tile->comps[0].data,
1419                                         (float*)tile->comps[1].data,
1420                                         (float*)tile->comps[2].data, 
1421                                         n);
1422                 }
1423         }
1424
1425         /*---------------TILE-------------------*/
1426
1427         for (compno = 0; compno < tile->numcomps; ++compno) {
1428                 opj_tcd_tilecomp_t* tilec = &tile->comps[compno];
1429                 opj_image_comp_t* imagec = &tcd->image->comps[compno];
1430                 opj_tcd_resolution_t* res = &tilec->resolutions[imagec->resno_decoded];
1431                 int adjust = imagec->sgnd ? 0 : 1 << (imagec->prec - 1);
1432                 int min = imagec->sgnd ? -(1 << (imagec->prec - 1)) : 0;
1433                 int max = imagec->sgnd ?  (1 << (imagec->prec - 1)) - 1 : (1 << imagec->prec) - 1;
1434
1435                 int tw = tilec->x1 - tilec->x0;
1436                 int w = imagec->w;
1437
1438                 int offset_x = int_ceildivpow2(imagec->x0, imagec->factor);
1439                 int offset_y = int_ceildivpow2(imagec->y0, imagec->factor);
1440
1441                 int i, j;
1442                 if(!imagec->data){
1443                         imagec->data = (int*) opj_malloc(imagec->w * imagec->h * sizeof(int));
1444                 }
1445                 if(tcd->tcp->tccps[compno].qmfbid == 1) {
1446                         for(j = res->y0; j < res->y1; ++j) {
1447                                 for(i = res->x0; i < res->x1; ++i) {
1448                                         int v = tilec->data[i - res->x0 + (j - res->y0) * tw];
1449                                         v += adjust;
1450                                         imagec->data[(i - offset_x) + (j - offset_y) * w] = int_clamp(v, min, max);
1451                                 }
1452                         }
1453                 }else{
1454                         for(j = res->y0; j < res->y1; ++j) {
1455                                 for(i = res->x0; i < res->x1; ++i) {
1456                                         float tmp = ((float*)tilec->data)[i - res->x0 + (j - res->y0) * tw];
1457                                         int v = lrintf(tmp);
1458                                         v += adjust;
1459                                         imagec->data[(i - offset_x) + (j - offset_y) * w] = int_clamp(v, min, max);
1460                                 }
1461                         }
1462                 }
1463                 opj_aligned_free(tilec->data);
1464         }
1465
1466         tile_time = opj_clock() - tile_time;    /* time needed to decode a tile */
1467         opj_event_msg(tcd->cinfo, EVT_INFO, "- tile decoded in %f s\n", tile_time);
1468
1469         if (eof) {
1470                 return false;
1471         }
1472         
1473         return true;
1474 }
1475
1476 void tcd_free_decode(opj_tcd_t *tcd) {
1477         opj_tcd_image_t *tcd_image = tcd->tcd_image;    
1478         opj_free(tcd_image->tiles);
1479 }
1480
1481 void tcd_free_decode_tile(opj_tcd_t *tcd, int tileno) {
1482         int compno,resno,bandno,precno;
1483
1484         opj_tcd_image_t *tcd_image = tcd->tcd_image;
1485
1486         opj_tcd_tile_t *tile = &tcd_image->tiles[tileno];
1487         for (compno = 0; compno < tile->numcomps; compno++) {
1488                 opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
1489                 for (resno = 0; resno < tilec->numresolutions; resno++) {
1490                         opj_tcd_resolution_t *res = &tilec->resolutions[resno];
1491                         for (bandno = 0; bandno < res->numbands; bandno++) {
1492                                 opj_tcd_band_t *band = &res->bands[bandno];
1493                                 for (precno = 0; precno < res->ph * res->pw; precno++) {
1494                                         opj_tcd_precinct_t *prec = &band->precincts[precno];
1495                                         if (prec->imsbtree != NULL) tgt_destroy(prec->imsbtree);
1496                                         if (prec->incltree != NULL) tgt_destroy(prec->incltree);
1497                                 }
1498                                 opj_free(band->precincts);
1499                         }
1500                 }
1501                 opj_free(tilec->resolutions);
1502         }
1503         opj_free(tile->comps);
1504 }
1505