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