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