Initial commit.
[pdfium.git] / core / src / fxcodec / fx_libopenjpeg / libopenjpeg20 / j2k.c
1 /*
2  * The copyright in this software is being made available under the 2-clauses 
3  * BSD License, included below. This software may be subject to other third 
4  * party and contributor rights, including patent rights, and no such rights
5  * are granted under this license.
6  *
7  * Copyright (c) 2002-2014, Universite catholique de Louvain (UCL), Belgium
8  * Copyright (c) 2002-2014, Professor Benoit Macq
9  * Copyright (c) 2001-2003, David Janssens
10  * Copyright (c) 2002-2003, Yannick Verschueren
11  * Copyright (c) 2003-2007, Francois-Olivier Devaux 
12  * Copyright (c) 2003-2014, Antonin Descampe
13  * Copyright (c) 2005, Herve Drolon, FreeImage Team
14  * Copyright (c) 2008, Jerome Fimes, Communications & Systemes <jerome.fimes@c-s.fr>
15  * Copyright (c) 2006-2007, Parvatha Elangovan
16  * Copyright (c) 2010-2011, Kaori Hagihara
17  * Copyright (c) 2011-2012, Centre National d'Etudes Spatiales (CNES), France 
18  * Copyright (c) 2012, CS Systemes d'Information, France
19  * All rights reserved.
20  *
21  * Redistribution and use in source and binary forms, with or without
22  * modification, are permitted provided that the following conditions
23  * are met:
24  * 1. Redistributions of source code must retain the above copyright
25  *    notice, this list of conditions and the following disclaimer.
26  * 2. Redistributions in binary form must reproduce the above copyright
27  *    notice, this list of conditions and the following disclaimer in the
28  *    documentation and/or other materials provided with the distribution.
29  *
30  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
31  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
32  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
33  * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
34  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
35  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
36  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
37  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
38  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
39  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
40  * POSSIBILITY OF SUCH DAMAGE.
41  */
42
43 #include "opj_includes.h"
44
45 #define CINEMA_24_CS 1302083    /*Codestream length for 24fps*/
46 #define CINEMA_48_CS 651041             /*Codestream length for 48fps*/
47 #define COMP_24_CS 1041666              /*Maximum size per color component for 2K & 4K @ 24fps*/
48 #define COMP_48_CS 520833               /*Maximum size per color component for 2K @ 48fps*/
49
50 /** @defgroup J2K J2K - JPEG-2000 codestream reader/writer */
51 /*@{*/
52
53 /** @name Local static functions */
54 /*@{*/
55
56 /**
57  * Sets up the procedures to do on reading header. Developpers wanting to extend the library can add their own reading procedures.
58  */
59 static void opj_j2k_setup_header_reading (opj_j2k_t *p_j2k);
60
61 /**
62  * The read header procedure.
63  */
64 static OPJ_BOOL opj_j2k_read_header_procedure(  opj_j2k_t *p_j2k,
65                                                 opj_stream_private_t *p_stream,
66                                                 opj_event_mgr_t * p_manager);
67
68 /**
69  * The default encoding validation procedure without any extension.
70  *
71  * @param       p_j2k                   the jpeg2000 codec to validate.
72  * @param       p_stream                the input stream to validate.
73  * @param       p_manager               the user event manager.
74  *
75  * @return true if the parameters are correct.
76  */
77 static OPJ_BOOL opj_j2k_encoding_validation (   opj_j2k_t * p_j2k,
78                                                 opj_stream_private_t *p_stream,
79                                                 opj_event_mgr_t * p_manager );
80
81 /**
82  * The default decoding validation procedure without any extension.
83  *
84  * @param       p_j2k                   the jpeg2000 codec to validate.
85  * @param       p_stream                                the input stream to validate.
86  * @param       p_manager               the user event manager.
87  *
88  * @return true if the parameters are correct.
89  */
90 static OPJ_BOOL opj_j2k_decoding_validation (   opj_j2k_t * p_j2k,
91                                                 opj_stream_private_t *p_stream,
92                                                 opj_event_mgr_t * p_manager );
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 opj_j2k_setup_encoding_validation (opj_j2k_t *p_j2k);
99
100 /**
101  * Sets up the validation ,i.e. adds the procedures to lauch to make sure the codec parameters
102  * are valid. Developpers wanting to extend the library can add their own validation procedures.
103  */
104 static void opj_j2k_setup_decoding_validation (opj_j2k_t *p_j2k);
105
106 /**
107  * Sets up the validation ,i.e. adds the procedures to lauch to make sure the codec parameters
108  * are valid. Developpers wanting to extend the library can add their own validation procedures.
109  */
110 static void opj_j2k_setup_end_compress (opj_j2k_t *p_j2k);
111
112 /**
113  * The mct encoding validation procedure.
114  *
115  * @param       p_j2k                   the jpeg2000 codec to validate.
116  * @param       p_stream                                the input stream to validate.
117  * @param       p_manager               the user event manager.
118  *
119  * @return true if the parameters are correct.
120  */
121 static OPJ_BOOL opj_j2k_mct_validation (opj_j2k_t * p_j2k,
122                                         opj_stream_private_t *p_stream,
123                                         opj_event_mgr_t * p_manager );
124
125 /**
126  * Builds the tcd decoder to use to decode tile.
127  */
128 static OPJ_BOOL opj_j2k_build_decoder ( opj_j2k_t * p_j2k,
129                                         opj_stream_private_t *p_stream,
130                                         opj_event_mgr_t * p_manager );
131 /**
132  * Builds the tcd encoder to use to encode tile.
133  */
134 static OPJ_BOOL opj_j2k_build_encoder ( opj_j2k_t * p_j2k,
135                                         opj_stream_private_t *p_stream,
136                                         opj_event_mgr_t * p_manager );
137
138 /**
139  * Creates a tile-coder decoder.
140  *
141  * @param       p_stream                        the stream to write data to.
142  * @param       p_j2k                           J2K codec.
143  * @param       p_manager                   the user event manager.
144 */
145 static OPJ_BOOL opj_j2k_create_tcd(     opj_j2k_t *p_j2k,
146                                                                     opj_stream_private_t *p_stream,
147                                                                     opj_event_mgr_t * p_manager );
148
149 /**
150  * Excutes the given procedures on the given codec.
151  *
152  * @param       p_procedure_list        the list of procedures to execute
153  * @param       p_j2k                           the jpeg2000 codec to execute the procedures on.
154  * @param       p_stream                        the stream to execute the procedures on.
155  * @param       p_manager                       the user manager.
156  *
157  * @return      true                            if all the procedures were successfully executed.
158  */
159 static OPJ_BOOL opj_j2k_exec (  opj_j2k_t * p_j2k,
160                             opj_procedure_list_t * p_procedure_list,
161                             opj_stream_private_t *p_stream,
162                             opj_event_mgr_t * p_manager);
163
164 /**
165  * Updates the rates of the tcp.
166  *
167  * @param       p_stream                                the stream to write data to.
168  * @param       p_j2k                           J2K codec.
169  * @param       p_manager               the user event manager.
170 */
171 static OPJ_BOOL opj_j2k_update_rates(   opj_j2k_t *p_j2k,
172                                                                             opj_stream_private_t *p_stream,
173                                                                             opj_event_mgr_t * p_manager );
174
175 /**
176  * Copies the decoding tile parameters onto all the tile parameters.
177  * Creates also the tile decoder.
178  */
179 static OPJ_BOOL opj_j2k_copy_default_tcp_and_create_tcd (       opj_j2k_t * p_j2k,
180                                                             opj_stream_private_t *p_stream,
181                                                             opj_event_mgr_t * p_manager );
182
183 /**
184  * Destroys the memory associated with the decoding of headers.
185  */
186 static OPJ_BOOL opj_j2k_destroy_header_memory ( opj_j2k_t * p_j2k,
187                                                 opj_stream_private_t *p_stream,
188                                                 opj_event_mgr_t * p_manager );
189
190 /**
191  * Reads the lookup table containing all the marker, status and action, and returns the handler associated
192  * with the marker value.
193  * @param       p_id            Marker value to look up
194  *
195  * @return      the handler associated with the id.
196 */
197 static const struct opj_dec_memory_marker_handler * opj_j2k_get_marker_handler (OPJ_UINT32 p_id);
198
199 /**
200  * Destroys a tile coding parameter structure.
201  *
202  * @param       p_tcp           the tile coding parameter to destroy.
203  */
204 static void opj_j2k_tcp_destroy (opj_tcp_t *p_tcp);
205
206 /**
207  * Destroys the data inside a tile coding parameter structure.
208  *
209  * @param       p_tcp           the tile coding parameter which contain data to destroy.
210  */
211 static void opj_j2k_tcp_data_destroy (opj_tcp_t *p_tcp);
212
213 /**
214  * Destroys a coding parameter structure.
215  *
216  * @param       p_cp            the coding parameter to destroy.
217  */
218 static void opj_j2k_cp_destroy (opj_cp_t *p_cp);
219
220 /**
221  * Writes a SPCod or SPCoc element, i.e. the coding style of a given component of a tile.
222  *
223  * @param       p_j2k           J2K codec.
224  * @param       p_tile_no       FIXME DOC
225  * @param       p_comp_no       the component number to output.
226  * @param       p_data          FIXME DOC
227  * @param       p_header_size   FIXME DOC
228  * @param       p_manager       the user event manager.
229  *
230  * @return FIXME DOC
231 */
232 static OPJ_BOOL opj_j2k_write_SPCod_SPCoc(      opj_j2k_t *p_j2k,
233                                                                                     OPJ_UINT32 p_tile_no,
234                                                                                     OPJ_UINT32 p_comp_no,
235                                                                                     OPJ_BYTE * p_data,
236                                                                                     OPJ_UINT32 * p_header_size,
237                                                                                     opj_event_mgr_t * p_manager );
238
239 /**
240  * Gets the size taken by writing a SPCod or SPCoc for the given tile and component.
241  *
242  * @param       p_j2k                   the J2K codec.
243  * @param       p_tile_no               the tile index.
244  * @param       p_comp_no               the component being outputted.
245  *
246  * @return      the number of bytes taken by the SPCod element.
247  */
248 static OPJ_UINT32 opj_j2k_get_SPCod_SPCoc_size (opj_j2k_t *p_j2k,
249                                                                                             OPJ_UINT32 p_tile_no,
250                                                                                             OPJ_UINT32 p_comp_no );
251
252 /**
253  * Reads a SPCod or SPCoc element, i.e. the coding style of a given component of a tile.
254  * @param       p_j2k           the jpeg2000 codec.
255  * @param       compno          FIXME DOC
256  * @param       p_header_data   the data contained in the COM box.
257  * @param       p_header_size   the size of the data contained in the COM marker.
258  * @param       p_manager       the user event manager.
259 */
260 static OPJ_BOOL opj_j2k_read_SPCod_SPCoc(   opj_j2k_t *p_j2k,
261                                             OPJ_UINT32 compno,
262                                             OPJ_BYTE * p_header_data,
263                                             OPJ_UINT32 * p_header_size,
264                                             opj_event_mgr_t * p_manager );
265
266 /**
267  * Gets the size taken by writing SQcd or SQcc element, i.e. the quantization values of a band in the QCD or QCC.
268  *
269  * @param       p_tile_no               the tile index.
270  * @param       p_comp_no               the component being outputted.
271  * @param       p_j2k                   the J2K codec.
272  *
273  * @return      the number of bytes taken by the SPCod element.
274  */
275 static OPJ_UINT32 opj_j2k_get_SQcd_SQcc_size (  opj_j2k_t *p_j2k,
276                                                                                     OPJ_UINT32 p_tile_no,
277                                                                                     OPJ_UINT32 p_comp_no );
278
279 /**
280  * Writes a SQcd or SQcc element, i.e. the quantization values of a band in the QCD or QCC.
281  *
282  * @param       p_tile_no               the tile to output.
283  * @param       p_comp_no               the component number to output.
284  * @param       p_data                  the data buffer.
285  * @param       p_header_size   pointer to the size of the data buffer, it is changed by the function.
286  * @param       p_j2k                   J2K codec.
287  * @param       p_manager               the user event manager.
288  *
289 */
290 static OPJ_BOOL opj_j2k_write_SQcd_SQcc(opj_j2k_t *p_j2k,
291                                                                             OPJ_UINT32 p_tile_no,
292                                                                             OPJ_UINT32 p_comp_no,
293                                                                             OPJ_BYTE * p_data,
294                                                                             OPJ_UINT32 * p_header_size,
295                                                                             opj_event_mgr_t * p_manager);
296
297 /**
298  * Updates the Tile Length Marker.
299  */
300 static void opj_j2k_update_tlm ( opj_j2k_t * p_j2k, OPJ_UINT32 p_tile_part_size);
301
302 /**
303  * Reads a SQcd or SQcc element, i.e. the quantization values of a band in the QCD or QCC.
304  *
305  * @param       p_j2k           J2K codec.
306  * @param       compno          the component number to output.
307  * @param       p_header_data   the data buffer.
308  * @param       p_header_size   pointer to the size of the data buffer, it is changed by the function.
309  * @param       p_manager       the user event manager.
310  *
311 */
312 static OPJ_BOOL opj_j2k_read_SQcd_SQcc( opj_j2k_t *p_j2k,
313                                         OPJ_UINT32 compno,
314                                         OPJ_BYTE * p_header_data,
315                                         OPJ_UINT32 * p_header_size,
316                                         opj_event_mgr_t * p_manager );
317
318 /**
319  * Copies the tile component parameters of all the component from the first tile component.
320  *
321  * @param               p_j2k           the J2k codec.
322  */
323 static void opj_j2k_copy_tile_component_parameters( opj_j2k_t *p_j2k );
324
325 /**
326  * Copies the tile quantization parameters of all the component from the first tile component.
327  *
328  * @param               p_j2k           the J2k codec.
329  */
330 static void opj_j2k_copy_tile_quantization_parameters( opj_j2k_t *p_j2k );
331
332 /**
333  * Reads the tiles.
334  */
335 static OPJ_BOOL opj_j2k_decode_tiles (  opj_j2k_t *p_j2k,
336                                         opj_stream_private_t *p_stream,
337                                         opj_event_mgr_t * p_manager);
338
339 static OPJ_BOOL opj_j2k_pre_write_tile ( opj_j2k_t * p_j2k,
340                                                                              OPJ_UINT32 p_tile_index,
341                                                                              opj_stream_private_t *p_stream,
342                                                                              opj_event_mgr_t * p_manager );
343
344 static OPJ_BOOL opj_j2k_update_image_data (opj_tcd_t * p_tcd, OPJ_BYTE * p_data, opj_image_t* p_output_image);
345
346 static void opj_j2k_get_tile_data (opj_tcd_t * p_tcd, OPJ_BYTE * p_data);
347
348 static OPJ_BOOL opj_j2k_post_write_tile (opj_j2k_t * p_j2k,
349                                                                              OPJ_BYTE * p_data,
350                                                                              OPJ_UINT32 p_data_size,
351                                                                              opj_stream_private_t *p_stream,
352                                                                              opj_event_mgr_t * p_manager );
353
354 /**
355  * Sets up the procedures to do on writing header.
356  * Developers wanting to extend the library can add their own writing procedures.
357  */
358 static void opj_j2k_setup_header_writing (opj_j2k_t *p_j2k);
359
360 static OPJ_BOOL opj_j2k_write_first_tile_part(  opj_j2k_t *p_j2k,
361                                                                                             OPJ_BYTE * p_data,
362                                                                                             OPJ_UINT32 * p_data_written,
363                                                                                             OPJ_UINT32 p_total_data_size,
364                                                                                             opj_stream_private_t *p_stream,
365                                                                                             struct opj_event_mgr * p_manager );
366
367 static OPJ_BOOL opj_j2k_write_all_tile_parts(   opj_j2k_t *p_j2k,
368                                                                                             OPJ_BYTE * p_data,
369                                                                                             OPJ_UINT32 * p_data_written,
370                                                                                             OPJ_UINT32 p_total_data_size,
371                                                                                             opj_stream_private_t *p_stream,
372                                                                                             struct opj_event_mgr * p_manager );
373
374 /**
375  * Gets the offset of the header.
376  *
377  * @param       p_stream                the stream to write data to.
378  * @param       p_j2k                   J2K codec.
379  * @param       p_manager               the user event manager.
380 */
381 static OPJ_BOOL opj_j2k_get_end_header( opj_j2k_t *p_j2k,
382                                         opj_stream_private_t *p_stream,
383                                         opj_event_mgr_t * p_manager );
384
385 static OPJ_BOOL opj_j2k_allocate_tile_element_cstr_index(opj_j2k_t *p_j2k);
386
387 /*
388  * -----------------------------------------------------------------------
389  * -----------------------------------------------------------------------
390  * -----------------------------------------------------------------------
391  */
392
393 /**
394  * Writes the SOC marker (Start Of Codestream)
395  *
396  * @param       p_stream                        the stream to write data to.
397  * @param       p_j2k                   J2K codec.
398  * @param       p_manager       the user event manager.
399 */
400 static OPJ_BOOL opj_j2k_write_soc(      opj_j2k_t *p_j2k,
401                                                         opj_stream_private_t *p_stream,
402                                                             opj_event_mgr_t * p_manager );
403
404 /**
405  * Reads a SOC marker (Start of Codestream)
406  * @param       p_j2k           the jpeg2000 file codec.
407  * @param       p_stream        XXX needs data
408  * @param       p_manager       the user event manager.
409 */
410 static OPJ_BOOL opj_j2k_read_soc(   opj_j2k_t *p_j2k,
411                                     opj_stream_private_t *p_stream,
412                                     opj_event_mgr_t * p_manager );
413
414 /**
415  * Writes the SIZ marker (image and tile size)
416  *
417  * @param       p_j2k           J2K codec.
418  * @param       p_stream        the stream to write data to.
419  * @param       p_manager       the user event manager.
420 */
421 static OPJ_BOOL opj_j2k_write_siz(      opj_j2k_t *p_j2k,
422                                                                 opj_stream_private_t *p_stream,
423                                                                 opj_event_mgr_t * p_manager );
424
425 /**
426  * Reads a SIZ marker (image and tile size)
427  * @param       p_j2k           the jpeg2000 file codec.
428  * @param       p_header_data   the data contained in the SIZ box.
429  * @param       p_header_size   the size of the data contained in the SIZ marker.
430  * @param       p_manager       the user event manager.
431 */
432 static OPJ_BOOL opj_j2k_read_siz(opj_j2k_t *p_j2k,
433                                  OPJ_BYTE * p_header_data,
434                                  OPJ_UINT32 p_header_size,
435                                  opj_event_mgr_t * p_manager);
436
437 /**
438  * Writes the COM marker (comment)
439  *
440  * @param       p_stream                        the stream to write data to.
441  * @param       p_j2k                   J2K codec.
442  * @param       p_manager       the user event manager.
443 */
444 static OPJ_BOOL opj_j2k_write_com(      opj_j2k_t *p_j2k,
445                                                                         opj_stream_private_t *p_stream,
446                                                                         opj_event_mgr_t * p_manager );
447
448 /**
449  * Reads a COM marker (comments)
450  * @param       p_j2k           the jpeg2000 file codec.
451  * @param       p_header_data   the data contained in the COM box.
452  * @param       p_header_size   the size of the data contained in the COM marker.
453  * @param       p_manager       the user event manager.
454 */
455 static OPJ_BOOL opj_j2k_read_com (  opj_j2k_t *p_j2k,
456                                     OPJ_BYTE * p_header_data,
457                                     OPJ_UINT32 p_header_size,
458                                     opj_event_mgr_t * p_manager );
459 /**
460  * Writes the COD marker (Coding style default)
461  *
462  * @param       p_stream                        the stream to write data to.
463  * @param       p_j2k                   J2K codec.
464  * @param       p_manager       the user event manager.
465 */
466 static OPJ_BOOL opj_j2k_write_cod(      opj_j2k_t *p_j2k,
467                                                                         opj_stream_private_t *p_stream,
468                                                                         opj_event_mgr_t * p_manager );
469
470 /**
471  * Reads a COD marker (Coding Styke defaults)
472  * @param       p_header_data   the data contained in the COD box.
473  * @param       p_j2k                   the jpeg2000 codec.
474  * @param       p_header_size   the size of the data contained in the COD marker.
475  * @param       p_manager               the user event manager.
476 */
477 static OPJ_BOOL opj_j2k_read_cod (  opj_j2k_t *p_j2k,
478                                     OPJ_BYTE * p_header_data,
479                                     OPJ_UINT32 p_header_size,
480                                     opj_event_mgr_t * p_manager);
481
482 #if 0
483 /**
484  * Writes the COC marker (Coding style component)
485  *
486  * @param       p_j2k       J2K codec.
487  * @param       p_comp_no   the index of the component to output.
488  * @param       p_stream    the stream to write data to.
489  * @param       p_manager   the user event manager.
490 */
491 static OPJ_BOOL opj_j2k_write_coc(  opj_j2k_t *p_j2k,
492                                                                 OPJ_UINT32 p_comp_no,
493                                                                 opj_stream_private_t *p_stream,
494                                                                 opj_event_mgr_t * p_manager );
495 #endif
496
497 #if 0
498 /**
499  * Writes the COC marker (Coding style component)
500  *
501  * @param       p_j2k                   J2K codec.
502  * @param       p_comp_no               the index of the component to output.
503  * @param       p_data          FIXME DOC
504  * @param       p_data_written  FIXME DOC
505  * @param       p_manager               the user event manager.
506 */
507 static void opj_j2k_write_coc_in_memory(opj_j2k_t *p_j2k,
508                                                                             OPJ_UINT32 p_comp_no,
509                                                                             OPJ_BYTE * p_data,
510                                                                             OPJ_UINT32 * p_data_written,
511                                                                             opj_event_mgr_t * p_manager );
512 #endif
513
514 /**
515  * Gets the maximum size taken by a coc.
516  *
517  * @param       p_j2k   the jpeg2000 codec to use.
518  */
519 static OPJ_UINT32 opj_j2k_get_max_coc_size(opj_j2k_t *p_j2k);
520
521 /**
522  * Reads a COC marker (Coding Style Component)
523  * @param       p_header_data   the data contained in the COC box.
524  * @param       p_j2k                   the jpeg2000 codec.
525  * @param       p_header_size   the size of the data contained in the COC marker.
526  * @param       p_manager               the user event manager.
527 */
528 static OPJ_BOOL opj_j2k_read_coc (  opj_j2k_t *p_j2k,
529                                     OPJ_BYTE * p_header_data,
530                                     OPJ_UINT32 p_header_size,
531                                     opj_event_mgr_t * p_manager );
532
533 /**
534  * Writes the QCD marker (quantization default)
535  *
536  * @param       p_j2k                   J2K codec.
537  * @param       p_stream                the stream to write data to.
538  * @param       p_manager               the user event manager.
539 */
540 static OPJ_BOOL opj_j2k_write_qcd(      opj_j2k_t *p_j2k,
541                                                                         opj_stream_private_t *p_stream,
542                                                                         opj_event_mgr_t * p_manager );
543
544 /**
545  * Reads a QCD marker (Quantization defaults)
546  * @param       p_header_data   the data contained in the QCD box.
547  * @param       p_j2k                   the jpeg2000 codec.
548  * @param       p_header_size   the size of the data contained in the QCD marker.
549  * @param       p_manager               the user event manager.
550 */
551 static OPJ_BOOL opj_j2k_read_qcd (  opj_j2k_t *p_j2k,
552                                     OPJ_BYTE * p_header_data,
553                                     OPJ_UINT32 p_header_size,
554                                     opj_event_mgr_t * p_manager );
555 #if 0
556 /**
557  * Writes the QCC marker (quantization component)
558  *
559  * @param       p_comp_no       the index of the component to output.
560  * @param       p_stream                the stream to write data to.
561  * @param       p_j2k                   J2K codec.
562  * @param       p_manager               the user event manager.
563 */
564 static OPJ_BOOL opj_j2k_write_qcc(      opj_j2k_t *p_j2k,
565                                                                         OPJ_UINT32 p_comp_no,
566                                                                         opj_stream_private_t *p_stream,
567                                                                         opj_event_mgr_t * p_manager );
568 #endif
569
570 #if 0
571 /**
572  * Writes the QCC marker (quantization component)
573  *
574  * @param       p_j2k           J2K codec.
575  * @param       p_comp_no       the index of the component to output.
576  * @param       p_data          FIXME DOC
577  * @param       p_data_written  the stream to write data to.
578  * @param       p_manager       the user event manager.
579 */
580 static void opj_j2k_write_qcc_in_memory(opj_j2k_t *p_j2k,
581                                                                             OPJ_UINT32 p_comp_no,
582                                                                             OPJ_BYTE * p_data,
583                                                                             OPJ_UINT32 * p_data_written,
584                                                                             opj_event_mgr_t * p_manager );
585 #endif
586
587 /**
588  * Gets the maximum size taken by a qcc.
589  */
590 static OPJ_UINT32 opj_j2k_get_max_qcc_size (opj_j2k_t *p_j2k);
591
592 /**
593  * Reads a QCC marker (Quantization component)
594  * @param       p_header_data   the data contained in the QCC box.
595  * @param       p_j2k                   the jpeg2000 codec.
596  * @param       p_header_size   the size of the data contained in the QCC marker.
597  * @param       p_manager               the user event manager.
598 */
599 static OPJ_BOOL opj_j2k_read_qcc(   opj_j2k_t *p_j2k,
600                                     OPJ_BYTE * p_header_data,
601                                     OPJ_UINT32 p_header_size,
602                                     opj_event_mgr_t * p_manager);
603 /**
604  * Writes the POC marker (Progression Order Change)
605  *
606  * @param       p_stream                                the stream to write data to.
607  * @param       p_j2k                           J2K codec.
608  * @param       p_manager               the user event manager.
609 */
610 static OPJ_BOOL opj_j2k_write_poc(      opj_j2k_t *p_j2k,
611                                                                         opj_stream_private_t *p_stream,
612                                                                         opj_event_mgr_t * p_manager );
613 /**
614  * Writes the POC marker (Progression Order Change)
615  *
616  * @param       p_j2k          J2K codec.
617  * @param       p_data         FIXME DOC
618  * @param       p_data_written the stream to write data to.
619  * @param       p_manager      the user event manager.
620  */
621 static void opj_j2k_write_poc_in_memory(opj_j2k_t *p_j2k,
622                                                                             OPJ_BYTE * p_data,
623                                                                             OPJ_UINT32 * p_data_written,
624                                                                             opj_event_mgr_t * p_manager );
625 /**
626  * Gets the maximum size taken by the writing of a POC.
627  */
628 static OPJ_UINT32 opj_j2k_get_max_poc_size(opj_j2k_t *p_j2k);
629
630 /**
631  * Reads a POC marker (Progression Order Change)
632  *
633  * @param       p_header_data   the data contained in the POC box.
634  * @param       p_j2k                   the jpeg2000 codec.
635  * @param       p_header_size   the size of the data contained in the POC marker.
636  * @param       p_manager               the user event manager.
637 */
638 static OPJ_BOOL opj_j2k_read_poc (  opj_j2k_t *p_j2k,
639                                     OPJ_BYTE * p_header_data,
640                                     OPJ_UINT32 p_header_size,
641                                     opj_event_mgr_t * p_manager );
642
643 /**
644  * Gets the maximum size taken by the toc headers of all the tile parts of any given tile.
645  */
646 static OPJ_UINT32 opj_j2k_get_max_toc_size (opj_j2k_t *p_j2k);
647
648 /**
649  * Gets the maximum size taken by the headers of the SOT.
650  *
651  * @param       p_j2k   the jpeg2000 codec to use.
652  */
653 static OPJ_UINT32 opj_j2k_get_specific_header_sizes(opj_j2k_t *p_j2k);
654
655 /**
656  * Reads a CRG marker (Component registration)
657  *
658  * @param       p_header_data   the data contained in the TLM box.
659  * @param       p_j2k                   the jpeg2000 codec.
660  * @param       p_header_size   the size of the data contained in the TLM marker.
661  * @param       p_manager               the user event manager.
662 */
663 static OPJ_BOOL opj_j2k_read_crg (  opj_j2k_t *p_j2k,
664                                     OPJ_BYTE * p_header_data,
665                                     OPJ_UINT32 p_header_size,
666                                     opj_event_mgr_t * p_manager );
667 /**
668  * Reads a TLM marker (Tile Length Marker)
669  *
670  * @param       p_header_data   the data contained in the TLM box.
671  * @param       p_j2k                   the jpeg2000 codec.
672  * @param       p_header_size   the size of the data contained in the TLM marker.
673  * @param       p_manager               the user event manager.
674 */
675 static OPJ_BOOL opj_j2k_read_tlm (  opj_j2k_t *p_j2k,
676                                     OPJ_BYTE * p_header_data,
677                                     OPJ_UINT32 p_header_size,
678                                     opj_event_mgr_t * p_manager);
679
680 /**
681  * Writes the updated tlm.
682  *
683  * @param       p_stream                the stream to write data to.
684  * @param       p_j2k                   J2K codec.
685  * @param       p_manager               the user event manager.
686 */
687 static OPJ_BOOL opj_j2k_write_updated_tlm(      opj_j2k_t *p_j2k,
688                                             opj_stream_private_t *p_stream,
689                                             opj_event_mgr_t * p_manager );
690
691 /**
692  * Reads a PLM marker (Packet length, main header marker)
693  *
694  * @param       p_header_data   the data contained in the TLM box.
695  * @param       p_j2k                   the jpeg2000 codec.
696  * @param       p_header_size   the size of the data contained in the TLM marker.
697  * @param       p_manager               the user event manager.
698 */
699 static OPJ_BOOL opj_j2k_read_plm (  opj_j2k_t *p_j2k,
700                                     OPJ_BYTE * p_header_data,
701                                     OPJ_UINT32 p_header_size,
702                                     opj_event_mgr_t * p_manager);
703 /**
704  * Reads a PLT marker (Packet length, tile-part header)
705  *
706  * @param       p_header_data   the data contained in the PLT box.
707  * @param       p_j2k                   the jpeg2000 codec.
708  * @param       p_header_size   the size of the data contained in the PLT marker.
709  * @param       p_manager               the user event manager.
710 */
711 static OPJ_BOOL opj_j2k_read_plt (  opj_j2k_t *p_j2k,
712                                     OPJ_BYTE * p_header_data,
713                                     OPJ_UINT32 p_header_size,
714                                     opj_event_mgr_t * p_manager );
715
716 #if 0
717 /**
718  * Reads a PPM marker (Packed packet headers, main header)
719  *
720  * @param       p_header_data   the data contained in the POC box.
721  * @param       p_j2k                   the jpeg2000 codec.
722  * @param       p_header_size   the size of the data contained in the POC marker.
723  * @param       p_manager               the user event manager.
724 */
725 static OPJ_BOOL j2k_read_ppm_v2 (
726                                                 opj_j2k_t *p_j2k,
727                                                 OPJ_BYTE * p_header_data,
728                                                 OPJ_UINT32 p_header_size,
729                                                 struct opj_event_mgr * p_manager
730                                         );
731 #endif
732
733 static OPJ_BOOL j2k_read_ppm_v3 (
734                                                 opj_j2k_t *p_j2k,
735                                                 OPJ_BYTE * p_header_data,
736                                                 OPJ_UINT32 p_header_size,
737                                                 opj_event_mgr_t * p_manager );
738
739 /**
740  * Reads a PPT marker (Packed packet headers, tile-part header)
741  *
742  * @param       p_header_data   the data contained in the PPT box.
743  * @param       p_j2k                   the jpeg2000 codec.
744  * @param       p_header_size   the size of the data contained in the PPT marker.
745  * @param       p_manager               the user event manager.
746 */
747 static OPJ_BOOL opj_j2k_read_ppt (  opj_j2k_t *p_j2k,
748                                     OPJ_BYTE * p_header_data,
749                                     OPJ_UINT32 p_header_size,
750                                     opj_event_mgr_t * p_manager );
751 /**
752  * Writes the TLM marker (Tile Length Marker)
753  *
754  * @param       p_stream                                the stream to write data to.
755  * @param       p_j2k                           J2K codec.
756  * @param       p_manager               the user event manager.
757 */
758 static OPJ_BOOL opj_j2k_write_tlm(      opj_j2k_t *p_j2k,
759                                                                         opj_stream_private_t *p_stream,
760                                                                         opj_event_mgr_t * p_manager );
761
762 /**
763  * Writes the SOT marker (Start of tile-part)
764  *
765  * @param       p_j2k            J2K codec.
766  * @param       p_data           FIXME DOC
767  * @param       p_data_written   FIXME DOC
768  * @param       p_stream         the stream to write data to.
769  * @param       p_manager        the user event manager.
770 */
771 static OPJ_BOOL opj_j2k_write_sot(      opj_j2k_t *p_j2k,
772                                                                         OPJ_BYTE * p_data,
773                                                                         OPJ_UINT32 * p_data_written,
774                                                                         const opj_stream_private_t *p_stream,
775                                                                         opj_event_mgr_t * p_manager );
776
777 /**
778  * Reads a PPT marker (Packed packet headers, tile-part header)
779  *
780  * @param       p_header_data   the data contained in the PPT box.
781  * @param       p_j2k                   the jpeg2000 codec.
782  * @param       p_header_size   the size of the data contained in the PPT marker.
783  * @param       p_manager               the user event manager.
784 */
785 static OPJ_BOOL opj_j2k_read_sot (  opj_j2k_t *p_j2k,
786                                     OPJ_BYTE * p_header_data,
787                                     OPJ_UINT32 p_header_size,
788                                     opj_event_mgr_t * p_manager );
789 /**
790  * Writes the SOD marker (Start of data)
791  *
792  * @param       p_j2k               J2K codec.
793  * @param       p_tile_coder        FIXME DOC
794  * @param       p_data              FIXME DOC
795  * @param       p_data_written      FIXME DOC
796  * @param       p_total_data_size   FIXME DOC
797  * @param       p_stream            the stream to write data to.
798  * @param       p_manager           the user event manager.
799 */
800 static OPJ_BOOL opj_j2k_write_sod(      opj_j2k_t *p_j2k,
801                                                                         opj_tcd_t * p_tile_coder,
802                                                                         OPJ_BYTE * p_data,
803                                                                         OPJ_UINT32 * p_data_written,
804                                                                         OPJ_UINT32 p_total_data_size,
805                                                                         const opj_stream_private_t *p_stream,
806                                                                         opj_event_mgr_t * p_manager );
807
808 /**
809  * Reads a SOD marker (Start Of Data)
810  *
811  * @param       p_j2k                   the jpeg2000 codec.
812  * @param       p_stream                FIXME DOC
813  * @param       p_manager               the user event manager.
814 */
815 static OPJ_BOOL opj_j2k_read_sod(   opj_j2k_t *p_j2k,
816                                     opj_stream_private_t *p_stream,
817                                     opj_event_mgr_t * p_manager );
818
819 void opj_j2k_update_tlm (opj_j2k_t * p_j2k, OPJ_UINT32 p_tile_part_size )
820 {
821         opj_write_bytes(p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current,p_j2k->m_current_tile_number,1);            /* PSOT */
822         ++p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current;
823
824         opj_write_bytes(p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current,p_tile_part_size,4);                                        /* PSOT */
825         p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current += 4;
826 }
827
828 /**
829  * Writes the RGN marker (Region Of Interest)
830  *
831  * @param       p_tile_no               the tile to output
832  * @param       p_comp_no               the component to output
833  * @param       nb_comps                the number of components
834  * @param       p_stream                the stream to write data to.
835  * @param       p_j2k                   J2K codec.
836  * @param       p_manager               the user event manager.
837 */
838 static OPJ_BOOL opj_j2k_write_rgn(  opj_j2k_t *p_j2k,
839                                     OPJ_UINT32 p_tile_no,
840                                     OPJ_UINT32 p_comp_no,
841                                     OPJ_UINT32 nb_comps,
842                                     opj_stream_private_t *p_stream,
843                                     opj_event_mgr_t * p_manager );
844
845 /**
846  * Reads a RGN marker (Region Of Interest)
847  *
848  * @param       p_header_data   the data contained in the POC box.
849  * @param       p_j2k                   the jpeg2000 codec.
850  * @param       p_header_size   the size of the data contained in the POC marker.
851  * @param       p_manager               the user event manager.
852 */
853 static OPJ_BOOL opj_j2k_read_rgn (opj_j2k_t *p_j2k,
854                                   OPJ_BYTE * p_header_data,
855                                   OPJ_UINT32 p_header_size,
856                                   opj_event_mgr_t * p_manager );
857
858 /**
859  * Writes the EOC marker (End of Codestream)
860  *
861  * @param       p_stream                the stream to write data to.
862  * @param       p_j2k                   J2K codec.
863  * @param       p_manager               the user event manager.
864 */
865 static OPJ_BOOL opj_j2k_write_eoc(      opj_j2k_t *p_j2k,
866                                     opj_stream_private_t *p_stream,
867                                     opj_event_mgr_t * p_manager );
868
869 #if 0
870 /**
871  * Reads a EOC marker (End Of Codestream)
872  *
873  * @param       p_j2k                   the jpeg2000 codec.
874  * @param       p_stream                FIXME DOC
875  * @param       p_manager               the user event manager.
876 */
877 static OPJ_BOOL opj_j2k_read_eoc (      opj_j2k_t *p_j2k,
878                                                                 opj_stream_private_t *p_stream,
879                                                                 opj_event_mgr_t * p_manager );
880 #endif
881
882 /**
883  * Writes the CBD-MCT-MCC-MCO markers (Multi components transform)
884  *
885  * @param       p_stream                        the stream to write data to.
886  * @param       p_j2k                   J2K codec.
887  * @param       p_manager       the user event manager.
888 */
889 static OPJ_BOOL opj_j2k_write_mct_data_group(   opj_j2k_t *p_j2k,
890                                                 opj_stream_private_t *p_stream,
891                                                 opj_event_mgr_t * p_manager );
892
893 /**
894  * Inits the Info
895  *
896  * @param       p_stream                the stream to write data to.
897  * @param       p_j2k                   J2K codec.
898  * @param       p_manager               the user event manager.
899 */
900 static OPJ_BOOL opj_j2k_init_info(      opj_j2k_t *p_j2k,
901                                     opj_stream_private_t *p_stream,
902                                     opj_event_mgr_t * p_manager );
903
904 /**
905 Add main header marker information
906 @param cstr_index    Codestream information structure
907 @param type         marker type
908 @param pos          byte offset of marker segment
909 @param len          length of marker segment
910  */
911 static OPJ_BOOL opj_j2k_add_mhmarker(opj_codestream_index_t *cstr_index, OPJ_UINT32 type, OPJ_OFF_T pos, OPJ_UINT32 len) ;
912 /**
913 Add tile header marker information
914 @param tileno       tile index number
915 @param cstr_index   Codestream information structure
916 @param type         marker type
917 @param pos          byte offset of marker segment
918 @param len          length of marker segment
919  */
920 static OPJ_BOOL opj_j2k_add_tlmarker(OPJ_UINT32 tileno, opj_codestream_index_t *cstr_index, OPJ_UINT32 type, OPJ_OFF_T pos, OPJ_UINT32 len);
921
922 /**
923  * Reads an unknown marker
924  *
925  * @param       p_j2k                   the jpeg2000 codec.
926  * @param       p_stream                the stream object to read from.
927  * @param       output_marker           FIXME DOC
928  * @param       p_manager               the user event manager.
929  *
930  * @return      true                    if the marker could be deduced.
931 */
932 static OPJ_BOOL opj_j2k_read_unk( opj_j2k_t *p_j2k,
933                                   opj_stream_private_t *p_stream,
934                                   OPJ_UINT32 *output_marker,
935                                   opj_event_mgr_t * p_manager );
936
937 /**
938  * Writes the MCT marker (Multiple Component Transform)
939  *
940  * @param       p_j2k           J2K codec.
941  * @param       p_mct_record    FIXME DOC
942  * @param       p_stream        the stream to write data to.
943  * @param       p_manager       the user event manager.
944 */
945 static OPJ_BOOL opj_j2k_write_mct_record(       opj_j2k_t *p_j2k,
946                                                                                     opj_mct_data_t * p_mct_record,
947                                             opj_stream_private_t *p_stream,
948                                             opj_event_mgr_t * p_manager );
949
950 /**
951  * Reads a MCT marker (Multiple Component Transform)
952  *
953  * @param       p_header_data   the data contained in the MCT box.
954  * @param       p_j2k                   the jpeg2000 codec.
955  * @param       p_header_size   the size of the data contained in the MCT marker.
956  * @param       p_manager               the user event manager.
957 */
958 static OPJ_BOOL opj_j2k_read_mct (      opj_j2k_t *p_j2k,
959                                                                     OPJ_BYTE * p_header_data,
960                                                                     OPJ_UINT32 p_header_size,
961                                                                     opj_event_mgr_t * p_manager );
962
963 /**
964  * Writes the MCC marker (Multiple Component Collection)
965  *
966  * @param       p_j2k                   J2K codec.
967  * @param       p_mcc_record            FIXME DOC
968  * @param       p_stream                the stream to write data to.
969  * @param       p_manager               the user event manager.
970 */
971 static OPJ_BOOL opj_j2k_write_mcc_record(   opj_j2k_t *p_j2k,
972                                             opj_simple_mcc_decorrelation_data_t * p_mcc_record,
973                                             opj_stream_private_t *p_stream,
974                                             opj_event_mgr_t * p_manager );
975
976 /**
977  * Reads a MCC marker (Multiple Component Collection)
978  *
979  * @param       p_header_data   the data contained in the MCC box.
980  * @param       p_j2k                   the jpeg2000 codec.
981  * @param       p_header_size   the size of the data contained in the MCC marker.
982  * @param       p_manager               the user event manager.
983 */
984 static OPJ_BOOL opj_j2k_read_mcc (      opj_j2k_t *p_j2k,
985                                                                     OPJ_BYTE * p_header_data,
986                                                                     OPJ_UINT32 p_header_size,
987                                                                     opj_event_mgr_t * p_manager );
988
989 /**
990  * Writes the MCO marker (Multiple component transformation ordering)
991  *
992  * @param       p_stream                                the stream to write data to.
993  * @param       p_j2k                           J2K codec.
994  * @param       p_manager               the user event manager.
995 */
996 static OPJ_BOOL opj_j2k_write_mco(      opj_j2k_t *p_j2k,
997                                     opj_stream_private_t *p_stream,
998                                     opj_event_mgr_t * p_manager );
999
1000 /**
1001  * Reads a MCO marker (Multiple Component Transform Ordering)
1002  *
1003  * @param       p_header_data   the data contained in the MCO box.
1004  * @param       p_j2k                   the jpeg2000 codec.
1005  * @param       p_header_size   the size of the data contained in the MCO marker.
1006  * @param       p_manager               the user event manager.
1007 */
1008 static OPJ_BOOL opj_j2k_read_mco (      opj_j2k_t *p_j2k,
1009                                                                     OPJ_BYTE * p_header_data,
1010                                                                     OPJ_UINT32 p_header_size,
1011                                                                     opj_event_mgr_t * p_manager );
1012
1013 static OPJ_BOOL opj_j2k_add_mct(opj_tcp_t * p_tcp, opj_image_t * p_image, OPJ_UINT32 p_index);
1014
1015 static void  opj_j2k_read_int16_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1016 static void  opj_j2k_read_int32_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1017 static void  opj_j2k_read_float32_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1018 static void  opj_j2k_read_float64_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1019
1020 static void  opj_j2k_read_int16_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1021 static void  opj_j2k_read_int32_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1022 static void  opj_j2k_read_float32_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1023 static void  opj_j2k_read_float64_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1024
1025 static void  opj_j2k_write_float_to_int16 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1026 static void  opj_j2k_write_float_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1027 static void  opj_j2k_write_float_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1028 static void  opj_j2k_write_float_to_float64 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1029
1030 /**
1031  * Ends the encoding, i.e. frees memory.
1032  *
1033  * @param       p_stream                the stream to write data to.
1034  * @param       p_j2k                   J2K codec.
1035  * @param       p_manager               the user event manager.
1036 */
1037 static OPJ_BOOL opj_j2k_end_encoding(   opj_j2k_t *p_j2k,
1038                                                                             opj_stream_private_t *p_stream,
1039                                                                             opj_event_mgr_t * p_manager );
1040
1041 /**
1042  * Writes the CBD marker (Component bit depth definition)
1043  *
1044  * @param       p_stream                                the stream to write data to.
1045  * @param       p_j2k                           J2K codec.
1046  * @param       p_manager               the user event manager.
1047 */
1048 static OPJ_BOOL opj_j2k_write_cbd(      opj_j2k_t *p_j2k,
1049                                                                     opj_stream_private_t *p_stream,
1050                                                                         opj_event_mgr_t * p_manager );
1051
1052 /**
1053  * Reads a CBD marker (Component bit depth definition)
1054  * @param       p_header_data   the data contained in the CBD box.
1055  * @param       p_j2k                   the jpeg2000 codec.
1056  * @param       p_header_size   the size of the data contained in the CBD marker.
1057  * @param       p_manager               the user event manager.
1058 */
1059 static OPJ_BOOL opj_j2k_read_cbd (      opj_j2k_t *p_j2k,
1060                                                                 OPJ_BYTE * p_header_data,
1061                                                                 OPJ_UINT32 p_header_size,
1062                                                                 opj_event_mgr_t * p_manager);
1063
1064 #if 0
1065 /**
1066  * Writes COC marker for each component.
1067  *
1068  * @param       p_stream                the stream to write data to.
1069  * @param       p_j2k                   J2K codec.
1070  * @param       p_manager               the user event manager.
1071 */
1072 static OPJ_BOOL opj_j2k_write_all_coc( opj_j2k_t *p_j2k,
1073                                                                         opj_stream_private_t *p_stream,
1074                                                                         opj_event_mgr_t * p_manager );
1075 #endif
1076
1077 #if 0
1078 /**
1079  * Writes QCC marker for each component.
1080  *
1081  * @param       p_stream                the stream to write data to.
1082  * @param       p_j2k                   J2K codec.
1083  * @param       p_manager               the user event manager.
1084 */
1085 static OPJ_BOOL opj_j2k_write_all_qcc( opj_j2k_t *p_j2k,
1086                                                                         opj_stream_private_t *p_stream,
1087                                                                         opj_event_mgr_t * p_manager );
1088 #endif
1089
1090 /**
1091  * Writes regions of interests.
1092  *
1093  * @param       p_stream                the stream to write data to.
1094  * @param       p_j2k                   J2K codec.
1095  * @param       p_manager               the user event manager.
1096 */
1097 static OPJ_BOOL opj_j2k_write_regions(  opj_j2k_t *p_j2k,
1098                                                                         opj_stream_private_t *p_stream,
1099                                                                         opj_event_mgr_t * p_manager );
1100
1101 /**
1102  * Writes EPC ????
1103  *
1104  * @param       p_stream                the stream to write data to.
1105  * @param       p_j2k                   J2K codec.
1106  * @param       p_manager               the user event manager.
1107 */
1108 static OPJ_BOOL opj_j2k_write_epc(      opj_j2k_t *p_j2k,
1109                                                                     opj_stream_private_t *p_stream,
1110                                                                     opj_event_mgr_t * p_manager );
1111
1112 /**
1113  * Checks the progression order changes values. Tells of the poc given as input are valid.
1114  * A nice message is outputted at errors.
1115  *
1116  * @param       p_pocs                  the progression order changes.
1117  * @param       p_nb_pocs               the number of progression order changes.
1118  * @param       p_nb_resolutions        the number of resolutions.
1119  * @param       numcomps                the number of components
1120  * @param       numlayers               the number of layers.
1121  * @param       p_manager               the user event manager.
1122  *
1123  * @return      true if the pocs are valid.
1124  */
1125 static OPJ_BOOL opj_j2k_check_poc_val(  const opj_poc_t *p_pocs,
1126                                                                             OPJ_UINT32 p_nb_pocs,
1127                                                                             OPJ_UINT32 p_nb_resolutions,
1128                                                                             OPJ_UINT32 numcomps,
1129                                                                             OPJ_UINT32 numlayers,
1130                                                                             opj_event_mgr_t * p_manager);
1131
1132 /**
1133  * Gets the number of tile parts used for the given change of progression (if any) and the given tile.
1134  *
1135  * @param               cp                      the coding parameters.
1136  * @param               pino            the offset of the given poc (i.e. its position in the coding parameter).
1137  * @param               tileno          the given tile.
1138  *
1139  * @return              the number of tile parts.
1140  */
1141 static OPJ_UINT32 opj_j2k_get_num_tp( opj_cp_t *cp, OPJ_UINT32 pino, OPJ_UINT32 tileno);
1142
1143 /**
1144  * Calculates the total number of tile parts needed by the encoder to
1145  * encode such an image. If not enough memory is available, then the function return false.
1146  *
1147  * @param       p_nb_tiles      pointer that will hold the number of tile parts.
1148  * @param       cp                      the coding parameters for the image.
1149  * @param       image           the image to encode.
1150  * @param       p_j2k                   the p_j2k encoder.
1151  * @param       p_manager       the user event manager.
1152  *
1153  * @return true if the function was successful, false else.
1154  */
1155 static OPJ_BOOL opj_j2k_calculate_tp(   opj_j2k_t *p_j2k,
1156                                                                             opj_cp_t *cp,
1157                                                                             OPJ_UINT32 * p_nb_tiles,
1158                                                                             opj_image_t *image,
1159                                                                             opj_event_mgr_t * p_manager);
1160
1161 static void opj_j2k_dump_MH_info(opj_j2k_t* p_j2k, FILE* out_stream);
1162
1163 static void opj_j2k_dump_MH_index(opj_j2k_t* p_j2k, FILE* out_stream);
1164
1165 static opj_codestream_index_t* opj_j2k_create_cstr_index(void);
1166
1167 static OPJ_FLOAT32 opj_j2k_get_tp_stride (opj_tcp_t * p_tcp);
1168
1169 static OPJ_FLOAT32 opj_j2k_get_default_stride (opj_tcp_t * p_tcp);
1170
1171 static int opj_j2k_initialise_4K_poc(opj_poc_t *POC, int numres);
1172
1173 static void opj_j2k_set_cinema_parameters(opj_cparameters_t *parameters, opj_image_t *image, opj_event_mgr_t *p_manager);
1174
1175 static OPJ_BOOL opj_j2k_is_cinema_compliant(opj_image_t *image, OPJ_CINEMA_MODE cinema_mode, opj_event_mgr_t *p_manager);
1176
1177 /*@}*/
1178
1179 /*@}*/
1180
1181 /* ----------------------------------------------------------------------- */
1182 typedef struct j2k_prog_order{
1183         OPJ_PROG_ORDER enum_prog;
1184         char str_prog[5];
1185 }j2k_prog_order_t;
1186
1187 j2k_prog_order_t j2k_prog_order_list[] = {
1188         {OPJ_CPRL, "CPRL"},
1189         {OPJ_LRCP, "LRCP"},
1190         {OPJ_PCRL, "PCRL"},
1191         {OPJ_RLCP, "RLCP"},
1192         {OPJ_RPCL, "RPCL"},
1193         {(OPJ_PROG_ORDER)-1, ""}
1194 };
1195
1196 /**
1197  * FIXME DOC
1198  */
1199 static const OPJ_UINT32 MCT_ELEMENT_SIZE [] =
1200 {
1201         2,
1202         4,
1203         4,
1204         8
1205 };
1206
1207 typedef void (* opj_j2k_mct_function) (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1208
1209 const opj_j2k_mct_function j2k_mct_read_functions_to_float [] =
1210 {
1211         opj_j2k_read_int16_to_float,
1212         opj_j2k_read_int32_to_float,
1213         opj_j2k_read_float32_to_float,
1214         opj_j2k_read_float64_to_float
1215 };
1216
1217 const opj_j2k_mct_function j2k_mct_read_functions_to_int32 [] =
1218 {
1219         opj_j2k_read_int16_to_int32,
1220         opj_j2k_read_int32_to_int32,
1221         opj_j2k_read_float32_to_int32,
1222         opj_j2k_read_float64_to_int32
1223 };
1224
1225 const opj_j2k_mct_function j2k_mct_write_functions_from_float [] =
1226 {
1227         opj_j2k_write_float_to_int16,
1228         opj_j2k_write_float_to_int32,
1229         opj_j2k_write_float_to_float,
1230         opj_j2k_write_float_to_float64
1231 };
1232
1233 typedef struct opj_dec_memory_marker_handler
1234 {
1235         /** marker value */
1236         OPJ_UINT32 id;
1237         /** value of the state when the marker can appear */
1238         OPJ_UINT32 states;
1239         /** action linked to the marker */
1240         OPJ_BOOL (*handler) (   opj_j2k_t *p_j2k,
1241                             OPJ_BYTE * p_header_data,
1242                             OPJ_UINT32 p_header_size,
1243                             opj_event_mgr_t * p_manager );
1244 }
1245 opj_dec_memory_marker_handler_t;
1246
1247 const opj_dec_memory_marker_handler_t j2k_memory_marker_handler_tab [] =
1248 {
1249   {J2K_MS_SOT, J2K_STATE_MH | J2K_STATE_TPHSOT, opj_j2k_read_sot},
1250   {J2K_MS_COD, J2K_STATE_MH | J2K_STATE_TPH, opj_j2k_read_cod},
1251   {J2K_MS_COC, J2K_STATE_MH | J2K_STATE_TPH, opj_j2k_read_coc},
1252   {J2K_MS_RGN, J2K_STATE_MH | J2K_STATE_TPH, opj_j2k_read_rgn},
1253   {J2K_MS_QCD, J2K_STATE_MH | J2K_STATE_TPH, opj_j2k_read_qcd},
1254   {J2K_MS_QCC, J2K_STATE_MH | J2K_STATE_TPH, opj_j2k_read_qcc},
1255   {J2K_MS_POC, J2K_STATE_MH | J2K_STATE_TPH, opj_j2k_read_poc},
1256   {J2K_MS_SIZ, J2K_STATE_MHSIZ, opj_j2k_read_siz},
1257   {J2K_MS_TLM, J2K_STATE_MH, opj_j2k_read_tlm},
1258   {J2K_MS_PLM, J2K_STATE_MH, opj_j2k_read_plm},
1259   {J2K_MS_PLT, J2K_STATE_TPH, opj_j2k_read_plt},
1260   {J2K_MS_PPM, J2K_STATE_MH, j2k_read_ppm_v3},
1261   {J2K_MS_PPT, J2K_STATE_TPH, opj_j2k_read_ppt},
1262   {J2K_MS_SOP, 0, 0},
1263   {J2K_MS_CRG, J2K_STATE_MH, opj_j2k_read_crg},
1264   {J2K_MS_COM, J2K_STATE_MH | J2K_STATE_TPH, opj_j2k_read_com},
1265   {J2K_MS_MCT, J2K_STATE_MH | J2K_STATE_TPH, opj_j2k_read_mct},
1266   {J2K_MS_CBD, J2K_STATE_MH , opj_j2k_read_cbd},
1267   {J2K_MS_MCC, J2K_STATE_MH | J2K_STATE_TPH, opj_j2k_read_mcc},
1268   {J2K_MS_MCO, J2K_STATE_MH | J2K_STATE_TPH, opj_j2k_read_mco},
1269 #ifdef USE_JPWL
1270 #ifdef TODO_MS /* remove these functions which are not commpatible with the v2 API */
1271   {J2K_MS_EPC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_epc},
1272   {J2K_MS_EPB, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_epb},
1273   {J2K_MS_ESD, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_esd},
1274   {J2K_MS_RED, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_red},
1275 #endif
1276 #endif /* USE_JPWL */
1277 #ifdef USE_JPSEC
1278   {J2K_MS_SEC, J2K_DEC_STATE_MH, j2k_read_sec},
1279   {J2K_MS_INSEC, 0, j2k_read_insec}
1280 #endif /* USE_JPSEC */
1281   {J2K_MS_UNK, J2K_STATE_MH | J2K_STATE_TPH, 0}/*opj_j2k_read_unk is directly used*/
1282 };
1283
1284 void  opj_j2k_read_int16_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1285 {
1286         OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
1287         OPJ_FLOAT32 * l_dest_data = (OPJ_FLOAT32 *) p_dest_data;
1288         OPJ_UINT32 i;
1289         OPJ_UINT32 l_temp;
1290
1291         for (i=0;i<p_nb_elem;++i) {
1292                 opj_read_bytes(l_src_data,&l_temp,2);
1293
1294                 l_src_data+=sizeof(OPJ_INT16);
1295
1296                 *(l_dest_data++) = (OPJ_FLOAT32) l_temp;
1297         }
1298 }
1299
1300 void  opj_j2k_read_int32_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1301 {
1302         OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
1303         OPJ_FLOAT32 * l_dest_data = (OPJ_FLOAT32 *) p_dest_data;
1304         OPJ_UINT32 i;
1305         OPJ_UINT32 l_temp;
1306
1307         for (i=0;i<p_nb_elem;++i) {
1308                 opj_read_bytes(l_src_data,&l_temp,4);
1309
1310                 l_src_data+=sizeof(OPJ_INT32);
1311
1312                 *(l_dest_data++) = (OPJ_FLOAT32) l_temp;
1313         }
1314 }
1315
1316 void  opj_j2k_read_float32_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1317 {
1318         OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
1319         OPJ_FLOAT32 * l_dest_data = (OPJ_FLOAT32 *) p_dest_data;
1320         OPJ_UINT32 i;
1321         OPJ_FLOAT32 l_temp;
1322
1323         for (i=0;i<p_nb_elem;++i) {
1324                 opj_read_float(l_src_data,&l_temp);
1325
1326                 l_src_data+=sizeof(OPJ_FLOAT32);
1327
1328                 *(l_dest_data++) = l_temp;
1329         }
1330 }
1331
1332 void  opj_j2k_read_float64_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1333 {
1334         OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
1335         OPJ_FLOAT32 * l_dest_data = (OPJ_FLOAT32 *) p_dest_data;
1336         OPJ_UINT32 i;
1337         OPJ_FLOAT64 l_temp;
1338
1339         for (i=0;i<p_nb_elem;++i) {
1340                 opj_read_double(l_src_data,&l_temp);
1341
1342                 l_src_data+=sizeof(OPJ_FLOAT64);
1343
1344                 *(l_dest_data++) = (OPJ_FLOAT32) l_temp;
1345         }
1346 }
1347
1348 void  opj_j2k_read_int16_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1349 {
1350         OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
1351         OPJ_INT32 * l_dest_data = (OPJ_INT32 *) p_dest_data;
1352         OPJ_UINT32 i;
1353         OPJ_UINT32 l_temp;
1354
1355         for (i=0;i<p_nb_elem;++i) {
1356                 opj_read_bytes(l_src_data,&l_temp,2);
1357
1358                 l_src_data+=sizeof(OPJ_INT16);
1359
1360                 *(l_dest_data++) = (OPJ_INT32) l_temp;
1361         }
1362 }
1363
1364 void  opj_j2k_read_int32_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1365 {
1366         OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
1367         OPJ_INT32 * l_dest_data = (OPJ_INT32 *) p_dest_data;
1368         OPJ_UINT32 i;
1369         OPJ_UINT32 l_temp;
1370
1371         for (i=0;i<p_nb_elem;++i) {
1372                 opj_read_bytes(l_src_data,&l_temp,4);
1373
1374                 l_src_data+=sizeof(OPJ_INT32);
1375
1376                 *(l_dest_data++) = (OPJ_INT32) l_temp;
1377         }
1378 }
1379
1380 void  opj_j2k_read_float32_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1381 {
1382         OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
1383         OPJ_INT32 * l_dest_data = (OPJ_INT32 *) p_dest_data;
1384         OPJ_UINT32 i;
1385         OPJ_FLOAT32 l_temp;
1386
1387         for (i=0;i<p_nb_elem;++i) {
1388                 opj_read_float(l_src_data,&l_temp);
1389
1390                 l_src_data+=sizeof(OPJ_FLOAT32);
1391
1392                 *(l_dest_data++) = (OPJ_INT32) l_temp;
1393         }
1394 }
1395
1396 void  opj_j2k_read_float64_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1397 {
1398         OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
1399         OPJ_INT32 * l_dest_data = (OPJ_INT32 *) p_dest_data;
1400         OPJ_UINT32 i;
1401         OPJ_FLOAT64 l_temp;
1402
1403         for (i=0;i<p_nb_elem;++i) {
1404                 opj_read_double(l_src_data,&l_temp);
1405
1406                 l_src_data+=sizeof(OPJ_FLOAT64);
1407
1408                 *(l_dest_data++) = (OPJ_INT32) l_temp;
1409         }
1410 }
1411
1412 void  opj_j2k_write_float_to_int16 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1413 {
1414         OPJ_BYTE * l_dest_data = (OPJ_BYTE *) p_dest_data;
1415         OPJ_FLOAT32 * l_src_data = (OPJ_FLOAT32 *) p_src_data;
1416         OPJ_UINT32 i;
1417         OPJ_UINT32 l_temp;
1418
1419         for (i=0;i<p_nb_elem;++i) {
1420                 l_temp = (OPJ_UINT32) *(l_src_data++);
1421
1422                 opj_write_bytes(l_dest_data,l_temp,sizeof(OPJ_INT16));
1423
1424                 l_dest_data+=sizeof(OPJ_INT16);
1425         }
1426 }
1427
1428 void opj_j2k_write_float_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1429 {
1430         OPJ_BYTE * l_dest_data = (OPJ_BYTE *) p_dest_data;
1431         OPJ_FLOAT32 * l_src_data = (OPJ_FLOAT32 *) p_src_data;
1432         OPJ_UINT32 i;
1433         OPJ_UINT32 l_temp;
1434
1435         for (i=0;i<p_nb_elem;++i) {
1436                 l_temp = (OPJ_UINT32) *(l_src_data++);
1437
1438                 opj_write_bytes(l_dest_data,l_temp,sizeof(OPJ_INT32));
1439
1440                 l_dest_data+=sizeof(OPJ_INT32);
1441         }
1442 }
1443
1444 void  opj_j2k_write_float_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1445 {
1446         OPJ_BYTE * l_dest_data = (OPJ_BYTE *) p_dest_data;
1447         OPJ_FLOAT32 * l_src_data = (OPJ_FLOAT32 *) p_src_data;
1448         OPJ_UINT32 i;
1449         OPJ_FLOAT32 l_temp;
1450
1451         for (i=0;i<p_nb_elem;++i) {
1452                 l_temp = (OPJ_FLOAT32) *(l_src_data++);
1453
1454                 opj_write_float(l_dest_data,l_temp);
1455
1456                 l_dest_data+=sizeof(OPJ_FLOAT32);
1457         }
1458 }
1459
1460 void  opj_j2k_write_float_to_float64 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1461 {
1462         OPJ_BYTE * l_dest_data = (OPJ_BYTE *) p_dest_data;
1463         OPJ_FLOAT32 * l_src_data = (OPJ_FLOAT32 *) p_src_data;
1464         OPJ_UINT32 i;
1465         OPJ_FLOAT64 l_temp;
1466
1467         for (i=0;i<p_nb_elem;++i) {
1468                 l_temp = (OPJ_FLOAT64) *(l_src_data++);
1469
1470                 opj_write_double(l_dest_data,l_temp);
1471
1472                 l_dest_data+=sizeof(OPJ_FLOAT64);
1473         }
1474 }
1475
1476 char *opj_j2k_convert_progression_order(OPJ_PROG_ORDER prg_order){
1477         j2k_prog_order_t *po;
1478         for(po = j2k_prog_order_list; po->enum_prog != -1; po++ ){
1479                 if(po->enum_prog == prg_order){
1480                         return po->str_prog;
1481                 }
1482         }
1483         return po->str_prog;
1484 }
1485
1486 OPJ_BOOL opj_j2k_check_poc_val( const opj_poc_t *p_pocs,
1487                                                         OPJ_UINT32 p_nb_pocs,
1488                                                         OPJ_UINT32 p_nb_resolutions,
1489                                                         OPJ_UINT32 p_num_comps,
1490                                                         OPJ_UINT32 p_num_layers,
1491                                                         opj_event_mgr_t * p_manager)
1492 {
1493         OPJ_UINT32* packet_array;
1494         OPJ_UINT32 index , resno, compno, layno;
1495         OPJ_UINT32 i;
1496         OPJ_UINT32 step_c = 1;
1497         OPJ_UINT32 step_r = p_num_comps * step_c;
1498         OPJ_UINT32 step_l = p_nb_resolutions * step_r;
1499         OPJ_BOOL loss = OPJ_FALSE;
1500         OPJ_UINT32 layno0 = 0;
1501
1502         packet_array = (OPJ_UINT32*) opj_calloc(step_l * p_num_layers, sizeof(OPJ_UINT32));
1503         if (packet_array == 00) {
1504                 opj_event_msg(p_manager , EVT_ERROR, "Not enough memory for checking the poc values.\n");
1505                 return OPJ_FALSE;
1506         }
1507         memset(packet_array,0,step_l * p_num_layers* sizeof(OPJ_UINT32));
1508
1509         if (p_nb_pocs == 0) {
1510         opj_free(packet_array);
1511                 return OPJ_TRUE;
1512         }
1513
1514         index = step_r * p_pocs->resno0;
1515         /* take each resolution for each poc */
1516         for (resno = p_pocs->resno0 ; resno < p_pocs->resno1 ; ++resno)
1517         {
1518                 OPJ_UINT32 res_index = index + p_pocs->compno0 * step_c;
1519
1520                 /* take each comp of each resolution for each poc */
1521                 for (compno = p_pocs->compno0 ; compno < p_pocs->compno1 ; ++compno) {
1522                         OPJ_UINT32 comp_index = res_index + layno0 * step_l;
1523
1524                         /* and finally take each layer of each res of ... */
1525                         for (layno = layno0; layno < p_pocs->layno1 ; ++layno) {
1526                                 /*index = step_r * resno + step_c * compno + step_l * layno;*/
1527                                 packet_array[comp_index] = 1;
1528                                 comp_index += step_l;
1529                         }
1530
1531                         res_index += step_c;
1532                 }
1533
1534                 index += step_r;
1535         }
1536         ++p_pocs;
1537
1538         /* iterate through all the pocs */
1539         for (i = 1; i < p_nb_pocs ; ++i) {
1540                 OPJ_UINT32 l_last_layno1 = (p_pocs-1)->layno1 ;
1541
1542                 layno0 = (p_pocs->layno1 > l_last_layno1)? l_last_layno1 : 0;
1543                 index = step_r * p_pocs->resno0;
1544
1545                 /* take each resolution for each poc */
1546                 for (resno = p_pocs->resno0 ; resno < p_pocs->resno1 ; ++resno) {
1547                         OPJ_UINT32 res_index = index + p_pocs->compno0 * step_c;
1548
1549                         /* take each comp of each resolution for each poc */
1550                         for (compno = p_pocs->compno0 ; compno < p_pocs->compno1 ; ++compno) {
1551                                 OPJ_UINT32 comp_index = res_index + layno0 * step_l;
1552
1553                                 /* and finally take each layer of each res of ... */
1554                                 for (layno = layno0; layno < p_pocs->layno1 ; ++layno) {
1555                                         /*index = step_r * resno + step_c * compno + step_l * layno;*/
1556                                         packet_array[comp_index] = 1;
1557                                         comp_index += step_l;
1558                                 }
1559
1560                                 res_index += step_c;
1561                         }
1562
1563                         index += step_r;
1564                 }
1565
1566                 ++p_pocs;
1567         }
1568
1569         index = 0;
1570         for (layno = 0; layno < p_num_layers ; ++layno) {
1571                 for (resno = 0; resno < p_nb_resolutions; ++resno) {
1572                         for (compno = 0; compno < p_num_comps; ++compno) {
1573                                 loss |= (packet_array[index]!=1);
1574                                 /*index = step_r * resno + step_c * compno + step_l * layno;*/
1575                                 index += step_c;
1576                         }
1577                 }
1578         }
1579
1580         if (loss) {
1581                 opj_event_msg(p_manager , EVT_ERROR, "Missing packets possible loss of data\n");
1582         }
1583
1584         opj_free(packet_array);
1585
1586         return !loss;
1587 }
1588
1589 /* ----------------------------------------------------------------------- */
1590
1591 OPJ_UINT32 opj_j2k_get_num_tp(opj_cp_t *cp, OPJ_UINT32 pino, OPJ_UINT32 tileno)
1592 {
1593         const OPJ_CHAR *prog = 00;
1594         OPJ_INT32 i;
1595         OPJ_UINT32 tpnum = 1;
1596         opj_tcp_t *tcp = 00;
1597         opj_poc_t * l_current_poc = 00;
1598
1599         /*  preconditions */
1600         assert(tileno < (cp->tw * cp->th));
1601         assert(pino < (cp->tcps[tileno].numpocs + 1));
1602
1603         /* get the given tile coding parameter */
1604         tcp = &cp->tcps[tileno];
1605         assert(tcp != 00);
1606
1607         l_current_poc = &(tcp->pocs[pino]);
1608         assert(l_current_poc != 0);
1609
1610         /* get the progression order as a character string */
1611         prog = opj_j2k_convert_progression_order(tcp->prg);
1612         assert(strlen(prog) > 0);
1613
1614         if (cp->m_specific_param.m_enc.m_tp_on == 1) {
1615                 for (i=0;i<4;++i) {
1616                         switch (prog[i])
1617                         {
1618                                 /* component wise */
1619                                 case 'C':
1620                                         tpnum *= l_current_poc->compE;
1621                                         break;
1622                                 /* resolution wise */
1623                                 case 'R':
1624                                         tpnum *= l_current_poc->resE;
1625                                         break;
1626                                 /* precinct wise */
1627                                 case 'P':
1628                                         tpnum *= l_current_poc->prcE;
1629                                         break;
1630                                 /* layer wise */
1631                                 case 'L':
1632                                         tpnum *= l_current_poc->layE;
1633                                         break;
1634                         }
1635                         /* whould we split here ? */
1636                         if ( cp->m_specific_param.m_enc.m_tp_flag == prog[i] ) {
1637                                 cp->m_specific_param.m_enc.m_tp_pos=i;
1638                                 break;
1639                         }
1640                 }
1641         }
1642         else {
1643                 tpnum=1;
1644         }
1645
1646         return tpnum;
1647 }
1648
1649 OPJ_BOOL opj_j2k_calculate_tp(  opj_j2k_t *p_j2k,
1650                                                         opj_cp_t *cp,
1651                                                         OPJ_UINT32 * p_nb_tiles,
1652                                                         opj_image_t *image,
1653                                                         opj_event_mgr_t * p_manager
1654                                 )
1655 {
1656         OPJ_UINT32 pino,tileno;
1657         OPJ_UINT32 l_nb_tiles;
1658         opj_tcp_t *tcp;
1659
1660         /* preconditions */
1661         assert(p_nb_tiles != 00);
1662         assert(cp != 00);
1663         assert(image != 00);
1664         assert(p_j2k != 00);
1665         assert(p_manager != 00);
1666
1667         l_nb_tiles = cp->tw * cp->th;
1668         * p_nb_tiles = 0;
1669         tcp = cp->tcps;
1670
1671         /* INDEX >> */
1672         /* TODO mergeV2: check this part which use cstr_info */
1673         /*if (p_j2k->cstr_info) {
1674                 opj_tile_info_t * l_info_tile_ptr = p_j2k->cstr_info->tile;
1675
1676                 for (tileno = 0; tileno < l_nb_tiles; ++tileno) {
1677                         OPJ_UINT32 cur_totnum_tp = 0;
1678
1679                         opj_pi_update_encoding_parameters(image,cp,tileno);
1680
1681                         for (pino = 0; pino <= tcp->numpocs; ++pino)
1682                         {
1683                                 OPJ_UINT32 tp_num = opj_j2k_get_num_tp(cp,pino,tileno);
1684
1685                                 *p_nb_tiles = *p_nb_tiles + tp_num;
1686
1687                                 cur_totnum_tp += tp_num;
1688                         }
1689
1690                         tcp->m_nb_tile_parts = cur_totnum_tp;
1691
1692                         l_info_tile_ptr->tp = (opj_tp_info_t *) opj_malloc(cur_totnum_tp * sizeof(opj_tp_info_t));
1693                         if (l_info_tile_ptr->tp == 00) {
1694                                 return OPJ_FALSE;
1695                         }
1696
1697                         memset(l_info_tile_ptr->tp,0,cur_totnum_tp * sizeof(opj_tp_info_t));
1698
1699                         l_info_tile_ptr->num_tps = cur_totnum_tp;
1700
1701                         ++l_info_tile_ptr;
1702                         ++tcp;
1703                 }
1704         }
1705         else */{
1706                 for (tileno = 0; tileno < l_nb_tiles; ++tileno) {
1707                         OPJ_UINT32 cur_totnum_tp = 0;
1708
1709                         opj_pi_update_encoding_parameters(image,cp,tileno);
1710
1711                         for (pino = 0; pino <= tcp->numpocs; ++pino) {
1712                                 OPJ_UINT32 tp_num = opj_j2k_get_num_tp(cp,pino,tileno);
1713
1714                                 *p_nb_tiles = *p_nb_tiles + tp_num;
1715
1716                                 cur_totnum_tp += tp_num;
1717                         }
1718                         tcp->m_nb_tile_parts = cur_totnum_tp;
1719
1720                         ++tcp;
1721                 }
1722         }
1723
1724         return OPJ_TRUE;
1725 }
1726
1727 OPJ_BOOL opj_j2k_write_soc(     opj_j2k_t *p_j2k,
1728                                                 opj_stream_private_t *p_stream,
1729                                                     opj_event_mgr_t * p_manager )
1730 {
1731         /* 2 bytes will be written */
1732         OPJ_BYTE * l_start_stream = 00;
1733
1734         /* preconditions */
1735         assert(p_stream != 00);
1736         assert(p_j2k != 00);
1737         assert(p_manager != 00);
1738
1739         l_start_stream = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
1740
1741         /* write SOC identifier */
1742         opj_write_bytes(l_start_stream,J2K_MS_SOC,2);
1743
1744         if (opj_stream_write_data(p_stream,l_start_stream,2,p_manager) != 2) {
1745                 return OPJ_FALSE;
1746         }
1747
1748 /* UniPG>> */
1749 #ifdef USE_JPWL
1750         /* update markers struct */
1751 /*
1752         OPJ_BOOL res = j2k_add_marker(p_j2k->cstr_info, J2K_MS_SOC, p_stream_tell(p_stream) - 2, 2);
1753 */
1754   assert( 0 && "TODO" );
1755 #endif /* USE_JPWL */
1756 /* <<UniPG */
1757
1758         return OPJ_TRUE;
1759 }
1760
1761 /**
1762  * Reads a SOC marker (Start of Codestream)
1763  * @param       p_j2k           the jpeg2000 file codec.
1764  * @param       p_stream        FIXME DOC
1765  * @param       p_manager       the user event manager.
1766 */
1767 static OPJ_BOOL opj_j2k_read_soc(   opj_j2k_t *p_j2k,
1768                                     opj_stream_private_t *p_stream,
1769                                     opj_event_mgr_t * p_manager
1770                                     )
1771 {
1772         OPJ_BYTE l_data [2];
1773         OPJ_UINT32 l_marker;
1774
1775         /* preconditions */
1776         assert(p_j2k != 00);
1777         assert(p_manager != 00);
1778         assert(p_stream != 00);
1779
1780         if (opj_stream_read_data(p_stream,l_data,2,p_manager) != 2) {
1781                 return OPJ_FALSE;
1782         }
1783
1784         opj_read_bytes(l_data,&l_marker,2);
1785         if (l_marker != J2K_MS_SOC) {
1786                 return OPJ_FALSE;
1787         }
1788
1789         /* Next marker should be a SIZ marker in the main header */
1790         p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_MHSIZ;
1791
1792         /* FIXME move it in a index structure included in p_j2k*/
1793         p_j2k->cstr_index->main_head_start = opj_stream_tell(p_stream) - 2;
1794
1795         opj_event_msg(p_manager, EVT_INFO, "Start to read j2k main header (%d).\n", p_j2k->cstr_index->main_head_start);
1796
1797         /* Add the marker to the codestream index*/
1798         if (OPJ_FALSE == opj_j2k_add_mhmarker(p_j2k->cstr_index, J2K_MS_SOC, p_j2k->cstr_index->main_head_start, 2)) {
1799                 opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to add mh marker\n");
1800                 return OPJ_FALSE;
1801         }
1802         return OPJ_TRUE;
1803 }
1804
1805 OPJ_BOOL opj_j2k_write_siz(     opj_j2k_t *p_j2k,
1806                                                         opj_stream_private_t *p_stream,
1807                                                         opj_event_mgr_t * p_manager )
1808 {
1809         OPJ_UINT32 i;
1810         OPJ_UINT32 l_size_len;
1811         OPJ_BYTE * l_current_ptr;
1812         opj_image_t * l_image = 00;
1813         opj_cp_t *cp = 00;
1814         opj_image_comp_t * l_img_comp = 00;
1815
1816         /* preconditions */
1817         assert(p_stream != 00);
1818         assert(p_j2k != 00);
1819         assert(p_manager != 00);
1820
1821         l_image = p_j2k->m_private_image;
1822         cp = &(p_j2k->m_cp);
1823         l_size_len = 40 + 3 * l_image->numcomps;
1824         l_img_comp = l_image->comps;
1825
1826         if (l_size_len > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
1827
1828                 OPJ_BYTE *new_header_tile_data = (OPJ_BYTE *) opj_realloc(p_j2k->m_specific_param.m_encoder.m_header_tile_data, l_size_len);
1829                 if (! new_header_tile_data) {
1830                         opj_free(p_j2k->m_specific_param.m_encoder.m_header_tile_data);
1831                         p_j2k->m_specific_param.m_encoder.m_header_tile_data = NULL;
1832                         p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = 0;
1833                         opj_event_msg(p_manager, EVT_ERROR, "Not enough memory for the SIZ marker\n");
1834                         return OPJ_FALSE;
1835                 }
1836                 p_j2k->m_specific_param.m_encoder.m_header_tile_data = new_header_tile_data;
1837                 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_size_len;
1838         }
1839
1840         l_current_ptr = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
1841
1842         /* write SOC identifier */
1843         opj_write_bytes(l_current_ptr,J2K_MS_SIZ,2);    /* SIZ */
1844         l_current_ptr+=2;
1845
1846         opj_write_bytes(l_current_ptr,l_size_len-2,2); /* L_SIZ */
1847         l_current_ptr+=2;
1848
1849         opj_write_bytes(l_current_ptr, cp->rsiz, 2);    /* Rsiz (capabilities) */
1850         l_current_ptr+=2;
1851
1852         opj_write_bytes(l_current_ptr, l_image->x1, 4); /* Xsiz */
1853         l_current_ptr+=4;
1854
1855         opj_write_bytes(l_current_ptr, l_image->y1, 4); /* Ysiz */
1856         l_current_ptr+=4;
1857
1858         opj_write_bytes(l_current_ptr, l_image->x0, 4); /* X0siz */
1859         l_current_ptr+=4;
1860
1861         opj_write_bytes(l_current_ptr, l_image->y0, 4); /* Y0siz */
1862         l_current_ptr+=4;
1863
1864         opj_write_bytes(l_current_ptr, cp->tdx, 4);             /* XTsiz */
1865         l_current_ptr+=4;
1866
1867         opj_write_bytes(l_current_ptr, cp->tdy, 4);             /* YTsiz */
1868         l_current_ptr+=4;
1869
1870         opj_write_bytes(l_current_ptr, cp->tx0, 4);             /* XT0siz */
1871         l_current_ptr+=4;
1872
1873         opj_write_bytes(l_current_ptr, cp->ty0, 4);             /* YT0siz */
1874         l_current_ptr+=4;
1875
1876         opj_write_bytes(l_current_ptr, l_image->numcomps, 2);   /* Csiz */
1877         l_current_ptr+=2;
1878
1879         for (i = 0; i < l_image->numcomps; ++i) {
1880                 /* TODO here with MCT ? */
1881                 opj_write_bytes(l_current_ptr, l_img_comp->prec - 1 + (l_img_comp->sgnd << 7), 1);      /* Ssiz_i */
1882                 ++l_current_ptr;
1883
1884                 opj_write_bytes(l_current_ptr, l_img_comp->dx, 1);      /* XRsiz_i */
1885                 ++l_current_ptr;
1886
1887                 opj_write_bytes(l_current_ptr, l_img_comp->dy, 1);      /* YRsiz_i */
1888                 ++l_current_ptr;
1889
1890                 ++l_img_comp;
1891         }
1892
1893         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) {
1894                 return OPJ_FALSE;
1895         }
1896
1897         return OPJ_TRUE;
1898 }
1899
1900 /**
1901  * Reads a SIZ marker (image and tile size)
1902  * @param       p_j2k           the jpeg2000 file codec.
1903  * @param       p_header_data   the data contained in the SIZ box.
1904  * @param       p_header_size   the size of the data contained in the SIZ marker.
1905  * @param       p_manager       the user event manager.
1906 */
1907 static OPJ_BOOL opj_j2k_read_siz(opj_j2k_t *p_j2k,
1908                                  OPJ_BYTE * p_header_data,
1909                                  OPJ_UINT32 p_header_size,
1910                                  opj_event_mgr_t * p_manager
1911                                  )
1912 {
1913         OPJ_UINT32 i;
1914         OPJ_UINT32 l_nb_comp;
1915         OPJ_UINT32 l_nb_comp_remain;
1916         OPJ_UINT32 l_remaining_size;
1917         OPJ_UINT32 l_nb_tiles;
1918         OPJ_UINT32 l_tmp;
1919         opj_image_t *l_image = 00;
1920         opj_cp_t *l_cp = 00;
1921         opj_image_comp_t * l_img_comp = 00;
1922         opj_tcp_t * l_current_tile_param = 00;
1923
1924         /* preconditions */
1925         assert(p_j2k != 00);
1926         assert(p_manager != 00);
1927         assert(p_header_data != 00);
1928
1929         l_image = p_j2k->m_private_image;
1930         l_cp = &(p_j2k->m_cp);
1931
1932         /* minimum size == 39 - 3 (= minimum component parameter) */
1933         if (p_header_size < 36) {
1934                 opj_event_msg(p_manager, EVT_ERROR, "Error with SIZ marker size\n");
1935                 return OPJ_FALSE;
1936         }
1937
1938         l_remaining_size = p_header_size - 36;
1939         l_nb_comp = l_remaining_size / 3;
1940         l_nb_comp_remain = l_remaining_size % 3;
1941         if (l_nb_comp_remain != 0){
1942                 opj_event_msg(p_manager, EVT_ERROR, "Error with SIZ marker size\n");
1943                 return OPJ_FALSE;
1944         }
1945
1946         opj_read_bytes(p_header_data,&l_tmp ,2);                                                /* Rsiz (capabilities) */
1947         p_header_data+=2;
1948         l_cp->rsiz = (OPJ_RSIZ_CAPABILITIES) l_tmp;
1949         opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_image->x1, 4);   /* Xsiz */
1950         p_header_data+=4;
1951         opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_image->y1, 4);   /* Ysiz */
1952         p_header_data+=4;
1953         opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_image->x0, 4);   /* X0siz */
1954         p_header_data+=4;
1955         opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_image->y0, 4);   /* Y0siz */
1956         p_header_data+=4;
1957         opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_cp->tdx, 4);             /* XTsiz */
1958         p_header_data+=4;
1959         opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_cp->tdy, 4);             /* YTsiz */
1960         p_header_data+=4;
1961         opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_cp->tx0, 4);             /* XT0siz */
1962         p_header_data+=4;
1963         opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_cp->ty0, 4);             /* YT0siz */
1964         p_header_data+=4;
1965         opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_tmp, 2);                 /* Csiz */
1966         p_header_data+=2;
1967         if (l_tmp < 16385)
1968                 l_image->numcomps = (OPJ_UINT16) l_tmp;
1969         else {
1970                 opj_event_msg(p_manager, EVT_ERROR, "Error with SIZ marker: number of component is illegal -> %d\n", l_tmp);
1971                 return OPJ_FALSE;
1972         }
1973
1974         if (l_image->numcomps != l_nb_comp) {
1975                 opj_event_msg(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);
1976                 return OPJ_FALSE;
1977         }
1978
1979         /* testcase 4035.pdf.SIGSEGV.d8b.3375 */
1980         if (l_image->x0 > l_image->x1 || l_image->y0 > l_image->y1) {
1981                 opj_event_msg(p_manager, EVT_ERROR, "Error with SIZ marker: negative image size (%d x %d)\n", l_image->x1 - l_image->x0, l_image->y1 - l_image->y0);
1982                 return OPJ_FALSE;
1983         }
1984         /* testcase 2539.pdf.SIGFPE.706.1712 (also 3622.pdf.SIGFPE.706.2916 and 4008.pdf.SIGFPE.706.3345 and maybe more) */
1985         if (!(l_cp->tdx * l_cp->tdy)) {
1986                 opj_event_msg(p_manager, EVT_ERROR, "Error with SIZ marker: invalid tile size (tdx: %d, tdy: %d)\n", l_cp->tdx, l_cp->tdy);
1987                 return OPJ_FALSE;
1988         }
1989
1990         /* testcase 1610.pdf.SIGSEGV.59c.681 */
1991         if (((OPJ_UINT64)l_image->x1) * ((OPJ_UINT64)l_image->y1) != (l_image->x1 * l_image->y1)) {
1992                 opj_event_msg(p_manager, EVT_ERROR, "Prevent buffer overflow (x1: %d, y1: %d)\n", l_image->x1, l_image->y1);
1993                 return OPJ_FALSE;
1994         }
1995
1996 #ifdef USE_JPWL
1997         if (l_cp->correct) {
1998                 /* if JPWL is on, we check whether TX errors have damaged
1999                   too much the SIZ parameters */
2000                 if (!(l_image->x1 * l_image->y1)) {
2001                         opj_event_msg(p_manager, EVT_ERROR,
2002                                 "JPWL: bad image size (%d x %d)\n",
2003                                 l_image->x1, l_image->y1);
2004                         if (!JPWL_ASSUME || JPWL_ASSUME) {
2005                                 opj_event_msg(p_manager, EVT_ERROR, "JPWL: giving up\n");
2006                                 return OPJ_FALSE;
2007                         }
2008                 }
2009
2010         /* FIXME check previously in the function so why keep this piece of code ? Need by the norm ?
2011                 if (l_image->numcomps != ((len - 38) / 3)) {
2012                         opj_event_msg(p_manager, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
2013                                 "JPWL: Csiz is %d => space in SIZ only for %d comps.!!!\n",
2014                                 l_image->numcomps, ((len - 38) / 3));
2015                         if (!JPWL_ASSUME) {
2016                                 opj_event_msg(p_manager, EVT_ERROR, "JPWL: giving up\n");
2017                                 return OPJ_FALSE;
2018                         }
2019         */              /* we try to correct */
2020         /*              opj_event_msg(p_manager, EVT_WARNING, "- trying to adjust this\n");
2021                         if (l_image->numcomps < ((len - 38) / 3)) {
2022                                 len = 38 + 3 * l_image->numcomps;
2023                                 opj_event_msg(p_manager, EVT_WARNING, "- setting Lsiz to %d => HYPOTHESIS!!!\n",
2024                                         len);
2025                         } else {
2026                                 l_image->numcomps = ((len - 38) / 3);
2027                                 opj_event_msg(p_manager, EVT_WARNING, "- setting Csiz to %d => HYPOTHESIS!!!\n",
2028                                         l_image->numcomps);
2029                         }
2030                 }
2031         */
2032
2033                 /* update components number in the jpwl_exp_comps filed */
2034                 l_cp->exp_comps = l_image->numcomps;
2035         }
2036 #endif /* USE_JPWL */
2037
2038         /* Allocate the resulting image components */
2039         l_image->comps = (opj_image_comp_t*) opj_calloc(l_image->numcomps, sizeof(opj_image_comp_t));
2040         if (l_image->comps == 00){
2041                 l_image->numcomps = 0;
2042                 opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to take in charge SIZ marker\n");
2043                 return OPJ_FALSE;
2044         }
2045
2046         memset(l_image->comps,0,l_image->numcomps * sizeof(opj_image_comp_t));
2047         l_img_comp = l_image->comps;
2048
2049         /* Read the component information */
2050         for (i = 0; i < l_image->numcomps; ++i){
2051                 OPJ_UINT32 tmp;
2052                 opj_read_bytes(p_header_data,&tmp,1);   /* Ssiz_i */
2053                 ++p_header_data;
2054                 l_img_comp->prec = (tmp & 0x7f) + 1;
2055                 l_img_comp->sgnd = tmp >> 7;
2056                 opj_read_bytes(p_header_data,&tmp,1);   /* XRsiz_i */
2057                 ++p_header_data;
2058                 l_img_comp->dx = (OPJ_UINT32)tmp; /* should be between 1 and 255 */
2059                 opj_read_bytes(p_header_data,&tmp,1);   /* YRsiz_i */
2060                 ++p_header_data;
2061                 l_img_comp->dy = (OPJ_UINT32)tmp; /* should be between 1 and 255 */
2062                 if( l_img_comp->dx < 1 || l_img_comp->dx > 255 ||
2063                     l_img_comp->dy < 1 || l_img_comp->dy > 255 ) {
2064                     opj_event_msg(p_manager, EVT_ERROR,
2065                                   "Invalid values for comp = %d : dx=%u dy=%u\n (should be between 1 and 255 according the JPEG2000 norm)",
2066                                   i, l_img_comp->dx, l_img_comp->dy);
2067                     return OPJ_FALSE;
2068                 }
2069
2070 #ifdef USE_JPWL
2071                 if (l_cp->correct) {
2072                 /* if JPWL is on, we check whether TX errors have damaged
2073                         too much the SIZ parameters, again */
2074                         if (!(l_image->comps[i].dx * l_image->comps[i].dy)) {
2075                                 opj_event_msg(p_manager, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
2076                                         "JPWL: bad XRsiz_%d/YRsiz_%d (%d x %d)\n",
2077                                         i, i, l_image->comps[i].dx, l_image->comps[i].dy);
2078                                 if (!JPWL_ASSUME) {
2079                                         opj_event_msg(p_manager, EVT_ERROR, "JPWL: giving up\n");
2080                                         return OPJ_FALSE;
2081                                 }
2082                                 /* we try to correct */
2083                                 opj_event_msg(p_manager, EVT_WARNING, "- trying to adjust them\n");
2084                                 if (!l_image->comps[i].dx) {
2085                                         l_image->comps[i].dx = 1;
2086                                         opj_event_msg(p_manager, EVT_WARNING, "- setting XRsiz_%d to %d => HYPOTHESIS!!!\n",
2087                                                 i, l_image->comps[i].dx);
2088                                 }
2089                                 if (!l_image->comps[i].dy) {
2090                                         l_image->comps[i].dy = 1;
2091                                         opj_event_msg(p_manager, EVT_WARNING, "- setting YRsiz_%d to %d => HYPOTHESIS!!!\n",
2092                                                 i, l_image->comps[i].dy);
2093                                 }
2094                         }
2095                 }
2096 #endif /* USE_JPWL */
2097                 l_img_comp->resno_decoded = 0;                                                          /* number of resolution decoded */
2098                 l_img_comp->factor = l_cp->m_specific_param.m_dec.m_reduce; /* reducing factor per component */
2099                 ++l_img_comp;
2100         }
2101
2102         /* Compute the number of tiles */
2103         l_cp->tw = (OPJ_UINT32)opj_int_ceildiv((OPJ_INT32)(l_image->x1 - l_cp->tx0), (OPJ_INT32)l_cp->tdx);
2104         l_cp->th = (OPJ_UINT32)opj_int_ceildiv((OPJ_INT32)(l_image->y1 - l_cp->ty0), (OPJ_INT32)l_cp->tdy);
2105
2106         /* Check that the number of tiles is valid */
2107         if (l_cp->tw == 0 || l_cp->th == 0 || l_cp->tw > 65535 / l_cp->th) {
2108             opj_event_msg(  p_manager, EVT_ERROR, 
2109                             "Invalid number of tiles : %u x %u (maximum fixed by jpeg2000 norm is 65535 tiles)\n",
2110                             l_cp->tw, l_cp->th);
2111             return OPJ_FALSE;
2112         }
2113         l_nb_tiles = l_cp->tw * l_cp->th;
2114
2115         /* Define the tiles which will be decoded */
2116         if (p_j2k->m_specific_param.m_decoder.m_discard_tiles) {
2117                 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;
2118                 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;
2119                 p_j2k->m_specific_param.m_decoder.m_end_tile_x = (OPJ_UINT32)opj_int_ceildiv((OPJ_INT32)(p_j2k->m_specific_param.m_decoder.m_end_tile_x - l_cp->tx0), (OPJ_INT32)l_cp->tdx);
2120                 p_j2k->m_specific_param.m_decoder.m_end_tile_y = (OPJ_UINT32)opj_int_ceildiv((OPJ_INT32)(p_j2k->m_specific_param.m_decoder.m_end_tile_y - l_cp->ty0), (OPJ_INT32)l_cp->tdy);
2121         }
2122         else {
2123                 p_j2k->m_specific_param.m_decoder.m_start_tile_x = 0;
2124                 p_j2k->m_specific_param.m_decoder.m_start_tile_y = 0;
2125                 p_j2k->m_specific_param.m_decoder.m_end_tile_x = l_cp->tw;
2126                 p_j2k->m_specific_param.m_decoder.m_end_tile_y = l_cp->th;
2127         }
2128
2129 #ifdef USE_JPWL
2130         if (l_cp->correct) {
2131                 /* if JPWL is on, we check whether TX errors have damaged
2132                   too much the SIZ parameters */
2133                 if ((l_cp->tw < 1) || (l_cp->th < 1) || (l_cp->tw > l_cp->max_tiles) || (l_cp->th > l_cp->max_tiles)) {
2134                         opj_event_msg(p_manager, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
2135                                 "JPWL: bad number of tiles (%d x %d)\n",
2136                                 l_cp->tw, l_cp->th);
2137                         if (!JPWL_ASSUME) {
2138                                 opj_event_msg(p_manager, EVT_ERROR, "JPWL: giving up\n");
2139                                 return OPJ_FALSE;
2140                         }
2141                         /* we try to correct */
2142                         opj_event_msg(p_manager, EVT_WARNING, "- trying to adjust them\n");
2143                         if (l_cp->tw < 1) {
2144                                 l_cp->tw= 1;
2145                                 opj_event_msg(p_manager, EVT_WARNING, "- setting %d tiles in x => HYPOTHESIS!!!\n",
2146                                                 l_cp->tw);
2147                         }
2148                         if (l_cp->tw > l_cp->max_tiles) {
2149                                 l_cp->tw= 1;
2150                                 opj_event_msg(p_manager, EVT_WARNING, "- too large x, increase expectance of %d\n"
2151                                         "- setting %d tiles in x => HYPOTHESIS!!!\n",
2152                                         l_cp->max_tiles, l_cp->tw);
2153                         }
2154                         if (l_cp->th < 1) {
2155                                 l_cp->th= 1;
2156                                 opj_event_msg(p_manager, EVT_WARNING, "- setting %d tiles in y => HYPOTHESIS!!!\n",
2157                                                 l_cp->th);
2158                         }
2159                         if (l_cp->th > l_cp->max_tiles) {
2160                                 l_cp->th= 1;
2161                                 opj_event_msg(p_manager, EVT_WARNING, "- too large y, increase expectance of %d to continue\n",
2162                                         "- setting %d tiles in y => HYPOTHESIS!!!\n",
2163                                         l_cp->max_tiles, l_cp->th);
2164                         }
2165                 }
2166         }
2167 #endif /* USE_JPWL */
2168
2169         /* memory allocations */
2170         l_cp->tcps = (opj_tcp_t*) opj_calloc(l_nb_tiles, sizeof(opj_tcp_t));
2171         if (l_cp->tcps == 00) {
2172                 opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to take in charge SIZ marker\n");
2173                 return OPJ_FALSE;
2174         }
2175         memset(l_cp->tcps,0,l_nb_tiles*sizeof(opj_tcp_t));
2176
2177 #ifdef USE_JPWL
2178         if (l_cp->correct) {
2179                 if (!l_cp->tcps) {
2180                         opj_event_msg(p_manager, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
2181                                 "JPWL: could not alloc tcps field of cp\n");
2182                         if (!JPWL_ASSUME || JPWL_ASSUME) {
2183                                 opj_event_msg(p_manager, EVT_ERROR, "JPWL: giving up\n");
2184                                 return OPJ_FALSE;
2185                         }
2186                 }
2187         }
2188 #endif /* USE_JPWL */
2189
2190         p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps =
2191                         (opj_tccp_t*) opj_calloc(l_image->numcomps, sizeof(opj_tccp_t));
2192         if(p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps  == 00) {
2193                 opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to take in charge SIZ marker\n");
2194                 return OPJ_FALSE;
2195         }
2196         memset(p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps ,0,l_image->numcomps*sizeof(opj_tccp_t));
2197
2198         p_j2k->m_specific_param.m_decoder.m_default_tcp->m_mct_records =
2199                         (opj_mct_data_t*)opj_malloc(OPJ_J2K_MCT_DEFAULT_NB_RECORDS * sizeof(opj_mct_data_t));
2200
2201         if (! p_j2k->m_specific_param.m_decoder.m_default_tcp->m_mct_records) {
2202                 opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to take in charge SIZ marker\n");
2203                 return OPJ_FALSE;
2204         }
2205         memset(p_j2k->m_specific_param.m_decoder.m_default_tcp->m_mct_records,0,OPJ_J2K_MCT_DEFAULT_NB_RECORDS * sizeof(opj_mct_data_t));
2206         p_j2k->m_specific_param.m_decoder.m_default_tcp->m_nb_max_mct_records = OPJ_J2K_MCT_DEFAULT_NB_RECORDS;
2207
2208         p_j2k->m_specific_param.m_decoder.m_default_tcp->m_mcc_records =
2209                         (opj_simple_mcc_decorrelation_data_t*)
2210                         opj_malloc(OPJ_J2K_MCC_DEFAULT_NB_RECORDS * sizeof(opj_simple_mcc_decorrelation_data_t));
2211
2212         if (! p_j2k->m_specific_param.m_decoder.m_default_tcp->m_mcc_records) {
2213                 opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to take in charge SIZ marker\n");
2214                 return OPJ_FALSE;
2215         }
2216         memset(p_j2k->m_specific_param.m_decoder.m_default_tcp->m_mcc_records,0,OPJ_J2K_MCC_DEFAULT_NB_RECORDS * sizeof(opj_simple_mcc_decorrelation_data_t));
2217         p_j2k->m_specific_param.m_decoder.m_default_tcp->m_nb_max_mcc_records = OPJ_J2K_MCC_DEFAULT_NB_RECORDS;
2218
2219         /* set up default dc level shift */
2220         for (i=0;i<l_image->numcomps;++i) {
2221                 if (! l_image->comps[i].sgnd) {
2222                         p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps[i].m_dc_level_shift = 1 << (l_image->comps[i].prec - 1);
2223                 }
2224         }
2225
2226         l_current_tile_param = l_cp->tcps;
2227         for     (i = 0; i < l_nb_tiles; ++i) {
2228                 l_current_tile_param->tccps = (opj_tccp_t*) opj_malloc(l_image->numcomps * sizeof(opj_tccp_t));
2229                 if (l_current_tile_param->tccps == 00) {
2230                         opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to take in charge SIZ marker\n");
2231                         return OPJ_FALSE;
2232                 }
2233                 memset(l_current_tile_param->tccps,0,l_image->numcomps * sizeof(opj_tccp_t));
2234
2235                 ++l_current_tile_param;
2236         }
2237
2238         p_j2k->m_specific_param.m_decoder.m_state =  J2K_STATE_MH; /* FIXME J2K_DEC_STATE_MH; */
2239         opj_image_comp_header_update(l_image,l_cp);
2240
2241         return OPJ_TRUE;
2242 }
2243
2244 OPJ_BOOL opj_j2k_write_com(     opj_j2k_t *p_j2k,
2245                                                         opj_stream_private_t *p_stream,
2246                                                         opj_event_mgr_t * p_manager
2247                             )
2248 {
2249         OPJ_UINT32 l_comment_size;
2250         OPJ_UINT32 l_total_com_size;
2251         const OPJ_CHAR *l_comment;
2252         OPJ_BYTE * l_current_ptr = 00;
2253
2254         /* preconditions */
2255         assert(p_j2k != 00);
2256         assert(p_stream != 00);
2257         assert(p_manager != 00);
2258
2259         l_comment = p_j2k->m_cp.comment;
2260         l_comment_size = (OPJ_UINT32)strlen(l_comment);
2261         l_total_com_size = l_comment_size + 6;
2262
2263         if (l_total_com_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
2264                 OPJ_BYTE *new_header_tile_data = (OPJ_BYTE *) opj_realloc(p_j2k->m_specific_param.m_encoder.m_header_tile_data, l_total_com_size);
2265                 if (! new_header_tile_data) {
2266                         opj_free(p_j2k->m_specific_param.m_encoder.m_header_tile_data);
2267                         p_j2k->m_specific_param.m_encoder.m_header_tile_data = NULL;
2268                         p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = 0;
2269                         opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to write the COM marker\n");
2270                         return OPJ_FALSE;
2271                 }
2272                 p_j2k->m_specific_param.m_encoder.m_header_tile_data = new_header_tile_data;
2273                 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_total_com_size;
2274         }
2275
2276         l_current_ptr = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
2277
2278         opj_write_bytes(l_current_ptr,J2K_MS_COM , 2);  /* COM */
2279         l_current_ptr+=2;
2280
2281         opj_write_bytes(l_current_ptr,l_total_com_size - 2 , 2);        /* L_COM */
2282         l_current_ptr+=2;
2283
2284         opj_write_bytes(l_current_ptr,1 , 2);   /* General use (IS 8859-15:1999 (Latin) values) */
2285         l_current_ptr+=2;
2286
2287         memcpy( l_current_ptr,l_comment,l_comment_size);
2288
2289         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) {
2290                 return OPJ_FALSE;
2291         }
2292
2293         return OPJ_TRUE;
2294 }
2295
2296 /**
2297  * Reads a COM marker (comments)
2298  * @param       p_j2k           the jpeg2000 file codec.
2299  * @param       p_header_data   the data contained in the COM box.
2300  * @param       p_header_size   the size of the data contained in the COM marker.
2301  * @param       p_manager               the user event manager.
2302 */
2303 static OPJ_BOOL opj_j2k_read_com (  opj_j2k_t *p_j2k,
2304                                     OPJ_BYTE * p_header_data,
2305                                     OPJ_UINT32 p_header_size,
2306                                     opj_event_mgr_t * p_manager
2307                                     )
2308 {
2309         /* preconditions */
2310         assert(p_j2k != 00);
2311         assert(p_manager != 00);
2312         assert(p_header_data != 00);
2313   (void)p_header_size;
2314
2315         return OPJ_TRUE;
2316 }
2317
2318 OPJ_BOOL opj_j2k_write_cod(     opj_j2k_t *p_j2k,
2319                                                         opj_stream_private_t *p_stream,
2320                                                         opj_event_mgr_t * p_manager )
2321 {
2322         opj_cp_t *l_cp = 00;
2323         opj_tcp_t *l_tcp = 00;
2324         OPJ_UINT32 l_code_size,l_remaining_size;
2325         OPJ_BYTE * l_current_data = 00;
2326
2327         /* preconditions */
2328         assert(p_j2k != 00);
2329         assert(p_manager != 00);
2330         assert(p_stream != 00);
2331
2332         l_cp = &(p_j2k->m_cp);
2333         l_tcp = &l_cp->tcps[p_j2k->m_current_tile_number];
2334         l_code_size = 9 + opj_j2k_get_SPCod_SPCoc_size(p_j2k,p_j2k->m_current_tile_number,0);
2335         l_remaining_size = l_code_size;
2336
2337         if (l_code_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
2338                 OPJ_BYTE *new_header_tile_data = (OPJ_BYTE *) opj_realloc(p_j2k->m_specific_param.m_encoder.m_header_tile_data, l_code_size);
2339                 if (! new_header_tile_data) {
2340                         opj_free(p_j2k->m_specific_param.m_encoder.m_header_tile_data);
2341                         p_j2k->m_specific_param.m_encoder.m_header_tile_data = NULL;
2342                         p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = 0;
2343                         opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to write COD marker\n");
2344                         return OPJ_FALSE;
2345                 }
2346                 p_j2k->m_specific_param.m_encoder.m_header_tile_data = new_header_tile_data;
2347                 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_code_size;
2348         }
2349
2350         l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
2351
2352         opj_write_bytes(l_current_data,J2K_MS_COD,2);           /* COD */
2353         l_current_data += 2;
2354
2355         opj_write_bytes(l_current_data,l_code_size-2,2);        /* L_COD */
2356         l_current_data += 2;
2357
2358         opj_write_bytes(l_current_data,l_tcp->csty,1);          /* Scod */
2359         ++l_current_data;
2360
2361         opj_write_bytes(l_current_data,l_tcp->prg,1);           /* SGcod (A) */
2362         ++l_current_data;
2363
2364         opj_write_bytes(l_current_data,l_tcp->numlayers,2);     /* SGcod (B) */
2365         l_current_data+=2;
2366
2367         opj_write_bytes(l_current_data,l_tcp->mct,1);           /* SGcod (C) */
2368         ++l_current_data;
2369
2370         l_remaining_size -= 9;
2371
2372         if (! opj_j2k_write_SPCod_SPCoc(p_j2k,p_j2k->m_current_tile_number,0,l_current_data,&l_remaining_size,p_manager)) {
2373                 opj_event_msg(p_manager, EVT_ERROR, "Error writing COD marker\n");
2374                 return OPJ_FALSE;
2375         }
2376
2377         if (l_remaining_size != 0) {
2378                 opj_event_msg(p_manager, EVT_ERROR, "Error writing COD marker\n");
2379                 return OPJ_FALSE;
2380         }
2381
2382         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) {
2383                 return OPJ_FALSE;
2384         }
2385
2386         return OPJ_TRUE;
2387 }
2388
2389 /**
2390  * Reads a COD marker (Coding Styke defaults)
2391  * @param       p_header_data   the data contained in the COD box.
2392  * @param       p_j2k                   the jpeg2000 codec.
2393  * @param       p_header_size   the size of the data contained in the COD marker.
2394  * @param       p_manager               the user event manager.
2395 */
2396 static OPJ_BOOL opj_j2k_read_cod (  opj_j2k_t *p_j2k,
2397                                     OPJ_BYTE * p_header_data,
2398                                     OPJ_UINT32 p_header_size,
2399                                     opj_event_mgr_t * p_manager
2400                                     )
2401 {
2402         /* loop */
2403         OPJ_UINT32 i;
2404         OPJ_UINT32 l_tmp;
2405         opj_cp_t *l_cp = 00;
2406         opj_tcp_t *l_tcp = 00;
2407         opj_image_t *l_image = 00;
2408
2409         /* preconditions */
2410         assert(p_header_data != 00);
2411         assert(p_j2k != 00);
2412         assert(p_manager != 00);
2413
2414         l_image = p_j2k->m_private_image;
2415         l_cp = &(p_j2k->m_cp);
2416
2417         /* If we are in the first tile-part header of the current tile */
2418         l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ?
2419                                 &l_cp->tcps[p_j2k->m_current_tile_number] :
2420                                 p_j2k->m_specific_param.m_decoder.m_default_tcp;
2421
2422         /* Make sure room is sufficient */
2423         if (p_header_size < 5) {
2424                 opj_event_msg(p_manager, EVT_ERROR, "Error reading COD marker\n");
2425                 return OPJ_FALSE;
2426         }
2427
2428         opj_read_bytes(p_header_data,&l_tcp->csty,1);           /* Scod */
2429         ++p_header_data;
2430         opj_read_bytes(p_header_data,&l_tmp,1);                         /* SGcod (A) */
2431         ++p_header_data;
2432         l_tcp->prg = (OPJ_PROG_ORDER) l_tmp;
2433         opj_read_bytes(p_header_data,&l_tcp->numlayers,2);      /* SGcod (B) */
2434         p_header_data+=2;
2435
2436         /* If user didn't set a number layer to decode take the max specify in the codestream. */
2437         if      (l_cp->m_specific_param.m_dec.m_layer) {
2438                 l_tcp->num_layers_to_decode = l_cp->m_specific_param.m_dec.m_layer;
2439         }
2440         else {
2441                 l_tcp->num_layers_to_decode = l_tcp->numlayers;
2442         }
2443
2444         opj_read_bytes(p_header_data,&l_tcp->mct,1);            /* SGcod (C) */
2445         ++p_header_data;
2446
2447         p_header_size -= 5;
2448         for     (i = 0; i < l_image->numcomps; ++i) {
2449                 l_tcp->tccps[i].csty = l_tcp->csty & J2K_CCP_CSTY_PRT;
2450         }
2451
2452         if (! opj_j2k_read_SPCod_SPCoc(p_j2k,0,p_header_data,&p_header_size,p_manager)) {
2453                 opj_event_msg(p_manager, EVT_ERROR, "Error reading COD marker\n");
2454                 return OPJ_FALSE;
2455         }
2456
2457         if (p_header_size != 0) {
2458                 opj_event_msg(p_manager, EVT_ERROR, "Error reading COD marker\n");
2459                 return OPJ_FALSE;
2460         }
2461
2462         /* Apply the coding style to other components of the current tile or the m_default_tcp*/
2463         opj_j2k_copy_tile_component_parameters(p_j2k);
2464
2465         /* Index */
2466 #ifdef WIP_REMOVE_MSD
2467         if (p_j2k->cstr_info) {
2468                 /*opj_codestream_info_t *l_cstr_info = p_j2k->cstr_info;*/
2469                 p_j2k->cstr_info->prog = l_tcp->prg;
2470                 p_j2k->cstr_info->numlayers = l_tcp->numlayers;
2471                 p_j2k->cstr_info->numdecompos = (OPJ_INT32*) opj_malloc(l_image->numcomps * sizeof(OPJ_UINT32));
2472                 for     (i = 0; i < l_image->numcomps; ++i) {
2473                         p_j2k->cstr_info->numdecompos[i] = l_tcp->tccps[i].numresolutions - 1;
2474                 }
2475         }
2476 #endif
2477
2478         return OPJ_TRUE;
2479 }
2480
2481 #if 0
2482 OPJ_BOOL opj_j2k_write_coc( opj_j2k_t *p_j2k,
2483                                                 OPJ_UINT32 p_comp_no,
2484                                                 opj_stream_private_t *p_stream,
2485                                                 opj_event_mgr_t * p_manager )
2486 {
2487         OPJ_UINT32 l_coc_size,l_remaining_size;
2488         OPJ_UINT32 l_comp_room;
2489
2490         /* preconditions */
2491         assert(p_j2k != 00);
2492         assert(p_manager != 00);
2493         assert(p_stream != 00);
2494
2495         l_comp_room = (p_j2k->m_private_image->numcomps <= 256) ? 1 : 2;
2496
2497         l_coc_size = 5 + l_comp_room + opj_j2k_get_SPCod_SPCoc_size(p_j2k,p_j2k->m_current_tile_number,p_comp_no);
2498
2499         if (l_coc_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
2500                 OPJ_BYTE *new_header_tile_data;
2501                 /*p_j2k->m_specific_param.m_encoder.m_header_tile_data
2502                         = (OPJ_BYTE*)opj_realloc(
2503                                 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
2504                                 l_coc_size);*/
2505
2506                 new_header_tile_data = (OPJ_BYTE *) opj_realloc(p_j2k->m_specific_param.m_encoder.m_header_tile_data, l_coc_size);
2507                 if (! new_header_tile_data) {
2508                         opj_free(p_j2k->m_specific_param.m_encoder.m_header_tile_data);
2509                         p_j2k->m_specific_param.m_encoder.m_header_tile_data = NULL;
2510                         p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = 0;
2511                         opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to write COC marker\n");
2512                         return OPJ_FALSE;
2513                 }
2514                 p_j2k->m_specific_param.m_encoder.m_header_tile_data = new_header_tile_data;
2515                 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_coc_size;
2516         }
2517
2518         opj_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);
2519
2520         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) {
2521                 return OPJ_FALSE;
2522         }
2523
2524         return OPJ_TRUE;
2525 }
2526 #endif
2527
2528 #if 0
2529 void opj_j2k_write_coc_in_memory(   opj_j2k_t *p_j2k,
2530                                                 OPJ_UINT32 p_comp_no,
2531                                                 OPJ_BYTE * p_data,
2532                                                 OPJ_UINT32 * p_data_written,
2533                                                 opj_event_mgr_t * p_manager
2534                                     )
2535 {
2536         opj_cp_t *l_cp = 00;
2537         opj_tcp_t *l_tcp = 00;
2538         OPJ_UINT32 l_coc_size,l_remaining_size;
2539         OPJ_BYTE * l_current_data = 00;
2540         opj_image_t *l_image = 00;
2541         OPJ_UINT32 l_comp_room;
2542
2543         /* preconditions */
2544         assert(p_j2k != 00);
2545         assert(p_manager != 00);
2546
2547         l_cp = &(p_j2k->m_cp);
2548         l_tcp = &l_cp->tcps[p_j2k->m_current_tile_number];
2549         l_image = p_j2k->m_private_image;
2550         l_comp_room = (l_image->numcomps <= 256) ? 1 : 2;
2551
2552         l_coc_size = 5 + l_comp_room + opj_j2k_get_SPCod_SPCoc_size(p_j2k,p_j2k->m_current_tile_number,p_comp_no);
2553         l_remaining_size = l_coc_size;
2554
2555         l_current_data = p_data;
2556
2557         opj_write_bytes(l_current_data,J2K_MS_COC,2);                           /* COC */
2558         l_current_data += 2;
2559
2560         opj_write_bytes(l_current_data,l_coc_size-2,2);                         /* L_COC */
2561         l_current_data += 2;
2562
2563         opj_write_bytes(l_current_data,p_comp_no, l_comp_room);         /* Ccoc */
2564         l_current_data+=l_comp_room;
2565
2566         opj_write_bytes(l_current_data, l_tcp->tccps[p_comp_no].csty, 1);               /* Scoc */
2567         ++l_current_data;
2568
2569         l_remaining_size -= (5 + l_comp_room);
2570         opj_j2k_write_SPCod_SPCoc(p_j2k,p_j2k->m_current_tile_number,0,l_current_data,&l_remaining_size,p_manager);
2571         * p_data_written = l_coc_size;
2572 }
2573 #endif
2574
2575 OPJ_UINT32 opj_j2k_get_max_coc_size(opj_j2k_t *p_j2k)
2576 {
2577         OPJ_UINT32 i,j;
2578         OPJ_UINT32 l_nb_comp;
2579         OPJ_UINT32 l_nb_tiles;
2580         OPJ_UINT32 l_max = 0;
2581
2582         /* preconditions */
2583
2584         l_nb_tiles = p_j2k->m_cp.tw * p_j2k->m_cp.th ;
2585         l_nb_comp = p_j2k->m_private_image->numcomps;
2586
2587         for (i=0;i<l_nb_tiles;++i) {
2588                 for (j=0;j<l_nb_comp;++j) {
2589                         l_max = opj_uint_max(l_max,opj_j2k_get_SPCod_SPCoc_size(p_j2k,i,j));
2590                 }
2591         }
2592
2593         return 6 + l_max;
2594 }
2595
2596 /**
2597  * Reads a COC marker (Coding Style Component)
2598  * @param       p_header_data   the data contained in the COC box.
2599  * @param       p_j2k                   the jpeg2000 codec.
2600  * @param       p_header_size   the size of the data contained in the COC marker.
2601  * @param       p_manager               the user event manager.
2602 */
2603 static OPJ_BOOL opj_j2k_read_coc (  opj_j2k_t *p_j2k,
2604                                     OPJ_BYTE * p_header_data,
2605                                     OPJ_UINT32 p_header_size,
2606                                     opj_event_mgr_t * p_manager
2607                                     )
2608 {
2609         opj_cp_t *l_cp = NULL;
2610         opj_tcp_t *l_tcp = NULL;
2611         opj_image_t *l_image = NULL;
2612         OPJ_UINT32 l_comp_room;
2613         OPJ_UINT32 l_comp_no;
2614
2615         /* preconditions */
2616         assert(p_header_data != 00);
2617         assert(p_j2k != 00);
2618         assert(p_manager != 00);
2619
2620         l_cp = &(p_j2k->m_cp);
2621         l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH ) ? /*FIXME J2K_DEC_STATE_TPH*/
2622                                 &l_cp->tcps[p_j2k->m_current_tile_number] :
2623                                 p_j2k->m_specific_param.m_decoder.m_default_tcp;
2624         l_image = p_j2k->m_private_image;
2625
2626         l_comp_room = l_image->numcomps <= 256 ? 1 : 2;
2627
2628         /* make sure room is sufficient*/
2629         if (p_header_size < l_comp_room + 1) {
2630                 opj_event_msg(p_manager, EVT_ERROR, "Error reading COC marker\n");
2631                 return OPJ_FALSE;
2632         }
2633         p_header_size -= l_comp_room + 1;
2634
2635         opj_read_bytes(p_header_data,&l_comp_no,l_comp_room);                   /* Ccoc */
2636         p_header_data += l_comp_room;
2637         if (l_comp_no >= l_image->numcomps) {
2638                 opj_event_msg(p_manager, EVT_ERROR, "Error reading COC marker (bad number of components)\n");
2639                 return OPJ_FALSE;
2640         }
2641
2642         opj_read_bytes(p_header_data,&l_tcp->tccps[l_comp_no].csty,1);                  /* Scoc */
2643         ++p_header_data ;
2644
2645         if (! opj_j2k_read_SPCod_SPCoc(p_j2k,l_comp_no,p_header_data,&p_header_size,p_manager)) {
2646                 opj_event_msg(p_manager, EVT_ERROR, "Error reading COC marker\n");
2647                 return OPJ_FALSE;
2648         }
2649
2650         if (p_header_size != 0) {
2651                 opj_event_msg(p_manager, EVT_ERROR, "Error reading COC marker\n");
2652                 return OPJ_FALSE;
2653         }
2654         return OPJ_TRUE;
2655 }
2656
2657 OPJ_BOOL opj_j2k_write_qcd(     opj_j2k_t *p_j2k,
2658                                                         opj_stream_private_t *p_stream,
2659                                                         opj_event_mgr_t * p_manager
2660                             )
2661 {
2662         OPJ_UINT32 l_qcd_size,l_remaining_size;
2663         OPJ_BYTE * l_current_data = 00;
2664
2665         /* preconditions */
2666         assert(p_j2k != 00);
2667         assert(p_manager != 00);
2668         assert(p_stream != 00);
2669
2670         l_qcd_size = 4 + opj_j2k_get_SQcd_SQcc_size(p_j2k,p_j2k->m_current_tile_number,0);
2671         l_remaining_size = l_qcd_size;
2672
2673         if (l_qcd_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
2674                 OPJ_BYTE *new_header_tile_data = (OPJ_BYTE *) opj_realloc(p_j2k->m_specific_param.m_encoder.m_header_tile_data, l_qcd_size);
2675                 if (! new_header_tile_data) {
2676                         opj_free(p_j2k->m_specific_param.m_encoder.m_header_tile_data);
2677                         p_j2k->m_specific_param.m_encoder.m_header_tile_data = NULL;
2678                         p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = 0;
2679                         opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to write QCD marker\n");
2680                         return OPJ_FALSE;
2681                 }
2682                 p_j2k->m_specific_param.m_encoder.m_header_tile_data = new_header_tile_data;
2683                 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_qcd_size;
2684         }
2685
2686         l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
2687
2688         opj_write_bytes(l_current_data,J2K_MS_QCD,2);           /* QCD */
2689         l_current_data += 2;
2690
2691         opj_write_bytes(l_current_data,l_qcd_size-2,2);         /* L_QCD */
2692         l_current_data += 2;
2693
2694         l_remaining_size -= 4;
2695
2696         if (! opj_j2k_write_SQcd_SQcc(p_j2k,p_j2k->m_current_tile_number,0,l_current_data,&l_remaining_size,p_manager)) {
2697                 opj_event_msg(p_manager, EVT_ERROR, "Error writing QCD marker\n");
2698                 return OPJ_FALSE;
2699         }
2700
2701         if (l_remaining_size != 0) {
2702                 opj_event_msg(p_manager, EVT_ERROR, "Error writing QCD marker\n");
2703                 return OPJ_FALSE;
2704         }
2705
2706         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) {
2707                 return OPJ_FALSE;
2708         }
2709
2710         return OPJ_TRUE;
2711 }
2712
2713 /**
2714  * Reads a QCD marker (Quantization defaults)
2715  * @param       p_header_data   the data contained in the QCD box.
2716  * @param       p_j2k                   the jpeg2000 codec.
2717  * @param       p_header_size   the size of the data contained in the QCD marker.
2718  * @param       p_manager               the user event manager.
2719 */
2720 static OPJ_BOOL opj_j2k_read_qcd (  opj_j2k_t *p_j2k,
2721                                     OPJ_BYTE * p_header_data,
2722                                     OPJ_UINT32 p_header_size,
2723                                     opj_event_mgr_t * p_manager
2724                                     )
2725 {
2726         /* preconditions */
2727         assert(p_header_data != 00);
2728         assert(p_j2k != 00);
2729         assert(p_manager != 00);
2730
2731         if (! opj_j2k_read_SQcd_SQcc(p_j2k,0,p_header_data,&p_header_size,p_manager)) {
2732                 opj_event_msg(p_manager, EVT_ERROR, "Error reading QCD marker\n");
2733                 return OPJ_FALSE;
2734         }
2735
2736         if (p_header_size != 0) {
2737                 opj_event_msg(p_manager, EVT_ERROR, "Error reading QCD marker\n");
2738                 return OPJ_FALSE;
2739         }
2740
2741         /* Apply the quantization parameters to other components of the current tile or the m_default_tcp */
2742         opj_j2k_copy_tile_quantization_parameters(p_j2k);
2743
2744         return OPJ_TRUE;
2745 }
2746
2747 #if 0
2748 OPJ_BOOL opj_j2k_write_qcc(     opj_j2k_t *p_j2k,
2749                                                 OPJ_UINT32 p_comp_no,
2750                                                 opj_stream_private_t *p_stream,
2751                                                 opj_event_mgr_t * p_manager
2752                             )
2753 {
2754         OPJ_UINT32 l_qcc_size,l_remaining_size;
2755
2756         /* preconditions */
2757         assert(p_j2k != 00);
2758         assert(p_manager != 00);
2759         assert(p_stream != 00);
2760
2761         l_qcc_size = 5 + opj_j2k_get_SQcd_SQcc_size(p_j2k,p_j2k->m_current_tile_number,p_comp_no);
2762         l_qcc_size += p_j2k->m_private_image->numcomps <= 256 ? 0:1;
2763         l_remaining_size = l_qcc_size;
2764
2765         if (l_qcc_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
2766                 OPJ_BYTE *new_header_tile_data = (OPJ_BYTE *) opj_realloc(p_j2k->m_specific_param.m_encoder.m_header_tile_data, l_qcc_size);
2767                 if (! new_header_tile_data) {
2768                         opj_free(p_j2k->m_specific_param.m_encoder.m_header_tile_data);
2769                         p_j2k->m_specific_param.m_encoder.m_header_tile_data = NULL;
2770                         p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = 0;
2771                         opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to write QCC marker\n");
2772                         return OPJ_FALSE;
2773                 }
2774                 p_j2k->m_specific_param.m_encoder.m_header_tile_data = new_header_tile_data;
2775                 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_qcc_size;
2776         }
2777
2778         opj_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);
2779
2780         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) {
2781                 return OPJ_FALSE;
2782         }
2783
2784         return OPJ_TRUE;
2785 }
2786 #endif
2787
2788 #if 0
2789 void opj_j2k_write_qcc_in_memory(   opj_j2k_t *p_j2k,
2790                                                                 OPJ_UINT32 p_comp_no,
2791                                                                 OPJ_BYTE * p_data,
2792                                                                 OPJ_UINT32 * p_data_written,
2793                                                                 opj_event_mgr_t * p_manager
2794                                     )
2795 {
2796         OPJ_UINT32 l_qcc_size,l_remaining_size;
2797         OPJ_BYTE * l_current_data = 00;
2798
2799         /* preconditions */
2800         assert(p_j2k != 00);
2801         assert(p_manager != 00);
2802
2803         l_qcc_size = 6 + opj_j2k_get_SQcd_SQcc_size(p_j2k,p_j2k->m_current_tile_number,p_comp_no);
2804         l_remaining_size = l_qcc_size;
2805
2806         l_current_data = p_data;
2807
2808         opj_write_bytes(l_current_data,J2K_MS_QCC,2);           /* QCC */
2809         l_current_data += 2;
2810
2811         if (p_j2k->m_private_image->numcomps <= 256) {
2812                 --l_qcc_size;
2813
2814                 opj_write_bytes(l_current_data,l_qcc_size-2,2);         /* L_QCC */
2815                 l_current_data += 2;
2816
2817                 opj_write_bytes(l_current_data, p_comp_no, 1);  /* Cqcc */
2818                 ++l_current_data;
2819
2820                 /* in the case only one byte is sufficient the last byte allocated is useless -> still do -6 for available */
2821                 l_remaining_size -= 6;
2822         }
2823         else {
2824                 opj_write_bytes(l_current_data,l_qcc_size-2,2);         /* L_QCC */
2825                 l_current_data += 2;
2826
2827                 opj_write_bytes(l_current_data, p_comp_no, 2);  /* Cqcc */
2828                 l_current_data+=2;
2829
2830                 l_remaining_size -= 6;
2831         }
2832
2833         opj_j2k_write_SQcd_SQcc(p_j2k,p_j2k->m_current_tile_number,p_comp_no,l_current_data,&l_remaining_size,p_manager);
2834
2835         *p_data_written = l_qcc_size;
2836 }
2837 #endif
2838
2839 OPJ_UINT32 opj_j2k_get_max_qcc_size (opj_j2k_t *p_j2k)
2840 {
2841         return opj_j2k_get_max_coc_size(p_j2k);
2842 }
2843
2844 /**
2845  * Reads a QCC marker (Quantization component)
2846  * @param       p_header_data   the data contained in the QCC box.
2847  * @param       p_j2k                   the jpeg2000 codec.
2848  * @param       p_header_size   the size of the data contained in the QCC marker.
2849  * @param       p_manager               the user event manager.
2850 */
2851 static OPJ_BOOL opj_j2k_read_qcc(   opj_j2k_t *p_j2k,
2852                                     OPJ_BYTE * p_header_data,
2853                                     OPJ_UINT32 p_header_size,
2854                                     opj_event_mgr_t * p_manager
2855                                     )
2856 {
2857         OPJ_UINT32 l_num_comp,l_comp_no;
2858
2859         /* preconditions */
2860         assert(p_header_data != 00);
2861         assert(p_j2k != 00);
2862         assert(p_manager != 00);
2863
2864         l_num_comp = p_j2k->m_private_image->numcomps;
2865
2866         if (l_num_comp <= 256) {
2867                 if (p_header_size < 1) {
2868                         opj_event_msg(p_manager, EVT_ERROR, "Error reading QCC marker\n");
2869                         return OPJ_FALSE;
2870                 }
2871                 opj_read_bytes(p_header_data,&l_comp_no,1);
2872                 ++p_header_data;
2873                 --p_header_size;
2874         }
2875         else {
2876                 if (p_header_size < 2) {
2877                         opj_event_msg(p_manager, EVT_ERROR, "Error reading QCC marker\n");
2878                         return OPJ_FALSE;
2879                 }
2880                 opj_read_bytes(p_header_data,&l_comp_no,2);
2881                 p_header_data+=2;
2882                 p_header_size-=2;
2883         }
2884
2885 #ifdef USE_JPWL
2886         if (p_j2k->m_cp.correct) {
2887
2888                 static OPJ_UINT32 backup_compno = 0;
2889
2890                 /* compno is negative or larger than the number of components!!! */
2891                 if (/*(l_comp_no < 0) ||*/ (l_comp_no >= l_num_comp)) {
2892                         opj_event_msg(p_manager, EVT_ERROR,
2893                                 "JPWL: bad component number in QCC (%d out of a maximum of %d)\n",
2894                                 l_comp_no, l_num_comp);
2895                         if (!JPWL_ASSUME) {
2896                                 opj_event_msg(p_manager, EVT_ERROR, "JPWL: giving up\n");
2897                                 return OPJ_FALSE;
2898                         }
2899                         /* we try to correct */
2900                         l_comp_no = backup_compno % l_num_comp;
2901                         opj_event_msg(p_manager, EVT_WARNING, "- trying to adjust this\n"
2902                                 "- setting component number to %d\n",
2903                                 l_comp_no);
2904                 }
2905
2906                 /* keep your private count of tiles */
2907                 backup_compno++;
2908         };
2909 #endif /* USE_JPWL */
2910
2911         if (l_comp_no >= p_j2k->m_private_image->numcomps) {
2912                 opj_event_msg(p_manager, EVT_ERROR,
2913                               "Invalid component number: %d, regarding the number of components %d\n",
2914                               l_comp_no, p_j2k->m_private_image->numcomps);
2915                 return OPJ_FALSE;
2916         }
2917
2918         if (! opj_j2k_read_SQcd_SQcc(p_j2k,l_comp_no,p_header_data,&p_header_size,p_manager)) {
2919                 opj_event_msg(p_manager, EVT_ERROR, "Error reading QCC marker\n");
2920                 return OPJ_FALSE;
2921         }
2922
2923         if (p_header_size != 0) {
2924                 opj_event_msg(p_manager, EVT_ERROR, "Error reading QCC marker\n");
2925                 return OPJ_FALSE;
2926         }
2927
2928         return OPJ_TRUE;
2929 }
2930
2931 OPJ_BOOL opj_j2k_write_poc(     opj_j2k_t *p_j2k,
2932                                                         opj_stream_private_t *p_stream,
2933                                                         opj_event_mgr_t * p_manager
2934                             )
2935 {
2936         OPJ_UINT32 l_nb_comp;
2937         OPJ_UINT32 l_nb_poc;
2938         OPJ_UINT32 l_poc_size;
2939         OPJ_UINT32 l_written_size = 0;
2940         opj_tcp_t *l_tcp = 00;
2941         OPJ_UINT32 l_poc_room;
2942
2943         /* preconditions */
2944         assert(p_j2k != 00);
2945         assert(p_manager != 00);
2946         assert(p_stream != 00);
2947
2948         l_tcp = &p_j2k->m_cp.tcps[p_j2k->m_current_tile_number];
2949         l_nb_comp = p_j2k->m_private_image->numcomps;
2950         l_nb_poc = 1 + l_tcp->numpocs;
2951
2952         if (l_nb_comp <= 256) {
2953                 l_poc_room = 1;
2954         }
2955         else {
2956                 l_poc_room = 2;
2957         }
2958         l_poc_size = 4 + (5 + 2 * l_poc_room) * l_nb_poc;
2959
2960         if (l_poc_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
2961                 OPJ_BYTE *new_header_tile_data = (OPJ_BYTE *) opj_realloc(p_j2k->m_specific_param.m_encoder.m_header_tile_data, l_poc_size);
2962                 if (! new_header_tile_data) {
2963                         opj_free(p_j2k->m_specific_param.m_encoder.m_header_tile_data);
2964                         p_j2k->m_specific_param.m_encoder.m_header_tile_data = NULL;
2965                         p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = 0;
2966                         opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to write POC marker\n");
2967                         return OPJ_FALSE;
2968                 }
2969                 p_j2k->m_specific_param.m_encoder.m_header_tile_data = new_header_tile_data;
2970                 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_poc_size;
2971         }
2972
2973         opj_j2k_write_poc_in_memory(p_j2k,p_j2k->m_specific_param.m_encoder.m_header_tile_data,&l_written_size,p_manager);
2974
2975         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) {
2976                 return OPJ_FALSE;
2977         }
2978
2979         return OPJ_TRUE;
2980 }
2981
2982 void opj_j2k_write_poc_in_memory(   opj_j2k_t *p_j2k,
2983                                                                 OPJ_BYTE * p_data,
2984                                                                 OPJ_UINT32 * p_data_written,
2985                                                                 opj_event_mgr_t * p_manager
2986                                     )
2987 {
2988         OPJ_UINT32 i;
2989         OPJ_BYTE * l_current_data = 00;
2990         OPJ_UINT32 l_nb_comp;
2991         OPJ_UINT32 l_nb_poc;
2992         OPJ_UINT32 l_poc_size;
2993         opj_image_t *l_image = 00;
2994         opj_tcp_t *l_tcp = 00;
2995         opj_tccp_t *l_tccp = 00;
2996         opj_poc_t *l_current_poc = 00;
2997         OPJ_UINT32 l_poc_room;
2998
2999         /* preconditions */
3000         assert(p_j2k != 00);
3001         assert(p_manager != 00);
3002
3003         l_tcp = &p_j2k->m_cp.tcps[p_j2k->m_current_tile_number];
3004         l_tccp = &l_tcp->tccps[0];
3005         l_image = p_j2k->m_private_image;
3006         l_nb_comp = l_image->numcomps;
3007         l_nb_poc = 1 + l_tcp->numpocs;
3008
3009         if (l_nb_comp <= 256) {
3010                 l_poc_room = 1;
3011         }
3012         else {
3013                 l_poc_room = 2;
3014         }
3015
3016         l_poc_size = 4 + (5 + 2 * l_poc_room) * l_nb_poc;
3017
3018         l_current_data = p_data;
3019
3020         opj_write_bytes(l_current_data,J2K_MS_POC,2);                                   /* POC  */
3021         l_current_data += 2;
3022
3023         opj_write_bytes(l_current_data,l_poc_size-2,2);                                 /* Lpoc */
3024         l_current_data += 2;
3025
3026         l_current_poc =  l_tcp->pocs;
3027         for (i = 0; i < l_nb_poc; ++i) {
3028                 opj_write_bytes(l_current_data,l_current_poc->resno0,1);                                /* RSpoc_i */
3029                 ++l_current_data;
3030
3031                 opj_write_bytes(l_current_data,l_current_poc->compno0,l_poc_room);              /* CSpoc_i */
3032                 l_current_data+=l_poc_room;
3033
3034                 opj_write_bytes(l_current_data,l_current_poc->layno1,2);                                /* LYEpoc_i */
3035                 l_current_data+=2;
3036
3037                 opj_write_bytes(l_current_data,l_current_poc->resno1,1);                                /* REpoc_i */
3038                 ++l_current_data;
3039
3040                 opj_write_bytes(l_current_data,l_current_poc->compno1,l_poc_room);              /* CEpoc_i */
3041                 l_current_data+=l_poc_room;
3042
3043                 opj_write_bytes(l_current_data,l_current_poc->prg,1);                                   /* Ppoc_i */
3044                 ++l_current_data;
3045
3046                 /* change the value of the max layer according to the actual number of layers in the file, components and resolutions*/
3047                 l_current_poc->layno1 = (OPJ_UINT32)opj_int_min((OPJ_INT32)l_current_poc->layno1, (OPJ_INT32)l_tcp->numlayers);
3048                 l_current_poc->resno1 = (OPJ_UINT32)opj_int_min((OPJ_INT32)l_current_poc->resno1, (OPJ_INT32)l_tccp->numresolutions);
3049                 l_current_poc->compno1 = (OPJ_UINT32)opj_int_min((OPJ_INT32)l_current_poc->compno1, (OPJ_INT32)l_nb_comp);
3050
3051                 ++l_current_poc;
3052         }
3053
3054         *p_data_written = l_poc_size;
3055 }
3056
3057 OPJ_UINT32 opj_j2k_get_max_poc_size(opj_j2k_t *p_j2k)
3058 {
3059         opj_tcp_t * l_tcp = 00;
3060         OPJ_UINT32 l_nb_tiles = 0;
3061         OPJ_UINT32 l_max_poc = 0;
3062         OPJ_UINT32 i;
3063
3064         l_tcp = p_j2k->m_cp.tcps;
3065         l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw;
3066
3067         for (i=0;i<l_nb_tiles;++i) {
3068                 l_max_poc = opj_uint_max(l_max_poc,l_tcp->numpocs);
3069                 ++l_tcp;
3070         }
3071
3072         ++l_max_poc;
3073
3074         return 4 + 9 * l_max_poc;
3075 }
3076
3077 OPJ_UINT32 opj_j2k_get_max_toc_size (opj_j2k_t *p_j2k)
3078 {
3079         OPJ_UINT32 i;
3080         OPJ_UINT32 l_nb_tiles;
3081         OPJ_UINT32 l_max = 0;
3082         opj_tcp_t * l_tcp = 00;
3083
3084         l_tcp = p_j2k->m_cp.tcps;
3085         l_nb_tiles = p_j2k->m_cp.tw * p_j2k->m_cp.th ;
3086
3087         for (i=0;i<l_nb_tiles;++i) {
3088                 l_max = opj_uint_max(l_max,l_tcp->m_nb_tile_parts);
3089
3090                 ++l_tcp;
3091         }
3092
3093         return 12 * l_max;
3094 }
3095
3096 OPJ_UINT32 opj_j2k_get_specific_header_sizes(opj_j2k_t *p_j2k)
3097 {
3098         OPJ_UINT32 l_nb_bytes = 0;
3099         OPJ_UINT32 l_nb_comps;
3100         OPJ_UINT32 l_coc_bytes,l_qcc_bytes;
3101
3102         l_nb_comps = p_j2k->m_private_image->numcomps - 1;
3103         l_nb_bytes += opj_j2k_get_max_toc_size(p_j2k);
3104
3105         if (p_j2k->m_cp.m_specific_param.m_enc.m_cinema == 0) {
3106                 l_coc_bytes = opj_j2k_get_max_coc_size(p_j2k);
3107                 l_nb_bytes += l_nb_comps * l_coc_bytes;
3108
3109                 l_qcc_bytes = opj_j2k_get_max_qcc_size(p_j2k);
3110                 l_nb_bytes += l_nb_comps * l_qcc_bytes;
3111         }
3112
3113         l_nb_bytes += opj_j2k_get_max_poc_size(p_j2k);
3114
3115         /*** DEVELOPER CORNER, Add room for your headers ***/
3116
3117         return l_nb_bytes;
3118 }
3119
3120 /**
3121  * Reads a POC marker (Progression Order Change)
3122  *
3123  * @param       p_header_data   the data contained in the POC box.
3124  * @param       p_j2k                   the jpeg2000 codec.
3125  * @param       p_header_size   the size of the data contained in the POC marker.
3126  * @param       p_manager               the user event manager.
3127 */
3128 static OPJ_BOOL opj_j2k_read_poc (  opj_j2k_t *p_j2k,
3129                                     OPJ_BYTE * p_header_data,
3130                                     OPJ_UINT32 p_header_size,
3131                                     opj_event_mgr_t * p_manager
3132                                     )
3133 {
3134         OPJ_UINT32 i, l_nb_comp, l_tmp;
3135         opj_image_t * l_image = 00;
3136         OPJ_UINT32 l_old_poc_nb, l_current_poc_nb, l_current_poc_remaining;
3137         OPJ_UINT32 l_chunk_size, l_comp_room;
3138
3139         opj_cp_t *l_cp = 00;
3140         opj_tcp_t *l_tcp = 00;
3141         opj_poc_t *l_current_poc = 00;
3142
3143         /* preconditions */
3144         assert(p_header_data != 00);
3145         assert(p_j2k != 00);
3146         assert(p_manager != 00);
3147
3148         l_image = p_j2k->m_private_image;
3149         l_nb_comp = l_image->numcomps;
3150         if (l_nb_comp <= 256) {
3151                 l_comp_room = 1;
3152         }
3153         else {
3154                 l_comp_room = 2;
3155         }
3156         l_chunk_size = 5 + 2 * l_comp_room;
3157         l_current_poc_nb = p_header_size / l_chunk_size;
3158         l_current_poc_remaining = p_header_size % l_chunk_size;
3159
3160         if ((l_current_poc_nb <= 0) || (l_current_poc_remaining != 0)) {
3161                 opj_event_msg(p_manager, EVT_ERROR, "Error reading POC marker\n");
3162                 return OPJ_FALSE;
3163         }
3164
3165         l_cp = &(p_j2k->m_cp);
3166         l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ?
3167                                 &l_cp->tcps[p_j2k->m_current_tile_number] :
3168                                 p_j2k->m_specific_param.m_decoder.m_default_tcp;
3169         l_old_poc_nb = l_tcp->POC ? l_tcp->numpocs + 1 : 0;
3170         l_current_poc_nb += l_old_poc_nb;
3171
3172         if(l_current_poc_nb >= 32)
3173           {
3174           opj_event_msg(p_manager, EVT_ERROR, "Too many POCs %d\n", l_current_poc_nb);
3175           return OPJ_FALSE;
3176           }
3177         assert(l_current_poc_nb < 32);
3178
3179         /* now poc is in use.*/
3180         l_tcp->POC = 1;
3181
3182         l_current_poc = &l_tcp->pocs[l_old_poc_nb];
3183         for     (i = l_old_poc_nb; i < l_current_poc_nb; ++i) {
3184                 opj_read_bytes(p_header_data,&(l_current_poc->resno0),1);                               /* RSpoc_i */
3185                 ++p_header_data;
3186                 opj_read_bytes(p_header_data,&(l_current_poc->compno0),l_comp_room);    /* CSpoc_i */
3187                 p_header_data+=l_comp_room;
3188                 opj_read_bytes(p_header_data,&(l_current_poc->layno1),2);                               /* LYEpoc_i */
3189                 /* make sure layer end is in acceptable bounds */
3190                 l_current_poc->layno1 = opj_uint_min(l_current_poc->layno1, l_tcp->numlayers);
3191                 p_header_data+=2;
3192                 opj_read_bytes(p_header_data,&(l_current_poc->resno1),1);                               /* REpoc_i */
3193                 ++p_header_data;
3194                 opj_read_bytes(p_header_data,&(l_current_poc->compno1),l_comp_room);    /* CEpoc_i */
3195                 p_header_data+=l_comp_room;
3196                 opj_read_bytes(p_header_data,&l_tmp,1);                                                                 /* Ppoc_i */
3197                 ++p_header_data;
3198                 l_current_poc->prg = (OPJ_PROG_ORDER) l_tmp;
3199                 /* make sure comp is in acceptable bounds */
3200                 l_current_poc->compno1 = opj_uint_min(l_current_poc->compno1, l_nb_comp);
3201                 ++l_current_poc;
3202         }
3203
3204         l_tcp->numpocs = l_current_poc_nb - 1;
3205         return OPJ_TRUE;
3206 }
3207
3208 /**
3209  * Reads a CRG marker (Component registration)
3210  *
3211  * @param       p_header_data   the data contained in the TLM box.
3212  * @param       p_j2k                   the jpeg2000 codec.
3213  * @param       p_header_size   the size of the data contained in the TLM marker.
3214  * @param       p_manager               the user event manager.
3215 */
3216 static OPJ_BOOL opj_j2k_read_crg (  opj_j2k_t *p_j2k,
3217                                     OPJ_BYTE * p_header_data,
3218                                     OPJ_UINT32 p_header_size,
3219                                     opj_event_mgr_t * p_manager
3220                                     )
3221 {
3222         OPJ_UINT32 l_nb_comp;
3223         /* preconditions */
3224         assert(p_header_data != 00);
3225         assert(p_j2k != 00);
3226         assert(p_manager != 00);
3227
3228         l_nb_comp = p_j2k->m_private_image->numcomps;
3229
3230         if (p_header_size != l_nb_comp *4) {
3231                 opj_event_msg(p_manager, EVT_ERROR, "Error reading CRG marker\n");
3232                 return OPJ_FALSE;
3233         }
3234         /* Do not care of this at the moment since only local variables are set here */
3235         /*
3236         for
3237                 (i = 0; i < l_nb_comp; ++i)
3238         {
3239                 opj_read_bytes(p_header_data,&l_Xcrg_i,2);                              // Xcrg_i
3240                 p_header_data+=2;
3241                 opj_read_bytes(p_header_data,&l_Ycrg_i,2);                              // Xcrg_i
3242                 p_header_data+=2;
3243         }
3244         */
3245         return OPJ_TRUE;
3246 }
3247
3248 /**
3249  * Reads a TLM marker (Tile Length Marker)
3250  *
3251  * @param       p_header_data   the data contained in the TLM box.
3252  * @param       p_j2k                   the jpeg2000 codec.
3253  * @param       p_header_size   the size of the data contained in the TLM marker.
3254  * @param       p_manager               the user event manager.
3255 */
3256 static OPJ_BOOL opj_j2k_read_tlm (  opj_j2k_t *p_j2k,
3257                                     OPJ_BYTE * p_header_data,
3258                                     OPJ_UINT32 p_header_size,
3259                                     opj_event_mgr_t * p_manager
3260                                     )
3261 {
3262         OPJ_UINT32 l_Ztlm, l_Stlm, l_ST, l_SP, l_tot_num_tp_remaining, l_quotient, l_Ptlm_size;
3263         /* preconditions */
3264         assert(p_header_data != 00);
3265         assert(p_j2k != 00);
3266         assert(p_manager != 00);
3267
3268         if (p_header_size < 2) {
3269       &n