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