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