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