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