[trunk] merge r1323 from branch 1.5 over to trunk
[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 void tcd_dump(FILE *fd, opj_tcd_t *tcd, opj_tcd_image_t * img) {
36         int tileno, compno, resno, bandno, precno;/*, cblkno;*/
37
38         fprintf(fd, "image {\n");
39         fprintf(fd, "  tw=%d, th=%d x0=%d x1=%d y0=%d y1=%d\n", 
40                 img->tw, img->th, tcd->image->x0, tcd->image->x1, tcd->image->y0, tcd->image->y1);
41
42         for (tileno = 0; tileno < img->th * img->tw; tileno++) {
43                 opj_tcd_tile_t *tile = &tcd->tcd_image->tiles[tileno];
44                 fprintf(fd, "  tile {\n");
45                 fprintf(fd, "    x0=%d, y0=%d, x1=%d, y1=%d, numcomps=%d\n",
46                         tile->x0, tile->y0, tile->x1, tile->y1, tile->numcomps);
47                 for (compno = 0; compno < tile->numcomps; compno++) {
48                         opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
49                         fprintf(fd, "    tilec {\n");
50                         fprintf(fd,
51                                 "      x0=%d, y0=%d, x1=%d, y1=%d, numresolutions=%d\n",
52                                 tilec->x0, tilec->y0, tilec->x1, tilec->y1, tilec->numresolutions);
53                         for (resno = 0; resno < tilec->numresolutions; resno++) {
54                                 opj_tcd_resolution_t *res = &tilec->resolutions[resno];
55                                 fprintf(fd, "\n   res {\n");
56                                 fprintf(fd,
57                                         "          x0=%d, y0=%d, x1=%d, y1=%d, pw=%d, ph=%d, numbands=%d\n",
58                                         res->x0, res->y0, res->x1, res->y1, res->pw, res->ph, res->numbands);
59                                 for (bandno = 0; bandno < res->numbands; bandno++) {
60                                         opj_tcd_band_t *band = &res->bands[bandno];
61                                         fprintf(fd, "        band {\n");
62                                         fprintf(fd,
63                                                 "          x0=%d, y0=%d, x1=%d, y1=%d, stepsize=%f, numbps=%d\n",
64                                                 band->x0, band->y0, band->x1, band->y1, band->stepsize, band->numbps);
65                                         for (precno = 0; precno < res->pw * res->ph; precno++) {
66                                                 opj_tcd_precinct_t *prec = &band->precincts[precno];
67                                                 fprintf(fd, "          prec {\n");
68                                                 fprintf(fd,
69                                                         "            x0=%d, y0=%d, x1=%d, y1=%d, cw=%d, ch=%d\n",
70                                                         prec->x0, prec->y0, prec->x1, prec->y1, prec->cw, prec->ch);
71                                                 /*
72                                                 for (cblkno = 0; cblkno < prec->cw * prec->ch; cblkno++) {
73                                                         opj_tcd_cblk_t *cblk = &prec->cblks[cblkno];
74                                                         fprintf(fd, "            cblk {\n");
75                                                         fprintf(fd,
76                                                                 "              x0=%d, y0=%d, x1=%d, y1=%d\n",
77                                                                 cblk->x0, cblk->y0, cblk->x1, cblk->y1);
78                                                         fprintf(fd, "            }\n");
79                                                 }
80                                                 */
81                                                 fprintf(fd, "          }\n");
82                                         }
83                                         fprintf(fd, "        }\n");
84                                 }
85                                 fprintf(fd, "      }\n");
86                         }
87                         fprintf(fd, "    }\n");
88                 }
89                 fprintf(fd, "  }\n");
90         }
91         fprintf(fd, "}\n");
92 }
93 /**
94 * Allocates memory for a decoding code block.
95 */
96 static opj_bool tcd_code_block_dec_allocate (opj_tcd_cblk_dec_v2_t * p_code_block);
97
98 /**
99 Free the memory allocated for encoding
100 @param tcd TCD handle
101 */
102 static void tcd_free_tile(opj_tcd_v2_t *tcd);
103
104
105 opj_bool tcd_t2_decode (
106                                         opj_tcd_v2_t *p_tcd,
107                                         OPJ_BYTE * p_src_data,
108                                         OPJ_UINT32 * p_data_read,
109                                         OPJ_UINT32 p_max_src_size,
110                                         opj_codestream_index_t *p_cstr_index
111                                         );
112
113 opj_bool tcd_t1_decode (
114                                           opj_tcd_v2_t *p_tcd
115                                          );
116
117 opj_bool tcd_dwt_decode (
118                                           opj_tcd_v2_t *p_tcd
119                                          );
120
121 opj_bool tcd_mct_decode (
122                                           opj_tcd_v2_t *p_tcd
123                                          );
124
125 opj_bool tcd_dc_level_shift_decode (
126                                                  opj_tcd_v2_t *p_tcd
127                                                  );
128
129 void tcd_code_block_dec_deallocate (opj_tcd_precinct_v2_t * p_precinct);
130 /* ----------------------------------------------------------------------- */
131
132 /**
133 Create a new TCD handle
134 */
135 opj_tcd_t* tcd_create(opj_common_ptr cinfo) {
136         /* create the tcd structure */
137         opj_tcd_t *tcd = (opj_tcd_t*)opj_malloc(sizeof(opj_tcd_t));
138         if(!tcd) return NULL;
139         tcd->cinfo = cinfo;
140         tcd->tcd_image = (opj_tcd_image_t*)opj_malloc(sizeof(opj_tcd_image_t));
141         if(!tcd->tcd_image) {
142                 opj_free(tcd);
143                 return NULL;
144         }
145
146         return tcd;
147 }
148
149 /**
150 Create a new TCD handle
151 */
152 opj_tcd_v2_t* tcd_create_v2(opj_bool p_is_decoder)
153 {
154         opj_tcd_v2_t *l_tcd = 00;
155
156         /* create the tcd structure */
157         l_tcd = (opj_tcd_v2_t*) opj_malloc(sizeof(opj_tcd_v2_t));
158         if (!l_tcd) {
159                 return 00;
160         }
161         memset(l_tcd,0,sizeof(opj_tcd_v2_t));
162
163         l_tcd->m_is_decoder = p_is_decoder ? 1 : 0;
164
165         l_tcd->tcd_image = (opj_tcd_image_v2_t*)opj_malloc(sizeof(opj_tcd_image_v2_t));
166         if (!l_tcd->tcd_image) {
167                 opj_free(l_tcd);
168                 return 00;
169         }
170         memset(l_tcd->tcd_image,0,sizeof(opj_tcd_image_v2_t));
171
172         return l_tcd;
173 }
174
175 /**
176 Destroy a previously created TCD handle
177 */
178 void tcd_destroy(opj_tcd_t *tcd) {
179         if(tcd) {
180                 opj_free(tcd->tcd_image);
181                 opj_free(tcd);
182         }
183 }
184
185 /* ----------------------------------------------------------------------- */
186
187 void tcd_malloc_encode(opj_tcd_t *tcd, opj_image_t * image, opj_cp_t * cp, int curtileno) {
188         int tileno, compno, resno, bandno, precno, cblkno;
189
190         tcd->image = image;
191         tcd->cp = cp;
192         tcd->tcd_image->tw = cp->tw;
193         tcd->tcd_image->th = cp->th;
194         tcd->tcd_image->tiles = (opj_tcd_tile_t *) opj_malloc(sizeof(opj_tcd_tile_t));
195         
196         for (tileno = 0; tileno < 1; tileno++) {
197                 opj_tcp_t *tcp = &cp->tcps[curtileno];
198                 int j;
199
200                 /* cfr p59 ISO/IEC FDIS15444-1 : 2000 (18 august 2000) */
201                 int p = curtileno % cp->tw;     /* si numerotation matricielle .. */
202                 int q = curtileno / cp->tw;     /* .. coordonnees de la tile (q,p) q pour ligne et p pour colonne */
203
204                 /* opj_tcd_tile_t *tile=&tcd->tcd_image->tiles[tileno]; */
205                 opj_tcd_tile_t *tile = tcd->tcd_image->tiles;
206
207                 /* 4 borders of the tile rescale on the image if necessary */
208                 tile->x0 = int_max(cp->tx0 + p * cp->tdx, image->x0);
209                 tile->y0 = int_max(cp->ty0 + q * cp->tdy, image->y0);
210                 tile->x1 = int_min(cp->tx0 + (p + 1) * cp->tdx, image->x1);
211                 tile->y1 = int_min(cp->ty0 + (q + 1) * cp->tdy, image->y1);
212                 tile->numcomps = image->numcomps;
213                 /* tile->PPT=image->PPT;  */
214
215                 /* Modification of the RATE >> */
216                 for (j = 0; j < tcp->numlayers; j++) {
217                         tcp->rates[j] = tcp->rates[j] ? 
218                                 cp->tp_on ? 
219                                         (((float) (tile->numcomps 
220                                         * (tile->x1 - tile->x0) 
221                                         * (tile->y1 - tile->y0)
222                                         * image->comps[0].prec))
223                                         /(tcp->rates[j] * 8 * image->comps[0].dx * image->comps[0].dy)) - (((tcd->cur_totnum_tp - 1) * 14 )/ tcp->numlayers)
224                                         :
225                                 ((float) (tile->numcomps 
226                                         * (tile->x1 - tile->x0) 
227                                         * (tile->y1 - tile->y0) 
228                                         * image->comps[0].prec))/ 
229                                         (tcp->rates[j] * 8 * image->comps[0].dx * image->comps[0].dy)
230                                         : 0;
231
232                         if (tcp->rates[j]) {
233                                 if (j && tcp->rates[j] < tcp->rates[j - 1] + 10) {
234                                         tcp->rates[j] = tcp->rates[j - 1] + 20;
235                                 } else {
236                                         if (!j && tcp->rates[j] < 30)
237                                                 tcp->rates[j] = 30;
238                                 }
239                                 
240                                 if(j == (tcp->numlayers-1)){
241                                         tcp->rates[j] = tcp->rates[j]- 2;
242                                 }
243                         }
244                 }
245                 /* << Modification of the RATE */
246                 
247                 tile->comps = (opj_tcd_tilecomp_t *) opj_malloc(image->numcomps * sizeof(opj_tcd_tilecomp_t));
248                 for (compno = 0; compno < tile->numcomps; compno++) {
249                         opj_tccp_t *tccp = &tcp->tccps[compno];
250
251                         opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
252
253                         /* border of each tile component (global) */
254                         tilec->x0 = int_ceildiv(tile->x0, image->comps[compno].dx);
255                         tilec->y0 = int_ceildiv(tile->y0, image->comps[compno].dy);
256                         tilec->x1 = int_ceildiv(tile->x1, image->comps[compno].dx);
257                         tilec->y1 = int_ceildiv(tile->y1, image->comps[compno].dy);
258                         
259                         tilec->data = (int *) opj_aligned_malloc((tilec->x1 - tilec->x0) * (tilec->y1 - tilec->y0) * sizeof(int));
260                         tilec->numresolutions = tccp->numresolutions;
261
262                         tilec->resolutions = (opj_tcd_resolution_t *) opj_malloc(tilec->numresolutions * sizeof(opj_tcd_resolution_t));
263                         
264                         for (resno = 0; resno < tilec->numresolutions; resno++) {
265                                 int pdx, pdy;
266                                 int levelno = tilec->numresolutions - 1 - resno;
267                                 int tlprcxstart, tlprcystart, brprcxend, brprcyend;
268                                 int tlcbgxstart, tlcbgystart, brcbgxend, brcbgyend;
269                                 int cbgwidthexpn, cbgheightexpn;
270                                 int cblkwidthexpn, cblkheightexpn;
271
272                                 opj_tcd_resolution_t *res = &tilec->resolutions[resno];
273                                 
274                                 /* border for each resolution level (global) */
275                                 res->x0 = int_ceildivpow2(tilec->x0, levelno);
276                                 res->y0 = int_ceildivpow2(tilec->y0, levelno);
277                                 res->x1 = int_ceildivpow2(tilec->x1, levelno);
278                                 res->y1 = int_ceildivpow2(tilec->y1, levelno);
279                                 
280                                 res->numbands = resno == 0 ? 1 : 3;
281                                 /* p. 35, table A-23, ISO/IEC FDIS154444-1 : 2000 (18 august 2000) */
282                                 if (tccp->csty & J2K_CCP_CSTY_PRT) {
283                                         pdx = tccp->prcw[resno];
284                                         pdy = tccp->prch[resno];
285                                 } else {
286                                         pdx = 15;
287                                         pdy = 15;
288                                 }
289                                 /* p. 64, B.6, ISO/IEC FDIS15444-1 : 2000 (18 august 2000)  */
290                                 tlprcxstart = int_floordivpow2(res->x0, pdx) << pdx;
291                                 tlprcystart = int_floordivpow2(res->y0, pdy) << pdy;
292                                 
293                                 brprcxend = int_ceildivpow2(res->x1, pdx) << pdx;
294                                 brprcyend = int_ceildivpow2(res->y1, pdy) << pdy;
295                                 
296                                 res->pw = (brprcxend - tlprcxstart) >> pdx;
297                                 res->ph = (brprcyend - tlprcystart) >> pdy;
298                                 
299                                 if (resno == 0) {
300                                         tlcbgxstart = tlprcxstart;
301                                         tlcbgystart = tlprcystart;
302                                         brcbgxend = brprcxend;
303                                         brcbgyend = brprcyend;
304                                         cbgwidthexpn = pdx;
305                                         cbgheightexpn = pdy;
306                                 } else {
307                                         tlcbgxstart = int_ceildivpow2(tlprcxstart, 1);
308                                         tlcbgystart = int_ceildivpow2(tlprcystart, 1);
309                                         brcbgxend = int_ceildivpow2(brprcxend, 1);
310                                         brcbgyend = int_ceildivpow2(brprcyend, 1);
311                                         cbgwidthexpn = pdx - 1;
312                                         cbgheightexpn = pdy - 1;
313                                 }
314                                 
315                                 cblkwidthexpn = int_min(tccp->cblkw, cbgwidthexpn);
316                                 cblkheightexpn = int_min(tccp->cblkh, cbgheightexpn);
317                                 
318                                 for (bandno = 0; bandno < res->numbands; bandno++) {
319                                         int x0b, y0b, i;
320                                         int gain, numbps;
321                                         opj_stepsize_t *ss = NULL;
322
323                                         opj_tcd_band_t *band = &res->bands[bandno];
324
325                                         band->bandno = resno == 0 ? 0 : bandno + 1;
326                                         x0b = (band->bandno == 1) || (band->bandno == 3) ? 1 : 0;
327                                         y0b = (band->bandno == 2) || (band->bandno == 3) ? 1 : 0;
328                                         
329                                         if (band->bandno == 0) {
330                                                 /* band border (global) */
331                                                 band->x0 = int_ceildivpow2(tilec->x0, levelno);
332                                                 band->y0 = int_ceildivpow2(tilec->y0, levelno);
333                                                 band->x1 = int_ceildivpow2(tilec->x1, levelno);
334                                                 band->y1 = int_ceildivpow2(tilec->y1, levelno);
335                                         } else {
336                                                 /* band border (global) */
337                                                 band->x0 = int_ceildivpow2(tilec->x0 - (1 << levelno) * x0b, levelno + 1);
338                                                 band->y0 = int_ceildivpow2(tilec->y0 - (1 << levelno) * y0b, levelno + 1);
339                                                 band->x1 = int_ceildivpow2(tilec->x1 - (1 << levelno) * x0b, levelno + 1);
340                                                 band->y1 = int_ceildivpow2(tilec->y1 - (1 << levelno) * y0b, levelno + 1);
341                                         }
342                                         
343                                         ss = &tccp->stepsizes[resno == 0 ? 0 : 3 * (resno - 1) + bandno + 1];
344                                         gain = tccp->qmfbid == 0 ? dwt_getgain_real(band->bandno) : dwt_getgain(band->bandno);                                  
345                                         numbps = image->comps[compno].prec + gain;
346                                         
347                                         band->stepsize = (float)((1.0 + ss->mant / 2048.0) * pow(2.0, numbps - ss->expn));
348                                         band->numbps = ss->expn + tccp->numgbits - 1;   /* WHY -1 ? */
349                                         
350                                         band->precincts = (opj_tcd_precinct_t *) opj_malloc(3 * res->pw * res->ph * sizeof(opj_tcd_precinct_t));
351                                         
352                                         for (i = 0; i < res->pw * res->ph * 3; i++) {
353                                                 band->precincts[i].imsbtree = NULL;
354                                                 band->precincts[i].incltree = NULL;
355                                                 band->precincts[i].cblks.enc = NULL;
356                                         }
357                                         
358                                         for (precno = 0; precno < res->pw * res->ph; precno++) {
359                                                 int tlcblkxstart, tlcblkystart, brcblkxend, brcblkyend;
360
361                                                 int cbgxstart = tlcbgxstart + (precno % res->pw) * (1 << cbgwidthexpn);
362                                                 int cbgystart = tlcbgystart + (precno / res->pw) * (1 << cbgheightexpn);
363                                                 int cbgxend = cbgxstart + (1 << cbgwidthexpn);
364                                                 int cbgyend = cbgystart + (1 << cbgheightexpn);
365
366                                                 opj_tcd_precinct_t *prc = &band->precincts[precno];
367
368                                                 /* precinct size (global) */
369                                                 prc->x0 = int_max(cbgxstart, band->x0);
370                                                 prc->y0 = int_max(cbgystart, band->y0);
371                                                 prc->x1 = int_min(cbgxend, band->x1);
372                                                 prc->y1 = int_min(cbgyend, band->y1);
373
374                                                 tlcblkxstart = int_floordivpow2(prc->x0, cblkwidthexpn) << cblkwidthexpn;
375                                                 tlcblkystart = int_floordivpow2(prc->y0, cblkheightexpn) << cblkheightexpn;
376                                                 brcblkxend = int_ceildivpow2(prc->x1, cblkwidthexpn) << cblkwidthexpn;
377                                                 brcblkyend = int_ceildivpow2(prc->y1, cblkheightexpn) << cblkheightexpn;
378                                                 prc->cw = (brcblkxend - tlcblkxstart) >> cblkwidthexpn;
379                                                 prc->ch = (brcblkyend - tlcblkystart) >> cblkheightexpn;
380
381                                                 prc->cblks.enc = (opj_tcd_cblk_enc_t*) opj_calloc((prc->cw * prc->ch), sizeof(opj_tcd_cblk_enc_t));
382                                                 prc->incltree = tgt_create(prc->cw, prc->ch);
383                                                 prc->imsbtree = tgt_create(prc->cw, prc->ch);
384                                                 
385                                                 for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
386                                                         int cblkxstart = tlcblkxstart + (cblkno % prc->cw) * (1 << cblkwidthexpn);
387                                                         int cblkystart = tlcblkystart + (cblkno / prc->cw) * (1 << cblkheightexpn);
388                                                         int cblkxend = cblkxstart + (1 << cblkwidthexpn);
389                                                         int cblkyend = cblkystart + (1 << cblkheightexpn);
390                                                         
391                                                         opj_tcd_cblk_enc_t* cblk = &prc->cblks.enc[cblkno];
392
393                                                         /* code-block size (global) */
394                                                         cblk->x0 = int_max(cblkxstart, prc->x0);
395                                                         cblk->y0 = int_max(cblkystart, prc->y0);
396                                                         cblk->x1 = int_min(cblkxend, prc->x1);
397                                                         cblk->y1 = int_min(cblkyend, prc->y1);
398                                                         cblk->data = (unsigned char*) opj_calloc(8192+2, sizeof(unsigned char));
399                                                         /* FIXME: mqc_init_enc and mqc_byteout underrun the buffer if we don't do this. Why? */
400                                                         cblk->data += 2;
401                                                         cblk->layers = (opj_tcd_layer_t*) opj_calloc(100, sizeof(opj_tcd_layer_t));
402                                                         cblk->passes = (opj_tcd_pass_t*) opj_calloc(100, sizeof(opj_tcd_pass_t));
403                                                 }
404                                         }
405                                 }
406                         }
407                 }
408         }
409         
410         /* tcd_dump(stdout, tcd, &tcd->tcd_image); */
411 }
412
413 void tcd_free_encode(opj_tcd_t *tcd) {
414         int tileno, compno, resno, bandno, precno, cblkno;
415
416         for (tileno = 0; tileno < 1; tileno++) {
417                 opj_tcd_tile_t *tile = tcd->tcd_image->tiles;
418
419                 for (compno = 0; compno < tile->numcomps; compno++) {
420                         opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
421
422                         for (resno = 0; resno < tilec->numresolutions; resno++) {
423                                 opj_tcd_resolution_t *res = &tilec->resolutions[resno];
424
425                                 for (bandno = 0; bandno < res->numbands; bandno++) {
426                                         opj_tcd_band_t *band = &res->bands[bandno];
427
428                                         for (precno = 0; precno < res->pw * res->ph; precno++) {
429                                                 opj_tcd_precinct_t *prc = &band->precincts[precno];
430
431                                                 if (prc->incltree != NULL) {
432                                                         tgt_destroy(prc->incltree);
433                                                         prc->incltree = NULL;
434                                                 }
435                                                 if (prc->imsbtree != NULL) {
436                                                         tgt_destroy(prc->imsbtree);     
437                                                         prc->imsbtree = NULL;
438                                                 }
439                                                 for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
440                                                         opj_free(prc->cblks.enc[cblkno].data - 2);
441                                                         opj_free(prc->cblks.enc[cblkno].layers);
442                                                         opj_free(prc->cblks.enc[cblkno].passes);
443                                                 }
444                                                 opj_free(prc->cblks.enc);
445                                         } /* for (precno */
446                                         opj_free(band->precincts);
447                                         band->precincts = NULL;
448                                 } /* for (bandno */
449                         } /* for (resno */
450                         opj_free(tilec->resolutions);
451                         tilec->resolutions = NULL;
452                 } /* for (compno */
453                 opj_free(tile->comps);
454                 tile->comps = NULL;
455         } /* for (tileno */
456         opj_free(tcd->tcd_image->tiles);
457         tcd->tcd_image->tiles = NULL;
458 }
459
460 void tcd_init_encode(opj_tcd_t *tcd, opj_image_t * image, opj_cp_t * cp, int curtileno) {
461         int tileno, compno, resno, bandno, precno, cblkno;
462
463         for (tileno = 0; tileno < 1; tileno++) {
464                 opj_tcp_t *tcp = &cp->tcps[curtileno];
465                 int j;
466                 /* cfr p59 ISO/IEC FDIS15444-1 : 2000 (18 august 2000) */
467                 int p = curtileno % cp->tw;
468                 int q = curtileno / cp->tw;
469
470                 opj_tcd_tile_t *tile = tcd->tcd_image->tiles;
471                 
472                 /* 4 borders of the tile rescale on the image if necessary */
473                 tile->x0 = int_max(cp->tx0 + p * cp->tdx, image->x0);
474                 tile->y0 = int_max(cp->ty0 + q * cp->tdy, image->y0);
475                 tile->x1 = int_min(cp->tx0 + (p + 1) * cp->tdx, image->x1);
476                 tile->y1 = int_min(cp->ty0 + (q + 1) * cp->tdy, image->y1);
477                 
478                 tile->numcomps = image->numcomps;
479                 /* tile->PPT=image->PPT; */
480
481                 /* Modification of the RATE >> */
482                 for (j = 0; j < tcp->numlayers; j++) {
483                         tcp->rates[j] = tcp->rates[j] ? 
484                                 cp->tp_on ? 
485                                         (((float) (tile->numcomps 
486                                         * (tile->x1 - tile->x0) 
487                                         * (tile->y1 - tile->y0)
488                                         * image->comps[0].prec))
489                                         /(tcp->rates[j] * 8 * image->comps[0].dx * image->comps[0].dy)) - (((tcd->cur_totnum_tp - 1) * 14 )/ tcp->numlayers)
490                                         :
491                                 ((float) (tile->numcomps 
492                                         * (tile->x1 - tile->x0) 
493                                         * (tile->y1 - tile->y0) 
494                                         * image->comps[0].prec))/ 
495                                         (tcp->rates[j] * 8 * image->comps[0].dx * image->comps[0].dy)
496                                         : 0;
497
498                         if (tcp->rates[j]) {
499                                 if (j && tcp->rates[j] < tcp->rates[j - 1] + 10) {
500                                         tcp->rates[j] = tcp->rates[j - 1] + 20;
501                                 } else {
502                                         if (!j && tcp->rates[j] < 30)
503                                                 tcp->rates[j] = 30;
504                                 }
505                         }
506                 }
507                 /* << Modification of the RATE */
508
509                 /* tile->comps=(opj_tcd_tilecomp_t*)opj_realloc(tile->comps,image->numcomps*sizeof(opj_tcd_tilecomp_t)); */
510                 for (compno = 0; compno < tile->numcomps; compno++) {
511                         opj_tccp_t *tccp = &tcp->tccps[compno];
512                         
513                         opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
514
515                         /* border of each tile component (global) */
516                         tilec->x0 = int_ceildiv(tile->x0, image->comps[compno].dx);
517                         tilec->y0 = int_ceildiv(tile->y0, image->comps[compno].dy);
518                         tilec->x1 = int_ceildiv(tile->x1, image->comps[compno].dx);
519                         tilec->y1 = int_ceildiv(tile->y1, image->comps[compno].dy);
520                         
521                         tilec->data = (int *) opj_aligned_malloc((tilec->x1 - tilec->x0) * (tilec->y1 - tilec->y0) * sizeof(int));
522                         tilec->numresolutions = tccp->numresolutions;
523                         /* tilec->resolutions=(opj_tcd_resolution_t*)opj_realloc(tilec->resolutions,tilec->numresolutions*sizeof(opj_tcd_resolution_t)); */
524                         for (resno = 0; resno < tilec->numresolutions; resno++) {
525                                 int pdx, pdy;
526
527                                 int levelno = tilec->numresolutions - 1 - resno;
528                                 int tlprcxstart, tlprcystart, brprcxend, brprcyend;
529                                 int tlcbgxstart, tlcbgystart, brcbgxend, brcbgyend;
530                                 int cbgwidthexpn, cbgheightexpn;
531                                 int cblkwidthexpn, cblkheightexpn;
532                                 
533                                 opj_tcd_resolution_t *res = &tilec->resolutions[resno];
534
535                                 /* border for each resolution level (global) */
536                                 res->x0 = int_ceildivpow2(tilec->x0, levelno);
537                                 res->y0 = int_ceildivpow2(tilec->y0, levelno);
538                                 res->x1 = int_ceildivpow2(tilec->x1, levelno);
539                                 res->y1 = int_ceildivpow2(tilec->y1, levelno);  
540                                 res->numbands = resno == 0 ? 1 : 3;
541
542                                 /* p. 35, table A-23, ISO/IEC FDIS154444-1 : 2000 (18 august 2000) */
543                                 if (tccp->csty & J2K_CCP_CSTY_PRT) {
544                                         pdx = tccp->prcw[resno];
545                                         pdy = tccp->prch[resno];
546                                 } else {
547                                         pdx = 15;
548                                         pdy = 15;
549                                 }
550                                 /* p. 64, B.6, ISO/IEC FDIS15444-1 : 2000 (18 august 2000)  */
551                                 tlprcxstart = int_floordivpow2(res->x0, pdx) << pdx;
552                                 tlprcystart = int_floordivpow2(res->y0, pdy) << pdy;
553                                 brprcxend = int_ceildivpow2(res->x1, pdx) << pdx;
554                                 brprcyend = int_ceildivpow2(res->y1, pdy) << pdy;
555                                 
556                                 res->pw = (brprcxend - tlprcxstart) >> pdx;
557                                 res->ph = (brprcyend - tlprcystart) >> pdy;
558                                 
559                                 if (resno == 0) {
560                                         tlcbgxstart = tlprcxstart;
561                                         tlcbgystart = tlprcystart;
562                                         brcbgxend = brprcxend;
563                                         brcbgyend = brprcyend;
564                                         cbgwidthexpn = pdx;
565                                         cbgheightexpn = pdy;
566                                 } else {
567                                         tlcbgxstart = int_ceildivpow2(tlprcxstart, 1);
568                                         tlcbgystart = int_ceildivpow2(tlprcystart, 1);
569                                         brcbgxend = int_ceildivpow2(brprcxend, 1);
570                                         brcbgyend = int_ceildivpow2(brprcyend, 1);
571                                         cbgwidthexpn = pdx - 1;
572                                         cbgheightexpn = pdy - 1;
573                                 }
574                                 
575                                 cblkwidthexpn = int_min(tccp->cblkw, cbgwidthexpn);
576                                 cblkheightexpn = int_min(tccp->cblkh, cbgheightexpn);
577                                 
578                                 for (bandno = 0; bandno < res->numbands; bandno++) {
579                                         int x0b, y0b;
580                                         int gain, numbps;
581                                         opj_stepsize_t *ss = NULL;
582
583                                         opj_tcd_band_t *band = &res->bands[bandno];
584
585                                         band->bandno = resno == 0 ? 0 : bandno + 1;
586                                         x0b = (band->bandno == 1) || (band->bandno == 3) ? 1 : 0;
587                                         y0b = (band->bandno == 2) || (band->bandno == 3) ? 1 : 0;
588                                         
589                                         if (band->bandno == 0) {
590                                                 /* band border */
591                                                 band->x0 = int_ceildivpow2(tilec->x0, levelno);
592                                                 band->y0 = int_ceildivpow2(tilec->y0, levelno);
593                                                 band->x1 = int_ceildivpow2(tilec->x1, levelno);
594                                                 band->y1 = int_ceildivpow2(tilec->y1, levelno);
595                                         } else {
596                                                 band->x0 = int_ceildivpow2(tilec->x0 - (1 << levelno) * x0b, levelno + 1);
597                                                 band->y0 = int_ceildivpow2(tilec->y0 - (1 << levelno) * y0b, levelno + 1);
598                                                 band->x1 = int_ceildivpow2(tilec->x1 - (1 << levelno) * x0b, levelno + 1);
599                                                 band->y1 = int_ceildivpow2(tilec->y1 - (1 << levelno) * y0b, levelno + 1);
600                                         }
601                                         
602                                         ss = &tccp->stepsizes[resno == 0 ? 0 : 3 * (resno - 1) + bandno + 1];
603                                         gain = tccp->qmfbid == 0 ? dwt_getgain_real(band->bandno) : dwt_getgain(band->bandno);
604                                         numbps = image->comps[compno].prec + gain;
605                                         band->stepsize = (float)((1.0 + ss->mant / 2048.0) * pow(2.0, numbps - ss->expn));
606                                         band->numbps = ss->expn + tccp->numgbits - 1;   /* WHY -1 ? */
607                                         
608                                         for (precno = 0; precno < res->pw * res->ph; precno++) {
609                                                 int tlcblkxstart, tlcblkystart, brcblkxend, brcblkyend;
610
611                                                 int cbgxstart = tlcbgxstart + (precno % res->pw) * (1 << cbgwidthexpn);
612                                                 int cbgystart = tlcbgystart + (precno / res->pw) * (1 << cbgheightexpn);
613                                                 int cbgxend = cbgxstart + (1 << cbgwidthexpn);
614                                                 int cbgyend = cbgystart + (1 << cbgheightexpn);
615                                                 
616                                                 opj_tcd_precinct_t *prc = &band->precincts[precno];
617
618                                                 /* precinct size (global) */
619                                                 prc->x0 = int_max(cbgxstart, band->x0);
620                                                 prc->y0 = int_max(cbgystart, band->y0);
621                                                 prc->x1 = int_min(cbgxend, band->x1);
622                                                 prc->y1 = int_min(cbgyend, band->y1);
623
624                                                 tlcblkxstart = int_floordivpow2(prc->x0, cblkwidthexpn) << cblkwidthexpn;
625                                                 tlcblkystart = int_floordivpow2(prc->y0, cblkheightexpn) << cblkheightexpn;
626                                                 brcblkxend = int_ceildivpow2(prc->x1, cblkwidthexpn) << cblkwidthexpn;
627                                                 brcblkyend = int_ceildivpow2(prc->y1, cblkheightexpn) << cblkheightexpn;
628                                                 prc->cw = (brcblkxend - tlcblkxstart) >> cblkwidthexpn;
629                                                 prc->ch = (brcblkyend - tlcblkystart) >> cblkheightexpn;
630
631                                                 opj_free(prc->cblks.enc);
632                                                 prc->cblks.enc = (opj_tcd_cblk_enc_t*) opj_calloc(prc->cw * prc->ch, sizeof(opj_tcd_cblk_enc_t));
633
634                                                 if (prc->incltree != NULL) {
635                                                         tgt_destroy(prc->incltree);
636                                                 }
637                                                 if (prc->imsbtree != NULL) {
638                                                         tgt_destroy(prc->imsbtree);
639                                                 }
640                                                 
641                                                 prc->incltree = tgt_create(prc->cw, prc->ch);
642                                                 prc->imsbtree = tgt_create(prc->cw, prc->ch);
643
644                                                 for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
645                                                         int cblkxstart = tlcblkxstart + (cblkno % prc->cw) * (1 << cblkwidthexpn);
646                                                         int cblkystart = tlcblkystart + (cblkno / prc->cw) * (1 << cblkheightexpn);
647                                                         int cblkxend = cblkxstart + (1 << cblkwidthexpn);
648                                                         int cblkyend = cblkystart + (1 << cblkheightexpn);
649
650                                                         opj_tcd_cblk_enc_t* cblk = &prc->cblks.enc[cblkno];
651
652                                                         /* code-block size (global) */
653                                                         cblk->x0 = int_max(cblkxstart, prc->x0);
654                                                         cblk->y0 = int_max(cblkystart, prc->y0);
655                                                         cblk->x1 = int_min(cblkxend, prc->x1);
656                                                         cblk->y1 = int_min(cblkyend, prc->y1);
657                                                         cblk->data = (unsigned char*) opj_calloc(8192+2, sizeof(unsigned char));
658                                                         /* FIXME: mqc_init_enc and mqc_byteout underrun the buffer if we don't do this. Why? */
659                                                         cblk->data += 2;
660                                                         cblk->layers = (opj_tcd_layer_t*) opj_calloc(100, sizeof(opj_tcd_layer_t));
661                                                         cblk->passes = (opj_tcd_pass_t*) opj_calloc(100, sizeof(opj_tcd_pass_t));
662                                                 }
663                                         } /* precno */
664                                 } /* bandno */
665                         } /* resno */
666                 } /* compno */
667         } /* tileno */
668
669         /* tcd_dump(stdout, tcd, &tcd->tcd_image); */
670 }
671
672 void tcd_malloc_decode(opj_tcd_t *tcd, opj_image_t * image, opj_cp_t * cp) {
673         int i, j, tileno, p, q;
674         unsigned int x0 = 0, y0 = 0, x1 = 0, y1 = 0, w, h;
675
676         tcd->image = image;
677         tcd->tcd_image->tw = cp->tw;
678         tcd->tcd_image->th = cp->th;
679         tcd->tcd_image->tiles = (opj_tcd_tile_t *) opj_malloc(cp->tw * cp->th * sizeof(opj_tcd_tile_t));
680
681         /* 
682         Allocate place to store the decoded data = final image
683         Place limited by the tile really present in the codestream 
684         */
685
686         for (j = 0; j < cp->tileno_size; j++) {
687                 opj_tcd_tile_t *tile;
688                 
689                 tileno = cp->tileno[j];         
690                 tile = &(tcd->tcd_image->tiles[cp->tileno[tileno]]);            
691                 tile->numcomps = image->numcomps;
692                 tile->comps = (opj_tcd_tilecomp_t*) opj_calloc(image->numcomps, sizeof(opj_tcd_tilecomp_t));
693         }
694
695         for (i = 0; i < image->numcomps; i++) {
696                 for (j = 0; j < cp->tileno_size; j++) {
697                         opj_tcd_tile_t *tile;
698                         opj_tcd_tilecomp_t *tilec;
699                         
700                         /* cfr p59 ISO/IEC FDIS15444-1 : 2000 (18 august 2000) */
701                         
702                         tileno = cp->tileno[j];
703                         
704                         tile = &(tcd->tcd_image->tiles[cp->tileno[tileno]]);
705                         tilec = &tile->comps[i];
706                         
707                         p = tileno % cp->tw;    /* si numerotation matricielle .. */
708                         q = tileno / cp->tw;    /* .. coordonnees de la tile (q,p) q pour ligne et p pour colonne */
709                         
710                         /* 4 borders of the tile rescale on the image if necessary */
711                         tile->x0 = int_max(cp->tx0 + p * cp->tdx, image->x0);
712                         tile->y0 = int_max(cp->ty0 + q * cp->tdy, image->y0);
713                         tile->x1 = int_min(cp->tx0 + (p + 1) * cp->tdx, image->x1);
714                         tile->y1 = int_min(cp->ty0 + (q + 1) * cp->tdy, image->y1);
715
716                         tilec->x0 = int_ceildiv(tile->x0, image->comps[i].dx);
717                         tilec->y0 = int_ceildiv(tile->y0, image->comps[i].dy);
718                         tilec->x1 = int_ceildiv(tile->x1, image->comps[i].dx);
719                         tilec->y1 = int_ceildiv(tile->y1, image->comps[i].dy);
720
721                         x0 = j == 0 ? tilec->x0 : int_min(x0, (unsigned int) tilec->x0);
722                         y0 = j == 0 ? tilec->y0 : int_min(y0,   (unsigned int) tilec->y0);
723                         x1 = j == 0 ? tilec->x1 : int_max(x1,   (unsigned int) tilec->x1);
724                         y1 = j == 0 ? tilec->y1 : int_max(y1,   (unsigned int) tilec->y1);
725                 }
726
727                 w = int_ceildivpow2(x1 - x0, image->comps[i].factor);
728                 h = int_ceildivpow2(y1 - y0, image->comps[i].factor);
729
730                 image->comps[i].w = w;
731                 image->comps[i].h = h;
732                 image->comps[i].x0 = x0;
733                 image->comps[i].y0 = y0;
734         }
735 }
736
737 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) {
738         int compno, resno, bandno, precno, cblkno;
739         opj_tcp_t *tcp;
740         opj_tcd_tile_t *tile;
741
742         OPJ_ARG_NOT_USED(cstr_info);
743
744         tcd->cp = cp;
745         
746         tcp = &(cp->tcps[cp->tileno[tileno]]);
747         tile = &(tcd->tcd_image->tiles[cp->tileno[tileno]]);
748         
749         tileno = cp->tileno[tileno];
750         
751         for (compno = 0; compno < tile->numcomps; compno++) {
752                 opj_tccp_t *tccp = &tcp->tccps[compno];
753                 opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
754                 
755                 /* border of each tile component (global) */
756                 tilec->x0 = int_ceildiv(tile->x0, image->comps[compno].dx);
757                 tilec->y0 = int_ceildiv(tile->y0, image->comps[compno].dy);
758                 tilec->x1 = int_ceildiv(tile->x1, image->comps[compno].dx);
759                 tilec->y1 = int_ceildiv(tile->y1, image->comps[compno].dy);
760
761                 tilec->numresolutions = tccp->numresolutions;
762                 tilec->resolutions = (opj_tcd_resolution_t *) opj_malloc(tilec->numresolutions * sizeof(opj_tcd_resolution_t));
763                 
764                 for (resno = 0; resno < tilec->numresolutions; resno++) {
765                         int pdx, pdy;
766                         int levelno = tilec->numresolutions - 1 - resno;
767                         int tlprcxstart, tlprcystart, brprcxend, brprcyend;
768                         int tlcbgxstart, tlcbgystart, brcbgxend, brcbgyend;
769                         int cbgwidthexpn, cbgheightexpn;
770                         int cblkwidthexpn, cblkheightexpn;
771                         
772                         opj_tcd_resolution_t *res = &tilec->resolutions[resno];
773                         
774                         /* border for each resolution level (global) */
775                         res->x0 = int_ceildivpow2(tilec->x0, levelno);
776                         res->y0 = int_ceildivpow2(tilec->y0, levelno);
777                         res->x1 = int_ceildivpow2(tilec->x1, levelno);
778                         res->y1 = int_ceildivpow2(tilec->y1, levelno);
779                         res->numbands = resno == 0 ? 1 : 3;
780                         
781                         /* p. 35, table A-23, ISO/IEC FDIS154444-1 : 2000 (18 august 2000) */
782                         if (tccp->csty & J2K_CCP_CSTY_PRT) {
783                                 pdx = tccp->prcw[resno];
784                                 pdy = tccp->prch[resno];
785                         } else {
786                                 pdx = 15;
787                                 pdy = 15;
788                         }                       
789                         
790                         /* p. 64, B.6, ISO/IEC FDIS15444-1 : 2000 (18 august 2000)  */
791                         tlprcxstart = int_floordivpow2(res->x0, pdx) << pdx;
792                         tlprcystart = int_floordivpow2(res->y0, pdy) << pdy;
793                         brprcxend = int_ceildivpow2(res->x1, pdx) << pdx;
794                         brprcyend = int_ceildivpow2(res->y1, pdy) << pdy;
795                         
796                         res->pw = (res->x0 == res->x1) ? 0 : ((brprcxend - tlprcxstart) >> pdx);
797                         res->ph = (res->y0 == res->y1) ? 0 : ((brprcyend - tlprcystart) >> pdy);
798                         
799                         if (resno == 0) {
800                                 tlcbgxstart = tlprcxstart;
801                                 tlcbgystart = tlprcystart;
802                                 brcbgxend = brprcxend;
803                                 brcbgyend = brprcyend;
804                                 cbgwidthexpn = pdx;
805                                 cbgheightexpn = pdy;
806                         } else {
807                                 tlcbgxstart = int_ceildivpow2(tlprcxstart, 1);
808                                 tlcbgystart = int_ceildivpow2(tlprcystart, 1);
809                                 brcbgxend = int_ceildivpow2(brprcxend, 1);
810                                 brcbgyend = int_ceildivpow2(brprcyend, 1);
811                                 cbgwidthexpn = pdx - 1;
812                                 cbgheightexpn = pdy - 1;
813                         }
814                         
815                         cblkwidthexpn = int_min(tccp->cblkw, cbgwidthexpn);
816                         cblkheightexpn = int_min(tccp->cblkh, cbgheightexpn);
817                         
818                         for (bandno = 0; bandno < res->numbands; bandno++) {
819                                 int x0b, y0b;
820                                 int gain, numbps;
821                                 opj_stepsize_t *ss = NULL;
822                                 
823                                 opj_tcd_band_t *band = &res->bands[bandno];
824                                 band->bandno = resno == 0 ? 0 : bandno + 1;
825                                 x0b = (band->bandno == 1) || (band->bandno == 3) ? 1 : 0;
826                                 y0b = (band->bandno == 2) || (band->bandno == 3) ? 1 : 0;
827                                 
828                                 if (band->bandno == 0) {
829                                         /* band border (global) */
830                                         band->x0 = int_ceildivpow2(tilec->x0, levelno);
831                                         band->y0 = int_ceildivpow2(tilec->y0, levelno);
832                                         band->x1 = int_ceildivpow2(tilec->x1, levelno);
833                                         band->y1 = int_ceildivpow2(tilec->y1, levelno);
834                                 } else {
835                                         /* band border (global) */
836                                         band->x0 = int_ceildivpow2(tilec->x0 - (1 << levelno) * x0b, levelno + 1);
837                                         band->y0 = int_ceildivpow2(tilec->y0 - (1 << levelno) * y0b, levelno + 1);
838                                         band->x1 = int_ceildivpow2(tilec->x1 - (1 << levelno) * x0b, levelno + 1);
839                                         band->y1 = int_ceildivpow2(tilec->y1 - (1 << levelno) * y0b, levelno + 1);
840                                 }
841                                 
842                                 ss = &tccp->stepsizes[resno == 0 ? 0 : 3 * (resno - 1) + bandno + 1];
843                                 gain = tccp->qmfbid == 0 ? dwt_getgain_real(band->bandno) : dwt_getgain(band->bandno);
844                                 numbps = image->comps[compno].prec + gain;
845                                 band->stepsize = (float)(((1.0 + ss->mant / 2048.0) * pow(2.0, numbps - ss->expn)) * 0.5);
846                                 band->numbps = ss->expn + tccp->numgbits - 1;   /* WHY -1 ? */
847                                 
848                                 band->precincts = (opj_tcd_precinct_t *) opj_malloc(res->pw * res->ph * sizeof(opj_tcd_precinct_t));
849                                 
850                                 for (precno = 0; precno < res->pw * res->ph; precno++) {
851                                         int tlcblkxstart, tlcblkystart, brcblkxend, brcblkyend;
852                                         int cbgxstart = tlcbgxstart + (precno % res->pw) * (1 << cbgwidthexpn);
853                                         int cbgystart = tlcbgystart + (precno / res->pw) * (1 << cbgheightexpn);
854                                         int cbgxend = cbgxstart + (1 << cbgwidthexpn);
855                                         int cbgyend = cbgystart + (1 << cbgheightexpn);
856                                         
857                                         opj_tcd_precinct_t *prc = &band->precincts[precno];
858                                         /* precinct size (global) */
859                                         prc->x0 = int_max(cbgxstart, band->x0);
860                                         prc->y0 = int_max(cbgystart, band->y0);
861                                         prc->x1 = int_min(cbgxend, band->x1);
862                                         prc->y1 = int_min(cbgyend, band->y1);
863                                         
864                                         tlcblkxstart = int_floordivpow2(prc->x0, cblkwidthexpn) << cblkwidthexpn;
865                                         tlcblkystart = int_floordivpow2(prc->y0, cblkheightexpn) << cblkheightexpn;
866                                         brcblkxend = int_ceildivpow2(prc->x1, cblkwidthexpn) << cblkwidthexpn;
867                                         brcblkyend = int_ceildivpow2(prc->y1, cblkheightexpn) << cblkheightexpn;
868                                         prc->cw = (brcblkxend - tlcblkxstart) >> cblkwidthexpn;
869                                         prc->ch = (brcblkyend - tlcblkystart) >> cblkheightexpn;
870
871                                         prc->cblks.dec = (opj_tcd_cblk_dec_t*) opj_malloc(prc->cw * prc->ch * sizeof(opj_tcd_cblk_dec_t));
872
873                                         prc->incltree = tgt_create(prc->cw, prc->ch);
874                                         prc->imsbtree = tgt_create(prc->cw, prc->ch);
875                                         
876                                         for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
877                                                 int cblkxstart = tlcblkxstart + (cblkno % prc->cw) * (1 << cblkwidthexpn);
878                                                 int cblkystart = tlcblkystart + (cblkno / prc->cw) * (1 << cblkheightexpn);
879                                                 int cblkxend = cblkxstart + (1 << cblkwidthexpn);
880                                                 int cblkyend = cblkystart + (1 << cblkheightexpn);                                      
881
882                                                 opj_tcd_cblk_dec_t* cblk = &prc->cblks.dec[cblkno];
883                                                 cblk->data = NULL;
884                                                 cblk->segs = NULL;
885                                                 /* code-block size (global) */
886                                                 cblk->x0 = int_max(cblkxstart, prc->x0);
887                                                 cblk->y0 = int_max(cblkystart, prc->y0);
888                                                 cblk->x1 = int_min(cblkxend, prc->x1);
889                                                 cblk->y1 = int_min(cblkyend, prc->y1);
890                                                 cblk->numsegs = 0;
891                                         }
892                                 } /* precno */
893                         } /* bandno */
894                 } /* resno */
895         } /* compno */
896         /* tcd_dump(stdout, tcd, &tcd->tcd_image); */
897 }
898
899 void tcd_makelayer_fixed(opj_tcd_t *tcd, int layno, int final) {
900         int compno, resno, bandno, precno, cblkno;
901         int value;                      /*, matrice[tcd_tcp->numlayers][tcd_tile->comps[0].numresolutions][3]; */
902         int matrice[10][10][3];
903         int i, j, k;
904
905         opj_cp_t *cp = tcd->cp;
906         opj_tcd_tile_t *tcd_tile = tcd->tcd_tile;
907         opj_tcp_t *tcd_tcp = tcd->tcp;
908
909         /*matrice=(int*)opj_malloc(tcd_tcp->numlayers*tcd_tile->comps[0].numresolutions*3*sizeof(int)); */
910         
911         for (compno = 0; compno < tcd_tile->numcomps; compno++) {
912                 opj_tcd_tilecomp_t *tilec = &tcd_tile->comps[compno];
913                 for (i = 0; i < tcd_tcp->numlayers; i++) {
914                         for (j = 0; j < tilec->numresolutions; j++) {
915                                 for (k = 0; k < 3; k++) {
916                                         matrice[i][j][k] =
917                                                 (int) (cp->matrice[i * tilec->numresolutions * 3 + j * 3 + k] 
918                                                 * (float) (tcd->image->comps[compno].prec / 16.0));
919                                 }
920                         }
921                 }
922         
923                 for (resno = 0; resno < tilec->numresolutions; resno++) {
924                         opj_tcd_resolution_t *res = &tilec->resolutions[resno];
925                         for (bandno = 0; bandno < res->numbands; bandno++) {
926                                 opj_tcd_band_t *band = &res->bands[bandno];
927                                 for (precno = 0; precno < res->pw * res->ph; precno++) {
928                                         opj_tcd_precinct_t *prc = &band->precincts[precno];
929                                         for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
930                                                 opj_tcd_cblk_enc_t *cblk = &prc->cblks.enc[cblkno];
931                                                 opj_tcd_layer_t *layer = &cblk->layers[layno];
932                                                 int n;
933                                                 int imsb = tcd->image->comps[compno].prec - cblk->numbps;       /* number of bit-plan equal to zero */
934                                                 /* Correction of the matrix of coefficient to include the IMSB information */
935                                                 if (layno == 0) {
936                                                         value = matrice[layno][resno][bandno];
937                                                         if (imsb >= value) {
938                                                                 value = 0;
939                                                         } else {
940                                                                 value -= imsb;
941                                                         }
942                                                 } else {
943                                                         value = matrice[layno][resno][bandno] - matrice[layno - 1][resno][bandno];
944                                                         if (imsb >= matrice[layno - 1][resno][bandno]) {
945                                                                 value -= (imsb - matrice[layno - 1][resno][bandno]);
946                                                                 if (value < 0) {
947                                                                         value = 0;
948                                                                 }
949                                                         }
950                                                 }
951                                                 
952                                                 if (layno == 0) {
953                                                         cblk->numpassesinlayers = 0;
954                                                 }
955                                                 
956                                                 n = cblk->numpassesinlayers;
957                                                 if (cblk->numpassesinlayers == 0) {
958                                                         if (value != 0) {
959                                                                 n = 3 * value - 2 + cblk->numpassesinlayers;
960                                                         } else {
961                                                                 n = cblk->numpassesinlayers;
962                                                         }
963                                                 } else {
964                                                         n = 3 * value + cblk->numpassesinlayers;
965                                                 }
966                                                 
967                                                 layer->numpasses = n - cblk->numpassesinlayers;
968                                                 
969                                                 if (!layer->numpasses)
970                                                         continue;
971                                                 
972                                                 if (cblk->numpassesinlayers == 0) {
973                                                         layer->len = cblk->passes[n - 1].rate;
974                                                         layer->data = cblk->data;
975                                                 } else {
976                                                         layer->len = cblk->passes[n - 1].rate - cblk->passes[cblk->numpassesinlayers - 1].rate;
977                                                         layer->data = cblk->data + cblk->passes[cblk->numpassesinlayers - 1].rate;
978                                                 }
979                                                 if (final)
980                                                         cblk->numpassesinlayers = n;
981                                         }
982                                 }
983                         }
984                 }
985         }
986 }
987
988 void tcd_rateallocate_fixed(opj_tcd_t *tcd) {
989         int layno;
990         for (layno = 0; layno < tcd->tcp->numlayers; layno++) {
991                 tcd_makelayer_fixed(tcd, layno, 1);
992         }
993 }
994
995 void tcd_makelayer(opj_tcd_t *tcd, int layno, double thresh, int final) {
996         int compno, resno, bandno, precno, cblkno, passno;
997         
998         opj_tcd_tile_t *tcd_tile = tcd->tcd_tile;
999
1000         tcd_tile->distolayer[layno] = 0;        /* fixed_quality */
1001         
1002         for (compno = 0; compno < tcd_tile->numcomps; compno++) {
1003                 opj_tcd_tilecomp_t *tilec = &tcd_tile->comps[compno];
1004                 for (resno = 0; resno < tilec->numresolutions; resno++) {
1005                         opj_tcd_resolution_t *res = &tilec->resolutions[resno];
1006                         for (bandno = 0; bandno < res->numbands; bandno++) {
1007                                 opj_tcd_band_t *band = &res->bands[bandno];
1008                                 for (precno = 0; precno < res->pw * res->ph; precno++) {
1009                                         opj_tcd_precinct_t *prc = &band->precincts[precno];
1010                                         for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
1011                                                 opj_tcd_cblk_enc_t *cblk = &prc->cblks.enc[cblkno];
1012                                                 opj_tcd_layer_t *layer = &cblk->layers[layno];
1013                                                 
1014                                                 int n;
1015                                                 if (layno == 0) {
1016                                                         cblk->numpassesinlayers = 0;
1017                                                 }
1018                                                 n = cblk->numpassesinlayers;
1019                                                 for (passno = cblk->numpassesinlayers; passno < cblk->totalpasses; passno++) {
1020                                                         int dr;
1021                                                         double dd;
1022                                                         opj_tcd_pass_t *pass = &cblk->passes[passno];
1023                                                         if (n == 0) {
1024                                                                 dr = pass->rate;
1025                                                                 dd = pass->distortiondec;
1026                                                         } else {
1027                                                                 dr = pass->rate - cblk->passes[n - 1].rate;
1028                                                                 dd = pass->distortiondec - cblk->passes[n - 1].distortiondec;
1029                                                         }
1030                                                         if (!dr) {
1031                                                                 if (dd != 0)
1032                                                                         n = passno + 1;
1033                                                                 continue;
1034                                                         }
1035                                                         if (dd / dr >= thresh)
1036                                                                 n = passno + 1;
1037                                                 }
1038                                                 layer->numpasses = n - cblk->numpassesinlayers;
1039                                                 
1040                                                 if (!layer->numpasses) {
1041                                                         layer->disto = 0;
1042                                                         continue;
1043                                                 }
1044                                                 if (cblk->numpassesinlayers == 0) {
1045                                                         layer->len = cblk->passes[n - 1].rate;
1046                                                         layer->data = cblk->data;
1047                                                         layer->disto = cblk->passes[n - 1].distortiondec;
1048                                                 } else {
1049                                                         layer->len = cblk->passes[n - 1].rate - cblk->passes[cblk->numpassesinlayers - 1].rate;
1050                                                         layer->data = cblk->data + cblk->passes[cblk->numpassesinlayers - 1].rate;
1051                                                         layer->disto = cblk->passes[n - 1].distortiondec - cblk->passes[cblk->numpassesinlayers - 1].distortiondec;
1052                                                 }
1053                                                 
1054                                                 tcd_tile->distolayer[layno] += layer->disto;    /* fixed_quality */
1055                                                 
1056                                                 if (final)
1057                                                         cblk->numpassesinlayers = n;
1058                                         }
1059                                 }
1060                         }
1061                 }
1062         }
1063 }
1064
1065 opj_bool tcd_rateallocate(opj_tcd_t *tcd, unsigned char *dest, int len, opj_codestream_info_t *cstr_info) {
1066         int compno, resno, bandno, precno, cblkno, passno, layno;
1067         double min, max;
1068         double cumdisto[100];   /* fixed_quality */
1069         const double K = 1;             /* 1.1; fixed_quality */
1070         double maxSE = 0;
1071
1072         opj_cp_t *cp = tcd->cp;
1073         opj_tcd_tile_t *tcd_tile = tcd->tcd_tile;
1074         opj_tcp_t *tcd_tcp = tcd->tcp;
1075
1076         min = DBL_MAX;
1077         max = 0;
1078         
1079         tcd_tile->numpix = 0;           /* fixed_quality */
1080         
1081         for (compno = 0; compno < tcd_tile->numcomps; compno++) {
1082                 opj_tcd_tilecomp_t *tilec = &tcd_tile->comps[compno];
1083                 tilec->numpix = 0;
1084
1085                 for (resno = 0; resno < tilec->numresolutions; resno++) {
1086                         opj_tcd_resolution_t *res = &tilec->resolutions[resno];
1087
1088                         for (bandno = 0; bandno < res->numbands; bandno++) {
1089                                 opj_tcd_band_t *band = &res->bands[bandno];
1090
1091                                 for (precno = 0; precno < res->pw * res->ph; precno++) {
1092                                         opj_tcd_precinct_t *prc = &band->precincts[precno];
1093
1094                                         for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
1095                                                 opj_tcd_cblk_enc_t *cblk = &prc->cblks.enc[cblkno];
1096
1097                                                 for (passno = 0; passno < cblk->totalpasses; passno++) {
1098                                                         opj_tcd_pass_t *pass = &cblk->passes[passno];
1099                                                         int dr;
1100                                                         double dd, rdslope;
1101                                                         if (passno == 0) {
1102                                                                 dr = pass->rate;
1103                                                                 dd = pass->distortiondec;
1104                                                         } else {
1105                                                                 dr = pass->rate - cblk->passes[passno - 1].rate;
1106                                                                 dd = pass->distortiondec - cblk->passes[passno - 1].distortiondec;
1107                                                         }
1108                                                         if (dr == 0) {
1109                                                                 continue;
1110                                                         }
1111                                                         rdslope = dd / dr;
1112                                                         if (rdslope < min) {
1113                                                                 min = rdslope;
1114                                                         }
1115                                                         if (rdslope > max) {
1116                                                                 max = rdslope;
1117                                                         }
1118                                                 } /* passno */
1119                                                 
1120                                                 /* fixed_quality */
1121                                                 tcd_tile->numpix += ((cblk->x1 - cblk->x0) * (cblk->y1 - cblk->y0));
1122                                                 tilec->numpix += ((cblk->x1 - cblk->x0) * (cblk->y1 - cblk->y0));
1123                                         } /* cbklno */
1124                                 } /* precno */
1125                         } /* bandno */
1126                 } /* resno */
1127                 
1128                 maxSE += (((double)(1 << tcd->image->comps[compno].prec) - 1.0) 
1129                         * ((double)(1 << tcd->image->comps[compno].prec) -1.0)) 
1130                         * ((double)(tilec->numpix));
1131         } /* compno */
1132         
1133         /* index file */
1134         if(cstr_info) {
1135                 opj_tile_info_t *tile_info = &cstr_info->tile[tcd->tcd_tileno];
1136                 tile_info->numpix = tcd_tile->numpix;
1137                 tile_info->distotile = tcd_tile->distotile;
1138                 tile_info->thresh = (double *) opj_malloc(tcd_tcp->numlayers * sizeof(double));
1139         }
1140         
1141         for (layno = 0; layno < tcd_tcp->numlayers; layno++) {
1142                 double lo = min;
1143                 double hi = max;
1144                 int success = 0;
1145                 int maxlen = tcd_tcp->rates[layno] ? int_min(((int) ceil(tcd_tcp->rates[layno])), len) : len;
1146                 double goodthresh = 0;
1147                 double stable_thresh = 0;
1148                 int i;
1149                 double distotarget;             /* fixed_quality */
1150                 
1151                 /* fixed_quality */
1152                 distotarget = tcd_tile->distotile - ((K * maxSE) / pow((float)10, tcd_tcp->distoratio[layno] / 10));
1153         
1154                 /* Don't try to find an optimal threshold but rather take everything not included yet, if
1155                   -r xx,yy,zz,0   (disto_alloc == 1 and rates == 0)
1156                   -q xx,yy,zz,0   (fixed_quality == 1 and distoratio == 0)
1157                   ==> possible to have some lossy layers and the last layer for sure lossless */
1158                 if ( ((cp->disto_alloc==1) && (tcd_tcp->rates[layno]>0)) || ((cp->fixed_quality==1) && (tcd_tcp->distoratio[layno]>0))) {
1159                         opj_t2_t *t2 = t2_create(tcd->cinfo, tcd->image, cp);
1160                         double thresh = 0;
1161
1162                         for (i = 0; i < 128; i++) {
1163                                 int l = 0;
1164                                 double distoachieved = 0;       /* fixed_quality */
1165                                 thresh = (lo + hi) / 2;
1166                                 
1167                                 tcd_makelayer(tcd, layno, thresh, 0);
1168                                 
1169                                 if (cp->fixed_quality) {        /* fixed_quality */
1170                                         if(cp->cinema){
1171                                                 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);
1172                                                 if (l == -999) {
1173                                                         lo = thresh;
1174                                                         continue;
1175                                                 }else{
1176                         distoachieved = layno == 0 ? 
1177                                                         tcd_tile->distolayer[0] : cumdisto[layno - 1] + tcd_tile->distolayer[layno];
1178                                                         if (distoachieved < distotarget) {
1179                                                                 hi=thresh; 
1180                                                                 stable_thresh = thresh;
1181                                                                 continue;
1182                                                         }else{
1183                                                                 lo=thresh;
1184                                                         }
1185                                                 }
1186                                         }else{
1187                                                 distoachieved = (layno == 0) ? 
1188                                                         tcd_tile->distolayer[0] : (cumdisto[layno - 1] + tcd_tile->distolayer[layno]);
1189                                                 if (distoachieved < distotarget) {
1190                                                         hi = thresh;
1191                                                         stable_thresh = thresh;
1192                                                         continue;
1193                                                 }
1194                                                 lo = thresh;
1195                                         }
1196                                 } else {
1197                                         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);
1198                                         /* TODO: what to do with l ??? seek / tell ??? */
1199                                         /* opj_event_msg(tcd->cinfo, EVT_INFO, "rate alloc: len=%d, max=%d\n", l, maxlen); */
1200                                         if (l == -999) {
1201                                                 lo = thresh;
1202                                                 continue;
1203                                         }
1204                                         hi = thresh;
1205                                         stable_thresh = thresh;
1206                                 }
1207                         }
1208                         success = 1;
1209                         goodthresh = stable_thresh == 0? thresh : stable_thresh;
1210                         t2_destroy(t2);
1211                 } else {
1212                         success = 1;
1213                         goodthresh = min;
1214                 }
1215                 
1216                 if (!success) {
1217                         return OPJ_FALSE;
1218                 }
1219                 
1220                 if(cstr_info) { /* Threshold for Marcela Index */
1221                         cstr_info->tile[tcd->tcd_tileno].thresh[layno] = goodthresh;
1222                 }
1223                 tcd_makelayer(tcd, layno, goodthresh, 1);
1224         
1225                 /* fixed_quality */
1226                 cumdisto[layno] = (layno == 0) ? tcd_tile->distolayer[0] : (cumdisto[layno - 1] + tcd_tile->distolayer[layno]); 
1227         }
1228
1229         return OPJ_TRUE;
1230 }
1231
1232 int tcd_encode_tile(opj_tcd_t *tcd, int tileno, unsigned char *dest, int len, opj_codestream_info_t *cstr_info) {
1233         int compno;
1234         int l, i, numpacks = 0;
1235         opj_tcd_tile_t *tile = NULL;
1236         opj_tcp_t *tcd_tcp = NULL;
1237         opj_cp_t *cp = NULL;
1238
1239         opj_tcp_t *tcp = &tcd->cp->tcps[0];
1240         opj_tccp_t *tccp = &tcp->tccps[0];
1241         opj_image_t *image = tcd->image;
1242         
1243         opj_t1_t *t1 = NULL;            /* T1 component */
1244         opj_t2_t *t2 = NULL;            /* T2 component */
1245
1246         tcd->tcd_tileno = tileno;
1247         tcd->tcd_tile = tcd->tcd_image->tiles;
1248         tcd->tcp = &tcd->cp->tcps[tileno];
1249
1250         tile = tcd->tcd_tile;
1251         tcd_tcp = tcd->tcp;
1252         cp = tcd->cp;
1253
1254         if(tcd->cur_tp_num == 0){
1255                 tcd->encoding_time = opj_clock();       /* time needed to encode a tile */
1256                 /* INDEX >> "Precinct_nb_X et Precinct_nb_Y" */
1257                 if(cstr_info) {
1258                         opj_tcd_tilecomp_t *tilec_idx = &tile->comps[0];        /* based on component 0 */
1259                         for (i = 0; i < tilec_idx->numresolutions; i++) {
1260                                 opj_tcd_resolution_t *res_idx = &tilec_idx->resolutions[i];
1261                                 
1262                                 cstr_info->tile[tileno].pw[i] = res_idx->pw;
1263                                 cstr_info->tile[tileno].ph[i] = res_idx->ph;
1264                                 
1265                                 numpacks += res_idx->pw * res_idx->ph;
1266                                 
1267                                 cstr_info->tile[tileno].pdx[i] = tccp->prcw[i];
1268                                 cstr_info->tile[tileno].pdy[i] = tccp->prch[i];
1269                         }
1270                         cstr_info->tile[tileno].packet = (opj_packet_info_t*) opj_calloc(cstr_info->numcomps * cstr_info->numlayers * numpacks, sizeof(opj_packet_info_t));
1271                 }
1272                 /* << INDEX */
1273                 
1274                 /*---------------TILE-------------------*/
1275                 
1276                 for (compno = 0; compno < tile->numcomps; compno++) {
1277                         int x, y;
1278                         
1279                         int adjust = image->comps[compno].sgnd ? 0 : 1 << (image->comps[compno].prec - 1);
1280                         int offset_x = int_ceildiv(image->x0, image->comps[compno].dx);
1281                         int offset_y = int_ceildiv(image->y0, image->comps[compno].dy);
1282                         
1283                         opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
1284                         int tw = tilec->x1 - tilec->x0;
1285                         int w = int_ceildiv(image->x1 - image->x0, image->comps[compno].dx);
1286                         
1287                         /* extract tile data */
1288                         
1289                         if (tcd_tcp->tccps[compno].qmfbid == 1) {
1290                                 for (y = tilec->y0; y < tilec->y1; y++) {
1291                                         /* start of the src tile scanline */
1292                                         int *data = &image->comps[compno].data[(tilec->x0 - offset_x) + (y - offset_y) * w];
1293                                         /* start of the dst tile scanline */
1294                                         int *tile_data = &tilec->data[(y - tilec->y0) * tw];
1295                                         for (x = tilec->x0; x < tilec->x1; x++) {
1296                                                 *tile_data++ = *data++ - adjust;
1297                                         }
1298                                 }
1299                         } else if (tcd_tcp->tccps[compno].qmfbid == 0) {
1300                                 for (y = tilec->y0; y < tilec->y1; y++) {
1301                                         /* start of the src tile scanline */
1302                                         int *data = &image->comps[compno].data[(tilec->x0 - offset_x) + (y - offset_y) * w];
1303                                         /* start of the dst tile scanline */
1304                                         int *tile_data = &tilec->data[(y - tilec->y0) * tw];
1305                                         for (x = tilec->x0; x < tilec->x1; x++) {
1306                                                 *tile_data++ = (*data++ - adjust) << 11;
1307                                         }
1308                                         
1309                                 }
1310                         }
1311                 }
1312                 
1313                 /*----------------MCT-------------------*/
1314                 if (tcd_tcp->mct) {
1315                         int samples = (tile->comps[0].x1 - tile->comps[0].x0) * (tile->comps[0].y1 - tile->comps[0].y0);
1316                         if (tcd_tcp->tccps[0].qmfbid == 0) {
1317                                 mct_encode_real(tile->comps[0].data, tile->comps[1].data, tile->comps[2].data, samples);
1318                         } else {
1319                                 mct_encode(tile->comps[0].data, tile->comps[1].data, tile->comps[2].data, samples);
1320                         }
1321                 }
1322                 
1323                 /*----------------DWT---------------------*/
1324                 
1325                 for (compno = 0; compno < tile->numcomps; compno++) {
1326                         opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
1327                         if (tcd_tcp->tccps[compno].qmfbid == 1) {
1328                                 dwt_encode(tilec);
1329                         } else if (tcd_tcp->tccps[compno].qmfbid == 0) {
1330                                 dwt_encode_real(tilec);
1331                         }
1332                 }
1333                 
1334                 /*------------------TIER1-----------------*/
1335                 t1 = t1_create(tcd->cinfo);
1336                 t1_encode_cblks(t1, tile, tcd_tcp);
1337                 t1_destroy(t1);
1338                 
1339                 /*-----------RATE-ALLOCATE------------------*/
1340                 
1341                 /* INDEX */
1342                 if(cstr_info) {
1343                         cstr_info->index_write = 0;
1344                 }
1345                 if (cp->disto_alloc || cp->fixed_quality) {     /* fixed_quality */
1346                         /* Normal Rate/distortion allocation */
1347                         tcd_rateallocate(tcd, dest, len, cstr_info);
1348                 } else {
1349                         /* Fixed layer allocation */
1350                         tcd_rateallocate_fixed(tcd);
1351                 }
1352         }
1353         /*--------------TIER2------------------*/
1354
1355         /* INDEX */
1356         if(cstr_info) {
1357                 cstr_info->index_write = 1;
1358         }
1359
1360         t2 = t2_create(tcd->cinfo, image, cp);
1361         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);
1362         t2_destroy(t2);
1363         
1364         /*---------------CLEAN-------------------*/
1365
1366         
1367         if(tcd->cur_tp_num == tcd->cur_totnum_tp - 1){
1368                 tcd->encoding_time = opj_clock() - tcd->encoding_time;
1369                 opj_event_msg(tcd->cinfo, EVT_INFO, "- tile encoded in %f s\n", tcd->encoding_time);
1370
1371                 /* cleaning memory */
1372                 for (compno = 0; compno < tile->numcomps; compno++) {
1373                         opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
1374                         opj_aligned_free(tilec->data);
1375                 }
1376         }
1377
1378         return l;
1379 }
1380
1381 opj_bool tcd_decode_tile(opj_tcd_t *tcd, unsigned char *src, int len, int tileno, opj_codestream_info_t *cstr_info) {
1382         int l;
1383         int compno;
1384         int eof = 0;
1385         double tile_time, t1_time, dwt_time;
1386         opj_tcd_tile_t *tile = NULL;
1387
1388         opj_t1_t *t1 = NULL;            /* T1 component */
1389         opj_t2_t *t2 = NULL;            /* T2 component */
1390         
1391         tcd->tcd_tileno = tileno;
1392         tcd->tcd_tile = &(tcd->tcd_image->tiles[tileno]);
1393         tcd->tcp = &(tcd->cp->tcps[tileno]);
1394         tile = tcd->tcd_tile;
1395         
1396         tile_time = opj_clock();        /* time needed to decode a tile */
1397         opj_event_msg(tcd->cinfo, EVT_INFO, "tile %d of %d\n", tileno + 1, tcd->cp->tw * tcd->cp->th);
1398
1399         /* INDEX >>  */
1400         if(cstr_info) {
1401                 int resno, compno, numprec = 0;
1402                 for (compno = 0; compno < cstr_info->numcomps; compno++) {
1403                         opj_tcp_t *tcp = &tcd->cp->tcps[0];
1404                         opj_tccp_t *tccp = &tcp->tccps[compno];
1405                         opj_tcd_tilecomp_t *tilec_idx = &tile->comps[compno];   
1406                         for (resno = 0; resno < tilec_idx->numresolutions; resno++) {
1407                                 opj_tcd_resolution_t *res_idx = &tilec_idx->resolutions[resno];
1408                                 cstr_info->tile[tileno].pw[resno] = res_idx->pw;
1409                                 cstr_info->tile[tileno].ph[resno] = res_idx->ph;
1410                                 numprec += res_idx->pw * res_idx->ph;
1411                                 if (tccp->csty & J2K_CP_CSTY_PRT) {
1412                                         cstr_info->tile[tileno].pdx[resno] = tccp->prcw[resno];
1413                                         cstr_info->tile[tileno].pdy[resno] = tccp->prch[resno];
1414                                 }
1415                                 else {
1416                                         cstr_info->tile[tileno].pdx[resno] = 15;
1417                                         cstr_info->tile[tileno].pdy[resno] = 15;
1418                                 }
1419                         }
1420                 }
1421                 cstr_info->tile[tileno].packet = (opj_packet_info_t *) opj_malloc(cstr_info->numlayers * numprec * sizeof(opj_packet_info_t));
1422                 cstr_info->packno = 0;
1423         }
1424         /* << INDEX */
1425         
1426         /*--------------TIER2------------------*/
1427         
1428         t2 = t2_create(tcd->cinfo, tcd->image, tcd->cp);
1429         l = t2_decode_packets(t2, src, len, tileno, tile, cstr_info);
1430         t2_destroy(t2);
1431
1432         if (l == -999) {
1433                 eof = 1;
1434                 opj_event_msg(tcd->cinfo, EVT_ERROR, "tcd_decode: incomplete bistream\n");
1435         }
1436         
1437         /*------------------TIER1-----------------*/
1438         
1439         t1_time = opj_clock();  /* time needed to decode a tile */
1440         t1 = t1_create(tcd->cinfo);
1441         for (compno = 0; compno < tile->numcomps; ++compno) {
1442                 opj_tcd_tilecomp_t* tilec = &tile->comps[compno];
1443                 /* The +3 is headroom required by the vectorized DWT */
1444                 tilec->data = (int*) opj_aligned_malloc((((tilec->x1 - tilec->x0) * (tilec->y1 - tilec->y0))+3) * sizeof(int));
1445                 t1_decode_cblks(t1, tilec, &tcd->tcp->tccps[compno]);
1446         }
1447         t1_destroy(t1);
1448         t1_time = opj_clock() - t1_time;
1449         opj_event_msg(tcd->cinfo, EVT_INFO, "- tiers-1 took %f s\n", t1_time);
1450         
1451         /*----------------DWT---------------------*/
1452
1453         dwt_time = opj_clock(); /* time needed to decode a tile */
1454         for (compno = 0; compno < tile->numcomps; compno++) {
1455                 opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
1456                 int numres2decode;
1457
1458                 if (tcd->cp->reduce != 0) {
1459                         tcd->image->comps[compno].resno_decoded =
1460                                 tile->comps[compno].numresolutions - tcd->cp->reduce - 1;
1461                         if (tcd->image->comps[compno].resno_decoded < 0) {                              
1462                                 opj_event_msg(tcd->cinfo, EVT_ERROR, "Error decoding tile. The number of resolutions to remove [%d+1] is higher than the number "
1463                                         " of resolutions in the original codestream [%d]\nModify the cp_reduce parameter.\n", tcd->cp->reduce, tile->comps[compno].numresolutions);
1464                                 return OPJ_FALSE;
1465                         }
1466                 }
1467
1468                 numres2decode = tcd->image->comps[compno].resno_decoded + 1;
1469                 if(numres2decode > 0){
1470                         if (tcd->tcp->tccps[compno].qmfbid == 1) {
1471                                 dwt_decode(tilec, numres2decode);
1472                         } else {
1473                                 dwt_decode_real(tilec, numres2decode);
1474                         }
1475                 }
1476         }
1477         dwt_time = opj_clock() - dwt_time;
1478         opj_event_msg(tcd->cinfo, EVT_INFO, "- dwt took %f s\n", dwt_time);
1479
1480         /*----------------MCT-------------------*/
1481
1482         if (tcd->tcp->mct) {
1483                 int n = (tile->comps[0].x1 - tile->comps[0].x0) * (tile->comps[0].y1 - tile->comps[0].y0);
1484
1485                 if (tile->numcomps >= 3 ){
1486                         if (tcd->tcp->tccps[0].qmfbid == 1) {
1487                                 mct_decode(
1488                                                 tile->comps[0].data,
1489                                                 tile->comps[1].data,
1490                                                 tile->comps[2].data,
1491                                                 n);
1492                         } else {
1493                                 mct_decode_real(
1494                                                 (float*)tile->comps[0].data,
1495                                                 (float*)tile->comps[1].data,
1496                                                 (float*)tile->comps[2].data,
1497                                                 n);
1498                         }
1499                 } else{
1500                         opj_event_msg(tcd->cinfo, EVT_WARNING,"Number of components (%d) is inconsistent with a MCT. Skip the MCT step.\n",tile->numcomps);
1501                 }
1502         }
1503
1504         /*---------------TILE-------------------*/
1505
1506         for (compno = 0; compno < tile->numcomps; ++compno) {
1507                 opj_tcd_tilecomp_t* tilec = &tile->comps[compno];
1508                 opj_image_comp_t* imagec = &tcd->image->comps[compno];
1509                 opj_tcd_resolution_t* res = &tilec->resolutions[imagec->resno_decoded];
1510                 int adjust = imagec->sgnd ? 0 : 1 << (imagec->prec - 1);
1511                 int min = imagec->sgnd ? -(1 << (imagec->prec - 1)) : 0;
1512                 int max = imagec->sgnd ?  (1 << (imagec->prec - 1)) - 1 : (1 << imagec->prec) - 1;
1513
1514                 int tw = tilec->x1 - tilec->x0;
1515                 int w = imagec->w;
1516
1517                 int offset_x = int_ceildivpow2(imagec->x0, imagec->factor);
1518                 int offset_y = int_ceildivpow2(imagec->y0, imagec->factor);
1519
1520                 int i, j;
1521                 if(!imagec->data){
1522                         imagec->data = (int*) opj_malloc(imagec->w * imagec->h * sizeof(int));
1523                 }
1524                 if(tcd->tcp->tccps[compno].qmfbid == 1) {
1525                         for(j = res->y0; j < res->y1; ++j) {
1526                                 for(i = res->x0; i < res->x1; ++i) {
1527                                         int v = tilec->data[i - res->x0 + (j - res->y0) * tw];
1528                                         v += adjust;
1529                                         imagec->data[(i - offset_x) + (j - offset_y) * w] = int_clamp(v, min, max);
1530                                 }
1531                         }
1532                 }else{
1533                         for(j = res->y0; j < res->y1; ++j) {
1534                                 for(i = res->x0; i < res->x1; ++i) {
1535                                         float tmp = ((float*)tilec->data)[i - res->x0 + (j - res->y0) * tw];
1536                                         int v = lrintf(tmp);
1537                                         v += adjust;
1538                                         imagec->data[(i - offset_x) + (j - offset_y) * w] = int_clamp(v, min, max);
1539                                 }
1540                         }
1541                 }
1542                 opj_aligned_free(tilec->data);
1543         }
1544
1545         tile_time = opj_clock() - tile_time;    /* time needed to decode a tile */
1546         opj_event_msg(tcd->cinfo, EVT_INFO, "- tile decoded in %f s\n", tile_time);
1547
1548         if (eof) {
1549                 return OPJ_FALSE;
1550         }
1551         
1552         return OPJ_TRUE;
1553 }
1554
1555 void tcd_free_decode(opj_tcd_t *tcd) {
1556         opj_tcd_image_t *tcd_image = tcd->tcd_image;    
1557         opj_free(tcd_image->tiles);
1558 }
1559
1560 void tcd_free_decode_tile(opj_tcd_t *tcd, int tileno) {
1561         int compno,resno,bandno,precno;
1562
1563         opj_tcd_image_t *tcd_image = tcd->tcd_image;
1564
1565         opj_tcd_tile_t *tile = &tcd_image->tiles[tileno];
1566         for (compno = 0; compno < tile->numcomps; compno++) {
1567                 opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
1568                 for (resno = 0; resno < tilec->numresolutions; resno++) {
1569                         opj_tcd_resolution_t *res = &tilec->resolutions[resno];
1570                         for (bandno = 0; bandno < res->numbands; bandno++) {
1571                                 opj_tcd_band_t *band = &res->bands[bandno];
1572                                 for (precno = 0; precno < res->ph * res->pw; precno++) {
1573                                         opj_tcd_precinct_t *prec = &band->precincts[precno];
1574                                         if (prec->imsbtree != NULL) tgt_destroy(prec->imsbtree);
1575                                         if (prec->incltree != NULL) tgt_destroy(prec->incltree);
1576                                 }
1577                                 opj_free(band->precincts);
1578                         }
1579                 }
1580                 opj_free(tilec->resolutions);
1581         }
1582         opj_free(tile->comps);
1583 }
1584
1585
1586 opj_bool tcd_init_v2(  opj_tcd_v2_t *p_tcd,
1587                                            opj_image_t * p_image,
1588                                            opj_cp_v2_t * p_cp )
1589 {
1590         OPJ_UINT32 l_tile_comp_size;
1591
1592         p_tcd->image = p_image;
1593         p_tcd->cp = p_cp;
1594
1595         p_tcd->tcd_image->tiles = (opj_tcd_tile_v2_t *) opj_malloc(sizeof(opj_tcd_tile_v2_t));
1596         if (! p_tcd->tcd_image->tiles) {
1597                 return OPJ_FALSE;
1598         }
1599         memset(p_tcd->tcd_image->tiles,0, sizeof(opj_tcd_tile_v2_t));
1600
1601         l_tile_comp_size = p_image->numcomps * sizeof(opj_tcd_tilecomp_v2_t);
1602         p_tcd->tcd_image->tiles->comps = (opj_tcd_tilecomp_v2_t *) opj_malloc(l_tile_comp_size);
1603         if (! p_tcd->tcd_image->tiles->comps ) {
1604                 return OPJ_FALSE;
1605         }
1606         memset( p_tcd->tcd_image->tiles->comps , 0 , l_tile_comp_size);
1607
1608         p_tcd->tcd_image->tiles->numcomps = p_image->numcomps;
1609         p_tcd->tp_pos = p_cp->m_specific_param.m_enc.m_tp_pos;
1610
1611         return OPJ_TRUE;
1612 }
1613
1614 /**
1615 Destroy a previously created TCD handle
1616 */
1617 void tcd_destroy_v2(opj_tcd_v2_t *tcd) {
1618         if (tcd) {
1619                 tcd_free_tile(tcd);
1620
1621                 if (tcd->tcd_image) {
1622                         opj_free(tcd->tcd_image);
1623                         tcd->tcd_image = 00;
1624                 }
1625                 opj_free(tcd);
1626         }
1627 }
1628
1629 /* ----------------------------------------------------------------------- */
1630 /**
1631  * Initialize the tile coder and may reuse some meory.
1632  * @param       p_tcd           TCD handle.
1633  * @param       p_image         raw image.
1634  * @param       p_cp            coding parameters.
1635  * @param       p_tile_no       current tile index to encode.
1636  *
1637  * @return true if the encoding values could be set (false otherwise).
1638 */
1639 #define MACRO_TCD_ALLOCATE(FUNCTION,TYPE,FRACTION,ELEMENT,FUNCTION_ELEMENT)             \
1640 opj_bool FUNCTION (     opj_tcd_v2_t *p_tcd,                                                                            \
1641                                         OPJ_UINT32 p_tile_no                                                                            \
1642                         )                                                                                                                                       \
1643 {                                                                                                                                                               \
1644         OPJ_UINT32 (*l_gain_ptr)(OPJ_UINT32) = 00;                                                                      \
1645         OPJ_UINT32 compno, resno, bandno, precno, cblkno;                                                       \
1646         opj_tcp_v2_t * l_tcp = 00;                                                                                                      \
1647         opj_cp_v2_t * l_cp = 00;                                                                                                        \
1648         opj_tcd_tile_v2_t * l_tile = 00;                                                                                        \
1649         opj_tccp_t *l_tccp = 00;                                                                                                        \
1650         opj_tcd_tilecomp_v2_t *l_tilec = 00;                                                                            \
1651         opj_image_comp_t * l_image_comp = 00;                                                                           \
1652         opj_tcd_resolution_v2_t *l_res = 00;                                                                            \
1653         opj_tcd_band_v2_t *l_band = 00;                                                                                         \
1654         opj_stepsize_t * l_step_size = 00;                                                                                      \
1655         opj_tcd_precinct_v2_t *l_current_precinct = 00;                                                         \
1656         TYPE* l_code_block = 00;                                                                                                        \
1657         opj_image_t *l_image = 00;                                                                                                      \
1658         OPJ_UINT32 p,q;                                                                                                                         \
1659         OPJ_UINT32 l_level_no;                                                                                                          \
1660         OPJ_UINT32 l_pdx, l_pdy;                                                                                                        \
1661         OPJ_UINT32 l_gain;                                                                                                                      \
1662         OPJ_INT32 l_x0b, l_y0b;                                                                                                         \
1663         /* extent of precincts , top left, bottom right**/                                                      \
1664         OPJ_INT32 l_tl_prc_x_start, l_tl_prc_y_start, l_br_prc_x_end, l_br_prc_y_end;   \
1665         /* number of precinct for a resolution */                                                                       \
1666         OPJ_UINT32 l_nb_precincts;                                                                                                      \
1667         /* room needed to store l_nb_precinct precinct for a resolution */                      \
1668         OPJ_UINT32 l_nb_precinct_size;                                                                                          \
1669         /* number of code blocks for a precinct*/                                                                       \
1670         OPJ_UINT32 l_nb_code_blocks;                                                                                            \
1671         /* room needed to store l_nb_code_blocks code blocks for a precinct*/           \
1672         OPJ_UINT32 l_nb_code_blocks_size;                                                                                       \
1673         /* size of data for a tile */                                                                                           \
1674         OPJ_UINT32 l_data_size;                                                                                                         \
1675                                                                                                                                                                 \
1676         l_cp = p_tcd->cp;                                                                                                                       \
1677         l_tcp = &(l_cp->tcps[p_tile_no]);                                                                                       \
1678         l_tile = p_tcd->tcd_image->tiles;                                                                                       \
1679         l_tccp = l_tcp->tccps;                                                                                                          \
1680         l_tilec = l_tile->comps;                                                                                                        \
1681         l_image = p_tcd->image;                                                                                                         \
1682         l_image_comp = p_tcd->image->comps;                                                                                     \
1683                                                                                                                                                                 \
1684         p = p_tile_no % l_cp->tw;       /* tile coordinates */                                                  \
1685         q = p_tile_no / l_cp->tw;                                                                                                       \
1686         /*fprintf(stderr, "Tile coordinate = %d,%d\n", p, q);*/                                         \
1687                                                                                                                                                                 \
1688         /* 4 borders of the tile rescale on the image if necessary */                           \
1689         l_tile->x0 = int_max(l_cp->tx0 + p * l_cp->tdx, l_image->x0);                           \
1690         l_tile->y0 = int_max(l_cp->ty0 + q * l_cp->tdy, l_image->y0);                           \
1691         l_tile->x1 = int_min(l_cp->tx0 + (p + 1) * l_cp->tdx, l_image->x1);                     \
1692         l_tile->y1 = int_min(l_cp->ty0 + (q + 1) * l_cp->tdy, l_image->y1);                     \
1693         /*fprintf(stderr, "Tile border = %d,%d,%d,%d\n", l_tile->x0, l_tile->y0,l_tile->x1,l_tile->y1);*/\
1694                                                                                                                                                                 \
1695         /*tile->numcomps = image->numcomps; */                                                                          \
1696         for(compno = 0; compno < l_tile->numcomps; ++compno) {                                          \
1697                 /*fprintf(stderr, "compno = %d/%d\n", compno, l_tile->numcomps);*/              \
1698                                                                                                                                                                 \
1699                 /* border of each l_tile component (global) */                                                  \
1700                 l_tilec->x0 = int_ceildiv(l_tile->x0, l_image_comp->dx);                                \
1701                 l_tilec->y0 = int_ceildiv(l_tile->y0, l_image_comp->dy);                                \
1702                 l_tilec->x1 = int_ceildiv(l_tile->x1, l_image_comp->dx);                                \
1703                 l_tilec->y1 = int_ceildiv(l_tile->y1, l_image_comp->dy);                                \
1704                 /*fprintf(stderr, "\tTile compo border = %d,%d,%d,%d\n", l_tilec->x0, l_tilec->y0,l_tilec->x1,l_tilec->y1);*/\
1705                                                                                                                                                                 \
1706                 l_data_size = (l_tilec->x1 - l_tilec->x0)                                                               \
1707                                         * (l_tilec->y1 - l_tilec->y0) * sizeof(OPJ_UINT32 );            \
1708                 l_tilec->numresolutions = l_tccp->numresolutions;                                               \
1709                 if (l_tccp->numresolutions < l_cp->m_specific_param.m_dec.m_reduce) {   \
1710                         l_tilec->minimum_num_resolutions = 1;                                                           \
1711                 }                                                                                                                                               \
1712                 else {                                                                                                                                  \
1713                         l_tilec->minimum_num_resolutions = l_tccp->numresolutions                       \
1714                                 - l_cp->m_specific_param.m_dec.m_reduce;                                                \
1715                 }                                                                                                                                               \
1716                                                                                                                                                                 \
1717                 if (l_tilec->data == 00) {                                                                                              \
1718             l_tilec->data = (OPJ_INT32 *) opj_malloc(l_data_size);              \
1719                         if (! l_tilec->data ) {                                                                                         \
1720                                 return OPJ_FALSE;                                                                                               \
1721                         }                                                                                                                                       \
1722             /*fprintf(stderr, "\tAllocate data of tilec (int): %d x OPJ_UINT32\n",l_data_size);*/       \
1723                                                                                                                                                                 \
1724                         l_tilec->data_size = l_data_size;                                                                       \
1725                 }                                                                                                                                               \
1726                 else if (l_data_size > l_tilec->data_size) {                                                    \
1727                         l_tilec->data = (OPJ_INT32 *) opj_realloc(l_tilec->data, l_data_size);  \
1728                         if (! l_tilec->data) {                                                                                          \
1729                                 return OPJ_FALSE;                                                                                               \
1730                         }                                                                                                                                       \
1731                         /*fprintf(stderr, "\tReallocate data of tilec (int): from %d to %d x OPJ_UINT32\n", l_tilec->data_size, l_data_size);*/         \
1732                         l_tilec->data_size = l_data_size;                                                                       \
1733                 }                                                                                                                                               \
1734                                                                                                                                                         \
1735                 l_data_size = l_tilec->numresolutions * sizeof(opj_tcd_resolution_v2_t);        \
1736                                                                                                                                                                 \
1737                 if (l_tilec->resolutions == 00) {                                                                               \
1738             l_tilec->resolutions = (opj_tcd_resolution_v2_t *) opj_malloc(l_data_size); \
1739                         if (! l_tilec->resolutions ) {                                                                          \
1740                                 return OPJ_FALSE;                                                                                               \
1741                         }                                                                                                                                       \
1742                         /*fprintf(stderr, "\tAllocate resolutions of tilec (opj_tcd_resolution_v2_t): %d\n",l_data_size);*/             \
1743                         l_tilec->resolutions_size = l_data_size;                                                        \
1744                         memset(l_tilec->resolutions,0,l_data_size);                                                     \
1745                 }                                                                                                                                               \
1746                 else if (l_data_size > l_tilec->resolutions_size) {                                             \
1747                         l_tilec->resolutions = (opj_tcd_resolution_v2_t *) opj_realloc(l_tilec->resolutions, l_data_size);      \
1748                         if (! l_tilec->resolutions) {                                                                           \
1749                                 return OPJ_FALSE;                                                                                               \
1750                         }                                                                                                                                       \
1751                         /*fprintf(stderr, "\tReallocate data of tilec (int): from %d to %d x OPJ_UINT32\n", l_tilec->resolutions_size, l_data_size);*/  \
1752                         memset(((OPJ_BYTE*) l_tilec->resolutions)+l_tilec->resolutions_size,0,l_data_size - l_tilec->resolutions_size); \
1753                         l_tilec->resolutions_size = l_data_size;                                                        \
1754                 }                                                                                                                                               \
1755                                                                                                                                                                 \
1756                 l_level_no = l_tilec->numresolutions - 1;                                                               \
1757                 l_res = l_tilec->resolutions;                                                                                   \
1758                 l_step_size = l_tccp->stepsizes;                                                                                \
1759                 if (l_tccp->qmfbid == 0) {                                                                                              \
1760                         l_gain_ptr = &dwt_getgain_real_v2;                                                                      \
1761                 }                                                                                                                                               \
1762                 else {                                                                                                                                  \
1763                         l_gain_ptr  = &dwt_getgain_v2;                                                                          \
1764                 }                                                                                                                                               \
1765                 /*fprintf(stderr, "\tlevel_no=%d\n",l_level_no);*/                                              \
1766                                                                                                                                                                 \
1767                 for(resno = 0; resno < l_tilec->numresolutions; ++resno) {                              \
1768                         /*fprintf(stderr, "\t\tresno = %d/%d\n", resno, l_tilec->numresolutions);*/     \
1769                         OPJ_INT32 tlcbgxstart, tlcbgystart, brcbgxend, brcbgyend;                       \
1770                         OPJ_UINT32 cbgwidthexpn, cbgheightexpn;                                                         \
1771                         OPJ_UINT32 cblkwidthexpn, cblkheightexpn;                                                       \
1772                                                                                                                                                                 \
1773                         /* border for each resolution level (global) */                                         \
1774                         l_res->x0 = int_ceildivpow2(l_tilec->x0, l_level_no);                           \
1775                         l_res->y0 = int_ceildivpow2(l_tilec->y0, l_level_no);                           \
1776                         l_res->x1 = int_ceildivpow2(l_tilec->x1, l_level_no);                           \
1777                         l_res->y1 = int_ceildivpow2(l_tilec->y1, l_level_no);                           \
1778                         /*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);*/        \
1779                         /* p. 35, table A-23, ISO/IEC FDIS154444-1 : 2000 (18 august 2000) */   \
1780                         l_pdx = l_tccp->prcw[resno];                                                                            \
1781                         l_pdy = l_tccp->prch[resno];                                                                            \
1782                         /*fprintf(stderr, "\t\t\tpdx=%d, pdy=%d\n", l_pdx, l_pdy);*/            \
1783                         /* p. 64, B.6, ISO/IEC FDIS15444-1 : 2000 (18 august 2000)  */          \
1784                         l_tl_prc_x_start = int_floordivpow2(l_res->x0, l_pdx) << l_pdx;         \
1785                         l_tl_prc_y_start = int_floordivpow2(l_res->y0, l_pdy) << l_pdy;         \
1786                         l_br_prc_x_end = int_ceildivpow2(l_res->x1, l_pdx) << l_pdx;            \
1787                         l_br_prc_y_end = int_ceildivpow2(l_res->y1, l_pdy) << l_pdy;            \
1788                         /*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 );*/  \
1789                                                                                                                                                                 \
1790                         l_res->pw = (l_res->x0 == l_res->x1) ? 0 : ((l_br_prc_x_end - l_tl_prc_x_start) >> l_pdx);      \
1791                         l_res->ph = (l_res->y0 == l_res->y1) ? 0 : ((l_br_prc_y_end - l_tl_prc_y_start) >> l_pdy);      \
1792                         /*fprintf(stderr, "\t\t\tres_pw=%d, res_ph=%d\n", l_res->pw, l_res->ph );*/     \
1793                                                                                                                                                                 \
1794                         l_nb_precincts = l_res->pw * l_res->ph;                                                         \
1795                         l_nb_precinct_size = l_nb_precincts * sizeof(opj_tcd_precinct_v2_t);    \
1796                         if (resno == 0) {                                                                                                       \
1797                                 tlcbgxstart = l_tl_prc_x_start;                                                                 \
1798                                 tlcbgystart = l_tl_prc_y_start;                                                                 \
1799                                 brcbgxend = l_br_prc_x_end;                                                                             \
1800                                 brcbgyend = l_br_prc_y_end;                                                                             \
1801                                 cbgwidthexpn = l_pdx;                                                                                   \
1802                                 cbgheightexpn = l_pdy;                                                                                  \
1803                                 l_res->numbands = 1;                                                                                    \
1804                         }                                                                                                                                       \
1805                         else {                                                                                                                          \
1806                                 tlcbgxstart = int_ceildivpow2(l_tl_prc_x_start, 1);                             \
1807                                 tlcbgystart = int_ceildivpow2(l_tl_prc_y_start, 1);                             \
1808                                 brcbgxend = int_ceildivpow2(l_br_prc_x_end, 1);                                 \
1809                                 brcbgyend = int_ceildivpow2(l_br_prc_y_end, 1);                                 \
1810                                 cbgwidthexpn = l_pdx - 1;                                                                               \
1811                                 cbgheightexpn = l_pdy - 1;                                                                              \
1812                                 l_res->numbands = 3;                                                                                    \
1813                         }                                                                                                                                       \
1814                                                                                                                                                                 \
1815                         cblkwidthexpn = uint_min(l_tccp->cblkw, cbgwidthexpn);                          \
1816                         cblkheightexpn = uint_min(l_tccp->cblkh, cbgheightexpn);                        \
1817                         l_band = l_res->bands;                                                                                          \
1818                                                                                                                                                                 \
1819                         for (bandno = 0; bandno < l_res->numbands; ++bandno) {                          \
1820                                 OPJ_INT32 numbps;\
1821                                 /*fprintf(stderr, "\t\t\tband_no=%d/%d\n", bandno, l_res->numbands );*/ \
1822                                                                                                                                                                 \
1823                                 if (resno == 0) {                                                                                               \
1824                                         l_band->bandno = 0 ;                                                                            \
1825                                         l_band->x0 = int_ceildivpow2(l_tilec->x0, l_level_no);          \
1826                                         l_band->y0 = int_ceildivpow2(l_tilec->y0, l_level_no);          \
1827                                         l_band->x1 = int_ceildivpow2(l_tilec->x1, l_level_no);          \
1828                                         l_band->y1 = int_ceildivpow2(l_tilec->y1, l_level_no);          \
1829                                 }                                                                                                                               \
1830                                 else {                                                                                                                  \
1831                                         l_band->bandno = bandno + 1;                                                            \
1832                                         /* x0b = 1 if bandno = 1 or 3 */                                                        \
1833                                         l_x0b = l_band->bandno&1;                                                                       \
1834                                         /* y0b = 1 if bandno = 2 or 3 */                                                        \
1835                                         l_y0b = (l_band->bandno)>>1;                                                            \
1836                                         /* l_band border (global) */                                                            \
1837                                         l_band->x0 = int_ceildivpow2(l_tilec->x0 - (1 << l_level_no) * l_x0b, l_level_no + 1);  \
1838                                         l_band->y0 = int_ceildivpow2(l_tilec->y0 - (1 << l_level_no) * l_y0b, l_level_no + 1);  \
1839                                         l_band->x1 = int_ceildivpow2(l_tilec->x1 - (1 << l_level_no) * l_x0b, l_level_no + 1);  \
1840                                         l_band->y1 = int_ceildivpow2(l_tilec->y1 - (1 << l_level_no) * l_y0b, l_level_no + 1);  \
1841                                 }                                                                                                                               \
1842                                                                                                                                                                 \
1843                                 /** avoid an if with storing function pointer */                                \
1844                                 l_gain = (*l_gain_ptr) (l_band->bandno);                                                \
1845                                 numbps = l_image_comp->prec + l_gain;                                                   \
1846                                 l_band->stepsize = (OPJ_FLOAT32)(((1.0 + l_step_size->mant / 2048.0) * pow(2.0, (OPJ_INT32) (numbps - l_step_size->expn)))) * FRACTION;\
1847                                 l_band->numbps = l_step_size->expn + l_tccp->numgbits - 1;      /* WHY -1 ? */\
1848                                                                                                                                                                 \
1849                                 if (! l_band->precincts) {                                                                              \
1850                                         l_band->precincts = (opj_tcd_precinct_v2_t *) opj_malloc( /*3 * */ l_nb_precinct_size);\
1851                                         if (! l_band->precincts) {                                                                      \
1852                                                 return OPJ_FALSE;                                                                               \
1853                                         }                                                                                                                       \
1854                                         /*fprintf(stderr, "\t\t\t\tAllocate precincts of a band (opj_tcd_precinct_v2_t): %d\n",l_nb_precinct_size);     */      \
1855                                         memset(l_band->precincts,0,l_nb_precinct_size);                         \
1856                                         l_band->precincts_data_size = l_nb_precinct_size;                       \
1857                                 }                                                                                                                               \
1858                                 else if (l_band->precincts_data_size < l_nb_precinct_size) {    \
1859                                                                                                                                                                 \
1860                                         l_band->precincts = (opj_tcd_precinct_v2_t *) opj_realloc(l_band->precincts,/*3 * */ l_nb_precinct_size);\
1861                                         if (! l_band->precincts) {                                                                      \
1862                                                 return OPJ_FALSE;                                                                               \
1863                                         }                                                                                                                       \
1864                                         /*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);*/\
1865                                         memset(((OPJ_BYTE *) l_band->precincts) + l_band->precincts_data_size,0,l_nb_precinct_size - l_band->precincts_data_size);\
1866                                         l_band->precincts_data_size = l_nb_precinct_size;                       \
1867                                 }                                                                                                                               \
1868                                                                                                                                                                 \
1869                                 l_current_precinct = l_band->precincts;                                                 \
1870                                 for     (precno = 0; precno < l_nb_precincts; ++precno) {                       \
1871                                         OPJ_INT32 tlcblkxstart, tlcblkystart, brcblkxend, brcblkyend;   \
1872                                         OPJ_INT32 cbgxstart = tlcbgxstart + (precno % l_res->pw) * (1 << cbgwidthexpn); \
1873                                         OPJ_INT32 cbgystart = tlcbgystart + (precno / l_res->pw) * (1 << cbgheightexpn);        \
1874                                         OPJ_INT32 cbgxend = cbgxstart + (1 << cbgwidthexpn);            \
1875                                         OPJ_INT32 cbgyend = cbgystart + (1 << cbgheightexpn);           \
1876                                         /*fprintf(stderr, "\t precno=%d; bandno=%d, resno=%d; compno=%d\n", precno, bandno , resno, compno);*/\
1877                                         /*fprintf(stderr, "\t tlcbgxstart(=%d) + (precno(=%d) percent res->pw(=%d)) * (1 << cbgwidthexpn(=%d)) \n",tlcbgxstart,precno,l_res->pw,cbgwidthexpn);*/\
1878                                                                                                                                                                 \
1879                                         /* precinct size (global) */                                                            \
1880                                         /*fprintf(stderr, "\t cbgxstart=%d, l_band->x0 = %d \n",cbgxstart, l_band->x0);*/ \
1881                                                                                                                                                                 \
1882                                         l_current_precinct->x0 = int_max(cbgxstart, l_band->x0);        \
1883                                         l_current_precinct->y0 = int_max(cbgystart, l_band->y0);        \
1884                                         l_current_precinct->x1 = int_min(cbgxend, l_band->x1);          \
1885                                         l_current_precinct->y1 = int_min(cbgyend, l_band->y1);          \
1886                                         /*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);*/ \
1887                                                                                                                                                                 \
1888                                         tlcblkxstart = int_floordivpow2(l_current_precinct->x0, cblkwidthexpn) << cblkwidthexpn;        \
1889                                         /*fprintf(stderr, "\t tlcblkxstart =%d\n",tlcblkxstart );*/ \
1890                                         tlcblkystart = int_floordivpow2(l_current_precinct->y0, cblkheightexpn) << cblkheightexpn;      \
1891                                         /*fprintf(stderr, "\t tlcblkystart =%d\n",tlcblkystart );*/ \
1892                                         brcblkxend = int_ceildivpow2(l_current_precinct->x1, cblkwidthexpn) << cblkwidthexpn;   \
1893                                         /*fprintf(stderr, "\t brcblkxend =%d\n",brcblkxend );*/         \
1894                                         brcblkyend = int_ceildivpow2(l_current_precinct->y1, cblkheightexpn) << cblkheightexpn; \
1895                                         /*fprintf(stderr, "\t brcblkyend =%d\n",brcblkyend );*/         \
1896                                         l_current_precinct->cw = (brcblkxend - tlcblkxstart) >> cblkwidthexpn;  \
1897                                         l_current_precinct->ch = (brcblkyend - tlcblkystart) >> cblkheightexpn; \
1898                                                                                                                                                                 \
1899                                         l_nb_code_blocks = l_current_precinct->cw * l_current_precinct->ch;     \
1900                                         /*fprintf(stderr, "\t\t\t\t precinct_cw = %d x recinct_ch = %d\n",l_current_precinct->cw, l_current_precinct->ch);      */      \
1901                                         l_nb_code_blocks_size = l_nb_code_blocks * sizeof(TYPE);        \
1902                                                                                                                                                                 \
1903                                         if (! l_current_precinct->cblks.ELEMENT) {                                      \
1904                                                 l_current_precinct->cblks.ELEMENT = (TYPE*) opj_malloc(l_nb_code_blocks_size);\
1905                                                 if (! l_current_precinct->cblks.ELEMENT ) {                             \
1906                                                         return OPJ_FALSE;                                                                       \
1907                                                 }                                                                                                               \
1908                                                 /*fprintf(stderr, "\t\t\t\tAllocate cblks of a precinct (opj_tcd_cblk_dec_v2_t): %d\n",l_nb_code_blocks_size);*/                \
1909                                                                                                                                                                 \
1910                                                 memset(l_current_precinct->cblks.ELEMENT,0,l_nb_code_blocks_size);\
1911                                                                                                                                                                 \
1912                                                 l_current_precinct->block_size = l_nb_code_blocks_size; \
1913                                         }                                                                                                                       \
1914                                         else if (l_nb_code_blocks_size > l_current_precinct->block_size) {      \
1915                                                 l_current_precinct->cblks.ELEMENT = (TYPE*)                                                                     \
1916                                                         opj_realloc(l_current_precinct->cblks.ELEMENT, l_nb_code_blocks_size);  \
1917                                                 if (! l_current_precinct->cblks.ELEMENT ) {                             \
1918                                                         return OPJ_FALSE;                                                                       \
1919                                                 }                                                                                                               \
1920                                                 /*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);     */      \
1921                                                                                                                                                                 \
1922                                                 memset(((OPJ_BYTE *) l_current_precinct->cblks.ELEMENT) + l_current_precinct->block_size        \
1923                                                                                 ,0                                                                                                                                                      \
1924                                                                                 ,l_nb_code_blocks_size - l_current_precinct->block_size);                                       \
1925                                                                                                                                                                 \
1926                                                 l_current_precinct->block_size = l_nb_code_blocks_size; \
1927                                         }                                                                                                                       \
1928                                                                                                                                                                 \
1929                                         if (! l_current_precinct->incltree) {                                           \
1930                         l_current_precinct->incltree = tgt_create_v2(l_current_precinct->cw,    \
1931                                                                                                                                   l_current_precinct->ch);      \
1932                                         }                                                                                                                       \
1933                                         else{                                                                                                           \
1934                                                 l_current_precinct->incltree = tgt_init(l_current_precinct->incltree,   \
1935                                                                                                                                 l_current_precinct->cw,                 \
1936                                                                                                                                 l_current_precinct->ch);                \
1937                                         }                                                                                                                       \
1938                                                                                                                                                                 \
1939                                         if (! l_current_precinct->incltree)     {                                               \
1940                                                 fprintf(stderr, "WARNING: No incltree created.\n");\
1941                                                 /*return OPJ_FALSE;*/                                                                           \
1942                                         }                                                                                                                       \
1943                                                                                                                                                                 \
1944                                         if (! l_current_precinct->imsbtree) {                                           \
1945                         l_current_precinct->imsbtree = tgt_create_v2(                           \
1946                                                                                                                 l_current_precinct->cw, \
1947                                                                                                                 l_current_precinct->ch);\
1948                                         }                                                                                                                       \
1949                                         else {                                                                                                          \
1950                                                 l_current_precinct->imsbtree = tgt_init(                                                        \
1951                                                                                                                         l_current_precinct->imsbtree,   \
1952                                                                                                                         l_current_precinct->cw,                 \
1953                                                                                                                         l_current_precinct->ch);                \
1954                                         }                                                                                                                       \
1955                                                                                                                                                                 \
1956                                         if (! l_current_precinct->imsbtree) {                                           \
1957                                                 fprintf(stderr, "WARNING: No imsbtree created.\n");\
1958                                                 /*return OPJ_FALSE;*/                                                                           \
1959                                         }                                                                                                                       \
1960                                                                                                                                                                 \
1961                                         l_code_block = l_current_precinct->cblks.ELEMENT;                       \
1962                                                                                                                                                                 \
1963                                         for (cblkno = 0; cblkno < l_nb_code_blocks; ++cblkno) {         \
1964                                                 OPJ_INT32 cblkxstart = tlcblkxstart + (cblkno % l_current_precinct->cw) * (1 << cblkwidthexpn);         \
1965                                                 OPJ_INT32 cblkystart = tlcblkystart + (cblkno / l_current_precinct->cw) * (1 << cblkheightexpn);        \
1966                                                 OPJ_INT32 cblkxend = cblkxstart + (1 << cblkwidthexpn); \
1967                                                 OPJ_INT32 cblkyend = cblkystart + (1 << cblkheightexpn);\
1968                                                                                                                                                                 \
1969                                                 /* code-block size (global) */                                                  \
1970                                                 l_code_block->x0 = int_max(cblkxstart, l_current_precinct->x0); \
1971                                                 l_code_block->y0 = int_max(cblkystart, l_current_precinct->y0); \
1972                                                 l_code_block->x1 = int_min(cblkxend, l_current_precinct->x1);   \
1973                                                 l_code_block->y1 = int_min(cblkyend, l_current_precinct->y1);   \
1974                                                                                                                                                                 \
1975                                                 if (! FUNCTION_ELEMENT(l_code_block)) {                                 \
1976                                                         return OPJ_FALSE;                                                                       \
1977                                                 }                                                                                                               \
1978                                                 ++l_code_block;                                                                                 \
1979                                         }                                                                                                                       \
1980                                         ++l_current_precinct;                                                                           \
1981                                 } /* precno */                                                                                                  \
1982                                 ++l_band;                                                                                                               \
1983                                 ++l_step_size;                                                                                                  \
1984                         } /* bandno */                                                                                                          \
1985                         ++l_res;                                                                                                                        \
1986                         --l_level_no;                                                                                                           \
1987                 } /* resno */                                                                                                                   \
1988                 ++l_tccp;                                                                                                                               \
1989                 ++l_tilec;                                                                                                                              \
1990                 ++l_image_comp;                                                                                                                 \
1991         } /* compno */                                                                                                                          \
1992         return OPJ_TRUE;                                                                                                                        \
1993 }                                                                                                                                                               \
1994
1995
1996 // V2 ENCODE MACRO_TCD_ALLOCATE(tcd_init_encode_tile,opj_tcd_cblk_enc_t,1.f,enc,tcd_code_block_enc_allocate)
1997 MACRO_TCD_ALLOCATE(tcd_init_decode_tile, opj_tcd_cblk_dec_v2_t, 0.5f, dec, tcd_code_block_dec_allocate)
1998
1999 #undef MACRO_TCD_ALLOCATE
2000
2001
2002 OPJ_UINT32 tcd_get_decoded_tile_size ( opj_tcd_v2_t *p_tcd )
2003 {
2004         OPJ_UINT32 i;
2005         OPJ_UINT32 l_data_size = 0;
2006         opj_image_comp_t * l_img_comp = 00;
2007         opj_tcd_tilecomp_v2_t * l_tile_comp = 00;
2008         opj_tcd_resolution_v2_t * l_res = 00;
2009         OPJ_UINT32 l_size_comp, l_remaining;
2010
2011         l_tile_comp = p_tcd->tcd_image->tiles->comps;
2012         l_img_comp = p_tcd->image->comps;
2013
2014         for (i=0;i<p_tcd->image->numcomps;++i) {
2015                 l_size_comp = l_img_comp->prec >> 3; /*(/ 8)*/
2016                 l_remaining = l_img_comp->prec & 7;  /* (%8) */
2017
2018                 if(l_remaining) {
2019                         ++l_size_comp;
2020                 }
2021
2022                 if (l_size_comp == 3) {
2023                         l_size_comp = 4;
2024                 }
2025
2026                 l_res = l_tile_comp->resolutions + l_tile_comp->minimum_num_resolutions - 1;
2027                 l_data_size += l_size_comp * (l_res->x1 - l_res->x0) * (l_res->y1 - l_res->y0);
2028                 ++l_img_comp;
2029                 ++l_tile_comp;
2030         }
2031
2032         return l_data_size;
2033 }
2034
2035
2036 opj_bool tcd_decode_tile_v2(
2037                                          opj_tcd_v2_t *p_tcd,
2038                                          OPJ_BYTE *p_src,
2039                                          OPJ_UINT32 p_max_length,
2040                                          OPJ_UINT32 p_tile_no,
2041                                          opj_codestream_index_t *p_cstr_index)
2042 {
2043         OPJ_UINT32 l_data_read;
2044         p_tcd->tcd_tileno = p_tile_no;
2045         p_tcd->tcp = &(p_tcd->cp->tcps[p_tile_no]);
2046
2047 #ifdef TODO_MSD /* FIXME */
2048         /* INDEX >>  */
2049         if(p_cstr_info) {
2050                 OPJ_UINT32 resno, compno, numprec = 0;
2051                 for (compno = 0; compno < (OPJ_UINT32) p_cstr_info->numcomps; compno++) {
2052                         opj_tcp_v2_t *tcp = &p_tcd->cp->tcps[0];
2053                         opj_tccp_t *tccp = &tcp->tccps[compno];
2054                         opj_tcd_tilecomp_v2_t *tilec_idx = &p_tcd->tcd_image->tiles->comps[compno];
2055                         for (resno = 0; resno < tilec_idx->numresolutions; resno++) {
2056                                 opj_tcd_resolution_v2_t *res_idx = &tilec_idx->resolutions[resno];
2057                                 p_cstr_info->tile[p_tile_no].pw[resno] = res_idx->pw;
2058                                 p_cstr_info->tile[p_tile_no].ph[resno] = res_idx->ph;
2059                                 numprec += res_idx->pw * res_idx->ph;
2060                                 p_cstr_info->tile[p_tile_no].pdx[resno] = tccp->prcw[resno];
2061                                 p_cstr_info->tile[p_tile_no].pdy[resno] = tccp->prch[resno];
2062                         }
2063                 }
2064                 p_cstr_info->tile[p_tile_no].packet = (opj_packet_info_t *) opj_malloc(p_cstr_info->numlayers * numprec * sizeof(opj_packet_info_t));
2065                 p_cstr_info->packno = 0;
2066         }
2067         /* << INDEX */
2068 #endif
2069
2070         /*--------------TIER2------------------*/
2071         // FIXME _ProfStart(PGROUP_T2);
2072         l_data_read = 0;
2073         if
2074                 (! tcd_t2_decode(p_tcd, p_src, &l_data_read, p_max_length, p_cstr_index))
2075         {
2076                 return OPJ_FALSE;
2077         }
2078         // FIXME _ProfStop(PGROUP_T2);
2079
2080         /*------------------TIER1-----------------*/
2081
2082         // FIXME _ProfStart(PGROUP_T1);
2083         if
2084                 (! tcd_t1_decode(p_tcd))
2085         {
2086                 return OPJ_FALSE;
2087         }
2088         // FIXME _ProfStop(PGROUP_T1);
2089
2090         /*----------------DWT---------------------*/
2091
2092         // FIXME _ProfStart(PGROUP_DWT);
2093         if
2094                 (! tcd_dwt_decode(p_tcd))
2095         {
2096                 return OPJ_FALSE;
2097         }
2098         // FIXME _ProfStop(PGROUP_DWT);
2099
2100         /*----------------MCT-------------------*/
2101         // FIXME _ProfStart(PGROUP_MCT);
2102         if
2103                 (! tcd_mct_decode(p_tcd))
2104         {
2105                 return OPJ_FALSE;
2106         }
2107         // FIXME _ProfStop(PGROUP_MCT);
2108
2109         // FIXME _ProfStart(PGROUP_DC_SHIFT);
2110         if
2111                 (! tcd_dc_level_shift_decode(p_tcd))
2112         {
2113                 return OPJ_FALSE;
2114         }
2115         // FIXME _ProfStop(PGROUP_DC_SHIFT);
2116
2117
2118         /*---------------TILE-------------------*/
2119         return OPJ_TRUE;
2120 }
2121
2122 opj_bool tcd_update_tile_data (
2123                                                  opj_tcd_v2_t *p_tcd,
2124                                                  OPJ_BYTE * p_dest,
2125                                                  OPJ_UINT32 p_dest_length
2126                                                  )
2127 {
2128         OPJ_UINT32 i,j,k,l_data_size = 0;
2129         opj_image_comp_t * l_img_comp = 00;
2130         opj_tcd_tilecomp_v2_t * l_tilec = 00;
2131         opj_tcd_resolution_v2_t * l_res;
2132         OPJ_UINT32 l_size_comp, l_remaining;
2133         OPJ_UINT32 l_stride, l_width,l_height;
2134
2135         l_data_size = tcd_get_decoded_tile_size(p_tcd);
2136         if (l_data_size > p_dest_length) {
2137                 return OPJ_FALSE;
2138         }
2139
2140         l_tilec = p_tcd->tcd_image->tiles->comps;
2141         l_img_comp = p_tcd->image->comps;
2142
2143         for (i=0;i<p_tcd->image->numcomps;++i) {
2144                 l_size_comp = l_img_comp->prec >> 3; /*(/ 8)*/
2145                 l_remaining = l_img_comp->prec & 7;  /* (%8) */
2146                 l_res = l_tilec->resolutions + l_img_comp->resno_decoded;
2147                 l_width = (l_res->x1 - l_res->x0);
2148                 l_height = (l_res->y1 - l_res->y0);
2149                 l_stride = (l_tilec->x1 - l_tilec->x0) - l_width;
2150
2151                 if (l_remaining) {
2152                         ++l_size_comp;
2153                 }
2154
2155                 if (l_size_comp == 3) {
2156                         l_size_comp = 4;
2157                 }
2158
2159                 switch (l_size_comp)
2160                         {
2161                         case 1:
2162                                 {
2163                                         OPJ_CHAR * l_dest_ptr = (OPJ_CHAR *) p_dest;
2164                                         const OPJ_INT32 * l_src_ptr = l_tilec->data;
2165
2166                                         if (l_img_comp->sgnd) {
2167                                                 for (j=0;j<l_height;++j) {
2168                                                         for (k=0;k<l_width;++k) {
2169                                                                 *(l_dest_ptr++) = (OPJ_CHAR) (*(l_src_ptr++));
2170                                                         }
2171                                                         l_src_ptr += l_stride;
2172                                                 }
2173                                         }
2174                                         else {
2175                                                 for (j=0;j<l_height;++j) {
2176                                                         for     (k=0;k<l_width;++k) {
2177                                                                 *(l_dest_ptr++) = (OPJ_BYTE) ((*(l_src_ptr++))&0xff);
2178                                                         }
2179                                                         l_src_ptr += l_stride;
2180                                                 }
2181                                         }
2182
2183                                         p_dest = (OPJ_BYTE *)l_dest_ptr;
2184                                 }
2185                                 break;
2186                         case 2:
2187                                 {
2188                                         const OPJ_INT32 * l_src_ptr = l_tilec->data;
2189                                         OPJ_INT16 * l_dest_ptr = (OPJ_INT16 *) p_dest;
2190
2191                                         if (l_img_comp->sgnd) {
2192                                                 for (j=0;j<l_height;++j) {
2193                                                         for (k=0;k<l_width;++k) {
2194                                                                 *(l_dest_ptr++) = (OPJ_INT16) (*(l_src_ptr++));
2195                                                         }
2196                                                         l_src_ptr += l_stride;
2197                                                 }
2198                                         }
2199                                         else {
2200                                                 for (j=0;j<l_height;++j) {
2201                                                         for (k=0;k<l_width;++k) {
2202                                                                 *(l_dest_ptr++) = (OPJ_UINT16) ((*(l_src_ptr++))&0xffff);
2203                                                         }
2204                                                         l_src_ptr += l_stride;
2205                                                 }
2206                                         }
2207
2208                                         p_dest = (OPJ_BYTE*) l_dest_ptr;
2209                                 }
2210                                 break;
2211                         case 4:
2212                                 {
2213                                         OPJ_INT32 * l_dest_ptr = (OPJ_INT32 *) p_dest;
2214                                         OPJ_INT32 * l_src_ptr = l_tilec->data;
2215
2216                                         for (j=0;j<l_height;++j) {
2217                                                 for (k=0;k<l_width;++k) {
2218                                                         *(l_dest_ptr++) = (*(l_src_ptr++));
2219                                                 }
2220                                                 l_src_ptr += l_stride;
2221                                         }
2222
2223                                         p_dest = (OPJ_BYTE*) l_dest_ptr;
2224                                 }
2225                                 break;
2226                 }
2227
2228                 ++l_img_comp;
2229                 ++l_tilec;
2230         }
2231
2232         return OPJ_TRUE;
2233 }
2234
2235
2236
2237
2238 void tcd_free_tile(opj_tcd_v2_t *p_tcd)
2239 {
2240         OPJ_UINT32 compno, resno, bandno, precno;
2241         opj_tcd_tile_v2_t *l_tile = 00;
2242         opj_tcd_tilecomp_v2_t *l_tile_comp = 00;
2243         opj_tcd_resolution_v2_t *l_res = 00;
2244         opj_tcd_band_v2_t *l_band = 00;
2245         opj_tcd_precinct_v2_t *l_precinct = 00;
2246         OPJ_UINT32 l_nb_resolutions, l_nb_precincts;
2247         void (* l_tcd_code_block_deallocate) (opj_tcd_precinct_v2_t *) = 00;
2248
2249         if (! p_tcd) {
2250                 return;
2251         }
2252
2253         if (! p_tcd->tcd_image) {
2254                 return;
2255         }
2256
2257         if (p_tcd->m_is_decoder) {
2258                 l_tcd_code_block_deallocate = tcd_code_block_dec_deallocate;
2259         }
2260         else {
2261                 // FIXME l_tcd_code_block_deallocate = tcd_code_block_enc_deallocate;
2262         }
2263
2264         l_tile = p_tcd->tcd_image->tiles;
2265         if (! l_tile) {
2266                 return;
2267         }
2268
2269         l_tile_comp = l_tile->comps;
2270
2271         for (compno = 0; compno < l_tile->numcomps; ++compno) {
2272                 l_res = l_tile_comp->resolutions;
2273                 if (l_res) {
2274
2275                         l_nb_resolutions = l_tile_comp->resolutions_size / sizeof(opj_tcd_resolution_v2_t);
2276                         for (resno = 0; resno < l_nb_resolutions; ++resno) {
2277                                 l_band = l_res->bands;
2278                                 for     (bandno = 0; bandno < 3; ++bandno) {
2279                                         l_precinct = l_band->precincts;
2280                                         if (l_precinct) {
2281
2282                                                 l_nb_precincts = l_band->precincts_data_size / sizeof(opj_tcd_precinct_v2_t);
2283                                                 for (precno = 0; precno < l_nb_precincts; ++precno) {
2284                                                         tgt_destroy(l_precinct->incltree);
2285                                                         l_precinct->incltree = 00;
2286                                                         tgt_destroy(l_precinct->imsbtree);
2287                                                         l_precinct->imsbtree = 00;
2288                                                         (*l_tcd_code_block_deallocate) (l_precinct);
2289                                                         ++l_precinct;
2290                                                 }
2291
2292                                                 opj_free(l_band->precincts);
2293                                                 l_band->precincts = 00;
2294                                         }
2295                                         ++l_band;
2296                                 } /* for (resno */
2297                                 ++l_res;
2298                         }
2299
2300                         opj_free(l_tile_comp->resolutions);
2301                         l_tile_comp->resolutions = 00;
2302                 }
2303
2304                 if (l_tile_comp->data) {
2305                         opj_free(l_tile_comp->data);
2306                         l_tile_comp->data = 00;
2307                 }
2308                 ++l_tile_comp;
2309         }
2310
2311         opj_free(l_tile->comps);
2312         l_tile->comps = 00;
2313         opj_free(p_tcd->tcd_image->tiles);
2314         p_tcd->tcd_image->tiles = 00;
2315 }
2316
2317
2318 /**
2319  * Allocates memory for a decoding code block.
2320  */
2321 opj_bool tcd_code_block_dec_allocate (opj_tcd_cblk_dec_v2_t * p_code_block)
2322 {
2323         OPJ_UINT32 l_seg_size;
2324
2325         if (! p_code_block->data) {
2326
2327                 p_code_block->data = (OPJ_BYTE*) opj_malloc(8192);
2328                 if (! p_code_block->data) {
2329                         return OPJ_FALSE;
2330                 }
2331                 /*fprintf(stderr, "Allocate 8192 elements of code_block->data\n");*/
2332
2333                 l_seg_size = J2K_DEFAULT_NB_SEGS * sizeof(opj_tcd_seg_t);
2334                 p_code_block->segs = (opj_tcd_seg_t *) opj_malloc(l_seg_size);
2335                 if (! p_code_block->segs) {
2336                         return OPJ_FALSE;
2337                 }
2338                 memset(p_code_block->segs,0,l_seg_size);
2339                 /*fprintf(stderr, "Allocate %d elements of code_block->data\n", J2K_DEFAULT_NB_SEGS * sizeof(opj_tcd_seg_t));*/
2340
2341                 p_code_block->m_current_max_segs = J2K_DEFAULT_NB_SEGS;
2342                 /*fprintf(stderr, "m_current_max_segs of code_block->data = %d\n", p_code_block->m_current_max_segs);*/
2343         }
2344         // TODO
2345         //p_code_block->numsegs = 0;
2346
2347         return OPJ_TRUE;
2348 }
2349
2350 opj_bool tcd_t2_decode (
2351                                         opj_tcd_v2_t *p_tcd,
2352                                         OPJ_BYTE * p_src_data,
2353                                         OPJ_UINT32 * p_data_read,
2354                                         OPJ_UINT32 p_max_src_size,
2355                                         opj_codestream_index_t *p_cstr_index
2356                                         )
2357 {
2358         opj_t2_v2_t * l_t2;
2359
2360         l_t2 = t2_create_v2(p_tcd->image, p_tcd->cp);
2361         if (l_t2 == 00) {
2362                 return OPJ_FALSE;
2363         }
2364
2365         if (! t2_decode_packets_v2(
2366                                         l_t2,
2367                                         p_tcd->tcd_tileno,
2368                                         p_tcd->tcd_image->tiles,
2369                                         p_src_data,
2370                                         p_data_read,
2371                                         p_max_src_size,
2372                                         p_cstr_index)) {
2373                 t2_destroy_v2(l_t2);
2374                 return OPJ_FALSE;
2375         }
2376
2377         t2_destroy_v2(l_t2);
2378
2379         /*---------------CLEAN-------------------*/
2380         return OPJ_TRUE;
2381 }
2382
2383 opj_bool tcd_t1_decode ( opj_tcd_v2_t *p_tcd )
2384 {
2385         OPJ_UINT32 compno;
2386         opj_t1_t * l_t1;
2387         opj_tcd_tile_v2_t * l_tile = p_tcd->tcd_image->tiles;
2388         opj_tcd_tilecomp_v2_t* l_tile_comp = l_tile->comps;
2389         opj_tccp_t * l_tccp = p_tcd->tcp->tccps;
2390
2391
2392         l_t1 = t1_create_v2();
2393         if (l_t1 == 00) {
2394                 return OPJ_FALSE;
2395         }
2396
2397         for (compno = 0; compno < l_tile->numcomps; ++compno) {
2398                 /* The +3 is headroom required by the vectorized DWT */
2399                 t1_decode_cblks_v2(l_t1, l_tile_comp, l_tccp);
2400                 ++l_tile_comp;
2401                 ++l_tccp;
2402         }
2403
2404         t1_destroy_v2(l_t1);
2405
2406         return OPJ_TRUE;
2407 }
2408
2409
2410 opj_bool tcd_dwt_decode ( opj_tcd_v2_t *p_tcd )
2411 {
2412         OPJ_UINT32 compno;
2413         opj_tcd_tile_v2_t * l_tile = p_tcd->tcd_image->tiles;
2414         opj_tcd_tilecomp_v2_t * l_tile_comp = l_tile->comps;
2415         opj_tccp_t * l_tccp = p_tcd->tcp->tccps;
2416         opj_image_comp_t * l_img_comp = p_tcd->image->comps;
2417
2418         for (compno = 0; compno < l_tile->numcomps; compno++) {
2419                 /*
2420                 if (tcd->cp->reduce != 0) {
2421                         tcd->image->comps[compno].resno_decoded =
2422                                 tile->comps[compno].numresolutions - tcd->cp->reduce - 1;
2423                         if (tcd->image->comps[compno].resno_decoded < 0)
2424                         {
2425                                 return false;
2426                         }
2427                 }
2428                 numres2decode = tcd->image->comps[compno].resno_decoded + 1;
2429                 if(numres2decode > 0){
2430                 */
2431
2432                 if (l_tccp->qmfbid == 1) {
2433                         if (! dwt_decode_v2(l_tile_comp, l_img_comp->resno_decoded+1)) {
2434                                 return OPJ_FALSE;
2435                         }
2436                 }
2437                 else {
2438                         if (! dwt_decode_real_v2(l_tile_comp, l_img_comp->resno_decoded+1)) {
2439                                 return OPJ_FALSE;
2440                         }
2441                 }
2442
2443                 ++l_tile_comp;
2444                 ++l_img_comp;
2445                 ++l_tccp;
2446         }
2447
2448         return OPJ_TRUE;
2449 }
2450 opj_bool tcd_mct_decode ( opj_tcd_v2_t *p_tcd )
2451 {
2452         opj_tcd_tile_v2_t * l_tile = p_tcd->tcd_image->tiles;
2453         opj_tcp_v2_t * l_tcp = p_tcd->tcp;
2454         opj_tcd_tilecomp_v2_t * l_tile_comp = l_tile->comps;
2455         OPJ_UINT32 l_samples,i;
2456
2457         if (! l_tcp->mct) {
2458                 return OPJ_TRUE;
2459         }
2460
2461         l_samples = (l_tile_comp->x1 - l_tile_comp->x0) * (l_tile_comp->y1 - l_tile_comp->y0);
2462
2463         if (l_tile->numcomps >= 3 ){
2464                 if (l_tcp->mct == 2) {
2465                         OPJ_BYTE ** l_data;
2466
2467                         if (! l_tcp->m_mct_decoding_matrix) {
2468                                 return OPJ_TRUE;
2469                         }
2470
2471                         l_data = (OPJ_BYTE **) opj_malloc(l_tile->numcomps*sizeof(OPJ_BYTE*));
2472                         if (! l_data) {
2473                                 return OPJ_FALSE;
2474                         }
2475
2476                         for (i=0;i<l_tile->numcomps;++i) {
2477                                 l_data[i] = (OPJ_BYTE*) l_tile_comp->data;
2478                                 ++l_tile_comp;
2479                         }
2480
2481                         if (! mct_decode_custom(// MCT data
2482                                                                         (OPJ_BYTE*) l_tcp->m_mct_decoding_matrix,
2483                                                                         // size of components
2484                                                                         l_samples,
2485                                                                         // components
2486                                                                         l_data,
2487                                                                         // nb of components (i.e. size of pData)
2488                                                                         l_tile->numcomps,
2489                                                                         // tells if the data is signed
2490                                                                         p_tcd->image->comps->sgnd)) {
2491                                 opj_free(l_data);
2492                                 return OPJ_FALSE;
2493                         }
2494
2495                         opj_free(l_data);
2496                 }
2497                 else {
2498                         if (l_tcp->tccps->qmfbid == 1) {
2499                                 mct_decode(     l_tile->comps[0].data,
2500                                                         l_tile->comps[1].data,
2501                                                         l_tile->comps[2].data,
2502                                                         l_samples);
2503                         }
2504                         else {
2505                                 mct_decode_real(        (float*)l_tile->comps[0].data,
2506                                                                         (float*)l_tile->comps[1].data,
2507                                                                         (float*)l_tile->comps[2].data,
2508                                                                         l_samples);
2509                         }
2510                 }
2511         }
2512         else {
2513                 /* FIXME need to use opj_event_msg_v2 function */
2514                 fprintf(stderr,"Number of components (%d) is inconsistent with a MCT. Skip the MCT step.\n",l_tile->numcomps);
2515         }
2516
2517         return OPJ_TRUE;
2518 }
2519
2520
2521 opj_bool tcd_dc_level_shift_decode ( opj_tcd_v2_t *p_tcd )
2522 {
2523         OPJ_UINT32 compno;
2524         opj_tcd_tilecomp_v2_t * l_tile_comp = 00;
2525         opj_tccp_t * l_tccp = 00;
2526         opj_image_comp_t * l_img_comp = 00;
2527         opj_tcd_resolution_v2_t* l_res = 00;
2528         opj_tcp_v2_t * l_tcp = 00;
2529         opj_tcd_tile_v2_t * l_tile;
2530         OPJ_UINT32 l_width,l_height,i,j;
2531         OPJ_INT32 * l_current_ptr;
2532         OPJ_INT32 l_min, l_max;
2533         OPJ_UINT32 l_stride;
2534
2535         l_tile = p_tcd->tcd_image->tiles;
2536         l_tile_comp = l_tile->comps;
2537         l_tcp = p_tcd->tcp;
2538         l_tccp = p_tcd->tcp->tccps;
2539         l_img_comp = p_tcd->image->comps;
2540
2541         for (compno = 0; compno < l_tile->numcomps; compno++) {
2542                 l_res = l_tile_comp->resolutions + l_img_comp->resno_decoded;
2543                 l_width = (l_res->x1 - l_res->x0);
2544                 l_height = (l_res->y1 - l_res->y0);
2545                 l_stride = (l_tile_comp->x1 - l_tile_comp->x0) - l_width;
2546
2547                 if (l_img_comp->sgnd) {
2548                         l_min = -(1 << (l_img_comp->prec - 1));
2549                         l_max = (1 << (l_img_comp->prec - 1)) - 1;
2550                 }
2551                 else {
2552             l_min = 0;
2553                         l_max = (1 << l_img_comp->prec) - 1;
2554                 }
2555
2556                 l_current_ptr = l_tile_comp->data;
2557
2558                 if (l_tccp->qmfbid == 1) {
2559                         for (j=0;j<l_height;++j) {
2560                                 for (i = 0; i < l_width; ++i) {
2561                                         *l_current_ptr = int_clamp(*l_current_ptr + l_tccp->m_dc_level_shift, l_min, l_max);
2562                                         ++l_current_ptr;
2563                                 }
2564                                 l_current_ptr += l_stride;
2565                         }
2566                 }
2567                 else {
2568                         for (j=0;j<l_height;++j) {
2569                                 for (i = 0; i < l_width; ++i) {
2570                                         OPJ_FLOAT32 l_value = *((OPJ_FLOAT32 *) l_current_ptr);
2571                                         *l_current_ptr = int_clamp(lrintf(l_value) + l_tccp->m_dc_level_shift, l_min, l_max); ;
2572                                         ++l_current_ptr;
2573                                 }
2574                                 l_current_ptr += l_stride;
2575                         }
2576                 }
2577
2578                 ++l_img_comp;
2579                 ++l_tccp;
2580                 ++l_tile_comp;
2581         }
2582
2583         return OPJ_TRUE;
2584 }
2585
2586
2587
2588 /**
2589  * Deallocates the encoding data of the given precinct.
2590  */
2591 void tcd_code_block_dec_deallocate (opj_tcd_precinct_v2_t * p_precinct)
2592 {
2593         OPJ_UINT32 cblkno , l_nb_code_blocks;
2594
2595         opj_tcd_cblk_dec_v2_t * l_code_block = p_precinct->cblks.dec;
2596         if (l_code_block) {
2597                 /*fprintf(stderr,"deallocate codeblock:{\n");*/
2598                 /*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);*/
2599                 /*fprintf(stderr,"\t numbps=%d, numlenbits=%d, len=%d, numnewpasses=%d, real_num_segs=%d, m_current_max_segs=%d\n ",
2600                                 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 );*/
2601
2602
2603                 l_nb_code_blocks = p_precinct->block_size / sizeof(opj_tcd_cblk_dec_v2_t);
2604                 /*fprintf(stderr,"nb_code_blocks =%d\t}\n", l_nb_code_blocks);*/
2605
2606                 for (cblkno = 0; cblkno < l_nb_code_blocks; ++cblkno) {
2607
2608                         if (l_code_block->data) {
2609                                 opj_free(l_code_block->data);
2610                                 l_code_block->data = 00;
2611                         }
2612
2613                         if (l_code_block->segs) {
2614                                 opj_free(l_code_block->segs );
2615                                 l_code_block->segs = 00;
2616                         }
2617
2618                         ++l_code_block;
2619                 }
2620
2621                 opj_free(p_precinct->cblks.dec);
2622                 p_precinct->cblks.dec = 00;
2623         }
2624 }
2625
2626