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