54bd05bd11799bc97e6111972bf7ba6382fb12ff
[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-2012, 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 static opj_bool j2k_read_header_procedure(  opj_j2k_v2_t *p_j2k,
53                                             opj_stream_private_t *p_stream,
54                                             opj_event_mgr_t * p_manager);
55
56 /**
57  * The default encoding validation procedure without any extension.
58  *
59  * @param       p_j2k                   the jpeg2000 codec to validate.
60  * @param       p_stream                the input stream to validate.
61  * @param       p_manager               the user event manager.
62  *
63  * @return true if the parameters are correct.
64  */
65 static opj_bool j2k_encoding_validation (   opj_j2k_v2_t * p_j2k,
66                                             opj_stream_private_t *p_stream,
67                                             opj_event_mgr_t * p_manager );
68
69 /**
70  * The default decoding validation procedure without any extension.
71  *
72  * @param       p_j2k                   the jpeg2000 codec to validate.
73  * @param       p_stream                                the input stream to validate.
74  * @param       p_manager               the user event manager.
75  *
76  * @return true if the parameters are correct.
77  */
78 static opj_bool j2k_decoding_validation (   opj_j2k_v2_t * p_j2k,
79                                             opj_stream_private_t *p_stream,
80                                             opj_event_mgr_t * p_manager );
81
82 /**
83  * Sets up the validation ,i.e. adds the procedures to lauch to make sure the codec parameters
84  * are valid. Developpers wanting to extend the library can add their own validation procedures.
85  */
86 static void j2k_setup_encoding_validation (opj_j2k_v2_t *p_j2k);
87
88 /**
89  * Sets up the validation ,i.e. adds the procedures to lauch to make sure the codec parameters
90  * are valid. Developpers wanting to extend the library can add their own validation procedures.
91  */
92 static void j2k_setup_decoding_validation (opj_j2k_v2_t *p_j2k);
93
94 /**
95  * Sets up the validation ,i.e. adds the procedures to lauch to make sure the codec parameters
96  * are valid. Developpers wanting to extend the library can add their own validation procedures.
97  */
98 static void j2k_setup_end_compress (opj_j2k_v2_t *p_j2k);
99
100 /**
101  * The mct encoding validation procedure.
102  *
103  * @param       p_j2k                   the jpeg2000 codec to validate.
104  * @param       p_stream                                the input stream to validate.
105  * @param       p_manager               the user event manager.
106  *
107  * @return true if the parameters are correct.
108  */
109 static opj_bool j2k_mct_validation (opj_j2k_v2_t * p_j2k,
110                                     opj_stream_private_t *p_stream,
111                                     opj_event_mgr_t * p_manager );
112
113 /**
114  * Builds the tcd decoder to use to decode tile.
115  */
116 static opj_bool j2k_build_decoder ( opj_j2k_v2_t * p_j2k,
117                                     opj_stream_private_t *p_stream,
118                                     opj_event_mgr_t * p_manager );
119 /**
120  * Builds the tcd encoder to use to encode tile.
121  */
122 static opj_bool j2k_build_encoder ( opj_j2k_v2_t * p_j2k,
123                                     opj_stream_private_t *p_stream,
124                                     opj_event_mgr_t * p_manager );
125
126 /**
127  * Creates a tile-coder decoder.
128  *
129  * @param       p_stream                        the stream to write data to.
130  * @param       p_j2k                           J2K codec.
131  * @param       p_manager                   the user event manager.
132 */
133 static opj_bool j2k_create_tcd( opj_j2k_v2_t *p_j2k,
134                                                                 struct opj_stream_private *p_stream,
135                                                                 struct opj_event_mgr * p_manager );
136
137 /**
138  * Excutes the given procedures on the given codec.
139  *
140  * @param       p_procedure_list        the list of procedures to execute
141  * @param       p_j2k                           the jpeg2000 codec to execute the procedures on.
142  * @param       p_stream                        the stream to execute the procedures on.
143  * @param       p_manager                       the user manager.
144  *
145  * @return      true                            if all the procedures were successfully executed.
146  */
147 static opj_bool j2k_exec (  opj_j2k_v2_t * p_j2k,
148                             opj_procedure_list_t * p_procedure_list,
149                             opj_stream_private_t *p_stream,
150                             opj_event_mgr_t * p_manager);
151
152 /**
153  * Updates the rates of the tcp.
154  *
155  * @param       p_stream                                the stream to write data to.
156  * @param       p_j2k                           J2K codec.
157  * @param       p_manager               the user event manager.
158 */
159 static opj_bool j2k_update_rates(       opj_j2k_v2_t *p_j2k,
160                                                                         opj_stream_private_t *p_stream,
161                                                                         opj_event_mgr_t * p_manager );
162
163 /**
164  * Copies the decoding tile parameters onto all the tile parameters.
165  * Creates also the tile decoder.
166  */
167 static opj_bool j2k_copy_default_tcp_and_create_tcd (   opj_j2k_v2_t * p_j2k,
168                                                         opj_stream_private_t *p_stream,
169                                                         opj_event_mgr_t * p_manager );
170
171 /**
172  * Destroys the memory associated with the decoding of headers.
173  */
174 static opj_bool j2k_destroy_header_memory ( opj_j2k_v2_t * p_j2k,
175                                             opj_stream_private_t *p_stream,
176                                             opj_event_mgr_t * p_manager );
177
178 /**
179  * Reads the lookup table containing all the marker, status and action, and returns the handler associated
180  * with the marker value.
181  * @param       p_id            Marker value to look up
182  *
183  * @return      the handler associated with the id.
184 */
185 static const struct opj_dec_memory_marker_handler * j2k_get_marker_handler (OPJ_UINT32 p_id);
186
187 /**
188  * Destroys a tile coding parameter structure.
189  *
190  * @param       p_tcp           the tile coding parameter to destroy.
191  */
192 static void j2k_tcp_destroy (opj_tcp_v2_t *p_tcp);
193
194 /**
195  * Destroys the data inside a tile coding parameter structure.
196  *
197  * @param       p_tcp           the tile coding parameter which contain data to destroy.
198  */
199 static void j2k_tcp_data_destroy (opj_tcp_v2_t *p_tcp);
200
201 /**
202  * Destroys a coding parameter structure.
203  *
204  * @param       p_cp            the coding parameter to destroy.
205  */
206 static void j2k_cp_destroy (opj_cp_v2_t *p_cp);
207
208
209 /**
210  * Writes a SPCod or SPCoc element, i.e. the coding style of a given component of a tile.
211  *
212  * @param       p_comp_no       the component number to output.
213  * @param       p_stream                        the stream to write data to.
214  * @param       p_j2k                   J2K codec.
215  * @param       p_manager       the user event manager.
216  *
217 */
218 static opj_bool j2k_write_SPCod_SPCoc(  opj_j2k_v2_t *p_j2k,
219                                                                                 OPJ_UINT32 p_tile_no,
220                                                                                 OPJ_UINT32 p_comp_no,
221                                                                                 OPJ_BYTE * p_data,
222                                                                                 OPJ_UINT32 * p_header_size,
223                                                                                 struct opj_event_mgr * p_manager );
224
225 /**
226  * Gets the size taken by writing a SPCod or SPCoc for the given tile and component.
227  *
228  * @param       p_tile_no               the tile index.
229  * @param       p_comp_no               the component being outputted.
230  * @param       p_j2k                   the J2K codec.
231  *
232  * @return      the number of bytes taken by the SPCod element.
233  */
234 static OPJ_UINT32 j2k_get_SPCod_SPCoc_size (opj_j2k_v2_t *p_j2k,
235                                                                                         OPJ_UINT32 p_tile_no,
236                                                                                         OPJ_UINT32 p_comp_no );
237
238 /**
239  * Reads a SPCod or SPCoc element, i.e. the coding style of a given component of a tile.
240  * @param       p_header_data   the data contained in the COM box.
241  * @param       p_j2k                   the jpeg2000 codec.
242  * @param       p_header_size   the size of the data contained in the COM marker.
243  * @param       p_manager               the user event manager.
244 */
245 static opj_bool j2k_read_SPCod_SPCoc(   opj_j2k_v2_t *p_j2k,
246                                         OPJ_UINT32 compno,
247                                         OPJ_BYTE * p_header_data,
248                                         OPJ_UINT32 * p_header_size,
249                                         opj_event_mgr_t * p_manager );
250
251 /**
252  * Gets the size taken by writing SQcd or SQcc element, i.e. the quantization values of a band in the QCD or QCC.
253  *
254  * @param       p_tile_no               the tile index.
255  * @param       p_comp_no               the component being outputted.
256  * @param       p_j2k                   the J2K codec.
257  *
258  * @return      the number of bytes taken by the SPCod element.
259  */
260 static OPJ_UINT32 j2k_get_SQcd_SQcc_size (      opj_j2k_v2_t *p_j2k,
261                                                                                 OPJ_UINT32 p_tile_no,
262                                                                                 OPJ_UINT32 p_comp_no );
263
264 /**
265  * Writes a SQcd or SQcc element, i.e. the quantization values of a band in the QCD or QCC.
266  *
267  * @param       p_tile_no               the tile to output.
268  * @param       p_comp_no               the component number to output.
269  * @param       p_data                  the data buffer.
270  * @param       p_header_size   pointer to the size of the data buffer, it is changed by the function.
271  * @param       p_j2k                   J2K codec.
272  * @param       p_manager               the user event manager.
273  *
274 */
275 static opj_bool j2k_write_SQcd_SQcc(opj_j2k_v2_t *p_j2k,
276                                                                         OPJ_UINT32 p_tile_no,
277                                                                         OPJ_UINT32 p_comp_no,
278                                                                         OPJ_BYTE * p_data,
279                                                                         OPJ_UINT32 * p_header_size,
280                                                                         opj_event_mgr_t * p_manager);
281
282 /**
283  * Updates the Tile Length Marker.
284  */
285 static void j2k_update_tlm ( opj_j2k_v2_t * p_j2k, OPJ_UINT32 p_tile_part_size);
286
287 /**
288  * Reads a SQcd or SQcc element, i.e. the quantization values of a band in the QCD or QCC.
289  *
290  * @param       p_tile_no               the tile to output.
291  * @param       p_comp_no               the component number to output.
292  * @param       p_data                  the data buffer.
293  * @param       p_header_size   pointer to the size of the data buffer, it is changed by the function.
294  * @param       p_j2k                           J2K codec.
295  * @param       p_manager               the user event manager.
296  *
297 */
298 static opj_bool j2k_read_SQcd_SQcc( opj_j2k_v2_t *p_j2k,
299                                     OPJ_UINT32 compno,
300                                     OPJ_BYTE * p_header_data,
301                                     OPJ_UINT32 * p_header_size,
302                                     opj_event_mgr_t * p_manager );
303
304 /**
305  * Copies the tile component parameters of all the component from the first tile component.
306  *
307  * @param               p_j2k           the J2k codec.
308  */
309 static void j2k_copy_tile_component_parameters( opj_j2k_v2_t *p_j2k );
310
311 /**
312  * Copies the tile quantization parameters of all the component from the first tile component.
313  *
314  * @param               p_j2k           the J2k codec.
315  */
316 static void j2k_copy_tile_quantization_parameters( opj_j2k_v2_t *p_j2k );
317
318 /**
319  * Reads the tiles.
320  */
321 static opj_bool j2k_decode_tiles (      opj_j2k_v2_t *p_j2k,
322                                     opj_stream_private_t *p_stream,
323                                     opj_event_mgr_t * p_manager);
324
325
326 static opj_bool j2k_pre_write_tile ( opj_j2k_v2_t * p_j2k,
327                                                                          OPJ_UINT32 p_tile_index,
328                                                                          opj_stream_private_t *p_stream,
329                                                                          opj_event_mgr_t * p_manager );
330
331 static opj_bool j2k_update_image_data (opj_tcd_v2_t * p_tcd, OPJ_BYTE * p_data, opj_image_t* p_output_image);
332
333 static void j2k_get_tile_data (opj_tcd_v2_t * p_tcd, OPJ_BYTE * p_data);
334
335 static opj_bool j2k_post_write_tile (opj_j2k_v2_t * p_j2k,
336                                                                          OPJ_BYTE * p_data,
337                                                                          OPJ_UINT32 p_data_size,
338                                                                          opj_stream_private_t *p_stream,
339                                                                          opj_event_mgr_t * p_manager );
340
341 /**
342  * Sets up the procedures to do on writing header.
343  * Developers wanting to extend the library can add their own writing procedures.
344  */
345 static void j2k_setup_header_writting (opj_j2k_v2_t *p_j2k);
346
347 static opj_bool j2k_write_first_tile_part(      opj_j2k_v2_t *p_j2k,
348                                                                                         OPJ_BYTE * p_data,
349                                                                                         OPJ_UINT32 * p_data_written,
350                                                                                         OPJ_UINT32 p_total_data_size,
351                                                                                         opj_stream_private_t *p_stream,
352                                                                                         struct opj_event_mgr * p_manager );
353
354 static opj_bool j2k_write_all_tile_parts(       opj_j2k_v2_t *p_j2k,
355                                                                                         OPJ_BYTE * p_data,
356                                                                                         OPJ_UINT32 * p_data_written,
357                                                                                         OPJ_UINT32 p_total_data_size,
358                                                                                         opj_stream_private_t *p_stream,
359                                                                                         struct opj_event_mgr * p_manager );
360
361 /**
362  * Gets the offset of the header.
363  *
364  * @param       p_stream                the stream to write data to.
365  * @param       p_j2k                   J2K codec.
366  * @param       p_manager               the user event manager.
367 */
368 static opj_bool j2k_get_end_header(     opj_j2k_v2_t *p_j2k,
369                                                                         struct opj_stream_private *p_stream,
370                                                                         struct opj_event_mgr * p_manager );
371
372 /*
373  * -----------------------------------------------------------------------
374  * -----------------------------------------------------------------------
375  * -----------------------------------------------------------------------
376  */
377
378 /**
379 Write the SOC marker (Start Of Codestream)
380 @param j2k J2K handle
381 */
382 static void j2k_write_soc(opj_j2k_t *j2k);
383
384 /**
385  * Writes the SOC marker (Start Of Codestream)
386  *
387  * @param       p_stream                        the stream to write data to.
388  * @param       p_j2k                   J2K codec.
389  * @param       p_manager       the user event manager.
390 */
391 static opj_bool j2k_write_soc_v2(       opj_j2k_v2_t *p_j2k,
392                                                                         struct opj_stream_private *p_stream,
393                                                                         struct opj_event_mgr * p_manager );
394
395 /**
396  * Reads a SOC marker (Start of Codestream)
397  * @param       p_header_data   the data contained in the SOC box.
398  * @param       jp2                             the jpeg2000 file codec.
399  * @param       p_header_size   the size of the data contained in the SOC marker.
400  * @param       p_manager               the user event manager.
401 */
402 static opj_bool opj_j2k_read_soc(
403                                         opj_j2k_v2_t *p_j2k,
404                                         struct opj_stream_private *p_stream,
405                                         struct opj_event_mgr * p_manager
406                                  );
407
408 /**
409 Write the SIZ marker (image and tile size)
410 @param j2k J2K handle
411 */
412 static void j2k_write_siz(opj_j2k_t *j2k);
413
414 /**
415  * Writes the SIZ marker (image and tile size)
416  *
417  * @param       p_stream                        the stream to write data to.
418  * @param       p_j2k                   J2K codec.
419  * @param       p_manager       the user event manager.
420 */
421 static opj_bool j2k_write_siz_v2(       opj_j2k_v2_t *p_j2k,
422                                                                         struct opj_stream_private *p_stream,
423                                                                         struct opj_event_mgr * p_manager );
424
425 /**
426 Read the SIZ marker (image and tile size)
427 @param j2k J2K handle
428 */
429 static void j2k_read_siz(opj_j2k_t *j2k);
430
431 /**
432  * Reads a SIZ marker (image and tile size)
433  * @param       p_header_data   the data contained in the SIZ box.
434  * @param       jp2                             the jpeg2000 file codec.
435  * @param       p_header_size   the size of the data contained in the SIZ marker.
436  * @param       p_manager               the user event manager.
437 */
438 static opj_bool j2k_read_siz_v2 (
439                                                   opj_j2k_v2_t *p_j2k,
440                                                   OPJ_BYTE * p_header_data,
441                                                   OPJ_UINT32 p_header_size,
442                                                   struct opj_event_mgr * p_manager
443                                         );
444
445 /**
446 Write the COM marker (comment)
447 @param j2k J2K handle
448 */
449 static void j2k_write_com(opj_j2k_t *j2k);
450
451 /**
452  * Writes the COM marker (comment)
453  * 
454  * @param       p_stream                        the stream to write data to.
455  * @param       p_j2k                   J2K codec.
456  * @param       p_manager       the user event manager.
457 */
458 static opj_bool j2k_write_com_v2(       opj_j2k_v2_t *p_j2k,
459                                                                         struct opj_stream_private *p_stream,
460                                                                         struct opj_event_mgr * p_manager );
461
462 /**
463 Read the COM marker (comment)
464 @param j2k J2K handle
465 */
466 static void j2k_read_com(opj_j2k_t *j2k);
467 /**
468  * Reads a COM marker (comments)
469  * @param       p_header_data   the data contained in the COM box.
470  * @param       jp2                             the jpeg2000 file codec.
471  * @param       p_header_size   the size of the data contained in the COM marker.
472  * @param       p_manager               the user event manager.
473 */
474 static opj_bool j2k_read_com_v2 (
475                                         opj_j2k_v2_t *p_j2k,
476                                         OPJ_BYTE * p_header_data,
477                                         OPJ_UINT32 p_header_size,
478                                         struct opj_event_mgr * p_manager
479                                         );
480 /**
481 Write the value concerning the specified component in the marker COD and COC
482 @param j2k J2K handle
483 @param compno Number of the component concerned by the information written
484 */
485 static void j2k_write_cox(opj_j2k_t *j2k, int compno);
486 /**
487 Read the value concerning the specified component in the marker COD and COC
488 @param j2k J2K handle
489 @param compno Number of the component concerned by the information read
490 */
491 static void j2k_read_cox(opj_j2k_t *j2k, int compno);
492 /**
493 Write the COD marker (coding style default)
494 @param j2k J2K handle
495 */
496 static void j2k_write_cod(opj_j2k_t *j2k);
497
498 /**
499  * Writes the COD marker (Coding style default)
500  *
501  * @param       p_stream                        the stream to write data to.
502  * @param       p_j2k                   J2K codec.
503  * @param       p_manager       the user event manager.
504 */
505 static opj_bool j2k_write_cod_v2(       opj_j2k_v2_t *p_j2k,
506                                                                         struct opj_stream_private *p_stream,
507                                                                         struct opj_event_mgr * p_manager );
508
509 /**
510 Read the COD marker (coding style default)
511 @param j2k J2K handle
512 */
513 static void j2k_read_cod(opj_j2k_t *j2k);
514
515 /**
516  * Reads a COD marker (Coding Styke defaults)
517  * @param       p_header_data   the data contained in the COD box.
518  * @param       p_j2k                   the jpeg2000 codec.
519  * @param       p_header_size   the size of the data contained in the COD marker.
520  * @param       p_manager               the user event manager.
521 */
522 static opj_bool j2k_read_cod_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 /**
530 Write the COC marker (coding style component)
531 @param j2k J2K handle
532 @param compno Number of the component concerned by the information written
533 */
534 static void j2k_write_coc(opj_j2k_t *j2k, int compno);
535
536 /**
537  * Writes the COC marker (Coding style component)
538  *
539  * @param       p_comp_number   the index of the component to output.
540  * @param       p_stream                                the stream to write data to.
541  * @param       p_j2k                           J2K codec.
542  * @param       p_manager               the user event manager.
543 */
544 static opj_bool j2k_write_coc_v2( opj_j2k_v2_t *p_j2k,
545                                                         OPJ_UINT32 p_comp_number,
546                                                         struct opj_stream_private *p_stream,
547                                                         struct opj_event_mgr * p_manager
548                                                   );
549 /**
550  * Writes the COC marker (Coding style component)
551  *
552  * @param       p_comp_no               the index of the component to output.
553  * @param       p_stream                the stream to write data to.
554  * @param       p_j2k                   J2K codec.
555  * @param       p_manager               the user event manager.
556 */
557 static void j2k_write_coc_in_memory(opj_j2k_v2_t *p_j2k,
558                                                                         OPJ_UINT32 p_comp_no,
559                                                                         OPJ_BYTE * p_data,
560                                                                         OPJ_UINT32 * p_data_written,
561                                                                         struct opj_event_mgr * p_manager );
562
563 /**
564  * Gets the maximum size taken by a coc.
565  *
566  * @param       p_j2k   the jpeg2000 codec to use.
567  */
568 static OPJ_UINT32 j2k_get_max_coc_size(opj_j2k_v2_t *p_j2k);
569
570 /**
571 Read the COC marker (coding style component)
572 @param j2k J2K handle
573 */
574 static void j2k_read_coc(opj_j2k_t *j2k);
575
576 /**
577  * Reads a COC marker (Coding Style Component)
578  * @param       p_header_data   the data contained in the COC box.
579  * @param       p_j2k                   the jpeg2000 codec.
580  * @param       p_header_size   the size of the data contained in the COC marker.
581  * @param       p_manager               the user event manager.
582 */
583 static opj_bool j2k_read_coc_v2 (
584                                         opj_j2k_v2_t *p_j2k,
585                                         OPJ_BYTE * p_header_data,
586                                         OPJ_UINT32 p_header_size,
587                                         struct opj_event_mgr * p_manager
588                                         );
589
590 /**
591 Write the value concerning the specified component in the marker QCD and QCC
592 @param j2k J2K handle
593 @param compno Number of the component concerned by the information written
594 */
595 static void j2k_write_qcx(opj_j2k_t *j2k, int compno);
596 /**
597 Read the value concerning the specified component in the marker QCD and QCC
598 @param j2k J2K handle
599 @param compno Number of the component concern by the information read
600 @param len Length of the information in the QCX part of the marker QCD/QCC
601 */
602 static void j2k_read_qcx(opj_j2k_t *j2k, int compno, int len);
603 /**
604 Write the QCD marker (quantization default)
605 @param j2k J2K handle
606 */
607 static void j2k_write_qcd(opj_j2k_t *j2k);
608
609 /**
610  * Writes the QCD marker (quantization default)
611  *
612  * @param       p_comp_number   the index of the component to output.
613  * @param       p_stream                the stream to write data to.
614  * @param       p_j2k                   J2K codec.
615  * @param       p_manager               the user event manager.
616 */
617 static opj_bool j2k_write_qcd_v2(       opj_j2k_v2_t *p_j2k,
618                                                                         struct opj_stream_private *p_stream,
619                                                                         struct opj_event_mgr * p_manager );
620
621 /**
622 Read the QCD marker (quantization default)
623 @param j2k J2K handle
624 */
625 static void j2k_read_qcd(opj_j2k_t *j2k);
626
627 /**
628  * Reads a QCD marker (Quantization defaults)
629  * @param       p_header_data   the data contained in the QCD box.
630  * @param       p_j2k                   the jpeg2000 codec.
631  * @param       p_header_size   the size of the data contained in the QCD marker.
632  * @param       p_manager               the user event manager.
633 */
634 static opj_bool j2k_read_qcd_v2 (
635                                         opj_j2k_v2_t *p_j2k,
636                                         OPJ_BYTE * p_header_data,
637                                         OPJ_UINT32 p_header_size,
638                                         struct opj_event_mgr * p_manager
639                                         );
640
641 /**
642 Write the QCC marker (quantization component)
643 @param j2k J2K handle
644 @param compno Number of the component concerned by the information written
645 */
646 static void j2k_write_qcc(opj_j2k_t *j2k, int compno);
647
648 /**
649  * Writes the QCC marker (quantization component)
650  *
651  * @param       p_comp_no       the index of the component to output.
652  * @param       p_stream                the stream to write data to.
653  * @param       p_j2k                   J2K codec.
654  * @param       p_manager               the user event manager.
655 */
656 static opj_bool j2k_write_qcc_v2(       opj_j2k_v2_t *p_j2k,
657                                                                         OPJ_UINT32 p_comp_no,
658                                                                         struct opj_stream_private *p_stream,
659                                                                         struct opj_event_mgr * p_manager );
660
661 /**
662  * Writes the QCC marker (quantization component)
663  *
664  * @param       p_comp_no       the index of the component to output.
665  * @param       p_stream                the stream to write data to.
666  * @param       p_j2k                   J2K codec.
667  * @param       p_manager               the user event manager.
668 */
669 static void j2k_write_qcc_in_memory(opj_j2k_v2_t *p_j2k,
670                                                                         OPJ_UINT32 p_comp_no,
671                                                                         OPJ_BYTE * p_data,
672                                                                         OPJ_UINT32 * p_data_written,
673                                                                         struct opj_event_mgr * p_manager );
674
675 /**
676  * Gets the maximum size taken by a qcc.
677  */
678 static OPJ_UINT32 j2k_get_max_qcc_size (opj_j2k_v2_t *p_j2k);
679
680 /**
681 Read the QCC marker (quantization component)
682 @param j2k J2K handle
683 */
684 static void j2k_read_qcc(opj_j2k_t *j2k);
685 /**
686  * Reads a QCC marker (Quantization component)
687  * @param       p_header_data   the data contained in the QCC box.
688  * @param       p_j2k                   the jpeg2000 codec.
689  * @param       p_header_size   the size of the data contained in the QCC marker.
690  * @param       p_manager               the user event manager.
691 */
692 static opj_bool j2k_read_qcc_v2(
693                                                         opj_j2k_v2_t *p_j2k,
694                                                         OPJ_BYTE * p_header_data,
695                                                         OPJ_UINT32 p_header_size,
696                                                         struct opj_event_mgr * p_manager);
697
698 /**
699 Write the POC marker (progression order change)
700 @param j2k J2K handle
701 */
702 static void j2k_write_poc(opj_j2k_t *j2k);
703
704 /**
705  * Writes the POC marker (Progression Order Change)
706  * 
707  * @param       p_stream                                the stream to write data to.
708  * @param       p_j2k                           J2K codec.
709  * @param       p_manager               the user event manager.
710 */
711 static opj_bool j2k_write_poc_v2(       opj_j2k_v2_t *p_j2k,
712                                                                         struct opj_stream_private *p_stream,
713                                                                         struct opj_event_mgr * p_manager );
714
715 /**
716  * Writes the POC marker (Progression Order Change)
717  *
718  * @param       p_stream                the stream to write data to.
719  * @param       p_j2k                   J2K codec.
720  * @param       p_manager               the user event manager.
721  */
722 static void j2k_write_poc_in_memory(opj_j2k_v2_t *p_j2k,
723                                                                         OPJ_BYTE * p_data,
724                                                                         OPJ_UINT32 * p_data_written,
725                                                                         struct opj_event_mgr * p_manager );
726
727 /**
728  * Gets the maximum size taken by the writting of a POC.
729  */
730 static OPJ_UINT32 j2k_get_max_poc_size(opj_j2k_v2_t *p_j2k);
731
732 /**
733  * Gets the maximum size taken by the toc headers of all the tile parts of any given tile.
734  */
735 static OPJ_UINT32 j2k_get_max_toc_size (opj_j2k_v2_t *p_j2k);
736
737 /**
738  * Gets the maximum size taken by the headers of the SOT.
739  *
740  * @param       p_j2k   the jpeg2000 codec to use.
741  */
742 static OPJ_UINT32 j2k_get_specific_header_sizes(opj_j2k_v2_t *p_j2k);
743
744 /**
745 Read the POC marker (progression order change)
746 @param j2k J2K handle
747 */
748 static void j2k_read_poc(opj_j2k_t *j2k);
749 /**
750  * Reads a POC marker (Progression Order Change)
751  *
752  * @param       p_header_data   the data contained in the POC box.
753  * @param       p_j2k                   the jpeg2000 codec.
754  * @param       p_header_size   the size of the data contained in the POC marker.
755  * @param       p_manager               the user event manager.
756 */
757 static opj_bool j2k_read_poc_v2 (
758                                                 opj_j2k_v2_t *p_j2k,
759                                                 OPJ_BYTE * p_header_data,
760                                                 OPJ_UINT32 p_header_size,
761                                                 struct opj_event_mgr * p_manager
762                                         );
763
764 /**
765 Read the CRG marker (component registration)
766 @param j2k J2K handle
767 */
768 static void j2k_read_crg(opj_j2k_t *j2k);
769 /**
770  * Reads a CRG marker (Component registration)
771  *
772  * @param       p_header_data   the data contained in the TLM box.
773  * @param       p_j2k                   the jpeg2000 codec.
774  * @param       p_header_size   the size of the data contained in the TLM marker.
775  * @param       p_manager               the user event manager.
776 */
777 static opj_bool j2k_read_crg_v2 (
778                                                 opj_j2k_v2_t *p_j2k,
779                                                 OPJ_BYTE * p_header_data,
780                                                 OPJ_UINT32 p_header_size,
781                                                 struct opj_event_mgr * p_manager
782                                         );
783 /**
784 Read the TLM marker (tile-part lengths)
785 @param j2k J2K handle
786 */
787 static void j2k_read_tlm(opj_j2k_t *j2k);
788 /**
789  * Reads a TLM marker (Tile Length Marker)
790  *
791  * @param       p_header_data   the data contained in the TLM box.
792  * @param       p_j2k                   the jpeg2000 codec.
793  * @param       p_header_size   the size of the data contained in the TLM marker.
794  * @param       p_manager               the user event manager.
795 */
796 static opj_bool j2k_read_tlm_v2 (
797                                                 opj_j2k_v2_t *p_j2k,
798                                                 OPJ_BYTE * p_header_data,
799                                                 OPJ_UINT32 p_header_size,
800                                                 struct opj_event_mgr * p_manager
801                                         );
802
803 /**
804  * Writes the updated tlm.
805  *
806  * @param       p_stream                the stream to write data to.
807  * @param       p_j2k                   J2K codec.
808  * @param       p_manager               the user event manager.
809 */
810 static opj_bool j2k_write_updated_tlm(  opj_j2k_v2_t *p_j2k,
811                                                                                 struct opj_stream_private *p_stream,
812                                                                                 struct opj_event_mgr * p_manager );
813
814 /**
815 Read the PLM marker (packet length, main header)
816 @param j2k J2K handle
817 */
818 static void j2k_read_plm(opj_j2k_t *j2k);
819
820 /**
821  * Reads a PLM marker (Packet length, main header marker)
822  *
823  * @param       p_header_data   the data contained in the TLM box.
824  * @param       p_j2k                   the jpeg2000 codec.
825  * @param       p_header_size   the size of the data contained in the TLM marker.
826  * @param       p_manager               the user event manager.
827 */
828 static opj_bool j2k_read_plm_v2 (
829                                                 opj_j2k_v2_t *p_j2k,
830                                                 OPJ_BYTE * p_header_data,
831                                                 OPJ_UINT32 p_header_size,
832                                                 struct opj_event_mgr * p_manager
833                                         );
834 /**
835 Read the PLT marker (packet length, tile-part header)
836 @param j2k J2K handle
837 */
838 static void j2k_read_plt(opj_j2k_t *j2k);
839 /**
840  * Reads a PLT marker (Packet length, tile-part header)
841  *
842  * @param       p_header_data   the data contained in the PLT box.
843  * @param       p_j2k                   the jpeg2000 codec.
844  * @param       p_header_size   the size of the data contained in the PLT marker.
845  * @param       p_manager               the user event manager.
846 */
847 static opj_bool j2k_read_plt_v2 (
848                                                 opj_j2k_v2_t *p_j2k,
849                                                 OPJ_BYTE * p_header_data,
850                                                 OPJ_UINT32 p_header_size,
851                                                 struct opj_event_mgr * p_manager
852                                         );
853 /**
854 Read the PPM marker (packet packet headers, main header)
855 @param j2k J2K handle
856 */
857 static void j2k_read_ppm(opj_j2k_t *j2k);
858 /**
859  * Reads a PPM marker (Packed packet headers, main header)
860  *
861  * @param       p_header_data   the data contained in the POC box.
862  * @param       p_j2k                   the jpeg2000 codec.
863  * @param       p_header_size   the size of the data contained in the POC marker.
864  * @param       p_manager               the user event manager.
865 */
866 #if 0
867 static opj_bool j2k_read_ppm_v2 (
868                                                 opj_j2k_v2_t *p_j2k,
869                                                 OPJ_BYTE * p_header_data,
870                                                 OPJ_UINT32 p_header_size,
871                                                 struct opj_event_mgr * p_manager
872                                         );
873 #endif
874
875 static opj_bool j2k_read_ppm_v3 (
876                                                 opj_j2k_v2_t *p_j2k,
877                                                 OPJ_BYTE * p_header_data,
878                                                 OPJ_UINT32 p_header_size,
879                                                 struct opj_event_mgr * p_manager
880                                         );
881
882 /**
883 Read the PPT marker (packet packet headers, tile-part header)
884 @param j2k J2K handle
885 */
886 static void j2k_read_ppt(opj_j2k_t *j2k);
887 /**
888  * Reads a PPT marker (Packed packet headers, tile-part header)
889  *
890  * @param       p_header_data   the data contained in the PPT box.
891  * @param       p_j2k                   the jpeg2000 codec.
892  * @param       p_header_size   the size of the data contained in the PPT marker.
893  * @param       p_manager               the user event manager.
894 */
895 static opj_bool j2k_read_ppt_v2 (
896                                                 opj_j2k_v2_t *p_j2k,
897                                                 OPJ_BYTE * p_header_data,
898                                                 OPJ_UINT32 p_header_size,
899                                                 struct opj_event_mgr * p_manager
900                                         );
901 /**
902 Write the TLM marker (Mainheader)
903 @param j2k J2K handle
904 */
905 static void j2k_write_tlm(opj_j2k_t *j2k);
906
907 /**
908  * Writes the TLM marker (Tile Length Marker)
909  * 
910  * @param       p_stream                                the stream to write data to.
911  * @param       p_j2k                           J2K codec.
912  * @param       p_manager               the user event manager.
913 */
914 static opj_bool j2k_write_tlm_v2(       opj_j2k_v2_t *p_j2k,
915                                                                         struct opj_stream_private *p_stream,
916                                                                         struct opj_event_mgr * p_manager );
917
918 /**
919 Write the SOT marker (start of tile-part)
920 @param j2k J2K handle
921 */
922 static void j2k_write_sot(opj_j2k_t *j2k);
923
924 /**
925  * Writes the SOT marker (Start of tile-part)
926  *
927  * @param       p_stream                the stream to write data to.
928  * @param       p_j2k                   J2K codec.
929  * @param       p_manager               the user event manager.
930 */
931 static opj_bool j2k_write_sot_v2(       opj_j2k_v2_t *p_j2k,
932                                                                         OPJ_BYTE * p_data,
933                                                                         OPJ_UINT32 * p_data_written,
934                                                                         const struct opj_stream_private *p_stream,
935                                                                         struct opj_event_mgr * p_manager );
936
937 /**
938  * Reads a PPT marker (Packed packet headers, tile-part header)
939  *
940  * @param       p_header_data   the data contained in the PPT box.
941  * @param       p_j2k                   the jpeg2000 codec.
942  * @param       p_header_size   the size of the data contained in the PPT marker.
943  * @param       p_manager               the user event manager.
944 */
945 static opj_bool j2k_read_sot_v2 (
946                                                 opj_j2k_v2_t *p_j2k,
947                                                 OPJ_BYTE * p_header_data,
948                                                 OPJ_UINT32 p_header_size,
949                                                 struct opj_event_mgr * p_manager
950                                         );
951 /**
952 Write the SOD marker (start of data)
953 @param j2k J2K handle
954 @param tile_coder Pointer to a TCD handle
955 */
956 static void j2k_write_sod(opj_j2k_t *j2k, void *tile_coder);
957
958 /**
959  * Writes the SOD marker (Start of data)
960  *
961  * @param       p_stream                                the stream to write data to.
962  * @param       p_j2k                           J2K codec.
963  * @param       p_manager               the user event manager.
964 */
965 static opj_bool j2k_write_sod_v2(       opj_j2k_v2_t *p_j2k,
966                                                                         struct opj_tcd_v2 * p_tile_coder,
967                                                                         OPJ_BYTE * p_data,
968                                                                         OPJ_UINT32 * p_data_written,
969                                                                         OPJ_UINT32 p_total_data_size,
970                                                                         const struct opj_stream_private *p_stream,
971                                                                         struct opj_event_mgr * p_manager );
972
973 /**
974 Read the SOD marker (start of data)
975 @param j2k J2K handle
976 */
977 static void j2k_read_sod(opj_j2k_t *j2k);
978
979 /**
980  * Reads a SOD marker (Start Of Data)
981  *
982  * @param       p_header_data   the data contained in the SOD box.
983  * @param       p_j2k                   the jpeg2000 codec.
984  * @param       p_header_size   the size of the data contained in the SOD marker.
985  * @param       p_manager               the user event manager.
986 */
987 static opj_bool j2k_read_sod_v2 (
988                                                 opj_j2k_v2_t *p_j2k,
989                                                 struct opj_stream_private *p_stream,
990                                                 struct opj_event_mgr * p_manager
991                                         );
992
993 /**
994  * Updates the Tile Length Marker.
995  */
996 void j2k_update_tlm (opj_j2k_v2_t * p_j2k, OPJ_UINT32 p_tile_part_size )
997 {
998         opj_write_bytes(p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current,p_j2k->m_current_tile_number,1);                                    /* PSOT */
999         ++p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current;
1000
1001         opj_write_bytes(p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current,p_tile_part_size,4);                                        /* PSOT */
1002         p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current += 4;
1003 }
1004
1005 /**
1006 Write the RGN marker (region-of-interest)
1007 @param j2k J2K handle
1008 @param compno Number of the component concerned by the information written
1009 @param tileno Number of the tile concerned by the information written
1010 */
1011 static void j2k_write_rgn(opj_j2k_t *j2k, int compno, int tileno);
1012
1013 /**
1014  * Writes the RGN marker (Region Of Interest)
1015  *
1016  * @param       p_tile_no               the tile to output
1017  * @param       p_comp_no               the component to output
1018  * @param       p_stream                                the stream to write data to.
1019  * @param       p_j2k                           J2K codec.
1020  * @param       p_manager               the user event manager.
1021 */
1022 static opj_bool j2k_write_rgn_v2(       opj_j2k_v2_t *p_j2k,
1023                                                                         OPJ_UINT32 p_tile_no,
1024                                                                         OPJ_UINT32 p_comp_no,
1025                                                                         struct opj_stream_private *p_stream,
1026                                                                         struct opj_event_mgr * p_manager );
1027
1028 /**
1029 Read the RGN marker (region-of-interest)
1030 @param j2k J2K handle
1031 */
1032 static void j2k_read_rgn(opj_j2k_t *j2k);
1033
1034 /**
1035  * Reads a RGN marker (Region Of Interest)
1036  *
1037  * @param       p_header_data   the data contained in the POC box.
1038  * @param       p_j2k                   the jpeg2000 codec.
1039  * @param       p_header_size   the size of the data contained in the POC marker.
1040  * @param       p_manager               the user event manager.
1041 */
1042 static opj_bool j2k_read_rgn_v2 (
1043                                                 opj_j2k_v2_t *p_j2k,
1044                                                 OPJ_BYTE * p_header_data,
1045                                                 OPJ_UINT32 p_header_size,
1046                                                 struct opj_event_mgr * p_manager
1047                                         ) ;
1048
1049 /**
1050  * Writes the EOC marker (End of Codestream)
1051  * 
1052  * @param       p_stream                the stream to write data to.
1053  * @param       p_j2k                   J2K codec.
1054  * @param       p_manager               the user event manager.
1055 */
1056 static opj_bool j2k_write_eoc_v2(       opj_j2k_v2_t *p_j2k,
1057                                                                         struct opj_stream_private *p_stream,
1058                                                                         struct opj_event_mgr * p_manager );
1059
1060 /**
1061 Write the EOC marker (end of codestream)
1062 @param j2k J2K handle
1063 */
1064 static void j2k_write_eoc(opj_j2k_t *j2k);
1065 /**
1066 Read the EOC marker (end of codestream)
1067 @param j2k J2K handle
1068 */
1069 static void j2k_read_eoc(opj_j2k_t *j2k);
1070
1071 /**
1072  * Reads a EOC marker (End Of Codestream)
1073  *
1074  * @param       p_header_data   the data contained in the SOD box.
1075  * @param       p_j2k                   the jpeg2000 codec.
1076  * @param       p_header_size   the size of the data contained in the SOD marker.
1077  * @param       p_manager               the user event manager.
1078 */
1079 #if 0
1080 static opj_bool j2k_read_eoc_v2 (
1081                                             opj_j2k_v2_t *p_j2k,
1082                                                 struct opj_stream_private *p_stream,
1083                                                 struct opj_event_mgr * p_manager
1084                                         ) ;
1085 #endif
1086
1087
1088
1089 /**
1090  * Writes the CBD-MCT-MCC-MCO markers (Multi components transform)
1091  *
1092  * @param       p_stream                        the stream to write data to.
1093  * @param       p_j2k                   J2K codec.
1094  * @param       p_manager       the user event manager.
1095 */
1096 static opj_bool j2k_write_mct_data_group(       opj_j2k_v2_t *p_j2k,
1097                                                                                         struct opj_stream_private *p_stream,
1098                                                                                         struct opj_event_mgr * p_manager );
1099
1100 /**
1101  * Inits the Info
1102  *
1103  * @param       p_stream                the stream to write data to.
1104  * @param       p_j2k                   J2K codec.
1105  * @param       p_manager               the user event manager.
1106 */
1107 static opj_bool j2k_init_info(  opj_j2k_v2_t *p_j2k,
1108                                                                 struct opj_stream_private *p_stream,
1109                                                                 struct opj_event_mgr * p_manager );
1110
1111 /**
1112 Read an unknown marker
1113 @param j2k J2K handle
1114 */
1115 static void j2k_read_unk(opj_j2k_t *j2k);
1116 /**
1117 Add main header marker information
1118 @param cstr_info Codestream information structure
1119 @param type marker type
1120 @param pos byte offset of marker segment
1121 @param len length of marker segment
1122  */
1123 static void j2k_add_mhmarker(opj_codestream_info_t *cstr_info, unsigned short int type, int pos, int len);
1124
1125 static void j2k_add_mhmarker_v2(opj_codestream_index_t *cstr_index, OPJ_UINT32 type, OPJ_OFF_T pos, OPJ_UINT32 len) ;
1126 /**
1127 Add tile header marker information
1128 @param tileno tile index number
1129 @param cstr_info Codestream information structure
1130 @param type marker type
1131 @param pos byte offset of marker segment
1132 @param len length of marker segment
1133  */
1134 static void j2k_add_tlmarker( int tileno, opj_codestream_info_t *cstr_info, unsigned short int type, int pos, int len);
1135
1136 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);
1137
1138 /**
1139  * Reads an unknown marker
1140  *
1141  * @param       p_stream                the stream object to read from.
1142  * @param       p_j2k                   the jpeg2000 codec.
1143  * @param       p_manager               the user event manager.
1144  *
1145  * @return      true                    if the marker could be deduced.
1146 */
1147 static opj_bool j2k_read_unk_v2 (       opj_j2k_v2_t *p_j2k,
1148                                                                         struct opj_stream_private *p_stream,
1149                                                                         OPJ_UINT32 *output_marker,
1150                                                                         struct opj_event_mgr * p_manager );
1151
1152 /**
1153  * Writes the MCT marker (Multiple Component Transform)
1154  *
1155  * @param       p_stream                                the stream to write data to.
1156  * @param       p_j2k                           J2K codec.
1157  * @param       p_manager               the user event manager.
1158 */
1159 static opj_bool j2k_write_mct_record(   opj_j2k_v2_t *p_j2k,
1160                                                                                 opj_mct_data_t * p_mct_record,
1161                                                                                 struct opj_stream_private *p_stream,
1162                                                                                 struct opj_event_mgr * p_manager );
1163
1164 /**
1165  * Reads a MCT marker (Multiple Component Transform)
1166  *
1167  * @param       p_header_data   the data contained in the MCT box.
1168  * @param       p_j2k                   the jpeg2000 codec.
1169  * @param       p_header_size   the size of the data contained in the MCT marker.
1170  * @param       p_manager               the user event manager.
1171 */
1172 static opj_bool j2k_read_mct (  opj_j2k_v2_t *p_j2k,
1173                                                                 OPJ_BYTE * p_header_data,
1174                                                                 OPJ_UINT32 p_header_size,
1175                                                                 struct opj_event_mgr * p_manager );
1176
1177 /**
1178  * Writes the MCC marker (Multiple Component Collection)
1179  *
1180  * @param       p_stream                the stream to write data to.
1181  * @param       p_j2k                   J2K codec.
1182  * @param       p_manager               the user event manager.
1183 */
1184 static opj_bool j2k_write_mcc_record(   opj_j2k_v2_t *p_j2k,
1185                                                                                 struct opj_simple_mcc_decorrelation_data * p_mcc_record,
1186                                                                                 struct opj_stream_private *p_stream,
1187                                                                                 struct opj_event_mgr * p_manager );
1188
1189
1190 /**
1191  * Reads a MCC marker (Multiple Component Collection)
1192  *
1193  * @param       p_header_data   the data contained in the MCC box.
1194  * @param       p_j2k                   the jpeg2000 codec.
1195  * @param       p_header_size   the size of the data contained in the MCC marker.
1196  * @param       p_manager               the user event manager.
1197 */
1198 static opj_bool j2k_read_mcc (  opj_j2k_v2_t *p_j2k,
1199                                                                 OPJ_BYTE * p_header_data,
1200                                                                 OPJ_UINT32 p_header_size,
1201                                                                 struct opj_event_mgr * p_manager );
1202
1203 /**
1204  * Writes the MCO marker (Multiple component transformation ordering)
1205  *
1206  * @param       p_stream                                the stream to write data to.
1207  * @param       p_j2k                           J2K codec.
1208  * @param       p_manager               the user event manager.
1209 */
1210 static opj_bool j2k_write_mco(  opj_j2k_v2_t *p_j2k,
1211                                                                 struct opj_stream_private *p_stream,
1212                                                                 struct opj_event_mgr * p_manager );
1213
1214 /**
1215  * Reads a MCO marker (Multiple Component Transform Ordering)
1216  *
1217  * @param       p_header_data   the data contained in the MCO box.
1218  * @param       p_j2k                   the jpeg2000 codec.
1219  * @param       p_header_size   the size of the data contained in the MCO marker.
1220  * @param       p_manager               the user event manager.
1221 */
1222 static opj_bool j2k_read_mco (  opj_j2k_v2_t *p_j2k,
1223                                                                 OPJ_BYTE * p_header_data,
1224                                                                 OPJ_UINT32 p_header_size,
1225                                                                 struct opj_event_mgr * p_manager );
1226
1227 static opj_bool j2k_add_mct(opj_tcp_v2_t * p_tcp, opj_image_t * p_image, OPJ_UINT32 p_index);
1228
1229 static void  j2k_read_int16_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1230 static void  j2k_read_int32_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1231 static void  j2k_read_float32_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1232 static void  j2k_read_float64_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1233
1234 static void  j2k_read_int16_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1235 static void  j2k_read_int32_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1236 static void  j2k_read_float32_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1237 static void  j2k_read_float64_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1238
1239 static void  j2k_write_float_to_int16 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1240 static void  j2k_write_float_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1241 static void  j2k_write_float_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1242 static void  j2k_write_float_to_float64 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1243
1244
1245 /**
1246  * Ends the encoding, i.e. frees memory.
1247  *
1248  * @param       p_stream                the stream to write data to.
1249  * @param       p_j2k                   J2K codec.
1250  * @param       p_manager               the user event manager.
1251 */
1252 static opj_bool j2k_end_encoding(       opj_j2k_v2_t *p_j2k,
1253                                                                         struct opj_stream_private *p_stream,
1254                                                                         struct opj_event_mgr * p_manager );
1255
1256 /**
1257  * Writes the CBD marker (Component bit depth definition)
1258  *
1259  * @param       p_stream                                the stream to write data to.
1260  * @param       p_j2k                           J2K codec.
1261  * @param       p_manager               the user event manager.
1262 */
1263 static opj_bool j2k_write_cbd(  opj_j2k_v2_t *p_j2k,
1264                                                                 struct opj_stream_private *p_stream,
1265                                                                 struct opj_event_mgr * p_manager );
1266
1267 /**
1268  * Reads a CBD marker (Component bit depth definition)
1269  * @param       p_header_data   the data contained in the CBD box.
1270  * @param       p_j2k                   the jpeg2000 codec.
1271  * @param       p_header_size   the size of the data contained in the CBD marker.
1272  * @param       p_manager               the user event manager.
1273 */
1274 static opj_bool j2k_read_cbd (  opj_j2k_v2_t *p_j2k,
1275                                                         OPJ_BYTE * p_header_data,
1276                                                         OPJ_UINT32 p_header_size,
1277                                                         struct opj_event_mgr * p_manager);
1278
1279 /**
1280  * Writes the image components.
1281  *
1282  * @param       p_stream                the stream to write data to.
1283  * @param       p_j2k                   J2K codec.
1284  * @param       p_manager               the user event manager.
1285 */
1286 static opj_bool j2k_write_image_components(     opj_j2k_v2_t *p_j2k,
1287                                                                                         struct opj_stream_private *p_stream,
1288                                                                                         struct opj_event_mgr * p_manager );
1289
1290 /**
1291  * Writes regions of interests.
1292  *
1293  * @param       p_stream                the stream to write data to.
1294  * @param       p_j2k                   J2K codec.
1295  * @param       p_manager               the user event manager.
1296 */
1297 static opj_bool j2k_write_regions(      opj_j2k_v2_t *p_j2k,
1298                                                                         struct opj_stream_private *p_stream,
1299                                                                         struct opj_event_mgr * p_manager );
1300
1301 /**
1302  * Writes EPC ????
1303  *
1304  * @param       p_stream                the stream to write data to.
1305  * @param       p_j2k                   J2K codec.
1306  * @param       p_manager               the user event manager.
1307 */
1308 static opj_bool j2k_write_epc(  opj_j2k_v2_t *p_j2k,
1309                                                                 struct opj_stream_private *p_stream,
1310                                                                 struct opj_event_mgr * p_manager );
1311
1312 /**
1313  * Checks the progression order changes values. Tells of the poc given as input are valid.
1314  * A nice message is outputted at errors.
1315  *
1316  * @param       p_pocs                          the progression order changes.
1317  * @param       p_nb_pocs                       the number of progression order changes.
1318  * @param       p_nb_resolutions        the number of resolutions.
1319  * @param       numcomps                        the number of components
1320  * @param       numlayers                       the number of layers.
1321  *
1322  * @return      true if the pocs are valid.
1323  */
1324 static opj_bool j2k_check_poc_val(      const opj_poc_t *p_pocs,
1325                                                                         OPJ_UINT32 p_nb_pocs,
1326                                                                         OPJ_UINT32 p_nb_resolutions,
1327                                                                         OPJ_UINT32 numcomps,
1328                                                                         OPJ_UINT32 numlayers,
1329                                                                         opj_event_mgr_t * p_manager);
1330
1331 /**
1332  * Gets the number of tile parts used for the given change of progression (if any) and the given tile.
1333  *
1334  * @param               cp                      the coding parameters.
1335  * @param               pino            the offset of the given poc (i.e. its position in the coding parameter).
1336  * @param               tileno          the given tile.
1337  *
1338  * @return              the number of tile parts.
1339  */
1340 static OPJ_UINT32 j2k_get_num_tp_v2( opj_cp_v2_t *cp, OPJ_UINT32 pino, OPJ_UINT32 tileno);
1341
1342 /**     mem allocation for TLM marker*/
1343 static int j2k_calculate_tp(opj_cp_t *cp,int img_numcomp,opj_image_t *image,opj_j2k_t *j2k );
1344
1345 /**
1346  * Calculates the total number of tile parts needed by the encoder to
1347  * encode such an image. If not enough memory is available, then the function return false.
1348  *
1349  * @param       p_nb_tiles      pointer that will hold the number of tile parts.
1350  * @param       cp                      the coding parameters for the image.
1351  * @param       image           the image to encode.
1352  * @param       p_j2k                   the p_j2k encoder.
1353  * @param       p_manager       the user event manager.
1354  *
1355  * @return true if the function was successful, false else.
1356  */
1357 static opj_bool j2k_calculate_tp_v2(opj_j2k_v2_t *p_j2k,
1358                                                                         opj_cp_v2_t *cp,
1359                                                                         OPJ_UINT32 * p_nb_tiles,
1360                                                                         opj_image_t *image,
1361                                                                         opj_event_mgr_t * p_manager);
1362
1363 static void j2k_dump_MH_info(opj_j2k_v2_t* p_j2k, FILE* out_stream);
1364
1365 static void j2k_dump_MH_index(opj_j2k_v2_t* p_j2k, FILE* out_stream);
1366
1367 static opj_codestream_index_t* j2k_create_cstr_index(void);
1368
1369 /*@}*/
1370
1371 /*@}*/
1372
1373 /* ----------------------------------------------------------------------- */
1374 typedef struct j2k_prog_order{
1375         OPJ_PROG_ORDER enum_prog;
1376         char str_prog[5];
1377 }j2k_prog_order_t;
1378
1379 j2k_prog_order_t j2k_prog_order_list[] = {
1380         {CPRL, "CPRL"},
1381         {LRCP, "LRCP"},
1382         {PCRL, "PCRL"},
1383         {RLCP, "RLCP"},
1384         {RPCL, "RPCL"},
1385         {(OPJ_PROG_ORDER)-1, ""}
1386 };
1387
1388
1389
1390 /**
1391  * FIXME DOC
1392  */
1393 const OPJ_UINT32 MCT_ELEMENT_SIZE [] =
1394 {
1395         2,
1396         4,
1397         4,
1398         8
1399 };
1400
1401 typedef void (* j2k_mct_function) (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1402
1403 const j2k_mct_function j2k_mct_read_functions_to_float [] =
1404 {
1405         j2k_read_int16_to_float,
1406         j2k_read_int32_to_float,
1407         j2k_read_float32_to_float,
1408         j2k_read_float64_to_float
1409 };
1410
1411 const j2k_mct_function j2k_mct_read_functions_to_int32 [] =
1412 {
1413         j2k_read_int16_to_int32,
1414         j2k_read_int32_to_int32,
1415         j2k_read_float32_to_int32,
1416         j2k_read_float64_to_int32
1417 };
1418
1419 const j2k_mct_function j2k_mct_write_functions_from_float [] =
1420 {
1421         j2k_write_float_to_int16,
1422         j2k_write_float_to_int32,
1423         j2k_write_float_to_float,
1424         j2k_write_float_to_float64
1425 };
1426
1427 typedef struct opj_dec_memory_marker_handler
1428 {
1429         /** marker value */
1430         OPJ_UINT32 id;
1431         /** value of the state when the marker can appear */
1432         OPJ_UINT32 states;
1433         /** action linked to the marker */
1434         opj_bool (*handler) (
1435                                         opj_j2k_v2_t *p_j2k,
1436                                         OPJ_BYTE * p_header_data,
1437                                         OPJ_UINT32 p_header_size,
1438                                         struct opj_event_mgr * p_manager
1439                                                 );
1440 }
1441 opj_dec_memory_marker_handler_t;
1442
1443 const opj_dec_memory_marker_handler_t j2k_memory_marker_handler_tab [] =
1444 {
1445 #ifdef TODO_MS
1446   {J2K_MS_SOT, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPHSOT, j2k_read_sot},
1447   {J2K_MS_COD, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_cod},
1448   {J2K_MS_COC, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_coc},
1449   {J2K_MS_RGN, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_rgn},
1450   {J2K_MS_QCD, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_qcd},
1451   {J2K_MS_QCC, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_qcc},
1452   {J2K_MS_POC, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_poc},
1453   {J2K_MS_SIZ, J2K_DEC_STATE_MHSIZ , j2k_read_siz},
1454   {J2K_MS_TLM, J2K_DEC_STATE_MH, j2k_read_tlm},
1455   {J2K_MS_PLM, J2K_DEC_STATE_MH, j2k_read_plm},
1456   {J2K_MS_PLT, J2K_DEC_STATE_TPH, j2k_read_plt},
1457   {J2K_MS_PPM, J2K_DEC_STATE_MH, j2k_read_ppm},
1458   {J2K_MS_PPT, J2K_DEC_STATE_TPH, j2k_read_ppt},
1459   {J2K_MS_SOP, 0, 0},
1460   {J2K_MS_CRG, J2K_DEC_STATE_MH, j2k_read_crg},
1461   {J2K_MS_COM, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_com},
1462   {J2K_MS_MCT, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_mct},
1463   {J2K_MS_CBD, J2K_DEC_STATE_MH , j2k_read_cbd},
1464   {J2K_MS_MCC, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_mcc},
1465   {J2K_MS_MCO, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_mco},
1466 #endif
1467   {J2K_MS_SOT, J2K_STATE_MH | J2K_STATE_TPHSOT, j2k_read_sot_v2},
1468   {J2K_MS_COD, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_cod_v2},
1469   {J2K_MS_COC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_coc_v2},
1470   {J2K_MS_RGN, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_rgn_v2},
1471   {J2K_MS_QCD, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_qcd_v2},
1472   {J2K_MS_QCC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_qcc_v2},
1473   {J2K_MS_POC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_poc_v2},
1474   {J2K_MS_SIZ, J2K_STATE_MHSIZ , j2k_read_siz_v2},
1475   {J2K_MS_TLM, J2K_STATE_MH, j2k_read_tlm_v2},
1476   {J2K_MS_PLM, J2K_STATE_MH, j2k_read_plm_v2},
1477   {J2K_MS_PLT, J2K_STATE_TPH, j2k_read_plt_v2},
1478   {J2K_MS_PPM, J2K_STATE_MH, j2k_read_ppm_v3},
1479   {J2K_MS_PPT, J2K_STATE_TPH, j2k_read_ppt_v2},
1480   {J2K_MS_SOP, 0, 0},
1481   {J2K_MS_CRG, J2K_STATE_MH, j2k_read_crg_v2},
1482   {J2K_MS_COM, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_com_v2},
1483   {J2K_MS_MCT, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_mct},
1484   {J2K_MS_CBD, J2K_STATE_MH , j2k_read_cbd},
1485   {J2K_MS_MCC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_mcc},
1486   {J2K_MS_MCO, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_mco},
1487 #ifdef USE_JPWL
1488 #ifdef TODO_MS /* FIXME */
1489   {J2K_MS_EPC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_epc},
1490   {J2K_MS_EPB, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_epb},
1491   {J2K_MS_ESD, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_esd},
1492   {J2K_MS_RED, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_red},
1493 #endif
1494 #endif /* USE_JPWL */
1495 #ifdef USE_JPSEC
1496   {J2K_MS_SEC, J2K_DEC_STATE_MH, j2k_read_sec},
1497   {J2K_MS_INSEC, 0, j2k_read_insec}
1498 #endif /* USE_JPSEC */
1499   {J2K_MS_UNK, J2K_STATE_MH | J2K_STATE_TPH, 0}/*j2k_read_unk_v2}*/
1500 };
1501
1502
1503
1504 void  j2k_read_int16_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1505 {
1506         OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
1507         OPJ_FLOAT32 * l_dest_data = (OPJ_FLOAT32 *) p_dest_data;
1508         OPJ_UINT32 i;
1509         OPJ_UINT32 l_temp;
1510
1511         for (i=0;i<p_nb_elem;++i) {
1512                 opj_read_bytes(l_src_data,&l_temp,2);
1513
1514                 l_src_data+=sizeof(OPJ_INT16);
1515
1516                 *(l_dest_data++) = (OPJ_FLOAT32) l_temp;
1517         }
1518 }
1519
1520 void  j2k_read_int32_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1521 {
1522         OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
1523         OPJ_FLOAT32 * l_dest_data = (OPJ_FLOAT32 *) p_dest_data;
1524         OPJ_UINT32 i;
1525         OPJ_UINT32 l_temp;
1526
1527         for (i=0;i<p_nb_elem;++i) {
1528                 opj_read_bytes(l_src_data,&l_temp,4);
1529
1530                 l_src_data+=sizeof(OPJ_INT32);
1531
1532                 *(l_dest_data++) = (OPJ_FLOAT32) l_temp;
1533         }
1534 }
1535
1536 void  j2k_read_float32_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1537 {
1538         OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
1539         OPJ_FLOAT32 * l_dest_data = (OPJ_FLOAT32 *) p_dest_data;
1540         OPJ_UINT32 i;
1541         OPJ_FLOAT32 l_temp;
1542
1543         for (i=0;i<p_nb_elem;++i) {
1544                 opj_read_float(l_src_data,&l_temp);
1545
1546                 l_src_data+=sizeof(OPJ_FLOAT32);
1547
1548                 *(l_dest_data++) = l_temp;
1549         }
1550 }
1551
1552 void  j2k_read_float64_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1553 {
1554         OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
1555         OPJ_FLOAT32 * l_dest_data = (OPJ_FLOAT32 *) p_dest_data;
1556         OPJ_UINT32 i;
1557         OPJ_FLOAT64 l_temp;
1558
1559         for (i=0;i<p_nb_elem;++i) {
1560                 opj_read_double(l_src_data,&l_temp);
1561
1562                 l_src_data+=sizeof(OPJ_FLOAT64);
1563
1564                 *(l_dest_data++) = (OPJ_FLOAT32) l_temp;
1565         }
1566 }
1567
1568 void  j2k_read_int16_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1569 {
1570         OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
1571         OPJ_INT32 * l_dest_data = (OPJ_INT32 *) p_dest_data;
1572         OPJ_UINT32 i;
1573         OPJ_UINT32 l_temp;
1574
1575         for (i=0;i<p_nb_elem;++i) {
1576                 opj_read_bytes(l_src_data,&l_temp,2);
1577
1578                 l_src_data+=sizeof(OPJ_INT16);
1579
1580                 *(l_dest_data++) = (OPJ_INT32) l_temp;
1581         }
1582 }
1583
1584 void  j2k_read_int32_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1585 {
1586         OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
1587         OPJ_INT32 * l_dest_data = (OPJ_INT32 *) p_dest_data;
1588         OPJ_UINT32 i;
1589         OPJ_UINT32 l_temp;
1590
1591         for (i=0;i<p_nb_elem;++i) {
1592                 opj_read_bytes(l_src_data,&l_temp,4);
1593
1594                 l_src_data+=sizeof(OPJ_INT32);
1595
1596                 *(l_dest_data++) = (OPJ_INT32) l_temp;
1597         }
1598 }
1599
1600 void  j2k_read_float32_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1601 {
1602         OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
1603         OPJ_INT32 * l_dest_data = (OPJ_INT32 *) p_dest_data;
1604         OPJ_UINT32 i;
1605         OPJ_FLOAT32 l_temp;
1606
1607         for (i=0;i<p_nb_elem;++i) {
1608                 opj_read_float(l_src_data,&l_temp);
1609
1610                 l_src_data+=sizeof(OPJ_FLOAT32);
1611
1612                 *(l_dest_data++) = (OPJ_INT32) l_temp;
1613         }
1614 }
1615
1616 void  j2k_read_float64_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1617 {
1618         OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
1619         OPJ_INT32 * l_dest_data = (OPJ_INT32 *) p_dest_data;
1620         OPJ_UINT32 i;
1621         OPJ_FLOAT64 l_temp;
1622
1623         for (i=0;i<p_nb_elem;++i) {
1624                 opj_read_double(l_src_data,&l_temp);
1625
1626                 l_src_data+=sizeof(OPJ_FLOAT64);
1627
1628                 *(l_dest_data++) = (OPJ_INT32) l_temp;
1629         }
1630 }
1631
1632 void  j2k_write_float_to_int16 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1633 {
1634         OPJ_BYTE * l_dest_data = (OPJ_BYTE *) p_dest_data;
1635         OPJ_FLOAT32 * l_src_data = (OPJ_FLOAT32 *) p_src_data;
1636         OPJ_UINT32 i;
1637         OPJ_UINT32 l_temp;
1638
1639         for (i=0;i<p_nb_elem;++i) {
1640                 l_temp = (OPJ_UINT32) *(l_src_data++);
1641
1642                 opj_write_bytes(l_dest_data,l_temp,sizeof(OPJ_INT16));
1643
1644                 l_dest_data+=sizeof(OPJ_INT16);
1645         }
1646 }
1647
1648 void  j2k_write_float_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1649 {
1650         OPJ_BYTE * l_dest_data = (OPJ_BYTE *) p_dest_data;
1651         OPJ_FLOAT32 * l_src_data = (OPJ_FLOAT32 *) p_src_data;
1652         OPJ_UINT32 i;
1653         OPJ_UINT32 l_temp;
1654
1655         for (i=0;i<p_nb_elem;++i) {
1656                 l_temp = (OPJ_UINT32) *(l_src_data++);
1657
1658                 opj_write_bytes(l_dest_data,l_temp,sizeof(OPJ_INT32));
1659
1660                 l_dest_data+=sizeof(OPJ_INT32);
1661         }
1662 }
1663
1664 void  j2k_write_float_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1665 {
1666         OPJ_BYTE * l_dest_data = (OPJ_BYTE *) p_dest_data;
1667         OPJ_FLOAT32 * l_src_data = (OPJ_FLOAT32 *) p_src_data;
1668         OPJ_UINT32 i;
1669         OPJ_FLOAT32 l_temp;
1670
1671         for (i=0;i<p_nb_elem;++i) {
1672                 l_temp = (OPJ_FLOAT32) *(l_src_data++);
1673
1674                 opj_write_float(l_dest_data,l_temp);
1675
1676                 l_dest_data+=sizeof(OPJ_FLOAT32);
1677         }
1678 }
1679
1680 void  j2k_write_float_to_float64 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1681 {
1682         OPJ_BYTE * l_dest_data = (OPJ_BYTE *) p_dest_data;
1683         OPJ_FLOAT32 * l_src_data = (OPJ_FLOAT32 *) p_src_data;
1684         OPJ_UINT32 i;
1685         OPJ_FLOAT64 l_temp;
1686
1687         for (i=0;i<p_nb_elem;++i) {
1688                 l_temp = (OPJ_FLOAT64) *(l_src_data++);
1689
1690                 opj_write_double(l_dest_data,l_temp);
1691
1692                 l_dest_data+=sizeof(OPJ_FLOAT64);
1693         }
1694 }
1695
1696
1697 /**
1698  * Converts an enum type progression order to string type.
1699  *
1700  * @param prg_order             the progression order to get.
1701  *
1702  * @return      the string representation of the given progression order.
1703  */
1704 char *j2k_convert_progression_order(OPJ_PROG_ORDER prg_order){
1705         j2k_prog_order_t *po;
1706         for(po = j2k_prog_order_list; po->enum_prog != -1; po++ ){
1707                 if(po->enum_prog == prg_order){
1708                         return po->str_prog;
1709                 }
1710         }
1711         return po->str_prog;
1712 }
1713
1714 /**
1715  * Checks the progression order changes values. Tells if the poc given as input are valid.
1716  *
1717  * @param       p_pocs                          the progression order changes.
1718  * @param       p_nb_pocs                       the number of progression order changes.
1719  * @param       p_nb_resolutions        the number of resolutions.
1720  * @param       numcomps                        the number of components
1721  * @param       numlayers                       the number of layers.
1722  * @param       p_manager                       the user event manager.
1723  *
1724  * @return      true if the pocs are valid.
1725  */
1726 opj_bool j2k_check_poc_val(     const opj_poc_t *p_pocs,
1727                                                         OPJ_UINT32 p_nb_pocs,
1728                                                         OPJ_UINT32 p_nb_resolutions,
1729                                                         OPJ_UINT32 p_num_comps,
1730                                                         OPJ_UINT32 p_num_layers,
1731                                                         opj_event_mgr_t * p_manager)
1732 {
1733         OPJ_UINT32* packet_array;
1734         OPJ_UINT32 index , resno, compno, layno;
1735         OPJ_UINT32 i;
1736         OPJ_UINT32 step_c = 1;
1737         OPJ_UINT32 step_r = p_num_comps * step_c;
1738         OPJ_UINT32 step_l = p_nb_resolutions * step_r;
1739         opj_bool loss = OPJ_FALSE;
1740         OPJ_UINT32 layno0 = 0;
1741
1742         packet_array = (OPJ_UINT32*) opj_calloc(step_l * p_num_layers, sizeof(OPJ_UINT32));
1743         if (packet_array == 00) {
1744                 opj_event_msg_v2(p_manager , EVT_ERROR, "Not enough memory for checking the poc values.\n");
1745                 return OPJ_FALSE;
1746         }
1747         memset(packet_array,0,step_l * p_num_layers* sizeof(OPJ_UINT32));
1748
1749         if (p_nb_pocs == 0) {
1750                 return OPJ_TRUE;
1751         }
1752
1753         index = step_r * p_pocs->resno0;
1754         // take each resolution for each poc
1755         for (resno = p_pocs->resno0 ; resno < p_pocs->resno1 ; ++resno)
1756         {
1757                 OPJ_UINT32 res_index = index + p_pocs->compno0 * step_c;
1758
1759                 // take each comp of each resolution for each poc
1760                 for (compno = p_pocs->compno0 ; compno < p_pocs->compno1 ; ++compno) {
1761                         OPJ_UINT32 comp_index = res_index + layno0 * step_l;
1762
1763                         // and finally take each layer of each res of ...
1764                         for (layno = layno0; layno < p_pocs->layno1 ; ++layno) {
1765                                 //index = step_r * resno + step_c * compno + step_l * layno;
1766                                 packet_array[comp_index] = 1;
1767                                 comp_index += step_l;
1768                         }
1769
1770                         res_index += step_c;
1771                 }
1772
1773                 index += step_r;
1774         }
1775         ++p_pocs;
1776
1777         // iterate through all the pocs
1778         for (i = 1; i < p_nb_pocs ; ++i) {
1779                 OPJ_UINT32 l_last_layno1 = (p_pocs-1)->layno1 ;
1780
1781                 layno0 = (p_pocs->layno1 > l_last_layno1)? l_last_layno1 : 0;
1782                 index = step_r * p_pocs->resno0;
1783
1784                 // take each resolution for each poc
1785                 for (resno = p_pocs->resno0 ; resno < p_pocs->resno1 ; ++resno) {
1786                         OPJ_UINT32 res_index = index + p_pocs->compno0 * step_c;
1787
1788                         // take each comp of each resolution for each poc
1789                         for (compno = p_pocs->compno0 ; compno < p_pocs->compno1 ; ++compno) {
1790                                 OPJ_UINT32 comp_index = res_index + layno0 * step_l;
1791
1792                                 // and finally take each layer of each res of ...
1793                                 for (layno = layno0; layno < p_pocs->layno1 ; ++layno) {
1794                                         //index = step_r * resno + step_c * compno + step_l * layno;
1795                                         packet_array[comp_index] = 1;
1796                                         comp_index += step_l;
1797                                 }
1798
1799                                 res_index += step_c;
1800                         }
1801
1802                         index += step_r;
1803                 }
1804
1805                 ++p_pocs;
1806         }
1807
1808         index = 0;
1809         for (layno = 0; layno < p_num_layers ; ++layno) {
1810                 for (resno = 0; resno < p_nb_resolutions; ++resno) {
1811                         for (compno = 0; compno < p_num_comps; ++compno) {
1812                                 loss |= (packet_array[index]!=1);
1813                                 //index = step_r * resno + step_c * compno + step_l * layno;
1814                                 index += step_c;
1815                         }
1816                 }
1817         }
1818
1819         if (loss) {
1820                 opj_event_msg_v2(p_manager , EVT_ERROR, "Missing packets possible loss of data\n");
1821         }
1822
1823         opj_free(packet_array);
1824
1825         return !loss;
1826 }
1827
1828 /* ----------------------------------------------------------------------- */
1829 static int j2k_get_num_tp(opj_cp_t *cp,int pino,int tileno){
1830         char *prog;
1831         int i;
1832         int tpnum=1,tpend=0;
1833         opj_tcp_t *tcp = &cp->tcps[tileno];
1834         prog = j2k_convert_progression_order(tcp->prg);
1835         
1836         if(cp->tp_on == 1){
1837                 for(i=0;i<4;i++){
1838                         if(tpend!=1){
1839                                 if( cp->tp_flag == prog[i] ){
1840                                         tpend=1;cp->tp_pos=i;
1841                                 }
1842                                 switch(prog[i]){
1843                                 case 'C':
1844                                         tpnum= tpnum * tcp->pocs[pino].compE;
1845                                         break;
1846                                 case 'R':
1847                                         tpnum= tpnum * tcp->pocs[pino].resE;
1848                                         break;
1849                                 case 'P':
1850                                         tpnum= tpnum * tcp->pocs[pino].prcE;
1851                                         break;
1852                                 case 'L':
1853                                         tpnum= tpnum * tcp->pocs[pino].layE;
1854                                         break;
1855                                 }
1856                         }
1857                 }
1858         }else{
1859                 tpnum=1;
1860         }
1861         return tpnum;
1862 }
1863
1864 /**
1865  * Gets the number of tile parts used for the given change of progression (if any) and the given tile.
1866  *
1867  * @param               cp                      the coding parameters.
1868  * @param               pino            the offset of the given poc (i.e. its position in the coding parameter).
1869  * @param               tileno          the given tile.
1870  *
1871  * @return              the number of tile parts.
1872  */
1873 OPJ_UINT32 j2k_get_num_tp_v2(opj_cp_v2_t *cp, OPJ_UINT32 pino, OPJ_UINT32 tileno)
1874 {
1875         const OPJ_CHAR *prog = 00;
1876         OPJ_UINT32 i;
1877         OPJ_UINT32 tpnum = 1;
1878         opj_tcp_v2_t *tcp = 00;
1879         opj_poc_t * l_current_poc = 00;
1880
1881         /*  preconditions */
1882         assert(tileno < (cp->tw * cp->th));
1883         assert(pino < (cp->tcps[tileno].numpocs + 1));
1884
1885         /* get the given tile coding parameter */
1886         tcp = &cp->tcps[tileno];
1887         assert(tcp != 00);
1888
1889         l_current_poc = &(tcp->pocs[pino]);
1890         assert(l_current_poc != 0);
1891
1892         /* get the progression order as a character string */
1893         prog = j2k_convert_progression_order(tcp->prg);
1894         assert(strlen(prog) > 0);
1895
1896         if (cp->m_specific_param.m_enc.m_tp_on == 1) {
1897                 for (i=0;i<4;++i) {
1898                         switch (prog[i])
1899                         {
1900                                 /* component wise */
1901                                 case 'C':
1902                                         tpnum *= l_current_poc->compE;
1903                                         break;
1904                                 /* resolution wise */
1905                                 case 'R':
1906                                         tpnum *= l_current_poc->resE;
1907                                         break;
1908                                 /* precinct wise */
1909                                 case 'P':
1910                                         tpnum *= l_current_poc->prcE;
1911                                         break;
1912                                 /* layer wise */
1913                                 case 'L':
1914                                         tpnum *= l_current_poc->layE;
1915                                         break;
1916                         }
1917                         /* whould we split here ? */
1918                         if ( cp->m_specific_param.m_enc.m_tp_flag == prog[i] ) {
1919                                 cp->m_specific_param.m_enc.m_tp_pos=i;
1920                                 break;
1921                         }
1922                 }
1923         }
1924         else {
1925                 tpnum=1;
1926         }
1927
1928         return tpnum;
1929 }
1930
1931 /**     mem allocation for TLM marker*/
1932 int j2k_calculate_tp(opj_cp_t *cp,int img_numcomp,opj_image_t *image,opj_j2k_t *j2k ){
1933         int pino,tileno,totnum_tp=0;
1934
1935         OPJ_ARG_NOT_USED(img_numcomp);
1936
1937         j2k->cur_totnum_tp = (int *) opj_malloc(cp->tw * cp->th * sizeof(int));
1938         for (tileno = 0; tileno < cp->tw * cp->th; tileno++) {
1939                 int cur_totnum_tp = 0;
1940                 opj_tcp_t *tcp = &cp->tcps[tileno];
1941                 for(pino = 0; pino <= tcp->numpocs; pino++) {
1942                         int tp_num=0;
1943                         opj_pi_iterator_t *pi = pi_initialise_encode(image, cp, tileno,FINAL_PASS);
1944                         if(!pi) { return -1;}
1945                         tp_num = j2k_get_num_tp(cp,pino,tileno);
1946                         totnum_tp = totnum_tp + tp_num;
1947                         cur_totnum_tp = cur_totnum_tp + tp_num;
1948                         pi_destroy(pi, cp, tileno);
1949                 }
1950                 j2k->cur_totnum_tp[tileno] = cur_totnum_tp;
1951                 /* INDEX >> */
1952                 if (j2k->cstr_info) {
1953                         j2k->cstr_info->tile[tileno].num_tps = cur_totnum_tp;
1954                         j2k->cstr_info->tile[tileno].tp = (opj_tp_info_t *) opj_malloc(cur_totnum_tp * sizeof(opj_tp_info_t));
1955                 }
1956                 /* << INDEX */
1957         }
1958         return totnum_tp;
1959 }
1960
1961 /**
1962  * Calculates the total number of tile parts needed by the encoder to
1963  * encode such an image. If not enough memory is available, then the function return false.
1964  *
1965  * @param       p_nb_tiles      pointer that will hold the number of tile parts.
1966  * @param       cp                      the coding parameters for the image.
1967  * @param       image           the image to encode.
1968  * @param       p_j2k                   the p_j2k encoder.
1969  * @param       p_manager       the user event manager.
1970  *
1971  * @return true if the function was successful, false else.
1972  */
1973 opj_bool j2k_calculate_tp_v2( opj_j2k_v2_t *p_j2k,
1974                                                           opj_cp_v2_t *cp,
1975                                                           OPJ_UINT32 * p_nb_tiles,
1976                                                           opj_image_t *image,
1977                                                           opj_event_mgr_t * p_manager)
1978 {
1979         OPJ_UINT32 pino,tileno;
1980         OPJ_UINT32 l_nb_tiles;
1981         opj_tcp_v2_t *tcp;
1982
1983         /* preconditions */
1984         assert(p_nb_tiles != 00);
1985         assert(cp != 00);
1986         assert(image != 00);
1987         assert(p_j2k != 00);
1988         assert(p_manager != 00);
1989
1990         l_nb_tiles = cp->tw * cp->th;
1991         * p_nb_tiles = 0;
1992         tcp = cp->tcps;
1993
1994         /* INDEX >> */
1995         /* TODO mergeV2: check this part which use cstr_info */
1996         /*if (p_j2k->cstr_info) {
1997                 opj_tile_info_t * l_info_tile_ptr = p_j2k->cstr_info->tile;
1998
1999                 for (tileno = 0; tileno < l_nb_tiles; ++tileno) {
2000                         OPJ_UINT32 cur_totnum_tp = 0;
2001
2002                         pi_update_encoding_parameters(image,cp,tileno);
2003
2004                         for (pino = 0; pino <= tcp->numpocs; ++pino)
2005                         {
2006                                 OPJ_UINT32 tp_num = j2k_get_num_tp_v2(cp,pino,tileno);
2007
2008                                 *p_nb_tiles = *p_nb_tiles + tp_num;
2009
2010                                 cur_totnum_tp += tp_num;
2011                         }
2012
2013                         tcp->m_nb_tile_parts = cur_totnum_tp;
2014
2015                         l_info_tile_ptr->tp = (opj_tp_info_t *) opj_malloc(cur_totnum_tp * sizeof(opj_tp_info_t));
2016                         if (l_info_tile_ptr->tp == 00) {
2017                                 return OPJ_FALSE;
2018                         }
2019
2020                         memset(l_info_tile_ptr->tp,0,cur_totnum_tp * sizeof(opj_tp_info_t));
2021
2022                         l_info_tile_ptr->num_tps = cur_totnum_tp;
2023
2024                         ++l_info_tile_ptr;
2025                         ++tcp;
2026                 }
2027         }
2028         else */{
2029                 for (tileno = 0; tileno < l_nb_tiles; ++tileno) {
2030                         OPJ_UINT32 cur_totnum_tp = 0;
2031
2032                         pi_update_encoding_parameters(image,cp,tileno);
2033
2034                         for (pino = 0; pino <= tcp->numpocs; ++pino) {
2035                                 OPJ_UINT32 tp_num = j2k_get_num_tp_v2(cp,pino,tileno);
2036
2037                                 *p_nb_tiles = *p_nb_tiles + tp_num;
2038
2039                                 cur_totnum_tp += tp_num;
2040                         }
2041                         tcp->m_nb_tile_parts = cur_totnum_tp;
2042
2043                         ++tcp;
2044                 }
2045         }
2046
2047         return OPJ_TRUE;
2048 }
2049
2050 static void j2k_write_soc(opj_j2k_t *j2k) {
2051         opj_cio_t *cio = j2k->cio;
2052         cio_write(cio, J2K_MS_SOC, 2);
2053
2054         if(j2k->cstr_info)
2055           j2k_add_mhmarker(j2k->cstr_info, J2K_MS_SOC, cio_tell(cio), 0);
2056
2057 /* UniPG>> */
2058 #ifdef USE_JPWL
2059
2060         /* update markers struct */
2061         j2k_add_marker(j2k->cstr_info, J2K_MS_SOC, cio_tell(cio) - 2, 2);
2062 #endif /* USE_JPWL */
2063 /* <<UniPG */
2064 }
2065
2066 /**
2067  * Writes the SOC marker (Start Of Codestream)
2068  *
2069  * @param       p_stream                        the stream to write data to.
2070  * @param       p_j2k                   J2K codec.
2071  * @param       p_manager       the user event manager.
2072 */
2073 opj_bool j2k_write_soc_v2(      opj_j2k_v2_t *p_j2k,
2074                                                         struct opj_stream_private *p_stream,
2075                                                         struct opj_event_mgr * p_manager )
2076 {
2077         /* 2 bytes will be written */
2078         OPJ_BYTE * l_start_stream = 00;
2079
2080         /* preconditions */
2081         assert(p_stream != 00);
2082         assert(p_j2k != 00);
2083         assert(p_manager != 00);
2084
2085         l_start_stream = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
2086
2087         /* write SOC identifier */
2088         opj_write_bytes(l_start_stream,J2K_MS_SOC,2);
2089
2090         if (opj_stream_write_data(p_stream,l_start_stream,2,p_manager) != 2) {
2091                 return OPJ_FALSE;
2092         }
2093
2094 /* UniPG>> */
2095 #ifdef USE_JPWL
2096         /* update markers struct */
2097 /*
2098         j2k_add_marker(p_j2k->cstr_info, J2K_MS_SOC, p_stream_tell(p_stream) - 2, 2);
2099 */
2100   assert( 0 && "TODO" );
2101 #endif /* USE_JPWL */
2102 /* <<UniPG */
2103
2104         return OPJ_TRUE;
2105 }
2106
2107
2108
2109 /**
2110  * Reads a SOC marker (Start of Codestream)
2111  * @param       p_header_data   the data contained in the SOC box.
2112  * @param       jp2                             the jpeg2000 file codec.
2113  * @param       p_header_size   the size of the data contained in the SOC marker.
2114  * @param       p_manager               the user event manager.
2115 */
2116 static opj_bool opj_j2k_read_soc(       opj_j2k_v2_t *p_j2k,
2117                                                                         struct opj_stream_private *p_stream,
2118                                                                         struct opj_event_mgr * p_manager )
2119 {
2120         OPJ_BYTE l_data [2];
2121         OPJ_UINT32 l_marker;
2122
2123         /* preconditions */
2124         assert(p_j2k != 00);
2125         assert(p_manager != 00);
2126         assert(p_stream != 00);
2127
2128         if (opj_stream_read_data(p_stream,l_data,2,p_manager) != 2) {
2129                 return OPJ_FALSE;
2130         }
2131
2132         opj_read_bytes(l_data,&l_marker,2);
2133         if (l_marker != J2K_MS_SOC) {
2134                 return OPJ_FALSE;
2135         }
2136
2137         /* Next marker should be a SIZ marker in the main header */
2138         p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_MHSIZ;
2139
2140         /* FIXME move it in a index structure included in p_j2k*/
2141         p_j2k->cstr_index->main_head_start = opj_stream_tell(p_stream) - 2;
2142
2143         opj_event_msg_v2(p_manager, EVT_INFO, "Start to read j2k main header (%d).\n", p_j2k->cstr_index->main_head_start);
2144
2145         /* Add the marker to the codestream index*/
2146         j2k_add_mhmarker_v2(p_j2k->cstr_index, J2K_MS_SOC, p_j2k->cstr_index->main_head_start, 2);
2147
2148         return OPJ_TRUE;
2149 }
2150
2151 static void j2k_write_siz(opj_j2k_t *j2k) {
2152         OPJ_UINT32 i;
2153         int lenp, len;
2154
2155         opj_cio_t *cio = j2k->cio;
2156         opj_image_t *image = j2k->image;
2157         opj_cp_t *cp = j2k->cp;
2158
2159         cio_write(cio, J2K_MS_SIZ, 2);  /* SIZ */
2160         lenp = cio_tell(cio);
2161         cio_skip(cio, 2);
2162         cio_write(cio, cp->rsiz, 2);                    /* Rsiz (capabilities) */
2163         cio_write(cio, image->x1, 4);   /* Xsiz */
2164         cio_write(cio, image->y1, 4);   /* Ysiz */
2165         cio_write(cio, image->x0, 4);   /* X0siz */
2166         cio_write(cio, image->y0, 4);   /* Y0siz */
2167         cio_write(cio, cp->tdx, 4);             /* XTsiz */
2168         cio_write(cio, cp->tdy, 4);             /* YTsiz */
2169         cio_write(cio, cp->tx0, 4);             /* XT0siz */
2170         cio_write(cio, cp->ty0, 4);             /* YT0siz */
2171         cio_write(cio, image->numcomps, 2);     /* Csiz */
2172         for (i = 0; i < image->numcomps; i++) {
2173                 cio_write(cio, image->comps[i].prec - 1 + (image->comps[i].sgnd << 7), 1);      /* Ssiz_i */
2174                 cio_write(cio, image->comps[i].dx, 1);  /* XRsiz_i */
2175                 cio_write(cio, image->comps[i].dy, 1);  /* YRsiz_i */
2176         }
2177         len = cio_tell(cio) - lenp;
2178         cio_seek(cio, lenp);
2179         cio_write(cio, len, 2);         /* Lsiz */
2180         cio_seek(cio, lenp + len);
2181         
2182         if(j2k->cstr_info)
2183           j2k_add_mhmarker(j2k->cstr_info, J2K_MS_SIZ, lenp, len);
2184 }
2185
2186 /**
2187  * Writes the SIZ marker (image and tile size)
2188  *
2189  * @param       p_stream                        the stream to write data to.
2190  * @param       p_j2k                   J2K codec.
2191  * @param       p_manager       the user event manager.
2192 */
2193 opj_bool j2k_write_siz_v2(      opj_j2k_v2_t *p_j2k,
2194                                                         struct opj_stream_private *p_stream,
2195                                                         struct opj_event_mgr * p_manager )
2196 {
2197         OPJ_UINT32 i;
2198         OPJ_UINT32 l_size_len;
2199         OPJ_BYTE * l_current_ptr;
2200         opj_image_t * l_image = 00;
2201         opj_cp_v2_t *cp = 00;
2202         opj_image_comp_t * l_img_comp = 00;
2203
2204         /* preconditions */
2205         assert(p_stream != 00);
2206         assert(p_j2k != 00);
2207         assert(p_manager != 00);
2208
2209         l_image = p_j2k->m_private_image;
2210         cp = &(p_j2k->m_cp);
2211         l_size_len = 40 + 3 * l_image->numcomps;
2212         l_img_comp = l_image->comps;
2213
2214         if (l_size_len > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
2215
2216                 p_j2k->m_specific_param.m_encoder.m_header_tile_data
2217                         = (OPJ_BYTE*)opj_realloc(
2218                                 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
2219                                 l_size_len);
2220                 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
2221                         return OPJ_FALSE;
2222                 }
2223
2224                 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_size_len;
2225         }
2226
2227         l_current_ptr = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
2228
2229         /* write SOC identifier */
2230         opj_write_bytes(l_current_ptr,J2K_MS_SIZ,2);    /* SIZ */
2231         l_current_ptr+=2;
2232
2233         opj_write_bytes(l_current_ptr,l_size_len-2,2); /* L_SIZ */
2234         l_current_ptr+=2;
2235
2236         opj_write_bytes(l_current_ptr, cp->rsiz, 2);    /* Rsiz (capabilities) */
2237         l_current_ptr+=2;
2238
2239         opj_write_bytes(l_current_ptr, l_image->x1, 4); /* Xsiz */
2240         l_current_ptr+=4;
2241
2242         opj_write_bytes(l_current_ptr, l_image->y1, 4); /* Ysiz */
2243         l_current_ptr+=4;
2244
2245         opj_write_bytes(l_current_ptr, l_image->x0, 4); /* X0siz */
2246         l_current_ptr+=4;
2247
2248         opj_write_bytes(l_current_ptr, l_image->y0, 4); /* Y0siz */
2249         l_current_ptr+=4;
2250
2251         opj_write_bytes(l_current_ptr, cp->tdx, 4);             /* XTsiz */
2252         l_current_ptr+=4;
2253
2254         opj_write_bytes(l_current_ptr, cp->tdy, 4);             /* YTsiz */
2255         l_current_ptr+=4;
2256
2257         opj_write_bytes(l_current_ptr, cp->tx0, 4);             /* XT0siz */
2258         l_current_ptr+=4;
2259
2260         opj_write_bytes(l_current_ptr, cp->ty0, 4);             /* YT0siz */
2261         l_current_ptr+=4;
2262
2263         opj_write_bytes(l_current_ptr, l_image->numcomps, 2);   /* Csiz */
2264         l_current_ptr+=2;
2265
2266         for (i = 0; i < l_image->numcomps; ++i) {
2267                 /* TODO here with MCT ? */
2268                 opj_write_bytes(l_current_ptr, l_img_comp->prec - 1 + (l_img_comp->sgnd << 7), 1);      /* Ssiz_i */
2269                 ++l_current_ptr;
2270
2271                 opj_write_bytes(l_current_ptr, l_img_comp->dx, 1);      /* XRsiz_i */
2272                 ++l_current_ptr;
2273
2274                 opj_write_bytes(l_current_ptr, l_img_comp->dy, 1);      /* YRsiz_i */
2275                 ++l_current_ptr;
2276
2277                 ++l_img_comp;
2278         }
2279
2280         if (opj_stream_write_data(p_stream,p_j2k->m_specific_param.m_encoder.m_header_tile_data,l_size_len,p_manager) != l_size_len) {
2281                 return OPJ_FALSE;
2282         }
2283
2284         return OPJ_TRUE;
2285 }
2286
2287 static void j2k_read_siz(opj_j2k_t *j2k) {
2288         int len;
2289         OPJ_UINT32 i;
2290         
2291         opj_cio_t *cio = j2k->cio;
2292         opj_image_t *image = j2k->image;
2293         opj_cp_t *cp = j2k->cp;
2294         
2295         len = cio_read(cio, 2);                 /* Lsiz */
2296         cio_read(cio, 2);                               /* Rsiz (capabilities) */
2297         image->x1 = cio_read(cio, 4);   /* Xsiz */
2298         image->y1 = cio_read(cio, 4);   /* Ysiz */
2299         image->x0 = cio_read(cio, 4);   /* X0siz */
2300         image->y0 = cio_read(cio, 4);   /* Y0siz */
2301         cp->tdx = cio_read(cio, 4);             /* XTsiz */
2302         cp->tdy = cio_read(cio, 4);             /* YTsiz */
2303         cp->tx0 = cio_read(cio, 4);             /* XT0siz */
2304         cp->ty0 = cio_read(cio, 4);             /* YT0siz */
2305         
2306   /* the following code triggers: */
2307   /* warning: comparison of unsigned expression < 0 is always false */
2308 #if 0
2309         if ((image->x0<0)||(image->x1<0)||(image->y0<0)||(image->y1<0)) {
2310                 opj_event_msg(j2k->cinfo, EVT_ERROR,
2311                                                                         "%s: invalid image size (x0:%d, x1:%d, y0:%d, y1:%d)\n",
2312                                                                         image->x0,image->x1,image->y0,image->y1);
2313                 return;
2314         }
2315 #endif
2316         
2317         image->numcomps = cio_read(cio, 2);     /* Csiz */
2318
2319 #ifdef USE_JPWL
2320         if (j2k->cp->correct) {
2321                 /* if JPWL is on, we check whether TX errors have damaged
2322                   too much the SIZ parameters */
2323                 if (!(image->x1 * image->y1)) {
2324                         opj_event_msg(j2k->cinfo, EVT_ERROR,
2325                                 "JPWL: bad image size (%d x %d)\n",
2326                                 image->x1, image->y1);
2327                         if (!JPWL_ASSUME || JPWL_ASSUME) {
2328                                 opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
2329                                 return;
2330                         }
2331                 }
2332     assert( len >= 38 );
2333                 if (image->numcomps != (OPJ_UINT32)((len - 38) / 3)) {
2334                         opj_event_msg(j2k->cinfo, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
2335                                 "JPWL: Csiz is %d => space in SIZ only for %d comps.!!!\n",
2336                                 image->numcomps, ((len - 38) / 3));
2337                         if (!JPWL_ASSUME) {
2338                                 opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
2339                                 return;
2340                         }
2341                         /* we try to correct */
2342                         opj_event_msg(j2k->cinfo, EVT_WARNING, "- trying to adjust this\n");
2343                         if (image->numcomps < (OPJ_UINT32)((len - 38) / 3)) {
2344                                 len = 38 + 3 * image->numcomps;
2345                                 opj_event_msg(j2k->cinfo, EVT_WARNING, "- setting Lsiz to %d => HYPOTHESIS!!!\n",
2346                                         len);                           
2347                         } else {
2348                                 image->numcomps = ((len - 38) / 3);
2349                                 opj_event_msg(j2k->cinfo, EVT_WARNING, "- setting Csiz to %d => HYPOTHESIS!!!\n",
2350                                         image->numcomps);                               
2351                         }
2352                 }
2353
2354                 /* update components number in the jpwl_exp_comps filed */
2355                 cp->exp_comps = image->numcomps;
2356         }
2357 #endif /* USE_JPWL */
2358
2359         image->comps = (opj_image_comp_t*) opj_calloc(image->numcomps, sizeof(opj_image_comp_t));
2360         for (i = 0; i < image->numcomps; i++) {
2361                 int tmp, w, h;
2362                 tmp = cio_read(cio, 1);         /* Ssiz_i */
2363                 image->comps[i].prec = (tmp & 0x7f) + 1;
2364                 image->comps[i].sgnd = tmp >> 7;
2365                 image->comps[i].dx = cio_read(cio, 1);  /* XRsiz_i */
2366                 image->comps[i].dy = cio_read(cio, 1);  /* YRsiz_i */
2367                 
2368 #ifdef USE_JPWL
2369                 if (j2k->cp->correct) {
2370                 /* if JPWL is on, we check whether TX errors have damaged
2371                         too much the SIZ parameters, again */
2372                         if (!(image->comps[i].dx * image->comps[i].dy)) {
2373                                 opj_event_msg(j2k->cinfo, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
2374                                         "JPWL: bad XRsiz_%d/YRsiz_%d (%d x %d)\n",
2375                                         i, i, image->comps[i].dx, image->comps[i].dy);
2376                                 if (!JPWL_ASSUME) {
2377                                         opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
2378                                         return;
2379                                 }
2380                                 /* we try to correct */
2381                                 opj_event_msg(j2k->cinfo, EVT_WARNING, "- trying to adjust them\n");
2382                                 if (!image->comps[i].dx) {
2383                                         image->comps[i].dx = 1;
2384                                         opj_event_msg(j2k->cinfo, EVT_WARNING, "- setting XRsiz_%d to %d => HYPOTHESIS!!!\n",
2385                                                 i, image->comps[i].dx);
2386                                 }
2387                                 if (!image->comps[i].dy) {
2388                                         image->comps[i].dy = 1;
2389                                         opj_event_msg(j2k->cinfo, EVT_WARNING, "- setting YRsiz_%d to %d => HYPOTHESIS!!!\n",
2390                                                 i, image->comps[i].dy);
2391                                 }
2392                         }
2393                         
2394                 }
2395 #endif /* USE_JPWL */
2396
2397                 /* TODO: unused ? */
2398                 w = int_ceildiv(image->x1 - image->x0, image->comps[i].dx);
2399                 h = int_ceildiv(image->y1 - image->y0, image->comps[i].dy);
2400
2401                 image->comps[i].resno_decoded = 0;      /* number of resolution decoded */
2402                 image->comps[i].factor = cp->reduce; /* reducing factor per component */
2403         }
2404         
2405         cp->tw = int_ceildiv(image->x1 - cp->tx0, cp->tdx);
2406         cp->th = int_ceildiv(image->y1 - cp->ty0, cp->tdy);
2407
2408 #ifdef USE_JPWL
2409         if (j2k->cp->correct) {
2410                 /* if JPWL is on, we check whether TX errors have damaged
2411                   too much the SIZ parameters */
2412                 if ((cp->tw < 1) || (cp->th < 1) || (cp->tw > cp->max_tiles) || (cp->th > cp->max_tiles)) {
2413                         opj_event_msg(j2k->cinfo, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
2414                                 "JPWL: bad number of tiles (%d x %d)\n",
2415                                 cp->tw, cp->th);
2416                         if (!JPWL_ASSUME) {
2417                                 opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
2418                                 return;
2419                         }
2420                         /* we try to correct */
2421                         opj_event_msg(j2k->cinfo, EVT_WARNING, "- trying to adjust them\n");
2422                         if (cp->tw < 1) {
2423                                 cp->tw= 1;
2424                                 opj_event_msg(j2k->cinfo, EVT_WARNING, "- setting %d tiles in x => HYPOTHESIS!!!\n",
2425                                         cp->tw);
2426                         }
2427                         if (cp->tw > cp->max_tiles) {
2428                                 cp->tw= 1;
2429                                 opj_event_msg(j2k->cinfo, EVT_WARNING, "- too large x, increase expectance of %d\n"
2430                                         "- setting %d tiles in x => HYPOTHESIS!!!\n",
2431                                         cp->max_tiles, cp->tw);
2432                         }
2433                         if (cp->th < 1) {
2434                                 cp->th= 1;
2435                                 opj_event_msg(j2k->cinfo, EVT_WARNING, "- setting %d tiles in y => HYPOTHESIS!!!\n",
2436                                         cp->th);
2437                         }
2438                         if (cp->th > cp->max_tiles) {
2439                                 cp->th= 1;
2440                                 opj_event_msg(j2k->cinfo, EVT_WARNING, "- too large y, increase expectance of %d to continue\n",
2441                                         "- setting %d tiles in y => HYPOTHESIS!!!\n",
2442                                         cp->max_tiles, cp->th);
2443                         }
2444                 }
2445         }
2446 #endif /* USE_JPWL */
2447
2448         cp->tcps = (opj_tcp_t*) opj_calloc(cp->tw * cp->th, sizeof(opj_tcp_t));
2449         cp->tileno = (int*) opj_malloc(cp->tw * cp->th * sizeof(int));
2450         cp->tileno_size = 0;
2451         
2452 #ifdef USE_JPWL
2453         if (j2k->cp->correct) {
2454                 if (!cp->tcps) {
2455                         opj_event_msg(j2k->cinfo, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
2456                                 "JPWL: could not alloc tcps field of cp\n");
2457                         if (!JPWL_ASSUME || JPWL_ASSUME) {
2458                                 opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
2459                                 return;
2460                         }
2461                 }
2462         }
2463 #endif /* USE_JPWL */
2464
2465         for (i = 0; i < (OPJ_UINT32)cp->tw * cp->th; i++) {
2466                 cp->tcps[i].POC = 0;
2467                 cp->tcps[i].numpocs = 0;
2468                 cp->tcps[i].first = 1;
2469         }
2470         
2471         /* Initialization for PPM marker */
2472         cp->ppm = 0;
2473         cp->ppm_data = NULL;
2474         cp->ppm_data_first = NULL;
2475         cp->ppm_previous = 0;
2476         cp->ppm_store = 0;
2477
2478         j2k->default_tcp->tccps = (opj_tccp_t*) opj_calloc(image->numcomps, sizeof(opj_tccp_t));
2479         for (i = 0; i < (OPJ_UINT32)cp->tw * cp->th; i++) {
2480                 cp->tcps[i].tccps = (opj_tccp_t*) opj_malloc(image->numcomps * sizeof(opj_tccp_t));
2481         }       
2482         j2k->tile_data = (unsigned char**) opj_calloc(cp->tw * cp->th, sizeof(unsigned char*));
2483         j2k->tile_len = (int*) opj_calloc(cp->tw * cp->th, sizeof(int));
2484         j2k->state = J2K_STATE_MH;
2485
2486         /* Index */
2487         if (j2k->cstr_info) {
2488                 opj_codestream_info_t *cstr_info = j2k->cstr_info;
2489                 cstr_info->image_w = image->x1 - image->x0;
2490                 cstr_info->image_h = image->y1 - image->y0;
2491                 cstr_info->numcomps = image->numcomps;
2492                 cstr_info->tw = cp->tw;
2493                 cstr_info->th = cp->th;
2494                 cstr_info->tile_x = cp->tdx;    
2495                 cstr_info->tile_y = cp->tdy;    
2496                 cstr_info->tile_Ox = cp->tx0;   
2497                 cstr_info->tile_Oy = cp->ty0;                   
2498                 cstr_info->tile = (opj_tile_info_t*) opj_calloc(cp->tw * cp->th, sizeof(opj_tile_info_t));              
2499         }
2500 }
2501
2502
2503 /**
2504  * Reads a SIZ marker (image and tile size)
2505  * @param       p_header_data   the data contained in the SIZ box.
2506  * @param       jp2                             the jpeg2000 file codec.
2507  * @param       p_header_size   the size of the data contained in the SIZ marker.
2508  * @param       p_manager               the user event manager.
2509 */
2510 opj_bool j2k_read_siz_v2 (
2511                                     opj_j2k_v2_t *p_j2k,
2512                                         OPJ_BYTE * p_header_data,
2513                                         OPJ_UINT32 p_header_size,
2514                                         struct opj_event_mgr * p_manager
2515                                         )
2516 {
2517         OPJ_UINT32 l_size, i;
2518         OPJ_UINT32 l_nb_comp;
2519         OPJ_UINT32 l_nb_comp_remain;
2520         OPJ_UINT32 l_remaining_size;
2521         OPJ_UINT32 l_nb_tiles;
2522         OPJ_UINT32 l_tmp;
2523         opj_image_t *l_image = 00;
2524         opj_cp_v2_t *l_cp = 00;
2525         opj_image_comp_t * l_img_comp = 00;
2526         opj_tcp_v2_t * l_current_tile_param = 00;
2527
2528         /* preconditions */
2529         assert(p_j2k != 00);
2530         assert(p_manager != 00);
2531         assert(p_header_data != 00);
2532
2533         l_image = p_j2k->m_private_image;
2534         l_cp = &(p_j2k->m_cp);
2535
2536         /* minimum size == 39 - 3 (= minimum component parameter) */
2537         if (p_header_size < 36) {
2538                 opj_event_msg_v2(p_manager, EVT_ERROR, "Error with SIZ marker size\n");
2539                 return OPJ_FALSE;
2540         }
2541
2542         l_remaining_size = p_header_size - 36;
2543         l_nb_comp = l_remaining_size / 3;
2544         l_nb_comp_remain = l_remaining_size % 3;
2545         if (l_nb_comp_remain != 0){
2546                 opj_event_msg_v2(p_manager, EVT_ERROR, "Error with SIZ marker size\n");
2547                 return OPJ_FALSE;
2548         }
2549
2550         l_size = p_header_size + 2;                                                                             /* Lsiz */
2551
2552         opj_read_bytes(p_header_data,&l_tmp ,2);                                                /* Rsiz (capabilities) */
2553         p_header_data+=2;
2554         l_cp->rsiz = (OPJ_RSIZ_CAPABILITIES) l_tmp;
2555         opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_image->x1, 4);   /* Xsiz */
2556         p_header_data+=4;
2557         opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_image->y1, 4);   /* Ysiz */
2558         p_header_data+=4;
2559         opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_image->x0, 4);   /* X0siz */
2560         p_header_data+=4;
2561         opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_image->y0, 4);   /* Y0siz */
2562         p_header_data+=4;
2563         opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_cp->tdx, 4);             /* XTsiz */
2564         p_header_data+=4;
2565         opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_cp->tdy, 4);             /* YTsiz */
2566         p_header_data+=4;
2567         opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_cp->tx0, 4);             /* XT0siz */
2568         p_header_data+=4;
2569         opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_cp->ty0, 4);             /* YT0siz */
2570         p_header_data+=4;
2571         opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_tmp, 2);                 /* Csiz */
2572         p_header_data+=2;
2573         if (l_tmp < 16385)
2574                 l_image->numcomps = (OPJ_UINT16) l_tmp;
2575         else {
2576                 opj_event_msg_v2(p_manager, EVT_ERROR, "Error with SIZ marker: number of component is illegal -> %d\n", l_tmp);
2577                 return OPJ_FALSE;
2578         }
2579
2580         if (l_image->numcomps != l_nb_comp) {
2581                 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);
2582                 return OPJ_FALSE;
2583         }
2584
2585 #ifdef USE_JPWL
2586         if (l_cp->correct) {
2587                 /* if JPWL is on, we check whether TX errors have damaged
2588                   too much the SIZ parameters */
2589                 if (!(l_image->x1 * l_image->y1)) {
2590                         opj_event_msg_v2(p_manager, EVT_ERROR,
2591                                 "JPWL: bad image size (%d x %d)\n",
2592                                 l_image->x1, l_image->y1);
2593                         if (!JPWL_ASSUME || JPWL_ASSUME) {
2594                                 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
2595                                 return OPJ_FALSE;
2596                         }
2597                 }
2598
2599         /* FIXME check previously in the function so why keep this piece of code ? Need by the norm ?
2600                 if (l_image->numcomps != ((len - 38) / 3)) {
2601                         opj_event_msg_v2(p_manager, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
2602                                 "JPWL: Csiz is %d => space in SIZ only for %d comps.!!!\n",
2603                                 l_image->numcomps, ((len - 38) / 3));
2604                         if (!JPWL_ASSUME) {
2605                                 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
2606                                 return OPJ_FALSE;
2607                         }
2608         */              /* we try to correct */
2609         /*              opj_event_msg_v2(p_manager, EVT_WARNING, "- trying to adjust this\n");
2610                         if (l_image->numcomps < ((len - 38) / 3)) {
2611                                 len = 38 + 3 * l_image->numcomps;
2612                                 opj_event_msg_v2(p_manager, EVT_WARNING, "- setting Lsiz to %d => HYPOTHESIS!!!\n",
2613                                         len);
2614                         } else {
2615                                 l_image->numcomps = ((len - 38) / 3);
2616                                 opj_event_msg_v2(p_manager, EVT_WARNING, "- setting Csiz to %d => HYPOTHESIS!!!\n",
2617                                         l_image->numcomps);
2618                         }
2619                 }
2620         */
2621
2622                 /* update components number in the jpwl_exp_comps filed */
2623                 l_cp->exp_comps = l_image->numcomps;
2624         }
2625 #endif /* USE_JPWL */
2626
2627         /* Allocate the resulting image components */
2628         l_image->comps = (opj_image_comp_t*) opj_calloc(l_image->numcomps, sizeof(opj_image_comp_t));
2629         if (l_image->comps == 00){
2630                 l_image->numcomps = 0;
2631                 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to take in charge SIZ marker\n");
2632                 return OPJ_FALSE;
2633         }
2634
2635         memset(l_image->comps,0,l_image->numcomps * sizeof(opj_image_comp_t));
2636         l_img_comp = l_image->comps;
2637
2638         /* Read the component information */
2639         for (i = 0; i < l_image->numcomps; ++i){
2640                 OPJ_UINT32 tmp;
2641                 opj_read_bytes(p_header_data,&tmp,1);   /* Ssiz_i */
2642                 ++p_header_data;
2643                 l_img_comp->prec = (tmp & 0x7f) + 1;
2644                 l_img_comp->sgnd = tmp >> 7;
2645                 opj_read_bytes(p_header_data,&tmp,1);   /* XRsiz_i */
2646                 ++p_header_data;
2647                 l_img_comp->dx = (OPJ_INT32)tmp; /* should be between 1 and 255 */
2648                 opj_read_bytes(p_header_data,&tmp,1);   /* YRsiz_i */
2649                 ++p_header_data;
2650                 l_img_comp->dy = (OPJ_INT32)tmp; /* should be between 1 and 255 */
2651
2652 #ifdef USE_JPWL
2653                 if (l_cp->correct) {
2654                 /* if JPWL is on, we check whether TX errors have damaged
2655                         too much the SIZ parameters, again */
2656                         if (!(l_image->comps[i].dx * l_image->comps[i].dy)) {
2657                                 opj_event_msg_v2(p_manager, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
2658                                         "JPWL: bad XRsiz_%d/YRsiz_%d (%d x %d)\n",
2659                                         i, i, l_image->comps[i].dx, l_image->comps[i].dy);
2660                                 if (!JPWL_ASSUME) {
2661                                         opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
2662                                         return OPJ_FALSE;
2663                                 }
2664                                 /* we try to correct */
2665                                 opj_event_msg_v2(p_manager, EVT_WARNING, "- trying to adjust them\n");
2666                                 if (!l_image->comps[i].dx) {
2667                                         l_image->comps[i].dx = 1;
2668                                         opj_event_msg_v2(p_manager, EVT_WARNING, "- setting XRsiz_%d to %d => HYPOTHESIS!!!\n",
2669                                                 i, l_image->comps[i].dx);
2670                                 }
2671                                 if (!l_image->comps[i].dy) {
2672                                         l_image->comps[i].dy = 1;
2673                                         opj_event_msg_v2(p_manager, EVT_WARNING, "- setting YRsiz_%d to %d => HYPOTHESIS!!!\n",
2674                                                 i, l_image->comps[i].dy);
2675                                 }
2676                         }
2677                 }
2678 #endif /* USE_JPWL */
2679                 l_img_comp->resno_decoded = 0;                                                          /* number of resolution decoded */
2680                 l_img_comp->factor = l_cp->m_specific_param.m_dec.m_reduce; /* reducing factor per component */
2681                 ++l_img_comp;
2682         }
2683
2684         /* Compute the number of tiles */
2685         l_cp->tw = int_ceildiv(l_image->x1 - l_cp->tx0, l_cp->tdx);
2686         l_cp->th = int_ceildiv(l_image->y1 - l_cp->ty0, l_cp->tdy);
2687         l_nb_tiles = l_cp->tw * l_cp->th;
2688
2689         /* Define the tiles which will be decoded */
2690         if (p_j2k->m_specific_param.m_decoder.m_discard_tiles) {
2691                 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;
2692                 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;
2693                 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);
2694                 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);
2695         }
2696         else {
2697                 p_j2k->m_specific_param.m_decoder.m_start_tile_x = 0;
2698                 p_j2k->m_specific_param.m_decoder.m_start_tile_y = 0;
2699                 p_j2k->m_specific_param.m_decoder.m_end_tile_x = l_cp->tw;
2700                 p_j2k->m_specific_param.m_decoder.m_end_tile_y = l_cp->th;
2701         }
2702
2703 #ifdef USE_JPWL
2704         if (l_cp->correct) {
2705                 /* if JPWL is on, we check whether TX errors have damaged
2706                   too much the SIZ parameters */
2707                 if ((l_cp->tw < 1) || (l_cp->th < 1) || (l_cp->tw > l_cp->max_tiles) || (l_cp->th > l_cp->max_tiles)) {
2708                         opj_event_msg_v2(p_manager, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
2709                                 "JPWL: bad number of tiles (%d x %d)\n",
2710                                 l_cp->tw, l_cp->th);
2711                         if (!JPWL_ASSUME) {
2712                                 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
2713                                 return OPJ_FALSE;
2714                         }
2715                         /* we try to correct */
2716                         opj_event_msg_v2(p_manager, EVT_WARNING, "- trying to adjust them\n");
2717                         if (l_cp->tw < 1) {
2718                                 l_cp->tw= 1;
2719                                 opj_event_msg_v2(p_manager, EVT_WARNING, "- setting %d tiles in x => HYPOTHESIS!!!\n",
2720                                                 l_cp->tw);
2721                         }
2722                         if (l_cp->tw > l_cp->max_tiles) {
2723                                 l_cp->tw= 1;
2724                                 opj_event_msg_v2(p_manager, EVT_WARNING, "- too large x, increase expectance of %d\n"
2725                                         "- setting %d tiles in x => HYPOTHESIS!!!\n",
2726                                         l_cp->max_tiles, l_cp->tw);
2727                         }
2728                         if (l_cp->th < 1) {
2729                                 l_cp->th= 1;
2730                                 opj_event_msg_v2(p_manager, EVT_WARNING, "- setting %d tiles in y => HYPOTHESIS!!!\n",
2731                                                 l_cp->th);
2732                         }
2733                         if (l_cp->th > l_cp->max_tiles) {
2734                                 l_cp->th= 1;
2735                                 opj_event_msg_v2(p_manager, EVT_WARNING, "- too large y, increase expectance of %d to continue\n",
2736                                         "- setting %d tiles in y => HYPOTHESIS!!!\n",
2737                                         l_cp->max_tiles, l_cp->th);
2738                         }
2739                 }
2740         }
2741 #endif /* USE_JPWL */
2742
2743         /* memory allocations */
2744         l_cp->tcps = (opj_tcp_v2_t*) opj_calloc(l_nb_tiles, sizeof(opj_tcp_v2_t));
2745         if (l_cp->tcps == 00) {
2746                 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to take in charge SIZ marker\n");
2747                 return OPJ_FALSE;
2748         }
2749         memset(l_cp->tcps,0,l_nb_tiles*sizeof(opj_tcp_t));
2750
2751 #ifdef USE_JPWL
2752         if (l_cp->correct) {
2753                 if (!l_cp->tcps) {
2754                         opj_event_msg_v2(p_manager, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
2755                                 "JPWL: could not alloc tcps field of cp\n");
2756                         if (!JPWL_ASSUME || JPWL_ASSUME) {
2757                                 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
2758                                 return OPJ_FALSE;
2759                         }
2760                 }
2761         }
2762 #endif /* USE_JPWL */
2763
2764         p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps =
2765                         (opj_tccp_t*) opj_calloc(l_image->numcomps, sizeof(opj_tccp_t));
2766         if(p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps  == 00) {
2767                 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to take in charge SIZ marker\n");
2768                 return OPJ_FALSE;
2769         }
2770         memset(p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps ,0,l_image->numcomps*sizeof(opj_tccp_t));
2771
2772         p_j2k->m_specific_param.m_decoder.m_default_tcp->m_mct_records =
2773                         (opj_mct_data_t*)opj_malloc(J2K_MCT_DEFAULT_NB_RECORDS * sizeof(opj_mct_data_t));
2774
2775         if (! p_j2k->m_specific_param.m_decoder.m_default_tcp->m_mct_records) {
2776                 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to take in charge SIZ marker\n");
2777                 return OPJ_FALSE;
2778         }
2779         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));
2780         p_j2k->m_specific_param.m_decoder.m_default_tcp->m_nb_max_mct_records = J2K_MCT_DEFAULT_NB_RECORDS;
2781
2782         p_j2k->m_specific_param.m_decoder.m_default_tcp->m_mcc_records =
2783                         (opj_simple_mcc_decorrelation_data_t*)
2784                         opj_malloc(J2K_MCC_DEFAULT_NB_RECORDS * sizeof(opj_simple_mcc_decorrelation_data_t));
2785
2786         if (! p_j2k->m_specific_param.m_decoder.m_default_tcp->m_mcc_records) {
2787                 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to take in charge SIZ marker\n");
2788                 return OPJ_FALSE;
2789         }
2790         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));
2791         p_j2k->m_specific_param.m_decoder.m_default_tcp->m_nb_max_mcc_records = J2K_MCC_DEFAULT_NB_RECORDS;
2792
2793         /* set up default dc level shift */
2794         for (i=0;i<l_image->numcomps;++i) {
2795                 if (! l_image->comps[i].sgnd) {
2796                         p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps[i].m_dc_level_shift = 1 << (l_image->comps[i].prec - 1);
2797                 }
2798         }
2799
2800         l_current_tile_param = l_cp->tcps;
2801         for     (i = 0; i < l_nb_tiles; ++i) {
2802                 l_current_tile_param->tccps = (opj_tccp_t*) opj_malloc(l_image->numcomps * sizeof(opj_tccp_t));
2803                 if (l_current_tile_param->tccps == 00) {
2804                         opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to take in charge SIZ marker\n");
2805                         return OPJ_FALSE;
2806                 }
2807                 memset(l_current_tile_param->tccps,0,l_image->numcomps * sizeof(opj_tccp_t));
2808
2809                 ++l_current_tile_param;
2810         }
2811
2812         p_j2k->m_specific_param.m_decoder.m_state =  J2K_STATE_MH; /* FIXME J2K_DEC_STATE_MH; */
2813         opj_image_comp_header_update(l_image,l_cp);
2814
2815         return OPJ_TRUE;
2816 }
2817
2818
2819
2820 static void j2k_write_com(opj_j2k_t *j2k) {
2821         unsigned int i;
2822         int lenp, len;
2823
2824         if(j2k->cp->comment) {
2825                 opj_cio_t *cio = j2k->cio;
2826                 char *comment = j2k->cp->comment;
2827
2828                 cio_write(cio, J2K_MS_COM, 2);
2829                 lenp = cio_tell(cio);
2830                 cio_skip(cio, 2);
2831                 cio_write(cio, 1, 2);           /* General use (IS 8859-15:1999 (Latin) values) */
2832                 for (i = 0; i < strlen(comment); i++) {
2833                         cio_write(cio, comment[i], 1);
2834                 }
2835                 len = cio_tell(cio) - lenp;
2836                 cio_seek(cio, lenp);
2837                 cio_write(cio, len, 2);
2838                 cio_seek(cio, lenp + len);
2839
2840                 
2841                 if(j2k->cstr_info)
2842                   j2k_add_mhmarker(j2k->cstr_info, J2K_MS_COM, lenp, len);
2843
2844         }
2845 }
2846
2847 /**
2848  * Writes the COM marker (comment)
2849  * 
2850  * @param       p_stream                        the stream to write data to.
2851  * @param       p_j2k                   J2K codec.
2852  * @param       p_manager       the user event manager.
2853 */
2854 opj_bool j2k_write_com_v2(      opj_j2k_v2_t *p_j2k,
2855                                                         struct opj_stream_private *p_stream,
2856                                                         struct opj_event_mgr * p_manager )
2857 {
2858         OPJ_UINT32 l_comment_size;
2859         OPJ_UINT32 l_total_com_size;
2860         const OPJ_CHAR *l_comment;
2861         OPJ_BYTE * l_current_ptr = 00;
2862
2863         // preconditions
2864         assert(p_j2k != 00);
2865         assert(p_stream != 00);
2866         assert(p_manager != 00);
2867         
2868         l_comment = p_j2k->m_cp.comment;
2869         l_comment_size = strlen(l_comment);
2870         l_total_com_size = l_comment_size + 6;
2871
2872         if (l_total_com_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
2873                 p_j2k->m_specific_param.m_encoder.m_header_tile_data 
2874                         = (OPJ_BYTE*)opj_realloc(       p_j2k->m_specific_param.m_encoder.m_header_tile_data,
2875                                                                                 l_total_com_size);
2876                 
2877                 if(! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
2878                         return OPJ_FALSE;
2879                 }
2880
2881                 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_total_com_size;
2882         }
2883
2884         l_current_ptr = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
2885         
2886         opj_write_bytes(l_current_ptr,J2K_MS_COM , 2);  /* COM */
2887         l_current_ptr+=2;
2888         
2889         opj_write_bytes(l_current_ptr,l_total_com_size - 2 , 2);        /* L_COM */
2890         l_current_ptr+=2;
2891         
2892         opj_write_bytes(l_current_ptr,1 , 2);   /* General use (IS 8859-15:1999 (Latin) values) */
2893         l_current_ptr+=2;
2894         
2895         memcpy( l_current_ptr,l_comment,l_comment_size);
2896         
2897         if (opj_stream_write_data(p_stream,p_j2k->m_specific_param.m_encoder.m_header_tile_data,l_total_com_size,p_manager) != l_total_com_size) {
2898                 return OPJ_FALSE;
2899         }
2900
2901         return OPJ_TRUE;
2902 }
2903
2904 static void j2k_read_com(opj_j2k_t *j2k) {
2905         int len;
2906         
2907         opj_cio_t *cio = j2k->cio;
2908
2909         len = cio_read(cio, 2);
2910         cio_skip(cio, len - 2);  
2911 }
2912 /**
2913  * Reads a COM marker (comments)
2914  * @param       p_header_data   the data contained in the COM box.
2915  * @param       jp2                             the jpeg2000 file codec.
2916  * @param       p_header_size   the size of the data contained in the COM marker.
2917  * @param       p_manager               the user event manager.
2918 */
2919 opj_bool j2k_read_com_v2 (
2920                                         opj_j2k_v2_t *p_j2k,
2921                                         OPJ_BYTE * p_header_data,
2922                                         OPJ_UINT32 p_header_size,
2923                                         struct opj_event_mgr * p_manager
2924                                         )
2925 {
2926         /* preconditions */
2927         assert(p_j2k != 00);
2928         assert(p_manager != 00);
2929         assert(p_header_data != 00);
2930   (void)p_header_size;
2931
2932         return OPJ_TRUE;
2933 }
2934
2935 static void j2k_write_cox(opj_j2k_t *j2k, int compno) {
2936         OPJ_UINT32 i;
2937
2938         opj_cp_t *cp = j2k->cp;
2939         opj_tcp_t *tcp = &cp->tcps[j2k->curtileno];
2940         opj_tccp_t *tccp = &tcp->tccps[compno];
2941         opj_cio_t *cio = j2k->cio;
2942         
2943         cio_write(cio, tccp->numresolutions - 1, 1);    /* SPcox (D) */
2944         cio_write(cio, tccp->cblkw - 2, 1);                             /* SPcox (E) */
2945         cio_write(cio, tccp->cblkh - 2, 1);                             /* SPcox (F) */
2946         cio_write(cio, tccp->cblksty, 1);                               /* SPcox (G) */
2947         cio_write(cio, tccp->qmfbid, 1);                                /* SPcox (H) */
2948         
2949         if (tccp->csty & J2K_CCP_CSTY_PRT) {
2950                 for (i = 0; i < tccp->numresolutions; i++) {
2951                         cio_write(cio, tccp->prcw[i] + (tccp->prch[i] << 4), 1);        /* SPcox (I_i) */
2952                 }
2953         }
2954 }
2955
2956 static void j2k_read_cox(opj_j2k_t *j2k, int compno) {
2957         OPJ_UINT32 i;
2958
2959         opj_cp_t *cp = j2k->cp;
2960         opj_tcp_t *tcp = j2k->state == J2K_STATE_TPH ? &cp->tcps[j2k->curtileno] : j2k->default_tcp;
2961         opj_tccp_t *tccp = &tcp->tccps[compno];
2962         opj_cio_t *cio = j2k->cio;
2963
2964         tccp->numresolutions = cio_read(cio, 1) + 1;    /* SPcox (D) */
2965
2966         /* If user wants to remove more resolutions than the codestream contains, return error*/
2967         assert(cp->reduce >= 0);
2968         if ((OPJ_UINT32)cp->reduce >= tccp->numresolutions) {
2969                 opj_event_msg(j2k->cinfo, EVT_ERROR, "Error decoding component %d.\nThe number of resolutions to remove is higher than the number "
2970                                         "of resolutions of this component\nModify the cp_reduce parameter.\n\n", compno);
2971                 j2k->state |= J2K_STATE_ERR;
2972         }
2973
2974         tccp->cblkw = cio_read(cio, 1) + 2;     /* SPcox (E) */
2975         tccp->cblkh = cio_read(cio, 1) + 2;     /* SPcox (F) */
2976         tccp->cblksty = cio_read(cio, 1);       /* SPcox (G) */
2977         tccp->qmfbid = cio_read(cio, 1);        /* SPcox (H) */
2978         if (tccp->csty & J2K_CP_CSTY_PRT) {
2979                 for (i = 0; i < tccp->numresolutions; i++) {
2980                         int tmp = cio_read(cio, 1);     /* SPcox (I_i) */
2981                         tccp->prcw[i] = tmp & 0xf;
2982                         tccp->prch[i] = tmp >> 4;
2983                 }
2984         }
2985
2986         /* INDEX >> */
2987         if(j2k->cstr_info && compno == 0) {
2988                 for (i = 0; i < tccp->numresolutions; i++) {
2989                         if (tccp->csty & J2K_CP_CSTY_PRT) {
2990                                 j2k->cstr_info->tile[j2k->curtileno].pdx[i] = tccp->prcw[i];
2991                                 j2k->cstr_info->tile[j2k->curtileno].pdy[i] = tccp->prch[i];
2992                         }
2993                         else {
2994                                 j2k->cstr_info->tile[j2k->curtileno].pdx[i] = 15;
2995                                 j2k->cstr_info->tile[j2k->curtileno].pdx[i] = 15;
2996                         }
2997                 }
2998         }
2999         /* << INDEX */
3000 }
3001
3002 static void j2k_write_cod(opj_j2k_t *j2k) {
3003         opj_cp_t *cp = NULL;
3004         opj_tcp_t *tcp = NULL;
3005         int lenp, len;
3006
3007         opj_cio_t *cio = j2k->cio;
3008         
3009         cio_write(cio, J2K_MS_COD, 2);  /* COD */
3010         
3011         lenp = cio_tell(cio);
3012         cio_skip(cio, 2);
3013         
3014         cp = j2k->cp;
3015         tcp = &cp->tcps[j2k->curtileno];
3016
3017         cio_write(cio, tcp->csty, 1);           /* Scod */
3018         cio_write(cio, tcp->prg, 1);            /* SGcod (A) */
3019         cio_write(cio, tcp->numlayers, 2);      /* SGcod (B) */
3020         cio_write(cio, tcp->mct, 1);            /* SGcod (C) */
3021         
3022         j2k_write_cox(j2k, 0);
3023         len = cio_tell(cio) - lenp;
3024         cio_seek(cio, lenp);
3025         cio_write(cio, len, 2);         /* Lcod */
3026         cio_seek(cio, lenp + len);
3027
3028         if(j2k->cstr_info)
3029           j2k_add_mhmarker(j2k->cstr_info, J2K_MS_COD, lenp, len);
3030
3031 }
3032
3033 /**
3034  * Writes the COD marker (Coding style default)
3035  *
3036  * @param       p_stream                        the stream to write data to.
3037  * @param       p_j2k                   J2K codec.
3038  * @param       p_manager       the user event manager.
3039 */
3040 opj_bool j2k_write_cod_v2(      opj_j2k_v2_t *p_j2k,
3041                                                         struct opj_stream_private *p_stream,
3042                                                         struct opj_event_mgr * p_manager )
3043 {
3044         opj_cp_v2_t *l_cp = 00;
3045         opj_tcp_v2_t *l_tcp = 00;
3046         OPJ_UINT32 l_code_size,l_remaining_size;
3047         OPJ_BYTE * l_current_data = 00;
3048
3049         /* preconditions */
3050         assert(p_j2k != 00);
3051         assert(p_manager != 00);
3052         assert(p_stream != 00);
3053
3054         l_cp = &(p_j2k->m_cp);
3055         l_tcp = &l_cp->tcps[p_j2k->m_current_tile_number];
3056         l_code_size = 9 + j2k_get_SPCod_SPCoc_size(p_j2k,p_j2k->m_current_tile_number,0);
3057         l_remaining_size = l_code_size;
3058
3059         if (l_code_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
3060                 p_j2k->m_specific_param.m_encoder.m_header_tile_data
3061                         = (OPJ_BYTE*)opj_realloc(
3062                                 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
3063                                 l_code_size);
3064
3065                 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
3066                         return OPJ_FALSE;
3067                 }
3068
3069                 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_code_size;
3070         }
3071
3072         l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
3073
3074         opj_write_bytes(l_current_data,J2K_MS_COD,2);           /* COD */
3075         l_current_data += 2;
3076
3077         opj_write_bytes(l_current_data,l_code_size-2,2);        /* L_COD */
3078         l_current_data += 2;
3079
3080         opj_write_bytes(l_current_data,l_tcp->csty,1);          /* Scod */
3081         ++l_current_data;
3082
3083         opj_write_bytes(l_current_data,l_tcp->prg,1);           /* SGcod (A) */
3084         ++l_current_data;
3085
3086         opj_write_bytes(l_current_data,l_tcp->numlayers,2);     /* SGcod (B) */
3087         l_current_data+=2;
3088
3089         opj_write_bytes(l_current_data,l_tcp->mct,1);           /* SGcod (C) */
3090         ++l_current_data;
3091
3092         l_remaining_size -= 9;
3093
3094         if (! j2k_write_SPCod_SPCoc(p_j2k,p_j2k->m_current_tile_number,0,l_current_data,&l_remaining_size,p_manager)) {
3095                 opj_event_msg_v2(p_manager, EVT_ERROR, "Error writting COD marker\n");
3096                 return OPJ_FALSE;
3097         }
3098
3099         if (l_remaining_size != 0) {
3100                 opj_event_msg_v2(p_manager, EVT_ERROR, "Error writting COD marker\n");
3101                 return OPJ_FALSE;
3102         }
3103
3104         if (opj_stream_write_data(p_stream,p_j2k->m_specific_param.m_encoder.m_header_tile_data,l_code_size,p_manager) != l_code_size) {
3105                 return OPJ_FALSE;
3106         }
3107
3108         return OPJ_TRUE;
3109 }
3110
3111 static void j2k_read_cod(opj_j2k_t *j2k) {
3112         int len, pos;
3113   OPJ_UINT32 i;
3114         
3115         opj_cio_t *cio = j2k->cio;
3116         opj_cp_t *cp = j2k->cp;
3117         opj_tcp_t *tcp = j2k->state == J2K_STATE_TPH ? &cp->tcps[j2k->curtileno] : j2k->default_tcp;
3118         opj_image_t *image = j2k->image;
3119         
3120         len = cio_read(cio, 2);                         /* Lcod */
3121         tcp->csty = cio_read(cio, 1);           /* Scod */
3122         tcp->prg = (OPJ_PROG_ORDER)cio_read(cio, 1);            /* SGcod (A) */
3123         tcp->numlayers = cio_read(cio, 2);      /* SGcod (B) */
3124         tcp->mct = cio_read(cio, 1);            /* SGcod (C) */
3125         
3126         pos = cio_tell(cio);
3127         for (i = 0; i < image->numcomps; i++) {
3128                 tcp->tccps[i].csty = tcp->csty & J2K_CP_CSTY_PRT;
3129                 cio_seek(cio, pos);
3130                 j2k_read_cox(j2k, i);
3131         }
3132
3133         /* Index */
3134         if (j2k->cstr_info) {
3135                 opj_codestream_info_t *cstr_info = j2k->cstr_info;
3136                 cstr_info->prog = tcp->prg;
3137                 cstr_info->numlayers = tcp->numlayers;
3138                 cstr_info->numdecompos = (int*) opj_malloc(image->numcomps * sizeof(int));
3139                 for (i = 0; i < image->numcomps; i++) {
3140                         cstr_info->numdecompos[i] = tcp->tccps[i].numresolutions - 1;
3141                 }
3142         }
3143 }
3144
3145 /**
3146  * Reads a COD marker (Coding Styke defaults)
3147  * @param       p_header_data   the data contained in the COD box.
3148  * @param       p_j2k                   the jpeg2000 codec.
3149  * @param       p_header_size   the size of the data contained in the COD marker.
3150  * @param       p_manager               the user event manager.
3151 */
3152 opj_bool j2k_read_cod_v2 (
3153                                         opj_j2k_v2_t *p_j2k,
3154                                         OPJ_BYTE * p_header_data,
3155                                         OPJ_UINT32 p_header_size,
3156                                         struct opj_event_mgr * p_manager
3157                                         )
3158 {
3159         /* loop */
3160         OPJ_UINT32 i;
3161         OPJ_UINT32 l_tmp;
3162         opj_cp_v2_t *l_cp = 00;
3163         opj_tcp_v2_t *l_tcp = 00;
3164         opj_image_t *l_image = 00;
3165
3166         /* preconditions */
3167         assert(p_header_data != 00);
3168         assert(p_j2k != 00);
3169         assert(p_manager != 00);
3170
3171         l_image = p_j2k->m_private_image;
3172         l_cp = &(p_j2k->m_cp);
3173
3174         /* If we are in the first tile-part header of the current tile */
3175         l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ?
3176                                 &l_cp->tcps[p_j2k->m_current_tile_number] :
3177                                 p_j2k->m_specific_param.m_decoder.m_default_tcp;
3178
3179         /* Make sure room is sufficient */
3180         if (p_header_size < 5) {
3181                 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading COD marker\n");
3182                 return OPJ_FALSE;
3183         }
3184
3185         opj_read_bytes(p_header_data,&l_tcp->csty,1);           /* Scod */
3186         ++p_header_data;
3187         opj_read_bytes(p_header_data,&l_tmp,1);                         /* SGcod (A) */
3188         ++p_header_data;
3189         l_tcp->prg = (OPJ_PROG_ORDER) l_tmp;
3190         opj_read_bytes(p_header_data,&l_tcp->numlayers,2);      /* SGcod (B) */
3191         p_header_data+=2;
3192
3193         /* If user didn't set a number layer to decode take the max specify in the codestream. */
3194         if      (l_cp->m_specific_param.m_dec.m_layer) {
3195                 l_tcp->num_layers_to_decode = l_cp->m_specific_param.m_dec.m_layer;
3196         }
3197         else {
3198                 l_tcp->num_layers_to_decode = l_tcp->numlayers;
3199         }
3200
3201         opj_read_bytes(p_header_data,&l_tcp->mct,1);            /* SGcod (C) */
3202         ++p_header_data;
3203
3204         p_header_size -= 5;
3205         for     (i = 0; i < l_image->numcomps; ++i) {
3206                 l_tcp->tccps[i].csty = l_tcp->csty & J2K_CCP_CSTY_PRT;
3207         }
3208
3209         if (! j2k_read_SPCod_SPCoc(p_j2k,0,p_header_data,&p_header_size,p_manager)) {
3210                 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading COD marker\n");
3211                 return OPJ_FALSE;
3212         }
3213
3214         if (p_header_size != 0) {
3215                 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading COD marker\n");
3216                 return OPJ_FALSE;
3217         }
3218
3219         /* Apply the coding style to other components of the current tile or the m_default_tcp*/
3220         j2k_copy_tile_component_parameters(p_j2k);
3221
3222         /* Index */
3223 #ifdef WIP_REMOVE_MSD
3224         if (p_j2k->cstr_info) {
3225                 /*opj_codestream_info_t *l_cstr_info = p_j2k->cstr_info;*/
3226                 p_j2k->cstr_info->prog = l_tcp->prg;
3227                 p_j2k->cstr_info->numlayers = l_tcp->numlayers;
3228                 p_j2k->cstr_info->numdecompos = (OPJ_INT32*) opj_malloc(l_image->numcomps * sizeof(OPJ_UINT32));
3229                 for     (i = 0; i < l_image->numcomps; ++i) {
3230                         p_j2k->cstr_info->numdecompos[i] = l_tcp->tccps[i].numresolutions - 1;
3231                 }
3232         }
3233 #endif
3234
3235         return OPJ_TRUE;
3236 }
3237
3238 static void j2k_write_coc(opj_j2k_t *j2k, int compno) {
3239         int lenp, len;
3240
3241         opj_cp_t *cp = j2k->cp;
3242         opj_tcp_t *tcp = &cp->tcps[j2k->curtileno];
3243         opj_image_t *image = j2k->image;
3244         opj_cio_t *cio = j2k->cio;
3245         
3246         cio_write(cio, J2K_MS_COC, 2);  /* COC */
3247         lenp = cio_tell(cio);
3248         cio_skip(cio, 2);
3249         cio_write(cio, compno, image->numcomps <= 256 ? 1 : 2); /* Ccoc */
3250         cio_write(cio, tcp->tccps[compno].csty, 1);     /* Scoc */
3251         j2k_write_cox(j2k, compno);
3252         len = cio_tell(cio) - lenp;
3253         cio_seek(cio, lenp);
3254         cio_write(cio, len, 2);                 /* Lcoc */
3255         cio_seek(cio, lenp + len);
3256 }
3257
3258 /**
3259  * Writes the COC marker (Coding style component)
3260  *
3261  * @param       p_comp_no               the index of the component to output.
3262  * @param       p_stream                                the stream to write data to.
3263  * @param       p_j2k                           J2K codec.
3264  * @param       p_manager               the user event manager.
3265 */
3266 opj_bool j2k_write_coc_v2(      opj_j2k_v2_t *p_j2k,
3267                                                         OPJ_UINT32 p_comp_no,
3268                                                         struct opj_stream_private *p_stream,
3269                                                         struct opj_event_mgr * p_manager )
3270 {
3271         OPJ_UINT32 l_coc_size,l_remaining_size;
3272         OPJ_UINT32 l_comp_room;
3273
3274         /* preconditions */
3275         assert(p_j2k != 00);
3276         assert(p_manager != 00);
3277         assert(p_stream != 00);
3278
3279         l_comp_room = (p_j2k->m_private_image->numcomps <= 256) ? 1 : 2;
3280
3281         l_coc_size = 5 + l_comp_room + j2k_get_SPCod_SPCoc_size(p_j2k,p_j2k->m_current_tile_number,p_comp_no);
3282
3283         if (l_coc_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
3284                 p_j2k->m_specific_param.m_encoder.m_header_tile_data
3285                         = (OPJ_BYTE*)opj_realloc(
3286                                 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
3287                                 l_coc_size);
3288                 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
3289                         return OPJ_FALSE;
3290                 }
3291
3292                 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_coc_size;
3293         }
3294
3295         j2k_write_coc_in_memory(p_j2k,p_comp_no,p_j2k->m_specific_param.m_encoder.m_header_tile_data,&l_remaining_size,p_manager);
3296
3297         if (opj_stream_write_data(p_stream,p_j2k->m_specific_param.m_encoder.m_header_tile_data,l_coc_size,p_manager) != l_coc_size) {
3298                 return OPJ_FALSE;
3299         }
3300
3301         return OPJ_TRUE;
3302 }
3303
3304 /**
3305  * Writes the COC marker (Coding style component)
3306  *
3307  * @param       p_comp_no               the index of the component to output.
3308  * @param       p_stream                                the stream to write data to.
3309  * @param       p_j2k                           J2K codec.
3310  * @param       p_manager               the user event manager.
3311 */
3312 void j2k_write_coc_in_memory(   opj_j2k_v2_t *p_j2k,
3313                                                                 OPJ_UINT32 p_comp_no,
3314                                                                 OPJ_BYTE * p_data,
3315                                                                 OPJ_UINT32 * p_data_written,
3316                                                                 struct opj_event_mgr * p_manager )
3317 {
3318         opj_cp_v2_t *l_cp = 00;
3319         opj_tcp_v2_t *l_tcp = 00;
3320         OPJ_UINT32 l_coc_size,l_remaining_size;
3321         OPJ_BYTE * l_current_data = 00;
3322         opj_image_t *l_image = 00;
3323         OPJ_UINT32 l_comp_room;
3324
3325         /* preconditions */
3326         assert(p_j2k != 00);
3327         assert(p_manager != 00);
3328
3329         l_cp = &(p_j2k->m_cp);
3330         l_tcp = &l_cp->tcps[p_j2k->m_current_tile_number];
3331         l_image = p_j2k->m_private_image;
3332         l_comp_room = (l_image->numcomps <= 256) ? 1 : 2;
3333
3334         l_coc_size = 5 + l_comp_room + j2k_get_SPCod_SPCoc_size(p_j2k,p_j2k->m_current_tile_number,p_comp_no);
3335         l_remaining_size = l_coc_size;
3336
3337         l_current_data = p_data;
3338
3339         opj_write_bytes(l_current_data,J2K_MS_COC,2);                           /* COC */
3340         l_current_data += 2;
3341
3342         opj_write_bytes(l_current_data,l_coc_size-2,2);                         /* L_COC */
3343         l_current_data += 2;
3344
3345         opj_write_bytes(l_current_data,p_comp_no, l_comp_room);         /* Ccoc */
3346         l_current_data+=l_comp_room;
3347
3348         opj_write_bytes(l_current_data, l_tcp->tccps[p_comp_no].csty, 1);               /* Scoc */
3349         ++l_current_data;
3350
3351         l_remaining_size -= (5 + l_comp_room);
3352         j2k_write_SPCod_SPCoc(p_j2k,p_j2k->m_current_tile_number,0,l_current_data,&l_remaining_size,p_manager);
3353         * p_data_written = l_coc_size;
3354 }
3355
3356 /**
3357  * Gets the maximum size taken by a coc.
3358  *
3359  * @param       p_j2k   the jpeg2000 codec to use.
3360  */
3361 OPJ_UINT32 j2k_get_max_coc_size(opj_j2k_v2_t *p_j2k)
3362 {
3363         OPJ_UINT32 i,j;
3364         OPJ_UINT32 l_nb_comp;
3365         OPJ_UINT32 l_nb_tiles;
3366         OPJ_UINT32 l_max = 0;
3367
3368         /* preconditions */
3369
3370         l_nb_tiles = p_j2k->m_cp.tw * p_j2k->m_cp.th ;
3371         l_nb_comp = p_j2k->m_private_image->numcomps;
3372
3373         for (i=0;i<l_nb_tiles;++i) {
3374                 for (j=0;j<l_nb_comp;++j) {
3375                         l_max = uint_max(l_max,j2k_get_SPCod_SPCoc_size(p_j2k,i,j));
3376                 }
3377         }
3378
3379         return 6 + l_max;
3380 }
3381
3382 static void j2k_read_coc(opj_j2k_t *j2k) {
3383         int len, compno;
3384
3385         opj_cp_t *cp = j2k->cp;
3386         opj_tcp_t *tcp = j2k->state == J2K_STATE_TPH ? &cp->tcps[j2k->curtileno] : j2k->default_tcp;
3387         opj_image_t *image = j2k->image;
3388         opj_cio_t *cio = j2k->cio;
3389         
3390         len = cio_read(cio, 2);         /* Lcoc */
3391         compno = cio_read(cio, image->numcomps <= 256 ? 1 : 2); /* Ccoc */
3392         tcp->tccps[compno].csty = cio_read(cio, 1);     /* Scoc */
3393         j2k_read_cox(j2k, compno);
3394 }
3395
3396 /**
3397  * Reads a COC marker (Coding Style Component)
3398  * @param       p_header_data   the data contained in the COC box.
3399  * @param       p_j2k                   the jpeg2000 codec.
3400  * @param       p_header_size   the size of the data contained in the COC marker.
3401  * @param       p_manager               the user event manager.
3402 */
3403 opj_bool j2k_read_coc_v2 (
3404                                         opj_j2k_v2_t *p_j2k,
3405                                         OPJ_BYTE * p_header_data,
3406                                         OPJ_UINT32 p_header_size,
3407                                         struct opj_event_mgr * p_manager
3408                                         )
3409 {
3410         opj_cp_v2_t *l_cp = NULL;
3411         opj_tcp_v2_t *l_tcp = NULL;
3412         opj_image_t *l_image = NULL;
3413         OPJ_UINT32 l_comp_room;
3414         OPJ_UINT32 l_comp_no;
3415
3416         /* preconditions */
3417         assert(p_header_data != 00);
3418         assert(p_j2k != 00);
3419         assert(p_manager != 00);
3420
3421         l_cp = &(p_j2k->m_cp);
3422         l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH ) ? /*FIXME J2K_DEC_STATE_TPH*/
3423                                 &l_cp->tcps[p_j2k->m_current_tile_number] :
3424                                 p_j2k->m_specific_param.m_decoder.m_default_tcp;
3425         l_image = p_j2k->m_private_image;
3426
3427         l_comp_room = l_image->numcomps <= 256 ? 1 : 2;
3428
3429         /* make sure room is sufficient*/
3430         if (p_header_size < l_comp_room + 1) {
3431                 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading COC marker\n");
3432                 return OPJ_FALSE;
3433         }
3434         p_header_size -= l_comp_room + 1;
3435
3436         opj_read_bytes(p_header_data,&l_comp_no,l_comp_room);                   /* Ccoc */
3437         p_header_data += l_comp_room;
3438         if (l_comp_no >= l_image->numcomps) {
3439                 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading COC marker (bad number of components)\n");
3440                 return OPJ_FALSE;
3441         }
3442
3443         opj_read_bytes(p_header_data,&l_tcp->tccps[l_comp_no].csty,1);                  /* Scoc */
3444         ++p_header_data ;
3445
3446         if (! j2k_read_SPCod_SPCoc(p_j2k,l_comp_no,p_header_data,&p_header_size,p_manager)) {
3447                 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading COC marker\n");
3448                 return OPJ_FALSE;
3449         }
3450
3451         if (p_header_size != 0) {
3452                 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading COC marker\n");
3453                 return OPJ_FALSE;
3454         }
3455         return OPJ_TRUE;
3456 }
3457
3458 static void j2k_write_qcx(opj_j2k_t *j2k, int compno) {
3459         int bandno, numbands;
3460         int expn, mant;
3461         
3462         opj_cp_t *cp = j2k->cp;
3463         opj_tcp_t *tcp = &cp->tcps[j2k->curtileno];
3464         opj_tccp_t *tccp = &tcp->tccps[compno];
3465         opj_cio_t *cio = j2k->cio;
3466         
3467         cio_write(cio, tccp->qntsty + (tccp->numgbits << 5), 1);        /* Sqcx */
3468         numbands = tccp->qntsty == J2K_CCP_QNTSTY_SIQNT ? 1 : tccp->numresolutions * 3 - 2;
3469         
3470         for (bandno = 0; bandno < numbands; bandno++) {
3471                 expn = tccp->stepsizes[bandno].expn;
3472                 mant = tccp->stepsizes[bandno].mant;
3473                 
3474                 if (tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) {
3475                         cio_write(cio, expn << 3, 1);   /* SPqcx_i */
3476                 } else {
3477                         cio_write(cio, (expn << 11) + mant, 2); /* SPqcx_i */
3478                 }
3479         }
3480 }
3481
3482 static void j2k_read_qcx(opj_j2k_t *j2k, int compno, int len) {
3483         int tmp;
3484         int bandno, numbands;
3485
3486         opj_cp_t *cp = j2k->cp;
3487         opj_tcp_t *tcp = j2k->state == J2K_STATE_TPH ? &cp->tcps[j2k->curtileno] : j2k->default_tcp;
3488         opj_tccp_t *tccp = &tcp->tccps[compno];
3489         opj_cio_t *cio = j2k->cio;
3490
3491         tmp = cio_read(cio, 1);         /* Sqcx */
3492         tccp->qntsty = tmp & 0x1f;
3493         tccp->numgbits = tmp >> 5;
3494         numbands = (tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) ? 
3495                 1 : ((tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) ? len - 1 : (len - 1) / 2);
3496
3497 #ifdef USE_JPWL
3498         if (j2k->cp->correct) {
3499
3500                 /* if JPWL is on, we check whether there are too many subbands */
3501                 if ((numbands < 0) || (numbands >= J2K_MAXBANDS)) {
3502                         opj_event_msg(j2k->cinfo, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
3503                                 "JPWL: bad number of subbands in Sqcx (%d)\n",
3504                                 numbands);
3505                         if (!JPWL_ASSUME) {
3506                                 opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
3507                                 return;
3508                         }
3509                         /* we try to correct */
3510                         numbands = 1;
3511                         opj_event_msg(j2k->cinfo, EVT_WARNING, "- trying to adjust them\n"
3512                                 "- setting number of bands to %d => HYPOTHESIS!!!\n",
3513                                 numbands);
3514                 };
3515
3516         };
3517
3518 #else
3519         /* We check whether there are too many subbands */
3520         if ((numbands < 0) || (numbands >= J2K_MAXBANDS)) {
3521                 opj_event_msg(j2k->cinfo, EVT_WARNING ,
3522                                         "bad number of subbands in Sqcx (%d) regarding to J2K_MAXBANDS (%d) \n"
3523                                     "- limiting number of bands to J2K_MAXBANDS and try to move to the next markers\n", numbands, J2K_MAXBANDS);
3524         }
3525
3526 #endif /* USE_JPWL */
3527
3528         for (bandno = 0; bandno < numbands; bandno++) {
3529                 int expn, mant;
3530                 if (tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) {
3531                         expn = cio_read(cio, 1) >> 3;   /* SPqcx_i */
3532                         mant = 0;
3533                 } else {
3534                         tmp = cio_read(cio, 2); /* SPqcx_i */
3535                         expn = tmp >> 11;
3536                         mant = tmp & 0x7ff;
3537                 }
3538                 if (bandno < J2K_MAXBANDS){
3539                         tccp->stepsizes[bandno].expn = expn;
3540                         tccp->stepsizes[bandno].mant = mant;
3541                 }
3542         }
3543         
3544         /* Add Antonin : if scalar_derived -> compute other stepsizes */
3545         if (tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) {
3546                 for (bandno = 1; bandno < J2K_MAXBANDS; bandno++) {
3547                         tccp->stepsizes[bandno].expn = 
3548                                 ((tccp->stepsizes[0].expn) - ((bandno - 1) / 3) > 0) ? 
3549                                         (tccp->stepsizes[0].expn) - ((bandno - 1) / 3) : 0;
3550                         tccp->stepsizes[bandno].mant = tccp->stepsizes[0].mant;
3551                 }
3552         }
3553         /* ddA */
3554 }
3555
3556 static void j2k_write_qcd(opj_j2k_t *j2k) {
3557         int lenp, len;
3558
3559         opj_cio_t *cio = j2k->cio;
3560         
3561         cio_write(cio, J2K_MS_QCD, 2);  /* QCD */
3562         lenp = cio_tell(cio);
3563         cio_skip(cio, 2);
3564         j2k_write_qcx(j2k, 0);
3565         len = cio_tell(cio) - lenp;
3566         cio_seek(cio, lenp);
3567         cio_write(cio, len, 2);                 /* Lqcd */
3568         cio_seek(cio, lenp + len);
3569
3570         if(j2k->cstr_info)
3571           j2k_add_mhmarker(j2k->cstr_info, J2K_MS_QCD, lenp, len);
3572 }
3573
3574 /**
3575  * Writes the QCD marker (quantization default)
3576  *
3577  * @param       p_comp_number   the index of the component to output.
3578  * @param       p_stream                                the stream to write data to.
3579  * @param       p_j2k                           J2K codec.
3580  * @param       p_manager               the user event manager.
3581 */
3582 opj_bool j2k_write_qcd_v2(      opj_j2k_v2_t *p_j2k,
3583                                                         struct opj_stream_private *p_stream,
3584                                                         struct opj_event_mgr * p_manager )
3585 {
3586         opj_cp_v2_t *l_cp = 00;
3587         opj_tcp_v2_t *l_tcp = 00;
3588         OPJ_UINT32 l_qcd_size,l_remaining_size;
3589         OPJ_BYTE * l_current_data = 00;
3590
3591         /* preconditions */
3592         assert(p_j2k != 00);
3593         assert(p_manager != 00);
3594         assert(p_stream != 00);
3595
3596         l_cp = &(p_j2k->m_cp);
3597         l_tcp = &l_cp->tcps[p_j2k->m_current_tile_number];
3598         l_qcd_size = 4 + j2k_get_SQcd_SQcc_size(p_j2k,p_j2k->m_current_tile_number,0);
3599         l_remaining_size = l_qcd_size;
3600
3601         if (l_qcd_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
3602                 p_j2k->m_specific_param.m_encoder.m_header_tile_data
3603                         = (OPJ_BYTE*)opj_realloc(
3604                                 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
3605                                 l_qcd_size);
3606
3607                 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
3608                         return OPJ_FALSE;
3609                 }
3610
3611                 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_qcd_size;
3612         }
3613
3614         l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
3615
3616         opj_write_bytes(l_current_data,J2K_MS_QCD,2);           /* QCD */
3617         l_current_data += 2;
3618
3619         opj_write_bytes(l_current_data,l_qcd_size-2,2);         /* L_QCD */
3620         l_current_data += 2;
3621
3622         l_remaining_size -= 4;
3623
3624         if (! j2k_write_SQcd_SQcc(p_j2k,p_j2k->m_current_tile_number,0,l_current_data,&l_remaining_size,p_manager)) {
3625                 opj_event_msg_v2(p_manager, EVT_ERROR, "Error writting QCD marker\n");
3626                 return OPJ_FALSE;
3627         }
3628
3629         if (l_remaining_size != 0) {
3630                 opj_event_msg_v2(p_manager, EVT_ERROR, "Error writting QCD marker\n");
3631                 return OPJ_FALSE;
3632         }
3633
3634         if (opj_stream_write_data(p_stream, p_j2k->m_specific_param.m_encoder.m_header_tile_data,l_qcd_size,p_manager) != l_qcd_size) {
3635                 return OPJ_FALSE;
3636         }
3637
3638         return OPJ_TRUE;
3639 }
3640
3641
3642 static void j2k_read_qcd(opj_j2k_t *j2k) {
3643         int len, pos;
3644   OPJ_UINT32 i;
3645
3646         opj_cio_t *cio = j2k->cio;
3647         opj_image_t *image = j2k->image;
3648         
3649         len = cio_read(cio, 2);         /* Lqcd */
3650         pos = cio_tell(cio);
3651         for (i = 0; i < image->numcomps; i++) {
3652                 cio_seek(cio, pos);
3653                 j2k_read_qcx(j2k, i, len - 2);
3654         }
3655 }
3656
3657 /**
3658  * Reads a QCD marker (Quantization defaults)
3659  * @param       p_header_data   the data contained in the QCD box.
3660  * @param       p_j2k                   the jpeg2000 codec.
3661  * @param       p_header_size   the size of the data contained in the QCD marker.
3662  * @param       p_manager               the user event manager.
3663 */
3664 opj_bool j2k_read_qcd_v2 (
3665                                     opj_j2k_v2_t *p_j2k,
3666                                         OPJ_BYTE * p_header_data,
3667                                         OPJ_UINT32 p_header_size,
3668                                         struct opj_event_mgr * p_manager
3669                                         )
3670 {
3671         /* preconditions */
3672         assert(p_header_data != 00);
3673         assert(p_j2k != 00);
3674         assert(p_manager != 00);
3675
3676         if (! j2k_read_SQcd_SQcc(p_j2k,0,p_header_data,&p_header_size,p_manager)) {
3677                 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading QCD marker\n");
3678                 return OPJ_FALSE;
3679         }
3680
3681         if (p_header_size != 0) {
3682                 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading QCD marker\n");
3683                 return OPJ_FALSE;
3684         }
3685
3686         /* Apply the quantization parameters to other components of the current tile or the m_default_tcp */
3687         j2k_copy_tile_quantization_parameters(p_j2k);
3688
3689         return OPJ_TRUE;
3690 }
3691
3692 static void j2k_write_qcc(opj_j2k_t *j2k, int compno) {
3693         int lenp, len;
3694
3695         opj_cio_t *cio = j2k->cio;
3696         
3697         cio_write(cio, J2K_MS_QCC, 2);  /* QCC */
3698         lenp = cio_tell(cio);
3699         cio_skip(cio, 2);
3700         cio_write(cio, compno, j2k->image->numcomps <= 256 ? 1 : 2);    /* Cqcc */
3701         j2k_write_qcx(j2k, compno);
3702         len = cio_tell(cio) - lenp;
3703         cio_seek(cio, lenp);
3704         cio_write(cio, len, 2);                 /* Lqcc */
3705         cio_seek(cio, lenp + len);
3706 }
3707
3708 /**
3709  * Writes the QCC marker (quantization component)
3710  *
3711  * @param       p_comp_no       the index of the component to output.
3712  * @param       p_stream                                the stream to write data to.
3713  * @param       p_j2k                           J2K codec.
3714  * @param       p_manager               the user event manager.
3715 */
3716 opj_bool j2k_write_qcc_v2(      opj_j2k_v2_t *p_j2k,
3717                                                         OPJ_UINT32 p_comp_no,
3718                                                         struct opj_stream_private *p_stream,
3719                                                         struct opj_event_mgr * p_manager )
3720 {
3721         OPJ_UINT32 l_qcc_size,l_remaining_size;
3722
3723         /* preconditions */
3724         assert(p_j2k != 00);
3725         assert(p_manager != 00);
3726         assert(p_stream != 00);
3727
3728         l_qcc_size = 6 + j2k_get_SQcd_SQcc_size(p_j2k,p_j2k->m_current_tile_number,p_comp_no);
3729         l_remaining_size = l_qcc_size;
3730
3731         if (l_qcc_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
3732                 p_j2k->m_specific_param.m_encoder.m_header_tile_data
3733                         = (OPJ_BYTE*)opj_realloc(
3734                                 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
3735                                 l_qcc_size);
3736                 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
3737                         return OPJ_FALSE;
3738                 }
3739
3740                 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_qcc_size;
3741         }
3742
3743         j2k_write_qcc_in_memory(p_j2k,p_comp_no,p_j2k->m_specific_param.m_encoder.m_header_tile_data,&l_remaining_size,p_manager);
3744
3745         if (opj_stream_write_data(p_stream,p_j2k->m_specific_param.m_encoder.m_header_tile_data,l_qcc_size,p_manager) != l_qcc_size) {
3746                 return OPJ_FALSE;
3747         }
3748
3749         return OPJ_TRUE;
3750 }
3751
3752 /**
3753  * Writes the QCC marker (quantization component)
3754  *
3755  * @param       p_comp_no       the index of the component to output.
3756  * @param       p_stream                                the stream to write data to.
3757  * @param       p_j2k                           J2K codec.
3758  * @param       p_manager               the user event manager.
3759 */
3760 void j2k_write_qcc_in_memory(   opj_j2k_v2_t *p_j2k,
3761                                                                 OPJ_UINT32 p_comp_no,
3762                                                                 OPJ_BYTE * p_data,
3763                                                                 OPJ_UINT32 * p_data_written,
3764                                                                 struct opj_event_mgr * p_manager )
3765 {
3766         OPJ_UINT32 l_qcc_size,l_remaining_size;
3767         OPJ_BYTE * l_current_data = 00;
3768
3769         /* preconditions */
3770         assert(p_j2k != 00);
3771         assert(p_manager != 00);
3772
3773         l_qcc_size = 6 + j2k_get_SQcd_SQcc_size(p_j2k,p_j2k->m_current_tile_number,p_comp_no);
3774         l_remaining_size = l_qcc_size;
3775
3776         l_current_data = p_data;
3777
3778         opj_write_bytes(l_current_data,J2K_MS_QCC,2);           /* QCC */
3779         l_current_data += 2;
3780
3781         if (p_j2k->m_private_image->numcomps <= 256) {
3782                 --l_qcc_size;
3783
3784                 opj_write_bytes(l_current_data,l_qcc_size-2,2);         /* L_QCC */
3785                 l_current_data += 2;
3786
3787                 opj_write_bytes(l_current_data, p_comp_no, 1);  /* Cqcc */
3788                 ++l_current_data;
3789
3790                 /* in the case only one byte is sufficient the last byte allocated is useless -> still do -6 for available */
3791                 l_remaining_size -= 6;
3792         }
3793         else {
3794                 opj_write_bytes(l_current_data,l_qcc_size-2,2);         /* L_QCC */
3795                 l_current_data += 2;
3796
3797                 opj_write_bytes(l_current_data, p_comp_no, 2);  /* Cqcc */
3798                 l_current_data+=2;
3799
3800                 l_remaining_size -= 6;
3801         }
3802
3803         j2k_write_SQcd_SQcc(p_j2k,p_j2k->m_current_tile_number,p_comp_no,l_current_data,&l_remaining_size,p_manager);
3804
3805         *p_data_written = l_qcc_size;
3806 }
3807
3808 /**
3809  * Gets the maximum size taken by a qcc.
3810  */
3811 OPJ_UINT32 j2k_get_max_qcc_size (opj_j2k_v2_t *p_j2k)
3812 {
3813         return j2k_get_max_coc_size(p_j2k);
3814 }
3815
3816 static void j2k_read_qcc(opj_j2k_t *j2k) {
3817         int len, compno;
3818         int numcomp = j2k->image->numcomps;
3819         opj_cio_t *cio = j2k->cio;
3820
3821         len = cio_read(cio, 2); /* Lqcc */
3822         compno = cio_read(cio, numcomp <= 256 ? 1 : 2); /* Cqcc */
3823
3824 #ifdef USE_JPWL
3825         if (j2k->cp->correct) {
3826
3827                 static int backup_compno = 0;
3828
3829                 /* compno is negative or larger than the number of components!!! */
3830                 if ((compno < 0) || (compno >= numcomp)) {
3831                         opj_event_msg(j2k->cinfo, EVT_ERROR,
3832                                 "JPWL: bad component number in QCC (%d out of a maximum of %d)\n",
3833                                 compno, numcomp);
3834                         if (!JPWL_ASSUME) {
3835                                 opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
3836                                 return;
3837                         }
3838                         /* we try to correct */
3839                         compno = backup_compno % numcomp;
3840                         opj_event_msg(j2k->cinfo, EVT_WARNING, "- trying to adjust this\n"
3841                                 "- setting component number to %d\n",
3842                                 compno);
3843                 }
3844
3845                 /* keep your private count of tiles */
3846                 backup_compno++;
3847         };
3848 #endif /* USE_JPWL */
3849
3850         j2k_read_qcx(j2k, compno, len - 2 - (numcomp <= 256 ? 1 : 2));
3851 }
3852
3853 /**
3854  * Reads a QCC marker (Quantization component)
3855  * @param       p_header_data   the data contained in the QCC box.
3856  * @param       p_j2k                   the jpeg2000 codec.
3857  * @param       p_header_size   the size of the data contained in the QCC marker.
3858  * @param       p_manager               the user event manager.
3859 */
3860 opj_bool j2k_read_qcc_v2(       opj_j2k_v2_t *p_j2k,
3861                                                         OPJ_BYTE * p_header_data,
3862                                                         OPJ_UINT32 p_header_size,
3863                                                         struct opj_event_mgr * p_manager)
3864 {
3865         OPJ_UINT32 l_num_comp,l_comp_no;
3866
3867         /* preconditions */
3868         assert(p_header_data != 00);
3869         assert(p_j2k != 00);
3870         assert(p_manager != 00);
3871
3872         l_num_comp = p_j2k->m_private_image->numcomps;
3873
3874         if (l_num_comp <= 256) {
3875                 if (p_header_size < 1) {
3876                         opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading QCC marker\n");
3877                         return OPJ_FALSE;
3878                 }
3879                 opj_read_bytes(p_header_data,&l_comp_no,1);
3880                 ++p_header_data;
3881                 --p_header_size;
3882         }
3883         else {
3884                 if (p_header_size < 2) {
3885                         opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading QCC marker\n");
3886                         return OPJ_FALSE;
3887                 }
3888                 opj_read_bytes(p_header_data,&l_comp_no,2);
3889                 p_header_data+=2;
3890                 p_header_size-=2;
3891         }
3892
3893 #ifdef USE_JPWL
3894         if (p_j2k->m_cp.correct) {
3895
3896                 static OPJ_UINT32 backup_compno = 0;
3897
3898                 /* compno is negative or larger than the number of components!!! */
3899                 if (/*(l_comp_no < 0) ||*/ (l_comp_no >= l_num_comp)) {
3900                         opj_event_msg_v2(p_manager, EVT_ERROR,
3901                                 "JPWL: bad component number in QCC (%d out of a maximum of %d)\n",
3902                                 l_comp_no, l_num_comp);
3903                         if (!JPWL_ASSUME) {
3904                                 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
3905                                 return OPJ_FALSE;
3906                         }
3907                         /* we try to correct */
3908                         l_comp_no = backup_compno % l_num_comp;
3909                         opj_event_msg_v2(p_manager, EVT_WARNING, "- trying to adjust this\n"
3910                                 "- setting component number to %d\n",
3911                                 l_comp_no);
3912                 }
3913
3914                 /* keep your private count of tiles */
3915                 backup_compno++;
3916         };
3917 #endif /* USE_JPWL */
3918
3919         if (! j2k_read_SQcd_SQcc(p_j2k,l_comp_no,p_header_data,&p_header_size,p_manager)) {
3920                 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading QCC marker\n");
3921                 return OPJ_FALSE;
3922         }
3923
3924         if (p_header_size != 0) {
3925                 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading QCC marker\n");
3926                 return OPJ_FALSE;
3927         }
3928
3929         return OPJ_TRUE;
3930 }
3931
3932
3933 static void j2k_write_poc(opj_j2k_t *j2k) {
3934         int len, numpchgs, i;
3935
3936         int numcomps = j2k->image->numcomps;
3937         
3938         opj_cp_t *cp = j2k->cp;
3939         opj_tcp_t *tcp = &cp->tcps[j2k->curtileno];
3940         opj_tccp_t *tccp = &tcp->tccps[0];
3941         opj_cio_t *cio = j2k->cio;
3942
3943         numpchgs = 1 + tcp->numpocs;
3944         cio_write(cio, J2K_MS_POC, 2);  /* POC  */
3945         len = 2 + (5 + 2 * (numcomps <= 256 ? 1 : 2)) * numpchgs;
3946         cio_write(cio, len, 2);         /* Lpoc */
3947         for (i = 0; i < numpchgs; i++) {
3948                 opj_poc_t *poc = &tcp->pocs[i];
3949                 cio_write(cio, poc->resno0, 1); /* RSpoc_i */
3950                 cio_write(cio, poc->compno0, (numcomps <= 256 ? 1 : 2));        /* CSpoc_i */
3951                 cio_write(cio, poc->layno1, 2); /* LYEpoc_i */
3952                 poc->layno1 = int_min(poc->layno1, tcp->numlayers);
3953                 cio_write(cio, poc->resno1, 1); /* REpoc_i */
3954                 poc->resno1 = int_min(poc->resno1, tccp->numresolutions);
3955                 cio_write(cio, poc->compno1, (numcomps <= 256 ? 1 : 2));        /* CEpoc_i */
3956                 poc->compno1 = int_min(poc->compno1, numcomps);
3957                 cio_write(cio, poc->prg, 1);    /* Ppoc_i */
3958         }
3959 }
3960
3961 /**
3962  * Writes the POC marker (Progression Order Change)
3963  * 
3964  * @param       p_stream                                the stream to write data to.
3965  * @param       p_j2k                           J2K codec.
3966  * @param       p_manager               the user event manager.
3967 */
3968 opj_bool j2k_write_poc_v2(      opj_j2k_v2_t *p_j2k,
3969                                                         struct opj_stream_private *p_stream,
3970                                                         struct opj_event_mgr * p_manager )
3971 {
3972         OPJ_UINT32 l_nb_comp;
3973         OPJ_UINT32 l_nb_poc;
3974         OPJ_UINT32 l_poc_size;
3975         OPJ_UINT32 l_written_size = 0;
3976         opj_tcp_v2_t *l_tcp = 00;
3977         opj_tccp_t *l_tccp = 00;
3978         OPJ_UINT32 l_poc_room;
3979
3980         // preconditions
3981         assert(p_j2k != 00);
3982         assert(p_manager != 00);
3983         assert(p_stream != 00);
3984
3985         l_tcp = &p_j2k->m_cp.tcps[p_j2k->m_current_tile_number];
3986         l_tccp = &l_tcp->tccps[0];
3987         l_nb_comp = p_j2k->m_private_image->numcomps;
3988         l_nb_poc = 1 + l_tcp->numpocs;
3989         
3990         if (l_nb_comp <= 256) {
3991                 l_poc_room = 1;
3992         }
3993         else {
3994                 l_poc_room = 2;
3995         }
3996         l_poc_size = 4 + (5 + 2 * l_poc_room) * l_nb_poc;
3997         
3998         if (l_poc_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
3999                 p_j2k->m_specific_param.m_encoder.m_header_tile_data 
4000                         = (OPJ_BYTE*)opj_realloc(
4001                                 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
4002                                 l_poc_size);
4003                 
4004                 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
4005                         return OPJ_FALSE;
4006                 }
4007
4008                 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_poc_size;
4009         }
4010
4011         j2k_write_poc_in_memory(p_j2k,p_j2k->m_specific_param.m_encoder.m_header_tile_data,&l_written_size,p_manager);
4012
4013         if (opj_stream_write_data(p_stream,p_j2k->m_specific_param.m_encoder.m_header_tile_data,l_poc_size,p_manager) != l_poc_size) {
4014                 return OPJ_FALSE;
4015         }
4016
4017         return OPJ_TRUE;
4018 }
4019
4020
4021 /**
4022  * Writes the POC marker (Progression Order Change)
4023  *
4024  * @param       p_stream                                the stream to write data to.
4025  * @param       p_j2k                           J2K codec.
4026  * @param       p_manager               the user event manager.
4027 */
4028 void j2k_write_poc_in_memory(   opj_j2k_v2_t *p_j2k,
4029                                                                 OPJ_BYTE * p_data,
4030                                                                 OPJ_UINT32 * p_data_written,
4031                                                                 struct opj_event_mgr * p_manager )
4032 {
4033         OPJ_UINT32 i;
4034         OPJ_BYTE * l_current_data = 00;
4035         OPJ_UINT32 l_nb_comp;
4036         OPJ_UINT32 l_nb_poc;
4037         OPJ_UINT32 l_poc_size;
4038         opj_image_t *l_image = 00;
4039         opj_tcp_v2_t *l_tcp = 00;
4040         opj_tccp_t *l_tccp = 00;
4041         opj_poc_t *l_current_poc = 00;
4042         OPJ_UINT32 l_poc_room;
4043
4044         /* preconditions */
4045         assert(p_j2k != 00);
4046         assert(p_manager != 00);
4047
4048         l_tcp = &p_j2k->m_cp.tcps[p_j2k->m_current_tile_number];
4049         l_tccp = &l_tcp->tccps[0];
4050         l_image = p_j2k->m_private_image;
4051         l_nb_comp = l_image->numcomps;
4052         l_nb_poc = 1 + l_tcp->numpocs;
4053
4054         if (l_nb_comp <= 256) {
4055                 l_poc_room = 1;
4056         }
4057         else {
4058                 l_poc_room = 2;
4059         }
4060
4061         l_poc_size = 4 + (5 + 2 * l_poc_room) * l_nb_poc;
4062
4063         l_current_data = p_data;
4064
4065         opj_write_bytes(l_current_data,J2K_MS_POC,2);                                   /* POC  */
4066         l_current_data += 2;
4067
4068         opj_write_bytes(l_current_data,l_poc_size-2,2);                                 /* Lpoc */
4069         l_current_data += 2;
4070
4071         l_current_poc =  l_tcp->pocs;
4072         for (i = 0; i < l_nb_poc; ++i) {
4073                 opj_write_bytes(l_current_data,l_current_poc->resno0,1);                                /* RSpoc_i */
4074                 ++l_current_data;
4075
4076                 opj_write_bytes(l_current_data,l_current_poc->compno0,l_poc_room);              /* CSpoc_i */
4077                 l_current_data+=l_poc_room;
4078
4079                 opj_write_bytes(l_current_data,l_current_poc->layno1,2);                                /* LYEpoc_i */
4080                 l_current_data+=2;
4081
4082                 opj_write_bytes(l_current_data,l_current_poc->resno1,1);                                /* REpoc_i */
4083                 ++l_current_data;
4084
4085                 opj_write_bytes(l_current_data,l_current_poc->compno1,l_poc_room);              /* CEpoc_i */
4086                 l_current_data+=l_poc_room;
4087
4088                 opj_write_bytes(l_current_data,l_current_poc->prg,1);                                   /* Ppoc_i */
4089                 ++l_current_data;
4090
4091                 /* change the value of the max layer according to the actual number of layers in the file, components and resolutions*/
4092                 l_current_poc->layno1 = int_min(l_current_poc->layno1, l_tcp->numlayers);
4093                 l_current_poc->resno1 = int_min(l_current_poc->resno1, l_tccp->numresolutions);
4094                 l_current_poc->compno1 = int_min(l_current_poc->compno1, l_nb_comp);
4095
4096                 ++l_current_poc;
4097         }
4098
4099         *p_data_written = l_poc_size;
4100 }
4101
4102 /**
4103  * Gets the maximum size taken by the writing of a POC.
4104  */
4105 OPJ_UINT32 j2k_get_max_poc_size(opj_j2k_v2_t *p_j2k)
4106 {
4107         opj_tcp_v2_t * l_tcp = 00;
4108         OPJ_UINT32 l_nb_tiles = 0;
4109         OPJ_UINT32 l_max_poc = 0;
4110         OPJ_UINT32 i;
4111
4112         l_tcp = p_j2k->m_cp.tcps;
4113         l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw;
4114
4115         for (i=0;i<l_nb_tiles;++i) {
4116                 l_max_poc = uint_max(l_max_poc,l_tcp->numpocs);
4117                 ++l_tcp;
4118         }
4119
4120         ++l_max_poc;
4121
4122         return 4 + 9 * l_max_poc;
4123 }
4124
4125 /**
4126  * Gets the maximum size taken by the toc headers of all the tile parts of any given tile.
4127  */
4128 OPJ_UINT32 j2k_get_max_toc_size (opj_j2k_v2_t *p_j2k)
4129 {
4130         OPJ_UINT32 i;
4131         OPJ_UINT32 l_nb_tiles;
4132         OPJ_UINT32 l_max = 0;
4133         opj_tcp_v2_t * l_tcp = 00;
4134
4135         l_tcp = p_j2k->m_cp.tcps;
4136         l_nb_tiles = p_j2k->m_cp.tw * p_j2k->m_cp.th ;
4137
4138         for (i=0;i<l_nb_tiles;++i) {
4139                 l_max = uint_max(l_max,l_tcp->m_nb_tile_parts);
4140
4141                 ++l_tcp;
4142         }
4143
4144         return 12 * l_max;
4145 }
4146
4147
4148 /**
4149  * Gets the maximum size taken by the headers of the SOT.
4150  *
4151  * @param       p_j2k   the jpeg2000 codec to use.
4152  */
4153 OPJ_UINT32 j2k_get_specific_header_sizes(opj_j2k_v2_t *p_j2k)
4154 {
4155         OPJ_UINT32 l_nb_bytes = 0;
4156         OPJ_UINT32 l_nb_comps;
4157         OPJ_UINT32 l_coc_bytes,l_qcc_bytes;
4158
4159         l_nb_comps = p_j2k->m_private_image->numcomps - 1;
4160         l_nb_bytes += j2k_get_max_toc_size(p_j2k);
4161
4162         if (p_j2k->m_cp.m_specific_param.m_enc.m_cinema == 0) {
4163                 l_coc_bytes = j2k_get_max_coc_size(p_j2k);
4164                 l_nb_bytes += l_nb_comps * l_coc_bytes;
4165
4166                 l_qcc_bytes = j2k_get_max_qcc_size(p_j2k);
4167                 l_nb_bytes += l_nb_comps * l_qcc_bytes;
4168         }
4169
4170         l_nb_bytes += j2k_get_max_poc_size(p_j2k);
4171
4172         /*** DEVELOPER CORNER, Add room for your headers ***/
4173
4174         return l_nb_bytes;
4175 }
4176
4177 static void j2k_read_poc(opj_j2k_t *j2k) {
4178         int len, numpchgs, i, old_poc;
4179
4180         int numcomps = j2k->image->numcomps;
4181         
4182         opj_cp_t *cp = j2k->cp;
4183         opj_tcp_t *tcp = j2k->state == J2K_STATE_TPH ? &cp->tcps[j2k->curtileno] : j2k->default_tcp;
4184         opj_cio_t *cio = j2k->cio;
4185         
4186         old_poc = tcp->POC ? tcp->numpocs + 1 : 0;
4187         tcp->POC = 1;
4188         len = cio_read(cio, 2);         /* Lpoc */
4189         numpchgs = (len - 2) / (5 + 2 * (numcomps <= 256 ? 1 : 2));
4190         
4191         for (i = old_poc; i < numpchgs + old_poc; i++) {
4192                 opj_poc_t *poc;
4193                 poc = &tcp->pocs[i];
4194                 poc->resno0 = cio_read(cio, 1); /* RSpoc_i */
4195                 poc->compno0 = cio_read(cio, numcomps <= 256 ? 1 : 2);  /* CSpoc_i */
4196                 poc->layno1 = cio_read(cio, 2);    /* LYEpoc_i */
4197                 poc->resno1 = cio_read(cio, 1);    /* REpoc_i */
4198                 poc->compno1 = int_min(
4199                         cio_read(cio, numcomps <= 256 ? 1 : 2), (unsigned int) numcomps);       /* CEpoc_i */
4200                 poc->prg = (OPJ_PROG_ORDER)cio_read(cio, 1);    /* Ppoc_i */
4201         }
4202         
4203         tcp->numpocs = numpchgs + old_poc - 1;
4204 }
4205
4206 /**
4207  * Reads a POC marker (Progression Order Change)
4208  *
4209  * @param       p_header_data   the data contained in the POC box.
4210  * @param       p_j2k                   the jpeg2000 codec.
4211  * @param       p_header_size   the size of the data contained in the POC marker.
4212  * @param       p_manager               the user event manager.
4213 */
4214 opj_bool j2k_read_poc_v2 (
4215                                                 opj_j2k_v2_t *p_j2k,
4216                                                 OPJ_BYTE * p_header_data,
4217                                                 OPJ_UINT32 p_header_size,
4218                                                 struct opj_event_mgr * p_manager)
4219 {
4220         OPJ_UINT32 i, l_nb_comp, l_tmp;
4221         opj_image_t * l_image = 00;
4222         OPJ_UINT32 l_old_poc_nb, l_current_poc_nb, l_current_poc_remaining;
4223         OPJ_UINT32 l_chunk_size, l_comp_room;
4224
4225         opj_cp_v2_t *l_cp = 00;
4226         opj_tcp_v2_t *l_tcp = 00;
4227         opj_poc_t *l_current_poc = 00;
4228
4229         /* preconditions */
4230         assert(p_header_data != 00);
4231         assert(p_j2k != 00);
4232         assert(p_manager != 00);
4233
4234         l_image = p_j2k->m_private_image;
4235         l_nb_comp = l_image->numcomps;
4236         if (l_nb_comp <= 256) {
4237                 l_comp_room = 1;
4238         }
4239         else {
4240                 l_comp_room = 2;
4241         }
4242         l_chunk_size = 5 + 2 * l_comp_room;
4243         l_current_poc_nb = p_header_size / l_chunk_size;
4244         l_current_poc_remaining = p_header_size % l_chunk_size;
4245
4246         if ((l_current_poc_nb <= 0) || (l_current_poc_remaining != 0)) {
4247                 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading POC marker\n");
4248                 return OPJ_FALSE;
4249         }
4250
4251         l_cp = &(p_j2k->m_cp);
4252         l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ?
4253                                 &l_cp->tcps[p_j2k->m_current_tile_number] :
4254                                 p_j2k->m_specific_param.m_decoder.m_default_tcp;
4255         l_old_poc_nb = l_tcp->POC ? l_tcp->numpocs + 1 : 0;
4256         l_current_poc_nb += l_old_poc_nb;
4257
4258         assert(l_current_poc_nb < 32);
4259
4260         /* now poc is in use.*/
4261         l_tcp->POC = 1;
4262
4263         l_current_poc = &l_tcp->pocs[l_old_poc_nb];
4264         for     (i = l_old_poc_nb; i < l_current_poc_nb; ++i) {
4265                 opj_read_bytes(p_header_data,&(l_current_poc->resno0),1);                               /* RSpoc_i */
4266                 ++p_header_data;
4267                 opj_read_bytes(p_header_data,&(l_current_poc->compno0),l_comp_room);    /* CSpoc_i */
4268                 p_header_data+=l_comp_room;
4269                 opj_read_bytes(p_header_data,&(l_current_poc->layno1),2);                               /* LYEpoc_i */
4270                 p_header_data+=2;
4271                 opj_read_bytes(p_header_data,&(l_current_poc->resno1),1);                               /* REpoc_i */
4272                 ++p_header_data;
4273                 opj_read_bytes(p_header_data,&(l_current_poc->compno1),l_comp_room);    /* CEpoc_i */
4274                 p_header_data+=l_comp_room;
4275                 opj_read_bytes(p_header_data,&l_tmp,1);                                                                 /* Ppoc_i */
4276                 ++p_header_data;
4277                 l_current_poc->prg = (OPJ_PROG_ORDER) l_tmp;
4278                 /* make sure comp is in acceptable bounds */
4279                 l_current_poc->compno1 = uint_min(l_current_poc->compno1, l_nb_comp);
4280                 ++l_current_poc;
4281         }
4282
4283         l_tcp->numpocs = l_current_poc_nb - 1;
4284         return OPJ_TRUE;
4285 }
4286
4287 static void j2k_read_crg(opj_j2k_t *j2k) {
4288         int len, i, Xcrg_i, Ycrg_i;
4289         
4290         opj_cio_t *cio = j2k->cio;
4291         int numcomps = j2k->image->numcomps;
4292         
4293         len = cio_read(cio, 2);                 /* Lcrg */
4294         for (i = 0; i < numcomps; i++) {
4295                 Xcrg_i = cio_read(cio, 2);      /* Xcrg_i */
4296                 Ycrg_i = cio_read(cio, 2);      /* Ycrg_i */
4297         }
4298 }
4299
4300 /**
4301  * Reads a CRG marker (Component registration)
4302  *
4303  * @param       p_header_data   the data contained in the TLM box.
4304  * @param       p_j2k                   the jpeg2000 codec.
4305  * @param       p_header_size   the size of the data contained in the TLM marker.
4306  * @param       p_manager               the user event manager.
4307 */
4308 opj_bool j2k_read_crg_v2 (
4309                                                 opj_j2k_v2_t *p_j2k,
4310                                                 OPJ_BYTE * p_header_data,
4311                                                 OPJ_UINT32 p_header_size,
4312                                                 struct opj_event_mgr * p_manager
4313                                         )
4314 {
4315         OPJ_UINT32 l_nb_comp;
4316         /* preconditions */
4317         assert(p_header_data != 00);
4318         assert(p_j2k != 00);
4319         assert(p_manager != 00);
4320
4321         l_nb_comp = p_j2k->m_private_image->numcomps;
4322
4323         if (p_header_size != l_nb_comp *4) {
4324                 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading CRG marker\n");
4325                 return OPJ_FALSE;
4326         }
4327         /* Do not care of this at the moment since only local variables are set here */
4328         /*
4329         for
4330                 (i = 0; i < l_nb_comp; ++i)
4331         {
4332                 opj_read_bytes(p_header_data,&l_Xcrg_i,2);                              // Xcrg_i
4333                 p_header_data+=2;
4334                 opj_read_bytes(p_header_data,&l_Ycrg_i,2);                              // Xcrg_i
4335                 p_header_data+=2;
4336         }
4337         */
4338         return OPJ_TRUE;
4339 }
4340
4341 static void j2k_read_tlm(opj_j2k_t *j2k) {
4342         int len, Ztlm, Stlm, ST, SP, tile_tlm, i;
4343         long int Ttlm_i, Ptlm_i;
4344
4345         opj_cio_t *cio = j2k->cio;
4346         
4347         len = cio_read(cio, 2);         /* Ltlm */
4348         Ztlm = cio_read(cio, 1);        /* Ztlm */
4349         Stlm = cio_read(cio, 1);        /* Stlm */
4350         ST = ((Stlm >> 4) & 0x01) + ((Stlm >> 4) & 0x02);
4351         SP = (Stlm >> 6) & 0x01;
4352         tile_tlm = (len - 4) / ((SP + 1) * 2 + ST);
4353         for (i = 0; i < tile_tlm; i++) {
4354                 Ttlm_i = cio_read(cio, ST);     /* Ttlm_i */
4355                 Ptlm_i = cio_read(cio, SP ? 4 : 2);     /* Ptlm_i */
4356         }
4357 }
4358
4359 /**
4360  * Reads a TLM marker (Tile Length Marker)
4361  *
4362  * @param       p_header_data   the data contained in the TLM box.
4363  * @param       p_j2k                   the jpeg2000 codec.
4364  * @param       p_header_size   the size of the data contained in the TLM marker.
4365  * @param       p_manager               the user event manager.
4366 */
4367 opj_bool j2k_read_tlm_v2 (
4368                                                 opj_j2k_v2_t *p_j2k,
4369                                                 OPJ_BYTE * p_header_data,
4370                                                 OPJ_UINT32 p_header_size,
4371                                                 struct opj_event_mgr * p_manager
4372                                         )
4373 {
4374         OPJ_UINT32 l_Ztlm, l_Stlm, l_ST, l_SP, l_tot_num_tp, l_tot_num_tp_remaining, l_quotient, l_Ptlm_size;
4375         /* preconditions */
4376         assert(p_header_data != 00);
4377         assert(p_j2k != 00);
4378         assert(p_manager != 00);
4379
4380         if (p_header_size < 2) {
4381                 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading TLM marker\n");
4382                 return OPJ_FALSE;
4383         }
4384         p_header_size -= 2;
4385
4386         opj_read_bytes(p_header_data,&l_Ztlm,1);                                /* Ztlm */
4387         ++p_header_data;
4388         opj_read_bytes(p_header_data,&l_Stlm,1);                                /* Stlm */
4389         ++p_header_data;
4390
4391         l_ST = ((l_Stlm >> 4) & 0x3);
4392         l_SP = (l_Stlm >> 6) & 0x1;
4393
4394         l_Ptlm_size = (l_SP + 1) * 2;
4395         l_quotient = l_Ptlm_size + l_ST;
4396
4397         l_tot_num_tp = p_header_size / l_quotient;
4398         l_tot_num_tp_remaining = p_header_size % l_quotient;
4399
4400         if (l_tot_num_tp_remaining != 0) {
4401                 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading TLM marker\n");
4402                 return OPJ_FALSE;
4403         }
4404         /* FIXME Do not care of this at the moment since only local variables are set here */
4405         /*
4406         for
4407                 (i = 0; i < l_tot_num_tp; ++i)
4408         {
4409                 opj_read_bytes(p_header_data,&l_Ttlm_i,l_ST);                           // Ttlm_i
4410                 p_header_data += l_ST;
4411                 opj_read_bytes(p_header_data,&l_Ptlm_i,l_Ptlm_size);            // Ptlm_i
4412                 p_header_data += l_Ptlm_size;
4413         }*/
4414         return OPJ_TRUE;
4415 }
4416
4417 static void j2k_read_plm(opj_j2k_t *j2k) {
4418         int len, i, Zplm, Nplm, add, packet_len = 0;
4419         
4420         opj_cio_t *cio = j2k->cio;
4421
4422         len = cio_read(cio, 2);         /* Lplm */
4423         Zplm = cio_read(cio, 1);        /* Zplm */
4424         len -= 3;
4425         while (len > 0) {
4426                 Nplm = cio_read(cio, 4);                /* Nplm */
4427                 len -= 4;
4428                 for (i = Nplm; i > 0; i--) {
4429                         add = cio_read(cio, 1);
4430                         len--;
4431                         packet_len = (packet_len << 7) + add;   /* Iplm_ij */
4432                         if ((add & 0x80) == 0) {
4433                                 /* New packet */
4434                                 packet_len = 0;
4435                         }
4436                         if (len <= 0)
4437                                 break;
4438                 }
4439         }
4440 }
4441
4442 /**
4443  * Reads a PLM marker (Packet length, main header marker)
4444  *
4445  * @param       p_header_data   the data contained in the TLM box.
4446  * @param       p_j2k                   the jpeg2000 codec.
4447  * @param       p_header_size   the size of the data contained in the TLM marker.
4448  * @param       p_manager               the user event manager.
4449 */
4450 opj_bool j2k_read_plm_v2 (
4451                                                 opj_j2k_v2_t *p_j2k,
4452                                                 OPJ_BYTE * p_header_data,
4453                                                 OPJ_UINT32 p_header_size,
4454                                                 struct opj_event_mgr * p_manager
4455                                         )
4456 {
4457         /* preconditions */
4458         assert(p_header_data != 00);
4459         assert(p_j2k != 00);
4460         assert(p_manager != 00);
4461
4462         if (p_header_size < 1) {
4463                 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PLM marker\n");
4464                 return OPJ_FALSE;
4465         }
4466         /* Do not care of this at the moment since only local variables are set here */
4467         /*
4468         opj_read_bytes(p_header_data,&l_Zplm,1);                                        // Zplm
4469         ++p_header_data;
4470         --p_header_size;
4471
4472         while
4473                 (p_header_size > 0)
4474         {
4475                 opj_read_bytes(p_header_data,&l_Nplm,1);                                // Nplm
4476                 ++p_header_data;
4477                 p_header_size -= (1+l_Nplm);
4478                 if
4479                         (p_header_size < 0)
4480                 {
4481                         opj_event_msg(p_manager, EVT_ERROR, "Error reading PLM marker\n");
4482                         return false;
4483                 }
4484                 for
4485                         (i = 0; i < l_Nplm; ++i)
4486                 {
4487                         opj_read_bytes(p_header_data,&l_tmp,1);                         // Iplm_ij
4488                         ++p_header_data;
4489                         // take only the last seven bytes
4490                         l_packet_len |= (l_tmp & 0x7f);
4491                         if
4492                                 (l_tmp & 0x80)
4493                         {
4494                                 l_packet_len <<= 7;
4495                         }
4496                         else
4497                         {
4498                 // store packet length and proceed to next packet
4499                                 l_packet_len = 0;
4500                         }
4501                 }
4502                 if
4503                         (l_packet_len != 0)
4504                 {
4505                         opj_event_msg(p_manager, EVT_ERROR, "Error reading PLM marker\n");
4506                         return false;
4507                 }
4508         }
4509         */
4510         return OPJ_TRUE;
4511 }
4512
4513 static void j2k_read_plt(opj_j2k_t *j2k) {
4514         int len, i, Zplt, packet_len = 0, add;
4515         
4516         opj_cio_t *cio = j2k->cio;
4517         
4518         len = cio_read(cio, 2);         /* Lplt */
4519         Zplt = cio_read(cio, 1);        /* Zplt */
4520         for (i = len - 3; i > 0; i--) {
4521                 add = cio_read(cio, 1);
4522                 packet_len = (packet_len << 7) + add;   /* Iplt_i */
4523                 if ((add & 0x80) == 0) {
4524                         /* New packet */
4525                         packet_len = 0;
4526                 }
4527         }
4528 }
4529
4530 /**
4531  * Reads a PLT marker (Packet length, tile-part header)
4532  *
4533  * @param       p_header_data   the data contained in the PLT box.
4534  * @param       p_j2k                   the jpeg2000 codec.
4535  * @param       p_header_size   the size of the data contained in the PLT marker.
4536  * @param       p_manager               the user event manager.
4537 */
4538 opj_bool j2k_read_plt_v2 (
4539                                                 opj_j2k_v2_t *p_j2k,
4540                                                 OPJ_BYTE * p_header_data,
4541                                                 OPJ_UINT32 p_header_size,
4542                                                 struct opj_event_mgr * p_manager
4543                                         )
4544 {
4545         OPJ_UINT32 l_Zplt, l_tmp, l_packet_len = 0, i;
4546
4547         /* preconditions */
4548         assert(p_header_data != 00);
4549         assert(p_j2k != 00);
4550         assert(p_manager != 00);
4551
4552         if (p_header_size < 1) {
4553                 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PLT marker\n");
4554                 return OPJ_FALSE;
4555         }
4556
4557         opj_read_bytes(p_header_data,&l_Zplt,1);                /* Zplt */
4558         ++p_header_data;
4559         --p_header_size;
4560
4561         for (i = 0; i < p_header_size; ++i) {
4562                 opj_read_bytes(p_header_data,&l_tmp,1);         /* Iplt_ij */
4563                 ++p_header_data;
4564                 /* take only the last seven bytes */
4565                 l_packet_len |= (l_tmp & 0x7f);
4566                 if (l_tmp & 0x80) {
4567                         l_packet_len <<= 7;
4568                 }
4569                 else {
4570             /* store packet length and proceed to next packet */
4571                         l_packet_len = 0;
4572                 }
4573         }
4574
4575         if (l_packet_len != 0) {
4576                 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PLT marker\n");
4577                 return OPJ_FALSE;
4578         }
4579
4580         return OPJ_TRUE;
4581 }
4582
4583 static void j2k_read_ppm(opj_j2k_t *j2k) {
4584         int len, Z_ppm, i, j;
4585         int N_ppm;
4586
4587         opj_cp_t *cp = j2k->cp;
4588         opj_cio_t *cio = j2k->cio;
4589         
4590         len = cio_read(cio, 2);
4591         cp->ppm = 1;
4592         
4593         Z_ppm = cio_read(cio, 1);       /* Z_ppm */
4594         len -= 3;
4595         while (len > 0) {
4596                 if (cp->ppm_previous == 0) {
4597                         N_ppm = cio_read(cio, 4);       /* N_ppm */
4598                         len -= 4;
4599                 } else {
4600                         N_ppm = cp->ppm_previous;
4601                 }
4602                 j = cp->ppm_store;
4603                 if (Z_ppm == 0) {       /* First PPM marker */
4604                         cp->ppm_data = (unsigned char *) opj_malloc(N_ppm * sizeof(unsigned char));
4605                         cp->ppm_data_first = cp->ppm_data;
4606                         cp->ppm_len = N_ppm;
4607                 } else {                        /* NON-first PPM marker */
4608                         cp->ppm_data = (unsigned char *) opj_realloc(cp->ppm_data, (N_ppm +     cp->ppm_store) * sizeof(unsigned char));
4609
4610 #ifdef USE_JPWL
4611                         /* this memory allocation check could be done even in non-JPWL cases */
4612                         if (cp->correct) {
4613                                 if (!cp->ppm_data) {
4614                                         opj_event_msg(j2k->cinfo, EVT_ERROR,
4615                                                 "JPWL: failed memory allocation during PPM marker parsing (pos. %x)\n",
4616                                                 cio_tell(cio));
4617                                         if (!JPWL_ASSUME || JPWL_ASSUME) {
4618                                                 opj_free(cp->ppm_data);
4619                                                 opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
4620                                                 return;
4621                                         }
4622                                 }
4623                         }
4624 #endif
4625
4626                         cp->ppm_data_first = cp->ppm_data;
4627                         cp->ppm_len = N_ppm + cp->ppm_store;
4628                 }
4629                 for (i = N_ppm; i > 0; i--) {   /* Read packet header */
4630                         cp->ppm_data[j] = cio_read(cio, 1);
4631                         j++;
4632                         len--;
4633                         if (len == 0)
4634                                 break;                  /* Case of non-finished packet header in present marker but finished in next one */
4635                 }
4636                 cp->ppm_previous = i - 1;
4637                 cp->ppm_store = j;
4638         }
4639 }
4640 /**
4641  * Reads a PPM marker (Packed packet headers, main header)
4642  *
4643  * @param       p_header_data   the data contained in the POC box.
4644  * @param       p_j2k                   the jpeg2000 codec.
4645  * @param       p_header_size   the size of the data contained in the POC marker.
4646  * @param       p_manager               the user event manager.
4647 */
4648 #if 0
4649 opj_bool j2k_read_ppm_v2 (
4650                                                 opj_j2k_v2_t *p_j2k,
4651                                                 OPJ_BYTE * p_header_data,
4652                                                 OPJ_UINT32 p_header_size,
4653                                                 struct opj_event_mgr * p_manager
4654                                         )
4655 {
4656
4657         opj_cp_v2_t *l_cp = 00;
4658         OPJ_UINT32 l_remaining_data, l_Z_ppm, l_N_ppm;
4659
4660         /* preconditions */
4661         assert(p_header_data != 00);
4662         assert(p_j2k != 00);
4663         assert(p_manager != 00);
4664
4665         if (p_header_size < 1) {
4666                 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PPM marker\n");
4667                 return OPJ_FALSE;
4668         }
4669
4670         l_cp = &(p_j2k->m_cp);
4671         l_cp->ppm = 1;
4672
4673         opj_read_bytes(p_header_data,&l_Z_ppm,1);               /* Z_ppm */
4674         ++p_header_data;
4675         --p_header_size;
4676
4677         /* First PPM marker */
4678         if (l_Z_ppm == 0) {
4679                 if (p_header_size < 4) {
4680                         opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PPM marker\n");
4681                         return OPJ_FALSE;
4682                 }
4683
4684                 opj_read_bytes(p_header_data,&l_N_ppm,4);               /* N_ppm */
4685                 p_header_data+=4;
4686                 p_header_size-=4;
4687
4688                 /* First PPM marker: Initialization */
4689                 l_cp->ppm_len = l_N_ppm;
4690                 l_cp->ppm_data_size = 0;
4691
4692                 l_cp->ppm_buffer = (OPJ_BYTE *) opj_malloc(l_cp->ppm_len);
4693                 if (l_cp->ppm_buffer == 00) {
4694                         opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory reading ppm marker\n");
4695                         return OPJ_FALSE;
4696                 }
4697                 memset(l_cp->ppm_buffer,0,l_cp->ppm_len);
4698
4699                 l_cp->ppm_data = l_cp->ppm_buffer;
4700         }
4701
4702         while (1) {
4703                 if (l_cp->ppm_data_size == l_cp->ppm_len) {
4704                         if (p_header_size >= 4) {
4705                                 /* read a N_ppm */
4706                                 opj_read_bytes(p_header_data,&l_N_ppm,4);               /* N_ppm */
4707                                 p_header_data+=4;
4708                                 p_header_size-=4;
4709                                 l_cp->ppm_len += l_N_ppm ;
4710
4711                                 l_cp->ppm_buffer = (OPJ_BYTE *) opj_realloc(l_cp->ppm_buffer, l_cp->ppm_len);
4712                                 if (l_cp->ppm_buffer == 00) {
4713                                         opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory reading ppm marker\n");
4714                                         return OPJ_FALSE;
4715                                 }
4716                                 memset(l_cp->ppm_buffer+l_cp->ppm_data_size,0,l_N_ppm);
4717
4718                                 l_cp->ppm_data = l_cp->ppm_buffer;
4719                         }
4720                         else {
4721                                 return OPJ_FALSE;
4722                         }
4723                 }
4724
4725                 l_remaining_data = l_cp->ppm_len - l_cp->ppm_data_size;
4726
4727                 if (l_remaining_data <= p_header_size) {
4728                         /* we must store less information than available in the packet */
4729                         memcpy(l_cp->ppm_buffer + l_cp->ppm_data_size , p_header_data , l_remaining_data);
4730                         l_cp->ppm_data_size = l_cp->ppm_len;
4731                         p_header_size -= l_remaining_data;
4732                         p_header_data += l_remaining_data;
4733                 }
4734                 else {
4735                         memcpy(l_cp->ppm_buffer + l_cp->ppm_data_size , p_header_data , p_header_size);
4736                         l_cp->ppm_data_size += p_header_size;
4737                         p_header_data += p_header_size;
4738                         p_header_size = 0;
4739                         break;
4740                 }
4741         }
4742
4743         return OPJ_TRUE;
4744 }
4745 #endif
4746
4747
4748
4749 /**
4750  * Reads a PPM marker (Packed packet headers, main header)
4751  *
4752  * @param       p_header_data   the data contained in the POC box.
4753  * @param       p_j2k                   the jpeg2000 codec.
4754  * @param       p_header_size   the size of the data contained in the POC marker.
4755  * @param       p_manager               the user event manager.
4756 */
4757 opj_bool j2k_read_ppm_v3 (
4758                                                 opj_j2k_v2_t *p_j2k,
4759                                                 OPJ_BYTE * p_header_data,
4760                                                 OPJ_UINT32 p_header_size,
4761                                                 struct opj_event_mgr * p_manager
4762                                         )
4763 {
4764         opj_cp_v2_t *l_cp = 00;
4765         OPJ_UINT32 l_remaining_data, l_Z_ppm, l_N_ppm;
4766
4767         /* preconditions */
4768         assert(p_header_data != 00);
4769         assert(p_j2k != 00);
4770         assert(p_manager != 00);
4771
4772         /* Minimum size of PPM marker is equal to the size of Zppm element */
4773         if (p_header_size < 1) {
4774                 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PPM marker\n");
4775                 return OPJ_FALSE;
4776         }
4777
4778         l_cp = &(p_j2k->m_cp);
4779         l_cp->ppm = 1;
4780
4781         opj_read_bytes(p_header_data,&l_Z_ppm,1);               /* Z_ppm */
4782         ++p_header_data;
4783         --p_header_size;
4784
4785         /* First PPM marker */
4786         if (l_Z_ppm == 0) {
4787                 /* We need now at least the Nppm^0 element */
4788                 if (p_header_size < 4) {
4789                         opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PPM marker\n");
4790                         return OPJ_FALSE;
4791                 }
4792
4793                 opj_read_bytes(p_header_data,&l_N_ppm,4);               /* First N_ppm */
4794                 p_header_data+=4;
4795                 p_header_size-=4;
4796
4797                 /* First PPM marker: Initialization */
4798                 l_cp->ppm_len = l_N_ppm;
4799                 l_cp->ppm_data_read = 0;
4800
4801                 l_cp->ppm_data = (OPJ_BYTE *) opj_malloc(l_cp->ppm_len);
4802                 if (l_cp->ppm_data == 00) {
4803                         opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory reading ppm marker\n");
4804                         return OPJ_FALSE;
4805                 }
4806                 memset(l_cp->ppm_data,0,l_cp->ppm_len);
4807
4808                 l_cp->ppm_data_current = l_cp->ppm_data;
4809
4810                 /*l_cp->ppm_data = l_cp->ppm_buffer;*/
4811         }
4812         else {
4813                 if (p_header_size < 4) {
4814                         opj_event_msg_v2(p_manager, EVT_WARNING, "Empty PPM marker\n");
4815                         return OPJ_TRUE;
4816                 }
4817                 else {
4818                         /* Uncompleted Ippm series in the previous PPM marker?*/
4819                         if (l_cp->ppm_data_read < l_cp->ppm_len) {
4820                                 /* Get the place where add the remaining Ippm series*/
4821                                 l_cp->ppm_data_current = &(l_cp->ppm_data[l_cp->ppm_data_read]);
4822                                 l_N_ppm = l_cp->ppm_len - l_cp->ppm_data_read;
4823                         }
4824                         else {
4825                                 opj_read_bytes(p_header_data,&l_N_ppm,4);               /* First N_ppm */
4826                                 p_header_data+=4;
4827                                 p_header_size-=4;
4828
4829                                 /* Increase the size of ppm_data to add the new Ippm series*/
4830                                 l_cp->ppm_data = (OPJ_BYTE *) opj_realloc(l_cp->ppm_data, l_cp->ppm_len + l_N_ppm);
4831
4832                                 /* Keep the position of the place where concatenate the new series*/
4833                                 l_cp->ppm_data_current = &(l_cp->ppm_data[l_cp->ppm_len]);
4834                                 l_cp->ppm_len += l_N_ppm;
4835                         }
4836                 }
4837         }
4838
4839         l_remaining_data = p_header_size;
4840
4841         while (l_remaining_data >= l_N_ppm) {
4842                 /* read a complete Ippm series*/
4843                 memcpy(l_cp->ppm_data_current, p_header_data, l_N_ppm);
4844                 p_header_size -= l_N_ppm;
4845                 p_header_data += l_N_ppm;
4846
4847                 l_cp->ppm_data_read += l_N_ppm; /* Increase the number of data read*/
4848
4849                 if (p_header_size)
4850                 {
4851                         opj_read_bytes(p_header_data,&l_N_ppm,4);               /* N_ppm^i */
4852                         p_header_data+=4;
4853                         p_header_size-=4;
4854                 }
4855                 else {
4856                         l_remaining_data = p_header_size;
4857                         break;
4858                 }
4859
4860                 l_remaining_data = p_header_size;
4861
4862                 /* Next Ippm series is a complete series ?*/
4863                 if (l_remaining_data > l_N_ppm) {
4864                         /* Increase the size of ppm_data to add the new Ippm series*/
4865                         l_cp->ppm_data = (OPJ_BYTE *) opj_realloc(l_cp->ppm_data, l_cp->ppm_len + l_N_ppm);
4866
4867                         /* Keep the position of the place where concatenate the new series */
4868                         l_cp->ppm_data_current = &(l_cp->ppm_data[l_cp->ppm_len]);
4869                         l_cp->ppm_len += l_N_ppm;
4870                 }
4871
4872         }
4873
4874         /* Need to read an incomplete Ippm series*/
4875         if (l_remaining_data) {
4876                 l_cp->ppm_data = (OPJ_BYTE *) opj_realloc(l_cp->ppm_data, l_cp->ppm_len + l_N_ppm);
4877
4878                 /* Keep the position of the place where concatenate the new series*/
4879                 l_cp->ppm_data_current = &(l_cp->ppm_data[l_cp->ppm_len]);
4880                 l_cp->ppm_len += l_N_ppm;
4881
4882                 /* Read incomplete Ippm series*/
4883                 memcpy(l_cp->ppm_data_current, p_header_data, l_remaining_data);
4884                 p_header_size -= l_remaining_data;
4885                 p_header_data += l_remaining_data;
4886
4887                 l_cp->ppm_data_read += l_remaining_data; /* Increase the number of data read*/
4888         }
4889
4890 #ifdef CLEAN_MSD
4891
4892                 if (l_cp->ppm_data_size == l_cp->ppm_len) {
4893                         if (p_header_size >= 4) {
4894                                 /* read a N_ppm*/
4895                                 opj_read_bytes(p_header_data,&l_N_ppm,4);               /* N_ppm */
4896                                 p_header_data+=4;
4897                                 p_header_size-=4;
4898                                 l_cp->ppm_len += l_N_ppm ;
4899
4900                                 l_cp->ppm_buffer = (OPJ_BYTE *) opj_realloc(l_cp->ppm_buffer, l_cp->ppm_len);
4901                                 if (l_cp->ppm_buffer == 00) {
4902                                         opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory reading ppm marker\n");
4903                                         return OPJ_FALSE;
4904                                 }
4905                                 memset(l_cp->ppm_buffer+l_cp->ppm_data_size,0,l_N_ppm);
4906
4907                                 l_cp->ppm_data = l_cp->ppm_buffer;
4908                         }
4909                         else {
4910                                 return OPJ_FALSE;
4911                         }
4912                 }
4913
4914                 l_remaining_data = l_cp->ppm_len - l_cp->ppm_data_size;
4915
4916                 if (l_remaining_data <= p_header_size) {
4917                         /* we must store less information than available in the packet */
4918                         memcpy(l_cp->ppm_buffer + l_cp->ppm_data_size , p_header_data , l_remaining_data);
4919                         l_cp->ppm_data_size = l_cp->ppm_len;
4920                         p_header_size -= l_remaining_data;
4921                         p_header_data += l_remaining_data;
4922                 }
4923                 else {
4924                         memcpy(l_cp->ppm_buffer + l_cp->ppm_data_size , p_header_data , p_header_size);
4925                         l_cp->ppm_data_size += p_header_size;
4926                         p_header_data += p_header_size;
4927                         p_header_size = 0;
4928                         break;
4929                 }
4930         }
4931 #endif
4932         return OPJ_TRUE;
4933 }
4934
4935 static void j2k_read_ppt(opj_j2k_t *j2k) {
4936         int len, Z_ppt, i, j = 0;
4937
4938         opj_cp_t *cp = j2k->cp;
4939         opj_tcp_t *tcp = cp->tcps + j2k->curtileno;
4940         opj_cio_t *cio = j2k->cio;
4941
4942         len = cio_read(cio, 2);
4943         Z_ppt = cio_read(cio, 1);
4944         tcp->ppt = 1;
4945         if (Z_ppt == 0) {               /* First PPT marker */
4946                 tcp->ppt_data = (unsigned char *) opj_malloc((len - 3) * sizeof(unsigned char));
4947                 tcp->ppt_data_first = tcp->ppt_data;
4948                 tcp->ppt_store = 0;
4949                 tcp->ppt_len = len - 3;
4950         } else {                        /* NON-first PPT marker */
4951                 tcp->ppt_data = (unsigned char *) opj_realloc(tcp->ppt_data, (len - 3 + tcp->ppt_store) * sizeof(unsigned char));
4952                 tcp->ppt_data_first = tcp->ppt_data;
4953                 tcp->ppt_len = len - 3 + tcp->ppt_store;
4954         }
4955         j = tcp->ppt_store;
4956         for (i = len - 3; i > 0; i--) {
4957                 tcp->ppt_data[j] = cio_read(cio, 1);
4958                 j++;
4959         }
4960         tcp->ppt_store = j;
4961 }
4962
4963 /**
4964  * Reads a PPT marker (Packed packet headers, tile-part header)
4965  *
4966  * @param       p_header_data   the data contained in the PPT box.
4967  * @param       p_j2k                   the jpeg2000 codec.
4968  * @param       p_header_size   the size of the data contained in the PPT marker.
4969  * @param       p_manager               the user event manager.
4970 */
4971 opj_bool j2k_read_ppt_v2 (      opj_j2k_v2_t *p_j2k,
4972                                                         OPJ_BYTE * p_header_data,
4973                                                         OPJ_UINT32 p_header_size,
4974                                                         struct opj_event_mgr * p_manager )
4975 {
4976         opj_cp_v2_t *l_cp = 00;
4977         opj_tcp_v2_t *l_tcp = 00;
4978         OPJ_UINT32 l_Z_ppt;
4979
4980         /* preconditions */
4981         assert(p_header_data != 00);
4982         assert(p_j2k != 00);
4983         assert(p_manager != 00);
4984
4985         /* We need to have the Z_ppt element at minimum */
4986         if (p_header_size < 1) {
4987                 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PPT marker\n");
4988                 return OPJ_FALSE;
4989         }
4990
4991         l_cp = &(p_j2k->m_cp);
4992         if (l_cp->ppm){
4993                 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");
4994                 return OPJ_FALSE;
4995         }
4996
4997         l_tcp = &(l_cp->tcps[p_j2k->m_current_tile_number]);
4998         l_tcp->ppt = 1;
4999
5000         opj_read_bytes(p_header_data,&l_Z_ppt,1);               /* Z_ppt */
5001         ++p_header_data;
5002         --p_header_size;
5003
5004         /* Allocate buffer to read the packet header */
5005         if (l_Z_ppt == 0) {
5006                 /* First PPT marker */
5007                 l_tcp->ppt_data_size = 0;
5008                 l_tcp->ppt_len = p_header_size;
5009
5010                 l_tcp->ppt_buffer = (OPJ_BYTE *) opj_calloc(l_tcp->ppt_len, sizeof(OPJ_BYTE) );
5011                 if (l_tcp->ppt_buffer == 00) {
5012                         opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory reading PPT marker\n");
5013                         return OPJ_FALSE;
5014                 }
5015                 l_tcp->ppt_data = l_tcp->ppt_buffer;
5016
5017                 /* memset(l_tcp->ppt_buffer,0,l_tcp->ppt_len); */
5018         }
5019         else {
5020                 l_tcp->ppt_len += p_header_size;
5021
5022                 l_tcp->ppt_buffer = (OPJ_BYTE *) opj_realloc(l_tcp->ppt_buffer,l_tcp->ppt_len);
5023                 if (l_tcp->ppt_buffer == 00) {
5024                         opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory reading PPT marker\n");
5025                         return OPJ_FALSE;
5026                 }
5027                 l_tcp->ppt_data = l_tcp->ppt_buffer;
5028
5029                 memset(l_tcp->ppt_buffer+l_tcp->ppt_data_size,0,p_header_size);
5030         }
5031
5032         /* Read packet header from buffer */
5033         memcpy(l_tcp->ppt_buffer+l_tcp->ppt_data_size,p_header_data,p_header_size);
5034
5035         l_tcp->ppt_data_size += p_header_size;
5036
5037         return OPJ_TRUE;
5038 }
5039
5040 /**
5041  * Writes the TLM marker (Tile Length Marker)
5042  * 
5043  * @param       p_stream                                the stream to write data to.
5044  * @param       p_j2k                           J2K codec.
5045  * @param       p_manager               the user event manager.
5046 */
5047 opj_bool j2k_write_tlm_v2(      opj_j2k_v2_t *p_j2k,
5048                                                         struct opj_stream_private *p_stream,
5049                                                         struct opj_event_mgr * p_manager )
5050 {
5051         OPJ_BYTE * l_current_data = 00;
5052         OPJ_UINT32 l_tlm_size;
5053
5054         // preconditions
5055         assert(p_j2k != 00);
5056         assert(p_manager != 00);
5057         assert(p_stream != 00);
5058
5059         l_tlm_size = 6 + (5*p_j2k->m_specific_param.m_encoder.m_total_tile_parts);
5060         
5061         if (l_tlm_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
5062                 p_j2k->m_specific_param.m_encoder.m_header_tile_data 
5063                         = (OPJ_BYTE*)opj_realloc(
5064                                 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
5065                                 l_tlm_size);
5066                 
5067                 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
5068                         return OPJ_FALSE;
5069                 }
5070
5071                 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_tlm_size;
5072         }
5073
5074         l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
5075
5076         /* change the way data is written to avoid seeking if possible */
5077         // TODO
5078         p_j2k->m_specific_param.m_encoder.m_tlm_start = opj_stream_tell(p_stream);
5079         
5080         opj_write_bytes(l_current_data,J2K_MS_TLM,2);                                   /* TLM */
5081         l_current_data += 2;
5082         
5083         opj_write_bytes(l_current_data,l_tlm_size-2,2);                                 /* Lpoc */
5084         l_current_data += 2;
5085         
5086         opj_write_bytes(l_current_data,0,1);                                                    /* Ztlm=0*/
5087         ++l_current_data;
5088         
5089         opj_write_bytes(l_current_data,0x50,1);                                                 /* Stlm ST=1(8bits-255 tiles max),SP=1(Ptlm=32bits) */
5090         ++l_current_data;
5091         
5092         /* do nothing on the 5 * l_j2k->m_specific_param.m_encoder.m_total_tile_parts remaining data */
5093         if (opj_stream_write_data(p_stream,p_j2k->m_specific_param.m_encoder.m_header_tile_data,l_tlm_size,p_manager) != l_tlm_size) {
5094                 return OPJ_FALSE;
5095         }
5096
5097         return OPJ_TRUE;
5098 }
5099
5100 static void j2k_write_tlm(opj_j2k_t *j2k){
5101         int lenp;
5102         opj_cio_t *cio = j2k->cio;
5103         j2k->tlm_start = cio_tell(cio);
5104         cio_write(cio, J2K_MS_TLM, 2);/* TLM */
5105         lenp = 4 + (5*j2k->totnum_tp);
5106         cio_write(cio,lenp,2);                          /* Ltlm */
5107         cio_write(cio, 0,1);                                    /* Ztlm=0*/
5108         cio_write(cio,80,1);                                    /* Stlm ST=1(8bits-255 tiles max),SP=1(Ptlm=32bits) */
5109         cio_skip(cio,5*j2k->totnum_tp);
5110 }
5111
5112 static void j2k_write_sot(opj_j2k_t *j2k) {
5113         int lenp, len;
5114
5115         opj_cio_t *cio = j2k->cio;
5116
5117         j2k->sot_start = cio_tell(cio);
5118         cio_write(cio, J2K_MS_SOT, 2);          /* SOT */
5119         lenp = cio_tell(cio);
5120         cio_skip(cio, 2);                                       /* Lsot (further) */
5121         cio_write(cio, j2k->curtileno, 2);      /* Isot */
5122         cio_skip(cio, 4);                                       /* Psot (further in j2k_write_sod) */
5123         cio_write(cio, j2k->cur_tp_num , 1);    /* TPsot */
5124         cio_write(cio, j2k->cur_totnum_tp[j2k->curtileno], 1);          /* TNsot */
5125         len = cio_tell(cio) - lenp;
5126         cio_seek(cio, lenp);
5127         cio_write(cio, len, 2);                         /* Lsot */
5128         cio_seek(cio, lenp + len);
5129
5130         /* UniPG>> */
5131 #ifdef USE_JPWL
5132         /* update markers struct */
5133         j2k_add_marker(j2k->cstr_info, J2K_MS_SOT, j2k->sot_start, len + 2);
5134 #endif /* USE_JPWL */
5135         /* <<UniPG */
5136
5137         if( j2k->cstr_info && j2k->cur_tp_num==0){
5138           j2k_add_tlmarker( j2k->curtileno, j2k->cstr_info, J2K_MS_SOT, lenp, len);
5139         }
5140 }
5141
5142 /**
5143  * Writes the SOT marker (Start of tile-part)
5144  *
5145  * @param       p_stream                                the stream to write data to.
5146  * @param       p_j2k                           J2K codec.
5147  * @param       p_manager               the user event manager.
5148 */
5149 opj_bool j2k_write_sot_v2(      opj_j2k_v2_t *p_j2k,
5150                                                         OPJ_BYTE * p_data,
5151                                                         OPJ_UINT32 * p_data_written,
5152                                                         const struct opj_stream_private *p_stream,
5153                                                         struct opj_event_mgr * p_manager )
5154 {
5155         /* preconditions */
5156         assert(p_j2k != 00);
5157         assert(p_manager != 00);
5158         assert(p_stream != 00);
5159
5160         opj_write_bytes(p_data,J2K_MS_SOT,2);                                   /* SOT */
5161         p_data += 2;
5162
5163         opj_write_bytes(p_data,10,2);                                                   /* Lsot */
5164         p_data += 2;
5165
5166         opj_write_bytes(p_data, p_j2k->m_current_tile_number,2);                        /* Isot */
5167         p_data += 2;
5168
5169         /* Psot  */
5170         p_data += 4;
5171
5172         opj_write_bytes(p_data, p_j2k->m_specific_param.m_encoder.m_current_tile_part_number,1);                        /* TPsot */
5173         ++p_data;
5174
5175         opj_write_bytes(p_data, p_j2k->m_cp.tcps[p_j2k->m_current_tile_number].m_nb_tile_parts,1);                      /* TNsot */
5176         ++p_data;
5177
5178         /* UniPG>> */
5179 #ifdef USE_JPWL
5180         /* update markers struct */
5181 /*
5182         j2k_add_marker(p_j2k->cstr_info, J2K_MS_SOT, p_j2k->sot_start, len + 2);
5183 */
5184   assert( 0 && "TODO" );
5185 #endif /* USE_JPWL */
5186
5187         * p_data_written = 12;
5188
5189         return OPJ_TRUE;
5190 }
5191
5192
5193
5194
5195 /**
5196  * Reads a PPT marker (Packed packet headers, tile-part header)
5197  *
5198  * @param       p_header_data   the data contained in the PPT box.
5199  * @param       p_j2k                   the jpeg2000 codec.
5200  * @param       p_header_size   the size of the data contained in the PPT marker.
5201  * @param       p_manager               the user event manager.
5202 */
5203 opj_bool j2k_read_sot_v2 (
5204                                                 opj_j2k_v2_t *p_j2k,
5205                                                 OPJ_BYTE * p_header_data,
5206                                                 OPJ_UINT32 p_header_size,
5207                                                 struct opj_event_mgr * p_manager
5208                                         )
5209 {
5210
5211         opj_cp_v2_t *l_cp = 00;
5212         opj_tcp_v2_t *l_tcp = 00;
5213         OPJ_UINT32 l_tot_len, l_num_parts = 0;
5214         OPJ_UINT32 l_current_part;
5215         OPJ_UINT32 l_tile_x,l_tile_y;
5216
5217         /* preconditions */
5218         assert(p_header_data != 00);
5219         assert(p_j2k != 00);
5220         assert(p_manager != 00);
5221
5222         /* Size of this marker is fixed = 12 (we have already read marker and its size)*/
5223         if (p_header_size != 8) {
5224                 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading SOT marker\n");
5225                 return OPJ_FALSE;
5226         }
5227
5228         l_cp = &(p_j2k->m_cp);
5229         opj_read_bytes(p_header_data,&(p_j2k->m_current_tile_number),2);                /* Isot */
5230         p_header_data+=2;
5231
5232         l_tcp = &l_cp->tcps[p_j2k->m_current_tile_number];
5233         l_tile_x = p_j2k->m_current_tile_number % l_cp->tw;
5234         l_tile_y = p_j2k->m_current_tile_number / l_cp->tw;
5235
5236 #ifdef USE_JPWL
5237         if (l_cp->correct) {
5238
5239                 int tileno = p_j2k->m_current_tile_number;
5240                 static int backup_tileno = 0;
5241
5242                 /* tileno is negative or larger than the number of tiles!!! */
5243                 if ((tileno < 0) || (tileno > (l_cp->tw * l_cp->th))) {
5244                         opj_event_msg_v2(p_manager, EVT_ERROR,
5245                                 "JPWL: bad tile number (%d out of a maximum of %d)\n",
5246                                 tileno, (l_cp->tw * l_cp->th));
5247                         if (!JPWL_ASSUME) {
5248                                 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
5249                                 return OPJ_FALSE;
5250                         }
5251                         /* we try to correct */
5252                         tileno = backup_tileno;
5253                         opj_event_msg_v2(p_manager, EVT_WARNING, "- trying to adjust this\n"
5254                                 "- setting tile number to %d\n",
5255                                 tileno);
5256                 }
5257
5258                 /* keep your private count of tiles */
5259                 backup_tileno++;
5260         };
5261 #endif /* USE_JPWL */
5262
5263         /* look for the tile in the list of already processed tile (in parts). */
5264         /* Optimization possible here with a more complex data structure and with the removing of tiles */
5265         /* since the time taken by this function can only grow at the time */
5266
5267         opj_read_bytes(p_header_data,&l_tot_len,4);             /* Psot */
5268         p_header_data+=4;
5269
5270         /* PSot should be equal to zero or >=14 or <= 2^32-1 */
5271         if ((l_tot_len !=0 ) && (l_tot_len < 14) )
5272         {
5273                 opj_event_msg_v2(p_manager, EVT_ERROR, "Psot value (%d) is not correct regards to the JPEG2000 norm!\n", l_tot_len);
5274                 return OPJ_FALSE;
5275         }
5276
5277
5278 #ifdef USE_JPWL
5279         if (l_cp->correct) {
5280
5281                 /* totlen is negative or larger than the bytes left!!! */
5282                 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))) { */
5283                         opj_event_msg_v2(p_manager, EVT_ERROR,
5284                                 "JPWL: bad tile byte size (%d bytes against %d bytes left)\n",
5285                                 l_tot_len, p_header_size ); /* FIXME it seems correct; for info in V1 -> p_stream_numbytesleft(p_stream) + 8); */
5286                         if (!JPWL_ASSUME) {
5287                                 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
5288                                 return OPJ_FALSE;
5289                         }
5290                         /* we try to correct */
5291                         l_tot_len = 0;
5292                         opj_event_msg_v2(p_manager, EVT_WARNING, "- trying to adjust this\n"
5293                                 "- setting Psot to %d => assuming it is the last tile\n",
5294                                 l_tot_len);
5295                 }
5296         };
5297 #endif /* USE_JPWL */
5298
5299         /* Ref A.4.2: Psot could be equal zero if it is the last tile-part of the codestream.*/
5300         if (!l_tot_len) {
5301                 opj_event_msg_v2(p_manager, EVT_INFO, "Psot value of the current tile-part is equal to zero, "
5302                                 "we assuming it is the last tile-part of the codestream.\n");
5303                 p_j2k->m_specific_param.m_decoder.m_last_tile_part = 1;
5304         }
5305
5306         opj_read_bytes(p_header_data,&l_current_part ,1);       /* TPsot */
5307         ++p_header_data;
5308
5309         opj_read_bytes(p_header_data,&l_num_parts ,1);          /* TNsot */
5310         ++p_header_data;
5311
5312         if (l_num_parts != 0) { /* Number of tile-part header is provided by this tile-part header */
5313                 /* Useful to manage the case of textGBR.jp2 file because two values of TNSot are allowed: the correct numbers of
5314                  * tile-parts for that tile and zero (A.4.2 of 15444-1 : 2002). */
5315                 if (l_tcp->m_nb_tile_parts) {
5316                         if (l_current_part >= l_tcp->m_nb_tile_parts){
5317                                 opj_event_msg_v2(p_manager, EVT_ERROR, "In SOT marker, TPSot (%d) is not valid regards to the current "
5318                                                 "number of tile-part (%d), giving up\n", l_current_part, l_tcp->m_nb_tile_parts );
5319                                 p_j2k->m_specific_param.m_decoder.m_last_tile_part = 1;
5320                                 return OPJ_FALSE;
5321                         }
5322                 }
5323                 l_tcp->m_nb_tile_parts = l_num_parts;
5324         }
5325
5326         /* If know the number of tile part header we will check if we didn't read the last*/
5327         if (l_tcp->m_nb_tile_parts) {
5328                 if (l_tcp->m_nb_tile_parts == (l_current_part + 1)) {
5329                         p_j2k->m_specific_param.m_decoder.m_can_decode = 1; /* Process the last tile-part header*/
5330                 }
5331         }
5332
5333         if (!p_j2k->m_specific_param.m_decoder.m_last_tile_part){
5334                 /* Keep the size of data to skip after this marker */
5335                 p_j2k->m_specific_param.m_decoder.m_sot_length = l_tot_len - 12; /* SOT_marker_size = 12 */
5336         }
5337         else {
5338                 /* FIXME: need to be computed from the number of bytes remaining in the codestream */
5339                 p_j2k->m_specific_param.m_decoder.m_sot_length = 0;
5340         }
5341
5342         p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_TPH;
5343
5344         /* Check if the current tile is outside the area we want decode or not corresponding to the tile index*/
5345         if (p_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec == -1) {
5346                 p_j2k->m_specific_param.m_decoder.m_skip_data =
5347                                 (l_tile_x < p_j2k->m_specific_param.m_decoder.m_start_tile_x)
5348                         ||      (l_tile_x >= p_j2k->m_specific_param.m_decoder.m_end_tile_x)
5349                         ||  (l_tile_y < p_j2k->m_specific_param.m_decoder.m_start_tile_y)
5350                         ||      (l_tile_y >= p_j2k->m_specific_param.m_decoder.m_end_tile_y);
5351         }
5352   else {
5353       assert( p_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec >= 0 );
5354     p_j2k->m_specific_param.m_decoder.m_skip_data =
5355       (p_j2k->m_current_tile_number != (OPJ_UINT32)p_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec);
5356     }
5357
5358         /* Index */
5359         if (p_j2k->cstr_index)
5360         {
5361     assert(p_j2k->cstr_index->tile_index != 00);
5362                 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tileno = p_j2k->m_current_tile_number;
5363                 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_tpsno = l_current_part;
5364
5365                 if (l_num_parts != 0){
5366                         p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].nb_tps = l_num_parts;
5367                         p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps = l_num_parts;
5368
5369
5370                         if (!p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index)
5371                                 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index =
5372                                         (opj_tp_index_t*)opj_calloc(l_num_parts, sizeof(opj_tp_index_t));
5373                         else
5374                                 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index =
5375                                         (opj_tp_index_t*)opj_realloc(
5376                                                         p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index,
5377                                                         l_num_parts* sizeof(opj_tp_index_t));
5378                 }
5379                 else{
5380                         /*if (!p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index)*/ {
5381
5382                                 if (!p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index) {
5383                                         p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps = 10;
5384                                         p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index =
5385                                                 (opj_tp_index_t*)opj_calloc( p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps,
5386                                                                                                          sizeof(opj_tp_index_t));
5387                                 }
5388
5389                                 if ( l_current_part >= p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps ){
5390                                         p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps += 10;
5391                                         p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index =
5392                                                 (opj_tp_index_t*)opj_realloc( p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index,
5393                                                                                                           p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps
5394                                                                                                           * sizeof(opj_tp_index_t));
5395                                 }
5396                         }
5397
5398                 }
5399
5400         }
5401
5402
5403         /* 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 */
5404         /* if (p_j2k->cstr_info) {
5405                 if (l_tcp->first) {
5406                         if (tileno == 0) {
5407                                 p_j2k->cstr_info->main_head_end = p_stream_tell(p_stream) - 13;
5408                         }
5409
5410                         p_j2k->cstr_info->tile[tileno].tileno = tileno;
5411                         p_j2k->cstr_info->tile[tileno].start_pos = p_stream_tell(p_stream) - 12;
5412                         p_j2k->cstr_info->tile[tileno].end_pos = p_j2k->cstr_info->tile[tileno].start_pos + totlen - 1;
5413                         p_j2k->cstr_info->tile[tileno].num_tps = numparts;
5414
5415                         if (numparts) {
5416                                 p_j2k->cstr_info->tile[tileno].tp = (opj_tp_info_t *) opj_malloc(numparts * sizeof(opj_tp_info_t));
5417                         }
5418                         else {
5419                                 p_j2k->cstr_info->tile[tileno].tp = (opj_tp_info_t *) opj_malloc(10 * sizeof(opj_tp_info_t)); // Fixme (10)
5420                         }
5421                 }
5422                 else {
5423                         p_j2k->cstr_info->tile[tileno].end_pos += totlen;
5424                 }
5425
5426                 p_j2k->cstr_info->tile[tileno].tp[partno].tp_start_pos = p_stream_tell(p_stream) - 12;
5427                 p_j2k->cstr_info->tile[tileno].tp[partno].tp_end_pos =
5428                 p_j2k->cstr_info->tile[tileno].tp[partno].tp_start_pos + totlen - 1;
5429         }*/
5430         return OPJ_TRUE;
5431 }
5432
5433 static void j2k_write_sod(opj_j2k_t *j2k, void *tile_coder) {
5434         int l, layno;
5435         int totlen;
5436         opj_tcp_t *tcp = NULL;
5437         opj_codestream_info_t *cstr_info = NULL;
5438         
5439         opj_tcd_t *tcd = (opj_tcd_t*)tile_coder;        /* cast is needed because of conflicts in header inclusions */
5440         opj_cp_t *cp = j2k->cp;
5441         opj_cio_t *cio = j2k->cio;
5442
5443         tcd->tp_num = j2k->tp_num ;
5444         tcd->cur_tp_num = j2k->cur_tp_num;
5445         
5446         cio_write(cio, J2K_MS_SOD, 2);
5447
5448         if( j2k->cstr_info && j2k->cur_tp_num==0){
5449           j2k_add_tlmarker( j2k->curtileno, j2k->cstr_info, J2K_MS_SOD, cio_tell(cio), 0);
5450         }
5451
5452         if (j2k->curtileno == 0) {
5453                 j2k->sod_start = cio_tell(cio) + j2k->pos_correction;
5454         }
5455
5456         /* INDEX >> */
5457         cstr_info = j2k->cstr_info;
5458         if (cstr_info) {
5459                 if (!j2k->cur_tp_num ) {
5460                         cstr_info->tile[j2k->curtileno].end_header = cio_tell(cio) + j2k->pos_correction - 1;
5461                         j2k->cstr_info->tile[j2k->curtileno].tileno = j2k->curtileno;
5462                 }
5463                 else{
5464                         if(cstr_info->tile[j2k->curtileno].packet[cstr_info->packno - 1].end_pos < cio_tell(cio))
5465                                 cstr_info->tile[j2k->curtileno].packet[cstr_info->packno].start_pos = cio_tell(cio);
5466                 }
5467                 /* UniPG>> */
5468 #ifdef USE_JPWL
5469                 /* update markers struct */
5470                 j2k_add_marker(j2k->cstr_info, J2K_MS_SOD, j2k->sod_start, 2);
5471 #endif /* USE_JPWL */
5472                 /* <<UniPG */
5473         }
5474         /* << INDEX */
5475         
5476         tcp = &cp->tcps[j2k->curtileno];
5477         for (layno = 0; layno < tcp->numlayers; layno++) {
5478                 if (tcp->rates[layno]>(j2k->sod_start / (cp->th * cp->tw))) {
5479                         tcp->rates[layno]-=(j2k->sod_start / (cp->th * cp->tw));
5480                 } else if (tcp->rates[layno]) {
5481                         tcp->rates[layno]=1;
5482                 }
5483         }
5484         if(j2k->cur_tp_num == 0){
5485                 tcd->tcd_image->tiles->packno = 0;
5486                 if(cstr_info)
5487                         cstr_info->packno = 0;
5488         }
5489         
5490         l = tcd_encode_tile(tcd, j2k->curtileno, cio_getbp(cio), cio_numbytesleft(cio) - 2, cstr_info);
5491         
5492         /* Writing Psot in SOT marker */
5493         totlen = cio_tell(cio) + l - j2k->sot_start;
5494         cio_seek(cio, j2k->sot_start + 6);
5495         cio_write(cio, totlen, 4);
5496         cio_seek(cio, j2k->sot_start + totlen);
5497         /* Writing Ttlm and Ptlm in TLM marker */
5498         if(cp->cinema){
5499                 cio_seek(cio, j2k->tlm_start + 6 + (5*j2k->cur_tp_num));
5500                 cio_write(cio, j2k->curtileno, 1);
5501                 cio_write(cio, totlen, 4);
5502         }
5503         cio_seek(cio, j2k->sot_start + totlen);
5504 }
5505
5506 /**
5507  * Writes the SOD marker (Start of data)
5508  *
5509  * @param       p_stream                                the stream to write data to.
5510  * @param       p_j2k                           J2K codec.
5511  * @param       p_manager               the user event manager.
5512 */
5513 opj_bool j2k_write_sod_v2(      opj_j2k_v2_t *p_j2k,
5514                                                         struct opj_tcd_v2 * p_tile_coder,
5515                                                         OPJ_BYTE * p_data,
5516                                                         OPJ_UINT32 * p_data_written,
5517                                                         OPJ_UINT32 p_total_data_size,
5518                                                         const struct opj_stream_private *p_stream,
5519                                                         struct opj_event_mgr * p_manager )
5520 {
5521         opj_tcp_v2_t *l_tcp = 00;
5522         opj_codestream_info_t *l_cstr_info = 00;
5523         opj_cp_v2_t *l_cp = 00;
5524
5525         OPJ_UINT32 l_size_tile;
5526         OPJ_UINT32 l_remaining_data;
5527
5528         /* preconditions */
5529         assert(p_j2k != 00);
5530         assert(p_manager != 00);
5531         assert(p_stream != 00);
5532
5533         opj_write_bytes(p_data,J2K_MS_SOD,2);                                   /* SOD */
5534         p_data += 2;
5535
5536         /* make room for the EOF marker */
5537         l_remaining_data =  p_total_data_size - 4;
5538
5539         l_cp = &(p_j2k->m_cp);
5540         l_tcp = &l_cp->tcps[p_j2k->m_current_tile_number];
5541
5542
5543         /* update tile coder */
5544         p_tile_coder->tp_num = p_j2k->m_specific_param.m_encoder.m_current_poc_tile_part_number ;
5545         p_tile_coder->cur_tp_num = p_j2k->m_specific_param.m_encoder.m_current_tile_part_number;
5546
5547         l_size_tile = l_cp->th * l_cp->tw;
5548
5549         /* INDEX >> */
5550         /* TODO mergeV2: check this part which use cstr_info */
5551         /*l_cstr_info = p_j2k->cstr_info;
5552         if (l_cstr_info) {
5553                 if (!p_j2k->m_specific_param.m_encoder.m_current_tile_part_number ) {
5554                         //TODO cstr_info->tile[p_j2k->m_current_tile_number].end_header = p_stream_tell(p_stream) + p_j2k->pos_correction - 1;
5555                         l_cstr_info->tile[p_j2k->m_current_tile_number].tileno = p_j2k->m_current_tile_number;
5556                 }
5557                 else {*/
5558                         /*
5559                         TODO
5560                         if
5561                                 (cstr_info->tile[p_j2k->m_current_tile_number].packet[cstr_info->packno - 1].end_pos < p_stream_tell(p_stream))
5562                         {
5563                                 cstr_info->tile[p_j2k->m_current_tile_number].packet[cstr_info->packno].start_pos = p_stream_tell(p_stream);
5564                         }*/
5565                 /*}*/
5566                 /* UniPG>> */
5567 #ifdef USE_JPWL
5568                 /* update markers struct */
5569                 /*j2k_add_marker(p_j2k->cstr_info, J2K_MS_SOD, p_j2k->sod_start, 2);
5570 */
5571   assert( 0 && "TODO" );
5572 #endif /* USE_JPWL */
5573                 /* <<UniPG */
5574         /*}*/
5575         /* << INDEX */
5576
5577         if (p_j2k->m_specific_param.m_encoder.m_current_tile_part_number == 0) {
5578                 p_tile_coder->tcd_image->tiles->packno = 0;
5579                 if (l_cstr_info) {
5580                         l_cstr_info->packno = 0;
5581                 }
5582         }
5583
5584         *p_data_written = 0;
5585
5586         if (! tcd_encode_tile_v2(p_tile_coder, p_j2k->m_current_tile_number, p_data, p_data_written, l_remaining_data , l_cstr_info)) {
5587                 opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot encode tile\n");
5588                 return OPJ_FALSE;
5589         }
5590
5591         *p_data_written += 2;
5592
5593         return OPJ_TRUE;
5594 }
5595
5596
5597 static void j2k_read_sod(opj_j2k_t *j2k) {
5598         int len, truncate = 0, i;
5599         unsigned char *data = NULL, *data_ptr = NULL;
5600
5601         opj_cio_t *cio = j2k->cio;
5602         int curtileno = j2k->curtileno;
5603
5604         /* Index */
5605         if (j2k->cstr_info) {
5606                 j2k->cstr_info->tile[j2k->curtileno].tp[j2k->cur_tp_num].tp_end_header =
5607                         cio_tell(cio) + j2k->pos_correction - 1;
5608                 if (j2k->cur_tp_num == 0)
5609                         j2k->cstr_info->tile[j2k->curtileno].end_header = cio_tell(cio) + j2k->pos_correction - 1;
5610                 j2k->cstr_info->packno = 0;
5611         }
5612         
5613         len = int_min(j2k->eot - cio_getbp(cio), cio_numbytesleft(cio) + 1);
5614
5615         if (len == cio_numbytesleft(cio) + 1) {
5616                 truncate = 1;           /* Case of a truncate codestream */
5617         }       
5618
5619         data = j2k->tile_data[curtileno];
5620         data = (unsigned char*) opj_realloc(data, (j2k->tile_len[curtileno] + len) * sizeof(unsigned char));
5621
5622         data_ptr = data + j2k->tile_len[curtileno];
5623         for (i = 0; i < len; i++) {
5624                 data_ptr[i] = cio_read(cio, 1);
5625         }
5626
5627         j2k->tile_len[curtileno] += len;
5628         j2k->tile_data[curtileno] = data;
5629         
5630         if (!truncate) {
5631                 j2k->state = J2K_STATE_TPHSOT;
5632         } else {
5633                 j2k->state = J2K_STATE_NEOC;    /* RAJOUTE !! */
5634         }
5635         j2k->cur_tp_num++;
5636 }
5637
5638 /**
5639  * Reads a SOD marker (Start Of Data)
5640  *
5641  * @param       p_header_data   the data contained in the SOD box.
5642  * @param       p_j2k                   the jpeg2000 codec.
5643  * @param       p_header_size   the size of the data contained in the SOD marker.
5644  * @param       p_manager               the user event manager.
5645 */
5646 opj_bool j2k_read_sod_v2 (
5647                                                 opj_j2k_v2_t *p_j2k,
5648                                                 struct opj_stream_private *p_stream,
5649                                                 struct opj_event_mgr * p_manager
5650                                         )
5651 {
5652         OPJ_UINT32 l_current_read_size;
5653         opj_codestream_index_t * l_cstr_index = 00;
5654         OPJ_BYTE ** l_current_data = 00;
5655         opj_tcp_v2_t * l_tcp = 00;
5656         OPJ_UINT32 * l_tile_len = 00;
5657
5658         /* preconditions */
5659         assert(p_j2k != 00);
5660         assert(p_manager != 00);
5661         assert(p_stream != 00);
5662
5663         l_tcp = &(p_j2k->m_cp.tcps[p_j2k->m_current_tile_number]);
5664
5665         if (p_j2k->m_specific_param.m_decoder.m_last_tile_part) {
5666                 /* opj_stream_get_number_byte_left returns OPJ_OFF_T
5667                 // but we are in the last tile part,
5668                 // so its result will fit on OPJ_UINT32 unless we find
5669                 // a file with a single tile part of more than 4 GB...*/
5670                 p_j2k->m_specific_param.m_decoder.m_sot_length = (OPJ_UINT32)(opj_stream_get_number_byte_left(p_stream) - 2);
5671         }
5672         else
5673                 p_j2k->m_specific_param.m_decoder.m_sot_length -= 2;
5674
5675         l_current_data = &(l_tcp->m_data);
5676         l_tile_len = &l_tcp->m_data_size;
5677
5678         if (! *l_current_data) {
5679                 *l_current_data = (OPJ_BYTE*) opj_malloc(p_j2k->m_specific_param.m_decoder.m_sot_length);
5680         }
5681         else {
5682                 *l_current_data = (OPJ_BYTE*) opj_realloc(*l_current_data, *l_tile_len + p_j2k->m_specific_param.m_decoder.m_sot_length);
5683         }
5684
5685         if (*l_current_data == 00) {
5686                 opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot decode tile\n");
5687                 return OPJ_FALSE;
5688         }
5689
5690
5691         /* Index */
5692         l_cstr_index = p_j2k->cstr_index;
5693         if (l_cstr_index) {
5694                 OPJ_OFF_T l_current_pos = opj_stream_tell(p_stream) - 2;
5695
5696                 OPJ_UINT32 l_current_tile_part = l_cstr_index->tile_index[p_j2k->m_current_tile_number].current_tpsno;
5697                 l_cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index[l_current_tile_part].end_header =
5698                                 l_current_pos;
5699                 l_cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index[l_current_tile_part].end_pos =
5700                                 l_current_pos + p_j2k->m_specific_param.m_decoder.m_sot_length + 2;
5701
5702                 j2k_add_tlmarker_v2(p_j2k->m_current_tile_number,
5703                                                         l_cstr_index,
5704                                                         J2K_MS_SOD,
5705                                                         l_current_pos,
5706                                                         p_j2k->m_specific_param.m_decoder.m_sot_length + 2);
5707
5708                 /*l_cstr_index->packno = 0;*/
5709         }
5710
5711         l_current_read_size = opj_stream_read_data(     p_stream,
5712                                                                                                 *l_current_data + *l_tile_len,
5713                                                                                                 p_j2k->m_specific_param.m_decoder.m_sot_length,
5714                                                                                                 p_manager);
5715
5716         if (l_current_read_size != p_j2k->m_specific_param.m_decoder.m_sot_length) {
5717                 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_NEOC;
5718         }
5719         else {
5720                 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_TPHSOT;
5721         }
5722
5723         *l_tile_len +=  l_current_read_size;
5724
5725         return OPJ_TRUE;
5726 }
5727
5728
5729 static void j2k_write_rgn(opj_j2k_t *j2k, int compno, int tileno) {
5730         opj_cp_t *cp = j2k->cp;
5731         opj_tcp_t *tcp = &cp->tcps[tileno];
5732         opj_cio_t *cio = j2k->cio;
5733         int numcomps = j2k->image->numcomps;
5734         
5735         cio_write(cio, J2K_MS_RGN, 2);                                          /* RGN  */
5736         cio_write(cio, numcomps <= 256 ? 5 : 6, 2);                     /* Lrgn */
5737         cio_write(cio, compno, numcomps <= 256 ? 1 : 2);        /* Crgn */
5738         cio_write(cio, 0, 1);                                                           /* Srgn */
5739         cio_write(cio, tcp->tccps[compno].roishift, 1);         /* SPrgn */
5740 }
5741
5742 /**
5743  * Writes the RGN marker (Region Of Interest)
5744  *
5745  * @param       p_tile_no               the tile to output
5746  * @param       p_comp_no               the component to output
5747  * @param       p_stream                                the stream to write data to.
5748  * @param       p_j2k                           J2K codec.
5749  * @param       p_manager               the user event manager.
5750 */
5751 opj_bool j2k_write_rgn_v2(      opj_j2k_v2_t *p_j2k,
5752                                                         OPJ_UINT32 p_tile_no,
5753                                                         OPJ_UINT32 p_comp_no,
5754                                                         struct opj_stream_private *p_stream,
5755                                                         struct opj_event_mgr * p_manager )
5756 {
5757         OPJ_BYTE * l_current_data = 00;
5758         OPJ_UINT32 l_nb_comp;
5759         OPJ_UINT32 l_rgn_size;
5760         opj_image_t *l_image = 00;
5761         opj_cp_v2_t *l_cp = 00;
5762         opj_tcp_v2_t *l_tcp = 00;
5763         opj_tccp_t *l_tccp = 00;
5764         OPJ_UINT32 l_comp_room;
5765
5766         /* preconditions */
5767         assert(p_j2k != 00);
5768         assert(p_manager != 00);
5769         assert(p_stream != 00);
5770
5771         l_cp = &(p_j2k->m_cp);
5772         l_tcp = &l_cp->tcps[p_tile_no];
5773         l_tccp = &l_tcp->tccps[p_comp_no];
5774
5775         l_nb_comp = l_image->numcomps;
5776
5777         if (l_nb_comp <= 256) {
5778                 l_comp_room = 1;
5779         }
5780         else {
5781                 l_comp_room = 2;
5782         }
5783
5784         l_rgn_size = 6 + l_comp_room;
5785
5786         l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
5787
5788         opj_write_bytes(l_current_data,J2K_MS_RGN,2);                                   /* RGN  */
5789         l_current_data += 2;
5790
5791         opj_write_bytes(l_current_data,l_rgn_size-2,2);                                 /* Lrgn */
5792         l_current_data += 2;
5793
5794         opj_write_bytes(l_current_data,p_comp_no,l_comp_room);                  /* Crgn */
5795         l_current_data+=l_comp_room;
5796
5797         opj_write_bytes(l_current_data, 0,1);                                                   /* Srgn */
5798         ++l_current_data;
5799
5800         opj_write_bytes(l_current_data, l_tccp->roishift,1);                    /* SPrgn */
5801         ++l_current_data;
5802
5803         if (opj_stream_write_data(p_stream,p_j2k->m_specific_param.m_encoder.m_header_tile_data,l_rgn_size,p_manager) != l_rgn_size) {
5804                 return OPJ_FALSE;
5805         }
5806
5807         return OPJ_TRUE;
5808 }
5809
5810 static void j2k_read_rgn(opj_j2k_t *j2k) {
5811         int len, compno, roisty;
5812
5813         opj_cp_t *cp = j2k->cp;
5814         opj_tcp_t *tcp = j2k->state == J2K_STATE_TPH ? &cp->tcps[j2k->curtileno] : j2k->default_tcp;
5815         opj_cio_t *cio = j2k->cio;
5816         int numcomps = j2k->image->numcomps;
5817
5818         len = cio_read(cio, 2);                                                                         /* Lrgn */
5819         compno = cio_read(cio, numcomps <= 256 ? 1 : 2);                        /* Crgn */
5820         roisty = cio_read(cio, 1);                                                                      /* Srgn */
5821
5822 #ifdef USE_JPWL
5823         if (j2k->cp->correct) {
5824                 /* totlen is negative or larger than the bytes left!!! */
5825                 if (compno >= numcomps) {
5826                         opj_event_msg(j2k->cinfo, EVT_ERROR,
5827                                 "JPWL: bad component number in RGN (%d when there are only %d)\n",
5828                                 compno, numcomps);
5829                         if (!JPWL_ASSUME || JPWL_ASSUME) {
5830                                 opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
5831                                 return;
5832                         }
5833                 }
5834         };
5835 #endif /* USE_JPWL */
5836
5837         tcp->tccps[compno].roishift = cio_read(cio, 1);                         /* SPrgn */
5838 }
5839
5840 static void j2k_write_eoc(opj_j2k_t *j2k) {
5841         opj_cio_t *cio = j2k->cio;
5842         /* opj_event_msg(j2k->cinfo, "%.8x: EOC\n", cio_tell(cio) + j2k->pos_correction); */
5843         cio_write(cio, J2K_MS_EOC, 2);
5844
5845 /* UniPG>> */
5846 #ifdef USE_JPWL
5847         /* update markers struct */
5848         j2k_add_marker(j2k->cstr_info, J2K_MS_EOC, cio_tell(cio) - 2, 2);
5849 #endif /* USE_JPWL */
5850 /* <<UniPG */
5851 }
5852
5853 /**
5854  * Writes the EOC marker (End of Codestream)
5855  * 
5856  * @param       p_stream                the stream to write data to.
5857  * @param       p_j2k                   J2K codec.
5858  * @param       p_manager               the user event manager.
5859 */
5860 opj_bool j2k_write_eoc_v2(      opj_j2k_v2_t *p_j2k,
5861                                                         struct opj_stream_private *p_stream,
5862                                                         struct opj_event_mgr * p_manager )
5863 {
5864         /* preconditions */
5865         assert(p_j2k != 00);
5866         assert(p_manager != 00);
5867         assert(p_stream != 00);
5868         
5869         opj_write_bytes(p_j2k->m_specific_param.m_encoder.m_header_tile_data,J2K_MS_EOC,2);                                     /* EOC */
5870         
5871
5872 /* UniPG>> */
5873 #ifdef USE_JPWL
5874         /* update markers struct */
5875         /*
5876         j2k_add_marker(p_j2k->cstr_info, J2K_MS_EOC, p_stream_tell(p_stream) - 2, 2);
5877 */
5878 #endif /* USE_JPWL */
5879
5880         if ( opj_stream_write_data(p_stream,p_j2k->m_specific_param.m_encoder.m_header_tile_data,2,p_manager) != 2) {
5881                 return OPJ_FALSE;
5882         }
5883
5884         if ( ! opj_stream_flush(p_stream,p_manager) ) {
5885                 return OPJ_FALSE;
5886         }
5887
5888         return OPJ_TRUE;
5889 }
5890
5891
5892 /**
5893  * Reads a RGN marker (Region Of Interest)
5894  *
5895  * @param       p_header_data   the data contained in the POC box.
5896  * @param       p_j2k                   the jpeg2000 codec.
5897  * @param       p_header_size   the size of the data contained in the POC marker.
5898  * @param       p_manager               the user event manager.
5899 */
5900 opj_bool j2k_read_rgn_v2 (
5901                                                 opj_j2k_v2_t *p_j2k,
5902                                                 OPJ_BYTE * p_header_data,
5903                                                 OPJ_UINT32 p_header_size,
5904                                                 struct opj_event_mgr * p_manager
5905                                         )
5906 {
5907         OPJ_UINT32 l_nb_comp;
5908         opj_image_t * l_image = 00;
5909
5910         opj_cp_v2_t *l_cp = 00;
5911         opj_tcp_v2_t *l_tcp = 00;
5912         OPJ_UINT32 l_comp_room, l_comp_no, l_roi_sty;
5913
5914         /* preconditions*/
5915         assert(p_header_data != 00);
5916         assert(p_j2k != 00);
5917         assert(p_manager != 00);
5918
5919         l_image = p_j2k->m_private_image;
5920         l_nb_comp = l_image->numcomps;
5921
5922         if (l_nb_comp <= 256) {
5923                 l_comp_room = 1; }
5924         else {
5925                 l_comp_room = 2; }
5926
5927         if (p_header_size != 2 + l_comp_room) {
5928                 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading RGN marker\n");
5929                 return OPJ_FALSE;
5930         }
5931
5932         l_cp = &(p_j2k->m_cp);
5933         l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ?
5934                                 &l_cp->tcps[p_j2k->m_current_tile_number] :
5935                                 p_j2k->m_specific_param.m_decoder.m_default_tcp;
5936
5937         opj_read_bytes(p_header_data,&l_comp_no,l_comp_room);           /* Crgn */
5938         p_header_data+=l_comp_room;
5939         opj_read_bytes(p_header_data,&l_roi_sty,1);                                     /* Srgn */
5940         ++p_header_data;
5941
5942 #ifdef USE_JPWL
5943         if (l_cp->correct) {
5944                 /* totlen is negative or larger than the bytes left!!! */
5945                 if (l_comp_room >= l_nb_comp) {
5946                         opj_event_msg_v2(p_manager, EVT_ERROR,
5947                                 "JPWL: bad component number in RGN (%d when there are only %d)\n",
5948                                 l_comp_room, l_nb_comp);
5949                         if (!JPWL_ASSUME || JPWL_ASSUME) {
5950                                 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
5951                                 return OPJ_FALSE;
5952                         }
5953                 }
5954         };
5955 #endif /* USE_JPWL */
5956
5957         opj_read_bytes(p_header_data,(OPJ_UINT32 *) (&(l_tcp->tccps[l_comp_no].roishift)),1);   /* SPrgn */
5958         ++p_header_data;
5959
5960         return OPJ_TRUE;
5961
5962 }
5963
5964 static OPJ_FLOAT32 get_tp_stride (opj_tcp_v2_t * p_tcp)
5965 {
5966         return (OPJ_FLOAT32) ((p_tcp->m_nb_tile_parts - 1) * 14);
5967 }
5968
5969 static OPJ_FLOAT32 get_default_stride (opj_tcp_v2_t * p_tcp)
5970 {
5971   (void)p_tcp;
5972         return 0;
5973 }
5974
5975 /**
5976  * Updates the rates of the tcp.
5977  *
5978  * @param       p_stream                the stream to write data to.
5979  * @param       p_j2k                   J2K codec.
5980  * @param       p_manager               the user event manager.
5981 */
5982 opj_bool j2k_update_rates(      opj_j2k_v2_t *p_j2k,
5983                                                         struct opj_stream_private *p_stream,
5984                                                         struct opj_event_mgr * p_manager )
5985 {
5986         opj_cp_v2_t * l_cp = 00;
5987         opj_image_t * l_image = 00;
5988         opj_tcp_v2_t * l_tcp = 00;
5989         opj_image_comp_t * l_img_comp = 00;
5990
5991         OPJ_UINT32 i,j,k;
5992         OPJ_INT32 l_x0,l_y0,l_x1,l_y1;
5993         OPJ_FLOAT32 * l_rates = 0;
5994         OPJ_FLOAT32 l_sot_remove;
5995         OPJ_UINT32 l_bits_empty, l_size_pixel;
5996         OPJ_UINT32 l_tile_size = 0;
5997         OPJ_UINT32 l_last_res;
5998         OPJ_FLOAT32 (* l_tp_stride_func)(opj_tcp_v2_t *) = 00;
5999
6000         /* preconditions */
6001         assert(p_j2k != 00);
6002         assert(p_manager != 00);
6003         assert(p_stream != 00);
6004
6005
6006         l_cp = &(p_j2k->m_cp);
6007         l_image = p_j2k->m_private_image;
6008         l_tcp = l_cp->tcps;
6009
6010         l_bits_empty = 8 * l_image->comps->dx * l_image->comps->dy;
6011         l_size_pixel = l_image->numcomps * l_image->comps->prec;
6012         l_sot_remove = ((OPJ_FLOAT32) opj_stream_tell(p_stream)) / (l_cp->th * l_cp->tw);
6013
6014         if (l_cp->m_specific_param.m_enc.m_tp_on) {
6015                 l_tp_stride_func = get_tp_stride;
6016         }
6017         else {
6018                 l_tp_stride_func = get_default_stride;
6019         }
6020
6021         for (i=0;i<l_cp->th;++i) {
6022                 for (j=0;j<l_cp->tw;++j) {
6023                         OPJ_FLOAT32 l_offset = ((*l_tp_stride_func)(l_tcp)) / l_tcp->numlayers;
6024
6025                         /* 4 borders of the tile rescale on the image if necessary */
6026                         l_x0 = int_max(l_cp->tx0 + j * l_cp->tdx, l_image->x0);
6027                         l_y0 = int_max(l_cp->ty0 + i * l_cp->tdy, l_image->y0);
6028                         l_x1 = int_min(l_cp->tx0 + (j + 1) * l_cp->tdx, l_image->x1);
6029                         l_y1 = int_min(l_cp->ty0 + (i + 1) * l_cp->tdy, l_image->y1);
6030
6031                         l_rates = l_tcp->rates;
6032
6033                         /* Modification of the RATE >> */
6034                         if (*l_rates) {
6035                                 *l_rates =              (( (float) (l_size_pixel * (l_x1 - l_x0) * (l_y1 - l_y0)))
6036                                                                 /
6037                                                                 ((*l_rates) * l_bits_empty)
6038                                                                 )
6039                                                                 -
6040                                                                 l_offset;
6041                         }
6042
6043                         ++l_rates;
6044
6045                         for (k = 1; k < l_tcp->numlayers; ++k) {
6046                                 if (*l_rates) {
6047                                         *l_rates =              (( (OPJ_FLOAT32) (l_size_pixel * (l_x1 - l_x0) * (l_y1 - l_y0)))
6048                                                                         /
6049                                                                                 ((*l_rates) * l_bits_empty)
6050                                                                         )
6051                                                                         -
6052                                                                         l_offset;
6053                                 }
6054
6055                                 ++l_rates;
6056                         }
6057
6058                         ++l_tcp;
6059
6060                 }
6061         }
6062
6063         l_tcp = l_cp->tcps;
6064
6065         for (i=0;i<l_cp->th;++i) {
6066                 for     (j=0;j<l_cp->tw;++j) {
6067                         l_rates = l_tcp->rates;
6068
6069                         if (*l_rates) {
6070                                 *l_rates -= l_sot_remove;
6071
6072                                 if (*l_rates < 30) {
6073                                         *l_rates = 30;
6074                                 }
6075                         }
6076
6077                         ++l_rates;
6078
6079                         l_last_res = l_tcp->numlayers - 1;
6080
6081                         for (k = 1; k < l_last_res; ++k) {
6082
6083                                 if (*l_rates) {
6084                                         *l_rates -= l_sot_remove;
6085
6086                                         if (*l_rates < *(l_rates - 1) + 10) {
6087                                                 *l_rates  = (*(l_rates - 1)) + 20;
6088                                         }
6089                                 }
6090
6091                                 ++l_rates;
6092                         }
6093
6094                         if (*l_rates) {
6095                                 *l_rates -= (l_sot_remove + 2.f);
6096
6097                                 if (*l_rates < *(l_rates - 1) + 10) {
6098                                         *l_rates  = (*(l_rates - 1)) + 20;
6099                                 }
6100                         }
6101
6102                         ++l_tcp;
6103                 }
6104         }
6105
6106         l_img_comp = l_image->comps;
6107         l_tile_size = 0;
6108
6109         for (i=0;i<l_image->numcomps;++i) {
6110                 l_tile_size += (        uint_ceildiv(l_cp->tdx,l_img_comp->dx)
6111                                                         *
6112                                                         uint_ceildiv(l_cp->tdy,l_img_comp->dy)
6113                                                         *
6114                                                         l_img_comp->prec
6115                                                 );
6116
6117                 ++l_img_comp;
6118         }
6119
6120         l_tile_size = (OPJ_UINT32) (l_tile_size * 0.1625); /* 1.3/8 = 0.1625 */
6121
6122         l_tile_size += j2k_get_specific_header_sizes(p_j2k);
6123
6124         p_j2k->m_specific_param.m_encoder.m_encoded_tile_size = l_tile_size;
6125         p_j2k->m_specific_param.m_encoder.m_encoded_tile_data =
6126                         (OPJ_BYTE *) opj_malloc(p_j2k->m_specific_param.m_encoder.m_encoded_tile_size);
6127         if (p_j2k->m_specific_param.m_encoder.m_encoded_tile_data == 00) {
6128                 return OPJ_FALSE;
6129         }
6130
6131         if (l_cp->m_specific_param.m_enc.m_cinema) {
6132                 p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer =
6133                                 (OPJ_BYTE *) opj_malloc(5*p_j2k->m_specific_param.m_encoder.m_total_tile_parts);
6134                 if (! p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer) {
6135                         return OPJ_FALSE;
6136                 }
6137
6138                 p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current =
6139                                 p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer;
6140         }
6141
6142         return OPJ_TRUE;
6143 }
6144
6145 static void j2k_read_eoc(opj_j2k_t *j2k) {
6146         int i, tileno;
6147         opj_bool success = OPJ_FALSE;
6148
6149         /* if packets should be decoded */
6150         if (j2k->cp->limit_decoding != DECODE_ALL_BUT_PACKETS) {
6151                 opj_tcd_t *tcd = tcd_create(j2k->cinfo);
6152                 tcd_malloc_decode(tcd, j2k->image, j2k->cp);
6153                 for (i = 0; i < j2k->cp->tileno_size; i++) {
6154                         tcd_malloc_decode_tile(tcd, j2k->image, j2k->cp, i, j2k->cstr_info);
6155                         if (j2k->cp->tileno[i] != -1)
6156                         {
6157                                 tileno = j2k->cp->tileno[i];
6158                                 success = tcd_decode_tile(tcd, j2k->tile_data[tileno], j2k->tile_len[tileno], tileno, j2k->cstr_info);
6159                                 opj_free(j2k->tile_data[tileno]);
6160                                 j2k->tile_data[tileno] = NULL;
6161                                 tcd_free_decode_tile(tcd, i);
6162                         }
6163                         else
6164                                 success = OPJ_FALSE;
6165                         if (success == OPJ_FALSE) {
6166                                 j2k->state |= J2K_STATE_ERR;
6167                                 break;
6168                         }
6169                 }
6170                 tcd_free_decode(tcd);
6171                 tcd_destroy(tcd);
6172         }
6173         /* if packets should not be decoded  */
6174         else {
6175                 for (i = 0; i < j2k->cp->tileno_size; i++) {
6176                         tileno = j2k->cp->tileno[i];
6177                         opj_free(j2k->tile_data[tileno]);
6178                         j2k->tile_data[tileno] = NULL;
6179                 }
6180         }       
6181         if (j2k->state & J2K_STATE_ERR)
6182                 j2k->state = J2K_STATE_MT + J2K_STATE_ERR;
6183         else
6184                 j2k->state = J2K_STATE_MT; 
6185 }
6186
6187 /**
6188  * Reads a EOC marker (End Of Codestream)
6189  *
6190  * @param       p_header_data   the data contained in the SOD box.
6191  * @param       p_j2k                   the jpeg2000 codec.
6192  * @param       p_header_size   the size of the data contained in the SOD marker.
6193  * @param       p_manager               the user event manager.
6194 */
6195 #if 0
6196 opj_bool j2k_read_eoc_v2 (      opj_j2k_v2_t *p_j2k,
6197                                                         struct opj_stream_private *p_stream,
6198                                                         struct opj_event_mgr * p_manager )
6199 {
6200         OPJ_UINT32 i;
6201         opj_tcd_v2_t * l_tcd = 00;
6202         OPJ_UINT32 l_nb_tiles;
6203         opj_tcp_v2_t * l_tcp = 00;
6204         opj_bool l_success;
6205
6206         /* preconditions */
6207         assert(p_j2k != 00);
6208         assert(p_manager != 00);
6209         assert(p_stream != 00);
6210
6211         l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw;
6212         l_tcp = p_j2k->m_cp.tcps;
6213
6214         l_tcd = tcd_create_v2(OPJ_TRUE);
6215         if (l_tcd == 00) {
6216                 opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot decode tile, memory error\n");
6217                 return OPJ_FALSE;
6218         }
6219
6220         for (i = 0; i < l_nb_tiles; ++i) {
6221                 if (l_tcp->m_data) {
6222                         if (! tcd_init_decode_tile(l_tcd, i)) {
6223                                 tcd_destroy_v2(l_tcd);
6224                                 opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot decode tile, memory error\n");
6225                                 return OPJ_FALSE;
6226                         }
6227
6228                         l_success = tcd_decode_tile_v2(l_tcd, l_tcp->m_data, l_tcp->m_data_size, i, p_j2k->cstr_index);
6229                         /* cleanup */
6230
6231                         if (! l_success) {
6232                                 p_j2k->m_specific_param.m_decoder.m_state |= J2K_STATE_ERR;
6233                                 break;
6234                         }
6235                 }
6236
6237                 j2k_tcp_destroy(l_tcp);
6238                 ++l_tcp;
6239         }
6240
6241         tcd_destroy_v2(l_tcd);
6242         return OPJ_TRUE;
6243 }
6244 #endif
6245
6246 /**
6247  * Gets the offset of the header.
6248  *
6249  * @param       p_stream                                the stream to write data to.
6250  * @param       p_j2k                           J2K codec.
6251  * @param       p_manager               the user event manager.
6252 */
6253 opj_bool j2k_get_end_header(opj_j2k_v2_t *p_j2k,
6254                                                         struct opj_stream_private *p_stream,
6255                                                         struct opj_event_mgr * p_manager )
6256 {
6257         /* preconditions */
6258         assert(p_j2k != 00);
6259         assert(p_manager != 00);
6260         assert(p_stream != 00);
6261
6262         p_j2k->cstr_index->main_head_end = opj_stream_tell(p_stream);
6263
6264         return OPJ_TRUE;
6265 }
6266
6267 /**
6268  * Writes the MCT marker (Multiple Component Transform)
6269  *
6270  * @param       p_stream                                the stream to write data to.
6271  * @param       p_j2k                           J2K codec.
6272  * @param       p_manager               the user event manager.
6273 */
6274 opj_bool j2k_write_mct_data_group(      opj_j2k_v2_t *p_j2k,
6275                                                                         struct opj_stream_private *p_stream,
6276                                                                         struct opj_event_mgr * p_manager )
6277 {
6278         OPJ_UINT32 i;
6279         opj_simple_mcc_decorrelation_data_t * l_mcc_record;
6280         opj_mct_data_t * l_mct_record;
6281         opj_tcp_v2_t * l_tcp;
6282
6283         /* preconditions */
6284         assert(p_j2k != 00);
6285         assert(p_stream != 00);
6286         assert(p_manager != 00);
6287
6288         if (! j2k_write_cbd(p_j2k,p_stream,p_manager)) {
6289                 return OPJ_FALSE;
6290         }
6291
6292         l_tcp = &(p_j2k->m_cp.tcps[p_j2k->m_current_tile_number]);
6293         l_mct_record = l_tcp->m_mct_records;
6294
6295         for (i=0;i<l_tcp->m_nb_mct_records;++i) {
6296
6297                 if (! j2k_write_mct_record(p_j2k,l_mct_record,p_stream,p_manager)) {
6298                         return OPJ_FALSE;
6299                 }
6300
6301                 ++l_mct_record;
6302         }
6303
6304         l_mcc_record = l_tcp->m_mcc_records;
6305
6306         for     (i=0;i<l_tcp->m_nb_mcc_records;++i) {
6307
6308                 if (! j2k_write_mcc_record(p_j2k,l_mcc_record,p_stream,p_manager)) {
6309                         return OPJ_FALSE;
6310                 }
6311
6312                 ++l_mcc_record;
6313         }
6314
6315         if (! j2k_write_mco(p_j2k,p_stream,p_manager)) {
6316                 return OPJ_FALSE;
6317         }
6318
6319         return OPJ_TRUE;
6320 }
6321
6322 /**
6323  * Writes the image components.
6324  *
6325  * @param       p_stream                                the stream to write data to.
6326  * @param       p_j2k                           J2K codec.
6327  * @param       p_manager               the user event manager.
6328 */
6329 opj_bool j2k_write_image_components(opj_j2k_v2_t *p_j2k,
6330                                                                         struct opj_stream_private *p_stream,
6331                                                                         struct opj_event_mgr * p_manager )
6332 {
6333         OPJ_UINT32 compno;
6334
6335         /* preconditions */
6336         assert(p_j2k != 00);
6337         assert(p_manager != 00);
6338         assert(p_stream != 00);
6339
6340         for (compno = 1; compno < p_j2k->m_private_image->numcomps; ++compno)
6341         {
6342                 if (! j2k_write_coc_v2(p_j2k,compno,p_stream, p_manager)) {
6343                         return OPJ_FALSE;
6344                 }
6345
6346                 if (! j2k_write_qcc_v2(p_j2k,compno,p_stream, p_manager)) {
6347                         return OPJ_FALSE;
6348                 }
6349         }
6350
6351         return OPJ_TRUE;
6352 }
6353
6354 /**
6355  * Writes regions of interests.
6356  *
6357  * @param       p_stream                                the stream to write data to.
6358  * @param       p_j2k                           J2K codec.
6359  * @param       p_manager               the user event manager.
6360 */
6361 opj_bool j2k_write_regions(     opj_j2k_v2_t *p_j2k,
6362                                                         struct opj_stream_private *p_stream,
6363                                                         struct opj_event_mgr * p_manager )
6364 {
6365         OPJ_UINT32 compno;
6366         const opj_tccp_t *l_tccp = 00;
6367
6368         /* preconditions */
6369         assert(p_j2k != 00);
6370         assert(p_manager != 00);
6371         assert(p_stream != 00);
6372
6373         l_tccp = p_j2k->m_cp.tcps->tccps;
6374
6375         for     (compno = 0; compno < p_j2k->m_private_image->numcomps; ++compno)  {
6376                 if (l_tccp->roishift) {
6377
6378                         if (! j2k_write_rgn_v2(p_j2k,0,compno,p_stream,p_manager)) {
6379                                 return OPJ_FALSE;
6380                         }
6381                 }
6382
6383                 ++l_tccp;
6384         }
6385
6386         return OPJ_TRUE;
6387 }
6388
6389 /**
6390  * Writes EPC ????
6391  *
6392  * @param       p_stream                the stream to write data to.
6393  * @param       p_j2k                   J2K codec.
6394  * @param       p_manager               the user event manager.
6395 */
6396 opj_bool j2k_write_epc( opj_j2k_v2_t *p_j2k,
6397                                                 struct opj_stream_private *p_stream,
6398                                                 struct opj_event_mgr * p_manager )
6399 {
6400         opj_codestream_index_t * l_cstr_index = 00;
6401
6402         /* preconditions */
6403         assert(p_j2k != 00);
6404         assert(p_manager != 00);
6405         assert(p_stream != 00);
6406
6407         l_cstr_index = p_j2k->cstr_index;
6408         if (l_cstr_index) {
6409                 l_cstr_index->codestream_size = opj_stream_tell(p_stream);
6410                 /* UniPG>> */
6411                 /* The following adjustment is done to adjust the codestream size */
6412                 /* if SOD is not at 0 in the buffer. Useful in case of JP2, where */
6413                 /* the first bunch of bytes is not in the codestream              */
6414                 l_cstr_index->codestream_size -= l_cstr_index->main_head_start;
6415                 /* <<UniPG */
6416         }
6417
6418 #ifdef USE_JPWL
6419         /* preparation of JPWL marker segments */
6420 #if 0
6421         if(cp->epc_on) {
6422
6423                 /* encode according to JPWL */
6424                 jpwl_encode(p_j2k, p_stream, image);
6425
6426         }
6427 #endif
6428   assert( 0 && "TODO" );
6429 #endif /* USE_JPWL */
6430
6431         return OPJ_TRUE;
6432 }
6433
6434 typedef struct opj_dec_mstabent {
6435         /** marker value */
6436         int id;
6437         /** value of the state when the marker can appear */
6438         int states;
6439         /** action linked to the marker */
6440         void (*handler) (opj_j2k_t *j2k);
6441 } opj_dec_mstabent_t;
6442
6443 opj_dec_mstabent_t j2k_dec_mstab[] = {
6444   /*{J2K_MS_SOC, J2K_STATE_MHSOC, j2k_read_soc},*/
6445   /*{J2K_MS_SOT, J2K_STATE_MH | J2K_STATE_TPHSOT, j2k_read_sot},*/
6446   {J2K_MS_SOD, J2K_STATE_TPH, j2k_read_sod},
6447   {J2K_MS_EOC, J2K_STATE_TPHSOT, j2k_read_eoc},
6448   {J2K_MS_SIZ, J2K_STATE_MHSIZ, j2k_read_siz},
6449   {J2K_MS_COD, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_cod},
6450   {J2K_MS_COC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_coc},
6451   {J2K_MS_RGN, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_rgn},
6452   {J2K_MS_QCD, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_qcd},
6453   {J2K_MS_QCC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_qcc},
6454   {J2K_MS_POC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_poc},
6455   {J2K_MS_TLM, J2K_STATE_MH, j2k_read_tlm},
6456   {J2K_MS_PLM, J2K_STATE_MH, j2k_read_plm},
6457   {J2K_MS_PLT, J2K_STATE_TPH, j2k_read_plt},
6458   {J2K_MS_PPM, J2K_STATE_MH, j2k_read_ppm},
6459   {J2K_MS_PPT, J2K_STATE_TPH, j2k_read_ppt},
6460   {J2K_MS_SOP, 0, 0},
6461   {J2K_MS_CRG, J2K_STATE_MH, j2k_read_crg},
6462   {J2K_MS_COM, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_com},
6463
6464 #ifdef USE_JPWL
6465   {J2K_MS_EPC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_epc},
6466   {J2K_MS_EPB, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_epb},
6467   {J2K_MS_ESD, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_esd},
6468   {J2K_MS_RED, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_red},
6469 #endif /* USE_JPWL */
6470 #ifdef USE_JPSEC
6471   {J2K_MS_SEC, J2K_STATE_MH, j2k_read_sec},
6472   {J2K_MS_INSEC, 0, j2k_read_insec},
6473 #endif /* USE_JPSEC */
6474
6475   {0, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_unk}
6476 };
6477
6478 static void j2k_read_unk(opj_j2k_t *j2k) {
6479         opj_event_msg(j2k->cinfo, EVT_WARNING, "Unknown marker\n");
6480
6481 #ifdef USE_JPWL
6482         if (j2k->cp->correct) {
6483                 int m = 0, id, i;
6484                 int min_id = 0, min_dist = 17, cur_dist = 0, tmp_id;
6485                 cio_seek(j2k->cio, cio_tell(j2k->cio) - 2);
6486                 id = cio_read(j2k->cio, 2);
6487                 opj_event_msg(j2k->cinfo, EVT_ERROR,
6488                         "JPWL: really don't know this marker %x\n",
6489                         id);
6490                 if (!JPWL_ASSUME) {
6491                         opj_event_msg(j2k->cinfo, EVT_ERROR,
6492                                 "- possible synch loss due to uncorrectable codestream errors => giving up\n");
6493                         return;
6494                 }
6495                 /* OK, activate this at your own risk!!! */
6496                 /* we look for the marker at the minimum hamming distance from this */
6497                 while (j2k_dec_mstab[m].id) {
6498                         
6499                         /* 1's where they differ */
6500                         tmp_id = j2k_dec_mstab[m].id ^ id;
6501
6502                         /* compute the hamming distance between our id and the current */
6503                         cur_dist = 0;
6504                         for (i = 0; i < 16; i++) {
6505                                 if ((tmp_id >> i) & 0x0001) {
6506                                         cur_dist++;
6507                                 }
6508                         }
6509
6510                         /* if current distance is smaller, set the minimum */
6511                         if (cur_dist < min_dist) {
6512                                 min_dist = cur_dist;
6513                                 min_id = j2k_dec_mstab[m].id;
6514                         }
6515                         
6516                         /* jump to the next marker */
6517                         m++;
6518                 }
6519
6520                 /* do we substitute the marker? */
6521                 if (min_dist < JPWL_MAXIMUM_HAMMING) {
6522                         opj_event_msg(j2k->cinfo, EVT_ERROR,
6523                                 "- marker %x is at distance %d from the read %x\n",
6524                                 min_id, min_dist, id);
6525                         opj_event_msg(j2k->cinfo, EVT_ERROR,
6526                                 "- trying to substitute in place and crossing fingers!\n");
6527                         cio_seek(j2k->cio, cio_tell(j2k->cio) - 2);
6528                         cio_write(j2k->cio, min_id, 2);
6529
6530                         /* rewind */
6531                         cio_seek(j2k->cio, cio_tell(j2k->cio) - 2);
6532
6533                 }
6534
6535         };
6536 #endif /* USE_JPWL */
6537
6538 }
6539
6540 /**
6541  * Reads an unknown marker
6542  *
6543  * @param       p_stream                                the stream object to read from.
6544  * @param       p_j2k                   the jpeg2000 codec.
6545  * @param       p_manager               the user event manager.
6546  *
6547  * @return      true                    if the marker could be deduced.
6548 */
6549 opj_bool j2k_read_unk_v2 (      opj_j2k_v2_t *p_j2k,
6550                                                         struct opj_stream_private *p_stream,
6551                                                         OPJ_UINT32 *output_marker,
6552                                                         struct opj_event_mgr * p_manager
6553                                                         )
6554 {
6555         OPJ_UINT32 l_unknown_marker;
6556         const opj_dec_memory_marker_handler_t * l_marker_handler;
6557         OPJ_UINT32 l_size_unk = 2;
6558
6559         /* preconditions*/
6560         assert(p_j2k != 00);
6561         assert(p_manager != 00);
6562         assert(p_stream != 00);
6563
6564         opj_event_msg_v2(p_manager, EVT_WARNING, "Unknown marker\n");
6565
6566         while(1) {
6567                 /* Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer*/
6568                 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
6569                         opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
6570                         return OPJ_FALSE;
6571                 }
6572
6573                 /* read 2 bytes as the new marker ID*/
6574                 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_unknown_marker,2);
6575
6576                 if (!(l_unknown_marker < 0xff00)) {
6577
6578                         /* Get the marker handler from the marker ID*/
6579                         l_marker_handler = j2k_get_marker_handler(l_unknown_marker);
6580
6581                         if (!(p_j2k->m_specific_param.m_decoder.m_state & l_marker_handler->states)) {
6582                                 opj_event_msg_v2(p_manager, EVT_ERROR, "Marker is not compliant with its position\n");
6583                                 return OPJ_FALSE;
6584                         }
6585                         else {
6586                                 if (l_marker_handler->id != J2K_MS_UNK) {
6587                                         /* Add the marker to the codestream index*/
6588                                         if (l_marker_handler->id != J2K_MS_SOT)
6589                                                 j2k_add_mhmarker_v2(p_j2k->cstr_index, J2K_MS_UNK,
6590                                                                                         (OPJ_UINT32) opj_stream_tell(p_stream) - l_size_unk,
6591                                                                                         l_size_unk);
6592                                         break; /* next marker is known and well located */
6593                                 }
6594                                 else
6595                                         l_size_unk += 2;
6596                         }
6597                 }
6598         }
6599
6600         *output_marker = l_marker_handler->id ;
6601
6602         return OPJ_TRUE;
6603 }
6604
6605 /**
6606 Read the lookup table containing all the marker, status and action
6607 @param id Marker value
6608 */
6609 static opj_dec_mstabent_t *j2k_dec_mstab_lookup(int id) {
6610         opj_dec_mstabent_t *e;
6611         for (e = j2k_dec_mstab; e->id != 0; e++) {
6612                 if (e->id == id) {
6613                         break;
6614                 }
6615         }
6616         return e;
6617 }
6618
6619 /**
6620  * Writes the MCT marker (Multiple Component Transform)
6621  *
6622  * @param       p_stream                                the stream to write data to.
6623  * @param       p_j2k                           J2K codec.
6624  * @param       p_manager               the user event manager.
6625 */
6626 opj_bool j2k_write_mct_record(  opj_j2k_v2_t *p_j2k,
6627                                                                 opj_mct_data_t * p_mct_record,
6628                                                                 struct opj_stream_private *p_stream,
6629                                                                 struct opj_event_mgr * p_manager )
6630 {
6631         OPJ_UINT32 l_mct_size;
6632         OPJ_BYTE * l_current_data = 00;
6633         OPJ_UINT32 l_tmp;
6634
6635         /* preconditions */
6636         assert(p_j2k != 00);
6637         assert(p_manager != 00);
6638         assert(p_stream != 00);
6639
6640         l_mct_size = 10 + p_mct_record->m_data_size;
6641
6642         if (l_mct_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
6643                 p_j2k->m_specific_param.m_encoder.m_header_tile_data
6644                         = (OPJ_BYTE*)opj_realloc(
6645                                 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
6646                                 l_mct_size);
6647
6648                 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
6649                         return OPJ_FALSE;
6650                 }
6651
6652                 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_mct_size;
6653         }
6654
6655         l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
6656
6657         opj_write_bytes(l_current_data,J2K_MS_MCT,2);                                   /* MCT */
6658         l_current_data += 2;
6659
6660         opj_write_bytes(l_current_data,l_mct_size-2,2);                                 /* Lmct */
6661         l_current_data += 2;
6662
6663         opj_write_bytes(l_current_data,0,2);                                                    /* Zmct */
6664         l_current_data += 2;
6665
6666         /* only one marker atm */
6667         l_tmp = (p_mct_record->m_index & 0xff) | (p_mct_record->m_array_type << 8) | (p_mct_record->m_element_type << 10);
6668
6669         opj_write_bytes(l_current_data,l_tmp,2);
6670         l_current_data += 2;
6671
6672         opj_write_bytes(l_current_data,0,2);                                                    /* Ymct */
6673         l_current_data+=2;
6674
6675         memcpy(l_current_data,p_mct_record->m_data,p_mct_record->m_data_size);
6676
6677         if (opj_stream_write_data(p_stream,p_j2k->m_specific_param.m_encoder.m_header_tile_data,l_mct_size,p_manager) != l_mct_size) {
6678                 return OPJ_FALSE;
6679         }
6680
6681         return OPJ_TRUE;
6682 }
6683
6684 /**
6685  * Reads a MCT marker (Multiple Component Transform)
6686  *
6687  * @param       p_header_data   the data contained in the MCT box.
6688  * @param       p_j2k                   the jpeg2000 codec.
6689  * @param       p_header_size   the size of the data contained in the MCT marker.
6690  * @param       p_manager               the user event manager.
6691 */
6692 opj_bool j2k_read_mct ( opj_j2k_v2_t *p_j2k,
6693                                                 OPJ_BYTE * p_header_data,
6694                                                 OPJ_UINT32 p_header_size,
6695                                                 struct opj_event_mgr * p_manager )
6696 {
6697         OPJ_UINT32 i;
6698         opj_tcp_v2_t *l_tcp = 00;
6699         OPJ_UINT32 l_tmp;
6700         OPJ_UINT32 l_indix;
6701         opj_mct_data_t * l_mct_data;
6702
6703         /* preconditions */
6704         assert(p_header_data != 00);
6705         assert(p_j2k != 00);
6706
6707         l_tcp = p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH ?
6708                         &p_j2k->m_cp.tcps[p_j2k->m_current_tile_number] :
6709                         p_j2k->m_specific_param.m_decoder.m_default_tcp;
6710
6711         if (p_header_size < 2) {
6712                 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCT marker\n");
6713                 return OPJ_FALSE;
6714         }
6715
6716         /* first marker */
6717         opj_read_bytes(p_header_data,&l_tmp,2);                         /* Zmct */
6718         p_header_data += 2;
6719         if (l_tmp != 0) {
6720                 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge mct data within multiple MCT records\n");
6721                 return OPJ_TRUE;
6722         }
6723
6724         if(p_header_size <= 6) {
6725                 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCT marker\n");
6726                 return OPJ_FALSE;
6727         }
6728
6729         /* Imct -> no need for other values, take the first, type is double with decorrelation x0000 1101 0000 0000*/
6730         opj_read_bytes(p_header_data,&l_tmp,2);                         /* Imct */
6731         p_header_data += 2;
6732
6733         l_indix = l_tmp & 0xff;
6734         l_mct_data = l_tcp->m_mct_records;
6735
6736         for (i=0;i<l_tcp->m_nb_mct_records;++i) {
6737                 if (l_mct_data->m_index == l_indix) {
6738                         break;
6739                 }
6740                 ++l_mct_data;
6741         }
6742
6743         /* NOT FOUND */
6744         if (i == l_tcp->m_nb_mct_records) {
6745                 if (l_tcp->m_nb_mct_records == l_tcp->m_nb_max_mct_records) {
6746                         l_tcp->m_nb_max_mct_records += J2K_MCT_DEFAULT_NB_RECORDS;
6747
6748                         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));
6749                         if(! l_tcp->m_mct_records) {
6750                                 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCT marker\n");
6751                                 return OPJ_FALSE;
6752                         }
6753
6754                         l_mct_data = l_tcp->m_mct_records + l_tcp->m_nb_mct_records;
6755                         memset(l_mct_data ,0,(l_tcp->m_nb_max_mct_records - l_tcp->m_nb_mct_records) * sizeof(opj_mct_data_t));
6756                 }
6757
6758                 l_mct_data = l_tcp->m_mct_records + l_tcp->m_nb_mct_records;
6759         }
6760
6761         if (l_mct_data->m_data) {
6762                 opj_free(l_mct_data->m_data);
6763                 l_mct_data->m_data = 00;
6764         }
6765
6766         l_mct_data->m_index = l_indix;
6767         l_mct_data->m_array_type = (J2K_MCT_ARRAY_TYPE)((l_tmp  >> 8) & 3);
6768         l_mct_data->m_element_type = (J2K_MCT_ELEMENT_TYPE)((l_tmp  >> 10) & 3);
6769
6770         opj_read_bytes(p_header_data,&l_tmp,2);                         /* Ymct */
6771         p_header_data+=2;
6772         if (l_tmp != 0) {
6773                 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge multiple MCT markers\n");
6774                 return OPJ_TRUE;
6775         }
6776
6777         p_header_size -= 6;
6778
6779         l_mct_data->m_data = (OPJ_BYTE*)opj_malloc(p_header_size);
6780         if (! l_mct_data->m_data) {
6781                 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCT marker\n");
6782                 return OPJ_FALSE;
6783         }
6784         memcpy(l_mct_data->m_data,p_header_data,p_header_size);
6785
6786         l_mct_data->m_data_size = p_header_size;
6787         ++l_tcp->m_nb_mct_records;
6788
6789         return OPJ_TRUE;
6790 }
6791
6792 /**
6793  * Writes the MCC marker (Multiple Component Collection)
6794  *
6795  * @param       p_stream                the stream to write data to.
6796  * @param       p_j2k                   J2K codec.
6797  * @param       p_manager               the user event manager.
6798 */
6799 opj_bool j2k_write_mcc_record(  opj_j2k_v2_t *p_j2k,
6800                                                                 struct opj_simple_mcc_decorrelation_data * p_mcc_record,
6801                                                                 struct opj_stream_private *p_stream,
6802                                                                 struct opj_event_mgr * p_manager )
6803 {
6804         OPJ_UINT32 i;
6805         OPJ_UINT32 l_mcc_size;
6806         OPJ_BYTE * l_current_data = 00;
6807         OPJ_UINT32 l_nb_bytes_for_comp;
6808         OPJ_UINT32 l_mask;
6809         OPJ_UINT32 l_tmcc;
6810
6811         /* preconditions */
6812         assert(p_j2k != 00);
6813         assert(p_manager != 00);
6814         assert(p_stream != 00);
6815
6816         if (p_mcc_record->m_nb_comps > 255 ) {
6817         l_nb_bytes_for_comp = 2;
6818                 l_mask = 0x8000;
6819         }
6820         else {
6821                 l_nb_bytes_for_comp = 1;
6822                 l_mask = 0;
6823         }
6824
6825         l_mcc_size = p_mcc_record->m_nb_comps * 2 * l_nb_bytes_for_comp + 19;
6826         if (l_mcc_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size)
6827         {
6828                 p_j2k->m_specific_param.m_encoder.m_header_tile_data
6829                         = (OPJ_BYTE*)opj_realloc(
6830                                 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
6831                                 l_mcc_size);
6832                 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
6833                         return OPJ_FALSE;
6834                 }
6835
6836                 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_mcc_size;
6837         }
6838
6839         l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
6840
6841         opj_write_bytes(l_current_data,J2K_MS_MCC,2);                                   /* MCC */
6842         l_current_data += 2;
6843
6844         opj_write_bytes(l_current_data,l_mcc_size-2,2);                                 /* Lmcc */
6845         l_current_data += 2;
6846
6847         /* first marker */
6848         opj_write_bytes(l_current_data,0,2);                                    /* Zmcc */
6849         l_current_data += 2;
6850
6851         opj_write_bytes(l_current_data,p_mcc_record->m_index,1);                                        /* Imcc -> no need for other values, take the first */
6852         ++l_current_data;
6853
6854         /* only one marker atm */
6855         opj_write_bytes(l_current_data,0,2);                                    /* Ymcc */
6856         l_current_data+=2;
6857
6858         opj_write_bytes(l_current_data,1,2);                                    /* Qmcc -> number of collections -> 1 */
6859         l_current_data+=2;
6860
6861         opj_write_bytes(l_current_data,0x1,1);                                  /* Xmcci type of component transformation -> array based decorrelation */
6862         ++l_current_data;
6863
6864         opj_write_bytes(l_current_data,p_mcc_record->m_nb_comps | l_mask,2);    /* Nmcci number of input components involved and size for each component offset = 8 bits */
6865         l_current_data+=2;
6866
6867         for (i=0;i<p_mcc_record->m_nb_comps;++i) {
6868                 opj_write_bytes(l_current_data,i,l_nb_bytes_for_comp);                          /* Cmccij Component offset*/
6869                 l_current_data+=l_nb_bytes_for_comp;
6870         }
6871
6872         opj_write_bytes(l_current_data,p_mcc_record->m_nb_comps|l_mask,2);      /* Mmcci number of output components involved and size for each component offset = 8 bits */
6873         l_current_data+=2;
6874
6875         for (i=0;i<p_mcc_record->m_nb_comps;++i)
6876         {
6877                 opj_write_bytes(l_current_data,i,l_nb_bytes_for_comp);                          /* Wmccij Component offset*/
6878                 l_current_data+=l_nb_bytes_for_comp;
6879         }
6880
6881         l_tmcc = ((!p_mcc_record->m_is_irreversible)&1)<<16;
6882
6883         if (p_mcc_record->m_decorrelation_array) {
6884                 l_tmcc |= p_mcc_record->m_decorrelation_array->m_index;
6885         }
6886
6887         if (p_mcc_record->m_offset_array) {
6888                 l_tmcc |= ((p_mcc_record->m_offset_array->m_index)<<8);
6889         }
6890
6891         opj_write_bytes(l_current_data,l_tmcc,3);       /* Tmcci : use MCT defined as number 1 and irreversible array based. */
6892         l_current_data+=3;
6893
6894         if (opj_stream_write_data(p_stream,p_j2k->m_specific_param.m_encoder.m_header_tile_data,l_mcc_size,p_manager) != l_mcc_size) {
6895                 return OPJ_FALSE;
6896         }
6897
6898         return OPJ_TRUE;
6899 }
6900
6901 /**
6902  * Reads a MCC marker (Multiple Component Collection)
6903  *
6904  * @param       p_header_data   the data contained in the MCC box.
6905  * @param       p_j2k                   the jpeg2000 codec.
6906  * @param       p_header_size   the size of the data contained in the MCC marker.
6907  * @param       p_manager               the user event manager.
6908 */
6909 opj_bool j2k_read_mcc ( opj_j2k_v2_t *p_j2k,
6910                                         OPJ_BYTE * p_header_data,
6911                                         OPJ_UINT32 p_header_size,
6912                                         struct opj_event_mgr * p_manager )
6913 {
6914         OPJ_UINT32 i,j;
6915         OPJ_UINT32 l_tmp;
6916         OPJ_UINT32 l_indix;
6917         opj_tcp_v2_t * l_tcp;
6918         opj_simple_mcc_decorrelation_data_t * l_mcc_record;
6919         opj_mct_data_t * l_mct_data;
6920         OPJ_UINT32 l_nb_collections;
6921         OPJ_UINT32 l_nb_comps;
6922         OPJ_UINT32 l_nb_bytes_by_comp;
6923
6924
6925         /* preconditions */
6926         assert(p_header_data != 00);
6927         assert(p_j2k != 00);
6928         assert(p_manager != 00);
6929
6930         l_tcp = p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH ?
6931                         &p_j2k->m_cp.tcps[p_j2k->m_current_tile_number] :
6932                         p_j2k->m_specific_param.m_decoder.m_default_tcp;
6933
6934         if (p_header_size < 2) {
6935                 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
6936                 return OPJ_FALSE;
6937         }
6938
6939         /* first marker */
6940         opj_read_bytes(p_header_data,&l_tmp,2);                         /* Zmcc */
6941         p_header_data += 2;
6942         if (l_tmp != 0) {
6943                 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge multiple data spanning\n");
6944                 return OPJ_TRUE;
6945         }
6946
6947         if (p_header_size < 7) {
6948                 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
6949                 return OPJ_FALSE;
6950         }
6951
6952         opj_read_bytes(p_header_data,&l_indix,1); /* Imcc -> no need for other values, take the first */
6953         ++p_header_data;
6954
6955         l_mcc_record = l_tcp->m_mcc_records;
6956
6957         for(i=0;i<l_tcp->m_nb_mcc_records;++i) {
6958                 if (l_mcc_record->m_index == l_indix) {
6959                         break;
6960                 }
6961                 ++l_mcc_record;
6962         }
6963
6964         /** NOT FOUND */
6965         if (i == l_tcp->m_nb_mcc_records) {
6966                 if (l_tcp->m_nb_mcc_records == l_tcp->m_nb_max_mcc_records) {
6967                         l_tcp->m_nb_max_mcc_records += J2K_MCC_DEFAULT_NB_RECORDS;
6968
6969                         l_tcp->m_mcc_records = (opj_simple_mcc_decorrelation_data_t*)
6970                                         opj_realloc(l_tcp->m_mcc_records,l_tcp->m_nb_max_mcc_records * sizeof(opj_simple_mcc_decorrelation_data_t));
6971                         if (! l_tcp->m_mcc_records) {
6972                                 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
6973                                 return OPJ_FALSE;
6974                         }
6975                         l_mcc_record = l_tcp->m_mcc_records + l_tcp->m_nb_mcc_records;
6976                         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));
6977                 }
6978                 l_mcc_record = l_tcp->m_mcc_records + l_tcp->m_nb_mcc_records;
6979         }
6980         l_mcc_record->m_index = l_indix;
6981
6982         /* only one marker atm */
6983         opj_read_bytes(p_header_data,&l_tmp,2);                         /* Ymcc */
6984         p_header_data+=2;
6985         if (l_tmp != 0) {
6986                 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge multiple data spanning\n");
6987                 return OPJ_TRUE;
6988         }
6989
6990         opj_read_bytes(p_header_data,&l_nb_collections,2);                              /* Qmcc -> number of collections -> 1 */
6991         p_header_data+=2;
6992
6993         if (l_nb_collections > 1) {
6994                 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge multiple collections\n");
6995                 return OPJ_TRUE;
6996         }
6997
6998         p_header_size -= 7;
6999
7000         for (i=0;i<l_nb_collections;++i) {
7001                 if (p_header_size < 3) {
7002                         opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
7003                         return OPJ_FALSE;
7004                 }
7005
7006                 opj_read_bytes(p_header_data,&l_tmp,1); /* Xmcci type of component transformation -> array based decorrelation */
7007                 ++p_header_data;
7008
7009                 if (l_tmp != 1) {
7010                         opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge collections other than array decorrelation\n");
7011                         return OPJ_TRUE;
7012                 }
7013
7014                 opj_read_bytes(p_header_data,&l_nb_comps,2);
7015
7016                 p_header_data+=2;
7017                 p_header_size-=3;
7018
7019                 l_nb_bytes_by_comp = 1 + (l_nb_comps>>15);
7020                 l_mcc_record->m_nb_comps = l_nb_comps & 0x7fff;
7021
7022                 if (p_header_size < (l_nb_bytes_by_comp * l_mcc_record->m_nb_comps + 2)) {
7023                         opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
7024                         return OPJ_FALSE;
7025                 }
7026
7027                 p_header_size -= (l_nb_bytes_by_comp * l_mcc_record->m_nb_comps + 2);
7028
7029                 for (j=0;j<l_mcc_record->m_nb_comps;++j) {
7030                         opj_read_bytes(p_header_data,&l_tmp,l_nb_bytes_by_comp);        /* Cmccij Component offset*/
7031                         p_header_data+=l_nb_bytes_by_comp;
7032
7033                         if (l_tmp != j) {
7034                                 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge collections with indix shuffle\n");
7035                                 return OPJ_TRUE;
7036                         }
7037                 }
7038
7039                 opj_read_bytes(p_header_data,&l_nb_comps,2);
7040                 p_header_data+=2;
7041
7042                 l_nb_bytes_by_comp = 1 + (l_nb_comps>>15);
7043                 l_nb_comps &= 0x7fff;
7044
7045                 if (l_nb_comps != l_mcc_record->m_nb_comps) {
7046                         opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge collections without same number of indixes\n");
7047                         return OPJ_TRUE;
7048                 }
7049
7050                 if (p_header_size < (l_nb_bytes_by_comp * l_mcc_record->m_nb_comps + 3)) {
7051                         opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
7052                         return OPJ_FALSE;
7053                 }
7054
7055                 p_header_size -= (l_nb_bytes_by_comp * l_mcc_record->m_nb_comps + 3);
7056
7057                 for (j=0;j<l_mcc_record->m_nb_comps;++j) {
7058                         opj_read_bytes(p_header_data,&l_tmp,l_nb_bytes_by_comp);        /* Wmccij Component offset*/
7059                         p_header_data+=l_nb_bytes_by_comp;
7060
7061                         if (l_tmp != j) {
7062                                 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge collections with indix shuffle\n");
7063                                 return OPJ_TRUE;
7064                         }
7065                 }
7066
7067                 opj_read_bytes(p_header_data,&l_tmp,3); /* Wmccij Component offset*/
7068                 p_header_data += 3;
7069
7070                 l_mcc_record->m_is_irreversible = ! ((l_tmp>>16) & 1);
7071                 l_mcc_record->m_decorrelation_array = 00;
7072                 l_mcc_record->m_offset_array = 00;
7073
7074                 l_indix = l_tmp & 0xff;
7075                 if (l_indix != 0) {
7076                         l_mct_data = l_tcp->m_mct_records;
7077                         for (j=0;j<l_tcp->m_nb_mct_records;++j) {
7078                                 if (l_mct_data->m_index == l_indix) {
7079                                         l_mcc_record->m_decorrelation_array = l_mct_data;
7080                                         break;
7081                                 }
7082                                 ++l_mct_data;
7083                         }
7084
7085                         if (l_mcc_record->m_decorrelation_array == 00) {
7086                                 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
7087                                 return OPJ_FALSE;
7088                         }
7089                 }
7090
7091                 l_indix = (l_tmp >> 8) & 0xff;
7092                 if (l_indix != 0) {
7093                         l_mct_data = l_tcp->m_mct_records;
7094                         for (j=0;j<l_tcp->m_nb_mct_records;++j) {
7095                                 if (l_mct_data->m_index == l_indix) {
7096                                         l_mcc_record->m_offset_array = l_mct_data;
7097                                         break;
7098                                 }
7099                                 ++l_mct_data;
7100                         }
7101
7102                         if (l_mcc_record->m_offset_array == 00) {
7103                                 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
7104                                 return OPJ_FALSE;
7105                         }
7106                 }
7107         }
7108
7109         if (p_header_size != 0) {
7110                 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
7111                 return OPJ_FALSE;
7112         }
7113
7114         ++l_tcp->m_nb_mcc_records;
7115
7116         return OPJ_TRUE;
7117 }
7118
7119
7120 /**
7121  * Writes the MCO marker (Multiple component transformation ordering)
7122  *
7123  * @param       p_stream                                the stream to write data to.
7124  * @param       p_j2k                           J2K codec.
7125  * @param       p_manager               the user event manager.
7126 */
7127 opj_bool j2k_write_mco( opj_j2k_v2_t *p_j2k,
7128                                                 struct opj_stream_private *p_stream,
7129                                                 struct opj_event_mgr * p_manager
7130                                   )
7131 {
7132         OPJ_BYTE * l_current_data = 00;
7133         OPJ_UINT32 l_mco_size;
7134         opj_tcp_v2_t * l_tcp = 00;
7135         opj_simple_mcc_decorrelation_data_t * l_mcc_record;
7136         OPJ_UINT32 i;
7137
7138         /* preconditions */
7139         assert(p_j2k != 00);
7140         assert(p_manager != 00);
7141         assert(p_stream != 00);
7142
7143         l_tcp =&(p_j2k->m_cp.tcps[p_j2k->m_current_tile_number]);
7144         l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
7145
7146         l_mco_size = 5 + l_tcp->m_nb_mcc_records;
7147         if (l_mco_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
7148
7149                 p_j2k->m_specific_param.m_encoder.m_header_tile_data
7150                         = (OPJ_BYTE*)opj_realloc(
7151                                 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
7152                                 l_mco_size);
7153                 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data)
7154                 {
7155                         return OPJ_FALSE;
7156                 }
7157
7158                 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_mco_size;
7159         }
7160
7161         opj_write_bytes(l_current_data,J2K_MS_MCO,2);                   /* MCO */
7162         l_current_data += 2;
7163
7164         opj_write_bytes(l_current_data,l_mco_size-2,2);                                 /* Lmco */
7165         l_current_data += 2;
7166
7167         opj_write_bytes(l_current_data,l_tcp->m_nb_mcc_records,1);                                      /* Nmco : only one tranform stage*/
7168         ++l_current_data;
7169
7170         l_mcc_record = l_tcp->m_mcc_records;
7171         for     (i=0;i<l_tcp->m_nb_mcc_records;++i) {
7172                 opj_write_bytes(l_current_data,l_mcc_record->m_index,1);                                        /* Imco -> use the mcc indicated by 1*/
7173                 ++l_current_data;
7174
7175                 ++l_mcc_record;
7176         }
7177
7178         if (opj_stream_write_data(p_stream,p_j2k->m_specific_param.m_encoder.m_header_tile_data,l_mco_size,p_manager) != l_mco_size) {
7179                 return OPJ_FALSE;
7180         }
7181
7182         return OPJ_TRUE;
7183 }
7184
7185 /**
7186  * Reads a MCO marker (Multiple Component Transform Ordering)
7187  *
7188  * @param       p_header_data   the data contained in the MCO box.
7189  * @param       p_j2k                   the jpeg2000 codec.
7190  * @param       p_header_size   the size of the data contained in the MCO marker.
7191  * @param       p_manager               the user event manager.
7192 */
7193 opj_bool j2k_read_mco ( opj_j2k_v2_t *p_j2k,
7194                                                 OPJ_BYTE * p_header_data,
7195                                                 OPJ_UINT32 p_header_size,
7196                                                 struct opj_event_mgr * p_manager )
7197 {
7198         OPJ_UINT32 l_tmp, i;
7199         OPJ_UINT32 l_nb_stages;
7200         opj_tcp_v2_t * l_tcp;
7201         opj_tccp_t * l_tccp;
7202         opj_image_t * l_image;
7203         opj_image_comp_t * l_img_comp;
7204
7205         /* preconditions */
7206         assert(p_header_data != 00);
7207         assert(p_j2k != 00);
7208         assert(p_manager != 00);
7209
7210         l_image = p_j2k->m_private_image;
7211         l_tcp = p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH ?
7212                         &p_j2k->m_cp.tcps[p_j2k->m_current_tile_number] :
7213                         p_j2k->m_specific_param.m_decoder.m_default_tcp;
7214
7215         if (p_header_size < 1) {
7216                 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCO marker\n");
7217                 return OPJ_FALSE;
7218         }
7219
7220         opj_read_bytes(p_header_data,&l_nb_stages,1);                           /* Nmco : only one tranform stage*/
7221         ++p_header_data;
7222
7223         if (l_nb_stages > 1) {
7224                 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge multiple transformation stages.\n");
7225                 return OPJ_TRUE;
7226         }
7227
7228         if (p_header_size != l_nb_stages + 1) {
7229                 opj_event_msg_v2(p_manager, EVT_WARNING, "Error reading MCO marker\n");
7230                 return OPJ_FALSE;
7231         }
7232
7233         l_tccp = l_tcp->tccps;
7234         l_img_comp = l_image->comps;
7235
7236         for (i=0;i<l_image->numcomps;++i) {
7237                 l_tccp->m_dc_level_shift = 0;
7238                 ++l_tccp;
7239         }
7240
7241         if (l_tcp->m_mct_decoding_matrix) {
7242                 opj_free(l_tcp->m_mct_decoding_matrix);
7243                 l_tcp->m_mct_decoding_matrix = 00;
7244         }
7245
7246         for (i=0;i<l_nb_stages;++i) {
7247                 opj_read_bytes(p_header_data,&l_tmp,1);
7248                 ++p_header_data;
7249
7250                 if (! j2k_add_mct(l_tcp,p_j2k->m_private_image,l_tmp)) {
7251                         return OPJ_FALSE;
7252                 }
7253         }
7254
7255         return OPJ_TRUE;
7256 }
7257
7258 opj_bool j2k_add_mct(opj_tcp_v2_t * p_tcp, opj_image_t * p_image, OPJ_UINT32 p_index)
7259 {
7260         OPJ_UINT32 i;
7261         opj_simple_mcc_decorrelation_data_t * l_mcc_record;
7262         opj_mct_data_t * l_deco_array, * l_offset_array;
7263         OPJ_UINT32 l_data_size,l_mct_size, l_offset_size;
7264         OPJ_UINT32 l_nb_elem;
7265         OPJ_UINT32 * l_offset_data, * l_current_offset_data;
7266         opj_tccp_t * l_tccp;
7267
7268         /* preconditions */
7269         assert(p_tcp != 00);
7270
7271         l_mcc_record = p_tcp->m_mcc_records;
7272
7273         for (i=0;i<p_tcp->m_nb_mcc_records;++i) {
7274                 if (l_mcc_record->m_index == p_index) {
7275                         break;
7276                 }
7277         }
7278
7279         if (i==p_tcp->m_nb_mcc_records) {
7280                 /** element discarded **/
7281                 return OPJ_TRUE;
7282         }
7283
7284         if (l_mcc_record->m_nb_comps != p_image->numcomps) {
7285                 /** do not support number of comps != image */
7286                 return OPJ_TRUE;
7287         }
7288
7289         l_deco_array = l_mcc_record->m_decorrelation_array;
7290
7291         if (l_deco_array) {
7292                 l_data_size = MCT_ELEMENT_SIZE[l_deco_array->m_element_type] * p_image->numcomps * p_image->numcomps;
7293                 if (l_deco_array->m_data_size != l_data_size) {
7294                         return OPJ_FALSE;
7295                 }
7296
7297                 l_nb_elem = p_image->numcomps * p_image->numcomps;
7298                 l_mct_size = l_nb_elem * sizeof(OPJ_FLOAT32);
7299                 p_tcp->m_mct_decoding_matrix = (OPJ_FLOAT32*)opj_malloc(l_mct_size);
7300
7301                 if (! p_tcp->m_mct_decoding_matrix ) {
7302                         return OPJ_FALSE;
7303                 }
7304
7305                 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);
7306         }
7307
7308         l_offset_array = l_mcc_record->m_offset_array;
7309
7310         if (l_offset_array) {
7311                 l_data_size = MCT_ELEMENT_SIZE[l_offset_array->m_element_type] * p_image->numcomps;
7312                 if (l_offset_array->m_data_size != l_data_size) {
7313                         return OPJ_FALSE;
7314                 }
7315
7316                 l_nb_elem = p_image->numcomps;
7317                 l_offset_size = l_nb_elem * sizeof(OPJ_UINT32);
7318                 l_offset_data = (OPJ_UINT32*)opj_malloc(l_offset_size);
7319
7320                 if (! l_offset_data ) {
7321                         return OPJ_FALSE;
7322                 }
7323
7324                 j2k_mct_read_functions_to_int32[l_offset_array->m_element_type](l_offset_array->m_data,l_offset_data,l_nb_elem);
7325
7326                 l_tccp = p_tcp->tccps;
7327                 l_current_offset_data = l_offset_data;
7328
7329                 for (i=0;i<p_image->numcomps;++i) {
7330                         l_tccp->m_dc_level_shift = *(l_current_offset_data++);
7331                         ++l_tccp;
7332                 }
7333
7334                 opj_free(l_offset_data);
7335         }
7336
7337         return OPJ_TRUE;
7338 }
7339
7340 /**
7341  * Writes the CBD marker (Component bit depth definition)
7342  *
7343  * @param       p_stream                                the stream to write data to.
7344  * @param       p_j2k                           J2K codec.
7345  * @param       p_manager               the user event manager.
7346 */
7347 opj_bool j2k_write_cbd( opj_j2k_v2_t *p_j2k,
7348                                                 struct opj_stream_private *p_stream,
7349                                                 struct opj_event_mgr * p_manager )
7350 {
7351         OPJ_UINT32 i;
7352         OPJ_UINT32 l_cbd_size;
7353         OPJ_BYTE * l_current_data = 00;
7354         opj_image_t *l_image = 00;
7355         opj_image_comp_t * l_comp = 00;
7356
7357         /* preconditions */
7358         assert(p_j2k != 00);
7359         assert(p_manager != 00);
7360         assert(p_stream != 00);
7361
7362         l_image = p_j2k->m_private_image;
7363         l_cbd_size = 6 + p_j2k->m_private_image->numcomps;
7364
7365         if (l_cbd_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
7366                 p_j2k->m_specific_param.m_encoder.m_header_tile_data
7367                         = (OPJ_BYTE*)opj_realloc(
7368                                 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
7369                                 l_cbd_size);
7370
7371                 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
7372                         return OPJ_FALSE;
7373                 }
7374
7375                 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_cbd_size;
7376         }
7377
7378         l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
7379
7380         opj_write_bytes(l_current_data,J2K_MS_CBD,2);                                   /* CBD */
7381         l_current_data += 2;
7382
7383         opj_write_bytes(l_current_data,l_cbd_size-2,2);                                 /* L_CBD */
7384         l_current_data += 2;
7385
7386         opj_write_bytes(l_current_data,l_image->numcomps, 2);           /* Ncbd */
7387         l_current_data+=2;
7388
7389         l_comp = l_image->comps;
7390
7391         for (i=0;i<l_image->numcomps;++i) {
7392                 opj_write_bytes(l_current_data, (l_comp->sgnd << 7) | (l_comp->prec - 1), 1);           /* Component bit depth */
7393                 ++l_current_data;
7394
7395                 ++l_comp;
7396         }
7397
7398         if (opj_stream_write_data(p_stream,p_j2k->m_specific_param.m_encoder.m_header_tile_data,l_cbd_size,p_manager) != l_cbd_size) {
7399                 return OPJ_FALSE;
7400         }
7401
7402         return OPJ_TRUE;
7403 }
7404
7405 /**
7406  * Reads a CBD marker (Component bit depth definition)
7407  * @param       p_header_data   the data contained in the CBD box.
7408  * @param       p_j2k                   the jpeg2000 codec.
7409  * @param       p_header_size   the size of the data contained in the CBD marker.
7410  * @param       p_manager               the user event manager.
7411 */
7412 opj_bool j2k_read_cbd ( opj_j2k_v2_t *p_j2k,
7413                                                 OPJ_BYTE * p_header_data,
7414                                                 OPJ_UINT32 p_header_size,
7415                                                 struct opj_event_mgr * p_manager)
7416 {
7417         OPJ_UINT32 l_nb_comp,l_num_comp;
7418         OPJ_UINT32 l_comp_def;
7419         OPJ_UINT32 i;
7420         opj_image_comp_t * l_comp = 00;
7421
7422         /* preconditions */
7423         assert(p_header_data != 00);
7424         assert(p_j2k != 00);
7425         assert(p_manager != 00);
7426
7427         l_num_comp = p_j2k->m_private_image->numcomps;
7428
7429         if (p_header_size != (p_j2k->m_private_image->numcomps + 2)) {
7430                 opj_event_msg_v2(p_manager, EVT_ERROR, "Crror reading CBD marker\n");
7431                 return OPJ_FALSE;
7432         }
7433
7434         opj_read_bytes(p_header_data,&l_nb_comp,2);                             /* Ncbd */
7435         p_header_data+=2;
7436
7437         if (l_nb_comp != l_num_comp) {
7438                 opj_event_msg_v2(p_manager, EVT_ERROR, "Crror reading CBD marker\n");
7439                 return OPJ_FALSE;
7440         }
7441
7442         l_comp = p_j2k->m_private_image->comps;
7443         for (i=0;i<l_num_comp;++i) {
7444                 opj_read_bytes(p_header_data,&l_comp_def,1);                    /* Component bit depth */
7445                 ++p_header_data;
7446         l_comp->sgnd = (l_comp_def>>7) & 1;
7447                 l_comp->prec = (l_comp_def&0x7f) + 1;
7448                 ++l_comp;
7449         }
7450
7451         return OPJ_TRUE;
7452 }
7453
7454
7455 /* ----------------------------------------------------------------------- */
7456 /* J2K / JPT decoder interface                                             */
7457 /* ----------------------------------------------------------------------- */
7458
7459
7460
7461 void j2k_destroy_decompress(opj_j2k_t *j2k) {
7462         int i = 0;
7463
7464         if(j2k->tile_len != NULL) {
7465                 opj_free(j2k->tile_len);
7466         }
7467         if(j2k->tile_data != NULL) {
7468                 opj_free(j2k->tile_data);
7469         }
7470         if(j2k->default_tcp != NULL) {
7471                 opj_tcp_t *default_tcp = j2k->default_tcp;
7472                 if(default_tcp->ppt_data_first != NULL) {
7473                         opj_free(default_tcp->ppt_data_first);
7474                 }
7475                 if(j2k->default_tcp->tccps != NULL) {
7476                         opj_free(j2k->default_tcp->tccps);
7477                 }
7478                 opj_free(j2k->default_tcp);
7479         }
7480         if(j2k->cp != NULL) {
7481                 opj_cp_t *cp = j2k->cp;
7482                 if(cp->tcps != NULL) {
7483                         for(i = 0; i < cp->tw * cp->th; i++) {
7484                                 if(cp->tcps[i].ppt_data_first != NULL) {
7485                                         opj_free(cp->tcps[i].ppt_data_first);
7486                                 }
7487                                 if(cp->tcps[i].tccps != NULL) {
7488                                         opj_free(cp->tcps[i].tccps);
7489                                 }
7490                         }
7491                         opj_free(cp->tcps);
7492                 }
7493                 if(cp->ppm_data_first != NULL) {
7494                         opj_free(cp->ppm_data_first);
7495                 }
7496                 if(cp->tileno != NULL) {
7497                         opj_free(cp->tileno);  
7498                 }
7499                 if(cp->comment != NULL) {
7500                         opj_free(cp->comment);
7501                 }
7502
7503                 opj_free(cp);
7504         }
7505         opj_free(j2k);
7506 }
7507
7508
7509 void opj_j2k_setup_decoder(opj_j2k_v2_t *j2k, opj_dparameters_t *parameters)
7510 {
7511         if(j2k && parameters) {
7512                 j2k->m_cp.m_specific_param.m_dec.m_layer = parameters->cp_layer;
7513                 j2k->m_cp.m_specific_param.m_dec.m_reduce = parameters->cp_reduce;
7514
7515 #ifdef USE_JPWL
7516                 j2k->m_cp.correct = parameters->jpwl_correct;
7517                 j2k->m_cp.exp_comps = parameters->jpwl_exp_comps;
7518                 j2k->m_cp.max_tiles = parameters->jpwl_max_tiles;
7519 #endif /* USE_JPWL */
7520         }
7521 }
7522
7523
7524
7525 /*
7526 * Read a JPT-stream and decode file
7527 *
7528 */
7529 opj_image_t* j2k_decode_jpt_stream(opj_j2k_t *j2k, opj_cio_t *cio,  opj_codestream_info_t *cstr_info) {
7530         opj_image_t *image = NULL;
7531         opj_jpt_msg_header_t header;
7532         int position;
7533         opj_common_ptr cinfo = j2k->cinfo;
7534
7535         OPJ_ARG_NOT_USED(cstr_info);
7536
7537         j2k->cio = cio;
7538
7539         /* create an empty image */
7540         image = opj_image_create0();
7541         j2k->image = image;
7542
7543         j2k->state = J2K_STATE_MHSOC;
7544         
7545         /* Initialize the header */
7546         jpt_init_msg_header(&header);
7547         /* Read the first header of the message */
7548         jpt_read_msg_header(cinfo, cio, &header);
7549         
7550         position = cio_tell(cio);
7551         if (header.Class_Id != 6) {     /* 6 : Main header data-bin message */
7552                 opj_image_destroy(image);
7553                 opj_event_msg(cinfo, EVT_ERROR, "[JPT-stream] : Expecting Main header first [class_Id %d] !\n", header.Class_Id);
7554                 return 0;
7555         }
7556         
7557         for (;;) {
7558                 opj_dec_mstabent_t *e = NULL;
7559                 int id;
7560                 
7561                 if (!cio_numbytesleft(cio)) {
7562                         j2k_read_eoc(j2k);
7563                         return image;
7564                 }
7565                 /* data-bin read -> need to read a new header */
7566                 if ((unsigned int) (cio_tell(cio) - position) == header.Msg_length) {
7567                         jpt_read_msg_header(cinfo, cio, &header);
7568                         position = cio_tell(cio);
7569                         if (header.Class_Id != 4) {     /* 4 : Tile data-bin message */
7570                                 opj_image_destroy(image);
7571                                 opj_event_msg(cinfo, EVT_ERROR, "[JPT-stream] : Expecting Tile info !\n");
7572                                 return 0;
7573                         }
7574                 }
7575                 
7576                 id = cio_read(cio, 2);
7577                 if (id >> 8 != 0xff) {
7578                         opj_image_destroy(image);
7579                         opj_event_msg(cinfo, EVT_ERROR, "%.8x: expected a marker instead of %x\n", cio_tell(cio) - 2, id);
7580                         return 0;
7581                 }
7582                 e = j2k_dec_mstab_lookup(id);
7583                 if (!(j2k->state & e->states)) {
7584                         opj_image_destroy(image);
7585                         opj_event_msg(cinfo, EVT_ERROR, "%.8x: unexpected marker %x\n", cio_tell(cio) - 2, id);
7586                         return 0;
7587                 }
7588                 if (e->handler) {
7589                         (*e->handler)(j2k);
7590                 }
7591                 if (j2k->state == J2K_STATE_MT) {
7592                         break;
7593                 }
7594                 if (j2k->state == J2K_STATE_NEOC) {
7595                         break;
7596                 }
7597         }
7598         if (j2k->state == J2K_STATE_NEOC) {
7599                 j2k_read_eoc(j2k);
7600         }
7601         
7602         if (j2k->state != J2K_STATE_MT) {
7603                 opj_event_msg(cinfo, EVT_WARNING, "Incomplete bitstream\n");
7604         }
7605
7606         return image;
7607 }
7608
7609 /* ----------------------------------------------------------------------- */
7610 /* J2K encoder interface                                                       */
7611 /* ----------------------------------------------------------------------- */
7612
7613 opj_j2k_t* j2k_create_compress(opj_common_ptr cinfo) {
7614         opj_j2k_t *j2k = (opj_j2k_t*) opj_calloc(1, sizeof(opj_j2k_t));
7615         if(j2k) {
7616                 j2k->cinfo = cinfo;
7617         }
7618         return j2k;
7619 }
7620
7621 opj_j2k_v2_t* j2k_create_compress_v2(void)
7622 {
7623         opj_j2k_v2_t *l_j2k = (opj_j2k_v2_t*) opj_malloc(sizeof(opj_j2k_v2_t));
7624         if (!l_j2k) {
7625                 return NULL;
7626         }
7627
7628         memset(l_j2k,0,sizeof(opj_j2k_v2_t));
7629
7630         l_j2k->m_is_decoder = 0;
7631         l_j2k->m_cp.m_is_decoder = 0;
7632
7633         l_j2k->m_specific_param.m_encoder.m_header_tile_data = (OPJ_BYTE *) opj_malloc(J2K_DEFAULT_HEADER_SIZE);
7634         if (! l_j2k->m_specific_param.m_encoder.m_header_tile_data) {
7635                 j2k_destroy(l_j2k);
7636                 return NULL;
7637         }
7638
7639         l_j2k->m_specific_param.m_encoder.m_header_tile_data_size = J2K_DEFAULT_HEADER_SIZE;
7640
7641         /* validation list creation*/
7642         l_j2k->m_validation_list = opj_procedure_list_create();
7643         if (! l_j2k->m_validation_list) {
7644                 j2k_destroy(l_j2k);
7645                 return NULL;
7646         }
7647
7648         /* execution list creation*/
7649         l_j2k->m_procedure_list = opj_procedure_list_create();
7650         if (! l_j2k->m_procedure_list) {
7651                 j2k_destroy(l_j2k);
7652                 return NULL;
7653         }
7654
7655         return l_j2k;
7656 }
7657
7658 void j2k_destroy_compress(opj_j2k_t *j2k) {
7659         int tileno;
7660
7661         if(!j2k) return;
7662         if(j2k->cp != NULL) {
7663                 opj_cp_t *cp = j2k->cp;
7664
7665                 if(cp->comment) {
7666                         opj_free(cp->comment);
7667                 }
7668                 if(cp->matrice) {
7669                         opj_free(cp->matrice);
7670                 }
7671                 for (tileno = 0; tileno < cp->tw * cp->th; tileno++) {
7672                         opj_free(cp->tcps[tileno].tccps);
7673                 }
7674                 opj_free(cp->tcps);
7675                 opj_free(cp);
7676         }
7677
7678         opj_free(j2k);
7679 }
7680
7681 void j2k_setup_encoder(opj_j2k_t *j2k, opj_cparameters_t *parameters, opj_image_t *image) {
7682         OPJ_UINT32 i,j;
7683   int tileno, numpocs_tile;
7684         opj_cp_t *cp = NULL;
7685
7686         if(!j2k || !parameters || ! image) {
7687                 return;
7688         }
7689
7690         /* create and initialize the coding parameters structure */
7691         cp = (opj_cp_t*) opj_calloc(1, sizeof(opj_cp_t));
7692
7693         /* keep a link to cp so that we can destroy it later in j2k_destroy_compress */
7694         j2k->cp = cp;
7695
7696         /* set default values for cp */
7697         cp->tw = 1;
7698         cp->th = 1;
7699
7700         /* 
7701         copy user encoding parameters 
7702         */
7703         cp->cinema = parameters->cp_cinema;
7704         cp->max_comp_size =     parameters->max_comp_size;
7705         cp->rsiz   = parameters->cp_rsiz;
7706         cp->disto_alloc = parameters->cp_disto_alloc;
7707         cp->fixed_alloc = parameters->cp_fixed_alloc;
7708         cp->fixed_quality = parameters->cp_fixed_quality;
7709
7710         /* mod fixed_quality */
7711         if(parameters->cp_matrice) {
7712                 size_t array_size = parameters->tcp_numlayers * parameters->numresolution * 3 * sizeof(int);
7713                 cp->matrice = (int *) opj_malloc(array_size);
7714                 memcpy(cp->matrice, parameters->cp_matrice, array_size);
7715         }
7716
7717         /* tiles */
7718         cp->tdx = parameters->cp_tdx;
7719         cp->tdy = parameters->cp_tdy;
7720
7721         /* tile offset */
7722         cp->tx0 = parameters->cp_tx0;
7723         cp->ty0 = parameters->cp_ty0;
7724
7725         /* comment string */
7726         if(parameters->cp_comment) {
7727                 cp->comment = (char*)opj_malloc(strlen(parameters->cp_comment) + 1);
7728                 if(cp->comment) {
7729                         strcpy(cp->comment, parameters->cp_comment);
7730                 }
7731         }
7732
7733         /*
7734         calculate other encoding parameters
7735         */
7736
7737         if (parameters->tile_size_on) {
7738                 cp->tw = int_ceildiv(image->x1 - cp->tx0, cp->tdx);
7739                 cp->th = int_ceildiv(image->y1 - cp->ty0, cp->tdy);
7740         } else {
7741                 cp->tdx = image->x1 - cp->tx0;
7742                 cp->tdy = image->y1 - cp->ty0;
7743         }
7744
7745         if(parameters->tp_on){
7746                 cp->tp_flag = parameters->tp_flag;
7747                 cp->tp_on = 1;
7748         }
7749         
7750         cp->img_size = 0;
7751         for(i=0;i<image->numcomps ;i++){
7752         cp->img_size += (image->comps[i].w *image->comps[i].h * image->comps[i].prec);
7753         }
7754
7755
7756 #ifdef USE_JPWL
7757         /*
7758         calculate JPWL encoding parameters
7759         */
7760
7761         if (parameters->jpwl_epc_on) {
7762                 OPJ_UINT32 i;
7763
7764                 /* set JPWL on */
7765                 cp->epc_on = OPJ_TRUE;
7766                 cp->info_on = OPJ_FALSE; /* no informative technique */
7767
7768                 /* set EPB on */
7769                 if ((parameters->jpwl_hprot_MH > 0) || (parameters->jpwl_hprot_TPH[0] > 0)) {
7770                         cp->epb_on = OPJ_TRUE;
7771                         
7772                         cp->hprot_MH = parameters->jpwl_hprot_MH;
7773                         for (i = 0; i < JPWL_MAX_NO_TILESPECS; i++) {
7774                                 cp->hprot_TPH_tileno[i] = parameters->jpwl_hprot_TPH_tileno[i];
7775                                 cp->hprot_TPH[i] = parameters->jpwl_hprot_TPH[i];
7776                         }
7777                         /* if tile specs are not specified, copy MH specs */
7778                         if (cp->hprot_TPH[0] == -1) {
7779                                 cp->hprot_TPH_tileno[0] = 0;
7780                                 cp->hprot_TPH[0] = parameters->jpwl_hprot_MH;
7781                         }
7782                         for (i = 0; i < JPWL_MAX_NO_PACKSPECS; i++) {
7783                                 cp->pprot_tileno[i] = parameters->jpwl_pprot_tileno[i];
7784                                 cp->pprot_packno[i] = parameters->jpwl_pprot_packno[i];
7785                                 cp->pprot[i] = parameters->jpwl_pprot[i];
7786                         }
7787                 }
7788
7789                 /* set ESD writing */
7790                 if ((parameters->jpwl_sens_size == 1) || (parameters->jpwl_sens_size == 2)) {
7791                         cp->esd_on = OPJ_TRUE;
7792
7793                         cp->sens_size = parameters->jpwl_sens_size;
7794                         cp->sens_addr = parameters->jpwl_sens_addr;
7795                         cp->sens_range = parameters->jpwl_sens_range;
7796
7797                         cp->sens_MH = parameters->jpwl_sens_MH;
7798                         for (i = 0; i < JPWL_MAX_NO_TILESPECS; i++) {
7799                                 cp->sens_TPH_tileno[i] = parameters->jpwl_sens_TPH_tileno[i];
7800                                 cp->sens_TPH[i] = parameters->jpwl_sens_TPH[i];
7801                         }
7802                 }
7803
7804                 /* always set RED writing to false: we are at the encoder */
7805                 cp->red_on = OPJ_FALSE;
7806
7807         } else {
7808                 cp->epc_on = OPJ_FALSE;
7809         }
7810 #endif /* USE_JPWL */
7811
7812
7813         /* initialize the mutiple tiles */
7814         /* ---------------------------- */
7815         cp->tcps = (opj_tcp_t*) opj_calloc(cp->tw * cp->th, sizeof(opj_tcp_t));
7816
7817         for (tileno = 0; tileno < cp->tw * cp->th; tileno++) {
7818                 opj_tcp_t *tcp = &cp->tcps[tileno];
7819                 tcp->numlayers = parameters->tcp_numlayers;
7820                 assert ( tcp->numlayers >= 0 );
7821                 for (j = 0; j < (OPJ_UINT32)tcp->numlayers; j++) {
7822                         if(cp->cinema){
7823                                 if (cp->fixed_quality) {
7824                                         tcp->distoratio[j] = parameters->tcp_distoratio[j];
7825                                 }
7826                                 tcp->rates[j] = parameters->tcp_rates[j];
7827                         }else{
7828                                 if (cp->fixed_quality) {        /* add fixed_quality */
7829                                         tcp->distoratio[j] = parameters->tcp_distoratio[j];
7830                                 } else {
7831                                         tcp->rates[j] = parameters->tcp_rates[j];
7832                                 }
7833                         }
7834                 }
7835                 tcp->csty = parameters->csty;
7836                 tcp->prg = parameters->prog_order;
7837                 tcp->mct = parameters->tcp_mct; 
7838
7839                 numpocs_tile = 0;
7840                 tcp->POC = 0;
7841                 if (parameters->numpocs) {
7842                         /* initialisation of POC */
7843                         tcp->POC = 1;
7844       assert( parameters->numpocs >= 0 );
7845                         for (i = 0; i < (OPJ_UINT32)parameters->numpocs; i++) {
7846                                 if((tileno == parameters->POC[i].tile - 1) || (parameters->POC[i].tile == -1)) {
7847                                         opj_poc_t *tcp_poc = &tcp->pocs[numpocs_tile];
7848                                         tcp_poc->resno0         = parameters->POC[numpocs_tile].resno0;
7849                                         tcp_poc->compno0        = parameters->POC[numpocs_tile].compno0;
7850                                         tcp_poc->layno1         = parameters->POC[numpocs_tile].layno1;
7851                                         tcp_poc->resno1         = parameters->POC[numpocs_tile].resno1;
7852                                         tcp_poc->compno1        = parameters->POC[numpocs_tile].compno1;
7853                                         tcp_poc->prg1           = parameters->POC[numpocs_tile].prg1;
7854                                         tcp_poc->tile           = parameters->POC[numpocs_tile].tile;
7855                                         numpocs_tile++;
7856                                 }
7857                         }
7858                         tcp->numpocs = numpocs_tile -1 ;
7859                 }else{ 
7860                         tcp->numpocs = 0;
7861                 }
7862
7863                 tcp->tccps = (opj_tccp_t*) opj_calloc(image->numcomps, sizeof(opj_tccp_t));
7864
7865                 for (i = 0; i < image->numcomps; i++) {
7866                         opj_tccp_t *tccp = &tcp->tccps[i];
7867                         tccp->csty = parameters->csty & 0x01;   /* 0 => one precinct || 1 => custom precinct  */
7868                         tccp->numresolutions = parameters->numresolution;
7869                         tccp->cblkw = int_floorlog2(parameters->cblockw_init);
7870                         tccp->cblkh = int_floorlog2(parameters->cblockh_init);
7871                         tccp->cblksty = parameters->mode;
7872                         tccp->qmfbid = parameters->irreversible ? 0 : 1;
7873                         tccp->qntsty = parameters->irreversible ? J2K_CCP_QNTSTY_SEQNT : J2K_CCP_QNTSTY_NOQNT;
7874                         tccp->numgbits = 2;
7875                         assert(parameters->roi_compno >= 0);
7876                         if (i == (OPJ_UINT32)parameters->roi_compno) {
7877                                 tccp->roishift = parameters->roi_shift;
7878                         } else {
7879                                 tccp->roishift = 0;
7880                         }
7881
7882                         if(parameters->cp_cinema)
7883                         {
7884                                 /*Precinct size for lowest frequency subband=128*/
7885                                 tccp->prcw[0] = 7;
7886                                 tccp->prch[0] = 7;
7887                                 /*Precinct size at all other resolutions = 256*/
7888                                 for (j = 1; j < tccp->numresolutions; j++) {
7889                                         tccp->prcw[j] = 8;
7890                                         tccp->prch[j] = 8;
7891                                 }
7892                         }else{
7893                                 if (parameters->csty & J2K_CCP_CSTY_PRT) {
7894                                         int p = 0;
7895                                         assert(tccp->numresolutions > 0);
7896                                         for (j = (OPJ_UINT32)(tccp->numresolutions - 1); (int)j >= 0; j--) {
7897                                                 if (p < parameters->res_spec) {
7898                                                         
7899                                                         if (parameters->prcw_init[p] < 1) {
7900                                                                 tccp->prcw[j] = 1;
7901                                                         } else {
7902                                                                 tccp->prcw[j] = int_floorlog2(parameters->prcw_init[p]);
7903                                                         }
7904                                                         
7905                                                         if (parameters->prch_init[p] < 1) {
7906                                                                 tccp->prch[j] = 1;
7907                                                         }else {
7908                                                                 tccp->prch[j] = int_floorlog2(parameters->prch_init[p]);
7909                                                         }
7910
7911                                                 } else {
7912                                                         int res_spec = parameters->res_spec;
7913                                                         int size_prcw = parameters->prcw_init[res_spec - 1] >> (p - (res_spec - 1));
7914                                                         int size_prch = parameters->prch_init[res_spec - 1] >> (p - (res_spec - 1));
7915                                                         
7916                                                         if (size_prcw < 1) {
7917                                                                 tccp->prcw[j] = 1;
7918                                                         } else {
7919                                                                 tccp->prcw[j] = int_floorlog2(size_prcw);
7920                                                         }
7921                                                         
7922                                                         if (size_prch < 1) {
7923                                                                 tccp->prch[j] = 1;
7924                                                         } else {
7925                                                                 tccp->prch[j] = int_floorlog2(size_prch);
7926                                                         }
7927                                                 }
7928                                                 p++;
7929                                                 /*printf("\nsize precinct for level %d : %d,%d\n", j,tccp->prcw[j], tccp->prch[j]); */
7930                                         }       /*end for*/
7931                                 } else {
7932                                         for (j = 0; j < tccp->numresolutions; j++) {
7933                                                 tccp->prcw[j] = 15;
7934                                                 tccp->prch[j] = 15;
7935                                         }
7936                                 }
7937                         }
7938
7939                         dwt_calc_explicit_stepsizes(tccp, image->comps[i].prec);
7940                 }
7941         }
7942 }
7943
7944 void j2k_setup_encoder_v2(      opj_j2k_v2_t *p_j2k,
7945                                                         opj_cparameters_t *parameters,
7946                                                         opj_image_t *image,
7947                                                         struct opj_event_mgr * p_manager)
7948 {
7949         OPJ_UINT32 i, j, tileno, numpocs_tile;
7950         opj_cp_v2_t *cp = 00;
7951         opj_bool l_res;
7952
7953         if(!p_j2k || !parameters || ! image) {
7954                 return;
7955         }
7956
7957         /* keep a link to cp so that we can destroy it later in j2k_destroy_compress */
7958         cp = &(p_j2k->m_cp);
7959
7960         /* set default values for cp */
7961         cp->tw = 1;
7962         cp->th = 1;
7963
7964         /*
7965         copy user encoding parameters
7966         */
7967         cp->m_specific_param.m_enc.m_cinema = parameters->cp_cinema;
7968         cp->m_specific_param.m_enc.m_max_comp_size =    parameters->max_comp_size;
7969         cp->rsiz   = parameters->cp_rsiz;
7970         cp->m_specific_param.m_enc.m_disto_alloc = parameters->cp_disto_alloc;
7971         cp->m_specific_param.m_enc.m_fixed_alloc = parameters->cp_fixed_alloc;
7972         cp->m_specific_param.m_enc.m_fixed_quality = parameters->cp_fixed_quality;
7973
7974         /* mod fixed_quality */
7975         if (parameters->cp_matrice) {
7976                 size_t array_size = parameters->tcp_numlayers * parameters->numresolution * 3 * sizeof(OPJ_INT32);
7977                 cp->m_specific_param.m_enc.m_matrice = (OPJ_INT32 *) opj_malloc(array_size);
7978                 memcpy(cp->m_specific_param.m_enc.m_matrice, parameters->cp_matrice, array_size);
7979         }
7980
7981         /* tiles */
7982         cp->tdx = parameters->cp_tdx;
7983         cp->tdy = parameters->cp_tdy;
7984
7985         /* tile offset */
7986         cp->tx0 = parameters->cp_tx0;
7987         cp->ty0 = parameters->cp_ty0;
7988
7989         /* comment string */
7990         if(parameters->cp_comment) {
7991                 cp->comment = (char*)opj_malloc(strlen(parameters->cp_comment) + 1);
7992                 if(cp->comment) {
7993                         strcpy(cp->comment, parameters->cp_comment);
7994                 }
7995         }
7996
7997         /*
7998         calculate other encoding parameters
7999         */
8000
8001         if (parameters->tile_size_on) {
8002                 cp->tw = int_ceildiv(image->x1 - cp->tx0, cp->tdx);
8003                 cp->th = int_ceildiv(image->y1 - cp->ty0, cp->tdy);
8004         } else {
8005                 cp->tdx = image->x1 - cp->tx0;
8006                 cp->tdy = image->y1 - cp->ty0;
8007         }
8008
8009         if (parameters->tp_on) {
8010                 cp->m_specific_param.m_enc.m_tp_flag = parameters->tp_flag;
8011                 cp->m_specific_param.m_enc.m_tp_on = 1;
8012         }
8013
8014 #ifdef USE_JPWL
8015         /*
8016         calculate JPWL encoding parameters
8017         */
8018
8019         if (parameters->jpwl_epc_on) {
8020                 OPJ_INT32 i;
8021
8022                 /* set JPWL on */
8023                 cp->epc_on = OPJ_TRUE;
8024                 cp->info_on = OPJ_FALSE; /* no informative technique */
8025
8026                 /* set EPB on */
8027                 if ((parameters->jpwl_hprot_MH > 0) || (parameters->jpwl_hprot_TPH[0] > 0)) {
8028                         cp->epb_on = OPJ_TRUE;
8029
8030                         cp->hprot_MH = parameters->jpwl_hprot_MH;
8031                         for (i = 0; i < JPWL_MAX_NO_TILESPECS; i++) {
8032                                 cp->hprot_TPH_tileno[i] = parameters->jpwl_hprot_TPH_tileno[i];
8033                                 cp->hprot_TPH[i] = parameters->jpwl_hprot_TPH[i];
8034                         }
8035                         /* if tile specs are not specified, copy MH specs */
8036                         if (cp->hprot_TPH[0] == -1) {
8037                                 cp->hprot_TPH_tileno[0] = 0;
8038                                 cp->hprot_TPH[0] = parameters->jpwl_hprot_MH;
8039                         }
8040                         for (i = 0; i < JPWL_MAX_NO_PACKSPECS; i++) {
8041                                 cp->pprot_tileno[i] = parameters->jpwl_pprot_tileno[i];
8042                                 cp->pprot_packno[i] = parameters->jpwl_pprot_packno[i];
8043                                 cp->pprot[i] = parameters->jpwl_pprot[i];
8044                         }
8045                 }
8046
8047                 /* set ESD writing */
8048                 if ((parameters->jpwl_sens_size == 1) || (parameters->jpwl_sens_size == 2)) {
8049                         cp->esd_on = OPJ_TRUE;
8050
8051                         cp->sens_size = parameters->jpwl_sens_size;
8052                         cp->sens_addr = parameters->jpwl_sens_addr;
8053                         cp->sens_range = parameters->jpwl_sens_range;
8054
8055                         cp->sens_MH = parameters->jpwl_sens_MH;
8056                         for (i = 0; i < JPWL_MAX_NO_TILESPECS; i++) {
8057                                 cp->sens_TPH_tileno[i] = parameters->jpwl_sens_TPH_tileno[i];
8058                                 cp->sens_TPH[i] = parameters->jpwl_sens_TPH[i];
8059                         }
8060                 }
8061
8062                 /* always set RED writing to false: we are at the encoder */
8063                 cp->red_on = OPJ_FALSE;
8064
8065         } else {
8066                 cp->epc_on = OPJ_FALSE;
8067         }
8068 #endif /* USE_JPWL */
8069
8070
8071         /* initialize the mutiple tiles */
8072         /* ---------------------------- */
8073         cp->tcps = (opj_tcp_v2_t*) opj_calloc(cp->tw * cp->th, sizeof(opj_tcp_v2_t));
8074         if (parameters->numpocs) {
8075                 /* initialisation of POC */
8076                 l_res = j2k_check_poc_val(parameters->POC,parameters->numpocs, parameters->numresolution, image->numcomps, parameters->tcp_numlayers, p_manager);
8077                 // TODO
8078         }
8079
8080         for (tileno = 0; tileno < cp->tw * cp->th; tileno++) {
8081                 opj_tcp_v2_t *tcp = &cp->tcps[tileno];
8082                 tcp->numlayers = parameters->tcp_numlayers;
8083
8084                 for (j = 0; j < tcp->numlayers; j++) {
8085                         if(cp->m_specific_param.m_enc.m_cinema){
8086                                 if (cp->m_specific_param.m_enc.m_fixed_quality) {
8087                                         tcp->distoratio[j] = parameters->tcp_distoratio[j];
8088                                 }
8089                                 tcp->rates[j] = parameters->tcp_rates[j];
8090                         }else{
8091                                 if (cp->m_specific_param.m_enc.m_fixed_quality) {       /* add fixed_quality */
8092                                         tcp->distoratio[j] = parameters->tcp_distoratio[j];
8093                                 } else {
8094                                         tcp->rates[j] = parameters->tcp_rates[j];
8095                                 }
8096                         }
8097                 }
8098
8099                 tcp->csty = parameters->csty;
8100                 tcp->prg = parameters->prog_order;
8101                 tcp->mct = parameters->tcp_mct;
8102
8103                 numpocs_tile = 0;
8104                 tcp->POC = 0;
8105
8106                 if (parameters->numpocs) {
8107                         /* initialisation of POC */
8108                         tcp->POC = 1;
8109                         // TODO
8110                         for (i = 0; i < (unsigned int) parameters->numpocs; i++) {
8111                                 if((tileno == parameters->POC[i].tile - 1) || (parameters->POC[i].tile == -1)) {
8112                                         opj_poc_t *tcp_poc = &tcp->pocs[numpocs_tile];
8113
8114                                         tcp_poc->resno0         = parameters->POC[numpocs_tile].resno0;
8115                                         tcp_poc->compno0        = parameters->POC[numpocs_tile].compno0;
8116                                         tcp_poc->layno1         = parameters->POC[numpocs_tile].layno1;
8117                                         tcp_poc->resno1         = parameters->POC[numpocs_tile].resno1;
8118                                         tcp_poc->compno1        = parameters->POC[numpocs_tile].compno1;
8119                                         tcp_poc->prg1           = parameters->POC[numpocs_tile].prg1;
8120                                         tcp_poc->tile           = parameters->POC[numpocs_tile].tile;
8121
8122                                         numpocs_tile++;
8123                                 }
8124                         }
8125
8126                         tcp->numpocs = numpocs_tile -1 ;
8127                 }else{
8128                         tcp->numpocs = 0;
8129                 }
8130
8131                 tcp->tccps = (opj_tccp_t*) opj_calloc(image->numcomps, sizeof(opj_tccp_t));
8132
8133                 if (parameters->mct_data) {
8134
8135                         opj_event_msg_v2(p_manager, EVT_ERROR, "MCT not supported for now\n");
8136                         return;
8137
8138                         /* TODO MSD : merge v2 add invert.c or used a external lib ?
8139                         OPJ_UINT32 lMctSize = image->numcomps * image->numcomps * sizeof(OPJ_FLOAT32);
8140                         OPJ_FLOAT32 * lTmpBuf = (OPJ_FLOAT32*)opj_malloc(lMctSize);
8141                         OPJ_INT32 * l_dc_shift = (OPJ_INT32 *) ((OPJ_BYTE *) parameters->mct_data + lMctSize);
8142
8143                         tcp->mct = 2;
8144                         tcp->m_mct_coding_matrix = (OPJ_FLOAT32*)opj_malloc(lMctSize);
8145                         memcpy(tcp->m_mct_coding_matrix,parameters->mct_data,lMctSize);
8146                         memcpy(lTmpBuf,parameters->mct_data,lMctSize);
8147
8148                         tcp->m_mct_decoding_matrix = (OPJ_FLOAT32*)opj_malloc(lMctSize);
8149                         assert(opj_matrix_inversion_f(lTmpBuf,(tcp->m_mct_decoding_matrix),image->numcomps));
8150
8151                         tcp->mct_norms = (OPJ_FLOAT64*)
8152                                         opj_malloc(image->numcomps * sizeof(OPJ_FLOAT64));
8153
8154                         opj_calculate_norms(tcp->mct_norms,image->numcomps,tcp->m_mct_decoding_matrix);
8155                         opj_free(lTmpBuf);
8156
8157                         for (i = 0; i < image->numcomps; i++) {
8158                                 opj_tccp_t *tccp = &tcp->tccps[i];
8159                                 tccp->m_dc_level_shift = l_dc_shift[i];
8160                         }
8161
8162                         j2k_setup_mct_encoding(tcp,image);
8163                         */
8164                 }
8165                 else {
8166                         for (i = 0; i < image->numcomps; i++) {
8167                                 opj_tccp_t *tccp = &tcp->tccps[i];
8168                                 opj_image_comp_t * l_comp = &(image->comps[i]);
8169
8170                                 if (! l_comp->sgnd) {
8171                                         tccp->m_dc_level_shift = 1 << (l_comp->prec - 1);
8172                                 }
8173                         }
8174                 }
8175
8176                 for (i = 0; i < image->numcomps; i++) {
8177                         opj_tccp_t *tccp = &tcp->tccps[i];
8178
8179                         tccp->csty = parameters->csty & 0x01;   /* 0 => one precinct || 1 => custom precinct  */
8180                         tccp->numresolutions = parameters->numresolution;
8181                         tccp->cblkw = int_floorlog2(parameters->cblockw_init);
8182                         tccp->cblkh = int_floorlog2(parameters->cblockh_init);
8183                         tccp->cblksty = parameters->mode;
8184                         tccp->qmfbid = parameters->irreversible ? 0 : 1;
8185                         tccp->qntsty = parameters->irreversible ? J2K_CCP_QNTSTY_SEQNT : J2K_CCP_QNTSTY_NOQNT;
8186                         tccp->numgbits = 2;
8187
8188                         if (i == parameters->roi_compno) {
8189                                 tccp->roishift = parameters->roi_shift;
8190                         } else {
8191                                 tccp->roishift = 0;
8192                         }
8193
8194                         if(parameters->cp_cinema) {
8195                                 //Precinct size for lowest frequency subband=128
8196                                 tccp->prcw[0] = 7;
8197                                 tccp->prch[0] = 7;
8198                                 //Precinct size at all other resolutions = 256
8199                                 for (j = 1; j < tccp->numresolutions; j++) {
8200                                         tccp->prcw[j] = 8;
8201                                         tccp->prch[j] = 8;
8202                                 }
8203                         }else{
8204                                 if (parameters->csty & J2K_CCP_CSTY_PRT) {
8205                                         OPJ_INT32 p = 0, it_res;
8206                                         for (it_res = tccp->numresolutions - 1; it_res >= 0; it_res--) {
8207                                                 if (p < parameters->res_spec) {
8208
8209                                                         if (parameters->prcw_init[p] < 1) {
8210                                                                 tccp->prcw[it_res] = 1;
8211                                                         } else {
8212                                                                 tccp->prcw[it_res] = int_floorlog2(parameters->prcw_init[p]);
8213                                                         }
8214
8215                                                         if (parameters->prch_init[p] < 1) {
8216                                                                 tccp->prch[it_res] = 1;
8217                                                         }else {
8218                                                                 tccp->prch[it_res] = int_floorlog2(parameters->prch_init[p]);
8219                                                         }
8220
8221                                                 } else {
8222                                                         int res_spec = parameters->res_spec;
8223                                                         int size_prcw = parameters->prcw_init[res_spec - 1] >> (p - (res_spec - 1));
8224                                                         int size_prch = parameters->prch_init[res_spec - 1] >> (p - (res_spec - 1));
8225
8226                                                         if (size_prcw < 1) {
8227                                                                 tccp->prcw[it_res] = 1;
8228                                                         } else {
8229                                                                 tccp->prcw[it_res] = int_floorlog2(size_prcw);
8230                                                         }
8231
8232                                                         if (size_prch < 1) {
8233                                                                 tccp->prch[it_res] = 1;
8234                                                         } else {
8235                                                                 tccp->prch[it_res] = int_floorlog2(size_prch);
8236                                                         }
8237                                                 }
8238                                                 p++;
8239                                                 /*printf("\nsize precinct for level %d : %d,%d\n", it_res,tccp->prcw[it_res], tccp->prch[it_res]); */
8240                                         }       //end for
8241                                 } else {
8242                                         for (j = 0; j < tccp->numresolutions; j++) {
8243                                                 tccp->prcw[j] = 15;
8244                                                 tccp->prch[j] = 15;
8245                                         }
8246                                 }
8247                         }
8248
8249                         dwt_calc_explicit_stepsizes(tccp, image->comps[i].prec);
8250                 }
8251         }
8252
8253         if (parameters->mct_data) {
8254                 opj_free(parameters->mct_data);
8255                 parameters->mct_data = 00;
8256         }
8257 }
8258
8259
8260 opj_bool j2k_encode(opj_j2k_t *j2k, opj_cio_t *cio, opj_image_t *image, opj_codestream_info_t *cstr_info) {
8261         int tileno;
8262   OPJ_UINT32 compno;
8263         opj_cp_t *cp = NULL;
8264
8265         opj_tcd_t *tcd = NULL;  /* TCD component */
8266
8267         j2k->cio = cio; 
8268         j2k->image = image;
8269
8270         cp = j2k->cp;
8271
8272         /* INDEX >> */
8273         j2k->cstr_info = cstr_info;
8274         if (cstr_info) {
8275                 OPJ_UINT32 compno;
8276                 cstr_info->tile = (opj_tile_info_t *) opj_malloc(cp->tw * cp->th * sizeof(opj_tile_info_t));
8277                 cstr_info->image_w = image->x1 - image->x0;
8278                 cstr_info->image_h = image->y1 - image->y0;
8279                 cstr_info->prog = (&cp->tcps[0])->prg;
8280                 cstr_info->tw = cp->tw;
8281                 cstr_info->th = cp->th;
8282                 cstr_info->tile_x = cp->tdx;    /* new version parser */
8283                 cstr_info->tile_y = cp->tdy;    /* new version parser */
8284                 cstr_info->tile_Ox = cp->tx0;   /* new version parser */
8285                 cstr_info->tile_Oy = cp->ty0;   /* new version parser */
8286                 cstr_info->numcomps = image->numcomps;
8287                 cstr_info->numlayers = (&cp->tcps[0])->numlayers;
8288                 cstr_info->numdecompos = (int*) opj_malloc(image->numcomps * sizeof(int));
8289                 for (compno=0; compno < image->numcomps; compno++) {
8290                         cstr_info->numdecompos[compno] = (&cp->tcps[0])->tccps->numresolutions - 1;
8291                 }
8292                 cstr_info->D_max = 0.0;         /* ADD Marcela */
8293                 cstr_info->main_head_start = cio_tell(cio); /* position of SOC */
8294                 cstr_info->maxmarknum = 100;
8295                 cstr_info->marker = (opj_marker_info_t *) opj_malloc(cstr_info->maxmarknum * sizeof(opj_marker_info_t));
8296                 cstr_info->marknum = 0;
8297         }
8298         /* << INDEX */
8299
8300         j2k_write_soc(j2k);
8301         j2k_write_siz(j2k);
8302         j2k_write_cod(j2k);
8303         j2k_write_qcd(j2k);
8304
8305         if(cp->cinema){
8306                 for (compno = 1; compno < image->numcomps; compno++) {
8307                         j2k_write_coc(j2k, compno);
8308                         j2k_write_qcc(j2k, compno);
8309                 }
8310         }
8311
8312         for (compno = 0; compno < image->numcomps; compno++) {
8313                 opj_tcp_t *tcp = &cp->tcps[0];
8314                 if (tcp->tccps[compno].roishift)
8315                         j2k_write_rgn(j2k, compno, 0);
8316         }
8317         if (cp->comment != NULL) {
8318                 j2k_write_com(j2k);
8319         }
8320
8321         j2k->totnum_tp = j2k_calculate_tp(cp,image->numcomps,image,j2k);
8322         /* TLM Marker*/
8323         if(cp->cinema){
8324                 j2k_write_tlm(j2k);
8325                 if (cp->cinema == CINEMA4K_24) {
8326                         j2k_write_poc(j2k);
8327                 }
8328         }
8329
8330         /* uncomment only for testing JPSEC marker writing */
8331         /* j2k_write_sec(j2k); */
8332
8333         /* INDEX >> */
8334         if(cstr_info) {
8335                 cstr_info->main_head_end = cio_tell(cio) - 1;
8336         }
8337         /* << INDEX */
8338         /**** Main Header ENDS here ***/
8339
8340         /* create the tile encoder */
8341         tcd = tcd_create(j2k->cinfo);
8342
8343         /* encode each tile */
8344         for (tileno = 0; tileno < cp->tw * cp->th; tileno++) {
8345                 int pino;
8346                 int tilepartno=0;
8347                 /* UniPG>> */
8348                 int acc_pack_num = 0;
8349                 /* <<UniPG */
8350
8351
8352                 opj_tcp_t *tcp = &cp->tcps[tileno];
8353                 opj_event_msg(j2k->cinfo, EVT_INFO, "tile number %d / %d\n", tileno + 1, cp->tw * cp->th);
8354
8355                 j2k->curtileno = tileno;
8356                 j2k->cur_tp_num = 0;
8357                 tcd->cur_totnum_tp = j2k->cur_totnum_tp[j2k->curtileno];
8358                 /* initialisation before tile encoding  */
8359                 if (tileno == 0) {
8360                         tcd_malloc_encode(tcd, image, cp, j2k->curtileno);
8361                 } else {
8362                         tcd_init_encode(tcd, image, cp, j2k->curtileno);
8363                 }
8364
8365                 /* INDEX >> */
8366                 if(cstr_info) {
8367                         cstr_info->tile[j2k->curtileno].start_pos = cio_tell(cio) + j2k->pos_correction;
8368                         cstr_info->tile[j2k->curtileno].maxmarknum = 10;
8369                         cstr_info->tile[j2k->curtileno].marker = (opj_marker_info_t *) opj_malloc(cstr_info->tile[j2k->curtileno].maxmarknum * sizeof(opj_marker_info_t));
8370                         cstr_info->tile[j2k->curtileno].marknum = 0;
8371                 }
8372                 /* << INDEX */
8373
8374                 for(pino = 0; pino <= tcp->numpocs; pino++) {
8375                         int tot_num_tp;
8376                         tcd->cur_pino=pino;
8377
8378                         /*Get number of tile parts*/
8379                         tot_num_tp = j2k_get_num_tp(cp,pino,tileno);
8380                         tcd->tp_pos = cp->tp_pos;
8381
8382                         for(tilepartno = 0; tilepartno < tot_num_tp ; tilepartno++){
8383                                 j2k->tp_num = tilepartno;
8384                                 /* INDEX >> */
8385                                 if(cstr_info)
8386                                         cstr_info->tile[j2k->curtileno].tp[j2k->cur_tp_num].tp_start_pos =
8387                                         cio_tell(cio) + j2k->pos_correction;
8388                                 /* << INDEX */
8389                                 j2k_write_sot(j2k);
8390
8391                                 if(j2k->cur_tp_num == 0 && cp->cinema == 0){
8392                                         for (compno = 1; compno < image->numcomps; compno++) {
8393                                                 j2k_write_coc(j2k, compno);
8394                                                 j2k_write_qcc(j2k, compno);
8395                                         }
8396                                         if (cp->tcps[tileno].numpocs) {
8397                                                 j2k_write_poc(j2k);
8398                                         }
8399                                 }
8400
8401                                 /* INDEX >> */
8402                                 if(cstr_info)
8403                                         cstr_info->tile[j2k->curtileno].tp[j2k->cur_tp_num].tp_end_header =
8404                                         cio_tell(cio) + j2k->pos_correction + 1;
8405                                 /* << INDEX */
8406
8407                                 j2k_write_sod(j2k, tcd);
8408
8409                                 /* INDEX >> */
8410                                 if(cstr_info) {
8411                                         cstr_info->tile[j2k->curtileno].tp[j2k->cur_tp_num].tp_end_pos =
8412                                                 cio_tell(cio) + j2k->pos_correction - 1;
8413                                         cstr_info->tile[j2k->curtileno].tp[j2k->cur_tp_num].tp_start_pack =
8414                                                 acc_pack_num;
8415                                         cstr_info->tile[j2k->curtileno].tp[j2k->cur_tp_num].tp_numpacks =
8416                                                 cstr_info->packno - acc_pack_num;
8417                                         acc_pack_num = cstr_info->packno;
8418                                 }
8419                                 /* << INDEX */
8420
8421                                 j2k->cur_tp_num++;
8422                         }                       
8423                 }
8424                 if(cstr_info) {
8425                         cstr_info->tile[j2k->curtileno].end_pos = cio_tell(cio) + j2k->pos_correction - 1;
8426                 }
8427
8428
8429                 /*
8430                 if (tile->PPT) { // BAD PPT !!! 
8431                 FILE *PPT_file;
8432                 int i;
8433                 PPT_file=fopen("PPT","rb");
8434                 fprintf(stderr,"%c%c%c%c",255,97,tile->len_ppt/256,tile->len_ppt%256);
8435                 for (i=0;i<tile->len_ppt;i++) {
8436                 unsigned char elmt;
8437                 fread(&elmt, 1, 1, PPT_file);
8438                 fwrite(&elmt,1,1,f);
8439                 }
8440                 fclose(PPT_file);
8441                 unlink("PPT");
8442                 }
8443                 */
8444
8445         }
8446
8447         /* destroy the tile encoder */
8448         tcd_free_encode(tcd);
8449         tcd_destroy(tcd);
8450
8451         opj_free(j2k->cur_totnum_tp);
8452
8453         j2k_write_eoc(j2k);
8454
8455         if(cstr_info) {
8456                 cstr_info->codestream_size = cio_tell(cio) + j2k->pos_correction;
8457                 /* UniPG>> */
8458                 /* The following adjustment is done to adjust the codestream size */
8459                 /* if SOD is not at 0 in the buffer. Useful in case of JP2, where */
8460                 /* the first bunch of bytes is not in the codestream              */
8461                 cstr_info->codestream_size -= cstr_info->main_head_start;
8462                 /* <<UniPG */
8463         }
8464
8465 #ifdef USE_JPWL
8466         /*
8467         preparation of JPWL marker segments
8468         */
8469         if(cp->epc_on) {
8470
8471                 /* encode according to JPWL */
8472                 jpwl_encode(j2k, cio, image);
8473
8474         }
8475 #endif /* USE_JPWL */
8476
8477         return OPJ_TRUE;
8478 }
8479
8480 static void j2k_add_mhmarker(opj_codestream_info_t *cstr_info, unsigned short int type, int pos, int len)
8481 {
8482         assert(cstr_info != 00);
8483
8484         /* expand the list? */
8485         if ((cstr_info->marknum + 1) > cstr_info->maxmarknum) {
8486                 cstr_info->maxmarknum = 100 + (int) ((float) cstr_info->maxmarknum * 1.0F);
8487                 cstr_info->marker = (opj_marker_info_t*)opj_realloc(cstr_info->marker, cstr_info->maxmarknum);
8488         }
8489
8490         /* add the marker */
8491         cstr_info->marker[cstr_info->marknum].type = type;
8492         cstr_info->marker[cstr_info->marknum].pos = pos;
8493         cstr_info->marker[cstr_info->marknum].len = len;
8494         cstr_info->marknum++;
8495
8496 }
8497
8498 static void j2k_add_mhmarker_v2(opj_codestream_index_t *cstr_index, OPJ_UINT32 type, OPJ_OFF_T pos, OPJ_UINT32 len)
8499 {
8500         assert(cstr_index != 00);
8501
8502         /* expand the list? */
8503         if ((cstr_index->marknum + 1) > cstr_index->maxmarknum) {
8504                 cstr_index->maxmarknum = 100 + (int) ((float) cstr_index->maxmarknum * 1.0F);
8505                 cstr_index->marker = (opj_marker_info_t*)opj_realloc(cstr_index->marker, cstr_index->maxmarknum *sizeof(opj_marker_info_t));
8506         }
8507
8508         /* add the marker */
8509         cstr_index->marker[cstr_index->marknum].type = (OPJ_UINT16)type;
8510         cstr_index->marker[cstr_index->marknum].pos = (OPJ_INT32)pos;
8511         cstr_index->marker[cstr_index->marknum].len = (OPJ_INT32)len;
8512         cstr_index->marknum++;
8513
8514 }
8515
8516 static void j2k_add_tlmarker( int tileno, opj_codestream_info_t *cstr_info, unsigned short int type, int pos, int len)
8517 {
8518         opj_marker_info_t *marker;
8519
8520         assert(cstr_info != 00);
8521
8522         /* expand the list? */
8523         if ((cstr_info->tile[tileno].marknum + 1) > cstr_info->tile[tileno].maxmarknum) {
8524                 cstr_info->tile[tileno].maxmarknum = 100 + (int) ((float) cstr_info->tile[tileno].maxmarknum * 1.0F);
8525                 cstr_info->tile[tileno].marker = (opj_marker_info_t*)opj_realloc(cstr_info->tile[tileno].marker, cstr_info->maxmarknum);
8526         }
8527
8528         marker = &(cstr_info->tile[tileno].marker[cstr_info->tile[tileno].marknum]);
8529
8530         /* add the marker */
8531         marker->type = type;
8532         marker->pos = pos;
8533         marker->len = len;
8534         cstr_info->tile[tileno].marknum++;
8535 }
8536
8537 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)
8538 {
8539         assert(cstr_index != 00);
8540         assert(cstr_index->tile_index != 00);
8541
8542         /* expand the list? */
8543         if ((cstr_index->tile_index[tileno].marknum + 1) > cstr_index->tile_index[tileno].maxmarknum) {
8544                 cstr_index->tile_index[tileno].maxmarknum = 100 + (int) ((float) cstr_index->tile_index[tileno].maxmarknum * 1.0F);
8545                 cstr_index->tile_index[tileno].marker =
8546                                 (opj_marker_info_t*)opj_realloc(cstr_index->tile_index[tileno].marker,
8547                                                                                                 cstr_index->tile_index[tileno].maxmarknum *sizeof(opj_marker_info_t));
8548         }
8549
8550         /* add the marker */
8551         cstr_index->tile_index[tileno].marker[cstr_index->tile_index[tileno].marknum].type = (OPJ_UINT16)type;
8552         cstr_index->tile_index[tileno].marker[cstr_index->tile_index[tileno].marknum].pos = (OPJ_INT32)pos;
8553         cstr_index->tile_index[tileno].marker[cstr_index->tile_index[tileno].marknum].len = (OPJ_INT32)len;
8554         cstr_index->tile_index[tileno].marknum++;
8555
8556         if (type == J2K_MS_SOT) {
8557                 OPJ_UINT32 l_current_tile_part = cstr_index->tile_index[tileno].current_tpsno;
8558
8559                 if (cstr_index->tile_index[tileno].tp_index)
8560                         cstr_index->tile_index[tileno].tp_index[l_current_tile_part].start_pos = pos;
8561
8562         }
8563 }
8564
8565
8566 /*
8567  * -----------------------------------------------------------------------
8568  * -----------------------------------------------------------------------
8569  * -----------------------------------------------------------------------
8570  */
8571
8572 /**
8573  * Ends the decompression procedures and possibiliy add data to be read after the
8574  * codestream.
8575  */
8576 opj_bool j2k_end_decompress(
8577                                                 opj_j2k_v2_t *p_j2k,
8578                                                 opj_stream_private_t *p_stream,
8579                                                 opj_event_mgr_t * p_manager)
8580 {
8581   (void)p_j2k;
8582   (void)p_stream;
8583   (void)p_manager;
8584         return OPJ_TRUE;
8585 }
8586
8587 /**
8588  * Reads a jpeg2000 codestream header structure.
8589
8590  *
8591  * @param p_stream the stream to read data from.
8592  * @param p_j2k the jpeg2000 codec.
8593  * @param p_manager the user event manager.
8594  *
8595  * @return true if the box is valid.
8596  */
8597 opj_bool j2k_read_header(       struct opj_stream_private *p_stream,
8598                                                         opj_j2k_v2_t* p_j2k,
8599                                                         opj_image_t** p_image,
8600                                                         struct opj_event_mgr* p_manager )
8601 {
8602         /* preconditions */
8603         assert(p_j2k != 00);
8604         assert(p_stream != 00);
8605         assert(p_manager != 00);
8606
8607         /* create an empty image header */
8608         p_j2k->m_private_image = opj_image_create0();
8609         if (! p_j2k->m_private_image) {
8610                 return OPJ_FALSE;
8611         }
8612
8613         /* customization of the validation */
8614         j2k_setup_decoding_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                 opj_image_destroy(p_j2k->m_private_image);
8619                 p_j2k->m_private_image = NULL;
8620                 return OPJ_FALSE;
8621         }
8622
8623         /* customization of the encoding */
8624         j2k_setup_header_reading(p_j2k);
8625
8626         /* read header */
8627         if (! j2k_exec (p_j2k,p_j2k->m_procedure_list,p_stream,p_manager)) {
8628                 opj_image_destroy(p_j2k->m_private_image);
8629                 p_j2k->m_private_image = NULL;
8630                 return OPJ_FALSE;
8631         }
8632
8633         *p_image = opj_image_create0();
8634         if (! (*p_image)) {
8635                 return OPJ_FALSE;
8636         }
8637
8638         /* Copy codestream image information to the output image */
8639         opj_copy_image_header(p_j2k->m_private_image, *p_image);
8640
8641     /*Allocate and initialize some elements of codestrem index*/
8642         if (!j2k_allocate_tile_element_cstr_index(p_j2k)){
8643                 return OPJ_FALSE;
8644         }
8645
8646         return OPJ_TRUE;
8647 }
8648
8649 /**
8650  * Sets up the procedures to do on reading header. Developpers wanting to extend the library can add their own reading procedures.
8651  */
8652 void j2k_setup_header_reading (opj_j2k_v2_t *p_j2k)
8653 {
8654         /* preconditions*/
8655         assert(p_j2k != 00);
8656
8657         opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_read_header_procedure);
8658
8659         /* DEVELOPER CORNER, add your custom procedures */
8660         opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_copy_default_tcp_and_create_tcd);
8661
8662 }
8663
8664 /**
8665  * Sets up the validation ,i.e. adds the procedures to lauch to make sure the codec parameters
8666  * are valid. Developpers wanting to extend the library can add their own validation procedures.
8667  */
8668 void j2k_setup_decoding_validation (opj_j2k_v2_t *p_j2k)
8669 {
8670         /* preconditions*/
8671         assert(p_j2k != 00);
8672
8673         opj_procedure_list_add_procedure(p_j2k->m_validation_list, (opj_procedure)j2k_build_decoder);
8674         opj_procedure_list_add_procedure(p_j2k->m_validation_list, (opj_procedure)j2k_decoding_validation);
8675         /* DEVELOPER CORNER, add your custom validation procedure */
8676
8677 }
8678
8679
8680 /**
8681  * The mct encoding validation procedure.
8682  *
8683  * @param       p_j2k                   the jpeg2000 codec to validate.
8684  * @param       p_stream                                the input stream to validate.
8685  * @param       p_manager               the user event manager.
8686  *
8687  * @return true if the parameters are correct.
8688  */
8689 opj_bool j2k_mct_validation (   opj_j2k_v2_t * p_j2k,
8690                                                                 opj_stream_private_t *p_stream,
8691                                                                 opj_event_mgr_t * p_manager )
8692 {
8693         opj_bool l_is_valid = OPJ_TRUE;
8694         OPJ_UINT32 i,j;
8695
8696         /* preconditions */
8697         assert(p_j2k != 00);
8698         assert(p_stream != 00);
8699         assert(p_manager != 00);
8700
8701         if ((p_j2k->m_cp.rsiz & 0x8200) == 0x8200) {
8702                 OPJ_UINT32 l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw;
8703                 opj_tcp_v2_t * l_tcp = p_j2k->m_cp.tcps;
8704
8705                 for (i=0;i<l_nb_tiles;++i) {
8706                         if (l_tcp->mct == 2) {
8707                                 opj_tccp_t * l_tccp = l_tcp->tccps;
8708                                 l_is_valid &= (l_tcp->m_mct_coding_matrix != 00);
8709
8710                                 for (j=0;j<p_j2k->m_private_image->numcomps;++j) {
8711                                         l_is_valid &= ! (l_tccp->qmfbid & 1);
8712                                         ++l_tccp;
8713                                 }
8714                         }
8715                         ++l_tcp;
8716                 }
8717         }
8718
8719         return l_is_valid;
8720 }
8721
8722 opj_bool j2k_setup_mct_encoding(opj_tcp_v2_t * p_tcp, opj_image_t * p_image)
8723 {
8724         OPJ_UINT32 i;
8725         OPJ_UINT32 l_indix = 1;
8726         opj_mct_data_t * l_mct_deco_data = 00,* l_mct_offset_data = 00;
8727         opj_simple_mcc_decorrelation_data_t * l_mcc_data;
8728         OPJ_UINT32 l_mct_size,l_nb_elem;
8729         OPJ_FLOAT32 * l_data, * l_current_data;
8730         opj_tccp_t * l_tccp;
8731
8732         // preconditions
8733         assert(p_tcp != 00);
8734
8735         if (p_tcp->mct != 2) {
8736                 return OPJ_TRUE;
8737         }
8738
8739         if (p_tcp->m_mct_decoding_matrix) {
8740                 if (p_tcp->m_nb_mct_records == p_tcp->m_nb_max_mct_records) {
8741                         p_tcp->m_nb_max_mct_records += J2K_MCT_DEFAULT_NB_RECORDS;
8742
8743                         p_tcp->m_mct_records = (opj_mct_data_t*)opj_realloc(p_tcp->m_mct_records,p_tcp->m_nb_max_mct_records * sizeof(opj_mct_data_t));
8744                         if (! p_tcp->m_mct_records) {
8745                                 return OPJ_FALSE;
8746                         }
8747                         l_mct_deco_data = p_tcp->m_mct_records + p_tcp->m_nb_mct_records;
8748
8749                         memset(l_mct_deco_data ,0,(p_tcp->m_nb_max_mct_records - p_tcp->m_nb_mct_records) * sizeof(opj_mct_data_t));
8750                 }
8751                 l_mct_deco_data = p_tcp->m_mct_records + p_tcp->m_nb_mct_records;
8752
8753                 if (l_mct_deco_data->m_data) {
8754                         opj_free(l_mct_deco_data->m_data);
8755                         l_mct_deco_data->m_data = 00;
8756                 }
8757
8758                 l_mct_deco_data->m_index = l_indix++;
8759                 l_mct_deco_data->m_array_type = MCT_TYPE_DECORRELATION;
8760                 l_mct_deco_data->m_element_type = MCT_TYPE_FLOAT;
8761                 l_nb_elem = p_image->numcomps * p_image->numcomps;
8762                 l_mct_size = l_nb_elem * MCT_ELEMENT_SIZE[l_mct_deco_data->m_element_type];
8763                 l_mct_deco_data->m_data = (OPJ_BYTE*)opj_malloc(l_mct_size );
8764
8765                 if (! l_mct_deco_data->m_data) {
8766                         return OPJ_FALSE;
8767                 }
8768
8769                 j2k_mct_write_functions_from_float[l_mct_deco_data->m_element_type](p_tcp->m_mct_decoding_matrix,l_mct_deco_data->m_data,l_nb_elem);
8770
8771                 l_mct_deco_data->m_data_size = l_mct_size;
8772                 ++p_tcp->m_nb_mct_records;
8773         }
8774
8775         if (p_tcp->m_nb_mct_records == p_tcp->m_nb_max_mct_records) {
8776                 p_tcp->m_nb_max_mct_records += J2K_MCT_DEFAULT_NB_RECORDS;
8777                 p_tcp->m_mct_records = (opj_mct_data_t*)opj_realloc(p_tcp->m_mct_records,p_tcp->m_nb_max_mct_records * sizeof(opj_mct_data_t));
8778
8779                 if (! p_tcp->m_mct_records) {
8780                         return OPJ_FALSE;
8781                 }
8782
8783                 l_mct_offset_data = p_tcp->m_mct_records + p_tcp->m_nb_mct_records;
8784                 memset(l_mct_offset_data ,0,(p_tcp->m_nb_max_mct_records - p_tcp->m_nb_mct_records) * sizeof(opj_mct_data_t));
8785
8786                 if (l_mct_deco_data) {
8787                         l_mct_deco_data = l_mct_offset_data - 1;
8788                 }
8789         }
8790
8791         l_mct_offset_data = p_tcp->m_mct_records + p_tcp->m_nb_mct_records;
8792
8793         if (l_mct_offset_data->m_data) {
8794                 opj_free(l_mct_offset_data->m_data);
8795                 l_mct_offset_data->m_data = 00;
8796         }
8797
8798         l_mct_offset_data->m_index = l_indix++;
8799         l_mct_offset_data->m_array_type = MCT_TYPE_OFFSET;
8800         l_mct_offset_data->m_element_type = MCT_TYPE_FLOAT;
8801         l_nb_elem = p_image->numcomps;
8802         l_mct_size = l_nb_elem * MCT_ELEMENT_SIZE[l_mct_offset_data->m_element_type];
8803         l_mct_offset_data->m_data = (OPJ_BYTE*)opj_malloc(l_mct_size );
8804
8805         if (! l_mct_offset_data->m_data) {
8806                 return OPJ_FALSE;
8807         }
8808
8809         l_data = (OPJ_FLOAT32*)opj_malloc(l_nb_elem * sizeof(OPJ_FLOAT32));
8810         if (! l_data) {
8811                 opj_free(l_mct_offset_data->m_data);
8812                 l_mct_offset_data->m_data = 00;
8813                 return OPJ_FALSE;
8814         }
8815
8816         l_tccp = p_tcp->tccps;
8817         l_current_data = l_data;
8818
8819         for (i=0;i<l_nb_elem;++i) {
8820                 *(l_current_data++) = (OPJ_FLOAT32) (l_tccp->m_dc_level_shift);
8821                 ++l_tccp;
8822         }
8823
8824         j2k_mct_write_functions_from_float[l_mct_offset_data->m_element_type](l_data,l_mct_offset_data->m_data,l_nb_elem);
8825
8826         opj_free(l_data);
8827
8828         l_mct_offset_data->m_data_size = l_mct_size;
8829
8830         ++p_tcp->m_nb_mct_records;
8831
8832         if (p_tcp->m_nb_mcc_records == p_tcp->m_nb_max_mcc_records) {
8833                 p_tcp->m_nb_max_mcc_records += J2K_MCT_DEFAULT_NB_RECORDS;
8834                 p_tcp->m_mcc_records = (opj_simple_mcc_decorrelation_data_t*)
8835                 opj_realloc(p_tcp->m_mcc_records,p_tcp->m_nb_max_mcc_records * sizeof(opj_simple_mcc_decorrelation_data_t));
8836
8837                 if (! p_tcp->m_mcc_records) {
8838                         return OPJ_FALSE;
8839                 }
8840                 l_mcc_data = p_tcp->m_mcc_records + p_tcp->m_nb_mcc_records;
8841                 memset(l_mcc_data ,0,(p_tcp->m_nb_max_mcc_records - p_tcp->m_nb_mcc_records) * sizeof(opj_simple_mcc_decorrelation_data_t));
8842
8843         }
8844
8845         l_mcc_data = p_tcp->m_mcc_records + p_tcp->m_nb_mcc_records;
8846         l_mcc_data->m_decorrelation_array = l_mct_deco_data;
8847         l_mcc_data->m_is_irreversible = 1;
8848         l_mcc_data->m_nb_comps = p_image->numcomps;
8849         l_mcc_data->m_index = l_indix++;
8850         l_mcc_data->m_offset_array = l_mct_offset_data;
8851         ++p_tcp->m_nb_mcc_records;
8852
8853         return OPJ_TRUE;
8854 }
8855
8856 /**
8857  * Builds the cp decoder parameters to use to decode tile.
8858  */
8859 opj_bool j2k_build_decoder (opj_j2k_v2_t * p_j2k,
8860                                                         opj_stream_private_t *p_stream,
8861                                                         opj_event_mgr_t * p_manager )
8862 {
8863         /* add here initialization of cp
8864            copy paste of setup_decoder */
8865   (void)p_j2k;
8866   (void)p_stream;
8867   (void)p_manager;
8868         return OPJ_TRUE;
8869 }
8870
8871 /**
8872  * Builds the cp encoder parameters to use to encode tile.
8873  */
8874 opj_bool j2k_build_encoder (opj_j2k_v2_t * p_j2k,
8875                                                         opj_stream_private_t *p_stream,
8876                                                         opj_event_mgr_t * p_manager )
8877 {
8878         /* add here initialization of cp
8879            copy paste of setup_encoder */
8880   (void)p_j2k;
8881   (void)p_stream;
8882   (void)p_manager;
8883         return OPJ_TRUE;
8884 }
8885
8886 /**
8887  * The default encoding validation procedure without any extension.
8888  *
8889  * @param       p_j2k                   the jpeg2000 codec to validate.
8890  * @param       p_stream                                the input stream to validate.
8891  * @param       p_manager               the user event manager.
8892  *
8893  * @return true if the parameters are correct.
8894  */
8895 opj_bool j2k_encoding_validation (      opj_j2k_v2_t * p_j2k,
8896                                                                         opj_stream_private_t *p_stream,
8897                                                                         opj_event_mgr_t * p_manager )
8898 {
8899         opj_bool l_is_valid = OPJ_TRUE;
8900
8901         /* preconditions */
8902         assert(p_j2k != 00);
8903         assert(p_stream != 00);
8904         assert(p_manager != 00);
8905
8906         /* STATE checking */
8907         /* make sure the state is at 0 */
8908         l_is_valid &= (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_NONE);
8909
8910         /* POINTER validation */
8911         /* make sure a p_j2k codec is present */
8912         l_is_valid &= (p_j2k->m_procedure_list != 00);
8913         /* make sure a validation list is present */
8914         l_is_valid &= (p_j2k->m_validation_list != 00);
8915
8916         if ((p_j2k->m_cp.tdx) < (OPJ_UINT32) (1 << p_j2k->m_cp.tcps->tccps->numresolutions)) {
8917                 opj_event_msg_v2(p_manager, EVT_ERROR, "Number of resolutions is too high in comparison to the size of tiles\n");
8918                 return OPJ_FALSE;
8919         }
8920
8921         if ((p_j2k->m_cp.tdy) < (OPJ_UINT32) (1 << p_j2k->m_cp.tcps->tccps->numresolutions)) {
8922                 opj_event_msg_v2(p_manager, EVT_ERROR, "Number of resolutions is too high in comparison to the size of tiles\n");
8923                 return OPJ_FALSE;
8924         }
8925
8926         /* PARAMETER VALIDATION */
8927         return l_is_valid;
8928 }
8929
8930 /**
8931  * The default decoding validation procedure without any extension.
8932  *
8933  * @param       p_j2k                   the jpeg2000 codec to validate.
8934  * @param       p_stream                                the input stream to validate.
8935  * @param       p_manager               the user event manager.
8936  *
8937  * @return true if the parameters are correct.
8938  */
8939 opj_bool j2k_decoding_validation (
8940                                                                 opj_j2k_v2_t *p_j2k,
8941                                                                 opj_stream_private_t *p_stream,
8942                                                                 opj_event_mgr_t * p_manager
8943                                                           )
8944 {
8945         opj_bool l_is_valid = OPJ_TRUE;
8946
8947         /* preconditions*/
8948         assert(p_j2k != 00);
8949         assert(p_stream != 00);
8950         assert(p_manager != 00);
8951
8952
8953         /* STATE checking */
8954         /* make sure the state is at 0 */
8955 #ifdef TODO_MSD
8956         l_is_valid &= (p_j2k->m_specific_param.m_decoder.m_state == J2K_DEC_STATE_NONE);
8957 #endif
8958         l_is_valid &= (p_j2k->m_specific_param.m_decoder.m_state == 0x0000);
8959
8960         /* POINTER validation */
8961         /* make sure a p_j2k codec is present */
8962         /* make sure a procedure list is present */
8963         l_is_valid &= (p_j2k->m_procedure_list != 00);
8964         /* make sure a validation list is present */
8965         l_is_valid &= (p_j2k->m_validation_list != 00);
8966
8967         /* PARAMETER VALIDATION */
8968         return l_is_valid;
8969 }
8970
8971 opj_bool j2k_read_header_procedure(     opj_j2k_v2_t *p_j2k,
8972                                                                         struct opj_stream_private *p_stream,
8973                                                                         struct opj_event_mgr * p_manager)
8974 {
8975         OPJ_UINT32 l_current_marker;
8976         OPJ_UINT32 l_marker_size;
8977         const opj_dec_memory_marker_handler_t * l_marker_handler = 00;
8978
8979         /* preconditions */
8980         assert(p_stream != 00);
8981         assert(p_j2k != 00);
8982         assert(p_manager != 00);
8983
8984         /*  We enter in the main header */
8985         p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_MHSOC;
8986
8987         /* Try to read the SOC marker, the codestream must begin with SOC marker */
8988         if (! opj_j2k_read_soc(p_j2k,p_stream,p_manager)) {
8989                 opj_event_msg_v2(p_manager, EVT_ERROR, "Expected a SOC marker \n");
8990                 return OPJ_FALSE;
8991         }
8992
8993         /* Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer */
8994         if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
8995                 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
8996                 return OPJ_FALSE;
8997         }
8998
8999         /* Read 2 bytes as the new marker ID */
9000         opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_current_marker,2);
9001
9002         /* Try to read until the SOT is detected */
9003         while (l_current_marker != J2K_MS_SOT) {
9004
9005                 /* Check if the current marker ID is valid */
9006                 if (l_current_marker < 0xff00) {
9007                         opj_event_msg_v2(p_manager, EVT_ERROR, "We expected read a marker ID (0xff--) instead of %.8x\n", l_current_marker);
9008                         return OPJ_FALSE;
9009                 }
9010
9011                 /* Get the marker handler from the marker ID */
9012                 l_marker_handler = j2k_get_marker_handler(l_current_marker);
9013
9014                 /* Manage case where marker is unknown */
9015                 if (l_marker_handler->id == J2K_MS_UNK) {
9016                         if (! j2k_read_unk_v2(p_j2k, p_stream, &l_current_marker, p_manager)){
9017                                 opj_event_msg_v2(p_manager, EVT_ERROR, "Unknow marker have been detected and generated error.\n");
9018                                 return OPJ_FALSE;
9019                         }
9020
9021                         if (l_current_marker == J2K_MS_SOT)
9022                                 break; /* SOT marker is detected main header is completely read */
9023                         else    /* Get the marker handler from the marker ID */
9024                                 l_marker_handler = j2k_get_marker_handler(l_current_marker);
9025                 }
9026
9027                 /* Check if the marker is known and if it is the right place to find it */
9028                 if (! (p_j2k->m_specific_param.m_decoder.m_state & l_marker_handler->states) ) {
9029                         opj_event_msg_v2(p_manager, EVT_ERROR, "Marker is not compliant with its position\n");
9030                         return OPJ_FALSE;
9031                 }
9032
9033                 /* Try to read 2 bytes (the marker size) from stream and copy them into the buffer */
9034                 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
9035                         opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
9036                         return OPJ_FALSE;
9037                 }
9038
9039                 /* read 2 bytes as the marker size */
9040                 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_marker_size,2);
9041                 l_marker_size -= 2; /* Subtract the size of the marker ID already read */
9042
9043                 /* Check if the marker size is compatible with the header data size */
9044                 if (l_marker_size > p_j2k->m_specific_param.m_decoder.m_header_data_size) {
9045                         p_j2k->m_specific_param.m_decoder.m_header_data = (OPJ_BYTE*)
9046                                         opj_realloc(p_j2k->m_specific_param.m_decoder.m_header_data,l_marker_size);
9047                         if (p_j2k->m_specific_param.m_decoder.m_header_data == 00) {
9048                                 return OPJ_FALSE;
9049                         }
9050                         p_j2k->m_specific_param.m_decoder.m_header_data_size = l_marker_size;
9051                 }
9052
9053                 /* Try to read the rest of the marker segment from stream and copy them into the buffer */
9054                 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) {
9055                         opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
9056                         return OPJ_FALSE;
9057                 }
9058
9059                 /* Read the marker segment with the correct marker handler */
9060                 if (! (*(l_marker_handler->handler))(p_j2k,p_j2k->m_specific_param.m_decoder.m_header_data,l_marker_size,p_manager)) {
9061                         opj_event_msg_v2(p_manager, EVT_ERROR, "Marker handler function failed to read the marker segment\n");
9062                         return OPJ_FALSE;
9063                 }
9064
9065                 /* Add the marker to the codestream index*/
9066                 j2k_add_mhmarker_v2(p_j2k->cstr_index,
9067                                                         l_marker_handler->id,
9068                                                         (OPJ_UINT32) opj_stream_tell(p_stream) - l_marker_size - 4,
9069                                                         l_marker_size + 4 );
9070
9071                 /* Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer */
9072                 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
9073                         opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
9074                         return OPJ_FALSE;
9075                 }
9076
9077                 /* read 2 bytes as the new marker ID */
9078                 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_current_marker,2);
9079         }
9080
9081         opj_event_msg_v2(p_manager, EVT_INFO, "Main header has been correctly decoded.\n");
9082
9083         /* Position of the last element if the main header */
9084         p_j2k->cstr_index->main_head_end = (OPJ_UINT32) opj_stream_tell(p_stream) - 2;
9085
9086         /* Next step: read a tile-part header */
9087         p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_TPHSOT;
9088
9089         return OPJ_TRUE;
9090 }
9091
9092 /**
9093  * Excutes the given procedures on the given codec.
9094  *
9095  * @param       p_procedure_list        the list of procedures to execute
9096  * @param       p_j2k                                   the jpeg2000 codec to execute the procedures on.
9097  * @param       p_stream                                        the stream to execute the procedures on.
9098  * @param       p_manager                       the user manager.
9099  *
9100  * @return      true                            if all the procedures were successfully executed.
9101  */
9102 opj_bool j2k_exec (     opj_j2k_v2_t * p_j2k,
9103                                         opj_procedure_list_t * p_procedure_list,
9104                                         opj_stream_private_t *p_stream,
9105                                         opj_event_mgr_t * p_manager )
9106 {
9107         opj_bool (** l_procedure) (opj_j2k_v2_t * ,opj_stream_private_t *,opj_event_mgr_t *) = 00;
9108         opj_bool l_result = OPJ_TRUE;
9109         OPJ_UINT32 l_nb_proc, i;
9110
9111         /* preconditions*/
9112         assert(p_procedure_list != 00);
9113         assert(p_j2k != 00);
9114         assert(p_stream != 00);
9115         assert(p_manager != 00);
9116
9117
9118         l_nb_proc = opj_procedure_list_get_nb_procedures(p_procedure_list);
9119         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);
9120
9121         for     (i=0;i<l_nb_proc;++i) {
9122                 l_result = l_result && ((*l_procedure) (p_j2k,p_stream,p_manager));
9123                 ++l_procedure;
9124         }
9125
9126         /* and clear the procedure list at the end.*/
9127         opj_procedure_list_clear(p_procedure_list);
9128         return l_result;
9129 }
9130
9131 /* FIXME DOC*/
9132 opj_bool j2k_copy_default_tcp_and_create_tcd
9133                                                 (
9134                                                 opj_j2k_v2_t * p_j2k,
9135                                                 opj_stream_private_t *p_stream,
9136                                                 opj_event_mgr_t * p_manager
9137                                                 )
9138 {
9139         opj_tcp_v2_t * l_tcp = 00;
9140         opj_tcp_v2_t * l_default_tcp = 00;
9141         OPJ_UINT32 l_nb_tiles;
9142         OPJ_UINT32 i,j;
9143         opj_tccp_t *l_current_tccp = 00;
9144         OPJ_UINT32 l_tccp_size;
9145         OPJ_UINT32 l_mct_size;
9146         opj_image_t * l_image;
9147         OPJ_UINT32 l_mcc_records_size,l_mct_records_size;
9148         opj_mct_data_t * l_src_mct_rec, *l_dest_mct_rec;
9149         opj_simple_mcc_decorrelation_data_t * l_src_mcc_rec, *l_dest_mcc_rec;
9150         OPJ_UINT32 l_offset;
9151
9152         /* preconditions */
9153         assert(p_j2k != 00);
9154         assert(p_stream != 00);
9155         assert(p_manager != 00);
9156
9157         l_image = p_j2k->m_private_image;
9158         l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw;
9159         l_tcp = p_j2k->m_cp.tcps;
9160         l_tccp_size = l_image->numcomps * sizeof(opj_tccp_t);
9161         l_default_tcp = p_j2k->m_specific_param.m_decoder.m_default_tcp;
9162         l_mct_size = l_image->numcomps * l_image->numcomps * sizeof(OPJ_FLOAT32);
9163
9164         /* For each tile */
9165         for (i=0; i<l_nb_tiles; ++i) {
9166                 /* keep the tile-compo coding parameters pointer of the current tile coding parameters*/
9167                 l_current_tccp = l_tcp->tccps;
9168                 /*Copy default coding parameters into the current tile coding parameters*/
9169                 memcpy(l_tcp, l_default_tcp, sizeof(opj_tcp_v2_t));
9170                 /* Initialize some values of the current tile coding parameters*/
9171                 l_tcp->ppt = 0;
9172                 l_tcp->ppt_data = 00;
9173                 /* Reconnect the tile-compo coding parameters pointer to the current tile coding parameters*/
9174                 l_tcp->tccps = l_current_tccp;
9175
9176                 /* Get the mct_decoding_matrix of the dflt_tile_cp and copy them into the current tile cp*/
9177                 if (l_default_tcp->m_mct_decoding_matrix) {
9178                         l_tcp->m_mct_decoding_matrix = (OPJ_FLOAT32*)opj_malloc(l_mct_size);
9179                         if (! l_tcp->m_mct_decoding_matrix ) {
9180                                 return OPJ_FALSE;
9181                         }
9182                         memcpy(l_tcp->m_mct_decoding_matrix,l_default_tcp->m_mct_decoding_matrix,l_mct_size);
9183                 }
9184
9185                 /* Get the mct_record of the dflt_tile_cp and copy them into the current tile cp*/
9186                 l_mct_records_size = l_default_tcp->m_nb_max_mct_records * sizeof(opj_mct_data_t);
9187                 l_tcp->m_mct_records = (opj_mct_data_t*)opj_malloc(l_mct_records_size);
9188                 if (! l_tcp->m_mct_records) {
9189                         return OPJ_FALSE;
9190                 }
9191                 memcpy(l_tcp->m_mct_records, l_default_tcp->m_mct_records,l_mct_records_size);
9192
9193                 /* Copy the mct record data from dflt_tile_cp to the current tile*/
9194                 l_src_mct_rec = l_default_tcp->m_mct_records;
9195                 l_dest_mct_rec = l_tcp->m_mct_records;
9196
9197                 for (j=0;j<l_default_tcp->m_nb_mct_records;++j) {
9198
9199                         if (l_src_mct_rec->m_data) {
9200
9201                                 l_dest_mct_rec->m_data = (OPJ_BYTE*) opj_malloc(l_src_mct_rec->m_data_size);
9202                                 if(! l_dest_mct_rec->m_data) {
9203                                         return OPJ_FALSE;
9204                                 }
9205                                 memcpy(l_dest_mct_rec->m_data,l_src_mct_rec->m_data,l_src_mct_rec->m_data_size);
9206                         }
9207
9208                         ++l_src_mct_rec;
9209                         ++l_dest_mct_rec;
9210                 }
9211
9212                 /* Get the mcc_record of the dflt_tile_cp and copy them into the current tile cp*/
9213                 l_mcc_records_size = l_default_tcp->m_nb_max_mcc_records * sizeof(opj_simple_mcc_decorrelation_data_t);
9214                 l_tcp->m_mcc_records = (opj_simple_mcc_decorrelation_data_t*) opj_malloc(l_mcc_records_size);
9215                 if (! l_tcp->m_mcc_records) {
9216                         return OPJ_FALSE;
9217                 }
9218                 memcpy(l_tcp->m_mcc_records,l_default_tcp->m_mcc_records,l_mcc_records_size);
9219
9220                 /* Copy the mcc record data from dflt_tile_cp to the current tile*/
9221                 l_src_mcc_rec = l_default_tcp->m_mcc_records;
9222                 l_dest_mcc_rec = l_tcp->m_mcc_records;
9223
9224                 for (j=0;j<l_default_tcp->m_nb_max_mcc_records;++j) {
9225
9226                         if (l_src_mcc_rec->m_decorrelation_array) {
9227                                 l_offset = l_src_mcc_rec->m_decorrelation_array - l_default_tcp->m_mct_records;
9228                                 l_dest_mcc_rec->m_decorrelation_array = l_tcp->m_mct_records + l_offset;
9229                         }
9230
9231                         if (l_src_mcc_rec->m_offset_array) {
9232                                 l_offset = l_src_mcc_rec->m_offset_array - l_default_tcp->m_mct_records;
9233                                 l_dest_mcc_rec->m_offset_array = l_tcp->m_mct_records + l_offset;
9234                         }
9235
9236                         ++l_src_mcc_rec;
9237                         ++l_dest_mcc_rec;
9238                 }
9239
9240                 /* Copy all the dflt_tile_compo_cp to the current tile cp */
9241                 memcpy(l_current_tccp,l_default_tcp->tccps,l_tccp_size);
9242
9243                 /* Move to next tile cp*/
9244                 ++l_tcp;
9245         }
9246
9247         /* Create the current tile decoder*/
9248         p_j2k->m_tcd = (opj_tcd_v2_t*)tcd_create_v2(OPJ_TRUE); /* FIXME why a cast ? */
9249         if (! p_j2k->m_tcd ) {
9250                 return OPJ_FALSE;
9251         }
9252
9253         if ( !tcd_init_v2(p_j2k->m_tcd, l_image, &(p_j2k->m_cp)) ) {
9254                 tcd_destroy_v2(p_j2k->m_tcd);
9255                 p_j2k->m_tcd = 00;
9256                 opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot decode tile, memory error\n");
9257                 return OPJ_FALSE;
9258         }
9259
9260         return OPJ_TRUE;
9261 }
9262
9263 /**
9264  * Reads the lookup table containing all the marker, status and action, and returns the handler associated
9265  * with the marker value.
9266  * @param       p_id            Marker value to look up
9267  *
9268  * @return      the handler associated with the id.
9269 */
9270 const opj_dec_memory_marker_handler_t * j2k_get_marker_handler (OPJ_UINT32 p_id)
9271 {
9272         const opj_dec_memory_marker_handler_t *e;
9273         for (e = j2k_memory_marker_handler_tab; e->id != 0; ++e) {
9274                 if (e->id == p_id) {
9275                         break; /* we find a handler corresponding to the marker ID*/
9276                 }
9277         }
9278         return e;
9279 }
9280
9281
9282 /**
9283  * Destroys a jpeg2000 codec.
9284  *
9285  * @param       p_j2k   the jpeg20000 structure to destroy.
9286  */
9287 void j2k_destroy (opj_j2k_v2_t *p_j2k)
9288 {
9289         if (p_j2k == 00) {
9290                 return;
9291         }
9292
9293         if (p_j2k->m_is_decoder) {
9294
9295                 if (p_j2k->m_specific_param.m_decoder.m_default_tcp != 00) {
9296                         j2k_tcp_destroy(p_j2k->m_specific_param.m_decoder.m_default_tcp);
9297                         opj_free(p_j2k->m_specific_param.m_decoder.m_default_tcp);
9298                         p_j2k->m_specific_param.m_decoder.m_default_tcp = 00;
9299                 }
9300
9301                 if (p_j2k->m_specific_param.m_decoder.m_header_data != 00) {
9302                         opj_free(p_j2k->m_specific_param.m_decoder.m_header_data);
9303                         p_j2k->m_specific_param.m_decoder.m_header_data = 00;
9304                         p_j2k->m_specific_param.m_decoder.m_header_data_size = 0;
9305                 }
9306         }
9307         else {
9308
9309                 if (p_j2k->m_specific_param.m_encoder.m_encoded_tile_data) {
9310                         opj_free(p_j2k->m_specific_param.m_encoder.m_encoded_tile_data);
9311                         p_j2k->m_specific_param.m_encoder.m_encoded_tile_data = 00;
9312                 }
9313
9314                 if (p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer) {
9315                         opj_free(p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer);
9316                         p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer = 00;
9317                         p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current = 00;
9318                 }
9319
9320                 if (p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
9321                         opj_free(p_j2k->m_specific_param.m_encoder.m_header_tile_data);
9322                         p_j2k->m_specific_param.m_encoder.m_header_tile_data = 00;
9323                         p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = 0;
9324                 }
9325         }
9326
9327         tcd_destroy_v2(p_j2k->m_tcd);
9328
9329         j2k_cp_destroy(&(p_j2k->m_cp));
9330         memset(&(p_j2k->m_cp),0,sizeof(opj_cp_v2_t));
9331
9332         opj_procedure_list_destroy(p_j2k->m_procedure_list);
9333         p_j2k->m_procedure_list = 00;
9334
9335         opj_procedure_list_destroy(p_j2k->m_validation_list);
9336         p_j2k->m_procedure_list = 00;
9337
9338         j2k_destroy_cstr_index(p_j2k->cstr_index);
9339         p_j2k->cstr_index = NULL;
9340
9341         opj_image_destroy(p_j2k->m_private_image);
9342         p_j2k->m_private_image = NULL;
9343
9344         opj_image_destroy(p_j2k->m_output_image);
9345         p_j2k->m_output_image = NULL;
9346
9347         opj_free(p_j2k);
9348 }
9349
9350 void j2k_destroy_cstr_index (opj_codestream_index_t *p_cstr_ind)
9351 {
9352         if (p_cstr_ind) {
9353
9354                 if (p_cstr_ind->marker) {
9355                         opj_free(p_cstr_ind->marker);
9356                         p_cstr_ind->marker = NULL;
9357                 }
9358
9359                 if (p_cstr_ind->tile_index) {
9360                         OPJ_UINT32 it_tile = 0;
9361
9362                         for (it_tile=0; it_tile < p_cstr_ind->nb_of_tiles; it_tile++) {
9363
9364                                 if(p_cstr_ind->tile_index[it_tile].packet_index) {
9365                                         opj_free(p_cstr_ind->tile_index[it_tile].packet_index);
9366                                         p_cstr_ind->tile_index[it_tile].packet_index = NULL;
9367                                 }
9368
9369                                 if(p_cstr_ind->tile_index[it_tile].tp_index){
9370                                         opj_free(p_cstr_ind->tile_index[it_tile].tp_index);
9371                                         p_cstr_ind->tile_index[it_tile].tp_index = NULL;
9372                                 }
9373
9374                                 if(p_cstr_ind->tile_index[it_tile].marker){
9375                                         opj_free(p_cstr_ind->tile_index[it_tile].marker);
9376                                         p_cstr_ind->tile_index[it_tile].marker = NULL;
9377
9378                                 }
9379                         }
9380
9381                         opj_free( p_cstr_ind->tile_index);
9382                         p_cstr_ind->tile_index = NULL;
9383                 }
9384
9385                 opj_free(p_cstr_ind);
9386         }
9387 }
9388
9389
9390
9391 /**
9392  * Destroys a tile coding parameter structure.
9393  *
9394  * @param       p_tcp           the tile coding parameter to destroy.
9395  */
9396 void j2k_tcp_destroy (opj_tcp_v2_t *p_tcp)
9397 {
9398         if (p_tcp == 00) {
9399                 return;
9400         }
9401
9402         if (p_tcp->ppt_buffer != 00) {
9403                 opj_free(p_tcp->ppt_buffer);
9404                 p_tcp->ppt_buffer = 00;
9405         }
9406
9407         if (p_tcp->tccps != 00) {
9408                 opj_free(p_tcp->tccps);
9409                 p_tcp->tccps = 00;
9410         }
9411
9412         if (p_tcp->m_mct_coding_matrix != 00) {
9413                 opj_free(p_tcp->m_mct_coding_matrix);
9414                 p_tcp->m_mct_coding_matrix = 00;
9415         }
9416
9417         if (p_tcp->m_mct_decoding_matrix != 00) {
9418                 opj_free(p_tcp->m_mct_decoding_matrix);
9419                 p_tcp->m_mct_decoding_matrix = 00;
9420         }
9421
9422         if (p_tcp->m_mcc_records) {
9423                 opj_free(p_tcp->m_mcc_records);
9424                 p_tcp->m_mcc_records = 00;
9425                 p_tcp->m_nb_max_mcc_records = 0;
9426                 p_tcp->m_nb_mcc_records = 0;
9427         }
9428
9429         if (p_tcp->m_mct_records) {
9430                 opj_mct_data_t * l_mct_data = p_tcp->m_mct_records;
9431                 OPJ_UINT32 i;
9432
9433                 for (i=0;i<p_tcp->m_nb_mct_records;++i) {
9434                         if (l_mct_data->m_data) {
9435                                 opj_free(l_mct_data->m_data);
9436                                 l_mct_data->m_data = 00;
9437                         }
9438
9439                         ++l_mct_data;
9440                 }
9441
9442                 opj_free(p_tcp->m_mct_records);
9443                 p_tcp->m_mct_records = 00;
9444         }
9445
9446         if (p_tcp->mct_norms != 00) {
9447                 opj_free(p_tcp->mct_norms);
9448                 p_tcp->mct_norms = 00;
9449         }
9450
9451         j2k_tcp_data_destroy(p_tcp);
9452
9453 }
9454
9455 /**
9456  * Destroys the data inside a tile coding parameter structure.
9457  *
9458  * @param       p_tcp           the tile coding parameter which contain data to destroy.
9459  */
9460 void j2k_tcp_data_destroy (opj_tcp_v2_t *p_tcp)
9461 {
9462         if (p_tcp->m_data) {
9463                 opj_free(p_tcp->m_data);
9464                 p_tcp->m_data = NULL;
9465                 p_tcp->m_data_size = 0;
9466         }
9467 }
9468
9469 /**
9470  * Destroys a coding parameter structure.
9471  *
9472  * @param       p_cp            the coding parameter to destroy.
9473  */
9474 void j2k_cp_destroy (opj_cp_v2_t *p_cp)
9475 {
9476         OPJ_UINT32 l_nb_tiles;
9477         opj_tcp_v2_t * l_current_tile = 00;
9478         OPJ_UINT32 i;
9479
9480         if
9481                 (p_cp == 00)
9482         {
9483                 return;
9484         }
9485         if
9486                 (p_cp->tcps != 00)
9487         {
9488                 l_current_tile = p_cp->tcps;
9489                 l_nb_tiles = p_cp->th * p_cp->tw;
9490
9491                 for
9492                         (i = 0; i < l_nb_tiles; ++i)
9493                 {
9494                         j2k_tcp_destroy(l_current_tile);
9495                         ++l_current_tile;
9496                 }
9497                 opj_free(p_cp->tcps);
9498                 p_cp->tcps = 00;
9499         }
9500         if
9501                 (p_cp->ppm_buffer != 00)
9502         {
9503                 opj_free(p_cp->ppm_buffer);
9504                 p_cp->ppm_buffer = 00;
9505         }
9506         if
9507                 (p_cp->comment != 00)
9508         {
9509                 opj_free(p_cp->comment);
9510                 p_cp->comment = 00;
9511         }
9512         if
9513                 (! p_cp->m_is_decoder)
9514         {
9515                 if
9516                         (p_cp->m_specific_param.m_enc.m_matrice)
9517                 {
9518                         opj_free(p_cp->m_specific_param.m_enc.m_matrice);
9519                         p_cp->m_specific_param.m_enc.m_matrice = 00;
9520                 }
9521         }
9522 }
9523
9524
9525
9526 /**
9527  * Reads a tile header.
9528  * @param       p_j2k           the jpeg2000 codec.
9529  * @param       p_stream                        the stream to write data to.
9530  * @param       p_manager       the user event manager.
9531  */
9532 opj_bool j2k_read_tile_header(  opj_j2k_v2_t * p_j2k,
9533                                                                 OPJ_UINT32 * p_tile_index,
9534                                                                 OPJ_UINT32 * p_data_size,
9535                                                                 OPJ_INT32 * p_tile_x0, OPJ_INT32 * p_tile_y0,
9536                                                                 OPJ_INT32 * p_tile_x1, OPJ_INT32 * p_tile_y1,
9537                                                                 OPJ_UINT32 * p_nb_comps,
9538                                                                 opj_bool * p_go_on,
9539                                                                 opj_stream_private_t *p_stream,
9540                                                                 opj_event_mgr_t * p_manager )
9541 {
9542         OPJ_UINT32 l_current_marker = J2K_MS_SOT;
9543         OPJ_UINT32 l_marker_size;
9544         const opj_dec_memory_marker_handler_t * l_marker_handler = 00;
9545         opj_tcp_v2_t * l_tcp = NULL;
9546         OPJ_UINT32 l_nb_tiles;
9547
9548         /* preconditions */
9549         assert(p_stream != 00);
9550         assert(p_j2k != 00);
9551         assert(p_manager != 00);
9552
9553         /* Reach the End Of Codestream ?*/
9554         if (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_EOC){
9555                 l_current_marker = J2K_MS_EOC;
9556         }
9557         /* We need to encounter a SOT marker (a new tile-part header) */
9558         else if (p_j2k->m_specific_param.m_decoder.m_state != J2K_STATE_TPHSOT){
9559                 return OPJ_FALSE;
9560         }
9561
9562         /* Read into the codestream until reach the EOC or ! can_decode ??? FIXME */
9563         while ( (!p_j2k->m_specific_param.m_decoder.m_can_decode) && (l_current_marker != J2K_MS_EOC) ) {
9564
9565                 /* Try to read until the Start Of Data is detected */
9566                 while (l_current_marker != J2K_MS_SOD) {
9567
9568                         /* Try to read 2 bytes (the marker size) from stream and copy them into the buffer */
9569                         if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
9570                                 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
9571                                 return OPJ_FALSE;
9572                         }
9573
9574                         /* Read 2 bytes from the buffer as the marker size */
9575                         opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_marker_size,2);
9576
9577                         /* Why this condition? FIXME */
9578                         if (p_j2k->m_specific_param.m_decoder.m_state & J2K_STATE_TPH){
9579                                 p_j2k->m_specific_param.m_decoder.m_sot_length -= (l_marker_size + 2);
9580                         }
9581                         l_marker_size -= 2; /* Subtract the size of the marker ID already read */
9582
9583                         /* Get the marker handler from the marker ID */
9584                         l_marker_handler = j2k_get_marker_handler(l_current_marker);
9585
9586                         /* Check if the marker is known and if it is the right place to find it */
9587                         if (! (p_j2k->m_specific_param.m_decoder.m_state & l_marker_handler->states) ) {
9588                                 opj_event_msg_v2(p_manager, EVT_ERROR, "Marker is not compliant with its position\n");
9589                                 return OPJ_FALSE;
9590                         }
9591 /* FIXME manage case of unknown marker as in the main header ? */
9592
9593                         /* Check if the marker size is compatible with the header data size */
9594                         if (l_marker_size > p_j2k->m_specific_param.m_decoder.m_header_data_size) {
9595                                 p_j2k->m_specific_param.m_decoder.m_header_data = (OPJ_BYTE*)
9596                                         opj_realloc(p_j2k->m_specific_param.m_decoder.m_header_data,l_marker_size);
9597                                 if (p_j2k->m_specific_param.m_decoder.m_header_data == 00) {
9598                                         return OPJ_FALSE;
9599                                 }
9600                                 p_j2k->m_specific_param.m_decoder.m_header_data_size = l_marker_size;
9601                         }
9602
9603                         /* Try to read the rest of the marker segment from stream and copy them into the buffer */
9604                         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) {
9605                                 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
9606                                 return OPJ_FALSE;
9607                         }
9608
9609                         /* Read the marker segment with the correct marker handler */
9610                         if (! (*(l_marker_handler->handler))(p_j2k,p_j2k->m_specific_param.m_decoder.m_header_data,l_marker_size,p_manager)) {
9611                                 opj_event_msg_v2(p_manager, EVT_ERROR, "Fail to read the current marker segment (%#x)\n", l_current_marker);
9612                                 return OPJ_FALSE;
9613                         }
9614
9615                         /* Add the marker to the codestream index*/
9616                         j2k_add_tlmarker_v2(p_j2k->m_current_tile_number,
9617                                                                 p_j2k->cstr_index,
9618                                                                 l_marker_handler->id,
9619                                                                 (OPJ_UINT32) opj_stream_tell(p_stream) - l_marker_size - 4,
9620                                                                 l_marker_size + 4 );
9621
9622                         /* Keep the position of the last SOT marker read */
9623                         if ( l_marker_handler->id == J2K_MS_SOT ) {
9624                                 OPJ_UINT32 sot_pos = (OPJ_UINT32) opj_stream_tell(p_stream) - l_marker_size - 4 ;
9625                                 if (sot_pos > p_j2k->m_specific_param.m_decoder.m_last_sot_read_pos)
9626                                 {
9627                                         p_j2k->m_specific_param.m_decoder.m_last_sot_read_pos = sot_pos;
9628                                 }
9629                         }
9630
9631
9632                         if (p_j2k->m_specific_param.m_decoder.m_skip_data) {
9633                                 /* Skip the rest of the tile part header*/
9634                                 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) {
9635                                         opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
9636                                         return OPJ_FALSE;
9637                                 }
9638                                 l_current_marker = J2K_MS_SOD; /* Normally we reached a SOD */
9639                         }
9640                         else {
9641                                 /* Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer*/
9642                                 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
9643                                         opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
9644                                         return OPJ_FALSE;
9645                                 }
9646                                 /* Read 2 bytes from the buffer as the new marker ID */
9647                                 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_current_marker,2);
9648                         }
9649                 }
9650
9651                 /* If we didn't skip data before, we need to read the SOD marker*/
9652                 if (! p_j2k->m_specific_param.m_decoder.m_skip_data) {
9653                         /* Try to read the SOD marker and skip data ? FIXME */
9654                         if (! j2k_read_sod_v2(p_j2k, p_stream, p_manager)) {
9655                                 return OPJ_FALSE;
9656                         }
9657
9658
9659
9660                         if (! p_j2k->m_specific_param.m_decoder.m_can_decode){
9661                                 /* Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer */
9662                                 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
9663                                         opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
9664                                         return OPJ_FALSE;
9665                                 }
9666
9667                                 /* Read 2 bytes from buffer as the new marker ID */
9668                                 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_current_marker,2);
9669                         }
9670                 }
9671                 else {
9672                         /* Indicate we will try to read a new tile-part header*/
9673                         p_j2k->m_specific_param.m_decoder.m_skip_data = 0;
9674                         p_j2k->m_specific_param.m_decoder.m_can_decode = 0;
9675                         p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_TPHSOT;
9676
9677                         /* Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer */
9678                         if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
9679                                 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
9680                                 return OPJ_FALSE;
9681                         }
9682
9683                         /* Read 2 bytes from buffer as the new marker ID */
9684                         opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_current_marker,2);
9685                 }
9686         }
9687
9688         /* Current marker is the EOC marker ?*/
9689         if (l_current_marker == J2K_MS_EOC) {
9690                 if (p_j2k->m_specific_param.m_decoder.m_state != J2K_STATE_EOC ){
9691                         p_j2k->m_current_tile_number = 0;
9692                         p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_EOC;
9693                 }
9694         }
9695
9696         /* FIXME DOC ???*/
9697         if ( ! p_j2k->m_specific_param.m_decoder.m_can_decode) {
9698                 l_tcp = p_j2k->m_cp.tcps + p_j2k->m_current_tile_number;
9699                 l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw;
9700
9701                 while( (p_j2k->m_current_tile_number < l_nb_tiles) && (l_tcp->m_data == 00) ) {
9702                         ++p_j2k->m_current_tile_number;
9703                         ++l_tcp;
9704                 }
9705
9706                 if (p_j2k->m_current_tile_number == l_nb_tiles) {
9707                         *p_go_on = OPJ_FALSE;
9708                         return OPJ_TRUE;
9709                 }
9710         }
9711
9712         /*FIXME ???*/
9713         if (! tcd_init_decode_tile(p_j2k->m_tcd, p_j2k->m_current_tile_number)) {
9714                 opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot decode tile, memory error\n");
9715                 return OPJ_FALSE;
9716         }
9717
9718         opj_event_msg_v2(p_manager, EVT_INFO, "Header of tile %d / %d has been read.\n",
9719                         p_j2k->m_current_tile_number, (p_j2k->m_cp.th * p_j2k->m_cp.tw) - 1);
9720
9721         *p_tile_index = p_j2k->m_current_tile_number;
9722         *p_go_on = OPJ_TRUE;
9723         *p_data_size = tcd_get_decoded_tile_size(p_j2k->m_tcd);
9724         *p_tile_x0 = p_j2k->m_tcd->tcd_image->tiles->x0;
9725         *p_tile_y0 = p_j2k->m_tcd->tcd_image->tiles->y0;
9726         *p_tile_x1 = p_j2k->m_tcd->tcd_image->tiles->x1;
9727         *p_tile_y1 = p_j2k->m_tcd->tcd_image->tiles->y1;
9728         *p_nb_comps = p_j2k->m_tcd->tcd_image->tiles->numcomps;
9729
9730          p_j2k->m_specific_param.m_decoder.m_state |= 0x0080;/* FIXME J2K_DEC_STATE_DATA;*/
9731
9732         return OPJ_TRUE;
9733 }
9734
9735
9736 opj_bool j2k_decode_tile (      opj_j2k_v2_t * p_j2k,
9737                                                         OPJ_UINT32 p_tile_index,
9738                                                         OPJ_BYTE * p_data,
9739                                                         OPJ_UINT32 p_data_size,
9740                                                         opj_stream_private_t *p_stream,
9741                                                         opj_event_mgr_t * p_manager )
9742 {
9743         OPJ_UINT32 l_current_marker;
9744         OPJ_BYTE l_data [2];
9745         opj_tcp_v2_t * l_tcp;
9746
9747         /* preconditions */
9748         assert(p_stream != 00);
9749         assert(p_j2k != 00);
9750         assert(p_manager != 00);
9751
9752         if ( !(p_j2k->m_specific_param.m_decoder.m_state & 0x0080/*FIXME J2K_DEC_STATE_DATA*/)
9753                 || (p_tile_index != p_j2k->m_current_tile_number) ) {
9754                 return OPJ_FALSE;
9755         }
9756
9757         l_tcp = &(p_j2k->m_cp.tcps[p_tile_index]);
9758         if (! l_tcp->m_data) {
9759                 j2k_tcp_destroy(l_tcp);
9760                 return OPJ_FALSE;
9761         }
9762
9763         if (! tcd_decode_tile_v2(       p_j2k->m_tcd,
9764                                                                 l_tcp->m_data,
9765                                                                 l_tcp->m_data_size,
9766                                                                 p_tile_index,
9767                                                                 p_j2k->cstr_index) ) {
9768                 j2k_tcp_destroy(l_tcp);
9769                 p_j2k->m_specific_param.m_decoder.m_state |= 0x8000;/*FIXME J2K_DEC_STATE_ERR;*/
9770                 return OPJ_FALSE;
9771         }
9772
9773         if (! tcd_update_tile_data(p_j2k->m_tcd,p_data,p_data_size)) {
9774                 return OPJ_FALSE;
9775         }
9776
9777         /* To avoid to destroy the tcp which can be useful when we try to decode a tile decoded before (cf j2k_random_tile_access)
9778          * we destroy just the data which will be re-read in read_tile_header*/
9779         /*j2k_tcp_destroy(l_tcp);
9780         p_j2k->m_tcd->tcp = 0;*/
9781         j2k_tcp_data_destroy(l_tcp);
9782
9783         p_j2k->m_specific_param.m_decoder.m_can_decode = 0;
9784         p_j2k->m_specific_param.m_decoder.m_state &= (~ (0x0080));/* FIXME J2K_DEC_STATE_DATA);*/
9785
9786         if (p_j2k->m_specific_param.m_decoder.m_state != 0x0100){ /*FIXME J2K_DEC_STATE_EOC)*/
9787                 if (opj_stream_read_data(p_stream,l_data,2,p_manager) != 2) {
9788                         opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
9789                         return OPJ_FALSE;
9790                 }
9791
9792                 opj_read_bytes(l_data,&l_current_marker,2);
9793
9794                 if (l_current_marker == J2K_MS_EOC) {
9795                         p_j2k->m_current_tile_number = 0;
9796                         p_j2k->m_specific_param.m_decoder.m_state =  0x0100;/*FIXME J2K_DEC_STATE_EOC;*/
9797                 }
9798                 else if (l_current_marker != J2K_MS_SOT)
9799                 {
9800                         opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short, expected SOT\n");
9801                         return OPJ_FALSE;
9802                 }
9803         }
9804
9805         return OPJ_TRUE;
9806 }
9807
9808
9809 opj_bool j2k_update_image_data (opj_tcd_v2_t * p_tcd, OPJ_BYTE * p_data, opj_image_t* p_output_image)
9810 {
9811         OPJ_UINT32 i,j,k = 0;
9812         OPJ_UINT32 l_width_src,l_height_src;
9813         OPJ_UINT32 l_width_dest,l_height_dest;
9814         OPJ_INT32 l_offset_x0_src, l_offset_y0_src, l_offset_x1_src, l_offset_y1_src;
9815         OPJ_INT32 l_start_offset_src, l_line_offset_src, l_end_offset_src ;
9816         OPJ_UINT32 l_start_x_dest , l_start_y_dest;
9817         OPJ_UINT32 l_x0_dest, l_y0_dest, l_x1_dest, l_y1_dest;
9818         OPJ_INT32 l_start_offset_dest, l_line_offset_dest;
9819
9820         opj_image_comp_t * l_img_comp_src = 00;
9821         opj_image_comp_t * l_img_comp_dest = 00;
9822
9823         opj_tcd_tilecomp_v2_t * l_tilec = 00;
9824         opj_image_t * l_image_src = 00;
9825         OPJ_UINT32 l_size_comp, l_remaining;
9826         OPJ_INT32 * l_dest_ptr;
9827         opj_tcd_resolution_v2_t* l_res= 00;
9828
9829         l_tilec = p_tcd->tcd_image->tiles->comps;
9830         l_image_src = p_tcd->image;
9831         l_img_comp_src = l_image_src->comps;
9832
9833         l_img_comp_dest = p_output_image->comps;
9834
9835         for (i=0; i<l_image_src->numcomps; i++) {
9836
9837                 /* Allocate output component buffer if necessary */
9838                 if (!l_img_comp_dest->data) {
9839
9840                         l_img_comp_dest->data = (OPJ_INT32*) opj_calloc(l_img_comp_dest->w * l_img_comp_dest->h, sizeof(OPJ_INT32));
9841                         if (! l_img_comp_dest->data) {
9842                                 return OPJ_FALSE;
9843                         }
9844                 }
9845
9846                 /* Copy info from decoded comp image to output image */
9847                 l_img_comp_dest->resno_decoded = l_img_comp_src->resno_decoded;
9848
9849                 /*-----*/
9850                 /* Compute the precision of the output buffer */
9851                 l_size_comp = l_img_comp_src->prec >> 3; /*(/ 8)*/
9852                 l_remaining = l_img_comp_src->prec & 7;  /* (%8) */
9853                 l_res = l_tilec->resolutions + l_img_comp_src->resno_decoded;
9854
9855                 if (l_remaining) {
9856                         ++l_size_comp;
9857                 }
9858
9859                 if (l_size_comp == 3) {
9860                         l_size_comp = 4;
9861                 }
9862                 /*-----*/
9863
9864                 /* Current tile component size*/
9865                 /*if (i == 0) {
9866                 fprintf(stdout, "SRC: l_res_x0=%d, l_res_x1=%d, l_res_y0=%d, l_res_y1=%d\n",
9867                                 l_res->x0, l_res->x1, l_res->y0, l_res->y1);
9868                 }*/
9869
9870                 l_width_src = (l_res->x1 - l_res->x0);
9871                 l_height_src = (l_res->y1 - l_res->y0);
9872
9873                 /* Border of the current output component*/
9874                 l_x0_dest = int_ceildivpow2(l_img_comp_dest->x0, l_img_comp_dest->factor);
9875                 l_y0_dest = int_ceildivpow2(l_img_comp_dest->y0, l_img_comp_dest->factor);
9876                 l_x1_dest = l_x0_dest + l_img_comp_dest->w;
9877                 l_y1_dest = l_y0_dest + l_img_comp_dest->h;
9878
9879                 /*if (i == 0) {
9880                 fprintf(stdout, "DEST: l_x0_dest=%d, l_x1_dest=%d, l_y0_dest=%d, l_y1_dest=%d (%d)\n",
9881                                 l_x0_dest, l_x1_dest, l_y0_dest, l_y1_dest, l_img_comp_dest->factor );
9882                 }*/
9883
9884                 /*-----*/
9885                 /* Compute the area (l_offset_x0_src, l_offset_y0_src, l_offset_x1_src, l_offset_y1_src)
9886                  * of the input buffer (decoded tile component) which will be move
9887                  * in the output buffer. Compute the area of the output buffer (l_start_x_dest,
9888                  * l_start_y_dest, l_width_dest, l_height_dest)  which will be modified
9889                  * by this input area.
9890                  * */
9891                 assert( l_res->x0 >= 0);
9892                 assert( l_res->x1 >= 0);
9893                 if ( l_x0_dest < (OPJ_UINT32)l_res->x0 ) {
9894                         l_start_x_dest = l_res->x0 - l_x0_dest;
9895                         l_offset_x0_src = 0;
9896
9897                         if ( l_x1_dest >= (OPJ_UINT32)l_res->x1 ) {
9898                                 l_width_dest = l_width_src;
9899                                 l_offset_x1_src = 0;
9900                         }
9901                         else {
9902                                 l_width_dest = l_x1_dest - l_res->x0 ;
9903                                 l_offset_x1_src = l_width_src - l_width_dest;
9904                         }
9905                 }
9906                 else {
9907                         l_start_x_dest = 0 ;
9908                         l_offset_x0_src = l_x0_dest - l_res->x0;
9909
9910                         if ( l_x1_dest >= (OPJ_UINT32)l_res->x1 ) {
9911                                 l_width_dest = l_width_src - l_offset_x0_src;
9912                                 l_offset_x1_src = 0;
9913                         }
9914                         else {
9915                                 l_width_dest = l_img_comp_dest->w ;
9916                                 l_offset_x1_src = l_res->x1 - l_x1_dest;
9917                         }
9918                 }
9919
9920                 if ( l_y0_dest < (OPJ_UINT32)l_res->y0 ) {
9921                         l_start_y_dest = l_res->y0 - l_y0_dest;
9922                         l_offset_y0_src = 0;
9923
9924                         if ( l_y1_dest >= (OPJ_UINT32)l_res->y1 ) {
9925                                 l_height_dest = l_height_src;
9926                                 l_offset_y1_src = 0;
9927                         }
9928                         else {
9929                                 l_height_dest = l_y1_dest - l_res->y0 ;
9930                                 l_offset_y1_src =  l_height_src - l_height_dest;
9931                         }
9932                 }
9933                 else {
9934                         l_start_y_dest = 0 ;
9935                         l_offset_y0_src = l_y0_dest - l_res->y0;
9936
9937                         if ( l_y1_dest >= (OPJ_UINT32)l_res->y1 ) {
9938                                 l_height_dest = l_height_src - l_offset_y0_src;
9939                                 l_offset_y1_src = 0;
9940                         }
9941                         else {
9942                                 l_height_dest = l_img_comp_dest->h ;
9943                                 l_offset_y1_src = l_res->y1 - l_y1_dest;
9944                         }
9945                 }
9946
9947                 if( (l_offset_x0_src < 0 ) || (l_offset_y0_src < 0 ) || (l_offset_x1_src < 0 ) || (l_offset_y1_src < 0 ) ){
9948                         return OPJ_FALSE;
9949                 }
9950                 /*-----*/
9951
9952                 /* Compute the input buffer offset */
9953                 l_start_offset_src = l_offset_x0_src + l_offset_y0_src * l_width_src;
9954                 l_line_offset_src = l_offset_x1_src + l_offset_x0_src;
9955                 l_end_offset_src = l_offset_y1_src * l_width_src - l_offset_x0_src;
9956
9957                 /* Compute the output buffer offset */
9958                 l_start_offset_dest = l_start_x_dest + l_start_y_dest * l_img_comp_dest->w;
9959                 l_line_offset_dest = l_img_comp_dest->w - l_width_dest;
9960
9961                 /* Move the output buffer to the first place where we will write*/
9962                 l_dest_ptr = l_img_comp_dest->data + l_start_offset_dest;
9963
9964                 /*if (i == 0) {
9965                         fprintf(stdout, "COMPO[%d]:\n",i);
9966                         fprintf(stdout, "SRC: l_start_x_src=%d, l_start_y_src=%d, l_width_src=%d, l_height_src=%d\n"
9967                                         "\t tile offset:%d, %d, %d, %d\n"
9968                                         "\t buffer offset: %d; %d, %d\n",
9969                                         l_res->x0, l_res->y0, l_width_src, l_height_src,
9970                                         l_offset_x0_src, l_offset_y0_src, l_offset_x1_src, l_offset_y1_src,
9971                                         l_start_offset_src, l_line_offset_src, l_end_offset_src);
9972
9973                         fprintf(stdout, "DEST: l_start_x_dest=%d, l_start_y_dest=%d, l_width_dest=%d, l_height_dest=%d\n"
9974                                         "\t start offset: %d, line offset= %d\n",
9975                                         l_start_x_dest, l_start_y_dest, l_width_dest, l_height_dest, l_start_offset_dest, l_line_offset_dest);
9976                 }*/
9977
9978
9979                 switch (l_size_comp) {
9980                         case 1:
9981                                 {
9982                                         OPJ_CHAR * l_src_ptr = (OPJ_CHAR*) p_data;
9983                                         l_src_ptr += l_start_offset_src; /* Move to the first place where we will read*/
9984
9985                                         if (l_img_comp_src->sgnd) {
9986                                                 for (j = 0 ; j < l_height_dest ; ++j) {
9987                                                         for ( k = 0 ; k < l_width_dest ; ++k) {
9988                                                                 *(l_dest_ptr++) = (OPJ_INT32) (*(l_src_ptr++)); /* Copy only the data needed for the output image */
9989                                                         }
9990
9991                                                         l_dest_ptr+= l_line_offset_dest; /* Move to the next place where we will write */
9992                                                         l_src_ptr += l_line_offset_src ; /* Move to the next place where we will read */
9993                                                 }
9994                                         }
9995                                         else {
9996                                                 for ( j = 0 ; j < l_height_dest ; ++j ) {
9997                                                         for ( k = 0 ; k < l_width_dest ; ++k) {
9998                                                                 *(l_dest_ptr++) = (OPJ_INT32) ((*(l_src_ptr++))&0xff);
9999                                                         }
10000
10001                                                         l_dest_ptr+= l_line_offset_dest;
10002                                                         l_src_ptr += l_line_offset_src;
10003                                                 }
10004                                         }
10005
10006                                         l_src_ptr += l_end_offset_src; /* Move to the end of this component-part of the input buffer */
10007                                         p_data = (OPJ_BYTE*) l_src_ptr; /* Keep the current position for the next component-part */
10008                                 }
10009                                 break;
10010                         case 2:
10011                                 {
10012                                         OPJ_INT16 * l_src_ptr = (OPJ_INT16 *) p_data;
10013                                         l_src_ptr += l_start_offset_src;
10014
10015                                         if (l_img_comp_src->sgnd) {
10016                                                 for (j=0;j<l_height_dest;++j) {
10017                                                         for (k=0;k<l_width_dest;++k) {
10018                                                                 *(l_dest_ptr++) = *(l_src_ptr++);
10019                                                         }
10020
10021                                                         l_dest_ptr+= l_line_offset_dest;
10022                                                         l_src_ptr += l_line_offset_src ;
10023                                                 }
10024                                         }
10025                                         else {
10026                                                 for (j=0;j<l_height_dest;++j) {
10027                                                         for (k=0;k<l_width_dest;++k) {
10028                                                                 *(l_dest_ptr++) = (*(l_src_ptr++))&0xffff;
10029                                                         }
10030
10031                                                         l_dest_ptr+= l_line_offset_dest;
10032                                                         l_src_ptr += l_line_offset_src ;
10033                                                 }
10034                                         }
10035
10036                                         l_src_ptr += l_end_offset_src;
10037                                         p_data = (OPJ_BYTE*) l_src_ptr;
10038                                 }
10039                                 break;
10040                         case 4:
10041                                 {
10042                                         OPJ_INT32 * l_src_ptr = (OPJ_INT32 *) p_data;
10043                                         l_src_ptr += l_start_offset_src;
10044
10045                                         for (j=0;j<l_height_dest;++j) {
10046                                                 for (k=0;k<l_width_dest;++k) {
10047                                                         *(l_dest_ptr++) = (*(l_src_ptr++));
10048                                                 }
10049
10050                                                 l_dest_ptr+= l_line_offset_dest;
10051                                                 l_src_ptr += l_line_offset_src ;
10052                                         }
10053
10054                                         l_src_ptr += l_end_offset_src;
10055                                         p_data = (OPJ_BYTE*) l_src_ptr;
10056                                 }
10057                                 break;
10058                 }
10059
10060                 ++l_img_comp_dest;
10061                 ++l_img_comp_src;
10062                 ++l_tilec;
10063         }
10064
10065         return OPJ_TRUE;
10066 }
10067
10068 /**
10069  * Sets the given area to be decoded. This function should be called right after opj_read_header and before any tile header reading.
10070  *
10071  * @param       p_j2k                   the jpeg2000 codec.
10072  * @param       p_start_x               the left position of the rectangle to decode (in image coordinates).
10073  * @param       p_end_x                 the right position of the rectangle to decode (in image coordinates).
10074  * @param       p_start_y               the up position of the rectangle to decode (in image coordinates).
10075  * @param       p_end_y                 the bottom position of the rectangle to decode (in image coordinates).
10076  * @param       p_manager               the user event manager
10077  *
10078  * @return      true                    if the area could be set.
10079  */
10080 opj_bool j2k_set_decode_area(   opj_j2k_v2_t *p_j2k,
10081                                                                 opj_image_t* p_image,
10082                                                                 OPJ_INT32 p_start_x, OPJ_INT32 p_start_y,
10083                                                                 OPJ_INT32 p_end_x, OPJ_INT32 p_end_y,
10084                                                                 struct opj_event_mgr * p_manager )
10085 {
10086         opj_cp_v2_t * l_cp = &(p_j2k->m_cp);
10087         opj_image_t * l_image = p_j2k->m_private_image;
10088
10089         OPJ_UINT32 it_comp;
10090         OPJ_INT32 l_comp_x1, l_comp_y1;
10091         opj_image_comp_t* l_img_comp = NULL;
10092
10093         /* Check if we are read the main header */
10094         if (p_j2k->m_specific_param.m_decoder.m_state != J2K_STATE_TPHSOT) { /* FIXME J2K_DEC_STATE_TPHSOT)*/
10095                 opj_event_msg_v2(p_manager, EVT_ERROR, "Need to decode the main header before begin to decode the remaining codestream");
10096                 return OPJ_FALSE;
10097         }
10098
10099         if ( !p_start_x && !p_start_y && !p_end_x && !p_end_y){
10100                 opj_event_msg_v2(p_manager, EVT_INFO, "No decoded area parameters, set the decoded area to the whole image\n");
10101
10102                 p_j2k->m_specific_param.m_decoder.m_start_tile_x = 0;
10103                 p_j2k->m_specific_param.m_decoder.m_start_tile_y = 0;
10104                 p_j2k->m_specific_param.m_decoder.m_end_tile_x = l_cp->tw;
10105                 p_j2k->m_specific_param.m_decoder.m_end_tile_y = l_cp->th;
10106
10107                 return OPJ_TRUE;
10108         }
10109
10110         /* ----- */
10111         /* Check if the positions provided by the user are correct */
10112
10113         /* Left */
10114         assert(p_start_x >= 0 );
10115         assert(p_start_y >= 0 );
10116
10117         if ((OPJ_UINT32)p_start_x > l_image->x1 ) {
10118                 opj_event_msg_v2(p_manager, EVT_ERROR,
10119                         "Left position of the decoded area (region_x0=%d) is outside the image area (Xsiz=%d).\n",
10120                         p_start_x, l_image->x1);
10121                 return OPJ_FALSE;
10122         }
10123         else if ((OPJ_UINT32)p_start_x < l_image->x0){
10124                 opj_event_msg_v2(p_manager, EVT_WARNING,
10125                                 "Left position of the decoded area (region_x0=%d) is outside the image area (XOsiz=%d).\n",
10126                                 p_start_x, l_image->x0);
10127                 p_j2k->m_specific_param.m_decoder.m_start_tile_x = 0;
10128                 p_image->x0 = l_image->x0;
10129         }
10130         else {
10131                 p_j2k->m_specific_param.m_decoder.m_start_tile_x = (p_start_x - l_cp->tx0) / l_cp->tdx;
10132                 p_image->x0 = p_start_x;
10133         }
10134
10135         /* Up */
10136         if ((OPJ_UINT32)p_start_y > l_image->y1){
10137                 opj_event_msg_v2(p_manager, EVT_ERROR,
10138                                 "Up position of the decoded area (region_y0=%d) is outside the image area (Ysiz=%d).\n",
10139                                 p_start_y, l_image->y1);
10140                 return OPJ_FALSE;
10141         }
10142         else if ((OPJ_UINT32)p_start_y < l_image->y0){
10143                 opj_event_msg_v2(p_manager, EVT_WARNING,
10144                                 "Up position of the decoded area (region_y0=%d) is outside the image area (YOsiz=%d).\n",
10145                                 p_start_y, l_image->y0);
10146                 p_j2k->m_specific_param.m_decoder.m_start_tile_y = 0;
10147                 p_image->y0 = l_image->y0;
10148         }
10149         else {
10150                 p_j2k->m_specific_param.m_decoder.m_start_tile_y = (p_start_y - l_cp->ty0) / l_cp->tdy;
10151                 p_image->y0 = p_start_y;
10152         }
10153
10154         /* Right */
10155         assert((OPJ_UINT32)p_end_x > 0);
10156         assert((OPJ_UINT32)p_end_y > 0);
10157         if ((OPJ_UINT32)p_end_x < l_image->x0) {
10158                 opj_event_msg_v2(p_manager, EVT_ERROR,
10159                         "Right position of the decoded area (region_x1=%d) is outside the image area (XOsiz=%d).\n",
10160                         p_end_x, l_image->x0);
10161                 return OPJ_FALSE;
10162         }
10163         else if ((OPJ_UINT32)p_end_x > l_image->x1) {
10164                 opj_event_msg_v2(p_manager, EVT_WARNING,
10165                         "Right position of the decoded area (region_x1=%d) is outside the image area (Xsiz=%d).\n",
10166                         p_end_x, l_image->x1);
10167                 p_j2k->m_specific_param.m_decoder.m_end_tile_x = l_cp->tw;
10168                 p_image->x1 = l_image->x1;
10169         }
10170         else {
10171                 p_j2k->m_specific_param.m_decoder.m_end_tile_x = int_ceildiv((p_end_x - l_cp->tx0), l_cp->tdx);
10172                 p_image->x1 = p_end_x;
10173         }
10174
10175         /* Bottom */
10176         if ((OPJ_UINT32)p_end_y < l_image->y0) {
10177                 opj_event_msg_v2(p_manager, EVT_ERROR,
10178                         "Bottom position of the decoded area (region_y1=%d) is outside the image area (YOsiz=%d).\n",
10179                         p_end_y, l_image->y0);
10180                 return OPJ_FALSE;
10181         }
10182         if ((OPJ_UINT32)p_end_y > l_image->y1){
10183                 opj_event_msg_v2(p_manager, EVT_WARNING,
10184                         "Bottom position of the decoded area (region_y1=%d) is outside the image area (Ysiz=%d).\n",
10185                         p_end_y, l_image->y1);
10186                 p_j2k->m_specific_param.m_decoder.m_end_tile_y = l_cp->th;
10187                 p_image->y1 = l_image->y1;
10188         }
10189         else{
10190                 p_j2k->m_specific_param.m_decoder.m_end_tile_y = int_ceildiv((p_end_y - l_cp->ty0), l_cp->tdy);
10191                 p_image->y1 = p_end_y;
10192         }
10193         /* ----- */
10194
10195         p_j2k->m_specific_param.m_decoder.m_discard_tiles = 1;
10196
10197         l_img_comp = p_image->comps;
10198         for (it_comp=0; it_comp < p_image->numcomps; ++it_comp)
10199         {
10200                 OPJ_INT32 l_h,l_w;
10201
10202                 l_img_comp->x0 = int_ceildiv(p_image->x0, l_img_comp->dx);
10203                 l_img_comp->y0 = int_ceildiv(p_image->y0, l_img_comp->dy);
10204                 l_comp_x1 = int_ceildiv(p_image->x1, l_img_comp->dx);
10205                 l_comp_y1 = int_ceildiv(p_image->y1, l_img_comp->dy);
10206
10207                 l_w = int_ceildivpow2(l_comp_x1, l_img_comp->factor)
10208                                 - int_ceildivpow2(l_img_comp->x0, l_img_comp->factor);
10209                 if (l_w < 0){
10210                         opj_event_msg_v2(p_manager, EVT_ERROR,
10211                                 "Size x of the decoded component image is incorrect (comp[%d].w=%d).\n",
10212                                 it_comp, l_w);
10213                         return OPJ_FALSE;
10214                 }
10215                 l_img_comp->w = l_w;
10216
10217                 l_h = int_ceildivpow2(l_comp_y1, l_img_comp->factor)
10218                                 - int_ceildivpow2(l_img_comp->y0, l_img_comp->factor);
10219                 if (l_h < 0){
10220                         opj_event_msg_v2(p_manager, EVT_ERROR,
10221                                 "Size y of the decoded component image is incorrect (comp[%d].h=%d).\n",
10222                                 it_comp, l_h);
10223                         return OPJ_FALSE;
10224                 }
10225                 l_img_comp->h = l_h;
10226
10227                 l_img_comp++;
10228         }
10229
10230         opj_event_msg_v2( p_manager, EVT_INFO,"Setting decoding area to %d,%d,%d,%d\n",
10231                         p_image->x0, p_image->y0, p_image->x1, p_image->y1);
10232
10233
10234         return OPJ_TRUE;
10235 }
10236
10237
10238 /* ----------------------------------------------------------------------- */
10239 /* J2K / JPT decoder interface                                             */
10240 /* ----------------------------------------------------------------------- */
10241 /**
10242  * Creates a J2K decompression structure.
10243  *
10244  * @return a handle to a J2K decompressor if successful, NULL otherwise.
10245 */
10246 opj_j2k_v2_t* opj_j2k_create_decompress(void)
10247 {
10248         opj_j2k_v2_t *l_j2k = (opj_j2k_v2_t*) opj_malloc(sizeof(opj_j2k_v2_t));
10249         if (!l_j2k) {
10250                 return 00;
10251         }
10252         memset(l_j2k,0,sizeof(opj_j2k_v2_t));
10253
10254         l_j2k->m_is_decoder = 1;
10255         l_j2k->m_cp.m_is_decoder = 1;
10256
10257         l_j2k->m_specific_param.m_decoder.m_default_tcp = (opj_tcp_v2_t*) opj_malloc(sizeof(opj_tcp_v2_t));
10258         if (!l_j2k->m_specific_param.m_decoder.m_default_tcp) {
10259                 j2k_destroy(l_j2k);
10260                 return 00;
10261         }
10262         memset(l_j2k->m_specific_param.m_decoder.m_default_tcp,0,sizeof(opj_tcp_v2_t));
10263
10264         l_j2k->m_specific_param.m_decoder.m_header_data = (OPJ_BYTE *) opj_malloc(J2K_DEFAULT_HEADER_SIZE);
10265         if (! l_j2k->m_specific_param.m_decoder.m_header_data) {
10266                 j2k_destroy(l_j2k);
10267                 return 00;
10268         }
10269
10270         l_j2k->m_specific_param.m_decoder.m_header_data_size = J2K_DEFAULT_HEADER_SIZE;
10271
10272         l_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec = -1 ;
10273
10274         l_j2k->m_specific_param.m_decoder.m_last_sot_read_pos = 0 ;
10275
10276         /* codestream index creation */
10277         l_j2k->cstr_index = j2k_create_cstr_index();
10278
10279                         /*(opj_codestream_index_t*) opj_malloc(sizeof(opj_codestream_index_t));
10280         if (!l_j2k->cstr_index){
10281                 j2k_destroy(l_j2k);
10282                 return NULL;
10283         }
10284
10285         l_j2k->cstr_index->marker = (opj_marker_info_t*) opj_malloc(100 * sizeof(opj_marker_info_t));
10286 */
10287
10288         /* validation list creation */
10289         l_j2k->m_validation_list = opj_procedure_list_create();
10290         if (! l_j2k->m_validation_list) {
10291                 j2k_destroy(l_j2k);
10292                 return 00;
10293         }
10294
10295         /* execution list creation */
10296         l_j2k->m_procedure_list = opj_procedure_list_create();
10297         if (! l_j2k->m_procedure_list) {
10298                 j2k_destroy(l_j2k);
10299                 return 00;
10300         }
10301
10302         return l_j2k;
10303 }
10304
10305
10306 opj_codestream_index_t* j2k_create_cstr_index(void)
10307 {
10308         opj_codestream_index_t* cstr_index = (opj_codestream_index_t*)
10309                         opj_calloc(1,sizeof(opj_codestream_index_t));
10310         if (!cstr_index)
10311                 return NULL;
10312
10313         cstr_index->maxmarknum = 100;
10314         cstr_index->marknum = 0;
10315         cstr_index->marker = (opj_marker_info_t*)
10316                         opj_calloc(cstr_index->maxmarknum, sizeof(opj_marker_info_t));
10317         if (!cstr_index-> marker)
10318                 return NULL;
10319
10320         cstr_index->tile_index = NULL;
10321
10322         return cstr_index;
10323 }
10324
10325
10326 /**
10327  * Gets the size taken by writing a SPCod or SPCoc for the given tile and component.
10328  *
10329  * @param       p_tile_no               the tile index.
10330  * @param       p_comp_no               the component being outputted.
10331  * @param       p_j2k                   the J2K codec.
10332  *
10333  * @return      the number of bytes taken by the SPCod element.
10334  */
10335 OPJ_UINT32 j2k_get_SPCod_SPCoc_size (   opj_j2k_v2_t *p_j2k,
10336                                                                                 OPJ_UINT32 p_tile_no,
10337                                                                                 OPJ_UINT32 p_comp_no )
10338 {
10339         opj_cp_v2_t *l_cp = 00;
10340         opj_tcp_v2_t *l_tcp = 00;
10341         opj_tccp_t *l_tccp = 00;
10342
10343         /* preconditions */
10344         assert(p_j2k != 00);
10345
10346         l_cp = &(p_j2k->m_cp);
10347         l_tcp = &l_cp->tcps[p_tile_no];
10348         l_tccp = &l_tcp->tccps[p_comp_no];
10349
10350         /* preconditions again */
10351         assert(p_tile_no < (l_cp->tw * l_cp->th));
10352         assert(p_comp_no < p_j2k->m_private_image->numcomps);
10353
10354         if (l_tccp->csty & J2K_CCP_CSTY_PRT) {
10355                 return 5 + l_tccp->numresolutions;
10356         }
10357         else {
10358                 return 5;
10359         }
10360 }
10361
10362 /**
10363  * Writes a SPCod or SPCoc element, i.e. the coding style of a given component of a tile.
10364  *
10365  * @param       p_comp_no       the component number to output.
10366  * @param       p_stream                        the stream to write data to.
10367  * @param       p_j2k                   J2K codec.
10368  * @param       p_manager       the user event manager.
10369  *
10370 */
10371 opj_bool j2k_write_SPCod_SPCoc( opj_j2k_v2_t *p_j2k,
10372                                                                 OPJ_UINT32 p_tile_no,
10373                                                                 OPJ_UINT32 p_comp_no,
10374                                                                 OPJ_BYTE * p_data,
10375                                                                 OPJ_UINT32 * p_header_size,
10376                                                                 struct opj_event_mgr * p_manager )
10377 {
10378         OPJ_UINT32 i;
10379         opj_cp_v2_t *l_cp = 00;
10380         opj_tcp_v2_t *l_tcp = 00;
10381         opj_tccp_t *l_tccp = 00;
10382
10383         /* preconditions */
10384         assert(p_j2k != 00);
10385         assert(p_header_size != 00);
10386         assert(p_manager != 00);
10387         assert(p_data != 00);
10388
10389         l_cp = &(p_j2k->m_cp);
10390         l_tcp = &l_cp->tcps[p_tile_no];
10391         l_tccp = &l_tcp->tccps[p_comp_no];
10392
10393         /* preconditions again */
10394         assert(p_tile_no < (l_cp->tw * l_cp->th));
10395         assert(p_comp_no <(p_j2k->m_private_image->numcomps));
10396
10397         if (*p_header_size < 5) {
10398                 opj_event_msg_v2(p_manager, EVT_ERROR, "Error writing SPCod SPCoc element\n");
10399                 return OPJ_FALSE;
10400         }
10401
10402         opj_write_bytes(p_data,l_tccp->numresolutions - 1, 1);  /* SPcoc (D) */
10403         ++p_data;
10404
10405         opj_write_bytes(p_data,l_tccp->cblkw - 2, 1);                   /* SPcoc (E) */
10406         ++p_data;
10407
10408         opj_write_bytes(p_data,l_tccp->cblkh - 2, 1);                   /* SPcoc (F) */
10409         ++p_data;
10410
10411         opj_write_bytes(p_data,l_tccp->cblksty, 1);                             /* SPcoc (G) */
10412         ++p_data;
10413
10414         opj_write_bytes(p_data,l_tccp->qmfbid, 1);                              /* SPcoc (H) */
10415         ++p_data;
10416
10417         *p_header_size = *p_header_size - 5;
10418
10419         if (l_tccp->csty & J2K_CCP_CSTY_PRT) {
10420
10421                 if (*p_header_size < l_tccp->numresolutions) {
10422                         opj_event_msg_v2(p_manager, EVT_ERROR, "Error writting SPCod SPCoc element\n");
10423                         return OPJ_FALSE;
10424                 }
10425
10426                 for (i = 0; i < l_tccp->numresolutions; ++i) {
10427                         opj_write_bytes(p_data,l_tccp->prcw[i] + (l_tccp->prch[i] << 4), 1);    /* SPcoc (I_i) */
10428                         ++p_data;
10429                 }
10430
10431                 *p_header_size = *p_header_size - l_tccp->numresolutions;
10432         }
10433
10434         return OPJ_TRUE;
10435 }
10436
10437 /**
10438  * Reads a SPCod or SPCoc element, i.e. the coding style of a given component of a tile.
10439  * @param       p_header_data   the data contained in the COM box.
10440  * @param       p_j2k                   the jpeg2000 codec.
10441  * @param       p_header_size   the size of the data contained in the COM marker.
10442  * @param       p_manager               the user event manager.
10443 */
10444 opj_bool j2k_read_SPCod_SPCoc(
10445                                                     opj_j2k_v2_t *p_j2k,
10446                                                         OPJ_UINT32 compno,
10447                                                         OPJ_BYTE * p_header_data,
10448                                                         OPJ_UINT32 * p_header_size,
10449                                                         struct opj_event_mgr * p_manager
10450                                                         )
10451 {
10452         OPJ_UINT32 i, l_tmp;
10453         opj_cp_v2_t *l_cp = NULL;
10454         opj_tcp_v2_t *l_tcp = NULL;
10455         opj_tccp_t *l_tccp = NULL;
10456         OPJ_BYTE * l_current_ptr = NULL;
10457
10458         /* preconditions */
10459         assert(p_j2k != 00);
10460         assert(p_manager != 00);
10461         assert(p_header_data != 00);
10462
10463         l_cp = &(p_j2k->m_cp);
10464         l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ?
10465                                 &l_cp->tcps[p_j2k->m_current_tile_number] :
10466                                 p_j2k->m_specific_param.m_decoder.m_default_tcp;
10467
10468         /* precondition again */
10469         assert(compno < p_j2k->m_private_image->numcomps);
10470
10471         l_tccp = &l_tcp->tccps[compno];
10472         l_current_ptr = p_header_data;
10473
10474         /* make sure room is sufficient */
10475         if (*p_header_size < 5) {
10476                 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading SPCod SPCoc element\n");
10477                 return OPJ_FALSE;
10478         }
10479
10480         opj_read_bytes(l_current_ptr, &l_tccp->numresolutions ,1);              /* SPcox (D) */
10481         ++l_tccp->numresolutions;                                                                               /* tccp->numresolutions = read() + 1 */
10482         ++l_current_ptr;
10483
10484         /* If user wants to remove more resolutions than the codestream contains, return error */
10485         if (l_cp->m_specific_param.m_dec.m_reduce >= l_tccp->numresolutions) {
10486                 opj_event_msg_v2(p_manager, EVT_ERROR, "Error decoding component %d.\nThe number of resolutions to remove is higher than the number "
10487                                         "of resolutions of this component\nModify the cp_reduce parameter.\n\n", compno);
10488                 p_j2k->m_specific_param.m_decoder.m_state |= 0x8000;/* FIXME J2K_DEC_STATE_ERR;*/
10489                 return OPJ_FALSE;
10490         }
10491
10492         opj_read_bytes(l_current_ptr,&l_tccp->cblkw ,1);                /* SPcoc (E) */
10493         ++l_current_ptr;
10494         l_tccp->cblkw += 2;
10495
10496         opj_read_bytes(l_current_ptr,&l_tccp->cblkh ,1);                /* SPcoc (F) */
10497         ++l_current_ptr;
10498         l_tccp->cblkh += 2;
10499
10500         opj_read_bytes(l_current_ptr,&l_tccp->cblksty ,1);              /* SPcoc (G) */
10501         ++l_current_ptr;
10502
10503         opj_read_bytes(l_current_ptr,&l_tccp->qmfbid ,1);               /* SPcoc (H) */
10504         ++l_current_ptr;
10505
10506         *p_header_size = *p_header_size - 5;
10507
10508         /* use custom precinct size ? */
10509         if (l_tccp->csty & J2K_CCP_CSTY_PRT) {
10510                 if (*p_header_size < l_tccp->numresolutions) {
10511                         opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading SPCod SPCoc element\n");
10512                         return OPJ_FALSE;
10513                 }
10514
10515                 for     (i = 0; i < l_tccp->numresolutions; ++i) {
10516                         opj_read_bytes(l_current_ptr,&l_tmp ,1);                /* SPcoc (I_i) */
10517                         ++l_current_ptr;
10518                         l_tccp->prcw[i] = l_tmp & 0xf;
10519                         l_tccp->prch[i] = l_tmp >> 4;
10520                 }
10521
10522                 *p_header_size = *p_header_size - l_tccp->numresolutions;
10523         }
10524         else {
10525                 /* set default size for the precinct width and height */
10526                 for     (i = 0; i < l_tccp->numresolutions; ++i) {
10527                         l_tccp->prcw[i] = 15;
10528                         l_tccp->prch[i] = 15;
10529                 }
10530         }
10531
10532 #ifdef WIP_REMOVE_MSD
10533         /* INDEX >> */
10534         if (p_j2k->cstr_info && compno == 0) {
10535                 OPJ_UINT32 l_data_size = l_tccp->numresolutions * sizeof(OPJ_UINT32);
10536
10537                 p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].tccp_info[compno].cblkh = l_tccp->cblkh;
10538                 p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].tccp_info[compno].cblkw = l_tccp->cblkw;
10539                 p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].tccp_info[compno].numresolutions = l_tccp->numresolutions;
10540                 p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].tccp_info[compno].cblksty = l_tccp->cblksty;
10541                 p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].tccp_info[compno].qmfbid = l_tccp->qmfbid;
10542
10543
10544                 memcpy(p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].pdx,l_tccp->prcw, l_data_size);
10545                 memcpy(p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].pdy,l_tccp->prch, l_data_size);
10546         }
10547         /* << INDEX */
10548 #endif
10549
10550         return OPJ_TRUE;
10551 }
10552
10553 /**
10554  * Copies the tile component parameters of all the component from the first tile component.
10555  *
10556  * @param               p_j2k           the J2k codec.
10557  */
10558 void j2k_copy_tile_component_parameters( opj_j2k_v2_t *p_j2k )
10559 {
10560         /* loop */
10561         OPJ_UINT32 i;
10562         opj_cp_v2_t *l_cp = NULL;
10563         opj_tcp_v2_t *l_tcp = NULL;
10564         opj_tccp_t *l_ref_tccp = NULL, *l_copied_tccp = NULL;
10565         OPJ_UINT32 l_prc_size;
10566
10567         /* preconditions */
10568         assert(p_j2k != 00);
10569
10570         l_cp = &(p_j2k->m_cp);
10571         l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ? /* FIXME J2K_DEC_STATE_TPH*/
10572                                 &l_cp->tcps[p_j2k->m_current_tile_number] :
10573                                 p_j2k->m_specific_param.m_decoder.m_default_tcp;
10574
10575         l_ref_tccp = &l_tcp->tccps[0];
10576         l_copied_tccp = l_ref_tccp + 1;
10577         l_prc_size = l_ref_tccp->numresolutions * sizeof(OPJ_UINT32);
10578
10579         for     (i=1; i<p_j2k->m_private_image->numcomps; ++i) {
10580                 l_copied_tccp->numresolutions = l_ref_tccp->numresolutions;
10581                 l_copied_tccp->cblkw = l_ref_tccp->cblkw;
10582                 l_copied_tccp->cblkh = l_ref_tccp->cblkh;
10583                 l_copied_tccp->cblksty = l_ref_tccp->cblksty;
10584                 l_copied_tccp->qmfbid = l_ref_tccp->qmfbid;
10585                 memcpy(l_copied_tccp->prcw,l_ref_tccp->prcw,l_prc_size);
10586                 memcpy(l_copied_tccp->prch,l_ref_tccp->prch,l_prc_size);
10587                 ++l_copied_tccp;
10588         }
10589 }
10590
10591 /**
10592  * Gets the size taken by writing SQcd or SQcc element, i.e. the quantization values of a band in the QCD or QCC.
10593  *
10594  * @param       p_tile_no               the tile index.
10595  * @param       p_comp_no               the component being outputted.
10596  * @param       p_j2k                   the J2K codec.
10597  *
10598  * @return      the number of bytes taken by the SPCod element.
10599  */
10600 OPJ_UINT32 j2k_get_SQcd_SQcc_size (     opj_j2k_v2_t *p_j2k,
10601                                                                         OPJ_UINT32 p_tile_no,
10602                                                                         OPJ_UINT32 p_comp_no )
10603 {
10604         OPJ_UINT32 l_num_bands;
10605
10606         opj_cp_v2_t *l_cp = 00;
10607         opj_tcp_v2_t *l_tcp = 00;
10608         opj_tccp_t *l_tccp = 00;
10609
10610         /* preconditions */
10611         assert(p_j2k != 00);
10612
10613         l_cp = &(p_j2k->m_cp);
10614         l_tcp = &l_cp->tcps[p_tile_no];
10615         l_tccp = &l_tcp->tccps[p_comp_no];
10616
10617         /* preconditions again */
10618         assert(p_tile_no < l_cp->tw * l_cp->th);
10619         assert(p_comp_no < p_j2k->m_private_image->numcomps);
10620
10621         l_num_bands = (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) ? 1 : (l_tccp->numresolutions * 3 - 2);
10622
10623         if (l_tccp->qntsty == J2K_CCP_QNTSTY_NOQNT)  {
10624                 return 1 + l_num_bands;
10625         }
10626         else {
10627                 return 1 + 2*l_num_bands;
10628         }
10629 }
10630
10631 /**
10632  * Writes a SQcd or SQcc element, i.e. the quantization values of a band.
10633  *
10634  * @param       p_tile_no               the tile to output.
10635  * @param       p_comp_no               the component number to output.
10636  * @param       p_data                  the data buffer.
10637  * @param       p_header_size   pointer to the size of the data buffer, it is changed by the function.
10638  * @param       p_j2k                           J2K codec.
10639  * @param       p_manager               the user event manager.
10640  *
10641 */
10642 opj_bool j2k_write_SQcd_SQcc(   opj_j2k_v2_t *p_j2k,
10643                                                                 OPJ_UINT32 p_tile_no,
10644                                                                 OPJ_UINT32 p_comp_no,
10645                                                                 OPJ_BYTE * p_data,
10646                                                                 OPJ_UINT32 * p_header_size,
10647                                                                 struct opj_event_mgr * p_manager )
10648 {
10649         OPJ_UINT32 l_header_size;
10650         OPJ_UINT32 l_band_no, l_num_bands;
10651         OPJ_UINT32 l_expn,l_mant;
10652
10653         opj_cp_v2_t *l_cp = 00;
10654         opj_tcp_v2_t *l_tcp = 00;
10655         opj_tccp_t *l_tccp = 00;
10656
10657         /* preconditions */
10658         assert(p_j2k != 00);
10659         assert(p_header_size != 00);
10660         assert(p_manager != 00);
10661         assert(p_data != 00);
10662
10663         l_cp = &(p_j2k->m_cp);
10664         l_tcp = &l_cp->tcps[p_tile_no];
10665         l_tccp = &l_tcp->tccps[p_comp_no];
10666
10667         /* preconditions again */
10668         assert(p_tile_no < l_cp->tw * l_cp->th);
10669         assert(p_comp_no <p_j2k->m_private_image->numcomps);
10670
10671         l_num_bands = (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) ? 1 : (l_tccp->numresolutions * 3 - 2);
10672
10673         if (l_tccp->qntsty == J2K_CCP_QNTSTY_NOQNT)  {
10674                 l_header_size = 1 + l_num_bands;
10675
10676                 if (*p_header_size < l_header_size) {
10677                         opj_event_msg_v2(p_manager, EVT_ERROR, "Error writing SQcd SQcc element\n");
10678                         return OPJ_FALSE;
10679                 }
10680
10681                 opj_write_bytes(p_data,l_tccp->qntsty + (l_tccp->numgbits << 5), 1);    /* Sqcx */
10682                 ++p_data;
10683
10684                 for (l_band_no = 0; l_band_no < l_num_bands; ++l_band_no) {
10685                         l_expn = l_tccp->stepsizes[l_band_no].expn;
10686                         opj_write_bytes(p_data, l_expn << 3, 1);        /* SPqcx_i */
10687                         ++p_data;
10688                 }
10689         }
10690         else {
10691                 l_header_size = 1 + 2*l_num_bands;
10692
10693                 if (*p_header_size < l_header_size) {
10694                         opj_event_msg_v2(p_manager, EVT_ERROR, "Error writing SQcd SQcc element\n");
10695                         return OPJ_FALSE;
10696                 }
10697
10698                 opj_write_bytes(p_data,l_tccp->qntsty + (l_tccp->numgbits << 5), 1);    /* Sqcx */
10699                 ++p_data;
10700
10701                 for (l_band_no = 0; l_band_no < l_num_bands; ++l_band_no) {
10702                         l_expn = l_tccp->stepsizes[l_band_no].expn;
10703                         l_mant = l_tccp->stepsizes[l_band_no].mant;
10704
10705                         opj_write_bytes(p_data, (l_expn << 11) + l_mant, 2);    /* SPqcx_i */
10706                         p_data += 2;
10707                 }
10708         }
10709
10710         *p_header_size = *p_header_size - l_header_size;
10711
10712         return OPJ_TRUE;
10713 }
10714
10715 /**
10716  * Reads a SQcd or SQcc element, i.e. the quantization values of a band.
10717  *
10718  * @param       p_comp_no               the component being targeted.
10719  * @param       p_header_data   the data contained in the COM box.
10720  * @param       p_j2k                   the jpeg2000 codec.
10721  * @param       p_header_size   the size of the data contained in the COM marker.
10722  * @param       p_manager               the user event manager.
10723 */
10724 opj_bool j2k_read_SQcd_SQcc(
10725                                                         opj_j2k_v2_t *p_j2k,
10726                                                         OPJ_UINT32 p_comp_no,
10727                                                         OPJ_BYTE* p_header_data,
10728                                                         OPJ_UINT32 * p_header_size,
10729                                                         struct opj_event_mgr * p_manager
10730                                                         )
10731 {
10732         /* loop*/
10733         OPJ_UINT32 l_band_no;
10734         opj_cp_v2_t *l_cp = 00;
10735         opj_tcp_v2_t *l_tcp = 00;
10736         opj_tccp_t *l_tccp = 00;
10737         OPJ_BYTE * l_current_ptr = 00;
10738         OPJ_UINT32 l_tmp, l_num_band;
10739
10740         /* preconditions*/
10741         assert(p_j2k != 00);
10742         assert(p_manager != 00);
10743         assert(p_header_data != 00);
10744
10745         l_cp = &(p_j2k->m_cp);
10746         /* come from tile part header or main header ?*/
10747         l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ? /*FIXME J2K_DEC_STATE_TPH*/
10748                                 &l_cp->tcps[p_j2k->m_current_tile_number] :
10749                                 p_j2k->m_specific_param.m_decoder.m_default_tcp;
10750
10751         /* precondition again*/
10752         assert(p_comp_no <  p_j2k->m_private_image->numcomps);
10753
10754         l_tccp = &l_tcp->tccps[p_comp_no];
10755         l_current_ptr = p_header_data;
10756
10757         if (*p_header_size < 1) {
10758                 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading SQcd or SQcc element\n");
10759                 return OPJ_FALSE;
10760         }
10761         *p_header_size -= 1;
10762
10763         opj_read_bytes(l_current_ptr, &l_tmp ,1);                       /* Sqcx */
10764         ++l_current_ptr;
10765
10766         l_tccp->qntsty = l_tmp & 0x1f;
10767         l_tccp->numgbits = l_tmp >> 5;
10768         if (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) {
10769         l_num_band = 1;
10770         }
10771         else {
10772                 l_num_band = (l_tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) ?
10773                         (*p_header_size) :
10774                         (*p_header_size) / 2;
10775
10776                 if( l_num_band > J2K_MAXBANDS ) {
10777                         opj_event_msg_v2(p_manager, EVT_WARNING, "While reading CCP_QNTSTY element inside QCD or QCC marker segment, "
10778                                 "number of subbands (%d) is greater to J2K_MAXBANDS (%d). So we limit the number of elements stored to "
10779                                 "J2K_MAXBANDS (%d) and skip the rest. \n", l_num_band, J2K_MAXBANDS, J2K_MAXBANDS);
10780                         /*return OPJ_FALSE;*/
10781                 }
10782         }
10783
10784 #ifdef USE_JPWL
10785         if (l_cp->correct) {
10786
10787                 /* if JPWL is on, we check whether there are too many subbands */
10788                 if (/*(l_num_band < 0) ||*/ (l_num_band >= J2K_MAXBANDS)) {
10789                         opj_event_msg_v2(p_manager, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
10790                                 "JPWL: bad number of subbands in Sqcx (%d)\n",
10791                                 l_num_band);
10792                         if (!JPWL_ASSUME) {
10793                                 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
10794                                 return OPJ_FALSE;
10795                         }
10796                         /* we try to correct */
10797                         l_num_band = 1;
10798                         opj_event_msg_v2(p_manager, EVT_WARNING, "- trying to adjust them\n"
10799                                 "- setting number of bands to %d => HYPOTHESIS!!!\n",
10800                                 l_num_band);
10801                 };
10802
10803         };
10804 #endif /* USE_JPWL */
10805
10806         if (l_tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) {
10807                 for     (l_band_no = 0; l_band_no < l_num_band; l_band_no++) {
10808                         opj_read_bytes(l_current_ptr, &l_tmp ,1);                       /* SPqcx_i */
10809                         ++l_current_ptr;
10810                         if (l_band_no < J2K_MAXBANDS){
10811                                 l_tccp->stepsizes[l_band_no].expn = l_tmp>>3;
10812                                 l_tccp->stepsizes[l_band_no].mant = 0;
10813                         }
10814                 }
10815                 *p_header_size = *p_header_size - l_num_band;
10816         }
10817         else {
10818                 for     (l_band_no = 0; l_band_no < l_num_band; l_band_no++) {
10819                         opj_read_bytes(l_current_ptr, &l_tmp ,2);                       /* SPqcx_i */
10820                         l_current_ptr+=2;
10821                         if (l_band_no < J2K_MAXBANDS){
10822                                 l_tccp->stepsizes[l_band_no].expn = l_tmp >> 11;
10823                                 l_tccp->stepsizes[l_band_no].mant = l_tmp & 0x7ff;
10824                         }
10825                 }
10826                 *p_header_size = *p_header_size - 2*l_num_band;
10827         }
10828
10829         /* Add Antonin : if scalar_derived -> compute other stepsizes */
10830         if (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) {
10831                 for (l_band_no = 1; l_band_no < J2K_MAXBANDS; l_band_no++) {
10832                         l_tccp->stepsizes[l_band_no].expn =
10833                                 ((l_tccp->stepsizes[0].expn) - ((l_band_no - 1) / 3) > 0) ?
10834                                         (l_tccp->stepsizes[0].expn) - ((l_band_no - 1) / 3) : 0;
10835                         l_tccp->stepsizes[l_band_no].mant = l_tccp->stepsizes[0].mant;
10836                 }
10837         }
10838
10839         return OPJ_TRUE;
10840 }
10841
10842 /**
10843  * Copies the tile component parameters of all the component from the first tile component.
10844  *
10845  * @param               p_j2k           the J2k codec.
10846  */
10847 void j2k_copy_tile_quantization_parameters( opj_j2k_v2_t *p_j2k )
10848 {
10849         OPJ_UINT32 i;
10850         opj_cp_v2_t *l_cp = NULL;
10851         opj_tcp_v2_t *l_tcp = NULL;
10852         opj_tccp_t *l_ref_tccp = NULL;
10853         opj_tccp_t *l_copied_tccp = NULL;
10854         OPJ_UINT32 l_size;
10855
10856         /* preconditions */
10857         assert(p_j2k != 00);
10858
10859         l_cp = &(p_j2k->m_cp);
10860         l_tcp = p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH ?
10861                         &l_cp->tcps[p_j2k->m_current_tile_number] :
10862                         p_j2k->m_specific_param.m_decoder.m_default_tcp;
10863
10864         l_ref_tccp = &l_tcp->tccps[0];
10865         l_copied_tccp = l_ref_tccp + 1;
10866         l_size = J2K_MAXBANDS * sizeof(opj_stepsize_t);
10867
10868         for     (i=1;i<p_j2k->m_private_image->numcomps;++i) {
10869                 l_copied_tccp->qntsty = l_ref_tccp->qntsty;
10870                 l_copied_tccp->numgbits = l_ref_tccp->numgbits;
10871                 memcpy(l_copied_tccp->stepsizes,l_ref_tccp->stepsizes,l_size);
10872                 ++l_copied_tccp;
10873         }
10874 }
10875
10876 /**
10877  * Dump some elements from the J2K decompression structure .
10878  *
10879  *@param p_j2k                          the jpeg2000 codec.
10880  *@param flag                           flag to describe what elments are dump.
10881  *@param out_stream                     output stream where dump the elements.
10882  *
10883 */
10884 void j2k_dump (opj_j2k_v2_t* p_j2k, OPJ_INT32 flag, FILE* out_stream)
10885 {
10886         /* Check if the flag is compatible with j2k file*/
10887         if ( (flag & OPJ_JP2_INFO) || (flag & OPJ_JP2_IND)){
10888                 fprintf(out_stream, "Wrong flag\n");
10889                 return;
10890         }
10891
10892         /* Dump the image_header */
10893         if (flag & OPJ_IMG_INFO){
10894                 if (p_j2k->m_private_image)
10895                         j2k_dump_image_header(p_j2k->m_private_image, 0, out_stream);
10896         }
10897
10898         /* Dump the codestream info from main header */
10899         if (flag & OPJ_J2K_MH_INFO){
10900                 j2k_dump_MH_info(p_j2k, out_stream);
10901         }
10902
10903
10904         /* Dump the codestream info of the current tile */
10905         if (flag & OPJ_J2K_TH_INFO){
10906
10907         }
10908
10909         /* Dump the codestream index from main header */
10910         if (flag & OPJ_J2K_MH_IND){
10911                 j2k_dump_MH_index(p_j2k, out_stream);
10912         }
10913
10914         /* Dump the codestream index of the current tile */
10915         if (flag & OPJ_J2K_TH_IND){
10916
10917         }
10918
10919 }
10920
10921 /**
10922  * Dump index elements of the codestream extract from the main header.
10923  *
10924  *@param p_j2k                          the jpeg2000 codec.
10925  *@param out_stream                     output stream where dump the elements.
10926  *
10927 */
10928 void j2k_dump_MH_index(opj_j2k_v2_t* p_j2k, FILE* out_stream)
10929 {
10930         opj_codestream_index_t* cstr_index = p_j2k->cstr_index;
10931         OPJ_UINT32 it_marker, it_tile, it_tile_part;
10932
10933         fprintf(out_stream, "Codestream index from main header: {\n");
10934
10935         fprintf(out_stream, "\t Main header start position=%" PRIi64 "\n"
10936                                     "\t Main header end position=%" PRIi64 "\n",
10937                         cstr_index->main_head_start, cstr_index->main_head_end);
10938
10939         fprintf(out_stream, "\t Marker list: {\n");
10940
10941         if (cstr_index->marker){
10942                 for (it_marker=0; it_marker < cstr_index->marknum ; it_marker++){
10943                         fprintf(out_stream, "\t\t type=%#x, pos=%" PRIi64 ", len=%d\n",
10944                                         cstr_index->marker[it_marker].type,
10945                                         cstr_index->marker[it_marker].pos,
10946                                         cstr_index->marker[it_marker].len );
10947                 }
10948         }
10949
10950         fprintf(out_stream, "\t }\n");
10951
10952
10953         if (cstr_index->tile_index){
10954
10955         /* Simple test to avoid to write empty information*/
10956         OPJ_UINT32 l_acc_nb_of_tile_part = 0;
10957         for (it_tile=0; it_tile < cstr_index->nb_of_tiles ; it_tile++){
10958                         l_acc_nb_of_tile_part += cstr_index->tile_index[it_tile].nb_tps;
10959         }
10960
10961         if (l_acc_nb_of_tile_part)
10962         {
10963             fprintf(out_stream, "\t Tile index: {\n");
10964        
10965                     for (it_tile=0; it_tile < cstr_index->nb_of_tiles ; it_tile++){
10966                             OPJ_UINT32 nb_of_tile_part = cstr_index->tile_index[it_tile].nb_tps;
10967
10968                             fprintf(out_stream, "\t\t nb of tile-part in tile [%d]=%d\n", it_tile, nb_of_tile_part);
10969
10970                             if (cstr_index->tile_index[it_tile].tp_index){
10971                                     for (it_tile_part =0; it_tile_part < nb_of_tile_part; it_tile_part++){
10972                                             fprintf(out_stream, "\t\t\t tile-part[%d]: star_pos=%" PRIi64 ", end_header=%" PRIi64 ", end_pos=%" PRIi64 ".\n",
10973                                                             it_tile_part,
10974                                                             cstr_index->tile_index[it_tile].tp_index[it_tile_part].start_pos,
10975                                                             cstr_index->tile_index[it_tile].tp_index[it_tile_part].end_header,
10976                                                             cstr_index->tile_index[it_tile].tp_index[it_tile_part].end_pos);
10977                                     }
10978                             }
10979
10980                             if (cstr_index->tile_index[it_tile].marker){
10981                                     for (it_marker=0; it_marker < cstr_index->tile_index[it_tile].marknum ; it_marker++){
10982                                             fprintf(out_stream, "\t\t type=%#x, pos=%" PRIi64 ", len=%d\n",
10983                                                             cstr_index->tile_index[it_tile].marker[it_marker].type,
10984                                                             cstr_index->tile_index[it_tile].marker[it_marker].pos,
10985                                                             cstr_index->tile_index[it_tile].marker[it_marker].len );
10986                                     }
10987                             }
10988                     }
10989                     fprintf(out_stream,"\t }\n");
10990         }
10991         }
10992
10993         fprintf(out_stream,"}\n");
10994
10995 }
10996
10997 /**
10998  * Dump info elements of the codestream extract from the main header.
10999  *
11000  *@param p_j2k                          the jpeg2000 codec.
11001  *@param out_stream                     output stream where dump the elements.
11002  *
11003 */
11004 void j2k_dump_MH_info(opj_j2k_v2_t* p_j2k, FILE* out_stream)
11005 {
11006         opj_tcp_v2_t * l_default_tile=NULL;
11007
11008         fprintf(out_stream, "Codestream info from main header: {\n");
11009
11010         fprintf(out_stream, "\t tx0=%d, ty0=%d\n", p_j2k->m_cp.tx0, p_j2k->m_cp.ty0);
11011         fprintf(out_stream, "\t tdx=%d, tdy=%d\n", p_j2k->m_cp.tdx, p_j2k->m_cp.tdy);
11012         fprintf(out_stream, "\t tw=%d, th=%d\n", p_j2k->m_cp.tw, p_j2k->m_cp.th);
11013
11014         l_default_tile = p_j2k->m_specific_param.m_decoder.m_default_tcp;
11015         if (l_default_tile)
11016         {
11017                 OPJ_INT32 compno;
11018                 OPJ_INT32 numcomps = p_j2k->m_private_image->numcomps;
11019
11020                 fprintf(out_stream, "\t default tile {\n");
11021                 fprintf(out_stream, "\t\t csty=%#x\n", l_default_tile->csty);
11022                 fprintf(out_stream, "\t\t prg=%#x\n", l_default_tile->prg);
11023                 fprintf(out_stream, "\t\t numlayers=%d\n", l_default_tile->numlayers);
11024                 fprintf(out_stream, "\t\t mct=%x\n", l_default_tile->mct);
11025
11026                 for (compno = 0; compno < numcomps; compno++) {
11027                         opj_tccp_t *l_tccp = &(l_default_tile->tccps[compno]);
11028                         OPJ_UINT32 resno;
11029       OPJ_INT32 bandno, numbands;
11030
11031                         /* coding style*/
11032                         fprintf(out_stream, "\t\t comp %d {\n", compno);
11033                         fprintf(out_stream, "\t\t\t csty=%#x\n", l_tccp->csty);
11034                         fprintf(out_stream, "\t\t\t numresolutions=%d\n", l_tccp->numresolutions);
11035                         fprintf(out_stream, "\t\t\t cblkw=2^%d\n", l_tccp->cblkw);
11036                         fprintf(out_stream, "\t\t\t cblkh=2^%d\n", l_tccp->cblkh);
11037                         fprintf(out_stream, "\t\t\t cblksty=%#x\n", l_tccp->cblksty);
11038                         fprintf(out_stream, "\t\t\t qmfbid=%d\n", l_tccp->qmfbid);
11039
11040                         fprintf(out_stream, "\t\t\t preccintsize (w,h)=");
11041                         for (resno = 0; resno < l_tccp->numresolutions; resno++) {
11042                                 fprintf(out_stream, "(%d,%d) ", l_tccp->prcw[resno], l_tccp->prch[resno]);
11043                         }
11044                         fprintf(out_stream, "\n");
11045
11046                         /* quantization style*/
11047                         fprintf(out_stream, "\t\t\t qntsty=%d\n", l_tccp->qntsty);
11048                         fprintf(out_stream, "\t\t\t numgbits=%d\n", l_tccp->numgbits);
11049                         fprintf(out_stream, "\t\t\t stepsizes (m,e)=");
11050                         numbands = (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) ? 1 : l_tccp->numresolutions * 3 - 2;
11051                         for (bandno = 0; bandno < numbands; bandno++) {
11052                                 fprintf(out_stream, "(%d,%d) ", l_tccp->stepsizes[bandno].mant,
11053                                         l_tccp->stepsizes[bandno].expn);
11054                         }
11055                         fprintf(out_stream, "\n");
11056
11057                         /* RGN value*/
11058                         fprintf(out_stream, "\t\t\t roishift=%d\n", l_tccp->roishift);
11059
11060                         fprintf(out_stream, "\t\t }\n");
11061                 } /*end of component of default tile*/
11062                 fprintf(out_stream, "\t }\n"); /*end of default tile*/
11063
11064         }
11065
11066         fprintf(out_stream, "}\n");
11067
11068 }
11069
11070 /**
11071  * Dump an image header structure.
11072  *
11073  *@param img_header                     the image header to dump.
11074  *@param dev_dump_flag          flag to describe if we are in the case of this function is use outside j2k_dump function
11075  *@param out_stream                     output stream where dump the elements.
11076  */
11077 void j2k_dump_image_header(opj_image_t* img_header, opj_bool dev_dump_flag, FILE* out_stream)
11078 {
11079         char tab[2];
11080
11081         if (dev_dump_flag){
11082                 fprintf(stdout, "[DEV] Dump an image_header struct {\n");
11083                 tab[0] = '\0';
11084         }
11085         else {
11086                 fprintf(out_stream, "Image info {\n");
11087                 tab[0] = '\t';tab[1] = '\0';
11088         }
11089
11090         fprintf(out_stream, "%s x0=%d, y0=%d\n", tab, img_header->x0, img_header->y0);
11091         fprintf(out_stream,     "%s x1=%d, y1=%d\n", tab, img_header->x1, img_header->y1);
11092         fprintf(out_stream, "%s numcomps=%d\n", tab, img_header->numcomps);
11093
11094         if (img_header->comps){
11095                 OPJ_UINT32 compno;
11096                 for (compno = 0; compno < img_header->numcomps; compno++) {
11097                         fprintf(out_stream, "%s\t component %d {\n", tab, compno);
11098                         j2k_dump_image_comp_header(&(img_header->comps[compno]), dev_dump_flag, out_stream);
11099                         fprintf(out_stream,"%s}\n",tab);
11100                 }
11101         }
11102
11103         fprintf(out_stream, "}\n");
11104 }
11105
11106 /**
11107  * Dump a component image header structure.
11108  *
11109  *@param comp_header            the component image header to dump.
11110  *@param dev_dump_flag          flag to describe if we are in the case of this function is use outside j2k_dump function
11111  *@param out_stream                     output stream where dump the elements.
11112  */
11113 void j2k_dump_image_comp_header(opj_image_comp_t* comp_header, opj_bool dev_dump_flag, FILE* out_stream)
11114 {
11115         char tab[3];
11116
11117         if (dev_dump_flag){
11118                 fprintf(stdout, "[DEV] Dump an image_comp_header struct {\n");
11119                 tab[0] = '\0';
11120         }       else {
11121                 tab[0] = '\t';tab[1] = '\t';tab[2] = '\0';
11122         }
11123
11124         fprintf(out_stream, "%s dx=%d, dy=%d\n", tab, comp_header->dx, comp_header->dy);
11125         fprintf(out_stream, "%s prec=%d\n", tab, comp_header->prec);
11126         fprintf(out_stream, "%s sgnd=%d\n", tab, comp_header->sgnd);
11127
11128         if (dev_dump_flag)
11129                 fprintf(out_stream, "}\n");
11130 }
11131
11132
11133 /**
11134  * Get the codestream info from a JPEG2000 codec.
11135  *
11136  *@param        p_j2k                           the component image header to dump.
11137  *
11138  *@return       the codestream information extract from the jpg2000 codec
11139  */
11140 opj_codestream_info_v2_t* j2k_get_cstr_info(opj_j2k_v2_t* p_j2k)
11141 {
11142         OPJ_UINT16 compno;
11143         OPJ_UINT16 numcomps = p_j2k->m_private_image->numcomps;
11144         opj_tcp_v2_t *l_default_tile;
11145         opj_codestream_info_v2_t* cstr_info = (opj_codestream_info_v2_t*) opj_calloc(1,sizeof(opj_codestream_info_v2_t));
11146
11147         cstr_info->nbcomps = p_j2k->m_private_image->numcomps;
11148
11149         cstr_info->tx0 = p_j2k->m_cp.tx0;
11150         cstr_info->ty0 = p_j2k->m_cp.ty0;
11151         cstr_info->tdx = p_j2k->m_cp.tdx;
11152         cstr_info->tdy = p_j2k->m_cp.tdy;
11153         cstr_info->tw = p_j2k->m_cp.tw;
11154         cstr_info->th = p_j2k->m_cp.th;
11155
11156         cstr_info->tile_info = NULL; /* Not fill from the main header*/
11157
11158         l_default_tile = p_j2k->m_specific_param.m_decoder.m_default_tcp;
11159
11160         cstr_info->m_default_tile_info.csty = l_default_tile->csty;
11161         cstr_info->m_default_tile_info.prg = l_default_tile->prg;
11162         cstr_info->m_default_tile_info.numlayers = l_default_tile->numlayers;
11163         cstr_info->m_default_tile_info.mct = l_default_tile->mct;
11164
11165         cstr_info->m_default_tile_info.tccp_info = (opj_tccp_info_t*) opj_calloc(cstr_info->nbcomps, sizeof(opj_tccp_info_t));
11166
11167         for (compno = 0; compno < numcomps; compno++) {
11168                 opj_tccp_t *l_tccp = &(l_default_tile->tccps[compno]);
11169                 opj_tccp_info_t *l_tccp_info = &(cstr_info->m_default_tile_info.tccp_info[compno]);
11170                 OPJ_INT32 bandno, numbands;
11171
11172                 /* coding style*/
11173                 l_tccp_info->csty = l_tccp->csty;
11174                 l_tccp_info->numresolutions = l_tccp->numresolutions;
11175                 l_tccp_info->cblkw = l_tccp->cblkw;
11176                 l_tccp_info->cblkh = l_tccp->cblkh;
11177                 l_tccp_info->cblksty = l_tccp->cblksty;
11178                 l_tccp_info->qmfbid = l_tccp->qmfbid;
11179                 if (l_tccp->numresolutions < J2K_MAXRLVLS)
11180                 {
11181                         memcpy(l_tccp_info->prch, l_tccp->prch, l_tccp->numresolutions);
11182                         memcpy(l_tccp_info->prcw, l_tccp->prcw, l_tccp->numresolutions);
11183                 }
11184
11185                 /* quantization style*/
11186                 l_tccp_info->qntsty = l_tccp->qntsty;
11187                 l_tccp_info->numgbits = l_tccp->numgbits;
11188
11189                 numbands = (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) ? 1 : l_tccp->numresolutions * 3 - 2;
11190                 if (numbands < J2K_MAXBANDS) {
11191                         for (bandno = 0; bandno < numbands; bandno++) {
11192                                 l_tccp_info->stepsizes_mant[bandno] = l_tccp->stepsizes[bandno].mant;
11193                                 l_tccp_info->stepsizes_expn[bandno] = l_tccp->stepsizes[bandno].expn;
11194                         }
11195                 }
11196
11197                 /* RGN value*/
11198                 l_tccp_info->roishift = l_tccp->roishift;
11199         }
11200
11201
11202         return cstr_info;
11203 }
11204
11205 /**
11206  * Get the codestream index from a JPEG2000 codec.
11207  *
11208  *@param        p_j2k                           the component image header to dump.
11209  *
11210  *@return       the codestream index extract from the jpg2000 codec
11211  */
11212 opj_codestream_index_t* j2k_get_cstr_index(opj_j2k_v2_t* p_j2k)
11213 {
11214         opj_codestream_index_t* l_cstr_index = (opj_codestream_index_t*)
11215                         opj_calloc(1,sizeof(opj_codestream_index_t));
11216         if (!l_cstr_index)
11217                 return NULL;
11218
11219         l_cstr_index->main_head_start = p_j2k->cstr_index->main_head_start;
11220         l_cstr_index->main_head_end = p_j2k->cstr_index->main_head_end;
11221         l_cstr_index->codestream_size = p_j2k->cstr_index->codestream_size;
11222
11223         l_cstr_index->marknum = p_j2k->cstr_index->marknum;
11224         l_cstr_index->marker = (opj_marker_info_t*)opj_malloc(l_cstr_index->marknum*sizeof(opj_marker_info_t));
11225         if (!l_cstr_index->marker){
11226                 opj_free( l_cstr_index);
11227                 return NULL;
11228         }
11229
11230         if (p_j2k->cstr_index->marker)
11231                 memcpy(l_cstr_index->marker, p_j2k->cstr_index->marker, l_cstr_index->marknum * sizeof(opj_marker_info_t) );
11232         else{
11233                 opj_free(l_cstr_index->marker);
11234                 l_cstr_index->marker = NULL;
11235         }
11236
11237         l_cstr_index->nb_of_tiles = p_j2k->cstr_index->nb_of_tiles;
11238         l_cstr_index->tile_index = (opj_tile_index_t*)opj_calloc(l_cstr_index->nb_of_tiles, sizeof(opj_tile_index_t) );
11239         if (!l_cstr_index->tile_index){
11240                 opj_free( l_cstr_index->marker);
11241                 opj_free( l_cstr_index);
11242                 return NULL;
11243         }
11244
11245         if (!p_j2k->cstr_index->tile_index){
11246                 opj_free(l_cstr_index->tile_index);
11247                 l_cstr_index->tile_index = NULL;
11248         }
11249         else {
11250                 OPJ_UINT32 it_tile = 0;
11251                 for (it_tile = 0; it_tile < l_cstr_index->nb_of_tiles; it_tile++ ){
11252
11253                         /* Tile Marker*/
11254                         l_cstr_index->tile_index[it_tile].marknum = p_j2k->cstr_index->tile_index[it_tile].marknum;
11255
11256                         l_cstr_index->tile_index[it_tile].marker =
11257                                 (opj_marker_info_t*)opj_malloc(l_cstr_index->tile_index[it_tile].marknum*sizeof(opj_marker_info_t));
11258
11259                         if (!l_cstr_index->tile_index[it_tile].marker) {
11260                                 OPJ_UINT32 it_tile_free;
11261
11262                                 for (it_tile_free=0; it_tile_free < it_tile; it_tile_free++){
11263                                         opj_free(l_cstr_index->tile_index[it_tile_free].marker);
11264                                 }
11265
11266                                 opj_free( l_cstr_index->tile_index);
11267                                 opj_free( l_cstr_index->marker);
11268                                 opj_free( l_cstr_index);
11269                                 return NULL;
11270                         }
11271
11272                         if (p_j2k->cstr_index->tile_index[it_tile].marker)
11273                                 memcpy( l_cstr_index->tile_index[it_tile].marker,
11274                                                 p_j2k->cstr_index->tile_index[it_tile].marker,
11275                                                 l_cstr_index->tile_index[it_tile].marknum * sizeof(opj_marker_info_t) );
11276                         else{
11277                                 opj_free(l_cstr_index->tile_index[it_tile].marker);
11278                                 l_cstr_index->tile_index[it_tile].marker = NULL;
11279                         }
11280
11281                         /* Tile part index*/
11282                         l_cstr_index->tile_index[it_tile].nb_tps = p_j2k->cstr_index->tile_index[it_tile].nb_tps;
11283
11284                         l_cstr_index->tile_index[it_tile].tp_index =
11285                                 (opj_tp_index_t*)opj_malloc(l_cstr_index->tile_index[it_tile].nb_tps*sizeof(opj_tp_index_t));
11286
11287                         if(!l_cstr_index->tile_index[it_tile].tp_index){
11288                                 OPJ_UINT32 it_tile_free;
11289
11290                                 for (it_tile_free=0; it_tile_free < it_tile; it_tile_free++){
11291                                         opj_free(l_cstr_index->tile_index[it_tile_free].marker);
11292                                         opj_free(l_cstr_index->tile_index[it_tile_free].tp_index);
11293                                 }
11294
11295                                 opj_free( l_cstr_index->tile_index);
11296                                 opj_free( l_cstr_index->marker);
11297                                 opj_free( l_cstr_index);
11298                                 return NULL;
11299                         }
11300
11301                         if (p_j2k->cstr_index->tile_index[it_tile].tp_index){
11302                                 memcpy( l_cstr_index->tile_index[it_tile].tp_index,
11303                                                 p_j2k->cstr_index->tile_index[it_tile].tp_index,
11304                                                 l_cstr_index->tile_index[it_tile].nb_tps * sizeof(opj_tp_index_t) );
11305                         }
11306                         else{
11307                                 opj_free(l_cstr_index->tile_index[it_tile].tp_index);
11308                                 l_cstr_index->tile_index[it_tile].tp_index = NULL;
11309                         }
11310
11311                         /* Packet index (NOT USED)*/
11312                         l_cstr_index->tile_index[it_tile].nb_packet = 0;
11313                         l_cstr_index->tile_index[it_tile].packet_index = NULL;
11314
11315                 }
11316         }
11317
11318         return l_cstr_index;
11319 }
11320
11321 static opj_bool j2k_allocate_tile_element_cstr_index(opj_j2k_v2_t *p_j2k)
11322 {
11323         OPJ_UINT32 it_tile=0;
11324
11325         p_j2k->cstr_index->nb_of_tiles = p_j2k->m_cp.tw * p_j2k->m_cp.th;
11326         p_j2k->cstr_index->tile_index = (opj_tile_index_t*)opj_calloc(p_j2k->cstr_index->nb_of_tiles, sizeof(opj_tile_index_t));
11327         if (!p_j2k->cstr_index->tile_index)
11328                 return OPJ_FALSE;
11329
11330         for (it_tile=0; it_tile < p_j2k->cstr_index->nb_of_tiles; it_tile++){
11331                 p_j2k->cstr_index->tile_index[it_tile].maxmarknum = 100;
11332                 p_j2k->cstr_index->tile_index[it_tile].marknum = 0;
11333                 p_j2k->cstr_index->tile_index[it_tile].marker = (opj_marker_info_t*)
11334                                 opj_calloc(p_j2k->cstr_index->tile_index[it_tile].maxmarknum, sizeof(opj_marker_info_t));
11335                 if (!p_j2k->cstr_index->tile_index[it_tile].marker)
11336                         return OPJ_FALSE;
11337         }
11338
11339         return OPJ_TRUE;
11340 }
11341
11342 /**
11343  * Reads the tiles.
11344  */
11345 opj_bool j2k_decode_tiles (     opj_j2k_v2_t *p_j2k,
11346                                                         opj_stream_private_t *p_stream,
11347                                                         opj_event_mgr_t * p_manager)
11348 {
11349         opj_bool l_go_on = OPJ_TRUE;
11350         OPJ_UINT32 l_current_tile_no;
11351         OPJ_UINT32 l_data_size,l_max_data_size;
11352         OPJ_INT32 l_tile_x0,l_tile_y0,l_tile_x1,l_tile_y1;
11353         OPJ_UINT32 l_nb_comps;
11354         OPJ_BYTE * l_current_data;
11355
11356         l_current_data = (OPJ_BYTE*)opj_malloc(1000);
11357         if (! l_current_data) {
11358                 return OPJ_FALSE;
11359         }
11360         l_max_data_size = 1000;
11361
11362
11363
11364         while (OPJ_TRUE) {
11365                 if (! j2k_read_tile_header(     p_j2k,
11366                                                                         &l_current_tile_no,
11367                                                                         &l_data_size,
11368                                                                         &l_tile_x0, &l_tile_y0,
11369                                                                         &l_tile_x1, &l_tile_y1,
11370                                                                         &l_nb_comps,
11371                                                                         &l_go_on,
11372                                                                         p_stream,
11373                                                                         p_manager)) {
11374                         opj_free(l_current_data);
11375                         return OPJ_FALSE;
11376                 }
11377
11378                 if (! l_go_on) {
11379                         break;
11380                 }
11381
11382                 if (l_data_size > l_max_data_size) {
11383                         l_current_data = (OPJ_BYTE*)opj_realloc(l_current_data,l_data_size);
11384                         if (! l_current_data) {
11385                                 opj_free(l_current_data);
11386                                 return OPJ_FALSE;
11387                         }
11388
11389                         l_max_data_size = l_data_size;
11390                 }
11391
11392                 if (! j2k_decode_tile(p_j2k,l_current_tile_no,l_current_data,l_data_size,p_stream,p_manager)) {
11393                         opj_free(l_current_data);
11394                         return OPJ_FALSE;
11395                 }
11396                 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);
11397
11398                 if (! j2k_update_image_data(p_j2k->m_tcd,l_current_data, p_j2k->m_output_image)) {
11399                         opj_free(l_current_data);
11400                         return OPJ_FALSE;
11401                 }
11402                 opj_event_msg_v2(p_manager, EVT_INFO, "Image data has been updated with tile %d.\n\n", l_current_tile_no + 1);
11403
11404         }
11405
11406         opj_free(l_current_data);
11407
11408         return OPJ_TRUE;
11409 }
11410
11411 /**
11412  * Sets up the procedures to do on decoding data. Developpers wanting to extend the library can add their own reading procedures.
11413  */
11414 static void j2k_setup_decoding (opj_j2k_v2_t *p_j2k)
11415 {
11416         /* preconditions*/
11417         assert(p_j2k != 00);
11418
11419         opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_decode_tiles);
11420         /* DEVELOPER CORNER, add your custom procedures */
11421
11422 }
11423
11424 /*
11425  * Read and decode one tile.
11426  */
11427 static opj_bool j2k_decode_one_tile (   opj_j2k_v2_t *p_j2k,
11428                                                                 opj_stream_private_t *p_stream,
11429                                                                 opj_event_mgr_t * p_manager)
11430 {
11431         opj_bool l_go_on = OPJ_TRUE;
11432         OPJ_UINT32 l_current_tile_no;
11433         OPJ_UINT32 l_tile_no_to_dec;
11434         OPJ_UINT32 l_data_size,l_max_data_size;
11435         OPJ_INT32 l_tile_x0,l_tile_y0,l_tile_x1,l_tile_y1;
11436         OPJ_UINT32 l_nb_comps;
11437         OPJ_BYTE * l_current_data;
11438
11439         l_current_data = (OPJ_BYTE*)opj_malloc(1000);
11440         if (! l_current_data) {
11441                 return OPJ_FALSE;
11442         }
11443         l_max_data_size = 1000;
11444
11445         /*Allocate and initialize some elements of codestrem index if not already done*/
11446         if( !p_j2k->cstr_index->tile_index)
11447         {
11448                 if (!j2k_allocate_tile_element_cstr_index(p_j2k)){
11449                         opj_free(l_current_data);
11450                         return OPJ_FALSE;
11451                 }
11452         }
11453         /* Move into the codestream to the first SOT used to decode the desired tile */
11454         l_tile_no_to_dec = p_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec;
11455         if (p_j2k->cstr_index->tile_index)
11456                 if(p_j2k->cstr_index->tile_index->tp_index)
11457                 {
11458                         if ( ! p_j2k->cstr_index->tile_index[l_tile_no_to_dec].nb_tps) {
11459                                 /* the index for this tile has not been built,
11460                                  *  so move to the last SOT read */
11461                                 if ( opj_stream_read_seek(p_stream, p_j2k->m_specific_param.m_decoder.m_last_sot_read_pos+2, p_manager) ){
11462                                         opj_event_msg_v2(p_manager, EVT_ERROR, "Problem with seek function\n");
11463                                         return OPJ_FALSE;
11464                                 }
11465                         }
11466                         else{
11467                                 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)) {
11468                                         opj_event_msg_v2(p_manager, EVT_ERROR, "Problem with seek function\n");
11469                                         return OPJ_FALSE;
11470                                 }
11471                         }
11472                         /* Special case if we have previously read the EOC marker (if the previous tile getted is the last ) */
11473                         if(p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_EOC)
11474                                 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_TPHSOT;
11475                 }
11476
11477         while (OPJ_TRUE) {
11478                 if (! j2k_read_tile_header(     p_j2k,
11479                                                                         &l_current_tile_no,
11480                                                                         &l_data_size,
11481                                                                         &l_tile_x0, &l_tile_y0,
11482                                                                         &l_tile_x1, &l_tile_y1,
11483                                                                         &l_nb_comps,
11484                                                                         &l_go_on,
11485                                                                         p_stream,
11486                                                                         p_manager)) {
11487                         opj_free(l_current_data);
11488                         return OPJ_FALSE;
11489                 }
11490
11491
11492                 if (! l_go_on) {
11493                         break;
11494                 }
11495
11496                 if (l_data_size > l_max_data_size) {
11497                         l_current_data = (OPJ_BYTE*)opj_realloc(l_current_data,l_data_size);
11498                         if (! l_current_data) {
11499                                 opj_free(l_current_data);
11500                                 return OPJ_FALSE;
11501                         }
11502
11503                         l_max_data_size = l_data_size;
11504                 }
11505
11506
11507
11508                 if (! j2k_decode_tile(p_j2k,l_current_tile_no,l_current_data,l_data_size,p_stream,p_manager)) {
11509                         opj_free(l_current_data);
11510                         return OPJ_FALSE;
11511                 }
11512                 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);
11513
11514                 if (! j2k_update_image_data(p_j2k->m_tcd,l_current_data, p_j2k->m_output_image)) {
11515                         opj_free(l_current_data);
11516                         return OPJ_FALSE;
11517                 }
11518                 opj_event_msg_v2(p_manager, EVT_INFO, "Image data has been updated with tile %d.\n\n", l_current_tile_no);
11519
11520                 if(l_current_tile_no == l_tile_no_to_dec)
11521                 {
11522                         /* move into the codestream to the the first SOT (FIXME or not move?)*/
11523                         if (!(opj_stream_read_seek(p_stream, p_j2k->cstr_index->main_head_end + 2, p_manager) ) ) {
11524                                 opj_event_msg_v2(p_manager, EVT_ERROR, "Problem with seek function\n");
11525                                 return OPJ_FALSE;
11526                         }
11527                         break;
11528                 }
11529                 else {
11530                         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);
11531                 }
11532
11533         }
11534
11535         opj_free(l_current_data);
11536
11537         return OPJ_TRUE;
11538 }
11539
11540
11541 /**
11542  * Sets up the procedures to do on decoding one tile. Developpers wanting to extend the library can add their own reading procedures.
11543  */
11544 static void j2k_setup_decoding_tile (opj_j2k_v2_t *p_j2k)
11545 {
11546         /* preconditions*/
11547         assert(p_j2k != 00);
11548
11549         opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_decode_one_tile);
11550         /* DEVELOPER CORNER, add your custom procedures */
11551
11552 }
11553
11554
11555 /**
11556  * Decodes the tiles of the stream.
11557  */
11558 opj_bool opj_j2k_decode(opj_j2k_v2_t * p_j2k,
11559                                                 opj_stream_private_t * p_stream,
11560                                                 opj_image_t * p_image,
11561                                                 opj_event_mgr_t * p_manager)
11562 {
11563         OPJ_UINT32 compno;
11564
11565         if (!p_image)
11566                 return OPJ_FALSE;
11567
11568         p_j2k->m_output_image = opj_image_create0();
11569         if (! (p_j2k->m_output_image)) {
11570                 return OPJ_FALSE;
11571         }
11572         opj_copy_image_header(p_image, p_j2k->m_output_image);
11573
11574         /* customization of the decoding */
11575         j2k_setup_decoding(p_j2k);
11576
11577         /* Decode the codestream */
11578         if (! j2k_exec (p_j2k,p_j2k->m_procedure_list,p_stream,p_manager)) {
11579                 opj_image_destroy(p_j2k->m_private_image);
11580                 p_j2k->m_private_image = NULL;
11581                 return OPJ_FALSE;
11582         }
11583
11584         /* Move data and copy one information from codec to output image*/
11585         for (compno = 0; compno < p_image->numcomps; compno++) {
11586                 p_image->comps[compno].resno_decoded = p_j2k->m_output_image->comps[compno].resno_decoded;
11587                 p_image->comps[compno].data = p_j2k->m_output_image->comps[compno].data;
11588                 p_j2k->m_output_image->comps[compno].data = NULL;
11589         }
11590
11591         return OPJ_TRUE;
11592 }
11593
11594
11595 /**
11596  * Get the decoded tile.
11597  *
11598  * @param       p_j2k                   the jpeg2000 codestream codec.
11599  * @param       p_stream                input_stream
11600  * @param       p_image                 output image.   .
11601  * @param       p_manager               the user event manager
11602  * @param       tile_index              index of the tile we want decode
11603  *
11604  * @return      true                    if succeed.
11605  */
11606 opj_bool j2k_get_tile(  opj_j2k_v2_t *p_j2k,
11607                                                 opj_stream_private_t *p_stream,
11608                                                 opj_image_t* p_image,
11609                                                 struct opj_event_mgr * p_manager,
11610                                                 OPJ_UINT32 tile_index )
11611 {
11612         OPJ_UINT32 compno;
11613         OPJ_UINT32 l_tile_x, l_tile_y;
11614         opj_image_comp_t* l_img_comp;
11615
11616         if (!p_image) {
11617                 opj_event_msg_v2(p_manager, EVT_ERROR, "We need an image previously created.\n");
11618                 return OPJ_FALSE;
11619         }
11620
11621         if ( /*(tile_index < 0) &&*/ (tile_index >= p_j2k->m_cp.tw * p_j2k->m_cp.th) ){
11622                 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);
11623                 return OPJ_FALSE;
11624         }
11625
11626         /* Compute the dimension of the desired tile*/
11627         l_tile_x = tile_index % p_j2k->m_cp.tw;
11628         l_tile_y = tile_index / p_j2k->m_cp.tw;
11629
11630         p_image->x0 = l_tile_x * p_j2k->m_cp.tdx + p_j2k->m_cp.tx0;
11631         if (p_image->x0 < p_j2k->m_private_image->x0)
11632                 p_image->x0 = p_j2k->m_private_image->x0;
11633         p_image->x1 = (l_tile_x + 1) * p_j2k->m_cp.tdx + p_j2k->m_cp.tx0;
11634         if (p_image->x1 > p_j2k->m_private_image->x1)
11635                 p_image->x1 = p_j2k->m_private_image->x1;
11636
11637         p_image->y0 = l_tile_y * p_j2k->m_cp.tdy + p_j2k->m_cp.ty0;
11638         if (p_image->y0 < p_j2k->m_private_image->y0)
11639                 p_image->y0 = p_j2k->m_private_image->y0;
11640         p_image->y1 = (l_tile_y + 1) * p_j2k->m_cp.tdy + p_j2k->m_cp.ty0;
11641         if (p_image->y1 > p_j2k->m_private_image->y1)
11642                 p_image->y1 = p_j2k->m_private_image->y1;
11643
11644         l_img_comp = p_image->comps;
11645         for (compno=0; compno < p_image->numcomps; ++compno)
11646         {
11647                 OPJ_INT32 l_comp_x1, l_comp_y1;
11648
11649                 l_img_comp->factor = p_j2k->m_private_image->comps[compno].factor;
11650
11651                 l_img_comp->x0 = int_ceildiv(p_image->x0, l_img_comp->dx);
11652                 l_img_comp->y0 = int_ceildiv(p_image->y0, l_img_comp->dy);
11653                 l_comp_x1 = int_ceildiv(p_image->x1, l_img_comp->dx);
11654                 l_comp_y1 = int_ceildiv(p_image->y1, l_img_comp->dy);
11655
11656                 l_img_comp->w = int_ceildivpow2(l_comp_x1, l_img_comp->factor) - int_ceildivpow2(l_img_comp->x0, l_img_comp->factor);
11657                 l_img_comp->h = int_ceildivpow2(l_comp_y1, l_img_comp->factor) - int_ceildivpow2(l_img_comp->y0, l_img_comp->factor);
11658
11659                 l_img_comp++;
11660         }
11661
11662         /* Destroy the previous output image*/
11663         if (p_j2k->m_output_image)
11664                 opj_image_destroy(p_j2k->m_output_image);
11665
11666         /* Create the ouput image from the information previously computed*/
11667         p_j2k->m_output_image = opj_image_create0();
11668         if (! (p_j2k->m_output_image)) {
11669                 return OPJ_FALSE;
11670         }
11671         opj_copy_image_header(p_image, p_j2k->m_output_image);
11672
11673         p_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec = tile_index;
11674
11675         /* customization of the decoding */
11676         j2k_setup_decoding_tile(p_j2k);
11677
11678         /* Decode the codestream */
11679         if (! j2k_exec (p_j2k,p_j2k->m_procedure_list,p_stream,p_manager)) {
11680                 opj_image_destroy(p_j2k->m_private_image);
11681                 p_j2k->m_private_image = NULL;
11682                 return OPJ_FALSE;
11683         }
11684
11685         /* Move data and copy one information from codec to output image*/
11686         for (compno = 0; compno < p_image->numcomps; compno++) {
11687                 p_image->comps[compno].resno_decoded = p_j2k->m_output_image->comps[compno].resno_decoded;
11688
11689                 if (p_image->comps[compno].data)
11690                         opj_free(p_image->comps[compno].data);
11691
11692                 p_image->comps[compno].data = p_j2k->m_output_image->comps[compno].data;
11693
11694                 p_j2k->m_output_image->comps[compno].data = NULL;
11695         }
11696
11697         return OPJ_TRUE;
11698 }
11699
11700 opj_bool j2k_set_decoded_resolution_factor(opj_j2k_v2_t *p_j2k, OPJ_UINT32 res_factor, opj_event_mgr_t * p_manager)
11701 {
11702         OPJ_UINT32 it_comp;
11703
11704         p_j2k->m_cp.m_specific_param.m_dec.m_reduce = res_factor;
11705
11706         if (p_j2k->m_private_image) {
11707                 if (p_j2k->m_private_image->comps) {
11708                         if (p_j2k->m_specific_param.m_decoder.m_default_tcp) {
11709                                 if (p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps) {
11710                                         for (it_comp = 0 ; it_comp < p_j2k->m_private_image->numcomps; it_comp++) {
11711                                                 OPJ_UINT32 max_res = p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps[it_comp].numresolutions;
11712                                                 if ( res_factor >= max_res){
11713                                                         opj_event_msg_v2(p_manager, EVT_ERROR, "Resolution factor is greater than the maximum resolution in the component.\n");
11714                                                         return OPJ_FALSE;
11715                                                 }
11716                                                 p_j2k->m_private_image->comps[it_comp].factor = res_factor;
11717                                         }
11718                                         return OPJ_TRUE;
11719                                 }
11720                         }
11721                 }
11722         }
11723
11724         return OPJ_FALSE;
11725 }
11726
11727
11728 /**
11729  * Encodes all the tiles in a row.
11730  */
11731 opj_bool j2k_encode_v2( opj_j2k_v2_t * p_j2k,
11732                                                 opj_stream_private_t *p_stream,
11733                                                 opj_event_mgr_t * p_manager )
11734 {
11735         OPJ_UINT32 i;
11736         OPJ_UINT32 l_nb_tiles;
11737         OPJ_UINT32 l_max_tile_size, l_current_tile_size;
11738         OPJ_BYTE * l_current_data;
11739
11740         /* preconditions */
11741         assert(p_j2k != 00);
11742         assert(p_stream != 00);
11743         assert(p_manager != 00);
11744
11745         l_current_data = (OPJ_BYTE*)opj_malloc(1000);
11746         if (! l_current_data) {
11747                 return OPJ_FALSE;
11748         }
11749         l_max_tile_size = 1000;
11750
11751         l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw;
11752         for (i=0;i<l_nb_tiles;++i) {
11753                 if (! j2k_pre_write_tile(p_j2k,i,p_stream,p_manager)) {
11754                         opj_free(l_current_data);
11755                         return OPJ_FALSE;
11756                 }
11757
11758                 l_current_tile_size = tcd_get_encoded_tile_size(p_j2k->m_tcd);
11759                 if (l_current_tile_size > l_max_tile_size) {
11760                         l_current_data = (OPJ_BYTE*)opj_realloc(l_current_data,l_current_tile_size);
11761                         if (! l_current_data) {
11762                                 return OPJ_FALSE;
11763                         }
11764                         l_max_tile_size = l_current_tile_size;
11765                 }
11766
11767                 j2k_get_tile_data(p_j2k->m_tcd,l_current_data);
11768
11769                 if (! j2k_post_write_tile (p_j2k,l_current_data,l_current_tile_size,p_stream,p_manager)) {
11770                         return OPJ_FALSE;
11771                 }
11772         }
11773
11774         opj_free(l_current_data);
11775         return OPJ_TRUE;
11776 }
11777
11778 /**
11779  * Ends the compression procedures and possibility add data to be read after the
11780  * codestream.
11781  */
11782 opj_bool j2k_end_compress(      opj_j2k_v2_t *p_j2k,
11783                                                         opj_stream_private_t *p_stream,
11784                                                         struct opj_event_mgr * p_manager)
11785 {
11786         /* customization of the encoding */
11787         j2k_setup_end_compress(p_j2k);
11788
11789         if (! j2k_exec (p_j2k, p_j2k->m_procedure_list, p_stream, p_manager))
11790         {
11791                 return OPJ_FALSE;
11792         }
11793
11794         return OPJ_TRUE;
11795 }
11796
11797
11798 /**
11799  * Starts a compression scheme, i.e. validates the codec parameters, writes the header.
11800  *
11801  * @param       p_j2k           the jpeg2000 codec.
11802  * @param       p_stream        the stream object.
11803  * @param       p_manager       the user event manager.
11804  *
11805  * @return true if the codec is valid.
11806  */
11807 opj_bool j2k_start_compress(opj_j2k_v2_t *p_j2k,
11808                                                         opj_stream_private_t *p_stream,
11809                                                         opj_image_t * p_image,
11810                                                         opj_event_mgr_t * p_manager)
11811 {
11812         /* preconditions */
11813         assert(p_j2k != 00);
11814         assert(p_stream != 00);
11815         assert(p_manager != 00);
11816
11817         p_j2k->m_private_image = opj_image_create0();
11818         opj_copy_image_header(p_image, p_j2k->m_private_image);
11819
11820         // TODO_MSD: Find a better way
11821         if (p_image->comps) {
11822                 OPJ_UINT32 it_comp;
11823                 for (it_comp = 0 ; it_comp < p_image->numcomps; it_comp++) {
11824                         if (p_image->comps[it_comp].data) {
11825                                 p_j2k->m_private_image->comps[it_comp].data =p_image->comps[it_comp].data;
11826                                 p_image->comps[it_comp].data = NULL;
11827
11828                         }
11829                 }
11830         }
11831
11832         /* customization of the validation */
11833         j2k_setup_encoding_validation (p_j2k);
11834
11835         /* validation of the parameters codec */
11836         if (! j2k_exec(p_j2k,p_j2k->m_validation_list,p_stream,p_manager)) {
11837                 return OPJ_FALSE;
11838         }
11839
11840         /* customization of the encoding */
11841         j2k_setup_header_writting(p_j2k);
11842
11843         /* write header */
11844         if (! j2k_exec (p_j2k,p_j2k->m_procedure_list,p_stream,p_manager)) {
11845                 return OPJ_FALSE;
11846         }
11847
11848         return OPJ_TRUE;
11849 }
11850
11851 /*
11852  *
11853  */
11854 opj_bool j2k_pre_write_tile (   opj_j2k_v2_t * p_j2k,
11855                                                                 OPJ_UINT32 p_tile_index,
11856                                                                 opj_stream_private_t *p_stream,
11857                                                                 opj_event_mgr_t * p_manager )
11858 {
11859   (void)p_stream;
11860         if (p_tile_index != p_j2k->m_current_tile_number) {
11861                 opj_event_msg_v2(p_manager, EVT_ERROR, "The given tile index does not match." );
11862                 return OPJ_FALSE;
11863         }
11864
11865         opj_event_msg_v2(p_manager, EVT_INFO, "tile number %d / %d\n", p_j2k->m_current_tile_number + 1, p_j2k->m_cp.tw * p_j2k->m_cp.th);
11866
11867         p_j2k->m_specific_param.m_encoder.m_current_tile_part_number = 0;
11868         p_j2k->m_tcd->cur_totnum_tp = p_j2k->m_cp.tcps[p_tile_index].m_nb_tile_parts;
11869         p_j2k->m_specific_param.m_encoder.m_current_poc_tile_part_number = 0;
11870
11871         /* initialisation before tile encoding  */
11872         if (! tcd_init_encode_tile(p_j2k->m_tcd, p_j2k->m_current_tile_number)) {
11873                 return OPJ_FALSE;
11874         }
11875
11876         return OPJ_TRUE;
11877 }
11878
11879 /*
11880  *
11881  */
11882 void j2k_get_tile_data (opj_tcd_v2_t * p_tcd, OPJ_BYTE * p_data)
11883 {
11884         OPJ_UINT32 i,j,k = 0;
11885         OPJ_UINT32 l_width,l_height,l_stride, l_offset_x,l_offset_y, l_image_width;
11886         opj_image_comp_t * l_img_comp = 00;
11887         opj_tcd_tilecomp_v2_t * l_tilec = 00;
11888         opj_image_t * l_image = 00;
11889         OPJ_UINT32 l_size_comp, l_remaining;
11890         OPJ_INT32 * l_src_ptr;
11891         l_tilec = p_tcd->tcd_image->tiles->comps;
11892         l_image = p_tcd->image;
11893         l_img_comp = l_image->comps;
11894
11895         for (i=0;i<p_tcd->image->numcomps;++i) {
11896                 l_size_comp = l_img_comp->prec >> 3; /* (/8) */
11897                 l_remaining = l_img_comp->prec & 7;  /* (%8) */
11898                 if (l_remaining) {
11899                         ++l_size_comp;
11900                 }
11901
11902                 if (l_size_comp == 3) {
11903                         l_size_comp = 4;
11904                 }
11905
11906                 l_width = (l_tilec->x1 - l_tilec->x0);
11907                 l_height = (l_tilec->y1 - l_tilec->y0);
11908                 l_offset_x = int_ceildiv(l_image->x0, l_img_comp->dx);
11909                 l_offset_y = int_ceildiv(l_image->y0, l_img_comp->dy);
11910                 l_image_width = int_ceildiv(l_image->x1 - l_image->x0, l_img_comp->dx);
11911                 l_stride = l_image_width - l_width;
11912                 l_src_ptr = l_img_comp->data + (l_tilec->x0 - l_offset_x) + (l_tilec->y0 - l_offset_y) * l_image_width;
11913
11914                 switch (l_size_comp) {
11915                         case 1:
11916                                 {
11917                                         OPJ_CHAR * l_dest_ptr = (OPJ_CHAR*) p_data;
11918                                         if (l_img_comp->sgnd) {
11919                                                 for     (j=0;j<l_height;++j) {
11920                                                         for (k=0;k<l_width;++k) {
11921                                                                 *(l_dest_ptr) = (OPJ_CHAR) (*l_src_ptr);
11922                                                                 ++l_dest_ptr;
11923                                                                 ++l_src_ptr;
11924                                                         }
11925                                                         l_src_ptr += l_stride;
11926                                                 }
11927                                         }
11928                                         else {
11929                                                 for (j=0;j<l_height;++j) {
11930                                                         for (k=0;k<l_width;++k) {
11931                                                                 *(l_dest_ptr) = (*l_src_ptr)&0xff;
11932                                                                 ++l_dest_ptr;
11933                                                                 ++l_src_ptr;
11934                                                         }
11935                                                         l_src_ptr += l_stride;
11936                                                 }
11937                                         }
11938
11939                                         p_data = (OPJ_BYTE*) l_dest_ptr;
11940                                 }
11941                                 break;
11942                         case 2:
11943                                 {
11944                                         OPJ_INT16 * l_dest_ptr = (OPJ_INT16 *) p_data;
11945                                         if (l_img_comp->sgnd) {
11946                                                 for (j=0;j<l_height;++j) {
11947                                                         for (k=0;k<l_width;++k) {
11948                                                                 *(l_dest_ptr++) = (OPJ_INT16) (*(l_src_ptr++));
11949                                                         }
11950                                                         l_src_ptr += l_stride;
11951                                                 }
11952                                         }
11953                                         else {
11954                                                 for (j=0;j<l_height;++j) {
11955                                                         for (k=0;k<l_width;++k) {
11956                                                                 *(l_dest_ptr++) = (*(l_src_ptr++))&0xffff;
11957                                                         }
11958                                                         l_src_ptr += l_stride;
11959                                                 }
11960                                         }
11961
11962                                         p_data = (OPJ_BYTE*) l_dest_ptr;
11963                                 }
11964                                 break;
11965                         case 4:
11966                                 {
11967                                         OPJ_INT32 * l_dest_ptr = (OPJ_INT32 *) p_data;
11968                                         for (j=0;j<l_height;++j) {
11969                                                 for (k=0;k<l_width;++k) {
11970                                                         *(l_dest_ptr++) = *(l_src_ptr++);
11971                                                 }
11972                                                 l_src_ptr += l_stride;
11973                                         }
11974
11975                                         p_data = (OPJ_BYTE*) l_dest_ptr;
11976                                 }
11977                                 break;
11978                 }
11979
11980                 ++l_img_comp;
11981                 ++l_tilec;
11982         }
11983 }
11984
11985
11986 /**
11987  * Write a tile.
11988  * @param       p_j2k           the jpeg2000 codec.
11989  * @param       p_stream        the stream to write data to.
11990  * @param       p_manager       the user event manager.
11991  */
11992 opj_bool j2k_post_write_tile (  opj_j2k_v2_t * p_j2k,
11993                                                                 OPJ_BYTE * p_data,
11994                                                                 OPJ_UINT32 p_data_size,
11995                                                                 opj_stream_private_t *p_stream,
11996                                                                 opj_event_mgr_t * p_manager )
11997 {
11998         opj_tcd_v2_t * l_tcd = 00;
11999         opj_cp_v2_t * l_cp = 00;
12000         opj_tcp_v2_t * l_tcp = 00;
12001         OPJ_UINT32 l_nb_bytes_written;
12002         OPJ_BYTE * l_current_data = 00;
12003         OPJ_UINT32 l_tile_size = 0;
12004         OPJ_UINT32 l_available_data;
12005
12006         /* preconditions */
12007         assert(p_j2k->m_specific_param.m_encoder.m_encoded_tile_data);
12008
12009         l_tcd = p_j2k->m_tcd;
12010         l_cp = &(p_j2k->m_cp);
12011         l_tcp = l_cp->tcps + p_j2k->m_current_tile_number;
12012
12013         l_tile_size = p_j2k->m_specific_param.m_encoder.m_encoded_tile_size;
12014         l_available_data = l_tile_size;
12015         l_current_data = p_j2k->m_specific_param.m_encoder.m_encoded_tile_data;
12016
12017         if (! tcd_copy_tile_data(l_tcd,p_data,p_data_size)) {
12018                 opj_event_msg_v2(p_manager, EVT_ERROR, "Size mismatch between tile data and sent data." );
12019                 return OPJ_FALSE;
12020         }
12021
12022         l_nb_bytes_written = 0;
12023         if (! j2k_write_first_tile_part(p_j2k,l_current_data,&l_nb_bytes_written,l_available_data,p_stream,p_manager)) {
12024                 return OPJ_FALSE;
12025         }
12026         l_current_data += l_nb_bytes_written;
12027         l_available_data -= l_nb_bytes_written;
12028
12029         l_nb_bytes_written = 0;
12030         if (! j2k_write_all_tile_parts(p_j2k,l_current_data,&l_nb_bytes_written,l_available_data,p_stream,p_manager)) {
12031                 return OPJ_FALSE;
12032         }
12033
12034         l_available_data -= l_nb_bytes_written;
12035         l_nb_bytes_written = l_tile_size - l_available_data;
12036
12037         if ( opj_stream_write_data(     p_stream,
12038                                                                 p_j2k->m_specific_param.m_encoder.m_encoded_tile_data,
12039                                                                 l_nb_bytes_written,p_manager) != l_nb_bytes_written) {
12040                 return OPJ_FALSE;
12041         }
12042
12043         ++p_j2k->m_current_tile_number;
12044
12045         return OPJ_TRUE;
12046 }
12047
12048
12049 /**
12050  * Sets up the validation ,i.e. adds the procedures to lauch to make sure the codec parameters
12051  * are valid. Developers wanting to extend the library can add their own validation procedures.
12052  */
12053 void j2k_setup_end_compress (opj_j2k_v2_t *p_j2k)
12054 {
12055         /* preconditions */
12056         assert(p_j2k != 00);
12057
12058         /* DEVELOPER CORNER, insert your custom procedures */
12059         opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_eoc_v2 );
12060
12061         if (p_j2k->m_cp.m_specific_param.m_enc.m_cinema) {
12062                 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_updated_tlm);
12063         }
12064
12065         opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_epc );
12066         opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_end_encoding );
12067         opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_destroy_header_memory);
12068 }
12069
12070 /**
12071  * Sets up the validation ,i.e. adds the procedures to lauch to make sure the codec parameters
12072  * are valid. Developers wanting to extend the library can add their own validation procedures.
12073  */
12074 void j2k_setup_encoding_validation (opj_j2k_v2_t *p_j2k)
12075 {
12076         /* preconditions */
12077         assert(p_j2k != 00);
12078
12079         opj_procedure_list_add_procedure(p_j2k->m_validation_list, (opj_procedure)j2k_build_encoder);
12080         opj_procedure_list_add_procedure(p_j2k->m_validation_list, (opj_procedure)j2k_encoding_validation);
12081
12082         /* DEVELOPER CORNER, add your custom validation procedure */
12083         opj_procedure_list_add_procedure(p_j2k->m_validation_list, (opj_procedure)j2k_mct_validation);
12084 }
12085
12086
12087 /**
12088  * Sets up the procedures to do on writing header.
12089  * Developers wanting to extend the library can add their own writing procedures.
12090  */
12091 void j2k_setup_header_writting (opj_j2k_v2_t *p_j2k)
12092 {
12093         /* preconditions */
12094         assert(p_j2k != 00);
12095
12096         opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_init_info );
12097         opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_soc_v2 );
12098         opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_siz_v2 );
12099         opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_cod_v2 );
12100         opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_qcd_v2 );
12101
12102
12103         if (p_j2k->m_cp.m_specific_param.m_enc.m_cinema) {
12104                 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_image_components );
12105                 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_tlm_v2 );
12106
12107                 if (p_j2k->m_cp.m_specific_param.m_enc.m_cinema == CINEMA4K_24) {
12108                         opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_poc_v2 );
12109                 }
12110         }
12111
12112         opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_regions);
12113
12114         if (p_j2k->m_cp.comment != 00)  {
12115                 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_com_v2);
12116         }
12117
12118         /* DEVELOPER CORNER, insert your custom procedures */
12119         if (p_j2k->m_cp.rsiz & MCT) {
12120                 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_mct_data_group );
12121         }
12122         /* End of Developer Corner */
12123
12124         if (p_j2k->cstr_index) {
12125                 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_get_end_header );
12126         }
12127
12128         opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_create_tcd);
12129         opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_update_rates);
12130 }
12131
12132
12133 opj_bool j2k_write_first_tile_part (opj_j2k_v2_t *p_j2k,
12134                                                                         OPJ_BYTE * p_data,
12135                                                                         OPJ_UINT32 * p_data_written,
12136                                                                         OPJ_UINT32 p_total_data_size,
12137                                                                         opj_stream_private_t *p_stream,
12138                                                                         struct opj_event_mgr * p_manager )
12139 {
12140         OPJ_UINT32 compno;
12141         OPJ_UINT32 l_nb_bytes_written = 0;
12142         OPJ_UINT32 l_current_nb_bytes_written;
12143         OPJ_BYTE * l_begin_data = 00;
12144
12145         opj_tcp_v2_t *l_tcp = 00;
12146         opj_tcd_v2_t * l_tcd = 00;
12147         opj_cp_v2_t * l_cp = 00;
12148
12149         l_tcd = p_j2k->m_tcd;
12150         l_cp = &(p_j2k->m_cp);
12151         l_tcp = l_cp->tcps + p_j2k->m_current_tile_number;
12152
12153         l_tcd->cur_pino = 0;
12154
12155         /*Get number of tile parts*/
12156         p_j2k->m_specific_param.m_encoder.m_current_poc_tile_part_number = 0;
12157
12158         /* INDEX >> */
12159         /* << INDEX */
12160
12161         l_current_nb_bytes_written = 0;
12162         l_begin_data = p_data;
12163         if (! j2k_write_sot_v2(p_j2k,p_data,&l_current_nb_bytes_written,p_stream,p_manager))
12164         {
12165                 return OPJ_FALSE;
12166         }
12167
12168         l_nb_bytes_written += l_current_nb_bytes_written;
12169         p_data += l_current_nb_bytes_written;
12170         p_total_data_size -= l_current_nb_bytes_written;
12171
12172         if (l_cp->m_specific_param.m_enc.m_cinema == 0) {
12173                 for (compno = 1; compno < p_j2k->m_private_image->numcomps; compno++) {
12174                         l_current_nb_bytes_written = 0;
12175                         j2k_write_coc_in_memory(p_j2k,compno,p_data,&l_current_nb_bytes_written,p_manager);
12176                         l_nb_bytes_written += l_current_nb_bytes_written;
12177                         p_data += l_current_nb_bytes_written;
12178                         p_total_data_size -= l_current_nb_bytes_written;
12179
12180                         l_current_nb_bytes_written = 0;
12181                         j2k_write_qcc_in_memory(p_j2k,compno,p_data,&l_current_nb_bytes_written,p_manager);
12182                         l_nb_bytes_written += l_current_nb_bytes_written;
12183                         p_data += l_current_nb_bytes_written;
12184                         p_total_data_size -= l_current_nb_bytes_written;
12185                 }
12186
12187                 if (l_cp->tcps[p_j2k->m_current_tile_number].numpocs) {
12188                         l_current_nb_bytes_written = 0;
12189                         j2k_write_poc_in_memory(p_j2k,p_data,&l_current_nb_bytes_written,p_manager);
12190                         l_nb_bytes_written += l_current_nb_bytes_written;
12191                         p_data += l_current_nb_bytes_written;
12192                         p_total_data_size -= l_current_nb_bytes_written;
12193                 }
12194         }
12195
12196         l_current_nb_bytes_written = 0;
12197         if (! j2k_write_sod_v2(p_j2k,l_tcd,p_data,&l_current_nb_bytes_written,p_total_data_size,p_stream,p_manager)) {
12198                 return OPJ_FALSE;
12199         }
12200
12201         l_nb_bytes_written += l_current_nb_bytes_written;
12202         * p_data_written = l_nb_bytes_written;
12203
12204         /* Writing Psot in SOT marker */
12205         opj_write_bytes(l_begin_data + 6,l_nb_bytes_written,4);                                 /* PSOT */
12206
12207         if (l_cp->m_specific_param.m_enc.m_cinema){
12208                 j2k_update_tlm(p_j2k,l_nb_bytes_written);
12209         }
12210
12211         return OPJ_TRUE;
12212 }
12213
12214 opj_bool j2k_write_all_tile_parts(      opj_j2k_v2_t *p_j2k,
12215                                                                         OPJ_BYTE * p_data,
12216                                                                         OPJ_UINT32 * p_data_written,
12217                                                                         OPJ_UINT32 p_total_data_size,
12218                                                                         opj_stream_private_t *p_stream,
12219                                                                         struct opj_event_mgr * p_manager
12220                                                                 )
12221 {
12222         OPJ_UINT32 tilepartno=0;
12223         OPJ_UINT32 l_nb_bytes_written = 0;
12224         OPJ_UINT32 l_current_nb_bytes_written;
12225         OPJ_UINT32 l_part_tile_size;
12226         OPJ_UINT32 tot_num_tp;
12227         OPJ_UINT32 pino;
12228
12229         OPJ_BYTE * l_begin_data;
12230         opj_tcp_v2_t *l_tcp = 00;
12231         opj_tcd_v2_t * l_tcd = 00;
12232         opj_cp_v2_t * l_cp = 00;
12233
12234
12235         l_tcd = p_j2k->m_tcd;
12236         l_cp = &(p_j2k->m_cp);
12237         l_tcp = l_cp->tcps + p_j2k->m_current_tile_number;
12238
12239         /*Get number of tile parts*/
12240         tot_num_tp = j2k_get_num_tp_v2(l_cp,0,p_j2k->m_current_tile_number);
12241
12242         for (tilepartno = 1; tilepartno < tot_num_tp ; ++tilepartno) {
12243                 p_j2k->m_specific_param.m_encoder.m_current_poc_tile_part_number = tilepartno;
12244                 l_current_nb_bytes_written = 0;
12245                 l_part_tile_size = 0;
12246                 l_begin_data = p_data;
12247
12248                 if (! j2k_write_sot_v2(p_j2k,p_data,&l_current_nb_bytes_written,p_stream,p_manager)) {
12249                         return OPJ_FALSE;
12250                 }
12251
12252                 l_nb_bytes_written += l_current_nb_bytes_written;
12253                 p_data += l_current_nb_bytes_written;
12254                 p_total_data_size -= l_current_nb_bytes_written;
12255                 l_part_tile_size += l_nb_bytes_written;
12256
12257                 l_current_nb_bytes_written = 0;
12258                 if (! j2k_write_sod_v2(p_j2k,l_tcd,p_data,&l_current_nb_bytes_written,p_total_data_size,p_stream,p_manager)) {
12259                         return OPJ_FALSE;
12260                 }
12261
12262                 p_data += l_current_nb_bytes_written;
12263                 l_nb_bytes_written += l_current_nb_bytes_written;
12264                 p_total_data_size -= l_current_nb_bytes_written;
12265                 l_part_tile_size += l_nb_bytes_written;
12266
12267                 /* Writing Psot in SOT marker */
12268                 opj_write_bytes(l_begin_data + 6,l_part_tile_size,4);                                   /* PSOT */
12269
12270                 if (l_cp->m_specific_param.m_enc.m_cinema) {
12271                         j2k_update_tlm(p_j2k,l_part_tile_size);
12272                 }
12273
12274                 ++p_j2k->m_specific_param.m_encoder.m_current_tile_part_number;
12275         }
12276
12277         for (pino = 1; pino <= l_tcp->numpocs; ++pino) {
12278                 l_tcd->cur_pino = pino;
12279
12280                 /*Get number of tile parts*/
12281                 tot_num_tp = j2k_get_num_tp_v2(l_cp,pino,p_j2k->m_current_tile_number);
12282                 for (tilepartno = 0; tilepartno < tot_num_tp ; ++tilepartno) {
12283                         p_j2k->m_specific_param.m_encoder.m_current_poc_tile_part_number = tilepartno;
12284                         l_current_nb_bytes_written = 0;
12285                         l_part_tile_size = 0;
12286                         l_begin_data = p_data;
12287
12288                         if (! j2k_write_sot_v2(p_j2k,p_data,&l_current_nb_bytes_written,p_stream,p_manager)) {
12289                                 return OPJ_FALSE;
12290                         }
12291
12292                         l_nb_bytes_written += l_current_nb_bytes_written;
12293                         p_data += l_current_nb_bytes_written;
12294                         p_total_data_size -= l_current_nb_bytes_written;
12295                         l_part_tile_size += l_current_nb_bytes_written;
12296
12297                         l_current_nb_bytes_written = 0;
12298
12299                         if (! j2k_write_sod_v2(p_j2k,l_tcd,p_data,&l_current_nb_bytes_written,p_total_data_size,p_stream,p_manager)) {
12300                                 return OPJ_FALSE;
12301                         }
12302
12303                         l_nb_bytes_written += l_current_nb_bytes_written;
12304                         p_data += l_current_nb_bytes_written;
12305                         p_total_data_size -= l_current_nb_bytes_written;
12306                         l_part_tile_size += l_current_nb_bytes_written;
12307
12308                         /* Writing Psot in SOT marker */
12309                         opj_write_bytes(l_begin_data + 6,l_part_tile_size,4);                                   /* PSOT */
12310
12311                         if (l_cp->m_specific_param.m_enc.m_cinema) {
12312                                 j2k_update_tlm(p_j2k,l_part_tile_size);
12313                         }
12314
12315                         ++p_j2k->m_specific_param.m_encoder.m_current_tile_part_number;
12316                 }
12317         }
12318
12319         *p_data_written = l_nb_bytes_written;
12320
12321         return OPJ_TRUE;
12322 }
12323
12324 /**
12325  * Writes the updated tlm.
12326  *
12327  * @param       p_stream                the stream to write data to.
12328  * @param       p_j2k                   J2K codec.
12329  * @param       p_manager               the user event manager.
12330 */
12331 opj_bool j2k_write_updated_tlm( opj_j2k_v2_t *p_j2k,
12332                                                                 struct opj_stream_private *p_stream,
12333                                                                 struct opj_event_mgr * p_manager )
12334 {
12335         OPJ_UINT32 l_tlm_size;
12336         OPJ_SIZE_T l_tlm_position, l_current_position;
12337
12338         /* preconditions */
12339         assert(p_j2k != 00);
12340         assert(p_manager != 00);
12341         assert(p_stream != 00);
12342
12343         l_tlm_size = 5 * p_j2k->m_specific_param.m_encoder.m_total_tile_parts;
12344         l_tlm_position = 6 + p_j2k->m_specific_param.m_encoder.m_tlm_start;
12345         l_current_position = opj_stream_tell(p_stream);
12346
12347         if (! opj_stream_seek(p_stream,l_tlm_position,p_manager)) {
12348                 return OPJ_FALSE;
12349         }
12350
12351         if (opj_stream_write_data(p_stream,p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer,l_tlm_size,p_manager) != l_tlm_size) {
12352                 return OPJ_FALSE;
12353         }
12354
12355         if (! opj_stream_seek(p_stream,l_current_position,p_manager)) {
12356                 return OPJ_FALSE;
12357         }
12358
12359         return OPJ_TRUE;
12360 }
12361
12362
12363 /**
12364  * Ends the encoding, i.e. frees memory.
12365  *
12366  * @param       p_stream                                the stream to write data to.
12367  * @param       p_j2k                           J2K codec.
12368  * @param       p_manager               the user event manager.
12369 */
12370 opj_bool j2k_end_encoding(      opj_j2k_v2_t *p_j2k,
12371                                                         struct opj_stream_private *p_stream,
12372                                                         struct opj_event_mgr * p_manager )
12373 {
12374         /* preconditions */
12375         assert(p_j2k != 00);
12376         assert(p_manager != 00);
12377         assert(p_stream != 00);
12378
12379         tcd_destroy_v2(p_j2k->m_tcd);
12380         p_j2k->m_tcd = 00;
12381
12382         if (p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer) {
12383                 opj_free(p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer);
12384                 p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer = 0;
12385                 p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current = 0;
12386         }
12387
12388         if (p_j2k->m_specific_param.m_encoder.m_encoded_tile_data) {
12389                 opj_free(p_j2k->m_specific_param.m_encoder.m_encoded_tile_data);
12390                 p_j2k->m_specific_param.m_encoder.m_encoded_tile_data = 0;
12391         }
12392
12393         p_j2k->m_specific_param.m_encoder.m_encoded_tile_size = 0;
12394
12395         return OPJ_TRUE;
12396 }
12397
12398 /**
12399  * Destroys the memory associated with the decoding of headers.
12400  */
12401 opj_bool j2k_destroy_header_memory (opj_j2k_v2_t * p_j2k,
12402                                                                         opj_stream_private_t *p_stream,
12403                                                                         opj_event_mgr_t * p_manager )
12404 {
12405         /* preconditions */
12406         assert(p_j2k != 00);
12407         assert(p_stream != 00);
12408         assert(p_manager != 00);
12409
12410         if (p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
12411                 opj_free(p_j2k->m_specific_param.m_encoder.m_header_tile_data);
12412                 p_j2k->m_specific_param.m_encoder.m_header_tile_data = 0;
12413         }
12414
12415         p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = 0;
12416
12417         return OPJ_TRUE;
12418 }
12419
12420
12421 /**
12422  * Inits the Info
12423  *
12424  * @param       p_stream                                the stream to write data to.
12425  * @param       p_j2k                           J2K codec.
12426  * @param       p_manager               the user event manager.
12427 */
12428 opj_bool j2k_init_info( opj_j2k_v2_t *p_j2k,
12429                                                 struct opj_stream_private *p_stream,
12430                                                 struct opj_event_mgr * p_manager )
12431 {
12432         opj_codestream_info_t * l_cstr_info = 00;
12433
12434         /* preconditions */
12435         assert(p_j2k != 00);
12436         assert(p_manager != 00);
12437         assert(p_stream != 00);
12438   (void)l_cstr_info;
12439
12440         /* TODO mergeV2: check this part which use cstr_info */
12441         /*l_cstr_info = p_j2k->cstr_info;
12442
12443         if (l_cstr_info)  {
12444                 OPJ_UINT32 compno;
12445                 l_cstr_info->tile = (opj_tile_info_t *) opj_malloc(p_j2k->m_cp.tw * p_j2k->m_cp.th * sizeof(opj_tile_info_t));
12446
12447                 l_cstr_info->image_w = p_j2k->m_image->x1 - p_j2k->m_image->x0;
12448                 l_cstr_info->image_h = p_j2k->m_image->y1 - p_j2k->m_image->y0;
12449
12450                 l_cstr_info->prog = (&p_j2k->m_cp.tcps[0])->prg;
12451
12452                 l_cstr_info->tw = p_j2k->m_cp.tw;
12453                 l_cstr_info->th = p_j2k->m_cp.th;
12454
12455                 l_cstr_info->tile_x = p_j2k->m_cp.tdx;*/        /* new version parser */
12456                 /*l_cstr_info->tile_y = p_j2k->m_cp.tdy;*/      /* new version parser */
12457                 /*l_cstr_info->tile_Ox = p_j2k->m_cp.tx0;*/     /* new version parser */
12458                 /*l_cstr_info->tile_Oy = p_j2k->m_cp.ty0;*/     /* new version parser */
12459
12460                 /*l_cstr_info->numcomps = p_j2k->m_image->numcomps;
12461
12462                 l_cstr_info->numlayers = (&p_j2k->m_cp.tcps[0])->numlayers;
12463
12464                 l_cstr_info->numdecompos = (OPJ_INT32*) opj_malloc(p_j2k->m_image->numcomps * sizeof(OPJ_INT32));
12465
12466                 for (compno=0; compno < p_j2k->m_image->numcomps; compno++) {
12467                         l_cstr_info->numdecompos[compno] = (&p_j2k->m_cp.tcps[0])->tccps->numresolutions - 1;
12468                 }
12469
12470                 l_cstr_info->D_max = 0.0;       */      /* ADD Marcela */
12471
12472                 /*l_cstr_info->main_head_start = opj_stream_tell(p_stream);*/ /* position of SOC */
12473
12474                 /*l_cstr_info->maxmarknum = 100;
12475                 l_cstr_info->marker = (opj_marker_info_t *) opj_malloc(l_cstr_info->maxmarknum * sizeof(opj_marker_info_t));
12476                 l_cstr_info->marknum = 0;
12477         }*/
12478
12479         return j2k_calculate_tp_v2(p_j2k,&(p_j2k->m_cp),&p_j2k->m_specific_param.m_encoder.m_total_tile_parts,p_j2k->m_private_image,p_manager);
12480 }
12481
12482 /**
12483  * Creates a tile-coder decoder.
12484  *
12485  * @param       p_stream                the stream to write data to.
12486  * @param       p_j2k                   J2K codec.
12487  * @param       p_manager               the user event manager.
12488 */
12489 opj_bool j2k_create_tcd(opj_j2k_v2_t *p_j2k,
12490                                                 struct opj_stream_private *p_stream,
12491                                                 struct opj_event_mgr * p_manager )
12492 {
12493         /* preconditions */
12494         assert(p_j2k != 00);
12495         assert(p_manager != 00);
12496         assert(p_stream != 00);
12497
12498         p_j2k->m_tcd = tcd_create_v2(OPJ_FALSE);
12499
12500         if (! p_j2k->m_tcd) {
12501                 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to create Tile Coder\n");
12502                 return OPJ_FALSE;
12503         }
12504
12505         if (! tcd_init_v2(p_j2k->m_tcd,p_j2k->m_private_image,&p_j2k->m_cp)) {
12506                 tcd_destroy_v2(p_j2k->m_tcd);
12507                 p_j2k->m_tcd = 00;
12508                 return OPJ_FALSE;
12509         }
12510
12511         return OPJ_TRUE;
12512 }
12513
12514
12515 /**
12516  * Writes a tile.
12517  * @param       p_j2k           the jpeg2000 codec.
12518  * @param       p_stream                        the stream to write data to.
12519  * @param       p_manager       the user event manager.
12520  */
12521 opj_bool j2k_write_tile (opj_j2k_v2_t * p_j2k,
12522                                                  OPJ_UINT32 p_tile_index,
12523                                                  OPJ_BYTE * p_data,
12524                                                  OPJ_UINT32 p_data_size,
12525                                                  opj_stream_private_t *p_stream,
12526                                                  opj_event_mgr_t * p_manager )
12527 {
12528         if (! j2k_pre_write_tile(p_j2k,p_tile_index,p_stream,p_manager)) {
12529                 opj_event_msg_v2(p_manager, EVT_ERROR, "Error while j2k_pre_write_tile with tile index = %d\n", p_tile_index);
12530                 return OPJ_FALSE;
12531         }
12532         else {
12533                 if (! j2k_post_write_tile(p_j2k,p_data,p_data_size,p_stream,p_manager)) {
12534                         opj_event_msg_v2(p_manager, EVT_ERROR, "Error while j2k_post_write_tile with tile index = %d\n", p_tile_index);
12535                         return OPJ_FALSE;
12536                 }
12537         }
12538
12539         return OPJ_TRUE;
12540 }