Upstream version 11.39.250.0
[platform/framework/web/crosswalk.git] / src / third_party / pdfium / core / src / fxcodec / fx_libopenjpeg / libopenjpeg20 / t2.c
1 /*
2  * The copyright in this software is being made available under the 2-clauses 
3  * BSD License, included below. This software may be subject to other third 
4  * party and contributor rights, including patent rights, and no such rights
5  * are granted under this license.
6  *
7  * Copyright (c) 2002-2014, Universite catholique de Louvain (UCL), Belgium
8  * Copyright (c) 2002-2014, Professor Benoit Macq
9  * Copyright (c) 2001-2003, David Janssens
10  * Copyright (c) 2002-2003, Yannick Verschueren
11  * Copyright (c) 2003-2007, Francois-Olivier Devaux 
12  * Copyright (c) 2003-2014, Antonin Descampe
13  * Copyright (c) 2005, Herve Drolon, FreeImage Team
14  * Copyright (c) 2008, 2011-2012, Centre National d'Etudes Spatiales (CNES), FR 
15  * Copyright (c) 2012, CS Systemes d'Information, France
16  * All rights reserved.
17  *
18  * Redistribution and use in source and binary forms, with or without
19  * modification, are permitted provided that the following conditions
20  * are met:
21  * 1. Redistributions of source code must retain the above copyright
22  *    notice, this list of conditions and the following disclaimer.
23  * 2. Redistributions in binary form must reproduce the above copyright
24  *    notice, this list of conditions and the following disclaimer in the
25  *    documentation and/or other materials provided with the distribution.
26  *
27  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
28  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30  * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
31  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37  * POSSIBILITY OF SUCH DAMAGE.
38  */
39
40 #include "opj_includes.h"
41
42 /** @defgroup T2 T2 - Implementation of a tier-2 coding */
43 /*@{*/
44
45 /** @name Local static functions */
46 /*@{*/
47
48 static void opj_t2_putcommacode(opj_bio_t *bio, OPJ_INT32 n);
49
50 static OPJ_UINT32 opj_t2_getcommacode(opj_bio_t *bio); 
51 /**
52 Variable length code for signalling delta Zil (truncation point)
53 @param bio  Bit Input/Output component
54 @param n    delta Zil
55 */
56 static void opj_t2_putnumpasses(opj_bio_t *bio, OPJ_UINT32 n);
57 static OPJ_UINT32 opj_t2_getnumpasses(opj_bio_t *bio);
58
59 /**
60 Encode a packet of a tile to a destination buffer
61 @param tileno Number of the tile encoded
62 @param tile Tile for which to write the packets
63 @param tcp Tile coding parameters
64 @param pi Packet identity
65 @param dest Destination buffer
66 @param p_data_written   FIXME DOC
67 @param len Length of the destination buffer
68 @param cstr_info Codestream information structure
69 @return
70 */
71 static OPJ_BOOL opj_t2_encode_packet(   OPJ_UINT32 tileno,
72                                         opj_tcd_tile_t *tile,
73                                         opj_tcp_t *tcp,
74                                         opj_pi_iterator_t *pi,
75                                         OPJ_BYTE *dest,
76                                         OPJ_UINT32 * p_data_written,
77                                         OPJ_UINT32 len,
78                                         opj_codestream_info_t *cstr_info);
79
80 /**
81 Decode a packet of a tile from a source buffer
82 @param t2 T2 handle
83 @param tile Tile for which to write the packets
84 @param tcp Tile coding parameters
85 @param pi Packet identity
86 @param src Source buffer
87 @param data_read   FIXME DOC
88 @param max_length  FIXME DOC
89 @param pack_info Packet information
90
91 @return  FIXME DOC
92 */
93 static OPJ_BOOL opj_t2_decode_packet(   opj_t2_t* t2,
94                                         opj_tcd_tile_t *tile,
95                                         opj_tcp_t *tcp,
96                                         opj_pi_iterator_t *pi,
97                                         OPJ_BYTE *src,
98                                         OPJ_UINT32 * data_read,
99                                         OPJ_UINT32 max_length,
100                                         opj_packet_info_t *pack_info);
101
102 static OPJ_BOOL opj_t2_skip_packet( opj_t2_t* p_t2,
103                                     opj_tcd_tile_t *p_tile,
104                                     opj_tcp_t *p_tcp,
105                                     opj_pi_iterator_t *p_pi,
106                                     OPJ_BYTE *p_src,
107                                     OPJ_UINT32 * p_data_read,
108                                     OPJ_UINT32 p_max_length,
109                                     opj_packet_info_t *p_pack_info);
110
111 static OPJ_BOOL opj_t2_read_packet_header(  opj_t2_t* p_t2,
112                                             opj_tcd_tile_t *p_tile,
113                                             opj_tcp_t *p_tcp,
114                                             opj_pi_iterator_t *p_pi,
115                                             OPJ_BOOL * p_is_data_present,
116                                             OPJ_BYTE *p_src_data,
117                                             OPJ_UINT32 * p_data_read,
118                                             OPJ_UINT32 p_max_length,
119                                             opj_packet_info_t *p_pack_info);
120
121 static OPJ_BOOL opj_t2_read_packet_data(opj_t2_t* p_t2,
122                                         opj_tcd_tile_t *p_tile,
123                                         opj_pi_iterator_t *p_pi,
124                                         OPJ_BYTE *p_src_data,
125                                         OPJ_UINT32 * p_data_read,
126                                         OPJ_UINT32 p_max_length,
127                                         opj_packet_info_t *pack_info);
128
129 static OPJ_BOOL opj_t2_skip_packet_data(opj_t2_t* p_t2,
130                                         opj_tcd_tile_t *p_tile,
131                                         opj_pi_iterator_t *p_pi,
132                                         OPJ_UINT32 * p_data_read,
133                                         OPJ_UINT32 p_max_length,
134                                         opj_packet_info_t *pack_info);
135
136 /**
137 @param cblk
138 @param index
139 @param cblksty
140 @param first
141 */
142 static OPJ_BOOL opj_t2_init_seg(    opj_tcd_cblk_dec_t* cblk,
143                                     OPJ_UINT32 index,
144                                     OPJ_UINT32 cblksty,
145                                     OPJ_UINT32 first);
146
147 /*@}*/
148
149 /*@}*/
150
151 /* ----------------------------------------------------------------------- */
152
153 /* #define RESTART 0x04 */
154 static void opj_t2_putcommacode(opj_bio_t *bio, OPJ_INT32 n) {
155         while (--n >= 0) {
156                 opj_bio_write(bio, 1, 1);
157         }
158         opj_bio_write(bio, 0, 1);
159 }
160
161 OPJ_UINT32 opj_t2_getcommacode(opj_bio_t *bio) 
162 {
163     OPJ_UINT32 n = 0;
164     while (opj_bio_read(bio, 1)) {
165             ++n;
166     }
167     return n;
168 }
169
170 void opj_t2_putnumpasses(opj_bio_t *bio, OPJ_UINT32 n) {
171         if (n == 1) {
172                 opj_bio_write(bio, 0, 1);
173         } else if (n == 2) {
174                 opj_bio_write(bio, 2, 2);
175         } else if (n <= 5) {
176                 opj_bio_write(bio, 0xc | (n - 3), 4);
177         } else if (n <= 36) {
178                 opj_bio_write(bio, 0x1e0 | (n - 6), 9);
179         } else if (n <= 164) {
180                 opj_bio_write(bio, 0xff80 | (n - 37), 16);
181         }
182 }
183
184 OPJ_UINT32 opj_t2_getnumpasses(opj_bio_t *bio) {
185         OPJ_UINT32 n;
186         if (!opj_bio_read(bio, 1))
187                 return 1;
188         if (!opj_bio_read(bio, 1))
189                 return 2;
190         if ((n = opj_bio_read(bio, 2)) != 3)
191                 return (3 + n);
192         if ((n = opj_bio_read(bio, 5)) != 31)
193                 return (6 + n);
194         return (37 + opj_bio_read(bio, 7));
195 }
196
197 /* ----------------------------------------------------------------------- */
198
199 OPJ_BOOL opj_t2_encode_packets( opj_t2_t* p_t2,
200                                 OPJ_UINT32 p_tile_no,
201                                 opj_tcd_tile_t *p_tile,
202                                 OPJ_UINT32 p_maxlayers,
203                                 OPJ_BYTE *p_dest,
204                                 OPJ_UINT32 * p_data_written,
205                                 OPJ_UINT32 p_max_len,
206                                 opj_codestream_info_t *cstr_info,
207                                 OPJ_UINT32 p_tp_num,
208                                 OPJ_INT32 p_tp_pos,
209                                 OPJ_UINT32 p_pino,
210                                 J2K_T2_MODE p_t2_mode)
211 {
212         OPJ_BYTE *l_current_data = p_dest;
213         OPJ_UINT32 l_nb_bytes = 0;
214         OPJ_UINT32 compno;
215         OPJ_UINT32 poc;
216         opj_pi_iterator_t *l_pi = 00;
217         opj_pi_iterator_t *l_current_pi = 00;
218         opj_image_t *l_image = p_t2->image;
219         opj_cp_t *l_cp = p_t2->cp;
220         opj_tcp_t *l_tcp = &l_cp->tcps[p_tile_no];
221         OPJ_UINT32 pocno = (l_cp->rsiz == OPJ_PROFILE_CINEMA_4K)? 2: 1;
222         OPJ_UINT32 l_max_comp = l_cp->m_specific_param.m_enc.m_max_comp_size > 0 ? l_image->numcomps : 1;
223         OPJ_UINT32 l_nb_pocs = l_tcp->numpocs + 1;
224
225         l_pi = opj_pi_initialise_encode(l_image, l_cp, p_tile_no, p_t2_mode);
226         if (!l_pi) {
227                 return OPJ_FALSE;
228         }
229
230         * p_data_written = 0;
231
232         if (p_t2_mode == THRESH_CALC ){ /* Calculating threshold */
233                 l_current_pi = l_pi;
234
235                 for     (compno = 0; compno < l_max_comp; ++compno) {
236                         OPJ_UINT32 l_comp_len = 0;
237                         l_current_pi = l_pi;
238
239                         for (poc = 0; poc < pocno ; ++poc) {
240                                 OPJ_UINT32 l_tp_num = compno;
241
242                                 /* TODO MSD : check why this function cannot fail (cf. v1) */
243                                 opj_pi_create_encode(l_pi, l_cp,p_tile_no,poc,l_tp_num,p_tp_pos,p_t2_mode);
244
245                                 if (l_current_pi->poc.prg == OPJ_PROG_UNKNOWN) {
246                                     /* TODO ADE : add an error */
247                                     opj_pi_destroy(l_pi, l_nb_pocs);
248                                     return OPJ_FALSE;
249                                 }
250                                 while (opj_pi_next(l_current_pi)) {
251                                         if (l_current_pi->layno < p_maxlayers) {
252                                                 l_nb_bytes = 0;
253
254                                                 if (! opj_t2_encode_packet(p_tile_no,p_tile, l_tcp, l_current_pi, l_current_data, &l_nb_bytes, p_max_len, cstr_info)) {
255                                                         opj_pi_destroy(l_pi, l_nb_pocs);
256                                                         return OPJ_FALSE;
257                                                 }
258
259                                                 l_comp_len += l_nb_bytes;
260                                                 l_current_data += l_nb_bytes;
261                                                 p_max_len -= l_nb_bytes;
262
263                                                 * p_data_written += l_nb_bytes;
264                                         }
265                                 }
266
267                                 if (l_cp->m_specific_param.m_enc.m_max_comp_size) {
268                                         if (l_comp_len > l_cp->m_specific_param.m_enc.m_max_comp_size) {
269                                                 opj_pi_destroy(l_pi, l_nb_pocs);
270                                                 return OPJ_FALSE;
271                                         }
272                                 }
273
274                                 ++l_current_pi;
275                         }
276                 }
277         }
278         else {  /* t2_mode == FINAL_PASS  */
279                 opj_pi_create_encode(l_pi, l_cp,p_tile_no,p_pino,p_tp_num,p_tp_pos,p_t2_mode);
280
281                 l_current_pi = &l_pi[p_pino];
282                 if (l_current_pi->poc.prg == OPJ_PROG_UNKNOWN) {
283                     /* TODO ADE : add an error */
284                     opj_pi_destroy(l_pi, l_nb_pocs);
285                     return OPJ_FALSE;
286                 }
287                 while (opj_pi_next(l_current_pi)) {
288                         if (l_current_pi->layno < p_maxlayers) {
289                                 l_nb_bytes=0;
290
291                                 if (! opj_t2_encode_packet(p_tile_no,p_tile, l_tcp, l_current_pi, l_current_data, &l_nb_bytes, p_max_len, cstr_info)) {
292                                         opj_pi_destroy(l_pi, l_nb_pocs);
293                                         return OPJ_FALSE;
294                                 }
295
296                                 l_current_data += l_nb_bytes;
297                                 p_max_len -= l_nb_bytes;
298
299                                 * p_data_written += l_nb_bytes;
300
301                                 /* INDEX >> */
302                                 if(cstr_info) {
303                                         if(cstr_info->index_write) {
304                                                 opj_tile_info_t *info_TL = &cstr_info->tile[p_tile_no];
305                                                 opj_packet_info_t *info_PK = &info_TL->packet[cstr_info->packno];
306                                                 if (!cstr_info->packno) {
307                                                         info_PK->start_pos = info_TL->end_header + 1;
308                                                 } else {
309                                                         info_PK->start_pos = ((l_cp->m_specific_param.m_enc.m_tp_on | l_tcp->POC)&& info_PK->start_pos) ? info_PK->start_pos : info_TL->packet[cstr_info->packno - 1].end_pos + 1;
310                                                 }
311                                                 info_PK->end_pos = info_PK->start_pos + l_nb_bytes - 1;
312                                                 info_PK->end_ph_pos += info_PK->start_pos - 1;  /* End of packet header which now only represents the distance
313                                                                                                                                                                                                                                                    to start of packet is incremented by value of start of packet*/
314                                         }
315
316                                         cstr_info->packno++;
317                                 }
318                                 /* << INDEX */
319                                 ++p_tile->packno;
320                         }
321                 }
322         }
323
324         opj_pi_destroy(l_pi, l_nb_pocs);
325
326         return OPJ_TRUE;
327 }
328
329 /* see issue 80 */
330 #if 0
331 #define JAS_FPRINTF fprintf
332 #else
333 /* issue 290 */
334 static void opj_null_jas_fprintf(FILE* file, const char * format, ...)
335 {
336   (void)file;
337   (void)format;
338 }
339 #define JAS_FPRINTF opj_null_jas_fprintf
340 #endif
341
342 OPJ_BOOL opj_t2_decode_packets( opj_t2_t *p_t2,
343                                 OPJ_UINT32 p_tile_no,
344                                 opj_tcd_tile_t *p_tile,
345                                 OPJ_BYTE *p_src,
346                                 OPJ_UINT32 * p_data_read,
347                                 OPJ_UINT32 p_max_len,
348                                 opj_codestream_index_t *p_cstr_index)
349 {
350         OPJ_BYTE *l_current_data = p_src;
351         opj_pi_iterator_t *l_pi = 00;
352         OPJ_UINT32 pino;
353         opj_image_t *l_image = p_t2->image;
354         opj_cp_t *l_cp = p_t2->cp;
355         opj_tcp_t *l_tcp = &(p_t2->cp->tcps[p_tile_no]);
356         OPJ_UINT32 l_nb_bytes_read;
357         OPJ_UINT32 l_nb_pocs = l_tcp->numpocs + 1;
358         opj_pi_iterator_t *l_current_pi = 00;
359 #ifdef TODO_MSD
360         OPJ_UINT32 curtp = 0;
361         OPJ_UINT32 tp_start_packno;
362 #endif 
363         opj_packet_info_t *l_pack_info = 00;
364         opj_image_comp_t* l_img_comp = 00;
365
366         OPJ_ARG_NOT_USED(p_cstr_index);
367
368 #ifdef TODO_MSD
369         if (p_cstr_index) {
370                 l_pack_info = p_cstr_index->tile_index[p_tile_no].packet;
371         }
372 #endif
373
374         /* create a packet iterator */
375         l_pi = opj_pi_create_decode(l_image, l_cp, p_tile_no);
376         if (!l_pi) {
377                 return OPJ_FALSE;
378         }
379
380
381         l_current_pi = l_pi;
382
383         for     (pino = 0; pino <= l_tcp->numpocs; ++pino) {
384
385                 /* if the resolution needed is too low, one dim of the tilec could be equal to zero
386                  * and no packets are used to decode this resolution and
387                  * l_current_pi->resno is always >= p_tile->comps[l_current_pi->compno].minimum_num_resolutions
388                  * and no l_img_comp->resno_decoded are computed
389                  */
390                 OPJ_BOOL* first_pass_failed = (OPJ_BOOL*)opj_malloc(l_image->numcomps * sizeof(OPJ_BOOL));
391                 if (!first_pass_failed)
392                 {
393                     opj_pi_destroy(l_pi,l_nb_pocs);
394                     return OPJ_FALSE;
395                 }
396                 memset(first_pass_failed, OPJ_TRUE, l_image->numcomps * sizeof(OPJ_BOOL));
397
398                 if (l_current_pi->poc.prg == OPJ_PROG_UNKNOWN) {
399                     /* TODO ADE : add an error */
400                     opj_pi_destroy(l_pi, l_nb_pocs);
401                     return OPJ_FALSE;
402                 }
403                 while (opj_pi_next(l_current_pi)) {
404                   JAS_FPRINTF( stderr, "packet offset=00000166 prg=%d cmptno=%02d rlvlno=%02d prcno=%03d lyrno=%02d\n\n",
405                     l_current_pi->poc.prg1, l_current_pi->compno, l_current_pi->resno, l_current_pi->precno, l_current_pi->layno );
406
407                         if (l_tcp->num_layers_to_decode > l_current_pi->layno
408                                         && l_current_pi->resno < p_tile->comps[l_current_pi->compno].minimum_num_resolutions) {
409                                 l_nb_bytes_read = 0;
410
411                                 first_pass_failed[l_current_pi->compno] = OPJ_FALSE;
412
413                                 if (! opj_t2_decode_packet(p_t2,p_tile,l_tcp,l_current_pi,l_current_data,&l_nb_bytes_read,p_max_len,l_pack_info)) {
414                                         opj_pi_destroy(l_pi,l_nb_pocs);
415                                         opj_free(first_pass_failed);
416                                         return OPJ_FALSE;
417                                 }
418
419                                 l_img_comp = &(l_image->comps[l_current_pi->compno]);
420                                 l_img_comp->resno_decoded = opj_uint_max(l_current_pi->resno, l_img_comp->resno_decoded);
421                         }
422                         else {
423                                 l_nb_bytes_read = 0;
424                                 if (! opj_t2_skip_packet(p_t2,p_tile,l_tcp,l_current_pi,l_current_data,&l_nb_bytes_read,p_max_len,l_pack_info)) {
425                                         opj_pi_destroy(l_pi,l_nb_pocs);
426                                         opj_free(first_pass_failed);
427                                         return OPJ_FALSE;
428                                 }
429                         }
430
431                         if (first_pass_failed[l_current_pi->compno]) {
432                                 l_img_comp = &(l_image->comps[l_current_pi->compno]);
433                                 if (l_img_comp->resno_decoded == 0)
434                                         l_img_comp->resno_decoded = p_tile->comps[l_current_pi->compno].minimum_num_resolutions - 1;
435                         }
436
437                         l_current_data += l_nb_bytes_read;
438                         p_max_len -= l_nb_bytes_read;
439
440                         /* INDEX >> */
441 #ifdef TODO_MSD
442                         if(p_cstr_info) {
443                                 opj_tile_info_v2_t *info_TL = &p_cstr_info->tile[p_tile_no];
444                                 opj_packet_info_t *info_PK = &info_TL->packet[p_cstr_info->packno];
445                                 tp_start_packno = 0;
446                                 if (!p_cstr_info->packno) {
447                                         info_PK->start_pos = info_TL->end_header + 1;
448                                 } else if (info_TL->packet[p_cstr_info->packno-1].end_pos >= (OPJ_INT32)p_cstr_info->tile[p_tile_no].tp[curtp].tp_end_pos){ /* New tile part */
449                                         info_TL->tp[curtp].tp_numpacks = p_cstr_info->packno - tp_start_packno; /* Number of packets in previous tile-part */
450                                         tp_start_packno = p_cstr_info->packno;
451                                         curtp++;
452                                         info_PK->start_pos = p_cstr_info->tile[p_tile_no].tp[curtp].tp_end_header+1;
453                                 } else {
454                                         info_PK->start_pos = (l_cp->m_specific_param.m_enc.m_tp_on && info_PK->start_pos) ? info_PK->start_pos : info_TL->packet[p_cstr_info->packno - 1].end_pos + 1;
455                                 }
456                                 info_PK->end_pos = info_PK->start_pos + l_nb_bytes_read - 1;
457                                 info_PK->end_ph_pos += info_PK->start_pos - 1;  /* End of packet header which now only represents the distance */
458                                 ++p_cstr_info->packno;
459                         }
460 #endif
461                         /* << INDEX */
462                 }
463                 ++l_current_pi;
464
465                 opj_free(first_pass_failed);
466         }
467         /* INDEX >> */
468 #ifdef TODO_MSD
469         if
470                 (p_cstr_info) {
471                 p_cstr_info->tile[p_tile_no].tp[curtp].tp_numpacks = p_cstr_info->packno - tp_start_packno; /* Number of packets in last tile-part */
472         }
473 #endif
474         /* << INDEX */
475
476         /* don't forget to release pi */
477         opj_pi_destroy(l_pi,l_nb_pocs);
478         *p_data_read = (OPJ_UINT32)(l_current_data - p_src);
479         return OPJ_TRUE;
480 }
481
482 /* ----------------------------------------------------------------------- */
483
484 /**
485  * Creates a Tier 2 handle
486  *
487  * @param       p_image         Source or destination image
488  * @param       p_cp            Image coding parameters.
489  * @return              a new T2 handle if successful, NULL otherwise.
490 */
491 opj_t2_t* opj_t2_create(opj_image_t *p_image, opj_cp_t *p_cp)
492 {
493         /* create the t2 structure */
494         opj_t2_t *l_t2 = (opj_t2_t*)opj_calloc(1,sizeof(opj_t2_t));
495         if (!l_t2) {
496                 return NULL;
497         }
498
499         l_t2->image = p_image;
500         l_t2->cp = p_cp;
501
502         return l_t2;
503 }
504
505 void opj_t2_destroy(opj_t2_t *t2) {
506         if(t2) {
507                 opj_free(t2);
508         }
509 }
510
511 OPJ_BOOL opj_t2_decode_packet(  opj_t2_t* p_t2,
512                                 opj_tcd_tile_t *p_tile,
513                                 opj_tcp_t *p_tcp,
514                                 opj_pi_iterator_t *p_pi,
515                                 OPJ_BYTE *p_src,
516                                 OPJ_UINT32 * p_data_read,
517                                 OPJ_UINT32 p_max_length,
518                                 opj_packet_info_t *p_pack_info)
519 {
520         OPJ_BOOL l_read_data;
521         OPJ_UINT32 l_nb_bytes_read = 0;
522         OPJ_UINT32 l_nb_total_bytes_read = 0;
523
524         *p_data_read = 0;
525
526         if (! opj_t2_read_packet_header(p_t2,p_tile,p_tcp,p_pi,&l_read_data,p_src,&l_nb_bytes_read,p_max_length,p_pack_info)) {
527                 return OPJ_FALSE;
528         }
529
530         p_src += l_nb_bytes_read;
531         l_nb_total_bytes_read += l_nb_bytes_read;
532         p_max_length -= l_nb_bytes_read;
533
534         /* we should read data for the packet */
535         if (l_read_data) {
536                 l_nb_bytes_read = 0;
537
538                 if (! opj_t2_read_packet_data(p_t2,p_tile,p_pi,p_src,&l_nb_bytes_read,p_max_length,p_pack_info)) {
539                         return OPJ_FALSE;
540                 }
541
542                 l_nb_total_bytes_read += l_nb_bytes_read;
543         }
544
545         *p_data_read = l_nb_total_bytes_read;
546
547         return OPJ_TRUE;
548 }
549
550 OPJ_BOOL opj_t2_encode_packet(  OPJ_UINT32 tileno,
551                                 opj_tcd_tile_t * tile,
552                                 opj_tcp_t * tcp,
553                                 opj_pi_iterator_t *pi,
554                                 OPJ_BYTE *dest,
555                                 OPJ_UINT32 * p_data_written,
556                                 OPJ_UINT32 length,
557                                 opj_codestream_info_t *cstr_info)
558 {
559         OPJ_UINT32 bandno, cblkno;
560         OPJ_BYTE* c = dest;
561         OPJ_UINT32 l_nb_bytes;
562         OPJ_UINT32 compno = pi->compno;     /* component value */
563         OPJ_UINT32 resno  = pi->resno;      /* resolution level value */
564         OPJ_UINT32 precno = pi->precno;     /* precinct value */
565         OPJ_UINT32 layno  = pi->layno;      /* quality layer value */
566         OPJ_UINT32 l_nb_blocks;
567         opj_tcd_band_t *band = 00;
568         opj_tcd_cblk_enc_t* cblk = 00;
569         opj_tcd_pass_t *pass = 00;
570
571         opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
572         opj_tcd_resolution_t *res = &tilec->resolutions[resno];
573
574         opj_bio_t *bio = 00;    /* BIO component */
575
576         /* <SOP 0xff91> */
577         if (tcp->csty & J2K_CP_CSTY_SOP) {
578                 c[0] = 255;
579                 c[1] = 145;
580                 c[2] = 0;
581                 c[3] = 4;
582 #if 0
583                 c[4] = (tile->packno % 65536) / 256;
584                 c[5] = (tile->packno % 65536) % 256;
585 #else
586                 c[4] = (tile->packno >> 8) & 0xff; /* packno is uint32_t */
587                 c[5] = tile->packno & 0xff;
588 #endif
589                 c += 6;
590                 length -= 6;
591         }
592         /* </SOP> */
593
594         if (!layno) {
595                 band = res->bands;
596
597                 for(bandno = 0; bandno < res->numbands; ++bandno) {
598                         opj_tcd_precinct_t *prc = &band->precincts[precno];
599
600                         opj_tgt_reset(prc->incltree);
601                         opj_tgt_reset(prc->imsbtree);
602
603                         l_nb_blocks = prc->cw * prc->ch;
604                         for     (cblkno = 0; cblkno < l_nb_blocks; ++cblkno) {
605                                 cblk = &prc->cblks.enc[cblkno];
606
607                                 cblk->numpasses = 0;
608                                 opj_tgt_setvalue(prc->imsbtree, cblkno, band->numbps - (OPJ_INT32)cblk->numbps);
609                         }
610                         ++band;
611                 }
612         }
613
614         bio = opj_bio_create();
615         if (!bio) {
616                 /* FIXME event manager error callback */
617                 return OPJ_FALSE;
618         }
619         opj_bio_init_enc(bio, c, length);
620         opj_bio_write(bio, 1, 1);           /* Empty header bit */
621
622         /* Writing Packet header */
623         band = res->bands;
624         for (bandno = 0; bandno < res->numbands; ++bandno)      {
625                 opj_tcd_precinct_t *prc = &band->precincts[precno];
626
627                 l_nb_blocks = prc->cw * prc->ch;
628                 cblk = prc->cblks.enc;
629
630                 for (cblkno = 0; cblkno < l_nb_blocks; ++cblkno) {
631                         opj_tcd_layer_t *layer = &cblk->layers[layno];
632
633                         if (!cblk->numpasses && layer->numpasses) {
634                                 opj_tgt_setvalue(prc->incltree, cblkno, (OPJ_INT32)layno);
635                         }
636
637                         ++cblk;
638                 }
639
640                 cblk = prc->cblks.enc;
641                 for (cblkno = 0; cblkno < l_nb_blocks; cblkno++) {
642                         opj_tcd_layer_t *layer = &cblk->layers[layno];
643                         OPJ_UINT32 increment = 0;
644                         OPJ_UINT32 nump = 0;
645                         OPJ_UINT32 len = 0, passno;
646                         OPJ_UINT32 l_nb_passes;
647
648                         /* cblk inclusion bits */
649                         if (!cblk->numpasses) {
650                                 opj_tgt_encode(bio, prc->incltree, cblkno, (OPJ_INT32)(layno + 1));
651                         } else {
652                                 opj_bio_write(bio, layer->numpasses != 0, 1);
653                         }
654
655                         /* if cblk not included, go to the next cblk  */
656                         if (!layer->numpasses) {
657                                 ++cblk;
658                                 continue;
659                         }
660
661                         /* if first instance of cblk --> zero bit-planes information */
662                         if (!cblk->numpasses) {
663                                 cblk->numlenbits = 3;
664                                 opj_tgt_encode(bio, prc->imsbtree, cblkno, 999);
665                         }
666
667                         /* number of coding passes included */
668                         opj_t2_putnumpasses(bio, layer->numpasses);
669                         l_nb_passes = cblk->numpasses + layer->numpasses;
670                         pass = cblk->passes +  cblk->numpasses;
671
672                         /* computation of the increase of the length indicator and insertion in the header     */
673                         for (passno = cblk->numpasses; passno < l_nb_passes; ++passno) {
674                                 ++nump;
675                                 len += pass->len;
676
677                                 if (pass->term || passno == (cblk->numpasses + layer->numpasses) - 1) {
678                                   increment = (OPJ_UINT32)opj_int_max((OPJ_INT32)increment, opj_int_floorlog2((OPJ_INT32)len) + 1
679                                     - ((OPJ_INT32)cblk->numlenbits + opj_int_floorlog2((OPJ_INT32)nump)));
680                                         len = 0;
681                                         nump = 0;
682                                 }
683
684                                 ++pass;
685                         }
686                         opj_t2_putcommacode(bio, (OPJ_INT32)increment);
687
688                         /* computation of the new Length indicator */
689                         cblk->numlenbits += increment;
690
691                         pass = cblk->passes +  cblk->numpasses;
692                         /* insertion of the codeword segment length */
693                         for (passno = cblk->numpasses; passno < l_nb_passes; ++passno) {
694                                 nump++;
695                                 len += pass->len;
696
697                                 if (pass->term || passno == (cblk->numpasses + layer->numpasses) - 1) {
698                                         opj_bio_write(bio, (OPJ_UINT32)len, cblk->numlenbits + (OPJ_UINT32)opj_int_floorlog2((OPJ_INT32)nump));
699                                         len = 0;
700                                         nump = 0;
701                                 }
702                                 ++pass;
703                         }
704
705                         ++cblk;
706                 }
707
708                 ++band;
709         }
710
711         if (!opj_bio_flush(bio)) {
712                 opj_bio_destroy(bio);
713                 return OPJ_FALSE;               /* modified to eliminate longjmp !! */
714         }
715
716         l_nb_bytes = (OPJ_UINT32)opj_bio_numbytes(bio);
717         c += l_nb_bytes;
718         length -= l_nb_bytes;
719
720         opj_bio_destroy(bio);
721
722         /* <EPH 0xff92> */
723         if (tcp->csty & J2K_CP_CSTY_EPH) {
724                 c[0] = 255;
725                 c[1] = 146;
726                 c += 2;
727                 length -= 2;
728         }
729         /* </EPH> */
730
731         /* << INDEX */
732         /* End of packet header position. Currently only represents the distance to start of packet
733            Will be updated later by incrementing with packet start value*/
734         if(cstr_info && cstr_info->index_write) {
735                 opj_packet_info_t *info_PK = &cstr_info->tile[tileno].packet[cstr_info->packno];
736                 info_PK->end_ph_pos = (OPJ_INT32)(c - dest);
737         }
738         /* INDEX >> */
739
740         /* Writing the packet body */
741         band = res->bands;
742         for (bandno = 0; bandno < res->numbands; bandno++) {
743                 opj_tcd_precinct_t *prc = &band->precincts[precno];
744
745                 l_nb_blocks = prc->cw * prc->ch;
746                 cblk = prc->cblks.enc;
747
748                 for (cblkno = 0; cblkno < l_nb_blocks; ++cblkno) {
749                         opj_tcd_layer_t *layer = &cblk->layers[layno];
750
751                         if (!layer->numpasses) {
752                                 ++cblk;
753                                 continue;
754                         }
755
756                         if (layer->len > length) {
757                                 return OPJ_FALSE;
758                         }
759
760                         memcpy(c, layer->data, layer->len);
761                         cblk->numpasses += layer->numpasses;
762                         c += layer->len;
763                         length -= layer->len;
764
765                         /* << INDEX */
766                         if(cstr_info && cstr_info->index_write) {
767                                 opj_packet_info_t *info_PK = &cstr_info->tile[tileno].packet[cstr_info->packno];
768                                 info_PK->disto += layer->disto;
769                                 if (cstr_info->D_max < info_PK->disto) {
770                                         cstr_info->D_max = info_PK->disto;
771                                 }
772                         }
773
774                         ++cblk;
775                         /* INDEX >> */
776                 }
777                 ++band;
778         }
779
780         assert( c >= dest );
781         * p_data_written += (OPJ_UINT32)(c - dest);
782
783         return OPJ_TRUE;
784 }
785
786 static OPJ_BOOL opj_t2_skip_packet( opj_t2_t* p_t2,
787                                     opj_tcd_tile_t *p_tile,
788                                     opj_tcp_t *p_tcp,
789                                     opj_pi_iterator_t *p_pi,
790                                     OPJ_BYTE *p_src,
791                                     OPJ_UINT32 * p_data_read,
792                                     OPJ_UINT32 p_max_length,
793                                     opj_packet_info_t *p_pack_info)
794 {
795         OPJ_BOOL l_read_data;
796         OPJ_UINT32 l_nb_bytes_read = 0;
797         OPJ_UINT32 l_nb_total_bytes_read = 0;
798
799         *p_data_read = 0;
800
801         if (! opj_t2_read_packet_header(p_t2,p_tile,p_tcp,p_pi,&l_read_data,p_src,&l_nb_bytes_read,p_max_length,p_pack_info)) {
802                 return OPJ_FALSE;
803         }
804
805         p_src += l_nb_bytes_read;
806         l_nb_total_bytes_read += l_nb_bytes_read;
807         p_max_length -= l_nb_bytes_read;
808
809         /* we should read data for the packet */
810         if (l_read_data) {
811                 l_nb_bytes_read = 0;
812
813                 if (! opj_t2_skip_packet_data(p_t2,p_tile,p_pi,&l_nb_bytes_read,p_max_length,p_pack_info)) {
814                         return OPJ_FALSE;
815                 }
816
817                 l_nb_total_bytes_read += l_nb_bytes_read;
818         }
819         *p_data_read = l_nb_total_bytes_read;
820
821         return OPJ_TRUE;
822 }
823
824
825 OPJ_BOOL opj_t2_read_packet_header( opj_t2_t* p_t2,
826                                     opj_tcd_tile_t *p_tile,
827                                     opj_tcp_t *p_tcp,
828                                     opj_pi_iterator_t *p_pi,
829                                     OPJ_BOOL * p_is_data_present,
830                                     OPJ_BYTE *p_src_data,
831                                     OPJ_UINT32 * p_data_read,
832                                     OPJ_UINT32 p_max_length,
833                                     opj_packet_info_t *p_pack_info)
834
835 {
836         /* loop */
837         OPJ_UINT32 bandno, cblkno;
838         OPJ_UINT32 l_nb_code_blocks;
839         OPJ_UINT32 l_remaining_length;
840         OPJ_UINT32 l_header_length;
841         OPJ_UINT32 * l_modified_length_ptr = 00;
842         OPJ_BYTE *l_current_data = p_src_data;
843         opj_cp_t *l_cp = p_t2->cp;
844         opj_bio_t *l_bio = 00;  /* BIO component */
845         opj_tcd_band_t *l_band = 00;
846         opj_tcd_cblk_dec_t* l_cblk = 00;
847         opj_tcd_resolution_t* l_res = &p_tile->comps[p_pi->compno].resolutions[p_pi->resno];
848
849         OPJ_BYTE *l_header_data = 00;
850         OPJ_BYTE **l_header_data_start = 00;
851
852         OPJ_UINT32 l_present;
853
854         if (p_pi->layno == 0) {
855                 l_band = l_res->bands;
856
857                 /* reset tagtrees */
858                 for (bandno = 0; bandno < l_res->numbands; ++bandno) {
859                         opj_tcd_precinct_t *l_prc = &l_band->precincts[p_pi->precno];
860
861                         if ( ! ((l_band->x1-l_band->x0 == 0)||(l_band->y1-l_band->y0 == 0)) ) {
862                                 opj_tgt_reset(l_prc->incltree);
863                                 opj_tgt_reset(l_prc->imsbtree);
864                                 l_cblk = l_prc->cblks.dec;
865
866                                 l_nb_code_blocks = l_prc->cw * l_prc->ch;
867                                 for (cblkno = 0; cblkno < l_nb_code_blocks; ++cblkno) {
868                                         l_cblk->numsegs = 0;
869                                         l_cblk->real_num_segs = 0;
870                                         ++l_cblk;
871                                 }
872                         }
873
874                         ++l_band;
875                 }
876         }
877
878         /* SOP markers */
879
880         if (p_tcp->csty & J2K_CP_CSTY_SOP) {
881                 if (p_max_length < 6) {
882                         /* TODO opj_event_msg(p_t2->cinfo->event_mgr, EVT_WARNING, "Not enough space for expected SOP marker\n"); */
883                         fprintf(stderr, "Not enough space for expected SOP marker\n");
884                 } else if ((*l_current_data) != 0xff || (*(l_current_data + 1) != 0x91)) {
885                         /* TODO opj_event_msg(p_t2->cinfo->event_mgr, EVT_WARNING, "Expected SOP marker\n"); */
886                         fprintf(stderr, "Warning: expected SOP marker\n");
887                 } else {
888                         l_current_data += 6;
889                 }
890
891                 /** TODO : check the Nsop value */
892         }
893
894         /*
895         When the marker PPT/PPM is used the packet header are store in PPT/PPM marker
896         This part deal with this caracteristic
897         step 1: Read packet header in the saved structure
898         step 2: Return to codestream for decoding
899         */
900
901         l_bio = opj_bio_create();
902         if (! l_bio) {
903                 return OPJ_FALSE;
904         }
905
906         if (l_cp->ppm == 1) { /* PPM */
907                 l_header_data_start = &l_cp->ppm_data;
908                 l_header_data = *l_header_data_start;
909                 l_modified_length_ptr = &(l_cp->ppm_len);
910
911         }
912         else if (p_tcp->ppt == 1) { /* PPT */
913                 l_header_data_start = &(p_tcp->ppt_data);
914                 l_header_data = *l_header_data_start;
915                 l_modified_length_ptr = &(p_tcp->ppt_len);
916         }
917         else {  /* Normal Case */
918                 l_header_data_start = &(l_current_data);
919                 l_header_data = *l_header_data_start;
920                 l_remaining_length = (OPJ_UINT32)(p_src_data+p_max_length-l_header_data);
921                 l_modified_length_ptr = &(l_remaining_length);
922         }
923
924         opj_bio_init_dec(l_bio, l_header_data,*l_modified_length_ptr);
925
926         l_present = opj_bio_read(l_bio, 1);
927         JAS_FPRINTF(stderr, "present=%d \n", l_present );
928         if (!l_present) {
929             /* TODO MSD: no test to control the output of this function*/
930                 opj_bio_inalign(l_bio);
931                 l_header_data += opj_bio_numbytes(l_bio);
932                 opj_bio_destroy(l_bio);
933
934                 /* EPH markers */
935                 if (p_tcp->csty & J2K_CP_CSTY_EPH) {
936                         if ((*l_modified_length_ptr - (OPJ_UINT32)(l_header_data - *l_header_data_start)) < 2U) {
937                                 fprintf(stderr, "Not enough space for expected EPH marker\n");
938                         } else if ((*l_header_data) != 0xff || (*(l_header_data + 1) != 0x92)) {
939                                 fprintf(stderr, "Error : expected EPH marker\n");
940                         } else {
941                                 l_header_data += 2;
942                         }
943                 }
944
945                 l_header_length = (OPJ_UINT32)(l_header_data - *l_header_data_start);
946                 *l_modified_length_ptr -= l_header_length;
947                 *l_header_data_start += l_header_length;
948
949                 /* << INDEX */
950                 /* End of packet header position. Currently only represents the distance to start of packet
951                    Will be updated later by incrementing with packet start value */
952                 if (p_pack_info) {
953                         p_pack_info->end_ph_pos = (OPJ_INT32)(l_current_data - p_src_data);
954                 }
955                 /* INDEX >> */
956
957                 * p_is_data_present = OPJ_FALSE;
958                 *p_data_read = (OPJ_UINT32)(l_current_data - p_src_data);
959                 return OPJ_TRUE;
960         }
961
962         l_band = l_res->bands;
963         for (bandno = 0; bandno < l_res->numbands; ++bandno) {
964                 opj_tcd_precinct_t *l_prc = &(l_band->precincts[p_pi->precno]);
965
966                 if ((l_band->x1-l_band->x0 == 0)||(l_band->y1-l_band->y0 == 0)) {
967                         ++l_band;
968                         continue;
969                 }
970
971                 l_nb_code_blocks = l_prc->cw * l_prc->ch;
972                 l_cblk = l_prc->cblks.dec;
973                 for (cblkno = 0; cblkno < l_nb_code_blocks; cblkno++) {
974                         OPJ_UINT32 l_included,l_increment, l_segno;
975                         OPJ_INT32 n;
976
977                         /* if cblk not yet included before --> inclusion tagtree */
978                         if (!l_cblk->numsegs) {
979                                 l_included = opj_tgt_decode(l_bio, l_prc->incltree, cblkno, (OPJ_INT32)(p_pi->layno + 1));
980                                 /* else one bit */
981                         }
982                         else {
983                                 l_included = opj_bio_read(l_bio, 1);
984                         }
985
986                         /* if cblk not included */
987                         if (!l_included) {
988                                 l_cblk->numnewpasses = 0;
989                                 ++l_cblk;
990         JAS_FPRINTF(stderr, "included=%d \n", l_included);
991                                 continue;
992                         }
993
994                         /* if cblk not yet included --> zero-bitplane tagtree */
995                         if (!l_cblk->numsegs) {
996                                 OPJ_UINT32 i = 0;
997
998                                 while (!opj_tgt_decode(l_bio, l_prc->imsbtree, cblkno, (OPJ_INT32)i)) {
999                                         ++i;
1000                                 }
1001
1002                                 l_cblk->numbps = (OPJ_UINT32)l_band->numbps + 1 - i;
1003                                 l_cblk->numlenbits = 3;
1004                         }
1005
1006                         /* number of coding passes */
1007                         l_cblk->numnewpasses = opj_t2_getnumpasses(l_bio);
1008                         l_increment = opj_t2_getcommacode(l_bio);
1009
1010                         /* length indicator increment */
1011                         l_cblk->numlenbits += l_increment;
1012                         l_segno = 0;
1013
1014                         if (!l_cblk->numsegs) {
1015                                 if (! opj_t2_init_seg(l_cblk, l_segno, p_tcp->tccps[p_pi->compno].cblksty, 1)) {
1016                                         opj_bio_destroy(l_bio);
1017                                         return OPJ_FALSE;
1018                                 }
1019                         }
1020                         else {
1021                                 l_segno = l_cblk->numsegs - 1;
1022                                 if (l_cblk->segs[l_segno].numpasses == l_cblk->segs[l_segno].maxpasses) {
1023                                         ++l_segno;
1024                                         if (! opj_t2_init_seg(l_cblk, l_segno, p_tcp->tccps[p_pi->compno].cblksty, 0)) {
1025                                                 opj_bio_destroy(l_bio);
1026                                                 return OPJ_FALSE;
1027                                         }
1028                                 }
1029                         }
1030                         n = (OPJ_INT32)l_cblk->numnewpasses;
1031
1032                         do {
1033                                 l_cblk->segs[l_segno].numnewpasses = (OPJ_UINT32)opj_int_min((OPJ_INT32)(l_cblk->segs[l_segno].maxpasses - l_cblk->segs[l_segno].numpasses), n);
1034                                 l_cblk->segs[l_segno].newlen = opj_bio_read(l_bio, l_cblk->numlenbits + opj_uint_floorlog2(l_cblk->segs[l_segno].numnewpasses));
1035                                         JAS_FPRINTF(stderr, "included=%d numnewpasses=%d increment=%d len=%d \n", l_included, l_cblk->segs[l_segno].numnewpasses, l_increment, l_cblk->segs[l_segno].newlen );
1036
1037                                 n -= (OPJ_INT32)l_cblk->segs[l_segno].numnewpasses;
1038                                 if (n > 0) {
1039                                         ++l_segno;
1040
1041                                         if (! opj_t2_init_seg(l_cblk, l_segno, p_tcp->tccps[p_pi->compno].cblksty, 0)) {
1042                                                 opj_bio_destroy(l_bio);
1043                                                 return OPJ_FALSE;
1044                                         }
1045                                 }
1046                         } while (n > 0);
1047
1048                         ++l_cblk;
1049                 }
1050
1051                 ++l_band;
1052         }
1053
1054         if (!opj_bio_inalign(l_bio)) {
1055                 opj_bio_destroy(l_bio);
1056                 return OPJ_FALSE;
1057         }
1058
1059         l_header_data += opj_bio_numbytes(l_bio);
1060         opj_bio_destroy(l_bio);
1061
1062         /* EPH markers */
1063         if (p_tcp->csty & J2K_CP_CSTY_EPH) {
1064                 if ((*l_modified_length_ptr - (OPJ_UINT32)(l_header_data - *l_header_data_start)) < 2U) {
1065                         fprintf(stderr, "Not enough space for expected EPH marker\n");
1066                 } else if ((*l_header_data) != 0xff || (*(l_header_data + 1) != 0x92)) {
1067                         /* TODO opj_event_msg(t2->cinfo->event_mgr, EVT_ERROR, "Expected EPH marker\n"); */
1068                         fprintf(stderr, "Error : expected EPH marker\n");
1069                 } else {
1070                         l_header_data += 2;
1071                 }
1072         }
1073
1074         l_header_length = (OPJ_UINT32)(l_header_data - *l_header_data_start);
1075         JAS_FPRINTF( stderr, "hdrlen=%d \n", l_header_length );
1076         JAS_FPRINTF( stderr, "packet body\n");
1077         *l_modified_length_ptr -= l_header_length;
1078         *l_header_data_start += l_header_length;
1079
1080         /* << INDEX */
1081         /* End of packet header position. Currently only represents the distance to start of packet
1082          Will be updated later by incrementing with packet start value */
1083         if (p_pack_info) {
1084                 p_pack_info->end_ph_pos = (OPJ_INT32)(l_current_data - p_src_data);
1085         }
1086         /* INDEX >> */
1087
1088         *p_is_data_present = OPJ_TRUE;
1089         *p_data_read = (OPJ_UINT32)(l_current_data - p_src_data);
1090
1091         return OPJ_TRUE;
1092 }
1093
1094 OPJ_BOOL opj_t2_read_packet_data(   opj_t2_t* p_t2,
1095                                     opj_tcd_tile_t *p_tile,
1096                                     opj_pi_iterator_t *p_pi,
1097                                     OPJ_BYTE *p_src_data,
1098                                     OPJ_UINT32 * p_data_read,
1099                                     OPJ_UINT32 p_max_length,
1100                                     opj_packet_info_t *pack_info)
1101 {
1102         OPJ_UINT32 bandno, cblkno;
1103         OPJ_UINT32 l_nb_code_blocks;
1104         OPJ_BYTE *l_current_data = p_src_data;
1105         opj_tcd_band_t *l_band = 00;
1106         opj_tcd_cblk_dec_t* l_cblk = 00;
1107         opj_tcd_resolution_t* l_res = &p_tile->comps[p_pi->compno].resolutions[p_pi->resno];
1108
1109         OPJ_ARG_NOT_USED(p_t2);
1110         OPJ_ARG_NOT_USED(pack_info);
1111
1112         l_band = l_res->bands;
1113         for (bandno = 0; bandno < l_res->numbands; ++bandno) {
1114                 opj_tcd_precinct_t *l_prc = &l_band->precincts[p_pi->precno];
1115
1116                 if ((l_band->x1-l_band->x0 == 0)||(l_band->y1-l_band->y0 == 0)) {
1117                         ++l_band;
1118                         continue;
1119                 }
1120
1121                 l_nb_code_blocks = l_prc->cw * l_prc->ch;
1122                 l_cblk = l_prc->cblks.dec;
1123
1124                 for (cblkno = 0; cblkno < l_nb_code_blocks; ++cblkno) {
1125                         opj_tcd_seg_t *l_seg = 00;
1126
1127                         if (!l_cblk->numnewpasses) {
1128                                 /* nothing to do */
1129                                 ++l_cblk;
1130                                 continue;
1131                         }
1132
1133                         if (!l_cblk->numsegs) {
1134                                 l_seg = l_cblk->segs;
1135                                 ++l_cblk->numsegs;
1136                                 l_cblk->data_current_size = 0;
1137                         }
1138                         else {
1139                                 l_seg = &l_cblk->segs[l_cblk->numsegs - 1];
1140
1141                                 if (l_seg->numpasses == l_seg->maxpasses) {
1142                                         ++l_seg;
1143                                         ++l_cblk->numsegs;
1144                                 }
1145                         }
1146
1147                         do {
1148                                 /* Check possible overflow (on l_current_data only, assumes input args already checked) then size */
1149                                 if ((((OPJ_SIZE_T)l_current_data + (OPJ_SIZE_T)l_seg->newlen) < (OPJ_SIZE_T)l_current_data) || (l_current_data + l_seg->newlen > p_src_data + p_max_length)) {
1150                                         fprintf(stderr, "read: segment too long (%d) with max (%d) for codeblock %d (p=%d, b=%d, r=%d, c=%d)\n",
1151                                                 l_seg->newlen, p_max_length, cblkno, p_pi->precno, bandno, p_pi->resno, p_pi->compno);
1152                                         return OPJ_FALSE;
1153                                 }
1154
1155 #ifdef USE_JPWL
1156                         /* we need here a j2k handle to verify if making a check to
1157                         the validity of cblocks parameters is selected from user (-W) */
1158
1159                                 /* let's check that we are not exceeding */
1160                                 if ((l_cblk->len + l_seg->newlen) > 8192) {
1161                                         opj_event_msg(p_t2->cinfo, EVT_WARNING,
1162                                                 "JPWL: segment too long (%d) for codeblock %d (p=%d, b=%d, r=%d, c=%d)\n",
1163                                                 l_seg->newlen, cblkno, p_pi->precno, bandno, p_pi->resno, p_pi->compno);
1164                                         if (!JPWL_ASSUME) {
1165                                                 opj_event_msg(p_t2->cinfo, EVT_ERROR, "JPWL: giving up\n");
1166                                                 return OPJ_FALSE;
1167                                         }
1168                                         l_seg->newlen = 8192 - l_cblk->len;
1169                                         opj_event_msg(p_t2->cinfo, EVT_WARNING, "      - truncating segment to %d\n", l_seg->newlen);
1170                                         break;
1171                                 };
1172
1173 #endif /* USE_JPWL */
1174                                 /* Check possible overflow on size */
1175                                 if ((l_cblk->data_current_size + l_seg->newlen) < l_cblk->data_current_size) {
1176                                         fprintf(stderr, "read: segment too long (%d) with current size (%d > %d) for codeblock %d (p=%d, b=%d, r=%d, c=%d)\n",
1177                                                 l_seg->newlen, l_cblk->data_current_size, 0xFFFFFFFF - l_seg->newlen, cblkno, p_pi->precno, bandno, p_pi->resno, p_pi->compno);
1178                                         return OPJ_FALSE;
1179                                 }
1180                                 /* Check if the cblk->data have allocated enough memory */
1181                                 if ((l_cblk->data_current_size + l_seg->newlen) > l_cblk->data_max_size) {
1182                                     OPJ_BYTE* new_cblk_data = (OPJ_BYTE*) opj_realloc(l_cblk->data, l_cblk->data_current_size + l_seg->newlen);
1183                                     if(! new_cblk_data) {
1184                                         opj_free(l_cblk->data);
1185                                         l_cblk->data = NULL;
1186                                         l_cblk->data_max_size = 0;
1187                                         /* opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to realloc code block cata!\n"); */
1188                                         return OPJ_FALSE;
1189                                     }
1190                                     l_cblk->data_max_size = l_cblk->data_current_size + l_seg->newlen;
1191                                     l_cblk->data = new_cblk_data;
1192                                 }
1193                                
1194                                 memcpy(l_cblk->data + l_cblk->data_current_size, l_current_data, l_seg->newlen);
1195
1196                                 if (l_seg->numpasses == 0) {
1197                                         l_seg->data = &l_cblk->data;
1198                                         l_seg->dataindex = l_cblk->data_current_size;
1199                                 }
1200
1201                                 l_current_data += l_seg->newlen;
1202                                 l_seg->numpasses += l_seg->numnewpasses;
1203                                 l_cblk->numnewpasses -= l_seg->numnewpasses;
1204
1205                                 l_seg->real_num_passes = l_seg->numpasses;
1206                                 l_cblk->data_current_size += l_seg->newlen;
1207                                 l_seg->len += l_seg->newlen;
1208
1209                                 if (l_cblk->numnewpasses > 0) {
1210                                         ++l_seg;
1211                                         ++l_cblk->numsegs;
1212                                 }
1213                         } while (l_cblk->numnewpasses > 0);
1214
1215                         l_cblk->real_num_segs = l_cblk->numsegs;
1216                         ++l_cblk;
1217                 } /* next code_block */
1218
1219                 ++l_band;
1220         }
1221
1222         *(p_data_read) = (OPJ_UINT32)(l_current_data - p_src_data);
1223
1224
1225         return OPJ_TRUE;
1226 }
1227
1228 OPJ_BOOL opj_t2_skip_packet_data(   opj_t2_t* p_t2,
1229                                     opj_tcd_tile_t *p_tile,
1230                                     opj_pi_iterator_t *p_pi,
1231                                     OPJ_UINT32 * p_data_read,
1232                                     OPJ_UINT32 p_max_length,
1233                                     opj_packet_info_t *pack_info)
1234 {
1235         OPJ_UINT32 bandno, cblkno;
1236         OPJ_UINT32 l_nb_code_blocks;
1237         opj_tcd_band_t *l_band = 00;
1238         opj_tcd_cblk_dec_t* l_cblk = 00;
1239         opj_tcd_resolution_t* l_res = &p_tile->comps[p_pi->compno].resolutions[p_pi->resno];
1240
1241         OPJ_ARG_NOT_USED(p_t2);
1242         OPJ_ARG_NOT_USED(pack_info);
1243
1244         *p_data_read = 0;
1245         l_band = l_res->bands;
1246
1247         for (bandno = 0; bandno < l_res->numbands; ++bandno) {
1248                 opj_tcd_precinct_t *l_prc = &l_band->precincts[p_pi->precno];
1249
1250                 if ((l_band->x1-l_band->x0 == 0)||(l_band->y1-l_band->y0 == 0)) {
1251                         ++l_band;
1252                         continue;
1253                 }
1254
1255                 l_nb_code_blocks = l_prc->cw * l_prc->ch;
1256                 l_cblk = l_prc->cblks.dec;
1257
1258                 for (cblkno = 0; cblkno < l_nb_code_blocks; ++cblkno) {
1259                         opj_tcd_seg_t *l_seg = 00;
1260
1261                         if (!l_cblk->numnewpasses) {
1262                                 /* nothing to do */
1263                                 ++l_cblk;
1264                                 continue;
1265                         }
1266
1267                         if (!l_cblk->numsegs) {
1268                                 l_seg = l_cblk->segs;
1269                                 ++l_cblk->numsegs;
1270                                 l_cblk->data_current_size = 0;
1271                         }
1272                         else {
1273                                 l_seg = &l_cblk->segs[l_cblk->numsegs - 1];
1274
1275                                 if (l_seg->numpasses == l_seg->maxpasses) {
1276                                         ++l_seg;
1277                                         ++l_cblk->numsegs;
1278                                 }
1279                         }
1280
1281                         do {
1282                                 /* Check possible overflow then size */
1283                                 if (((*p_data_read + l_seg->newlen) < (*p_data_read)) || ((*p_data_read + l_seg->newlen) > p_max_length)) {
1284                                         fprintf(stderr, "skip: segment too long (%d) with max (%d) for codeblock %d (p=%d, b=%d, r=%d, c=%d)\n",
1285                                                 l_seg->newlen, p_max_length, cblkno, p_pi->precno, bandno, p_pi->resno, p_pi->compno);
1286                                         return OPJ_FALSE;
1287                                 }
1288
1289 #ifdef USE_JPWL
1290                         /* we need here a j2k handle to verify if making a check to
1291                         the validity of cblocks parameters is selected from user (-W) */
1292
1293                                 /* let's check that we are not exceeding */
1294                                 if ((l_cblk->len + l_seg->newlen) > 8192) {
1295                                         opj_event_msg(p_t2->cinfo, EVT_WARNING,
1296                                                 "JPWL: segment too long (%d) for codeblock %d (p=%d, b=%d, r=%d, c=%d)\n",
1297                                                 l_seg->newlen, cblkno, p_pi->precno, bandno, p_pi->resno, p_pi->compno);
1298                                         if (!JPWL_ASSUME) {
1299                                                 opj_event_msg(p_t2->cinfo, EVT_ERROR, "JPWL: giving up\n");
1300                                                 return -999;
1301                                         }
1302                                         l_seg->newlen = 8192 - l_cblk->len;
1303                                         opj_event_msg(p_t2->cinfo, EVT_WARNING, "      - truncating segment to %d\n", l_seg->newlen);
1304                                         break;
1305                                 };
1306
1307 #endif /* USE_JPWL */
1308                                         JAS_FPRINTF(stderr, "p_data_read (%d) newlen (%d) \n", *p_data_read, l_seg->newlen );
1309                                 *(p_data_read) += l_seg->newlen;
1310
1311                                 l_seg->numpasses += l_seg->numnewpasses;
1312                                 l_cblk->numnewpasses -= l_seg->numnewpasses;
1313                                 if (l_cblk->numnewpasses > 0)
1314                                 {
1315                                         ++l_seg;
1316                                         ++l_cblk->numsegs;
1317                                 }
1318                         } while (l_cblk->numnewpasses > 0);
1319
1320                         ++l_cblk;
1321                 }
1322
1323                 ++l_band;
1324         }
1325
1326         return OPJ_TRUE;
1327 }
1328
1329
1330 OPJ_BOOL opj_t2_init_seg(   opj_tcd_cblk_dec_t* cblk,
1331                             OPJ_UINT32 index, 
1332                             OPJ_UINT32 cblksty, 
1333                             OPJ_UINT32 first)
1334 {
1335         opj_tcd_seg_t* seg = 00;
1336         OPJ_UINT32 l_nb_segs = index + 1;
1337
1338         if (l_nb_segs > cblk->m_current_max_segs) {
1339                 opj_tcd_seg_t* new_segs;
1340                 cblk->m_current_max_segs += OPJ_J2K_DEFAULT_NB_SEGS;
1341
1342                 new_segs = (opj_tcd_seg_t*) opj_realloc(cblk->segs, cblk->m_current_max_segs * sizeof(opj_tcd_seg_t));
1343                 if(! new_segs) {
1344                         opj_free(cblk->segs);
1345                         cblk->segs = NULL;
1346                         cblk->m_current_max_segs = 0;
1347                         /* opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to initialize segment %d\n", l_nb_segs); */
1348                         return OPJ_FALSE;
1349                 }
1350                 cblk->segs = new_segs;
1351         }
1352
1353         seg = &cblk->segs[index];
1354         memset(seg,0,sizeof(opj_tcd_seg_t));
1355
1356         if (cblksty & J2K_CCP_CBLKSTY_TERMALL) {
1357                 seg->maxpasses = 1;
1358         }
1359         else if (cblksty & J2K_CCP_CBLKSTY_LAZY) {
1360                 if (first) {
1361                         seg->maxpasses = 10;
1362                 } else {
1363                         seg->maxpasses = (((seg - 1)->maxpasses == 1) || ((seg - 1)->maxpasses == 10)) ? 2 : 1;
1364                 }
1365         } else {
1366                 seg->maxpasses = 109;
1367         }
1368
1369         return OPJ_TRUE;
1370 }