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