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