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