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