changed function definitions of INT and FIX modules to 'inline'
[openjpeg.git] / libopenjpeg / t2.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 /** @defgroup T2 T2 - Implementation of a tier-2 coding */
34 /*@{*/
35
36 /** @name Local static functions */
37 /*@{*/
38
39 static void t2_putcommacode(opj_bio_t *bio, int n);
40 static int t2_getcommacode(opj_bio_t *bio);
41 /**
42 Variable length code for signalling delta Zil (truncation point)
43 @param bio Bit Input/Output component
44 @param n delta Zil
45 */
46 static void t2_putnumpasses(opj_bio_t *bio, int n);
47 static int t2_getnumpasses(opj_bio_t *bio);
48 /**
49 Encode a packet of a tile to a destination buffer
50 @param tile Tile for which to write the packets
51 @param tcp Tile coding parameters
52 @param pi Packet identity
53 @param dest Destination buffer
54 @param len Length of the destination buffer
55 @param image_info Structure to create an index file
56 @param tileno Number of the tile encoded
57 @return 
58 */
59 static int t2_encode_packet(opj_tcd_tile_t *tile, opj_tcp_t *tcp, opj_pi_iterator_t *pi, unsigned char *dest, int len, opj_image_info_t *image_info, int tileno);
60 /**
61 @param seg
62 @param cblksty
63 @param first
64 */
65 static void t2_init_seg(opj_tcd_seg_t *seg, int cblksty, int first);
66 /**
67 Decode a packet of a tile from a source buffer
68 @param t2 T2 handle
69 @param src Source buffer
70 @param len Length of the source buffer
71 @param tile Tile for which to write the packets
72 @param tcp Tile coding parameters
73 @param pi Packet identity
74 @return 
75 */
76 static int t2_decode_packet(opj_t2_t* t2, unsigned char *src, int len, opj_tcd_tile_t *tile, opj_tcp_t *tcp, opj_pi_iterator_t *pi);
77
78 /*@}*/
79
80 /*@}*/
81
82 /* ----------------------------------------------------------------------- */
83
84 /* #define RESTART 0x04 */
85
86 static void t2_putcommacode(opj_bio_t *bio, int n) {
87         while (--n >= 0) {
88                 bio_write(bio, 1, 1);
89         }
90         bio_write(bio, 0, 1);
91 }
92
93 static int t2_getcommacode(opj_bio_t *bio) {
94         int n;
95         for (n = 0; bio_read(bio, 1); n++) {
96                 ;
97         }
98         return n;
99 }
100
101 static void t2_putnumpasses(opj_bio_t *bio, int n) {
102         if (n == 1) {
103                 bio_write(bio, 0, 1);
104         } else if (n == 2) {
105                 bio_write(bio, 2, 2);
106         } else if (n <= 5) {
107                 bio_write(bio, 0xc | (n - 3), 4);
108         } else if (n <= 36) {
109                 bio_write(bio, 0x1e0 | (n - 6), 9);
110         } else if (n <= 164) {
111                 bio_write(bio, 0xff80 | (n - 37), 16);
112         }
113 }
114
115 static int t2_getnumpasses(opj_bio_t *bio) {
116         int n;
117         if (!bio_read(bio, 1))
118                 return 1;
119         if (!bio_read(bio, 1))
120                 return 2;
121         if ((n = bio_read(bio, 2)) != 3)
122                 return (3 + n);
123         if ((n = bio_read(bio, 5)) != 31)
124                 return (6 + n);
125         return (37 + bio_read(bio, 7));
126 }
127
128 static int t2_encode_packet(opj_tcd_tile_t * tile, opj_tcp_t * tcp, opj_pi_iterator_t *pi, unsigned char *dest, int len, opj_image_info_t * image_info, int tileno) {
129         int bandno, cblkno;
130         unsigned char *sop = 0, *eph = 0;
131         unsigned char *c = dest;
132
133         int compno = pi->compno;        /* component value */
134         int resno  = pi->resno;         /* resolution level value */
135         int precno = pi->precno;        /* precinct value */
136         int layno  = pi->layno;         /* quality layer value */
137
138         opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
139         opj_tcd_resolution_t *res = &tilec->resolutions[resno];
140         
141         opj_bio_t *bio = NULL;  /* BIO component */
142         
143         /* <SOP 0xff91> */
144         if (tcp->csty & J2K_CP_CSTY_SOP) {
145                 sop = (unsigned char *) opj_malloc(6 * sizeof(unsigned char));
146                 sop[0] = 255;
147                 sop[1] = 145;
148                 sop[2] = 0;
149                 sop[3] = 4;
150                 sop[4] = (image_info->num % 65536) / 256;
151                 sop[5] = (image_info->num % 65536) % 256;
152                 memcpy(c, sop, 6);
153                 opj_free(sop);
154                 c += 6;
155         }
156         /* </SOP> */
157         
158         if (!layno) {
159                 for (bandno = 0; bandno < res->numbands; bandno++) {
160                         opj_tcd_band_t *band = &res->bands[bandno];
161                         opj_tcd_precinct_t *prc = &band->precincts[precno];
162                         tgt_reset(prc->incltree);
163                         tgt_reset(prc->imsbtree);
164                         for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
165                                 opj_tcd_cblk_t *cblk = &prc->cblks[cblkno];
166                                 cblk->numpasses = 0;
167                                 tgt_setvalue(prc->imsbtree, cblkno, band->numbps - cblk->numbps);
168                         }
169                 }
170         }
171         
172         bio = bio_create();
173         bio_init_enc(bio, c, len);
174         bio_write(bio, 1, 1);           /* Empty header bit */
175         
176         /* Writing Packet header */
177         for (bandno = 0; bandno < res->numbands; bandno++) {
178                 opj_tcd_band_t *band = &res->bands[bandno];
179                 opj_tcd_precinct_t *prc = &band->precincts[precno];
180                 for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
181                         opj_tcd_cblk_t *cblk = &prc->cblks[cblkno];
182                         opj_tcd_layer_t *layer = &cblk->layers[layno];
183                         if (!cblk->numpasses && layer->numpasses) {
184                                 tgt_setvalue(prc->incltree, cblkno, layno);
185                         }
186                 }
187                 for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
188                         opj_tcd_cblk_t *cblk = &prc->cblks[cblkno];
189                         opj_tcd_layer_t *layer = &cblk->layers[layno];
190                         int increment = 0;
191                         int nump = 0;
192                         int len = 0, passno;
193                         /* cblk inclusion bits */
194                         if (!cblk->numpasses) {
195                                 tgt_encode(bio, prc->incltree, cblkno, layno + 1);
196                         } else {
197                                 bio_write(bio, layer->numpasses != 0, 1);
198                         }
199                         /* if cblk not included, go to the next cblk  */
200                         if (!layer->numpasses) {
201                                 continue;
202                         }
203                         /* if first instance of cblk --> zero bit-planes information */
204                         if (!cblk->numpasses) {
205                                 cblk->numlenbits = 3;
206                                 tgt_encode(bio, prc->imsbtree, cblkno, 999);
207                         }
208                         /* number of coding passes included */
209                         t2_putnumpasses(bio, layer->numpasses);
210                         
211                         /* computation of the increase of the length indicator and insertion in the header     */
212                         for (passno = cblk->numpasses; passno < cblk->numpasses + layer->numpasses; passno++) {
213                                 opj_tcd_pass_t *pass = &cblk->passes[passno];
214                                 nump++;
215                                 len += pass->len;
216                                 if (pass->term || passno == (cblk->numpasses + layer->numpasses) - 1) {
217                                         increment = int_max(increment, int_floorlog2(len) + 1 - (cblk->numlenbits + int_floorlog2(nump)));
218                                         len = 0;
219                                         nump = 0;
220                                 }
221                         }
222                         t2_putcommacode(bio, increment);
223
224                         /* computation of the new Length indicator */
225                         cblk->numlenbits += increment;
226
227                         /* insertion of the codeword segment length */
228                         for (passno = cblk->numpasses; passno < cblk->numpasses + layer->numpasses; passno++) {
229                                 opj_tcd_pass_t *pass = &cblk->passes[passno];
230                                 nump++;
231                                 len += pass->len;
232                                 if (pass->term || passno == (cblk->numpasses + layer->numpasses) - 1) {
233                                         bio_write(bio, len, cblk->numlenbits + int_floorlog2(nump));
234                                         len = 0;
235                                         nump = 0;
236                                 }
237                         }
238                 }
239         }
240         
241         if (bio_flush(bio)) {
242                 return -999;            /* modified to eliminate longjmp !! */
243         }
244         
245         c += bio_numbytes(bio);
246
247         bio_destroy(bio);
248         
249         /* <EPH 0xff92> */
250         if (tcp->csty & J2K_CP_CSTY_EPH) {
251                 eph = (unsigned char *) opj_malloc(2 * sizeof(unsigned char));
252                 eph[0] = 255;
253                 eph[1] = 146;
254                 memcpy(c, eph, 2);
255                 opj_free(eph);
256                 c += 2;
257         }
258         /* </EPH> */
259         
260         /* Writing the packet body */
261         
262         for (bandno = 0; bandno < res->numbands; bandno++) {
263                 opj_tcd_band_t *band = &res->bands[bandno];
264                 opj_tcd_precinct_t *prc = &band->precincts[precno];
265                 for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
266                         opj_tcd_cblk_t *cblk = &prc->cblks[cblkno];
267                         opj_tcd_layer_t *layer = &cblk->layers[layno];
268                         if (!layer->numpasses) {
269                                 continue;
270                         }
271                         if (c + layer->len > dest + len) {
272                                 return -999;
273                         }
274                         
275                         memcpy(c, layer->data, layer->len);
276                         cblk->numpasses += layer->numpasses;
277                         c += layer->len;
278                         /* ADD for index Cfr. Marcela --> delta disto by packet */
279                         if(image_info && image_info->index_write && image_info->index_on) {
280                                 opj_tile_info_t *info_TL = &image_info->tile[tileno];
281                                 opj_packet_info_t *info_PK = &info_TL->packet[image_info->num];
282                                 info_PK->disto += layer->disto;
283                                 if (image_info->D_max < info_PK->disto) {
284                                         image_info->D_max = info_PK->disto;
285                                 }
286                         }
287                         /* </ADD> */
288                 }
289         }
290         
291         return (c - dest);
292 }
293
294 static void t2_init_seg(opj_tcd_seg_t * seg, int cblksty, int first) {
295         seg->numpasses = 0;
296         seg->len = 0;
297         if (cblksty & J2K_CCP_CBLKSTY_TERMALL) {
298                 seg->maxpasses = 1;
299         }
300         else if (cblksty & J2K_CCP_CBLKSTY_LAZY) {
301                 if (first) {
302                         seg->maxpasses = 10;
303                 } else {
304                         seg->maxpasses = (((seg - 1)->maxpasses == 1) || ((seg - 1)->maxpasses == 10)) ? 2 : 1;
305                 }
306         } else {
307                 seg->maxpasses = 109;
308         }
309 }
310
311 static int t2_decode_packet(opj_t2_t* t2, unsigned char *src, int len, opj_tcd_tile_t *tile, opj_tcp_t *tcp, opj_pi_iterator_t *pi) {
312         int bandno, cblkno;
313         unsigned char *c = src;
314
315         opj_cp_t *cp = t2->cp;
316
317         int compno = pi->compno;        /* component value */
318         int resno  = pi->resno;         /* resolution level value */
319         int precno = pi->precno;        /* precinct value */
320         int layno  = pi->layno;         /* quality layer value */
321
322         opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
323         opj_tcd_resolution_t *res = &tilec->resolutions[resno];
324         
325         unsigned char *hd = NULL;
326         int present;
327         
328         opj_bio_t *bio = NULL;  /* BIO component */
329         
330         if (layno == 0) {
331                 for (bandno = 0; bandno < res->numbands; bandno++) {
332                         opj_tcd_band_t *band = &res->bands[bandno];
333                         opj_tcd_precinct_t *prc = &band->precincts[precno];
334                         
335                         if ((band->x1-band->x0 == 0)||(band->y1-band->y0 == 0)) continue;
336                         
337                         tgt_reset(prc->incltree);
338                         tgt_reset(prc->imsbtree);
339                         for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
340                                 opj_tcd_cblk_t *cblk = &prc->cblks[cblkno];
341                                 cblk->numsegs = 0;
342                         }
343                 }
344         }
345         
346         /* SOP markers */
347         
348         if (tcp->csty & J2K_CP_CSTY_SOP) {
349                 if ((*c) != 0xff || (*(c + 1) != 0x91)) {
350                         opj_event_msg(t2->cinfo, EVT_WARNING, "Expected SOP marker\n");
351                 } else {
352                         c += 6;
353                 }
354                 
355                 /** TODO : check the Nsop value */
356         }
357         
358         /* 
359         When the marker PPT/PPM is used the packet header are store in PPT/PPM marker
360         This part deal with this caracteristic
361         step 1: Read packet header in the saved structure
362         step 2: Return to codestream for decoding 
363         */
364
365         bio = bio_create();
366         
367         if (cp->ppm == 1) {             /* PPM */
368                 hd = cp->ppm_data;
369                 bio_init_dec(bio, hd, cp->ppm_len);
370         } else if (tcp->ppt == 1) {     /* PPT */
371                 hd = tcp->ppt_data;
372                 bio_init_dec(bio, hd, tcp->ppt_len);
373         } else {                        /* Normal Case */
374                 hd = c;
375                 bio_init_dec(bio, hd, src+len-hd);
376         }
377         
378         present = bio_read(bio, 1);
379         
380         if (!present) {
381                 bio_inalign(bio);
382                 hd += bio_numbytes(bio);
383                 bio_destroy(bio);
384                 
385                 /* EPH markers */
386                 
387                 if (tcp->csty & J2K_CP_CSTY_EPH) {
388                         if ((*hd) != 0xff || (*(hd + 1) != 0x92)) {
389                                 printf("Error : expected EPH marker\n");
390                         } else {
391                                 hd += 2;
392                         }
393                 }
394                 
395                 if (cp->ppm == 1) {             /* PPM case */
396                         cp->ppm_len += cp->ppm_data-hd;
397                         cp->ppm_data = hd;
398                         return (c - src);
399                 }
400                 if (tcp->ppt == 1) {    /* PPT case */
401                         tcp->ppt_len+=tcp->ppt_data-hd;
402                         tcp->ppt_data = hd;
403                         return (c - src);
404                 }
405                 
406                 return (hd - src);
407         }
408         
409         for (bandno = 0; bandno < res->numbands; bandno++) {
410                 opj_tcd_band_t *band = &res->bands[bandno];
411                 opj_tcd_precinct_t *prc = &band->precincts[precno];
412                 
413                 if ((band->x1-band->x0 == 0)||(band->y1-band->y0 == 0)) continue;
414                 
415                 for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
416                         int included, increment, n;
417                         opj_tcd_cblk_t *cblk = &prc->cblks[cblkno];
418                         opj_tcd_seg_t *seg = NULL;
419                         /* if cblk not yet included before --> inclusion tagtree */
420                         if (!cblk->numsegs) {
421                                 included = tgt_decode(bio, prc->incltree, cblkno, layno + 1);
422                                 /* else one bit */
423                         } else {
424                                 included = bio_read(bio, 1);
425                         }
426                         /* if cblk not included */
427                         if (!included) {
428                                 cblk->numnewpasses = 0;
429                                 continue;
430                         }
431                         /* if cblk not yet included --> zero-bitplane tagtree */
432                         if (!cblk->numsegs) {
433                                 int i, numimsbs;
434                                 for (i = 0; !tgt_decode(bio, prc->imsbtree, cblkno, i); i++) {
435                                         ;
436                                 }
437                                 numimsbs = i - 1;
438                                 cblk->numbps = band->numbps - numimsbs;
439                                 cblk->numlenbits = 3;
440                         }
441                         /* number of coding passes */
442                         cblk->numnewpasses = t2_getnumpasses(bio);
443                         increment = t2_getcommacode(bio);
444                         /* length indicator increment */
445                         cblk->numlenbits += increment;
446                         if (!cblk->numsegs) {
447                                 seg = &cblk->segs[0];
448                                 t2_init_seg(seg, tcp->tccps[compno].cblksty, 1);
449                         } else {
450                                 seg = &cblk->segs[cblk->numsegs - 1];
451                                 if (seg->numpasses == seg->maxpasses) {
452                                         t2_init_seg(++seg, tcp->tccps[compno].cblksty, 0);
453                                 }
454                         }
455                         n = cblk->numnewpasses;
456                         
457                         do {
458                                 seg->numnewpasses = int_min(seg->maxpasses - seg->numpasses, n);
459                                 seg->newlen = bio_read(bio, cblk->numlenbits + int_floorlog2(seg->numnewpasses));
460                                 n -= seg->numnewpasses;
461                                 if (n > 0) {
462                                         t2_init_seg(++seg, tcp->tccps[compno].cblksty, 0);
463                                 }
464                         } while (n > 0);
465                 }
466         }
467         
468         if (bio_inalign(bio)) {
469                 bio_destroy(bio);
470                 return -999;
471         }
472         
473         hd += bio_numbytes(bio);
474         bio_destroy(bio);
475         
476         /* EPH markers */
477         if (tcp->csty & J2K_CP_CSTY_EPH) {
478                 if ((*hd) != 0xff || (*(hd + 1) != 0x92)) {
479                         opj_event_msg(t2->cinfo, EVT_ERROR, "Expected EPH marker\n");
480                 } else {
481                         hd += 2;
482                 }
483         }
484         
485         if (cp->ppm==1) {
486                 cp->ppm_len+=cp->ppm_data-hd;
487                 cp->ppm_data = hd;
488         } else if (tcp->ppt == 1) {
489                 tcp->ppt_len+=tcp->ppt_data-hd;
490                 tcp->ppt_data = hd;
491         } else {
492                 c=hd;
493         }
494         
495         for (bandno = 0; bandno < res->numbands; bandno++) {
496                 opj_tcd_band_t *band = &res->bands[bandno];
497                 opj_tcd_precinct_t *prc = &band->precincts[precno];
498                 
499                 if ((band->x1-band->x0 == 0)||(band->y1-band->y0 == 0)) continue;
500                 
501                 for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
502                         opj_tcd_cblk_t *cblk = &prc->cblks[cblkno];
503                         opj_tcd_seg_t *seg = NULL;
504                         if (!cblk->numnewpasses)
505                                 continue;
506                         if (!cblk->numsegs) {
507                                 seg = &cblk->segs[0];
508                                 cblk->numsegs++;
509                                 cblk->len = 0;
510                         } else {
511                                 seg = &cblk->segs[cblk->numsegs - 1];
512                                 if (seg->numpasses == seg->maxpasses) {
513                                         seg++;
514                                         cblk->numsegs++;
515                                 }
516                         }
517                         
518                         do {
519                                 if (c + seg->newlen > src + len) {
520                                         return -999;
521                                 }
522                                 
523                                 memcpy(cblk->data + cblk->len, c, seg->newlen);
524                                 if (seg->numpasses == 0) {
525                                         seg->data = cblk->data + cblk->len;
526                                 }
527                                 c += seg->newlen;
528                                 cblk->len += seg->newlen;
529                                 seg->len += seg->newlen;
530                                 seg->numpasses += seg->numnewpasses;
531                                 cblk->numnewpasses -= seg->numnewpasses;
532                                 if (cblk->numnewpasses > 0) {
533                                         seg++;
534                                         cblk->numsegs++;
535                                 }
536                         } while (cblk->numnewpasses > 0);
537                 }
538         }
539         
540         return (c - src);
541 }
542
543 /* ----------------------------------------------------------------------- */
544
545 int t2_encode_packets(opj_t2_t* t2, int tileno, opj_tcd_tile_t *tile, int maxlayers, unsigned char *dest, int len, opj_image_info_t *image_info) {
546         unsigned char *c = dest;
547         int e = 0;
548         opj_pi_iterator_t *pi = NULL;
549         int pino;
550
551         opj_image_t *image = t2->image;
552         opj_cp_t *cp = t2->cp;
553         
554         /* create a packet iterator */
555         pi = pi_create(image, cp, tileno);
556         if(!pi) {
557                 /* TODO: throw an error */
558                 return -999;
559         }
560         
561         if(image_info) {
562                 image_info->num = 0;
563         }
564         
565         for (pino = 0; pino <= cp->tcps[tileno].numpocs; pino++) {
566                 while (pi_next(&pi[pino])) {
567                         if (pi[pino].layno < maxlayers) {
568                                 e = t2_encode_packet(tile, &cp->tcps[tileno], &pi[pino], c, dest + len - c, image_info, tileno);
569                                 if (e == -999) {
570                                         break;
571                                 } else {
572                                         c += e;
573                                 }
574                                 
575                                 /* INDEX >> */
576                                 if(image_info && image_info->index_on) {
577                                         if(image_info->index_write) {
578                                                 opj_tile_info_t *info_TL = &image_info->tile[tileno];
579                                                 opj_packet_info_t *info_PK = &info_TL->packet[image_info->num];
580                                                 if (!image_info->num) {
581                                                         info_PK->start_pos = info_TL->end_header + 1;
582                                                 } else {
583                                                         info_PK->start_pos = info_TL->packet[image_info->num - 1].end_pos + 1;
584                                                 }
585                                                 info_PK->end_pos = info_PK->start_pos + e - 1;
586                                         }
587
588                                         image_info->num++;
589                                 }
590                                 /* << INDEX */
591                         }
592                 }
593         }
594
595         /* don't forget to release pi */
596         pi_destroy(pi, cp, tileno);
597         
598         if (e == -999) {
599                 return e;
600         }
601
602     return (c - dest);
603 }
604
605 int t2_decode_packets(opj_t2_t *t2, unsigned char *src, int len, int tileno, opj_tcd_tile_t *tile) {
606         unsigned char *c = src;
607         opj_pi_iterator_t *pi;
608         int pino, e = 0;
609         int n = 0;
610
611         opj_image_t *image = t2->image;
612         opj_cp_t *cp = t2->cp;
613         
614         /* create a packet iterator */
615         pi = pi_create(image, cp, tileno);
616         if(!pi) {
617                 /* TODO: throw an error */
618                 return -999;
619         }
620         
621         for (pino = 0; pino <= cp->tcps[tileno].numpocs; pino++) {
622                 while (pi_next(&pi[pino])) {
623                         if ((cp->layer==0) || (cp->layer>=((pi[pino].layno)+1))) {
624                                 e = t2_decode_packet(t2, c, src + len - c, tile, &cp->tcps[tileno], &pi[pino]);
625                         } else {
626                                 e = 0;
627                         }
628                         
629                         /* progression in resolution */
630                         image->comps[pi[pino].compno].resno_decoded =   
631                                 (e > 0) ? 
632                                 int_max(pi[pino].resno, image->comps[pi[pino].compno].resno_decoded) 
633                                 : image->comps[pi[pino].compno].resno_decoded;
634                         n++;
635                         
636                         if (e == -999) {                /* ADD */
637                                 break;
638                         } else {
639                                 c += e;
640                         }
641                 }
642         }
643
644         /* don't forget to release pi */
645         pi_destroy(pi, cp, tileno);
646         
647         if (e == -999) {
648                 return e;
649         }
650         
651     return (c - src);
652 }
653
654 /* ----------------------------------------------------------------------- */
655
656 opj_t2_t* t2_create(opj_common_ptr cinfo, opj_image_t *image, opj_cp_t *cp) {
657         /* create the tcd structure */
658         opj_t2_t *t2 = (opj_t2_t*)opj_malloc(sizeof(opj_t2_t));
659         if(!t2) return NULL;
660         t2->cinfo = cinfo;
661         t2->image = image;
662         t2->cp = cp;
663
664         return t2;
665 }
666
667 void t2_destroy(opj_t2_t *t2) {
668         if(t2) {
669                 opj_free(t2);
670         }
671 }
672