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