remove deprecated v1 style function opj_decode and opj_decode_with_info
[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 static opj_bool tcd_dc_level_shift_encode ( opj_tcd_v2_t *p_tcd );
38
39 static opj_bool tcd_mct_encode ( opj_tcd_v2_t *p_tcd );
40
41 static opj_bool tcd_dwt_encode ( opj_tcd_v2_t *p_tcd );
42
43 static opj_bool tcd_t1_encode ( opj_tcd_v2_t *p_tcd );
44
45 static opj_bool tcd_t2_encode ( opj_tcd_v2_t *p_tcd,
46                                                                 OPJ_BYTE * p_dest_data,
47                                                                 OPJ_UINT32 * p_data_written,
48                                                                 OPJ_UINT32 p_max_dest_size,
49                                                                 opj_codestream_info_t *p_cstr_info );
50
51 static opj_bool tcd_rate_allocate_encode(       opj_tcd_v2_t *p_tcd,
52                                                                                         OPJ_BYTE * p_dest_data,
53                                                                                         OPJ_UINT32 p_max_dest_size,
54                                                                                         opj_codestream_info_t *p_cstr_info );
55
56 /* ----------------------------------------------------------------------- */
57
58
59 void tcd_dump(FILE *fd, opj_tcd_t *tcd, opj_tcd_image_t * img) {
60         int tileno, compno, resno, bandno, precno;/*, cblkno;*/
61
62         fprintf(fd, "image {\n");
63         fprintf(fd, "  tw=%d, th=%d x0=%d x1=%d y0=%d y1=%d\n", 
64                 img->tw, img->th, tcd->image->x0, tcd->image->x1, tcd->image->y0, tcd->image->y1);
65
66         for (tileno = 0; tileno < img->th * img->tw; tileno++) {
67                 opj_tcd_tile_t *tile = &tcd->tcd_image->tiles[tileno];
68                 fprintf(fd, "  tile {\n");
69                 fprintf(fd, "    x0=%d, y0=%d, x1=%d, y1=%d, numcomps=%d\n",
70                         tile->x0, tile->y0, tile->x1, tile->y1, tile->numcomps);
71                 for (compno = 0; compno < tile->numcomps; compno++) {
72                         opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
73                         fprintf(fd, "    tilec {\n");
74                         fprintf(fd,
75                                 "      x0=%d, y0=%d, x1=%d, y1=%d, numresolutions=%d\n",
76                                 tilec->x0, tilec->y0, tilec->x1, tilec->y1, tilec->numresolutions);
77                         for (resno = 0; resno < tilec->numresolutions; resno++) {
78                                 opj_tcd_resolution_t *res = &tilec->resolutions[resno];
79                                 fprintf(fd, "\n   res {\n");
80                                 fprintf(fd,
81                                         "          x0=%d, y0=%d, x1=%d, y1=%d, pw=%d, ph=%d, numbands=%d\n",
82                                         res->x0, res->y0, res->x1, res->y1, res->pw, res->ph, res->numbands);
83                                 for (bandno = 0; bandno < res->numbands; bandno++) {
84                                         opj_tcd_band_t *band = &res->bands[bandno];
85                                         fprintf(fd, "        band {\n");
86                                         fprintf(fd,
87                                                 "          x0=%d, y0=%d, x1=%d, y1=%d, stepsize=%f, numbps=%d\n",
88                                                 band->x0, band->y0, band->x1, band->y1, band->stepsize, band->numbps);
89                                         for (precno = 0; precno < res->pw * res->ph; precno++) {
90                                                 opj_tcd_precinct_t *prec = &band->precincts[precno];
91                                                 fprintf(fd, "          prec {\n");
92                                                 fprintf(fd,
93                                                         "            x0=%d, y0=%d, x1=%d, y1=%d, cw=%d, ch=%d\n",
94                                                         prec->x0, prec->y0, prec->x1, prec->y1, prec->cw, prec->ch);
95                                                 /*
96                                                 for (cblkno = 0; cblkno < prec->cw * prec->ch; cblkno++) {
97                                                         opj_tcd_cblk_t *cblk = &prec->cblks[cblkno];
98                                                         fprintf(fd, "            cblk {\n");
99                                                         fprintf(fd,
100                                                                 "              x0=%d, y0=%d, x1=%d, y1=%d\n",
101                                                                 cblk->x0, cblk->y0, cblk->x1, cblk->y1);
102                                                         fprintf(fd, "            }\n");
103                                                 }
104                                                 */
105                                                 fprintf(fd, "          }\n");
106                                         }
107                                         fprintf(fd, "        }\n");
108                                 }
109                                 fprintf(fd, "      }\n");
110                         }
111                         fprintf(fd, "    }\n");
112                 }
113                 fprintf(fd, "  }\n");
114         }
115         fprintf(fd, "}\n");
116 }
117 /**
118 * Allocates memory for a decoding code block.
119 */
120 static opj_bool tcd_code_block_dec_allocate (opj_tcd_cblk_dec_v2_t * p_code_block);
121
122 /**
123  * Deallocates the decoding data of the given precinct.
124  */
125 static void tcd_code_block_dec_deallocate (opj_tcd_precinct_v2_t * p_precinct);
126
127 /**
128  * Allocates memory for an encoding code block.
129  */
130 static opj_bool tcd_code_block_enc_allocate (opj_tcd_cblk_enc_v2_t * p_code_block);
131
132 /**
133  * Deallocates the encoding data of the given precinct.
134  */
135 static void tcd_code_block_enc_deallocate (opj_tcd_precinct_v2_t * p_precinct);
136
137
138 /**
139 Free the memory allocated for encoding
140 @param tcd TCD handle
141 */
142 static void tcd_free_tile(opj_tcd_v2_t *tcd);
143
144
145 opj_bool tcd_t2_decode (
146                                         opj_tcd_v2_t *p_tcd,
147                                         OPJ_BYTE * p_src_data,
148                                         OPJ_UINT32 * p_data_read,
149                                         OPJ_UINT32 p_max_src_size,
150                                         opj_codestream_index_t *p_cstr_index
151                                         );
152
153 opj_bool tcd_t1_decode (
154                                           opj_tcd_v2_t *p_tcd
155                                          );
156
157 opj_bool tcd_dwt_decode (
158                                           opj_tcd_v2_t *p_tcd
159                                          );
160
161 opj_bool tcd_mct_decode (
162                                           opj_tcd_v2_t *p_tcd
163                                          );
164
165 opj_bool tcd_dc_level_shift_decode (
166                                                  opj_tcd_v2_t *p_tcd
167                                                  );
168
169
170 /* ----------------------------------------------------------------------- */
171
172 /**
173 Create a new TCD handle
174 */
175 opj_tcd_t* tcd_create(opj_common_ptr cinfo) {
176         /* create the tcd structure */
177         opj_tcd_t *tcd = (opj_tcd_t*)opj_malloc(sizeof(opj_tcd_t));
178         if(!tcd) return NULL;
179         tcd->cinfo = cinfo;
180         tcd->tcd_image = (opj_tcd_image_t*)opj_malloc(sizeof(opj_tcd_image_t));
181         if(!tcd->tcd_image) {
182                 opj_free(tcd);
183                 return NULL;
184         }
185
186         return tcd;
187 }
188
189 /**
190 Create a new TCD handle
191 */
192 opj_tcd_v2_t* tcd_create_v2(opj_bool p_is_decoder)
193 {
194         opj_tcd_v2_t *l_tcd = 00;
195
196         /* create the tcd structure */
197         l_tcd = (opj_tcd_v2_t*) opj_malloc(sizeof(opj_tcd_v2_t));
198         if (!l_tcd) {
199                 return 00;
200         }
201         memset(l_tcd,0,sizeof(opj_tcd_v2_t));
202
203         l_tcd->m_is_decoder = p_is_decoder ? 1 : 0;
204
205         l_tcd->tcd_image = (opj_tcd_image_v2_t*)opj_malloc(sizeof(opj_tcd_image_v2_t));
206         if (!l_tcd->tcd_image) {
207                 opj_free(l_tcd);
208                 return 00;
209         }
210         memset(l_tcd->tcd_image,0,sizeof(opj_tcd_image_v2_t));
211
212         return l_tcd;
213 }
214
215 /**
216 Destroy a previously created TCD handle
217 */
218 void tcd_destroy(opj_tcd_t *tcd) {
219         if(tcd) {
220                 opj_free(tcd->tcd_image);
221                 opj_free(tcd);
222         }
223 }
224
225 /* ----------------------------------------------------------------------- */
226
227 void tcd_malloc_encode(opj_tcd_t *tcd, opj_image_t * image, opj_cp_t * cp, int curtileno) {
228         int tileno, compno, resno, bandno, precno, cblkno;
229
230         tcd->image = image;
231         tcd->cp = cp;
232         tcd->tcd_image->tw = cp->tw;
233         tcd->tcd_image->th = cp->th;
234         tcd->tcd_image->tiles = (opj_tcd_tile_t *) opj_malloc(sizeof(opj_tcd_tile_t));
235         
236         for (tileno = 0; tileno < 1; tileno++) {
237                 opj_tcp_t *tcp = &cp->tcps[curtileno];
238                 int j;
239
240                 /* cfr p59 ISO/IEC FDIS15444-1 : 2000 (18 august 2000) */
241                 int p = curtileno % cp->tw;     /* si numerotation matricielle .. */
242                 int q = curtileno / cp->tw;     /* .. coordonnees de la tile (q,p) q pour ligne et p pour colonne */
243
244                 /* opj_tcd_tile_t *tile=&tcd->tcd_image->tiles[tileno]; */
245                 opj_tcd_tile_t *tile = tcd->tcd_image->tiles;
246
247                 /* 4 borders of the tile rescale on the image if necessary */
248                 tile->x0 = int_max(cp->tx0 + p * cp->tdx, image->x0);
249                 tile->y0 = int_max(cp->ty0 + q * cp->tdy, image->y0);
250                 tile->x1 = int_min(cp->tx0 + (p + 1) * cp->tdx, image->x1);
251                 tile->y1 = int_min(cp->ty0 + (q + 1) * cp->tdy, image->y1);
252                 tile->numcomps = image->numcomps;
253                 /* tile->PPT=image->PPT;  */
254
255                 /* Modification of the RATE >> */
256                 for (j = 0; j < tcp->numlayers; j++) {
257                         tcp->rates[j] = tcp->rates[j] ? 
258                                 cp->tp_on ? 
259                                         (((float) (tile->numcomps 
260                                         * (tile->x1 - tile->x0) 
261                                         * (tile->y1 - tile->y0)
262                                         * image->comps[0].prec))
263                                         /(tcp->rates[j] * 8 * image->comps[0].dx * image->comps[0].dy)) - (((tcd->cur_totnum_tp - 1) * 14 )/ tcp->numlayers)
264                                         :
265                                 ((float) (tile->numcomps 
266                                         * (tile->x1 - tile->x0) 
267                                         * (tile->y1 - tile->y0) 
268                                         * image->comps[0].prec))/ 
269                                         (tcp->rates[j] * 8 * image->comps[0].dx * image->comps[0].dy)
270                                         : 0;
271
272                         if (tcp->rates[j]) {
273                                 if (j && tcp->rates[j] < tcp->rates[j - 1] + 10) {
274                                         tcp->rates[j] = tcp->rates[j - 1] + 20;
275                                 } else {
276                                         if (!j && tcp->rates[j] < 30)
277                                                 tcp->rates[j] = 30;
278                                 }
279                                 
280                                 if(j == (tcp->numlayers-1)){
281                                         tcp->rates[j] = tcp->rates[j]- 2;
282                                 }
283                         }
284                 }
285                 /* << Modification of the RATE */
286                 
287                 tile->comps = (opj_tcd_tilecomp_t *) opj_malloc(image->numcomps * sizeof(opj_tcd_tilecomp_t));
288                 for (compno = 0; compno < tile->numcomps; compno++) {
289                         opj_tccp_t *tccp = &tcp->tccps[compno];
290
291                         opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
292
293                         /* border of each tile component (global) */
294                         tilec->x0 = int_ceildiv(tile->x0, image->comps[compno].dx);
295                         tilec->y0 = int_ceildiv(tile->y0, image->comps[compno].dy);
296                         tilec->x1 = int_ceildiv(tile->x1, image->comps[compno].dx);
297                         tilec->y1 = int_ceildiv(tile->y1, image->comps[compno].dy);
298                         
299                         tilec->data = (int *) opj_aligned_malloc((tilec->x1 - tilec->x0) * (tilec->y1 - tilec->y0) * sizeof(int));
300                         tilec->numresolutions = tccp->numresolutions;
301
302                         tilec->resolutions = (opj_tcd_resolution_t *) opj_malloc(tilec->numresolutions * sizeof(opj_tcd_resolution_t));
303                         
304                         for (resno = 0; resno < tilec->numresolutions; resno++) {
305                                 int pdx, pdy;
306                                 int levelno = tilec->numresolutions - 1 - resno;
307                                 int tlprcxstart, tlprcystart, brprcxend, brprcyend;
308                                 int tlcbgxstart, tlcbgystart, brcbgxend, brcbgyend;
309                                 int cbgwidthexpn, cbgheightexpn;
310                                 int cblkwidthexpn, cblkheightexpn;
311
312                                 opj_tcd_resolution_t *res = &tilec->resolutions[resno];
313                                 
314                                 /* border for each resolution level (global) */
315                                 res->x0 = int_ceildivpow2(tilec->x0, levelno);
316                                 res->y0 = int_ceildivpow2(tilec->y0, levelno);
317                                 res->x1 = int_ceildivpow2(tilec->x1, levelno);
318                                 res->y1 = int_ceildivpow2(tilec->y1, levelno);
319                                 
320                                 res->numbands = resno == 0 ? 1 : 3;
321                                 /* p. 35, table A-23, ISO/IEC FDIS154444-1 : 2000 (18 august 2000) */
322                                 if (tccp->csty & J2K_CCP_CSTY_PRT) {
323                                         pdx = tccp->prcw[resno];
324                                         pdy = tccp->prch[resno];
325                                 } else {
326                                         pdx = 15;
327                                         pdy = 15;
328                                 }
329                                 /* p. 64, B.6, ISO/IEC FDIS15444-1 : 2000 (18 august 2000)  */
330                                 tlprcxstart = int_floordivpow2(res->x0, pdx) << pdx;
331                                 tlprcystart = int_floordivpow2(res->y0, pdy) << pdy;
332                                 
333                                 brprcxend = int_ceildivpow2(res->x1, pdx) << pdx;
334                                 brprcyend = int_ceildivpow2(res->y1, pdy) << pdy;
335                                 
336                                 res->pw = (brprcxend - tlprcxstart) >> pdx;
337                                 res->ph = (brprcyend - tlprcystart) >> pdy;
338                                 
339                                 if (resno == 0) {
340                                         tlcbgxstart = tlprcxstart;
341                                         tlcbgystart = tlprcystart;
342                                         brcbgxend = brprcxend;
343                                         brcbgyend = brprcyend;
344                                         cbgwidthexpn = pdx;
345                                         cbgheightexpn = pdy;
346                                 } else {
347                                         tlcbgxstart = int_ceildivpow2(tlprcxstart, 1);
348                                         tlcbgystart = int_ceildivpow2(tlprcystart, 1);
349                                         brcbgxend = int_ceildivpow2(brprcxend, 1);
350                                         brcbgyend = int_ceildivpow2(brprcyend, 1);
351                                         cbgwidthexpn = pdx - 1;
352                                         cbgheightexpn = pdy - 1;
353                                 }
354                                 
355                                 cblkwidthexpn = int_min(tccp->cblkw, cbgwidthexpn);
356                                 cblkheightexpn = int_min(tccp->cblkh, cbgheightexpn);
357                                 
358                                 for (bandno = 0; bandno < res->numbands; bandno++) {
359                                         int x0b, y0b, i;
360                                         int gain, numbps;
361                                         opj_stepsize_t *ss = NULL;
362
363                                         opj_tcd_band_t *band = &res->bands[bandno];
364
365                                         band->bandno = resno == 0 ? 0 : bandno + 1;
366                                         x0b = (band->bandno == 1) || (band->bandno == 3) ? 1 : 0;
367                                         y0b = (band->bandno == 2) || (band->bandno == 3) ? 1 : 0;
368                                         
369                                         if (band->bandno == 0) {
370                                                 /* band border (global) */
371                                                 band->x0 = int_ceildivpow2(tilec->x0, levelno);
372                                                 band->y0 = int_ceildivpow2(tilec->y0, levelno);
373                                                 band->x1 = int_ceildivpow2(tilec->x1, levelno);
374                                                 band->y1 = int_ceildivpow2(tilec->y1, levelno);
375                                         } else {
376                                                 /* band border (global) */
377                                                 band->x0 = int_ceildivpow2(tilec->x0 - (1 << levelno) * x0b, levelno + 1);
378                                                 band->y0 = int_ceildivpow2(tilec->y0 - (1 << levelno) * y0b, levelno + 1);
379                                                 band->x1 = int_ceildivpow2(tilec->x1 - (1 << levelno) * x0b, levelno + 1);
380                                                 band->y1 = int_ceildivpow2(tilec->y1 - (1 << levelno) * y0b, levelno + 1);
381                                         }
382                                         
383                                         ss = &tccp->stepsizes[resno == 0 ? 0 : 3 * (resno - 1) + bandno + 1];
384                                         gain = tccp->qmfbid == 0 ? dwt_getgain_real(band->bandno) : dwt_getgain(band->bandno);                                  
385                                         numbps = image->comps[compno].prec + gain;
386                                         
387                                         band->stepsize = (float)((1.0 + ss->mant / 2048.0) * pow(2.0, numbps - ss->expn));
388                                         band->numbps = ss->expn + tccp->numgbits - 1;   /* WHY -1 ? */
389                                         
390                                         band->precincts = (opj_tcd_precinct_t *) opj_malloc(3 * res->pw * res->ph * sizeof(opj_tcd_precinct_t));
391                                         
392                                         for (i = 0; i < res->pw * res->ph * 3; i++) {
393                                                 band->precincts[i].imsbtree = NULL;
394                                                 band->precincts[i].incltree = NULL;
395                                                 band->precincts[i].cblks.enc = NULL;
396                                         }
397                                         
398                                         for (precno = 0; precno < res->pw * res->ph; precno++) {
399                                                 int tlcblkxstart, tlcblkystart, brcblkxend, brcblkyend;
400
401                                                 int cbgxstart = tlcbgxstart + (precno % res->pw) * (1 << cbgwidthexpn);
402                                                 int cbgystart = tlcbgystart + (precno / res->pw) * (1 << cbgheightexpn);
403                                                 int cbgxend = cbgxstart + (1 << cbgwidthexpn);
404                                                 int cbgyend = cbgystart + (1 << cbgheightexpn);
405
406                                                 opj_tcd_precinct_t *prc = &band->precincts[precno];
407
408                                                 /* precinct size (global) */
409                                                 prc->x0 = int_max(cbgxstart, band->x0);
410                                                 prc->y0 = int_max(cbgystart, band->y0);
411                                                 prc->x1 = int_min(cbgxend, band->x1);
412                                                 prc->y1 = int_min(cbgyend, band->y1);
413
414                                                 tlcblkxstart = int_floordivpow2(prc->x0, cblkwidthexpn) << cblkwidthexpn;
415                                                 tlcblkystart = int_floordivpow2(prc->y0, cblkheightexpn) << cblkheightexpn;
416                                                 brcblkxend = int_ceildivpow2(prc->x1, cblkwidthexpn) << cblkwidthexpn;
417                                                 brcblkyend = int_ceildivpow2(prc->y1, cblkheightexpn) << cblkheightexpn;
418                                                 prc->cw = (brcblkxend - tlcblkxstart) >> cblkwidthexpn;
419                                                 prc->ch = (brcblkyend - tlcblkystart) >> cblkheightexpn;
420
421                                                 prc->cblks.enc = (opj_tcd_cblk_enc_t*) opj_calloc((prc->cw * prc->ch), sizeof(opj_tcd_cblk_enc_t));
422                                                 prc->incltree = tgt_create(prc->cw, prc->ch);
423                                                 prc->imsbtree = tgt_create(prc->cw, prc->ch);
424                                                 
425                                                 for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
426                                                         int cblkxstart = tlcblkxstart + (cblkno % prc->cw) * (1 << cblkwidthexpn);
427                                                         int cblkystart = tlcblkystart + (cblkno / prc->cw) * (1 << cblkheightexpn);
428                                                         int cblkxend = cblkxstart + (1 << cblkwidthexpn);
429                                                         int cblkyend = cblkystart + (1 << cblkheightexpn);
430                                                         
431                                                         opj_tcd_cblk_enc_t* cblk = &prc->cblks.enc[cblkno];
432
433                                                         /* code-block size (global) */
434                                                         cblk->x0 = int_max(cblkxstart, prc->x0);
435                                                         cblk->y0 = int_max(cblkystart, prc->y0);
436                                                         cblk->x1 = int_min(cblkxend, prc->x1);
437                                                         cblk->y1 = int_min(cblkyend, prc->y1);
438                                                         cblk->data = (unsigned char*) opj_calloc(8192+2, sizeof(unsigned char));
439                                                         /* FIXME: mqc_init_enc and mqc_byteout underrun the buffer if we don't do this. Why? */
440                                                         cblk->data[0] = 0;
441                                                         cblk->data[1] = 0;
442                                                         cblk->data += 2;
443                                                         cblk->layers = (opj_tcd_layer_t*) opj_calloc(100, sizeof(opj_tcd_layer_t));
444                                                         cblk->passes = (opj_tcd_pass_t*) opj_calloc(100, sizeof(opj_tcd_pass_t));
445                                                 }
446                                         }
447                                 }
448                         }
449                 }
450         }
451         
452         /* tcd_dump(stdout, tcd, &tcd->tcd_image); */
453 }
454
455 void tcd_free_encode(opj_tcd_t *tcd) {
456         int tileno, compno, resno, bandno, precno, cblkno;
457
458         for (tileno = 0; tileno < 1; tileno++) {
459                 opj_tcd_tile_t *tile = tcd->tcd_image->tiles;
460
461                 for (compno = 0; compno < tile->numcomps; compno++) {
462                         opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
463
464                         for (resno = 0; resno < tilec->numresolutions; resno++) {
465                                 opj_tcd_resolution_t *res = &tilec->resolutions[resno];
466
467                                 for (bandno = 0; bandno < res->numbands; bandno++) {
468                                         opj_tcd_band_t *band = &res->bands[bandno];
469
470                                         for (precno = 0; precno < res->pw * res->ph; precno++) {
471                                                 opj_tcd_precinct_t *prc = &band->precincts[precno];
472
473                                                 if (prc->incltree != NULL) {
474                                                         tgt_destroy(prc->incltree);
475                                                         prc->incltree = NULL;
476                                                 }
477                                                 if (prc->imsbtree != NULL) {
478                                                         tgt_destroy(prc->imsbtree);     
479                                                         prc->imsbtree = NULL;
480                                                 }
481                                                 for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
482                                                         opj_free(prc->cblks.enc[cblkno].data - 2);
483                                                         opj_free(prc->cblks.enc[cblkno].layers);
484                                                         opj_free(prc->cblks.enc[cblkno].passes);
485                                                 }
486                                                 opj_free(prc->cblks.enc);
487                                         } /* for (precno */
488                                         opj_free(band->precincts);
489                                         band->precincts = NULL;
490                                 } /* for (bandno */
491                         } /* for (resno */
492                         opj_free(tilec->resolutions);
493                         tilec->resolutions = NULL;
494                 } /* for (compno */
495                 opj_free(tile->comps);
496                 tile->comps = NULL;
497         } /* for (tileno */
498         opj_free(tcd->tcd_image->tiles);
499         tcd->tcd_image->tiles = NULL;
500 }
501
502 void tcd_init_encode(opj_tcd_t *tcd, opj_image_t * image, opj_cp_t * cp, int curtileno) {
503         int tileno, compno, resno, bandno, precno, cblkno;
504
505         for (tileno = 0; tileno < 1; tileno++) {
506                 opj_tcp_t *tcp = &cp->tcps[curtileno];
507                 int j;
508                 /* cfr p59 ISO/IEC FDIS15444-1 : 2000 (18 august 2000) */
509                 int p = curtileno % cp->tw;
510                 int q = curtileno / cp->tw;
511
512                 opj_tcd_tile_t *tile = tcd->tcd_image->tiles;
513                 
514                 /* 4 borders of the tile rescale on the image if necessary */
515                 tile->x0 = int_max(cp->tx0 + p * cp->tdx, image->x0);
516                 tile->y0 = int_max(cp->ty0 + q * cp->tdy, image->y0);
517                 tile->x1 = int_min(cp->tx0 + (p + 1) * cp->tdx, image->x1);
518                 tile->y1 = int_min(cp->ty0 + (q + 1) * cp->tdy, image->y1);
519                 
520                 tile->numcomps = image->numcomps;
521                 /* tile->PPT=image->PPT; */
522
523                 /* Modification of the RATE >> */
524                 for (j = 0; j < tcp->numlayers; j++) {
525                         tcp->rates[j] = tcp->rates[j] ? 
526                                 cp->tp_on ? 
527                                         (((float) (tile->numcomps 
528                                         * (tile->x1 - tile->x0) 
529                                         * (tile->y1 - tile->y0)
530                                         * image->comps[0].prec))
531                                         /(tcp->rates[j] * 8 * image->comps[0].dx * image->comps[0].dy)) - (((tcd->cur_totnum_tp - 1) * 14 )/ tcp->numlayers)
532                                         :
533                                 ((float) (tile->numcomps 
534                                         * (tile->x1 - tile->x0) 
535                                         * (tile->y1 - tile->y0) 
536                                         * image->comps[0].prec))/ 
537                                         (tcp->rates[j] * 8 * image->comps[0].dx * image->comps[0].dy)
538                                         : 0;
539
540                         if (tcp->rates[j]) {
541                                 if (j && tcp->rates[j] < tcp->rates[j - 1] + 10) {
542                                         tcp->rates[j] = tcp->rates[j - 1] + 20;
543                                 } else {
544                                         if (!j && tcp->rates[j] < 30)
545                                                 tcp->rates[j] = 30;
546                                 }
547                         }
548                 }
549                 /* << Modification of the RATE */
550
551                 /* tile->comps=(opj_tcd_tilecomp_t*)opj_realloc(tile->comps,image->numcomps*sizeof(opj_tcd_tilecomp_t)); */
552                 for (compno = 0; compno < tile->numcomps; compno++) {
553                         opj_tccp_t *tccp = &tcp->tccps[compno];
554                         
555                         opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
556
557                         /* border of each tile component (global) */
558                         tilec->x0 = int_ceildiv(tile->x0, image->comps[compno].dx);
559                         tilec->y0 = int_ceildiv(tile->y0, image->comps[compno].dy);
560                         tilec->x1 = int_ceildiv(tile->x1, image->comps[compno].dx);
561                         tilec->y1 = int_ceildiv(tile->y1, image->comps[compno].dy);
562                         
563                         tilec->data = (int *) opj_aligned_malloc((tilec->x1 - tilec->x0) * (tilec->y1 - tilec->y0) * sizeof(int));
564                         tilec->numresolutions = tccp->numresolutions;
565                         /* tilec->resolutions=(opj_tcd_resolution_t*)opj_realloc(tilec->resolutions,tilec->numresolutions*sizeof(opj_tcd_resolution_t)); */
566                         for (resno = 0; resno < tilec->numresolutions; resno++) {
567                                 int pdx, pdy;
568
569                                 int levelno = tilec->numresolutions - 1 - resno;
570                                 int tlprcxstart, tlprcystart, brprcxend, brprcyend;
571                                 int tlcbgxstart, tlcbgystart, brcbgxend, brcbgyend;
572                                 int cbgwidthexpn, cbgheightexpn;
573                                 int cblkwidthexpn, cblkheightexpn;
574                                 
575                                 opj_tcd_resolution_t *res = &tilec->resolutions[resno];
576
577                                 /* border for each resolution level (global) */
578                                 res->x0 = int_ceildivpow2(tilec->x0, levelno);
579                                 res->y0 = int_ceildivpow2(tilec->y0, levelno);
580                                 res->x1 = int_ceildivpow2(tilec->x1, levelno);
581                                 res->y1 = int_ceildivpow2(tilec->y1, levelno);  
582                                 res->numbands = resno == 0 ? 1 : 3;
583
584                                 /* p. 35, table A-23, ISO/IEC FDIS154444-1 : 2000 (18 august 2000) */
585                                 if (tccp->csty & J2K_CCP_CSTY_PRT) {
586                                         pdx = tccp->prcw[resno];
587                                         pdy = tccp->prch[resno];
588                                 } else {
589                                         pdx = 15;
590                                         pdy = 15;
591                                 }
592                                 /* p. 64, B.6, ISO/IEC FDIS15444-1 : 2000 (18 august 2000)  */
593                                 tlprcxstart = int_floordivpow2(res->x0, pdx) << pdx;
594                                 tlprcystart = int_floordivpow2(res->y0, pdy) << pdy;
595                                 brprcxend = int_ceildivpow2(res->x1, pdx) << pdx;
596                                 brprcyend = int_ceildivpow2(res->y1, pdy) << pdy;
597                                 
598                                 res->pw = (brprcxend - tlprcxstart) >> pdx;
599                                 res->ph = (brprcyend - tlprcystart) >> pdy;
600                                 
601                                 if (resno == 0) {
602                                         tlcbgxstart = tlprcxstart;
603                                         tlcbgystart = tlprcystart;
604                                         brcbgxend = brprcxend;
605                                         brcbgyend = brprcyend;
606                                         cbgwidthexpn = pdx;
607                                         cbgheightexpn = pdy;
608                                 } else {
609                                         tlcbgxstart = int_ceildivpow2(tlprcxstart, 1);
610                                         tlcbgystart = int_ceildivpow2(tlprcystart, 1);
611                                         brcbgxend = int_ceildivpow2(brprcxend, 1);
612                                         brcbgyend = int_ceildivpow2(brprcyend, 1);
613                                         cbgwidthexpn = pdx - 1;
614                                         cbgheightexpn = pdy - 1;
615                                 }
616                                 
617                                 cblkwidthexpn = int_min(tccp->cblkw, cbgwidthexpn);
618                                 cblkheightexpn = int_min(tccp->cblkh, cbgheightexpn);
619                                 
620                                 for (bandno = 0; bandno < res->numbands; bandno++) {
621                                         int x0b, y0b;
622                                         int gain, numbps;
623                                         opj_stepsize_t *ss = NULL;
624
625                                         opj_tcd_band_t *band = &res->bands[bandno];
626
627                                         band->bandno = resno == 0 ? 0 : bandno + 1;
628                                         x0b = (band->bandno == 1) || (band->bandno == 3) ? 1 : 0;
629                                         y0b = (band->bandno == 2) || (band->bandno == 3) ? 1 : 0;
630                                         
631                                         if (band->bandno == 0) {
632                                                 /* band border */
633                                                 band->x0 = int_ceildivpow2(tilec->x0, levelno);
634                                                 band->y0 = int_ceildivpow2(tilec->y0, levelno);
635                                                 band->x1 = int_ceildivpow2(tilec->x1, levelno);
636                                                 band->y1 = int_ceildivpow2(tilec->y1, levelno);
637                                         } else {
638                                                 band->x0 = int_ceildivpow2(tilec->x0 - (1 << levelno) * x0b, levelno + 1);
639                                                 band->y0 = int_ceildivpow2(tilec->y0 - (1 << levelno) * y0b, levelno + 1);
640                                                 band->x1 = int_ceildivpow2(tilec->x1 - (1 << levelno) * x0b, levelno + 1);
641                                                 band->y1 = int_ceildivpow2(tilec->y1 - (1 << levelno) * y0b, levelno + 1);
642                                         }
643                                         
644                                         ss = &tccp->stepsizes[resno == 0 ? 0 : 3 * (resno - 1) + bandno + 1];
645                                         gain = tccp->qmfbid == 0 ? dwt_getgain_real(band->bandno) : dwt_getgain(band->bandno);
646                                         numbps = image->comps[compno].prec + gain;
647                                         band->stepsize = (float)((1.0 + ss->mant / 2048.0) * pow(2.0, numbps - ss->expn));
648                                         band->numbps = ss->expn + tccp->numgbits - 1;   /* WHY -1 ? */
649                                         
650                                         for (precno = 0; precno < res->pw * res->ph; precno++) {
651                                                 int tlcblkxstart, tlcblkystart, brcblkxend, brcblkyend;
652
653                                                 int cbgxstart = tlcbgxstart + (precno % res->pw) * (1 << cbgwidthexpn);
654                                                 int cbgystart = tlcbgystart + (precno / res->pw) * (1 << cbgheightexpn);
655                                                 int cbgxend = cbgxstart + (1 << cbgwidthexpn);
656                                                 int cbgyend = cbgystart + (1 << cbgheightexpn);
657                                                 
658                                                 opj_tcd_precinct_t *prc = &band->precincts[precno];
659
660                                                 /* precinct size (global) */
661                                                 prc->x0 = int_max(cbgxstart, band->x0);
662                                                 prc->y0 = int_max(cbgystart, band->y0);
663                                                 prc->x1 = int_min(cbgxend, band->x1);
664                                                 prc->y1 = int_min(cbgyend, band->y1);
665
666                                                 tlcblkxstart = int_floordivpow2(prc->x0, cblkwidthexpn) << cblkwidthexpn;
667                                                 tlcblkystart = int_floordivpow2(prc->y0, cblkheightexpn) << cblkheightexpn;
668                                                 brcblkxend = int_ceildivpow2(prc->x1, cblkwidthexpn) << cblkwidthexpn;
669                                                 brcblkyend = int_ceildivpow2(prc->y1, cblkheightexpn) << cblkheightexpn;
670                                                 prc->cw = (brcblkxend - tlcblkxstart) >> cblkwidthexpn;
671                                                 prc->ch = (brcblkyend - tlcblkystart) >> cblkheightexpn;
672
673                                                 opj_free(prc->cblks.enc);
674                                                 prc->cblks.enc = (opj_tcd_cblk_enc_t*) opj_calloc(prc->cw * prc->ch, sizeof(opj_tcd_cblk_enc_t));
675
676                                                 if (prc->incltree != NULL) {
677                                                         tgt_destroy(prc->incltree);
678                                                 }
679                                                 if (prc->imsbtree != NULL) {
680                                                         tgt_destroy(prc->imsbtree);
681                                                 }
682                                                 
683                                                 prc->incltree = tgt_create(prc->cw, prc->ch);
684                                                 prc->imsbtree = tgt_create(prc->cw, prc->ch);
685
686                                                 for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
687                                                         int cblkxstart = tlcblkxstart + (cblkno % prc->cw) * (1 << cblkwidthexpn);
688                                                         int cblkystart = tlcblkystart + (cblkno / prc->cw) * (1 << cblkheightexpn);
689                                                         int cblkxend = cblkxstart + (1 << cblkwidthexpn);
690                                                         int cblkyend = cblkystart + (1 << cblkheightexpn);
691
692                                                         opj_tcd_cblk_enc_t* cblk = &prc->cblks.enc[cblkno];
693
694                                                         /* code-block size (global) */
695                                                         cblk->x0 = int_max(cblkxstart, prc->x0);
696                                                         cblk->y0 = int_max(cblkystart, prc->y0);
697                                                         cblk->x1 = int_min(cblkxend, prc->x1);
698                                                         cblk->y1 = int_min(cblkyend, prc->y1);
699                                                         cblk->data = (unsigned char*) opj_calloc(8192+2, sizeof(unsigned char));
700                                                         /* FIXME: mqc_init_enc and mqc_byteout underrun the buffer if we don't do this. Why? */
701                                                         cblk->data[0] = 0;
702                                                         cblk->data[1] = 0;
703                                                         cblk->data += 2;
704                                                         cblk->layers = (opj_tcd_layer_t*) opj_calloc(100, sizeof(opj_tcd_layer_t));
705                                                         cblk->passes = (opj_tcd_pass_t*) opj_calloc(100, sizeof(opj_tcd_pass_t));
706                                                 }
707                                         } /* precno */
708                                 } /* bandno */
709                         } /* resno */
710                 } /* compno */
711         } /* tileno */
712
713         /* tcd_dump(stdout, tcd, &tcd->tcd_image); */
714 }
715
716 void tcd_malloc_decode(opj_tcd_t *tcd, opj_image_t * image, opj_cp_t * cp) {
717         int i, j, tileno, p, q;
718         unsigned int x0 = 0, y0 = 0, x1 = 0, y1 = 0, w, h;
719
720         tcd->image = image;
721         tcd->tcd_image->tw = cp->tw;
722         tcd->tcd_image->th = cp->th;
723         tcd->tcd_image->tiles = (opj_tcd_tile_t *) opj_malloc(cp->tw * cp->th * sizeof(opj_tcd_tile_t));
724
725         /* 
726         Allocate place to store the decoded data = final image
727         Place limited by the tile really present in the codestream 
728         */
729
730         for (j = 0; j < cp->tileno_size; j++) {
731                 opj_tcd_tile_t *tile;
732                 
733                 tileno = cp->tileno[j];         
734                 tile = &(tcd->tcd_image->tiles[cp->tileno[tileno]]);            
735                 tile->numcomps = image->numcomps;
736                 tile->comps = (opj_tcd_tilecomp_t*) opj_calloc(image->numcomps, sizeof(opj_tcd_tilecomp_t));
737         }
738
739         for (i = 0; i < image->numcomps; i++) {
740                 for (j = 0; j < cp->tileno_size; j++) {
741                         opj_tcd_tile_t *tile;
742                         opj_tcd_tilecomp_t *tilec;
743                         
744                         /* cfr p59 ISO/IEC FDIS15444-1 : 2000 (18 august 2000) */
745                         
746                         tileno = cp->tileno[j];
747                         
748                         tile = &(tcd->tcd_image->tiles[cp->tileno[tileno]]);
749                         tilec = &tile->comps[i];
750                         
751                         p = tileno % cp->tw;    /* si numerotation matricielle .. */
752                         q = tileno / cp->tw;    /* .. coordonnees de la tile (q,p) q pour ligne et p pour colonne */
753                         
754                         /* 4 borders of the tile rescale on the image if necessary */
755                         tile->x0 = int_max(cp->tx0 + p * cp->tdx, image->x0);
756                         tile->y0 = int_max(cp->ty0 + q * cp->tdy, image->y0);
757                         tile->x1 = int_min(cp->tx0 + (p + 1) * cp->tdx, image->x1);
758                         tile->y1 = int_min(cp->ty0 + (q + 1) * cp->tdy, image->y1);
759
760                         tilec->x0 = int_ceildiv(tile->x0, image->comps[i].dx);
761                         tilec->y0 = int_ceildiv(tile->y0, image->comps[i].dy);
762                         tilec->x1 = int_ceildiv(tile->x1, image->comps[i].dx);
763                         tilec->y1 = int_ceildiv(tile->y1, image->comps[i].dy);
764
765                         x0 = j == 0 ? tilec->x0 : int_min(x0, (unsigned int) tilec->x0);
766                         y0 = j == 0 ? tilec->y0 : int_min(y0,   (unsigned int) tilec->y0);
767                         x1 = j == 0 ? tilec->x1 : int_max(x1,   (unsigned int) tilec->x1);
768                         y1 = j == 0 ? tilec->y1 : int_max(y1,   (unsigned int) tilec->y1);
769                 }
770
771                 w = int_ceildivpow2(x1 - x0, image->comps[i].factor);
772                 h = int_ceildivpow2(y1 - y0, image->comps[i].factor);
773
774                 image->comps[i].w = w;
775                 image->comps[i].h = h;
776                 image->comps[i].x0 = x0;
777                 image->comps[i].y0 = y0;
778         }
779 }
780
781 void tcd_malloc_decode_tile(opj_tcd_t *tcd, opj_image_t * image, opj_cp_t * cp, int tileno, opj_codestream_info_t *cstr_info) {
782         int compno, resno, bandno, precno, cblkno;
783         opj_tcp_t *tcp;
784         opj_tcd_tile_t *tile;
785
786         OPJ_ARG_NOT_USED(cstr_info);
787
788         tcd->cp = cp;
789         
790         tcp = &(cp->tcps[cp->tileno[tileno]]);
791         tile = &(tcd->tcd_image->tiles[cp->tileno[tileno]]);
792         
793         tileno = cp->tileno[tileno];
794         
795         for (compno = 0; compno < tile->numcomps; compno++) {
796                 opj_tccp_t *tccp = &tcp->tccps[compno];
797                 opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
798                 
799                 if (tccp->numresolutions <= 0)
800                 {
801                         cp->tileno[tileno] = -1;
802                         return;
803                 }
804
805                 /* border of each tile component (global) */
806                 tilec->x0 = int_ceildiv(tile->x0, image->comps[compno].dx);
807                 tilec->y0 = int_ceildiv(tile->y0, image->comps[compno].dy);
808                 tilec->x1 = int_ceildiv(tile->x1, image->comps[compno].dx);
809                 tilec->y1 = int_ceildiv(tile->y1, image->comps[compno].dy);
810
811                 tilec->numresolutions = tccp->numresolutions;
812                 tilec->resolutions = (opj_tcd_resolution_t *) opj_malloc(tilec->numresolutions * sizeof(opj_tcd_resolution_t));
813                 
814                 for (resno = 0; resno < tilec->numresolutions; resno++) {
815                         int pdx, pdy;
816                         int levelno = tilec->numresolutions - 1 - resno;
817                         int tlprcxstart, tlprcystart, brprcxend, brprcyend;
818                         int tlcbgxstart, tlcbgystart, brcbgxend, brcbgyend;
819                         int cbgwidthexpn, cbgheightexpn;
820                         int cblkwidthexpn, cblkheightexpn;
821                         
822                         opj_tcd_resolution_t *res = &tilec->resolutions[resno];
823                         
824                         /* border for each resolution level (global) */
825                         res->x0 = int_ceildivpow2(tilec->x0, levelno);
826                         res->y0 = int_ceildivpow2(tilec->y0, levelno);
827                         res->x1 = int_ceildivpow2(tilec->x1, levelno);
828                         res->y1 = int_ceildivpow2(tilec->y1, levelno);
829                         res->numbands = resno == 0 ? 1 : 3;
830                         
831                         /* p. 35, table A-23, ISO/IEC FDIS154444-1 : 2000 (18 august 2000) */
832                         if (tccp->csty & J2K_CCP_CSTY_PRT) {
833                                 pdx = tccp->prcw[resno];
834                                 pdy = tccp->prch[resno];
835                         } else {
836                                 pdx = 15;
837                                 pdy = 15;
838                         }                       
839                         
840                         /* p. 64, B.6, ISO/IEC FDIS15444-1 : 2000 (18 august 2000)  */
841                         tlprcxstart = int_floordivpow2(res->x0, pdx) << pdx;
842                         tlprcystart = int_floordivpow2(res->y0, pdy) << pdy;
843                         brprcxend = int_ceildivpow2(res->x1, pdx) << pdx;
844                         brprcyend = int_ceildivpow2(res->y1, pdy) << pdy;
845                         
846                         res->pw = (res->x0 == res->x1) ? 0 : ((brprcxend - tlprcxstart) >> pdx);
847                         res->ph = (res->y0 == res->y1) ? 0 : ((brprcyend - tlprcystart) >> pdy);
848                         
849                         if (resno == 0) {
850                                 tlcbgxstart = tlprcxstart;
851                                 tlcbgystart = tlprcystart;
852                                 brcbgxend = brprcxend;
853                                 brcbgyend = brprcyend;
854                                 cbgwidthexpn = pdx;
855                                 cbgheightexpn = pdy;
856                         } else {
857                                 tlcbgxstart = int_ceildivpow2(tlprcxstart, 1);
858                                 tlcbgystart = int_ceildivpow2(tlprcystart, 1);
859                                 brcbgxend = int_ceildivpow2(brprcxend, 1);
860                                 brcbgyend = int_ceildivpow2(brprcyend, 1);
861                                 cbgwidthexpn = pdx - 1;
862                                 cbgheightexpn = pdy - 1;
863                         }
864                         
865                         cblkwidthexpn = int_min(tccp->cblkw, cbgwidthexpn);
866                         cblkheightexpn = int_min(tccp->cblkh, cbgheightexpn);
867                         
868                         for (bandno = 0; bandno < res->numbands; bandno++) {
869                                 int x0b, y0b;
870                                 int gain, numbps;
871                                 opj_stepsize_t *ss = NULL;
872                                 
873                                 opj_tcd_band_t *band = &res->bands[bandno];
874                                 band->bandno = resno == 0 ? 0 : bandno + 1;
875                                 x0b = (band->bandno == 1) || (band->bandno == 3) ? 1 : 0;
876                                 y0b = (band->bandno == 2) || (band->bandno == 3) ? 1 : 0;
877                                 
878                                 if (band->bandno == 0) {
879                                         /* band border (global) */
880                                         band->x0 = int_ceildivpow2(tilec->x0, levelno);
881                                         band->y0 = int_ceildivpow2(tilec->y0, levelno);
882                                         band->x1 = int_ceildivpow2(tilec->x1, levelno);
883                                         band->y1 = int_ceildivpow2(tilec->y1, levelno);
884                                 } else {
885                                         /* band border (global) */
886                                         band->x0 = int_ceildivpow2(tilec->x0 - (1 << levelno) * x0b, levelno + 1);
887                                         band->y0 = int_ceildivpow2(tilec->y0 - (1 << levelno) * y0b, levelno + 1);
888                                         band->x1 = int_ceildivpow2(tilec->x1 - (1 << levelno) * x0b, levelno + 1);
889                                         band->y1 = int_ceildivpow2(tilec->y1 - (1 << levelno) * y0b, levelno + 1);
890                                 }
891                                 
892                                 ss = &tccp->stepsizes[resno == 0 ? 0 : 3 * (resno - 1) + bandno + 1];
893                                 gain = tccp->qmfbid == 0 ? dwt_getgain_real(band->bandno) : dwt_getgain(band->bandno);
894                                 numbps = image->comps[compno].prec + gain;
895                                 band->stepsize = (float)(((1.0 + ss->mant / 2048.0) * pow(2.0, numbps - ss->expn)) * 0.5);
896                                 band->numbps = ss->expn + tccp->numgbits - 1;   /* WHY -1 ? */
897                                 
898                                 band->precincts = (opj_tcd_precinct_t *) opj_malloc(res->pw * res->ph * sizeof(opj_tcd_precinct_t));
899                                 
900                                 for (precno = 0; precno < res->pw * res->ph; precno++) {
901                                         int tlcblkxstart, tlcblkystart, brcblkxend, brcblkyend;
902                                         int cbgxstart = tlcbgxstart + (precno % res->pw) * (1 << cbgwidthexpn);
903                                         int cbgystart = tlcbgystart + (precno / res->pw) * (1 << cbgheightexpn);
904                                         int cbgxend = cbgxstart + (1 << cbgwidthexpn);
905                                         int cbgyend = cbgystart + (1 << cbgheightexpn);
906                                         
907                                         opj_tcd_precinct_t *prc = &band->precincts[precno];
908                                         /* precinct size (global) */
909                                         prc->x0 = int_max(cbgxstart, band->x0);
910                                         prc->y0 = int_max(cbgystart, band->y0);
911                                         prc->x1 = int_min(cbgxend, band->x1);
912                                         prc->y1 = int_min(cbgyend, band->y1);
913                                         
914                                         tlcblkxstart = int_floordivpow2(prc->x0, cblkwidthexpn) << cblkwidthexpn;
915                                         tlcblkystart = int_floordivpow2(prc->y0, cblkheightexpn) << cblkheightexpn;
916                                         brcblkxend = int_ceildivpow2(prc->x1, cblkwidthexpn) << cblkwidthexpn;
917                                         brcblkyend = int_ceildivpow2(prc->y1, cblkheightexpn) << cblkheightexpn;
918                                         prc->cw = (brcblkxend - tlcblkxstart) >> cblkwidthexpn;
919                                         prc->ch = (brcblkyend - tlcblkystart) >> cblkheightexpn;
920
921                                         prc->cblks.dec = (opj_tcd_cblk_dec_t*) opj_malloc(prc->cw * prc->ch * sizeof(opj_tcd_cblk_dec_t));
922
923                                         prc->incltree = tgt_create(prc->cw, prc->ch);
924                                         prc->imsbtree = tgt_create(prc->cw, prc->ch);
925                                         
926                                         for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
927                                                 int cblkxstart = tlcblkxstart + (cblkno % prc->cw) * (1 << cblkwidthexpn);
928                                                 int cblkystart = tlcblkystart + (cblkno / prc->cw) * (1 << cblkheightexpn);
929                                                 int cblkxend = cblkxstart + (1 << cblkwidthexpn);
930                                                 int cblkyend = cblkystart + (1 << cblkheightexpn);                                      
931
932                                                 opj_tcd_cblk_dec_t* cblk = &prc->cblks.dec[cblkno];
933                                                 cblk->data = NULL;
934                                                 cblk->segs = NULL;
935                                                 /* code-block size (global) */
936                                                 cblk->x0 = int_max(cblkxstart, prc->x0);
937                                                 cblk->y0 = int_max(cblkystart, prc->y0);
938                                                 cblk->x1 = int_min(cblkxend, prc->x1);
939                                                 cblk->y1 = int_min(cblkyend, prc->y1);
940                                                 cblk->numsegs = 0;
941                                         }
942                                 } /* precno */
943                         } /* bandno */
944                 } /* resno */
945         } /* compno */
946         /* tcd_dump(stdout, tcd, &tcd->tcd_image); */
947 }
948
949 void tcd_makelayer_fixed(opj_tcd_t *tcd, int layno, int final) {
950         int compno, resno, bandno, precno, cblkno;
951         int value;                      /*, matrice[tcd_tcp->numlayers][tcd_tile->comps[0].numresolutions][3]; */
952         int matrice[10][10][3];
953         int i, j, k;
954
955         opj_cp_t *cp = tcd->cp;
956         opj_tcd_tile_t *tcd_tile = tcd->tcd_tile;
957         opj_tcp_t *tcd_tcp = tcd->tcp;
958
959         /*matrice=(int*)opj_malloc(tcd_tcp->numlayers*tcd_tile->comps[0].numresolutions*3*sizeof(int)); */
960         
961         for (compno = 0; compno < tcd_tile->numcomps; compno++) {
962                 opj_tcd_tilecomp_t *tilec = &tcd_tile->comps[compno];
963                 for (i = 0; i < tcd_tcp->numlayers; i++) {
964                         for (j = 0; j < tilec->numresolutions; j++) {
965                                 for (k = 0; k < 3; k++) {
966                                         matrice[i][j][k] =
967                                                 (int) (cp->matrice[i * tilec->numresolutions * 3 + j * 3 + k] 
968                                                 * (float) (tcd->image->comps[compno].prec / 16.0));
969                                 }
970                         }
971                 }
972         
973                 for (resno = 0; resno < tilec->numresolutions; resno++) {
974                         opj_tcd_resolution_t *res = &tilec->resolutions[resno];
975                         for (bandno = 0; bandno < res->numbands; bandno++) {
976                                 opj_tcd_band_t *band = &res->bands[bandno];
977                                 for (precno = 0; precno < res->pw * res->ph; precno++) {
978                                         opj_tcd_precinct_t *prc = &band->precincts[precno];
979                                         for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
980                                                 opj_tcd_cblk_enc_t *cblk = &prc->cblks.enc[cblkno];
981                                                 opj_tcd_layer_t *layer = &cblk->layers[layno];
982                                                 int n;
983                                                 int imsb = tcd->image->comps[compno].prec - cblk->numbps;       /* number of bit-plan equal to zero */
984                                                 /* Correction of the matrix of coefficient to include the IMSB information */
985                                                 if (layno == 0) {
986                                                         value = matrice[layno][resno][bandno];
987                                                         if (imsb >= value) {
988                                                                 value = 0;
989                                                         } else {
990                                                                 value -= imsb;
991                                                         }
992                                                 } else {
993                                                         value = matrice[layno][resno][bandno] - matrice[layno - 1][resno][bandno];
994                                                         if (imsb >= matrice[layno - 1][resno][bandno]) {
995                                                                 value -= (imsb - matrice[layno - 1][resno][bandno]);
996                                                                 if (value < 0) {
997                                                                         value = 0;
998                                                                 }
999                                                         }
1000                                                 }
1001                                                 
1002                                                 if (layno == 0) {
1003                                                         cblk->numpassesinlayers = 0;
1004                                                 }
1005                                                 
1006                                                 n = cblk->numpassesinlayers;
1007                                                 if (cblk->numpassesinlayers == 0) {
1008                                                         if (value != 0) {
1009                                                                 n = 3 * value - 2 + cblk->numpassesinlayers;
1010                                                         } else {
1011                                                                 n = cblk->numpassesinlayers;
1012                                                         }
1013                                                 } else {
1014                                                         n = 3 * value + cblk->numpassesinlayers;
1015                                                 }
1016                                                 
1017                                                 layer->numpasses = n - cblk->numpassesinlayers;
1018                                                 
1019                                                 if (!layer->numpasses)
1020                                                         continue;
1021                                                 
1022                                                 if (cblk->numpassesinlayers == 0) {
1023                                                         layer->len = cblk->passes[n - 1].rate;
1024                                                         layer->data = cblk->data;
1025                                                 } else {
1026                                                         layer->len = cblk->passes[n - 1].rate - cblk->passes[cblk->numpassesinlayers - 1].rate;
1027                                                         layer->data = cblk->data + cblk->passes[cblk->numpassesinlayers - 1].rate;
1028                                                 }
1029                                                 if (final)
1030                                                         cblk->numpassesinlayers = n;
1031                                         }
1032                                 }
1033                         }
1034                 }
1035         }
1036 }
1037
1038 void tcd_rateallocate_fixed(opj_tcd_t *tcd) {
1039         int layno;
1040         for (layno = 0; layno < tcd->tcp->numlayers; layno++) {
1041                 tcd_makelayer_fixed(tcd, layno, 1);
1042         }
1043 }
1044
1045 void tcd_rateallocate_fixed_v2(opj_tcd_v2_t *tcd) {
1046         OPJ_UINT32 layno;
1047
1048         for (layno = 0; layno < tcd->tcp->numlayers; layno++) {
1049                 tcd_makelayer_fixed_v2(tcd, layno, 1);
1050         }
1051 }
1052
1053 void tcd_makelayer(opj_tcd_t *tcd, int layno, double thresh, int final) {
1054         int compno, resno, bandno, precno, cblkno, passno;
1055         
1056         opj_tcd_tile_t *tcd_tile = tcd->tcd_tile;
1057
1058         tcd_tile->distolayer[layno] = 0;        /* fixed_quality */
1059         
1060         for (compno = 0; compno < tcd_tile->numcomps; compno++) {
1061                 opj_tcd_tilecomp_t *tilec = &tcd_tile->comps[compno];
1062                 for (resno = 0; resno < tilec->numresolutions; resno++) {
1063                         opj_tcd_resolution_t *res = &tilec->resolutions[resno];
1064                         for (bandno = 0; bandno < res->numbands; bandno++) {
1065                                 opj_tcd_band_t *band = &res->bands[bandno];
1066                                 for (precno = 0; precno < res->pw * res->ph; precno++) {
1067                                         opj_tcd_precinct_t *prc = &band->precincts[precno];
1068                                         for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
1069                                                 opj_tcd_cblk_enc_t *cblk = &prc->cblks.enc[cblkno];
1070                                                 opj_tcd_layer_t *layer = &cblk->layers[layno];
1071                                                 
1072                                                 int n;
1073                                                 if (layno == 0) {
1074                                                         cblk->numpassesinlayers = 0;
1075                                                 }
1076                                                 n = cblk->numpassesinlayers;
1077                                                 for (passno = cblk->numpassesinlayers; passno < cblk->totalpasses; passno++) {
1078                                                         int dr;
1079                                                         double dd;
1080                                                         opj_tcd_pass_t *pass = &cblk->passes[passno];
1081                                                         if (n == 0) {
1082                                                                 dr = pass->rate;
1083                                                                 dd = pass->distortiondec;
1084                                                         } else {
1085                                                                 dr = pass->rate - cblk->passes[n - 1].rate;
1086                                                                 dd = pass->distortiondec - cblk->passes[n - 1].distortiondec;
1087                                                         }
1088                                                         if (!dr) {
1089                                                                 if (dd != 0)
1090                                                                         n = passno + 1;
1091                                                                 continue;
1092                                                         }
1093                                                         if (dd / dr >= thresh)
1094                                                                 n = passno + 1;
1095                                                 }
1096                                                 layer->numpasses = n - cblk->numpassesinlayers;
1097                                                 
1098                                                 if (!layer->numpasses) {
1099                                                         layer->disto = 0;
1100                                                         continue;
1101                                                 }
1102                                                 if (cblk->numpassesinlayers == 0) {
1103                                                         layer->len = cblk->passes[n - 1].rate;
1104                                                         layer->data = cblk->data;
1105                                                         layer->disto = cblk->passes[n - 1].distortiondec;
1106                                                 } else {
1107                                                         layer->len = cblk->passes[n - 1].rate - cblk->passes[cblk->numpassesinlayers - 1].rate;
1108                                                         layer->data = cblk->data + cblk->passes[cblk->numpassesinlayers - 1].rate;
1109                                                         layer->disto = cblk->passes[n - 1].distortiondec - cblk->passes[cblk->numpassesinlayers - 1].distortiondec;
1110                                                 }
1111                                                 
1112                                                 tcd_tile->distolayer[layno] += layer->disto;    /* fixed_quality */
1113                                                 
1114                                                 if (final)
1115                                                         cblk->numpassesinlayers = n;
1116                                         }
1117                                 }
1118                         }
1119                 }
1120         }
1121 }
1122
1123 void tcd_makelayer_v2(  opj_tcd_v2_t *tcd,
1124                                                 OPJ_UINT32 layno,
1125                                                 OPJ_FLOAT64 thresh,
1126                                                 OPJ_UINT32 final)
1127 {
1128         OPJ_UINT32 compno, resno, bandno, precno, cblkno;
1129         OPJ_UINT32 passno;
1130
1131         opj_tcd_tile_v2_t *tcd_tile = tcd->tcd_image->tiles;
1132
1133         tcd_tile->distolayer[layno] = 0;        /* fixed_quality */
1134
1135         for (compno = 0; compno < tcd_tile->numcomps; compno++) {
1136                 opj_tcd_tilecomp_v2_t *tilec = &tcd_tile->comps[compno];
1137
1138                 for (resno = 0; resno < tilec->numresolutions; resno++) {
1139                         opj_tcd_resolution_v2_t *res = &tilec->resolutions[resno];
1140
1141                         for (bandno = 0; bandno < res->numbands; bandno++) {
1142                                 opj_tcd_band_v2_t *band = &res->bands[bandno];
1143
1144                                 for (precno = 0; precno < res->pw * res->ph; precno++) {
1145                                         opj_tcd_precinct_v2_t *prc = &band->precincts[precno];
1146
1147                                         for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
1148                                                 opj_tcd_cblk_enc_v2_t *cblk = &prc->cblks.enc[cblkno];
1149                                                 opj_tcd_layer_t *layer = &cblk->layers[layno];
1150                                                 OPJ_UINT32 n;
1151
1152                                                 if (layno == 0) {
1153                                                         cblk->numpassesinlayers = 0;
1154                                                 }
1155
1156                                                 n = cblk->numpassesinlayers;
1157
1158                                                 for (passno = cblk->numpassesinlayers; passno < cblk->totalpasses; passno++) {
1159                                                         OPJ_INT32 dr;
1160                                                         OPJ_FLOAT64 dd;
1161                                                         opj_tcd_pass_v2_t *pass = &cblk->passes[passno];
1162
1163                                                         if (n == 0) {
1164                                                                 dr = pass->rate;
1165                                                                 dd = pass->distortiondec;
1166                                                         } else {
1167                                                                 dr = pass->rate - cblk->passes[n - 1].rate;
1168                                                                 dd = pass->distortiondec - cblk->passes[n - 1].distortiondec;
1169                                                         }
1170
1171                                                         if (!dr) {
1172                                                                 if (dd != 0)
1173                                                                         n = passno + 1;
1174                                                                 continue;
1175                                                         }
1176                                                         if (dd / dr >= thresh)
1177                                                                 n = passno + 1;
1178                                                 }
1179
1180                                                 layer->numpasses = n - cblk->numpassesinlayers;
1181
1182                                                 if (!layer->numpasses) {
1183                                                         layer->disto = 0;
1184                                                         continue;
1185                                                 }
1186
1187                                                 if (cblk->numpassesinlayers == 0) {
1188                                                         layer->len = cblk->passes[n - 1].rate;
1189                                                         layer->data = cblk->data;
1190                                                         layer->disto = cblk->passes[n - 1].distortiondec;
1191                                                 } else {
1192                                                         layer->len = cblk->passes[n - 1].rate - cblk->passes[cblk->numpassesinlayers - 1].rate;
1193                                                         layer->data = cblk->data + cblk->passes[cblk->numpassesinlayers - 1].rate;
1194                                                         layer->disto = cblk->passes[n - 1].distortiondec - cblk->passes[cblk->numpassesinlayers - 1].distortiondec;
1195                                                 }
1196
1197                                                 tcd_tile->distolayer[layno] += layer->disto;    /* fixed_quality */
1198
1199                                                 if (final)
1200                                                         cblk->numpassesinlayers = n;
1201                                         }
1202                                 }
1203                         }
1204                 }
1205         }
1206 }
1207
1208 void tcd_makelayer_fixed_v2(opj_tcd_v2_t *tcd, OPJ_UINT32 layno, OPJ_UINT32 final) {
1209         OPJ_UINT32 compno, resno, bandno, precno, cblkno;
1210         OPJ_INT32 value;                        /*, matrice[tcd_tcp->numlayers][tcd_tile->comps[0].numresolutions][3]; */
1211         OPJ_INT32 matrice[10][10][3];
1212         OPJ_UINT32 i, j, k;
1213
1214         opj_cp_v2_t *cp = tcd->cp;
1215         opj_tcd_tile_v2_t *tcd_tile = tcd->tcd_image->tiles;
1216         opj_tcp_v2_t *tcd_tcp = tcd->tcp;
1217
1218         for (compno = 0; compno < tcd_tile->numcomps; compno++) {
1219                 opj_tcd_tilecomp_v2_t *tilec = &tcd_tile->comps[compno];
1220
1221                 for (i = 0; i < tcd_tcp->numlayers; i++) {
1222                         for (j = 0; j < tilec->numresolutions; j++) {
1223                                 for (k = 0; k < 3; k++) {
1224                                         matrice[i][j][k] =
1225                                                 (OPJ_INT32) (cp->m_specific_param.m_enc.m_matrice[i * tilec->numresolutions * 3 + j * 3 + k]
1226                                                 * (OPJ_FLOAT32) (tcd->image->comps[compno].prec / 16.0));
1227                                 }
1228                         }
1229                 }
1230
1231                 for (resno = 0; resno < tilec->numresolutions; resno++) {
1232                         opj_tcd_resolution_v2_t *res = &tilec->resolutions[resno];
1233
1234                         for (bandno = 0; bandno < res->numbands; bandno++) {
1235                                 opj_tcd_band_v2_t *band = &res->bands[bandno];
1236
1237                                 for (precno = 0; precno < res->pw * res->ph; precno++) {
1238                                         opj_tcd_precinct_v2_t *prc = &band->precincts[precno];
1239
1240                                         for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
1241                                                 opj_tcd_cblk_enc_v2_t *cblk = &prc->cblks.enc[cblkno];
1242                                                 opj_tcd_layer_t *layer = &cblk->layers[layno];
1243                                                 OPJ_UINT32 n;
1244                                                 OPJ_INT32 imsb = tcd->image->comps[compno].prec - cblk->numbps; /* number of bit-plan equal to zero */
1245
1246                                                 /* Correction of the matrix of coefficient to include the IMSB information */
1247                                                 if (layno == 0) {
1248                                                         value = matrice[layno][resno][bandno];
1249                                                         if (imsb >= value) {
1250                                                                 value = 0;
1251                                                         } else {
1252                                                                 value -= imsb;
1253                                                         }
1254                                                 } else {
1255                                                         value = matrice[layno][resno][bandno] - matrice[layno - 1][resno][bandno];
1256                                                         if (imsb >= matrice[layno - 1][resno][bandno]) {
1257                                                                 value -= (imsb - matrice[layno - 1][resno][bandno]);
1258                                                                 if (value < 0) {
1259                                                                         value = 0;
1260                                                                 }
1261                                                         }
1262                                                 }
1263
1264                                                 if (layno == 0) {
1265                                                         cblk->numpassesinlayers = 0;
1266                                                 }
1267
1268                                                 n = cblk->numpassesinlayers;
1269                                                 if (cblk->numpassesinlayers == 0) {
1270                                                         if (value != 0) {
1271                                                                 n = 3 * value - 2 + cblk->numpassesinlayers;
1272                                                         } else {
1273                                                                 n = cblk->numpassesinlayers;
1274                                                         }
1275                                                 } else {
1276                                                         n = 3 * value + cblk->numpassesinlayers;
1277                                                 }
1278
1279                                                 layer->numpasses = n - cblk->numpassesinlayers;
1280
1281                                                 if (!layer->numpasses)
1282                                                         continue;
1283
1284                                                 if (cblk->numpassesinlayers == 0) {
1285                                                         layer->len = cblk->passes[n - 1].rate;
1286                                                         layer->data = cblk->data;
1287                                                 } else {
1288                                                         layer->len = cblk->passes[n - 1].rate - cblk->passes[cblk->numpassesinlayers - 1].rate;
1289                                                         layer->data = cblk->data + cblk->passes[cblk->numpassesinlayers - 1].rate;
1290                                                 }
1291
1292                                                 if (final)
1293                                                         cblk->numpassesinlayers = n;
1294                                         }
1295                                 }
1296                         }
1297                 }
1298         }
1299 }
1300
1301 opj_bool tcd_rateallocate(opj_tcd_t *tcd, unsigned char *dest, int len, opj_codestream_info_t *cstr_info) {
1302         int compno, resno, bandno, precno, cblkno, passno, layno;
1303         double min, max;
1304         double cumdisto[100];   /* fixed_quality */
1305         const double K = 1;             /* 1.1; fixed_quality */
1306         double maxSE = 0;
1307
1308         opj_cp_t *cp = tcd->cp;
1309         opj_tcd_tile_t *tcd_tile = tcd->tcd_tile;
1310         opj_tcp_t *tcd_tcp = tcd->tcp;
1311
1312         min = DBL_MAX;
1313         max = 0;
1314         
1315         tcd_tile->numpix = 0;           /* fixed_quality */
1316         
1317         for (compno = 0; compno < tcd_tile->numcomps; compno++) {
1318                 opj_tcd_tilecomp_t *tilec = &tcd_tile->comps[compno];
1319                 tilec->numpix = 0;
1320
1321                 for (resno = 0; resno < tilec->numresolutions; resno++) {
1322                         opj_tcd_resolution_t *res = &tilec->resolutions[resno];
1323
1324                         for (bandno = 0; bandno < res->numbands; bandno++) {
1325                                 opj_tcd_band_t *band = &res->bands[bandno];
1326
1327                                 for (precno = 0; precno < res->pw * res->ph; precno++) {
1328                                         opj_tcd_precinct_t *prc = &band->precincts[precno];
1329
1330                                         for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
1331                                                 opj_tcd_cblk_enc_t *cblk = &prc->cblks.enc[cblkno];
1332
1333                                                 for (passno = 0; passno < cblk->totalpasses; passno++) {
1334                                                         opj_tcd_pass_t *pass = &cblk->passes[passno];
1335                                                         int dr;
1336                                                         double dd, rdslope;
1337                                                         if (passno == 0) {
1338                                                                 dr = pass->rate;
1339                                                                 dd = pass->distortiondec;
1340                                                         } else {
1341                                                                 dr = pass->rate - cblk->passes[passno - 1].rate;
1342                                                                 dd = pass->distortiondec - cblk->passes[passno - 1].distortiondec;
1343                                                         }
1344                                                         if (dr == 0) {
1345                                                                 continue;
1346                                                         }
1347                                                         rdslope = dd / dr;
1348                                                         if (rdslope < min) {
1349                                                                 min = rdslope;
1350                                                         }
1351                                                         if (rdslope > max) {
1352                                                                 max = rdslope;
1353                                                         }
1354                                                 } /* passno */
1355                                                 
1356                                                 /* fixed_quality */
1357                                                 tcd_tile->numpix += ((cblk->x1 - cblk->x0) * (cblk->y1 - cblk->y0));
1358                                                 tilec->numpix += ((cblk->x1 - cblk->x0) * (cblk->y1 - cblk->y0));
1359                                         } /* cbklno */
1360                                 } /* precno */
1361                         } /* bandno */
1362                 } /* resno */
1363                 
1364                 maxSE += (((double)(1 << tcd->image->comps[compno].prec) - 1.0) 
1365                         * ((double)(1 << tcd->image->comps[compno].prec) -1.0)) 
1366                         * ((double)(tilec->numpix));
1367         } /* compno */
1368         
1369         /* index file */
1370         if(cstr_info) {
1371                 opj_tile_info_t *tile_info = &cstr_info->tile[tcd->tcd_tileno];
1372                 tile_info->numpix = tcd_tile->numpix;
1373                 tile_info->distotile = tcd_tile->distotile;
1374                 tile_info->thresh = (double *) opj_malloc(tcd_tcp->numlayers * sizeof(double));
1375         }
1376         
1377         for (layno = 0; layno < tcd_tcp->numlayers; layno++) {
1378                 double lo = min;
1379                 double hi = max;
1380                 int success = 0;
1381                 int maxlen = tcd_tcp->rates[layno] ? int_min(((int) ceil(tcd_tcp->rates[layno])), len) : len;
1382                 double goodthresh = 0;
1383                 double stable_thresh = 0;
1384                 int i;
1385                 double distotarget;             /* fixed_quality */
1386                 
1387                 /* fixed_quality */
1388                 distotarget = tcd_tile->distotile - ((K * maxSE) / pow((float)10, tcd_tcp->distoratio[layno] / 10));
1389         
1390                 /* Don't try to find an optimal threshold but rather take everything not included yet, if
1391                   -r xx,yy,zz,0   (disto_alloc == 1 and rates == 0)
1392                   -q xx,yy,zz,0   (fixed_quality == 1 and distoratio == 0)
1393                   ==> possible to have some lossy layers and the last layer for sure lossless */
1394                 if ( ((cp->disto_alloc==1) && (tcd_tcp->rates[layno]>0)) || ((cp->fixed_quality==1) && (tcd_tcp->distoratio[layno]>0))) {
1395                         opj_t2_t *t2 = t2_create(tcd->cinfo, tcd->image, cp);
1396                         double thresh = 0;
1397
1398                         for (i = 0; i < 128; i++) {
1399                                 int l = 0;
1400                                 double distoachieved = 0;       /* fixed_quality */
1401                                 thresh = (lo + hi) / 2;
1402                                 
1403                                 tcd_makelayer(tcd, layno, thresh, 0);
1404                                 
1405                                 if (cp->fixed_quality) {        /* fixed_quality */
1406                                         if(cp->cinema){
1407                                                 l = t2_encode_packets(t2,tcd->tcd_tileno, tcd_tile, layno + 1, dest, maxlen, cstr_info,tcd->cur_tp_num,tcd->tp_pos,tcd->cur_pino,THRESH_CALC, tcd->cur_totnum_tp);
1408                                                 if (l == -999) {
1409                                                         lo = thresh;
1410                                                         continue;
1411                                                 }else{
1412                         distoachieved = layno == 0 ? 
1413                                                         tcd_tile->distolayer[0] : cumdisto[layno - 1] + tcd_tile->distolayer[layno];
1414                                                         if (distoachieved < distotarget) {
1415                                                                 hi=thresh; 
1416                                                                 stable_thresh = thresh;
1417                                                                 continue;
1418                                                         }else{
1419                                                                 lo=thresh;
1420                                                         }
1421                                                 }
1422                                         }else{
1423                                                 distoachieved = (layno == 0) ? 
1424                                                         tcd_tile->distolayer[0] : (cumdisto[layno - 1] + tcd_tile->distolayer[layno]);
1425                                                 if (distoachieved < distotarget) {
1426                                                         hi = thresh;
1427                                                         stable_thresh = thresh;
1428                                                         continue;
1429                                                 }
1430                                                 lo = thresh;
1431                                         }
1432                                 } else {
1433                                         l = t2_encode_packets(t2, tcd->tcd_tileno, tcd_tile, layno + 1, dest, maxlen, cstr_info,tcd->cur_tp_num,tcd->tp_pos,tcd->cur_pino,THRESH_CALC, tcd->cur_totnum_tp);
1434                                         /* TODO: what to do with l ??? seek / tell ??? */
1435                                         /* opj_event_msg(tcd->cinfo, EVT_INFO, "rate alloc: len=%d, max=%d\n", l, maxlen); */
1436                                         if (l == -999) {
1437                                                 lo = thresh;
1438                                                 continue;
1439                                         }
1440                                         hi = thresh;
1441                                         stable_thresh = thresh;
1442                                 }
1443                         }
1444                         success = 1;
1445                         goodthresh = stable_thresh == 0? thresh : stable_thresh;
1446                         t2_destroy(t2);
1447                 } else {
1448                         success = 1;
1449                         goodthresh = min;
1450                 }
1451                 
1452                 if (!success) {
1453                         return OPJ_FALSE;
1454                 }
1455                 
1456                 if(cstr_info) { /* Threshold for Marcela Index */
1457                         cstr_info->tile[tcd->tcd_tileno].thresh[layno] = goodthresh;
1458                 }
1459                 tcd_makelayer(tcd, layno, goodthresh, 1);
1460         
1461                 /* fixed_quality */
1462                 cumdisto[layno] = (layno == 0) ? tcd_tile->distolayer[0] : (cumdisto[layno - 1] + tcd_tile->distolayer[layno]); 
1463         }
1464
1465         return OPJ_TRUE;
1466 }
1467
1468 opj_bool tcd_rateallocate_v2(   opj_tcd_v2_t *tcd,
1469                                                                 OPJ_BYTE *dest,
1470                                                                 OPJ_UINT32 * p_data_written,
1471                                                                 OPJ_UINT32 len,
1472                                                                 opj_codestream_info_t *cstr_info)
1473 {
1474         OPJ_UINT32 compno, resno, bandno, precno, cblkno, layno;
1475         OPJ_UINT32 passno;
1476         OPJ_FLOAT64 min, max;
1477         OPJ_FLOAT64 cumdisto[100];      /* fixed_quality */
1478         const OPJ_FLOAT64 K = 1;                /* 1.1; fixed_quality */
1479         OPJ_FLOAT64 maxSE = 0;
1480
1481         opj_cp_v2_t *cp = tcd->cp;
1482         opj_tcd_tile_v2_t *tcd_tile = tcd->tcd_image->tiles;
1483         opj_tcp_v2_t *tcd_tcp = tcd->tcp;
1484
1485         min = DBL_MAX;
1486         max = 0;
1487
1488         tcd_tile->numpix = 0;           /* fixed_quality */
1489
1490         for (compno = 0; compno < tcd_tile->numcomps; compno++) {
1491                 opj_tcd_tilecomp_v2_t *tilec = &tcd_tile->comps[compno];
1492                 tilec->numpix = 0;
1493
1494                 for (resno = 0; resno < tilec->numresolutions; resno++) {
1495                         opj_tcd_resolution_v2_t *res = &tilec->resolutions[resno];
1496
1497                         for (bandno = 0; bandno < res->numbands; bandno++) {
1498                                 opj_tcd_band_v2_t *band = &res->bands[bandno];
1499
1500                                 for (precno = 0; precno < res->pw * res->ph; precno++) {
1501                                         opj_tcd_precinct_v2_t *prc = &band->precincts[precno];
1502
1503                                         for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
1504                                                 opj_tcd_cblk_enc_v2_t *cblk = &prc->cblks.enc[cblkno];
1505
1506                                                 for (passno = 0; passno < cblk->totalpasses; passno++) {
1507                                                         opj_tcd_pass_v2_t *pass = &cblk->passes[passno];
1508                                                         OPJ_INT32 dr;
1509                                                         OPJ_FLOAT64 dd, rdslope;
1510
1511                                                         if (passno == 0) {
1512                                                                 dr = pass->rate;
1513                                                                 dd = pass->distortiondec;
1514                                                         } else {
1515                                                                 dr = pass->rate - cblk->passes[passno - 1].rate;
1516                                                                 dd = pass->distortiondec - cblk->passes[passno - 1].distortiondec;
1517                                                         }
1518
1519                                                         if (dr == 0) {
1520                                                                 continue;
1521                                                         }
1522
1523                                                         rdslope = dd / dr;
1524                                                         if (rdslope < min) {
1525                                                                 min = rdslope;
1526                                                         }
1527
1528                                                         if (rdslope > max) {
1529                                                                 max = rdslope;
1530                                                         }
1531                                                 } /* passno */
1532
1533                                                 /* fixed_quality */
1534                                                 tcd_tile->numpix += ((cblk->x1 - cblk->x0) * (cblk->y1 - cblk->y0));
1535                                                 tilec->numpix += ((cblk->x1 - cblk->x0) * (cblk->y1 - cblk->y0));
1536                                         } /* cbklno */
1537                                 } /* precno */
1538                         } /* bandno */
1539                 } /* resno */
1540
1541                 maxSE += (((OPJ_FLOAT64)(1 << tcd->image->comps[compno].prec) - 1.0)
1542                         * ((OPJ_FLOAT64)(1 << tcd->image->comps[compno].prec) -1.0))
1543                         * ((OPJ_FLOAT64)(tilec->numpix));
1544         } /* compno */
1545
1546         /* index file */
1547         if(cstr_info) {
1548                 opj_tile_info_t *tile_info = &cstr_info->tile[tcd->tcd_tileno];
1549                 tile_info->numpix = tcd_tile->numpix;
1550                 tile_info->distotile = tcd_tile->distotile;
1551                 tile_info->thresh = (OPJ_FLOAT64 *) opj_malloc(tcd_tcp->numlayers * sizeof(OPJ_FLOAT64));
1552         }
1553
1554         for (layno = 0; layno < tcd_tcp->numlayers; layno++) {
1555                 OPJ_FLOAT64 lo = min;
1556                 OPJ_FLOAT64 hi = max;
1557                 opj_bool success = OPJ_FALSE;
1558                 OPJ_UINT32 maxlen = tcd_tcp->rates[layno] ? uint_min(((OPJ_UINT32) ceil(tcd_tcp->rates[layno])), len) : len;
1559                 OPJ_FLOAT64 goodthresh = 0;
1560                 OPJ_FLOAT64 stable_thresh = 0;
1561                 OPJ_UINT32 i;
1562                 OPJ_FLOAT64 distotarget;                /* fixed_quality */
1563
1564                 /* fixed_quality */
1565                 distotarget = tcd_tile->distotile - ((K * maxSE) / pow((OPJ_FLOAT32)10, tcd_tcp->distoratio[layno] / 10));
1566
1567                 /* Don't try to find an optimal threshold but rather take everything not included yet, if
1568                   -r xx,yy,zz,0   (disto_alloc == 1 and rates == 0)
1569                   -q xx,yy,zz,0   (fixed_quality == 1 and distoratio == 0)
1570                   ==> possible to have some lossy layers and the last layer for sure lossless */
1571                 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))) {
1572                         opj_t2_v2_t*t2 = t2_create_v2(tcd->image, cp);
1573                         OPJ_FLOAT64 thresh = 0;
1574
1575                         if (t2 == 00) {
1576                                 return OPJ_FALSE;
1577                         }
1578
1579                         for     (i = 0; i < 128; ++i) {
1580                                 OPJ_FLOAT64 distoachieved = 0;  /* fixed_quality */
1581
1582                                 thresh = (lo + hi) / 2;
1583
1584                                 tcd_makelayer_v2(tcd, layno, thresh, 0);
1585
1586                                 if (cp->m_specific_param.m_enc.m_fixed_quality) {       /* fixed_quality */
1587                                         if(cp->m_specific_param.m_enc.m_cinema){
1588                                                 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)) {
1589
1590                                                         lo = thresh;
1591                                                         continue;
1592                                                 }
1593                                                 else {
1594                                                         distoachieved = layno == 0 ?
1595                                                                         tcd_tile->distolayer[0] : cumdisto[layno - 1] + tcd_tile->distolayer[layno];
1596
1597                                                         if (distoachieved < distotarget) {
1598                                                                 hi=thresh;
1599                                                                 stable_thresh = thresh;
1600                                                                 continue;
1601                                                         }else{
1602                                                                 lo=thresh;
1603                                                         }
1604                                                 }
1605                                         }else{
1606                                                 distoachieved = (layno == 0) ?
1607                                                                 tcd_tile->distolayer[0] : (cumdisto[layno - 1] + tcd_tile->distolayer[layno]);
1608
1609                                                 if (distoachieved < distotarget) {
1610                                                         hi = thresh;
1611                                                         stable_thresh = thresh;
1612                                                         continue;
1613                                                 }
1614                                                 lo = thresh;
1615                                         }
1616                                 } else {
1617                                         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))
1618                                         {
1619                                                 /* TODO: what to do with l ??? seek / tell ??? */
1620                                                 /* opj_event_msg(tcd->cinfo, EVT_INFO, "rate alloc: len=%d, max=%d\n", l, maxlen); */
1621                                                 lo = thresh;
1622                                                 continue;
1623                                         }
1624
1625                                         hi = thresh;
1626                                         stable_thresh = thresh;
1627                                 }
1628                         }
1629
1630                         success = OPJ_TRUE;
1631                         goodthresh = stable_thresh == 0? thresh : stable_thresh;
1632
1633                         t2_destroy_v2(t2);
1634                 } else {
1635                         success = OPJ_TRUE;
1636                         goodthresh = min;
1637                 }
1638
1639                 if (!success) {
1640                         return OPJ_FALSE;
1641                 }
1642
1643                 if(cstr_info) { /* Threshold for Marcela Index */
1644                         cstr_info->tile[tcd->tcd_tileno].thresh[layno] = goodthresh;
1645                 }
1646
1647                 tcd_makelayer_v2(tcd, layno, goodthresh, 1);
1648
1649                 /* fixed_quality */
1650                 cumdisto[layno] = (layno == 0) ? tcd_tile->distolayer[0] : (cumdisto[layno - 1] + tcd_tile->distolayer[layno]);
1651         }
1652
1653         return OPJ_TRUE;
1654 }
1655
1656
1657 int tcd_encode_tile(opj_tcd_t *tcd, int tileno, unsigned char *dest, int len, opj_codestream_info_t *cstr_info) {
1658         int compno;
1659         int l, i, numpacks = 0;
1660         opj_tcd_tile_t *tile = NULL;
1661         opj_tcp_t *tcd_tcp = NULL;
1662         opj_cp_t *cp = NULL;
1663
1664         opj_tcp_t *tcp = &tcd->cp->tcps[0];
1665         opj_tccp_t *tccp = &tcp->tccps[0];
1666         opj_image_t *image = tcd->image;
1667         
1668         opj_t1_t *t1 = NULL;            /* T1 component */
1669         opj_t2_t *t2 = NULL;            /* T2 component */
1670
1671         tcd->tcd_tileno = tileno;
1672         tcd->tcd_tile = tcd->tcd_image->tiles;
1673         tcd->tcp = &tcd->cp->tcps[tileno];
1674
1675         tile = tcd->tcd_tile;
1676         tcd_tcp = tcd->tcp;
1677         cp = tcd->cp;
1678
1679         if(tcd->cur_tp_num == 0){
1680                 tcd->encoding_time = opj_clock();       /* time needed to encode a tile */
1681                 /* INDEX >> "Precinct_nb_X et Precinct_nb_Y" */
1682                 if(cstr_info) {
1683                         opj_tcd_tilecomp_t *tilec_idx = &tile->comps[0];        /* based on component 0 */
1684                         for (i = 0; i < tilec_idx->numresolutions; i++) {
1685                                 opj_tcd_resolution_t *res_idx = &tilec_idx->resolutions[i];
1686                                 
1687                                 cstr_info->tile[tileno].pw[i] = res_idx->pw;
1688                                 cstr_info->tile[tileno].ph[i] = res_idx->ph;
1689                                 
1690                                 numpacks += res_idx->pw * res_idx->ph;
1691                                 
1692                                 cstr_info->tile[tileno].pdx[i] = tccp->prcw[i];
1693                                 cstr_info->tile[tileno].pdy[i] = tccp->prch[i];
1694                         }
1695                         cstr_info->tile[tileno].packet = (opj_packet_info_t*) opj_calloc(cstr_info->numcomps * cstr_info->numlayers * numpacks, sizeof(opj_packet_info_t));
1696                 }
1697                 /* << INDEX */
1698                 
1699                 /*---------------TILE-------------------*/
1700                 
1701                 for (compno = 0; compno < tile->numcomps; compno++) {
1702                         int x, y;
1703                         
1704                         int adjust = image->comps[compno].sgnd ? 0 : 1 << (image->comps[compno].prec - 1);
1705                         int offset_x = int_ceildiv(image->x0, image->comps[compno].dx);
1706                         int offset_y = int_ceildiv(image->y0, image->comps[compno].dy);
1707                         
1708                         opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
1709                         int tw = tilec->x1 - tilec->x0;
1710                         int w = int_ceildiv(image->x1 - image->x0, image->comps[compno].dx);
1711                         
1712                         /* extract tile data */
1713                         
1714                         if (tcd_tcp->tccps[compno].qmfbid == 1) {
1715                                 for (y = tilec->y0; y < tilec->y1; y++) {
1716                                         /* start of the src tile scanline */
1717                                         int *data = &image->comps[compno].data[(tilec->x0 - offset_x) + (y - offset_y) * w];
1718                                         /* start of the dst tile scanline */
1719                                         int *tile_data = &tilec->data[(y - tilec->y0) * tw];
1720                                         for (x = tilec->x0; x < tilec->x1; x++) {
1721                                                 *tile_data++ = *data++ - adjust;
1722                                         }
1723                                 }
1724                         } else if (tcd_tcp->tccps[compno].qmfbid == 0) {
1725                                 for (y = tilec->y0; y < tilec->y1; y++) {
1726                                         /* start of the src tile scanline */
1727                                         int *data = &image->comps[compno].data[(tilec->x0 - offset_x) + (y - offset_y) * w];
1728                                         /* start of the dst tile scanline */
1729                                         int *tile_data = &tilec->data[(y - tilec->y0) * tw];
1730                                         for (x = tilec->x0; x < tilec->x1; x++) {
1731                                                 *tile_data++ = (*data++ - adjust) << 11;
1732                                         }
1733                                         
1734                                 }
1735                         }
1736                 }
1737                 
1738                 /*----------------MCT-------------------*/
1739                 if (tcd_tcp->mct) {
1740                         int samples = (tile->comps[0].x1 - tile->comps[0].x0) * (tile->comps[0].y1 - tile->comps[0].y0);
1741                         if (tcd_tcp->tccps[0].qmfbid == 0) {
1742                                 mct_encode_real(tile->comps[0].data, tile->comps[1].data, tile->comps[2].data, samples);
1743                         } else {
1744                                 mct_encode(tile->comps[0].data, tile->comps[1].data, tile->comps[2].data, samples);
1745                         }
1746                 }
1747                 
1748                 /*----------------DWT---------------------*/
1749                 
1750                 for (compno = 0; compno < tile->numcomps; compno++) {
1751                         opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
1752                         if (tcd_tcp->tccps[compno].qmfbid == 1) {
1753                                 dwt_encode(tilec);
1754                         } else if (tcd_tcp->tccps[compno].qmfbid == 0) {
1755                                 dwt_encode_real(tilec);
1756                         }
1757                 }
1758                 
1759                 /*------------------TIER1-----------------*/
1760                 t1 = t1_create(tcd->cinfo);
1761                 t1_encode_cblks(t1, tile, tcd_tcp);
1762                 t1_destroy(t1);
1763                 
1764                 /*-----------RATE-ALLOCATE------------------*/
1765                 
1766                 /* INDEX */
1767                 if(cstr_info) {
1768                         cstr_info->index_write = 0;
1769                 }
1770                 if (cp->disto_alloc || cp->fixed_quality) {     /* fixed_quality */
1771                         /* Normal Rate/distortion allocation */
1772                         tcd_rateallocate(tcd, dest, len, cstr_info);
1773                 } else {
1774                         /* Fixed layer allocation */
1775                         tcd_rateallocate_fixed(tcd);
1776                 }
1777         }
1778         /*--------------TIER2------------------*/
1779
1780         /* INDEX */
1781         if(cstr_info) {
1782                 cstr_info->index_write = 1;
1783         }
1784
1785         t2 = t2_create(tcd->cinfo, image, cp);
1786         l = t2_encode_packets(t2,tileno, tile, tcd_tcp->numlayers, dest, len, cstr_info,tcd->tp_num,tcd->tp_pos,tcd->cur_pino,FINAL_PASS,tcd->cur_totnum_tp);
1787         t2_destroy(t2);
1788         
1789         /*---------------CLEAN-------------------*/
1790
1791         
1792         if(tcd->cur_tp_num == tcd->cur_totnum_tp - 1){
1793                 tcd->encoding_time = opj_clock() - tcd->encoding_time;
1794                 opj_event_msg(tcd->cinfo, EVT_INFO, "- tile encoded in %f s\n", tcd->encoding_time);
1795
1796                 /* cleaning memory */
1797                 for (compno = 0; compno < tile->numcomps; compno++) {
1798                         opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
1799                         opj_aligned_free(tilec->data);
1800                 }
1801         }
1802
1803         return l;
1804 }
1805
1806 opj_bool tcd_decode_tile(opj_tcd_t *tcd, unsigned char *src, int len, int tileno, opj_codestream_info_t *cstr_info) {
1807         int l;
1808         int compno;
1809         int eof = 0;
1810         double tile_time, t1_time, dwt_time;
1811         opj_tcd_tile_t *tile = NULL;
1812
1813         opj_t1_t *t1 = NULL;            /* T1 component */
1814         opj_t2_t *t2 = NULL;            /* T2 component */
1815         
1816         tcd->tcd_tileno = tileno;
1817         tcd->tcd_tile = &(tcd->tcd_image->tiles[tileno]);
1818         tcd->tcp = &(tcd->cp->tcps[tileno]);
1819         tile = tcd->tcd_tile;
1820         
1821         tile_time = opj_clock();        /* time needed to decode a tile */
1822         opj_event_msg(tcd->cinfo, EVT_INFO, "tile %d of %d\n", tileno + 1, tcd->cp->tw * tcd->cp->th);
1823
1824         /* INDEX >>  */
1825         if(cstr_info) {
1826                 int resno, compno, numprec = 0;
1827                 for (compno = 0; compno < cstr_info->numcomps; compno++) {
1828                         opj_tcp_t *tcp = &tcd->cp->tcps[0];
1829                         opj_tccp_t *tccp = &tcp->tccps[compno];
1830                         opj_tcd_tilecomp_t *tilec_idx = &tile->comps[compno];   
1831                         for (resno = 0; resno < tilec_idx->numresolutions; resno++) {
1832                                 opj_tcd_resolution_t *res_idx = &tilec_idx->resolutions[resno];
1833                                 cstr_info->tile[tileno].pw[resno] = res_idx->pw;
1834                                 cstr_info->tile[tileno].ph[resno] = res_idx->ph;
1835                                 numprec += res_idx->pw * res_idx->ph;
1836                                 if (tccp->csty & J2K_CP_CSTY_PRT) {
1837                                         cstr_info->tile[tileno].pdx[resno] = tccp->prcw[resno];
1838                                         cstr_info->tile[tileno].pdy[resno] = tccp->prch[resno];
1839                                 }
1840                                 else {
1841                                         cstr_info->tile[tileno].pdx[resno] = 15;
1842                                         cstr_info->tile[tileno].pdy[resno] = 15;
1843                                 }
1844                         }
1845                 }
1846                 cstr_info->tile[tileno].packet = (opj_packet_info_t *) opj_malloc(cstr_info->numlayers * numprec * sizeof(opj_packet_info_t));
1847                 cstr_info->packno = 0;
1848         }
1849         /* << INDEX */
1850         
1851         /*--------------TIER2------------------*/
1852         
1853         t2 = t2_create(tcd->cinfo, tcd->image, tcd->cp);
1854         l = t2_decode_packets(t2, src, len, tileno, tile, cstr_info);
1855         t2_destroy(t2);
1856
1857         if (l == -999) {
1858                 eof = 1;
1859                 opj_event_msg(tcd->cinfo, EVT_ERROR, "tcd_decode: incomplete bistream\n");
1860         }
1861         
1862         /*------------------TIER1-----------------*/
1863         
1864         t1_time = opj_clock();  /* time needed to decode a tile */
1865         t1 = t1_create(tcd->cinfo);
1866         for (compno = 0; compno < tile->numcomps; ++compno) {
1867                 opj_tcd_tilecomp_t* tilec = &tile->comps[compno];
1868                 /* The +3 is headroom required by the vectorized DWT */
1869                 tilec->data = (int*) opj_aligned_malloc((((tilec->x1 - tilec->x0) * (tilec->y1 - tilec->y0))+3) * sizeof(int));
1870                 t1_decode_cblks(t1, tilec, &tcd->tcp->tccps[compno]);
1871         }
1872         t1_destroy(t1);
1873         t1_time = opj_clock() - t1_time;
1874         opj_event_msg(tcd->cinfo, EVT_INFO, "- tiers-1 took %f s\n", t1_time);
1875         
1876         /*----------------DWT---------------------*/
1877
1878         dwt_time = opj_clock(); /* time needed to decode a tile */
1879         for (compno = 0; compno < tile->numcomps; compno++) {
1880                 opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
1881                 int numres2decode;
1882
1883                 if (tcd->cp->reduce != 0) {
1884                         tcd->image->comps[compno].resno_decoded =
1885                                 tile->comps[compno].numresolutions - tcd->cp->reduce - 1;
1886                         if (tcd->image->comps[compno].resno_decoded < 0) {                              
1887                                 opj_event_msg(tcd->cinfo, EVT_ERROR, "Error decoding tile. The number of resolutions to remove [%d+1] is higher than the number "
1888                                         " of resolutions in the original codestream [%d]\nModify the cp_reduce parameter.\n", tcd->cp->reduce, tile->comps[compno].numresolutions);
1889                                 return OPJ_FALSE;
1890                         }
1891                 }
1892
1893                 numres2decode = tcd->image->comps[compno].resno_decoded + 1;
1894                 if(numres2decode > 0){
1895                         if (tcd->tcp->tccps[compno].qmfbid == 1) {
1896                                 dwt_decode(tilec, numres2decode);
1897                         } else {
1898                                 dwt_decode_real(tilec, numres2decode);
1899                         }
1900                 }
1901         }
1902         dwt_time = opj_clock() - dwt_time;
1903         opj_event_msg(tcd->cinfo, EVT_INFO, "- dwt took %f s\n", dwt_time);
1904
1905         /*----------------MCT-------------------*/
1906
1907         if (tcd->tcp->mct) {
1908                 int n = (tile->comps[0].x1 - tile->comps[0].x0) * (tile->comps[0].y1 - tile->comps[0].y0);
1909
1910                 if (tile->numcomps >= 3 ){
1911                         if (tcd->tcp->tccps[0].qmfbid == 1) {
1912                                 mct_decode(
1913                                                 tile->comps[0].data,
1914                                                 tile->comps[1].data,
1915                                                 tile->comps[2].data,
1916                                                 n);
1917                         } else {
1918                                 mct_decode_real(
1919                                                 (float*)tile->comps[0].data,
1920                                                 (float*)tile->comps[1].data,
1921                                                 (float*)tile->comps[2].data,
1922                                                 n);
1923                         }
1924                 } else{
1925                         opj_event_msg(tcd->cinfo, EVT_WARNING,"Number of components (%d) is inconsistent with a MCT. Skip the MCT step.\n",tile->numcomps);
1926                 }
1927         }
1928
1929         /*---------------TILE-------------------*/
1930
1931         for (compno = 0; compno < tile->numcomps; ++compno) {
1932                 opj_tcd_tilecomp_t* tilec = &tile->comps[compno];
1933                 opj_image_comp_t* imagec = &tcd->image->comps[compno];
1934                 opj_tcd_resolution_t* res = &tilec->resolutions[imagec->resno_decoded];
1935                 int adjust = imagec->sgnd ? 0 : 1 << (imagec->prec - 1);
1936                 int min = imagec->sgnd ? -(1 << (imagec->prec - 1)) : 0;
1937                 int max = imagec->sgnd ?  (1 << (imagec->prec - 1)) - 1 : (1 << imagec->prec) - 1;
1938
1939                 int tw = tilec->x1 - tilec->x0;
1940                 int w = imagec->w;
1941
1942                 int offset_x = int_ceildivpow2(imagec->x0, imagec->factor);
1943                 int offset_y = int_ceildivpow2(imagec->y0, imagec->factor);
1944
1945                 int i, j;
1946                 if(!imagec->data){
1947                         imagec->data = (int*) opj_malloc(imagec->w * imagec->h * sizeof(int));
1948                 }
1949                 if(tcd->tcp->tccps[compno].qmfbid == 1) {
1950                         for(j = res->y0; j < res->y1; ++j) {
1951                                 for(i = res->x0; i < res->x1; ++i) {
1952                                         int v = tilec->data[i - res->x0 + (j - res->y0) * tw];
1953                                         v += adjust;
1954                                         imagec->data[(i - offset_x) + (j - offset_y) * w] = int_clamp(v, min, max);
1955                                 }
1956                         }
1957                 }else{
1958                         for(j = res->y0; j < res->y1; ++j) {
1959                                 for(i = res->x0; i < res->x1; ++i) {
1960                                         float tmp = ((float*)tilec->data)[i - res->x0 + (j - res->y0) * tw];
1961                                         int v = lrintf(tmp);
1962                                         v += adjust;
1963                                         imagec->data[(i - offset_x) + (j - offset_y) * w] = int_clamp(v, min, max);
1964                                 }
1965                         }
1966                 }
1967                 opj_aligned_free(tilec->data);
1968         }
1969
1970         tile_time = opj_clock() - tile_time;    /* time needed to decode a tile */
1971         opj_event_msg(tcd->cinfo, EVT_INFO, "- tile decoded in %f s\n", tile_time);
1972
1973         if (eof) {
1974                 return OPJ_FALSE;
1975         }
1976         
1977         return OPJ_TRUE;
1978 }
1979
1980 void tcd_free_decode(opj_tcd_t *tcd) {
1981         opj_tcd_image_t *tcd_image = tcd->tcd_image;    
1982         opj_free(tcd_image->tiles);
1983 }
1984
1985 void tcd_free_decode_tile(opj_tcd_t *tcd, int tileno) {
1986         int compno,resno,bandno,precno;
1987
1988         opj_tcd_image_t *tcd_image = tcd->tcd_image;
1989
1990         opj_tcd_tile_t *tile = &tcd_image->tiles[tileno];
1991         for (compno = 0; compno < tile->numcomps; compno++) {
1992                 opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
1993                 for (resno = 0; resno < tilec->numresolutions; resno++) {
1994                         opj_tcd_resolution_t *res = &tilec->resolutions[resno];
1995                         for (bandno = 0; bandno < res->numbands; bandno++) {
1996                                 opj_tcd_band_t *band = &res->bands[bandno];
1997                                 for (precno = 0; precno < res->ph * res->pw; precno++) {
1998                                         opj_tcd_precinct_t *prec = &band->precincts[precno];
1999                                         if (prec->imsbtree != NULL) tgt_destroy(prec->imsbtree);
2000                                         if (prec->incltree != NULL) tgt_destroy(prec->incltree);
2001                                 }
2002                                 opj_free(band->precincts);
2003                         }
2004                 }
2005                 opj_free(tilec->resolutions);
2006         }
2007         opj_free(tile->comps);
2008 }
2009
2010
2011 opj_bool tcd_init_v2(  opj_tcd_v2_t *p_tcd,
2012                                            opj_image_t * p_image,
2013                                            opj_cp_v2_t * p_cp )
2014 {
2015         OPJ_UINT32 l_tile_comp_size;
2016
2017         p_tcd->image = p_image;
2018         p_tcd->cp = p_cp;
2019
2020         p_tcd->tcd_image->tiles = (opj_tcd_tile_v2_t *) opj_malloc(sizeof(opj_tcd_tile_v2_t));
2021         if (! p_tcd->tcd_image->tiles) {
2022                 return OPJ_FALSE;
2023         }
2024         memset(p_tcd->tcd_image->tiles,0, sizeof(opj_tcd_tile_v2_t));
2025
2026         l_tile_comp_size = p_image->numcomps * sizeof(opj_tcd_tilecomp_v2_t);
2027         p_tcd->tcd_image->tiles->comps = (opj_tcd_tilecomp_v2_t *) opj_malloc(l_tile_comp_size);
2028         if (! p_tcd->tcd_image->tiles->comps ) {
2029                 return OPJ_FALSE;
2030         }
2031         memset( p_tcd->tcd_image->tiles->comps , 0 , l_tile_comp_size);
2032
2033         p_tcd->tcd_image->tiles->numcomps = p_image->numcomps;
2034         p_tcd->tp_pos = p_cp->m_specific_param.m_enc.m_tp_pos;
2035
2036         return OPJ_TRUE;
2037 }
2038
2039 /**
2040 Destroy a previously created TCD handle
2041 */
2042 void tcd_destroy_v2(opj_tcd_v2_t *tcd) {
2043         if (tcd) {
2044                 tcd_free_tile(tcd);
2045
2046                 if (tcd->tcd_image) {
2047                         opj_free(tcd->tcd_image);
2048                         tcd->tcd_image = 00;
2049                 }
2050                 opj_free(tcd);
2051         }
2052 }
2053
2054 /* ----------------------------------------------------------------------- */
2055 /**
2056  * Initialize the tile coder and may reuse some meory.
2057  * @param       p_tcd           TCD handle.
2058  * @param       p_image         raw image.
2059  * @param       p_cp            coding parameters.
2060  * @param       p_tile_no       current tile index to encode.
2061  *
2062  * @return true if the encoding values could be set (false otherwise).
2063 */
2064 #define MACRO_TCD_ALLOCATE(FUNCTION,TYPE,FRACTION,ELEMENT,FUNCTION_ELEMENT)             \
2065 opj_bool FUNCTION (     opj_tcd_v2_t *p_tcd,                                                                            \
2066                                         OPJ_UINT32 p_tile_no                                                                            \
2067                         )                                                                                                                                       \
2068 {                                                                                                                                                               \
2069         OPJ_UINT32 (*l_gain_ptr)(OPJ_UINT32) = 00;                                                                      \
2070         OPJ_UINT32 compno, resno, bandno, precno, cblkno;                                                       \
2071         opj_tcp_v2_t * l_tcp = 00;                                                                                                      \
2072         opj_cp_v2_t * l_cp = 00;                                                                                                        \
2073         opj_tcd_tile_v2_t * l_tile = 00;                                                                                        \
2074         opj_tccp_t *l_tccp = 00;                                                                                                        \
2075         opj_tcd_tilecomp_v2_t *l_tilec = 00;                                                                            \
2076         opj_image_comp_t * l_image_comp = 00;                                                                           \
2077         opj_tcd_resolution_v2_t *l_res = 00;                                                                            \
2078         opj_tcd_band_v2_t *l_band = 00;                                                                                         \
2079         opj_stepsize_t * l_step_size = 00;                                                                                      \
2080         opj_tcd_precinct_v2_t *l_current_precinct = 00;                                                         \
2081         TYPE* l_code_block = 00;                                                                                                        \
2082         opj_image_t *l_image = 00;                                                                                                      \
2083         OPJ_UINT32 p,q;                                                                                                                         \
2084         OPJ_UINT32 l_level_no;                                                                                                          \
2085         OPJ_UINT32 l_pdx, l_pdy;                                                                                                        \
2086         OPJ_UINT32 l_gain;                                                                                                                      \
2087         OPJ_INT32 l_x0b, l_y0b;                                                                                                         \
2088         /* extent of precincts , top left, bottom right**/                                                      \
2089         OPJ_INT32 l_tl_prc_x_start, l_tl_prc_y_start, l_br_prc_x_end, l_br_prc_y_end;   \
2090         /* number of precinct for a resolution */                                                                       \
2091         OPJ_UINT32 l_nb_precincts;                                                                                                      \
2092         /* room needed to store l_nb_precinct precinct for a resolution */                      \
2093         OPJ_UINT32 l_nb_precinct_size;                                                                                          \
2094         /* number of code blocks for a precinct*/                                                                       \
2095         OPJ_UINT32 l_nb_code_blocks;                                                                                            \
2096         /* room needed to store l_nb_code_blocks code blocks for a precinct*/           \
2097         OPJ_UINT32 l_nb_code_blocks_size;                                                                                       \
2098         /* size of data for a tile */                                                                                           \
2099         OPJ_UINT32 l_data_size;                                                                                                         \
2100                                                                                                                                                                 \
2101         l_cp = p_tcd->cp;                                                                                                                       \
2102         l_tcp = &(l_cp->tcps[p_tile_no]);                                                                                       \
2103         l_tile = p_tcd->tcd_image->tiles;                                                                                       \
2104         l_tccp = l_tcp->tccps;                                                                                                          \
2105         l_tilec = l_tile->comps;                                                                                                        \
2106         l_image = p_tcd->image;                                                                                                         \
2107         l_image_comp = p_tcd->image->comps;                                                                                     \
2108                                                                                                                                                                 \
2109         p = p_tile_no % l_cp->tw;       /* tile coordinates */                                                  \
2110         q = p_tile_no / l_cp->tw;                                                                                                       \
2111         /*fprintf(stderr, "Tile coordinate = %d,%d\n", p, q);*/                                         \
2112                                                                                                                                                                 \
2113         /* 4 borders of the tile rescale on the image if necessary */                           \
2114         l_tile->x0 = int_max(l_cp->tx0 + p * l_cp->tdx, l_image->x0);                           \
2115         l_tile->y0 = int_max(l_cp->ty0 + q * l_cp->tdy, l_image->y0);                           \
2116         l_tile->x1 = int_min(l_cp->tx0 + (p + 1) * l_cp->tdx, l_image->x1);                     \
2117         l_tile->y1 = int_min(l_cp->ty0 + (q + 1) * l_cp->tdy, l_image->y1);                     \
2118         /*fprintf(stderr, "Tile border = %d,%d,%d,%d\n", l_tile->x0, l_tile->y0,l_tile->x1,l_tile->y1);*/\
2119                                                                                                                                                                 \
2120         /*tile->numcomps = image->numcomps; */                                                                          \
2121         for(compno = 0; compno < l_tile->numcomps; ++compno) {                                          \
2122                 /*fprintf(stderr, "compno = %d/%d\n", compno, l_tile->numcomps);*/              \
2123                                                                                                                                                                 \
2124                 /* border of each l_tile component (global) */                                                  \
2125                 l_tilec->x0 = int_ceildiv(l_tile->x0, l_image_comp->dx);                                \
2126                 l_tilec->y0 = int_ceildiv(l_tile->y0, l_image_comp->dy);                                \
2127                 l_tilec->x1 = int_ceildiv(l_tile->x1, l_image_comp->dx);                                \
2128                 l_tilec->y1 = int_ceildiv(l_tile->y1, l_image_comp->dy);                                \
2129                 /*fprintf(stderr, "\tTile compo border = %d,%d,%d,%d\n", l_tilec->x0, l_tilec->y0,l_tilec->x1,l_tilec->y1);*/\
2130                                                                                                                                                                 \
2131                 l_data_size = (l_tilec->x1 - l_tilec->x0)                                                               \
2132                                         * (l_tilec->y1 - l_tilec->y0) * sizeof(OPJ_UINT32 );            \
2133                 l_tilec->numresolutions = l_tccp->numresolutions;                                               \
2134                 if (l_tccp->numresolutions < l_cp->m_specific_param.m_dec.m_reduce) {   \
2135                         l_tilec->minimum_num_resolutions = 1;                                                           \
2136                 }                                                                                                                                               \
2137                 else {                                                                                                                                  \
2138                         l_tilec->minimum_num_resolutions = l_tccp->numresolutions                       \
2139                                 - l_cp->m_specific_param.m_dec.m_reduce;                                                \
2140                 }                                                                                                                                               \
2141                                                                                                                                                                 \
2142                 if (l_tilec->data == 00) {                                                                                              \
2143             l_tilec->data = (OPJ_INT32 *) opj_malloc(l_data_size);              \
2144                         if (! l_tilec->data ) {                                                                                         \
2145                                 return OPJ_FALSE;                                                                                               \
2146                         }                                                                                                                                       \
2147             /*fprintf(stderr, "\tAllocate data of tilec (int): %d x OPJ_UINT32\n",l_data_size);*/       \
2148                                                                                                                                                                 \
2149                         l_tilec->data_size = l_data_size;                                                                       \
2150                 }                                                                                                                                               \
2151                 else if (l_data_size > l_tilec->data_size) {                                                    \
2152                         l_tilec->data = (OPJ_INT32 *) opj_realloc(l_tilec->data, l_data_size);  \
2153                         if (! l_tilec->data) {                                                                                          \
2154                                 return OPJ_FALSE;                                                                                               \
2155                         }                                                                                                                                       \
2156                         /*fprintf(stderr, "\tReallocate data of tilec (int): from %d to %d x OPJ_UINT32\n", l_tilec->data_size, l_data_size);*/         \
2157                         l_tilec->data_size = l_data_size;                                                                       \
2158                 }                                                                                                                                               \
2159                                                                                                                                                         \
2160                 l_data_size = l_tilec->numresolutions * sizeof(opj_tcd_resolution_v2_t);        \
2161                                                                                                                                                                 \
2162                 if (l_tilec->resolutions == 00) {                                                                               \
2163             l_tilec->resolutions = (opj_tcd_resolution_v2_t *) opj_malloc(l_data_size); \
2164                         if (! l_tilec->resolutions ) {                                                                          \
2165                                 return OPJ_FALSE;                                                                                               \
2166                         }                                                                                                                                       \
2167                         /*fprintf(stderr, "\tAllocate resolutions of tilec (opj_tcd_resolution_v2_t): %d\n",l_data_size);*/             \
2168                         l_tilec->resolutions_size = l_data_size;                                                        \
2169                         memset(l_tilec->resolutions,0,l_data_size);                                                     \
2170                 }                                                                                                                                               \
2171                 else if (l_data_size > l_tilec->resolutions_size) {                                             \
2172                         l_tilec->resolutions = (opj_tcd_resolution_v2_t *) opj_realloc(l_tilec->resolutions, l_data_size);      \
2173                         if (! l_tilec->resolutions) {                                                                           \
2174                                 return OPJ_FALSE;                                                                                               \
2175                         }                                                                                                                                       \
2176                         /*fprintf(stderr, "\tReallocate data of tilec (int): from %d to %d x OPJ_UINT32\n", l_tilec->resolutions_size, l_data_size);*/  \
2177                         memset(((OPJ_BYTE*) l_tilec->resolutions)+l_tilec->resolutions_size,0,l_data_size - l_tilec->resolutions_size); \
2178                         l_tilec->resolutions_size = l_data_size;                                                        \
2179                 }                                                                                                                                               \
2180                                                                                                                                                                 \
2181                 l_level_no = l_tilec->numresolutions - 1;                                                               \
2182                 l_res = l_tilec->resolutions;                                                                                   \
2183                 l_step_size = l_tccp->stepsizes;                                                                                \
2184                 if (l_tccp->qmfbid == 0) {                                                                                              \
2185                         l_gain_ptr = &dwt_getgain_real_v2;                                                                      \
2186                 }                                                                                                                                               \
2187                 else {                                                                                                                                  \
2188                         l_gain_ptr  = &dwt_getgain_v2;                                                                          \
2189                 }                                                                                                                                               \
2190                 /*fprintf(stderr, "\tlevel_no=%d\n",l_level_no);*/                                              \
2191                                                                                                                                                                 \
2192                 for(resno = 0; resno < l_tilec->numresolutions; ++resno) {                              \
2193                         /*fprintf(stderr, "\t\tresno = %d/%d\n", resno, l_tilec->numresolutions);*/     \
2194                         OPJ_INT32 tlcbgxstart, tlcbgystart, brcbgxend, brcbgyend;                       \
2195                         OPJ_UINT32 cbgwidthexpn, cbgheightexpn;                                                         \
2196                         OPJ_UINT32 cblkwidthexpn, cblkheightexpn;                                                       \
2197                                                                                                                                                                 \
2198                         /* border for each resolution level (global) */                                         \
2199                         l_res->x0 = int_ceildivpow2(l_tilec->x0, l_level_no);                           \
2200                         l_res->y0 = int_ceildivpow2(l_tilec->y0, l_level_no);                           \
2201                         l_res->x1 = int_ceildivpow2(l_tilec->x1, l_level_no);                           \
2202                         l_res->y1 = int_ceildivpow2(l_tilec->y1, l_level_no);                           \
2203                         /*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);*/        \
2204                         /* p. 35, table A-23, ISO/IEC FDIS154444-1 : 2000 (18 august 2000) */   \
2205                         l_pdx = l_tccp->prcw[resno];                                                                            \
2206                         l_pdy = l_tccp->prch[resno];                                                                            \
2207                         /*fprintf(stderr, "\t\t\tpdx=%d, pdy=%d\n", l_pdx, l_pdy);*/            \
2208                         /* p. 64, B.6, ISO/IEC FDIS15444-1 : 2000 (18 august 2000)  */          \
2209                         l_tl_prc_x_start = int_floordivpow2(l_res->x0, l_pdx) << l_pdx;         \
2210                         l_tl_prc_y_start = int_floordivpow2(l_res->y0, l_pdy) << l_pdy;         \
2211                         l_br_prc_x_end = int_ceildivpow2(l_res->x1, l_pdx) << l_pdx;            \
2212                         l_br_prc_y_end = int_ceildivpow2(l_res->y1, l_pdy) << l_pdy;            \
2213                         /*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 );*/  \
2214                                                                                                                                                                 \
2215                         l_res->pw = (l_res->x0 == l_res->x1) ? 0 : ((l_br_prc_x_end - l_tl_prc_x_start) >> l_pdx);      \
2216                         l_res->ph = (l_res->y0 == l_res->y1) ? 0 : ((l_br_prc_y_end - l_tl_prc_y_start) >> l_pdy);      \
2217                         /*fprintf(stderr, "\t\t\tres_pw=%d, res_ph=%d\n", l_res->pw, l_res->ph );*/     \
2218                                                                                                                                                                 \
2219                         l_nb_precincts = l_res->pw * l_res->ph;                                                         \
2220                         l_nb_precinct_size = l_nb_precincts * sizeof(opj_tcd_precinct_v2_t);    \
2221                         if (resno == 0) {                                                                                                       \
2222                                 tlcbgxstart = l_tl_prc_x_start;                                                                 \
2223                                 tlcbgystart = l_tl_prc_y_start;                                                                 \
2224                                 brcbgxend = l_br_prc_x_end;                                                                             \
2225                                 brcbgyend = l_br_prc_y_end;                                                                             \
2226                                 cbgwidthexpn = l_pdx;                                                                                   \
2227                                 cbgheightexpn = l_pdy;                                                                                  \
2228                                 l_res->numbands = 1;                                                                                    \
2229                         }                                                                                                                                       \
2230                         else {                                                                                                                          \
2231                                 tlcbgxstart = int_ceildivpow2(l_tl_prc_x_start, 1);                             \
2232                                 tlcbgystart = int_ceildivpow2(l_tl_prc_y_start, 1);                             \
2233                                 brcbgxend = int_ceildivpow2(l_br_prc_x_end, 1);                                 \
2234                                 brcbgyend = int_ceildivpow2(l_br_prc_y_end, 1);                                 \
2235                                 cbgwidthexpn = l_pdx - 1;                                                                               \
2236                                 cbgheightexpn = l_pdy - 1;                                                                              \
2237                                 l_res->numbands = 3;                                                                                    \
2238                         }                                                                                                                                       \
2239                                                                                                                                                                 \
2240                         cblkwidthexpn = uint_min(l_tccp->cblkw, cbgwidthexpn);                          \
2241                         cblkheightexpn = uint_min(l_tccp->cblkh, cbgheightexpn);                        \
2242                         l_band = l_res->bands;                                                                                          \
2243                                                                                                                                                                 \
2244                         for (bandno = 0; bandno < l_res->numbands; ++bandno) {                          \
2245                                 OPJ_INT32 numbps;\
2246                                 /*fprintf(stderr, "\t\t\tband_no=%d/%d\n", bandno, l_res->numbands );*/ \
2247                                                                                                                                                                 \
2248                                 if (resno == 0) {                                                                                               \
2249                                         l_band->bandno = 0 ;                                                                            \
2250                                         l_band->x0 = int_ceildivpow2(l_tilec->x0, l_level_no);          \
2251                                         l_band->y0 = int_ceildivpow2(l_tilec->y0, l_level_no);          \
2252                                         l_band->x1 = int_ceildivpow2(l_tilec->x1, l_level_no);          \
2253                                         l_band->y1 = int_ceildivpow2(l_tilec->y1, l_level_no);          \
2254                                 }                                                                                                                               \
2255                                 else {                                                                                                                  \
2256                                         l_band->bandno = bandno + 1;                                                            \
2257                                         /* x0b = 1 if bandno = 1 or 3 */                                                        \
2258                                         l_x0b = l_band->bandno&1;                                                                       \
2259                                         /* y0b = 1 if bandno = 2 or 3 */                                                        \
2260                                         l_y0b = (l_band->bandno)>>1;                                                            \
2261                                         /* l_band border (global) */                                                            \
2262                                         l_band->x0 = int_ceildivpow2(l_tilec->x0 - (1 << l_level_no) * l_x0b, l_level_no + 1);  \
2263                                         l_band->y0 = int_ceildivpow2(l_tilec->y0 - (1 << l_level_no) * l_y0b, l_level_no + 1);  \
2264                                         l_band->x1 = int_ceildivpow2(l_tilec->x1 - (1 << l_level_no) * l_x0b, l_level_no + 1);  \
2265                                         l_band->y1 = int_ceildivpow2(l_tilec->y1 - (1 << l_level_no) * l_y0b, l_level_no + 1);  \
2266                                 }                                                                                                                               \
2267                                                                                                                                                                 \
2268                                 /** avoid an if with storing function pointer */                                \
2269                                 l_gain = (*l_gain_ptr) (l_band->bandno);                                                \
2270                                 numbps = l_image_comp->prec + l_gain;                                                   \
2271                                 l_band->stepsize = (OPJ_FLOAT32)(((1.0 + l_step_size->mant / 2048.0) * pow(2.0, (OPJ_INT32) (numbps - l_step_size->expn)))) * FRACTION;\
2272                                 l_band->numbps = l_step_size->expn + l_tccp->numgbits - 1;      /* WHY -1 ? */\
2273                                                                                                                                                                 \
2274                                 if (! l_band->precincts) {                                                                              \
2275                                         l_band->precincts = (opj_tcd_precinct_v2_t *) opj_malloc( /*3 * */ l_nb_precinct_size);\
2276                                         if (! l_band->precincts) {                                                                      \
2277                                                 return OPJ_FALSE;                                                                               \
2278                                         }                                                                                                                       \
2279                                         /*fprintf(stderr, "\t\t\t\tAllocate precincts of a band (opj_tcd_precinct_v2_t): %d\n",l_nb_precinct_size);     */      \
2280                                         memset(l_band->precincts,0,l_nb_precinct_size);                         \
2281                                         l_band->precincts_data_size = l_nb_precinct_size;                       \
2282                                 }                                                                                                                               \
2283                                 else if (l_band->precincts_data_size < l_nb_precinct_size) {    \
2284                                                                                                                                                                 \
2285                                         l_band->precincts = (opj_tcd_precinct_v2_t *) opj_realloc(l_band->precincts,/*3 * */ l_nb_precinct_size);\
2286                                         if (! l_band->precincts) {                                                                      \
2287                                                 return OPJ_FALSE;                                                                               \
2288                                         }                                                                                                                       \
2289                                         /*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);*/\
2290                                         memset(((OPJ_BYTE *) l_band->precincts) + l_band->precincts_data_size,0,l_nb_precinct_size - l_band->precincts_data_size);\
2291                                         l_band->precincts_data_size = l_nb_precinct_size;                       \
2292                                 }                                                                                                                               \
2293                                                                                                                                                                 \
2294                                 l_current_precinct = l_band->precincts;                                                 \
2295                                 for     (precno = 0; precno < l_nb_precincts; ++precno) {                       \
2296                                         OPJ_INT32 tlcblkxstart, tlcblkystart, brcblkxend, brcblkyend;   \
2297                                         OPJ_INT32 cbgxstart = tlcbgxstart + (precno % l_res->pw) * (1 << cbgwidthexpn); \
2298                                         OPJ_INT32 cbgystart = tlcbgystart + (precno / l_res->pw) * (1 << cbgheightexpn);        \
2299                                         OPJ_INT32 cbgxend = cbgxstart + (1 << cbgwidthexpn);            \
2300                                         OPJ_INT32 cbgyend = cbgystart + (1 << cbgheightexpn);           \
2301                                         /*fprintf(stderr, "\t precno=%d; bandno=%d, resno=%d; compno=%d\n", precno, bandno , resno, compno);*/\
2302                                         /*fprintf(stderr, "\t tlcbgxstart(=%d) + (precno(=%d) percent res->pw(=%d)) * (1 << cbgwidthexpn(=%d)) \n",tlcbgxstart,precno,l_res->pw,cbgwidthexpn);*/\
2303                                                                                                                                                                 \
2304                                         /* precinct size (global) */                                                            \
2305                                         /*fprintf(stderr, "\t cbgxstart=%d, l_band->x0 = %d \n",cbgxstart, l_band->x0);*/ \
2306                                                                                                                                                                 \
2307                                         l_current_precinct->x0 = int_max(cbgxstart, l_band->x0);        \
2308                                         l_current_precinct->y0 = int_max(cbgystart, l_band->y0);        \
2309                                         l_current_precinct->x1 = int_min(cbgxend, l_band->x1);          \
2310                                         l_current_precinct->y1 = int_min(cbgyend, l_band->y1);          \
2311                                         /*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);*/ \
2312                                                                                                                                                                 \
2313                                         tlcblkxstart = int_floordivpow2(l_current_precinct->x0, cblkwidthexpn) << cblkwidthexpn;        \
2314                                         /*fprintf(stderr, "\t tlcblkxstart =%d\n",tlcblkxstart );*/ \
2315                                         tlcblkystart = int_floordivpow2(l_current_precinct->y0, cblkheightexpn) << cblkheightexpn;      \
2316                                         /*fprintf(stderr, "\t tlcblkystart =%d\n",tlcblkystart );*/ \
2317                                         brcblkxend = int_ceildivpow2(l_current_precinct->x1, cblkwidthexpn) << cblkwidthexpn;   \
2318                                         /*fprintf(stderr, "\t brcblkxend =%d\n",brcblkxend );*/         \
2319                                         brcblkyend = int_ceildivpow2(l_current_precinct->y1, cblkheightexpn) << cblkheightexpn; \
2320                                         /*fprintf(stderr, "\t brcblkyend =%d\n",brcblkyend );*/         \
2321                                         l_current_precinct->cw = (brcblkxend - tlcblkxstart) >> cblkwidthexpn;  \
2322                                         l_current_precinct->ch = (brcblkyend - tlcblkystart) >> cblkheightexpn; \
2323                                                                                                                                                                 \
2324                                         l_nb_code_blocks = l_current_precinct->cw * l_current_precinct->ch;     \
2325                                         /*fprintf(stderr, "\t\t\t\t precinct_cw = %d x recinct_ch = %d\n",l_current_precinct->cw, l_current_precinct->ch);      */      \
2326                                         l_nb_code_blocks_size = l_nb_code_blocks * sizeof(TYPE);        \
2327                                                                                                                                                                 \
2328                                         if (! l_current_precinct->cblks.ELEMENT) {                                      \
2329                                                 l_current_precinct->cblks.ELEMENT = (TYPE*) opj_malloc(l_nb_code_blocks_size);\
2330                                                 if (! l_current_precinct->cblks.ELEMENT ) {                             \
2331                                                         return OPJ_FALSE;                                                                       \
2332                                                 }                                                                                                               \
2333                                                 /*fprintf(stderr, "\t\t\t\tAllocate cblks of a precinct (opj_tcd_cblk_dec_v2_t): %d\n",l_nb_code_blocks_size);*/                \
2334                                                                                                                                                                 \
2335                                                 memset(l_current_precinct->cblks.ELEMENT,0,l_nb_code_blocks_size);\
2336                                                                                                                                                                 \
2337                                                 l_current_precinct->block_size = l_nb_code_blocks_size; \
2338                                         }                                                                                                                       \
2339                                         else if (l_nb_code_blocks_size > l_current_precinct->block_size) {      \
2340                                                 l_current_precinct->cblks.ELEMENT = (TYPE*)                                                                     \
2341                                                         opj_realloc(l_current_precinct->cblks.ELEMENT, l_nb_code_blocks_size);  \
2342                                                 if (! l_current_precinct->cblks.ELEMENT ) {                             \
2343                                                         return OPJ_FALSE;                                                                       \
2344                                                 }                                                                                                               \
2345                                                 /*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);     */      \
2346                                                                                                                                                                 \
2347                                                 memset(((OPJ_BYTE *) l_current_precinct->cblks.ELEMENT) + l_current_precinct->block_size        \
2348                                                                                 ,0                                                                                                                                                      \
2349                                                                                 ,l_nb_code_blocks_size - l_current_precinct->block_size);                                       \
2350                                                                                                                                                                 \
2351                                                 l_current_precinct->block_size = l_nb_code_blocks_size; \
2352                                         }                                                                                                                       \
2353                                                                                                                                                                 \
2354                                         if (! l_current_precinct->incltree) {                                           \
2355                         l_current_precinct->incltree = tgt_create_v2(l_current_precinct->cw,    \
2356                                                                                                                                   l_current_precinct->ch);      \
2357                                         }                                                                                                                       \
2358                                         else{                                                                                                           \
2359                                                 l_current_precinct->incltree = tgt_init(l_current_precinct->incltree,   \
2360                                                                                                                                 l_current_precinct->cw,                 \
2361                                                                                                                                 l_current_precinct->ch);                \
2362                                         }                                                                                                                       \
2363                                                                                                                                                                 \
2364                                         if (! l_current_precinct->incltree)     {                                               \
2365                                                 fprintf(stderr, "WARNING: No incltree created.\n");\
2366                                                 /*return OPJ_FALSE;*/                                                                           \
2367                                         }                                                                                                                       \
2368                                                                                                                                                                 \
2369                                         if (! l_current_precinct->imsbtree) {                                           \
2370                         l_current_precinct->imsbtree = tgt_create_v2(                           \
2371                                                                                                                 l_current_precinct->cw, \
2372                                                                                                                 l_current_precinct->ch);\
2373                                         }                                                                                                                       \
2374                                         else {                                                                                                          \
2375                                                 l_current_precinct->imsbtree = tgt_init(                                                        \
2376                                                                                                                         l_current_precinct->imsbtree,   \
2377                                                                                                                         l_current_precinct->cw,                 \
2378                                                                                                                         l_current_precinct->ch);                \
2379                                         }                                                                                                                       \
2380                                                                                                                                                                 \
2381                                         if (! l_current_precinct->imsbtree) {                                           \
2382                                                 fprintf(stderr, "WARNING: No imsbtree created.\n");\
2383                                                 /*return OPJ_FALSE;*/                                                                           \
2384                                         }                                                                                                                       \
2385                                                                                                                                                                 \
2386                                         l_code_block = l_current_precinct->cblks.ELEMENT;                       \
2387                                                                                                                                                                 \
2388                                         for (cblkno = 0; cblkno < l_nb_code_blocks; ++cblkno) {         \
2389                                                 OPJ_INT32 cblkxstart = tlcblkxstart + (cblkno % l_current_precinct->cw) * (1 << cblkwidthexpn);         \
2390                                                 OPJ_INT32 cblkystart = tlcblkystart + (cblkno / l_current_precinct->cw) * (1 << cblkheightexpn);        \
2391                                                 OPJ_INT32 cblkxend = cblkxstart + (1 << cblkwidthexpn); \
2392                                                 OPJ_INT32 cblkyend = cblkystart + (1 << cblkheightexpn);\
2393                                                                                                                                                                 \
2394                                                 /* code-block size (global) */                                                  \
2395                                                 l_code_block->x0 = int_max(cblkxstart, l_current_precinct->x0); \
2396                                                 l_code_block->y0 = int_max(cblkystart, l_current_precinct->y0); \
2397                                                 l_code_block->x1 = int_min(cblkxend, l_current_precinct->x1);   \
2398                                                 l_code_block->y1 = int_min(cblkyend, l_current_precinct->y1);   \
2399                                                                                                                                                                 \
2400                                                 if (! FUNCTION_ELEMENT(l_code_block)) {                                 \
2401                                                         return OPJ_FALSE;                                                                       \
2402                                                 }                                                                                                               \
2403                                                 ++l_code_block;                                                                                 \
2404                                         }                                                                                                                       \
2405                                         ++l_current_precinct;                                                                           \
2406                                 } /* precno */                                                                                                  \
2407                                 ++l_band;                                                                                                               \
2408                                 ++l_step_size;                                                                                                  \
2409                         } /* bandno */                                                                                                          \
2410                         ++l_res;                                                                                                                        \
2411                         --l_level_no;                                                                                                           \
2412                 } /* resno */                                                                                                                   \
2413                 ++l_tccp;                                                                                                                               \
2414                 ++l_tilec;                                                                                                                              \
2415                 ++l_image_comp;                                                                                                                 \
2416         } /* compno */                                                                                                                          \
2417         return OPJ_TRUE;                                                                                                                        \
2418 }                                                                                                                                                               \
2419
2420
2421 MACRO_TCD_ALLOCATE(tcd_init_encode_tile, opj_tcd_cblk_enc_v2_t, 1.f, enc, tcd_code_block_enc_allocate)
2422 MACRO_TCD_ALLOCATE(tcd_init_decode_tile, opj_tcd_cblk_dec_v2_t, 0.5f, dec, tcd_code_block_dec_allocate)
2423
2424 #undef MACRO_TCD_ALLOCATE
2425
2426 /**
2427  * Allocates memory for an encoding code block.
2428  */
2429 opj_bool tcd_code_block_enc_allocate (opj_tcd_cblk_enc_v2_t * p_code_block)
2430 {
2431         if (! p_code_block->data) {
2432
2433                 p_code_block->data = (OPJ_BYTE*) opj_malloc(8192+1);
2434                 if(! p_code_block->data) {
2435                         return OPJ_FALSE;
2436                 }
2437
2438                 p_code_block->data[0] = 0;
2439                 p_code_block->data+=1;
2440
2441                 /* no memset since data */
2442                 p_code_block->layers = (opj_tcd_layer_t*) opj_malloc(100 * sizeof(opj_tcd_layer_t));
2443                 if (! p_code_block->layers) {
2444                         return OPJ_FALSE;
2445                 }
2446
2447                 p_code_block->passes = (opj_tcd_pass_v2_t*) opj_malloc(100 * sizeof(opj_tcd_pass_v2_t));
2448                 if (! p_code_block->passes) {
2449                         return OPJ_FALSE;
2450                 }
2451         }
2452
2453         memset(p_code_block->layers,0,100 * sizeof(opj_tcd_layer_t));
2454         memset(p_code_block->passes,0,100 * sizeof(opj_tcd_pass_v2_t));
2455
2456         return OPJ_TRUE;
2457 }
2458
2459 /**
2460  * Allocates memory for a decoding code block.
2461  */
2462 opj_bool tcd_code_block_dec_allocate (opj_tcd_cblk_dec_v2_t * p_code_block)
2463 {
2464         OPJ_UINT32 l_seg_size;
2465
2466         if (! p_code_block->data) {
2467
2468                 p_code_block->data = (OPJ_BYTE*) opj_malloc(8192);
2469                 if (! p_code_block->data) {
2470                         return OPJ_FALSE;
2471                 }
2472                 /*fprintf(stderr, "Allocate 8192 elements of code_block->data\n");*/
2473
2474                 l_seg_size = J2K_DEFAULT_NB_SEGS * sizeof(opj_tcd_seg_t);
2475                 p_code_block->segs = (opj_tcd_seg_t *) opj_malloc(l_seg_size);
2476                 if (! p_code_block->segs) {
2477                         return OPJ_FALSE;
2478                 }
2479                 memset(p_code_block->segs,0,l_seg_size);
2480                 /*fprintf(stderr, "Allocate %d elements of code_block->data\n", J2K_DEFAULT_NB_SEGS * sizeof(opj_tcd_seg_t));*/
2481
2482                 p_code_block->m_current_max_segs = J2K_DEFAULT_NB_SEGS;
2483                 /*fprintf(stderr, "m_current_max_segs of code_block->data = %d\n", p_code_block->m_current_max_segs);*/
2484         }
2485         /* TODO */
2486         /*p_code_block->numsegs = 0; */
2487
2488         return OPJ_TRUE;
2489 }
2490
2491 OPJ_UINT32 tcd_get_decoded_tile_size ( opj_tcd_v2_t *p_tcd )
2492 {
2493         OPJ_UINT32 i;
2494         OPJ_UINT32 l_data_size = 0;
2495         opj_image_comp_t * l_img_comp = 00;
2496         opj_tcd_tilecomp_v2_t * l_tile_comp = 00;
2497         opj_tcd_resolution_v2_t * l_res = 00;
2498         OPJ_UINT32 l_size_comp, l_remaining;
2499
2500         l_tile_comp = p_tcd->tcd_image->tiles->comps;
2501         l_img_comp = p_tcd->image->comps;
2502
2503         for (i=0;i<p_tcd->image->numcomps;++i) {
2504                 l_size_comp = l_img_comp->prec >> 3; /*(/ 8)*/
2505                 l_remaining = l_img_comp->prec & 7;  /* (%8) */
2506
2507                 if(l_remaining) {
2508                         ++l_size_comp;
2509                 }
2510
2511                 if (l_size_comp == 3) {
2512                         l_size_comp = 4;
2513                 }
2514
2515                 l_res = l_tile_comp->resolutions + l_tile_comp->minimum_num_resolutions - 1;
2516                 l_data_size += l_size_comp * (l_res->x1 - l_res->x0) * (l_res->y1 - l_res->y0);
2517                 ++l_img_comp;
2518                 ++l_tile_comp;
2519         }
2520
2521         return l_data_size;
2522 }
2523
2524 opj_bool tcd_encode_tile_v2(opj_tcd_v2_t *p_tcd,
2525                                                         OPJ_UINT32 p_tile_no,
2526                                                         OPJ_BYTE *p_dest,
2527                                                         OPJ_UINT32 * p_data_written,
2528                                                         OPJ_UINT32 p_max_length,
2529                                                         opj_codestream_info_t *p_cstr_info)
2530 {
2531
2532         if (p_tcd->cur_tp_num == 0) {
2533
2534                 p_tcd->tcd_tileno = p_tile_no;
2535                 p_tcd->tcp = &p_tcd->cp->tcps[p_tile_no];
2536
2537                 /* INDEX >> "Precinct_nb_X et Precinct_nb_Y" */
2538                 if(p_cstr_info)  {
2539                         OPJ_UINT32 l_num_packs = 0;
2540                         OPJ_UINT32 i;
2541                         opj_tcd_tilecomp_v2_t *l_tilec_idx = &p_tcd->tcd_image->tiles->comps[0];        /* based on component 0 */
2542                         opj_tccp_t *l_tccp = p_tcd->tcp->tccps; /* based on component 0 */
2543
2544                         for (i = 0; i < l_tilec_idx->numresolutions; i++) {
2545                                 opj_tcd_resolution_v2_t *l_res_idx = &l_tilec_idx->resolutions[i];
2546
2547                                 p_cstr_info->tile[p_tile_no].pw[i] = l_res_idx->pw;
2548                                 p_cstr_info->tile[p_tile_no].ph[i] = l_res_idx->ph;
2549
2550                                 l_num_packs += l_res_idx->pw * l_res_idx->ph;
2551                                 p_cstr_info->tile[p_tile_no].pdx[i] = l_tccp->prcw[i];
2552                                 p_cstr_info->tile[p_tile_no].pdy[i] = l_tccp->prch[i];
2553                         }
2554                         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));
2555                 }
2556                 /* << INDEX */
2557
2558                 /* FIXME _ProfStart(PGROUP_DC_SHIFT); */
2559                 /*---------------TILE-------------------*/
2560                 if (! tcd_dc_level_shift_encode(p_tcd)) {
2561                         return OPJ_FALSE;
2562                 }
2563                 /* FIXME _ProfStop(PGROUP_DC_SHIFT); */
2564
2565                 /* FIXME _ProfStart(PGROUP_MCT); */
2566                 if (! tcd_mct_encode(p_tcd)) {
2567                         return OPJ_FALSE;
2568                 }
2569                 /* FIXME _ProfStop(PGROUP_MCT); */
2570
2571                 /* FIXME _ProfStart(PGROUP_DWT); */
2572                 if (! tcd_dwt_encode(p_tcd)) {
2573                         return OPJ_FALSE;
2574                 }
2575                 /* FIXME  _ProfStop(PGROUP_DWT); */
2576
2577                 /* FIXME  _ProfStart(PGROUP_T1); */
2578                 if (! tcd_t1_encode(p_tcd)) {
2579                         return OPJ_FALSE;
2580                 }
2581                 /* FIXME _ProfStop(PGROUP_T1); */
2582
2583                 /* FIXME _ProfStart(PGROUP_RATE); */
2584                 if (! tcd_rate_allocate_encode(p_tcd,p_dest,p_max_length,p_cstr_info)) {
2585                         return OPJ_FALSE;
2586                 }
2587                 /* FIXME _ProfStop(PGROUP_RATE); */
2588
2589         }
2590         /*--------------TIER2------------------*/
2591
2592         /* INDEX */
2593         if (p_cstr_info) {
2594                 p_cstr_info->index_write = 1;
2595         }
2596         /* FIXME _ProfStart(PGROUP_T2); */
2597
2598         if (! tcd_t2_encode(p_tcd,p_dest,p_data_written,p_max_length,p_cstr_info)) {
2599                 return OPJ_FALSE;
2600         }
2601         /* FIXME _ProfStop(PGROUP_T2); */
2602
2603         /*---------------CLEAN-------------------*/
2604
2605         return OPJ_TRUE;
2606 }
2607
2608 opj_bool tcd_decode_tile_v2(
2609                                          opj_tcd_v2_t *p_tcd,
2610                                          OPJ_BYTE *p_src,
2611                                          OPJ_UINT32 p_max_length,
2612                                          OPJ_UINT32 p_tile_no,
2613                                          opj_codestream_index_t *p_cstr_index)
2614 {
2615         OPJ_UINT32 l_data_read;
2616         p_tcd->tcd_tileno = p_tile_no;
2617         p_tcd->tcp = &(p_tcd->cp->tcps[p_tile_no]);
2618
2619 #ifdef TODO_MSD /* FIXME */
2620         /* INDEX >>  */
2621         if(p_cstr_info) {
2622                 OPJ_UINT32 resno, compno, numprec = 0;
2623                 for (compno = 0; compno < (OPJ_UINT32) p_cstr_info->numcomps; compno++) {
2624                         opj_tcp_v2_t *tcp = &p_tcd->cp->tcps[0];
2625                         opj_tccp_t *tccp = &tcp->tccps[compno];
2626                         opj_tcd_tilecomp_v2_t *tilec_idx = &p_tcd->tcd_image->tiles->comps[compno];
2627                         for (resno = 0; resno < tilec_idx->numresolutions; resno++) {
2628                                 opj_tcd_resolution_v2_t *res_idx = &tilec_idx->resolutions[resno];
2629                                 p_cstr_info->tile[p_tile_no].pw[resno] = res_idx->pw;
2630                                 p_cstr_info->tile[p_tile_no].ph[resno] = res_idx->ph;
2631                                 numprec += res_idx->pw * res_idx->ph;
2632                                 p_cstr_info->tile[p_tile_no].pdx[resno] = tccp->prcw[resno];
2633                                 p_cstr_info->tile[p_tile_no].pdy[resno] = tccp->prch[resno];
2634                         }
2635                 }
2636                 p_cstr_info->tile[p_tile_no].packet = (opj_packet_info_t *) opj_malloc(p_cstr_info->numlayers * numprec * sizeof(opj_packet_info_t));
2637                 p_cstr_info->packno = 0;
2638         }
2639         /* << INDEX */
2640 #endif
2641
2642         /*--------------TIER2------------------*/
2643         /* FIXME _ProfStart(PGROUP_T2); */
2644         l_data_read = 0;
2645         if
2646                 (! tcd_t2_decode(p_tcd, p_src, &l_data_read, p_max_length, p_cstr_index))
2647         {
2648                 return OPJ_FALSE;
2649         }
2650         /* FIXME _ProfStop(PGROUP_T2); */
2651
2652         /*------------------TIER1-----------------*/
2653
2654         /* FIXME _ProfStart(PGROUP_T1); */
2655         if
2656                 (! tcd_t1_decode(p_tcd))
2657         {
2658                 return OPJ_FALSE;
2659         }
2660         /* FIXME _ProfStop(PGROUP_T1); */
2661
2662         /*----------------DWT---------------------*/
2663
2664         /* FIXME _ProfStart(PGROUP_DWT); */
2665         if
2666                 (! tcd_dwt_decode(p_tcd))
2667         {
2668                 return OPJ_FALSE;
2669         }
2670         /* FIXME _ProfStop(PGROUP_DWT); */
2671
2672         /*----------------MCT-------------------*/
2673         /* FIXME _ProfStart(PGROUP_MCT); */
2674         if
2675                 (! tcd_mct_decode(p_tcd))
2676         {
2677                 return OPJ_FALSE;
2678         }
2679         /* FIXME _ProfStop(PGROUP_MCT); */
2680
2681         /* FIXME _ProfStart(PGROUP_DC_SHIFT); */
2682         if
2683                 (! tcd_dc_level_shift_decode(p_tcd))
2684         {
2685                 return OPJ_FALSE;
2686         }
2687         /* FIXME _ProfStop(PGROUP_DC_SHIFT); */
2688
2689
2690         /*---------------TILE-------------------*/
2691         return OPJ_TRUE;
2692 }
2693
2694 opj_bool tcd_update_tile_data (
2695                                                  opj_tcd_v2_t *p_tcd,
2696                                                  OPJ_BYTE * p_dest,
2697                                                  OPJ_UINT32 p_dest_length
2698                                                  )
2699 {
2700         OPJ_UINT32 i,j,k,l_data_size = 0;
2701         opj_image_comp_t * l_img_comp = 00;
2702         opj_tcd_tilecomp_v2_t * l_tilec = 00;
2703         opj_tcd_resolution_v2_t * l_res;
2704         OPJ_UINT32 l_size_comp, l_remaining;
2705         OPJ_UINT32 l_stride, l_width,l_height;
2706
2707         l_data_size = tcd_get_decoded_tile_size(p_tcd);
2708         if (l_data_size > p_dest_length) {
2709                 return OPJ_FALSE;
2710         }
2711
2712         l_tilec = p_tcd->tcd_image->tiles->comps;
2713         l_img_comp = p_tcd->image->comps;
2714
2715         for (i=0;i<p_tcd->image->numcomps;++i) {
2716                 l_size_comp = l_img_comp->prec >> 3; /*(/ 8)*/
2717                 l_remaining = l_img_comp->prec & 7;  /* (%8) */
2718                 l_res = l_tilec->resolutions + l_img_comp->resno_decoded;
2719                 l_width = (l_res->x1 - l_res->x0);
2720                 l_height = (l_res->y1 - l_res->y0);
2721                 l_stride = (l_tilec->x1 - l_tilec->x0) - l_width;
2722
2723                 if (l_remaining) {
2724                         ++l_size_comp;
2725                 }
2726
2727                 if (l_size_comp == 3) {
2728                         l_size_comp = 4;
2729                 }
2730
2731                 switch (l_size_comp)
2732                         {
2733                         case 1:
2734                                 {
2735                                         OPJ_CHAR * l_dest_ptr = (OPJ_CHAR *) p_dest;
2736                                         const OPJ_INT32 * l_src_ptr = l_tilec->data;
2737
2738                                         if (l_img_comp->sgnd) {
2739                                                 for (j=0;j<l_height;++j) {
2740                                                         for (k=0;k<l_width;++k) {
2741                                                                 *(l_dest_ptr++) = (OPJ_CHAR) (*(l_src_ptr++));
2742                                                         }
2743                                                         l_src_ptr += l_stride;
2744                                                 }
2745                                         }
2746                                         else {
2747                                                 for (j=0;j<l_height;++j) {
2748                                                         for     (k=0;k<l_width;++k) {
2749                                                                 *(l_dest_ptr++) = (OPJ_BYTE) ((*(l_src_ptr++))&0xff);
2750                                                         }
2751                                                         l_src_ptr += l_stride;
2752                                                 }
2753                                         }
2754
2755                                         p_dest = (OPJ_BYTE *)l_dest_ptr;
2756                                 }
2757                                 break;
2758                         case 2:
2759                                 {
2760                                         const OPJ_INT32 * l_src_ptr = l_tilec->data;
2761                                         OPJ_INT16 * l_dest_ptr = (OPJ_INT16 *) p_dest;
2762
2763                                         if (l_img_comp->sgnd) {
2764                                                 for (j=0;j<l_height;++j) {
2765                                                         for (k=0;k<l_width;++k) {
2766                                                                 *(l_dest_ptr++) = (OPJ_INT16) (*(l_src_ptr++));
2767                                                         }
2768                                                         l_src_ptr += l_stride;
2769                                                 }
2770                                         }
2771                                         else {
2772                                                 for (j=0;j<l_height;++j) {
2773                                                         for (k=0;k<l_width;++k) {
2774                                                                 *(l_dest_ptr++) = (OPJ_UINT16) ((*(l_src_ptr++))&0xffff);
2775                                                         }
2776                                                         l_src_ptr += l_stride;
2777                                                 }
2778                                         }
2779
2780                                         p_dest = (OPJ_BYTE*) l_dest_ptr;
2781                                 }
2782                                 break;
2783                         case 4:
2784                                 {
2785                                         OPJ_INT32 * l_dest_ptr = (OPJ_INT32 *) p_dest;
2786                                         OPJ_INT32 * l_src_ptr = l_tilec->data;
2787
2788                                         for (j=0;j<l_height;++j) {
2789                                                 for (k=0;k<l_width;++k) {
2790                                                         *(l_dest_ptr++) = (*(l_src_ptr++));
2791                                                 }
2792                                                 l_src_ptr += l_stride;
2793                                         }
2794
2795                                         p_dest = (OPJ_BYTE*) l_dest_ptr;
2796                                 }
2797                                 break;
2798                 }
2799
2800                 ++l_img_comp;
2801                 ++l_tilec;
2802         }
2803
2804         return OPJ_TRUE;
2805 }
2806
2807
2808
2809
2810 void tcd_free_tile(opj_tcd_v2_t *p_tcd)
2811 {
2812         OPJ_UINT32 compno, resno, bandno, precno;
2813         opj_tcd_tile_v2_t *l_tile = 00;
2814         opj_tcd_tilecomp_v2_t *l_tile_comp = 00;
2815         opj_tcd_resolution_v2_t *l_res = 00;
2816         opj_tcd_band_v2_t *l_band = 00;
2817         opj_tcd_precinct_v2_t *l_precinct = 00;
2818         OPJ_UINT32 l_nb_resolutions, l_nb_precincts;
2819         void (* l_tcd_code_block_deallocate) (opj_tcd_precinct_v2_t *) = 00;
2820
2821         if (! p_tcd) {
2822                 return;
2823         }
2824
2825         if (! p_tcd->tcd_image) {
2826                 return;
2827         }
2828
2829         if (p_tcd->m_is_decoder) {
2830                 l_tcd_code_block_deallocate = tcd_code_block_dec_deallocate;
2831         }
2832         else {
2833                 l_tcd_code_block_deallocate = tcd_code_block_enc_deallocate;
2834         }
2835
2836         l_tile = p_tcd->tcd_image->tiles;
2837         if (! l_tile) {
2838                 return;
2839         }
2840
2841         l_tile_comp = l_tile->comps;
2842
2843         for (compno = 0; compno < l_tile->numcomps; ++compno) {
2844                 l_res = l_tile_comp->resolutions;
2845                 if (l_res) {
2846
2847                         l_nb_resolutions = l_tile_comp->resolutions_size / sizeof(opj_tcd_resolution_v2_t);
2848                         for (resno = 0; resno < l_nb_resolutions; ++resno) {
2849                                 l_band = l_res->bands;
2850                                 for     (bandno = 0; bandno < 3; ++bandno) {
2851                                         l_precinct = l_band->precincts;
2852                                         if (l_precinct) {
2853
2854                                                 l_nb_precincts = l_band->precincts_data_size / sizeof(opj_tcd_precinct_v2_t);
2855                                                 for (precno = 0; precno < l_nb_precincts; ++precno) {
2856                                                         tgt_destroy(l_precinct->incltree);
2857                                                         l_precinct->incltree = 00;
2858                                                         tgt_destroy(l_precinct->imsbtree);
2859                                                         l_precinct->imsbtree = 00;
2860                                                         (*l_tcd_code_block_deallocate) (l_precinct);
2861                                                         ++l_precinct;
2862                                                 }
2863
2864                                                 opj_free(l_band->precincts);
2865                                                 l_band->precincts = 00;
2866                                         }
2867                                         ++l_band;
2868                                 } /* for (resno */
2869                                 ++l_res;
2870                         }
2871
2872                         opj_free(l_tile_comp->resolutions);
2873                         l_tile_comp->resolutions = 00;
2874                 }
2875
2876                 if (l_tile_comp->data) {
2877                         opj_free(l_tile_comp->data);
2878                         l_tile_comp->data = 00;
2879                 }
2880                 ++l_tile_comp;
2881         }
2882
2883         opj_free(l_tile->comps);
2884         l_tile->comps = 00;
2885         opj_free(p_tcd->tcd_image->tiles);
2886         p_tcd->tcd_image->tiles = 00;
2887 }
2888
2889
2890 opj_bool tcd_t2_decode (
2891                                         opj_tcd_v2_t *p_tcd,
2892                                         OPJ_BYTE * p_src_data,
2893                                         OPJ_UINT32 * p_data_read,
2894                                         OPJ_UINT32 p_max_src_size,
2895                                         opj_codestream_index_t *p_cstr_index
2896                                         )
2897 {
2898         opj_t2_v2_t * l_t2;
2899
2900         l_t2 = t2_create_v2(p_tcd->image, p_tcd->cp);
2901         if (l_t2 == 00) {
2902                 return OPJ_FALSE;
2903         }
2904
2905         if (! t2_decode_packets_v2(
2906                                         l_t2,
2907                                         p_tcd->tcd_tileno,
2908                                         p_tcd->tcd_image->tiles,
2909                                         p_src_data,
2910                                         p_data_read,
2911                                         p_max_src_size,
2912                                         p_cstr_index)) {
2913                 t2_destroy_v2(l_t2);
2914                 return OPJ_FALSE;
2915         }
2916
2917         t2_destroy_v2(l_t2);
2918
2919         /*---------------CLEAN-------------------*/
2920         return OPJ_TRUE;
2921 }
2922
2923 opj_bool tcd_t1_decode ( opj_tcd_v2_t *p_tcd )
2924 {
2925         OPJ_UINT32 compno;
2926         opj_t1_t * l_t1;
2927         opj_tcd_tile_v2_t * l_tile = p_tcd->tcd_image->tiles;
2928         opj_tcd_tilecomp_v2_t* l_tile_comp = l_tile->comps;
2929         opj_tccp_t * l_tccp = p_tcd->tcp->tccps;
2930
2931
2932         l_t1 = t1_create_v2();
2933         if (l_t1 == 00) {
2934                 return OPJ_FALSE;
2935         }
2936
2937         for (compno = 0; compno < l_tile->numcomps; ++compno) {
2938                 /* The +3 is headroom required by the vectorized DWT */
2939                 t1_decode_cblks_v2(l_t1, l_tile_comp, l_tccp);
2940                 ++l_tile_comp;
2941                 ++l_tccp;
2942         }
2943
2944         t1_destroy_v2(l_t1);
2945
2946         return OPJ_TRUE;
2947 }
2948
2949
2950 opj_bool tcd_dwt_decode ( opj_tcd_v2_t *p_tcd )
2951 {
2952         OPJ_UINT32 compno;
2953         opj_tcd_tile_v2_t * l_tile = p_tcd->tcd_image->tiles;
2954         opj_tcd_tilecomp_v2_t * l_tile_comp = l_tile->comps;
2955         opj_tccp_t * l_tccp = p_tcd->tcp->tccps;
2956         opj_image_comp_t * l_img_comp = p_tcd->image->comps;
2957
2958         for (compno = 0; compno < l_tile->numcomps; compno++) {
2959                 /*
2960                 if (tcd->cp->reduce != 0) {
2961                         tcd->image->comps[compno].resno_decoded =
2962                                 tile->comps[compno].numresolutions - tcd->cp->reduce - 1;
2963                         if (tcd->image->comps[compno].resno_decoded < 0)
2964                         {
2965                                 return false;
2966                         }
2967                 }
2968                 numres2decode = tcd->image->comps[compno].resno_decoded + 1;
2969                 if(numres2decode > 0){
2970                 */
2971
2972                 if (l_tccp->qmfbid == 1) {
2973                         if (! dwt_decode_v2(l_tile_comp, l_img_comp->resno_decoded+1)) {
2974                                 return OPJ_FALSE;
2975                         }
2976                 }
2977                 else {
2978                         if (! dwt_decode_real_v2(l_tile_comp, l_img_comp->resno_decoded+1)) {
2979                                 return OPJ_FALSE;
2980                         }
2981                 }
2982
2983                 ++l_tile_comp;
2984                 ++l_img_comp;
2985                 ++l_tccp;
2986         }
2987
2988         return OPJ_TRUE;
2989 }
2990 opj_bool tcd_mct_decode ( opj_tcd_v2_t *p_tcd )
2991 {
2992         opj_tcd_tile_v2_t * l_tile = p_tcd->tcd_image->tiles;
2993         opj_tcp_v2_t * l_tcp = p_tcd->tcp;
2994         opj_tcd_tilecomp_v2_t * l_tile_comp = l_tile->comps;
2995         OPJ_UINT32 l_samples,i;
2996
2997         if (! l_tcp->mct) {
2998                 return OPJ_TRUE;
2999         }
3000
3001         l_samples = (l_tile_comp->x1 - l_tile_comp->x0) * (l_tile_comp->y1 - l_tile_comp->y0);
3002
3003         if (l_tile->numcomps >= 3 ){
3004                 if (l_tcp->mct == 2) {
3005                         OPJ_BYTE ** l_data;
3006
3007                         if (! l_tcp->m_mct_decoding_matrix) {
3008                                 return OPJ_TRUE;
3009                         }
3010
3011                         l_data = (OPJ_BYTE **) opj_malloc(l_tile->numcomps*sizeof(OPJ_BYTE*));
3012                         if (! l_data) {
3013                                 return OPJ_FALSE;
3014                         }
3015
3016                         for (i=0;i<l_tile->numcomps;++i) {
3017                                 l_data[i] = (OPJ_BYTE*) l_tile_comp->data;
3018                                 ++l_tile_comp;
3019                         }
3020
3021                         if (! mct_decode_custom(/* MCT data */
3022                                                                         (OPJ_BYTE*) l_tcp->m_mct_decoding_matrix,
3023                                                                         /* size of components */
3024                                                                         l_samples,
3025                                                                         /* components */
3026                                                                         l_data,
3027                                                                         /* nb of components (i.e. size of pData) */
3028                                                                         l_tile->numcomps,
3029                                                                         /* tells if the data is signed */
3030                                                                         p_tcd->image->comps->sgnd)) {
3031                                 opj_free(l_data);
3032                                 return OPJ_FALSE;
3033                         }
3034
3035                         opj_free(l_data);
3036                 }
3037                 else {
3038                         if (l_tcp->tccps->qmfbid == 1) {
3039                                 mct_decode(     l_tile->comps[0].data,
3040                                                         l_tile->comps[1].data,
3041                                                         l_tile->comps[2].data,
3042                                                         l_samples);
3043                         }
3044                         else {
3045                                 mct_decode_real(        (float*)l_tile->comps[0].data,
3046                                                                         (float*)l_tile->comps[1].data,
3047                                                                         (float*)l_tile->comps[2].data,
3048                                                                         l_samples);
3049                         }
3050                 }
3051         }
3052         else {
3053                 /* FIXME need to use opj_event_msg_v2 function */
3054                 fprintf(stderr,"Number of components (%d) is inconsistent with a MCT. Skip the MCT step.\n",l_tile->numcomps);
3055         }
3056
3057         return OPJ_TRUE;
3058 }
3059
3060
3061 opj_bool tcd_dc_level_shift_decode ( opj_tcd_v2_t *p_tcd )
3062 {
3063         OPJ_UINT32 compno;
3064         opj_tcd_tilecomp_v2_t * l_tile_comp = 00;
3065         opj_tccp_t * l_tccp = 00;
3066         opj_image_comp_t * l_img_comp = 00;
3067         opj_tcd_resolution_v2_t* l_res = 00;
3068         opj_tcp_v2_t * l_tcp = 00;
3069         opj_tcd_tile_v2_t * l_tile;
3070         OPJ_UINT32 l_width,l_height,i,j;
3071         OPJ_INT32 * l_current_ptr;
3072         OPJ_INT32 l_min, l_max;
3073         OPJ_UINT32 l_stride;
3074
3075         l_tile = p_tcd->tcd_image->tiles;
3076         l_tile_comp = l_tile->comps;
3077         l_tcp = p_tcd->tcp;
3078         l_tccp = p_tcd->tcp->tccps;
3079         l_img_comp = p_tcd->image->comps;
3080
3081         for (compno = 0; compno < l_tile->numcomps; compno++) {
3082                 l_res = l_tile_comp->resolutions + l_img_comp->resno_decoded;
3083                 l_width = (l_res->x1 - l_res->x0);
3084                 l_height = (l_res->y1 - l_res->y0);
3085                 l_stride = (l_tile_comp->x1 - l_tile_comp->x0) - l_width;
3086
3087                 if (l_img_comp->sgnd) {
3088                         l_min = -(1 << (l_img_comp->prec - 1));
3089                         l_max = (1 << (l_img_comp->prec - 1)) - 1;
3090                 }
3091                 else {
3092             l_min = 0;
3093                         l_max = (1 << l_img_comp->prec) - 1;
3094                 }
3095
3096                 l_current_ptr = l_tile_comp->data;
3097
3098                 if (l_tccp->qmfbid == 1) {
3099                         for (j=0;j<l_height;++j) {
3100                                 for (i = 0; i < l_width; ++i) {
3101                                         *l_current_ptr = int_clamp(*l_current_ptr + l_tccp->m_dc_level_shift, l_min, l_max);
3102                                         ++l_current_ptr;
3103                                 }
3104                                 l_current_ptr += l_stride;
3105                         }
3106                 }
3107                 else {
3108                         for (j=0;j<l_height;++j) {
3109                                 for (i = 0; i < l_width; ++i) {
3110                                         OPJ_FLOAT32 l_value = *((OPJ_FLOAT32 *) l_current_ptr);
3111                                         *l_current_ptr = int_clamp(lrintf(l_value) + l_tccp->m_dc_level_shift, l_min, l_max); ;
3112                                         ++l_current_ptr;
3113                                 }
3114                                 l_current_ptr += l_stride;
3115                         }
3116                 }
3117
3118                 ++l_img_comp;
3119                 ++l_tccp;
3120                 ++l_tile_comp;
3121         }
3122
3123         return OPJ_TRUE;
3124 }
3125
3126
3127
3128 /**
3129  * Deallocates the encoding data of the given precinct.
3130  */
3131 void tcd_code_block_dec_deallocate (opj_tcd_precinct_v2_t * p_precinct)
3132 {
3133         OPJ_UINT32 cblkno , l_nb_code_blocks;
3134
3135         opj_tcd_cblk_dec_v2_t * l_code_block = p_precinct->cblks.dec;
3136         if (l_code_block) {
3137                 /*fprintf(stderr,"deallocate codeblock:{\n");*/
3138                 /*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);*/
3139                 /*fprintf(stderr,"\t numbps=%d, numlenbits=%d, len=%d, numnewpasses=%d, real_num_segs=%d, m_current_max_segs=%d\n ",
3140                                 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 );*/
3141
3142
3143                 l_nb_code_blocks = p_precinct->block_size / sizeof(opj_tcd_cblk_dec_v2_t);
3144                 /*fprintf(stderr,"nb_code_blocks =%d\t}\n", l_nb_code_blocks);*/
3145
3146                 for (cblkno = 0; cblkno < l_nb_code_blocks; ++cblkno) {
3147
3148                         if (l_code_block->data) {
3149                                 opj_free(l_code_block->data);
3150                                 l_code_block->data = 00;
3151                         }
3152
3153                         if (l_code_block->segs) {
3154                                 opj_free(l_code_block->segs );
3155                                 l_code_block->segs = 00;
3156                         }
3157
3158                         ++l_code_block;
3159                 }
3160
3161                 opj_free(p_precinct->cblks.dec);
3162                 p_precinct->cblks.dec = 00;
3163         }
3164 }
3165
3166 /**
3167  * Deallocates the encoding data of the given precinct.
3168  */
3169 void tcd_code_block_enc_deallocate (opj_tcd_precinct_v2_t * p_precinct)
3170 {       
3171         OPJ_UINT32 cblkno , l_nb_code_blocks;
3172
3173         opj_tcd_cblk_enc_v2_t * l_code_block = p_precinct->cblks.enc;
3174         if (l_code_block) {
3175                 l_nb_code_blocks = p_precinct->block_size / sizeof(opj_tcd_cblk_enc_t);
3176                 
3177                 for     (cblkno = 0; cblkno < l_nb_code_blocks; ++cblkno)  {
3178                         if (l_code_block->data) {
3179                                 opj_free(l_code_block->data-1);
3180                                 l_code_block->data = 00;
3181                         }
3182
3183                         if (l_code_block->layers) {
3184                                 opj_free(l_code_block->layers );
3185                                 l_code_block->layers = 00;
3186                         }
3187
3188                         if (l_code_block->passes) {
3189                                 opj_free(l_code_block->passes );
3190                                 l_code_block->passes = 00;
3191                         }
3192                         ++l_code_block;
3193                 }
3194
3195                 opj_free(p_precinct->cblks.enc);
3196                 
3197                 p_precinct->cblks.enc = 00;
3198         }
3199 }
3200
3201 OPJ_UINT32 tcd_get_encoded_tile_size ( opj_tcd_v2_t *p_tcd )
3202 {
3203         OPJ_UINT32 i,l_data_size = 0;
3204         opj_image_comp_t * l_img_comp = 00;
3205         opj_tcd_tilecomp_v2_t * l_tilec = 00;
3206         OPJ_UINT32 l_size_comp, l_remaining;
3207
3208         l_tilec = p_tcd->tcd_image->tiles->comps;
3209         l_img_comp = p_tcd->image->comps;
3210         for (i=0;i<p_tcd->image->numcomps;++i) {
3211                 l_size_comp = l_img_comp->prec >> 3; /*(/ 8)*/
3212                 l_remaining = l_img_comp->prec & 7;  /* (%8) */
3213
3214                 if (l_remaining) {
3215                         ++l_size_comp;
3216                 }
3217
3218                 if (l_size_comp == 3) {
3219                         l_size_comp = 4;
3220                 }
3221
3222                 l_data_size += l_size_comp * (l_tilec->x1 - l_tilec->x0) * (l_tilec->y1 - l_tilec->y0);
3223                 ++l_img_comp;
3224                 ++l_tilec;
3225         }
3226
3227         return l_data_size;
3228 }
3229                 
3230 opj_bool tcd_dc_level_shift_encode ( opj_tcd_v2_t *p_tcd )
3231 {
3232         OPJ_UINT32 compno;
3233         opj_tcd_tilecomp_v2_t * l_tile_comp = 00;
3234         opj_tccp_t * l_tccp = 00;
3235         opj_image_comp_t * l_img_comp = 00;
3236         opj_tcp_v2_t * l_tcp = 00;
3237         opj_tcd_tile_v2_t * l_tile;
3238         OPJ_UINT32 l_nb_elem,i;
3239         OPJ_INT32 * l_current_ptr;
3240
3241         l_tile = p_tcd->tcd_image->tiles;
3242         l_tile_comp = l_tile->comps;
3243         l_tcp = p_tcd->tcp;
3244         l_tccp = p_tcd->tcp->tccps;
3245         l_img_comp = p_tcd->image->comps;
3246
3247         for (compno = 0; compno < l_tile->numcomps; compno++) {
3248                 l_current_ptr = l_tile_comp->data;
3249                 l_nb_elem = (l_tile_comp->x1 - l_tile_comp->x0) * (l_tile_comp->y1 - l_tile_comp->y0);
3250
3251                 if (l_tccp->qmfbid == 1) {
3252                         for     (i = 0; i < l_nb_elem; ++i) {
3253                                 *l_current_ptr -= l_tccp->m_dc_level_shift ;
3254                                 ++l_current_ptr;
3255                         }
3256                 }
3257                 else {
3258                         for (i = 0; i < l_nb_elem; ++i) {
3259                                 *l_current_ptr = (*l_current_ptr - l_tccp->m_dc_level_shift) << 11 ;
3260                                 ++l_current_ptr;
3261                         }
3262                 }
3263
3264                 ++l_img_comp;
3265                 ++l_tccp;
3266                 ++l_tile_comp;
3267         }
3268
3269         return OPJ_TRUE;
3270 }
3271
3272 opj_bool tcd_mct_encode ( opj_tcd_v2_t *p_tcd )
3273 {
3274         opj_tcd_tile_v2_t * l_tile = p_tcd->tcd_image->tiles;
3275         opj_tcd_tilecomp_v2_t * l_tile_comp = p_tcd->tcd_image->tiles->comps;
3276         OPJ_UINT32 samples = (l_tile_comp->x1 - l_tile_comp->x0) * (l_tile_comp->y1 - l_tile_comp->y0);
3277         OPJ_UINT32 i;
3278         OPJ_BYTE ** l_data = 00;
3279         opj_tcp_v2_t * l_tcp = p_tcd->tcp;
3280
3281         if(!p_tcd->tcp->mct) {
3282                 return OPJ_TRUE;
3283         }
3284
3285         if (p_tcd->tcp->mct == 2) {
3286                 if (! p_tcd->tcp->m_mct_coding_matrix) {
3287                         return OPJ_TRUE;
3288                 }
3289
3290         l_data = (OPJ_BYTE **) opj_malloc(l_tile->numcomps*sizeof(OPJ_BYTE*));
3291                 if (! l_data) {
3292                         return OPJ_FALSE;
3293                 }
3294
3295                 for (i=0;i<l_tile->numcomps;++i) {
3296                         l_data[i] = (OPJ_BYTE*) l_tile_comp->data;
3297                         ++l_tile_comp;
3298                 }
3299
3300                 if (! mct_encode_custom(/* MCT data */
3301                                         (OPJ_BYTE*) p_tcd->tcp->m_mct_coding_matrix,
3302                                         /* size of components */
3303                                         samples,
3304                                         /* components */
3305                                         l_data,
3306                                         /* nb of components (i.e. size of pData) */
3307                                         l_tile->numcomps,
3308                                         /* tells if the data is signed */
3309                                         p_tcd->image->comps->sgnd) )
3310                 {
3311             opj_free(l_data);
3312                         return OPJ_FALSE;
3313                 }
3314
3315                 opj_free(l_data);
3316         }
3317         else if (l_tcp->tccps->qmfbid == 0) {
3318                 mct_encode_real(l_tile->comps[0].data, l_tile->comps[1].data, l_tile->comps[2].data, samples);
3319         }
3320         else {
3321                 mct_encode(l_tile->comps[0].data, l_tile->comps[1].data, l_tile->comps[2].data, samples);
3322         }
3323
3324         return OPJ_TRUE;
3325 }
3326
3327 opj_bool tcd_dwt_encode ( opj_tcd_v2_t *p_tcd )
3328 {
3329         opj_tcd_tile_v2_t * l_tile = p_tcd->tcd_image->tiles;
3330         opj_tcd_tilecomp_v2_t * l_tile_comp = p_tcd->tcd_image->tiles->comps;
3331         opj_tccp_t * l_tccp = p_tcd->tcp->tccps;
3332         OPJ_UINT32 compno;
3333
3334         for (compno = 0; compno < l_tile->numcomps; ++compno) {
3335                 if (l_tccp->qmfbid == 1) {
3336                         if (! dwt_encode_v2(l_tile_comp)) {
3337                                 return OPJ_FALSE;
3338                         }
3339                 }
3340                 else if (l_tccp->qmfbid == 0) {
3341                         if (! dwt_encode_real_v2(l_tile_comp)) {
3342                                 return OPJ_FALSE;
3343                         }
3344                 }
3345
3346                 ++l_tile_comp;
3347                 ++l_tccp;
3348         }
3349
3350         return OPJ_TRUE;
3351 }
3352
3353 opj_bool tcd_t1_encode ( opj_tcd_v2_t *p_tcd )
3354 {
3355         opj_t1_t * l_t1;
3356         const OPJ_FLOAT64 * l_mct_norms;
3357         opj_tcp_v2_t * l_tcp = p_tcd->tcp;
3358
3359         l_t1 = t1_create_v2();
3360         if (l_t1 == 00) {
3361                 return OPJ_FALSE;
3362         }
3363
3364         if (l_tcp->mct == 1) {
3365                 /* irreversible encoding */
3366                 if (l_tcp->tccps->qmfbid == 0) {
3367                         l_mct_norms = get_mct_norms_real();
3368                 }
3369                 else {
3370                         l_mct_norms = get_mct_norms();
3371                 }
3372         }
3373         else {
3374                 l_mct_norms = (const OPJ_FLOAT64 *) (l_tcp->mct_norms);
3375         }
3376
3377         if (! t1_encode_cblks_v2(l_t1, p_tcd->tcd_image->tiles , l_tcp, l_mct_norms)) {
3378         t1_destroy_v2(l_t1);
3379                 return OPJ_FALSE;
3380         }
3381
3382         t1_destroy_v2(l_t1);
3383
3384         return OPJ_TRUE;
3385 }
3386
3387 opj_bool tcd_t2_encode (opj_tcd_v2_t *p_tcd,
3388                                                 OPJ_BYTE * p_dest_data,
3389                                                 OPJ_UINT32 * p_data_written,
3390                                                 OPJ_UINT32 p_max_dest_size,
3391                                                 opj_codestream_info_t *p_cstr_info )
3392 {
3393         opj_t2_v2_t * l_t2;
3394
3395         l_t2 = t2_create_v2(p_tcd->image, p_tcd->cp);
3396         if (l_t2 == 00) {
3397                 return OPJ_FALSE;
3398         }
3399
3400         if (! t2_encode_packets_v2(
3401                                         l_t2,
3402                                         p_tcd->tcd_tileno,
3403                                         p_tcd->tcd_image->tiles,
3404                                         p_tcd->tcp->numlayers,
3405                                         p_dest_data,
3406                                         p_data_written,
3407                                         p_max_dest_size,
3408                                         p_cstr_info,
3409                                         p_tcd->tp_num,
3410                                         p_tcd->tp_pos,
3411                                         p_tcd->cur_pino,
3412                                         FINAL_PASS))
3413         {
3414                 t2_destroy_v2(l_t2);
3415                 return OPJ_FALSE;
3416         }
3417
3418         t2_destroy_v2(l_t2);
3419
3420         /*---------------CLEAN-------------------*/
3421         return OPJ_TRUE;
3422 }
3423
3424
3425 opj_bool tcd_rate_allocate_encode(      opj_tcd_v2_t *p_tcd,
3426                                                                         OPJ_BYTE * p_dest_data,
3427                                                                         OPJ_UINT32 p_max_dest_size,
3428                                                                         opj_codestream_info_t *p_cstr_info )
3429 {
3430         opj_cp_v2_t * l_cp = p_tcd->cp;
3431         OPJ_UINT32 l_nb_written = 0;
3432
3433         if (p_cstr_info)  {
3434                 p_cstr_info->index_write = 0;
3435         }
3436
3437         if (l_cp->m_specific_param.m_enc.m_disto_alloc|| l_cp->m_specific_param.m_enc.m_fixed_quality)  {
3438                 /* fixed_quality */
3439                 /* Normal Rate/distortion allocation */
3440                 if (! tcd_rateallocate_v2(p_tcd, p_dest_data,&l_nb_written, p_max_dest_size, p_cstr_info)) {
3441                         return OPJ_FALSE;
3442                 }
3443         }
3444         else {
3445                 /* Fixed layer allocation */
3446                 tcd_rateallocate_fixed_v2(p_tcd);
3447         }
3448
3449         return OPJ_TRUE;
3450 }
3451
3452
3453 opj_bool tcd_copy_tile_data (   opj_tcd_v2_t *p_tcd,
3454                                                                 OPJ_BYTE * p_src,
3455                                                                 OPJ_UINT32 p_src_length )
3456 {
3457         OPJ_UINT32 i,j,l_data_size = 0;
3458         opj_image_comp_t * l_img_comp = 00;
3459         opj_tcd_tilecomp_v2_t * l_tilec = 00;
3460         OPJ_UINT32 l_size_comp, l_remaining;
3461         OPJ_UINT32 l_nb_elem;
3462
3463         l_data_size = tcd_get_encoded_tile_size(p_tcd);
3464         if (l_data_size != p_src_length) {
3465                 return OPJ_FALSE;
3466         }
3467
3468         l_tilec = p_tcd->tcd_image->tiles->comps;
3469         l_img_comp = p_tcd->image->comps;
3470         for (i=0;i<p_tcd->image->numcomps;++i) {
3471                 l_size_comp = l_img_comp->prec >> 3; /*(/ 8)*/
3472                 l_remaining = l_img_comp->prec & 7;  /* (%8) */
3473                 l_nb_elem = (l_tilec->x1 - l_tilec->x0) * (l_tilec->y1 - l_tilec->y0);
3474
3475                 if (l_remaining) {
3476                         ++l_size_comp;
3477                 }
3478
3479                 if (l_size_comp == 3) {
3480                         l_size_comp = 4;
3481                 }
3482
3483                 switch (l_size_comp) {
3484                         case 1:
3485                                 {
3486                                         OPJ_CHAR * l_src_ptr = (OPJ_CHAR *) p_src;
3487                                         OPJ_INT32 * l_dest_ptr = l_tilec->data;
3488
3489                                         if (l_img_comp->sgnd) {
3490                                                 for (j=0;j<l_nb_elem;++j) {
3491                                                         *(l_dest_ptr++) = (OPJ_INT32) (*(l_src_ptr++));
3492                                                 }
3493                                         }
3494                                         else {
3495                                                 for (j=0;j<l_nb_elem;++j) {
3496                                                         *(l_dest_ptr++) = (*(l_src_ptr++))&0xff;
3497                                                 }
3498                                         }
3499
3500                                         p_src = (OPJ_BYTE*) l_src_ptr;
3501                                 }
3502                                 break;
3503                         case 2:
3504                                 {
3505                                         OPJ_INT32 * l_dest_ptr = l_tilec->data;
3506                                         OPJ_INT16 * l_src_ptr = (OPJ_INT16 *) p_src;
3507
3508                                         if (l_img_comp->sgnd) {
3509                                                 for (j=0;j<l_nb_elem;++j) {
3510                                                         *(l_dest_ptr++) = (OPJ_INT32) (*(l_src_ptr++));
3511                                                 }
3512                                         }
3513                                         else {
3514                                                 for (j=0;j<l_nb_elem;++j) {
3515                                                         *(l_dest_ptr++) = (*(l_src_ptr++))&0xffff;
3516                                                 }
3517                                         }
3518
3519                                         p_src = (OPJ_BYTE*) l_src_ptr;
3520                                 }
3521                                 break;
3522                         case 4:
3523                                 {
3524                                         OPJ_INT32 * l_src_ptr = (OPJ_INT32 *) p_src;
3525                                         OPJ_INT32 * l_dest_ptr = l_tilec->data;
3526
3527                                         for (j=0;j<l_nb_elem;++j) {
3528                                                 *(l_dest_ptr++) = (OPJ_INT32) (*(l_src_ptr++));
3529                                         }
3530
3531                                         p_src = (OPJ_BYTE*) l_src_ptr;
3532                                 }
3533                                 break;
3534                 }
3535
3536                 ++l_img_comp;
3537                 ++l_tilec;
3538         }
3539
3540         return OPJ_TRUE;
3541 }