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