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