[trunk] replace more void* object pointer to proper function pointer: opj_procedure...
[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 /* ----------------------------------------------------------------------- */
36
37 static opj_bool tcd_dc_level_shift_encode ( opj_tcd_v2_t *p_tcd );
38
39 static opj_bool tcd_mct_encode ( opj_tcd_v2_t *p_tcd );
40
41 static opj_bool tcd_dwt_encode ( opj_tcd_v2_t *p_tcd );
42
43 static opj_bool tcd_t1_encode ( opj_tcd_v2_t *p_tcd );
44
45 static opj_bool tcd_t2_encode ( opj_tcd_v2_t *p_tcd,
46                                                                 OPJ_BYTE * p_dest_data,
47                                                                 OPJ_UINT32 * p_data_written,
48                                                                 OPJ_UINT32 p_max_dest_size,
49                                                                 opj_codestream_info_t *p_cstr_info );
50
51 static opj_bool tcd_rate_allocate_encode(       opj_tcd_v2_t *p_tcd,
52                                                                                         OPJ_BYTE * p_dest_data,
53                                                                                         OPJ_UINT32 p_max_dest_size,
54                                                                                         opj_codestream_info_t *p_cstr_info );
55
56 /* ----------------------------------------------------------------------- */
57
58
59 void tcd_dump(FILE *fd, opj_tcd_t *tcd, opj_tcd_image_t * img) {
60         int tileno, compno, resno, bandno, precno;/*, cblkno;*/
61
62         fprintf(fd, "image {\n");
63         fprintf(fd, "  tw=%d, th=%d x0=%d x1=%d y0=%d y1=%d\n", 
64                 img->tw, img->th, tcd->image->x0, tcd->image->x1, tcd->image->y0, tcd->image->y1);
65
66         for (tileno = 0; tileno < img->th * img->tw; tileno++) {
67                 opj_tcd_tile_t *tile = &tcd->tcd_image->tiles[tileno];
68                 fprintf(fd, "  tile {\n");
69                 fprintf(fd, "    x0=%d, y0=%d, x1=%d, y1=%d, numcomps=%d\n",
70                         tile->x0, tile->y0, tile->x1, tile->y1, tile->numcomps);
71                 for (compno = 0; compno < tile->numcomps; compno++) {
72                         opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
73                         fprintf(fd, "    tilec {\n");
74                         fprintf(fd,
75                                 "      x0=%d, y0=%d, x1=%d, y1=%d, numresolutions=%d\n",
76                                 tilec->x0, tilec->y0, tilec->x1, tilec->y1, tilec->numresolutions);
77                         for (resno = 0; resno < tilec->numresolutions; resno++) {
78                                 opj_tcd_resolution_t *res = &tilec->resolutions[resno];
79                                 fprintf(fd, "\n   res {\n");
80                                 fprintf(fd,
81                                         "          x0=%d, y0=%d, x1=%d, y1=%d, pw=%d, ph=%d, numbands=%d\n",
82                                         res->x0, res->y0, res->x1, res->y1, res->pw, res->ph, res->numbands);
83                                 for (bandno = 0; bandno < res->numbands; bandno++) {
84                                         opj_tcd_band_t *band = &res->bands[bandno];
85                                         fprintf(fd, "        band {\n");
86                                         fprintf(fd,
87                                                 "          x0=%d, y0=%d, x1=%d, y1=%d, stepsize=%f, numbps=%d\n",
88                                                 band->x0, band->y0, band->x1, band->y1, band->stepsize, band->numbps);
89                                         for (precno = 0; precno < res->pw * res->ph; precno++) {
90                                                 opj_tcd_precinct_t *prec = &band->precincts[precno];
91                                                 fprintf(fd, "          prec {\n");
92                                                 fprintf(fd,
93                                                         "            x0=%d, y0=%d, x1=%d, y1=%d, cw=%d, ch=%d\n",
94                                                         prec->x0, prec->y0, prec->x1, prec->y1, prec->cw, prec->ch);
95                                                 /*
96                                                 for (cblkno = 0; cblkno < prec->cw * prec->ch; cblkno++) {
97                                                         opj_tcd_cblk_t *cblk = &prec->cblks[cblkno];
98                                                         fprintf(fd, "            cblk {\n");
99                                                         fprintf(fd,
100                                                                 "              x0=%d, y0=%d, x1=%d, y1=%d\n",
101                                                                 cblk->x0, cblk->y0, cblk->x1, cblk->y1);
102                                                         fprintf(fd, "            }\n");
103                                                 }
104                                                 */
105                                                 fprintf(fd, "          }\n");
106                                         }
107                                         fprintf(fd, "        }\n");
108                                 }
109                                 fprintf(fd, "      }\n");
110                         }
111                         fprintf(fd, "    }\n");
112                 }
113                 fprintf(fd, "  }\n");
114         }
115         fprintf(fd, "}\n");
116 }
117 /**
118 * Allocates memory for a decoding code block.
119 */
120 static opj_bool tcd_code_block_dec_allocate (opj_tcd_cblk_dec_v2_t * p_code_block);
121
122 /**
123  * Allocates memory for an encoding code block.
124  */
125 static opj_bool tcd_code_block_enc_allocate (opj_tcd_cblk_enc_v2_t * p_code_block);
126
127
128 /**
129 Free the memory allocated for encoding
130 @param tcd TCD handle
131 */
132 static void tcd_free_tile(opj_tcd_v2_t *tcd);
133
134
135 opj_bool tcd_t2_decode (
136                                         opj_tcd_v2_t *p_tcd,
137                                         OPJ_BYTE * p_src_data,
138                                         OPJ_UINT32 * p_data_read,
139                                         OPJ_UINT32 p_max_src_size,
140                                         opj_codestream_index_t *p_cstr_index
141                                         );
142
143 opj_bool tcd_t1_decode (
144                                           opj_tcd_v2_t *p_tcd
145                                          );
146
147 opj_bool tcd_dwt_decode (
148                                           opj_tcd_v2_t *p_tcd
149                                          );
150
151 opj_bool tcd_mct_decode (
152                                           opj_tcd_v2_t *p_tcd
153                                          );
154
155 opj_bool tcd_dc_level_shift_decode (
156                                                  opj_tcd_v2_t *p_tcd
157                                                  );
158
159 void tcd_code_block_dec_deallocate (opj_tcd_precinct_v2_t * p_precinct);
160 /* ----------------------------------------------------------------------- */
161
162 /**
163 Create a new TCD handle
164 */
165 opj_tcd_t* tcd_create(opj_common_ptr cinfo) {
166         /* create the tcd structure */
167         opj_tcd_t *tcd = (opj_tcd_t*)opj_malloc(sizeof(opj_tcd_t));
168         if(!tcd) return NULL;
169         tcd->cinfo = cinfo;
170         tcd->tcd_image = (opj_tcd_image_t*)opj_malloc(sizeof(opj_tcd_image_t));
171         if(!tcd->tcd_image) {
172                 opj_free(tcd);
173                 return NULL;
174         }
175
176         return tcd;
177 }
178
179 /**
180 Create a new TCD handle
181 */
182 opj_tcd_v2_t* tcd_create_v2(opj_bool p_is_decoder)
183 {
184         opj_tcd_v2_t *l_tcd = 00;
185
186         /* create the tcd structure */
187         l_tcd = (opj_tcd_v2_t*) opj_malloc(sizeof(opj_tcd_v2_t));
188         if (!l_tcd) {
189                 return 00;
190         }
191         memset(l_tcd,0,sizeof(opj_tcd_v2_t));
192
193         l_tcd->m_is_decoder = p_is_decoder ? 1 : 0;
194
195         l_tcd->tcd_image = (opj_tcd_image_v2_t*)opj_malloc(sizeof(opj_tcd_image_v2_t));
196         if (!l_tcd->tcd_image) {
197                 opj_free(l_tcd);
198                 return 00;
199         }
200         memset(l_tcd->tcd_image,0,sizeof(opj_tcd_image_v2_t));
201
202         return l_tcd;
203 }
204
205 /**
206 Destroy a previously created TCD handle
207 */
208 void tcd_destroy(opj_tcd_t *tcd) {
209         if(tcd) {
210                 opj_free(tcd->tcd_image);
211                 opj_free(tcd);
212         }
213 }
214
215 /* ----------------------------------------------------------------------- */
216
217 void tcd_malloc_encode(opj_tcd_t *tcd, opj_image_t * image, opj_cp_t * cp, int curtileno) {
218         int tileno, compno, resno, bandno, precno, cblkno;
219
220         tcd->image = image;
221         tcd->cp = cp;
222         tcd->tcd_image->tw = cp->tw;
223         tcd->tcd_image->th = cp->th;
224         tcd->tcd_image->tiles = (opj_tcd_tile_t *) opj_malloc(sizeof(opj_tcd_tile_t));
225         
226         for (tileno = 0; tileno < 1; tileno++) {
227                 opj_tcp_t *tcp = &cp->tcps[curtileno];
228                 int j;
229
230                 /* cfr p59 ISO/IEC FDIS15444-1 : 2000 (18 august 2000) */
231                 int p = curtileno % cp->tw;     /* si numerotation matricielle .. */
232                 int q = curtileno / cp->tw;     /* .. coordonnees de la tile (q,p) q pour ligne et p pour colonne */
233
234                 /* opj_tcd_tile_t *tile=&tcd->tcd_image->tiles[tileno]; */
235                 opj_tcd_tile_t *tile = tcd->tcd_image->tiles;
236
237                 /* 4 borders of the tile rescale on the image if necessary */
238                 tile->x0 = int_max(cp->tx0 + p * cp->tdx, image->x0);
239                 tile->y0 = int_max(cp->ty0 + q * cp->tdy, image->y0);
240                 tile->x1 = int_min(cp->tx0 + (p + 1) * cp->tdx, image->x1);
241                 tile->y1 = int_min(cp->ty0 + (q + 1) * cp->tdy, image->y1);
242                 tile->numcomps = image->numcomps;
243                 /* tile->PPT=image->PPT;  */
244
245                 /* Modification of the RATE >> */
246                 for (j = 0; j < tcp->numlayers; j++) {
247                         tcp->rates[j] = tcp->rates[j] ? 
248                                 cp->tp_on ? 
249                                         (((float) (tile->numcomps 
250                                         * (tile->x1 - tile->x0) 
251                                         * (tile->y1 - tile->y0)
252                                         * image->comps[0].prec))
253                                         /(tcp->rates[j] * 8 * image->comps[0].dx * image->comps[0].dy)) - (((tcd->cur_totnum_tp - 1) * 14 )/ tcp->numlayers)
254                                         :
255                                 ((float) (tile->numcomps 
256                                         * (tile->x1 - tile->x0) 
257                                         * (tile->y1 - tile->y0) 
258                                         * image->comps[0].prec))/ 
259                                         (tcp->rates[j] * 8 * image->comps[0].dx * image->comps[0].dy)
260                                         : 0;
261
262                         if (tcp->rates[j]) {
263                                 if (j && tcp->rates[j] < tcp->rates[j - 1] + 10) {
264                                         tcp->rates[j] = tcp->rates[j - 1] + 20;
265                                 } else {
266                                         if (!j && tcp->rates[j] < 30)
267                                                 tcp->rates[j] = 30;
268                                 }
269                                 
270                                 if(j == (tcp->numlayers-1)){
271                                         tcp->rates[j] = tcp->rates[j]- 2;
272                                 }
273                         }
274                 }
275                 /* << Modification of the RATE */
276                 
277                 tile->comps = (opj_tcd_tilecomp_t *) opj_malloc(image->numcomps * sizeof(opj_tcd_tilecomp_t));
278                 for (compno = 0; compno < tile->numcomps; compno++) {
279                         opj_tccp_t *tccp = &tcp->tccps[compno];
280
281                         opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
282
283                         /* border of each tile component (global) */
284                         tilec->x0 = int_ceildiv(tile->x0, image->comps[compno].dx);
285                         tilec->y0 = int_ceildiv(tile->y0, image->comps[compno].dy);
286                         tilec->x1 = int_ceildiv(tile->x1, image->comps[compno].dx);
287                         tilec->y1 = int_ceildiv(tile->y1, image->comps[compno].dy);
288                         
289                         tilec->data = (int *) opj_aligned_malloc((tilec->x1 - tilec->x0) * (tilec->y1 - tilec->y0) * sizeof(int));
290                         tilec->numresolutions = tccp->numresolutions;
291
292                         tilec->resolutions = (opj_tcd_resolution_t *) opj_malloc(tilec->numresolutions * sizeof(opj_tcd_resolution_t));
293                         
294                         for (resno = 0; resno < tilec->numresolutions; resno++) {
295                                 int pdx, pdy;
296                                 int levelno = tilec->numresolutions - 1 - resno;
297                                 int tlprcxstart, tlprcystart, brprcxend, brprcyend;
298                                 int tlcbgxstart, tlcbgystart, brcbgxend, brcbgyend;
299                                 int cbgwidthexpn, cbgheightexpn;
300                                 int cblkwidthexpn, cblkheightexpn;
301
302                                 opj_tcd_resolution_t *res = &tilec->resolutions[resno];
303                                 
304                                 /* border for each resolution level (global) */
305                                 res->x0 = int_ceildivpow2(tilec->x0, levelno);
306                                 res->y0 = int_ceildivpow2(tilec->y0, levelno);
307                                 res->x1 = int_ceildivpow2(tilec->x1, levelno);
308                                 res->y1 = int_ceildivpow2(tilec->y1, levelno);
309                                 
310                                 res->numbands = resno == 0 ? 1 : 3;
311                                 /* p. 35, table A-23, ISO/IEC FDIS154444-1 : 2000 (18 august 2000) */
312                                 if (tccp->csty & J2K_CCP_CSTY_PRT) {
313                                         pdx = tccp->prcw[resno];
314                                         pdy = tccp->prch[resno];
315                                 } else {
316                                         pdx = 15;
317                                         pdy = 15;
318                                 }
319                                 /* p. 64, B.6, ISO/IEC FDIS15444-1 : 2000 (18 august 2000)  */
320                                 tlprcxstart = int_floordivpow2(res->x0, pdx) << pdx;
321                                 tlprcystart = int_floordivpow2(res->y0, pdy) << pdy;
322                                 
323                                 brprcxend = int_ceildivpow2(res->x1, pdx) << pdx;
324                                 brprcyend = int_ceildivpow2(res->y1, pdy) << pdy;
325                                 
326                                 res->pw = (brprcxend - tlprcxstart) >> pdx;
327                                 res->ph = (brprcyend - tlprcystart) >> pdy;
328                                 
329                                 if (resno == 0) {
330                                         tlcbgxstart = tlprcxstart;
331                                         tlcbgystart = tlprcystart;
332                                         brcbgxend = brprcxend;
333                                         brcbgyend = brprcyend;
334                                         cbgwidthexpn = pdx;
335                                         cbgheightexpn = pdy;
336                                 } else {
337                                         tlcbgxstart = int_ceildivpow2(tlprcxstart, 1);
338                                         tlcbgystart = int_ceildivpow2(tlprcystart, 1);
339                                         brcbgxend = int_ceildivpow2(brprcxend, 1);
340                                         brcbgyend = int_ceildivpow2(brprcyend, 1);
341                                         cbgwidthexpn = pdx - 1;
342                                         cbgheightexpn = pdy - 1;
343                                 }
344                                 
345                                 cblkwidthexpn = int_min(tccp->cblkw, cbgwidthexpn);
346                                 cblkheightexpn = int_min(tccp->cblkh, cbgheightexpn);
347                                 
348                                 for (bandno = 0; bandno < res->numbands; bandno++) {
349                                         int x0b, y0b, i;
350                                         int gain, numbps;
351                                         opj_stepsize_t *ss = NULL;
352
353                                         opj_tcd_band_t *band = &res->bands[bandno];
354
355                                         band->bandno = resno == 0 ? 0 : bandno + 1;
356                                         x0b = (band->bandno == 1) || (band->bandno == 3) ? 1 : 0;
357                                         y0b = (band->bandno == 2) || (band->bandno == 3) ? 1 : 0;
358                                         
359                                         if (band->bandno == 0) {
360                                                 /* band border (global) */
361                                                 band->x0 = int_ceildivpow2(tilec->x0, levelno);
362                                                 band->y0 = int_ceildivpow2(tilec->y0, levelno);
363                                                 band->x1 = int_ceildivpow2(tilec->x1, levelno);
364                                                 band->y1 = int_ceildivpow2(tilec->y1, levelno);
365                                         } else {
366                                                 /* band border (global) */
367                                                 band->x0 = int_ceildivpow2(tilec->x0 - (1 << levelno) * x0b, levelno + 1);
368                                                 band->y0 = int_ceildivpow2(tilec->y0 - (1 << levelno) * y0b, levelno + 1);
369                                                 band->x1 = int_ceildivpow2(tilec->x1 - (1 << levelno) * x0b, levelno + 1);
370                                                 band->y1 = int_ceildivpow2(tilec->y1 - (1 << levelno) * y0b, levelno + 1);
371                                         }
372                                         
373                                         ss = &tccp->stepsizes[resno == 0 ? 0 : 3 * (resno - 1) + bandno + 1];
374                                         gain = tccp->qmfbid == 0 ? dwt_getgain_real(band->bandno) : dwt_getgain(band->bandno);                                  
375                                         numbps = image->comps[compno].prec + gain;
376                                         
377                                         band->stepsize = (float)((1.0 + ss->mant / 2048.0) * pow(2.0, numbps - ss->expn));
378                                         band->numbps = ss->expn + tccp->numgbits - 1;   /* WHY -1 ? */
379                                         
380                                         band->precincts = (opj_tcd_precinct_t *) opj_malloc(3 * res->pw * res->ph * sizeof(opj_tcd_precinct_t));
381                                         
382                                         for (i = 0; i < res->pw * res->ph * 3; i++) {
383                                                 band->precincts[i].imsbtree = NULL;
384                                                 band->precincts[i].incltree = NULL;
385                                                 band->precincts[i].cblks.enc = NULL;
386                                         }
387                                         
388                                         for (precno = 0; precno < res->pw * res->ph; precno++) {
389                                                 int tlcblkxstart, tlcblkystart, brcblkxend, brcblkyend;
390
391                                                 int cbgxstart = tlcbgxstart + (precno % res->pw) * (1 << cbgwidthexpn);
392                                                 int cbgystart = tlcbgystart + (precno / res->pw) * (1 << cbgheightexpn);
393                                                 int cbgxend = cbgxstart + (1 << cbgwidthexpn);
394                                                 int cbgyend = cbgystart + (1 << cbgheightexpn);
395
396                                                 opj_tcd_precinct_t *prc = &band->precincts[precno];
397
398                                                 /* precinct size (global) */
399                                                 prc->x0 = int_max(cbgxstart, band->x0);
400                                                 prc->y0 = int_max(cbgystart, band->y0);
401                                                 prc->x1 = int_min(cbgxend, band->x1);
402                                                 prc->y1 = int_min(cbgyend, band->y1);
403
404                                                 tlcblkxstart = int_floordivpow2(prc->x0, cblkwidthexpn) << cblkwidthexpn;
405                                                 tlcblkystart = int_floordivpow2(prc->y0, cblkheightexpn) << cblkheightexpn;
406                                                 brcblkxend = int_ceildivpow2(prc->x1, cblkwidthexpn) << cblkwidthexpn;
407                                                 brcblkyend = int_ceildivpow2(prc->y1, cblkheightexpn) << cblkheightexpn;
408                                                 prc->cw = (brcblkxend - tlcblkxstart) >> cblkwidthexpn;
409                                                 prc->ch = (brcblkyend - tlcblkystart) >> cblkheightexpn;
410
411                                                 prc->cblks.enc = (opj_tcd_cblk_enc_t*) opj_calloc((prc->cw * prc->ch), sizeof(opj_tcd_cblk_enc_t));
412                                                 prc->incltree = tgt_create(prc->cw, prc->ch);
413                                                 prc->imsbtree = tgt_create(prc->cw, prc->ch);
414                                                 
415                                                 for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
416                                                         int cblkxstart = tlcblkxstart + (cblkno % prc->cw) * (1 << cblkwidthexpn);
417                                                         int cblkystart = tlcblkystart + (cblkno / prc->cw) * (1 << cblkheightexpn);
418                                                         int cblkxend = cblkxstart + (1 << cblkwidthexpn);
419                                                         int cblkyend = cblkystart + (1 << cblkheightexpn);
420                                                         
421                                                         opj_tcd_cblk_enc_t* cblk = &prc->cblks.enc[cblkno];
422
423                                                         /* code-block size (global) */
424                                                         cblk->x0 = int_max(cblkxstart, prc->x0);
425                                                         cblk->y0 = int_max(cblkystart, prc->y0);
426                                                         cblk->x1 = int_min(cblkxend, prc->x1);
427                                                         cblk->y1 = int_min(cblkyend, prc->y1);
428                                                         cblk->data = (unsigned char*) opj_calloc(8192+2, sizeof(unsigned char));
429                                                         /* FIXME: mqc_init_enc and mqc_byteout underrun the buffer if we don't do this. Why? */
430                                                         cblk->data += 2;
431                                                         cblk->layers = (opj_tcd_layer_t*) opj_calloc(100, sizeof(opj_tcd_layer_t));
432                                                         cblk->passes = (opj_tcd_pass_t*) opj_calloc(100, sizeof(opj_tcd_pass_t));
433                                                 }
434                                         }
435                                 }
436                         }
437                 }
438         }
439         
440         /* tcd_dump(stdout, tcd, &tcd->tcd_image); */
441 }
442
443 void tcd_free_encode(opj_tcd_t *tcd) {
444         int tileno, compno, resno, bandno, precno, cblkno;
445
446         for (tileno = 0; tileno < 1; tileno++) {
447                 opj_tcd_tile_t *tile = tcd->tcd_image->tiles;
448
449                 for (compno = 0; compno < tile->numcomps; compno++) {
450                         opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
451
452                         for (resno = 0; resno < tilec->numresolutions; resno++) {
453                                 opj_tcd_resolution_t *res = &tilec->resolutions[resno];
454
455                                 for (bandno = 0; bandno < res->numbands; bandno++) {
456                                         opj_tcd_band_t *band = &res->bands[bandno];
457
458                                         for (precno = 0; precno < res->pw * res->ph; precno++) {
459                                                 opj_tcd_precinct_t *prc = &band->precincts[precno];
460
461                                                 if (prc->incltree != NULL) {
462                                                         tgt_destroy(prc->incltree);
463                                                         prc->incltree = NULL;
464                                                 }
465                                                 if (prc->imsbtree != NULL) {
466                                                         tgt_destroy(prc->imsbtree);     
467                                                         prc->imsbtree = NULL;
468                                                 }
469                                                 for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
470                                                         opj_free(prc->cblks.enc[cblkno].data - 2);
471                                                         opj_free(prc->cblks.enc[cblkno].layers);
472                                                         opj_free(prc->cblks.enc[cblkno].passes);
473                                                 }
474                                                 opj_free(prc->cblks.enc);
475                                         } /* for (precno */
476                                         opj_free(band->precincts);
477                                         band->precincts = NULL;
478                                 } /* for (bandno */
479                         } /* for (resno */
480                         opj_free(tilec->resolutions);
481                         tilec->resolutions = NULL;
482                 } /* for (compno */
483                 opj_free(tile->comps);
484                 tile->comps = NULL;
485         } /* for (tileno */
486         opj_free(tcd->tcd_image->tiles);
487         tcd->tcd_image->tiles = NULL;
488 }
489
490 void tcd_init_encode(opj_tcd_t *tcd, opj_image_t * image, opj_cp_t * cp, int curtileno) {
491         int tileno, compno, resno, bandno, precno, cblkno;
492
493         for (tileno = 0; tileno < 1; tileno++) {
494                 opj_tcp_t *tcp = &cp->tcps[curtileno];
495                 int j;
496                 /* cfr p59 ISO/IEC FDIS15444-1 : 2000 (18 august 2000) */
497                 int p = curtileno % cp->tw;
498                 int q = curtileno / cp->tw;
499
500                 opj_tcd_tile_t *tile = tcd->tcd_image->tiles;
501                 
502                 /* 4 borders of the tile rescale on the image if necessary */
503                 tile->x0 = int_max(cp->tx0 + p * cp->tdx, image->x0);
504                 tile->y0 = int_max(cp->ty0 + q * cp->tdy, image->y0);
505                 tile->x1 = int_min(cp->tx0 + (p + 1) * cp->tdx, image->x1);
506                 tile->y1 = int_min(cp->ty0 + (q + 1) * cp->tdy, image->y1);
507                 
508                 tile->numcomps = image->numcomps;
509                 /* tile->PPT=image->PPT; */
510
511                 /* Modification of the RATE >> */
512                 for (j = 0; j < tcp->numlayers; j++) {
513                         tcp->rates[j] = tcp->rates[j] ? 
514                                 cp->tp_on ? 
515                                         (((float) (tile->numcomps 
516                                         * (tile->x1 - tile->x0) 
517                                         * (tile->y1 - tile->y0)
518                                         * image->comps[0].prec))
519                                         /(tcp->rates[j] * 8 * image->comps[0].dx * image->comps[0].dy)) - (((tcd->cur_totnum_tp - 1) * 14 )/ tcp->numlayers)
520                                         :
521                                 ((float) (tile->numcomps 
522                                         * (tile->x1 - tile->x0) 
523                                         * (tile->y1 - tile->y0) 
524                                         * image->comps[0].prec))/ 
525                                         (tcp->rates[j] * 8 * image->comps[0].dx * image->comps[0].dy)
526                                         : 0;
527
528                         if (tcp->rates[j]) {
529                                 if (j && tcp->rates[j] < tcp->rates[j - 1] + 10) {
530                                         tcp->rates[j] = tcp->rates[j - 1] + 20;
531                                 } else {
532                                         if (!j && tcp->rates[j] < 30)
533                                                 tcp->rates[j] = 30;
534                                 }
535                         }
536                 }
537                 /* << Modification of the RATE */
538
539                 /* tile->comps=(opj_tcd_tilecomp_t*)opj_realloc(tile->comps,image->numcomps*sizeof(opj_tcd_tilecomp_t)); */
540                 for (compno = 0; compno < tile->numcomps; compno++) {
541                         opj_tccp_t *tccp = &tcp->tccps[compno];
542                         
543                         opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
544
545                         /* border of each tile component (global) */
546                         tilec->x0 = int_ceildiv(tile->x0, image->comps[compno].dx);
547                         tilec->y0 = int_ceildiv(tile->y0, image->comps[compno].dy);
548                         tilec->x1 = int_ceildiv(tile->x1, image->comps[compno].dx);
549                         tilec->y1 = int_ceildiv(tile->y1, image->comps[compno].dy);
550                         
551                         tilec->data = (int *) opj_aligned_malloc((tilec->x1 - tilec->x0) * (tilec->y1 - tilec->y0) * sizeof(int));
552                         tilec->numresolutions = tccp->numresolutions;
553                         /* tilec->resolutions=(opj_tcd_resolution_t*)opj_realloc(tilec->resolutions,tilec->numresolutions*sizeof(opj_tcd_resolution_t)); */
554                         for (resno = 0; resno < tilec->numresolutions; resno++) {
555                                 int pdx, pdy;
556
557                                 int levelno = tilec->numresolutions - 1 - resno;
558                                 int tlprcxstart, tlprcystart, brprcxend, brprcyend;
559                                 int tlcbgxstart, tlcbgystart, brcbgxend, brcbgyend;
560                                 int cbgwidthexpn, cbgheightexpn;
561                                 int cblkwidthexpn, cblkheightexpn;
562                                 
563                                 opj_tcd_resolution_t *res = &tilec->resolutions[resno];
564
565                                 /* border for each resolution level (global) */
566                                 res->x0 = int_ceildivpow2(tilec->x0, levelno);
567                                 res->y0 = int_ceildivpow2(tilec->y0, levelno);
568                                 res->x1 = int_ceildivpow2(tilec->x1, levelno);
569                                 res->y1 = int_ceildivpow2(tilec->y1, levelno);  
570                                 res->numbands = resno == 0 ? 1 : 3;
571
572                                 /* p. 35, table A-23, ISO/IEC FDIS154444-1 : 2000 (18 august 2000) */
573                                 if (tccp->csty & J2K_CCP_CSTY_PRT) {
574                                         pdx = tccp->prcw[resno];
575                                         pdy = tccp->prch[resno];
576                                 } else {
577                                         pdx = 15;
578                                         pdy = 15;
579                                 }
580                                 /* p. 64, B.6, ISO/IEC FDIS15444-1 : 2000 (18 august 2000)  */
581                                 tlprcxstart = int_floordivpow2(res->x0, pdx) << pdx;
582                                 tlprcystart = int_floordivpow2(res->y0, pdy) << pdy;
583                                 brprcxend = int_ceildivpow2(res->x1, pdx) << pdx;
584                                 brprcyend = int_ceildivpow2(res->y1, pdy) << pdy;
585                                 
586                                 res->pw = (brprcxend - tlprcxstart) >> pdx;
587                                 res->ph = (brprcyend - tlprcystart) >> pdy;
588                                 
589                                 if (resno == 0) {
590                                         tlcbgxstart = tlprcxstart;
591                                         tlcbgystart = tlprcystart;
592                                         brcbgxend = brprcxend;
593                                         brcbgyend = brprcyend;
594                                         cbgwidthexpn = pdx;
595                                         cbgheightexpn = pdy;
596                                 } else {
597                                         tlcbgxstart = int_ceildivpow2(tlprcxstart, 1);
598                                         tlcbgystart = int_ceildivpow2(tlprcystart, 1);
599                                         brcbgxend = int_ceildivpow2(brprcxend, 1);
600                                         brcbgyend = int_ceildivpow2(brprcyend, 1);
601                                         cbgwidthexpn = pdx - 1;
602                                         cbgheightexpn = pdy - 1;
603                                 }
604                                 
605                                 cblkwidthexpn = int_min(tccp->cblkw, cbgwidthexpn);
606                                 cblkheightexpn = int_min(tccp->cblkh, cbgheightexpn);
607                                 
608                                 for (bandno = 0; bandno < res->numbands; bandno++) {
609                                         int x0b, y0b;
610                                         int gain, numbps;
611                                         opj_stepsize_t *ss = NULL;
612
613                                         opj_tcd_band_t *band = &res->bands[bandno];
614
615                                         band->bandno = resno == 0 ? 0 : bandno + 1;
616                                         x0b = (band->bandno == 1) || (band->bandno == 3) ? 1 : 0;
617                                         y0b = (band->bandno == 2) || (band->bandno == 3) ? 1 : 0;
618                                         
619                                         if (band->bandno == 0) {
620                                                 /* band border */
621                                                 band->x0 = int_ceildivpow2(tilec->x0, levelno);
622                                                 band->y0 = int_ceildivpow2(tilec->y0, levelno);
623                                                 band->x1 = int_ceildivpow2(tilec->x1, levelno);
624                                                 band->y1 = int_ceildivpow2(tilec->y1, levelno);
625                                         } else {
626                                                 band->x0 = int_ceildivpow2(tilec->x0 - (1 << levelno) * x0b, levelno + 1);
627                                                 band->y0 = int_ceildivpow2(tilec->y0 - (1 << levelno) * y0b, levelno + 1);
628                                                 band->x1 = int_ceildivpow2(tilec->x1 - (1 << levelno) * x0b, levelno + 1);
629                                                 band->y1 = int_ceildivpow2(tilec->y1 - (1 << levelno) * y0b, levelno + 1);
630                                         }
631                                         
632                                         ss = &tccp->stepsizes[resno == 0 ? 0 : 3 * (resno - 1) + bandno + 1];
633                                         gain = tccp->qmfbid == 0 ? dwt_getgain_real(band->bandno) : dwt_getgain(band->bandno);
634                                         numbps = image->comps[compno].prec + gain;
635                                         band->stepsize = (float)((1.0 + ss->mant / 2048.0) * pow(2.0, numbps - ss->expn));
636                                         band->numbps = ss->expn + tccp->numgbits - 1;   /* WHY -1 ? */
637                                         
638                                         for (precno = 0; precno < res->pw * res->ph; precno++) {
639                                                 int tlcblkxstart, tlcblkystart, brcblkxend, brcblkyend;
640
641                                                 int cbgxstart = tlcbgxstart + (precno % res->pw) * (1 << cbgwidthexpn);
642                                                 int cbgystart = tlcbgystart + (precno / res->pw) * (1 << cbgheightexpn);
643                                                 int cbgxend = cbgxstart + (1 << cbgwidthexpn);
644                                                 int cbgyend = cbgystart + (1 << cbgheightexpn);
645                                                 
646                                                 opj_tcd_precinct_t *prc = &band->precincts[precno];
647
648                                                 /* precinct size (global) */
649                                                 prc->x0 = int_max(cbgxstart, band->x0);
650                                                 prc->y0 = int_max(cbgystart, band->y0);
651                                                 prc->x1 = int_min(cbgxend, band->x1);
652                                                 prc->y1 = int_min(cbgyend, band->y1);
653
654                                                 tlcblkxstart = int_floordivpow2(prc->x0, cblkwidthexpn) << cblkwidthexpn;
655                                                 tlcblkystart = int_floordivpow2(prc->y0, cblkheightexpn) << cblkheightexpn;
656                                                 brcblkxend = int_ceildivpow2(prc->x1, cblkwidthexpn) << cblkwidthexpn;
657                                                 brcblkyend = int_ceildivpow2(prc->y1, cblkheightexpn) << cblkheightexpn;
658                                                 prc->cw = (brcblkxend - tlcblkxstart) >> cblkwidthexpn;
659                                                 prc->ch = (brcblkyend - tlcblkystart) >> cblkheightexpn;
660
661                                                 opj_free(prc->cblks.enc);
662                                                 prc->cblks.enc = (opj_tcd_cblk_enc_t*) opj_calloc(prc->cw * prc->ch, sizeof(opj_tcd_cblk_enc_t));
663
664                                                 if (prc->incltree != NULL) {
665                                                         tgt_destroy(prc->incltree);
666                                                 }
667                                                 if (prc->imsbtree != NULL) {
668                                                         tgt_destroy(prc->imsbtree);
669                                                 }
670                                                 
671                                                 prc->incltree = tgt_create(prc->cw, prc->ch);
672                                                 prc->imsbtree = tgt_create(prc->cw, prc->ch);
673
674                                                 for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
675                                                         int cblkxstart = tlcblkxstart + (cblkno % prc->cw) * (1 << cblkwidthexpn);
676                                                         int cblkystart = tlcblkystart + (cblkno / prc->cw) * (1 << cblkheightexpn);
677                                                         int cblkxend = cblkxstart + (1 << cblkwidthexpn);
678                                                         int cblkyend = cblkystart + (1 << cblkheightexpn);
679
680                                                         opj_tcd_cblk_enc_t* cblk = &prc->cblks.enc[cblkno];
681
682                                                         /* code-block size (global) */
683                                                         cblk->x0 = int_max(cblkxstart, prc->x0);
684                                                         cblk->y0 = int_max(cblkystart, prc->y0);
685                                                         cblk->x1 = int_min(cblkxend, prc->x1);
686                                                         cblk->y1 = int_min(cblkyend, prc->y1);
687                                                         cblk->data = (unsigned char*) opj_calloc(8192+2, sizeof(unsigned char));
688                                                         /* FIXME: mqc_init_enc and mqc_byteout underrun the buffer if we don't do this. Why? */
689                                                         cblk->data += 2;
690                                                         cblk->layers = (opj_tcd_layer_t*) opj_calloc(100, sizeof(opj_tcd_layer_t));
691                                                         cblk->passes = (opj_tcd_pass_t*) opj_calloc(100, sizeof(opj_tcd_pass_t));
692                                                 }
693                                         } /* precno */
694                                 } /* bandno */
695                         } /* resno */
696                 } /* compno */
697         } /* tileno */
698
699         /* tcd_dump(stdout, tcd, &tcd->tcd_image); */
700 }
701
702 void tcd_malloc_decode(opj_tcd_t *tcd, opj_image_t * image, opj_cp_t * cp) {
703         int i, j, tileno, p, q;
704         unsigned int x0 = 0, y0 = 0, x1 = 0, y1 = 0, w, h;
705
706         tcd->image = image;
707         tcd->tcd_image->tw = cp->tw;
708         tcd->tcd_image->th = cp->th;
709         tcd->tcd_image->tiles = (opj_tcd_tile_t *) opj_malloc(cp->tw * cp->th * sizeof(opj_tcd_tile_t));
710
711         /* 
712         Allocate place to store the decoded data = final image
713         Place limited by the tile really present in the codestream 
714         */
715
716         for (j = 0; j < cp->tileno_size; j++) {
717                 opj_tcd_tile_t *tile;
718                 
719                 tileno = cp->tileno[j];         
720                 tile = &(tcd->tcd_image->tiles[cp->tileno[tileno]]);            
721                 tile->numcomps = image->numcomps;
722                 tile->comps = (opj_tcd_tilecomp_t*) opj_calloc(image->numcomps, sizeof(opj_tcd_tilecomp_t));
723         }
724
725         for (i = 0; i < image->numcomps; i++) {
726                 for (j = 0; j < cp->tileno_size; j++) {
727                         opj_tcd_tile_t *tile;
728                         opj_tcd_tilecomp_t *tilec;
729                         
730                         /* cfr p59 ISO/IEC FDIS15444-1 : 2000 (18 august 2000) */
731                         
732                         tileno = cp->tileno[j];
733                         
734                         tile = &(tcd->tcd_image->tiles[cp->tileno[tileno]]);
735                         tilec = &tile->comps[i];
736                         
737                         p = tileno % cp->tw;    /* si numerotation matricielle .. */
738                         q = tileno / cp->tw;    /* .. coordonnees de la tile (q,p) q pour ligne et p pour colonne */
739                         
740                         /* 4 borders of the tile rescale on the image if necessary */
741                         tile->x0 = int_max(cp->tx0 + p * cp->tdx, image->x0);
742                         tile->y0 = int_max(cp->ty0 + q * cp->tdy, image->y0);
743                         tile->x1 = int_min(cp->tx0 + (p + 1) * cp->tdx, image->x1);
744                         tile->y1 = int_min(cp->ty0 + (q + 1) * cp->tdy, image->y1);
745
746                         tilec->x0 = int_ceildiv(tile->x0, image->comps[i].dx);
747                         tilec->y0 = int_ceildiv(tile->y0, image->comps[i].dy);
748                         tilec->x1 = int_ceildiv(tile->x1, image->comps[i].dx);
749                         tilec->y1 = int_ceildiv(tile->y1, image->comps[i].dy);
750
751                         x0 = j == 0 ? tilec->x0 : int_min(x0, (unsigned int) tilec->x0);
752                         y0 = j == 0 ? tilec->y0 : int_min(y0,   (unsigned int) tilec->y0);
753                         x1 = j == 0 ? tilec->x1 : int_max(x1,   (unsigned int) tilec->x1);
754                         y1 = j == 0 ? tilec->y1 : int_max(y1,   (unsigned int) tilec->y1);
755                 }
756
757                 w = int_ceildivpow2(x1 - x0, image->comps[i].factor);
758                 h = int_ceildivpow2(y1 - y0, image->comps[i].factor);
759
760                 image->comps[i].w = w;
761                 image->comps[i].h = h;
762                 image->comps[i].x0 = x0;
763                 image->comps[i].y0 = y0;
764         }
765 }
766
767 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) {
768         int compno, resno, bandno, precno, cblkno;
769         opj_tcp_t *tcp;
770         opj_tcd_tile_t *tile;
771
772         OPJ_ARG_NOT_USED(cstr_info);
773
774         tcd->cp = cp;
775         
776         tcp = &(cp->tcps[cp->tileno[tileno]]);
777         tile = &(tcd->tcd_image->tiles[cp->tileno[tileno]]);
778         
779         tileno = cp->tileno[tileno];
780         
781         for (compno = 0; compno < tile->numcomps; compno++) {
782                 opj_tccp_t *tccp = &tcp->tccps[compno];
783                 opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
784                 
785                 /* border of each tile component (global) */
786                 tilec->x0 = int_ceildiv(tile->x0, image->comps[compno].dx);
787                 tilec->y0 = int_ceildiv(tile->y0, image->comps[compno].dy);
788                 tilec->x1 = int_ceildiv(tile->x1, image->comps[compno].dx);
789                 tilec->y1 = int_ceildiv(tile->y1, image->comps[compno].dy);
790
791                 tilec->numresolutions = tccp->numresolutions;
792                 tilec->resolutions = (opj_tcd_resolution_t *) opj_malloc(tilec->numresolutions * sizeof(opj_tcd_resolution_t));
793                 
794                 for (resno = 0; resno < tilec->numresolutions; resno++) {
795                         int pdx, pdy;
796                         int levelno = tilec->numresolutions - 1 - resno;
797                         int tlprcxstart, tlprcystart, brprcxend, brprcyend;
798                         int tlcbgxstart, tlcbgystart, brcbgxend, brcbgyend;
799                         int cbgwidthexpn, cbgheightexpn;
800                         int cblkwidthexpn, cblkheightexpn;
801                         
802                         opj_tcd_resolution_t *res = &tilec->resolutions[resno];
803                         
804                         /* border for each resolution level (global) */
805                         res->x0 = int_ceildivpow2(tilec->x0, levelno);
806                         res->y0 = int_ceildivpow2(tilec->y0, levelno);
807                         res->x1 = int_ceildivpow2(tilec->x1, levelno);
808                         res->y1 = int_ceildivpow2(tilec->y1, levelno);
809                         res->numbands = resno == 0 ? 1 : 3;
810                         
811                         /* p. 35, table A-23, ISO/IEC FDIS154444-1 : 2000 (18 august 2000) */
812                         if (tccp->csty & J2K_CCP_CSTY_PRT) {
813                                 pdx = tccp->prcw[resno];
814                                 pdy = tccp->prch[resno];
815                         } else {
816                                 pdx = 15;
817                                 pdy = 15;
818                         }                       
819                         
820                         /* p. 64, B.6, ISO/IEC FDIS15444-1 : 2000 (18 august 2000)  */
821                         tlprcxstart = int_floordivpow2(res->x0, pdx) << pdx;
822                         tlprcystart = int_floordivpow2(res->y0, pdy) << pdy;
823                         brprcxend = int_ceildivpow2(res->x1, pdx) << pdx;
824                         brprcyend = int_ceildivpow2(res->y1, pdy) << pdy;
825                         
826                         res->pw = (res->x0 == res->x1) ? 0 : ((brprcxend - tlprcxstart) >> pdx);
827                         res->ph = (res->y0 == res->y1) ? 0 : ((brprcyend - tlprcystart) >> pdy);
828                         
829                         if (resno == 0) {
830                                 tlcbgxstart = tlprcxstart;
831                                 tlcbgystart = tlprcystart;
832                                 brcbgxend = brprcxend;
833                                 brcbgyend = brprcyend;
834                                 cbgwidthexpn = pdx;
835                                 cbgheightexpn = pdy;
836                         } else {
837                                 tlcbgxstart = int_ceildivpow2(tlprcxstart, 1);
838                                 tlcbgystart = int_ceildivpow2(tlprcystart, 1);
839                                 brcbgxend = int_ceildivpow2(brprcxend, 1);
840                                 brcbgyend = int_ceildivpow2(brprcyend, 1);
841                                 cbgwidthexpn = pdx - 1;
842                                 cbgheightexpn = pdy - 1;
843                         }
844                         
845                         cblkwidthexpn = int_min(tccp->cblkw, cbgwidthexpn);
846                         cblkheightexpn = int_min(tccp->cblkh, cbgheightexpn);
847                         
848                         for (bandno = 0; bandno < res->numbands; bandno++) {
849                                 int x0b, y0b;
850                                 int gain, numbps;
851                                 opj_stepsize_t *ss = NULL;
852                                 
853                                 opj_tcd_band_t *band = &res->bands[bandno];
854                                 band->bandno = resno == 0 ? 0 : bandno + 1;
855                                 x0b = (band->bandno == 1) || (band->bandno == 3) ? 1 : 0;
856                                 y0b = (band->bandno == 2) || (band->bandno == 3) ? 1 : 0;
857                                 
858                                 if (band->bandno == 0) {
859                                         /* band border (global) */
860                                         band->x0 = int_ceildivpow2(tilec->x0, levelno);
861                                         band->y0 = int_ceildivpow2(tilec->y0, levelno);
862                                         band->x1 = int_ceildivpow2(tilec->x1, levelno);
863                                         band->y1 = int_ceildivpow2(tilec->y1, levelno);
864                                 } else {
865                                         /* band border (global) */
866                                         band->x0 = int_ceildivpow2(tilec->x0 - (1 << levelno) * x0b, levelno + 1);
867                                         band->y0 = int_ceildivpow2(tilec->y0 - (1 << levelno) * y0b, levelno + 1);
868                                         band->x1 = int_ceildivpow2(tilec->x1 - (1 << levelno) * x0b, levelno + 1);
869                                         band->y1 = int_ceildivpow2(tilec->y1 - (1 << levelno) * y0b, levelno + 1);
870                                 }
871                                 
872                                 ss = &tccp->stepsizes[resno == 0 ? 0 : 3 * (resno - 1) + bandno + 1];
873                                 gain = tccp->qmfbid == 0 ? dwt_getgain_real(band->bandno) : dwt_getgain(band->bandno);
874                                 numbps = image->comps[compno].prec + gain;
875                                 band->stepsize = (float)(((1.0 + ss->mant / 2048.0) * pow(2.0, numbps - ss->expn)) * 0.5);
876                                 band->numbps = ss->expn + tccp->numgbits - 1;   /* WHY -1 ? */
877                                 
878                                 band->precincts = (opj_tcd_precinct_t *) opj_malloc(res->pw * res->ph * sizeof(opj_tcd_precinct_t));
879                                 
880                                 for (precno = 0; precno < res->pw * res->ph; precno++) {
881                                         int tlcblkxstart, tlcblkystart, brcblkxend, brcblkyend;
882                                         int cbgxstart = tlcbgxstart + (precno % res->pw) * (1 << cbgwidthexpn);
883                                         int cbgystart = tlcbgystart + (precno / res->pw) * (1 << cbgheightexpn);
884                                         int cbgxend = cbgxstart + (1 << cbgwidthexpn);
885                                         int cbgyend = cbgystart + (1 << cbgheightexpn);
886                                         
887                                         opj_tcd_precinct_t *prc = &band->precincts[precno];
888                                         /* precinct size (global) */
889                                         prc->x0 = int_max(cbgxstart, band->x0);
890                                         prc->y0 = int_max(cbgystart, band->y0);
891                                         prc->x1 = int_min(cbgxend, band->x1);
892                                         prc->y1 = int_min(cbgyend, band->y1);
893                                         
894                                         tlcblkxstart = int_floordivpow2(prc->x0, cblkwidthexpn) << cblkwidthexpn;
895                                         tlcblkystart = int_floordivpow2(prc->y0, cblkheightexpn) << cblkheightexpn;
896                                         brcblkxend = int_ceildivpow2(prc->x1, cblkwidthexpn) << cblkwidthexpn;
897                                         brcblkyend = int_ceildivpow2(prc->y1, cblkheightexpn) << cblkheightexpn;
898                                         prc->cw = (brcblkxend - tlcblkxstart) >> cblkwidthexpn;
899                                         prc->ch = (brcblkyend - tlcblkystart) >> cblkheightexpn;
900
901                                         prc->cblks.dec = (opj_tcd_cblk_dec_t*) opj_malloc(prc->cw * prc->ch * sizeof(opj_tcd_cblk_dec_t));
902
903                                         prc->incltree = tgt_create(prc->cw, prc->ch);
904                                         prc->imsbtree = tgt_create(prc->cw, prc->ch);
905                                         
906                                         for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
907                                                 int cblkxstart = tlcblkxstart + (cblkno % prc->cw) * (1 << cblkwidthexpn);
908                                                 int cblkystart = tlcblkystart + (cblkno / prc->cw) * (1 << cblkheightexpn);
909                                                 int cblkxend = cblkxstart + (1 << cblkwidthexpn);
910                                                 int cblkyend = cblkystart + (1 << cblkheightexpn);                                      
911
912                                                 opj_tcd_cblk_dec_t* cblk = &prc->cblks.dec[cblkno];
913                                                 cblk->data = NULL;
914                                                 cblk->segs = NULL;
915                                                 /* code-block size (global) */
916                                                 cblk->x0 = int_max(cblkxstart, prc->x0);
917                                                 cblk->y0 = int_max(cblkystart, prc->y0);
918                                                 cblk->x1 = int_min(cblkxend, prc->x1);
919                                                 cblk->y1 = int_min(cblkyend, prc->y1);
920                                                 cblk->numsegs = 0;
921                                         }
922                                 } /* precno */
923                         } /* bandno */
924                 } /* resno */
925         } /* compno */
926         /* tcd_dump(stdout, tcd, &tcd->tcd_image); */
927 }
928
929 void tcd_makelayer_fixed(opj_tcd_t *tcd, int layno, int final) {
930         int compno, resno, bandno, precno, cblkno;
931         int value;                      /*, matrice[tcd_tcp->numlayers][tcd_tile->comps[0].numresolutions][3]; */
932         int matrice[10][10][3];
933         int i, j, k;
934
935         opj_cp_t *cp = tcd->cp;
936         opj_tcd_tile_t *tcd_tile = tcd->tcd_tile;
937         opj_tcp_t *tcd_tcp = tcd->tcp;
938
939         /*matrice=(int*)opj_malloc(tcd_tcp->numlayers*tcd_tile->comps[0].numresolutions*3*sizeof(int)); */
940         
941         for (compno = 0; compno < tcd_tile->numcomps; compno++) {
942                 opj_tcd_tilecomp_t *tilec = &tcd_tile->comps[compno];
943                 for (i = 0; i < tcd_tcp->numlayers; i++) {
944                         for (j = 0; j < tilec->numresolutions; j++) {
945                                 for (k = 0; k < 3; k++) {
946                                         matrice[i][j][k] =
947                                                 (int) (cp->matrice[i * tilec->numresolutions * 3 + j * 3 + k] 
948                                                 * (float) (tcd->image->comps[compno].prec / 16.0));
949                                 }
950                         }
951                 }
952         
953                 for (resno = 0; resno < tilec->numresolutions; resno++) {
954                         opj_tcd_resolution_t *res = &tilec->resolutions[resno];
955                         for (bandno = 0; bandno < res->numbands; bandno++) {
956                                 opj_tcd_band_t *band = &res->bands[bandno];
957                                 for (precno = 0; precno < res->pw * res->ph; precno++) {
958                                         opj_tcd_precinct_t *prc = &band->precincts[precno];
959                                         for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
960                                                 opj_tcd_cblk_enc_t *cblk = &prc->cblks.enc[cblkno];
961                                                 opj_tcd_layer_t *layer = &cblk->layers[layno];
962                                                 int n;
963                                                 int imsb = tcd->image->comps[compno].prec - cblk->numbps;       /* number of bit-plan equal to zero */
964                                                 /* Correction of the matrix of coefficient to include the IMSB information */
965                                                 if (layno == 0) {
966                                                         value = matrice[layno][resno][bandno];
967                                                         if (imsb >= value) {
968                                                                 value = 0;
969                                                         } else {
970                                                                 value -= imsb;
971                                                         }
972                                                 } else {
973                                                         value = matrice[layno][resno][bandno] - matrice[layno - 1][resno][bandno];
974                                                         if (imsb >= matrice[layno - 1][resno][bandno]) {
975                                                                 value -= (imsb - matrice[layno - 1][resno][bandno]);
976                                                                 if (value < 0) {
977                                                                         value = 0;
978                                                                 }
979                                                         }
980                                                 }
981                                                 
982                                                 if (layno == 0) {
983                                                         cblk->numpassesinlayers = 0;
984                                                 }
985                                                 
986                                                 n = cblk->numpassesinlayers;
987                                                 if (cblk->numpassesinlayers == 0) {
988                                                         if (value != 0) {
989                                                                 n = 3 * value - 2 + cblk->numpassesinlayers;
990                                                         } else {
991                                                                 n = cblk->numpassesinlayers;
992                                                         }
993                                                 } else {
994                                                         n = 3 * value + cblk->numpassesinlayers;
995                                                 }
996                                                 
997                                                 layer->numpasses = n - cblk->numpassesinlayers;
998                                                 
999                                                 if (!layer->numpasses)
1000                                                         continue;
1001                                                 
1002                                                 if (cblk->numpassesinlayers == 0) {
1003                                                         layer->len = cblk->passes[n - 1].rate;
1004                                                         layer->data = cblk->data;
1005                                                 } else {
1006                                                         layer->len = cblk->passes[n - 1].rate - cblk->passes[cblk->numpassesinlayers - 1].rate;
1007                                                         layer->data = cblk->data + cblk->passes[cblk->numpassesinlayers - 1].rate;
1008                                                 }
1009                                                 if (final)
1010                                                         cblk->numpassesinlayers = n;
1011                                         }
1012                                 }
1013                         }
1014                 }
1015         }
1016 }
1017
1018 void tcd_rateallocate_fixed(opj_tcd_t *tcd) {
1019         int layno;
1020         for (layno = 0; layno < tcd->tcp->numlayers; layno++) {
1021                 tcd_makelayer_fixed(tcd, layno, 1);
1022         }
1023 }
1024
1025 void tcd_rateallocate_fixed_v2(opj_tcd_v2_t *tcd) {
1026         OPJ_UINT32 layno;
1027
1028         for (layno = 0; layno < tcd->tcp->numlayers; layno++) {
1029                 tcd_makelayer_fixed_v2(tcd, layno, 1);
1030         }
1031 }
1032
1033 void tcd_makelayer(opj_tcd_t *tcd, int layno, double thresh, int final) {
1034         int compno, resno, bandno, precno, cblkno, passno;
1035         
1036         opj_tcd_tile_t *tcd_tile = tcd->tcd_tile;
1037
1038         tcd_tile->distolayer[layno] = 0;        /* fixed_quality */
1039         
1040         for (compno = 0; compno < tcd_tile->numcomps; compno++) {
1041                 opj_tcd_tilecomp_t *tilec = &tcd_tile->comps[compno];
1042                 for (resno = 0; resno < tilec->numresolutions; resno++) {
1043                         opj_tcd_resolution_t *res = &tilec->resolutions[resno];
1044                         for (bandno = 0; bandno < res->numbands; bandno++) {
1045                                 opj_tcd_band_t *band = &res->bands[bandno];
1046                                 for (precno = 0; precno < res->pw * res->ph; precno++) {
1047                                         opj_tcd_precinct_t *prc = &band->precincts[precno];
1048                                         for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
1049                                                 opj_tcd_cblk_enc_t *cblk = &prc->cblks.enc[cblkno];
1050                                                 opj_tcd_layer_t *layer = &cblk->layers[layno];
1051                                                 
1052                                                 int n;
1053                                                 if (layno == 0) {
1054                                                         cblk->numpassesinlayers = 0;
1055                                                 }
1056                                                 n = cblk->numpassesinlayers;
1057                                                 for (passno = cblk->numpassesinlayers; passno < cblk->totalpasses; passno++) {
1058                                                         int dr;
1059                                                         double dd;
1060                                                         opj_tcd_pass_t *pass = &cblk->passes[passno];
1061                                                         if (n == 0) {
1062                                                                 dr = pass->rate;
1063                                                                 dd = pass->distortiondec;
1064                                                         } else {
1065                                                                 dr = pass->rate - cblk->passes[n - 1].rate;
1066                                                                 dd = pass->distortiondec - cblk->passes[n - 1].distortiondec;
1067                                                         }
1068                                                         if (!dr) {
1069                                                                 if (dd != 0)
1070                                                                         n = passno + 1;
1071                                                                 continue;
1072                                                         }
1073                                                         if (dd / dr >= thresh)
1074                                                                 n = passno + 1;
1075                                                 }
1076                                                 layer->numpasses = n - cblk->numpassesinlayers;
1077                                                 
1078                                                 if (!layer->numpasses) {
1079                                                         layer->disto = 0;
1080                                                         continue;
1081                                                 }
1082                                                 if (cblk->numpassesinlayers == 0) {
1083                                                         layer->len = cblk->passes[n - 1].rate;
1084                                                         layer->data = cblk->data;
1085                                                         layer->disto = cblk->passes[n - 1].distortiondec;
1086                                                 } else {
1087                                                         layer->len = cblk->passes[n - 1].rate - cblk->passes[cblk->numpassesinlayers - 1].rate;
1088                                                         layer->data = cblk->data + cblk->passes[cblk->numpassesinlayers - 1].rate;
1089                                                         layer->disto = cblk->passes[n - 1].distortiondec - cblk->passes[cblk->numpassesinlayers - 1].distortiondec;
1090                                                 }
1091                                                 
1092                                                 tcd_tile->distolayer[layno] += layer->disto;    /* fixed_quality */
1093                                                 
1094                                                 if (final)
1095                                                         cblk->numpassesinlayers = n;
1096                                         }
1097                                 }
1098                         }
1099                 }
1100         }
1101 }
1102
1103 void tcd_makelayer_v2(  opj_tcd_v2_t *tcd,
1104                                                 OPJ_UINT32 layno,
1105                                                 OPJ_FLOAT64 thresh,
1106                                                 OPJ_UINT32 final)
1107 {
1108         OPJ_UINT32 compno, resno, bandno, precno, cblkno;
1109         OPJ_UINT32 passno;
1110
1111         opj_tcd_tile_v2_t *tcd_tile = tcd->tcd_image->tiles;
1112
1113         tcd_tile->distolayer[layno] = 0;        /* fixed_quality */
1114
1115         for (compno = 0; compno < tcd_tile->numcomps; compno++) {
1116                 opj_tcd_tilecomp_v2_t *tilec = &tcd_tile->comps[compno];
1117
1118                 for (resno = 0; resno < tilec->numresolutions; resno++) {
1119                         opj_tcd_resolution_v2_t *res = &tilec->resolutions[resno];
1120
1121                         for (bandno = 0; bandno < res->numbands; bandno++) {
1122                                 opj_tcd_band_v2_t *band = &res->bands[bandno];
1123
1124                                 for (precno = 0; precno < res->pw * res->ph; precno++) {
1125                                         opj_tcd_precinct_v2_t *prc = &band->precincts[precno];
1126
1127                                         for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
1128                                                 opj_tcd_cblk_enc_v2_t *cblk = &prc->cblks.enc[cblkno];
1129                                                 opj_tcd_layer_t *layer = &cblk->layers[layno];
1130                                                 OPJ_UINT32 n;
1131
1132                                                 if (layno == 0) {
1133                                                         cblk->numpassesinlayers = 0;
1134                                                 }
1135
1136                                                 n = cblk->numpassesinlayers;
1137
1138                                                 for (passno = cblk->numpassesinlayers; passno < cblk->totalpasses; passno++) {
1139                                                         OPJ_INT32 dr;
1140                                                         OPJ_FLOAT64 dd;
1141                                                         opj_tcd_pass_v2_t *pass = &cblk->passes[passno];
1142
1143                                                         if (n == 0) {
1144                                                                 dr = pass->rate;
1145                                                                 dd = pass->distortiondec;
1146                                                         } else {
1147                                                                 dr = pass->rate - cblk->passes[n - 1].rate;
1148                                                                 dd = pass->distortiondec - cblk->passes[n - 1].distortiondec;
1149                                                         }
1150
1151                                                         if (!dr) {
1152                                                                 if (dd != 0)
1153                                                                         n = passno + 1;
1154                                                                 continue;
1155                                                         }
1156                                                         if (dd / dr >= thresh)
1157                                                                 n = passno + 1;
1158                                                 }
1159
1160                                                 layer->numpasses = n - cblk->numpassesinlayers;
1161
1162                                                 if (!layer->numpasses) {
1163                                                         layer->disto = 0;
1164                                                         continue;
1165                                                 }
1166
1167                                                 if (cblk->numpassesinlayers == 0) {
1168                                                         layer->len = cblk->passes[n - 1].rate;
1169                                                         layer->data = cblk->data;
1170                                                         layer->disto = cblk->passes[n - 1].distortiondec;
1171                                                 } else {
1172                                                         layer->len = cblk->passes[n - 1].rate - cblk->passes[cblk->numpassesinlayers - 1].rate;
1173                                                         layer->data = cblk->data + cblk->passes[cblk->numpassesinlayers - 1].rate;
1174                                                         layer->disto = cblk->passes[n - 1].distortiondec - cblk->passes[cblk->numpassesinlayers - 1].distortiondec;
1175                                                 }
1176
1177                                                 tcd_tile->distolayer[layno] += layer->disto;    /* fixed_quality */
1178
1179                                                 if (final)
1180                                                         cblk->numpassesinlayers = n;
1181                                         }
1182                                 }
1183                         }
1184                 }
1185         }
1186 }
1187
1188 void tcd_makelayer_fixed_v2(opj_tcd_v2_t *tcd, OPJ_UINT32 layno, OPJ_UINT32 final) {
1189         OPJ_UINT32 compno, resno, bandno, precno, cblkno;
1190         OPJ_INT32 value;                        /*, matrice[tcd_tcp->numlayers][tcd_tile->comps[0].numresolutions][3]; */
1191         OPJ_INT32 matrice[10][10][3];
1192         OPJ_UINT32 i, j, k;
1193
1194         opj_cp_v2_t *cp = tcd->cp;
1195         opj_tcd_tile_v2_t *tcd_tile = tcd->tcd_image->tiles;
1196         opj_tcp_v2_t *tcd_tcp = tcd->tcp;
1197
1198         for (compno = 0; compno < tcd_tile->numcomps; compno++) {
1199                 opj_tcd_tilecomp_v2_t *tilec = &tcd_tile->comps[compno];
1200
1201                 for (i = 0; i < tcd_tcp->numlayers; i++) {
1202                         for (j = 0; j < tilec->numresolutions; j++) {
1203                                 for (k = 0; k < 3; k++) {
1204                                         matrice[i][j][k] =
1205                                                 (OPJ_INT32) (cp->m_specific_param.m_enc.m_matrice[i * tilec->numresolutions * 3 + j * 3 + k]
1206                                                 * (OPJ_FLOAT32) (tcd->image->comps[compno].prec / 16.0));
1207                                 }
1208                         }
1209                 }
1210
1211                 for (resno = 0; resno < tilec->numresolutions; resno++) {
1212                         opj_tcd_resolution_v2_t *res = &tilec->resolutions[resno];
1213
1214                         for (bandno = 0; bandno < res->numbands; bandno++) {
1215                                 opj_tcd_band_v2_t *band = &res->bands[bandno];
1216
1217                                 for (precno = 0; precno < res->pw * res->ph; precno++) {
1218                                         opj_tcd_precinct_v2_t *prc = &band->precincts[precno];
1219
1220                                         for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
1221                                                 opj_tcd_cblk_enc_v2_t *cblk = &prc->cblks.enc[cblkno];
1222                                                 opj_tcd_layer_t *layer = &cblk->layers[layno];
1223                                                 OPJ_UINT32 n;
1224                                                 OPJ_INT32 imsb = tcd->image->comps[compno].prec - cblk->numbps; /* number of bit-plan equal to zero */
1225
1226                                                 /* Correction of the matrix of coefficient to include the IMSB information */
1227                                                 if (layno == 0) {
1228                                                         value = matrice[layno][resno][bandno];
1229                                                         if (imsb >= value) {
1230                                                                 value = 0;
1231                                                         } else {
1232                                                                 value -= imsb;
1233                                                         }
1234                                                 } else {
1235                                                         value = matrice[layno][resno][bandno] - matrice[layno - 1][resno][bandno];
1236                                                         if (imsb >= matrice[layno - 1][resno][bandno]) {
1237                                                                 value -= (imsb - matrice[layno - 1][resno][bandno]);
1238                                                                 if (value < 0) {
1239                                                                         value = 0;
1240                                                                 }
1241                                                         }
1242                                                 }
1243
1244                                                 if (layno == 0) {
1245                                                         cblk->numpassesinlayers = 0;
1246                                                 }
1247
1248                                                 n = cblk->numpassesinlayers;
1249                                                 if (cblk->numpassesinlayers == 0) {
1250                                                         if (value != 0) {
1251                                                                 n = 3 * value - 2 + cblk->numpassesinlayers;
1252                                                         } else {
1253                                                                 n = cblk->numpassesinlayers;
1254                                                         }
1255                                                 } else {
1256                                                         n = 3 * value + cblk->numpassesinlayers;
1257                                                 }
1258
1259                                                 layer->numpasses = n - cblk->numpassesinlayers;
1260
1261                                                 if (!layer->numpasses)
1262                                                         continue;
1263
1264                                                 if (cblk->numpassesinlayers == 0) {
1265                                                         layer->len = cblk->passes[n - 1].rate;
1266                                                         layer->data = cblk->data;
1267                                                 } else {
1268                                                         layer->len = cblk->passes[n - 1].rate - cblk->passes[cblk->numpassesinlayers - 1].rate;
1269                                                         layer->data = cblk->data + cblk->passes[cblk->numpassesinlayers - 1].rate;
1270                                                 }
1271
1272                                                 if (final)
1273                                                         cblk->numpassesinlayers = n;
1274                                         }
1275                                 }
1276                         }
1277                 }
1278         }
1279 }
1280
1281 opj_bool tcd_rateallocate(opj_tcd_t *tcd, unsigned char *dest, int len, opj_codestream_info_t *cstr_info) {
1282         int compno, resno, bandno, precno, cblkno, passno, layno;
1283         double min, max;
1284         double cumdisto[100];   /* fixed_quality */
1285         const double K = 1;             /* 1.1; fixed_quality */
1286         double maxSE = 0;
1287
1288         opj_cp_t *cp = tcd->cp;
1289         opj_tcd_tile_t *tcd_tile = tcd->tcd_tile;
1290         opj_tcp_t *tcd_tcp = tcd->tcp;
1291
1292         min = DBL_MAX;
1293         max = 0;
1294         
1295         tcd_tile->numpix = 0;           /* fixed_quality */
1296         
1297         for (compno = 0; compno < tcd_tile->numcomps; compno++) {
1298                 opj_tcd_tilecomp_t *tilec = &tcd_tile->comps[compno];
1299                 tilec->numpix = 0;
1300
1301                 for (resno = 0; resno < tilec->numresolutions; resno++) {
1302                         opj_tcd_resolution_t *res = &tilec->resolutions[resno];
1303
1304                         for (bandno = 0; bandno < res->numbands; bandno++) {
1305                                 opj_tcd_band_t *band = &res->bands[bandno];
1306
1307                                 for (precno = 0; precno < res->pw * res->ph; precno++) {
1308                                         opj_tcd_precinct_t *prc = &band->precincts[precno];
1309
1310                                         for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
1311                                                 opj_tcd_cblk_enc_t *cblk = &prc->cblks.enc[cblkno];
1312
1313                                                 for (passno = 0; passno < cblk->totalpasses; passno++) {
1314                                                         opj_tcd_pass_t *pass = &cblk->passes[passno];
1315                                                         int dr;
1316                                                         double dd, rdslope;
1317                                                         if (passno == 0) {
1318                                                                 dr = pass->rate;
1319                                                                 dd = pass->distortiondec;
1320                                                         } else {
1321                                                                 dr = pass->rate - cblk->passes[passno - 1].rate;
1322                                                                 dd = pass->distortiondec - cblk->passes[passno - 1].distortiondec;
1323                                                         }
1324                                                         if (dr == 0) {
1325                                                                 continue;
1326                                                         }
1327                                                         rdslope = dd / dr;
1328                                                         if (rdslope < min) {
1329                                                                 min = rdslope;
1330                                                         }
1331                                                         if (rdslope > max) {
1332                                                                 max = rdslope;
1333                                                         }
1334                                                 } /* passno */
1335                                                 
1336                                                 /* fixed_quality */
1337                                                 tcd_tile->numpix += ((cblk->x1 - cblk->x0) * (cblk->y1 - cblk->y0));
1338                                                 tilec->numpix += ((cblk->x1 - cblk->x0) * (cblk->y1 - cblk->y0));
1339                                         } /* cbklno */
1340                                 } /* precno */
1341                         } /* bandno */
1342                 } /* resno */
1343                 
1344                 maxSE += (((double)(1 << tcd->image->comps[compno].prec) - 1.0) 
1345                         * ((double)(1 << tcd->image->comps[compno].prec) -1.0)) 
1346                         * ((double)(tilec->numpix));
1347         } /* compno */
1348         
1349         /* index file */
1350         if(cstr_info) {
1351                 opj_tile_info_t *tile_info = &cstr_info->tile[tcd->tcd_tileno];
1352                 tile_info->numpix = tcd_tile->numpix;
1353                 tile_info->distotile = tcd_tile->distotile;
1354                 tile_info->thresh = (double *) opj_malloc(tcd_tcp->numlayers * sizeof(double));
1355         }
1356         
1357         for (layno = 0; layno < tcd_tcp->numlayers; layno++) {
1358                 double lo = min;
1359                 double hi = max;
1360                 int success = 0;
1361                 int maxlen = tcd_tcp->rates[layno] ? int_min(((int) ceil(tcd_tcp->rates[layno])), len) : len;
1362                 double goodthresh = 0;
1363                 double stable_thresh = 0;
1364                 int i;
1365                 double distotarget;             /* fixed_quality */
1366                 
1367                 /* fixed_quality */
1368                 distotarget = tcd_tile->distotile - ((K * maxSE) / pow((float)10, tcd_tcp->distoratio[layno] / 10));
1369         
1370                 /* Don't try to find an optimal threshold but rather take everything not included yet, if
1371                   -r xx,yy,zz,0   (disto_alloc == 1 and rates == 0)
1372                   -q xx,yy,zz,0   (fixed_quality == 1 and distoratio == 0)
1373                   ==> possible to have some lossy layers and the last layer for sure lossless */
1374                 if ( ((cp->disto_alloc==1) && (tcd_tcp->rates[layno]>0)) || ((cp->fixed_quality==1) && (tcd_tcp->distoratio[layno]>0))) {
1375                         opj_t2_t *t2 = t2_create(tcd->cinfo, tcd->image, cp);
1376                         double thresh = 0;
1377
1378                         for (i = 0; i < 128; i++) {
1379                                 int l = 0;
1380                                 double distoachieved = 0;       /* fixed_quality */
1381                                 thresh = (lo + hi) / 2;
1382                                 
1383                                 tcd_makelayer(tcd, layno, thresh, 0);
1384                                 
1385                                 if (cp->fixed_quality) {        /* fixed_quality */
1386                                         if(cp->cinema){
1387                                                 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);
1388                                                 if (l == -999) {
1389                                                         lo = thresh;
1390                                                         continue;
1391                                                 }else{
1392                         distoachieved = layno == 0 ? 
1393                                                         tcd_tile->distolayer[0] : cumdisto[layno - 1] + tcd_tile->distolayer[layno];
1394                                                         if (distoachieved < distotarget) {
1395                                                                 hi=thresh; 
1396                                                                 stable_thresh = thresh;
1397                                                                 continue;
1398                                                         }else{
1399                                                                 lo=thresh;
1400                                                         }
1401                                                 }
1402                                         }else{
1403                                                 distoachieved = (layno == 0) ? 
1404                                                         tcd_tile->distolayer[0] : (cumdisto[layno - 1] + tcd_tile->distolayer[layno]);
1405                                                 if (distoachieved < distotarget) {
1406                                                         hi = thresh;
1407                                                         stable_thresh = thresh;
1408                                                         continue;
1409                                                 }
1410                                                 lo = thresh;
1411                                         }
1412                                 } else {
1413                                         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);
1414                                         /* TODO: what to do with l ??? seek / tell ??? */
1415                                         /* opj_event_msg(tcd->cinfo, EVT_INFO, "rate alloc: len=%d, max=%d\n", l, maxlen); */
1416                                         if (l == -999) {
1417                                                 lo = thresh;
1418                                                 continue;
1419                                         }
1420                                         hi = thresh;
1421                                         stable_thresh = thresh;
1422                                 }
1423                         }
1424                         success = 1;
1425                         goodthresh = stable_thresh == 0? thresh : stable_thresh;
1426                         t2_destroy(t2);
1427                 } else {
1428                         success = 1;
1429                         goodthresh = min;
1430                 }
1431                 
1432                 if (!success) {
1433                         return OPJ_FALSE;
1434                 }
1435                 
1436                 if(cstr_info) { /* Threshold for Marcela Index */
1437                         cstr_info->tile[tcd->tcd_tileno].thresh[layno] = goodthresh;
1438                 }
1439                 tcd_makelayer(tcd, layno, goodthresh, 1);
1440         
1441                 /* fixed_quality */
1442                 cumdisto[layno] = (layno == 0) ? tcd_tile->distolayer[0] : (cumdisto[layno - 1] + tcd_tile->distolayer[layno]); 
1443         }
1444
1445         return OPJ_TRUE;
1446 }
1447
1448 opj_bool tcd_rateallocate_v2(   opj_tcd_v2_t *tcd,
1449                                                                 OPJ_BYTE *dest,
1450                                                                 OPJ_UINT32 * p_data_written,
1451                                                                 OPJ_UINT32 len,
1452                                                                 opj_codestream_info_t *cstr_info)
1453 {
1454         OPJ_UINT32 compno, resno, bandno, precno, cblkno, layno;
1455         OPJ_UINT32 passno;
1456         OPJ_FLOAT64 min, max;
1457         OPJ_FLOAT64 cumdisto[100];      /* fixed_quality */
1458         const OPJ_FLOAT64 K = 1;                /* 1.1; fixed_quality */
1459         OPJ_FLOAT64 maxSE = 0;
1460
1461         opj_cp_v2_t *cp = tcd->cp;
1462         opj_tcd_tile_v2_t *tcd_tile = tcd->tcd_image->tiles;
1463         opj_tcp_v2_t *tcd_tcp = tcd->tcp;
1464
1465         min = DBL_MAX;
1466         max = 0;
1467
1468         tcd_tile->numpix = 0;           /* fixed_quality */
1469
1470         for (compno = 0; compno < tcd_tile->numcomps; compno++) {
1471                 opj_tcd_tilecomp_v2_t *tilec = &tcd_tile->comps[compno];
1472                 tilec->numpix = 0;
1473
1474                 for (resno = 0; resno < tilec->numresolutions; resno++) {
1475                         opj_tcd_resolution_v2_t *res = &tilec->resolutions[resno];
1476
1477                         for (bandno = 0; bandno < res->numbands; bandno++) {
1478                                 opj_tcd_band_v2_t *band = &res->bands[bandno];
1479
1480                                 for (precno = 0; precno < res->pw * res->ph; precno++) {
1481                                         opj_tcd_precinct_v2_t *prc = &band->precincts[precno];
1482
1483                                         for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
1484                                                 opj_tcd_cblk_enc_v2_t *cblk = &prc->cblks.enc[cblkno];
1485
1486                                                 for (passno = 0; passno < cblk->totalpasses; passno++) {
1487                                                         opj_tcd_pass_v2_t *pass = &cblk->passes[passno];
1488                                                         OPJ_INT32 dr;
1489                                                         OPJ_FLOAT64 dd, rdslope;
1490
1491                                                         if (passno == 0) {
1492                                                                 dr = pass->rate;
1493                                                                 dd = pass->distortiondec;
1494                                                         } else {
1495                                                                 dr = pass->rate - cblk->passes[passno - 1].rate;
1496                                                                 dd = pass->distortiondec - cblk->passes[passno - 1].distortiondec;
1497                                                         }
1498
1499                                                         if (dr == 0) {
1500                                                                 continue;
1501                                                         }
1502
1503                                                         rdslope = dd / dr;
1504                                                         if (rdslope < min) {
1505                                                                 min = rdslope;
1506                                                         }
1507
1508                                                         if (rdslope > max) {
1509                                                                 max = rdslope;
1510                                                         }
1511                                                 } /* passno */
1512
1513                                                 /* fixed_quality */
1514                                                 tcd_tile->numpix += ((cblk->x1 - cblk->x0) * (cblk->y1 - cblk->y0));
1515                                                 tilec->numpix += ((cblk->x1 - cblk->x0) * (cblk->y1 - cblk->y0));
1516                                         } /* cbklno */
1517                                 } /* precno */
1518                         } /* bandno */
1519                 } /* resno */
1520
1521                 maxSE += (((OPJ_FLOAT64)(1 << tcd->image->comps[compno].prec) - 1.0)
1522                         * ((OPJ_FLOAT64)(1 << tcd->image->comps[compno].prec) -1.0))
1523                         * ((OPJ_FLOAT64)(tilec->numpix));
1524         } /* compno */
1525
1526         /* index file */
1527         if(cstr_info) {
1528                 opj_tile_info_t *tile_info = &cstr_info->tile[tcd->tcd_tileno];
1529                 tile_info->numpix = tcd_tile->numpix;
1530                 tile_info->distotile = tcd_tile->distotile;
1531                 tile_info->thresh = (OPJ_FLOAT64 *) opj_malloc(tcd_tcp->numlayers * sizeof(OPJ_FLOAT64));
1532         }
1533
1534         for (layno = 0; layno < tcd_tcp->numlayers; layno++) {
1535                 OPJ_FLOAT64 lo = min;
1536                 OPJ_FLOAT64 hi = max;
1537                 opj_bool success = OPJ_FALSE;
1538                 OPJ_UINT32 maxlen = tcd_tcp->rates[layno] ? uint_min(((OPJ_UINT32) ceil(tcd_tcp->rates[layno])), len) : len;
1539                 OPJ_FLOAT64 goodthresh = 0;
1540                 OPJ_FLOAT64 stable_thresh = 0;
1541                 OPJ_UINT32 i;
1542                 OPJ_FLOAT64 distotarget;                /* fixed_quality */
1543
1544                 /* fixed_quality */
1545                 distotarget = tcd_tile->distotile - ((K * maxSE) / pow((OPJ_FLOAT32)10, tcd_tcp->distoratio[layno] / 10));
1546
1547                 /* Don't try to find an optimal threshold but rather take everything not included yet, if
1548                   -r xx,yy,zz,0   (disto_alloc == 1 and rates == 0)
1549                   -q xx,yy,zz,0   (fixed_quality == 1 and distoratio == 0)
1550                   ==> possible to have some lossy layers and the last layer for sure lossless */
1551                 if ( ((cp->m_specific_param.m_enc.m_disto_alloc==1) && (tcd_tcp->rates[layno]>0)) || ((cp->m_specific_param.m_enc.m_fixed_quality==1) && (tcd_tcp->distoratio[layno]>0))) {
1552                         opj_t2_v2_t*t2 = t2_create_v2(tcd->image, cp);
1553                         OPJ_FLOAT64 thresh = 0;
1554
1555                         if (t2 == 00) {
1556                                 return OPJ_FALSE;
1557                         }
1558
1559                         for     (i = 0; i < 128; ++i) {
1560                                 OPJ_FLOAT64 distoachieved = 0;  /* fixed_quality */
1561
1562                                 thresh = (lo + hi) / 2;
1563
1564                                 tcd_makelayer_v2(tcd, layno, thresh, 0);
1565
1566                                 if (cp->m_specific_param.m_enc.m_fixed_quality) {       /* fixed_quality */
1567                                         if(cp->m_specific_param.m_enc.m_cinema){
1568                                                 if (! t2_encode_packets_v2(t2,tcd->tcd_tileno, tcd_tile, layno + 1, dest, p_data_written, maxlen, cstr_info,tcd->cur_tp_num,tcd->tp_pos,tcd->cur_pino,THRESH_CALC)) {
1569
1570                                                         lo = thresh;
1571                                                         continue;
1572                                                 }
1573                                                 else {
1574                                                         distoachieved = layno == 0 ?
1575                                                                         tcd_tile->distolayer[0] : cumdisto[layno - 1] + tcd_tile->distolayer[layno];
1576
1577                                                         if (distoachieved < distotarget) {
1578                                                                 hi=thresh;
1579                                                                 stable_thresh = thresh;
1580                                                                 continue;
1581                                                         }else{
1582                                                                 lo=thresh;
1583                                                         }
1584                                                 }
1585                                         }else{
1586                                                 distoachieved = (layno == 0) ?
1587                                                                 tcd_tile->distolayer[0] : (cumdisto[layno - 1] + tcd_tile->distolayer[layno]);
1588
1589                                                 if (distoachieved < distotarget) {
1590                                                         hi = thresh;
1591                                                         stable_thresh = thresh;
1592                                                         continue;
1593                                                 }
1594                                                 lo = thresh;
1595                                         }
1596                                 } else {
1597                                         if (! t2_encode_packets_v2(t2, tcd->tcd_tileno, tcd_tile, layno + 1, dest,p_data_written, maxlen, cstr_info,tcd->cur_tp_num,tcd->tp_pos,tcd->cur_pino,THRESH_CALC))
1598                                         {
1599                                                 /* TODO: what to do with l ??? seek / tell ??? */
1600                                                 /* opj_event_msg(tcd->cinfo, EVT_INFO, "rate alloc: len=%d, max=%d\n", l, maxlen); */
1601                                                 lo = thresh;
1602                                                 continue;
1603                                         }
1604
1605                                         hi = thresh;
1606                                         stable_thresh = thresh;
1607                                 }
1608                         }
1609
1610                         success = OPJ_TRUE;
1611                         goodthresh = stable_thresh == 0? thresh : stable_thresh;
1612
1613                         t2_destroy_v2(t2);
1614                 } else {
1615                         success = OPJ_TRUE;
1616                         goodthresh = min;
1617                 }
1618
1619                 if (!success) {
1620                         return OPJ_FALSE;
1621                 }
1622
1623                 if(cstr_info) { /* Threshold for Marcela Index */
1624                         cstr_info->tile[tcd->tcd_tileno].thresh[layno] = goodthresh;
1625                 }
1626
1627                 tcd_makelayer_v2(tcd, layno, goodthresh, 1);
1628
1629                 /* fixed_quality */
1630                 cumdisto[layno] = (layno == 0) ? tcd_tile->distolayer[0] : (cumdisto[layno - 1] + tcd_tile->distolayer[layno]);
1631         }
1632
1633         return OPJ_TRUE;
1634 }
1635
1636
1637 int tcd_encode_tile(opj_tcd_t *tcd, int tileno, unsigned char *dest, int len, opj_codestream_info_t *cstr_info) {
1638         int compno;
1639         int l, i, numpacks = 0;
1640         opj_tcd_tile_t *tile = NULL;
1641         opj_tcp_t *tcd_tcp = NULL;
1642         opj_cp_t *cp = NULL;
1643
1644         opj_tcp_t *tcp = &tcd->cp->tcps[0];
1645         opj_tccp_t *tccp = &tcp->tccps[0];
1646         opj_image_t *image = tcd->image;
1647         
1648         opj_t1_t *t1 = NULL;            /* T1 component */
1649         opj_t2_t *t2 = NULL;            /* T2 component */
1650
1651         tcd->tcd_tileno = tileno;
1652         tcd->tcd_tile = tcd->tcd_image->tiles;
1653         tcd->tcp = &tcd->cp->tcps[tileno];
1654
1655         tile = tcd->tcd_tile;
1656         tcd_tcp = tcd->tcp;
1657         cp = tcd->cp;
1658
1659         if(tcd->cur_tp_num == 0){
1660                 tcd->encoding_time = opj_clock();       /* time needed to encode a tile */
1661                 /* INDEX >> "Precinct_nb_X et Precinct_nb_Y" */
1662                 if(cstr_info) {
1663                         opj_tcd_tilecomp_t *tilec_idx = &tile->comps[0];        /* based on component 0 */
1664                         for (i = 0; i < tilec_idx->numresolutions; i++) {
1665                                 opj_tcd_resolution_t *res_idx = &tilec_idx->resolutions[i];
1666                                 
1667                                 cstr_info->tile[tileno].pw[i] = res_idx->pw;
1668                                 cstr_info->tile[tileno].ph[i] = res_idx->ph;
1669                                 
1670                                 numpacks += res_idx->pw * res_idx->ph;
1671                                 
1672                                 cstr_info->tile[tileno].pdx[i] = tccp->prcw[i];
1673                                 cstr_info->tile[tileno].pdy[i] = tccp->prch[i];
1674                         }
1675                         cstr_info->tile[tileno].packet = (opj_packet_info_t*) opj_calloc(cstr_info->numcomps * cstr_info->numlayers * numpacks, sizeof(opj_packet_info_t));
1676                 }
1677                 /* << INDEX */
1678                 
1679                 /*---------------TILE-------------------*/
1680                 
1681                 for (compno = 0; compno < tile->numcomps; compno++) {
1682                         int x, y;
1683                         
1684                         int adjust = image->comps[compno].sgnd ? 0 : 1 << (image->comps[compno].prec - 1);
1685                         int offset_x = int_ceildiv(image->x0, image->comps[compno].dx);
1686                         int offset_y = int_ceildiv(image->y0, image->comps[compno].dy);
1687                         
1688                         opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
1689                         int tw = tilec->x1 - tilec->x0;
1690                         int w = int_ceildiv(image->x1 - image->x0, image->comps[compno].dx);
1691                         
1692                         /* extract tile data */
1693                         
1694                         if (tcd_tcp->tccps[compno].qmfbid == 1) {
1695                                 for (y = tilec->y0; y < tilec->y1; y++) {
1696                                         /* start of the src tile scanline */
1697                                         int *data = &image->comps[compno].data[(tilec->x0 - offset_x) + (y - offset_y) * w];
1698                                         /* start of the dst tile scanline */
1699                                         int *tile_data = &tilec->data[(y - tilec->y0) * tw];
1700                                         for (x = tilec->x0; x < tilec->x1; x++) {
1701                                                 *tile_data++ = *data++ - adjust;
1702                                         }
1703                                 }
1704                         } else if (tcd_tcp->tccps[compno].qmfbid == 0) {
1705                                 for (y = tilec->y0; y < tilec->y1; y++) {
1706                                         /* start of the src tile scanline */
1707                                         int *data = &image->comps[compno].data[(tilec->x0 - offset_x) + (y - offset_y) * w];
1708                                         /* start of the dst tile scanline */
1709                                         int *tile_data = &tilec->data[(y - tilec->y0) * tw];
1710                                         for (x = tilec->x0; x < tilec->x1; x++) {
1711                                                 *tile_data++ = (*data++ - adjust) << 11;
1712                                         }
1713                                         
1714                                 }
1715                         }
1716                 }
1717                 
1718                 /*----------------MCT-------------------*/
1719                 if (tcd_tcp->mct) {
1720                         int samples = (tile->comps[0].x1 - tile->comps[0].x0) * (tile->comps[0].y1 - tile->comps[0].y0);
1721                         if (tcd_tcp->tccps[0].qmfbid == 0) {
1722                                 mct_encode_real(tile->comps[0].data, tile->comps[1].data, tile->comps[2].data, samples);
1723                         } else {
1724                                 mct_encode(tile->comps[0].data, tile->comps[1].data, tile->comps[2].data, samples);
1725                         }
1726                 }
1727                 
1728                 /*----------------DWT---------------------*/
1729                 
1730                 for (compno = 0; compno < tile->numcomps; compno++) {
1731                         opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
1732                         if (tcd_tcp->tccps[compno].qmfbid == 1) {
1733                                 dwt_encode(tilec);
1734                         } else if (tcd_tcp->tccps[compno].qmfbid == 0) {
1735                                 dwt_encode_real(tilec);
1736                         }
1737                 }
1738                 
1739                 /*------------------TIER1-----------------*/
1740                 t1 = t1_create(tcd->cinfo);
1741                 t1_encode_cblks(t1, tile, tcd_tcp);
1742                 t1_destroy(t1);
1743                 
1744                 /*-----------RATE-ALLOCATE------------------*/
1745                 
1746                 /* INDEX */
1747                 if(cstr_info) {
1748                         cstr_info->index_write = 0;
1749                 }
1750                 if (cp->disto_alloc || cp->fixed_quality) {     /* fixed_quality */
1751                         /* Normal Rate/distortion allocation */
1752                         tcd_rateallocate(tcd, dest, len, cstr_info);
1753                 } else {
1754                         /* Fixed layer allocation */
1755                         tcd_rateallocate_fixed(tcd);
1756                 }
1757         }
1758         /*--------------TIER2------------------*/
1759
1760         /* INDEX */
1761         if(cstr_info) {
1762                 cstr_info->index_write = 1;
1763         }
1764
1765         t2 = t2_create(tcd->cinfo, image, cp);
1766         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);
1767         t2_destroy(t2);
1768         
1769         /*---------------CLEAN-------------------*/
1770
1771         
1772         if(tcd->cur_tp_num == tcd->cur_totnum_tp - 1){
1773                 tcd->encoding_time = opj_clock() - tcd->encoding_time;
1774                 opj_event_msg(tcd->cinfo, EVT_INFO, "- tile encoded in %f s\n", tcd->encoding_time);
1775
1776                 /* cleaning memory */
1777                 for (compno = 0; compno < tile->numcomps; compno++) {
1778                         opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
1779                         opj_aligned_free(tilec->data);
1780                 }
1781         }
1782
1783         return l;
1784 }
1785
1786 opj_bool tcd_decode_tile(opj_tcd_t *tcd, unsigned char *src, int len, int tileno, opj_codestream_info_t *cstr_info) {
1787         int l;
1788         int compno;
1789         int eof = 0;
1790         double tile_time, t1_time, dwt_time;
1791         opj_tcd_tile_t *tile = NULL;
1792
1793         opj_t1_t *t1 = NULL;            /* T1 component */
1794         opj_t2_t *t2 = NULL;            /* T2 component */
1795         
1796         tcd->tcd_tileno = tileno;
1797         tcd->tcd_tile = &(tcd->tcd_image->tiles[tileno]);
1798         tcd->tcp = &(tcd->cp->tcps[tileno]);
1799         tile = tcd->tcd_tile;
1800         
1801         tile_time = opj_clock();        /* time needed to decode a tile */
1802         opj_event_msg(tcd->cinfo, EVT_INFO, "tile %d of %d\n", tileno + 1, tcd->cp->tw * tcd->cp->th);
1803
1804         /* INDEX >>  */
1805         if(cstr_info) {
1806                 int resno, compno, numprec = 0;
1807                 for (compno = 0; compno < cstr_info->numcomps; compno++) {
1808                         opj_tcp_t *tcp = &tcd->cp->tcps[0];
1809                         opj_tccp_t *tccp = &tcp->tccps[compno];
1810                         opj_tcd_tilecomp_t *tilec_idx = &tile->comps[compno];   
1811                         for (resno = 0; resno < tilec_idx->numresolutions; resno++) {
1812                                 opj_tcd_resolution_t *res_idx = &tilec_idx->resolutions[resno];
1813                                 cstr_info->tile[tileno].pw[resno] = res_idx->pw;
1814                                 cstr_info->tile[tileno].ph[resno] = res_idx->ph;
1815                                 numprec += res_idx->pw * res_idx->ph;
1816                                 if (tccp->csty & J2K_CP_CSTY_PRT) {
1817                                         cstr_info->tile[tileno].pdx[resno] = tccp->prcw[resno];
1818                                         cstr_info->tile[tileno].pdy[resno] = tccp->prch[resno];
1819                                 }
1820                                 else {
1821                                         cstr_info->tile[tileno].pdx[resno] = 15;
1822                                         cstr_info->tile[tileno].pdy[resno] = 15;
1823                                 }
1824                         }
1825                 }
1826                 cstr_info->tile[tileno].packet = (opj_packet_info_t *) opj_malloc(cstr_info->numlayers * numprec * sizeof(opj_packet_info_t));
1827                 cstr_info->packno = 0;
1828         }
1829         /* << INDEX */
1830         
1831         /*--------------TIER2------------------*/
1832         
1833         t2 = t2_create(tcd->cinfo, tcd->image, tcd->cp);
1834         l = t2_decode_packets(t2, src, len, tileno, tile, cstr_info);
1835         t2_destroy(t2);
1836
1837         if (l == -999) {
1838                 eof = 1;
1839                 opj_event_msg(tcd->cinfo, EVT_ERROR, "tcd_decode: incomplete bistream\n");
1840         }
1841         
1842         /*------------------TIER1-----------------*/
1843         
1844         t1_time = opj_clock();  /* time needed to decode a tile */
1845         t1 = t1_create(tcd->cinfo);
1846         for (compno = 0; compno < tile->numcomps; ++compno) {
1847                 opj_tcd_tilecomp_t* tilec = &tile->comps[compno];
1848                 /* The +3 is headroom required by the vectorized DWT */
1849                 tilec->data = (int*) opj_aligned_malloc((((tilec->x1 - tilec->x0) * (tilec->y1 - tilec->y0))+3) * sizeof(int));
1850                 t1_decode_cblks(t1, tilec, &tcd->tcp->tccps[compno]);
1851         }
1852         t1_destroy(t1);
1853         t1_time = opj_clock() - t1_time;
1854         opj_event_msg(tcd->cinfo, EVT_INFO, "- tiers-1 took %f s\n", t1_time);
1855         
1856         /*----------------DWT---------------------*/
1857
1858         dwt_time = opj_clock(); /* time needed to decode a tile */
1859         for (compno = 0; compno < tile->numcomps; compno++) {
1860                 opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
1861                 int numres2decode;
1862
1863                 if (tcd->cp->reduce != 0) {
1864                         tcd->image->comps[compno].resno_decoded =
1865                                 tile->comps[compno].numresolutions - tcd->cp->reduce - 1;
1866                         if (tcd->image->comps[compno].resno_decoded < 0) {                              
1867                                 opj_event_msg(tcd->cinfo, EVT_ERROR, "Error decoding tile. The number of resolutions to remove [%d+1] is higher than the number "
1868                                         " of resolutions in the original codestream [%d]\nModify the cp_reduce parameter.\n", tcd->cp->reduce, tile->comps[compno].numresolutions);
1869                                 return OPJ_FALSE;
1870                         }
1871                 }
1872
1873                 numres2decode = tcd->image->comps[compno].resno_decoded + 1;
1874                 if(numres2decode > 0){
1875                         if (tcd->tcp->tccps[compno].qmfbid == 1) {
1876                                 dwt_decode(tilec, numres2decode);
1877                         } else {
1878                                 dwt_decode_real(tilec, numres2decode);
1879                         }
1880                 }
1881         }
1882         dwt_time = opj_clock() - dwt_time;
1883         opj_event_msg(tcd->cinfo, EVT_INFO, "- dwt took %f s\n", dwt_time);
1884
1885         /*----------------MCT-------------------*/
1886
1887         if (tcd->tcp->mct) {
1888                 int n = (tile->comps[0].x1 - tile->comps[0].x0) * (tile->comps[0].y1 - tile->comps[0].y0);
1889
1890                 if (tile->numcomps >= 3 ){
1891                         if (tcd->tcp->tccps[0].qmfbid == 1) {
1892                                 mct_decode(
1893                                                 tile->comps[0].data,
1894                                                 tile->comps[1].data,
1895                                                 tile->comps[2].data,
1896                                                 n);
1897                         } else {
1898                                 mct_decode_real(
1899                                                 (float*)tile->comps[0].data,
1900                                                 (float*)tile->comps[1].data,
1901                                                 (float*)tile->comps[2].data,
1902                                                 n);
1903                         }
1904                 } else{
1905                         opj_event_msg(tcd->cinfo, EVT_WARNING,"Number of components (%d) is inconsistent with a MCT. Skip the MCT step.\n",tile->numcomps);
1906                 }
1907         }
1908
1909         /*---------------TILE-------------------*/
1910
1911         for (compno = 0; compno < tile->numcomps; ++compno) {
1912                 opj_tcd_tilecomp_t* tilec = &tile->comps[compno];
1913                 opj_image_comp_t* imagec = &tcd->image->comps[compno];
1914                 opj_tcd_resolution_t* res = &tilec->resolutions[imagec->resno_decoded];
1915                 int adjust = imagec->sgnd ? 0 : 1 << (imagec->prec - 1);
1916                 int min = imagec->sgnd ? -(1 << (imagec->prec - 1)) : 0;
1917                 int max = imagec->sgnd ?  (1 << (imagec->prec - 1)) - 1 : (1 << imagec->prec) - 1;
1918
1919                 int tw = tilec->x1 - tilec->x0;
1920                 int w = imagec->w;
1921
1922                 int offset_x = int_ceildivpow2(imagec->x0, imagec->factor);
1923                 int offset_y = int_ceildivpow2(imagec->y0, imagec->factor);
1924
1925                 int i, j;
1926                 if(!imagec->data){
1927                         imagec->data = (int*) opj_malloc(imagec->w * imagec->h * sizeof(int));
1928                 }
1929                 if(tcd->tcp->tccps[compno].qmfbid == 1) {
1930                         for(j = res->y0; j < res->y1; ++j) {
1931                                 for(i = res->x0; i < res->x1; ++i) {
1932                                         int v = tilec->data[i - res->x0 + (j - res->y0) * tw];
1933                                         v += adjust;
1934                                         imagec->data[(i - offset_x) + (j - offset_y) * w] = int_clamp(v, min, max);
1935                                 }
1936                         }
1937                 }else{
1938                         for(j = res->y0; j < res->y1; ++j) {
1939                                 for(i = res->x0; i < res->x1; ++i) {
1940                                         float tmp = ((float*)tilec->data)[i - res->x0 + (j - res->y0) * tw];
1941                                         int v = lrintf(tmp);
1942                                         v += adjust;
1943                                         imagec->data[(i - offset_x) + (j - offset_y) * w] = int_clamp(v, min, max);
1944                                 }
1945                         }
1946                 }
1947                 opj_aligned_free(tilec->data);
1948         }
1949
1950         tile_time = opj_clock() - tile_time;    /* time needed to decode a tile */
1951         opj_event_msg(tcd->cinfo, EVT_INFO, "- tile decoded in %f s\n", tile_time);
1952
1953         if (eof) {
1954                 return OPJ_FALSE;
1955         }
1956         
1957         return OPJ_TRUE;
1958 }
1959
1960 void tcd_free_decode(opj_tcd_t *tcd) {
1961         opj_tcd_image_t *tcd_image = tcd->tcd_image;    
1962         opj_free(tcd_image->tiles);
1963 }
1964
1965 void tcd_free_decode_tile(opj_tcd_t *tcd, int tileno) {
1966         int compno,resno,bandno,precno;
1967
1968         opj_tcd_image_t *tcd_image = tcd->tcd_image;
1969
1970         opj_tcd_tile_t *tile = &tcd_image->tiles[tileno];
1971         for (compno = 0; compno < tile->numcomps; compno++) {
1972                 opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
1973                 for (resno = 0; resno < tilec->numresolutions; resno++) {
1974                         opj_tcd_resolution_t *res = &tilec->resolutions[resno];
1975                         for (bandno = 0; bandno < res->numbands; bandno++) {
1976                                 opj_tcd_band_t *band = &res->bands[bandno];
1977                                 for (precno = 0; precno < res->ph * res->pw; precno++) {
1978                                         opj_tcd_precinct_t *prec = &band->precincts[precno];
1979                                         if (prec->imsbtree != NULL) tgt_destroy(prec->imsbtree);
1980                                         if (prec->incltree != NULL) tgt_destroy(prec->incltree);
1981                                 }
1982                                 opj_free(band->precincts);
1983                         }
1984                 }
1985                 opj_free(tilec->resolutions);
1986         }
1987         opj_free(tile->comps);
1988 }
1989
1990
1991 opj_bool tcd_init_v2(  opj_tcd_v2_t *p_tcd,
1992                                            opj_image_t * p_image,
1993                                            opj_cp_v2_t * p_cp )
1994 {
1995         OPJ_UINT32 l_tile_comp_size;
1996
1997         p_tcd->image = p_image;
1998         p_tcd->cp = p_cp;
1999
2000         p_tcd->tcd_image->tiles = (opj_tcd_tile_v2_t *) opj_malloc(sizeof(opj_tcd_tile_v2_t));
2001         if (! p_tcd->tcd_image->tiles) {
2002                 return OPJ_FALSE;
2003         }
2004         memset(p_tcd->tcd_image->tiles,0, sizeof(opj_tcd_tile_v2_t));
2005
2006         l_tile_comp_size = p_image->numcomps * sizeof(opj_tcd_tilecomp_v2_t);
2007         p_tcd->tcd_image->tiles->comps = (opj_tcd_tilecomp_v2_t *) opj_malloc(l_tile_comp_size);
2008         if (! p_tcd->tcd_image->tiles->comps ) {
2009                 return OPJ_FALSE;
2010         }
2011         memset( p_tcd->tcd_image->tiles->comps , 0 , l_tile_comp_size);
2012
2013         p_tcd->tcd_image->tiles->numcomps = p_image->numcomps;
2014         p_tcd->tp_pos = p_cp->m_specific_param.m_enc.m_tp_pos;
2015
2016         return OPJ_TRUE;
2017 }
2018
2019 /**
2020 Destroy a previously created TCD handle
2021 */
2022 void tcd_destroy_v2(opj_tcd_v2_t *tcd) {
2023         if (tcd) {
2024                 tcd_free_tile(tcd);
2025
2026                 if (tcd->tcd_image) {
2027                         opj_free(tcd->tcd_image);
2028                         tcd->tcd_image = 00;
2029                 }
2030                 opj_free(tcd);
2031         }
2032 }
2033
2034 /* ----------------------------------------------------------------------- */
2035 /**
2036  * Initialize the tile coder and may reuse some meory.
2037  * @param       p_tcd           TCD handle.
2038  * @param       p_image         raw image.
2039  * @param       p_cp            coding parameters.
2040  * @param       p_tile_no       current tile index to encode.
2041  *
2042  * @return true if the encoding values could be set (false otherwise).
2043 */
2044 #define MACRO_TCD_ALLOCATE(FUNCTION,TYPE,FRACTION,ELEMENT,FUNCTION_ELEMENT)             \
2045 opj_bool FUNCTION (     opj_tcd_v2_t *p_tcd,                                                                            \
2046                                         OPJ_UINT32 p_tile_no                                                                            \
2047                         )                                                                                                                                       \
2048 {                                                                                                                                                               \
2049         OPJ_UINT32 (*l_gain_ptr)(OPJ_UINT32) = 00;                                                                      \
2050         OPJ_UINT32 compno, resno, bandno, precno, cblkno;                                                       \
2051         opj_tcp_v2_t * l_tcp = 00;                                                                                                      \
2052         opj_cp_v2_t * l_cp = 00;                                                                                                        \
2053         opj_tcd_tile_v2_t * l_tile = 00;                                                                                        \
2054         opj_tccp_t *l_tccp = 00;                                                                                                        \
2055         opj_tcd_tilecomp_v2_t *l_tilec = 00;                                                                            \
2056         opj_image_comp_t * l_image_comp = 00;                                                                           \
2057         opj_tcd_resolution_v2_t *l_res = 00;                                                                            \
2058         opj_tcd_band_v2_t *l_band = 00;                                                                                         \
2059         opj_stepsize_t * l_step_size = 00;                                                                                      \
2060         opj_tcd_precinct_v2_t *l_current_precinct = 00;                                                         \
2061         TYPE* l_code_block = 00;                                                                                                        \
2062         opj_image_t *l_image = 00;                                                                                                      \
2063         OPJ_UINT32 p,q;                                                                                                                         \
2064         OPJ_UINT32 l_level_no;                                                                                                          \
2065         OPJ_UINT32 l_pdx, l_pdy;                                                                                                        \
2066         OPJ_UINT32 l_gain;                                                                                                                      \
2067         OPJ_INT32 l_x0b, l_y0b;                                                                                                         \
2068         /* extent of precincts , top left, bottom right**/                                                      \
2069         OPJ_INT32 l_tl_prc_x_start, l_tl_prc_y_start, l_br_prc_x_end, l_br_prc_y_end;   \
2070         /* number of precinct for a resolution */                                                                       \
2071         OPJ_UINT32 l_nb_precincts;                                                                                                      \
2072         /* room needed to store l_nb_precinct precinct for a resolution */                      \
2073         OPJ_UINT32 l_nb_precinct_size;                                                                                          \
2074         /* number of code blocks for a precinct*/                                                                       \
2075         OPJ_UINT32 l_nb_code_blocks;                                                                                            \
2076         /* room needed to store l_nb_code_blocks code blocks for a precinct*/           \
2077         OPJ_UINT32 l_nb_code_blocks_size;                                                                                       \
2078         /* size of data for a tile */                                                                                           \
2079         OPJ_UINT32 l_data_size;                                                                                                         \
2080                                                                                                                                                                 \
2081         l_cp = p_tcd->cp;                                                                                                                       \
2082         l_tcp = &(l_cp->tcps[p_tile_no]);                                                                                       \
2083         l_tile = p_tcd->tcd_image->tiles;                                                                                       \
2084         l_tccp = l_tcp->tccps;                                                                                                          \
2085         l_tilec = l_tile->comps;                                                                                                        \
2086         l_image = p_tcd->image;                                                                                                         \
2087         l_image_comp = p_tcd->image->comps;                                                                                     \
2088                                                                                                                                                                 \
2089         p = p_tile_no % l_cp->tw;       /* tile coordinates */                                                  \
2090         q = p_tile_no / l_cp->tw;                                                                                                       \
2091         /*fprintf(stderr, "Tile coordinate = %d,%d\n", p, q);*/                                         \
2092                                                                                                                                                                 \
2093         /* 4 borders of the tile rescale on the image if necessary */                           \
2094         l_tile->x0 = int_max(l_cp->tx0 + p * l_cp->tdx, l_image->x0);                           \
2095         l_tile->y0 = int_max(l_cp->ty0 + q * l_cp->tdy, l_image->y0);                           \
2096         l_tile->x1 = int_min(l_cp->tx0 + (p + 1) * l_cp->tdx, l_image->x1);                     \
2097         l_tile->y1 = int_min(l_cp->ty0 + (q + 1) * l_cp->tdy, l_image->y1);                     \
2098         /*fprintf(stderr, "Tile border = %d,%d,%d,%d\n", l_tile->x0, l_tile->y0,l_tile->x1,l_tile->y1);*/\
2099                                                                                                                                                                 \
2100         /*tile->numcomps = image->numcomps; */                                                                          \
2101         for(compno = 0; compno < l_tile->numcomps; ++compno) {                                          \
2102                 /*fprintf(stderr, "compno = %d/%d\n", compno, l_tile->numcomps);*/              \
2103                                                                                                                                                                 \
2104                 /* border of each l_tile component (global) */                                                  \
2105                 l_tilec->x0 = int_ceildiv(l_tile->x0, l_image_comp->dx);                                \
2106                 l_tilec->y0 = int_ceildiv(l_tile->y0, l_image_comp->dy);                                \
2107                 l_tilec->x1 = int_ceildiv(l_tile->x1, l_image_comp->dx);                                \
2108                 l_tilec->y1 = int_ceildiv(l_tile->y1, l_image_comp->dy);                                \
2109                 /*fprintf(stderr, "\tTile compo border = %d,%d,%d,%d\n", l_tilec->x0, l_tilec->y0,l_tilec->x1,l_tilec->y1);*/\
2110                                                                                                                                                                 \
2111                 l_data_size = (l_tilec->x1 - l_tilec->x0)                                                               \
2112                                         * (l_tilec->y1 - l_tilec->y0) * sizeof(OPJ_UINT32 );            \
2113                 l_tilec->numresolutions = l_tccp->numresolutions;                                               \
2114                 if (l_tccp->numresolutions < l_cp->m_specific_param.m_dec.m_reduce) {   \
2115                         l_tilec->minimum_num_resolutions = 1;                                                           \
2116                 }                                                                                                                                               \
2117                 else {                                                                                                                                  \
2118                         l_tilec->minimum_num_resolutions = l_tccp->numresolutions                       \
2119                                 - l_cp->m_specific_param.m_dec.m_reduce;                                                \
2120                 }                                                                                                                                               \
2121                                                                                                                                                                 \
2122                 if (l_tilec->data == 00) {                                                                                              \
2123             l_tilec->data = (OPJ_INT32 *) opj_malloc(l_data_size);              \
2124                         if (! l_tilec->data ) {                                                                                         \
2125                                 return OPJ_FALSE;                                                                                               \
2126                         }                                                                                                                                       \
2127             /*fprintf(stderr, "\tAllocate data of tilec (int): %d x OPJ_UINT32\n",l_data_size);*/       \
2128                                                                                                                                                                 \
2129                         l_tilec->data_size = l_data_size;                                                                       \
2130                 }                                                                                                                                               \
2131                 else if (l_data_size > l_tilec->data_size) {                                                    \
2132                         l_tilec->data = (OPJ_INT32 *) opj_realloc(l_tilec->data, l_data_size);  \
2133                         if (! l_tilec->data) {                                                                                          \
2134                                 return OPJ_FALSE;                                                                                               \
2135                         }                                                                                                                                       \
2136                         /*fprintf(stderr, "\tReallocate data of tilec (int): from %d to %d x OPJ_UINT32\n", l_tilec->data_size, l_data_size);*/         \
2137                         l_tilec->data_size = l_data_size;                                                                       \
2138                 }                                                                                                                                               \
2139                                                                                                                                                         \
2140                 l_data_size = l_tilec->numresolutions * sizeof(opj_tcd_resolution_v2_t);        \
2141                                                                                                                                                                 \
2142                 if (l_tilec->resolutions == 00) {                                                                               \
2143             l_tilec->resolutions = (opj_tcd_resolution_v2_t *) opj_malloc(l_data_size); \
2144                         if (! l_tilec->resolutions ) {                                                                          \
2145                                 return OPJ_FALSE;                                                                                               \
2146                         }                                                                                                                                       \
2147                         /*fprintf(stderr, "\tAllocate resolutions of tilec (opj_tcd_resolution_v2_t): %d\n",l_data_size);*/             \
2148                         l_tilec->resolutions_size = l_data_size;                                                        \
2149                         memset(l_tilec->resolutions,0,l_data_size);                                                     \
2150                 }                                                                                                                                               \
2151                 else if (l_data_size > l_tilec->resolutions_size) {                                             \
2152                         l_tilec->resolutions = (opj_tcd_resolution_v2_t *) opj_realloc(l_tilec->resolutions, l_data_size);      \
2153                         if (! l_tilec->resolutions) {                                                                           \
2154                                 return OPJ_FALSE;                                                                                               \
2155                         }                                                                                                                                       \
2156                         /*fprintf(stderr, "\tReallocate data of tilec (int): from %d to %d x OPJ_UINT32\n", l_tilec->resolutions_size, l_data_size);*/  \
2157                         memset(((OPJ_BYTE*) l_tilec->resolutions)+l_tilec->resolutions_size,0,l_data_size - l_tilec->resolutions_size); \
2158                         l_tilec->resolutions_size = l_data_size;                                                        \
2159                 }                                                                                                                                               \
2160                                                                                                                                                                 \
2161                 l_level_no = l_tilec->numresolutions - 1;                                                               \
2162                 l_res = l_tilec->resolutions;                                                                                   \
2163                 l_step_size = l_tccp->stepsizes;                                                                                \
2164                 if (l_tccp->qmfbid == 0) {                                                                                              \
2165                         l_gain_ptr = &dwt_getgain_real_v2;                                                                      \
2166                 }                                                                                                                                               \
2167                 else {                                                                                                                                  \
2168                         l_gain_ptr  = &dwt_getgain_v2;                                                                          \
2169                 }                                                                                                                                               \
2170                 /*fprintf(stderr, "\tlevel_no=%d\n",l_level_no);*/                                              \
2171                                                                                                                                                                 \
2172                 for(resno = 0; resno < l_tilec->numresolutions; ++resno) {                              \
2173                         /*fprintf(stderr, "\t\tresno = %d/%d\n", resno, l_tilec->numresolutions);*/     \
2174                         OPJ_INT32 tlcbgxstart, tlcbgystart, brcbgxend, brcbgyend;                       \
2175                         OPJ_UINT32 cbgwidthexpn, cbgheightexpn;                                                         \
2176                         OPJ_UINT32 cblkwidthexpn, cblkheightexpn;                                                       \
2177                                                                                                                                                                 \
2178                         /* border for each resolution level (global) */                                         \
2179                         l_res->x0 = int_ceildivpow2(l_tilec->x0, l_level_no);                           \
2180                         l_res->y0 = int_ceildivpow2(l_tilec->y0, l_level_no);                           \
2181                         l_res->x1 = int_ceildivpow2(l_tilec->x1, l_level_no);                           \
2182                         l_res->y1 = int_ceildivpow2(l_tilec->y1, l_level_no);                           \
2183                         /*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);*/        \
2184                         /* p. 35, table A-23, ISO/IEC FDIS154444-1 : 2000 (18 august 2000) */   \
2185                         l_pdx = l_tccp->prcw[resno];                                                                            \
2186                         l_pdy = l_tccp->prch[resno];                                                                            \
2187                         /*fprintf(stderr, "\t\t\tpdx=%d, pdy=%d\n", l_pdx, l_pdy);*/            \
2188                         /* p. 64, B.6, ISO/IEC FDIS15444-1 : 2000 (18 august 2000)  */          \
2189                         l_tl_prc_x_start = int_floordivpow2(l_res->x0, l_pdx) << l_pdx;         \
2190                         l_tl_prc_y_start = int_floordivpow2(l_res->y0, l_pdy) << l_pdy;         \
2191                         l_br_prc_x_end = int_ceildivpow2(l_res->x1, l_pdx) << l_pdx;            \
2192                         l_br_prc_y_end = int_ceildivpow2(l_res->y1, l_pdy) << l_pdy;            \
2193                         /*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 );*/  \
2194                                                                                                                                                                 \
2195                         l_res->pw = (l_res->x0 == l_res->x1) ? 0 : ((l_br_prc_x_end - l_tl_prc_x_start) >> l_pdx);      \
2196                         l_res->ph = (l_res->y0 == l_res->y1) ? 0 : ((l_br_prc_y_end - l_tl_prc_y_start) >> l_pdy);      \
2197                         /*fprintf(stderr, "\t\t\tres_pw=%d, res_ph=%d\n", l_res->pw, l_res->ph );*/     \
2198                                                                                                                                                                 \
2199                         l_nb_precincts = l_res->pw * l_res->ph;                                                         \
2200                         l_nb_precinct_size = l_nb_precincts * sizeof(opj_tcd_precinct_v2_t);    \
2201                         if (resno == 0) {                                                                                                       \
2202                                 tlcbgxstart = l_tl_prc_x_start;                                                                 \
2203                                 tlcbgystart = l_tl_prc_y_start;                                                                 \
2204                                 brcbgxend = l_br_prc_x_end;                                                                             \
2205                                 brcbgyend = l_br_prc_y_end;                                                                             \
2206                                 cbgwidthexpn = l_pdx;                                                                                   \
2207                                 cbgheightexpn = l_pdy;                                                                                  \
2208                                 l_res->numbands = 1;                                                                                    \
2209                         }                                                                                                                                       \
2210                         else {                                                                                                                          \
2211                                 tlcbgxstart = int_ceildivpow2(l_tl_prc_x_start, 1);                             \
2212                                 tlcbgystart = int_ceildivpow2(l_tl_prc_y_start, 1);                             \
2213                                 brcbgxend = int_ceildivpow2(l_br_prc_x_end, 1);                                 \
2214                                 brcbgyend = int_ceildivpow2(l_br_prc_y_end, 1);                                 \
2215                                 cbgwidthexpn = l_pdx - 1;                                                                               \
2216                                 cbgheightexpn = l_pdy - 1;                                                                              \
2217                                 l_res->numbands = 3;                                                                                    \
2218                         }                                                                                                                                       \
2219                                                                                                                                                                 \
2220                         cblkwidthexpn = uint_min(l_tccp->cblkw, cbgwidthexpn);                          \
2221                         cblkheightexpn = uint_min(l_tccp->cblkh, cbgheightexpn);                        \
2222                         l_band = l_res->bands;                                                                                          \
2223                                                                                                                                                                 \
2224                         for (bandno = 0; bandno < l_res->numbands; ++bandno) {                          \
2225                                 OPJ_INT32 numbps;\
2226                                 /*fprintf(stderr, "\t\t\tband_no=%d/%d\n", bandno, l_res->numbands );*/ \
2227                                                                                                                                                                 \
2228                                 if (resno == 0) {                                                                                               \
2229                                         l_band->bandno = 0 ;                                                                            \
2230                                         l_band->x0 = int_ceildivpow2(l_tilec->x0, l_level_no);          \
2231                                         l_band->y0 = int_ceildivpow2(l_tilec->y0, l_level_no);          \
2232                                         l_band->x1 = int_ceildivpow2(l_tilec->x1, l_level_no);          \
2233                                         l_band->y1 = int_ceildivpow2(l_tilec->y1, l_level_no);          \
2234                                 }                                                                                                                               \
2235                                 else {                                                                                                                  \
2236                                         l_band->bandno = bandno + 1;                                                            \
2237                                         /* x0b = 1 if bandno = 1 or 3 */                                                        \
2238                                         l_x0b = l_band->bandno&1;                                                                       \
2239                                         /* y0b = 1 if bandno = 2 or 3 */                                                        \
2240                                         l_y0b = (l_band->bandno)>>1;                                                            \
2241                                         /* l_band border (global) */                                                            \
2242                                         l_band->x0 = int_ceildivpow2(l_tilec->x0 - (1 << l_level_no) * l_x0b, l_level_no + 1);  \
2243                                         l_band->y0 = int_ceildivpow2(l_tilec->y0 - (1 << l_level_no) * l_y0b, l_level_no + 1);  \
2244                                         l_band->x1 = int_ceildivpow2(l_tilec->x1 - (1 << l_level_no) * l_x0b, l_level_no + 1);  \
2245                                         l_band->y1 = int_ceildivpow2(l_tilec->y1 - (1 << l_level_no) * l_y0b, l_level_no + 1);  \
2246                                 }                                                                                                                               \
2247                                                                                                                                                                 \
2248                                 /** avoid an if with storing function pointer */                                \
2249                                 l_gain = (*l_gain_ptr) (l_band->bandno);                                                \
2250                                 numbps = l_image_comp->prec + l_gain;                                                   \
2251                                 l_band->stepsize = (OPJ_FLOAT32)(((1.0 + l_step_size->mant / 2048.0) * pow(2.0, (OPJ_INT32) (numbps - l_step_size->expn)))) * FRACTION;\
2252                                 l_band->numbps = l_step_size->expn + l_tccp->numgbits - 1;      /* WHY -1 ? */\
2253                                                                                                                                                                 \
2254                                 if (! l_band->precincts) {                                                                              \
2255                                         l_band->precincts = (opj_tcd_precinct_v2_t *) opj_malloc( /*3 * */ l_nb_precinct_size);\
2256                                         if (! l_band->precincts) {                                                                      \
2257                                                 return OPJ_FALSE;                                                                               \
2258                                         }                                                                                                                       \
2259                                         /*fprintf(stderr, "\t\t\t\tAllocate precincts of a band (opj_tcd_precinct_v2_t): %d\n",l_nb_precinct_size);     */      \
2260                                         memset(l_band->precincts,0,l_nb_precinct_size);                         \
2261                                         l_band->precincts_data_size = l_nb_precinct_size;                       \
2262                                 }                                                                                                                               \
2263                                 else if (l_band->precincts_data_size < l_nb_precinct_size) {    \
2264                                                                                                                                                                 \
2265                                         l_band->precincts = (opj_tcd_precinct_v2_t *) opj_realloc(l_band->precincts,/*3 * */ l_nb_precinct_size);\
2266                                         if (! l_band->precincts) {                                                                      \
2267                                                 return OPJ_FALSE;                                                                               \
2268                                         }                                                                                                                       \
2269                                         /*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);*/\
2270                                         memset(((OPJ_BYTE *) l_band->precincts) + l_band->precincts_data_size,0,l_nb_precinct_size - l_band->precincts_data_size);\
2271                                         l_band->precincts_data_size = l_nb_precinct_size;                       \
2272                                 }                                                                                                                               \
2273                                                                                                                                                                 \
2274                                 l_current_precinct = l_band->precincts;                                                 \
2275                                 for     (precno = 0; precno < l_nb_precincts; ++precno) {                       \
2276                                         OPJ_INT32 tlcblkxstart, tlcblkystart, brcblkxend, brcblkyend;   \
2277                                         OPJ_INT32 cbgxstart = tlcbgxstart + (precno % l_res->pw) * (1 << cbgwidthexpn); \
2278                                         OPJ_INT32 cbgystart = tlcbgystart + (precno / l_res->pw) * (1 << cbgheightexpn);        \
2279                                         OPJ_INT32 cbgxend = cbgxstart + (1 << cbgwidthexpn);            \
2280                                         OPJ_INT32 cbgyend = cbgystart + (1 << cbgheightexpn);           \
2281                                         /*fprintf(stderr, "\t precno=%d; bandno=%d, resno=%d; compno=%d\n", precno, bandno , resno, compno);*/\
2282                                         /*fprintf(stderr, "\t tlcbgxstart(=%d) + (precno(=%d) percent res->pw(=%d)) * (1 << cbgwidthexpn(=%d)) \n",tlcbgxstart,precno,l_res->pw,cbgwidthexpn);*/\
2283                                                                                                                                                                 \
2284                                         /* precinct size (global) */                                                            \
2285                                         /*fprintf(stderr, "\t cbgxstart=%d, l_band->x0 = %d \n",cbgxstart, l_band->x0);*/ \
2286                                                                                                                                                                 \
2287                                         l_current_precinct->x0 = int_max(cbgxstart, l_band->x0);        \
2288                                         l_current_precinct->y0 = int_max(cbgystart, l_band->y0);        \
2289                                         l_current_precinct->x1 = int_min(cbgxend, l_band->x1);          \
2290                                         l_current_precinct->y1 = int_min(cbgyend, l_band->y1);          \
2291                                         /*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);*/ \
2292                                                                                                                                                                 \
2293                                         tlcblkxstart = int_floordivpow2(l_current_precinct->x0, cblkwidthexpn) << cblkwidthexpn;        \
2294                                         /*fprintf(stderr, "\t tlcblkxstart =%d\n",tlcblkxstart );*/ \
2295                                         tlcblkystart = int_floordivpow2(l_current_precinct->y0, cblkheightexpn) << cblkheightexpn;      \
2296                                         /*fprintf(stderr, "\t tlcblkystart =%d\n",tlcblkystart );*/ \
2297                                         brcblkxend = int_ceildivpow2(l_current_precinct->x1, cblkwidthexpn) << cblkwidthexpn;   \
2298                                         /*fprintf(stderr, "\t brcblkxend =%d\n",brcblkxend );*/         \
2299                                         brcblkyend = int_ceildivpow2(l_current_precinct->y1, cblkheightexpn) << cblkheightexpn; \
2300                                         /*fprintf(stderr, "\t brcblkyend =%d\n",brcblkyend );*/         \
2301                                         l_current_precinct->cw = (brcblkxend - tlcblkxstart) >> cblkwidthexpn;  \
2302                                         l_current_precinct->ch = (brcblkyend - tlcblkystart) >> cblkheightexpn; \
2303                                                                                                                                                                 \
2304                                         l_nb_code_blocks = l_current_precinct->cw * l_current_precinct->ch;     \
2305                                         /*fprintf(stderr, "\t\t\t\t precinct_cw = %d x recinct_ch = %d\n",l_current_precinct->cw, l_current_precinct->ch);      */      \
2306                                         l_nb_code_blocks_size = l_nb_code_blocks * sizeof(TYPE);        \
2307                                                                                                                                                                 \
2308                                         if (! l_current_precinct->cblks.ELEMENT) {                                      \
2309                                                 l_current_precinct->cblks.ELEMENT = (TYPE*) opj_malloc(l_nb_code_blocks_size);\
2310                                                 if (! l_current_precinct->cblks.ELEMENT ) {                             \
2311                                                         return OPJ_FALSE;                                                                       \
2312                                                 }                                                                                                               \
2313                                                 /*fprintf(stderr, "\t\t\t\tAllocate cblks of a precinct (opj_tcd_cblk_dec_v2_t): %d\n",l_nb_code_blocks_size);*/                \
2314                                                                                                                                                                 \
2315                                                 memset(l_current_precinct->cblks.ELEMENT,0,l_nb_code_blocks_size);\
2316                                                                                                                                                                 \
2317                                                 l_current_precinct->block_size = l_nb_code_blocks_size; \
2318                                         }                                                                                                                       \
2319                                         else if (l_nb_code_blocks_size > l_current_precinct->block_size) {      \
2320                                                 l_current_precinct->cblks.ELEMENT = (TYPE*)                                                                     \
2321                                                         opj_realloc(l_current_precinct->cblks.ELEMENT, l_nb_code_blocks_size);  \
2322                                                 if (! l_current_precinct->cblks.ELEMENT ) {                             \
2323                                                         return OPJ_FALSE;                                                                       \
2324                                                 }                                                                                                               \
2325                                                 /*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);     */      \
2326                                                                                                                                                                 \
2327                                                 memset(((OPJ_BYTE *) l_current_precinct->cblks.ELEMENT) + l_current_precinct->block_size        \
2328                                                                                 ,0                                                                                                                                                      \
2329                                                                                 ,l_nb_code_blocks_size - l_current_precinct->block_size);                                       \
2330                                                                                                                                                                 \
2331                                                 l_current_precinct->block_size = l_nb_code_blocks_size; \
2332                                         }                                                                                                                       \
2333                                                                                                                                                                 \
2334                                         if (! l_current_precinct->incltree) {                                           \
2335                         l_current_precinct->incltree = tgt_create_v2(l_current_precinct->cw,    \
2336                                                                                                                                   l_current_precinct->ch);      \
2337                                         }                                                                                                                       \
2338                                         else{                                                                                                           \
2339                                                 l_current_precinct->incltree = tgt_init(l_current_precinct->incltree,   \
2340                                                                                                                                 l_current_precinct->cw,                 \
2341                                                                                                                                 l_current_precinct->ch);                \
2342                                         }                                                                                                                       \
2343                                                                                                                                                                 \
2344                                         if (! l_current_precinct->incltree)     {                                               \
2345                                                 fprintf(stderr, "WARNING: No incltree created.\n");\
2346                                                 /*return OPJ_FALSE;*/                                                                           \
2347                                         }                                                                                                                       \
2348                                                                                                                                                                 \
2349                                         if (! l_current_precinct->imsbtree) {                                           \
2350                         l_current_precinct->imsbtree = tgt_create_v2(                           \
2351                                                                                                                 l_current_precinct->cw, \
2352                                                                                                                 l_current_precinct->ch);\
2353                                         }                                                                                                                       \
2354                                         else {                                                                                                          \
2355                                                 l_current_precinct->imsbtree = tgt_init(                                                        \
2356                                                                                                                         l_current_precinct->imsbtree,   \
2357                                                                                                                         l_current_precinct->cw,                 \
2358                                                                                                                         l_current_precinct->ch);                \
2359                                         }                                                                                                                       \
2360                                                                                                                                                                 \
2361                                         if (! l_current_precinct->imsbtree) {                                           \
2362                                                 fprintf(stderr, "WARNING: No imsbtree created.\n");\
2363                                                 /*return OPJ_FALSE;*/                                                                           \
2364                                         }                                                                                                                       \
2365                                                                                                                                                                 \
2366                                         l_code_block = l_current_precinct->cblks.ELEMENT;                       \
2367                                                                                                                                                                 \
2368                                         for (cblkno = 0; cblkno < l_nb_code_blocks; ++cblkno) {         \
2369                                                 OPJ_INT32 cblkxstart = tlcblkxstart + (cblkno % l_current_precinct->cw) * (1 << cblkwidthexpn);         \
2370                                                 OPJ_INT32 cblkystart = tlcblkystart + (cblkno / l_current_precinct->cw) * (1 << cblkheightexpn);        \
2371                                                 OPJ_INT32 cblkxend = cblkxstart + (1 << cblkwidthexpn); \
2372                                                 OPJ_INT32 cblkyend = cblkystart + (1 << cblkheightexpn);\
2373                                                                                                                                                                 \
2374                                                 /* code-block size (global) */                                                  \
2375                                                 l_code_block->x0 = int_max(cblkxstart, l_current_precinct->x0); \
2376                                                 l_code_block->y0 = int_max(cblkystart, l_current_precinct->y0); \
2377                                                 l_code_block->x1 = int_min(cblkxend, l_current_precinct->x1);   \
2378                                                 l_code_block->y1 = int_min(cblkyend, l_current_precinct->y1);   \
2379                                                                                                                                                                 \
2380                                                 if (! FUNCTION_ELEMENT(l_code_block)) {                                 \
2381                                                         return OPJ_FALSE;                                                                       \
2382                                                 }                                                                                                               \
2383                                                 ++l_code_block;                                                                                 \
2384                                         }                                                                                                                       \
2385                                         ++l_current_precinct;                                                                           \
2386                                 } /* precno */                                                                                                  \
2387                                 ++l_band;                                                                                                               \
2388                                 ++l_step_size;                                                                                                  \
2389                         } /* bandno */                                                                                                          \
2390                         ++l_res;                                                                                                                        \
2391                         --l_level_no;                                                                                                           \
2392                 } /* resno */                                                                                                                   \
2393                 ++l_tccp;                                                                                                                               \
2394                 ++l_tilec;                                                                                                                              \
2395                 ++l_image_comp;                                                                                                                 \
2396         } /* compno */                                                                                                                          \
2397         return OPJ_TRUE;                                                                                                                        \
2398 }                                                                                                                                                               \
2399
2400
2401 MACRO_TCD_ALLOCATE(tcd_init_encode_tile, opj_tcd_cblk_enc_v2_t, 1.f, enc, tcd_code_block_enc_allocate)
2402 MACRO_TCD_ALLOCATE(tcd_init_decode_tile, opj_tcd_cblk_dec_v2_t, 0.5f, dec, tcd_code_block_dec_allocate)
2403
2404 #undef MACRO_TCD_ALLOCATE
2405
2406 /**
2407  * Allocates memory for an encoding code block.
2408  */
2409 opj_bool tcd_code_block_enc_allocate (opj_tcd_cblk_enc_v2_t * p_code_block)
2410 {
2411         if (! p_code_block->data) {
2412
2413                 p_code_block->data = (OPJ_BYTE*) opj_malloc(8192+1);
2414                 if(! p_code_block->data) {
2415                         return OPJ_FALSE;
2416                 }
2417
2418                 p_code_block->data+=1;
2419
2420                 /* no memset since data */
2421                 p_code_block->layers = (opj_tcd_layer_t*) opj_malloc(100 * sizeof(opj_tcd_layer_t));
2422                 if (! p_code_block->layers) {
2423                         return OPJ_FALSE;
2424                 }
2425
2426                 p_code_block->passes = (opj_tcd_pass_v2_t*) opj_malloc(100 * sizeof(opj_tcd_pass_v2_t));
2427                 if (! p_code_block->passes) {
2428                         return OPJ_FALSE;
2429                 }
2430         }
2431
2432         memset(p_code_block->layers,0,100 * sizeof(opj_tcd_layer_t));
2433         memset(p_code_block->passes,0,100 * sizeof(opj_tcd_pass_v2_t));
2434
2435         return OPJ_TRUE;
2436 }
2437
2438 /**
2439  * Allocates memory for a decoding code block.
2440  */
2441 opj_bool tcd_code_block_dec_allocate (opj_tcd_cblk_dec_v2_t * p_code_block)
2442 {
2443         OPJ_UINT32 l_seg_size;
2444
2445         if (! p_code_block->data) {
2446
2447                 p_code_block->data = (OPJ_BYTE*) opj_malloc(8192);
2448                 if (! p_code_block->data) {
2449                         return OPJ_FALSE;
2450                 }
2451                 /*fprintf(stderr, "Allocate 8192 elements of code_block->data\n");*/
2452
2453                 l_seg_size = J2K_DEFAULT_NB_SEGS * sizeof(opj_tcd_seg_t);
2454                 p_code_block->segs = (opj_tcd_seg_t *) opj_malloc(l_seg_size);
2455                 if (! p_code_block->segs) {
2456                         return OPJ_FALSE;
2457                 }
2458                 memset(p_code_block->segs,0,l_seg_size);
2459                 /*fprintf(stderr, "Allocate %d elements of code_block->data\n", J2K_DEFAULT_NB_SEGS * sizeof(opj_tcd_seg_t));*/
2460
2461                 p_code_block->m_current_max_segs = J2K_DEFAULT_NB_SEGS;
2462                 /*fprintf(stderr, "m_current_max_segs of code_block->data = %d\n", p_code_block->m_current_max_segs);*/
2463         }
2464         /* TODO */
2465         /*p_code_block->numsegs = 0; */
2466
2467         return OPJ_TRUE;
2468 }
2469
2470 OPJ_UINT32 tcd_get_decoded_tile_size ( opj_tcd_v2_t *p_tcd )
2471 {
2472         OPJ_UINT32 i;
2473         OPJ_UINT32 l_data_size = 0;
2474         opj_image_comp_t * l_img_comp = 00;
2475         opj_tcd_tilecomp_v2_t * l_tile_comp = 00;
2476         opj_tcd_resolution_v2_t * l_res = 00;
2477         OPJ_UINT32 l_size_comp, l_remaining;
2478
2479         l_tile_comp = p_tcd->tcd_image->tiles->comps;
2480         l_img_comp = p_tcd->image->comps;
2481
2482         for (i=0;i<p_tcd->image->numcomps;++i) {
2483                 l_size_comp = l_img_comp->prec >> 3; /*(/ 8)*/
2484                 l_remaining = l_img_comp->prec & 7;  /* (%8) */
2485
2486                 if(l_remaining) {
2487                         ++l_size_comp;
2488                 }
2489
2490                 if (l_size_comp == 3) {
2491                         l_size_comp = 4;
2492                 }
2493
2494                 l_res = l_tile_comp->resolutions + l_tile_comp->minimum_num_resolutions - 1;
2495                 l_data_size += l_size_comp * (l_res->x1 - l_res->x0) * (l_res->y1 - l_res->y0);
2496                 ++l_img_comp;
2497                 ++l_tile_comp;
2498         }
2499
2500         return l_data_size;
2501 }
2502
2503 opj_bool tcd_encode_tile_v2(opj_tcd_v2_t *p_tcd,
2504                                                         OPJ_UINT32 p_tile_no,
2505                                                         OPJ_BYTE *p_dest,
2506                                                         OPJ_UINT32 * p_data_written,
2507                                                         OPJ_UINT32 p_max_length,
2508                                                         opj_codestream_info_t *p_cstr_info)
2509 {
2510
2511         if (p_tcd->cur_tp_num == 0) {
2512
2513                 p_tcd->tcd_tileno = p_tile_no;
2514                 p_tcd->tcp = &p_tcd->cp->tcps[p_tile_no];
2515
2516                 /* INDEX >> "Precinct_nb_X et Precinct_nb_Y" */
2517                 if(p_cstr_info)  {
2518                         OPJ_UINT32 l_num_packs = 0;
2519                         OPJ_UINT32 i;
2520                         opj_tcd_tilecomp_v2_t *l_tilec_idx = &p_tcd->tcd_image->tiles->comps[0];        /* based on component 0 */
2521                         opj_tccp_t *l_tccp = p_tcd->tcp->tccps; /* based on component 0 */
2522
2523                         for (i = 0; i < l_tilec_idx->numresolutions; i++) {
2524                                 opj_tcd_resolution_v2_t *l_res_idx = &l_tilec_idx->resolutions[i];
2525
2526                                 p_cstr_info->tile[p_tile_no].pw[i] = l_res_idx->pw;
2527                                 p_cstr_info->tile[p_tile_no].ph[i] = l_res_idx->ph;
2528
2529                                 l_num_packs += l_res_idx->pw * l_res_idx->ph;
2530                                 p_cstr_info->tile[p_tile_no].pdx[i] = l_tccp->prcw[i];
2531                                 p_cstr_info->tile[p_tile_no].pdy[i] = l_tccp->prch[i];
2532                         }
2533                         p_cstr_info->tile[p_tile_no].packet = (opj_packet_info_t*) opj_calloc(p_cstr_info->numcomps * p_cstr_info->numlayers * l_num_packs, sizeof(opj_packet_info_t));
2534                 }
2535                 /* << INDEX */
2536
2537                 /* FIXME _ProfStart(PGROUP_DC_SHIFT); */
2538                 /*---------------TILE-------------------*/
2539                 if (! tcd_dc_level_shift_encode(p_tcd)) {
2540                         return OPJ_FALSE;
2541                 }
2542                 /* FIXME _ProfStop(PGROUP_DC_SHIFT); */
2543
2544                 /* FIXME _ProfStart(PGROUP_MCT); */
2545                 if (! tcd_mct_encode(p_tcd)) {
2546                         return OPJ_FALSE;
2547                 }
2548                 /* FIXME _ProfStop(PGROUP_MCT); */
2549
2550                 /* FIXME _ProfStart(PGROUP_DWT); */
2551                 if (! tcd_dwt_encode(p_tcd)) {
2552                         return OPJ_FALSE;
2553                 }
2554                 /* FIXME  _ProfStop(PGROUP_DWT); */
2555
2556                 /* FIXME  _ProfStart(PGROUP_T1); */
2557                 if (! tcd_t1_encode(p_tcd)) {
2558                         return OPJ_FALSE;
2559                 }
2560                 /* FIXME _ProfStop(PGROUP_T1); */
2561
2562                 /* FIXME _ProfStart(PGROUP_RATE); */
2563                 if (! tcd_rate_allocate_encode(p_tcd,p_dest,p_max_length,p_cstr_info)) {
2564                         return OPJ_FALSE;
2565                 }
2566                 /* FIXME _ProfStop(PGROUP_RATE); */
2567
2568         }
2569         /*--------------TIER2------------------*/
2570
2571         /* INDEX */
2572         if (p_cstr_info) {
2573                 p_cstr_info->index_write = 1;
2574         }
2575         /* FIXME _ProfStart(PGROUP_T2); */
2576
2577         if (! tcd_t2_encode(p_tcd,p_dest,p_data_written,p_max_length,p_cstr_info)) {
2578                 return OPJ_FALSE;
2579         }
2580         /* FIXME _ProfStop(PGROUP_T2); */
2581
2582         /*---------------CLEAN-------------------*/
2583
2584         return OPJ_TRUE;
2585 }
2586
2587 opj_bool tcd_decode_tile_v2(
2588                                          opj_tcd_v2_t *p_tcd,
2589                                          OPJ_BYTE *p_src,
2590                                          OPJ_UINT32 p_max_length,
2591                                          OPJ_UINT32 p_tile_no,
2592                                          opj_codestream_index_t *p_cstr_index)
2593 {
2594         OPJ_UINT32 l_data_read;
2595         p_tcd->tcd_tileno = p_tile_no;
2596         p_tcd->tcp = &(p_tcd->cp->tcps[p_tile_no]);
2597
2598 #ifdef TODO_MSD /* FIXME */
2599         /* INDEX >>  */
2600         if(p_cstr_info) {
2601                 OPJ_UINT32 resno, compno, numprec = 0;
2602                 for (compno = 0; compno < (OPJ_UINT32) p_cstr_info->numcomps; compno++) {
2603                         opj_tcp_v2_t *tcp = &p_tcd->cp->tcps[0];
2604                         opj_tccp_t *tccp = &tcp->tccps[compno];
2605                         opj_tcd_tilecomp_v2_t *tilec_idx = &p_tcd->tcd_image->tiles->comps[compno];
2606                         for (resno = 0; resno < tilec_idx->numresolutions; resno++) {
2607                                 opj_tcd_resolution_v2_t *res_idx = &tilec_idx->resolutions[resno];
2608                                 p_cstr_info->tile[p_tile_no].pw[resno] = res_idx->pw;
2609                                 p_cstr_info->tile[p_tile_no].ph[resno] = res_idx->ph;
2610                                 numprec += res_idx->pw * res_idx->ph;
2611                                 p_cstr_info->tile[p_tile_no].pdx[resno] = tccp->prcw[resno];
2612                                 p_cstr_info->tile[p_tile_no].pdy[resno] = tccp->prch[resno];
2613                         }
2614                 }
2615                 p_cstr_info->tile[p_tile_no].packet = (opj_packet_info_t *) opj_malloc(p_cstr_info->numlayers * numprec * sizeof(opj_packet_info_t));
2616                 p_cstr_info->packno = 0;
2617         }
2618         /* << INDEX */
2619 #endif
2620
2621         /*--------------TIER2------------------*/
2622         /* FIXME _ProfStart(PGROUP_T2); */
2623         l_data_read = 0;
2624         if
2625                 (! tcd_t2_decode(p_tcd, p_src, &l_data_read, p_max_length, p_cstr_index))
2626         {
2627                 return OPJ_FALSE;
2628         }
2629         /* FIXME _ProfStop(PGROUP_T2); */
2630
2631         /*------------------TIER1-----------------*/
2632
2633         /* FIXME _ProfStart(PGROUP_T1); */
2634         if
2635                 (! tcd_t1_decode(p_tcd))
2636         {
2637                 return OPJ_FALSE;
2638         }
2639         /* FIXME _ProfStop(PGROUP_T1); */
2640
2641         /*----------------DWT---------------------*/
2642
2643         /* FIXME _ProfStart(PGROUP_DWT); */
2644         if
2645                 (! tcd_dwt_decode(p_tcd))
2646         {
2647                 return OPJ_FALSE;
2648         }
2649         /* FIXME _ProfStop(PGROUP_DWT); */
2650
2651         /*----------------MCT-------------------*/
2652         /* FIXME _ProfStart(PGROUP_MCT); */
2653         if
2654                 (! tcd_mct_decode(p_tcd))
2655         {
2656                 return OPJ_FALSE;
2657         }
2658         /* FIXME _ProfStop(PGROUP_MCT); */
2659
2660         /* FIXME _ProfStart(PGROUP_DC_SHIFT); */
2661         if
2662                 (! tcd_dc_level_shift_decode(p_tcd))
2663         {
2664                 return OPJ_FALSE;
2665         }
2666         /* FIXME _ProfStop(PGROUP_DC_SHIFT); */
2667
2668
2669         /*---------------TILE-------------------*/
2670         return OPJ_TRUE;
2671 }
2672
2673 opj_bool tcd_update_tile_data (
2674                                                  opj_tcd_v2_t *p_tcd,
2675                                                  OPJ_BYTE * p_dest,
2676                                                  OPJ_UINT32 p_dest_length
2677                                                  )
2678 {
2679         OPJ_UINT32 i,j,k,l_data_size = 0;
2680         opj_image_comp_t * l_img_comp = 00;
2681         opj_tcd_tilecomp_v2_t * l_tilec = 00;
2682         opj_tcd_resolution_v2_t * l_res;
2683         OPJ_UINT32 l_size_comp, l_remaining;
2684         OPJ_UINT32 l_stride, l_width,l_height;
2685
2686         l_data_size = tcd_get_decoded_tile_size(p_tcd);
2687         if (l_data_size > p_dest_length) {
2688                 return OPJ_FALSE;
2689         }
2690
2691         l_tilec = p_tcd->tcd_image->tiles->comps;
2692         l_img_comp = p_tcd->image->comps;
2693
2694         for (i=0;i<p_tcd->image->numcomps;++i) {
2695                 l_size_comp = l_img_comp->prec >> 3; /*(/ 8)*/
2696                 l_remaining = l_img_comp->prec & 7;  /* (%8) */
2697                 l_res = l_tilec->resolutions + l_img_comp->resno_decoded;
2698                 l_width = (l_res->x1 - l_res->x0);
2699                 l_height = (l_res->y1 - l_res->y0);
2700                 l_stride = (l_tilec->x1 - l_tilec->x0) - l_width;
2701
2702                 if (l_remaining) {
2703                         ++l_size_comp;
2704                 }
2705
2706                 if (l_size_comp == 3) {
2707                         l_size_comp = 4;
2708                 }
2709
2710                 switch (l_size_comp)
2711                         {
2712                         case 1:
2713                                 {
2714                                         OPJ_CHAR * l_dest_ptr = (OPJ_CHAR *) p_dest;
2715                                         const OPJ_INT32 * l_src_ptr = l_tilec->data;
2716
2717                                         if (l_img_comp->sgnd) {
2718                                                 for (j=0;j<l_height;++j) {
2719                                                         for (k=0;k<l_width;++k) {
2720                                                                 *(l_dest_ptr++) = (OPJ_CHAR) (*(l_src_ptr++));
2721                                                         }
2722                                                         l_src_ptr += l_stride;
2723                                                 }
2724                                         }
2725                                         else {
2726                                                 for (j=0;j<l_height;++j) {
2727                                                         for     (k=0;k<l_width;++k) {
2728                                                                 *(l_dest_ptr++) = (OPJ_BYTE) ((*(l_src_ptr++))&0xff);
2729                                                         }
2730                                                         l_src_ptr += l_stride;
2731                                                 }
2732                                         }
2733
2734                                         p_dest = (OPJ_BYTE *)l_dest_ptr;
2735                                 }
2736                                 break;
2737                         case 2:
2738                                 {
2739                                         const OPJ_INT32 * l_src_ptr = l_tilec->data;
2740                                         OPJ_INT16 * l_dest_ptr = (OPJ_INT16 *) p_dest;
2741
2742                                         if (l_img_comp->sgnd) {
2743                                                 for (j=0;j<l_height;++j) {
2744                                                         for (k=0;k<l_width;++k) {
2745                                                                 *(l_dest_ptr++) = (OPJ_INT16) (*(l_src_ptr++));
2746                                                         }
2747                                                         l_src_ptr += l_stride;
2748                                                 }
2749                                         }
2750                                         else {
2751                                                 for (j=0;j<l_height;++j) {
2752                                                         for (k=0;k<l_width;++k) {
2753                                                                 *(l_dest_ptr++) = (OPJ_UINT16) ((*(l_src_ptr++))&0xffff);
2754                                                         }
2755                                                         l_src_ptr += l_stride;
2756                                                 }
2757                                         }
2758
2759                                         p_dest = (OPJ_BYTE*) l_dest_ptr;
2760                                 }
2761                                 break;
2762                         case 4:
2763                                 {
2764                                         OPJ_INT32 * l_dest_ptr = (OPJ_INT32 *) p_dest;
2765                                         OPJ_INT32 * l_src_ptr = l_tilec->data;
2766
2767                                         for (j=0;j<l_height;++j) {
2768                                                 for (k=0;k<l_width;++k) {
2769                                                         *(l_dest_ptr++) = (*(l_src_ptr++));
2770                                                 }
2771                                                 l_src_ptr += l_stride;
2772                                         }
2773
2774                                         p_dest = (OPJ_BYTE*) l_dest_ptr;
2775                                 }
2776                                 break;
2777                 }
2778
2779                 ++l_img_comp;
2780                 ++l_tilec;
2781         }
2782
2783         return OPJ_TRUE;
2784 }
2785
2786
2787
2788
2789 void tcd_free_tile(opj_tcd_v2_t *p_tcd)
2790 {
2791         OPJ_UINT32 compno, resno, bandno, precno;
2792         opj_tcd_tile_v2_t *l_tile = 00;
2793         opj_tcd_tilecomp_v2_t *l_tile_comp = 00;
2794         opj_tcd_resolution_v2_t *l_res = 00;
2795         opj_tcd_band_v2_t *l_band = 00;
2796         opj_tcd_precinct_v2_t *l_precinct = 00;
2797         OPJ_UINT32 l_nb_resolutions, l_nb_precincts;
2798         void (* l_tcd_code_block_deallocate) (opj_tcd_precinct_v2_t *) = 00;
2799
2800         if (! p_tcd) {
2801                 return;
2802         }
2803
2804         if (! p_tcd->tcd_image) {
2805                 return;
2806         }
2807
2808         if (p_tcd->m_is_decoder) {
2809                 l_tcd_code_block_deallocate = tcd_code_block_dec_deallocate;
2810         }
2811         else {
2812                 /* FIXME l_tcd_code_block_deallocate = tcd_code_block_enc_deallocate; */
2813         }
2814
2815         l_tile = p_tcd->tcd_image->tiles;
2816         if (! l_tile) {
2817                 return;
2818         }
2819
2820         l_tile_comp = l_tile->comps;
2821
2822         for (compno = 0; compno < l_tile->numcomps; ++compno) {
2823                 l_res = l_tile_comp->resolutions;
2824                 if (l_res) {
2825
2826                         l_nb_resolutions = l_tile_comp->resolutions_size / sizeof(opj_tcd_resolution_v2_t);
2827                         for (resno = 0; resno < l_nb_resolutions; ++resno) {
2828                                 l_band = l_res->bands;
2829                                 for     (bandno = 0; bandno < 3; ++bandno) {
2830                                         l_precinct = l_band->precincts;
2831                                         if (l_precinct) {
2832
2833                                                 l_nb_precincts = l_band->precincts_data_size / sizeof(opj_tcd_precinct_v2_t);
2834                                                 for (precno = 0; precno < l_nb_precincts; ++precno) {
2835                                                         tgt_destroy(l_precinct->incltree);
2836                                                         l_precinct->incltree = 00;
2837                                                         tgt_destroy(l_precinct->imsbtree);
2838                                                         l_precinct->imsbtree = 00;
2839                                                         (*l_tcd_code_block_deallocate) (l_precinct);
2840                                                         ++l_precinct;
2841                                                 }
2842
2843                                                 opj_free(l_band->precincts);
2844                                                 l_band->precincts = 00;
2845                                         }
2846                                         ++l_band;
2847                                 } /* for (resno */
2848                                 ++l_res;
2849                         }
2850
2851                         opj_free(l_tile_comp->resolutions);
2852                         l_tile_comp->resolutions = 00;
2853                 }
2854
2855                 if (l_tile_comp->data) {
2856                         opj_free(l_tile_comp->data);
2857                         l_tile_comp->data = 00;
2858                 }
2859                 ++l_tile_comp;
2860         }
2861
2862         opj_free(l_tile->comps);
2863         l_tile->comps = 00;
2864         opj_free(p_tcd->tcd_image->tiles);
2865         p_tcd->tcd_image->tiles = 00;
2866 }
2867
2868
2869 opj_bool tcd_t2_decode (
2870                                         opj_tcd_v2_t *p_tcd,
2871                                         OPJ_BYTE * p_src_data,
2872                                         OPJ_UINT32 * p_data_read,
2873                                         OPJ_UINT32 p_max_src_size,
2874                                         opj_codestream_index_t *p_cstr_index
2875                                         )
2876 {
2877         opj_t2_v2_t * l_t2;
2878
2879         l_t2 = t2_create_v2(p_tcd->image, p_tcd->cp);
2880         if (l_t2 == 00) {
2881                 return OPJ_FALSE;
2882         }
2883
2884         if (! t2_decode_packets_v2(
2885                                         l_t2,
2886                                         p_tcd->tcd_tileno,
2887                                         p_tcd->tcd_image->tiles,
2888                                         p_src_data,
2889                                         p_data_read,
2890                                         p_max_src_size,
2891                                         p_cstr_index)) {
2892                 t2_destroy_v2(l_t2);
2893                 return OPJ_FALSE;
2894         }
2895
2896         t2_destroy_v2(l_t2);
2897
2898         /*---------------CLEAN-------------------*/
2899         return OPJ_TRUE;
2900 }
2901
2902 opj_bool tcd_t1_decode ( opj_tcd_v2_t *p_tcd )
2903 {
2904         OPJ_UINT32 compno;
2905         opj_t1_t * l_t1;
2906         opj_tcd_tile_v2_t * l_tile = p_tcd->tcd_image->tiles;
2907         opj_tcd_tilecomp_v2_t* l_tile_comp = l_tile->comps;
2908         opj_tccp_t * l_tccp = p_tcd->tcp->tccps;
2909
2910
2911         l_t1 = t1_create_v2();
2912         if (l_t1 == 00) {
2913                 return OPJ_FALSE;
2914         }
2915
2916         for (compno = 0; compno < l_tile->numcomps; ++compno) {
2917                 /* The +3 is headroom required by the vectorized DWT */
2918                 t1_decode_cblks_v2(l_t1, l_tile_comp, l_tccp);
2919                 ++l_tile_comp;
2920                 ++l_tccp;
2921         }
2922
2923         t1_destroy_v2(l_t1);
2924
2925         return OPJ_TRUE;
2926 }
2927
2928
2929 opj_bool tcd_dwt_decode ( opj_tcd_v2_t *p_tcd )
2930 {
2931         OPJ_UINT32 compno;
2932         opj_tcd_tile_v2_t * l_tile = p_tcd->tcd_image->tiles;
2933         opj_tcd_tilecomp_v2_t * l_tile_comp = l_tile->comps;
2934         opj_tccp_t * l_tccp = p_tcd->tcp->tccps;
2935         opj_image_comp_t * l_img_comp = p_tcd->image->comps;
2936
2937         for (compno = 0; compno < l_tile->numcomps; compno++) {
2938                 /*
2939                 if (tcd->cp->reduce != 0) {
2940                         tcd->image->comps[compno].resno_decoded =
2941                                 tile->comps[compno].numresolutions - tcd->cp->reduce - 1;
2942                         if (tcd->image->comps[compno].resno_decoded < 0)
2943                         {
2944                                 return false;
2945                         }
2946                 }
2947                 numres2decode = tcd->image->comps[compno].resno_decoded + 1;
2948                 if(numres2decode > 0){
2949                 */
2950
2951                 if (l_tccp->qmfbid == 1) {
2952                         if (! dwt_decode_v2(l_tile_comp, l_img_comp->resno_decoded+1)) {
2953                                 return OPJ_FALSE;
2954                         }
2955                 }
2956                 else {
2957                         if (! dwt_decode_real_v2(l_tile_comp, l_img_comp->resno_decoded+1)) {
2958                                 return OPJ_FALSE;
2959                         }
2960                 }
2961
2962                 ++l_tile_comp;
2963                 ++l_img_comp;
2964                 ++l_tccp;
2965         }
2966
2967         return OPJ_TRUE;
2968 }
2969 opj_bool tcd_mct_decode ( opj_tcd_v2_t *p_tcd )
2970 {
2971         opj_tcd_tile_v2_t * l_tile = p_tcd->tcd_image->tiles;
2972         opj_tcp_v2_t * l_tcp = p_tcd->tcp;
2973         opj_tcd_tilecomp_v2_t * l_tile_comp = l_tile->comps;
2974         OPJ_UINT32 l_samples,i;
2975
2976         if (! l_tcp->mct) {
2977                 return OPJ_TRUE;
2978         }
2979
2980         l_samples = (l_tile_comp->x1 - l_tile_comp->x0) * (l_tile_comp->y1 - l_tile_comp->y0);
2981
2982         if (l_tile->numcomps >= 3 ){
2983                 if (l_tcp->mct == 2) {
2984                         OPJ_BYTE ** l_data;
2985
2986                         if (! l_tcp->m_mct_decoding_matrix) {
2987                                 return OPJ_TRUE;
2988                         }
2989
2990                         l_data = (OPJ_BYTE **) opj_malloc(l_tile->numcomps*sizeof(OPJ_BYTE*));
2991                         if (! l_data) {
2992                                 return OPJ_FALSE;
2993                         }
2994
2995                         for (i=0;i<l_tile->numcomps;++i) {
2996                                 l_data[i] = (OPJ_BYTE*) l_tile_comp->data;
2997                                 ++l_tile_comp;
2998                         }
2999
3000                         if (! mct_decode_custom(/* MCT data */
3001                                                                         (OPJ_BYTE*) l_tcp->m_mct_decoding_matrix,
3002                                                                         /* size of components */
3003                                                                         l_samples,
3004                                                                         /* components */
3005                                                                         l_data,
3006                                                                         /* nb of components (i.e. size of pData) */
3007                                                                         l_tile->numcomps,
3008                                                                         /* tells if the data is signed */
3009                                                                         p_tcd->image->comps->sgnd)) {
3010                                 opj_free(l_data);
3011                                 return OPJ_FALSE;
3012                         }
3013
3014                         opj_free(l_data);
3015                 }
3016                 else {
3017                         if (l_tcp->tccps->qmfbid == 1) {
3018                                 mct_decode(     l_tile->comps[0].data,
3019                                                         l_tile->comps[1].data,
3020                                                         l_tile->comps[2].data,
3021                                                         l_samples);
3022                         }
3023                         else {
3024                                 mct_decode_real(        (float*)l_tile->comps[0].data,
3025                                                                         (float*)l_tile->comps[1].data,
3026                                                                         (float*)l_tile->comps[2].data,
3027                                                                         l_samples);
3028                         }
3029                 }
3030         }
3031         else {
3032                 /* FIXME need to use opj_event_msg_v2 function */
3033                 fprintf(stderr,"Number of components (%d) is inconsistent with a MCT. Skip the MCT step.\n",l_tile->numcomps);
3034         }
3035
3036         return OPJ_TRUE;
3037 }
3038
3039
3040 opj_bool tcd_dc_level_shift_decode ( opj_tcd_v2_t *p_tcd )
3041 {
3042         OPJ_UINT32 compno;
3043         opj_tcd_tilecomp_v2_t * l_tile_comp = 00;
3044         opj_tccp_t * l_tccp = 00;
3045         opj_image_comp_t * l_img_comp = 00;
3046         opj_tcd_resolution_v2_t* l_res = 00;
3047         opj_tcp_v2_t * l_tcp = 00;
3048         opj_tcd_tile_v2_t * l_tile;
3049         OPJ_UINT32 l_width,l_height,i,j;
3050         OPJ_INT32 * l_current_ptr;
3051         OPJ_INT32 l_min, l_max;
3052         OPJ_UINT32 l_stride;
3053
3054         l_tile = p_tcd->tcd_image->tiles;
3055         l_tile_comp = l_tile->comps;
3056         l_tcp = p_tcd->tcp;
3057         l_tccp = p_tcd->tcp->tccps;
3058         l_img_comp = p_tcd->image->comps;
3059
3060         for (compno = 0; compno < l_tile->numcomps; compno++) {
3061                 l_res = l_tile_comp->resolutions + l_img_comp->resno_decoded;
3062                 l_width = (l_res->x1 - l_res->x0);
3063                 l_height = (l_res->y1 - l_res->y0);
3064                 l_stride = (l_tile_comp->x1 - l_tile_comp->x0) - l_width;
3065
3066                 if (l_img_comp->sgnd) {
3067                         l_min = -(1 << (l_img_comp->prec - 1));
3068                         l_max = (1 << (l_img_comp->prec - 1)) - 1;
3069                 }
3070                 else {
3071             l_min = 0;
3072                         l_max = (1 << l_img_comp->prec) - 1;
3073                 }
3074
3075                 l_current_ptr = l_tile_comp->data;
3076
3077                 if (l_tccp->qmfbid == 1) {
3078                         for (j=0;j<l_height;++j) {
3079                                 for (i = 0; i < l_width; ++i) {
3080                                         *l_current_ptr = int_clamp(*l_current_ptr + l_tccp->m_dc_level_shift, l_min, l_max);
3081                                         ++l_current_ptr;
3082                                 }
3083                                 l_current_ptr += l_stride;
3084                         }
3085                 }
3086                 else {
3087                         for (j=0;j<l_height;++j) {
3088                                 for (i = 0; i < l_width; ++i) {
3089                                         OPJ_FLOAT32 l_value = *((OPJ_FLOAT32 *) l_current_ptr);
3090                                         *l_current_ptr = int_clamp(lrintf(l_value) + l_tccp->m_dc_level_shift, l_min, l_max); ;
3091                                         ++l_current_ptr;
3092                                 }
3093                                 l_current_ptr += l_stride;
3094                         }
3095                 }
3096
3097                 ++l_img_comp;
3098                 ++l_tccp;
3099                 ++l_tile_comp;
3100         }
3101
3102         return OPJ_TRUE;
3103 }
3104
3105
3106
3107 /**
3108  * Deallocates the encoding data of the given precinct.
3109  */
3110 void tcd_code_block_dec_deallocate (opj_tcd_precinct_v2_t * p_precinct)
3111 {
3112         OPJ_UINT32 cblkno , l_nb_code_blocks;
3113
3114         opj_tcd_cblk_dec_v2_t * l_code_block = p_precinct->cblks.dec;
3115         if (l_code_block) {
3116                 /*fprintf(stderr,"deallocate codeblock:{\n");*/
3117                 /*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);*/
3118                 /*fprintf(stderr,"\t numbps=%d, numlenbits=%d, len=%d, numnewpasses=%d, real_num_segs=%d, m_current_max_segs=%d\n ",
3119                                 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 );*/
3120
3121
3122                 l_nb_code_blocks = p_precinct->block_size / sizeof(opj_tcd_cblk_dec_v2_t);
3123                 /*fprintf(stderr,"nb_code_blocks =%d\t}\n", l_nb_code_blocks);*/
3124
3125                 for (cblkno = 0; cblkno < l_nb_code_blocks; ++cblkno) {
3126
3127                         if (l_code_block->data) {
3128                                 opj_free(l_code_block->data);
3129                                 l_code_block->data = 00;
3130                         }
3131
3132                         if (l_code_block->segs) {
3133                                 opj_free(l_code_block->segs );
3134                                 l_code_block->segs = 00;
3135                         }
3136
3137                         ++l_code_block;
3138                 }
3139
3140                 opj_free(p_precinct->cblks.dec);
3141                 p_precinct->cblks.dec = 00;
3142         }
3143 }
3144
3145 OPJ_UINT32 tcd_get_encoded_tile_size ( opj_tcd_v2_t *p_tcd )
3146 {
3147         OPJ_UINT32 i,l_data_size = 0;
3148         opj_image_comp_t * l_img_comp = 00;
3149         opj_tcd_tilecomp_v2_t * l_tilec = 00;
3150         OPJ_UINT32 l_size_comp, l_remaining;
3151
3152         l_tilec = p_tcd->tcd_image->tiles->comps;
3153         l_img_comp = p_tcd->image->comps;
3154         for (i=0;i<p_tcd->image->numcomps;++i) {
3155                 l_size_comp = l_img_comp->prec >> 3; /*(/ 8)*/
3156                 l_remaining = l_img_comp->prec & 7;  /* (%8) */
3157
3158                 if (l_remaining) {
3159                         ++l_size_comp;
3160                 }
3161
3162                 if (l_size_comp == 3) {
3163                         l_size_comp = 4;
3164                 }
3165
3166                 l_data_size += l_size_comp * (l_tilec->x1 - l_tilec->x0) * (l_tilec->y1 - l_tilec->y0);
3167                 ++l_img_comp;
3168                 ++l_tilec;
3169         }
3170
3171         return l_data_size;
3172 }
3173                 
3174 opj_bool tcd_dc_level_shift_encode ( opj_tcd_v2_t *p_tcd )
3175 {
3176         OPJ_UINT32 compno;
3177         opj_tcd_tilecomp_v2_t * l_tile_comp = 00;
3178         opj_tccp_t * l_tccp = 00;
3179         opj_image_comp_t * l_img_comp = 00;
3180         opj_tcp_v2_t * l_tcp = 00;
3181         opj_tcd_tile_v2_t * l_tile;
3182         OPJ_UINT32 l_nb_elem,i;
3183         OPJ_INT32 * l_current_ptr;
3184
3185         l_tile = p_tcd->tcd_image->tiles;
3186         l_tile_comp = l_tile->comps;
3187         l_tcp = p_tcd->tcp;
3188         l_tccp = p_tcd->tcp->tccps;
3189         l_img_comp = p_tcd->image->comps;
3190
3191         for (compno = 0; compno < l_tile->numcomps; compno++) {
3192                 l_current_ptr = l_tile_comp->data;
3193                 l_nb_elem = (l_tile_comp->x1 - l_tile_comp->x0) * (l_tile_comp->y1 - l_tile_comp->y0);
3194
3195                 if (l_tccp->qmfbid == 1) {
3196                         for     (i = 0; i < l_nb_elem; ++i) {
3197                                 *l_current_ptr -= l_tccp->m_dc_level_shift ;
3198                                 ++l_current_ptr;
3199                         }
3200                 }
3201                 else {
3202                         for (i = 0; i < l_nb_elem; ++i) {
3203                                 *l_current_ptr = (*l_current_ptr - l_tccp->m_dc_level_shift) << 11 ;
3204                                 ++l_current_ptr;
3205                         }
3206                 }
3207
3208                 ++l_img_comp;
3209                 ++l_tccp;
3210                 ++l_tile_comp;
3211         }
3212
3213         return OPJ_TRUE;
3214 }
3215
3216 opj_bool tcd_mct_encode ( opj_tcd_v2_t *p_tcd )
3217 {
3218         opj_tcd_tile_v2_t * l_tile = p_tcd->tcd_image->tiles;
3219         opj_tcd_tilecomp_v2_t * l_tile_comp = p_tcd->tcd_image->tiles->comps;
3220         OPJ_UINT32 samples = (l_tile_comp->x1 - l_tile_comp->x0) * (l_tile_comp->y1 - l_tile_comp->y0);
3221         OPJ_UINT32 i;
3222         OPJ_BYTE ** l_data = 00;
3223         opj_tcp_v2_t * l_tcp = p_tcd->tcp;
3224
3225         if(!p_tcd->tcp->mct) {
3226                 return OPJ_TRUE;
3227         }
3228
3229         if (p_tcd->tcp->mct == 2) {
3230                 if (! p_tcd->tcp->m_mct_coding_matrix) {
3231                         return OPJ_TRUE;
3232                 }
3233
3234         l_data = (OPJ_BYTE **) opj_malloc(l_tile->numcomps*sizeof(OPJ_BYTE*));
3235                 if (! l_data) {
3236                         return OPJ_FALSE;
3237                 }
3238
3239                 for (i=0;i<l_tile->numcomps;++i) {
3240                         l_data[i] = (OPJ_BYTE*) l_tile_comp->data;
3241                         ++l_tile_comp;
3242                 }
3243
3244                 if (! mct_encode_custom(/* MCT data */
3245                                         (OPJ_BYTE*) p_tcd->tcp->m_mct_coding_matrix,
3246                                         /* size of components */
3247                                         samples,
3248                                         /* components */
3249                                         l_data,
3250                                         /* nb of components (i.e. size of pData) */
3251                                         l_tile->numcomps,
3252                                         /* tells if the data is signed */
3253                                         p_tcd->image->comps->sgnd) )
3254                 {
3255             opj_free(l_data);
3256                         return OPJ_FALSE;
3257                 }
3258
3259                 opj_free(l_data);
3260         }
3261         else if (l_tcp->tccps->qmfbid == 0) {
3262                 mct_encode_real(l_tile->comps[0].data, l_tile->comps[1].data, l_tile->comps[2].data, samples);
3263         }
3264         else {
3265                 mct_encode(l_tile->comps[0].data, l_tile->comps[1].data, l_tile->comps[2].data, samples);
3266         }
3267
3268         return OPJ_TRUE;
3269 }
3270
3271 opj_bool tcd_dwt_encode ( opj_tcd_v2_t *p_tcd )
3272 {
3273         opj_tcd_tile_v2_t * l_tile = p_tcd->tcd_image->tiles;
3274         opj_tcd_tilecomp_v2_t * l_tile_comp = p_tcd->tcd_image->tiles->comps;
3275         opj_tccp_t * l_tccp = p_tcd->tcp->tccps;
3276         OPJ_UINT32 compno;
3277
3278         for (compno = 0; compno < l_tile->numcomps; ++compno) {
3279                 if (l_tccp->qmfbid == 1) {
3280                         if (! dwt_encode_v2(l_tile_comp)) {
3281                                 return OPJ_FALSE;
3282                         }
3283                 }
3284                 else if (l_tccp->qmfbid == 0) {
3285                         if (! dwt_encode_real_v2(l_tile_comp)) {
3286                                 return OPJ_FALSE;
3287                         }
3288                 }
3289
3290                 ++l_tile_comp;
3291                 ++l_tccp;
3292         }
3293
3294         return OPJ_TRUE;
3295 }
3296
3297 opj_bool tcd_t1_encode ( opj_tcd_v2_t *p_tcd )
3298 {
3299         opj_t1_t * l_t1;
3300         const OPJ_FLOAT64 * l_mct_norms;
3301         opj_tcp_v2_t * l_tcp = p_tcd->tcp;
3302
3303         l_t1 = t1_create_v2();
3304         if (l_t1 == 00) {
3305                 return OPJ_FALSE;
3306         }
3307
3308         if (l_tcp->mct == 1) {
3309                 /* irreversible encoding */
3310                 if (l_tcp->tccps->qmfbid == 0) {
3311                         l_mct_norms = get_mct_norms_real();
3312                 }
3313                 else {
3314                         l_mct_norms = get_mct_norms();
3315                 }
3316         }
3317         else {
3318                 l_mct_norms = (const OPJ_FLOAT64 *) (l_tcp->mct_norms);
3319         }
3320
3321         if (! t1_encode_cblks_v2(l_t1, p_tcd->tcd_image->tiles , l_tcp, l_mct_norms)) {
3322         t1_destroy_v2(l_t1);
3323                 return OPJ_FALSE;
3324         }
3325
3326         t1_destroy_v2(l_t1);
3327
3328         return OPJ_TRUE;
3329 }
3330
3331 opj_bool tcd_t2_encode (opj_tcd_v2_t *p_tcd,
3332                                                 OPJ_BYTE * p_dest_data,
3333                                                 OPJ_UINT32 * p_data_written,
3334                                                 OPJ_UINT32 p_max_dest_size,
3335                                                 opj_codestream_info_t *p_cstr_info )
3336 {
3337         opj_t2_v2_t * l_t2;
3338
3339         l_t2 = t2_create_v2(p_tcd->image, p_tcd->cp);
3340         if (l_t2 == 00) {
3341                 return OPJ_FALSE;
3342         }
3343
3344         if (! t2_encode_packets_v2(
3345                                         l_t2,
3346                                         p_tcd->tcd_tileno,
3347                                         p_tcd->tcd_image->tiles,
3348                                         p_tcd->tcp->numlayers,
3349                                         p_dest_data,
3350                                         p_data_written,
3351                                         p_max_dest_size,
3352                                         p_cstr_info,
3353                                         p_tcd->tp_num,
3354                                         p_tcd->tp_pos,
3355                                         p_tcd->cur_pino,
3356                                         FINAL_PASS))
3357         {
3358                 t2_destroy_v2(l_t2);
3359                 return OPJ_FALSE;
3360         }
3361
3362         t2_destroy_v2(l_t2);
3363
3364         /*---------------CLEAN-------------------*/
3365         return OPJ_TRUE;
3366 }
3367
3368
3369 opj_bool tcd_rate_allocate_encode(      opj_tcd_v2_t *p_tcd,
3370                                                                         OPJ_BYTE * p_dest_data,
3371                                                                         OPJ_UINT32 p_max_dest_size,
3372                                                                         opj_codestream_info_t *p_cstr_info )
3373 {
3374         opj_cp_v2_t * l_cp = p_tcd->cp;
3375         OPJ_UINT32 l_nb_written = 0;
3376
3377         if (p_cstr_info)  {
3378                 p_cstr_info->index_write = 0;
3379         }
3380
3381         if (l_cp->m_specific_param.m_enc.m_disto_alloc|| l_cp->m_specific_param.m_enc.m_fixed_quality)  {
3382                 /* fixed_quality */
3383                 /* Normal Rate/distortion allocation */
3384                 if (! tcd_rateallocate_v2(p_tcd, p_dest_data,&l_nb_written, p_max_dest_size, p_cstr_info)) {
3385                         return OPJ_FALSE;
3386                 }
3387         }
3388         else {
3389                 /* Fixed layer allocation */
3390                 tcd_rateallocate_fixed_v2(p_tcd);
3391         }
3392
3393         return OPJ_TRUE;
3394 }
3395
3396
3397 opj_bool tcd_copy_tile_data (   opj_tcd_v2_t *p_tcd,
3398                                                                 OPJ_BYTE * p_src,
3399                                                                 OPJ_UINT32 p_src_length )
3400 {
3401         OPJ_UINT32 i,j,l_data_size = 0;
3402         opj_image_comp_t * l_img_comp = 00;
3403         opj_tcd_tilecomp_v2_t * l_tilec = 00;
3404         OPJ_UINT32 l_size_comp, l_remaining;
3405         OPJ_UINT32 l_nb_elem;
3406
3407         l_data_size = tcd_get_encoded_tile_size(p_tcd);
3408         if (l_data_size != p_src_length) {
3409                 return OPJ_FALSE;
3410         }
3411
3412         l_tilec = p_tcd->tcd_image->tiles->comps;
3413         l_img_comp = p_tcd->image->comps;
3414         for (i=0;i<p_tcd->image->numcomps;++i) {
3415                 l_size_comp = l_img_comp->prec >> 3; /*(/ 8)*/
3416                 l_remaining = l_img_comp->prec & 7;  /* (%8) */
3417                 l_nb_elem = (l_tilec->x1 - l_tilec->x0) * (l_tilec->y1 - l_tilec->y0);
3418
3419                 if (l_remaining) {
3420                         ++l_size_comp;
3421                 }
3422
3423                 if (l_size_comp == 3) {
3424                         l_size_comp = 4;
3425                 }
3426
3427                 switch (l_size_comp) {
3428                         case 1:
3429                                 {
3430                                         OPJ_CHAR * l_src_ptr = (OPJ_CHAR *) p_src;
3431                                         OPJ_INT32 * l_dest_ptr = l_tilec->data;
3432
3433                                         if (l_img_comp->sgnd) {
3434                                                 for (j=0;j<l_nb_elem;++j) {
3435                                                         *(l_dest_ptr++) = (OPJ_INT32) (*(l_src_ptr++));
3436                                                 }
3437                                         }
3438                                         else {
3439                                                 for (j=0;j<l_nb_elem;++j) {
3440                                                         *(l_dest_ptr++) = (*(l_src_ptr++))&0xff;
3441                                                 }
3442                                         }
3443
3444                                         p_src = (OPJ_BYTE*) l_src_ptr;
3445                                 }
3446                                 break;
3447                         case 2:
3448                                 {
3449                                         OPJ_INT32 * l_dest_ptr = l_tilec->data;
3450                                         OPJ_INT16 * l_src_ptr = (OPJ_INT16 *) p_src;
3451
3452                                         if (l_img_comp->sgnd) {
3453                                                 for (j=0;j<l_nb_elem;++j) {
3454                                                         *(l_dest_ptr++) = (OPJ_INT32) (*(l_src_ptr++));
3455                                                 }
3456                                         }
3457                                         else {
3458                                                 for (j=0;j<l_nb_elem;++j) {
3459                                                         *(l_dest_ptr++) = (*(l_src_ptr++))&0xffff;
3460                                                 }
3461                                         }
3462
3463                                         p_src = (OPJ_BYTE*) l_src_ptr;
3464                                 }
3465                                 break;
3466                         case 4:
3467                                 {
3468                                         OPJ_INT32 * l_src_ptr = (OPJ_INT32 *) p_src;
3469                                         OPJ_INT32 * l_dest_ptr = l_tilec->data;
3470
3471                                         for (j=0;j<l_nb_elem;++j) {
3472                                                 *(l_dest_ptr++) = (OPJ_INT32) (*(l_src_ptr++));
3473                                         }
3474
3475                                         p_src = (OPJ_BYTE*) l_src_ptr;
3476                                 }
3477                                 break;
3478                 }
3479
3480                 ++l_img_comp;
3481                 ++l_tilec;
3482         }
3483
3484         return OPJ_TRUE;
3485 }