[trunk] removed warnings when building tcd.c (Update issue 442)
[openjpeg.git] / src / lib / openjp2 / tcd.c
1 /*
2  * The copyright in this software is being made available under the 2-clauses 
3  * BSD License, included below. This software may be subject to other third 
4  * party and contributor rights, including patent rights, and no such rights
5  * are granted under this license.
6  *
7  * Copyright (c) 2002-2014, Universite catholique de Louvain (UCL), Belgium
8  * Copyright (c) 2002-2014, Professor Benoit Macq
9  * Copyright (c) 2001-2003, David Janssens
10  * Copyright (c) 2002-2003, Yannick Verschueren
11  * Copyright (c) 2003-2007, Francois-Olivier Devaux 
12  * Copyright (c) 2003-2014, Antonin Descampe
13  * Copyright (c) 2005, Herve Drolon, FreeImage Team
14  * Copyright (c) 2006-2007, Parvatha Elangovan
15  * Copyright (c) 2008, 2011-2012, Centre National d'Etudes Spatiales (CNES), FR 
16  * Copyright (c) 2012, CS Systemes d'Information, France
17  * All rights reserved.
18  *
19  * Redistribution and use in source and binary forms, with or without
20  * modification, are permitted provided that the following conditions
21  * are met:
22  * 1. Redistributions of source code must retain the above copyright
23  *    notice, this list of conditions and the following disclaimer.
24  * 2. Redistributions in binary form must reproduce the above copyright
25  *    notice, this list of conditions and the following disclaimer in the
26  *    documentation and/or other materials provided with the distribution.
27  *
28  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
29  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31  * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
32  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
33  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
34  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
35  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
36  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
37  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
38  * POSSIBILITY OF SUCH DAMAGE.
39  */
40
41 #include "opj_includes.h"
42
43 /* ----------------------------------------------------------------------- */
44
45 /* TODO MSD: */
46 #ifdef TODO_MSD 
47 void tcd_dump(FILE *fd, opj_tcd_t *tcd, opj_tcd_image_t * img) {
48         int tileno, compno, resno, bandno, precno;/*, cblkno;*/
49
50         fprintf(fd, "image {\n");
51         fprintf(fd, "  tw=%d, th=%d x0=%d x1=%d y0=%d y1=%d\n", 
52                 img->tw, img->th, tcd->image->x0, tcd->image->x1, tcd->image->y0, tcd->image->y1);
53
54         for (tileno = 0; tileno < img->th * img->tw; tileno++) {
55                 opj_tcd_tile_t *tile = &tcd->tcd_image->tiles[tileno];
56                 fprintf(fd, "  tile {\n");
57                 fprintf(fd, "    x0=%d, y0=%d, x1=%d, y1=%d, numcomps=%d\n",
58                         tile->x0, tile->y0, tile->x1, tile->y1, tile->numcomps);
59                 for (compno = 0; compno < tile->numcomps; compno++) {
60                         opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
61                         fprintf(fd, "    tilec {\n");
62                         fprintf(fd,
63                                 "      x0=%d, y0=%d, x1=%d, y1=%d, numresolutions=%d\n",
64                                 tilec->x0, tilec->y0, tilec->x1, tilec->y1, tilec->numresolutions);
65                         for (resno = 0; resno < tilec->numresolutions; resno++) {
66                                 opj_tcd_resolution_t *res = &tilec->resolutions[resno];
67                                 fprintf(fd, "\n   res {\n");
68                                 fprintf(fd,
69                                         "          x0=%d, y0=%d, x1=%d, y1=%d, pw=%d, ph=%d, numbands=%d\n",
70                                         res->x0, res->y0, res->x1, res->y1, res->pw, res->ph, res->numbands);
71                                 for (bandno = 0; bandno < res->numbands; bandno++) {
72                                         opj_tcd_band_t *band = &res->bands[bandno];
73                                         fprintf(fd, "        band {\n");
74                                         fprintf(fd,
75                                                 "          x0=%d, y0=%d, x1=%d, y1=%d, stepsize=%f, numbps=%d\n",
76                                                 band->x0, band->y0, band->x1, band->y1, band->stepsize, band->numbps);
77                                         for (precno = 0; precno < res->pw * res->ph; precno++) {
78                                                 opj_tcd_precinct_t *prec = &band->precincts[precno];
79                                                 fprintf(fd, "          prec {\n");
80                                                 fprintf(fd,
81                                                         "            x0=%d, y0=%d, x1=%d, y1=%d, cw=%d, ch=%d\n",
82                                                         prec->x0, prec->y0, prec->x1, prec->y1, prec->cw, prec->ch);
83                                                 /*
84                                                 for (cblkno = 0; cblkno < prec->cw * prec->ch; cblkno++) {
85                                                         opj_tcd_cblk_t *cblk = &prec->cblks[cblkno];
86                                                         fprintf(fd, "            cblk {\n");
87                                                         fprintf(fd,
88                                                                 "              x0=%d, y0=%d, x1=%d, y1=%d\n",
89                                                                 cblk->x0, cblk->y0, cblk->x1, cblk->y1);
90                                                         fprintf(fd, "            }\n");
91                                                 }
92                                                 */
93                                                 fprintf(fd, "          }\n");
94                                         }
95                                         fprintf(fd, "        }\n");
96                                 }
97                                 fprintf(fd, "      }\n");
98                         }
99                         fprintf(fd, "    }\n");
100                 }
101                 fprintf(fd, "  }\n");
102         }
103         fprintf(fd, "}\n");
104 }
105 #endif
106
107 /**
108  * Initializes tile coding/decoding
109  */
110 static INLINE OPJ_BOOL opj_tcd_init_tile(opj_tcd_t *p_tcd, OPJ_UINT32 p_tile_no, OPJ_BOOL isEncoder, OPJ_FLOAT32 fraction, OPJ_SIZE_T sizeof_block);
111
112 /**
113 * Allocates memory for a decoding code block.
114 */
115 static OPJ_BOOL opj_tcd_code_block_dec_allocate (opj_tcd_cblk_dec_t * p_code_block);
116
117 /**
118  * Deallocates the decoding data of the given precinct.
119  */
120 static void opj_tcd_code_block_dec_deallocate (opj_tcd_precinct_t * p_precinct);
121
122 /**
123  * Allocates memory for an encoding code block (but not data).
124  */
125 static OPJ_BOOL opj_tcd_code_block_enc_allocate (opj_tcd_cblk_enc_t * p_code_block);
126
127 /**
128  * Allocates data for an encoding code block
129  */
130 static OPJ_BOOL opj_tcd_code_block_enc_allocate_data (opj_tcd_cblk_enc_t * p_code_block);
131
132 /**
133  * Deallocates the encoding data of the given precinct.
134  */
135 static void opj_tcd_code_block_enc_deallocate (opj_tcd_precinct_t * p_precinct);
136
137
138 /**
139 Free the memory allocated for encoding
140 @param tcd TCD handle
141 */
142 static void opj_tcd_free_tile(opj_tcd_t *tcd);
143
144
145 static OPJ_BOOL opj_tcd_t2_decode ( opj_tcd_t *p_tcd,
146                                     OPJ_BYTE * p_src_data,
147                                     OPJ_UINT32 * p_data_read,
148                                     OPJ_UINT32 p_max_src_size,
149                                     opj_codestream_index_t *p_cstr_index );
150
151 static OPJ_BOOL opj_tcd_t1_decode (opj_tcd_t *p_tcd);
152
153 static OPJ_BOOL opj_tcd_dwt_decode (opj_tcd_t *p_tcd);
154
155 static OPJ_BOOL opj_tcd_mct_decode (opj_tcd_t *p_tcd);
156
157 static OPJ_BOOL opj_tcd_dc_level_shift_decode (opj_tcd_t *p_tcd);
158
159
160 static OPJ_BOOL opj_tcd_dc_level_shift_encode ( opj_tcd_t *p_tcd );
161
162 static OPJ_BOOL opj_tcd_mct_encode ( opj_tcd_t *p_tcd );
163
164 static OPJ_BOOL opj_tcd_dwt_encode ( opj_tcd_t *p_tcd );
165
166 static OPJ_BOOL opj_tcd_t1_encode ( opj_tcd_t *p_tcd );
167
168 static OPJ_BOOL opj_tcd_t2_encode (     opj_tcd_t *p_tcd,
169                                                                     OPJ_BYTE * p_dest_data,
170                                                                     OPJ_UINT32 * p_data_written,
171                                                                     OPJ_UINT32 p_max_dest_size,
172                                                                     opj_codestream_info_t *p_cstr_info );
173
174 static OPJ_BOOL opj_tcd_rate_allocate_encode(   opj_tcd_t *p_tcd,
175                                                                                         OPJ_BYTE * p_dest_data,
176                                                                                         OPJ_UINT32 p_max_dest_size,
177                                                                                         opj_codestream_info_t *p_cstr_info );
178
179 /* ----------------------------------------------------------------------- */
180
181 /**
182 Create a new TCD handle
183 */
184 opj_tcd_t* opj_tcd_create(OPJ_BOOL p_is_decoder)
185 {
186         opj_tcd_t *l_tcd = 00;
187
188         /* create the tcd structure */
189         l_tcd = (opj_tcd_t*) opj_calloc(1,sizeof(opj_tcd_t));
190         if (!l_tcd) {
191                 return 00;
192         }
193
194         l_tcd->m_is_decoder = p_is_decoder ? 1 : 0;
195
196         l_tcd->tcd_image = (opj_tcd_image_t*)opj_calloc(1,sizeof(opj_tcd_image_t));
197         if (!l_tcd->tcd_image) {
198                 opj_free(l_tcd);
199                 return 00;
200         }
201
202         return l_tcd;
203 }
204
205
206 /* ----------------------------------------------------------------------- */
207
208 void opj_tcd_rateallocate_fixed(opj_tcd_t *tcd) {
209         OPJ_UINT32 layno;
210
211         for (layno = 0; layno < tcd->tcp->numlayers; layno++) {
212                 opj_tcd_makelayer_fixed(tcd, layno, 1);
213         }
214 }
215
216
217 void opj_tcd_makelayer( opj_tcd_t *tcd,
218                                                 OPJ_UINT32 layno,
219                                                 OPJ_FLOAT64 thresh,
220                                                 OPJ_UINT32 final)
221 {
222         OPJ_UINT32 compno, resno, bandno, precno, cblkno;
223         OPJ_UINT32 passno;
224
225         opj_tcd_tile_t *tcd_tile = tcd->tcd_image->tiles;
226
227         tcd_tile->distolayer[layno] = 0;        /* fixed_quality */
228
229         for (compno = 0; compno < tcd_tile->numcomps; compno++) {
230                 opj_tcd_tilecomp_t *tilec = &tcd_tile->comps[compno];
231
232                 for (resno = 0; resno < tilec->numresolutions; resno++) {
233                         opj_tcd_resolution_t *res = &tilec->resolutions[resno];
234
235                         for (bandno = 0; bandno < res->numbands; bandno++) {
236                                 opj_tcd_band_t *band = &res->bands[bandno];
237
238                                 for (precno = 0; precno < res->pw * res->ph; precno++) {
239                                         opj_tcd_precinct_t *prc = &band->precincts[precno];
240
241                                         for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
242                                                 opj_tcd_cblk_enc_t *cblk = &prc->cblks.enc[cblkno];
243                                                 opj_tcd_layer_t *layer = &cblk->layers[layno];
244                                                 OPJ_UINT32 n;
245
246                                                 if (layno == 0) {
247                                                         cblk->numpassesinlayers = 0;
248                                                 }
249
250                                                 n = cblk->numpassesinlayers;
251
252                                                 for (passno = cblk->numpassesinlayers; passno < cblk->totalpasses; passno++) {
253                                                         OPJ_UINT32 dr;
254                                                         OPJ_FLOAT64 dd;
255                                                         opj_tcd_pass_t *pass = &cblk->passes[passno];
256
257                                                         if (n == 0) {
258                                                                 dr = pass->rate;
259                                                                 dd = pass->distortiondec;
260                                                         } else {
261                                                                 dr = pass->rate - cblk->passes[n - 1].rate;
262                                                                 dd = pass->distortiondec - cblk->passes[n - 1].distortiondec;
263                                                         }
264
265                                                         if (!dr) {
266                                                                 if (dd != 0)
267                                                                         n = passno + 1;
268                                                                 continue;
269                                                         }
270                                                         if (dd / dr >= thresh)
271                                                                 n = passno + 1;
272                                                 }
273
274                                                 layer->numpasses = n - cblk->numpassesinlayers;
275
276                                                 if (!layer->numpasses) {
277                                                         layer->disto = 0;
278                                                         continue;
279                                                 }
280
281                                                 if (cblk->numpassesinlayers == 0) {
282                                                         layer->len = cblk->passes[n - 1].rate;
283                                                         layer->data = cblk->data;
284                                                         layer->disto = cblk->passes[n - 1].distortiondec;
285                                                 } else {
286                                                         layer->len = cblk->passes[n - 1].rate - cblk->passes[cblk->numpassesinlayers - 1].rate;
287                                                         layer->data = cblk->data + cblk->passes[cblk->numpassesinlayers - 1].rate;
288                                                         layer->disto = cblk->passes[n - 1].distortiondec - cblk->passes[cblk->numpassesinlayers - 1].distortiondec;
289                                                 }
290
291                                                 tcd_tile->distolayer[layno] += layer->disto;    /* fixed_quality */
292
293                                                 if (final)
294                                                         cblk->numpassesinlayers = n;
295                                         }
296                                 }
297                         }
298                 }
299         }
300 }
301
302 void opj_tcd_makelayer_fixed(opj_tcd_t *tcd, OPJ_UINT32 layno, OPJ_UINT32 final) {
303         OPJ_UINT32 compno, resno, bandno, precno, cblkno;
304         OPJ_INT32 value;                        /*, matrice[tcd_tcp->numlayers][tcd_tile->comps[0].numresolutions][3]; */
305         OPJ_INT32 matrice[10][10][3];
306         OPJ_UINT32 i, j, k;
307
308         opj_cp_t *cp = tcd->cp;
309         opj_tcd_tile_t *tcd_tile = tcd->tcd_image->tiles;
310         opj_tcp_t *tcd_tcp = tcd->tcp;
311
312         for (compno = 0; compno < tcd_tile->numcomps; compno++) {
313                 opj_tcd_tilecomp_t *tilec = &tcd_tile->comps[compno];
314
315                 for (i = 0; i < tcd_tcp->numlayers; i++) {
316                         for (j = 0; j < tilec->numresolutions; j++) {
317                                 for (k = 0; k < 3; k++) {
318                                         matrice[i][j][k] =
319                                                 (OPJ_INT32) ((OPJ_FLOAT32)cp->m_specific_param.m_enc.m_matrice[i * tilec->numresolutions * 3 + j * 3 + k]
320                                                 * (OPJ_FLOAT32) (tcd->image->comps[compno].prec / 16.0));
321                                 }
322                         }
323                 }
324
325                 for (resno = 0; resno < tilec->numresolutions; resno++) {
326                         opj_tcd_resolution_t *res = &tilec->resolutions[resno];
327
328                         for (bandno = 0; bandno < res->numbands; bandno++) {
329                                 opj_tcd_band_t *band = &res->bands[bandno];
330
331                                 for (precno = 0; precno < res->pw * res->ph; precno++) {
332                                         opj_tcd_precinct_t *prc = &band->precincts[precno];
333
334                                         for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
335                                                 opj_tcd_cblk_enc_t *cblk = &prc->cblks.enc[cblkno];
336                                                 opj_tcd_layer_t *layer = &cblk->layers[layno];
337                                                 OPJ_UINT32 n;
338                                                 OPJ_INT32 imsb = (OPJ_INT32)(tcd->image->comps[compno].prec - cblk->numbps); /* number of bit-plan equal to zero */
339
340                                                 /* Correction of the matrix of coefficient to include the IMSB information */
341                                                 if (layno == 0) {
342                                                         value = matrice[layno][resno][bandno];
343                                                         if (imsb >= value) {
344                                                                 value = 0;
345                                                         } else {
346                                                                 value -= imsb;
347                                                         }
348                                                 } else {
349                                                         value = matrice[layno][resno][bandno] - matrice[layno - 1][resno][bandno];
350                                                         if (imsb >= matrice[layno - 1][resno][bandno]) {
351                                                                 value -= (imsb - matrice[layno - 1][resno][bandno]);
352                                                                 if (value < 0) {
353                                                                         value = 0;
354                                                                 }
355                                                         }
356                                                 }
357
358                                                 if (layno == 0) {
359                                                         cblk->numpassesinlayers = 0;
360                                                 }
361
362                                                 n = cblk->numpassesinlayers;
363                                                 if (cblk->numpassesinlayers == 0) {
364                                                         if (value != 0) {
365                                                                 n = 3 * (OPJ_UINT32)value - 2 + cblk->numpassesinlayers;
366                                                         } else {
367                                                                 n = cblk->numpassesinlayers;
368                                                         }
369                                                 } else {
370                                                         n = 3 * (OPJ_UINT32)value + cblk->numpassesinlayers;
371                                                 }
372
373                                                 layer->numpasses = n - cblk->numpassesinlayers;
374
375                                                 if (!layer->numpasses)
376                                                         continue;
377
378                                                 if (cblk->numpassesinlayers == 0) {
379                                                         layer->len = cblk->passes[n - 1].rate;
380                                                         layer->data = cblk->data;
381                                                 } else {
382                                                         layer->len = cblk->passes[n - 1].rate - cblk->passes[cblk->numpassesinlayers - 1].rate;
383                                                         layer->data = cblk->data + cblk->passes[cblk->numpassesinlayers - 1].rate;
384                                                 }
385
386                                                 if (final)
387                                                         cblk->numpassesinlayers = n;
388                                         }
389                                 }
390                         }
391                 }
392         }
393 }
394
395 OPJ_BOOL opj_tcd_rateallocate(  opj_tcd_t *tcd,
396                                                                 OPJ_BYTE *dest,
397                                                                 OPJ_UINT32 * p_data_written,
398                                                                 OPJ_UINT32 len,
399                                                                 opj_codestream_info_t *cstr_info)
400 {
401         OPJ_UINT32 compno, resno, bandno, precno, cblkno, layno;
402         OPJ_UINT32 passno;
403         OPJ_FLOAT64 min, max;
404         OPJ_FLOAT64 cumdisto[100];      /* fixed_quality */
405         const OPJ_FLOAT64 K = 1;                /* 1.1; fixed_quality */
406         OPJ_FLOAT64 maxSE = 0;
407
408         opj_cp_t *cp = tcd->cp;
409         opj_tcd_tile_t *tcd_tile = tcd->tcd_image->tiles;
410         opj_tcp_t *tcd_tcp = tcd->tcp;
411
412         min = DBL_MAX;
413         max = 0;
414
415         tcd_tile->numpix = 0;           /* fixed_quality */
416
417         for (compno = 0; compno < tcd_tile->numcomps; compno++) {
418                 opj_tcd_tilecomp_t *tilec = &tcd_tile->comps[compno];
419                 tilec->numpix = 0;
420
421                 for (resno = 0; resno < tilec->numresolutions; resno++) {
422                         opj_tcd_resolution_t *res = &tilec->resolutions[resno];
423
424                         for (bandno = 0; bandno < res->numbands; bandno++) {
425                                 opj_tcd_band_t *band = &res->bands[bandno];
426
427                                 for (precno = 0; precno < res->pw * res->ph; precno++) {
428                                         opj_tcd_precinct_t *prc = &band->precincts[precno];
429
430                                         for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
431                                                 opj_tcd_cblk_enc_t *cblk = &prc->cblks.enc[cblkno];
432
433                                                 for (passno = 0; passno < cblk->totalpasses; passno++) {
434                                                         opj_tcd_pass_t *pass = &cblk->passes[passno];
435                                                         OPJ_INT32 dr;
436                                                         OPJ_FLOAT64 dd, rdslope;
437
438                                                         if (passno == 0) {
439                                                                 dr = (OPJ_INT32)pass->rate;
440                                                                 dd = pass->distortiondec;
441                                                         } else {
442                                                                 dr = (OPJ_INT32)(pass->rate - cblk->passes[passno - 1].rate);
443                                                                 dd = pass->distortiondec - cblk->passes[passno - 1].distortiondec;
444                                                         }
445
446                                                         if (dr == 0) {
447                                                                 continue;
448                                                         }
449
450                                                         rdslope = dd / dr;
451                                                         if (rdslope < min) {
452                                                                 min = rdslope;
453                                                         }
454
455                                                         if (rdslope > max) {
456                                                                 max = rdslope;
457                                                         }
458                                                 } /* passno */
459
460                                                 /* fixed_quality */
461                                                 tcd_tile->numpix += ((cblk->x1 - cblk->x0) * (cblk->y1 - cblk->y0));
462                                                 tilec->numpix += ((cblk->x1 - cblk->x0) * (cblk->y1 - cblk->y0));
463                                         } /* cbklno */
464                                 } /* precno */
465                         } /* bandno */
466                 } /* resno */
467
468                 maxSE += (((OPJ_FLOAT64)(1 << tcd->image->comps[compno].prec) - 1.0)
469                         * ((OPJ_FLOAT64)(1 << tcd->image->comps[compno].prec) -1.0))
470                         * ((OPJ_FLOAT64)(tilec->numpix));
471         } /* compno */
472
473         /* index file */
474         if(cstr_info) {
475                 opj_tile_info_t *tile_info = &cstr_info->tile[tcd->tcd_tileno];
476                 tile_info->numpix = tcd_tile->numpix;
477                 tile_info->distotile = tcd_tile->distotile;
478                 tile_info->thresh = (OPJ_FLOAT64 *) opj_malloc(tcd_tcp->numlayers * sizeof(OPJ_FLOAT64));
479                 if (!tile_info->thresh) {
480                         /* FIXME event manager error callback */
481                         return OPJ_FALSE;
482                 }
483         }
484
485         for (layno = 0; layno < tcd_tcp->numlayers; layno++) {
486                 OPJ_FLOAT64 lo = min;
487                 OPJ_FLOAT64 hi = max;
488                 OPJ_BOOL success = OPJ_FALSE;
489                 OPJ_UINT32 maxlen = tcd_tcp->rates[layno] ? opj_uint_min(((OPJ_UINT32) ceil(tcd_tcp->rates[layno])), len) : len;
490                 OPJ_FLOAT64 goodthresh = 0;
491                 OPJ_FLOAT64 stable_thresh = 0;
492                 OPJ_UINT32 i;
493                 OPJ_FLOAT64 distotarget;                /* fixed_quality */
494
495                 /* fixed_quality */
496                 distotarget = tcd_tile->distotile - ((K * maxSE) / pow((OPJ_FLOAT32)10, tcd_tcp->distoratio[layno] / 10));
497
498                 /* Don't try to find an optimal threshold but rather take everything not included yet, if
499                   -r xx,yy,zz,0   (disto_alloc == 1 and rates == 0)
500                   -q xx,yy,zz,0   (fixed_quality == 1 and distoratio == 0)
501                   ==> possible to have some lossy layers and the last layer for sure lossless */
502                 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))) {
503                         opj_t2_t*t2 = opj_t2_create(tcd->image, cp);
504                         OPJ_FLOAT64 thresh = 0;
505
506                         if (t2 == 00) {
507                                 return OPJ_FALSE;
508                         }
509
510                         for     (i = 0; i < 128; ++i) {
511                                 OPJ_FLOAT64 distoachieved = 0;  /* fixed_quality */
512
513                                 thresh = (lo + hi) / 2;
514
515                                 opj_tcd_makelayer(tcd, layno, thresh, 0);
516
517                                 if (cp->m_specific_param.m_enc.m_fixed_quality) {       /* fixed_quality */
518                                         if(OPJ_IS_CINEMA(cp->rsiz)){
519                                                 if (! opj_t2_encode_packets(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)) {
520
521                                                         lo = thresh;
522                                                         continue;
523                                                 }
524                                                 else {
525                                                         distoachieved = layno == 0 ?
526                                                                         tcd_tile->distolayer[0] : cumdisto[layno - 1] + tcd_tile->distolayer[layno];
527
528                                                         if (distoachieved < distotarget) {
529                                                                 hi=thresh;
530                                                                 stable_thresh = thresh;
531                                                                 continue;
532                                                         }else{
533                                                                 lo=thresh;
534                                                         }
535                                                 }
536                                         }else{
537                                                 distoachieved = (layno == 0) ?
538                                                                 tcd_tile->distolayer[0] : (cumdisto[layno - 1] + tcd_tile->distolayer[layno]);
539
540                                                 if (distoachieved < distotarget) {
541                                                         hi = thresh;
542                                                         stable_thresh = thresh;
543                                                         continue;
544                                                 }
545                                                 lo = thresh;
546                                         }
547                                 } else {
548                                         if (! opj_t2_encode_packets(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))
549                                         {
550                                                 /* TODO: what to do with l ??? seek / tell ??? */
551                                                 /* opj_event_msg(tcd->cinfo, EVT_INFO, "rate alloc: len=%d, max=%d\n", l, maxlen); */
552                                                 lo = thresh;
553                                                 continue;
554                                         }
555
556                                         hi = thresh;
557                                         stable_thresh = thresh;
558                                 }
559                         }
560
561                         success = OPJ_TRUE;
562                         goodthresh = stable_thresh == 0? thresh : stable_thresh;
563
564                         opj_t2_destroy(t2);
565                 } else {
566                         success = OPJ_TRUE;
567                         goodthresh = min;
568                 }
569
570                 if (!success) {
571                         return OPJ_FALSE;
572                 }
573
574                 if(cstr_info) { /* Threshold for Marcela Index */
575                         cstr_info->tile[tcd->tcd_tileno].thresh[layno] = goodthresh;
576                 }
577
578                 opj_tcd_makelayer(tcd, layno, goodthresh, 1);
579
580                 /* fixed_quality */
581                 cumdisto[layno] = (layno == 0) ? tcd_tile->distolayer[0] : (cumdisto[layno - 1] + tcd_tile->distolayer[layno]);
582         }
583
584         return OPJ_TRUE;
585 }
586
587 OPJ_BOOL opj_tcd_init( opj_tcd_t *p_tcd,
588                                            opj_image_t * p_image,
589                                            opj_cp_t * p_cp )
590 {
591         p_tcd->image = p_image;
592         p_tcd->cp = p_cp;
593
594         p_tcd->tcd_image->tiles = (opj_tcd_tile_t *) opj_calloc(1,sizeof(opj_tcd_tile_t));
595         if (! p_tcd->tcd_image->tiles) {
596                 return OPJ_FALSE;
597         }
598
599         p_tcd->tcd_image->tiles->comps = (opj_tcd_tilecomp_t *) opj_calloc(p_image->numcomps,sizeof(opj_tcd_tilecomp_t));
600         if (! p_tcd->tcd_image->tiles->comps ) {
601                 return OPJ_FALSE;
602         }
603
604         p_tcd->tcd_image->tiles->numcomps = p_image->numcomps;
605         p_tcd->tp_pos = p_cp->m_specific_param.m_enc.m_tp_pos;
606
607         return OPJ_TRUE;
608 }
609
610 /**
611 Destroy a previously created TCD handle
612 */
613 void opj_tcd_destroy(opj_tcd_t *tcd) {
614         if (tcd) {
615                 opj_tcd_free_tile(tcd);
616
617                 if (tcd->tcd_image) {
618                         opj_free(tcd->tcd_image);
619                         tcd->tcd_image = 00;
620                 }
621                 opj_free(tcd);
622         }
623 }
624
625 OPJ_BOOL opj_alloc_tile_component_data(opj_tcd_tilecomp_t *l_tilec)
626 {
627         if ((l_tilec->data == 00) || ((l_tilec->data_size_needed > l_tilec->data_size) && (l_tilec->ownsData == OPJ_FALSE))) {
628                 l_tilec->data = (OPJ_INT32 *) opj_malloc(l_tilec->data_size_needed);
629                 if (! l_tilec->data ) {
630                         return OPJ_FALSE;
631                 }
632                 /*fprintf(stderr, "tAllocate data of tilec (int): %d x OPJ_UINT32n",l_data_size);*/
633                 l_tilec->data_size = l_tilec->data_size_needed;
634                 l_tilec->ownsData = OPJ_TRUE;
635         }
636         else if (l_tilec->data_size_needed > l_tilec->data_size) {
637                 OPJ_INT32 * new_data = (OPJ_INT32 *) opj_realloc(l_tilec->data, l_tilec->data_size_needed);
638                 /* opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to handle tile datan"); */
639                 /* fprintf(stderr, "Not enough memory to handle tile data"); */
640                 if (! new_data) {
641                         opj_free(l_tilec->data);
642                         l_tilec->data = NULL;
643                         l_tilec->data_size = 0;
644                         l_tilec->data_size_needed = 0;
645                         l_tilec->ownsData = OPJ_FALSE;
646                         return OPJ_FALSE;
647                 }
648                 l_tilec->data = new_data;
649                 /*fprintf(stderr, "tReallocate data of tilec (int): from %d to %d x OPJ_UINT32n", l_tilec->data_size, l_data_size);*/
650                 l_tilec->data_size = l_tilec->data_size_needed;
651                 l_tilec->ownsData = OPJ_TRUE;
652         }
653         return OPJ_TRUE;
654 }
655
656 /* ----------------------------------------------------------------------- */
657
658 static INLINE OPJ_BOOL opj_tcd_init_tile(opj_tcd_t *p_tcd, OPJ_UINT32 p_tile_no, OPJ_BOOL isEncoder, OPJ_FLOAT32 fraction, OPJ_SIZE_T sizeof_block)
659 {
660         OPJ_UINT32 (*l_gain_ptr)(OPJ_UINT32) = 00;
661         OPJ_UINT32 compno, resno, bandno, precno, cblkno;
662         opj_tcp_t * l_tcp = 00;
663         opj_cp_t * l_cp = 00;
664         opj_tcd_tile_t * l_tile = 00;
665         opj_tccp_t *l_tccp = 00;
666         opj_tcd_tilecomp_t *l_tilec = 00;
667         opj_image_comp_t * l_image_comp = 00;
668         opj_tcd_resolution_t *l_res = 00;
669         opj_tcd_band_t *l_band = 00;
670         opj_stepsize_t * l_step_size = 00;
671         opj_tcd_precinct_t *l_current_precinct = 00;
672         opj_image_t *l_image = 00;
673         OPJ_UINT32 p,q;
674         OPJ_UINT32 l_level_no;
675         OPJ_UINT32 l_pdx, l_pdy;
676         OPJ_UINT32 l_gain;
677         OPJ_INT32 l_x0b, l_y0b;
678         /* extent of precincts , top left, bottom right**/
679         OPJ_INT32 l_tl_prc_x_start, l_tl_prc_y_start, l_br_prc_x_end, l_br_prc_y_end;
680         /* number of precinct for a resolution */
681         OPJ_UINT32 l_nb_precincts;
682         /* room needed to store l_nb_precinct precinct for a resolution */
683         OPJ_UINT32 l_nb_precinct_size;
684         /* number of code blocks for a precinct*/
685         OPJ_UINT32 l_nb_code_blocks;
686         /* room needed to store l_nb_code_blocks code blocks for a precinct*/
687         OPJ_UINT32 l_nb_code_blocks_size;
688         /* size of data for a tile */
689         OPJ_UINT32 l_data_size;
690         
691         l_cp = p_tcd->cp;
692         l_tcp = &(l_cp->tcps[p_tile_no]);
693         l_tile = p_tcd->tcd_image->tiles;
694         l_tccp = l_tcp->tccps;
695         l_tilec = l_tile->comps;
696         l_image = p_tcd->image;
697         l_image_comp = p_tcd->image->comps;
698         
699         p = p_tile_no % l_cp->tw;       /* tile coordinates */
700         q = p_tile_no / l_cp->tw;
701         /*fprintf(stderr, "Tile coordinate = %d,%d\n", p, q);*/
702         
703         /* 4 borders of the tile rescale on the image if necessary */
704         l_tile->x0 = opj_int_max((OPJ_INT32)(l_cp->tx0 + p * l_cp->tdx), (OPJ_INT32)l_image->x0);
705         l_tile->y0 = opj_int_max((OPJ_INT32)(l_cp->ty0 + q * l_cp->tdy), (OPJ_INT32)l_image->y0);
706         l_tile->x1 = opj_int_min((OPJ_INT32)(l_cp->tx0 + (p + 1) * l_cp->tdx), (OPJ_INT32)l_image->x1);
707         l_tile->y1 = opj_int_min((OPJ_INT32)(l_cp->ty0 + (q + 1) * l_cp->tdy), (OPJ_INT32)l_image->y1);
708         /* testcase 1888.pdf.asan.35.988 */
709         if (l_tccp->numresolutions == 0) {
710                 fprintf(stderr, "tiles require at least one resolution\n");
711                 return OPJ_FALSE;
712         }
713         /*fprintf(stderr, "Tile border = %d,%d,%d,%d\n", l_tile->x0, l_tile->y0,l_tile->x1,l_tile->y1);*/
714         
715         /*tile->numcomps = image->numcomps; */
716         for (compno = 0; compno < l_tile->numcomps; ++compno) {
717                 /*fprintf(stderr, "compno = %d/%d\n", compno, l_tile->numcomps);*/
718                 l_image_comp->resno_decoded = 0;
719                 /* border of each l_tile component (global) */
720                 l_tilec->x0 = opj_int_ceildiv(l_tile->x0, (OPJ_INT32)l_image_comp->dx);
721                 l_tilec->y0 = opj_int_ceildiv(l_tile->y0, (OPJ_INT32)l_image_comp->dy);
722                 l_tilec->x1 = opj_int_ceildiv(l_tile->x1, (OPJ_INT32)l_image_comp->dx);
723                 l_tilec->y1 = opj_int_ceildiv(l_tile->y1, (OPJ_INT32)l_image_comp->dy);
724                 /*fprintf(stderr, "\tTile compo border = %d,%d,%d,%d\n", l_tilec->x0, l_tilec->y0,l_tilec->x1,l_tilec->y1);*/
725                 
726                 /* compute l_data_size with overflow check */
727                 l_data_size = (OPJ_UINT32)(l_tilec->x1 - l_tilec->x0);
728                 if ((((OPJ_UINT32)-1) / l_data_size) < (OPJ_UINT32)(l_tilec->y1 - l_tilec->y0)) {
729                         /* TODO event */
730                         return OPJ_FALSE;
731                 }
732                 l_data_size = l_data_size * (OPJ_UINT32)(l_tilec->y1 - l_tilec->y0);
733                 
734                 if ((((OPJ_UINT32)-1) / (OPJ_UINT32)sizeof(OPJ_UINT32)) < l_data_size) {
735                         /* TODO event */
736                         return OPJ_FALSE;
737                 }
738                 l_data_size = l_data_size * (OPJ_UINT32)sizeof(OPJ_UINT32);
739                 l_tilec->numresolutions = l_tccp->numresolutions;
740                 if (l_tccp->numresolutions < l_cp->m_specific_param.m_dec.m_reduce) {
741                         l_tilec->minimum_num_resolutions = 1;
742                 }
743                 else {
744                         l_tilec->minimum_num_resolutions = l_tccp->numresolutions - l_cp->m_specific_param.m_dec.m_reduce;
745                 }
746                 
747                 l_tilec->data_size_needed = l_data_size;
748                 if (p_tcd->m_is_decoder && !opj_alloc_tile_component_data(l_tilec)) {
749                         return OPJ_FALSE;
750                 }
751                 
752                 l_data_size = l_tilec->numresolutions * (OPJ_UINT32)sizeof(opj_tcd_resolution_t);
753                 
754                 if (l_tilec->resolutions == 00) {
755                         l_tilec->resolutions = (opj_tcd_resolution_t *) opj_malloc(l_data_size);
756                         if (! l_tilec->resolutions ) {
757                                 return OPJ_FALSE;
758                         }
759                         /*fprintf(stderr, "\tAllocate resolutions of tilec (opj_tcd_resolution_t): %d\n",l_data_size);*/
760                         l_tilec->resolutions_size = l_data_size;
761                         memset(l_tilec->resolutions,0,l_data_size);
762                 }
763                 else if (l_data_size > l_tilec->resolutions_size) {
764                         opj_tcd_resolution_t* new_resolutions = (opj_tcd_resolution_t *) opj_realloc(l_tilec->resolutions, l_data_size);
765                         if (! new_resolutions) {
766                                 /* opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to tile resolutions\n");                                                                                         */
767                                 fprintf(stderr, "Not enough memory to tile resolutions\n");
768                                 opj_free(l_tilec->resolutions);
769                                 l_tilec->resolutions = NULL;
770                                 l_tilec->resolutions_size = 0;
771                                 return OPJ_FALSE;
772                         }
773                         l_tilec->resolutions = new_resolutions;
774                         /*fprintf(stderr, "\tReallocate data of tilec (int): from %d to %d x OPJ_UINT32\n", l_tilec->resolutions_size, l_data_size);*/
775                         memset(((OPJ_BYTE*) l_tilec->resolutions)+l_tilec->resolutions_size,0,l_data_size - l_tilec->resolutions_size);
776                         l_tilec->resolutions_size = l_data_size;
777                 }
778                 
779                 l_level_no = l_tilec->numresolutions - 1;
780                 l_res = l_tilec->resolutions;
781                 l_step_size = l_tccp->stepsizes;
782                 if (l_tccp->qmfbid == 0) {
783                         l_gain_ptr = &opj_dwt_getgain_real;
784                 }
785                 else {
786                         l_gain_ptr  = &opj_dwt_getgain;
787                 }
788                 /*fprintf(stderr, "\tlevel_no=%d\n",l_level_no);*/
789                 
790                 for (resno = 0; resno < l_tilec->numresolutions; ++resno) {
791                         /*fprintf(stderr, "\t\tresno = %d/%d\n", resno, l_tilec->numresolutions);*/
792                         OPJ_INT32 tlcbgxstart, tlcbgystart /*, brcbgxend, brcbgyend*/;
793                         OPJ_UINT32 cbgwidthexpn, cbgheightexpn;
794                         OPJ_UINT32 cblkwidthexpn, cblkheightexpn;
795                         
796                         /* border for each resolution level (global) */
797                         l_res->x0 = opj_int_ceildivpow2(l_tilec->x0, (OPJ_INT32)l_level_no);
798                         l_res->y0 = opj_int_ceildivpow2(l_tilec->y0, (OPJ_INT32)l_level_no);
799                         l_res->x1 = opj_int_ceildivpow2(l_tilec->x1, (OPJ_INT32)l_level_no);
800                         l_res->y1 = opj_int_ceildivpow2(l_tilec->y1, (OPJ_INT32)l_level_no);
801                         /*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);*/
802                         /* p. 35, table A-23, ISO/IEC FDIS154444-1 : 2000 (18 august 2000) */
803                         l_pdx = l_tccp->prcw[resno];
804                         l_pdy = l_tccp->prch[resno];
805                         /*fprintf(stderr, "\t\t\tpdx=%d, pdy=%d\n", l_pdx, l_pdy);*/
806                         /* p. 64, B.6, ISO/IEC FDIS15444-1 : 2000 (18 august 2000)  */
807                         l_tl_prc_x_start = opj_int_floordivpow2(l_res->x0, (OPJ_INT32)l_pdx) << l_pdx;
808                         l_tl_prc_y_start = opj_int_floordivpow2(l_res->y0, (OPJ_INT32)l_pdy) << l_pdy;
809                         l_br_prc_x_end = opj_int_ceildivpow2(l_res->x1, (OPJ_INT32)l_pdx) << l_pdx;
810                         l_br_prc_y_end = opj_int_ceildivpow2(l_res->y1, (OPJ_INT32)l_pdy) << l_pdy;
811                         /*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 );*/
812                         
813                         l_res->pw = (l_res->x0 == l_res->x1) ? 0 : (OPJ_UINT32)((l_br_prc_x_end - l_tl_prc_x_start) >> l_pdx);
814                         l_res->ph = (l_res->y0 == l_res->y1) ? 0 : (OPJ_UINT32)((l_br_prc_y_end - l_tl_prc_y_start) >> l_pdy);
815                         /*fprintf(stderr, "\t\t\tres_pw=%d, res_ph=%d\n", l_res->pw, l_res->ph );*/
816                         
817                         l_nb_precincts = l_res->pw * l_res->ph;
818                         l_nb_precinct_size = l_nb_precincts * (OPJ_UINT32)sizeof(opj_tcd_precinct_t);
819                         if (resno == 0) {
820                                 tlcbgxstart = l_tl_prc_x_start;
821                                 tlcbgystart = l_tl_prc_y_start;
822                                 /*brcbgxend = l_br_prc_x_end;*/
823                                 /* brcbgyend = l_br_prc_y_end;*/
824                                 cbgwidthexpn = l_pdx;
825                                 cbgheightexpn = l_pdy;
826                                 l_res->numbands = 1;
827                         }
828                         else {
829                                 tlcbgxstart = opj_int_ceildivpow2(l_tl_prc_x_start, 1);
830                                 tlcbgystart = opj_int_ceildivpow2(l_tl_prc_y_start, 1);
831                                 /*brcbgxend = opj_int_ceildivpow2(l_br_prc_x_end, 1);*/
832                                 /*brcbgyend = opj_int_ceildivpow2(l_br_prc_y_end, 1);*/
833                                 cbgwidthexpn = l_pdx - 1;
834                                 cbgheightexpn = l_pdy - 1;
835                                 l_res->numbands = 3;
836                         }
837                         
838                         cblkwidthexpn = opj_uint_min(l_tccp->cblkw, cbgwidthexpn);
839                         cblkheightexpn = opj_uint_min(l_tccp->cblkh, cbgheightexpn);
840                         l_band = l_res->bands;
841                         
842                         for (bandno = 0; bandno < l_res->numbands; ++bandno) {
843                                 OPJ_INT32 numbps;
844                                 /*fprintf(stderr, "\t\t\tband_no=%d/%d\n", bandno, l_res->numbands );*/
845                                 
846                                 if (resno == 0) {
847                                         l_band->bandno = 0 ;
848                                         l_band->x0 = opj_int_ceildivpow2(l_tilec->x0, (OPJ_INT32)l_level_no);
849                                         l_band->y0 = opj_int_ceildivpow2(l_tilec->y0, (OPJ_INT32)l_level_no);
850                                         l_band->x1 = opj_int_ceildivpow2(l_tilec->x1, (OPJ_INT32)l_level_no);
851                                         l_band->y1 = opj_int_ceildivpow2(l_tilec->y1, (OPJ_INT32)l_level_no);
852                                 }
853                                 else {
854                                         l_band->bandno = bandno + 1;
855                                         /* x0b = 1 if bandno = 1 or 3 */
856                                         l_x0b = l_band->bandno&1;
857                                         /* y0b = 1 if bandno = 2 or 3 */
858                                         l_y0b = (OPJ_INT32)((l_band->bandno)>>1);
859                                         /* l_band border (global) */
860                                         l_band->x0 = opj_int_ceildivpow2(l_tilec->x0 - (1 << l_level_no) * l_x0b, (OPJ_INT32)(l_level_no + 1));
861                                         l_band->y0 = opj_int_ceildivpow2(l_tilec->y0 - (1 << l_level_no) * l_y0b, (OPJ_INT32)(l_level_no + 1));
862                                         l_band->x1 = opj_int_ceildivpow2(l_tilec->x1 - (1 << l_level_no) * l_x0b, (OPJ_INT32)(l_level_no + 1));
863                                         l_band->y1 = opj_int_ceildivpow2(l_tilec->y1 - (1 << l_level_no) * l_y0b, (OPJ_INT32)(l_level_no + 1));
864                                 }
865                                 
866                                 /** avoid an if with storing function pointer */
867                                 l_gain = (*l_gain_ptr) (l_band->bandno);
868                                 numbps = (OPJ_INT32)(l_image_comp->prec + l_gain);
869                                 l_band->stepsize = (OPJ_FLOAT32)(((1.0 + l_step_size->mant / 2048.0) * pow(2.0, (OPJ_INT32) (numbps - l_step_size->expn)))) * fraction;
870                                 l_band->numbps = l_step_size->expn + (OPJ_INT32)l_tccp->numgbits - 1;      /* WHY -1 ? */
871                                 
872                                 if (! l_band->precincts) {
873                                         l_band->precincts = (opj_tcd_precinct_t *) opj_malloc( /*3 * */ l_nb_precinct_size);
874                                         if (! l_band->precincts) {
875                                                 return OPJ_FALSE;
876                                         }
877                                         /*fprintf(stderr, "\t\t\t\tAllocate precincts of a band (opj_tcd_precinct_t): %d\n",l_nb_precinct_size);     */
878                                         memset(l_band->precincts,0,l_nb_precinct_size);
879                                         l_band->precincts_data_size = l_nb_precinct_size;
880                                 }
881                                 else if (l_band->precincts_data_size < l_nb_precinct_size) {
882                                         
883                                         opj_tcd_precinct_t * new_precincts = (opj_tcd_precinct_t *) opj_realloc(l_band->precincts,/*3 * */ l_nb_precinct_size);
884                                         if (! new_precincts) {
885                                                 /* opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to handle band precints\n");                                                                    */
886                                                 fprintf(stderr, "Not enough memory to handle band precints\n");
887                                                 opj_free(l_band->precincts);
888                                                 l_band->precincts = NULL;
889                                                 l_band->precincts_data_size = 0;
890                                                 return OPJ_FALSE;
891                                         }
892                                         l_band->precincts = new_precincts;
893                                         /*fprintf(stderr, "\t\t\t\tReallocate precincts of a band (opj_tcd_precinct_t): from %d to %d\n",l_band->precincts_data_size, l_nb_precinct_size);*/
894                                         memset(((OPJ_BYTE *) l_band->precincts) + l_band->precincts_data_size,0,l_nb_precinct_size - l_band->precincts_data_size);
895                                         l_band->precincts_data_size = l_nb_precinct_size;
896                                 }
897                                 
898                                 l_current_precinct = l_band->precincts;
899                                 for (precno = 0; precno < l_nb_precincts; ++precno) {
900                                         OPJ_INT32 tlcblkxstart, tlcblkystart, brcblkxend, brcblkyend;
901                                         OPJ_INT32 cbgxstart = tlcbgxstart + (OPJ_INT32)(precno % l_res->pw) * (1 << cbgwidthexpn);
902                                         OPJ_INT32 cbgystart = tlcbgystart + (OPJ_INT32)(precno / l_res->pw) * (1 << cbgheightexpn);
903                                         OPJ_INT32 cbgxend = cbgxstart + (1 << cbgwidthexpn);
904                                         OPJ_INT32 cbgyend = cbgystart + (1 << cbgheightexpn);
905                                         /*fprintf(stderr, "\t precno=%d; bandno=%d, resno=%d; compno=%d\n", precno, bandno , resno, compno);*/
906                                         /*fprintf(stderr, "\t tlcbgxstart(=%d) + (precno(=%d) percent res->pw(=%d)) * (1 << cbgwidthexpn(=%d)) \n",tlcbgxstart,precno,l_res->pw,cbgwidthexpn);*/
907                                         
908                                         /* precinct size (global) */
909                                         /*fprintf(stderr, "\t cbgxstart=%d, l_band->x0 = %d \n",cbgxstart, l_band->x0);*/
910                                         
911                                         l_current_precinct->x0 = opj_int_max(cbgxstart, l_band->x0);
912                                         l_current_precinct->y0 = opj_int_max(cbgystart, l_band->y0);
913                                         l_current_precinct->x1 = opj_int_min(cbgxend, l_band->x1);
914                                         l_current_precinct->y1 = opj_int_min(cbgyend, l_band->y1);
915                                         /*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);*/
916                                         
917                                         tlcblkxstart = opj_int_floordivpow2(l_current_precinct->x0, (OPJ_INT32)cblkwidthexpn) << cblkwidthexpn;
918                                         /*fprintf(stderr, "\t tlcblkxstart =%d\n",tlcblkxstart );*/
919                                         tlcblkystart = opj_int_floordivpow2(l_current_precinct->y0, (OPJ_INT32)cblkheightexpn) << cblkheightexpn;
920                                         /*fprintf(stderr, "\t tlcblkystart =%d\n",tlcblkystart );*/
921                                         brcblkxend = opj_int_ceildivpow2(l_current_precinct->x1, (OPJ_INT32)cblkwidthexpn) << cblkwidthexpn;
922                                         /*fprintf(stderr, "\t brcblkxend =%d\n",brcblkxend );*/
923                                         brcblkyend = opj_int_ceildivpow2(l_current_precinct->y1, (OPJ_INT32)cblkheightexpn) << cblkheightexpn;
924                                         /*fprintf(stderr, "\t brcblkyend =%d\n",brcblkyend );*/
925                                         l_current_precinct->cw = (OPJ_UINT32)((brcblkxend - tlcblkxstart) >> cblkwidthexpn);
926                                         l_current_precinct->ch = (OPJ_UINT32)((brcblkyend - tlcblkystart) >> cblkheightexpn);
927                                         
928                                         l_nb_code_blocks = l_current_precinct->cw * l_current_precinct->ch;
929                                         /*fprintf(stderr, "\t\t\t\t precinct_cw = %d x recinct_ch = %d\n",l_current_precinct->cw, l_current_precinct->ch);      */
930                                         l_nb_code_blocks_size = l_nb_code_blocks * (OPJ_UINT32)sizeof_block;
931                                         
932                                         if (! l_current_precinct->cblks.blocks) {
933                                                 l_current_precinct->cblks.blocks = opj_malloc(l_nb_code_blocks_size);
934                                                 if (! l_current_precinct->cblks.blocks ) {
935                                                         return OPJ_FALSE;
936                                                 }
937                                                 /*fprintf(stderr, "\t\t\t\tAllocate cblks of a precinct (opj_tcd_cblk_dec_t): %d\n",l_nb_code_blocks_size);*/
938                                                 
939                                                 memset(l_current_precinct->cblks.blocks,0,l_nb_code_blocks_size);
940                                                 
941                                                 l_current_precinct->block_size = l_nb_code_blocks_size;
942                                         }
943                                         else if (l_nb_code_blocks_size > l_current_precinct->block_size) {
944                                                 void *new_blocks = opj_realloc(l_current_precinct->cblks.blocks, l_nb_code_blocks_size);
945                                                 if (! new_blocks) {
946                                                         opj_free(l_current_precinct->cblks.blocks);
947                                                         l_current_precinct->cblks.blocks = NULL;
948                                                         l_current_precinct->block_size = 0;
949                                                         /* opj_event_msg(p_manager, EVT_ERROR, "Not enough memory for current precinct codeblock element\n");                                              */
950                                                         fprintf(stderr, "Not enough memory for current precinct codeblock element\n");
951                                                         return OPJ_FALSE;
952                                                 }
953                                                 l_current_precinct->cblks.blocks = new_blocks;
954                                                 /*fprintf(stderr, "\t\t\t\tReallocate cblks of a precinct (opj_tcd_cblk_dec_t): from %d to %d\n",l_current_precinct->block_size, l_nb_code_blocks_size);     */
955                                                 
956                                                 memset(((OPJ_BYTE *) l_current_precinct->cblks.blocks) + l_current_precinct->block_size
957                                                                          ,0
958                                                                          ,l_nb_code_blocks_size - l_current_precinct->block_size);
959                                                 
960                                                 l_current_precinct->block_size = l_nb_code_blocks_size;
961                                         }
962                                         
963                                         if (! l_current_precinct->incltree) {
964                                                 l_current_precinct->incltree = opj_tgt_create(l_current_precinct->cw,
965                                                                                                                                                                                                                                         l_current_precinct->ch);
966                                         }
967                                         else{
968                                                 l_current_precinct->incltree = opj_tgt_init(l_current_precinct->incltree,
969                                                                                                                                                                                                                                 l_current_precinct->cw,
970                                                                                                                                                                                                                                 l_current_precinct->ch);
971                                         }
972
973                                         if (! l_current_precinct->incltree)     {
974                                                 fprintf(stderr, "WARNING: No incltree created.\n");
975                                                 /*return OPJ_FALSE;*/
976                                         }
977
978                                         if (! l_current_precinct->imsbtree) {
979                                                 l_current_precinct->imsbtree = opj_tgt_create(
980                                                                                                                                                                                                                                         l_current_precinct->cw,
981                                                                                                                                                                                                                                         l_current_precinct->ch);
982                                         }
983                                         else {
984                                                 l_current_precinct->imsbtree = opj_tgt_init(
985                                                                                                                                                                                                                                 l_current_precinct->imsbtree,
986                                                                                                                                                                                                                                 l_current_precinct->cw,
987                                                                                                                                                                                                                                 l_current_precinct->ch);
988                                         }
989
990                                         if (! l_current_precinct->imsbtree) {
991                                                 fprintf(stderr, "WARNING: No imsbtree created.\n");
992                                                 /*return OPJ_FALSE;*/
993                                         }
994
995                                         for (cblkno = 0; cblkno < l_nb_code_blocks; ++cblkno) {
996                                                 OPJ_INT32 cblkxstart = tlcblkxstart + (OPJ_INT32)(cblkno % l_current_precinct->cw) * (1 << cblkwidthexpn);
997                                                 OPJ_INT32 cblkystart = tlcblkystart + (OPJ_INT32)(cblkno / l_current_precinct->cw) * (1 << cblkheightexpn);
998                                                 OPJ_INT32 cblkxend = cblkxstart + (1 << cblkwidthexpn);
999                                                 OPJ_INT32 cblkyend = cblkystart + (1 << cblkheightexpn);
1000                                                 
1001                                                 if (isEncoder) {
1002                                                         opj_tcd_cblk_enc_t* l_code_block = l_current_precinct->cblks.enc + cblkno;
1003                                                         
1004                                                         if (! opj_tcd_code_block_enc_allocate(l_code_block)) {
1005                                                                 return OPJ_FALSE;
1006                                                         }
1007                                                         /* code-block size (global) */
1008                                                         l_code_block->x0 = opj_int_max(cblkxstart, l_current_precinct->x0);
1009                                                         l_code_block->y0 = opj_int_max(cblkystart, l_current_precinct->y0);
1010                                                         l_code_block->x1 = opj_int_min(cblkxend, l_current_precinct->x1);
1011                                                         l_code_block->y1 = opj_int_min(cblkyend, l_current_precinct->y1);
1012                                                         
1013                                                         if (! opj_tcd_code_block_enc_allocate_data(l_code_block)) {
1014                                                                 return OPJ_FALSE;
1015                                                         }
1016                                                 } else {
1017                                                         opj_tcd_cblk_dec_t* l_code_block = l_current_precinct->cblks.dec + cblkno;
1018                                                         
1019                                                         if (! opj_tcd_code_block_dec_allocate(l_code_block)) {
1020                                                                 return OPJ_FALSE;
1021                                                         }
1022                                                         /* code-block size (global) */
1023                                                         l_code_block->x0 = opj_int_max(cblkxstart, l_current_precinct->x0);
1024                                                         l_code_block->y0 = opj_int_max(cblkystart, l_current_precinct->y0);
1025                                                         l_code_block->x1 = opj_int_min(cblkxend, l_current_precinct->x1);
1026                                                         l_code_block->y1 = opj_int_min(cblkyend, l_current_precinct->y1);
1027                                                 }
1028                                         }
1029                                         ++l_current_precinct;
1030                                 } /* precno */
1031                                 ++l_band;
1032                                 ++l_step_size;
1033                         } /* bandno */
1034                         ++l_res;
1035                         --l_level_no;
1036                 } /* resno */
1037                 ++l_tccp;
1038                 ++l_tilec;
1039                 ++l_image_comp;
1040         } /* compno */
1041         return OPJ_TRUE;
1042 }
1043
1044 OPJ_BOOL opj_tcd_init_encode_tile (opj_tcd_t *p_tcd, OPJ_UINT32 p_tile_no)
1045 {
1046         return opj_tcd_init_tile(p_tcd, p_tile_no, OPJ_TRUE, 1.0F, sizeof(opj_tcd_cblk_enc_t));
1047 }
1048
1049 OPJ_BOOL opj_tcd_init_decode_tile (opj_tcd_t *p_tcd, OPJ_UINT32 p_tile_no)
1050 {
1051         return opj_tcd_init_tile(p_tcd, p_tile_no, OPJ_FALSE, 0.5F, sizeof(opj_tcd_cblk_dec_t));
1052 }
1053
1054 /**
1055  * Allocates memory for an encoding code block (but not data memory).
1056  */
1057 static OPJ_BOOL opj_tcd_code_block_enc_allocate (opj_tcd_cblk_enc_t * p_code_block)
1058 {
1059         if (! p_code_block->layers) {
1060                 /* no memset since data */
1061                 p_code_block->layers = (opj_tcd_layer_t*) opj_calloc(100, sizeof(opj_tcd_layer_t));
1062                 if (! p_code_block->layers) {
1063                         return OPJ_FALSE;
1064                 }
1065         }
1066         if (! p_code_block->passes) {
1067                 p_code_block->passes = (opj_tcd_pass_t*) opj_calloc(100, sizeof(opj_tcd_pass_t));
1068                 if (! p_code_block->passes) {
1069                         return OPJ_FALSE;
1070                 }
1071         }
1072         return OPJ_TRUE;
1073 }
1074
1075 /**
1076  * Allocates data memory for an encoding code block.
1077  */
1078 static OPJ_BOOL opj_tcd_code_block_enc_allocate_data (opj_tcd_cblk_enc_t * p_code_block)
1079 {
1080         OPJ_UINT32 l_data_size;
1081         
1082         l_data_size = (OPJ_UINT32)((p_code_block->x1 - p_code_block->x0) * (p_code_block->y1 - p_code_block->y0) * (OPJ_INT32)sizeof(OPJ_UINT32));
1083         
1084         if (l_data_size > p_code_block->data_size) {
1085                 if (p_code_block->data) {
1086                         opj_free(p_code_block->data - 1); /* again, why -1 */
1087                 }
1088                 p_code_block->data = (OPJ_BYTE*) opj_malloc(l_data_size);
1089                 if(! p_code_block->data) {
1090                         p_code_block->data_size = 0U;
1091                         return OPJ_FALSE;
1092                 }
1093                 p_code_block->data_size = l_data_size;
1094                 
1095                 p_code_block->data[0] = 0;
1096                 p_code_block->data+=1;   /*why +1 ?*/
1097         }
1098         return OPJ_TRUE;
1099 }
1100
1101 /**
1102  * Allocates memory for a decoding code block.
1103  */
1104 static OPJ_BOOL opj_tcd_code_block_dec_allocate (opj_tcd_cblk_dec_t * p_code_block)
1105 {
1106         if (! p_code_block->data) {
1107
1108                 p_code_block->data = (OPJ_BYTE*) opj_malloc(OPJ_J2K_DEFAULT_CBLK_DATA_SIZE);
1109                 if (! p_code_block->data) {
1110                         return OPJ_FALSE;
1111                 }
1112                 p_code_block->data_max_size = OPJ_J2K_DEFAULT_CBLK_DATA_SIZE;
1113                 /*fprintf(stderr, "Allocate 8192 elements of code_block->data\n");*/
1114
1115                 p_code_block->segs = (opj_tcd_seg_t *) opj_calloc(OPJ_J2K_DEFAULT_NB_SEGS,sizeof(opj_tcd_seg_t));
1116                 if (! p_code_block->segs) {
1117                         return OPJ_FALSE;
1118                 }
1119                 /*fprintf(stderr, "Allocate %d elements of code_block->data\n", OPJ_J2K_DEFAULT_NB_SEGS * sizeof(opj_tcd_seg_t));*/
1120
1121                 p_code_block->m_current_max_segs = OPJ_J2K_DEFAULT_NB_SEGS;
1122                 /*fprintf(stderr, "m_current_max_segs of code_block->data = %d\n", p_code_block->m_current_max_segs);*/
1123         } else {
1124                                         /* sanitize */
1125                                         OPJ_BYTE* l_data = p_code_block->data;
1126                                         OPJ_UINT32 l_data_max_size = p_code_block->data_max_size;
1127                                         opj_tcd_seg_t * l_segs = p_code_block->segs;
1128                                         OPJ_UINT32 l_current_max_segs = p_code_block->m_current_max_segs;
1129
1130                                         memset(p_code_block, 0, sizeof(opj_tcd_cblk_dec_t));
1131                                         p_code_block->data = l_data;
1132                                         p_code_block->data_max_size = l_data_max_size;
1133                                         p_code_block->segs = l_segs;
1134                                         p_code_block->m_current_max_segs = l_current_max_segs;
1135                                 }
1136
1137         return OPJ_TRUE;
1138 }
1139
1140 OPJ_UINT32 opj_tcd_get_decoded_tile_size ( opj_tcd_t *p_tcd )
1141 {
1142         OPJ_UINT32 i;
1143         OPJ_UINT32 l_data_size = 0;
1144         opj_image_comp_t * l_img_comp = 00;
1145         opj_tcd_tilecomp_t * l_tile_comp = 00;
1146         opj_tcd_resolution_t * l_res = 00;
1147         OPJ_UINT32 l_size_comp, l_remaining;
1148
1149         l_tile_comp = p_tcd->tcd_image->tiles->comps;
1150         l_img_comp = p_tcd->image->comps;
1151
1152         for (i=0;i<p_tcd->image->numcomps;++i) {
1153                 l_size_comp = l_img_comp->prec >> 3; /*(/ 8)*/
1154                 l_remaining = l_img_comp->prec & 7;  /* (%8) */
1155
1156                 if(l_remaining) {
1157                         ++l_size_comp;
1158                 }
1159
1160                 if (l_size_comp == 3) {
1161                         l_size_comp = 4;
1162                 }
1163
1164                 l_res = l_tile_comp->resolutions + l_tile_comp->minimum_num_resolutions - 1;
1165                 l_data_size += l_size_comp * (OPJ_UINT32)((l_res->x1 - l_res->x0) * (l_res->y1 - l_res->y0));
1166                 ++l_img_comp;
1167                 ++l_tile_comp;
1168         }
1169
1170         return l_data_size;
1171 }
1172
1173 OPJ_BOOL opj_tcd_encode_tile(   opj_tcd_t *p_tcd,
1174                                                         OPJ_UINT32 p_tile_no,
1175                                                         OPJ_BYTE *p_dest,
1176                                                         OPJ_UINT32 * p_data_written,
1177                                                         OPJ_UINT32 p_max_length,
1178                                                         opj_codestream_info_t *p_cstr_info)
1179 {
1180
1181         if (p_tcd->cur_tp_num == 0) {
1182
1183                 p_tcd->tcd_tileno = p_tile_no;
1184                 p_tcd->tcp = &p_tcd->cp->tcps[p_tile_no];
1185
1186                 /* INDEX >> "Precinct_nb_X et Precinct_nb_Y" */
1187                 if(p_cstr_info)  {
1188                         OPJ_UINT32 l_num_packs = 0;
1189                         OPJ_UINT32 i;
1190                         opj_tcd_tilecomp_t *l_tilec_idx = &p_tcd->tcd_image->tiles->comps[0];        /* based on component 0 */
1191                         opj_tccp_t *l_tccp = p_tcd->tcp->tccps; /* based on component 0 */
1192
1193                         for (i = 0; i < l_tilec_idx->numresolutions; i++) {
1194                                 opj_tcd_resolution_t *l_res_idx = &l_tilec_idx->resolutions[i];
1195
1196                                 p_cstr_info->tile[p_tile_no].pw[i] = (int)l_res_idx->pw;
1197                                 p_cstr_info->tile[p_tile_no].ph[i] = (int)l_res_idx->ph;
1198
1199                                 l_num_packs += l_res_idx->pw * l_res_idx->ph;
1200                                 p_cstr_info->tile[p_tile_no].pdx[i] = (int)l_tccp->prcw[i];
1201                                 p_cstr_info->tile[p_tile_no].pdy[i] = (int)l_tccp->prch[i];
1202                         }
1203                         p_cstr_info->tile[p_tile_no].packet = (opj_packet_info_t*) opj_calloc((size_t)p_cstr_info->numcomps * (size_t)p_cstr_info->numlayers * l_num_packs, sizeof(opj_packet_info_t));
1204                         if (!p_cstr_info->tile[p_tile_no].packet) {
1205                                 /* FIXME event manager error callback */
1206                                 return OPJ_FALSE;
1207                         }
1208                 }
1209                 /* << INDEX */
1210
1211                 /* FIXME _ProfStart(PGROUP_DC_SHIFT); */
1212                 /*---------------TILE-------------------*/
1213                 if (! opj_tcd_dc_level_shift_encode(p_tcd)) {
1214                         return OPJ_FALSE;
1215                 }
1216                 /* FIXME _ProfStop(PGROUP_DC_SHIFT); */
1217
1218                 /* FIXME _ProfStart(PGROUP_MCT); */
1219                 if (! opj_tcd_mct_encode(p_tcd)) {
1220                         return OPJ_FALSE;
1221                 }
1222                 /* FIXME _ProfStop(PGROUP_MCT); */
1223
1224                 /* FIXME _ProfStart(PGROUP_DWT); */
1225                 if (! opj_tcd_dwt_encode(p_tcd)) {
1226                         return OPJ_FALSE;
1227                 }
1228                 /* FIXME  _ProfStop(PGROUP_DWT); */
1229
1230                 /* FIXME  _ProfStart(PGROUP_T1); */
1231                 if (! opj_tcd_t1_encode(p_tcd)) {
1232                         return OPJ_FALSE;
1233                 }
1234                 /* FIXME _ProfStop(PGROUP_T1); */
1235
1236                 /* FIXME _ProfStart(PGROUP_RATE); */
1237                 if (! opj_tcd_rate_allocate_encode(p_tcd,p_dest,p_max_length,p_cstr_info)) {
1238                         return OPJ_FALSE;
1239                 }
1240                 /* FIXME _ProfStop(PGROUP_RATE); */
1241
1242         }
1243         /*--------------TIER2------------------*/
1244
1245         /* INDEX */
1246         if (p_cstr_info) {
1247                 p_cstr_info->index_write = 1;
1248         }
1249         /* FIXME _ProfStart(PGROUP_T2); */
1250
1251         if (! opj_tcd_t2_encode(p_tcd,p_dest,p_data_written,p_max_length,p_cstr_info)) {
1252                 return OPJ_FALSE;
1253         }
1254         /* FIXME _ProfStop(PGROUP_T2); */
1255
1256         /*---------------CLEAN-------------------*/
1257
1258         return OPJ_TRUE;
1259 }
1260
1261 OPJ_BOOL opj_tcd_decode_tile(   opj_tcd_t *p_tcd,
1262                                 OPJ_BYTE *p_src,
1263                                 OPJ_UINT32 p_max_length,
1264                                 OPJ_UINT32 p_tile_no,
1265                                 opj_codestream_index_t *p_cstr_index
1266                                 )
1267 {
1268         OPJ_UINT32 l_data_read;
1269         p_tcd->tcd_tileno = p_tile_no;
1270         p_tcd->tcp = &(p_tcd->cp->tcps[p_tile_no]);
1271
1272 #ifdef TODO_MSD /* FIXME */
1273         /* INDEX >>  */
1274         if(p_cstr_info) {
1275                 OPJ_UINT32 resno, compno, numprec = 0;
1276                 for (compno = 0; compno < (OPJ_UINT32) p_cstr_info->numcomps; compno++) {
1277                         opj_tcp_t *tcp = &p_tcd->cp->tcps[0];
1278                         opj_tccp_t *tccp = &tcp->tccps[compno];
1279                         opj_tcd_tilecomp_t *tilec_idx = &p_tcd->tcd_image->tiles->comps[compno];
1280                         for (resno = 0; resno < tilec_idx->numresolutions; resno++) {
1281                                 opj_tcd_resolution_t *res_idx = &tilec_idx->resolutions[resno];
1282                                 p_cstr_info->tile[p_tile_no].pw[resno] = res_idx->pw;
1283                                 p_cstr_info->tile[p_tile_no].ph[resno] = res_idx->ph;
1284                                 numprec += res_idx->pw * res_idx->ph;
1285                                 p_cstr_info->tile[p_tile_no].pdx[resno] = tccp->prcw[resno];
1286                                 p_cstr_info->tile[p_tile_no].pdy[resno] = tccp->prch[resno];
1287                         }
1288                 }
1289                 p_cstr_info->tile[p_tile_no].packet = (opj_packet_info_t *) opj_malloc(p_cstr_info->numlayers * numprec * sizeof(opj_packet_info_t));
1290                 p_cstr_info->packno = 0;
1291         }
1292         /* << INDEX */
1293 #endif
1294
1295         /*--------------TIER2------------------*/
1296         /* FIXME _ProfStart(PGROUP_T2); */
1297         l_data_read = 0;
1298         if (! opj_tcd_t2_decode(p_tcd, p_src, &l_data_read, p_max_length, p_cstr_index))
1299         {
1300                 return OPJ_FALSE;
1301         }
1302         /* FIXME _ProfStop(PGROUP_T2); */
1303
1304         /*------------------TIER1-----------------*/
1305
1306         /* FIXME _ProfStart(PGROUP_T1); */
1307         if
1308                 (! opj_tcd_t1_decode(p_tcd))
1309         {
1310                 return OPJ_FALSE;
1311         }
1312         /* FIXME _ProfStop(PGROUP_T1); */
1313
1314         /*----------------DWT---------------------*/
1315
1316         /* FIXME _ProfStart(PGROUP_DWT); */
1317         if
1318                 (! opj_tcd_dwt_decode(p_tcd))
1319         {
1320                 return OPJ_FALSE;
1321         }
1322         /* FIXME _ProfStop(PGROUP_DWT); */
1323
1324         /*----------------MCT-------------------*/
1325         /* FIXME _ProfStart(PGROUP_MCT); */
1326         if
1327                 (! opj_tcd_mct_decode(p_tcd))
1328         {
1329                 return OPJ_FALSE;
1330         }
1331         /* FIXME _ProfStop(PGROUP_MCT); */
1332
1333         /* FIXME _ProfStart(PGROUP_DC_SHIFT); */
1334         if
1335                 (! opj_tcd_dc_level_shift_decode(p_tcd))
1336         {
1337                 return OPJ_FALSE;
1338         }
1339         /* FIXME _ProfStop(PGROUP_DC_SHIFT); */
1340
1341
1342         /*---------------TILE-------------------*/
1343         return OPJ_TRUE;
1344 }
1345
1346 OPJ_BOOL opj_tcd_update_tile_data ( opj_tcd_t *p_tcd,
1347                                     OPJ_BYTE * p_dest,
1348                                     OPJ_UINT32 p_dest_length
1349                                     )
1350 {
1351         OPJ_UINT32 i,j,k,l_data_size = 0;
1352         opj_image_comp_t * l_img_comp = 00;
1353         opj_tcd_tilecomp_t * l_tilec = 00;
1354         opj_tcd_resolution_t * l_res;
1355         OPJ_UINT32 l_size_comp, l_remaining;
1356         OPJ_UINT32 l_stride, l_width,l_height;
1357
1358         l_data_size = opj_tcd_get_decoded_tile_size(p_tcd);
1359         if (l_data_size > p_dest_length) {
1360                 return OPJ_FALSE;
1361         }
1362
1363         l_tilec = p_tcd->tcd_image->tiles->comps;
1364         l_img_comp = p_tcd->image->comps;
1365
1366         for (i=0;i<p_tcd->image->numcomps;++i) {
1367                 l_size_comp = l_img_comp->prec >> 3; /*(/ 8)*/
1368                 l_remaining = l_img_comp->prec & 7;  /* (%8) */
1369                 l_res = l_tilec->resolutions + l_img_comp->resno_decoded;
1370                 l_width = (OPJ_UINT32)(l_res->x1 - l_res->x0);
1371                 l_height = (OPJ_UINT32)(l_res->y1 - l_res->y0);
1372                 l_stride = (OPJ_UINT32)(l_tilec->x1 - l_tilec->x0) - l_width;
1373
1374                 if (l_remaining) {
1375                         ++l_size_comp;
1376                 }
1377
1378                 if (l_size_comp == 3) {
1379                         l_size_comp = 4;
1380                 }
1381
1382                 switch (l_size_comp)
1383                         {
1384                         case 1:
1385                                 {
1386                                         OPJ_CHAR * l_dest_ptr = (OPJ_CHAR *) p_dest;
1387                                         const OPJ_INT32 * l_src_ptr = l_tilec->data;
1388
1389                                         if (l_img_comp->sgnd) {
1390                                                 for (j=0;j<l_height;++j) {
1391                                                         for (k=0;k<l_width;++k) {
1392                                                                 *(l_dest_ptr++) = (OPJ_CHAR) (*(l_src_ptr++));
1393                                                         }
1394                                                         l_src_ptr += l_stride;
1395                                                 }
1396                                         }
1397                                         else {
1398                                                 for (j=0;j<l_height;++j) {
1399                                                         for     (k=0;k<l_width;++k) {
1400                                                                 *(l_dest_ptr++) = (OPJ_CHAR) ((*(l_src_ptr++))&0xff);
1401                                                         }
1402                                                         l_src_ptr += l_stride;
1403                                                 }
1404                                         }
1405
1406                                         p_dest = (OPJ_BYTE *)l_dest_ptr;
1407                                 }
1408                                 break;
1409                         case 2:
1410                                 {
1411                                         const OPJ_INT32 * l_src_ptr = l_tilec->data;
1412                                         OPJ_INT16 * l_dest_ptr = (OPJ_INT16 *) p_dest;
1413
1414                                         if (l_img_comp->sgnd) {
1415                                                 for (j=0;j<l_height;++j) {
1416                                                         for (k=0;k<l_width;++k) {
1417                                                                 *(l_dest_ptr++) = (OPJ_INT16) (*(l_src_ptr++));
1418                                                         }
1419                                                         l_src_ptr += l_stride;
1420                                                 }
1421                                         }
1422                                         else {
1423                                                 for (j=0;j<l_height;++j) {
1424                                                         for (k=0;k<l_width;++k) {
1425                                                                 *(l_dest_ptr++) = (OPJ_INT16) ((*(l_src_ptr++))&0xffff);
1426                                                         }
1427                                                         l_src_ptr += l_stride;
1428                                                 }
1429                                         }
1430
1431                                         p_dest = (OPJ_BYTE*) l_dest_ptr;
1432                                 }
1433                                 break;
1434                         case 4:
1435                                 {
1436                                         OPJ_INT32 * l_dest_ptr = (OPJ_INT32 *) p_dest;
1437                                         OPJ_INT32 * l_src_ptr = l_tilec->data;
1438
1439                                         for (j=0;j<l_height;++j) {
1440                                                 for (k=0;k<l_width;++k) {
1441                                                         *(l_dest_ptr++) = (*(l_src_ptr++));
1442                                                 }
1443                                                 l_src_ptr += l_stride;
1444                                         }
1445
1446                                         p_dest = (OPJ_BYTE*) l_dest_ptr;
1447                                 }
1448                                 break;
1449                 }
1450
1451                 ++l_img_comp;
1452                 ++l_tilec;
1453         }
1454
1455         return OPJ_TRUE;
1456 }
1457
1458
1459
1460
1461 void opj_tcd_free_tile(opj_tcd_t *p_tcd)
1462 {
1463         OPJ_UINT32 compno, resno, bandno, precno;
1464         opj_tcd_tile_t *l_tile = 00;
1465         opj_tcd_tilecomp_t *l_tile_comp = 00;
1466         opj_tcd_resolution_t *l_res = 00;
1467         opj_tcd_band_t *l_band = 00;
1468         opj_tcd_precinct_t *l_precinct = 00;
1469         OPJ_UINT32 l_nb_resolutions, l_nb_precincts;
1470         void (* l_tcd_code_block_deallocate) (opj_tcd_precinct_t *) = 00;
1471
1472         if (! p_tcd) {
1473                 return;
1474         }
1475
1476         if (! p_tcd->tcd_image) {
1477                 return;
1478         }
1479
1480         if (p_tcd->m_is_decoder) {
1481                 l_tcd_code_block_deallocate = opj_tcd_code_block_dec_deallocate;
1482         }
1483         else {
1484                 l_tcd_code_block_deallocate = opj_tcd_code_block_enc_deallocate;
1485         }
1486
1487         l_tile = p_tcd->tcd_image->tiles;
1488         if (! l_tile) {
1489                 return;
1490         }
1491
1492         l_tile_comp = l_tile->comps;
1493
1494         for (compno = 0; compno < l_tile->numcomps; ++compno) {
1495                 l_res = l_tile_comp->resolutions;
1496                 if (l_res) {
1497
1498                         l_nb_resolutions = l_tile_comp->resolutions_size / sizeof(opj_tcd_resolution_t);
1499                         for (resno = 0; resno < l_nb_resolutions; ++resno) {
1500                                 l_band = l_res->bands;
1501                                 for     (bandno = 0; bandno < 3; ++bandno) {
1502                                         l_precinct = l_band->precincts;
1503                                         if (l_precinct) {
1504
1505                                                 l_nb_precincts = l_band->precincts_data_size / sizeof(opj_tcd_precinct_t);
1506                                                 for (precno = 0; precno < l_nb_precincts; ++precno) {
1507                                                         opj_tgt_destroy(l_precinct->incltree);
1508                                                         l_precinct->incltree = 00;
1509                                                         opj_tgt_destroy(l_precinct->imsbtree);
1510                                                         l_precinct->imsbtree = 00;
1511                                                         (*l_tcd_code_block_deallocate) (l_precinct);
1512                                                         ++l_precinct;
1513                                                 }
1514
1515                                                 opj_free(l_band->precincts);
1516                                                 l_band->precincts = 00;
1517                                         }
1518                                         ++l_band;
1519                                 } /* for (resno */
1520                                 ++l_res;
1521                         }
1522
1523                         opj_free(l_tile_comp->resolutions);
1524                         l_tile_comp->resolutions = 00;
1525                 }
1526
1527                 if (l_tile_comp->ownsData && l_tile_comp->data) {
1528                         opj_free(l_tile_comp->data);
1529                         l_tile_comp->data = 00;
1530                         l_tile_comp->ownsData = 0;
1531                         l_tile_comp->data_size = 0;
1532                         l_tile_comp->data_size_needed = 0;
1533                 }
1534                 ++l_tile_comp;
1535         }
1536
1537         opj_free(l_tile->comps);
1538         l_tile->comps = 00;
1539         opj_free(p_tcd->tcd_image->tiles);
1540         p_tcd->tcd_image->tiles = 00;
1541 }
1542
1543
1544 OPJ_BOOL opj_tcd_t2_decode (opj_tcd_t *p_tcd,
1545                             OPJ_BYTE * p_src_data,
1546                             OPJ_UINT32 * p_data_read,
1547                             OPJ_UINT32 p_max_src_size,
1548                             opj_codestream_index_t *p_cstr_index
1549                             )
1550 {
1551         opj_t2_t * l_t2;
1552
1553         l_t2 = opj_t2_create(p_tcd->image, p_tcd->cp);
1554         if (l_t2 == 00) {
1555                 return OPJ_FALSE;
1556         }
1557
1558         if (! opj_t2_decode_packets(
1559                                         l_t2,
1560                                         p_tcd->tcd_tileno,
1561                                         p_tcd->tcd_image->tiles,
1562                                         p_src_data,
1563                                         p_data_read,
1564                                         p_max_src_size,
1565                                         p_cstr_index)) {
1566                 opj_t2_destroy(l_t2);
1567                 return OPJ_FALSE;
1568         }
1569
1570         opj_t2_destroy(l_t2);
1571
1572         /*---------------CLEAN-------------------*/
1573         return OPJ_TRUE;
1574 }
1575
1576 OPJ_BOOL opj_tcd_t1_decode ( opj_tcd_t *p_tcd )
1577 {
1578         OPJ_UINT32 compno;
1579         opj_t1_t * l_t1;
1580         opj_tcd_tile_t * l_tile = p_tcd->tcd_image->tiles;
1581         opj_tcd_tilecomp_t* l_tile_comp = l_tile->comps;
1582         opj_tccp_t * l_tccp = p_tcd->tcp->tccps;
1583
1584
1585         l_t1 = opj_t1_create(OPJ_FALSE);
1586         if (l_t1 == 00) {
1587                 return OPJ_FALSE;
1588         }
1589
1590         for (compno = 0; compno < l_tile->numcomps; ++compno) {
1591                 /* The +3 is headroom required by the vectorized DWT */
1592                 if (OPJ_FALSE == opj_t1_decode_cblks(l_t1, l_tile_comp, l_tccp)) {
1593                         opj_t1_destroy(l_t1);
1594                         return OPJ_FALSE;
1595                 }
1596                 ++l_tile_comp;
1597                 ++l_tccp;
1598         }
1599
1600         opj_t1_destroy(l_t1);
1601
1602         return OPJ_TRUE;
1603 }
1604
1605
1606 OPJ_BOOL opj_tcd_dwt_decode ( opj_tcd_t *p_tcd )
1607 {
1608         OPJ_UINT32 compno;
1609         opj_tcd_tile_t * l_tile = p_tcd->tcd_image->tiles;
1610         opj_tcd_tilecomp_t * l_tile_comp = l_tile->comps;
1611         opj_tccp_t * l_tccp = p_tcd->tcp->tccps;
1612         opj_image_comp_t * l_img_comp = p_tcd->image->comps;
1613
1614         for (compno = 0; compno < l_tile->numcomps; compno++) {
1615                 /*
1616                 if (tcd->cp->reduce != 0) {
1617                         tcd->image->comps[compno].resno_decoded =
1618                                 tile->comps[compno].numresolutions - tcd->cp->reduce - 1;
1619                         if (tcd->image->comps[compno].resno_decoded < 0)
1620                         {
1621                                 return false;
1622                         }
1623                 }
1624                 numres2decode = tcd->image->comps[compno].resno_decoded + 1;
1625                 if(numres2decode > 0){
1626                 */
1627
1628                 if (l_tccp->qmfbid == 1) {
1629                         if (! opj_dwt_decode(l_tile_comp, l_img_comp->resno_decoded+1)) {
1630                                 return OPJ_FALSE;
1631                         }
1632                 }
1633                 else {
1634                         if (! opj_dwt_decode_real(l_tile_comp, l_img_comp->resno_decoded+1)) {
1635                                 return OPJ_FALSE;
1636                         }
1637                 }
1638
1639                 ++l_tile_comp;
1640                 ++l_img_comp;
1641                 ++l_tccp;
1642         }
1643
1644         return OPJ_TRUE;
1645 }
1646 OPJ_BOOL opj_tcd_mct_decode ( opj_tcd_t *p_tcd )
1647 {
1648         opj_tcd_tile_t * l_tile = p_tcd->tcd_image->tiles;
1649         opj_tcp_t * l_tcp = p_tcd->tcp;
1650         opj_tcd_tilecomp_t * l_tile_comp = l_tile->comps;
1651         OPJ_UINT32 l_samples,i;
1652
1653         if (! l_tcp->mct) {
1654                 return OPJ_TRUE;
1655         }
1656
1657         l_samples = (OPJ_UINT32)((l_tile_comp->x1 - l_tile_comp->x0) * (l_tile_comp->y1 - l_tile_comp->y0));
1658
1659         if (l_tile->numcomps >= 3 ){
1660                 /* testcase 1336.pdf.asan.47.376 */
1661                 if ((l_tile->comps[0].x1 - l_tile->comps[0].x0) * (l_tile->comps[0].y1 - l_tile->comps[0].y0) < (OPJ_INT32)l_samples ||
1662                     (l_tile->comps[1].x1 - l_tile->comps[1].x0) * (l_tile->comps[1].y1 - l_tile->comps[1].y0) < (OPJ_INT32)l_samples ||
1663                     (l_tile->comps[2].x1 - l_tile->comps[2].x0) * (l_tile->comps[2].y1 - l_tile->comps[2].y0) < (OPJ_INT32)l_samples) {
1664                         fprintf(stderr, "Tiles don't all have the same dimension. Skip the MCT step.\n");
1665                         return OPJ_FALSE;
1666                 }
1667                 else if (l_tcp->mct == 2) {
1668                         OPJ_BYTE ** l_data;
1669
1670                         if (! l_tcp->m_mct_decoding_matrix) {
1671                                 return OPJ_TRUE;
1672                         }
1673
1674                         l_data = (OPJ_BYTE **) opj_malloc(l_tile->numcomps*sizeof(OPJ_BYTE*));
1675                         if (! l_data) {
1676                                 return OPJ_FALSE;
1677                         }
1678
1679                         for (i=0;i<l_tile->numcomps;++i) {
1680                                 l_data[i] = (OPJ_BYTE*) l_tile_comp->data;
1681                                 ++l_tile_comp;
1682                         }
1683
1684                         if (! opj_mct_decode_custom(/* MCT data */
1685                                                                         (OPJ_BYTE*) l_tcp->m_mct_decoding_matrix,
1686                                                                         /* size of components */
1687                                                                         l_samples,
1688                                                                         /* components */
1689                                                                         l_data,
1690                                                                         /* nb of components (i.e. size of pData) */
1691                                                                         l_tile->numcomps,
1692                                                                         /* tells if the data is signed */
1693                                                                         p_tcd->image->comps->sgnd)) {
1694                                 opj_free(l_data);
1695                                 return OPJ_FALSE;
1696                         }
1697
1698                         opj_free(l_data);
1699                 }
1700                 else {
1701                         if (l_tcp->tccps->qmfbid == 1) {
1702                                 opj_mct_decode(     l_tile->comps[0].data,
1703                                                         l_tile->comps[1].data,
1704                                                         l_tile->comps[2].data,
1705                                                         l_samples);
1706                         }
1707                         else {
1708                             opj_mct_decode_real((OPJ_FLOAT32*)l_tile->comps[0].data,
1709                                                 (OPJ_FLOAT32*)l_tile->comps[1].data,
1710                                                 (OPJ_FLOAT32*)l_tile->comps[2].data,
1711                                                 l_samples);
1712                         }
1713                 }
1714         }
1715         else {
1716                 /* FIXME need to use opj_event_msg function */
1717                 fprintf(stderr,"Number of components (%d) is inconsistent with a MCT. Skip the MCT step.\n",l_tile->numcomps);
1718         }
1719
1720         return OPJ_TRUE;
1721 }
1722
1723
1724 OPJ_BOOL opj_tcd_dc_level_shift_decode ( opj_tcd_t *p_tcd )
1725 {
1726         OPJ_UINT32 compno;
1727         opj_tcd_tilecomp_t * l_tile_comp = 00;
1728         opj_tccp_t * l_tccp = 00;
1729         opj_image_comp_t * l_img_comp = 00;
1730         opj_tcd_resolution_t* l_res = 00;
1731         opj_tcd_tile_t * l_tile;
1732         OPJ_UINT32 l_width,l_height,i,j;
1733         OPJ_INT32 * l_current_ptr;
1734         OPJ_INT32 l_min, l_max;
1735         OPJ_UINT32 l_stride;
1736
1737         l_tile = p_tcd->tcd_image->tiles;
1738         l_tile_comp = l_tile->comps;
1739         l_tccp = p_tcd->tcp->tccps;
1740         l_img_comp = p_tcd->image->comps;
1741
1742         for (compno = 0; compno < l_tile->numcomps; compno++) {
1743                 l_res = l_tile_comp->resolutions + l_img_comp->resno_decoded;
1744                 l_width = (OPJ_UINT32)(l_res->x1 - l_res->x0);
1745                 l_height = (OPJ_UINT32)(l_res->y1 - l_res->y0);
1746                 l_stride = (OPJ_UINT32)(l_tile_comp->x1 - l_tile_comp->x0) - l_width;
1747
1748                 assert(l_height == 0 || l_width + l_stride <= l_tile_comp->data_size / l_height); /*MUPDF*/
1749
1750                 if (l_img_comp->sgnd) {
1751                         l_min = -(1 << (l_img_comp->prec - 1));
1752                         l_max = (1 << (l_img_comp->prec - 1)) - 1;
1753                 }
1754                 else {
1755             l_min = 0;
1756                         l_max = (1 << l_img_comp->prec) - 1;
1757                 }
1758
1759                 l_current_ptr = l_tile_comp->data;
1760
1761                 if (l_tccp->qmfbid == 1) {
1762                         for (j=0;j<l_height;++j) {
1763                                 for (i = 0; i < l_width; ++i) {
1764                                         *l_current_ptr = opj_int_clamp(*l_current_ptr + l_tccp->m_dc_level_shift, l_min, l_max);
1765                                         ++l_current_ptr;
1766                                 }
1767                                 l_current_ptr += l_stride;
1768                         }
1769                 }
1770                 else {
1771                         for (j=0;j<l_height;++j) {
1772                                 for (i = 0; i < l_width; ++i) {
1773                                         OPJ_FLOAT32 l_value = *((OPJ_FLOAT32 *) l_current_ptr);
1774                                         *l_current_ptr = opj_int_clamp((OPJ_INT32)lrintf(l_value) + l_tccp->m_dc_level_shift, l_min, l_max); ;
1775                                         ++l_current_ptr;
1776                                 }
1777                                 l_current_ptr += l_stride;
1778                         }
1779                 }
1780
1781                 ++l_img_comp;
1782                 ++l_tccp;
1783                 ++l_tile_comp;
1784         }
1785
1786         return OPJ_TRUE;
1787 }
1788
1789
1790
1791 /**
1792  * Deallocates the encoding data of the given precinct.
1793  */
1794 void opj_tcd_code_block_dec_deallocate (opj_tcd_precinct_t * p_precinct)
1795 {
1796         OPJ_UINT32 cblkno , l_nb_code_blocks;
1797
1798         opj_tcd_cblk_dec_t * l_code_block = p_precinct->cblks.dec;
1799         if (l_code_block) {
1800                 /*fprintf(stderr,"deallocate codeblock:{\n");*/
1801                 /*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);*/
1802                 /*fprintf(stderr,"\t numbps=%d, numlenbits=%d, len=%d, numnewpasses=%d, real_num_segs=%d, m_current_max_segs=%d\n ",
1803                                 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 );*/
1804
1805
1806                 l_nb_code_blocks = p_precinct->block_size / sizeof(opj_tcd_cblk_dec_t);
1807                 /*fprintf(stderr,"nb_code_blocks =%d\t}\n", l_nb_code_blocks);*/
1808
1809                 for (cblkno = 0; cblkno < l_nb_code_blocks; ++cblkno) {
1810
1811                         if (l_code_block->data) {
1812                                 opj_free(l_code_block->data);
1813                                 l_code_block->data = 00;
1814                         }
1815
1816                         if (l_code_block->segs) {
1817                                 opj_free(l_code_block->segs );
1818                                 l_code_block->segs = 00;
1819                         }
1820
1821                         ++l_code_block;
1822                 }
1823
1824                 opj_free(p_precinct->cblks.dec);
1825                 p_precinct->cblks.dec = 00;
1826         }
1827 }
1828
1829 /**
1830  * Deallocates the encoding data of the given precinct.
1831  */
1832 void opj_tcd_code_block_enc_deallocate (opj_tcd_precinct_t * p_precinct)
1833 {       
1834         OPJ_UINT32 cblkno , l_nb_code_blocks;
1835
1836         opj_tcd_cblk_enc_t * l_code_block = p_precinct->cblks.enc;
1837         if (l_code_block) {
1838                 l_nb_code_blocks = p_precinct->block_size / sizeof(opj_tcd_cblk_enc_t);
1839                 
1840                 for     (cblkno = 0; cblkno < l_nb_code_blocks; ++cblkno)  {
1841                         if (l_code_block->data) {
1842                                 opj_free(l_code_block->data - 1);
1843                                 l_code_block->data = 00;
1844                         }
1845
1846                         if (l_code_block->layers) {
1847                                 opj_free(l_code_block->layers );
1848                                 l_code_block->layers = 00;
1849                         }
1850
1851                         if (l_code_block->passes) {
1852                                 opj_free(l_code_block->passes );
1853                                 l_code_block->passes = 00;
1854                         }
1855                         ++l_code_block;
1856                 }
1857
1858                 opj_free(p_precinct->cblks.enc);
1859                 
1860                 p_precinct->cblks.enc = 00;
1861         }
1862 }
1863
1864 OPJ_UINT32 opj_tcd_get_encoded_tile_size ( opj_tcd_t *p_tcd )
1865 {
1866         OPJ_UINT32 i,l_data_size = 0;
1867         opj_image_comp_t * l_img_comp = 00;
1868         opj_tcd_tilecomp_t * l_tilec = 00;
1869         OPJ_UINT32 l_size_comp, l_remaining;
1870
1871         l_tilec = p_tcd->tcd_image->tiles->comps;
1872         l_img_comp = p_tcd->image->comps;
1873         for (i=0;i<p_tcd->image->numcomps;++i) {
1874                 l_size_comp = l_img_comp->prec >> 3; /*(/ 8)*/
1875                 l_remaining = l_img_comp->prec & 7;  /* (%8) */
1876
1877                 if (l_remaining) {
1878                         ++l_size_comp;
1879                 }
1880
1881                 if (l_size_comp == 3) {
1882                         l_size_comp = 4;
1883                 }
1884
1885                 l_data_size += l_size_comp * (OPJ_UINT32)((l_tilec->x1 - l_tilec->x0) * (l_tilec->y1 - l_tilec->y0));
1886                 ++l_img_comp;
1887                 ++l_tilec;
1888         }
1889
1890         return l_data_size;
1891 }
1892                 
1893 OPJ_BOOL opj_tcd_dc_level_shift_encode ( opj_tcd_t *p_tcd )
1894 {
1895         OPJ_UINT32 compno;
1896         opj_tcd_tilecomp_t * l_tile_comp = 00;
1897         opj_tccp_t * l_tccp = 00;
1898         opj_image_comp_t * l_img_comp = 00;
1899         opj_tcd_tile_t * l_tile;
1900         OPJ_UINT32 l_nb_elem,i;
1901         OPJ_INT32 * l_current_ptr;
1902
1903         l_tile = p_tcd->tcd_image->tiles;
1904         l_tile_comp = l_tile->comps;
1905         l_tccp = p_tcd->tcp->tccps;
1906         l_img_comp = p_tcd->image->comps;
1907
1908         for (compno = 0; compno < l_tile->numcomps; compno++) {
1909                 l_current_ptr = l_tile_comp->data;
1910                 l_nb_elem = (OPJ_UINT32)((l_tile_comp->x1 - l_tile_comp->x0) * (l_tile_comp->y1 - l_tile_comp->y0));
1911
1912                 if (l_tccp->qmfbid == 1) {
1913                         for     (i = 0; i < l_nb_elem; ++i) {
1914                                 *l_current_ptr -= l_tccp->m_dc_level_shift ;
1915                                 ++l_current_ptr;
1916                         }
1917                 }
1918                 else {
1919                         for (i = 0; i < l_nb_elem; ++i) {
1920                                 *l_current_ptr = (*l_current_ptr - l_tccp->m_dc_level_shift) << 11 ;
1921                                 ++l_current_ptr;
1922                         }
1923                 }
1924
1925                 ++l_img_comp;
1926                 ++l_tccp;
1927                 ++l_tile_comp;
1928         }
1929
1930         return OPJ_TRUE;
1931 }
1932
1933 OPJ_BOOL opj_tcd_mct_encode ( opj_tcd_t *p_tcd )
1934 {
1935         opj_tcd_tile_t * l_tile = p_tcd->tcd_image->tiles;
1936         opj_tcd_tilecomp_t * l_tile_comp = p_tcd->tcd_image->tiles->comps;
1937         OPJ_UINT32 samples = (OPJ_UINT32)((l_tile_comp->x1 - l_tile_comp->x0) * (l_tile_comp->y1 - l_tile_comp->y0));
1938         OPJ_UINT32 i;
1939         OPJ_BYTE ** l_data = 00;
1940         opj_tcp_t * l_tcp = p_tcd->tcp;
1941
1942         if(!p_tcd->tcp->mct) {
1943                 return OPJ_TRUE;
1944         }
1945
1946         if (p_tcd->tcp->mct == 2) {
1947                 if (! p_tcd->tcp->m_mct_coding_matrix) {
1948                         return OPJ_TRUE;
1949                 }
1950
1951         l_data = (OPJ_BYTE **) opj_malloc(l_tile->numcomps*sizeof(OPJ_BYTE*));
1952                 if (! l_data) {
1953                         return OPJ_FALSE;
1954                 }
1955
1956                 for (i=0;i<l_tile->numcomps;++i) {
1957                         l_data[i] = (OPJ_BYTE*) l_tile_comp->data;
1958                         ++l_tile_comp;
1959                 }
1960
1961                 if (! opj_mct_encode_custom(/* MCT data */
1962                                         (OPJ_BYTE*) p_tcd->tcp->m_mct_coding_matrix,
1963                                         /* size of components */
1964                                         samples,
1965                                         /* components */
1966                                         l_data,
1967                                         /* nb of components (i.e. size of pData) */
1968                                         l_tile->numcomps,
1969                                         /* tells if the data is signed */
1970                                         p_tcd->image->comps->sgnd) )
1971                 {
1972             opj_free(l_data);
1973                         return OPJ_FALSE;
1974                 }
1975
1976                 opj_free(l_data);
1977         }
1978         else if (l_tcp->tccps->qmfbid == 0) {
1979                 opj_mct_encode_real(l_tile->comps[0].data, l_tile->comps[1].data, l_tile->comps[2].data, samples);
1980         }
1981         else {
1982                 opj_mct_encode(l_tile->comps[0].data, l_tile->comps[1].data, l_tile->comps[2].data, samples);
1983         }
1984
1985         return OPJ_TRUE;
1986 }
1987
1988 OPJ_BOOL opj_tcd_dwt_encode ( opj_tcd_t *p_tcd )
1989 {
1990         opj_tcd_tile_t * l_tile = p_tcd->tcd_image->tiles;
1991         opj_tcd_tilecomp_t * l_tile_comp = p_tcd->tcd_image->tiles->comps;
1992         opj_tccp_t * l_tccp = p_tcd->tcp->tccps;
1993         OPJ_UINT32 compno;
1994
1995         for (compno = 0; compno < l_tile->numcomps; ++compno) {
1996                 if (l_tccp->qmfbid == 1) {
1997                         if (! opj_dwt_encode(l_tile_comp)) {
1998                                 return OPJ_FALSE;
1999                         }
2000                 }
2001                 else if (l_tccp->qmfbid == 0) {
2002                         if (! opj_dwt_encode_real(l_tile_comp)) {
2003                                 return OPJ_FALSE;
2004                         }
2005                 }
2006
2007                 ++l_tile_comp;
2008                 ++l_tccp;
2009         }
2010
2011         return OPJ_TRUE;
2012 }
2013
2014 OPJ_BOOL opj_tcd_t1_encode ( opj_tcd_t *p_tcd )
2015 {
2016         opj_t1_t * l_t1;
2017         const OPJ_FLOAT64 * l_mct_norms;
2018         OPJ_UINT32 l_mct_numcomps = 0U;
2019         opj_tcp_t * l_tcp = p_tcd->tcp;
2020
2021         l_t1 = opj_t1_create(OPJ_TRUE);
2022         if (l_t1 == 00) {
2023                 return OPJ_FALSE;
2024         }
2025
2026         if (l_tcp->mct == 1) {
2027                 l_mct_numcomps = 3U;
2028                 /* irreversible encoding */
2029                 if (l_tcp->tccps->qmfbid == 0) {
2030                         l_mct_norms = opj_mct_get_mct_norms_real();
2031                 }
2032                 else {
2033                         l_mct_norms = opj_mct_get_mct_norms();
2034                 }
2035         }
2036         else {
2037                 l_mct_numcomps = p_tcd->image->numcomps;
2038                 l_mct_norms = (const OPJ_FLOAT64 *) (l_tcp->mct_norms);
2039         }
2040
2041         if (! opj_t1_encode_cblks(l_t1, p_tcd->tcd_image->tiles , l_tcp, l_mct_norms, l_mct_numcomps)) {
2042         opj_t1_destroy(l_t1);
2043                 return OPJ_FALSE;
2044         }
2045
2046         opj_t1_destroy(l_t1);
2047
2048         return OPJ_TRUE;
2049 }
2050
2051 OPJ_BOOL opj_tcd_t2_encode (opj_tcd_t *p_tcd,
2052                                                 OPJ_BYTE * p_dest_data,
2053                                                 OPJ_UINT32 * p_data_written,
2054                                                 OPJ_UINT32 p_max_dest_size,
2055                                                 opj_codestream_info_t *p_cstr_info )
2056 {
2057         opj_t2_t * l_t2;
2058
2059         l_t2 = opj_t2_create(p_tcd->image, p_tcd->cp);
2060         if (l_t2 == 00) {
2061                 return OPJ_FALSE;
2062         }
2063
2064         if (! opj_t2_encode_packets(
2065                                         l_t2,
2066                                         p_tcd->tcd_tileno,
2067                                         p_tcd->tcd_image->tiles,
2068                                         p_tcd->tcp->numlayers,
2069                                         p_dest_data,
2070                                         p_data_written,
2071                                         p_max_dest_size,
2072                                         p_cstr_info,
2073                                         p_tcd->tp_num,
2074                                         p_tcd->tp_pos,
2075                                         p_tcd->cur_pino,
2076                                         FINAL_PASS))
2077         {
2078                 opj_t2_destroy(l_t2);
2079                 return OPJ_FALSE;
2080         }
2081
2082         opj_t2_destroy(l_t2);
2083
2084         /*---------------CLEAN-------------------*/
2085         return OPJ_TRUE;
2086 }
2087
2088
2089 OPJ_BOOL opj_tcd_rate_allocate_encode(  opj_tcd_t *p_tcd,
2090                                                                             OPJ_BYTE * p_dest_data,
2091                                                                             OPJ_UINT32 p_max_dest_size,
2092                                                                             opj_codestream_info_t *p_cstr_info )
2093 {
2094         opj_cp_t * l_cp = p_tcd->cp;
2095         OPJ_UINT32 l_nb_written = 0;
2096
2097         if (p_cstr_info)  {
2098                 p_cstr_info->index_write = 0;
2099         }
2100
2101         if (l_cp->m_specific_param.m_enc.m_disto_alloc|| l_cp->m_specific_param.m_enc.m_fixed_quality)  {
2102                 /* fixed_quality */
2103                 /* Normal Rate/distortion allocation */
2104                 if (! opj_tcd_rateallocate(p_tcd, p_dest_data,&l_nb_written, p_max_dest_size, p_cstr_info)) {
2105                         return OPJ_FALSE;
2106                 }
2107         }
2108         else {
2109                 /* Fixed layer allocation */
2110                 opj_tcd_rateallocate_fixed(p_tcd);
2111         }
2112
2113         return OPJ_TRUE;
2114 }
2115
2116
2117 OPJ_BOOL opj_tcd_copy_tile_data (       opj_tcd_t *p_tcd,
2118                                                                     OPJ_BYTE * p_src,
2119                                                                     OPJ_UINT32 p_src_length )
2120 {
2121         OPJ_UINT32 i,j,l_data_size = 0;
2122         opj_image_comp_t * l_img_comp = 00;
2123         opj_tcd_tilecomp_t * l_tilec = 00;
2124         OPJ_UINT32 l_size_comp, l_remaining;
2125         OPJ_UINT32 l_nb_elem;
2126
2127         l_data_size = opj_tcd_get_encoded_tile_size(p_tcd);
2128         if (l_data_size != p_src_length) {
2129                 return OPJ_FALSE;
2130         }
2131
2132         l_tilec = p_tcd->tcd_image->tiles->comps;
2133         l_img_comp = p_tcd->image->comps;
2134         for (i=0;i<p_tcd->image->numcomps;++i) {
2135                 l_size_comp = l_img_comp->prec >> 3; /*(/ 8)*/
2136                 l_remaining = l_img_comp->prec & 7;  /* (%8) */
2137                 l_nb_elem = (OPJ_UINT32)((l_tilec->x1 - l_tilec->x0) * (l_tilec->y1 - l_tilec->y0));
2138
2139                 if (l_remaining) {
2140                         ++l_size_comp;
2141                 }
2142
2143                 if (l_size_comp == 3) {
2144                         l_size_comp = 4;
2145                 }
2146
2147                 switch (l_size_comp) {
2148                         case 1:
2149                                 {
2150                                         OPJ_CHAR * l_src_ptr = (OPJ_CHAR *) p_src;
2151                                         OPJ_INT32 * l_dest_ptr = l_tilec->data;
2152
2153                                         if (l_img_comp->sgnd) {
2154                                                 for (j=0;j<l_nb_elem;++j) {
2155                                                         *(l_dest_ptr++) = (OPJ_INT32) (*(l_src_ptr++));
2156                                                 }
2157                                         }
2158                                         else {
2159                                                 for (j=0;j<l_nb_elem;++j) {
2160                                                         *(l_dest_ptr++) = (*(l_src_ptr++))&0xff;
2161                                                 }
2162                                         }
2163
2164                                         p_src = (OPJ_BYTE*) l_src_ptr;
2165                                 }
2166                                 break;
2167                         case 2:
2168                                 {
2169                                         OPJ_INT32 * l_dest_ptr = l_tilec->data;
2170                                         OPJ_INT16 * l_src_ptr = (OPJ_INT16 *) p_src;
2171
2172                                         if (l_img_comp->sgnd) {
2173                                                 for (j=0;j<l_nb_elem;++j) {
2174                                                         *(l_dest_ptr++) = (OPJ_INT32) (*(l_src_ptr++));
2175                                                 }
2176                                         }
2177                                         else {
2178                                                 for (j=0;j<l_nb_elem;++j) {
2179                                                         *(l_dest_ptr++) = (*(l_src_ptr++))&0xffff;
2180                                                 }
2181                                         }
2182
2183                                         p_src = (OPJ_BYTE*) l_src_ptr;
2184                                 }
2185                                 break;
2186                         case 4:
2187                                 {
2188                                         OPJ_INT32 * l_src_ptr = (OPJ_INT32 *) p_src;
2189                                         OPJ_INT32 * l_dest_ptr = l_tilec->data;
2190
2191                                         for (j=0;j<l_nb_elem;++j) {
2192                                                 *(l_dest_ptr++) = (OPJ_INT32) (*(l_src_ptr++));
2193                                         }
2194
2195                                         p_src = (OPJ_BYTE*) l_src_ptr;
2196                                 }
2197                                 break;
2198                 }
2199
2200                 ++l_img_comp;
2201                 ++l_tilec;
2202         }
2203
2204         return OPJ_TRUE;
2205 }