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