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