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