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