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