Consolidated some calls to mqc_setcurctx. Cleaned up t1_generate_luts to output the...
[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                                                 for (cblkno = 0; cblkno < prec->cw * prec->ch; cblkno++) {
72                                                         opj_tcd_cblk_t *cblk = &prec->cblks[cblkno];
73                                                         fprintf(fd, "            cblk {\n");
74                                                         fprintf(fd,
75                                                                 "              x0=%d, y0=%d, x1=%d, y1=%d\n",
76                                                                 cblk->x0, cblk->y0, cblk->x1, cblk->y1);
77                                                         fprintf(fd, "            }\n");
78                                                 }
79                                                 fprintf(fd, "          }\n");
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
92 /* ----------------------------------------------------------------------- */
93
94 /**
95 Create a new TCD handle
96 */
97 opj_tcd_t* tcd_create(opj_common_ptr cinfo) {
98         /* create the tcd structure */
99         opj_tcd_t *tcd = (opj_tcd_t*)opj_malloc(sizeof(opj_tcd_t));
100         if(!tcd) return NULL;
101         tcd->cinfo = cinfo;
102         tcd->tcd_image = (opj_tcd_image_t*)opj_malloc(sizeof(opj_tcd_image_t));
103         if(!tcd->tcd_image) {
104                 opj_free(tcd);
105                 return NULL;
106         }
107
108         return tcd;
109 }
110
111 /**
112 Destroy a previously created TCD handle
113 */
114 void tcd_destroy(opj_tcd_t *tcd) {
115         if(tcd) {
116                 opj_free(tcd->tcd_image);
117                 opj_free(tcd);
118         }
119 }
120
121 /* ----------------------------------------------------------------------- */
122
123 void tcd_malloc_encode(opj_tcd_t *tcd, opj_image_t * image, opj_cp_t * cp, int curtileno) {
124         int tileno, compno, resno, bandno, precno, cblkno;
125
126         tcd->image = image;
127         tcd->cp = cp;
128         tcd->tcd_image->tw = cp->tw;
129         tcd->tcd_image->th = cp->th;
130         tcd->tcd_image->tiles = (opj_tcd_tile_t *) opj_malloc(sizeof(opj_tcd_tile_t));
131         
132         for (tileno = 0; tileno < 1; tileno++) {
133                 opj_tcp_t *tcp = &cp->tcps[curtileno];
134                 int j;
135
136                 /* cfr p59 ISO/IEC FDIS15444-1 : 2000 (18 august 2000) */
137                 int p = curtileno % cp->tw;     /* si numerotation matricielle .. */
138                 int q = curtileno / cp->tw;     /* .. coordonnees de la tile (q,p) q pour ligne et p pour colonne */
139
140                 /* opj_tcd_tile_t *tile=&tcd->tcd_image->tiles[tileno]; */
141                 opj_tcd_tile_t *tile = tcd->tcd_image->tiles;
142
143                 /* 4 borders of the tile rescale on the image if necessary */
144                 tile->x0 = int_max(cp->tx0 + p * cp->tdx, image->x0);
145                 tile->y0 = int_max(cp->ty0 + q * cp->tdy, image->y0);
146                 tile->x1 = int_min(cp->tx0 + (p + 1) * cp->tdx, image->x1);
147                 tile->y1 = int_min(cp->ty0 + (q + 1) * cp->tdy, image->y1);
148                 tile->numcomps = image->numcomps;
149                 /* tile->PPT=image->PPT;  */
150
151                 /* Modification of the RATE >> */
152                 for (j = 0; j < tcp->numlayers; j++) {
153                         tcp->rates[j] = tcp->rates[j] ? 
154                                 cp->tp_on ? 
155                                         (((float) (tile->numcomps 
156                                         * (tile->x1 - tile->x0) 
157                                         * (tile->y1 - tile->y0)
158                                         * image->comps[0].prec))
159                                         /(tcp->rates[j] * 8 * image->comps[0].dx * image->comps[0].dy)) - (((tcd->cur_totnum_tp - 1) * 14 )/ tcp->numlayers)
160                                         :
161                                 ((float) (tile->numcomps 
162                                         * (tile->x1 - tile->x0) 
163                                         * (tile->y1 - tile->y0) 
164                                         * image->comps[0].prec))/ 
165                                         (tcp->rates[j] * 8 * image->comps[0].dx * image->comps[0].dy)
166                                         : 0;
167
168                         if (tcp->rates[j]) {
169                                 if (j && tcp->rates[j] < tcp->rates[j - 1] + 10) {
170                                         tcp->rates[j] = tcp->rates[j - 1] + 20;
171                                 } else {
172                                         if (!j && tcp->rates[j] < 30)
173                                                 tcp->rates[j] = 30;
174                                 }
175                                 
176                                 if(j == (tcp->numlayers-1)){
177                                         tcp->rates[j] = tcp->rates[j]- 2;
178                                 }
179                         }
180                 }
181                 /* << Modification of the RATE */
182                 
183                 tile->comps = (opj_tcd_tilecomp_t *) opj_malloc(image->numcomps * sizeof(opj_tcd_tilecomp_t));
184                 for (compno = 0; compno < tile->numcomps; compno++) {
185                         opj_tccp_t *tccp = &tcp->tccps[compno];
186
187                         opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
188
189                         /* border of each tile component (global) */
190                         tilec->x0 = int_ceildiv(tile->x0, image->comps[compno].dx);
191                         tilec->y0 = int_ceildiv(tile->y0, image->comps[compno].dy);
192                         tilec->x1 = int_ceildiv(tile->x1, image->comps[compno].dx);
193                         tilec->y1 = int_ceildiv(tile->y1, image->comps[compno].dy);
194                         
195                         tilec->data = (int *) opj_aligned_malloc((tilec->x1 - tilec->x0) * (tilec->y1 - tilec->y0) * sizeof(int));
196                         tilec->numresolutions = tccp->numresolutions;
197
198                         tilec->resolutions = (opj_tcd_resolution_t *) opj_malloc(tilec->numresolutions * sizeof(opj_tcd_resolution_t));
199                         
200                         for (resno = 0; resno < tilec->numresolutions; resno++) {
201                                 int pdx, pdy;
202                                 int levelno = tilec->numresolutions - 1 - resno;
203                                 int tlprcxstart, tlprcystart, brprcxend, brprcyend;
204                                 int tlcbgxstart, tlcbgystart, brcbgxend, brcbgyend;
205                                 int cbgwidthexpn, cbgheightexpn;
206                                 int cblkwidthexpn, cblkheightexpn;
207
208                                 opj_tcd_resolution_t *res = &tilec->resolutions[resno];
209                                 
210                                 /* border for each resolution level (global) */
211                                 res->x0 = int_ceildivpow2(tilec->x0, levelno);
212                                 res->y0 = int_ceildivpow2(tilec->y0, levelno);
213                                 res->x1 = int_ceildivpow2(tilec->x1, levelno);
214                                 res->y1 = int_ceildivpow2(tilec->y1, levelno);
215                                 
216                                 res->numbands = resno == 0 ? 1 : 3;
217                                 /* p. 35, table A-23, ISO/IEC FDIS154444-1 : 2000 (18 august 2000) */
218                                 if (tccp->csty & J2K_CCP_CSTY_PRT) {
219                                         pdx = tccp->prcw[resno];
220                                         pdy = tccp->prch[resno];
221                                 } else {
222                                         pdx = 15;
223                                         pdy = 15;
224                                 }
225                                 /* p. 64, B.6, ISO/IEC FDIS15444-1 : 2000 (18 august 2000)  */
226                                 tlprcxstart = int_floordivpow2(res->x0, pdx) << pdx;
227                                 tlprcystart = int_floordivpow2(res->y0, pdy) << pdy;
228                                 
229                                 brprcxend = int_ceildivpow2(res->x1, pdx) << pdx;
230                                 brprcyend = int_ceildivpow2(res->y1, pdy) << pdy;
231                                 
232                                 res->pw = (brprcxend - tlprcxstart) >> pdx;
233                                 res->ph = (brprcyend - tlprcystart) >> pdy;
234                                 
235                                 if (resno == 0) {
236                                         tlcbgxstart = tlprcxstart;
237                                         tlcbgystart = tlprcystart;
238                                         brcbgxend = brprcxend;
239                                         brcbgyend = brprcyend;
240                                         cbgwidthexpn = pdx;
241                                         cbgheightexpn = pdy;
242                                 } else {
243                                         tlcbgxstart = int_ceildivpow2(tlprcxstart, 1);
244                                         tlcbgystart = int_ceildivpow2(tlprcystart, 1);
245                                         brcbgxend = int_ceildivpow2(brprcxend, 1);
246                                         brcbgyend = int_ceildivpow2(brprcyend, 1);
247                                         cbgwidthexpn = pdx - 1;
248                                         cbgheightexpn = pdy - 1;
249                                 }
250                                 
251                                 cblkwidthexpn = int_min(tccp->cblkw, cbgwidthexpn);
252                                 cblkheightexpn = int_min(tccp->cblkh, cbgheightexpn);
253                                 
254                                 for (bandno = 0; bandno < res->numbands; bandno++) {
255                                         int x0b, y0b, i;
256                                         int gain, numbps;
257                                         opj_stepsize_t *ss = NULL;
258
259                                         opj_tcd_band_t *band = &res->bands[bandno];
260
261                                         band->bandno = resno == 0 ? 0 : bandno + 1;
262                                         x0b = (band->bandno == 1) || (band->bandno == 3) ? 1 : 0;
263                                         y0b = (band->bandno == 2) || (band->bandno == 3) ? 1 : 0;
264                                         
265                                         if (band->bandno == 0) {
266                                                 /* band border (global) */
267                                                 band->x0 = int_ceildivpow2(tilec->x0, levelno);
268                                                 band->y0 = int_ceildivpow2(tilec->y0, levelno);
269                                                 band->x1 = int_ceildivpow2(tilec->x1, levelno);
270                                                 band->y1 = int_ceildivpow2(tilec->y1, levelno);
271                                         } else {
272                                                 /* band border (global) */
273                                                 band->x0 = int_ceildivpow2(tilec->x0 - (1 << levelno) * x0b, levelno + 1);
274                                                 band->y0 = int_ceildivpow2(tilec->y0 - (1 << levelno) * y0b, levelno + 1);
275                                                 band->x1 = int_ceildivpow2(tilec->x1 - (1 << levelno) * x0b, levelno + 1);
276                                                 band->y1 = int_ceildivpow2(tilec->y1 - (1 << levelno) * y0b, levelno + 1);
277                                         }
278                                         
279                                         ss = &tccp->stepsizes[resno == 0 ? 0 : 3 * (resno - 1) + bandno + 1];
280                                         gain = tccp->qmfbid == 0 ? dwt_getgain_real(band->bandno) : dwt_getgain(band->bandno);                                  
281                                         numbps = image->comps[compno].prec + gain;
282                                         
283                                         band->stepsize = (float)((1.0 + ss->mant / 2048.0) * pow(2.0, numbps - ss->expn));
284                                         band->numbps = ss->expn + tccp->numgbits - 1;   /* WHY -1 ? */
285                                         
286                                         band->precincts = (opj_tcd_precinct_t *) opj_malloc(3 * res->pw * res->ph * sizeof(opj_tcd_precinct_t));
287                                         
288                                         for (i = 0; i < res->pw * res->ph * 3; i++) {
289                                                 band->precincts[i].imsbtree = NULL;
290                                                 band->precincts[i].incltree = NULL;
291                                         }
292                                         
293                                         for (precno = 0; precno < res->pw * res->ph; precno++) {
294                                                 int tlcblkxstart, tlcblkystart, brcblkxend, brcblkyend;
295
296                                                 int cbgxstart = tlcbgxstart + (precno % res->pw) * (1 << cbgwidthexpn);
297                                                 int cbgystart = tlcbgystart + (precno / res->pw) * (1 << cbgheightexpn);
298                                                 int cbgxend = cbgxstart + (1 << cbgwidthexpn);
299                                                 int cbgyend = cbgystart + (1 << cbgheightexpn);
300
301                                                 opj_tcd_precinct_t *prc = &band->precincts[precno];
302
303                                                 /* precinct size (global) */
304                                                 prc->x0 = int_max(cbgxstart, band->x0);
305                                                 prc->y0 = int_max(cbgystart, band->y0);
306                                                 prc->x1 = int_min(cbgxend, band->x1);
307                                                 prc->y1 = int_min(cbgyend, band->y1);
308
309                                                 tlcblkxstart = int_floordivpow2(prc->x0, cblkwidthexpn) << cblkwidthexpn;
310                                                 tlcblkystart = int_floordivpow2(prc->y0, cblkheightexpn) << cblkheightexpn;
311                                                 brcblkxend = int_ceildivpow2(prc->x1, cblkwidthexpn) << cblkwidthexpn;
312                                                 brcblkyend = int_ceildivpow2(prc->y1, cblkheightexpn) << cblkheightexpn;
313                                                 prc->cw = (brcblkxend - tlcblkxstart) >> cblkwidthexpn;
314                                                 prc->ch = (brcblkyend - tlcblkystart) >> cblkheightexpn;
315
316                                                 prc->cblks = (opj_tcd_cblk_t *) opj_malloc((prc->cw * prc->ch) * sizeof(opj_tcd_cblk_t));
317                                                 prc->incltree = tgt_create(prc->cw, prc->ch);
318                                                 prc->imsbtree = tgt_create(prc->cw, prc->ch);
319                                                 
320                                                 for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
321                                                         int cblkxstart = tlcblkxstart + (cblkno % prc->cw) * (1 << cblkwidthexpn);
322                                                         int cblkystart = tlcblkystart + (cblkno / prc->cw) * (1 << cblkheightexpn);
323                                                         int cblkxend = cblkxstart + (1 << cblkwidthexpn);
324                                                         int cblkyend = cblkystart + (1 << cblkheightexpn);
325                                                         
326                                                         opj_tcd_cblk_t *cblk = &prc->cblks[cblkno];
327
328                                                         /* code-block size (global) */
329                                                         cblk->x0 = int_max(cblkxstart, prc->x0);
330                                                         cblk->y0 = int_max(cblkystart, prc->y0);
331                                                         cblk->x1 = int_min(cblkxend, prc->x1);
332                                                         cblk->y1 = int_min(cblkyend, prc->y1);
333                                                 }
334                                         }
335                                 }
336                         }
337                 }
338         }
339         
340         /* tcd_dump(stdout, tcd, &tcd->tcd_image); */
341 }
342
343 void tcd_free_encode(opj_tcd_t *tcd) {
344         int tileno, compno, resno, bandno, precno;
345
346         for (tileno = 0; tileno < 1; tileno++) {
347                 opj_tcd_tile_t *tile = tcd->tcd_image->tiles;
348
349                 for (compno = 0; compno < tile->numcomps; compno++) {
350                         opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
351
352                         for (resno = 0; resno < tilec->numresolutions; resno++) {
353                                 opj_tcd_resolution_t *res = &tilec->resolutions[resno];
354
355                                 for (bandno = 0; bandno < res->numbands; bandno++) {
356                                         opj_tcd_band_t *band = &res->bands[bandno];
357
358                                         for (precno = 0; precno < res->pw * res->ph; precno++) {
359                                                 opj_tcd_precinct_t *prc = &band->precincts[precno];
360
361                                                 if (prc->incltree != NULL) {
362                                                         tgt_destroy(prc->incltree);
363                                                         prc->incltree = NULL;
364                                                 }
365                                                 if (prc->imsbtree != NULL) {
366                                                         tgt_destroy(prc->imsbtree);     
367                                                         prc->imsbtree = NULL;
368                                                 }
369                                                 opj_free(prc->cblks);
370                                                 prc->cblks = NULL;
371                                         } /* for (precno */
372                                         opj_free(band->precincts);
373                                         band->precincts = NULL;
374                                 } /* for (bandno */
375                         } /* for (resno */
376                         opj_free(tilec->resolutions);
377                         tilec->resolutions = NULL;
378                 } /* for (compno */
379                 opj_free(tile->comps);
380                 tile->comps = NULL;
381         } /* for (tileno */
382         opj_free(tcd->tcd_image->tiles);
383         tcd->tcd_image->tiles = NULL;
384 }
385
386 void tcd_init_encode(opj_tcd_t *tcd, opj_image_t * image, opj_cp_t * cp, int curtileno) {
387         int tileno, compno, resno, bandno, precno, cblkno;
388
389         for (tileno = 0; tileno < 1; tileno++) {
390                 opj_tcp_t *tcp = &cp->tcps[curtileno];
391                 int j;
392                 /* cfr p59 ISO/IEC FDIS15444-1 : 2000 (18 august 2000) */
393                 int p = curtileno % cp->tw;
394                 int q = curtileno / cp->tw;
395
396                 opj_tcd_tile_t *tile = tcd->tcd_image->tiles;
397                 
398                 /* 4 borders of the tile rescale on the image if necessary */
399                 tile->x0 = int_max(cp->tx0 + p * cp->tdx, image->x0);
400                 tile->y0 = int_max(cp->ty0 + q * cp->tdy, image->y0);
401                 tile->x1 = int_min(cp->tx0 + (p + 1) * cp->tdx, image->x1);
402                 tile->y1 = int_min(cp->ty0 + (q + 1) * cp->tdy, image->y1);
403                 
404                 tile->numcomps = image->numcomps;
405                 /* tile->PPT=image->PPT; */
406
407                 /* Modification of the RATE >> */
408                 for (j = 0; j < tcp->numlayers; j++) {
409                         tcp->rates[j] = tcp->rates[j] ? 
410                                                 ((float) (tile->numcomps 
411                                                                 * (tile->x1 - tile->x0) 
412                                                                 * (tile->y1 - tile->y0) 
413                                                                 * image->comps[0].prec))/ 
414                                                 (tcp->rates[j] * 8 * image->comps[0].dx * image->comps[0].dy) 
415                                                 : 0;
416
417                         if (tcp->rates[j]) {
418                                 if (j && tcp->rates[j] < tcp->rates[j - 1] + 10) {
419                                         tcp->rates[j] = tcp->rates[j - 1] + 20;
420                                 } else {
421                                         if (!j && tcp->rates[j] < 30)
422                                                 tcp->rates[j] = 30;
423                                 }
424                         }
425                 }
426                 /* << Modification of the RATE */
427
428                 /* tile->comps=(opj_tcd_tilecomp_t*)opj_realloc(tile->comps,image->numcomps*sizeof(opj_tcd_tilecomp_t)); */
429                 for (compno = 0; compno < tile->numcomps; compno++) {
430                         opj_tccp_t *tccp = &tcp->tccps[compno];
431                         
432                         opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
433
434                         /* border of each tile component (global) */
435                         tilec->x0 = int_ceildiv(tile->x0, image->comps[compno].dx);
436                         tilec->y0 = int_ceildiv(tile->y0, image->comps[compno].dy);
437                         tilec->x1 = int_ceildiv(tile->x1, image->comps[compno].dx);
438                         tilec->y1 = int_ceildiv(tile->y1, image->comps[compno].dy);
439                         
440                         tilec->data = (int *) opj_aligned_malloc((tilec->x1 - tilec->x0) * (tilec->y1 - tilec->y0) * sizeof(int));
441                         tilec->numresolutions = tccp->numresolutions;
442                         /* tilec->resolutions=(opj_tcd_resolution_t*)opj_realloc(tilec->resolutions,tilec->numresolutions*sizeof(opj_tcd_resolution_t)); */
443                         for (resno = 0; resno < tilec->numresolutions; resno++) {
444                                 int pdx, pdy;
445
446                                 int levelno = tilec->numresolutions - 1 - resno;
447                                 int tlprcxstart, tlprcystart, brprcxend, brprcyend;
448                                 int tlcbgxstart, tlcbgystart, brcbgxend, brcbgyend;
449                                 int cbgwidthexpn, cbgheightexpn;
450                                 int cblkwidthexpn, cblkheightexpn;
451                                 
452                                 opj_tcd_resolution_t *res = &tilec->resolutions[resno];
453
454                                 /* border for each resolution level (global) */
455                                 res->x0 = int_ceildivpow2(tilec->x0, levelno);
456                                 res->y0 = int_ceildivpow2(tilec->y0, levelno);
457                                 res->x1 = int_ceildivpow2(tilec->x1, levelno);
458                                 res->y1 = int_ceildivpow2(tilec->y1, levelno);  
459                                 res->numbands = resno == 0 ? 1 : 3;
460
461                                 /* p. 35, table A-23, ISO/IEC FDIS154444-1 : 2000 (18 august 2000) */
462                                 if (tccp->csty & J2K_CCP_CSTY_PRT) {
463                                         pdx = tccp->prcw[resno];
464                                         pdy = tccp->prch[resno];
465                                 } else {
466                                         pdx = 15;
467                                         pdy = 15;
468                                 }
469                                 /* p. 64, B.6, ISO/IEC FDIS15444-1 : 2000 (18 august 2000)  */
470                                 tlprcxstart = int_floordivpow2(res->x0, pdx) << pdx;
471                                 tlprcystart = int_floordivpow2(res->y0, pdy) << pdy;
472                                 brprcxend = int_ceildivpow2(res->x1, pdx) << pdx;
473                                 brprcyend = int_ceildivpow2(res->y1, pdy) << pdy;
474                                 
475                                 res->pw = (brprcxend - tlprcxstart) >> pdx;
476                                 res->ph = (brprcyend - tlprcystart) >> pdy;
477                                 
478                                 if (resno == 0) {
479                                         tlcbgxstart = tlprcxstart;
480                                         tlcbgystart = tlprcystart;
481                                         brcbgxend = brprcxend;
482                                         brcbgyend = brprcyend;
483                                         cbgwidthexpn = pdx;
484                                         cbgheightexpn = pdy;
485                                 } else {
486                                         tlcbgxstart = int_ceildivpow2(tlprcxstart, 1);
487                                         tlcbgystart = int_ceildivpow2(tlprcystart, 1);
488                                         brcbgxend = int_ceildivpow2(brprcxend, 1);
489                                         brcbgyend = int_ceildivpow2(brprcyend, 1);
490                                         cbgwidthexpn = pdx - 1;
491                                         cbgheightexpn = pdy - 1;
492                                 }
493                                 
494                                 cblkwidthexpn = int_min(tccp->cblkw, cbgwidthexpn);
495                                 cblkheightexpn = int_min(tccp->cblkh, cbgheightexpn);
496                                 
497                                 for (bandno = 0; bandno < res->numbands; bandno++) {
498                                         int x0b, y0b;
499                                         int gain, numbps;
500                                         opj_stepsize_t *ss = NULL;
501
502                                         opj_tcd_band_t *band = &res->bands[bandno];
503
504                                         band->bandno = resno == 0 ? 0 : bandno + 1;
505                                         x0b = (band->bandno == 1) || (band->bandno == 3) ? 1 : 0;
506                                         y0b = (band->bandno == 2) || (band->bandno == 3) ? 1 : 0;
507                                         
508                                         if (band->bandno == 0) {
509                                                 /* band border */
510                                                 band->x0 = int_ceildivpow2(tilec->x0, levelno);
511                                                 band->y0 = int_ceildivpow2(tilec->y0, levelno);
512                                                 band->x1 = int_ceildivpow2(tilec->x1, levelno);
513                                                 band->y1 = int_ceildivpow2(tilec->y1, levelno);
514                                         } else {
515                                                 band->x0 = int_ceildivpow2(tilec->x0 - (1 << levelno) * x0b, levelno + 1);
516                                                 band->y0 = int_ceildivpow2(tilec->y0 - (1 << levelno) * y0b, levelno + 1);
517                                                 band->x1 = int_ceildivpow2(tilec->x1 - (1 << levelno) * x0b, levelno + 1);
518                                                 band->y1 = int_ceildivpow2(tilec->y1 - (1 << levelno) * y0b, levelno + 1);
519                                         }
520                                         
521                                         ss = &tccp->stepsizes[resno == 0 ? 0 : 3 * (resno - 1) + bandno + 1];
522                                         gain = tccp->qmfbid == 0 ? dwt_getgain_real(band->bandno) : dwt_getgain(band->bandno);
523                                         numbps = image->comps[compno].prec + gain;
524                                         band->stepsize = (float)((1.0 + ss->mant / 2048.0) * pow(2.0, numbps - ss->expn));
525                                         band->numbps = ss->expn + tccp->numgbits - 1;   /* WHY -1 ? */
526                                         
527                                         for (precno = 0; precno < res->pw * res->ph; precno++) {
528                                                 int tlcblkxstart, tlcblkystart, brcblkxend, brcblkyend;
529
530                                                 int cbgxstart = tlcbgxstart + (precno % res->pw) * (1 << cbgwidthexpn);
531                                                 int cbgystart = tlcbgystart + (precno / res->pw) * (1 << cbgheightexpn);
532                                                 int cbgxend = cbgxstart + (1 << cbgwidthexpn);
533                                                 int cbgyend = cbgystart + (1 << cbgheightexpn);
534                                                 
535                                                 opj_tcd_precinct_t *prc = &band->precincts[precno];
536
537                                                 /* precinct size (global) */
538                                                 prc->x0 = int_max(cbgxstart, band->x0);
539                                                 prc->y0 = int_max(cbgystart, band->y0);
540                                                 prc->x1 = int_min(cbgxend, band->x1);
541                                                 prc->y1 = int_min(cbgyend, band->y1);
542
543                                                 tlcblkxstart = int_floordivpow2(prc->x0, cblkwidthexpn) << cblkwidthexpn;
544                                                 tlcblkystart = int_floordivpow2(prc->y0, cblkheightexpn) << cblkheightexpn;
545                                                 brcblkxend = int_ceildivpow2(prc->x1, cblkwidthexpn) << cblkwidthexpn;
546                                                 brcblkyend = int_ceildivpow2(prc->y1, cblkheightexpn) << cblkheightexpn;
547                                                 prc->cw = (brcblkxend - tlcblkxstart) >> cblkwidthexpn;
548                                                 prc->ch = (brcblkyend - tlcblkystart) >> cblkheightexpn;
549
550                                                 opj_free(prc->cblks);
551                                                 prc->cblks = (opj_tcd_cblk_t *) opj_malloc(prc->cw * prc->ch * sizeof(opj_tcd_cblk_t));
552
553                                                 if (prc->incltree != NULL) {
554                                                         tgt_destroy(prc->incltree);
555                                                 }
556                                                 if (prc->imsbtree != NULL) {
557                                                         tgt_destroy(prc->imsbtree);
558                                                 }
559                                                 
560                                                 prc->incltree = tgt_create(prc->cw, prc->ch);
561                                                 prc->imsbtree = tgt_create(prc->cw, prc->ch);
562
563                                                 for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
564                                                         int cblkxstart = tlcblkxstart + (cblkno % prc->cw) * (1 << cblkwidthexpn);
565                                                         int cblkystart = tlcblkystart + (cblkno / prc->cw) * (1 << cblkheightexpn);
566                                                         int cblkxend = cblkxstart + (1 << cblkwidthexpn);
567                                                         int cblkyend = cblkystart + (1 << cblkheightexpn);
568
569                                                         opj_tcd_cblk_t *cblk = &prc->cblks[cblkno];
570                                                         
571                                                         /* code-block size (global) */
572                                                         cblk->x0 = int_max(cblkxstart, prc->x0);
573                                                         cblk->y0 = int_max(cblkystart, prc->y0);
574                                                         cblk->x1 = int_min(cblkxend, prc->x1);
575                                                         cblk->y1 = int_min(cblkyend, prc->y1);
576                                                 }
577                                         } /* precno */
578                                 } /* bandno */
579                         } /* resno */
580                 } /* compno */
581         } /* tileno */
582
583         /* tcd_dump(stdout, tcd, &tcd->tcd_image); */
584 }
585
586 void tcd_malloc_decode(opj_tcd_t *tcd, opj_image_t * image, opj_cp_t * cp) {
587         int i, j, tileno, p, q;
588         unsigned int x0 = 0, y0 = 0, x1 = 0, y1 = 0, w, h;
589
590         tcd->image = image;
591         tcd->tcd_image->tw = cp->tw;
592         tcd->tcd_image->th = cp->th;
593         tcd->tcd_image->tiles = (opj_tcd_tile_t *) opj_malloc(cp->tw * cp->th * sizeof(opj_tcd_tile_t));
594
595         /* 
596         Allocate place to store the decoded data = final image
597         Place limited by the tile really present in the codestream 
598         */
599
600         for (j = 0; j < cp->tileno_size; j++) {
601                 opj_tcd_tile_t *tile;
602                 
603                 tileno = cp->tileno[j];
604                 
605                 tile = &(tcd->tcd_image->tiles[cp->tileno[tileno]]);
606                 
607                 tile->numcomps = image->numcomps;
608                 tile->comps = (opj_tcd_tilecomp_t *) opj_malloc(image->numcomps * sizeof(opj_tcd_tilecomp_t));
609         }
610
611         
612         for (i = 0; i < image->numcomps; i++) {
613                 for (j = 0; j < cp->tileno_size; j++) {
614                         opj_tcd_tile_t *tile;
615                         opj_tcd_tilecomp_t *tilec;
616                         
617                         /* cfr p59 ISO/IEC FDIS15444-1 : 2000 (18 august 2000) */
618                         
619                         tileno = cp->tileno[j];
620                         
621                         tile = &(tcd->tcd_image->tiles[cp->tileno[tileno]]);
622                         tilec = &tile->comps[i];
623                         
624                         p = tileno % cp->tw;    /* si numerotation matricielle .. */
625                         q = tileno / cp->tw;    /* .. coordonnees de la tile (q,p) q pour ligne et p pour colonne */
626                         
627                         /* 4 borders of the tile rescale on the image if necessary */
628                         tile->x0 = int_max(cp->tx0 + p * cp->tdx, image->x0);
629                         tile->y0 = int_max(cp->ty0 + q * cp->tdy, image->y0);
630                         tile->x1 = int_min(cp->tx0 + (p + 1) * cp->tdx, image->x1);
631                         tile->y1 = int_min(cp->ty0 + (q + 1) * cp->tdy, image->y1);
632
633                         tilec->x0 = int_ceildiv(tile->x0, image->comps[i].dx);
634                         tilec->y0 = int_ceildiv(tile->y0, image->comps[i].dy);
635                         tilec->x1 = int_ceildiv(tile->x1, image->comps[i].dx);
636                         tilec->y1 = int_ceildiv(tile->y1, image->comps[i].dy);
637
638                         x0 = j == 0 ? tilec->x0 : int_min(x0, (unsigned int) tilec->x0);
639                         y0 = j == 0 ? tilec->y0 : int_min(y0,   (unsigned int) tilec->x0);
640                         x1 = j == 0 ? tilec->x1 : int_max(x1,   (unsigned int) tilec->x1);
641                         y1 = j == 0 ? tilec->y1 : int_max(y1,   (unsigned int) tilec->y1);
642                 }
643                 
644                 w = x1 - x0;
645                 h = y1 - y0;
646                 
647                 image->comps[i].data = (int *) opj_malloc(w * h * sizeof(int));
648                 image->comps[i].w = w;
649                 image->comps[i].h = h;
650                 image->comps[i].x0 = x0;
651                 image->comps[i].y0 = y0;
652         }
653
654 }
655
656 void tcd_malloc_decode_tile(opj_tcd_t *tcd, opj_image_t * image, opj_cp_t * cp, int tileno) {
657         int compno, resno, bandno, precno, cblkno;
658         unsigned int x0 = 0, y0 = 0, x1 = 0, y1 = 0;
659         opj_tcp_t *tcp;
660         opj_tcd_tile_t *tile;
661
662         tcd->cp = cp;
663         
664         tcp = &(cp->tcps[cp->tileno[tileno]]);
665         tile = &(tcd->tcd_image->tiles[cp->tileno[tileno]]);
666         
667         tileno = cp->tileno[tileno];
668         
669         for (compno = 0; compno < tile->numcomps; compno++) {
670                 opj_tccp_t *tccp = &tcp->tccps[compno];
671                 opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
672                 
673                 /* border of each tile component (global) */
674                 tilec->x0 = int_ceildiv(tile->x0, image->comps[compno].dx);
675                 tilec->y0 = int_ceildiv(tile->y0, image->comps[compno].dy);
676                 tilec->x1 = int_ceildiv(tile->x1, image->comps[compno].dx);
677                 tilec->y1 = int_ceildiv(tile->y1, image->comps[compno].dy);
678                 
679                 tilec->data = (int*) opj_aligned_malloc((tilec->x1 - tilec->x0) * (tilec->y1 - tilec->y0) * sizeof(int));
680                 tilec->numresolutions = tccp->numresolutions;
681                 tilec->resolutions = (opj_tcd_resolution_t *) opj_malloc(tilec->numresolutions * sizeof(opj_tcd_resolution_t));
682                 
683                 for (resno = 0; resno < tilec->numresolutions; resno++) {
684                         int pdx, pdy;
685                         int levelno = tilec->numresolutions - 1 - resno;
686                         int tlprcxstart, tlprcystart, brprcxend, brprcyend;
687                         int tlcbgxstart, tlcbgystart, brcbgxend, brcbgyend;
688                         int cbgwidthexpn, cbgheightexpn;
689                         int cblkwidthexpn, cblkheightexpn;
690                         
691                         opj_tcd_resolution_t *res = &tilec->resolutions[resno];
692                         
693                         /* border for each resolution level (global) */
694                         res->x0 = int_ceildivpow2(tilec->x0, levelno);
695                         res->y0 = int_ceildivpow2(tilec->y0, levelno);
696                         res->x1 = int_ceildivpow2(tilec->x1, levelno);
697                         res->y1 = int_ceildivpow2(tilec->y1, levelno);
698                         res->numbands = resno == 0 ? 1 : 3;
699                         
700                         /* p. 35, table A-23, ISO/IEC FDIS154444-1 : 2000 (18 august 2000) */
701                         if (tccp->csty & J2K_CCP_CSTY_PRT) {
702                                 pdx = tccp->prcw[resno];
703                                 pdy = tccp->prch[resno];
704                         } else {
705                                 pdx = 15;
706                                 pdy = 15;
707                         }
708                         
709                         /* p. 64, B.6, ISO/IEC FDIS15444-1 : 2000 (18 august 2000)  */
710                         tlprcxstart = int_floordivpow2(res->x0, pdx) << pdx;
711                         tlprcystart = int_floordivpow2(res->y0, pdy) << pdy;
712                         brprcxend = int_ceildivpow2(res->x1, pdx) << pdx;
713                         brprcyend = int_ceildivpow2(res->y1, pdy) << pdy;
714                         
715                         res->pw = (res->x0 == res->x1) ? 0 : ((brprcxend - tlprcxstart) >> pdx);
716                         res->ph = (res->y0 == res->y1) ? 0 : ((brprcyend - tlprcystart) >> pdy);
717                         
718                         if (resno == 0) {
719                                 tlcbgxstart = tlprcxstart;
720                                 tlcbgystart = tlprcystart;
721                                 brcbgxend = brprcxend;
722                                 brcbgyend = brprcyend;
723                                 cbgwidthexpn = pdx;
724                                 cbgheightexpn = pdy;
725                         } else {
726                                 tlcbgxstart = int_ceildivpow2(tlprcxstart, 1);
727                                 tlcbgystart = int_ceildivpow2(tlprcystart, 1);
728                                 brcbgxend = int_ceildivpow2(brprcxend, 1);
729                                 brcbgyend = int_ceildivpow2(brprcyend, 1);
730                                 cbgwidthexpn = pdx - 1;
731                                 cbgheightexpn = pdy - 1;
732                         }
733                         
734                         cblkwidthexpn = int_min(tccp->cblkw, cbgwidthexpn);
735                         cblkheightexpn = int_min(tccp->cblkh, cbgheightexpn);
736                         
737                         for (bandno = 0; bandno < res->numbands; bandno++) {
738                                 int x0b, y0b;
739                                 int gain, numbps;
740                                 opj_stepsize_t *ss = NULL;
741                                 
742                                 opj_tcd_band_t *band = &res->bands[bandno];
743                                 band->bandno = resno == 0 ? 0 : bandno + 1;
744                                 x0b = (band->bandno == 1) || (band->bandno == 3) ? 1 : 0;
745                                 y0b = (band->bandno == 2) || (band->bandno == 3) ? 1 : 0;
746                                 
747                                 if (band->bandno == 0) {
748                                         /* band border (global) */
749                                         band->x0 = int_ceildivpow2(tilec->x0, levelno);
750                                         band->y0 = int_ceildivpow2(tilec->y0, levelno);
751                                         band->x1 = int_ceildivpow2(tilec->x1, levelno);
752                                         band->y1 = int_ceildivpow2(tilec->y1, levelno);
753                                 } else {
754                                         /* band border (global) */
755                                         band->x0 = int_ceildivpow2(tilec->x0 - (1 << levelno) * x0b, levelno + 1);
756                                         band->y0 = int_ceildivpow2(tilec->y0 - (1 << levelno) * y0b, levelno + 1);
757                                         band->x1 = int_ceildivpow2(tilec->x1 - (1 << levelno) * x0b, levelno + 1);
758                                         band->y1 = int_ceildivpow2(tilec->y1 - (1 << levelno) * y0b, levelno + 1);
759                                 }
760                                 
761                                 ss = &tccp->stepsizes[resno == 0 ? 0 : 3 * (resno - 1) + bandno + 1];
762                                 gain = tccp->qmfbid == 0 ? dwt_getgain_real(band->bandno) : dwt_getgain(band->bandno);
763                                 numbps = image->comps[compno].prec + gain;
764                                 band->stepsize = (float)((1.0 + ss->mant / 2048.0) * pow(2.0, numbps - ss->expn));
765                                 band->numbps = ss->expn + tccp->numgbits - 1;   /* WHY -1 ? */
766                                 
767                                 band->precincts = (opj_tcd_precinct_t *) opj_malloc(res->pw * res->ph * sizeof(opj_tcd_precinct_t));
768                                 
769                                 for (precno = 0; precno < res->pw * res->ph; precno++) {
770                                         int tlcblkxstart, tlcblkystart, brcblkxend, brcblkyend;
771                                         int cbgxstart = tlcbgxstart + (precno % res->pw) * (1 << cbgwidthexpn);
772                                         int cbgystart = tlcbgystart + (precno / res->pw) * (1 << cbgheightexpn);
773                                         int cbgxend = cbgxstart + (1 << cbgwidthexpn);
774                                         int cbgyend = cbgystart + (1 << cbgheightexpn);
775                                         
776                                         opj_tcd_precinct_t *prc = &band->precincts[precno];
777                                         /* precinct size (global) */
778                                         prc->x0 = int_max(cbgxstart, band->x0);
779                                         prc->y0 = int_max(cbgystart, band->y0);
780                                         prc->x1 = int_min(cbgxend, band->x1);
781                                         prc->y1 = int_min(cbgyend, band->y1);
782                                         
783                                         tlcblkxstart = int_floordivpow2(prc->x0, cblkwidthexpn) << cblkwidthexpn;
784                                         tlcblkystart = int_floordivpow2(prc->y0, cblkheightexpn) << cblkheightexpn;
785                                         brcblkxend = int_ceildivpow2(prc->x1, cblkwidthexpn) << cblkwidthexpn;
786                                         brcblkyend = int_ceildivpow2(prc->y1, cblkheightexpn) << cblkheightexpn;
787                                         prc->cw = (brcblkxend - tlcblkxstart) >> cblkwidthexpn;
788                                         prc->ch = (brcblkyend - tlcblkystart) >> cblkheightexpn;
789                                         
790                                         prc->cblks = (opj_tcd_cblk_t *) opj_malloc(prc->cw * prc->ch * sizeof(opj_tcd_cblk_t));
791                                         
792                                         prc->incltree = tgt_create(prc->cw, prc->ch);
793                                         prc->imsbtree = tgt_create(prc->cw, prc->ch);
794                                         
795                                         for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
796                                                 int cblkxstart = tlcblkxstart + (cblkno % prc->cw) * (1 << cblkwidthexpn);
797                                                 int cblkystart = tlcblkystart + (cblkno / prc->cw) * (1 << cblkheightexpn);
798                                                 int cblkxend = cblkxstart + (1 << cblkwidthexpn);
799                                                 int cblkyend = cblkystart + (1 << cblkheightexpn);                                      
800                                                 
801                                                 /* code-block size (global) */
802                                                 opj_tcd_cblk_t *cblk = &prc->cblks[cblkno];
803                                                 cblk->x0 = int_max(cblkxstart, prc->x0);
804                                                 cblk->y0 = int_max(cblkystart, prc->y0);
805                                                 cblk->x1 = int_min(cblkxend, prc->x1);
806                                                 cblk->y1 = int_min(cblkyend, prc->y1);
807                                         }
808                                 } /* precno */
809                         } /* bandno */
810                 } /* resno */
811         } /* compno */
812         
813         /* tcd_dump(stdout, tcd, &tcd->tcd_image); */
814 }
815
816 void tcd_makelayer_fixed(opj_tcd_t *tcd, int layno, int final) {
817         int compno, resno, bandno, precno, cblkno;
818         int value;                      /*, matrice[tcd_tcp->numlayers][tcd_tile->comps[0].numresolutions][3]; */
819         int matrice[10][10][3];
820         int i, j, k;
821
822         opj_cp_t *cp = tcd->cp;
823         opj_tcd_tile_t *tcd_tile = tcd->tcd_tile;
824         opj_tcp_t *tcd_tcp = tcd->tcp;
825
826         /*matrice=(int*)opj_malloc(tcd_tcp->numlayers*tcd_tile->comps[0].numresolutions*3*sizeof(int)); */
827         
828         for (compno = 0; compno < tcd_tile->numcomps; compno++) {
829                 opj_tcd_tilecomp_t *tilec = &tcd_tile->comps[compno];
830                 for (i = 0; i < tcd_tcp->numlayers; i++) {
831                         for (j = 0; j < tilec->numresolutions; j++) {
832                                 for (k = 0; k < 3; k++) {
833                                         matrice[i][j][k] =
834                                                 (int) (cp->matrice[i * tilec->numresolutions * 3 + j * 3 + k] 
835                                                 * (float) (tcd->image->comps[compno].prec / 16.0));
836                                 }
837                         }
838                 }
839         
840                 for (resno = 0; resno < tilec->numresolutions; resno++) {
841                         opj_tcd_resolution_t *res = &tilec->resolutions[resno];
842                         for (bandno = 0; bandno < res->numbands; bandno++) {
843                                 opj_tcd_band_t *band = &res->bands[bandno];
844                                 for (precno = 0; precno < res->pw * res->ph; precno++) {
845                                         opj_tcd_precinct_t *prc = &band->precincts[precno];
846                                         for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
847                                                 opj_tcd_cblk_t *cblk = &prc->cblks[cblkno];
848                                                 opj_tcd_layer_t *layer = &cblk->layers[layno];
849                                                 int n;
850                                                 int imsb = tcd->image->comps[compno].prec - cblk->numbps;       /* number of bit-plan equal to zero */
851                                                 /* Correction of the matrix of coefficient to include the IMSB information */
852                                                 if (layno == 0) {
853                                                         value = matrice[layno][resno][bandno];
854                                                         if (imsb >= value) {
855                                                                 value = 0;
856                                                         } else {
857                                                                 value -= imsb;
858                                                         }
859                                                 } else {
860                                                         value = matrice[layno][resno][bandno] - matrice[layno - 1][resno][bandno];
861                                                         if (imsb >= matrice[layno - 1][resno][bandno]) {
862                                                                 value -= (imsb - matrice[layno - 1][resno][bandno]);
863                                                                 if (value < 0) {
864                                                                         value = 0;
865                                                                 }
866                                                         }
867                                                 }
868                                                 
869                                                 if (layno == 0) {
870                                                         cblk->numpassesinlayers = 0;
871                                                 }
872                                                 
873                                                 n = cblk->numpassesinlayers;
874                                                 if (cblk->numpassesinlayers == 0) {
875                                                         if (value != 0) {
876                                                                 n = 3 * value - 2 + cblk->numpassesinlayers;
877                                                         } else {
878                                                                 n = cblk->numpassesinlayers;
879                                                         }
880                                                 } else {
881                                                         n = 3 * value + cblk->numpassesinlayers;
882                                                 }
883                                                 
884                                                 layer->numpasses = n - cblk->numpassesinlayers;
885                                                 
886                                                 if (!layer->numpasses)
887                                                         continue;
888                                                 
889                                                 if (cblk->numpassesinlayers == 0) {
890                                                         layer->len = cblk->passes[n - 1].rate;
891                                                         layer->data = cblk->data;
892                                                 } else {
893                                                         layer->len = cblk->passes[n - 1].rate - cblk->passes[cblk->numpassesinlayers - 1].rate;
894                                                         layer->data = cblk->data + cblk->passes[cblk->numpassesinlayers - 1].rate;
895                                                 }
896                                                 if (final)
897                                                         cblk->numpassesinlayers = n;
898                                         }
899                                 }
900                         }
901                 }
902         }
903 }
904
905 void tcd_rateallocate_fixed(opj_tcd_t *tcd) {
906         int layno;
907         for (layno = 0; layno < tcd->tcp->numlayers; layno++) {
908                 tcd_makelayer_fixed(tcd, layno, 1);
909         }
910 }
911
912 void tcd_makelayer(opj_tcd_t *tcd, int layno, double thresh, int final) {
913         int compno, resno, bandno, precno, cblkno, passno;
914         
915         opj_tcd_tile_t *tcd_tile = tcd->tcd_tile;
916
917         tcd_tile->distolayer[layno] = 0;        /* fixed_quality */
918         
919         for (compno = 0; compno < tcd_tile->numcomps; compno++) {
920                 opj_tcd_tilecomp_t *tilec = &tcd_tile->comps[compno];
921                 for (resno = 0; resno < tilec->numresolutions; resno++) {
922                         opj_tcd_resolution_t *res = &tilec->resolutions[resno];
923                         for (bandno = 0; bandno < res->numbands; bandno++) {
924                                 opj_tcd_band_t *band = &res->bands[bandno];
925                                 for (precno = 0; precno < res->pw * res->ph; precno++) {
926                                         opj_tcd_precinct_t *prc = &band->precincts[precno];
927                                         for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
928                                                 opj_tcd_cblk_t *cblk = &prc->cblks[cblkno];
929                                                 opj_tcd_layer_t *layer = &cblk->layers[layno];
930                                                 
931                                                 int n;
932                                                 if (layno == 0) {
933                                                         cblk->numpassesinlayers = 0;
934                                                 }
935                                                 n = cblk->numpassesinlayers;
936                                                 for (passno = cblk->numpassesinlayers; passno < cblk->totalpasses; passno++) {
937                                                         int dr;
938                                                         double dd;
939                                                         opj_tcd_pass_t *pass = &cblk->passes[passno];
940                                                         if (n == 0) {
941                                                                 dr = pass->rate;
942                                                                 dd = pass->distortiondec;
943                                                         } else {
944                                                                 dr = pass->rate - cblk->passes[n - 1].rate;
945                                                                 dd = pass->distortiondec - cblk->passes[n - 1].distortiondec;
946                                                         }
947                                                         if (!dr) {
948                                                                 if (dd != 0)
949                                                                         n = passno + 1;
950                                                                 continue;
951                                                         }
952                                                         if (dd / dr >= thresh)
953                                                                 n = passno + 1;
954                                                 }
955                                                 layer->numpasses = n - cblk->numpassesinlayers;
956                                                 
957                                                 if (!layer->numpasses) {
958                                                         layer->disto = 0;
959                                                         continue;
960                                                 }
961                                                 if (cblk->numpassesinlayers == 0) {
962                                                         layer->len = cblk->passes[n - 1].rate;
963                                                         layer->data = cblk->data;
964                                                         layer->disto = cblk->passes[n - 1].distortiondec;
965                                                 } else {
966                                                         layer->len = cblk->passes[n - 1].rate - cblk->passes[cblk->numpassesinlayers - 1].rate;
967                                                         layer->data = cblk->data + cblk->passes[cblk->numpassesinlayers - 1].rate;
968                                                         layer->disto = cblk->passes[n - 1].distortiondec - cblk->passes[cblk->numpassesinlayers - 1].distortiondec;
969                                                 }
970                                                 
971                                                 tcd_tile->distolayer[layno] += layer->disto;    /* fixed_quality */
972                                                 
973                                                 if (final)
974                                                         cblk->numpassesinlayers = n;
975                                         }
976                                 }
977                         }
978                 }
979         }
980 }
981
982 bool tcd_rateallocate(opj_tcd_t *tcd, unsigned char *dest, int len, opj_image_info_t * image_info) {
983         int compno, resno, bandno, precno, cblkno, passno, layno;
984         double min, max;
985         double cumdisto[100];   /* fixed_quality */
986         const double K = 1;             /* 1.1; fixed_quality */
987         double maxSE = 0;
988
989         opj_cp_t *cp = tcd->cp;
990         opj_tcd_tile_t *tcd_tile = tcd->tcd_tile;
991         opj_tcp_t *tcd_tcp = tcd->tcp;
992
993         min = DBL_MAX;
994         max = 0;
995         
996         tcd_tile->nbpix = 0;            /* fixed_quality */
997         
998         for (compno = 0; compno < tcd_tile->numcomps; compno++) {
999                 opj_tcd_tilecomp_t *tilec = &tcd_tile->comps[compno];
1000                 tilec->nbpix = 0;
1001
1002                 for (resno = 0; resno < tilec->numresolutions; resno++) {
1003                         opj_tcd_resolution_t *res = &tilec->resolutions[resno];
1004
1005                         for (bandno = 0; bandno < res->numbands; bandno++) {
1006                                 opj_tcd_band_t *band = &res->bands[bandno];
1007
1008                                 for (precno = 0; precno < res->pw * res->ph; precno++) {
1009                                         opj_tcd_precinct_t *prc = &band->precincts[precno];
1010
1011                                         for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
1012                                                 opj_tcd_cblk_t *cblk = &prc->cblks[cblkno];
1013
1014                                                 for (passno = 0; passno < cblk->totalpasses; passno++) {
1015                                                         opj_tcd_pass_t *pass = &cblk->passes[passno];
1016                                                         int dr;
1017                                                         double dd, rdslope;
1018                                                         if (passno == 0) {
1019                                                                 dr = pass->rate;
1020                                                                 dd = pass->distortiondec;
1021                                                         } else {
1022                                                                 dr = pass->rate - cblk->passes[passno - 1].rate;
1023                                                                 dd = pass->distortiondec - cblk->passes[passno - 1].distortiondec;
1024                                                         }
1025                                                         if (dr == 0) {
1026                                                                 continue;
1027                                                         }
1028                                                         rdslope = dd / dr;
1029                                                         if (rdslope < min) {
1030                                                                 min = rdslope;
1031                                                         }
1032                                                         if (rdslope > max) {
1033                                                                 max = rdslope;
1034                                                         }
1035                                                 } /* passno */
1036                                                 
1037                                                 /* fixed_quality */
1038                                                 tcd_tile->nbpix += ((cblk->x1 - cblk->x0) * (cblk->y1 - cblk->y0));
1039                                                 tilec->nbpix += ((cblk->x1 - cblk->x0) * (cblk->y1 - cblk->y0));
1040                                         } /* cbklno */
1041                                 } /* precno */
1042                         } /* bandno */
1043                 } /* resno */
1044                 
1045                 maxSE += (((double)(1 << tcd->image->comps[compno].prec) - 1.0) 
1046                         * ((double)(1 << tcd->image->comps[compno].prec) -1.0)) 
1047                         * ((double)(tilec->nbpix));
1048         } /* compno */
1049         
1050         /* index file */
1051         if(image_info && image_info->index_on) {
1052                 opj_tile_info_t *tile_info = &image_info->tile[tcd->tcd_tileno];
1053                 tile_info->nbpix = tcd_tile->nbpix;
1054                 tile_info->distotile = tcd_tile->distotile;
1055                 tile_info->thresh = (double *) opj_malloc(tcd_tcp->numlayers * sizeof(double));
1056         }
1057         
1058         for (layno = 0; layno < tcd_tcp->numlayers; layno++) {
1059                 double lo = min;
1060                 double hi = max;
1061                 int success = 0;
1062                 int maxlen = tcd_tcp->rates[layno] ? int_min(((int) ceil(tcd_tcp->rates[layno])), len) : len;
1063                 double goodthresh = 0;
1064                 double stable_thresh = 0;
1065                 int i;
1066                 double distotarget;             /* fixed_quality */
1067                 
1068                 /* fixed_quality */
1069                 distotarget = tcd_tile->distotile - ((K * maxSE) / pow((float)10, tcd_tcp->distoratio[layno] / 10));
1070         
1071                 if ((tcd_tcp->rates[layno]) || (cp->disto_alloc == 0)) {
1072                         opj_t2_t *t2 = t2_create(tcd->cinfo, tcd->image, cp);
1073
1074                         for (i = 0; i < 32; i++) {
1075                                 double thresh = (lo + hi) / 2;
1076                                 int l = 0;
1077                                 double distoachieved = 0;       /* fixed_quality */
1078                                 
1079                                 tcd_makelayer(tcd, layno, thresh, 0);
1080                                 
1081                                 if (cp->fixed_quality) {        /* fixed_quality */
1082                                         if(cp->cinema){
1083                                                 l = t2_encode_packets(t2,tcd->tcd_tileno, tcd_tile, layno + 1, dest, maxlen, image_info,tcd->cur_tp_num,tcd->tp_pos,tcd->cur_pino,THRESH_CALC);
1084                                                 if (l == -999) {
1085                                                         lo = thresh;
1086                                                         continue;
1087                                                 }else{
1088                         distoachieved = layno == 0 ? 
1089                                                         tcd_tile->distolayer[0] : cumdisto[layno - 1] + tcd_tile->distolayer[layno];
1090                                                         if (distoachieved < distotarget) {
1091                                                                 hi=thresh; 
1092                                                                 stable_thresh = thresh;
1093                                                                 continue;
1094                                                         }else{
1095                                                                 lo=thresh;
1096                                                         }
1097                                                 }
1098                                         }else{
1099                                                 distoachieved = (layno == 0) ? 
1100                                                         tcd_tile->distolayer[0] : (cumdisto[layno - 1] + tcd_tile->distolayer[layno]);
1101                                                 if (distoachieved < distotarget) {
1102                                                         hi = thresh;
1103                                                         stable_thresh = thresh;
1104                                                         continue;
1105                                                 }
1106                                                 lo = thresh;
1107                                         }
1108                                 } else {
1109                                         l = t2_encode_packets(t2, tcd->tcd_tileno, tcd_tile, layno + 1, dest, maxlen, image_info,tcd->cur_tp_num,tcd->tp_pos,tcd->cur_pino,THRESH_CALC);
1110                                         /* TODO: what to do with l ??? seek / tell ??? */
1111                                         /* opj_event_msg(tcd->cinfo, EVT_INFO, "rate alloc: len=%d, max=%d\n", l, maxlen); */
1112                                         if (l == -999) {
1113                                                 lo = thresh;
1114                                                 continue;
1115                                         }
1116                                         hi = thresh;
1117                                         stable_thresh = thresh;
1118                                 }
1119                         }
1120                         success = 1;
1121                         goodthresh = stable_thresh;
1122                         t2_destroy(t2);
1123                 } else {
1124                         success = 1;
1125                         goodthresh = min;
1126                 }
1127                 
1128                 if (!success) {
1129                         return false;
1130                 }
1131                 
1132                 if(image_info && image_info->index_on) {        /* Threshold for Marcela Index */
1133                         image_info->tile[tcd->tcd_tileno].thresh[layno] = goodthresh;
1134                 }
1135                 tcd_makelayer(tcd, layno, goodthresh, 1);
1136         
1137                 /* fixed_quality */
1138                 cumdisto[layno] = (layno == 0) ? tcd_tile->distolayer[0] : (cumdisto[layno - 1] + tcd_tile->distolayer[layno]); 
1139         }
1140
1141         return true;
1142 }
1143
1144 int tcd_encode_tile(opj_tcd_t *tcd, int tileno, unsigned char *dest, int len, opj_image_info_t * image_info) {
1145         int compno;
1146         int l, i, npck = 0;
1147         opj_tcd_tile_t *tile = NULL;
1148         opj_tcp_t *tcd_tcp = NULL;
1149         opj_cp_t *cp = NULL;
1150
1151         opj_tcp_t *tcp = &tcd->cp->tcps[0];
1152         opj_tccp_t *tccp = &tcp->tccps[0];
1153         opj_image_t *image = tcd->image;
1154         
1155         opj_t1_t *t1 = NULL;            /* T1 component */
1156         opj_t2_t *t2 = NULL;            /* T2 component */
1157
1158         tcd->tcd_tileno = tileno;
1159         tcd->tcd_tile = tcd->tcd_image->tiles;
1160         tcd->tcp = &tcd->cp->tcps[tileno];
1161
1162         tile = tcd->tcd_tile;
1163         tcd_tcp = tcd->tcp;
1164         cp = tcd->cp;
1165
1166         if(tcd->cur_tp_num == 0){
1167                 tcd->encoding_time = opj_clock();       /* time needed to encode a tile */
1168                 /* INDEX >> "Precinct_nb_X et Precinct_nb_Y" */
1169                 if(image_info && image_info->index_on) {
1170                         opj_tcd_tilecomp_t *tilec_idx = &tile->comps[0];        /* based on component 0 */
1171                         for (i = 0; i < tilec_idx->numresolutions; i++) {
1172                                 opj_tcd_resolution_t *res_idx = &tilec_idx->resolutions[i];
1173                                 
1174                                 image_info->tile[tileno].pw[i] = res_idx->pw;
1175                                 image_info->tile[tileno].ph[i] = res_idx->ph;
1176                                 
1177                                 npck += res_idx->pw * res_idx->ph;
1178                                 
1179                                 image_info->tile[tileno].pdx[i] = tccp->prcw[i];
1180                                 image_info->tile[tileno].pdy[i] = tccp->prch[i];
1181                         }
1182                         image_info->tile[tileno].packet = (opj_packet_info_t *) opj_malloc(image_info->comp * image_info->layer * npck * sizeof(opj_packet_info_t));
1183                 }
1184                 /* << INDEX */
1185                 
1186                 /*---------------TILE-------------------*/
1187                 
1188                 for (compno = 0; compno < tile->numcomps; compno++) {
1189                         int x, y;
1190                         
1191                         int adjust = image->comps[compno].sgnd ? 0 : 1 << (image->comps[compno].prec - 1);
1192                         int offset_x = int_ceildiv(image->x0, image->comps[compno].dx);
1193                         int offset_y = int_ceildiv(image->y0, image->comps[compno].dy);
1194                         
1195                         opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
1196                         int tw = tilec->x1 - tilec->x0;
1197                         int w = int_ceildiv(image->x1 - image->x0, image->comps[compno].dx);
1198                         
1199                         /* extract tile data */
1200                         
1201                         if (tcd_tcp->tccps[compno].qmfbid == 1) {
1202                                 for (y = tilec->y0; y < tilec->y1; y++) {
1203                                         /* start of the src tile scanline */
1204                                         int *data = &image->comps[compno].data[(tilec->x0 - offset_x) + (y - offset_y) * w];
1205                                         /* start of the dst tile scanline */
1206                                         int *tile_data = &tilec->data[(y - tilec->y0) * tw];
1207                                         for (x = tilec->x0; x < tilec->x1; x++) {
1208                                                 *tile_data++ = *data++ - adjust;
1209                                         }
1210                                 }
1211                         } else if (tcd_tcp->tccps[compno].qmfbid == 0) {
1212                                 for (y = tilec->y0; y < tilec->y1; y++) {
1213                                         /* start of the src tile scanline */
1214                                         int *data = &image->comps[compno].data[(tilec->x0 - offset_x) + (y - offset_y) * w];
1215                                         /* start of the dst tile scanline */
1216                                         int *tile_data = &tilec->data[(y - tilec->y0) * tw];
1217                                         for (x = tilec->x0; x < tilec->x1; x++) {
1218                                                 *tile_data++ = (*data++ - adjust) << 11;
1219                                         }
1220                                         
1221                                 }
1222                         }
1223                 }
1224                 
1225                 /*----------------MCT-------------------*/
1226                 if (tcd_tcp->mct) {
1227                         int samples = (tile->comps[0].x1 - tile->comps[0].x0) * (tile->comps[0].y1 - tile->comps[0].y0);
1228                         if (tcd_tcp->tccps[0].qmfbid == 0) {
1229                                 mct_encode_real(tile->comps[0].data, tile->comps[1].data, tile->comps[2].data, samples);
1230                         } else {
1231                                 mct_encode(tile->comps[0].data, tile->comps[1].data, tile->comps[2].data, samples);
1232                         }
1233                 }
1234                 
1235                 /*----------------DWT---------------------*/
1236                 
1237                 for (compno = 0; compno < tile->numcomps; compno++) {
1238                         opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
1239                         if (tcd_tcp->tccps[compno].qmfbid == 1) {
1240                                 dwt_encode(tilec);
1241                         } else if (tcd_tcp->tccps[compno].qmfbid == 0) {
1242                                 dwt_encode_real(tilec);
1243                         }
1244                 }
1245                 
1246                 /*------------------TIER1-----------------*/
1247                 t1 = t1_create(tcd->cinfo);
1248                 t1_encode_cblks(t1, tile, tcd_tcp);
1249                 t1_destroy(t1);
1250                 
1251                 /*-----------RATE-ALLOCATE------------------*/
1252                 
1253                 /* INDEX */
1254                 if(image_info) {
1255                         image_info->index_write = 0;
1256                 }
1257                 if (cp->disto_alloc || cp->fixed_quality) {     /* fixed_quality */
1258                         /* Normal Rate/distortion allocation */
1259                         tcd_rateallocate(tcd, dest, len, image_info);
1260                 } else {
1261                         /* Fixed layer allocation */
1262                         tcd_rateallocate_fixed(tcd);
1263                 }
1264         }
1265         /*--------------TIER2------------------*/
1266
1267         /* INDEX */
1268         if(image_info) {
1269                 image_info->index_write = 1;
1270         }
1271
1272         t2 = t2_create(tcd->cinfo, image, cp);
1273         l = t2_encode_packets(t2,tileno, tile, tcd_tcp->numlayers, dest, len, image_info,tcd->tp_num,tcd->tp_pos,tcd->cur_pino,FINAL_PASS);
1274         t2_destroy(t2);
1275         
1276         /*---------------CLEAN-------------------*/
1277
1278         
1279         if(tcd->cur_tp_num == tcd->cur_totnum_tp - 1){
1280                 tcd->encoding_time = opj_clock() - tcd->encoding_time;
1281                 opj_event_msg(tcd->cinfo, EVT_INFO, "- tile encoded in %f s\n", tcd->encoding_time);
1282
1283                 /* cleaning memory */
1284                 for (compno = 0; compno < tile->numcomps; compno++) {
1285                         opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
1286                         opj_aligned_free(tilec->data);
1287                 }
1288         }
1289
1290         return l;
1291 }
1292
1293 bool tcd_decode_tile(opj_tcd_t *tcd, unsigned char *src, int len, int tileno) {
1294         int l;
1295         int compno;
1296         int eof = 0;
1297         double tile_time, t1_time, dwt_time;
1298         opj_tcd_tile_t *tile = NULL;
1299
1300         opj_t1_t *t1 = NULL;            /* T1 component */
1301         opj_t2_t *t2 = NULL;            /* T2 component */
1302         
1303         tcd->tcd_tileno = tileno;
1304         tcd->tcd_tile = &(tcd->tcd_image->tiles[tileno]);
1305         tcd->tcp = &(tcd->cp->tcps[tileno]);
1306         tile = tcd->tcd_tile;
1307         
1308         tile_time = opj_clock();        /* time needed to decode a tile */
1309         opj_event_msg(tcd->cinfo, EVT_INFO, "tile %d of %d\n", tileno + 1, tcd->cp->tw * tcd->cp->th);
1310         
1311         /*--------------TIER2------------------*/
1312         
1313         t2 = t2_create(tcd->cinfo, tcd->image, tcd->cp);
1314         l = t2_decode_packets(t2, src, len, tileno, tile);
1315         t2_destroy(t2);
1316
1317         if (l == -999) {
1318                 eof = 1;
1319                 opj_event_msg(tcd->cinfo, EVT_ERROR, "tcd_decode: incomplete bistream\n");
1320         }
1321         
1322         /*------------------TIER1-----------------*/
1323         
1324         t1_time = opj_clock();  /* time needed to decode a tile */
1325         t1 = t1_create(tcd->cinfo);
1326         t1_decode_cblks(t1, tile, tcd->tcp);
1327         t1_destroy(t1);
1328         t1_time = opj_clock() - t1_time;
1329         opj_event_msg(tcd->cinfo, EVT_INFO, "- tiers-1 took %f s\n", t1_time);
1330         
1331         /*----------------DWT---------------------*/
1332
1333         dwt_time = opj_clock(); /* time needed to decode a tile */
1334         for (compno = 0; compno < tile->numcomps; compno++) {
1335                 opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
1336                 if (tcd->cp->reduce != 0) {
1337                         tcd->image->comps[compno].resno_decoded =
1338                                 tile->comps[compno].numresolutions - tcd->cp->reduce - 1;
1339                 }
1340         
1341                 if (tcd->tcp->tccps[compno].qmfbid == 1) {
1342                         dwt_decode(tilec, tilec->numresolutions - 1 - tcd->image->comps[compno].resno_decoded);
1343                 } else {
1344                         dwt_decode_real(tilec, tilec->numresolutions - 1 - tcd->image->comps[compno].resno_decoded);
1345                 }
1346
1347                 if (tile->comps[compno].numresolutions > 0) {
1348                         tcd->image->comps[compno].factor = tile->comps[compno].numresolutions - (tcd->image->comps[compno].resno_decoded + 1);
1349                 }
1350         }
1351         dwt_time = opj_clock() - dwt_time;
1352         opj_event_msg(tcd->cinfo, EVT_INFO, "- dwt took %f s\n", dwt_time);
1353         
1354         /*----------------MCT-------------------*/
1355         
1356         if (tcd->tcp->mct) {
1357                 if (tcd->tcp->tccps[0].qmfbid == 1) {
1358                         mct_decode(tile->comps[0].data, tile->comps[1].data, tile->comps[2].data, 
1359                                 (tile->comps[0].x1 - tile->comps[0].x0) * (tile->comps[0].y1 - tile->comps[0].y0));
1360                 } else {
1361                         mct_decode_real(tile->comps[0].data, tile->comps[1].data, tile->comps[2].data, 
1362                                 (tile->comps[0].x1 - tile->comps[0].x0) * (tile->comps[0].y1 - tile->comps[0].y0));
1363                 }
1364         }
1365         
1366         /*---------------TILE-------------------*/
1367         
1368         for (compno = 0; compno < tile->numcomps; compno++) {
1369                 opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
1370                 opj_tcd_resolution_t *res =     &tilec->resolutions[tcd->image->comps[compno].resno_decoded];
1371                 int adjust = tcd->image->comps[compno].sgnd ? 0 : 1 << (tcd->image->comps[compno].prec - 1);
1372                 int min = tcd->image->comps[compno].sgnd ? 
1373                         -(1 << (tcd->image->comps[compno].prec - 1)) : 0;
1374                 int max = tcd->image->comps[compno].sgnd ? 
1375                         (1 << (tcd->image->comps[compno].prec - 1)) - 1 : (1 << tcd->image->comps[compno].prec) - 1;
1376                 
1377                 int tw = tilec->x1 - tilec->x0;
1378                 int w = tcd->image->comps[compno].w;
1379                 
1380                 int i, j;
1381                 int offset_x = int_ceildivpow2(tcd->image->comps[compno].x0, tcd->image->comps[compno].factor);
1382                 int offset_y = int_ceildivpow2(tcd->image->comps[compno].y0, tcd->image->comps[compno].factor);
1383                 
1384                 for (j = res->y0; j < res->y1; j++) {
1385                         for (i = res->x0; i < res->x1; i++) {
1386                                 int v;
1387                                 float tmp = (float)((tilec->data[i - res->x0 + (j - res->y0) * tw]) / 8192.0);
1388
1389                                 if (tcd->tcp->tccps[compno].qmfbid == 1) {
1390                                         v = tilec->data[i - res->x0 + (j - res->y0) * tw];
1391                                 } else {
1392                                         int tmp2 = ((int) (floor(fabs(tmp)))) + ((int) floor(fabs(tmp*2))%2);
1393                                         v = ((tmp < 0) ? -tmp2:tmp2);
1394                                 }
1395                                 v += adjust;
1396                                 
1397                                 tcd->image->comps[compno].data[(i - offset_x) + (j - offset_y) * w] = int_clamp(v, min, max);
1398                         }
1399                 }
1400         }
1401         
1402         tile_time = opj_clock() - tile_time;    /* time needed to decode a tile */
1403         opj_event_msg(tcd->cinfo, EVT_INFO, "- tile decoded in %f s\n", tile_time);
1404
1405         for (compno = 0; compno < tile->numcomps; compno++) {
1406                 opj_aligned_free(tcd->tcd_image->tiles[tileno].comps[compno].data);
1407                 tcd->tcd_image->tiles[tileno].comps[compno].data = NULL;
1408         }
1409
1410         if (eof) {
1411                 return false;
1412         }
1413         
1414         return true;
1415 }
1416
1417 void tcd_free_decode(opj_tcd_t *tcd) {
1418         opj_tcd_image_t *tcd_image = tcd->tcd_image;    
1419         if (tcd_image->tiles != NULL) opj_free(tcd_image->tiles);
1420 }
1421
1422 void tcd_free_decode_tile(opj_tcd_t *tcd, int tileno) {
1423         int compno,resno,bandno,precno;
1424
1425         opj_tcd_image_t *tcd_image = tcd->tcd_image;
1426
1427         opj_tcd_tile_t *tile = &tcd_image->tiles[tileno];
1428         for (compno = 0; compno < tile->numcomps; compno++) {
1429                 opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
1430                 for (resno = 0; resno < tilec->numresolutions; resno++) {
1431                         opj_tcd_resolution_t *res = &tilec->resolutions[resno];
1432                         for (bandno = 0; bandno < res->numbands; bandno++) {
1433                                 opj_tcd_band_t *band = &res->bands[bandno];
1434                                 for (precno = 0; precno < res->ph * res->pw; precno++) {
1435                                         opj_tcd_precinct_t *prec = &band->precincts[precno];
1436                                         if (prec->cblks != NULL) opj_free(prec->cblks);
1437                                         if (prec->imsbtree != NULL) tgt_destroy(prec->imsbtree);
1438                                         if (prec->incltree != NULL) tgt_destroy(prec->incltree);
1439                                 }
1440                                 if (band->precincts != NULL) opj_free(band->precincts);
1441                         }
1442                 }
1443                 if (tilec->resolutions != NULL) opj_free(tilec->resolutions);
1444         }
1445         if (tile->comps != NULL) opj_free(tile->comps);
1446 }
1447
1448
1449
1450