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