Upstream version 9.38.198.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                         if (l_pdx == 0 || l_pdy == 0)                                                                                                                                                                \
779                             return OPJ_FALSE;                                                                                                                                                                     \
780                         /*fprintf(stderr, "\t\t\tpdx=%d, pdy=%d\n", l_pdx, l_pdy);*/                                                                                                                              \
781                         /* p. 64, B.6, ISO/IEC FDIS15444-1 : 2000 (18 august 2000)  */                                                                                                                            \
782                         l_tl_prc_x_start = opj_int_floordivpow2(l_res->x0, (OPJ_INT32)l_pdx) << l_pdx;                                                                                                                           \
783                         l_tl_prc_y_start = opj_int_floordivpow2(l_res->y0, (OPJ_INT32)l_pdy) << l_pdy;                                                                                                                           \
784                         l_br_prc_x_end = opj_int_ceildivpow2(l_res->x1, (OPJ_INT32)l_pdx) << l_pdx;                                                                                                                              \
785                         l_br_prc_y_end = opj_int_ceildivpow2(l_res->y1, (OPJ_INT32)l_pdy) << l_pdy;                                                                                                                              \
786                         /*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 );*/                    \
787                                                                                                                                                                                                                   \
788                         l_res->pw = (l_res->x0 == l_res->x1) ? 0 : (OPJ_UINT32)((l_br_prc_x_end - l_tl_prc_x_start) >> l_pdx);                                                                                                \
789                         l_res->ph = (l_res->y0 == l_res->y1) ? 0 : (OPJ_UINT32)((l_br_prc_y_end - l_tl_prc_y_start) >> l_pdy);                                                                                                \
790                         /*fprintf(stderr, "\t\t\tres_pw=%d, res_ph=%d\n", l_res->pw, l_res->ph );*/                                                                                                               \
791                                                                                                                                                                                                                   \
792                         l_nb_precincts = l_res->pw * l_res->ph;                                                                                                                                                   \
793                         l_nb_precinct_size = l_nb_precincts * (OPJ_UINT32)sizeof(opj_tcd_precinct_t);                                                                                                                      \
794                         if (resno == 0) {                                                                                                                                                                         \
795                                 tlcbgxstart = l_tl_prc_x_start;                                                                                                                                                   \
796                                 tlcbgystart = l_tl_prc_y_start;                                                                                                                                                   \
797                                 /*brcbgxend = l_br_prc_x_end;*/                                                                                                                                                       \
798                                /* brcbgyend = l_br_prc_y_end;*/                                                                                                                                                       \
799                                 cbgwidthexpn = l_pdx;                                                                                                                                                             \
800                                 cbgheightexpn = l_pdy;                                                                                                                                                            \
801                                 l_res->numbands = 1;                                                                                                                                                              \
802                         }                                                                                                                                                                                         \
803                         else {                                                                                                                                                                                    \
804                                 tlcbgxstart = opj_int_ceildivpow2(l_tl_prc_x_start, 1);                                                                                                                               \
805                                 tlcbgystart = opj_int_ceildivpow2(l_tl_prc_y_start, 1);                                                                                                                               \
806                                 /*brcbgxend = opj_int_ceildivpow2(l_br_prc_x_end, 1);*/                                                                                                                            \
807                                 /*brcbgyend = opj_int_ceildivpow2(l_br_prc_y_end, 1);*/                                                                                                                            \
808                                 cbgwidthexpn = l_pdx - 1;                                                                                                                                                         \
809                                 cbgheightexpn = l_pdy - 1;                                                                                                                                                        \
810                                 l_res->numbands = 3;                                                                                                                                                              \
811                         }                                                                                                                                                                                         \
812                                                                                                                                                                                                                   \
813                         cblkwidthexpn = opj_uint_min(l_tccp->cblkw, cbgwidthexpn);                                                                                                                                    \
814                         cblkheightexpn = opj_uint_min(l_tccp->cblkh, cbgheightexpn);                                                                                                                                  \
815                         l_band = l_res->bands;                                                                                                                                                                    \
816                                                                                                                                                                                                                   \
817                         for (bandno = 0; bandno < l_res->numbands; ++bandno) {                                                                                                                                    \
818                                 OPJ_INT32 numbps;                                                                                                                                                                 \
819                                 /*fprintf(stderr, "\t\t\tband_no=%d/%d\n", bandno, l_res->numbands );*/                                                                                                           \
820                                                                                                                                                                                                                   \
821                                 if (resno == 0) {                                                                                                                                                                 \
822                                         l_band->bandno = 0 ;                                                                                                                                                      \
823                                         l_band->x0 = opj_int_ceildivpow2(l_tilec->x0, (OPJ_INT32)l_level_no);                                                                                                                    \
824                                         l_band->y0 = opj_int_ceildivpow2(l_tilec->y0, (OPJ_INT32)l_level_no);                                                                                                                    \
825                                         l_band->x1 = opj_int_ceildivpow2(l_tilec->x1, (OPJ_INT32)l_level_no);                                                                                                                    \
826                                         l_band->y1 = opj_int_ceildivpow2(l_tilec->y1, (OPJ_INT32)l_level_no);                                                                                                                    \
827                                 }                                                                                                                                                                                 \
828                                 else {                                                                                                                                                                            \
829                                         l_band->bandno = bandno + 1;                                                                                                                                              \
830                                         /* x0b = 1 if bandno = 1 or 3 */                                                                                                                                          \
831                                         l_x0b = l_band->bandno&1;                                                                                                                                                 \
832                                         /* y0b = 1 if bandno = 2 or 3 */                                                                                                                                          \
833                                         l_y0b = (OPJ_INT32)((l_band->bandno)>>1);                                                                                                                                              \
834                                         /* l_band border (global) */                                                                                                                                              \
835                                         l_band->x0 = opj_int_ceildivpow2(l_tilec->x0 - (1 << l_level_no) * l_x0b, (OPJ_INT32)(l_level_no + 1));                                                                                    \
836                                         l_band->y0 = opj_int_ceildivpow2(l_tilec->y0 - (1 << l_level_no) * l_y0b, (OPJ_INT32)(l_level_no + 1));                                                                                    \
837                                         l_band->x1 = opj_int_ceildivpow2(l_tilec->x1 - (1 << l_level_no) * l_x0b, (OPJ_INT32)(l_level_no + 1));                                                                                    \
838                                         l_band->y1 = opj_int_ceildivpow2(l_tilec->y1 - (1 << l_level_no) * l_y0b, (OPJ_INT32)(l_level_no + 1));                                                                                    \
839                                 }                                                                                                                                                                                 \
840                                                                                                                                                                                                                   \
841                                 /** avoid an if with storing function pointer */                                                                                                                                  \
842                                 l_gain = (*l_gain_ptr) (l_band->bandno);                                                                                                                                          \
843                                 numbps = (OPJ_INT32)(l_image_comp->prec + l_gain);                                                                                                                                             \
844                                 l_band->stepsize = (OPJ_FLOAT32)(((1.0 + l_step_size->mant / 2048.0) * pow(2.0, (OPJ_INT32) (numbps - l_step_size->expn)))) * FRACTION;                                           \
845                                 l_band->numbps = l_step_size->expn + (OPJ_INT32)l_tccp->numgbits - 1;      /* WHY -1 ? */                                                                                                    \
846                                                                                                                                                                                                                   \
847                                 if (! l_band->precincts) {                                                                                                                                                        \
848                                         l_band->precincts = (opj_tcd_precinct_t *) opj_malloc( /*3 * */ l_nb_precinct_size);                                                                                   \
849                                         if (! l_band->precincts) {                                                                                                                                                \
850                                                 return OPJ_FALSE;                                                                                                                                                 \
851                                         }                                                                                                                                                                         \
852                                         /*fprintf(stderr, "\t\t\t\tAllocate precincts of a band (opj_tcd_precinct_t): %d\n",l_nb_precinct_size);     */                                                        \
853                                         memset(l_band->precincts,0,l_nb_precinct_size);                                                                                                                           \
854                                         l_band->precincts_data_size = l_nb_precinct_size;                                                                                                                         \
855                                 }                                                                                                                                                                                 \
856                                 else if (l_band->precincts_data_size < l_nb_precinct_size) {                                                                                                                      \
857                                                                                                                                                                                                                   \
858                                         opj_tcd_precinct_t * new_precincts = (opj_tcd_precinct_t *) opj_realloc(l_band->precincts,/*3 * */ l_nb_precinct_size);                                             \
859                                         if (! new_precincts) {                                                                                                                                                    \
860                                                 /* opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to handle band precints\n");                                                                    */   \
861                                                 fprintf(stderr, "Not enough memory to handle band precints\n");                                                                                                   \
862                                                 opj_free(l_band->precincts);                                                                                                                                      \
863                                                 l_band->precincts = NULL;                                                                                                                                         \
864                                                 l_band->precincts_data_size = 0;                                                                                                                                  \
865                                                 return OPJ_FALSE;                                                                                                                                                 \
866                                         }                                                                                                                                                                         \
867                                         l_band->precincts = new_precincts;                                                                                                                                        \
868                                         /*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);*/                   \
869                                         memset(((OPJ_BYTE *) l_band->precincts) + l_band->precincts_data_size,0,l_nb_precinct_size - l_band->precincts_data_size);                                                \
870                                         l_band->precincts_data_size = l_nb_precinct_size;                                                                                                                         \
871                                 }                                                                                                                                                                                 \
872                                                                                                                                                                                                                   \
873                                 l_current_precinct = l_band->precincts;                                                                                                                                           \
874                                 for     (precno = 0; precno < l_nb_precincts; ++precno) {                                                                                                                         \
875                                         OPJ_INT32 tlcblkxstart, tlcblkystart, brcblkxend, brcblkyend;                                                                                                             \
876                                         OPJ_INT32 cbgxstart = tlcbgxstart + (OPJ_INT32)(precno % l_res->pw) * (1 << cbgwidthexpn);                                                                                           \
877                                         OPJ_INT32 cbgystart = tlcbgystart + (OPJ_INT32)(precno / l_res->pw) * (1 << cbgheightexpn);                                                                                          \
878                                         OPJ_INT32 cbgxend = cbgxstart + (1 << cbgwidthexpn);                                                                                                                      \
879                                         OPJ_INT32 cbgyend = cbgystart + (1 << cbgheightexpn);                                                                                                                     \
880                                         /*fprintf(stderr, "\t precno=%d; bandno=%d, resno=%d; compno=%d\n", precno, bandno , resno, compno);*/                                                                    \
881                                         /*fprintf(stderr, "\t tlcbgxstart(=%d) + (precno(=%d) percent res->pw(=%d)) * (1 << cbgwidthexpn(=%d)) \n",tlcbgxstart,precno,l_res->pw,cbgwidthexpn);*/                  \
882                                                                                                                                                                                                                   \
883                                         /* precinct size (global) */                                                                                                                                              \
884                                         /*fprintf(stderr, "\t cbgxstart=%d, l_band->x0 = %d \n",cbgxstart, l_band->x0);*/                                                                                         \
885                                                                                                                                                                                                                   \
886                                         l_current_precinct->x0 = opj_int_max(cbgxstart, l_band->x0);                                                                                                                  \
887                                         l_current_precinct->y0 = opj_int_max(cbgystart, l_band->y0);                                                                                                                  \
888                                         l_current_precinct->x1 = opj_int_min(cbgxend, l_band->x1);                                                                                                                    \
889                                         l_current_precinct->y1 = opj_int_min(cbgyend, l_band->y1);                                                                                                                    \
890                                         /*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);*/    \
891                                                                                                                                                                                                                   \
892                                         tlcblkxstart = opj_int_floordivpow2(l_current_precinct->x0, (OPJ_INT32)cblkwidthexpn) << cblkwidthexpn;                                                                                  \
893                                         /*fprintf(stderr, "\t tlcblkxstart =%d\n",tlcblkxstart );*/                                                                                                               \
894                                         tlcblkystart = opj_int_floordivpow2(l_current_precinct->y0, (OPJ_INT32)cblkheightexpn) << cblkheightexpn;                                                                                \
895                                         /*fprintf(stderr, "\t tlcblkystart =%d\n",tlcblkystart );*/                                                                                                               \
896                                         brcblkxend = opj_int_ceildivpow2(l_current_precinct->x1, (OPJ_INT32)cblkwidthexpn) << cblkwidthexpn;                                                                                     \
897                                         /*fprintf(stderr, "\t brcblkxend =%d\n",brcblkxend );*/                                                                                                                   \
898                                         brcblkyend = opj_int_ceildivpow2(l_current_precinct->y1, (OPJ_INT32)cblkheightexpn) << cblkheightexpn;                                                                                   \
899                                         /*fprintf(stderr, "\t brcblkyend =%d\n",brcblkyend );*/                                                                                                                   \
900                                         l_current_precinct->cw = (OPJ_UINT32)((brcblkxend - tlcblkxstart) >> cblkwidthexpn);                                                                                                    \
901                                         l_current_precinct->ch = (OPJ_UINT32)((brcblkyend - tlcblkystart) >> cblkheightexpn);                                                                                                   \
902                                                                                                                                                                                                                   \
903                                         l_nb_code_blocks = l_current_precinct->cw * l_current_precinct->ch;                                                                                                       \
904                                         /*fprintf(stderr, "\t\t\t\t precinct_cw = %d x recinct_ch = %d\n",l_current_precinct->cw, l_current_precinct->ch);      */                                                \
905                                         l_nb_code_blocks_size = l_nb_code_blocks * (OPJ_UINT32)sizeof(TYPE);                                                                                                                  \
906                                                                                                                                                                                                                   \
907                                         if (! l_current_precinct->cblks.ELEMENT) {                                                                                                                                \
908                                                 l_current_precinct->cblks.ELEMENT = (TYPE*) opj_malloc(l_nb_code_blocks_size);                                                                                    \
909                                                 if (! l_current_precinct->cblks.ELEMENT ) {                                                                                                                       \
910                                                         return OPJ_FALSE;                                                                                                                                         \
911                                                 }                                                                                                                                                                 \
912                                                 /*fprintf(stderr, "\t\t\t\tAllocate cblks of a precinct (opj_tcd_cblk_dec_t): %d\n",l_nb_code_blocks_size);*/                                                  \
913                                                                                                                                                                                                                   \
914                                                 memset(l_current_precinct->cblks.ELEMENT,0,l_nb_code_blocks_size);                                                                                                \
915                                                                                                                                                                                                                   \
916                                                 l_current_precinct->block_size = l_nb_code_blocks_size;                                                                                                           \
917                                         }                                                                                                                                                                         \
918                                         else if (l_nb_code_blocks_size > l_current_precinct->block_size) {                                                                                                        \
919                                                 TYPE *new_ELEMENT = (TYPE*) opj_realloc(l_current_precinct->cblks.ELEMENT, l_nb_code_blocks_size);                                                                \
920                                                 if (! new_ELEMENT) {                                                                                                                                              \
921                                                         opj_free(l_current_precinct->cblks.ELEMENT);                                                                                                              \
922                                                         l_current_precinct->cblks.ELEMENT = NULL;                                                                                                                 \
923                                                         l_current_precinct->block_size = 0;                                                                                                                       \
924                                                         /* opj_event_msg(p_manager, EVT_ERROR, "Not enough memory for current precinct codeblock element\n");                                              */  \
925                                                         fprintf(stderr, "Not enough memory for current precinct codeblock element\n");                                                                            \
926                                                         return OPJ_FALSE;                                                                                                                                         \
927                                                 }                                                                                                                                                                 \
928                                                 l_current_precinct->cblks.ELEMENT = new_ELEMENT;                                                                                                                  \
929                                                 /*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);     */\
930                                                                                                                                                                                                                   \
931                                                 memset(((OPJ_BYTE *) l_current_precinct->cblks.ELEMENT) + l_current_precinct->block_size                                                                          \
932                                                                 ,0                                                                                                                                                \
933                                                                 ,l_nb_code_blocks_size - l_current_precinct->block_size);                                                                                         \
934                                                                                                                                                                                                                   \
935                                                 l_current_precinct->block_size = l_nb_code_blocks_size;                                                                                                           \
936                                         }                                                                                                                                                                         \
937                                                                                                                                                                                                                   \
938                                         if (! l_current_precinct->incltree) {                                                                                                                                     \
939                                                 l_current_precinct->incltree = opj_tgt_create(l_current_precinct->cw,                                                                                              \
940                                                                 l_current_precinct->ch);                                                                                                                          \
941                                         }                                                                                                                                                                         \
942                                         else{                                                                                                                                                                     \
943                                                 l_current_precinct->incltree = opj_tgt_init(l_current_precinct->incltree,                                                                                             \
944                                                                 l_current_precinct->cw,                                                                                                                           \
945                                                                 l_current_precinct->ch);                                                                                                                          \
946                                         }                                                                                                                                                                         \
947                                                                                                                                                                                                                   \
948                                         if (! l_current_precinct->incltree)     {                                                                                                                                 \
949                                                 fprintf(stderr, "WARNING: No incltree created.\n");                                                                                                               \
950                                                 /*return OPJ_FALSE;*/                                                                                                                                             \
951                                         }                                                                                                                                                                         \
952                                                                                                                                                                                                                   \
953                                         if (! l_current_precinct->imsbtree) {                                                                                                                                     \
954                                                 l_current_precinct->imsbtree = opj_tgt_create(                                                                                                                     \
955                                                                 l_current_precinct->cw,                                                                                                                           \
956                                                                 l_current_precinct->ch);                                                                                                                          \
957                                         }                                                                                                                                                                         \
958                                         else {                                                                                                                                                                    \
959                                                 l_current_precinct->imsbtree = opj_tgt_init(                                                                                                                          \
960                                                                 l_current_precinct->imsbtree,                                                                                                                     \
961                                                                 l_current_precinct->cw,                                                                                                                           \
962                                                                 l_current_precinct->ch);                                                                                                                          \
963                                         }                                                                                                                                                                         \
964                                                                                                                                                                                                                   \
965                                         if (! l_current_precinct->imsbtree) {                                                                                                                                     \
966                                                 fprintf(stderr, "WARNING: No imsbtree created.\n");                                                                                                               \
967                                                 /*return OPJ_FALSE;*/                                                                                                                                             \
968                                         }                                                                                                                                                                         \
969                                                                                                                                                                                                                   \
970                                         l_code_block = l_current_precinct->cblks.ELEMENT;                                                                                                                         \
971                                                                                                                                                                                                                   \
972                                         for (cblkno = 0; cblkno < l_nb_code_blocks; ++cblkno) {                                                                                                                   \
973                                                 OPJ_INT32 cblkxstart = tlcblkxstart + (OPJ_INT32)(cblkno % l_current_precinct->cw) * (1 << cblkwidthexpn);                                                                   \
974                                                 OPJ_INT32 cblkystart = tlcblkystart + (OPJ_INT32)(cblkno / l_current_precinct->cw) * (1 << cblkheightexpn);                                                                  \
975                                                 OPJ_INT32 cblkxend = cblkxstart + (1 << cblkwidthexpn);                                                                                                           \
976                                                 OPJ_INT32 cblkyend = cblkystart + (1 << cblkheightexpn);                                                                                                          \
977                                                                                                                                                                                                                   \
978                                                 /* code-block size (global) */                                                                                                                                    \
979                                                 l_code_block->x0 = opj_int_max(cblkxstart, l_current_precinct->x0);                                                                                                   \
980                                                 l_code_block->y0 = opj_int_max(cblkystart, l_current_precinct->y0);                                                                                                   \
981                                                 l_code_block->x1 = opj_int_min(cblkxend, l_current_precinct->x1);                                                                                                     \
982                                                 l_code_block->y1 = opj_int_min(cblkyend, l_current_precinct->y1);                                                                                                     \
983                                                                                                                                                                                                                   \
984                                                 if (! FUNCTION_ELEMENT(l_code_block)) {                                                                                                                           \
985                                                         return OPJ_FALSE;                                                                                                                                         \
986                                                 }                                                                                                                                                                 \
987                                                 ++l_code_block;                                                                                                                                                   \
988                                         }                                                                                                                                                                         \
989                                         ++l_current_precinct;                                                                                                                                                     \
990                                 } /* precno */                                                                                                                                                                    \
991                                 ++l_band;                                                                                                                                                                         \
992                                 ++l_step_size;                                                                                                                                                                    \
993                         } /* bandno */                                                                                                                                                                            \
994                         ++l_res;                                                                                                                                                                                  \
995                         --l_level_no;                                                                                                                                                                             \
996                 } /* resno */                                                                                                                                                                                     \
997                 ++l_tccp;                                                                                                                                                                                         \
998                 ++l_tilec;                                                                                                                                                                                        \
999                 ++l_image_comp;                                                                                                                                                                                   \
1000         } /* compno */                                                                                                                                                                                            \
1001         return OPJ_TRUE;                                                                                                                                                                                          \
1002 }                                                                                                                                                                                                                 \
1003
1004
1005 OPJ_MACRO_TCD_ALLOCATE(opj_tcd_init_encode_tile, opj_tcd_cblk_enc_t, 1.f, enc, opj_tcd_code_block_enc_allocate)
1006 OPJ_MACRO_TCD_ALLOCATE(opj_tcd_init_decode_tile, opj_tcd_cblk_dec_t, 0.5f, dec, opj_tcd_code_block_dec_allocate)
1007
1008 #undef OPJ_MACRO_TCD_ALLOCATE
1009
1010 /**
1011  * Allocates memory for an encoding code block.
1012  */
1013 OPJ_BOOL opj_tcd_code_block_enc_allocate (opj_tcd_cblk_enc_t * p_code_block)
1014 {
1015         if (! p_code_block->data) {
1016
1017                 p_code_block->data = (OPJ_BYTE*) opj_malloc(OPJ_J2K_DEFAULT_CBLK_DATA_SIZE*2); /*why +1 ?*/
1018                 if(! p_code_block->data) {
1019                         return OPJ_FALSE;
1020                 }
1021
1022                 p_code_block->data[0] = 0;
1023                 p_code_block->data+=1;
1024
1025                 /* no memset since data */
1026                 p_code_block->layers = (opj_tcd_layer_t*) opj_malloc(100 * sizeof(opj_tcd_layer_t));
1027                 if (! p_code_block->layers) {
1028                         return OPJ_FALSE;
1029                 }
1030
1031                 p_code_block->passes = (opj_tcd_pass_t*) opj_malloc(100 * sizeof(opj_tcd_pass_t));
1032                 if (! p_code_block->passes) {
1033                         return OPJ_FALSE;
1034                 }
1035         }
1036
1037         memset(p_code_block->layers,0,100 * sizeof(opj_tcd_layer_t));
1038         memset(p_code_block->passes,0,100 * sizeof(opj_tcd_pass_t));
1039
1040         return OPJ_TRUE;
1041 }
1042
1043 /**
1044  * Allocates memory for a decoding code block.
1045  */
1046 OPJ_BOOL opj_tcd_code_block_dec_allocate (opj_tcd_cblk_dec_t * p_code_block)
1047 {
1048         OPJ_UINT32 l_seg_size;
1049
1050         if (! p_code_block->data) {
1051
1052                 p_code_block->data = (OPJ_BYTE*) opj_malloc(OPJ_J2K_DEFAULT_CBLK_DATA_SIZE);
1053                 if (! p_code_block->data) {
1054                         return OPJ_FALSE;
1055                 }
1056                 p_code_block->data_max_size = OPJ_J2K_DEFAULT_CBLK_DATA_SIZE;
1057                 /*fprintf(stderr, "Allocate 8192 elements of code_block->data\n");*/
1058
1059                 l_seg_size = OPJ_J2K_DEFAULT_NB_SEGS * sizeof(opj_tcd_seg_t);
1060                 p_code_block->segs = (opj_tcd_seg_t *) opj_malloc(l_seg_size);
1061                 if (! p_code_block->segs) {
1062                         return OPJ_FALSE;
1063                 }
1064                 memset(p_code_block->segs,0,l_seg_size);
1065                 /*fprintf(stderr, "Allocate %d elements of code_block->data\n", OPJ_J2K_DEFAULT_NB_SEGS * sizeof(opj_tcd_seg_t));*/
1066
1067                 p_code_block->m_current_max_segs = OPJ_J2K_DEFAULT_NB_SEGS;
1068                 /*fprintf(stderr, "m_current_max_segs of code_block->data = %d\n", p_code_block->m_current_max_segs);*/
1069         }
1070         /* TODO */
1071         /*p_code_block->numsegs = 0; */
1072
1073         return OPJ_TRUE;
1074 }
1075
1076 OPJ_UINT32 opj_tcd_get_decoded_tile_size ( opj_tcd_t *p_tcd )
1077 {
1078         OPJ_UINT32 i;
1079         OPJ_UINT32 l_data_size = 0;
1080         opj_image_comp_t * l_img_comp = 00;
1081         opj_tcd_tilecomp_t * l_tile_comp = 00;
1082         opj_tcd_resolution_t * l_res = 00;
1083         OPJ_UINT32 l_size_comp, l_remaining;
1084
1085         l_tile_comp = p_tcd->tcd_image->tiles->comps;
1086         l_img_comp = p_tcd->image->comps;
1087
1088         for (i=0;i<p_tcd->image->numcomps;++i) {
1089                 l_size_comp = l_img_comp->prec >> 3; /*(/ 8)*/
1090                 l_remaining = l_img_comp->prec & 7;  /* (%8) */
1091
1092                 if(l_remaining) {
1093                         ++l_size_comp;
1094                 }
1095
1096                 if (l_size_comp == 3) {
1097                         l_size_comp = 4;
1098                 }
1099
1100                 l_res = l_tile_comp->resolutions + l_tile_comp->minimum_num_resolutions - 1;
1101                 l_data_size += l_size_comp * (OPJ_UINT32)((l_res->x1 - l_res->x0) * (l_res->y1 - l_res->y0));
1102                 ++l_img_comp;
1103                 ++l_tile_comp;
1104         }
1105
1106         return l_data_size;
1107 }
1108
1109 OPJ_BOOL opj_tcd_encode_tile(   opj_tcd_t *p_tcd,
1110                                                         OPJ_UINT32 p_tile_no,
1111                                                         OPJ_BYTE *p_dest,
1112                                                         OPJ_UINT32 * p_data_written,
1113                                                         OPJ_UINT32 p_max_length,
1114                                                         opj_codestream_info_t *p_cstr_info)
1115 {
1116
1117         if (p_tcd->cur_tp_num == 0) {
1118
1119                 p_tcd->tcd_tileno = p_tile_no;
1120                 p_tcd->tcp = &p_tcd->cp->tcps[p_tile_no];
1121
1122                 /* INDEX >> "Precinct_nb_X et Precinct_nb_Y" */
1123                 if(p_cstr_info)  {
1124                         OPJ_UINT32 l_num_packs = 0;
1125                         OPJ_UINT32 i;
1126                         opj_tcd_tilecomp_t *l_tilec_idx = &p_tcd->tcd_image->tiles->comps[0];        /* based on component 0 */
1127                         opj_tccp_t *l_tccp = p_tcd->tcp->tccps; /* based on component 0 */
1128
1129                         for (i = 0; i < l_tilec_idx->numresolutions; i++) {
1130                                 opj_tcd_resolution_t *l_res_idx = &l_tilec_idx->resolutions[i];
1131
1132                                 p_cstr_info->tile[p_tile_no].pw[i] = (int)l_res_idx->pw;
1133                                 p_cstr_info->tile[p_tile_no].ph[i] = (int)l_res_idx->ph;
1134
1135                                 l_num_packs += l_res_idx->pw * l_res_idx->ph;
1136                                 p_cstr_info->tile[p_tile_no].pdx[i] = (int)l_tccp->prcw[i];
1137                                 p_cstr_info->tile[p_tile_no].pdy[i] = (int)l_tccp->prch[i];
1138                         }
1139                         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));
1140                 }
1141                 /* << INDEX */
1142
1143                 /* FIXME _ProfStart(PGROUP_DC_SHIFT); */
1144                 /*---------------TILE-------------------*/
1145                 if (! opj_tcd_dc_level_shift_encode(p_tcd)) {
1146                         return OPJ_FALSE;
1147                 }
1148                 /* FIXME _ProfStop(PGROUP_DC_SHIFT); */
1149
1150                 /* FIXME _ProfStart(PGROUP_MCT); */
1151                 if (! opj_tcd_mct_encode(p_tcd)) {
1152                         return OPJ_FALSE;
1153                 }
1154                 /* FIXME _ProfStop(PGROUP_MCT); */
1155
1156                 /* FIXME _ProfStart(PGROUP_DWT); */
1157                 if (! opj_tcd_dwt_encode(p_tcd)) {
1158                         return OPJ_FALSE;
1159                 }
1160                 /* FIXME  _ProfStop(PGROUP_DWT); */
1161
1162                 /* FIXME  _ProfStart(PGROUP_T1); */
1163                 if (! opj_tcd_t1_encode(p_tcd)) {
1164                         return OPJ_FALSE;
1165                 }
1166                 /* FIXME _ProfStop(PGROUP_T1); */
1167
1168                 /* FIXME _ProfStart(PGROUP_RATE); */
1169                 if (! opj_tcd_rate_allocate_encode(p_tcd,p_dest,p_max_length,p_cstr_info)) {
1170                         return OPJ_FALSE;
1171                 }
1172                 /* FIXME _ProfStop(PGROUP_RATE); */
1173
1174         }
1175         /*--------------TIER2------------------*/
1176
1177         /* INDEX */
1178         if (p_cstr_info) {
1179                 p_cstr_info->index_write = 1;
1180         }
1181         /* FIXME _ProfStart(PGROUP_T2); */
1182
1183         if (! opj_tcd_t2_encode(p_tcd,p_dest,p_data_written,p_max_length,p_cstr_info)) {
1184                 return OPJ_FALSE;
1185         }
1186         /* FIXME _ProfStop(PGROUP_T2); */
1187
1188         /*---------------CLEAN-------------------*/
1189
1190         return OPJ_TRUE;
1191 }
1192
1193 OPJ_BOOL opj_tcd_decode_tile(   opj_tcd_t *p_tcd,
1194                                 OPJ_BYTE *p_src,
1195                                 OPJ_UINT32 p_max_length,
1196                                 OPJ_UINT32 p_tile_no,
1197                                 opj_codestream_index_t *p_cstr_index
1198                                 )
1199 {
1200         OPJ_UINT32 l_data_read;
1201         p_tcd->tcd_tileno = p_tile_no;
1202         p_tcd->tcp = &(p_tcd->cp->tcps[p_tile_no]);
1203
1204 #ifdef TODO_MSD /* FIXME */
1205         /* INDEX >>  */
1206         if(p_cstr_info) {
1207                 OPJ_UINT32 resno, compno, numprec = 0;
1208                 for (compno = 0; compno < (OPJ_UINT32) p_cstr_info->numcomps; compno++) {
1209                         opj_tcp_t *tcp = &p_tcd->cp->tcps[0];
1210                         opj_tccp_t *tccp = &tcp->tccps[compno];
1211                         opj_tcd_tilecomp_t *tilec_idx = &p_tcd->tcd_image->tiles->comps[compno];
1212                         for (resno = 0; resno < tilec_idx->numresolutions; resno++) {
1213                                 opj_tcd_resolution_t *res_idx = &tilec_idx->resolutions[resno];
1214                                 p_cstr_info->tile[p_tile_no].pw[resno] = res_idx->pw;
1215                                 p_cstr_info->tile[p_tile_no].ph[resno] = res_idx->ph;
1216                                 numprec += res_idx->pw * res_idx->ph;
1217                                 p_cstr_info->tile[p_tile_no].pdx[resno] = tccp->prcw[resno];
1218                                 p_cstr_info->tile[p_tile_no].pdy[resno] = tccp->prch[resno];
1219                         }
1220                 }
1221                 p_cstr_info->tile[p_tile_no].packet = (opj_packet_info_t *) opj_malloc(p_cstr_info->numlayers * numprec * sizeof(opj_packet_info_t));
1222                 p_cstr_info->packno = 0;
1223         }
1224         /* << INDEX */
1225 #endif
1226
1227         /*--------------TIER2------------------*/
1228         /* FIXME _ProfStart(PGROUP_T2); */
1229         l_data_read = 0;
1230         if (! opj_tcd_t2_decode(p_tcd, p_src, &l_data_read, p_max_length, p_cstr_index))
1231         {
1232                 return OPJ_FALSE;
1233         }
1234         /* FIXME _ProfStop(PGROUP_T2); */
1235
1236         /*------------------TIER1-----------------*/
1237
1238         /* FIXME _ProfStart(PGROUP_T1); */
1239         if
1240                 (! opj_tcd_t1_decode(p_tcd))
1241         {
1242                 return OPJ_FALSE;
1243         }
1244         /* FIXME _ProfStop(PGROUP_T1); */
1245
1246         /*----------------DWT---------------------*/
1247
1248         /* FIXME _ProfStart(PGROUP_DWT); */
1249         if
1250                 (! opj_tcd_dwt_decode(p_tcd))
1251         {
1252                 return OPJ_FALSE;
1253         }
1254         /* FIXME _ProfStop(PGROUP_DWT); */
1255
1256         /*----------------MCT-------------------*/
1257         /* FIXME _ProfStart(PGROUP_MCT); */
1258         if
1259                 (! opj_tcd_mct_decode(p_tcd))
1260         {
1261                 return OPJ_FALSE;
1262         }
1263         /* FIXME _ProfStop(PGROUP_MCT); */
1264
1265         /* FIXME _ProfStart(PGROUP_DC_SHIFT); */
1266         if
1267                 (! opj_tcd_dc_level_shift_decode(p_tcd))
1268         {
1269                 return OPJ_FALSE;
1270         }
1271         /* FIXME _ProfStop(PGROUP_DC_SHIFT); */
1272
1273
1274         /*---------------TILE-------------------*/
1275         return OPJ_TRUE;
1276 }
1277
1278 OPJ_BOOL opj_tcd_update_tile_data ( opj_tcd_t *p_tcd,
1279                                     OPJ_BYTE * p_dest,
1280                                     OPJ_UINT32 p_dest_length
1281                                     )
1282 {
1283         OPJ_UINT32 i,j,k,l_data_size = 0;
1284         opj_image_comp_t * l_img_comp = 00;
1285         opj_tcd_tilecomp_t * l_tilec = 00;
1286         opj_tcd_resolution_t * l_res;
1287         OPJ_UINT32 l_size_comp, l_remaining;
1288         OPJ_UINT32 l_stride, l_width,l_height;
1289
1290         l_data_size = opj_tcd_get_decoded_tile_size(p_tcd);
1291         if (l_data_size > p_dest_length) {
1292                 return OPJ_FALSE;
1293         }
1294
1295         l_tilec = p_tcd->tcd_image->tiles->comps;
1296         l_img_comp = p_tcd->image->comps;
1297
1298         for (i=0;i<p_tcd->image->numcomps;++i) {
1299                 l_size_comp = l_img_comp->prec >> 3; /*(/ 8)*/
1300                 l_remaining = l_img_comp->prec & 7;  /* (%8) */
1301                 l_res = l_tilec->resolutions + l_img_comp->resno_decoded;
1302                 l_width = (OPJ_UINT32)(l_res->x1 - l_res->x0);
1303                 l_height = (OPJ_UINT32)(l_res->y1 - l_res->y0);
1304                 l_stride = (OPJ_UINT32)(l_tilec->x1 - l_tilec->x0) - l_width;
1305
1306                 if (l_remaining) {
1307                         ++l_size_comp;
1308                 }
1309
1310                 if (l_size_comp == 3) {
1311                         l_size_comp = 4;
1312                 }
1313
1314                 switch (l_size_comp)
1315                         {
1316                         case 1:
1317                                 {
1318                                         OPJ_CHAR * l_dest_ptr = (OPJ_CHAR *) p_dest;
1319                                         const OPJ_INT32 * l_src_ptr = l_tilec->data;
1320
1321                                         if (l_img_comp->sgnd) {
1322                                                 for (j=0;j<l_height;++j) {
1323                                                         for (k=0;k<l_width;++k) {
1324                                                                 *(l_dest_ptr++) = (OPJ_CHAR) (*(l_src_ptr++));
1325                                                         }
1326                                                         l_src_ptr += l_stride;
1327                                                 }
1328                                         }
1329                                         else {
1330                                                 for (j=0;j<l_height;++j) {
1331                                                         for     (k=0;k<l_width;++k) {
1332                                                                 *(l_dest_ptr++) = (OPJ_CHAR) ((*(l_src_ptr++))&0xff);
1333                                                         }
1334                                                         l_src_ptr += l_stride;
1335                                                 }
1336                                         }
1337
1338                                         p_dest = (OPJ_BYTE *)l_dest_ptr;
1339                                 }
1340                                 break;
1341                         case 2:
1342                                 {
1343                                         const OPJ_INT32 * l_src_ptr = l_tilec->data;
1344                                         OPJ_INT16 * l_dest_ptr = (OPJ_INT16 *) p_dest;
1345
1346                                         if (l_img_comp->sgnd) {
1347                                                 for (j=0;j<l_height;++j) {
1348                                                         for (k=0;k<l_width;++k) {
1349                                                                 *(l_dest_ptr++) = (OPJ_INT16) (*(l_src_ptr++));
1350                                                         }
1351                                                         l_src_ptr += l_stride;
1352                                                 }
1353                                         }
1354                                         else {
1355                                                 for (j=0;j<l_height;++j) {
1356                                                         for (k=0;k<l_width;++k) {
1357                                                                 *(l_dest_ptr++) = (OPJ_INT16) ((*(l_src_ptr++))&0xffff);
1358                                                         }
1359                                                         l_src_ptr += l_stride;
1360                                                 }
1361                                         }
1362
1363                                         p_dest = (OPJ_BYTE*) l_dest_ptr;
1364                                 }
1365                                 break;
1366                         case 4:
1367                                 {
1368                                         OPJ_INT32 * l_dest_ptr = (OPJ_INT32 *) p_dest;
1369                                         OPJ_INT32 * l_src_ptr = l_tilec->data;
1370
1371                                         for (j=0;j<l_height;++j) {
1372                                                 for (k=0;k<l_width;++k) {
1373                                                         *(l_dest_ptr++) = (*(l_src_ptr++));
1374                                                 }
1375                                                 l_src_ptr += l_stride;
1376                                         }
1377
1378                                         p_dest = (OPJ_BYTE*) l_dest_ptr;
1379                                 }
1380                                 break;
1381                 }
1382
1383                 ++l_img_comp;
1384                 ++l_tilec;
1385         }
1386
1387         return OPJ_TRUE;
1388 }
1389
1390
1391
1392
1393 void opj_tcd_free_tile(opj_tcd_t *p_tcd)
1394 {
1395         OPJ_UINT32 compno, resno, bandno, precno;
1396         opj_tcd_tile_t *l_tile = 00;
1397         opj_tcd_tilecomp_t *l_tile_comp = 00;
1398         opj_tcd_resolution_t *l_res = 00;
1399         opj_tcd_band_t *l_band = 00;
1400         opj_tcd_precinct_t *l_precinct = 00;
1401         OPJ_UINT32 l_nb_resolutions, l_nb_precincts;
1402         void (* l_tcd_code_block_deallocate) (opj_tcd_precinct_t *) = 00;
1403
1404         if (! p_tcd) {
1405                 return;
1406         }
1407
1408         if (! p_tcd->tcd_image) {
1409                 return;
1410         }
1411
1412         if (p_tcd->m_is_decoder) {
1413                 l_tcd_code_block_deallocate = opj_tcd_code_block_dec_deallocate;
1414         }
1415         else {
1416                 l_tcd_code_block_deallocate = opj_tcd_code_block_enc_deallocate;
1417         }
1418
1419         l_tile = p_tcd->tcd_image->tiles;
1420         if (! l_tile) {
1421                 return;
1422         }
1423
1424         l_tile_comp = l_tile->comps;
1425
1426         for (compno = 0; compno < l_tile->numcomps; ++compno) {
1427                 l_res = l_tile_comp->resolutions;
1428                 if (l_res) {
1429
1430                         l_nb_resolutions = l_tile_comp->resolutions_size / sizeof(opj_tcd_resolution_t);
1431                         for (resno = 0; resno < l_nb_resolutions; ++resno) {
1432                                 l_band = l_res->bands;
1433                                 for     (bandno = 0; bandno < 3; ++bandno) {
1434                                         l_precinct = l_band->precincts;
1435                                         if (l_precinct) {
1436
1437                                                 l_nb_precincts = l_band->precincts_data_size / sizeof(opj_tcd_precinct_t);
1438                                                 for (precno = 0; precno < l_nb_precincts; ++precno) {
1439                                                         opj_tgt_destroy(l_precinct->incltree);
1440                                                         l_precinct->incltree = 00;
1441                                                         opj_tgt_destroy(l_precinct->imsbtree);
1442                                                         l_precinct->imsbtree = 00;
1443                                                         (*l_tcd_code_block_deallocate) (l_precinct);
1444                                                         ++l_precinct;
1445                                                 }
1446
1447                                                 opj_free(l_band->precincts);
1448                                                 l_band->precincts = 00;
1449                                         }
1450                                         ++l_band;
1451                                 } /* for (resno */
1452                                 ++l_res;
1453                         }
1454
1455                         opj_free(l_tile_comp->resolutions);
1456                         l_tile_comp->resolutions = 00;
1457                 }
1458
1459                 if (l_tile_comp->data) {
1460                         opj_free(l_tile_comp->data);
1461                         l_tile_comp->data = 00;
1462                 }
1463                 ++l_tile_comp;
1464         }
1465
1466         opj_free(l_tile->comps);
1467         l_tile->comps = 00;
1468         opj_free(p_tcd->tcd_image->tiles);
1469         p_tcd->tcd_image->tiles = 00;
1470 }
1471
1472
1473 OPJ_BOOL opj_tcd_t2_decode (opj_tcd_t *p_tcd,
1474                             OPJ_BYTE * p_src_data,
1475                             OPJ_UINT32 * p_data_read,
1476                             OPJ_UINT32 p_max_src_size,
1477                             opj_codestream_index_t *p_cstr_index
1478                             )
1479 {
1480         opj_t2_t * l_t2;
1481
1482         l_t2 = opj_t2_create(p_tcd->image, p_tcd->cp);
1483         if (l_t2 == 00) {
1484                 return OPJ_FALSE;
1485         }
1486
1487         if (! opj_t2_decode_packets(
1488                                         l_t2,
1489                                         p_tcd->tcd_tileno,
1490                                         p_tcd->tcd_image->tiles,
1491                                         p_src_data,
1492                                         p_data_read,
1493                                         p_max_src_size,
1494                                         p_cstr_index)) {
1495                 opj_t2_destroy(l_t2);
1496                 return OPJ_FALSE;
1497         }
1498
1499         opj_t2_destroy(l_t2);
1500
1501         /*---------------CLEAN-------------------*/
1502         return OPJ_TRUE;
1503 }
1504
1505 OPJ_BOOL opj_tcd_t1_decode ( opj_tcd_t *p_tcd )
1506 {
1507         OPJ_UINT32 compno;
1508         opj_t1_t * l_t1;
1509         opj_tcd_tile_t * l_tile = p_tcd->tcd_image->tiles;
1510         opj_tcd_tilecomp_t* l_tile_comp = l_tile->comps;
1511         opj_tccp_t * l_tccp = p_tcd->tcp->tccps;
1512
1513
1514         l_t1 = opj_t1_create();
1515         if (l_t1 == 00) {
1516                 return OPJ_FALSE;
1517         }
1518
1519         for (compno = 0; compno < l_tile->numcomps; ++compno) {
1520                 /* The +3 is headroom required by the vectorized DWT */
1521                 if (OPJ_FALSE == opj_t1_decode_cblks(l_t1, l_tile_comp, l_tccp)) {
1522                         opj_t1_destroy(l_t1);
1523                         return OPJ_FALSE;
1524                 }
1525                 ++l_tile_comp;
1526                 ++l_tccp;
1527         }
1528
1529         opj_t1_destroy(l_t1);
1530
1531         return OPJ_TRUE;
1532 }
1533
1534
1535 OPJ_BOOL opj_tcd_dwt_decode ( opj_tcd_t *p_tcd )
1536 {
1537         OPJ_UINT32 compno;
1538         opj_tcd_tile_t * l_tile = p_tcd->tcd_image->tiles;
1539         opj_tcd_tilecomp_t * l_tile_comp = l_tile->comps;
1540         opj_tccp_t * l_tccp = p_tcd->tcp->tccps;
1541         opj_image_comp_t * l_img_comp = p_tcd->image->comps;
1542
1543         for (compno = 0; compno < l_tile->numcomps; compno++) {
1544                 /*
1545                 if (tcd->cp->reduce != 0) {
1546                         tcd->image->comps[compno].resno_decoded =
1547                                 tile->comps[compno].numresolutions - tcd->cp->reduce - 1;
1548                         if (tcd->image->comps[compno].resno_decoded < 0)
1549                         {
1550                                 return false;
1551                         }
1552                 }
1553                 numres2decode = tcd->image->comps[compno].resno_decoded + 1;
1554                 if(numres2decode > 0){
1555                 */
1556
1557                 if (l_tccp->qmfbid == 1) {
1558                         if (! opj_dwt_decode(l_tile_comp, l_img_comp->resno_decoded+1)) {
1559                                 return OPJ_FALSE;
1560                         }
1561                 }
1562                 else {
1563                         if (! opj_dwt_decode_real(l_tile_comp, l_img_comp->resno_decoded+1)) {
1564                                 return OPJ_FALSE;
1565                         }
1566                 }
1567
1568                 ++l_tile_comp;
1569                 ++l_img_comp;
1570                 ++l_tccp;
1571         }
1572
1573         return OPJ_TRUE;
1574 }
1575 OPJ_BOOL opj_tcd_mct_decode ( opj_tcd_t *p_tcd )
1576 {
1577         opj_tcd_tile_t * l_tile = p_tcd->tcd_image->tiles;
1578         opj_tcp_t * l_tcp = p_tcd->tcp;
1579         opj_tcd_tilecomp_t * l_tile_comp = l_tile->comps;
1580         OPJ_UINT32 l_samples,i;
1581
1582         if (! l_tcp->mct) {
1583                 return OPJ_TRUE;
1584         }
1585
1586         l_samples = (OPJ_UINT32)((l_tile_comp->x1 - l_tile_comp->x0) * (l_tile_comp->y1 - l_tile_comp->y0));
1587
1588         if (l_tile->numcomps >= 3 ){
1589                 /* testcase 1336.pdf.asan.47.376 */
1590                 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 ||
1591                     (l_tile->comps[1].x1 - l_tile->comps[1].x0) * (l_tile->comps[1].y1 - l_tile->comps[1].y0) < (OPJ_INT32)l_samples ||
1592                     (l_tile->comps[2].x1 - l_tile->comps[2].x0) * (l_tile->comps[2].y1 - l_tile->comps[2].y0) < (OPJ_INT32)l_samples) {
1593                         fprintf(stderr, "Tiles don't all have the same dimension. Skip the MCT step.\n");
1594                         return OPJ_FALSE;
1595                 }
1596                 else if (l_tcp->mct == 2) {
1597                         OPJ_BYTE ** l_data;
1598
1599                         if (! l_tcp->m_mct_decoding_matrix) {
1600                                 return OPJ_TRUE;
1601                         }
1602
1603                         l_data = (OPJ_BYTE **) opj_malloc(l_tile->numcomps*sizeof(OPJ_BYTE*));
1604                         if (! l_data) {
1605                                 return OPJ_FALSE;
1606                         }
1607
1608                         for (i=0;i<l_tile->numcomps;++i) {
1609                                 l_data[i] = (OPJ_BYTE*) l_tile_comp->data;
1610                                 ++l_tile_comp;
1611                         }
1612
1613                         if (! opj_mct_decode_custom(/* MCT data */
1614                                                                         (OPJ_BYTE*) l_tcp->m_mct_decoding_matrix,
1615                                                                         /* size of components */
1616                                                                         l_samples,
1617                                                                         /* components */
1618                                                                         l_data,
1619                                                                         /* nb of components (i.e. size of pData) */
1620                                                                         l_tile->numcomps,
1621                                                                         /* tells if the data is signed */
1622                                                                         p_tcd->image->comps->sgnd)) {
1623                                 opj_free(l_data);
1624                                 return OPJ_FALSE;
1625                         }
1626
1627                         opj_free(l_data);
1628                 }
1629                 else {
1630                         if (l_tcp->tccps->qmfbid == 1) {
1631                                 opj_mct_decode(     l_tile->comps[0].data,
1632                                                         l_tile->comps[1].data,
1633                                                         l_tile->comps[2].data,
1634                                                         l_samples);
1635                         }
1636                         else {
1637                             opj_mct_decode_real((OPJ_FLOAT32*)l_tile->comps[0].data,
1638                                                 (OPJ_FLOAT32*)l_tile->comps[1].data,
1639                                                 (OPJ_FLOAT32*)l_tile->comps[2].data,
1640                                                 l_samples);
1641                         }
1642                 }
1643         }
1644         else {
1645                 /* FIXME need to use opj_event_msg function */
1646                 fprintf(stderr,"Number of components (%d) is inconsistent with a MCT. Skip the MCT step.\n",l_tile->numcomps);
1647         }
1648
1649         return OPJ_TRUE;
1650 }
1651
1652
1653 OPJ_BOOL opj_tcd_dc_level_shift_decode ( opj_tcd_t *p_tcd )
1654 {
1655         OPJ_UINT32 compno;
1656         opj_tcd_tilecomp_t * l_tile_comp = 00;
1657         opj_tccp_t * l_tccp = 00;
1658         opj_image_comp_t * l_img_comp = 00;
1659         opj_tcd_resolution_t* l_res = 00;
1660         opj_tcd_tile_t * l_tile;
1661         OPJ_UINT32 l_width,l_height,i,j;
1662         OPJ_INT32 * l_current_ptr;
1663         OPJ_INT32 l_min, l_max;
1664         OPJ_UINT32 l_stride;
1665
1666         l_tile = p_tcd->tcd_image->tiles;
1667         l_tile_comp = l_tile->comps;
1668         l_tccp = p_tcd->tcp->tccps;
1669         l_img_comp = p_tcd->image->comps;
1670
1671         for (compno = 0; compno < l_tile->numcomps; compno++) {
1672                 l_res = l_tile_comp->resolutions + l_img_comp->resno_decoded;
1673                 l_width = (OPJ_UINT32)(l_res->x1 - l_res->x0);
1674                 l_height = (OPJ_UINT32)(l_res->y1 - l_res->y0);
1675                 l_stride = (OPJ_UINT32)(l_tile_comp->x1 - l_tile_comp->x0) - l_width;
1676
1677                 assert(l_height == 0 || l_width + l_stride <= l_tile_comp->data_size / l_height); /*MUPDF*/
1678
1679                 if (l_img_comp->sgnd) {
1680                         l_min = -(1 << (l_img_comp->prec - 1));
1681                         l_max = (1 << (l_img_comp->prec - 1)) - 1;
1682                 }
1683                 else {
1684             l_min = 0;
1685                         l_max = (1 << l_img_comp->prec) - 1;
1686                 }
1687
1688                 l_current_ptr = l_tile_comp->data;
1689
1690                 if (l_tccp->qmfbid == 1) {
1691                         for (j=0;j<l_height;++j) {
1692                                 for (i = 0; i < l_width; ++i) {
1693                                         *l_current_ptr = opj_int_clamp(*l_current_ptr + l_tccp->m_dc_level_shift, l_min, l_max);
1694                                         ++l_current_ptr;
1695                                 }
1696                                 l_current_ptr += l_stride;
1697                         }
1698                 }
1699                 else {
1700                         for (j=0;j<l_height;++j) {
1701                                 for (i = 0; i < l_width; ++i) {
1702                                         OPJ_FLOAT32 l_value = *((OPJ_FLOAT32 *) l_current_ptr);
1703                                         *l_current_ptr = opj_int_clamp((OPJ_INT32)lrintf(l_value) + l_tccp->m_dc_level_shift, l_min, l_max); ;
1704                                         ++l_current_ptr;
1705                                 }
1706                                 l_current_ptr += l_stride;
1707                         }
1708                 }
1709
1710                 ++l_img_comp;
1711                 ++l_tccp;
1712                 ++l_tile_comp;
1713         }
1714
1715         return OPJ_TRUE;
1716 }
1717
1718
1719
1720 /**
1721  * Deallocates the encoding data of the given precinct.
1722  */
1723 void opj_tcd_code_block_dec_deallocate (opj_tcd_precinct_t * p_precinct)
1724 {
1725         OPJ_UINT32 cblkno , l_nb_code_blocks;
1726
1727         opj_tcd_cblk_dec_t * l_code_block = p_precinct->cblks.dec;
1728         if (l_code_block) {
1729                 /*fprintf(stderr,"deallocate codeblock:{\n");*/
1730                 /*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);*/
1731                 /*fprintf(stderr,"\t numbps=%d, numlenbits=%d, len=%d, numnewpasses=%d, real_num_segs=%d, m_current_max_segs=%d\n ",
1732                                 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 );*/
1733
1734
1735                 l_nb_code_blocks = p_precinct->block_size / sizeof(opj_tcd_cblk_dec_t);
1736                 /*fprintf(stderr,"nb_code_blocks =%d\t}\n", l_nb_code_blocks);*/
1737
1738                 for (cblkno = 0; cblkno < l_nb_code_blocks; ++cblkno) {
1739
1740                         if (l_code_block->data) {
1741                                 opj_free(l_code_block->data);
1742                                 l_code_block->data = 00;
1743                         }
1744
1745                         if (l_code_block->segs) {
1746                                 opj_free(l_code_block->segs );
1747                                 l_code_block->segs = 00;
1748                         }
1749
1750                         ++l_code_block;
1751                 }
1752
1753                 opj_free(p_precinct->cblks.dec);
1754                 p_precinct->cblks.dec = 00;
1755         }
1756 }
1757
1758 /**
1759  * Deallocates the encoding data of the given precinct.
1760  */
1761 void opj_tcd_code_block_enc_deallocate (opj_tcd_precinct_t * p_precinct)
1762 {       
1763         OPJ_UINT32 cblkno , l_nb_code_blocks;
1764
1765         opj_tcd_cblk_enc_t * l_code_block = p_precinct->cblks.enc;
1766         if (l_code_block) {
1767                 l_nb_code_blocks = p_precinct->block_size / sizeof(opj_tcd_cblk_enc_t);
1768                 
1769                 for     (cblkno = 0; cblkno < l_nb_code_blocks; ++cblkno)  {
1770                         if (l_code_block->data) {
1771                                 opj_free(l_code_block->data - 1);
1772                                 l_code_block->data = 00;
1773                         }
1774
1775                         if (l_code_block->layers) {
1776                                 opj_free(l_code_block->layers );
1777                                 l_code_block->layers = 00;
1778                         }
1779
1780                         if (l_code_block->passes) {
1781                                 opj_free(l_code_block->passes );
1782                                 l_code_block->passes = 00;
1783                         }
1784                         ++l_code_block;
1785                 }
1786
1787                 opj_free(p_precinct->cblks.enc);
1788                 
1789                 p_precinct->cblks.enc = 00;
1790         }
1791 }
1792
1793 OPJ_UINT32 opj_tcd_get_encoded_tile_size ( opj_tcd_t *p_tcd )
1794 {
1795         OPJ_UINT32 i,l_data_size = 0;
1796         opj_image_comp_t * l_img_comp = 00;
1797         opj_tcd_tilecomp_t * l_tilec = 00;
1798         OPJ_UINT32 l_size_comp, l_remaining;
1799
1800         l_tilec = p_tcd->tcd_image->tiles->comps;
1801         l_img_comp = p_tcd->image->comps;
1802         for (i=0;i<p_tcd->image->numcomps;++i) {
1803                 l_size_comp = l_img_comp->prec >> 3; /*(/ 8)*/
1804                 l_remaining = l_img_comp->prec & 7;  /* (%8) */
1805
1806                 if (l_remaining) {
1807                         ++l_size_comp;
1808                 }
1809
1810                 if (l_size_comp == 3) {
1811                         l_size_comp = 4;
1812                 }
1813
1814                 l_data_size += l_size_comp * (OPJ_UINT32)((l_tilec->x1 - l_tilec->x0) * (l_tilec->y1 - l_tilec->y0));
1815                 ++l_img_comp;
1816                 ++l_tilec;
1817         }
1818
1819         return l_data_size;
1820 }
1821                 
1822 OPJ_BOOL opj_tcd_dc_level_shift_encode ( opj_tcd_t *p_tcd )
1823 {
1824         OPJ_UINT32 compno;
1825         opj_tcd_tilecomp_t * l_tile_comp = 00;
1826         opj_tccp_t * l_tccp = 00;
1827         opj_image_comp_t * l_img_comp = 00;
1828         opj_tcd_tile_t * l_tile;
1829         OPJ_UINT32 l_nb_elem,i;
1830         OPJ_INT32 * l_current_ptr;
1831
1832         l_tile = p_tcd->tcd_image->tiles;
1833         l_tile_comp = l_tile->comps;
1834         l_tccp = p_tcd->tcp->tccps;
1835         l_img_comp = p_tcd->image->comps;
1836
1837         for (compno = 0; compno < l_tile->numcomps; compno++) {
1838                 l_current_ptr = l_tile_comp->data;
1839                 l_nb_elem = (OPJ_UINT32)((l_tile_comp->x1 - l_tile_comp->x0) * (l_tile_comp->y1 - l_tile_comp->y0));
1840
1841                 if (l_tccp->qmfbid == 1) {
1842                         for     (i = 0; i < l_nb_elem; ++i) {
1843                                 *l_current_ptr -= l_tccp->m_dc_level_shift ;
1844                                 ++l_current_ptr;
1845                         }
1846                 }
1847                 else {
1848                         for (i = 0; i < l_nb_elem; ++i) {
1849                                 *l_current_ptr = (*l_current_ptr - l_tccp->m_dc_level_shift) << 11 ;
1850                                 ++l_current_ptr;
1851                         }
1852                 }
1853
1854                 ++l_img_comp;
1855                 ++l_tccp;
1856                 ++l_tile_comp;
1857         }
1858
1859         return OPJ_TRUE;
1860 }
1861
1862 OPJ_BOOL opj_tcd_mct_encode ( opj_tcd_t *p_tcd )
1863 {
1864         opj_tcd_tile_t * l_tile = p_tcd->tcd_image->tiles;
1865         opj_tcd_tilecomp_t * l_tile_comp = p_tcd->tcd_image->tiles->comps;
1866         OPJ_UINT32 samples = (OPJ_UINT32)((l_tile_comp->x1 - l_tile_comp->x0) * (l_tile_comp->y1 - l_tile_comp->y0));
1867         OPJ_UINT32 i;
1868         OPJ_BYTE ** l_data = 00;
1869         opj_tcp_t * l_tcp = p_tcd->tcp;
1870
1871         if(!p_tcd->tcp->mct) {
1872                 return OPJ_TRUE;
1873         }
1874
1875         if (p_tcd->tcp->mct == 2) {
1876                 if (! p_tcd->tcp->m_mct_coding_matrix) {
1877                         return OPJ_TRUE;
1878                 }
1879
1880         l_data = (OPJ_BYTE **) opj_malloc(l_tile->numcomps*sizeof(OPJ_BYTE*));
1881                 if (! l_data) {
1882                         return OPJ_FALSE;
1883                 }
1884
1885                 for (i=0;i<l_tile->numcomps;++i) {
1886                         l_data[i] = (OPJ_BYTE*) l_tile_comp->data;
1887                         ++l_tile_comp;
1888                 }
1889
1890                 if (! opj_mct_encode_custom(/* MCT data */
1891                                         (OPJ_BYTE*) p_tcd->tcp->m_mct_coding_matrix,
1892                                         /* size of components */
1893                                         samples,
1894                                         /* components */
1895                                         l_data,
1896                                         /* nb of components (i.e. size of pData) */
1897                                         l_tile->numcomps,
1898                                         /* tells if the data is signed */
1899                                         p_tcd->image->comps->sgnd) )
1900                 {
1901             opj_free(l_data);
1902                         return OPJ_FALSE;
1903                 }
1904
1905                 opj_free(l_data);
1906         }
1907         else if (l_tcp->tccps->qmfbid == 0) {
1908                 opj_mct_encode_real(l_tile->comps[0].data, l_tile->comps[1].data, l_tile->comps[2].data, samples);
1909         }
1910         else {
1911                 opj_mct_encode(l_tile->comps[0].data, l_tile->comps[1].data, l_tile->comps[2].data, samples);
1912         }
1913
1914         return OPJ_TRUE;
1915 }
1916
1917 OPJ_BOOL opj_tcd_dwt_encode ( opj_tcd_t *p_tcd )
1918 {
1919         opj_tcd_tile_t * l_tile = p_tcd->tcd_image->tiles;
1920         opj_tcd_tilecomp_t * l_tile_comp = p_tcd->tcd_image->tiles->comps;
1921         opj_tccp_t * l_tccp = p_tcd->tcp->tccps;
1922         OPJ_UINT32 compno;
1923
1924         for (compno = 0; compno < l_tile->numcomps; ++compno) {
1925                 if (l_tccp->qmfbid == 1) {
1926                         if (! opj_dwt_encode(l_tile_comp)) {
1927                                 return OPJ_FALSE;
1928                         }
1929                 }
1930                 else if (l_tccp->qmfbid == 0) {
1931                         if (! opj_dwt_encode_real(l_tile_comp)) {
1932                                 return OPJ_FALSE;
1933                         }
1934                 }
1935
1936                 ++l_tile_comp;
1937                 ++l_tccp;
1938         }
1939
1940         return OPJ_TRUE;
1941 }
1942
1943 OPJ_BOOL opj_tcd_t1_encode ( opj_tcd_t *p_tcd )
1944 {
1945         opj_t1_t * l_t1;
1946         const OPJ_FLOAT64 * l_mct_norms;
1947         opj_tcp_t * l_tcp = p_tcd->tcp;
1948
1949         l_t1 = opj_t1_create();
1950         if (l_t1 == 00) {
1951                 return OPJ_FALSE;
1952         }
1953
1954         if (l_tcp->mct == 1) {
1955                 /* irreversible encoding */
1956                 if (l_tcp->tccps->qmfbid == 0) {
1957                         l_mct_norms = opj_mct_get_mct_norms_real();
1958                 }
1959                 else {
1960                         l_mct_norms = opj_mct_get_mct_norms();
1961                 }
1962         }
1963         else {
1964                 l_mct_norms = (const OPJ_FLOAT64 *) (l_tcp->mct_norms);
1965         }
1966
1967         if (! opj_t1_encode_cblks(l_t1, p_tcd->tcd_image->tiles , l_tcp, l_mct_norms)) {
1968         opj_t1_destroy(l_t1);
1969                 return OPJ_FALSE;
1970         }
1971
1972         opj_t1_destroy(l_t1);
1973
1974         return OPJ_TRUE;
1975 }
1976
1977 OPJ_BOOL opj_tcd_t2_encode (opj_tcd_t *p_tcd,
1978                                                 OPJ_BYTE * p_dest_data,
1979                                                 OPJ_UINT32 * p_data_written,
1980                                                 OPJ_UINT32 p_max_dest_size,
1981                                                 opj_codestream_info_t *p_cstr_info )
1982 {
1983         opj_t2_t * l_t2;
1984
1985         l_t2 = opj_t2_create(p_tcd->image, p_tcd->cp);
1986         if (l_t2 == 00) {
1987                 return OPJ_FALSE;
1988         }
1989
1990         if (! opj_t2_encode_packets(
1991                                         l_t2,
1992                                         p_tcd->tcd_tileno,
1993                                         p_tcd->tcd_image->tiles,
1994                                         p_tcd->tcp->numlayers,
1995                                         p_dest_data,
1996                                         p_data_written,
1997                                         p_max_dest_size,
1998                                         p_cstr_info,
1999                                         p_tcd->tp_num,
2000                                         p_tcd->tp_pos,
2001                                         p_tcd->cur_pino,
2002                                         FINAL_PASS))
2003         {
2004                 opj_t2_destroy(l_t2);
2005                 return OPJ_FALSE;
2006         }
2007
2008         opj_t2_destroy(l_t2);
2009
2010         /*---------------CLEAN-------------------*/
2011         return OPJ_TRUE;
2012 }
2013
2014
2015 OPJ_BOOL opj_tcd_rate_allocate_encode(  opj_tcd_t *p_tcd,
2016                                                                             OPJ_BYTE * p_dest_data,
2017                                                                             OPJ_UINT32 p_max_dest_size,
2018                                                                             opj_codestream_info_t *p_cstr_info )
2019 {
2020         opj_cp_t * l_cp = p_tcd->cp;
2021         OPJ_UINT32 l_nb_written = 0;
2022
2023         if (p_cstr_info)  {
2024                 p_cstr_info->index_write = 0;
2025         }
2026
2027         if (l_cp->m_specific_param.m_enc.m_disto_alloc|| l_cp->m_specific_param.m_enc.m_fixed_quality)  {
2028                 /* fixed_quality */
2029                 /* Normal Rate/distortion allocation */
2030                 if (! opj_tcd_rateallocate(p_tcd, p_dest_data,&l_nb_written, p_max_dest_size, p_cstr_info)) {
2031                         return OPJ_FALSE;
2032                 }
2033         }
2034         else {
2035                 /* Fixed layer allocation */
2036                 opj_tcd_rateallocate_fixed(p_tcd);
2037         }
2038
2039         return OPJ_TRUE;
2040 }
2041
2042
2043 OPJ_BOOL opj_tcd_copy_tile_data (       opj_tcd_t *p_tcd,
2044                                                                     OPJ_BYTE * p_src,
2045                                                                     OPJ_UINT32 p_src_length )
2046 {
2047         OPJ_UINT32 i,j,l_data_size = 0;
2048         opj_image_comp_t * l_img_comp = 00;
2049         opj_tcd_tilecomp_t * l_tilec = 00;
2050         OPJ_UINT32 l_size_comp, l_remaining;
2051         OPJ_UINT32 l_nb_elem;
2052
2053         l_data_size = opj_tcd_get_encoded_tile_size(p_tcd);
2054         if (l_data_size != p_src_length) {
2055                 return OPJ_FALSE;
2056         }
2057
2058         l_tilec = p_tcd->tcd_image->tiles->comps;
2059         l_img_comp = p_tcd->image->comps;
2060         for (i=0;i<p_tcd->image->numcomps;++i) {
2061                 l_size_comp = l_img_comp->prec >> 3; /*(/ 8)*/
2062                 l_remaining = l_img_comp->prec & 7;  /* (%8) */
2063                 l_nb_elem = (OPJ_UINT32)((l_tilec->x1 - l_tilec->x0) * (l_tilec->y1 - l_tilec->y0));
2064
2065                 if (l_remaining) {
2066                         ++l_size_comp;
2067                 }
2068
2069                 if (l_size_comp == 3) {
2070                         l_size_comp = 4;
2071                 }
2072
2073                 switch (l_size_comp) {
2074                         case 1:
2075                                 {
2076                                         OPJ_CHAR * l_src_ptr = (OPJ_CHAR *) p_src;
2077                                         OPJ_INT32 * l_dest_ptr = l_tilec->data;
2078
2079                                         if (l_img_comp->sgnd) {
2080                                                 for (j=0;j<l_nb_elem;++j) {
2081                                                         *(l_dest_ptr++) = (OPJ_INT32) (*(l_src_ptr++));
2082                                                 }
2083                                         }
2084                                         else {
2085                                                 for (j=0;j<l_nb_elem;++j) {
2086                                                         *(l_dest_ptr++) = (*(l_src_ptr++))&0xff;
2087                                                 }
2088                                         }
2089
2090                                         p_src = (OPJ_BYTE*) l_src_ptr;
2091                                 }
2092                                 break;
2093                         case 2:
2094                                 {
2095                                         OPJ_INT32 * l_dest_ptr = l_tilec->data;
2096                                         OPJ_INT16 * l_src_ptr = (OPJ_INT16 *) p_src;
2097
2098                                         if (l_img_comp->sgnd) {
2099                                                 for (j=0;j<l_nb_elem;++j) {
2100                                                         *(l_dest_ptr++) = (OPJ_INT32) (*(l_src_ptr++));
2101                                                 }
2102                                         }
2103                                         else {
2104                                                 for (j=0;j<l_nb_elem;++j) {
2105                                                         *(l_dest_ptr++) = (*(l_src_ptr++))&0xffff;
2106                                                 }
2107                                         }
2108
2109                                         p_src = (OPJ_BYTE*) l_src_ptr;
2110                                 }
2111                                 break;
2112                         case 4:
2113                                 {
2114                                         OPJ_INT32 * l_src_ptr = (OPJ_INT32 *) p_src;
2115                                         OPJ_INT32 * l_dest_ptr = l_tilec->data;
2116
2117                                         for (j=0;j<l_nb_elem;++j) {
2118                                                 *(l_dest_ptr++) = (OPJ_INT32) (*(l_src_ptr++));
2119                                         }
2120
2121                                         p_src = (OPJ_BYTE*) l_src_ptr;
2122                                 }
2123                                 break;
2124                 }
2125
2126                 ++l_img_comp;
2127                 ++l_tilec;
2128         }
2129
2130         return OPJ_TRUE;
2131 }