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