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