[trunk] WIP: manage output image with area correponding to the set decoded area
[openjpeg.git] / libopenjpeg / j2k.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  * Copyright (c) 2006-2007, Parvatha Elangovan
9  * Copyright (c) 2010-2011, Kaori Hagihara
10  * All rights reserved.
11  *
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions
14  * are met:
15  * 1. Redistributions of source code must retain the above copyright
16  *    notice, this list of conditions and the following disclaimer.
17  * 2. Redistributions in binary form must reproduce the above copyright
18  *    notice, this list of conditions and the following disclaimer in the
19  *    documentation and/or other materials provided with the distribution.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
22  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
25  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31  * POSSIBILITY OF SUCH DAMAGE.
32  */
33
34 #include "opj_includes.h"
35
36 /** @defgroup J2K J2K - JPEG-2000 codestream reader/writer */
37 /*@{*/
38
39 /** @name Local static functions */
40 /*@{*/
41
42 /**
43  * Sets up the procedures to do on reading header. Developpers wanting to extend the library can add their own reading procedures.
44  */
45 void j2k_setup_header_reading (opj_j2k_v2_t *p_j2k);
46
47 /**
48  * The read header procedure.
49  */
50 opj_bool j2k_read_header_procedure(
51                                                             opj_j2k_v2_t *p_j2k,
52                                                                 struct opj_stream_private *p_stream,
53                                                                 struct opj_event_mgr * p_manager);
54
55 /**
56  * The default decoding validation procedure without any extension.
57  *
58  * @param       p_j2k                   the jpeg2000 codec to validate.
59  * @param       p_stream                                the input stream to validate.
60  * @param       p_manager               the user event manager.
61  *
62  * @return true if the parameters are correct.
63  */
64 opj_bool j2k_decoding_validation (
65                                                                 opj_j2k_v2_t * p_j2k,
66                                                                 opj_stream_private_t *p_stream,
67                                                                 opj_event_mgr_t * p_manager
68                                                         );
69
70 /**
71  * Sets up the validation ,i.e. adds the procedures to lauch to make sure the codec parameters
72  * are valid. Developpers wanting to extend the library can add their own validation procedures.
73  */
74 static void j2k_setup_decoding_validation (opj_j2k_v2_t *p_j2k);
75
76 /**
77  * Builds the tcd decoder to use to decode tile.
78  */
79 opj_bool j2k_build_decoder (
80                                                 opj_j2k_v2_t * p_j2k,
81                                                 opj_stream_private_t *p_stream,
82                                                 opj_event_mgr_t * p_manager
83                                                 );
84
85 /**
86  * Excutes the given procedures on the given codec.
87  *
88  * @param       p_procedure_list        the list of procedures to execute
89  * @param       p_j2k                                   the jpeg2000 codec to execute the procedures on.
90  * @param       p_stream                                        the stream to execute the procedures on.
91  * @param       p_manager                       the user manager.
92  *
93  * @return      true                            if all the procedures were successfully executed.
94  */
95 static opj_bool j2k_exec (
96                                         opj_j2k_v2_t * p_j2k,
97                                         opj_procedure_list_t * p_procedure_list,
98                                         opj_stream_private_t *p_stream,
99                                         opj_event_mgr_t * p_manager
100                                   );
101
102 /**
103  * Copies the decoding tile parameters onto all the tile parameters.
104  * Creates also the tile decoder.
105  */
106 opj_bool j2k_copy_default_tcp_and_create_tcd (  opj_j2k_v2_t * p_j2k,
107                                                                                                 opj_stream_private_t *p_stream,
108                                                                                                 opj_event_mgr_t * p_manager );
109
110 /**
111  * Reads the lookup table containing all the marker, status and action, and returns the handler associated
112  * with the marker value.
113  * @param       p_id            Marker value to look up
114  *
115  * @return      the handler associated with the id.
116 */
117 static const struct opj_dec_memory_marker_handler * j2k_get_marker_handler (OPJ_UINT32 p_id);
118
119 /**
120  * Destroys a tile coding parameter structure.
121  *
122  * @param       p_tcp           the tile coding parameter to destroy.
123  */
124 static void j2k_tcp_destroy (opj_tcp_v2_t *p_tcp);
125
126
127 /**
128  * Destroys a coding parameter structure.
129  *
130  * @param       p_cp            the coding parameter to destroy.
131  */
132 static void j2k_cp_destroy (opj_cp_v2_t *p_cp);
133
134
135 /**
136  * Reads a SPCod or SPCoc element, i.e. the coding style of a given component of a tile.
137  * @param       p_header_data   the data contained in the COM box.
138  * @param       p_j2k                   the jpeg2000 codec.
139  * @param       p_header_size   the size of the data contained in the COM marker.
140  * @param       p_manager               the user event manager.
141 */
142 static opj_bool j2k_read_SPCod_SPCoc(
143                                                         opj_j2k_v2_t *p_j2k,
144                                                         OPJ_UINT32 compno,
145                                                         OPJ_BYTE * p_header_data,
146                                                         OPJ_UINT32 * p_header_size,
147                                                         struct opj_event_mgr * p_manager
148                                                         );
149
150 /**
151  * Reads a SQcd or SQcc element, i.e. the quantization values of a band in the QCD or QCC.
152  *
153  * @param       p_tile_no               the tile to output.
154  * @param       p_comp_no               the component number to output.
155  * @param       p_data                  the data buffer.
156  * @param       p_header_size   pointer to the size of the data buffer, it is changed by the function.
157  * @param       p_j2k                           J2K codec.
158  * @param       p_manager               the user event manager.
159  *
160 */
161 static opj_bool j2k_read_SQcd_SQcc(
162                                                         opj_j2k_v2_t *p_j2k,
163                                                         OPJ_UINT32 compno,
164                                                         OPJ_BYTE * p_header_data,
165                                                         OPJ_UINT32 * p_header_size,
166                                                         struct opj_event_mgr * p_manager
167                                         );
168
169 /**
170  * Copies the tile component parameters of all the component from the first tile component.
171  *
172  * @param               p_j2k           the J2k codec.
173  */
174 static void j2k_copy_tile_component_parameters(
175                                                         opj_j2k_v2_t *p_j2k
176                                                         );
177
178 /**
179  * Copies the tile quantization parameters of all the component from the first tile component.
180  *
181  * @param               p_j2k           the J2k codec.
182  */
183 static void j2k_copy_tile_quantization_parameters(
184                                                         opj_j2k_v2_t *p_j2k
185                                                         );
186
187 /**
188  * Reads the tiles.
189  */
190 opj_bool j2k_decode_tiles (             opj_j2k_v2_t *p_j2k,
191                                                                 opj_stream_private_t *p_stream,
192                                                                 opj_event_mgr_t * p_manager);
193
194 static opj_bool j2k_update_image_data (opj_tcd_v2_t * p_tcd, OPJ_BYTE * p_data, opj_image_t* p_output_image);
195
196
197 /*
198  * -----------------------------------------------------------------------
199  * -----------------------------------------------------------------------
200  * -----------------------------------------------------------------------
201  */
202
203 /**
204 Write the SOC marker (Start Of Codestream)
205 @param j2k J2K handle
206 */
207 static void j2k_write_soc(opj_j2k_t *j2k);
208 /**
209 Read the SOC marker (Start of Codestream)
210 @param j2k J2K handle
211 */
212 static void j2k_read_soc(opj_j2k_t *j2k);
213
214 /**
215  * Reads a SOC marker (Start of Codestream)
216  * @param       p_header_data   the data contained in the SOC box.
217  * @param       jp2                             the jpeg2000 file codec.
218  * @param       p_header_size   the size of the data contained in the SOC marker.
219  * @param       p_manager               the user event manager.
220 */
221 static opj_bool j2k_read_soc_v2(
222                                         opj_j2k_v2_t *p_j2k,
223                                         struct opj_stream_private *p_stream,
224                                         struct opj_event_mgr * p_manager
225                                  );
226
227 /**
228 Write the SIZ marker (image and tile size)
229 @param j2k J2K handle
230 */
231 static void j2k_write_siz(opj_j2k_t *j2k);
232 /**
233 Read the SIZ marker (image and tile size)
234 @param j2k J2K handle
235 */
236 static void j2k_read_siz(opj_j2k_t *j2k);
237
238 /**
239  * Reads a SIZ marker (image and tile size)
240  * @param       p_header_data   the data contained in the SIZ box.
241  * @param       jp2                             the jpeg2000 file codec.
242  * @param       p_header_size   the size of the data contained in the SIZ marker.
243  * @param       p_manager               the user event manager.
244 */
245 static opj_bool j2k_read_siz_v2 (
246                                                   opj_j2k_v2_t *p_j2k,
247                                                   OPJ_BYTE * p_header_data,
248                                                   OPJ_UINT32 p_header_size,
249                                                   struct opj_event_mgr * p_manager
250                                         );
251
252 /**
253 Write the COM marker (comment)
254 @param j2k J2K handle
255 */
256 static void j2k_write_com(opj_j2k_t *j2k);
257 /**
258 Read the COM marker (comment)
259 @param j2k J2K handle
260 */
261 static void j2k_read_com(opj_j2k_t *j2k);
262 /**
263  * Reads a COM marker (comments)
264  * @param       p_header_data   the data contained in the COM box.
265  * @param       jp2                             the jpeg2000 file codec.
266  * @param       p_header_size   the size of the data contained in the COM marker.
267  * @param       p_manager               the user event manager.
268 */
269 static opj_bool j2k_read_com_v2 (
270                                         opj_j2k_v2_t *p_j2k,
271                                         OPJ_BYTE * p_header_data,
272                                         OPJ_UINT32 p_header_size,
273                                         struct opj_event_mgr * p_manager
274                                         );
275 /**
276 Write the value concerning the specified component in the marker COD and COC
277 @param j2k J2K handle
278 @param compno Number of the component concerned by the information written
279 */
280 static void j2k_write_cox(opj_j2k_t *j2k, int compno);
281 /**
282 Read the value concerning the specified component in the marker COD and COC
283 @param j2k J2K handle
284 @param compno Number of the component concerned by the information read
285 */
286 static void j2k_read_cox(opj_j2k_t *j2k, int compno);
287 /**
288 Write the COD marker (coding style default)
289 @param j2k J2K handle
290 */
291 static void j2k_write_cod(opj_j2k_t *j2k);
292 /**
293 Read the COD marker (coding style default)
294 @param j2k J2K handle
295 */
296 static void j2k_read_cod(opj_j2k_t *j2k);
297
298 /**
299  * Reads a COD marker (Coding Styke defaults)
300  * @param       p_header_data   the data contained in the COD box.
301  * @param       p_j2k                   the jpeg2000 codec.
302  * @param       p_header_size   the size of the data contained in the COD marker.
303  * @param       p_manager               the user event manager.
304 */
305 static opj_bool j2k_read_cod_v2 (
306                                         opj_j2k_v2_t *p_j2k,
307                                         OPJ_BYTE * p_header_data,
308                                         OPJ_UINT32 p_header_size,
309                                         struct opj_event_mgr * p_manager
310                                         );
311
312 /**
313 Write the COC marker (coding style component)
314 @param j2k J2K handle
315 @param compno Number of the component concerned by the information written
316 */
317 static void j2k_write_coc(opj_j2k_t *j2k, int compno);
318 /**
319 Read the COC marker (coding style component)
320 @param j2k J2K handle
321 */
322 static void j2k_read_coc(opj_j2k_t *j2k);
323
324 /**
325  * Reads a COC marker (Coding Style Component)
326  * @param       p_header_data   the data contained in the COC box.
327  * @param       p_j2k                   the jpeg2000 codec.
328  * @param       p_header_size   the size of the data contained in the COC marker.
329  * @param       p_manager               the user event manager.
330 */
331 static opj_bool j2k_read_coc_v2 (
332                                         opj_j2k_v2_t *p_j2k,
333                                         OPJ_BYTE * p_header_data,
334                                         OPJ_UINT32 p_header_size,
335                                         struct opj_event_mgr * p_manager
336                                         );
337
338 /**
339 Write the value concerning the specified component in the marker QCD and QCC
340 @param j2k J2K handle
341 @param compno Number of the component concerned by the information written
342 */
343 static void j2k_write_qcx(opj_j2k_t *j2k, int compno);
344 /**
345 Read the value concerning the specified component in the marker QCD and QCC
346 @param j2k J2K handle
347 @param compno Number of the component concern by the information read
348 @param len Length of the information in the QCX part of the marker QCD/QCC
349 */
350 static void j2k_read_qcx(opj_j2k_t *j2k, int compno, int len);
351 /**
352 Write the QCD marker (quantization default)
353 @param j2k J2K handle
354 */
355 static void j2k_write_qcd(opj_j2k_t *j2k);
356 /**
357 Read the QCD marker (quantization default)
358 @param j2k J2K handle
359 */
360 static void j2k_read_qcd(opj_j2k_t *j2k);
361
362 /**
363  * Reads a QCD marker (Quantization defaults)
364  * @param       p_header_data   the data contained in the QCD box.
365  * @param       p_j2k                   the jpeg2000 codec.
366  * @param       p_header_size   the size of the data contained in the QCD marker.
367  * @param       p_manager               the user event manager.
368 */
369 static opj_bool j2k_read_qcd_v2 (
370                                         opj_j2k_v2_t *p_j2k,
371                                         OPJ_BYTE * p_header_data,
372                                         OPJ_UINT32 p_header_size,
373                                         struct opj_event_mgr * p_manager
374                                         );
375
376 /**
377 Write the QCC marker (quantization component)
378 @param j2k J2K handle
379 @param compno Number of the component concerned by the information written
380 */
381 static void j2k_write_qcc(opj_j2k_t *j2k, int compno);
382 /**
383 Read the QCC marker (quantization component)
384 @param j2k J2K handle
385 */
386 static void j2k_read_qcc(opj_j2k_t *j2k);
387 /**
388  * Reads a QCC marker (Quantization component)
389  * @param       p_header_data   the data contained in the QCC box.
390  * @param       p_j2k                   the jpeg2000 codec.
391  * @param       p_header_size   the size of the data contained in the QCC marker.
392  * @param       p_manager               the user event manager.
393 */
394 static opj_bool j2k_read_qcc_v2(
395                                                         opj_j2k_v2_t *p_j2k,
396                                                         OPJ_BYTE * p_header_data,
397                                                         OPJ_UINT32 p_header_size,
398                                                         struct opj_event_mgr * p_manager);
399
400 /**
401 Write the POC marker (progression order change)
402 @param j2k J2K handle
403 */
404 static void j2k_write_poc(opj_j2k_t *j2k);
405 /**
406 Read the POC marker (progression order change)
407 @param j2k J2K handle
408 */
409 static void j2k_read_poc(opj_j2k_t *j2k);
410 /**
411  * Reads a POC marker (Progression Order Change)
412  *
413  * @param       p_header_data   the data contained in the POC box.
414  * @param       p_j2k                   the jpeg2000 codec.
415  * @param       p_header_size   the size of the data contained in the POC marker.
416  * @param       p_manager               the user event manager.
417 */
418 static opj_bool j2k_read_poc_v2 (
419                                                 opj_j2k_v2_t *p_j2k,
420                                                 OPJ_BYTE * p_header_data,
421                                                 OPJ_UINT32 p_header_size,
422                                                 struct opj_event_mgr * p_manager
423                                         );
424 /**
425 Read the CRG marker (component registration)
426 @param j2k J2K handle
427 */
428 static void j2k_read_crg(opj_j2k_t *j2k);
429 /**
430  * Reads a CRG marker (Component registration)
431  *
432  * @param       p_header_data   the data contained in the TLM box.
433  * @param       p_j2k                   the jpeg2000 codec.
434  * @param       p_header_size   the size of the data contained in the TLM marker.
435  * @param       p_manager               the user event manager.
436 */
437 static opj_bool j2k_read_crg_v2 (
438                                                 opj_j2k_v2_t *p_j2k,
439                                                 OPJ_BYTE * p_header_data,
440                                                 OPJ_UINT32 p_header_size,
441                                                 struct opj_event_mgr * p_manager
442                                         );
443 /**
444 Read the TLM marker (tile-part lengths)
445 @param j2k J2K handle
446 */
447 static void j2k_read_tlm(opj_j2k_t *j2k);
448 /**
449  * Reads a TLM marker (Tile Length Marker)
450  *
451  * @param       p_header_data   the data contained in the TLM box.
452  * @param       p_j2k                   the jpeg2000 codec.
453  * @param       p_header_size   the size of the data contained in the TLM marker.
454  * @param       p_manager               the user event manager.
455 */
456 static opj_bool j2k_read_tlm_v2 (
457                                                 opj_j2k_v2_t *p_j2k,
458                                                 OPJ_BYTE * p_header_data,
459                                                 OPJ_UINT32 p_header_size,
460                                                 struct opj_event_mgr * p_manager
461                                         );
462 /**
463 Read the PLM marker (packet length, main header)
464 @param j2k J2K handle
465 */
466 static void j2k_read_plm(opj_j2k_t *j2k);
467
468 /**
469  * Reads a PLM marker (Packet length, main header marker)
470  *
471  * @param       p_header_data   the data contained in the TLM box.
472  * @param       p_j2k                   the jpeg2000 codec.
473  * @param       p_header_size   the size of the data contained in the TLM marker.
474  * @param       p_manager               the user event manager.
475 */
476 static opj_bool j2k_read_plm_v2 (
477                                                 opj_j2k_v2_t *p_j2k,
478                                                 OPJ_BYTE * p_header_data,
479                                                 OPJ_UINT32 p_header_size,
480                                                 struct opj_event_mgr * p_manager
481                                         );
482 /**
483 Read the PLT marker (packet length, tile-part header)
484 @param j2k J2K handle
485 */
486 static void j2k_read_plt(opj_j2k_t *j2k);
487 /**
488  * Reads a PLT marker (Packet length, tile-part header)
489  *
490  * @param       p_header_data   the data contained in the PLT box.
491  * @param       p_j2k                   the jpeg2000 codec.
492  * @param       p_header_size   the size of the data contained in the PLT marker.
493  * @param       p_manager               the user event manager.
494 */
495 static opj_bool j2k_read_plt_v2 (
496                                                 opj_j2k_v2_t *p_j2k,
497                                                 OPJ_BYTE * p_header_data,
498                                                 OPJ_UINT32 p_header_size,
499                                                 struct opj_event_mgr * p_manager
500                                         );
501 /**
502 Read the PPM marker (packet packet headers, main header)
503 @param j2k J2K handle
504 */
505 static void j2k_read_ppm(opj_j2k_t *j2k);
506 /**
507  * Reads a PPM marker (Packed packet headers, main header)
508  *
509  * @param       p_header_data   the data contained in the POC box.
510  * @param       p_j2k                   the jpeg2000 codec.
511  * @param       p_header_size   the size of the data contained in the POC marker.
512  * @param       p_manager               the user event manager.
513 */
514 static opj_bool j2k_read_ppm_v2 (
515                                                 opj_j2k_v2_t *p_j2k,
516                                                 OPJ_BYTE * p_header_data,
517                                                 OPJ_UINT32 p_header_size,
518                                                 struct opj_event_mgr * p_manager
519                                         );
520
521 static opj_bool j2k_read_ppm_v3 (
522                                                 opj_j2k_v2_t *p_j2k,
523                                                 OPJ_BYTE * p_header_data,
524                                                 OPJ_UINT32 p_header_size,
525                                                 struct opj_event_mgr * p_manager
526                                         );
527
528 /**
529 Read the PPT marker (packet packet headers, tile-part header)
530 @param j2k J2K handle
531 */
532 static void j2k_read_ppt(opj_j2k_t *j2k);
533 /**
534  * Reads a PPT marker (Packed packet headers, tile-part header)
535  *
536  * @param       p_header_data   the data contained in the PPT box.
537  * @param       p_j2k                   the jpeg2000 codec.
538  * @param       p_header_size   the size of the data contained in the PPT marker.
539  * @param       p_manager               the user event manager.
540 */
541 static opj_bool j2k_read_ppt_v2 (
542                                                 opj_j2k_v2_t *p_j2k,
543                                                 OPJ_BYTE * p_header_data,
544                                                 OPJ_UINT32 p_header_size,
545                                                 struct opj_event_mgr * p_manager
546                                         );
547 /**
548 Write the TLM marker (Mainheader)
549 @param j2k J2K handle
550 */
551 static void j2k_write_tlm(opj_j2k_t *j2k);
552 /**
553 Write the SOT marker (start of tile-part)
554 @param j2k J2K handle
555 */
556 static void j2k_write_sot(opj_j2k_t *j2k);
557 /**
558 Read the SOT marker (start of tile-part)
559 @param j2k J2K handle
560 */
561 static void j2k_read_sot(opj_j2k_t *j2k);
562
563 /**
564  * Reads a PPT marker (Packed packet headers, tile-part header)
565  *
566  * @param       p_header_data   the data contained in the PPT box.
567  * @param       p_j2k                   the jpeg2000 codec.
568  * @param       p_header_size   the size of the data contained in the PPT marker.
569  * @param       p_manager               the user event manager.
570 */
571 static opj_bool j2k_read_sot_v2 (
572                                                 opj_j2k_v2_t *p_j2k,
573                                                 OPJ_BYTE * p_header_data,
574                                                 OPJ_UINT32 p_header_size,
575                                                 struct opj_event_mgr * p_manager
576                                         );
577 /**
578 Write the SOD marker (start of data)
579 @param j2k J2K handle
580 @param tile_coder Pointer to a TCD handle
581 */
582 static void j2k_write_sod(opj_j2k_t *j2k, void *tile_coder);
583 /**
584 Read the SOD marker (start of data)
585 @param j2k J2K handle
586 */
587 static void j2k_read_sod(opj_j2k_t *j2k);
588
589 /**
590  * Reads a SOD marker (Start Of Data)
591  *
592  * @param       p_header_data   the data contained in the SOD box.
593  * @param       p_j2k                   the jpeg2000 codec.
594  * @param       p_header_size   the size of the data contained in the SOD marker.
595  * @param       p_manager               the user event manager.
596 */
597 static opj_bool j2k_read_sod_v2 (
598                                                 opj_j2k_v2_t *p_j2k,
599                                                 struct opj_stream_private *p_stream,
600                                                 struct opj_event_mgr * p_manager
601                                         );
602
603 /**
604 Write the RGN marker (region-of-interest)
605 @param j2k J2K handle
606 @param compno Number of the component concerned by the information written
607 @param tileno Number of the tile concerned by the information written
608 */
609 static void j2k_write_rgn(opj_j2k_t *j2k, int compno, int tileno);
610 /**
611 Read the RGN marker (region-of-interest)
612 @param j2k J2K handle
613 */
614 static void j2k_read_rgn(opj_j2k_t *j2k);
615
616 /**
617  * Reads a RGN marker (Region Of Interest)
618  *
619  * @param       p_header_data   the data contained in the POC box.
620  * @param       p_j2k                   the jpeg2000 codec.
621  * @param       p_header_size   the size of the data contained in the POC marker.
622  * @param       p_manager               the user event manager.
623 */
624 static opj_bool j2k_read_rgn_v2 (
625                                                 opj_j2k_v2_t *p_j2k,
626                                                 OPJ_BYTE * p_header_data,
627                                                 OPJ_UINT32 p_header_size,
628                                                 struct opj_event_mgr * p_manager
629                                         ) ;
630
631 /**
632 Write the EOC marker (end of codestream)
633 @param j2k J2K handle
634 */
635 static void j2k_write_eoc(opj_j2k_t *j2k);
636 /**
637 Read the EOC marker (end of codestream)
638 @param j2k J2K handle
639 */
640 static void j2k_read_eoc(opj_j2k_t *j2k);
641
642 /**
643  * Reads a EOC marker (End Of Codestream)
644  *
645  * @param       p_header_data   the data contained in the SOD box.
646  * @param       p_j2k                   the jpeg2000 codec.
647  * @param       p_header_size   the size of the data contained in the SOD marker.
648  * @param       p_manager               the user event manager.
649 */
650 static opj_bool j2k_read_eoc_v2 (
651                                             opj_j2k_v2_t *p_j2k,
652                                                 struct opj_stream_private *p_stream,
653                                                 struct opj_event_mgr * p_manager
654                                         ) ;
655
656 /**
657 Read an unknown marker
658 @param j2k J2K handle
659 */
660 static void j2k_read_unk(opj_j2k_t *j2k);
661 /**
662 Add main header marker information
663 @param cstr_info Codestream information structure
664 @param type marker type
665 @param pos byte offset of marker segment
666 @param len length of marker segment
667  */
668 static void j2k_add_mhmarker(opj_codestream_info_t *cstr_info, unsigned short int type, int pos, int len);
669
670 static void j2k_add_mhmarker_v2(opj_codestream_index_t *cstr_index, OPJ_UINT32 type, OPJ_UINT32 pos, OPJ_UINT32 len) ;
671 /**
672 Add tile header marker information
673 @param tileno tile index number
674 @param cstr_info Codestream information structure
675 @param type marker type
676 @param pos byte offset of marker segment
677 @param len length of marker segment
678  */
679 static void j2k_add_tlmarker( int tileno, opj_codestream_info_t *cstr_info, unsigned short int type, int pos, int len);
680
681 static void j2k_add_tlmarker_v2(OPJ_UINT32 tileno, opj_codestream_index_t *cstr_index, OPJ_UINT32 type, OPJ_UINT32 pos, OPJ_UINT32 len);
682
683 /**
684  * Reads an unknown marker
685  *
686  * @param       p_stream                the stream object to read from.
687  * @param       p_j2k                   the jpeg2000 codec.
688  * @param       p_manager               the user event manager.
689  *
690  * @return      true                    if the marker could be deduced.
691 */
692 static opj_bool j2k_read_unk_v2 (       opj_j2k_v2_t *p_j2k,
693                                                                         struct opj_stream_private *p_stream,
694                                                                         OPJ_UINT32 *output_marker,
695                                                                         struct opj_event_mgr * p_manager );
696
697 /**
698  * Reads a MCT marker (Multiple Component Transform)
699  *
700  * @param       p_header_data   the data contained in the MCT box.
701  * @param       p_j2k                   the jpeg2000 codec.
702  * @param       p_header_size   the size of the data contained in the MCT marker.
703  * @param       p_manager               the user event manager.
704 */
705 static opj_bool j2k_read_mct (  opj_j2k_v2_t *p_j2k,
706                                                                 OPJ_BYTE * p_header_data,
707                                                                 OPJ_UINT32 p_header_size,
708                                                                 struct opj_event_mgr * p_manager );
709
710 /**
711  * Reads a MCC marker (Multiple Component Collection)
712  *
713  * @param       p_header_data   the data contained in the MCC box.
714  * @param       p_j2k                   the jpeg2000 codec.
715  * @param       p_header_size   the size of the data contained in the MCC marker.
716  * @param       p_manager               the user event manager.
717 */
718 static opj_bool j2k_read_mcc (  opj_j2k_v2_t *p_j2k,
719                                                         OPJ_BYTE * p_header_data,
720                                                         OPJ_UINT32 p_header_size,
721                                                         struct opj_event_mgr * p_manager );
722
723 /**
724  * Reads a MCO marker (Multiple Component Transform Ordering)
725  *
726  * @param       p_header_data   the data contained in the MCO box.
727  * @param       p_j2k                   the jpeg2000 codec.
728  * @param       p_header_size   the size of the data contained in the MCO marker.
729  * @param       p_manager               the user event manager.
730 */
731 static opj_bool j2k_read_mco (  opj_j2k_v2_t *p_j2k,
732                                                                 OPJ_BYTE * p_header_data,
733                                                                 OPJ_UINT32 p_header_size,
734                                                                 struct opj_event_mgr * p_manager );
735
736 static opj_bool j2k_add_mct(opj_tcp_v2_t * p_tcp, opj_image_t * p_image, OPJ_UINT32 p_index);
737
738 static void  j2k_read_int16_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
739 static void  j2k_read_int32_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
740 static void  j2k_read_float32_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
741 static void  j2k_read_float64_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
742
743 static void  j2k_read_int16_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
744 static void  j2k_read_int32_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
745 static void  j2k_read_float32_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
746 static void  j2k_read_float64_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
747
748 static void  j2k_write_float_to_int16 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
749 static void  j2k_write_float_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
750 static void  j2k_write_float_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
751 static void  j2k_write_float_to_float64 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
752
753 /**
754  * Reads a CBD marker (Component bit depth definition)
755  * @param       p_header_data   the data contained in the CBD box.
756  * @param       p_j2k                   the jpeg2000 codec.
757  * @param       p_header_size   the size of the data contained in the CBD marker.
758  * @param       p_manager               the user event manager.
759 */
760 static opj_bool j2k_read_cbd (  opj_j2k_v2_t *p_j2k,
761                                                         OPJ_BYTE * p_header_data,
762                                                         OPJ_UINT32 p_header_size,
763                                                         struct opj_event_mgr * p_manager);
764
765
766 static void j2k_dump_MH_info(opj_j2k_v2_t* p_j2k, FILE* out_stream);
767
768 static void j2k_dump_MH_index(opj_j2k_v2_t* p_j2k, FILE* out_stream);
769
770 static opj_codestream_index_t* j2k_create_cstr_index(void);
771
772 /*@}*/
773
774 /*@}*/
775
776 /* ----------------------------------------------------------------------- */
777 typedef struct j2k_prog_order{
778         OPJ_PROG_ORDER enum_prog;
779         char str_prog[5];
780 }j2k_prog_order_t;
781
782 j2k_prog_order_t j2k_prog_order_list[] = {
783         {CPRL, "CPRL"},
784         {LRCP, "LRCP"},
785         {PCRL, "PCRL"},
786         {RLCP, "RLCP"},
787         {RPCL, "RPCL"},
788         {(OPJ_PROG_ORDER)-1, ""}
789 };
790
791
792
793 /**
794  * FIXME DOC
795  */
796 const OPJ_UINT32 MCT_ELEMENT_SIZE [] =
797 {
798         2,
799         4,
800         4,
801         8
802 };
803
804 typedef void (* j2k_mct_function) (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
805
806 const j2k_mct_function j2k_mct_read_functions_to_float [] =
807 {
808         j2k_read_int16_to_float,
809         j2k_read_int32_to_float,
810         j2k_read_float32_to_float,
811         j2k_read_float64_to_float
812 };
813
814 const j2k_mct_function j2k_mct_read_functions_to_int32 [] =
815 {
816         j2k_read_int16_to_int32,
817         j2k_read_int32_to_int32,
818         j2k_read_float32_to_int32,
819         j2k_read_float64_to_int32
820 };
821
822 const j2k_mct_function j2k_mct_write_functions_from_float [] =
823 {
824         j2k_write_float_to_int16,
825         j2k_write_float_to_int32,
826         j2k_write_float_to_float,
827         j2k_write_float_to_float64
828 };
829
830 typedef struct opj_dec_memory_marker_handler
831 {
832         /** marker value */
833         OPJ_UINT32 id;
834         /** value of the state when the marker can appear */
835         OPJ_UINT32 states;
836         /** action linked to the marker */
837         opj_bool (*handler) (
838                                         opj_j2k_v2_t *p_j2k,
839                                         OPJ_BYTE * p_header_data,
840                                         OPJ_UINT32 p_header_size,
841                                         struct opj_event_mgr * p_manager
842                                                 );
843 }
844 opj_dec_memory_marker_handler_t;
845
846 const opj_dec_memory_marker_handler_t j2k_memory_marker_handler_tab [] =
847 {
848 #ifdef TODO_MS
849   {J2K_MS_SOT, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPHSOT, j2k_read_sot},
850   {J2K_MS_COD, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_cod},
851   {J2K_MS_COC, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_coc},
852   {J2K_MS_RGN, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_rgn},
853   {J2K_MS_QCD, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_qcd},
854   {J2K_MS_QCC, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_qcc},
855   {J2K_MS_POC, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_poc},
856   {J2K_MS_SIZ, J2K_DEC_STATE_MHSIZ , j2k_read_siz},
857   {J2K_MS_TLM, J2K_DEC_STATE_MH, j2k_read_tlm},
858   {J2K_MS_PLM, J2K_DEC_STATE_MH, j2k_read_plm},
859   {J2K_MS_PLT, J2K_DEC_STATE_TPH, j2k_read_plt},
860   {J2K_MS_PPM, J2K_DEC_STATE_MH, j2k_read_ppm},
861   {J2K_MS_PPT, J2K_DEC_STATE_TPH, j2k_read_ppt},
862   {J2K_MS_SOP, 0, 0},
863   {J2K_MS_CRG, J2K_DEC_STATE_MH, j2k_read_crg},
864   {J2K_MS_COM, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_com},
865   {J2K_MS_MCT, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_mct},
866   {J2K_MS_CBD, J2K_DEC_STATE_MH , j2k_read_cbd},
867   {J2K_MS_MCC, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_mcc},
868   {J2K_MS_MCO, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_mco},
869 #endif
870   {J2K_MS_SOT, J2K_STATE_MH | J2K_STATE_TPHSOT, j2k_read_sot_v2},
871   {J2K_MS_COD, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_cod_v2},
872   {J2K_MS_COC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_coc_v2},
873   {J2K_MS_RGN, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_rgn_v2},
874   {J2K_MS_QCD, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_qcd_v2},
875   {J2K_MS_QCC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_qcc_v2},
876   {J2K_MS_POC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_poc_v2},
877   {J2K_MS_SIZ, J2K_STATE_MHSIZ , j2k_read_siz_v2},
878   {J2K_MS_TLM, J2K_STATE_MH, j2k_read_tlm_v2},
879   {J2K_MS_PLM, J2K_STATE_MH, j2k_read_plm_v2},
880   {J2K_MS_PLT, J2K_STATE_TPH, j2k_read_plt_v2},
881   {J2K_MS_PPM, J2K_STATE_MH, j2k_read_ppm_v3},
882   {J2K_MS_PPT, J2K_STATE_TPH, j2k_read_ppt_v2},
883   {J2K_MS_SOP, 0, 0},
884   {J2K_MS_CRG, J2K_STATE_MH, j2k_read_crg_v2},
885   {J2K_MS_COM, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_com_v2},
886   {J2K_MS_MCT, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_mct},
887   {J2K_MS_CBD, J2K_STATE_MH , j2k_read_cbd},
888   {J2K_MS_MCC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_mcc},
889   {J2K_MS_MCO, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_mco},
890 #ifdef USE_JPWL
891 #ifdef TODO_MS /* FIXME */
892   {J2K_MS_EPC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_epc},
893   {J2K_MS_EPB, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_epb},
894   {J2K_MS_ESD, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_esd},
895   {J2K_MS_RED, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_red},
896 #endif
897 #endif /* USE_JPWL */
898 #ifdef USE_JPSEC
899   {J2K_MS_SEC, J2K_DEC_STATE_MH, j2k_read_sec},
900   {J2K_MS_INSEC, 0, j2k_read_insec}
901 #endif /* USE_JPSEC */
902   {J2K_MS_UNK, J2K_STATE_MH | J2K_STATE_TPH, 0}//j2k_read_unk_v2}
903 };
904
905
906
907 void  j2k_read_int16_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
908 {
909         OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
910         OPJ_FLOAT32 * l_dest_data = (OPJ_FLOAT32 *) p_dest_data;
911         OPJ_UINT32 i;
912         OPJ_UINT32 l_temp;
913
914         for (i=0;i<p_nb_elem;++i) {
915                 opj_read_bytes(l_src_data,&l_temp,2);
916
917                 l_src_data+=sizeof(OPJ_INT16);
918
919                 *(l_dest_data++) = (OPJ_FLOAT32) l_temp;
920         }
921 }
922
923 void  j2k_read_int32_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
924 {
925         OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
926         OPJ_FLOAT32 * l_dest_data = (OPJ_FLOAT32 *) p_dest_data;
927         OPJ_UINT32 i;
928         OPJ_UINT32 l_temp;
929
930         for (i=0;i<p_nb_elem;++i) {
931                 opj_read_bytes(l_src_data,&l_temp,4);
932
933                 l_src_data+=sizeof(OPJ_INT32);
934
935                 *(l_dest_data++) = (OPJ_FLOAT32) l_temp;
936         }
937 }
938
939 void  j2k_read_float32_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
940 {
941         OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
942         OPJ_FLOAT32 * l_dest_data = (OPJ_FLOAT32 *) p_dest_data;
943         OPJ_UINT32 i;
944         OPJ_FLOAT32 l_temp;
945
946         for (i=0;i<p_nb_elem;++i) {
947                 opj_read_float(l_src_data,&l_temp);
948
949                 l_src_data+=sizeof(OPJ_FLOAT32);
950
951                 *(l_dest_data++) = l_temp;
952         }
953 }
954
955 void  j2k_read_float64_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
956 {
957         OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
958         OPJ_FLOAT32 * l_dest_data = (OPJ_FLOAT32 *) p_dest_data;
959         OPJ_UINT32 i;
960         OPJ_FLOAT64 l_temp;
961
962         for (i=0;i<p_nb_elem;++i) {
963                 opj_read_double(l_src_data,&l_temp);
964
965                 l_src_data+=sizeof(OPJ_FLOAT64);
966
967                 *(l_dest_data++) = (OPJ_FLOAT32) l_temp;
968         }
969 }
970
971 void  j2k_read_int16_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
972 {
973         OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
974         OPJ_INT32 * l_dest_data = (OPJ_INT32 *) p_dest_data;
975         OPJ_UINT32 i;
976         OPJ_UINT32 l_temp;
977
978         for (i=0;i<p_nb_elem;++i) {
979                 opj_read_bytes(l_src_data,&l_temp,2);
980
981                 l_src_data+=sizeof(OPJ_INT16);
982
983                 *(l_dest_data++) = (OPJ_INT32) l_temp;
984         }
985 }
986
987 void  j2k_read_int32_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
988 {
989         OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
990         OPJ_INT32 * l_dest_data = (OPJ_INT32 *) p_dest_data;
991         OPJ_UINT32 i;
992         OPJ_UINT32 l_temp;
993
994         for (i=0;i<p_nb_elem;++i) {
995                 opj_read_bytes(l_src_data,&l_temp,4);
996
997                 l_src_data+=sizeof(OPJ_INT32);
998
999                 *(l_dest_data++) = (OPJ_INT32) l_temp;
1000         }
1001 }
1002
1003 void  j2k_read_float32_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1004 {
1005         OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
1006         OPJ_INT32 * l_dest_data = (OPJ_INT32 *) p_dest_data;
1007         OPJ_UINT32 i;
1008         OPJ_FLOAT32 l_temp;
1009
1010         for (i=0;i<p_nb_elem;++i) {
1011                 opj_read_float(l_src_data,&l_temp);
1012
1013                 l_src_data+=sizeof(OPJ_FLOAT32);
1014
1015                 *(l_dest_data++) = (OPJ_INT32) l_temp;
1016         }
1017 }
1018
1019 void  j2k_read_float64_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1020 {
1021         OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
1022         OPJ_INT32 * l_dest_data = (OPJ_INT32 *) p_dest_data;
1023         OPJ_UINT32 i;
1024         OPJ_FLOAT64 l_temp;
1025
1026         for (i=0;i<p_nb_elem;++i) {
1027                 opj_read_double(l_src_data,&l_temp);
1028
1029                 l_src_data+=sizeof(OPJ_FLOAT64);
1030
1031                 *(l_dest_data++) = (OPJ_INT32) l_temp;
1032         }
1033 }
1034
1035 void  j2k_write_float_to_int16 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1036 {
1037         OPJ_BYTE * l_dest_data = (OPJ_BYTE *) p_dest_data;
1038         OPJ_FLOAT32 * l_src_data = (OPJ_FLOAT32 *) p_src_data;
1039         OPJ_UINT32 i;
1040         OPJ_UINT32 l_temp;
1041
1042         for (i=0;i<p_nb_elem;++i) {
1043                 l_temp = (OPJ_UINT32) *(l_src_data++);
1044
1045                 opj_write_bytes(l_dest_data,l_temp,sizeof(OPJ_INT16));
1046
1047                 l_dest_data+=sizeof(OPJ_INT16);
1048         }
1049 }
1050
1051 void  j2k_write_float_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1052 {
1053         OPJ_BYTE * l_dest_data = (OPJ_BYTE *) p_dest_data;
1054         OPJ_FLOAT32 * l_src_data = (OPJ_FLOAT32 *) p_src_data;
1055         OPJ_UINT32 i;
1056         OPJ_UINT32 l_temp;
1057
1058         for (i=0;i<p_nb_elem;++i) {
1059                 l_temp = (OPJ_UINT32) *(l_src_data++);
1060
1061                 opj_write_bytes(l_dest_data,l_temp,sizeof(OPJ_INT32));
1062
1063                 l_dest_data+=sizeof(OPJ_INT32);
1064         }
1065 }
1066
1067 void  j2k_write_float_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1068 {
1069         OPJ_BYTE * l_dest_data = (OPJ_BYTE *) p_dest_data;
1070         OPJ_FLOAT32 * l_src_data = (OPJ_FLOAT32 *) p_src_data;
1071         OPJ_UINT32 i;
1072         OPJ_FLOAT32 l_temp;
1073
1074         for (i=0;i<p_nb_elem;++i) {
1075                 l_temp = (OPJ_FLOAT32) *(l_src_data++);
1076
1077                 opj_write_float(l_dest_data,l_temp);
1078
1079                 l_dest_data+=sizeof(OPJ_FLOAT32);
1080         }
1081 }
1082
1083 void  j2k_write_float_to_float64 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1084 {
1085         OPJ_BYTE * l_dest_data = (OPJ_BYTE *) p_dest_data;
1086         OPJ_FLOAT32 * l_src_data = (OPJ_FLOAT32 *) p_src_data;
1087         OPJ_UINT32 i;
1088         OPJ_FLOAT64 l_temp;
1089
1090         for (i=0;i<p_nb_elem;++i) {
1091                 l_temp = (OPJ_FLOAT64) *(l_src_data++);
1092
1093                 opj_write_double(l_dest_data,l_temp);
1094
1095                 l_dest_data+=sizeof(OPJ_FLOAT64);
1096         }
1097 }
1098
1099
1100 /**
1101  * Converts an enum type progression order to string type.
1102  *
1103  * @param prg_order             the progression order to get.
1104  *
1105  * @return      the string representation of the given progression order.
1106  */
1107 char *j2k_convert_progression_order(OPJ_PROG_ORDER prg_order){
1108         j2k_prog_order_t *po;
1109         for(po = j2k_prog_order_list; po->enum_prog != -1; po++ ){
1110                 if(po->enum_prog == prg_order){
1111                         return po->str_prog;
1112                 }
1113         }
1114         return po->str_prog;
1115 }
1116
1117 /* ----------------------------------------------------------------------- */
1118 static int j2k_get_num_tp(opj_cp_t *cp,int pino,int tileno){
1119         char *prog;
1120         int i;
1121         int tpnum=1,tpend=0;
1122         opj_tcp_t *tcp = &cp->tcps[tileno];
1123         prog = j2k_convert_progression_order(tcp->prg);
1124         
1125         if(cp->tp_on == 1){
1126                 for(i=0;i<4;i++){
1127                         if(tpend!=1){
1128                                 if( cp->tp_flag == prog[i] ){
1129                                         tpend=1;cp->tp_pos=i;
1130                                 }
1131                                 switch(prog[i]){
1132                                 case 'C':
1133                                         tpnum= tpnum * tcp->pocs[pino].compE;
1134                                         break;
1135                                 case 'R':
1136                                         tpnum= tpnum * tcp->pocs[pino].resE;
1137                                         break;
1138                                 case 'P':
1139                                         tpnum= tpnum * tcp->pocs[pino].prcE;
1140                                         break;
1141                                 case 'L':
1142                                         tpnum= tpnum * tcp->pocs[pino].layE;
1143                                         break;
1144                                 }
1145                         }
1146                 }
1147         }else{
1148                 tpnum=1;
1149         }
1150         return tpnum;
1151 }
1152
1153 /**     mem allocation for TLM marker*/
1154 int j2k_calculate_tp(opj_cp_t *cp,int img_numcomp,opj_image_t *image,opj_j2k_t *j2k ){
1155         int pino,tileno,totnum_tp=0;
1156
1157         OPJ_ARG_NOT_USED(img_numcomp);
1158
1159         j2k->cur_totnum_tp = (int *) opj_malloc(cp->tw * cp->th * sizeof(int));
1160         for (tileno = 0; tileno < cp->tw * cp->th; tileno++) {
1161                 int cur_totnum_tp = 0;
1162                 opj_tcp_t *tcp = &cp->tcps[tileno];
1163                 for(pino = 0; pino <= tcp->numpocs; pino++) {
1164                         int tp_num=0;
1165                         opj_pi_iterator_t *pi = pi_initialise_encode(image, cp, tileno,FINAL_PASS);
1166                         if(!pi) { return -1;}
1167                         tp_num = j2k_get_num_tp(cp,pino,tileno);
1168                         totnum_tp = totnum_tp + tp_num;
1169                         cur_totnum_tp = cur_totnum_tp + tp_num;
1170                         pi_destroy(pi, cp, tileno);
1171                 }
1172                 j2k->cur_totnum_tp[tileno] = cur_totnum_tp;
1173                 /* INDEX >> */
1174                 if (j2k->cstr_info) {
1175                         j2k->cstr_info->tile[tileno].num_tps = cur_totnum_tp;
1176                         j2k->cstr_info->tile[tileno].tp = (opj_tp_info_t *) opj_malloc(cur_totnum_tp * sizeof(opj_tp_info_t));
1177                 }
1178                 /* << INDEX */
1179         }
1180         return totnum_tp;
1181 }
1182
1183 static void j2k_write_soc(opj_j2k_t *j2k) {
1184         opj_cio_t *cio = j2k->cio;
1185         cio_write(cio, J2K_MS_SOC, 2);
1186
1187         if(j2k->cstr_info)
1188           j2k_add_mhmarker(j2k->cstr_info, J2K_MS_SOC, cio_tell(cio), 0);
1189
1190 /* UniPG>> */
1191 #ifdef USE_JPWL
1192
1193         /* update markers struct */
1194         j2k_add_marker(j2k->cstr_info, J2K_MS_SOC, cio_tell(cio) - 2, 2);
1195 #endif /* USE_JPWL */
1196 /* <<UniPG */
1197 }
1198
1199 static void j2k_read_soc(opj_j2k_t *j2k) {      
1200         j2k->state = J2K_STATE_MHSIZ;
1201         /* Index */
1202         if (j2k->cstr_info) {
1203                 j2k->cstr_info->main_head_start = cio_tell(j2k->cio) - 2;
1204                 j2k->cstr_info->codestream_size = cio_numbytesleft(j2k->cio) + 2 - j2k->cstr_info->main_head_start;
1205         }
1206 }
1207
1208 /**
1209  * Reads a SOC marker (Start of Codestream)
1210  * @param       p_header_data   the data contained in the SOC box.
1211  * @param       jp2                             the jpeg2000 file codec.
1212  * @param       p_header_size   the size of the data contained in the SOC marker.
1213  * @param       p_manager               the user event manager.
1214 */
1215 static opj_bool j2k_read_soc_v2(        opj_j2k_v2_t *p_j2k,
1216                                                                         struct opj_stream_private *p_stream,
1217                                                                         struct opj_event_mgr * p_manager )
1218 {
1219         OPJ_BYTE l_data [2];
1220         OPJ_UINT32 l_marker;
1221
1222         /* preconditions */
1223         assert(p_j2k != 00);
1224         assert(p_manager != 00);
1225         assert(p_stream != 00);
1226
1227         if (opj_stream_read_data(p_stream,l_data,2,p_manager) != 2) {
1228                 return OPJ_FALSE;
1229         }
1230
1231         opj_read_bytes(l_data,&l_marker,2);
1232         if (l_marker != J2K_MS_SOC) {
1233                 return OPJ_FALSE;
1234         }
1235
1236         /* Next marker should be a SIZ marker in the main header */
1237         p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_MHSIZ;
1238
1239         /* FIXME move it in a index structure included in p_j2k*/
1240         p_j2k->cstr_index->main_head_start = (OPJ_UINT32) opj_stream_tell(p_stream) - 2;
1241
1242         opj_event_msg_v2(p_manager, EVT_INFO, "Start to read j2k main header (%d).\n", p_j2k->cstr_index->main_head_start);
1243
1244         /* Add the marker to the codestream index*/
1245         j2k_add_mhmarker_v2(p_j2k->cstr_index, J2K_MS_SOC, p_j2k->cstr_index->main_head_start, 2);
1246
1247         return OPJ_TRUE;
1248 }
1249
1250 static void j2k_write_siz(opj_j2k_t *j2k) {
1251         int i;
1252         int lenp, len;
1253
1254         opj_cio_t *cio = j2k->cio;
1255         opj_image_t *image = j2k->image;
1256         opj_cp_t *cp = j2k->cp;
1257
1258         cio_write(cio, J2K_MS_SIZ, 2);  /* SIZ */
1259         lenp = cio_tell(cio);
1260         cio_skip(cio, 2);
1261         cio_write(cio, cp->rsiz, 2);                    /* Rsiz (capabilities) */
1262         cio_write(cio, image->x1, 4);   /* Xsiz */
1263         cio_write(cio, image->y1, 4);   /* Ysiz */
1264         cio_write(cio, image->x0, 4);   /* X0siz */
1265         cio_write(cio, image->y0, 4);   /* Y0siz */
1266         cio_write(cio, cp->tdx, 4);             /* XTsiz */
1267         cio_write(cio, cp->tdy, 4);             /* YTsiz */
1268         cio_write(cio, cp->tx0, 4);             /* XT0siz */
1269         cio_write(cio, cp->ty0, 4);             /* YT0siz */
1270         cio_write(cio, image->numcomps, 2);     /* Csiz */
1271         for (i = 0; i < image->numcomps; i++) {
1272                 cio_write(cio, image->comps[i].prec - 1 + (image->comps[i].sgnd << 7), 1);      /* Ssiz_i */
1273                 cio_write(cio, image->comps[i].dx, 1);  /* XRsiz_i */
1274                 cio_write(cio, image->comps[i].dy, 1);  /* YRsiz_i */
1275         }
1276         len = cio_tell(cio) - lenp;
1277         cio_seek(cio, lenp);
1278         cio_write(cio, len, 2);         /* Lsiz */
1279         cio_seek(cio, lenp + len);
1280         
1281         if(j2k->cstr_info)
1282           j2k_add_mhmarker(j2k->cstr_info, J2K_MS_SIZ, lenp, len);
1283 }
1284
1285 static void j2k_read_siz(opj_j2k_t *j2k) {
1286         int len, i;
1287         
1288         opj_cio_t *cio = j2k->cio;
1289         opj_image_t *image = j2k->image;
1290         opj_cp_t *cp = j2k->cp;
1291         
1292         len = cio_read(cio, 2);                 /* Lsiz */
1293         cio_read(cio, 2);                               /* Rsiz (capabilities) */
1294         image->x1 = cio_read(cio, 4);   /* Xsiz */
1295         image->y1 = cio_read(cio, 4);   /* Ysiz */
1296         image->x0 = cio_read(cio, 4);   /* X0siz */
1297         image->y0 = cio_read(cio, 4);   /* Y0siz */
1298         cp->tdx = cio_read(cio, 4);             /* XTsiz */
1299         cp->tdy = cio_read(cio, 4);             /* YTsiz */
1300         cp->tx0 = cio_read(cio, 4);             /* XT0siz */
1301         cp->ty0 = cio_read(cio, 4);             /* YT0siz */
1302         
1303         if ((image->x0<0)||(image->x1<0)||(image->y0<0)||(image->y1<0)) {
1304                 opj_event_msg(j2k->cinfo, EVT_ERROR,
1305                                                                         "%s: invalid image size (x0:%d, x1:%d, y0:%d, y1:%d)\n",
1306                                                                         image->x0,image->x1,image->y0,image->y1);
1307                 return;
1308         }
1309         
1310         image->numcomps = cio_read(cio, 2);     /* Csiz */
1311
1312 #ifdef USE_JPWL
1313         if (j2k->cp->correct) {
1314                 /* if JPWL is on, we check whether TX errors have damaged
1315                   too much the SIZ parameters */
1316                 if (!(image->x1 * image->y1)) {
1317                         opj_event_msg(j2k->cinfo, EVT_ERROR,
1318                                 "JPWL: bad image size (%d x %d)\n",
1319                                 image->x1, image->y1);
1320                         if (!JPWL_ASSUME || JPWL_ASSUME) {
1321                                 opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
1322                                 return;
1323                         }
1324                 }
1325                 if (image->numcomps != ((len - 38) / 3)) {
1326                         opj_event_msg(j2k->cinfo, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
1327                                 "JPWL: Csiz is %d => space in SIZ only for %d comps.!!!\n",
1328                                 image->numcomps, ((len - 38) / 3));
1329                         if (!JPWL_ASSUME) {
1330                                 opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
1331                                 return;
1332                         }
1333                         /* we try to correct */
1334                         opj_event_msg(j2k->cinfo, EVT_WARNING, "- trying to adjust this\n");
1335                         if (image->numcomps < ((len - 38) / 3)) {
1336                                 len = 38 + 3 * image->numcomps;
1337                                 opj_event_msg(j2k->cinfo, EVT_WARNING, "- setting Lsiz to %d => HYPOTHESIS!!!\n",
1338                                         len);                           
1339                         } else {
1340                                 image->numcomps = ((len - 38) / 3);
1341                                 opj_event_msg(j2k->cinfo, EVT_WARNING, "- setting Csiz to %d => HYPOTHESIS!!!\n",
1342                                         image->numcomps);                               
1343                         }
1344                 }
1345
1346                 /* update components number in the jpwl_exp_comps filed */
1347                 cp->exp_comps = image->numcomps;
1348         }
1349 #endif /* USE_JPWL */
1350
1351         image->comps = (opj_image_comp_t*) opj_calloc(image->numcomps, sizeof(opj_image_comp_t));
1352         for (i = 0; i < image->numcomps; i++) {
1353                 int tmp, w, h;
1354                 tmp = cio_read(cio, 1);         /* Ssiz_i */
1355                 image->comps[i].prec = (tmp & 0x7f) + 1;
1356                 image->comps[i].sgnd = tmp >> 7;
1357                 image->comps[i].dx = cio_read(cio, 1);  /* XRsiz_i */
1358                 image->comps[i].dy = cio_read(cio, 1);  /* YRsiz_i */
1359                 
1360 #ifdef USE_JPWL
1361                 if (j2k->cp->correct) {
1362                 /* if JPWL is on, we check whether TX errors have damaged
1363                         too much the SIZ parameters, again */
1364                         if (!(image->comps[i].dx * image->comps[i].dy)) {
1365                                 opj_event_msg(j2k->cinfo, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
1366                                         "JPWL: bad XRsiz_%d/YRsiz_%d (%d x %d)\n",
1367                                         i, i, image->comps[i].dx, image->comps[i].dy);
1368                                 if (!JPWL_ASSUME) {
1369                                         opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
1370                                         return;
1371                                 }
1372                                 /* we try to correct */
1373                                 opj_event_msg(j2k->cinfo, EVT_WARNING, "- trying to adjust them\n");
1374                                 if (!image->comps[i].dx) {
1375                                         image->comps[i].dx = 1;
1376                                         opj_event_msg(j2k->cinfo, EVT_WARNING, "- setting XRsiz_%d to %d => HYPOTHESIS!!!\n",
1377                                                 i, image->comps[i].dx);
1378                                 }
1379                                 if (!image->comps[i].dy) {
1380                                         image->comps[i].dy = 1;
1381                                         opj_event_msg(j2k->cinfo, EVT_WARNING, "- setting YRsiz_%d to %d => HYPOTHESIS!!!\n",
1382                                                 i, image->comps[i].dy);
1383                                 }
1384                         }
1385                         
1386                 }
1387 #endif /* USE_JPWL */
1388
1389                 /* TODO: unused ? */
1390                 w = int_ceildiv(image->x1 - image->x0, image->comps[i].dx);
1391                 h = int_ceildiv(image->y1 - image->y0, image->comps[i].dy);
1392
1393                 image->comps[i].resno_decoded = 0;      /* number of resolution decoded */
1394                 image->comps[i].factor = cp->reduce; /* reducing factor per component */
1395         }
1396         
1397         cp->tw = int_ceildiv(image->x1 - cp->tx0, cp->tdx);
1398         cp->th = int_ceildiv(image->y1 - cp->ty0, cp->tdy);
1399
1400 #ifdef USE_JPWL
1401         if (j2k->cp->correct) {
1402                 /* if JPWL is on, we check whether TX errors have damaged
1403                   too much the SIZ parameters */
1404                 if ((cp->tw < 1) || (cp->th < 1) || (cp->tw > cp->max_tiles) || (cp->th > cp->max_tiles)) {
1405                         opj_event_msg(j2k->cinfo, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
1406                                 "JPWL: bad number of tiles (%d x %d)\n",
1407                                 cp->tw, cp->th);
1408                         if (!JPWL_ASSUME) {
1409                                 opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
1410                                 return;
1411                         }
1412                         /* we try to correct */
1413                         opj_event_msg(j2k->cinfo, EVT_WARNING, "- trying to adjust them\n");
1414                         if (cp->tw < 1) {
1415                                 cp->tw= 1;
1416                                 opj_event_msg(j2k->cinfo, EVT_WARNING, "- setting %d tiles in x => HYPOTHESIS!!!\n",
1417                                         cp->tw);
1418                         }
1419                         if (cp->tw > cp->max_tiles) {
1420                                 cp->tw= 1;
1421                                 opj_event_msg(j2k->cinfo, EVT_WARNING, "- too large x, increase expectance of %d\n"
1422                                         "- setting %d tiles in x => HYPOTHESIS!!!\n",
1423                                         cp->max_tiles, cp->tw);
1424                         }
1425                         if (cp->th < 1) {
1426                                 cp->th= 1;
1427                                 opj_event_msg(j2k->cinfo, EVT_WARNING, "- setting %d tiles in y => HYPOTHESIS!!!\n",
1428                                         cp->th);
1429                         }
1430                         if (cp->th > cp->max_tiles) {
1431                                 cp->th= 1;
1432                                 opj_event_msg(j2k->cinfo, EVT_WARNING, "- too large y, increase expectance of %d to continue\n",
1433                                         "- setting %d tiles in y => HYPOTHESIS!!!\n",
1434                                         cp->max_tiles, cp->th);
1435                         }
1436                 }
1437         }
1438 #endif /* USE_JPWL */
1439
1440         cp->tcps = (opj_tcp_t*) opj_calloc(cp->tw * cp->th, sizeof(opj_tcp_t));
1441         cp->tileno = (int*) opj_malloc(cp->tw * cp->th * sizeof(int));
1442         cp->tileno_size = 0;
1443         
1444 #ifdef USE_JPWL
1445         if (j2k->cp->correct) {
1446                 if (!cp->tcps) {
1447                         opj_event_msg(j2k->cinfo, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
1448                                 "JPWL: could not alloc tcps field of cp\n");
1449                         if (!JPWL_ASSUME || JPWL_ASSUME) {
1450                                 opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
1451                                 return;
1452                         }
1453                 }
1454         }
1455 #endif /* USE_JPWL */
1456
1457         for (i = 0; i < cp->tw * cp->th; i++) {
1458                 cp->tcps[i].POC = 0;
1459                 cp->tcps[i].numpocs = 0;
1460                 cp->tcps[i].first = 1;
1461         }
1462         
1463         /* Initialization for PPM marker */
1464         cp->ppm = 0;
1465         cp->ppm_data = NULL;
1466         cp->ppm_data_first = NULL;
1467         cp->ppm_previous = 0;
1468         cp->ppm_store = 0;
1469
1470         j2k->default_tcp->tccps = (opj_tccp_t*) opj_calloc(image->numcomps, sizeof(opj_tccp_t));
1471         for (i = 0; i < cp->tw * cp->th; i++) {
1472                 cp->tcps[i].tccps = (opj_tccp_t*) opj_malloc(image->numcomps * sizeof(opj_tccp_t));
1473         }       
1474         j2k->tile_data = (unsigned char**) opj_calloc(cp->tw * cp->th, sizeof(unsigned char*));
1475         j2k->tile_len = (int*) opj_calloc(cp->tw * cp->th, sizeof(int));
1476         j2k->state = J2K_STATE_MH;
1477
1478         /* Index */
1479         if (j2k->cstr_info) {
1480                 opj_codestream_info_t *cstr_info = j2k->cstr_info;
1481                 cstr_info->image_w = image->x1 - image->x0;
1482                 cstr_info->image_h = image->y1 - image->y0;
1483                 cstr_info->numcomps = image->numcomps;
1484                 cstr_info->tw = cp->tw;
1485                 cstr_info->th = cp->th;
1486                 cstr_info->tile_x = cp->tdx;    
1487                 cstr_info->tile_y = cp->tdy;    
1488                 cstr_info->tile_Ox = cp->tx0;   
1489                 cstr_info->tile_Oy = cp->ty0;                   
1490                 cstr_info->tile = (opj_tile_info_t*) opj_calloc(cp->tw * cp->th, sizeof(opj_tile_info_t));              
1491         }
1492 }
1493
1494
1495 /**
1496  * Reads a SIZ marker (image and tile size)
1497  * @param       p_header_data   the data contained in the SIZ box.
1498  * @param       jp2                             the jpeg2000 file codec.
1499  * @param       p_header_size   the size of the data contained in the SIZ marker.
1500  * @param       p_manager               the user event manager.
1501 */
1502 opj_bool j2k_read_siz_v2 (
1503                                     opj_j2k_v2_t *p_j2k,
1504                                         OPJ_BYTE * p_header_data,
1505                                         OPJ_UINT32 p_header_size,
1506                                         struct opj_event_mgr * p_manager
1507                                         )
1508 {
1509         OPJ_UINT32 l_size, i;
1510         OPJ_UINT32 l_nb_comp;
1511         OPJ_UINT32 l_nb_comp_remain;
1512         OPJ_UINT32 l_remaining_size;
1513         OPJ_UINT32 l_nb_tiles;
1514         OPJ_UINT32 l_tmp;
1515         opj_image_t *l_image = 00;
1516         opj_cp_v2_t *l_cp = 00;
1517         opj_image_comp_t * l_img_comp = 00;
1518         opj_tcp_v2_t * l_current_tile_param = 00;
1519
1520         // preconditions
1521         assert(p_j2k != 00);
1522         assert(p_manager != 00);
1523         assert(p_header_data != 00);
1524
1525         l_image = p_j2k->m_private_image;
1526         l_cp = &(p_j2k->m_cp);
1527
1528         // minimum size == 39 - 3 (= minimum component parameter)
1529         if (p_header_size < 36) {
1530                 opj_event_msg_v2(p_manager, EVT_ERROR, "Error with SIZ marker size\n");
1531                 return OPJ_FALSE;
1532         }
1533
1534         l_remaining_size = p_header_size - 36;
1535         l_nb_comp = l_remaining_size / 3;
1536         l_nb_comp_remain = l_remaining_size % 3;
1537         if (l_nb_comp_remain != 0){
1538                 opj_event_msg_v2(p_manager, EVT_ERROR, "Error with SIZ marker size\n");
1539                 return OPJ_FALSE;
1540         }
1541
1542         l_size = p_header_size + 2;                                                                             /* Lsiz */
1543
1544         opj_read_bytes(p_header_data,&l_tmp ,2);                                                /* Rsiz (capabilities) */
1545         p_header_data+=2;
1546         l_cp->rsiz = (OPJ_RSIZ_CAPABILITIES) l_tmp;
1547         opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_image->x1, 4);   /* Xsiz */
1548         p_header_data+=4;
1549         opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_image->y1, 4);   /* Ysiz */
1550         p_header_data+=4;
1551         opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_image->x0, 4);   /* X0siz */
1552         p_header_data+=4;
1553         opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_image->y0, 4);   /* Y0siz */
1554         p_header_data+=4;
1555         opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_cp->tdx, 4);             /* XTsiz */
1556         p_header_data+=4;
1557         opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_cp->tdy, 4);             /* YTsiz */
1558         p_header_data+=4;
1559         opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_cp->tx0, 4);             /* XT0siz */
1560         p_header_data+=4;
1561         opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_cp->ty0, 4);             /* YT0siz */
1562         p_header_data+=4;
1563         opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_tmp, 2);                 /* Csiz */
1564         p_header_data+=2;
1565         if (l_tmp < 16385)
1566                 l_image->numcomps = (OPJ_UINT16) l_tmp;
1567         else {
1568                 opj_event_msg_v2(p_manager, EVT_ERROR, "Error with SIZ marker: number of component is illegal -> %d\n", l_tmp);
1569                 return OPJ_FALSE;
1570         }
1571
1572         if (l_image->numcomps != l_nb_comp) {
1573                 opj_event_msg_v2(p_manager, EVT_ERROR, "Error with SIZ marker: number of component is not compatible with the remaining number of parameters ( %d vs %d)\n", l_image->numcomps, l_nb_comp);
1574                 return OPJ_FALSE;
1575         }
1576
1577 #ifdef USE_JPWL
1578         if (l_cp->correct) {
1579                 /* if JPWL is on, we check whether TX errors have damaged
1580                   too much the SIZ parameters */
1581                 if (!(l_image->x1 * l_image->y1)) {
1582                         opj_event_msg_v2(p_manager, EVT_ERROR,
1583                                 "JPWL: bad image size (%d x %d)\n",
1584                                 l_image->x1, l_image->y1);
1585                         if (!JPWL_ASSUME || JPWL_ASSUME) {
1586                                 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
1587                                 return OPJ_FALSE;
1588                         }
1589                 }
1590
1591         /* FIXME check previously in the function so why keep this piece of code ? Need by the norm ?
1592                 if (l_image->numcomps != ((len - 38) / 3)) {
1593                         opj_event_msg_v2(p_manager, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
1594                                 "JPWL: Csiz is %d => space in SIZ only for %d comps.!!!\n",
1595                                 l_image->numcomps, ((len - 38) / 3));
1596                         if (!JPWL_ASSUME) {
1597                                 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
1598                                 return OPJ_FALSE;
1599                         }
1600         */              /* we try to correct */
1601         /*              opj_event_msg_v2(p_manager, EVT_WARNING, "- trying to adjust this\n");
1602                         if (l_image->numcomps < ((len - 38) / 3)) {
1603                                 len = 38 + 3 * l_image->numcomps;
1604                                 opj_event_msg_v2(p_manager, EVT_WARNING, "- setting Lsiz to %d => HYPOTHESIS!!!\n",
1605                                         len);
1606                         } else {
1607                                 l_image->numcomps = ((len - 38) / 3);
1608                                 opj_event_msg_v2(p_manager, EVT_WARNING, "- setting Csiz to %d => HYPOTHESIS!!!\n",
1609                                         l_image->numcomps);
1610                         }
1611                 }
1612         */
1613
1614                 /* update components number in the jpwl_exp_comps filed */
1615                 l_cp->exp_comps = l_image->numcomps;
1616         }
1617 #endif /* USE_JPWL */
1618
1619         // Allocate the resulting image components
1620         l_image->comps = (opj_image_comp_t*) opj_calloc(l_image->numcomps, sizeof(opj_image_comp_t));
1621         if (l_image->comps == 00){
1622                 l_image->numcomps = 0;
1623                 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to take in charge SIZ marker\n");
1624                 return OPJ_FALSE;
1625         }
1626
1627         memset(l_image->comps,0,l_image->numcomps * sizeof(opj_image_comp_t));
1628         l_img_comp = l_image->comps;
1629
1630         // Read the component information
1631         for (i = 0; i < l_image->numcomps; ++i){
1632                 OPJ_UINT32 tmp;
1633                 opj_read_bytes(p_header_data,&tmp,1);   /* Ssiz_i */
1634                 ++p_header_data;
1635                 l_img_comp->prec = (tmp & 0x7f) + 1;
1636                 l_img_comp->sgnd = tmp >> 7;
1637                 opj_read_bytes(p_header_data,&tmp,1);   /* XRsiz_i */
1638                 ++p_header_data;
1639                 l_img_comp->dx = (OPJ_INT32)tmp; // should be between 1 and 255
1640                 opj_read_bytes(p_header_data,&tmp,1);   /* YRsiz_i */
1641                 ++p_header_data;
1642                 l_img_comp->dy = (OPJ_INT32)tmp; // should be between 1 and 255
1643
1644 #ifdef USE_JPWL
1645                 if (l_cp->correct) {
1646                 /* if JPWL is on, we check whether TX errors have damaged
1647                         too much the SIZ parameters, again */
1648                         if (!(l_image->comps[i].dx * l_image->comps[i].dy)) {
1649                                 opj_event_msg_v2(p_manager, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
1650                                         "JPWL: bad XRsiz_%d/YRsiz_%d (%d x %d)\n",
1651                                         i, i, l_image->comps[i].dx, l_image->comps[i].dy);
1652                                 if (!JPWL_ASSUME) {
1653                                         opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
1654                                         return OPJ_FALSE;
1655                                 }
1656                                 /* we try to correct */
1657                                 opj_event_msg_v2(p_manager, EVT_WARNING, "- trying to adjust them\n");
1658                                 if (!l_image->comps[i].dx) {
1659                                         l_image->comps[i].dx = 1;
1660                                         opj_event_msg_v2(p_manager, EVT_WARNING, "- setting XRsiz_%d to %d => HYPOTHESIS!!!\n",
1661                                                 i, l_image->comps[i].dx);
1662                                 }
1663                                 if (!l_image->comps[i].dy) {
1664                                         l_image->comps[i].dy = 1;
1665                                         opj_event_msg_v2(p_manager, EVT_WARNING, "- setting YRsiz_%d to %d => HYPOTHESIS!!!\n",
1666                                                 i, l_image->comps[i].dy);
1667                                 }
1668                         }
1669                 }
1670 #endif /* USE_JPWL */
1671                 l_img_comp->resno_decoded = 0;                                                          /* number of resolution decoded */
1672                 l_img_comp->factor = l_cp->m_specific_param.m_dec.m_reduce; /* reducing factor per component */
1673                 ++l_img_comp;
1674         }
1675
1676         // Compute the number of tiles
1677         l_cp->tw = int_ceildiv(l_image->x1 - l_cp->tx0, l_cp->tdx);
1678         l_cp->th = int_ceildiv(l_image->y1 - l_cp->ty0, l_cp->tdy);
1679         l_nb_tiles = l_cp->tw * l_cp->th;
1680
1681         // Define the tiles which will be decoded
1682         if (p_j2k->m_specific_param.m_decoder.m_discard_tiles) {
1683                 p_j2k->m_specific_param.m_decoder.m_start_tile_x = (p_j2k->m_specific_param.m_decoder.m_start_tile_x - l_cp->tx0) / l_cp->tdx;
1684                 p_j2k->m_specific_param.m_decoder.m_start_tile_y = (p_j2k->m_specific_param.m_decoder.m_start_tile_y - l_cp->ty0) / l_cp->tdy;
1685                 p_j2k->m_specific_param.m_decoder.m_end_tile_x = int_ceildiv((p_j2k->m_specific_param.m_decoder.m_end_tile_x - l_cp->tx0), l_cp->tdx);
1686                 p_j2k->m_specific_param.m_decoder.m_end_tile_y = int_ceildiv((p_j2k->m_specific_param.m_decoder.m_end_tile_y - l_cp->ty0), l_cp->tdy);
1687         }
1688         else {
1689                 p_j2k->m_specific_param.m_decoder.m_start_tile_x = 0;
1690                 p_j2k->m_specific_param.m_decoder.m_start_tile_y = 0;
1691                 p_j2k->m_specific_param.m_decoder.m_end_tile_x = l_cp->tw;
1692                 p_j2k->m_specific_param.m_decoder.m_end_tile_y = l_cp->th;
1693         }
1694
1695 #ifdef USE_JPWL
1696         if (l_cp->correct) {
1697                 /* if JPWL is on, we check whether TX errors have damaged
1698                   too much the SIZ parameters */
1699                 if ((l_cp->tw < 1) || (l_cp->th < 1) || (l_cp->tw > l_cp->max_tiles) || (l_cp->th > l_cp->max_tiles)) {
1700                         opj_event_msg_v2(p_manager, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
1701                                 "JPWL: bad number of tiles (%d x %d)\n",
1702                                 l_cp->tw, l_cp->th);
1703                         if (!JPWL_ASSUME) {
1704                                 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
1705                                 return OPJ_FALSE;
1706                         }
1707                         /* we try to correct */
1708                         opj_event_msg_v2(p_manager, EVT_WARNING, "- trying to adjust them\n");
1709                         if (l_cp->tw < 1) {
1710                                 l_cp->tw= 1;
1711                                 opj_event_msg_v2(p_manager, EVT_WARNING, "- setting %d tiles in x => HYPOTHESIS!!!\n",
1712                                                 l_cp->tw);
1713                         }
1714                         if (l_cp->tw > l_cp->max_tiles) {
1715                                 l_cp->tw= 1;
1716                                 opj_event_msg_v2(p_manager, EVT_WARNING, "- too large x, increase expectance of %d\n"
1717                                         "- setting %d tiles in x => HYPOTHESIS!!!\n",
1718                                         l_cp->max_tiles, l_cp->tw);
1719                         }
1720                         if (l_cp->th < 1) {
1721                                 l_cp->th= 1;
1722                                 opj_event_msg_v2(p_manager, EVT_WARNING, "- setting %d tiles in y => HYPOTHESIS!!!\n",
1723                                                 l_cp->th);
1724                         }
1725                         if (l_cp->th > l_cp->max_tiles) {
1726                                 l_cp->th= 1;
1727                                 opj_event_msg_v2(p_manager, EVT_WARNING, "- too large y, increase expectance of %d to continue\n",
1728                                         "- setting %d tiles in y => HYPOTHESIS!!!\n",
1729                                         l_cp->max_tiles, l_cp->th);
1730                         }
1731                 }
1732         }
1733 #endif /* USE_JPWL */
1734
1735         /* memory allocations */
1736         l_cp->tcps = (opj_tcp_v2_t*) opj_calloc(l_nb_tiles, sizeof(opj_tcp_v2_t));
1737         if (l_cp->tcps == 00) {
1738                 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to take in charge SIZ marker\n");
1739                 return OPJ_FALSE;
1740         }
1741         memset(l_cp->tcps,0,l_nb_tiles*sizeof(opj_tcp_t));
1742
1743 #ifdef USE_JPWL
1744         if (l_cp->correct) {
1745                 if (!l_cp->tcps) {
1746                         opj_event_msg_v2(p_manager, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
1747                                 "JPWL: could not alloc tcps field of cp\n");
1748                         if (!JPWL_ASSUME || JPWL_ASSUME) {
1749                                 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
1750                                 return OPJ_FALSE;
1751                         }
1752                 }
1753         }
1754 #endif /* USE_JPWL */
1755
1756         p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps =
1757                         (opj_tccp_t*) opj_calloc(l_image->numcomps, sizeof(opj_tccp_t));
1758         if(p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps  == 00) {
1759                 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to take in charge SIZ marker\n");
1760                 return OPJ_FALSE;
1761         }
1762         memset(p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps ,0,l_image->numcomps*sizeof(opj_tccp_t));
1763
1764         p_j2k->m_specific_param.m_decoder.m_default_tcp->m_mct_records =
1765                         (opj_mct_data_t*)opj_malloc(J2K_MCT_DEFAULT_NB_RECORDS * sizeof(opj_mct_data_t));
1766
1767         if (! p_j2k->m_specific_param.m_decoder.m_default_tcp->m_mct_records) {
1768                 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to take in charge SIZ marker\n");
1769                 return OPJ_FALSE;
1770         }
1771         memset(p_j2k->m_specific_param.m_decoder.m_default_tcp->m_mct_records,0,J2K_MCT_DEFAULT_NB_RECORDS * sizeof(opj_mct_data_t));
1772         p_j2k->m_specific_param.m_decoder.m_default_tcp->m_nb_max_mct_records = J2K_MCT_DEFAULT_NB_RECORDS;
1773
1774         p_j2k->m_specific_param.m_decoder.m_default_tcp->m_mcc_records =
1775                         (opj_simple_mcc_decorrelation_data_t*)
1776                         opj_malloc(J2K_MCC_DEFAULT_NB_RECORDS * sizeof(opj_simple_mcc_decorrelation_data_t));
1777
1778         if (! p_j2k->m_specific_param.m_decoder.m_default_tcp->m_mcc_records) {
1779                 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to take in charge SIZ marker\n");
1780                 return OPJ_FALSE;
1781         }
1782         memset(p_j2k->m_specific_param.m_decoder.m_default_tcp->m_mcc_records,0,J2K_MCC_DEFAULT_NB_RECORDS * sizeof(opj_simple_mcc_decorrelation_data_t));
1783         p_j2k->m_specific_param.m_decoder.m_default_tcp->m_nb_max_mcc_records = J2K_MCC_DEFAULT_NB_RECORDS;
1784
1785         /* set up default dc level shift */
1786         for (i=0;i<l_image->numcomps;++i) {
1787                 if (! l_image->comps[i].sgnd) {
1788                         p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps[i].m_dc_level_shift = 1 << (l_image->comps[i].prec - 1);
1789                 }
1790         }
1791
1792         l_current_tile_param = l_cp->tcps;
1793         for     (i = 0; i < l_nb_tiles; ++i) {
1794                 l_current_tile_param->tccps = (opj_tccp_t*) opj_malloc(l_image->numcomps * sizeof(opj_tccp_t));
1795                 if (l_current_tile_param->tccps == 00) {
1796                         opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to take in charge SIZ marker\n");
1797                         return OPJ_FALSE;
1798                 }
1799                 memset(l_current_tile_param->tccps,0,l_image->numcomps * sizeof(opj_tccp_t));
1800
1801                 ++l_current_tile_param;
1802         }
1803
1804         p_j2k->m_specific_param.m_decoder.m_state =  J2K_STATE_MH; // FIXME J2K_DEC_STATE_MH;
1805         opj_image_comp_header_update(l_image,l_cp);
1806
1807         return OPJ_TRUE;
1808 }
1809
1810
1811
1812 static void j2k_write_com(opj_j2k_t *j2k) {
1813         unsigned int i;
1814         int lenp, len;
1815
1816         if(j2k->cp->comment) {
1817                 opj_cio_t *cio = j2k->cio;
1818                 char *comment = j2k->cp->comment;
1819
1820                 cio_write(cio, J2K_MS_COM, 2);
1821                 lenp = cio_tell(cio);
1822                 cio_skip(cio, 2);
1823                 cio_write(cio, 1, 2);           /* General use (IS 8859-15:1999 (Latin) values) */
1824                 for (i = 0; i < strlen(comment); i++) {
1825                         cio_write(cio, comment[i], 1);
1826                 }
1827                 len = cio_tell(cio) - lenp;
1828                 cio_seek(cio, lenp);
1829                 cio_write(cio, len, 2);
1830                 cio_seek(cio, lenp + len);
1831
1832                 
1833                 if(j2k->cstr_info)
1834                   j2k_add_mhmarker(j2k->cstr_info, J2K_MS_COM, lenp, len);
1835
1836         }
1837 }
1838
1839 static void j2k_read_com(opj_j2k_t *j2k) {
1840         int len;
1841         
1842         opj_cio_t *cio = j2k->cio;
1843
1844         len = cio_read(cio, 2);
1845         cio_skip(cio, len - 2);  
1846 }
1847 /**
1848  * Reads a COM marker (comments)
1849  * @param       p_header_data   the data contained in the COM box.
1850  * @param       jp2                             the jpeg2000 file codec.
1851  * @param       p_header_size   the size of the data contained in the COM marker.
1852  * @param       p_manager               the user event manager.
1853 */
1854 opj_bool j2k_read_com_v2 (
1855                                         opj_j2k_v2_t *p_j2k,
1856                                         OPJ_BYTE * p_header_data,
1857                                         OPJ_UINT32 p_header_size,
1858                                         struct opj_event_mgr * p_manager
1859                                         )
1860 {
1861         // preconditions
1862         assert(p_j2k != 00);
1863         assert(p_manager != 00);
1864         assert(p_header_data != 00);
1865
1866         return OPJ_TRUE;
1867 }
1868
1869 static void j2k_write_cox(opj_j2k_t *j2k, int compno) {
1870         int i;
1871
1872         opj_cp_t *cp = j2k->cp;
1873         opj_tcp_t *tcp = &cp->tcps[j2k->curtileno];
1874         opj_tccp_t *tccp = &tcp->tccps[compno];
1875         opj_cio_t *cio = j2k->cio;
1876         
1877         cio_write(cio, tccp->numresolutions - 1, 1);    /* SPcox (D) */
1878         cio_write(cio, tccp->cblkw - 2, 1);                             /* SPcox (E) */
1879         cio_write(cio, tccp->cblkh - 2, 1);                             /* SPcox (F) */
1880         cio_write(cio, tccp->cblksty, 1);                               /* SPcox (G) */
1881         cio_write(cio, tccp->qmfbid, 1);                                /* SPcox (H) */
1882         
1883         if (tccp->csty & J2K_CCP_CSTY_PRT) {
1884                 for (i = 0; i < tccp->numresolutions; i++) {
1885                         cio_write(cio, tccp->prcw[i] + (tccp->prch[i] << 4), 1);        /* SPcox (I_i) */
1886                 }
1887         }
1888 }
1889
1890 static void j2k_read_cox(opj_j2k_t *j2k, int compno) {
1891         int i;
1892
1893         opj_cp_t *cp = j2k->cp;
1894         opj_tcp_t *tcp = j2k->state == J2K_STATE_TPH ? &cp->tcps[j2k->curtileno] : j2k->default_tcp;
1895         opj_tccp_t *tccp = &tcp->tccps[compno];
1896         opj_cio_t *cio = j2k->cio;
1897
1898         tccp->numresolutions = cio_read(cio, 1) + 1;    /* SPcox (D) */
1899
1900         // If user wants to remove more resolutions than the codestream contains, return error
1901         if (cp->reduce >= tccp->numresolutions) {
1902                 opj_event_msg(j2k->cinfo, EVT_ERROR, "Error decoding component %d.\nThe number of resolutions to remove is higher than the number "
1903                                         "of resolutions of this component\nModify the cp_reduce parameter.\n\n", compno);
1904                 j2k->state |= J2K_STATE_ERR;
1905         }
1906
1907         tccp->cblkw = cio_read(cio, 1) + 2;     /* SPcox (E) */
1908         tccp->cblkh = cio_read(cio, 1) + 2;     /* SPcox (F) */
1909         tccp->cblksty = cio_read(cio, 1);       /* SPcox (G) */
1910         tccp->qmfbid = cio_read(cio, 1);        /* SPcox (H) */
1911         if (tccp->csty & J2K_CP_CSTY_PRT) {
1912                 for (i = 0; i < tccp->numresolutions; i++) {
1913                         int tmp = cio_read(cio, 1);     /* SPcox (I_i) */
1914                         tccp->prcw[i] = tmp & 0xf;
1915                         tccp->prch[i] = tmp >> 4;
1916                 }
1917         }
1918
1919         /* INDEX >> */
1920         if(j2k->cstr_info && compno == 0) {
1921                 for (i = 0; i < tccp->numresolutions; i++) {
1922                         if (tccp->csty & J2K_CP_CSTY_PRT) {
1923                                 j2k->cstr_info->tile[j2k->curtileno].pdx[i] = tccp->prcw[i];
1924                                 j2k->cstr_info->tile[j2k->curtileno].pdy[i] = tccp->prch[i];
1925                         }
1926                         else {
1927                                 j2k->cstr_info->tile[j2k->curtileno].pdx[i] = 15;
1928                                 j2k->cstr_info->tile[j2k->curtileno].pdx[i] = 15;
1929                         }
1930                 }
1931         }
1932         /* << INDEX */
1933 }
1934
1935 static void j2k_write_cod(opj_j2k_t *j2k) {
1936         opj_cp_t *cp = NULL;
1937         opj_tcp_t *tcp = NULL;
1938         int lenp, len;
1939
1940         opj_cio_t *cio = j2k->cio;
1941         
1942         cio_write(cio, J2K_MS_COD, 2);  /* COD */
1943         
1944         lenp = cio_tell(cio);
1945         cio_skip(cio, 2);
1946         
1947         cp = j2k->cp;
1948         tcp = &cp->tcps[j2k->curtileno];
1949
1950         cio_write(cio, tcp->csty, 1);           /* Scod */
1951         cio_write(cio, tcp->prg, 1);            /* SGcod (A) */
1952         cio_write(cio, tcp->numlayers, 2);      /* SGcod (B) */
1953         cio_write(cio, tcp->mct, 1);            /* SGcod (C) */
1954         
1955         j2k_write_cox(j2k, 0);
1956         len = cio_tell(cio) - lenp;
1957         cio_seek(cio, lenp);
1958         cio_write(cio, len, 2);         /* Lcod */
1959         cio_seek(cio, lenp + len);
1960
1961         if(j2k->cstr_info)
1962           j2k_add_mhmarker(j2k->cstr_info, J2K_MS_COD, lenp, len);
1963
1964 }
1965
1966 static void j2k_read_cod(opj_j2k_t *j2k) {
1967         int len, i, pos;
1968         
1969         opj_cio_t *cio = j2k->cio;
1970         opj_cp_t *cp = j2k->cp;
1971         opj_tcp_t *tcp = j2k->state == J2K_STATE_TPH ? &cp->tcps[j2k->curtileno] : j2k->default_tcp;
1972         opj_image_t *image = j2k->image;
1973         
1974         len = cio_read(cio, 2);                         /* Lcod */
1975         tcp->csty = cio_read(cio, 1);           /* Scod */
1976         tcp->prg = (OPJ_PROG_ORDER)cio_read(cio, 1);            /* SGcod (A) */
1977         tcp->numlayers = cio_read(cio, 2);      /* SGcod (B) */
1978         tcp->mct = cio_read(cio, 1);            /* SGcod (C) */
1979         
1980         pos = cio_tell(cio);
1981         for (i = 0; i < image->numcomps; i++) {
1982                 tcp->tccps[i].csty = tcp->csty & J2K_CP_CSTY_PRT;
1983                 cio_seek(cio, pos);
1984                 j2k_read_cox(j2k, i);
1985         }
1986
1987         /* Index */
1988         if (j2k->cstr_info) {
1989                 opj_codestream_info_t *cstr_info = j2k->cstr_info;
1990                 cstr_info->prog = tcp->prg;
1991                 cstr_info->numlayers = tcp->numlayers;
1992                 cstr_info->numdecompos = (int*) opj_malloc(image->numcomps * sizeof(int));
1993                 for (i = 0; i < image->numcomps; i++) {
1994                         cstr_info->numdecompos[i] = tcp->tccps[i].numresolutions - 1;
1995                 }
1996         }
1997 }
1998
1999 /**
2000  * Reads a COD marker (Coding Styke defaults)
2001  * @param       p_header_data   the data contained in the COD box.
2002  * @param       p_j2k                   the jpeg2000 codec.
2003  * @param       p_header_size   the size of the data contained in the COD marker.
2004  * @param       p_manager               the user event manager.
2005 */
2006 opj_bool j2k_read_cod_v2 (
2007                                         opj_j2k_v2_t *p_j2k,
2008                                         OPJ_BYTE * p_header_data,
2009                                         OPJ_UINT32 p_header_size,
2010                                         struct opj_event_mgr * p_manager
2011                                         )
2012 {
2013         // loop
2014         OPJ_UINT32 i;
2015         OPJ_UINT32 l_tmp;
2016         opj_cp_v2_t *l_cp = 00;
2017         opj_tcp_v2_t *l_tcp = 00;
2018         opj_image_t *l_image = 00;
2019
2020         /* preconditions */
2021         assert(p_header_data != 00);
2022         assert(p_j2k != 00);
2023         assert(p_manager != 00);
2024
2025         l_image = p_j2k->m_private_image;
2026         l_cp = &(p_j2k->m_cp);
2027
2028         /* If we are in the first tile-part header of the current tile */
2029         l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ?
2030                                 &l_cp->tcps[p_j2k->m_current_tile_number] :
2031                                 p_j2k->m_specific_param.m_decoder.m_default_tcp;
2032
2033         /* Make sure room is sufficient */
2034         if (p_header_size < 5) {
2035                 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading COD marker\n");
2036                 return OPJ_FALSE;
2037         }
2038
2039         opj_read_bytes(p_header_data,&l_tcp->csty,1);           /* Scod */
2040         ++p_header_data;
2041         opj_read_bytes(p_header_data,&l_tmp,1);                         /* SGcod (A) */
2042         ++p_header_data;
2043         l_tcp->prg = (OPJ_PROG_ORDER) l_tmp;
2044         opj_read_bytes(p_header_data,&l_tcp->numlayers,2);      /* SGcod (B) */
2045         p_header_data+=2;
2046
2047         // If user didn't set a number layer to decode take the max specify in the codestream.
2048         if      (l_cp->m_specific_param.m_dec.m_layer) {
2049                 l_tcp->num_layers_to_decode = l_cp->m_specific_param.m_dec.m_layer;
2050         }
2051         else {
2052                 l_tcp->num_layers_to_decode = l_tcp->numlayers;
2053         }
2054
2055         opj_read_bytes(p_header_data,&l_tcp->mct,1);            /* SGcod (C) */
2056         ++p_header_data;
2057
2058         p_header_size -= 5;
2059         for     (i = 0; i < l_image->numcomps; ++i) {
2060                 l_tcp->tccps[i].csty = l_tcp->csty & J2K_CCP_CSTY_PRT;
2061         }
2062
2063         if (! j2k_read_SPCod_SPCoc(p_j2k,0,p_header_data,&p_header_size,p_manager)) {
2064                 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading COD marker\n");
2065                 return OPJ_FALSE;
2066         }
2067
2068         if (p_header_size != 0) {
2069                 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading COD marker\n");
2070                 return OPJ_FALSE;
2071         }
2072
2073         /* Apply the coding style to other components of the current tile or the m_default_tcp*/
2074         j2k_copy_tile_component_parameters(p_j2k);
2075
2076         /* Index */
2077 #ifdef WIP_REMOVE_MSD
2078         if (p_j2k->cstr_info) {
2079                 //opj_codestream_info_t *l_cstr_info = p_j2k->cstr_info;
2080                 p_j2k->cstr_info->prog = l_tcp->prg;
2081                 p_j2k->cstr_info->numlayers = l_tcp->numlayers;
2082                 p_j2k->cstr_info->numdecompos = (OPJ_INT32*) opj_malloc(l_image->numcomps * sizeof(OPJ_UINT32));
2083                 for     (i = 0; i < l_image->numcomps; ++i) {
2084                         p_j2k->cstr_info->numdecompos[i] = l_tcp->tccps[i].numresolutions - 1;
2085                 }
2086         }
2087 #endif
2088
2089         return OPJ_TRUE;
2090 }
2091
2092 static void j2k_write_coc(opj_j2k_t *j2k, int compno) {
2093         int lenp, len;
2094
2095         opj_cp_t *cp = j2k->cp;
2096         opj_tcp_t *tcp = &cp->tcps[j2k->curtileno];
2097         opj_image_t *image = j2k->image;
2098         opj_cio_t *cio = j2k->cio;
2099         
2100         cio_write(cio, J2K_MS_COC, 2);  /* COC */
2101         lenp = cio_tell(cio);
2102         cio_skip(cio, 2);
2103         cio_write(cio, compno, image->numcomps <= 256 ? 1 : 2); /* Ccoc */
2104         cio_write(cio, tcp->tccps[compno].csty, 1);     /* Scoc */
2105         j2k_write_cox(j2k, compno);
2106         len = cio_tell(cio) - lenp;
2107         cio_seek(cio, lenp);
2108         cio_write(cio, len, 2);                 /* Lcoc */
2109         cio_seek(cio, lenp + len);
2110 }
2111
2112 static void j2k_read_coc(opj_j2k_t *j2k) {
2113         int len, compno;
2114
2115         opj_cp_t *cp = j2k->cp;
2116         opj_tcp_t *tcp = j2k->state == J2K_STATE_TPH ? &cp->tcps[j2k->curtileno] : j2k->default_tcp;
2117         opj_image_t *image = j2k->image;
2118         opj_cio_t *cio = j2k->cio;
2119         
2120         len = cio_read(cio, 2);         /* Lcoc */
2121         compno = cio_read(cio, image->numcomps <= 256 ? 1 : 2); /* Ccoc */
2122         tcp->tccps[compno].csty = cio_read(cio, 1);     /* Scoc */
2123         j2k_read_cox(j2k, compno);
2124 }
2125
2126 /**
2127  * Reads a COC marker (Coding Style Component)
2128  * @param       p_header_data   the data contained in the COC box.
2129  * @param       p_j2k                   the jpeg2000 codec.
2130  * @param       p_header_size   the size of the data contained in the COC marker.
2131  * @param       p_manager               the user event manager.
2132 */
2133 opj_bool j2k_read_coc_v2 (
2134                                         opj_j2k_v2_t *p_j2k,
2135                                         OPJ_BYTE * p_header_data,
2136                                         OPJ_UINT32 p_header_size,
2137                                         struct opj_event_mgr * p_manager
2138                                         )
2139 {
2140         opj_cp_v2_t *l_cp = NULL;
2141         opj_tcp_v2_t *l_tcp = NULL;
2142         opj_image_t *l_image = NULL;
2143         OPJ_UINT32 l_comp_room;
2144         OPJ_UINT32 l_comp_no;
2145
2146         // preconditions
2147         assert(p_header_data != 00);
2148         assert(p_j2k != 00);
2149         assert(p_manager != 00);
2150
2151         l_cp = &(p_j2k->m_cp);
2152         l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH ) ? /*FIXME J2K_DEC_STATE_TPH*/
2153                                 &l_cp->tcps[p_j2k->m_current_tile_number] :
2154                                 p_j2k->m_specific_param.m_decoder.m_default_tcp;
2155         l_image = p_j2k->m_private_image;
2156
2157         l_comp_room = l_image->numcomps <= 256 ? 1 : 2;
2158
2159         // make sure room is sufficient
2160         if (p_header_size < l_comp_room + 1) {
2161                 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading COC marker\n");
2162                 return OPJ_FALSE;
2163         }
2164         p_header_size -= l_comp_room + 1;
2165
2166         opj_read_bytes(p_header_data,&l_comp_no,l_comp_room);                   /* Ccoc */
2167         p_header_data += l_comp_room;
2168         if (l_comp_no >= l_image->numcomps) {
2169                 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading COC marker (bad number of components)\n");
2170                 return OPJ_FALSE;
2171         }
2172
2173         opj_read_bytes(p_header_data,&l_tcp->tccps[l_comp_no].csty,1);                  /* Scoc */
2174         ++p_header_data ;
2175
2176         if (! j2k_read_SPCod_SPCoc(p_j2k,l_comp_no,p_header_data,&p_header_size,p_manager)) {
2177                 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading COC marker\n");
2178                 return OPJ_FALSE;
2179         }
2180
2181         if (p_header_size != 0) {
2182                 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading COC marker\n");
2183                 return OPJ_FALSE;
2184         }
2185         return OPJ_TRUE;
2186 }
2187
2188 static void j2k_write_qcx(opj_j2k_t *j2k, int compno) {
2189         int bandno, numbands;
2190         int expn, mant;
2191         
2192         opj_cp_t *cp = j2k->cp;
2193         opj_tcp_t *tcp = &cp->tcps[j2k->curtileno];
2194         opj_tccp_t *tccp = &tcp->tccps[compno];
2195         opj_cio_t *cio = j2k->cio;
2196         
2197         cio_write(cio, tccp->qntsty + (tccp->numgbits << 5), 1);        /* Sqcx */
2198         numbands = tccp->qntsty == J2K_CCP_QNTSTY_SIQNT ? 1 : tccp->numresolutions * 3 - 2;
2199         
2200         for (bandno = 0; bandno < numbands; bandno++) {
2201                 expn = tccp->stepsizes[bandno].expn;
2202                 mant = tccp->stepsizes[bandno].mant;
2203                 
2204                 if (tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) {
2205                         cio_write(cio, expn << 3, 1);   /* SPqcx_i */
2206                 } else {
2207                         cio_write(cio, (expn << 11) + mant, 2); /* SPqcx_i */
2208                 }
2209         }
2210 }
2211
2212 static void j2k_read_qcx(opj_j2k_t *j2k, int compno, int len) {
2213         int tmp;
2214         int bandno, numbands;
2215
2216         opj_cp_t *cp = j2k->cp;
2217         opj_tcp_t *tcp = j2k->state == J2K_STATE_TPH ? &cp->tcps[j2k->curtileno] : j2k->default_tcp;
2218         opj_tccp_t *tccp = &tcp->tccps[compno];
2219         opj_cio_t *cio = j2k->cio;
2220
2221         tmp = cio_read(cio, 1);         /* Sqcx */
2222         tccp->qntsty = tmp & 0x1f;
2223         tccp->numgbits = tmp >> 5;
2224         numbands = (tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) ? 
2225                 1 : ((tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) ? len - 1 : (len - 1) / 2);
2226
2227 #ifdef USE_JPWL
2228         if (j2k->cp->correct) {
2229
2230                 /* if JPWL is on, we check whether there are too many subbands */
2231                 if ((numbands < 0) || (numbands >= J2K_MAXBANDS)) {
2232                         opj_event_msg(j2k->cinfo, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
2233                                 "JPWL: bad number of subbands in Sqcx (%d)\n",
2234                                 numbands);
2235                         if (!JPWL_ASSUME) {
2236                                 opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
2237                                 return;
2238                         }
2239                         /* we try to correct */
2240                         numbands = 1;
2241                         opj_event_msg(j2k->cinfo, EVT_WARNING, "- trying to adjust them\n"
2242                                 "- setting number of bands to %d => HYPOTHESIS!!!\n",
2243                                 numbands);
2244                 };
2245
2246         };
2247
2248 #else
2249         /* We check whether there are too many subbands */
2250         if ((numbands < 0) || (numbands >= J2K_MAXBANDS)) {
2251                 opj_event_msg(j2k->cinfo, EVT_WARNING ,
2252                                         "bad number of subbands in Sqcx (%d) regarding to J2K_MAXBANDS (%d) \n"
2253                                     "- limiting number of bands to J2K_MAXBANDS and try to move to the next markers\n", numbands, J2K_MAXBANDS);
2254         }
2255
2256 #endif /* USE_JPWL */
2257
2258         for (bandno = 0; bandno < numbands; bandno++) {
2259                 int expn, mant;
2260                 if (tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) {
2261                         expn = cio_read(cio, 1) >> 3;   /* SPqcx_i */
2262                         mant = 0;
2263                 } else {
2264                         tmp = cio_read(cio, 2); /* SPqcx_i */
2265                         expn = tmp >> 11;
2266                         mant = tmp & 0x7ff;
2267                 }
2268                 if (bandno < J2K_MAXBANDS){
2269                         tccp->stepsizes[bandno].expn = expn;
2270                         tccp->stepsizes[bandno].mant = mant;
2271                 }
2272         }
2273         
2274         /* Add Antonin : if scalar_derived -> compute other stepsizes */
2275         if (tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) {
2276                 for (bandno = 1; bandno < J2K_MAXBANDS; bandno++) {
2277                         tccp->stepsizes[bandno].expn = 
2278                                 ((tccp->stepsizes[0].expn) - ((bandno - 1) / 3) > 0) ? 
2279                                         (tccp->stepsizes[0].expn) - ((bandno - 1) / 3) : 0;
2280                         tccp->stepsizes[bandno].mant = tccp->stepsizes[0].mant;
2281                 }
2282         }
2283         /* ddA */
2284 }
2285
2286 static void j2k_write_qcd(opj_j2k_t *j2k) {
2287         int lenp, len;
2288
2289         opj_cio_t *cio = j2k->cio;
2290         
2291         cio_write(cio, J2K_MS_QCD, 2);  /* QCD */
2292         lenp = cio_tell(cio);
2293         cio_skip(cio, 2);
2294         j2k_write_qcx(j2k, 0);
2295         len = cio_tell(cio) - lenp;
2296         cio_seek(cio, lenp);
2297         cio_write(cio, len, 2);                 /* Lqcd */
2298         cio_seek(cio, lenp + len);
2299
2300         if(j2k->cstr_info)
2301           j2k_add_mhmarker(j2k->cstr_info, J2K_MS_QCD, lenp, len);
2302 }
2303
2304 static void j2k_read_qcd(opj_j2k_t *j2k) {
2305         int len, i, pos;
2306
2307         opj_cio_t *cio = j2k->cio;
2308         opj_image_t *image = j2k->image;
2309         
2310         len = cio_read(cio, 2);         /* Lqcd */
2311         pos = cio_tell(cio);
2312         for (i = 0; i < image->numcomps; i++) {
2313                 cio_seek(cio, pos);
2314                 j2k_read_qcx(j2k, i, len - 2);
2315         }
2316 }
2317
2318 /**
2319  * Reads a QCD marker (Quantization defaults)
2320  * @param       p_header_data   the data contained in the QCD box.
2321  * @param       p_j2k                   the jpeg2000 codec.
2322  * @param       p_header_size   the size of the data contained in the QCD marker.
2323  * @param       p_manager               the user event manager.
2324 */
2325 opj_bool j2k_read_qcd_v2 (
2326                                     opj_j2k_v2_t *p_j2k,
2327                                         OPJ_BYTE * p_header_data,
2328                                         OPJ_UINT32 p_header_size,
2329                                         struct opj_event_mgr * p_manager
2330                                         )
2331 {
2332         /* preconditions */
2333         assert(p_header_data != 00);
2334         assert(p_j2k != 00);
2335         assert(p_manager != 00);
2336
2337         if (! j2k_read_SQcd_SQcc(p_j2k,0,p_header_data,&p_header_size,p_manager)) {
2338                 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading QCD marker\n");
2339                 return OPJ_FALSE;
2340         }
2341
2342         if (p_header_size != 0) {
2343                 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading QCD marker\n");
2344                 return OPJ_FALSE;
2345         }
2346
2347         /* Apply the quantization parameters to other components of the current tile or the m_default_tcp */
2348         j2k_copy_tile_quantization_parameters(p_j2k);
2349
2350         return OPJ_TRUE;
2351 }
2352
2353 static void j2k_write_qcc(opj_j2k_t *j2k, int compno) {
2354         int lenp, len;
2355
2356         opj_cio_t *cio = j2k->cio;
2357         
2358         cio_write(cio, J2K_MS_QCC, 2);  /* QCC */
2359         lenp = cio_tell(cio);
2360         cio_skip(cio, 2);
2361         cio_write(cio, compno, j2k->image->numcomps <= 256 ? 1 : 2);    /* Cqcc */
2362         j2k_write_qcx(j2k, compno);
2363         len = cio_tell(cio) - lenp;
2364         cio_seek(cio, lenp);
2365         cio_write(cio, len, 2);                 /* Lqcc */
2366         cio_seek(cio, lenp + len);
2367 }
2368
2369 static void j2k_read_qcc(opj_j2k_t *j2k) {
2370         int len, compno;
2371         int numcomp = j2k->image->numcomps;
2372         opj_cio_t *cio = j2k->cio;
2373
2374         len = cio_read(cio, 2); /* Lqcc */
2375         compno = cio_read(cio, numcomp <= 256 ? 1 : 2); /* Cqcc */
2376
2377 #ifdef USE_JPWL
2378         if (j2k->cp->correct) {
2379
2380                 static int backup_compno = 0;
2381
2382                 /* compno is negative or larger than the number of components!!! */
2383                 if ((compno < 0) || (compno >= numcomp)) {
2384                         opj_event_msg(j2k->cinfo, EVT_ERROR,
2385                                 "JPWL: bad component number in QCC (%d out of a maximum of %d)\n",
2386                                 compno, numcomp);
2387                         if (!JPWL_ASSUME) {
2388                                 opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
2389                                 return;
2390                         }
2391                         /* we try to correct */
2392                         compno = backup_compno % numcomp;
2393                         opj_event_msg(j2k->cinfo, EVT_WARNING, "- trying to adjust this\n"
2394                                 "- setting component number to %d\n",
2395                                 compno);
2396                 }
2397
2398                 /* keep your private count of tiles */
2399                 backup_compno++;
2400         };
2401 #endif /* USE_JPWL */
2402
2403         j2k_read_qcx(j2k, compno, len - 2 - (numcomp <= 256 ? 1 : 2));
2404 }
2405
2406 /**
2407  * Reads a QCC marker (Quantization component)
2408  * @param       p_header_data   the data contained in the QCC box.
2409  * @param       p_j2k                   the jpeg2000 codec.
2410  * @param       p_header_size   the size of the data contained in the QCC marker.
2411  * @param       p_manager               the user event manager.
2412 */
2413 opj_bool j2k_read_qcc_v2(       opj_j2k_v2_t *p_j2k,
2414                                                         OPJ_BYTE * p_header_data,
2415                                                         OPJ_UINT32 p_header_size,
2416                                                         struct opj_event_mgr * p_manager)
2417 {
2418         OPJ_UINT32 l_num_comp,l_comp_no;
2419
2420         /* preconditions */
2421         assert(p_header_data != 00);
2422         assert(p_j2k != 00);
2423         assert(p_manager != 00);
2424
2425         l_num_comp = p_j2k->m_private_image->numcomps;
2426
2427         if (l_num_comp <= 256) {
2428                 if (p_header_size < 1) {
2429                         opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading QCC marker\n");
2430                         return OPJ_FALSE;
2431                 }
2432                 opj_read_bytes(p_header_data,&l_comp_no,1);
2433                 ++p_header_data;
2434                 --p_header_size;
2435         }
2436         else {
2437                 if (p_header_size < 2) {
2438                         opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading QCC marker\n");
2439                         return OPJ_FALSE;
2440                 }
2441                 opj_read_bytes(p_header_data,&l_comp_no,2);
2442                 p_header_data+=2;
2443                 p_header_size-=2;
2444         }
2445
2446 #ifdef USE_JPWL
2447         if (p_j2k->m_cp.correct) {
2448
2449                 static OPJ_UINT32 backup_compno = 0;
2450
2451                 /* compno is negative or larger than the number of components!!! */
2452                 if ((l_comp_no < 0) || (l_comp_no >= l_num_comp)) {
2453                         opj_event_msg_v2(p_manager, EVT_ERROR,
2454                                 "JPWL: bad component number in QCC (%d out of a maximum of %d)\n",
2455                                 l_comp_no, l_num_comp);
2456                         if (!JPWL_ASSUME) {
2457                                 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
2458                                 return OPJ_FALSE;
2459                         }
2460                         /* we try to correct */
2461                         l_comp_no = backup_compno % l_num_comp;
2462                         opj_event_msg_v2(p_manager, EVT_WARNING, "- trying to adjust this\n"
2463                                 "- setting component number to %d\n",
2464                                 l_comp_no);
2465                 }
2466
2467                 /* keep your private count of tiles */
2468                 backup_compno++;
2469         };
2470 #endif /* USE_JPWL */
2471
2472         if (! j2k_read_SQcd_SQcc(p_j2k,l_comp_no,p_header_data,&p_header_size,p_manager)) {
2473                 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading QCC marker\n");
2474                 return OPJ_FALSE;
2475         }
2476
2477         if (p_header_size != 0) {
2478                 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading QCC marker\n");
2479                 return OPJ_FALSE;
2480         }
2481
2482         return OPJ_TRUE;
2483 }
2484
2485
2486 static void j2k_write_poc(opj_j2k_t *j2k) {
2487         int len, numpchgs, i;
2488
2489         int numcomps = j2k->image->numcomps;
2490         
2491         opj_cp_t *cp = j2k->cp;
2492         opj_tcp_t *tcp = &cp->tcps[j2k->curtileno];
2493         opj_tccp_t *tccp = &tcp->tccps[0];
2494         opj_cio_t *cio = j2k->cio;
2495
2496         numpchgs = 1 + tcp->numpocs;
2497         cio_write(cio, J2K_MS_POC, 2);  /* POC  */
2498         len = 2 + (5 + 2 * (numcomps <= 256 ? 1 : 2)) * numpchgs;
2499         cio_write(cio, len, 2);         /* Lpoc */
2500         for (i = 0; i < numpchgs; i++) {
2501                 opj_poc_t *poc = &tcp->pocs[i];
2502                 cio_write(cio, poc->resno0, 1); /* RSpoc_i */
2503                 cio_write(cio, poc->compno0, (numcomps <= 256 ? 1 : 2));        /* CSpoc_i */
2504                 cio_write(cio, poc->layno1, 2); /* LYEpoc_i */
2505                 poc->layno1 = int_min(poc->layno1, tcp->numlayers);
2506                 cio_write(cio, poc->resno1, 1); /* REpoc_i */
2507                 poc->resno1 = int_min(poc->resno1, tccp->numresolutions);
2508                 cio_write(cio, poc->compno1, (numcomps <= 256 ? 1 : 2));        /* CEpoc_i */
2509                 poc->compno1 = int_min(poc->compno1, numcomps);
2510                 cio_write(cio, poc->prg, 1);    /* Ppoc_i */
2511         }
2512 }
2513
2514 static void j2k_read_poc(opj_j2k_t *j2k) {
2515         int len, numpchgs, i, old_poc;
2516
2517         int numcomps = j2k->image->numcomps;
2518         
2519         opj_cp_t *cp = j2k->cp;
2520         opj_tcp_t *tcp = j2k->state == J2K_STATE_TPH ? &cp->tcps[j2k->curtileno] : j2k->default_tcp;
2521         opj_cio_t *cio = j2k->cio;
2522         
2523         old_poc = tcp->POC ? tcp->numpocs + 1 : 0;
2524         tcp->POC = 1;
2525         len = cio_read(cio, 2);         /* Lpoc */
2526         numpchgs = (len - 2) / (5 + 2 * (numcomps <= 256 ? 1 : 2));
2527         
2528         for (i = old_poc; i < numpchgs + old_poc; i++) {
2529                 opj_poc_t *poc;
2530                 poc = &tcp->pocs[i];
2531                 poc->resno0 = cio_read(cio, 1); /* RSpoc_i */
2532                 poc->compno0 = cio_read(cio, numcomps <= 256 ? 1 : 2);  /* CSpoc_i */
2533                 poc->layno1 = cio_read(cio, 2);    /* LYEpoc_i */
2534                 poc->resno1 = cio_read(cio, 1);    /* REpoc_i */
2535                 poc->compno1 = int_min(
2536                         cio_read(cio, numcomps <= 256 ? 1 : 2), (unsigned int) numcomps);       /* CEpoc_i */
2537                 poc->prg = (OPJ_PROG_ORDER)cio_read(cio, 1);    /* Ppoc_i */
2538         }
2539         
2540         tcp->numpocs = numpchgs + old_poc - 1;
2541 }
2542
2543 /**
2544  * Reads a POC marker (Progression Order Change)
2545  *
2546  * @param       p_header_data   the data contained in the POC box.
2547  * @param       p_j2k                   the jpeg2000 codec.
2548  * @param       p_header_size   the size of the data contained in the POC marker.
2549  * @param       p_manager               the user event manager.
2550 */
2551 opj_bool j2k_read_poc_v2 (
2552                                                 opj_j2k_v2_t *p_j2k,
2553                                                 OPJ_BYTE * p_header_data,
2554                                                 OPJ_UINT32 p_header_size,
2555                                                 struct opj_event_mgr * p_manager)
2556 {
2557         OPJ_UINT32 i, l_nb_comp, l_tmp;
2558         opj_image_t * l_image = 00;
2559         OPJ_UINT32 l_old_poc_nb, l_current_poc_nb, l_current_poc_remaining;
2560         OPJ_UINT32 l_chunk_size, l_comp_room;
2561
2562         opj_cp_v2_t *l_cp = 00;
2563         opj_tcp_v2_t *l_tcp = 00;
2564         opj_poc_t *l_current_poc = 00;
2565
2566         /* preconditions */
2567         assert(p_header_data != 00);
2568         assert(p_j2k != 00);
2569         assert(p_manager != 00);
2570
2571         l_image = p_j2k->m_private_image;
2572         l_nb_comp = l_image->numcomps;
2573         if (l_nb_comp <= 256) {
2574                 l_comp_room = 1;
2575         }
2576         else {
2577                 l_comp_room = 2;
2578         }
2579         l_chunk_size = 5 + 2 * l_comp_room;
2580         l_current_poc_nb = p_header_size / l_chunk_size;
2581         l_current_poc_remaining = p_header_size % l_chunk_size;
2582
2583         if ((l_current_poc_nb <= 0) || (l_current_poc_remaining != 0)) {
2584                 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading POC marker\n");
2585                 return OPJ_FALSE;
2586         }
2587
2588         l_cp = &(p_j2k->m_cp);
2589         l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ?
2590                                 &l_cp->tcps[p_j2k->m_current_tile_number] :
2591                                 p_j2k->m_specific_param.m_decoder.m_default_tcp;
2592         l_old_poc_nb = l_tcp->POC ? l_tcp->numpocs + 1 : 0;
2593         l_current_poc_nb += l_old_poc_nb;
2594
2595         assert(l_current_poc_nb < 32);
2596
2597         /* now poc is in use.*/
2598         l_tcp->POC = 1;
2599
2600         l_current_poc = &l_tcp->pocs[l_old_poc_nb];
2601         for     (i = l_old_poc_nb; i < l_current_poc_nb; ++i) {
2602                 opj_read_bytes(p_header_data,&(l_current_poc->resno0),1);                               /* RSpoc_i */
2603                 ++p_header_data;
2604                 opj_read_bytes(p_header_data,&(l_current_poc->compno0),l_comp_room);    /* CSpoc_i */
2605                 p_header_data+=l_comp_room;
2606                 opj_read_bytes(p_header_data,&(l_current_poc->layno1),2);                               /* LYEpoc_i */
2607                 p_header_data+=2;
2608                 opj_read_bytes(p_header_data,&(l_current_poc->resno1),1);                               /* REpoc_i */
2609                 ++p_header_data;
2610                 opj_read_bytes(p_header_data,&(l_current_poc->compno1),l_comp_room);    /* CEpoc_i */
2611                 p_header_data+=l_comp_room;
2612                 opj_read_bytes(p_header_data,&l_tmp,1);                                                                 /* Ppoc_i */
2613                 ++p_header_data;
2614                 l_current_poc->prg = (OPJ_PROG_ORDER) l_tmp;
2615                 /* make sure comp is in acceptable bounds */
2616                 l_current_poc->compno1 = uint_min(l_current_poc->compno1, l_nb_comp);
2617                 ++l_current_poc;
2618         }
2619
2620         l_tcp->numpocs = l_current_poc_nb - 1;
2621         return OPJ_TRUE;
2622 }
2623
2624 static void j2k_read_crg(opj_j2k_t *j2k) {
2625         int len, i, Xcrg_i, Ycrg_i;
2626         
2627         opj_cio_t *cio = j2k->cio;
2628         int numcomps = j2k->image->numcomps;
2629         
2630         len = cio_read(cio, 2);                 /* Lcrg */
2631         for (i = 0; i < numcomps; i++) {
2632                 Xcrg_i = cio_read(cio, 2);      /* Xcrg_i */
2633                 Ycrg_i = cio_read(cio, 2);      /* Ycrg_i */
2634         }
2635 }
2636
2637 /**
2638  * Reads a CRG marker (Component registration)
2639  *
2640  * @param       p_header_data   the data contained in the TLM box.
2641  * @param       p_j2k                   the jpeg2000 codec.
2642  * @param       p_header_size   the size of the data contained in the TLM marker.
2643  * @param       p_manager               the user event manager.
2644 */
2645 opj_bool j2k_read_crg_v2 (
2646                                                 opj_j2k_v2_t *p_j2k,
2647                                                 OPJ_BYTE * p_header_data,
2648                                                 OPJ_UINT32 p_header_size,
2649                                                 struct opj_event_mgr * p_manager
2650                                         )
2651 {
2652         OPJ_UINT32 l_nb_comp;
2653         // preconditions
2654         assert(p_header_data != 00);
2655         assert(p_j2k != 00);
2656         assert(p_manager != 00);
2657
2658         l_nb_comp = p_j2k->m_private_image->numcomps;
2659
2660         if (p_header_size != l_nb_comp *4) {
2661                 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading CRG marker\n");
2662                 return OPJ_FALSE;
2663         }
2664         /* Do not care of this at the moment since only local variables are set here */
2665         /*
2666         for
2667                 (i = 0; i < l_nb_comp; ++i)
2668         {
2669                 opj_read_bytes(p_header_data,&l_Xcrg_i,2);                              // Xcrg_i
2670                 p_header_data+=2;
2671                 opj_read_bytes(p_header_data,&l_Ycrg_i,2);                              // Xcrg_i
2672                 p_header_data+=2;
2673         }
2674         */
2675         return OPJ_TRUE;
2676 }
2677
2678 static void j2k_read_tlm(opj_j2k_t *j2k) {
2679         int len, Ztlm, Stlm, ST, SP, tile_tlm, i;
2680         long int Ttlm_i, Ptlm_i;
2681
2682         opj_cio_t *cio = j2k->cio;
2683         
2684         len = cio_read(cio, 2);         /* Ltlm */
2685         Ztlm = cio_read(cio, 1);        /* Ztlm */
2686         Stlm = cio_read(cio, 1);        /* Stlm */
2687         ST = ((Stlm >> 4) & 0x01) + ((Stlm >> 4) & 0x02);
2688         SP = (Stlm >> 6) & 0x01;
2689         tile_tlm = (len - 4) / ((SP + 1) * 2 + ST);
2690         for (i = 0; i < tile_tlm; i++) {
2691                 Ttlm_i = cio_read(cio, ST);     /* Ttlm_i */
2692                 Ptlm_i = cio_read(cio, SP ? 4 : 2);     /* Ptlm_i */
2693         }
2694 }
2695
2696 /**
2697  * Reads a TLM marker (Tile Length Marker)
2698  *
2699  * @param       p_header_data   the data contained in the TLM box.
2700  * @param       p_j2k                   the jpeg2000 codec.
2701  * @param       p_header_size   the size of the data contained in the TLM marker.
2702  * @param       p_manager               the user event manager.
2703 */
2704 opj_bool j2k_read_tlm_v2 (
2705                                                 opj_j2k_v2_t *p_j2k,
2706                                                 OPJ_BYTE * p_header_data,
2707                                                 OPJ_UINT32 p_header_size,
2708                                                 struct opj_event_mgr * p_manager
2709                                         )
2710 {
2711         OPJ_UINT32 l_Ztlm, l_Stlm, l_ST, l_SP, l_tot_num_tp, l_tot_num_tp_remaining, l_quotient, l_Ptlm_size;
2712         // preconditions
2713         assert(p_header_data != 00);
2714         assert(p_j2k != 00);
2715         assert(p_manager != 00);
2716
2717         if (p_header_size < 2) {
2718                 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading TLM marker\n");
2719                 return OPJ_FALSE;
2720         }
2721         p_header_size -= 2;
2722
2723         opj_read_bytes(p_header_data,&l_Ztlm,1);                                /* Ztlm */
2724         ++p_header_data;
2725         opj_read_bytes(p_header_data,&l_Stlm,1);                                /* Stlm */
2726         ++p_header_data;
2727
2728         l_ST = ((l_Stlm >> 4) & 0x3);
2729         l_SP = (l_Stlm >> 6) & 0x1;
2730
2731         l_Ptlm_size = (l_SP + 1) * 2;
2732         l_quotient = l_Ptlm_size + l_ST;
2733
2734         l_tot_num_tp = p_header_size / l_quotient;
2735         l_tot_num_tp_remaining = p_header_size % l_quotient;
2736
2737         if (l_tot_num_tp_remaining != 0) {
2738                 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading TLM marker\n");
2739                 return OPJ_FALSE;
2740         }
2741         /* FIXME Do not care of this at the moment since only local variables are set here */
2742         /*
2743         for
2744                 (i = 0; i < l_tot_num_tp; ++i)
2745         {
2746                 opj_read_bytes(p_header_data,&l_Ttlm_i,l_ST);                           // Ttlm_i
2747                 p_header_data += l_ST;
2748                 opj_read_bytes(p_header_data,&l_Ptlm_i,l_Ptlm_size);            // Ptlm_i
2749                 p_header_data += l_Ptlm_size;
2750         }*/
2751         return OPJ_TRUE;
2752 }
2753
2754 static void j2k_read_plm(opj_j2k_t *j2k) {
2755         int len, i, Zplm, Nplm, add, packet_len = 0;
2756         
2757         opj_cio_t *cio = j2k->cio;
2758
2759         len = cio_read(cio, 2);         /* Lplm */
2760         Zplm = cio_read(cio, 1);        /* Zplm */
2761         len -= 3;
2762         while (len > 0) {
2763                 Nplm = cio_read(cio, 4);                /* Nplm */
2764                 len -= 4;
2765                 for (i = Nplm; i > 0; i--) {
2766                         add = cio_read(cio, 1);
2767                         len--;
2768                         packet_len = (packet_len << 7) + add;   /* Iplm_ij */
2769                         if ((add & 0x80) == 0) {
2770                                 /* New packet */
2771                                 packet_len = 0;
2772                         }
2773                         if (len <= 0)
2774                                 break;
2775                 }
2776         }
2777 }
2778
2779 /**
2780  * Reads a PLM marker (Packet length, main header marker)
2781  *
2782  * @param       p_header_data   the data contained in the TLM box.
2783  * @param       p_j2k                   the jpeg2000 codec.
2784  * @param       p_header_size   the size of the data contained in the TLM marker.
2785  * @param       p_manager               the user event manager.
2786 */
2787 opj_bool j2k_read_plm_v2 (
2788                                                 opj_j2k_v2_t *p_j2k,
2789                                                 OPJ_BYTE * p_header_data,
2790                                                 OPJ_UINT32 p_header_size,
2791                                                 struct opj_event_mgr * p_manager
2792                                         )
2793 {
2794         // preconditions
2795         assert(p_header_data != 00);
2796         assert(p_j2k != 00);
2797         assert(p_manager != 00);
2798
2799         if (p_header_size < 1) {
2800                 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PLM marker\n");
2801                 return OPJ_FALSE;
2802         }
2803         /* Do not care of this at the moment since only local variables are set here */
2804         /*
2805         opj_read_bytes(p_header_data,&l_Zplm,1);                                        // Zplm
2806         ++p_header_data;
2807         --p_header_size;
2808
2809         while
2810                 (p_header_size > 0)
2811         {
2812                 opj_read_bytes(p_header_data,&l_Nplm,1);                                // Nplm
2813                 ++p_header_data;
2814                 p_header_size -= (1+l_Nplm);
2815                 if
2816                         (p_header_size < 0)
2817                 {
2818                         opj_event_msg(p_manager, EVT_ERROR, "Error reading PLM marker\n");
2819                         return false;
2820                 }
2821                 for
2822                         (i = 0; i < l_Nplm; ++i)
2823                 {
2824                         opj_read_bytes(p_header_data,&l_tmp,1);                         // Iplm_ij
2825                         ++p_header_data;
2826                         // take only the last seven bytes
2827                         l_packet_len |= (l_tmp & 0x7f);
2828                         if
2829                                 (l_tmp & 0x80)
2830                         {
2831                                 l_packet_len <<= 7;
2832                         }
2833                         else
2834                         {
2835                 // store packet length and proceed to next packet
2836                                 l_packet_len = 0;
2837                         }
2838                 }
2839                 if
2840                         (l_packet_len != 0)
2841                 {
2842                         opj_event_msg(p_manager, EVT_ERROR, "Error reading PLM marker\n");
2843                         return false;
2844                 }
2845         }
2846         */
2847         return OPJ_TRUE;
2848 }
2849
2850 static void j2k_read_plt(opj_j2k_t *j2k) {
2851         int len, i, Zplt, packet_len = 0, add;
2852         
2853         opj_cio_t *cio = j2k->cio;
2854         
2855         len = cio_read(cio, 2);         /* Lplt */
2856         Zplt = cio_read(cio, 1);        /* Zplt */
2857         for (i = len - 3; i > 0; i--) {
2858                 add = cio_read(cio, 1);
2859                 packet_len = (packet_len << 7) + add;   /* Iplt_i */
2860                 if ((add & 0x80) == 0) {
2861                         /* New packet */
2862                         packet_len = 0;
2863                 }
2864         }
2865 }
2866
2867 /**
2868  * Reads a PLT marker (Packet length, tile-part header)
2869  *
2870  * @param       p_header_data   the data contained in the PLT box.
2871  * @param       p_j2k                   the jpeg2000 codec.
2872  * @param       p_header_size   the size of the data contained in the PLT marker.
2873  * @param       p_manager               the user event manager.
2874 */
2875 opj_bool j2k_read_plt_v2 (
2876                                                 opj_j2k_v2_t *p_j2k,
2877                                                 OPJ_BYTE * p_header_data,
2878                                                 OPJ_UINT32 p_header_size,
2879                                                 struct opj_event_mgr * p_manager
2880                                         )
2881 {
2882         OPJ_UINT32 l_Zplt, l_tmp, l_packet_len = 0, i;
2883
2884         // preconditions
2885         assert(p_header_data != 00);
2886         assert(p_j2k != 00);
2887         assert(p_manager != 00);
2888
2889         if (p_header_size < 1) {
2890                 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PLM marker\n");
2891                 return OPJ_FALSE;
2892         }
2893
2894         opj_read_bytes(p_header_data,&l_Zplt,1);                /* Zplt */
2895         ++p_header_data;
2896         --p_header_size;
2897
2898         for (i = 0; i < p_header_size; ++i) {
2899                 opj_read_bytes(p_header_data,&l_tmp,1);         /* Iplt_ij */
2900                 ++p_header_data;
2901                 // take only the last seven bytes
2902                 l_packet_len |= (l_tmp & 0x7f);
2903                 if (l_tmp & 0x80) {
2904                         l_packet_len <<= 7;
2905                 }
2906                 else {
2907             // store packet length and proceed to next packet
2908                         l_packet_len = 0;
2909                 }
2910         }
2911
2912         if (l_packet_len != 0) {
2913                 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PLM marker\n");
2914                 return OPJ_FALSE;
2915         }
2916
2917         return OPJ_TRUE;
2918 }
2919
2920 static void j2k_read_ppm(opj_j2k_t *j2k) {
2921         int len, Z_ppm, i, j;
2922         int N_ppm;
2923
2924         opj_cp_t *cp = j2k->cp;
2925         opj_cio_t *cio = j2k->cio;
2926         
2927         len = cio_read(cio, 2);
2928         cp->ppm = 1;
2929         
2930         Z_ppm = cio_read(cio, 1);       /* Z_ppm */
2931         len -= 3;
2932         while (len > 0) {
2933                 if (cp->ppm_previous == 0) {
2934                         N_ppm = cio_read(cio, 4);       /* N_ppm */
2935                         len -= 4;
2936                 } else {
2937                         N_ppm = cp->ppm_previous;
2938                 }
2939                 j = cp->ppm_store;
2940                 if (Z_ppm == 0) {       /* First PPM marker */
2941                         cp->ppm_data = (unsigned char *) opj_malloc(N_ppm * sizeof(unsigned char));
2942                         cp->ppm_data_first = cp->ppm_data;
2943                         cp->ppm_len = N_ppm;
2944                 } else {                        /* NON-first PPM marker */
2945                         cp->ppm_data = (unsigned char *) opj_realloc(cp->ppm_data, (N_ppm +     cp->ppm_store) * sizeof(unsigned char));
2946
2947 #ifdef USE_JPWL
2948                         /* this memory allocation check could be done even in non-JPWL cases */
2949                         if (cp->correct) {
2950                                 if (!cp->ppm_data) {
2951                                         opj_event_msg(j2k->cinfo, EVT_ERROR,
2952                                                 "JPWL: failed memory allocation during PPM marker parsing (pos. %x)\n",
2953                                                 cio_tell(cio));
2954                                         if (!JPWL_ASSUME || JPWL_ASSUME) {
2955                                                 opj_free(cp->ppm_data);
2956                                                 opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
2957                                                 return;
2958                                         }
2959                                 }
2960                         }
2961 #endif
2962
2963                         cp->ppm_data_first = cp->ppm_data;
2964                         cp->ppm_len = N_ppm + cp->ppm_store;
2965                 }
2966                 for (i = N_ppm; i > 0; i--) {   /* Read packet header */
2967                         cp->ppm_data[j] = cio_read(cio, 1);
2968                         j++;
2969                         len--;
2970                         if (len == 0)
2971                                 break;                  /* Case of non-finished packet header in present marker but finished in next one */
2972                 }
2973                 cp->ppm_previous = i - 1;
2974                 cp->ppm_store = j;
2975         }
2976 }
2977 /**
2978  * Reads a PPM marker (Packed packet headers, main header)
2979  *
2980  * @param       p_header_data   the data contained in the POC box.
2981  * @param       p_j2k                   the jpeg2000 codec.
2982  * @param       p_header_size   the size of the data contained in the POC marker.
2983  * @param       p_manager               the user event manager.
2984 */
2985 opj_bool j2k_read_ppm_v2 (
2986                                                 opj_j2k_v2_t *p_j2k,
2987                                                 OPJ_BYTE * p_header_data,
2988                                                 OPJ_UINT32 p_header_size,
2989                                                 struct opj_event_mgr * p_manager
2990                                         )
2991 {
2992
2993         opj_cp_v2_t *l_cp = 00;
2994         OPJ_UINT32 l_remaining_data, l_Z_ppm, l_N_ppm;
2995
2996         // preconditions
2997         assert(p_header_data != 00);
2998         assert(p_j2k != 00);
2999         assert(p_manager != 00);
3000
3001         if (p_header_size < 1) {
3002                 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PPM marker\n");
3003                 return OPJ_FALSE;
3004         }
3005
3006         l_cp = &(p_j2k->m_cp);
3007         l_cp->ppm = 1;
3008
3009         opj_read_bytes(p_header_data,&l_Z_ppm,1);               /* Z_ppm */
3010         ++p_header_data;
3011         --p_header_size;
3012
3013         // First PPM marker
3014         if (l_Z_ppm == 0) {
3015                 if (p_header_size < 4) {
3016                         opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PPM marker\n");
3017                         return OPJ_FALSE;
3018                 }
3019
3020                 opj_read_bytes(p_header_data,&l_N_ppm,4);               /* N_ppm */
3021                 p_header_data+=4;
3022                 p_header_size-=4;
3023
3024                 /* First PPM marker: Initialization */
3025                 l_cp->ppm_len = l_N_ppm;
3026                 l_cp->ppm_data_size = 0;
3027
3028                 l_cp->ppm_buffer = (OPJ_BYTE *) opj_malloc(l_cp->ppm_len);
3029                 if (l_cp->ppm_buffer == 00) {
3030                         opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory reading ppm marker\n");
3031                         return OPJ_FALSE;
3032                 }
3033                 memset(l_cp->ppm_buffer,0,l_cp->ppm_len);
3034
3035                 l_cp->ppm_data = l_cp->ppm_buffer;
3036         }
3037
3038         while (1) {
3039                 if (l_cp->ppm_data_size == l_cp->ppm_len) {
3040                         if (p_header_size >= 4) {
3041                                 // read a N_ppm
3042                                 opj_read_bytes(p_header_data,&l_N_ppm,4);               /* N_ppm */
3043                                 p_header_data+=4;
3044                                 p_header_size-=4;
3045                                 l_cp->ppm_len += l_N_ppm ;
3046
3047                                 l_cp->ppm_buffer = (OPJ_BYTE *) opj_realloc(l_cp->ppm_buffer, l_cp->ppm_len);
3048                                 if (l_cp->ppm_buffer == 00) {
3049                                         opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory reading ppm marker\n");
3050                                         return OPJ_FALSE;
3051                                 }
3052                                 memset(l_cp->ppm_buffer+l_cp->ppm_data_size,0,l_N_ppm);
3053
3054                                 l_cp->ppm_data = l_cp->ppm_buffer;
3055                         }
3056                         else {
3057                                 return OPJ_FALSE;
3058                         }
3059                 }
3060
3061                 l_remaining_data = l_cp->ppm_len - l_cp->ppm_data_size;
3062
3063                 if (l_remaining_data <= p_header_size) {
3064                         /* we must store less information than available in the packet */
3065                         memcpy(l_cp->ppm_buffer + l_cp->ppm_data_size , p_header_data , l_remaining_data);
3066                         l_cp->ppm_data_size = l_cp->ppm_len;
3067                         p_header_size -= l_remaining_data;
3068                         p_header_data += l_remaining_data;
3069                 }
3070                 else {
3071                         memcpy(l_cp->ppm_buffer + l_cp->ppm_data_size , p_header_data , p_header_size);
3072                         l_cp->ppm_data_size += p_header_size;
3073                         p_header_data += p_header_size;
3074                         p_header_size = 0;
3075                         break;
3076                 }
3077         }
3078
3079         return OPJ_TRUE;
3080 }
3081
3082
3083
3084 /**
3085  * Reads a PPM marker (Packed packet headers, main header)
3086  *
3087  * @param       p_header_data   the data contained in the POC box.
3088  * @param       p_j2k                   the jpeg2000 codec.
3089  * @param       p_header_size   the size of the data contained in the POC marker.
3090  * @param       p_manager               the user event manager.
3091 */
3092 opj_bool j2k_read_ppm_v3 (
3093                                                 opj_j2k_v2_t *p_j2k,
3094                                                 OPJ_BYTE * p_header_data,
3095                                                 OPJ_UINT32 p_header_size,
3096                                                 struct opj_event_mgr * p_manager
3097                                         )
3098 {
3099         opj_cp_v2_t *l_cp = 00;
3100         OPJ_UINT32 l_remaining_data, l_Z_ppm, l_N_ppm;
3101
3102         // preconditions
3103         assert(p_header_data != 00);
3104         assert(p_j2k != 00);
3105         assert(p_manager != 00);
3106
3107         // Minimum size of PPM marker is equal to the size of Zppm element
3108         if (p_header_size < 1) {
3109                 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PPM marker\n");
3110                 return OPJ_FALSE;
3111         }
3112
3113         l_cp = &(p_j2k->m_cp);
3114         l_cp->ppm = 1;
3115
3116         opj_read_bytes(p_header_data,&l_Z_ppm,1);               /* Z_ppm */
3117         ++p_header_data;
3118         --p_header_size;
3119
3120         // First PPM marker
3121         if (l_Z_ppm == 0) {
3122                 // We need now at least the Nppm^0 element
3123                 if (p_header_size < 4) {
3124                         opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PPM marker\n");
3125                         return OPJ_FALSE;
3126                 }
3127
3128                 opj_read_bytes(p_header_data,&l_N_ppm,4);               /* First N_ppm */
3129                 p_header_data+=4;
3130                 p_header_size-=4;
3131
3132                 /* First PPM marker: Initialization */
3133                 l_cp->ppm_len = l_N_ppm;
3134                 l_cp->ppm_data_read = 0;
3135
3136                 l_cp->ppm_data = (OPJ_BYTE *) opj_malloc(l_cp->ppm_len);
3137                 if (l_cp->ppm_data == 00) {
3138                         opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory reading ppm marker\n");
3139                         return OPJ_FALSE;
3140                 }
3141                 memset(l_cp->ppm_data,0,l_cp->ppm_len);
3142
3143                 l_cp->ppm_data_current = l_cp->ppm_data;
3144
3145                 //l_cp->ppm_data = l_cp->ppm_buffer;
3146         }
3147         else {
3148                 if (p_header_size < 4) {
3149                         opj_event_msg_v2(p_manager, EVT_WARNING, "Empty PPM marker\n");
3150                         return OPJ_TRUE;
3151                 }
3152                 else {
3153                         // Uncompleted Ippm series in the previous PPM marker?
3154                         if (l_cp->ppm_data_read < l_cp->ppm_len) {
3155                                 // Get the place where add the remaining Ippm series
3156                                 l_cp->ppm_data_current = &(l_cp->ppm_data[l_cp->ppm_data_read]);
3157                                 l_N_ppm = l_cp->ppm_len - l_cp->ppm_data_read;
3158                         }
3159                         else {
3160                                 opj_read_bytes(p_header_data,&l_N_ppm,4);               /* First N_ppm */
3161                                 p_header_data+=4;
3162                                 p_header_size-=4;
3163
3164                                 // Increase the size of ppm_data to add the new Ippm series
3165                                 l_cp->ppm_data = (OPJ_BYTE *) opj_realloc(l_cp->ppm_data, l_cp->ppm_len + l_N_ppm);
3166
3167                                 // Keep the position of the place where concatenate the new series
3168                                 l_cp->ppm_data_current = &(l_cp->ppm_data[l_cp->ppm_len]);
3169                                 l_cp->ppm_len += l_N_ppm;
3170                         }
3171                 }
3172         }
3173
3174         l_remaining_data = p_header_size;
3175
3176         while (l_remaining_data >= l_N_ppm) {
3177                 // read a complete Ippm series
3178                 memcpy(l_cp->ppm_data_current, p_header_data, l_N_ppm);
3179                 p_header_size -= l_N_ppm;
3180                 p_header_data += l_N_ppm;
3181
3182                 l_cp->ppm_data_read += l_N_ppm; // Increase the number of data read
3183
3184                 if (p_header_size)
3185                 {
3186                         opj_read_bytes(p_header_data,&l_N_ppm,4);               /* N_ppm^i */
3187                         p_header_data+=4;
3188                         p_header_size-=4;
3189                 }
3190                 else {
3191                         l_remaining_data = p_header_size;
3192                         break;
3193                 }
3194
3195                 l_remaining_data = p_header_size;
3196
3197                 // Next Ippm series is a complete series ?
3198                 if (l_remaining_data > l_N_ppm) {
3199                         // Increase the size of ppm_data to add the new Ippm series
3200                         l_cp->ppm_data = (OPJ_BYTE *) opj_realloc(l_cp->ppm_data, l_cp->ppm_len + l_N_ppm);
3201
3202                         // Keep the position of the place where concatenate the new series
3203                         l_cp->ppm_data_current = &(l_cp->ppm_data[l_cp->ppm_len]);
3204                         l_cp->ppm_len += l_N_ppm;
3205                 }
3206
3207         }
3208
3209         // Need to read an incomplete Ippm series
3210         if (l_remaining_data) {
3211                 l_cp->ppm_data = (OPJ_BYTE *) opj_realloc(l_cp->ppm_data, l_cp->ppm_len + l_N_ppm);
3212
3213                 // Keep the position of the place where concatenate the new series
3214                 l_cp->ppm_data_current = &(l_cp->ppm_data[l_cp->ppm_len]);
3215                 l_cp->ppm_len += l_N_ppm;
3216
3217                 // Read incomplete Ippm series
3218                 memcpy(l_cp->ppm_data_current, p_header_data, l_remaining_data);
3219                 p_header_size -= l_remaining_data;
3220                 p_header_data += l_remaining_data;
3221
3222                 l_cp->ppm_data_read += l_remaining_data; // Increase the number of data read
3223         }
3224
3225 #ifdef CLEAN_MSD
3226
3227                 if (l_cp->ppm_data_size == l_cp->ppm_len) {
3228                         if (p_header_size >= 4) {
3229                                 // read a N_ppm
3230                                 opj_read_bytes(p_header_data,&l_N_ppm,4);               /* N_ppm */
3231                                 p_header_data+=4;
3232                                 p_header_size-=4;
3233                                 l_cp->ppm_len += l_N_ppm ;
3234
3235                                 l_cp->ppm_buffer = (OPJ_BYTE *) opj_realloc(l_cp->ppm_buffer, l_cp->ppm_len);
3236                                 if (l_cp->ppm_buffer == 00) {
3237                                         opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory reading ppm marker\n");
3238                                         return OPJ_FALSE;
3239                                 }
3240                                 memset(l_cp->ppm_buffer+l_cp->ppm_data_size,0,l_N_ppm);
3241
3242                                 l_cp->ppm_data = l_cp->ppm_buffer;
3243                         }
3244                         else {
3245                                 return OPJ_FALSE;
3246                         }
3247                 }
3248
3249                 l_remaining_data = l_cp->ppm_len - l_cp->ppm_data_size;
3250
3251                 if (l_remaining_data <= p_header_size) {
3252                         /* we must store less information than available in the packet */
3253                         memcpy(l_cp->ppm_buffer + l_cp->ppm_data_size , p_header_data , l_remaining_data);
3254                         l_cp->ppm_data_size = l_cp->ppm_len;
3255                         p_header_size -= l_remaining_data;
3256                         p_header_data += l_remaining_data;
3257                 }
3258                 else {
3259                         memcpy(l_cp->ppm_buffer + l_cp->ppm_data_size , p_header_data , p_header_size);
3260                         l_cp->ppm_data_size += p_header_size;
3261                         p_header_data += p_header_size;
3262                         p_header_size = 0;
3263                         break;
3264                 }
3265         }
3266 #endif
3267         return OPJ_TRUE;
3268 }
3269
3270 static void j2k_read_ppt(opj_j2k_t *j2k) {
3271         int len, Z_ppt, i, j = 0;
3272
3273         opj_cp_t *cp = j2k->cp;
3274         opj_tcp_t *tcp = cp->tcps + j2k->curtileno;
3275         opj_cio_t *cio = j2k->cio;
3276
3277         len = cio_read(cio, 2);
3278         Z_ppt = cio_read(cio, 1);
3279         tcp->ppt = 1;
3280         if (Z_ppt == 0) {               /* First PPT marker */
3281                 tcp->ppt_data = (unsigned char *) opj_malloc((len - 3) * sizeof(unsigned char));
3282                 tcp->ppt_data_first = tcp->ppt_data;
3283                 tcp->ppt_store = 0;
3284                 tcp->ppt_len = len - 3;
3285         } else {                        /* NON-first PPT marker */
3286                 tcp->ppt_data = (unsigned char *) opj_realloc(tcp->ppt_data, (len - 3 + tcp->ppt_store) * sizeof(unsigned char));
3287                 tcp->ppt_data_first = tcp->ppt_data;
3288                 tcp->ppt_len = len - 3 + tcp->ppt_store;
3289         }
3290         j = tcp->ppt_store;
3291         for (i = len - 3; i > 0; i--) {
3292                 tcp->ppt_data[j] = cio_read(cio, 1);
3293                 j++;
3294         }
3295         tcp->ppt_store = j;
3296 }
3297
3298 /**
3299  * Reads a PPT marker (Packed packet headers, tile-part header)
3300  *
3301  * @param       p_header_data   the data contained in the PPT box.
3302  * @param       p_j2k                   the jpeg2000 codec.
3303  * @param       p_header_size   the size of the data contained in the PPT marker.
3304  * @param       p_manager               the user event manager.
3305 */
3306 opj_bool j2k_read_ppt_v2 (      opj_j2k_v2_t *p_j2k,
3307                                                         OPJ_BYTE * p_header_data,
3308                                                         OPJ_UINT32 p_header_size,
3309                                                         struct opj_event_mgr * p_manager )
3310 {
3311         opj_cp_v2_t *l_cp = 00;
3312         opj_tcp_v2_t *l_tcp = 00;
3313         OPJ_UINT32 l_Z_ppt;
3314
3315         /* preconditions */
3316         assert(p_header_data != 00);
3317         assert(p_j2k != 00);
3318         assert(p_manager != 00);
3319
3320         /* We need to have the Z_ppt element at minimum */
3321         if (p_header_size < 1) {
3322                 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PPT marker\n");
3323                 return OPJ_FALSE;
3324         }
3325
3326         l_cp = &(p_j2k->m_cp);
3327         if (l_cp->ppm){
3328                 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PPT marker: packet header have been previously found in the main header (PPM marker).\n");
3329                 return OPJ_FALSE;
3330         }
3331
3332         l_tcp = &(l_cp->tcps[p_j2k->m_current_tile_number]);
3333         l_tcp->ppt = 1;
3334
3335         opj_read_bytes(p_header_data,&l_Z_ppt,1);               /* Z_ppt */
3336         ++p_header_data;
3337         --p_header_size;
3338
3339         /* Allocate buffer to read the packet header */
3340         if (l_Z_ppt == 0) {
3341                 /* First PPT marker */
3342                 l_tcp->ppt_data_size = 0;
3343                 l_tcp->ppt_len = p_header_size;
3344
3345                 l_tcp->ppt_buffer = (OPJ_BYTE *) opj_calloc(l_tcp->ppt_len, sizeof(OPJ_BYTE) );
3346                 if (l_tcp->ppt_buffer == 00) {
3347                         opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory reading PPT marker\n");
3348                         return OPJ_FALSE;
3349                 }
3350                 l_tcp->ppt_data = l_tcp->ppt_buffer;
3351
3352                 /* memset(l_tcp->ppt_buffer,0,l_tcp->ppt_len); */
3353         }
3354         else {
3355                 l_tcp->ppt_len += p_header_size;
3356
3357                 l_tcp->ppt_buffer = (OPJ_BYTE *) opj_realloc(l_tcp->ppt_buffer,l_tcp->ppt_len);
3358                 if (l_tcp->ppt_buffer == 00) {
3359                         opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory reading PPT marker\n");
3360                         return OPJ_FALSE;
3361                 }
3362                 l_tcp->ppt_data = l_tcp->ppt_buffer;
3363
3364                 memset(l_tcp->ppt_buffer+l_tcp->ppt_data_size,0,p_header_size);
3365         }
3366
3367         /* Read packet header from buffer */
3368         memcpy(l_tcp->ppt_buffer+l_tcp->ppt_data_size,p_header_data,p_header_size);
3369
3370         l_tcp->ppt_data_size += p_header_size;
3371
3372         return OPJ_TRUE;
3373 }
3374
3375 static void j2k_write_tlm(opj_j2k_t *j2k){
3376         int lenp;
3377         opj_cio_t *cio = j2k->cio;
3378         j2k->tlm_start = cio_tell(cio);
3379         cio_write(cio, J2K_MS_TLM, 2);/* TLM */
3380         lenp = 4 + (5*j2k->totnum_tp);
3381         cio_write(cio,lenp,2);                          /* Ltlm */
3382         cio_write(cio, 0,1);                                    /* Ztlm=0*/
3383         cio_write(cio,80,1);                                    /* Stlm ST=1(8bits-255 tiles max),SP=1(Ptlm=32bits) */
3384         cio_skip(cio,5*j2k->totnum_tp);
3385 }
3386
3387 static void j2k_write_sot(opj_j2k_t *j2k) {
3388         int lenp, len;
3389
3390         opj_cio_t *cio = j2k->cio;
3391
3392         j2k->sot_start = cio_tell(cio);
3393         cio_write(cio, J2K_MS_SOT, 2);          /* SOT */
3394         lenp = cio_tell(cio);
3395         cio_skip(cio, 2);                                       /* Lsot (further) */
3396         cio_write(cio, j2k->curtileno, 2);      /* Isot */
3397         cio_skip(cio, 4);                                       /* Psot (further in j2k_write_sod) */
3398         cio_write(cio, j2k->cur_tp_num , 1);    /* TPsot */
3399         cio_write(cio, j2k->cur_totnum_tp[j2k->curtileno], 1);          /* TNsot */
3400         len = cio_tell(cio) - lenp;
3401         cio_seek(cio, lenp);
3402         cio_write(cio, len, 2);                         /* Lsot */
3403         cio_seek(cio, lenp + len);
3404
3405         /* UniPG>> */
3406 #ifdef USE_JPWL
3407         /* update markers struct */
3408         j2k_add_marker(j2k->cstr_info, J2K_MS_SOT, j2k->sot_start, len + 2);
3409 #endif /* USE_JPWL */
3410         /* <<UniPG */
3411
3412         if( j2k->cstr_info && j2k->cur_tp_num==0){
3413           j2k_add_tlmarker( j2k->curtileno, j2k->cstr_info, J2K_MS_SOT, lenp, len);
3414         }
3415 }
3416
3417 static void j2k_read_sot(opj_j2k_t *j2k) {
3418         int len, tileno, totlen, partno, numparts, i;
3419         opj_tcp_t *tcp = NULL;
3420         char status = 0;
3421
3422         opj_cp_t *cp = j2k->cp;
3423         opj_cio_t *cio = j2k->cio;
3424
3425         len = cio_read(cio, 2);
3426         tileno = cio_read(cio, 2);
3427
3428 #ifdef USE_JPWL
3429         if (j2k->cp->correct) {
3430
3431                 static int backup_tileno = 0;
3432
3433                 /* tileno is negative or larger than the number of tiles!!! */
3434                 if ((tileno < 0) || (tileno > (cp->tw * cp->th))) {
3435                         opj_event_msg(j2k->cinfo, EVT_ERROR,
3436                                 "JPWL: bad tile number (%d out of a maximum of %d)\n",
3437                                 tileno, (cp->tw * cp->th));
3438                         if (!JPWL_ASSUME) {
3439                                 opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
3440                                 return;
3441                         }
3442                         /* we try to correct */
3443                         tileno = backup_tileno;
3444                         opj_event_msg(j2k->cinfo, EVT_WARNING, "- trying to adjust this\n"
3445                                 "- setting tile number to %d\n",
3446                                 tileno);
3447                 }
3448
3449                 /* keep your private count of tiles */
3450                 backup_tileno++;
3451         };
3452 #endif /* USE_JPWL */
3453         
3454         if (cp->tileno_size == 0) {
3455                 cp->tileno[cp->tileno_size] = tileno;
3456                 cp->tileno_size++;
3457         } else {
3458                 i = 0;
3459                 while (i < cp->tileno_size && status == 0) {
3460                         status = cp->tileno[i] == tileno ? 1 : 0;
3461                         i++;
3462                 }
3463                 if (status == 0) {
3464                         cp->tileno[cp->tileno_size] = tileno;
3465                         cp->tileno_size++;
3466                 }
3467         }
3468         
3469         totlen = cio_read(cio, 4);
3470
3471 #ifdef USE_JPWL
3472         if (j2k->cp->correct) {
3473
3474                 /* totlen is negative or larger than the bytes left!!! */
3475                 if ((totlen < 0) || (totlen > (cio_numbytesleft(cio) + 8))) {
3476                         opj_event_msg(j2k->cinfo, EVT_ERROR,
3477                                 "JPWL: bad tile byte size (%d bytes against %d bytes left)\n",
3478                                 totlen, cio_numbytesleft(cio) + 8);
3479                         if (!JPWL_ASSUME) {
3480                                 opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
3481                                 return;
3482                         }
3483                         /* we try to correct */
3484                         totlen = 0;
3485                         opj_event_msg(j2k->cinfo, EVT_WARNING, "- trying to adjust this\n"
3486                                 "- setting Psot to %d => assuming it is the last tile\n",
3487                                 totlen);
3488                 }
3489
3490         };
3491 #endif /* USE_JPWL */
3492
3493         if (!totlen)
3494                 totlen = cio_numbytesleft(cio) + 8;
3495         
3496         partno = cio_read(cio, 1);
3497         numparts = cio_read(cio, 1);
3498   
3499   if (partno >= numparts) {
3500     opj_event_msg(j2k->cinfo, EVT_WARNING, "SOT marker inconsistency in tile %d: tile-part index greater (%d) than number of tile-parts (%d)\n", tileno, partno, numparts);
3501     numparts = partno+1;
3502   }
3503         
3504         j2k->curtileno = tileno;
3505         j2k->cur_tp_num = partno;
3506         j2k->eot = cio_getbp(cio) - 12 + totlen;
3507         j2k->state = J2K_STATE_TPH;
3508         tcp = &cp->tcps[j2k->curtileno];
3509
3510         /* Index */
3511         if (j2k->cstr_info) {
3512                 if (tcp->first) {
3513                         if (tileno == 0) 
3514                                 j2k->cstr_info->main_head_end = cio_tell(cio) - 13;
3515                         j2k->cstr_info->tile[tileno].tileno = tileno;
3516                         j2k->cstr_info->tile[tileno].start_pos = cio_tell(cio) - 12;
3517                         j2k->cstr_info->tile[tileno].end_pos = j2k->cstr_info->tile[tileno].start_pos + totlen - 1;                             
3518     } else {
3519                         j2k->cstr_info->tile[tileno].end_pos += totlen;
3520                 }
3521     j2k->cstr_info->tile[tileno].num_tps = numparts;
3522     if (numparts)
3523       j2k->cstr_info->tile[tileno].tp = (opj_tp_info_t *) opj_realloc(j2k->cstr_info->tile[tileno].tp, numparts * sizeof(opj_tp_info_t));
3524     else
3525       j2k->cstr_info->tile[tileno].tp = (opj_tp_info_t *) opj_realloc(j2k->cstr_info->tile[tileno].tp, 10 * sizeof(opj_tp_info_t)); // Fixme (10)
3526                 j2k->cstr_info->tile[tileno].tp[partno].tp_start_pos = cio_tell(cio) - 12;
3527                 j2k->cstr_info->tile[tileno].tp[partno].tp_end_pos = 
3528                         j2k->cstr_info->tile[tileno].tp[partno].tp_start_pos + totlen - 1;
3529         }
3530         
3531         if (tcp->first == 1) {          
3532                 /* Initialization PPT */
3533                 opj_tccp_t *tmp = tcp->tccps;
3534                 memcpy(tcp, j2k->default_tcp, sizeof(opj_tcp_t));
3535                 tcp->ppt = 0;
3536                 tcp->ppt_data = NULL;
3537                 tcp->ppt_data_first = NULL;
3538                 tcp->tccps = tmp;
3539
3540                 for (i = 0; i < j2k->image->numcomps; i++) {
3541                         tcp->tccps[i] = j2k->default_tcp->tccps[i];
3542                 }
3543                 cp->tcps[j2k->curtileno].first = 0;
3544         }
3545 }
3546
3547 /**
3548  * Reads a PPT marker (Packed packet headers, tile-part header)
3549  *
3550  * @param       p_header_data   the data contained in the PPT box.
3551  * @param       p_j2k                   the jpeg2000 codec.
3552  * @param       p_header_size   the size of the data contained in the PPT marker.
3553  * @param       p_manager               the user event manager.
3554 */
3555 opj_bool j2k_read_sot_v2 (
3556                                                 opj_j2k_v2_t *p_j2k,
3557                                                 OPJ_BYTE * p_header_data,
3558                                                 OPJ_UINT32 p_header_size,
3559                                                 struct opj_event_mgr * p_manager
3560                                         )
3561 {
3562
3563         opj_cp_v2_t *l_cp = 00;
3564         opj_tcp_v2_t *l_tcp = 00;
3565         OPJ_UINT32 l_tot_len, l_num_parts = 0;
3566         OPJ_UINT32 l_current_part;
3567         OPJ_UINT32 l_tile_x,l_tile_y;
3568
3569         /* preconditions */
3570         assert(p_header_data != 00);
3571         assert(p_j2k != 00);
3572         assert(p_manager != 00);
3573
3574         /* Size of this marker is fixed = 12 (we have already read marker and its size)*/
3575         if (p_header_size != 8) {
3576                 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading SOT marker\n");
3577                 return OPJ_FALSE;
3578         }
3579
3580         l_cp = &(p_j2k->m_cp);
3581         opj_read_bytes(p_header_data,&(p_j2k->m_current_tile_number),2);                /* Isot */
3582         p_header_data+=2;
3583
3584         l_tcp = &l_cp->tcps[p_j2k->m_current_tile_number];
3585         l_tile_x = p_j2k->m_current_tile_number % l_cp->tw;
3586         l_tile_y = p_j2k->m_current_tile_number / l_cp->tw;
3587
3588 #ifdef USE_JPWL
3589         if (l_cp->correct) {
3590
3591                 int tileno = p_j2k->m_current_tile_number;
3592                 static int backup_tileno = 0;
3593
3594                 /* tileno is negative or larger than the number of tiles!!! */
3595                 if ((tileno < 0) || (tileno > (l_cp->tw * l_cp->th))) {
3596                         opj_event_msg_v2(p_manager, EVT_ERROR,
3597                                 "JPWL: bad tile number (%d out of a maximum of %d)\n",
3598                                 tileno, (l_cp->tw * l_cp->th));
3599                         if (!JPWL_ASSUME) {
3600                                 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
3601                                 return OPJ_FALSE;
3602                         }
3603                         /* we try to correct */
3604                         tileno = backup_tileno;
3605                         opj_event_msg_v2(p_manager, EVT_WARNING, "- trying to adjust this\n"
3606                                 "- setting tile number to %d\n",
3607                                 tileno);
3608                 }
3609
3610                 /* keep your private count of tiles */
3611                 backup_tileno++;
3612         };
3613 #endif /* USE_JPWL */
3614
3615         /* look for the tile in the list of already processed tile (in parts). */
3616         /* Optimization possible here with a more complex data structure and with the removing of tiles */
3617         /* since the time taken by this function can only grow at the time */
3618
3619         opj_read_bytes(p_header_data,&l_tot_len,4);             /* Psot */
3620         p_header_data+=4;
3621
3622 #ifdef USE_JPWL
3623         if (l_cp->correct) {
3624
3625                 /* totlen is negative or larger than the bytes left!!! */
3626                 if ((l_tot_len < 0) || (l_tot_len > p_header_size ) ) { /* FIXME it seems correct; for info in V1 -> (p_stream_numbytesleft(p_stream) + 8))) { */
3627                         opj_event_msg_v2(p_manager, EVT_ERROR,
3628                                 "JPWL: bad tile byte size (%d bytes against %d bytes left)\n",
3629                                 l_tot_len, p_header_size ); /* FIXME it seems correct; for info in V1 -> p_stream_numbytesleft(p_stream) + 8); */
3630                         if (!JPWL_ASSUME) {
3631                                 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
3632                                 return OPJ_FALSE;
3633                         }
3634                         /* we try to correct */
3635                         l_tot_len = 0;
3636                         opj_event_msg_v2(p_manager, EVT_WARNING, "- trying to adjust this\n"
3637                                 "- setting Psot to %d => assuming it is the last tile\n",
3638                                 l_tot_len);
3639                 }
3640         };
3641 #endif /* USE_JPWL */
3642
3643         /* Ref A.4.2: Psot could be equal zero if it is the last tile-part of the codestream.*/
3644         if (!l_tot_len) {
3645                 opj_event_msg_v2(p_manager, EVT_INFO, "Psot value of the current tile-part is equal to zero, "
3646                                 "we assuming it is the last tile-part of the codestream.\n");
3647                 p_j2k->m_specific_param.m_decoder.m_last_tile_part = 1;
3648         }
3649
3650         opj_read_bytes(p_header_data,&l_current_part ,1);       /* TPsot */
3651         ++p_header_data;
3652
3653         opj_read_bytes(p_header_data,&l_num_parts ,1);          /* TNsot */
3654         ++p_header_data;
3655
3656         if (l_num_parts != 0) { /* Number of tile-part header is provided by this tile-part header */
3657                 /* Useful to manage the case of textGBR.jp2 file because two values of TNSot are allowed: the correct numbers of
3658                  * tile-parts for that tile and zero (A.4.2 of 15444-1 : 2002). */
3659                 if (l_tcp->m_nb_tile_parts) {
3660                         if (l_current_part >= l_tcp->m_nb_tile_parts){
3661                                 opj_event_msg_v2(p_manager, EVT_ERROR, "In SOT marker, TPSot (%d) is not valid regards to the current "
3662                                                 "number of tile-part (%d), giving up\n", l_current_part, l_tcp->m_nb_tile_parts );
3663                                 p_j2k->m_specific_param.m_decoder.m_last_tile_part = 1;
3664                                 return OPJ_FALSE;
3665                         }
3666                 }
3667                 l_tcp->m_nb_tile_parts = l_num_parts;
3668         }
3669
3670         /* If know the number of tile part header we will check if we didn't read the last*/
3671         if (l_tcp->m_nb_tile_parts) {
3672                 if (l_tcp->m_nb_tile_parts == (l_current_part + 1)) {
3673                         p_j2k->m_specific_param.m_decoder.m_can_decode = 1; /* Process the last tile-part header*/
3674                 }
3675         }
3676
3677         if (!p_j2k->m_specific_param.m_decoder.m_last_tile_part){
3678                 /* Keep the size of data to skip after this marker */
3679                 p_j2k->m_specific_param.m_decoder.m_sot_length = l_tot_len - 12; /* SOT_marker_size = 12 */
3680         }
3681         else {
3682                 /* FIXME: need to be computed from the number of bytes remaining in the codestream */
3683                 p_j2k->m_specific_param.m_decoder.m_sot_length = 0;
3684         }
3685
3686         p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_TPH;
3687
3688         /* Check if the current tile is outside the area we want decode (in tile index)*/
3689         p_j2k->m_specific_param.m_decoder.m_skip_data =
3690                         (l_tile_x < p_j2k->m_specific_param.m_decoder.m_start_tile_x)
3691                 ||      (l_tile_x >= p_j2k->m_specific_param.m_decoder.m_end_tile_x)
3692                 ||  (l_tile_y < p_j2k->m_specific_param.m_decoder.m_start_tile_y)
3693                 ||      (l_tile_y >= p_j2k->m_specific_param.m_decoder.m_end_tile_y);
3694
3695         /* Index */
3696         if (p_j2k->cstr_index)
3697         {
3698                 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tileno = p_j2k->m_current_tile_number;
3699                 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_tpsno = l_current_part;
3700
3701                 if (l_num_parts != 0){
3702                         p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].nb_tps = l_num_parts;
3703                         p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps = l_num_parts;
3704
3705
3706                         if (!p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index)
3707                                 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index =
3708                                         (opj_tp_index_t*)opj_calloc(l_num_parts, sizeof(opj_tp_index_t));
3709                         else
3710                                 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index =
3711                                         (opj_tp_index_t*)opj_realloc(
3712                                                         p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index,
3713                                                         l_num_parts* sizeof(opj_tp_index_t));
3714                 }
3715                 else{
3716                         /*if (!p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index)*/ {
3717
3718                                 if (!p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index) {
3719                                         p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps = 10;
3720                                         p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index =
3721                                                 (opj_tp_index_t*)opj_calloc( p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps,
3722                                                                                                          sizeof(opj_tp_index_t));
3723                                 }
3724
3725                                 if ( l_current_part >= p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps ){
3726                                         p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps += 10;
3727                                         p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index =
3728                                                 (opj_tp_index_t*)opj_realloc( p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index,
3729                                                                                                           p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps
3730                                                                                                           * sizeof(opj_tp_index_t));
3731                                 }
3732                         }
3733
3734                 }
3735
3736         }
3737
3738
3739         /* FIXME move this onto a separate method to call before reading any SOT, remove part about main_end header, use a index struct inside p_j2k */
3740         /* if (p_j2k->cstr_info) {
3741                 if (l_tcp->first) {
3742                         if (tileno == 0) {
3743                                 p_j2k->cstr_info->main_head_end = p_stream_tell(p_stream) - 13;
3744                         }
3745
3746                         p_j2k->cstr_info->tile[tileno].tileno = tileno;
3747                         p_j2k->cstr_info->tile[tileno].start_pos = p_stream_tell(p_stream) - 12;
3748                         p_j2k->cstr_info->tile[tileno].end_pos = p_j2k->cstr_info->tile[tileno].start_pos + totlen - 1;
3749                         p_j2k->cstr_info->tile[tileno].num_tps = numparts;
3750
3751                         if (numparts) {
3752                                 p_j2k->cstr_info->tile[tileno].tp = (opj_tp_info_t *) opj_malloc(numparts * sizeof(opj_tp_info_t));
3753                         }
3754                         else {
3755                                 p_j2k->cstr_info->tile[tileno].tp = (opj_tp_info_t *) opj_malloc(10 * sizeof(opj_tp_info_t)); // Fixme (10)
3756                         }
3757                 }
3758                 else {
3759                         p_j2k->cstr_info->tile[tileno].end_pos += totlen;
3760                 }
3761
3762                 p_j2k->cstr_info->tile[tileno].tp[partno].tp_start_pos = p_stream_tell(p_stream) - 12;
3763                 p_j2k->cstr_info->tile[tileno].tp[partno].tp_end_pos =
3764                 p_j2k->cstr_info->tile[tileno].tp[partno].tp_start_pos + totlen - 1;
3765         }*/
3766         return OPJ_TRUE;
3767 }
3768
3769 static void j2k_write_sod(opj_j2k_t *j2k, void *tile_coder) {
3770         int l, layno;
3771         int totlen;
3772         opj_tcp_t *tcp = NULL;
3773         opj_codestream_info_t *cstr_info = NULL;
3774         
3775         opj_tcd_t *tcd = (opj_tcd_t*)tile_coder;        /* cast is needed because of conflicts in header inclusions */
3776         opj_cp_t *cp = j2k->cp;
3777         opj_cio_t *cio = j2k->cio;
3778
3779         tcd->tp_num = j2k->tp_num ;
3780         tcd->cur_tp_num = j2k->cur_tp_num;
3781         
3782         cio_write(cio, J2K_MS_SOD, 2);
3783
3784         if( j2k->cstr_info && j2k->cur_tp_num==0){
3785           j2k_add_tlmarker( j2k->curtileno, j2k->cstr_info, J2K_MS_SOD, cio_tell(cio), 0);
3786         }
3787
3788         if (j2k->curtileno == 0) {
3789                 j2k->sod_start = cio_tell(cio) + j2k->pos_correction;
3790         }
3791
3792         /* INDEX >> */
3793         cstr_info = j2k->cstr_info;
3794         if (cstr_info) {
3795                 if (!j2k->cur_tp_num ) {
3796                         cstr_info->tile[j2k->curtileno].end_header = cio_tell(cio) + j2k->pos_correction - 1;
3797                         j2k->cstr_info->tile[j2k->curtileno].tileno = j2k->curtileno;
3798                 }
3799                 else{
3800                         if(cstr_info->tile[j2k->curtileno].packet[cstr_info->packno - 1].end_pos < cio_tell(cio))
3801                                 cstr_info->tile[j2k->curtileno].packet[cstr_info->packno].start_pos = cio_tell(cio);
3802                 }
3803                 /* UniPG>> */
3804 #ifdef USE_JPWL
3805                 /* update markers struct */
3806                 j2k_add_marker(j2k->cstr_info, J2K_MS_SOD, j2k->sod_start, 2);
3807 #endif /* USE_JPWL */
3808                 /* <<UniPG */
3809         }
3810         /* << INDEX */
3811         
3812         tcp = &cp->tcps[j2k->curtileno];
3813         for (layno = 0; layno < tcp->numlayers; layno++) {
3814                 if (tcp->rates[layno]>(j2k->sod_start / (cp->th * cp->tw))) {
3815                         tcp->rates[layno]-=(j2k->sod_start / (cp->th * cp->tw));
3816                 } else if (tcp->rates[layno]) {
3817                         tcp->rates[layno]=1;
3818                 }
3819         }
3820         if(j2k->cur_tp_num == 0){
3821                 tcd->tcd_image->tiles->packno = 0;
3822                 if(cstr_info)
3823                         cstr_info->packno = 0;
3824         }
3825         
3826         l = tcd_encode_tile(tcd, j2k->curtileno, cio_getbp(cio), cio_numbytesleft(cio) - 2, cstr_info);
3827         
3828         /* Writing Psot in SOT marker */
3829         totlen = cio_tell(cio) + l - j2k->sot_start;
3830         cio_seek(cio, j2k->sot_start + 6);
3831         cio_write(cio, totlen, 4);
3832         cio_seek(cio, j2k->sot_start + totlen);
3833         /* Writing Ttlm and Ptlm in TLM marker */
3834         if(cp->cinema){
3835                 cio_seek(cio, j2k->tlm_start + 6 + (5*j2k->cur_tp_num));
3836                 cio_write(cio, j2k->curtileno, 1);
3837                 cio_write(cio, totlen, 4);
3838         }
3839         cio_seek(cio, j2k->sot_start + totlen);
3840 }
3841
3842 static void j2k_read_sod(opj_j2k_t *j2k) {
3843         int len, truncate = 0, i;
3844         unsigned char *data = NULL, *data_ptr = NULL;
3845
3846         opj_cio_t *cio = j2k->cio;
3847         int curtileno = j2k->curtileno;
3848
3849         /* Index */
3850         if (j2k->cstr_info) {
3851                 j2k->cstr_info->tile[j2k->curtileno].tp[j2k->cur_tp_num].tp_end_header =
3852                         cio_tell(cio) + j2k->pos_correction - 1;
3853                 if (j2k->cur_tp_num == 0)
3854                         j2k->cstr_info->tile[j2k->curtileno].end_header = cio_tell(cio) + j2k->pos_correction - 1;
3855                 j2k->cstr_info->packno = 0;
3856         }
3857         
3858         len = int_min(j2k->eot - cio_getbp(cio), cio_numbytesleft(cio) + 1);
3859
3860         if (len == cio_numbytesleft(cio) + 1) {
3861                 truncate = 1;           /* Case of a truncate codestream */
3862         }       
3863
3864         data = j2k->tile_data[curtileno];
3865         data = (unsigned char*) opj_realloc(data, (j2k->tile_len[curtileno] + len) * sizeof(unsigned char));
3866
3867         data_ptr = data + j2k->tile_len[curtileno];
3868         for (i = 0; i < len; i++) {
3869                 data_ptr[i] = cio_read(cio, 1);
3870         }
3871
3872         j2k->tile_len[curtileno] += len;
3873         j2k->tile_data[curtileno] = data;
3874         
3875         if (!truncate) {
3876                 j2k->state = J2K_STATE_TPHSOT;
3877         } else {
3878                 j2k->state = J2K_STATE_NEOC;    /* RAJOUTE !! */
3879         }
3880         j2k->cur_tp_num++;
3881 }
3882
3883 /**
3884  * Reads a SOD marker (Start Of Data)
3885  *
3886  * @param       p_header_data   the data contained in the SOD box.
3887  * @param       p_j2k                   the jpeg2000 codec.
3888  * @param       p_header_size   the size of the data contained in the SOD marker.
3889  * @param       p_manager               the user event manager.
3890 */
3891 opj_bool j2k_read_sod_v2 (
3892                                                 opj_j2k_v2_t *p_j2k,
3893                                                 struct opj_stream_private *p_stream,
3894                                                 struct opj_event_mgr * p_manager
3895                                         )
3896 {
3897         OPJ_UINT32 l_current_read_size;
3898         opj_codestream_index_t * l_cstr_index = 00;
3899         OPJ_BYTE ** l_current_data = 00;
3900         opj_tcp_v2_t * l_tcp = 00;
3901         OPJ_UINT32 * l_tile_len = 00;
3902
3903         /* preconditions */
3904         assert(p_j2k != 00);
3905         assert(p_manager != 00);
3906         assert(p_stream != 00);
3907
3908         l_tcp = &(p_j2k->m_cp.tcps[p_j2k->m_current_tile_number]);
3909
3910         if (p_j2k->m_specific_param.m_decoder.m_last_tile_part)
3911                 p_j2k->m_specific_param.m_decoder.m_sot_length = opj_stream_get_number_byte_left(p_stream) - 2;
3912         else
3913                 p_j2k->m_specific_param.m_decoder.m_sot_length -= 2;
3914
3915         l_current_data = &(l_tcp->m_data);
3916         l_tile_len = &l_tcp->m_data_size;
3917
3918         if (! *l_current_data) {
3919                 *l_current_data = (OPJ_BYTE*) opj_malloc/*FIXME V2 -> my_opj_malloc*/(p_j2k->m_specific_param.m_decoder.m_sot_length);
3920         }
3921         else {
3922                 *l_current_data = (OPJ_BYTE*) opj_realloc/*FIXME V2 -> my_opj_realloc*/(*l_current_data, *l_tile_len + p_j2k->m_specific_param.m_decoder.m_sot_length);
3923         }
3924
3925         if (*l_current_data == 00) {
3926                 opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot decode tile\n");
3927                 return OPJ_FALSE;
3928         }
3929
3930
3931         /* Index */
3932         l_cstr_index = p_j2k->cstr_index;
3933         if (l_cstr_index) {
3934                 OPJ_SIZE_T l_current_pos = opj_stream_tell(p_stream) - 2;
3935                 OPJ_UINT32 l_current_tile_part =l_cstr_index->tile_index[p_j2k->m_current_tile_number].current_tpsno;
3936                 l_cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index[l_current_tile_part].end_header =
3937                                 l_current_pos;
3938                 l_cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index[l_current_tile_part].end_pos =
3939                                 l_current_pos + p_j2k->m_specific_param.m_decoder.m_sot_length + 2;
3940
3941                 j2k_add_tlmarker_v2(p_j2k->m_current_tile_number,
3942                                                         l_cstr_index,
3943                                                         J2K_MS_SOD,
3944                                                         l_current_pos,
3945                                                         p_j2k->m_specific_param.m_decoder.m_sot_length + 2);
3946
3947                 /*l_cstr_index->packno = 0;*/
3948         }
3949
3950
3951
3952
3953
3954
3955
3956         l_current_read_size = opj_stream_read_data(     p_stream,
3957                                                                                                 *l_current_data + *l_tile_len,
3958                                                                                                 p_j2k->m_specific_param.m_decoder.m_sot_length,
3959                                                                                                 p_manager);
3960
3961         if (l_current_read_size != p_j2k->m_specific_param.m_decoder.m_sot_length) {
3962                 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_NEOC;
3963         }
3964         else {
3965                 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_TPHSOT;
3966         }
3967
3968         *l_tile_len +=  l_current_read_size;
3969
3970         return OPJ_TRUE;
3971 }
3972
3973
3974 static void j2k_write_rgn(opj_j2k_t *j2k, int compno, int tileno) {
3975         opj_cp_t *cp = j2k->cp;
3976         opj_tcp_t *tcp = &cp->tcps[tileno];
3977         opj_cio_t *cio = j2k->cio;
3978         int numcomps = j2k->image->numcomps;
3979         
3980         cio_write(cio, J2K_MS_RGN, 2);                                          /* RGN  */
3981         cio_write(cio, numcomps <= 256 ? 5 : 6, 2);                     /* Lrgn */
3982         cio_write(cio, compno, numcomps <= 256 ? 1 : 2);        /* Crgn */
3983         cio_write(cio, 0, 1);                                                           /* Srgn */
3984         cio_write(cio, tcp->tccps[compno].roishift, 1);         /* SPrgn */
3985 }
3986
3987 static void j2k_read_rgn(opj_j2k_t *j2k) {
3988         int len, compno, roisty;
3989
3990         opj_cp_t *cp = j2k->cp;
3991         opj_tcp_t *tcp = j2k->state == J2K_STATE_TPH ? &cp->tcps[j2k->curtileno] : j2k->default_tcp;
3992         opj_cio_t *cio = j2k->cio;
3993         int numcomps = j2k->image->numcomps;
3994
3995         len = cio_read(cio, 2);                                                                         /* Lrgn */
3996         compno = cio_read(cio, numcomps <= 256 ? 1 : 2);                        /* Crgn */
3997         roisty = cio_read(cio, 1);                                                                      /* Srgn */
3998
3999 #ifdef USE_JPWL
4000         if (j2k->cp->correct) {
4001                 /* totlen is negative or larger than the bytes left!!! */
4002                 if (compno >= numcomps) {
4003                         opj_event_msg(j2k->cinfo, EVT_ERROR,
4004                                 "JPWL: bad component number in RGN (%d when there are only %d)\n",
4005                                 compno, numcomps);
4006                         if (!JPWL_ASSUME || JPWL_ASSUME) {
4007                                 opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
4008                                 return;
4009                         }
4010                 }
4011         };
4012 #endif /* USE_JPWL */
4013
4014         tcp->tccps[compno].roishift = cio_read(cio, 1);                         /* SPrgn */
4015 }
4016
4017 static void j2k_write_eoc(opj_j2k_t *j2k) {
4018         opj_cio_t *cio = j2k->cio;
4019         /* opj_event_msg(j2k->cinfo, "%.8x: EOC\n", cio_tell(cio) + j2k->pos_correction); */
4020         cio_write(cio, J2K_MS_EOC, 2);
4021
4022 /* UniPG>> */
4023 #ifdef USE_JPWL
4024         /* update markers struct */
4025         j2k_add_marker(j2k->cstr_info, J2K_MS_EOC, cio_tell(cio) - 2, 2);
4026 #endif /* USE_JPWL */
4027 /* <<UniPG */
4028 }
4029
4030 /**
4031  * Reads a RGN marker (Region Of Interest)
4032  *
4033  * @param       p_header_data   the data contained in the POC box.
4034  * @param       p_j2k                   the jpeg2000 codec.
4035  * @param       p_header_size   the size of the data contained in the POC marker.
4036  * @param       p_manager               the user event manager.
4037 */
4038 opj_bool j2k_read_rgn_v2 (
4039                                                 opj_j2k_v2_t *p_j2k,
4040                                                 OPJ_BYTE * p_header_data,
4041                                                 OPJ_UINT32 p_header_size,
4042                                                 struct opj_event_mgr * p_manager
4043                                         )
4044 {
4045         OPJ_UINT32 l_nb_comp;
4046         opj_image_t * l_image = 00;
4047
4048         opj_cp_v2_t *l_cp = 00;
4049         opj_tcp_v2_t *l_tcp = 00;
4050         OPJ_UINT32 l_comp_room, l_comp_no, l_roi_sty;
4051
4052         // preconditions
4053         assert(p_header_data != 00);
4054         assert(p_j2k != 00);
4055         assert(p_manager != 00);
4056
4057         l_image = p_j2k->m_private_image;
4058         l_nb_comp = l_image->numcomps;
4059
4060         if (l_nb_comp <= 256) {
4061                 l_comp_room = 1; }
4062         else {
4063                 l_comp_room = 2; }
4064
4065         if (p_header_size != 2 + l_comp_room) {
4066                 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading RGN marker\n");
4067                 return OPJ_FALSE;
4068         }
4069
4070         l_cp = &(p_j2k->m_cp);
4071         l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ?
4072                                 &l_cp->tcps[p_j2k->m_current_tile_number] :
4073                                 p_j2k->m_specific_param.m_decoder.m_default_tcp;
4074
4075         opj_read_bytes(p_header_data,&l_comp_no,l_comp_room);           /* Crgn */
4076         p_header_data+=l_comp_room;
4077         opj_read_bytes(p_header_data,&l_roi_sty,1);                                     /* Srgn */
4078         ++p_header_data;
4079
4080 #ifdef USE_JPWL
4081         if (l_cp->correct) {
4082                 /* totlen is negative or larger than the bytes left!!! */
4083                 if (l_comp_room >= l_nb_comp) {
4084                         opj_event_msg_v2(p_manager, EVT_ERROR,
4085                                 "JPWL: bad component number in RGN (%d when there are only %d)\n",
4086                                 l_comp_room, l_nb_comp);
4087                         if (!JPWL_ASSUME || JPWL_ASSUME) {
4088                                 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
4089                                 return OPJ_FALSE;
4090                         }
4091                 }
4092         };
4093 #endif /* USE_JPWL */
4094
4095         opj_read_bytes(p_header_data,(OPJ_UINT32 *) (&(l_tcp->tccps[l_comp_no].roishift)),1);   /* SPrgn */
4096         ++p_header_data;
4097
4098         return OPJ_TRUE;
4099
4100 }
4101
4102 static void j2k_read_eoc(opj_j2k_t *j2k) {
4103         int i, tileno;
4104         opj_bool success;
4105
4106         /* if packets should be decoded */
4107         if (j2k->cp->limit_decoding != DECODE_ALL_BUT_PACKETS) {
4108                 opj_tcd_t *tcd = tcd_create(j2k->cinfo);
4109                 tcd_malloc_decode(tcd, j2k->image, j2k->cp);
4110                 for (i = 0; i < j2k->cp->tileno_size; i++) {
4111                         tcd_malloc_decode_tile(tcd, j2k->image, j2k->cp, i, j2k->cstr_info);
4112                         tileno = j2k->cp->tileno[i];
4113                         success = tcd_decode_tile(tcd, j2k->tile_data[tileno], j2k->tile_len[tileno], tileno, j2k->cstr_info);
4114                         opj_free(j2k->tile_data[tileno]);
4115                         j2k->tile_data[tileno] = NULL;
4116                         tcd_free_decode_tile(tcd, i);
4117                         if (success == OPJ_FALSE) {
4118                                 j2k->state |= J2K_STATE_ERR;
4119                                 break;
4120                         }
4121                 }
4122                 tcd_free_decode(tcd);
4123                 tcd_destroy(tcd);
4124         }
4125         /* if packets should not be decoded  */
4126         else {
4127                 for (i = 0; i < j2k->cp->tileno_size; i++) {
4128                         tileno = j2k->cp->tileno[i];
4129                         opj_free(j2k->tile_data[tileno]);
4130                         j2k->tile_data[tileno] = NULL;
4131                 }
4132         }       
4133         if (j2k->state & J2K_STATE_ERR)
4134                 j2k->state = J2K_STATE_MT + J2K_STATE_ERR;
4135         else
4136                 j2k->state = J2K_STATE_MT; 
4137 }
4138
4139 /**
4140  * Reads a EOC marker (End Of Codestream)
4141  *
4142  * @param       p_header_data   the data contained in the SOD box.
4143  * @param       p_j2k                   the jpeg2000 codec.
4144  * @param       p_header_size   the size of the data contained in the SOD marker.
4145  * @param       p_manager               the user event manager.
4146 */
4147 opj_bool j2k_read_eoc_v2 (      opj_j2k_v2_t *p_j2k,
4148                                                         struct opj_stream_private *p_stream,
4149                                                         struct opj_event_mgr * p_manager )
4150 {
4151         OPJ_UINT32 i;
4152         opj_tcd_v2_t * l_tcd = 00;
4153         OPJ_UINT32 l_nb_tiles;
4154         opj_tcp_v2_t * l_tcp = 00;
4155         opj_bool l_success;
4156
4157         /* preconditions */
4158         assert(p_j2k != 00);
4159         assert(p_manager != 00);
4160         assert(p_stream != 00);
4161
4162         l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw;
4163         l_tcp = p_j2k->m_cp.tcps;
4164
4165         l_tcd = tcd_create_v2(OPJ_TRUE);
4166         if (l_tcd == 00) {
4167                 opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot decode tile, memory error\n");
4168                 return OPJ_FALSE;
4169         }
4170
4171         for (i = 0; i < l_nb_tiles; ++i) {
4172                 if (l_tcp->m_data) {
4173                         if (! tcd_init_decode_tile(l_tcd, i)) {
4174                                 tcd_destroy_v2(l_tcd);
4175                                 opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot decode tile, memory error\n");
4176                                 return OPJ_FALSE;
4177                         }
4178
4179                         l_success = tcd_decode_tile_v2(l_tcd, l_tcp->m_data, l_tcp->m_data_size, i, p_j2k->cstr_index);
4180                         /* cleanup */
4181
4182                         if (! l_success) {
4183                                 p_j2k->m_specific_param.m_decoder.m_state |= J2K_STATE_ERR;
4184                                 break;
4185                         }
4186                 }
4187
4188                 j2k_tcp_destroy(l_tcp);
4189                 ++l_tcp;
4190         }
4191
4192         tcd_destroy_v2(l_tcd);
4193         return OPJ_TRUE;
4194 }
4195
4196 typedef struct opj_dec_mstabent {
4197         /** marker value */
4198         int id;
4199         /** value of the state when the marker can appear */
4200         int states;
4201         /** action linked to the marker */
4202         void (*handler) (opj_j2k_t *j2k);
4203 } opj_dec_mstabent_t;
4204
4205 opj_dec_mstabent_t j2k_dec_mstab[] = {
4206   {J2K_MS_SOC, J2K_STATE_MHSOC, j2k_read_soc},
4207   {J2K_MS_SOT, J2K_STATE_MH | J2K_STATE_TPHSOT, j2k_read_sot},
4208   {J2K_MS_SOD, J2K_STATE_TPH, j2k_read_sod},
4209   {J2K_MS_EOC, J2K_STATE_TPHSOT, j2k_read_eoc},
4210   {J2K_MS_SIZ, J2K_STATE_MHSIZ, j2k_read_siz},
4211   {J2K_MS_COD, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_cod},
4212   {J2K_MS_COC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_coc},
4213   {J2K_MS_RGN, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_rgn},
4214   {J2K_MS_QCD, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_qcd},
4215   {J2K_MS_QCC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_qcc},
4216   {J2K_MS_POC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_poc},
4217   {J2K_MS_TLM, J2K_STATE_MH, j2k_read_tlm},
4218   {J2K_MS_PLM, J2K_STATE_MH, j2k_read_plm},
4219   {J2K_MS_PLT, J2K_STATE_TPH, j2k_read_plt},
4220   {J2K_MS_PPM, J2K_STATE_MH, j2k_read_ppm},
4221   {J2K_MS_PPT, J2K_STATE_TPH, j2k_read_ppt},
4222   {J2K_MS_SOP, 0, 0},
4223   {J2K_MS_CRG, J2K_STATE_MH, j2k_read_crg},
4224   {J2K_MS_COM, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_com},
4225
4226 #ifdef USE_JPWL
4227   {J2K_MS_EPC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_epc},
4228   {J2K_MS_EPB, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_epb},
4229   {J2K_MS_ESD, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_esd},
4230   {J2K_MS_RED, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_red},
4231 #endif /* USE_JPWL */
4232 #ifdef USE_JPSEC
4233   {J2K_MS_SEC, J2K_STATE_MH, j2k_read_sec},
4234   {J2K_MS_INSEC, 0, j2k_read_insec},
4235 #endif /* USE_JPSEC */
4236
4237   {0, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_unk}
4238 };
4239
4240 static void j2k_read_unk(opj_j2k_t *j2k) {
4241         opj_event_msg(j2k->cinfo, EVT_WARNING, "Unknown marker\n");
4242
4243 #ifdef USE_JPWL
4244         if (j2k->cp->correct) {
4245                 int m = 0, id, i;
4246                 int min_id = 0, min_dist = 17, cur_dist = 0, tmp_id;
4247                 cio_seek(j2k->cio, cio_tell(j2k->cio) - 2);
4248                 id = cio_read(j2k->cio, 2);
4249                 opj_event_msg(j2k->cinfo, EVT_ERROR,
4250                         "JPWL: really don't know this marker %x\n",
4251                         id);
4252                 if (!JPWL_ASSUME) {
4253                         opj_event_msg(j2k->cinfo, EVT_ERROR,
4254                                 "- possible synch loss due to uncorrectable codestream errors => giving up\n");
4255                         return;
4256                 }
4257                 /* OK, activate this at your own risk!!! */
4258                 /* we look for the marker at the minimum hamming distance from this */
4259                 while (j2k_dec_mstab[m].id) {
4260                         
4261                         /* 1's where they differ */
4262                         tmp_id = j2k_dec_mstab[m].id ^ id;
4263
4264                         /* compute the hamming distance between our id and the current */
4265                         cur_dist = 0;
4266                         for (i = 0; i < 16; i++) {
4267                                 if ((tmp_id >> i) & 0x0001) {
4268                                         cur_dist++;
4269                                 }
4270                         }
4271
4272                         /* if current distance is smaller, set the minimum */
4273                         if (cur_dist < min_dist) {
4274                                 min_dist = cur_dist;
4275                                 min_id = j2k_dec_mstab[m].id;
4276                         }
4277                         
4278                         /* jump to the next marker */
4279                         m++;
4280                 }
4281
4282                 /* do we substitute the marker? */
4283                 if (min_dist < JPWL_MAXIMUM_HAMMING) {
4284                         opj_event_msg(j2k->cinfo, EVT_ERROR,
4285                                 "- marker %x is at distance %d from the read %x\n",
4286                                 min_id, min_dist, id);
4287                         opj_event_msg(j2k->cinfo, EVT_ERROR,
4288                                 "- trying to substitute in place and crossing fingers!\n");
4289                         cio_seek(j2k->cio, cio_tell(j2k->cio) - 2);
4290                         cio_write(j2k->cio, min_id, 2);
4291
4292                         /* rewind */
4293                         cio_seek(j2k->cio, cio_tell(j2k->cio) - 2);
4294
4295                 }
4296
4297         };
4298 #endif /* USE_JPWL */
4299
4300 }
4301
4302 /**
4303  * Reads an unknown marker
4304  *
4305  * @param       p_stream                                the stream object to read from.
4306  * @param       p_j2k                   the jpeg2000 codec.
4307  * @param       p_manager               the user event manager.
4308  *
4309  * @return      true                    if the marker could be deduced.
4310 */
4311 opj_bool j2k_read_unk_v2 (      opj_j2k_v2_t *p_j2k,
4312                                                         struct opj_stream_private *p_stream,
4313                                                         OPJ_UINT32 *output_marker,
4314                                                         struct opj_event_mgr * p_manager
4315                                                         )
4316 {
4317         OPJ_UINT32 l_unknown_marker;
4318         const opj_dec_memory_marker_handler_t * l_marker_handler;
4319         OPJ_UINT32 l_size_unk = 2;
4320
4321         // preconditions
4322         assert(p_j2k != 00);
4323         assert(p_manager != 00);
4324         assert(p_stream != 00);
4325
4326         opj_event_msg_v2(p_manager, EVT_WARNING, "Unknown marker\n");
4327
4328         while(1) {
4329                 // Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer
4330                 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
4331                         opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
4332                         return OPJ_FALSE;
4333                 }
4334
4335                 // read 2 bytes as the new marker ID
4336                 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_unknown_marker,2);
4337
4338                 if (!(l_unknown_marker < 0xff00)) {
4339
4340                         // Get the marker handler from the marker ID
4341                         l_marker_handler = j2k_get_marker_handler(l_unknown_marker);
4342
4343                         if (!(p_j2k->m_specific_param.m_decoder.m_state & l_marker_handler->states)) {
4344                                 opj_event_msg_v2(p_manager, EVT_ERROR, "Marker is not compliant with its position\n");
4345                                 return OPJ_FALSE;
4346                         }
4347                         else {
4348                                 if (l_marker_handler->id != J2K_MS_UNK) {
4349                                         /* Add the marker to the codestream index*/
4350                                         if (l_marker_handler->id != J2K_MS_SOT)
4351                                                 j2k_add_mhmarker_v2(p_j2k->cstr_index, J2K_MS_UNK,
4352                                                                                         (OPJ_UINT32) opj_stream_tell(p_stream) - l_size_unk,
4353                                                                                         l_size_unk);
4354                                         break; /* next marker is known and well located */
4355                                 }
4356                                 else
4357                                         l_size_unk += 2;
4358                         }
4359                 }
4360         }
4361
4362         *output_marker = l_marker_handler->id ;
4363
4364         return OPJ_TRUE;
4365 }
4366
4367 /**
4368 Read the lookup table containing all the marker, status and action
4369 @param id Marker value
4370 */
4371 static opj_dec_mstabent_t *j2k_dec_mstab_lookup(int id) {
4372         opj_dec_mstabent_t *e;
4373         for (e = j2k_dec_mstab; e->id != 0; e++) {
4374                 if (e->id == id) {
4375                         break;
4376                 }
4377         }
4378         return e;
4379 }
4380
4381
4382 /**
4383  * Reads a MCT marker (Multiple Component Transform)
4384  *
4385  * @param       p_header_data   the data contained in the MCT box.
4386  * @param       p_j2k                   the jpeg2000 codec.
4387  * @param       p_header_size   the size of the data contained in the MCT marker.
4388  * @param       p_manager               the user event manager.
4389 */
4390 opj_bool j2k_read_mct ( opj_j2k_v2_t *p_j2k,
4391                                                 OPJ_BYTE * p_header_data,
4392                                                 OPJ_UINT32 p_header_size,
4393                                                 struct opj_event_mgr * p_manager )
4394 {
4395         OPJ_UINT32 i;
4396         opj_tcp_v2_t *l_tcp = 00;
4397         OPJ_UINT32 l_tmp;
4398         OPJ_UINT32 l_indix;
4399         opj_mct_data_t * l_mct_data;
4400
4401         /* preconditions */
4402         assert(p_header_data != 00);
4403         assert(p_j2k != 00);
4404
4405         l_tcp = p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH ?
4406                         &p_j2k->m_cp.tcps[p_j2k->m_current_tile_number] :
4407                         p_j2k->m_specific_param.m_decoder.m_default_tcp;
4408
4409         if (p_header_size < 2) {
4410                 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCT marker\n");
4411                 return OPJ_FALSE;
4412         }
4413
4414         /* first marker */
4415         opj_read_bytes(p_header_data,&l_tmp,2);                         /* Zmct */
4416         p_header_data += 2;
4417         if (l_tmp != 0) {
4418                 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge mct data within multiple MCT records\n");
4419                 return OPJ_TRUE;
4420         }
4421
4422         if(p_header_size <= 6) {
4423                 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCT marker\n");
4424                 return OPJ_FALSE;
4425         }
4426
4427         /* Imct -> no need for other values, take the first, type is double with decorrelation x0000 1101 0000 0000*/
4428         opj_read_bytes(p_header_data,&l_tmp,2);                         /* Imct */
4429         p_header_data += 2;
4430
4431         l_indix = l_tmp & 0xff;
4432         l_mct_data = l_tcp->m_mct_records;
4433
4434         for (i=0;i<l_tcp->m_nb_mct_records;++i) {
4435                 if (l_mct_data->m_index == l_indix) {
4436                         break;
4437                 }
4438                 ++l_mct_data;
4439         }
4440
4441         /* NOT FOUND */
4442         if (i == l_tcp->m_nb_mct_records) {
4443                 if (l_tcp->m_nb_mct_records == l_tcp->m_nb_max_mct_records) {
4444                         l_tcp->m_nb_max_mct_records += J2K_MCT_DEFAULT_NB_RECORDS;
4445
4446                         l_tcp->m_mct_records = (opj_mct_data_t*)opj_realloc(l_tcp->m_mct_records,l_tcp->m_nb_max_mct_records * sizeof(opj_mct_data_t));
4447                         if(! l_tcp->m_mct_records) {
4448                                 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCT marker\n");
4449                                 return OPJ_FALSE;
4450                         }
4451
4452                         l_mct_data = l_tcp->m_mct_records + l_tcp->m_nb_mct_records;
4453                         memset(l_mct_data ,0,(l_tcp->m_nb_max_mct_records - l_tcp->m_nb_mct_records) * sizeof(opj_mct_data_t));
4454                 }
4455
4456                 l_mct_data = l_tcp->m_mct_records + l_tcp->m_nb_mct_records;
4457         }
4458
4459         if (l_mct_data->m_data) {
4460                 opj_free(l_mct_data->m_data);
4461                 l_mct_data->m_data = 00;
4462         }
4463
4464         l_mct_data->m_index = l_indix;
4465         l_mct_data->m_array_type = (J2K_MCT_ARRAY_TYPE)((l_tmp  >> 8) & 3);
4466         l_mct_data->m_element_type = (J2K_MCT_ELEMENT_TYPE)((l_tmp  >> 10) & 3);
4467
4468         opj_read_bytes(p_header_data,&l_tmp,2);                         /* Ymct */
4469         p_header_data+=2;
4470         if (l_tmp != 0) {
4471                 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge multiple MCT markers\n");
4472                 return OPJ_TRUE;
4473         }
4474
4475         p_header_size -= 6;
4476
4477         l_mct_data->m_data = (OPJ_BYTE*)opj_malloc(p_header_size);
4478         if (! l_mct_data->m_data) {
4479                 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCT marker\n");
4480                 return OPJ_FALSE;
4481         }
4482         memcpy(l_mct_data->m_data,p_header_data,p_header_size);
4483
4484         l_mct_data->m_data_size = p_header_size;
4485         ++l_tcp->m_nb_mct_records;
4486
4487         return OPJ_TRUE;
4488 }
4489
4490 /**
4491  * Reads a MCC marker (Multiple Component Collection)
4492  *
4493  * @param       p_header_data   the data contained in the MCC box.
4494  * @param       p_j2k                   the jpeg2000 codec.
4495  * @param       p_header_size   the size of the data contained in the MCC marker.
4496  * @param       p_manager               the user event manager.
4497 */
4498 opj_bool j2k_read_mcc ( opj_j2k_v2_t *p_j2k,
4499                                         OPJ_BYTE * p_header_data,
4500                                         OPJ_UINT32 p_header_size,
4501                                         struct opj_event_mgr * p_manager )
4502 {
4503         OPJ_UINT32 i,j;
4504         OPJ_UINT32 l_tmp;
4505         OPJ_UINT32 l_indix;
4506         opj_tcp_v2_t * l_tcp;
4507         opj_simple_mcc_decorrelation_data_t * l_mcc_record;
4508         opj_mct_data_t * l_mct_data;
4509         OPJ_UINT32 l_nb_collections;
4510         OPJ_UINT32 l_nb_comps;
4511         OPJ_UINT32 l_nb_bytes_by_comp;
4512
4513
4514         /* preconditions */
4515         assert(p_header_data != 00);
4516         assert(p_j2k != 00);
4517         assert(p_manager != 00);
4518
4519         l_tcp = p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH ?
4520                         &p_j2k->m_cp.tcps[p_j2k->m_current_tile_number] :
4521                         p_j2k->m_specific_param.m_decoder.m_default_tcp;
4522
4523         if (p_header_size < 2) {
4524                 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
4525                 return OPJ_FALSE;
4526         }
4527
4528         /* first marker */
4529         opj_read_bytes(p_header_data,&l_tmp,2);                         /* Zmcc */
4530         p_header_data += 2;
4531         if (l_tmp != 0) {
4532                 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge multiple data spanning\n");
4533                 return OPJ_TRUE;
4534         }
4535
4536         if (p_header_size < 7) {
4537                 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
4538                 return OPJ_FALSE;
4539         }
4540
4541         opj_read_bytes(p_header_data,&l_indix,1); /* Imcc -> no need for other values, take the first */
4542         ++p_header_data;
4543
4544         l_mcc_record = l_tcp->m_mcc_records;
4545
4546         for(i=0;i<l_tcp->m_nb_mcc_records;++i) {
4547                 if (l_mcc_record->m_index == l_indix) {
4548                         break;
4549                 }
4550                 ++l_mcc_record;
4551         }
4552
4553         /** NOT FOUND */
4554         if (i == l_tcp->m_nb_mcc_records) {
4555                 if (l_tcp->m_nb_mcc_records == l_tcp->m_nb_max_mcc_records) {
4556                         l_tcp->m_nb_max_mcc_records += J2K_MCC_DEFAULT_NB_RECORDS;
4557
4558                         l_tcp->m_mcc_records = (opj_simple_mcc_decorrelation_data_t*)
4559                                         opj_realloc(l_tcp->m_mcc_records,l_tcp->m_nb_max_mcc_records * sizeof(opj_simple_mcc_decorrelation_data_t));
4560                         if (! l_tcp->m_mcc_records) {
4561                                 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
4562                                 return OPJ_FALSE;
4563                         }
4564                         l_mcc_record = l_tcp->m_mcc_records + l_tcp->m_nb_mcc_records;
4565                         memset(l_mcc_record,0,(l_tcp->m_nb_max_mcc_records-l_tcp->m_nb_mcc_records) * sizeof(opj_simple_mcc_decorrelation_data_t));
4566                 }
4567                 l_mcc_record = l_tcp->m_mcc_records + l_tcp->m_nb_mcc_records;
4568         }
4569         l_mcc_record->m_index = l_indix;
4570
4571         /* only one marker atm */
4572         opj_read_bytes(p_header_data,&l_tmp,2);                         /* Ymcc */
4573         p_header_data+=2;
4574         if (l_tmp != 0) {
4575                 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge multiple data spanning\n");
4576                 return OPJ_TRUE;
4577         }
4578
4579         opj_read_bytes(p_header_data,&l_nb_collections,2);                              /* Qmcc -> number of collections -> 1 */
4580         p_header_data+=2;
4581
4582         if (l_nb_collections > 1) {
4583                 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge multiple collections\n");
4584                 return OPJ_TRUE;
4585         }
4586
4587         p_header_size -= 7;
4588
4589         for (i=0;i<l_nb_collections;++i) {
4590                 if (p_header_size < 3) {
4591                         opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
4592                         return OPJ_FALSE;
4593                 }
4594
4595                 opj_read_bytes(p_header_data,&l_tmp,1); /* Xmcci type of component transformation -> array based decorrelation */
4596                 ++p_header_data;
4597
4598                 if (l_tmp != 1) {
4599                         opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge collections other than array decorrelation\n");
4600                         return OPJ_TRUE;
4601                 }
4602
4603                 opj_read_bytes(p_header_data,&l_nb_comps,2);
4604
4605                 p_header_data+=2;
4606                 p_header_size-=3;
4607
4608                 l_nb_bytes_by_comp = 1 + (l_nb_comps>>15);
4609                 l_mcc_record->m_nb_comps = l_nb_comps & 0x7fff;
4610
4611                 if (p_header_size < (l_nb_bytes_by_comp * l_mcc_record->m_nb_comps + 2)) {
4612                         opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
4613                         return OPJ_FALSE;
4614                 }
4615
4616                 p_header_size -= (l_nb_bytes_by_comp * l_mcc_record->m_nb_comps + 2);
4617
4618                 for (j=0;j<l_mcc_record->m_nb_comps;++j) {
4619                         opj_read_bytes(p_header_data,&l_tmp,l_nb_bytes_by_comp);        /* Cmccij Component offset*/
4620                         p_header_data+=l_nb_bytes_by_comp;
4621
4622                         if (l_tmp != j) {
4623                                 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge collections with indix shuffle\n");
4624                                 return OPJ_TRUE;
4625                         }
4626                 }
4627
4628                 opj_read_bytes(p_header_data,&l_nb_comps,2);
4629                 p_header_data+=2;
4630
4631                 l_nb_bytes_by_comp = 1 + (l_nb_comps>>15);
4632                 l_nb_comps &= 0x7fff;
4633
4634                 if (l_nb_comps != l_mcc_record->m_nb_comps) {
4635                         opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge collections without same number of indixes\n");
4636                         return OPJ_TRUE;
4637                 }
4638
4639                 if (p_header_size < (l_nb_bytes_by_comp * l_mcc_record->m_nb_comps + 3)) {
4640                         opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
4641                         return OPJ_FALSE;
4642                 }
4643
4644                 p_header_size -= (l_nb_bytes_by_comp * l_mcc_record->m_nb_comps + 3);
4645
4646                 for (j=0;j<l_mcc_record->m_nb_comps;++j) {
4647                         opj_read_bytes(p_header_data,&l_tmp,l_nb_bytes_by_comp);        /* Wmccij Component offset*/
4648                         p_header_data+=l_nb_bytes_by_comp;
4649
4650                         if (l_tmp != j) {
4651                                 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge collections with indix shuffle\n");
4652                                 return OPJ_TRUE;
4653                         }
4654                 }
4655
4656                 opj_read_bytes(p_header_data,&l_tmp,3); /* Wmccij Component offset*/
4657                 p_header_data += 3;
4658
4659                 l_mcc_record->m_is_irreversible = ! ((l_tmp>>16) & 1);
4660                 l_mcc_record->m_decorrelation_array = 00;
4661                 l_mcc_record->m_offset_array = 00;
4662
4663                 l_indix = l_tmp & 0xff;
4664                 if (l_indix != 0) {
4665                         l_mct_data = l_tcp->m_mct_records;
4666                         for (j=0;j<l_tcp->m_nb_mct_records;++j) {
4667                                 if (l_mct_data->m_index == l_indix) {
4668                                         l_mcc_record->m_decorrelation_array = l_mct_data;
4669                                         break;
4670                                 }
4671                                 ++l_mct_data;
4672                         }
4673
4674                         if (l_mcc_record->m_decorrelation_array == 00) {
4675                                 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
4676                                 return OPJ_FALSE;
4677                         }
4678                 }
4679
4680                 l_indix = (l_tmp >> 8) & 0xff;
4681                 if (l_indix != 0) {
4682                         l_mct_data = l_tcp->m_mct_records;
4683                         for (j=0;j<l_tcp->m_nb_mct_records;++j) {
4684                                 if (l_mct_data->m_index == l_indix) {
4685                                         l_mcc_record->m_offset_array = l_mct_data;
4686                                         break;
4687                                 }
4688                                 ++l_mct_data;
4689                         }
4690
4691                         if (l_mcc_record->m_offset_array == 00) {
4692                                 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
4693                                 return OPJ_FALSE;
4694                         }
4695                 }
4696         }
4697
4698         if (p_header_size != 0) {
4699                 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
4700                 return OPJ_FALSE;
4701         }
4702
4703         ++l_tcp->m_nb_mcc_records;
4704
4705         return OPJ_TRUE;
4706 }
4707
4708 /**
4709  * Reads a MCO marker (Multiple Component Transform Ordering)
4710  *
4711  * @param       p_header_data   the data contained in the MCO box.
4712  * @param       p_j2k                   the jpeg2000 codec.
4713  * @param       p_header_size   the size of the data contained in the MCO marker.
4714  * @param       p_manager               the user event manager.
4715 */
4716 opj_bool j2k_read_mco ( opj_j2k_v2_t *p_j2k,
4717                                                 OPJ_BYTE * p_header_data,
4718                                                 OPJ_UINT32 p_header_size,
4719                                                 struct opj_event_mgr * p_manager )
4720 {
4721         OPJ_UINT32 l_tmp, i;
4722         OPJ_UINT32 l_nb_stages;
4723         opj_tcp_v2_t * l_tcp;
4724         opj_tccp_t * l_tccp;
4725         opj_image_t * l_image;
4726         opj_image_comp_t * l_img_comp;
4727
4728         /* preconditions */
4729         assert(p_header_data != 00);
4730         assert(p_j2k != 00);
4731         assert(p_manager != 00);
4732
4733         l_image = p_j2k->m_private_image;
4734         l_tcp = p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH ?
4735                         &p_j2k->m_cp.tcps[p_j2k->m_current_tile_number] :
4736                         p_j2k->m_specific_param.m_decoder.m_default_tcp;
4737
4738         if (p_header_size < 1) {
4739                 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCO marker\n");
4740                 return OPJ_FALSE;
4741         }
4742
4743         opj_read_bytes(p_header_data,&l_nb_stages,1);                           /* Nmco : only one tranform stage*/
4744         ++p_header_data;
4745
4746         if (l_nb_stages > 1) {
4747                 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge multiple transformation stages.\n");
4748                 return OPJ_TRUE;
4749         }
4750
4751         if (p_header_size != l_nb_stages + 1) {
4752                 opj_event_msg_v2(p_manager, EVT_WARNING, "Error reading MCO marker\n");
4753                 return OPJ_FALSE;
4754         }
4755
4756         l_tccp = l_tcp->tccps;
4757         l_img_comp = l_image->comps;
4758
4759         for (i=0;i<l_image->numcomps;++i) {
4760                 l_tccp->m_dc_level_shift = 0;
4761                 ++l_tccp;
4762         }
4763
4764         if (l_tcp->m_mct_decoding_matrix) {
4765                 opj_free(l_tcp->m_mct_decoding_matrix);
4766                 l_tcp->m_mct_decoding_matrix = 00;
4767         }
4768
4769         for (i=0;i<l_nb_stages;++i) {
4770                 opj_read_bytes(p_header_data,&l_tmp,1);
4771                 ++p_header_data;
4772
4773                 if (! j2k_add_mct(l_tcp,p_j2k->m_private_image,l_tmp)) {
4774                         return OPJ_FALSE;
4775                 }
4776         }
4777
4778         return OPJ_TRUE;
4779 }
4780
4781 opj_bool j2k_add_mct(opj_tcp_v2_t * p_tcp, opj_image_t * p_image, OPJ_UINT32 p_index)
4782 {
4783         OPJ_UINT32 i;
4784         opj_simple_mcc_decorrelation_data_t * l_mcc_record;
4785         opj_mct_data_t * l_deco_array, * l_offset_array;
4786         OPJ_UINT32 l_data_size,l_mct_size, l_offset_size;
4787         OPJ_UINT32 l_nb_elem;
4788         OPJ_UINT32 * l_offset_data, * l_current_offset_data;
4789         opj_tccp_t * l_tccp;
4790
4791         /* preconditions */
4792         assert(p_tcp != 00);
4793
4794         l_mcc_record = p_tcp->m_mcc_records;
4795
4796         for (i=0;i<p_tcp->m_nb_mcc_records;++i) {
4797                 if (l_mcc_record->m_index == p_index) {
4798                         break;
4799                 }
4800         }
4801
4802         if (i==p_tcp->m_nb_mcc_records) {
4803                 /** element discarded **/
4804                 return OPJ_TRUE;
4805         }
4806
4807         if (l_mcc_record->m_nb_comps != p_image->numcomps) {
4808                 /** do not support number of comps != image */
4809                 return OPJ_TRUE;
4810         }
4811
4812         l_deco_array = l_mcc_record->m_decorrelation_array;
4813
4814         if (l_deco_array) {
4815                 l_data_size = MCT_ELEMENT_SIZE[l_deco_array->m_element_type] * p_image->numcomps * p_image->numcomps;
4816                 if (l_deco_array->m_data_size != l_data_size) {
4817                         return OPJ_FALSE;
4818                 }
4819
4820                 l_nb_elem = p_image->numcomps * p_image->numcomps;
4821                 l_mct_size = l_nb_elem * sizeof(OPJ_FLOAT32);
4822                 p_tcp->m_mct_decoding_matrix = (OPJ_FLOAT32*)opj_malloc(l_mct_size);
4823
4824                 if (! p_tcp->m_mct_decoding_matrix ) {
4825                         return OPJ_FALSE;
4826                 }
4827
4828                 j2k_mct_read_functions_to_float[l_deco_array->m_element_type](l_deco_array->m_data,p_tcp->m_mct_decoding_matrix,l_nb_elem);
4829         }
4830
4831         l_offset_array = l_mcc_record->m_offset_array;
4832
4833         if (l_offset_array) {
4834                 l_data_size = MCT_ELEMENT_SIZE[l_offset_array->m_element_type] * p_image->numcomps;
4835                 if (l_offset_array->m_data_size != l_data_size) {
4836                         return OPJ_FALSE;
4837                 }
4838
4839                 l_nb_elem = p_image->numcomps;
4840                 l_offset_size = l_nb_elem * sizeof(OPJ_UINT32);
4841                 l_offset_data = (OPJ_UINT32*)opj_malloc(l_offset_size);
4842
4843                 if (! l_offset_data ) {
4844                         return OPJ_FALSE;
4845                 }
4846
4847                 j2k_mct_read_functions_to_int32[l_offset_array->m_element_type](l_offset_array->m_data,l_offset_data,l_nb_elem);
4848
4849                 l_tccp = p_tcp->tccps;
4850                 l_current_offset_data = l_offset_data;
4851
4852                 for (i=0;i<p_image->numcomps;++i) {
4853                         l_tccp->m_dc_level_shift = *(l_current_offset_data++);
4854                         ++l_tccp;
4855                 }
4856
4857                 opj_free(l_offset_data);
4858         }
4859
4860         return OPJ_TRUE;
4861 }
4862
4863 /**
4864  * Reads a CBD marker (Component bit depth definition)
4865  * @param       p_header_data   the data contained in the CBD box.
4866  * @param       p_j2k                   the jpeg2000 codec.
4867  * @param       p_header_size   the size of the data contained in the CBD marker.
4868  * @param       p_manager               the user event manager.
4869 */
4870 opj_bool j2k_read_cbd ( opj_j2k_v2_t *p_j2k,
4871                                                 OPJ_BYTE * p_header_data,
4872                                                 OPJ_UINT32 p_header_size,
4873                                                 struct opj_event_mgr * p_manager)
4874 {
4875         OPJ_UINT32 l_nb_comp,l_num_comp;
4876         OPJ_UINT32 l_comp_def;
4877         OPJ_UINT32 i;
4878         opj_image_comp_t * l_comp = 00;
4879
4880         /* preconditions */
4881         assert(p_header_data != 00);
4882         assert(p_j2k != 00);
4883         assert(p_manager != 00);
4884
4885         l_num_comp = p_j2k->m_private_image->numcomps;
4886
4887         if (p_header_size != (p_j2k->m_private_image->numcomps + 2)) {
4888                 opj_event_msg_v2(p_manager, EVT_ERROR, "Crror reading CBD marker\n");
4889                 return OPJ_FALSE;
4890         }
4891
4892         opj_read_bytes(p_header_data,&l_nb_comp,2);                             /* Ncbd */
4893         p_header_data+=2;
4894
4895         if (l_nb_comp != l_num_comp) {
4896                 opj_event_msg_v2(p_manager, EVT_ERROR, "Crror reading CBD marker\n");
4897                 return OPJ_FALSE;
4898         }
4899
4900         l_comp = p_j2k->m_private_image->comps;
4901         for (i=0;i<l_num_comp;++i) {
4902                 opj_read_bytes(p_header_data,&l_comp_def,1);                    /* Component bit depth */
4903                 ++p_header_data;
4904         l_comp->sgnd = (l_comp_def>>7) & 1;
4905                 l_comp->prec = (l_comp_def&0x7f) + 1;
4906                 ++l_comp;
4907         }
4908
4909         return OPJ_TRUE;
4910 }
4911
4912
4913 /* ----------------------------------------------------------------------- */
4914 /* J2K / JPT decoder interface                                             */
4915 /* ----------------------------------------------------------------------- */
4916
4917 opj_j2k_t* j2k_create_decompress(opj_common_ptr cinfo) {
4918         opj_j2k_t *j2k = (opj_j2k_t*) opj_calloc(1, sizeof(opj_j2k_t));
4919         if(!j2k)
4920                 return NULL;
4921
4922         j2k->default_tcp = (opj_tcp_t*) opj_calloc(1, sizeof(opj_tcp_t));
4923         if(!j2k->default_tcp) {
4924                 opj_free(j2k);
4925                 return NULL;
4926         }
4927
4928         j2k->cinfo = cinfo;
4929         j2k->tile_data = NULL;
4930
4931         return j2k;
4932 }
4933
4934 void j2k_destroy_decompress(opj_j2k_t *j2k) {
4935         int i = 0;
4936
4937         if(j2k->tile_len != NULL) {
4938                 opj_free(j2k->tile_len);
4939         }
4940         if(j2k->tile_data != NULL) {
4941                 opj_free(j2k->tile_data);
4942         }
4943         if(j2k->default_tcp != NULL) {
4944                 opj_tcp_t *default_tcp = j2k->default_tcp;
4945                 if(default_tcp->ppt_data_first != NULL) {
4946                         opj_free(default_tcp->ppt_data_first);
4947                 }
4948                 if(j2k->default_tcp->tccps != NULL) {
4949                         opj_free(j2k->default_tcp->tccps);
4950                 }
4951                 opj_free(j2k->default_tcp);
4952         }
4953         if(j2k->cp != NULL) {
4954                 opj_cp_t *cp = j2k->cp;
4955                 if(cp->tcps != NULL) {
4956                         for(i = 0; i < cp->tw * cp->th; i++) {
4957                                 if(cp->tcps[i].ppt_data_first != NULL) {
4958                                         opj_free(cp->tcps[i].ppt_data_first);
4959                                 }
4960                                 if(cp->tcps[i].tccps != NULL) {
4961                                         opj_free(cp->tcps[i].tccps);
4962                                 }
4963                         }
4964                         opj_free(cp->tcps);
4965                 }
4966                 if(cp->ppm_data_first != NULL) {
4967                         opj_free(cp->ppm_data_first);
4968                 }
4969                 if(cp->tileno != NULL) {
4970                         opj_free(cp->tileno);  
4971                 }
4972                 if(cp->comment != NULL) {
4973                         opj_free(cp->comment);
4974                 }
4975
4976                 opj_free(cp);
4977         }
4978         opj_free(j2k);
4979 }
4980
4981 void j2k_setup_decoder(opj_j2k_t *j2k, opj_dparameters_t *parameters) {
4982         if(j2k && parameters) {
4983                 /* create and initialize the coding parameters structure */
4984                 opj_cp_t *cp = (opj_cp_t*) opj_calloc(1, sizeof(opj_cp_t));
4985                 cp->reduce = parameters->cp_reduce;     
4986                 cp->layer = parameters->cp_layer;
4987                 cp->limit_decoding = parameters->cp_limit_decoding;
4988
4989 #ifdef USE_JPWL
4990                 cp->correct = parameters->jpwl_correct;
4991                 cp->exp_comps = parameters->jpwl_exp_comps;
4992                 cp->max_tiles = parameters->jpwl_max_tiles;
4993 #endif /* USE_JPWL */
4994
4995
4996                 /* keep a link to cp so that we can destroy it later in j2k_destroy_decompress */
4997                 j2k->cp = cp;
4998         }
4999 }
5000
5001 void j2k_setup_decoder_v2(opj_j2k_v2_t *j2k, opj_dparameters_t *parameters)
5002 {
5003         if(j2k && parameters) {
5004                 j2k->m_cp.m_specific_param.m_dec.m_layer = parameters->cp_layer;
5005                 j2k->m_cp.m_specific_param.m_dec.m_reduce = parameters->cp_reduce;
5006
5007 #ifdef USE_JPWL
5008                 j2k->m_cp.correct = parameters->jpwl_correct;
5009                 j2k->m_cp.exp_comps = parameters->jpwl_exp_comps;
5010                 j2k->m_cp.max_tiles = parameters->jpwl_max_tiles;
5011 #endif /* USE_JPWL */
5012         }
5013 }
5014
5015 opj_image_t* j2k_decode(opj_j2k_t *j2k, opj_cio_t *cio, opj_codestream_info_t *cstr_info) {
5016         opj_image_t *image = NULL;
5017
5018         opj_common_ptr cinfo = j2k->cinfo;      
5019
5020         j2k->cio = cio;
5021         j2k->cstr_info = cstr_info;
5022         if (cstr_info)
5023                 memset(cstr_info, 0, sizeof(opj_codestream_info_t));
5024
5025         /* create an empty image */
5026         image = opj_image_create0();
5027         j2k->image = image;
5028
5029         j2k->state = J2K_STATE_MHSOC;
5030
5031         for (;;) {
5032                 opj_dec_mstabent_t *e;
5033                 int id = cio_read(cio, 2);
5034
5035 #ifdef USE_JPWL
5036                 /* we try to honor JPWL correction power */
5037                 if (j2k->cp->correct) {
5038
5039                         int orig_pos = cio_tell(cio);
5040                         opj_bool status;
5041
5042                         /* call the corrector */
5043                         status = jpwl_correct(j2k);
5044
5045                         /* go back to where you were */
5046                         cio_seek(cio, orig_pos - 2);
5047
5048                         /* re-read the marker */
5049                         id = cio_read(cio, 2);
5050
5051                         /* check whether it begins with ff */
5052                         if (id >> 8 != 0xff) {
5053                                 opj_event_msg(cinfo, EVT_ERROR,
5054                                         "JPWL: possible bad marker %x at %d\n",
5055                                         id, cio_tell(cio) - 2);
5056                                 if (!JPWL_ASSUME) {
5057                                         opj_image_destroy(image);
5058                                         opj_event_msg(cinfo, EVT_ERROR, "JPWL: giving up\n");
5059                                         return 0;
5060                                 }
5061                                 /* we try to correct */
5062                                 id = id | 0xff00;
5063                                 cio_seek(cio, cio_tell(cio) - 2);
5064                                 cio_write(cio, id, 2);
5065                                 opj_event_msg(cinfo, EVT_WARNING, "- trying to adjust this\n"
5066                                         "- setting marker to %x\n",
5067                                         id);
5068                         }
5069
5070                 }
5071 #endif /* USE_JPWL */
5072
5073                 if (id >> 8 != 0xff) {
5074                         opj_image_destroy(image);
5075                         opj_event_msg(cinfo, EVT_ERROR, "%.8x: expected a marker instead of %x\n", cio_tell(cio) - 2, id);
5076                         return 0;
5077                 }
5078                 e = j2k_dec_mstab_lookup(id);
5079                 // Check if the marker is known
5080                 if (!(j2k->state & e->states)) {
5081                         opj_image_destroy(image);
5082                         opj_event_msg(cinfo, EVT_ERROR, "%.8x: unexpected marker %x\n", cio_tell(cio) - 2, id);
5083                         return 0;
5084                 }
5085                 // Check if the decoding is limited to the main header
5086                 if (e->id == J2K_MS_SOT && j2k->cp->limit_decoding == LIMIT_TO_MAIN_HEADER) {
5087                         opj_event_msg(cinfo, EVT_INFO, "Main Header decoded.\n");
5088                         return image;
5089                 }               
5090
5091                 if (e->handler) {
5092                         (*e->handler)(j2k);
5093                 }
5094                 if (j2k->state & J2K_STATE_ERR) 
5095                         return NULL;    
5096
5097                 if (j2k->state == J2K_STATE_MT) {
5098                         break;
5099                 }
5100                 if (j2k->state == J2K_STATE_NEOC) {
5101                         break;
5102                 }
5103         }
5104         if (j2k->state == J2K_STATE_NEOC) {
5105                 j2k_read_eoc(j2k);
5106         }
5107
5108         if (j2k->state != J2K_STATE_MT) {
5109                 opj_event_msg(cinfo, EVT_WARNING, "Incomplete bitstream\n");
5110         }
5111         return image;
5112 }
5113
5114 /*
5115 * Read a JPT-stream and decode file
5116 *
5117 */
5118 opj_image_t* j2k_decode_jpt_stream(opj_j2k_t *j2k, opj_cio_t *cio,  opj_codestream_info_t *cstr_info) {
5119         opj_image_t *image = NULL;
5120         opj_jpt_msg_header_t header;
5121         int position;
5122         opj_common_ptr cinfo = j2k->cinfo;
5123
5124         OPJ_ARG_NOT_USED(cstr_info);
5125
5126         j2k->cio = cio;
5127
5128         /* create an empty image */
5129         image = opj_image_create0();
5130         j2k->image = image;
5131
5132         j2k->state = J2K_STATE_MHSOC;
5133         
5134         /* Initialize the header */
5135         jpt_init_msg_header(&header);
5136         /* Read the first header of the message */
5137         jpt_read_msg_header(cinfo, cio, &header);
5138         
5139         position = cio_tell(cio);
5140         if (header.Class_Id != 6) {     /* 6 : Main header data-bin message */
5141                 opj_image_destroy(image);
5142                 opj_event_msg(cinfo, EVT_ERROR, "[JPT-stream] : Expecting Main header first [class_Id %d] !\n", header.Class_Id);
5143                 return 0;
5144         }
5145         
5146         for (;;) {
5147                 opj_dec_mstabent_t *e = NULL;
5148                 int id;
5149                 
5150                 if (!cio_numbytesleft(cio)) {
5151                         j2k_read_eoc(j2k);
5152                         return image;
5153                 }
5154                 /* data-bin read -> need to read a new header */
5155                 if ((unsigned int) (cio_tell(cio) - position) == header.Msg_length) {
5156                         jpt_read_msg_header(cinfo, cio, &header);
5157                         position = cio_tell(cio);
5158                         if (header.Class_Id != 4) {     /* 4 : Tile data-bin message */
5159                                 opj_image_destroy(image);
5160                                 opj_event_msg(cinfo, EVT_ERROR, "[JPT-stream] : Expecting Tile info !\n");
5161                                 return 0;
5162                         }
5163                 }
5164                 
5165                 id = cio_read(cio, 2);
5166                 if (id >> 8 != 0xff) {
5167                         opj_image_destroy(image);
5168                         opj_event_msg(cinfo, EVT_ERROR, "%.8x: expected a marker instead of %x\n", cio_tell(cio) - 2, id);
5169                         return 0;
5170                 }
5171                 e = j2k_dec_mstab_lookup(id);
5172                 if (!(j2k->state & e->states)) {
5173                         opj_image_destroy(image);
5174                         opj_event_msg(cinfo, EVT_ERROR, "%.8x: unexpected marker %x\n", cio_tell(cio) - 2, id);
5175                         return 0;
5176                 }
5177                 if (e->handler) {
5178                         (*e->handler)(j2k);
5179                 }
5180                 if (j2k->state == J2K_STATE_MT) {
5181                         break;
5182                 }
5183                 if (j2k->state == J2K_STATE_NEOC) {
5184                         break;
5185                 }
5186         }
5187         if (j2k->state == J2K_STATE_NEOC) {
5188                 j2k_read_eoc(j2k);
5189         }
5190         
5191         if (j2k->state != J2K_STATE_MT) {
5192                 opj_event_msg(cinfo, EVT_WARNING, "Incomplete bitstream\n");
5193         }
5194
5195         return image;
5196 }
5197
5198 /* ----------------------------------------------------------------------- */
5199 /* J2K encoder interface                                                       */
5200 /* ----------------------------------------------------------------------- */
5201
5202 opj_j2k_t* j2k_create_compress(opj_common_ptr cinfo) {
5203         opj_j2k_t *j2k = (opj_j2k_t*) opj_calloc(1, sizeof(opj_j2k_t));
5204         if(j2k) {
5205                 j2k->cinfo = cinfo;
5206         }
5207         return j2k;
5208 }
5209
5210 opj_j2k_v2_t* j2k_create_compress_v2()
5211 {
5212         opj_j2k_v2_t *l_j2k = (opj_j2k_v2_t*) opj_malloc(sizeof(opj_j2k_v2_t));
5213         if (!l_j2k) {
5214                 return NULL;
5215         }
5216
5217         memset(l_j2k,0,sizeof(opj_j2k_v2_t));
5218
5219         l_j2k->m_is_decoder = 0;
5220         l_j2k->m_cp.m_is_decoder = 0;
5221
5222         l_j2k->m_specific_param.m_encoder.m_header_tile_data = (OPJ_BYTE *) opj_malloc(J2K_DEFAULT_HEADER_SIZE);
5223         if (! l_j2k->m_specific_param.m_encoder.m_header_tile_data) {
5224                 j2k_destroy(l_j2k);
5225                 return NULL;
5226         }
5227
5228         l_j2k->m_specific_param.m_encoder.m_header_tile_data_size = J2K_DEFAULT_HEADER_SIZE;
5229
5230         // validation list creation
5231         l_j2k->m_validation_list = opj_procedure_list_create();
5232         if (! l_j2k->m_validation_list) {
5233                 j2k_destroy(l_j2k);
5234                 return NULL;
5235         }
5236
5237         // execution list creation
5238         l_j2k->m_procedure_list = opj_procedure_list_create();
5239         if (! l_j2k->m_procedure_list) {
5240                 j2k_destroy(l_j2k);
5241                 return NULL;
5242         }
5243
5244         return l_j2k;
5245 }
5246
5247 void j2k_destroy_compress(opj_j2k_t *j2k) {
5248         int tileno;
5249
5250         if(!j2k) return;
5251         if(j2k->cp != NULL) {
5252                 opj_cp_t *cp = j2k->cp;
5253
5254                 if(cp->comment) {
5255                         opj_free(cp->comment);
5256                 }
5257                 if(cp->matrice) {
5258                         opj_free(cp->matrice);
5259                 }
5260                 for (tileno = 0; tileno < cp->tw * cp->th; tileno++) {
5261                         opj_free(cp->tcps[tileno].tccps);
5262                 }
5263                 opj_free(cp->tcps);
5264                 opj_free(cp);
5265         }
5266
5267         opj_free(j2k);
5268 }
5269
5270 void j2k_setup_encoder(opj_j2k_t *j2k, opj_cparameters_t *parameters, opj_image_t *image) {
5271         int i, j, tileno, numpocs_tile;
5272         opj_cp_t *cp = NULL;
5273
5274         if(!j2k || !parameters || ! image) {
5275                 return;
5276         }
5277
5278         /* create and initialize the coding parameters structure */
5279         cp = (opj_cp_t*) opj_calloc(1, sizeof(opj_cp_t));
5280
5281         /* keep a link to cp so that we can destroy it later in j2k_destroy_compress */
5282         j2k->cp = cp;
5283
5284         /* set default values for cp */
5285         cp->tw = 1;
5286         cp->th = 1;
5287
5288         /* 
5289         copy user encoding parameters 
5290         */
5291         cp->cinema = parameters->cp_cinema;
5292         cp->max_comp_size =     parameters->max_comp_size;
5293         cp->rsiz   = parameters->cp_rsiz;
5294         cp->disto_alloc = parameters->cp_disto_alloc;
5295         cp->fixed_alloc = parameters->cp_fixed_alloc;
5296         cp->fixed_quality = parameters->cp_fixed_quality;
5297
5298         /* mod fixed_quality */
5299         if(parameters->cp_matrice) {
5300                 size_t array_size = parameters->tcp_numlayers * parameters->numresolution * 3 * sizeof(int);
5301                 cp->matrice = (int *) opj_malloc(array_size);
5302                 memcpy(cp->matrice, parameters->cp_matrice, array_size);
5303         }
5304
5305         /* tiles */
5306         cp->tdx = parameters->cp_tdx;
5307         cp->tdy = parameters->cp_tdy;
5308
5309         /* tile offset */
5310         cp->tx0 = parameters->cp_tx0;
5311         cp->ty0 = parameters->cp_ty0;
5312
5313         /* comment string */
5314         if(parameters->cp_comment) {
5315                 cp->comment = (char*)opj_malloc(strlen(parameters->cp_comment) + 1);
5316                 if(cp->comment) {
5317                         strcpy(cp->comment, parameters->cp_comment);
5318                 }
5319         }
5320
5321         /*
5322         calculate other encoding parameters
5323         */
5324
5325         if (parameters->tile_size_on) {
5326                 cp->tw = int_ceildiv(image->x1 - cp->tx0, cp->tdx);
5327                 cp->th = int_ceildiv(image->y1 - cp->ty0, cp->tdy);
5328         } else {
5329                 cp->tdx = image->x1 - cp->tx0;
5330                 cp->tdy = image->y1 - cp->ty0;
5331         }
5332
5333         if(parameters->tp_on){
5334                 cp->tp_flag = parameters->tp_flag;
5335                 cp->tp_on = 1;
5336         }
5337         
5338         cp->img_size = 0;
5339         for(i=0;i<image->numcomps ;i++){
5340         cp->img_size += (image->comps[i].w *image->comps[i].h * image->comps[i].prec);
5341         }
5342
5343
5344 #ifdef USE_JPWL
5345         /*
5346         calculate JPWL encoding parameters
5347         */
5348
5349         if (parameters->jpwl_epc_on) {
5350                 int i;
5351
5352                 /* set JPWL on */
5353                 cp->epc_on = OPJ_TRUE;
5354                 cp->info_on = OPJ_FALSE; /* no informative technique */
5355
5356                 /* set EPB on */
5357                 if ((parameters->jpwl_hprot_MH > 0) || (parameters->jpwl_hprot_TPH[0] > 0)) {
5358                         cp->epb_on = OPJ_TRUE;
5359                         
5360                         cp->hprot_MH = parameters->jpwl_hprot_MH;
5361                         for (i = 0; i < JPWL_MAX_NO_TILESPECS; i++) {
5362                                 cp->hprot_TPH_tileno[i] = parameters->jpwl_hprot_TPH_tileno[i];
5363                                 cp->hprot_TPH[i] = parameters->jpwl_hprot_TPH[i];
5364                         }
5365                         /* if tile specs are not specified, copy MH specs */
5366                         if (cp->hprot_TPH[0] == -1) {
5367                                 cp->hprot_TPH_tileno[0] = 0;
5368                                 cp->hprot_TPH[0] = parameters->jpwl_hprot_MH;
5369                         }
5370                         for (i = 0; i < JPWL_MAX_NO_PACKSPECS; i++) {
5371                                 cp->pprot_tileno[i] = parameters->jpwl_pprot_tileno[i];
5372                                 cp->pprot_packno[i] = parameters->jpwl_pprot_packno[i];
5373                                 cp->pprot[i] = parameters->jpwl_pprot[i];
5374                         }
5375                 }
5376
5377                 /* set ESD writing */
5378                 if ((parameters->jpwl_sens_size == 1) || (parameters->jpwl_sens_size == 2)) {
5379                         cp->esd_on = OPJ_TRUE;
5380
5381                         cp->sens_size = parameters->jpwl_sens_size;
5382                         cp->sens_addr = parameters->jpwl_sens_addr;
5383                         cp->sens_range = parameters->jpwl_sens_range;
5384
5385                         cp->sens_MH = parameters->jpwl_sens_MH;
5386                         for (i = 0; i < JPWL_MAX_NO_TILESPECS; i++) {
5387                                 cp->sens_TPH_tileno[i] = parameters->jpwl_sens_TPH_tileno[i];
5388                                 cp->sens_TPH[i] = parameters->jpwl_sens_TPH[i];
5389                         }
5390                 }
5391
5392                 /* always set RED writing to false: we are at the encoder */
5393                 cp->red_on = OPJ_FALSE;
5394
5395         } else {
5396                 cp->epc_on = OPJ_FALSE;
5397         }
5398 #endif /* USE_JPWL */
5399
5400
5401         /* initialize the mutiple tiles */
5402         /* ---------------------------- */
5403         cp->tcps = (opj_tcp_t*) opj_calloc(cp->tw * cp->th, sizeof(opj_tcp_t));
5404
5405         for (tileno = 0; tileno < cp->tw * cp->th; tileno++) {
5406                 opj_tcp_t *tcp = &cp->tcps[tileno];
5407                 tcp->numlayers = parameters->tcp_numlayers;
5408                 for (j = 0; j < tcp->numlayers; j++) {
5409                         if(cp->cinema){
5410                                 if (cp->fixed_quality) {
5411                                         tcp->distoratio[j] = parameters->tcp_distoratio[j];
5412                                 }
5413                                 tcp->rates[j] = parameters->tcp_rates[j];
5414                         }else{
5415                                 if (cp->fixed_quality) {        /* add fixed_quality */
5416                                         tcp->distoratio[j] = parameters->tcp_distoratio[j];
5417                                 } else {
5418                                         tcp->rates[j] = parameters->tcp_rates[j];
5419                                 }
5420                         }
5421                 }
5422                 tcp->csty = parameters->csty;
5423                 tcp->prg = parameters->prog_order;
5424                 tcp->mct = parameters->tcp_mct; 
5425
5426                 numpocs_tile = 0;
5427                 tcp->POC = 0;
5428                 if (parameters->numpocs) {
5429                         /* initialisation of POC */
5430                         tcp->POC = 1;
5431                         for (i = 0; i < parameters->numpocs; i++) {
5432                                 if((tileno == parameters->POC[i].tile - 1) || (parameters->POC[i].tile == -1)) {
5433                                         opj_poc_t *tcp_poc = &tcp->pocs[numpocs_tile];
5434                                         tcp_poc->resno0         = parameters->POC[numpocs_tile].resno0;
5435                                         tcp_poc->compno0        = parameters->POC[numpocs_tile].compno0;
5436                                         tcp_poc->layno1         = parameters->POC[numpocs_tile].layno1;
5437                                         tcp_poc->resno1         = parameters->POC[numpocs_tile].resno1;
5438                                         tcp_poc->compno1        = parameters->POC[numpocs_tile].compno1;
5439                                         tcp_poc->prg1           = parameters->POC[numpocs_tile].prg1;
5440                                         tcp_poc->tile           = parameters->POC[numpocs_tile].tile;
5441                                         numpocs_tile++;
5442                                 }
5443                         }
5444                         tcp->numpocs = numpocs_tile -1 ;
5445                 }else{ 
5446                         tcp->numpocs = 0;
5447                 }
5448
5449                 tcp->tccps = (opj_tccp_t*) opj_calloc(image->numcomps, sizeof(opj_tccp_t));
5450
5451                 for (i = 0; i < image->numcomps; i++) {
5452                         opj_tccp_t *tccp = &tcp->tccps[i];
5453                         tccp->csty = parameters->csty & 0x01;   /* 0 => one precinct || 1 => custom precinct  */
5454                         tccp->numresolutions = parameters->numresolution;
5455                         tccp->cblkw = int_floorlog2(parameters->cblockw_init);
5456                         tccp->cblkh = int_floorlog2(parameters->cblockh_init);
5457                         tccp->cblksty = parameters->mode;
5458                         tccp->qmfbid = parameters->irreversible ? 0 : 1;
5459                         tccp->qntsty = parameters->irreversible ? J2K_CCP_QNTSTY_SEQNT : J2K_CCP_QNTSTY_NOQNT;
5460                         tccp->numgbits = 2;
5461                         if (i == parameters->roi_compno) {
5462                                 tccp->roishift = parameters->roi_shift;
5463                         } else {
5464                                 tccp->roishift = 0;
5465                         }
5466
5467                         if(parameters->cp_cinema)
5468                         {
5469                                 //Precinct size for lowest frequency subband=128
5470                                 tccp->prcw[0] = 7;
5471                                 tccp->prch[0] = 7;
5472                                 //Precinct size at all other resolutions = 256
5473                                 for (j = 1; j < tccp->numresolutions; j++) {
5474                                         tccp->prcw[j] = 8;
5475                                         tccp->prch[j] = 8;
5476                                 }
5477                         }else{
5478                                 if (parameters->csty & J2K_CCP_CSTY_PRT) {
5479                                         int p = 0;
5480                                         for (j = tccp->numresolutions - 1; j >= 0; j--) {
5481                                                 if (p < parameters->res_spec) {
5482                                                         
5483                                                         if (parameters->prcw_init[p] < 1) {
5484                                                                 tccp->prcw[j] = 1;
5485                                                         } else {
5486                                                                 tccp->prcw[j] = int_floorlog2(parameters->prcw_init[p]);
5487                                                         }
5488                                                         
5489                                                         if (parameters->prch_init[p] < 1) {
5490                                                                 tccp->prch[j] = 1;
5491                                                         }else {
5492                                                                 tccp->prch[j] = int_floorlog2(parameters->prch_init[p]);
5493                                                         }
5494
5495                                                 } else {
5496                                                         int res_spec = parameters->res_spec;
5497                                                         int size_prcw = parameters->prcw_init[res_spec - 1] >> (p - (res_spec - 1));
5498                                                         int size_prch = parameters->prch_init[res_spec - 1] >> (p - (res_spec - 1));
5499                                                         
5500                                                         if (size_prcw < 1) {
5501                                                                 tccp->prcw[j] = 1;
5502                                                         } else {
5503                                                                 tccp->prcw[j] = int_floorlog2(size_prcw);
5504                                                         }
5505                                                         
5506                                                         if (size_prch < 1) {
5507                                                                 tccp->prch[j] = 1;
5508                                                         } else {
5509                                                                 tccp->prch[j] = int_floorlog2(size_prch);
5510                                                         }
5511                                                 }
5512                                                 p++;
5513                                                 /*printf("\nsize precinct for level %d : %d,%d\n", j,tccp->prcw[j], tccp->prch[j]); */
5514                                         }       //end for
5515                                 } else {
5516                                         for (j = 0; j < tccp->numresolutions; j++) {
5517                                                 tccp->prcw[j] = 15;
5518                                                 tccp->prch[j] = 15;
5519                                         }
5520                                 }
5521                         }
5522
5523                         dwt_calc_explicit_stepsizes(tccp, image->comps[i].prec);
5524                 }
5525         }
5526 }
5527
5528 opj_bool j2k_encode(opj_j2k_t *j2k, opj_cio_t *cio, opj_image_t *image, opj_codestream_info_t *cstr_info) {
5529         int tileno, compno;
5530         opj_cp_t *cp = NULL;
5531
5532         opj_tcd_t *tcd = NULL;  /* TCD component */
5533
5534         j2k->cio = cio; 
5535         j2k->image = image;
5536
5537         cp = j2k->cp;
5538
5539         /* INDEX >> */
5540         j2k->cstr_info = cstr_info;
5541         if (cstr_info) {
5542                 int compno;
5543                 cstr_info->tile = (opj_tile_info_t *) opj_malloc(cp->tw * cp->th * sizeof(opj_tile_info_t));
5544                 cstr_info->image_w = image->x1 - image->x0;
5545                 cstr_info->image_h = image->y1 - image->y0;
5546                 cstr_info->prog = (&cp->tcps[0])->prg;
5547                 cstr_info->tw = cp->tw;
5548                 cstr_info->th = cp->th;
5549                 cstr_info->tile_x = cp->tdx;    /* new version parser */
5550                 cstr_info->tile_y = cp->tdy;    /* new version parser */
5551                 cstr_info->tile_Ox = cp->tx0;   /* new version parser */
5552                 cstr_info->tile_Oy = cp->ty0;   /* new version parser */
5553                 cstr_info->numcomps = image->numcomps;
5554                 cstr_info->numlayers = (&cp->tcps[0])->numlayers;
5555                 cstr_info->numdecompos = (int*) opj_malloc(image->numcomps * sizeof(int));
5556                 for (compno=0; compno < image->numcomps; compno++) {
5557                         cstr_info->numdecompos[compno] = (&cp->tcps[0])->tccps->numresolutions - 1;
5558                 }
5559                 cstr_info->D_max = 0.0;         /* ADD Marcela */
5560                 cstr_info->main_head_start = cio_tell(cio); /* position of SOC */
5561                 cstr_info->maxmarknum = 100;
5562                 cstr_info->marker = (opj_marker_info_t *) opj_malloc(cstr_info->maxmarknum * sizeof(opj_marker_info_t));
5563                 cstr_info->marknum = 0;
5564         }
5565         /* << INDEX */
5566
5567         j2k_write_soc(j2k);
5568         j2k_write_siz(j2k);
5569         j2k_write_cod(j2k);
5570         j2k_write_qcd(j2k);
5571
5572         if(cp->cinema){
5573                 for (compno = 1; compno < image->numcomps; compno++) {
5574                         j2k_write_coc(j2k, compno);
5575                         j2k_write_qcc(j2k, compno);
5576                 }
5577         }
5578
5579         for (compno = 0; compno < image->numcomps; compno++) {
5580                 opj_tcp_t *tcp = &cp->tcps[0];
5581                 if (tcp->tccps[compno].roishift)
5582                         j2k_write_rgn(j2k, compno, 0);
5583         }
5584         if (cp->comment != NULL) {
5585                 j2k_write_com(j2k);
5586         }
5587
5588         j2k->totnum_tp = j2k_calculate_tp(cp,image->numcomps,image,j2k);
5589         /* TLM Marker*/
5590         if(cp->cinema){
5591                 j2k_write_tlm(j2k);
5592                 if (cp->cinema == CINEMA4K_24) {
5593                         j2k_write_poc(j2k);
5594                 }
5595         }
5596
5597         /* uncomment only for testing JPSEC marker writing */
5598         /* j2k_write_sec(j2k); */
5599
5600         /* INDEX >> */
5601         if(cstr_info) {
5602                 cstr_info->main_head_end = cio_tell(cio) - 1;
5603         }
5604         /* << INDEX */
5605         /**** Main Header ENDS here ***/
5606
5607         /* create the tile encoder */
5608         tcd = tcd_create(j2k->cinfo);
5609
5610         /* encode each tile */
5611         for (tileno = 0; tileno < cp->tw * cp->th; tileno++) {
5612                 int pino;
5613                 int tilepartno=0;
5614                 /* UniPG>> */
5615                 int acc_pack_num = 0;
5616                 /* <<UniPG */
5617
5618
5619                 opj_tcp_t *tcp = &cp->tcps[tileno];
5620                 opj_event_msg(j2k->cinfo, EVT_INFO, "tile number %d / %d\n", tileno + 1, cp->tw * cp->th);
5621
5622                 j2k->curtileno = tileno;
5623                 j2k->cur_tp_num = 0;
5624                 tcd->cur_totnum_tp = j2k->cur_totnum_tp[j2k->curtileno];
5625                 /* initialisation before tile encoding  */
5626                 if (tileno == 0) {
5627                         tcd_malloc_encode(tcd, image, cp, j2k->curtileno);
5628                 } else {
5629                         tcd_init_encode(tcd, image, cp, j2k->curtileno);
5630                 }
5631
5632                 /* INDEX >> */
5633                 if(cstr_info) {
5634                         cstr_info->tile[j2k->curtileno].start_pos = cio_tell(cio) + j2k->pos_correction;
5635                         cstr_info->tile[j2k->curtileno].maxmarknum = 10;
5636                         cstr_info->tile[j2k->curtileno].marker = (opj_marker_info_t *) opj_malloc(cstr_info->tile[j2k->curtileno].maxmarknum * sizeof(opj_marker_info_t));
5637                         cstr_info->tile[j2k->curtileno].marknum = 0;
5638                 }
5639                 /* << INDEX */
5640
5641                 for(pino = 0; pino <= tcp->numpocs; pino++) {
5642                         int tot_num_tp;
5643                         tcd->cur_pino=pino;
5644
5645                         /*Get number of tile parts*/
5646                         tot_num_tp = j2k_get_num_tp(cp,pino,tileno);
5647                         tcd->tp_pos = cp->tp_pos;
5648
5649                         for(tilepartno = 0; tilepartno < tot_num_tp ; tilepartno++){
5650                                 j2k->tp_num = tilepartno;
5651                                 /* INDEX >> */
5652                                 if(cstr_info)
5653                                         cstr_info->tile[j2k->curtileno].tp[j2k->cur_tp_num].tp_start_pos =
5654                                         cio_tell(cio) + j2k->pos_correction;
5655                                 /* << INDEX */
5656                                 j2k_write_sot(j2k);
5657
5658                                 if(j2k->cur_tp_num == 0 && cp->cinema == 0){
5659                                         for (compno = 1; compno < image->numcomps; compno++) {
5660                                                 j2k_write_coc(j2k, compno);
5661                                                 j2k_write_qcc(j2k, compno);
5662                                         }
5663                                         if (cp->tcps[tileno].numpocs) {
5664                                                 j2k_write_poc(j2k);
5665                                         }
5666                                 }
5667
5668                                 /* INDEX >> */
5669                                 if(cstr_info)
5670                                         cstr_info->tile[j2k->curtileno].tp[j2k->cur_tp_num].tp_end_header =
5671                                         cio_tell(cio) + j2k->pos_correction + 1;
5672                                 /* << INDEX */
5673
5674                                 j2k_write_sod(j2k, tcd);
5675
5676                                 /* INDEX >> */
5677                                 if(cstr_info) {
5678                                         cstr_info->tile[j2k->curtileno].tp[j2k->cur_tp_num].tp_end_pos =
5679                                                 cio_tell(cio) + j2k->pos_correction - 1;
5680                                         cstr_info->tile[j2k->curtileno].tp[j2k->cur_tp_num].tp_start_pack =
5681                                                 acc_pack_num;
5682                                         cstr_info->tile[j2k->curtileno].tp[j2k->cur_tp_num].tp_numpacks =
5683                                                 cstr_info->packno - acc_pack_num;
5684                                         acc_pack_num = cstr_info->packno;
5685                                 }
5686                                 /* << INDEX */
5687
5688                                 j2k->cur_tp_num++;
5689                         }                       
5690                 }
5691                 if(cstr_info) {
5692                         cstr_info->tile[j2k->curtileno].end_pos = cio_tell(cio) + j2k->pos_correction - 1;
5693                 }
5694
5695
5696                 /*
5697                 if (tile->PPT) { // BAD PPT !!! 
5698                 FILE *PPT_file;
5699                 int i;
5700                 PPT_file=fopen("PPT","rb");
5701                 fprintf(stderr,"%c%c%c%c",255,97,tile->len_ppt/256,tile->len_ppt%256);
5702                 for (i=0;i<tile->len_ppt;i++) {
5703                 unsigned char elmt;
5704                 fread(&elmt, 1, 1, PPT_file);
5705                 fwrite(&elmt,1,1,f);
5706                 }
5707                 fclose(PPT_file);
5708                 unlink("PPT");
5709                 }
5710                 */
5711
5712         }
5713
5714         /* destroy the tile encoder */
5715         tcd_free_encode(tcd);
5716         tcd_destroy(tcd);
5717
5718         opj_free(j2k->cur_totnum_tp);
5719
5720         j2k_write_eoc(j2k);
5721
5722         if(cstr_info) {
5723                 cstr_info->codestream_size = cio_tell(cio) + j2k->pos_correction;
5724                 /* UniPG>> */
5725                 /* The following adjustment is done to adjust the codestream size */
5726                 /* if SOD is not at 0 in the buffer. Useful in case of JP2, where */
5727                 /* the first bunch of bytes is not in the codestream              */
5728                 cstr_info->codestream_size -= cstr_info->main_head_start;
5729                 /* <<UniPG */
5730         }
5731
5732 #ifdef USE_JPWL
5733         /*
5734         preparation of JPWL marker segments
5735         */
5736         if(cp->epc_on) {
5737
5738                 /* encode according to JPWL */
5739                 jpwl_encode(j2k, cio, image);
5740
5741         }
5742 #endif /* USE_JPWL */
5743
5744         return OPJ_TRUE;
5745 }
5746
5747 static void j2k_add_mhmarker(opj_codestream_info_t *cstr_info, unsigned short int type, int pos, int len) {
5748
5749         if (!cstr_info)
5750                 return;
5751
5752         /* expand the list? */
5753         if ((cstr_info->marknum + 1) > cstr_info->maxmarknum) {
5754                 cstr_info->maxmarknum = 100 + (int) ((float) cstr_info->maxmarknum * 1.0F);
5755                 cstr_info->marker = (opj_marker_info_t*)opj_realloc(cstr_info->marker, cstr_info->maxmarknum);
5756         }
5757
5758         /* add the marker */
5759         cstr_info->marker[cstr_info->marknum].type = type;
5760         cstr_info->marker[cstr_info->marknum].pos = pos;
5761         cstr_info->marker[cstr_info->marknum].len = len;
5762         cstr_info->marknum++;
5763
5764 }
5765
5766 static void j2k_add_mhmarker_v2(opj_codestream_index_t *cstr_index, OPJ_UINT32 type, OPJ_UINT32 pos, OPJ_UINT32 len) {
5767
5768         if (!cstr_index)
5769                 return;
5770
5771         /* expand the list? */
5772         if ((cstr_index->marknum + 1) > cstr_index->maxmarknum) {
5773                 cstr_index->maxmarknum = 100 + (int) ((float) cstr_index->maxmarknum * 1.0F);
5774                 cstr_index->marker = (opj_marker_info_t*)opj_realloc(cstr_index->marker, cstr_index->maxmarknum *sizeof(opj_marker_info_t));
5775         }
5776
5777         /* add the marker */
5778         cstr_index->marker[cstr_index->marknum].type = (OPJ_UINT16)type;
5779         cstr_index->marker[cstr_index->marknum].pos = (OPJ_INT32)pos;
5780         cstr_index->marker[cstr_index->marknum].len = (OPJ_INT32)len;
5781         cstr_index->marknum++;
5782
5783 }
5784
5785 static void j2k_add_tlmarker( int tileno, opj_codestream_info_t *cstr_info, unsigned short int type, int pos, int len) {
5786
5787
5788   opj_marker_info_t *marker;
5789
5790         if (!cstr_info)
5791                 return;
5792
5793         /* expand the list? */
5794         if ((cstr_info->tile[tileno].marknum + 1) > cstr_info->tile[tileno].maxmarknum) {
5795                 cstr_info->tile[tileno].maxmarknum = 100 + (int) ((float) cstr_info->tile[tileno].maxmarknum * 1.0F);
5796                 cstr_info->tile[tileno].marker = (opj_marker_info_t*)opj_realloc(cstr_info->tile[tileno].marker, cstr_info->maxmarknum);
5797         }
5798
5799         marker = &(cstr_info->tile[tileno].marker[cstr_info->tile[tileno].marknum]);
5800
5801         /* add the marker */
5802         marker->type = type;
5803         marker->pos = pos;
5804         marker->len = len;
5805         cstr_info->tile[tileno].marknum++;
5806 }
5807
5808 static void j2k_add_tlmarker_v2(OPJ_UINT32 tileno, opj_codestream_index_t *cstr_index, OPJ_UINT32 type, OPJ_UINT32 pos, OPJ_UINT32 len)
5809 {
5810
5811         if (!cstr_index)
5812                 return;
5813
5814         if (!cstr_index->tile_index)
5815
5816         /* expand the list? */
5817         if ((cstr_index->tile_index[tileno].marknum + 1) > cstr_index->tile_index[tileno].maxmarknum) {
5818                 cstr_index->tile_index[tileno].maxmarknum = 100 + (int) ((float) cstr_index->tile_index[tileno].maxmarknum * 1.0F);
5819                 cstr_index->tile_index[tileno].marker =
5820                                 (opj_marker_info_t*)opj_realloc(cstr_index->tile_index[tileno].marker,
5821                                                                                                 cstr_index->tile_index[tileno].maxmarknum *sizeof(opj_marker_info_t));
5822         }
5823
5824         /* add the marker */
5825         cstr_index->tile_index[tileno].marker[cstr_index->tile_index[tileno].marknum].type = (OPJ_UINT16)type;
5826         cstr_index->tile_index[tileno].marker[cstr_index->tile_index[tileno].marknum].pos = (OPJ_INT32)pos;
5827         cstr_index->tile_index[tileno].marker[cstr_index->tile_index[tileno].marknum].len = (OPJ_INT32)len;
5828         cstr_index->tile_index[tileno].marknum++;
5829
5830         if (type == J2K_MS_SOT) {
5831                 OPJ_UINT32 l_current_tile_part = cstr_index->tile_index[tileno].current_tpsno;
5832
5833                 if (cstr_index->tile_index[tileno].tp_index)
5834                         cstr_index->tile_index[tileno].tp_index[l_current_tile_part].start_pos = pos;
5835
5836         }
5837
5838 }
5839
5840
5841
5842
5843 /*
5844  * -----------------------------------------------------------------------
5845  * -----------------------------------------------------------------------
5846  * -----------------------------------------------------------------------
5847  */
5848
5849 /**
5850  * Ends the decompression procedures and possibiliy add data to be read after the
5851  * codestream.
5852  */
5853 opj_bool j2k_end_decompress(
5854                                                 opj_j2k_v2_t *p_j2k,
5855                                                 opj_stream_private_t *p_stream,
5856                                                 opj_event_mgr_t * p_manager)
5857 {
5858         return OPJ_TRUE;
5859 }
5860
5861 /**
5862  * Reads a jpeg2000 codestream header structure.
5863
5864  *
5865  * @param p_stream the stream to read data from.
5866  * @param p_j2k the jpeg2000 codec.
5867  * @param p_manager the user event manager.
5868  *
5869  * @return true if the box is valid.
5870  */
5871 opj_bool j2k_read_header(       struct opj_stream_private *p_stream,
5872                                                         opj_j2k_v2_t* p_j2k,
5873                                                         opj_image_t** p_image,
5874                                                         struct opj_event_mgr* p_manager )
5875 {
5876         /* preconditions */
5877         assert(p_j2k != 00);
5878         assert(p_stream != 00);
5879         assert(p_manager != 00);
5880
5881         /* create an empty image header */
5882         p_j2k->m_private_image = opj_image_create0();
5883         if (! p_j2k->m_private_image) {
5884                 return OPJ_FALSE;
5885         }
5886
5887         /* customization of the validation */
5888         j2k_setup_decoding_validation(p_j2k);
5889
5890         /* validation of the parameters codec */
5891         if (! j2k_exec(p_j2k, p_j2k->m_validation_list, p_stream,p_manager)) {
5892                 opj_image_destroy(p_j2k->m_private_image);
5893                 p_j2k->m_private_image = NULL;
5894                 return OPJ_FALSE;
5895         }
5896
5897         /* customization of the encoding */
5898         j2k_setup_header_reading(p_j2k);
5899
5900         /* read header */
5901         if (! j2k_exec (p_j2k,p_j2k->m_procedure_list,p_stream,p_manager)) {
5902                 opj_image_destroy(p_j2k->m_private_image);
5903                 p_j2k->m_private_image = NULL;
5904                 return OPJ_FALSE;
5905         }
5906
5907         *p_image = opj_image_create0();
5908         if (! (*p_image)) {
5909                 return OPJ_FALSE;
5910         }
5911
5912         /* Copy codestream image information to the output image */
5913         opj_copy_image_header(p_j2k->m_private_image, *p_image);
5914
5915
5916         return OPJ_TRUE;
5917 }
5918
5919 /**
5920  * Sets up the procedures to do on reading header. Developpers wanting to extend the library can add their own reading procedures.
5921  */
5922 void j2k_setup_header_reading (opj_j2k_v2_t *p_j2k)
5923 {
5924         // preconditions
5925         assert(p_j2k != 00);
5926
5927         opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(void*)j2k_read_header_procedure);
5928
5929         /* DEVELOPER CORNER, add your custom procedures */
5930         opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(void*)j2k_copy_default_tcp_and_create_tcd);
5931
5932 }
5933
5934 /**
5935  * Sets up the validation ,i.e. adds the procedures to lauch to make sure the codec parameters
5936  * are valid. Developpers wanting to extend the library can add their own validation procedures.
5937  */
5938 void j2k_setup_decoding_validation (opj_j2k_v2_t *p_j2k)
5939 {
5940         // preconditions
5941         assert(p_j2k != 00);
5942
5943         opj_procedure_list_add_procedure(p_j2k->m_validation_list, (void*)j2k_build_decoder);
5944         opj_procedure_list_add_procedure(p_j2k->m_validation_list, (void*)j2k_decoding_validation);
5945         /* DEVELOPER CORNER, add your custom validation procedure */
5946
5947 }
5948
5949 /**
5950  * Builds the cp decoder parameters to use to decode tile.
5951  */
5952 opj_bool j2k_build_decoder (
5953                                                 opj_j2k_v2_t * p_j2k,
5954                                                 opj_stream_private_t *p_stream,
5955                                                 opj_event_mgr_t * p_manager
5956                                                 )
5957 {
5958         // add here initialization of cp
5959         // copy paste of setup_decoder
5960         return OPJ_TRUE;
5961 }
5962
5963 /**
5964  * The default decoding validation procedure without any extension.
5965  *
5966  * @param       p_j2k                   the jpeg2000 codec to validate.
5967  * @param       p_stream                                the input stream to validate.
5968  * @param       p_manager               the user event manager.
5969  *
5970  * @return true if the parameters are correct.
5971  */
5972 opj_bool j2k_decoding_validation (
5973                                                                 opj_j2k_v2_t *p_j2k,
5974                                                                 opj_stream_private_t *p_stream,
5975                                                                 opj_event_mgr_t * p_manager
5976                                                           )
5977 {
5978         opj_bool l_is_valid = OPJ_TRUE;
5979
5980         // preconditions
5981         assert(p_j2k != 00);
5982         assert(p_stream != 00);
5983         assert(p_manager != 00);
5984
5985
5986         /* STATE checking */
5987         /* make sure the state is at 0 */
5988 #ifdef TODO_MSD
5989         l_is_valid &= (p_j2k->m_specific_param.m_decoder.m_state == J2K_DEC_STATE_NONE);
5990 #endif
5991         l_is_valid &= (p_j2k->m_specific_param.m_decoder.m_state == 0x0000);
5992
5993         /* POINTER validation */
5994         /* make sure a p_j2k codec is present */
5995         /* make sure a procedure list is present */
5996         l_is_valid &= (p_j2k->m_procedure_list != 00);
5997         /* make sure a validation list is present */
5998         l_is_valid &= (p_j2k->m_validation_list != 00);
5999
6000         /* PARAMETER VALIDATION */
6001         return l_is_valid;
6002 }
6003
6004 opj_bool j2k_read_header_procedure(     opj_j2k_v2_t *p_j2k,
6005                                                                         struct opj_stream_private *p_stream,
6006                                                                         struct opj_event_mgr * p_manager)
6007 {
6008         OPJ_UINT32 l_current_marker;
6009         OPJ_UINT32 l_marker_size;
6010         const opj_dec_memory_marker_handler_t * l_marker_handler = 00;
6011
6012         /* preconditions */
6013         assert(p_stream != 00);
6014         assert(p_j2k != 00);
6015         assert(p_manager != 00);
6016
6017         /*  We enter in the main header */
6018         p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_MHSOC;
6019
6020         /* Try to read the SOC marker, the codestream must begin with SOC marker */
6021         if (! j2k_read_soc_v2(p_j2k,p_stream,p_manager)) {
6022                 opj_event_msg_v2(p_manager, EVT_ERROR, "Expected a SOC marker \n");
6023                 return OPJ_FALSE;
6024         }
6025
6026         /* Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer */
6027         if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
6028                 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
6029                 return OPJ_FALSE;
6030         }
6031
6032         /* Read 2 bytes as the new marker ID */
6033         opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_current_marker,2);
6034
6035         /* Try to read until the SOT is detected */
6036         while (l_current_marker != J2K_MS_SOT) {
6037
6038                 /* Check if the current marker ID is valid */
6039                 if (l_current_marker < 0xff00) {
6040                         opj_event_msg_v2(p_manager, EVT_ERROR, "We expected read a marker ID (0xff--) instead of %.8x\n", l_current_marker);
6041                         return OPJ_FALSE;
6042                 }
6043
6044                 /* Get the marker handler from the marker ID */
6045                 l_marker_handler = j2k_get_marker_handler(l_current_marker);
6046
6047                 /* Manage case where marker is unknown */
6048                 if (l_marker_handler->id == J2K_MS_UNK) {
6049                         if (! j2k_read_unk_v2(p_j2k, p_stream, &l_current_marker, p_manager)){
6050                                 opj_event_msg_v2(p_manager, EVT_ERROR, "Unknow marker have been detected and generated error.\n");
6051                                 return OPJ_FALSE;
6052                         }
6053
6054                         if (l_current_marker == J2K_MS_SOT)
6055                                 break; /* SOT marker is detected main header is completely read */
6056                         else    /* Get the marker handler from the marker ID */
6057                                 l_marker_handler = j2k_get_marker_handler(l_current_marker);
6058                 }
6059
6060                 /* Check if the marker is known and if it is the right place to find it */
6061                 if (! (p_j2k->m_specific_param.m_decoder.m_state & l_marker_handler->states) ) {
6062                         opj_event_msg_v2(p_manager, EVT_ERROR, "Marker is not compliant with its position\n");
6063                         return OPJ_FALSE;
6064                 }
6065
6066                 /* Try to read 2 bytes (the marker size) from stream and copy them into the buffer */
6067                 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
6068                         opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
6069                         return OPJ_FALSE;
6070                 }
6071
6072                 /* read 2 bytes as the marker size */
6073                 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_marker_size,2);
6074                 l_marker_size -= 2; // Subtract the size of the marker ID already read
6075
6076                 /* Check if the marker size is compatible with the header data size */
6077                 if (l_marker_size > p_j2k->m_specific_param.m_decoder.m_header_data_size) {
6078                         p_j2k->m_specific_param.m_decoder.m_header_data = (OPJ_BYTE*)
6079                                         opj_realloc(p_j2k->m_specific_param.m_decoder.m_header_data,l_marker_size);
6080                         if (p_j2k->m_specific_param.m_decoder.m_header_data == 00) {
6081                                 return OPJ_FALSE;
6082                         }
6083                         p_j2k->m_specific_param.m_decoder.m_header_data_size = l_marker_size;
6084                 }
6085
6086                 /* Try to read the rest of the marker segment from stream and copy them into the buffer */
6087                 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,l_marker_size,p_manager) != l_marker_size) {
6088                         opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
6089                         return OPJ_FALSE;
6090                 }
6091
6092                 /* Read the marker segment with the correct marker handler */
6093                 if (! (*(l_marker_handler->handler))(p_j2k,p_j2k->m_specific_param.m_decoder.m_header_data,l_marker_size,p_manager)) {
6094                         opj_event_msg_v2(p_manager, EVT_ERROR, "Marker handler function failed to read the marker segment\n");
6095                         return OPJ_FALSE;
6096                 }
6097
6098                 /* Add the marker to the codestream index*/
6099                 j2k_add_mhmarker_v2(p_j2k->cstr_index,
6100                                                         l_marker_handler->id,
6101                                                         (OPJ_UINT32) opj_stream_tell(p_stream) - l_marker_size - 4,
6102                                                         l_marker_size + 4 );
6103
6104                 /* Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer */
6105                 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
6106                         opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
6107                         return OPJ_FALSE;
6108                 }
6109
6110                 /* read 2 bytes as the new marker ID */
6111                 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_current_marker,2);
6112         }
6113
6114         opj_event_msg_v2(p_manager, EVT_INFO, "Main header has been correctly decode.\n");
6115
6116         /* Position of the last element if the main header */
6117         p_j2k->cstr_index->main_head_end = (OPJ_UINT32) opj_stream_tell(p_stream) - 2;
6118
6119         /* Next step: read a tile-part header */
6120         p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_TPHSOT;
6121
6122         return OPJ_TRUE;
6123 }
6124
6125 /**
6126  * Excutes the given procedures on the given codec.
6127  *
6128  * @param       p_procedure_list        the list of procedures to execute
6129  * @param       p_j2k                                   the jpeg2000 codec to execute the procedures on.
6130  * @param       p_stream                                        the stream to execute the procedures on.
6131  * @param       p_manager                       the user manager.
6132  *
6133  * @return      true                            if all the procedures were successfully executed.
6134  */
6135 opj_bool j2k_exec (     opj_j2k_v2_t * p_j2k,
6136                                         opj_procedure_list_t * p_procedure_list,
6137                                         opj_stream_private_t *p_stream,
6138                                         opj_event_mgr_t * p_manager )
6139 {
6140         opj_bool (** l_procedure) (opj_j2k_v2_t * ,opj_stream_private_t *,opj_event_mgr_t *) = 00;
6141         opj_bool l_result = OPJ_TRUE;
6142         OPJ_UINT32 l_nb_proc, i;
6143
6144         // preconditions
6145         assert(p_procedure_list != 00);
6146         assert(p_j2k != 00);
6147         assert(p_stream != 00);
6148         assert(p_manager != 00);
6149
6150
6151         l_nb_proc = opj_procedure_list_get_nb_procedures(p_procedure_list);
6152         l_procedure = (opj_bool (**) (opj_j2k_v2_t * ,opj_stream_private_t *,opj_event_mgr_t *)) opj_procedure_list_get_first_procedure(p_procedure_list);
6153
6154         for     (i=0;i<l_nb_proc;++i) {
6155                 l_result = l_result && ((*l_procedure) (p_j2k,p_stream,p_manager));
6156                 ++l_procedure;
6157         }
6158
6159         // and clear the procedure list at the end.
6160         opj_procedure_list_clear(p_procedure_list);
6161         return l_result;
6162 }
6163
6164 /* FIXME DOC*/
6165 opj_bool j2k_copy_default_tcp_and_create_tcd
6166                                                 (
6167                                                 opj_j2k_v2_t * p_j2k,
6168                                                 opj_stream_private_t *p_stream,
6169                                                 opj_event_mgr_t * p_manager
6170                                                 )
6171 {
6172         opj_tcp_v2_t * l_tcp = 00;
6173         opj_tcp_v2_t * l_default_tcp = 00;
6174         OPJ_UINT32 l_nb_tiles;
6175         OPJ_UINT32 i,j;
6176         opj_tccp_t *l_current_tccp = 00;
6177         OPJ_UINT32 l_tccp_size;
6178         OPJ_UINT32 l_mct_size;
6179         opj_image_t * l_image;
6180         OPJ_UINT32 l_mcc_records_size,l_mct_records_size;
6181         opj_mct_data_t * l_src_mct_rec, *l_dest_mct_rec;
6182         opj_simple_mcc_decorrelation_data_t * l_src_mcc_rec, *l_dest_mcc_rec;
6183         OPJ_UINT32 l_offset;
6184
6185         /* preconditions */
6186         assert(p_j2k != 00);
6187         assert(p_stream != 00);
6188         assert(p_manager != 00);
6189
6190         l_image = p_j2k->m_private_image;
6191         l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw;
6192         l_tcp = p_j2k->m_cp.tcps;
6193         l_tccp_size = l_image->numcomps * sizeof(opj_tccp_t);
6194         l_default_tcp = p_j2k->m_specific_param.m_decoder.m_default_tcp;
6195         l_mct_size = l_image->numcomps * l_image->numcomps * sizeof(OPJ_FLOAT32);
6196
6197         /* For each tile */
6198         for (i=0; i<l_nb_tiles; ++i) {
6199                 /* keep the tile-compo coding parameters pointer of the current tile coding parameters*/
6200                 l_current_tccp = l_tcp->tccps;
6201                 /*Copy default coding parameters into the current tile coding parameters*/
6202                 memcpy(l_tcp, l_default_tcp, sizeof(opj_tcp_v2_t));
6203                 /* Initialize some values of the current tile coding parameters*/
6204                 l_tcp->ppt = 0;
6205                 l_tcp->ppt_data = 00;
6206                 /* Reconnect the tile-compo coding parameters pointer to the current tile coding parameters*/
6207                 l_tcp->tccps = l_current_tccp;
6208
6209                 /* Get the mct_decoding_matrix of the dflt_tile_cp and copy them into the current tile cp*/
6210                 if (l_default_tcp->m_mct_decoding_matrix) {
6211                         l_tcp->m_mct_decoding_matrix = (OPJ_FLOAT32*)opj_malloc(l_mct_size);
6212                         if (! l_tcp->m_mct_decoding_matrix ) {
6213                                 return OPJ_FALSE;
6214                         }
6215                         memcpy(l_tcp->m_mct_decoding_matrix,l_default_tcp->m_mct_decoding_matrix,l_mct_size);
6216                 }
6217
6218                 /* Get the mct_record of the dflt_tile_cp and copy them into the current tile cp*/
6219                 l_mct_records_size = l_default_tcp->m_nb_max_mct_records * sizeof(opj_mct_data_t);
6220                 l_tcp->m_mct_records = (opj_mct_data_t*)opj_malloc(l_mct_records_size);
6221                 if (! l_tcp->m_mct_records) {
6222                         return OPJ_FALSE;
6223                 }
6224                 memcpy(l_tcp->m_mct_records, l_default_tcp->m_mct_records,l_mct_records_size);
6225
6226                 /* Copy the mct record data from dflt_tile_cp to the current tile*/
6227                 l_src_mct_rec = l_default_tcp->m_mct_records;
6228                 l_dest_mct_rec = l_tcp->m_mct_records;
6229
6230                 for (j=0;j<l_default_tcp->m_nb_mct_records;++j) {
6231
6232                         if (l_src_mct_rec->m_data) {
6233
6234                                 l_dest_mct_rec->m_data = (OPJ_BYTE*) opj_malloc(l_src_mct_rec->m_data_size);
6235                                 if(! l_dest_mct_rec->m_data) {
6236                                         return OPJ_FALSE;
6237                                 }
6238                                 memcpy(l_dest_mct_rec->m_data,l_src_mct_rec->m_data,l_src_mct_rec->m_data_size);
6239                         }
6240
6241                         ++l_src_mct_rec;
6242                         ++l_dest_mct_rec;
6243                 }
6244
6245                 /* Get the mcc_record of the dflt_tile_cp and copy them into the current tile cp*/
6246                 l_mcc_records_size = l_default_tcp->m_nb_max_mcc_records * sizeof(opj_simple_mcc_decorrelation_data_t);
6247                 l_tcp->m_mcc_records = (opj_simple_mcc_decorrelation_data_t*) opj_malloc(l_mcc_records_size);
6248                 if (! l_tcp->m_mcc_records) {
6249                         return OPJ_FALSE;
6250                 }
6251                 memcpy(l_tcp->m_mcc_records,l_default_tcp->m_mcc_records,l_mcc_records_size);
6252
6253                 /* Copy the mcc record data from dflt_tile_cp to the current tile*/
6254                 l_src_mcc_rec = l_default_tcp->m_mcc_records;
6255                 l_dest_mcc_rec = l_tcp->m_mcc_records;
6256
6257                 for (j=0;j<l_default_tcp->m_nb_max_mcc_records;++j) {
6258
6259                         if (l_src_mcc_rec->m_decorrelation_array) {
6260                                 l_offset = l_src_mcc_rec->m_decorrelation_array - l_default_tcp->m_mct_records;
6261                                 l_dest_mcc_rec->m_decorrelation_array = l_tcp->m_mct_records + l_offset;
6262                         }
6263
6264                         if (l_src_mcc_rec->m_offset_array) {
6265                                 l_offset = l_src_mcc_rec->m_offset_array - l_default_tcp->m_mct_records;
6266                                 l_dest_mcc_rec->m_offset_array = l_tcp->m_mct_records + l_offset;
6267                         }
6268
6269                         ++l_src_mcc_rec;
6270                         ++l_dest_mcc_rec;
6271                 }
6272
6273                 /* Copy all the dflt_tile_compo_cp to the current tile cp */
6274                 memcpy(l_current_tccp,l_default_tcp->tccps,l_tccp_size);
6275
6276                 /* Move to next tile cp*/
6277                 ++l_tcp;
6278         }
6279
6280         /* Create the current tile decoder*/
6281         p_j2k->m_tcd = (opj_tcd_v2_t*)tcd_create_v2(OPJ_TRUE); // FIXME why a cast ?
6282         if (! p_j2k->m_tcd ) {
6283                 return OPJ_FALSE;
6284         }
6285
6286         if ( !tcd_init_v2(p_j2k->m_tcd, l_image, &(p_j2k->m_cp)) ) {
6287                 tcd_destroy_v2(p_j2k->m_tcd);
6288                 p_j2k->m_tcd = 00;
6289                 opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot decode tile, memory error\n");
6290                 return OPJ_FALSE;
6291         }
6292
6293         return OPJ_TRUE;
6294 }
6295
6296 /**
6297  * Reads the lookup table containing all the marker, status and action, and returns the handler associated
6298  * with the marker value.
6299  * @param       p_id            Marker value to look up
6300  *
6301  * @return      the handler associated with the id.
6302 */
6303 const opj_dec_memory_marker_handler_t * j2k_get_marker_handler (const OPJ_UINT32 p_id)
6304 {
6305         const opj_dec_memory_marker_handler_t *e;
6306         for (e = j2k_memory_marker_handler_tab; e->id != 0; ++e) {
6307                 if (e->id == p_id) {
6308                         break; // we find a handler corresponding to the marker ID
6309                 }
6310         }
6311         return e;
6312 }
6313
6314
6315 /**
6316  * Destroys a jpeg2000 codec.
6317  *
6318  * @param       p_j2k   the jpeg20000 structure to destroy.
6319  */
6320 void j2k_destroy (opj_j2k_v2_t *p_j2k)
6321 {
6322         if (p_j2k == 00) {
6323                 return;
6324         }
6325
6326         if (p_j2k->m_is_decoder) {
6327
6328                 if (p_j2k->m_specific_param.m_decoder.m_default_tcp != 00) {
6329                         j2k_tcp_destroy(p_j2k->m_specific_param.m_decoder.m_default_tcp);
6330                         opj_free(p_j2k->m_specific_param.m_decoder.m_default_tcp);
6331                         p_j2k->m_specific_param.m_decoder.m_default_tcp = 00;
6332                 }
6333
6334                 if (p_j2k->m_specific_param.m_decoder.m_header_data != 00) {
6335                         opj_free(p_j2k->m_specific_param.m_decoder.m_header_data);
6336                         p_j2k->m_specific_param.m_decoder.m_header_data = 00;
6337                         p_j2k->m_specific_param.m_decoder.m_header_data_size = 0;
6338                 }
6339         }
6340         else {
6341
6342                 if (p_j2k->m_specific_param.m_encoder.m_encoded_tile_data) {
6343                         opj_free(p_j2k->m_specific_param.m_encoder.m_encoded_tile_data);
6344                         p_j2k->m_specific_param.m_encoder.m_encoded_tile_data = 00;
6345                 }
6346
6347                 if (p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer) {
6348                         opj_free(p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer);
6349                         p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer = 00;
6350                         p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current = 00;
6351                 }
6352
6353                 if (p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
6354                         opj_free(p_j2k->m_specific_param.m_encoder.m_header_tile_data);
6355                         p_j2k->m_specific_param.m_encoder.m_header_tile_data = 00;
6356                         p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = 0;
6357                 }
6358         }
6359
6360         tcd_destroy_v2(p_j2k->m_tcd);
6361
6362         j2k_cp_destroy(&(p_j2k->m_cp));
6363         memset(&(p_j2k->m_cp),0,sizeof(opj_cp_v2_t));
6364
6365         opj_procedure_list_destroy(p_j2k->m_procedure_list);
6366         p_j2k->m_procedure_list = 00;
6367
6368         opj_procedure_list_destroy(p_j2k->m_validation_list);
6369         p_j2k->m_procedure_list = 00;
6370
6371         j2k_destroy_cstr_index(p_j2k->cstr_index);
6372         p_j2k->cstr_index = NULL;
6373
6374         opj_image_destroy(p_j2k->m_private_image);
6375         p_j2k->m_private_image = NULL;
6376
6377         opj_image_destroy(p_j2k->m_output_image);
6378         p_j2k->m_output_image = NULL;
6379
6380         opj_free(p_j2k);
6381 }
6382
6383 void j2k_destroy_cstr_index (opj_codestream_index_t *p_cstr_ind)
6384 {
6385         if (p_cstr_ind) {
6386
6387                 if (p_cstr_ind->marker) {
6388                         opj_free(p_cstr_ind->marker);
6389                         p_cstr_ind->marker = NULL;
6390                 }
6391
6392                 if (p_cstr_ind->tile_index) {
6393                         OPJ_UINT32 it_tile = 0;
6394
6395                         for (it_tile=0; it_tile < p_cstr_ind->nb_of_tiles; it_tile++) {
6396
6397                                 if(p_cstr_ind->tile_index[it_tile].packet_index) {
6398                                         opj_free(p_cstr_ind->tile_index[it_tile].packet_index);
6399                                         p_cstr_ind->tile_index[it_tile].packet_index = NULL;
6400                                 }
6401
6402                                 if(p_cstr_ind->tile_index[it_tile].tp_index){
6403                                         opj_free(p_cstr_ind->tile_index[it_tile].tp_index);
6404                                         p_cstr_ind->tile_index[it_tile].tp_index = NULL;
6405                                 }
6406
6407                                 if(p_cstr_ind->tile_index[it_tile].marker){
6408                                         opj_free(p_cstr_ind->tile_index[it_tile].marker);
6409                                         p_cstr_ind->tile_index[it_tile].marker = NULL;
6410
6411                                 }
6412                         }
6413
6414                         opj_free( p_cstr_ind->tile_index);
6415                         p_cstr_ind->tile_index = NULL;
6416                 }
6417
6418                 opj_free(p_cstr_ind);
6419         }
6420 }
6421
6422
6423
6424 /**
6425  * Destroys a tile coding parameter structure.
6426  *
6427  * @param       p_tcp           the tile coding parameter to destroy.
6428  */
6429 void j2k_tcp_destroy (opj_tcp_v2_t *p_tcp)
6430 {
6431         if (p_tcp == 00) {
6432                 return;
6433         }
6434
6435         if (p_tcp->ppt_buffer != 00) {
6436                 opj_free(p_tcp->ppt_buffer);
6437                 p_tcp->ppt_buffer = 00;
6438         }
6439
6440         if (p_tcp->tccps != 00) {
6441                 opj_free(p_tcp->tccps);
6442                 p_tcp->tccps = 00;
6443         }
6444
6445         if (p_tcp->m_mct_coding_matrix != 00) {
6446                 opj_free(p_tcp->m_mct_coding_matrix);
6447                 p_tcp->m_mct_coding_matrix = 00;
6448         }
6449
6450         if (p_tcp->m_mct_decoding_matrix != 00) {
6451                 opj_free(p_tcp->m_mct_decoding_matrix);
6452                 p_tcp->m_mct_decoding_matrix = 00;
6453         }
6454
6455         if (p_tcp->m_mcc_records) {
6456                 opj_free(p_tcp->m_mcc_records);
6457                 p_tcp->m_mcc_records = 00;
6458                 p_tcp->m_nb_max_mcc_records = 0;
6459                 p_tcp->m_nb_mcc_records = 0;
6460         }
6461
6462         if (p_tcp->m_mct_records) {
6463                 opj_mct_data_t * l_mct_data = p_tcp->m_mct_records;
6464                 OPJ_UINT32 i;
6465
6466                 for (i=0;i<p_tcp->m_nb_mct_records;++i) {
6467                         if (l_mct_data->m_data) {
6468                                 opj_free(l_mct_data->m_data);
6469                                 l_mct_data->m_data = 00;
6470                         }
6471
6472                         ++l_mct_data;
6473                 }
6474
6475                 opj_free(p_tcp->m_mct_records);
6476                 p_tcp->m_mct_records = 00;
6477         }
6478
6479         if (p_tcp->mct_norms != 00) {
6480                 opj_free(p_tcp->mct_norms);
6481                 p_tcp->mct_norms = 00;
6482         }
6483
6484         if (p_tcp->m_data) {
6485                 opj_free(p_tcp->m_data);
6486                 p_tcp->m_data = 00;
6487         }
6488 }
6489
6490
6491 /**
6492  * Destroys a coding parameter structure.
6493  *
6494  * @param       p_cp            the coding parameter to destroy.
6495  */
6496 void j2k_cp_destroy (opj_cp_v2_t *p_cp)
6497 {
6498         OPJ_UINT32 l_nb_tiles;
6499         opj_tcp_v2_t * l_current_tile = 00;
6500         OPJ_UINT32 i;
6501
6502         if
6503                 (p_cp == 00)
6504         {
6505                 return;
6506         }
6507         if
6508                 (p_cp->tcps != 00)
6509         {
6510                 l_current_tile = p_cp->tcps;
6511                 l_nb_tiles = p_cp->th * p_cp->tw;
6512
6513                 for
6514                         (i = 0; i < l_nb_tiles; ++i)
6515                 {
6516                         j2k_tcp_destroy(l_current_tile);
6517                         ++l_current_tile;
6518                 }
6519                 opj_free(p_cp->tcps);
6520                 p_cp->tcps = 00;
6521         }
6522         if
6523                 (p_cp->ppm_buffer != 00)
6524         {
6525                 opj_free(p_cp->ppm_buffer);
6526                 p_cp->ppm_buffer = 00;
6527         }
6528         if
6529                 (p_cp->comment != 00)
6530         {
6531                 opj_free(p_cp->comment);
6532                 p_cp->comment = 00;
6533         }
6534         if
6535                 (! p_cp->m_is_decoder)
6536         {
6537                 if
6538                         (p_cp->m_specific_param.m_enc.m_matrice)
6539                 {
6540                         opj_free(p_cp->m_specific_param.m_enc.m_matrice);
6541                         p_cp->m_specific_param.m_enc.m_matrice = 00;
6542                 }
6543         }
6544 }
6545
6546
6547
6548 /**
6549  * Reads a tile header.
6550  * @param       p_j2k           the jpeg2000 codec.
6551  * @param       p_stream                        the stream to write data to.
6552  * @param       p_manager       the user event manager.
6553  */
6554 opj_bool j2k_read_tile_header(  opj_j2k_v2_t * p_j2k,
6555                                                                 OPJ_UINT32 * p_tile_index,
6556                                                                 OPJ_UINT32 * p_data_size,
6557                                                                 OPJ_INT32 * p_tile_x0, OPJ_INT32 * p_tile_y0,
6558                                                                 OPJ_INT32 * p_tile_x1, OPJ_INT32 * p_tile_y1,
6559                                                                 OPJ_UINT32 * p_nb_comps,
6560                                                                 opj_bool * p_go_on,
6561                                                                 opj_stream_private_t *p_stream,
6562                                                                 opj_event_mgr_t * p_manager )
6563 {
6564         OPJ_UINT32 l_current_marker = J2K_MS_SOT;
6565         OPJ_UINT32 l_marker_size;
6566         const opj_dec_memory_marker_handler_t * l_marker_handler = 00;
6567         opj_tcp_v2_t * l_tcp = NULL;
6568         OPJ_UINT32 l_nb_tiles;
6569
6570         /* preconditions */
6571         assert(p_stream != 00);
6572         assert(p_j2k != 00);
6573         assert(p_manager != 00);
6574
6575         /* Reach the End Of Codestream ?*/
6576         if (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_EOC){
6577                 l_current_marker = J2K_MS_EOC;
6578         }
6579         /* We need to encounter a SOT marker (a new tile-part header) */
6580         else if (p_j2k->m_specific_param.m_decoder.m_state != J2K_STATE_TPHSOT){
6581                 return OPJ_FALSE;
6582         }
6583
6584         /* Read into the codestream until reach the EOC or ! can_decode ??? FIXME */
6585         while ( (!p_j2k->m_specific_param.m_decoder.m_can_decode) && (l_current_marker != J2K_MS_EOC) ) {
6586
6587                 /* Try to read until the Start Of Data is detected */
6588                 while (l_current_marker != J2K_MS_SOD) {
6589
6590                         /* Try to read 2 bytes (the marker size) from stream and copy them into the buffer */
6591                         if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
6592                                 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
6593                                 return OPJ_FALSE;
6594                         }
6595
6596                         /* Read 2 bytes from the buffer as the marker size */
6597                         opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_marker_size,2);
6598
6599                         /* Why this condition? FIXME */
6600                         if (p_j2k->m_specific_param.m_decoder.m_state & J2K_STATE_TPH){
6601                                 p_j2k->m_specific_param.m_decoder.m_sot_length -= (l_marker_size + 2);
6602                         }
6603                         l_marker_size -= 2; /* Subtract the size of the marker ID already read */
6604
6605                         /* Get the marker handler from the marker ID */
6606                         l_marker_handler = j2k_get_marker_handler(l_current_marker);
6607
6608                         /* Check if the marker is known and if it is the right place to find it */
6609                         if (! (p_j2k->m_specific_param.m_decoder.m_state & l_marker_handler->states) ) {
6610                                 opj_event_msg_v2(p_manager, EVT_ERROR, "Marker is not compliant with its position\n");
6611                                 return OPJ_FALSE;
6612                         }
6613 /* FIXME manage case of unknown marker as in the main header ? */
6614
6615                         /* Check if the marker size is compatible with the header data size */
6616                         if (l_marker_size > p_j2k->m_specific_param.m_decoder.m_header_data_size) {
6617                                 p_j2k->m_specific_param.m_decoder.m_header_data = (OPJ_BYTE*)
6618                                         opj_realloc(p_j2k->m_specific_param.m_decoder.m_header_data,l_marker_size);
6619                                 if (p_j2k->m_specific_param.m_decoder.m_header_data == 00) {
6620                                         return OPJ_FALSE;
6621                                 }
6622                                 p_j2k->m_specific_param.m_decoder.m_header_data_size = l_marker_size;
6623                         }
6624
6625                         /* Try to read the rest of the marker segment from stream and copy them into the buffer */
6626                         if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,l_marker_size,p_manager) != l_marker_size) {
6627                                 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
6628                                 return OPJ_FALSE;
6629                         }
6630
6631                         /* Read the marker segment with the correct marker handler */
6632                         if (! (*(l_marker_handler->handler))(p_j2k,p_j2k->m_specific_param.m_decoder.m_header_data,l_marker_size,p_manager)) {
6633                                 opj_event_msg_v2(p_manager, EVT_ERROR, "Fail to read the current marker segment (%#x)\n", l_current_marker);
6634                                 return OPJ_FALSE;
6635                         }
6636
6637 /*                      if (l_current_marker == J2K_MS_SOT)
6638                                 j2k_add_tlmarker();*/
6639
6640                         /* Add the marker to the codestream index*/
6641                         j2k_add_tlmarker_v2(p_j2k->m_current_tile_number,
6642                                                                 p_j2k->cstr_index,
6643                                                                 l_marker_handler->id,
6644                                                                 (OPJ_UINT32) opj_stream_tell(p_stream) - l_marker_size - 4,
6645                                                                 l_marker_size + 4 );
6646
6647                         if (p_j2k->m_specific_param.m_decoder.m_skip_data) {
6648                                 /* Skip the rest of the tile part header*/
6649                                 if (opj_stream_skip(p_stream,p_j2k->m_specific_param.m_decoder.m_sot_length,p_manager) != p_j2k->m_specific_param.m_decoder.m_sot_length) {
6650                                         opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
6651                                         return OPJ_FALSE;
6652                                 }
6653                                 l_current_marker = J2K_MS_SOD; /* Normally we reached a SOD */
6654                         }
6655                         else {
6656                                 /* Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer*/
6657                                 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
6658                                         opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
6659                                         return OPJ_FALSE;
6660                                 }
6661                                 /* Read 2 bytes from the buffer as the new marker ID */
6662                                 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_current_marker,2);
6663                         }
6664                 }
6665
6666                 /* If we didn't skip data before, we need to read the SOD marker*/
6667                 if (! p_j2k->m_specific_param.m_decoder.m_skip_data) {
6668                         /* Try to read the SOD marker and skip data ? FIXME */
6669                         if (! j2k_read_sod_v2(p_j2k, p_stream, p_manager)) {
6670                                 return OPJ_FALSE;
6671                         }
6672
6673
6674
6675                         if (! p_j2k->m_specific_param.m_decoder.m_can_decode){
6676                                 /* Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer */
6677                                 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
6678                                         opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
6679                                         return OPJ_FALSE;
6680                                 }
6681
6682                                 /* Read 2 bytes from buffer as the new marker ID */
6683                                 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_current_marker,2);
6684                         }
6685                 }
6686                 else {
6687                         /* Indicate we will try to read a new tile-part header*/
6688                         p_j2k->m_specific_param.m_decoder.m_skip_data = 0;
6689                         p_j2k->m_specific_param.m_decoder.m_can_decode = 0;
6690                         p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_TPHSOT;
6691
6692                         /* Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer */
6693                         if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
6694                                 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
6695                                 return OPJ_FALSE;
6696                         }
6697
6698                         /* Read 2 bytes from buffer as the new marker ID */
6699                         opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_current_marker,2);
6700                 }
6701         }
6702
6703         /* Current marker is the EOC marker ?*/
6704         if (l_current_marker == J2K_MS_EOC) {
6705                 if (p_j2k->m_specific_param.m_decoder.m_state != J2K_STATE_EOC ){
6706                         p_j2k->m_current_tile_number = 0;
6707                         p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_EOC;
6708                 }
6709         }
6710
6711         /* FIXME DOC ???*/
6712         if ( ! p_j2k->m_specific_param.m_decoder.m_can_decode) {
6713                 l_tcp = p_j2k->m_cp.tcps + p_j2k->m_current_tile_number;
6714                 l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw;
6715
6716                 while( (p_j2k->m_current_tile_number < l_nb_tiles) && (l_tcp->m_data == 00) ) {
6717                         ++p_j2k->m_current_tile_number;
6718                         ++l_tcp;
6719                 }
6720
6721                 if (p_j2k->m_current_tile_number == l_nb_tiles) {
6722                         *p_go_on = OPJ_FALSE;
6723                         return OPJ_TRUE;
6724                 }
6725         }
6726
6727         /*FIXME ???*/
6728         if (! tcd_init_decode_tile(p_j2k->m_tcd, p_j2k->m_current_tile_number)) {
6729                 opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot decode tile, memory error\n");
6730                 return OPJ_FALSE;
6731         }
6732
6733         opj_event_msg_v2(p_manager, EVT_INFO, "Header of tile %d / %d has been read.\n",
6734                         p_j2k->m_current_tile_number +1, p_j2k->m_cp.th * p_j2k->m_cp.tw);
6735
6736         *p_tile_index = p_j2k->m_current_tile_number;
6737         *p_go_on = OPJ_TRUE;
6738         *p_data_size = tcd_get_decoded_tile_size(p_j2k->m_tcd);
6739         *p_tile_x0 = p_j2k->m_tcd->tcd_image->tiles->x0;
6740         *p_tile_y0 = p_j2k->m_tcd->tcd_image->tiles->y0;
6741         *p_tile_x1 = p_j2k->m_tcd->tcd_image->tiles->x1;
6742         *p_tile_y1 = p_j2k->m_tcd->tcd_image->tiles->y1;
6743         *p_nb_comps = p_j2k->m_tcd->tcd_image->tiles->numcomps;
6744
6745          p_j2k->m_specific_param.m_decoder.m_state |= 0x0080;// FIXME J2K_DEC_STATE_DATA;
6746
6747         return OPJ_TRUE;
6748 }
6749
6750
6751 opj_bool j2k_decode_tile (      opj_j2k_v2_t * p_j2k,
6752                                                         OPJ_UINT32 p_tile_index,
6753                                                         OPJ_BYTE * p_data,
6754                                                         OPJ_UINT32 p_data_size,
6755                                                         opj_stream_private_t *p_stream,
6756                                                         opj_event_mgr_t * p_manager )
6757 {
6758         OPJ_UINT32 l_current_marker;
6759         OPJ_BYTE l_data [2];
6760         opj_tcp_v2_t * l_tcp;
6761
6762         /* preconditions */
6763         assert(p_stream != 00);
6764         assert(p_j2k != 00);
6765         assert(p_manager != 00);
6766
6767         if ( !(p_j2k->m_specific_param.m_decoder.m_state & 0x0080/*FIXME J2K_DEC_STATE_DATA*/)
6768                 || (p_tile_index != p_j2k->m_current_tile_number) ) {
6769                 return OPJ_FALSE;
6770         }
6771
6772         l_tcp = &(p_j2k->m_cp.tcps[p_tile_index]);
6773         if (! l_tcp->m_data) {
6774                 j2k_tcp_destroy(&(p_j2k->m_cp.tcps[p_tile_index]));
6775                 return OPJ_FALSE;
6776         }
6777
6778         if (! tcd_decode_tile_v2(       p_j2k->m_tcd,
6779                                                                 l_tcp->m_data,
6780                                                                 l_tcp->m_data_size,
6781                                                                 p_tile_index,
6782                                                                 p_j2k->cstr_index) ) {
6783                 j2k_tcp_destroy(l_tcp);
6784                 p_j2k->m_specific_param.m_decoder.m_state |= 0x8000;//FIXME J2K_DEC_STATE_ERR;
6785                 return OPJ_FALSE;
6786         }
6787
6788         if (! tcd_update_tile_data(p_j2k->m_tcd,p_data,p_data_size)) {
6789                 return OPJ_FALSE;
6790         }
6791
6792         j2k_tcp_destroy(l_tcp);
6793         p_j2k->m_tcd->tcp = 0;
6794
6795         p_j2k->m_specific_param.m_decoder.m_can_decode = 0;
6796         p_j2k->m_specific_param.m_decoder.m_state &= (~ (0x0080));// FIXME J2K_DEC_STATE_DATA);
6797
6798         if (p_j2k->m_specific_param.m_decoder.m_state != 0x0100){ //FIXME J2K_DEC_STATE_EOC)
6799                 if (opj_stream_read_data(p_stream,l_data,2,p_manager) != 2) {
6800                         opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
6801                         return OPJ_FALSE;
6802                 }
6803
6804                 opj_read_bytes(l_data,&l_current_marker,2);
6805
6806                 if (l_current_marker == J2K_MS_EOC) {
6807                         p_j2k->m_current_tile_number = 0;
6808                         p_j2k->m_specific_param.m_decoder.m_state =  0x0100;//FIXME J2K_DEC_STATE_EOC;
6809                 }
6810                 else if (l_current_marker != J2K_MS_SOT)
6811                 {
6812                         opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short, expected SOT\n");
6813                         return OPJ_FALSE;
6814                 }
6815         }
6816
6817         return OPJ_TRUE;
6818 }
6819
6820
6821 opj_bool j2k_update_image_data (opj_tcd_v2_t * p_tcd, OPJ_BYTE * p_data, opj_image_t* p_output_image)
6822 {
6823         OPJ_UINT32 i,j,k = 0;
6824         OPJ_UINT32 l_width_src,l_height_src;
6825         OPJ_UINT32 l_width_dest,l_height_dest;
6826         OPJ_INT32 l_offset_x0_src, l_offset_y0_src, l_offset_x1_src, l_offset_y1_src;
6827         OPJ_INT32 l_start_offset_src, l_line_offset_src, l_end_offset_src ;
6828         OPJ_UINT32 l_start_x_dest , l_start_y_dest;
6829         OPJ_UINT32 l_x0_dest, l_y0_dest, l_x1_dest, l_y1_dest;
6830         OPJ_INT32 l_start_offset_dest, l_line_offset_dest;
6831
6832         opj_image_comp_t * l_img_comp_src = 00;
6833         opj_image_comp_t * l_img_comp_dest = 00;
6834
6835         opj_tcd_tilecomp_v2_t * l_tilec = 00;
6836         opj_image_t * l_image_src = 00;
6837         OPJ_UINT32 l_size_comp, l_remaining;
6838         OPJ_INT32 * l_dest_ptr;
6839         opj_tcd_resolution_v2_t* l_res= 00;
6840
6841         l_tilec = p_tcd->tcd_image->tiles->comps;
6842         l_image_src = p_tcd->image;
6843         l_img_comp_src = l_image_src->comps;
6844
6845         l_img_comp_dest = p_output_image->comps;
6846
6847         for (i=0; i<l_image_src->numcomps; i++) {
6848
6849                 /* Allocate output component buffer if necessary */
6850                 if (!l_img_comp_dest->data) {
6851
6852                         l_img_comp_dest->data = (OPJ_INT32*) opj_calloc(l_img_comp_dest->w * l_img_comp_dest->h, sizeof(OPJ_INT32));
6853                         if (! l_img_comp_dest->data) {
6854                                 return OPJ_FALSE;
6855                         }
6856                 }
6857
6858                 /* Copy info from decoded comp image to output image */
6859                 l_img_comp_dest->resno_decoded = l_img_comp_src->resno_decoded;
6860
6861                 /*-----*/
6862                 /* Compute the precision of the output buffer */
6863                 l_size_comp = l_img_comp_src->prec >> 3; /*(/ 8)*/
6864                 l_remaining = l_img_comp_src->prec & 7;  /* (%8) */
6865                 l_res = l_tilec->resolutions + l_img_comp_src->resno_decoded;
6866
6867                 if (l_remaining) {
6868                         ++l_size_comp;
6869                 }
6870
6871                 if (l_size_comp == 3) {
6872                         l_size_comp = 4;
6873                 }
6874                 /*-----*/
6875
6876                 /* Current tile component size*/
6877                 l_width_src = (l_res->x1 - l_res->x0);
6878                 l_height_src = (l_res->y1 - l_res->y0);
6879
6880                 /* Border of the current output component*/
6881                 l_x0_dest = int_ceildivpow2(l_img_comp_dest->x0, l_img_comp_dest->factor);
6882                 l_y0_dest = int_ceildivpow2(l_img_comp_dest->y0, l_img_comp_dest->factor);
6883                 l_x1_dest = l_x0_dest + l_img_comp_dest->w;
6884                 l_y1_dest = l_y0_dest + l_img_comp_dest->h;
6885
6886                 /*-----*/
6887                 /* Compute the area (l_offset_x0_src, l_offset_y0_src, l_offset_x1_src, l_offset_y1_src)
6888                  * of the input buffer (decoded tile component) which will be move
6889                  * in the output buffer. Compute the area of the output buffer (l_start_x_dest,
6890                  * l_start_y_dest, l_width_dest, l_height_dest)  which will be modified
6891                  * by this input area.
6892                  * */
6893                 if ( l_x0_dest < l_res->x0 ) {
6894                         l_start_x_dest = l_res->x0 - l_x0_dest;
6895                         l_offset_x0_src = 0;
6896
6897                         if ( l_x1_dest >= l_res->x1 ) {
6898                                 l_width_dest = l_width_src;
6899                                 l_offset_x1_src = 0;
6900                         }
6901                         else {
6902                                 l_width_dest = l_x1_dest - l_res->x0 ;
6903                                 l_offset_x1_src = l_width_src - l_width_dest;
6904                         }
6905                 }
6906                 else {
6907                         l_start_x_dest = 0 ;
6908                         l_offset_x0_src = l_x0_dest - l_res->x0;
6909
6910                         if ( l_x1_dest >= l_res->x1 ) {
6911                                 l_width_dest = l_width_src - l_offset_x0_src;
6912                                 l_offset_x1_src = 0;
6913                         }
6914                         else {
6915                                 l_width_dest = l_img_comp_dest->w ;
6916                                 l_offset_x1_src = l_res->x1 - l_x1_dest;
6917                         }
6918                 }
6919
6920                 if ( l_y0_dest < l_res->y0 ) {
6921                         l_start_y_dest = l_res->y0 - l_y0_dest;
6922                         l_offset_y0_src = 0;
6923
6924                         if ( l_y1_dest >= l_res->y1 ) {
6925                                 l_height_dest = l_height_src;
6926                                 l_offset_y1_src = 0;
6927                         }
6928                         else {
6929                                 l_height_dest = l_y1_dest - l_res->y0 ;
6930                                 l_offset_y1_src =  l_height_src - l_height_dest;
6931                         }
6932                 }
6933                 else {
6934                         l_start_y_dest = 0 ;
6935                         l_offset_y0_src = l_y0_dest - l_res->y0;
6936
6937                         if ( l_y1_dest >= l_res->y1 ) {
6938                                 l_height_dest = l_height_src - l_offset_y0_src;
6939                                 l_offset_y1_src = 0;
6940                         }
6941                         else {
6942                                 l_height_dest = l_img_comp_dest->h ;
6943                                 l_offset_y1_src = l_res->y1 - l_y1_dest;
6944                         }
6945                 }
6946
6947                 if( (l_offset_x0_src < 0 ) || (l_offset_y0_src < 0 ) || (l_offset_x1_src < 0 ) || (l_offset_y1_src < 0 ) ){
6948                         return OPJ_FALSE;
6949                 }
6950                 /*-----*/
6951
6952                 /* Compute the input buffer offset */
6953                 l_start_offset_src = l_offset_x0_src + l_offset_y0_src * l_width_src;
6954                 l_line_offset_src = l_offset_x1_src + l_offset_x0_src;
6955                 l_end_offset_src = l_offset_y1_src * l_width_src - l_offset_x0_src;
6956
6957                 /* Compute the output buffer offset */
6958                 l_start_offset_dest = l_start_x_dest + l_start_y_dest * l_img_comp_dest->w;
6959                 l_line_offset_dest = l_img_comp_dest->w - l_width_dest;
6960
6961                 /* Move the output buffer to the first place where we will write*/
6962                 l_dest_ptr = l_img_comp_dest->data + l_start_offset_dest;
6963
6964                 if (i == 0) {
6965                         fprintf(stdout, "COMPO[%d]:\n",i);
6966                         fprintf(stdout, "SRC: l_start_x_src=%d, l_start_y_src=%d, l_width_src=%d, l_height_src=%d\n"
6967                                         "\t tile offset:%d, %d, %d, %d\n"
6968                                         "\t buffer offset: %d; %d, %d\n",
6969                                         l_res->x0, l_res->y0, l_width_src, l_height_src,
6970                                         l_offset_x0_src, l_offset_y0_src, l_offset_x1_src, l_offset_y1_src,
6971                                         l_start_offset_src, l_line_offset_src, l_end_offset_src);
6972
6973                         fprintf(stdout, "DEST: l_start_x_dest=%d, l_start_y_dest=%d, l_width_dest=%d, l_height_dest=%d\n"
6974                                         "\t start offset: %d, line offset= %d\n",
6975                                         l_start_x_dest, l_start_y_dest, l_width_dest, l_height_dest, l_start_offset_dest, l_line_offset_dest);
6976                 }
6977
6978
6979                 switch (l_size_comp) {
6980                         case 1:
6981                                 {
6982                                         OPJ_CHAR * l_src_ptr = (OPJ_CHAR*) p_data;
6983                                         l_src_ptr += l_start_offset_src; /* Move to the first place where we will read*/
6984
6985                                         if (l_img_comp_src->sgnd) {
6986                                                 for (j = 0 ; j < l_height_dest ; ++j) {
6987                                                         for ( k = 0 ; k < l_width_dest ; ++k) {
6988                                                                 *(l_dest_ptr++) = (OPJ_INT32) (*(l_src_ptr++)); /* Copy only the data needed for the output image */
6989                                                         }
6990
6991                                                         l_dest_ptr+= l_line_offset_dest; /* Move to the next place where we will write */
6992                                                         l_src_ptr += l_line_offset_src ; /* Move to the next place where we will read */
6993                                                 }
6994                                         }
6995                                         else {
6996                                                 for ( j = 0 ; j < l_height_dest ; ++j ) {
6997                                                         for ( k = 0 ; k < l_width_dest ; ++k) {
6998                                                                 *(l_dest_ptr++) = (OPJ_INT32) ((*(l_src_ptr++))&0xff);
6999                                                         }
7000
7001                                                         l_dest_ptr+= l_line_offset_dest;
7002                                                         l_src_ptr += l_line_offset_src;
7003                                                 }
7004                                         }
7005
7006                                         l_src_ptr += l_end_offset_src; /* Move to the end of this component-part of the input buffer */
7007                                         p_data = (OPJ_BYTE*) l_src_ptr; /* Keep the current position for the next component-part */
7008                                 }
7009                                 break;
7010                         case 2:
7011                                 {
7012                                         OPJ_INT16 * l_src_ptr = (OPJ_INT16 *) p_data;
7013                                         l_src_ptr += l_start_offset_src;
7014
7015                                         if (l_img_comp_src->sgnd) {
7016                                                 for (j=0;j<l_height_dest;++j) {
7017                                                         for (k=0;k<l_width_dest;++k) {
7018                                                                 *(l_dest_ptr++) = *(l_src_ptr++);
7019                                                         }
7020
7021                                                         l_dest_ptr+= l_line_offset_dest;
7022                                                         l_src_ptr += l_line_offset_src ;
7023                                                 }
7024                                         }
7025                                         else {
7026                                                 for (j=0;j<l_height_dest;++j) {
7027                                                         for (k=0;k<l_width_dest;++k) {
7028                                                                 *(l_dest_ptr++) = (*(l_src_ptr++))&0xffff;
7029                                                         }
7030
7031                                                         l_dest_ptr+= l_line_offset_dest;
7032                                                         l_src_ptr += l_line_offset_src ;
7033                                                 }
7034                                         }
7035
7036                                         l_src_ptr += l_end_offset_src;
7037                                         p_data = (OPJ_BYTE*) l_src_ptr;
7038                                 }
7039                                 break;
7040                         case 4:
7041                                 {
7042                                         OPJ_INT32 * l_src_ptr = (OPJ_INT32 *) p_data;
7043                                         l_src_ptr += l_start_offset_src;
7044
7045                                         for (j=0;j<l_height_dest;++j) {
7046                                                 for (k=0;k<l_width_dest;++k) {
7047                                                         *(l_dest_ptr++) = (*(l_src_ptr++));
7048                                                 }
7049
7050                                                 l_dest_ptr+= l_line_offset_dest;
7051                                                 l_src_ptr += l_line_offset_src ;
7052                                         }
7053
7054                                         l_src_ptr += l_end_offset_src;
7055                                         p_data = (OPJ_BYTE*) l_src_ptr;
7056                                 }
7057                                 break;
7058                 }
7059
7060                 ++l_img_comp_dest;
7061                 ++l_img_comp_src;
7062                 ++l_tilec;
7063         }
7064
7065         return OPJ_TRUE;
7066 }
7067
7068 /**
7069  * Sets the given area to be decoded. This function should be called right after opj_read_header and before any tile header reading.
7070  *
7071  * @param       p_j2k                   the jpeg2000 codec.
7072  * @param       p_start_x               the left position of the rectangle to decode (in image coordinates).
7073  * @param       p_end_x                 the right position of the rectangle to decode (in image coordinates).
7074  * @param       p_start_y               the up position of the rectangle to decode (in image coordinates).
7075  * @param       p_end_y                 the bottom position of the rectangle to decode (in image coordinates).
7076  * @param       p_manager               the user event manager
7077  *
7078  * @return      true                    if the area could be set.
7079  */
7080 opj_bool j2k_set_decode_area(   opj_j2k_v2_t *p_j2k,
7081                                                                 opj_image_t* p_image,
7082                                                                 OPJ_INT32 p_start_x, OPJ_INT32 p_start_y,
7083                                                                 OPJ_INT32 p_end_x, OPJ_INT32 p_end_y,
7084                                                                 struct opj_event_mgr * p_manager )
7085 {
7086         opj_cp_v2_t * l_cp = &(p_j2k->m_cp);
7087         opj_image_t * l_image = p_j2k->m_private_image;
7088
7089         OPJ_UINT32 it_comp;
7090         OPJ_INT32 l_comp_x1, l_comp_y1;
7091         opj_image_comp_t* l_img_comp = NULL;
7092
7093         /* Check if we are read the main header */
7094         if (p_j2k->m_specific_param.m_decoder.m_state != J2K_STATE_TPHSOT) { // FIXME J2K_DEC_STATE_TPHSOT)
7095                 opj_event_msg_v2(p_manager, EVT_ERROR, "Need to decode the main header before begin to decode the remaining codestream");
7096                 return OPJ_FALSE;
7097         }
7098
7099         if ( !p_start_x && !p_start_y && !p_end_x && !p_end_y){
7100                 opj_event_msg_v2(p_manager, EVT_INFO, "No decoded area parameters, set the decoded area to the all image\n");
7101
7102                 p_j2k->m_specific_param.m_decoder.m_start_tile_x = 0;
7103                 p_j2k->m_specific_param.m_decoder.m_start_tile_y = 0;
7104                 p_j2k->m_specific_param.m_decoder.m_end_tile_x = l_cp->tw;
7105                 p_j2k->m_specific_param.m_decoder.m_end_tile_y = l_cp->th;
7106
7107                 return OPJ_TRUE;
7108         }
7109
7110         /* ----- */
7111         /* Check if the positions provided by the user are correct */
7112
7113         /* Left */
7114         if (p_start_x > l_image->x1 ) {
7115                 opj_event_msg_v2(p_manager, EVT_ERROR,
7116                         "Left position of the decoded area (region_x0=%d) is outside the image area (Xsiz=%d).\n",
7117                         p_start_x, l_image->x1);
7118                 return OPJ_FALSE;
7119         }
7120         else if (p_start_x < l_image->x0){
7121                 opj_event_msg_v2(p_manager, EVT_WARNING,
7122                                 "Left position of the decoded area (region_x0=%d) is outside the image area (XOsiz=%d).\n",
7123                                 p_start_x, l_image->x0);
7124                 p_j2k->m_specific_param.m_decoder.m_start_tile_x = 0;
7125                 p_image->x0 = l_image->x0;
7126         }
7127         else {
7128                 p_j2k->m_specific_param.m_decoder.m_start_tile_x = (p_start_x - l_cp->tx0) / l_cp->tdx;
7129                 p_image->x0 = p_start_x;
7130         }
7131
7132         /* Up */
7133         if (p_start_y > l_image->y1){
7134                 opj_event_msg_v2(p_manager, EVT_ERROR,
7135                                 "Up position of the decoded area (region_y0=%d) is outside the image area (Ysiz=%d).\n",
7136                                 p_start_y, l_image->y1);
7137                 return OPJ_FALSE;
7138         }
7139         else if (p_start_y < l_image->y0){
7140                 opj_event_msg_v2(p_manager, EVT_WARNING,
7141                                 "Up position of the decoded area (region_y0=%d) is outside the image area (YOsiz=%d).\n",
7142                                 p_start_y, l_image->y0);
7143                 p_j2k->m_specific_param.m_decoder.m_start_tile_y = 0;
7144                 p_image->y0 = l_image->y0;
7145         }
7146         else {
7147                 p_j2k->m_specific_param.m_decoder.m_start_tile_y = (p_start_y - l_cp->ty0) / l_cp->tdy;
7148                 p_image->y0 = p_start_y;
7149         }
7150
7151         /* Right */
7152         if (p_end_x < l_image->x0) {
7153                 opj_event_msg_v2(p_manager, EVT_ERROR,
7154                         "Right position of the decoded area (region_x1=%d) is outside the image area (XOsiz=%d).\n",
7155                         p_end_x, l_image->x0);
7156                 return OPJ_FALSE;
7157         }
7158         else if (p_end_x > l_image->x1) {
7159                 opj_event_msg_v2(p_manager, EVT_WARNING,
7160                         "Right position of the decoded area (region_x1=%d) is outside the image area (Xsiz=%d).\n",
7161                         p_end_x, l_image->x1);
7162                 p_j2k->m_specific_param.m_decoder.m_end_tile_x = l_cp->tw;
7163                 p_image->x1 = l_image->x1;
7164         }
7165         else {
7166                 p_j2k->m_specific_param.m_decoder.m_end_tile_x = int_ceildiv((p_end_x - l_cp->tx0), l_cp->tdx);
7167                 p_image->x1 = p_end_x;
7168         }
7169
7170         /* Bottom */
7171         if (p_end_y < l_image->y0) {
7172                 opj_event_msg_v2(p_manager, EVT_ERROR,
7173                         "Right position of the decoded area (region_y1=%d) is outside the image area (YOsiz=%d).\n",
7174                         p_end_y, l_image->y0);
7175                 return OPJ_FALSE;
7176         }
7177         if (p_end_y > l_image->y1){
7178                 opj_event_msg_v2(p_manager, EVT_WARNING,
7179                         "Bottom position of the decoded area (region_y1=%d) is outside the image area (Ysiz=%d).\n",
7180                         p_end_y, l_image->y1);
7181                 p_j2k->m_specific_param.m_decoder.m_end_tile_y = l_cp->th;
7182                 p_image->y1 = l_image->y1;
7183         }
7184         else{
7185                 p_j2k->m_specific_param.m_decoder.m_end_tile_y = int_ceildiv((p_end_y - l_cp->ty0), l_cp->tdy);
7186                 p_image->y1 = p_end_y;
7187         }
7188         /* ----- */
7189
7190         p_j2k->m_specific_param.m_decoder.m_discard_tiles = 1;
7191
7192         l_img_comp = p_image->comps;
7193         for (it_comp=0; it_comp < p_image->numcomps; ++it_comp)
7194         {
7195                 l_img_comp->x0 = int_ceildiv(p_image->x0, l_img_comp->dx);
7196                 l_img_comp->y0 = int_ceildiv(p_image->y0, l_img_comp->dy);
7197                 l_comp_x1 = int_ceildiv(p_image->x1, l_img_comp->dx);
7198                 l_comp_y1 = int_ceildiv(p_image->y1, l_img_comp->dy);
7199
7200                 l_img_comp->w = int_ceildivpow2(l_comp_x1 - l_img_comp->x0, l_img_comp->factor);
7201                 if (l_img_comp->w <= 0){
7202                         opj_event_msg_v2(p_manager, EVT_ERROR,
7203                                 "Size x of the decoded component image is incorrect (comp[%d].w=%d).\n",
7204                                 it_comp, l_img_comp->w);
7205                         return OPJ_FALSE;
7206                 }
7207
7208                 l_img_comp->h = int_ceildivpow2(l_comp_y1 - l_img_comp->y0, l_img_comp->factor);
7209                 if (l_img_comp->h <= 0){
7210                         opj_event_msg_v2(p_manager, EVT_ERROR,
7211                                 "Size y of the decoded component image is incorrect (comp[%d].h=%d).\n",
7212                                 it_comp, l_img_comp->h);
7213                         return OPJ_FALSE;
7214                 }
7215
7216                 l_img_comp++;
7217         }
7218
7219         opj_event_msg_v2( p_manager, EVT_INFO,"Setting decoding area to %d,%d,%d,%d\n",
7220                         p_image->x0, p_image->y0, p_image->x1, p_image->y1);
7221
7222
7223         return OPJ_TRUE;
7224 }
7225
7226
7227 /* ----------------------------------------------------------------------- */
7228 /* J2K / JPT decoder interface                                             */
7229 /* ----------------------------------------------------------------------- */
7230 /**
7231  * Creates a J2K decompression structure.
7232  *
7233  * @return a handle to a J2K decompressor if successful, NULL otherwise.
7234 */
7235 opj_j2k_v2_t* j2k_create_decompress_v2()
7236 {
7237         opj_j2k_v2_t *l_j2k = (opj_j2k_v2_t*) opj_malloc(sizeof(opj_j2k_v2_t));
7238         if (!l_j2k) {
7239                 return 00;
7240         }
7241         memset(l_j2k,0,sizeof(opj_j2k_v2_t));
7242
7243         l_j2k->m_is_decoder = 1;
7244         l_j2k->m_cp.m_is_decoder = 1;
7245
7246         l_j2k->m_specific_param.m_decoder.m_default_tcp = (opj_tcp_v2_t*) opj_malloc(sizeof(opj_tcp_v2_t));
7247         if (!l_j2k->m_specific_param.m_decoder.m_default_tcp) {
7248                 j2k_destroy(l_j2k);
7249                 return 00;
7250         }
7251         memset(l_j2k->m_specific_param.m_decoder.m_default_tcp,0,sizeof(opj_tcp_v2_t));
7252
7253         l_j2k->m_specific_param.m_decoder.m_header_data = (OPJ_BYTE *) opj_malloc(J2K_DEFAULT_HEADER_SIZE);
7254         if (! l_j2k->m_specific_param.m_decoder.m_header_data) {
7255                 j2k_destroy(l_j2k);
7256                 return 00;
7257         }
7258
7259         l_j2k->m_specific_param.m_decoder.m_header_data_size = J2K_DEFAULT_HEADER_SIZE;
7260
7261         /* codestream index creation */
7262         l_j2k->cstr_index = j2k_create_cstr_index();
7263
7264                         /*(opj_codestream_index_t*) opj_malloc(sizeof(opj_codestream_index_t));
7265         if (!l_j2k->cstr_index){
7266                 j2k_destroy(l_j2k);
7267                 return NULL;
7268         }
7269
7270         l_j2k->cstr_index->marker = (opj_marker_info_t*) opj_malloc(100 * sizeof(opj_marker_info_t));
7271 */
7272
7273         /* validation list creation */
7274         l_j2k->m_validation_list = opj_procedure_list_create();
7275         if (! l_j2k->m_validation_list) {
7276                 j2k_destroy(l_j2k);
7277                 return 00;
7278         }
7279
7280         /* execution list creation */
7281         l_j2k->m_procedure_list = opj_procedure_list_create();
7282         if (! l_j2k->m_procedure_list) {
7283                 j2k_destroy(l_j2k);
7284                 return 00;
7285         }
7286
7287         return l_j2k;
7288 }
7289
7290
7291 opj_codestream_index_t* j2k_create_cstr_index(void)
7292 {
7293         opj_codestream_index_t* cstr_index = (opj_codestream_index_t*)
7294                         opj_calloc(1,sizeof(opj_codestream_index_t));
7295         if (!cstr_index)
7296                 return NULL;
7297
7298         cstr_index->maxmarknum = 100;
7299         cstr_index->marknum = 0;
7300         cstr_index->marker = (opj_marker_info_t*)
7301                         opj_calloc(cstr_index->maxmarknum, sizeof(opj_marker_info_t));
7302         if (!cstr_index-> marker)
7303                 return NULL;
7304
7305         cstr_index->tile_index = NULL;
7306
7307         return cstr_index;
7308 }
7309
7310 /**
7311  * Reads a SPCod or SPCoc element, i.e. the coding style of a given component of a tile.
7312  * @param       p_header_data   the data contained in the COM box.
7313  * @param       p_j2k                   the jpeg2000 codec.
7314  * @param       p_header_size   the size of the data contained in the COM marker.
7315  * @param       p_manager               the user event manager.
7316 */
7317 opj_bool j2k_read_SPCod_SPCoc(
7318                                                     opj_j2k_v2_t *p_j2k,
7319                                                         OPJ_UINT32 compno,
7320                                                         OPJ_BYTE * p_header_data,
7321                                                         OPJ_UINT32 * p_header_size,
7322                                                         struct opj_event_mgr * p_manager
7323                                                         )
7324 {
7325         OPJ_UINT32 i, l_tmp;
7326         opj_cp_v2_t *l_cp = NULL;
7327         opj_tcp_v2_t *l_tcp = NULL;
7328         opj_tccp_t *l_tccp = NULL;
7329         OPJ_BYTE * l_current_ptr = NULL;
7330
7331         /* preconditions */
7332         assert(p_j2k != 00);
7333         assert(p_manager != 00);
7334         assert(p_header_data != 00);
7335
7336         l_cp = &(p_j2k->m_cp);
7337         l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ?
7338                                 &l_cp->tcps[p_j2k->m_current_tile_number] :
7339                                 p_j2k->m_specific_param.m_decoder.m_default_tcp;
7340
7341         /* precondition again */
7342         assert(compno < p_j2k->m_private_image->numcomps);
7343
7344         l_tccp = &l_tcp->tccps[compno];
7345         l_current_ptr = p_header_data;
7346
7347         /* make sure room is sufficient */
7348         if (*p_header_size < 5) {
7349                 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading SPCod SPCoc element\n");
7350                 return OPJ_FALSE;
7351         }
7352
7353         opj_read_bytes(l_current_ptr, &l_tccp->numresolutions ,1);              /* SPcox (D) */
7354         ++l_tccp->numresolutions;                                                                               /* tccp->numresolutions = read() + 1 */
7355         ++l_current_ptr;
7356
7357         /* If user wants to remove more resolutions than the codestream contains, return error */
7358         if (l_cp->m_specific_param.m_dec.m_reduce >= l_tccp->numresolutions) {
7359                 opj_event_msg_v2(p_manager, EVT_ERROR, "Error decoding component %d.\nThe number of resolutions to remove is higher than the number "
7360                                         "of resolutions of this component\nModify the cp_reduce parameter.\n\n", compno);
7361                 p_j2k->m_specific_param.m_decoder.m_state |= 0x8000;// FIXME J2K_DEC_STATE_ERR;
7362                 return OPJ_FALSE;
7363         }
7364
7365         opj_read_bytes(l_current_ptr,&l_tccp->cblkw ,1);                /* SPcoc (E) */
7366         ++l_current_ptr;
7367         l_tccp->cblkw += 2;
7368
7369         opj_read_bytes(l_current_ptr,&l_tccp->cblkh ,1);                /* SPcoc (F) */
7370         ++l_current_ptr;
7371         l_tccp->cblkh += 2;
7372
7373         opj_read_bytes(l_current_ptr,&l_tccp->cblksty ,1);              /* SPcoc (G) */
7374         ++l_current_ptr;
7375
7376         opj_read_bytes(l_current_ptr,&l_tccp->qmfbid ,1);               /* SPcoc (H) */
7377         ++l_current_ptr;
7378
7379         *p_header_size = *p_header_size - 5;
7380
7381         /* use custom precinct size ? */
7382         if (l_tccp->csty & J2K_CCP_CSTY_PRT) {
7383                 if (*p_header_size < l_tccp->numresolutions) {
7384                         opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading SPCod SPCoc element\n");
7385                         return OPJ_FALSE;
7386                 }
7387
7388                 for     (i = 0; i < l_tccp->numresolutions; ++i) {
7389                         opj_read_bytes(l_current_ptr,&l_tmp ,1);                /* SPcoc (I_i) */
7390                         ++l_current_ptr;
7391                         l_tccp->prcw[i] = l_tmp & 0xf;
7392                         l_tccp->prch[i] = l_tmp >> 4;
7393                 }
7394
7395                 *p_header_size = *p_header_size - l_tccp->numresolutions;
7396         }
7397         else {
7398                 /* set default size for the precinct width and height */
7399                 for     (i = 0; i < l_tccp->numresolutions; ++i) {
7400                         l_tccp->prcw[i] = 15;
7401                         l_tccp->prch[i] = 15;
7402                 }
7403         }
7404
7405 #ifdef WIP_REMOVE_MSD
7406         /* INDEX >> */
7407         if (p_j2k->cstr_info && compno == 0) {
7408                 OPJ_UINT32 l_data_size = l_tccp->numresolutions * sizeof(OPJ_UINT32);
7409
7410                 p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].tccp_info[compno].cblkh = l_tccp->cblkh;
7411                 p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].tccp_info[compno].cblkw = l_tccp->cblkw;
7412                 p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].tccp_info[compno].numresolutions = l_tccp->numresolutions;
7413                 p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].tccp_info[compno].cblksty = l_tccp->cblksty;
7414                 p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].tccp_info[compno].qmfbid = l_tccp->qmfbid;
7415
7416
7417                 memcpy(p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].pdx,l_tccp->prcw, l_data_size);
7418                 memcpy(p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].pdy,l_tccp->prch, l_data_size);
7419         }
7420         /* << INDEX */
7421 #endif
7422
7423         return OPJ_TRUE;
7424 }
7425
7426 /**
7427  * Copies the tile component parameters of all the component from the first tile component.
7428  *
7429  * @param               p_j2k           the J2k codec.
7430  */
7431 void j2k_copy_tile_component_parameters( opj_j2k_v2_t *p_j2k )
7432 {
7433         // loop
7434         OPJ_UINT32 i;
7435         opj_cp_v2_t *l_cp = NULL;
7436         opj_tcp_v2_t *l_tcp = NULL;
7437         opj_tccp_t *l_ref_tccp = NULL, *l_copied_tccp = NULL;
7438         OPJ_UINT32 l_prc_size;
7439
7440         /* preconditions */
7441         assert(p_j2k != 00);
7442
7443         l_cp = &(p_j2k->m_cp);
7444         l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ? /* FIXME J2K_DEC_STATE_TPH*/
7445                                 &l_cp->tcps[p_j2k->m_current_tile_number] :
7446                                 p_j2k->m_specific_param.m_decoder.m_default_tcp;
7447
7448         l_ref_tccp = &l_tcp->tccps[0];
7449         l_copied_tccp = l_ref_tccp + 1;
7450         l_prc_size = l_ref_tccp->numresolutions * sizeof(OPJ_UINT32);
7451
7452         for     (i=1; i<p_j2k->m_private_image->numcomps; ++i) {
7453                 l_copied_tccp->numresolutions = l_ref_tccp->numresolutions;
7454                 l_copied_tccp->cblkw = l_ref_tccp->cblkw;
7455                 l_copied_tccp->cblkh = l_ref_tccp->cblkh;
7456                 l_copied_tccp->cblksty = l_ref_tccp->cblksty;
7457                 l_copied_tccp->qmfbid = l_ref_tccp->qmfbid;
7458                 memcpy(l_copied_tccp->prcw,l_ref_tccp->prcw,l_prc_size);
7459                 memcpy(l_copied_tccp->prch,l_ref_tccp->prch,l_prc_size);
7460                 ++l_copied_tccp;
7461         }
7462 }
7463
7464 /**
7465  * Reads a SQcd or SQcc element, i.e. the quantization values of a band.
7466  *
7467  * @param       p_comp_no               the component being targeted.
7468  * @param       p_header_data   the data contained in the COM box.
7469  * @param       p_j2k                   the jpeg2000 codec.
7470  * @param       p_header_size   the size of the data contained in the COM marker.
7471  * @param       p_manager               the user event manager.
7472 */
7473 opj_bool j2k_read_SQcd_SQcc(
7474                                                         opj_j2k_v2_t *p_j2k,
7475                                                         OPJ_UINT32 p_comp_no,
7476                                                         OPJ_BYTE* p_header_data,
7477                                                         OPJ_UINT32 * p_header_size,
7478                                                         struct opj_event_mgr * p_manager
7479                                                         )
7480 {
7481         // loop
7482         OPJ_UINT32 l_band_no;
7483         opj_cp_v2_t *l_cp = 00;
7484         opj_tcp_v2_t *l_tcp = 00;
7485         opj_tccp_t *l_tccp = 00;
7486         OPJ_BYTE * l_current_ptr = 00;
7487         OPJ_UINT32 l_tmp, l_num_band;
7488
7489         // preconditions
7490         assert(p_j2k != 00);
7491         assert(p_manager != 00);
7492         assert(p_header_data != 00);
7493
7494         l_cp = &(p_j2k->m_cp);
7495         // come from tile part header or main header ?
7496         l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ? /*FIXME J2K_DEC_STATE_TPH*/
7497                                 &l_cp->tcps[p_j2k->m_current_tile_number] :
7498                                 p_j2k->m_specific_param.m_decoder.m_default_tcp;
7499
7500         // precondition again
7501         assert(p_comp_no <  p_j2k->m_private_image->numcomps);
7502
7503         l_tccp = &l_tcp->tccps[p_comp_no];
7504         l_current_ptr = p_header_data;
7505
7506         if (*p_header_size < 1) {
7507                 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading SQcd or SQcc element\n");
7508                 return OPJ_FALSE;
7509         }
7510         *p_header_size -= 1;
7511
7512         opj_read_bytes(l_current_ptr, &l_tmp ,1);                       /* Sqcx */
7513         ++l_current_ptr;
7514
7515         l_tccp->qntsty = l_tmp & 0x1f;
7516         l_tccp->numgbits = l_tmp >> 5;
7517         if (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) {
7518         l_num_band = 1;
7519         }
7520         else {
7521                 l_num_band = (l_tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) ?
7522                         (*p_header_size) :
7523                         (*p_header_size) / 2;
7524
7525                 if( l_num_band > J2K_MAXBANDS ) {
7526                         opj_event_msg_v2(p_manager, EVT_WARNING, "While reading CCP_QNTSTY element inside QCD or QCC marker segment, "
7527                                 "number of subbands (%d) is greater to J2K_MAXBANDS (%d). So we limiting the number of elements stored to "
7528                                 "J2K_MAXBANDS (%d) and skip the other. \n", l_num_band, J2K_MAXBANDS, J2K_MAXBANDS);
7529                         //return OPJ_FALSE;
7530                 }
7531         }
7532
7533 #ifdef USE_JPWL
7534         if (l_cp->correct) {
7535
7536                 /* if JPWL is on, we check whether there are too many subbands */
7537                 if ((l_num_band < 0) || (l_num_band >= J2K_MAXBANDS)) {
7538                         opj_event_msg_v2(p_manager, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
7539                                 "JPWL: bad number of subbands in Sqcx (%d)\n",
7540                                 l_num_band);
7541                         if (!JPWL_ASSUME) {
7542                                 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
7543                                 return OPJ_FALSE;
7544                         }
7545                         /* we try to correct */
7546                         l_num_band = 1;
7547                         opj_event_msg_v2(p_manager, EVT_WARNING, "- trying to adjust them\n"
7548                                 "- setting number of bands to %d => HYPOTHESIS!!!\n",
7549                                 l_num_band);
7550                 };
7551
7552         };
7553 #endif /* USE_JPWL */
7554
7555         if (l_tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) {
7556                 for     (l_band_no = 0; l_band_no < l_num_band; l_band_no++) {
7557                         opj_read_bytes(l_current_ptr, &l_tmp ,1);                       /* SPqcx_i */
7558                         ++l_current_ptr;
7559                         if (l_band_no < J2K_MAXBANDS){
7560                                 l_tccp->stepsizes[l_band_no].expn = l_tmp>>3;
7561                                 l_tccp->stepsizes[l_band_no].mant = 0;
7562                         }
7563                 }
7564                 *p_header_size = *p_header_size - l_num_band;
7565         }
7566         else {
7567                 for     (l_band_no = 0; l_band_no < l_num_band; l_band_no++) {
7568                         opj_read_bytes(l_current_ptr, &l_tmp ,2);                       /* SPqcx_i */
7569                         l_current_ptr+=2;
7570                         if (l_band_no < J2K_MAXBANDS){
7571                                 l_tccp->stepsizes[l_band_no].expn = l_tmp >> 11;
7572                                 l_tccp->stepsizes[l_band_no].mant = l_tmp & 0x7ff;
7573                         }
7574                 }
7575                 *p_header_size = *p_header_size - 2*l_num_band;
7576         }
7577
7578         /* Add Antonin : if scalar_derived -> compute other stepsizes */
7579         if (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) {
7580                 for (l_band_no = 1; l_band_no < J2K_MAXBANDS; l_band_no++) {
7581                         l_tccp->stepsizes[l_band_no].expn =
7582                                 ((l_tccp->stepsizes[0].expn) - ((l_band_no - 1) / 3) > 0) ?
7583                                         (l_tccp->stepsizes[0].expn) - ((l_band_no - 1) / 3) : 0;
7584                         l_tccp->stepsizes[l_band_no].mant = l_tccp->stepsizes[0].mant;
7585                 }
7586         }
7587
7588         return OPJ_TRUE;
7589 }
7590
7591 /**
7592  * Copies the tile component parameters of all the component from the first tile component.
7593  *
7594  * @param               p_j2k           the J2k codec.
7595  */
7596 void j2k_copy_tile_quantization_parameters( opj_j2k_v2_t *p_j2k )
7597 {
7598         OPJ_UINT32 i;
7599         opj_cp_v2_t *l_cp = NULL;
7600         opj_tcp_v2_t *l_tcp = NULL;
7601         opj_tccp_t *l_ref_tccp = NULL;
7602         opj_tccp_t *l_copied_tccp = NULL;
7603         OPJ_UINT32 l_size;
7604
7605         /* preconditions */
7606         assert(p_j2k != 00);
7607
7608         l_cp = &(p_j2k->m_cp);
7609         l_tcp = p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH ?
7610                         &l_cp->tcps[p_j2k->m_current_tile_number] :
7611                         p_j2k->m_specific_param.m_decoder.m_default_tcp;
7612
7613         l_ref_tccp = &l_tcp->tccps[0];
7614         l_copied_tccp = l_ref_tccp + 1;
7615         l_size = J2K_MAXBANDS * sizeof(opj_stepsize_t);
7616
7617         for     (i=1;i<p_j2k->m_private_image->numcomps;++i) {
7618                 l_copied_tccp->qntsty = l_ref_tccp->qntsty;
7619                 l_copied_tccp->numgbits = l_ref_tccp->numgbits;
7620                 memcpy(l_copied_tccp->stepsizes,l_ref_tccp->stepsizes,l_size);
7621                 ++l_copied_tccp;
7622         }
7623 }
7624
7625 /**
7626  * Dump some elements from the J2K decompression structure .
7627  *
7628  *@param p_j2k                          the jpeg2000 codec.
7629  *@param flag                           flag to describe what elments are dump.
7630  *@param out_stream                     output stream where dump the elements.
7631  *
7632 */
7633 void j2k_dump (opj_j2k_v2_t* p_j2k, OPJ_INT32 flag, FILE* out_stream)
7634 {
7635         /* Check if the flag is compatible with j2k file*/
7636         if ( (flag & OPJ_JP2_INFO) || (flag & OPJ_JP2_IND)){
7637                 fprintf(out_stream, "Wrong flag\n");
7638                 return;
7639         }
7640
7641         /* Dump the image_header */
7642         if (flag & OPJ_IMG_INFO){
7643                 if (p_j2k->m_private_image)
7644                         j2k_dump_image_header(p_j2k->m_private_image, 0, out_stream);
7645         }
7646
7647         /* Dump the codestream info from main header */
7648         if (flag & OPJ_J2K_MH_INFO){
7649                 j2k_dump_MH_info(p_j2k, out_stream);
7650         }
7651
7652
7653         /* Dump the codestream info of the current tile */
7654         if (flag & OPJ_J2K_TH_INFO){
7655
7656         }
7657
7658         /* Dump the codestream index from main header */
7659         if (flag & OPJ_J2K_MH_IND){
7660                 j2k_dump_MH_index(p_j2k, out_stream);
7661         }
7662
7663         /* Dump the codestream index of the current tile */
7664         if (flag & OPJ_J2K_TH_IND){
7665
7666         }
7667
7668 }
7669
7670 /**
7671  * Dump index elements of the codestream extract from the main header.
7672  *
7673  *@param p_j2k                          the jpeg2000 codec.
7674  *@param out_stream                     output stream where dump the elements.
7675  *
7676 */
7677 void j2k_dump_MH_index(opj_j2k_v2_t* p_j2k, FILE* out_stream)
7678 {
7679         opj_codestream_index_t* cstr_index = p_j2k->cstr_index;
7680         OPJ_UINT32 it_marker, it_tile, it_tile_part;
7681
7682         fprintf(out_stream, "Codestream index from main header: {\n");
7683
7684         fprintf(out_stream, "\t Main header start position=%d\n\t Main header end position=%d\n",
7685                         cstr_index->main_head_start, cstr_index->main_head_end);
7686
7687         fprintf(out_stream, "\t Marker list: {\n");
7688
7689         if (cstr_index->marker){
7690                 for (it_marker=0; it_marker < cstr_index->marknum ; it_marker++){
7691                         fprintf(out_stream, "\t\t type=%#x, pos=%d, len=%d\n",
7692                                         cstr_index->marker[it_marker].type,
7693                                         cstr_index->marker[it_marker].pos,
7694                                         cstr_index->marker[it_marker].len );
7695                 }
7696         }
7697
7698         fprintf(out_stream, "\t }\n");
7699
7700
7701         if (cstr_index->tile_index){
7702                 fprintf(out_stream, "\t Tile index: {\n");
7703
7704                 for (it_tile=0; it_tile < cstr_index->nb_of_tiles ; it_tile++){
7705                         OPJ_UINT32 nb_of_tile_part = cstr_index->tile_index[it_tile].nb_tps;
7706
7707                         fprintf(out_stream, "\t\t nb of tile-part in tile [%d]=%d\n", it_tile, nb_of_tile_part);
7708
7709                         if (cstr_index->tile_index[it_tile].tp_index){
7710                                 for (it_tile_part =0; it_tile_part < nb_of_tile_part; it_tile_part++){
7711                                         fprintf(out_stream, "\t\t\t tile-part[%d]: star_pos=%d, end_header=%d, end_pos=%d.\n",
7712                                                         it_tile_part,
7713                                                         cstr_index->tile_index[it_tile].tp_index[it_tile_part].start_pos,
7714                                                         cstr_index->tile_index[it_tile].tp_index[it_tile_part].end_header,
7715                                                         cstr_index->tile_index[it_tile].tp_index[it_tile_part].end_pos);
7716                                 }
7717                         }
7718
7719                         if (cstr_index->tile_index[it_tile].marker){
7720                                 for (it_marker=0; it_marker < cstr_index->tile_index[it_tile].marknum ; it_marker++){
7721                                         fprintf(out_stream, "\t\t type=%#x, pos=%d, len=%d\n",
7722                                                         cstr_index->tile_index[it_tile].marker[it_marker].type,
7723                                                         cstr_index->tile_index[it_tile].marker[it_marker].pos,
7724                                                         cstr_index->tile_index[it_tile].marker[it_marker].len );
7725                                 }
7726                         }
7727                 }
7728                 fprintf(out_stream,"\t }\n");
7729         }
7730
7731         fprintf(out_stream,"}\n");
7732
7733 }
7734
7735 /**
7736  * Dump info elements of the codestream extract from the main header.
7737  *
7738  *@param p_j2k                          the jpeg2000 codec.
7739  *@param out_stream                     output stream where dump the elements.
7740  *
7741 */
7742 void j2k_dump_MH_info(opj_j2k_v2_t* p_j2k, FILE* out_stream)
7743 {
7744         opj_tcp_v2_t * l_default_tile=NULL;
7745
7746         fprintf(out_stream, "Codestream info from main header: {\n");
7747
7748         fprintf(out_stream, "\t tx0=%d, ty0=%d\n", p_j2k->m_cp.tx0, p_j2k->m_cp.ty0);
7749         fprintf(out_stream, "\t tdx=%d, tdy=%d\n", p_j2k->m_cp.tdx, p_j2k->m_cp.tdy);
7750         fprintf(out_stream, "\t tw=%d, th=%d\n", p_j2k->m_cp.tw, p_j2k->m_cp.th);
7751
7752         l_default_tile = p_j2k->m_specific_param.m_decoder.m_default_tcp;
7753         if (l_default_tile)
7754         {
7755                 OPJ_INT32 compno;
7756                 OPJ_INT32 numcomps = p_j2k->m_private_image->numcomps;
7757
7758                 fprintf(out_stream, "\t default tile {\n");
7759                 fprintf(out_stream, "\t\t csty=%#x\n", l_default_tile->csty);
7760                 fprintf(out_stream, "\t\t prg=%#x\n", l_default_tile->prg);
7761                 fprintf(out_stream, "\t\t numlayers=%d\n", l_default_tile->numlayers);
7762                 fprintf(out_stream, "\t\t mct=%x\n", l_default_tile->mct);
7763
7764                 for (compno = 0; compno < numcomps; compno++) {
7765                         opj_tccp_t *l_tccp = &(l_default_tile->tccps[compno]);
7766                         OPJ_INT32 resno, bandno, numbands;
7767
7768                         /* coding style*/
7769                         fprintf(out_stream, "\t\t comp %d {\n", compno);
7770                         fprintf(out_stream, "\t\t\t csty=%#x\n", l_tccp->csty);
7771                         fprintf(out_stream, "\t\t\t numresolutions=%d\n", l_tccp->numresolutions);
7772                         fprintf(out_stream, "\t\t\t cblkw=2^%d\n", l_tccp->cblkw);
7773                         fprintf(out_stream, "\t\t\t cblkh=2^%d\n", l_tccp->cblkh);
7774                         fprintf(out_stream, "\t\t\t cblksty=%#x\n", l_tccp->cblksty);
7775                         fprintf(out_stream, "\t\t\t qmfbid=%d\n", l_tccp->qmfbid);
7776
7777                         fprintf(out_stream, "\t\t\t preccintsize (w,h)=");
7778                         for (resno = 0; resno < l_tccp->numresolutions; resno++) {
7779                                 fprintf(out_stream, "(%d,%d) ", l_tccp->prcw[resno], l_tccp->prch[resno]);
7780                         }
7781                         fprintf(out_stream, "\n");
7782
7783                         /* quantization style*/
7784                         fprintf(out_stream, "\t\t\t qntsty=%d\n", l_tccp->qntsty);
7785                         fprintf(out_stream, "\t\t\t numgbits=%d\n", l_tccp->numgbits);
7786                         fprintf(out_stream, "\t\t\t stepsizes (m,e)=");
7787                         numbands = (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) ? 1 : l_tccp->numresolutions * 3 - 2;
7788                         for (bandno = 0; bandno < numbands; bandno++) {
7789                                 fprintf(out_stream, "(%d,%d) ", l_tccp->stepsizes[bandno].mant,
7790                                         l_tccp->stepsizes[bandno].expn);
7791                         }
7792                         fprintf(out_stream, "\n");
7793
7794                         /* RGN value*/
7795                         fprintf(out_stream, "\t\t\t roishift=%d\n", l_tccp->roishift);
7796
7797                         fprintf(out_stream, "\t\t }\n");
7798                 } /*end of component of default tile*/
7799                 fprintf(out_stream, "\t }\n"); /*end of default tile*/
7800
7801         }
7802
7803         fprintf(out_stream, "}\n");
7804
7805 }
7806
7807 /**
7808  * Dump an image header structure.
7809  *
7810  *@param img_header                     the image header to dump.
7811  *@param dev_dump_flag          flag to describe if we are in the case of this function is use outside j2k_dump function
7812  *@param out_stream                     output stream where dump the elements.
7813  */
7814 void j2k_dump_image_header(opj_image_t* img_header, opj_bool dev_dump_flag, FILE* out_stream)
7815 {
7816         char tab[2];
7817
7818         if (dev_dump_flag){
7819                 fprintf(stdout, "[DEV] Dump a image_header struct {\n");
7820                 tab[0] = '\0';
7821         }
7822         else {
7823                 fprintf(out_stream, "Image info {\n");
7824                 tab[0] = '\t';tab[1] = '\0';
7825         }
7826
7827         fprintf(out_stream, "%s x0=%d, y0=%d\n", tab, img_header->x0, img_header->y0);
7828         fprintf(out_stream,     "%s x1=%d, y1=%d\n", tab, img_header->x1, img_header->y1);
7829         fprintf(out_stream, "%s numcomps=%d\n", tab, img_header->numcomps);
7830
7831         if (img_header->comps){
7832                 int compno;
7833                 for (compno = 0; compno < img_header->numcomps; compno++) {
7834                         fprintf(out_stream, "%s\t component %d {\n", tab, compno);
7835                         j2k_dump_image_comp_header(&(img_header->comps[compno]), dev_dump_flag, out_stream);
7836                         fprintf(out_stream,"%s}\n",tab);
7837                 }
7838         }
7839
7840         fprintf(out_stream, "}\n");
7841 }
7842
7843 /**
7844  * Dump a component image header structure.
7845  *
7846  *@param comp_header            the component image header to dump.
7847  *@param dev_dump_flag          flag to describe if we are in the case of this function is use outside j2k_dump function
7848  *@param out_stream                     output stream where dump the elements.
7849  */
7850 void j2k_dump_image_comp_header(opj_image_comp_t* comp_header, opj_bool dev_dump_flag, FILE* out_stream)
7851 {
7852         char tab[3];
7853
7854         if (dev_dump_flag){
7855                 fprintf(stdout, "[DEV] Dump a image_comp_header struct {\n");
7856                 tab[0] = '\0';
7857         }       else {
7858                 tab[0] = '\t';tab[1] = '\t';tab[2] = '\0';
7859         }
7860
7861         fprintf(out_stream, "%s dx=%d, dy=%d\n", tab, comp_header->dx, comp_header->dy);
7862         fprintf(out_stream, "%s prec=%d\n", tab, comp_header->prec);
7863         fprintf(out_stream, "%s sgnd=%d\n", tab, comp_header->sgnd);
7864
7865         if (dev_dump_flag)
7866                 fprintf(out_stream, "}\n");
7867 }
7868
7869
7870 /**
7871  * Get the codestream info from a JPEG2000 codec.
7872  *
7873  *@param        p_j2k                           the component image header to dump.
7874  *
7875  *@return       the codestream information extract from the jpg2000 codec
7876  */
7877 opj_codestream_info_v2_t* j2k_get_cstr_info(opj_j2k_v2_t* p_j2k)
7878 {
7879         OPJ_UINT16 compno;
7880         OPJ_UINT16 numcomps = p_j2k->m_private_image->numcomps;
7881         opj_tcp_v2_t *l_default_tile;
7882         opj_codestream_info_v2_t* cstr_info = (opj_codestream_info_v2_t*) opj_calloc(1,sizeof(opj_codestream_info_v2_t));
7883
7884         cstr_info->nbcomps = p_j2k->m_private_image->numcomps;
7885
7886         cstr_info->tx0 = p_j2k->m_cp.tx0;
7887         cstr_info->ty0 = p_j2k->m_cp.ty0;
7888         cstr_info->tdx = p_j2k->m_cp.tdx;
7889         cstr_info->tdy = p_j2k->m_cp.tdy;
7890         cstr_info->tw = p_j2k->m_cp.tw;
7891         cstr_info->th = p_j2k->m_cp.th;
7892
7893         cstr_info->tile_info = NULL; /* Not fill from the main header*/
7894
7895         l_default_tile = p_j2k->m_specific_param.m_decoder.m_default_tcp;
7896
7897         cstr_info->m_default_tile_info.csty = l_default_tile->csty;
7898         cstr_info->m_default_tile_info.prg = l_default_tile->prg;
7899         cstr_info->m_default_tile_info.numlayers = l_default_tile->numlayers;
7900         cstr_info->m_default_tile_info.mct = l_default_tile->mct;
7901
7902         cstr_info->m_default_tile_info.tccp_info = (opj_tccp_info_t*) opj_calloc(cstr_info->nbcomps, sizeof(opj_tccp_info_t));
7903
7904         for (compno = 0; compno < numcomps; compno++) {
7905                 opj_tccp_t *l_tccp = &(l_default_tile->tccps[compno]);
7906                 opj_tccp_info_t *l_tccp_info = &(cstr_info->m_default_tile_info.tccp_info[compno]);
7907                 OPJ_INT32 bandno, numbands;
7908
7909                 /* coding style*/
7910                 l_tccp_info->csty = l_tccp->csty;
7911                 l_tccp_info->numresolutions = l_tccp->numresolutions;
7912                 l_tccp_info->cblkw = l_tccp->cblkw;
7913                 l_tccp_info->cblkh = l_tccp->cblkh;
7914                 l_tccp_info->cblksty = l_tccp->cblksty;
7915                 l_tccp_info->qmfbid = l_tccp->qmfbid;
7916                 if (l_tccp->numresolutions < J2K_MAXRLVLS)
7917                 {
7918                         memcpy(l_tccp_info->prch, l_tccp->prch, l_tccp->numresolutions);
7919                         memcpy(l_tccp_info->prcw, l_tccp->prcw, l_tccp->numresolutions);
7920                 }
7921
7922                 /* quantization style*/
7923                 l_tccp_info->qntsty = l_tccp->qntsty;
7924                 l_tccp_info->numgbits = l_tccp->numgbits;
7925
7926                 numbands = (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) ? 1 : l_tccp->numresolutions * 3 - 2;
7927                 if (numbands < J2K_MAXBANDS) {
7928                         for (bandno = 0; bandno < numbands; bandno++) {
7929                                 l_tccp_info->stepsizes_mant[bandno] = l_tccp->stepsizes[bandno].mant;
7930                                 l_tccp_info->stepsizes_expn[bandno] = l_tccp->stepsizes[bandno].expn;
7931                         }
7932                 }
7933
7934                 /* RGN value*/
7935                 l_tccp_info->roishift = l_tccp->roishift;
7936         }
7937
7938
7939         return cstr_info;
7940 }
7941
7942 /**
7943  * Get the codestream index from a JPEG2000 codec.
7944  *
7945  *@param        p_j2k                           the component image header to dump.
7946  *
7947  *@return       the codestream index extract from the jpg2000 codec
7948  */
7949 opj_codestream_index_t* j2k_get_cstr_index(opj_j2k_v2_t* p_j2k)
7950 {
7951         opj_codestream_index_t* l_cstr_index = (opj_codestream_index_t*)
7952                         opj_calloc(1,sizeof(opj_codestream_index_t));
7953         if (!l_cstr_index)
7954                 return NULL;
7955
7956         l_cstr_index->main_head_start = p_j2k->cstr_index->main_head_start;
7957         l_cstr_index->main_head_end = p_j2k->cstr_index->main_head_end;
7958         l_cstr_index->codestream_size = p_j2k->cstr_index->codestream_size;
7959
7960         l_cstr_index->marknum = p_j2k->cstr_index->marknum;
7961         l_cstr_index->marker = (opj_marker_info_t*)opj_malloc(l_cstr_index->marknum*sizeof(opj_marker_info_t));
7962         if (!l_cstr_index->marker){
7963                 opj_free( l_cstr_index);
7964                 return NULL;
7965         }
7966
7967         if (p_j2k->cstr_index->marker)
7968                 memcpy(l_cstr_index->marker, p_j2k->cstr_index->marker, l_cstr_index->marknum * sizeof(opj_marker_info_t) );
7969         else{
7970                 opj_free(l_cstr_index->marker);
7971                 l_cstr_index->marker = NULL;
7972         }
7973
7974         l_cstr_index->nb_of_tiles = p_j2k->cstr_index->nb_of_tiles;
7975         l_cstr_index->tile_index = (opj_tile_index_t*)opj_calloc(l_cstr_index->nb_of_tiles, sizeof(opj_tile_index_t) );
7976         if (!l_cstr_index->tile_index){
7977                 opj_free( l_cstr_index->marker);
7978                 opj_free( l_cstr_index);
7979                 return NULL;
7980         }
7981
7982         if (!p_j2k->cstr_index->tile_index){
7983                 opj_free(l_cstr_index->tile_index);
7984                 l_cstr_index->tile_index = NULL;
7985         }
7986         else {
7987                 OPJ_UINT32 it_tile = 0;
7988                 for (it_tile = 0; it_tile < l_cstr_index->nb_of_tiles; it_tile++ ){
7989
7990                         /* Tile Marker*/
7991                         l_cstr_index->tile_index[it_tile].marknum = p_j2k->cstr_index->tile_index[it_tile].marknum;
7992
7993                         l_cstr_index->tile_index[it_tile].marker =
7994                                 (opj_marker_info_t*)opj_malloc(l_cstr_index->tile_index[it_tile].marknum*sizeof(opj_marker_info_t));
7995
7996                         if (!l_cstr_index->tile_index[it_tile].marker) {
7997                                 OPJ_UINT32 it_tile_free;
7998
7999                                 for (it_tile_free=0; it_tile_free < it_tile; it_tile_free++){
8000                                         opj_free(l_cstr_index->tile_index[it_tile_free].marker);
8001                                 }
8002
8003                                 opj_free( l_cstr_index->tile_index);
8004                                 opj_free( l_cstr_index->marker);
8005                                 opj_free( l_cstr_index);
8006                                 return NULL;
8007                         }
8008
8009                         if (p_j2k->cstr_index->tile_index[it_tile].marker)
8010                                 memcpy( l_cstr_index->tile_index[it_tile].marker,
8011                                                 p_j2k->cstr_index->tile_index[it_tile].marker,
8012                                                 l_cstr_index->tile_index[it_tile].marknum * sizeof(opj_marker_info_t) );
8013                         else{
8014                                 opj_free(l_cstr_index->tile_index[it_tile].marker);
8015                                 l_cstr_index->tile_index[it_tile].marker = NULL;
8016                         }
8017
8018                         /* Tile part index*/
8019                         l_cstr_index->tile_index[it_tile].nb_tps = p_j2k->cstr_index->tile_index[it_tile].nb_tps;
8020
8021                         l_cstr_index->tile_index[it_tile].tp_index =
8022                                 (opj_tp_index_t*)opj_malloc(l_cstr_index->tile_index[it_tile].nb_tps*sizeof(opj_tp_index_t));
8023
8024                         if(!l_cstr_index->tile_index[it_tile].tp_index){
8025                                 OPJ_UINT32 it_tile_free;
8026
8027                                 for (it_tile_free=0; it_tile_free < it_tile; it_tile_free++){
8028                                         opj_free(l_cstr_index->tile_index[it_tile_free].marker);
8029                                         opj_free(l_cstr_index->tile_index[it_tile_free].tp_index);
8030                                 }
8031
8032                                 opj_free( l_cstr_index->tile_index);
8033                                 opj_free( l_cstr_index->marker);
8034                                 opj_free( l_cstr_index);
8035                                 return NULL;
8036                         }
8037
8038                         if (p_j2k->cstr_index->tile_index[it_tile].tp_index){
8039                                 memcpy( l_cstr_index->tile_index[it_tile].tp_index,
8040                                                 p_j2k->cstr_index->tile_index[it_tile].tp_index,
8041                                                 l_cstr_index->tile_index[it_tile].nb_tps * sizeof(opj_tp_index_t) );
8042                         }
8043                         else{
8044                                 opj_free(l_cstr_index->tile_index[it_tile].tp_index);
8045                                 l_cstr_index->tile_index[it_tile].tp_index = NULL;
8046                         }
8047
8048                         /* Packet index (NOT USED)*/
8049                         l_cstr_index->tile_index[it_tile].nb_packet = 0;
8050                         l_cstr_index->tile_index[it_tile].packet_index = NULL;
8051
8052                 }
8053         }
8054
8055         return l_cstr_index;
8056 }
8057
8058 opj_bool j2k_allocate_tile_element_cstr_index(opj_j2k_v2_t *p_j2k)
8059 {
8060         OPJ_UINT32 it_tile=0;
8061
8062         p_j2k->cstr_index->nb_of_tiles = p_j2k->m_cp.tw * p_j2k->m_cp.tw;
8063         p_j2k->cstr_index->tile_index = (opj_tile_index_t*)opj_calloc(p_j2k->cstr_index->nb_of_tiles, sizeof(opj_tile_index_t));
8064         if (!p_j2k->cstr_index->tile_index)
8065                 return OPJ_FALSE;
8066
8067         for (it_tile=0; it_tile < p_j2k->cstr_index->nb_of_tiles; it_tile++){
8068                 p_j2k->cstr_index->tile_index[it_tile].maxmarknum = 100;
8069                 p_j2k->cstr_index->tile_index[it_tile].marknum = 0;
8070                 p_j2k->cstr_index->tile_index[it_tile].marker = (opj_marker_info_t*)
8071                                 opj_calloc(p_j2k->cstr_index->tile_index[it_tile].maxmarknum, sizeof(opj_marker_info_t));
8072                 if (!p_j2k->cstr_index->tile_index[it_tile].marker)
8073                         return OPJ_FALSE;
8074         }
8075
8076         return OPJ_TRUE;
8077 }
8078
8079 /**
8080  * Reads the tiles.
8081  */
8082 opj_bool j2k_decode_tiles (     opj_j2k_v2_t *p_j2k,
8083                                                         opj_stream_private_t *p_stream,
8084                                                         opj_event_mgr_t * p_manager)
8085 {
8086         opj_bool l_go_on = OPJ_TRUE;
8087         OPJ_UINT32 l_current_tile_no;
8088         OPJ_UINT32 l_data_size,l_max_data_size;
8089         OPJ_INT32 l_tile_x0,l_tile_y0,l_tile_x1,l_tile_y1;
8090         OPJ_UINT32 l_nb_comps;
8091         OPJ_BYTE * l_current_data;
8092
8093         l_current_data = (OPJ_BYTE*)opj_malloc(1000);
8094         if (! l_current_data) {
8095                 return OPJ_FALSE;
8096         }
8097         l_max_data_size = 1000;
8098
8099         /*Allocate and initialize some elements of codestrem index*/
8100         if (!j2k_allocate_tile_element_cstr_index(p_j2k))
8101                 return OPJ_FALSE;
8102
8103         while (OPJ_TRUE) {
8104                 if (! j2k_read_tile_header(     p_j2k,
8105                                                                         &l_current_tile_no,
8106                                                                         &l_data_size,
8107                                                                         &l_tile_x0, &l_tile_y0,
8108                                                                         &l_tile_x1, &l_tile_y1,
8109                                                                         &l_nb_comps,
8110                                                                         &l_go_on,
8111                                                                         p_stream,
8112                                                                         p_manager)) {
8113                         opj_free(l_current_data);
8114                         return OPJ_FALSE;
8115                 }
8116
8117                 if (! l_go_on) {
8118                         break;
8119                 }
8120
8121                 if (l_data_size > l_max_data_size) {
8122                         l_current_data = (OPJ_BYTE*)opj_realloc(l_current_data,l_data_size);
8123                         if (! l_current_data) {
8124                                 return OPJ_FALSE;
8125                         }
8126
8127                         l_max_data_size = l_data_size;
8128                 }
8129
8130                 if (! j2k_decode_tile(p_j2k,l_current_tile_no,l_current_data,l_data_size,p_stream,p_manager)) {
8131                         opj_free(l_current_data);
8132                         return OPJ_FALSE;
8133                 }
8134                 opj_event_msg_v2(p_manager, EVT_INFO, "Tile %d/%d has been decode.\n", l_current_tile_no +1, p_j2k->m_cp.th * p_j2k->m_cp.tw);
8135
8136                 if (! j2k_update_image_data(p_j2k->m_tcd,l_current_data, p_j2k->m_output_image)) {
8137                         opj_free(l_current_data);
8138                         return OPJ_FALSE;
8139                 }
8140                 opj_event_msg_v2(p_manager, EVT_INFO, "Image data has been updated with tile %d.\n\n", l_current_tile_no + 1);
8141
8142         }
8143
8144         opj_free(l_current_data);
8145
8146         return OPJ_TRUE;
8147 }
8148
8149 /**
8150  * Sets up the procedures to do on decoding data. Developpers wanting to extend the library can add their own reading procedures.
8151  */
8152 void j2k_setup_decoding (opj_j2k_v2_t *p_j2k)
8153 {
8154         // preconditions
8155         assert(p_j2k != 00);
8156
8157         opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(void*)j2k_decode_tiles);
8158         /* DEVELOPER CORNER, add your custom procedures */
8159
8160 }
8161
8162
8163 /**
8164  * Decodes the tiles of the stream.
8165  */
8166 opj_bool j2k_decode_v2( opj_j2k_v2_t * p_j2k,
8167                                                 opj_stream_private_t * p_stream,
8168                                                 opj_image_t * p_image,
8169                                                 opj_event_mgr_t * p_manager)
8170 {
8171         OPJ_UINT32 compno;
8172
8173         if (!p_image)
8174                 return OPJ_FALSE;
8175
8176         p_j2k->m_output_image = opj_image_create0();
8177         if (! (p_j2k->m_output_image)) {
8178                 return OPJ_FALSE;
8179         }
8180         opj_copy_image_header(p_image, p_j2k->m_output_image);
8181
8182         /* customization of the decoding */
8183         j2k_setup_decoding(p_j2k);
8184
8185         /* Decode the codestream */
8186         if (! j2k_exec (p_j2k,p_j2k->m_procedure_list,p_stream,p_manager)) {
8187                 opj_image_destroy(p_j2k->m_private_image);
8188                 p_j2k->m_private_image = NULL;
8189                 return OPJ_FALSE;
8190         }
8191
8192         /* Move data and copy one information from codec to output image*/
8193         for (compno = 0; compno < p_image->numcomps; compno++) {
8194                 p_image->comps[compno].resno_decoded = p_j2k->m_output_image->comps[compno].resno_decoded;
8195                 p_image->comps[compno].data = p_j2k->m_output_image->comps[compno].data;
8196                 p_j2k->m_output_image->comps[compno].data = NULL;
8197         }
8198
8199         return OPJ_TRUE;
8200 }