[trunk] Another round of fixes for sign conversion warnings.
[openjpeg.git] / src / lib / openmj2 / t2.c
1 /*
2  * Copyright (c) 2002-2007, Communications and Remote Sensing Laboratory, Universite catholique de Louvain (UCL), Belgium
3  * Copyright (c) 2002-2007, Professor Benoit Macq
4  * Copyright (c) 2001-2003, David Janssens
5  * Copyright (c) 2002-2003, Yannick Verschueren
6  * Copyright (c) 2003-2007, Francois-Olivier Devaux and Antonin Descampe
7  * Copyright (c) 2005, Herve Drolon, FreeImage Team
8  * All rights reserved.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
20  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
23  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31
32 #include "opj_includes.h"
33
34 /** @defgroup T2 T2 - Implementation of a tier-2 coding */
35 /*@{*/
36
37 /** @name Local static functions */
38 /*@{*/
39
40 static void t2_putcommacode(opj_bio_t *bio, int n);
41 static int t2_getcommacode(opj_bio_t *bio);
42 /**
43 Variable length code for signalling delta Zil (truncation point)
44 @param bio Bit Input/Output component
45 @param n delta Zil
46 */
47 static void t2_putnumpasses(opj_bio_t *bio, int n);
48 static int t2_getnumpasses(opj_bio_t *bio);
49 /**
50 Encode a packet of a tile to a destination buffer
51 @param tile Tile for which to write the packets
52 @param tcp Tile coding parameters
53 @param pi Packet identity
54 @param dest Destination buffer
55 @param len Length of the destination buffer
56 @param cstr_info Codestream information structure 
57 @param tileno Number of the tile encoded
58 @return 
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_codestream_info_t *cstr_info, int tileno);
61 /**
62 @param cblk
63 @param index
64 @param cblksty
65 @param first
66 */
67 static opj_bool t2_init_seg(opj_tcd_cblk_dec_t* cblk, int index, 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 @param pack_info Packet information
77 @return 
78 */
79 static int t2_decode_packet(opj_t2_t* t2, unsigned char *src, int len, opj_tcd_tile_t *tile, 
80                                                                                                                 opj_tcp_t *tcp, opj_pi_iterator_t *pi, opj_packet_info_t *pack_info);
81
82 /*@}*/
83
84 /*@}*/
85
86 /* ----------------------------------------------------------------------- */
87
88 /* #define RESTART 0x04 */
89
90 static void t2_putcommacode(opj_bio_t *bio, int n) {
91         while (--n >= 0) {
92                 bio_write(bio, 1, 1);
93         }
94         bio_write(bio, 0, 1);
95 }
96
97 static int t2_getcommacode(opj_bio_t *bio) {
98         int n;
99         for (n = 0; bio_read(bio, 1); n++) {
100                 ;
101         }
102         return n;
103 }
104
105 static void t2_putnumpasses(opj_bio_t *bio, int n) {
106         if (n == 1) {
107                 bio_write(bio, 0, 1);
108         } else if (n == 2) {
109                 bio_write(bio, 2, 2);
110         } else if (n <= 5) {
111                 bio_write(bio, 0xc | (n - 3), 4);
112         } else if (n <= 36) {
113                 bio_write(bio, 0x1e0 | (n - 6), 9);
114         } else if (n <= 164) {
115                 bio_write(bio, 0xff80 | (n - 37), 16);
116         }
117 }
118
119 static int t2_getnumpasses(opj_bio_t *bio) {
120         int n;
121         if (!bio_read(bio, 1))
122                 return 1;
123         if (!bio_read(bio, 1))
124                 return 2;
125         if ((n = bio_read(bio, 2)) != 3)
126                 return (3 + n);
127         if ((n = bio_read(bio, 5)) != 31)
128                 return (6 + n);
129         return (37 + bio_read(bio, 7));
130 }
131
132 static int t2_encode_packet(opj_tcd_tile_t * tile, opj_tcp_t * tcp, opj_pi_iterator_t *pi, unsigned char *dest, int length, opj_codestream_info_t *cstr_info, int tileno) {
133         int bandno, cblkno;
134         unsigned char *c = dest;
135
136         int compno = pi->compno;        /* component value */
137         int resno  = pi->resno;         /* resolution level value */
138         int precno = pi->precno;        /* precinct value */
139         int layno  = pi->layno;         /* quality layer value */
140
141         opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
142         opj_tcd_resolution_t *res = &tilec->resolutions[resno];
143         
144         opj_bio_t *bio = NULL;  /* BIO component */
145         
146         /* <SOP 0xff91> */
147         if (tcp->csty & J2K_CP_CSTY_SOP) {
148                 c[0] = 255;
149                 c[1] = 145;
150                 c[2] = 0;
151                 c[3] = 4;
152                 c[4] = (unsigned char)((tile->packno % 65536) / 256);
153                 c[5] = (unsigned char)((tile->packno % 65536) % 256);
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_enc_t* cblk = &prc->cblks.enc[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, length);
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_enc_t* cblk = &prc->cblks.enc[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_enc_t* cblk = &prc->cblks.enc[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                 bio_destroy(bio);
243                 return -999;            /* modified to eliminate longjmp !! */
244         }
245
246         c += bio_numbytes(bio);
247         bio_destroy(bio);
248         
249         /* <EPH 0xff92> */
250         if (tcp->csty & J2K_CP_CSTY_EPH) {
251                 c[0] = 255;
252                 c[1] = 146;
253                 c += 2;
254         }
255         /* </EPH> */
256
257         /* << INDEX */
258         /* End of packet header position. Currently only represents the distance to start of packet
259         // Will be updated later by incrementing with packet start value */
260         if(cstr_info && cstr_info->index_write) {
261                 opj_packet_info_t *info_PK = &cstr_info->tile[tileno].packet[cstr_info->packno];
262                 info_PK->end_ph_pos = (int)(c - dest);
263         }
264         /* INDEX >> */
265         
266         /* Writing the packet body */
267         
268         for (bandno = 0; bandno < res->numbands; bandno++) {
269                 opj_tcd_band_t *band = &res->bands[bandno];
270                 opj_tcd_precinct_t *prc = &band->precincts[precno];
271                 for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
272                         opj_tcd_cblk_enc_t* cblk = &prc->cblks.enc[cblkno];
273                         opj_tcd_layer_t *layer = &cblk->layers[layno];
274                         if (!layer->numpasses) {
275                                 continue;
276                         }
277                         if (c + layer->len > dest + length) {
278                                 return -999;
279                         }
280                         
281                         memcpy(c, layer->data, layer->len);
282                         cblk->numpasses += layer->numpasses;
283                         c += layer->len;
284                         /* << INDEX */ 
285                         if(cstr_info && cstr_info->index_write) {
286                                 opj_packet_info_t *info_PK = &cstr_info->tile[tileno].packet[cstr_info->packno];
287                                 info_PK->disto += layer->disto;
288                                 if (cstr_info->D_max < info_PK->disto) {
289                                         cstr_info->D_max = info_PK->disto;
290                                 }
291                         }
292                         /* INDEX >> */
293                 }
294         }
295         
296         return (c - dest);
297 }
298
299 static opj_bool t2_init_seg(opj_tcd_cblk_dec_t* cblk, int index, int cblksty, int first) {
300         opj_tcd_seg_t* seg;
301     opj_tcd_seg_t* segs;
302     segs = (opj_tcd_seg_t*) opj_realloc(cblk->segs, (index + 1) * sizeof(opj_tcd_seg_t));
303
304     if (segs == NULL)
305     {
306         return OPJ_FALSE;
307     }
308     cblk->segs = segs;
309
310         seg = &cblk->segs[index];
311         seg->data = NULL;
312         seg->dataindex = 0;
313         seg->numpasses = 0;
314         seg->len = 0;
315         if (cblksty & J2K_CCP_CBLKSTY_TERMALL) {
316                 seg->maxpasses = 1;
317         }
318         else if (cblksty & J2K_CCP_CBLKSTY_LAZY) {
319                 if (first) {
320                         seg->maxpasses = 10;
321                 } else {
322                         seg->maxpasses = (((seg - 1)->maxpasses == 1) || ((seg - 1)->maxpasses == 10)) ? 2 : 1;
323                 }
324         } else {
325                 seg->maxpasses = 109;
326         }
327
328     return OPJ_TRUE;
329 }
330
331 static int t2_decode_packet(opj_t2_t* t2, unsigned char *src, int len, opj_tcd_tile_t *tile, 
332                                                                                                                 opj_tcp_t *tcp, opj_pi_iterator_t *pi, opj_packet_info_t *pack_info) {
333         int bandno, cblkno;
334         unsigned char *c = src;
335
336         opj_cp_t *cp = t2->cp;
337
338         int compno = pi->compno;        /* component value */
339         int resno  = pi->resno;         /* resolution level value */
340         int precno = pi->precno;        /* precinct value */
341         int layno  = pi->layno;         /* quality layer value */
342
343         opj_tcd_resolution_t* res = &tile->comps[compno].resolutions[resno];
344
345         unsigned char *hd = NULL;
346         int present;
347         
348         opj_bio_t *bio = NULL;  /* BIO component */
349         
350         if (layno == 0) {
351                 for (bandno = 0; bandno < res->numbands; bandno++) {
352                         opj_tcd_band_t *band = &res->bands[bandno];
353                         opj_tcd_precinct_t *prc = &band->precincts[precno];
354                         
355                         if ((band->x1-band->x0 == 0)||(band->y1-band->y0 == 0)) continue;
356                         
357                         tgt_reset(prc->incltree);
358                         tgt_reset(prc->imsbtree);
359                         for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
360                                 opj_tcd_cblk_dec_t* cblk = &prc->cblks.dec[cblkno];
361                                 cblk->numsegs = 0;
362                         }
363                 }
364         }
365         
366         /* SOP markers */
367         
368         if (tcp->csty & J2K_CP_CSTY_SOP) {
369                 if ((*c) != 0xff || (*(c + 1) != 0x91)) {
370                         opj_event_msg(t2->cinfo, EVT_WARNING, "Expected SOP marker\n");
371                 } else {
372                         c += 6;
373                 }
374                 
375                 /** TODO : check the Nsop value */
376         }
377         
378         /* 
379         When the marker PPT/PPM is used the packet header are store in PPT/PPM marker
380         This part deal with this caracteristic
381         step 1: Read packet header in the saved structure
382         step 2: Return to codestream for decoding 
383         */
384
385         bio = bio_create();
386         
387         if (cp->ppm == 1) {             /* PPM */
388                 hd = cp->ppm_data;
389                 bio_init_dec(bio, hd, cp->ppm_len);
390         } else if (tcp->ppt == 1) {     /* PPT */
391                 hd = tcp->ppt_data;
392                 bio_init_dec(bio, hd, tcp->ppt_len);
393         } else {                        /* Normal Case */
394                 hd = c;
395                 bio_init_dec(bio, hd, src+len-hd);
396         }
397         
398         present = bio_read(bio, 1);
399         
400         if (!present) {
401                 bio_inalign(bio);
402                 hd += bio_numbytes(bio);
403                 bio_destroy(bio);
404                 
405                 /* EPH markers */
406                 
407                 if (tcp->csty & J2K_CP_CSTY_EPH) {
408                         if ((*hd) != 0xff || (*(hd + 1) != 0x92)) {
409                                 printf("Error : expected EPH marker\n");
410                         } else {
411                                 hd += 2;
412                         }
413                 }
414
415                 /* << INDEX */
416                 /* End of packet header position. Currently only represents the distance to start of packet
417                 // Will be updated later by incrementing with packet start value*/
418                 if(pack_info) {
419                         pack_info->end_ph_pos = (int)(c - src);
420                 }
421                 /* INDEX >> */
422                 
423                 if (cp->ppm == 1) {             /* PPM case */
424                         cp->ppm_len += cp->ppm_data-hd;
425                         cp->ppm_data = hd;
426                         return (c - src);
427                 }
428                 if (tcp->ppt == 1) {    /* PPT case */
429                         tcp->ppt_len+=tcp->ppt_data-hd;
430                         tcp->ppt_data = hd;
431                         return (c - src);
432                 }
433                 
434                 return (hd - src);
435         }
436         
437         for (bandno = 0; bandno < res->numbands; bandno++) {
438                 opj_tcd_band_t *band = &res->bands[bandno];
439                 opj_tcd_precinct_t *prc = &band->precincts[precno];
440                 
441                 if ((band->x1-band->x0 == 0)||(band->y1-band->y0 == 0)) continue;
442                 
443                 for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
444                         int included, increment, n, segno;
445                         opj_tcd_cblk_dec_t* cblk = &prc->cblks.dec[cblkno];
446                         /* if cblk not yet included before --> inclusion tagtree */
447                         if (!cblk->numsegs) {
448                                 included = tgt_decode(bio, prc->incltree, cblkno, layno + 1);
449                                 /* else one bit */
450                         } else {
451                                 included = bio_read(bio, 1);
452                         }
453                         /* if cblk not included */
454                         if (!included) {
455                                 cblk->numnewpasses = 0;
456                                 continue;
457                         }
458                         /* if cblk not yet included --> zero-bitplane tagtree */
459                         if (!cblk->numsegs) {
460                                 int i, numimsbs;
461                                 for (i = 0; !tgt_decode(bio, prc->imsbtree, cblkno, i); i++) {
462                                         ;
463                                 }
464                                 numimsbs = i - 1;
465                                 cblk->numbps = band->numbps - numimsbs;
466                                 cblk->numlenbits = 3;
467                         }
468                         /* number of coding passes */
469                         cblk->numnewpasses = t2_getnumpasses(bio);
470                         increment = t2_getcommacode(bio);
471                         /* length indicator increment */
472                         cblk->numlenbits += increment;
473                         segno = 0;
474                         if (!cblk->numsegs) {
475                 if (!t2_init_seg(cblk, segno, tcp->tccps[compno].cblksty, 1))
476                 {
477                     opj_event_msg(t2->cinfo, EVT_ERROR, "Out of memory\n");
478                     bio_destroy(bio);
479                     return -999;
480                 }
481                         } else {
482                                 segno = cblk->numsegs - 1;
483                                 if (cblk->segs[segno].numpasses == cblk->segs[segno].maxpasses) {
484                                         ++segno;
485                     if (!t2_init_seg(cblk, segno, tcp->tccps[compno].cblksty, 0))
486                     {
487                         opj_event_msg(t2->cinfo, EVT_ERROR, "Out of memory\n");
488                         bio_destroy(bio);
489                         return -999;
490                     }
491                                 }
492                         }
493                         n = cblk->numnewpasses;
494                         
495                         do {
496                                 cblk->segs[segno].numnewpasses = int_min(cblk->segs[segno].maxpasses - cblk->segs[segno].numpasses, n);
497                                 cblk->segs[segno].newlen = bio_read(bio, cblk->numlenbits + int_floorlog2(cblk->segs[segno].numnewpasses));
498                                 n -= cblk->segs[segno].numnewpasses;
499                                 if (n > 0) {
500                                         ++segno;
501                     if (!t2_init_seg(cblk, segno, tcp->tccps[compno].cblksty, 0))
502                     {
503                         opj_event_msg(t2->cinfo, EVT_ERROR, "Out of memory\n");
504                         bio_destroy(bio);
505                         return -999;
506                     }
507                                 }
508                         } while (n > 0);
509                 }
510         }
511         
512         if (bio_inalign(bio)) {
513                 bio_destroy(bio);
514                 return -999;
515         }
516         
517         hd += bio_numbytes(bio);
518         bio_destroy(bio);
519         
520         /* EPH markers */
521         if (tcp->csty & J2K_CP_CSTY_EPH) {
522                 if ((*hd) != 0xff || (*(hd + 1) != 0x92)) {
523                         opj_event_msg(t2->cinfo, EVT_ERROR, "Expected EPH marker\n");
524                         return -999;
525                 } else {
526                         hd += 2;
527                 }
528         }
529
530         /* << INDEX */
531         /* End of packet header position. Currently only represents the distance to start of packet
532         // Will be updated later by incrementing with packet start value*/
533         if(pack_info) {
534                 pack_info->end_ph_pos = (int)(hd - src);
535         }
536         /* INDEX >> */
537         
538         if (cp->ppm==1) {
539                 cp->ppm_len+=cp->ppm_data-hd;
540                 cp->ppm_data = hd;
541         } else if (tcp->ppt == 1) {
542                 tcp->ppt_len+=tcp->ppt_data-hd;
543                 tcp->ppt_data = hd;
544         } else {
545                 c=hd;
546         }
547         
548         for (bandno = 0; bandno < res->numbands; bandno++) {
549                 opj_tcd_band_t *band = &res->bands[bandno];
550                 opj_tcd_precinct_t *prc = &band->precincts[precno];
551                 
552                 if ((band->x1-band->x0 == 0)||(band->y1-band->y0 == 0)) continue;
553                 
554                 for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
555                         opj_tcd_cblk_dec_t* cblk = &prc->cblks.dec[cblkno];
556                         opj_tcd_seg_t *seg = NULL;
557                         if (!cblk->numnewpasses)
558                                 continue;
559                         if (!cblk->numsegs) {
560                                 seg = &cblk->segs[0];
561                                 cblk->numsegs++;
562                                 cblk->len = 0;
563                         } else {
564                                 seg = &cblk->segs[cblk->numsegs - 1];
565                                 if (seg->numpasses == seg->maxpasses) {
566                                         seg++;
567                                         cblk->numsegs++;
568                                 }
569                         }
570                         
571                         do {
572                                 if (c + seg->newlen > src + len) {
573                                         return -999;
574                                 }
575
576 #ifdef USE_JPWL
577                         /* we need here a j2k handle to verify if making a check to
578                         the validity of cblocks parameters is selected from user (-W) */
579
580                                 /* let's check that we are not exceeding */
581                                 if ((cblk->len + seg->newlen) > 8192) {
582                                         opj_event_msg(t2->cinfo, EVT_WARNING,
583                                                 "JPWL: segment too long (%d) for codeblock %d (p=%d, b=%d, r=%d, c=%d)\n",
584                                                 seg->newlen, cblkno, precno, bandno, resno, compno);
585                                         if (!JPWL_ASSUME) {
586                                                 opj_event_msg(t2->cinfo, EVT_ERROR, "JPWL: giving up\n");
587                                                 return -999;
588                                         }
589                                         seg->newlen = 8192 - cblk->len;
590                                         opj_event_msg(t2->cinfo, EVT_WARNING, "      - truncating segment to %d\n", seg->newlen);
591                                         break;
592                                 };
593
594 #endif /* USE_JPWL */
595                                 
596                                 cblk->data = (unsigned char*) opj_realloc(cblk->data, (cblk->len + seg->newlen) * sizeof(unsigned char));
597                                 memcpy(cblk->data + cblk->len, c, seg->newlen);
598                                 if (seg->numpasses == 0) {
599                                         seg->data = &cblk->data;
600                                         seg->dataindex = cblk->len;
601                                 }
602                                 c += seg->newlen;
603                                 cblk->len += seg->newlen;
604                                 seg->len += seg->newlen;
605                                 seg->numpasses += seg->numnewpasses;
606                                 cblk->numnewpasses -= seg->numnewpasses;
607                                 if (cblk->numnewpasses > 0) {
608                                         seg++;
609                                         cblk->numsegs++;
610                                 }
611                         } while (cblk->numnewpasses > 0);
612                 }
613         }
614         
615         return (c - src);
616 }
617
618 /* ----------------------------------------------------------------------- */
619
620 int t2_encode_packets(opj_t2_t* t2,int tileno, opj_tcd_tile_t *tile, int maxlayers, unsigned char *dest, int len, opj_codestream_info_t *cstr_info,int tpnum, int tppos,int pino, J2K_T2_MODE t2_mode, int cur_totnum_tp){
621         unsigned char *c = dest;
622         int e = 0;
623         int compno;
624         opj_pi_iterator_t *pi = NULL;
625         int poc;
626         opj_image_t *image = t2->image;
627         opj_cp_t *cp = t2->cp;
628         opj_tcp_t *tcp = &cp->tcps[tileno];
629         int pocno = cp->cinema == CINEMA4K_24? 2: 1;
630         int maxcomp = cp->max_comp_size > 0 ? image->numcomps : 1;
631         
632         pi = pi_initialise_encode(image, cp, tileno, t2_mode);
633         if(!pi) {
634                 /* TODO: throw an error */
635                 return -999;
636         }
637         
638         if(t2_mode == THRESH_CALC ){ /* Calculating threshold */
639                 for(compno = 0; compno < maxcomp; compno++ ){
640                         for(poc = 0; poc < pocno ; poc++){
641                                 int comp_len = 0;
642                                 int tpnum = compno;
643                                 if (pi_create_encode(pi, cp,tileno,poc,tpnum,tppos,t2_mode,cur_totnum_tp)) {
644                                         opj_event_msg(t2->cinfo, EVT_ERROR, "Error initializing Packet Iterator\n");
645                                         pi_destroy(pi, cp, tileno);
646                                         return -999;
647                                 }
648                                 while (pi_next(&pi[poc])) {
649                                         if (pi[poc].layno < maxlayers) {
650                                                 e = t2_encode_packet(tile, &cp->tcps[tileno], &pi[poc], c, dest + len - c, cstr_info, tileno);
651                                                 comp_len = comp_len + e;
652                                                 if (e == -999) {
653                                                         break;
654                                                 } else {
655                                                         c += e;
656                                                 }
657                                         }
658                                 }
659                                 if (e == -999) break;
660                                 if (cp->max_comp_size){
661                                         if (comp_len > cp->max_comp_size){
662                                                 e = -999;
663                                                 break;
664                                         }
665                                 }
666                         }
667                         if (e == -999)  break;
668                 }
669         }else{  /* t2_mode == FINAL_PASS  */
670                 pi_create_encode(pi, cp,tileno,pino,tpnum,tppos,t2_mode,cur_totnum_tp);
671                 while (pi_next(&pi[pino])) {
672                         if (pi[pino].layno < maxlayers) {
673                                 e = t2_encode_packet(tile, &cp->tcps[tileno], &pi[pino], c, dest + len - c, cstr_info, tileno);
674                                 if (e == -999) {
675                                         break;
676                                 } else {
677                                         c += e;
678                                 }
679                                 /* INDEX >> */
680                                 if(cstr_info) {
681                                         if(cstr_info->index_write) {
682                                                 opj_tile_info_t *info_TL = &cstr_info->tile[tileno];
683                                                 opj_packet_info_t *info_PK = &info_TL->packet[cstr_info->packno];
684                                                 if (!cstr_info->packno) {
685                                                         info_PK->start_pos = info_TL->end_header + 1;
686                                                 } else {
687                                                         info_PK->start_pos = ((cp->tp_on | tcp->POC)&& info_PK->start_pos) ? info_PK->start_pos : info_TL->packet[cstr_info->packno - 1].end_pos + 1;
688                                                 }
689                                                 info_PK->end_pos = info_PK->start_pos + e - 1;
690                                                 info_PK->end_ph_pos += info_PK->start_pos - 1;  /* End of packet header which now only represents the distance 
691                                                                                                                                                                                                                                                 // to start of packet is incremented by value of start of packet*/
692                                         }
693                                         
694                                         cstr_info->packno++;
695                                 }
696                                 /* << INDEX */
697                                 tile->packno++;
698                         }
699                 }
700         }
701         
702         pi_destroy(pi, cp, tileno);
703         
704         if (e == -999) {
705                 return e;
706         }
707         
708   return (c - dest);
709 }
710
711 int t2_decode_packets(opj_t2_t *t2, unsigned char *src, int len, int tileno, opj_tcd_tile_t *tile, opj_codestream_info_t *cstr_info) {
712         unsigned char *c = src;
713         opj_pi_iterator_t *pi;
714         int pino, e = 0;
715         int n = 0, curtp = 0;
716         int tp_start_packno;
717
718         opj_image_t *image = t2->image;
719         opj_cp_t *cp = t2->cp;
720         
721         /* create a packet iterator */
722         pi = pi_create_decode(image, cp, tileno);
723         if(!pi) {
724                 /* TODO: throw an error */
725                 return -999;
726         }
727
728         tp_start_packno = 0;
729         
730         for (pino = 0; pino <= cp->tcps[tileno].numpocs; pino++) {
731                 while (pi_next(&pi[pino])) {
732                         if ((cp->layer==0) || (cp->layer>=((pi[pino].layno)+1))) {
733                                 opj_packet_info_t *pack_info;
734                                 if (cstr_info)
735                                         pack_info = &cstr_info->tile[tileno].packet[cstr_info->packno];
736                                 else
737                                         pack_info = NULL;
738                                 e = t2_decode_packet(t2, c, src + len - c, tile, &cp->tcps[tileno], &pi[pino], pack_info);
739                         } else {
740                                 e = 0;
741                         }
742             if(e == -999)
743             {
744                 pi_destroy(pi, cp, tileno);
745                 return -999;
746             }
747                         /* progression in resolution */
748                         image->comps[pi[pino].compno].resno_decoded =   
749                                 (e > 0) ? 
750                                 int_max(pi[pino].resno, image->comps[pi[pino].compno].resno_decoded) 
751                                 : image->comps[pi[pino].compno].resno_decoded;
752                         n++;
753
754                         /* INDEX >> */
755                         if(cstr_info) {
756                                 opj_tile_info_t *info_TL = &cstr_info->tile[tileno];
757                                 opj_packet_info_t *info_PK = &info_TL->packet[cstr_info->packno];
758                                 if (!cstr_info->packno) {
759                                         info_PK->start_pos = info_TL->end_header + 1;
760                                 } else if (info_TL->packet[cstr_info->packno-1].end_pos >= (int)cstr_info->tile[tileno].tp[curtp].tp_end_pos){ /* New tile part*/
761                                         info_TL->tp[curtp].tp_numpacks = cstr_info->packno - tp_start_packno; /* Number of packets in previous tile-part*/
762           info_TL->tp[curtp].tp_start_pack = tp_start_packno;
763                                         tp_start_packno = cstr_info->packno;
764                                         curtp++;
765                                         info_PK->start_pos = cstr_info->tile[tileno].tp[curtp].tp_end_header+1;
766                                 } else {
767                                         info_PK->start_pos = (cp->tp_on && info_PK->start_pos) ? info_PK->start_pos : info_TL->packet[cstr_info->packno - 1].end_pos + 1;
768                                 }
769                                 info_PK->end_pos = info_PK->start_pos + e - 1;
770                                 info_PK->end_ph_pos += info_PK->start_pos - 1;  /* End of packet header which now only represents the distance 
771                                                                                                                                                                                                                                 // to start of packet is incremented by value of start of packet*/
772                                 cstr_info->packno++;
773                         }
774                         /* << INDEX */
775                         
776                         if (e == -999) {                /* ADD */
777                                 break;
778                         } else {
779                                 c += e;
780                         }                       
781                 }
782         }
783         /* INDEX >> */
784         if(cstr_info) {
785                 cstr_info->tile[tileno].tp[curtp].tp_numpacks = cstr_info->packno - tp_start_packno; /* Number of packets in last tile-part*/
786     cstr_info->tile[tileno].tp[curtp].tp_start_pack = tp_start_packno;
787         }
788         /* << INDEX */
789
790         /* don't forget to release pi */
791         pi_destroy(pi, cp, tileno);
792         
793         if (e == -999) {
794                 return e;
795         }
796         
797         return (c - src);
798 }
799
800 /* ----------------------------------------------------------------------- */
801
802 opj_t2_t* t2_create(opj_common_ptr cinfo, opj_image_t *image, opj_cp_t *cp) {
803         /* create the tcd structure */
804         opj_t2_t *t2 = (opj_t2_t*)opj_malloc(sizeof(opj_t2_t));
805         if(!t2) return NULL;
806         t2->cinfo = cinfo;
807         t2->image = image;
808         t2->cp = cp;
809
810         return t2;
811 }
812
813 void t2_destroy(opj_t2_t *t2) {
814         if(t2) {
815                 opj_free(t2);
816         }
817 }
818
819
820
821
822