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