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