enable all progression orders
[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 * Allocates memory for a decoding code block.
95 */
96 static opj_bool tcd_code_block_dec_allocate (opj_tcd_cblk_dec_v2_t * p_code_block);
97
98 /**
99 Free the memory allocated for encoding
100 @param tcd TCD handle
101 */
102 static void tcd_free_tile(opj_tcd_v2_t *tcd);
103
104
105 opj_bool tcd_t2_decode (
106                                         opj_tcd_v2_t *p_tcd,
107                                         OPJ_BYTE * p_src_data,
108                                         OPJ_UINT32 * p_data_read,
109                                         OPJ_UINT32 p_max_src_size,
110                                         opj_codestream_index_t *p_cstr_index
111                                         );
112
113 opj_bool tcd_t1_decode (
114                                           opj_tcd_v2_t *p_tcd
115                                          );
116
117 opj_bool tcd_dwt_decode (
118                                           opj_tcd_v2_t *p_tcd
119                                          );
120
121 opj_bool tcd_mct_decode (
122                                           opj_tcd_v2_t *p_tcd
123                                          );
124
125 opj_bool tcd_dc_level_shift_decode (
126                                                  opj_tcd_v2_t *p_tcd
127                                                  );
128
129 void tcd_code_block_dec_deallocate (opj_tcd_precinct_v2_t * p_precinct);
130 /* ----------------------------------------------------------------------- */
131
132 /**
133 Create a new TCD handle
134 */
135 opj_tcd_t* tcd_create(opj_common_ptr cinfo) {
136         /* create the tcd structure */
137         opj_tcd_t *tcd = (opj_tcd_t*)opj_malloc(sizeof(opj_tcd_t));
138         if(!tcd) return NULL;
139         tcd->cinfo = cinfo;
140         tcd->tcd_image = (opj_tcd_image_t*)opj_malloc(sizeof(opj_tcd_image_t));
141         if(!tcd->tcd_image) {
142                 opj_free(tcd);
143                 return NULL;
144         }
145
146         return tcd;
147 }
148
149 /**
150 Create a new TCD handle
151 */
152 opj_tcd_v2_t* tcd_create_v2(opj_bool p_is_decoder)
153 {
154         opj_tcd_v2_t *l_tcd = 00;
155
156         /* create the tcd structure */
157         l_tcd = (opj_tcd_v2_t*) opj_malloc(sizeof(opj_tcd_v2_t));
158         if (!l_tcd) {
159                 return 00;
160         }
161         memset(l_tcd,0,sizeof(opj_tcd_v2_t));
162
163         l_tcd->m_is_decoder = p_is_decoder ? 1 : 0;
164
165         l_tcd->tcd_image = (opj_tcd_image_v2_t*)opj_malloc(sizeof(opj_tcd_image_v2_t));
166         if (!l_tcd->tcd_image) {
167                 opj_free(l_tcd);
168                 return 00;
169         }
170         memset(l_tcd->tcd_image,0,sizeof(opj_tcd_image_v2_t));
171
172         return l_tcd;
173 }
174
175 /**
176 Destroy a previously created TCD handle
177 */
178 void tcd_destroy(opj_tcd_t *tcd) {
179         if(tcd) {
180                 opj_free(tcd->tcd_image);
181                 opj_free(tcd);
182         }
183 }
184
185 /* ----------------------------------------------------------------------- */
186
187 void tcd_malloc_encode(opj_tcd_t *tcd, opj_image_t * image, opj_cp_t * cp, int curtileno) {
188         int tileno, compno, resno, bandno, precno, cblkno;
189
190         tcd->image = image;
191         tcd->cp = cp;
192         tcd->tcd_image->tw = cp->tw;
193         tcd->tcd_image->th = cp->th;
194         tcd->tcd_image->tiles = (opj_tcd_tile_t *) opj_malloc(sizeof(opj_tcd_tile_t));
195         
196         for (tileno = 0; tileno < 1; tileno++) {
197                 opj_tcp_t *tcp = &cp->tcps[curtileno];
198                 int j;
199
200                 /* cfr p59 ISO/IEC FDIS15444-1 : 2000 (18 august 2000) */
201                 int p = curtileno % cp->tw;     /* si numerotation matricielle .. */
202                 int q = curtileno / cp->tw;     /* .. coordonnees de la tile (q,p) q pour ligne et p pour colonne */
203
204                 /* opj_tcd_tile_t *tile=&tcd->tcd_image->tiles[tileno]; */
205                 opj_tcd_tile_t *tile = tcd->tcd_image->tiles;
206
207                 /* 4 borders of the tile rescale on the image if necessary */
208                 tile->x0 = int_max(cp->tx0 + p * cp->tdx, image->x0);
209                 tile->y0 = int_max(cp->ty0 + q * cp->tdy, image->y0);
210                 tile->x1 = int_min(cp->tx0 + (p + 1) * cp->tdx, image->x1);
211                 tile->y1 = int_min(cp->ty0 + (q + 1) * cp->tdy, image->y1);
212                 tile->numcomps = image->numcomps;
213                 /* tile->PPT=image->PPT;  */
214
215                 /* Modification of the RATE >> */
216                 for (j = 0; j < tcp->numlayers; j++) {
217                         tcp->rates[j] = tcp->rates[j] ? 
218                                 cp->tp_on ? 
219                                         (((float) (tile->numcomps 
220                                         * (tile->x1 - tile->x0) 
221                                         * (tile->y1 - tile->y0)
222                                         * image->comps[0].prec))
223                                         /(tcp->rates[j] * 8 * image->comps[0].dx * image->comps[0].dy)) - (((tcd->cur_totnum_tp - 1) * 14 )/ tcp->numlayers)
224                                         :
225                                 ((float) (tile->numcomps 
226                                         * (tile->x1 - tile->x0) 
227                                         * (tile->y1 - tile->y0) 
228                                         * image->comps[0].prec))/ 
229                                         (tcp->rates[j] * 8 * image->comps[0].dx * image->comps[0].dy)
230                                         : 0;
231
232                         if (tcp->rates[j]) {
233                                 if (j && tcp->rates[j] < tcp->rates[j - 1] + 10) {
234                                         tcp->rates[j] = tcp->rates[j - 1] + 20;
235                                 } else {
236                                         if (!j && tcp->rates[j] < 30)
237                                                 tcp->rates[j] = 30;
238                                 }
239                                 
240                                 if(j == (tcp->numlayers-1)){
241                                         tcp->rates[j] = tcp->rates[j]- 2;
242                                 }
243                         }
244                 }
245                 /* << Modification of the RATE */
246                 
247                 tile->comps = (opj_tcd_tilecomp_t *) opj_malloc(image->numcomps * sizeof(opj_tcd_tilecomp_t));
248                 for (compno = 0; compno < tile->numcomps; compno++) {
249                         opj_tccp_t *tccp = &tcp->tccps[compno];
250
251                         opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
252
253                         /* border of each tile component (global) */
254                         tilec->x0 = int_ceildiv(tile->x0, image->comps[compno].dx);
255                         tilec->y0 = int_ceildiv(tile->y0, image->comps[compno].dy);
256                         tilec->x1 = int_ceildiv(tile->x1, image->comps[compno].dx);
257                         tilec->y1 = int_ceildiv(tile->y1, image->comps[compno].dy);
258                         
259                         tilec->data = (int *) opj_aligned_malloc((tilec->x1 - tilec->x0) * (tilec->y1 - tilec->y0) * sizeof(int));
260                         tilec->numresolutions = tccp->numresolutions;
261
262                         tilec->resolutions = (opj_tcd_resolution_t *) opj_malloc(tilec->numresolutions * sizeof(opj_tcd_resolution_t));
263                         
264                         for (resno = 0; resno < tilec->numresolutions; resno++) {
265                                 int pdx, pdy;
266                                 int levelno = tilec->numresolutions - 1 - resno;
267                                 int tlprcxstart, tlprcystart, brprcxend, brprcyend;
268                                 int tlcbgxstart, tlcbgystart, brcbgxend, brcbgyend;
269                                 int cbgwidthexpn, cbgheightexpn;
270                                 int cblkwidthexpn, cblkheightexpn;
271
272                                 opj_tcd_resolution_t *res = &tilec->resolutions[resno];
273                                 
274                                 /* border for each resolution level (global) */
275                                 res->x0 = int_ceildivpow2(tilec->x0, levelno);
276                                 res->y0 = int_ceildivpow2(tilec->y0, levelno);
277                                 res->x1 = int_ceildivpow2(tilec->x1, levelno);
278                                 res->y1 = int_ceildivpow2(tilec->y1, levelno);
279                                 
280                                 res->numbands = resno == 0 ? 1 : 3;
281                                 /* p. 35, table A-23, ISO/IEC FDIS154444-1 : 2000 (18 august 2000) */
282                                 if (tccp->csty & J2K_CCP_CSTY_PRT) {
283                                         pdx = tccp->prcw[resno];
284                                         pdy = tccp->prch[resno];
285                                 } else {
286                                         pdx = 15;
287                                         pdy = 15;
288                                 }
289                                 /* p. 64, B.6, ISO/IEC FDIS15444-1 : 2000 (18 august 2000)  */
290                                 tlprcxstart = int_floordivpow2(res->x0, pdx) << pdx;
291                                 tlprcystart = int_floordivpow2(res->y0, pdy) << pdy;
292                                 
293                                 brprcxend = int_ceildivpow2(res->x1, pdx) << pdx;
294                                 brprcyend = int_ceildivpow2(res->y1, pdy) << pdy;
295                                 
296                                 res->pw = (brprcxend - tlprcxstart) >> pdx;
297                                 res->ph = (brprcyend - tlprcystart) >> pdy;
298                                 
299                                 if (resno == 0) {
300                                         tlcbgxstart = tlprcxstart;
301                                         tlcbgystart = tlprcystart;
302                                         brcbgxend = brprcxend;
303                                         brcbgyend = brprcyend;
304                                         cbgwidthexpn = pdx;
305                                         cbgheightexpn = pdy;
306                                 } else {
307                                         tlcbgxstart = int_ceildivpow2(tlprcxstart, 1);
308                                         tlcbgystart = int_ceildivpow2(tlprcystart, 1);
309                                         brcbgxend = int_ceildivpow2(brprcxend, 1);
310                                         brcbgyend = int_ceildivpow2(brprcyend, 1);
311                                         cbgwidthexpn = pdx - 1;
312                                         cbgheightexpn = pdy - 1;
313                                 }
314                                 
315                                 cblkwidthexpn = int_min(tccp->cblkw, cbgwidthexpn);
316                                 cblkheightexpn = int_min(tccp->cblkh, cbgheightexpn);
317                                 
318                                 for (bandno = 0; bandno < res->numbands; bandno++) {
319                                         int x0b, y0b, i;
320                                         int gain, numbps;
321                                         opj_stepsize_t *ss = NULL;
322
323                                         opj_tcd_band_t *band = &res->bands[bandno];
324
325                                         band->bandno = resno == 0 ? 0 : bandno + 1;
326                                         x0b = (band->bandno == 1) || (band->bandno == 3) ? 1 : 0;
327                                         y0b = (band->bandno == 2) || (band->bandno == 3) ? 1 : 0;
328                                         
329                                         if (band->bandno == 0) {
330                                                 /* band border (global) */
331                                                 band->x0 = int_ceildivpow2(tilec->x0, levelno);
332                                                 band->y0 = int_ceildivpow2(tilec->y0, levelno);
333                                                 band->x1 = int_ceildivpow2(tilec->x1, levelno);
334                                                 band->y1 = int_ceildivpow2(tilec->y1, levelno);
335                                         } else {
336                                                 /* band border (global) */
337                                                 band->x0 = int_ceildivpow2(tilec->x0 - (1 << levelno) * x0b, levelno + 1);
338                                                 band->y0 = int_ceildivpow2(tilec->y0 - (1 << levelno) * y0b, levelno + 1);
339                                                 band->x1 = int_ceildivpow2(tilec->x1 - (1 << levelno) * x0b, levelno + 1);
340                                                 band->y1 = int_ceildivpow2(tilec->y1 - (1 << levelno) * y0b, levelno + 1);
341                                         }
342                                         
343                                         ss = &tccp->stepsizes[resno == 0 ? 0 : 3 * (resno - 1) + bandno + 1];
344                                         gain = tccp->qmfbid == 0 ? dwt_getgain_real(band->bandno) : dwt_getgain(band->bandno);                                  
345                                         numbps = image->comps[compno].prec + gain;
346                                         
347                                         band->stepsize = (float)((1.0 + ss->mant / 2048.0) * pow(2.0, numbps - ss->expn));
348                                         band->numbps = ss->expn + tccp->numgbits - 1;   /* WHY -1 ? */
349                                         
350                                         band->precincts = (opj_tcd_precinct_t *) opj_malloc(3 * res->pw * res->ph * sizeof(opj_tcd_precinct_t));
351                                         
352                                         for (i = 0; i < res->pw * res->ph * 3; i++) {
353                                                 band->precincts[i].imsbtree = NULL;
354                                                 band->precincts[i].incltree = NULL;
355                                         }
356                                         
357                                         for (precno = 0; precno < res->pw * res->ph; precno++) {
358                                                 int tlcblkxstart, tlcblkystart, brcblkxend, brcblkyend;
359
360                                                 int cbgxstart = tlcbgxstart + (precno % res->pw) * (1 << cbgwidthexpn);
361                                                 int cbgystart = tlcbgystart + (precno / res->pw) * (1 << cbgheightexpn);
362                                                 int cbgxend = cbgxstart + (1 << cbgwidthexpn);
363                                                 int cbgyend = cbgystart + (1 << cbgheightexpn);
364
365                                                 opj_tcd_precinct_t *prc = &band->precincts[precno];
366
367                                                 /* precinct size (global) */
368                                                 prc->x0 = int_max(cbgxstart, band->x0);
369                                                 prc->y0 = int_max(cbgystart, band->y0);
370                                                 prc->x1 = int_min(cbgxend, band->x1);
371                                                 prc->y1 = int_min(cbgyend, band->y1);
372
373                                                 tlcblkxstart = int_floordivpow2(prc->x0, cblkwidthexpn) << cblkwidthexpn;
374                                                 tlcblkystart = int_floordivpow2(prc->y0, cblkheightexpn) << cblkheightexpn;
375                                                 brcblkxend = int_ceildivpow2(prc->x1, cblkwidthexpn) << cblkwidthexpn;
376                                                 brcblkyend = int_ceildivpow2(prc->y1, cblkheightexpn) << cblkheightexpn;
377                                                 prc->cw = (brcblkxend - tlcblkxstart) >> cblkwidthexpn;
378                                                 prc->ch = (brcblkyend - tlcblkystart) >> cblkheightexpn;
379
380                                                 prc->cblks.enc = (opj_tcd_cblk_enc_t*) opj_calloc((prc->cw * prc->ch), sizeof(opj_tcd_cblk_enc_t));
381                                                 prc->incltree = tgt_create(prc->cw, prc->ch);
382                                                 prc->imsbtree = tgt_create(prc->cw, prc->ch);
383                                                 
384                                                 for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
385                                                         int cblkxstart = tlcblkxstart + (cblkno % prc->cw) * (1 << cblkwidthexpn);
386                                                         int cblkystart = tlcblkystart + (cblkno / prc->cw) * (1 << cblkheightexpn);
387                                                         int cblkxend = cblkxstart + (1 << cblkwidthexpn);
388                                                         int cblkyend = cblkystart + (1 << cblkheightexpn);
389                                                         
390                                                         opj_tcd_cblk_enc_t* cblk = &prc->cblks.enc[cblkno];
391
392                                                         /* code-block size (global) */
393                                                         cblk->x0 = int_max(cblkxstart, prc->x0);
394                                                         cblk->y0 = int_max(cblkystart, prc->y0);
395                                                         cblk->x1 = int_min(cblkxend, prc->x1);
396                                                         cblk->y1 = int_min(cblkyend, prc->y1);
397                                                         cblk->data = (unsigned char*) opj_calloc(8192+2, sizeof(unsigned char));
398                                                         /* FIXME: mqc_init_enc and mqc_byteout underrun the buffer if we don't do this. Why? */
399                                                         cblk->data += 2;
400                                                         cblk->layers = (opj_tcd_layer_t*) opj_calloc(100, sizeof(opj_tcd_layer_t));
401                                                         cblk->passes = (opj_tcd_pass_t*) opj_calloc(100, sizeof(opj_tcd_pass_t));
402                                                 }
403                                         }
404                                 }
405                         }
406                 }
407         }
408         
409         /* tcd_dump(stdout, tcd, &tcd->tcd_image); */
410 }
411
412 void tcd_free_encode(opj_tcd_t *tcd) {
413         int tileno, compno, resno, bandno, precno, cblkno;
414
415         for (tileno = 0; tileno < 1; tileno++) {
416                 opj_tcd_tile_t *tile = tcd->tcd_image->tiles;
417
418                 for (compno = 0; compno < tile->numcomps; compno++) {
419                         opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
420
421                         for (resno = 0; resno < tilec->numresolutions; resno++) {
422                                 opj_tcd_resolution_t *res = &tilec->resolutions[resno];
423
424                                 for (bandno = 0; bandno < res->numbands; bandno++) {
425                                         opj_tcd_band_t *band = &res->bands[bandno];
426
427                                         for (precno = 0; precno < res->pw * res->ph; precno++) {
428                                                 opj_tcd_precinct_t *prc = &band->precincts[precno];
429
430                                                 if (prc->incltree != NULL) {
431                                                         tgt_destroy(prc->incltree);
432                                                         prc->incltree = NULL;
433                                                 }
434                                                 if (prc->imsbtree != NULL) {
435                                                         tgt_destroy(prc->imsbtree);     
436                                                         prc->imsbtree = NULL;
437                                                 }
438                                                 for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
439                                                         opj_free(prc->cblks.enc[cblkno].data - 2);
440                                                         opj_free(prc->cblks.enc[cblkno].layers);
441                                                         opj_free(prc->cblks.enc[cblkno].passes);
442                                                 }
443                                                 opj_free(prc->cblks.enc);
444                                         } /* for (precno */
445                                         opj_free(band->precincts);
446                                         band->precincts = NULL;
447                                 } /* for (bandno */
448                         } /* for (resno */
449                         opj_free(tilec->resolutions);
450                         tilec->resolutions = NULL;
451                 } /* for (compno */
452                 opj_free(tile->comps);
453                 tile->comps = NULL;
454         } /* for (tileno */
455         opj_free(tcd->tcd_image->tiles);
456         tcd->tcd_image->tiles = NULL;
457 }
458
459 void tcd_init_encode(opj_tcd_t *tcd, opj_image_t * image, opj_cp_t * cp, int curtileno) {
460         int tileno, compno, resno, bandno, precno, cblkno;
461
462         for (tileno = 0; tileno < 1; tileno++) {
463                 opj_tcp_t *tcp = &cp->tcps[curtileno];
464                 int j;
465                 /* cfr p59 ISO/IEC FDIS15444-1 : 2000 (18 august 2000) */
466                 int p = curtileno % cp->tw;
467                 int q = curtileno / cp->tw;
468
469                 opj_tcd_tile_t *tile = tcd->tcd_image->tiles;
470                 
471                 /* 4 borders of the tile rescale on the image if necessary */
472                 tile->x0 = int_max(cp->tx0 + p * cp->tdx, image->x0);
473                 tile->y0 = int_max(cp->ty0 + q * cp->tdy, image->y0);
474                 tile->x1 = int_min(cp->tx0 + (p + 1) * cp->tdx, image->x1);
475                 tile->y1 = int_min(cp->ty0 + (q + 1) * cp->tdy, image->y1);
476                 
477                 tile->numcomps = image->numcomps;
478                 /* tile->PPT=image->PPT; */
479
480                 /* Modification of the RATE >> */
481                 for (j = 0; j < tcp->numlayers; j++) {
482                         tcp->rates[j] = tcp->rates[j] ? 
483                                 cp->tp_on ? 
484                                         (((float) (tile->numcomps 
485                                         * (tile->x1 - tile->x0) 
486                                         * (tile->y1 - tile->y0)
487                                         * image->comps[0].prec))
488                                         /(tcp->rates[j] * 8 * image->comps[0].dx * image->comps[0].dy)) - (((tcd->cur_totnum_tp - 1) * 14 )/ tcp->numlayers)
489                                         :
490                                 ((float) (tile->numcomps 
491                                         * (tile->x1 - tile->x0) 
492                                         * (tile->y1 - tile->y0) 
493                                         * image->comps[0].prec))/ 
494                                         (tcp->rates[j] * 8 * image->comps[0].dx * image->comps[0].dy)
495                                         : 0;
496
497                         if (tcp->rates[j]) {
498                                 if (j && tcp->rates[j] < tcp->rates[j - 1] + 10) {
499                                         tcp->rates[j] = tcp->rates[j - 1] + 20;
500                                 } else {
501                                         if (!j && tcp->rates[j] < 30)
502                                                 tcp->rates[j] = 30;
503                                 }
504                         }
505                 }
506                 /* << Modification of the RATE */
507
508                 /* tile->comps=(opj_tcd_tilecomp_t*)opj_realloc(tile->comps,image->numcomps*sizeof(opj_tcd_tilecomp_t)); */
509                 for (compno = 0; compno < tile->numcomps; compno++) {
510                         opj_tccp_t *tccp = &tcp->tccps[compno];
511                         
512                         opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
513
514                         /* border of each tile component (global) */
515                         tilec->x0 = int_ceildiv(tile->x0, image->comps[compno].dx);
516                         tilec->y0 = int_ceildiv(tile->y0, image->comps[compno].dy);
517                         tilec->x1 = int_ceildiv(tile->x1, image->comps[compno].dx);
518                         tilec->y1 = int_ceildiv(tile->y1, image->comps[compno].dy);
519                         
520                         tilec->data = (int *) opj_aligned_malloc((tilec->x1 - tilec->x0) * (tilec->y1 - tilec->y0) * sizeof(int));
521                         tilec->numresolutions = tccp->numresolutions;
522                         /* tilec->resolutions=(opj_tcd_resolution_t*)opj_realloc(tilec->resolutions,tilec->numresolutions*sizeof(opj_tcd_resolution_t)); */
523                         for (resno = 0; resno < tilec->numresolutions; resno++) {
524                                 int pdx, pdy;
525
526                                 int levelno = tilec->numresolutions - 1 - resno;
527                                 int tlprcxstart, tlprcystart, brprcxend, brprcyend;
528                                 int tlcbgxstart, tlcbgystart, brcbgxend, brcbgyend;
529                                 int cbgwidthexpn, cbgheightexpn;
530                                 int cblkwidthexpn, cblkheightexpn;
531                                 
532                                 opj_tcd_resolution_t *res = &tilec->resolutions[resno];
533
534                                 /* border for each resolution level (global) */
535                                 res->x0 = int_ceildivpow2(tilec->x0, levelno);
536                                 res->y0 = int_ceildivpow2(tilec->y0, levelno);
537                                 res->x1 = int_ceildivpow2(tilec->x1, levelno);
538                                 res->y1 = int_ceildivpow2(tilec->y1, levelno);  
539                                 res->numbands = resno == 0 ? 1 : 3;
540
541                                 /* p. 35, table A-23, ISO/IEC FDIS154444-1 : 2000 (18 august 2000) */
542                                 if (tccp->csty & J2K_CCP_CSTY_PRT) {
543                                         pdx = tccp->prcw[resno];
544                                         pdy = tccp->prch[resno];
545                                 } else {
546                                         pdx = 15;
547                                         pdy = 15;
548                                 }
549                                 /* p. 64, B.6, ISO/IEC FDIS15444-1 : 2000 (18 august 2000)  */
550                                 tlprcxstart = int_floordivpow2(res->x0, pdx) << pdx;
551                                 tlprcystart = int_floordivpow2(res->y0, pdy) << pdy;
552                                 brprcxend = int_ceildivpow2(res->x1, pdx) << pdx;
553                                 brprcyend = int_ceildivpow2(res->y1, pdy) << pdy;
554                                 
555                                 res->pw = (brprcxend - tlprcxstart) >> pdx;
556                                 res->ph = (brprcyend - tlprcystart) >> pdy;
557                                 
558                                 if (resno == 0) {
559                                         tlcbgxstart = tlprcxstart;
560                                         tlcbgystart = tlprcystart;
561                                         brcbgxend = brprcxend;
562                                         brcbgyend = brprcyend;
563                                         cbgwidthexpn = pdx;
564                                         cbgheightexpn = pdy;
565                                 } else {
566                                         tlcbgxstart = int_ceildivpow2(tlprcxstart, 1);
567                                         tlcbgystart = int_ceildivpow2(tlprcystart, 1);
568                                         brcbgxend = int_ceildivpow2(brprcxend, 1);
569                                         brcbgyend = int_ceildivpow2(brprcyend, 1);
570                                         cbgwidthexpn = pdx - 1;
571                                         cbgheightexpn = pdy - 1;
572                                 }
573                                 
574                                 cblkwidthexpn = int_min(tccp->cblkw, cbgwidthexpn);
575                                 cblkheightexpn = int_min(tccp->cblkh, cbgheightexpn);
576                                 
577                                 for (bandno = 0; bandno < res->numbands; bandno++) {
578                                         int x0b, y0b;
579                                         int gain, numbps;
580                                         opj_stepsize_t *ss = NULL;
581
582                                         opj_tcd_band_t *band = &res->bands[bandno];
583
584                                         band->bandno = resno == 0 ? 0 : bandno + 1;
585                                         x0b = (band->bandno == 1) || (band->bandno == 3) ? 1 : 0;
586                                         y0b = (band->bandno == 2) || (band->bandno == 3) ? 1 : 0;
587                                         
588                                         if (band->bandno == 0) {
589                                                 /* band border */
590                                                 band->x0 = int_ceildivpow2(tilec->x0, levelno);
591                                                 band->y0 = int_ceildivpow2(tilec->y0, levelno);
592                                                 band->x1 = int_ceildivpow2(tilec->x1, levelno);
593                                                 band->y1 = int_ceildivpow2(tilec->y1, levelno);
594                                         } else {
595                                                 band->x0 = int_ceildivpow2(tilec->x0 - (1 << levelno) * x0b, levelno + 1);
596                                                 band->y0 = int_ceildivpow2(tilec->y0 - (1 << levelno) * y0b, levelno + 1);
597                                                 band->x1 = int_ceildivpow2(tilec->x1 - (1 << levelno) * x0b, levelno + 1);
598                                                 band->y1 = int_ceildivpow2(tilec->y1 - (1 << levelno) * y0b, levelno + 1);
599                                         }
600                                         
601                                         ss = &tccp->stepsizes[resno == 0 ? 0 : 3 * (resno - 1) + bandno + 1];
602                                         gain = tccp->qmfbid == 0 ? dwt_getgain_real(band->bandno) : dwt_getgain(band->bandno);
603                                         numbps = image->comps[compno].prec + gain;
604                                         band->stepsize = (float)((1.0 + ss->mant / 2048.0) * pow(2.0, numbps - ss->expn));
605                                         band->numbps = ss->expn + tccp->numgbits - 1;   /* WHY -1 ? */
606                                         
607                                         for (precno = 0; precno < res->pw * res->ph; precno++) {
608                                                 int tlcblkxstart, tlcblkystart, brcblkxend, brcblkyend;
609
610                                                 int cbgxstart = tlcbgxstart + (precno % res->pw) * (1 << cbgwidthexpn);
611                                                 int cbgystart = tlcbgystart + (precno / res->pw) * (1 << cbgheightexpn);
612                                                 int cbgxend = cbgxstart + (1 << cbgwidthexpn);
613                                                 int cbgyend = cbgystart + (1 << cbgheightexpn);
614                                                 
615                                                 opj_tcd_precinct_t *prc = &band->precincts[precno];
616
617                                                 /* precinct size (global) */
618                                                 prc->x0 = int_max(cbgxstart, band->x0);
619                                                 prc->y0 = int_max(cbgystart, band->y0);
620                                                 prc->x1 = int_min(cbgxend, band->x1);
621                                                 prc->y1 = int_min(cbgyend, band->y1);
622
623                                                 tlcblkxstart = int_floordivpow2(prc->x0, cblkwidthexpn) << cblkwidthexpn;
624                                                 tlcblkystart = int_floordivpow2(prc->y0, cblkheightexpn) << cblkheightexpn;
625                                                 brcblkxend = int_ceildivpow2(prc->x1, cblkwidthexpn) << cblkwidthexpn;
626                                                 brcblkyend = int_ceildivpow2(prc->y1, cblkheightexpn) << cblkheightexpn;
627                                                 prc->cw = (brcblkxend - tlcblkxstart) >> cblkwidthexpn;
628                                                 prc->ch = (brcblkyend - tlcblkystart) >> cblkheightexpn;
629
630                                                 opj_free(prc->cblks.enc);
631                                                 prc->cblks.enc = (opj_tcd_cblk_enc_t*) opj_calloc(prc->cw * prc->ch, sizeof(opj_tcd_cblk_enc_t));
632
633                                                 if (prc->incltree != NULL) {
634                                                         tgt_destroy(prc->incltree);
635                                                 }
636                                                 if (prc->imsbtree != NULL) {
637                                                         tgt_destroy(prc->imsbtree);
638                                                 }
639                                                 
640                                                 prc->incltree = tgt_create(prc->cw, prc->ch);
641                                                 prc->imsbtree = tgt_create(prc->cw, prc->ch);
642
643                                                 for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
644                                                         int cblkxstart = tlcblkxstart + (cblkno % prc->cw) * (1 << cblkwidthexpn);
645                                                         int cblkystart = tlcblkystart + (cblkno / prc->cw) * (1 << cblkheightexpn);
646                                                         int cblkxend = cblkxstart + (1 << cblkwidthexpn);
647                                                         int cblkyend = cblkystart + (1 << cblkheightexpn);
648
649                                                         opj_tcd_cblk_enc_t* cblk = &prc->cblks.enc[cblkno];
650
651                                                         /* code-block size (global) */
652                                                         cblk->x0 = int_max(cblkxstart, prc->x0);
653                                                         cblk->y0 = int_max(cblkystart, prc->y0);
654                                                         cblk->x1 = int_min(cblkxend, prc->x1);
655                                                         cblk->y1 = int_min(cblkyend, prc->y1);
656                                                         cblk->data = (unsigned char*) opj_calloc(8192+2, sizeof(unsigned char));
657                                                         /* FIXME: mqc_init_enc and mqc_byteout underrun the buffer if we don't do this. Why? */
658                                                         cblk->data += 2;
659                                                         cblk->layers = (opj_tcd_layer_t*) opj_calloc(100, sizeof(opj_tcd_layer_t));
660                                                         cblk->passes = (opj_tcd_pass_t*) opj_calloc(100, sizeof(opj_tcd_pass_t));
661                                                 }
662                                         } /* precno */
663                                 } /* bandno */
664                         } /* resno */
665                 } /* compno */
666         } /* tileno */
667
668         /* tcd_dump(stdout, tcd, &tcd->tcd_image); */
669 }
670
671 void tcd_malloc_decode(opj_tcd_t *tcd, opj_image_t * image, opj_cp_t * cp) {
672         int i, j, tileno, p, q;
673         unsigned int x0 = 0, y0 = 0, x1 = 0, y1 = 0, w, h;
674
675         tcd->image = image;
676         tcd->tcd_image->tw = cp->tw;
677         tcd->tcd_image->th = cp->th;
678         tcd->tcd_image->tiles = (opj_tcd_tile_t *) opj_malloc(cp->tw * cp->th * sizeof(opj_tcd_tile_t));
679
680         /* 
681         Allocate place to store the decoded data = final image
682         Place limited by the tile really present in the codestream 
683         */
684
685         for (j = 0; j < cp->tileno_size; j++) {
686                 opj_tcd_tile_t *tile;
687                 
688                 tileno = cp->tileno[j];         
689                 tile = &(tcd->tcd_image->tiles[cp->tileno[tileno]]);            
690                 tile->numcomps = image->numcomps;
691                 tile->comps = (opj_tcd_tilecomp_t*) opj_calloc(image->numcomps, sizeof(opj_tcd_tilecomp_t));
692         }
693
694         for (i = 0; i < image->numcomps; i++) {
695                 for (j = 0; j < cp->tileno_size; j++) {
696                         opj_tcd_tile_t *tile;
697                         opj_tcd_tilecomp_t *tilec;
698                         
699                         /* cfr p59 ISO/IEC FDIS15444-1 : 2000 (18 august 2000) */
700                         
701                         tileno = cp->tileno[j];
702                         
703                         tile = &(tcd->tcd_image->tiles[cp->tileno[tileno]]);
704                         tilec = &tile->comps[i];
705                         
706                         p = tileno % cp->tw;    /* si numerotation matricielle .. */
707                         q = tileno / cp->tw;    /* .. coordonnees de la tile (q,p) q pour ligne et p pour colonne */
708                         
709                         /* 4 borders of the tile rescale on the image if necessary */
710                         tile->x0 = int_max(cp->tx0 + p * cp->tdx, image->x0);
711                         tile->y0 = int_max(cp->ty0 + q * cp->tdy, image->y0);
712                         tile->x1 = int_min(cp->tx0 + (p + 1) * cp->tdx, image->x1);
713                         tile->y1 = int_min(cp->ty0 + (q + 1) * cp->tdy, image->y1);
714
715                         tilec->x0 = int_ceildiv(tile->x0, image->comps[i].dx);
716                         tilec->y0 = int_ceildiv(tile->y0, image->comps[i].dy);
717                         tilec->x1 = int_ceildiv(tile->x1, image->comps[i].dx);
718                         tilec->y1 = int_ceildiv(tile->y1, image->comps[i].dy);
719
720                         x0 = j == 0 ? tilec->x0 : int_min(x0, (unsigned int) tilec->x0);
721                         y0 = j == 0 ? tilec->y0 : int_min(y0,   (unsigned int) tilec->x0);
722                         x1 = j == 0 ? tilec->x1 : int_max(x1,   (unsigned int) tilec->x1);
723                         y1 = j == 0 ? tilec->y1 : int_max(y1,   (unsigned int) tilec->y1);
724                 }
725
726                 w = int_ceildivpow2(x1 - x0, image->comps[i].factor);
727                 h = int_ceildivpow2(y1 - y0, image->comps[i].factor);
728
729                 image->comps[i].w = w;
730                 image->comps[i].h = h;
731                 image->comps[i].x0 = x0;
732                 image->comps[i].y0 = y0;
733         }
734 }
735
736 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) {
737         int compno, resno, bandno, precno, cblkno;
738         opj_tcp_t *tcp;
739         opj_tcd_tile_t *tile;
740
741         OPJ_ARG_NOT_USED(cstr_info);
742
743         tcd->cp = cp;
744         
745         tcp = &(cp->tcps[cp->tileno[tileno]]);
746         tile = &(tcd->tcd_image->tiles[cp->tileno[tileno]]);
747         
748         tileno = cp->tileno[tileno];
749         
750         for (compno = 0; compno < tile->numcomps; compno++) {
751                 opj_tccp_t *tccp = &tcp->tccps[compno];
752                 opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
753                 
754                 /* border of each tile component (global) */
755                 tilec->x0 = int_ceildiv(tile->x0, image->comps[compno].dx);
756                 tilec->y0 = int_ceildiv(tile->y0, image->comps[compno].dy);
757                 tilec->x1 = int_ceildiv(tile->x1, image->comps[compno].dx);
758                 tilec->y1 = int_ceildiv(tile->y1, image->comps[compno].dy);
759
760                 tilec->numresolutions = tccp->numresolutions;
761                 tilec->resolutions = (opj_tcd_resolution_t *) opj_malloc(tilec->numresolutions * sizeof(opj_tcd_resolution_t));
762                 
763                 for (resno = 0; resno < tilec->numresolutions; resno++) {
764                         int pdx, pdy;
765                         int levelno = tilec->numresolutions - 1 - resno;
766                         int tlprcxstart, tlprcystart, brprcxend, brprcyend;
767                         int tlcbgxstart, tlcbgystart, brcbgxend, brcbgyend;
768                         int cbgwidthexpn, cbgheightexpn;
769                         int cblkwidthexpn, cblkheightexpn;
770                         
771                         opj_tcd_resolution_t *res = &tilec->resolutions[resno];
772                         
773                         /* border for each resolution level (global) */
774                         res->x0 = int_ceildivpow2(tilec->x0, levelno);
775                         res->y0 = int_ceildivpow2(tilec->y0, levelno);
776                         res->x1 = int_ceildivpow2(tilec->x1, levelno);
777                         res->y1 = int_ceildivpow2(tilec->y1, levelno);
778                         res->numbands = resno == 0 ? 1 : 3;
779                         
780                         /* p. 35, table A-23, ISO/IEC FDIS154444-1 : 2000 (18 august 2000) */
781                         if (tccp->csty & J2K_CCP_CSTY_PRT) {
782                                 pdx = tccp->prcw[resno];
783                                 pdy = tccp->prch[resno];
784                         } else {
785                                 pdx = 15;
786                                 pdy = 15;
787                         }                       
788                         
789                         /* p. 64, B.6, ISO/IEC FDIS15444-1 : 2000 (18 august 2000)  */
790                         tlprcxstart = int_floordivpow2(res->x0, pdx) << pdx;
791                         tlprcystart = int_floordivpow2(res->y0, pdy) << pdy;
792                         brprcxend = int_ceildivpow2(res->x1, pdx) << pdx;
793                         brprcyend = int_ceildivpow2(res->y1, pdy) << pdy;
794                         
795                         res->pw = (res->x0 == res->x1) ? 0 : ((brprcxend - tlprcxstart) >> pdx);
796                         res->ph = (res->y0 == res->y1) ? 0 : ((brprcyend - tlprcystart) >> pdy);
797                         
798                         if (resno == 0) {
799                                 tlcbgxstart = tlprcxstart;
800                                 tlcbgystart = tlprcystart;
801                                 brcbgxend = brprcxend;
802                                 brcbgyend = brprcyend;
803                                 cbgwidthexpn = pdx;
804                                 cbgheightexpn = pdy;
805                         } else {
806                                 tlcbgxstart = int_ceildivpow2(tlprcxstart, 1);
807                                 tlcbgystart = int_ceildivpow2(tlprcystart, 1);
808                                 brcbgxend = int_ceildivpow2(brprcxend, 1);
809                                 brcbgyend = int_ceildivpow2(brprcyend, 1);
810                                 cbgwidthexpn = pdx - 1;
811                                 cbgheightexpn = pdy - 1;
812                         }
813                         
814                         cblkwidthexpn = int_min(tccp->cblkw, cbgwidthexpn);
815                         cblkheightexpn = int_min(tccp->cblkh, cbgheightexpn);
816                         
817                         for (bandno = 0; bandno < res->numbands; bandno++) {
818                                 int x0b, y0b;
819                                 int gain, numbps;
820                                 opj_stepsize_t *ss = NULL;
821                                 
822                                 opj_tcd_band_t *band = &res->bands[bandno];
823                                 band->bandno = resno == 0 ? 0 : bandno + 1;
824                                 x0b = (band->bandno == 1) || (band->bandno == 3) ? 1 : 0;
825                                 y0b = (band->bandno == 2) || (band->bandno == 3) ? 1 : 0;
826                                 
827                                 if (band->bandno == 0) {
828                                         /* band border (global) */
829                                         band->x0 = int_ceildivpow2(tilec->x0, levelno);
830                                         band->y0 = int_ceildivpow2(tilec->y0, levelno);
831                                         band->x1 = int_ceildivpow2(tilec->x1, levelno);
832                                         band->y1 = int_ceildivpow2(tilec->y1, levelno);
833                                 } else {
834                                         /* band border (global) */
835                                         band->x0 = int_ceildivpow2(tilec->x0 - (1 << levelno) * x0b, levelno + 1);
836                                         band->y0 = int_ceildivpow2(tilec->y0 - (1 << levelno) * y0b, levelno + 1);
837                                         band->x1 = int_ceildivpow2(tilec->x1 - (1 << levelno) * x0b, levelno + 1);
838                                         band->y1 = int_ceildivpow2(tilec->y1 - (1 << levelno) * y0b, levelno + 1);
839                                 }
840                                 
841                                 ss = &tccp->stepsizes[resno == 0 ? 0 : 3 * (resno - 1) + bandno + 1];
842                                 gain = tccp->qmfbid == 0 ? dwt_getgain_real(band->bandno) : dwt_getgain(band->bandno);
843                                 numbps = image->comps[compno].prec + gain;
844                                 band->stepsize = (float)(((1.0 + ss->mant / 2048.0) * pow(2.0, numbps - ss->expn)) * 0.5);
845                                 band->numbps = ss->expn + tccp->numgbits - 1;   /* WHY -1 ? */
846                                 
847                                 band->precincts = (opj_tcd_precinct_t *) opj_malloc(res->pw * res->ph * sizeof(opj_tcd_precinct_t));
848                                 
849                                 for (precno = 0; precno < res->pw * res->ph; precno++) {
850                                         int tlcblkxstart, tlcblkystart, brcblkxend, brcblkyend;
851                                         int cbgxstart = tlcbgxstart + (precno % res->pw) * (1 << cbgwidthexpn);
852                                         int cbgystart = tlcbgystart + (precno / res->pw) * (1 << cbgheightexpn);
853                                         int cbgxend = cbgxstart + (1 << cbgwidthexpn);
854                                         int cbgyend = cbgystart + (1 << cbgheightexpn);
855                                         
856                                         opj_tcd_precinct_t *prc = &band->precincts[precno];
857                                         /* precinct size (global) */
858                                         prc->x0 = int_max(cbgxstart, band->x0);
859                                         prc->y0 = int_max(cbgystart, band->y0);
860                                         prc->x1 = int_min(cbgxend, band->x1);
861                                         prc->y1 = int_min(cbgyend, band->y1);
862                                         
863                                         tlcblkxstart = int_floordivpow2(prc->x0, cblkwidthexpn) << cblkwidthexpn;
864                                         tlcblkystart = int_floordivpow2(prc->y0, cblkheightexpn) << cblkheightexpn;
865                                         brcblkxend = int_ceildivpow2(prc->x1, cblkwidthexpn) << cblkwidthexpn;
866                                         brcblkyend = int_ceildivpow2(prc->y1, cblkheightexpn) << cblkheightexpn;
867                                         prc->cw = (brcblkxend - tlcblkxstart) >> cblkwidthexpn;
868                                         prc->ch = (brcblkyend - tlcblkystart) >> cblkheightexpn;
869
870                                         prc->cblks.dec = (opj_tcd_cblk_dec_t*) opj_malloc(prc->cw * prc->ch * sizeof(opj_tcd_cblk_dec_t));
871
872                                         prc->incltree = tgt_create(prc->cw, prc->ch);
873                                         prc->imsbtree = tgt_create(prc->cw, prc->ch);
874                                         
875                                         for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
876                                                 int cblkxstart = tlcblkxstart + (cblkno % prc->cw) * (1 << cblkwidthexpn);
877                                                 int cblkystart = tlcblkystart + (cblkno / prc->cw) * (1 << cblkheightexpn);
878                                                 int cblkxend = cblkxstart + (1 << cblkwidthexpn);
879                                                 int cblkyend = cblkystart + (1 << cblkheightexpn);                                      
880
881                                                 opj_tcd_cblk_dec_t* cblk = &prc->cblks.dec[cblkno];
882                                                 cblk->data = NULL;
883                                                 cblk->segs = NULL;
884                                                 /* code-block size (global) */
885                                                 cblk->x0 = int_max(cblkxstart, prc->x0);
886                                                 cblk->y0 = int_max(cblkystart, prc->y0);
887                                                 cblk->x1 = int_min(cblkxend, prc->x1);
888                                                 cblk->y1 = int_min(cblkyend, prc->y1);
889                                                 cblk->numsegs = 0;
890                                         }
891                                 } /* precno */
892                         } /* bandno */
893                 } /* resno */
894         } /* compno */
895         /* tcd_dump(stdout, tcd, &tcd->tcd_image); */
896 }
897
898 void tcd_makelayer_fixed(opj_tcd_t *tcd, int layno, int final) {
899         int compno, resno, bandno, precno, cblkno;
900         int value;                      /*, matrice[tcd_tcp->numlayers][tcd_tile->comps[0].numresolutions][3]; */
901         int matrice[10][10][3];
902         int i, j, k;
903
904         opj_cp_t *cp = tcd->cp;
905         opj_tcd_tile_t *tcd_tile = tcd->tcd_tile;
906         opj_tcp_t *tcd_tcp = tcd->tcp;
907
908         /*matrice=(int*)opj_malloc(tcd_tcp->numlayers*tcd_tile->comps[0].numresolutions*3*sizeof(int)); */
909         
910         for (compno = 0; compno < tcd_tile->numcomps; compno++) {
911                 opj_tcd_tilecomp_t *tilec = &tcd_tile->comps[compno];
912                 for (i = 0; i < tcd_tcp->numlayers; i++) {
913                         for (j = 0; j < tilec->numresolutions; j++) {
914                                 for (k = 0; k < 3; k++) {
915                                         matrice[i][j][k] =
916                                                 (int) (cp->matrice[i * tilec->numresolutions * 3 + j * 3 + k] 
917                                                 * (float) (tcd->image->comps[compno].prec / 16.0));
918                                 }
919                         }
920                 }
921         
922                 for (resno = 0; resno < tilec->numresolutions; resno++) {
923                         opj_tcd_resolution_t *res = &tilec->resolutions[resno];
924                         for (bandno = 0; bandno < res->numbands; bandno++) {
925                                 opj_tcd_band_t *band = &res->bands[bandno];
926                                 for (precno = 0; precno < res->pw * res->ph; precno++) {
927                                         opj_tcd_precinct_t *prc = &band->precincts[precno];
928                                         for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
929                                                 opj_tcd_cblk_enc_t *cblk = &prc->cblks.enc[cblkno];
930                                                 opj_tcd_layer_t *layer = &cblk->layers[layno];
931                                                 int n;
932                                                 int imsb = tcd->image->comps[compno].prec - cblk->numbps;       /* number of bit-plan equal to zero */
933                                                 /* Correction of the matrix of coefficient to include the IMSB information */
934                                                 if (layno == 0) {
935                                                         value = matrice[layno][resno][bandno];
936                                                         if (imsb >= value) {
937                                                                 value = 0;
938                                                         } else {
939                                                                 value -= imsb;
940                                                         }
941                                                 } else {
942                                                         value = matrice[layno][resno][bandno] - matrice[layno - 1][resno][bandno];
943                                                         if (imsb >= matrice[layno - 1][resno][bandno]) {
944                                                                 value -= (imsb - matrice[layno - 1][resno][bandno]);
945                                                                 if (value < 0) {
946                                                                         value = 0;
947                                                                 }
948                                                         }
949                                                 }
950                                                 
951                                                 if (layno == 0) {
952                                                         cblk->numpassesinlayers = 0;
953                                                 }
954                                                 
955                                                 n = cblk->numpassesinlayers;
956                                                 if (cblk->numpassesinlayers == 0) {
957                                                         if (value != 0) {
958                                                                 n = 3 * value - 2 + cblk->numpassesinlayers;
959                                                         } else {
960                                                                 n = cblk->numpassesinlayers;
961                                                         }
962                                                 } else {
963                                                         n = 3 * value + cblk->numpassesinlayers;
964                                                 }
965                                                 
966                                                 layer->numpasses = n - cblk->numpassesinlayers;
967                                                 
968                                                 if (!layer->numpasses)
969                                                         continue;
970                                                 
971                                                 if (cblk->numpassesinlayers == 0) {
972                                                         layer->len = cblk->passes[n - 1].rate;
973                                                         layer->data = cblk->data;
974                                                 } else {
975                                                         layer->len = cblk->passes[n - 1].rate - cblk->passes[cblk->numpassesinlayers - 1].rate;
976                                                         layer->data = cblk->data + cblk->passes[cblk->numpassesinlayers - 1].rate;
977                                                 }
978                                                 if (final)
979                                                         cblk->numpassesinlayers = n;
980                                         }
981                                 }
982                         }
983                 }
984         }
985 }
986
987 void tcd_rateallocate_fixed(opj_tcd_t *tcd) {
988         int layno;
989         for (layno = 0; layno < tcd->tcp->numlayers; layno++) {
990                 tcd_makelayer_fixed(tcd, layno, 1);
991         }
992 }
993
994 void tcd_makelayer(opj_tcd_t *tcd, int layno, double thresh, int final) {
995         int compno, resno, bandno, precno, cblkno, passno;
996         
997         opj_tcd_tile_t *tcd_tile = tcd->tcd_tile;
998
999         tcd_tile->distolayer[layno] = 0;        /* fixed_quality */
1000         
1001         for (compno = 0; compno < tcd_tile->numcomps; compno++) {
1002                 opj_tcd_tilecomp_t *tilec = &tcd_tile->comps[compno];
1003                 for (resno = 0; resno < tilec->numresolutions; resno++) {
1004                         opj_tcd_resolution_t *res = &tilec->resolutions[resno];
1005                         for (bandno = 0; bandno < res->numbands; bandno++) {
1006                                 opj_tcd_band_t *band = &res->bands[bandno];
1007                                 for (precno = 0; precno < res->pw * res->ph; precno++) {
1008                                         opj_tcd_precinct_t *prc = &band->precincts[precno];
1009                                         for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
1010                                                 opj_tcd_cblk_enc_t *cblk = &prc->cblks.enc[cblkno];
1011                                                 opj_tcd_layer_t *layer = &cblk->layers[layno];
1012                                                 
1013                                                 int n;
1014                                                 if (layno == 0) {
1015                                                         cblk->numpassesinlayers = 0;
1016                                                 }
1017                                                 n = cblk->numpassesinlayers;
1018                                                 for (passno = cblk->numpassesinlayers; passno < cblk->totalpasses; passno++) {
1019                                                         int dr;
1020                                                         double dd;
1021                                                         opj_tcd_pass_t *pass = &cblk->passes[passno];
1022                                                         if (n == 0) {
1023                                                                 dr = pass->rate;
1024                                                                 dd = pass->distortiondec;
1025                                                         } else {
1026                                                                 dr = pass->rate - cblk->passes[n - 1].rate;
1027                                                                 dd = pass->distortiondec - cblk->passes[n - 1].distortiondec;
1028                                                         }
1029                                                         if (!dr) {
1030                                                                 if (dd != 0)
1031                                                                         n = passno + 1;
1032                                                                 continue;
1033                                                         }
1034                                                         if (dd / dr >= thresh)
1035                                                                 n = passno + 1;
1036                                                 }
1037                                                 layer->numpasses = n - cblk->numpassesinlayers;
1038                                                 
1039                                                 if (!layer->numpasses) {
1040                                                         layer->disto = 0;
1041                                                         continue;
1042                                                 }
1043                                                 if (cblk->numpassesinlayers == 0) {
1044                                                         layer->len = cblk->passes[n - 1].rate;
1045                                                         layer->data = cblk->data;
1046                                                         layer->disto = cblk->passes[n - 1].distortiondec;
1047                                                 } else {
1048                                                         layer->len = cblk->passes[n - 1].rate - cblk->passes[cblk->numpassesinlayers - 1].rate;
1049                                                         layer->data = cblk->data + cblk->passes[cblk->numpassesinlayers - 1].rate;
1050                                                         layer->disto = cblk->passes[n - 1].distortiondec - cblk->passes[cblk->numpassesinlayers - 1].distortiondec;
1051                                                 }
1052                                                 
1053                                                 tcd_tile->distolayer[layno] += layer->disto;    /* fixed_quality */
1054                                                 
1055                                                 if (final)
1056                                                         cblk->numpassesinlayers = n;
1057                                         }
1058                                 }
1059                         }
1060                 }
1061         }
1062 }
1063
1064 opj_bool tcd_rateallocate(opj_tcd_t *tcd, unsigned char *dest, int len, opj_codestream_info_t *cstr_info) {
1065         int compno, resno, bandno, precno, cblkno, passno, layno;
1066         double min, max;
1067         double cumdisto[100];   /* fixed_quality */
1068         const double K = 1;             /* 1.1; fixed_quality */
1069         double maxSE = 0;
1070
1071         opj_cp_t *cp = tcd->cp;
1072         opj_tcd_tile_t *tcd_tile = tcd->tcd_tile;
1073         opj_tcp_t *tcd_tcp = tcd->tcp;
1074
1075         min = DBL_MAX;
1076         max = 0;
1077         
1078         tcd_tile->numpix = 0;           /* fixed_quality */
1079         
1080         for (compno = 0; compno < tcd_tile->numcomps; compno++) {
1081                 opj_tcd_tilecomp_t *tilec = &tcd_tile->comps[compno];
1082                 tilec->numpix = 0;
1083
1084                 for (resno = 0; resno < tilec->numresolutions; resno++) {
1085                         opj_tcd_resolution_t *res = &tilec->resolutions[resno];
1086
1087                         for (bandno = 0; bandno < res->numbands; bandno++) {
1088                                 opj_tcd_band_t *band = &res->bands[bandno];
1089
1090                                 for (precno = 0; precno < res->pw * res->ph; precno++) {
1091                                         opj_tcd_precinct_t *prc = &band->precincts[precno];
1092
1093                                         for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
1094                                                 opj_tcd_cblk_enc_t *cblk = &prc->cblks.enc[cblkno];
1095
1096                                                 for (passno = 0; passno < cblk->totalpasses; passno++) {
1097                                                         opj_tcd_pass_t *pass = &cblk->passes[passno];
1098                                                         int dr;
1099                                                         double dd, rdslope;
1100                                                         if (passno == 0) {
1101                                                                 dr = pass->rate;
1102                                                                 dd = pass->distortiondec;
1103                                                         } else {
1104                                                                 dr = pass->rate - cblk->passes[passno - 1].rate;
1105                                                                 dd = pass->distortiondec - cblk->passes[passno - 1].distortiondec;
1106                                                         }
1107                                                         if (dr == 0) {
1108                                                                 continue;
1109                                                         }
1110                                                         rdslope = dd / dr;
1111                                                         if (rdslope < min) {
1112                                                                 min = rdslope;
1113                                                         }
1114                                                         if (rdslope > max) {
1115                                                                 max = rdslope;
1116                                                         }
1117                                                 } /* passno */
1118                                                 
1119                                                 /* fixed_quality */
1120                                                 tcd_tile->numpix += ((cblk->x1 - cblk->x0) * (cblk->y1 - cblk->y0));
1121                                                 tilec->numpix += ((cblk->x1 - cblk->x0) * (cblk->y1 - cblk->y0));
1122                                         } /* cbklno */
1123                                 } /* precno */
1124                         } /* bandno */
1125                 } /* resno */
1126                 
1127                 maxSE += (((double)(1 << tcd->image->comps[compno].prec) - 1.0) 
1128                         * ((double)(1 << tcd->image->comps[compno].prec) -1.0)) 
1129                         * ((double)(tilec->numpix));
1130         } /* compno */
1131         
1132         /* index file */
1133         if(cstr_info) {
1134                 opj_tile_info_t *tile_info = &cstr_info->tile[tcd->tcd_tileno];
1135                 tile_info->numpix = tcd_tile->numpix;
1136                 tile_info->distotile = tcd_tile->distotile;
1137                 tile_info->thresh = (double *) opj_malloc(tcd_tcp->numlayers * sizeof(double));
1138         }
1139         
1140         for (layno = 0; layno < tcd_tcp->numlayers; layno++) {
1141                 double lo = min;
1142                 double hi = max;
1143                 int success = 0;
1144                 int maxlen = tcd_tcp->rates[layno] ? int_min(((int) ceil(tcd_tcp->rates[layno])), len) : len;
1145                 double goodthresh = 0;
1146                 double stable_thresh = 0;
1147                 int i;
1148                 double distotarget;             /* fixed_quality */
1149                 
1150                 /* fixed_quality */
1151                 distotarget = tcd_tile->distotile - ((K * maxSE) / pow((float)10, tcd_tcp->distoratio[layno] / 10));
1152         
1153                 /* Don't try to find an optimal threshold but rather take everything not included yet, if
1154                   -r xx,yy,zz,0   (disto_alloc == 1 and rates == 0)
1155                   -q xx,yy,zz,0   (fixed_quality == 1 and distoratio == 0)
1156                   ==> possible to have some lossy layers and the last layer for sure lossless */
1157                 if ( ((cp->disto_alloc==1) && (tcd_tcp->rates[layno]>0)) || ((cp->fixed_quality==1) && (tcd_tcp->distoratio[layno]>0))) {
1158                         opj_t2_t *t2 = t2_create(tcd->cinfo, tcd->image, cp);
1159                         double thresh = 0;
1160
1161                         for (i = 0; i < 128; i++) {
1162                                 int l = 0;
1163                                 double distoachieved = 0;       /* fixed_quality */
1164                                 thresh = (lo + hi) / 2;
1165                                 
1166                                 tcd_makelayer(tcd, layno, thresh, 0);
1167                                 
1168                                 if (cp->fixed_quality) {        /* fixed_quality */
1169                                         if(cp->cinema){
1170                                                 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);
1171                                                 if (l == -999) {
1172                                                         lo = thresh;
1173                                                         continue;
1174                                                 }else{
1175                         distoachieved = layno == 0 ? 
1176                                                         tcd_tile->distolayer[0] : cumdisto[layno - 1] + tcd_tile->distolayer[layno];
1177                                                         if (distoachieved < distotarget) {
1178                                                                 hi=thresh; 
1179                                                                 stable_thresh = thresh;
1180                                                                 continue;
1181                                                         }else{
1182                                                                 lo=thresh;
1183                                                         }
1184                                                 }
1185                                         }else{
1186                                                 distoachieved = (layno == 0) ? 
1187                                                         tcd_tile->distolayer[0] : (cumdisto[layno - 1] + tcd_tile->distolayer[layno]);
1188                                                 if (distoachieved < distotarget) {
1189                                                         hi = thresh;
1190                                                         stable_thresh = thresh;
1191                                                         continue;
1192                                                 }
1193                                                 lo = thresh;
1194                                         }
1195                                 } else {
1196                                         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);
1197                                         /* TODO: what to do with l ??? seek / tell ??? */
1198                                         /* opj_event_msg(tcd->cinfo, EVT_INFO, "rate alloc: len=%d, max=%d\n", l, maxlen); */
1199                                         if (l == -999) {
1200                                                 lo = thresh;
1201                                                 continue;
1202                                         }
1203                                         hi = thresh;
1204                                         stable_thresh = thresh;
1205                                 }
1206                         }
1207                         success = 1;
1208                         goodthresh = stable_thresh == 0? thresh : stable_thresh;
1209                         t2_destroy(t2);
1210                 } else {
1211                         success = 1;
1212                         goodthresh = min;
1213                 }
1214                 
1215                 if (!success) {
1216                         return OPJ_FALSE;
1217                 }
1218                 
1219                 if(cstr_info) { /* Threshold for Marcela Index */
1220                         cstr_info->tile[tcd->tcd_tileno].thresh[layno] = goodthresh;
1221                 }
1222                 tcd_makelayer(tcd, layno, goodthresh, 1);
1223         
1224                 /* fixed_quality */
1225                 cumdisto[layno] = (layno == 0) ? tcd_tile->distolayer[0] : (cumdisto[layno - 1] + tcd_tile->distolayer[layno]); 
1226         }
1227
1228         return OPJ_TRUE;
1229 }
1230
1231 int tcd_encode_tile(opj_tcd_t *tcd, int tileno, unsigned char *dest, int len, opj_codestream_info_t *cstr_info) {
1232         int compno;
1233         int l, i, numpacks = 0;
1234         opj_tcd_tile_t *tile = NULL;
1235         opj_tcp_t *tcd_tcp = NULL;
1236         opj_cp_t *cp = NULL;
1237
1238         opj_tcp_t *tcp = &tcd->cp->tcps[0];
1239         opj_tccp_t *tccp = &tcp->tccps[0];
1240         opj_image_t *image = tcd->image;
1241         
1242         opj_t1_t *t1 = NULL;            /* T1 component */
1243         opj_t2_t *t2 = NULL;            /* T2 component */
1244
1245         tcd->tcd_tileno = tileno;
1246         tcd->tcd_tile = tcd->tcd_image->tiles;
1247         tcd->tcp = &tcd->cp->tcps[tileno];
1248
1249         tile = tcd->tcd_tile;
1250         tcd_tcp = tcd->tcp;
1251         cp = tcd->cp;
1252
1253         if(tcd->cur_tp_num == 0){
1254                 tcd->encoding_time = opj_clock();       /* time needed to encode a tile */
1255                 /* INDEX >> "Precinct_nb_X et Precinct_nb_Y" */
1256                 if(cstr_info) {
1257                         opj_tcd_tilecomp_t *tilec_idx = &tile->comps[0];        /* based on component 0 */
1258                         for (i = 0; i < tilec_idx->numresolutions; i++) {
1259                                 opj_tcd_resolution_t *res_idx = &tilec_idx->resolutions[i];
1260                                 
1261                                 cstr_info->tile[tileno].pw[i] = res_idx->pw;
1262                                 cstr_info->tile[tileno].ph[i] = res_idx->ph;
1263                                 
1264                                 numpacks += res_idx->pw * res_idx->ph;
1265                                 
1266                                 cstr_info->tile[tileno].pdx[i] = tccp->prcw[i];
1267                                 cstr_info->tile[tileno].pdy[i] = tccp->prch[i];
1268                         }
1269                         cstr_info->tile[tileno].packet = (opj_packet_info_t*) opj_calloc(cstr_info->numcomps * cstr_info->numlayers * numpacks, sizeof(opj_packet_info_t));
1270                 }
1271                 /* << INDEX */
1272                 
1273                 /*---------------TILE-------------------*/
1274                 
1275                 for (compno = 0; compno < tile->numcomps; compno++) {
1276                         int x, y;
1277                         
1278                         int adjust = image->comps[compno].sgnd ? 0 : 1 << (image->comps[compno].prec - 1);
1279                         int offset_x = int_ceildiv(image->x0, image->comps[compno].dx);
1280                         int offset_y = int_ceildiv(image->y0, image->comps[compno].dy);
1281                         
1282                         opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
1283                         int tw = tilec->x1 - tilec->x0;
1284                         int w = int_ceildiv(image->x1 - image->x0, image->comps[compno].dx);
1285                         
1286                         /* extract tile data */
1287                         
1288                         if (tcd_tcp->tccps[compno].qmfbid == 1) {
1289                                 for (y = tilec->y0; y < tilec->y1; y++) {
1290                                         /* start of the src tile scanline */
1291                                         int *data = &image->comps[compno].data[(tilec->x0 - offset_x) + (y - offset_y) * w];
1292                                         /* start of the dst tile scanline */
1293                                         int *tile_data = &tilec->data[(y - tilec->y0) * tw];
1294                                         for (x = tilec->x0; x < tilec->x1; x++) {
1295                                                 *tile_data++ = *data++ - adjust;
1296                                         }
1297                                 }
1298                         } else if (tcd_tcp->tccps[compno].qmfbid == 0) {
1299                                 for (y = tilec->y0; y < tilec->y1; y++) {
1300                                         /* start of the src tile scanline */
1301                                         int *data = &image->comps[compno].data[(tilec->x0 - offset_x) + (y - offset_y) * w];
1302                                         /* start of the dst tile scanline */
1303                                         int *tile_data = &tilec->data[(y - tilec->y0) * tw];
1304                                         for (x = tilec->x0; x < tilec->x1; x++) {
1305                                                 *tile_data++ = (*data++ - adjust) << 11;
1306                                         }
1307                                         
1308                                 }
1309                         }
1310                 }
1311                 
1312                 /*----------------MCT-------------------*/
1313                 if (tcd_tcp->mct) {
1314                         int samples = (tile->comps[0].x1 - tile->comps[0].x0) * (tile->comps[0].y1 - tile->comps[0].y0);
1315                         if (tcd_tcp->tccps[0].qmfbid == 0) {
1316                                 mct_encode_real(tile->comps[0].data, tile->comps[1].data, tile->comps[2].data, samples);
1317                         } else {
1318                                 mct_encode(tile->comps[0].data, tile->comps[1].data, tile->comps[2].data, samples);
1319                         }
1320                 }
1321                 
1322                 /*----------------DWT---------------------*/
1323                 
1324                 for (compno = 0; compno < tile->numcomps; compno++) {
1325                         opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
1326                         if (tcd_tcp->tccps[compno].qmfbid == 1) {
1327                                 dwt_encode(tilec);
1328                         } else if (tcd_tcp->tccps[compno].qmfbid == 0) {
1329                                 dwt_encode_real(tilec);
1330                         }
1331                 }
1332                 
1333                 /*------------------TIER1-----------------*/
1334                 t1 = t1_create(tcd->cinfo);
1335                 t1_encode_cblks(t1, tile, tcd_tcp);
1336                 t1_destroy(t1);
1337                 
1338                 /*-----------RATE-ALLOCATE------------------*/
1339                 
1340                 /* INDEX */
1341                 if(cstr_info) {
1342                         cstr_info->index_write = 0;
1343                 }
1344                 if (cp->disto_alloc || cp->fixed_quality) {     /* fixed_quality */
1345                         /* Normal Rate/distortion allocation */
1346                         tcd_rateallocate(tcd, dest, len, cstr_info);
1347                 } else {
1348                         /* Fixed layer allocation */
1349                         tcd_rateallocate_fixed(tcd);
1350                 }
1351         }
1352         /*--------------TIER2------------------*/
1353
1354         /* INDEX */
1355         if(cstr_info) {
1356                 cstr_info->index_write = 1;
1357         }
1358
1359         t2 = t2_create(tcd->cinfo, image, cp);
1360         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);
1361         t2_destroy(t2);
1362         
1363         /*---------------CLEAN-------------------*/
1364
1365         
1366         if(tcd->cur_tp_num == tcd->cur_totnum_tp - 1){
1367                 tcd->encoding_time = opj_clock() - tcd->encoding_time;
1368                 opj_event_msg(tcd->cinfo, EVT_INFO, "- tile encoded in %f s\n", tcd->encoding_time);
1369
1370                 /* cleaning memory */
1371                 for (compno = 0; compno < tile->numcomps; compno++) {
1372                         opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
1373                         opj_aligned_free(tilec->data);
1374                 }
1375         }
1376
1377         return l;
1378 }
1379
1380 opj_bool tcd_decode_tile(opj_tcd_t *tcd, unsigned char *src, int len, int tileno, opj_codestream_info_t *cstr_info) {
1381         int l;
1382         int compno;
1383         int eof = 0;
1384         double tile_time, t1_time, dwt_time;
1385         opj_tcd_tile_t *tile = NULL;
1386
1387         opj_t1_t *t1 = NULL;            /* T1 component */
1388         opj_t2_t *t2 = NULL;            /* T2 component */
1389         
1390         tcd->tcd_tileno = tileno;
1391         tcd->tcd_tile = &(tcd->tcd_image->tiles[tileno]);
1392         tcd->tcp = &(tcd->cp->tcps[tileno]);
1393         tile = tcd->tcd_tile;
1394         
1395         tile_time = opj_clock();        /* time needed to decode a tile */
1396         opj_event_msg(tcd->cinfo, EVT_INFO, "tile %d of %d\n", tileno + 1, tcd->cp->tw * tcd->cp->th);
1397
1398         /* INDEX >>  */
1399         if(cstr_info) {
1400                 int resno, compno, numprec = 0;
1401                 for (compno = 0; compno < cstr_info->numcomps; compno++) {
1402                         opj_tcp_t *tcp = &tcd->cp->tcps[0];
1403                         opj_tccp_t *tccp = &tcp->tccps[compno];
1404                         opj_tcd_tilecomp_t *tilec_idx = &tile->comps[compno];   
1405                         for (resno = 0; resno < tilec_idx->numresolutions; resno++) {
1406                                 opj_tcd_resolution_t *res_idx = &tilec_idx->resolutions[resno];
1407                                 cstr_info->tile[tileno].pw[resno] = res_idx->pw;
1408                                 cstr_info->tile[tileno].ph[resno] = res_idx->ph;
1409                                 numprec += res_idx->pw * res_idx->ph;
1410                                 if (tccp->csty & J2K_CP_CSTY_PRT) {
1411                                         cstr_info->tile[tileno].pdx[resno] = tccp->prcw[resno];
1412                                         cstr_info->tile[tileno].pdy[resno] = tccp->prch[resno];
1413                                 }
1414                                 else {
1415                                         cstr_info->tile[tileno].pdx[resno] = 15;
1416                                         cstr_info->tile[tileno].pdx[resno] = 15;
1417                                 }
1418                         }
1419                 }
1420                 cstr_info->tile[tileno].packet = (opj_packet_info_t *) opj_malloc(cstr_info->numlayers * numprec * sizeof(opj_packet_info_t));
1421                 cstr_info->packno = 0;
1422         }
1423         /* << INDEX */
1424         
1425         /*--------------TIER2------------------*/
1426         
1427         t2 = t2_create(tcd->cinfo, tcd->image, tcd->cp);
1428         l = t2_decode_packets(t2, src, len, tileno, tile, cstr_info);
1429         t2_destroy(t2);
1430
1431         if (l == -999) {
1432                 eof = 1;
1433                 opj_event_msg(tcd->cinfo, EVT_ERROR, "tcd_decode: incomplete bistream\n");
1434         }
1435         
1436         /*------------------TIER1-----------------*/
1437         
1438         t1_time = opj_clock();  /* time needed to decode a tile */
1439         t1 = t1_create(tcd->cinfo);
1440         for (compno = 0; compno < tile->numcomps; ++compno) {
1441                 opj_tcd_tilecomp_t* tilec = &tile->comps[compno];
1442                 /* The +3 is headroom required by the vectorized DWT */
1443                 tilec->data = (int*) opj_aligned_malloc((((tilec->x1 - tilec->x0) * (tilec->y1 - tilec->y0))+3) * sizeof(int));
1444                 t1_decode_cblks(t1, tilec, &tcd->tcp->tccps[compno]);
1445         }
1446         t1_destroy(t1);
1447         t1_time = opj_clock() - t1_time;
1448         opj_event_msg(tcd->cinfo, EVT_INFO, "- tiers-1 took %f s\n", t1_time);
1449         
1450         /*----------------DWT---------------------*/
1451
1452         dwt_time = opj_clock(); /* time needed to decode a tile */
1453         for (compno = 0; compno < tile->numcomps; compno++) {
1454                 opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
1455                 int numres2decode;
1456
1457                 if (tcd->cp->reduce != 0) {
1458                         tcd->image->comps[compno].resno_decoded =
1459                                 tile->comps[compno].numresolutions - tcd->cp->reduce - 1;
1460                         if (tcd->image->comps[compno].resno_decoded < 0) {                              
1461                                 opj_event_msg(tcd->cinfo, EVT_ERROR, "Error decoding tile. The number of resolutions to remove [%d+1] is higher than the number "
1462                                         " of resolutions in the original codestream [%d]\nModify the cp_reduce parameter.\n", tcd->cp->reduce, tile->comps[compno].numresolutions);
1463                                 return OPJ_FALSE;
1464                         }
1465                 }
1466
1467                 numres2decode = tcd->image->comps[compno].resno_decoded + 1;
1468                 if(numres2decode > 0){
1469                         if (tcd->tcp->tccps[compno].qmfbid == 1) {
1470                                 dwt_decode(tilec, numres2decode);
1471                         } else {
1472                                 dwt_decode_real(tilec, numres2decode);
1473                         }
1474                 }
1475         }
1476         dwt_time = opj_clock() - dwt_time;
1477         opj_event_msg(tcd->cinfo, EVT_INFO, "- dwt took %f s\n", dwt_time);
1478
1479         /*----------------MCT-------------------*/
1480
1481         if (tcd->tcp->mct) {
1482                 int n = (tile->comps[0].x1 - tile->comps[0].x0) * (tile->comps[0].y1 - tile->comps[0].y0);
1483
1484                 if (tile->numcomps >= 3 ){
1485                         if (tcd->tcp->tccps[0].qmfbid == 1) {
1486                                 mct_decode(
1487                                                 tile->comps[0].data,
1488                                                 tile->comps[1].data,
1489                                                 tile->comps[2].data,
1490                                                 n);
1491                         } else {
1492                                 mct_decode_real(
1493                                                 (float*)tile->comps[0].data,
1494                                                 (float*)tile->comps[1].data,
1495                                                 (float*)tile->comps[2].data,
1496                                                 n);
1497                         }
1498                 } else{
1499                         opj_event_msg(tcd->cinfo, EVT_WARNING,"Number of components (%d) is inconsistent with a MCT. Skip the MCT step.\n",tile->numcomps);
1500                 }
1501         }
1502
1503         /*---------------TILE-------------------*/
1504
1505         for (compno = 0; compno < tile->numcomps; ++compno) {
1506                 opj_tcd_tilecomp_t* tilec = &tile->comps[compno];
1507                 opj_image_comp_t* imagec = &tcd->image->comps[compno];
1508                 opj_tcd_resolution_t* res = &tilec->resolutions[imagec->resno_decoded];
1509                 int adjust = imagec->sgnd ? 0 : 1 << (imagec->prec - 1);
1510                 int min = imagec->sgnd ? -(1 << (imagec->prec - 1)) : 0;
1511                 int max = imagec->sgnd ?  (1 << (imagec->prec - 1)) - 1 : (1 << imagec->prec) - 1;
1512
1513                 int tw = tilec->x1 - tilec->x0;
1514                 int w = imagec->w;
1515
1516                 int offset_x = int_ceildivpow2(imagec->x0, imagec->factor);
1517                 int offset_y = int_ceildivpow2(imagec->y0, imagec->factor);
1518
1519                 int i, j;
1520                 if(!imagec->data){
1521                         imagec->data = (int*) opj_malloc(imagec->w * imagec->h * sizeof(int));
1522                 }
1523                 if(tcd->tcp->tccps[compno].qmfbid == 1) {
1524                         for(j = res->y0; j < res->y1; ++j) {
1525                                 for(i = res->x0; i < res->x1; ++i) {
1526                                         int v = tilec->data[i - res->x0 + (j - res->y0) * tw];
1527                                         v += adjust;
1528                                         imagec->data[(i - offset_x) + (j - offset_y) * w] = int_clamp(v, min, max);
1529                                 }
1530                         }
1531                 }else{
1532                         for(j = res->y0; j < res->y1; ++j) {
1533                                 for(i = res->x0; i < res->x1; ++i) {
1534                                         float tmp = ((float*)tilec->data)[i - res->x0 + (j - res->y0) * tw];
1535                                         int v = lrintf(tmp);
1536                                         v += adjust;
1537                                         imagec->data[(i - offset_x) + (j - offset_y) * w] = int_clamp(v, min, max);
1538                                 }
1539                         }
1540                 }
1541                 opj_aligned_free(tilec->data);
1542         }
1543
1544         tile_time = opj_clock() - tile_time;    /* time needed to decode a tile */
1545         opj_event_msg(tcd->cinfo, EVT_INFO, "- tile decoded in %f s\n", tile_time);
1546
1547         if (eof) {
1548                 return OPJ_FALSE;
1549         }
1550         
1551         return OPJ_TRUE;
1552 }
1553
1554 void tcd_free_decode(opj_tcd_t *tcd) {
1555         opj_tcd_image_t *tcd_image = tcd->tcd_image;    
1556         opj_free(tcd_image->tiles);
1557 }
1558
1559 void tcd_free_decode_tile(opj_tcd_t *tcd, int tileno) {
1560         int compno,resno,bandno,precno;
1561
1562         opj_tcd_image_t *tcd_image = tcd->tcd_image;
1563
1564         opj_tcd_tile_t *tile = &tcd_image->tiles[tileno];
1565         for (compno = 0; compno < tile->numcomps; compno++) {
1566                 opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
1567                 for (resno = 0; resno < tilec->numresolutions; resno++) {
1568                         opj_tcd_resolution_t *res = &tilec->resolutions[resno];
1569                         for (bandno = 0; bandno < res->numbands; bandno++) {
1570                                 opj_tcd_band_t *band = &res->bands[bandno];
1571                                 for (precno = 0; precno < res->ph * res->pw; precno++) {
1572                                         opj_tcd_precinct_t *prec = &band->precincts[precno];
1573                                         if (prec->imsbtree != NULL) tgt_destroy(prec->imsbtree);
1574                                         if (prec->incltree != NULL) tgt_destroy(prec->incltree);
1575                                 }
1576                                 opj_free(band->precincts);
1577                         }
1578                 }
1579                 opj_free(tilec->resolutions);
1580         }
1581         opj_free(tile->comps);
1582 }
1583
1584
1585 opj_bool tcd_init_v2(  opj_tcd_v2_t *p_tcd,
1586                                            opj_image_t * p_image,
1587                                            opj_cp_v2_t * p_cp )
1588 {
1589         OPJ_UINT32 l_tile_comp_size;
1590
1591         p_tcd->image = p_image;
1592         p_tcd->cp = p_cp;
1593
1594         p_tcd->tcd_image->tiles = (opj_tcd_tile_v2_t *) opj_malloc(sizeof(opj_tcd_tile_v2_t));
1595         if (! p_tcd->tcd_image->tiles) {
1596                 return OPJ_FALSE;
1597         }
1598         memset(p_tcd->tcd_image->tiles,0, sizeof(opj_tcd_tile_v2_t));
1599
1600         l_tile_comp_size = p_image->numcomps * sizeof(opj_tcd_tilecomp_v2_t);
1601         p_tcd->tcd_image->tiles->comps = (opj_tcd_tilecomp_v2_t *) opj_malloc(l_tile_comp_size);
1602         if (! p_tcd->tcd_image->tiles->comps ) {
1603                 return OPJ_FALSE;
1604         }
1605         memset( p_tcd->tcd_image->tiles->comps , 0 , l_tile_comp_size);
1606
1607         p_tcd->tcd_image->tiles->numcomps = p_image->numcomps;
1608         p_tcd->tp_pos = p_cp->m_specific_param.m_enc.m_tp_pos;
1609
1610         return OPJ_TRUE;
1611 }
1612
1613 /**
1614 Destroy a previously created TCD handle
1615 */
1616 void tcd_destroy_v2(opj_tcd_v2_t *tcd) {
1617         if (tcd) {
1618                 tcd_free_tile(tcd);
1619
1620                 if (tcd->tcd_image) {
1621                         opj_free(tcd->tcd_image);
1622                         tcd->tcd_image = 00;
1623                 }
1624                 opj_free(tcd);
1625         }
1626 }
1627
1628 /* ----------------------------------------------------------------------- */
1629 /**
1630  * Initialize the tile coder and may reuse some meory.
1631  * @param       p_tcd           TCD handle.
1632  * @param       p_image         raw image.
1633  * @param       p_cp            coding parameters.
1634  * @param       p_tile_no       current tile index to encode.
1635  *
1636  * @return true if the encoding values could be set (false otherwise).
1637 */
1638 #define MACRO_TCD_ALLOCATE(FUNCTION,TYPE,FRACTION,ELEMENT,FUNCTION_ELEMENT)             \
1639 opj_bool FUNCTION (     opj_tcd_v2_t *p_tcd,                                                                            \
1640                                         OPJ_UINT32 p_tile_no                                                                            \
1641                         )                                                                                                                                       \
1642 {                                                                                                                                                               \
1643         OPJ_UINT32 (*l_gain_ptr)(OPJ_UINT32) = 00;                                                                      \
1644         OPJ_UINT32 compno, resno, bandno, precno, cblkno;                                                       \
1645         opj_tcp_v2_t * l_tcp = 00;                                                                                                      \
1646         opj_cp_v2_t * l_cp = 00;                                                                                                        \
1647         opj_tcd_tile_v2_t * l_tile = 00;                                                                                        \
1648         opj_tccp_t *l_tccp = 00;                                                                                                        \
1649         opj_tcd_tilecomp_v2_t *l_tilec = 00;                                                                            \
1650         opj_image_comp_t * l_image_comp = 00;                                                                           \
1651         opj_tcd_resolution_v2_t *l_res = 00;                                                                            \
1652         opj_tcd_band_v2_t *l_band = 00;                                                                                         \
1653         opj_stepsize_t * l_step_size = 00;                                                                                      \
1654         opj_tcd_precinct_v2_t *l_current_precinct = 00;                                                         \
1655         TYPE* l_code_block = 00;                                                                                                        \
1656         opj_image_t *l_image = 00;                                                                                                      \
1657         OPJ_UINT32 p,q;                                                                                                                         \
1658         OPJ_UINT32 l_level_no;                                                                                                          \
1659         OPJ_UINT32 l_pdx, l_pdy;                                                                                                        \
1660         OPJ_UINT32 l_gain;                                                                                                                      \
1661         OPJ_INT32 l_x0b, l_y0b;                                                                                                         \
1662         /* extent of precincts , top left, bottom right**/                                                      \
1663         OPJ_INT32 l_tl_prc_x_start, l_tl_prc_y_start, l_br_prc_x_end, l_br_prc_y_end;   \
1664         /* number of precinct for a resolution */                                                                       \
1665         OPJ_UINT32 l_nb_precincts;                                                                                                      \
1666         /* room needed to store l_nb_precinct precinct for a resolution */                      \
1667         OPJ_UINT32 l_nb_precinct_size;                                                                                          \
1668         /* number of code blocks for a precinct*/                                                                       \
1669         OPJ_UINT32 l_nb_code_blocks;                                                                                            \
1670         /* room needed to store l_nb_code_blocks code blocks for a precinct*/           \
1671         OPJ_UINT32 l_nb_code_blocks_size;                                                                                       \
1672         /* size of data for a tile */                                                                                           \
1673         OPJ_UINT32 l_data_size;                                                                                                         \
1674                                                                                                                                                                 \
1675         l_cp = p_tcd->cp;                                                                                                                       \
1676         l_tcp = &(l_cp->tcps[p_tile_no]);                                                                                       \
1677         l_tile = p_tcd->tcd_image->tiles;                                                                                       \
1678         l_tccp = l_tcp->tccps;                                                                                                          \
1679         l_tilec = l_tile->comps;                                                                                                        \
1680         l_image = p_tcd->image;                                                                                                         \
1681         l_image_comp = p_tcd->image->comps;                                                                                     \
1682                                                                                                                                                                 \
1683         p = p_tile_no % l_cp->tw;       /* tile coordinates */                                                  \
1684         q = p_tile_no / l_cp->tw;                                                                                                       \
1685         /*fprintf(stderr, "Tile coordinate = %d,%d\n", p, q);*/                                         \
1686                                                                                                                                                                 \
1687         /* 4 borders of the tile rescale on the image if necessary */                           \
1688         l_tile->x0 = int_max(l_cp->tx0 + p * l_cp->tdx, l_image->x0);                           \
1689         l_tile->y0 = int_max(l_cp->ty0 + q * l_cp->tdy, l_image->y0);                           \
1690         l_tile->x1 = int_min(l_cp->tx0 + (p + 1) * l_cp->tdx, l_image->x1);                     \
1691         l_tile->y1 = int_min(l_cp->ty0 + (q + 1) * l_cp->tdy, l_image->y1);                     \
1692         /*fprintf(stderr, "Tile border = %d,%d,%d,%d\n", l_tile->x0, l_tile->y0,l_tile->x1,l_tile->y1);*/\
1693                                                                                                                                                                 \
1694         /*tile->numcomps = image->numcomps; */                                                                          \
1695         for(compno = 0; compno < l_tile->numcomps; ++compno) {                                          \
1696                 /*fprintf(stderr, "compno = %d/%d\n", compno, l_tile->numcomps);*/              \
1697                                                                                                                                                                 \
1698                 /* border of each l_tile component (global) */                                                  \
1699                 l_tilec->x0 = int_ceildiv(l_tile->x0, l_image_comp->dx);                                \
1700                 l_tilec->y0 = int_ceildiv(l_tile->y0, l_image_comp->dy);                                \
1701                 l_tilec->x1 = int_ceildiv(l_tile->x1, l_image_comp->dx);                                \
1702                 l_tilec->y1 = int_ceildiv(l_tile->y1, l_image_comp->dy);                                \
1703                 /*fprintf(stderr, "\tTile compo border = %d,%d,%d,%d\n", l_tilec->x0, l_tilec->y0,l_tilec->x1,l_tilec->y1);*/\
1704                                                                                                                                                                 \
1705                 l_data_size = (l_tilec->x1 - l_tilec->x0)                                                               \
1706                                         * (l_tilec->y1 - l_tilec->y0) * sizeof(OPJ_UINT32 );            \
1707                 l_tilec->numresolutions = l_tccp->numresolutions;                                               \
1708                 if (l_tccp->numresolutions < l_cp->m_specific_param.m_dec.m_reduce) {   \
1709                         l_tilec->minimum_num_resolutions = 1;                                                           \
1710                 }                                                                                                                                               \
1711                 else {                                                                                                                                  \
1712                         l_tilec->minimum_num_resolutions = l_tccp->numresolutions                       \
1713                                 - l_cp->m_specific_param.m_dec.m_reduce;                                                \
1714                 }                                                                                                                                               \
1715                                                                                                                                                                 \
1716                 if (l_tilec->data == 00) {                                                                                              \
1717             l_tilec->data = (OPJ_INT32 *) opj_malloc(l_data_size);              \
1718                         if (! l_tilec->data ) {                                                                                         \
1719                                 return OPJ_FALSE;                                                                                               \
1720                         }                                                                                                                                       \
1721             /*fprintf(stderr, "\tAllocate data of tilec (int): %d x OPJ_UINT32\n",l_data_size);*/       \
1722                                                                                                                                                                 \
1723                         l_tilec->data_size = l_data_size;                                                                       \
1724                 }                                                                                                                                               \
1725                 else if (l_data_size > l_tilec->data_size) {                                                    \
1726                         l_tilec->data = (OPJ_INT32 *) opj_realloc(l_tilec->data, l_data_size);  \
1727                         if (! l_tilec->data) {                                                                                          \
1728                                 return OPJ_FALSE;                                                                                               \
1729                         }                                                                                                                                       \
1730                         /*fprintf(stderr, "\tReallocate data of tilec (int): from %d to %d x OPJ_UINT32\n", l_tilec->data_size, l_data_size);*/         \
1731                         l_tilec->data_size = l_data_size;                                                                       \
1732                 }                                                                                                                                               \
1733                                                                                                                                                         \
1734                 l_data_size = l_tilec->numresolutions * sizeof(opj_tcd_resolution_v2_t);        \
1735                                                                                                                                                                 \
1736                 if (l_tilec->resolutions == 00) {                                                                               \
1737             l_tilec->resolutions = (opj_tcd_resolution_v2_t *) opj_malloc(l_data_size); \
1738                         if (! l_tilec->resolutions ) {                                                                          \
1739                                 return OPJ_FALSE;                                                                                               \
1740                         }                                                                                                                                       \
1741                         /*fprintf(stderr, "\tAllocate resolutions of tilec (opj_tcd_resolution_v2_t): %d\n",l_data_size);*/             \
1742                         l_tilec->resolutions_size = l_data_size;                                                        \
1743                         memset(l_tilec->resolutions,0,l_data_size);                                                     \
1744                 }                                                                                                                                               \
1745                 else if (l_data_size > l_tilec->resolutions_size) {                                             \
1746                         l_tilec->resolutions = (opj_tcd_resolution_v2_t *) opj_realloc(l_tilec->resolutions, l_data_size);      \
1747                         if (! l_tilec->resolutions) {                                                                           \
1748                                 return OPJ_FALSE;                                                                                               \
1749                         }                                                                                                                                       \
1750                         /*fprintf(stderr, "\tReallocate data of tilec (int): from %d to %d x OPJ_UINT32\n", l_tilec->resolutions_size, l_data_size);*/  \
1751                         memset(((OPJ_BYTE*) l_tilec->resolutions)+l_tilec->resolutions_size,0,l_data_size - l_tilec->resolutions_size); \
1752                         l_tilec->resolutions_size = l_data_size;                                                        \
1753                 }                                                                                                                                               \
1754                                                                                                                                                                 \
1755                 l_level_no = l_tilec->numresolutions - 1;                                                               \
1756                 l_res = l_tilec->resolutions;                                                                                   \
1757                 l_step_size = l_tccp->stepsizes;                                                                                \
1758                 if (l_tccp->qmfbid == 0) {                                                                                              \
1759                         l_gain_ptr = &dwt_getgain_real_v2;                                                                      \
1760                 }                                                                                                                                               \
1761                 else {                                                                                                                                  \
1762                         l_gain_ptr  = &dwt_getgain_v2;                                                                          \
1763                 }                                                                                                                                               \
1764                 /*fprintf(stderr, "\tlevel_no=%d\n",l_level_no);*/                                              \
1765                                                                                                                                                                 \
1766                 for(resno = 0; resno < l_tilec->numresolutions; ++resno) {                              \
1767                         /*fprintf(stderr, "\t\tresno = %d/%d\n", resno, l_tilec->numresolutions);*/     \
1768                         OPJ_INT32 tlcbgxstart, tlcbgystart, brcbgxend, brcbgyend;                       \
1769                         OPJ_UINT32 cbgwidthexpn, cbgheightexpn;                                                         \
1770                         OPJ_UINT32 cblkwidthexpn, cblkheightexpn;                                                       \
1771                                                                                                                                                                 \
1772                         /* border for each resolution level (global) */                                         \
1773                         l_res->x0 = int_ceildivpow2(l_tilec->x0, l_level_no);                           \
1774                         l_res->y0 = int_ceildivpow2(l_tilec->y0, l_level_no);                           \
1775                         l_res->x1 = int_ceildivpow2(l_tilec->x1, l_level_no);                           \
1776                         l_res->y1 = int_ceildivpow2(l_tilec->y1, l_level_no);                           \
1777                         /*fprintf(stderr, "\t\t\tres_x0= %d, res_y0 =%d, res_x1=%d, res_y1=%d\n", l_res->x0, l_res->y0, l_res->x1, l_res->y1);*/        \
1778                         /* p. 35, table A-23, ISO/IEC FDIS154444-1 : 2000 (18 august 2000) */   \
1779                         l_pdx = l_tccp->prcw[resno];                                                                            \
1780                         l_pdy = l_tccp->prch[resno];                                                                            \
1781                         /*fprintf(stderr, "\t\t\tpdx=%d, pdy=%d\n", l_pdx, l_pdy);*/            \
1782                         /* p. 64, B.6, ISO/IEC FDIS15444-1 : 2000 (18 august 2000)  */          \
1783                         l_tl_prc_x_start = int_floordivpow2(l_res->x0, l_pdx) << l_pdx;         \
1784                         l_tl_prc_y_start = int_floordivpow2(l_res->y0, l_pdy) << l_pdy;         \
1785                         l_br_prc_x_end = int_ceildivpow2(l_res->x1, l_pdx) << l_pdx;            \
1786                         l_br_prc_y_end = int_ceildivpow2(l_res->y1, l_pdy) << l_pdy;            \
1787                         /*fprintf(stderr, "\t\t\tprc_x_start=%d, prc_y_start=%d, br_prc_x_end=%d, br_prc_y_end=%d \n", l_tl_prc_x_start, l_tl_prc_y_start, l_br_prc_x_end ,l_br_prc_y_end );*/  \
1788                                                                                                                                                                 \
1789                         l_res->pw = (l_res->x0 == l_res->x1) ? 0 : ((l_br_prc_x_end - l_tl_prc_x_start) >> l_pdx);      \
1790                         l_res->ph = (l_res->y0 == l_res->y1) ? 0 : ((l_br_prc_y_end - l_tl_prc_y_start) >> l_pdy);      \
1791                         /*fprintf(stderr, "\t\t\tres_pw=%d, res_ph=%d\n", l_res->pw, l_res->ph );*/     \
1792                                                                                                                                                                 \
1793                         l_nb_precincts = l_res->pw * l_res->ph;                                                         \
1794                         l_nb_precinct_size = l_nb_precincts * sizeof(opj_tcd_precinct_v2_t);    \
1795                         if (resno == 0) {                                                                                                       \
1796                                 tlcbgxstart = l_tl_prc_x_start;                                                                 \
1797                                 tlcbgystart = l_tl_prc_y_start;                                                                 \
1798                                 brcbgxend = l_br_prc_x_end;                                                                             \
1799                                 brcbgyend = l_br_prc_y_end;                                                                             \
1800                                 cbgwidthexpn = l_pdx;                                                                                   \
1801                                 cbgheightexpn = l_pdy;                                                                                  \
1802                                 l_res->numbands = 1;                                                                                    \
1803                         }                                                                                                                                       \
1804                         else {                                                                                                                          \
1805                                 tlcbgxstart = int_ceildivpow2(l_tl_prc_x_start, 1);                             \
1806                                 tlcbgystart = int_ceildivpow2(l_tl_prc_y_start, 1);                             \
1807                                 brcbgxend = int_ceildivpow2(l_br_prc_x_end, 1);                                 \
1808                                 brcbgyend = int_ceildivpow2(l_br_prc_y_end, 1);                                 \
1809                                 cbgwidthexpn = l_pdx - 1;                                                                               \
1810                                 cbgheightexpn = l_pdy - 1;                                                                              \
1811                                 l_res->numbands = 3;                                                                                    \
1812                         }                                                                                                                                       \
1813                                                                                                                                                                 \
1814                         cblkwidthexpn = uint_min(l_tccp->cblkw, cbgwidthexpn);                          \
1815                         cblkheightexpn = uint_min(l_tccp->cblkh, cbgheightexpn);                        \
1816                         l_band = l_res->bands;                                                                                          \
1817                                                                                                                                                                 \
1818                         for (bandno = 0; bandno < l_res->numbands; ++bandno) {                          \
1819                                 OPJ_INT32 numbps;\
1820                                 /*fprintf(stderr, "\t\t\tband_no=%d/%d\n", bandno, l_res->numbands );*/ \
1821                                                                                                                                                                 \
1822                                 if (resno == 0) {                                                                                               \
1823                                         l_band->bandno = 0 ;                                                                            \
1824                                         l_band->x0 = int_ceildivpow2(l_tilec->x0, l_level_no);          \
1825                                         l_band->y0 = int_ceildivpow2(l_tilec->y0, l_level_no);          \
1826                                         l_band->x1 = int_ceildivpow2(l_tilec->x1, l_level_no);          \
1827                                         l_band->y1 = int_ceildivpow2(l_tilec->y1, l_level_no);          \
1828                                 }                                                                                                                               \
1829                                 else {                                                                                                                  \
1830                                         l_band->bandno = bandno + 1;                                                            \
1831                                         /* x0b = 1 if bandno = 1 or 3 */                                                        \
1832                                         l_x0b = l_band->bandno&1;                                                                       \
1833                                         /* y0b = 1 if bandno = 2 or 3 */                                                        \
1834                                         l_y0b = (l_band->bandno)>>1;                                                            \
1835                                         /* l_band border (global) */                                                            \
1836                                         l_band->x0 = int_ceildivpow2(l_tilec->x0 - (1 << l_level_no) * l_x0b, l_level_no + 1);  \
1837                                         l_band->y0 = int_ceildivpow2(l_tilec->y0 - (1 << l_level_no) * l_y0b, l_level_no + 1);  \
1838                                         l_band->x1 = int_ceildivpow2(l_tilec->x1 - (1 << l_level_no) * l_x0b, l_level_no + 1);  \
1839                                         l_band->y1 = int_ceildivpow2(l_tilec->y1 - (1 << l_level_no) * l_y0b, l_level_no + 1);  \
1840                                 }                                                                                                                               \
1841                                                                                                                                                                 \
1842                                 /** avoid an if with storing function pointer */                                \
1843                                 l_gain = (*l_gain_ptr) (l_band->bandno);                                                \
1844                                 numbps = l_image_comp->prec + l_gain;                                                   \
1845                                 l_band->stepsize = (OPJ_FLOAT32)(((1.0 + l_step_size->mant / 2048.0) * pow(2.0, (OPJ_INT32) (numbps - l_step_size->expn)))) * FRACTION;\
1846                                 l_band->numbps = l_step_size->expn + l_tccp->numgbits - 1;      /* WHY -1 ? */\
1847                                                                                                                                                                 \
1848                                 if (! l_band->precincts) {                                                                              \
1849                                         l_band->precincts = (opj_tcd_precinct_v2_t *) opj_malloc( /*3 * */ l_nb_precinct_size);\
1850                                         if (! l_band->precincts) {                                                                      \
1851                                                 return OPJ_FALSE;                                                                               \
1852                                         }                                                                                                                       \
1853                                         /*fprintf(stderr, "\t\t\t\tAllocate precincts of a band (opj_tcd_precinct_v2_t): %d\n",l_nb_precinct_size);     */      \
1854                                         memset(l_band->precincts,0,l_nb_precinct_size);                         \
1855                                         l_band->precincts_data_size = l_nb_precinct_size;                       \
1856                                 }                                                                                                                               \
1857                                 else if (l_band->precincts_data_size < l_nb_precinct_size) {    \
1858                                                                                                                                                                 \
1859                                         l_band->precincts = (opj_tcd_precinct_v2_t *) opj_realloc(l_band->precincts,/*3 * */ l_nb_precinct_size);\
1860                                         if (! l_band->precincts) {                                                                      \
1861                                                 return OPJ_FALSE;                                                                               \
1862                                         }                                                                                                                       \
1863                                         /*fprintf(stderr, "\t\t\t\tReallocate precincts of a band (opj_tcd_precinct_v2_t): from %d to %d\n",l_band->precincts_data_size, l_nb_precinct_size);*/\
1864                                         memset(((OPJ_BYTE *) l_band->precincts) + l_band->precincts_data_size,0,l_nb_precinct_size - l_band->precincts_data_size);\
1865                                         l_band->precincts_data_size = l_nb_precinct_size;                       \
1866                                 }                                                                                                                               \
1867                                                                                                                                                                 \
1868                                 l_current_precinct = l_band->precincts;                                                 \
1869                                 for     (precno = 0; precno < l_nb_precincts; ++precno) {                       \
1870                                         OPJ_INT32 tlcblkxstart, tlcblkystart, brcblkxend, brcblkyend;   \
1871                                         OPJ_INT32 cbgxstart = tlcbgxstart + (precno % l_res->pw) * (1 << cbgwidthexpn); \
1872                                         OPJ_INT32 cbgystart = tlcbgystart + (precno / l_res->pw) * (1 << cbgheightexpn);        \
1873                                         OPJ_INT32 cbgxend = cbgxstart + (1 << cbgwidthexpn);            \
1874                                         OPJ_INT32 cbgyend = cbgystart + (1 << cbgheightexpn);           \
1875                                         /*fprintf(stderr, "\t precno=%d; bandno=%d, resno=%d; compno=%d\n", precno, bandno , resno, compno);*/\
1876                                         /*fprintf(stderr, "\t tlcbgxstart(=%d) + (precno(=%d) percent res->pw(=%d)) * (1 << cbgwidthexpn(=%d)) \n",tlcbgxstart,precno,l_res->pw,cbgwidthexpn);*/\
1877                                                                                                                                                                 \
1878                                         /* precinct size (global) */                                                            \
1879                                         /*fprintf(stderr, "\t cbgxstart=%d, l_band->x0 = %d \n",cbgxstart, l_band->x0);*/ \
1880                                                                                                                                                                 \
1881                                         l_current_precinct->x0 = int_max(cbgxstart, l_band->x0);        \
1882                                         l_current_precinct->y0 = int_max(cbgystart, l_band->y0);        \
1883                                         l_current_precinct->x1 = int_min(cbgxend, l_band->x1);          \
1884                                         l_current_precinct->y1 = int_min(cbgyend, l_band->y1);          \
1885                                         /*fprintf(stderr, "\t prc_x0=%d; prc_y0=%d, prc_x1=%d; prc_y1=%d\n",l_current_precinct->x0, l_current_precinct->y0 ,l_current_precinct->x1, l_current_precinct->y1);*/ \
1886                                                                                                                                                                 \
1887                                         tlcblkxstart = int_floordivpow2(l_current_precinct->x0, cblkwidthexpn) << cblkwidthexpn;        \
1888                                         /*fprintf(stderr, "\t tlcblkxstart =%d\n",tlcblkxstart );*/ \
1889                                         tlcblkystart = int_floordivpow2(l_current_precinct->y0, cblkheightexpn) << cblkheightexpn;      \
1890                                         /*fprintf(stderr, "\t tlcblkystart =%d\n",tlcblkystart );*/ \
1891                                         brcblkxend = int_ceildivpow2(l_current_precinct->x1, cblkwidthexpn) << cblkwidthexpn;   \
1892                                         /*fprintf(stderr, "\t brcblkxend =%d\n",brcblkxend );*/         \
1893                                         brcblkyend = int_ceildivpow2(l_current_precinct->y1, cblkheightexpn) << cblkheightexpn; \
1894                                         /*fprintf(stderr, "\t brcblkyend =%d\n",brcblkyend );*/         \
1895                                         l_current_precinct->cw = (brcblkxend - tlcblkxstart) >> cblkwidthexpn;  \
1896                                         l_current_precinct->ch = (brcblkyend - tlcblkystart) >> cblkheightexpn; \
1897                                                                                                                                                                 \
1898                                         l_nb_code_blocks = l_current_precinct->cw * l_current_precinct->ch;     \
1899                                         /*fprintf(stderr, "\t\t\t\t precinct_cw = %d x recinct_ch = %d\n",l_current_precinct->cw, l_current_precinct->ch);      */      \
1900                                         l_nb_code_blocks_size = l_nb_code_blocks * sizeof(TYPE);        \
1901                                                                                                                                                                 \
1902                                         if (! l_current_precinct->cblks.ELEMENT) {                                      \
1903                                                 l_current_precinct->cblks.ELEMENT = (TYPE*) opj_malloc(l_nb_code_blocks_size);\
1904                                                 if (! l_current_precinct->cblks.ELEMENT ) {                             \
1905                                                         return OPJ_FALSE;                                                                       \
1906                                                 }                                                                                                               \
1907                                                 /*fprintf(stderr, "\t\t\t\tAllocate cblks of a precinct (opj_tcd_cblk_dec_v2_t): %d\n",l_nb_code_blocks_size);*/                \
1908                                                                                                                                                                 \
1909                                                 memset(l_current_precinct->cblks.ELEMENT,0,l_nb_code_blocks_size);\
1910                                                                                                                                                                 \
1911                                                 l_current_precinct->block_size = l_nb_code_blocks_size; \
1912                                         }                                                                                                                       \
1913                                         else if (l_nb_code_blocks_size > l_current_precinct->block_size) {      \
1914                                                 l_current_precinct->cblks.ELEMENT = (TYPE*)                                                                     \
1915                                                         opj_realloc(l_current_precinct->cblks.ELEMENT, l_nb_code_blocks_size);  \
1916                                                 if (! l_current_precinct->cblks.ELEMENT ) {                             \
1917                                                         return OPJ_FALSE;                                                                       \
1918                                                 }                                                                                                               \
1919                                                 /*fprintf(stderr, "\t\t\t\tReallocate cblks of a precinct (opj_tcd_cblk_dec_v2_t): from %d to %d\n",l_current_precinct->block_size, l_nb_code_blocks_size);     */      \
1920                                                                                                                                                                 \
1921                                                 memset(((OPJ_BYTE *) l_current_precinct->cblks.ELEMENT) + l_current_precinct->block_size        \
1922                                                                                 ,0                                                                                                                                                      \
1923                                                                                 ,l_nb_code_blocks_size - l_current_precinct->block_size);                                       \
1924                                                                                                                                                                 \
1925                                                 l_current_precinct->block_size = l_nb_code_blocks_size; \
1926                                         }                                                                                                                       \
1927                                                                                                                                                                 \
1928                                         if (! l_current_precinct->incltree) {                                           \
1929                         l_current_precinct->incltree = tgt_create_v2(l_current_precinct->cw,    \
1930                                                                                                                                   l_current_precinct->ch);      \
1931                                         }                                                                                                                       \
1932                                         else{                                                                                                           \
1933                                                 l_current_precinct->incltree = tgt_init(l_current_precinct->incltree,   \
1934                                                                                                                                 l_current_precinct->cw,                 \
1935                                                                                                                                 l_current_precinct->ch);                \
1936                                         }                                                                                                                       \
1937                                                                                                                                                                 \
1938                                         if (! l_current_precinct->incltree)     {                                               \
1939                                                 fprintf(stderr, "WARNING: No incltree created.\n");\
1940                                                 /*return OPJ_FALSE;*/                                                                           \
1941                                         }                                                                                                                       \
1942                                                                                                                                                                 \
1943                                         if (! l_current_precinct->imsbtree) {                                           \
1944                         l_current_precinct->imsbtree = tgt_create_v2(                           \
1945                                                                                                                 l_current_precinct->cw, \
1946                                                                                                                 l_current_precinct->ch);\
1947                                         }                                                                                                                       \
1948                                         else {                                                                                                          \
1949                                                 l_current_precinct->imsbtree = tgt_init(                                                        \
1950                                                                                                                         l_current_precinct->imsbtree,   \
1951                                                                                                                         l_current_precinct->cw,                 \
1952                                                                                                                         l_current_precinct->ch);                \
1953                                         }                                                                                                                       \
1954                                                                                                                                                                 \
1955                                         if (! l_current_precinct->imsbtree) {                                           \
1956                                                 fprintf(stderr, "WARNING: No imsbtree created.\n");\
1957                                                 /*return OPJ_FALSE;*/                                                                           \
1958                                         }                                                                                                                       \
1959                                                                                                                                                                 \
1960                                         l_code_block = l_current_precinct->cblks.ELEMENT;                       \
1961                                                                                                                                                                 \
1962                                         for (cblkno = 0; cblkno < l_nb_code_blocks; ++cblkno) {         \
1963                                                 OPJ_INT32 cblkxstart = tlcblkxstart + (cblkno % l_current_precinct->cw) * (1 << cblkwidthexpn);         \
1964                                                 OPJ_INT32 cblkystart = tlcblkystart + (cblkno / l_current_precinct->cw) * (1 << cblkheightexpn);        \
1965                                                 OPJ_INT32 cblkxend = cblkxstart + (1 << cblkwidthexpn); \
1966                                                 OPJ_INT32 cblkyend = cblkystart + (1 << cblkheightexpn);\
1967                                                                                                                                                                 \
1968                                                 /* code-block size (global) */                                                  \
1969                                                 l_code_block->x0 = int_max(cblkxstart, l_current_precinct->x0); \
1970                                                 l_code_block->y0 = int_max(cblkystart, l_current_precinct->y0); \
1971                                                 l_code_block->x1 = int_min(cblkxend, l_current_precinct->x1);   \
1972                                                 l_code_block->y1 = int_min(cblkyend, l_current_precinct->y1);   \
1973                                                                                                                                                                 \
1974                                                 if (! FUNCTION_ELEMENT(l_code_block)) {                                 \
1975                                                         return OPJ_FALSE;                                                                       \
1976                                                 }                                                                                                               \
1977                                                 ++l_code_block;                                                                                 \
1978                                         }                                                                                                                       \
1979                                         ++l_current_precinct;                                                                           \
1980                                 } /* precno */                                                                                                  \
1981                                 ++l_band;                                                                                                               \
1982                                 ++l_step_size;                                                                                                  \
1983                         } /* bandno */                                                                                                          \
1984                         ++l_res;                                                                                                                        \
1985                         --l_level_no;                                                                                                           \
1986                 } /* resno */                                                                                                                   \
1987                 ++l_tccp;                                                                                                                               \
1988                 ++l_tilec;                                                                                                                              \
1989                 ++l_image_comp;                                                                                                                 \
1990         } /* compno */                                                                                                                          \
1991         return OPJ_TRUE;                                                                                                                        \
1992 }                                                                                                                                                               \
1993
1994
1995 // V2 ENCODE MACRO_TCD_ALLOCATE(tcd_init_encode_tile,opj_tcd_cblk_enc_t,1.f,enc,tcd_code_block_enc_allocate)
1996 MACRO_TCD_ALLOCATE(tcd_init_decode_tile, opj_tcd_cblk_dec_v2_t, 0.5f, dec, tcd_code_block_dec_allocate)
1997
1998 #undef MACRO_TCD_ALLOCATE
1999
2000
2001 OPJ_UINT32 tcd_get_decoded_tile_size ( opj_tcd_v2_t *p_tcd )
2002 {
2003         OPJ_UINT32 i;
2004         OPJ_UINT32 l_data_size = 0;
2005         opj_image_comp_t * l_img_comp = 00;
2006         opj_tcd_tilecomp_v2_t * l_tile_comp = 00;
2007         opj_tcd_resolution_v2_t * l_res = 00;
2008         OPJ_UINT32 l_size_comp, l_remaining;
2009
2010         l_tile_comp = p_tcd->tcd_image->tiles->comps;
2011         l_img_comp = p_tcd->image->comps;
2012
2013         for (i=0;i<p_tcd->image->numcomps;++i) {
2014                 l_size_comp = l_img_comp->prec >> 3; /*(/ 8)*/
2015                 l_remaining = l_img_comp->prec & 7;  /* (%8) */
2016
2017                 if(l_remaining) {
2018                         ++l_size_comp;
2019                 }
2020
2021                 if (l_size_comp == 3) {
2022                         l_size_comp = 4;
2023                 }
2024
2025                 l_res = l_tile_comp->resolutions + l_tile_comp->minimum_num_resolutions - 1;
2026                 l_data_size += l_size_comp * (l_res->x1 - l_res->x0) * (l_res->y1 - l_res->y0);
2027                 ++l_img_comp;
2028                 ++l_tile_comp;
2029         }
2030
2031         return l_data_size;
2032 }
2033
2034
2035 opj_bool tcd_decode_tile_v2(
2036                                          opj_tcd_v2_t *p_tcd,
2037                                          OPJ_BYTE *p_src,
2038                                          OPJ_UINT32 p_max_length,
2039                                          OPJ_UINT32 p_tile_no,
2040                                          opj_codestream_index_t *p_cstr_index)
2041 {
2042         OPJ_UINT32 l_data_read;
2043         p_tcd->tcd_tileno = p_tile_no;
2044         p_tcd->tcp = &(p_tcd->cp->tcps[p_tile_no]);
2045
2046 #ifdef TODO_MSD /* FIXME */
2047         /* INDEX >>  */
2048         if(p_cstr_info) {
2049                 OPJ_UINT32 resno, compno, numprec = 0;
2050                 for (compno = 0; compno < (OPJ_UINT32) p_cstr_info->numcomps; compno++) {
2051                         opj_tcp_v2_t *tcp = &p_tcd->cp->tcps[0];
2052                         opj_tccp_t *tccp = &tcp->tccps[compno];
2053                         opj_tcd_tilecomp_v2_t *tilec_idx = &p_tcd->tcd_image->tiles->comps[compno];
2054                         for (resno = 0; resno < tilec_idx->numresolutions; resno++) {
2055                                 opj_tcd_resolution_v2_t *res_idx = &tilec_idx->resolutions[resno];
2056                                 p_cstr_info->tile[p_tile_no].pw[resno] = res_idx->pw;
2057                                 p_cstr_info->tile[p_tile_no].ph[resno] = res_idx->ph;
2058                                 numprec += res_idx->pw * res_idx->ph;
2059                                 p_cstr_info->tile[p_tile_no].pdx[resno] = tccp->prcw[resno];
2060                                 p_cstr_info->tile[p_tile_no].pdy[resno] = tccp->prch[resno];
2061                         }
2062                 }
2063                 p_cstr_info->tile[p_tile_no].packet = (opj_packet_info_t *) opj_malloc(p_cstr_info->numlayers * numprec * sizeof(opj_packet_info_t));
2064                 p_cstr_info->packno = 0;
2065         }
2066         /* << INDEX */
2067 #endif
2068
2069         /*--------------TIER2------------------*/
2070         // FIXME _ProfStart(PGROUP_T2);
2071         l_data_read = 0;
2072         if
2073                 (! tcd_t2_decode(p_tcd, p_src, &l_data_read, p_max_length, p_cstr_index))
2074         {
2075                 return OPJ_FALSE;
2076         }
2077         // FIXME _ProfStop(PGROUP_T2);
2078
2079         /*------------------TIER1-----------------*/
2080
2081         // FIXME _ProfStart(PGROUP_T1);
2082         if
2083                 (! tcd_t1_decode(p_tcd))
2084         {
2085                 return OPJ_FALSE;
2086         }
2087         // FIXME _ProfStop(PGROUP_T1);
2088
2089         /*----------------DWT---------------------*/
2090
2091         // FIXME _ProfStart(PGROUP_DWT);
2092         if
2093                 (! tcd_dwt_decode(p_tcd))
2094         {
2095                 return OPJ_FALSE;
2096         }
2097         // FIXME _ProfStop(PGROUP_DWT);
2098
2099         /*----------------MCT-------------------*/
2100         // FIXME _ProfStart(PGROUP_MCT);
2101         if
2102                 (! tcd_mct_decode(p_tcd))
2103         {
2104                 return OPJ_FALSE;
2105         }
2106         // FIXME _ProfStop(PGROUP_MCT);
2107
2108         // FIXME _ProfStart(PGROUP_DC_SHIFT);
2109         if
2110                 (! tcd_dc_level_shift_decode(p_tcd))
2111         {
2112                 return OPJ_FALSE;
2113         }
2114         // FIXME _ProfStop(PGROUP_DC_SHIFT);
2115
2116
2117         /*---------------TILE-------------------*/
2118         return OPJ_TRUE;
2119 }
2120
2121 opj_bool tcd_update_tile_data (
2122                                                  opj_tcd_v2_t *p_tcd,
2123                                                  OPJ_BYTE * p_dest,
2124                                                  OPJ_UINT32 p_dest_length
2125                                                  )
2126 {
2127         OPJ_UINT32 i,j,k,l_data_size = 0;
2128         opj_image_comp_t * l_img_comp = 00;
2129         opj_tcd_tilecomp_v2_t * l_tilec = 00;
2130         opj_tcd_resolution_v2_t * l_res;
2131         OPJ_UINT32 l_size_comp, l_remaining;
2132         OPJ_UINT32 l_stride, l_width,l_height;
2133
2134         l_data_size = tcd_get_decoded_tile_size(p_tcd);
2135         if (l_data_size > p_dest_length) {
2136                 return OPJ_FALSE;
2137         }
2138
2139         l_tilec = p_tcd->tcd_image->tiles->comps;
2140         l_img_comp = p_tcd->image->comps;
2141
2142         for (i=0;i<p_tcd->image->numcomps;++i) {
2143                 l_size_comp = l_img_comp->prec >> 3; /*(/ 8)*/
2144                 l_remaining = l_img_comp->prec & 7;  /* (%8) */
2145                 l_res = l_tilec->resolutions + l_img_comp->resno_decoded;
2146                 l_width = (l_res->x1 - l_res->x0);
2147                 l_height = (l_res->y1 - l_res->y0);
2148                 l_stride = (l_tilec->x1 - l_tilec->x0) - l_width;
2149
2150                 if (l_remaining) {
2151                         ++l_size_comp;
2152                 }
2153
2154                 if (l_size_comp == 3) {
2155                         l_size_comp = 4;
2156                 }
2157
2158                 switch (l_size_comp)
2159                         {
2160                         case 1:
2161                                 {
2162                                         OPJ_CHAR * l_dest_ptr = (OPJ_CHAR *) p_dest;
2163                                         const OPJ_INT32 * l_src_ptr = l_tilec->data;
2164
2165                                         if (l_img_comp->sgnd) {
2166                                                 for (j=0;j<l_height;++j) {
2167                                                         for (k=0;k<l_width;++k) {
2168                                                                 *(l_dest_ptr++) = (OPJ_CHAR) (*(l_src_ptr++));
2169                                                         }
2170                                                         l_src_ptr += l_stride;
2171                                                 }
2172                                         }
2173                                         else {
2174                                                 for (j=0;j<l_height;++j) {
2175                                                         for     (k=0;k<l_width;++k) {
2176                                                                 *(l_dest_ptr++) = (OPJ_BYTE) ((*(l_src_ptr++))&0xff);
2177                                                         }
2178                                                         l_src_ptr += l_stride;
2179                                                 }
2180                                         }
2181
2182                                         p_dest = (OPJ_BYTE *)l_dest_ptr;
2183                                 }
2184                                 break;
2185                         case 2:
2186                                 {
2187                                         const OPJ_INT32 * l_src_ptr = l_tilec->data;
2188                                         OPJ_INT16 * l_dest_ptr = (OPJ_INT16 *) p_dest;
2189
2190                                         if (l_img_comp->sgnd) {
2191                                                 for (j=0;j<l_height;++j) {
2192                                                         for (k=0;k<l_width;++k) {
2193                                                                 *(l_dest_ptr++) = (OPJ_INT16) (*(l_src_ptr++));
2194                                                         }
2195                                                         l_src_ptr += l_stride;
2196                                                 }
2197                                         }
2198                                         else {
2199                                                 for (j=0;j<l_height;++j) {
2200                                                         for (k=0;k<l_width;++k) {
2201                                                                 *(l_dest_ptr++) = (OPJ_UINT16) ((*(l_src_ptr++))&0xffff);
2202                                                         }
2203                                                         l_src_ptr += l_stride;
2204                                                 }
2205                                         }
2206
2207                                         p_dest = (OPJ_BYTE*) l_dest_ptr;
2208                                 }
2209                                 break;
2210                         case 4:
2211                                 {
2212                                         OPJ_INT32 * l_dest_ptr = (OPJ_INT32 *) p_dest;
2213                                         OPJ_INT32 * l_src_ptr = l_tilec->data;
2214
2215                                         for (j=0;j<l_height;++j) {
2216                                                 for (k=0;k<l_width;++k) {
2217                                                         *(l_dest_ptr++) = (*(l_src_ptr++));
2218                                                 }
2219                                                 l_src_ptr += l_stride;
2220                                         }
2221
2222                                         p_dest = (OPJ_BYTE*) l_dest_ptr;
2223                                 }
2224                                 break;
2225                 }
2226
2227                 ++l_img_comp;
2228                 ++l_tilec;
2229         }
2230
2231         return OPJ_TRUE;
2232 }
2233
2234
2235
2236
2237 void tcd_free_tile(opj_tcd_v2_t *p_tcd)
2238 {
2239         OPJ_UINT32 compno, resno, bandno, precno;
2240         opj_tcd_tile_v2_t *l_tile = 00;
2241         opj_tcd_tilecomp_v2_t *l_tile_comp = 00;
2242         opj_tcd_resolution_v2_t *l_res = 00;
2243         opj_tcd_band_v2_t *l_band = 00;
2244         opj_tcd_precinct_v2_t *l_precinct = 00;
2245         OPJ_UINT32 l_nb_resolutions, l_nb_precincts;
2246         void (* l_tcd_code_block_deallocate) (opj_tcd_precinct_v2_t *) = 00;
2247
2248         if (! p_tcd) {
2249                 return;
2250         }
2251
2252         if (! p_tcd->tcd_image) {
2253                 return;
2254         }
2255
2256         if (p_tcd->m_is_decoder) {
2257                 l_tcd_code_block_deallocate = tcd_code_block_dec_deallocate;
2258         }
2259         else {
2260                 // FIXME l_tcd_code_block_deallocate = tcd_code_block_enc_deallocate;
2261         }
2262
2263         l_tile = p_tcd->tcd_image->tiles;
2264         if (! l_tile) {
2265                 return;
2266         }
2267
2268         l_tile_comp = l_tile->comps;
2269
2270         for (compno = 0; compno < l_tile->numcomps; ++compno) {
2271                 l_res = l_tile_comp->resolutions;
2272                 if (l_res) {
2273
2274                         l_nb_resolutions = l_tile_comp->resolutions_size / sizeof(opj_tcd_resolution_v2_t);
2275                         for (resno = 0; resno < l_nb_resolutions; ++resno) {
2276                                 l_band = l_res->bands;
2277                                 for     (bandno = 0; bandno < 3; ++bandno) {
2278                                         l_precinct = l_band->precincts;
2279                                         if (l_precinct) {
2280
2281                                                 l_nb_precincts = l_band->precincts_data_size / sizeof(opj_tcd_precinct_v2_t);
2282                                                 for (precno = 0; precno < l_nb_precincts; ++precno) {
2283                                                         tgt_destroy(l_precinct->incltree);
2284                                                         l_precinct->incltree = 00;
2285                                                         tgt_destroy(l_precinct->imsbtree);
2286                                                         l_precinct->imsbtree = 00;
2287                                                         (*l_tcd_code_block_deallocate) (l_precinct);
2288                                                         ++l_precinct;
2289                                                 }
2290
2291                                                 opj_free(l_band->precincts);
2292                                                 l_band->precincts = 00;
2293                                         }
2294                                         ++l_band;
2295                                 } /* for (resno */
2296                                 ++l_res;
2297                         }
2298
2299                         opj_free(l_tile_comp->resolutions);
2300                         l_tile_comp->resolutions = 00;
2301                 }
2302
2303                 if (l_tile_comp->data) {
2304                         opj_free(l_tile_comp->data);
2305                         l_tile_comp->data = 00;
2306                 }
2307                 ++l_tile_comp;
2308         }
2309
2310         opj_free(l_tile->comps);
2311         l_tile->comps = 00;
2312         opj_free(p_tcd->tcd_image->tiles);
2313         p_tcd->tcd_image->tiles = 00;
2314 }
2315
2316
2317 /**
2318  * Allocates memory for a decoding code block.
2319  */
2320 opj_bool tcd_code_block_dec_allocate (opj_tcd_cblk_dec_v2_t * p_code_block)
2321 {
2322         OPJ_UINT32 l_seg_size;
2323
2324         if (! p_code_block->data) {
2325
2326                 p_code_block->data = (OPJ_BYTE*) opj_malloc(8192);
2327                 if (! p_code_block->data) {
2328                         return OPJ_FALSE;
2329                 }
2330                 /*fprintf(stderr, "Allocate 8192 elements of code_block->data\n");*/
2331
2332                 l_seg_size = J2K_DEFAULT_NB_SEGS * sizeof(opj_tcd_seg_t);
2333                 p_code_block->segs = (opj_tcd_seg_t *) opj_malloc(l_seg_size);
2334                 if (! p_code_block->segs) {
2335                         return OPJ_FALSE;
2336                 }
2337                 memset(p_code_block->segs,0,l_seg_size);
2338                 /*fprintf(stderr, "Allocate %d elements of code_block->data\n", J2K_DEFAULT_NB_SEGS * sizeof(opj_tcd_seg_t));*/
2339
2340                 p_code_block->m_current_max_segs = J2K_DEFAULT_NB_SEGS;
2341                 /*fprintf(stderr, "m_current_max_segs of code_block->data = %d\n", p_code_block->m_current_max_segs);*/
2342         }
2343         // TODO
2344         //p_code_block->numsegs = 0;
2345
2346         return OPJ_TRUE;
2347 }
2348
2349 opj_bool tcd_t2_decode (
2350                                         opj_tcd_v2_t *p_tcd,
2351                                         OPJ_BYTE * p_src_data,
2352                                         OPJ_UINT32 * p_data_read,
2353                                         OPJ_UINT32 p_max_src_size,
2354                                         opj_codestream_index_t *p_cstr_index
2355                                         )
2356 {
2357         opj_t2_v2_t * l_t2;
2358
2359         l_t2 = t2_create_v2(p_tcd->image, p_tcd->cp);
2360         if (l_t2 == 00) {
2361                 return OPJ_FALSE;
2362         }
2363
2364         if (! t2_decode_packets_v2(
2365                                         l_t2,
2366                                         p_tcd->tcd_tileno,
2367                                         p_tcd->tcd_image->tiles,
2368                                         p_src_data,
2369                                         p_data_read,
2370                                         p_max_src_size,
2371                                         p_cstr_index)) {
2372                 t2_destroy_v2(l_t2);
2373                 return OPJ_FALSE;
2374         }
2375
2376         t2_destroy_v2(l_t2);
2377
2378         /*---------------CLEAN-------------------*/
2379         return OPJ_TRUE;
2380 }
2381
2382 opj_bool tcd_t1_decode ( opj_tcd_v2_t *p_tcd )
2383 {
2384         OPJ_UINT32 compno;
2385         opj_t1_t * l_t1;
2386         opj_tcd_tile_v2_t * l_tile = p_tcd->tcd_image->tiles;
2387         opj_tcd_tilecomp_v2_t* l_tile_comp = l_tile->comps;
2388         opj_tccp_t * l_tccp = p_tcd->tcp->tccps;
2389
2390
2391         l_t1 = t1_create_v2();
2392         if (l_t1 == 00) {
2393                 return OPJ_FALSE;
2394         }
2395
2396         for (compno = 0; compno < l_tile->numcomps; ++compno) {
2397                 /* The +3 is headroom required by the vectorized DWT */
2398                 t1_decode_cblks_v2(l_t1, l_tile_comp, l_tccp);
2399                 ++l_tile_comp;
2400                 ++l_tccp;
2401         }
2402
2403         t1_destroy_v2(l_t1);
2404
2405         return OPJ_TRUE;
2406 }
2407
2408
2409 opj_bool tcd_dwt_decode ( opj_tcd_v2_t *p_tcd )
2410 {
2411         OPJ_UINT32 compno;
2412         opj_tcd_tile_v2_t * l_tile = p_tcd->tcd_image->tiles;
2413         opj_tcd_tilecomp_v2_t * l_tile_comp = l_tile->comps;
2414         opj_tccp_t * l_tccp = p_tcd->tcp->tccps;
2415         opj_image_comp_t * l_img_comp = p_tcd->image->comps;
2416
2417         for (compno = 0; compno < l_tile->numcomps; compno++) {
2418                 /*
2419                 if (tcd->cp->reduce != 0) {
2420                         tcd->image->comps[compno].resno_decoded =
2421                                 tile->comps[compno].numresolutions - tcd->cp->reduce - 1;
2422                         if (tcd->image->comps[compno].resno_decoded < 0)
2423                         {
2424                                 return false;
2425                         }
2426                 }
2427                 numres2decode = tcd->image->comps[compno].resno_decoded + 1;
2428                 if(numres2decode > 0){
2429                 */
2430
2431                 if (l_tccp->qmfbid == 1) {
2432                         if (! dwt_decode_v2(l_tile_comp, l_img_comp->resno_decoded+1)) {
2433                                 return OPJ_FALSE;
2434                         }
2435                 }
2436                 else {
2437                         if (! dwt_decode_real_v2(l_tile_comp, l_img_comp->resno_decoded+1)) {
2438                                 return OPJ_FALSE;
2439                         }
2440                 }
2441
2442                 ++l_tile_comp;
2443                 ++l_img_comp;
2444                 ++l_tccp;
2445         }
2446
2447         return OPJ_TRUE;
2448 }
2449 opj_bool tcd_mct_decode ( opj_tcd_v2_t *p_tcd )
2450 {
2451         opj_tcd_tile_v2_t * l_tile = p_tcd->tcd_image->tiles;
2452         opj_tcp_v2_t * l_tcp = p_tcd->tcp;
2453         opj_tcd_tilecomp_v2_t * l_tile_comp = l_tile->comps;
2454         OPJ_UINT32 l_samples,i;
2455
2456         if (! l_tcp->mct) {
2457                 return OPJ_TRUE;
2458         }
2459
2460         l_samples = (l_tile_comp->x1 - l_tile_comp->x0) * (l_tile_comp->y1 - l_tile_comp->y0);
2461
2462         if (l_tile->numcomps >= 3 ){
2463                 if (l_tcp->mct == 2) {
2464                         OPJ_BYTE ** l_data;
2465
2466                         if (! l_tcp->m_mct_decoding_matrix) {
2467                                 return OPJ_TRUE;
2468                         }
2469
2470                         l_data = (OPJ_BYTE **) opj_malloc(l_tile->numcomps*sizeof(OPJ_BYTE*));
2471                         if (! l_data) {
2472                                 return OPJ_FALSE;
2473                         }
2474
2475                         for (i=0;i<l_tile->numcomps;++i) {
2476                                 l_data[i] = (OPJ_BYTE*) l_tile_comp->data;
2477                                 ++l_tile_comp;
2478                         }
2479
2480                         if (! mct_decode_custom(// MCT data
2481                                                                         (OPJ_BYTE*) l_tcp->m_mct_decoding_matrix,
2482                                                                         // size of components
2483                                                                         l_samples,
2484                                                                         // components
2485                                                                         l_data,
2486                                                                         // nb of components (i.e. size of pData)
2487                                                                         l_tile->numcomps,
2488                                                                         // tells if the data is signed
2489                                                                         p_tcd->image->comps->sgnd)) {
2490                                 opj_free(l_data);
2491                                 return OPJ_FALSE;
2492                         }
2493
2494                         opj_free(l_data);
2495                 }
2496                 else {
2497                         if (l_tcp->tccps->qmfbid == 1) {
2498                                 mct_decode(     l_tile->comps[0].data,
2499                                                         l_tile->comps[1].data,
2500                                                         l_tile->comps[2].data,
2501                                                         l_samples);
2502                         }
2503                         else {
2504                                 mct_decode_real(        (float*)l_tile->comps[0].data,
2505                                                                         (float*)l_tile->comps[1].data,
2506                                                                         (float*)l_tile->comps[2].data,
2507                                                                         l_samples);
2508                         }
2509                 }
2510         }
2511         else {
2512                 /* FIXME need to use opj_event_msg_v2 function */
2513                 fprintf(stderr,"Number of components (%d) is inconsistent with a MCT. Skip the MCT step.\n",l_tile->numcomps);
2514         }
2515
2516         return OPJ_TRUE;
2517 }
2518
2519
2520 opj_bool tcd_dc_level_shift_decode ( opj_tcd_v2_t *p_tcd )
2521 {
2522         OPJ_UINT32 compno;
2523         opj_tcd_tilecomp_v2_t * l_tile_comp = 00;
2524         opj_tccp_t * l_tccp = 00;
2525         opj_image_comp_t * l_img_comp = 00;
2526         opj_tcd_resolution_v2_t* l_res = 00;
2527         opj_tcp_v2_t * l_tcp = 00;
2528         opj_tcd_tile_v2_t * l_tile;
2529         OPJ_UINT32 l_width,l_height,i,j;
2530         OPJ_INT32 * l_current_ptr;
2531         OPJ_INT32 l_min, l_max;
2532         OPJ_UINT32 l_stride;
2533
2534         l_tile = p_tcd->tcd_image->tiles;
2535         l_tile_comp = l_tile->comps;
2536         l_tcp = p_tcd->tcp;
2537         l_tccp = p_tcd->tcp->tccps;
2538         l_img_comp = p_tcd->image->comps;
2539
2540         for (compno = 0; compno < l_tile->numcomps; compno++) {
2541                 l_res = l_tile_comp->resolutions + l_img_comp->resno_decoded;
2542                 l_width = (l_res->x1 - l_res->x0);
2543                 l_height = (l_res->y1 - l_res->y0);
2544                 l_stride = (l_tile_comp->x1 - l_tile_comp->x0) - l_width;
2545
2546                 if (l_img_comp->sgnd) {
2547                         l_min = -(1 << (l_img_comp->prec - 1));
2548                         l_max = (1 << (l_img_comp->prec - 1)) - 1;
2549                 }
2550                 else {
2551             l_min = 0;
2552                         l_max = (1 << l_img_comp->prec) - 1;
2553                 }
2554
2555                 l_current_ptr = l_tile_comp->data;
2556
2557                 if (l_tccp->qmfbid == 1) {
2558                         for (j=0;j<l_height;++j) {
2559                                 for (i = 0; i < l_width; ++i) {
2560                                         *l_current_ptr = int_clamp(*l_current_ptr + l_tccp->m_dc_level_shift, l_min, l_max);
2561                                         ++l_current_ptr;
2562                                 }
2563                                 l_current_ptr += l_stride;
2564                         }
2565                 }
2566                 else {
2567                         for (j=0;j<l_height;++j) {
2568                                 for (i = 0; i < l_width; ++i) {
2569                                         OPJ_FLOAT32 l_value = *((OPJ_FLOAT32 *) l_current_ptr);
2570                                         *l_current_ptr = int_clamp(lrintf(l_value) + l_tccp->m_dc_level_shift, l_min, l_max); ;
2571                                         ++l_current_ptr;
2572                                 }
2573                                 l_current_ptr += l_stride;
2574                         }
2575                 }
2576
2577                 ++l_img_comp;
2578                 ++l_tccp;
2579                 ++l_tile_comp;
2580         }
2581
2582         return OPJ_TRUE;
2583 }
2584
2585
2586
2587 /**
2588  * Deallocates the encoding data of the given precinct.
2589  */
2590 void tcd_code_block_dec_deallocate (opj_tcd_precinct_v2_t * p_precinct)
2591 {
2592         OPJ_UINT32 cblkno , l_nb_code_blocks;
2593
2594         opj_tcd_cblk_dec_v2_t * l_code_block = p_precinct->cblks.dec;
2595         if (l_code_block) {
2596                 /*fprintf(stderr,"deallocate codeblock:{\n");*/
2597                 /*fprintf(stderr,"\t x0=%d, y0=%d, x1=%d, y1=%d\n",l_code_block->x0, l_code_block->y0, l_code_block->x1, l_code_block->y1);*/
2598                 /*fprintf(stderr,"\t numbps=%d, numlenbits=%d, len=%d, numnewpasses=%d, real_num_segs=%d, m_current_max_segs=%d\n ",
2599                                 l_code_block->numbps, l_code_block->numlenbits, l_code_block->len, l_code_block->numnewpasses, l_code_block->real_num_segs, l_code_block->m_current_max_segs );*/
2600
2601
2602                 l_nb_code_blocks = p_precinct->block_size / sizeof(opj_tcd_cblk_dec_v2_t);
2603                 /*fprintf(stderr,"nb_code_blocks =%d\t}\n", l_nb_code_blocks);*/
2604
2605                 for (cblkno = 0; cblkno < l_nb_code_blocks; ++cblkno) {
2606
2607                         if (l_code_block->data) {
2608                                 opj_free(l_code_block->data);
2609                                 l_code_block->data = 00;
2610                         }
2611
2612                         if (l_code_block->segs) {
2613                                 opj_free(l_code_block->segs );
2614                                 l_code_block->segs = 00;
2615                         }
2616
2617                         ++l_code_block;
2618                 }
2619
2620                 opj_free(p_precinct->cblks.dec);
2621                 p_precinct->cblks.dec = 00;
2622         }
2623 }
2624
2625