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