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