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