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