Fix XFA builds under chromium checkouts
[pdfium.git] / 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 = NULL;
391                                         
392                 if (l_current_pi->poc.prg == OPJ_PROG_UNKNOWN) {
393                     /* TODO ADE : add an error */
394                     opj_pi_destroy(l_pi, l_nb_pocs);
395                     return OPJ_FALSE;
396                 }
397                                         
398                 first_pass_failed = (OPJ_BOOL*)opj_malloc(l_image->numcomps * sizeof(OPJ_BOOL));
399                 if (!first_pass_failed)
400                 {
401                     opj_pi_destroy(l_pi,l_nb_pocs);
402                     return OPJ_FALSE;
403                 }
404                 memset(first_pass_failed, OPJ_TRUE, l_image->numcomps * sizeof(OPJ_BOOL));
405
406                 while (opj_pi_next(l_current_pi)) {
407                   JAS_FPRINTF( stderr, "packet offset=00000166 prg=%d cmptno=%02d rlvlno=%02d prcno=%03d lyrno=%02d\n\n",
408                     l_current_pi->poc.prg1, l_current_pi->compno, l_current_pi->resno, l_current_pi->precno, l_current_pi->layno );
409
410                         if (l_tcp->num_layers_to_decode > l_current_pi->layno
411                                         && l_current_pi->resno < p_tile->comps[l_current_pi->compno].minimum_num_resolutions) {
412                                 l_nb_bytes_read = 0;
413
414                                 first_pass_failed[l_current_pi->compno] = OPJ_FALSE;
415
416                                 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)) {
417                                         opj_pi_destroy(l_pi,l_nb_pocs);
418                                         opj_free(first_pass_failed);
419                                         return OPJ_FALSE;
420                                 }
421
422                                 l_img_comp = &(l_image->comps[l_current_pi->compno]);
423                                 l_img_comp->resno_decoded = opj_uint_max(l_current_pi->resno, l_img_comp->resno_decoded);
424                         }
425                         else {
426                                 l_nb_bytes_read = 0;
427                                 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)) {
428                                         opj_pi_destroy(l_pi,l_nb_pocs);
429                                         opj_free(first_pass_failed);
430                                         return OPJ_FALSE;
431                                 }
432                         }
433
434                         if (first_pass_failed[l_current_pi->compno]) {
435                                 l_img_comp = &(l_image->comps[l_current_pi->compno]);
436                                 if (l_img_comp->resno_decoded == 0)
437                                         l_img_comp->resno_decoded = p_tile->comps[l_current_pi->compno].minimum_num_resolutions - 1;
438                         }
439
440                         l_current_data += l_nb_bytes_read;
441                         p_max_len -= l_nb_bytes_read;
442
443                         /* INDEX >> */
444 #ifdef TODO_MSD
445                         if(p_cstr_info) {
446                                 opj_tile_info_v2_t *info_TL = &p_cstr_info->tile[p_tile_no];
447                                 opj_packet_info_t *info_PK = &info_TL->packet[p_cstr_info->packno];
448                                 tp_start_packno = 0;
449                                 if (!p_cstr_info->packno) {
450                                         info_PK->start_pos = info_TL->end_header + 1;
451                                 } 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 */
452                                         info_TL->tp[curtp].tp_numpacks = p_cstr_info->packno - tp_start_packno; /* Number of packets in previous tile-part */
453                                         tp_start_packno = p_cstr_info->packno;
454                                         curtp++;
455                                         info_PK->start_pos = p_cstr_info->tile[p_tile_no].tp[curtp].tp_end_header+1;
456                                 } else {
457                                         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;
458                                 }
459                                 info_PK->end_pos = info_PK->start_pos + l_nb_bytes_read - 1;
460                                 info_PK->end_ph_pos += info_PK->start_pos - 1;  /* End of packet header which now only represents the distance */
461                                 ++p_cstr_info->packno;
462                         }
463 #endif
464                         /* << INDEX */
465                 }
466                 ++l_current_pi;
467
468                 opj_free(first_pass_failed);
469         }
470         /* INDEX >> */
471 #ifdef TODO_MSD
472         if
473                 (p_cstr_info) {
474                 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 */
475         }
476 #endif
477         /* << INDEX */
478
479         /* don't forget to release pi */
480         opj_pi_destroy(l_pi,l_nb_pocs);
481         *p_data_read = (OPJ_UINT32)(l_current_data - p_src);
482         return OPJ_TRUE;
483 }
484
485 /* ----------------------------------------------------------------------- */
486
487 /**
488  * Creates a Tier 2 handle
489  *
490  * @param       p_image         Source or destination image
491  * @param       p_cp            Image coding parameters.
492  * @return              a new T2 handle if successful, NULL otherwise.
493 */
494 opj_t2_t* opj_t2_create(opj_image_t *p_image, opj_cp_t *p_cp)
495 {
496         /* create the t2 structure */
497         opj_t2_t *l_t2 = (opj_t2_t*)opj_calloc(1,sizeof(opj_t2_t));
498         if (!l_t2) {
499                 return NULL;
500         }
501
502         l_t2->image = p_image;
503         l_t2->cp = p_cp;
504
505         return l_t2;
506 }
507
508 void opj_t2_destroy(opj_t2_t *t2) {
509         if(t2) {
510                 opj_free(t2);
511         }
512 }
513
514 OPJ_BOOL opj_t2_decode_packet(  opj_t2_t* p_t2,
515                                 opj_tcd_tile_t *p_tile,
516                                 opj_tcp_t *p_tcp,
517                                 opj_pi_iterator_t *p_pi,
518                                 OPJ_BYTE *p_src,
519                                 OPJ_UINT32 * p_data_read,
520                                 OPJ_UINT32 p_max_length,
521                                 opj_packet_info_t *p_pack_info)
522 {
523         OPJ_BOOL l_read_data;
524         OPJ_UINT32 l_nb_bytes_read = 0;
525         OPJ_UINT32 l_nb_total_bytes_read = 0;
526
527         *p_data_read = 0;
528
529         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)) {
530                 return OPJ_FALSE;
531         }
532
533         p_src += l_nb_bytes_read;
534         l_nb_total_bytes_read += l_nb_bytes_read;
535         p_max_length -= l_nb_bytes_read;
536
537         /* we should read data for the packet */
538         if (l_read_data) {
539                 l_nb_bytes_read = 0;
540
541                 if (! opj_t2_read_packet_data(p_t2,p_tile,p_pi,p_src,&l_nb_bytes_read,p_max_length,p_pack_info)) {
542                         return OPJ_FALSE;
543                 }
544
545                 l_nb_total_bytes_read += l_nb_bytes_read;
546         }
547
548         *p_data_read = l_nb_total_bytes_read;
549
550         return OPJ_TRUE;
551 }
552
553 OPJ_BOOL opj_t2_encode_packet(  OPJ_UINT32 tileno,
554                                 opj_tcd_tile_t * tile,
555                                 opj_tcp_t * tcp,
556                                 opj_pi_iterator_t *pi,
557                                 OPJ_BYTE *dest,
558                                 OPJ_UINT32 * p_data_written,
559                                 OPJ_UINT32 length,
560                                 opj_codestream_info_t *cstr_info)
561 {
562         OPJ_UINT32 bandno, cblkno;
563         OPJ_BYTE* c = dest;
564         OPJ_UINT32 l_nb_bytes;
565         OPJ_UINT32 compno = pi->compno;     /* component value */
566         OPJ_UINT32 resno  = pi->resno;      /* resolution level value */
567         OPJ_UINT32 precno = pi->precno;     /* precinct value */
568         OPJ_UINT32 layno  = pi->layno;      /* quality layer value */
569         OPJ_UINT32 l_nb_blocks;
570         opj_tcd_band_t *band = 00;
571         opj_tcd_cblk_enc_t* cblk = 00;
572         opj_tcd_pass_t *pass = 00;
573
574         opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
575         opj_tcd_resolution_t *res = &tilec->resolutions[resno];
576
577         opj_bio_t *bio = 00;    /* BIO component */
578
579         /* <SOP 0xff91> */
580         if (tcp->csty & J2K_CP_CSTY_SOP) {
581                 c[0] = 255;
582                 c[1] = 145;
583                 c[2] = 0;
584                 c[3] = 4;
585 #if 0
586                 c[4] = (tile->packno % 65536) / 256;
587                 c[5] = (tile->packno % 65536) % 256;
588 #else
589                 c[4] = (tile->packno >> 8) & 0xff; /* packno is uint32_t */
590                 c[5] = tile->packno & 0xff;
591 #endif
592                 c += 6;
593                 length -= 6;
594         }
595         /* </SOP> */
596
597         if (!layno) {
598                 band = res->bands;
599
600                 for(bandno = 0; bandno < res->numbands; ++bandno) {
601                         opj_tcd_precinct_t *prc = &band->precincts[precno];
602
603                         opj_tgt_reset(prc->incltree);
604                         opj_tgt_reset(prc->imsbtree);
605
606                         l_nb_blocks = prc->cw * prc->ch;
607                         for     (cblkno = 0; cblkno < l_nb_blocks; ++cblkno) {
608                                 cblk = &prc->cblks.enc[cblkno];
609
610                                 cblk->numpasses = 0;
611                                 opj_tgt_setvalue(prc->imsbtree, cblkno, band->numbps - (OPJ_INT32)cblk->numbps);
612                         }
613                         ++band;
614                 }
615         }
616
617         bio = opj_bio_create();
618         if (!bio) {
619                 /* FIXME event manager error callback */
620                 return OPJ_FALSE;
621         }
622         opj_bio_init_enc(bio, c, length);
623         opj_bio_write(bio, 1, 1);           /* Empty header bit */
624
625         /* Writing Packet header */
626         band = res->bands;
627         for (bandno = 0; bandno < res->numbands; ++bandno)      {
628                 opj_tcd_precinct_t *prc = &band->precincts[precno];
629
630                 l_nb_blocks = prc->cw * prc->ch;
631                 cblk = prc->cblks.enc;
632
633                 for (cblkno = 0; cblkno < l_nb_blocks; ++cblkno) {
634                         opj_tcd_layer_t *layer = &cblk->layers[layno];
635
636                         if (!cblk->numpasses && layer->numpasses) {
637                                 opj_tgt_setvalue(prc->incltree, cblkno, (OPJ_INT32)layno);
638                         }
639
640                         ++cblk;
641                 }
642
643                 cblk = prc->cblks.enc;
644                 for (cblkno = 0; cblkno < l_nb_blocks; cblkno++) {
645                         opj_tcd_layer_t *layer = &cblk->layers[layno];
646                         OPJ_UINT32 increment = 0;
647                         OPJ_UINT32 nump = 0;
648                         OPJ_UINT32 len = 0, passno;
649                         OPJ_UINT32 l_nb_passes;
650
651                         /* cblk inclusion bits */
652                         if (!cblk->numpasses) {
653                                 opj_tgt_encode(bio, prc->incltree, cblkno, (OPJ_INT32)(layno + 1));
654                         } else {
655                                 opj_bio_write(bio, layer->numpasses != 0, 1);
656                         }
657
658                         /* if cblk not included, go to the next cblk  */
659                         if (!layer->numpasses) {
660                                 ++cblk;
661                                 continue;
662                         }
663
664                         /* if first instance of cblk --> zero bit-planes information */
665                         if (!cblk->numpasses) {
666                                 cblk->numlenbits = 3;
667                                 opj_tgt_encode(bio, prc->imsbtree, cblkno, 999);
668                         }
669
670                         /* number of coding passes included */
671                         opj_t2_putnumpasses(bio, layer->numpasses);
672                         l_nb_passes = cblk->numpasses + layer->numpasses;
673                         pass = cblk->passes +  cblk->numpasses;
674
675                         /* computation of the increase of the length indicator and insertion in the header     */
676                         for (passno = cblk->numpasses; passno < l_nb_passes; ++passno) {
677                                 ++nump;
678                                 len += pass->len;
679
680                                 if (pass->term || passno == (cblk->numpasses + layer->numpasses) - 1) {
681                                   increment = (OPJ_UINT32)opj_int_max((OPJ_INT32)increment, opj_int_floorlog2((OPJ_INT32)len) + 1
682                                     - ((OPJ_INT32)cblk->numlenbits + opj_int_floorlog2((OPJ_INT32)nump)));
683                                         len = 0;
684                                         nump = 0;
685                                 }
686
687                                 ++pass;
688                         }
689                         opj_t2_putcommacode(bio, (OPJ_INT32)increment);
690
691                         /* computation of the new Length indicator */
692                         cblk->numlenbits += increment;
693
694                         pass = cblk->passes +  cblk->numpasses;
695                         /* insertion of the codeword segment length */
696                         for (passno = cblk->numpasses; passno < l_nb_passes; ++passno) {
697                                 nump++;
698                                 len += pass->len;
699
700                                 if (pass->term || passno == (cblk->numpasses + layer->numpasses) - 1) {
701                                         opj_bio_write(bio, (OPJ_UINT32)len, cblk->numlenbits + (OPJ_UINT32)opj_int_floorlog2((OPJ_INT32)nump));
702                                         len = 0;
703                                         nump = 0;
704                                 }
705                                 ++pass;
706                         }
707
708                         ++cblk;
709                 }
710
711                 ++band;
712         }
713
714         if (!opj_bio_flush(bio)) {
715                 opj_bio_destroy(bio);
716                 return OPJ_FALSE;               /* modified to eliminate longjmp !! */
717         }
718
719         l_nb_bytes = (OPJ_UINT32)opj_bio_numbytes(bio);
720         c += l_nb_bytes;
721         length -= l_nb_bytes;
722
723         opj_bio_destroy(bio);
724
725         /* <EPH 0xff92> */
726         if (tcp->csty & J2K_CP_CSTY_EPH) {
727                 c[0] = 255;
728                 c[1] = 146;
729                 c += 2;
730                 length -= 2;
731         }
732         /* </EPH> */
733
734         /* << INDEX */
735         /* End of packet header position. Currently only represents the distance to start of packet
736            Will be updated later by incrementing with packet start value*/
737         if(cstr_info && cstr_info->index_write) {
738                 opj_packet_info_t *info_PK = &cstr_info->tile[tileno].packet[cstr_info->packno];
739                 info_PK->end_ph_pos = (OPJ_INT32)(c - dest);
740         }
741         /* INDEX >> */
742
743         /* Writing the packet body */
744         band = res->bands;
745         for (bandno = 0; bandno < res->numbands; bandno++) {
746                 opj_tcd_precinct_t *prc = &band->precincts[precno];
747
748                 l_nb_blocks = prc->cw * prc->ch;
749                 cblk = prc->cblks.enc;
750
751                 for (cblkno = 0; cblkno < l_nb_blocks; ++cblkno) {
752                         opj_tcd_layer_t *layer = &cblk->layers[layno];
753
754                         if (!layer->numpasses) {
755                                 ++cblk;
756                                 continue;
757                         }
758
759                         if (layer->len > length) {
760                                 return OPJ_FALSE;
761                         }
762
763                         memcpy(c, layer->data, layer->len);
764                         cblk->numpasses += layer->numpasses;
765                         c += layer->len;
766                         length -= layer->len;
767
768                         /* << INDEX */
769                         if(cstr_info && cstr_info->index_write) {
770                                 opj_packet_info_t *info_PK = &cstr_info->tile[tileno].packet[cstr_info->packno];
771                                 info_PK->disto += layer->disto;
772                                 if (cstr_info->D_max < info_PK->disto) {
773                                         cstr_info->D_max = info_PK->disto;
774                                 }
775                         }
776
777                         ++cblk;
778                         /* INDEX >> */
779                 }
780                 ++band;
781         }
782
783         assert( c >= dest );
784         * p_data_written += (OPJ_UINT32)(c - dest);
785
786         return OPJ_TRUE;
787 }
788
789 static OPJ_BOOL opj_t2_skip_packet( opj_t2_t* p_t2,
790                                     opj_tcd_tile_t *p_tile,
791                                     opj_tcp_t *p_tcp,
792                                     opj_pi_iterator_t *p_pi,
793                                     OPJ_BYTE *p_src,
794                                     OPJ_UINT32 * p_data_read,
795                                     OPJ_UINT32 p_max_length,
796                                     opj_packet_info_t *p_pack_info)
797 {
798         OPJ_BOOL l_read_data;
799         OPJ_UINT32 l_nb_bytes_read = 0;
800         OPJ_UINT32 l_nb_total_bytes_read = 0;
801
802         *p_data_read = 0;
803
804         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)) {
805                 return OPJ_FALSE;
806         }
807
808         p_src += l_nb_bytes_read;
809         l_nb_total_bytes_read += l_nb_bytes_read;
810         p_max_length -= l_nb_bytes_read;
811
812         /* we should read data for the packet */
813         if (l_read_data) {
814                 l_nb_bytes_read = 0;
815
816                 if (! opj_t2_skip_packet_data(p_t2,p_tile,p_pi,&l_nb_bytes_read,p_max_length,p_pack_info)) {
817                         return OPJ_FALSE;
818                 }
819
820                 l_nb_total_bytes_read += l_nb_bytes_read;
821         }
822         *p_data_read = l_nb_total_bytes_read;
823
824         return OPJ_TRUE;
825 }
826
827
828 OPJ_BOOL opj_t2_read_packet_header( opj_t2_t* p_t2,
829                                     opj_tcd_tile_t *p_tile,
830                                     opj_tcp_t *p_tcp,
831                                     opj_pi_iterator_t *p_pi,
832                                     OPJ_BOOL * p_is_data_present,
833                                     OPJ_BYTE *p_src_data,
834                                     OPJ_UINT32 * p_data_read,
835                                     OPJ_UINT32 p_max_length,
836                                     opj_packet_info_t *p_pack_info)
837
838 {
839         /* loop */
840         OPJ_UINT32 bandno, cblkno;
841         OPJ_UINT32 l_nb_code_blocks;
842         OPJ_UINT32 l_remaining_length;
843         OPJ_UINT32 l_header_length;
844         OPJ_UINT32 * l_modified_length_ptr = 00;
845         OPJ_BYTE *l_current_data = p_src_data;
846         opj_cp_t *l_cp = p_t2->cp;
847         opj_bio_t *l_bio = 00;  /* BIO component */
848         opj_tcd_band_t *l_band = 00;
849         opj_tcd_cblk_dec_t* l_cblk = 00;
850         opj_tcd_resolution_t* l_res = &p_tile->comps[p_pi->compno].resolutions[p_pi->resno];
851
852         OPJ_BYTE *l_header_data = 00;
853         OPJ_BYTE **l_header_data_start = 00;
854
855         OPJ_UINT32 l_present;
856
857         if (p_pi->layno == 0) {
858                 l_band = l_res->bands;
859
860                 /* reset tagtrees */
861                 for (bandno = 0; bandno < l_res->numbands; ++bandno) {
862                         opj_tcd_precinct_t *l_prc = &l_band->precincts[p_pi->precno];
863
864                         if ( ! ((l_band->x1-l_band->x0 == 0)||(l_band->y1-l_band->y0 == 0)) ) {
865                                 opj_tgt_reset(l_prc->incltree);
866                                 opj_tgt_reset(l_prc->imsbtree);
867                                 l_cblk = l_prc->cblks.dec;
868
869                                 l_nb_code_blocks = l_prc->cw * l_prc->ch;
870                                 for (cblkno = 0; cblkno < l_nb_code_blocks; ++cblkno) {
871                                         l_cblk->numsegs = 0;
872                                         l_cblk->real_num_segs = 0;
873                                         ++l_cblk;
874                                 }
875                         }
876
877                         ++l_band;
878                 }
879         }
880
881         /* SOP markers */
882
883         if (p_tcp->csty & J2K_CP_CSTY_SOP) {
884                 if (p_max_length < 6) {
885                         /* TODO opj_event_msg(p_t2->cinfo->event_mgr, EVT_WARNING, "Not enough space for expected SOP marker\n"); */
886                         fprintf(stderr, "Not enough space for expected SOP marker\n");
887                 } else if ((*l_current_data) != 0xff || (*(l_current_data + 1) != 0x91)) {
888                         /* TODO opj_event_msg(p_t2->cinfo->event_mgr, EVT_WARNING, "Expected SOP marker\n"); */
889                         fprintf(stderr, "Warning: expected SOP marker\n");
890                 } else {
891                         l_current_data += 6;
892                 }
893
894                 /** TODO : check the Nsop value */
895         }
896
897         /*
898         When the marker PPT/PPM is used the packet header are store in PPT/PPM marker
899         This part deal with this caracteristic
900         step 1: Read packet header in the saved structure
901         step 2: Return to codestream for decoding
902         */
903
904         l_bio = opj_bio_create();
905         if (! l_bio) {
906                 return OPJ_FALSE;
907         }
908
909         if (l_cp->ppm == 1) { /* PPM */
910                 l_header_data_start = &l_cp->ppm_data;
911                 l_header_data = *l_header_data_start;
912                 l_modified_length_ptr = &(l_cp->ppm_len);
913
914         }
915         else if (p_tcp->ppt == 1) { /* PPT */
916                 l_header_data_start = &(p_tcp->ppt_data);
917                 l_header_data = *l_header_data_start;
918                 l_modified_length_ptr = &(p_tcp->ppt_len);
919         }
920         else {  /* Normal Case */
921                 l_header_data_start = &(l_current_data);
922                 l_header_data = *l_header_data_start;
923                 l_remaining_length = (OPJ_UINT32)(p_src_data+p_max_length-l_header_data);
924                 l_modified_length_ptr = &(l_remaining_length);
925         }
926
927         opj_bio_init_dec(l_bio, l_header_data,*l_modified_length_ptr);
928
929         l_present = opj_bio_read(l_bio, 1);
930         JAS_FPRINTF(stderr, "present=%d \n", l_present );
931         if (!l_present) {
932             /* TODO MSD: no test to control the output of this function*/
933                 opj_bio_inalign(l_bio);
934                 l_header_data += opj_bio_numbytes(l_bio);
935                 opj_bio_destroy(l_bio);
936
937                 /* EPH markers */
938                 if (p_tcp->csty & J2K_CP_CSTY_EPH) {
939                         if ((*l_modified_length_ptr - (OPJ_UINT32)(l_header_data - *l_header_data_start)) < 2U) {
940                                 fprintf(stderr, "Not enough space for expected EPH marker\n");
941                         } else if ((*l_header_data) != 0xff || (*(l_header_data + 1) != 0x92)) {
942                                 fprintf(stderr, "Error : expected EPH marker\n");
943                         } else {
944                                 l_header_data += 2;
945                         }
946                 }
947
948                 l_header_length = (OPJ_UINT32)(l_header_data - *l_header_data_start);
949                 *l_modified_length_ptr -= l_header_length;
950                 *l_header_data_start += l_header_length;
951
952                 /* << INDEX */
953                 /* End of packet header position. Currently only represents the distance to start of packet
954                    Will be updated later by incrementing with packet start value */
955                 if (p_pack_info) {
956                         p_pack_info->end_ph_pos = (OPJ_INT32)(l_current_data - p_src_data);
957                 }
958                 /* INDEX >> */
959
960                 * p_is_data_present = OPJ_FALSE;
961                 *p_data_read = (OPJ_UINT32)(l_current_data - p_src_data);
962                 return OPJ_TRUE;
963         }
964
965         l_band = l_res->bands;
966         for (bandno = 0; bandno < l_res->numbands; ++bandno) {
967                 opj_tcd_precinct_t *l_prc = &(l_band->precincts[p_pi->precno]);
968
969                 if ((l_band->x1-l_band->x0 == 0)||(l_band->y1-l_band->y0 == 0)) {
970                         ++l_band;
971                         continue;
972                 }
973
974                 l_nb_code_blocks = l_prc->cw * l_prc->ch;
975                 l_cblk = l_prc->cblks.dec;
976                 for (cblkno = 0; cblkno < l_nb_code_blocks; cblkno++) {
977                         OPJ_UINT32 l_included,l_increment, l_segno;
978                         OPJ_INT32 n;
979
980                         /* if cblk not yet included before --> inclusion tagtree */
981                         if (!l_cblk->numsegs) {
982                                 l_included = opj_tgt_decode(l_bio, l_prc->incltree, cblkno, (OPJ_INT32)(p_pi->layno + 1));
983                                 /* else one bit */
984                         }
985                         else {
986                                 l_included = opj_bio_read(l_bio, 1);
987                         }
988
989                         /* if cblk not included */
990                         if (!l_included) {
991                                 l_cblk->numnewpasses = 0;
992                                 ++l_cblk;
993         JAS_FPRINTF(stderr, "included=%d \n", l_included);
994                                 continue;
995                         }
996
997                         /* if cblk not yet included --> zero-bitplane tagtree */
998                         if (!l_cblk->numsegs) {
999                                 OPJ_UINT32 i = 0;
1000
1001                                 while (!opj_tgt_decode(l_bio, l_prc->imsbtree, cblkno, (OPJ_INT32)i)) {
1002                                         ++i;
1003                                 }
1004
1005                                 l_cblk->numbps = (OPJ_UINT32)l_band->numbps + 1 - i;
1006                                 l_cblk->numlenbits = 3;
1007                         }
1008
1009                         /* number of coding passes */
1010                         l_cblk->numnewpasses = opj_t2_getnumpasses(l_bio);
1011                         l_increment = opj_t2_getcommacode(l_bio);
1012
1013                         /* length indicator increment */
1014                         l_cblk->numlenbits += l_increment;
1015                         l_segno = 0;
1016
1017                         if (!l_cblk->numsegs) {
1018                                 if (! opj_t2_init_seg(l_cblk, l_segno, p_tcp->tccps[p_pi->compno].cblksty, 1)) {
1019                                         opj_bio_destroy(l_bio);
1020                                         return OPJ_FALSE;
1021                                 }
1022                         }
1023                         else {
1024                                 l_segno = l_cblk->numsegs - 1;
1025                                 if (l_cblk->segs[l_segno].numpasses == l_cblk->segs[l_segno].maxpasses) {
1026                                         ++l_segno;
1027                                         if (! opj_t2_init_seg(l_cblk, l_segno, p_tcp->tccps[p_pi->compno].cblksty, 0)) {
1028                                                 opj_bio_destroy(l_bio);
1029                                                 return OPJ_FALSE;
1030                                         }
1031                                 }
1032                         }
1033                         n = (OPJ_INT32)l_cblk->numnewpasses;
1034
1035                         do {
1036                                 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);
1037                                 l_cblk->segs[l_segno].newlen = opj_bio_read(l_bio, l_cblk->numlenbits + opj_uint_floorlog2(l_cblk->segs[l_segno].numnewpasses));
1038                                         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 );
1039
1040                                 n -= (OPJ_INT32)l_cblk->segs[l_segno].numnewpasses;
1041                                 if (n > 0) {
1042                                         ++l_segno;
1043
1044                                         if (! opj_t2_init_seg(l_cblk, l_segno, p_tcp->tccps[p_pi->compno].cblksty, 0)) {
1045                                                 opj_bio_destroy(l_bio);
1046                                                 return OPJ_FALSE;
1047                                         }
1048                                 }
1049                         } while (n > 0);
1050
1051                         ++l_cblk;
1052                 }
1053
1054                 ++l_band;
1055         }
1056
1057         if (!opj_bio_inalign(l_bio)) {
1058                 opj_bio_destroy(l_bio);
1059                 return OPJ_FALSE;
1060         }
1061
1062         l_header_data += opj_bio_numbytes(l_bio);
1063         opj_bio_destroy(l_bio);
1064
1065         /* EPH markers */
1066         if (p_tcp->csty & J2K_CP_CSTY_EPH) {
1067                 if ((*l_modified_length_ptr - (OPJ_UINT32)(l_header_data - *l_header_data_start)) < 2U) {
1068                         fprintf(stderr, "Not enough space for expected EPH marker\n");
1069                 } else if ((*l_header_data) != 0xff || (*(l_header_data + 1) != 0x92)) {
1070                         /* TODO opj_event_msg(t2->cinfo->event_mgr, EVT_ERROR, "Expected EPH marker\n"); */
1071                         fprintf(stderr, "Error : expected EPH marker\n");
1072                 } else {
1073                         l_header_data += 2;
1074                 }
1075         }
1076
1077         l_header_length = (OPJ_UINT32)(l_header_data - *l_header_data_start);
1078         JAS_FPRINTF( stderr, "hdrlen=%d \n", l_header_length );
1079         JAS_FPRINTF( stderr, "packet body\n");
1080         *l_modified_length_ptr -= l_header_length;
1081         *l_header_data_start += l_header_length;
1082
1083         /* << INDEX */
1084         /* End of packet header position. Currently only represents the distance to start of packet
1085          Will be updated later by incrementing with packet start value */
1086         if (p_pack_info) {
1087                 p_pack_info->end_ph_pos = (OPJ_INT32)(l_current_data - p_src_data);
1088         }
1089         /* INDEX >> */
1090
1091         *p_is_data_present = OPJ_TRUE;
1092         *p_data_read = (OPJ_UINT32)(l_current_data - p_src_data);
1093
1094         return OPJ_TRUE;
1095 }
1096
1097 OPJ_BOOL opj_t2_read_packet_data(   opj_t2_t* p_t2,
1098                                     opj_tcd_tile_t *p_tile,
1099                                     opj_pi_iterator_t *p_pi,
1100                                     OPJ_BYTE *p_src_data,
1101                                     OPJ_UINT32 * p_data_read,
1102                                     OPJ_UINT32 p_max_length,
1103                                     opj_packet_info_t *pack_info)
1104 {
1105         OPJ_UINT32 bandno, cblkno;
1106         OPJ_UINT32 l_nb_code_blocks;
1107         OPJ_BYTE *l_current_data = p_src_data;
1108         opj_tcd_band_t *l_band = 00;
1109         opj_tcd_cblk_dec_t* l_cblk = 00;
1110         opj_tcd_resolution_t* l_res = &p_tile->comps[p_pi->compno].resolutions[p_pi->resno];
1111
1112         OPJ_ARG_NOT_USED(p_t2);
1113         OPJ_ARG_NOT_USED(pack_info);
1114
1115         l_band = l_res->bands;
1116         for (bandno = 0; bandno < l_res->numbands; ++bandno) {
1117                 opj_tcd_precinct_t *l_prc = &l_band->precincts[p_pi->precno];
1118
1119                 if ((l_band->x1-l_band->x0 == 0)||(l_band->y1-l_band->y0 == 0)) {
1120                         ++l_band;
1121                         continue;
1122                 }
1123
1124                 l_nb_code_blocks = l_prc->cw * l_prc->ch;
1125                 l_cblk = l_prc->cblks.dec;
1126
1127                 for (cblkno = 0; cblkno < l_nb_code_blocks; ++cblkno) {
1128                         opj_tcd_seg_t *l_seg = 00;
1129
1130                         if (!l_cblk->numnewpasses) {
1131                                 /* nothing to do */
1132                                 ++l_cblk;
1133                                 continue;
1134                         }
1135
1136                         if (!l_cblk->numsegs) {
1137                                 l_seg = l_cblk->segs;
1138                                 ++l_cblk->numsegs;
1139                                 l_cblk->data_current_size = 0;
1140                         }
1141                         else {
1142                                 l_seg = &l_cblk->segs[l_cblk->numsegs - 1];
1143
1144                                 if (l_seg->numpasses == l_seg->maxpasses) {
1145                                         ++l_seg;
1146                                         ++l_cblk->numsegs;
1147                                 }
1148                         }
1149
1150                         do {
1151                                 /* Check possible overflow (on l_current_data only, assumes input args already checked) then size */
1152                                 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)) {
1153                                         fprintf(stderr, "read: segment too long (%d) with max (%d) for codeblock %d (p=%d, b=%d, r=%d, c=%d)\n",
1154                                                 l_seg->newlen, p_max_length, cblkno, p_pi->precno, bandno, p_pi->resno, p_pi->compno);
1155                                         return OPJ_FALSE;
1156                                 }
1157
1158 #ifdef USE_JPWL
1159                         /* we need here a j2k handle to verify if making a check to
1160                         the validity of cblocks parameters is selected from user (-W) */
1161
1162                                 /* let's check that we are not exceeding */
1163                                 if ((l_cblk->len + l_seg->newlen) > 8192) {
1164                                         opj_event_msg(p_t2->cinfo, EVT_WARNING,
1165                                                 "JPWL: segment too long (%d) for codeblock %d (p=%d, b=%d, r=%d, c=%d)\n",
1166                                                 l_seg->newlen, cblkno, p_pi->precno, bandno, p_pi->resno, p_pi->compno);
1167                                         if (!JPWL_ASSUME) {
1168                                                 opj_event_msg(p_t2->cinfo, EVT_ERROR, "JPWL: giving up\n");
1169                                                 return OPJ_FALSE;
1170                                         }
1171                                         l_seg->newlen = 8192 - l_cblk->len;
1172                                         opj_event_msg(p_t2->cinfo, EVT_WARNING, "      - truncating segment to %d\n", l_seg->newlen);
1173                                         break;
1174                                 };
1175
1176 #endif /* USE_JPWL */
1177                                 /* Check possible overflow on size */
1178                                 if ((l_cblk->data_current_size + l_seg->newlen) < l_cblk->data_current_size) {
1179                                         fprintf(stderr, "read: segment too long (%d) with current size (%d > %d) for codeblock %d (p=%d, b=%d, r=%d, c=%d)\n",
1180                                                 l_seg->newlen, l_cblk->data_current_size, 0xFFFFFFFF - l_seg->newlen, cblkno, p_pi->precno, bandno, p_pi->resno, p_pi->compno);
1181                                         return OPJ_FALSE;
1182                                 }
1183                                 /* Check if the cblk->data have allocated enough memory */
1184                                 if ((l_cblk->data_current_size + l_seg->newlen) > l_cblk->data_max_size) {
1185                                     OPJ_BYTE* new_cblk_data = (OPJ_BYTE*) opj_realloc(l_cblk->data, l_cblk->data_current_size + l_seg->newlen);
1186                                     if(! new_cblk_data) {
1187                                         opj_free(l_cblk->data);
1188                                         l_cblk->data = NULL;
1189                                         l_cblk->data_max_size = 0;
1190                                         /* opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to realloc code block cata!\n"); */
1191                                         return OPJ_FALSE;
1192                                     }
1193                                     l_cblk->data_max_size = l_cblk->data_current_size + l_seg->newlen;
1194                                     l_cblk->data = new_cblk_data;
1195                                 }
1196                                
1197                                 memcpy(l_cblk->data + l_cblk->data_current_size, l_current_data, l_seg->newlen);
1198
1199                                 if (l_seg->numpasses == 0) {
1200                                         l_seg->data = &l_cblk->data;
1201                                         l_seg->dataindex = l_cblk->data_current_size;
1202                                 }
1203
1204                                 l_current_data += l_seg->newlen;
1205                                 l_seg->numpasses += l_seg->numnewpasses;
1206                                 l_cblk->numnewpasses -= l_seg->numnewpasses;
1207
1208                                 l_seg->real_num_passes = l_seg->numpasses;
1209                                 l_cblk->data_current_size += l_seg->newlen;
1210                                 l_seg->len += l_seg->newlen;
1211
1212                                 if (l_cblk->numnewpasses > 0) {
1213                                         ++l_seg;
1214                                         ++l_cblk->numsegs;
1215                                 }
1216                         } while (l_cblk->numnewpasses > 0);
1217
1218                         l_cblk->real_num_segs = l_cblk->numsegs;
1219                         ++l_cblk;
1220                 } /* next code_block */
1221
1222                 ++l_band;
1223         }
1224
1225         *(p_data_read) = (OPJ_UINT32)(l_current_data - p_src_data);
1226
1227
1228         return OPJ_TRUE;
1229 }
1230
1231 OPJ_BOOL opj_t2_skip_packet_data(   opj_t2_t* p_t2,
1232                                     opj_tcd_tile_t *p_tile,
1233                                     opj_pi_iterator_t *p_pi,
1234                                     OPJ_UINT32 * p_data_read,
1235                                     OPJ_UINT32 p_max_length,
1236                                     opj_packet_info_t *pack_info)
1237 {
1238         OPJ_UINT32 bandno, cblkno;
1239         OPJ_UINT32 l_nb_code_blocks;
1240         opj_tcd_band_t *l_band = 00;
1241         opj_tcd_cblk_dec_t* l_cblk = 00;
1242         opj_tcd_resolution_t* l_res = &p_tile->comps[p_pi->compno].resolutions[p_pi->resno];
1243
1244         OPJ_ARG_NOT_USED(p_t2);
1245         OPJ_ARG_NOT_USED(pack_info);
1246
1247         *p_data_read = 0;
1248         l_band = l_res->bands;
1249
1250         for (bandno = 0; bandno < l_res->numbands; ++bandno) {
1251                 opj_tcd_precinct_t *l_prc = &l_band->precincts[p_pi->precno];
1252
1253                 if ((l_band->x1-l_band->x0 == 0)||(l_band->y1-l_band->y0 == 0)) {
1254                         ++l_band;
1255                         continue;
1256                 }
1257
1258                 l_nb_code_blocks = l_prc->cw * l_prc->ch;
1259                 l_cblk = l_prc->cblks.dec;
1260
1261                 for (cblkno = 0; cblkno < l_nb_code_blocks; ++cblkno) {
1262                         opj_tcd_seg_t *l_seg = 00;
1263
1264                         if (!l_cblk->numnewpasses) {
1265                                 /* nothing to do */
1266                                 ++l_cblk;
1267                                 continue;
1268                         }
1269
1270                         if (!l_cblk->numsegs) {
1271                                 l_seg = l_cblk->segs;
1272                                 ++l_cblk->numsegs;
1273                                 l_cblk->data_current_size = 0;
1274                         }
1275                         else {
1276                                 l_seg = &l_cblk->segs[l_cblk->numsegs - 1];
1277
1278                                 if (l_seg->numpasses == l_seg->maxpasses) {
1279                                         ++l_seg;
1280                                         ++l_cblk->numsegs;
1281                                 }
1282                         }
1283
1284                         do {
1285                                 /* Check possible overflow then size */
1286                                 if (((*p_data_read + l_seg->newlen) < (*p_data_read)) || ((*p_data_read + l_seg->newlen) > p_max_length)) {
1287                                         fprintf(stderr, "skip: segment too long (%d) with max (%d) for codeblock %d (p=%d, b=%d, r=%d, c=%d)\n",
1288                                                 l_seg->newlen, p_max_length, cblkno, p_pi->precno, bandno, p_pi->resno, p_pi->compno);
1289                                         return OPJ_FALSE;
1290                                 }
1291
1292 #ifdef USE_JPWL
1293                         /* we need here a j2k handle to verify if making a check to
1294                         the validity of cblocks parameters is selected from user (-W) */
1295
1296                                 /* let's check that we are not exceeding */
1297                                 if ((l_cblk->len + l_seg->newlen) > 8192) {
1298                                         opj_event_msg(p_t2->cinfo, EVT_WARNING,
1299                                                 "JPWL: segment too long (%d) for codeblock %d (p=%d, b=%d, r=%d, c=%d)\n",
1300                                                 l_seg->newlen, cblkno, p_pi->precno, bandno, p_pi->resno, p_pi->compno);
1301                                         if (!JPWL_ASSUME) {
1302                                                 opj_event_msg(p_t2->cinfo, EVT_ERROR, "JPWL: giving up\n");
1303                                                 return -999;
1304                                         }
1305                                         l_seg->newlen = 8192 - l_cblk->len;
1306                                         opj_event_msg(p_t2->cinfo, EVT_WARNING, "      - truncating segment to %d\n", l_seg->newlen);
1307                                         break;
1308                                 };
1309
1310 #endif /* USE_JPWL */
1311                                         JAS_FPRINTF(stderr, "p_data_read (%d) newlen (%d) \n", *p_data_read, l_seg->newlen );
1312                                 *(p_data_read) += l_seg->newlen;
1313
1314                                 l_seg->numpasses += l_seg->numnewpasses;
1315                                 l_cblk->numnewpasses -= l_seg->numnewpasses;
1316                                 if (l_cblk->numnewpasses > 0)
1317                                 {
1318                                         ++l_seg;
1319                                         ++l_cblk->numsegs;
1320                                 }
1321                         } while (l_cblk->numnewpasses > 0);
1322
1323                         ++l_cblk;
1324                 }
1325
1326                 ++l_band;
1327         }
1328
1329         return OPJ_TRUE;
1330 }
1331
1332
1333 OPJ_BOOL opj_t2_init_seg(   opj_tcd_cblk_dec_t* cblk,
1334                             OPJ_UINT32 index, 
1335                             OPJ_UINT32 cblksty, 
1336                             OPJ_UINT32 first)
1337 {
1338         opj_tcd_seg_t* seg = 00;
1339         OPJ_UINT32 l_nb_segs = index + 1;
1340
1341         if (l_nb_segs > cblk->m_current_max_segs) {
1342                 opj_tcd_seg_t* new_segs;
1343                 cblk->m_current_max_segs += OPJ_J2K_DEFAULT_NB_SEGS;
1344
1345                 new_segs = (opj_tcd_seg_t*) opj_realloc(cblk->segs, cblk->m_current_max_segs * sizeof(opj_tcd_seg_t));
1346                 if(! new_segs) {
1347                         opj_free(cblk->segs);
1348                         cblk->segs = NULL;
1349                         cblk->m_current_max_segs = 0;
1350                         /* opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to initialize segment %d\n", l_nb_segs); */
1351                         return OPJ_FALSE;
1352                 }
1353                 cblk->segs = new_segs;
1354         }
1355
1356         seg = &cblk->segs[index];
1357         memset(seg,0,sizeof(opj_tcd_seg_t));
1358
1359         if (cblksty & J2K_CCP_CBLKSTY_TERMALL) {
1360                 seg->maxpasses = 1;
1361         }
1362         else if (cblksty & J2K_CCP_CBLKSTY_LAZY) {
1363                 if (first) {
1364                         seg->maxpasses = 10;
1365                 } else {
1366                         seg->maxpasses = (((seg - 1)->maxpasses == 1) || ((seg - 1)->maxpasses == 10)) ? 2 : 1;
1367                 }
1368         } else {
1369                 seg->maxpasses = 109;
1370         }
1371
1372         return OPJ_TRUE;
1373 }