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