Update to openjpeg r2944
[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;
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         if (l_image->x0 > l_image->x1 || l_image->y0 > l_image->y1) {
1985                 opj_event_msg(p_manager, EVT_ERROR, "Error with SIZ marker: negative image size (%d x %d)\n", l_image->x1 - l_image->x0, l_image->y1 - l_image->y0);
1986                 return OPJ_FALSE;
1987         }
1988         /* testcase 2539.pdf.SIGFPE.706.1712 (also 3622.pdf.SIGFPE.706.2916 and 4008.pdf.SIGFPE.706.3345 and maybe more) */
1989         if (!(l_cp->tdx * l_cp->tdy)) {
1990                 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);
1991                 return OPJ_FALSE;
1992         }
1993
1994         /* testcase 1610.pdf.SIGSEGV.59c.681 */
1995         if (((OPJ_UINT64)l_image->x1) * ((OPJ_UINT64)l_image->y1) != (l_image->x1 * l_image->y1)) {
1996                 opj_event_msg(p_manager, EVT_ERROR, "Prevent buffer overflow (x1: %d, y1: %d)\n", l_image->x1, l_image->y1);
1997                 return OPJ_FALSE;
1998         }
1999
2000 #ifdef USE_JPWL
2001         if (l_cp->correct) {
2002                 /* if JPWL is on, we check whether TX errors have damaged
2003                   too much the SIZ parameters */
2004                 if (!(l_image->x1 * l_image->y1)) {
2005                         opj_event_msg(p_manager, EVT_ERROR,
2006                                 "JPWL: bad image size (%d x %d)\n",
2007                                 l_image->x1, l_image->y1);
2008                         if (!JPWL_ASSUME || JPWL_ASSUME) {
2009                                 opj_event_msg(p_manager, EVT_ERROR, "JPWL: giving up\n");
2010                                 return OPJ_FALSE;
2011                         }
2012                 }
2013
2014         /* FIXME check previously in the function so why keep this piece of code ? Need by the norm ?
2015                 if (l_image->numcomps != ((len - 38) / 3)) {
2016                         opj_event_msg(p_manager, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
2017                                 "JPWL: Csiz is %d => space in SIZ only for %d comps.!!!\n",
2018                                 l_image->numcomps, ((len - 38) / 3));
2019                         if (!JPWL_ASSUME) {
2020                                 opj_event_msg(p_manager, EVT_ERROR, "JPWL: giving up\n");
2021                                 return OPJ_FALSE;
2022                         }
2023         */              /* we try to correct */
2024         /*              opj_event_msg(p_manager, EVT_WARNING, "- trying to adjust this\n");
2025                         if (l_image->numcomps < ((len - 38) / 3)) {
2026                                 len = 38 + 3 * l_image->numcomps;
2027                                 opj_event_msg(p_manager, EVT_WARNING, "- setting Lsiz to %d => HYPOTHESIS!!!\n",
2028                                         len);
2029                         } else {
2030                                 l_image->numcomps = ((len - 38) / 3);
2031                                 opj_event_msg(p_manager, EVT_WARNING, "- setting Csiz to %d => HYPOTHESIS!!!\n",
2032                                         l_image->numcomps);
2033                         }
2034                 }
2035         */
2036
2037                 /* update components number in the jpwl_exp_comps filed */
2038                 l_cp->exp_comps = l_image->numcomps;
2039         }
2040 #endif /* USE_JPWL */
2041
2042         /* Allocate the resulting image components */
2043         l_image->comps = (opj_image_comp_t*) opj_calloc(l_image->numcomps, sizeof(opj_image_comp_t));
2044         if (l_image->comps == 00){
2045                 l_image->numcomps = 0;
2046                 opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to take in charge SIZ marker\n");
2047                 return OPJ_FALSE;
2048         }
2049
2050         l_img_comp = l_image->comps;
2051
2052         /* Read the component information */
2053         for (i = 0; i < l_image->numcomps; ++i){
2054                 OPJ_UINT32 tmp;
2055                 opj_read_bytes(p_header_data,&tmp,1);   /* Ssiz_i */
2056                 ++p_header_data;
2057                 l_img_comp->prec = (tmp & 0x7f) + 1;
2058                 l_img_comp->sgnd = tmp >> 7;
2059                 opj_read_bytes(p_header_data,&tmp,1);   /* XRsiz_i */
2060                 ++p_header_data;
2061                 l_img_comp->dx = (OPJ_UINT32)tmp; /* should be between 1 and 255 */
2062                 opj_read_bytes(p_header_data,&tmp,1);   /* YRsiz_i */
2063                 ++p_header_data;
2064                 l_img_comp->dy = (OPJ_UINT32)tmp; /* should be between 1 and 255 */
2065                 if( l_img_comp->dx < 1 || l_img_comp->dx > 255 ||
2066                     l_img_comp->dy < 1 || l_img_comp->dy > 255 ) {
2067                     opj_event_msg(p_manager, EVT_ERROR,
2068                                   "Invalid values for comp = %d : dx=%u dy=%u\n (should be between 1 and 255 according the JPEG2000 norm)",
2069                                   i, l_img_comp->dx, l_img_comp->dy);
2070                     return OPJ_FALSE;
2071                 }
2072
2073 #ifdef USE_JPWL
2074                 if (l_cp->correct) {
2075                 /* if JPWL is on, we check whether TX errors have damaged
2076                         too much the SIZ parameters, again */
2077                         if (!(l_image->comps[i].dx * l_image->comps[i].dy)) {
2078                                 opj_event_msg(p_manager, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
2079                                         "JPWL: bad XRsiz_%d/YRsiz_%d (%d x %d)\n",
2080                                         i, i, l_image->comps[i].dx, l_image->comps[i].dy);
2081                                 if (!JPWL_ASSUME) {
2082                                         opj_event_msg(p_manager, EVT_ERROR, "JPWL: giving up\n");
2083                                         return OPJ_FALSE;
2084                                 }
2085                                 /* we try to correct */
2086                                 opj_event_msg(p_manager, EVT_WARNING, "- trying to adjust them\n");
2087                                 if (!l_image->comps[i].dx) {
2088                                         l_image->comps[i].dx = 1;
2089                                         opj_event_msg(p_manager, EVT_WARNING, "- setting XRsiz_%d to %d => HYPOTHESIS!!!\n",
2090                                                 i, l_image->comps[i].dx);
2091                                 }
2092                                 if (!l_image->comps[i].dy) {
2093                                         l_image->comps[i].dy = 1;
2094                                         opj_event_msg(p_manager, EVT_WARNING, "- setting YRsiz_%d to %d => HYPOTHESIS!!!\n",
2095                                                 i, l_image->comps[i].dy);
2096                                 }
2097                         }
2098                 }
2099 #endif /* USE_JPWL */
2100                 l_img_comp->resno_decoded = 0;                                                          /* number of resolution decoded */
2101                 l_img_comp->factor = l_cp->m_specific_param.m_dec.m_reduce; /* reducing factor per component */
2102                 ++l_img_comp;
2103         }
2104
2105         /* Compute the number of tiles */
2106         l_cp->tw = (OPJ_UINT32)opj_int_ceildiv((OPJ_INT32)(l_image->x1 - l_cp->tx0), (OPJ_INT32)l_cp->tdx);
2107         l_cp->th = (OPJ_UINT32)opj_int_ceildiv((OPJ_INT32)(l_image->y1 - l_cp->ty0), (OPJ_INT32)l_cp->tdy);
2108
2109         /* Check that the number of tiles is valid */
2110         if (l_cp->tw == 0 || l_cp->th == 0 || l_cp->tw > 65535 / l_cp->th) {
2111             opj_event_msg(  p_manager, EVT_ERROR, 
2112                             "Invalid number of tiles : %u x %u (maximum fixed by jpeg2000 norm is 65535 tiles)\n",
2113                             l_cp->tw, l_cp->th);
2114             return OPJ_FALSE;
2115         }
2116         l_nb_tiles = l_cp->tw * l_cp->th;
2117
2118         /* Define the tiles which will be decoded */
2119         if (p_j2k->m_specific_param.m_decoder.m_discard_tiles) {
2120                 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;
2121                 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;
2122                 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);
2123                 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);
2124         }
2125         else {
2126                 p_j2k->m_specific_param.m_decoder.m_start_tile_x = 0;
2127                 p_j2k->m_specific_param.m_decoder.m_start_tile_y = 0;
2128                 p_j2k->m_specific_param.m_decoder.m_end_tile_x = l_cp->tw;
2129                 p_j2k->m_specific_param.m_decoder.m_end_tile_y = l_cp->th;
2130         }
2131
2132 #ifdef USE_JPWL
2133         if (l_cp->correct) {
2134                 /* if JPWL is on, we check whether TX errors have damaged
2135                   too much the SIZ parameters */
2136                 if ((l_cp->tw < 1) || (l_cp->th < 1) || (l_cp->tw > l_cp->max_tiles) || (l_cp->th > l_cp->max_tiles)) {
2137                         opj_event_msg(p_manager, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
2138                                 "JPWL: bad number of tiles (%d x %d)\n",
2139                                 l_cp->tw, l_cp->th);
2140                         if (!JPWL_ASSUME) {
2141                                 opj_event_msg(p_manager, EVT_ERROR, "JPWL: giving up\n");
2142                                 return OPJ_FALSE;
2143                         }
2144                         /* we try to correct */
2145                         opj_event_msg(p_manager, EVT_WARNING, "- trying to adjust them\n");
2146                         if (l_cp->tw < 1) {
2147                                 l_cp->tw= 1;
2148                                 opj_event_msg(p_manager, EVT_WARNING, "- setting %d tiles in x => HYPOTHESIS!!!\n",
2149                                                 l_cp->tw);
2150                         }
2151                         if (l_cp->tw > l_cp->max_tiles) {
2152                                 l_cp->tw= 1;
2153                                 opj_event_msg(p_manager, EVT_WARNING, "- too large x, increase expectance of %d\n"
2154                                         "- setting %d tiles in x => HYPOTHESIS!!!\n",
2155                                         l_cp->max_tiles, l_cp->tw);
2156                         }
2157                         if (l_cp->th < 1) {
2158                                 l_cp->th= 1;
2159                                 opj_event_msg(p_manager, EVT_WARNING, "- setting %d tiles in y => HYPOTHESIS!!!\n",
2160                                                 l_cp->th);
2161                         }
2162                         if (l_cp->th > l_cp->max_tiles) {
2163                                 l_cp->th= 1;
2164                                 opj_event_msg(p_manager, EVT_WARNING, "- too large y, increase expectance of %d to continue\n",
2165                                         "- setting %d tiles in y => HYPOTHESIS!!!\n",
2166                                         l_cp->max_tiles, l_cp->th);
2167                         }
2168                 }
2169         }
2170 #endif /* USE_JPWL */
2171
2172         /* memory allocations */
2173         l_cp->tcps = (opj_tcp_t*) opj_calloc(l_nb_tiles, sizeof(opj_tcp_t));
2174         if (l_cp->tcps == 00) {
2175                 opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to take in charge SIZ marker\n");
2176                 return OPJ_FALSE;
2177         }
2178
2179 #ifdef USE_JPWL
2180         if (l_cp->correct) {
2181                 if (!l_cp->tcps) {
2182                         opj_event_msg(p_manager, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
2183                                 "JPWL: could not alloc tcps field of cp\n");
2184                         if (!JPWL_ASSUME || JPWL_ASSUME) {
2185                                 opj_event_msg(p_manager, EVT_ERROR, "JPWL: giving up\n");
2186                                 return OPJ_FALSE;
2187                         }
2188                 }
2189         }
2190 #endif /* USE_JPWL */
2191
2192         p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps =
2193                         (opj_tccp_t*) opj_calloc(l_image->numcomps, sizeof(opj_tccp_t));
2194         if(p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps  == 00) {
2195                 opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to take in charge SIZ marker\n");
2196                 return OPJ_FALSE;
2197         }
2198
2199         p_j2k->m_specific_param.m_decoder.m_default_tcp->m_mct_records =
2200                         (opj_mct_data_t*)opj_calloc(OPJ_J2K_MCT_DEFAULT_NB_RECORDS ,sizeof(opj_mct_data_t));
2201
2202         if (! p_j2k->m_specific_param.m_decoder.m_default_tcp->m_mct_records) {
2203                 opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to take in charge SIZ marker\n");
2204                 return OPJ_FALSE;
2205         }
2206         p_j2k->m_specific_param.m_decoder.m_default_tcp->m_nb_max_mct_records = OPJ_J2K_MCT_DEFAULT_NB_RECORDS;
2207
2208         p_j2k->m_specific_param.m_decoder.m_default_tcp->m_mcc_records =
2209                         (opj_simple_mcc_decorrelation_data_t*)
2210                         opj_calloc(OPJ_J2K_MCC_DEFAULT_NB_RECORDS, sizeof(opj_simple_mcc_decorrelation_data_t));
2211
2212         if (! p_j2k->m_specific_param.m_decoder.m_default_tcp->m_mcc_records) {
2213                 opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to take in charge SIZ marker\n");
2214                 return OPJ_FALSE;
2215         }
2216         p_j2k->m_specific_param.m_decoder.m_default_tcp->m_nb_max_mcc_records = OPJ_J2K_MCC_DEFAULT_NB_RECORDS;
2217
2218         /* set up default dc level shift */
2219         for (i=0;i<l_image->numcomps;++i) {
2220                 if (! l_image->comps[i].sgnd) {
2221                         p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps[i].m_dc_level_shift = 1 << (l_image->comps[i].prec - 1);
2222                 }
2223         }
2224
2225         l_current_tile_param = l_cp->tcps;
2226         for     (i = 0; i < l_nb_tiles; ++i) {
2227                 l_current_tile_param->tccps = (opj_tccp_t*) opj_calloc(l_image->numcomps, sizeof(opj_tccp_t));
2228                 if (l_current_tile_param->tccps == 00) {
2229                         opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to take in charge SIZ marker\n");
2230                         return OPJ_FALSE;
2231                 }
2232
2233                 ++l_current_tile_param;
2234         }
2235
2236         p_j2k->m_specific_param.m_decoder.m_state =  J2K_STATE_MH; /* FIXME J2K_DEC_STATE_MH; */
2237         opj_image_comp_header_update(l_image,l_cp);
2238
2239         return OPJ_TRUE;
2240 }
2241
2242 OPJ_BOOL opj_j2k_write_com(     opj_j2k_t *p_j2k,
2243                                                         opj_stream_private_t *p_stream,
2244                                                         opj_event_mgr_t * p_manager
2245                             )
2246 {
2247         OPJ_UINT32 l_comment_size;
2248         OPJ_UINT32 l_total_com_size;
2249         const OPJ_CHAR *l_comment;
2250         OPJ_BYTE * l_current_ptr = 00;
2251
2252         /* preconditions */
2253         assert(p_j2k != 00);
2254         assert(p_stream != 00);
2255         assert(p_manager != 00);
2256
2257         l_comment = p_j2k->m_cp.comment;
2258         l_comment_size = (OPJ_UINT32)strlen(l_comment);
2259         l_total_com_size = l_comment_size + 6;
2260
2261         if (l_total_com_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
2262                 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);
2263                 if (! new_header_tile_data) {
2264                         opj_free(p_j2k->m_specific_param.m_encoder.m_header_tile_data);
2265                         p_j2k->m_specific_param.m_encoder.m_header_tile_data = NULL;
2266                         p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = 0;
2267                         opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to write the COM marker\n");
2268                         return OPJ_FALSE;
2269                 }
2270                 p_j2k->m_specific_param.m_encoder.m_header_tile_data = new_header_tile_data;
2271                 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_total_com_size;
2272         }
2273
2274         l_current_ptr = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
2275
2276         opj_write_bytes(l_current_ptr,J2K_MS_COM , 2);  /* COM */
2277         l_current_ptr+=2;
2278
2279         opj_write_bytes(l_current_ptr,l_total_com_size - 2 , 2);        /* L_COM */
2280         l_current_ptr+=2;
2281
2282         opj_write_bytes(l_current_ptr,1 , 2);   /* General use (IS 8859-15:1999 (Latin) values) */
2283         l_current_ptr+=2;
2284
2285         memcpy( l_current_ptr,l_comment,l_comment_size);
2286
2287         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) {
2288                 return OPJ_FALSE;
2289         }
2290
2291         return OPJ_TRUE;
2292 }
2293
2294 /**
2295  * Reads a COM marker (comments)
2296  * @param       p_j2k           the jpeg2000 file codec.
2297  * @param       p_header_data   the data contained in the COM box.
2298  * @param       p_header_size   the size of the data contained in the COM marker.
2299  * @param       p_manager               the user event manager.
2300 */
2301 static OPJ_BOOL opj_j2k_read_com (  opj_j2k_t *p_j2k,
2302                                     OPJ_BYTE * p_header_data,
2303                                     OPJ_UINT32 p_header_size,
2304                                     opj_event_mgr_t * p_manager
2305                                     )
2306 {
2307         /* preconditions */
2308         assert(p_j2k != 00);
2309         assert(p_manager != 00);
2310         assert(p_header_data != 00);
2311   (void)p_header_size;
2312
2313         return OPJ_TRUE;
2314 }
2315
2316 OPJ_BOOL opj_j2k_write_cod(     opj_j2k_t *p_j2k,
2317                                                         opj_stream_private_t *p_stream,
2318                                                         opj_event_mgr_t * p_manager )
2319 {
2320         opj_cp_t *l_cp = 00;
2321         opj_tcp_t *l_tcp = 00;
2322         OPJ_UINT32 l_code_size,l_remaining_size;
2323         OPJ_BYTE * l_current_data = 00;
2324
2325         /* preconditions */
2326         assert(p_j2k != 00);
2327         assert(p_manager != 00);
2328         assert(p_stream != 00);
2329
2330         l_cp = &(p_j2k->m_cp);
2331         l_tcp = &l_cp->tcps[p_j2k->m_current_tile_number];
2332         l_code_size = 9 + opj_j2k_get_SPCod_SPCoc_size(p_j2k,p_j2k->m_current_tile_number,0);
2333         l_remaining_size = l_code_size;
2334
2335         if (l_code_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
2336                 OPJ_BYTE *new_header_tile_data = (OPJ_BYTE *) opj_realloc(p_j2k->m_specific_param.m_encoder.m_header_tile_data, l_code_size);
2337                 if (! new_header_tile_data) {
2338                         opj_free(p_j2k->m_specific_param.m_encoder.m_header_tile_data);
2339                         p_j2k->m_specific_param.m_encoder.m_header_tile_data = NULL;
2340                         p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = 0;
2341                         opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to write COD marker\n");
2342                         return OPJ_FALSE;
2343                 }
2344                 p_j2k->m_specific_param.m_encoder.m_header_tile_data = new_header_tile_data;
2345                 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_code_size;
2346         }
2347
2348         l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
2349
2350         opj_write_bytes(l_current_data,J2K_MS_COD,2);           /* COD */
2351         l_current_data += 2;
2352
2353         opj_write_bytes(l_current_data,l_code_size-2,2);        /* L_COD */
2354         l_current_data += 2;
2355
2356         opj_write_bytes(l_current_data,l_tcp->csty,1);          /* Scod */
2357         ++l_current_data;
2358
2359         opj_write_bytes(l_current_data,l_tcp->prg,1);           /* SGcod (A) */
2360         ++l_current_data;
2361
2362         opj_write_bytes(l_current_data,l_tcp->numlayers,2);     /* SGcod (B) */
2363         l_current_data+=2;
2364
2365         opj_write_bytes(l_current_data,l_tcp->mct,1);           /* SGcod (C) */
2366         ++l_current_data;
2367
2368         l_remaining_size -= 9;
2369
2370         if (! opj_j2k_write_SPCod_SPCoc(p_j2k,p_j2k->m_current_tile_number,0,l_current_data,&l_remaining_size,p_manager)) {
2371                 opj_event_msg(p_manager, EVT_ERROR, "Error writing COD marker\n");
2372                 return OPJ_FALSE;
2373         }
2374
2375         if (l_remaining_size != 0) {
2376                 opj_event_msg(p_manager, EVT_ERROR, "Error writing COD marker\n");
2377                 return OPJ_FALSE;
2378         }
2379
2380         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) {
2381                 return OPJ_FALSE;
2382         }
2383
2384         return OPJ_TRUE;
2385 }
2386
2387 /**
2388  * Reads a COD marker (Coding Styke defaults)
2389  * @param       p_header_data   the data contained in the COD box.
2390  * @param       p_j2k                   the jpeg2000 codec.
2391  * @param       p_header_size   the size of the data contained in the COD marker.
2392  * @param       p_manager               the user event manager.
2393 */
2394 static OPJ_BOOL opj_j2k_read_cod (  opj_j2k_t *p_j2k,
2395                                     OPJ_BYTE * p_header_data,
2396                                     OPJ_UINT32 p_header_size,
2397                                     opj_event_mgr_t * p_manager
2398                                     )
2399 {
2400         /* loop */
2401         OPJ_UINT32 i;
2402         OPJ_UINT32 l_tmp;
2403         opj_cp_t *l_cp = 00;
2404         opj_tcp_t *l_tcp = 00;
2405         opj_image_t *l_image = 00;
2406
2407         /* preconditions */
2408         assert(p_header_data != 00);
2409         assert(p_j2k != 00);
2410         assert(p_manager != 00);
2411
2412         l_image = p_j2k->m_private_image;
2413         l_cp = &(p_j2k->m_cp);
2414
2415         /* If we are in the first tile-part header of the current tile */
2416         l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ?
2417                                 &l_cp->tcps[p_j2k->m_current_tile_number] :
2418                                 p_j2k->m_specific_param.m_decoder.m_default_tcp;
2419
2420         /* Make sure room is sufficient */
2421         if (p_header_size < 5) {
2422                 opj_event_msg(p_manager, EVT_ERROR, "Error reading COD marker\n");
2423                 return OPJ_FALSE;
2424         }
2425
2426         opj_read_bytes(p_header_data,&l_tcp->csty,1);           /* Scod */
2427         ++p_header_data;
2428         /* Make sure we know how to decode this */
2429         if ((l_tcp->csty & ~(OPJ_UINT32)(J2K_CP_CSTY_PRT | J2K_CP_CSTY_SOP | J2K_CP_CSTY_EPH)) != 0U) {
2430                 opj_event_msg(p_manager, EVT_ERROR, "Unknown Scod value in COD marker\n");
2431                 return OPJ_FALSE;
2432         }
2433         opj_read_bytes(p_header_data,&l_tmp,1);                         /* SGcod (A) */
2434         ++p_header_data;
2435         l_tcp->prg = (OPJ_PROG_ORDER) l_tmp;
2436         /* Make sure progression order is valid */
2437         if (l_tcp->prg > OPJ_CPRL ) {
2438                 opj_event_msg(p_manager, EVT_ERROR, "Unknown progression order in COD marker\n");
2439                 l_tcp->prg = OPJ_PROG_UNKNOWN;
2440         }
2441         opj_read_bytes(p_header_data,&l_tcp->numlayers,2);      /* SGcod (B) */
2442         p_header_data+=2;
2443
2444         /* If user didn't set a number layer to decode take the max specify in the codestream. */
2445         if      (l_cp->m_specific_param.m_dec.m_layer) {
2446                 l_tcp->num_layers_to_decode = l_cp->m_specific_param.m_dec.m_layer;
2447         }
2448         else {
2449                 l_tcp->num_layers_to_decode = l_tcp->numlayers;
2450         }
2451
2452         opj_read_bytes(p_header_data,&l_tcp->mct,1);            /* SGcod (C) */
2453         ++p_header_data;
2454
2455         p_header_size -= 5;
2456         for     (i = 0; i < l_image->numcomps; ++i) {
2457                 l_tcp->tccps[i].csty = l_tcp->csty & J2K_CCP_CSTY_PRT;
2458         }
2459
2460         if (! opj_j2k_read_SPCod_SPCoc(p_j2k,0,p_header_data,&p_header_size,p_manager)) {
2461                 opj_event_msg(p_manager, EVT_ERROR, "Error reading COD marker\n");
2462                 return OPJ_FALSE;
2463         }
2464
2465         if (p_header_size != 0) {
2466                 opj_event_msg(p_manager, EVT_ERROR, "Error reading COD marker\n");
2467                 return OPJ_FALSE;
2468         }
2469
2470         /* Apply the coding style to other components of the current tile or the m_default_tcp*/
2471         opj_j2k_copy_tile_component_parameters(p_j2k);
2472
2473         /* Index */
2474 #ifdef WIP_REMOVE_MSD
2475         if (p_j2k->cstr_info) {
2476                 /*opj_codestream_info_t *l_cstr_info = p_j2k->cstr_info;*/
2477                 p_j2k->cstr_info->prog = l_tcp->prg;
2478                 p_j2k->cstr_info->numlayers = l_tcp->numlayers;
2479                 p_j2k->cstr_info->numdecompos = (OPJ_INT32*) opj_malloc(l_image->numcomps * sizeof(OPJ_UINT32));
2480                 for     (i = 0; i < l_image->numcomps; ++i) {
2481                         p_j2k->cstr_info->numdecompos[i] = l_tcp->tccps[i].numresolutions - 1;
2482                 }
2483         }
2484 #endif
2485
2486         return OPJ_TRUE;
2487 }
2488
2489 #if 0
2490 OPJ_BOOL opj_j2k_write_coc( opj_j2k_t *p_j2k,
2491                                                 OPJ_UINT32 p_comp_no,
2492                                                 opj_stream_private_t *p_stream,
2493                                                 opj_event_mgr_t * p_manager )
2494 {
2495         OPJ_UINT32 l_coc_size,l_remaining_size;
2496         OPJ_UINT32 l_comp_room;
2497
2498         /* preconditions */
2499         assert(p_j2k != 00);
2500         assert(p_manager != 00);
2501         assert(p_stream != 00);
2502
2503         l_comp_room = (p_j2k->m_private_image->numcomps <= 256) ? 1 : 2;
2504
2505         l_coc_size = 5 + l_comp_room + opj_j2k_get_SPCod_SPCoc_size(p_j2k,p_j2k->m_current_tile_number,p_comp_no);
2506
2507         if (l_coc_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
2508                 OPJ_BYTE *new_header_tile_data;
2509                 /*p_j2k->m_specific_param.m_encoder.m_header_tile_data
2510                         = (OPJ_BYTE*)opj_realloc(
2511                                 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
2512                                 l_coc_size);*/
2513
2514                 new_header_tile_data = (OPJ_BYTE *) opj_realloc(p_j2k->m_specific_param.m_encoder.m_header_tile_data, l_coc_size);
2515                 if (! new_header_tile_data) {
2516                         opj_free(p_j2k->m_specific_param.m_encoder.m_header_tile_data);
2517                         p_j2k->m_specific_param.m_encoder.m_header_tile_data = NULL;
2518                         p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = 0;
2519                         opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to write COC marker\n");
2520                         return OPJ_FALSE;
2521                 }
2522                 p_j2k->m_specific_param.m_encoder.m_header_tile_data = new_header_tile_data;
2523                 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_coc_size;
2524         }
2525
2526         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);
2527
2528         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) {
2529                 return OPJ_FALSE;
2530         }
2531
2532         return OPJ_TRUE;
2533 }
2534 #endif
2535
2536 #if 0
2537 void opj_j2k_write_coc_in_memory(   opj_j2k_t *p_j2k,
2538                                                 OPJ_UINT32 p_comp_no,
2539                                                 OPJ_BYTE * p_data,
2540                                                 OPJ_UINT32 * p_data_written,
2541                                                 opj_event_mgr_t * p_manager
2542                                     )
2543 {
2544         opj_cp_t *l_cp = 00;
2545         opj_tcp_t *l_tcp = 00;
2546         OPJ_UINT32 l_coc_size,l_remaining_size;
2547         OPJ_BYTE * l_current_data = 00;
2548         opj_image_t *l_image = 00;
2549         OPJ_UINT32 l_comp_room;
2550
2551         /* preconditions */
2552         assert(p_j2k != 00);
2553         assert(p_manager != 00);
2554
2555         l_cp = &(p_j2k->m_cp);
2556         l_tcp = &l_cp->tcps[p_j2k->m_current_tile_number];
2557         l_image = p_j2k->m_private_image;
2558         l_comp_room = (l_image->numcomps <= 256) ? 1 : 2;
2559
2560         l_coc_size = 5 + l_comp_room + opj_j2k_get_SPCod_SPCoc_size(p_j2k,p_j2k->m_current_tile_number,p_comp_no);
2561         l_remaining_size = l_coc_size;
2562
2563         l_current_data = p_data;
2564
2565         opj_write_bytes(l_current_data,J2K_MS_COC,2);                           /* COC */
2566         l_current_data += 2;
2567
2568         opj_write_bytes(l_current_data,l_coc_size-2,2);                         /* L_COC */
2569         l_current_data += 2;
2570
2571         opj_write_bytes(l_current_data,p_comp_no, l_comp_room);         /* Ccoc */
2572         l_current_data+=l_comp_room;
2573
2574         opj_write_bytes(l_current_data, l_tcp->tccps[p_comp_no].csty, 1);               /* Scoc */
2575         ++l_current_data;
2576
2577         l_remaining_size -= (5 + l_comp_room);
2578         opj_j2k_write_SPCod_SPCoc(p_j2k,p_j2k->m_current_tile_number,0,l_current_data,&l_remaining_size,p_manager);
2579         * p_data_written = l_coc_size;
2580 }
2581 #endif
2582
2583 OPJ_UINT32 opj_j2k_get_max_coc_size(opj_j2k_t *p_j2k)
2584 {
2585         OPJ_UINT32 i,j;
2586         OPJ_UINT32 l_nb_comp;
2587         OPJ_UINT32 l_nb_tiles;
2588         OPJ_UINT32 l_max = 0;
2589
2590         /* preconditions */
2591
2592         l_nb_tiles = p_j2k->m_cp.tw * p_j2k->m_cp.th ;
2593         l_nb_comp = p_j2k->m_private_image->numcomps;
2594
2595         for (i=0;i<l_nb_tiles;++i) {
2596                 for (j=0;j<l_nb_comp;++j) {
2597                         l_max = opj_uint_max(l_max,opj_j2k_get_SPCod_SPCoc_size(p_j2k,i,j));
2598                 }
2599         }
2600
2601         return 6 + l_max;
2602 }
2603
2604 /**
2605  * Reads a COC marker (Coding Style Component)
2606  * @param       p_header_data   the data contained in the COC box.
2607  * @param       p_j2k                   the jpeg2000 codec.
2608  * @param       p_header_size   the size of the data contained in the COC marker.
2609  * @param       p_manager               the user event manager.
2610 */
2611 static OPJ_BOOL opj_j2k_read_coc (  opj_j2k_t *p_j2k,
2612                                     OPJ_BYTE * p_header_data,
2613                                     OPJ_UINT32 p_header_size,
2614                                     opj_event_mgr_t * p_manager
2615                                     )
2616 {
2617         opj_cp_t *l_cp = NULL;
2618         opj_tcp_t *l_tcp = NULL;
2619         opj_image_t *l_image = NULL;
2620         OPJ_UINT32 l_comp_room;
2621         OPJ_UINT32 l_comp_no;
2622
2623         /* preconditions */
2624         assert(p_header_data != 00);
2625         assert(p_j2k != 00);
2626         assert(p_manager != 00);
2627
2628         l_cp = &(p_j2k->m_cp);
2629         l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH ) ? /*FIXME J2K_DEC_STATE_TPH*/
2630                                 &l_cp->tcps[p_j2k->m_current_tile_number] :
2631                                 p_j2k->m_specific_param.m_decoder.m_default_tcp;
2632         l_image = p_j2k->m_private_image;
2633
2634         l_comp_room = l_image->numcomps <= 256 ? 1 : 2;
2635
2636         /* make sure room is sufficient*/
2637         if (p_header_size < l_comp_room + 1) {
2638                 opj_event_msg(p_manager, EVT_ERROR, "Error reading COC marker\n");
2639                 return OPJ_FALSE;
2640         }
2641         p_header_size -= l_comp_room + 1;
2642
2643         opj_read_bytes(p_header_data,&l_comp_no,l_comp_room);                   /* Ccoc */
2644         p_header_data += l_comp_room;
2645         if (l_comp_no >= l_image->numcomps) {
2646                 opj_event_msg(p_manager, EVT_ERROR, "Error reading COC marker (bad number of components)\n");
2647                 return OPJ_FALSE;
2648         }
2649
2650         opj_read_bytes(p_header_data,&l_tcp->tccps[l_comp_no].csty,1);                  /* Scoc */
2651         ++p_header_data ;
2652
2653         if (! opj_j2k_read_SPCod_SPCoc(p_j2k,l_comp_no,p_header_data,&p_header_size,p_manager)) {
2654                 opj_event_msg(p_manager, EVT_ERROR, "Error reading COC marker\n");
2655                 return OPJ_FALSE;
2656         }
2657
2658         if (p_header_size != 0) {
2659                 opj_event_msg(p_manager, EVT_ERROR, "Error reading COC marker\n");
2660                 return OPJ_FALSE;
2661         }
2662         return OPJ_TRUE;
2663 }
2664
2665 OPJ_BOOL opj_j2k_write_qcd(     opj_j2k_t *p_j2k,
2666                                                         opj_stream_private_t *p_stream,
2667                                                         opj_event_mgr_t * p_manager
2668                             )
2669 {
2670         OPJ_UINT32 l_qcd_size,l_remaining_size;
2671         OPJ_BYTE * l_current_data = 00;
2672
2673         /* preconditions */
2674         assert(p_j2k != 00);
2675         assert(p_manager != 00);
2676         assert(p_stream != 00);
2677
2678         l_qcd_size = 4 + opj_j2k_get_SQcd_SQcc_size(p_j2k,p_j2k->m_current_tile_number,0);
2679         l_remaining_size = l_qcd_size;
2680
2681         if (l_qcd_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
2682                 OPJ_BYTE *new_header_tile_data = (OPJ_BYTE *) opj_realloc(p_j2k->m_specific_param.m_encoder.m_header_tile_data, l_qcd_size);
2683                 if (! new_header_tile_data) {
2684                         opj_free(p_j2k->m_specific_param.m_encoder.m_header_tile_data);
2685                         p_j2k->m_specific_param.m_encoder.m_header_tile_data = NULL;
2686                         p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = 0;
2687                         opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to write QCD marker\n");
2688                         return OPJ_FALSE;
2689                 }
2690                 p_j2k->m_specific_param.m_encoder.m_header_tile_data = new_header_tile_data;
2691                 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_qcd_size;
2692         }
2693
2694         l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
2695
2696         opj_write_bytes(l_current_data,J2K_MS_QCD,2);           /* QCD */
2697         l_current_data += 2;
2698
2699         opj_write_bytes(l_current_data,l_qcd_size-2,2);         /* L_QCD */
2700         l_current_data += 2;
2701
2702         l_remaining_size -= 4;
2703
2704         if (! opj_j2k_write_SQcd_SQcc(p_j2k,p_j2k->m_current_tile_number,0,l_current_data,&l_remaining_size,p_manager)) {
2705                 opj_event_msg(p_manager, EVT_ERROR, "Error writing QCD marker\n");
2706                 return OPJ_FALSE;
2707         }
2708
2709         if (l_remaining_size != 0) {
2710                 opj_event_msg(p_manager, EVT_ERROR, "Error writing QCD marker\n");
2711                 return OPJ_FALSE;
2712         }
2713
2714         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) {
2715                 return OPJ_FALSE;
2716         }
2717
2718         return OPJ_TRUE;
2719 }
2720
2721 /**
2722  * Reads a QCD marker (Quantization defaults)
2723  * @param       p_header_data   the data contained in the QCD box.
2724  * @param       p_j2k                   the jpeg2000 codec.
2725  * @param       p_header_size   the size of the data contained in the QCD marker.
2726  * @param       p_manager               the user event manager.
2727 */
2728 static OPJ_BOOL opj_j2k_read_qcd (  opj_j2k_t *p_j2k,
2729                                     OPJ_BYTE * p_header_data,
2730                                     OPJ_UINT32 p_header_size,
2731                                     opj_event_mgr_t * p_manager
2732                                     )
2733 {
2734         /* preconditions */
2735         assert(p_header_data != 00);
2736         assert(p_j2k != 00);
2737         assert(p_manager != 00);
2738
2739         if (! opj_j2k_read_SQcd_SQcc(p_j2k,0,p_header_data,&p_header_size,p_manager)) {
2740                 opj_event_msg(p_manager, EVT_ERROR, "Error reading QCD marker\n");
2741                 return OPJ_FALSE;
2742         }
2743
2744         if (p_header_size != 0) {
2745                 opj_event_msg(p_manager, EVT_ERROR, "Error reading QCD marker\n");
2746                 return OPJ_FALSE;
2747         }
2748
2749         /* Apply the quantization parameters to other components of the current tile or the m_default_tcp */
2750         opj_j2k_copy_tile_quantization_parameters(p_j2k);
2751
2752         return OPJ_TRUE;
2753 }
2754
2755 #if 0
2756 OPJ_BOOL opj_j2k_write_qcc(     opj_j2k_t *p_j2k,
2757                                                 OPJ_UINT32 p_comp_no,
2758                                                 opj_stream_private_t *p_stream,
2759                                                 opj_event_mgr_t * p_manager
2760                             )
2761 {
2762         OPJ_UINT32 l_qcc_size,l_remaining_size;
2763
2764         /* preconditions */
2765         assert(p_j2k != 00);
2766         assert(p_manager != 00);
2767         assert(p_stream != 00);
2768
2769         l_qcc_size = 5 + opj_j2k_get_SQcd_SQcc_size(p_j2k,p_j2k->m_current_tile_number,p_comp_no);
2770         l_qcc_size += p_j2k->m_private_image->numcomps <= 256 ? 0:1;
2771         l_remaining_size = l_qcc_size;
2772
2773         if (l_qcc_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
2774                 OPJ_BYTE *new_header_tile_data = (OPJ_BYTE *) opj_realloc(p_j2k->m_specific_param.m_encoder.m_header_tile_data, l_qcc_size);
2775                 if (! new_header_tile_data) {
2776                         opj_free(p_j2k->m_specific_param.m_encoder.m_header_tile_data);
2777                         p_j2k->m_specific_param.m_encoder.m_header_tile_data = NULL;
2778                         p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = 0;
2779                         opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to write QCC marker\n");
2780                         return OPJ_FALSE;
2781                 }
2782                 p_j2k->m_specific_param.m_encoder.m_header_tile_data = new_header_tile_data;
2783                 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_qcc_size;
2784         }
2785
2786         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);
2787
2788         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) {
2789                 return OPJ_FALSE;
2790         }
2791
2792         return OPJ_TRUE;
2793 }
2794 #endif
2795
2796 #if 0
2797 void opj_j2k_write_qcc_in_memory(   opj_j2k_t *p_j2k,
2798                                                                 OPJ_UINT32 p_comp_no,
2799                                                                 OPJ_BYTE * p_data,
2800                                                                 OPJ_UINT32 * p_data_written,
2801                                                                 opj_event_mgr_t * p_manager
2802                                     )
2803 {
2804         OPJ_UINT32 l_qcc_size,l_remaining_size;
2805         OPJ_BYTE * l_current_data = 00;
2806
2807         /* preconditions */
2808         assert(p_j2k != 00);
2809         assert(p_manager != 00);
2810
2811         l_qcc_size = 6 + opj_j2k_get_SQcd_SQcc_size(p_j2k,p_j2k->m_current_tile_number,p_comp_no);
2812         l_remaining_size = l_qcc_size;
2813
2814         l_current_data = p_data;
2815
2816         opj_write_bytes(l_current_data,J2K_MS_QCC,2);           /* QCC */
2817         l_current_data += 2;
2818
2819         if (p_j2k->m_private_image->numcomps <= 256) {
2820                 --l_qcc_size;
2821
2822                 opj_write_bytes(l_current_data,l_qcc_size-2,2);         /* L_QCC */
2823                 l_current_data += 2;
2824
2825                 opj_write_bytes(l_current_data, p_comp_no, 1);  /* Cqcc */
2826                 ++l_current_data;
2827
2828                 /* in the case only one byte is sufficient the last byte allocated is useless -> still do -6 for available */
2829                 l_remaining_size -= 6;
2830         }
2831         else {
2832                 opj_write_bytes(l_current_data,l_qcc_size-2,2);         /* L_QCC */
2833                 l_current_data += 2;
2834
2835                 opj_write_bytes(l_current_data, p_comp_no, 2);  /* Cqcc */
2836                 l_current_data+=2;
2837
2838                 l_remaining_size -= 6;
2839         }
2840
2841         opj_j2k_write_SQcd_SQcc(p_j2k,p_j2k->m_current_tile_number,p_comp_no,l_current_data,&l_remaining_size,p_manager);
2842
2843         *p_data_written = l_qcc_size;
2844 }
2845 #endif
2846
2847 OPJ_UINT32 opj_j2k_get_max_qcc_size (opj_j2k_t *p_j2k)
2848 {
2849         return opj_j2k_get_max_coc_size(p_j2k);
2850 }
2851
2852 /**
2853  * Reads a QCC marker (Quantization component)
2854  * @param       p_header_data   the data contained in the QCC box.
2855  * @param       p_j2k                   the jpeg2000 codec.
2856  * @param       p_header_size   the size of the data contained in the QCC marker.
2857  * @param       p_manager               the user event manager.
2858 */
2859 static OPJ_BOOL opj_j2k_read_qcc(   opj_j2k_t *p_j2k,
2860                                     OPJ_BYTE * p_header_data,
2861                                     OPJ_UINT32 p_header_size,
2862                                     opj_event_mgr_t * p_manager
2863                                     )
2864 {
2865         OPJ_UINT32 l_num_comp,l_comp_no;
2866
2867         /* preconditions */
2868         assert(p_header_data != 00);
2869         assert(p_j2k != 00);
2870         assert(p_manager != 00);
2871
2872         l_num_comp = p_j2k->m_private_image->numcomps;
2873
2874         if (l_num_comp <= 256) {
2875                 if (p_header_size < 1) {
2876                         opj_event_msg(p_manager, EVT_ERROR, "Error reading QCC marker\n");
2877                         return OPJ_FALSE;
2878                 }
2879                 opj_read_bytes(p_header_data,&l_comp_no,1);
2880                 ++p_header_data;
2881                 --p_header_size;
2882         }
2883         else {
2884                 if (p_header_size < 2) {
2885                         opj_event_msg(p_manager, EVT_ERROR, "Error reading QCC marker\n");
2886                         return OPJ_FALSE;
2887                 }
2888                 opj_read_bytes(p_header_data,&l_comp_no,2);
2889                 p_header_data+=2;
2890                 p_header_size-=2;
2891         }
2892
2893 #ifdef USE_JPWL
2894         if (p_j2k->m_cp.correct) {
2895
2896                 static OPJ_UINT32 backup_compno = 0;
2897
2898                 /* compno is negative or larger than the number of components!!! */
2899                 if (/*(l_comp_no < 0) ||*/ (l_comp_no >= l_num_comp)) {
2900                         opj_event_msg(p_manager, EVT_ERROR,
2901                                 "JPWL: bad component number in QCC (%d out of a maximum of %d)\n",
2902                                 l_comp_no, l_num_comp);
2903                         if (!JPWL_ASSUME) {
2904                                 opj_event_msg(p_manager, EVT_ERROR, "JPWL: giving up\n");
2905                                 return OPJ_FALSE;
2906                         }
2907                         /* we try to correct */
2908                         l_comp_no = backup_compno % l_num_comp;
2909                         opj_event_msg(p_manager, EVT_WARNING, "- trying to adjust this\n"
2910                                 "- setting component number to %d\n",
2911                                 l_comp_no);
2912                 }
2913
2914                 /* keep your private count of tiles */
2915                 backup_compno++;
2916         };
2917 #endif /* USE_JPWL */
2918
2919         if (l_comp_no >= p_j2k->m_private_image->numcomps) {
2920                 opj_event_msg(p_manager, EVT_ERROR,
2921                               "Invalid component number: %d, regarding the number of components %d\n",
2922                               l_comp_no, p_j2k->m_private_image->numcomps);
2923                 return OPJ_FALSE;
2924         }
2925
2926         if (! opj_j2k_read_SQcd_SQcc(p_j2k,l_comp_no,p_header_data,&p_header_size,p_manager)) {
2927                 opj_event_msg(p_manager, EVT_ERROR, "Error reading QCC marker\n");
2928                 return OPJ_FALSE;
2929         }
2930
2931         if (p_header_size != 0) {
2932                 opj_event_msg(p_manager, EVT_ERROR, "Error reading QCC marker\n");
2933                 return OPJ_FALSE;
2934         }
2935
2936         return OPJ_TRUE;
2937 }
2938
2939 OPJ_BOOL opj_j2k_write_poc(     opj_j2k_t *p_j2k,
2940                                                         opj_stream_private_t *p_stream,
2941                                                         opj_event_mgr_t * p_manager
2942                             )
2943 {
2944         OPJ_UINT32 l_nb_comp;
2945         OPJ_UINT32 l_nb_poc;
2946         OPJ_UINT32 l_poc_size;
2947         OPJ_UINT32 l_written_size = 0;
2948         opj_tcp_t *l_tcp = 00;
2949         OPJ_UINT32 l_poc_room;
2950
2951         /* preconditions */
2952         assert(p_j2k != 00);
2953         assert(p_manager != 00);
2954         assert(p_stream != 00);
2955
2956         l_tcp = &p_j2k->m_cp.tcps[p_j2k->m_current_tile_number];
2957         l_nb_comp = p_j2k->m_private_image->numcomps;
2958         l_nb_poc = 1 + l_tcp->numpocs;
2959
2960         if (l_nb_comp <= 256) {
2961                 l_poc_room = 1;
2962         }
2963         else {
2964                 l_poc_room = 2;
2965         }
2966         l_poc_size = 4 + (5 + 2 * l_poc_room) * l_nb_poc;
2967
2968         if (l_poc_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
2969                 OPJ_BYTE *new_header_tile_data = (OPJ_BYTE *) opj_realloc(p_j2k->m_specific_param.m_encoder.m_header_tile_data, l_poc_size);
2970                 if (! new_header_tile_data) {
2971                         opj_free(p_j2k->m_specific_param.m_encoder.m_header_tile_data);
2972                         p_j2k->m_specific_param.m_encoder.m_header_tile_data = NULL;
2973                         p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = 0;
2974                         opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to write POC marker\n");
2975                         return OPJ_FALSE;
2976                 }
2977                 p_j2k->m_specific_param.m_encoder.m_header_tile_data = new_header_tile_data;
2978                 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_poc_size;
2979         }
2980
2981         opj_j2k_write_poc_in_memory(p_j2k,p_j2k->m_specific_param.m_encoder.m_header_tile_data,&l_written_size,p_manager);
2982
2983         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) {
2984                 return OPJ_FALSE;
2985         }
2986
2987         return OPJ_TRUE;
2988 }
2989
2990 void opj_j2k_write_poc_in_memory(   opj_j2k_t *p_j2k,
2991                                                                 OPJ_BYTE * p_data,
2992                                                                 OPJ_UINT32 * p_data_written,
2993                                                                 opj_event_mgr_t * p_manager
2994                                     )
2995 {
2996         OPJ_UINT32 i;
2997         OPJ_BYTE * l_current_data = 00;
2998         OPJ_UINT32 l_nb_comp;
2999         OPJ_UINT32 l_nb_poc;
3000         OPJ_UINT32 l_poc_size;
3001         opj_image_t *l_image = 00;
3002         opj_tcp_t *l_tcp = 00;
3003         opj_tccp_t *l_tccp = 00;
3004         opj_poc_t *l_current_poc = 00;
3005         OPJ_UINT32 l_poc_room;
3006
3007         /* preconditions */
3008         assert(p_j2k != 00);
3009         assert(p_manager != 00);
3010
3011         l_tcp = &p_j2k->m_cp.tcps[p_j2k->m_current_tile_number];
3012         l_tccp = &l_tcp->tccps[0];
3013         l_image = p_j2k->m_private_image;
3014         l_nb_comp = l_image->numcomps;
3015         l_nb_poc = 1 + l_tcp->numpocs;
3016
3017         if (l_nb_comp <= 256) {
3018                 l_poc_room = 1;
3019         }
3020         else {
3021                 l_poc_room = 2;
3022         }
3023
3024         l_poc_size = 4 + (5 + 2 * l_poc_room) * l_nb_poc;
3025
3026         l_current_data = p_data;
3027
3028         opj_write_bytes(l_current_data,J2K_MS_POC,2);                                   /* POC  */
3029         l_current_data += 2;
3030
3031         opj_write_bytes(l_current_data,l_poc_size-2,2);                                 /* Lpoc */
3032         l_current_data += 2;
3033
3034         l_current_poc =  l_tcp->pocs;
3035         for (i = 0; i < l_nb_poc; ++i) {
3036                 opj_write_bytes(l_current_data,l_current_poc->resno0,1);                                /* RSpoc_i */
3037                 ++l_current_data;
3038
3039                 opj_write_bytes(l_current_data,l_current_poc->compno0,l_poc_room);              /* CSpoc_i */
3040                 l_current_data+=l_poc_room;
3041
3042                 opj_write_bytes(l_current_data,l_current_poc->layno1,2);                                /* LYEpoc_i */
3043                 l_current_data+=2;
3044
3045                 opj_write_bytes(l_current_data,l_current_poc->resno1,1);                                /* REpoc_i */
3046                 ++l_current_data;
3047
3048                 opj_write_bytes(l_current_data,l_current_poc->compno1,l_poc_room);              /* CEpoc_i */
3049                 l_current_data+=l_poc_room;
3050
3051                 opj_write_bytes(l_current_data,l_current_poc->prg,1);                                   /* Ppoc_i */
3052                 ++l_current_data;
3053
3054                 /* change the value of the max layer according to the actual number of layers in the file, components and resolutions*/
3055                 l_current_poc->layno1 = (OPJ_UINT32)opj_int_min((OPJ_INT32)l_current_poc->layno1, (OPJ_INT32)l_tcp->numlayers);
3056                 l_current_poc->resno1 = (OPJ_UINT32)opj_int_min((OPJ_INT32)l_current_poc->resno1, (OPJ_INT32)l_tccp->numresolutions);
3057                 l_current_poc->compno1 = (OPJ_UINT32)opj_int_min((OPJ_INT32)l_current_poc->compno1, (OPJ_INT32)l_nb_comp);
3058
3059                 ++l_current_poc;
3060         }
3061
3062         *p_data_written = l_poc_size;
3063 }
3064
3065 OPJ_UINT32 opj_j2k_get_max_poc_size(opj_j2k_t *p_j2k)
3066 {
3067         opj_tcp_t * l_tcp = 00;
3068         OPJ_UINT32 l_nb_tiles = 0;
3069         OPJ_UINT32 l_max_poc = 0;
3070         OPJ_UINT32 i;
3071
3072         l_tcp = p_j2k->m_cp.tcps;
3073         l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw;
3074
3075         for (i=0;i<l_nb_tiles;++i) {
3076                 l_max_poc = opj_uint_max(l_max_poc,l_tcp->numpocs);
3077                 ++l_tcp;
3078         }
3079
3080         ++l_max_poc;
3081
3082         return 4 + 9 * l_max_poc;
3083 }
3084
3085 OPJ_UINT32 opj_j2k_get_max_toc_size (opj_j2k_t *p_j2k)
3086 {
3087         OPJ_UINT32 i;
3088         OPJ_UINT32 l_nb_tiles;
3089         OPJ_UINT32 l_max = 0;
3090         opj_tcp_t * l_tcp = 00;
3091
3092         l_tcp = p_j2k->m_cp.tcps;
3093         l_nb_tiles = p_j2k->m_cp.tw * p_j2k->m_cp.th ;
3094
3095         for (i=0;i<l_nb_tiles;++i) {
3096                 l_max = opj_uint_max(l_max,l_tcp->m_nb_tile_parts);
3097
3098                 ++l_tcp;
3099         }
3100
3101         return 12 * l_max;
3102 }
3103
3104 OPJ_UINT32 opj_j2k_get_specific_header_sizes(opj_j2k_t *p_j2k)
3105 {
3106         OPJ_UINT32 l_nb_bytes = 0;
3107         OPJ_UINT32 l_nb_comps;
3108         OPJ_UINT32 l_coc_bytes,l_qcc_bytes;
3109
3110         l_nb_comps = p_j2k->m_private_image->numcomps - 1;
3111         l_nb_bytes += opj_j2k_get_max_toc_size(p_j2k);
3112
3113         if (!(OPJ_IS_CINEMA(p_j2k->m_cp.rsiz))) {
3114                 l_coc_bytes = opj_j2k_get_max_coc_size(p_j2k);
3115                 l_nb_bytes += l_nb_comps * l_coc_bytes;
3116
3117                 l_qcc_bytes = opj_j2k_get_max_qcc_size(p_j2k);
3118                 l_nb_bytes += l_nb_comps * l_qcc_bytes;
3119         }
3120
3121         l_nb_bytes += opj_j2k_get_max_poc_size(p_j2k);
3122
3123         /*** DEVELOPER CORNER, Add room for your headers ***/
3124
3125         return l_nb_bytes;
3126 }
3127
3128 /**
3129  * Reads a POC marker (Progression Order Change)
3130  *
3131  * @param       p_header_data   the data contained in the POC box.
3132  * @param       p_j2k                   the jpeg2000 codec.
3133  * @param       p_header_size   the size of the data contained in the POC marker.
3134  * @param       p_manager               the user event manager.
3135 */
3136 static OPJ_BOOL opj_j2k_read_poc (  opj_j2k_t *p_j2k,
3137                                     OPJ_BYTE * p_header_data,
3138                                     OPJ_UINT32 p_header_size,
3139                                     opj_event_mgr_t * p_manager
3140                                     )
3141 {
3142         OPJ_UINT32 i, l_nb_comp, l_tmp;
3143         opj_image_t * l_image = 00;
3144         OPJ_UINT32 l_old_poc_nb, l_current_poc_nb, l_current_poc_remaining;
3145         OPJ_UINT32 l_chunk_size, l_comp_room;
3146
3147         opj_cp_t *l_cp = 00;
3148         opj_tcp_t *l_tcp = 00;
3149         opj_poc_t *l_current_poc = 00;
3150
3151         /* preconditions */
3152         assert(p_header_data != 00);
3153         assert(p_j2k != 00);
3154         assert(p_manager != 00);
3155
3156         l_image = p_j2k->m_private_image;
3157         l_nb_comp = l_image->numcomps;
3158         if (l_nb_comp <= 256) {
3159                 l_comp_room = 1;
3160         }
3161         else {
3162                 l_comp_room = 2;
3163         }
3164         l_chunk_size = 5 + 2 * l_comp_room;
3165         l_current_poc_nb = p_header_size / l_chunk_size;
3166         l_current_poc_remaining = p_header_size % l_chunk_size;
3167
3168         if ((l_current_poc_nb <= 0) || (l_current_poc_remaining != 0)) {
3169                 opj_event_msg(p_manager, EVT_ERROR, "Error reading POC marker\n");
3170                 return OPJ_FALSE;
3171         }
3172
3173         l_cp = &(p_j2k->m_cp);
3174         l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ?
3175                                 &l_cp->tcps[p_j2k->m_current_tile_number] :
3176                                 p_j2k->m_specific_param.m_decoder.m_default_tcp;
3177         l_old_poc_nb = l_tcp->POC ? l_tcp->numpocs + 1 : 0;
3178         l_current_poc_nb += l_old_poc_nb;
3179
3180         if(l_current_poc_nb >= 32)
3181           {
3182           opj_event_msg(p_manager, EVT_ERROR, "Too many POCs %d\n", l_current_poc_nb);
3183           return OPJ_FALSE;
3184           }
3185         assert(l_current_poc_nb < 32);
3186
3187         /* now poc is in use.*/
3188         l_tcp->POC = 1;
3189
3190         l_current_poc = &l_tcp->pocs[l_old_poc_nb];
3191         for     (i = l_old_poc_nb; i < l_current_poc_nb; ++i) {
3192                 opj_read_bytes(p_header_data,&(l_current_poc->resno0),1);                               /* RSpoc_i */
3193                 ++p_header_data;
3194                 opj_read_bytes(p_header_data,&(l_current_poc->compno0),l_comp_room);    /* CSpoc_i */
3195                 p_header_data+=l_comp_room;
3196                 opj_read_bytes(p_header_data,&(l_current_poc->layno1),2);                               /* LYEpoc_i */
3197                 /* make sure layer end is in acceptable bounds */
3198                 l_current_poc->layno1 = opj_uint_min(l_current_poc->layno1, l_tcp->numlayers);
3199                 p_header_data+=2;
3200                 opj_read_bytes(p_header_data,&(l_current_poc->resno1),1);                               /* REpoc_i */
3201                 ++p_header_data;
3202                 opj_read_bytes(p_header_data,&(l_current_poc->compno1),l_comp_room);    /* CEpoc_i */
3203                 p_header_data+=l_comp_room;
3204                 opj_read_bytes(p_header_data,&l_tmp,1);                                                                 /* Ppoc_i */
3205                 ++p_header_data;
3206                 l_current_poc->prg = (OPJ_PROG_ORDER) l_tmp;
3207                 /* make sure comp is in acceptable bounds */
3208                 l_current_poc->compno1 = opj_uint_min(l_current_poc->compno1, l_nb_comp);
3209                 ++l_current_poc;
3210         }
3211
3212         l_tcp->numpocs = l_current_poc_nb - 1;
3213         return OPJ_TRUE;
3214 }
3215
3216 /**
3217  * Reads a CRG marker (Component registration)
3218  *
3219  * @param       p_header_data   the data contained in the TLM box.
3220  * @param       p_j2k                   the jpeg2000 codec.
3221  * @param       p_header_size   the size of the data contained in the TLM marker.
3222  * @param       p_manager               the user event manager.
3223 */
3224 static OPJ_BOOL opj_j2k_read_crg (  opj_j2k_t *p_j2k,
3225                                     OPJ_BYTE * p_header_data,
3226                                     OPJ_UINT32 p_header_size,
3227                                     opj_event_mgr_t * p_manager
3228                                     )
3229 {
3230         OPJ_UINT32 l_nb_comp;
3231         /* preconditions */
3232         assert(p_header_data != 00);
3233         assert(p_j2k != 00);
3234         assert(p_manager != 00);
3235
3236         l_nb_comp = p_j2k->m_private_image->numcomps;
3237
3238         if (p_header_size != l_nb_comp *4) {
3239                 opj_event_msg(p_manager, EVT_ERROR, "Error reading CRG marker\n");
3240                 return OPJ_FALSE;
3241         }
3242         /* Do not care of this at the moment since only local variables are set here */
3243         /*
3244         for
3245                 (i = 0; i < l_nb_comp; ++i)
3246         {
3247                 opj_read_bytes(p_header_data,&l_Xcrg_i,2);                              // Xcrg_i