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