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