[trunk] add a sentinel in debug code
[openjpeg.git] / src / lib / openjp2 / j2k.c
1 /*
2  * Copyright (c) 2002-2007, Communications and Remote Sensing Laboratory, Universite catholique de Louvain (UCL), Belgium
3  * Copyright (c) 2002-2007, Professor Benoit Macq
4  * Copyright (c) 2001-2003, David Janssens
5  * Copyright (c) 2002-2003, Yannick Verschueren
6  * Copyright (c) 2003-2007, Francois-Olivier Devaux and Antonin Descampe
7  * Copyright (c) 2005, Herve Drolon, FreeImage Team
8  * Copyright (c) 2008, Jerome Fimes, Communications & Systemes <jerome.fimes@c-s.fr>
9  * Copyright (c) 2006-2007, Parvatha Elangovan
10  * Copyright (c) 2010-2011, Kaori Hagihara
11  * Copyright (c) 2011-2012, Centre National d'Etudes Spatiales (CNES), France 
12  * Copyright (c) 2012, CS Systemes d'Information, France
13  * All rights reserved.
14  *
15  * Redistribution and use in source and binary forms, with or without
16  * modification, are permitted provided that the following conditions
17  * are met:
18  * 1. Redistributions of source code must retain the above copyright
19  *    notice, this list of conditions and the following disclaimer.
20  * 2. Redistributions in binary form must reproduce the above copyright
21  *    notice, this list of conditions and the following disclaimer in the
22  *    documentation and/or other materials provided with the distribution.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
25  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27  * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
28  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
29  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
30  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
31  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
32  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
33  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34  * POSSIBILITY OF SUCH DAMAGE.
35  */
36
37 #include "opj_includes.h"
38
39 #define CINEMA_24_CS 1302083    /*Codestream length for 24fps*/
40 #define CINEMA_48_CS 651041             /*Codestream length for 48fps*/
41 #define COMP_24_CS 1041666              /*Maximum size per color component for 2K & 4K @ 24fps*/
42 #define COMP_48_CS 520833               /*Maximum size per color component for 2K @ 48fps*/
43
44 /** @defgroup J2K J2K - JPEG-2000 codestream reader/writer */
45 /*@{*/
46
47 /** @name Local static functions */
48 /*@{*/
49
50 /**
51  * Sets up the procedures to do on reading header. Developpers wanting to extend the library can add their own reading procedures.
52  */
53 static void opj_j2k_setup_header_reading (opj_j2k_t *p_j2k);
54
55 /**
56  * The read header procedure.
57  */
58 static OPJ_BOOL opj_j2k_read_header_procedure(  opj_j2k_t *p_j2k,
59                                                 opj_stream_private_t *p_stream,
60                                                 opj_event_mgr_t * p_manager);
61
62 /**
63  * The default encoding validation procedure without any extension.
64  *
65  * @param       p_j2k                   the jpeg2000 codec to validate.
66  * @param       p_stream                the input stream to validate.
67  * @param       p_manager               the user event manager.
68  *
69  * @return true if the parameters are correct.
70  */
71 static OPJ_BOOL opj_j2k_encoding_validation (   opj_j2k_t * p_j2k,
72                                                 opj_stream_private_t *p_stream,
73                                                 opj_event_mgr_t * p_manager );
74
75 /**
76  * The default decoding validation procedure without any extension.
77  *
78  * @param       p_j2k                   the jpeg2000 codec to validate.
79  * @param       p_stream                                the input stream to validate.
80  * @param       p_manager               the user event manager.
81  *
82  * @return true if the parameters are correct.
83  */
84 static OPJ_BOOL opj_j2k_decoding_validation (   opj_j2k_t * p_j2k,
85                                                 opj_stream_private_t *p_stream,
86                                                 opj_event_mgr_t * p_manager );
87
88 /**
89  * Sets up the validation ,i.e. adds the procedures to lauch to make sure the codec parameters
90  * are valid. Developpers wanting to extend the library can add their own validation procedures.
91  */
92 static void opj_j2k_setup_encoding_validation (opj_j2k_t *p_j2k);
93
94 /**
95  * Sets up the validation ,i.e. adds the procedures to lauch to make sure the codec parameters
96  * are valid. Developpers wanting to extend the library can add their own validation procedures.
97  */
98 static void opj_j2k_setup_decoding_validation (opj_j2k_t *p_j2k);
99
100 /**
101  * Sets up the validation ,i.e. adds the procedures to lauch to make sure the codec parameters
102  * are valid. Developpers wanting to extend the library can add their own validation procedures.
103  */
104 static void opj_j2k_setup_end_compress (opj_j2k_t *p_j2k);
105
106 /**
107  * The mct encoding validation procedure.
108  *
109  * @param       p_j2k                   the jpeg2000 codec to validate.
110  * @param       p_stream                                the input stream to validate.
111  * @param       p_manager               the user event manager.
112  *
113  * @return true if the parameters are correct.
114  */
115 static OPJ_BOOL opj_j2k_mct_validation (opj_j2k_t * p_j2k,
116                                         opj_stream_private_t *p_stream,
117                                         opj_event_mgr_t * p_manager );
118
119 /**
120  * Builds the tcd decoder to use to decode tile.
121  */
122 static OPJ_BOOL opj_j2k_build_decoder ( opj_j2k_t * p_j2k,
123                                         opj_stream_private_t *p_stream,
124                                         opj_event_mgr_t * p_manager );
125 /**
126  * Builds the tcd encoder to use to encode tile.
127  */
128 static OPJ_BOOL opj_j2k_build_encoder ( opj_j2k_t * p_j2k,
129                                         opj_stream_private_t *p_stream,
130                                         opj_event_mgr_t * p_manager );
131
132 /**
133  * Creates a tile-coder decoder.
134  *
135  * @param       p_stream                        the stream to write data to.
136  * @param       p_j2k                           J2K codec.
137  * @param       p_manager                   the user event manager.
138 */
139 static OPJ_BOOL opj_j2k_create_tcd(     opj_j2k_t *p_j2k,
140                                                                     opj_stream_private_t *p_stream,
141                                                                     opj_event_mgr_t * p_manager );
142
143 /**
144  * Excutes the given procedures on the given codec.
145  *
146  * @param       p_procedure_list        the list of procedures to execute
147  * @param       p_j2k                           the jpeg2000 codec to execute the procedures on.
148  * @param       p_stream                        the stream to execute the procedures on.
149  * @param       p_manager                       the user manager.
150  *
151  * @return      true                            if all the procedures were successfully executed.
152  */
153 static OPJ_BOOL opj_j2k_exec (  opj_j2k_t * p_j2k,
154                             opj_procedure_list_t * p_procedure_list,
155                             opj_stream_private_t *p_stream,
156                             opj_event_mgr_t * p_manager);
157
158 /**
159  * Updates the rates of the tcp.
160  *
161  * @param       p_stream                                the stream to write data to.
162  * @param       p_j2k                           J2K codec.
163  * @param       p_manager               the user event manager.
164 */
165 static OPJ_BOOL opj_j2k_update_rates(   opj_j2k_t *p_j2k,
166                                                                             opj_stream_private_t *p_stream,
167                                                                             opj_event_mgr_t * p_manager );
168
169 /**
170  * Copies the decoding tile parameters onto all the tile parameters.
171  * Creates also the tile decoder.
172  */
173 static OPJ_BOOL opj_j2k_copy_default_tcp_and_create_tcd (       opj_j2k_t * p_j2k,
174                                                             opj_stream_private_t *p_stream,
175                                                             opj_event_mgr_t * p_manager );
176
177 /**
178  * Destroys the memory associated with the decoding of headers.
179  */
180 static OPJ_BOOL opj_j2k_destroy_header_memory ( opj_j2k_t * p_j2k,
181                                                 opj_stream_private_t *p_stream,
182                                                 opj_event_mgr_t * p_manager );
183
184 /**
185  * Reads the lookup table containing all the marker, status and action, and returns the handler associated
186  * with the marker value.
187  * @param       p_id            Marker value to look up
188  *
189  * @return      the handler associated with the id.
190 */
191 static const struct opj_dec_memory_marker_handler * opj_j2k_get_marker_handler (OPJ_UINT32 p_id);
192
193 /**
194  * Destroys a tile coding parameter structure.
195  *
196  * @param       p_tcp           the tile coding parameter to destroy.
197  */
198 static void opj_j2k_tcp_destroy (opj_tcp_t *p_tcp);
199
200 /**
201  * Destroys the data inside a tile coding parameter structure.
202  *
203  * @param       p_tcp           the tile coding parameter which contain data to destroy.
204  */
205 static void opj_j2k_tcp_data_destroy (opj_tcp_t *p_tcp);
206
207 /**
208  * Destroys a coding parameter structure.
209  *
210  * @param       p_cp            the coding parameter to destroy.
211  */
212 static void opj_j2k_cp_destroy (opj_cp_t *p_cp);
213
214 /**
215  * Writes a SPCod or SPCoc element, i.e. the coding style of a given component of a tile.
216  *
217  * @param       p_j2k           J2K codec.
218  * @param       p_tile_no       FIXME DOC
219  * @param       p_comp_no       the component number to output.
220  * @param       p_data          FIXME DOC
221  * @param       p_header_size   FIXME DOC
222  * @param       p_manager       the user event manager.
223  *
224  * @return FIXME DOC
225 */
226 static OPJ_BOOL opj_j2k_write_SPCod_SPCoc(      opj_j2k_t *p_j2k,
227                                                                                     OPJ_UINT32 p_tile_no,
228                                                                                     OPJ_UINT32 p_comp_no,
229                                                                                     OPJ_BYTE * p_data,
230                                                                                     OPJ_UINT32 * p_header_size,
231                                                                                     opj_event_mgr_t * p_manager );
232
233 /**
234  * Gets the size taken by writing a SPCod or SPCoc for the given tile and component.
235  *
236  * @param       p_j2k                   the J2K codec.
237  * @param       p_tile_no               the tile index.
238  * @param       p_comp_no               the component being outputted.
239  *
240  * @return      the number of bytes taken by the SPCod element.
241  */
242 static OPJ_UINT32 opj_j2k_get_SPCod_SPCoc_size (opj_j2k_t *p_j2k,
243                                                                                             OPJ_UINT32 p_tile_no,
244                                                                                             OPJ_UINT32 p_comp_no );
245
246 /**
247  * Reads a SPCod or SPCoc element, i.e. the coding style of a given component of a tile.
248  * @param       p_j2k           the jpeg2000 codec.
249  * @param       compno          FIXME DOC
250  * @param       p_header_data   the data contained in the COM box.
251  * @param       p_header_size   the size of the data contained in the COM marker.
252  * @param       p_manager       the user event manager.
253 */
254 static OPJ_BOOL opj_j2k_read_SPCod_SPCoc(   opj_j2k_t *p_j2k,
255                                             OPJ_UINT32 compno,
256                                             OPJ_BYTE * p_header_data,
257                                             OPJ_UINT32 * p_header_size,
258                                             opj_event_mgr_t * p_manager );
259
260 /**
261  * Gets the size taken by writing SQcd or SQcc element, i.e. the quantization values of a band in the QCD or QCC.
262  *
263  * @param       p_tile_no               the tile index.
264  * @param       p_comp_no               the component being outputted.
265  * @param       p_j2k                   the J2K codec.
266  *
267  * @return      the number of bytes taken by the SPCod element.
268  */
269 static OPJ_UINT32 opj_j2k_get_SQcd_SQcc_size (  opj_j2k_t *p_j2k,
270                                                                                     OPJ_UINT32 p_tile_no,
271                                                                                     OPJ_UINT32 p_comp_no );
272
273 /**
274  * Writes a SQcd or SQcc element, i.e. the quantization values of a band in the QCD or QCC.
275  *
276  * @param       p_tile_no               the tile to output.
277  * @param       p_comp_no               the component number to output.
278  * @param       p_data                  the data buffer.
279  * @param       p_header_size   pointer to the size of the data buffer, it is changed by the function.
280  * @param       p_j2k                   J2K codec.
281  * @param       p_manager               the user event manager.
282  *
283 */
284 static OPJ_BOOL opj_j2k_write_SQcd_SQcc(opj_j2k_t *p_j2k,
285                                                                             OPJ_UINT32 p_tile_no,
286                                                                             OPJ_UINT32 p_comp_no,
287                                                                             OPJ_BYTE * p_data,
288                                                                             OPJ_UINT32 * p_header_size,
289                                                                             opj_event_mgr_t * p_manager);
290
291 /**
292  * Updates the Tile Length Marker.
293  */
294 static void opj_j2k_update_tlm ( opj_j2k_t * p_j2k, OPJ_UINT32 p_tile_part_size);
295
296 /**
297  * Reads a SQcd or SQcc element, i.e. the quantization values of a band in the QCD or QCC.
298  *
299  * @param       p_j2k           J2K codec.
300  * @param       compno          the component number to output.
301  * @param       p_header_data   the data buffer.
302  * @param       p_header_size   pointer to the size of the data buffer, it is changed by the function.
303  * @param       p_manager       the user event manager.
304  *
305 */
306 static OPJ_BOOL opj_j2k_read_SQcd_SQcc( opj_j2k_t *p_j2k,
307                                         OPJ_UINT32 compno,
308                                         OPJ_BYTE * p_header_data,
309                                         OPJ_UINT32 * p_header_size,
310                                         opj_event_mgr_t * p_manager );
311
312 /**
313  * Copies the tile component parameters of all the component from the first tile component.
314  *
315  * @param               p_j2k           the J2k codec.
316  */
317 static void opj_j2k_copy_tile_component_parameters( opj_j2k_t *p_j2k );
318
319 /**
320  * Copies the tile quantization parameters of all the component from the first tile component.
321  *
322  * @param               p_j2k           the J2k codec.
323  */
324 static void opj_j2k_copy_tile_quantization_parameters( opj_j2k_t *p_j2k );
325
326 /**
327  * Reads the tiles.
328  */
329 static OPJ_BOOL opj_j2k_decode_tiles (  opj_j2k_t *p_j2k,
330                                         opj_stream_private_t *p_stream,
331                                         opj_event_mgr_t * p_manager);
332
333 static OPJ_BOOL opj_j2k_pre_write_tile ( opj_j2k_t * p_j2k,
334                                                                              OPJ_UINT32 p_tile_index,
335                                                                              opj_stream_private_t *p_stream,
336                                                                              opj_event_mgr_t * p_manager );
337
338 static OPJ_BOOL opj_j2k_update_image_data (opj_tcd_t * p_tcd, OPJ_BYTE * p_data, opj_image_t* p_output_image);
339
340 static void opj_j2k_get_tile_data (opj_tcd_t * p_tcd, OPJ_BYTE * p_data);
341
342 static OPJ_BOOL opj_j2k_post_write_tile (opj_j2k_t * p_j2k,
343                                                                              OPJ_BYTE * p_data,
344                                                                              OPJ_UINT32 p_data_size,
345                                                                              opj_stream_private_t *p_stream,
346                                                                              opj_event_mgr_t * p_manager );
347
348 /**
349  * Sets up the procedures to do on writing header.
350  * Developers wanting to extend the library can add their own writing procedures.
351  */
352 static void opj_j2k_setup_header_writing (opj_j2k_t *p_j2k);
353
354 static OPJ_BOOL opj_j2k_write_first_tile_part(  opj_j2k_t *p_j2k,
355                                                                                             OPJ_BYTE * p_data,
356                                                                                             OPJ_UINT32 * p_data_written,
357                                                                                             OPJ_UINT32 p_total_data_size,
358                                                                                             opj_stream_private_t *p_stream,
359                                                                                             struct opj_event_mgr * p_manager );
360
361 static OPJ_BOOL opj_j2k_write_all_tile_parts(   opj_j2k_t *p_j2k,
362                                                                                             OPJ_BYTE * p_data,
363                                                                                             OPJ_UINT32 * p_data_written,
364                                                                                             OPJ_UINT32 p_total_data_size,
365                                                                                             opj_stream_private_t *p_stream,
366                                                                                             struct opj_event_mgr * p_manager );
367
368 /**
369  * Gets the offset of the header.
370  *
371  * @param       p_stream                the stream to write data to.
372  * @param       p_j2k                   J2K codec.
373  * @param       p_manager               the user event manager.
374 */
375 static OPJ_BOOL opj_j2k_get_end_header( opj_j2k_t *p_j2k,
376                                         opj_stream_private_t *p_stream,
377                                         opj_event_mgr_t * p_manager );
378
379 static OPJ_BOOL opj_j2k_allocate_tile_element_cstr_index(opj_j2k_t *p_j2k);
380
381 /*
382  * -----------------------------------------------------------------------
383  * -----------------------------------------------------------------------
384  * -----------------------------------------------------------------------
385  */
386
387 /**
388  * Writes the SOC marker (Start Of Codestream)
389  *
390  * @param       p_stream                        the stream to write data to.
391  * @param       p_j2k                   J2K codec.
392  * @param       p_manager       the user event manager.
393 */
394 static OPJ_BOOL opj_j2k_write_soc(      opj_j2k_t *p_j2k,
395                                                         opj_stream_private_t *p_stream,
396                                                             opj_event_mgr_t * p_manager );
397
398 /**
399  * Reads a SOC marker (Start of Codestream)
400  * @param       p_j2k           the jpeg2000 file codec.
401  * @param       p_stream        XXX needs data
402  * @param       p_manager       the user event manager.
403 */
404 static OPJ_BOOL opj_j2k_read_soc(   opj_j2k_t *p_j2k,
405                                     opj_stream_private_t *p_stream,
406                                     opj_event_mgr_t * p_manager );
407
408 /**
409  * Writes the SIZ marker (image and tile size)
410  *
411  * @param       p_j2k           J2K codec.
412  * @param       p_stream        the stream to write data to.
413  * @param       p_manager       the user event manager.
414 */
415 static OPJ_BOOL opj_j2k_write_siz(      opj_j2k_t *p_j2k,
416                                                                 opj_stream_private_t *p_stream,
417                                                                 opj_event_mgr_t * p_manager );
418
419 /**
420  * Reads a SIZ marker (image and tile size)
421  * @param       p_j2k           the jpeg2000 file codec.
422  * @param       p_header_data   the data contained in the SIZ box.
423  * @param       p_header_size   the size of the data contained in the SIZ marker.
424  * @param       p_manager       the user event manager.
425 */
426 static OPJ_BOOL opj_j2k_read_siz(opj_j2k_t *p_j2k,
427                                  OPJ_BYTE * p_header_data,
428                                  OPJ_UINT32 p_header_size,
429                                  opj_event_mgr_t * p_manager);
430
431 /**
432  * Writes the COM marker (comment)
433  *
434  * @param       p_stream                        the stream to write data to.
435  * @param       p_j2k                   J2K codec.
436  * @param       p_manager       the user event manager.
437 */
438 static OPJ_BOOL opj_j2k_write_com(      opj_j2k_t *p_j2k,
439                                                                         opj_stream_private_t *p_stream,
440                                                                         opj_event_mgr_t * p_manager );
441
442 /**
443  * Reads a COM marker (comments)
444  * @param       p_j2k           the jpeg2000 file codec.
445  * @param       p_header_data   the data contained in the COM box.
446  * @param       p_header_size   the size of the data contained in the COM marker.
447  * @param       p_manager       the user event manager.
448 */
449 static OPJ_BOOL opj_j2k_read_com (  opj_j2k_t *p_j2k,
450                                     OPJ_BYTE * p_header_data,
451                                     OPJ_UINT32 p_header_size,
452                                     opj_event_mgr_t * p_manager );
453 /**
454  * Writes the COD marker (Coding style default)
455  *
456  * @param       p_stream                        the stream to write data to.
457  * @param       p_j2k                   J2K codec.
458  * @param       p_manager       the user event manager.
459 */
460 static OPJ_BOOL opj_j2k_write_cod(      opj_j2k_t *p_j2k,
461                                                                         opj_stream_private_t *p_stream,
462                                                                         opj_event_mgr_t * p_manager );
463
464 /**
465  * Reads a COD marker (Coding Styke defaults)
466  * @param       p_header_data   the data contained in the COD box.
467  * @param       p_j2k                   the jpeg2000 codec.
468  * @param       p_header_size   the size of the data contained in the COD marker.
469  * @param       p_manager               the user event manager.
470 */
471 static OPJ_BOOL opj_j2k_read_cod (  opj_j2k_t *p_j2k,
472                                     OPJ_BYTE * p_header_data,
473                                     OPJ_UINT32 p_header_size,
474                                     opj_event_mgr_t * p_manager);
475
476 /**
477  * Writes the COC marker (Coding style component)
478  *
479  * @param       p_j2k       J2K codec.
480  * @param       p_comp_no   the index of the component to output.
481  * @param       p_stream    the stream to write data to.
482  * @param       p_manager   the user event manager.
483 */
484 static OPJ_BOOL opj_j2k_write_coc(  opj_j2k_t *p_j2k,
485                                                                 OPJ_UINT32 p_comp_no,
486                                                                 opj_stream_private_t *p_stream,
487                                                                 opj_event_mgr_t * p_manager );
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_data          FIXME DOC
494  * @param       p_data_written  FIXME DOC
495  * @param       p_manager               the user event manager.
496 */
497 static void opj_j2k_write_coc_in_memory(opj_j2k_t *p_j2k,
498                                                                             OPJ_UINT32 p_comp_no,
499                                                                             OPJ_BYTE * p_data,
500                                                                             OPJ_UINT32 * p_data_written,
501                                                                             opj_event_mgr_t * p_manager );
502
503 /**
504  * Gets the maximum size taken by a coc.
505  *
506  * @param       p_j2k   the jpeg2000 codec to use.
507  */
508 static OPJ_UINT32 opj_j2k_get_max_coc_size(opj_j2k_t *p_j2k);
509
510 /**
511  * Reads a COC marker (Coding Style Component)
512  * @param       p_header_data   the data contained in the COC box.
513  * @param       p_j2k                   the jpeg2000 codec.
514  * @param       p_header_size   the size of the data contained in the COC marker.
515  * @param       p_manager               the user event manager.
516 */
517 static OPJ_BOOL opj_j2k_read_coc (  opj_j2k_t *p_j2k,
518                                     OPJ_BYTE * p_header_data,
519                                     OPJ_UINT32 p_header_size,
520                                     opj_event_mgr_t * p_manager );
521
522 /**
523  * Writes the QCD marker (quantization default)
524  *
525  * @param       p_j2k                   J2K codec.
526  * @param       p_stream                the stream to write data to.
527  * @param       p_manager               the user event manager.
528 */
529 static OPJ_BOOL opj_j2k_write_qcd(      opj_j2k_t *p_j2k,
530                                                                         opj_stream_private_t *p_stream,
531                                                                         opj_event_mgr_t * p_manager );
532
533 /**
534  * Reads a QCD marker (Quantization defaults)
535  * @param       p_header_data   the data contained in the QCD box.
536  * @param       p_j2k                   the jpeg2000 codec.
537  * @param       p_header_size   the size of the data contained in the QCD marker.
538  * @param       p_manager               the user event manager.
539 */
540 static OPJ_BOOL opj_j2k_read_qcd (  opj_j2k_t *p_j2k,
541                                     OPJ_BYTE * p_header_data,
542                                     OPJ_UINT32 p_header_size,
543                                     opj_event_mgr_t * p_manager );
544 /**
545  * Writes the QCC marker (quantization component)
546  *
547  * @param       p_comp_no       the index of the component to output.
548  * @param       p_stream                the stream to write data to.
549  * @param       p_j2k                   J2K codec.
550  * @param       p_manager               the user event manager.
551 */
552 static OPJ_BOOL opj_j2k_write_qcc(      opj_j2k_t *p_j2k,
553                                                                         OPJ_UINT32 p_comp_no,
554                                                                         opj_stream_private_t *p_stream,
555                                                                         opj_event_mgr_t * p_manager );
556
557 /**
558  * Writes the QCC marker (quantization component)
559  *
560  * @param       p_j2k           J2K codec.
561  * @param       p_comp_no       the index of the component to output.
562  * @param       p_data          FIXME DOC
563  * @param       p_data_written  the stream to write data to.
564  * @param       p_manager       the user event manager.
565 */
566 static void opj_j2k_write_qcc_in_memory(opj_j2k_t *p_j2k,
567                                                                             OPJ_UINT32 p_comp_no,
568                                                                             OPJ_BYTE * p_data,
569                                                                             OPJ_UINT32 * p_data_written,
570                                                                             opj_event_mgr_t * p_manager );
571
572 /**
573  * Gets the maximum size taken by a qcc.
574  */
575 static OPJ_UINT32 opj_j2k_get_max_qcc_size (opj_j2k_t *p_j2k);
576
577 /**
578  * Reads a QCC marker (Quantization component)
579  * @param       p_header_data   the data contained in the QCC box.
580  * @param       p_j2k                   the jpeg2000 codec.
581  * @param       p_header_size   the size of the data contained in the QCC marker.
582  * @param       p_manager               the user event manager.
583 */
584 static OPJ_BOOL opj_j2k_read_qcc(   opj_j2k_t *p_j2k,
585                                     OPJ_BYTE * p_header_data,
586                                     OPJ_UINT32 p_header_size,
587                                     opj_event_mgr_t * p_manager);
588 /**
589  * Writes the POC marker (Progression Order Change)
590  *
591  * @param       p_stream                                the stream to write data to.
592  * @param       p_j2k                           J2K codec.
593  * @param       p_manager               the user event manager.
594 */
595 static OPJ_BOOL opj_j2k_write_poc(      opj_j2k_t *p_j2k,
596                                                                         opj_stream_private_t *p_stream,
597                                                                         opj_event_mgr_t * p_manager );
598 /**
599  * Writes the POC marker (Progression Order Change)
600  *
601  * @param       p_j2k          J2K codec.
602  * @param       p_data         FIXME DOC
603  * @param       p_data_written the stream to write data to.
604  * @param       p_manager      the user event manager.
605  */
606 static void opj_j2k_write_poc_in_memory(opj_j2k_t *p_j2k,
607                                                                             OPJ_BYTE * p_data,
608                                                                             OPJ_UINT32 * p_data_written,
609                                                                             opj_event_mgr_t * p_manager );
610 /**
611  * Gets the maximum size taken by the writing of a POC.
612  */
613 static OPJ_UINT32 opj_j2k_get_max_poc_size(opj_j2k_t *p_j2k);
614
615 /**
616  * Reads a POC marker (Progression Order Change)
617  *
618  * @param       p_header_data   the data contained in the POC box.
619  * @param       p_j2k                   the jpeg2000 codec.
620  * @param       p_header_size   the size of the data contained in the POC marker.
621  * @param       p_manager               the user event manager.
622 */
623 static OPJ_BOOL opj_j2k_read_poc (  opj_j2k_t *p_j2k,
624                                     OPJ_BYTE * p_header_data,
625                                     OPJ_UINT32 p_header_size,
626                                     opj_event_mgr_t * p_manager );
627
628 /**
629  * Gets the maximum size taken by the toc headers of all the tile parts of any given tile.
630  */
631 static OPJ_UINT32 opj_j2k_get_max_toc_size (opj_j2k_t *p_j2k);
632
633 /**
634  * Gets the maximum size taken by the headers of the SOT.
635  *
636  * @param       p_j2k   the jpeg2000 codec to use.
637  */
638 static OPJ_UINT32 opj_j2k_get_specific_header_sizes(opj_j2k_t *p_j2k);
639
640 /**
641  * Reads a CRG marker (Component registration)
642  *
643  * @param       p_header_data   the data contained in the TLM box.
644  * @param       p_j2k                   the jpeg2000 codec.
645  * @param       p_header_size   the size of the data contained in the TLM marker.
646  * @param       p_manager               the user event manager.
647 */
648 static OPJ_BOOL opj_j2k_read_crg (  opj_j2k_t *p_j2k,
649                                     OPJ_BYTE * p_header_data,
650                                     OPJ_UINT32 p_header_size,
651                                     opj_event_mgr_t * p_manager );
652 /**
653  * Reads a TLM marker (Tile Length Marker)
654  *
655  * @param       p_header_data   the data contained in the TLM box.
656  * @param       p_j2k                   the jpeg2000 codec.
657  * @param       p_header_size   the size of the data contained in the TLM marker.
658  * @param       p_manager               the user event manager.
659 */
660 static OPJ_BOOL opj_j2k_read_tlm (  opj_j2k_t *p_j2k,
661                                     OPJ_BYTE * p_header_data,
662                                     OPJ_UINT32 p_header_size,
663                                     opj_event_mgr_t * p_manager);
664
665 /**
666  * Writes the updated tlm.
667  *
668  * @param       p_stream                the stream to write data to.
669  * @param       p_j2k                   J2K codec.
670  * @param       p_manager               the user event manager.
671 */
672 static OPJ_BOOL opj_j2k_write_updated_tlm(      opj_j2k_t *p_j2k,
673                                             opj_stream_private_t *p_stream,
674                                             opj_event_mgr_t * p_manager );
675
676 /**
677  * Reads a PLM marker (Packet length, main header marker)
678  *
679  * @param       p_header_data   the data contained in the TLM box.
680  * @param       p_j2k                   the jpeg2000 codec.
681  * @param       p_header_size   the size of the data contained in the TLM marker.
682  * @param       p_manager               the user event manager.
683 */
684 static OPJ_BOOL opj_j2k_read_plm (  opj_j2k_t *p_j2k,
685                                     OPJ_BYTE * p_header_data,
686                                     OPJ_UINT32 p_header_size,
687                                     opj_event_mgr_t * p_manager);
688 /**
689  * Reads a PLT marker (Packet length, tile-part header)
690  *
691  * @param       p_header_data   the data contained in the PLT box.
692  * @param       p_j2k                   the jpeg2000 codec.
693  * @param       p_header_size   the size of the data contained in the PLT marker.
694  * @param       p_manager               the user event manager.
695 */
696 static OPJ_BOOL opj_j2k_read_plt (  opj_j2k_t *p_j2k,
697                                     OPJ_BYTE * p_header_data,
698                                     OPJ_UINT32 p_header_size,
699                                     opj_event_mgr_t * p_manager );
700
701 #if 0
702 /**
703  * Reads a PPM marker (Packed packet headers, main header)
704  *
705  * @param       p_header_data   the data contained in the POC box.
706  * @param       p_j2k                   the jpeg2000 codec.
707  * @param       p_header_size   the size of the data contained in the POC marker.
708  * @param       p_manager               the user event manager.
709 */
710 static OPJ_BOOL j2k_read_ppm_v2 (
711                                                 opj_j2k_t *p_j2k,
712                                                 OPJ_BYTE * p_header_data,
713                                                 OPJ_UINT32 p_header_size,
714                                                 struct opj_event_mgr * p_manager
715                                         );
716 #endif
717
718 static OPJ_BOOL j2k_read_ppm_v3 (
719                                                 opj_j2k_t *p_j2k,
720                                                 OPJ_BYTE * p_header_data,
721                                                 OPJ_UINT32 p_header_size,
722                                                 opj_event_mgr_t * p_manager );
723
724 /**
725  * Reads a PPT marker (Packed packet headers, tile-part header)
726  *
727  * @param       p_header_data   the data contained in the PPT box.
728  * @param       p_j2k                   the jpeg2000 codec.
729  * @param       p_header_size   the size of the data contained in the PPT marker.
730  * @param       p_manager               the user event manager.
731 */
732 static OPJ_BOOL opj_j2k_read_ppt (  opj_j2k_t *p_j2k,
733                                     OPJ_BYTE * p_header_data,
734                                     OPJ_UINT32 p_header_size,
735                                     opj_event_mgr_t * p_manager );
736 /**
737  * Writes the TLM marker (Tile Length Marker)
738  *
739  * @param       p_stream                                the stream to write data to.
740  * @param       p_j2k                           J2K codec.
741  * @param       p_manager               the user event manager.
742 */
743 static OPJ_BOOL opj_j2k_write_tlm(      opj_j2k_t *p_j2k,
744                                                                         opj_stream_private_t *p_stream,
745                                                                         opj_event_mgr_t * p_manager );
746
747 /**
748  * Writes the SOT marker (Start of tile-part)
749  *
750  * @param       p_j2k            J2K codec.
751  * @param       p_data           FIXME DOC
752  * @param       p_data_written   FIXME DOC
753  * @param       p_stream         the stream to write data to.
754  * @param       p_manager        the user event manager.
755 */
756 static OPJ_BOOL opj_j2k_write_sot(      opj_j2k_t *p_j2k,
757                                                                         OPJ_BYTE * p_data,
758                                                                         OPJ_UINT32 * p_data_written,
759                                                                         const opj_stream_private_t *p_stream,
760                                                                         opj_event_mgr_t * p_manager );
761
762 /**
763  * Reads a PPT marker (Packed packet headers, tile-part header)
764  *
765  * @param       p_header_data   the data contained in the PPT box.
766  * @param       p_j2k                   the jpeg2000 codec.
767  * @param       p_header_size   the size of the data contained in the PPT marker.
768  * @param       p_manager               the user event manager.
769 */
770 static OPJ_BOOL opj_j2k_read_sot (  opj_j2k_t *p_j2k,
771                                     OPJ_BYTE * p_header_data,
772                                     OPJ_UINT32 p_header_size,
773                                     opj_event_mgr_t * p_manager );
774 /**
775  * Writes the SOD marker (Start of data)
776  *
777  * @param       p_j2k               J2K codec.
778  * @param       p_tile_coder        FIXME DOC
779  * @param       p_data              FIXME DOC
780  * @param       p_data_written      FIXME DOC
781  * @param       p_total_data_size   FIXME DOC
782  * @param       p_stream            the stream to write data to.
783  * @param       p_manager           the user event manager.
784 */
785 static OPJ_BOOL opj_j2k_write_sod(      opj_j2k_t *p_j2k,
786                                                                         opj_tcd_t * p_tile_coder,
787                                                                         OPJ_BYTE * p_data,
788                                                                         OPJ_UINT32 * p_data_written,
789                                                                         OPJ_UINT32 p_total_data_size,
790                                                                         const opj_stream_private_t *p_stream,
791                                                                         opj_event_mgr_t * p_manager );
792
793 /**
794  * Reads a SOD marker (Start Of Data)
795  *
796  * @param       p_j2k                   the jpeg2000 codec.
797  * @param       p_stream                FIXME DOC
798  * @param       p_manager               the user event manager.
799 */
800 static OPJ_BOOL opj_j2k_read_sod(   opj_j2k_t *p_j2k,
801                                     opj_stream_private_t *p_stream,
802                                     opj_event_mgr_t * p_manager );
803
804 void opj_j2k_update_tlm (opj_j2k_t * p_j2k, OPJ_UINT32 p_tile_part_size )
805 {
806         opj_write_bytes(p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current,p_j2k->m_current_tile_number,1);            /* PSOT */
807         ++p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current;
808
809         opj_write_bytes(p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current,p_tile_part_size,4);                                        /* PSOT */
810         p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current += 4;
811 }
812
813 /**
814  * Writes the RGN marker (Region Of Interest)
815  *
816  * @param       p_tile_no               the tile to output
817  * @param       p_comp_no               the component to output
818  * @param       nb_comps                the number of components
819  * @param       p_stream                the stream to write data to.
820  * @param       p_j2k                   J2K codec.
821  * @param       p_manager               the user event manager.
822 */
823 static OPJ_BOOL opj_j2k_write_rgn(  opj_j2k_t *p_j2k,
824                                     OPJ_UINT32 p_tile_no,
825                                     OPJ_UINT32 p_comp_no,
826                                     OPJ_UINT32 nb_comps,
827                                     opj_stream_private_t *p_stream,
828                                     opj_event_mgr_t * p_manager );
829
830 /**
831  * Reads a RGN marker (Region Of Interest)
832  *
833  * @param       p_header_data   the data contained in the POC box.
834  * @param       p_j2k                   the jpeg2000 codec.
835  * @param       p_header_size   the size of the data contained in the POC marker.
836  * @param       p_manager               the user event manager.
837 */
838 static OPJ_BOOL opj_j2k_read_rgn (opj_j2k_t *p_j2k,
839                                   OPJ_BYTE * p_header_data,
840                                   OPJ_UINT32 p_header_size,
841                                   opj_event_mgr_t * p_manager );
842
843 /**
844  * Writes the EOC marker (End of Codestream)
845  *
846  * @param       p_stream                the stream to write data to.
847  * @param       p_j2k                   J2K codec.
848  * @param       p_manager               the user event manager.
849 */
850 static OPJ_BOOL opj_j2k_write_eoc(      opj_j2k_t *p_j2k,
851                                     opj_stream_private_t *p_stream,
852                                     opj_event_mgr_t * p_manager );
853
854 /**
855  * Reads a EOC marker (End Of Codestream)
856  *
857  * @param       p_j2k                   the jpeg2000 codec.
858  * @param       p_stream                FIXME DOC
859  * @param       p_manager               the user event manager.
860 */
861 static OPJ_BOOL opj_j2k_read_eoc (      opj_j2k_t *p_j2k,
862                                                                 opj_stream_private_t *p_stream,
863                                                                 opj_event_mgr_t * p_manager );
864
865 /**
866  * Writes the CBD-MCT-MCC-MCO markers (Multi components transform)
867  *
868  * @param       p_stream                        the stream to write data to.
869  * @param       p_j2k                   J2K codec.
870  * @param       p_manager       the user event manager.
871 */
872 static OPJ_BOOL opj_j2k_write_mct_data_group(   opj_j2k_t *p_j2k,
873                                                 opj_stream_private_t *p_stream,
874                                                 opj_event_mgr_t * p_manager );
875
876 /**
877  * Inits the Info
878  *
879  * @param       p_stream                the stream to write data to.
880  * @param       p_j2k                   J2K codec.
881  * @param       p_manager               the user event manager.
882 */
883 static OPJ_BOOL opj_j2k_init_info(      opj_j2k_t *p_j2k,
884                                     opj_stream_private_t *p_stream,
885                                     opj_event_mgr_t * p_manager );
886
887 /**
888 Add main header marker information
889 @param cstr_index    Codestream information structure
890 @param type         marker type
891 @param pos          byte offset of marker segment
892 @param len          length of marker segment
893  */
894 static OPJ_BOOL opj_j2k_add_mhmarker(opj_codestream_index_t *cstr_index, OPJ_UINT32 type, OPJ_OFF_T pos, OPJ_UINT32 len) ;
895 /**
896 Add tile header marker information
897 @param tileno       tile index number
898 @param cstr_index   Codestream information structure
899 @param type         marker type
900 @param pos          byte offset of marker segment
901 @param len          length of marker segment
902  */
903 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);
904
905 /**
906  * Reads an unknown marker
907  *
908  * @param       p_j2k                   the jpeg2000 codec.
909  * @param       p_stream                the stream object to read from.
910  * @param       output_marker           FIXME DOC
911  * @param       p_manager               the user event manager.
912  *
913  * @return      true                    if the marker could be deduced.
914 */
915 static OPJ_BOOL opj_j2k_read_unk( opj_j2k_t *p_j2k,
916                                   opj_stream_private_t *p_stream,
917                                   OPJ_UINT32 *output_marker,
918                                   opj_event_mgr_t * p_manager );
919
920 /**
921  * Writes the MCT marker (Multiple Component Transform)
922  *
923  * @param       p_j2k           J2K codec.
924  * @param       p_mct_record    FIXME DOC
925  * @param       p_stream        the stream to write data to.
926  * @param       p_manager       the user event manager.
927 */
928 static OPJ_BOOL opj_j2k_write_mct_record(       opj_j2k_t *p_j2k,
929                                                                                     opj_mct_data_t * p_mct_record,
930                                             opj_stream_private_t *p_stream,
931                                             opj_event_mgr_t * p_manager );
932
933 /**
934  * Reads a MCT marker (Multiple Component Transform)
935  *
936  * @param       p_header_data   the data contained in the MCT box.
937  * @param       p_j2k                   the jpeg2000 codec.
938  * @param       p_header_size   the size of the data contained in the MCT marker.
939  * @param       p_manager               the user event manager.
940 */
941 static OPJ_BOOL opj_j2k_read_mct (      opj_j2k_t *p_j2k,
942                                                                     OPJ_BYTE * p_header_data,
943                                                                     OPJ_UINT32 p_header_size,
944                                                                     opj_event_mgr_t * p_manager );
945
946 /**
947  * Writes the MCC marker (Multiple Component Collection)
948  *
949  * @param       p_j2k                   J2K codec.
950  * @param       p_mcc_record            FIXME DOC
951  * @param       p_stream                the stream to write data to.
952  * @param       p_manager               the user event manager.
953 */
954 static OPJ_BOOL opj_j2k_write_mcc_record(   opj_j2k_t *p_j2k,
955                                             opj_simple_mcc_decorrelation_data_t * p_mcc_record,
956                                             opj_stream_private_t *p_stream,
957                                             opj_event_mgr_t * p_manager );
958
959 /**
960  * Reads a MCC marker (Multiple Component Collection)
961  *
962  * @param       p_header_data   the data contained in the MCC box.
963  * @param       p_j2k                   the jpeg2000 codec.
964  * @param       p_header_size   the size of the data contained in the MCC marker.
965  * @param       p_manager               the user event manager.
966 */
967 static OPJ_BOOL opj_j2k_read_mcc (      opj_j2k_t *p_j2k,
968                                                                     OPJ_BYTE * p_header_data,
969                                                                     OPJ_UINT32 p_header_size,
970                                                                     opj_event_mgr_t * p_manager );
971
972 /**
973  * Writes the MCO marker (Multiple component transformation ordering)
974  *
975  * @param       p_stream                                the stream to write data to.
976  * @param       p_j2k                           J2K codec.
977  * @param       p_manager               the user event manager.
978 */
979 static OPJ_BOOL opj_j2k_write_mco(      opj_j2k_t *p_j2k,
980                                     opj_stream_private_t *p_stream,
981                                     opj_event_mgr_t * p_manager );
982
983 /**
984  * Reads a MCO marker (Multiple Component Transform Ordering)
985  *
986  * @param       p_header_data   the data contained in the MCO box.
987  * @param       p_j2k                   the jpeg2000 codec.
988  * @param       p_header_size   the size of the data contained in the MCO marker.
989  * @param       p_manager               the user event manager.
990 */
991 static OPJ_BOOL opj_j2k_read_mco (      opj_j2k_t *p_j2k,
992                                                                     OPJ_BYTE * p_header_data,
993                                                                     OPJ_UINT32 p_header_size,
994                                                                     opj_event_mgr_t * p_manager );
995
996 static OPJ_BOOL opj_j2k_add_mct(opj_tcp_t * p_tcp, opj_image_t * p_image, OPJ_UINT32 p_index);
997
998 static void  opj_j2k_read_int16_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
999 static void  opj_j2k_read_int32_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1000 static void  opj_j2k_read_float32_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1001 static void  opj_j2k_read_float64_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1002
1003 static void  opj_j2k_read_int16_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1004 static void  opj_j2k_read_int32_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1005 static void  opj_j2k_read_float32_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1006 static void  opj_j2k_read_float64_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1007
1008 static void  opj_j2k_write_float_to_int16 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1009 static void  opj_j2k_write_float_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1010 static void  opj_j2k_write_float_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1011 static void  opj_j2k_write_float_to_float64 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1012
1013 /**
1014  * Ends the encoding, i.e. frees memory.
1015  *
1016  * @param       p_stream                the stream to write data to.
1017  * @param       p_j2k                   J2K codec.
1018  * @param       p_manager               the user event manager.
1019 */
1020 static OPJ_BOOL opj_j2k_end_encoding(   opj_j2k_t *p_j2k,
1021                                                                             opj_stream_private_t *p_stream,
1022                                                                             opj_event_mgr_t * p_manager );
1023
1024 /**
1025  * Writes the CBD marker (Component bit depth definition)
1026  *
1027  * @param       p_stream                                the stream to write data to.
1028  * @param       p_j2k                           J2K codec.
1029  * @param       p_manager               the user event manager.
1030 */
1031 static OPJ_BOOL opj_j2k_write_cbd(      opj_j2k_t *p_j2k,
1032                                                                     opj_stream_private_t *p_stream,
1033                                                                         opj_event_mgr_t * p_manager );
1034
1035 /**
1036  * Reads a CBD marker (Component bit depth definition)
1037  * @param       p_header_data   the data contained in the CBD box.
1038  * @param       p_j2k                   the jpeg2000 codec.
1039  * @param       p_header_size   the size of the data contained in the CBD marker.
1040  * @param       p_manager               the user event manager.
1041 */
1042 static OPJ_BOOL opj_j2k_read_cbd (      opj_j2k_t *p_j2k,
1043                                                                 OPJ_BYTE * p_header_data,
1044                                                                 OPJ_UINT32 p_header_size,
1045                                                                 opj_event_mgr_t * p_manager);
1046
1047 /**
1048  * Writes COC marker for each component.
1049  *
1050  * @param       p_stream                the stream to write data to.
1051  * @param       p_j2k                   J2K codec.
1052  * @param       p_manager               the user event manager.
1053 */
1054 static OPJ_BOOL opj_j2k_write_all_coc( opj_j2k_t *p_j2k,
1055                                                                         opj_stream_private_t *p_stream,
1056                                                                         opj_event_mgr_t * p_manager );
1057
1058 /**
1059  * Writes QCC marker for each component.
1060  *
1061  * @param       p_stream                the stream to write data to.
1062  * @param       p_j2k                   J2K codec.
1063  * @param       p_manager               the user event manager.
1064 */
1065 static OPJ_BOOL opj_j2k_write_all_qcc( opj_j2k_t *p_j2k,
1066                                                                         opj_stream_private_t *p_stream,
1067                                                                         opj_event_mgr_t * p_manager );
1068
1069 /**
1070  * Writes regions of interests.
1071  *
1072  * @param       p_stream                the stream to write data to.
1073  * @param       p_j2k                   J2K codec.
1074  * @param       p_manager               the user event manager.
1075 */
1076 static OPJ_BOOL opj_j2k_write_regions(  opj_j2k_t *p_j2k,
1077                                                                         opj_stream_private_t *p_stream,
1078                                                                         opj_event_mgr_t * p_manager );
1079
1080 /**
1081  * Writes EPC ????
1082  *
1083  * @param       p_stream                the stream to write data to.
1084  * @param       p_j2k                   J2K codec.
1085  * @param       p_manager               the user event manager.
1086 */
1087 static OPJ_BOOL opj_j2k_write_epc(      opj_j2k_t *p_j2k,
1088                                                                     opj_stream_private_t *p_stream,
1089                                                                     opj_event_mgr_t * p_manager );
1090
1091 /**
1092  * Checks the progression order changes values. Tells of the poc given as input are valid.
1093  * A nice message is outputted at errors.
1094  *
1095  * @param       p_pocs                  the progression order changes.
1096  * @param       p_nb_pocs               the number of progression order changes.
1097  * @param       p_nb_resolutions        the number of resolutions.
1098  * @param       numcomps                the number of components
1099  * @param       numlayers               the number of layers.
1100  * @param       p_manager               the user event manager.
1101  *
1102  * @return      true if the pocs are valid.
1103  */
1104 static OPJ_BOOL opj_j2k_check_poc_val(  const opj_poc_t *p_pocs,
1105                                                                             OPJ_UINT32 p_nb_pocs,
1106                                                                             OPJ_UINT32 p_nb_resolutions,
1107                                                                             OPJ_UINT32 numcomps,
1108                                                                             OPJ_UINT32 numlayers,
1109                                                                             opj_event_mgr_t * p_manager);
1110
1111 /**
1112  * Gets the number of tile parts used for the given change of progression (if any) and the given tile.
1113  *
1114  * @param               cp                      the coding parameters.
1115  * @param               pino            the offset of the given poc (i.e. its position in the coding parameter).
1116  * @param               tileno          the given tile.
1117  *
1118  * @return              the number of tile parts.
1119  */
1120 static OPJ_UINT32 opj_j2k_get_num_tp( opj_cp_t *cp, OPJ_UINT32 pino, OPJ_UINT32 tileno);
1121
1122 /**
1123  * Calculates the total number of tile parts needed by the encoder to
1124  * encode such an image. If not enough memory is available, then the function return false.
1125  *
1126  * @param       p_nb_tiles      pointer that will hold the number of tile parts.
1127  * @param       cp                      the coding parameters for the image.
1128  * @param       image           the image to encode.
1129  * @param       p_j2k                   the p_j2k encoder.
1130  * @param       p_manager       the user event manager.
1131  *
1132  * @return true if the function was successful, false else.
1133  */
1134 static OPJ_BOOL opj_j2k_calculate_tp(   opj_j2k_t *p_j2k,
1135                                                                             opj_cp_t *cp,
1136                                                                             OPJ_UINT32 * p_nb_tiles,
1137                                                                             opj_image_t *image,
1138                                                                             opj_event_mgr_t * p_manager);
1139
1140 static void opj_j2k_dump_MH_info(opj_j2k_t* p_j2k, FILE* out_stream);
1141
1142 static void opj_j2k_dump_MH_index(opj_j2k_t* p_j2k, FILE* out_stream);
1143
1144 static opj_codestream_index_t* opj_j2k_create_cstr_index(void);
1145
1146 static OPJ_FLOAT32 opj_j2k_get_tp_stride (opj_tcp_t * p_tcp);
1147
1148 static OPJ_FLOAT32 opj_j2k_get_default_stride (opj_tcp_t * p_tcp);
1149
1150 static int opj_j2k_initialise_4K_poc(opj_poc_t *POC, int numres);
1151
1152 static void opj_j2k_set_cinema_parameters(opj_cparameters_t *parameters, opj_image_t *image, opj_event_mgr_t *p_manager);
1153
1154 static OPJ_BOOL opj_j2k_is_cinema_compliant(opj_image_t *image, OPJ_CINEMA_MODE cinema_mode, opj_event_mgr_t *p_manager);
1155
1156 /*@}*/
1157
1158 /*@}*/
1159
1160 /* ----------------------------------------------------------------------- */
1161 typedef struct j2k_prog_order{
1162         OPJ_PROG_ORDER enum_prog;
1163         char str_prog[5];
1164 }j2k_prog_order_t;
1165
1166 j2k_prog_order_t j2k_prog_order_list[] = {
1167         {OPJ_CPRL, "CPRL"},
1168         {OPJ_LRCP, "LRCP"},
1169         {OPJ_PCRL, "PCRL"},
1170         {OPJ_RLCP, "RLCP"},
1171         {OPJ_RPCL, "RPCL"},
1172         {(OPJ_PROG_ORDER)-1, ""}
1173 };
1174
1175 /**
1176  * FIXME DOC
1177  */
1178 static const OPJ_UINT32 MCT_ELEMENT_SIZE [] =
1179 {
1180         2,
1181         4,
1182         4,
1183         8
1184 };
1185
1186 typedef void (* opj_j2k_mct_function) (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1187
1188 const opj_j2k_mct_function j2k_mct_read_functions_to_float [] =
1189 {
1190         opj_j2k_read_int16_to_float,
1191         opj_j2k_read_int32_to_float,
1192         opj_j2k_read_float32_to_float,
1193         opj_j2k_read_float64_to_float
1194 };
1195
1196 const opj_j2k_mct_function j2k_mct_read_functions_to_int32 [] =
1197 {
1198         opj_j2k_read_int16_to_int32,
1199         opj_j2k_read_int32_to_int32,
1200         opj_j2k_read_float32_to_int32,
1201         opj_j2k_read_float64_to_int32
1202 };
1203
1204 const opj_j2k_mct_function j2k_mct_write_functions_from_float [] =
1205 {
1206         opj_j2k_write_float_to_int16,
1207         opj_j2k_write_float_to_int32,
1208         opj_j2k_write_float_to_float,
1209         opj_j2k_write_float_to_float64
1210 };
1211
1212 typedef struct opj_dec_memory_marker_handler
1213 {
1214         /** marker value */
1215         OPJ_UINT32 id;
1216         /** value of the state when the marker can appear */
1217         OPJ_UINT32 states;
1218         /** action linked to the marker */
1219         OPJ_BOOL (*handler) (   opj_j2k_t *p_j2k,
1220                             OPJ_BYTE * p_header_data,
1221                             OPJ_UINT32 p_header_size,
1222                             opj_event_mgr_t * p_manager );
1223 }
1224 opj_dec_memory_marker_handler_t;
1225
1226 const opj_dec_memory_marker_handler_t j2k_memory_marker_handler_tab [] =
1227 {
1228   {J2K_MS_SOT, J2K_STATE_MH | J2K_STATE_TPHSOT, opj_j2k_read_sot},
1229   {J2K_MS_COD, J2K_STATE_MH | J2K_STATE_TPH, opj_j2k_read_cod},
1230   {J2K_MS_COC, J2K_STATE_MH | J2K_STATE_TPH, opj_j2k_read_coc},
1231   {J2K_MS_RGN, J2K_STATE_MH | J2K_STATE_TPH, opj_j2k_read_rgn},
1232   {J2K_MS_QCD, J2K_STATE_MH | J2K_STATE_TPH, opj_j2k_read_qcd},
1233   {J2K_MS_QCC, J2K_STATE_MH | J2K_STATE_TPH, opj_j2k_read_qcc},
1234   {J2K_MS_POC, J2K_STATE_MH | J2K_STATE_TPH, opj_j2k_read_poc},
1235   {J2K_MS_SIZ, J2K_STATE_MHSIZ, opj_j2k_read_siz},
1236   {J2K_MS_TLM, J2K_STATE_MH, opj_j2k_read_tlm},
1237   {J2K_MS_PLM, J2K_STATE_MH, opj_j2k_read_plm},
1238   {J2K_MS_PLT, J2K_STATE_TPH, opj_j2k_read_plt},
1239   {J2K_MS_PPM, J2K_STATE_MH, j2k_read_ppm_v3},
1240   {J2K_MS_PPT, J2K_STATE_TPH, opj_j2k_read_ppt},
1241   {J2K_MS_SOP, 0, 0},
1242   {J2K_MS_CRG, J2K_STATE_MH, opj_j2k_read_crg},
1243   {J2K_MS_COM, J2K_STATE_MH | J2K_STATE_TPH, opj_j2k_read_com},
1244   {J2K_MS_MCT, J2K_STATE_MH | J2K_STATE_TPH, opj_j2k_read_mct},
1245   {J2K_MS_CBD, J2K_STATE_MH , opj_j2k_read_cbd},
1246   {J2K_MS_MCC, J2K_STATE_MH | J2K_STATE_TPH, opj_j2k_read_mcc},
1247   {J2K_MS_MCO, J2K_STATE_MH | J2K_STATE_TPH, opj_j2k_read_mco},
1248 #ifdef USE_JPWL
1249 #ifdef TODO_MS /* remove these functions which are not commpatible with the v2 API */
1250   {J2K_MS_EPC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_epc},
1251   {J2K_MS_EPB, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_epb},
1252   {J2K_MS_ESD, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_esd},
1253   {J2K_MS_RED, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_red},
1254 #endif
1255 #endif /* USE_JPWL */
1256 #ifdef USE_JPSEC
1257   {J2K_MS_SEC, J2K_DEC_STATE_MH, j2k_read_sec},
1258   {J2K_MS_INSEC, 0, j2k_read_insec}
1259 #endif /* USE_JPSEC */
1260   {J2K_MS_UNK, J2K_STATE_MH | J2K_STATE_TPH, 0}/*opj_j2k_read_unk is directly used*/
1261 };
1262
1263 void  opj_j2k_read_int16_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1264 {
1265         OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
1266         OPJ_FLOAT32 * l_dest_data = (OPJ_FLOAT32 *) p_dest_data;
1267         OPJ_UINT32 i;
1268         OPJ_UINT32 l_temp;
1269
1270         for (i=0;i<p_nb_elem;++i) {
1271                 opj_read_bytes(l_src_data,&l_temp,2);
1272
1273                 l_src_data+=sizeof(OPJ_INT16);
1274
1275                 *(l_dest_data++) = (OPJ_FLOAT32) l_temp;
1276         }
1277 }
1278
1279 void  opj_j2k_read_int32_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1280 {
1281         OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
1282         OPJ_FLOAT32 * l_dest_data = (OPJ_FLOAT32 *) p_dest_data;
1283         OPJ_UINT32 i;
1284         OPJ_UINT32 l_temp;
1285
1286         for (i=0;i<p_nb_elem;++i) {
1287                 opj_read_bytes(l_src_data,&l_temp,4);
1288
1289                 l_src_data+=sizeof(OPJ_INT32);
1290
1291                 *(l_dest_data++) = (OPJ_FLOAT32) l_temp;
1292         }
1293 }
1294
1295 void  opj_j2k_read_float32_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1296 {
1297         OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
1298         OPJ_FLOAT32 * l_dest_data = (OPJ_FLOAT32 *) p_dest_data;
1299         OPJ_UINT32 i;
1300         OPJ_FLOAT32 l_temp;
1301
1302         for (i=0;i<p_nb_elem;++i) {
1303                 opj_read_float(l_src_data,&l_temp);
1304
1305                 l_src_data+=sizeof(OPJ_FLOAT32);
1306
1307                 *(l_dest_data++) = l_temp;
1308         }
1309 }
1310
1311 void  opj_j2k_read_float64_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1312 {
1313         OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
1314         OPJ_FLOAT32 * l_dest_data = (OPJ_FLOAT32 *) p_dest_data;
1315         OPJ_UINT32 i;
1316         OPJ_FLOAT64 l_temp;
1317
1318         for (i=0;i<p_nb_elem;++i) {
1319                 opj_read_double(l_src_data,&l_temp);
1320
1321                 l_src_data+=sizeof(OPJ_FLOAT64);
1322
1323                 *(l_dest_data++) = (OPJ_FLOAT32) l_temp;
1324         }
1325 }
1326
1327 void  opj_j2k_read_int16_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1328 {
1329         OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
1330         OPJ_INT32 * l_dest_data = (OPJ_INT32 *) p_dest_data;
1331         OPJ_UINT32 i;
1332         OPJ_UINT32 l_temp;
1333
1334         for (i=0;i<p_nb_elem;++i) {
1335                 opj_read_bytes(l_src_data,&l_temp,2);
1336
1337                 l_src_data+=sizeof(OPJ_INT16);
1338
1339                 *(l_dest_data++) = (OPJ_INT32) l_temp;
1340         }
1341 }
1342
1343 void  opj_j2k_read_int32_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1344 {
1345         OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
1346         OPJ_INT32 * l_dest_data = (OPJ_INT32 *) p_dest_data;
1347         OPJ_UINT32 i;
1348         OPJ_UINT32 l_temp;
1349
1350         for (i=0;i<p_nb_elem;++i) {
1351                 opj_read_bytes(l_src_data,&l_temp,4);
1352
1353                 l_src_data+=sizeof(OPJ_INT32);
1354
1355                 *(l_dest_data++) = (OPJ_INT32) l_temp;
1356         }
1357 }
1358
1359 void  opj_j2k_read_float32_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1360 {
1361         OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
1362         OPJ_INT32 * l_dest_data = (OPJ_INT32 *) p_dest_data;
1363         OPJ_UINT32 i;
1364         OPJ_FLOAT32 l_temp;
1365
1366         for (i=0;i<p_nb_elem;++i) {
1367                 opj_read_float(l_src_data,&l_temp);
1368
1369                 l_src_data+=sizeof(OPJ_FLOAT32);
1370
1371                 *(l_dest_data++) = (OPJ_INT32) l_temp;
1372         }
1373 }
1374
1375 void  opj_j2k_read_float64_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1376 {
1377         OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
1378         OPJ_INT32 * l_dest_data = (OPJ_INT32 *) p_dest_data;
1379         OPJ_UINT32 i;
1380         OPJ_FLOAT64 l_temp;
1381
1382         for (i=0;i<p_nb_elem;++i) {
1383                 opj_read_double(l_src_data,&l_temp);
1384
1385                 l_src_data+=sizeof(OPJ_FLOAT64);
1386
1387                 *(l_dest_data++) = (OPJ_INT32) l_temp;
1388         }
1389 }
1390
1391 void  opj_j2k_write_float_to_int16 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1392 {
1393         OPJ_BYTE * l_dest_data = (OPJ_BYTE *) p_dest_data;
1394         OPJ_FLOAT32 * l_src_data = (OPJ_FLOAT32 *) p_src_data;
1395         OPJ_UINT32 i;
1396         OPJ_UINT32 l_temp;
1397
1398         for (i=0;i<p_nb_elem;++i) {
1399                 l_temp = (OPJ_UINT32) *(l_src_data++);
1400
1401                 opj_write_bytes(l_dest_data,l_temp,sizeof(OPJ_INT16));
1402
1403                 l_dest_data+=sizeof(OPJ_INT16);
1404         }
1405 }
1406
1407 void opj_j2k_write_float_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1408 {
1409         OPJ_BYTE * l_dest_data = (OPJ_BYTE *) p_dest_data;
1410         OPJ_FLOAT32 * l_src_data = (OPJ_FLOAT32 *) p_src_data;
1411         OPJ_UINT32 i;
1412         OPJ_UINT32 l_temp;
1413
1414         for (i=0;i<p_nb_elem;++i) {
1415                 l_temp = (OPJ_UINT32) *(l_src_data++);
1416
1417                 opj_write_bytes(l_dest_data,l_temp,sizeof(OPJ_INT32));
1418
1419                 l_dest_data+=sizeof(OPJ_INT32);
1420         }
1421 }
1422
1423 void  opj_j2k_write_float_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1424 {
1425         OPJ_BYTE * l_dest_data = (OPJ_BYTE *) p_dest_data;
1426         OPJ_FLOAT32 * l_src_data = (OPJ_FLOAT32 *) p_src_data;
1427         OPJ_UINT32 i;
1428         OPJ_FLOAT32 l_temp;
1429
1430         for (i=0;i<p_nb_elem;++i) {
1431                 l_temp = (OPJ_FLOAT32) *(l_src_data++);
1432
1433                 opj_write_float(l_dest_data,l_temp);
1434
1435                 l_dest_data+=sizeof(OPJ_FLOAT32);
1436         }
1437 }
1438
1439 void  opj_j2k_write_float_to_float64 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1440 {
1441         OPJ_BYTE * l_dest_data = (OPJ_BYTE *) p_dest_data;
1442         OPJ_FLOAT32 * l_src_data = (OPJ_FLOAT32 *) p_src_data;
1443         OPJ_UINT32 i;
1444         OPJ_FLOAT64 l_temp;
1445
1446         for (i=0;i<p_nb_elem;++i) {
1447                 l_temp = (OPJ_FLOAT64) *(l_src_data++);
1448
1449                 opj_write_double(l_dest_data,l_temp);
1450
1451                 l_dest_data+=sizeof(OPJ_FLOAT64);
1452         }
1453 }
1454
1455 char *opj_j2k_convert_progression_order(OPJ_PROG_ORDER prg_order){
1456         j2k_prog_order_t *po;
1457         for(po = j2k_prog_order_list; po->enum_prog != -1; po++ ){
1458                 if(po->enum_prog == prg_order){
1459                         return po->str_prog;
1460                 }
1461         }
1462         return po->str_prog;
1463 }
1464
1465 OPJ_BOOL opj_j2k_check_poc_val( const opj_poc_t *p_pocs,
1466                                                         OPJ_UINT32 p_nb_pocs,
1467                                                         OPJ_UINT32 p_nb_resolutions,
1468                                                         OPJ_UINT32 p_num_comps,
1469                                                         OPJ_UINT32 p_num_layers,
1470                                                         opj_event_mgr_t * p_manager)
1471 {
1472         OPJ_UINT32* packet_array;
1473         OPJ_UINT32 index , resno, compno, layno;
1474         OPJ_UINT32 i;
1475         OPJ_UINT32 step_c = 1;
1476         OPJ_UINT32 step_r = p_num_comps * step_c;
1477         OPJ_UINT32 step_l = p_nb_resolutions * step_r;
1478         OPJ_BOOL loss = OPJ_FALSE;
1479         OPJ_UINT32 layno0 = 0;
1480
1481         packet_array = (OPJ_UINT32*) opj_calloc(step_l * p_num_layers, sizeof(OPJ_UINT32));
1482         if (packet_array == 00) {
1483                 opj_event_msg(p_manager , EVT_ERROR, "Not enough memory for checking the poc values.\n");
1484                 return OPJ_FALSE;
1485         }
1486         memset(packet_array,0,step_l * p_num_layers* sizeof(OPJ_UINT32));
1487
1488         if (p_nb_pocs == 0) {
1489                 return OPJ_TRUE;
1490         }
1491
1492         index = step_r * p_pocs->resno0;
1493         /* take each resolution for each poc */
1494         for (resno = p_pocs->resno0 ; resno < p_pocs->resno1 ; ++resno)
1495         {
1496                 OPJ_UINT32 res_index = index + p_pocs->compno0 * step_c;
1497
1498                 /* take each comp of each resolution for each poc */
1499                 for (compno = p_pocs->compno0 ; compno < p_pocs->compno1 ; ++compno) {
1500                         OPJ_UINT32 comp_index = res_index + layno0 * step_l;
1501
1502                         /* and finally take each layer of each res of ... */
1503                         for (layno = layno0; layno < p_pocs->layno1 ; ++layno) {
1504                                 /*index = step_r * resno + step_c * compno + step_l * layno;*/
1505                                 packet_array[comp_index] = 1;
1506                                 comp_index += step_l;
1507                         }
1508
1509                         res_index += step_c;
1510                 }
1511
1512                 index += step_r;
1513         }
1514         ++p_pocs;
1515
1516         /* iterate through all the pocs */
1517         for (i = 1; i < p_nb_pocs ; ++i) {
1518                 OPJ_UINT32 l_last_layno1 = (p_pocs-1)->layno1 ;
1519
1520                 layno0 = (p_pocs->layno1 > l_last_layno1)? l_last_layno1 : 0;
1521                 index = step_r * p_pocs->resno0;
1522
1523                 /* take each resolution for each poc */
1524                 for (resno = p_pocs->resno0 ; resno < p_pocs->resno1 ; ++resno) {
1525                         OPJ_UINT32 res_index = index + p_pocs->compno0 * step_c;
1526
1527                         /* take each comp of each resolution for each poc */
1528                         for (compno = p_pocs->compno0 ; compno < p_pocs->compno1 ; ++compno) {
1529                                 OPJ_UINT32 comp_index = res_index + layno0 * step_l;
1530
1531                                 /* and finally take each layer of each res of ... */
1532                                 for (layno = layno0; layno < p_pocs->layno1 ; ++layno) {
1533                                         /*index = step_r * resno + step_c * compno + step_l * layno;*/
1534                                         packet_array[comp_index] = 1;
1535                                         comp_index += step_l;
1536                                 }
1537
1538                                 res_index += step_c;
1539                         }
1540
1541                         index += step_r;
1542                 }
1543
1544                 ++p_pocs;
1545         }
1546
1547         index = 0;
1548         for (layno = 0; layno < p_num_layers ; ++layno) {
1549                 for (resno = 0; resno < p_nb_resolutions; ++resno) {
1550                         for (compno = 0; compno < p_num_comps; ++compno) {
1551                                 loss |= (packet_array[index]!=1);
1552                                 /*index = step_r * resno + step_c * compno + step_l * layno;*/
1553                                 index += step_c;
1554                         }
1555                 }
1556         }
1557
1558         if (loss) {
1559                 opj_event_msg(p_manager , EVT_ERROR, "Missing packets possible loss of data\n");
1560         }
1561
1562         opj_free(packet_array);
1563
1564         return !loss;
1565 }
1566
1567 /* ----------------------------------------------------------------------- */
1568
1569 OPJ_UINT32 opj_j2k_get_num_tp(opj_cp_t *cp, OPJ_UINT32 pino, OPJ_UINT32 tileno)
1570 {
1571         const OPJ_CHAR *prog = 00;
1572         OPJ_UINT32 i;
1573         OPJ_UINT32 tpnum = 1;
1574         opj_tcp_t *tcp = 00;
1575         opj_poc_t * l_current_poc = 00;
1576
1577         /*  preconditions */
1578         assert(tileno < (cp->tw * cp->th));
1579         assert(pino < (cp->tcps[tileno].numpocs + 1));
1580
1581         /* get the given tile coding parameter */
1582         tcp = &cp->tcps[tileno];
1583         assert(tcp != 00);
1584
1585         l_current_poc = &(tcp->pocs[pino]);
1586         assert(l_current_poc != 0);
1587
1588         /* get the progression order as a character string */
1589         prog = opj_j2k_convert_progression_order(tcp->prg);
1590         assert(strlen(prog) > 0);
1591
1592         if (cp->m_specific_param.m_enc.m_tp_on == 1) {
1593                 for (i=0;i<4;++i) {
1594                         switch (prog[i])
1595                         {
1596                                 /* component wise */
1597                                 case 'C':
1598                                         tpnum *= l_current_poc->compE;
1599                                         break;
1600                                 /* resolution wise */
1601                                 case 'R':
1602                                         tpnum *= l_current_poc->resE;
1603                                         break;
1604                                 /* precinct wise */
1605                                 case 'P':
1606                                         tpnum *= l_current_poc->prcE;
1607                                         break;
1608                                 /* layer wise */
1609                                 case 'L':
1610                                         tpnum *= l_current_poc->layE;
1611                                         break;
1612                         }
1613                         /* whould we split here ? */
1614                         if ( cp->m_specific_param.m_enc.m_tp_flag == prog[i] ) {
1615                                 cp->m_specific_param.m_enc.m_tp_pos=i;
1616                                 break;
1617                         }
1618                 }
1619         }
1620         else {
1621                 tpnum=1;
1622         }
1623
1624         return tpnum;
1625 }
1626
1627 OPJ_BOOL opj_j2k_calculate_tp(  opj_j2k_t *p_j2k,
1628                                                         opj_cp_t *cp,
1629                                                         OPJ_UINT32 * p_nb_tiles,
1630                                                         opj_image_t *image,
1631                                                         opj_event_mgr_t * p_manager
1632                                 )
1633 {
1634         OPJ_UINT32 pino,tileno;
1635         OPJ_UINT32 l_nb_tiles;
1636         opj_tcp_t *tcp;
1637
1638         /* preconditions */
1639         assert(p_nb_tiles != 00);
1640         assert(cp != 00);
1641         assert(image != 00);
1642         assert(p_j2k != 00);
1643         assert(p_manager != 00);
1644
1645         l_nb_tiles = cp->tw * cp->th;
1646         * p_nb_tiles = 0;
1647         tcp = cp->tcps;
1648
1649         /* INDEX >> */
1650         /* TODO mergeV2: check this part which use cstr_info */
1651         /*if (p_j2k->cstr_info) {
1652                 opj_tile_info_t * l_info_tile_ptr = p_j2k->cstr_info->tile;
1653
1654                 for (tileno = 0; tileno < l_nb_tiles; ++tileno) {
1655                         OPJ_UINT32 cur_totnum_tp = 0;
1656
1657                         opj_pi_update_encoding_parameters(image,cp,tileno);
1658
1659                         for (pino = 0; pino <= tcp->numpocs; ++pino)
1660                         {
1661                                 OPJ_UINT32 tp_num = opj_j2k_get_num_tp(cp,pino,tileno);
1662
1663                                 *p_nb_tiles = *p_nb_tiles + tp_num;
1664
1665                                 cur_totnum_tp += tp_num;
1666                         }
1667
1668                         tcp->m_nb_tile_parts = cur_totnum_tp;
1669
1670                         l_info_tile_ptr->tp = (opj_tp_info_t *) opj_malloc(cur_totnum_tp * sizeof(opj_tp_info_t));
1671                         if (l_info_tile_ptr->tp == 00) {
1672                                 return OPJ_FALSE;
1673                         }
1674
1675                         memset(l_info_tile_ptr->tp,0,cur_totnum_tp * sizeof(opj_tp_info_t));
1676
1677                         l_info_tile_ptr->num_tps = cur_totnum_tp;
1678
1679                         ++l_info_tile_ptr;
1680                         ++tcp;
1681                 }
1682         }
1683         else */{
1684                 for (tileno = 0; tileno < l_nb_tiles; ++tileno) {
1685                         OPJ_UINT32 cur_totnum_tp = 0;
1686
1687                         opj_pi_update_encoding_parameters(image,cp,tileno);
1688
1689                         for (pino = 0; pino <= tcp->numpocs; ++pino) {
1690                                 OPJ_UINT32 tp_num = opj_j2k_get_num_tp(cp,pino,tileno);
1691
1692                                 *p_nb_tiles = *p_nb_tiles + tp_num;
1693
1694                                 cur_totnum_tp += tp_num;
1695                         }
1696                         tcp->m_nb_tile_parts = cur_totnum_tp;
1697
1698                         ++tcp;
1699                 }
1700         }
1701
1702         return OPJ_TRUE;
1703 }
1704
1705 OPJ_BOOL opj_j2k_write_soc(     opj_j2k_t *p_j2k,
1706                                                 opj_stream_private_t *p_stream,
1707                                                     opj_event_mgr_t * p_manager )
1708 {
1709         /* 2 bytes will be written */
1710         OPJ_BYTE * l_start_stream = 00;
1711
1712         /* preconditions */
1713         assert(p_stream != 00);
1714         assert(p_j2k != 00);
1715         assert(p_manager != 00);
1716
1717         l_start_stream = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
1718
1719         /* write SOC identifier */
1720         opj_write_bytes(l_start_stream,J2K_MS_SOC,2);
1721
1722         if (opj_stream_write_data(p_stream,l_start_stream,2,p_manager) != 2) {
1723                 return OPJ_FALSE;
1724         }
1725
1726 /* UniPG>> */
1727 #ifdef USE_JPWL
1728         /* update markers struct */
1729 /*
1730         OPJ_BOOL res = j2k_add_marker(p_j2k->cstr_info, J2K_MS_SOC, p_stream_tell(p_stream) - 2, 2);
1731 */
1732   assert( 0 && "TODO" );
1733 #endif /* USE_JPWL */
1734 /* <<UniPG */
1735
1736         return OPJ_TRUE;
1737 }
1738
1739 /**
1740  * Reads a SOC marker (Start of Codestream)
1741  * @param       p_j2k           the jpeg2000 file codec.
1742  * @param       p_stream        FIXME DOC
1743  * @param       p_manager       the user event manager.
1744 */
1745 static OPJ_BOOL opj_j2k_read_soc(   opj_j2k_t *p_j2k,
1746                                     opj_stream_private_t *p_stream,
1747                                     opj_event_mgr_t * p_manager
1748                                     )
1749 {
1750         OPJ_BYTE l_data [2];
1751         OPJ_UINT32 l_marker;
1752
1753         /* preconditions */
1754         assert(p_j2k != 00);
1755         assert(p_manager != 00);
1756         assert(p_stream != 00);
1757
1758         if (opj_stream_read_data(p_stream,l_data,2,p_manager) != 2) {
1759                 return OPJ_FALSE;
1760         }
1761
1762         opj_read_bytes(l_data,&l_marker,2);
1763         if (l_marker != J2K_MS_SOC) {
1764                 return OPJ_FALSE;
1765         }
1766
1767         /* Next marker should be a SIZ marker in the main header */
1768         p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_MHSIZ;
1769
1770         /* FIXME move it in a index structure included in p_j2k*/
1771         p_j2k->cstr_index->main_head_start = opj_stream_tell(p_stream) - 2;
1772
1773         opj_event_msg(p_manager, EVT_INFO, "Start to read j2k main header (%d).\n", p_j2k->cstr_index->main_head_start);
1774
1775         /* Add the marker to the codestream index*/
1776         if (OPJ_FALSE == opj_j2k_add_mhmarker(p_j2k->cstr_index, J2K_MS_SOC, p_j2k->cstr_index->main_head_start, 2)) {
1777                 opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to add mh marker\n");
1778                 return OPJ_FALSE;
1779         }
1780         return OPJ_TRUE;
1781 }
1782
1783 OPJ_BOOL opj_j2k_write_siz(     opj_j2k_t *p_j2k,
1784                                                         opj_stream_private_t *p_stream,
1785                                                         opj_event_mgr_t * p_manager )
1786 {
1787         OPJ_UINT32 i;
1788         OPJ_UINT32 l_size_len;
1789         OPJ_BYTE * l_current_ptr;
1790         opj_image_t * l_image = 00;
1791         opj_cp_t *cp = 00;
1792         opj_image_comp_t * l_img_comp = 00;
1793
1794         /* preconditions */
1795         assert(p_stream != 00);
1796         assert(p_j2k != 00);
1797         assert(p_manager != 00);
1798
1799         l_image = p_j2k->m_private_image;
1800         cp = &(p_j2k->m_cp);
1801         l_size_len = 40 + 3 * l_image->numcomps;
1802         l_img_comp = l_image->comps;
1803
1804         if (l_size_len > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
1805
1806                 OPJ_BYTE *new_header_tile_data = (OPJ_BYTE *) opj_realloc(p_j2k->m_specific_param.m_encoder.m_header_tile_data, l_size_len);
1807                 if (! new_header_tile_data) {
1808                         opj_free(p_j2k->m_specific_param.m_encoder.m_header_tile_data);
1809                         p_j2k->m_specific_param.m_encoder.m_header_tile_data = NULL;
1810                         p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = 0;
1811                         opj_event_msg(p_manager, EVT_ERROR, "Not enough memory for the SIZ marker\n");
1812                         return OPJ_FALSE;
1813                 }
1814                 p_j2k->m_specific_param.m_encoder.m_header_tile_data = new_header_tile_data;
1815                 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_size_len;
1816         }
1817
1818         l_current_ptr = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
1819
1820         /* write SOC identifier */
1821         opj_write_bytes(l_current_ptr,J2K_MS_SIZ,2);    /* SIZ */
1822         l_current_ptr+=2;
1823
1824         opj_write_bytes(l_current_ptr,l_size_len-2,2); /* L_SIZ */
1825         l_current_ptr+=2;
1826
1827         opj_write_bytes(l_current_ptr, cp->rsiz, 2);    /* Rsiz (capabilities) */
1828         l_current_ptr+=2;
1829
1830         opj_write_bytes(l_current_ptr, l_image->x1, 4); /* Xsiz */
1831         l_current_ptr+=4;
1832
1833         opj_write_bytes(l_current_ptr, l_image->y1, 4); /* Ysiz */
1834         l_current_ptr+=4;
1835
1836         opj_write_bytes(l_current_ptr, l_image->x0, 4); /* X0siz */
1837         l_current_ptr+=4;
1838
1839         opj_write_bytes(l_current_ptr, l_image->y0, 4); /* Y0siz */
1840         l_current_ptr+=4;
1841
1842         opj_write_bytes(l_current_ptr, cp->tdx, 4);             /* XTsiz */
1843         l_current_ptr+=4;
1844
1845         opj_write_bytes(l_current_ptr, cp->tdy, 4);             /* YTsiz */
1846         l_current_ptr+=4;
1847
1848         opj_write_bytes(l_current_ptr, cp->tx0, 4);             /* XT0siz */
1849         l_current_ptr+=4;
1850
1851         opj_write_bytes(l_current_ptr, cp->ty0, 4);             /* YT0siz */
1852         l_current_ptr+=4;
1853
1854         opj_write_bytes(l_current_ptr, l_image->numcomps, 2);   /* Csiz */
1855         l_current_ptr+=2;
1856
1857         for (i = 0; i < l_image->numcomps; ++i) {
1858                 /* TODO here with MCT ? */
1859                 opj_write_bytes(l_current_ptr, l_img_comp->prec - 1 + (l_img_comp->sgnd << 7), 1);      /* Ssiz_i */
1860                 ++l_current_ptr;
1861
1862                 opj_write_bytes(l_current_ptr, l_img_comp->dx, 1);      /* XRsiz_i */
1863                 ++l_current_ptr;
1864
1865                 opj_write_bytes(l_current_ptr, l_img_comp->dy, 1);      /* YRsiz_i */
1866                 ++l_current_ptr;
1867
1868                 ++l_img_comp;
1869         }
1870
1871         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) {
1872                 return OPJ_FALSE;
1873         }
1874
1875         return OPJ_TRUE;
1876 }
1877
1878 /**
1879  * Reads a SIZ marker (image and tile size)
1880  * @param       p_j2k           the jpeg2000 file codec.
1881  * @param       p_header_data   the data contained in the SIZ box.
1882  * @param       p_header_size   the size of the data contained in the SIZ marker.
1883  * @param       p_manager       the user event manager.
1884 */
1885 static OPJ_BOOL opj_j2k_read_siz(opj_j2k_t *p_j2k,
1886                                  OPJ_BYTE * p_header_data,
1887                                  OPJ_UINT32 p_header_size,
1888                                  opj_event_mgr_t * p_manager
1889                                  )
1890 {
1891         OPJ_UINT32 i;
1892         OPJ_UINT32 l_nb_comp;
1893         OPJ_UINT32 l_nb_comp_remain;
1894         OPJ_UINT32 l_remaining_size;
1895         OPJ_UINT32 l_nb_tiles;
1896         OPJ_UINT32 l_tmp;
1897         opj_image_t *l_image = 00;
1898         opj_cp_t *l_cp = 00;
1899         opj_image_comp_t * l_img_comp = 00;
1900         opj_tcp_t * l_current_tile_param = 00;
1901
1902         /* preconditions */
1903         assert(p_j2k != 00);
1904         assert(p_manager != 00);
1905         assert(p_header_data != 00);
1906
1907         l_image = p_j2k->m_private_image;
1908         l_cp = &(p_j2k->m_cp);
1909
1910         /* minimum size == 39 - 3 (= minimum component parameter) */
1911         if (p_header_size < 36) {
1912                 opj_event_msg(p_manager, EVT_ERROR, "Error with SIZ marker size\n");
1913                 return OPJ_FALSE;
1914         }
1915
1916         l_remaining_size = p_header_size - 36;
1917         l_nb_comp = l_remaining_size / 3;
1918         l_nb_comp_remain = l_remaining_size % 3;
1919         if (l_nb_comp_remain != 0){
1920                 opj_event_msg(p_manager, EVT_ERROR, "Error with SIZ marker size\n");
1921                 return OPJ_FALSE;
1922         }
1923
1924         opj_read_bytes(p_header_data,&l_tmp ,2);                                                /* Rsiz (capabilities) */
1925         p_header_data+=2;
1926         l_cp->rsiz = (OPJ_RSIZ_CAPABILITIES) l_tmp;
1927         opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_image->x1, 4);   /* Xsiz */
1928         p_header_data+=4;
1929         opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_image->y1, 4);   /* Ysiz */
1930         p_header_data+=4;
1931         opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_image->x0, 4);   /* X0siz */
1932         p_header_data+=4;
1933         opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_image->y0, 4);   /* Y0siz */
1934         p_header_data+=4;
1935         opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_cp->tdx, 4);             /* XTsiz */
1936         p_header_data+=4;
1937         opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_cp->tdy, 4);             /* YTsiz */
1938         p_header_data+=4;
1939         opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_cp->tx0, 4);             /* XT0siz */
1940         p_header_data+=4;
1941         opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_cp->ty0, 4);             /* YT0siz */
1942         p_header_data+=4;
1943         opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_tmp, 2);                 /* Csiz */
1944         p_header_data+=2;
1945         if (l_tmp < 16385)
1946                 l_image->numcomps = (OPJ_UINT16) l_tmp;
1947         else {
1948                 opj_event_msg(p_manager, EVT_ERROR, "Error with SIZ marker: number of component is illegal -> %d\n", l_tmp);
1949                 return OPJ_FALSE;
1950         }
1951
1952         if (l_image->numcomps != l_nb_comp) {
1953                 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);
1954                 return OPJ_FALSE;
1955         }
1956
1957         /* testcase 4035.pdf.SIGSEGV.d8b.3375 */
1958         if (l_image->x0 > l_image->x1 || l_image->y0 > l_image->y1) {
1959                 opj_event_msg(p_manager, EVT_ERROR, "Error with SIZ marker: negative image size (%d x %d)\n", l_image->x1 - l_image->x0, l_image->y1 - l_image->y0);
1960                 return OPJ_FALSE;
1961         }
1962         /* testcase 2539.pdf.SIGFPE.706.1712 (also 3622.pdf.SIGFPE.706.2916 and 4008.pdf.SIGFPE.706.3345 and maybe more) */
1963         if (!(l_cp->tdx * l_cp->tdy)) {
1964                 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);
1965                 return OPJ_FALSE;
1966         }
1967
1968         /* testcase 1610.pdf.SIGSEGV.59c.681 */
1969         if (((OPJ_UINT64)l_image->x1) * ((OPJ_UINT64)l_image->y1) != (l_image->x1 * l_image->y1)) {
1970                 opj_event_msg(p_manager, EVT_ERROR, "Prevent buffer overflow (x1: %d, y1: %d)", l_image->x1, l_image->y1);
1971                 return OPJ_FALSE;
1972         }
1973
1974 #ifdef USE_JPWL
1975         if (l_cp->correct) {
1976                 /* if JPWL is on, we check whether TX errors have damaged
1977                   too much the SIZ parameters */
1978                 if (!(l_image->x1 * l_image->y1)) {
1979                         opj_event_msg(p_manager, EVT_ERROR,
1980                                 "JPWL: bad image size (%d x %d)\n",
1981                                 l_image->x1, l_image->y1);
1982                         if (!JPWL_ASSUME || JPWL_ASSUME) {
1983                                 opj_event_msg(p_manager, EVT_ERROR, "JPWL: giving up\n");
1984                                 return OPJ_FALSE;
1985                         }
1986                 }
1987
1988         /* FIXME check previously in the function so why keep this piece of code ? Need by the norm ?
1989                 if (l_image->numcomps != ((len - 38) / 3)) {
1990                         opj_event_msg(p_manager, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
1991                                 "JPWL: Csiz is %d => space in SIZ only for %d comps.!!!\n",
1992                                 l_image->numcomps, ((len - 38) / 3));
1993                         if (!JPWL_ASSUME) {
1994                                 opj_event_msg(p_manager, EVT_ERROR, "JPWL: giving up\n");
1995                                 return OPJ_FALSE;
1996                         }
1997         */              /* we try to correct */
1998         /*              opj_event_msg(p_manager, EVT_WARNING, "- trying to adjust this\n");
1999                         if (l_image->numcomps < ((len - 38) / 3)) {
2000                                 len = 38 + 3 * l_image->numcomps;
2001                                 opj_event_msg(p_manager, EVT_WARNING, "- setting Lsiz to %d => HYPOTHESIS!!!\n",
2002                                         len);
2003                         } else {
2004                                 l_image->numcomps = ((len - 38) / 3);
2005                                 opj_event_msg(p_manager, EVT_WARNING, "- setting Csiz to %d => HYPOTHESIS!!!\n",
2006                                         l_image->numcomps);
2007                         }
2008                 }
2009         */
2010
2011                 /* update components number in the jpwl_exp_comps filed */
2012                 l_cp->exp_comps = l_image->numcomps;
2013         }
2014 #endif /* USE_JPWL */
2015
2016         /* Allocate the resulting image components */
2017         l_image->comps = (opj_image_comp_t*) opj_calloc(l_image->numcomps, sizeof(opj_image_comp_t));
2018         if (l_image->comps == 00){
2019                 l_image->numcomps = 0;
2020                 opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to take in charge SIZ marker\n");
2021                 return OPJ_FALSE;
2022         }
2023
2024         memset(l_image->comps,0,l_image->numcomps * sizeof(opj_image_comp_t));
2025         l_img_comp = l_image->comps;
2026
2027         /* Read the component information */
2028         for (i = 0; i < l_image->numcomps; ++i){
2029                 OPJ_UINT32 tmp;
2030                 opj_read_bytes(p_header_data,&tmp,1);   /* Ssiz_i */
2031                 ++p_header_data;
2032                 l_img_comp->prec = (tmp & 0x7f) + 1;
2033                 l_img_comp->sgnd = tmp >> 7;
2034                 opj_read_bytes(p_header_data,&tmp,1);   /* XRsiz_i */
2035                 ++p_header_data;
2036                 l_img_comp->dx = (OPJ_INT32)tmp; /* should be between 1 and 255 */
2037                 opj_read_bytes(p_header_data,&tmp,1);   /* YRsiz_i */
2038                 ++p_header_data;
2039                 l_img_comp->dy = (OPJ_INT32)tmp; /* should be between 1 and 255 */
2040                 if( l_img_comp->dx < 1 || l_img_comp->dx > 255 ||
2041                     l_img_comp->dy < 1 || l_img_comp->dy > 255 ) {
2042                     opj_event_msg(p_manager, EVT_ERROR,
2043                                   "Invalid values for comp = %d : dx=%u dy=%u\n (should be between 1 and 255 according the JPEG2000 norm)",
2044                                   i, l_img_comp->dx, l_img_comp->dy);
2045                     return OPJ_FALSE;
2046                 }
2047
2048 #ifdef USE_JPWL
2049                 if (l_cp->correct) {
2050                 /* if JPWL is on, we check whether TX errors have damaged
2051                         too much the SIZ parameters, again */
2052                         if (!(l_image->comps[i].dx * l_image->comps[i].dy)) {
2053                                 opj_event_msg(p_manager, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
2054                                         "JPWL: bad XRsiz_%d/YRsiz_%d (%d x %d)\n",
2055                                         i, i, l_image->comps[i].dx, l_image->comps[i].dy);
2056                                 if (!JPWL_ASSUME) {
2057                                         opj_event_msg(p_manager, EVT_ERROR, "JPWL: giving up\n");
2058                                         return OPJ_FALSE;
2059                                 }
2060                                 /* we try to correct */
2061                                 opj_event_msg(p_manager, EVT_WARNING, "- trying to adjust them\n");
2062                                 if (!l_image->comps[i].dx) {
2063                                         l_image->comps[i].dx = 1;
2064                                         opj_event_msg(p_manager, EVT_WARNING, "- setting XRsiz_%d to %d => HYPOTHESIS!!!\n",
2065                                                 i, l_image->comps[i].dx);
2066                                 }
2067                                 if (!l_image->comps[i].dy) {
2068                                         l_image->comps[i].dy = 1;
2069                                         opj_event_msg(p_manager, EVT_WARNING, "- setting YRsiz_%d to %d => HYPOTHESIS!!!\n",
2070                                                 i, l_image->comps[i].dy);
2071                                 }
2072                         }
2073                 }
2074 #endif /* USE_JPWL */
2075                 l_img_comp->resno_decoded = 0;                                                          /* number of resolution decoded */
2076                 l_img_comp->factor = l_cp->m_specific_param.m_dec.m_reduce; /* reducing factor per component */
2077                 ++l_img_comp;
2078         }
2079
2080         /* Compute the number of tiles */
2081         l_cp->tw = opj_int_ceildiv(l_image->x1 - l_cp->tx0, l_cp->tdx);
2082         l_cp->th = opj_int_ceildiv(l_image->y1 - l_cp->ty0, l_cp->tdy);
2083
2084         /* Check that the number of tiles is valid */
2085         if (l_cp->tw == 0 || l_cp->th == 0 || l_cp->tw > 65535 / l_cp->th) {
2086             opj_event_msg(  p_manager, EVT_ERROR, 
2087                             "Invalid number of tiles : %u x %u (maximum fixed by jpeg2000 norm is 65535 tiles)\n",
2088                             l_cp->tw, l_cp->th);
2089             return OPJ_FALSE;
2090         }
2091         l_nb_tiles = l_cp->tw * l_cp->th;
2092
2093         /* Define the tiles which will be decoded */
2094         if (p_j2k->m_specific_param.m_decoder.m_discard_tiles) {
2095                 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;
2096                 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;
2097                 p_j2k->m_specific_param.m_decoder.m_end_tile_x = opj_int_ceildiv((p_j2k->m_specific_param.m_decoder.m_end_tile_x - l_cp->tx0), l_cp->tdx);
2098                 p_j2k->m_specific_param.m_decoder.m_end_tile_y = opj_int_ceildiv((p_j2k->m_specific_param.m_decoder.m_end_tile_y - l_cp->ty0), l_cp->tdy);
2099         }
2100         else {
2101                 p_j2k->m_specific_param.m_decoder.m_start_tile_x = 0;
2102                 p_j2k->m_specific_param.m_decoder.m_start_tile_y = 0;
2103                 p_j2k->m_specific_param.m_decoder.m_end_tile_x = l_cp->tw;
2104                 p_j2k->m_specific_param.m_decoder.m_end_tile_y = l_cp->th;
2105         }
2106
2107 #ifdef USE_JPWL
2108         if (l_cp->correct) {
2109                 /* if JPWL is on, we check whether TX errors have damaged
2110                   too much the SIZ parameters */
2111                 if ((l_cp->tw < 1) || (l_cp->th < 1) || (l_cp->tw > l_cp->max_tiles) || (l_cp->th > l_cp->max_tiles)) {
2112                         opj_event_msg(p_manager, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
2113                                 "JPWL: bad number of tiles (%d x %d)\n",
2114                                 l_cp->tw, l_cp->th);
2115                         if (!JPWL_ASSUME) {
2116                                 opj_event_msg(p_manager, EVT_ERROR, "JPWL: giving up\n");
2117                                 return OPJ_FALSE;
2118                         }
2119                         /* we try to correct */
2120                         opj_event_msg(p_manager, EVT_WARNING, "- trying to adjust them\n");
2121                         if (l_cp->tw < 1) {
2122                                 l_cp->tw= 1;
2123                                 opj_event_msg(p_manager, EVT_WARNING, "- setting %d tiles in x => HYPOTHESIS!!!\n",
2124                                                 l_cp->tw);
2125                         }
2126                         if (l_cp->tw > l_cp->max_tiles) {
2127                                 l_cp->tw= 1;
2128                                 opj_event_msg(p_manager, EVT_WARNING, "- too large x, increase expectance of %d\n"
2129                                         "- setting %d tiles in x => HYPOTHESIS!!!\n",
2130                                         l_cp->max_tiles, l_cp->tw);
2131                         }
2132                         if (l_cp->th < 1) {
2133                                 l_cp->th= 1;
2134                                 opj_event_msg(p_manager, EVT_WARNING, "- setting %d tiles in y => HYPOTHESIS!!!\n",
2135                                                 l_cp->th);
2136                         }
2137                         if (l_cp->th > l_cp->max_tiles) {
2138                                 l_cp->th= 1;
2139                                 opj_event_msg(p_manager, EVT_WARNING, "- too large y, increase expectance of %d to continue\n",
2140                                         "- setting %d tiles in y => HYPOTHESIS!!!\n",
2141                                         l_cp->max_tiles, l_cp->th);
2142                         }
2143                 }
2144         }
2145 #endif /* USE_JPWL */
2146
2147         /* memory allocations */
2148         l_cp->tcps = (opj_tcp_t*) opj_calloc(l_nb_tiles, sizeof(opj_tcp_t));
2149         if (l_cp->tcps == 00) {
2150                 opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to take in charge SIZ marker\n");
2151                 return OPJ_FALSE;
2152         }
2153         memset(l_cp->tcps,0,l_nb_tiles*sizeof(opj_tcp_t));
2154
2155 #ifdef USE_JPWL
2156         if (l_cp->correct) {
2157                 if (!l_cp->tcps) {
2158                         opj_event_msg(p_manager, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
2159                                 "JPWL: could not alloc tcps field of cp\n");
2160                         if (!JPWL_ASSUME || JPWL_ASSUME) {
2161                                 opj_event_msg(p_manager, EVT_ERROR, "JPWL: giving up\n");
2162                                 return OPJ_FALSE;
2163                         }
2164                 }
2165         }
2166 #endif /* USE_JPWL */
2167
2168         p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps =
2169                         (opj_tccp_t*) opj_calloc(l_image->numcomps, sizeof(opj_tccp_t));
2170         if(p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps  == 00) {
2171                 opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to take in charge SIZ marker\n");
2172                 return OPJ_FALSE;
2173         }
2174         memset(p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps ,0,l_image->numcomps*sizeof(opj_tccp_t));
2175
2176         p_j2k->m_specific_param.m_decoder.m_default_tcp->m_mct_records =
2177                         (opj_mct_data_t*)opj_malloc(OPJ_J2K_MCT_DEFAULT_NB_RECORDS * sizeof(opj_mct_data_t));
2178
2179         if (! p_j2k->m_specific_param.m_decoder.m_default_tcp->m_mct_records) {
2180                 opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to take in charge SIZ marker\n");
2181                 return OPJ_FALSE;
2182         }
2183         memset(p_j2k->m_specific_param.m_decoder.m_default_tcp->m_mct_records,0,OPJ_J2K_MCT_DEFAULT_NB_RECORDS * sizeof(opj_mct_data_t));
2184         p_j2k->m_specific_param.m_decoder.m_default_tcp->m_nb_max_mct_records = OPJ_J2K_MCT_DEFAULT_NB_RECORDS;
2185
2186         p_j2k->m_specific_param.m_decoder.m_default_tcp->m_mcc_records =
2187                         (opj_simple_mcc_decorrelation_data_t*)
2188                         opj_malloc(OPJ_J2K_MCC_DEFAULT_NB_RECORDS * sizeof(opj_simple_mcc_decorrelation_data_t));
2189
2190         if (! p_j2k->m_specific_param.m_decoder.m_default_tcp->m_mcc_records) {
2191                 opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to take in charge SIZ marker\n");
2192                 return OPJ_FALSE;
2193         }
2194         memset(p_j2k->m_specific_param.m_decoder.m_default_tcp->m_mcc_records,0,OPJ_J2K_MCC_DEFAULT_NB_RECORDS * sizeof(opj_simple_mcc_decorrelation_data_t));
2195         p_j2k->m_specific_param.m_decoder.m_default_tcp->m_nb_max_mcc_records = OPJ_J2K_MCC_DEFAULT_NB_RECORDS;
2196
2197         /* set up default dc level shift */
2198         for (i=0;i<l_image->numcomps;++i) {
2199                 if (! l_image->comps[i].sgnd) {
2200                         p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps[i].m_dc_level_shift = 1 << (l_image->comps[i].prec - 1);
2201                 }
2202         }
2203
2204         l_current_tile_param = l_cp->tcps;
2205         for     (i = 0; i < l_nb_tiles; ++i) {
2206                 l_current_tile_param->tccps = (opj_tccp_t*) opj_malloc(l_image->numcomps * sizeof(opj_tccp_t));
2207                 if (l_current_tile_param->tccps == 00) {
2208                         opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to take in charge SIZ marker\n");
2209                         return OPJ_FALSE;
2210                 }
2211                 memset(l_current_tile_param->tccps,0,l_image->numcomps * sizeof(opj_tccp_t));
2212
2213                 ++l_current_tile_param;
2214         }
2215
2216         p_j2k->m_specific_param.m_decoder.m_state =  J2K_STATE_MH; /* FIXME J2K_DEC_STATE_MH; */
2217         opj_image_comp_header_update(l_image,l_cp);
2218
2219         return OPJ_TRUE;
2220 }
2221
2222 OPJ_BOOL opj_j2k_write_com(     opj_j2k_t *p_j2k,
2223                                                         opj_stream_private_t *p_stream,
2224                                                         opj_event_mgr_t * p_manager
2225                             )
2226 {
2227         OPJ_UINT32 l_comment_size;
2228         OPJ_UINT32 l_total_com_size;
2229         const OPJ_CHAR *l_comment;
2230         OPJ_BYTE * l_current_ptr = 00;
2231
2232         /* preconditions */
2233         assert(p_j2k != 00);
2234         assert(p_stream != 00);
2235         assert(p_manager != 00);
2236
2237         l_comment = p_j2k->m_cp.comment;
2238         l_comment_size = strlen(l_comment);
2239         l_total_com_size = l_comment_size + 6;
2240
2241         if (l_total_com_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
2242                 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);
2243                 if (! new_header_tile_data) {
2244                         opj_free(p_j2k->m_specific_param.m_encoder.m_header_tile_data);
2245                         p_j2k->m_specific_param.m_encoder.m_header_tile_data = NULL;
2246                         p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = 0;
2247                         opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to write the COM marker\n");
2248                         return OPJ_FALSE;
2249                 }
2250                 p_j2k->m_specific_param.m_encoder.m_header_tile_data = new_header_tile_data;
2251                 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_total_com_size;
2252         }
2253
2254         l_current_ptr = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
2255
2256         opj_write_bytes(l_current_ptr,J2K_MS_COM , 2);  /* COM */
2257         l_current_ptr+=2;
2258
2259         opj_write_bytes(l_current_ptr,l_total_com_size - 2 , 2);        /* L_COM */
2260         l_current_ptr+=2;
2261
2262         opj_write_bytes(l_current_ptr,1 , 2);   /* General use (IS 8859-15:1999 (Latin) values) */
2263         l_current_ptr+=2;
2264
2265         memcpy( l_current_ptr,l_comment,l_comment_size);
2266
2267         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) {
2268                 return OPJ_FALSE;
2269         }
2270
2271         return OPJ_TRUE;
2272 }
2273
2274 /**
2275  * Reads a COM marker (comments)
2276  * @param       p_j2k           the jpeg2000 file codec.
2277  * @param       p_header_data   the data contained in the COM box.
2278  * @param       p_header_size   the size of the data contained in the COM marker.
2279  * @param       p_manager               the user event manager.
2280 */
2281 static OPJ_BOOL opj_j2k_read_com (  opj_j2k_t *p_j2k,
2282                                     OPJ_BYTE * p_header_data,
2283                                     OPJ_UINT32 p_header_size,
2284                                     opj_event_mgr_t * p_manager
2285                                     )
2286 {
2287         /* preconditions */
2288         assert(p_j2k != 00);
2289         assert(p_manager != 00);
2290         assert(p_header_data != 00);
2291   (void)p_header_size;
2292
2293         return OPJ_TRUE;
2294 }
2295
2296 OPJ_BOOL opj_j2k_write_cod(     opj_j2k_t *p_j2k,
2297                                                         opj_stream_private_t *p_stream,
2298                                                         opj_event_mgr_t * p_manager )
2299 {
2300         opj_cp_t *l_cp = 00;
2301         opj_tcp_t *l_tcp = 00;
2302         OPJ_UINT32 l_code_size,l_remaining_size;
2303         OPJ_BYTE * l_current_data = 00;
2304
2305         /* preconditions */
2306         assert(p_j2k != 00);
2307         assert(p_manager != 00);
2308         assert(p_stream != 00);
2309
2310         l_cp = &(p_j2k->m_cp);
2311         l_tcp = &l_cp->tcps[p_j2k->m_current_tile_number];
2312         l_code_size = 9 + opj_j2k_get_SPCod_SPCoc_size(p_j2k,p_j2k->m_current_tile_number,0);
2313         l_remaining_size = l_code_size;
2314
2315         if (l_code_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
2316                 OPJ_BYTE *new_header_tile_data = (OPJ_BYTE *) opj_realloc(p_j2k->m_specific_param.m_encoder.m_header_tile_data, l_code_size);
2317                 if (! new_header_tile_data) {
2318                         opj_free(p_j2k->m_specific_param.m_encoder.m_header_tile_data);
2319                         p_j2k->m_specific_param.m_encoder.m_header_tile_data = NULL;
2320                         p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = 0;
2321                         opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to write COD marker\n");
2322                         return OPJ_FALSE;
2323                 }
2324                 p_j2k->m_specific_param.m_encoder.m_header_tile_data = new_header_tile_data;
2325                 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_code_size;
2326         }
2327
2328         l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
2329
2330         opj_write_bytes(l_current_data,J2K_MS_COD,2);           /* COD */
2331         l_current_data += 2;
2332
2333         opj_write_bytes(l_current_data,l_code_size-2,2);        /* L_COD */
2334         l_current_data += 2;
2335
2336         opj_write_bytes(l_current_data,l_tcp->csty,1);          /* Scod */
2337         ++l_current_data;
2338
2339         opj_write_bytes(l_current_data,l_tcp->prg,1);           /* SGcod (A) */
2340         ++l_current_data;
2341
2342         opj_write_bytes(l_current_data,l_tcp->numlayers,2);     /* SGcod (B) */
2343         l_current_data+=2;
2344
2345         opj_write_bytes(l_current_data,l_tcp->mct,1);           /* SGcod (C) */
2346         ++l_current_data;
2347
2348         l_remaining_size -= 9;
2349
2350         if (! opj_j2k_write_SPCod_SPCoc(p_j2k,p_j2k->m_current_tile_number,0,l_current_data,&l_remaining_size,p_manager)) {
2351                 opj_event_msg(p_manager, EVT_ERROR, "Error writing COD marker\n");
2352                 return OPJ_FALSE;
2353         }
2354
2355         if (l_remaining_size != 0) {
2356                 opj_event_msg(p_manager, EVT_ERROR, "Error writing COD marker\n");
2357                 return OPJ_FALSE;
2358         }
2359
2360         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) {
2361                 return OPJ_FALSE;
2362         }
2363
2364         return OPJ_TRUE;
2365 }
2366
2367 /**
2368  * Reads a COD marker (Coding Styke defaults)
2369  * @param       p_header_data   the data contained in the COD box.
2370  * @param       p_j2k                   the jpeg2000 codec.
2371  * @param       p_header_size   the size of the data contained in the COD marker.
2372  * @param       p_manager               the user event manager.
2373 */
2374 static OPJ_BOOL opj_j2k_read_cod (  opj_j2k_t *p_j2k,
2375                                     OPJ_BYTE * p_header_data,
2376                                     OPJ_UINT32 p_header_size,
2377                                     opj_event_mgr_t * p_manager
2378                                     )
2379 {
2380         /* loop */
2381         OPJ_UINT32 i;
2382         OPJ_UINT32 l_tmp;
2383         opj_cp_t *l_cp = 00;
2384         opj_tcp_t *l_tcp = 00;
2385         opj_image_t *l_image = 00;
2386
2387         /* preconditions */
2388         assert(p_header_data != 00);
2389         assert(p_j2k != 00);
2390         assert(p_manager != 00);
2391
2392         l_image = p_j2k->m_private_image;
2393         l_cp = &(p_j2k->m_cp);
2394
2395         /* If we are in the first tile-part header of the current tile */
2396         l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ?
2397                                 &l_cp->tcps[p_j2k->m_current_tile_number] :
2398                                 p_j2k->m_specific_param.m_decoder.m_default_tcp;
2399
2400         /* Make sure room is sufficient */
2401         if (p_header_size < 5) {
2402                 opj_event_msg(p_manager, EVT_ERROR, "Error reading COD marker\n");
2403                 return OPJ_FALSE;
2404         }
2405
2406         opj_read_bytes(p_header_data,&l_tcp->csty,1);           /* Scod */
2407         ++p_header_data;
2408         opj_read_bytes(p_header_data,&l_tmp,1);                         /* SGcod (A) */
2409         ++p_header_data;
2410         l_tcp->prg = (OPJ_PROG_ORDER) l_tmp;
2411         opj_read_bytes(p_header_data,&l_tcp->numlayers,2);      /* SGcod (B) */
2412         p_header_data+=2;
2413
2414         /* If user didn't set a number layer to decode take the max specify in the codestream. */
2415         if      (l_cp->m_specific_param.m_dec.m_layer) {
2416                 l_tcp->num_layers_to_decode = l_cp->m_specific_param.m_dec.m_layer;
2417         }
2418         else {
2419                 l_tcp->num_layers_to_decode = l_tcp->numlayers;
2420         }
2421
2422         opj_read_bytes(p_header_data,&l_tcp->mct,1);            /* SGcod (C) */
2423         ++p_header_data;
2424
2425         p_header_size -= 5;
2426         for     (i = 0; i < l_image->numcomps; ++i) {
2427                 l_tcp->tccps[i].csty = l_tcp->csty & J2K_CCP_CSTY_PRT;
2428         }
2429
2430         if (! opj_j2k_read_SPCod_SPCoc(p_j2k,0,p_header_data,&p_header_size,p_manager)) {
2431                 opj_event_msg(p_manager, EVT_ERROR, "Error reading COD marker\n");
2432                 return OPJ_FALSE;
2433         }
2434
2435         if (p_header_size != 0) {
2436                 opj_event_msg(p_manager, EVT_ERROR, "Error reading COD marker\n");
2437                 return OPJ_FALSE;
2438         }
2439
2440         /* Apply the coding style to other components of the current tile or the m_default_tcp*/
2441         opj_j2k_copy_tile_component_parameters(p_j2k);
2442
2443         /* Index */
2444 #ifdef WIP_REMOVE_MSD
2445         if (p_j2k->cstr_info) {
2446                 /*opj_codestream_info_t *l_cstr_info = p_j2k->cstr_info;*/
2447                 p_j2k->cstr_info->prog = l_tcp->prg;
2448                 p_j2k->cstr_info->numlayers = l_tcp->numlayers;
2449                 p_j2k->cstr_info->numdecompos = (OPJ_INT32*) opj_malloc(l_image->numcomps * sizeof(OPJ_UINT32));
2450                 for     (i = 0; i < l_image->numcomps; ++i) {
2451                         p_j2k->cstr_info->numdecompos[i] = l_tcp->tccps[i].numresolutions - 1;
2452                 }
2453         }
2454 #endif
2455
2456         return OPJ_TRUE;
2457 }
2458
2459 OPJ_BOOL opj_j2k_write_coc( opj_j2k_t *p_j2k,
2460                                                 OPJ_UINT32 p_comp_no,
2461                                                 opj_stream_private_t *p_stream,
2462                                                 opj_event_mgr_t * p_manager )
2463 {
2464         OPJ_UINT32 l_coc_size,l_remaining_size;
2465         OPJ_UINT32 l_comp_room;
2466
2467         /* preconditions */
2468         assert(p_j2k != 00);
2469         assert(p_manager != 00);
2470         assert(p_stream != 00);
2471
2472         l_comp_room = (p_j2k->m_private_image->numcomps <= 256) ? 1 : 2;
2473
2474         l_coc_size = 5 + l_comp_room + opj_j2k_get_SPCod_SPCoc_size(p_j2k,p_j2k->m_current_tile_number,p_comp_no);
2475
2476         if (l_coc_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
2477                 OPJ_BYTE *new_header_tile_data;
2478                 /*p_j2k->m_specific_param.m_encoder.m_header_tile_data
2479                         = (OPJ_BYTE*)opj_realloc(
2480                                 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
2481                                 l_coc_size);*/
2482
2483                 new_header_tile_data = (OPJ_BYTE *) opj_realloc(p_j2k->m_specific_param.m_encoder.m_header_tile_data, l_coc_size);
2484                 if (! new_header_tile_data) {
2485                         opj_free(p_j2k->m_specific_param.m_encoder.m_header_tile_data);
2486                         p_j2k->m_specific_param.m_encoder.m_header_tile_data = NULL;
2487                         p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = 0;
2488                         opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to write COC marker\n");
2489                         return OPJ_FALSE;
2490                 }
2491                 p_j2k->m_specific_param.m_encoder.m_header_tile_data = new_header_tile_data;
2492                 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_coc_size;
2493         }
2494
2495         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);
2496
2497         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) {
2498                 return OPJ_FALSE;
2499         }
2500
2501         return OPJ_TRUE;
2502 }
2503
2504 void opj_j2k_write_coc_in_memory(   opj_j2k_t *p_j2k,
2505                                                 OPJ_UINT32 p_comp_no,
2506                                                 OPJ_BYTE * p_data,
2507                                                 OPJ_UINT32 * p_data_written,
2508                                                 opj_event_mgr_t * p_manager
2509                                     )
2510 {
2511         opj_cp_t *l_cp = 00;
2512         opj_tcp_t *l_tcp = 00;
2513         OPJ_UINT32 l_coc_size,l_remaining_size;
2514         OPJ_BYTE * l_current_data = 00;
2515         opj_image_t *l_image = 00;
2516         OPJ_UINT32 l_comp_room;
2517
2518         /* preconditions */
2519         assert(p_j2k != 00);
2520         assert(p_manager != 00);
2521
2522         l_cp = &(p_j2k->m_cp);
2523         l_tcp = &l_cp->tcps[p_j2k->m_current_tile_number];
2524         l_image = p_j2k->m_private_image;
2525         l_comp_room = (l_image->numcomps <= 256) ? 1 : 2;
2526
2527         l_coc_size = 5 + l_comp_room + opj_j2k_get_SPCod_SPCoc_size(p_j2k,p_j2k->m_current_tile_number,p_comp_no);
2528         l_remaining_size = l_coc_size;
2529
2530         l_current_data = p_data;
2531
2532         opj_write_bytes(l_current_data,J2K_MS_COC,2);                           /* COC */
2533         l_current_data += 2;
2534
2535         opj_write_bytes(l_current_data,l_coc_size-2,2);                         /* L_COC */
2536         l_current_data += 2;
2537
2538         opj_write_bytes(l_current_data,p_comp_no, l_comp_room);         /* Ccoc */
2539         l_current_data+=l_comp_room;
2540
2541         opj_write_bytes(l_current_data, l_tcp->tccps[p_comp_no].csty, 1);               /* Scoc */
2542         ++l_current_data;
2543
2544         l_remaining_size -= (5 + l_comp_room);
2545         opj_j2k_write_SPCod_SPCoc(p_j2k,p_j2k->m_current_tile_number,0,l_current_data,&l_remaining_size,p_manager);
2546         * p_data_written = l_coc_size;
2547 }
2548
2549 OPJ_UINT32 opj_j2k_get_max_coc_size(opj_j2k_t *p_j2k)
2550 {
2551         OPJ_UINT32 i,j;
2552         OPJ_UINT32 l_nb_comp;
2553         OPJ_UINT32 l_nb_tiles;
2554         OPJ_UINT32 l_max = 0;
2555
2556         /* preconditions */
2557
2558         l_nb_tiles = p_j2k->m_cp.tw * p_j2k->m_cp.th ;
2559         l_nb_comp = p_j2k->m_private_image->numcomps;
2560
2561         for (i=0;i<l_nb_tiles;++i) {
2562                 for (j=0;j<l_nb_comp;++j) {
2563                         l_max = opj_uint_max(l_max,opj_j2k_get_SPCod_SPCoc_size(p_j2k,i,j));
2564                 }
2565         }
2566
2567         return 6 + l_max;
2568 }
2569
2570 /**
2571  * Reads a COC marker (Coding Style Component)
2572  * @param       p_header_data   the data contained in the COC box.
2573  * @param       p_j2k                   the jpeg2000 codec.
2574  * @param       p_header_size   the size of the data contained in the COC marker.
2575  * @param       p_manager               the user event manager.
2576 */
2577 static OPJ_BOOL opj_j2k_read_coc (  opj_j2k_t *p_j2k,
2578                                     OPJ_BYTE * p_header_data,
2579                                     OPJ_UINT32 p_header_size,
2580                                     opj_event_mgr_t * p_manager
2581                                     )
2582 {
2583         opj_cp_t *l_cp = NULL;
2584         opj_tcp_t *l_tcp = NULL;
2585         opj_image_t *l_image = NULL;
2586         OPJ_UINT32 l_comp_room;
2587         OPJ_UINT32 l_comp_no;
2588
2589         /* preconditions */
2590         assert(p_header_data != 00);
2591         assert(p_j2k != 00);
2592         assert(p_manager != 00);
2593
2594         l_cp = &(p_j2k->m_cp);
2595         l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH ) ? /*FIXME J2K_DEC_STATE_TPH*/
2596                                 &l_cp->tcps[p_j2k->m_current_tile_number] :
2597                                 p_j2k->m_specific_param.m_decoder.m_default_tcp;
2598         l_image = p_j2k->m_private_image;
2599
2600         l_comp_room = l_image->numcomps <= 256 ? 1 : 2;
2601
2602         /* make sure room is sufficient*/
2603         if (p_header_size < l_comp_room + 1) {
2604                 opj_event_msg(p_manager, EVT_ERROR, "Error reading COC marker\n");
2605                 return OPJ_FALSE;
2606         }
2607         p_header_size -= l_comp_room + 1;
2608
2609         opj_read_bytes(p_header_data,&l_comp_no,l_comp_room);                   /* Ccoc */
2610         p_header_data += l_comp_room;
2611         if (l_comp_no >= l_image->numcomps) {
2612                 opj_event_msg(p_manager, EVT_ERROR, "Error reading COC marker (bad number of components)\n");
2613                 return OPJ_FALSE;
2614         }
2615
2616         opj_read_bytes(p_header_data,&l_tcp->tccps[l_comp_no].csty,1);                  /* Scoc */
2617         ++p_header_data ;
2618
2619         if (! opj_j2k_read_SPCod_SPCoc(p_j2k,l_comp_no,p_header_data,&p_header_size,p_manager)) {
2620                 opj_event_msg(p_manager, EVT_ERROR, "Error reading COC marker\n");
2621                 return OPJ_FALSE;
2622         }
2623
2624         if (p_header_size != 0) {
2625                 opj_event_msg(p_manager, EVT_ERROR, "Error reading COC marker\n");
2626                 return OPJ_FALSE;
2627         }
2628         return OPJ_TRUE;
2629 }
2630
2631 OPJ_BOOL opj_j2k_write_qcd(     opj_j2k_t *p_j2k,
2632                                                         opj_stream_private_t *p_stream,
2633                                                         opj_event_mgr_t * p_manager
2634                             )
2635 {
2636         OPJ_UINT32 l_qcd_size,l_remaining_size;
2637         OPJ_BYTE * l_current_data = 00;
2638
2639         /* preconditions */
2640         assert(p_j2k != 00);
2641         assert(p_manager != 00);
2642         assert(p_stream != 00);
2643
2644         l_qcd_size = 4 + opj_j2k_get_SQcd_SQcc_size(p_j2k,p_j2k->m_current_tile_number,0);
2645         l_remaining_size = l_qcd_size;
2646
2647         if (l_qcd_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
2648                 OPJ_BYTE *new_header_tile_data = (OPJ_BYTE *) opj_realloc(p_j2k->m_specific_param.m_encoder.m_header_tile_data, l_qcd_size);
2649                 if (! new_header_tile_data) {
2650                         opj_free(p_j2k->m_specific_param.m_encoder.m_header_tile_data);
2651                         p_j2k->m_specific_param.m_encoder.m_header_tile_data = NULL;
2652                         p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = 0;
2653                         opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to write QCD marker\n");
2654                         return OPJ_FALSE;
2655                 }
2656                 p_j2k->m_specific_param.m_encoder.m_header_tile_data = new_header_tile_data;
2657                 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_qcd_size;
2658         }
2659
2660         l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
2661
2662         opj_write_bytes(l_current_data,J2K_MS_QCD,2);           /* QCD */
2663         l_current_data += 2;
2664
2665         opj_write_bytes(l_current_data,l_qcd_size-2,2);         /* L_QCD */
2666         l_current_data += 2;
2667
2668         l_remaining_size -= 4;
2669
2670         if (! opj_j2k_write_SQcd_SQcc(p_j2k,p_j2k->m_current_tile_number,0,l_current_data,&l_remaining_size,p_manager)) {
2671                 opj_event_msg(p_manager, EVT_ERROR, "Error writing QCD marker\n");
2672                 return OPJ_FALSE;
2673         }
2674
2675         if (l_remaining_size != 0) {
2676                 opj_event_msg(p_manager, EVT_ERROR, "Error writing QCD marker\n");
2677                 return OPJ_FALSE;
2678         }
2679
2680         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) {
2681                 return OPJ_FALSE;
2682         }
2683
2684         return OPJ_TRUE;
2685 }
2686
2687 /**
2688  * Reads a QCD marker (Quantization defaults)
2689  * @param       p_header_data   the data contained in the QCD box.
2690  * @param       p_j2k                   the jpeg2000 codec.
2691  * @param       p_header_size   the size of the data contained in the QCD marker.
2692  * @param       p_manager               the user event manager.
2693 */
2694 static OPJ_BOOL opj_j2k_read_qcd (  opj_j2k_t *p_j2k,
2695                                     OPJ_BYTE * p_header_data,
2696                                     OPJ_UINT32 p_header_size,
2697                                     opj_event_mgr_t * p_manager
2698                                     )
2699 {
2700         /* preconditions */
2701         assert(p_header_data != 00);
2702         assert(p_j2k != 00);
2703         assert(p_manager != 00);
2704
2705         if (! opj_j2k_read_SQcd_SQcc(p_j2k,0,p_header_data,&p_header_size,p_manager)) {
2706                 opj_event_msg(p_manager, EVT_ERROR, "Error reading QCD marker\n");
2707                 return OPJ_FALSE;
2708         }
2709
2710         if (p_header_size != 0) {
2711                 opj_event_msg(p_manager, EVT_ERROR, "Error reading QCD marker\n");
2712                 return OPJ_FALSE;
2713         }
2714
2715         /* Apply the quantization parameters to other components of the current tile or the m_default_tcp */
2716         opj_j2k_copy_tile_quantization_parameters(p_j2k);
2717
2718         return OPJ_TRUE;
2719 }
2720
2721 OPJ_BOOL opj_j2k_write_qcc(     opj_j2k_t *p_j2k,
2722                                                 OPJ_UINT32 p_comp_no,
2723                                                 opj_stream_private_t *p_stream,
2724                                                 opj_event_mgr_t * p_manager
2725                             )
2726 {
2727         OPJ_UINT32 l_qcc_size,l_remaining_size;
2728
2729         /* preconditions */
2730         assert(p_j2k != 00);
2731         assert(p_manager != 00);
2732         assert(p_stream != 00);
2733
2734         l_qcc_size = 5 + opj_j2k_get_SQcd_SQcc_size(p_j2k,p_j2k->m_current_tile_number,p_comp_no);
2735         l_qcc_size += p_j2k->m_private_image->numcomps <= 256 ? 0:1;
2736         l_remaining_size = l_qcc_size;
2737
2738         if (l_qcc_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
2739                 OPJ_BYTE *new_header_tile_data = (OPJ_BYTE *) opj_realloc(p_j2k->m_specific_param.m_encoder.m_header_tile_data, l_qcc_size);
2740                 if (! new_header_tile_data) {
2741                         opj_free(p_j2k->m_specific_param.m_encoder.m_header_tile_data);
2742                         p_j2k->m_specific_param.m_encoder.m_header_tile_data = NULL;
2743                         p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = 0;
2744                         opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to write QCC marker\n");
2745                         return OPJ_FALSE;
2746                 }
2747                 p_j2k->m_specific_param.m_encoder.m_header_tile_data = new_header_tile_data;
2748                 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_qcc_size;
2749         }
2750
2751         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);
2752
2753         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) {
2754                 return OPJ_FALSE;
2755         }
2756
2757         return OPJ_TRUE;
2758 }
2759
2760 void opj_j2k_write_qcc_in_memory(   opj_j2k_t *p_j2k,
2761                                                                 OPJ_UINT32 p_comp_no,
2762                                                                 OPJ_BYTE * p_data,
2763                                                                 OPJ_UINT32 * p_data_written,
2764                                                                 opj_event_mgr_t * p_manager
2765                                     )
2766 {
2767         OPJ_UINT32 l_qcc_size,l_remaining_size;
2768         OPJ_BYTE * l_current_data = 00;
2769
2770         /* preconditions */
2771         assert(p_j2k != 00);
2772         assert(p_manager != 00);
2773
2774         l_qcc_size = 6 + opj_j2k_get_SQcd_SQcc_size(p_j2k,p_j2k->m_current_tile_number,p_comp_no);
2775         l_remaining_size = l_qcc_size;
2776
2777         l_current_data = p_data;
2778
2779         opj_write_bytes(l_current_data,J2K_MS_QCC,2);           /* QCC */
2780         l_current_data += 2;
2781
2782         if (p_j2k->m_private_image->numcomps <= 256) {
2783                 --l_qcc_size;
2784
2785                 opj_write_bytes(l_current_data,l_qcc_size-2,2);         /* L_QCC */
2786                 l_current_data += 2;
2787
2788                 opj_write_bytes(l_current_data, p_comp_no, 1);  /* Cqcc */
2789                 ++l_current_data;
2790
2791                 /* in the case only one byte is sufficient the last byte allocated is useless -> still do -6 for available */
2792                 l_remaining_size -= 6;
2793         }
2794         else {
2795                 opj_write_bytes(l_current_data,l_qcc_size-2,2);         /* L_QCC */
2796                 l_current_data += 2;
2797
2798                 opj_write_bytes(l_current_data, p_comp_no, 2);  /* Cqcc */
2799                 l_current_data+=2;
2800
2801                 l_remaining_size -= 6;
2802         }
2803
2804         opj_j2k_write_SQcd_SQcc(p_j2k,p_j2k->m_current_tile_number,p_comp_no,l_current_data,&l_remaining_size,p_manager);
2805
2806         *p_data_written = l_qcc_size;
2807 }
2808
2809 OPJ_UINT32 opj_j2k_get_max_qcc_size (opj_j2k_t *p_j2k)
2810 {
2811         return opj_j2k_get_max_coc_size(p_j2k);
2812 }
2813
2814 /**
2815  * Reads a QCC marker (Quantization component)
2816  * @param       p_header_data   the data contained in the QCC box.
2817  * @param       p_j2k                   the jpeg2000 codec.
2818  * @param       p_header_size   the size of the data contained in the QCC marker.
2819  * @param       p_manager               the user event manager.
2820 */
2821 static OPJ_BOOL opj_j2k_read_qcc(   opj_j2k_t *p_j2k,
2822                                     OPJ_BYTE * p_header_data,
2823                                     OPJ_UINT32 p_header_size,
2824                                     opj_event_mgr_t * p_manager
2825                                     )
2826 {
2827         OPJ_UINT32 l_num_comp,l_comp_no;
2828
2829         /* preconditions */
2830         assert(p_header_data != 00);
2831         assert(p_j2k != 00);
2832         assert(p_manager != 00);
2833
2834         l_num_comp = p_j2k->m_private_image->numcomps;
2835
2836         if (l_num_comp <= 256) {
2837                 if (p_header_size < 1) {
2838                         opj_event_msg(p_manager, EVT_ERROR, "Error reading QCC marker\n");
2839                         return OPJ_FALSE;
2840                 }
2841                 opj_read_bytes(p_header_data,&l_comp_no,1);
2842                 ++p_header_data;
2843                 --p_header_size;
2844         }
2845         else {
2846                 if (p_header_size < 2) {
2847                         opj_event_msg(p_manager, EVT_ERROR, "Error reading QCC marker\n");
2848                         return OPJ_FALSE;
2849                 }
2850                 opj_read_bytes(p_header_data,&l_comp_no,2);
2851                 p_header_data+=2;
2852                 p_header_size-=2;
2853         }
2854
2855 #ifdef USE_JPWL
2856         if (p_j2k->m_cp.correct) {
2857
2858                 static OPJ_UINT32 backup_compno = 0;
2859
2860                 /* compno is negative or larger than the number of components!!! */
2861                 if (/*(l_comp_no < 0) ||*/ (l_comp_no >= l_num_comp)) {
2862                         opj_event_msg(p_manager, EVT_ERROR,
2863                                 "JPWL: bad component number in QCC (%d out of a maximum of %d)\n",
2864                                 l_comp_no, l_num_comp);
2865                         if (!JPWL_ASSUME) {
2866                                 opj_event_msg(p_manager, EVT_ERROR, "JPWL: giving up\n");
2867                                 return OPJ_FALSE;
2868                         }
2869                         /* we try to correct */
2870                         l_comp_no = backup_compno % l_num_comp;
2871                         opj_event_msg(p_manager, EVT_WARNING, "- trying to adjust this\n"
2872                                 "- setting component number to %d\n",
2873                                 l_comp_no);
2874                 }
2875
2876                 /* keep your private count of tiles */
2877                 backup_compno++;
2878         };
2879 #endif /* USE_JPWL */
2880
2881         if (l_comp_no >= p_j2k->m_private_image->numcomps) {
2882                 opj_event_msg(p_manager, EVT_ERROR,
2883                               "Invalid component number: %d, regarding the number of components %d\n",
2884                               l_comp_no, p_j2k->m_private_image->numcomps);
2885                 return OPJ_FALSE;
2886         }
2887
2888         if (! opj_j2k_read_SQcd_SQcc(p_j2k,l_comp_no,p_header_data,&p_header_size,p_manager)) {
2889                 opj_event_msg(p_manager, EVT_ERROR, "Error reading QCC marker\n");
2890                 return OPJ_FALSE;
2891         }
2892
2893         if (p_header_size != 0) {
2894                 opj_event_msg(p_manager, EVT_ERROR, "Error reading QCC marker\n");
2895                 return OPJ_FALSE;
2896         }
2897
2898         return OPJ_TRUE;
2899 }
2900
2901 OPJ_BOOL opj_j2k_write_poc(     opj_j2k_t *p_j2k,
2902                                                         opj_stream_private_t *p_stream,
2903                                                         opj_event_mgr_t * p_manager
2904                             )
2905 {
2906         OPJ_UINT32 l_nb_comp;
2907         OPJ_UINT32 l_nb_poc;
2908         OPJ_UINT32 l_poc_size;
2909         OPJ_UINT32 l_written_size = 0;
2910         opj_tcp_t *l_tcp = 00;
2911         OPJ_UINT32 l_poc_room;
2912
2913         /* preconditions */
2914         assert(p_j2k != 00);
2915         assert(p_manager != 00);
2916         assert(p_stream != 00);
2917
2918         l_tcp = &p_j2k->m_cp.tcps[p_j2k->m_current_tile_number];
2919         l_nb_comp = p_j2k->m_private_image->numcomps;
2920         l_nb_poc = 1 + l_tcp->numpocs;
2921
2922         if (l_nb_comp <= 256) {
2923                 l_poc_room = 1;
2924         }
2925         else {
2926                 l_poc_room = 2;
2927         }
2928         l_poc_size = 4 + (5 + 2 * l_poc_room) * l_nb_poc;
2929
2930         if (l_poc_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
2931                 OPJ_BYTE *new_header_tile_data = (OPJ_BYTE *) opj_realloc(p_j2k->m_specific_param.m_encoder.m_header_tile_data, l_poc_size);
2932                 if (! new_header_tile_data) {
2933                         opj_free(p_j2k->m_specific_param.m_encoder.m_header_tile_data);
2934                         p_j2k->m_specific_param.m_encoder.m_header_tile_data = NULL;
2935                         p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = 0;
2936                         opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to write POC marker\n");
2937                         return OPJ_FALSE;
2938                 }
2939                 p_j2k->m_specific_param.m_encoder.m_header_tile_data = new_header_tile_data;
2940                 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_poc_size;
2941         }
2942
2943         opj_j2k_write_poc_in_memory(p_j2k,p_j2k->m_specific_param.m_encoder.m_header_tile_data,&l_written_size,p_manager);
2944
2945         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) {
2946                 return OPJ_FALSE;
2947         }
2948
2949         return OPJ_TRUE;
2950 }
2951
2952 void opj_j2k_write_poc_in_memory(   opj_j2k_t *p_j2k,
2953                                                                 OPJ_BYTE * p_data,
2954                                                                 OPJ_UINT32 * p_data_written,
2955                                                                 opj_event_mgr_t * p_manager
2956                                     )
2957 {
2958         OPJ_UINT32 i;
2959         OPJ_BYTE * l_current_data = 00;
2960         OPJ_UINT32 l_nb_comp;
2961         OPJ_UINT32 l_nb_poc;
2962         OPJ_UINT32 l_poc_size;
2963         opj_image_t *l_image = 00;
2964         opj_tcp_t *l_tcp = 00;
2965         opj_tccp_t *l_tccp = 00;
2966         opj_poc_t *l_current_poc = 00;
2967         OPJ_UINT32 l_poc_room;
2968
2969         /* preconditions */
2970         assert(p_j2k != 00);
2971         assert(p_manager != 00);
2972
2973         l_tcp = &p_j2k->m_cp.tcps[p_j2k->m_current_tile_number];
2974         l_tccp = &l_tcp->tccps[0];
2975         l_image = p_j2k->m_private_image;
2976         l_nb_comp = l_image->numcomps;
2977         l_nb_poc = 1 + l_tcp->numpocs;
2978
2979         if (l_nb_comp <= 256) {
2980                 l_poc_room = 1;
2981         }
2982         else {
2983                 l_poc_room = 2;
2984         }
2985
2986         l_poc_size = 4 + (5 + 2 * l_poc_room) * l_nb_poc;
2987
2988         l_current_data = p_data;
2989
2990         opj_write_bytes(l_current_data,J2K_MS_POC,2);                                   /* POC  */
2991         l_current_data += 2;
2992
2993         opj_write_bytes(l_current_data,l_poc_size-2,2);                                 /* Lpoc */
2994         l_current_data += 2;
2995
2996         l_current_poc =  l_tcp->pocs;
2997         for (i = 0; i < l_nb_poc; ++i) {
2998                 opj_write_bytes(l_current_data,l_current_poc->resno0,1);                                /* RSpoc_i */
2999                 ++l_current_data;
3000
3001                 opj_write_bytes(l_current_data,l_current_poc->compno0,l_poc_room);              /* CSpoc_i */
3002                 l_current_data+=l_poc_room;
3003
3004                 opj_write_bytes(l_current_data,l_current_poc->layno1,2);                                /* LYEpoc_i */
3005                 l_current_data+=2;
3006
3007                 opj_write_bytes(l_current_data,l_current_poc->resno1,1);                                /* REpoc_i */
3008                 ++l_current_data;
3009
3010                 opj_write_bytes(l_current_data,l_current_poc->compno1,l_poc_room);              /* CEpoc_i */
3011                 l_current_data+=l_poc_room;
3012
3013                 opj_write_bytes(l_current_data,l_current_poc->prg,1);                                   /* Ppoc_i */
3014                 ++l_current_data;
3015
3016                 /* change the value of the max layer according to the actual number of layers in the file, components and resolutions*/
3017                 l_current_poc->layno1 = opj_int_min(l_current_poc->layno1, l_tcp->numlayers);
3018                 l_current_poc->resno1 = opj_int_min(l_current_poc->resno1, l_tccp->numresolutions);
3019                 l_current_poc->compno1 = opj_int_min(l_current_poc->compno1, l_nb_comp);
3020
3021                 ++l_current_poc;
3022         }
3023
3024         *p_data_written = l_poc_size;
3025 }
3026
3027 OPJ_UINT32 opj_j2k_get_max_poc_size(opj_j2k_t *p_j2k)
3028 {
3029         opj_tcp_t * l_tcp = 00;
3030         OPJ_UINT32 l_nb_tiles = 0;
3031         OPJ_UINT32 l_max_poc = 0;
3032         OPJ_UINT32 i;
3033
3034         l_tcp = p_j2k->m_cp.tcps;
3035         l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw;
3036
3037         for (i=0;i<l_nb_tiles;++i) {
3038                 l_max_poc = opj_uint_max(l_max_poc,l_tcp->numpocs);
3039                 ++l_tcp;
3040         }
3041
3042         ++l_max_poc;
3043
3044         return 4 + 9 * l_max_poc;
3045 }
3046
3047 OPJ_UINT32 opj_j2k_get_max_toc_size (opj_j2k_t *p_j2k)
3048 {
3049         OPJ_UINT32 i;
3050         OPJ_UINT32 l_nb_tiles;
3051         OPJ_UINT32 l_max = 0;
3052         opj_tcp_t * l_tcp = 00;
3053
3054         l_tcp = p_j2k->m_cp.tcps;
3055         l_nb_tiles = p_j2k->m_cp.tw * p_j2k->m_cp.th ;
3056
3057         for (i=0;i<l_nb_tiles;++i) {
3058                 l_max = opj_uint_max(l_max,l_tcp->m_nb_tile_parts);
3059
3060                 ++l_tcp;
3061         }
3062
3063         return 12 * l_max;
3064 }
3065
3066 OPJ_UINT32 opj_j2k_get_specific_header_sizes(opj_j2k_t *p_j2k)
3067 {
3068         OPJ_UINT32 l_nb_bytes = 0;
3069         OPJ_UINT32 l_nb_comps;
3070         OPJ_UINT32 l_coc_bytes,l_qcc_bytes;
3071
3072         l_nb_comps = p_j2k->m_private_image->numcomps - 1;
3073         l_nb_bytes += opj_j2k_get_max_toc_size(p_j2k);
3074
3075         if (p_j2k->m_cp.m_specific_param.m_enc.m_cinema == 0) {
3076                 l_coc_bytes = opj_j2k_get_max_coc_size(p_j2k);
3077                 l_nb_bytes += l_nb_comps * l_coc_bytes;
3078
3079                 l_qcc_bytes = opj_j2k_get_max_qcc_size(p_j2k);
3080                 l_nb_bytes += l_nb_comps * l_qcc_bytes;
3081         }
3082
3083         l_nb_bytes += opj_j2k_get_max_poc_size(p_j2k);
3084
3085         /*** DEVELOPER CORNER, Add room for your headers ***/
3086
3087         return l_nb_bytes;
3088 }
3089
3090 /**
3091  * Reads a POC marker (Progression Order Change)
3092  *
3093  * @param       p_header_data   the data contained in the POC box.
3094  * @param       p_j2k                   the jpeg2000 codec.
3095  * @param       p_header_size   the size of the data contained in the POC marker.
3096  * @param       p_manager               the user event manager.
3097 */
3098 static OPJ_BOOL opj_j2k_read_poc (  opj_j2k_t *p_j2k,
3099                                     OPJ_BYTE * p_header_data,
3100                                     OPJ_UINT32 p_header_size,
3101                                     opj_event_mgr_t * p_manager
3102                                     )
3103 {
3104         OPJ_UINT32 i, l_nb_comp, l_tmp;
3105         opj_image_t * l_image = 00;
3106         OPJ_UINT32 l_old_poc_nb, l_current_poc_nb, l_current_poc_remaining;
3107         OPJ_UINT32 l_chunk_size, l_comp_room;
3108
3109         opj_cp_t *l_cp = 00;
3110         opj_tcp_t *l_tcp = 00;
3111         opj_poc_t *l_current_poc = 00;
3112
3113         /* preconditions */
3114         assert(p_header_data != 00);
3115         assert(p_j2k != 00);
3116         assert(p_manager != 00);
3117
3118         l_image = p_j2k->m_private_image;
3119         l_nb_comp = l_image->numcomps;
3120         if (l_nb_comp <= 256) {
3121                 l_comp_room = 1;
3122         }
3123         else {
3124                 l_comp_room = 2;
3125         }
3126         l_chunk_size = 5 + 2 * l_comp_room;
3127         l_current_poc_nb = p_header_size / l_chunk_size;
3128         l_current_poc_remaining = p_header_size % l_chunk_size;
3129
3130         if ((l_current_poc_nb <= 0) || (l_current_poc_remaining != 0)) {
3131                 opj_event_msg(p_manager, EVT_ERROR, "Error reading POC marker\n");
3132                 return OPJ_FALSE;
3133         }
3134
3135         l_cp = &(p_j2k->m_cp);
3136         l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ?
3137                                 &l_cp->tcps[p_j2k->m_current_tile_number] :
3138                                 p_j2k->m_specific_param.m_decoder.m_default_tcp;
3139         l_old_poc_nb = l_tcp->POC ? l_tcp->numpocs + 1 : 0;
3140         l_current_poc_nb += l_old_poc_nb;
3141
3142         assert(l_current_poc_nb < 32);
3143
3144         /* now poc is in use.*/
3145         l_tcp->POC = 1;
3146
3147         l_current_poc = &l_tcp->pocs[l_old_poc_nb];
3148         for     (i = l_old_poc_nb; i < l_current_poc_nb; ++i) {
3149                 opj_read_bytes(p_header_data,&(l_current_poc->resno0),1);                               /* RSpoc_i */
3150                 ++p_header_data;
3151                 opj_read_bytes(p_header_data,&(l_current_poc->compno0),l_comp_room);    /* CSpoc_i */
3152                 p_header_data+=l_comp_room;
3153                 opj_read_bytes(p_header_data,&(l_current_poc->layno1),2);                               /* LYEpoc_i */
3154                 p_header_data+=2;
3155                 opj_read_bytes(p_header_data,&(l_current_poc->resno1),1);                               /* REpoc_i */
3156                 ++p_header_data;
3157                 opj_read_bytes(p_header_data,&(l_current_poc->compno1),l_comp_room);    /* CEpoc_i */
3158                 p_header_data+=l_comp_room;
3159                 opj_read_bytes(p_header_data,&l_tmp,1);                                                                 /* Ppoc_i */
3160                 ++p_header_data;
3161                 l_current_poc->prg = (OPJ_PROG_ORDER) l_tmp;
3162                 /* make sure comp is in acceptable bounds */
3163                 l_current_poc->compno1 = opj_uint_min(l_current_poc->compno1, l_nb_comp);
3164                 ++l_current_poc;
3165         }
3166
3167         l_tcp->numpocs = l_current_poc_nb - 1;
3168         return OPJ_TRUE;
3169 }
3170
3171 /**
3172  * Reads a CRG marker (Component registration)
3173  *
3174  * @param       p_header_data   the data contained in the TLM box.
3175  * @param       p_j2k                   the jpeg2000 codec.
3176  * @param       p_header_size   the size of the data contained in the TLM marker.
3177  * @param       p_manager               the user event manager.
3178 */
3179 static OPJ_BOOL opj_j2k_read_crg (  opj_j2k_t *p_j2k,
3180                                     OPJ_BYTE * p_header_data,
3181                                     OPJ_UINT32 p_header_size,
3182                                     opj_event_mgr_t * p_manager
3183                                     )
3184 {
3185         OPJ_UINT32 l_nb_comp;
3186         /* preconditions */
3187         assert(p_header_data != 00);
3188         assert(p_j2k != 00);
3189         assert(p_manager != 00);
3190
3191         l_nb_comp = p_j2k->m_private_image->numcomps;
3192
3193         if (p_header_size != l_nb_comp *4) {
3194                 opj_event_msg(p_manager, EVT_ERROR, "Error reading CRG marker\n");
3195                 return OPJ_FALSE;
3196         }
3197         /* Do not care of this at the moment since only local variables are set here */
3198         /*
3199         for
3200                 (i = 0; i < l_nb_comp; ++i)
3201         {
3202                 opj_read_bytes(p_header_data,&l_Xcrg_i,2);                              // Xcrg_i
3203                 p_header_data+=2;
3204                 opj_read_bytes(p_header_data,&l_Ycrg_i,2);                              // Xcrg_i
3205                 p_header_data+=2;
3206         }
3207         */
3208         return OPJ_TRUE;
3209 }
3210
3211 /**
3212  * Reads a TLM marker (Tile Length Marker)
3213  *
3214  * @param       p_header_data   the data contained in the TLM box.
3215  * @param       p_j2k                   the jpeg2000 codec.
3216  * @param       p_header_size   the size of the data contained in the TLM marker.
3217  * @param       p_manager               the user event manager.
3218 */
3219 static OPJ_BOOL opj_j2k_read_tlm (  opj_j2k_t *p_j2k,
3220                                     OPJ_BYTE * p_header_data,
3221                                     OPJ_UINT32 p_header_size,
3222                                     opj_event_mgr_t * p_manager
3223                                     )
3224 {
3225         OPJ_UINT32 l_Ztlm, l_Stlm, l_ST, l_SP, l_tot_num_tp_remaining, l_quotient, l_Ptlm_size;
3226         /* preconditions */
3227         assert(p_header_data != 00);
3228         assert(p_j2k != 00);
3229         assert(p_manager != 00);
3230
3231         if (p_header_size < 2) {
3232                 opj_event_msg(p_manager, EVT_ERROR, "Error reading TLM marker\n");
3233                 return OPJ_FALSE;
3234         }
3235         p_header_size -= 2;
3236
3237         opj_read_bytes(p_header_data,&l_Ztlm,1);                                /* Ztlm */
3238         ++p_header_data;
3239         opj_read_bytes(p_header_data,&l_Stlm,1);                                /* Stlm */
3240         ++p_header_data;
3241
3242         l_ST = ((l_Stlm >> 4) & 0x3);
3243         l_SP = (l_Stlm >> 6) & 0x1;
3244
3245         l_Ptlm_size = (l_SP + 1) * 2;
3246         l_quotient = l_Ptlm_size + l_ST;
3247
3248         l_tot_num_tp_remaining = p_header_size % l_quotient;
3249
3250         if (l_tot_num_tp_remaining != 0) {
3251                 opj_event_msg(p_manager, EVT_ERROR, "Error reading TLM marker\n");
3252                 return OPJ_FALSE;
3253         }
3254         /* FIXME Do not care of this at the moment since only local variables are set here */
3255         /*
3256         for
3257                 (i = 0; i < l_tot_num_tp; ++i)
3258         {
3259                 opj_read_bytes(p_header_data,&l_Ttlm_i,l_ST);                           // Ttlm_i
3260                 p_header_data += l_ST;
3261                 opj_read_bytes(p_header_data,&l_Ptlm_i,l_Ptlm_size);            // Ptlm_i
3262                 p_header_data += l_Ptlm_size;
3263         }*/
3264         return OPJ_TRUE;
3265 }
3266
3267 /**
3268  * Reads a PLM marker (Packet length, main header marker)
3269  *
3270  * @param       p_header_data   the data contained in the TLM box.
3271  * @param       p_j2k                   the jpeg2000 codec.
3272  * @param       p_header_size   the size of the data contained in the TLM marker.
3273  * @param       p_manager               the user event manager.
3274 */
3275 static OPJ_BOOL opj_j2k_read_plm (  opj_j2k_t *p_j2k,
3276                                     OPJ_BYTE * p_header_data,
3277                                     OPJ_UINT32 p_header_size,
3278                                     opj_event_mgr_t * p_manager
3279                                     )
3280 {
3281         /* preconditions */
3282         assert(p_header_data != 00);
3283         assert(p_j2k != 00);
3284         assert(p_manager != 00);
3285
3286         if (p_header_size < 1) {
3287                 opj_event_msg(p_manager, EVT_ERROR, "Error reading PLM marker\n");
3288                 return OPJ_FALSE;
3289         }
3290         /* Do not care of this at the moment since only local variables are set here */
3291         /*
3292         opj_read_bytes(p_header_data,&l_Zplm,1);                                        // Zplm
3293         ++p_header_data;
3294         --p_header_size;
3295
3296         while
3297                 (p_header_size > 0)
3298         {
3299                 opj_read_bytes(p_header_data,&l_Nplm,1);                                // Nplm
3300                 ++p_header_data;
3301                 p_header_size -= (1+l_Nplm);
3302                 if
3303                         (p_header_size < 0)
3304                 {
3305                         opj_event_msg(p_manager, EVT_ERROR, "Error reading PLM marker\n");
3306                         return false;
3307                 }
3308                 for
3309                         (i = 0; i < l_Nplm; ++i)
3310                 {
3311                         opj_read_bytes(p_header_data,&l_tmp,1);                         // Iplm_ij
3312                         ++p_header_data;
3313                         // take only the last seven bytes
3314                         l_packet_len |= (l_tmp & 0x7f);
3315                         if
3316                                 (l_tmp & 0x80)
3317                         {
3318                                 l_packet_len <<= 7;
3319                         }
3320                         else
3321                         {
3322                 // store packet length and proceed to next packet
3323                                 l_packet_len = 0;
3324                         }
3325                 }
3326                 if
3327                         (l_packet_len != 0)
3328                 {
3329                         opj_event_msg(p_manager, EVT_ERROR, "Error reading PLM marker\n");
3330                         return false;
3331                 }
3332         }
3333         */
3334         return OPJ_TRUE;
3335 }
3336
3337 /**
3338  * Reads a PLT marker (Packet length, tile-part header)
3339  *
3340  * @param       p_header_data   the data contained in the PLT box.
3341  * @param       p_j2k                   the jpeg2000 codec.
3342  * @param       p_header_size   the size of the data contained in the PLT marker.
3343  * @param       p_manager               the user event manager.
3344 */
3345 static OPJ_BOOL opj_j2k_read_plt (  opj_j2k_t *p_j2k,
3346                                     OPJ_BYTE * p_header_data,
3347                                     OPJ_UINT32 p_header_size,
3348                                     opj_event_mgr_t * p_manager
3349                                     )
3350 {
3351         OPJ_UINT32 l_Zplt, l_tmp, l_packet_len = 0, i;
3352
3353         /* preconditions */
3354         assert(p_header_data != 00);
3355         assert(p_j2k != 00);
3356         assert(p_manager != 00);
3357
3358         if (p_header_size < 1) {
3359                 opj_event_msg(p_manager, EVT_ERROR, "Error reading PLT marker\n");
3360                 return OPJ_FALSE;
3361         }
3362
3363         opj_read_bytes(p_header_data,&l_Zplt,1);                /* Zplt */
3364         ++p_header_data;
3365         --p_header_size;
3366
3367         for (i = 0; i < p_header_size; ++i) {
3368                 opj_read_bytes(p_header_data,&l_tmp,1);         /* Iplt_ij */
3369                 ++p_header_data;
3370                 /* take only the last seven bytes */
3371                 l_packet_len |= (l_tmp & 0x7f);
3372                 if (l_tmp & 0x80) {
3373                         l_packet_len <<= 7;
3374                 }
3375                 else {
3376             /* store packet length and proceed to next packet */
3377                         l_packet_len = 0;
3378                 }
3379         }
3380
3381         if (l_packet_len != 0) {
3382                 opj_event_msg(p_manager, EVT_ERROR, "Error reading PLT marker\n");
3383                 return OPJ_FALSE;
3384         }
3385
3386         return OPJ_TRUE;
3387 }
3388
3389 #if 0
3390 OPJ_BOOL j2k_read_ppm_v2 (
3391                                                 opj_j2k_t *p_j2k,
3392                                                 OPJ_BYTE * p_header_data,
3393                                                 OPJ_UINT32 p_header_size,
3394                                                 struct opj_event_mgr * p_manager
3395                                         )
3396 {
3397
3398         opj_cp_t *l_cp = 00;
3399         OPJ_UINT32 l_remaining_data, l_Z_ppm, l_N_ppm;
3400
3401         /* preconditions */
3402         assert(p_header_data != 00);
3403         assert(p_j2k != 00);
3404         assert(p_manager != 00);
3405
3406         if (p_header_size < 1) {
3407                 opj_event_msg(p_manager, EVT_ERROR, "Error reading PPM marker\n");
3408                 return OPJ_FALSE;
3409         }
3410
3411         l_cp = &(p_j2k->m_cp);
3412         l_cp->ppm = 1;
3413
3414         opj_read_bytes(p_header_data,&l_Z_ppm,1);               /* Z_ppm */
3415         ++p_header_data;
3416         --p_header_size;
3417
3418         /* First PPM marker */
3419         if (l_Z_ppm == 0) {
3420                 if (p_header_size < 4) {
3421                         opj_event_msg(p_manager, EVT_ERROR, "Error reading PPM marker\n");
3422                         return OPJ_FALSE;
3423                 }
3424
3425                 opj_read_bytes(p_header_data,&l_N_ppm,4);               /* N_ppm */
3426                 p_header_data+=4;
3427                 p_header_size-=4;
3428
3429                 /* First PPM marker: Initialization */
3430                 l_cp->ppm_len = l_N_ppm;
3431                 l_cp->ppm_data_size = 0;
3432
3433                 l_cp->ppm_buffer = (OPJ_BYTE *) opj_malloc(l_cp->ppm_len);
3434                 if (l_cp->ppm_buffer == 00) {
3435                         opj_event_msg(p_manager, EVT_ERROR, "Not enough memory reading ppm marker\n");
3436                         return OPJ_FALSE;
3437                 }
3438                 memset(l_cp->ppm_buffer,0,l_cp->ppm_len);
3439
3440                 l_cp->ppm_data = l_cp->ppm_buffer;
3441         }
3442
3443         while (1) {
3444                 if (l_cp->ppm_data_size == l_cp->ppm_len) {
3445                         if (p_header_size >= 4) {
3446                                 /* read a N_ppm */
3447                                 opj_read_bytes(p_header_data,&l_N_ppm,4);               /* N_ppm */
3448                                 p_header_data+=4;
3449                                 p_header_size-=4;
3450                                 l_cp->ppm_len += l_N_ppm ;
3451
3452                                 OPJ_BYTE *new_ppm_buffer = (OPJ_BYTE *) opj_realloc(l_cp->ppm_buffer, l_cp->ppm_len);
3453                                 if (! new_ppm_buffer) {
3454                                         opj_free(l_cp->ppm_buffer);
3455                                         l_cp->ppm_buffer = NULL;
3456                                         l_cp->ppm_len = 0;
3457                                         l_cp->ppm_data = NULL;
3458                                         opj_event_msg(p_manager, EVT_ERROR, "Not enough memory reading ppm marker\n");
3459                                         return OPJ_FALSE;
3460                                 }
3461                                 l_cp->ppm_buffer = new_ppm_buffer;
3462                                 memset(l_cp->ppm_buffer+l_cp->ppm_data_size,0,l_N_ppm);
3463                                 l_cp->ppm_data = l_cp->ppm_buffer;
3464                         }
3465                         else {
3466                                 return OPJ_FALSE;
3467                         }
3468                 }
3469
3470                 l_remaining_data = l_cp->ppm_len - l_cp->ppm_data_size;
3471
3472                 if (l_remaining_data <= p_header_size) {
3473                         /* we must store less information than available in the packet */
3474                         memcpy(l_cp->ppm_buffer + l_cp->ppm_data_size , p_header_data , l_remaining_data);
3475                         l_cp->ppm_data_size = l_cp->ppm_len;
3476                         p_header_size -= l_remaining_data;
3477                         p_header_data += l_remaining_data;
3478                 }
3479                 else {
3480                         memcpy(l_cp->ppm_buffer + l_cp->ppm_data_size , p_header_data , p_header_size);
3481                         l_cp->ppm_data_size += p_header_size;
3482                         p_header_data += p_header_size;
3483                         p_header_size = 0;
3484                         break;
3485                 }
3486         }
3487
3488         return OPJ_TRUE;
3489 }
3490 #endif
3491
3492 OPJ_BOOL j2k_read_ppm_v3 (
3493                                                 opj_j2k_t *p_j2k,
3494                                                 OPJ_BYTE * p_header_data,
3495                                                 OPJ_UINT32 p_header_size,
3496                                                 struct opj_event_mgr * p_manager
3497                                         )
3498 {
3499         opj_cp_t *l_cp = 00;
3500         OPJ_UINT32 l_remaining_data, l_Z_ppm, l_N_ppm;
3501
3502         /* preconditions */
3503         assert(p_header_data != 00);
3504         assert(p_j2k != 00);
3505         assert(p_manager != 00);
3506
3507         /* Minimum size of PPM marker is equal to the size of Zppm element */
3508         if (p_header_size < 1) {
3509                 opj_event_msg(p_manager, EVT_ERROR, "Error reading PPM marker\n");
3510                 return OPJ_FALSE;
3511         }
3512
3513         l_cp = &(p_j2k->m_cp);
3514         l_cp->ppm = 1;
3515
3516         opj_read_bytes(p_header_data,&l_Z_ppm,1);               /* Z_ppm */
3517         ++p_header_data;
3518         --p_header_size;
3519
3520         /* First PPM marker */
3521         if (l_Z_ppm == 0) {
3522                 /* We need now at least the Nppm^0 element */
3523                 if (p_header_size < 4) {
3524                         opj_event_msg(p_manager, EVT_ERROR, "Error reading PPM marker\n");
3525                         return OPJ_FALSE;
3526                 }
3527
3528                 opj_read_bytes(p_header_data,&l_N_ppm,4);               /* First N_ppm */
3529                 p_header_data+=4;
3530                 p_header_size-=4;
3531
3532                 /* First PPM marker: Initialization */
3533                 l_cp->ppm_len = l_N_ppm;
3534                 l_cp->ppm_data_read = 0;
3535
3536                 l_cp->ppm_data = (OPJ_BYTE *) opj_malloc(l_cp->ppm_len);
3537                 l_cp->ppm_buffer = l_cp->ppm_data;
3538                 if (l_cp->ppm_data == 00) {
3539                         opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to read ppm marker\n");
3540                         return OPJ_FALSE;
3541                 }
3542                 memset(l_cp->ppm_data,0,l_cp->ppm_len);
3543
3544                 l_cp->ppm_data_current = l_cp->ppm_data;
3545
3546                 /*l_cp->ppm_data = l_cp->ppm_buffer;*/
3547         }
3548         else {
3549                 if (p_header_size < 4) {
3550                         opj_event_msg(p_manager, EVT_WARNING, "Empty PPM marker\n");
3551                         return OPJ_TRUE;
3552                 }
3553                 else {
3554                         /* Uncompleted Ippm series in the previous PPM marker?*/
3555                         if (l_cp->ppm_data_read < l_cp->ppm_len) {
3556                                 /* Get the place where add the remaining Ippm series*/
3557                                 l_cp->ppm_data_current = &(l_cp->ppm_data[l_cp->ppm_data_read]);
3558                                 l_N_ppm = l_cp->ppm_len - l_cp->ppm_data_read;
3559                         }
3560                         else {
3561                                 OPJ_BYTE *new_ppm_data;
3562                                 opj_read_bytes(p_header_data,&l_N_ppm,4);               /* First N_ppm */
3563                                 p_header_data+=4;
3564                                 p_header_size-=4;
3565
3566                                 /* Increase the size of ppm_data to add the new Ippm series*/
3567                                 assert(l_cp->ppm_data == l_cp->ppm_buffer && "We need ppm_data and ppm_buffer to be the same when reallocating");
3568                                 new_ppm_data = (OPJ_BYTE *) opj_realloc(l_cp->ppm_data, l_cp->ppm_len + l_N_ppm);
3569                                 if (! new_ppm_data) {
3570                                         opj_free(l_cp->ppm_data);
3571                                         l_cp->ppm_data = NULL;
3572                                         l_cp->ppm_buffer = NULL;  /* TODO: no need for a new local variable: ppm_buffer and ppm_data are enough */
3573                                         l_cp->ppm_len = 0;
3574                                         opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to increase the size of ppm_data to add the new Ippm series\n");
3575                                         return OPJ_FALSE;
3576                                 }
3577                                 l_cp->ppm_data = new_ppm_data;
3578                                 l_cp->ppm_buffer = l_cp->ppm_data;
3579
3580                                 /* Keep the position of the place where concatenate the new series*/
3581                                 l_cp->ppm_data_current = &(l_cp->ppm_data[l_cp->ppm_len]);
3582                                 l_cp->ppm_len += l_N_ppm;
3583                         }
3584                 }
3585         }
3586
3587         l_remaining_data = p_header_size;
3588
3589         while (l_remaining_data >= l_N_ppm) {
3590                 /* read a complete Ippm series*/
3591                 memcpy(l_cp->ppm_data_current, p_header_data, l_N_ppm);
3592                 p_header_size -= l_N_ppm;
3593                 p_header_data += l_N_ppm;
3594
3595                 l_cp->ppm_data_read += l_N_ppm; /* Increase the number of data read*/
3596
3597                 if (p_header_size)
3598                 {
3599                         opj_read_bytes(p_header_data,&l_N_ppm,4);               /* N_ppm^i */
3600                         p_header_data+=4;
3601                         p_header_size-=4;
3602                 }
3603                 else {
3604                         l_remaining_data = p_header_size;
3605                         break;
3606                 }
3607
3608                 l_remaining_data = p_header_size;
3609
3610                 /* Next Ippm series is a complete series ?*/
3611                 if (l_remaining_data > l_N_ppm) {
3612                         OPJ_BYTE *new_ppm_data;
3613                         /* Increase the size of ppm_data to add the new Ippm series*/
3614                         assert(l_cp->ppm_data == l_cp->ppm_buffer && "We need ppm_data and ppm_buffer to be the same when reallocating");
3615                         new_ppm_data = (OPJ_BYTE *) opj_realloc(l_cp->ppm_data, l_cp->ppm_len + l_N_ppm);
3616                         if (! new_ppm_data) {
3617                                 opj_free(l_cp->ppm_data);
3618                                 l_cp->ppm_data = NULL;
3619                                 l_cp->ppm_buffer = NULL;  /* TODO: no need for a new local variable: ppm_buffer and ppm_data are enough */
3620                                 l_cp->ppm_len = 0;
3621                                 opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to increase the size of ppm_data to add the new (complete) Ippm series\n");
3622                                 return OPJ_FALSE;
3623                         }
3624                         l_cp->ppm_data = new_ppm_data;
3625                         l_cp->ppm_buffer = l_cp->ppm_data;
3626
3627                         /* Keep the position of the place where concatenate the new series */
3628                         l_cp->ppm_data_current = &(l_cp->ppm_data[l_cp->ppm_len]);
3629                         l_cp->ppm_len += l_N_ppm;
3630                 }
3631
3632         }
3633
3634         /* Need to read an incomplete Ippm series*/
3635         if (l_remaining_data) {
3636                 OPJ_BYTE *new_ppm_data;
3637                 assert(l_cp->ppm_data == l_cp->ppm_buffer && "We need ppm_data and ppm_buffer to be the same when reallocating");
3638                 new_ppm_data = (OPJ_BYTE *) opj_realloc(l_cp->ppm_data, l_cp->ppm_len + l_N_ppm);
3639                 if (! new_ppm_data) {
3640                         opj_free(l_cp->ppm_data);
3641                         l_cp->ppm_data = NULL;
3642                         l_cp->ppm_buffer = NULL;  /* TODO: no need for a new local variable: ppm_buffer and ppm_data are enough */
3643                         l_cp->ppm_len = 0;
3644                         opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to increase the size of ppm_data to add the new (incomplete) Ippm series\n");
3645                         return OPJ_FALSE;
3646                 }
3647                 l_cp->ppm_data = new_ppm_data;
3648                 l_cp->ppm_buffer = l_cp->ppm_data;
3649
3650                 /* Keep the position of the place where concatenate the new series*/
3651                 l_cp->ppm_data_current = &(l_cp->ppm_data[l_cp->ppm_len]);
3652                 l_cp->ppm_len += l_N_ppm;
3653
3654                 /* Read incomplete Ippm series*/
3655                 memcpy(l_cp->ppm_data_current, p_header_data, l_remaining_data);
3656                 p_header_size -= l_remaining_data;
3657                 p_header_data += l_remaining_data;
3658
3659                 l_cp->ppm_data_read += l_remaining_data; /* Increase the number of data read*/
3660         }
3661
3662 #ifdef CLEAN_MSD
3663
3664                 if (l_cp->ppm_data_size == l_cp->ppm_len) {
3665                         if (p_header_size >= 4) {
3666                                 /* read a N_ppm*/
3667                                 opj_read_bytes(p_header_data,&l_N_ppm,4);               /* N_ppm */
3668                                 p_header_data+=4;
3669                                 p_header_size-=4;
3670                                 l_cp->ppm_len += l_N_ppm ;
3671
3672                                 OPJ_BYTE *new_ppm_buffer = (OPJ_BYTE *) opj_realloc(l_cp->ppm_buffer, l_cp->ppm_len);
3673                                 if (! new_ppm_buffer) {
3674                                         opj_free(l_cp->ppm_buffer);
3675                                         l_cp->ppm_buffer = NULL;
3676                                         l_cp->ppm_len = 0;
3677                                         opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to read ppm marker\n");
3678                                         return OPJ_FALSE;
3679                                 }
3680                                 l_cp->ppm_buffer = new_ppm_buffer;
3681                                 memset(l_cp->ppm_buffer+l_cp->ppm_data_size,0,l_N_ppm);
3682
3683                                 l_cp->ppm_data = l_cp->ppm_buffer;
3684                         }
3685                         else {
3686                                 return OPJ_FALSE;
3687                         }
3688                 }
3689
3690                 l_remaining_data = l_cp->ppm_len - l_cp->ppm_data_size;
3691
3692                 if (l_remaining_data <= p_header_size) {
3693                         /* we must store less information than available in the packet */
3694                         memcpy(l_cp->ppm_buffer + l_cp->ppm_data_size , p_header_data , l_remaining_data);
3695                         l_cp->ppm_data_size = l_cp->ppm_len;
3696                         p_header_size -= l_remaining_data;
3697                         p_header_data += l_remaining_data;
3698                 }
3699                 else {
3700                         memcpy(l_cp->ppm_buffer + l_cp->ppm_data_size , p_header_data , p_header_size);
3701                         l_cp->ppm_data_size += p_header_size;
3702                         p_header_data += p_header_size;
3703                         p_header_size = 0;
3704                         break;
3705                 }
3706         }
3707 #endif
3708         return OPJ_TRUE;
3709 }
3710
3711 /**
3712  * Reads a PPT marker (Packed packet headers, tile-part header)
3713  *
3714  * @param       p_header_data   the data contained in the PPT box.
3715  * @param       p_j2k                   the jpeg2000 codec.
3716  * @param       p_header_size   the size of the data contained in the PPT marker.
3717  * @param       p_manager               the user event manager.
3718 */
3719 static OPJ_BOOL opj_j2k_read_ppt (  opj_j2k_t *p_j2k,
3720                                     OPJ_BYTE * p_header_data,
3721                                     OPJ_UINT32 p_header_size,
3722                                     opj_event_mgr_t * p_manager
3723                                     )
3724 {
3725         opj_cp_t *l_cp = 00;
3726         opj_tcp_t *l_tcp = 00;
3727         OPJ_UINT32 l_Z_ppt;
3728
3729         /* preconditions */
3730         assert(p_header_data != 00);
3731         assert(p_j2k != 00);
3732         assert(p_manager != 00);
3733
3734         /* We need to have the Z_ppt element at minimum */
3735         if (p_header_size < 1) {
3736                 opj_event_msg(p_manager, EVT_ERROR, "Error reading PPT marker\n");
3737                 return OPJ_FALSE;
3738         }
3739
3740         l_cp = &(p_j2k->m_cp);
3741         if (l_cp->ppm){
3742                 opj_event_msg(p_manager, EVT_ERROR, "Error reading PPT marker: packet header have been previously found in the main header (PPM marker).\n");
3743                 return OPJ_FALSE;
3744         }
3745
3746         l_tcp = &(l_cp->tcps[p_j2k->m_current_tile_number]);
3747         l_tcp->ppt = 1;
3748
3749         opj_read_bytes(p_header_data,&l_Z_ppt,1);               /* Z_ppt */
3750         ++p_header_data;
3751         --p_header_size;
3752
3753         /* Allocate buffer to read the packet header */
3754         if (l_Z_ppt == 0) {
3755                 /* First PPT marker */
3756                 l_tcp->ppt_data_size = 0;
3757                 l_tcp->ppt_len = p_header_size;
3758
3759                 l_tcp->ppt_buffer = (OPJ_BYTE *) opj_calloc(l_tcp->ppt_len, sizeof(OPJ_BYTE) );
3760                 if (l_tcp->ppt_buffer == 00) {
3761                         opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to read PPT marker\n");
3762                         return OPJ_FALSE;
3763                 }
3764                 l_tcp->ppt_data = l_tcp->ppt_buffer;
3765
3766                 /* memset(l_tcp->ppt_buffer,0,l_tcp->ppt_len); */
3767         }
3768         else {
3769                 OPJ_BYTE *new_ppt_buffer;
3770                 l_tcp->ppt_len += p_header_size;
3771
3772                 new_ppt_buffer = (OPJ_BYTE *) opj_realloc(l_tcp->ppt_buffer, l_tcp->ppt_len);
3773                 if (! new_ppt_buffer) {
3774                         opj_free(l_tcp->ppt_buffer);
3775                         l_tcp->ppt_buffer = NULL;
3776                         l_tcp->ppt_len = 0;
3777                         opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to read PPT marker\n");
3778                         return OPJ_FALSE;
3779                 }
3780                 l_tcp->ppt_buffer = new_ppt_buffer;
3781                 l_tcp->ppt_data = l_tcp->ppt_buffer;
3782
3783                 memset(l_tcp->ppt_buffer+l_tcp->ppt_data_size,0,p_header_size);
3784         }
3785
3786         /* Read packet header from buffer */
3787         memcpy(l_tcp->ppt_buffer+l_tcp->ppt_data_size,p_header_data,p_header_size);
3788
3789         l_tcp->ppt_data_size += p_header_size;
3790
3791         return OPJ_TRUE;
3792 }
3793
3794 OPJ_BOOL opj_j2k_write_tlm(     opj_j2k_t *p_j2k,
3795                                                         opj_stream_private_t *p_stream,
3796                                                         opj_event_mgr_t * p_manager
3797                             )
3798 {
3799         OPJ_BYTE * l_current_data = 00;
3800         OPJ_UINT32 l_tlm_size;
3801
3802         /* preconditions */
3803         assert(p_j2k != 00);
3804         assert(p_manager != 00);
3805         assert(p_stream != 00);
3806
3807         l_tlm_size = 6 + (5*p_j2k->m_specific_param.m_encoder.m_total_tile_parts);
3808
3809         if (l_tlm_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
3810                 OPJ_BYTE *new_header_tile_data = (OPJ_BYTE *) opj_realloc(p_j2k->m_specific_param.m_encoder.m_header_tile_data, l_tlm_size);
3811                 if (! new_header_tile_data) {
3812                         opj_free(p_j2k->m_specific_param.m_encoder.m_header_tile_data);
3813                         p_j2k->m_specific_param.m_encoder.m_header_tile_data = NULL;
3814                         p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = 0;
3815                         opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to write TLM marker\n");
3816                         return OPJ_FALSE;
3817                 }
3818                 p_j2k->m_specific_param.m_encoder.m_header_tile_data = new_header_tile_data;
3819                 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_tlm_size;
3820         }
3821
3822         l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
3823
3824         /* change the way data is written to avoid seeking if possible */
3825         /* TODO */
3826         p_j2k->m_specific_param.m_encoder.m_tlm_start = opj_stream_tell(p_stream);
3827
3828         opj_write_bytes(l_current_data,J2K_MS_TLM,2);                                   /* TLM */
3829         l_current_data += 2;
3830
3831         opj_write_bytes(l_current_data,l_tlm_size-2,2);                                 /* Lpoc */
3832         l_current_data += 2;
3833
3834         opj_write_bytes(l_current_data,0,1);                                                    /* Ztlm=0*/
3835         ++l_current_data;
3836
3837         opj_write_bytes(l_current_data,0x50,1);                                                 /* Stlm ST=1(8bits-255 tiles max),SP=1(Ptlm=32bits) */
3838         ++l_current_data;
3839
3840         /* do nothing on the 5 * l_j2k->m_specific_param.m_encoder.m_total_tile_parts remaining data */
3841         if (opj_stream_write_data(p_stream,p_j2k->m_specific_param.m_encoder.m_header_tile_data,l_tlm_size,p_manager) != l_tlm_size) {
3842                 return OPJ_FALSE;
3843         }
3844
3845         return OPJ_TRUE;
3846 }
3847
3848 OPJ_BOOL opj_j2k_write_sot(     opj_j2k_t *p_j2k,
3849                                                         OPJ_BYTE * p_data,
3850                                                         OPJ_UINT32 * p_data_written,
3851                                                         const opj_stream_private_t *p_stream,
3852                                                         opj_event_mgr_t * p_manager
3853                             )
3854 {
3855         /* preconditions */
3856         assert(p_j2k != 00);
3857         assert(p_manager != 00);
3858         assert(p_stream != 00);
3859
3860         opj_write_bytes(p_data,J2K_MS_SOT,2);                                   /* SOT */
3861         p_data += 2;
3862
3863         opj_write_bytes(p_data,10,2);                                                   /* Lsot */
3864         p_data += 2;
3865
3866         opj_write_bytes(p_data, p_j2k->m_current_tile_number,2);                        /* Isot */
3867         p_data += 2;
3868
3869         /* Psot  */
3870         p_data += 4;
3871
3872         opj_write_bytes(p_data, p_j2k->m_specific_param.m_encoder.m_current_tile_part_number,1);                        /* TPsot */
3873         ++p_data;
3874
3875         opj_write_bytes(p_data, p_j2k->m_cp.tcps[p_j2k->m_current_tile_number].m_nb_tile_parts,1);                      /* TNsot */
3876         ++p_data;
3877
3878         /* UniPG>> */
3879 #ifdef USE_JPWL
3880         /* update markers struct */
3881 /*
3882         OPJ_BOOL res = j2k_add_marker(p_j2k->cstr_info, J2K_MS_SOT, p_j2k->sot_start, len + 2);
3883 */
3884   assert( 0 && "TODO" );
3885 #endif /* USE_JPWL */
3886
3887         * p_data_written = 12;
3888
3889         return OPJ_TRUE;
3890 }
3891
3892 OPJ_BOOL opj_j2k_read_sot ( opj_j2k_t *p_j2k,
3893                             OPJ_BYTE * p_header_data,
3894                             OPJ_UINT32 p_header_size,
3895                             opj_event_mgr_t * p_manager )
3896 {
3897         opj_cp_t *l_cp = 00;
3898         opj_tcp_t *l_tcp = 00;
3899         OPJ_UINT32 l_tot_len, l_num_parts = 0;
3900         OPJ_UINT32 l_current_part;
3901         OPJ_UINT32 l_tile_x,l_tile_y;
3902
3903         /* preconditions */
3904         assert(p_header_data != 00);
3905         assert(p_j2k != 00);
3906         assert(p_manager != 00);
3907
3908         /* Size of this marker is fixed = 12 (we have already read marker and its size)*/
3909         if (p_header_size != 8) {
3910                 opj_event_msg(p_manager, EVT_ERROR, "Error reading SOT marker\n");
3911                 return OPJ_FALSE;
3912         }
3913
3914         l_cp = &(p_j2k->m_cp);
3915         opj_read_bytes(p_header_data,&(p_j2k->m_current_tile_number),2);                /* Isot */
3916         p_header_data+=2;
3917
3918         /* testcase 2.pdf.SIGFPE.706.1112 */
3919         if (p_j2k->m_current_tile_number >= l_cp->tw * l_cp->th) {
3920                 opj_event_msg(p_manager, EVT_ERROR, "Invalid tile number %d\n", p_j2k->m_current_tile_number);
3921                 return OPJ_FALSE;
3922         }
3923
3924         l_tcp = &l_cp->tcps[p_j2k->m_current_tile_number];
3925         l_tile_x = p_j2k->m_current_tile_number % l_cp->tw;
3926         l_tile_y = p_j2k->m_current_tile_number / l_cp->tw;
3927
3928 #ifdef USE_JPWL
3929         if (l_cp->correct) {
3930
3931                 OPJ_UINT32 tileno = p_j2k->m_current_tile_number;
3932                 static OPJ_UINT32 backup_tileno = 0;
3933
3934                 /* tileno is negative or larger than the number of tiles!!! */
3935                 if (tileno > (l_cp->tw * l_cp->th)) {
3936                         opj_event_msg(p_manager, EVT_ERROR,
3937                                         "JPWL: bad tile number (%d out of a maximum of %d)\n",
3938                                         tileno, (l_cp->tw * l_cp->th));
3939                         if (!JPWL_ASSUME) {
3940                                 opj_event_msg(p_manager, EVT_ERROR, "JPWL: giving up\n");
3941                                 return OPJ_FALSE;
3942                         }
3943                         /* we try to correct */
3944                         tileno = backup_tileno;
3945                         opj_event_msg(p_manager, EVT_WARNING, "- trying to adjust this\n"
3946                                         "- setting tile number to %d\n",
3947                                         tileno);
3948                 }
3949
3950                 /* keep your private count of tiles */
3951                 backup_tileno++;
3952         };
3953 #endif /* USE_JPWL */
3954
3955         /* look for the tile in the list of already processed tile (in parts). */
3956         /* Optimization possible here with a more complex data structure and with the removing of tiles */
3957         /* since the time taken by this function can only grow at the time */
3958
3959         opj_read_bytes(p_header_data,&l_tot_len,4);             /* Psot */
3960         p_header_data+=4;
3961
3962         /* PSot should be equal to zero or >=14 or <= 2^32-1 */
3963         if ((l_tot_len !=0 ) && (l_tot_len < 14) )
3964         {
3965             if (l_tot_len == 12 ) /* MSD: Special case for the PHR data which are read by kakadu*/
3966             {
3967                 opj_event_msg(p_manager, EVT_WARNING, "Empty SOT marker detected: Psot=%d.\n", l_tot_len);
3968             }
3969             else
3970             {
3971                 opj_event_msg(p_manager, EVT_ERROR, "Psot value is not correct regards to the JPEG2000 norm: %d.\n", l_tot_len);
3972                 return OPJ_FALSE;
3973             }
3974         }
3975
3976 #ifdef USE_JPWL
3977         if (l_cp->correct) {
3978
3979                 /* totlen is negative or larger than the bytes left!!! */
3980                 if (/*(l_tot_len < 0) ||*/ (l_tot_len > p_header_size ) ) { /* FIXME it seems correct; for info in V1 -> (p_stream_numbytesleft(p_stream) + 8))) { */
3981                         opj_event_msg(p_manager, EVT_ERROR,
3982                                         "JPWL: bad tile byte size (%d bytes against %d bytes left)\n",
3983                                         l_tot_len, p_header_size ); /* FIXME it seems correct; for info in V1 -> p_stream_numbytesleft(p_stream) + 8); */
3984                         if (!JPWL_ASSUME) {
3985                                 opj_event_msg(p_manager, EVT_ERROR, "JPWL: giving up\n");
3986                                 return OPJ_FALSE;
3987                         }
3988                         /* we try to correct */
3989                         l_tot_len = 0;
3990                         opj_event_msg(p_manager, EVT_WARNING, "- trying to adjust this\n"
3991                                         "- setting Psot to %d => assuming it is the last tile\n",
3992                                         l_tot_len);
3993                 }
3994                 };
3995 #endif /* USE_JPWL */
3996
3997                 /* Ref A.4.2: Psot could be equal zero if it is the last tile-part of the codestream.*/
3998                 if (!l_tot_len) {
3999                         opj_event_msg(p_manager, EVT_INFO, "Psot value of the current tile-part is equal to zero, "
4000                                         "we assuming it is the last tile-part of the codestream.\n");
4001                         p_j2k->m_specific_param.m_decoder.m_last_tile_part = 1;
4002                 }
4003
4004                 opj_read_bytes(p_header_data,&l_current_part ,1);       /* TPsot */
4005                 ++p_header_data;
4006
4007                 opj_read_bytes(p_header_data,&l_num_parts ,1);          /* TNsot */
4008                 ++p_header_data;
4009
4010                 if (l_num_parts != 0) { /* Number of tile-part header is provided by this tile-part header */
4011                         /* Useful to manage the case of textGBR.jp2 file because two values of TNSot are allowed: the correct numbers of
4012                          * tile-parts for that tile and zero (A.4.2 of 15444-1 : 2002). */
4013                         if (l_num_parts < l_tcp->m_nb_tile_parts) {
4014                                 l_num_parts = l_tcp->m_nb_tile_parts;
4015                         }
4016                         if (l_current_part >= l_num_parts) {
4017                                 /* testcase 451.pdf.SIGSEGV.ce9.3723 */
4018                                 l_num_parts = l_current_part + 1;
4019                         }
4020                         l_tcp->m_nb_tile_parts = l_num_parts;
4021                 }
4022
4023                 /* If know the number of tile part header we will check if we didn't read the last*/
4024                 if (l_tcp->m_nb_tile_parts) {
4025                         if (l_tcp->m_nb_tile_parts == l_current_part) {
4026                                 p_j2k->m_specific_param.m_decoder.m_can_decode = 1; /* Process the last tile-part header*/
4027                         }
4028                 }
4029
4030                 if (!p_j2k->m_specific_param.m_decoder.m_last_tile_part){
4031                         /* Keep the size of data to skip after this marker */
4032                         p_j2k->m_specific_param.m_decoder.m_sot_length = l_tot_len - 12; /* SOT_marker_size = 12 */
4033                 }
4034                 else {
4035                         /* FIXME: need to be computed from the number of bytes remaining in the codestream */
4036                         p_j2k->m_specific_param.m_decoder.m_sot_length = 0;
4037                 }
4038
4039                 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_TPH;
4040
4041                 /* Check if the current tile is outside the area we want decode or not corresponding to the tile index*/
4042                 if (p_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec == -1) {
4043                         p_j2k->m_specific_param.m_decoder.m_skip_data =
4044                                 (l_tile_x < p_j2k->m_specific_param.m_decoder.m_start_tile_x)
4045                                 ||      (l_tile_x >= p_j2k->m_specific_param.m_decoder.m_end_tile_x)
4046                                 ||  (l_tile_y < p_j2k->m_specific_param.m_decoder.m_start_tile_y)
4047                                 ||      (l_tile_y >= p_j2k->m_specific_param.m_decoder.m_end_tile_y);
4048                 }
4049                 else {
4050                         assert( p_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec >= 0 );
4051                         p_j2k->m_specific_param.m_decoder.m_skip_data =
4052                                 (p_j2k->m_current_tile_number != (OPJ_UINT32)p_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec);
4053                 }
4054
4055                 /* Index */
4056                 if (p_j2k->cstr_index)
4057                 {
4058                         assert(p_j2k->cstr_index->tile_index != 00);
4059                         p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tileno = p_j2k->m_current_tile_number;
4060                         p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_tpsno = l_current_part;
4061
4062                         if (l_num_parts != 0){
4063                                 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].nb_tps = l_num_parts;
4064                                 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps = l_num_parts;
4065
4066                                 if (!p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index) {
4067                                         p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index =
4068                                                 (opj_tp_index_t*)opj_calloc(l_num_parts, sizeof(opj_tp_index_t));
4069                                 }
4070                                 else {
4071                                         opj_tp_index_t *new_tp_index = (opj_tp_index_t *) opj_realloc(
4072                                                         p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index, l_num_parts* sizeof(opj_tp_index_t));
4073                                         if (! new_tp_index) {
4074                                                 opj_free(p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index);
4075                                                 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index = NULL;
4076                                                 opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to read PPT marker\n");
4077                                                 return OPJ_FALSE;
4078                                         }
4079                                         p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index = new_tp_index;
4080                                 }
4081                         }
4082                         else{
4083                                 /*if (!p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index)*/ {
4084
4085                                         if (!p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index) {
4086                                                 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps = 10;
4087                                                 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index =
4088                                                         (opj_tp_index_t*)opj_calloc( p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps,
4089                                                                         sizeof(opj_tp_index_t));
4090                                         }
4091
4092                                         if ( l_current_part >= p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps ){
4093                                                 opj_tp_index_t *new_tp_index;
4094                                                 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps = l_current_part + 1;
4095                                                 new_tp_index = (opj_tp_index_t *) opj_realloc(
4096                                                                 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index,
4097                                                                 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps * sizeof(opj_tp_index_t));
4098                                                 if (! new_tp_index) {
4099                                                         opj_free(p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index);
4100                                                         p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index = NULL;
4101                                                         p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps = 0;
4102                                                         opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to read PPT marker\n");
4103                                                         return OPJ_FALSE;
4104                                                 }
4105                                                 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index = new_tp_index;
4106                                         }
4107                                 }
4108
4109                         }
4110
4111                 }
4112
4113                 /* FIXME move this onto a separate method to call before reading any SOT, remove part about main_end header, use a index struct inside p_j2k */
4114                 /* if (p_j2k->cstr_info) {
4115                    if (l_tcp->first) {
4116                    if (tileno == 0) {
4117                    p_j2k->cstr_info->main_head_end = p_stream_tell(p_stream) - 13;
4118                    }
4119
4120                    p_j2k->cstr_info->tile[tileno].tileno = tileno;
4121                    p_j2k->cstr_info->tile[tileno].start_pos = p_stream_tell(p_stream) - 12;
4122                    p_j2k->cstr_info->tile[tileno].end_pos = p_j2k->cstr_info->tile[tileno].start_pos + totlen - 1;
4123                    p_j2k->cstr_info->tile[tileno].num_tps = numparts;
4124
4125                    if (numparts) {
4126                    p_j2k->cstr_info->tile[tileno].tp = (opj_tp_info_t *) opj_malloc(numparts * sizeof(opj_tp_info_t));
4127                    }
4128                    else {
4129                    p_j2k->cstr_info->tile[tileno].tp = (opj_tp_info_t *) opj_malloc(10 * sizeof(opj_tp_info_t)); // Fixme (10)
4130                    }
4131                    }
4132                    else {
4133                    p_j2k->cstr_info->tile[tileno].end_pos += totlen;
4134                    }
4135
4136                    p_j2k->cstr_info->tile[tileno].tp[partno].tp_start_pos = p_stream_tell(p_stream) - 12;
4137                    p_j2k->cstr_info->tile[tileno].tp[partno].tp_end_pos =
4138                    p_j2k->cstr_info->tile[tileno].tp[partno].tp_start_pos + totlen - 1;
4139                    }*/
4140                 return OPJ_TRUE;
4141         }
4142
4143 OPJ_BOOL opj_j2k_write_sod(     opj_j2k_t *p_j2k,
4144                                                         opj_tcd_t * p_tile_coder,
4145                                                         OPJ_BYTE * p_data,
4146                                                         OPJ_UINT32 * p_data_written,
4147                                                         OPJ_UINT32 p_total_data_size,
4148                                                         const opj_stream_private_t *p_stream,
4149                                                         opj_event_mgr_t * p_manager
4150                             )
4151 {
4152         opj_codestream_info_t *l_cstr_info = 00;
4153         OPJ_UINT32 l_remaining_data;
4154
4155         /* preconditions */
4156         assert(p_j2k != 00);
4157         assert(p_manager != 00);
4158         assert(p_stream != 00);
4159
4160         opj_write_bytes(p_data,J2K_MS_SOD,2);                                   /* SOD */
4161         p_data += 2;
4162
4163         /* make room for the EOF marker */
4164         l_remaining_data =  p_total_data_size - 4;
4165
4166         /* update tile coder */
4167         p_tile_coder->tp_num = p_j2k->m_specific_param.m_encoder.m_current_poc_tile_part_number ;
4168         p_tile_coder->cur_tp_num = p_j2k->m_specific_param.m_encoder.m_current_tile_part_number;
4169
4170          /* INDEX >> */
4171         /* TODO mergeV2: check this part which use cstr_info */
4172         /*l_cstr_info = p_j2k->cstr_info;
4173         if (l_cstr_info) {
4174                 if (!p_j2k->m_specific_param.m_encoder.m_current_tile_part_number ) {
4175                         //TODO cstr_info->tile[p_j2k->m_current_tile_number].end_header = p_stream_tell(p_stream) + p_j2k->pos_correction - 1;
4176                         l_cstr_info->tile[p_j2k->m_current_tile_number].tileno = p_j2k->m_current_tile_number;
4177                 }
4178                 else {*/
4179                         /*
4180                         TODO
4181                         if
4182                                 (cstr_info->tile[p_j2k->m_current_tile_number].packet[cstr_info->packno - 1].end_pos < p_stream_tell(p_stream))
4183                         {
4184                                 cstr_info->tile[p_j2k->m_current_tile_number].packet[cstr_info->packno].start_pos = p_stream_tell(p_stream);
4185                         }*/
4186                 /*}*/
4187                 /* UniPG>> */
4188 #ifdef USE_JPWL
4189                 /* update markers struct */
4190                 /*OPJ_BOOL res = j2k_add_marker(p_j2k->cstr_info, J2K_MS_SOD, p_j2k->sod_start, 2);
4191 */
4192   assert( 0 && "TODO" );
4193 #endif /* USE_JPWL */
4194                 /* <<UniPG */
4195         /*}*/
4196         /* << INDEX */
4197
4198         if (p_j2k->m_specific_param.m_encoder.m_current_tile_part_number == 0) {
4199                 p_tile_coder->tcd_image->tiles->packno = 0;
4200                 if (l_cstr_info) {
4201                         l_cstr_info->packno = 0;
4202                 }
4203         }
4204
4205         *p_data_written = 0;
4206
4207         if (! opj_tcd_encode_tile(p_tile_coder, p_j2k->m_current_tile_number, p_data, p_data_written, l_remaining_data , l_cstr_info)) {
4208                 opj_event_msg(p_manager, EVT_ERROR, "Cannot encode tile\n");
4209                 return OPJ_FALSE;
4210         }
4211
4212         *p_data_written += 2;
4213
4214         return OPJ_TRUE;
4215 }
4216
4217 OPJ_BOOL opj_j2k_read_sod (opj_j2k_t *p_j2k,
4218                            opj_stream_private_t *p_stream,
4219                                                    opj_event_mgr_t * p_manager
4220                            )
4221 {
4222         OPJ_UINT32 l_current_read_size;
4223         opj_codestream_index_t * l_cstr_index = 00;
4224         OPJ_BYTE ** l_current_data = 00;
4225         opj_tcp_t * l_tcp = 00;
4226         OPJ_UINT32 * l_tile_len = 00;
4227         OPJ_BOOL l_sot_length_pb_detected = OPJ_FALSE;
4228
4229         /* preconditions */
4230         assert(p_j2k != 00);
4231         assert(p_manager != 00);
4232         assert(p_stream != 00);
4233
4234         l_tcp = &(p_j2k->m_cp.tcps[p_j2k->m_current_tile_number]);
4235
4236         if (p_j2k->m_specific_param.m_decoder.m_last_tile_part) {
4237                 /* opj_stream_get_number_byte_left returns OPJ_OFF_T
4238                 // but we are in the last tile part,
4239                 // so its result will fit on OPJ_UINT32 unless we find
4240                 // a file with a single tile part of more than 4 GB...*/
4241                 p_j2k->m_specific_param.m_decoder.m_sot_length = (OPJ_UINT32)(opj_stream_get_number_byte_left(p_stream) - 2);
4242         }
4243         else {
4244             /* Check to avoid pass the limit of OPJ_UINT32 */
4245             if (p_j2k->m_specific_param.m_decoder.m_sot_length >= 2 )
4246                 p_j2k->m_specific_param.m_decoder.m_sot_length -= 2;
4247             else {
4248                 /* MSD: case commented to support empty SOT marker (PHR data) */
4249             }
4250         }
4251
4252         l_current_data = &(l_tcp->m_data);
4253         l_tile_len = &l_tcp->m_data_size;
4254
4255         /* Patch to support new PHR data */
4256         if (p_j2k->m_specific_param.m_decoder.m_sot_length) {
4257             if (! *l_current_data) {
4258                 /* LH: oddly enough, in this path, l_tile_len!=0.
4259                  * TODO: If this was consistant, we could simplify the code to only use realloc(), as realloc(0,...) default to malloc(0,...).
4260                  */
4261                 *l_current_data = (OPJ_BYTE*) opj_malloc(p_j2k->m_specific_param.m_decoder.m_sot_length);
4262             }
4263             else {
4264                 OPJ_BYTE *l_new_current_data = (OPJ_BYTE *) opj_realloc(*l_current_data, *l_tile_len + p_j2k->m_specific_param.m_decoder.m_sot_length);
4265                 if (! l_new_current_data) {
4266                         opj_free(*l_current_data);
4267                         /*nothing more is done as l_current_data will be set to null, and just
4268                           afterward we enter in the error path
4269                           and the actual tile_len is updated (committed) at the end of the
4270                           function. */
4271                 }
4272                 *l_current_data = l_new_current_data;
4273             }
4274             
4275             if (*l_current_data == 00) {
4276                 opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to decode tile\n");
4277                 return OPJ_FALSE;
4278             }
4279         }
4280         else {
4281             l_sot_length_pb_detected = OPJ_TRUE;
4282         }
4283
4284         /* Index */
4285         l_cstr_index = p_j2k->cstr_index;
4286         if (l_cstr_index) {
4287                 OPJ_OFF_T l_current_pos = opj_stream_tell(p_stream) - 2;
4288
4289                 OPJ_UINT32 l_current_tile_part = l_cstr_index->tile_index[p_j2k->m_current_tile_number].current_tpsno;
4290                 l_cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index[l_current_tile_part].end_header =
4291                                 l_current_pos;
4292                 l_cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index[l_current_tile_part].end_pos =
4293                                 l_current_pos + p_j2k->m_specific_param.m_decoder.m_sot_length + 2;
4294
4295                 if (OPJ_FALSE == opj_j2k_add_tlmarker(p_j2k->m_current_tile_number,
4296                                         l_cstr_index,
4297                                         J2K_MS_SOD,
4298                                         l_current_pos,
4299                                         p_j2k->m_specific_param.m_decoder.m_sot_length + 2)) {
4300                         opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to add tl marker\n");
4301                         return OPJ_FALSE;
4302                 }
4303
4304                 /*l_cstr_index->packno = 0;*/
4305         }
4306
4307         /* Patch to support new PHR data */
4308         if (!l_sot_length_pb_detected) {
4309             l_current_read_size = opj_stream_read_data(
4310                         p_stream,
4311                         *l_current_data + *l_tile_len,
4312                         p_j2k->m_specific_param.m_decoder.m_sot_length,
4313                         p_manager);
4314         }
4315         else
4316         {
4317             l_current_read_size = 0;
4318         }
4319
4320         if (l_current_read_size != p_j2k->m_specific_param.m_decoder.m_sot_length) {
4321                 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_NEOC;
4322         }
4323         else {
4324                 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_TPHSOT;
4325         }
4326
4327         *l_tile_len +=  l_current_read_size;
4328
4329         return OPJ_TRUE;
4330 }
4331
4332  OPJ_BOOL opj_j2k_write_rgn(opj_j2k_t *p_j2k,
4333                             OPJ_UINT32 p_tile_no,
4334                             OPJ_UINT32 p_comp_no,
4335                             OPJ_UINT32 nb_comps,
4336                             opj_stream_private_t *p_stream,
4337                             opj_event_mgr_t * p_manager
4338                             )
4339 {
4340         OPJ_BYTE * l_current_data = 00;
4341         OPJ_UINT32 l_rgn_size;
4342         opj_cp_t *l_cp = 00;
4343         opj_tcp_t *l_tcp = 00;
4344         opj_tccp_t *l_tccp = 00;
4345         OPJ_UINT32 l_comp_room;
4346
4347         /* preconditions */
4348         assert(p_j2k != 00);
4349         assert(p_manager != 00);
4350         assert(p_stream != 00);
4351
4352         l_cp = &(p_j2k->m_cp);
4353         l_tcp = &l_cp->tcps[p_tile_no];
4354         l_tccp = &l_tcp->tccps[p_comp_no];
4355
4356         if (nb_comps <= 256) {
4357                 l_comp_room = 1;
4358         }
4359         else {
4360                 l_comp_room = 2;
4361         }
4362
4363         l_rgn_size = 6 + l_comp_room;
4364
4365         l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
4366
4367         opj_write_bytes(l_current_data,J2K_MS_RGN,2);                                   /* RGN  */
4368         l_current_data += 2;
4369
4370         opj_write_bytes(l_current_data,l_rgn_size-2,2);                                 /* Lrgn */
4371         l_current_data += 2;
4372
4373         opj_write_bytes(l_current_data,p_comp_no,l_comp_room);                          /* Crgn */
4374         l_current_data+=l_comp_room;
4375
4376         opj_write_bytes(l_current_data, 0,1);                                           /* Srgn */
4377         ++l_current_data;
4378
4379         opj_write_bytes(l_current_data, l_tccp->roishift,1);                            /* SPrgn */
4380         ++l_current_data;
4381
4382         if (opj_stream_write_data(p_stream,p_j2k->m_specific_param.m_encoder.m_header_tile_data,l_rgn_size,p_manager) != l_rgn_size) {
4383                 return OPJ_FALSE;
4384         }
4385
4386         return OPJ_TRUE;
4387 }
4388
4389 OPJ_BOOL opj_j2k_write_eoc(     opj_j2k_t *p_j2k,
4390                             opj_stream_private_t *p_stream,
4391                             opj_event_mgr_t * p_manager
4392                             )
4393 {
4394         /* preconditions */
4395         assert(p_j2k != 00);
4396         assert(p_manager != 00);
4397         assert(p_stream != 00);
4398
4399         opj_write_bytes(p_j2k->m_specific_param.m_encoder.m_header_tile_data,J2K_MS_EOC,2);                                     /* EOC */
4400
4401 /* UniPG>> */
4402 #ifdef USE_JPWL
4403         /* update markers struct */
4404         /*
4405         OPJ_BOOL res = j2k_add_marker(p_j2k->cstr_info, J2K_MS_EOC, p_stream_tell(p_stream) - 2, 2);
4406 */
4407 #endif /* USE_JPWL */
4408
4409         if ( opj_stream_write_data(p_stream,p_j2k->m_specific_param.m_encoder.m_header_tile_data,2,p_manager) != 2) {
4410                 return OPJ_FALSE;
4411         }
4412
4413         if ( ! opj_stream_flush(p_stream,p_manager) ) {
4414                 return OPJ_FALSE;
4415         }
4416
4417         return OPJ_TRUE;
4418 }
4419
4420 /**
4421  * Reads a RGN marker (Region Of Interest)
4422  *
4423  * @param       p_header_data   the data contained in the POC box.
4424  * @param       p_j2k                   the jpeg2000 codec.
4425  * @param       p_header_size   the size of the data contained in the POC marker.
4426  * @param       p_manager               the user event manager.
4427 */
4428 static OPJ_BOOL opj_j2k_read_rgn (opj_j2k_t *p_j2k,
4429                                   OPJ_BYTE * p_header_data,
4430                                   OPJ_UINT32 p_header_size,
4431                                   opj_event_mgr_t * p_manager
4432                                   )
4433 {
4434         OPJ_UINT32 l_nb_comp;
4435         opj_image_t * l_image = 00;
4436
4437         opj_cp_t *l_cp = 00;
4438         opj_tcp_t *l_tcp = 00;
4439         OPJ_UINT32 l_comp_room, l_comp_no, l_roi_sty;
4440
4441         /* preconditions*/
4442         assert(p_header_data != 00);
4443         assert(p_j2k != 00);
4444         assert(p_manager != 00);
4445
4446         l_image = p_j2k->m_private_image;
4447         l_nb_comp = l_image->numcomps;
4448
4449         if (l_nb_comp <= 256) {
4450                 l_comp_room = 1; }
4451         else {
4452                 l_comp_room = 2; }
4453
4454         if (p_header_size != 2 + l_comp_room) {
4455                 opj_event_msg(p_manager, EVT_ERROR, "Error reading RGN marker\n");
4456                 return OPJ_FALSE;
4457         }
4458
4459         l_cp = &(p_j2k->m_cp);
4460         l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ?
4461                                 &l_cp->tcps[p_j2k->m_current_tile_number] :
4462                                 p_j2k->m_specific_param.m_decoder.m_default_tcp;
4463
4464         opj_read_bytes(p_header_data,&l_comp_no,l_comp_room);           /* Crgn */
4465         p_header_data+=l_comp_room;
4466         opj_read_bytes(p_header_data,&l_roi_sty,1);                                     /* Srgn */
4467         ++p_header_data;
4468
4469 #ifdef USE_JPWL
4470         if (l_cp->correct) {
4471                 /* totlen is negative or larger than the bytes left!!! */
4472                 if (l_comp_room >= l_nb_comp) {
4473                         opj_event_msg(p_manager, EVT_ERROR,
4474                                 "JPWL: bad component number in RGN (%d when there are only %d)\n",
4475                                 l_comp_room, l_nb_comp);
4476                         if (!JPWL_ASSUME || JPWL_ASSUME) {
4477                                 opj_event_msg(p_manager, EVT_ERROR, "JPWL: giving up\n");
4478                                 return OPJ_FALSE;
4479                         }
4480                 }
4481         };
4482 #endif /* USE_JPWL */
4483
4484         opj_read_bytes(p_header_data,(OPJ_UINT32 *) (&(l_tcp->tccps[l_comp_no].roishift)),1);   /* SPrgn */
4485         ++p_header_data;
4486
4487         return OPJ_TRUE;
4488
4489 }
4490
4491 OPJ_FLOAT32 opj_j2k_get_tp_stride (opj_tcp_t * p_tcp)
4492 {
4493         return (OPJ_FLOAT32) ((p_tcp->m_nb_tile_parts - 1) * 14);
4494 }
4495
4496 OPJ_FLOAT32 opj_j2k_get_default_stride (opj_tcp_t * p_tcp)
4497 {
4498     (void)p_tcp;
4499     return 0;
4500 }
4501
4502 OPJ_BOOL opj_j2k_update_rates(  opj_j2k_t *p_j2k,
4503                                                             opj_stream_private_t *p_stream,
4504                                                             opj_event_mgr_t * p_manager )
4505 {
4506         opj_cp_t * l_cp = 00;
4507         opj_image_t * l_image = 00;
4508         opj_tcp_t * l_tcp = 00;
4509         opj_image_comp_t * l_img_comp = 00;
4510
4511         OPJ_UINT32 i,j,k;
4512         OPJ_INT32 l_x0,l_y0,l_x1,l_y1;
4513         OPJ_FLOAT32 * l_rates = 0;
4514         OPJ_FLOAT32 l_sot_remove;
4515         OPJ_UINT32 l_bits_empty, l_size_pixel;
4516         OPJ_UINT32 l_tile_size = 0;
4517         OPJ_UINT32 l_last_res;
4518         OPJ_FLOAT32 (* l_tp_stride_func)(opj_tcp_t *) = 00;
4519
4520         /* preconditions */
4521         assert(p_j2k != 00);
4522         assert(p_manager != 00);
4523         assert(p_stream != 00);
4524
4525         l_cp = &(p_j2k->m_cp);
4526         l_image = p_j2k->m_private_image;
4527         l_tcp = l_cp->tcps;
4528
4529         l_bits_empty = 8 * l_image->comps->dx * l_image->comps->dy;
4530         l_size_pixel = l_image->numcomps * l_image->comps->prec;
4531         l_sot_remove = ((OPJ_FLOAT32) opj_stream_tell(p_stream)) / (l_cp->th * l_cp->tw);
4532
4533         if (l_cp->m_specific_param.m_enc.m_tp_on) {
4534                 l_tp_stride_func = opj_j2k_get_tp_stride;
4535         }
4536         else {
4537                 l_tp_stride_func = opj_j2k_get_default_stride;
4538         }
4539
4540         for (i=0;i<l_cp->th;++i) {
4541                 for (j=0;j<l_cp->tw;++j) {
4542                         OPJ_FLOAT32 l_offset = ((*l_tp_stride_func)(l_tcp)) / l_tcp->numlayers;
4543
4544                         /* 4 borders of the tile rescale on the image if necessary */
4545                         l_x0 = opj_int_max(l_cp->tx0 + j * l_cp->tdx, l_image->x0);
4546                         l_y0 = opj_int_max(l_cp->ty0 + i * l_cp->tdy, l_image->y0);
4547                         l_x1 = opj_int_min(l_cp->tx0 + (j + 1) * l_cp->tdx, l_image->x1);
4548                         l_y1 = opj_int_min(l_cp->ty0 + (i + 1) * l_cp->tdy, l_image->y1);
4549
4550                         l_rates = l_tcp->rates;
4551
4552                         /* Modification of the RATE >> */
4553                         if (*l_rates) {
4554                                 *l_rates =              (( (OPJ_FLOAT32) (l_size_pixel * (l_x1 - l_x0) * (l_y1 - l_y0)))
4555                                                                 /
4556                                                                 ((*l_rates) * l_bits_empty)
4557                                                                 )
4558                                                                 -
4559                                                                 l_offset;
4560                         }
4561
4562                         ++l_rates;
4563
4564                         for (k = 1; k < l_tcp->numlayers; ++k) {
4565                                 if (*l_rates) {
4566                                         *l_rates =              (( (OPJ_FLOAT32) (l_size_pixel * (l_x1 - l_x0) * (l_y1 - l_y0)))
4567                                                                         /
4568                                                                                 ((*l_rates) * l_bits_empty)
4569                                                                         )
4570                                                                         -
4571                                                                         l_offset;
4572                                 }
4573
4574                                 ++l_rates;
4575                         }
4576
4577                         ++l_tcp;
4578
4579                 }
4580         }
4581
4582         l_tcp = l_cp->tcps;
4583
4584         for (i=0;i<l_cp->th;++i) {
4585                 for     (j=0;j<l_cp->tw;++j) {
4586                         l_rates = l_tcp->rates;
4587
4588                         if (*l_rates) {
4589                                 *l_rates -= l_sot_remove;
4590
4591                                 if (*l_rates < 30) {
4592                                         *l_rates = 30;
4593                                 }
4594                         }
4595
4596                         ++l_rates;
4597
4598                         l_last_res = l_tcp->numlayers - 1;
4599
4600                         for (k = 1; k < l_last_res; ++k) {
4601
4602                                 if (*l_rates) {
4603                                         *l_rates -= l_sot_remove;
4604
4605                                         if (*l_rates < *(l_rates - 1) + 10) {
4606                                                 *l_rates  = (*(l_rates - 1)) + 20;
4607                                         }
4608                                 }
4609
4610                                 ++l_rates;
4611                         }
4612
4613                         if (*l_rates) {
4614                                 *l_rates -= (l_sot_remove + 2.f);
4615
4616                                 if (*l_rates < *(l_rates - 1) + 10) {
4617                                         *l_rates  = (*(l_rates - 1)) + 20;
4618                                 }
4619                         }
4620
4621                         ++l_tcp;
4622                 }
4623         }
4624
4625         l_img_comp = l_image->comps;
4626         l_tile_size = 0;
4627
4628         for (i=0;i<l_image->numcomps;++i) {
4629                 l_tile_size += (        opj_uint_ceildiv(l_cp->tdx,l_img_comp->dx)
4630                                                         *
4631                                                         opj_uint_ceildiv(l_cp->tdy,l_img_comp->dy)
4632                                                         *
4633                                                         l_img_comp->prec
4634                                                 );
4635
4636                 ++l_img_comp;
4637         }
4638
4639         l_tile_size = (OPJ_UINT32) (l_tile_size * 0.1625); /* 1.3/8 = 0.1625 */
4640
4641         l_tile_size += opj_j2k_get_specific_header_sizes(p_j2k);
4642
4643         p_j2k->m_specific_param.m_encoder.m_encoded_tile_size = l_tile_size;
4644         p_j2k->m_specific_param.m_encoder.m_encoded_tile_data =
4645                         (OPJ_BYTE *) opj_malloc(p_j2k->m_specific_param.m_encoder.m_encoded_tile_size);
4646         if (p_j2k->m_specific_param.m_encoder.m_encoded_tile_data == 00) {
4647                 return OPJ_FALSE;
4648         }
4649
4650         if (l_cp->m_specific_param.m_enc.m_cinema) {
4651                 p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer =
4652                                 (OPJ_BYTE *) opj_malloc(5*p_j2k->m_specific_param.m_encoder.m_total_tile_parts);
4653                 if (! p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer) {
4654                         return OPJ_FALSE;
4655                 }
4656
4657                 p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current =
4658                                 p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer;
4659         }
4660
4661         return OPJ_TRUE;
4662 }
4663
4664 OPJ_BOOL opj_j2k_read_eoc (     opj_j2k_t *p_j2k,
4665                                                         opj_stream_private_t *p_stream,
4666                                                         opj_event_mgr_t * p_manager )
4667 {
4668         OPJ_UINT32 i;
4669         opj_tcd_t * l_tcd = 00;
4670         OPJ_UINT32 l_nb_tiles;
4671         opj_tcp_t * l_tcp = 00;
4672         OPJ_BOOL l_success;
4673
4674         /* preconditions */
4675         assert(p_j2k != 00);
4676         assert(p_manager != 00);
4677         assert(p_stream != 00);
4678
4679         l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw;
4680         l_tcp = p_j2k->m_cp.tcps;
4681
4682         l_tcd = opj_tcd_create(OPJ_TRUE);
4683         if (l_tcd == 00) {
4684                 opj_event_msg(p_manager, EVT_ERROR, "Cannot decode tile, memory error\n");
4685                 return OPJ_FALSE;
4686         }
4687
4688         for (i = 0; i < l_nb_tiles; ++i) {
4689                 if (l_tcp->m_data) {
4690                         if (! opj_tcd_init_decode_tile(l_tcd, i)) {
4691                                 opj_tcd_destroy(l_tcd);
4692                                 opj_event_msg(p_manager, EVT_ERROR, "Cannot decode tile, memory error\n");
4693                                 return OPJ_FALSE;
4694                         }
4695
4696                         l_success = opj_tcd_decode_tile(l_tcd, l_tcp->m_data, l_tcp->m_data_size, i, p_j2k->cstr_index);
4697                         /* cleanup */
4698
4699                         if (! l_success) {
4700                                 p_j2k->m_specific_param.m_decoder.m_state |= J2K_STATE_ERR;
4701                                 break;
4702                         }
4703                 }
4704
4705                 opj_j2k_tcp_destroy(l_tcp);
4706                 ++l_tcp;
4707         }
4708
4709         opj_tcd_destroy(l_tcd);
4710         return OPJ_TRUE;
4711 }
4712
4713 OPJ_BOOL opj_j2k_get_end_header(opj_j2k_t *p_j2k,
4714                                                         struct opj_stream_private *p_stream,
4715                                                         struct opj_event_mgr * p_manager )
4716 {
4717         /* preconditions */
4718         assert(p_j2k != 00);
4719         assert(p_manager != 00);
4720         assert(p_stream != 00);
4721
4722         p_j2k->cstr_index->main_head_end = opj_stream_tell(p_stream);
4723
4724         return OPJ_TRUE;
4725 }
4726
4727 OPJ_BOOL opj_j2k_write_mct_data_group(  opj_j2k_t *p_j2k,
4728                                                                         struct opj_stream_private *p_stream,
4729                                                                         struct opj_event_mgr * p_manager )
4730 {
4731         OPJ_UINT32 i;
4732         opj_simple_mcc_decorrelation_data_t * l_mcc_record;
4733         opj_mct_data_t * l_mct_record;
4734         opj_tcp_t * l_tcp;
4735
4736         /* preconditions */
4737         assert(p_j2k != 00);
4738         assert(p_stream != 00);
4739         assert(p_manager != 00);
4740
4741         if (! opj_j2k_write_cbd(p_j2k,p_stream,p_manager)) {
4742                 return OPJ_FALSE;
4743         }
4744
4745         l_tcp = &(p_j2k->m_cp.tcps[p_j2k->m_current_tile_number]);
4746         l_mct_record = l_tcp->m_mct_records;
4747
4748         for (i=0;i<l_tcp->m_nb_mct_records;++i) {
4749
4750                 if (! opj_j2k_write_mct_record(p_j2k,l_mct_record,p_stream,p_manager)) {
4751                         return OPJ_FALSE;
4752                 }
4753
4754                 ++l_mct_record;
4755         }
4756
4757         l_mcc_record = l_tcp->m_mcc_records;
4758
4759         for     (i=0;i<l_tcp->m_nb_mcc_records;++i) {
4760
4761                 if (! opj_j2k_write_mcc_record(p_j2k,l_mcc_record,p_stream,p_manager)) {
4762                         return OPJ_FALSE;
4763                 }
4764
4765                 ++l_mcc_record;
4766         }
4767
4768         if (! opj_j2k_write_mco(p_j2k,p_stream,p_manager)) {
4769                 return OPJ_FALSE;
4770         }
4771
4772         return OPJ_TRUE;
4773 }
4774
4775 OPJ_BOOL opj_j2k_write_all_coc(opj_j2k_t *p_j2k,
4776                                                                         struct opj_stream_private *p_stream,
4777                                                                         struct opj_event_mgr * p_manager )
4778 {
4779         OPJ_UINT32 compno;
4780
4781         /* preconditions */
4782         assert(p_j2k != 00);
4783         assert(p_manager != 00);
4784         assert(p_stream != 00);
4785
4786         for (compno = 0; compno < p_j2k->m_private_image->numcomps; ++compno)
4787         {
4788                 if (! opj_j2k_write_coc(p_j2k,compno,p_stream, p_manager)) {
4789                         return OPJ_FALSE;
4790                 }
4791         }
4792
4793         return OPJ_TRUE;
4794 }
4795
4796 OPJ_BOOL opj_j2k_write_all_qcc(opj_j2k_t *p_j2k,
4797                                                                         struct opj_stream_private *p_stream,
4798                                                                         struct opj_event_mgr * p_manager )
4799 {
4800         OPJ_UINT32 compno;
4801
4802         /* preconditions */
4803         assert(p_j2k != 00);
4804         assert(p_manager != 00);
4805         assert(p_stream != 00);
4806
4807         for (compno = 0; compno < p_j2k->m_private_image->numcomps; ++compno)
4808         {
4809                 if (! opj_j2k_write_qcc(p_j2k,compno,p_stream, p_manager)) {
4810                         return OPJ_FALSE;
4811                 }
4812         }
4813
4814         return OPJ_TRUE;
4815 }
4816
4817
4818 OPJ_BOOL opj_j2k_write_regions( opj_j2k_t *p_j2k,
4819                                                         struct opj_stream_private *p_stream,
4820                                                         struct opj_event_mgr * p_manager )
4821 {
4822         OPJ_UINT32 compno;
4823         const opj_tccp_t *l_tccp = 00;
4824
4825         /* preconditions */
4826         assert(p_j2k != 00);
4827         assert(p_manager != 00);
4828         assert(p_stream != 00);
4829
4830         l_tccp = p_j2k->m_cp.tcps->tccps;
4831
4832         for     (compno = 0; compno < p_j2k->m_private_image->numcomps; ++compno)  {
4833                 if (l_tccp->roishift) {
4834
4835                         if (! opj_j2k_write_rgn(p_j2k,0,compno,p_j2k->m_private_image->numcomps,p_stream,p_manager)) {
4836                                 return OPJ_FALSE;
4837                         }
4838                 }
4839
4840                 ++l_tccp;
4841         }
4842
4843         return OPJ_TRUE;
4844 }
4845
4846 OPJ_BOOL opj_j2k_write_epc(     opj_j2k_t *p_j2k,
4847                                                 struct opj_stream_private *p_stream,
4848                                                 struct opj_event_mgr * p_manager )
4849 {
4850         opj_codestream_index_t * l_cstr_index = 00;
4851
4852         /* preconditions */
4853         assert(p_j2k != 00);
4854         assert(p_manager != 00);
4855         assert(p_stream != 00);
4856
4857         l_cstr_index = p_j2k->cstr_index;
4858         if (l_cstr_index) {
4859                 l_cstr_index->codestream_size = opj_stream_tell(p_stream);
4860                 /* UniPG>> */
4861                 /* The following adjustment is done to adjust the codestream size */
4862                 /* if SOD is not at 0 in the buffer. Useful in case of JP2, where */
4863                 /* the first bunch of bytes is not in the codestream              */
4864                 l_cstr_index->codestream_size -= l_cstr_index->main_head_start;
4865                 /* <<UniPG */
4866         }
4867
4868 #ifdef USE_JPWL
4869         /* preparation of JPWL marker segments */
4870 #if 0
4871         if(cp->epc_on) {
4872
4873                 /* encode according to JPWL */
4874                 jpwl_encode(p_j2k, p_stream, image);
4875
4876         }
4877 #endif
4878   assert( 0 && "TODO" );
4879 #endif /* USE_JPWL */
4880
4881         return OPJ_TRUE;
4882 }
4883
4884 OPJ_BOOL opj_j2k_read_unk (     opj_j2k_t *p_j2k,
4885                                                         opj_stream_private_t *p_stream,
4886                                                         OPJ_UINT32 *output_marker,
4887                                                         opj_event_mgr_t * p_manager
4888                                                         )
4889 {
4890         OPJ_UINT32 l_unknown_marker;
4891         const opj_dec_memory_marker_handler_t * l_marker_handler;
4892         OPJ_UINT32 l_size_unk = 2;
4893
4894         /* preconditions*/
4895         assert(p_j2k != 00);
4896         assert(p_manager != 00);
4897         assert(p_stream != 00);
4898
4899         opj_event_msg(p_manager, EVT_WARNING, "Unknown marker\n");
4900
4901         while(1) {
4902                 /* Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer*/
4903                 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
4904                         opj_event_msg(p_manager, EVT_ERROR, "Stream too short\n");
4905                         return OPJ_FALSE;
4906                 }
4907
4908                 /* read 2 bytes as the new marker ID*/
4909                 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_unknown_marker,2);
4910
4911                 if (!(l_unknown_marker < 0xff00)) {
4912
4913                         /* Get the marker handler from the marker ID*/
4914                         l_marker_handler = opj_j2k_get_marker_handler(l_unknown_marker);
4915
4916                         if (!(p_j2k->m_specific_param.m_decoder.m_state & l_marker_handler->states)) {
4917                                 opj_event_msg(p_manager, EVT_ERROR, "Marker is not compliant with its position\n");
4918                                 return OPJ_FALSE;
4919                         }
4920                         else {
4921                                 if (l_marker_handler->id != J2K_MS_UNK) {
4922                                         /* Add the marker to the codestream index*/
4923                                         if (l_marker_handler->id != J2K_MS_SOT)
4924                                         {
4925                                                 OPJ_BOOL res = opj_j2k_add_mhmarker(p_j2k->cstr_index, J2K_MS_UNK,
4926                                                                 (OPJ_UINT32) opj_stream_tell(p_stream) - l_size_unk,
4927                                                                 l_size_unk);
4928                                                 if (res == OPJ_FALSE) {
4929                                                         opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to add mh marker\n");
4930                                                         return OPJ_FALSE;
4931                                                 }
4932                                         }
4933                                         break; /* next marker is known and well located */
4934                                 }
4935                                 else
4936                                         l_size_unk += 2;
4937                         }
4938                 }
4939         }
4940
4941         *output_marker = l_marker_handler->id ;
4942
4943         return OPJ_TRUE;
4944 }
4945
4946 OPJ_BOOL opj_j2k_write_mct_record(      opj_j2k_t *p_j2k,
4947                                                                 opj_mct_data_t * p_mct_record,
4948                                                                 struct opj_stream_private *p_stream,
4949                                                                 struct opj_event_mgr * p_manager )
4950 {
4951         OPJ_UINT32 l_mct_size;
4952         OPJ_BYTE * l_current_data = 00;
4953         OPJ_UINT32 l_tmp;
4954
4955         /* preconditions */
4956         assert(p_j2k != 00);
4957         assert(p_manager != 00);
4958         assert(p_stream != 00);
4959
4960         l_mct_size = 10 + p_mct_record->m_data_size;
4961
4962         if (l_mct_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
4963                 OPJ_BYTE *new_header_tile_data = (OPJ_BYTE *) opj_realloc(p_j2k->m_specific_param.m_encoder.m_header_tile_data, l_mct_size);
4964                 if (! new_header_tile_data) {
4965                         opj_free(p_j2k->m_specific_param.m_encoder.m_header_tile_data);
4966                         p_j2k->m_specific_param.m_encoder.m_header_tile_data = NULL;
4967                         p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = 0;
4968                         opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to write MCT marker\n");
4969                         return OPJ_FALSE;
4970                 }
4971                 p_j2k->m_specific_param.m_encoder.m_header_tile_data = new_header_tile_data;
4972                 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_mct_size;
4973         }
4974
4975         l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
4976
4977         opj_write_bytes(l_current_data,J2K_MS_MCT,2);                                   /* MCT */
4978         l_current_data += 2;
4979
4980         opj_write_bytes(l_current_data,l_mct_size-2,2);                                 /* Lmct */
4981         l_current_data += 2;
4982
4983         opj_write_bytes(l_current_data,0,2);                                                    /* Zmct */
4984         l_current_data += 2;
4985
4986         /* only one marker atm */
4987         l_tmp = (p_mct_record->m_index & 0xff) | (p_mct_record->m_array_type << 8) | (p_mct_record->m_element_type << 10);
4988
4989         opj_write_bytes(l_current_data,l_tmp,2);
4990         l_current_data += 2;
4991
4992         opj_write_bytes(l_current_data,0,2);                                                    /* Ymct */
4993         l_current_data+=2;
4994
4995         memcpy(l_current_data,p_mct_record->m_data,p_mct_record->m_data_size);
4996
4997         if (opj_stream_write_data(p_stream,p_j2k->m_specific_param.m_encoder.m_header_tile_data,l_mct_size,p_manager) != l_mct_size) {
4998                 return OPJ_FALSE;
4999         }
5000
5001         return OPJ_TRUE;
5002 }
5003
5004 /**
5005  * Reads a MCT marker (Multiple Component Transform)
5006  *
5007  * @param       p_header_data   the data contained in the MCT box.
5008  * @param       p_j2k                   the jpeg2000 codec.
5009  * @param       p_header_size   the size of the data contained in the MCT marker.
5010  * @param       p_manager               the user event manager.
5011 */
5012 static OPJ_BOOL opj_j2k_read_mct (      opj_j2k_t *p_j2k,
5013                                                                     OPJ_BYTE * p_header_data,
5014                                                                     OPJ_UINT32 p_header_size,
5015                                                                     opj_event_mgr_t * p_manager
5016                                     )
5017 {
5018         OPJ_UINT32 i;
5019         opj_tcp_t *l_tcp = 00;
5020         OPJ_UINT32 l_tmp;
5021         OPJ_UINT32 l_indix;
5022         opj_mct_data_t * l_mct_data;
5023
5024         /* preconditions */
5025         assert(p_header_data != 00);
5026         assert(p_j2k != 00);
5027
5028         l_tcp = p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH ?
5029                         &p_j2k->m_cp.tcps[p_j2k->m_current_tile_number] :
5030                         p_j2k->m_specific_param.m_decoder.m_default_tcp;
5031
5032         if (p_header_size < 2) {
5033                 opj_event_msg(p_manager, EVT_ERROR, "Error reading MCT marker\n");
5034                 return OPJ_FALSE;
5035         }
5036
5037         /* first marker */
5038         opj_read_bytes(p_header_data,&l_tmp,2);                         /* Zmct */
5039         p_header_data += 2;
5040         if (l_tmp != 0) {
5041                 opj_event_msg(p_manager, EVT_WARNING, "Cannot take in charge mct data within multiple MCT records\n");
5042                 return OPJ_TRUE;
5043         }
5044
5045         if(p_header_size <= 6) {
5046                 opj_event_msg(p_manager, EVT_ERROR, "Error reading MCT marker\n");
5047                 return OPJ_FALSE;
5048         }
5049
5050         /* Imct -> no need for other values, take the first, type is double with decorrelation x0000 1101 0000 0000*/
5051         opj_read_bytes(p_header_data,&l_tmp,2);                         /* Imct */
5052         p_header_data += 2;
5053
5054         l_indix = l_tmp & 0xff;
5055         l_mct_data = l_tcp->m_mct_records;
5056
5057         for (i=0;i<l_tcp->m_nb_mct_records;++i) {
5058                 if (l_mct_data->m_index == l_indix) {
5059                         break;
5060                 }
5061                 ++l_mct_data;
5062         }
5063
5064         /* NOT FOUND */
5065         if (i == l_tcp->m_nb_mct_records) {
5066                 if (l_tcp->m_nb_mct_records == l_tcp->m_nb_max_mct_records) {
5067                         opj_mct_data_t *new_mct_records;
5068                         l_tcp->m_nb_max_mct_records += OPJ_J2K_MCT_DEFAULT_NB_RECORDS;
5069
5070                         new_mct_records = (opj_mct_data_t *) opj_realloc(l_tcp->m_mct_records, l_tcp->m_nb_max_mct_records * sizeof(opj_mct_data_t));
5071                         if (! new_mct_records) {
5072                                 opj_free(l_tcp->m_mct_records);
5073                                 l_tcp->m_mct_records = NULL;
5074                                 l_tcp->m_nb_max_mct_records = 0;
5075                                 l_tcp->m_nb_mct_records = 0;
5076                                 opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to read MCT marker\n");
5077                                 return OPJ_FALSE;
5078                         }
5079                         l_tcp->m_mct_records = new_mct_records;
5080                         l_mct_data = l_tcp->m_mct_records + l_tcp->m_nb_mct_records;
5081                         memset(l_mct_data ,0,(l_tcp->m_nb_max_mct_records - l_tcp->m_nb_mct_records) * sizeof(opj_mct_data_t));
5082                 }
5083
5084                 l_mct_data = l_tcp->m_mct_records + l_tcp->m_nb_mct_records;
5085         }
5086
5087         if (l_mct_data->m_data) {
5088                 opj_free(l_mct_data->m_data);
5089                 l_mct_data->m_data = 00;
5090         }
5091
5092         l_mct_data->m_index = l_indix;
5093         l_mct_data->m_array_type = (J2K_MCT_ARRAY_TYPE)((l_tmp  >> 8) & 3);
5094         l_mct_data->m_element_type = (J2K_MCT_ELEMENT_TYPE)((l_tmp  >> 10) & 3);
5095
5096         opj_read_bytes(p_header_data,&l_tmp,2);                         /* Ymct */
5097         p_header_data+=2;
5098         if (l_tmp != 0) {
5099                 opj_event_msg(p_manager, EVT_WARNING, "Cannot take in charge multiple MCT markers\n");
5100                 return OPJ_TRUE;
5101         }
5102
5103         p_header_size -= 6;
5104
5105         l_mct_data->m_data = (OPJ_BYTE*)opj_malloc(p_header_size);
5106         if (! l_mct_data->m_data) {
5107                 opj_event_msg(p_manager, EVT_ERROR, "Error reading MCT marker\n");
5108                 return OPJ_FALSE;
5109         }
5110         memcpy(l_mct_data->m_data,p_header_data,p_header_size);
5111
5112         l_mct_data->m_data_size = p_header_size;
5113         ++l_tcp->m_nb_mct_records;
5114
5115         return OPJ_TRUE;
5116 }
5117
5118 OPJ_BOOL opj_j2k_write_mcc_record(      opj_j2k_t *p_j2k,
5119                                                                 struct opj_simple_mcc_decorrelation_data * p_mcc_record,
5120                                                                 struct opj_stream_private *p_stream,
5121                                                                 struct opj_event_mgr * p_manager )
5122 {
5123         OPJ_UINT32 i;
5124         OPJ_UINT32 l_mcc_size;
5125         OPJ_BYTE * l_current_data = 00;
5126         OPJ_UINT32 l_nb_bytes_for_comp;
5127         OPJ_UINT32 l_mask;
5128         OPJ_UINT32 l_tmcc;
5129
5130         /* preconditions */
5131         assert(p_j2k != 00);
5132         assert(p_manager != 00);
5133         assert(p_stream != 00);
5134
5135         if (p_mcc_record->m_nb_comps > 255 ) {
5136         l_nb_bytes_for_comp = 2;
5137                 l_mask = 0x8000;
5138         }
5139         else {
5140                 l_nb_bytes_for_comp = 1;
5141                 l_mask = 0;
5142         }
5143
5144         l_mcc_size = p_mcc_record->m_nb_comps * 2 * l_nb_bytes_for_comp + 19;
5145         if (l_mcc_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size)
5146         {
5147                 OPJ_BYTE *new_header_tile_data = (OPJ_BYTE *) opj_realloc(p_j2k->m_specific_param.m_encoder.m_header_tile_data, l_mcc_size);
5148                 if (! new_header_tile_data) {
5149                         opj_free(p_j2k->m_specific_param.m_encoder.m_header_tile_data);
5150                         p_j2k->m_specific_param.m_encoder.m_header_tile_data = NULL;
5151                         p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = 0;
5152                         opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to write MCC marker\n");
5153                         return OPJ_FALSE;
5154                 }
5155                 p_j2k->m_specific_param.m_encoder.m_header_tile_data = new_header_tile_data;
5156                 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_mcc_size;
5157         }
5158
5159         l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
5160
5161         opj_write_bytes(l_current_data,J2K_MS_MCC,2);                                   /* MCC */
5162         l_current_data += 2;
5163
5164         opj_write_bytes(l_current_data,l_mcc_size-2,2);                                 /* Lmcc */
5165         l_current_data += 2;
5166
5167         /* first marker */
5168         opj_write_bytes(l_current_data,0,2);                                    /* Zmcc */
5169         l_current_data += 2;
5170
5171         opj_write_bytes(l_current_data,p_mcc_record->m_index,1);                                        /* Imcc -> no need for other values, take the first */
5172         ++l_current_data;
5173
5174         /* only one marker atm */
5175         opj_write_bytes(l_current_data,0,2);                                    /* Ymcc */
5176         l_current_data+=2;
5177
5178         opj_write_bytes(l_current_data,1,2);                                    /* Qmcc -> number of collections -> 1 */
5179         l_current_data+=2;
5180
5181         opj_write_bytes(l_current_data,0x1,1);                                  /* Xmcci type of component transformation -> array based decorrelation */
5182         ++l_current_data;
5183
5184         opj_write_bytes(l_current_data,p_mcc_record->m_nb_comps | l_mask,2);    /* Nmcci number of input components involved and size for each component offset = 8 bits */
5185         l_current_data+=2;
5186
5187         for (i=0;i<p_mcc_record->m_nb_comps;++i) {
5188                 opj_write_bytes(l_current_data,i,l_nb_bytes_for_comp);                          /* Cmccij Component offset*/
5189                 l_current_data+=l_nb_bytes_for_comp;
5190         }
5191
5192         opj_write_bytes(l_current_data,p_mcc_record->m_nb_comps|l_mask,2);      /* Mmcci number of output components involved and size for each component offset = 8 bits */
5193         l_current_data+=2;
5194
5195         for (i=0;i<p_mcc_record->m_nb_comps;++i)
5196         {
5197                 opj_write_bytes(l_current_data,i,l_nb_bytes_for_comp);                          /* Wmccij Component offset*/
5198                 l_current_data+=l_nb_bytes_for_comp;
5199         }
5200
5201         l_tmcc = ((!p_mcc_record->m_is_irreversible)&1)<<16;
5202
5203         if (p_mcc_record->m_decorrelation_array) {
5204                 l_tmcc |= p_mcc_record->m_decorrelation_array->m_index;
5205         }
5206
5207         if (p_mcc_record->m_offset_array) {
5208                 l_tmcc |= ((p_mcc_record->m_offset_array->m_index)<<8);
5209         }
5210
5211         opj_write_bytes(l_current_data,l_tmcc,3);       /* Tmcci : use MCT defined as number 1 and irreversible array based. */
5212         l_current_data+=3;
5213
5214         if (opj_stream_write_data(p_stream,p_j2k->m_specific_param.m_encoder.m_header_tile_data,l_mcc_size,p_manager) != l_mcc_size) {
5215                 return OPJ_FALSE;
5216         }
5217
5218         return OPJ_TRUE;
5219 }
5220
5221 OPJ_BOOL opj_j2k_read_mcc (     opj_j2k_t *p_j2k,
5222                                                 OPJ_BYTE * p_header_data,
5223                                                 OPJ_UINT32 p_header_size,
5224                                                 opj_event_mgr_t * p_manager )
5225 {
5226         OPJ_UINT32 i,j;
5227         OPJ_UINT32 l_tmp;
5228         OPJ_UINT32 l_indix;
5229         opj_tcp_t * l_tcp;
5230         opj_simple_mcc_decorrelation_data_t * l_mcc_record;
5231         opj_mct_data_t * l_mct_data;
5232         OPJ_UINT32 l_nb_collections;
5233         OPJ_UINT32 l_nb_comps;
5234         OPJ_UINT32 l_nb_bytes_by_comp;
5235
5236         /* preconditions */
5237         assert(p_header_data != 00);
5238         assert(p_j2k != 00);
5239         assert(p_manager != 00);
5240
5241         l_tcp = p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH ?
5242                         &p_j2k->m_cp.tcps[p_j2k->m_current_tile_number] :
5243                         p_j2k->m_specific_param.m_decoder.m_default_tcp;
5244
5245         if (p_header_size < 2) {
5246                 opj_event_msg(p_manager, EVT_ERROR, "Error reading MCC marker\n");
5247                 return OPJ_FALSE;
5248         }
5249
5250         /* first marker */
5251         opj_read_bytes(p_header_data,&l_tmp,2);                         /* Zmcc */
5252         p_header_data += 2;
5253         if (l_tmp != 0) {
5254                 opj_event_msg(p_manager, EVT_WARNING, "Cannot take in charge multiple data spanning\n");
5255                 return OPJ_TRUE;
5256         }
5257
5258         if (p_header_size < 7) {
5259                 opj_event_msg(p_manager, EVT_ERROR, "Error reading MCC marker\n");
5260                 return OPJ_FALSE;
5261         }
5262
5263         opj_read_bytes(p_header_data,&l_indix,1); /* Imcc -> no need for other values, take the first */
5264         ++p_header_data;
5265
5266         l_mcc_record = l_tcp->m_mcc_records;
5267
5268         for(i=0;i<l_tcp->m_nb_mcc_records;++i) {
5269                 if (l_mcc_record->m_index == l_indix) {
5270                         break;
5271                 }
5272                 ++l_mcc_record;
5273         }
5274
5275         /** NOT FOUND */
5276         if (i == l_tcp->m_nb_mcc_records) {
5277                 if (l_tcp->m_nb_mcc_records == l_tcp->m_nb_max_mcc_records) {
5278                         opj_simple_mcc_decorrelation_data_t *new_mcc_records;
5279                         l_tcp->m_nb_max_mcc_records += OPJ_J2K_MCC_DEFAULT_NB_RECORDS;
5280
5281                         new_mcc_records = (opj_simple_mcc_decorrelation_data_t *) opj_realloc(
5282                                         l_tcp->m_mcc_records, l_tcp->m_nb_max_mcc_records * sizeof(opj_simple_mcc_decorrelation_data_t));
5283                         if (! new_mcc_records) {
5284                                 opj_free(l_tcp->m_mcc_records);
5285                                 l_tcp->m_mcc_records = NULL;
5286                                 l_tcp->m_nb_max_mcc_records = 0;
5287                                 l_tcp->m_nb_mcc_records = 0;
5288                                 opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to read MCC marker\n");
5289                                 return OPJ_FALSE;
5290                         }
5291                         l_tcp->m_mcc_records = new_mcc_records;
5292                         l_mcc_record = l_tcp->m_mcc_records + l_tcp->m_nb_mcc_records;
5293                         memset(l_mcc_record,0,(l_tcp->m_nb_max_mcc_records-l_tcp->m_nb_mcc_records) * sizeof(opj_simple_mcc_decorrelation_data_t));
5294                 }
5295                 l_mcc_record = l_tcp->m_mcc_records + l_tcp->m_nb_mcc_records;
5296         }
5297         l_mcc_record->m_index = l_indix;
5298
5299         /* only one marker atm */
5300         opj_read_bytes(p_header_data,&l_tmp,2);                         /* Ymcc */
5301         p_header_data+=2;
5302         if (l_tmp != 0) {
5303                 opj_event_msg(p_manager, EVT_WARNING, "Cannot take in charge multiple data spanning\n");
5304                 return OPJ_TRUE;
5305         }
5306
5307         opj_read_bytes(p_header_data,&l_nb_collections,2);                              /* Qmcc -> number of collections -> 1 */
5308         p_header_data+=2;
5309
5310         if (l_nb_collections > 1) {
5311                 opj_event_msg(p_manager, EVT_WARNING, "Cannot take in charge multiple collections\n");
5312                 return OPJ_TRUE;
5313         }
5314
5315         p_header_size -= 7;
5316
5317         for (i=0;i<l_nb_collections;++i) {
5318                 if (p_header_size < 3) {
5319                         opj_event_msg(p_manager, EVT_ERROR, "Error reading MCC marker\n");
5320                         return OPJ_FALSE;
5321                 }
5322
5323                 opj_read_bytes(p_header_data,&l_tmp,1); /* Xmcci type of component transformation -> array based decorrelation */
5324                 ++p_header_data;
5325
5326                 if (l_tmp != 1) {
5327                         opj_event_msg(p_manager, EVT_WARNING, "Cannot take in charge collections other than array decorrelation\n");
5328                         return OPJ_TRUE;
5329                 }
5330
5331                 opj_read_bytes(p_header_data,&l_nb_comps,2);
5332
5333                 p_header_data+=2;
5334                 p_header_size-=3;
5335
5336                 l_nb_bytes_by_comp = 1 + (l_nb_comps>>15);
5337                 l_mcc_record->m_nb_comps = l_nb_comps & 0x7fff;
5338
5339                 if (p_header_size < (l_nb_bytes_by_comp * l_mcc_record->m_nb_comps + 2)) {
5340                         opj_event_msg(p_manager, EVT_ERROR, "Error reading MCC marker\n");
5341                         return OPJ_FALSE;
5342                 }
5343
5344                 p_header_size -= (l_nb_bytes_by_comp * l_mcc_record->m_nb_comps + 2);
5345
5346                 for (j=0;j<l_mcc_record->m_nb_comps;++j) {
5347                         opj_read_bytes(p_header_data,&l_tmp,l_nb_bytes_by_comp);        /* Cmccij Component offset*/
5348                         p_header_data+=l_nb_bytes_by_comp;
5349
5350                         if (l_tmp != j) {
5351                                 opj_event_msg(p_manager, EVT_WARNING, "Cannot take in charge collections with indix shuffle\n");
5352                                 return OPJ_TRUE;
5353                         }
5354                 }
5355
5356                 opj_read_bytes(p_header_data,&l_nb_comps,2);
5357                 p_header_data+=2;
5358
5359                 l_nb_bytes_by_comp = 1 + (l_nb_comps>>15);
5360                 l_nb_comps &= 0x7fff;
5361
5362                 if (l_nb_comps != l_mcc_record->m_nb_comps) {
5363                         opj_event_msg(p_manager, EVT_WARNING, "Cannot take in charge collections without same number of indixes\n");
5364                         return OPJ_TRUE;
5365                 }
5366
5367                 if (p_header_size < (l_nb_bytes_by_comp * l_mcc_record->m_nb_comps + 3)) {
5368                         opj_event_msg(p_manager, EVT_ERROR, "Error reading MCC marker\n");
5369                         return OPJ_FALSE;
5370                 }
5371
5372                 p_header_size -= (l_nb_bytes_by_comp * l_mcc_record->m_nb_comps + 3);
5373
5374                 for (j=0;j<l_mcc_record->m_nb_comps;++j) {
5375                         opj_read_bytes(p_header_data,&l_tmp,l_nb_bytes_by_comp);        /* Wmccij Component offset*/
5376                         p_header_data+=l_nb_bytes_by_comp;
5377
5378                         if (l_tmp != j) {
5379                                 opj_event_msg(p_manager, EVT_WARNING, "Cannot take in charge collections with indix shuffle\n");
5380                                 return OPJ_TRUE;
5381                         }
5382                 }
5383
5384                 opj_read_bytes(p_header_data,&l_tmp,3); /* Wmccij Component offset*/
5385                 p_header_data += 3;
5386
5387                 l_mcc_record->m_is_irreversible = ! ((l_tmp>>16) & 1);
5388                 l_mcc_record->m_decorrelation_array = 00;
5389                 l_mcc_record->m_offset_array = 00;
5390
5391                 l_indix = l_tmp & 0xff;
5392                 if (l_indix != 0) {
5393                         l_mct_data = l_tcp->m_mct_records;
5394                         for (j=0;j<l_tcp->m_nb_mct_records;++j) {
5395                                 if (l_mct_data->m_index == l_indix) {
5396                                         l_mcc_record->m_decorrelation_array = l_mct_data;
5397                                         break;
5398                                 }
5399                                 ++l_mct_data;
5400                         }
5401
5402                         if (l_mcc_record->m_decorrelation_array == 00) {
5403                                 opj_event_msg(p_manager, EVT_ERROR, "Error reading MCC marker\n");
5404                                 return OPJ_FALSE;
5405                         }
5406                 }
5407
5408                 l_indix = (l_tmp >> 8) & 0xff;
5409                 if (l_indix != 0) {
5410                         l_mct_data = l_tcp->m_mct_records;
5411                         for (j=0;j<l_tcp->m_nb_mct_records;++j) {
5412                                 if (l_mct_data->m_index == l_indix) {
5413                                         l_mcc_record->m_offset_array = l_mct_data;
5414                                         break;
5415                                 }
5416                                 ++l_mct_data;
5417                         }
5418
5419                         if (l_mcc_record->m_offset_array == 00) {
5420                                 opj_event_msg(p_manager, EVT_ERROR, "Error reading MCC marker\n");
5421                                 return OPJ_FALSE;
5422                         }
5423                 }
5424         }
5425
5426         if (p_header_size != 0) {
5427                 opj_event_msg(p_manager, EVT_ERROR, "Error reading MCC marker\n");
5428                 return OPJ_FALSE;
5429         }
5430
5431         ++l_tcp->m_nb_mcc_records;
5432
5433         return OPJ_TRUE;
5434 }
5435
5436 OPJ_BOOL opj_j2k_write_mco(     opj_j2k_t *p_j2k,
5437                                                 struct opj_stream_private *p_stream,
5438                                                 struct opj_event_mgr * p_manager
5439                                   )
5440 {
5441         OPJ_BYTE * l_current_data = 00;
5442         OPJ_UINT32 l_mco_size;
5443         opj_tcp_t * l_tcp = 00;
5444         opj_simple_mcc_decorrelation_data_t * l_mcc_record;
5445         OPJ_UINT32 i;
5446
5447         /* preconditions */
5448         assert(p_j2k != 00);
5449         assert(p_manager != 00);
5450         assert(p_stream != 00);
5451
5452         l_tcp =&(p_j2k->m_cp.tcps[p_j2k->m_current_tile_number]);
5453         l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
5454
5455         l_mco_size = 5 + l_tcp->m_nb_mcc_records;
5456         if (l_mco_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
5457
5458                 OPJ_BYTE *new_header_tile_data = (OPJ_BYTE *) opj_realloc(p_j2k->m_specific_param.m_encoder.m_header_tile_data, l_mco_size);
5459                 if (! new_header_tile_data) {
5460                         opj_free(p_j2k->m_specific_param.m_encoder.m_header_tile_data);
5461                         p_j2k->m_specific_param.m_encoder.m_header_tile_data = NULL;
5462                         p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = 0;
5463                         opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to write MCO marker\n");
5464                         return OPJ_FALSE;
5465                 }
5466                 p_j2k->m_specific_param.m_encoder.m_header_tile_data = new_header_tile_data;
5467                 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_mco_size;
5468         }
5469
5470         opj_write_bytes(l_current_data,J2K_MS_MCO,2);                   /* MCO */
5471         l_current_data += 2;
5472
5473         opj_write_bytes(l_current_data,l_mco_size-2,2);                 /* Lmco */
5474         l_current_data += 2;
5475
5476         opj_write_bytes(l_current_data,l_tcp->m_nb_mcc_records,1);      /* Nmco : only one tranform stage*/
5477         ++l_current_data;
5478
5479         l_mcc_record = l_tcp->m_mcc_records;
5480         for     (i=0;i<l_tcp->m_nb_mcc_records;++i) {
5481                 opj_write_bytes(l_current_data,l_mcc_record->m_index,1);/* Imco -> use the mcc indicated by 1*/
5482                 ++l_current_data;
5483
5484                 ++l_mcc_record;
5485         }
5486
5487         if (opj_stream_write_data(p_stream,p_j2k->m_specific_param.m_encoder.m_header_tile_data,l_mco_size,p_manager) != l_mco_size) {
5488                 return OPJ_FALSE;
5489         }
5490
5491         return OPJ_TRUE;
5492 }
5493
5494 /**
5495  * Reads a MCO marker (Multiple Component Transform Ordering)
5496  *
5497  * @param       p_header_data   the data contained in the MCO box.
5498  * @param       p_j2k                   the jpeg2000 codec.
5499  * @param       p_header_size   the size of the data contained in the MCO marker.
5500  * @param       p_manager               the user event manager.
5501 */
5502 static OPJ_BOOL opj_j2k_read_mco (      opj_j2k_t *p_j2k,
5503                                                                     OPJ_BYTE * p_header_data,
5504                                                                     OPJ_UINT32 p_header_size,
5505                                                                     opj_event_mgr_t * p_manager
5506                                     )
5507 {
5508         OPJ_UINT32 l_tmp, i;
5509         OPJ_UINT32 l_nb_stages;
5510         opj_tcp_t * l_tcp;
5511         opj_tccp_t * l_tccp;
5512         opj_image_t * l_image;
5513
5514         /* preconditions */
5515         assert(p_header_data != 00);
5516         assert(p_j2k != 00);
5517         assert(p_manager != 00);
5518
5519         l_image = p_j2k->m_private_image;
5520         l_tcp = p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH ?
5521                         &p_j2k->m_cp.tcps[p_j2k->m_current_tile_number] :
5522                         p_j2k->m_specific_param.m_decoder.m_default_tcp;
5523
5524         if (p_header_size < 1) {
5525                 opj_event_msg(p_manager, EVT_ERROR, "Error reading MCO marker\n");
5526                 return OPJ_FALSE;
5527         }
5528
5529         opj_read_bytes(p_header_data,&l_nb_stages,1);                           /* Nmco : only one tranform stage*/
5530         ++p_header_data;
5531
5532         if (l_nb_stages > 1) {
5533                 opj_event_msg(p_manager, EVT_WARNING, "Cannot take in charge multiple transformation stages.\n");
5534                 return OPJ_TRUE;
5535         }
5536
5537         if (p_header_size != l_nb_stages + 1) {
5538                 opj_event_msg(p_manager, EVT_WARNING, "Error reading MCO marker\n");
5539                 return OPJ_FALSE;
5540         }
5541
5542         l_tccp = l_tcp->tccps;
5543
5544         for (i=0;i<l_image->numcomps;++i) {
5545                 l_tccp->m_dc_level_shift = 0;
5546                 ++l_tccp;
5547         }
5548
5549         if (l_tcp->m_mct_decoding_matrix) {
5550                 opj_free(l_tcp->m_mct_decoding_matrix);
5551                 l_tcp->m_mct_decoding_matrix = 00;
5552         }
5553
5554         for (i=0;i<l_nb_stages;++i) {
5555                 opj_read_bytes(p_header_data,&l_tmp,1);
5556                 ++p_header_data;
5557
5558                 if (! opj_j2k_add_mct(l_tcp,p_j2k->m_private_image,l_tmp)) {
5559                         return OPJ_FALSE;
5560                 }
5561         }
5562
5563         return OPJ_TRUE;
5564 }
5565
5566 OPJ_BOOL opj_j2k_add_mct(opj_tcp_t * p_tcp, opj_image_t * p_image, OPJ_UINT32 p_index)
5567 {
5568         OPJ_UINT32 i;
5569         opj_simple_mcc_decorrelation_data_t * l_mcc_record;
5570         opj_mct_data_t * l_deco_array, * l_offset_array;
5571         OPJ_UINT32 l_data_size,l_mct_size, l_offset_size;
5572         OPJ_UINT32 l_nb_elem;
5573         OPJ_UINT32 * l_offset_data, * l_current_offset_data;
5574         opj_tccp_t * l_tccp;
5575
5576         /* preconditions */
5577         assert(p_tcp != 00);
5578
5579         l_mcc_record = p_tcp->m_mcc_records;
5580
5581         for (i=0;i<p_tcp->m_nb_mcc_records;++i) {
5582                 if (l_mcc_record->m_index == p_index) {
5583                         break;
5584                 }
5585         }
5586
5587         if (i==p_tcp->m_nb_mcc_records) {
5588                 /** element discarded **/
5589                 return OPJ_TRUE;
5590         }
5591
5592         if (l_mcc_record->m_nb_comps != p_image->numcomps) {
5593                 /** do not support number of comps != image */
5594                 return OPJ_TRUE;
5595         }
5596
5597         l_deco_array = l_mcc_record->m_decorrelation_array;
5598
5599         if (l_deco_array) {
5600                 l_data_size = MCT_ELEMENT_SIZE[l_deco_array->m_element_type] * p_image->numcomps * p_image->numcomps;
5601                 if (l_deco_array->m_data_size != l_data_size) {
5602                         return OPJ_FALSE;
5603                 }
5604
5605                 l_nb_elem = p_image->numcomps * p_image->numcomps;
5606                 l_mct_size = l_nb_elem * sizeof(OPJ_FLOAT32);
5607                 p_tcp->m_mct_decoding_matrix = (OPJ_FLOAT32*)opj_malloc(l_mct_size);
5608
5609                 if (! p_tcp->m_mct_decoding_matrix ) {
5610                         return OPJ_FALSE;
5611                 }
5612
5613                 j2k_mct_read_functions_to_float[l_deco_array->m_element_type](l_deco_array->m_data,p_tcp->m_mct_decoding_matrix,l_nb_elem);
5614         }
5615
5616         l_offset_array = l_mcc_record->m_offset_array;
5617
5618         if (l_offset_array) {
5619                 l_data_size = MCT_ELEMENT_SIZE[l_offset_array->m_element_type] * p_image->numcomps;
5620                 if (l_offset_array->m_data_size != l_data_size) {
5621                         return OPJ_FALSE;
5622                 }
5623
5624                 l_nb_elem = p_image->numcomps;
5625                 l_offset_size = l_nb_elem * sizeof(OPJ_UINT32);
5626                 l_offset_data = (OPJ_UINT32*)opj_malloc(l_offset_size);
5627
5628                 if (! l_offset_data ) {
5629                         return OPJ_FALSE;
5630                 }
5631
5632                 j2k_mct_read_functions_to_int32[l_offset_array->m_element_type](l_offset_array->m_data,l_offset_data,l_nb_elem);
5633
5634                 l_tccp = p_tcp->tccps;
5635                 l_current_offset_data = l_offset_data;
5636
5637                 for (i=0;i<p_image->numcomps;++i) {
5638                         l_tccp->m_dc_level_shift = *(l_current_offset_data++);
5639                         ++l_tccp;
5640                 }
5641
5642                 opj_free(l_offset_data);
5643         }
5644
5645         return OPJ_TRUE;
5646 }
5647
5648 OPJ_BOOL opj_j2k_write_cbd( opj_j2k_t *p_j2k,
5649                                                 struct opj_stream_private *p_stream,
5650                                                 struct opj_event_mgr * p_manager )
5651 {
5652         OPJ_UINT32 i;
5653         OPJ_UINT32 l_cbd_size;
5654         OPJ_BYTE * l_current_data = 00;
5655         opj_image_t *l_image = 00;
5656         opj_image_comp_t * l_comp = 00;
5657
5658         /* preconditions */
5659         assert(p_j2k != 00);
5660         assert(p_manager != 00);
5661         assert(p_stream != 00);
5662
5663         l_image = p_j2k->m_private_image;
5664         l_cbd_size = 6 + p_j2k->m_private_image->numcomps;
5665
5666         if (l_cbd_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
5667                 OPJ_BYTE *new_header_tile_data = (OPJ_BYTE *) opj_realloc(p_j2k->m_specific_param.m_encoder.m_header_tile_data, l_cbd_size);
5668                 if (! new_header_tile_data) {
5669                         opj_free(p_j2k->m_specific_param.m_encoder.m_header_tile_data);
5670                         p_j2k->m_specific_param.m_encoder.m_header_tile_data = NULL;
5671                         p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = 0;
5672                         opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to write CBD marker\n");
5673                         return OPJ_FALSE;
5674                 }
5675                 p_j2k->m_specific_param.m_encoder.m_header_tile_data = new_header_tile_data;
5676                 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_cbd_size;
5677         }
5678
5679         l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
5680
5681         opj_write_bytes(l_current_data,J2K_MS_CBD,2);                   /* CBD */
5682         l_current_data += 2;
5683
5684         opj_write_bytes(l_current_data,l_cbd_size-2,2);                 /* L_CBD */
5685         l_current_data += 2;
5686
5687         opj_write_bytes(l_current_data,l_image->numcomps, 2);           /* Ncbd */
5688         l_current_data+=2;
5689
5690         l_comp = l_image->comps;
5691
5692         for (i=0;i<l_image->numcomps;++i) {
5693                 opj_write_bytes(l_current_data, (l_comp->sgnd << 7) | (l_comp->prec - 1), 1);           /* Component bit depth */
5694                 ++l_current_data;
5695
5696                 ++l_comp;
5697         }
5698
5699         if (opj_stream_write_data(p_stream,p_j2k->m_specific_param.m_encoder.m_header_tile_data,l_cbd_size,p_manager) != l_cbd_size) {
5700                 return OPJ_FALSE;
5701         }
5702
5703         return OPJ_TRUE;
5704 }
5705
5706 /**
5707  * Reads a CBD marker (Component bit depth definition)
5708  * @param       p_header_data   the data contained in the CBD box.
5709  * @param       p_j2k                   the jpeg2000 codec.
5710  * @param       p_header_size   the size of the data contained in the CBD marker.
5711  * @param       p_manager               the user event manager.
5712 */
5713 static OPJ_BOOL opj_j2k_read_cbd (      opj_j2k_t *p_j2k,
5714                                                                 OPJ_BYTE * p_header_data,
5715                                                                 OPJ_UINT32 p_header_size,
5716                                                                 opj_event_mgr_t * p_manager
5717                                     )
5718 {
5719         OPJ_UINT32 l_nb_comp,l_num_comp;
5720         OPJ_UINT32 l_comp_def;
5721         OPJ_UINT32 i;
5722         opj_image_comp_t * l_comp = 00;
5723
5724         /* preconditions */
5725         assert(p_header_data != 00);
5726         assert(p_j2k != 00);
5727         assert(p_manager != 00);
5728
5729         l_num_comp = p_j2k->m_private_image->numcomps;
5730
5731         if (p_header_size != (p_j2k->m_private_image->numcomps + 2)) {
5732                 opj_event_msg(p_manager, EVT_ERROR, "Crror reading CBD marker\n");
5733                 return OPJ_FALSE;
5734         }
5735
5736         opj_read_bytes(p_header_data,&l_nb_comp,2);                             /* Ncbd */
5737         p_header_data+=2;
5738
5739         if (l_nb_comp != l_num_comp) {
5740                 opj_event_msg(p_manager, EVT_ERROR, "Crror reading CBD marker\n");
5741                 return OPJ_FALSE;
5742         }
5743
5744         l_comp = p_j2k->m_private_image->comps;
5745         for (i=0;i<l_num_comp;++i) {
5746                 opj_read_bytes(p_header_data,&l_comp_def,1);                    /* Component bit depth */
5747                 ++p_header_data;
5748         l_comp->sgnd = (l_comp_def>>7) & 1;
5749                 l_comp->prec = (l_comp_def&0x7f) + 1;
5750                 ++l_comp;
5751         }
5752
5753         return OPJ_TRUE;
5754 }
5755
5756 /* ----------------------------------------------------------------------- */
5757 /* J2K / JPT decoder interface                                             */
5758 /* ----------------------------------------------------------------------- */
5759
5760 void opj_j2k_setup_decoder(opj_j2k_t *j2k, opj_dparameters_t *parameters)
5761 {
5762         if(j2k && parameters) {
5763                 j2k->m_cp.m_specific_param.m_dec.m_layer = parameters->cp_layer;
5764                 j2k->m_cp.m_specific_param.m_dec.m_reduce = parameters->cp_reduce;
5765
5766 #ifdef USE_JPWL
5767                 j2k->m_cp.correct = parameters->jpwl_correct;
5768                 j2k->m_cp.exp_comps = parameters->jpwl_exp_comps;
5769                 j2k->m_cp.max_tiles = parameters->jpwl_max_tiles;
5770 #endif /* USE_JPWL */
5771         }
5772 }
5773
5774 /* ----------------------------------------------------------------------- */
5775 /* J2K encoder interface                                                       */
5776 /* ----------------------------------------------------------------------- */
5777
5778 opj_j2k_t* opj_j2k_create_compress(void)
5779 {
5780         opj_j2k_t *l_j2k = (opj_j2k_t*) opj_malloc(sizeof(opj_j2k_t));
5781         if (!l_j2k) {
5782                 return NULL;
5783         }
5784
5785         memset(l_j2k,0,sizeof(opj_j2k_t));
5786
5787         l_j2k->m_is_decoder = 0;
5788         l_j2k->m_cp.m_is_decoder = 0;
5789
5790         l_j2k->m_specific_param.m_encoder.m_header_tile_data = (OPJ_BYTE *) opj_malloc(OPJ_J2K_DEFAULT_HEADER_SIZE);
5791         if (! l_j2k->m_specific_param.m_encoder.m_header_tile_data) {
5792                 opj_j2k_destroy(l_j2k);
5793                 return NULL;
5794         }
5795
5796         l_j2k->m_specific_param.m_encoder.m_header_tile_data_size = OPJ_J2K_DEFAULT_HEADER_SIZE;
5797
5798         /* validation list creation*/
5799         l_j2k->m_validation_list = opj_procedure_list_create();
5800         if (! l_j2k->m_validation_list) {
5801                 opj_j2k_destroy(l_j2k);
5802                 return NULL;
5803         }
5804
5805         /* execution list creation*/
5806         l_j2k->m_procedure_list = opj_procedure_list_create();
5807         if (! l_j2k->m_procedure_list) {
5808                 opj_j2k_destroy(l_j2k);
5809                 return NULL;
5810         }
5811
5812         return l_j2k;
5813 }
5814
5815 int opj_j2k_initialise_4K_poc(opj_poc_t *POC, int numres){
5816     POC[0].tile  = 1;
5817     POC[0].resno0  = 0;
5818     POC[0].compno0 = 0;
5819     POC[0].layno1  = 1;
5820     POC[0].resno1  = numres-1;
5821     POC[0].compno1 = 3;
5822     POC[0].prg1 = OPJ_CPRL;
5823     POC[1].tile  = 1;
5824     POC[1].resno0  = numres-1;
5825     POC[1].compno0 = 0;
5826     POC[1].layno1  = 1;
5827     POC[1].resno1  = numres;
5828     POC[1].compno1 = 3;
5829     POC[1].prg1 = OPJ_CPRL;
5830     return 2;
5831 }
5832
5833 void opj_j2k_set_cinema_parameters(opj_cparameters_t *parameters, opj_image_t *image, opj_event_mgr_t *p_manager)
5834 {
5835     /* Configure cinema parameters */
5836     float max_rate = 0;
5837     float temp_rate = 0;
5838     int i;
5839
5840     /* profile (Rsiz) */
5841     switch (parameters->cp_cinema){
5842     case OPJ_CINEMA2K_24:
5843     case OPJ_CINEMA2K_48:
5844         parameters->cp_rsiz = OPJ_CINEMA2K;
5845         break;
5846     case OPJ_CINEMA4K_24:
5847         parameters->cp_rsiz = OPJ_CINEMA4K;
5848         break;
5849     }
5850
5851     /* No tiling */
5852     parameters->tile_size_on = OPJ_FALSE;
5853     parameters->cp_tdx=1;
5854     parameters->cp_tdy=1;
5855
5856     /* One tile part for each component */
5857     parameters->tp_flag = 'C';
5858     parameters->tp_on = 1;
5859
5860     /* Tile and Image shall be at (0,0) */
5861     parameters->cp_tx0 = 0;
5862     parameters->cp_ty0 = 0;
5863     parameters->image_offset_x0 = 0;
5864     parameters->image_offset_y0 = 0;
5865
5866     /* Codeblock size= 32*32 */
5867     parameters->cblockw_init = 32;
5868     parameters->cblockh_init = 32;
5869
5870     /* Codeblock style: no mode switch enabled */
5871     parameters->mode = 0;
5872
5873     /* No ROI */
5874     parameters->roi_compno = -1;
5875
5876     /* No subsampling */
5877     parameters->subsampling_dx = 1;
5878     parameters->subsampling_dy = 1;
5879
5880     /* 9-7 transform */
5881     parameters->irreversible = 1;
5882
5883     /* Number of layers */
5884     if (parameters->tcp_numlayers > 1){
5885         opj_event_msg(p_manager, EVT_WARNING,
5886                 "JPEG 2000 Profile-3 and 4 (2k/4k dc profile) requires:\n"
5887                 "1 single quality layer"
5888                 "-> Number of layers forced to 1 (rather than %d)\n",
5889                 parameters->tcp_numlayers);
5890         parameters->tcp_numlayers = 1;
5891     }
5892
5893     /* Resolution levels */
5894     switch (parameters->cp_cinema){
5895     case OPJ_CINEMA2K_24:
5896     case OPJ_CINEMA2K_48:
5897         if(parameters->numresolution > 6){
5898             opj_event_msg(p_manager, EVT_WARNING,
5899                     "JPEG 2000 Profile-3 (2k dc profile) requires:\n"
5900                     "Number of decomposition levels <= 5\n"
5901                     "-> Number of decomposition levels forced to 5 (rather than %d)\n",
5902                     parameters->numresolution+1);
5903             parameters->numresolution = 6;
5904         }
5905         break;
5906     case OPJ_CINEMA4K_24:
5907         if(parameters->numresolution < 2){
5908             opj_event_msg(p_manager, EVT_WARNING,
5909                     "JPEG 2000 Profile-4 (4k dc profile) requires:\n"
5910                     "Number of decomposition levels >= 1 && <= 6\n"
5911                     "-> Number of decomposition levels forced to 1 (rather than %d)\n",
5912                     parameters->numresolution+1);
5913             parameters->numresolution = 1;
5914         }else if(parameters->numresolution > 7){
5915             opj_event_msg(p_manager, EVT_WARNING,
5916                     "JPEG 2000 Profile-4 (4k dc profile) requires:\n"
5917                     "Number of decomposition levels >= 1 && <= 6\n"
5918                     "-> Number of decomposition levels forced to 6 (rather than %d)\n",
5919                     parameters->numresolution+1);
5920             parameters->numresolution = 7;
5921         }
5922         break;
5923     default :
5924         break;
5925     }
5926
5927     /* Precincts */
5928     parameters->csty |= 0x01;
5929     parameters->res_spec = parameters->numresolution-1;
5930     for (i = 0; i<parameters->res_spec; i++) {
5931         parameters->prcw_init[i] = 256;
5932         parameters->prch_init[i] = 256;
5933     }
5934
5935     /* The progression order shall be CPRL */
5936     parameters->prog_order = OPJ_CPRL;
5937
5938     /* Progression order changes for 4K, disallowed for 2K */
5939     if (parameters->cp_cinema == OPJ_CINEMA4K_24) {
5940         parameters->numpocs = opj_j2k_initialise_4K_poc(parameters->POC,parameters->numresolution);
5941     } else {
5942         parameters->numpocs = 0;
5943     }
5944
5945     /* Limited bit-rate */
5946     parameters->cp_disto_alloc = 1;
5947     switch (parameters->cp_cinema){
5948     case OPJ_CINEMA2K_24:
5949     case OPJ_CINEMA4K_24:
5950         max_rate = ((float) (image->numcomps * image->comps[0].w * image->comps[0].h * image->comps[0].prec))/
5951                 (CINEMA_24_CS * 8 * image->comps[0].dx * image->comps[0].dy);
5952         if (parameters->tcp_rates[0] == 0){
5953             parameters->tcp_rates[0] = max_rate;
5954         }else{
5955             temp_rate =((float) (image->numcomps * image->comps[0].w * image->comps[0].h * image->comps[0].prec))/
5956                     (parameters->tcp_rates[0] * 8 * image->comps[0].dx * image->comps[0].dy);
5957             if (temp_rate > CINEMA_24_CS ){
5958                 opj_event_msg(p_manager, EVT_WARNING,
5959                         "JPEG 2000 Profile-3 and 4 (2k/4k dc profile) requires:\n"
5960                         "Maximum 1302083 compressed bytes @ 24fps\n"
5961                         "-> Specified rate (%3.1f) exceeds this limit. Rate will be forced to %3.1f.\n",
5962                         parameters->tcp_rates[0], max_rate);
5963                 parameters->tcp_rates[0]= max_rate;
5964             }else{
5965                 opj_event_msg(p_manager, EVT_WARNING,
5966                         "JPEG 2000 Profile-3 and 4 (2k/4k dc profile):\n"
5967                         "INFO : Specified rate (%3.1f) is below the 2k/4k limit @ 24fps.\n",
5968                         parameters->tcp_rates[0]);
5969             }
5970         }
5971         parameters->max_comp_size = COMP_24_CS;
5972         break;
5973     case OPJ_CINEMA2K_48:
5974         max_rate = ((float) (image->numcomps * image->comps[0].w * image->comps[0].h * image->comps[0].prec))/
5975                 (CINEMA_48_CS * 8 * image->comps[0].dx * image->comps[0].dy);
5976         if (parameters->tcp_rates[0] == 0){
5977             parameters->tcp_rates[0] = max_rate;
5978         }else{
5979             temp_rate =((float) (image->numcomps * image->comps[0].w * image->comps[0].h * image->comps[0].prec))/
5980                     (parameters->tcp_rates[0] * 8 * image->comps[0].dx * image->comps[0].dy);
5981             if (temp_rate > CINEMA_48_CS ){
5982                 opj_event_msg(p_manager, EVT_WARNING,
5983                         "JPEG 2000 Profile-3 (2k dc profile) requires:\n"
5984                         "Maximum 651041 compressed bytes @ 48fps\n"
5985                         "-> Specified rate (%3.1f) exceeds this limit. Rate will be forced to %3.1f.\n",
5986                         parameters->tcp_rates[0], max_rate);
5987                 parameters->tcp_rates[0]= max_rate;
5988             }else{
5989                 opj_event_msg(p_manager, EVT_WARNING,
5990                         "JPEG 2000 Profile-3 (2k dc profile):\n"
5991                         "INFO : Specified rate (%3.1f) is below the 2k limit @ 48 fps.\n",
5992                         parameters->tcp_rates[0]);
5993             }
5994         }
5995         parameters->max_comp_size = COMP_48_CS;
5996         break;
5997     default:
5998         break;
5999     }
6000 }
6001
6002 OPJ_BOOL opj_j2k_is_cinema_compliant(opj_image_t *image, OPJ_CINEMA_MODE cinema_mode, opj_event_mgr_t *p_manager)
6003 {
6004     OPJ_UINT32 i;
6005
6006     /* Number of components */
6007     if (image->numcomps != 3){
6008         opj_event_msg(p_manager, EVT_WARNING,
6009                 "JPEG 2000 Profile-3 (2k dc profile) requires:\n"
6010                 "3 components"
6011                 "-> Number of components of input image (%d) is not compliant\n"
6012                 "-> Non-profile-3 codestream will be generated\n",
6013                 image->numcomps);
6014         return OPJ_FALSE;
6015     }
6016
6017     /* Bitdepth */
6018     for (i = 0; i < image->numcomps; i++) {
6019         if ((image->comps[i].bpp != 12) | (image->comps[i].sgnd)){
6020             char signed_str[] = "signed";
6021             char unsigned_str[] = "unsigned";
6022             char *tmp_str = image->comps[i].sgnd?signed_str:unsigned_str;
6023             opj_event_msg(p_manager, EVT_WARNING,
6024                     "JPEG 2000 Profile-3 (2k dc profile) requires:\n"
6025                     "Precision of each component shall be 12 bits unsigned"
6026                     "-> At least component %d of input image (%d bits, %s) is not compliant\n"
6027                     "-> Non-profile-3 codestream will be generated\n",
6028                     i,image->comps[i].bpp, tmp_str);
6029             return OPJ_FALSE;
6030         }
6031     }
6032
6033     /* Image size */
6034     switch (cinema_mode){
6035     case OPJ_CINEMA2K_24:
6036     case OPJ_CINEMA2K_48:
6037         if (((image->comps[0].w > 2048) | (image->comps[0].h > 1080))){
6038             opj_event_msg(p_manager, EVT_WARNING,
6039                     "JPEG 2000 Profile-3 (2k dc profile) requires:\n"
6040                     "width <= 2048 and height <= 1080\n"
6041                     "-> Input image size %d x %d is not compliant\n"
6042                     "-> Non-profile-3 codestream will be generated\n",
6043                     image->comps[0].w,image->comps[0].h);
6044             return OPJ_FALSE;
6045         }
6046         break;
6047     case OPJ_CINEMA4K_24:
6048         if (((image->comps[0].w > 4096) | (image->comps[0].h > 2160))){
6049             opj_event_msg(p_manager, EVT_WARNING,
6050                     "JPEG 2000 Profile-4 (4k dc profile) requires:\n"
6051                     "width <= 4096 and height <= 2160\n"
6052                     "-> Image size %d x %d is not compliant\n"
6053                     "-> Non-profile-4 codestream will be generated\n",
6054                     image->comps[0].w,image->comps[0].h);
6055             return OPJ_FALSE;
6056         }
6057         break;
6058     default :
6059         break;
6060     }
6061
6062     return OPJ_TRUE;
6063 }
6064
6065 void opj_j2k_setup_encoder(     opj_j2k_t *p_j2k,
6066                                                     opj_cparameters_t *parameters,
6067                                                     opj_image_t *image,
6068                                                     opj_event_mgr_t * p_manager)
6069 {
6070         OPJ_UINT32 i, j, tileno, numpocs_tile;
6071         opj_cp_t *cp = 00;
6072
6073         if(!p_j2k || !parameters || ! image) {
6074                 return;
6075         }
6076
6077         /* keep a link to cp so that we can destroy it later in j2k_destroy_compress */
6078         cp = &(p_j2k->m_cp);
6079
6080         /* set default values for cp */
6081         cp->tw = 1;
6082         cp->th = 1;
6083
6084         /* set cinema parameters if required */
6085         if (parameters->cp_cinema){
6086             opj_j2k_set_cinema_parameters(parameters,image,p_manager);
6087             if (!opj_j2k_is_cinema_compliant(image,parameters->cp_cinema,p_manager)) {
6088                 parameters->cp_rsiz = OPJ_STD_RSIZ;
6089             }
6090         }
6091
6092         /*
6093         copy user encoding parameters
6094         */
6095         cp->m_specific_param.m_enc.m_cinema = parameters->cp_cinema;
6096         cp->m_specific_param.m_enc.m_max_comp_size =    parameters->max_comp_size;
6097         cp->rsiz   = parameters->cp_rsiz;
6098         cp->m_specific_param.m_enc.m_disto_alloc = parameters->cp_disto_alloc;
6099         cp->m_specific_param.m_enc.m_fixed_alloc = parameters->cp_fixed_alloc;
6100         cp->m_specific_param.m_enc.m_fixed_quality = parameters->cp_fixed_quality;
6101
6102         /* mod fixed_quality */
6103         if (parameters->cp_fixed_alloc && parameters->cp_matrice) {
6104                 size_t array_size = parameters->tcp_numlayers * parameters->numresolution * 3 * sizeof(OPJ_INT32);
6105                 cp->m_specific_param.m_enc.m_matrice = (OPJ_INT32 *) opj_malloc(array_size);
6106                 memcpy(cp->m_specific_param.m_enc.m_matrice, parameters->cp_matrice, array_size);
6107         }
6108
6109         /* tiles */
6110         cp->tdx = parameters->cp_tdx;
6111         cp->tdy = parameters->cp_tdy;
6112
6113         /* tile offset */
6114         cp->tx0 = parameters->cp_tx0;
6115         cp->ty0 = parameters->cp_ty0;
6116
6117         /* comment string */
6118         if(parameters->cp_comment) {
6119                 cp->comment = (char*)opj_malloc(strlen(parameters->cp_comment) + 1);
6120                 if(cp->comment) {
6121                         strcpy(cp->comment, parameters->cp_comment);
6122                 }
6123         }
6124
6125         /*
6126         calculate other encoding parameters
6127         */
6128
6129         if (parameters->tile_size_on) {
6130                 cp->tw = opj_int_ceildiv(image->x1 - cp->tx0, cp->tdx);
6131                 cp->th = opj_int_ceildiv(image->y1 - cp->ty0, cp->tdy);
6132         } else {
6133                 cp->tdx = image->x1 - cp->tx0;
6134                 cp->tdy = image->y1 - cp->ty0;
6135         }
6136
6137         if (parameters->tp_on) {
6138                 cp->m_specific_param.m_enc.m_tp_flag = parameters->tp_flag;
6139                 cp->m_specific_param.m_enc.m_tp_on = 1;
6140         }
6141
6142 #ifdef USE_JPWL
6143         /*
6144         calculate JPWL encoding parameters
6145         */
6146
6147         if (parameters->jpwl_epc_on) {
6148                 OPJ_INT32 i;
6149
6150                 /* set JPWL on */
6151                 cp->epc_on = OPJ_TRUE;
6152                 cp->info_on = OPJ_FALSE; /* no informative technique */
6153
6154                 /* set EPB on */
6155                 if ((parameters->jpwl_hprot_MH > 0) || (parameters->jpwl_hprot_TPH[0] > 0)) {
6156                         cp->epb_on = OPJ_TRUE;
6157
6158                         cp->hprot_MH = parameters->jpwl_hprot_MH;
6159                         for (i = 0; i < JPWL_MAX_NO_TILESPECS; i++) {
6160                                 cp->hprot_TPH_tileno[i] = parameters->jpwl_hprot_TPH_tileno[i];
6161                                 cp->hprot_TPH[i] = parameters->jpwl_hprot_TPH[i];
6162                         }
6163                         /* if tile specs are not specified, copy MH specs */
6164                         if (cp->hprot_TPH[0] == -1) {
6165                                 cp->hprot_TPH_tileno[0] = 0;
6166                                 cp->hprot_TPH[0] = parameters->jpwl_hprot_MH;
6167                         }
6168                         for (i = 0; i < JPWL_MAX_NO_PACKSPECS; i++) {
6169                                 cp->pprot_tileno[i] = parameters->jpwl_pprot_tileno[i];
6170                                 cp->pprot_packno[i] = parameters->jpwl_pprot_packno[i];
6171                                 cp->pprot[i] = parameters->jpwl_pprot[i];
6172                         }
6173                 }
6174
6175                 /* set ESD writing */
6176                 if ((parameters->jpwl_sens_size == 1) || (parameters->jpwl_sens_size == 2)) {
6177                         cp->esd_on = OPJ_TRUE;
6178
6179                         cp->sens_size = parameters->jpwl_sens_size;
6180                         cp->sens_addr = parameters->jpwl_sens_addr;
6181                         cp->sens_range = parameters->jpwl_sens_range;
6182
6183                         cp->sens_MH = parameters->jpwl_sens_MH;
6184                         for (i = 0; i < JPWL_MAX_NO_TILESPECS; i++) {
6185                                 cp->sens_TPH_tileno[i] = parameters->jpwl_sens_TPH_tileno[i];
6186                                 cp->sens_TPH[i] = parameters->jpwl_sens_TPH[i];
6187                         }
6188                 }
6189
6190                 /* always set RED writing to false: we are at the encoder */
6191                 cp->red_on = OPJ_FALSE;
6192
6193         } else {
6194                 cp->epc_on = OPJ_FALSE;
6195         }
6196 #endif /* USE_JPWL */
6197
6198         /* initialize the mutiple tiles */
6199         /* ---------------------------- */
6200         cp->tcps = (opj_tcp_t*) opj_calloc(cp->tw * cp->th, sizeof(opj_tcp_t));
6201         if (parameters->numpocs) {
6202                 /* initialisation of POC */
6203                 opj_j2k_check_poc_val(parameters->POC,parameters->numpocs, parameters->numresolution, image->numcomps, parameters->tcp_numlayers, p_manager);
6204                 /* TODO MSD use the return value*/
6205         }
6206
6207         for (tileno = 0; tileno < cp->tw * cp->th; tileno++) {
6208                 opj_tcp_t *tcp = &cp->tcps[tileno];
6209                 tcp->numlayers = parameters->tcp_numlayers;
6210
6211                 for (j = 0; j < tcp->numlayers; j++) {
6212                         if(cp->m_specific_param.m_enc.m_cinema){
6213                                 if (cp->m_specific_param.m_enc.m_fixed_quality) {
6214                                         tcp->distoratio[j] = parameters->tcp_distoratio[j];
6215                                 }
6216                                 tcp->rates[j] = parameters->tcp_rates[j];
6217                         }else{
6218                                 if (cp->m_specific_param.m_enc.m_fixed_quality) {       /* add fixed_quality */
6219                                         tcp->distoratio[j] = parameters->tcp_distoratio[j];
6220                                 } else {
6221                                         tcp->rates[j] = parameters->tcp_rates[j];
6222                                 }
6223                         }
6224                 }
6225
6226                 tcp->csty = parameters->csty;
6227                 tcp->prg = parameters->prog_order;
6228                 tcp->mct = parameters->tcp_mct;
6229
6230                 numpocs_tile = 0;
6231                 tcp->POC = 0;
6232
6233                 if (parameters->numpocs) {
6234                         /* initialisation of POC */
6235                         tcp->POC = 1;
6236                         for (i = 0; i < parameters->numpocs; i++) {
6237                                 if (tileno + 1 == parameters->POC[i].tile )  {
6238                                         opj_poc_t *tcp_poc = &tcp->pocs[numpocs_tile];
6239
6240                                         tcp_poc->resno0         = parameters->POC[numpocs_tile].resno0;
6241                                         tcp_poc->compno0        = parameters->POC[numpocs_tile].compno0;
6242                                         tcp_poc->layno1         = parameters->POC[numpocs_tile].layno1;
6243                                         tcp_poc->resno1         = parameters->POC[numpocs_tile].resno1;
6244                                         tcp_poc->compno1        = parameters->POC[numpocs_tile].compno1;
6245                                         tcp_poc->prg1           = parameters->POC[numpocs_tile].prg1;
6246                                         tcp_poc->tile           = parameters->POC[numpocs_tile].tile;
6247
6248                                         numpocs_tile++;
6249                                 }
6250                         }
6251
6252                         tcp->numpocs = numpocs_tile -1 ;
6253                 }else{
6254                         tcp->numpocs = 0;
6255                 }
6256
6257                 tcp->tccps = (opj_tccp_t*) opj_calloc(image->numcomps, sizeof(opj_tccp_t));
6258
6259                 if (parameters->mct_data) {
6260                       
6261                     OPJ_UINT32 lMctSize = image->numcomps * image->numcomps * sizeof(OPJ_FLOAT32);
6262                     OPJ_FLOAT32 * lTmpBuf = (OPJ_FLOAT32*)opj_malloc(lMctSize);
6263                     OPJ_INT32 * l_dc_shift = (OPJ_INT32 *) ((OPJ_BYTE *) parameters->mct_data + lMctSize);
6264
6265                     tcp->mct = 2;
6266                     tcp->m_mct_coding_matrix = (OPJ_FLOAT32*)opj_malloc(lMctSize);
6267                     memcpy(tcp->m_mct_coding_matrix,parameters->mct_data,lMctSize);
6268                     memcpy(lTmpBuf,parameters->mct_data,lMctSize);
6269
6270                     tcp->m_mct_decoding_matrix = (OPJ_FLOAT32*)opj_malloc(lMctSize);
6271                     assert(opj_matrix_inversion_f(lTmpBuf,(tcp->m_mct_decoding_matrix),image->numcomps));
6272
6273                     tcp->mct_norms = (OPJ_FLOAT64*)
6274                                     opj_malloc(image->numcomps * sizeof(OPJ_FLOAT64));
6275
6276                     opj_calculate_norms(tcp->mct_norms,image->numcomps,tcp->m_mct_decoding_matrix);
6277                     opj_free(lTmpBuf);
6278
6279                     for (i = 0; i < image->numcomps; i++) {
6280                             opj_tccp_t *tccp = &tcp->tccps[i];
6281                             tccp->m_dc_level_shift = l_dc_shift[i];
6282                     }
6283
6284                     opj_j2k_setup_mct_encoding(tcp,image);                        
6285                 }
6286                 else {
6287                         for (i = 0; i < image->numcomps; i++) {
6288                                 opj_tccp_t *tccp = &tcp->tccps[i];
6289                                 opj_image_comp_t * l_comp = &(image->comps[i]);
6290
6291                                 if (! l_comp->sgnd) {
6292                                         tccp->m_dc_level_shift = 1 << (l_comp->prec - 1);
6293                                 }
6294                         }
6295                 }
6296
6297                 for (i = 0; i < image->numcomps; i++) {
6298                         opj_tccp_t *tccp = &tcp->tccps[i];
6299
6300                         tccp->csty = parameters->csty & 0x01;   /* 0 => one precinct || 1 => custom precinct  */
6301                         tccp->numresolutions = parameters->numresolution;
6302                         tccp->cblkw = opj_int_floorlog2(parameters->cblockw_init);
6303                         tccp->cblkh = opj_int_floorlog2(parameters->cblockh_init);
6304                         tccp->cblksty = parameters->mode;
6305                         tccp->qmfbid = parameters->irreversible ? 0 : 1;
6306                         tccp->qntsty = parameters->irreversible ? J2K_CCP_QNTSTY_SEQNT : J2K_CCP_QNTSTY_NOQNT;
6307                         tccp->numgbits = 2;
6308
6309                         if ((OPJ_INT32)i == parameters->roi_compno) {
6310                                 tccp->roishift = parameters->roi_shift;
6311                         } else {
6312                                 tccp->roishift = 0;
6313                         }
6314
6315                                 if (parameters->csty & J2K_CCP_CSTY_PRT) {
6316                                         OPJ_INT32 p = 0, it_res;
6317                                         assert( tccp->numresolutions > 0 );
6318                                         for (it_res = tccp->numresolutions - 1; it_res >= 0; it_res--) {
6319                                                 if (p < parameters->res_spec) {
6320
6321                                                         if (parameters->prcw_init[p] < 1) {
6322                                                                 tccp->prcw[it_res] = 1;
6323                                                         } else {
6324                                                                 tccp->prcw[it_res] = opj_int_floorlog2(parameters->prcw_init[p]);
6325                                                         }
6326
6327                                                         if (parameters->prch_init[p] < 1) {
6328                                                                 tccp->prch[it_res] = 1;
6329                                                         }else {
6330                                                                 tccp->prch[it_res] = opj_int_floorlog2(parameters->prch_init[p]);
6331                                                         }
6332
6333                                                 } else {
6334                                                         OPJ_INT32 res_spec = parameters->res_spec;
6335                                                         OPJ_INT32 size_prcw = 0;
6336                                                         OPJ_INT32 size_prch = 0;
6337
6338                                                         assert(res_spec>0); /* issue 189 */
6339                                                         size_prcw = parameters->prcw_init[res_spec - 1] >> (p - (res_spec - 1));
6340                                                         size_prch = parameters->prch_init[res_spec - 1] >> (p - (res_spec - 1));
6341
6342
6343                                                         if (size_prcw < 1) {
6344                                                                 tccp->prcw[it_res] = 1;
6345                                                         } else {
6346                                                                 tccp->prcw[it_res] = opj_int_floorlog2(size_prcw);
6347                                                         }
6348
6349                                                         if (size_prch < 1) {
6350                                                                 tccp->prch[it_res] = 1;
6351                                                         } else {
6352                                                                 tccp->prch[it_res] = opj_int_floorlog2(size_prch);
6353                                                         }
6354                                                 }
6355                                                 p++;
6356                                                 /*printf("\nsize precinct for level %d : %d,%d\n", it_res,tccp->prcw[it_res], tccp->prch[it_res]); */
6357                                         }       /*end for*/
6358                                 } else {
6359                                         for (j = 0; j < tccp->numresolutions; j++) {
6360                                                 tccp->prcw[j] = 15;
6361                                                 tccp->prch[j] = 15;
6362                                         }
6363                                 }
6364
6365                         opj_dwt_calc_explicit_stepsizes(tccp, image->comps[i].prec);
6366                 }
6367         }
6368
6369         if (parameters->mct_data) {
6370                 opj_free(parameters->mct_data);
6371                 parameters->mct_data = 00;
6372         }
6373 }
6374
6375 static OPJ_BOOL opj_j2k_add_mhmarker(opj_codestream_index_t *cstr_index, OPJ_UINT32 type, OPJ_OFF_T pos, OPJ_UINT32 len)
6376 {
6377         assert(cstr_index != 00);
6378
6379         /* expand the list? */
6380         if ((cstr_index->marknum + 1) > cstr_index->maxmarknum) {
6381                 opj_marker_info_t *new_marker;
6382                 cstr_index->maxmarknum = 100 + (OPJ_INT32) ((OPJ_FLOAT32) cstr_index->maxmarknum * 1.0F);
6383                 new_marker = (opj_marker_info_t *) opj_realloc(cstr_index->marker, cstr_index->maxmarknum *sizeof(opj_marker_info_t));
6384                 if (! new_marker) {
6385                         opj_free(cstr_index->marker);
6386                         cstr_index->marker = NULL;
6387                         cstr_index->maxmarknum = 0;
6388                         cstr_index->marknum = 0;
6389                         /* opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to add mh marker\n"); */
6390                         return OPJ_FALSE;
6391                 }
6392                 cstr_index->marker = new_marker;
6393         }
6394
6395         /* add the marker */
6396         cstr_index->marker[cstr_index->marknum].type = (OPJ_UINT16)type;
6397         cstr_index->marker[cstr_index->marknum].pos = (OPJ_INT32)pos;
6398         cstr_index->marker[cstr_index->marknum].len = (OPJ_INT32)len;
6399         cstr_index->marknum++;
6400         return OPJ_TRUE;
6401 }
6402
6403 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)
6404 {
6405         assert(cstr_index != 00);
6406         assert(cstr_index->tile_index != 00);
6407
6408         /* expand the list? */
6409         if ((cstr_index->tile_index[tileno].marknum + 1) > cstr_index->tile_index[tileno].maxmarknum) {
6410                 opj_marker_info_t *new_marker;
6411                 cstr_index->tile_index[tileno].maxmarknum = 100 + (OPJ_INT32) ((OPJ_FLOAT32) cstr_index->tile_index[tileno].maxmarknum * 1.0F);
6412                 new_marker = (opj_marker_info_t *) opj_realloc(
6413                                 cstr_index->tile_index[tileno].marker,
6414                                 cstr_index->tile_index[tileno].maxmarknum *sizeof(opj_marker_info_t));
6415                 if (! new_marker) {
6416                         opj_free(cstr_index->tile_index[tileno].marker);
6417                         cstr_index->tile_index[tileno].marker = NULL;
6418                         cstr_index->tile_index[tileno].maxmarknum = 0;
6419                         cstr_index->tile_index[tileno].marknum = 0;
6420                         /* opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to add tl marker\n"); */
6421                         return OPJ_FALSE;
6422                 }
6423                 cstr_index->tile_index[tileno].marker = new_marker;
6424         }
6425
6426         /* add the marker */
6427         cstr_index->tile_index[tileno].marker[cstr_index->tile_index[tileno].marknum].type = (OPJ_UINT16)type;
6428         cstr_index->tile_index[tileno].marker[cstr_index->tile_index[tileno].marknum].pos = (OPJ_INT32)pos;
6429         cstr_index->tile_index[tileno].marker[cstr_index->tile_index[tileno].marknum].len = (OPJ_INT32)len;
6430         cstr_index->tile_index[tileno].marknum++;
6431
6432         if (type == J2K_MS_SOT) {
6433                 OPJ_UINT32 l_current_tile_part = cstr_index->tile_index[tileno].current_tpsno;
6434
6435                 if (cstr_index->tile_index[tileno].tp_index)
6436                         cstr_index->tile_index[tileno].tp_index[l_current_tile_part].start_pos = pos;
6437
6438         }
6439         return OPJ_TRUE;
6440 }
6441
6442 /*
6443  * -----------------------------------------------------------------------
6444  * -----------------------------------------------------------------------
6445  * -----------------------------------------------------------------------
6446  */
6447
6448 OPJ_BOOL opj_j2k_end_decompress(opj_j2k_t *p_j2k,
6449                                 opj_stream_private_t *p_stream,
6450                                 opj_event_mgr_t * p_manager
6451                                 )
6452 {
6453     (void)p_j2k;
6454     (void)p_stream;
6455     (void)p_manager;
6456     return OPJ_TRUE;
6457 }
6458
6459 OPJ_BOOL opj_j2k_read_header(   opj_stream_private_t *p_stream,
6460                                                             opj_j2k_t* p_j2k,
6461                                                             opj_image_t** p_image,
6462                                                             opj_event_mgr_t* p_manager )
6463 {
6464         /* preconditions */
6465         assert(p_j2k != 00);
6466         assert(p_stream != 00);
6467         assert(p_manager != 00);
6468
6469         /* create an empty image header */
6470         p_j2k->m_private_image = opj_image_create0();
6471         if (! p_j2k->m_private_image) {
6472                 return OPJ_FALSE;
6473         }
6474
6475         /* customization of the validation */
6476         opj_j2k_setup_decoding_validation(p_j2k);
6477
6478         /* validation of the parameters codec */
6479         if (! opj_j2k_exec(p_j2k, p_j2k->m_validation_list, p_stream,p_manager)) {
6480                 opj_image_destroy(p_j2k->m_private_image);
6481                 p_j2k->m_private_image = NULL;
6482                 return OPJ_FALSE;
6483         }
6484
6485         /* customization of the encoding */
6486         opj_j2k_setup_header_reading(p_j2k);
6487
6488         /* read header */
6489         if (! opj_j2k_exec (p_j2k,p_j2k->m_procedure_list,p_stream,p_manager)) {
6490                 opj_image_destroy(p_j2k->m_private_image);
6491                 p_j2k->m_private_image = NULL;
6492                 return OPJ_FALSE;
6493         }
6494
6495         *p_image = opj_image_create0();
6496         if (! (*p_image)) {
6497                 return OPJ_FALSE;
6498         }
6499
6500         /* Copy codestream image information to the output image */
6501         opj_copy_image_header(p_j2k->m_private_image, *p_image);
6502
6503     /*Allocate and initialize some elements of codestrem index*/
6504         if (!opj_j2k_allocate_tile_element_cstr_index(p_j2k)){
6505                 return OPJ_FALSE;
6506         }
6507
6508         return OPJ_TRUE;
6509 }
6510
6511 void opj_j2k_setup_header_reading (opj_j2k_t *p_j2k)
6512 {
6513         /* preconditions*/
6514         assert(p_j2k != 00);
6515
6516         opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)opj_j2k_read_header_procedure);
6517
6518         /* DEVELOPER CORNER, add your custom procedures */
6519         opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)opj_j2k_copy_default_tcp_and_create_tcd);
6520
6521 }
6522
6523 void opj_j2k_setup_decoding_validation (opj_j2k_t *p_j2k)
6524 {
6525         /* preconditions*/
6526         assert(p_j2k != 00);
6527
6528         opj_procedure_list_add_procedure(p_j2k->m_validation_list, (opj_procedure)opj_j2k_build_decoder);
6529         opj_procedure_list_add_procedure(p_j2k->m_validation_list, (opj_procedure)opj_j2k_decoding_validation);
6530         /* DEVELOPER CORNER, add your custom validation procedure */
6531
6532 }
6533
6534 OPJ_BOOL opj_j2k_mct_validation (       opj_j2k_t * p_j2k,
6535                                                                 opj_stream_private_t *p_stream,
6536                                                                 opj_event_mgr_t * p_manager )
6537 {
6538         OPJ_BOOL l_is_valid = OPJ_TRUE;
6539         OPJ_UINT32 i,j;
6540
6541         /* preconditions */
6542         assert(p_j2k != 00);
6543         assert(p_stream != 00);
6544         assert(p_manager != 00);
6545
6546         if ((p_j2k->m_cp.rsiz & 0x8200) == 0x8200) {
6547                 OPJ_UINT32 l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw;
6548                 opj_tcp_t * l_tcp = p_j2k->m_cp.tcps;
6549
6550                 for (i=0;i<l_nb_tiles;++i) {
6551                         if (l_tcp->mct == 2) {
6552                                 opj_tccp_t * l_tccp = l_tcp->tccps;
6553                                 l_is_valid &= (l_tcp->m_mct_coding_matrix != 00);
6554
6555                                 for (j=0;j<p_j2k->m_private_image->numcomps;++j) {
6556                                         l_is_valid &= ! (l_tccp->qmfbid & 1);
6557                                         ++l_tccp;
6558                                 }
6559                         }
6560                         ++l_tcp;
6561                 }
6562         }
6563
6564         return l_is_valid;
6565 }
6566
6567 OPJ_BOOL opj_j2k_setup_mct_encoding(opj_tcp_t * p_tcp, opj_image_t * p_image)
6568 {
6569         OPJ_UINT32 i;
6570         OPJ_UINT32 l_indix = 1;
6571         opj_mct_data_t * l_mct_deco_data = 00,* l_mct_offset_data = 00;
6572         opj_simple_mcc_decorrelation_data_t * l_mcc_data;
6573         OPJ_UINT32 l_mct_size,l_nb_elem;
6574         OPJ_FLOAT32 * l_data, * l_current_data;
6575         opj_tccp_t * l_tccp;
6576
6577         /* preconditions */
6578         assert(p_tcp != 00);
6579
6580         if (p_tcp->mct != 2) {
6581                 return OPJ_TRUE;
6582         }
6583
6584         if (p_tcp->m_mct_decoding_matrix) {
6585                 if (p_tcp->m_nb_mct_records == p_tcp->m_nb_max_mct_records) {
6586                         opj_mct_data_t *new_mct_records;
6587                         p_tcp->m_nb_max_mct_records += OPJ_J2K_MCT_DEFAULT_NB_RECORDS;
6588
6589                         new_mct_records = (opj_mct_data_t *) opj_realloc(p_tcp->m_mct_records, p_tcp->m_nb_max_mct_records * sizeof(opj_mct_data_t));
6590                         if (! new_mct_records) {
6591                                 opj_free(p_tcp->m_mct_records);
6592                                 p_tcp->m_mct_records = NULL;
6593                                 p_tcp->m_nb_max_mct_records = 0;
6594                                 p_tcp->m_nb_mct_records = 0;
6595                                 /* opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to setup mct encoding\n"); */
6596                                 return OPJ_FALSE;
6597                         }
6598                         p_tcp->m_mct_records = new_mct_records;
6599                         l_mct_deco_data = p_tcp->m_mct_records + p_tcp->m_nb_mct_records;
6600
6601                         memset(l_mct_deco_data ,0,(p_tcp->m_nb_max_mct_records - p_tcp->m_nb_mct_records) * sizeof(opj_mct_data_t));
6602                 }
6603                 l_mct_deco_data = p_tcp->m_mct_records + p_tcp->m_nb_mct_records;
6604
6605                 if (l_mct_deco_data->m_data) {
6606                         opj_free(l_mct_deco_data->m_data);
6607                         l_mct_deco_data->m_data = 00;
6608                 }
6609
6610                 l_mct_deco_data->m_index = l_indix++;
6611                 l_mct_deco_data->m_array_type = MCT_TYPE_DECORRELATION;
6612                 l_mct_deco_data->m_element_type = MCT_TYPE_FLOAT;
6613                 l_nb_elem = p_image->numcomps * p_image->numcomps;
6614                 l_mct_size = l_nb_elem * MCT_ELEMENT_SIZE[l_mct_deco_data->m_element_type];
6615                 l_mct_deco_data->m_data = (OPJ_BYTE*)opj_malloc(l_mct_size );
6616
6617                 if (! l_mct_deco_data->m_data) {
6618                         return OPJ_FALSE;
6619                 }
6620
6621                 j2k_mct_write_functions_from_float[l_mct_deco_data->m_element_type](p_tcp->m_mct_decoding_matrix,l_mct_deco_data->m_data,l_nb_elem);
6622
6623                 l_mct_deco_data->m_data_size = l_mct_size;
6624                 ++p_tcp->m_nb_mct_records;
6625         }
6626
6627         if (p_tcp->m_nb_mct_records == p_tcp->m_nb_max_mct_records) {
6628                 opj_mct_data_t *new_mct_records;
6629                 p_tcp->m_nb_max_mct_records += OPJ_J2K_MCT_DEFAULT_NB_RECORDS;
6630                 new_mct_records = (opj_mct_data_t *) opj_realloc(p_tcp->m_mct_records, p_tcp->m_nb_max_mct_records * sizeof(opj_mct_data_t));
6631                 if (! new_mct_records) {
6632                         opj_free(p_tcp->m_mct_records);
6633                         p_tcp->m_mct_records = NULL;
6634                         p_tcp->m_nb_max_mct_records = 0;
6635                         p_tcp->m_nb_mct_records = 0;
6636                         /* opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to setup mct encoding\n"); */
6637                         return OPJ_FALSE;
6638                 }
6639                 p_tcp->m_mct_records = new_mct_records;
6640                 l_mct_offset_data = p_tcp->m_mct_records + p_tcp->m_nb_mct_records;
6641
6642                 memset(l_mct_offset_data ,0,(p_tcp->m_nb_max_mct_records - p_tcp->m_nb_mct_records) * sizeof(opj_mct_data_t));
6643
6644                 if (l_mct_deco_data) {
6645                         l_mct_deco_data = l_mct_offset_data - 1;
6646                 }
6647         }
6648
6649         l_mct_offset_data = p_tcp->m_mct_records + p_tcp->m_nb_mct_records;
6650
6651         if (l_mct_offset_data->m_data) {
6652                 opj_free(l_mct_offset_data->m_data);
6653                 l_mct_offset_data->m_data = 00;
6654         }
6655
6656         l_mct_offset_data->m_index = l_indix++;
6657         l_mct_offset_data->m_array_type = MCT_TYPE_OFFSET;
6658         l_mct_offset_data->m_element_type = MCT_TYPE_FLOAT;
6659         l_nb_elem = p_image->numcomps;
6660         l_mct_size = l_nb_elem * MCT_ELEMENT_SIZE[l_mct_offset_data->m_element_type];
6661         l_mct_offset_data->m_data = (OPJ_BYTE*)opj_malloc(l_mct_size );
6662
6663         if (! l_mct_offset_data->m_data) {
6664                 return OPJ_FALSE;
6665         }
6666
6667         l_data = (OPJ_FLOAT32*)opj_malloc(l_nb_elem * sizeof(OPJ_FLOAT32));
6668         if (! l_data) {
6669                 opj_free(l_mct_offset_data->m_data);
6670                 l_mct_offset_data->m_data = 00;
6671                 return OPJ_FALSE;
6672         }
6673
6674         l_tccp = p_tcp->tccps;
6675         l_current_data = l_data;
6676
6677         for (i=0;i<l_nb_elem;++i) {
6678                 *(l_current_data++) = (OPJ_FLOAT32) (l_tccp->m_dc_level_shift);
6679                 ++l_tccp;
6680         }
6681
6682         j2k_mct_write_functions_from_float[l_mct_offset_data->m_element_type](l_data,l_mct_offset_data->m_data,l_nb_elem);
6683
6684         opj_free(l_data);
6685
6686         l_mct_offset_data->m_data_size = l_mct_size;
6687
6688         ++p_tcp->m_nb_mct_records;
6689
6690         if (p_tcp->m_nb_mcc_records == p_tcp->m_nb_max_mcc_records) {
6691                 opj_simple_mcc_decorrelation_data_t *new_mcc_records;
6692                 p_tcp->m_nb_max_mcc_records += OPJ_J2K_MCT_DEFAULT_NB_RECORDS;
6693                 new_mcc_records = (opj_simple_mcc_decorrelation_data_t *) opj_realloc(
6694                                 p_tcp->m_mcc_records, p_tcp->m_nb_max_mcc_records * sizeof(opj_simple_mcc_decorrelation_data_t));
6695                 if (! new_mcc_records) {
6696                         opj_free(p_tcp->m_mcc_records);
6697                         p_tcp->m_mcc_records = NULL;
6698                         p_tcp->m_nb_max_mcc_records = 0;
6699                         p_tcp->m_nb_mcc_records = 0;
6700                         /* opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to setup mct encoding\n"); */
6701                         return OPJ_FALSE;
6702                 }
6703                 p_tcp->m_mcc_records = new_mcc_records;
6704                 l_mcc_data = p_tcp->m_mcc_records + p_tcp->m_nb_mcc_records;
6705                 memset(l_mcc_data ,0,(p_tcp->m_nb_max_mcc_records - p_tcp->m_nb_mcc_records) * sizeof(opj_simple_mcc_decorrelation_data_t));
6706
6707         }
6708
6709         l_mcc_data = p_tcp->m_mcc_records + p_tcp->m_nb_mcc_records;
6710         l_mcc_data->m_decorrelation_array = l_mct_deco_data;
6711         l_mcc_data->m_is_irreversible = 1;
6712         l_mcc_data->m_nb_comps = p_image->numcomps;
6713         l_mcc_data->m_index = l_indix++;
6714         l_mcc_data->m_offset_array = l_mct_offset_data;
6715         ++p_tcp->m_nb_mcc_records;
6716
6717         return OPJ_TRUE;
6718 }
6719
6720 OPJ_BOOL opj_j2k_build_decoder (opj_j2k_t * p_j2k,
6721                                                             opj_stream_private_t *p_stream,
6722                                                             opj_event_mgr_t * p_manager )
6723 {
6724         /* add here initialization of cp
6725            copy paste of setup_decoder */
6726   (void)p_j2k;
6727   (void)p_stream;
6728   (void)p_manager;
6729         return OPJ_TRUE;
6730 }
6731
6732 OPJ_BOOL opj_j2k_build_encoder (opj_j2k_t * p_j2k,
6733                                                         opj_stream_private_t *p_stream,
6734                                                         opj_event_mgr_t * p_manager )
6735 {
6736         /* add here initialization of cp
6737            copy paste of setup_encoder */
6738   (void)p_j2k;
6739   (void)p_stream;
6740   (void)p_manager;
6741         return OPJ_TRUE;
6742 }
6743
6744 OPJ_BOOL opj_j2k_encoding_validation (  opj_j2k_t * p_j2k,
6745                                                                             opj_stream_private_t *p_stream,
6746                                                                             opj_event_mgr_t * p_manager )
6747 {
6748         OPJ_BOOL l_is_valid = OPJ_TRUE;
6749
6750         /* preconditions */
6751         assert(p_j2k != 00);
6752         assert(p_stream != 00);
6753         assert(p_manager != 00);
6754
6755         /* STATE checking */
6756         /* make sure the state is at 0 */
6757         l_is_valid &= (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_NONE);
6758
6759         /* POINTER validation */
6760         /* make sure a p_j2k codec is present */
6761         l_is_valid &= (p_j2k->m_procedure_list != 00);
6762         /* make sure a validation list is present */
6763         l_is_valid &= (p_j2k->m_validation_list != 00);
6764
6765         if ((p_j2k->m_cp.tdx) < (OPJ_UINT32) (1 << p_j2k->m_cp.tcps->tccps->numresolutions)) {
6766                 opj_event_msg(p_manager, EVT_ERROR, "Number of resolutions is too high in comparison to the size of tiles\n");
6767                 return OPJ_FALSE;
6768         }
6769
6770         if ((p_j2k->m_cp.tdy) < (OPJ_UINT32) (1 << p_j2k->m_cp.tcps->tccps->numresolutions)) {
6771                 opj_event_msg(p_manager, EVT_ERROR, "Number of resolutions is too high in comparison to the size of tiles\n");
6772                 return OPJ_FALSE;
6773         }
6774
6775         /* PARAMETER VALIDATION */
6776         return l_is_valid;
6777 }
6778
6779 OPJ_BOOL opj_j2k_decoding_validation (  opj_j2k_t *p_j2k,
6780                                         opj_stream_private_t *p_stream,
6781                                         opj_event_mgr_t * p_manager
6782                                         )
6783 {
6784         OPJ_BOOL l_is_valid = OPJ_TRUE;
6785
6786         /* preconditions*/
6787         assert(p_j2k != 00);
6788         assert(p_stream != 00);
6789         assert(p_manager != 00);
6790
6791         /* STATE checking */
6792         /* make sure the state is at 0 */
6793 #ifdef TODO_MSD
6794         l_is_valid &= (p_j2k->m_specific_param.m_decoder.m_state == J2K_DEC_STATE_NONE);
6795 #endif
6796         l_is_valid &= (p_j2k->m_specific_param.m_decoder.m_state == 0x0000);
6797
6798         /* POINTER validation */
6799         /* make sure a p_j2k codec is present */
6800         /* make sure a procedure list is present */
6801         l_is_valid &= (p_j2k->m_procedure_list != 00);
6802         /* make sure a validation list is present */
6803         l_is_valid &= (p_j2k->m_validation_list != 00);
6804
6805         /* PARAMETER VALIDATION */
6806         return l_is_valid;
6807 }
6808
6809 OPJ_BOOL opj_j2k_read_header_procedure( opj_j2k_t *p_j2k,
6810                                                                             opj_stream_private_t *p_stream,
6811                                                                             opj_event_mgr_t * p_manager)
6812 {
6813         OPJ_UINT32 l_current_marker;
6814         OPJ_UINT32 l_marker_size;
6815         const opj_dec_memory_marker_handler_t * l_marker_handler = 00;
6816
6817         /* preconditions */
6818         assert(p_stream != 00);
6819         assert(p_j2k != 00);
6820         assert(p_manager != 00);
6821
6822         /*  We enter in the main header */
6823         p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_MHSOC;
6824
6825         /* Try to read the SOC marker, the codestream must begin with SOC marker */
6826         if (! opj_j2k_read_soc(p_j2k,p_stream,p_manager)) {
6827                 opj_event_msg(p_manager, EVT_ERROR, "Expected a SOC marker \n");
6828                 return OPJ_FALSE;
6829         }
6830
6831         /* Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer */
6832         if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
6833                 opj_event_msg(p_manager, EVT_ERROR, "Stream too short\n");
6834                 return OPJ_FALSE;
6835         }
6836
6837         /* Read 2 bytes as the new marker ID */
6838         opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_current_marker,2);
6839
6840         /* Try to read until the SOT is detected */
6841         while (l_current_marker != J2K_MS_SOT) {
6842
6843                 /* Check if the current marker ID is valid */
6844                 if (l_current_marker < 0xff00) {
6845                         opj_event_msg(p_manager, EVT_ERROR, "We expected read a marker ID (0xff--) instead of %.8x\n", l_current_marker);
6846                         return OPJ_FALSE;
6847                 }
6848
6849                 /* Get the marker handler from the marker ID */
6850                 l_marker_handler = opj_j2k_get_marker_handler(l_current_marker);
6851
6852                 /* Manage case where marker is unknown */
6853                 if (l_marker_handler->id == J2K_MS_UNK) {
6854                         if (! opj_j2k_read_unk(p_j2k, p_stream, &l_current_marker, p_manager)){
6855                                 opj_event_msg(p_manager, EVT_ERROR, "Unknow marker have been detected and generated error.\n");
6856                                 return OPJ_FALSE;
6857                         }
6858
6859                         if (l_current_marker == J2K_MS_SOT)
6860                                 break; /* SOT marker is detected main header is completely read */
6861                         else    /* Get the marker handler from the marker ID */
6862                                 l_marker_handler = opj_j2k_get_marker_handler(l_current_marker);
6863                 }
6864
6865                 /* Check if the marker is known and if it is the right place to find it */
6866                 if (! (p_j2k->m_specific_param.m_decoder.m_state & l_marker_handler->states) ) {
6867                         opj_event_msg(p_manager, EVT_ERROR, "Marker is not compliant with its position\n");
6868                         return OPJ_FALSE;
6869                 }
6870
6871                 /* Try to read 2 bytes (the marker size) from stream and copy them into the buffer */
6872                 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
6873                         opj_event_msg(p_manager, EVT_ERROR, "Stream too short\n");
6874                         return OPJ_FALSE;
6875                 }
6876
6877                 /* read 2 bytes as the marker size */
6878                 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_marker_size,2);
6879                 l_marker_size -= 2; /* Subtract the size of the marker ID already read */
6880
6881                 /* Check if the marker size is compatible with the header data size */
6882                 if (l_marker_size > p_j2k->m_specific_param.m_decoder.m_header_data_size) {
6883                         OPJ_BYTE *new_header_data = (OPJ_BYTE *) opj_realloc(p_j2k->m_specific_param.m_decoder.m_header_data, l_marker_size);
6884                         if (! new_header_data) {
6885                                 opj_free(p_j2k->m_specific_param.m_decoder.m_header_data);
6886                                 p_j2k->m_specific_param.m_decoder.m_header_data = NULL;
6887                                 p_j2k->m_specific_param.m_decoder.m_header_data_size = 0;
6888                                 opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to read header\n");
6889                                 return OPJ_FALSE;
6890                         }
6891                         p_j2k->m_specific_param.m_decoder.m_header_data = new_header_data;
6892                         p_j2k->m_specific_param.m_decoder.m_header_data_size = l_marker_size;
6893                 }
6894
6895                 /* Try to read the rest of the marker segment from stream and copy them into the buffer */
6896                 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,l_marker_size,p_manager) != l_marker_size) {
6897                         opj_event_msg(p_manager, EVT_ERROR, "Stream too short\n");
6898                         return OPJ_FALSE;
6899                 }
6900
6901                 /* Read the marker segment with the correct marker handler */
6902                 if (! (*(l_marker_handler->handler))(p_j2k,p_j2k->m_specific_param.m_decoder.m_header_data,l_marker_size,p_manager)) {
6903                         opj_event_msg(p_manager, EVT_ERROR, "Marker handler function failed to read the marker segment\n");
6904                         return OPJ_FALSE;
6905                 }
6906
6907                 /* Add the marker to the codestream index*/
6908                 if (OPJ_FALSE == opj_j2k_add_mhmarker(
6909                                         p_j2k->cstr_index,
6910                                         l_marker_handler->id,
6911                                         (OPJ_UINT32) opj_stream_tell(p_stream) - l_marker_size - 4,
6912                                         l_marker_size + 4 )) {
6913                         opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to add mh marker\n");
6914                         return OPJ_FALSE;
6915                 }
6916
6917                 /* Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer */
6918                 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
6919                         opj_event_msg(p_manager, EVT_ERROR, "Stream too short\n");
6920                         return OPJ_FALSE;
6921                 }
6922
6923                 /* read 2 bytes as the new marker ID */
6924                 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_current_marker,2);
6925         }
6926
6927         opj_event_msg(p_manager, EVT_INFO, "Main header has been correctly decoded.\n");
6928
6929         /* Position of the last element if the main header */
6930         p_j2k->cstr_index->main_head_end = (OPJ_UINT32) opj_stream_tell(p_stream) - 2;
6931
6932         /* Next step: read a tile-part header */
6933         p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_TPHSOT;
6934
6935         return OPJ_TRUE;
6936 }
6937
6938 OPJ_BOOL opj_j2k_exec ( opj_j2k_t * p_j2k,
6939                                         opj_procedure_list_t * p_procedure_list,
6940                                         opj_stream_private_t *p_stream,
6941                                         opj_event_mgr_t * p_manager )
6942 {
6943         OPJ_BOOL (** l_procedure) (opj_j2k_t * ,opj_stream_private_t *,opj_event_mgr_t *) = 00;
6944         OPJ_BOOL l_result = OPJ_TRUE;
6945         OPJ_UINT32 l_nb_proc, i;
6946
6947         /* preconditions*/
6948         assert(p_procedure_list != 00);
6949         assert(p_j2k != 00);
6950         assert(p_stream != 00);
6951         assert(p_manager != 00);
6952
6953         l_nb_proc = opj_procedure_list_get_nb_procedures(p_procedure_list);
6954         l_procedure = (OPJ_BOOL (**) (opj_j2k_t * ,opj_stream_private_t *,opj_event_mgr_t *)) opj_procedure_list_get_first_procedure(p_procedure_list);
6955
6956         for     (i=0;i<l_nb_proc;++i) {
6957                 l_result = l_result && ((*l_procedure) (p_j2k,p_stream,p_manager));
6958                 ++l_procedure;
6959         }
6960
6961         /* and clear the procedure list at the end.*/
6962         opj_procedure_list_clear(p_procedure_list);
6963         return l_result;
6964 }
6965
6966 /* FIXME DOC*/
6967 static OPJ_BOOL opj_j2k_copy_default_tcp_and_create_tcd (       opj_j2k_t * p_j2k,
6968                                                             opj_stream_private_t *p_stream,
6969                                                             opj_event_mgr_t * p_manager
6970                                                             )
6971 {
6972         opj_tcp_t * l_tcp = 00;
6973         opj_tcp_t * l_default_tcp = 00;
6974         OPJ_UINT32 l_nb_tiles;
6975         OPJ_UINT32 i,j;
6976         opj_tccp_t *l_current_tccp = 00;
6977         OPJ_UINT32 l_tccp_size;
6978         OPJ_UINT32 l_mct_size;
6979         opj_image_t * l_image;
6980         OPJ_UINT32 l_mcc_records_size,l_mct_records_size;
6981         opj_mct_data_t * l_src_mct_rec, *l_dest_mct_rec;
6982         opj_simple_mcc_decorrelation_data_t * l_src_mcc_rec, *l_dest_mcc_rec;
6983         OPJ_UINT32 l_offset;
6984
6985         /* preconditions */
6986         assert(p_j2k != 00);
6987         assert(p_stream != 00);
6988         assert(p_manager != 00);
6989
6990         l_image = p_j2k->m_private_image;
6991         l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw;
6992         l_tcp = p_j2k->m_cp.tcps;
6993         l_tccp_size = l_image->numcomps * sizeof(opj_tccp_t);
6994         l_default_tcp = p_j2k->m_specific_param.m_decoder.m_default_tcp;
6995         l_mct_size = l_image->numcomps * l_image->numcomps * sizeof(OPJ_FLOAT32);
6996
6997         /* For each tile */
6998         for (i=0; i<l_nb_tiles; ++i) {
6999                 /* keep the tile-compo coding parameters pointer of the current tile coding parameters*/
7000                 l_current_tccp = l_tcp->tccps;
7001                 /*Copy default coding parameters into the current tile coding parameters*/
7002                 memcpy(l_tcp, l_default_tcp, sizeof(opj_tcp_t));
7003                 /* Initialize some values of the current tile coding parameters*/
7004                 l_tcp->ppt = 0;
7005                 l_tcp->ppt_data = 00;
7006                 /* Reconnect the tile-compo coding parameters pointer to the current tile coding parameters*/
7007                 l_tcp->tccps = l_current_tccp;
7008
7009                 /* Get the mct_decoding_matrix of the dflt_tile_cp and copy them into the current tile cp*/
7010                 if (l_default_tcp->m_mct_decoding_matrix) {
7011                         l_tcp->m_mct_decoding_matrix = (OPJ_FLOAT32*)opj_malloc(l_mct_size);
7012                         if (! l_tcp->m_mct_decoding_matrix ) {
7013                                 return OPJ_FALSE;
7014                         }
7015                         memcpy(l_tcp->m_mct_decoding_matrix,l_default_tcp->m_mct_decoding_matrix,l_mct_size);
7016                 }
7017
7018                 /* Get the mct_record of the dflt_tile_cp and copy them into the current tile cp*/
7019                 l_mct_records_size = l_default_tcp->m_nb_max_mct_records * sizeof(opj_mct_data_t);
7020                 l_tcp->m_mct_records = (opj_mct_data_t*)opj_malloc(l_mct_records_size);
7021                 if (! l_tcp->m_mct_records) {
7022                         return OPJ_FALSE;
7023                 }
7024                 memcpy(l_tcp->m_mct_records, l_default_tcp->m_mct_records,l_mct_records_size);
7025
7026                 /* Copy the mct record data from dflt_tile_cp to the current tile*/
7027                 l_src_mct_rec = l_default_tcp->m_mct_records;
7028                 l_dest_mct_rec = l_tcp->m_mct_records;
7029
7030                 for (j=0;j<l_default_tcp->m_nb_mct_records;++j) {
7031
7032                         if (l_src_mct_rec->m_data) {
7033
7034                                 l_dest_mct_rec->m_data = (OPJ_BYTE*) opj_malloc(l_src_mct_rec->m_data_size);
7035                                 if(! l_dest_mct_rec->m_data) {
7036                                         return OPJ_FALSE;
7037                                 }
7038                                 memcpy(l_dest_mct_rec->m_data,l_src_mct_rec->m_data,l_src_mct_rec->m_data_size);
7039                         }
7040
7041                         ++l_src_mct_rec;
7042                         ++l_dest_mct_rec;
7043                 }
7044
7045                 /* Get the mcc_record of the dflt_tile_cp and copy them into the current tile cp*/
7046                 l_mcc_records_size = l_default_tcp->m_nb_max_mcc_records * sizeof(opj_simple_mcc_decorrelation_data_t);
7047                 l_tcp->m_mcc_records = (opj_simple_mcc_decorrelation_data_t*) opj_malloc(l_mcc_records_size);
7048                 if (! l_tcp->m_mcc_records) {
7049                         return OPJ_FALSE;
7050                 }
7051                 memcpy(l_tcp->m_mcc_records,l_default_tcp->m_mcc_records,l_mcc_records_size);
7052
7053                 /* Copy the mcc record data from dflt_tile_cp to the current tile*/
7054                 l_src_mcc_rec = l_default_tcp->m_mcc_records;
7055                 l_dest_mcc_rec = l_tcp->m_mcc_records;
7056
7057                 for (j=0;j<l_default_tcp->m_nb_max_mcc_records;++j) {
7058
7059                         if (l_src_mcc_rec->m_decorrelation_array) {
7060                                 l_offset = l_src_mcc_rec->m_decorrelation_array - l_default_tcp->m_mct_records;
7061                                 l_dest_mcc_rec->m_decorrelation_array = l_tcp->m_mct_records + l_offset;
7062                         }
7063
7064                         if (l_src_mcc_rec->m_offset_array) {
7065                                 l_offset = l_src_mcc_rec->m_offset_array - l_default_tcp->m_mct_records;
7066                                 l_dest_mcc_rec->m_offset_array = l_tcp->m_mct_records + l_offset;
7067                         }
7068
7069                         ++l_src_mcc_rec;
7070                         ++l_dest_mcc_rec;
7071                 }
7072
7073                 /* Copy all the dflt_tile_compo_cp to the current tile cp */
7074                 memcpy(l_current_tccp,l_default_tcp->tccps,l_tccp_size);
7075
7076                 /* Move to next tile cp*/
7077                 ++l_tcp;
7078         }
7079
7080         /* Create the current tile decoder*/
7081         p_j2k->m_tcd = (opj_tcd_t*)opj_tcd_create(OPJ_TRUE); /* FIXME why a cast ? */
7082         if (! p_j2k->m_tcd ) {
7083                 return OPJ_FALSE;
7084         }
7085
7086         if ( !opj_tcd_init(p_j2k->m_tcd, l_image, &(p_j2k->m_cp)) ) {
7087                 opj_tcd_destroy(p_j2k->m_tcd);
7088                 p_j2k->m_tcd = 00;
7089                 opj_event_msg(p_manager, EVT_ERROR, "Cannot decode tile, memory error\n");
7090                 return OPJ_FALSE;
7091         }
7092
7093         return OPJ_TRUE;
7094 }
7095
7096 const opj_dec_memory_marker_handler_t * opj_j2k_get_marker_handler (OPJ_UINT32 p_id)
7097 {
7098         const opj_dec_memory_marker_handler_t *e;
7099         for (e = j2k_memory_marker_handler_tab; e->id != 0; ++e) {
7100                 if (e->id == p_id) {
7101                         break; /* we find a handler corresponding to the marker ID*/
7102                 }
7103         }
7104         return e;
7105 }
7106
7107 void opj_j2k_destroy (opj_j2k_t *p_j2k)
7108 {
7109         if (p_j2k == 00) {
7110                 return;
7111         }
7112
7113         if (p_j2k->m_is_decoder) {
7114
7115                 if (p_j2k->m_specific_param.m_decoder.m_default_tcp != 00) {
7116                         opj_j2k_tcp_destroy(p_j2k->m_specific_param.m_decoder.m_default_tcp);
7117                         opj_free(p_j2k->m_specific_param.m_decoder.m_default_tcp);
7118                         p_j2k->m_specific_param.m_decoder.m_default_tcp = 00;
7119                 }
7120
7121                 if (p_j2k->m_specific_param.m_decoder.m_header_data != 00) {
7122                         opj_free(p_j2k->m_specific_param.m_decoder.m_header_data);
7123                         p_j2k->m_specific_param.m_decoder.m_header_data = 00;
7124                         p_j2k->m_specific_param.m_decoder.m_header_data_size = 0;
7125                 }
7126         }
7127         else {
7128
7129                 if (p_j2k->m_specific_param.m_encoder.m_encoded_tile_data) {
7130                         opj_free(p_j2k->m_specific_param.m_encoder.m_encoded_tile_data);
7131                         p_j2k->m_specific_param.m_encoder.m_encoded_tile_data = 00;
7132                 }
7133
7134                 if (p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer) {
7135                         opj_free(p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer);
7136                         p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer = 00;
7137                         p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current = 00;
7138                 }
7139
7140                 if (p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
7141                         opj_free(p_j2k->m_specific_param.m_encoder.m_header_tile_data);
7142                         p_j2k->m_specific_param.m_encoder.m_header_tile_data = 00;
7143                         p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = 0;
7144                 }
7145         }
7146
7147         opj_tcd_destroy(p_j2k->m_tcd);
7148
7149         opj_j2k_cp_destroy(&(p_j2k->m_cp));
7150         memset(&(p_j2k->m_cp),0,sizeof(opj_cp_t));
7151
7152         opj_procedure_list_destroy(p_j2k->m_procedure_list);
7153         p_j2k->m_procedure_list = 00;
7154
7155         opj_procedure_list_destroy(p_j2k->m_validation_list);
7156         p_j2k->m_procedure_list = 00;
7157
7158         j2k_destroy_cstr_index(p_j2k->cstr_index);
7159         p_j2k->cstr_index = NULL;
7160
7161         opj_image_destroy(p_j2k->m_private_image);
7162         p_j2k->m_private_image = NULL;
7163
7164         opj_image_destroy(p_j2k->m_output_image);
7165         p_j2k->m_output_image = NULL;
7166
7167         opj_free(p_j2k);
7168 }
7169
7170 void j2k_destroy_cstr_index (opj_codestream_index_t *p_cstr_ind)
7171 {
7172         if (p_cstr_ind) {
7173
7174                 if (p_cstr_ind->marker) {
7175                         opj_free(p_cstr_ind->marker);
7176                         p_cstr_ind->marker = NULL;
7177                 }
7178
7179                 if (p_cstr_ind->tile_index) {
7180                         OPJ_UINT32 it_tile = 0;
7181
7182                         for (it_tile=0; it_tile < p_cstr_ind->nb_of_tiles; it_tile++) {
7183
7184                                 if(p_cstr_ind->tile_index[it_tile].packet_index) {
7185                                         opj_free(p_cstr_ind->tile_index[it_tile].packet_index);
7186                                         p_cstr_ind->tile_index[it_tile].packet_index = NULL;
7187                                 }
7188
7189                                 if(p_cstr_ind->tile_index[it_tile].tp_index){
7190                                         opj_free(p_cstr_ind->tile_index[it_tile].tp_index);
7191                                         p_cstr_ind->tile_index[it_tile].tp_index = NULL;
7192                                 }
7193
7194                                 if(p_cstr_ind->tile_index[it_tile].marker){
7195                                         opj_free(p_cstr_ind->tile_index[it_tile].marker);
7196                                         p_cstr_ind->tile_index[it_tile].marker = NULL;
7197
7198                                 }
7199                         }
7200
7201                         opj_free( p_cstr_ind->tile_index);
7202                         p_cstr_ind->tile_index = NULL;
7203                 }
7204
7205                 opj_free(p_cstr_ind);
7206         }
7207 }
7208
7209 void opj_j2k_tcp_destroy (opj_tcp_t *p_tcp)
7210 {
7211         if (p_tcp == 00) {
7212                 return;
7213         }
7214
7215         if (p_tcp->ppt_buffer != 00) {
7216                 opj_free(p_tcp->ppt_buffer);
7217                 p_tcp->ppt_buffer = 00;
7218         }
7219
7220         if (p_tcp->tccps != 00) {
7221                 opj_free(p_tcp->tccps);
7222                 p_tcp->tccps = 00;
7223         }
7224
7225         if (p_tcp->m_mct_coding_matrix != 00) {
7226                 opj_free(p_tcp->m_mct_coding_matrix);
7227                 p_tcp->m_mct_coding_matrix = 00;
7228         }
7229
7230         if (p_tcp->m_mct_decoding_matrix != 00) {
7231                 opj_free(p_tcp->m_mct_decoding_matrix);
7232                 p_tcp->m_mct_decoding_matrix = 00;
7233         }
7234
7235         if (p_tcp->m_mcc_records) {
7236                 opj_free(p_tcp->m_mcc_records);
7237                 p_tcp->m_mcc_records = 00;
7238                 p_tcp->m_nb_max_mcc_records = 0;
7239                 p_tcp->m_nb_mcc_records = 0;
7240         }
7241
7242         if (p_tcp->m_mct_records) {
7243                 opj_mct_data_t * l_mct_data = p_tcp->m_mct_records;
7244                 OPJ_UINT32 i;
7245
7246                 for (i=0;i<p_tcp->m_nb_mct_records;++i) {
7247                         if (l_mct_data->m_data) {
7248                                 opj_free(l_mct_data->m_data);
7249                                 l_mct_data->m_data = 00;
7250                         }
7251
7252                         ++l_mct_data;
7253                 }
7254
7255                 opj_free(p_tcp->m_mct_records);
7256                 p_tcp->m_mct_records = 00;
7257         }
7258
7259         if (p_tcp->mct_norms != 00) {
7260                 opj_free(p_tcp->mct_norms);
7261                 p_tcp->mct_norms = 00;
7262         }
7263
7264         opj_j2k_tcp_data_destroy(p_tcp);
7265
7266 }
7267
7268 void opj_j2k_tcp_data_destroy (opj_tcp_t *p_tcp)
7269 {
7270         if (p_tcp->m_data) {
7271                 opj_free(p_tcp->m_data);
7272                 p_tcp->m_data = NULL;
7273                 p_tcp->m_data_size = 0;
7274         }
7275 }
7276
7277 void opj_j2k_cp_destroy (opj_cp_t *p_cp)
7278 {
7279         OPJ_UINT32 l_nb_tiles;
7280         opj_tcp_t * l_current_tile = 00;
7281         OPJ_UINT32 i;
7282
7283         if (p_cp == 00)
7284         {
7285                 return;
7286         }
7287         if (p_cp->tcps != 00)
7288         {
7289                 l_current_tile = p_cp->tcps;
7290                 l_nb_tiles = p_cp->th * p_cp->tw;
7291
7292                 for (i = 0; i < l_nb_tiles; ++i)
7293                 {
7294                         opj_j2k_tcp_destroy(l_current_tile);
7295                         ++l_current_tile;
7296                 }
7297                 opj_free(p_cp->tcps);
7298                 p_cp->tcps = 00;
7299         }
7300         opj_free(p_cp->ppm_buffer);
7301         p_cp->ppm_buffer = 00;
7302         p_cp->ppm_data = NULL; /* ppm_data belongs to the allocated buffer pointed by ppm_buffer */
7303         opj_free(p_cp->comment);
7304         p_cp->comment = 00;
7305         if (! p_cp->m_is_decoder)
7306         {
7307                 opj_free(p_cp->m_specific_param.m_enc.m_matrice);
7308                 p_cp->m_specific_param.m_enc.m_matrice = 00;
7309         }
7310 }
7311
7312 OPJ_BOOL opj_j2k_read_tile_header(      opj_j2k_t * p_j2k,
7313                                                                     OPJ_UINT32 * p_tile_index,
7314                                                                     OPJ_UINT32 * p_data_size,
7315                                                                     OPJ_INT32 * p_tile_x0, OPJ_INT32 * p_tile_y0,
7316                                                                     OPJ_INT32 * p_tile_x1, OPJ_INT32 * p_tile_y1,
7317                                                                     OPJ_UINT32 * p_nb_comps,
7318                                                                     OPJ_BOOL * p_go_on,
7319                                                                     opj_stream_private_t *p_stream,
7320                                                                     opj_event_mgr_t * p_manager )
7321 {
7322         OPJ_UINT32 l_current_marker = J2K_MS_SOT;
7323         OPJ_UINT32 l_marker_size;
7324         const opj_dec_memory_marker_handler_t * l_marker_handler = 00;
7325         opj_tcp_t * l_tcp = NULL;
7326         OPJ_UINT32 l_nb_tiles;
7327
7328         /* preconditions */
7329         assert(p_stream != 00);
7330         assert(p_j2k != 00);
7331         assert(p_manager != 00);
7332
7333         /* Reach the End Of Codestream ?*/
7334         if (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_EOC){
7335                 l_current_marker = J2K_MS_EOC;
7336         }
7337         /* We need to encounter a SOT marker (a new tile-part header) */
7338         else if (p_j2k->m_specific_param.m_decoder.m_state != J2K_STATE_TPHSOT){
7339                 return OPJ_FALSE;
7340         }
7341
7342         /* Read into the codestream until reach the EOC or ! can_decode ??? FIXME */
7343         while ( (!p_j2k->m_specific_param.m_decoder.m_can_decode) && (l_current_marker != J2K_MS_EOC) ) {
7344
7345                 /* Try to read until the Start Of Data is detected */
7346                 while (l_current_marker != J2K_MS_SOD) {
7347                     
7348                     if(opj_stream_get_number_byte_left(p_stream) == 0)
7349                     {
7350                         p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_NEOC;
7351                         break;
7352                     }
7353
7354                         /* Try to read 2 bytes (the marker size) from stream and copy them into the buffer */
7355                         if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
7356                                 opj_event_msg(p_manager, EVT_ERROR, "Stream too short\n");
7357                                 return OPJ_FALSE;
7358                         }
7359
7360                         /* Read 2 bytes from the buffer as the marker size */
7361                         opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_marker_size,2);
7362
7363                         /* cf. https://code.google.com/p/openjpeg/issues/detail?id=226 */
7364                         if (l_current_marker == 0x8080 && opj_stream_get_number_byte_left(p_stream) == 0) {
7365                                 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_NEOC;
7366                                 break;
7367                         }
7368
7369                         /* Why this condition? FIXME */
7370                         if (p_j2k->m_specific_param.m_decoder.m_state & J2K_STATE_TPH){
7371                                 p_j2k->m_specific_param.m_decoder.m_sot_length -= (l_marker_size + 2);
7372                         }
7373                         l_marker_size -= 2; /* Subtract the size of the marker ID already read */
7374
7375                         /* Get the marker handler from the marker ID */
7376                         l_marker_handler = opj_j2k_get_marker_handler(l_current_marker);
7377
7378                         /* Check if the marker is known and if it is the right place to find it */
7379                         if (! (p_j2k->m_specific_param.m_decoder.m_state & l_marker_handler->states) ) {
7380                                 opj_event_msg(p_manager, EVT_ERROR, "Marker is not compliant with its position\n");
7381                                 return OPJ_FALSE;
7382                         }
7383 /* FIXME manage case of unknown marker as in the main header ? */
7384
7385                         /* Check if the marker size is compatible with the header data size */
7386                         if (l_marker_size > p_j2k->m_specific_param.m_decoder.m_header_data_size) {
7387                                 OPJ_BYTE *new_header_data = (OPJ_BYTE *) opj_realloc(p_j2k->m_specific_param.m_decoder.m_header_data, l_marker_size);
7388                                 if (! new_header_data) {
7389                                         opj_free(p_j2k->m_specific_param.m_decoder.m_header_data);
7390                                         p_j2k->m_specific_param.m_decoder.m_header_data = NULL;
7391                                         p_j2k->m_specific_param.m_decoder.m_header_data_size = 0;
7392                                         opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to read header\n");
7393                                         return OPJ_FALSE;
7394                                 }
7395                                 p_j2k->m_specific_param.m_decoder.m_header_data = new_header_data;
7396                                 p_j2k->m_specific_param.m_decoder.m_header_data_size = l_marker_size;
7397                         }
7398
7399                         /* Try to read the rest of the marker segment from stream and copy them into the buffer */
7400                         if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,l_marker_size,p_manager) != l_marker_size) {
7401                                 opj_event_msg(p_manager, EVT_ERROR, "Stream too short\n");
7402                                 return OPJ_FALSE;
7403                         }
7404
7405                         if (!l_marker_handler->handler) {
7406                                 /* See issue #175 */
7407                                 opj_event_msg(p_manager, EVT_ERROR, "Not sure how that happened.\n");
7408                                 return OPJ_FALSE;
7409                         }
7410                         /* Read the marker segment with the correct marker handler */
7411                         if (! (*(l_marker_handler->handler))(p_j2k,p_j2k->m_specific_param.m_decoder.m_header_data,l_marker_size,p_manager)) {
7412                                 opj_event_msg(p_manager, EVT_ERROR, "Fail to read the current marker segment (%#x)\n", l_current_marker);
7413                                 return OPJ_FALSE;
7414                         }
7415
7416                         /* Add the marker to the codestream index*/
7417                         if (OPJ_FALSE == opj_j2k_add_tlmarker(p_j2k->m_current_tile_number,
7418                                                 p_j2k->cstr_index,
7419                                                 l_marker_handler->id,
7420                                                 (OPJ_UINT32) opj_stream_tell(p_stream) - l_marker_size - 4,
7421                                                 l_marker_size + 4 )) {
7422                                 opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to add tl marker\n");
7423                                 return OPJ_FALSE;
7424                         }
7425
7426                         /* Keep the position of the last SOT marker read */
7427                         if ( l_marker_handler->id == J2K_MS_SOT ) {
7428                                 OPJ_UINT32 sot_pos = (OPJ_UINT32) opj_stream_tell(p_stream) - l_marker_size - 4 ;
7429                                 if (sot_pos > p_j2k->m_specific_param.m_decoder.m_last_sot_read_pos)
7430                                 {
7431                                         p_j2k->m_specific_param.m_decoder.m_last_sot_read_pos = sot_pos;
7432                                 }
7433                         }
7434
7435                         if (p_j2k->m_specific_param.m_decoder.m_skip_data) {
7436                                 /* Skip the rest of the tile part header*/
7437                                 if (opj_stream_skip(p_stream,p_j2k->m_specific_param.m_decoder.m_sot_length,p_manager) != p_j2k->m_specific_param.m_decoder.m_sot_length) {
7438                                         opj_event_msg(p_manager, EVT_ERROR, "Stream too short\n");
7439                                         return OPJ_FALSE;
7440                                 }
7441                                 l_current_marker = J2K_MS_SOD; /* Normally we reached a SOD */
7442                         }
7443                         else {
7444                                 /* Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer*/
7445                                 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
7446                                         opj_event_msg(p_manager, EVT_ERROR, "Stream too short\n");
7447                                         return OPJ_FALSE;
7448                                 }
7449                                 /* Read 2 bytes from the buffer as the new marker ID */
7450                                 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_current_marker,2);
7451                         }
7452                 }
7453                 if(opj_stream_get_number_byte_left(p_stream) == 0
7454                     && p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_NEOC)
7455                     break;
7456
7457                 /* If we didn't skip data before, we need to read the SOD marker*/
7458                 if (! p_j2k->m_specific_param.m_decoder.m_skip_data) {
7459                         /* Try to read the SOD marker and skip data ? FIXME */
7460                         if (! opj_j2k_read_sod(p_j2k, p_stream, p_manager)) {
7461                                 return OPJ_FALSE;
7462                         }
7463
7464                         if (! p_j2k->m_specific_param.m_decoder.m_can_decode){
7465                                 /* Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer */
7466                                 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
7467                                         opj_event_msg(p_manager, EVT_ERROR, "Stream too short\n");
7468                                         return OPJ_FALSE;
7469                                 }
7470
7471                                 /* Read 2 bytes from buffer as the new marker ID */
7472                                 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_current_marker,2);
7473                         }
7474                 }
7475                 else {
7476                         /* Indicate we will try to read a new tile-part header*/
7477                         p_j2k->m_specific_param.m_decoder.m_skip_data = 0;
7478                         p_j2k->m_specific_param.m_decoder.m_can_decode = 0;
7479                         p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_TPHSOT;
7480
7481                         /* Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer */
7482                         if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
7483                                 opj_event_msg(p_manager, EVT_ERROR, "Stream too short\n");
7484                                 return OPJ_FALSE;
7485                         }
7486
7487                         /* Read 2 bytes from buffer as the new marker ID */
7488                         opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_current_marker,2);
7489                 }
7490         }
7491
7492         /* Current marker is the EOC marker ?*/
7493         if (l_current_marker == J2K_MS_EOC) {
7494                 if (p_j2k->m_specific_param.m_decoder.m_state != J2K_STATE_EOC ){
7495                         p_j2k->m_current_tile_number = 0;
7496                         p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_EOC;
7497                 }
7498         }
7499
7500         /* FIXME DOC ???*/
7501         if ( ! p_j2k->m_specific_param.m_decoder.m_can_decode) {
7502                 l_tcp = p_j2k->m_cp.tcps + p_j2k->m_current_tile_number;
7503                 l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw;
7504
7505                 while( (p_j2k->m_current_tile_number < l_nb_tiles) && (l_tcp->m_data == 00) ) {
7506                         ++p_j2k->m_current_tile_number;
7507                         ++l_tcp;
7508                 }
7509
7510                 if (p_j2k->m_current_tile_number == l_nb_tiles) {
7511                         *p_go_on = OPJ_FALSE;
7512                         return OPJ_TRUE;
7513                 }
7514         }
7515
7516         /*FIXME ???*/
7517         if (! opj_tcd_init_decode_tile(p_j2k->m_tcd, p_j2k->m_current_tile_number)) {
7518                 opj_event_msg(p_manager, EVT_ERROR, "Cannot decode tile, memory error\n");
7519                 return OPJ_FALSE;
7520         }
7521
7522         opj_event_msg(p_manager, EVT_INFO, "Header of tile %d / %d has been read.\n",
7523                         p_j2k->m_current_tile_number, (p_j2k->m_cp.th * p_j2k->m_cp.tw) - 1);
7524
7525         *p_tile_index = p_j2k->m_current_tile_number;
7526         *p_go_on = OPJ_TRUE;
7527         *p_data_size = opj_tcd_get_decoded_tile_size(p_j2k->m_tcd);
7528         *p_tile_x0 = p_j2k->m_tcd->tcd_image->tiles->x0;
7529         *p_tile_y0 = p_j2k->m_tcd->tcd_image->tiles->y0;
7530         *p_tile_x1 = p_j2k->m_tcd->tcd_image->tiles->x1;
7531         *p_tile_y1 = p_j2k->m_tcd->tcd_image->tiles->y1;
7532         *p_nb_comps = p_j2k->m_tcd->tcd_image->tiles->numcomps;
7533
7534          p_j2k->m_specific_param.m_decoder.m_state |= 0x0080;/* FIXME J2K_DEC_STATE_DATA;*/
7535
7536         return OPJ_TRUE;
7537 }
7538
7539 OPJ_BOOL opj_j2k_decode_tile (  opj_j2k_t * p_j2k,
7540                                                         OPJ_UINT32 p_tile_index,
7541                                                         OPJ_BYTE * p_data,
7542                                                         OPJ_UINT32 p_data_size,
7543                                                         opj_stream_private_t *p_stream,
7544                                                         opj_event_mgr_t * p_manager )
7545 {
7546         OPJ_UINT32 l_current_marker;
7547         OPJ_BYTE l_data [2];
7548         opj_tcp_t * l_tcp;
7549
7550         /* preconditions */
7551         assert(p_stream != 00);
7552         assert(p_j2k != 00);
7553         assert(p_manager != 00);
7554
7555         if ( !(p_j2k->m_specific_param.m_decoder.m_state & 0x0080/*FIXME J2K_DEC_STATE_DATA*/)
7556                 || (p_tile_index != p_j2k->m_current_tile_number) ) {
7557                 return OPJ_FALSE;
7558         }
7559
7560         l_tcp = &(p_j2k->m_cp.tcps[p_tile_index]);
7561         if (! l_tcp->m_data) {
7562                 opj_j2k_tcp_destroy(l_tcp);
7563                 return OPJ_FALSE;
7564         }
7565
7566         if (! opj_tcd_decode_tile(      p_j2k->m_tcd,
7567                                                                 l_tcp->m_data,
7568                                                                 l_tcp->m_data_size,
7569                                                                 p_tile_index,
7570                                                                 p_j2k->cstr_index) ) {
7571                 opj_j2k_tcp_destroy(l_tcp);
7572                 p_j2k->m_specific_param.m_decoder.m_state |= 0x8000;/*FIXME J2K_DEC_STATE_ERR;*/
7573                 return OPJ_FALSE;
7574         }
7575
7576         if (! opj_tcd_update_tile_data(p_j2k->m_tcd,p_data,p_data_size)) {
7577                 return OPJ_FALSE;
7578         }
7579
7580         /* To avoid to destroy the tcp which can be useful when we try to decode a tile decoded before (cf j2k_random_tile_access)
7581          * we destroy just the data which will be re-read in read_tile_header*/
7582         /*opj_j2k_tcp_destroy(l_tcp);
7583         p_j2k->m_tcd->tcp = 0;*/
7584         opj_j2k_tcp_data_destroy(l_tcp);
7585
7586         p_j2k->m_specific_param.m_decoder.m_can_decode = 0;
7587         p_j2k->m_specific_param.m_decoder.m_state &= (~ (0x0080));/* FIXME J2K_DEC_STATE_DATA);*/
7588
7589         if(opj_stream_get_number_byte_left(p_stream) == 0 
7590             && p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_NEOC){
7591             return OPJ_TRUE;
7592         }
7593
7594         if (p_j2k->m_specific_param.m_decoder.m_state != 0x0100){ /*FIXME J2K_DEC_STATE_EOC)*/
7595                 if (opj_stream_read_data(p_stream,l_data,2,p_manager) != 2) {
7596                         opj_event_msg(p_manager, EVT_ERROR, "Stream too short\n");
7597                         return OPJ_FALSE;
7598                 }
7599
7600                 opj_read_bytes(l_data,&l_current_marker,2);
7601
7602                 if (l_current_marker == J2K_MS_EOC) {
7603                         p_j2k->m_current_tile_number = 0;
7604                         p_j2k->m_specific_param.m_decoder.m_state =  0x0100;/*FIXME J2K_DEC_STATE_EOC;*/
7605                 }
7606                 else if (l_current_marker != J2K_MS_SOT)
7607                 {
7608                         opj_event_msg(p_manager, EVT_ERROR, "Stream too short, expected SOT\n");
7609                         
7610                         if(opj_stream_get_number_byte_left(p_stream) == 0) {
7611                             p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_NEOC;
7612                             return OPJ_TRUE;
7613                         }
7614                         return OPJ_FALSE;
7615                 }
7616         }
7617
7618         return OPJ_TRUE;
7619 }
7620
7621 OPJ_BOOL opj_j2k_update_image_data (opj_tcd_t * p_tcd, OPJ_BYTE * p_data, opj_image_t* p_output_image)
7622 {
7623         OPJ_UINT32 i,j,k = 0;
7624         OPJ_UINT32 l_width_src,l_height_src;
7625         OPJ_UINT32 l_width_dest,l_height_dest;
7626         OPJ_INT32 l_offset_x0_src, l_offset_y0_src, l_offset_x1_src, l_offset_y1_src;
7627         OPJ_INT32 l_start_offset_src, l_line_offset_src, l_end_offset_src ;
7628         OPJ_UINT32 l_start_x_dest , l_start_y_dest;
7629         OPJ_UINT32 l_x0_dest, l_y0_dest, l_x1_dest, l_y1_dest;
7630         OPJ_INT32 l_start_offset_dest, l_line_offset_dest;
7631
7632         opj_image_comp_t * l_img_comp_src = 00;
7633         opj_image_comp_t * l_img_comp_dest = 00;
7634
7635         opj_tcd_tilecomp_t * l_tilec = 00;
7636         opj_image_t * l_image_src = 00;
7637         OPJ_UINT32 l_size_comp, l_remaining;
7638         OPJ_INT32 * l_dest_ptr;
7639         opj_tcd_resolution_t* l_res= 00;
7640
7641         l_tilec = p_tcd->tcd_image->tiles->comps;
7642         l_image_src = p_tcd->image;
7643         l_img_comp_src = l_image_src->comps;
7644
7645         l_img_comp_dest = p_output_image->comps;
7646
7647         for (i=0; i<l_image_src->numcomps; i++) {
7648
7649                 /* Allocate output component buffer if necessary */
7650                 if (!l_img_comp_dest->data) {
7651
7652                         l_img_comp_dest->data = (OPJ_INT32*) opj_calloc(l_img_comp_dest->w * l_img_comp_dest->h, sizeof(OPJ_INT32));
7653                         if (! l_img_comp_dest->data) {
7654                                 return OPJ_FALSE;
7655                         }
7656                 }
7657
7658                 /* Copy info from decoded comp image to output image */
7659                 l_img_comp_dest->resno_decoded = l_img_comp_src->resno_decoded;
7660
7661                 /*-----*/
7662                 /* Compute the precision of the output buffer */
7663                 l_size_comp = l_img_comp_src->prec >> 3; /*(/ 8)*/
7664                 l_remaining = l_img_comp_src->prec & 7;  /* (%8) */
7665                 l_res = l_tilec->resolutions + l_img_comp_src->resno_decoded;
7666
7667                 if (l_remaining) {
7668                         ++l_size_comp;
7669                 }
7670
7671                 if (l_size_comp == 3) {
7672                         l_size_comp = 4;
7673                 }
7674                 /*-----*/
7675
7676                 /* Current tile component size*/
7677                 /*if (i == 0) {
7678                 fprintf(stdout, "SRC: l_res_x0=%d, l_res_x1=%d, l_res_y0=%d, l_res_y1=%d\n",
7679                                 l_res->x0, l_res->x1, l_res->y0, l_res->y1);
7680                 }*/
7681
7682                 l_width_src = (l_res->x1 - l_res->x0);
7683                 l_height_src = (l_res->y1 - l_res->y0);
7684
7685                 /* Border of the current output component*/
7686                 l_x0_dest = opj_int_ceildivpow2(l_img_comp_dest->x0, l_img_comp_dest->factor);
7687                 l_y0_dest = opj_int_ceildivpow2(l_img_comp_dest->y0, l_img_comp_dest->factor);
7688                 l_x1_dest = l_x0_dest + l_img_comp_dest->w;
7689                 l_y1_dest = l_y0_dest + l_img_comp_dest->h;
7690
7691                 /*if (i == 0) {
7692                 fprintf(stdout, "DEST: l_x0_dest=%d, l_x1_dest=%d, l_y0_dest=%d, l_y1_dest=%d (%d)\n",
7693                                 l_x0_dest, l_x1_dest, l_y0_dest, l_y1_dest, l_img_comp_dest->factor );
7694                 }*/
7695
7696                 /*-----*/
7697                 /* Compute the area (l_offset_x0_src, l_offset_y0_src, l_offset_x1_src, l_offset_y1_src)
7698                  * of the input buffer (decoded tile component) which will be move
7699                  * in the output buffer. Compute the area of the output buffer (l_start_x_dest,
7700                  * l_start_y_dest, l_width_dest, l_height_dest)  which will be modified
7701                  * by this input area.
7702                  * */
7703                 assert( l_res->x0 >= 0);
7704                 assert( l_res->x1 >= 0);
7705                 if ( l_x0_dest < (OPJ_UINT32)l_res->x0 ) {
7706                         l_start_x_dest = l_res->x0 - l_x0_dest;
7707                         l_offset_x0_src = 0;
7708
7709                         if ( l_x1_dest >= (OPJ_UINT32)l_res->x1 ) {
7710                                 l_width_dest = l_width_src;
7711                                 l_offset_x1_src = 0;
7712                         }
7713                         else {
7714                                 l_width_dest = l_x1_dest - l_res->x0 ;
7715                                 l_offset_x1_src = l_width_src - l_width_dest;
7716                         }
7717                 }
7718                 else {
7719                         l_start_x_dest = 0 ;
7720                         l_offset_x0_src = l_x0_dest - l_res->x0;
7721
7722                         if ( l_x1_dest >= (OPJ_UINT32)l_res->x1 ) {
7723                                 l_width_dest = l_width_src - l_offset_x0_src;
7724                                 l_offset_x1_src = 0;
7725                         }
7726                         else {
7727                                 l_width_dest = l_img_comp_dest->w ;
7728                                 l_offset_x1_src = l_res->x1 - l_x1_dest;
7729                         }
7730                 }
7731
7732                 if ( l_y0_dest < (OPJ_UINT32)l_res->y0 ) {
7733                         l_start_y_dest = l_res->y0 - l_y0_dest;
7734                         l_offset_y0_src = 0;
7735
7736                         if ( l_y1_dest >= (OPJ_UINT32)l_res->y1 ) {
7737                                 l_height_dest = l_height_src;
7738                                 l_offset_y1_src = 0;
7739                         }
7740                         else {
7741                                 l_height_dest = l_y1_dest - l_res->y0 ;
7742                                 l_offset_y1_src =  l_height_src - l_height_dest;
7743                         }
7744                 }
7745                 else {
7746                         l_start_y_dest = 0 ;
7747                         l_offset_y0_src = l_y0_dest - l_res->y0;
7748
7749                         if ( l_y1_dest >= (OPJ_UINT32)l_res->y1 ) {
7750                                 l_height_dest = l_height_src - l_offset_y0_src;
7751                                 l_offset_y1_src = 0;
7752                         }
7753                         else {
7754                                 l_height_dest = l_img_comp_dest->h ;
7755                                 l_offset_y1_src = l_res->y1 - l_y1_dest;
7756                         }
7757                 }
7758
7759                 if( (l_offset_x0_src < 0 ) || (l_offset_y0_src < 0 ) || (l_offset_x1_src < 0 ) || (l_offset_y1_src < 0 ) ){
7760                         return OPJ_FALSE;
7761                 }
7762                 /* testcase 2977.pdf.asan.67.2198 */
7763                 if ((OPJ_INT32)l_width_dest < 0 || (OPJ_INT32)l_height_dest < 0) {
7764                         return OPJ_FALSE;
7765                 }
7766                 /*-----*/
7767
7768                 /* Compute the input buffer offset */
7769                 l_start_offset_src = l_offset_x0_src + l_offset_y0_src * l_width_src;
7770                 l_line_offset_src = l_offset_x1_src + l_offset_x0_src;
7771                 l_end_offset_src = l_offset_y1_src * l_width_src - l_offset_x0_src;
7772
7773                 /* Compute the output buffer offset */
7774                 l_start_offset_dest = l_start_x_dest + l_start_y_dest * l_img_comp_dest->w;
7775                 l_line_offset_dest = l_img_comp_dest->w - l_width_dest;
7776
7777                 /* Move the output buffer to the first place where we will write*/
7778                 l_dest_ptr = l_img_comp_dest->data + l_start_offset_dest;
7779
7780                 /*if (i == 0) {
7781                         fprintf(stdout, "COMPO[%d]:\n",i);
7782                         fprintf(stdout, "SRC: l_start_x_src=%d, l_start_y_src=%d, l_width_src=%d, l_height_src=%d\n"
7783                                         "\t tile offset:%d, %d, %d, %d\n"
7784                                         "\t buffer offset: %d; %d, %d\n",
7785                                         l_res->x0, l_res->y0, l_width_src, l_height_src,
7786                                         l_offset_x0_src, l_offset_y0_src, l_offset_x1_src, l_offset_y1_src,
7787                                         l_start_offset_src, l_line_offset_src, l_end_offset_src);
7788
7789                         fprintf(stdout, "DEST: l_start_x_dest=%d, l_start_y_dest=%d, l_width_dest=%d, l_height_dest=%d\n"
7790                                         "\t start offset: %d, line offset= %d\n",
7791                                         l_start_x_dest, l_start_y_dest, l_width_dest, l_height_dest, l_start_offset_dest, l_line_offset_dest);
7792                 }*/
7793
7794                 switch (l_size_comp) {
7795                         case 1:
7796                                 {
7797                                         OPJ_CHAR * l_src_ptr = (OPJ_CHAR*) p_data;
7798                                         l_src_ptr += l_start_offset_src; /* Move to the first place where we will read*/
7799
7800                                         if (l_img_comp_src->sgnd) {
7801                                                 for (j = 0 ; j < l_height_dest ; ++j) {
7802                                                         for ( k = 0 ; k < l_width_dest ; ++k) {
7803                                                                 *(l_dest_ptr++) = (OPJ_INT32) (*(l_src_ptr++)); /* Copy only the data needed for the output image */
7804                                                         }
7805
7806                                                         l_dest_ptr+= l_line_offset_dest; /* Move to the next place where we will write */
7807                                                         l_src_ptr += l_line_offset_src ; /* Move to the next place where we will read */
7808                                                 }
7809                                         }
7810                                         else {
7811                                                 for ( j = 0 ; j < l_height_dest ; ++j ) {
7812                                                         for ( k = 0 ; k < l_width_dest ; ++k) {
7813                                                                 *(l_dest_ptr++) = (OPJ_INT32) ((*(l_src_ptr++))&0xff);
7814                                                         }
7815
7816                                                         l_dest_ptr+= l_line_offset_dest;
7817                                                         l_src_ptr += l_line_offset_src;
7818                                                 }
7819                                         }
7820
7821                                         l_src_ptr += l_end_offset_src; /* Move to the end of this component-part of the input buffer */
7822                                         p_data = (OPJ_BYTE*) l_src_ptr; /* Keep the current position for the next component-part */
7823                                 }
7824                                 break;
7825                         case 2:
7826                                 {
7827                                         OPJ_INT16 * l_src_ptr = (OPJ_INT16 *) p_data;
7828                                         l_src_ptr += l_start_offset_src;
7829
7830                                         if (l_img_comp_src->sgnd) {
7831                                                 for (j=0;j<l_height_dest;++j) {
7832                                                         for (k=0;k<l_width_dest;++k) {
7833                                                                 *(l_dest_ptr++) = *(l_src_ptr++);
7834                                                         }
7835
7836                                                         l_dest_ptr+= l_line_offset_dest;
7837                                                         l_src_ptr += l_line_offset_src ;
7838                                                 }
7839                                         }
7840                                         else {
7841                                                 for (j=0;j<l_height_dest;++j) {
7842                                                         for (k=0;k<l_width_dest;++k) {
7843                                                                 *(l_dest_ptr++) = (*(l_src_ptr++))&0xffff;
7844                                                         }
7845
7846                                                         l_dest_ptr+= l_line_offset_dest;
7847                                                         l_src_ptr += l_line_offset_src ;
7848                                                 }
7849                                         }
7850
7851                                         l_src_ptr += l_end_offset_src;
7852                                         p_data = (OPJ_BYTE*) l_src_ptr;
7853                                 }
7854                                 break;
7855                         case 4:
7856                                 {
7857                                         OPJ_INT32 * l_src_ptr = (OPJ_INT32 *) p_data;
7858                                         l_src_ptr += l_start_offset_src;
7859
7860                                         for (j=0;j<l_height_dest;++j) {
7861                                                 for (k=0;k<l_width_dest;++k) {
7862                                                         *(l_dest_ptr++) = (*(l_src_ptr++));
7863                                                 }
7864
7865                                                 l_dest_ptr+= l_line_offset_dest;
7866                                                 l_src_ptr += l_line_offset_src ;
7867                                         }
7868
7869                                         l_src_ptr += l_end_offset_src;
7870                                         p_data = (OPJ_BYTE*) l_src_ptr;
7871                                 }
7872                                 break;
7873                 }
7874
7875                 ++l_img_comp_dest;
7876                 ++l_img_comp_src;
7877                 ++l_tilec;
7878         }
7879
7880         return OPJ_TRUE;
7881 }
7882
7883 OPJ_BOOL opj_j2k_set_decode_area(       opj_j2k_t *p_j2k,
7884                                                                     opj_image_t* p_image,
7885                                                                     OPJ_INT32 p_start_x, OPJ_INT32 p_start_y,
7886                                                                     OPJ_INT32 p_end_x, OPJ_INT32 p_end_y,
7887                                                                     opj_event_mgr_t * p_manager )
7888 {
7889         opj_cp_t * l_cp = &(p_j2k->m_cp);
7890         opj_image_t * l_image = p_j2k->m_private_image;
7891
7892         OPJ_UINT32 it_comp;
7893         OPJ_INT32 l_comp_x1, l_comp_y1;
7894         opj_image_comp_t* l_img_comp = NULL;
7895
7896         /* Check if we are read the main header */
7897         if (p_j2k->m_specific_param.m_decoder.m_state != J2K_STATE_TPHSOT) { /* FIXME J2K_DEC_STATE_TPHSOT)*/
7898                 opj_event_msg(p_manager, EVT_ERROR, "Need to decode the main header before begin to decode the remaining codestream");
7899                 return OPJ_FALSE;
7900         }
7901
7902         if ( !p_start_x && !p_start_y && !p_end_x && !p_end_y){
7903                 opj_event_msg(p_manager, EVT_INFO, "No decoded area parameters, set the decoded area to the whole image\n");
7904
7905                 p_j2k->m_specific_param.m_decoder.m_start_tile_x = 0;
7906                 p_j2k->m_specific_param.m_decoder.m_start_tile_y = 0;
7907                 p_j2k->m_specific_param.m_decoder.m_end_tile_x = l_cp->tw;
7908                 p_j2k->m_specific_param.m_decoder.m_end_tile_y = l_cp->th;
7909
7910                 return OPJ_TRUE;
7911         }
7912
7913         /* ----- */
7914         /* Check if the positions provided by the user are correct */
7915
7916         /* Left */
7917         assert(p_start_x >= 0 );
7918         assert(p_start_y >= 0 );
7919
7920         if ((OPJ_UINT32)p_start_x > l_image->x1 ) {
7921                 opj_event_msg(p_manager, EVT_ERROR,
7922                         "Left position of the decoded area (region_x0=%d) is outside the image area (Xsiz=%d).\n",
7923                         p_start_x, l_image->x1);
7924                 return OPJ_FALSE;
7925         }
7926         else if ((OPJ_UINT32)p_start_x < l_image->x0){
7927                 opj_event_msg(p_manager, EVT_WARNING,
7928                                 "Left position of the decoded area (region_x0=%d) is outside the image area (XOsiz=%d).\n",
7929                                 p_start_x, l_image->x0);
7930                 p_j2k->m_specific_param.m_decoder.m_start_tile_x = 0;
7931                 p_image->x0 = l_image->x0;
7932         }
7933         else {
7934                 p_j2k->m_specific_param.m_decoder.m_start_tile_x = (p_start_x - l_cp->tx0) / l_cp->tdx;
7935                 p_image->x0 = p_start_x;
7936         }
7937
7938         /* Up */
7939         if ((OPJ_UINT32)p_start_y > l_image->y1){
7940                 opj_event_msg(p_manager, EVT_ERROR,
7941                                 "Up position of the decoded area (region_y0=%d) is outside the image area (Ysiz=%d).\n",
7942                                 p_start_y, l_image->y1);
7943                 return OPJ_FALSE;
7944         }
7945         else if ((OPJ_UINT32)p_start_y < l_image->y0){
7946                 opj_event_msg(p_manager, EVT_WARNING,
7947                                 "Up position of the decoded area (region_y0=%d) is outside the image area (YOsiz=%d).\n",
7948                                 p_start_y, l_image->y0);
7949                 p_j2k->m_specific_param.m_decoder.m_start_tile_y = 0;
7950                 p_image->y0 = l_image->y0;
7951         }
7952         else {
7953                 p_j2k->m_specific_param.m_decoder.m_start_tile_y = (p_start_y - l_cp->ty0) / l_cp->tdy;
7954                 p_image->y0 = p_start_y;
7955         }
7956
7957         /* Right */
7958         assert((OPJ_UINT32)p_end_x > 0);
7959         assert((OPJ_UINT32)p_end_y > 0);
7960         if ((OPJ_UINT32)p_end_x < l_image->x0) {
7961                 opj_event_msg(p_manager, EVT_ERROR,
7962                         "Right position of the decoded area (region_x1=%d) is outside the image area (XOsiz=%d).\n",
7963                         p_end_x, l_image->x0);
7964                 return OPJ_FALSE;
7965         }
7966         else if ((OPJ_UINT32)p_end_x > l_image->x1) {
7967                 opj_event_msg(p_manager, EVT_WARNING,
7968                         "Right position of the decoded area (region_x1=%d) is outside the image area (Xsiz=%d).\n",
7969                         p_end_x, l_image->x1);
7970                 p_j2k->m_specific_param.m_decoder.m_end_tile_x = l_cp->tw;
7971                 p_image->x1 = l_image->x1;
7972         }
7973         else {
7974                 p_j2k->m_specific_param.m_decoder.m_end_tile_x = opj_int_ceildiv((p_end_x - l_cp->tx0), l_cp->tdx);
7975                 p_image->x1 = p_end_x;
7976         }
7977
7978         /* Bottom */
7979         if ((OPJ_UINT32)p_end_y < l_image->y0) {
7980                 opj_event_msg(p_manager, EVT_ERROR,
7981                         "Bottom position of the decoded area (region_y1=%d) is outside the image area (YOsiz=%d).\n",
7982                         p_end_y, l_image->y0);
7983                 return OPJ_FALSE;
7984         }
7985         if ((OPJ_UINT32)p_end_y > l_image->y1){
7986                 opj_event_msg(p_manager, EVT_WARNING,
7987                         "Bottom position of the decoded area (region_y1=%d) is outside the image area (Ysiz=%d).\n",
7988                         p_end_y, l_image->y1);
7989                 p_j2k->m_specific_param.m_decoder.m_end_tile_y = l_cp->th;
7990                 p_image->y1 = l_image->y1;
7991         }
7992         else{
7993                 p_j2k->m_specific_param.m_decoder.m_end_tile_y = opj_int_ceildiv((p_end_y - l_cp->ty0), l_cp->tdy);
7994                 p_image->y1 = p_end_y;
7995         }
7996         /* ----- */
7997
7998         p_j2k->m_specific_param.m_decoder.m_discard_tiles = 1;
7999
8000         l_img_comp = p_image->comps;
8001         for (it_comp=0; it_comp < p_image->numcomps; ++it_comp)
8002         {
8003                 OPJ_INT32 l_h,l_w;
8004
8005                 l_img_comp->x0 = opj_int_ceildiv(p_image->x0, l_img_comp->dx);
8006                 l_img_comp->y0 = opj_int_ceildiv(p_image->y0, l_img_comp->dy);
8007                 l_comp_x1 = opj_int_ceildiv(p_image->x1, l_img_comp->dx);
8008                 l_comp_y1 = opj_int_ceildiv(p_image->y1, l_img_comp->dy);
8009
8010                 l_w = opj_int_ceildivpow2(l_comp_x1, l_img_comp->factor)
8011                                 - opj_int_ceildivpow2(l_img_comp->x0, l_img_comp->factor);
8012                 if (l_w < 0){
8013                         opj_event_msg(p_manager, EVT_ERROR,
8014                                 "Size x of the decoded component image is incorrect (comp[%d].w=%d).\n",
8015                                 it_comp, l_w);
8016                         return OPJ_FALSE;
8017                 }
8018                 l_img_comp->w = l_w;
8019
8020                 l_h = opj_int_ceildivpow2(l_comp_y1, l_img_comp->factor)
8021                                 - opj_int_ceildivpow2(l_img_comp->y0, l_img_comp->factor);
8022                 if (l_h < 0){
8023                         opj_event_msg(p_manager, EVT_ERROR,
8024                                 "Size y of the decoded component image is incorrect (comp[%d].h=%d).\n",
8025                                 it_comp, l_h);
8026                         return OPJ_FALSE;
8027                 }
8028                 l_img_comp->h = l_h;
8029
8030                 l_img_comp++;
8031         }
8032
8033         opj_event_msg( p_manager, EVT_INFO,"Setting decoding area to %d,%d,%d,%d\n",
8034                         p_image->x0, p_image->y0, p_image->x1, p_image->y1);
8035
8036         return OPJ_TRUE;
8037 }
8038
8039 opj_j2k_t* opj_j2k_create_decompress(void)
8040 {
8041         opj_j2k_t *l_j2k = (opj_j2k_t*) opj_malloc(sizeof(opj_j2k_t));
8042         if (!l_j2k) {
8043                 return 00;
8044         }
8045         memset(l_j2k,0,sizeof(opj_j2k_t));
8046
8047         l_j2k->m_is_decoder = 1;
8048         l_j2k->m_cp.m_is_decoder = 1;
8049
8050         l_j2k->m_specific_param.m_decoder.m_default_tcp = (opj_tcp_t*) opj_malloc(sizeof(opj_tcp_t));
8051         if (!l_j2k->m_specific_param.m_decoder.m_default_tcp) {
8052                 opj_j2k_destroy(l_j2k);
8053                 return 00;
8054         }
8055         memset(l_j2k->m_specific_param.m_decoder.m_default_tcp,0,sizeof(opj_tcp_t));
8056
8057         l_j2k->m_specific_param.m_decoder.m_header_data = (OPJ_BYTE *) opj_malloc(OPJ_J2K_DEFAULT_HEADER_SIZE);
8058         if (! l_j2k->m_specific_param.m_decoder.m_header_data) {
8059                 opj_j2k_destroy(l_j2k);
8060                 return 00;
8061         }
8062
8063         l_j2k->m_specific_param.m_decoder.m_header_data_size = OPJ_J2K_DEFAULT_HEADER_SIZE;
8064
8065         l_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec = -1 ;
8066
8067         l_j2k->m_specific_param.m_decoder.m_last_sot_read_pos = 0 ;
8068
8069         /* codestream index creation */
8070         l_j2k->cstr_index = opj_j2k_create_cstr_index();
8071
8072                         /*(opj_codestream_index_t*) opj_malloc(sizeof(opj_codestream_index_t));
8073         if (!l_j2k->cstr_index){
8074                 opj_j2k_destroy(l_j2k);
8075                 return NULL;
8076         }
8077
8078         l_j2k->cstr_index->marker = (opj_marker_info_t*) opj_malloc(100 * sizeof(opj_marker_info_t));
8079 */
8080
8081         /* validation list creation */
8082         l_j2k->m_validation_list = opj_procedure_list_create();
8083         if (! l_j2k->m_validation_list) {
8084                 opj_j2k_destroy(l_j2k);
8085                 return 00;
8086         }
8087
8088         /* execution list creation */
8089         l_j2k->m_procedure_list = opj_procedure_list_create();
8090         if (! l_j2k->m_procedure_list) {
8091                 opj_j2k_destroy(l_j2k);
8092                 return 00;
8093         }
8094
8095         return l_j2k;
8096 }
8097
8098 opj_codestream_index_t* opj_j2k_create_cstr_index(void)
8099 {
8100         opj_codestream_index_t* cstr_index = (opj_codestream_index_t*)
8101                         opj_calloc(1,sizeof(opj_codestream_index_t));
8102         if (!cstr_index)
8103                 return NULL;
8104
8105         cstr_index->maxmarknum = 100;
8106         cstr_index->marknum = 0;
8107         cstr_index->marker = (opj_marker_info_t*)
8108                         opj_calloc(cstr_index->maxmarknum, sizeof(opj_marker_info_t));
8109         if (!cstr_index-> marker)
8110                 return NULL;
8111
8112         cstr_index->tile_index = NULL;
8113
8114         return cstr_index;
8115 }
8116
8117 OPJ_UINT32 opj_j2k_get_SPCod_SPCoc_size (       opj_j2k_t *p_j2k,
8118                                                                                 OPJ_UINT32 p_tile_no,
8119                                                                                 OPJ_UINT32 p_comp_no )
8120 {
8121         opj_cp_t *l_cp = 00;
8122         opj_tcp_t *l_tcp = 00;
8123         opj_tccp_t *l_tccp = 00;
8124
8125         /* preconditions */
8126         assert(p_j2k != 00);
8127
8128         l_cp = &(p_j2k->m_cp);
8129         l_tcp = &l_cp->tcps[p_tile_no];
8130         l_tccp = &l_tcp->tccps[p_comp_no];
8131
8132         /* preconditions again */
8133         assert(p_tile_no < (l_cp->tw * l_cp->th));
8134         assert(p_comp_no < p_j2k->m_private_image->numcomps);
8135
8136         if (l_tccp->csty & J2K_CCP_CSTY_PRT) {
8137                 return 5 + l_tccp->numresolutions;
8138         }
8139         else {
8140                 return 5;
8141         }
8142 }
8143
8144 OPJ_BOOL opj_j2k_write_SPCod_SPCoc(     opj_j2k_t *p_j2k,
8145                                                                     OPJ_UINT32 p_tile_no,
8146                                                                     OPJ_UINT32 p_comp_no,
8147                                                                     OPJ_BYTE * p_data,
8148                                                                     OPJ_UINT32 * p_header_size,
8149                                                                     struct opj_event_mgr * p_manager )
8150 {
8151         OPJ_UINT32 i;
8152         opj_cp_t *l_cp = 00;
8153         opj_tcp_t *l_tcp = 00;
8154         opj_tccp_t *l_tccp = 00;
8155
8156         /* preconditions */
8157         assert(p_j2k != 00);
8158         assert(p_header_size != 00);
8159         assert(p_manager != 00);
8160         assert(p_data != 00);
8161
8162         l_cp = &(p_j2k->m_cp);
8163         l_tcp = &l_cp->tcps[p_tile_no];
8164         l_tccp = &l_tcp->tccps[p_comp_no];
8165
8166         /* preconditions again */
8167         assert(p_tile_no < (l_cp->tw * l_cp->th));
8168         assert(p_comp_no <(p_j2k->m_private_image->numcomps));
8169
8170         if (*p_header_size < 5) {
8171                 opj_event_msg(p_manager, EVT_ERROR, "Error writing SPCod SPCoc element\n");
8172                 return OPJ_FALSE;
8173         }
8174
8175         opj_write_bytes(p_data,l_tccp->numresolutions - 1, 1);  /* SPcoc (D) */
8176         ++p_data;
8177
8178         opj_write_bytes(p_data,l_tccp->cblkw - 2, 1);                   /* SPcoc (E) */
8179         ++p_data;
8180
8181         opj_write_bytes(p_data,l_tccp->cblkh - 2, 1);                   /* SPcoc (F) */
8182         ++p_data;
8183
8184         opj_write_bytes(p_data,l_tccp->cblksty, 1);                             /* SPcoc (G) */
8185         ++p_data;
8186
8187         opj_write_bytes(p_data,l_tccp->qmfbid, 1);                              /* SPcoc (H) */
8188         ++p_data;
8189
8190         *p_header_size = *p_header_size - 5;
8191
8192         if (l_tccp->csty & J2K_CCP_CSTY_PRT) {
8193
8194                 if (*p_header_size < l_tccp->numresolutions) {
8195                         opj_event_msg(p_manager, EVT_ERROR, "Error writing SPCod SPCoc element\n");
8196                         return OPJ_FALSE;
8197                 }
8198
8199                 for (i = 0; i < l_tccp->numresolutions; ++i) {
8200                         opj_write_bytes(p_data,l_tccp->prcw[i] + (l_tccp->prch[i] << 4), 1);    /* SPcoc (I_i) */
8201                         ++p_data;
8202                 }
8203
8204                 *p_header_size = *p_header_size - l_tccp->numresolutions;
8205         }
8206
8207         return OPJ_TRUE;
8208 }
8209
8210 OPJ_BOOL opj_j2k_read_SPCod_SPCoc(  opj_j2k_t *p_j2k,
8211                                                                 OPJ_UINT32 compno,
8212                                                                 OPJ_BYTE * p_header_data,
8213                                                                 OPJ_UINT32 * p_header_size,
8214                                                                 opj_event_mgr_t * p_manager)
8215 {
8216         OPJ_UINT32 i, l_tmp;
8217         opj_cp_t *l_cp = NULL;
8218         opj_tcp_t *l_tcp = NULL;
8219         opj_tccp_t *l_tccp = NULL;
8220         OPJ_BYTE * l_current_ptr = NULL;
8221
8222         /* preconditions */
8223         assert(p_j2k != 00);
8224         assert(p_manager != 00);
8225         assert(p_header_data != 00);
8226
8227         l_cp = &(p_j2k->m_cp);
8228         l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ?
8229                                 &l_cp->tcps[p_j2k->m_current_tile_number] :
8230                                 p_j2k->m_specific_param.m_decoder.m_default_tcp;
8231
8232         /* precondition again */
8233         assert(compno < p_j2k->m_private_image->numcomps);
8234
8235         l_tccp = &l_tcp->tccps[compno];
8236         l_current_ptr = p_header_data;
8237
8238         /* make sure room is sufficient */
8239         if (*p_header_size < 5) {
8240                 opj_event_msg(p_manager, EVT_ERROR, "Error reading SPCod SPCoc element\n");
8241                 return OPJ_FALSE;
8242         }
8243
8244         opj_read_bytes(l_current_ptr, &l_tccp->numresolutions ,1);              /* SPcox (D) */
8245         ++l_tccp->numresolutions;                                                                               /* tccp->numresolutions = read() + 1 */
8246         if (l_tccp->numresolutions > OPJ_J2K_MAXRLVLS) {
8247                 opj_event_msg(p_manager, EVT_ERROR,
8248                               "Invalid value for numresolutions : %d, max value is set in openjpeg.h at %d\n",
8249                               l_tccp->numresolutions, OPJ_J2K_MAXRLVLS);
8250                 return OPJ_FALSE;
8251         }
8252         ++l_current_ptr;
8253
8254         /* If user wants to remove more resolutions than the codestream contains, return error */
8255         if (l_cp->m_specific_param.m_dec.m_reduce >= l_tccp->numresolutions) {
8256                 opj_event_msg(p_manager, EVT_ERROR, "Error decoding component %d.\nThe number of resolutions to remove is higher than the number "
8257                                         "of resolutions of this component\nModify the cp_reduce parameter.\n\n", compno);
8258                 p_j2k->m_specific_param.m_decoder.m_state |= 0x8000;/* FIXME J2K_DEC_STATE_ERR;*/
8259                 return OPJ_FALSE;
8260         }
8261
8262         opj_read_bytes(l_current_ptr,&l_tccp->cblkw ,1);                /* SPcoc (E) */
8263         ++l_current_ptr;
8264         l_tccp->cblkw += 2;
8265
8266         opj_read_bytes(l_current_ptr,&l_tccp->cblkh ,1);                /* SPcoc (F) */
8267         ++l_current_ptr;
8268         l_tccp->cblkh += 2;
8269
8270         opj_read_bytes(l_current_ptr,&l_tccp->cblksty ,1);              /* SPcoc (G) */
8271         ++l_current_ptr;
8272
8273         opj_read_bytes(l_current_ptr,&l_tccp->qmfbid ,1);               /* SPcoc (H) */
8274         ++l_current_ptr;
8275
8276         *p_header_size = *p_header_size - 5;
8277
8278         /* use custom precinct size ? */
8279         if (l_tccp->csty & J2K_CCP_CSTY_PRT) {
8280                 if (*p_header_size < l_tccp->numresolutions) {
8281                         opj_event_msg(p_manager, EVT_ERROR, "Error reading SPCod SPCoc element\n");
8282                         return OPJ_FALSE;
8283                 }
8284
8285                 for     (i = 0; i < l_tccp->numresolutions; ++i) {
8286                         opj_read_bytes(l_current_ptr,&l_tmp ,1);                /* SPcoc (I_i) */
8287                         ++l_current_ptr;
8288                         l_tccp->prcw[i] = l_tmp & 0xf;
8289                         l_tccp->prch[i] = l_tmp >> 4;
8290                 }
8291
8292                 *p_header_size = *p_header_size - l_tccp->numresolutions;
8293         }
8294         else {
8295                 /* set default size for the precinct width and height */
8296                 for     (i = 0; i < l_tccp->numresolutions; ++i) {
8297                         l_tccp->prcw[i] = 15;
8298                         l_tccp->prch[i] = 15;
8299                 }
8300         }
8301
8302 #ifdef WIP_REMOVE_MSD
8303         /* INDEX >> */
8304         if (p_j2k->cstr_info && compno == 0) {
8305                 OPJ_UINT32 l_data_size = l_tccp->numresolutions * sizeof(OPJ_UINT32);
8306
8307                 p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].tccp_info[compno].cblkh = l_tccp->cblkh;
8308                 p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].tccp_info[compno].cblkw = l_tccp->cblkw;
8309                 p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].tccp_info[compno].numresolutions = l_tccp->numresolutions;
8310                 p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].tccp_info[compno].cblksty = l_tccp->cblksty;
8311                 p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].tccp_info[compno].qmfbid = l_tccp->qmfbid;
8312
8313                 memcpy(p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].pdx,l_tccp->prcw, l_data_size);
8314                 memcpy(p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].pdy,l_tccp->prch, l_data_size);
8315         }
8316         /* << INDEX */
8317 #endif
8318
8319         return OPJ_TRUE;
8320 }
8321
8322 void opj_j2k_copy_tile_component_parameters( opj_j2k_t *p_j2k )
8323 {
8324         /* loop */
8325         OPJ_UINT32 i;
8326         opj_cp_t *l_cp = NULL;
8327         opj_tcp_t *l_tcp = NULL;
8328         opj_tccp_t *l_ref_tccp = NULL, *l_copied_tccp = NULL;
8329         OPJ_UINT32 l_prc_size;
8330
8331         /* preconditions */
8332         assert(p_j2k != 00);
8333
8334         l_cp = &(p_j2k->m_cp);
8335         l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ? /* FIXME J2K_DEC_STATE_TPH*/
8336                                 &l_cp->tcps[p_j2k->m_current_tile_number] :
8337                                 p_j2k->m_specific_param.m_decoder.m_default_tcp;
8338
8339         l_ref_tccp = &l_tcp->tccps[0];
8340         l_copied_tccp = l_ref_tccp + 1;
8341         l_prc_size = l_ref_tccp->numresolutions * sizeof(OPJ_UINT32);
8342
8343         for     (i=1; i<p_j2k->m_private_image->numcomps; ++i) {
8344                 l_copied_tccp->numresolutions = l_ref_tccp->numresolutions;
8345                 l_copied_tccp->cblkw = l_ref_tccp->cblkw;
8346                 l_copied_tccp->cblkh = l_ref_tccp->cblkh;
8347                 l_copied_tccp->cblksty = l_ref_tccp->cblksty;
8348                 l_copied_tccp->qmfbid = l_ref_tccp->qmfbid;
8349                 memcpy(l_copied_tccp->prcw,l_ref_tccp->prcw,l_prc_size);
8350                 memcpy(l_copied_tccp->prch,l_ref_tccp->prch,l_prc_size);
8351                 ++l_copied_tccp;
8352         }
8353 }
8354
8355 OPJ_UINT32 opj_j2k_get_SQcd_SQcc_size ( opj_j2k_t *p_j2k,
8356                                                                         OPJ_UINT32 p_tile_no,
8357                                                                         OPJ_UINT32 p_comp_no )
8358 {
8359         OPJ_UINT32 l_num_bands;
8360
8361         opj_cp_t *l_cp = 00;
8362         opj_tcp_t *l_tcp = 00;
8363         opj_tccp_t *l_tccp = 00;
8364
8365         /* preconditions */
8366         assert(p_j2k != 00);
8367
8368         l_cp = &(p_j2k->m_cp);
8369         l_tcp = &l_cp->tcps[p_tile_no];
8370         l_tccp = &l_tcp->tccps[p_comp_no];
8371
8372         /* preconditions again */
8373         assert(p_tile_no < l_cp->tw * l_cp->th);
8374         assert(p_comp_no < p_j2k->m_private_image->numcomps);
8375
8376         l_num_bands = (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) ? 1 : (l_tccp->numresolutions * 3 - 2);
8377
8378         if (l_tccp->qntsty == J2K_CCP_QNTSTY_NOQNT)  {
8379                 return 1 + l_num_bands;
8380         }
8381         else {
8382                 return 1 + 2*l_num_bands;
8383         }
8384 }
8385
8386 OPJ_BOOL opj_j2k_write_SQcd_SQcc(       opj_j2k_t *p_j2k,
8387                                                                 OPJ_UINT32 p_tile_no,
8388                                                                 OPJ_UINT32 p_comp_no,
8389                                                                 OPJ_BYTE * p_data,
8390                                                                 OPJ_UINT32 * p_header_size,
8391                                                                 struct opj_event_mgr * p_manager )
8392 {
8393         OPJ_UINT32 l_header_size;
8394         OPJ_UINT32 l_band_no, l_num_bands;
8395         OPJ_UINT32 l_expn,l_mant;
8396
8397         opj_cp_t *l_cp = 00;
8398         opj_tcp_t *l_tcp = 00;
8399         opj_tccp_t *l_tccp = 00;
8400
8401         /* preconditions */
8402         assert(p_j2k != 00);
8403         assert(p_header_size != 00);
8404         assert(p_manager != 00);
8405         assert(p_data != 00);
8406
8407         l_cp = &(p_j2k->m_cp);
8408         l_tcp = &l_cp->tcps[p_tile_no];
8409         l_tccp = &l_tcp->tccps[p_comp_no];
8410
8411         /* preconditions again */
8412         assert(p_tile_no < l_cp->tw * l_cp->th);
8413         assert(p_comp_no <p_j2k->m_private_image->numcomps);
8414
8415         l_num_bands = (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) ? 1 : (l_tccp->numresolutions * 3 - 2);
8416
8417         if (l_tccp->qntsty == J2K_CCP_QNTSTY_NOQNT)  {
8418                 l_header_size = 1 + l_num_bands;
8419
8420                 if (*p_header_size < l_header_size) {
8421                         opj_event_msg(p_manager, EVT_ERROR, "Error writing SQcd SQcc element\n");
8422                         return OPJ_FALSE;
8423                 }
8424
8425                 opj_write_bytes(p_data,l_tccp->qntsty + (l_tccp->numgbits << 5), 1);    /* Sqcx */
8426                 ++p_data;
8427
8428                 for (l_band_no = 0; l_band_no < l_num_bands; ++l_band_no) {
8429                         l_expn = l_tccp->stepsizes[l_band_no].expn;
8430                         opj_write_bytes(p_data, l_expn << 3, 1);        /* SPqcx_i */
8431                         ++p_data;
8432                 }
8433         }
8434         else {
8435                 l_header_size = 1 + 2*l_num_bands;
8436
8437                 if (*p_header_size < l_header_size) {
8438                         opj_event_msg(p_manager, EVT_ERROR, "Error writing SQcd SQcc element\n");
8439                         return OPJ_FALSE;
8440                 }
8441
8442                 opj_write_bytes(p_data,l_tccp->qntsty + (l_tccp->numgbits << 5), 1);    /* Sqcx */
8443                 ++p_data;
8444
8445                 for (l_band_no = 0; l_band_no < l_num_bands; ++l_band_no) {
8446                         l_expn = l_tccp->stepsizes[l_band_no].expn;
8447                         l_mant = l_tccp->stepsizes[l_band_no].mant;
8448
8449                         opj_write_bytes(p_data, (l_expn << 11) + l_mant, 2);    /* SPqcx_i */
8450                         p_data += 2;
8451                 }
8452         }
8453
8454         *p_header_size = *p_header_size - l_header_size;
8455
8456         return OPJ_TRUE;
8457 }
8458
8459 OPJ_BOOL opj_j2k_read_SQcd_SQcc(opj_j2k_t *p_j2k,
8460                                                             OPJ_UINT32 p_comp_no,
8461                                                             OPJ_BYTE* p_header_data,
8462                                                             OPJ_UINT32 * p_header_size,
8463                                                             opj_event_mgr_t * p_manager
8464                                                             )
8465 {
8466         /* loop*/
8467         OPJ_UINT32 l_band_no;
8468         opj_cp_t *l_cp = 00;
8469         opj_tcp_t *l_tcp = 00;
8470         opj_tccp_t *l_tccp = 00;
8471         OPJ_BYTE * l_current_ptr = 00;
8472         OPJ_UINT32 l_tmp, l_num_band;
8473
8474         /* preconditions*/
8475         assert(p_j2k != 00);
8476         assert(p_manager != 00);
8477         assert(p_header_data != 00);
8478
8479         l_cp = &(p_j2k->m_cp);
8480         /* come from tile part header or main header ?*/
8481         l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ? /*FIXME J2K_DEC_STATE_TPH*/
8482                                 &l_cp->tcps[p_j2k->m_current_tile_number] :
8483                                 p_j2k->m_specific_param.m_decoder.m_default_tcp;
8484
8485         /* precondition again*/
8486         assert(p_comp_no <  p_j2k->m_private_image->numcomps);
8487
8488         l_tccp = &l_tcp->tccps[p_comp_no];
8489         l_current_ptr = p_header_data;
8490
8491         if (*p_header_size < 1) {
8492                 opj_event_msg(p_manager, EVT_ERROR, "Error reading SQcd or SQcc element\n");
8493                 return OPJ_FALSE;
8494         }
8495         *p_header_size -= 1;
8496
8497         opj_read_bytes(l_current_ptr, &l_tmp ,1);                       /* Sqcx */
8498         ++l_current_ptr;
8499
8500         l_tccp->qntsty = l_tmp & 0x1f;
8501         l_tccp->numgbits = l_tmp >> 5;
8502         if (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) {
8503         l_num_band = 1;
8504         }
8505         else {
8506                 l_num_band = (l_tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) ?
8507                         (*p_header_size) :
8508                         (*p_header_size) / 2;
8509
8510                 if( l_num_band > OPJ_J2K_MAXBANDS ) {
8511                         opj_event_msg(p_manager, EVT_WARNING, "While reading CCP_QNTSTY element inside QCD or QCC marker segment, "
8512                                 "number of subbands (%d) is greater to OPJ_J2K_MAXBANDS (%d). So we limit the number of elements stored to "
8513                                 "OPJ_J2K_MAXBANDS (%d) and skip the rest. \n", l_num_band, OPJ_J2K_MAXBANDS, OPJ_J2K_MAXBANDS);
8514                         /*return OPJ_FALSE;*/
8515                 }
8516         }
8517
8518 #ifdef USE_JPWL
8519         if (l_cp->correct) {
8520
8521                 /* if JPWL is on, we check whether there are too many subbands */
8522                 if (/*(l_num_band < 0) ||*/ (l_num_band >= OPJ_J2K_MAXBANDS)) {
8523                         opj_event_msg(p_manager, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
8524                                 "JPWL: bad number of subbands in Sqcx (%d)\n",
8525                                 l_num_band);
8526                         if (!JPWL_ASSUME) {
8527                                 opj_event_msg(p_manager, EVT_ERROR, "JPWL: giving up\n");
8528                                 return OPJ_FALSE;
8529                         }
8530                         /* we try to correct */
8531                         l_num_band = 1;
8532                         opj_event_msg(p_manager, EVT_WARNING, "- trying to adjust them\n"
8533                                 "- setting number of bands to %d => HYPOTHESIS!!!\n",
8534                                 l_num_band);
8535                 };
8536
8537         };
8538 #endif /* USE_JPWL */
8539
8540         if (l_tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) {
8541                 for     (l_band_no = 0; l_band_no < l_num_band; l_band_no++) {
8542                         opj_read_bytes(l_current_ptr, &l_tmp ,1);                       /* SPqcx_i */
8543                         ++l_current_ptr;
8544                         if (l_band_no < OPJ_J2K_MAXBANDS){
8545                                 l_tccp->stepsizes[l_band_no].expn = l_tmp>>3;
8546                                 l_tccp->stepsizes[l_band_no].mant = 0;
8547                         }
8548                 }
8549                 *p_header_size = *p_header_size - l_num_band;
8550         }
8551         else {
8552                 for     (l_band_no = 0; l_band_no < l_num_band; l_band_no++) {
8553                         opj_read_bytes(l_current_ptr, &l_tmp ,2);                       /* SPqcx_i */
8554                         l_current_ptr+=2;
8555                         if (l_band_no < OPJ_J2K_MAXBANDS){
8556                                 l_tccp->stepsizes[l_band_no].expn = l_tmp >> 11;
8557                                 l_tccp->stepsizes[l_band_no].mant = l_tmp & 0x7ff;
8558                         }
8559                 }
8560                 *p_header_size = *p_header_size - 2*l_num_band;
8561         }
8562
8563         /* Add Antonin : if scalar_derived -> compute other stepsizes */
8564         if (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) {
8565                 for (l_band_no = 1; l_band_no < OPJ_J2K_MAXBANDS; l_band_no++) {
8566                         l_tccp->stepsizes[l_band_no].expn =
8567                                 ((l_tccp->stepsizes[0].expn) - ((l_band_no - 1) / 3) > 0) ?
8568                                         (l_tccp->stepsizes[0].expn) - ((l_band_no - 1) / 3) : 0;
8569                         l_tccp->stepsizes[l_band_no].mant = l_tccp->stepsizes[0].mant;
8570                 }
8571         }
8572
8573         return OPJ_TRUE;
8574 }
8575
8576 void opj_j2k_copy_tile_quantization_parameters( opj_j2k_t *p_j2k )
8577 {
8578         OPJ_UINT32 i;
8579         opj_cp_t *l_cp = NULL;
8580         opj_tcp_t *l_tcp = NULL;
8581         opj_tccp_t *l_ref_tccp = NULL;
8582         opj_tccp_t *l_copied_tccp = NULL;
8583         OPJ_UINT32 l_size;
8584
8585         /* preconditions */
8586         assert(p_j2k != 00);
8587
8588         l_cp = &(p_j2k->m_cp);
8589         l_tcp = p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH ?
8590                         &l_cp->tcps[p_j2k->m_current_tile_number] :
8591                         p_j2k->m_specific_param.m_decoder.m_default_tcp;
8592
8593         l_ref_tccp = &l_tcp->tccps[0];
8594         l_copied_tccp = l_ref_tccp + 1;
8595         l_size = OPJ_J2K_MAXBANDS * sizeof(opj_stepsize_t);
8596
8597         for     (i=1;i<p_j2k->m_private_image->numcomps;++i) {
8598                 l_copied_tccp->qntsty = l_ref_tccp->qntsty;
8599                 l_copied_tccp->numgbits = l_ref_tccp->numgbits;
8600                 memcpy(l_copied_tccp->stepsizes,l_ref_tccp->stepsizes,l_size);
8601                 ++l_copied_tccp;
8602         }
8603 }
8604
8605 void j2k_dump (opj_j2k_t* p_j2k, OPJ_INT32 flag, FILE* out_stream)
8606 {
8607         /* Check if the flag is compatible with j2k file*/
8608         if ( (flag & OPJ_JP2_INFO) || (flag & OPJ_JP2_IND)){
8609                 fprintf(out_stream, "Wrong flag\n");
8610                 return;
8611         }
8612
8613         /* Dump the image_header */
8614         if (flag & OPJ_IMG_INFO){
8615                 if (p_j2k->m_private_image)
8616                         j2k_dump_image_header(p_j2k->m_private_image, 0, out_stream);
8617         }
8618
8619         /* Dump the codestream info from main header */
8620         if (flag & OPJ_J2K_MH_INFO){
8621                 opj_j2k_dump_MH_info(p_j2k, out_stream);
8622         }
8623
8624         /* Dump the codestream info of the current tile */
8625         if (flag & OPJ_J2K_TH_INFO){
8626
8627         }
8628
8629         /* Dump the codestream index from main header */
8630         if (flag & OPJ_J2K_MH_IND){
8631                 opj_j2k_dump_MH_index(p_j2k, out_stream);
8632         }
8633
8634         /* Dump the codestream index of the current tile */
8635         if (flag & OPJ_J2K_TH_IND){
8636
8637         }
8638
8639 }
8640
8641 void opj_j2k_dump_MH_index(opj_j2k_t* p_j2k, FILE* out_stream)
8642 {
8643         opj_codestream_index_t* cstr_index = p_j2k->cstr_index;
8644         OPJ_UINT32 it_marker, it_tile, it_tile_part;
8645
8646         fprintf(out_stream, "Codestream index from main header: {\n");
8647
8648         fprintf(out_stream, "\t Main header start position=%" PRIi64 "\n"
8649                                     "\t Main header end position=%" PRIi64 "\n",
8650                         cstr_index->main_head_start, cstr_index->main_head_end);
8651
8652         fprintf(out_stream, "\t Marker list: {\n");
8653
8654         if (cstr_index->marker){
8655                 for (it_marker=0; it_marker < cstr_index->marknum ; it_marker++){
8656                         fprintf(out_stream, "\t\t type=%#x, pos=%" PRIi64 ", len=%d\n",
8657                                         cstr_index->marker[it_marker].type,
8658                                         cstr_index->marker[it_marker].pos,
8659                                         cstr_index->marker[it_marker].len );
8660                 }
8661         }
8662
8663         fprintf(out_stream, "\t }\n");
8664
8665         if (cstr_index->tile_index){
8666
8667         /* Simple test to avoid to write empty information*/
8668         OPJ_UINT32 l_acc_nb_of_tile_part = 0;
8669         for (it_tile=0; it_tile < cstr_index->nb_of_tiles ; it_tile++){
8670                         l_acc_nb_of_tile_part += cstr_index->tile_index[it_tile].nb_tps;
8671         }
8672
8673         if (l_acc_nb_of_tile_part)
8674         {
8675             fprintf(out_stream, "\t Tile index: {\n");
8676
8677                     for (it_tile=0; it_tile < cstr_index->nb_of_tiles ; it_tile++){
8678                             OPJ_UINT32 nb_of_tile_part = cstr_index->tile_index[it_tile].nb_tps;
8679
8680                             fprintf(out_stream, "\t\t nb of tile-part in tile [%d]=%d\n", it_tile, nb_of_tile_part);
8681
8682                             if (cstr_index->tile_index[it_tile].tp_index){
8683                                     for (it_tile_part =0; it_tile_part < nb_of_tile_part; it_tile_part++){
8684                                             fprintf(out_stream, "\t\t\t tile-part[%d]: star_pos=%" PRIi64 ", end_header=%" PRIi64 ", end_pos=%" PRIi64 ".\n",
8685                                                             it_tile_part,
8686                                                             cstr_index->tile_index[it_tile].tp_index[it_tile_part].start_pos,
8687                                                             cstr_index->tile_index[it_tile].tp_index[it_tile_part].end_header,
8688                                                             cstr_index->tile_index[it_tile].tp_index[it_tile_part].end_pos);
8689                                     }
8690                             }
8691
8692                             if (cstr_index->tile_index[it_tile].marker){
8693                                     for (it_marker=0; it_marker < cstr_index->tile_index[it_tile].marknum ; it_marker++){
8694                                             fprintf(out_stream, "\t\t type=%#x, pos=%" PRIi64 ", len=%d\n",
8695                                                             cstr_index->tile_index[it_tile].marker[it_marker].type,
8696                                                             cstr_index->tile_index[it_tile].marker[it_marker].pos,
8697                                                             cstr_index->tile_index[it_tile].marker[it_marker].len );
8698                                     }
8699                             }
8700                     }
8701                     fprintf(out_stream,"\t }\n");
8702         }
8703         }
8704
8705         fprintf(out_stream,"}\n");
8706
8707 }
8708
8709 void opj_j2k_dump_MH_info(opj_j2k_t* p_j2k, FILE* out_stream)
8710 {
8711         opj_tcp_t * l_default_tile=NULL;
8712
8713         fprintf(out_stream, "Codestream info from main header: {\n");
8714
8715         fprintf(out_stream, "\t tx0=%d, ty0=%d\n", p_j2k->m_cp.tx0, p_j2k->m_cp.ty0);
8716         fprintf(out_stream, "\t tdx=%d, tdy=%d\n", p_j2k->m_cp.tdx, p_j2k->m_cp.tdy);
8717         fprintf(out_stream, "\t tw=%d, th=%d\n", p_j2k->m_cp.tw, p_j2k->m_cp.th);
8718
8719         l_default_tile = p_j2k->m_specific_param.m_decoder.m_default_tcp;
8720         if (l_default_tile)
8721         {
8722                 OPJ_INT32 compno;
8723                 OPJ_INT32 numcomps = p_j2k->m_private_image->numcomps;
8724
8725                 fprintf(out_stream, "\t default tile {\n");
8726                 fprintf(out_stream, "\t\t csty=%#x\n", l_default_tile->csty);
8727                 fprintf(out_stream, "\t\t prg=%#x\n", l_default_tile->prg);
8728                 fprintf(out_stream, "\t\t numlayers=%d\n", l_default_tile->numlayers);
8729                 fprintf(out_stream, "\t\t mct=%x\n", l_default_tile->mct);
8730
8731                 for (compno = 0; compno < numcomps; compno++) {
8732                         opj_tccp_t *l_tccp = &(l_default_tile->tccps[compno]);
8733                         OPJ_UINT32 resno;
8734       OPJ_INT32 bandno, numbands;
8735
8736                         /* coding style*/
8737                         fprintf(out_stream, "\t\t comp %d {\n", compno);
8738                         fprintf(out_stream, "\t\t\t csty=%#x\n", l_tccp->csty);
8739                         fprintf(out_stream, "\t\t\t numresolutions=%d\n", l_tccp->numresolutions);
8740                         fprintf(out_stream, "\t\t\t cblkw=2^%d\n", l_tccp->cblkw);
8741                         fprintf(out_stream, "\t\t\t cblkh=2^%d\n", l_tccp->cblkh);
8742                         fprintf(out_stream, "\t\t\t cblksty=%#x\n", l_tccp->cblksty);
8743                         fprintf(out_stream, "\t\t\t qmfbid=%d\n", l_tccp->qmfbid);
8744
8745                         fprintf(out_stream, "\t\t\t preccintsize (w,h)=");
8746                         for (resno = 0; resno < l_tccp->numresolutions; resno++) {
8747                                 fprintf(out_stream, "(%d,%d) ", l_tccp->prcw[resno], l_tccp->prch[resno]);
8748                         }
8749                         fprintf(out_stream, "\n");
8750
8751                         /* quantization style*/
8752                         fprintf(out_stream, "\t\t\t qntsty=%d\n", l_tccp->qntsty);
8753                         fprintf(out_stream, "\t\t\t numgbits=%d\n", l_tccp->numgbits);
8754                         fprintf(out_stream, "\t\t\t stepsizes (m,e)=");
8755                         numbands = (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) ? 1 : l_tccp->numresolutions * 3 - 2;
8756                         for (bandno = 0; bandno < numbands; bandno++) {
8757                                 fprintf(out_stream, "(%d,%d) ", l_tccp->stepsizes[bandno].mant,
8758                                         l_tccp->stepsizes[bandno].expn);
8759                         }
8760                         fprintf(out_stream, "\n");
8761
8762                         /* RGN value*/
8763                         fprintf(out_stream, "\t\t\t roishift=%d\n", l_tccp->roishift);
8764
8765                         fprintf(out_stream, "\t\t }\n");
8766                 } /*end of component of default tile*/
8767                 fprintf(out_stream, "\t }\n"); /*end of default tile*/
8768
8769         }
8770
8771         fprintf(out_stream, "}\n");
8772
8773 }
8774
8775 void j2k_dump_image_header(opj_image_t* img_header, OPJ_BOOL dev_dump_flag, FILE* out_stream)
8776 {
8777         char tab[2];
8778
8779         if (dev_dump_flag){
8780                 fprintf(stdout, "[DEV] Dump an image_header struct {\n");
8781                 tab[0] = '\0';
8782         }
8783         else {
8784                 fprintf(out_stream, "Image info {\n");
8785                 tab[0] = '\t';tab[1] = '\0';
8786         }
8787
8788         fprintf(out_stream, "%s x0=%d, y0=%d\n", tab, img_header->x0, img_header->y0);
8789         fprintf(out_stream,     "%s x1=%d, y1=%d\n", tab, img_header->x1, img_header->y1);
8790         fprintf(out_stream, "%s numcomps=%d\n", tab, img_header->numcomps);
8791
8792         if (img_header->comps){
8793                 OPJ_UINT32 compno;
8794                 for (compno = 0; compno < img_header->numcomps; compno++) {
8795                         fprintf(out_stream, "%s\t component %d {\n", tab, compno);
8796                         j2k_dump_image_comp_header(&(img_header->comps[compno]), dev_dump_flag, out_stream);
8797                         fprintf(out_stream,"%s}\n",tab);
8798                 }
8799         }
8800
8801         fprintf(out_stream, "}\n");
8802 }
8803
8804 void j2k_dump_image_comp_header(opj_image_comp_t* comp_header, OPJ_BOOL dev_dump_flag, FILE* out_stream)
8805 {
8806         char tab[3];
8807
8808         if (dev_dump_flag){
8809                 fprintf(stdout, "[DEV] Dump an image_comp_header struct {\n");
8810                 tab[0] = '\0';
8811         }       else {
8812                 tab[0] = '\t';tab[1] = '\t';tab[2] = '\0';
8813         }
8814
8815         fprintf(out_stream, "%s dx=%d, dy=%d\n", tab, comp_header->dx, comp_header->dy);
8816         fprintf(out_stream, "%s prec=%d\n", tab, comp_header->prec);
8817         fprintf(out_stream, "%s sgnd=%d\n", tab, comp_header->sgnd);
8818
8819         if (dev_dump_flag)
8820                 fprintf(out_stream, "}\n");
8821 }
8822
8823 opj_codestream_info_v2_t* j2k_get_cstr_info(opj_j2k_t* p_j2k)
8824 {
8825         OPJ_UINT32 compno;
8826         OPJ_UINT32 numcomps = p_j2k->m_private_image->numcomps;
8827         opj_tcp_t *l_default_tile;
8828         opj_codestream_info_v2_t* cstr_info = (opj_codestream_info_v2_t*) opj_calloc(1,sizeof(opj_codestream_info_v2_t));
8829                 if (!cstr_info)
8830                         return NULL;
8831
8832         cstr_info->nbcomps = p_j2k->m_private_image->numcomps;
8833
8834         cstr_info->tx0 = p_j2k->m_cp.tx0;
8835         cstr_info->ty0 = p_j2k->m_cp.ty0;
8836         cstr_info->tdx = p_j2k->m_cp.tdx;
8837         cstr_info->tdy = p_j2k->m_cp.tdy;
8838         cstr_info->tw = p_j2k->m_cp.tw;
8839         cstr_info->th = p_j2k->m_cp.th;
8840
8841         cstr_info->tile_info = NULL; /* Not fill from the main header*/
8842
8843         l_default_tile = p_j2k->m_specific_param.m_decoder.m_default_tcp;
8844
8845         cstr_info->m_default_tile_info.csty = l_default_tile->csty;
8846         cstr_info->m_default_tile_info.prg = l_default_tile->prg;
8847         cstr_info->m_default_tile_info.numlayers = l_default_tile->numlayers;
8848         cstr_info->m_default_tile_info.mct = l_default_tile->mct;
8849
8850         cstr_info->m_default_tile_info.tccp_info = (opj_tccp_info_t*) opj_calloc(cstr_info->nbcomps, sizeof(opj_tccp_info_t));
8851                 if (!cstr_info->m_default_tile_info.tccp_info)
8852                 {
8853                         opj_destroy_cstr_info(&cstr_info);
8854                         return NULL;
8855                 }
8856
8857         for (compno = 0; compno < numcomps; compno++) {
8858                 opj_tccp_t *l_tccp = &(l_default_tile->tccps[compno]);
8859                 opj_tccp_info_t *l_tccp_info = &(cstr_info->m_default_tile_info.tccp_info[compno]);
8860                 OPJ_INT32 bandno, numbands;
8861
8862                 /* coding style*/
8863                 l_tccp_info->csty = l_tccp->csty;
8864                 l_tccp_info->numresolutions = l_tccp->numresolutions;
8865                 l_tccp_info->cblkw = l_tccp->cblkw;
8866                 l_tccp_info->cblkh = l_tccp->cblkh;
8867                 l_tccp_info->cblksty = l_tccp->cblksty;
8868                 l_tccp_info->qmfbid = l_tccp->qmfbid;
8869                 if (l_tccp->numresolutions < OPJ_J2K_MAXRLVLS)
8870                 {
8871                         memcpy(l_tccp_info->prch, l_tccp->prch, l_tccp->numresolutions);
8872                         memcpy(l_tccp_info->prcw, l_tccp->prcw, l_tccp->numresolutions);
8873                 }
8874
8875                 /* quantization style*/
8876                 l_tccp_info->qntsty = l_tccp->qntsty;
8877                 l_tccp_info->numgbits = l_tccp->numgbits;
8878
8879                 numbands = (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) ? 1 : l_tccp->numresolutions * 3 - 2;
8880                 if (numbands < OPJ_J2K_MAXBANDS) {
8881                         for (bandno = 0; bandno < numbands; bandno++) {
8882                                 l_tccp_info->stepsizes_mant[bandno] = l_tccp->stepsizes[bandno].mant;
8883                                 l_tccp_info->stepsizes_expn[bandno] = l_tccp->stepsizes[bandno].expn;
8884                         }
8885                 }
8886
8887                 /* RGN value*/
8888                 l_tccp_info->roishift = l_tccp->roishift;
8889         }
8890
8891         return cstr_info;
8892 }
8893
8894 opj_codestream_index_t* j2k_get_cstr_index(opj_j2k_t* p_j2k)
8895 {
8896         opj_codestream_index_t* l_cstr_index = (opj_codestream_index_t*)
8897                         opj_calloc(1,sizeof(opj_codestream_index_t));
8898         if (!l_cstr_index)
8899                 return NULL;
8900
8901         l_cstr_index->main_head_start = p_j2k->cstr_index->main_head_start;
8902         l_cstr_index->main_head_end = p_j2k->cstr_index->main_head_end;
8903         l_cstr_index->codestream_size = p_j2k->cstr_index->codestream_size;
8904
8905         l_cstr_index->marknum = p_j2k->cstr_index->marknum;
8906         l_cstr_index->marker = (opj_marker_info_t*)opj_malloc(l_cstr_index->marknum*sizeof(opj_marker_info_t));
8907         if (!l_cstr_index->marker){
8908                 opj_free( l_cstr_index);
8909                 return NULL;
8910         }
8911
8912         if (p_j2k->cstr_index->marker)
8913                 memcpy(l_cstr_index->marker, p_j2k->cstr_index->marker, l_cstr_index->marknum * sizeof(opj_marker_info_t) );
8914         else{
8915                 opj_free(l_cstr_index->marker);
8916                 l_cstr_index->marker = NULL;
8917         }
8918
8919         l_cstr_index->nb_of_tiles = p_j2k->cstr_index->nb_of_tiles;
8920         l_cstr_index->tile_index = (opj_tile_index_t*)opj_calloc(l_cstr_index->nb_of_tiles, sizeof(opj_tile_index_t) );
8921         if (!l_cstr_index->tile_index){
8922                 opj_free( l_cstr_index->marker);
8923                 opj_free( l_cstr_index);
8924                 return NULL;
8925         }
8926
8927         if (!p_j2k->cstr_index->tile_index){
8928                 opj_free(l_cstr_index->tile_index);
8929                 l_cstr_index->tile_index = NULL;
8930         }
8931         else {
8932                 OPJ_UINT32 it_tile = 0;
8933                 for (it_tile = 0; it_tile < l_cstr_index->nb_of_tiles; it_tile++ ){
8934
8935                         /* Tile Marker*/
8936                         l_cstr_index->tile_index[it_tile].marknum = p_j2k->cstr_index->tile_index[it_tile].marknum;
8937
8938                         l_cstr_index->tile_index[it_tile].marker =
8939                                 (opj_marker_info_t*)opj_malloc(l_cstr_index->tile_index[it_tile].marknum*sizeof(opj_marker_info_t));
8940
8941                         if (!l_cstr_index->tile_index[it_tile].marker) {
8942                                 OPJ_UINT32 it_tile_free;
8943
8944                                 for (it_tile_free=0; it_tile_free < it_tile; it_tile_free++){
8945                                         opj_free(l_cstr_index->tile_index[it_tile_free].marker);
8946                                 }
8947
8948                                 opj_free( l_cstr_index->tile_index);
8949                                 opj_free( l_cstr_index->marker);
8950                                 opj_free( l_cstr_index);
8951                                 return NULL;
8952                         }
8953
8954                         if (p_j2k->cstr_index->tile_index[it_tile].marker)
8955                                 memcpy( l_cstr_index->tile_index[it_tile].marker,
8956                                                 p_j2k->cstr_index->tile_index[it_tile].marker,
8957                                                 l_cstr_index->tile_index[it_tile].marknum * sizeof(opj_marker_info_t) );
8958                         else{
8959                                 opj_free(l_cstr_index->tile_index[it_tile].marker);
8960                                 l_cstr_index->tile_index[it_tile].marker = NULL;
8961                         }
8962
8963                         /* Tile part index*/
8964                         l_cstr_index->tile_index[it_tile].nb_tps = p_j2k->cstr_index->tile_index[it_tile].nb_tps;
8965
8966                         l_cstr_index->tile_index[it_tile].tp_index =
8967                                 (opj_tp_index_t*)opj_malloc(l_cstr_index->tile_index[it_tile].nb_tps*sizeof(opj_tp_index_t));
8968
8969                         if(!l_cstr_index->tile_index[it_tile].tp_index){
8970                                 OPJ_UINT32 it_tile_free;
8971
8972                                 for (it_tile_free=0; it_tile_free < it_tile; it_tile_free++){
8973                                         opj_free(l_cstr_index->tile_index[it_tile_free].marker);
8974                                         opj_free(l_cstr_index->tile_index[it_tile_free].tp_index);
8975                                 }
8976
8977                                 opj_free( l_cstr_index->tile_index);
8978                                 opj_free( l_cstr_index->marker);
8979                                 opj_free( l_cstr_index);
8980                                 return NULL;
8981                         }
8982
8983                         if (p_j2k->cstr_index->tile_index[it_tile].tp_index){
8984                                 memcpy( l_cstr_index->tile_index[it_tile].tp_index,
8985                                                 p_j2k->cstr_index->tile_index[it_tile].tp_index,
8986                                                 l_cstr_index->tile_index[it_tile].nb_tps * sizeof(opj_tp_index_t) );
8987                         }
8988                         else{
8989                                 opj_free(l_cstr_index->tile_index[it_tile].tp_index);
8990                                 l_cstr_index->tile_index[it_tile].tp_index = NULL;
8991                         }
8992
8993                         /* Packet index (NOT USED)*/
8994                         l_cstr_index->tile_index[it_tile].nb_packet = 0;
8995                         l_cstr_index->tile_index[it_tile].packet_index = NULL;
8996
8997                 }
8998         }
8999
9000         return l_cstr_index;
9001 }
9002
9003 OPJ_BOOL opj_j2k_allocate_tile_element_cstr_index(opj_j2k_t *p_j2k)
9004 {
9005         OPJ_UINT32 it_tile=0;
9006
9007         p_j2k->cstr_index->nb_of_tiles = p_j2k->m_cp.tw * p_j2k->m_cp.th;
9008         p_j2k->cstr_index->tile_index = (opj_tile_index_t*)opj_calloc(p_j2k->cstr_index->nb_of_tiles, sizeof(opj_tile_index_t));
9009         if (!p_j2k->cstr_index->tile_index)
9010                 return OPJ_FALSE;
9011
9012         for (it_tile=0; it_tile < p_j2k->cstr_index->nb_of_tiles; it_tile++){
9013                 p_j2k->cstr_index->tile_index[it_tile].maxmarknum = 100;
9014                 p_j2k->cstr_index->tile_index[it_tile].marknum = 0;
9015                 p_j2k->cstr_index->tile_index[it_tile].marker = (opj_marker_info_t*)
9016                                 opj_calloc(p_j2k->cstr_index->tile_index[it_tile].maxmarknum, sizeof(opj_marker_info_t));
9017                 if (!p_j2k->cstr_index->tile_index[it_tile].marker)
9018                         return OPJ_FALSE;
9019         }
9020
9021         return OPJ_TRUE;
9022 }
9023
9024 OPJ_BOOL opj_j2k_decode_tiles ( opj_j2k_t *p_j2k,
9025                                                             opj_stream_private_t *p_stream,
9026                                                             opj_event_mgr_t * p_manager)
9027 {
9028         OPJ_BOOL l_go_on = OPJ_TRUE;
9029         OPJ_UINT32 l_current_tile_no;
9030         OPJ_UINT32 l_data_size,l_max_data_size;
9031         OPJ_INT32 l_tile_x0,l_tile_y0,l_tile_x1,l_tile_y1;
9032         OPJ_UINT32 l_nb_comps;
9033         OPJ_BYTE * l_current_data;
9034         OPJ_UINT32 nr_tiles = 0;
9035
9036         l_current_data = (OPJ_BYTE*)opj_malloc(1000);
9037         if (! l_current_data) {
9038                 opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to decode tiles\n");
9039                 return OPJ_FALSE;
9040         }
9041         l_max_data_size = 1000;
9042
9043         while (OPJ_TRUE) {
9044                 if (! opj_j2k_read_tile_header( p_j2k,
9045                                         &l_current_tile_no,
9046                                         &l_data_size,
9047                                         &l_tile_x0, &l_tile_y0,
9048                                         &l_tile_x1, &l_tile_y1,
9049                                         &l_nb_comps,
9050                                         &l_go_on,
9051                                         p_stream,
9052                                         p_manager)) {
9053                         opj_free(l_current_data);
9054                         return OPJ_FALSE;
9055                 }
9056
9057                 if (! l_go_on) {
9058                         break;
9059                 }
9060
9061                 if (l_data_size > l_max_data_size) {
9062                         OPJ_BYTE *l_new_current_data = (OPJ_BYTE *) opj_realloc(l_current_data, l_data_size);
9063                         if (! l_new_current_data) {
9064                                 opj_free(l_current_data);
9065                                 opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to decode tile %d/%d\n", l_current_tile_no +1, p_j2k->m_cp.th * p_j2k->m_cp.tw);
9066                                 return OPJ_FALSE;
9067                         }
9068                         l_current_data = l_new_current_data;
9069                         l_max_data_size = l_data_size;
9070                 }
9071
9072                 if (! opj_j2k_decode_tile(p_j2k,l_current_tile_no,l_current_data,l_data_size,p_stream,p_manager)) {
9073                         opj_free(l_current_data);
9074                         return OPJ_FALSE;
9075                 }
9076                 opj_event_msg(p_manager, EVT_INFO, "Tile %d/%d has been decoded.\n", l_current_tile_no +1, p_j2k->m_cp.th * p_j2k->m_cp.tw);
9077
9078                 if (! opj_j2k_update_image_data(p_j2k->m_tcd,l_current_data, p_j2k->m_output_image)) {
9079                         opj_free(l_current_data);
9080                         return OPJ_FALSE;
9081                 }
9082                 opj_event_msg(p_manager, EVT_INFO, "Image data has been updated with tile %d.\n\n", l_current_tile_no + 1);
9083                 
9084                 if(opj_stream_get_number_byte_left(p_stream) == 0  
9085                     && p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_NEOC)
9086                     break;
9087                 if(++nr_tiles ==  p_j2k->m_cp.th * p_j2k->m_cp.tw) 
9088                     break;
9089         }
9090
9091         opj_free(l_current_data);
9092
9093         return OPJ_TRUE;
9094 }
9095
9096 /**
9097  * Sets up the procedures to do on decoding data. Developpers wanting to extend the library can add their own reading procedures.
9098  */
9099 static void opj_j2k_setup_decoding (opj_j2k_t *p_j2k)
9100 {
9101         /* preconditions*/
9102         assert(p_j2k != 00);
9103
9104         opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)opj_j2k_decode_tiles);
9105         /* DEVELOPER CORNER, add your custom procedures */
9106
9107 }
9108
9109 /*
9110  * Read and decode one tile.
9111  */
9112 static OPJ_BOOL opj_j2k_decode_one_tile (       opj_j2k_t *p_j2k,
9113                                                                             opj_stream_private_t *p_stream,
9114                                                                             opj_event_mgr_t * p_manager)
9115 {
9116         OPJ_BOOL l_go_on = OPJ_TRUE;
9117         OPJ_UINT32 l_current_tile_no;
9118         OPJ_UINT32 l_tile_no_to_dec;
9119         OPJ_UINT32 l_data_size,l_max_data_size;
9120         OPJ_INT32 l_tile_x0,l_tile_y0,l_tile_x1,l_tile_y1;
9121         OPJ_UINT32 l_nb_comps;
9122         OPJ_BYTE * l_current_data;
9123
9124         l_current_data = (OPJ_BYTE*)opj_malloc(1000);
9125         if (! l_current_data) {
9126                 opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to decode one tile\n");
9127                 return OPJ_FALSE;
9128         }
9129         l_max_data_size = 1000;
9130
9131         /*Allocate and initialize some elements of codestrem index if not already done*/
9132         if( !p_j2k->cstr_index->tile_index)
9133         {
9134                 if (!opj_j2k_allocate_tile_element_cstr_index(p_j2k)){
9135                         opj_free(l_current_data);
9136                         return OPJ_FALSE;
9137                 }
9138         }
9139         /* Move into the codestream to the first SOT used to decode the desired tile */
9140         l_tile_no_to_dec = p_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec;
9141         if (p_j2k->cstr_index->tile_index)
9142                 if(p_j2k->cstr_index->tile_index->tp_index)
9143                 {
9144                         if ( ! p_j2k->cstr_index->tile_index[l_tile_no_to_dec].nb_tps) {
9145                                 /* the index for this tile has not been built,
9146                                  *  so move to the last SOT read */
9147                                 if ( !(opj_stream_read_seek(p_stream, p_j2k->m_specific_param.m_decoder.m_last_sot_read_pos+2, p_manager)) ){
9148                                         opj_event_msg(p_manager, EVT_ERROR, "Problem with seek function\n");
9149                                         return OPJ_FALSE;
9150                                 }
9151                         }
9152                         else{
9153                                 if ( !(opj_stream_read_seek(p_stream, p_j2k->cstr_index->tile_index[l_tile_no_to_dec].tp_index[0].start_pos+2, p_manager)) ) {
9154                                         opj_event_msg(p_manager, EVT_ERROR, "Problem with seek function\n");
9155                                         return OPJ_FALSE;
9156                                 }
9157                         }
9158                         /* Special case if we have previously read the EOC marker (if the previous tile getted is the last ) */
9159                         if(p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_EOC)
9160                                 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_TPHSOT;
9161                 }
9162
9163         while (OPJ_TRUE) {
9164                 if (! opj_j2k_read_tile_header( p_j2k,
9165                                         &l_current_tile_no,
9166                                         &l_data_size,
9167                                         &l_tile_x0, &l_tile_y0,
9168                                         &l_tile_x1, &l_tile_y1,
9169                                         &l_nb_comps,
9170                                         &l_go_on,
9171                                         p_stream,
9172                                         p_manager)) {
9173                         opj_free(l_current_data);
9174                         return OPJ_FALSE;
9175                 }
9176
9177                 if (! l_go_on) {
9178                         break;
9179                 }
9180
9181                 if (l_data_size > l_max_data_size) {
9182                         OPJ_BYTE *l_new_current_data = (OPJ_BYTE *) opj_realloc(l_current_data, l_data_size);
9183                         if (! l_new_current_data) {
9184                                 opj_free(l_current_data);
9185                                 l_current_data = NULL;
9186                                 /* TODO: LH: why tile numbering policy used in messages differs from
9187                                    the one used in opj_j2k_decode_tiles() ? */
9188                                 opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to decode tile %d/%d\n", l_current_tile_no, (p_j2k->m_cp.th * p_j2k->m_cp.tw) - 1);
9189                                 return OPJ_FALSE;
9190                         }
9191                         l_current_data = l_new_current_data;
9192                         l_max_data_size = l_data_size;
9193                 }
9194
9195                 if (! opj_j2k_decode_tile(p_j2k,l_current_tile_no,l_current_data,l_data_size,p_stream,p_manager)) {
9196                         opj_free(l_current_data);
9197                         return OPJ_FALSE;
9198                 }
9199                 opj_event_msg(p_manager, EVT_INFO, "Tile %d/%d has been decoded.\n", l_current_tile_no, (p_j2k->m_cp.th * p_j2k->m_cp.tw) - 1);
9200
9201                 if (! opj_j2k_update_image_data(p_j2k->m_tcd,l_current_data, p_j2k->m_output_image)) {
9202                         opj_free(l_current_data);
9203                         return OPJ_FALSE;
9204                 }
9205                 opj_event_msg(p_manager, EVT_INFO, "Image data has been updated with tile %d.\n\n", l_current_tile_no);
9206
9207                 if(l_current_tile_no == l_tile_no_to_dec)
9208                 {
9209                         /* move into the codestream to the the first SOT (FIXME or not move?)*/
9210                         if (!(opj_stream_read_seek(p_stream, p_j2k->cstr_index->main_head_end + 2, p_manager) ) ) {
9211                                 opj_event_msg(p_manager, EVT_ERROR, "Problem with seek function\n");
9212                                 return OPJ_FALSE;
9213                         }
9214                         break;
9215                 }
9216                 else {
9217                         opj_event_msg(p_manager, EVT_WARNING, "Tile read, decode and updated is not the desired (%d vs %d).\n", l_current_tile_no, l_tile_no_to_dec);
9218                 }
9219
9220         }
9221
9222         opj_free(l_current_data);
9223
9224         return OPJ_TRUE;
9225 }
9226
9227 /**
9228  * Sets up the procedures to do on decoding one tile. Developpers wanting to extend the library can add their own reading procedures.
9229  */
9230 static void opj_j2k_setup_decoding_tile (opj_j2k_t *p_j2k)
9231 {
9232         /* preconditions*/
9233         assert(p_j2k != 00);
9234
9235         opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)opj_j2k_decode_one_tile);
9236         /* DEVELOPER CORNER, add your custom procedures */
9237
9238 }
9239
9240 OPJ_BOOL opj_j2k_decode(opj_j2k_t * p_j2k,
9241                                                 opj_stream_private_t * p_stream,
9242                                                 opj_image_t * p_image,
9243                                                 opj_event_mgr_t * p_manager)
9244 {
9245         OPJ_UINT32 compno;
9246
9247         if (!p_image)
9248                 return OPJ_FALSE;
9249
9250         p_j2k->m_output_image = opj_image_create0();
9251         if (! (p_j2k->m_output_image)) {
9252                 return OPJ_FALSE;
9253         }
9254         opj_copy_image_header(p_image, p_j2k->m_output_image);
9255
9256         /* customization of the decoding */
9257         opj_j2k_setup_decoding(p_j2k);
9258
9259         /* Decode the codestream */
9260         if (! opj_j2k_exec (p_j2k,p_j2k->m_procedure_list,p_stream,p_manager)) {
9261                 opj_image_destroy(p_j2k->m_private_image);
9262                 p_j2k->m_private_image = NULL;
9263                 return OPJ_FALSE;
9264         }
9265
9266         /* Move data and copy one information from codec to output image*/
9267         for (compno = 0; compno < p_image->numcomps; compno++) {
9268                 p_image->comps[compno].resno_decoded = p_j2k->m_output_image->comps[compno].resno_decoded;
9269                 p_image->comps[compno].data = p_j2k->m_output_image->comps[compno].data;
9270                 p_j2k->m_output_image->comps[compno].data = NULL;
9271         }
9272
9273         return OPJ_TRUE;
9274 }
9275
9276 OPJ_BOOL opj_j2k_get_tile(      opj_j2k_t *p_j2k,
9277                                                     opj_stream_private_t *p_stream,
9278                                                     opj_image_t* p_image,
9279                                                     opj_event_mgr_t * p_manager,
9280                                                     OPJ_UINT32 tile_index )
9281 {
9282         OPJ_UINT32 compno;
9283         OPJ_UINT32 l_tile_x, l_tile_y;
9284         opj_image_comp_t* l_img_comp;
9285
9286         if (!p_image) {
9287                 opj_event_msg(p_manager, EVT_ERROR, "We need an image previously created.\n");
9288                 return OPJ_FALSE;
9289         }
9290
9291         if ( /*(tile_index < 0) &&*/ (tile_index >= p_j2k->m_cp.tw * p_j2k->m_cp.th) ){
9292                 opj_event_msg(p_manager, EVT_ERROR, "Tile index provided by the user is incorrect %d (max = %d) \n", tile_index, (p_j2k->m_cp.tw * p_j2k->m_cp.th) - 1);
9293                 return OPJ_FALSE;
9294         }
9295
9296         /* Compute the dimension of the desired tile*/
9297         l_tile_x = tile_index % p_j2k->m_cp.tw;
9298         l_tile_y = tile_index / p_j2k->m_cp.tw;
9299
9300         p_image->x0 = l_tile_x * p_j2k->m_cp.tdx + p_j2k->m_cp.tx0;
9301         if (p_image->x0 < p_j2k->m_private_image->x0)
9302                 p_image->x0 = p_j2k->m_private_image->x0;
9303         p_image->x1 = (l_tile_x + 1) * p_j2k->m_cp.tdx + p_j2k->m_cp.tx0;
9304         if (p_image->x1 > p_j2k->m_private_image->x1)
9305                 p_image->x1 = p_j2k->m_private_image->x1;
9306
9307         p_image->y0 = l_tile_y * p_j2k->m_cp.tdy + p_j2k->m_cp.ty0;
9308         if (p_image->y0 < p_j2k->m_private_image->y0)
9309                 p_image->y0 = p_j2k->m_private_image->y0;
9310         p_image->y1 = (l_tile_y + 1) * p_j2k->m_cp.tdy + p_j2k->m_cp.ty0;
9311         if (p_image->y1 > p_j2k->m_private_image->y1)
9312                 p_image->y1 = p_j2k->m_private_image->y1;
9313
9314         l_img_comp = p_image->comps;
9315         for (compno=0; compno < p_image->numcomps; ++compno)
9316         {
9317                 OPJ_INT32 l_comp_x1, l_comp_y1;
9318
9319                 l_img_comp->factor = p_j2k->m_private_image->comps[compno].factor;
9320
9321                 l_img_comp->x0 = opj_int_ceildiv(p_image->x0, l_img_comp->dx);
9322                 l_img_comp->y0 = opj_int_ceildiv(p_image->y0, l_img_comp->dy);
9323                 l_comp_x1 = opj_int_ceildiv(p_image->x1, l_img_comp->dx);
9324                 l_comp_y1 = opj_int_ceildiv(p_image->y1, l_img_comp->dy);
9325
9326                 l_img_comp->w = opj_int_ceildivpow2(l_comp_x1, l_img_comp->factor) - opj_int_ceildivpow2(l_img_comp->x0, l_img_comp->factor);
9327                 l_img_comp->h = opj_int_ceildivpow2(l_comp_y1, l_img_comp->factor) - opj_int_ceildivpow2(l_img_comp->y0, l_img_comp->factor);
9328
9329                 l_img_comp++;
9330         }
9331
9332         /* Destroy the previous output image*/
9333         if (p_j2k->m_output_image)
9334                 opj_image_destroy(p_j2k->m_output_image);
9335
9336         /* Create the ouput image from the information previously computed*/
9337         p_j2k->m_output_image = opj_image_create0();
9338         if (! (p_j2k->m_output_image)) {
9339                 return OPJ_FALSE;
9340         }
9341         opj_copy_image_header(p_image, p_j2k->m_output_image);
9342
9343         p_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec = tile_index;
9344
9345         /* customization of the decoding */
9346         opj_j2k_setup_decoding_tile(p_j2k);
9347
9348         /* Decode the codestream */
9349         if (! opj_j2k_exec (p_j2k,p_j2k->m_procedure_list,p_stream,p_manager)) {
9350                 opj_image_destroy(p_j2k->m_private_image);
9351                 p_j2k->m_private_image = NULL;
9352                 return OPJ_FALSE;
9353         }
9354
9355         /* Move data and copy one information from codec to output image*/
9356         for (compno = 0; compno < p_image->numcomps; compno++) {
9357                 p_image->comps[compno].resno_decoded = p_j2k->m_output_image->comps[compno].resno_decoded;
9358
9359                 if (p_image->comps[compno].data)
9360                         opj_free(p_image->comps[compno].data);
9361
9362                 p_image->comps[compno].data = p_j2k->m_output_image->comps[compno].data;
9363
9364                 p_j2k->m_output_image->comps[compno].data = NULL;
9365         }
9366
9367         return OPJ_TRUE;
9368 }
9369
9370 OPJ_BOOL opj_j2k_set_decoded_resolution_factor(opj_j2k_t *p_j2k,
9371                                                OPJ_UINT32 res_factor,
9372                                                opj_event_mgr_t * p_manager)
9373 {
9374         OPJ_UINT32 it_comp;
9375
9376         p_j2k->m_cp.m_specific_param.m_dec.m_reduce = res_factor;
9377
9378         if (p_j2k->m_private_image) {
9379                 if (p_j2k->m_private_image->comps) {
9380                         if (p_j2k->m_specific_param.m_decoder.m_default_tcp) {
9381                                 if (p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps) {
9382                                         for (it_comp = 0 ; it_comp < p_j2k->m_private_image->numcomps; it_comp++) {
9383                                                 OPJ_UINT32 max_res = p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps[it_comp].numresolutions;
9384                                                 if ( res_factor >= max_res){
9385                                                         opj_event_msg(p_manager, EVT_ERROR, "Resolution factor is greater than the maximum resolution in the component.\n");
9386                                                         return OPJ_FALSE;
9387                                                 }
9388                                                 p_j2k->m_private_image->comps[it_comp].factor = res_factor;
9389                                         }
9390                                         return OPJ_TRUE;
9391                                 }
9392                         }
9393                 }
9394         }
9395
9396         return OPJ_FALSE;
9397 }
9398
9399 OPJ_BOOL opj_j2k_encode(opj_j2k_t * p_j2k,
9400                         opj_stream_private_t *p_stream,
9401                         opj_event_mgr_t * p_manager )
9402 {
9403         OPJ_UINT32 i;
9404         OPJ_UINT32 l_nb_tiles;
9405         OPJ_UINT32 l_max_tile_size, l_current_tile_size;
9406         OPJ_BYTE * l_current_data;
9407
9408         /* preconditions */
9409         assert(p_j2k != 00);
9410         assert(p_stream != 00);
9411         assert(p_manager != 00);
9412
9413         l_current_data = (OPJ_BYTE*)opj_malloc(1000);
9414         if (! l_current_data) {
9415                 opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to encode all tiles\n");
9416                 return OPJ_FALSE;
9417         }
9418         l_max_tile_size = 1000;
9419
9420         l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw;
9421         for (i=0;i<l_nb_tiles;++i) {
9422                 if (! opj_j2k_pre_write_tile(p_j2k,i,p_stream,p_manager)) {
9423                         opj_free(l_current_data);
9424                         return OPJ_FALSE;
9425                 }
9426
9427                 l_current_tile_size = opj_tcd_get_encoded_tile_size(p_j2k->m_tcd);
9428                 if (l_current_tile_size > l_max_tile_size) {
9429                         OPJ_BYTE *l_new_current_data = (OPJ_BYTE *) opj_realloc(l_current_data, l_current_tile_size);
9430                         if (! l_new_current_data) {
9431                                 opj_free(l_current_data);
9432                                 opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to encode all tiles\n");
9433                                 return OPJ_FALSE;
9434                         }
9435                         l_current_data = l_new_current_data;
9436                         l_max_tile_size = l_current_tile_size;
9437                 }
9438
9439                 opj_j2k_get_tile_data(p_j2k->m_tcd,l_current_data);
9440
9441                 if (! opj_j2k_post_write_tile (p_j2k,l_current_data,l_current_tile_size,p_stream,p_manager)) {
9442                         return OPJ_FALSE;
9443                 }
9444         }
9445
9446         opj_free(l_current_data);
9447         return OPJ_TRUE;
9448 }
9449
9450 OPJ_BOOL opj_j2k_end_compress(  opj_j2k_t *p_j2k,
9451                                                         opj_stream_private_t *p_stream,
9452                                                         opj_event_mgr_t * p_manager)
9453 {
9454         /* customization of the encoding */
9455         opj_j2k_setup_end_compress(p_j2k);
9456
9457         if (! opj_j2k_exec (p_j2k, p_j2k->m_procedure_list, p_stream, p_manager))
9458         {
9459                 return OPJ_FALSE;
9460         }
9461
9462         return OPJ_TRUE;
9463 }
9464
9465 OPJ_BOOL opj_j2k_start_compress(opj_j2k_t *p_j2k,
9466                                                             opj_stream_private_t *p_stream,
9467                                                             opj_image_t * p_image,
9468                                                             opj_event_mgr_t * p_manager)
9469 {
9470         /* preconditions */
9471         assert(p_j2k != 00);
9472         assert(p_stream != 00);
9473         assert(p_manager != 00);
9474
9475         p_j2k->m_private_image = opj_image_create0();
9476         opj_copy_image_header(p_image, p_j2k->m_private_image);
9477
9478         /* TODO_MSD: Find a better way */
9479         if (p_image->comps) {
9480                 OPJ_UINT32 it_comp;
9481                 for (it_comp = 0 ; it_comp < p_image->numcomps; it_comp++) {
9482                         if (p_image->comps[it_comp].data) {
9483                                 p_j2k->m_private_image->comps[it_comp].data =p_image->comps[it_comp].data;
9484                                 p_image->comps[it_comp].data = NULL;
9485
9486                         }
9487                 }
9488         }
9489
9490         /* customization of the validation */
9491         opj_j2k_setup_encoding_validation (p_j2k);
9492
9493         /* validation of the parameters codec */
9494         if (! opj_j2k_exec(p_j2k,p_j2k->m_validation_list,p_stream,p_manager)) {
9495                 return OPJ_FALSE;
9496         }
9497
9498         /* customization of the encoding */
9499         opj_j2k_setup_header_writing(p_j2k);
9500
9501         /* write header */
9502         if (! opj_j2k_exec (p_j2k,p_j2k->m_procedure_list,p_stream,p_manager)) {
9503                 return OPJ_FALSE;
9504         }
9505
9506         return OPJ_TRUE;
9507 }
9508
9509 OPJ_BOOL opj_j2k_pre_write_tile (       opj_j2k_t * p_j2k,
9510                                                                 OPJ_UINT32 p_tile_index,
9511                                                                 opj_stream_private_t *p_stream,
9512                                                                 opj_event_mgr_t * p_manager )
9513 {
9514   (void)p_stream;
9515         if (p_tile_index != p_j2k->m_current_tile_number) {
9516                 opj_event_msg(p_manager, EVT_ERROR, "The given tile index does not match." );
9517                 return OPJ_FALSE;
9518         }
9519
9520         opj_event_msg(p_manager, EVT_INFO, "tile number %d / %d\n", p_j2k->m_current_tile_number + 1, p_j2k->m_cp.tw * p_j2k->m_cp.th);
9521
9522         p_j2k->m_specific_param.m_encoder.m_current_tile_part_number = 0;
9523         p_j2k->m_tcd->cur_totnum_tp = p_j2k->m_cp.tcps[p_tile_index].m_nb_tile_parts;
9524         p_j2k->m_specific_param.m_encoder.m_current_poc_tile_part_number = 0;
9525
9526         /* initialisation before tile encoding  */
9527         if (! opj_tcd_init_encode_tile(p_j2k->m_tcd, p_j2k->m_current_tile_number)) {
9528                 return OPJ_FALSE;
9529         }
9530
9531         return OPJ_TRUE;
9532 }
9533
9534 void opj_j2k_get_tile_data (opj_tcd_t * p_tcd, OPJ_BYTE * p_data)
9535 {
9536         OPJ_UINT32 i,j,k = 0;
9537         OPJ_UINT32 l_width,l_height,l_stride, l_offset_x,l_offset_y, l_image_width;
9538         opj_image_comp_t * l_img_comp = 00;
9539         opj_tcd_tilecomp_t * l_tilec = 00;
9540         opj_image_t * l_image = 00;
9541         OPJ_UINT32 l_size_comp, l_remaining;
9542         OPJ_INT32 * l_src_ptr;
9543         l_tilec = p_tcd->tcd_image->tiles->comps;
9544         l_image = p_tcd->image;
9545         l_img_comp = l_image->comps;
9546
9547         for (i=0;i<p_tcd->image->numcomps;++i) {
9548                 l_size_comp = l_img_comp->prec >> 3; /* (/8) */
9549                 l_remaining = l_img_comp->prec & 7;  /* (%8) */
9550                 if (l_remaining) {
9551                         ++l_size_comp;
9552                 }
9553
9554                 if (l_size_comp == 3) {
9555                         l_size_comp = 4;
9556                 }
9557
9558                 l_width = (l_tilec->x1 - l_tilec->x0);
9559                 l_height = (l_tilec->y1 - l_tilec->y0);
9560                 l_offset_x = opj_int_ceildiv(l_image->x0, l_img_comp->dx);
9561                 l_offset_y = opj_int_ceildiv(l_image->y0, l_img_comp->dy);
9562                 l_image_width = opj_int_ceildiv(l_image->x1 - l_image->x0, l_img_comp->dx);
9563                 l_stride = l_image_width - l_width;
9564                 l_src_ptr = l_img_comp->data + (l_tilec->x0 - l_offset_x) + (l_tilec->y0 - l_offset_y) * l_image_width;
9565
9566                 switch (l_size_comp) {
9567                         case 1:
9568                                 {
9569                                         OPJ_CHAR * l_dest_ptr = (OPJ_CHAR*) p_data;
9570                                         if (l_img_comp->sgnd) {
9571                                                 for     (j=0;j<l_height;++j) {
9572                                                         for (k=0;k<l_width;++k) {
9573                                                                 *(l_dest_ptr) = (OPJ_CHAR) (*l_src_ptr);
9574                                                                 ++l_dest_ptr;
9575                                                                 ++l_src_ptr;
9576                                                         }
9577                                                         l_src_ptr += l_stride;
9578                                                 }
9579                                         }
9580                                         else {
9581                                                 for (j=0;j<l_height;++j) {
9582                                                         for (k=0;k<l_width;++k) {
9583                                                                 *(l_dest_ptr) = (*l_src_ptr)&0xff;
9584                                                                 ++l_dest_ptr;
9585                                                                 ++l_src_ptr;
9586                                                         }
9587                                                         l_src_ptr += l_stride;
9588                                                 }
9589                                         }
9590
9591                                         p_data = (OPJ_BYTE*) l_dest_ptr;
9592                                 }
9593                                 break;
9594                         case 2:
9595                                 {
9596                                         OPJ_INT16 * l_dest_ptr = (OPJ_INT16 *) p_data;
9597                                         if (l_img_comp->sgnd) {
9598                                                 for (j=0;j<l_height;++j) {
9599                                                         for (k=0;k<l_width;++k) {
9600                                                                 *(l_dest_ptr++) = (OPJ_INT16) (*(l_src_ptr++));
9601                                                         }
9602                                                         l_src_ptr += l_stride;
9603                                                 }
9604                                         }
9605                                         else {
9606                                                 for (j=0;j<l_height;++j) {
9607                                                         for (k=0;k<l_width;++k) {
9608                                                                 *(l_dest_ptr++) = (*(l_src_ptr++))&0xffff;
9609                                                         }
9610                                                         l_src_ptr += l_stride;
9611                                                 }
9612                                         }
9613
9614                                         p_data = (OPJ_BYTE*) l_dest_ptr;
9615                                 }
9616                                 break;
9617                         case 4:
9618                                 {
9619                                         OPJ_INT32 * l_dest_ptr = (OPJ_INT32 *) p_data;
9620                                         for (j=0;j<l_height;++j) {
9621                                                 for (k=0;k<l_width;++k) {
9622                                                         *(l_dest_ptr++) = *(l_src_ptr++);
9623                                                 }
9624                                                 l_src_ptr += l_stride;
9625                                         }
9626
9627                                         p_data = (OPJ_BYTE*) l_dest_ptr;
9628                                 }
9629                                 break;
9630                 }
9631
9632                 ++l_img_comp;
9633                 ++l_tilec;
9634         }
9635 }
9636
9637 OPJ_BOOL opj_j2k_post_write_tile (      opj_j2k_t * p_j2k,
9638                                                                 OPJ_BYTE * p_data,
9639                                                                 OPJ_UINT32 p_data_size,
9640                                                                 opj_stream_private_t *p_stream,
9641                                                                 opj_event_mgr_t * p_manager )
9642 {
9643         opj_tcd_t * l_tcd = 00;
9644         OPJ_UINT32 l_nb_bytes_written;
9645         OPJ_BYTE * l_current_data = 00;
9646         OPJ_UINT32 l_tile_size = 0;
9647         OPJ_UINT32 l_available_data;
9648
9649         /* preconditions */
9650         assert(p_j2k->m_specific_param.m_encoder.m_encoded_tile_data);
9651
9652         l_tcd = p_j2k->m_tcd;
9653         
9654         l_tile_size = p_j2k->m_specific_param.m_encoder.m_encoded_tile_size;
9655         l_available_data = l_tile_size;
9656         l_current_data = p_j2k->m_specific_param.m_encoder.m_encoded_tile_data;
9657
9658         if (! opj_tcd_copy_tile_data(l_tcd,p_data,p_data_size)) {
9659                 opj_event_msg(p_manager, EVT_ERROR, "Size mismatch between tile data and sent data." );
9660                 return OPJ_FALSE;
9661         }
9662
9663         l_nb_bytes_written = 0;
9664         if (! opj_j2k_write_first_tile_part(p_j2k,l_current_data,&l_nb_bytes_written,l_available_data,p_stream,p_manager)) {
9665                 return OPJ_FALSE;
9666         }
9667         l_current_data += l_nb_bytes_written;
9668         l_available_data -= l_nb_bytes_written;
9669
9670         l_nb_bytes_written = 0;
9671         if (! opj_j2k_write_all_tile_parts(p_j2k,l_current_data,&l_nb_bytes_written,l_available_data,p_stream,p_manager)) {
9672                 return OPJ_FALSE;
9673         }
9674
9675         l_available_data -= l_nb_bytes_written;
9676         l_nb_bytes_written = l_tile_size - l_available_data;
9677
9678         if ( opj_stream_write_data(     p_stream,
9679                                                                 p_j2k->m_specific_param.m_encoder.m_encoded_tile_data,
9680                                                                 l_nb_bytes_written,p_manager) != l_nb_bytes_written) {
9681                 return OPJ_FALSE;
9682         }
9683
9684         ++p_j2k->m_current_tile_number;
9685
9686         return OPJ_TRUE;
9687 }
9688
9689 void opj_j2k_setup_end_compress (opj_j2k_t *p_j2k)
9690 {
9691         /* preconditions */
9692         assert(p_j2k != 00);
9693
9694         /* DEVELOPER CORNER, insert your custom procedures */
9695         opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)opj_j2k_write_eoc );
9696
9697         if (p_j2k->m_cp.m_specific_param.m_enc.m_cinema) {
9698                 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)opj_j2k_write_updated_tlm);
9699         }
9700
9701         opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)opj_j2k_write_epc );
9702         opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)opj_j2k_end_encoding );
9703         opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)opj_j2k_destroy_header_memory);
9704 }
9705
9706 void opj_j2k_setup_encoding_validation (opj_j2k_t *p_j2k)
9707 {
9708         /* preconditions */
9709         assert(p_j2k != 00);
9710
9711         opj_procedure_list_add_procedure(p_j2k->m_validation_list, (opj_procedure)opj_j2k_build_encoder);
9712         opj_procedure_list_add_procedure(p_j2k->m_validation_list, (opj_procedure)opj_j2k_encoding_validation);
9713
9714         /* DEVELOPER CORNER, add your custom validation procedure */
9715         opj_procedure_list_add_procedure(p_j2k->m_validation_list, (opj_procedure)opj_j2k_mct_validation);
9716 }
9717
9718 void opj_j2k_setup_header_writing (opj_j2k_t *p_j2k)
9719 {
9720         /* preconditions */
9721         assert(p_j2k != 00);
9722
9723         opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)opj_j2k_init_info );
9724         opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)opj_j2k_write_soc );
9725         opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)opj_j2k_write_siz );
9726         opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)opj_j2k_write_cod );
9727         opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)opj_j2k_write_qcd );
9728
9729         if (p_j2k->m_cp.m_specific_param.m_enc.m_cinema) {
9730                 /* No need for COC or QCC, QCD and COD are used
9731                 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)opj_j2k_write_all_coc );
9732                 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)opj_j2k_write_all_qcc );
9733                 */
9734                 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)opj_j2k_write_tlm );
9735
9736                 if (p_j2k->m_cp.m_specific_param.m_enc.m_cinema == OPJ_CINEMA4K_24) {
9737                         opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)opj_j2k_write_poc );
9738                 }
9739         }
9740
9741         opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)opj_j2k_write_regions);
9742
9743         if (p_j2k->m_cp.comment != 00)  {
9744                 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)opj_j2k_write_com);
9745         }
9746
9747         /* DEVELOPER CORNER, insert your custom procedures */
9748         if (p_j2k->m_cp.rsiz & OPJ_MCT) {
9749                 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)opj_j2k_write_mct_data_group );
9750         }
9751         /* End of Developer Corner */
9752
9753         if (p_j2k->cstr_index) {
9754                 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)opj_j2k_get_end_header );
9755         }
9756
9757         opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)opj_j2k_create_tcd);
9758         opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)opj_j2k_update_rates);
9759 }
9760
9761 OPJ_BOOL opj_j2k_write_first_tile_part (opj_j2k_t *p_j2k,
9762                                                                         OPJ_BYTE * p_data,
9763                                                                         OPJ_UINT32 * p_data_written,
9764                                                                         OPJ_UINT32 p_total_data_size,
9765                                                                         opj_stream_private_t *p_stream,
9766                                                                         struct opj_event_mgr * p_manager )
9767 {
9768 //        OPJ_UINT32 compno;
9769         OPJ_UINT32 l_nb_bytes_written = 0;
9770         OPJ_UINT32 l_current_nb_bytes_written;
9771         OPJ_BYTE * l_begin_data = 00;
9772
9773         opj_tcd_t * l_tcd = 00;
9774         opj_cp_t * l_cp = 00;
9775
9776         l_tcd = p_j2k->m_tcd;
9777         l_cp = &(p_j2k->m_cp);
9778
9779         l_tcd->cur_pino = 0;
9780
9781         /*Get number of tile parts*/
9782         p_j2k->m_specific_param.m_encoder.m_current_poc_tile_part_number = 0;
9783
9784         /* INDEX >> */
9785         /* << INDEX */
9786
9787         l_current_nb_bytes_written = 0;
9788         l_begin_data = p_data;
9789         if (! opj_j2k_write_sot(p_j2k,p_data,&l_current_nb_bytes_written,p_stream,p_manager))
9790         {
9791                 return OPJ_FALSE;
9792         }
9793
9794         l_nb_bytes_written += l_current_nb_bytes_written;
9795         p_data += l_current_nb_bytes_written;
9796         p_total_data_size -= l_current_nb_bytes_written;
9797
9798         if (l_cp->m_specific_param.m_enc.m_cinema == 0) {
9799 //                for (compno = 1; compno < p_j2k->m_private_image->numcomps; compno++) {
9800 //                        l_current_nb_bytes_written = 0;
9801 //                        opj_j2k_write_coc_in_memory(p_j2k,compno,p_data,&l_current_nb_bytes_written,p_manager);
9802 //                        l_nb_bytes_written += l_current_nb_bytes_written;
9803 //                        p_data += l_current_nb_bytes_written;
9804 //                        p_total_data_size -= l_current_nb_bytes_written;
9805
9806 //                        l_current_nb_bytes_written = 0;
9807 //                        opj_j2k_write_qcc_in_memory(p_j2k,compno,p_data,&l_current_nb_bytes_written,p_manager);
9808 //                        l_nb_bytes_written += l_current_nb_bytes_written;
9809 //                        p_data += l_current_nb_bytes_written;
9810 //                        p_total_data_size -= l_current_nb_bytes_written;
9811 //                }
9812
9813                 if (l_cp->tcps[p_j2k->m_current_tile_number].numpocs) {
9814                         l_current_nb_bytes_written = 0;
9815                         opj_j2k_write_poc_in_memory(p_j2k,p_data,&l_current_nb_bytes_written,p_manager);
9816                         l_nb_bytes_written += l_current_nb_bytes_written;
9817                         p_data += l_current_nb_bytes_written;
9818                         p_total_data_size -= l_current_nb_bytes_written;
9819                 }
9820         }
9821
9822         l_current_nb_bytes_written = 0;
9823         if (! opj_j2k_write_sod(p_j2k,l_tcd,p_data,&l_current_nb_bytes_written,p_total_data_size,p_stream,p_manager)) {
9824                 return OPJ_FALSE;
9825         }
9826
9827         l_nb_bytes_written += l_current_nb_bytes_written;
9828         * p_data_written = l_nb_bytes_written;
9829
9830         /* Writing Psot in SOT marker */
9831         opj_write_bytes(l_begin_data + 6,l_nb_bytes_written,4);                                 /* PSOT */
9832
9833         if (l_cp->m_specific_param.m_enc.m_cinema){
9834                 opj_j2k_update_tlm(p_j2k,l_nb_bytes_written);
9835         }
9836
9837         return OPJ_TRUE;
9838 }
9839
9840 OPJ_BOOL opj_j2k_write_all_tile_parts(  opj_j2k_t *p_j2k,
9841                                                                         OPJ_BYTE * p_data,
9842                                                                         OPJ_UINT32 * p_data_written,
9843                                                                         OPJ_UINT32 p_total_data_size,
9844                                                                         opj_stream_private_t *p_stream,
9845                                                                         struct opj_event_mgr * p_manager
9846                                                                 )
9847 {
9848         OPJ_UINT32 tilepartno=0;
9849         OPJ_UINT32 l_nb_bytes_written = 0;
9850         OPJ_UINT32 l_current_nb_bytes_written;
9851         OPJ_UINT32 l_part_tile_size;
9852         OPJ_UINT32 tot_num_tp;
9853         OPJ_UINT32 pino;
9854
9855         OPJ_BYTE * l_begin_data;
9856         opj_tcp_t *l_tcp = 00;
9857         opj_tcd_t * l_tcd = 00;
9858         opj_cp_t * l_cp = 00;
9859
9860         l_tcd = p_j2k->m_tcd;
9861         l_cp = &(p_j2k->m_cp);
9862         l_tcp = l_cp->tcps + p_j2k->m_current_tile_number;
9863
9864         /*Get number of tile parts*/
9865         tot_num_tp = opj_j2k_get_num_tp(l_cp,0,p_j2k->m_current_tile_number);
9866
9867         /* start writing remaining tile parts */
9868         ++p_j2k->m_specific_param.m_encoder.m_current_tile_part_number;
9869         for (tilepartno = 1; tilepartno < tot_num_tp ; ++tilepartno) {
9870                 p_j2k->m_specific_param.m_encoder.m_current_poc_tile_part_number = tilepartno;
9871                 l_current_nb_bytes_written = 0;
9872                 l_part_tile_size = 0;
9873                 l_begin_data = p_data;
9874
9875                 if (! opj_j2k_write_sot(p_j2k,p_data,&l_current_nb_bytes_written,p_stream,p_manager)) {
9876                         return OPJ_FALSE;
9877                 }
9878
9879                 l_nb_bytes_written += l_current_nb_bytes_written;
9880                 p_data += l_current_nb_bytes_written;
9881                 p_total_data_size -= l_current_nb_bytes_written;
9882                 l_part_tile_size += l_current_nb_bytes_written;
9883
9884                 l_current_nb_bytes_written = 0;
9885                 if (! opj_j2k_write_sod(p_j2k,l_tcd,p_data,&l_current_nb_bytes_written,p_total_data_size,p_stream,p_manager)) {
9886                         return OPJ_FALSE;
9887                 }
9888
9889                 p_data += l_current_nb_bytes_written;
9890                 l_nb_bytes_written += l_current_nb_bytes_written;
9891                 p_total_data_size -= l_current_nb_bytes_written;
9892                 l_part_tile_size += l_current_nb_bytes_written;
9893
9894                 /* Writing Psot in SOT marker */
9895                 opj_write_bytes(l_begin_data + 6,l_part_tile_size,4);                                   /* PSOT */
9896
9897                 if (l_cp->m_specific_param.m_enc.m_cinema) {
9898                         opj_j2k_update_tlm(p_j2k,l_part_tile_size);
9899                 }
9900
9901                 ++p_j2k->m_specific_param.m_encoder.m_current_tile_part_number;
9902         }
9903
9904         for (pino = 1; pino <= l_tcp->numpocs; ++pino) {
9905                 l_tcd->cur_pino = pino;
9906
9907                 /*Get number of tile parts*/
9908                 tot_num_tp = opj_j2k_get_num_tp(l_cp,pino,p_j2k->m_current_tile_number);
9909                 for (tilepartno = 0; tilepartno < tot_num_tp ; ++tilepartno) {
9910                         p_j2k->m_specific_param.m_encoder.m_current_poc_tile_part_number = tilepartno;
9911                         l_current_nb_bytes_written = 0;
9912                         l_part_tile_size = 0;
9913                         l_begin_data = p_data;
9914
9915                         if (! opj_j2k_write_sot(p_j2k,p_data,&l_current_nb_bytes_written,p_stream,p_manager)) {
9916                                 return OPJ_FALSE;
9917                         }
9918
9919                         l_nb_bytes_written += l_current_nb_bytes_written;
9920                         p_data += l_current_nb_bytes_written;
9921                         p_total_data_size -= l_current_nb_bytes_written;
9922                         l_part_tile_size += l_current_nb_bytes_written;
9923
9924                         l_current_nb_bytes_written = 0;
9925
9926                         if (! opj_j2k_write_sod(p_j2k,l_tcd,p_data,&l_current_nb_bytes_written,p_total_data_size,p_stream,p_manager)) {
9927                                 return OPJ_FALSE;
9928                         }
9929
9930                         l_nb_bytes_written += l_current_nb_bytes_written;
9931                         p_data += l_current_nb_bytes_written;
9932                         p_total_data_size -= l_current_nb_bytes_written;
9933                         l_part_tile_size += l_current_nb_bytes_written;
9934
9935                         /* Writing Psot in SOT marker */
9936                         opj_write_bytes(l_begin_data + 6,l_part_tile_size,4);                                   /* PSOT */
9937
9938                         if (l_cp->m_specific_param.m_enc.m_cinema) {
9939                                 opj_j2k_update_tlm(p_j2k,l_part_tile_size);
9940                         }
9941
9942                         ++p_j2k->m_specific_param.m_encoder.m_current_tile_part_number;
9943                 }
9944         }
9945
9946         *p_data_written = l_nb_bytes_written;
9947
9948         return OPJ_TRUE;
9949 }
9950
9951 OPJ_BOOL opj_j2k_write_updated_tlm( opj_j2k_t *p_j2k,
9952                                                                     struct opj_stream_private *p_stream,
9953                                                                     struct opj_event_mgr * p_manager )
9954 {
9955         OPJ_UINT32 l_tlm_size;
9956         OPJ_OFF_T l_tlm_position, l_current_position;
9957
9958         /* preconditions */
9959         assert(p_j2k != 00);
9960         assert(p_manager != 00);
9961         assert(p_stream != 00);
9962
9963         l_tlm_size = 5 * p_j2k->m_specific_param.m_encoder.m_total_tile_parts;
9964         l_tlm_position = 6 + p_j2k->m_specific_param.m_encoder.m_tlm_start;
9965         l_current_position = opj_stream_tell(p_stream);
9966
9967         if (! opj_stream_seek(p_stream,l_tlm_position,p_manager)) {
9968                 return OPJ_FALSE;
9969         }
9970
9971         if (opj_stream_write_data(p_stream,p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer,l_tlm_size,p_manager) != l_tlm_size) {
9972                 return OPJ_FALSE;
9973         }
9974
9975         if (! opj_stream_seek(p_stream,l_current_position,p_manager)) {
9976                 return OPJ_FALSE;
9977         }
9978
9979         return OPJ_TRUE;
9980 }
9981
9982 OPJ_BOOL opj_j2k_end_encoding(  opj_j2k_t *p_j2k,
9983                                                         struct opj_stream_private *p_stream,
9984                                                         struct opj_event_mgr * p_manager )
9985 {
9986         /* preconditions */
9987         assert(p_j2k != 00);
9988         assert(p_manager != 00);
9989         assert(p_stream != 00);
9990
9991         opj_tcd_destroy(p_j2k->m_tcd);
9992         p_j2k->m_tcd = 00;
9993
9994         if (p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer) {
9995                 opj_free(p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer);
9996                 p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer = 0;
9997                 p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current = 0;
9998         }
9999
10000         if (p_j2k->m_specific_param.m_encoder.m_encoded_tile_data) {
10001                 opj_free(p_j2k->m_specific_param.m_encoder.m_encoded_tile_data);
10002                 p_j2k->m_specific_param.m_encoder.m_encoded_tile_data = 0;
10003         }
10004
10005         p_j2k->m_specific_param.m_encoder.m_encoded_tile_size = 0;
10006
10007         return OPJ_TRUE;
10008 }
10009
10010 /**
10011  * Destroys the memory associated with the decoding of headers.
10012  */
10013 static OPJ_BOOL opj_j2k_destroy_header_memory ( opj_j2k_t * p_j2k,
10014                                                 opj_stream_private_t *p_stream,
10015                                                 opj_event_mgr_t * p_manager
10016                                                 )
10017 {
10018         /* preconditions */
10019         assert(p_j2k != 00);
10020         assert(p_stream != 00);
10021         assert(p_manager != 00);
10022
10023         if (p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
10024                 opj_free(p_j2k->m_specific_param.m_encoder.m_header_tile_data);
10025                 p_j2k->m_specific_param.m_encoder.m_header_tile_data = 0;
10026         }
10027
10028         p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = 0;
10029
10030         return OPJ_TRUE;
10031 }
10032
10033 OPJ_BOOL opj_j2k_init_info(     opj_j2k_t *p_j2k,
10034                                                 struct opj_stream_private *p_stream,
10035                                                 struct opj_event_mgr * p_manager )
10036 {
10037         opj_codestream_info_t * l_cstr_info = 00;
10038
10039         /* preconditions */
10040         assert(p_j2k != 00);
10041         assert(p_manager != 00);
10042         assert(p_stream != 00);
10043   (void)l_cstr_info;
10044
10045         /* TODO mergeV2: check this part which use cstr_info */
10046         /*l_cstr_info = p_j2k->cstr_info;
10047
10048         if (l_cstr_info)  {
10049                 OPJ_UINT32 compno;
10050                 l_cstr_info->tile = (opj_tile_info_t *) opj_malloc(p_j2k->m_cp.tw * p_j2k->m_cp.th * sizeof(opj_tile_info_t));
10051
10052                 l_cstr_info->image_w = p_j2k->m_image->x1 - p_j2k->m_image->x0;
10053                 l_cstr_info->image_h = p_j2k->m_image->y1 - p_j2k->m_image->y0;
10054
10055                 l_cstr_info->prog = (&p_j2k->m_cp.tcps[0])->prg;
10056
10057                 l_cstr_info->tw = p_j2k->m_cp.tw;
10058                 l_cstr_info->th = p_j2k->m_cp.th;
10059
10060                 l_cstr_info->tile_x = p_j2k->m_cp.tdx;*/        /* new version parser */
10061                 /*l_cstr_info->tile_y = p_j2k->m_cp.tdy;*/      /* new version parser */
10062                 /*l_cstr_info->tile_Ox = p_j2k->m_cp.tx0;*/     /* new version parser */
10063                 /*l_cstr_info->tile_Oy = p_j2k->m_cp.ty0;*/     /* new version parser */
10064
10065                 /*l_cstr_info->numcomps = p_j2k->m_image->numcomps;
10066
10067                 l_cstr_info->numlayers = (&p_j2k->m_cp.tcps[0])->numlayers;
10068
10069                 l_cstr_info->numdecompos = (OPJ_INT32*) opj_malloc(p_j2k->m_image->numcomps * sizeof(OPJ_INT32));
10070
10071                 for (compno=0; compno < p_j2k->m_image->numcomps; compno++) {
10072                         l_cstr_info->numdecompos[compno] = (&p_j2k->m_cp.tcps[0])->tccps->numresolutions - 1;
10073                 }
10074
10075                 l_cstr_info->D_max = 0.0;       */      /* ADD Marcela */
10076
10077                 /*l_cstr_info->main_head_start = opj_stream_tell(p_stream);*/ /* position of SOC */
10078
10079                 /*l_cstr_info->maxmarknum = 100;
10080                 l_cstr_info->marker = (opj_marker_info_t *) opj_malloc(l_cstr_info->maxmarknum * sizeof(opj_marker_info_t));
10081                 l_cstr_info->marknum = 0;
10082         }*/
10083
10084         return opj_j2k_calculate_tp(p_j2k,&(p_j2k->m_cp),&p_j2k->m_specific_param.m_encoder.m_total_tile_parts,p_j2k->m_private_image,p_manager);
10085 }
10086
10087 /**
10088  * Creates a tile-coder decoder.
10089  *
10090  * @param       p_stream                the stream to write data to.
10091  * @param       p_j2k                   J2K codec.
10092  * @param       p_manager               the user event manager.
10093 */
10094 static OPJ_BOOL opj_j2k_create_tcd(     opj_j2k_t *p_j2k,
10095                                                                     opj_stream_private_t *p_stream,
10096                                                                     opj_event_mgr_t * p_manager
10097                                     )
10098 {
10099         /* preconditions */
10100         assert(p_j2k != 00);
10101         assert(p_manager != 00);
10102         assert(p_stream != 00);
10103
10104         p_j2k->m_tcd = opj_tcd_create(OPJ_FALSE);
10105
10106         if (! p_j2k->m_tcd) {
10107                 opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to create Tile Coder\n");
10108                 return OPJ_FALSE;
10109         }
10110
10111         if (!opj_tcd_init(p_j2k->m_tcd,p_j2k->m_private_image,&p_j2k->m_cp)) {
10112                 opj_tcd_destroy(p_j2k->m_tcd);
10113                 p_j2k->m_tcd = 00;
10114                 return OPJ_FALSE;
10115         }
10116
10117         return OPJ_TRUE;
10118 }
10119
10120 OPJ_BOOL opj_j2k_write_tile (opj_j2k_t * p_j2k,
10121                                                  OPJ_UINT32 p_tile_index,
10122                                                  OPJ_BYTE * p_data,
10123                                                  OPJ_UINT32 p_data_size,
10124                                                  opj_stream_private_t *p_stream,
10125                                                  opj_event_mgr_t * p_manager )
10126 {
10127         if (! opj_j2k_pre_write_tile(p_j2k,p_tile_index,p_stream,p_manager)) {
10128                 opj_event_msg(p_manager, EVT_ERROR, "Error while opj_j2k_pre_write_tile with tile index = %d\n", p_tile_index);
10129                 return OPJ_FALSE;
10130         }
10131         else {
10132                 if (! opj_j2k_post_write_tile(p_j2k,p_data,p_data_size,p_stream,p_manager)) {
10133                         opj_event_msg(p_manager, EVT_ERROR, "Error while opj_j2k_post_write_tile with tile index = %d\n", p_tile_index);
10134                         return OPJ_FALSE;
10135                 }
10136         }
10137
10138         return OPJ_TRUE;
10139 }