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