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