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