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