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