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