ae1522bf2708fb8731c5b150bf0341ceeec5fbe7
[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, 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 void j2k_setup_header_reading (opj_j2k_v2_t *p_j2k);
48
49 /**
50  * The read header procedure.
51  */
52 opj_bool j2k_read_header_procedure(
53                                                             opj_j2k_v2_t *p_j2k,
54                                                                 struct opj_stream_private *p_stream,
55                                                                 struct opj_event_mgr * p_manager);
56
57 /**
58  * The default encoding validation procedure without any extension.
59  *
60  * @param       p_j2k                   the jpeg2000 codec to validate.
61  * @param       p_stream                                the input stream to validate.
62  * @param       p_manager               the user event manager.
63  *
64  * @return true if the parameters are correct.
65  */
66 opj_bool j2k_encoding_validation (      opj_j2k_v2_t * p_j2k,
67                                                                         opj_stream_private_t *p_stream,
68                                                                         opj_event_mgr_t * p_manager );
69
70 /**
71  * The default decoding validation procedure without any extension.
72  *
73  * @param       p_j2k                   the jpeg2000 codec to validate.
74  * @param       p_stream                                the input stream to validate.
75  * @param       p_manager               the user event manager.
76  *
77  * @return true if the parameters are correct.
78  */
79 opj_bool j2k_decoding_validation (
80                                                                 opj_j2k_v2_t * p_j2k,
81                                                                 opj_stream_private_t *p_stream,
82                                                                 opj_event_mgr_t * p_manager
83                                                         );
84
85 /**
86  * Sets up the validation ,i.e. adds the procedures to lauch to make sure the codec parameters
87  * are valid. Developpers wanting to extend the library can add their own validation procedures.
88  */
89 static void j2k_setup_encoding_validation (opj_j2k_v2_t *p_j2k);
90
91 /**
92  * Sets up the validation ,i.e. adds the procedures to lauch to make sure the codec parameters
93  * are valid. Developpers wanting to extend the library can add their own validation procedures.
94  */
95 static void j2k_setup_decoding_validation (opj_j2k_v2_t *p_j2k);
96
97 /**
98  * Sets up the validation ,i.e. adds the procedures to lauch to make sure the codec parameters
99  * are valid. Developpers wanting to extend the library can add their own validation procedures.
100  */
101 static void j2k_setup_end_compress (opj_j2k_v2_t *p_j2k);
102
103 /**
104  * The mct encoding validation procedure.
105  *
106  * @param       p_j2k                   the jpeg2000 codec to validate.
107  * @param       p_stream                                the input stream to validate.
108  * @param       p_manager               the user event manager.
109  *
110  * @return true if the parameters are correct.
111  */
112 opj_bool j2k_mct_validation (   opj_j2k_v2_t * p_j2k,
113                                                                 opj_stream_private_t *p_stream,
114                                                                 opj_event_mgr_t * p_manager );
115
116 /**
117  * Builds the tcd decoder to use to decode tile.
118  */
119 opj_bool j2k_build_decoder (opj_j2k_v2_t * p_j2k,
120                                                         opj_stream_private_t *p_stream,
121                                                         opj_event_mgr_t * p_manager );
122 /**
123  * Builds the tcd encoder to use to encode tile.
124  */
125 opj_bool j2k_build_encoder (opj_j2k_v2_t * p_j2k,
126                                                         opj_stream_private_t *p_stream,
127                                                         opj_event_mgr_t * p_manager );
128
129 /**
130  * Creates a tile-coder decoder.
131  *
132  * @param       p_stream                                the stream to write data to.
133  * @param       p_j2k                           J2K codec.
134  * @param       p_manager               the user event manager.
135 */
136 static opj_bool j2k_create_tcd( opj_j2k_v2_t *p_j2k,
137                                                                 struct opj_stream_private *p_stream,
138                                                                 struct opj_event_mgr * p_manager );
139
140 /**
141  * Excutes the given procedures on the given codec.
142  *
143  * @param       p_procedure_list        the list of procedures to execute
144  * @param       p_j2k                                   the jpeg2000 codec to execute the procedures on.
145  * @param       p_stream                                        the stream to execute the procedures on.
146  * @param       p_manager                       the user manager.
147  *
148  * @return      true                            if all the procedures were successfully executed.
149  */
150 static opj_bool j2k_exec (
151                                         opj_j2k_v2_t * p_j2k,
152                                         opj_procedure_list_t * p_procedure_list,
153                                         opj_stream_private_t *p_stream,
154                                         opj_event_mgr_t * p_manager
155                                   );
156
157 /**
158  * Updates the rates of the tcp.
159  *
160  * @param       p_stream                                the stream to write data to.
161  * @param       p_j2k                           J2K codec.
162  * @param       p_manager               the user event manager.
163 */
164 static opj_bool j2k_update_rates(       opj_j2k_v2_t *p_j2k,
165                                                                         struct opj_stream_private *p_stream,
166                                                                         struct opj_event_mgr * p_manager );
167
168 /**
169  * Copies the decoding tile parameters onto all the tile parameters.
170  * Creates also the tile decoder.
171  */
172 opj_bool j2k_copy_default_tcp_and_create_tcd (  opj_j2k_v2_t * p_j2k,
173                                                                                                 opj_stream_private_t *p_stream,
174                                                                                                 opj_event_mgr_t * p_manager );
175
176 /**
177  * Destroys the memory associated with the decoding of headers.
178  */
179 opj_bool j2k_destroy_header_memory (opj_j2k_v2_t * p_j2k,
180                                                                         opj_stream_private_t *p_stream,
181                                                                         opj_event_mgr_t * p_manager );
182
183 /**
184  * Reads the lookup table containing all the marker, status and action, and returns the handler associated
185  * with the marker value.
186  * @param       p_id            Marker value to look up
187  *
188  * @return      the handler associated with the id.
189 */
190 static const struct opj_dec_memory_marker_handler * j2k_get_marker_handler (OPJ_UINT32 p_id);
191
192 /**
193  * Destroys a tile coding parameter structure.
194  *
195  * @param       p_tcp           the tile coding parameter to destroy.
196  */
197 static void j2k_tcp_destroy (opj_tcp_v2_t *p_tcp);
198
199 /**
200  * Destroys the data inside a tile coding parameter structure.
201  *
202  * @param       p_tcp           the tile coding parameter which contain data to destroy.
203  */
204 static void j2k_tcp_data_destroy (opj_tcp_v2_t *p_tcp);
205
206 /**
207  * Destroys a coding parameter structure.
208  *
209  * @param       p_cp            the coding parameter to destroy.
210  */
211 static void j2k_cp_destroy (opj_cp_v2_t *p_cp);
212
213
214 /**
215  * Writes a SPCod or SPCoc element, i.e. the coding style of a given component of a tile.
216  *
217  * @param       p_comp_no       the component number to output.
218  * @param       p_stream                        the stream to write data to.
219  * @param       p_j2k                   J2K codec.
220  * @param       p_manager       the user event manager.
221  *
222 */
223 static opj_bool j2k_write_SPCod_SPCoc(  opj_j2k_v2_t *p_j2k,
224                                                                                 OPJ_UINT32 p_tile_no,
225                                                                                 OPJ_UINT32 p_comp_no,
226                                                                                 OPJ_BYTE * p_data,
227                                                                                 OPJ_UINT32 * p_header_size,
228                                                                                 struct opj_event_mgr * p_manager );
229
230 /**
231  * Gets the size taken by writing a SPCod or SPCoc for the given tile and component.
232  *
233  * @param       p_tile_no               the tile index.
234  * @param       p_comp_no               the component being outputted.
235  * @param       p_j2k                   the J2K codec.
236  *
237  * @return      the number of bytes taken by the SPCod element.
238  */
239 static OPJ_UINT32 j2k_get_SPCod_SPCoc_size (opj_j2k_v2_t *p_j2k,
240                                                                                         OPJ_UINT32 p_tile_no,
241                                                                                         OPJ_UINT32 p_comp_no );
242
243 /**
244  * Reads a SPCod or SPCoc element, i.e. the coding style of a given component of a tile.
245  * @param       p_header_data   the data contained in the COM box.
246  * @param       p_j2k                   the jpeg2000 codec.
247  * @param       p_header_size   the size of the data contained in the COM marker.
248  * @param       p_manager               the user event manager.
249 */
250 static opj_bool j2k_read_SPCod_SPCoc(
251                                                         opj_j2k_v2_t *p_j2k,
252                                                         OPJ_UINT32 compno,
253                                                         OPJ_BYTE * p_header_data,
254                                                         OPJ_UINT32 * p_header_size,
255                                                         struct opj_event_mgr * p_manager
256                                                         );
257
258 /**
259  * Gets the size taken by writing SQcd or SQcc element, i.e. the quantization values of a band in the QCD or QCC.
260  *
261  * @param       p_tile_no               the tile index.
262  * @param       p_comp_no               the component being outputted.
263  * @param       p_j2k                   the J2K codec.
264  *
265  * @return      the number of bytes taken by the SPCod element.
266  */
267 static OPJ_UINT32 j2k_get_SQcd_SQcc_size (      opj_j2k_v2_t *p_j2k,
268                                                                                 OPJ_UINT32 p_tile_no,
269                                                                                 OPJ_UINT32 p_comp_no );
270
271 /**
272  * Writes a SQcd or SQcc element, i.e. the quantization values of a band in the QCD or QCC.
273  *
274  * @param       p_tile_no               the tile to output.
275  * @param       p_comp_no               the component number to output.
276  * @param       p_data                  the data buffer.
277  * @param       p_header_size   pointer to the size of the data buffer, it is changed by the function.
278  * @param       p_j2k                   J2K codec.
279  * @param       p_manager               the user event manager.
280  *
281 */
282 static opj_bool j2k_write_SQcd_SQcc(opj_j2k_v2_t *p_j2k,
283                                                                         OPJ_UINT32 p_tile_no,
284                                                                         OPJ_UINT32 p_comp_no,
285                                                                         OPJ_BYTE * p_data,
286                                                                         OPJ_UINT32 * p_header_size,
287                                                                         struct opj_event_mgr * p_manager
288                                                                         );
289
290 /**
291  * Updates the Tile Length Marker.
292  */
293 static void j2k_update_tlm ( opj_j2k_v2_t * p_j2k, OPJ_UINT32 p_tile_part_size);
294
295 /**
296  * Reads a SQcd or SQcc element, i.e. the quantization values of a band in the QCD or QCC.
297  *
298  * @param       p_tile_no               the tile to output.
299  * @param       p_comp_no               the component number to output.
300  * @param       p_data                  the data buffer.
301  * @param       p_header_size   pointer to the size of the data buffer, it is changed by the function.
302  * @param       p_j2k                           J2K codec.
303  * @param       p_manager               the user event manager.
304  *
305 */
306 static opj_bool j2k_read_SQcd_SQcc(
307                                                         opj_j2k_v2_t *p_j2k,
308                                                         OPJ_UINT32 compno,
309                                                         OPJ_BYTE * p_header_data,
310                                                         OPJ_UINT32 * p_header_size,
311                                                         struct opj_event_mgr * p_manager
312                                         );
313
314 /**
315  * Copies the tile component parameters of all the component from the first tile component.
316  *
317  * @param               p_j2k           the J2k codec.
318  */
319 static void j2k_copy_tile_component_parameters(
320                                                         opj_j2k_v2_t *p_j2k
321                                                         );
322
323 /**
324  * Copies the tile quantization parameters of all the component from the first tile component.
325  *
326  * @param               p_j2k           the J2k codec.
327  */
328 static void j2k_copy_tile_quantization_parameters(
329                                                         opj_j2k_v2_t *p_j2k
330                                                         );
331
332 /**
333  * Reads the tiles.
334  */
335 opj_bool j2k_decode_tiles (     opj_j2k_v2_t *p_j2k,
336                                                         opj_stream_private_t *p_stream,
337                                                         opj_event_mgr_t * p_manager);
338
339
340 static opj_bool j2k_pre_write_tile ( opj_j2k_v2_t * p_j2k,
341                                                                          OPJ_UINT32 p_tile_index,
342                                                                          opj_stream_private_t *p_stream,
343                                                                          opj_event_mgr_t * p_manager );
344
345 static opj_bool j2k_update_image_data (opj_tcd_v2_t * p_tcd, OPJ_BYTE * p_data, opj_image_t* p_output_image);
346
347 static void j2k_get_tile_data (opj_tcd_v2_t * p_tcd, OPJ_BYTE * p_data);
348
349 static opj_bool j2k_post_write_tile (opj_j2k_v2_t * p_j2k,
350                                                                          OPJ_BYTE * p_data,
351                                                                          OPJ_UINT32 p_data_size,
352                                                                          opj_stream_private_t *p_stream,
353                                                                          opj_event_mgr_t * p_manager );
354
355 /**
356  * Sets up the procedures to do on writing header.
357  * Developers wanting to extend the library can add their own writing procedures.
358  */
359 void j2k_setup_header_writting (opj_j2k_v2_t *p_j2k);
360
361 static opj_bool j2k_write_first_tile_part(      opj_j2k_v2_t *p_j2k,
362                                                                                         OPJ_BYTE * p_data,
363                                                                                         OPJ_UINT32 * p_data_written,
364                                                                                         OPJ_UINT32 p_total_data_size,
365                                                                                         opj_stream_private_t *p_stream,
366                                                                                         struct opj_event_mgr * p_manager );
367
368 static opj_bool j2k_write_all_tile_parts(       opj_j2k_v2_t *p_j2k,
369                                                                                         OPJ_BYTE * p_data,
370                                                                                         OPJ_UINT32 * p_data_written,
371                                                                                         OPJ_UINT32 p_total_data_size,
372                                                                                         opj_stream_private_t *p_stream,
373                                                                                         struct opj_event_mgr * p_manager );
374
375 /**
376  * Gets the offset of the header.
377  *
378  * @param       p_stream                the stream to write data to.
379  * @param       p_j2k                   J2K codec.
380  * @param       p_manager               the user event manager.
381 */
382 static opj_bool j2k_get_end_header(     opj_j2k_v2_t *p_j2k,
383                                                                         struct opj_stream_private *p_stream,
384                                                                         struct opj_event_mgr * p_manager );
385
386 /*
387  * -----------------------------------------------------------------------
388  * -----------------------------------------------------------------------
389  * -----------------------------------------------------------------------
390  */
391
392 /**
393 Write the SOC marker (Start Of Codestream)
394 @param j2k J2K handle
395 */
396 static void j2k_write_soc(opj_j2k_t *j2k);
397
398 /**
399  * Writes the SOC marker (Start Of Codestream)
400  *
401  * @param       p_stream                        the stream to write data to.
402  * @param       p_j2k                   J2K codec.
403  * @param       p_manager       the user event manager.
404 */
405 static opj_bool j2k_write_soc_v2(       opj_j2k_v2_t *p_j2k,
406                                                                         struct opj_stream_private *p_stream,
407                                                                         struct opj_event_mgr * p_manager );
408
409 /**
410 Read the SOC marker (Start of Codestream)
411 @param j2k J2K handle
412 */
413 static void j2k_read_soc(opj_j2k_t *j2k);
414
415 /**
416  * Reads a SOC marker (Start of Codestream)
417  * @param       p_header_data   the data contained in the SOC box.
418  * @param       jp2                             the jpeg2000 file codec.
419  * @param       p_header_size   the size of the data contained in the SOC marker.
420  * @param       p_manager               the user event manager.
421 */
422 static opj_bool j2k_read_soc_v2(
423                                         opj_j2k_v2_t *p_j2k,
424                                         struct opj_stream_private *p_stream,
425                                         struct opj_event_mgr * p_manager
426                                  );
427
428 /**
429 Write the SIZ marker (image and tile size)
430 @param j2k J2K handle
431 */
432 static void j2k_write_siz(opj_j2k_t *j2k);
433
434 /**
435  * Writes the SIZ marker (image and tile size)
436  *
437  * @param       p_stream                        the stream to write data to.
438  * @param       p_j2k                   J2K codec.
439  * @param       p_manager       the user event manager.
440 */
441 static opj_bool j2k_write_siz_v2(       opj_j2k_v2_t *p_j2k,
442                                                                         struct opj_stream_private *p_stream,
443                                                                         struct opj_event_mgr * p_manager );
444
445 /**
446 Read the SIZ marker (image and tile size)
447 @param j2k J2K handle
448 */
449 static void j2k_read_siz(opj_j2k_t *j2k);
450
451 /**
452  * Reads a SIZ marker (image and tile size)
453  * @param       p_header_data   the data contained in the SIZ box.
454  * @param       jp2                             the jpeg2000 file codec.
455  * @param       p_header_size   the size of the data contained in the SIZ marker.
456  * @param       p_manager               the user event manager.
457 */
458 static opj_bool j2k_read_siz_v2 (
459                                                   opj_j2k_v2_t *p_j2k,
460                                                   OPJ_BYTE * p_header_data,
461                                                   OPJ_UINT32 p_header_size,
462                                                   struct opj_event_mgr * p_manager
463                                         );
464
465 /**
466 Write the COM marker (comment)
467 @param j2k J2K handle
468 */
469 static void j2k_write_com(opj_j2k_t *j2k);
470
471 /**
472  * Writes the COM marker (comment)
473  * 
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 j2k_write_com_v2(       opj_j2k_v2_t *p_j2k,
479                                                                         struct opj_stream_private *p_stream,
480                                                                         struct opj_event_mgr * p_manager );
481
482 /**
483 Read the COM marker (comment)
484 @param j2k J2K handle
485 */
486 static void j2k_read_com(opj_j2k_t *j2k);
487 /**
488  * Reads a COM marker (comments)
489  * @param       p_header_data   the data contained in the COM box.
490  * @param       jp2                             the jpeg2000 file codec.
491  * @param       p_header_size   the size of the data contained in the COM marker.
492  * @param       p_manager               the user event manager.
493 */
494 static opj_bool j2k_read_com_v2 (
495                                         opj_j2k_v2_t *p_j2k,
496                                         OPJ_BYTE * p_header_data,
497                                         OPJ_UINT32 p_header_size,
498                                         struct opj_event_mgr * p_manager
499                                         );
500 /**
501 Write the value concerning the specified component in the marker COD and COC
502 @param j2k J2K handle
503 @param compno Number of the component concerned by the information written
504 */
505 static void j2k_write_cox(opj_j2k_t *j2k, int compno);
506 /**
507 Read the value concerning the specified component in the marker COD and COC
508 @param j2k J2K handle
509 @param compno Number of the component concerned by the information read
510 */
511 static void j2k_read_cox(opj_j2k_t *j2k, int compno);
512 /**
513 Write the COD marker (coding style default)
514 @param j2k J2K handle
515 */
516 static void j2k_write_cod(opj_j2k_t *j2k);
517
518 /**
519  * Writes the COD marker (Coding style default)
520  *
521  * @param       p_stream                        the stream to write data to.
522  * @param       p_j2k                   J2K codec.
523  * @param       p_manager       the user event manager.
524 */
525 static opj_bool j2k_write_cod_v2(       opj_j2k_v2_t *p_j2k,
526                                                                         struct opj_stream_private *p_stream,
527                                                                         struct opj_event_mgr * p_manager );
528
529 /**
530 Read the COD marker (coding style default)
531 @param j2k J2K handle
532 */
533 static void j2k_read_cod(opj_j2k_t *j2k);
534
535 /**
536  * Reads a COD marker (Coding Styke defaults)
537  * @param       p_header_data   the data contained in the COD box.
538  * @param       p_j2k                   the jpeg2000 codec.
539  * @param       p_header_size   the size of the data contained in the COD marker.
540  * @param       p_manager               the user event manager.
541 */
542 static opj_bool j2k_read_cod_v2 (
543                                         opj_j2k_v2_t *p_j2k,
544                                         OPJ_BYTE * p_header_data,
545                                         OPJ_UINT32 p_header_size,
546                                         struct opj_event_mgr * p_manager
547                                         );
548
549 /**
550 Write the COC marker (coding style component)
551 @param j2k J2K handle
552 @param compno Number of the component concerned by the information written
553 */
554 static void j2k_write_coc(opj_j2k_t *j2k, int compno);
555
556 /**
557  * Writes the COC marker (Coding style component)
558  *
559  * @param       p_comp_number   the index of the component to output.
560  * @param       p_stream                                the stream to write data to.
561  * @param       p_j2k                           J2K codec.
562  * @param       p_manager               the user event manager.
563 */
564 static opj_bool j2k_write_coc_v2( opj_j2k_v2_t *p_j2k,
565                                                         OPJ_UINT32 p_comp_number,
566                                                         struct opj_stream_private *p_stream,
567                                                         struct opj_event_mgr * p_manager
568                                                   );
569 /**
570  * Writes the COC marker (Coding style component)
571  *
572  * @param       p_comp_no               the index of the component to output.
573  * @param       p_stream                the stream to write data to.
574  * @param       p_j2k                   J2K codec.
575  * @param       p_manager               the user event manager.
576 */
577 static void j2k_write_coc_in_memory(opj_j2k_v2_t *p_j2k,
578                                                                         OPJ_UINT32 p_comp_no,
579                                                                         OPJ_BYTE * p_data,
580                                                                         OPJ_UINT32 * p_data_written,
581                                                                         struct opj_event_mgr * p_manager );
582
583 /**
584  * Gets the maximum size taken by a coc.
585  *
586  * @param       p_j2k   the jpeg2000 codec to use.
587  */
588 static OPJ_UINT32 j2k_get_max_coc_size(opj_j2k_v2_t *p_j2k);
589
590 /**
591 Read the COC marker (coding style component)
592 @param j2k J2K handle
593 */
594 static void j2k_read_coc(opj_j2k_t *j2k);
595
596 /**
597  * Reads a COC marker (Coding Style Component)
598  * @param       p_header_data   the data contained in the COC box.
599  * @param       p_j2k                   the jpeg2000 codec.
600  * @param       p_header_size   the size of the data contained in the COC marker.
601  * @param       p_manager               the user event manager.
602 */
603 static opj_bool j2k_read_coc_v2 (
604                                         opj_j2k_v2_t *p_j2k,
605                                         OPJ_BYTE * p_header_data,
606                                         OPJ_UINT32 p_header_size,
607                                         struct opj_event_mgr * p_manager
608                                         );
609
610 /**
611 Write the value concerning the specified component in the marker QCD and QCC
612 @param j2k J2K handle
613 @param compno Number of the component concerned by the information written
614 */
615 static void j2k_write_qcx(opj_j2k_t *j2k, int compno);
616 /**
617 Read the value concerning the specified component in the marker QCD and QCC
618 @param j2k J2K handle
619 @param compno Number of the component concern by the information read
620 @param len Length of the information in the QCX part of the marker QCD/QCC
621 */
622 static void j2k_read_qcx(opj_j2k_t *j2k, int compno, int len);
623 /**
624 Write the QCD marker (quantization default)
625 @param j2k J2K handle
626 */
627 static void j2k_write_qcd(opj_j2k_t *j2k);
628
629 /**
630  * Writes the QCD marker (quantization default)
631  *
632  * @param       p_comp_number   the index of the component to output.
633  * @param       p_stream                the stream to write data to.
634  * @param       p_j2k                   J2K codec.
635  * @param       p_manager               the user event manager.
636 */
637 static opj_bool j2k_write_qcd_v2(       opj_j2k_v2_t *p_j2k,
638                                                                         struct opj_stream_private *p_stream,
639                                                                         struct opj_event_mgr * p_manager );
640
641 /**
642 Read the QCD marker (quantization default)
643 @param j2k J2K handle
644 */
645 static void j2k_read_qcd(opj_j2k_t *j2k);
646
647 /**
648  * Reads a QCD marker (Quantization defaults)
649  * @param       p_header_data   the data contained in the QCD box.
650  * @param       p_j2k                   the jpeg2000 codec.
651  * @param       p_header_size   the size of the data contained in the QCD marker.
652  * @param       p_manager               the user event manager.
653 */
654 static opj_bool j2k_read_qcd_v2 (
655                                         opj_j2k_v2_t *p_j2k,
656                                         OPJ_BYTE * p_header_data,
657                                         OPJ_UINT32 p_header_size,
658                                         struct opj_event_mgr * p_manager
659                                         );
660
661 /**
662 Write the QCC marker (quantization component)
663 @param j2k J2K handle
664 @param compno Number of the component concerned by the information written
665 */
666 static void j2k_write_qcc(opj_j2k_t *j2k, int compno);
667
668 /**
669  * Writes the QCC marker (quantization component)
670  *
671  * @param       p_comp_no       the index of the component to output.
672  * @param       p_stream                the stream to write data to.
673  * @param       p_j2k                   J2K codec.
674  * @param       p_manager               the user event manager.
675 */
676 static opj_bool j2k_write_qcc_v2(       opj_j2k_v2_t *p_j2k,
677                                                                         OPJ_UINT32 p_comp_no,
678                                                                         struct opj_stream_private *p_stream,
679                                                                         struct opj_event_mgr * p_manager );
680
681 /**
682  * Writes the QCC marker (quantization component)
683  *
684  * @param       p_comp_no       the index of the component to output.
685  * @param       p_stream                the stream to write data to.
686  * @param       p_j2k                   J2K codec.
687  * @param       p_manager               the user event manager.
688 */
689 static void j2k_write_qcc_in_memory(opj_j2k_v2_t *p_j2k,
690                                                                         OPJ_UINT32 p_comp_no,
691                                                                         OPJ_BYTE * p_data,
692                                                                         OPJ_UINT32 * p_data_written,
693                                                                         struct opj_event_mgr * p_manager );
694
695 /**
696  * Gets the maximum size taken by a qcc.
697  */
698 static OPJ_UINT32 j2k_get_max_qcc_size (opj_j2k_v2_t *p_j2k);
699
700 /**
701 Read the QCC marker (quantization component)
702 @param j2k J2K handle
703 */
704 static void j2k_read_qcc(opj_j2k_t *j2k);
705 /**
706  * Reads a QCC marker (Quantization component)
707  * @param       p_header_data   the data contained in the QCC box.
708  * @param       p_j2k                   the jpeg2000 codec.
709  * @param       p_header_size   the size of the data contained in the QCC marker.
710  * @param       p_manager               the user event manager.
711 */
712 static opj_bool j2k_read_qcc_v2(
713                                                         opj_j2k_v2_t *p_j2k,
714                                                         OPJ_BYTE * p_header_data,
715                                                         OPJ_UINT32 p_header_size,
716                                                         struct opj_event_mgr * p_manager);
717
718 /**
719 Write the POC marker (progression order change)
720 @param j2k J2K handle
721 */
722 static void j2k_write_poc(opj_j2k_t *j2k);
723
724 /**
725  * Writes the POC marker (Progression Order Change)
726  * 
727  * @param       p_stream                                the stream to write data to.
728  * @param       p_j2k                           J2K codec.
729  * @param       p_manager               the user event manager.
730 */
731 static opj_bool j2k_write_poc_v2(       opj_j2k_v2_t *p_j2k,
732                                                                         struct opj_stream_private *p_stream,
733                                                                         struct opj_event_mgr * p_manager );
734
735 /**
736  * Writes the POC marker (Progression Order Change)
737  *
738  * @param       p_stream                the stream to write data to.
739  * @param       p_j2k                   J2K codec.
740  * @param       p_manager               the user event manager.
741  */
742 static void j2k_write_poc_in_memory(opj_j2k_v2_t *p_j2k,
743                                                                         OPJ_BYTE * p_data,
744                                                                         OPJ_UINT32 * p_data_written,
745                                                                         struct opj_event_mgr * p_manager );
746
747 /**
748  * Gets the maximum size taken by the writting of a POC.
749  */
750 static OPJ_UINT32 j2k_get_max_poc_size(opj_j2k_v2_t *p_j2k);
751
752 /**
753  * Gets the maximum size taken by the toc headers of all the tile parts of any given tile.
754  */
755 static OPJ_UINT32 j2k_get_max_toc_size (opj_j2k_v2_t *p_j2k);
756
757 /**
758  * Gets the maximum size taken by the headers of the SOT.
759  *
760  * @param       p_j2k   the jpeg2000 codec to use.
761  */
762 static OPJ_UINT32 j2k_get_specific_header_sizes(opj_j2k_v2_t *p_j2k);
763
764 /**
765 Read the POC marker (progression order change)
766 @param j2k J2K handle
767 */
768 static void j2k_read_poc(opj_j2k_t *j2k);
769 /**
770  * Reads a POC marker (Progression Order Change)
771  *
772  * @param       p_header_data   the data contained in the POC box.
773  * @param       p_j2k                   the jpeg2000 codec.
774  * @param       p_header_size   the size of the data contained in the POC marker.
775  * @param       p_manager               the user event manager.
776 */
777 static opj_bool j2k_read_poc_v2 (
778                                                 opj_j2k_v2_t *p_j2k,
779                                                 OPJ_BYTE * p_header_data,
780                                                 OPJ_UINT32 p_header_size,
781                                                 struct opj_event_mgr * p_manager
782                                         );
783
784 /**
785 Read the CRG marker (component registration)
786 @param j2k J2K handle
787 */
788 static void j2k_read_crg(opj_j2k_t *j2k);
789 /**
790  * Reads a CRG marker (Component registration)
791  *
792  * @param       p_header_data   the data contained in the TLM box.
793  * @param       p_j2k                   the jpeg2000 codec.
794  * @param       p_header_size   the size of the data contained in the TLM marker.
795  * @param       p_manager               the user event manager.
796 */
797 static opj_bool j2k_read_crg_v2 (
798                                                 opj_j2k_v2_t *p_j2k,
799                                                 OPJ_BYTE * p_header_data,
800                                                 OPJ_UINT32 p_header_size,
801                                                 struct opj_event_mgr * p_manager
802                                         );
803 /**
804 Read the TLM marker (tile-part lengths)
805 @param j2k J2K handle
806 */
807 static void j2k_read_tlm(opj_j2k_t *j2k);
808 /**
809  * Reads a TLM marker (Tile Length Marker)
810  *
811  * @param       p_header_data   the data contained in the TLM box.
812  * @param       p_j2k                   the jpeg2000 codec.
813  * @param       p_header_size   the size of the data contained in the TLM marker.
814  * @param       p_manager               the user event manager.
815 */
816 static opj_bool j2k_read_tlm_v2 (
817                                                 opj_j2k_v2_t *p_j2k,
818                                                 OPJ_BYTE * p_header_data,
819                                                 OPJ_UINT32 p_header_size,
820                                                 struct opj_event_mgr * p_manager
821                                         );
822
823 /**
824  * Writes the updated tlm.
825  *
826  * @param       p_stream                the stream to write data to.
827  * @param       p_j2k                   J2K codec.
828  * @param       p_manager               the user event manager.
829 */
830 static opj_bool j2k_write_updated_tlm(  opj_j2k_v2_t *p_j2k,
831                                                                                 struct opj_stream_private *p_stream,
832                                                                                 struct opj_event_mgr * p_manager );
833
834 /**
835 Read the PLM marker (packet length, main header)
836 @param j2k J2K handle
837 */
838 static void j2k_read_plm(opj_j2k_t *j2k);
839
840 /**
841  * Reads a PLM marker (Packet length, main header marker)
842  *
843  * @param       p_header_data   the data contained in the TLM box.
844  * @param       p_j2k                   the jpeg2000 codec.
845  * @param       p_header_size   the size of the data contained in the TLM marker.
846  * @param       p_manager               the user event manager.
847 */
848 static opj_bool j2k_read_plm_v2 (
849                                                 opj_j2k_v2_t *p_j2k,
850                                                 OPJ_BYTE * p_header_data,
851                                                 OPJ_UINT32 p_header_size,
852                                                 struct opj_event_mgr * p_manager
853                                         );
854 /**
855 Read the PLT marker (packet length, tile-part header)
856 @param j2k J2K handle
857 */
858 static void j2k_read_plt(opj_j2k_t *j2k);
859 /**
860  * Reads a PLT marker (Packet length, tile-part header)
861  *
862  * @param       p_header_data   the data contained in the PLT box.
863  * @param       p_j2k                   the jpeg2000 codec.
864  * @param       p_header_size   the size of the data contained in the PLT marker.
865  * @param       p_manager               the user event manager.
866 */
867 static opj_bool j2k_read_plt_v2 (
868                                                 opj_j2k_v2_t *p_j2k,
869                                                 OPJ_BYTE * p_header_data,
870                                                 OPJ_UINT32 p_header_size,
871                                                 struct opj_event_mgr * p_manager
872                                         );
873 /**
874 Read the PPM marker (packet packet headers, main header)
875 @param j2k J2K handle
876 */
877 static void j2k_read_ppm(opj_j2k_t *j2k);
878 /**
879  * Reads a PPM marker (Packed packet headers, main header)
880  *
881  * @param       p_header_data   the data contained in the POC box.
882  * @param       p_j2k                   the jpeg2000 codec.
883  * @param       p_header_size   the size of the data contained in the POC marker.
884  * @param       p_manager               the user event manager.
885 */
886 #if 0
887 static opj_bool j2k_read_ppm_v2 (
888                                                 opj_j2k_v2_t *p_j2k,
889                                                 OPJ_BYTE * p_header_data,
890                                                 OPJ_UINT32 p_header_size,
891                                                 struct opj_event_mgr * p_manager
892                                         );
893 #endif
894
895 static opj_bool j2k_read_ppm_v3 (
896                                                 opj_j2k_v2_t *p_j2k,
897                                                 OPJ_BYTE * p_header_data,
898                                                 OPJ_UINT32 p_header_size,
899                                                 struct opj_event_mgr * p_manager
900                                         );
901
902 /**
903 Read the PPT marker (packet packet headers, tile-part header)
904 @param j2k J2K handle
905 */
906 static void j2k_read_ppt(opj_j2k_t *j2k);
907 /**
908  * Reads a PPT marker (Packed packet headers, tile-part header)
909  *
910  * @param       p_header_data   the data contained in the PPT box.
911  * @param       p_j2k                   the jpeg2000 codec.
912  * @param       p_header_size   the size of the data contained in the PPT marker.
913  * @param       p_manager               the user event manager.
914 */
915 static opj_bool j2k_read_ppt_v2 (
916                                                 opj_j2k_v2_t *p_j2k,
917                                                 OPJ_BYTE * p_header_data,
918                                                 OPJ_UINT32 p_header_size,
919                                                 struct opj_event_mgr * p_manager
920                                         );
921 /**
922 Write the TLM marker (Mainheader)
923 @param j2k J2K handle
924 */
925 static void j2k_write_tlm(opj_j2k_t *j2k);
926
927 /**
928  * Writes the TLM marker (Tile Length Marker)
929  * 
930  * @param       p_stream                                the stream to write data to.
931  * @param       p_j2k                           J2K codec.
932  * @param       p_manager               the user event manager.
933 */
934 static opj_bool j2k_write_tlm_v2(       opj_j2k_v2_t *p_j2k,
935                                                                         struct opj_stream_private *p_stream,
936                                                                         struct opj_event_mgr * p_manager );
937
938 /**
939 Write the SOT marker (start of tile-part)
940 @param j2k J2K handle
941 */
942 static void j2k_write_sot(opj_j2k_t *j2k);
943 /**
944 Read the SOT marker (start of tile-part)
945 @param j2k J2K handle
946 */
947 static void j2k_read_sot(opj_j2k_t *j2k);
948
949 /**
950  * Writes the SOT marker (Start of tile-part)
951  *
952  * @param       p_stream                the stream to write data to.
953  * @param       p_j2k                   J2K codec.
954  * @param       p_manager               the user event manager.
955 */
956 static opj_bool j2k_write_sot_v2(       opj_j2k_v2_t *p_j2k,
957                                                                         OPJ_BYTE * p_data,
958                                                                         OPJ_UINT32 * p_data_written,
959                                                                         const struct opj_stream_private *p_stream,
960                                                                         struct opj_event_mgr * p_manager );
961
962 /**
963  * Reads a PPT marker (Packed packet headers, tile-part header)
964  *
965  * @param       p_header_data   the data contained in the PPT box.
966  * @param       p_j2k                   the jpeg2000 codec.
967  * @param       p_header_size   the size of the data contained in the PPT marker.
968  * @param       p_manager               the user event manager.
969 */
970 static opj_bool j2k_read_sot_v2 (
971                                                 opj_j2k_v2_t *p_j2k,
972                                                 OPJ_BYTE * p_header_data,
973                                                 OPJ_UINT32 p_header_size,
974                                                 struct opj_event_mgr * p_manager
975                                         );
976 /**
977 Write the SOD marker (start of data)
978 @param j2k J2K handle
979 @param tile_coder Pointer to a TCD handle
980 */
981 static void j2k_write_sod(opj_j2k_t *j2k, void *tile_coder);
982
983 /**
984  * Writes the SOD marker (Start of data)
985  *
986  * @param       p_stream                                the stream to write data to.
987  * @param       p_j2k                           J2K codec.
988  * @param       p_manager               the user event manager.
989 */
990 static opj_bool j2k_write_sod_v2(       opj_j2k_v2_t *p_j2k,
991                                                                         struct opj_tcd_v2 * p_tile_coder,
992                                                                         OPJ_BYTE * p_data,
993                                                                         OPJ_UINT32 * p_data_written,
994                                                                         OPJ_UINT32 p_total_data_size,
995                                                                         const struct opj_stream_private *p_stream,
996                                                                         struct opj_event_mgr * p_manager );
997
998 /**
999 Read the SOD marker (start of data)
1000 @param j2k J2K handle
1001 */
1002 static void j2k_read_sod(opj_j2k_t *j2k);
1003
1004 /**
1005  * Reads a SOD marker (Start Of Data)
1006  *
1007  * @param       p_header_data   the data contained in the SOD box.
1008  * @param       p_j2k                   the jpeg2000 codec.
1009  * @param       p_header_size   the size of the data contained in the SOD marker.
1010  * @param       p_manager               the user event manager.
1011 */
1012 static opj_bool j2k_read_sod_v2 (
1013                                                 opj_j2k_v2_t *p_j2k,
1014                                                 struct opj_stream_private *p_stream,
1015                                                 struct opj_event_mgr * p_manager
1016                                         );
1017
1018 /**
1019  * Updates the Tile Length Marker.
1020  */
1021 void j2k_update_tlm (opj_j2k_v2_t * p_j2k, OPJ_UINT32 p_tile_part_size )
1022 {
1023         opj_write_bytes(p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current,p_j2k->m_current_tile_number,1);                                    /* PSOT */
1024         ++p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current;
1025
1026         opj_write_bytes(p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current,p_tile_part_size,4);                                        /* PSOT */
1027         p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current += 4;
1028 }
1029
1030 /**
1031 Write the RGN marker (region-of-interest)
1032 @param j2k J2K handle
1033 @param compno Number of the component concerned by the information written
1034 @param tileno Number of the tile concerned by the information written
1035 */
1036 static void j2k_write_rgn(opj_j2k_t *j2k, int compno, int tileno);
1037
1038 /**
1039  * Writes the RGN marker (Region Of Interest)
1040  *
1041  * @param       p_tile_no               the tile to output
1042  * @param       p_comp_no               the component to output
1043  * @param       p_stream                                the stream to write data to.
1044  * @param       p_j2k                           J2K codec.
1045  * @param       p_manager               the user event manager.
1046 */
1047 static opj_bool j2k_write_rgn_v2(       opj_j2k_v2_t *p_j2k,
1048                                                                         OPJ_UINT32 p_tile_no,
1049                                                                         OPJ_UINT32 p_comp_no,
1050                                                                         struct opj_stream_private *p_stream,
1051                                                                         struct opj_event_mgr * p_manager );
1052
1053 /**
1054 Read the RGN marker (region-of-interest)
1055 @param j2k J2K handle
1056 */
1057 static void j2k_read_rgn(opj_j2k_t *j2k);
1058
1059 /**
1060  * Reads a RGN marker (Region Of Interest)
1061  *
1062  * @param       p_header_data   the data contained in the POC box.
1063  * @param       p_j2k                   the jpeg2000 codec.
1064  * @param       p_header_size   the size of the data contained in the POC marker.
1065  * @param       p_manager               the user event manager.
1066 */
1067 static opj_bool j2k_read_rgn_v2 (
1068                                                 opj_j2k_v2_t *p_j2k,
1069                                                 OPJ_BYTE * p_header_data,
1070                                                 OPJ_UINT32 p_header_size,
1071                                                 struct opj_event_mgr * p_manager
1072                                         ) ;
1073
1074 /**
1075  * Writes the EOC marker (End of Codestream)
1076  * 
1077  * @param       p_stream                the stream to write data to.
1078  * @param       p_j2k                   J2K codec.
1079  * @param       p_manager               the user event manager.
1080 */
1081 static opj_bool j2k_write_eoc_v2(       opj_j2k_v2_t *p_j2k,
1082                                                                         struct opj_stream_private *p_stream,
1083                                                                         struct opj_event_mgr * p_manager );
1084
1085 /**
1086 Write the EOC marker (end of codestream)
1087 @param j2k J2K handle
1088 */
1089 static void j2k_write_eoc(opj_j2k_t *j2k);
1090 /**
1091 Read the EOC marker (end of codestream)
1092 @param j2k J2K handle
1093 */
1094 static void j2k_read_eoc(opj_j2k_t *j2k);
1095
1096 /**
1097  * Reads a EOC marker (End Of Codestream)
1098  *
1099  * @param       p_header_data   the data contained in the SOD box.
1100  * @param       p_j2k                   the jpeg2000 codec.
1101  * @param       p_header_size   the size of the data contained in the SOD marker.
1102  * @param       p_manager               the user event manager.
1103 */
1104 #if 0
1105 static opj_bool j2k_read_eoc_v2 (
1106                                             opj_j2k_v2_t *p_j2k,
1107                                                 struct opj_stream_private *p_stream,
1108                                                 struct opj_event_mgr * p_manager
1109                                         ) ;
1110 #endif
1111
1112
1113
1114 /**
1115  * Writes the CBD-MCT-MCC-MCO markers (Multi components transform)
1116  *
1117  * @param       p_stream                        the stream to write data to.
1118  * @param       p_j2k                   J2K codec.
1119  * @param       p_manager       the user event manager.
1120 */
1121 static opj_bool j2k_write_mct_data_group(       opj_j2k_v2_t *p_j2k,
1122                                                                                         struct opj_stream_private *p_stream,
1123                                                                                         struct opj_event_mgr * p_manager );
1124
1125 /**
1126  * Inits the Info
1127  *
1128  * @param       p_stream                the stream to write data to.
1129  * @param       p_j2k                   J2K codec.
1130  * @param       p_manager               the user event manager.
1131 */
1132 static opj_bool j2k_init_info(  opj_j2k_v2_t *p_j2k,
1133                                                                 struct opj_stream_private *p_stream,
1134                                                                 struct opj_event_mgr * p_manager );
1135
1136 /**
1137 Read an unknown marker
1138 @param j2k J2K handle
1139 */
1140 static void j2k_read_unk(opj_j2k_t *j2k);
1141 /**
1142 Add main header marker information
1143 @param cstr_info Codestream information structure
1144 @param type marker type
1145 @param pos byte offset of marker segment
1146 @param len length of marker segment
1147  */
1148 static void j2k_add_mhmarker(opj_codestream_info_t *cstr_info, unsigned short int type, int pos, int len);
1149
1150 static void j2k_add_mhmarker_v2(opj_codestream_index_t *cstr_index, OPJ_UINT32 type, OPJ_OFF_T pos, OPJ_UINT32 len) ;
1151 /**
1152 Add tile header marker information
1153 @param tileno tile index number
1154 @param cstr_info Codestream information structure
1155 @param type marker type
1156 @param pos byte offset of marker segment
1157 @param len length of marker segment
1158  */
1159 static void j2k_add_tlmarker( int tileno, opj_codestream_info_t *cstr_info, unsigned short int type, int pos, int len);
1160
1161 static void j2k_add_tlmarker_v2(OPJ_UINT32 tileno, opj_codestream_index_t *cstr_index, OPJ_UINT32 type, OPJ_OFF_T pos, OPJ_UINT32 len);
1162
1163 /**
1164  * Reads an unknown marker
1165  *
1166  * @param       p_stream                the stream object to read from.
1167  * @param       p_j2k                   the jpeg2000 codec.
1168  * @param       p_manager               the user event manager.
1169  *
1170  * @return      true                    if the marker could be deduced.
1171 */
1172 static opj_bool j2k_read_unk_v2 (       opj_j2k_v2_t *p_j2k,
1173                                                                         struct opj_stream_private *p_stream,
1174                                                                         OPJ_UINT32 *output_marker,
1175                                                                         struct opj_event_mgr * p_manager );
1176
1177 /**
1178  * Writes the MCT marker (Multiple Component Transform)
1179  *
1180  * @param       p_stream                                the stream to write data to.
1181  * @param       p_j2k                           J2K codec.
1182  * @param       p_manager               the user event manager.
1183 */
1184 static opj_bool j2k_write_mct_record(   opj_j2k_v2_t *p_j2k,
1185                                                                                 opj_mct_data_t * p_mct_record,
1186                                                                                 struct opj_stream_private *p_stream,
1187                                                                                 struct opj_event_mgr * p_manager );
1188
1189 /**
1190  * Reads a MCT marker (Multiple Component Transform)
1191  *
1192  * @param       p_header_data   the data contained in the MCT box.
1193  * @param       p_j2k                   the jpeg2000 codec.
1194  * @param       p_header_size   the size of the data contained in the MCT marker.
1195  * @param       p_manager               the user event manager.
1196 */
1197 static opj_bool j2k_read_mct (  opj_j2k_v2_t *p_j2k,
1198                                                                 OPJ_BYTE * p_header_data,
1199                                                                 OPJ_UINT32 p_header_size,
1200                                                                 struct opj_event_mgr * p_manager );
1201
1202 /**
1203  * Writes the MCC marker (Multiple Component Collection)
1204  *
1205  * @param       p_stream                the stream to write data to.
1206  * @param       p_j2k                   J2K codec.
1207  * @param       p_manager               the user event manager.
1208 */
1209 static opj_bool j2k_write_mcc_record(   opj_j2k_v2_t *p_j2k,
1210                                                                                 struct opj_simple_mcc_decorrelation_data * p_mcc_record,
1211                                                                                 struct opj_stream_private *p_stream,
1212                                                                                 struct opj_event_mgr * p_manager );
1213
1214
1215 /**
1216  * Reads a MCC marker (Multiple Component Collection)
1217  *
1218  * @param       p_header_data   the data contained in the MCC box.
1219  * @param       p_j2k                   the jpeg2000 codec.
1220  * @param       p_header_size   the size of the data contained in the MCC marker.
1221  * @param       p_manager               the user event manager.
1222 */
1223 static opj_bool j2k_read_mcc (  opj_j2k_v2_t *p_j2k,
1224                                                                 OPJ_BYTE * p_header_data,
1225                                                                 OPJ_UINT32 p_header_size,
1226                                                                 struct opj_event_mgr * p_manager );
1227
1228 /**
1229  * Writes the MCO marker (Multiple component transformation ordering)
1230  *
1231  * @param       p_stream                                the stream to write data to.
1232  * @param       p_j2k                           J2K codec.
1233  * @param       p_manager               the user event manager.
1234 */
1235 static opj_bool j2k_write_mco(  opj_j2k_v2_t *p_j2k,
1236                                                                 struct opj_stream_private *p_stream,
1237                                                                 struct opj_event_mgr * p_manager );
1238
1239 /**
1240  * Reads a MCO marker (Multiple Component Transform Ordering)
1241  *
1242  * @param       p_header_data   the data contained in the MCO box.
1243  * @param       p_j2k                   the jpeg2000 codec.
1244  * @param       p_header_size   the size of the data contained in the MCO marker.
1245  * @param       p_manager               the user event manager.
1246 */
1247 static opj_bool j2k_read_mco (  opj_j2k_v2_t *p_j2k,
1248                                                                 OPJ_BYTE * p_header_data,
1249                                                                 OPJ_UINT32 p_header_size,
1250                                                                 struct opj_event_mgr * p_manager );
1251
1252 static opj_bool j2k_add_mct(opj_tcp_v2_t * p_tcp, opj_image_t * p_image, OPJ_UINT32 p_index);
1253
1254 static void  j2k_read_int16_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1255 static void  j2k_read_int32_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1256 static void  j2k_read_float32_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1257 static void  j2k_read_float64_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1258
1259 static void  j2k_read_int16_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1260 static void  j2k_read_int32_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1261 static void  j2k_read_float32_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1262 static void  j2k_read_float64_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1263
1264 static void  j2k_write_float_to_int16 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1265 static void  j2k_write_float_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1266 static void  j2k_write_float_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1267 static void  j2k_write_float_to_float64 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1268
1269
1270 /**
1271  * Ends the encoding, i.e. frees memory.
1272  *
1273  * @param       p_stream                the stream to write data to.
1274  * @param       p_j2k                   J2K codec.
1275  * @param       p_manager               the user event manager.
1276 */
1277 static opj_bool j2k_end_encoding(       opj_j2k_v2_t *p_j2k,
1278                                                                         struct opj_stream_private *p_stream,
1279                                                                         struct opj_event_mgr * p_manager );
1280
1281 /**
1282  * Writes the CBD marker (Component bit depth definition)
1283  *
1284  * @param       p_stream                                the stream to write data to.
1285  * @param       p_j2k                           J2K codec.
1286  * @param       p_manager               the user event manager.
1287 */
1288 static opj_bool j2k_write_cbd(  opj_j2k_v2_t *p_j2k,
1289                                                                 struct opj_stream_private *p_stream,
1290                                                                 struct opj_event_mgr * p_manager );
1291
1292 /**
1293  * Reads a CBD marker (Component bit depth definition)
1294  * @param       p_header_data   the data contained in the CBD box.
1295  * @param       p_j2k                   the jpeg2000 codec.
1296  * @param       p_header_size   the size of the data contained in the CBD marker.
1297  * @param       p_manager               the user event manager.
1298 */
1299 static opj_bool j2k_read_cbd (  opj_j2k_v2_t *p_j2k,
1300                                                         OPJ_BYTE * p_header_data,
1301                                                         OPJ_UINT32 p_header_size,
1302                                                         struct opj_event_mgr * p_manager);
1303
1304 /**
1305  * Writes the image components.
1306  *
1307  * @param       p_stream                the stream to write data to.
1308  * @param       p_j2k                   J2K codec.
1309  * @param       p_manager               the user event manager.
1310 */
1311 static opj_bool j2k_write_image_components(     opj_j2k_v2_t *p_j2k,
1312                                                                                         struct opj_stream_private *p_stream,
1313                                                                                         struct opj_event_mgr * p_manager );
1314
1315 /**
1316  * Writes regions of interests.
1317  *
1318  * @param       p_stream                the stream to write data to.
1319  * @param       p_j2k                   J2K codec.
1320  * @param       p_manager               the user event manager.
1321 */
1322 static opj_bool j2k_write_regions(      opj_j2k_v2_t *p_j2k,
1323                                                                         struct opj_stream_private *p_stream,
1324                                                                         struct opj_event_mgr * p_manager );
1325
1326 /**
1327  * Writes EPC ????
1328  *
1329  * @param       p_stream                the stream to write data to.
1330  * @param       p_j2k                   J2K codec.
1331  * @param       p_manager               the user event manager.
1332 */
1333 static opj_bool j2k_write_epc(  opj_j2k_v2_t *p_j2k,
1334                                                                 struct opj_stream_private *p_stream,
1335                                                                 struct opj_event_mgr * p_manager );
1336
1337 /**
1338  * Checks the progression order changes values. Tells of the poc given as input are valid.
1339  * A nice message is outputted at errors.
1340  *
1341  * @param       p_pocs                          the progression order changes.
1342  * @param       p_nb_pocs                       the number of progression order changes.
1343  * @param       p_nb_resolutions        the number of resolutions.
1344  * @param       numcomps                        the number of components
1345  * @param       numlayers                       the number of layers.
1346  *
1347  * @return      true if the pocs are valid.
1348  */
1349 static opj_bool j2k_check_poc_val(      const opj_poc_t *p_pocs,
1350                                                                         OPJ_UINT32 p_nb_pocs,
1351                                                                         OPJ_UINT32 p_nb_resolutions,
1352                                                                         OPJ_UINT32 numcomps,
1353                                                                         OPJ_UINT32 numlayers,
1354                                                                         opj_event_mgr_t * p_manager);
1355
1356 /**
1357  * Gets the number of tile parts used for the given change of progression (if any) and the given tile.
1358  *
1359  * @param               cp                      the coding parameters.
1360  * @param               pino            the offset of the given poc (i.e. its position in the coding parameter).
1361  * @param               tileno          the given tile.
1362  *
1363  * @return              the number of tile parts.
1364  */
1365 static OPJ_UINT32 j2k_get_num_tp_v2( opj_cp_v2_t *cp, OPJ_UINT32 pino, OPJ_UINT32 tileno);
1366
1367 /**     mem allocation for TLM marker*/
1368 static int j2k_calculate_tp(opj_cp_t *cp,int img_numcomp,opj_image_t *image,opj_j2k_t *j2k );
1369
1370 /**
1371  * Calculates the total number of tile parts needed by the encoder to
1372  * encode such an image. If not enough memory is available, then the function return false.
1373  *
1374  * @param       p_nb_tiles      pointer that will hold the number of tile parts.
1375  * @param       cp                      the coding parameters for the image.
1376  * @param       image           the image to encode.
1377  * @param       p_j2k                   the p_j2k encoder.
1378  * @param       p_manager       the user event manager.
1379  *
1380  * @return true if the function was successful, false else.
1381  */
1382 static opj_bool j2k_calculate_tp_v2(opj_j2k_v2_t *p_j2k,
1383                                                                         opj_cp_v2_t *cp,
1384                                                                         OPJ_UINT32 * p_nb_tiles,
1385                                                                         opj_image_t *image,
1386                                                                         opj_event_mgr_t * p_manager);
1387
1388 static void j2k_dump_MH_info(opj_j2k_v2_t* p_j2k, FILE* out_stream);
1389
1390 static void j2k_dump_MH_index(opj_j2k_v2_t* p_j2k, FILE* out_stream);
1391
1392 static opj_codestream_index_t* j2k_create_cstr_index(void);
1393
1394 /*@}*/
1395
1396 /*@}*/
1397
1398 /* ----------------------------------------------------------------------- */
1399 typedef struct j2k_prog_order{
1400         OPJ_PROG_ORDER enum_prog;
1401         char str_prog[5];
1402 }j2k_prog_order_t;
1403
1404 j2k_prog_order_t j2k_prog_order_list[] = {
1405         {CPRL, "CPRL"},
1406         {LRCP, "LRCP"},
1407         {PCRL, "PCRL"},
1408         {RLCP, "RLCP"},
1409         {RPCL, "RPCL"},
1410         {(OPJ_PROG_ORDER)-1, ""}
1411 };
1412
1413
1414
1415 /**
1416  * FIXME DOC
1417  */
1418 const OPJ_UINT32 MCT_ELEMENT_SIZE [] =
1419 {
1420         2,
1421         4,
1422         4,
1423         8
1424 };
1425
1426 typedef void (* j2k_mct_function) (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1427
1428 const j2k_mct_function j2k_mct_read_functions_to_float [] =
1429 {
1430         j2k_read_int16_to_float,
1431         j2k_read_int32_to_float,
1432         j2k_read_float32_to_float,
1433         j2k_read_float64_to_float
1434 };
1435
1436 const j2k_mct_function j2k_mct_read_functions_to_int32 [] =
1437 {
1438         j2k_read_int16_to_int32,
1439         j2k_read_int32_to_int32,
1440         j2k_read_float32_to_int32,
1441         j2k_read_float64_to_int32
1442 };
1443
1444 const j2k_mct_function j2k_mct_write_functions_from_float [] =
1445 {
1446         j2k_write_float_to_int16,
1447         j2k_write_float_to_int32,
1448         j2k_write_float_to_float,
1449         j2k_write_float_to_float64
1450 };
1451
1452 typedef struct opj_dec_memory_marker_handler
1453 {
1454         /** marker value */
1455         OPJ_UINT32 id;
1456         /** value of the state when the marker can appear */
1457         OPJ_UINT32 states;
1458         /** action linked to the marker */
1459         opj_bool (*handler) (
1460                                         opj_j2k_v2_t *p_j2k,
1461                                         OPJ_BYTE * p_header_data,
1462                                         OPJ_UINT32 p_header_size,
1463                                         struct opj_event_mgr * p_manager
1464                                                 );
1465 }
1466 opj_dec_memory_marker_handler_t;
1467
1468 const opj_dec_memory_marker_handler_t j2k_memory_marker_handler_tab [] =
1469 {
1470 #ifdef TODO_MS
1471   {J2K_MS_SOT, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPHSOT, j2k_read_sot},
1472   {J2K_MS_COD, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_cod},
1473   {J2K_MS_COC, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_coc},
1474   {J2K_MS_RGN, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_rgn},
1475   {J2K_MS_QCD, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_qcd},
1476   {J2K_MS_QCC, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_qcc},
1477   {J2K_MS_POC, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_poc},
1478   {J2K_MS_SIZ, J2K_DEC_STATE_MHSIZ , j2k_read_siz},
1479   {J2K_MS_TLM, J2K_DEC_STATE_MH, j2k_read_tlm},
1480   {J2K_MS_PLM, J2K_DEC_STATE_MH, j2k_read_plm},
1481   {J2K_MS_PLT, J2K_DEC_STATE_TPH, j2k_read_plt},
1482   {J2K_MS_PPM, J2K_DEC_STATE_MH, j2k_read_ppm},
1483   {J2K_MS_PPT, J2K_DEC_STATE_TPH, j2k_read_ppt},
1484   {J2K_MS_SOP, 0, 0},
1485   {J2K_MS_CRG, J2K_DEC_STATE_MH, j2k_read_crg},
1486   {J2K_MS_COM, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_com},
1487   {J2K_MS_MCT, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_mct},
1488   {J2K_MS_CBD, J2K_DEC_STATE_MH , j2k_read_cbd},
1489   {J2K_MS_MCC, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_mcc},
1490   {J2K_MS_MCO, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_mco},
1491 #endif
1492   {J2K_MS_SOT, J2K_STATE_MH | J2K_STATE_TPHSOT, j2k_read_sot_v2},
1493   {J2K_MS_COD, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_cod_v2},
1494   {J2K_MS_COC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_coc_v2},
1495   {J2K_MS_RGN, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_rgn_v2},
1496   {J2K_MS_QCD, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_qcd_v2},
1497   {J2K_MS_QCC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_qcc_v2},
1498   {J2K_MS_POC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_poc_v2},
1499   {J2K_MS_SIZ, J2K_STATE_MHSIZ , j2k_read_siz_v2},
1500   {J2K_MS_TLM, J2K_STATE_MH, j2k_read_tlm_v2},
1501   {J2K_MS_PLM, J2K_STATE_MH, j2k_read_plm_v2},
1502   {J2K_MS_PLT, J2K_STATE_TPH, j2k_read_plt_v2},
1503   {J2K_MS_PPM, J2K_STATE_MH, j2k_read_ppm_v3},
1504   {J2K_MS_PPT, J2K_STATE_TPH, j2k_read_ppt_v2},
1505   {J2K_MS_SOP, 0, 0},
1506   {J2K_MS_CRG, J2K_STATE_MH, j2k_read_crg_v2},
1507   {J2K_MS_COM, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_com_v2},
1508   {J2K_MS_MCT, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_mct},
1509   {J2K_MS_CBD, J2K_STATE_MH , j2k_read_cbd},
1510   {J2K_MS_MCC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_mcc},
1511   {J2K_MS_MCO, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_mco},
1512 #ifdef USE_JPWL
1513 #ifdef TODO_MS /* FIXME */
1514   {J2K_MS_EPC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_epc},
1515   {J2K_MS_EPB, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_epb},
1516   {J2K_MS_ESD, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_esd},
1517   {J2K_MS_RED, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_red},
1518 #endif
1519 #endif /* USE_JPWL */
1520 #ifdef USE_JPSEC
1521   {J2K_MS_SEC, J2K_DEC_STATE_MH, j2k_read_sec},
1522   {J2K_MS_INSEC, 0, j2k_read_insec}
1523 #endif /* USE_JPSEC */
1524   {J2K_MS_UNK, J2K_STATE_MH | J2K_STATE_TPH, 0}/*j2k_read_unk_v2}*/
1525 };
1526
1527
1528
1529 void  j2k_read_int16_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1530 {
1531         OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
1532         OPJ_FLOAT32 * l_dest_data = (OPJ_FLOAT32 *) p_dest_data;
1533         OPJ_UINT32 i;
1534         OPJ_UINT32 l_temp;
1535
1536         for (i=0;i<p_nb_elem;++i) {
1537                 opj_read_bytes(l_src_data,&l_temp,2);
1538
1539                 l_src_data+=sizeof(OPJ_INT16);
1540
1541                 *(l_dest_data++) = (OPJ_FLOAT32) l_temp;
1542         }
1543 }
1544
1545 void  j2k_read_int32_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1546 {
1547         OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
1548         OPJ_FLOAT32 * l_dest_data = (OPJ_FLOAT32 *) p_dest_data;
1549         OPJ_UINT32 i;
1550         OPJ_UINT32 l_temp;
1551
1552         for (i=0;i<p_nb_elem;++i) {
1553                 opj_read_bytes(l_src_data,&l_temp,4);
1554
1555                 l_src_data+=sizeof(OPJ_INT32);
1556
1557                 *(l_dest_data++) = (OPJ_FLOAT32) l_temp;
1558         }
1559 }
1560
1561 void  j2k_read_float32_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1562 {
1563         OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
1564         OPJ_FLOAT32 * l_dest_data = (OPJ_FLOAT32 *) p_dest_data;
1565         OPJ_UINT32 i;
1566         OPJ_FLOAT32 l_temp;
1567
1568         for (i=0;i<p_nb_elem;++i) {
1569                 opj_read_float(l_src_data,&l_temp);
1570
1571                 l_src_data+=sizeof(OPJ_FLOAT32);
1572
1573                 *(l_dest_data++) = l_temp;
1574         }
1575 }
1576
1577 void  j2k_read_float64_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1578 {
1579         OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
1580         OPJ_FLOAT32 * l_dest_data = (OPJ_FLOAT32 *) p_dest_data;
1581         OPJ_UINT32 i;
1582         OPJ_FLOAT64 l_temp;
1583
1584         for (i=0;i<p_nb_elem;++i) {
1585                 opj_read_double(l_src_data,&l_temp);
1586
1587                 l_src_data+=sizeof(OPJ_FLOAT64);
1588
1589                 *(l_dest_data++) = (OPJ_FLOAT32) l_temp;
1590         }
1591 }
1592
1593 void  j2k_read_int16_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1594 {
1595         OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
1596         OPJ_INT32 * l_dest_data = (OPJ_INT32 *) p_dest_data;
1597         OPJ_UINT32 i;
1598         OPJ_UINT32 l_temp;
1599
1600         for (i=0;i<p_nb_elem;++i) {
1601                 opj_read_bytes(l_src_data,&l_temp,2);
1602
1603                 l_src_data+=sizeof(OPJ_INT16);
1604
1605                 *(l_dest_data++) = (OPJ_INT32) l_temp;
1606         }
1607 }
1608
1609 void  j2k_read_int32_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1610 {
1611         OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
1612         OPJ_INT32 * l_dest_data = (OPJ_INT32 *) p_dest_data;
1613         OPJ_UINT32 i;
1614         OPJ_UINT32 l_temp;
1615
1616         for (i=0;i<p_nb_elem;++i) {
1617                 opj_read_bytes(l_src_data,&l_temp,4);
1618
1619                 l_src_data+=sizeof(OPJ_INT32);
1620
1621                 *(l_dest_data++) = (OPJ_INT32) l_temp;
1622         }
1623 }
1624
1625 void  j2k_read_float32_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1626 {
1627         OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
1628         OPJ_INT32 * l_dest_data = (OPJ_INT32 *) p_dest_data;
1629         OPJ_UINT32 i;
1630         OPJ_FLOAT32 l_temp;
1631
1632         for (i=0;i<p_nb_elem;++i) {
1633                 opj_read_float(l_src_data,&l_temp);
1634
1635                 l_src_data+=sizeof(OPJ_FLOAT32);
1636
1637                 *(l_dest_data++) = (OPJ_INT32) l_temp;
1638         }
1639 }
1640
1641 void  j2k_read_float64_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1642 {
1643         OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
1644         OPJ_INT32 * l_dest_data = (OPJ_INT32 *) p_dest_data;
1645         OPJ_UINT32 i;
1646         OPJ_FLOAT64 l_temp;
1647
1648         for (i=0;i<p_nb_elem;++i) {
1649                 opj_read_double(l_src_data,&l_temp);
1650
1651                 l_src_data+=sizeof(OPJ_FLOAT64);
1652
1653                 *(l_dest_data++) = (OPJ_INT32) l_temp;
1654         }
1655 }
1656
1657 void  j2k_write_float_to_int16 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1658 {
1659         OPJ_BYTE * l_dest_data = (OPJ_BYTE *) p_dest_data;
1660         OPJ_FLOAT32 * l_src_data = (OPJ_FLOAT32 *) p_src_data;
1661         OPJ_UINT32 i;
1662         OPJ_UINT32 l_temp;
1663
1664         for (i=0;i<p_nb_elem;++i) {
1665                 l_temp = (OPJ_UINT32) *(l_src_data++);
1666
1667                 opj_write_bytes(l_dest_data,l_temp,sizeof(OPJ_INT16));
1668
1669                 l_dest_data+=sizeof(OPJ_INT16);
1670         }
1671 }
1672
1673 void  j2k_write_float_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1674 {
1675         OPJ_BYTE * l_dest_data = (OPJ_BYTE *) p_dest_data;
1676         OPJ_FLOAT32 * l_src_data = (OPJ_FLOAT32 *) p_src_data;
1677         OPJ_UINT32 i;
1678         OPJ_UINT32 l_temp;
1679
1680         for (i=0;i<p_nb_elem;++i) {
1681                 l_temp = (OPJ_UINT32) *(l_src_data++);
1682
1683                 opj_write_bytes(l_dest_data,l_temp,sizeof(OPJ_INT32));
1684
1685                 l_dest_data+=sizeof(OPJ_INT32);
1686         }
1687 }
1688
1689 void  j2k_write_float_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1690 {
1691         OPJ_BYTE * l_dest_data = (OPJ_BYTE *) p_dest_data;
1692         OPJ_FLOAT32 * l_src_data = (OPJ_FLOAT32 *) p_src_data;
1693         OPJ_UINT32 i;
1694         OPJ_FLOAT32 l_temp;
1695
1696         for (i=0;i<p_nb_elem;++i) {
1697                 l_temp = (OPJ_FLOAT32) *(l_src_data++);
1698
1699                 opj_write_float(l_dest_data,l_temp);
1700
1701                 l_dest_data+=sizeof(OPJ_FLOAT32);
1702         }
1703 }
1704
1705 void  j2k_write_float_to_float64 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1706 {
1707         OPJ_BYTE * l_dest_data = (OPJ_BYTE *) p_dest_data;
1708         OPJ_FLOAT32 * l_src_data = (OPJ_FLOAT32 *) p_src_data;
1709         OPJ_UINT32 i;
1710         OPJ_FLOAT64 l_temp;
1711
1712         for (i=0;i<p_nb_elem;++i) {
1713                 l_temp = (OPJ_FLOAT64) *(l_src_data++);
1714
1715                 opj_write_double(l_dest_data,l_temp);
1716
1717                 l_dest_data+=sizeof(OPJ_FLOAT64);
1718         }
1719 }
1720
1721
1722 /**
1723  * Converts an enum type progression order to string type.
1724  *
1725  * @param prg_order             the progression order to get.
1726  *
1727  * @return      the string representation of the given progression order.
1728  */
1729 char *j2k_convert_progression_order(OPJ_PROG_ORDER prg_order){
1730         j2k_prog_order_t *po;
1731         for(po = j2k_prog_order_list; po->enum_prog != -1; po++ ){
1732                 if(po->enum_prog == prg_order){
1733                         return po->str_prog;
1734                 }
1735         }
1736         return po->str_prog;
1737 }
1738
1739 /**
1740  * Checks the progression order changes values. Tells if the poc given as input are valid.
1741  *
1742  * @param       p_pocs                          the progression order changes.
1743  * @param       p_nb_pocs                       the number of progression order changes.
1744  * @param       p_nb_resolutions        the number of resolutions.
1745  * @param       numcomps                        the number of components
1746  * @param       numlayers                       the number of layers.
1747  * @param       p_manager                       the user event manager.
1748  *
1749  * @return      true if the pocs are valid.
1750  */
1751 opj_bool j2k_check_poc_val(     const opj_poc_t *p_pocs,
1752                                                         OPJ_UINT32 p_nb_pocs,
1753                                                         OPJ_UINT32 p_nb_resolutions,
1754                                                         OPJ_UINT32 p_num_comps,
1755                                                         OPJ_UINT32 p_num_layers,
1756                                                         opj_event_mgr_t * p_manager)
1757 {
1758         OPJ_UINT32* packet_array;
1759         OPJ_UINT32 index , resno, compno, layno;
1760         OPJ_UINT32 i;
1761         OPJ_UINT32 step_c = 1;
1762         OPJ_UINT32 step_r = p_num_comps * step_c;
1763         OPJ_UINT32 step_l = p_nb_resolutions * step_r;
1764         opj_bool loss = OPJ_FALSE;
1765         OPJ_UINT32 layno0 = 0;
1766
1767         packet_array = (OPJ_UINT32*) opj_calloc(step_l * p_num_layers, sizeof(OPJ_UINT32));
1768         if (packet_array == 00) {
1769                 opj_event_msg_v2(p_manager , EVT_ERROR, "Not enough memory for checking the poc values.\n");
1770                 return OPJ_FALSE;
1771         }
1772         memset(packet_array,0,step_l * p_num_layers* sizeof(OPJ_UINT32));
1773
1774         if (p_nb_pocs == 0) {
1775                 return OPJ_TRUE;
1776         }
1777
1778         index = step_r * p_pocs->resno0;
1779         // take each resolution for each poc
1780         for (resno = p_pocs->resno0 ; resno < p_pocs->resno1 ; ++resno)
1781         {
1782                 OPJ_UINT32 res_index = index + p_pocs->compno0 * step_c;
1783
1784                 // take each comp of each resolution for each poc
1785                 for (compno = p_pocs->compno0 ; compno < p_pocs->compno1 ; ++compno) {
1786                         OPJ_UINT32 comp_index = res_index + layno0 * step_l;
1787
1788                         // and finally take each layer of each res of ...
1789                         for (layno = layno0; layno < p_pocs->layno1 ; ++layno) {
1790                                 //index = step_r * resno + step_c * compno + step_l * layno;
1791                                 packet_array[comp_index] = 1;
1792                                 comp_index += step_l;
1793                         }
1794
1795                         res_index += step_c;
1796                 }
1797
1798                 index += step_r;
1799         }
1800         ++p_pocs;
1801
1802         // iterate through all the pocs
1803         for (i = 1; i < p_nb_pocs ; ++i) {
1804                 OPJ_UINT32 l_last_layno1 = (p_pocs-1)->layno1 ;
1805
1806                 layno0 = (p_pocs->layno1 > l_last_layno1)? l_last_layno1 : 0;
1807                 index = step_r * p_pocs->resno0;
1808
1809                 // take each resolution for each poc
1810                 for (resno = p_pocs->resno0 ; resno < p_pocs->resno1 ; ++resno) {
1811                         OPJ_UINT32 res_index = index + p_pocs->compno0 * step_c;
1812
1813                         // take each comp of each resolution for each poc
1814                         for (compno = p_pocs->compno0 ; compno < p_pocs->compno1 ; ++compno) {
1815                                 OPJ_UINT32 comp_index = res_index + layno0 * step_l;
1816
1817                                 // and finally take each layer of each res of ...
1818                                 for (layno = layno0; layno < p_pocs->layno1 ; ++layno) {
1819                                         //index = step_r * resno + step_c * compno + step_l * layno;
1820                                         packet_array[comp_index] = 1;
1821                                         comp_index += step_l;
1822                                 }
1823
1824                                 res_index += step_c;
1825                         }
1826
1827                         index += step_r;
1828                 }
1829
1830                 ++p_pocs;
1831         }
1832
1833         index = 0;
1834         for (layno = 0; layno < p_num_layers ; ++layno) {
1835                 for (resno = 0; resno < p_nb_resolutions; ++resno) {
1836                         for (compno = 0; compno < p_num_comps; ++compno) {
1837                                 loss |= (packet_array[index]!=1);
1838                                 //index = step_r * resno + step_c * compno + step_l * layno;
1839                                 index += step_c;
1840                         }
1841                 }
1842         }
1843
1844         if (loss) {
1845                 opj_event_msg_v2(p_manager , EVT_ERROR, "Missing packets possible loss of data\n");
1846         }
1847
1848         opj_free(packet_array);
1849
1850         return !loss;
1851 }
1852
1853 /* ----------------------------------------------------------------------- */
1854 static int j2k_get_num_tp(opj_cp_t *cp,int pino,int tileno){
1855         char *prog;
1856         int i;
1857         int tpnum=1,tpend=0;
1858         opj_tcp_t *tcp = &cp->tcps[tileno];
1859         prog = j2k_convert_progression_order(tcp->prg);
1860         
1861         if(cp->tp_on == 1){
1862                 for(i=0;i<4;i++){
1863                         if(tpend!=1){
1864                                 if( cp->tp_flag == prog[i] ){
1865                                         tpend=1;cp->tp_pos=i;
1866                                 }
1867                                 switch(prog[i]){
1868                                 case 'C':
1869                                         tpnum= tpnum * tcp->pocs[pino].compE;
1870                                         break;
1871                                 case 'R':
1872                                         tpnum= tpnum * tcp->pocs[pino].resE;
1873                                         break;
1874                                 case 'P':
1875                                         tpnum= tpnum * tcp->pocs[pino].prcE;
1876                                         break;
1877                                 case 'L':
1878                                         tpnum= tpnum * tcp->pocs[pino].layE;
1879                                         break;
1880                                 }
1881                         }
1882                 }
1883         }else{
1884                 tpnum=1;
1885         }
1886         return tpnum;
1887 }
1888
1889 /**
1890  * Gets the number of tile parts used for the given change of progression (if any) and the given tile.
1891  *
1892  * @param               cp                      the coding parameters.
1893  * @param               pino            the offset of the given poc (i.e. its position in the coding parameter).
1894  * @param               tileno          the given tile.
1895  *
1896  * @return              the number of tile parts.
1897  */
1898 OPJ_UINT32 j2k_get_num_tp_v2(opj_cp_v2_t *cp, OPJ_UINT32 pino, OPJ_UINT32 tileno)
1899 {
1900         const OPJ_CHAR *prog = 00;
1901         OPJ_UINT32 i;
1902         OPJ_UINT32 tpnum = 1;
1903         opj_tcp_v2_t *tcp = 00;
1904         opj_poc_t * l_current_poc = 00;
1905
1906         /*  preconditions */
1907         assert(tileno < (cp->tw * cp->th));
1908         assert(pino < (cp->tcps[tileno].numpocs + 1));
1909
1910         /* get the given tile coding parameter */
1911         tcp = &cp->tcps[tileno];
1912         assert(tcp != 00);
1913
1914         l_current_poc = &(tcp->pocs[pino]);
1915         assert(l_current_poc != 0);
1916
1917         /* get the progression order as a character string */
1918         prog = j2k_convert_progression_order(tcp->prg);
1919         assert(strlen(prog) > 0);
1920
1921         if (cp->m_specific_param.m_enc.m_tp_on == 1) {
1922                 for (i=0;i<4;++i) {
1923                         switch (prog[i])
1924                         {
1925                                 /* component wise */
1926                                 case 'C':
1927                                         tpnum *= l_current_poc->compE;
1928                                         break;
1929                                 /* resolution wise */
1930                                 case 'R':
1931                                         tpnum *= l_current_poc->resE;
1932                                         break;
1933                                 /* precinct wise */
1934                                 case 'P':
1935                                         tpnum *= l_current_poc->prcE;
1936                                         break;
1937                                 /* layer wise */
1938                                 case 'L':
1939                                         tpnum *= l_current_poc->layE;
1940                                         break;
1941                         }
1942                         /* whould we split here ? */
1943                         if ( cp->m_specific_param.m_enc.m_tp_flag == prog[i] ) {
1944                                 cp->m_specific_param.m_enc.m_tp_pos=i;
1945                                 break;
1946                         }
1947                 }
1948         }
1949         else {
1950                 tpnum=1;
1951         }
1952
1953         return tpnum;
1954 }
1955
1956 /**     mem allocation for TLM marker*/
1957 int j2k_calculate_tp(opj_cp_t *cp,int img_numcomp,opj_image_t *image,opj_j2k_t *j2k ){
1958         int pino,tileno,totnum_tp=0;
1959
1960         OPJ_ARG_NOT_USED(img_numcomp);
1961
1962         j2k->cur_totnum_tp = (int *) opj_malloc(cp->tw * cp->th * sizeof(int));
1963         for (tileno = 0; tileno < cp->tw * cp->th; tileno++) {
1964                 int cur_totnum_tp = 0;
1965                 opj_tcp_t *tcp = &cp->tcps[tileno];
1966                 for(pino = 0; pino <= tcp->numpocs; pino++) {
1967                         int tp_num=0;
1968                         opj_pi_iterator_t *pi = pi_initialise_encode(image, cp, tileno,FINAL_PASS);
1969                         if(!pi) { return -1;}
1970                         tp_num = j2k_get_num_tp(cp,pino,tileno);
1971                         totnum_tp = totnum_tp + tp_num;
1972                         cur_totnum_tp = cur_totnum_tp + tp_num;
1973                         pi_destroy(pi, cp, tileno);
1974                 }
1975                 j2k->cur_totnum_tp[tileno] = cur_totnum_tp;
1976                 /* INDEX >> */
1977                 if (j2k->cstr_info) {
1978                         j2k->cstr_info->tile[tileno].num_tps = cur_totnum_tp;
1979                         j2k->cstr_info->tile[tileno].tp = (opj_tp_info_t *) opj_malloc(cur_totnum_tp * sizeof(opj_tp_info_t));
1980                 }
1981                 /* << INDEX */
1982         }
1983         return totnum_tp;
1984 }
1985
1986 /**
1987  * Calculates the total number of tile parts needed by the encoder to
1988  * encode such an image. If not enough memory is available, then the function return false.
1989  *
1990  * @param       p_nb_tiles      pointer that will hold the number of tile parts.
1991  * @param       cp                      the coding parameters for the image.
1992  * @param       image           the image to encode.
1993  * @param       p_j2k                   the p_j2k encoder.
1994  * @param       p_manager       the user event manager.
1995  *
1996  * @return true if the function was successful, false else.
1997  */
1998 opj_bool j2k_calculate_tp_v2( opj_j2k_v2_t *p_j2k,
1999                                                           opj_cp_v2_t *cp,
2000                                                           OPJ_UINT32 * p_nb_tiles,
2001                                                           opj_image_t *image,
2002                                                           opj_event_mgr_t * p_manager)
2003 {
2004         OPJ_UINT32 pino,tileno;
2005         OPJ_UINT32 l_nb_tiles;
2006         opj_tcp_v2_t *tcp;
2007
2008         /* preconditions */
2009         assert(p_nb_tiles != 00);
2010         assert(cp != 00);
2011         assert(image != 00);
2012         assert(p_j2k != 00);
2013         assert(p_manager != 00);
2014
2015         l_nb_tiles = cp->tw * cp->th;
2016         * p_nb_tiles = 0;
2017         tcp = cp->tcps;
2018
2019         /* INDEX >> */
2020         /* TODO mergeV2: check this part which use cstr_info */
2021         /*if (p_j2k->cstr_info) {
2022                 opj_tile_info_t * l_info_tile_ptr = p_j2k->cstr_info->tile;
2023
2024                 for (tileno = 0; tileno < l_nb_tiles; ++tileno) {
2025                         OPJ_UINT32 cur_totnum_tp = 0;
2026
2027                         pi_update_encoding_parameters(image,cp,tileno);
2028
2029                         for (pino = 0; pino <= tcp->numpocs; ++pino)
2030                         {
2031                                 OPJ_UINT32 tp_num = j2k_get_num_tp_v2(cp,pino,tileno);
2032
2033                                 *p_nb_tiles = *p_nb_tiles + tp_num;
2034
2035                                 cur_totnum_tp += tp_num;
2036                         }
2037
2038                         tcp->m_nb_tile_parts = cur_totnum_tp;
2039
2040                         l_info_tile_ptr->tp = (opj_tp_info_t *) opj_malloc(cur_totnum_tp * sizeof(opj_tp_info_t));
2041                         if (l_info_tile_ptr->tp == 00) {
2042                                 return OPJ_FALSE;
2043                         }
2044
2045                         memset(l_info_tile_ptr->tp,0,cur_totnum_tp * sizeof(opj_tp_info_t));
2046
2047                         l_info_tile_ptr->num_tps = cur_totnum_tp;
2048
2049                         ++l_info_tile_ptr;
2050                         ++tcp;
2051                 }
2052         }
2053         else */{
2054                 for (tileno = 0; tileno < l_nb_tiles; ++tileno) {
2055                         OPJ_UINT32 cur_totnum_tp = 0;
2056
2057                         pi_update_encoding_parameters(image,cp,tileno);
2058
2059                         for (pino = 0; pino <= tcp->numpocs; ++pino) {
2060                                 OPJ_UINT32 tp_num = j2k_get_num_tp_v2(cp,pino,tileno);
2061
2062                                 *p_nb_tiles = *p_nb_tiles + tp_num;
2063
2064                                 cur_totnum_tp += tp_num;
2065                         }
2066                         tcp->m_nb_tile_parts = cur_totnum_tp;
2067
2068                         ++tcp;
2069                 }
2070         }
2071
2072         return OPJ_TRUE;
2073 }
2074
2075 static void j2k_write_soc(opj_j2k_t *j2k) {
2076         opj_cio_t *cio = j2k->cio;
2077         cio_write(cio, J2K_MS_SOC, 2);
2078
2079         if(j2k->cstr_info)
2080           j2k_add_mhmarker(j2k->cstr_info, J2K_MS_SOC, cio_tell(cio), 0);
2081
2082 /* UniPG>> */
2083 #ifdef USE_JPWL
2084
2085         /* update markers struct */
2086         j2k_add_marker(j2k->cstr_info, J2K_MS_SOC, cio_tell(cio) - 2, 2);
2087 #endif /* USE_JPWL */
2088 /* <<UniPG */
2089 }
2090
2091 /**
2092  * Writes the SOC marker (Start Of Codestream)
2093  *
2094  * @param       p_stream                        the stream to write data to.
2095  * @param       p_j2k                   J2K codec.
2096  * @param       p_manager       the user event manager.
2097 */
2098 opj_bool j2k_write_soc_v2(      opj_j2k_v2_t *p_j2k,
2099                                                         struct opj_stream_private *p_stream,
2100                                                         struct opj_event_mgr * p_manager )
2101 {
2102         /* 2 bytes will be written */
2103         OPJ_BYTE * l_start_stream = 00;
2104
2105         /* preconditions */
2106         assert(p_stream != 00);
2107         assert(p_j2k != 00);
2108         assert(p_manager != 00);
2109
2110         l_start_stream = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
2111
2112         /* write SOC identifier */
2113         opj_write_bytes(l_start_stream,J2K_MS_SOC,2);
2114
2115         if (opj_stream_write_data(p_stream,l_start_stream,2,p_manager) != 2) {
2116                 return OPJ_FALSE;
2117         }
2118
2119 /* UniPG>> */
2120 #ifdef USE_JPWL
2121         /* update markers struct */
2122 /*
2123         j2k_add_marker(p_j2k->cstr_info, J2K_MS_SOC, p_stream_tell(p_stream) - 2, 2);
2124 */
2125   assert( 0 && "TODO" );
2126 #endif /* USE_JPWL */
2127 /* <<UniPG */
2128
2129         return OPJ_TRUE;
2130 }
2131
2132 static void j2k_read_soc(opj_j2k_t *j2k) {      
2133         j2k->state = J2K_STATE_MHSIZ;
2134         /* Index */
2135         if (j2k->cstr_info) {
2136                 j2k->cstr_info->main_head_start = cio_tell(j2k->cio) - 2;
2137                 j2k->cstr_info->codestream_size = cio_numbytesleft(j2k->cio) + 2 - j2k->cstr_info->main_head_start;
2138         }
2139 }
2140
2141 /**
2142  * Reads a SOC marker (Start of Codestream)
2143  * @param       p_header_data   the data contained in the SOC box.
2144  * @param       jp2                             the jpeg2000 file codec.
2145  * @param       p_header_size   the size of the data contained in the SOC marker.
2146  * @param       p_manager               the user event manager.
2147 */
2148 static opj_bool j2k_read_soc_v2(        opj_j2k_v2_t *p_j2k,
2149                                                                         struct opj_stream_private *p_stream,
2150                                                                         struct opj_event_mgr * p_manager )
2151 {
2152         OPJ_BYTE l_data [2];
2153         OPJ_UINT32 l_marker;
2154
2155         /* preconditions */
2156         assert(p_j2k != 00);
2157         assert(p_manager != 00);
2158         assert(p_stream != 00);
2159
2160         if (opj_stream_read_data(p_stream,l_data,2,p_manager) != 2) {
2161                 return OPJ_FALSE;
2162         }
2163
2164         opj_read_bytes(l_data,&l_marker,2);
2165         if (l_marker != J2K_MS_SOC) {
2166                 return OPJ_FALSE;
2167         }
2168
2169         /* Next marker should be a SIZ marker in the main header */
2170         p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_MHSIZ;
2171
2172         /* FIXME move it in a index structure included in p_j2k*/
2173         p_j2k->cstr_index->main_head_start = opj_stream_tell(p_stream) - 2;
2174
2175         opj_event_msg_v2(p_manager, EVT_INFO, "Start to read j2k main header (%d).\n", p_j2k->cstr_index->main_head_start);
2176
2177         /* Add the marker to the codestream index*/
2178         j2k_add_mhmarker_v2(p_j2k->cstr_index, J2K_MS_SOC, p_j2k->cstr_index->main_head_start, 2);
2179
2180         return OPJ_TRUE;
2181 }
2182
2183 static void j2k_write_siz(opj_j2k_t *j2k) {
2184         OPJ_UINT32 i;
2185         int lenp, len;
2186
2187         opj_cio_t *cio = j2k->cio;
2188         opj_image_t *image = j2k->image;
2189         opj_cp_t *cp = j2k->cp;
2190
2191         cio_write(cio, J2K_MS_SIZ, 2);  /* SIZ */
2192         lenp = cio_tell(cio);
2193         cio_skip(cio, 2);
2194         cio_write(cio, cp->rsiz, 2);                    /* Rsiz (capabilities) */
2195         cio_write(cio, image->x1, 4);   /* Xsiz */
2196         cio_write(cio, image->y1, 4);   /* Ysiz */
2197         cio_write(cio, image->x0, 4);   /* X0siz */
2198         cio_write(cio, image->y0, 4);   /* Y0siz */
2199         cio_write(cio, cp->tdx, 4);             /* XTsiz */
2200         cio_write(cio, cp->tdy, 4);             /* YTsiz */
2201         cio_write(cio, cp->tx0, 4);             /* XT0siz */
2202         cio_write(cio, cp->ty0, 4);             /* YT0siz */
2203         cio_write(cio, image->numcomps, 2);     /* Csiz */
2204         for (i = 0; i < image->numcomps; i++) {
2205                 cio_write(cio, image->comps[i].prec - 1 + (image->comps[i].sgnd << 7), 1);      /* Ssiz_i */
2206                 cio_write(cio, image->comps[i].dx, 1);  /* XRsiz_i */
2207                 cio_write(cio, image->comps[i].dy, 1);  /* YRsiz_i */
2208         }
2209         len = cio_tell(cio) - lenp;
2210         cio_seek(cio, lenp);
2211         cio_write(cio, len, 2);         /* Lsiz */
2212         cio_seek(cio, lenp + len);
2213         
2214         if(j2k->cstr_info)
2215           j2k_add_mhmarker(j2k->cstr_info, J2K_MS_SIZ, lenp, len);
2216 }
2217
2218 /**
2219  * Writes the SIZ marker (image and tile size)
2220  *
2221  * @param       p_stream                        the stream to write data to.
2222  * @param       p_j2k                   J2K codec.
2223  * @param       p_manager       the user event manager.
2224 */
2225 opj_bool j2k_write_siz_v2(      opj_j2k_v2_t *p_j2k,
2226                                                         struct opj_stream_private *p_stream,
2227                                                         struct opj_event_mgr * p_manager )
2228 {
2229         OPJ_UINT32 i;
2230         OPJ_UINT32 l_size_len;
2231         OPJ_BYTE * l_current_ptr;
2232         opj_image_t * l_image = 00;
2233         opj_cp_v2_t *cp = 00;
2234         opj_image_comp_t * l_img_comp = 00;
2235
2236         /* preconditions */
2237         assert(p_stream != 00);
2238         assert(p_j2k != 00);
2239         assert(p_manager != 00);
2240
2241         l_image = p_j2k->m_private_image;
2242         cp = &(p_j2k->m_cp);
2243         l_size_len = 40 + 3 * l_image->numcomps;
2244         l_img_comp = l_image->comps;
2245
2246         if (l_size_len > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
2247
2248                 p_j2k->m_specific_param.m_encoder.m_header_tile_data
2249                         = (OPJ_BYTE*)opj_realloc(
2250                                 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
2251                                 l_size_len);
2252                 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
2253                         return OPJ_FALSE;
2254                 }
2255
2256                 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_size_len;
2257         }
2258
2259         l_current_ptr = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
2260
2261         /* write SOC identifier */
2262         opj_write_bytes(l_current_ptr,J2K_MS_SIZ,2);    /* SIZ */
2263         l_current_ptr+=2;
2264
2265         opj_write_bytes(l_current_ptr,l_size_len-2,2); /* L_SIZ */
2266         l_current_ptr+=2;
2267
2268         opj_write_bytes(l_current_ptr, cp->rsiz, 2);    /* Rsiz (capabilities) */
2269         l_current_ptr+=2;
2270
2271         opj_write_bytes(l_current_ptr, l_image->x1, 4); /* Xsiz */
2272         l_current_ptr+=4;
2273
2274         opj_write_bytes(l_current_ptr, l_image->y1, 4); /* Ysiz */
2275         l_current_ptr+=4;
2276
2277         opj_write_bytes(l_current_ptr, l_image->x0, 4); /* X0siz */
2278         l_current_ptr+=4;
2279
2280         opj_write_bytes(l_current_ptr, l_image->y0, 4); /* Y0siz */
2281         l_current_ptr+=4;
2282
2283         opj_write_bytes(l_current_ptr, cp->tdx, 4);             /* XTsiz */
2284         l_current_ptr+=4;
2285
2286         opj_write_bytes(l_current_ptr, cp->tdy, 4);             /* YTsiz */
2287         l_current_ptr+=4;
2288
2289         opj_write_bytes(l_current_ptr, cp->tx0, 4);             /* XT0siz */
2290         l_current_ptr+=4;
2291
2292         opj_write_bytes(l_current_ptr, cp->ty0, 4);             /* YT0siz */
2293         l_current_ptr+=4;
2294
2295         opj_write_bytes(l_current_ptr, l_image->numcomps, 2);   /* Csiz */
2296         l_current_ptr+=2;
2297
2298         for (i = 0; i < l_image->numcomps; ++i) {
2299                 /* TODO here with MCT ? */
2300                 opj_write_bytes(l_current_ptr, l_img_comp->prec - 1 + (l_img_comp->sgnd << 7), 1);      /* Ssiz_i */
2301                 ++l_current_ptr;
2302
2303                 opj_write_bytes(l_current_ptr, l_img_comp->dx, 1);      /* XRsiz_i */
2304                 ++l_current_ptr;
2305
2306                 opj_write_bytes(l_current_ptr, l_img_comp->dy, 1);      /* YRsiz_i */
2307                 ++l_current_ptr;
2308
2309                 ++l_img_comp;
2310         }
2311
2312         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) {
2313                 return OPJ_FALSE;
2314         }
2315
2316         return OPJ_TRUE;
2317 }
2318
2319 static void j2k_read_siz(opj_j2k_t *j2k) {
2320         int len;
2321         OPJ_UINT32 i;
2322         
2323         opj_cio_t *cio = j2k->cio;
2324         opj_image_t *image = j2k->image;
2325         opj_cp_t *cp = j2k->cp;
2326         
2327         len = cio_read(cio, 2);                 /* Lsiz */
2328         cio_read(cio, 2);                               /* Rsiz (capabilities) */
2329         image->x1 = cio_read(cio, 4);   /* Xsiz */
2330         image->y1 = cio_read(cio, 4);   /* Ysiz */
2331         image->x0 = cio_read(cio, 4);   /* X0siz */
2332         image->y0 = cio_read(cio, 4);   /* Y0siz */
2333         cp->tdx = cio_read(cio, 4);             /* XTsiz */
2334         cp->tdy = cio_read(cio, 4);             /* YTsiz */
2335         cp->tx0 = cio_read(cio, 4);             /* XT0siz */
2336         cp->ty0 = cio_read(cio, 4);             /* YT0siz */
2337         
2338   /* the following code triggers: */
2339   /* warning: comparison of unsigned expression < 0 is always false */
2340 #if 0
2341         if ((image->x0<0)||(image->x1<0)||(image->y0<0)||(image->y1<0)) {
2342                 opj_event_msg(j2k->cinfo, EVT_ERROR,
2343                                                                         "%s: invalid image size (x0:%d, x1:%d, y0:%d, y1:%d)\n",
2344                                                                         image->x0,image->x1,image->y0,image->y1);
2345                 return;
2346         }
2347 #endif
2348         
2349         image->numcomps = cio_read(cio, 2);     /* Csiz */
2350
2351 #ifdef USE_JPWL
2352         if (j2k->cp->correct) {
2353                 /* if JPWL is on, we check whether TX errors have damaged
2354                   too much the SIZ parameters */
2355                 if (!(image->x1 * image->y1)) {
2356                         opj_event_msg(j2k->cinfo, EVT_ERROR,
2357                                 "JPWL: bad image size (%d x %d)\n",
2358                                 image->x1, image->y1);
2359                         if (!JPWL_ASSUME || JPWL_ASSUME) {
2360                                 opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
2361                                 return;
2362                         }
2363                 }
2364     assert( len >= 38 );
2365                 if (image->numcomps != (OPJ_UINT32)((len - 38) / 3)) {
2366                         opj_event_msg(j2k->cinfo, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
2367                                 "JPWL: Csiz is %d => space in SIZ only for %d comps.!!!\n",
2368                                 image->numcomps, ((len - 38) / 3));
2369                         if (!JPWL_ASSUME) {
2370                                 opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
2371                                 return;
2372                         }
2373                         /* we try to correct */
2374                         opj_event_msg(j2k->cinfo, EVT_WARNING, "- trying to adjust this\n");
2375                         if (image->numcomps < (OPJ_UINT32)((len - 38) / 3)) {
2376                                 len = 38 + 3 * image->numcomps;
2377                                 opj_event_msg(j2k->cinfo, EVT_WARNING, "- setting Lsiz to %d => HYPOTHESIS!!!\n",
2378                                         len);                           
2379                         } else {
2380                                 image->numcomps = ((len - 38) / 3);
2381                                 opj_event_msg(j2k->cinfo, EVT_WARNING, "- setting Csiz to %d => HYPOTHESIS!!!\n",
2382                                         image->numcomps);                               
2383                         }
2384                 }
2385
2386                 /* update components number in the jpwl_exp_comps filed */
2387                 cp->exp_comps = image->numcomps;
2388         }
2389 #endif /* USE_JPWL */
2390
2391         image->comps = (opj_image_comp_t*) opj_calloc(image->numcomps, sizeof(opj_image_comp_t));
2392         for (i = 0; i < image->numcomps; i++) {
2393                 int tmp, w, h;
2394                 tmp = cio_read(cio, 1);         /* Ssiz_i */
2395                 image->comps[i].prec = (tmp & 0x7f) + 1;
2396                 image->comps[i].sgnd = tmp >> 7;
2397                 image->comps[i].dx = cio_read(cio, 1);  /* XRsiz_i */
2398                 image->comps[i].dy = cio_read(cio, 1);  /* YRsiz_i */
2399                 
2400 #ifdef USE_JPWL
2401                 if (j2k->cp->correct) {
2402                 /* if JPWL is on, we check whether TX errors have damaged
2403                         too much the SIZ parameters, again */
2404                         if (!(image->comps[i].dx * image->comps[i].dy)) {
2405                                 opj_event_msg(j2k->cinfo, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
2406                                         "JPWL: bad XRsiz_%d/YRsiz_%d (%d x %d)\n",
2407                                         i, i, image->comps[i].dx, image->comps[i].dy);
2408                                 if (!JPWL_ASSUME) {
2409                                         opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
2410                                         return;
2411                                 }
2412                                 /* we try to correct */
2413                                 opj_event_msg(j2k->cinfo, EVT_WARNING, "- trying to adjust them\n");
2414                                 if (!image->comps[i].dx) {
2415                                         image->comps[i].dx = 1;
2416                                         opj_event_msg(j2k->cinfo, EVT_WARNING, "- setting XRsiz_%d to %d => HYPOTHESIS!!!\n",
2417                                                 i, image->comps[i].dx);
2418                                 }
2419                                 if (!image->comps[i].dy) {
2420                                         image->comps[i].dy = 1;
2421                                         opj_event_msg(j2k->cinfo, EVT_WARNING, "- setting YRsiz_%d to %d => HYPOTHESIS!!!\n",
2422                                                 i, image->comps[i].dy);
2423                                 }
2424                         }
2425                         
2426                 }
2427 #endif /* USE_JPWL */
2428
2429                 /* TODO: unused ? */
2430                 w = int_ceildiv(image->x1 - image->x0, image->comps[i].dx);
2431                 h = int_ceildiv(image->y1 - image->y0, image->comps[i].dy);
2432
2433                 image->comps[i].resno_decoded = 0;      /* number of resolution decoded */
2434                 image->comps[i].factor = cp->reduce; /* reducing factor per component */
2435         }
2436         
2437         cp->tw = int_ceildiv(image->x1 - cp->tx0, cp->tdx);
2438         cp->th = int_ceildiv(image->y1 - cp->ty0, cp->tdy);
2439
2440 #ifdef USE_JPWL
2441         if (j2k->cp->correct) {
2442                 /* if JPWL is on, we check whether TX errors have damaged
2443                   too much the SIZ parameters */
2444                 if ((cp->tw < 1) || (cp->th < 1) || (cp->tw > cp->max_tiles) || (cp->th > cp->max_tiles)) {
2445                         opj_event_msg(j2k->cinfo, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
2446                                 "JPWL: bad number of tiles (%d x %d)\n",
2447                                 cp->tw, cp->th);
2448                         if (!JPWL_ASSUME) {
2449                                 opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
2450                                 return;
2451                         }
2452                         /* we try to correct */
2453                         opj_event_msg(j2k->cinfo, EVT_WARNING, "- trying to adjust them\n");
2454                         if (cp->tw < 1) {
2455                                 cp->tw= 1;
2456                                 opj_event_msg(j2k->cinfo, EVT_WARNING, "- setting %d tiles in x => HYPOTHESIS!!!\n",
2457                                         cp->tw);
2458                         }
2459                         if (cp->tw > cp->max_tiles) {
2460                                 cp->tw= 1;
2461                                 opj_event_msg(j2k->cinfo, EVT_WARNING, "- too large x, increase expectance of %d\n"
2462                                         "- setting %d tiles in x => HYPOTHESIS!!!\n",
2463                                         cp->max_tiles, cp->tw);
2464                         }
2465                         if (cp->th < 1) {
2466                                 cp->th= 1;
2467                                 opj_event_msg(j2k->cinfo, EVT_WARNING, "- setting %d tiles in y => HYPOTHESIS!!!\n",
2468                                         cp->th);
2469                         }
2470                         if (cp->th > cp->max_tiles) {
2471                                 cp->th= 1;
2472                                 opj_event_msg(j2k->cinfo, EVT_WARNING, "- too large y, increase expectance of %d to continue\n",
2473                                         "- setting %d tiles in y => HYPOTHESIS!!!\n",
2474                                         cp->max_tiles, cp->th);
2475                         }
2476                 }
2477         }
2478 #endif /* USE_JPWL */
2479
2480         cp->tcps = (opj_tcp_t*) opj_calloc(cp->tw * cp->th, sizeof(opj_tcp_t));
2481         cp->tileno = (int*) opj_malloc(cp->tw * cp->th * sizeof(int));
2482         cp->tileno_size = 0;
2483         
2484 #ifdef USE_JPWL
2485         if (j2k->cp->correct) {
2486                 if (!cp->tcps) {
2487                         opj_event_msg(j2k->cinfo, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
2488                                 "JPWL: could not alloc tcps field of cp\n");
2489                         if (!JPWL_ASSUME || JPWL_ASSUME) {
2490                                 opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
2491                                 return;
2492                         }
2493                 }
2494         }
2495 #endif /* USE_JPWL */
2496
2497         for (i = 0; i < (OPJ_UINT32)cp->tw * cp->th; i++) {
2498                 cp->tcps[i].POC = 0;
2499                 cp->tcps[i].numpocs = 0;
2500                 cp->tcps[i].first = 1;
2501         }
2502         
2503         /* Initialization for PPM marker */
2504         cp->ppm = 0;
2505         cp->ppm_data = NULL;
2506         cp->ppm_data_first = NULL;
2507         cp->ppm_previous = 0;
2508         cp->ppm_store = 0;
2509
2510         j2k->default_tcp->tccps = (opj_tccp_t*) opj_calloc(image->numcomps, sizeof(opj_tccp_t));
2511         for (i = 0; i < (OPJ_UINT32)cp->tw * cp->th; i++) {
2512                 cp->tcps[i].tccps = (opj_tccp_t*) opj_malloc(image->numcomps * sizeof(opj_tccp_t));
2513         }       
2514         j2k->tile_data = (unsigned char**) opj_calloc(cp->tw * cp->th, sizeof(unsigned char*));
2515         j2k->tile_len = (int*) opj_calloc(cp->tw * cp->th, sizeof(int));
2516         j2k->state = J2K_STATE_MH;
2517
2518         /* Index */
2519         if (j2k->cstr_info) {
2520                 opj_codestream_info_t *cstr_info = j2k->cstr_info;
2521                 cstr_info->image_w = image->x1 - image->x0;
2522                 cstr_info->image_h = image->y1 - image->y0;
2523                 cstr_info->numcomps = image->numcomps;
2524                 cstr_info->tw = cp->tw;
2525                 cstr_info->th = cp->th;
2526                 cstr_info->tile_x = cp->tdx;    
2527                 cstr_info->tile_y = cp->tdy;    
2528                 cstr_info->tile_Ox = cp->tx0;   
2529                 cstr_info->tile_Oy = cp->ty0;                   
2530                 cstr_info->tile = (opj_tile_info_t*) opj_calloc(cp->tw * cp->th, sizeof(opj_tile_info_t));              
2531         }
2532 }
2533
2534
2535 /**
2536  * Reads a SIZ marker (image and tile size)
2537  * @param       p_header_data   the data contained in the SIZ box.
2538  * @param       jp2                             the jpeg2000 file codec.
2539  * @param       p_header_size   the size of the data contained in the SIZ marker.
2540  * @param       p_manager               the user event manager.
2541 */
2542 opj_bool j2k_read_siz_v2 (
2543                                     opj_j2k_v2_t *p_j2k,
2544                                         OPJ_BYTE * p_header_data,
2545                                         OPJ_UINT32 p_header_size,
2546                                         struct opj_event_mgr * p_manager
2547                                         )
2548 {
2549         OPJ_UINT32 l_size, i;
2550         OPJ_UINT32 l_nb_comp;
2551         OPJ_UINT32 l_nb_comp_remain;
2552         OPJ_UINT32 l_remaining_size;
2553         OPJ_UINT32 l_nb_tiles;
2554         OPJ_UINT32 l_tmp;
2555         opj_image_t *l_image = 00;
2556         opj_cp_v2_t *l_cp = 00;
2557         opj_image_comp_t * l_img_comp = 00;
2558         opj_tcp_v2_t * l_current_tile_param = 00;
2559
2560         /* preconditions */
2561         assert(p_j2k != 00);
2562         assert(p_manager != 00);
2563         assert(p_header_data != 00);
2564
2565         l_image = p_j2k->m_private_image;
2566         l_cp = &(p_j2k->m_cp);
2567
2568         /* minimum size == 39 - 3 (= minimum component parameter) */
2569         if (p_header_size < 36) {
2570                 opj_event_msg_v2(p_manager, EVT_ERROR, "Error with SIZ marker size\n");
2571                 return OPJ_FALSE;
2572         }
2573
2574         l_remaining_size = p_header_size - 36;
2575         l_nb_comp = l_remaining_size / 3;
2576         l_nb_comp_remain = l_remaining_size % 3;
2577         if (l_nb_comp_remain != 0){
2578                 opj_event_msg_v2(p_manager, EVT_ERROR, "Error with SIZ marker size\n");
2579                 return OPJ_FALSE;
2580         }
2581
2582         l_size = p_header_size + 2;                                                                             /* Lsiz */
2583
2584         opj_read_bytes(p_header_data,&l_tmp ,2);                                                /* Rsiz (capabilities) */
2585         p_header_data+=2;
2586         l_cp->rsiz = (OPJ_RSIZ_CAPABILITIES) l_tmp;
2587         opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_image->x1, 4);   /* Xsiz */
2588         p_header_data+=4;
2589         opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_image->y1, 4);   /* Ysiz */
2590         p_header_data+=4;
2591         opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_image->x0, 4);   /* X0siz */
2592         p_header_data+=4;
2593         opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_image->y0, 4);   /* Y0siz */
2594         p_header_data+=4;
2595         opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_cp->tdx, 4);             /* XTsiz */
2596         p_header_data+=4;
2597         opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_cp->tdy, 4);             /* YTsiz */
2598         p_header_data+=4;
2599         opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_cp->tx0, 4);             /* XT0siz */
2600         p_header_data+=4;
2601         opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_cp->ty0, 4);             /* YT0siz */
2602         p_header_data+=4;
2603         opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_tmp, 2);                 /* Csiz */
2604         p_header_data+=2;
2605         if (l_tmp < 16385)
2606                 l_image->numcomps = (OPJ_UINT16) l_tmp;
2607         else {
2608                 opj_event_msg_v2(p_manager, EVT_ERROR, "Error with SIZ marker: number of component is illegal -> %d\n", l_tmp);
2609                 return OPJ_FALSE;
2610         }
2611
2612         if (l_image->numcomps != l_nb_comp) {
2613                 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);
2614                 return OPJ_FALSE;
2615         }
2616
2617 #ifdef USE_JPWL
2618         if (l_cp->correct) {
2619                 /* if JPWL is on, we check whether TX errors have damaged
2620                   too much the SIZ parameters */
2621                 if (!(l_image->x1 * l_image->y1)) {
2622                         opj_event_msg_v2(p_manager, EVT_ERROR,
2623                                 "JPWL: bad image size (%d x %d)\n",
2624                                 l_image->x1, l_image->y1);
2625                         if (!JPWL_ASSUME || JPWL_ASSUME) {
2626                                 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
2627                                 return OPJ_FALSE;
2628                         }
2629                 }
2630
2631         /* FIXME check previously in the function so why keep this piece of code ? Need by the norm ?
2632                 if (l_image->numcomps != ((len - 38) / 3)) {
2633                         opj_event_msg_v2(p_manager, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
2634                                 "JPWL: Csiz is %d => space in SIZ only for %d comps.!!!\n",
2635                                 l_image->numcomps, ((len - 38) / 3));
2636                         if (!JPWL_ASSUME) {
2637                                 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
2638                                 return OPJ_FALSE;
2639                         }
2640         */              /* we try to correct */
2641         /*              opj_event_msg_v2(p_manager, EVT_WARNING, "- trying to adjust this\n");
2642                         if (l_image->numcomps < ((len - 38) / 3)) {
2643                                 len = 38 + 3 * l_image->numcomps;
2644                                 opj_event_msg_v2(p_manager, EVT_WARNING, "- setting Lsiz to %d => HYPOTHESIS!!!\n",
2645                                         len);
2646                         } else {
2647                                 l_image->numcomps = ((len - 38) / 3);
2648                                 opj_event_msg_v2(p_manager, EVT_WARNING, "- setting Csiz to %d => HYPOTHESIS!!!\n",
2649                                         l_image->numcomps);
2650                         }
2651                 }
2652         */
2653
2654                 /* update components number in the jpwl_exp_comps filed */
2655                 l_cp->exp_comps = l_image->numcomps;
2656         }
2657 #endif /* USE_JPWL */
2658
2659         /* Allocate the resulting image components */
2660         l_image->comps = (opj_image_comp_t*) opj_calloc(l_image->numcomps, sizeof(opj_image_comp_t));
2661         if (l_image->comps == 00){
2662                 l_image->numcomps = 0;
2663                 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to take in charge SIZ marker\n");
2664                 return OPJ_FALSE;
2665         }
2666
2667         memset(l_image->comps,0,l_image->numcomps * sizeof(opj_image_comp_t));
2668         l_img_comp = l_image->comps;
2669
2670         /* Read the component information */
2671         for (i = 0; i < l_image->numcomps; ++i){
2672                 OPJ_UINT32 tmp;
2673                 opj_read_bytes(p_header_data,&tmp,1);   /* Ssiz_i */
2674                 ++p_header_data;
2675                 l_img_comp->prec = (tmp & 0x7f) + 1;
2676                 l_img_comp->sgnd = tmp >> 7;
2677                 opj_read_bytes(p_header_data,&tmp,1);   /* XRsiz_i */
2678                 ++p_header_data;
2679                 l_img_comp->dx = (OPJ_INT32)tmp; /* should be between 1 and 255 */
2680                 opj_read_bytes(p_header_data,&tmp,1);   /* YRsiz_i */
2681                 ++p_header_data;
2682                 l_img_comp->dy = (OPJ_INT32)tmp; /* should be between 1 and 255 */
2683
2684 #ifdef USE_JPWL
2685                 if (l_cp->correct) {
2686                 /* if JPWL is on, we check whether TX errors have damaged
2687                         too much the SIZ parameters, again */
2688                         if (!(l_image->comps[i].dx * l_image->comps[i].dy)) {
2689                                 opj_event_msg_v2(p_manager, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
2690                                         "JPWL: bad XRsiz_%d/YRsiz_%d (%d x %d)\n",
2691                                         i, i, l_image->comps[i].dx, l_image->comps[i].dy);
2692                                 if (!JPWL_ASSUME) {
2693                                         opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
2694                                         return OPJ_FALSE;
2695                                 }
2696                                 /* we try to correct */
2697                                 opj_event_msg_v2(p_manager, EVT_WARNING, "- trying to adjust them\n");
2698                                 if (!l_image->comps[i].dx) {
2699                                         l_image->comps[i].dx = 1;
2700                                         opj_event_msg_v2(p_manager, EVT_WARNING, "- setting XRsiz_%d to %d => HYPOTHESIS!!!\n",
2701                                                 i, l_image->comps[i].dx);
2702                                 }
2703                                 if (!l_image->comps[i].dy) {
2704                                         l_image->comps[i].dy = 1;
2705                                         opj_event_msg_v2(p_manager, EVT_WARNING, "- setting YRsiz_%d to %d => HYPOTHESIS!!!\n",
2706                                                 i, l_image->comps[i].dy);
2707                                 }
2708                         }
2709                 }
2710 #endif /* USE_JPWL */
2711                 l_img_comp->resno_decoded = 0;                                                          /* number of resolution decoded */
2712                 l_img_comp->factor = l_cp->m_specific_param.m_dec.m_reduce; /* reducing factor per component */
2713                 ++l_img_comp;
2714         }
2715
2716         /* Compute the number of tiles */
2717         l_cp->tw = int_ceildiv(l_image->x1 - l_cp->tx0, l_cp->tdx);
2718         l_cp->th = int_ceildiv(l_image->y1 - l_cp->ty0, l_cp->tdy);
2719         l_nb_tiles = l_cp->tw * l_cp->th;
2720
2721         /* Define the tiles which will be decoded */
2722         if (p_j2k->m_specific_param.m_decoder.m_discard_tiles) {
2723                 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;
2724                 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;
2725                 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);
2726                 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);
2727         }
2728         else {
2729                 p_j2k->m_specific_param.m_decoder.m_start_tile_x = 0;
2730                 p_j2k->m_specific_param.m_decoder.m_start_tile_y = 0;
2731                 p_j2k->m_specific_param.m_decoder.m_end_tile_x = l_cp->tw;
2732                 p_j2k->m_specific_param.m_decoder.m_end_tile_y = l_cp->th;
2733         }
2734
2735 #ifdef USE_JPWL
2736         if (l_cp->correct) {
2737                 /* if JPWL is on, we check whether TX errors have damaged
2738                   too much the SIZ parameters */
2739                 if ((l_cp->tw < 1) || (l_cp->th < 1) || (l_cp->tw > l_cp->max_tiles) || (l_cp->th > l_cp->max_tiles)) {
2740                         opj_event_msg_v2(p_manager, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
2741                                 "JPWL: bad number of tiles (%d x %d)\n",
2742                                 l_cp->tw, l_cp->th);
2743                         if (!JPWL_ASSUME) {
2744                                 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
2745                                 return OPJ_FALSE;
2746                         }
2747                         /* we try to correct */
2748                         opj_event_msg_v2(p_manager, EVT_WARNING, "- trying to adjust them\n");
2749                         if (l_cp->tw < 1) {
2750                                 l_cp->tw= 1;
2751                                 opj_event_msg_v2(p_manager, EVT_WARNING, "- setting %d tiles in x => HYPOTHESIS!!!\n",
2752                                                 l_cp->tw);
2753                         }
2754                         if (l_cp->tw > l_cp->max_tiles) {
2755                                 l_cp->tw= 1;
2756                                 opj_event_msg_v2(p_manager, EVT_WARNING, "- too large x, increase expectance of %d\n"
2757                                         "- setting %d tiles in x => HYPOTHESIS!!!\n",
2758                                         l_cp->max_tiles, l_cp->tw);
2759                         }
2760                         if (l_cp->th < 1) {
2761                                 l_cp->th= 1;
2762                                 opj_event_msg_v2(p_manager, EVT_WARNING, "- setting %d tiles in y => HYPOTHESIS!!!\n",
2763                                                 l_cp->th);
2764                         }
2765                         if (l_cp->th > l_cp->max_tiles) {
2766                                 l_cp->th= 1;
2767                                 opj_event_msg_v2(p_manager, EVT_WARNING, "- too large y, increase expectance of %d to continue\n",
2768                                         "- setting %d tiles in y => HYPOTHESIS!!!\n",
2769                                         l_cp->max_tiles, l_cp->th);
2770                         }
2771                 }
2772         }
2773 #endif /* USE_JPWL */
2774
2775         /* memory allocations */
2776         l_cp->tcps = (opj_tcp_v2_t*) opj_calloc(l_nb_tiles, sizeof(opj_tcp_v2_t));
2777         if (l_cp->tcps == 00) {
2778                 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to take in charge SIZ marker\n");
2779                 return OPJ_FALSE;
2780         }
2781         memset(l_cp->tcps,0,l_nb_tiles*sizeof(opj_tcp_t));
2782
2783 #ifdef USE_JPWL
2784         if (l_cp->correct) {
2785                 if (!l_cp->tcps) {
2786                         opj_event_msg_v2(p_manager, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
2787                                 "JPWL: could not alloc tcps field of cp\n");
2788                         if (!JPWL_ASSUME || JPWL_ASSUME) {
2789                                 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
2790                                 return OPJ_FALSE;
2791                         }
2792                 }
2793         }
2794 #endif /* USE_JPWL */
2795
2796         p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps =
2797                         (opj_tccp_t*) opj_calloc(l_image->numcomps, sizeof(opj_tccp_t));
2798         if(p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps  == 00) {
2799                 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to take in charge SIZ marker\n");
2800                 return OPJ_FALSE;
2801         }
2802         memset(p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps ,0,l_image->numcomps*sizeof(opj_tccp_t));
2803
2804         p_j2k->m_specific_param.m_decoder.m_default_tcp->m_mct_records =
2805                         (opj_mct_data_t*)opj_malloc(J2K_MCT_DEFAULT_NB_RECORDS * sizeof(opj_mct_data_t));
2806
2807         if (! p_j2k->m_specific_param.m_decoder.m_default_tcp->m_mct_records) {
2808                 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to take in charge SIZ marker\n");
2809                 return OPJ_FALSE;
2810         }
2811         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));
2812         p_j2k->m_specific_param.m_decoder.m_default_tcp->m_nb_max_mct_records = J2K_MCT_DEFAULT_NB_RECORDS;
2813
2814         p_j2k->m_specific_param.m_decoder.m_default_tcp->m_mcc_records =
2815                         (opj_simple_mcc_decorrelation_data_t*)
2816                         opj_malloc(J2K_MCC_DEFAULT_NB_RECORDS * sizeof(opj_simple_mcc_decorrelation_data_t));
2817
2818         if (! p_j2k->m_specific_param.m_decoder.m_default_tcp->m_mcc_records) {
2819                 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to take in charge SIZ marker\n");
2820                 return OPJ_FALSE;
2821         }
2822         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));
2823         p_j2k->m_specific_param.m_decoder.m_default_tcp->m_nb_max_mcc_records = J2K_MCC_DEFAULT_NB_RECORDS;
2824
2825         /* set up default dc level shift */
2826         for (i=0;i<l_image->numcomps;++i) {
2827                 if (! l_image->comps[i].sgnd) {
2828                         p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps[i].m_dc_level_shift = 1 << (l_image->comps[i].prec - 1);
2829                 }
2830         }
2831
2832         l_current_tile_param = l_cp->tcps;
2833         for     (i = 0; i < l_nb_tiles; ++i) {
2834                 l_current_tile_param->tccps = (opj_tccp_t*) opj_malloc(l_image->numcomps * sizeof(opj_tccp_t));
2835                 if (l_current_tile_param->tccps == 00) {
2836                         opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to take in charge SIZ marker\n");
2837                         return OPJ_FALSE;
2838                 }
2839                 memset(l_current_tile_param->tccps,0,l_image->numcomps * sizeof(opj_tccp_t));
2840
2841                 ++l_current_tile_param;
2842         }
2843
2844         p_j2k->m_specific_param.m_decoder.m_state =  J2K_STATE_MH; /* FIXME J2K_DEC_STATE_MH; */
2845         opj_image_comp_header_update(l_image,l_cp);
2846
2847         return OPJ_TRUE;
2848 }
2849
2850
2851
2852 static void j2k_write_com(opj_j2k_t *j2k) {
2853         unsigned int i;
2854         int lenp, len;
2855
2856         if(j2k->cp->comment) {
2857                 opj_cio_t *cio = j2k->cio;
2858                 char *comment = j2k->cp->comment;
2859
2860                 cio_write(cio, J2K_MS_COM, 2);
2861                 lenp = cio_tell(cio);
2862                 cio_skip(cio, 2);
2863                 cio_write(cio, 1, 2);           /* General use (IS 8859-15:1999 (Latin) values) */
2864                 for (i = 0; i < strlen(comment); i++) {
2865                         cio_write(cio, comment[i], 1);
2866                 }
2867                 len = cio_tell(cio) - lenp;
2868                 cio_seek(cio, lenp);
2869                 cio_write(cio, len, 2);
2870                 cio_seek(cio, lenp + len);
2871
2872                 
2873                 if(j2k->cstr_info)
2874                   j2k_add_mhmarker(j2k->cstr_info, J2K_MS_COM, lenp, len);
2875
2876         }
2877 }
2878
2879 /**
2880  * Writes the COM marker (comment)
2881  * 
2882  * @param       p_stream                        the stream to write data to.
2883  * @param       p_j2k                   J2K codec.
2884  * @param       p_manager       the user event manager.
2885 */
2886 opj_bool j2k_write_com_v2(      opj_j2k_v2_t *p_j2k,
2887                                                         struct opj_stream_private *p_stream,
2888                                                         struct opj_event_mgr * p_manager )
2889 {
2890         OPJ_UINT32 l_comment_size;
2891         OPJ_UINT32 l_total_com_size;
2892         const OPJ_CHAR *l_comment;
2893         OPJ_BYTE * l_current_ptr = 00;
2894
2895         // preconditions
2896         assert(p_j2k != 00);
2897         assert(p_stream != 00);
2898         assert(p_manager != 00);
2899         
2900         l_comment = p_j2k->m_cp.comment;
2901         l_comment_size = strlen(l_comment);
2902         l_total_com_size = l_comment_size + 6;
2903
2904         if (l_total_com_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
2905                 p_j2k->m_specific_param.m_encoder.m_header_tile_data 
2906                         = (OPJ_BYTE*)opj_realloc(       p_j2k->m_specific_param.m_encoder.m_header_tile_data,
2907                                                                                 l_total_com_size);
2908                 
2909                 if(! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
2910                         return OPJ_FALSE;
2911                 }
2912
2913                 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_total_com_size;
2914         }
2915
2916         l_current_ptr = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
2917         
2918         opj_write_bytes(l_current_ptr,J2K_MS_COM , 2);  /* COM */
2919         l_current_ptr+=2;
2920         
2921         opj_write_bytes(l_current_ptr,l_total_com_size - 2 , 2);        /* L_COM */
2922         l_current_ptr+=2;
2923         
2924         opj_write_bytes(l_current_ptr,1 , 2);   /* General use (IS 8859-15:1999 (Latin) values) */
2925         l_current_ptr+=2;
2926         
2927         memcpy( l_current_ptr,l_comment,l_comment_size);
2928         
2929         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) {
2930                 return OPJ_FALSE;
2931         }
2932
2933         return OPJ_TRUE;
2934 }
2935
2936 static void j2k_read_com(opj_j2k_t *j2k) {
2937         int len;
2938         
2939         opj_cio_t *cio = j2k->cio;
2940
2941         len = cio_read(cio, 2);
2942         cio_skip(cio, len - 2);  
2943 }
2944 /**
2945  * Reads a COM marker (comments)
2946  * @param       p_header_data   the data contained in the COM box.
2947  * @param       jp2                             the jpeg2000 file codec.
2948  * @param       p_header_size   the size of the data contained in the COM marker.
2949  * @param       p_manager               the user event manager.
2950 */
2951 opj_bool j2k_read_com_v2 (
2952                                         opj_j2k_v2_t *p_j2k,
2953                                         OPJ_BYTE * p_header_data,
2954                                         OPJ_UINT32 p_header_size,
2955                                         struct opj_event_mgr * p_manager
2956                                         )
2957 {
2958         /* preconditions */
2959         assert(p_j2k != 00);
2960         assert(p_manager != 00);
2961         assert(p_header_data != 00);
2962   (void)p_header_size;
2963
2964         return OPJ_TRUE;
2965 }
2966
2967 static void j2k_write_cox(opj_j2k_t *j2k, int compno) {
2968         OPJ_UINT32 i;
2969
2970         opj_cp_t *cp = j2k->cp;
2971         opj_tcp_t *tcp = &cp->tcps[j2k->curtileno];
2972         opj_tccp_t *tccp = &tcp->tccps[compno];
2973         opj_cio_t *cio = j2k->cio;
2974         
2975         cio_write(cio, tccp->numresolutions - 1, 1);    /* SPcox (D) */
2976         cio_write(cio, tccp->cblkw - 2, 1);                             /* SPcox (E) */
2977         cio_write(cio, tccp->cblkh - 2, 1);                             /* SPcox (F) */
2978         cio_write(cio, tccp->cblksty, 1);                               /* SPcox (G) */
2979         cio_write(cio, tccp->qmfbid, 1);                                /* SPcox (H) */
2980         
2981         if (tccp->csty & J2K_CCP_CSTY_PRT) {
2982                 for (i = 0; i < tccp->numresolutions; i++) {
2983                         cio_write(cio, tccp->prcw[i] + (tccp->prch[i] << 4), 1);        /* SPcox (I_i) */
2984                 }
2985         }
2986 }
2987
2988 static void j2k_read_cox(opj_j2k_t *j2k, int compno) {
2989         OPJ_UINT32 i;
2990
2991         opj_cp_t *cp = j2k->cp;
2992         opj_tcp_t *tcp = j2k->state == J2K_STATE_TPH ? &cp->tcps[j2k->curtileno] : j2k->default_tcp;
2993         opj_tccp_t *tccp = &tcp->tccps[compno];
2994         opj_cio_t *cio = j2k->cio;
2995
2996         tccp->numresolutions = cio_read(cio, 1) + 1;    /* SPcox (D) */
2997
2998         /* If user wants to remove more resolutions than the codestream contains, return error*/
2999         assert(cp->reduce >= 0);
3000         if ((OPJ_UINT32)cp->reduce >= tccp->numresolutions) {
3001                 opj_event_msg(j2k->cinfo, EVT_ERROR, "Error decoding component %d.\nThe number of resolutions to remove is higher than the number "
3002                                         "of resolutions of this component\nModify the cp_reduce parameter.\n\n", compno);
3003                 j2k->state |= J2K_STATE_ERR;
3004         }
3005
3006         tccp->cblkw = cio_read(cio, 1) + 2;     /* SPcox (E) */
3007         tccp->cblkh = cio_read(cio, 1) + 2;     /* SPcox (F) */
3008         tccp->cblksty = cio_read(cio, 1);       /* SPcox (G) */
3009         tccp->qmfbid = cio_read(cio, 1);        /* SPcox (H) */
3010         if (tccp->csty & J2K_CP_CSTY_PRT) {
3011                 for (i = 0; i < tccp->numresolutions; i++) {
3012                         int tmp = cio_read(cio, 1);     /* SPcox (I_i) */
3013                         tccp->prcw[i] = tmp & 0xf;
3014                         tccp->prch[i] = tmp >> 4;
3015                 }
3016         }
3017
3018         /* INDEX >> */
3019         if(j2k->cstr_info && compno == 0) {
3020                 for (i = 0; i < tccp->numresolutions; i++) {
3021                         if (tccp->csty & J2K_CP_CSTY_PRT) {
3022                                 j2k->cstr_info->tile[j2k->curtileno].pdx[i] = tccp->prcw[i];
3023                                 j2k->cstr_info->tile[j2k->curtileno].pdy[i] = tccp->prch[i];
3024                         }
3025                         else {
3026                                 j2k->cstr_info->tile[j2k->curtileno].pdx[i] = 15;
3027                                 j2k->cstr_info->tile[j2k->curtileno].pdx[i] = 15;
3028                         }
3029                 }
3030         }
3031         /* << INDEX */
3032 }
3033
3034 static void j2k_write_cod(opj_j2k_t *j2k) {
3035         opj_cp_t *cp = NULL;
3036         opj_tcp_t *tcp = NULL;
3037         int lenp, len;
3038
3039         opj_cio_t *cio = j2k->cio;
3040         
3041         cio_write(cio, J2K_MS_COD, 2);  /* COD */
3042         
3043         lenp = cio_tell(cio);
3044         cio_skip(cio, 2);
3045         
3046         cp = j2k->cp;
3047         tcp = &cp->tcps[j2k->curtileno];
3048
3049         cio_write(cio, tcp->csty, 1);           /* Scod */
3050         cio_write(cio, tcp->prg, 1);            /* SGcod (A) */
3051         cio_write(cio, tcp->numlayers, 2);      /* SGcod (B) */
3052         cio_write(cio, tcp->mct, 1);            /* SGcod (C) */
3053         
3054         j2k_write_cox(j2k, 0);
3055         len = cio_tell(cio) - lenp;
3056         cio_seek(cio, lenp);
3057         cio_write(cio, len, 2);         /* Lcod */
3058         cio_seek(cio, lenp + len);
3059
3060         if(j2k->cstr_info)
3061           j2k_add_mhmarker(j2k->cstr_info, J2K_MS_COD, lenp, len);
3062
3063 }
3064
3065 /**
3066  * Writes the COD marker (Coding style default)
3067  *
3068  * @param       p_stream                        the stream to write data to.
3069  * @param       p_j2k                   J2K codec.
3070  * @param       p_manager       the user event manager.
3071 */
3072 opj_bool j2k_write_cod_v2(      opj_j2k_v2_t *p_j2k,
3073                                                         struct opj_stream_private *p_stream,
3074                                                         struct opj_event_mgr * p_manager )
3075 {
3076         opj_cp_v2_t *l_cp = 00;
3077         opj_tcp_v2_t *l_tcp = 00;
3078         OPJ_UINT32 l_code_size,l_remaining_size;
3079         OPJ_BYTE * l_current_data = 00;
3080
3081         /* preconditions */
3082         assert(p_j2k != 00);
3083         assert(p_manager != 00);
3084         assert(p_stream != 00);
3085
3086         l_cp = &(p_j2k->m_cp);
3087         l_tcp = &l_cp->tcps[p_j2k->m_current_tile_number];
3088         l_code_size = 9 + j2k_get_SPCod_SPCoc_size(p_j2k,p_j2k->m_current_tile_number,0);
3089         l_remaining_size = l_code_size;
3090
3091         if (l_code_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
3092                 p_j2k->m_specific_param.m_encoder.m_header_tile_data
3093                         = (OPJ_BYTE*)opj_realloc(
3094                                 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
3095                                 l_code_size);
3096
3097                 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
3098                         return OPJ_FALSE;
3099                 }
3100
3101                 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_code_size;
3102         }
3103
3104         l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
3105
3106         opj_write_bytes(l_current_data,J2K_MS_COD,2);           /* COD */
3107         l_current_data += 2;
3108
3109         opj_write_bytes(l_current_data,l_code_size-2,2);        /* L_COD */
3110         l_current_data += 2;
3111
3112         opj_write_bytes(l_current_data,l_tcp->csty,1);          /* Scod */
3113         ++l_current_data;
3114
3115         opj_write_bytes(l_current_data,l_tcp->prg,1);           /* SGcod (A) */
3116         ++l_current_data;
3117
3118         opj_write_bytes(l_current_data,l_tcp->numlayers,2);     /* SGcod (B) */
3119         l_current_data+=2;
3120
3121         opj_write_bytes(l_current_data,l_tcp->mct,1);           /* SGcod (C) */
3122         ++l_current_data;
3123
3124         l_remaining_size -= 9;
3125
3126         if (! j2k_write_SPCod_SPCoc(p_j2k,p_j2k->m_current_tile_number,0,l_current_data,&l_remaining_size,p_manager)) {
3127                 opj_event_msg_v2(p_manager, EVT_ERROR, "Error writting COD marker\n");
3128                 return OPJ_FALSE;
3129         }
3130
3131         if (l_remaining_size != 0) {
3132                 opj_event_msg_v2(p_manager, EVT_ERROR, "Error writting COD marker\n");
3133                 return OPJ_FALSE;
3134         }
3135
3136         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) {
3137                 return OPJ_FALSE;
3138         }
3139
3140         return OPJ_TRUE;
3141 }
3142
3143 static void j2k_read_cod(opj_j2k_t *j2k) {
3144         int len, pos;
3145   OPJ_UINT32 i;
3146         
3147         opj_cio_t *cio = j2k->cio;
3148         opj_cp_t *cp = j2k->cp;
3149         opj_tcp_t *tcp = j2k->state == J2K_STATE_TPH ? &cp->tcps[j2k->curtileno] : j2k->default_tcp;
3150         opj_image_t *image = j2k->image;
3151         
3152         len = cio_read(cio, 2);                         /* Lcod */
3153         tcp->csty = cio_read(cio, 1);           /* Scod */
3154         tcp->prg = (OPJ_PROG_ORDER)cio_read(cio, 1);            /* SGcod (A) */
3155         tcp->numlayers = cio_read(cio, 2);      /* SGcod (B) */
3156         tcp->mct = cio_read(cio, 1);            /* SGcod (C) */
3157         
3158         pos = cio_tell(cio);
3159         for (i = 0; i < image->numcomps; i++) {
3160                 tcp->tccps[i].csty = tcp->csty & J2K_CP_CSTY_PRT;
3161                 cio_seek(cio, pos);
3162                 j2k_read_cox(j2k, i);
3163         }
3164
3165         /* Index */
3166         if (j2k->cstr_info) {
3167                 opj_codestream_info_t *cstr_info = j2k->cstr_info;
3168                 cstr_info->prog = tcp->prg;
3169                 cstr_info->numlayers = tcp->numlayers;
3170                 cstr_info->numdecompos = (int*) opj_malloc(image->numcomps * sizeof(int));
3171                 for (i = 0; i < image->numcomps; i++) {
3172                         cstr_info->numdecompos[i] = tcp->tccps[i].numresolutions - 1;
3173                 }
3174         }
3175 }
3176
3177 /**
3178  * Reads a COD marker (Coding Styke defaults)
3179  * @param       p_header_data   the data contained in the COD box.
3180  * @param       p_j2k                   the jpeg2000 codec.
3181  * @param       p_header_size   the size of the data contained in the COD marker.
3182  * @param       p_manager               the user event manager.
3183 */
3184 opj_bool j2k_read_cod_v2 (
3185                                         opj_j2k_v2_t *p_j2k,
3186                                         OPJ_BYTE * p_header_data,
3187                                         OPJ_UINT32 p_header_size,
3188                                         struct opj_event_mgr * p_manager
3189                                         )
3190 {
3191         /* loop */
3192         OPJ_UINT32 i;
3193         OPJ_UINT32 l_tmp;
3194         opj_cp_v2_t *l_cp = 00;
3195         opj_tcp_v2_t *l_tcp = 00;
3196         opj_image_t *l_image = 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_cp = &(p_j2k->m_cp);
3205
3206         /* If we are in the first tile-part header of the current tile */
3207         l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ?
3208                                 &l_cp->tcps[p_j2k->m_current_tile_number] :
3209                                 p_j2k->m_specific_param.m_decoder.m_default_tcp;
3210
3211         /* Make sure room is sufficient */
3212         if (p_header_size < 5) {
3213                 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading COD marker\n");
3214                 return OPJ_FALSE;
3215         }
3216
3217         opj_read_bytes(p_header_data,&l_tcp->csty,1);           /* Scod */
3218         ++p_header_data;
3219         opj_read_bytes(p_header_data,&l_tmp,1);                         /* SGcod (A) */
3220         ++p_header_data;
3221         l_tcp->prg = (OPJ_PROG_ORDER) l_tmp;
3222         opj_read_bytes(p_header_data,&l_tcp->numlayers,2);      /* SGcod (B) */
3223         p_header_data+=2;
3224
3225         /* If user didn't set a number layer to decode take the max specify in the codestream. */
3226         if      (l_cp->m_specific_param.m_dec.m_layer) {
3227                 l_tcp->num_layers_to_decode = l_cp->m_specific_param.m_dec.m_layer;
3228         }
3229         else {
3230                 l_tcp->num_layers_to_decode = l_tcp->numlayers;
3231         }
3232
3233         opj_read_bytes(p_header_data,&l_tcp->mct,1);            /* SGcod (C) */
3234         ++p_header_data;
3235
3236         p_header_size -= 5;
3237         for     (i = 0; i < l_image->numcomps; ++i) {
3238                 l_tcp->tccps[i].csty = l_tcp->csty & J2K_CCP_CSTY_PRT;
3239         }
3240
3241         if (! j2k_read_SPCod_SPCoc(p_j2k,0,p_header_data,&p_header_size,p_manager)) {
3242                 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading COD marker\n");
3243                 return OPJ_FALSE;
3244         }
3245
3246         if (p_header_size != 0) {
3247                 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading COD marker\n");
3248                 return OPJ_FALSE;
3249         }
3250
3251         /* Apply the coding style to other components of the current tile or the m_default_tcp*/
3252         j2k_copy_tile_component_parameters(p_j2k);
3253
3254         /* Index */
3255 #ifdef WIP_REMOVE_MSD
3256         if (p_j2k->cstr_info) {
3257                 /*opj_codestream_info_t *l_cstr_info = p_j2k->cstr_info;*/
3258                 p_j2k->cstr_info->prog = l_tcp->prg;
3259                 p_j2k->cstr_info->numlayers = l_tcp->numlayers;
3260                 p_j2k->cstr_info->numdecompos = (OPJ_INT32*) opj_malloc(l_image->numcomps * sizeof(OPJ_UINT32));
3261                 for     (i = 0; i < l_image->numcomps; ++i) {
3262                         p_j2k->cstr_info->numdecompos[i] = l_tcp->tccps[i].numresolutions - 1;
3263                 }
3264         }
3265 #endif
3266
3267         return OPJ_TRUE;
3268 }
3269
3270 static void j2k_write_coc(opj_j2k_t *j2k, int compno) {
3271         int lenp, len;
3272
3273         opj_cp_t *cp = j2k->cp;
3274         opj_tcp_t *tcp = &cp->tcps[j2k->curtileno];
3275         opj_image_t *image = j2k->image;
3276         opj_cio_t *cio = j2k->cio;
3277         
3278         cio_write(cio, J2K_MS_COC, 2);  /* COC */
3279         lenp = cio_tell(cio);
3280         cio_skip(cio, 2);
3281         cio_write(cio, compno, image->numcomps <= 256 ? 1 : 2); /* Ccoc */
3282         cio_write(cio, tcp->tccps[compno].csty, 1);     /* Scoc */
3283         j2k_write_cox(j2k, compno);
3284         len = cio_tell(cio) - lenp;
3285         cio_seek(cio, lenp);
3286         cio_write(cio, len, 2);                 /* Lcoc */
3287         cio_seek(cio, lenp + len);
3288 }
3289
3290 /**
3291  * Writes the COC marker (Coding style component)
3292  *
3293  * @param       p_comp_no               the index of the component to output.
3294  * @param       p_stream                                the stream to write data to.
3295  * @param       p_j2k                           J2K codec.
3296  * @param       p_manager               the user event manager.
3297 */
3298 opj_bool j2k_write_coc_v2(      opj_j2k_v2_t *p_j2k,
3299                                                         OPJ_UINT32 p_comp_no,
3300                                                         struct opj_stream_private *p_stream,
3301                                                         struct opj_event_mgr * p_manager )
3302 {
3303         OPJ_UINT32 l_coc_size,l_remaining_size;
3304         OPJ_UINT32 l_comp_room;
3305
3306         /* preconditions */
3307         assert(p_j2k != 00);
3308         assert(p_manager != 00);
3309         assert(p_stream != 00);
3310
3311         l_comp_room = (p_j2k->m_private_image->numcomps <= 256) ? 1 : 2;
3312
3313         l_coc_size = 5 + l_comp_room + j2k_get_SPCod_SPCoc_size(p_j2k,p_j2k->m_current_tile_number,p_comp_no);
3314
3315         if (l_coc_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
3316                 p_j2k->m_specific_param.m_encoder.m_header_tile_data
3317                         = (OPJ_BYTE*)opj_realloc(
3318                                 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
3319                                 l_coc_size);
3320                 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
3321                         return OPJ_FALSE;
3322                 }
3323
3324                 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_coc_size;
3325         }
3326
3327         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);
3328
3329         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) {
3330                 return OPJ_FALSE;
3331         }
3332
3333         return OPJ_TRUE;
3334 }
3335
3336 /**
3337  * Writes the COC marker (Coding style component)
3338  *
3339  * @param       p_comp_no               the index of the component to output.
3340  * @param       p_stream                                the stream to write data to.
3341  * @param       p_j2k                           J2K codec.
3342  * @param       p_manager               the user event manager.
3343 */
3344 void j2k_write_coc_in_memory(   opj_j2k_v2_t *p_j2k,
3345                                                                 OPJ_UINT32 p_comp_no,
3346                                                                 OPJ_BYTE * p_data,
3347                                                                 OPJ_UINT32 * p_data_written,
3348                                                                 struct opj_event_mgr * p_manager )
3349 {
3350         opj_cp_v2_t *l_cp = 00;
3351         opj_tcp_v2_t *l_tcp = 00;
3352         OPJ_UINT32 l_coc_size,l_remaining_size;
3353         OPJ_BYTE * l_current_data = 00;
3354         opj_image_t *l_image = 00;
3355         OPJ_UINT32 l_comp_room;
3356
3357         /* preconditions */
3358         assert(p_j2k != 00);
3359         assert(p_manager != 00);
3360
3361         l_cp = &(p_j2k->m_cp);
3362         l_tcp = &l_cp->tcps[p_j2k->m_current_tile_number];
3363         l_image = p_j2k->m_private_image;
3364         l_comp_room = (l_image->numcomps <= 256) ? 1 : 2;
3365
3366         l_coc_size = 5 + l_comp_room + j2k_get_SPCod_SPCoc_size(p_j2k,p_j2k->m_current_tile_number,p_comp_no);
3367         l_remaining_size = l_coc_size;
3368
3369         l_current_data = p_data;
3370
3371         opj_write_bytes(l_current_data,J2K_MS_COC,2);                           /* COC */
3372         l_current_data += 2;
3373
3374         opj_write_bytes(l_current_data,l_coc_size-2,2);                         /* L_COC */
3375         l_current_data += 2;
3376
3377         opj_write_bytes(l_current_data,p_comp_no, l_comp_room);         /* Ccoc */
3378         l_current_data+=l_comp_room;
3379
3380         opj_write_bytes(l_current_data, l_tcp->tccps[p_comp_no].csty, 1);               /* Scoc */
3381         ++l_current_data;
3382
3383         l_remaining_size -= (5 + l_comp_room);
3384         j2k_write_SPCod_SPCoc(p_j2k,p_j2k->m_current_tile_number,0,l_current_data,&l_remaining_size,p_manager);
3385         * p_data_written = l_coc_size;
3386 }
3387
3388 /**
3389  * Gets the maximum size taken by a coc.
3390  *
3391  * @param       p_j2k   the jpeg2000 codec to use.
3392  */
3393 OPJ_UINT32 j2k_get_max_coc_size(opj_j2k_v2_t *p_j2k)
3394 {
3395         OPJ_UINT32 i,j;
3396         OPJ_UINT32 l_nb_comp;
3397         OPJ_UINT32 l_nb_tiles;
3398         OPJ_UINT32 l_max = 0;
3399
3400         /* preconditions */
3401
3402         l_nb_tiles = p_j2k->m_cp.tw * p_j2k->m_cp.th ;
3403         l_nb_comp = p_j2k->m_private_image->numcomps;
3404
3405         for (i=0;i<l_nb_tiles;++i) {
3406                 for (j=0;j<l_nb_comp;++j) {
3407                         l_max = uint_max(l_max,j2k_get_SPCod_SPCoc_size(p_j2k,i,j));
3408                 }
3409         }
3410
3411         return 6 + l_max;
3412 }
3413
3414 static void j2k_read_coc(opj_j2k_t *j2k) {
3415         int len, compno;
3416
3417         opj_cp_t *cp = j2k->cp;
3418         opj_tcp_t *tcp = j2k->state == J2K_STATE_TPH ? &cp->tcps[j2k->curtileno] : j2k->default_tcp;
3419         opj_image_t *image = j2k->image;
3420         opj_cio_t *cio = j2k->cio;
3421         
3422         len = cio_read(cio, 2);         /* Lcoc */
3423         compno = cio_read(cio, image->numcomps <= 256 ? 1 : 2); /* Ccoc */
3424         tcp->tccps[compno].csty = cio_read(cio, 1);     /* Scoc */
3425         j2k_read_cox(j2k, compno);
3426 }
3427
3428 /**
3429  * Reads a COC marker (Coding Style Component)
3430  * @param       p_header_data   the data contained in the COC box.
3431  * @param       p_j2k                   the jpeg2000 codec.
3432  * @param       p_header_size   the size of the data contained in the COC marker.
3433  * @param       p_manager               the user event manager.
3434 */
3435 opj_bool j2k_read_coc_v2 (
3436                                         opj_j2k_v2_t *p_j2k,
3437                                         OPJ_BYTE * p_header_data,
3438                                         OPJ_UINT32 p_header_size,
3439                                         struct opj_event_mgr * p_manager
3440                                         )
3441 {
3442         opj_cp_v2_t *l_cp = NULL;
3443         opj_tcp_v2_t *l_tcp = NULL;
3444         opj_image_t *l_image = NULL;
3445         OPJ_UINT32 l_comp_room;
3446         OPJ_UINT32 l_comp_no;
3447
3448         /* preconditions */
3449         assert(p_header_data != 00);
3450         assert(p_j2k != 00);
3451         assert(p_manager != 00);
3452
3453         l_cp = &(p_j2k->m_cp);
3454         l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH ) ? /*FIXME J2K_DEC_STATE_TPH*/
3455                                 &l_cp->tcps[p_j2k->m_current_tile_number] :
3456                                 p_j2k->m_specific_param.m_decoder.m_default_tcp;
3457         l_image = p_j2k->m_private_image;
3458
3459         l_comp_room = l_image->numcomps <= 256 ? 1 : 2;
3460
3461         /* make sure room is sufficient*/
3462         if (p_header_size < l_comp_room + 1) {
3463                 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading COC marker\n");
3464                 return OPJ_FALSE;
3465         }
3466         p_header_size -= l_comp_room + 1;
3467
3468         opj_read_bytes(p_header_data,&l_comp_no,l_comp_room);                   /* Ccoc */
3469         p_header_data += l_comp_room;
3470         if (l_comp_no >= l_image->numcomps) {
3471                 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading COC marker (bad number of components)\n");
3472                 return OPJ_FALSE;
3473         }
3474
3475         opj_read_bytes(p_header_data,&l_tcp->tccps[l_comp_no].csty,1);                  /* Scoc */
3476         ++p_header_data ;
3477
3478         if (! j2k_read_SPCod_SPCoc(p_j2k,l_comp_no,p_header_data,&p_header_size,p_manager)) {
3479                 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading COC marker\n");
3480                 return OPJ_FALSE;
3481         }
3482
3483         if (p_header_size != 0) {
3484                 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading COC marker\n");
3485                 return OPJ_FALSE;
3486         }
3487         return OPJ_TRUE;
3488 }
3489
3490 static void j2k_write_qcx(opj_j2k_t *j2k, int compno) {
3491         int bandno, numbands;
3492         int expn, mant;
3493         
3494         opj_cp_t *cp = j2k->cp;
3495         opj_tcp_t *tcp = &cp->tcps[j2k->curtileno];
3496         opj_tccp_t *tccp = &tcp->tccps[compno];
3497         opj_cio_t *cio = j2k->cio;
3498         
3499         cio_write(cio, tccp->qntsty + (tccp->numgbits << 5), 1);        /* Sqcx */
3500         numbands = tccp->qntsty == J2K_CCP_QNTSTY_SIQNT ? 1 : tccp->numresolutions * 3 - 2;
3501         
3502         for (bandno = 0; bandno < numbands; bandno++) {
3503                 expn = tccp->stepsizes[bandno].expn;
3504                 mant = tccp->stepsizes[bandno].mant;
3505                 
3506                 if (tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) {
3507                         cio_write(cio, expn << 3, 1);   /* SPqcx_i */
3508                 } else {
3509                         cio_write(cio, (expn << 11) + mant, 2); /* SPqcx_i */
3510                 }
3511         }
3512 }
3513
3514 static void j2k_read_qcx(opj_j2k_t *j2k, int compno, int len) {
3515         int tmp;
3516         int bandno, numbands;
3517
3518         opj_cp_t *cp = j2k->cp;
3519         opj_tcp_t *tcp = j2k->state == J2K_STATE_TPH ? &cp->tcps[j2k->curtileno] : j2k->default_tcp;
3520         opj_tccp_t *tccp = &tcp->tccps[compno];
3521         opj_cio_t *cio = j2k->cio;
3522
3523         tmp = cio_read(cio, 1);         /* Sqcx */
3524         tccp->qntsty = tmp & 0x1f;
3525         tccp->numgbits = tmp >> 5;
3526         numbands = (tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) ? 
3527                 1 : ((tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) ? len - 1 : (len - 1) / 2);
3528
3529 #ifdef USE_JPWL
3530         if (j2k->cp->correct) {
3531
3532                 /* if JPWL is on, we check whether there are too many subbands */
3533                 if ((numbands < 0) || (numbands >= J2K_MAXBANDS)) {
3534                         opj_event_msg(j2k->cinfo, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
3535                                 "JPWL: bad number of subbands in Sqcx (%d)\n",
3536                                 numbands);
3537                         if (!JPWL_ASSUME) {
3538                                 opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
3539                                 return;
3540                         }
3541                         /* we try to correct */
3542                         numbands = 1;
3543                         opj_event_msg(j2k->cinfo, EVT_WARNING, "- trying to adjust them\n"
3544                                 "- setting number of bands to %d => HYPOTHESIS!!!\n",
3545                                 numbands);
3546                 };
3547
3548         };
3549
3550 #else
3551         /* We check whether there are too many subbands */
3552         if ((numbands < 0) || (numbands >= J2K_MAXBANDS)) {
3553                 opj_event_msg(j2k->cinfo, EVT_WARNING ,
3554                                         "bad number of subbands in Sqcx (%d) regarding to J2K_MAXBANDS (%d) \n"
3555                                     "- limiting number of bands to J2K_MAXBANDS and try to move to the next markers\n", numbands, J2K_MAXBANDS);
3556         }
3557
3558 #endif /* USE_JPWL */
3559
3560         for (bandno = 0; bandno < numbands; bandno++) {
3561                 int expn, mant;
3562                 if (tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) {
3563                         expn = cio_read(cio, 1) >> 3;   /* SPqcx_i */
3564                         mant = 0;
3565                 } else {
3566                         tmp = cio_read(cio, 2); /* SPqcx_i */
3567                         expn = tmp >> 11;
3568                         mant = tmp & 0x7ff;
3569                 }
3570                 if (bandno < J2K_MAXBANDS){
3571                         tccp->stepsizes[bandno].expn = expn;
3572                         tccp->stepsizes[bandno].mant = mant;
3573                 }
3574         }
3575         
3576         /* Add Antonin : if scalar_derived -> compute other stepsizes */
3577         if (tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) {
3578                 for (bandno = 1; bandno < J2K_MAXBANDS; bandno++) {
3579                         tccp->stepsizes[bandno].expn = 
3580                                 ((tccp->stepsizes[0].expn) - ((bandno - 1) / 3) > 0) ? 
3581                                         (tccp->stepsizes[0].expn) - ((bandno - 1) / 3) : 0;
3582                         tccp->stepsizes[bandno].mant = tccp->stepsizes[0].mant;
3583                 }
3584         }
3585         /* ddA */
3586 }
3587
3588 static void j2k_write_qcd(opj_j2k_t *j2k) {
3589         int lenp, len;
3590
3591         opj_cio_t *cio = j2k->cio;
3592         
3593         cio_write(cio, J2K_MS_QCD, 2);  /* QCD */
3594         lenp = cio_tell(cio);
3595         cio_skip(cio, 2);
3596         j2k_write_qcx(j2k, 0);
3597         len = cio_tell(cio) - lenp;
3598         cio_seek(cio, lenp);
3599         cio_write(cio, len, 2);                 /* Lqcd */
3600         cio_seek(cio, lenp + len);
3601
3602         if(j2k->cstr_info)
3603           j2k_add_mhmarker(j2k->cstr_info, J2K_MS_QCD, lenp, len);
3604 }
3605
3606 /**
3607  * Writes the QCD marker (quantization default)
3608  *
3609  * @param       p_comp_number   the index of the component to output.
3610  * @param       p_stream                                the stream to write data to.
3611  * @param       p_j2k                           J2K codec.
3612  * @param       p_manager               the user event manager.
3613 */
3614 opj_bool j2k_write_qcd_v2(      opj_j2k_v2_t *p_j2k,
3615                                                         struct opj_stream_private *p_stream,
3616                                                         struct opj_event_mgr * p_manager )
3617 {
3618         opj_cp_v2_t *l_cp = 00;
3619         opj_tcp_v2_t *l_tcp = 00;
3620         OPJ_UINT32 l_qcd_size,l_remaining_size;
3621         OPJ_BYTE * l_current_data = 00;
3622
3623         /* preconditions */
3624         assert(p_j2k != 00);
3625         assert(p_manager != 00);
3626         assert(p_stream != 00);
3627
3628         l_cp = &(p_j2k->m_cp);
3629         l_tcp = &l_cp->tcps[p_j2k->m_current_tile_number];
3630         l_qcd_size = 4 + j2k_get_SQcd_SQcc_size(p_j2k,p_j2k->m_current_tile_number,0);
3631         l_remaining_size = l_qcd_size;
3632
3633         if (l_qcd_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
3634                 p_j2k->m_specific_param.m_encoder.m_header_tile_data
3635                         = (OPJ_BYTE*)opj_realloc(
3636                                 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
3637                                 l_qcd_size);
3638
3639                 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
3640                         return OPJ_FALSE;
3641                 }
3642
3643                 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_qcd_size;
3644         }
3645
3646         l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
3647
3648         opj_write_bytes(l_current_data,J2K_MS_QCD,2);           /* QCD */
3649         l_current_data += 2;
3650
3651         opj_write_bytes(l_current_data,l_qcd_size-2,2);         /* L_QCD */
3652         l_current_data += 2;
3653
3654         l_remaining_size -= 4;
3655
3656         if (! j2k_write_SQcd_SQcc(p_j2k,p_j2k->m_current_tile_number,0,l_current_data,&l_remaining_size,p_manager)) {
3657                 opj_event_msg_v2(p_manager, EVT_ERROR, "Error writting QCD marker\n");
3658                 return OPJ_FALSE;
3659         }
3660
3661         if (l_remaining_size != 0) {
3662                 opj_event_msg_v2(p_manager, EVT_ERROR, "Error writting QCD marker\n");
3663                 return OPJ_FALSE;
3664         }
3665
3666         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) {
3667                 return OPJ_FALSE;
3668         }
3669
3670         return OPJ_TRUE;
3671 }
3672
3673
3674 static void j2k_read_qcd(opj_j2k_t *j2k) {
3675         int len, pos;
3676   OPJ_UINT32 i;
3677
3678         opj_cio_t *cio = j2k->cio;
3679         opj_image_t *image = j2k->image;
3680         
3681         len = cio_read(cio, 2);         /* Lqcd */
3682         pos = cio_tell(cio);
3683         for (i = 0; i < image->numcomps; i++) {
3684                 cio_seek(cio, pos);
3685                 j2k_read_qcx(j2k, i, len - 2);
3686         }
3687 }
3688
3689 /**
3690  * Reads a QCD marker (Quantization defaults)
3691  * @param       p_header_data   the data contained in the QCD box.
3692  * @param       p_j2k                   the jpeg2000 codec.
3693  * @param       p_header_size   the size of the data contained in the QCD marker.
3694  * @param       p_manager               the user event manager.
3695 */
3696 opj_bool j2k_read_qcd_v2 (
3697                                     opj_j2k_v2_t *p_j2k,
3698                                         OPJ_BYTE * p_header_data,
3699                                         OPJ_UINT32 p_header_size,
3700                                         struct opj_event_mgr * p_manager
3701                                         )
3702 {
3703         /* preconditions */
3704         assert(p_header_data != 00);
3705         assert(p_j2k != 00);
3706         assert(p_manager != 00);
3707
3708         if (! j2k_read_SQcd_SQcc(p_j2k,0,p_header_data,&p_header_size,p_manager)) {
3709                 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading QCD marker\n");
3710                 return OPJ_FALSE;
3711         }
3712
3713         if (p_header_size != 0) {
3714                 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading QCD marker\n");
3715                 return OPJ_FALSE;
3716         }
3717
3718         /* Apply the quantization parameters to other components of the current tile or the m_default_tcp */
3719         j2k_copy_tile_quantization_parameters(p_j2k);
3720
3721         return OPJ_TRUE;
3722 }
3723
3724 static void j2k_write_qcc(opj_j2k_t *j2k, int compno) {
3725         int lenp, len;
3726
3727         opj_cio_t *cio = j2k->cio;
3728         
3729         cio_write(cio, J2K_MS_QCC, 2);  /* QCC */
3730         lenp = cio_tell(cio);
3731         cio_skip(cio, 2);
3732         cio_write(cio, compno, j2k->image->numcomps <= 256 ? 1 : 2);    /* Cqcc */
3733         j2k_write_qcx(j2k, compno);
3734         len = cio_tell(cio) - lenp;
3735         cio_seek(cio, lenp);
3736         cio_write(cio, len, 2);                 /* Lqcc */
3737         cio_seek(cio, lenp + len);
3738 }
3739
3740 /**
3741  * Writes the QCC marker (quantization component)
3742  *
3743  * @param       p_comp_no       the index of the component to output.
3744  * @param       p_stream                                the stream to write data to.
3745  * @param       p_j2k                           J2K codec.
3746  * @param       p_manager               the user event manager.
3747 */
3748 opj_bool j2k_write_qcc_v2(      opj_j2k_v2_t *p_j2k,
3749                                                         OPJ_UINT32 p_comp_no,
3750                                                         struct opj_stream_private *p_stream,
3751                                                         struct opj_event_mgr * p_manager )
3752 {
3753         OPJ_UINT32 l_qcc_size,l_remaining_size;
3754
3755         /* preconditions */
3756         assert(p_j2k != 00);
3757         assert(p_manager != 00);
3758         assert(p_stream != 00);
3759
3760         l_qcc_size = 6 + j2k_get_SQcd_SQcc_size(p_j2k,p_j2k->m_current_tile_number,p_comp_no);
3761         l_remaining_size = l_qcc_size;
3762
3763         if (l_qcc_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
3764                 p_j2k->m_specific_param.m_encoder.m_header_tile_data
3765                         = (OPJ_BYTE*)opj_realloc(
3766                                 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
3767                                 l_qcc_size);
3768                 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
3769                         return OPJ_FALSE;
3770                 }
3771
3772                 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_qcc_size;
3773         }
3774
3775         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);
3776
3777         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) {
3778                 return OPJ_FALSE;
3779         }
3780
3781         return OPJ_TRUE;
3782 }
3783
3784 /**
3785  * Writes the QCC marker (quantization component)
3786  *
3787  * @param       p_comp_no       the index of the component to output.
3788  * @param       p_stream                                the stream to write data to.
3789  * @param       p_j2k                           J2K codec.
3790  * @param       p_manager               the user event manager.
3791 */
3792 void j2k_write_qcc_in_memory(   opj_j2k_v2_t *p_j2k,
3793                                                                 OPJ_UINT32 p_comp_no,
3794                                                                 OPJ_BYTE * p_data,
3795                                                                 OPJ_UINT32 * p_data_written,
3796                                                                 struct opj_event_mgr * p_manager )
3797 {
3798         OPJ_UINT32 l_qcc_size,l_remaining_size;
3799         OPJ_BYTE * l_current_data = 00;
3800
3801         /* preconditions */
3802         assert(p_j2k != 00);
3803         assert(p_manager != 00);
3804
3805         l_qcc_size = 6 + j2k_get_SQcd_SQcc_size(p_j2k,p_j2k->m_current_tile_number,p_comp_no);
3806         l_remaining_size = l_qcc_size;
3807
3808         l_current_data = p_data;
3809
3810         opj_write_bytes(l_current_data,J2K_MS_QCC,2);           /* QCC */
3811         l_current_data += 2;
3812
3813         if (p_j2k->m_private_image->numcomps <= 256) {
3814                 --l_qcc_size;
3815
3816                 opj_write_bytes(l_current_data,l_qcc_size-2,2);         /* L_QCC */
3817                 l_current_data += 2;
3818
3819                 opj_write_bytes(l_current_data, p_comp_no, 1);  /* Cqcc */
3820                 ++l_current_data;
3821
3822                 /* in the case only one byte is sufficient the last byte allocated is useless -> still do -6 for available */
3823                 l_remaining_size -= 6;
3824         }
3825         else {
3826                 opj_write_bytes(l_current_data,l_qcc_size-2,2);         /* L_QCC */
3827                 l_current_data += 2;
3828
3829                 opj_write_bytes(l_current_data, p_comp_no, 2);  /* Cqcc */
3830                 l_current_data+=2;
3831
3832                 l_remaining_size -= 6;
3833         }
3834
3835         j2k_write_SQcd_SQcc(p_j2k,p_j2k->m_current_tile_number,p_comp_no,l_current_data,&l_remaining_size,p_manager);
3836
3837         *p_data_written = l_qcc_size;
3838 }
3839
3840 /**
3841  * Gets the maximum size taken by a qcc.
3842  */
3843 OPJ_UINT32 j2k_get_max_qcc_size (opj_j2k_v2_t *p_j2k)
3844 {
3845         return j2k_get_max_coc_size(p_j2k);
3846 }
3847
3848 static void j2k_read_qcc(opj_j2k_t *j2k) {
3849         int len, compno;
3850         int numcomp = j2k->image->numcomps;
3851         opj_cio_t *cio = j2k->cio;
3852
3853         len = cio_read(cio, 2); /* Lqcc */
3854         compno = cio_read(cio, numcomp <= 256 ? 1 : 2); /* Cqcc */
3855
3856 #ifdef USE_JPWL
3857         if (j2k->cp->correct) {
3858
3859                 static int backup_compno = 0;
3860
3861                 /* compno is negative or larger than the number of components!!! */
3862                 if ((compno < 0) || (compno >= numcomp)) {
3863                         opj_event_msg(j2k->cinfo, EVT_ERROR,
3864                                 "JPWL: bad component number in QCC (%d out of a maximum of %d)\n",
3865                                 compno, numcomp);
3866                         if (!JPWL_ASSUME) {
3867                                 opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
3868                                 return;
3869                         }
3870                         /* we try to correct */
3871                         compno = backup_compno % numcomp;
3872                         opj_event_msg(j2k->cinfo, EVT_WARNING, "- trying to adjust this\n"
3873                                 "- setting component number to %d\n",
3874                                 compno);
3875                 }
3876
3877                 /* keep your private count of tiles */
3878                 backup_compno++;
3879         };
3880 #endif /* USE_JPWL */
3881
3882         j2k_read_qcx(j2k, compno, len - 2 - (numcomp <= 256 ? 1 : 2));
3883 }
3884
3885 /**
3886  * Reads a QCC marker (Quantization component)
3887  * @param       p_header_data   the data contained in the QCC box.
3888  * @param       p_j2k                   the jpeg2000 codec.
3889  * @param       p_header_size   the size of the data contained in the QCC marker.
3890  * @param       p_manager               the user event manager.
3891 */
3892 opj_bool j2k_read_qcc_v2(       opj_j2k_v2_t *p_j2k,
3893                                                         OPJ_BYTE * p_header_data,
3894                                                         OPJ_UINT32 p_header_size,
3895                                                         struct opj_event_mgr * p_manager)
3896 {
3897         OPJ_UINT32 l_num_comp,l_comp_no;
3898
3899         /* preconditions */
3900         assert(p_header_data != 00);
3901         assert(p_j2k != 00);
3902         assert(p_manager != 00);
3903
3904         l_num_comp = p_j2k->m_private_image->numcomps;
3905
3906         if (l_num_comp <= 256) {
3907                 if (p_header_size < 1) {
3908                         opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading QCC marker\n");
3909                         return OPJ_FALSE;
3910                 }
3911                 opj_read_bytes(p_header_data,&l_comp_no,1);
3912                 ++p_header_data;
3913                 --p_header_size;
3914         }
3915         else {
3916                 if (p_header_size < 2) {
3917                         opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading QCC marker\n");
3918                         return OPJ_FALSE;
3919                 }
3920                 opj_read_bytes(p_header_data,&l_comp_no,2);
3921                 p_header_data+=2;
3922                 p_header_size-=2;
3923         }
3924
3925 #ifdef USE_JPWL
3926         if (p_j2k->m_cp.correct) {
3927
3928                 static OPJ_UINT32 backup_compno = 0;
3929
3930                 /* compno is negative or larger than the number of components!!! */
3931                 if (/*(l_comp_no < 0) ||*/ (l_comp_no >= l_num_comp)) {
3932                         opj_event_msg_v2(p_manager, EVT_ERROR,
3933                                 "JPWL: bad component number in QCC (%d out of a maximum of %d)\n",
3934                                 l_comp_no, l_num_comp);
3935                         if (!JPWL_ASSUME) {
3936                                 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
3937                                 return OPJ_FALSE;
3938                         }
3939                         /* we try to correct */
3940                         l_comp_no = backup_compno % l_num_comp;
3941                         opj_event_msg_v2(p_manager, EVT_WARNING, "- trying to adjust this\n"
3942                                 "- setting component number to %d\n",
3943                                 l_comp_no);
3944                 }
3945
3946                 /* keep your private count of tiles */
3947                 backup_compno++;
3948         };
3949 #endif /* USE_JPWL */
3950
3951         if (! j2k_read_SQcd_SQcc(p_j2k,l_comp_no,p_header_data,&p_header_size,p_manager)) {
3952                 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading QCC marker\n");
3953                 return OPJ_FALSE;
3954         }
3955
3956         if (p_header_size != 0) {
3957                 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading QCC marker\n");
3958                 return OPJ_FALSE;
3959         }
3960
3961         return OPJ_TRUE;
3962 }
3963
3964
3965 static void j2k_write_poc(opj_j2k_t *j2k) {
3966         int len, numpchgs, i;
3967
3968         int numcomps = j2k->image->numcomps;
3969         
3970         opj_cp_t *cp = j2k->cp;
3971         opj_tcp_t *tcp = &cp->tcps[j2k->curtileno];
3972         opj_tccp_t *tccp = &tcp->tccps[0];
3973         opj_cio_t *cio = j2k->cio;
3974
3975         numpchgs = 1 + tcp->numpocs;
3976         cio_write(cio, J2K_MS_POC, 2);  /* POC  */
3977         len = 2 + (5 + 2 * (numcomps <= 256 ? 1 : 2)) * numpchgs;
3978         cio_write(cio, len, 2);         /* Lpoc */
3979         for (i = 0; i < numpchgs; i++) {
3980                 opj_poc_t *poc = &tcp->pocs[i];
3981                 cio_write(cio, poc->resno0, 1); /* RSpoc_i */
3982                 cio_write(cio, poc->compno0, (numcomps <= 256 ? 1 : 2));        /* CSpoc_i */
3983                 cio_write(cio, poc->layno1, 2); /* LYEpoc_i */
3984                 poc->layno1 = int_min(poc->layno1, tcp->numlayers);
3985                 cio_write(cio, poc->resno1, 1); /* REpoc_i */
3986                 poc->resno1 = int_min(poc->resno1, tccp->numresolutions);
3987                 cio_write(cio, poc->compno1, (numcomps <= 256 ? 1 : 2));        /* CEpoc_i */
3988                 poc->compno1 = int_min(poc->compno1, numcomps);
3989                 cio_write(cio, poc->prg, 1);    /* Ppoc_i */
3990         }
3991 }
3992
3993 /**
3994  * Writes the POC marker (Progression Order Change)
3995  * 
3996  * @param       p_stream                                the stream to write data to.
3997  * @param       p_j2k                           J2K codec.
3998  * @param       p_manager               the user event manager.
3999 */
4000 opj_bool j2k_write_poc_v2(      opj_j2k_v2_t *p_j2k,
4001                                                         struct opj_stream_private *p_stream,
4002                                                         struct opj_event_mgr * p_manager )
4003 {
4004         OPJ_UINT32 l_nb_comp;
4005         OPJ_UINT32 l_nb_poc;
4006         OPJ_UINT32 l_poc_size;
4007         OPJ_UINT32 l_written_size = 0;
4008         opj_tcp_v2_t *l_tcp = 00;
4009         opj_tccp_t *l_tccp = 00;
4010         OPJ_UINT32 l_poc_room;
4011
4012         // preconditions
4013         assert(p_j2k != 00);
4014         assert(p_manager != 00);
4015         assert(p_stream != 00);
4016
4017         l_tcp = &p_j2k->m_cp.tcps[p_j2k->m_current_tile_number];
4018         l_tccp = &l_tcp->tccps[0];
4019         l_nb_comp = p_j2k->m_private_image->numcomps;
4020         l_nb_poc = 1 + l_tcp->numpocs;
4021         
4022         if (l_nb_comp <= 256) {
4023                 l_poc_room = 1;
4024         }
4025         else {
4026                 l_poc_room = 2;
4027         }
4028         l_poc_size = 4 + (5 + 2 * l_poc_room) * l_nb_poc;
4029         
4030         if (l_poc_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
4031                 p_j2k->m_specific_param.m_encoder.m_header_tile_data 
4032                         = (OPJ_BYTE*)opj_realloc(
4033                                 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
4034                                 l_poc_size);
4035                 
4036                 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
4037                         return OPJ_FALSE;
4038                 }
4039
4040                 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_poc_size;
4041         }
4042
4043         j2k_write_poc_in_memory(p_j2k,p_j2k->m_specific_param.m_encoder.m_header_tile_data,&l_written_size,p_manager);
4044
4045         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) {
4046                 return OPJ_FALSE;
4047         }
4048
4049         return OPJ_TRUE;
4050 }
4051
4052
4053 /**
4054  * Writes the POC marker (Progression Order Change)
4055  *
4056  * @param       p_stream                                the stream to write data to.
4057  * @param       p_j2k                           J2K codec.
4058  * @param       p_manager               the user event manager.
4059 */
4060 void j2k_write_poc_in_memory(   opj_j2k_v2_t *p_j2k,
4061                                                                 OPJ_BYTE * p_data,
4062                                                                 OPJ_UINT32 * p_data_written,
4063                                                                 struct opj_event_mgr * p_manager )
4064 {
4065         OPJ_UINT32 i;
4066         OPJ_BYTE * l_current_data = 00;
4067         OPJ_UINT32 l_nb_comp;
4068         OPJ_UINT32 l_nb_poc;
4069         OPJ_UINT32 l_poc_size;
4070         opj_image_t *l_image = 00;
4071         opj_tcp_v2_t *l_tcp = 00;
4072         opj_tccp_t *l_tccp = 00;
4073         opj_poc_t *l_current_poc = 00;
4074         OPJ_UINT32 l_poc_room;
4075
4076         /* preconditions */
4077         assert(p_j2k != 00);
4078         assert(p_manager != 00);
4079
4080         l_tcp = &p_j2k->m_cp.tcps[p_j2k->m_current_tile_number];
4081         l_tccp = &l_tcp->tccps[0];
4082         l_image = p_j2k->m_private_image;
4083         l_nb_comp = l_image->numcomps;
4084         l_nb_poc = 1 + l_tcp->numpocs;
4085
4086         if (l_nb_comp <= 256) {
4087                 l_poc_room = 1;
4088         }
4089         else {
4090                 l_poc_room = 2;
4091         }
4092
4093         l_poc_size = 4 + (5 + 2 * l_poc_room) * l_nb_poc;
4094
4095         l_current_data = p_data;
4096
4097         opj_write_bytes(l_current_data,J2K_MS_POC,2);                                   /* POC  */
4098         l_current_data += 2;
4099
4100         opj_write_bytes(l_current_data,l_poc_size-2,2);                                 /* Lpoc */
4101         l_current_data += 2;
4102
4103         l_current_poc =  l_tcp->pocs;
4104         for (i = 0; i < l_nb_poc; ++i) {
4105                 opj_write_bytes(l_current_data,l_current_poc->resno0,1);                                /* RSpoc_i */
4106                 ++l_current_data;
4107
4108                 opj_write_bytes(l_current_data,l_current_poc->compno0,l_poc_room);              /* CSpoc_i */
4109                 l_current_data+=l_poc_room;
4110
4111                 opj_write_bytes(l_current_data,l_current_poc->layno1,2);                                /* LYEpoc_i */
4112                 l_current_data+=2;
4113
4114                 opj_write_bytes(l_current_data,l_current_poc->resno1,1);                                /* REpoc_i */
4115                 ++l_current_data;
4116
4117                 opj_write_bytes(l_current_data,l_current_poc->compno1,l_poc_room);              /* CEpoc_i */
4118                 l_current_data+=l_poc_room;
4119
4120                 opj_write_bytes(l_current_data,l_current_poc->prg,1);                                   /* Ppoc_i */
4121                 ++l_current_data;
4122
4123                 /* change the value of the max layer according to the actual number of layers in the file, components and resolutions*/
4124                 l_current_poc->layno1 = int_min(l_current_poc->layno1, l_tcp->numlayers);
4125                 l_current_poc->resno1 = int_min(l_current_poc->resno1, l_tccp->numresolutions);
4126                 l_current_poc->compno1 = int_min(l_current_poc->compno1, l_nb_comp);
4127
4128                 ++l_current_poc;
4129         }
4130
4131         *p_data_written = l_poc_size;
4132 }
4133
4134 /**
4135  * Gets the maximum size taken by the writing of a POC.
4136  */
4137 OPJ_UINT32 j2k_get_max_poc_size(opj_j2k_v2_t *p_j2k)
4138 {
4139         opj_tcp_v2_t * l_tcp = 00;
4140         OPJ_UINT32 l_nb_tiles = 0;
4141         OPJ_UINT32 l_max_poc = 0;
4142         OPJ_UINT32 i;
4143
4144         l_tcp = p_j2k->m_cp.tcps;
4145         l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw;
4146
4147         for (i=0;i<l_nb_tiles;++i) {
4148                 l_max_poc = uint_max(l_max_poc,l_tcp->numpocs);
4149                 ++l_tcp;
4150         }
4151
4152         ++l_max_poc;
4153
4154         return 4 + 9 * l_max_poc;
4155 }
4156
4157 /**
4158  * Gets the maximum size taken by the toc headers of all the tile parts of any given tile.
4159  */
4160 OPJ_UINT32 j2k_get_max_toc_size (opj_j2k_v2_t *p_j2k)
4161 {
4162         OPJ_UINT32 i;
4163         OPJ_UINT32 l_nb_tiles;
4164         OPJ_UINT32 l_max = 0;
4165         opj_tcp_v2_t * l_tcp = 00;
4166
4167         l_tcp = p_j2k->m_cp.tcps;
4168         l_nb_tiles = p_j2k->m_cp.tw * p_j2k->m_cp.th ;
4169
4170         for (i=0;i<l_nb_tiles;++i) {
4171                 l_max = uint_max(l_max,l_tcp->m_nb_tile_parts);
4172
4173                 ++l_tcp;
4174         }
4175
4176         return 12 * l_max;
4177 }
4178
4179
4180 /**
4181  * Gets the maximum size taken by the headers of the SOT.
4182  *
4183  * @param       p_j2k   the jpeg2000 codec to use.
4184  */
4185 OPJ_UINT32 j2k_get_specific_header_sizes(opj_j2k_v2_t *p_j2k)
4186 {
4187         OPJ_UINT32 l_nb_bytes = 0;
4188         OPJ_UINT32 l_nb_comps;
4189         OPJ_UINT32 l_coc_bytes,l_qcc_bytes;
4190
4191         l_nb_comps = p_j2k->m_private_image->numcomps - 1;
4192         l_nb_bytes += j2k_get_max_toc_size(p_j2k);
4193
4194         if (p_j2k->m_cp.m_specific_param.m_enc.m_cinema == 0) {
4195                 l_coc_bytes = j2k_get_max_coc_size(p_j2k);
4196                 l_nb_bytes += l_nb_comps * l_coc_bytes;
4197
4198                 l_qcc_bytes = j2k_get_max_qcc_size(p_j2k);
4199                 l_nb_bytes += l_nb_comps * l_qcc_bytes;
4200         }
4201
4202         l_nb_bytes += j2k_get_max_poc_size(p_j2k);
4203
4204         /*** DEVELOPER CORNER, Add room for your headers ***/
4205
4206         return l_nb_bytes;
4207 }
4208
4209 static void j2k_read_poc(opj_j2k_t *j2k) {
4210         int len, numpchgs, i, old_poc;
4211
4212         int numcomps = j2k->image->numcomps;
4213         
4214         opj_cp_t *cp = j2k->cp;
4215         opj_tcp_t *tcp = j2k->state == J2K_STATE_TPH ? &cp->tcps[j2k->curtileno] : j2k->default_tcp;
4216         opj_cio_t *cio = j2k->cio;
4217         
4218         old_poc = tcp->POC ? tcp->numpocs + 1 : 0;
4219         tcp->POC = 1;
4220         len = cio_read(cio, 2);         /* Lpoc */
4221         numpchgs = (len - 2) / (5 + 2 * (numcomps <= 256 ? 1 : 2));
4222         
4223         for (i = old_poc; i < numpchgs + old_poc; i++) {
4224                 opj_poc_t *poc;
4225                 poc = &tcp->pocs[i];
4226                 poc->resno0 = cio_read(cio, 1); /* RSpoc_i */
4227                 poc->compno0 = cio_read(cio, numcomps <= 256 ? 1 : 2);  /* CSpoc_i */
4228                 poc->layno1 = cio_read(cio, 2);    /* LYEpoc_i */
4229                 poc->resno1 = cio_read(cio, 1);    /* REpoc_i */
4230                 poc->compno1 = int_min(
4231                         cio_read(cio, numcomps <= 256 ? 1 : 2), (unsigned int) numcomps);       /* CEpoc_i */
4232                 poc->prg = (OPJ_PROG_ORDER)cio_read(cio, 1);    /* Ppoc_i */
4233         }
4234         
4235         tcp->numpocs = numpchgs + old_poc - 1;
4236 }
4237
4238 /**
4239  * Reads a POC marker (Progression Order Change)
4240  *
4241  * @param       p_header_data   the data contained in the POC box.
4242  * @param       p_j2k                   the jpeg2000 codec.
4243  * @param       p_header_size   the size of the data contained in the POC marker.
4244  * @param       p_manager               the user event manager.
4245 */
4246 opj_bool j2k_read_poc_v2 (
4247                                                 opj_j2k_v2_t *p_j2k,
4248                                                 OPJ_BYTE * p_header_data,
4249                                                 OPJ_UINT32 p_header_size,
4250                                                 struct opj_event_mgr * p_manager)
4251 {
4252         OPJ_UINT32 i, l_nb_comp, l_tmp;
4253         opj_image_t * l_image = 00;
4254         OPJ_UINT32 l_old_poc_nb, l_current_poc_nb, l_current_poc_remaining;
4255         OPJ_UINT32 l_chunk_size, l_comp_room;
4256
4257         opj_cp_v2_t *l_cp = 00;
4258         opj_tcp_v2_t *l_tcp = 00;
4259         opj_poc_t *l_current_poc = 00;
4260
4261         /* preconditions */
4262         assert(p_header_data != 00);
4263         assert(p_j2k != 00);
4264         assert(p_manager != 00);
4265
4266         l_image = p_j2k->m_private_image;
4267         l_nb_comp = l_image->numcomps;
4268         if (l_nb_comp <= 256) {
4269                 l_comp_room = 1;
4270         }
4271         else {
4272                 l_comp_room = 2;
4273         }
4274         l_chunk_size = 5 + 2 * l_comp_room;
4275         l_current_poc_nb = p_header_size / l_chunk_size;
4276         l_current_poc_remaining = p_header_size % l_chunk_size;
4277
4278         if ((l_current_poc_nb <= 0) || (l_current_poc_remaining != 0)) {
4279                 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading POC marker\n");
4280                 return OPJ_FALSE;
4281         }
4282
4283         l_cp = &(p_j2k->m_cp);
4284         l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ?
4285                                 &l_cp->tcps[p_j2k->m_current_tile_number] :
4286                                 p_j2k->m_specific_param.m_decoder.m_default_tcp;
4287         l_old_poc_nb = l_tcp->POC ? l_tcp->numpocs + 1 : 0;
4288         l_current_poc_nb += l_old_poc_nb;
4289
4290         assert(l_current_poc_nb < 32);
4291
4292         /* now poc is in use.*/
4293         l_tcp->POC = 1;
4294
4295         l_current_poc = &l_tcp->pocs[l_old_poc_nb];
4296         for     (i = l_old_poc_nb; i < l_current_poc_nb; ++i) {
4297                 opj_read_bytes(p_header_data,&(l_current_poc->resno0),1);                               /* RSpoc_i */
4298                 ++p_header_data;
4299                 opj_read_bytes(p_header_data,&(l_current_poc->compno0),l_comp_room);    /* CSpoc_i */
4300                 p_header_data+=l_comp_room;
4301                 opj_read_bytes(p_header_data,&(l_current_poc->layno1),2);                               /* LYEpoc_i */
4302                 p_header_data+=2;
4303                 opj_read_bytes(p_header_data,&(l_current_poc->resno1),1);                               /* REpoc_i */
4304                 ++p_header_data;
4305                 opj_read_bytes(p_header_data,&(l_current_poc->compno1),l_comp_room);    /* CEpoc_i */
4306                 p_header_data+=l_comp_room;
4307                 opj_read_bytes(p_header_data,&l_tmp,1);                                                                 /* Ppoc_i */
4308                 ++p_header_data;
4309                 l_current_poc->prg = (OPJ_PROG_ORDER) l_tmp;
4310                 /* make sure comp is in acceptable bounds */
4311                 l_current_poc->compno1 = uint_min(l_current_poc->compno1, l_nb_comp);
4312                 ++l_current_poc;
4313         }
4314
4315         l_tcp->numpocs = l_current_poc_nb - 1;
4316         return OPJ_TRUE;
4317 }
4318
4319 static void j2k_read_crg(opj_j2k_t *j2k) {
4320         int len, i, Xcrg_i, Ycrg_i;
4321         
4322         opj_cio_t *cio = j2k->cio;
4323         int numcomps = j2k->image->numcomps;
4324         
4325         len = cio_read(cio, 2);                 /* Lcrg */
4326         for (i = 0; i < numcomps; i++) {
4327                 Xcrg_i = cio_read(cio, 2);      /* Xcrg_i */
4328                 Ycrg_i = cio_read(cio, 2);      /* Ycrg_i */
4329         }
4330 }
4331
4332 /**
4333  * Reads a CRG marker (Component registration)
4334  *
4335  * @param       p_header_data   the data contained in the TLM box.
4336  * @param       p_j2k                   the jpeg2000 codec.
4337  * @param       p_header_size   the size of the data contained in the TLM marker.
4338  * @param       p_manager               the user event manager.
4339 */
4340 opj_bool j2k_read_crg_v2 (
4341                                                 opj_j2k_v2_t *p_j2k,
4342                                                 OPJ_BYTE * p_header_data,
4343                                                 OPJ_UINT32 p_header_size,
4344                                                 struct opj_event_mgr * p_manager
4345                                         )
4346 {
4347         OPJ_UINT32 l_nb_comp;
4348         /* preconditions */
4349         assert(p_header_data != 00);
4350         assert(p_j2k != 00);
4351         assert(p_manager != 00);
4352
4353         l_nb_comp = p_j2k->m_private_image->numcomps;
4354
4355         if (p_header_size != l_nb_comp *4) {
4356                 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading CRG marker\n");
4357                 return OPJ_FALSE;
4358         }
4359         /* Do not care of this at the moment since only local variables are set here */
4360         /*
4361         for
4362                 (i = 0; i < l_nb_comp; ++i)
4363         {
4364                 opj_read_bytes(p_header_data,&l_Xcrg_i,2);                              // Xcrg_i
4365                 p_header_data+=2;
4366                 opj_read_bytes(p_header_data,&l_Ycrg_i,2);                              // Xcrg_i
4367                 p_header_data+=2;
4368         }
4369         */
4370         return OPJ_TRUE;
4371 }
4372
4373 static void j2k_read_tlm(opj_j2k_t *j2k) {
4374         int len, Ztlm, Stlm, ST, SP, tile_tlm, i;
4375         long int Ttlm_i, Ptlm_i;
4376
4377         opj_cio_t *cio = j2k->cio;
4378         
4379         len = cio_read(cio, 2);         /* Ltlm */
4380         Ztlm = cio_read(cio, 1);        /* Ztlm */
4381         Stlm = cio_read(cio, 1);        /* Stlm */
4382         ST = ((Stlm >> 4) & 0x01) + ((Stlm >> 4) & 0x02);
4383         SP = (Stlm >> 6) & 0x01;
4384         tile_tlm = (len - 4) / ((SP + 1) * 2 + ST);
4385         for (i = 0; i < tile_tlm; i++) {
4386                 Ttlm_i = cio_read(cio, ST);     /* Ttlm_i */
4387                 Ptlm_i = cio_read(cio, SP ? 4 : 2);     /* Ptlm_i */
4388         }
4389 }
4390
4391 /**
4392  * Reads a TLM marker (Tile Length Marker)
4393  *
4394  * @param       p_header_data   the data contained in the TLM box.
4395  * @param       p_j2k                   the jpeg2000 codec.
4396  * @param       p_header_size   the size of the data contained in the TLM marker.
4397  * @param       p_manager               the user event manager.
4398 */
4399 opj_bool j2k_read_tlm_v2 (
4400                                                 opj_j2k_v2_t *p_j2k,
4401                                                 OPJ_BYTE * p_header_data,
4402                                                 OPJ_UINT32 p_header_size,
4403                                                 struct opj_event_mgr * p_manager
4404                                         )
4405 {
4406         OPJ_UINT32 l_Ztlm, l_Stlm, l_ST, l_SP, l_tot_num_tp, l_tot_num_tp_remaining, l_quotient, l_Ptlm_size;
4407         /* preconditions */
4408         assert(p_header_data != 00);
4409         assert(p_j2k != 00);
4410         assert(p_manager != 00);
4411
4412         if (p_header_size < 2) {
4413                 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading TLM marker\n");
4414                 return OPJ_FALSE;
4415         }
4416         p_header_size -= 2;
4417
4418         opj_read_bytes(p_header_data,&l_Ztlm,1);                                /* Ztlm */
4419         ++p_header_data;
4420         opj_read_bytes(p_header_data,&l_Stlm,1);                                /* Stlm */
4421         ++p_header_data;
4422
4423         l_ST = ((l_Stlm >> 4) & 0x3);
4424         l_SP = (l_Stlm >> 6) & 0x1;
4425
4426         l_Ptlm_size = (l_SP + 1) * 2;
4427         l_quotient = l_Ptlm_size + l_ST;
4428
4429         l_tot_num_tp = p_header_size / l_quotient;
4430         l_tot_num_tp_remaining = p_header_size % l_quotient;
4431
4432         if (l_tot_num_tp_remaining != 0) {
4433                 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading TLM marker\n");
4434                 return OPJ_FALSE;
4435         }
4436         /* FIXME Do not care of this at the moment since only local variables are set here */
4437         /*
4438         for
4439                 (i = 0; i < l_tot_num_tp; ++i)
4440         {
4441                 opj_read_bytes(p_header_data,&l_Ttlm_i,l_ST);                           // Ttlm_i
4442                 p_header_data += l_ST;
4443                 opj_read_bytes(p_header_data,&l_Ptlm_i,l_Ptlm_size);            // Ptlm_i
4444                 p_header_data += l_Ptlm_size;
4445         }*/
4446         return OPJ_TRUE;
4447 }
4448
4449 static void j2k_read_plm(opj_j2k_t *j2k) {
4450         int len, i, Zplm, Nplm, add, packet_len = 0;
4451         
4452         opj_cio_t *cio = j2k->cio;
4453
4454         len = cio_read(cio, 2);         /* Lplm */
4455         Zplm = cio_read(cio, 1);        /* Zplm */
4456         len -= 3;
4457         while (len > 0) {
4458                 Nplm = cio_read(cio, 4);                /* Nplm */
4459                 len -= 4;
4460                 for (i = Nplm; i > 0; i--) {
4461                         add = cio_read(cio, 1);
4462                         len--;
4463                         packet_len = (packet_len << 7) + add;   /* Iplm_ij */
4464                         if ((add & 0x80) == 0) {
4465                                 /* New packet */
4466                                 packet_len = 0;
4467                         }
4468                         if (len <= 0)
4469                                 break;
4470                 }
4471         }
4472 }
4473
4474 /**
4475  * Reads a PLM marker (Packet length, main header marker)
4476  *
4477  * @param       p_header_data   the data contained in the TLM box.
4478  * @param       p_j2k                   the jpeg2000 codec.
4479  * @param       p_header_size   the size of the data contained in the TLM marker.
4480  * @param       p_manager               the user event manager.
4481 */
4482 opj_bool j2k_read_plm_v2 (
4483                                                 opj_j2k_v2_t *p_j2k,
4484                                                 OPJ_BYTE * p_header_data,
4485                                                 OPJ_UINT32 p_header_size,
4486                                                 struct opj_event_mgr * p_manager
4487                                         )
4488 {
4489         /* preconditions */
4490         assert(p_header_data != 00);
4491         assert(p_j2k != 00);
4492         assert(p_manager != 00);
4493
4494         if (p_header_size < 1) {
4495                 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PLM marker\n");
4496                 return OPJ_FALSE;
4497         }
4498         /* Do not care of this at the moment since only local variables are set here */
4499         /*
4500         opj_read_bytes(p_header_data,&l_Zplm,1);                                        // Zplm
4501         ++p_header_data;
4502         --p_header_size;
4503
4504         while
4505                 (p_header_size > 0)
4506         {
4507                 opj_read_bytes(p_header_data,&l_Nplm,1);                                // Nplm
4508                 ++p_header_data;
4509                 p_header_size -= (1+l_Nplm);
4510                 if
4511                         (p_header_size < 0)
4512                 {
4513                         opj_event_msg(p_manager, EVT_ERROR, "Error reading PLM marker\n");
4514                         return false;
4515                 }
4516                 for
4517                         (i = 0; i < l_Nplm; ++i)
4518                 {
4519                         opj_read_bytes(p_header_data,&l_tmp,1);                         // Iplm_ij
4520                         ++p_header_data;
4521                         // take only the last seven bytes
4522                         l_packet_len |= (l_tmp & 0x7f);
4523                         if
4524                                 (l_tmp & 0x80)
4525                         {
4526                                 l_packet_len <<= 7;
4527                         }
4528                         else
4529                         {
4530                 // store packet length and proceed to next packet
4531                                 l_packet_len = 0;
4532                         }
4533                 }
4534                 if
4535                         (l_packet_len != 0)
4536                 {
4537                         opj_event_msg(p_manager, EVT_ERROR, "Error reading PLM marker\n");
4538                         return false;
4539                 }
4540         }
4541         */
4542         return OPJ_TRUE;
4543 }
4544
4545 static void j2k_read_plt(opj_j2k_t *j2k) {
4546         int len, i, Zplt, packet_len = 0, add;
4547         
4548         opj_cio_t *cio = j2k->cio;
4549         
4550         len = cio_read(cio, 2);         /* Lplt */
4551         Zplt = cio_read(cio, 1);        /* Zplt */
4552         for (i = len - 3; i > 0; i--) {
4553                 add = cio_read(cio, 1);
4554                 packet_len = (packet_len << 7) + add;   /* Iplt_i */
4555                 if ((add & 0x80) == 0) {
4556                         /* New packet */
4557                         packet_len = 0;
4558                 }
4559         }
4560 }
4561
4562 /**
4563  * Reads a PLT marker (Packet length, tile-part header)
4564  *
4565  * @param       p_header_data   the data contained in the PLT box.
4566  * @param       p_j2k                   the jpeg2000 codec.
4567  * @param       p_header_size   the size of the data contained in the PLT marker.
4568  * @param       p_manager               the user event manager.
4569 */
4570 opj_bool j2k_read_plt_v2 (
4571                                                 opj_j2k_v2_t *p_j2k,
4572                                                 OPJ_BYTE * p_header_data,
4573                                                 OPJ_UINT32 p_header_size,
4574                                                 struct opj_event_mgr * p_manager
4575                                         )
4576 {
4577         OPJ_UINT32 l_Zplt, l_tmp, l_packet_len = 0, i;
4578
4579         /* preconditions */
4580         assert(p_header_data != 00);
4581         assert(p_j2k != 00);
4582         assert(p_manager != 00);
4583
4584         if (p_header_size < 1) {
4585                 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PLT marker\n");
4586                 return OPJ_FALSE;
4587         }
4588
4589         opj_read_bytes(p_header_data,&l_Zplt,1);                /* Zplt */
4590         ++p_header_data;
4591         --p_header_size;
4592
4593         for (i = 0; i < p_header_size; ++i) {
4594                 opj_read_bytes(p_header_data,&l_tmp,1);         /* Iplt_ij */
4595                 ++p_header_data;
4596                 /* take only the last seven bytes */
4597                 l_packet_len |= (l_tmp & 0x7f);
4598                 if (l_tmp & 0x80) {
4599                         l_packet_len <<= 7;
4600                 }
4601                 else {
4602             /* store packet length and proceed to next packet */
4603                         l_packet_len = 0;
4604                 }
4605         }
4606
4607         if (l_packet_len != 0) {
4608                 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PLT marker\n");
4609                 return OPJ_FALSE;
4610         }
4611
4612         return OPJ_TRUE;
4613 }
4614
4615 static void j2k_read_ppm(opj_j2k_t *j2k) {
4616         int len, Z_ppm, i, j;
4617         int N_ppm;
4618
4619         opj_cp_t *cp = j2k->cp;
4620         opj_cio_t *cio = j2k->cio;
4621         
4622         len = cio_read(cio, 2);
4623         cp->ppm = 1;
4624         
4625         Z_ppm = cio_read(cio, 1);       /* Z_ppm */
4626         len -= 3;
4627         while (len > 0) {
4628                 if (cp->ppm_previous == 0) {
4629                         N_ppm = cio_read(cio, 4);       /* N_ppm */
4630                         len -= 4;
4631                 } else {
4632                         N_ppm = cp->ppm_previous;
4633                 }
4634                 j = cp->ppm_store;
4635                 if (Z_ppm == 0) {       /* First PPM marker */
4636                         cp->ppm_data = (unsigned char *) opj_malloc(N_ppm * sizeof(unsigned char));
4637                         cp->ppm_data_first = cp->ppm_data;
4638                         cp->ppm_len = N_ppm;
4639                 } else {                        /* NON-first PPM marker */
4640                         cp->ppm_data = (unsigned char *) opj_realloc(cp->ppm_data, (N_ppm +     cp->ppm_store) * sizeof(unsigned char));
4641
4642 #ifdef USE_JPWL
4643                         /* this memory allocation check could be done even in non-JPWL cases */
4644                         if (cp->correct) {
4645                                 if (!cp->ppm_data) {
4646                                         opj_event_msg(j2k->cinfo, EVT_ERROR,
4647                                                 "JPWL: failed memory allocation during PPM marker parsing (pos. %x)\n",
4648                                                 cio_tell(cio));
4649                                         if (!JPWL_ASSUME || JPWL_ASSUME) {
4650                                                 opj_free(cp->ppm_data);
4651                                                 opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
4652                                                 return;
4653                                         }
4654                                 }
4655                         }
4656 #endif
4657
4658                         cp->ppm_data_first = cp->ppm_data;
4659                         cp->ppm_len = N_ppm + cp->ppm_store;
4660                 }
4661                 for (i = N_ppm; i > 0; i--) {   /* Read packet header */
4662                         cp->ppm_data[j] = cio_read(cio, 1);
4663                         j++;
4664                         len--;
4665                         if (len == 0)
4666                                 break;                  /* Case of non-finished packet header in present marker but finished in next one */
4667                 }
4668                 cp->ppm_previous = i - 1;
4669                 cp->ppm_store = j;
4670         }
4671 }
4672 /**
4673  * Reads a PPM marker (Packed packet headers, main header)
4674  *
4675  * @param       p_header_data   the data contained in the POC box.
4676  * @param       p_j2k                   the jpeg2000 codec.
4677  * @param       p_header_size   the size of the data contained in the POC marker.
4678  * @param       p_manager               the user event manager.
4679 */
4680 #if 0
4681 opj_bool j2k_read_ppm_v2 (
4682                                                 opj_j2k_v2_t *p_j2k,
4683                                                 OPJ_BYTE * p_header_data,
4684                                                 OPJ_UINT32 p_header_size,
4685                                                 struct opj_event_mgr * p_manager
4686                                         )
4687 {
4688
4689         opj_cp_v2_t *l_cp = 00;
4690         OPJ_UINT32 l_remaining_data, l_Z_ppm, l_N_ppm;
4691
4692         /* preconditions */
4693         assert(p_header_data != 00);
4694         assert(p_j2k != 00);
4695         assert(p_manager != 00);
4696
4697         if (p_header_size < 1) {
4698                 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PPM marker\n");
4699                 return OPJ_FALSE;
4700         }
4701
4702         l_cp = &(p_j2k->m_cp);
4703         l_cp->ppm = 1;
4704
4705         opj_read_bytes(p_header_data,&l_Z_ppm,1);               /* Z_ppm */
4706         ++p_header_data;
4707         --p_header_size;
4708
4709         /* First PPM marker */
4710         if (l_Z_ppm == 0) {
4711                 if (p_header_size < 4) {
4712                         opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PPM marker\n");
4713                         return OPJ_FALSE;
4714                 }
4715
4716                 opj_read_bytes(p_header_data,&l_N_ppm,4);               /* N_ppm */
4717                 p_header_data+=4;
4718                 p_header_size-=4;
4719
4720                 /* First PPM marker: Initialization */
4721                 l_cp->ppm_len = l_N_ppm;
4722                 l_cp->ppm_data_size = 0;
4723
4724                 l_cp->ppm_buffer = (OPJ_BYTE *) opj_malloc(l_cp->ppm_len);
4725                 if (l_cp->ppm_buffer == 00) {
4726                         opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory reading ppm marker\n");
4727                         return OPJ_FALSE;
4728                 }
4729                 memset(l_cp->ppm_buffer,0,l_cp->ppm_len);
4730
4731                 l_cp->ppm_data = l_cp->ppm_buffer;
4732         }
4733
4734         while (1) {
4735                 if (l_cp->ppm_data_size == l_cp->ppm_len) {
4736                         if (p_header_size >= 4) {
4737                                 /* read a N_ppm */
4738                                 opj_read_bytes(p_header_data,&l_N_ppm,4);               /* N_ppm */
4739                                 p_header_data+=4;
4740                                 p_header_size-=4;
4741                                 l_cp->ppm_len += l_N_ppm ;
4742
4743                                 l_cp->ppm_buffer = (OPJ_BYTE *) opj_realloc(l_cp->ppm_buffer, l_cp->ppm_len);
4744                                 if (l_cp->ppm_buffer == 00) {
4745                                         opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory reading ppm marker\n");
4746                                         return OPJ_FALSE;
4747                                 }
4748                                 memset(l_cp->ppm_buffer+l_cp->ppm_data_size,0,l_N_ppm);
4749
4750                                 l_cp->ppm_data = l_cp->ppm_buffer;
4751                         }
4752                         else {
4753                                 return OPJ_FALSE;
4754                         }
4755                 }
4756
4757                 l_remaining_data = l_cp->ppm_len - l_cp->ppm_data_size;
4758
4759                 if (l_remaining_data <= p_header_size) {
4760                         /* we must store less information than available in the packet */
4761                         memcpy(l_cp->ppm_buffer + l_cp->ppm_data_size , p_header_data , l_remaining_data);
4762                         l_cp->ppm_data_size = l_cp->ppm_len;
4763                         p_header_size -= l_remaining_data;
4764                         p_header_data += l_remaining_data;
4765                 }
4766                 else {
4767                         memcpy(l_cp->ppm_buffer + l_cp->ppm_data_size , p_header_data , p_header_size);
4768                         l_cp->ppm_data_size += p_header_size;
4769                         p_header_data += p_header_size;
4770                         p_header_size = 0;
4771                         break;
4772                 }
4773         }
4774
4775         return OPJ_TRUE;
4776 }
4777 #endif
4778
4779
4780
4781 /**
4782  * Reads a PPM marker (Packed packet headers, main header)
4783  *
4784  * @param       p_header_data   the data contained in the POC box.
4785  * @param       p_j2k                   the jpeg2000 codec.
4786  * @param       p_header_size   the size of the data contained in the POC marker.
4787  * @param       p_manager               the user event manager.
4788 */
4789 opj_bool j2k_read_ppm_v3 (
4790                                                 opj_j2k_v2_t *p_j2k,
4791                                                 OPJ_BYTE * p_header_data,
4792                                                 OPJ_UINT32 p_header_size,
4793                                                 struct opj_event_mgr * p_manager
4794                                         )
4795 {
4796         opj_cp_v2_t *l_cp = 00;
4797         OPJ_UINT32 l_remaining_data, l_Z_ppm, l_N_ppm;
4798
4799         /* preconditions */
4800         assert(p_header_data != 00);
4801         assert(p_j2k != 00);
4802         assert(p_manager != 00);
4803
4804         /* Minimum size of PPM marker is equal to the size of Zppm element */
4805         if (p_header_size < 1) {
4806                 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PPM marker\n");
4807                 return OPJ_FALSE;
4808         }
4809
4810         l_cp = &(p_j2k->m_cp);
4811         l_cp->ppm = 1;
4812
4813         opj_read_bytes(p_header_data,&l_Z_ppm,1);               /* Z_ppm */
4814         ++p_header_data;
4815         --p_header_size;
4816
4817         /* First PPM marker */
4818         if (l_Z_ppm == 0) {
4819                 /* We need now at least the Nppm^0 element */
4820                 if (p_header_size < 4) {
4821                         opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PPM marker\n");
4822                         return OPJ_FALSE;
4823                 }
4824
4825                 opj_read_bytes(p_header_data,&l_N_ppm,4);               /* First N_ppm */
4826                 p_header_data+=4;
4827                 p_header_size-=4;
4828
4829                 /* First PPM marker: Initialization */
4830                 l_cp->ppm_len = l_N_ppm;
4831                 l_cp->ppm_data_read = 0;
4832
4833                 l_cp->ppm_data = (OPJ_BYTE *) opj_malloc(l_cp->ppm_len);
4834                 if (l_cp->ppm_data == 00) {
4835                         opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory reading ppm marker\n");
4836                         return OPJ_FALSE;
4837                 }
4838                 memset(l_cp->ppm_data,0,l_cp->ppm_len);
4839
4840                 l_cp->ppm_data_current = l_cp->ppm_data;
4841
4842                 /*l_cp->ppm_data = l_cp->ppm_buffer;*/
4843         }
4844         else {
4845                 if (p_header_size < 4) {
4846                         opj_event_msg_v2(p_manager, EVT_WARNING, "Empty PPM marker\n");
4847                         return OPJ_TRUE;
4848                 }
4849                 else {
4850                         /* Uncompleted Ippm series in the previous PPM marker?*/
4851                         if (l_cp->ppm_data_read < l_cp->ppm_len) {
4852                                 /* Get the place where add the remaining Ippm series*/
4853                                 l_cp->ppm_data_current = &(l_cp->ppm_data[l_cp->ppm_data_read]);
4854                                 l_N_ppm = l_cp->ppm_len - l_cp->ppm_data_read;
4855                         }
4856                         else {
4857                                 opj_read_bytes(p_header_data,&l_N_ppm,4);               /* First N_ppm */
4858                                 p_header_data+=4;
4859                                 p_header_size-=4;
4860
4861                                 /* Increase the size of ppm_data to add the new Ippm series*/
4862                                 l_cp->ppm_data = (OPJ_BYTE *) opj_realloc(l_cp->ppm_data, l_cp->ppm_len + l_N_ppm);
4863
4864                                 /* Keep the position of the place where concatenate the new series*/
4865                                 l_cp->ppm_data_current = &(l_cp->ppm_data[l_cp->ppm_len]);
4866                                 l_cp->ppm_len += l_N_ppm;
4867                         }
4868                 }
4869         }
4870
4871         l_remaining_data = p_header_size;
4872
4873         while (l_remaining_data >= l_N_ppm) {
4874                 /* read a complete Ippm series*/
4875                 memcpy(l_cp->ppm_data_current, p_header_data, l_N_ppm);
4876                 p_header_size -= l_N_ppm;
4877                 p_header_data += l_N_ppm;
4878
4879                 l_cp->ppm_data_read += l_N_ppm; /* Increase the number of data read*/
4880
4881                 if (p_header_size)
4882                 {
4883                         opj_read_bytes(p_header_data,&l_N_ppm,4);               /* N_ppm^i */
4884                         p_header_data+=4;
4885                         p_header_size-=4;
4886                 }
4887                 else {
4888                         l_remaining_data = p_header_size;
4889                         break;
4890                 }
4891
4892                 l_remaining_data = p_header_size;
4893
4894                 /* Next Ippm series is a complete series ?*/
4895                 if (l_remaining_data > l_N_ppm) {
4896                         /* Increase the size of ppm_data to add the new Ippm series*/
4897                         l_cp->ppm_data = (OPJ_BYTE *) opj_realloc(l_cp->ppm_data, l_cp->ppm_len + l_N_ppm);
4898
4899                         /* Keep the position of the place where concatenate the new series */
4900                         l_cp->ppm_data_current = &(l_cp->ppm_data[l_cp->ppm_len]);
4901                         l_cp->ppm_len += l_N_ppm;
4902                 }
4903
4904         }
4905
4906         /* Need to read an incomplete Ippm series*/
4907         if (l_remaining_data) {
4908                 l_cp->ppm_data = (OPJ_BYTE *) opj_realloc(l_cp->ppm_data, l_cp->ppm_len + l_N_ppm);
4909
4910                 /* Keep the position of the place where concatenate the new series*/
4911                 l_cp->ppm_data_current = &(l_cp->ppm_data[l_cp->ppm_len]);
4912                 l_cp->ppm_len += l_N_ppm;
4913
4914                 /* Read incomplete Ippm series*/
4915                 memcpy(l_cp->ppm_data_current, p_header_data, l_remaining_data);
4916                 p_header_size -= l_remaining_data;
4917                 p_header_data += l_remaining_data;
4918
4919                 l_cp->ppm_data_read += l_remaining_data; /* Increase the number of data read*/
4920         }
4921
4922 #ifdef CLEAN_MSD
4923
4924                 if (l_cp->ppm_data_size == l_cp->ppm_len) {
4925                         if (p_header_size >= 4) {
4926                                 /* read a N_ppm*/
4927                                 opj_read_bytes(p_header_data,&l_N_ppm,4);               /* N_ppm */
4928                                 p_header_data+=4;
4929                                 p_header_size-=4;
4930                                 l_cp->ppm_len += l_N_ppm ;
4931
4932                                 l_cp->ppm_buffer = (OPJ_BYTE *) opj_realloc(l_cp->ppm_buffer, l_cp->ppm_len);
4933                                 if (l_cp->ppm_buffer == 00) {
4934                                         opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory reading ppm marker\n");
4935                                         return OPJ_FALSE;
4936                                 }
4937                                 memset(l_cp->ppm_buffer+l_cp->ppm_data_size,0,l_N_ppm);
4938
4939                                 l_cp->ppm_data = l_cp->ppm_buffer;
4940                         }
4941                         else {
4942                                 return OPJ_FALSE;
4943                         }
4944                 }
4945
4946                 l_remaining_data = l_cp->ppm_len - l_cp->ppm_data_size;
4947
4948                 if (l_remaining_data <= p_header_size) {
4949                         /* we must store less information than available in the packet */
4950                         memcpy(l_cp->ppm_buffer + l_cp->ppm_data_size , p_header_data , l_remaining_data);
4951                         l_cp->ppm_data_size = l_cp->ppm_len;
4952                         p_header_size -= l_remaining_data;
4953                         p_header_data += l_remaining_data;
4954                 }
4955                 else {
4956                         memcpy(l_cp->ppm_buffer + l_cp->ppm_data_size , p_header_data , p_header_size);
4957                         l_cp->ppm_data_size += p_header_size;
4958                         p_header_data += p_header_size;
4959                         p_header_size = 0;
4960                         break;
4961                 }
4962         }
4963 #endif
4964         return OPJ_TRUE;
4965 }
4966
4967 static void j2k_read_ppt(opj_j2k_t *j2k) {
4968         int len, Z_ppt, i, j = 0;
4969
4970         opj_cp_t *cp = j2k->cp;
4971         opj_tcp_t *tcp = cp->tcps + j2k->curtileno;
4972         opj_cio_t *cio = j2k->cio;
4973
4974         len = cio_read(cio, 2);
4975         Z_ppt = cio_read(cio, 1);
4976         tcp->ppt = 1;
4977         if (Z_ppt == 0) {               /* First PPT marker */
4978                 tcp->ppt_data = (unsigned char *) opj_malloc((len - 3) * sizeof(unsigned char));
4979                 tcp->ppt_data_first = tcp->ppt_data;
4980                 tcp->ppt_store = 0;
4981                 tcp->ppt_len = len - 3;
4982         } else {                        /* NON-first PPT marker */
4983                 tcp->ppt_data = (unsigned char *) opj_realloc(tcp->ppt_data, (len - 3 + tcp->ppt_store) * sizeof(unsigned char));
4984                 tcp->ppt_data_first = tcp->ppt_data;
4985                 tcp->ppt_len = len - 3 + tcp->ppt_store;
4986         }
4987         j = tcp->ppt_store;
4988         for (i = len - 3; i > 0; i--) {
4989                 tcp->ppt_data[j] = cio_read(cio, 1);
4990                 j++;
4991         }
4992         tcp->ppt_store = j;
4993 }
4994
4995 /**
4996  * Reads a PPT marker (Packed packet headers, tile-part header)
4997  *
4998  * @param       p_header_data   the data contained in the PPT box.
4999  * @param       p_j2k                   the jpeg2000 codec.
5000  * @param       p_header_size   the size of the data contained in the PPT marker.
5001  * @param       p_manager               the user event manager.
5002 */
5003 opj_bool j2k_read_ppt_v2 (      opj_j2k_v2_t *p_j2k,
5004                                                         OPJ_BYTE * p_header_data,
5005                                                         OPJ_UINT32 p_header_size,
5006                                                         struct opj_event_mgr * p_manager )
5007 {
5008         opj_cp_v2_t *l_cp = 00;
5009         opj_tcp_v2_t *l_tcp = 00;
5010         OPJ_UINT32 l_Z_ppt;
5011
5012         /* preconditions */
5013         assert(p_header_data != 00);
5014         assert(p_j2k != 00);
5015         assert(p_manager != 00);
5016
5017         /* We need to have the Z_ppt element at minimum */
5018         if (p_header_size < 1) {
5019                 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PPT marker\n");
5020                 return OPJ_FALSE;
5021         }
5022
5023         l_cp = &(p_j2k->m_cp);
5024         if (l_cp->ppm){
5025                 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");
5026                 return OPJ_FALSE;
5027         }
5028
5029         l_tcp = &(l_cp->tcps[p_j2k->m_current_tile_number]);
5030         l_tcp->ppt = 1;
5031
5032         opj_read_bytes(p_header_data,&l_Z_ppt,1);               /* Z_ppt */
5033         ++p_header_data;
5034         --p_header_size;
5035
5036         /* Allocate buffer to read the packet header */
5037         if (l_Z_ppt == 0) {
5038                 /* First PPT marker */
5039                 l_tcp->ppt_data_size = 0;
5040                 l_tcp->ppt_len = p_header_size;
5041
5042                 l_tcp->ppt_buffer = (OPJ_BYTE *) opj_calloc(l_tcp->ppt_len, sizeof(OPJ_BYTE) );
5043                 if (l_tcp->ppt_buffer == 00) {
5044                         opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory reading PPT marker\n");
5045                         return OPJ_FALSE;
5046                 }
5047                 l_tcp->ppt_data = l_tcp->ppt_buffer;
5048
5049                 /* memset(l_tcp->ppt_buffer,0,l_tcp->ppt_len); */
5050         }
5051         else {
5052                 l_tcp->ppt_len += p_header_size;
5053
5054                 l_tcp->ppt_buffer = (OPJ_BYTE *) opj_realloc(l_tcp->ppt_buffer,l_tcp->ppt_len);
5055                 if (l_tcp->ppt_buffer == 00) {
5056                         opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory reading PPT marker\n");
5057                         return OPJ_FALSE;
5058                 }
5059                 l_tcp->ppt_data = l_tcp->ppt_buffer;
5060
5061                 memset(l_tcp->ppt_buffer+l_tcp->ppt_data_size,0,p_header_size);
5062         }
5063
5064         /* Read packet header from buffer */
5065         memcpy(l_tcp->ppt_buffer+l_tcp->ppt_data_size,p_header_data,p_header_size);
5066
5067         l_tcp->ppt_data_size += p_header_size;
5068
5069         return OPJ_TRUE;
5070 }
5071
5072 /**
5073  * Writes the TLM marker (Tile Length Marker)
5074  * 
5075  * @param       p_stream                                the stream to write data to.
5076  * @param       p_j2k                           J2K codec.
5077  * @param       p_manager               the user event manager.
5078 */
5079 opj_bool j2k_write_tlm_v2(      opj_j2k_v2_t *p_j2k,
5080                                                         struct opj_stream_private *p_stream,
5081                                                         struct opj_event_mgr * p_manager )
5082 {
5083         OPJ_BYTE * l_current_data = 00;
5084         OPJ_UINT32 l_tlm_size;
5085
5086         // preconditions
5087         assert(p_j2k != 00);
5088         assert(p_manager != 00);
5089         assert(p_stream != 00);
5090
5091         l_tlm_size = 6 + (5*p_j2k->m_specific_param.m_encoder.m_total_tile_parts);
5092         
5093         if (l_tlm_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
5094                 p_j2k->m_specific_param.m_encoder.m_header_tile_data 
5095                         = (OPJ_BYTE*)opj_realloc(
5096                                 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
5097                                 l_tlm_size);
5098                 
5099                 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
5100                         return OPJ_FALSE;
5101                 }
5102
5103                 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_tlm_size;
5104         }
5105
5106         l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
5107
5108         /* change the way data is written to avoid seeking if possible */
5109         // TODO
5110         p_j2k->m_specific_param.m_encoder.m_tlm_start = opj_stream_tell(p_stream);
5111         
5112         opj_write_bytes(l_current_data,J2K_MS_TLM,2);                                   /* TLM */
5113         l_current_data += 2;
5114         
5115         opj_write_bytes(l_current_data,l_tlm_size-2,2);                                 /* Lpoc */
5116         l_current_data += 2;
5117         
5118         opj_write_bytes(l_current_data,0,1);                                                    /* Ztlm=0*/
5119         ++l_current_data;
5120         
5121         opj_write_bytes(l_current_data,0x50,1);                                                 /* Stlm ST=1(8bits-255 tiles max),SP=1(Ptlm=32bits) */
5122         ++l_current_data;
5123         
5124         /* do nothing on the 5 * l_j2k->m_specific_param.m_encoder.m_total_tile_parts remaining data */
5125         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) {
5126                 return OPJ_FALSE;
5127         }
5128
5129         return OPJ_TRUE;
5130 }
5131
5132 static void j2k_write_tlm(opj_j2k_t *j2k){
5133         int lenp;
5134         opj_cio_t *cio = j2k->cio;
5135         j2k->tlm_start = cio_tell(cio);
5136         cio_write(cio, J2K_MS_TLM, 2);/* TLM */
5137         lenp = 4 + (5*j2k->totnum_tp);
5138         cio_write(cio,lenp,2);                          /* Ltlm */
5139         cio_write(cio, 0,1);                                    /* Ztlm=0*/
5140         cio_write(cio,80,1);                                    /* Stlm ST=1(8bits-255 tiles max),SP=1(Ptlm=32bits) */
5141         cio_skip(cio,5*j2k->totnum_tp);
5142 }
5143
5144 static void j2k_write_sot(opj_j2k_t *j2k) {
5145         int lenp, len;
5146
5147         opj_cio_t *cio = j2k->cio;
5148
5149         j2k->sot_start = cio_tell(cio);
5150         cio_write(cio, J2K_MS_SOT, 2);          /* SOT */
5151         lenp = cio_tell(cio);
5152         cio_skip(cio, 2);                                       /* Lsot (further) */
5153         cio_write(cio, j2k->curtileno, 2);      /* Isot */
5154         cio_skip(cio, 4);                                       /* Psot (further in j2k_write_sod) */
5155         cio_write(cio, j2k->cur_tp_num , 1);    /* TPsot */
5156         cio_write(cio, j2k->cur_totnum_tp[j2k->curtileno], 1);          /* TNsot */
5157         len = cio_tell(cio) - lenp;
5158         cio_seek(cio, lenp);
5159         cio_write(cio, len, 2);                         /* Lsot */
5160         cio_seek(cio, lenp + len);
5161
5162         /* UniPG>> */
5163 #ifdef USE_JPWL
5164         /* update markers struct */
5165         j2k_add_marker(j2k->cstr_info, J2K_MS_SOT, j2k->sot_start, len + 2);
5166 #endif /* USE_JPWL */
5167         /* <<UniPG */
5168
5169         if( j2k->cstr_info && j2k->cur_tp_num==0){
5170           j2k_add_tlmarker( j2k->curtileno, j2k->cstr_info, J2K_MS_SOT, lenp, len);
5171         }
5172 }
5173
5174 /**
5175  * Writes the SOT marker (Start of tile-part)
5176  *
5177  * @param       p_stream                                the stream to write data to.
5178  * @param       p_j2k                           J2K codec.
5179  * @param       p_manager               the user event manager.
5180 */
5181 opj_bool j2k_write_sot_v2(      opj_j2k_v2_t *p_j2k,
5182                                                         OPJ_BYTE * p_data,
5183                                                         OPJ_UINT32 * p_data_written,
5184                                                         const struct opj_stream_private *p_stream,
5185                                                         struct opj_event_mgr * p_manager )
5186 {
5187         /* preconditions */
5188         assert(p_j2k != 00);
5189         assert(p_manager != 00);
5190         assert(p_stream != 00);
5191
5192         opj_write_bytes(p_data,J2K_MS_SOT,2);                                   /* SOT */
5193         p_data += 2;
5194
5195         opj_write_bytes(p_data,10,2);                                                   /* Lsot */
5196         p_data += 2;
5197
5198         opj_write_bytes(p_data, p_j2k->m_current_tile_number,2);                        /* Isot */
5199         p_data += 2;
5200
5201         /* Psot  */
5202         p_data += 4;
5203
5204         opj_write_bytes(p_data, p_j2k->m_specific_param.m_encoder.m_current_tile_part_number,1);                        /* TPsot */
5205         ++p_data;
5206
5207         opj_write_bytes(p_data, p_j2k->m_cp.tcps[p_j2k->m_current_tile_number].m_nb_tile_parts,1);                      /* TNsot */
5208         ++p_data;
5209
5210         /* UniPG>> */
5211 #ifdef USE_JPWL
5212         /* update markers struct */
5213 /*
5214         j2k_add_marker(p_j2k->cstr_info, J2K_MS_SOT, p_j2k->sot_start, len + 2);
5215 */
5216   assert( 0 && "TODO" );
5217 #endif /* USE_JPWL */
5218
5219         * p_data_written = 12;
5220
5221         return OPJ_TRUE;
5222 }
5223
5224
5225 static void j2k_read_sot(opj_j2k_t *j2k) {
5226         int len, tileno, totlen, partno, numparts;
5227   OPJ_UINT32 i;
5228         opj_tcp_t *tcp = NULL;
5229         char status = 0;
5230
5231         opj_cp_t *cp = j2k->cp;
5232         opj_cio_t *cio = j2k->cio;
5233
5234         len = cio_read(cio, 2);
5235         tileno = cio_read(cio, 2);
5236
5237 #ifdef USE_JPWL
5238         if (j2k->cp->correct) {
5239
5240                 static int backup_tileno = 0;
5241
5242                 /* tileno is negative or larger than the number of tiles!!! */
5243                 if ((tileno < 0) || (tileno >= (cp->tw * cp->th))) {
5244                         opj_event_msg(j2k->cinfo, EVT_ERROR,
5245                                 "JPWL: bad tile number (%d out of a maximum of %d)\n",
5246                                 tileno, (cp->tw * cp->th));
5247                         if (!JPWL_ASSUME) {
5248                                 opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
5249                                 return;
5250                         }
5251                         /* we try to correct */
5252                         tileno = backup_tileno;
5253                         opj_event_msg(j2k->cinfo, EVT_WARNING, "- trying to adjust this\n"
5254                                 "- setting tile number to %d\n",
5255                                 tileno);
5256                 }
5257
5258                 /* keep your private count of tiles */
5259                 backup_tileno++;
5260         }
5261   else
5262 #endif /* USE_JPWL */
5263   {
5264     /* tileno is negative or larger than the number of tiles!!! */
5265     if ((tileno < 0) || (tileno >= (cp->tw * cp->th))) {
5266       opj_event_msg(j2k->cinfo, EVT_ERROR,
5267         "JPWL: bad tile number (%d out of a maximum of %d)\n",
5268         tileno, (cp->tw * cp->th));
5269       return;
5270     }
5271   }
5272         
5273         if (cp->tileno_size == 0) {
5274                 cp->tileno[cp->tileno_size] = tileno;
5275                 cp->tileno_size++;
5276         } else {
5277                 i = 0;
5278                 assert(cp->tileno_size >= 0);
5279                 while (i < (OPJ_UINT32)cp->tileno_size && status == 0) {
5280                         status = cp->tileno[i] == tileno ? 1 : 0;
5281                         i++;
5282                 }
5283                 if (status == 0) {
5284                         cp->tileno[cp->tileno_size] = tileno;
5285                         cp->tileno_size++;
5286                 }
5287         }
5288         
5289         totlen = cio_read(cio, 4);
5290
5291 #ifdef USE_JPWL
5292         if (j2k->cp->correct) {
5293
5294                 /* totlen is negative or larger than the bytes left!!! */
5295                 if ((totlen < 0) || (totlen > (cio_numbytesleft(cio) + 8))) {
5296                         opj_event_msg(j2k->cinfo, EVT_ERROR,
5297                                 "JPWL: bad tile byte size (%d bytes against %d bytes left)\n",
5298                                 totlen, cio_numbytesleft(cio) + 8);
5299                         if (!JPWL_ASSUME) {
5300                                 opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
5301                                 return;
5302                         }
5303                         /* we try to correct */
5304                         totlen = 0;
5305                         opj_event_msg(j2k->cinfo, EVT_WARNING, "- trying to adjust this\n"
5306                                 "- setting Psot to %d => assuming it is the last tile\n",
5307                                 totlen);
5308                 }
5309
5310         }
5311   else
5312 #endif /* USE_JPWL */
5313   {
5314     /* totlen is negative or larger than the bytes left!!! */
5315     if ((totlen < 0) || (totlen > (cio_numbytesleft(cio) + 8))) {
5316       opj_event_msg(j2k->cinfo, EVT_ERROR,
5317         "JPWL: bad tile byte size (%d bytes against %d bytes left)\n",
5318         totlen, cio_numbytesleft(cio) + 8);
5319       return;
5320     }
5321   }
5322
5323         if (!totlen)
5324                 totlen = cio_numbytesleft(cio) + 8;
5325         
5326         partno = cio_read(cio, 1);
5327         numparts = cio_read(cio, 1);
5328   
5329   if (partno >= numparts) {
5330     opj_event_msg(j2k->cinfo, EVT_WARNING, "SOT marker inconsistency in tile %d: tile-part index greater (%d) than number of tile-parts (%d)\n", tileno, partno, numparts);
5331     numparts = partno+1;
5332   }
5333         
5334         j2k->curtileno = tileno;
5335         j2k->cur_tp_num = partno;
5336         j2k->eot = cio_getbp(cio) - 12 + totlen;
5337         j2k->state = J2K_STATE_TPH;
5338         tcp = &cp->tcps[j2k->curtileno];
5339
5340         /* Index */
5341         if (j2k->cstr_info) {
5342                 if (tcp->first) {
5343                         if (tileno == 0) 
5344                                 j2k->cstr_info->main_head_end = cio_tell(cio) - 13;
5345                         j2k->cstr_info->tile[tileno].tileno = tileno;
5346                         j2k->cstr_info->tile[tileno].start_pos = cio_tell(cio) - 12;
5347                         j2k->cstr_info->tile[tileno].end_pos = j2k->cstr_info->tile[tileno].start_pos + totlen - 1;                             
5348     } else {
5349                         j2k->cstr_info->tile[tileno].end_pos += totlen;
5350                 }
5351     j2k->cstr_info->tile[tileno].num_tps = numparts;
5352     if (numparts)
5353       j2k->cstr_info->tile[tileno].tp = (opj_tp_info_t *) opj_realloc(j2k->cstr_info->tile[tileno].tp, numparts * sizeof(opj_tp_info_t));
5354     else
5355       j2k->cstr_info->tile[tileno].tp = (opj_tp_info_t *) opj_realloc(j2k->cstr_info->tile[tileno].tp, 10 * sizeof(opj_tp_info_t)); /* Fixme (10)*/
5356                 j2k->cstr_info->tile[tileno].tp[partno].tp_start_pos = cio_tell(cio) - 12;
5357                 j2k->cstr_info->tile[tileno].tp[partno].tp_end_pos = 
5358                         j2k->cstr_info->tile[tileno].tp[partno].tp_start_pos + totlen - 1;
5359         }
5360         
5361         if (tcp->first == 1) {          
5362                 /* Initialization PPT */
5363                 opj_tccp_t *tmp = tcp->tccps;
5364                 memcpy(tcp, j2k->default_tcp, sizeof(opj_tcp_t));
5365                 tcp->ppt = 0;
5366                 tcp->ppt_data = NULL;
5367                 tcp->ppt_data_first = NULL;
5368                 tcp->tccps = tmp;
5369
5370                 for (i = 0; i < j2k->image->numcomps; i++) {
5371                         tcp->tccps[i] = j2k->default_tcp->tccps[i];
5372                 }
5373                 cp->tcps[j2k->curtileno].first = 0;
5374         }
5375 }
5376
5377 /**
5378  * Reads a PPT marker (Packed packet headers, tile-part header)
5379  *
5380  * @param       p_header_data   the data contained in the PPT box.
5381  * @param       p_j2k                   the jpeg2000 codec.
5382  * @param       p_header_size   the size of the data contained in the PPT marker.
5383  * @param       p_manager               the user event manager.
5384 */
5385 opj_bool j2k_read_sot_v2 (
5386                                                 opj_j2k_v2_t *p_j2k,
5387                                                 OPJ_BYTE * p_header_data,
5388                                                 OPJ_UINT32 p_header_size,
5389                                                 struct opj_event_mgr * p_manager
5390                                         )
5391 {
5392
5393         opj_cp_v2_t *l_cp = 00;
5394         opj_tcp_v2_t *l_tcp = 00;
5395         OPJ_UINT32 l_tot_len, l_num_parts = 0;
5396         OPJ_UINT32 l_current_part;
5397         OPJ_UINT32 l_tile_x,l_tile_y;
5398
5399         /* preconditions */
5400         assert(p_header_data != 00);
5401         assert(p_j2k != 00);
5402         assert(p_manager != 00);
5403
5404         /* Size of this marker is fixed = 12 (we have already read marker and its size)*/
5405         if (p_header_size != 8) {
5406                 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading SOT marker\n");
5407                 return OPJ_FALSE;
5408         }
5409
5410         l_cp = &(p_j2k->m_cp);
5411         opj_read_bytes(p_header_data,&(p_j2k->m_current_tile_number),2);                /* Isot */
5412         p_header_data+=2;
5413
5414         l_tcp = &l_cp->tcps[p_j2k->m_current_tile_number];
5415         l_tile_x = p_j2k->m_current_tile_number % l_cp->tw;
5416         l_tile_y = p_j2k->m_current_tile_number / l_cp->tw;
5417
5418 #ifdef USE_JPWL
5419         if (l_cp->correct) {
5420
5421                 int tileno = p_j2k->m_current_tile_number;
5422                 static int backup_tileno = 0;
5423
5424                 /* tileno is negative or larger than the number of tiles!!! */
5425                 if ((tileno < 0) || (tileno > (l_cp->tw * l_cp->th))) {
5426                         opj_event_msg_v2(p_manager, EVT_ERROR,
5427                                 "JPWL: bad tile number (%d out of a maximum of %d)\n",
5428                                 tileno, (l_cp->tw * l_cp->th));
5429                         if (!JPWL_ASSUME) {
5430                                 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
5431                                 return OPJ_FALSE;
5432                         }
5433                         /* we try to correct */
5434                         tileno = backup_tileno;
5435                         opj_event_msg_v2(p_manager, EVT_WARNING, "- trying to adjust this\n"
5436                                 "- setting tile number to %d\n",
5437                                 tileno);
5438                 }
5439
5440                 /* keep your private count of tiles */
5441                 backup_tileno++;
5442         };
5443 #endif /* USE_JPWL */
5444
5445         /* look for the tile in the list of already processed tile (in parts). */
5446         /* Optimization possible here with a more complex data structure and with the removing of tiles */
5447         /* since the time taken by this function can only grow at the time */
5448
5449         opj_read_bytes(p_header_data,&l_tot_len,4);             /* Psot */
5450         p_header_data+=4;
5451
5452         /* PSot should be equal to zero or >=14 or <= 2^32-1 */
5453         if ((l_tot_len !=0 ) && (l_tot_len < 14) )
5454         {
5455                 opj_event_msg_v2(p_manager, EVT_ERROR, "Psot value (%d) is not correct regards to the JPEG2000 norm!\n", l_tot_len);
5456                 return OPJ_FALSE;
5457         }
5458
5459
5460 #ifdef USE_JPWL
5461         if (l_cp->correct) {
5462
5463                 /* totlen is negative or larger than the bytes left!!! */
5464                 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))) { */
5465                         opj_event_msg_v2(p_manager, EVT_ERROR,
5466                                 "JPWL: bad tile byte size (%d bytes against %d bytes left)\n",
5467                                 l_tot_len, p_header_size ); /* FIXME it seems correct; for info in V1 -> p_stream_numbytesleft(p_stream) + 8); */
5468                         if (!JPWL_ASSUME) {
5469                                 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
5470                                 return OPJ_FALSE;
5471                         }
5472                         /* we try to correct */
5473                         l_tot_len = 0;
5474                         opj_event_msg_v2(p_manager, EVT_WARNING, "- trying to adjust this\n"
5475                                 "- setting Psot to %d => assuming it is the last tile\n",
5476                                 l_tot_len);
5477                 }
5478         };
5479 #endif /* USE_JPWL */
5480
5481         /* Ref A.4.2: Psot could be equal zero if it is the last tile-part of the codestream.*/
5482         if (!l_tot_len) {
5483                 opj_event_msg_v2(p_manager, EVT_INFO, "Psot value of the current tile-part is equal to zero, "
5484                                 "we assuming it is the last tile-part of the codestream.\n");
5485                 p_j2k->m_specific_param.m_decoder.m_last_tile_part = 1;
5486         }
5487
5488         opj_read_bytes(p_header_data,&l_current_part ,1);       /* TPsot */
5489         ++p_header_data;
5490
5491         opj_read_bytes(p_header_data,&l_num_parts ,1);          /* TNsot */
5492         ++p_header_data;
5493
5494         if (l_num_parts != 0) { /* Number of tile-part header is provided by this tile-part header */
5495                 /* Useful to manage the case of textGBR.jp2 file because two values of TNSot are allowed: the correct numbers of
5496                  * tile-parts for that tile and zero (A.4.2 of 15444-1 : 2002). */
5497                 if (l_tcp->m_nb_tile_parts) {
5498                         if (l_current_part >= l_tcp->m_nb_tile_parts){
5499                                 opj_event_msg_v2(p_manager, EVT_ERROR, "In SOT marker, TPSot (%d) is not valid regards to the current "
5500                                                 "number of tile-part (%d), giving up\n", l_current_part, l_tcp->m_nb_tile_parts );
5501                                 p_j2k->m_specific_param.m_decoder.m_last_tile_part = 1;
5502                                 return OPJ_FALSE;
5503                         }
5504                 }
5505                 l_tcp->m_nb_tile_parts = l_num_parts;
5506         }
5507
5508         /* If know the number of tile part header we will check if we didn't read the last*/
5509         if (l_tcp->m_nb_tile_parts) {
5510                 if (l_tcp->m_nb_tile_parts == (l_current_part + 1)) {
5511                         p_j2k->m_specific_param.m_decoder.m_can_decode = 1; /* Process the last tile-part header*/
5512                 }
5513         }
5514
5515         if (!p_j2k->m_specific_param.m_decoder.m_last_tile_part){
5516                 /* Keep the size of data to skip after this marker */
5517                 p_j2k->m_specific_param.m_decoder.m_sot_length = l_tot_len - 12; /* SOT_marker_size = 12 */
5518         }
5519         else {
5520                 /* FIXME: need to be computed from the number of bytes remaining in the codestream */
5521                 p_j2k->m_specific_param.m_decoder.m_sot_length = 0;
5522         }
5523
5524         p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_TPH;
5525
5526         /* Check if the current tile is outside the area we want decode or not corresponding to the tile index*/
5527         if (p_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec == -1) {
5528                 p_j2k->m_specific_param.m_decoder.m_skip_data =
5529                                 (l_tile_x < p_j2k->m_specific_param.m_decoder.m_start_tile_x)
5530                         ||      (l_tile_x >= p_j2k->m_specific_param.m_decoder.m_end_tile_x)
5531                         ||  (l_tile_y < p_j2k->m_specific_param.m_decoder.m_start_tile_y)
5532                         ||      (l_tile_y >= p_j2k->m_specific_param.m_decoder.m_end_tile_y);
5533         }
5534   else {
5535       assert( p_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec >= 0 );
5536     p_j2k->m_specific_param.m_decoder.m_skip_data =
5537       (p_j2k->m_current_tile_number != (OPJ_UINT32)p_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec);
5538     }
5539
5540         /* Index */
5541         if (p_j2k->cstr_index)
5542         {
5543     assert(p_j2k->cstr_index->tile_index != 00);
5544                 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tileno = p_j2k->m_current_tile_number;
5545                 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_tpsno = l_current_part;
5546
5547                 if (l_num_parts != 0){
5548                         p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].nb_tps = l_num_parts;
5549                         p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps = l_num_parts;
5550
5551
5552                         if (!p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index)
5553                                 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index =
5554                                         (opj_tp_index_t*)opj_calloc(l_num_parts, sizeof(opj_tp_index_t));
5555                         else
5556                                 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index =
5557                                         (opj_tp_index_t*)opj_realloc(
5558                                                         p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index,
5559                                                         l_num_parts* sizeof(opj_tp_index_t));
5560                 }
5561                 else{
5562                         /*if (!p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index)*/ {
5563
5564                                 if (!p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index) {
5565                                         p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps = 10;
5566                                         p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index =
5567                                                 (opj_tp_index_t*)opj_calloc( p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps,
5568                                                                                                          sizeof(opj_tp_index_t));
5569                                 }
5570
5571                                 if ( l_current_part >= p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps ){
5572                                         p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps += 10;
5573                                         p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index =
5574                                                 (opj_tp_index_t*)opj_realloc( p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index,
5575                                                                                                           p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps
5576                                                                                                           * sizeof(opj_tp_index_t));
5577                                 }
5578                         }
5579
5580                 }
5581
5582         }
5583
5584
5585         /* 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 */
5586         /* if (p_j2k->cstr_info) {
5587                 if (l_tcp->first) {
5588                         if (tileno == 0) {
5589                                 p_j2k->cstr_info->main_head_end = p_stream_tell(p_stream) - 13;
5590                         }
5591
5592                         p_j2k->cstr_info->tile[tileno].tileno = tileno;
5593                         p_j2k->cstr_info->tile[tileno].start_pos = p_stream_tell(p_stream) - 12;
5594                         p_j2k->cstr_info->tile[tileno].end_pos = p_j2k->cstr_info->tile[tileno].start_pos + totlen - 1;
5595                         p_j2k->cstr_info->tile[tileno].num_tps = numparts;
5596
5597                         if (numparts) {
5598                                 p_j2k->cstr_info->tile[tileno].tp = (opj_tp_info_t *) opj_malloc(numparts * sizeof(opj_tp_info_t));
5599                         }
5600                         else {
5601                                 p_j2k->cstr_info->tile[tileno].tp = (opj_tp_info_t *) opj_malloc(10 * sizeof(opj_tp_info_t)); // Fixme (10)
5602                         }
5603                 }
5604                 else {
5605                         p_j2k->cstr_info->tile[tileno].end_pos += totlen;
5606                 }
5607
5608                 p_j2k->cstr_info->tile[tileno].tp[partno].tp_start_pos = p_stream_tell(p_stream) - 12;
5609                 p_j2k->cstr_info->tile[tileno].tp[partno].tp_end_pos =
5610                 p_j2k->cstr_info->tile[tileno].tp[partno].tp_start_pos + totlen - 1;
5611         }*/
5612         return OPJ_TRUE;
5613 }
5614
5615 static void j2k_write_sod(opj_j2k_t *j2k, void *tile_coder) {
5616         int l, layno;
5617         int totlen;
5618         opj_tcp_t *tcp = NULL;
5619         opj_codestream_info_t *cstr_info = NULL;
5620         
5621         opj_tcd_t *tcd = (opj_tcd_t*)tile_coder;        /* cast is needed because of conflicts in header inclusions */
5622         opj_cp_t *cp = j2k->cp;
5623         opj_cio_t *cio = j2k->cio;
5624
5625         tcd->tp_num = j2k->tp_num ;
5626         tcd->cur_tp_num = j2k->cur_tp_num;
5627         
5628         cio_write(cio, J2K_MS_SOD, 2);
5629
5630         if( j2k->cstr_info && j2k->cur_tp_num==0){
5631           j2k_add_tlmarker( j2k->curtileno, j2k->cstr_info, J2K_MS_SOD, cio_tell(cio), 0);
5632         }
5633
5634         if (j2k->curtileno == 0) {
5635                 j2k->sod_start = cio_tell(cio) + j2k->pos_correction;
5636         }
5637
5638         /* INDEX >> */
5639         cstr_info = j2k->cstr_info;
5640         if (cstr_info) {
5641                 if (!j2k->cur_tp_num ) {
5642                         cstr_info->tile[j2k->curtileno].end_header = cio_tell(cio) + j2k->pos_correction - 1;
5643                         j2k->cstr_info->tile[j2k->curtileno].tileno = j2k->curtileno;
5644                 }
5645                 else{
5646                         if(cstr_info->tile[j2k->curtileno].packet[cstr_info->packno - 1].end_pos < cio_tell(cio))
5647                                 cstr_info->tile[j2k->curtileno].packet[cstr_info->packno].start_pos = cio_tell(cio);
5648                 }
5649                 /* UniPG>> */
5650 #ifdef USE_JPWL
5651                 /* update markers struct */
5652                 j2k_add_marker(j2k->cstr_info, J2K_MS_SOD, j2k->sod_start, 2);
5653 #endif /* USE_JPWL */
5654                 /* <<UniPG */
5655         }
5656         /* << INDEX */
5657         
5658         tcp = &cp->tcps[j2k->curtileno];
5659         for (layno = 0; layno < tcp->numlayers; layno++) {
5660                 if (tcp->rates[layno]>(j2k->sod_start / (cp->th * cp->tw))) {
5661                         tcp->rates[layno]-=(j2k->sod_start / (cp->th * cp->tw));
5662                 } else if (tcp->rates[layno]) {
5663                         tcp->rates[layno]=1;
5664                 }
5665         }
5666         if(j2k->cur_tp_num == 0){
5667                 tcd->tcd_image->tiles->packno = 0;
5668                 if(cstr_info)
5669                         cstr_info->packno = 0;
5670         }
5671         
5672         l = tcd_encode_tile(tcd, j2k->curtileno, cio_getbp(cio), cio_numbytesleft(cio) - 2, cstr_info);
5673         
5674         /* Writing Psot in SOT marker */
5675         totlen = cio_tell(cio) + l - j2k->sot_start;
5676         cio_seek(cio, j2k->sot_start + 6);
5677         cio_write(cio, totlen, 4);
5678         cio_seek(cio, j2k->sot_start + totlen);
5679         /* Writing Ttlm and Ptlm in TLM marker */
5680         if(cp->cinema){
5681                 cio_seek(cio, j2k->tlm_start + 6 + (5*j2k->cur_tp_num));
5682                 cio_write(cio, j2k->curtileno, 1);
5683                 cio_write(cio, totlen, 4);
5684         }
5685         cio_seek(cio, j2k->sot_start + totlen);
5686 }
5687
5688 /**
5689  * Writes the SOD marker (Start of data)
5690  *
5691  * @param       p_stream                                the stream to write data to.
5692  * @param       p_j2k                           J2K codec.
5693  * @param       p_manager               the user event manager.
5694 */
5695 opj_bool j2k_write_sod_v2(      opj_j2k_v2_t *p_j2k,
5696                                                         struct opj_tcd_v2 * p_tile_coder,
5697                                                         OPJ_BYTE * p_data,
5698                                                         OPJ_UINT32 * p_data_written,
5699                                                         OPJ_UINT32 p_total_data_size,
5700                                                         const struct opj_stream_private *p_stream,
5701                                                         struct opj_event_mgr * p_manager )
5702 {
5703         opj_tcp_v2_t *l_tcp = 00;
5704         opj_codestream_info_t *l_cstr_info = 00;
5705         opj_cp_v2_t *l_cp = 00;
5706
5707         OPJ_UINT32 l_size_tile;
5708         OPJ_UINT32 l_remaining_data;
5709
5710         /* preconditions */
5711         assert(p_j2k != 00);
5712         assert(p_manager != 00);
5713         assert(p_stream != 00);
5714
5715         opj_write_bytes(p_data,J2K_MS_SOD,2);                                   /* SOD */
5716         p_data += 2;
5717
5718         /* make room for the EOF marker */
5719         l_remaining_data =  p_total_data_size - 4;
5720
5721         l_cp = &(p_j2k->m_cp);
5722         l_tcp = &l_cp->tcps[p_j2k->m_current_tile_number];
5723
5724
5725         /* update tile coder */
5726         p_tile_coder->tp_num = p_j2k->m_specific_param.m_encoder.m_current_poc_tile_part_number ;
5727         p_tile_coder->cur_tp_num = p_j2k->m_specific_param.m_encoder.m_current_tile_part_number;
5728
5729         l_size_tile = l_cp->th * l_cp->tw;
5730
5731         /* INDEX >> */
5732         /* TODO mergeV2: check this part which use cstr_info */
5733         /*l_cstr_info = p_j2k->cstr_info;
5734         if (l_cstr_info) {
5735                 if (!p_j2k->m_specific_param.m_encoder.m_current_tile_part_number ) {
5736                         //TODO cstr_info->tile[p_j2k->m_current_tile_number].end_header = p_stream_tell(p_stream) + p_j2k->pos_correction - 1;
5737                         l_cstr_info->tile[p_j2k->m_current_tile_number].tileno = p_j2k->m_current_tile_number;
5738                 }
5739                 else {*/
5740                         /*
5741                         TODO
5742                         if
5743                                 (cstr_info->tile[p_j2k->m_current_tile_number].packet[cstr_info->packno - 1].end_pos < p_stream_tell(p_stream))
5744                         {
5745                                 cstr_info->tile[p_j2k->m_current_tile_number].packet[cstr_info->packno].start_pos = p_stream_tell(p_stream);
5746                         }*/
5747                 /*}*/
5748                 /* UniPG>> */
5749 #ifdef USE_JPWL
5750                 /* update markers struct */
5751                 /*j2k_add_marker(p_j2k->cstr_info, J2K_MS_SOD, p_j2k->sod_start, 2);
5752 */
5753   assert( 0 && "TODO" );
5754 #endif /* USE_JPWL */
5755                 /* <<UniPG */
5756         /*}*/
5757         /* << INDEX */
5758
5759         if (p_j2k->m_specific_param.m_encoder.m_current_tile_part_number == 0) {
5760                 p_tile_coder->tcd_image->tiles->packno = 0;
5761                 if (l_cstr_info) {
5762                         l_cstr_info->packno = 0;
5763                 }
5764         }
5765
5766         *p_data_written = 0;
5767
5768         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)) {
5769                 opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot encode tile\n");
5770                 return OPJ_FALSE;
5771         }
5772
5773         *p_data_written += 2;
5774
5775         return OPJ_TRUE;
5776 }
5777
5778
5779 static void j2k_read_sod(opj_j2k_t *j2k) {
5780         int len, truncate = 0, i;
5781         unsigned char *data = NULL, *data_ptr = NULL;
5782
5783         opj_cio_t *cio = j2k->cio;
5784         int curtileno = j2k->curtileno;
5785
5786         /* Index */
5787         if (j2k->cstr_info) {
5788                 j2k->cstr_info->tile[j2k->curtileno].tp[j2k->cur_tp_num].tp_end_header =
5789                         cio_tell(cio) + j2k->pos_correction - 1;
5790                 if (j2k->cur_tp_num == 0)
5791                         j2k->cstr_info->tile[j2k->curtileno].end_header = cio_tell(cio) + j2k->pos_correction - 1;
5792                 j2k->cstr_info->packno = 0;
5793         }
5794         
5795         len = int_min(j2k->eot - cio_getbp(cio), cio_numbytesleft(cio) + 1);
5796
5797         if (len == cio_numbytesleft(cio) + 1) {
5798                 truncate = 1;           /* Case of a truncate codestream */
5799         }       
5800
5801         data = j2k->tile_data[curtileno];
5802         data = (unsigned char*) opj_realloc(data, (j2k->tile_len[curtileno] + len) * sizeof(unsigned char));
5803
5804         data_ptr = data + j2k->tile_len[curtileno];
5805         for (i = 0; i < len; i++) {
5806                 data_ptr[i] = cio_read(cio, 1);
5807         }
5808
5809         j2k->tile_len[curtileno] += len;
5810         j2k->tile_data[curtileno] = data;
5811         
5812         if (!truncate) {
5813                 j2k->state = J2K_STATE_TPHSOT;
5814         } else {
5815                 j2k->state = J2K_STATE_NEOC;    /* RAJOUTE !! */
5816         }
5817         j2k->cur_tp_num++;
5818 }
5819
5820 /**
5821  * Reads a SOD marker (Start Of Data)
5822  *
5823  * @param       p_header_data   the data contained in the SOD box.
5824  * @param       p_j2k                   the jpeg2000 codec.
5825  * @param       p_header_size   the size of the data contained in the SOD marker.
5826  * @param       p_manager               the user event manager.
5827 */
5828 opj_bool j2k_read_sod_v2 (
5829                                                 opj_j2k_v2_t *p_j2k,
5830                                                 struct opj_stream_private *p_stream,
5831                                                 struct opj_event_mgr * p_manager
5832                                         )
5833 {
5834         OPJ_UINT32 l_current_read_size;
5835         opj_codestream_index_t * l_cstr_index = 00;
5836         OPJ_BYTE ** l_current_data = 00;
5837         opj_tcp_v2_t * l_tcp = 00;
5838         OPJ_UINT32 * l_tile_len = 00;
5839
5840         /* preconditions */
5841         assert(p_j2k != 00);
5842         assert(p_manager != 00);
5843         assert(p_stream != 00);
5844
5845         l_tcp = &(p_j2k->m_cp.tcps[p_j2k->m_current_tile_number]);
5846
5847         if (p_j2k->m_specific_param.m_decoder.m_last_tile_part) {
5848                 /* opj_stream_get_number_byte_left returns OPJ_OFF_T
5849                 // but we are in the last tile part,
5850                 // so its result will fit on OPJ_UINT32 unless we find
5851                 // a file with a single tile part of more than 4 GB...*/
5852                 p_j2k->m_specific_param.m_decoder.m_sot_length = (OPJ_UINT32)(opj_stream_get_number_byte_left(p_stream) - 2);
5853         }
5854         else
5855                 p_j2k->m_specific_param.m_decoder.m_sot_length -= 2;
5856
5857         l_current_data = &(l_tcp->m_data);
5858         l_tile_len = &l_tcp->m_data_size;
5859
5860         if (! *l_current_data) {
5861                 *l_current_data = (OPJ_BYTE*) opj_malloc(p_j2k->m_specific_param.m_decoder.m_sot_length);
5862         }
5863         else {
5864                 *l_current_data = (OPJ_BYTE*) opj_realloc(*l_current_data, *l_tile_len + p_j2k->m_specific_param.m_decoder.m_sot_length);
5865         }
5866
5867         if (*l_current_data == 00) {
5868                 opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot decode tile\n");
5869                 return OPJ_FALSE;
5870         }
5871
5872
5873         /* Index */
5874         l_cstr_index = p_j2k->cstr_index;
5875         if (l_cstr_index) {
5876                 OPJ_OFF_T l_current_pos = opj_stream_tell(p_stream) - 2;
5877
5878                 OPJ_UINT32 l_current_tile_part = l_cstr_index->tile_index[p_j2k->m_current_tile_number].current_tpsno;
5879                 l_cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index[l_current_tile_part].end_header =
5880                                 l_current_pos;
5881                 l_cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index[l_current_tile_part].end_pos =
5882                                 l_current_pos + p_j2k->m_specific_param.m_decoder.m_sot_length + 2;
5883
5884                 j2k_add_tlmarker_v2(p_j2k->m_current_tile_number,
5885                                                         l_cstr_index,
5886                                                         J2K_MS_SOD,
5887                                                         l_current_pos,
5888                                                         p_j2k->m_specific_param.m_decoder.m_sot_length + 2);
5889
5890                 /*l_cstr_index->packno = 0;*/
5891         }
5892
5893         l_current_read_size = opj_stream_read_data(     p_stream,
5894                                                                                                 *l_current_data + *l_tile_len,
5895                                                                                                 p_j2k->m_specific_param.m_decoder.m_sot_length,
5896                                                                                                 p_manager);
5897
5898         if (l_current_read_size != p_j2k->m_specific_param.m_decoder.m_sot_length) {
5899                 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_NEOC;
5900         }
5901         else {
5902                 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_TPHSOT;
5903         }
5904
5905         *l_tile_len +=  l_current_read_size;
5906
5907         return OPJ_TRUE;
5908 }
5909
5910
5911 static void j2k_write_rgn(opj_j2k_t *j2k, int compno, int tileno) {
5912         opj_cp_t *cp = j2k->cp;
5913         opj_tcp_t *tcp = &cp->tcps[tileno];
5914         opj_cio_t *cio = j2k->cio;
5915         int numcomps = j2k->image->numcomps;
5916         
5917         cio_write(cio, J2K_MS_RGN, 2);                                          /* RGN  */
5918         cio_write(cio, numcomps <= 256 ? 5 : 6, 2);                     /* Lrgn */
5919         cio_write(cio, compno, numcomps <= 256 ? 1 : 2);        /* Crgn */
5920         cio_write(cio, 0, 1);                                                           /* Srgn */
5921         cio_write(cio, tcp->tccps[compno].roishift, 1);         /* SPrgn */
5922 }
5923
5924 /**
5925  * Writes the RGN marker (Region Of Interest)
5926  *
5927  * @param       p_tile_no               the tile to output
5928  * @param       p_comp_no               the component to output
5929  * @param       p_stream                                the stream to write data to.
5930  * @param       p_j2k                           J2K codec.
5931  * @param       p_manager               the user event manager.
5932 */
5933 opj_bool j2k_write_rgn_v2(      opj_j2k_v2_t *p_j2k,
5934                                                         OPJ_UINT32 p_tile_no,
5935                                                         OPJ_UINT32 p_comp_no,
5936                                                         struct opj_stream_private *p_stream,
5937                                                         struct opj_event_mgr * p_manager )
5938 {
5939         OPJ_BYTE * l_current_data = 00;
5940         OPJ_UINT32 l_nb_comp;
5941         OPJ_UINT32 l_rgn_size;
5942         opj_image_t *l_image = 00;
5943         opj_cp_v2_t *l_cp = 00;
5944         opj_tcp_v2_t *l_tcp = 00;
5945         opj_tccp_t *l_tccp = 00;
5946         OPJ_UINT32 l_comp_room;
5947
5948         /* preconditions */
5949         assert(p_j2k != 00);
5950         assert(p_manager != 00);
5951         assert(p_stream != 00);
5952
5953         l_cp = &(p_j2k->m_cp);
5954         l_tcp = &l_cp->tcps[p_tile_no];
5955         l_tccp = &l_tcp->tccps[p_comp_no];
5956
5957         l_nb_comp = l_image->numcomps;
5958
5959         if (l_nb_comp <= 256) {
5960                 l_comp_room = 1;
5961         }
5962         else {
5963                 l_comp_room = 2;
5964         }
5965
5966         l_rgn_size = 6 + l_comp_room;
5967
5968         l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
5969
5970         opj_write_bytes(l_current_data,J2K_MS_RGN,2);                                   /* RGN  */
5971         l_current_data += 2;
5972
5973         opj_write_bytes(l_current_data,l_rgn_size-2,2);                                 /* Lrgn */
5974         l_current_data += 2;
5975
5976         opj_write_bytes(l_current_data,p_comp_no,l_comp_room);                  /* Crgn */
5977         l_current_data+=l_comp_room;
5978
5979         opj_write_bytes(l_current_data, 0,1);                                                   /* Srgn */
5980         ++l_current_data;
5981
5982         opj_write_bytes(l_current_data, l_tccp->roishift,1);                    /* SPrgn */
5983         ++l_current_data;
5984
5985         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) {
5986                 return OPJ_FALSE;
5987         }
5988
5989         return OPJ_TRUE;
5990 }
5991
5992 static void j2k_read_rgn(opj_j2k_t *j2k) {
5993         int len, compno, roisty;
5994
5995         opj_cp_t *cp = j2k->cp;
5996         opj_tcp_t *tcp = j2k->state == J2K_STATE_TPH ? &cp->tcps[j2k->curtileno] : j2k->default_tcp;
5997         opj_cio_t *cio = j2k->cio;
5998         int numcomps = j2k->image->numcomps;
5999
6000         len = cio_read(cio, 2);                                                                         /* Lrgn */
6001         compno = cio_read(cio, numcomps <= 256 ? 1 : 2);                        /* Crgn */
6002         roisty = cio_read(cio, 1);                                                                      /* Srgn */
6003
6004 #ifdef USE_JPWL
6005         if (j2k->cp->correct) {
6006                 /* totlen is negative or larger than the bytes left!!! */
6007                 if (compno >= numcomps) {
6008                         opj_event_msg(j2k->cinfo, EVT_ERROR,
6009                                 "JPWL: bad component number in RGN (%d when there are only %d)\n",
6010                                 compno, numcomps);
6011                         if (!JPWL_ASSUME || JPWL_ASSUME) {
6012                                 opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
6013                                 return;
6014                         }
6015                 }
6016         };
6017 #endif /* USE_JPWL */
6018
6019         tcp->tccps[compno].roishift = cio_read(cio, 1);                         /* SPrgn */
6020 }
6021
6022 static void j2k_write_eoc(opj_j2k_t *j2k) {
6023         opj_cio_t *cio = j2k->cio;
6024         /* opj_event_msg(j2k->cinfo, "%.8x: EOC\n", cio_tell(cio) + j2k->pos_correction); */
6025         cio_write(cio, J2K_MS_EOC, 2);
6026
6027 /* UniPG>> */
6028 #ifdef USE_JPWL
6029         /* update markers struct */
6030         j2k_add_marker(j2k->cstr_info, J2K_MS_EOC, cio_tell(cio) - 2, 2);
6031 #endif /* USE_JPWL */
6032 /* <<UniPG */
6033 }
6034
6035 /**
6036  * Writes the EOC marker (End of Codestream)
6037  * 
6038  * @param       p_stream                the stream to write data to.
6039  * @param       p_j2k                   J2K codec.
6040  * @param       p_manager               the user event manager.
6041 */
6042 opj_bool j2k_write_eoc_v2(      opj_j2k_v2_t *p_j2k,
6043                                                         struct opj_stream_private *p_stream,
6044                                                         struct opj_event_mgr * p_manager )
6045 {
6046         /* preconditions */
6047         assert(p_j2k != 00);
6048         assert(p_manager != 00);
6049         assert(p_stream != 00);
6050         
6051         opj_write_bytes(p_j2k->m_specific_param.m_encoder.m_header_tile_data,J2K_MS_EOC,2);                                     /* EOC */
6052         
6053
6054 /* UniPG>> */
6055 #ifdef USE_JPWL
6056         /* update markers struct */
6057         /*
6058         j2k_add_marker(p_j2k->cstr_info, J2K_MS_EOC, p_stream_tell(p_stream) - 2, 2);
6059 */
6060 #endif /* USE_JPWL */
6061
6062         if ( opj_stream_write_data(p_stream,p_j2k->m_specific_param.m_encoder.m_header_tile_data,2,p_manager) != 2) {
6063                 return OPJ_FALSE;
6064         }
6065
6066         if ( ! opj_stream_flush(p_stream,p_manager) ) {
6067                 return OPJ_FALSE;
6068         }
6069
6070         return OPJ_TRUE;
6071 }
6072
6073
6074 /**
6075  * Reads a RGN marker (Region Of Interest)
6076  *
6077  * @param       p_header_data   the data contained in the POC box.
6078  * @param       p_j2k                   the jpeg2000 codec.
6079  * @param       p_header_size   the size of the data contained in the POC marker.
6080  * @param       p_manager               the user event manager.
6081 */
6082 opj_bool j2k_read_rgn_v2 (
6083                                                 opj_j2k_v2_t *p_j2k,
6084                                                 OPJ_BYTE * p_header_data,
6085                                                 OPJ_UINT32 p_header_size,
6086                                                 struct opj_event_mgr * p_manager
6087                                         )
6088 {
6089         OPJ_UINT32 l_nb_comp;
6090         opj_image_t * l_image = 00;
6091
6092         opj_cp_v2_t *l_cp = 00;
6093         opj_tcp_v2_t *l_tcp = 00;
6094         OPJ_UINT32 l_comp_room, l_comp_no, l_roi_sty;
6095
6096         /* preconditions*/
6097         assert(p_header_data != 00);
6098         assert(p_j2k != 00);
6099         assert(p_manager != 00);
6100
6101         l_image = p_j2k->m_private_image;
6102         l_nb_comp = l_image->numcomps;
6103
6104         if (l_nb_comp <= 256) {
6105                 l_comp_room = 1; }
6106         else {
6107                 l_comp_room = 2; }
6108
6109         if (p_header_size != 2 + l_comp_room) {
6110                 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading RGN marker\n");
6111                 return OPJ_FALSE;
6112         }
6113
6114         l_cp = &(p_j2k->m_cp);
6115         l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ?
6116                                 &l_cp->tcps[p_j2k->m_current_tile_number] :
6117                                 p_j2k->m_specific_param.m_decoder.m_default_tcp;
6118
6119         opj_read_bytes(p_header_data,&l_comp_no,l_comp_room);           /* Crgn */
6120         p_header_data+=l_comp_room;
6121         opj_read_bytes(p_header_data,&l_roi_sty,1);                                     /* Srgn */
6122         ++p_header_data;
6123
6124 #ifdef USE_JPWL
6125         if (l_cp->correct) {
6126                 /* totlen is negative or larger than the bytes left!!! */
6127                 if (l_comp_room >= l_nb_comp) {
6128                         opj_event_msg_v2(p_manager, EVT_ERROR,
6129                                 "JPWL: bad component number in RGN (%d when there are only %d)\n",
6130                                 l_comp_room, l_nb_comp);
6131                         if (!JPWL_ASSUME || JPWL_ASSUME) {
6132                                 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
6133                                 return OPJ_FALSE;
6134                         }
6135                 }
6136         };
6137 #endif /* USE_JPWL */
6138
6139         opj_read_bytes(p_header_data,(OPJ_UINT32 *) (&(l_tcp->tccps[l_comp_no].roishift)),1);   /* SPrgn */
6140         ++p_header_data;
6141
6142         return OPJ_TRUE;
6143
6144 }
6145
6146 static OPJ_FLOAT32 get_tp_stride (opj_tcp_v2_t * p_tcp)
6147 {
6148         return (OPJ_FLOAT32) ((p_tcp->m_nb_tile_parts - 1) * 14);
6149 }
6150
6151 static OPJ_FLOAT32 get_default_stride (opj_tcp_v2_t * p_tcp)
6152 {
6153   (void)p_tcp;
6154         return 0;
6155 }
6156
6157 /**
6158  * Updates the rates of the tcp.
6159  *
6160  * @param       p_stream                the stream to write data to.
6161  * @param       p_j2k                   J2K codec.
6162  * @param       p_manager               the user event manager.
6163 */
6164 opj_bool j2k_update_rates(      opj_j2k_v2_t *p_j2k,
6165                                                         struct opj_stream_private *p_stream,
6166                                                         struct opj_event_mgr * p_manager )
6167 {
6168         opj_cp_v2_t * l_cp = 00;
6169         opj_image_t * l_image = 00;
6170         opj_tcp_v2_t * l_tcp = 00;
6171         opj_image_comp_t * l_img_comp = 00;
6172
6173         OPJ_UINT32 i,j,k;
6174         OPJ_INT32 l_x0,l_y0,l_x1,l_y1;
6175         OPJ_FLOAT32 * l_rates = 0;
6176         OPJ_FLOAT32 l_sot_remove;
6177         OPJ_UINT32 l_bits_empty, l_size_pixel;
6178         OPJ_UINT32 l_tile_size = 0;
6179         OPJ_UINT32 l_last_res;
6180         OPJ_FLOAT32 (* l_tp_stride_func)(opj_tcp_v2_t *) = 00;
6181
6182         /* preconditions */
6183         assert(p_j2k != 00);
6184         assert(p_manager != 00);
6185         assert(p_stream != 00);
6186
6187
6188         l_cp = &(p_j2k->m_cp);
6189         l_image = p_j2k->m_private_image;
6190         l_tcp = l_cp->tcps;
6191
6192         l_bits_empty = 8 * l_image->comps->dx * l_image->comps->dy;
6193         l_size_pixel = l_image->numcomps * l_image->comps->prec;
6194         l_sot_remove = ((OPJ_FLOAT32) opj_stream_tell(p_stream)) / (l_cp->th * l_cp->tw);
6195
6196         if (l_cp->m_specific_param.m_enc.m_tp_on) {
6197                 l_tp_stride_func = get_tp_stride;
6198         }
6199         else {
6200                 l_tp_stride_func = get_default_stride;
6201         }
6202
6203         for (i=0;i<l_cp->th;++i) {
6204                 for (j=0;j<l_cp->tw;++j) {
6205                         OPJ_FLOAT32 l_offset = ((*l_tp_stride_func)(l_tcp)) / l_tcp->numlayers;
6206
6207                         /* 4 borders of the tile rescale on the image if necessary */
6208                         l_x0 = int_max(l_cp->tx0 + j * l_cp->tdx, l_image->x0);
6209                         l_y0 = int_max(l_cp->ty0 + i * l_cp->tdy, l_image->y0);
6210                         l_x1 = int_min(l_cp->tx0 + (j + 1) * l_cp->tdx, l_image->x1);
6211                         l_y1 = int_min(l_cp->ty0 + (i + 1) * l_cp->tdy, l_image->y1);
6212
6213                         l_rates = l_tcp->rates;
6214
6215                         /* Modification of the RATE >> */
6216                         if (*l_rates) {
6217                                 *l_rates =              (( (float) (l_size_pixel * (l_x1 - l_x0) * (l_y1 - l_y0)))
6218                                                                 /
6219                                                                 ((*l_rates) * l_bits_empty)
6220                                                                 )
6221                                                                 -
6222                                                                 l_offset;
6223                         }
6224
6225                         ++l_rates;
6226
6227                         for (k = 1; k < l_tcp->numlayers; ++k) {
6228                                 if (*l_rates) {
6229                                         *l_rates =              (( (OPJ_FLOAT32) (l_size_pixel * (l_x1 - l_x0) * (l_y1 - l_y0)))
6230                                                                         /
6231                                                                                 ((*l_rates) * l_bits_empty)
6232                                                                         )
6233                                                                         -
6234                                                                         l_offset;
6235                                 }
6236
6237                                 ++l_rates;
6238                         }
6239
6240                         ++l_tcp;
6241
6242                 }
6243         }
6244
6245         l_tcp = l_cp->tcps;
6246
6247         for (i=0;i<l_cp->th;++i) {
6248                 for     (j=0;j<l_cp->tw;++j) {
6249                         l_rates = l_tcp->rates;
6250
6251                         if (*l_rates) {
6252                                 *l_rates -= l_sot_remove;
6253
6254                                 if (*l_rates < 30) {
6255                                         *l_rates = 30;
6256                                 }
6257                         }
6258
6259                         ++l_rates;
6260
6261                         l_last_res = l_tcp->numlayers - 1;
6262
6263                         for (k = 1; k < l_last_res; ++k) {
6264
6265                                 if (*l_rates) {
6266                                         *l_rates -= l_sot_remove;
6267
6268                                         if (*l_rates < *(l_rates - 1) + 10) {
6269                                                 *l_rates  = (*(l_rates - 1)) + 20;
6270                                         }
6271                                 }
6272
6273                                 ++l_rates;
6274                         }
6275
6276                         if (*l_rates) {
6277                                 *l_rates -= (l_sot_remove + 2.f);
6278
6279                                 if (*l_rates < *(l_rates - 1) + 10) {
6280                                         *l_rates  = (*(l_rates - 1)) + 20;
6281                                 }
6282                         }
6283
6284                         ++l_tcp;
6285                 }
6286         }
6287
6288         l_img_comp = l_image->comps;
6289         l_tile_size = 0;
6290
6291         for (i=0;i<l_image->numcomps;++i) {
6292                 l_tile_size += (        uint_ceildiv(l_cp->tdx,l_img_comp->dx)
6293                                                         *
6294                                                         uint_ceildiv(l_cp->tdy,l_img_comp->dy)
6295                                                         *
6296                                                         l_img_comp->prec
6297                                                 );
6298
6299                 ++l_img_comp;
6300         }
6301
6302         l_tile_size = (OPJ_UINT32) (l_tile_size * 0.1625); /* 1.3/8 = 0.1625 */
6303
6304         l_tile_size += j2k_get_specific_header_sizes(p_j2k);
6305
6306         p_j2k->m_specific_param.m_encoder.m_encoded_tile_size = l_tile_size;
6307         p_j2k->m_specific_param.m_encoder.m_encoded_tile_data =
6308                         (OPJ_BYTE *) opj_malloc(p_j2k->m_specific_param.m_encoder.m_encoded_tile_size);
6309         if (p_j2k->m_specific_param.m_encoder.m_encoded_tile_data == 00) {
6310                 return OPJ_FALSE;
6311         }
6312
6313         if (l_cp->m_specific_param.m_enc.m_cinema) {
6314                 p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer =
6315                                 (OPJ_BYTE *) opj_malloc(5*p_j2k->m_specific_param.m_encoder.m_total_tile_parts);
6316                 if (! p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer) {
6317                         return OPJ_FALSE;
6318                 }
6319
6320                 p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current =
6321                                 p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer;
6322         }
6323
6324         return OPJ_TRUE;
6325 }
6326
6327 static void j2k_read_eoc(opj_j2k_t *j2k) {
6328         int i, tileno;
6329         opj_bool success = OPJ_FALSE;
6330
6331         /* if packets should be decoded */
6332         if (j2k->cp->limit_decoding != DECODE_ALL_BUT_PACKETS) {
6333                 opj_tcd_t *tcd = tcd_create(j2k->cinfo);
6334                 tcd_malloc_decode(tcd, j2k->image, j2k->cp);
6335                 for (i = 0; i < j2k->cp->tileno_size; i++) {
6336                         tcd_malloc_decode_tile(tcd, j2k->image, j2k->cp, i, j2k->cstr_info);
6337                         if (j2k->cp->tileno[i] != -1)
6338                         {
6339                                 tileno = j2k->cp->tileno[i];
6340                                 success = tcd_decode_tile(tcd, j2k->tile_data[tileno], j2k->tile_len[tileno], tileno, j2k->cstr_info);
6341                                 opj_free(j2k->tile_data[tileno]);
6342                                 j2k->tile_data[tileno] = NULL;
6343                                 tcd_free_decode_tile(tcd, i);
6344                         }
6345                         else
6346                                 success = OPJ_FALSE;
6347                         if (success == OPJ_FALSE) {
6348                                 j2k->state |= J2K_STATE_ERR;
6349                                 break;
6350                         }
6351                 }
6352                 tcd_free_decode(tcd);
6353                 tcd_destroy(tcd);
6354         }
6355         /* if packets should not be decoded  */
6356         else {
6357                 for (i = 0; i < j2k->cp->tileno_size; i++) {
6358                         tileno = j2k->cp->tileno[i];
6359                         opj_free(j2k->tile_data[tileno]);
6360                         j2k->tile_data[tileno] = NULL;
6361                 }
6362         }       
6363         if (j2k->state & J2K_STATE_ERR)
6364                 j2k->state = J2K_STATE_MT + J2K_STATE_ERR;
6365         else
6366                 j2k->state = J2K_STATE_MT; 
6367 }
6368
6369 /**
6370  * Reads a EOC marker (End Of Codestream)
6371  *
6372  * @param       p_header_data   the data contained in the SOD box.
6373  * @param       p_j2k                   the jpeg2000 codec.
6374  * @param       p_header_size   the size of the data contained in the SOD marker.
6375  * @param       p_manager               the user event manager.
6376 */
6377 #if 0
6378 opj_bool j2k_read_eoc_v2 (      opj_j2k_v2_t *p_j2k,
6379                                                         struct opj_stream_private *p_stream,
6380                                                         struct opj_event_mgr * p_manager )
6381 {
6382         OPJ_UINT32 i;
6383         opj_tcd_v2_t * l_tcd = 00;
6384         OPJ_UINT32 l_nb_tiles;
6385         opj_tcp_v2_t * l_tcp = 00;
6386         opj_bool l_success;
6387
6388         /* preconditions */
6389         assert(p_j2k != 00);
6390         assert(p_manager != 00);
6391         assert(p_stream != 00);
6392
6393         l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw;
6394         l_tcp = p_j2k->m_cp.tcps;
6395
6396         l_tcd = tcd_create_v2(OPJ_TRUE);
6397         if (l_tcd == 00) {
6398                 opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot decode tile, memory error\n");
6399                 return OPJ_FALSE;
6400         }
6401
6402         for (i = 0; i < l_nb_tiles; ++i) {
6403                 if (l_tcp->m_data) {
6404                         if (! tcd_init_decode_tile(l_tcd, i)) {
6405                                 tcd_destroy_v2(l_tcd);
6406                                 opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot decode tile, memory error\n");
6407                                 return OPJ_FALSE;
6408                         }
6409
6410                         l_success = tcd_decode_tile_v2(l_tcd, l_tcp->m_data, l_tcp->m_data_size, i, p_j2k->cstr_index);
6411                         /* cleanup */
6412
6413                         if (! l_success) {
6414                                 p_j2k->m_specific_param.m_decoder.m_state |= J2K_STATE_ERR;
6415                                 break;
6416                         }
6417                 }
6418
6419                 j2k_tcp_destroy(l_tcp);
6420                 ++l_tcp;
6421         }
6422
6423         tcd_destroy_v2(l_tcd);
6424         return OPJ_TRUE;
6425 }
6426 #endif
6427
6428 /**
6429  * Gets the offset of the header.
6430  *
6431  * @param       p_stream                                the stream to write data to.
6432  * @param       p_j2k                           J2K codec.
6433  * @param       p_manager               the user event manager.
6434 */
6435 opj_bool j2k_get_end_header(opj_j2k_v2_t *p_j2k,
6436                                                         struct opj_stream_private *p_stream,
6437                                                         struct opj_event_mgr * p_manager )
6438 {
6439         /* preconditions */
6440         assert(p_j2k != 00);
6441         assert(p_manager != 00);
6442         assert(p_stream != 00);
6443
6444         p_j2k->cstr_index->main_head_end = opj_stream_tell(p_stream);
6445
6446         return OPJ_TRUE;
6447 }
6448
6449 /**
6450  * Writes the MCT marker (Multiple Component Transform)
6451  *
6452  * @param       p_stream                                the stream to write data to.
6453  * @param       p_j2k                           J2K codec.
6454  * @param       p_manager               the user event manager.
6455 */
6456 opj_bool j2k_write_mct_data_group(      opj_j2k_v2_t *p_j2k,
6457                                                                         struct opj_stream_private *p_stream,
6458                                                                         struct opj_event_mgr * p_manager )
6459 {
6460         OPJ_UINT32 i;
6461         opj_simple_mcc_decorrelation_data_t * l_mcc_record;
6462         opj_mct_data_t * l_mct_record;
6463         opj_tcp_v2_t * l_tcp;
6464
6465         /* preconditions */
6466         assert(p_j2k != 00);
6467         assert(p_stream != 00);
6468         assert(p_manager != 00);
6469
6470         if (! j2k_write_cbd(p_j2k,p_stream,p_manager)) {
6471                 return OPJ_FALSE;
6472         }
6473
6474         l_tcp = &(p_j2k->m_cp.tcps[p_j2k->m_current_tile_number]);
6475         l_mct_record = l_tcp->m_mct_records;
6476
6477         for (i=0;i<l_tcp->m_nb_mct_records;++i) {
6478
6479                 if (! j2k_write_mct_record(p_j2k,l_mct_record,p_stream,p_manager)) {
6480                         return OPJ_FALSE;
6481                 }
6482
6483                 ++l_mct_record;
6484         }
6485
6486         l_mcc_record = l_tcp->m_mcc_records;
6487
6488         for     (i=0;i<l_tcp->m_nb_mcc_records;++i) {
6489
6490                 if (! j2k_write_mcc_record(p_j2k,l_mcc_record,p_stream,p_manager)) {
6491                         return OPJ_FALSE;
6492                 }
6493
6494                 ++l_mcc_record;
6495         }
6496
6497         if (! j2k_write_mco(p_j2k,p_stream,p_manager)) {
6498                 return OPJ_FALSE;
6499         }
6500
6501         return OPJ_TRUE;
6502 }
6503
6504 /**
6505  * Writes the image components.
6506  *
6507  * @param       p_stream                                the stream to write data to.
6508  * @param       p_j2k                           J2K codec.
6509  * @param       p_manager               the user event manager.
6510 */
6511 opj_bool j2k_write_image_components(opj_j2k_v2_t *p_j2k,
6512                                                                         struct opj_stream_private *p_stream,
6513                                                                         struct opj_event_mgr * p_manager )
6514 {
6515         OPJ_UINT32 compno;
6516
6517         /* preconditions */
6518         assert(p_j2k != 00);
6519         assert(p_manager != 00);
6520         assert(p_stream != 00);
6521
6522         for (compno = 1; compno < p_j2k->m_private_image->numcomps; ++compno)
6523         {
6524                 if (! j2k_write_coc_v2(p_j2k,compno,p_stream, p_manager)) {
6525                         return OPJ_FALSE;
6526                 }
6527
6528                 if (! j2k_write_qcc_v2(p_j2k,compno,p_stream, p_manager)) {
6529                         return OPJ_FALSE;
6530                 }
6531         }
6532
6533         return OPJ_TRUE;
6534 }
6535
6536 /**
6537  * Writes regions of interests.
6538  *
6539  * @param       p_stream                                the stream to write data to.
6540  * @param       p_j2k                           J2K codec.
6541  * @param       p_manager               the user event manager.
6542 */
6543 opj_bool j2k_write_regions(     opj_j2k_v2_t *p_j2k,
6544                                                         struct opj_stream_private *p_stream,
6545                                                         struct opj_event_mgr * p_manager )
6546 {
6547         OPJ_UINT32 compno;
6548         const opj_tccp_t *l_tccp = 00;
6549
6550         /* preconditions */
6551         assert(p_j2k != 00);
6552         assert(p_manager != 00);
6553         assert(p_stream != 00);
6554
6555         l_tccp = p_j2k->m_cp.tcps->tccps;
6556
6557         for     (compno = 0; compno < p_j2k->m_private_image->numcomps; ++compno)  {
6558                 if (l_tccp->roishift) {
6559
6560                         if (! j2k_write_rgn_v2(p_j2k,0,compno,p_stream,p_manager)) {
6561                                 return OPJ_FALSE;
6562                         }
6563                 }
6564
6565                 ++l_tccp;
6566         }
6567
6568         return OPJ_TRUE;
6569 }
6570
6571 /**
6572  * Writes EPC ????
6573  *
6574  * @param       p_stream                the stream to write data to.
6575  * @param       p_j2k                   J2K codec.
6576  * @param       p_manager               the user event manager.
6577 */
6578 opj_bool j2k_write_epc( opj_j2k_v2_t *p_j2k,
6579                                                 struct opj_stream_private *p_stream,
6580                                                 struct opj_event_mgr * p_manager )
6581 {
6582         opj_codestream_index_t * l_cstr_index = 00;
6583
6584         /* preconditions */
6585         assert(p_j2k != 00);
6586         assert(p_manager != 00);
6587         assert(p_stream != 00);
6588
6589         l_cstr_index = p_j2k->cstr_index;
6590         if (l_cstr_index) {
6591                 l_cstr_index->codestream_size = opj_stream_tell(p_stream);
6592                 /* UniPG>> */
6593                 /* The following adjustment is done to adjust the codestream size */
6594                 /* if SOD is not at 0 in the buffer. Useful in case of JP2, where */
6595                 /* the first bunch of bytes is not in the codestream              */
6596                 l_cstr_index->codestream_size -= l_cstr_index->main_head_start;
6597                 /* <<UniPG */
6598         }
6599
6600 #ifdef USE_JPWL
6601         /* preparation of JPWL marker segments */
6602 #if 0
6603         if(cp->epc_on) {
6604
6605                 /* encode according to JPWL */
6606                 jpwl_encode(p_j2k, p_stream, image);
6607
6608         }
6609 #endif
6610   assert( 0 && "TODO" );
6611 #endif /* USE_JPWL */
6612
6613         return OPJ_TRUE;
6614 }
6615
6616 typedef struct opj_dec_mstabent {
6617         /** marker value */
6618         int id;
6619         /** value of the state when the marker can appear */
6620         int states;
6621         /** action linked to the marker */
6622         void (*handler) (opj_j2k_t *j2k);
6623 } opj_dec_mstabent_t;
6624
6625 opj_dec_mstabent_t j2k_dec_mstab[] = {
6626   {J2K_MS_SOC, J2K_STATE_MHSOC, j2k_read_soc},
6627   {J2K_MS_SOT, J2K_STATE_MH | J2K_STATE_TPHSOT, j2k_read_sot},
6628   {J2K_MS_SOD, J2K_STATE_TPH, j2k_read_sod},
6629   {J2K_MS_EOC, J2K_STATE_TPHSOT, j2k_read_eoc},
6630   {J2K_MS_SIZ, J2K_STATE_MHSIZ, j2k_read_siz},
6631   {J2K_MS_COD, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_cod},
6632   {J2K_MS_COC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_coc},
6633   {J2K_MS_RGN, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_rgn},
6634   {J2K_MS_QCD, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_qcd},
6635   {J2K_MS_QCC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_qcc},
6636   {J2K_MS_POC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_poc},
6637   {J2K_MS_TLM, J2K_STATE_MH, j2k_read_tlm},
6638   {J2K_MS_PLM, J2K_STATE_MH, j2k_read_plm},
6639   {J2K_MS_PLT, J2K_STATE_TPH, j2k_read_plt},
6640   {J2K_MS_PPM, J2K_STATE_MH, j2k_read_ppm},
6641   {J2K_MS_PPT, J2K_STATE_TPH, j2k_read_ppt},
6642   {J2K_MS_SOP, 0, 0},
6643   {J2K_MS_CRG, J2K_STATE_MH, j2k_read_crg},
6644   {J2K_MS_COM, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_com},
6645
6646 #ifdef USE_JPWL
6647   {J2K_MS_EPC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_epc},
6648   {J2K_MS_EPB, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_epb},
6649   {J2K_MS_ESD, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_esd},
6650   {J2K_MS_RED, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_red},
6651 #endif /* USE_JPWL */
6652 #ifdef USE_JPSEC
6653   {J2K_MS_SEC, J2K_STATE_MH, j2k_read_sec},
6654   {J2K_MS_INSEC, 0, j2k_read_insec},
6655 #endif /* USE_JPSEC */
6656
6657   {0, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_unk}
6658 };
6659
6660 static void j2k_read_unk(opj_j2k_t *j2k) {
6661         opj_event_msg(j2k->cinfo, EVT_WARNING, "Unknown marker\n");
6662
6663 #ifdef USE_JPWL
6664         if (j2k->cp->correct) {
6665                 int m = 0, id, i;
6666                 int min_id = 0, min_dist = 17, cur_dist = 0, tmp_id;
6667                 cio_seek(j2k->cio, cio_tell(j2k->cio) - 2);
6668                 id = cio_read(j2k->cio, 2);
6669                 opj_event_msg(j2k->cinfo, EVT_ERROR,
6670                         "JPWL: really don't know this marker %x\n",
6671                         id);
6672                 if (!JPWL_ASSUME) {
6673                         opj_event_msg(j2k->cinfo, EVT_ERROR,
6674                                 "- possible synch loss due to uncorrectable codestream errors => giving up\n");
6675                         return;
6676                 }
6677                 /* OK, activate this at your own risk!!! */
6678                 /* we look for the marker at the minimum hamming distance from this */
6679                 while (j2k_dec_mstab[m].id) {
6680                         
6681                         /* 1's where they differ */
6682                         tmp_id = j2k_dec_mstab[m].id ^ id;
6683
6684                         /* compute the hamming distance between our id and the current */
6685                         cur_dist = 0;
6686                         for (i = 0; i < 16; i++) {
6687                                 if ((tmp_id >> i) & 0x0001) {
6688                                         cur_dist++;
6689                                 }
6690                         }
6691
6692                         /* if current distance is smaller, set the minimum */
6693                         if (cur_dist < min_dist) {
6694                                 min_dist = cur_dist;
6695                                 min_id = j2k_dec_mstab[m].id;
6696                         }
6697                         
6698                         /* jump to the next marker */
6699                         m++;
6700                 }
6701
6702                 /* do we substitute the marker? */
6703                 if (min_dist < JPWL_MAXIMUM_HAMMING) {
6704                         opj_event_msg(j2k->cinfo, EVT_ERROR,
6705                                 "- marker %x is at distance %d from the read %x\n",
6706                                 min_id, min_dist, id);
6707                         opj_event_msg(j2k->cinfo, EVT_ERROR,
6708                                 "- trying to substitute in place and crossing fingers!\n");
6709                         cio_seek(j2k->cio, cio_tell(j2k->cio) - 2);
6710                         cio_write(j2k->cio, min_id, 2);
6711
6712                         /* rewind */
6713                         cio_seek(j2k->cio, cio_tell(j2k->cio) - 2);
6714
6715                 }
6716
6717         };
6718 #endif /* USE_JPWL */
6719
6720 }
6721
6722 /**
6723  * Reads an unknown marker
6724  *
6725  * @param       p_stream                                the stream object to read from.
6726  * @param       p_j2k                   the jpeg2000 codec.
6727  * @param       p_manager               the user event manager.
6728  *
6729  * @return      true                    if the marker could be deduced.
6730 */
6731 opj_bool j2k_read_unk_v2 (      opj_j2k_v2_t *p_j2k,
6732                                                         struct opj_stream_private *p_stream,
6733                                                         OPJ_UINT32 *output_marker,
6734                                                         struct opj_event_mgr * p_manager
6735                                                         )
6736 {
6737         OPJ_UINT32 l_unknown_marker;
6738         const opj_dec_memory_marker_handler_t * l_marker_handler;
6739         OPJ_UINT32 l_size_unk = 2;
6740
6741         /* preconditions*/
6742         assert(p_j2k != 00);
6743         assert(p_manager != 00);
6744         assert(p_stream != 00);
6745
6746         opj_event_msg_v2(p_manager, EVT_WARNING, "Unknown marker\n");
6747
6748         while(1) {
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_unknown_marker,2);
6757
6758                 if (!(l_unknown_marker < 0xff00)) {
6759
6760                         /* Get the marker handler from the marker ID*/
6761                         l_marker_handler = j2k_get_marker_handler(l_unknown_marker);
6762
6763                         if (!(p_j2k->m_specific_param.m_decoder.m_state & l_marker_handler->states)) {
6764                                 opj_event_msg_v2(p_manager, EVT_ERROR, "Marker is not compliant with its position\n");
6765                                 return OPJ_FALSE;
6766                         }
6767                         else {
6768                                 if (l_marker_handler->id != J2K_MS_UNK) {
6769                                         /* Add the marker to the codestream index*/
6770                                         if (l_marker_handler->id != J2K_MS_SOT)
6771                                                 j2k_add_mhmarker_v2(p_j2k->cstr_index, J2K_MS_UNK,
6772                                                                                         (OPJ_UINT32) opj_stream_tell(p_stream) - l_size_unk,
6773                                                                                         l_size_unk);
6774                                         break; /* next marker is known and well located */
6775                                 }
6776                                 else
6777                                         l_size_unk += 2;
6778                         }
6779                 }
6780         }
6781
6782         *output_marker = l_marker_handler->id ;
6783
6784         return OPJ_TRUE;
6785 }
6786
6787 /**
6788 Read the lookup table containing all the marker, status and action
6789 @param id Marker value
6790 */
6791 static opj_dec_mstabent_t *j2k_dec_mstab_lookup(int id) {
6792         opj_dec_mstabent_t *e;
6793         for (e = j2k_dec_mstab; e->id != 0; e++) {
6794                 if (e->id == id) {
6795                         break;
6796                 }
6797         }
6798         return e;
6799 }
6800
6801 /**
6802  * Writes the MCT marker (Multiple Component Transform)
6803  *
6804  * @param       p_stream                                the stream to write data to.
6805  * @param       p_j2k                           J2K codec.
6806  * @param       p_manager               the user event manager.
6807 */
6808 opj_bool j2k_write_mct_record(  opj_j2k_v2_t *p_j2k,
6809                                                                 opj_mct_data_t * p_mct_record,
6810                                                                 struct opj_stream_private *p_stream,
6811                                                                 struct opj_event_mgr * p_manager )
6812 {
6813         OPJ_UINT32 l_mct_size;
6814         OPJ_BYTE * l_current_data = 00;
6815         OPJ_UINT32 l_tmp;
6816
6817         /* preconditions */
6818         assert(p_j2k != 00);
6819         assert(p_manager != 00);
6820         assert(p_stream != 00);
6821
6822         l_mct_size = 10 + p_mct_record->m_data_size;
6823
6824         if (l_mct_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
6825                 p_j2k->m_specific_param.m_encoder.m_header_tile_data
6826                         = (OPJ_BYTE*)opj_realloc(
6827                                 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
6828                                 l_mct_size);
6829
6830                 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
6831                         return OPJ_FALSE;
6832                 }
6833
6834                 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_mct_size;
6835         }
6836
6837         l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
6838
6839         opj_write_bytes(l_current_data,J2K_MS_MCT,2);                                   /* MCT */
6840         l_current_data += 2;
6841
6842         opj_write_bytes(l_current_data,l_mct_size-2,2);                                 /* Lmct */
6843         l_current_data += 2;
6844
6845         opj_write_bytes(l_current_data,0,2);                                                    /* Zmct */
6846         l_current_data += 2;
6847
6848         /* only one marker atm */
6849         l_tmp = (p_mct_record->m_index & 0xff) | (p_mct_record->m_array_type << 8) | (p_mct_record->m_element_type << 10);
6850
6851         opj_write_bytes(l_current_data,l_tmp,2);
6852         l_current_data += 2;
6853
6854         opj_write_bytes(l_current_data,0,2);                                                    /* Ymct */
6855         l_current_data+=2;
6856
6857         memcpy(l_current_data,p_mct_record->m_data,p_mct_record->m_data_size);
6858
6859         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) {
6860                 return OPJ_FALSE;
6861         }
6862
6863         return OPJ_TRUE;
6864 }
6865
6866 /**
6867  * Reads a MCT marker (Multiple Component Transform)
6868  *
6869  * @param       p_header_data   the data contained in the MCT box.
6870  * @param       p_j2k                   the jpeg2000 codec.
6871  * @param       p_header_size   the size of the data contained in the MCT marker.
6872  * @param       p_manager               the user event manager.
6873 */
6874 opj_bool j2k_read_mct ( opj_j2k_v2_t *p_j2k,
6875                                                 OPJ_BYTE * p_header_data,
6876                                                 OPJ_UINT32 p_header_size,
6877                                                 struct opj_event_mgr * p_manager )
6878 {
6879         OPJ_UINT32 i;
6880         opj_tcp_v2_t *l_tcp = 00;
6881         OPJ_UINT32 l_tmp;
6882         OPJ_UINT32 l_indix;
6883         opj_mct_data_t * l_mct_data;
6884
6885         /* preconditions */
6886         assert(p_header_data != 00);
6887         assert(p_j2k != 00);
6888
6889         l_tcp = p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH ?
6890                         &p_j2k->m_cp.tcps[p_j2k->m_current_tile_number] :
6891                         p_j2k->m_specific_param.m_decoder.m_default_tcp;
6892
6893         if (p_header_size < 2) {
6894                 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCT marker\n");
6895                 return OPJ_FALSE;
6896         }
6897
6898         /* first marker */
6899         opj_read_bytes(p_header_data,&l_tmp,2);                         /* Zmct */
6900         p_header_data += 2;
6901         if (l_tmp != 0) {
6902                 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge mct data within multiple MCT records\n");
6903                 return OPJ_TRUE;
6904         }
6905
6906         if(p_header_size <= 6) {
6907                 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCT marker\n");
6908                 return OPJ_FALSE;
6909         }
6910
6911         /* Imct -> no need for other values, take the first, type is double with decorrelation x0000 1101 0000 0000*/
6912         opj_read_bytes(p_header_data,&l_tmp,2);                         /* Imct */
6913         p_header_data += 2;
6914
6915         l_indix = l_tmp & 0xff;
6916         l_mct_data = l_tcp->m_mct_records;
6917
6918         for (i=0;i<l_tcp->m_nb_mct_records;++i) {
6919                 if (l_mct_data->m_index == l_indix) {
6920                         break;
6921                 }
6922                 ++l_mct_data;
6923         }
6924
6925         /* NOT FOUND */
6926         if (i == l_tcp->m_nb_mct_records) {
6927                 if (l_tcp->m_nb_mct_records == l_tcp->m_nb_max_mct_records) {
6928                         l_tcp->m_nb_max_mct_records += J2K_MCT_DEFAULT_NB_RECORDS;
6929
6930                         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));
6931                         if(! l_tcp->m_mct_records) {
6932                                 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCT marker\n");
6933                                 return OPJ_FALSE;
6934                         }
6935
6936                         l_mct_data = l_tcp->m_mct_records + l_tcp->m_nb_mct_records;
6937                         memset(l_mct_data ,0,(l_tcp->m_nb_max_mct_records - l_tcp->m_nb_mct_records) * sizeof(opj_mct_data_t));
6938                 }
6939
6940                 l_mct_data = l_tcp->m_mct_records + l_tcp->m_nb_mct_records;
6941         }
6942
6943         if (l_mct_data->m_data) {
6944                 opj_free(l_mct_data->m_data);
6945                 l_mct_data->m_data = 00;
6946         }
6947
6948         l_mct_data->m_index = l_indix;
6949         l_mct_data->m_array_type = (J2K_MCT_ARRAY_TYPE)((l_tmp  >> 8) & 3);
6950         l_mct_data->m_element_type = (J2K_MCT_ELEMENT_TYPE)((l_tmp  >> 10) & 3);
6951
6952         opj_read_bytes(p_header_data,&l_tmp,2);                         /* Ymct */
6953         p_header_data+=2;
6954         if (l_tmp != 0) {
6955                 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge multiple MCT markers\n");
6956                 return OPJ_TRUE;
6957         }
6958
6959         p_header_size -= 6;
6960
6961         l_mct_data->m_data = (OPJ_BYTE*)opj_malloc(p_header_size);
6962         if (! l_mct_data->m_data) {
6963                 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCT marker\n");
6964                 return OPJ_FALSE;
6965         }
6966         memcpy(l_mct_data->m_data,p_header_data,p_header_size);
6967
6968         l_mct_data->m_data_size = p_header_size;
6969         ++l_tcp->m_nb_mct_records;
6970
6971         return OPJ_TRUE;
6972 }
6973
6974 /**
6975  * Writes the MCC marker (Multiple Component Collection)
6976  *
6977  * @param       p_stream                the stream to write data to.
6978  * @param       p_j2k                   J2K codec.
6979  * @param       p_manager               the user event manager.
6980 */
6981 opj_bool j2k_write_mcc_record(  opj_j2k_v2_t *p_j2k,
6982                                                                 struct opj_simple_mcc_decorrelation_data * p_mcc_record,
6983                                                                 struct opj_stream_private *p_stream,
6984                                                                 struct opj_event_mgr * p_manager )
6985 {
6986         OPJ_UINT32 i;
6987         OPJ_UINT32 l_mcc_size;
6988         OPJ_BYTE * l_current_data = 00;
6989         OPJ_UINT32 l_nb_bytes_for_comp;
6990         OPJ_UINT32 l_mask;
6991         OPJ_UINT32 l_tmcc;
6992
6993         /* preconditions */
6994         assert(p_j2k != 00);
6995         assert(p_manager != 00);
6996         assert(p_stream != 00);
6997
6998         if (p_mcc_record->m_nb_comps > 255 ) {
6999         l_nb_bytes_for_comp = 2;
7000                 l_mask = 0x8000;
7001         }
7002         else {
7003                 l_nb_bytes_for_comp = 1;
7004                 l_mask = 0;
7005         }
7006
7007         l_mcc_size = p_mcc_record->m_nb_comps * 2 * l_nb_bytes_for_comp + 19;
7008         if (l_mcc_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size)
7009         {
7010                 p_j2k->m_specific_param.m_encoder.m_header_tile_data
7011                         = (OPJ_BYTE*)opj_realloc(
7012                                 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
7013                                 l_mcc_size);
7014                 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
7015                         return OPJ_FALSE;
7016                 }
7017
7018                 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_mcc_size;
7019         }
7020
7021         l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
7022
7023         opj_write_bytes(l_current_data,J2K_MS_MCC,2);                                   /* MCC */
7024         l_current_data += 2;
7025
7026         opj_write_bytes(l_current_data,l_mcc_size-2,2);                                 /* Lmcc */
7027         l_current_data += 2;
7028
7029         /* first marker */
7030         opj_write_bytes(l_current_data,0,2);                                    /* Zmcc */
7031         l_current_data += 2;
7032
7033         opj_write_bytes(l_current_data,p_mcc_record->m_index,1);                                        /* Imcc -> no need for other values, take the first */
7034         ++l_current_data;
7035
7036         /* only one marker atm */
7037         opj_write_bytes(l_current_data,0,2);                                    /* Ymcc */
7038         l_current_data+=2;
7039
7040         opj_write_bytes(l_current_data,1,2);                                    /* Qmcc -> number of collections -> 1 */
7041         l_current_data+=2;
7042
7043         opj_write_bytes(l_current_data,0x1,1);                                  /* Xmcci type of component transformation -> array based decorrelation */
7044         ++l_current_data;
7045
7046         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 */
7047         l_current_data+=2;
7048
7049         for (i=0;i<p_mcc_record->m_nb_comps;++i) {
7050                 opj_write_bytes(l_current_data,i,l_nb_bytes_for_comp);                          /* Cmccij Component offset*/
7051                 l_current_data+=l_nb_bytes_for_comp;
7052         }
7053
7054         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 */
7055         l_current_data+=2;
7056
7057         for (i=0;i<p_mcc_record->m_nb_comps;++i)
7058         {
7059                 opj_write_bytes(l_current_data,i,l_nb_bytes_for_comp);                          /* Wmccij Component offset*/
7060                 l_current_data+=l_nb_bytes_for_comp;
7061         }
7062
7063         l_tmcc = ((!p_mcc_record->m_is_irreversible)&1)<<16;
7064
7065         if (p_mcc_record->m_decorrelation_array) {
7066                 l_tmcc |= p_mcc_record->m_decorrelation_array->m_index;
7067         }
7068
7069         if (p_mcc_record->m_offset_array) {
7070                 l_tmcc |= ((p_mcc_record->m_offset_array->m_index)<<8);
7071         }
7072
7073         opj_write_bytes(l_current_data,l_tmcc,3);       /* Tmcci : use MCT defined as number 1 and irreversible array based. */
7074         l_current_data+=3;
7075
7076         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) {
7077                 return OPJ_FALSE;
7078         }
7079
7080         return OPJ_TRUE;
7081 }
7082
7083 /**
7084  * Reads a MCC marker (Multiple Component Collection)
7085  *
7086  * @param       p_header_data   the data contained in the MCC box.
7087  * @param       p_j2k                   the jpeg2000 codec.
7088  * @param       p_header_size   the size of the data contained in the MCC marker.
7089  * @param       p_manager               the user event manager.
7090 */
7091 opj_bool j2k_read_mcc ( opj_j2k_v2_t *p_j2k,
7092                                         OPJ_BYTE * p_header_data,
7093                                         OPJ_UINT32 p_header_size,
7094                                         struct opj_event_mgr * p_manager )
7095 {
7096         OPJ_UINT32 i,j;
7097         OPJ_UINT32 l_tmp;
7098         OPJ_UINT32 l_indix;
7099         opj_tcp_v2_t * l_tcp;
7100         opj_simple_mcc_decorrelation_data_t * l_mcc_record;
7101         opj_mct_data_t * l_mct_data;
7102         OPJ_UINT32 l_nb_collections;
7103         OPJ_UINT32 l_nb_comps;
7104         OPJ_UINT32 l_nb_bytes_by_comp;
7105
7106
7107         /* preconditions */
7108         assert(p_header_data != 00);
7109         assert(p_j2k != 00);
7110         assert(p_manager != 00);
7111
7112         l_tcp = p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH ?
7113                         &p_j2k->m_cp.tcps[p_j2k->m_current_tile_number] :
7114                         p_j2k->m_specific_param.m_decoder.m_default_tcp;
7115
7116         if (p_header_size < 2) {
7117                 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
7118                 return OPJ_FALSE;
7119         }
7120
7121         /* first marker */
7122         opj_read_bytes(p_header_data,&l_tmp,2);                         /* Zmcc */
7123         p_header_data += 2;
7124         if (l_tmp != 0) {
7125                 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge multiple data spanning\n");
7126                 return OPJ_TRUE;
7127         }
7128
7129         if (p_header_size < 7) {
7130                 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
7131                 return OPJ_FALSE;
7132         }
7133
7134         opj_read_bytes(p_header_data,&l_indix,1); /* Imcc -> no need for other values, take the first */
7135         ++p_header_data;
7136
7137         l_mcc_record = l_tcp->m_mcc_records;
7138
7139         for(i=0;i<l_tcp->m_nb_mcc_records;++i) {
7140                 if (l_mcc_record->m_index == l_indix) {
7141                         break;
7142                 }
7143                 ++l_mcc_record;
7144         }
7145
7146         /** NOT FOUND */
7147         if (i == l_tcp->m_nb_mcc_records) {
7148                 if (l_tcp->m_nb_mcc_records == l_tcp->m_nb_max_mcc_records) {
7149                         l_tcp->m_nb_max_mcc_records += J2K_MCC_DEFAULT_NB_RECORDS;
7150
7151                         l_tcp->m_mcc_records = (opj_simple_mcc_decorrelation_data_t*)
7152                                         opj_realloc(l_tcp->m_mcc_records,l_tcp->m_nb_max_mcc_records * sizeof(opj_simple_mcc_decorrelation_data_t));
7153                         if (! l_tcp->m_mcc_records) {
7154                                 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
7155                                 return OPJ_FALSE;
7156                         }
7157                         l_mcc_record = l_tcp->m_mcc_records + l_tcp->m_nb_mcc_records;
7158                         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));
7159                 }
7160                 l_mcc_record = l_tcp->m_mcc_records + l_tcp->m_nb_mcc_records;
7161         }
7162         l_mcc_record->m_index = l_indix;
7163
7164         /* only one marker atm */
7165         opj_read_bytes(p_header_data,&l_tmp,2);                         /* Ymcc */
7166         p_header_data+=2;
7167         if (l_tmp != 0) {
7168                 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge multiple data spanning\n");
7169                 return OPJ_TRUE;
7170         }
7171
7172         opj_read_bytes(p_header_data,&l_nb_collections,2);                              /* Qmcc -> number of collections -> 1 */
7173         p_header_data+=2;
7174
7175         if (l_nb_collections > 1) {
7176                 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge multiple collections\n");
7177                 return OPJ_TRUE;
7178         }
7179
7180         p_header_size -= 7;
7181
7182         for (i=0;i<l_nb_collections;++i) {
7183                 if (p_header_size < 3) {
7184                         opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
7185                         return OPJ_FALSE;
7186                 }
7187
7188                 opj_read_bytes(p_header_data,&l_tmp,1); /* Xmcci type of component transformation -> array based decorrelation */
7189                 ++p_header_data;
7190
7191                 if (l_tmp != 1) {
7192                         opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge collections other than array decorrelation\n");
7193                         return OPJ_TRUE;
7194                 }
7195
7196                 opj_read_bytes(p_header_data,&l_nb_comps,2);
7197
7198                 p_header_data+=2;
7199                 p_header_size-=3;
7200
7201                 l_nb_bytes_by_comp = 1 + (l_nb_comps>>15);
7202                 l_mcc_record->m_nb_comps = l_nb_comps & 0x7fff;
7203
7204                 if (p_header_size < (l_nb_bytes_by_comp * l_mcc_record->m_nb_comps + 2)) {
7205                         opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
7206                         return OPJ_FALSE;
7207                 }
7208
7209                 p_header_size -= (l_nb_bytes_by_comp * l_mcc_record->m_nb_comps + 2);
7210
7211                 for (j=0;j<l_mcc_record->m_nb_comps;++j) {
7212                         opj_read_bytes(p_header_data,&l_tmp,l_nb_bytes_by_comp);        /* Cmccij Component offset*/
7213                         p_header_data+=l_nb_bytes_by_comp;
7214
7215                         if (l_tmp != j) {
7216                                 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge collections with indix shuffle\n");
7217                                 return OPJ_TRUE;
7218                         }
7219                 }
7220
7221                 opj_read_bytes(p_header_data,&l_nb_comps,2);
7222                 p_header_data+=2;
7223
7224                 l_nb_bytes_by_comp = 1 + (l_nb_comps>>15);
7225                 l_nb_comps &= 0x7fff;
7226
7227                 if (l_nb_comps != l_mcc_record->m_nb_comps) {
7228                         opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge collections without same number of indixes\n");
7229                         return OPJ_TRUE;
7230                 }
7231
7232                 if (p_header_size < (l_nb_bytes_by_comp * l_mcc_record->m_nb_comps + 3)) {
7233                         opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
7234                         return OPJ_FALSE;
7235                 }
7236
7237                 p_header_size -= (l_nb_bytes_by_comp * l_mcc_record->m_nb_comps + 3);
7238
7239                 for (j=0;j<l_mcc_record->m_nb_comps;++j) {
7240                         opj_read_bytes(p_header_data,&l_tmp,l_nb_bytes_by_comp);        /* Wmccij Component offset*/
7241                         p_header_data+=l_nb_bytes_by_comp;
7242
7243                         if (l_tmp != j) {
7244                                 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge collections with indix shuffle\n");
7245                                 return OPJ_TRUE;
7246                         }
7247                 }
7248
7249                 opj_read_bytes(p_header_data,&l_tmp,3); /* Wmccij Component offset*/
7250                 p_header_data += 3;
7251
7252                 l_mcc_record->m_is_irreversible = ! ((l_tmp>>16) & 1);
7253                 l_mcc_record->m_decorrelation_array = 00;
7254                 l_mcc_record->m_offset_array = 00;
7255
7256                 l_indix = l_tmp & 0xff;
7257                 if (l_indix != 0) {
7258                         l_mct_data = l_tcp->m_mct_records;
7259                         for (j=0;j<l_tcp->m_nb_mct_records;++j) {
7260                                 if (l_mct_data->m_index == l_indix) {
7261                                         l_mcc_record->m_decorrelation_array = l_mct_data;
7262                                         break;
7263                                 }
7264                                 ++l_mct_data;
7265                         }
7266
7267                         if (l_mcc_record->m_decorrelation_array == 00) {
7268                                 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
7269                                 return OPJ_FALSE;
7270                         }
7271                 }
7272
7273                 l_indix = (l_tmp >> 8) & 0xff;
7274                 if (l_indix != 0) {
7275                         l_mct_data = l_tcp->m_mct_records;
7276                         for (j=0;j<l_tcp->m_nb_mct_records;++j) {
7277                                 if (l_mct_data->m_index == l_indix) {
7278                                         l_mcc_record->m_offset_array = l_mct_data;
7279                                         break;
7280                                 }
7281                                 ++l_mct_data;
7282                         }
7283
7284                         if (l_mcc_record->m_offset_array == 00) {
7285                                 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
7286                                 return OPJ_FALSE;
7287                         }
7288                 }
7289         }
7290
7291         if (p_header_size != 0) {
7292                 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
7293                 return OPJ_FALSE;
7294         }
7295
7296         ++l_tcp->m_nb_mcc_records;
7297
7298         return OPJ_TRUE;
7299 }
7300
7301
7302 /**
7303  * Writes the MCO marker (Multiple component transformation ordering)
7304  *
7305  * @param       p_stream                                the stream to write data to.
7306  * @param       p_j2k                           J2K codec.
7307  * @param       p_manager               the user event manager.
7308 */
7309 opj_bool j2k_write_mco( opj_j2k_v2_t *p_j2k,
7310                                                 struct opj_stream_private *p_stream,
7311                                                 struct opj_event_mgr * p_manager
7312                                   )
7313 {
7314         OPJ_BYTE * l_current_data = 00;
7315         OPJ_UINT32 l_mco_size;
7316         opj_tcp_v2_t * l_tcp = 00;
7317         opj_simple_mcc_decorrelation_data_t * l_mcc_record;
7318         OPJ_UINT32 i;
7319
7320         /* preconditions */
7321         assert(p_j2k != 00);
7322         assert(p_manager != 00);
7323         assert(p_stream != 00);
7324
7325         l_tcp =&(p_j2k->m_cp.tcps[p_j2k->m_current_tile_number]);
7326         l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
7327
7328         l_mco_size = 5 + l_tcp->m_nb_mcc_records;
7329         if (l_mco_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
7330
7331                 p_j2k->m_specific_param.m_encoder.m_header_tile_data
7332                         = (OPJ_BYTE*)opj_realloc(
7333                                 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
7334                                 l_mco_size);
7335                 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data)
7336                 {
7337                         return OPJ_FALSE;
7338                 }
7339
7340                 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_mco_size;
7341         }
7342
7343         opj_write_bytes(l_current_data,J2K_MS_MCO,2);                   /* MCO */
7344         l_current_data += 2;
7345
7346         opj_write_bytes(l_current_data,l_mco_size-2,2);                                 /* Lmco */
7347         l_current_data += 2;
7348
7349         opj_write_bytes(l_current_data,l_tcp->m_nb_mcc_records,1);                                      /* Nmco : only one tranform stage*/
7350         ++l_current_data;
7351
7352         l_mcc_record = l_tcp->m_mcc_records;
7353         for     (i=0;i<l_tcp->m_nb_mcc_records;++i) {
7354                 opj_write_bytes(l_current_data,l_mcc_record->m_index,1);                                        /* Imco -> use the mcc indicated by 1*/
7355                 ++l_current_data;
7356
7357                 ++l_mcc_record;
7358         }
7359
7360         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) {
7361                 return OPJ_FALSE;
7362         }
7363
7364         return OPJ_TRUE;
7365 }
7366
7367 /**
7368  * Reads a MCO marker (Multiple Component Transform Ordering)
7369  *
7370  * @param       p_header_data   the data contained in the MCO box.
7371  * @param       p_j2k                   the jpeg2000 codec.
7372  * @param       p_header_size   the size of the data contained in the MCO marker.
7373  * @param       p_manager               the user event manager.
7374 */
7375 opj_bool j2k_read_mco ( opj_j2k_v2_t *p_j2k,
7376                                                 OPJ_BYTE * p_header_data,
7377                                                 OPJ_UINT32 p_header_size,
7378                                                 struct opj_event_mgr * p_manager )
7379 {
7380         OPJ_UINT32 l_tmp, i;
7381         OPJ_UINT32 l_nb_stages;
7382         opj_tcp_v2_t * l_tcp;
7383         opj_tccp_t * l_tccp;
7384         opj_image_t * l_image;
7385         opj_image_comp_t * l_img_comp;
7386
7387         /* preconditions */
7388         assert(p_header_data != 00);
7389         assert(p_j2k != 00);
7390         assert(p_manager != 00);
7391
7392         l_image = p_j2k->m_private_image;
7393         l_tcp = p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH ?
7394                         &p_j2k->m_cp.tcps[p_j2k->m_current_tile_number] :
7395                         p_j2k->m_specific_param.m_decoder.m_default_tcp;
7396
7397         if (p_header_size < 1) {
7398                 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCO marker\n");
7399                 return OPJ_FALSE;
7400         }
7401
7402         opj_read_bytes(p_header_data,&l_nb_stages,1);                           /* Nmco : only one tranform stage*/
7403         ++p_header_data;
7404
7405         if (l_nb_stages > 1) {
7406                 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge multiple transformation stages.\n");
7407                 return OPJ_TRUE;
7408         }
7409
7410         if (p_header_size != l_nb_stages + 1) {
7411                 opj_event_msg_v2(p_manager, EVT_WARNING, "Error reading MCO marker\n");
7412                 return OPJ_FALSE;
7413         }
7414
7415         l_tccp = l_tcp->tccps;
7416         l_img_comp = l_image->comps;
7417
7418         for (i=0;i<l_image->numcomps;++i) {
7419                 l_tccp->m_dc_level_shift = 0;
7420                 ++l_tccp;
7421         }
7422
7423         if (l_tcp->m_mct_decoding_matrix) {
7424                 opj_free(l_tcp->m_mct_decoding_matrix);
7425                 l_tcp->m_mct_decoding_matrix = 00;
7426         }
7427
7428         for (i=0;i<l_nb_stages;++i) {
7429                 opj_read_bytes(p_header_data,&l_tmp,1);
7430                 ++p_header_data;
7431
7432                 if (! j2k_add_mct(l_tcp,p_j2k->m_private_image,l_tmp)) {
7433                         return OPJ_FALSE;
7434                 }
7435         }
7436
7437         return OPJ_TRUE;
7438 }
7439
7440 opj_bool j2k_add_mct(opj_tcp_v2_t * p_tcp, opj_image_t * p_image, OPJ_UINT32 p_index)
7441 {
7442         OPJ_UINT32 i;
7443         opj_simple_mcc_decorrelation_data_t * l_mcc_record;
7444         opj_mct_data_t * l_deco_array, * l_offset_array;
7445         OPJ_UINT32 l_data_size,l_mct_size, l_offset_size;
7446         OPJ_UINT32 l_nb_elem;
7447         OPJ_UINT32 * l_offset_data, * l_current_offset_data;
7448         opj_tccp_t * l_tccp;
7449
7450         /* preconditions */
7451         assert(p_tcp != 00);
7452
7453         l_mcc_record = p_tcp->m_mcc_records;
7454
7455         for (i=0;i<p_tcp->m_nb_mcc_records;++i) {
7456                 if (l_mcc_record->m_index == p_index) {
7457                         break;
7458                 }
7459         }
7460
7461         if (i==p_tcp->m_nb_mcc_records) {
7462                 /** element discarded **/
7463                 return OPJ_TRUE;
7464         }
7465
7466         if (l_mcc_record->m_nb_comps != p_image->numcomps) {
7467                 /** do not support number of comps != image */
7468                 return OPJ_TRUE;
7469         }
7470
7471         l_deco_array = l_mcc_record->m_decorrelation_array;
7472
7473         if (l_deco_array) {
7474                 l_data_size = MCT_ELEMENT_SIZE[l_deco_array->m_element_type] * p_image->numcomps * p_image->numcomps;
7475                 if (l_deco_array->m_data_size != l_data_size) {
7476                         return OPJ_FALSE;
7477                 }
7478
7479                 l_nb_elem = p_image->numcomps * p_image->numcomps;
7480                 l_mct_size = l_nb_elem * sizeof(OPJ_FLOAT32);
7481                 p_tcp->m_mct_decoding_matrix = (OPJ_FLOAT32*)opj_malloc(l_mct_size);
7482
7483                 if (! p_tcp->m_mct_decoding_matrix ) {
7484                         return OPJ_FALSE;
7485                 }
7486
7487                 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);
7488         }
7489
7490         l_offset_array = l_mcc_record->m_offset_array;
7491
7492         if (l_offset_array) {
7493                 l_data_size = MCT_ELEMENT_SIZE[l_offset_array->m_element_type] * p_image->numcomps;
7494                 if (l_offset_array->m_data_size != l_data_size) {
7495                         return OPJ_FALSE;
7496                 }
7497
7498                 l_nb_elem = p_image->numcomps;
7499                 l_offset_size = l_nb_elem * sizeof(OPJ_UINT32);
7500                 l_offset_data = (OPJ_UINT32*)opj_malloc(l_offset_size);
7501
7502                 if (! l_offset_data ) {
7503                         return OPJ_FALSE;
7504                 }
7505
7506                 j2k_mct_read_functions_to_int32[l_offset_array->m_element_type](l_offset_array->m_data,l_offset_data,l_nb_elem);
7507
7508                 l_tccp = p_tcp->tccps;
7509                 l_current_offset_data = l_offset_data;
7510
7511                 for (i=0;i<p_image->numcomps;++i) {
7512                         l_tccp->m_dc_level_shift = *(l_current_offset_data++);
7513                         ++l_tccp;
7514                 }
7515
7516                 opj_free(l_offset_data);
7517         }
7518
7519         return OPJ_TRUE;
7520 }
7521
7522 /**
7523  * Writes the CBD marker (Component bit depth definition)
7524  *
7525  * @param       p_stream                                the stream to write data to.
7526  * @param       p_j2k                           J2K codec.
7527  * @param       p_manager               the user event manager.
7528 */
7529 opj_bool j2k_write_cbd( opj_j2k_v2_t *p_j2k,
7530                                                 struct opj_stream_private *p_stream,
7531                                                 struct opj_event_mgr * p_manager )
7532 {
7533         OPJ_UINT32 i;
7534         OPJ_UINT32 l_cbd_size;
7535         OPJ_BYTE * l_current_data = 00;
7536         opj_image_t *l_image = 00;
7537         opj_image_comp_t * l_comp = 00;
7538
7539         /* preconditions */
7540         assert(p_j2k != 00);
7541         assert(p_manager != 00);
7542         assert(p_stream != 00);
7543
7544         l_image = p_j2k->m_private_image;
7545         l_cbd_size = 6 + p_j2k->m_private_image->numcomps;
7546
7547         if (l_cbd_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
7548                 p_j2k->m_specific_param.m_encoder.m_header_tile_data
7549                         = (OPJ_BYTE*)opj_realloc(
7550                                 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
7551                                 l_cbd_size);
7552
7553                 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
7554                         return OPJ_FALSE;
7555                 }
7556
7557                 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_cbd_size;
7558         }
7559
7560         l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
7561
7562         opj_write_bytes(l_current_data,J2K_MS_CBD,2);                                   /* CBD */
7563         l_current_data += 2;
7564
7565         opj_write_bytes(l_current_data,l_cbd_size-2,2);                                 /* L_CBD */
7566         l_current_data += 2;
7567
7568         opj_write_bytes(l_current_data,l_image->numcomps, 2);           /* Ncbd */
7569         l_current_data+=2;
7570
7571         l_comp = l_image->comps;
7572
7573         for (i=0;i<l_image->numcomps;++i) {
7574                 opj_write_bytes(l_current_data, (l_comp->sgnd << 7) | (l_comp->prec - 1), 1);           /* Component bit depth */
7575                 ++l_current_data;
7576
7577                 ++l_comp;
7578         }
7579
7580         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) {
7581                 return OPJ_FALSE;
7582         }
7583
7584         return OPJ_TRUE;
7585 }
7586
7587 /**
7588  * Reads a CBD marker (Component bit depth definition)
7589  * @param       p_header_data   the data contained in the CBD box.
7590  * @param       p_j2k                   the jpeg2000 codec.
7591  * @param       p_header_size   the size of the data contained in the CBD marker.
7592  * @param       p_manager               the user event manager.
7593 */
7594 opj_bool j2k_read_cbd ( opj_j2k_v2_t *p_j2k,
7595                                                 OPJ_BYTE * p_header_data,
7596                                                 OPJ_UINT32 p_header_size,
7597                                                 struct opj_event_mgr * p_manager)
7598 {
7599         OPJ_UINT32 l_nb_comp,l_num_comp;
7600         OPJ_UINT32 l_comp_def;
7601         OPJ_UINT32 i;
7602         opj_image_comp_t * l_comp = 00;
7603
7604         /* preconditions */
7605         assert(p_header_data != 00);
7606         assert(p_j2k != 00);
7607         assert(p_manager != 00);
7608
7609         l_num_comp = p_j2k->m_private_image->numcomps;
7610
7611         if (p_header_size != (p_j2k->m_private_image->numcomps + 2)) {
7612                 opj_event_msg_v2(p_manager, EVT_ERROR, "Crror reading CBD marker\n");
7613                 return OPJ_FALSE;
7614         }
7615
7616         opj_read_bytes(p_header_data,&l_nb_comp,2);                             /* Ncbd */
7617         p_header_data+=2;
7618
7619         if (l_nb_comp != l_num_comp) {
7620                 opj_event_msg_v2(p_manager, EVT_ERROR, "Crror reading CBD marker\n");
7621                 return OPJ_FALSE;
7622         }
7623
7624         l_comp = p_j2k->m_private_image->comps;
7625         for (i=0;i<l_num_comp;++i) {
7626                 opj_read_bytes(p_header_data,&l_comp_def,1);                    /* Component bit depth */
7627                 ++p_header_data;
7628         l_comp->sgnd = (l_comp_def>>7) & 1;
7629                 l_comp->prec = (l_comp_def&0x7f) + 1;
7630                 ++l_comp;
7631         }
7632
7633         return OPJ_TRUE;
7634 }
7635
7636
7637 /* ----------------------------------------------------------------------- */
7638 /* J2K / JPT decoder interface                                             */
7639 /* ----------------------------------------------------------------------- */
7640
7641 opj_j2k_t* j2k_create_decompress(opj_common_ptr cinfo) {
7642         opj_j2k_t *j2k = (opj_j2k_t*) opj_calloc(1, sizeof(opj_j2k_t));
7643         if(!j2k)
7644                 return NULL;
7645
7646         j2k->default_tcp = (opj_tcp_t*) opj_calloc(1, sizeof(opj_tcp_t));
7647         if(!j2k->default_tcp) {
7648                 opj_free(j2k);
7649                 return NULL;
7650         }
7651
7652         j2k->cinfo = cinfo;
7653         j2k->tile_data = NULL;
7654
7655         return j2k;
7656 }
7657
7658 void j2k_destroy_decompress(opj_j2k_t *j2k) {
7659         int i = 0;
7660
7661         if(j2k->tile_len != NULL) {
7662                 opj_free(j2k->tile_len);
7663         }
7664         if(j2k->tile_data != NULL) {
7665                 opj_free(j2k->tile_data);
7666         }
7667         if(j2k->default_tcp != NULL) {
7668                 opj_tcp_t *default_tcp = j2k->default_tcp;
7669                 if(default_tcp->ppt_data_first != NULL) {
7670                         opj_free(default_tcp->ppt_data_first);
7671                 }
7672                 if(j2k->default_tcp->tccps != NULL) {
7673                         opj_free(j2k->default_tcp->tccps);
7674                 }
7675                 opj_free(j2k->default_tcp);
7676         }
7677         if(j2k->cp != NULL) {
7678                 opj_cp_t *cp = j2k->cp;
7679                 if(cp->tcps != NULL) {
7680                         for(i = 0; i < cp->tw * cp->th; i++) {
7681                                 if(cp->tcps[i].ppt_data_first != NULL) {
7682                                         opj_free(cp->tcps[i].ppt_data_first);
7683                                 }
7684                                 if(cp->tcps[i].tccps != NULL) {
7685                                         opj_free(cp->tcps[i].tccps);
7686                                 }
7687                         }
7688                         opj_free(cp->tcps);
7689                 }
7690                 if(cp->ppm_data_first != NULL) {
7691                         opj_free(cp->ppm_data_first);
7692                 }
7693                 if(cp->tileno != NULL) {
7694                         opj_free(cp->tileno);  
7695                 }
7696                 if(cp->comment != NULL) {
7697                         opj_free(cp->comment);
7698                 }
7699
7700                 opj_free(cp);
7701         }
7702         opj_free(j2k);
7703 }
7704
7705 void j2k_setup_decoder(opj_j2k_t *j2k, opj_dparameters_t *parameters) {
7706         if(j2k && parameters) {
7707                 /* create and initialize the coding parameters structure */
7708                 opj_cp_t *cp = (opj_cp_t*) opj_calloc(1, sizeof(opj_cp_t));
7709                 cp->reduce = parameters->cp_reduce;     
7710                 cp->layer = parameters->cp_layer;
7711                 cp->limit_decoding = parameters->cp_limit_decoding;
7712
7713 #ifdef USE_JPWL
7714                 cp->correct = parameters->jpwl_correct;
7715                 cp->exp_comps = parameters->jpwl_exp_comps;
7716                 cp->max_tiles = parameters->jpwl_max_tiles;
7717 #endif /* USE_JPWL */
7718
7719
7720                 /* keep a link to cp so that we can destroy it later in j2k_destroy_decompress */
7721                 j2k->cp = cp;
7722         }
7723 }
7724
7725 void j2k_setup_decoder_v2(opj_j2k_v2_t *j2k, opj_dparameters_t *parameters)
7726 {
7727         if(j2k && parameters) {
7728                 j2k->m_cp.m_specific_param.m_dec.m_layer = parameters->cp_layer;
7729                 j2k->m_cp.m_specific_param.m_dec.m_reduce = parameters->cp_reduce;
7730
7731 #ifdef USE_JPWL
7732                 j2k->m_cp.correct = parameters->jpwl_correct;
7733                 j2k->m_cp.exp_comps = parameters->jpwl_exp_comps;
7734                 j2k->m_cp.max_tiles = parameters->jpwl_max_tiles;
7735 #endif /* USE_JPWL */
7736         }
7737 }
7738
7739 opj_image_t* j2k_decode(opj_j2k_t *j2k, opj_cio_t *cio, opj_codestream_info_t *cstr_info) {
7740         opj_image_t *image = NULL;
7741
7742         opj_common_ptr cinfo = j2k->cinfo;      
7743
7744         j2k->cio = cio;
7745         j2k->cstr_info = cstr_info;
7746         if (cstr_info)
7747                 memset(cstr_info, 0, sizeof(opj_codestream_info_t));
7748
7749         /* create an empty image */
7750         image = opj_image_create0();
7751         j2k->image = image;
7752
7753         j2k->state = J2K_STATE_MHSOC;
7754
7755         for (;;) {
7756                 opj_dec_mstabent_t *e;
7757                 int id = cio_read(cio, 2);
7758
7759 #ifdef USE_JPWL
7760                 /* we try to honor JPWL correction power */
7761                 if (j2k->cp->correct) {
7762
7763                         int orig_pos = cio_tell(cio);
7764                         opj_bool status;
7765
7766                         /* call the corrector */
7767                         status = jpwl_correct(j2k);
7768
7769                         /* go back to where you were */
7770                         cio_seek(cio, orig_pos - 2);
7771
7772                         /* re-read the marker */
7773                         id = cio_read(cio, 2);
7774
7775                         /* check whether it begins with ff */
7776                         if (id >> 8 != 0xff) {
7777                                 opj_event_msg(cinfo, EVT_ERROR,
7778                                         "JPWL: possible bad marker %x at %d\n",
7779                                         id, cio_tell(cio) - 2);
7780                                 if (!JPWL_ASSUME) {
7781                                         opj_image_destroy(image);
7782                                         opj_event_msg(cinfo, EVT_ERROR, "JPWL: giving up\n");
7783                                         return 0;
7784                                 }
7785                                 /* we try to correct */
7786                                 id = id | 0xff00;
7787                                 cio_seek(cio, cio_tell(cio) - 2);
7788                                 cio_write(cio, id, 2);
7789                                 opj_event_msg(cinfo, EVT_WARNING, "- trying to adjust this\n"
7790                                         "- setting marker to %x\n",
7791                                         id);
7792                         }
7793
7794                 }
7795 #endif /* USE_JPWL */
7796
7797                 if (id >> 8 != 0xff) {
7798                         opj_image_destroy(image);
7799                         opj_event_msg(cinfo, EVT_ERROR, "%.8x: expected a marker instead of %x\n", cio_tell(cio) - 2, id);
7800                         return 0;
7801                 }
7802                 e = j2k_dec_mstab_lookup(id);
7803                 /* Check if the marker is known*/
7804                 if (!(j2k->state & e->states)) {
7805                         opj_image_destroy(image);
7806                         opj_event_msg(cinfo, EVT_ERROR, "%.8x: unexpected marker %x\n", cio_tell(cio) - 2, id);
7807                         return 0;
7808                 }
7809                 /* Check if the decoding is limited to the main header*/
7810                 if (e->id == J2K_MS_SOT && j2k->cp->limit_decoding == LIMIT_TO_MAIN_HEADER) {
7811                         opj_event_msg(cinfo, EVT_INFO, "Main Header decoded.\n");
7812                         return image;
7813                 }               
7814
7815                 if (e->handler) {
7816                         (*e->handler)(j2k);
7817                 }
7818                 if (j2k->state & J2K_STATE_ERR) 
7819                         return NULL;    
7820
7821                 if (j2k->state == J2K_STATE_MT) {
7822                         break;
7823                 }
7824                 if (j2k->state == J2K_STATE_NEOC) {
7825                         break;
7826                 }
7827         }
7828         if (j2k->state == J2K_STATE_NEOC) {
7829                 j2k_read_eoc(j2k);
7830         }
7831
7832         if (j2k->state != J2K_STATE_MT) {
7833                 opj_event_msg(cinfo, EVT_WARNING, "Incomplete bitstream\n");
7834         }
7835         return image;
7836 }
7837
7838 /*
7839 * Read a JPT-stream and decode file
7840 *
7841 */
7842 opj_image_t* j2k_decode_jpt_stream(opj_j2k_t *j2k, opj_cio_t *cio,  opj_codestream_info_t *cstr_info) {
7843         opj_image_t *image = NULL;
7844         opj_jpt_msg_header_t header;
7845         int position;
7846         opj_common_ptr cinfo = j2k->cinfo;
7847
7848         OPJ_ARG_NOT_USED(cstr_info);
7849
7850         j2k->cio = cio;
7851
7852         /* create an empty image */
7853         image = opj_image_create0();
7854         j2k->image = image;
7855
7856         j2k->state = J2K_STATE_MHSOC;
7857         
7858         /* Initialize the header */
7859         jpt_init_msg_header(&header);
7860         /* Read the first header of the message */
7861         jpt_read_msg_header(cinfo, cio, &header);
7862         
7863         position = cio_tell(cio);
7864         if (header.Class_Id != 6) {     /* 6 : Main header data-bin message */
7865                 opj_image_destroy(image);
7866                 opj_event_msg(cinfo, EVT_ERROR, "[JPT-stream] : Expecting Main header first [class_Id %d] !\n", header.Class_Id);
7867                 return 0;
7868         }
7869         
7870         for (;;) {
7871                 opj_dec_mstabent_t *e = NULL;
7872                 int id;
7873                 
7874                 if (!cio_numbytesleft(cio)) {
7875                         j2k_read_eoc(j2k);
7876                         return image;
7877                 }
7878                 /* data-bin read -> need to read a new header */
7879                 if ((unsigned int) (cio_tell(cio) - position) == header.Msg_length) {
7880                         jpt_read_msg_header(cinfo, cio, &header);
7881                         position = cio_tell(cio);
7882                         if (header.Class_Id != 4) {     /* 4 : Tile data-bin message */
7883                                 opj_image_destroy(image);
7884                                 opj_event_msg(cinfo, EVT_ERROR, "[JPT-stream] : Expecting Tile info !\n");
7885                                 return 0;
7886                         }
7887                 }
7888                 
7889                 id = cio_read(cio, 2);
7890                 if (id >> 8 != 0xff) {
7891                         opj_image_destroy(image);
7892                         opj_event_msg(cinfo, EVT_ERROR, "%.8x: expected a marker instead of %x\n", cio_tell(cio) - 2, id);
7893                         return 0;
7894                 }
7895                 e = j2k_dec_mstab_lookup(id);
7896                 if (!(j2k->state & e->states)) {
7897                         opj_image_destroy(image);
7898                         opj_event_msg(cinfo, EVT_ERROR, "%.8x: unexpected marker %x\n", cio_tell(cio) - 2, id);
7899                         return 0;
7900                 }
7901                 if (e->handler) {
7902                         (*e->handler)(j2k);
7903                 }
7904                 if (j2k->state == J2K_STATE_MT) {
7905                         break;
7906                 }
7907                 if (j2k->state == J2K_STATE_NEOC) {
7908                         break;
7909                 }
7910         }
7911         if (j2k->state == J2K_STATE_NEOC) {
7912                 j2k_read_eoc(j2k);
7913         }
7914         
7915         if (j2k->state != J2K_STATE_MT) {
7916                 opj_event_msg(cinfo, EVT_WARNING, "Incomplete bitstream\n");
7917         }
7918
7919         return image;
7920 }
7921
7922 /* ----------------------------------------------------------------------- */
7923 /* J2K encoder interface                                                       */
7924 /* ----------------------------------------------------------------------- */
7925
7926 opj_j2k_t* j2k_create_compress(opj_common_ptr cinfo) {
7927         opj_j2k_t *j2k = (opj_j2k_t*) opj_calloc(1, sizeof(opj_j2k_t));
7928         if(j2k) {
7929                 j2k->cinfo = cinfo;
7930         }
7931         return j2k;
7932 }
7933
7934 opj_j2k_v2_t* j2k_create_compress_v2(void)
7935 {
7936         opj_j2k_v2_t *l_j2k = (opj_j2k_v2_t*) opj_malloc(sizeof(opj_j2k_v2_t));
7937         if (!l_j2k) {
7938                 return NULL;
7939         }
7940
7941         memset(l_j2k,0,sizeof(opj_j2k_v2_t));
7942
7943         l_j2k->m_is_decoder = 0;
7944         l_j2k->m_cp.m_is_decoder = 0;
7945
7946         l_j2k->m_specific_param.m_encoder.m_header_tile_data = (OPJ_BYTE *) opj_malloc(J2K_DEFAULT_HEADER_SIZE);
7947         if (! l_j2k->m_specific_param.m_encoder.m_header_tile_data) {
7948                 j2k_destroy(l_j2k);
7949                 return NULL;
7950         }
7951
7952         l_j2k->m_specific_param.m_encoder.m_header_tile_data_size = J2K_DEFAULT_HEADER_SIZE;
7953
7954         /* validation list creation*/
7955         l_j2k->m_validation_list = opj_procedure_list_create();
7956         if (! l_j2k->m_validation_list) {
7957                 j2k_destroy(l_j2k);
7958                 return NULL;
7959         }
7960
7961         /* execution list creation*/
7962         l_j2k->m_procedure_list = opj_procedure_list_create();
7963         if (! l_j2k->m_procedure_list) {
7964                 j2k_destroy(l_j2k);
7965                 return NULL;
7966         }
7967
7968         return l_j2k;
7969 }
7970
7971 void j2k_destroy_compress(opj_j2k_t *j2k) {
7972         int tileno;
7973
7974         if(!j2k) return;
7975         if(j2k->cp != NULL) {
7976                 opj_cp_t *cp = j2k->cp;
7977
7978                 if(cp->comment) {
7979                         opj_free(cp->comment);
7980                 }
7981                 if(cp->matrice) {
7982                         opj_free(cp->matrice);
7983                 }
7984                 for (tileno = 0; tileno < cp->tw * cp->th; tileno++) {
7985                         opj_free(cp->tcps[tileno].tccps);
7986                 }
7987                 opj_free(cp->tcps);
7988                 opj_free(cp);
7989         }
7990
7991         opj_free(j2k);
7992 }
7993
7994 void j2k_setup_encoder(opj_j2k_t *j2k, opj_cparameters_t *parameters, opj_image_t *image) {
7995         OPJ_UINT32 i,j;
7996   int tileno, numpocs_tile;
7997         opj_cp_t *cp = NULL;
7998
7999         if(!j2k || !parameters || ! image) {
8000                 return;
8001         }
8002
8003         /* create and initialize the coding parameters structure */
8004         cp = (opj_cp_t*) opj_calloc(1, sizeof(opj_cp_t));
8005
8006         /* keep a link to cp so that we can destroy it later in j2k_destroy_compress */
8007         j2k->cp = cp;
8008
8009         /* set default values for cp */
8010         cp->tw = 1;
8011         cp->th = 1;
8012
8013         /* 
8014         copy user encoding parameters 
8015         */
8016         cp->cinema = parameters->cp_cinema;
8017         cp->max_comp_size =     parameters->max_comp_size;
8018         cp->rsiz   = parameters->cp_rsiz;
8019         cp->disto_alloc = parameters->cp_disto_alloc;
8020         cp->fixed_alloc = parameters->cp_fixed_alloc;
8021         cp->fixed_quality = parameters->cp_fixed_quality;
8022
8023         /* mod fixed_quality */
8024         if(parameters->cp_matrice) {
8025                 size_t array_size = parameters->tcp_numlayers * parameters->numresolution * 3 * sizeof(int);
8026                 cp->matrice = (int *) opj_malloc(array_size);
8027                 memcpy(cp->matrice, parameters->cp_matrice, array_size);
8028         }
8029
8030         /* tiles */
8031         cp->tdx = parameters->cp_tdx;
8032         cp->tdy = parameters->cp_tdy;
8033
8034         /* tile offset */
8035         cp->tx0 = parameters->cp_tx0;
8036         cp->ty0 = parameters->cp_ty0;
8037
8038         /* comment string */
8039         if(parameters->cp_comment) {
8040                 cp->comment = (char*)opj_malloc(strlen(parameters->cp_comment) + 1);
8041                 if(cp->comment) {
8042                         strcpy(cp->comment, parameters->cp_comment);
8043                 }
8044         }
8045
8046         /*
8047         calculate other encoding parameters
8048         */
8049
8050         if (parameters->tile_size_on) {
8051                 cp->tw = int_ceildiv(image->x1 - cp->tx0, cp->tdx);
8052                 cp->th = int_ceildiv(image->y1 - cp->ty0, cp->tdy);
8053         } else {
8054                 cp->tdx = image->x1 - cp->tx0;
8055                 cp->tdy = image->y1 - cp->ty0;
8056         }
8057
8058         if(parameters->tp_on){
8059                 cp->tp_flag = parameters->tp_flag;
8060                 cp->tp_on = 1;
8061         }
8062         
8063         cp->img_size = 0;
8064         for(i=0;i<image->numcomps ;i++){
8065         cp->img_size += (image->comps[i].w *image->comps[i].h * image->comps[i].prec);
8066         }
8067
8068
8069 #ifdef USE_JPWL
8070         /*
8071         calculate JPWL encoding parameters
8072         */
8073
8074         if (parameters->jpwl_epc_on) {
8075                 OPJ_UINT32 i;
8076
8077                 /* set JPWL on */
8078                 cp->epc_on = OPJ_TRUE;
8079                 cp->info_on = OPJ_FALSE; /* no informative technique */
8080
8081                 /* set EPB on */
8082                 if ((parameters->jpwl_hprot_MH > 0) || (parameters->jpwl_hprot_TPH[0] > 0)) {
8083                         cp->epb_on = OPJ_TRUE;
8084                         
8085                         cp->hprot_MH = parameters->jpwl_hprot_MH;
8086                         for (i = 0; i < JPWL_MAX_NO_TILESPECS; i++) {
8087                                 cp->hprot_TPH_tileno[i] = parameters->jpwl_hprot_TPH_tileno[i];
8088                                 cp->hprot_TPH[i] = parameters->jpwl_hprot_TPH[i];
8089                         }
8090                         /* if tile specs are not specified, copy MH specs */
8091                         if (cp->hprot_TPH[0] == -1) {
8092                                 cp->hprot_TPH_tileno[0] = 0;
8093                                 cp->hprot_TPH[0] = parameters->jpwl_hprot_MH;
8094                         }
8095                         for (i = 0; i < JPWL_MAX_NO_PACKSPECS; i++) {
8096                                 cp->pprot_tileno[i] = parameters->jpwl_pprot_tileno[i];
8097                                 cp->pprot_packno[i] = parameters->jpwl_pprot_packno[i];
8098                                 cp->pprot[i] = parameters->jpwl_pprot[i];
8099                         }
8100                 }
8101
8102                 /* set ESD writing */
8103                 if ((parameters->jpwl_sens_size == 1) || (parameters->jpwl_sens_size == 2)) {
8104                         cp->esd_on = OPJ_TRUE;
8105
8106                         cp->sens_size = parameters->jpwl_sens_size;
8107                         cp->sens_addr = parameters->jpwl_sens_addr;
8108                         cp->sens_range = parameters->jpwl_sens_range;
8109
8110                         cp->sens_MH = parameters->jpwl_sens_MH;
8111                         for (i = 0; i < JPWL_MAX_NO_TILESPECS; i++) {
8112                                 cp->sens_TPH_tileno[i] = parameters->jpwl_sens_TPH_tileno[i];
8113                                 cp->sens_TPH[i] = parameters->jpwl_sens_TPH[i];
8114                         }
8115                 }
8116
8117                 /* always set RED writing to false: we are at the encoder */
8118                 cp->red_on = OPJ_FALSE;
8119
8120         } else {
8121                 cp->epc_on = OPJ_FALSE;
8122         }
8123 #endif /* USE_JPWL */
8124
8125
8126         /* initialize the mutiple tiles */
8127         /* ---------------------------- */
8128         cp->tcps = (opj_tcp_t*) opj_calloc(cp->tw * cp->th, sizeof(opj_tcp_t));
8129
8130         for (tileno = 0; tileno < cp->tw * cp->th; tileno++) {
8131                 opj_tcp_t *tcp = &cp->tcps[tileno];
8132                 tcp->numlayers = parameters->tcp_numlayers;
8133                 assert ( tcp->numlayers >= 0 );
8134                 for (j = 0; j < (OPJ_UINT32)tcp->numlayers; j++) {
8135                         if(cp->cinema){
8136                                 if (cp->fixed_quality) {
8137                                         tcp->distoratio[j] = parameters->tcp_distoratio[j];
8138                                 }
8139                                 tcp->rates[j] = parameters->tcp_rates[j];
8140                         }else{
8141                                 if (cp->fixed_quality) {        /* add fixed_quality */
8142                                         tcp->distoratio[j] = parameters->tcp_distoratio[j];
8143                                 } else {
8144                                         tcp->rates[j] = parameters->tcp_rates[j];
8145                                 }
8146                         }
8147                 }
8148                 tcp->csty = parameters->csty;
8149                 tcp->prg = parameters->prog_order;
8150                 tcp->mct = parameters->tcp_mct; 
8151
8152                 numpocs_tile = 0;
8153                 tcp->POC = 0;
8154                 if (parameters->numpocs) {
8155                         /* initialisation of POC */
8156                         tcp->POC = 1;
8157       assert( parameters->numpocs >= 0 );
8158                         for (i = 0; i < (OPJ_UINT32)parameters->numpocs; i++) {
8159                                 if((tileno == parameters->POC[i].tile - 1) || (parameters->POC[i].tile == -1)) {
8160                                         opj_poc_t *tcp_poc = &tcp->pocs[numpocs_tile];
8161                                         tcp_poc->resno0         = parameters->POC[numpocs_tile].resno0;
8162                                         tcp_poc->compno0        = parameters->POC[numpocs_tile].compno0;
8163                                         tcp_poc->layno1         = parameters->POC[numpocs_tile].layno1;
8164                                         tcp_poc->resno1         = parameters->POC[numpocs_tile].resno1;
8165                                         tcp_poc->compno1        = parameters->POC[numpocs_tile].compno1;
8166                                         tcp_poc->prg1           = parameters->POC[numpocs_tile].prg1;
8167                                         tcp_poc->tile           = parameters->POC[numpocs_tile].tile;
8168                                         numpocs_tile++;
8169                                 }
8170                         }
8171                         tcp->numpocs = numpocs_tile -1 ;
8172                 }else{ 
8173                         tcp->numpocs = 0;
8174                 }
8175
8176                 tcp->tccps = (opj_tccp_t*) opj_calloc(image->numcomps, sizeof(opj_tccp_t));
8177
8178                 for (i = 0; i < image->numcomps; i++) {
8179                         opj_tccp_t *tccp = &tcp->tccps[i];
8180                         tccp->csty = parameters->csty & 0x01;   /* 0 => one precinct || 1 => custom precinct  */
8181                         tccp->numresolutions = parameters->numresolution;
8182                         tccp->cblkw = int_floorlog2(parameters->cblockw_init);
8183                         tccp->cblkh = int_floorlog2(parameters->cblockh_init);
8184                         tccp->cblksty = parameters->mode;
8185                         tccp->qmfbid = parameters->irreversible ? 0 : 1;
8186                         tccp->qntsty = parameters->irreversible ? J2K_CCP_QNTSTY_SEQNT : J2K_CCP_QNTSTY_NOQNT;
8187                         tccp->numgbits = 2;
8188                         assert(parameters->roi_compno >= 0);
8189                         if (i == (OPJ_UINT32)parameters->roi_compno) {
8190                                 tccp->roishift = parameters->roi_shift;
8191                         } else {
8192                                 tccp->roishift = 0;
8193                         }
8194
8195                         if(parameters->cp_cinema)
8196                         {
8197                                 /*Precinct size for lowest frequency subband=128*/
8198                                 tccp->prcw[0] = 7;
8199                                 tccp->prch[0] = 7;
8200                                 /*Precinct size at all other resolutions = 256*/
8201                                 for (j = 1; j < tccp->numresolutions; j++) {
8202                                         tccp->prcw[j] = 8;
8203                                         tccp->prch[j] = 8;
8204                                 }
8205                         }else{
8206                                 if (parameters->csty & J2K_CCP_CSTY_PRT) {
8207                                         int p = 0;
8208                                         assert(tccp->numresolutions > 0);
8209                                         for (j = (OPJ_UINT32)(tccp->numresolutions - 1); (int)j >= 0; j--) {
8210                                                 if (p < parameters->res_spec) {
8211                                                         
8212                                                         if (parameters->prcw_init[p] < 1) {
8213                                                                 tccp->prcw[j] = 1;
8214                                                         } else {
8215                                                                 tccp->prcw[j] = int_floorlog2(parameters->prcw_init[p]);
8216                                                         }
8217                                                         
8218                                                         if (parameters->prch_init[p] < 1) {
8219                                                                 tccp->prch[j] = 1;
8220                                                         }else {
8221                                                                 tccp->prch[j] = int_floorlog2(parameters->prch_init[p]);
8222                                                         }
8223
8224                                                 } else {
8225                                                         int res_spec = parameters->res_spec;
8226                                                         int size_prcw = parameters->prcw_init[res_spec - 1] >> (p - (res_spec - 1));
8227                                                         int size_prch = parameters->prch_init[res_spec - 1] >> (p - (res_spec - 1));
8228                                                         
8229                                                         if (size_prcw < 1) {
8230                                                                 tccp->prcw[j] = 1;
8231                                                         } else {
8232                                                                 tccp->prcw[j] = int_floorlog2(size_prcw);
8233                                                         }
8234                                                         
8235                                                         if (size_prch < 1) {
8236                                                                 tccp->prch[j] = 1;
8237                                                         } else {
8238                                                                 tccp->prch[j] = int_floorlog2(size_prch);
8239                                                         }
8240                                                 }
8241                                                 p++;
8242                                                 /*printf("\nsize precinct for level %d : %d,%d\n", j,tccp->prcw[j], tccp->prch[j]); */
8243                                         }       /*end for*/
8244                                 } else {
8245                                         for (j = 0; j < tccp->numresolutions; j++) {
8246                                                 tccp->prcw[j] = 15;
8247                                                 tccp->prch[j] = 15;
8248                                         }
8249                                 }
8250                         }
8251
8252                         dwt_calc_explicit_stepsizes(tccp, image->comps[i].prec);
8253                 }
8254         }
8255 }
8256
8257 void j2k_setup_encoder_v2(      opj_j2k_v2_t *p_j2k,
8258                                                         opj_cparameters_t *parameters,
8259                                                         opj_image_t *image,
8260                                                         struct opj_event_mgr * p_manager)
8261 {
8262         OPJ_UINT32 i, j, tileno, numpocs_tile;
8263         opj_cp_v2_t *cp = 00;
8264         opj_bool l_res;
8265
8266         if(!p_j2k || !parameters || ! image) {
8267                 return;
8268         }
8269
8270         /* keep a link to cp so that we can destroy it later in j2k_destroy_compress */
8271         cp = &(p_j2k->m_cp);
8272
8273         /* set default values for cp */
8274         cp->tw = 1;
8275         cp->th = 1;
8276
8277         /*
8278         copy user encoding parameters
8279         */
8280         cp->m_specific_param.m_enc.m_cinema = parameters->cp_cinema;
8281         cp->m_specific_param.m_enc.m_max_comp_size =    parameters->max_comp_size;
8282         cp->rsiz   = parameters->cp_rsiz;
8283         cp->m_specific_param.m_enc.m_disto_alloc = parameters->cp_disto_alloc;
8284         cp->m_specific_param.m_enc.m_fixed_alloc = parameters->cp_fixed_alloc;
8285         cp->m_specific_param.m_enc.m_fixed_quality = parameters->cp_fixed_quality;
8286
8287         /* mod fixed_quality */
8288         if (parameters->cp_matrice) {
8289                 size_t array_size = parameters->tcp_numlayers * parameters->numresolution * 3 * sizeof(OPJ_INT32);
8290                 cp->m_specific_param.m_enc.m_matrice = (OPJ_INT32 *) opj_malloc(array_size);
8291                 memcpy(cp->m_specific_param.m_enc.m_matrice, parameters->cp_matrice, array_size);
8292         }
8293
8294         /* tiles */
8295         cp->tdx = parameters->cp_tdx;
8296         cp->tdy = parameters->cp_tdy;
8297
8298         /* tile offset */
8299         cp->tx0 = parameters->cp_tx0;
8300         cp->ty0 = parameters->cp_ty0;
8301
8302         /* comment string */
8303         if(parameters->cp_comment) {
8304                 cp->comment = (char*)opj_malloc(strlen(parameters->cp_comment) + 1);
8305                 if(cp->comment) {
8306                         strcpy(cp->comment, parameters->cp_comment);
8307                 }
8308         }
8309
8310         /*
8311         calculate other encoding parameters
8312         */
8313
8314         if (parameters->tile_size_on) {
8315                 cp->tw = int_ceildiv(image->x1 - cp->tx0, cp->tdx);
8316                 cp->th = int_ceildiv(image->y1 - cp->ty0, cp->tdy);
8317         } else {
8318                 cp->tdx = image->x1 - cp->tx0;
8319                 cp->tdy = image->y1 - cp->ty0;
8320         }
8321
8322         if (parameters->tp_on) {
8323                 cp->m_specific_param.m_enc.m_tp_flag = parameters->tp_flag;
8324                 cp->m_specific_param.m_enc.m_tp_on = 1;
8325         }
8326
8327 #ifdef USE_JPWL
8328         /*
8329         calculate JPWL encoding parameters
8330         */
8331
8332         if (parameters->jpwl_epc_on) {
8333                 OPJ_INT32 i;
8334
8335                 /* set JPWL on */
8336                 cp->epc_on = OPJ_TRUE;
8337                 cp->info_on = OPJ_FALSE; /* no informative technique */
8338
8339                 /* set EPB on */
8340                 if ((parameters->jpwl_hprot_MH > 0) || (parameters->jpwl_hprot_TPH[0] > 0)) {
8341                         cp->epb_on = OPJ_TRUE;
8342
8343                         cp->hprot_MH = parameters->jpwl_hprot_MH;
8344                         for (i = 0; i < JPWL_MAX_NO_TILESPECS; i++) {
8345                                 cp->hprot_TPH_tileno[i] = parameters->jpwl_hprot_TPH_tileno[i];
8346                                 cp->hprot_TPH[i] = parameters->jpwl_hprot_TPH[i];
8347                         }
8348                         /* if tile specs are not specified, copy MH specs */
8349                         if (cp->hprot_TPH[0] == -1) {
8350                                 cp->hprot_TPH_tileno[0] = 0;
8351                                 cp->hprot_TPH[0] = parameters->jpwl_hprot_MH;
8352                         }
8353                         for (i = 0; i < JPWL_MAX_NO_PACKSPECS; i++) {
8354                                 cp->pprot_tileno[i] = parameters->jpwl_pprot_tileno[i];
8355                                 cp->pprot_packno[i] = parameters->jpwl_pprot_packno[i];
8356                                 cp->pprot[i] = parameters->jpwl_pprot[i];
8357                         }
8358                 }
8359
8360                 /* set ESD writing */
8361                 if ((parameters->jpwl_sens_size == 1) || (parameters->jpwl_sens_size == 2)) {
8362                         cp->esd_on = OPJ_TRUE;
8363
8364                         cp->sens_size = parameters->jpwl_sens_size;
8365                         cp->sens_addr = parameters->jpwl_sens_addr;
8366                         cp->sens_range = parameters->jpwl_sens_range;
8367
8368                         cp->sens_MH = parameters->jpwl_sens_MH;
8369                         for (i = 0; i < JPWL_MAX_NO_TILESPECS; i++) {
8370                                 cp->sens_TPH_tileno[i] = parameters->jpwl_sens_TPH_tileno[i];
8371                                 cp->sens_TPH[i] = parameters->jpwl_sens_TPH[i];
8372                         }
8373                 }
8374
8375                 /* always set RED writing to false: we are at the encoder */
8376                 cp->red_on = OPJ_FALSE;
8377
8378         } else {
8379                 cp->epc_on = OPJ_FALSE;
8380         }
8381 #endif /* USE_JPWL */
8382
8383
8384         /* initialize the mutiple tiles */
8385         /* ---------------------------- */
8386         cp->tcps = (opj_tcp_v2_t*) opj_calloc(cp->tw * cp->th, sizeof(opj_tcp_v2_t));
8387         if (parameters->numpocs) {
8388                 /* initialisation of POC */
8389                 l_res = j2k_check_poc_val(parameters->POC,parameters->numpocs, parameters->numresolution, image->numcomps, parameters->tcp_numlayers, p_manager);
8390                 // TODO
8391         }
8392
8393         for (tileno = 0; tileno < cp->tw * cp->th; tileno++) {
8394                 opj_tcp_v2_t *tcp = &cp->tcps[tileno];
8395                 tcp->numlayers = parameters->tcp_numlayers;
8396
8397                 for (j = 0; j < tcp->numlayers; j++) {
8398                         if(cp->m_specific_param.m_enc.m_cinema){
8399                                 if (cp->m_specific_param.m_enc.m_fixed_quality) {
8400                                         tcp->distoratio[j] = parameters->tcp_distoratio[j];
8401                                 }
8402                                 tcp->rates[j] = parameters->tcp_rates[j];
8403                         }else{
8404                                 if (cp->m_specific_param.m_enc.m_fixed_quality) {       /* add fixed_quality */
8405                                         tcp->distoratio[j] = parameters->tcp_distoratio[j];
8406                                 } else {
8407                                         tcp->rates[j] = parameters->tcp_rates[j];
8408                                 }
8409                         }
8410                 }
8411
8412                 tcp->csty = parameters->csty;
8413                 tcp->prg = parameters->prog_order;
8414                 tcp->mct = parameters->tcp_mct;
8415
8416                 numpocs_tile = 0;
8417                 tcp->POC = 0;
8418
8419                 if (parameters->numpocs) {
8420                         /* initialisation of POC */
8421                         tcp->POC = 1;
8422                         // TODO
8423                         for (i = 0; i < (unsigned int) parameters->numpocs; i++) {
8424                                 if((tileno == parameters->POC[i].tile - 1) || (parameters->POC[i].tile == -1)) {
8425                                         opj_poc_t *tcp_poc = &tcp->pocs[numpocs_tile];
8426
8427                                         tcp_poc->resno0         = parameters->POC[numpocs_tile].resno0;
8428                                         tcp_poc->compno0        = parameters->POC[numpocs_tile].compno0;
8429                                         tcp_poc->layno1         = parameters->POC[numpocs_tile].layno1;
8430                                         tcp_poc->resno1         = parameters->POC[numpocs_tile].resno1;
8431                                         tcp_poc->compno1        = parameters->POC[numpocs_tile].compno1;
8432                                         tcp_poc->prg1           = parameters->POC[numpocs_tile].prg1;
8433                                         tcp_poc->tile           = parameters->POC[numpocs_tile].tile;
8434
8435                                         numpocs_tile++;
8436                                 }
8437                         }
8438
8439                         tcp->numpocs = numpocs_tile -1 ;
8440                 }else{
8441                         tcp->numpocs = 0;
8442                 }
8443
8444                 tcp->tccps = (opj_tccp_t*) opj_calloc(image->numcomps, sizeof(opj_tccp_t));
8445
8446                 if (parameters->mct_data) {
8447
8448                         opj_event_msg_v2(p_manager, EVT_ERROR, "MCT not supported for now\n");
8449                         return;
8450
8451                         /* TODO MSD : merge v2 add invert.c or used a external lib ?
8452                         OPJ_UINT32 lMctSize = image->numcomps * image->numcomps * sizeof(OPJ_FLOAT32);
8453                         OPJ_FLOAT32 * lTmpBuf = (OPJ_FLOAT32*)opj_malloc(lMctSize);
8454                         OPJ_INT32 * l_dc_shift = (OPJ_INT32 *) ((OPJ_BYTE *) parameters->mct_data + lMctSize);
8455
8456                         tcp->mct = 2;
8457                         tcp->m_mct_coding_matrix = (OPJ_FLOAT32*)opj_malloc(lMctSize);
8458                         memcpy(tcp->m_mct_coding_matrix,parameters->mct_data,lMctSize);
8459                         memcpy(lTmpBuf,parameters->mct_data,lMctSize);
8460
8461                         tcp->m_mct_decoding_matrix = (OPJ_FLOAT32*)opj_malloc(lMctSize);
8462                         assert(opj_matrix_inversion_f(lTmpBuf,(tcp->m_mct_decoding_matrix),image->numcomps));
8463
8464                         tcp->mct_norms = (OPJ_FLOAT64*)
8465                                         opj_malloc(image->numcomps * sizeof(OPJ_FLOAT64));
8466
8467                         opj_calculate_norms(tcp->mct_norms,image->numcomps,tcp->m_mct_decoding_matrix);
8468                         opj_free(lTmpBuf);
8469
8470                         for (i = 0; i < image->numcomps; i++) {
8471                                 opj_tccp_t *tccp = &tcp->tccps[i];
8472                                 tccp->m_dc_level_shift = l_dc_shift[i];
8473                         }
8474
8475                         j2k_setup_mct_encoding(tcp,image);
8476                         */
8477                 }
8478                 else {
8479                         for (i = 0; i < image->numcomps; i++) {
8480                                 opj_tccp_t *tccp = &tcp->tccps[i];
8481                                 opj_image_comp_t * l_comp = &(image->comps[i]);
8482
8483                                 if (! l_comp->sgnd) {
8484                                         tccp->m_dc_level_shift = 1 << (l_comp->prec - 1);
8485                                 }
8486                         }
8487                 }
8488
8489                 for (i = 0; i < image->numcomps; i++) {
8490                         opj_tccp_t *tccp = &tcp->tccps[i];
8491
8492                         tccp->csty = parameters->csty & 0x01;   /* 0 => one precinct || 1 => custom precinct  */
8493                         tccp->numresolutions = parameters->numresolution;
8494                         tccp->cblkw = int_floorlog2(parameters->cblockw_init);
8495                         tccp->cblkh = int_floorlog2(parameters->cblockh_init);
8496                         tccp->cblksty = parameters->mode;
8497                         tccp->qmfbid = parameters->irreversible ? 0 : 1;
8498                         tccp->qntsty = parameters->irreversible ? J2K_CCP_QNTSTY_SEQNT : J2K_CCP_QNTSTY_NOQNT;
8499                         tccp->numgbits = 2;
8500
8501                         if (i == parameters->roi_compno) {
8502                                 tccp->roishift = parameters->roi_shift;
8503                         } else {
8504                                 tccp->roishift = 0;
8505                         }
8506
8507                         if(parameters->cp_cinema) {
8508                                 //Precinct size for lowest frequency subband=128
8509                                 tccp->prcw[0] = 7;
8510                                 tccp->prch[0] = 7;
8511                                 //Precinct size at all other resolutions = 256
8512                                 for (j = 1; j < tccp->numresolutions; j++) {
8513                                         tccp->prcw[j] = 8;
8514                                         tccp->prch[j] = 8;
8515                                 }
8516                         }else{
8517                                 if (parameters->csty & J2K_CCP_CSTY_PRT) {
8518                                         OPJ_INT32 p = 0, it_res;
8519                                         for (it_res = tccp->numresolutions - 1; it_res >= 0; it_res--) {
8520                                                 if (p < parameters->res_spec) {
8521
8522                                                         if (parameters->prcw_init[p] < 1) {
8523                                                                 tccp->prcw[it_res] = 1;
8524                                                         } else {
8525                                                                 tccp->prcw[it_res] = int_floorlog2(parameters->prcw_init[p]);
8526                                                         }
8527
8528                                                         if (parameters->prch_init[p] < 1) {
8529                                                                 tccp->prch[it_res] = 1;
8530                                                         }else {
8531                                                                 tccp->prch[it_res] = int_floorlog2(parameters->prch_init[p]);
8532                                                         }
8533
8534                                                 } else {
8535                                                         int res_spec = parameters->res_spec;
8536                                                         int size_prcw = parameters->prcw_init[res_spec - 1] >> (p - (res_spec - 1));
8537                                                         int size_prch = parameters->prch_init[res_spec - 1] >> (p - (res_spec - 1));
8538
8539                                                         if (size_prcw < 1) {
8540                                                                 tccp->prcw[it_res] = 1;
8541                                                         } else {
8542                                                                 tccp->prcw[it_res] = int_floorlog2(size_prcw);
8543                                                         }
8544
8545                                                         if (size_prch < 1) {
8546                                                                 tccp->prch[it_res] = 1;
8547                                                         } else {
8548                                                                 tccp->prch[it_res] = int_floorlog2(size_prch);
8549                                                         }
8550                                                 }
8551                                                 p++;
8552                                                 /*printf("\nsize precinct for level %d : %d,%d\n", it_res,tccp->prcw[it_res], tccp->prch[it_res]); */
8553                                         }       //end for
8554                                 } else {
8555                                         for (j = 0; j < tccp->numresolutions; j++) {
8556                                                 tccp->prcw[j] = 15;
8557                                                 tccp->prch[j] = 15;
8558                                         }
8559                                 }
8560                         }
8561
8562                         dwt_calc_explicit_stepsizes(tccp, image->comps[i].prec);
8563                 }
8564         }
8565
8566         if (parameters->mct_data) {
8567                 opj_free(parameters->mct_data);
8568                 parameters->mct_data = 00;
8569         }
8570 }
8571
8572
8573 opj_bool j2k_encode(opj_j2k_t *j2k, opj_cio_t *cio, opj_image_t *image, opj_codestream_info_t *cstr_info) {
8574         int tileno;
8575   OPJ_UINT32 compno;
8576         opj_cp_t *cp = NULL;
8577
8578         opj_tcd_t *tcd = NULL;  /* TCD component */
8579
8580         j2k->cio = cio; 
8581         j2k->image = image;
8582
8583         cp = j2k->cp;
8584
8585         /* INDEX >> */
8586         j2k->cstr_info = cstr_info;
8587         if (cstr_info) {
8588                 OPJ_UINT32 compno;
8589                 cstr_info->tile = (opj_tile_info_t *) opj_malloc(cp->tw * cp->th * sizeof(opj_tile_info_t));
8590                 cstr_info->image_w = image->x1 - image->x0;
8591                 cstr_info->image_h = image->y1 - image->y0;
8592                 cstr_info->prog = (&cp->tcps[0])->prg;
8593                 cstr_info->tw = cp->tw;
8594                 cstr_info->th = cp->th;
8595                 cstr_info->tile_x = cp->tdx;    /* new version parser */
8596                 cstr_info->tile_y = cp->tdy;    /* new version parser */
8597                 cstr_info->tile_Ox = cp->tx0;   /* new version parser */
8598                 cstr_info->tile_Oy = cp->ty0;   /* new version parser */
8599                 cstr_info->numcomps = image->numcomps;
8600                 cstr_info->numlayers = (&cp->tcps[0])->numlayers;
8601                 cstr_info->numdecompos = (int*) opj_malloc(image->numcomps * sizeof(int));
8602                 for (compno=0; compno < image->numcomps; compno++) {
8603                         cstr_info->numdecompos[compno] = (&cp->tcps[0])->tccps->numresolutions - 1;
8604                 }
8605                 cstr_info->D_max = 0.0;         /* ADD Marcela */
8606                 cstr_info->main_head_start = cio_tell(cio); /* position of SOC */
8607                 cstr_info->maxmarknum = 100;
8608                 cstr_info->marker = (opj_marker_info_t *) opj_malloc(cstr_info->maxmarknum * sizeof(opj_marker_info_t));
8609                 cstr_info->marknum = 0;
8610         }
8611         /* << INDEX */
8612
8613         j2k_write_soc(j2k);
8614         j2k_write_siz(j2k);
8615         j2k_write_cod(j2k);
8616         j2k_write_qcd(j2k);
8617
8618         if(cp->cinema){
8619                 for (compno = 1; compno < image->numcomps; compno++) {
8620                         j2k_write_coc(j2k, compno);
8621                         j2k_write_qcc(j2k, compno);
8622                 }
8623         }
8624
8625         for (compno = 0; compno < image->numcomps; compno++) {
8626                 opj_tcp_t *tcp = &cp->tcps[0];
8627                 if (tcp->tccps[compno].roishift)
8628                         j2k_write_rgn(j2k, compno, 0);
8629         }
8630         if (cp->comment != NULL) {
8631                 j2k_write_com(j2k);
8632         }
8633
8634         j2k->totnum_tp = j2k_calculate_tp(cp,image->numcomps,image,j2k);
8635         /* TLM Marker*/
8636         if(cp->cinema){
8637                 j2k_write_tlm(j2k);
8638                 if (cp->cinema == CINEMA4K_24) {
8639                         j2k_write_poc(j2k);
8640                 }
8641         }
8642
8643         /* uncomment only for testing JPSEC marker writing */
8644         /* j2k_write_sec(j2k); */
8645
8646         /* INDEX >> */
8647         if(cstr_info) {
8648                 cstr_info->main_head_end = cio_tell(cio) - 1;
8649         }
8650         /* << INDEX */
8651         /**** Main Header ENDS here ***/
8652
8653         /* create the tile encoder */
8654         tcd = tcd_create(j2k->cinfo);
8655
8656         /* encode each tile */
8657         for (tileno = 0; tileno < cp->tw * cp->th; tileno++) {
8658                 int pino;
8659                 int tilepartno=0;
8660                 /* UniPG>> */
8661                 int acc_pack_num = 0;
8662                 /* <<UniPG */
8663
8664
8665                 opj_tcp_t *tcp = &cp->tcps[tileno];
8666                 opj_event_msg(j2k->cinfo, EVT_INFO, "tile number %d / %d\n", tileno + 1, cp->tw * cp->th);
8667
8668                 j2k->curtileno = tileno;
8669                 j2k->cur_tp_num = 0;
8670                 tcd->cur_totnum_tp = j2k->cur_totnum_tp[j2k->curtileno];
8671                 /* initialisation before tile encoding  */
8672                 if (tileno == 0) {
8673                         tcd_malloc_encode(tcd, image, cp, j2k->curtileno);
8674                 } else {
8675                         tcd_init_encode(tcd, image, cp, j2k->curtileno);
8676                 }
8677
8678                 /* INDEX >> */
8679                 if(cstr_info) {
8680                         cstr_info->tile[j2k->curtileno].start_pos = cio_tell(cio) + j2k->pos_correction;
8681                         cstr_info->tile[j2k->curtileno].maxmarknum = 10;
8682                         cstr_info->tile[j2k->curtileno].marker = (opj_marker_info_t *) opj_malloc(cstr_info->tile[j2k->curtileno].maxmarknum * sizeof(opj_marker_info_t));
8683                         cstr_info->tile[j2k->curtileno].marknum = 0;
8684                 }
8685                 /* << INDEX */
8686
8687                 for(pino = 0; pino <= tcp->numpocs; pino++) {
8688                         int tot_num_tp;
8689                         tcd->cur_pino=pino;
8690
8691                         /*Get number of tile parts*/
8692                         tot_num_tp = j2k_get_num_tp(cp,pino,tileno);
8693                         tcd->tp_pos = cp->tp_pos;
8694
8695                         for(tilepartno = 0; tilepartno < tot_num_tp ; tilepartno++){
8696                                 j2k->tp_num = tilepartno;
8697                                 /* INDEX >> */
8698                                 if(cstr_info)
8699                                         cstr_info->tile[j2k->curtileno].tp[j2k->cur_tp_num].tp_start_pos =
8700                                         cio_tell(cio) + j2k->pos_correction;
8701                                 /* << INDEX */
8702                                 j2k_write_sot(j2k);
8703
8704                                 if(j2k->cur_tp_num == 0 && cp->cinema == 0){
8705                                         for (compno = 1; compno < image->numcomps; compno++) {
8706                                                 j2k_write_coc(j2k, compno);
8707                                                 j2k_write_qcc(j2k, compno);
8708                                         }
8709                                         if (cp->tcps[tileno].numpocs) {
8710                                                 j2k_write_poc(j2k);
8711                                         }
8712                                 }
8713
8714                                 /* INDEX >> */
8715                                 if(cstr_info)
8716                                         cstr_info->tile[j2k->curtileno].tp[j2k->cur_tp_num].tp_end_header =
8717                                         cio_tell(cio) + j2k->pos_correction + 1;
8718                                 /* << INDEX */
8719
8720                                 j2k_write_sod(j2k, tcd);
8721
8722                                 /* INDEX >> */
8723                                 if(cstr_info) {
8724                                         cstr_info->tile[j2k->curtileno].tp[j2k->cur_tp_num].tp_end_pos =
8725                                                 cio_tell(cio) + j2k->pos_correction - 1;
8726                                         cstr_info->tile[j2k->curtileno].tp[j2k->cur_tp_num].tp_start_pack =
8727                                                 acc_pack_num;
8728                                         cstr_info->tile[j2k->curtileno].tp[j2k->cur_tp_num].tp_numpacks =
8729                                                 cstr_info->packno - acc_pack_num;
8730                                         acc_pack_num = cstr_info->packno;
8731                                 }
8732                                 /* << INDEX */
8733
8734                                 j2k->cur_tp_num++;
8735                         }                       
8736                 }
8737                 if(cstr_info) {
8738                         cstr_info->tile[j2k->curtileno].end_pos = cio_tell(cio) + j2k->pos_correction - 1;
8739                 }
8740
8741
8742                 /*
8743                 if (tile->PPT) { // BAD PPT !!! 
8744                 FILE *PPT_file;
8745                 int i;
8746                 PPT_file=fopen("PPT","rb");
8747                 fprintf(stderr,"%c%c%c%c",255,97,tile->len_ppt/256,tile->len_ppt%256);
8748                 for (i=0;i<tile->len_ppt;i++) {
8749                 unsigned char elmt;
8750                 fread(&elmt, 1, 1, PPT_file);
8751                 fwrite(&elmt,1,1,f);
8752                 }
8753                 fclose(PPT_file);
8754                 unlink("PPT");
8755                 }
8756                 */
8757
8758         }
8759
8760         /* destroy the tile encoder */
8761         tcd_free_encode(tcd);
8762         tcd_destroy(tcd);
8763
8764         opj_free(j2k->cur_totnum_tp);
8765
8766         j2k_write_eoc(j2k);
8767
8768         if(cstr_info) {
8769                 cstr_info->codestream_size = cio_tell(cio) + j2k->pos_correction;
8770                 /* UniPG>> */
8771                 /* The following adjustment is done to adjust the codestream size */
8772                 /* if SOD is not at 0 in the buffer. Useful in case of JP2, where */
8773                 /* the first bunch of bytes is not in the codestream              */
8774                 cstr_info->codestream_size -= cstr_info->main_head_start;
8775                 /* <<UniPG */
8776         }
8777
8778 #ifdef USE_JPWL
8779         /*
8780         preparation of JPWL marker segments
8781         */
8782         if(cp->epc_on) {
8783
8784                 /* encode according to JPWL */
8785                 jpwl_encode(j2k, cio, image);
8786
8787         }
8788 #endif /* USE_JPWL */
8789
8790         return OPJ_TRUE;
8791 }
8792
8793 static void j2k_add_mhmarker(opj_codestream_info_t *cstr_info, unsigned short int type, int pos, int len)
8794 {
8795         assert(cstr_info != 00);
8796
8797         /* expand the list? */
8798         if ((cstr_info->marknum + 1) > cstr_info->maxmarknum) {
8799                 cstr_info->maxmarknum = 100 + (int) ((float) cstr_info->maxmarknum * 1.0F);
8800                 cstr_info->marker = (opj_marker_info_t*)opj_realloc(cstr_info->marker, cstr_info->maxmarknum);
8801         }
8802
8803         /* add the marker */
8804         cstr_info->marker[cstr_info->marknum].type = type;
8805         cstr_info->marker[cstr_info->marknum].pos = pos;
8806         cstr_info->marker[cstr_info->marknum].len = len;
8807         cstr_info->marknum++;
8808
8809 }
8810
8811 static void j2k_add_mhmarker_v2(opj_codestream_index_t *cstr_index, OPJ_UINT32 type, OPJ_OFF_T pos, OPJ_UINT32 len)
8812 {
8813         assert(cstr_index != 00);
8814
8815         /* expand the list? */
8816         if ((cstr_index->marknum + 1) > cstr_index->maxmarknum) {
8817                 cstr_index->maxmarknum = 100 + (int) ((float) cstr_index->maxmarknum * 1.0F);
8818                 cstr_index->marker = (opj_marker_info_t*)opj_realloc(cstr_index->marker, cstr_index->maxmarknum *sizeof(opj_marker_info_t));
8819         }
8820
8821         /* add the marker */
8822         cstr_index->marker[cstr_index->marknum].type = (OPJ_UINT16)type;
8823         cstr_index->marker[cstr_index->marknum].pos = (OPJ_INT32)pos;
8824         cstr_index->marker[cstr_index->marknum].len = (OPJ_INT32)len;
8825         cstr_index->marknum++;
8826
8827 }
8828
8829 static void j2k_add_tlmarker( int tileno, opj_codestream_info_t *cstr_info, unsigned short int type, int pos, int len)
8830 {
8831         opj_marker_info_t *marker;
8832
8833         assert(cstr_info != 00);
8834
8835         /* expand the list? */
8836         if ((cstr_info->tile[tileno].marknum + 1) > cstr_info->tile[tileno].maxmarknum) {
8837                 cstr_info->tile[tileno].maxmarknum = 100 + (int) ((float) cstr_info->tile[tileno].maxmarknum * 1.0F);
8838                 cstr_info->tile[tileno].marker = (opj_marker_info_t*)opj_realloc(cstr_info->tile[tileno].marker, cstr_info->maxmarknum);
8839         }
8840
8841         marker = &(cstr_info->tile[tileno].marker[cstr_info->tile[tileno].marknum]);
8842
8843         /* add the marker */
8844         marker->type = type;
8845         marker->pos = pos;
8846         marker->len = len;
8847         cstr_info->tile[tileno].marknum++;
8848 }
8849
8850 static void j2k_add_tlmarker_v2(OPJ_UINT32 tileno, opj_codestream_index_t *cstr_index, OPJ_UINT32 type, OPJ_OFF_T pos, OPJ_UINT32 len)
8851 {
8852         assert(cstr_index != 00);
8853         assert(cstr_index->tile_index != 00);
8854
8855         /* expand the list? */
8856         if ((cstr_index->tile_index[tileno].marknum + 1) > cstr_index->tile_index[tileno].maxmarknum) {
8857                 cstr_index->tile_index[tileno].maxmarknum = 100 + (int) ((float) cstr_index->tile_index[tileno].maxmarknum * 1.0F);
8858                 cstr_index->tile_index[tileno].marker =
8859                                 (opj_marker_info_t*)opj_realloc(cstr_index->tile_index[tileno].marker,
8860                                                                                                 cstr_index->tile_index[tileno].maxmarknum *sizeof(opj_marker_info_t));
8861         }
8862
8863         /* add the marker */
8864         cstr_index->tile_index[tileno].marker[cstr_index->tile_index[tileno].marknum].type = (OPJ_UINT16)type;
8865         cstr_index->tile_index[tileno].marker[cstr_index->tile_index[tileno].marknum].pos = (OPJ_INT32)pos;
8866         cstr_index->tile_index[tileno].marker[cstr_index->tile_index[tileno].marknum].len = (OPJ_INT32)len;
8867         cstr_index->tile_index[tileno].marknum++;
8868
8869         if (type == J2K_MS_SOT) {
8870                 OPJ_UINT32 l_current_tile_part = cstr_index->tile_index[tileno].current_tpsno;
8871
8872                 if (cstr_index->tile_index[tileno].tp_index)
8873                         cstr_index->tile_index[tileno].tp_index[l_current_tile_part].start_pos = pos;
8874
8875         }
8876 }
8877
8878
8879 /*
8880  * -----------------------------------------------------------------------
8881  * -----------------------------------------------------------------------
8882  * -----------------------------------------------------------------------
8883  */
8884
8885 /**
8886  * Ends the decompression procedures and possibiliy add data to be read after the
8887  * codestream.
8888  */
8889 opj_bool j2k_end_decompress(
8890                                                 opj_j2k_v2_t *p_j2k,
8891                                                 opj_stream_private_t *p_stream,
8892                                                 opj_event_mgr_t * p_manager)
8893 {
8894   (void)p_j2k;
8895   (void)p_stream;
8896   (void)p_manager;
8897         return OPJ_TRUE;
8898 }
8899
8900 /**
8901  * Reads a jpeg2000 codestream header structure.
8902
8903  *
8904  * @param p_stream the stream to read data from.
8905  * @param p_j2k the jpeg2000 codec.
8906  * @param p_manager the user event manager.
8907  *
8908  * @return true if the box is valid.
8909  */
8910 opj_bool j2k_read_header(       struct opj_stream_private *p_stream,
8911                                                         opj_j2k_v2_t* p_j2k,
8912                                                         opj_image_t** p_image,
8913                                                         struct opj_event_mgr* p_manager )
8914 {
8915         /* preconditions */
8916         assert(p_j2k != 00);
8917         assert(p_stream != 00);
8918         assert(p_manager != 00);
8919
8920         /* create an empty image header */
8921         p_j2k->m_private_image = opj_image_create0();
8922         if (! p_j2k->m_private_image) {
8923                 return OPJ_FALSE;
8924         }
8925
8926         /* customization of the validation */
8927         j2k_setup_decoding_validation(p_j2k);
8928
8929         /* validation of the parameters codec */
8930         if (! j2k_exec(p_j2k, p_j2k->m_validation_list, p_stream,p_manager)) {
8931                 opj_image_destroy(p_j2k->m_private_image);
8932                 p_j2k->m_private_image = NULL;
8933                 return OPJ_FALSE;
8934         }
8935
8936         /* customization of the encoding */
8937         j2k_setup_header_reading(p_j2k);
8938
8939         /* read header */
8940         if (! j2k_exec (p_j2k,p_j2k->m_procedure_list,p_stream,p_manager)) {
8941                 opj_image_destroy(p_j2k->m_private_image);
8942                 p_j2k->m_private_image = NULL;
8943                 return OPJ_FALSE;
8944         }
8945
8946         *p_image = opj_image_create0();
8947         if (! (*p_image)) {
8948                 return OPJ_FALSE;
8949         }
8950
8951         /* Copy codestream image information to the output image */
8952         opj_copy_image_header(p_j2k->m_private_image, *p_image);
8953
8954     /*Allocate and initialize some elements of codestrem index*/
8955         if (!j2k_allocate_tile_element_cstr_index(p_j2k)){
8956                 return OPJ_FALSE;
8957         }
8958
8959         return OPJ_TRUE;
8960 }
8961
8962 /**
8963  * Sets up the procedures to do on reading header. Developpers wanting to extend the library can add their own reading procedures.
8964  */
8965 void j2k_setup_header_reading (opj_j2k_v2_t *p_j2k)
8966 {
8967         /* preconditions*/
8968         assert(p_j2k != 00);
8969
8970         opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_read_header_procedure);
8971
8972         /* DEVELOPER CORNER, add your custom procedures */
8973         opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_copy_default_tcp_and_create_tcd);
8974
8975 }
8976
8977 /**
8978  * Sets up the validation ,i.e. adds the procedures to lauch to make sure the codec parameters
8979  * are valid. Developpers wanting to extend the library can add their own validation procedures.
8980  */
8981 void j2k_setup_decoding_validation (opj_j2k_v2_t *p_j2k)
8982 {
8983         /* preconditions*/
8984         assert(p_j2k != 00);
8985
8986         opj_procedure_list_add_procedure(p_j2k->m_validation_list, (opj_procedure)j2k_build_decoder);
8987         opj_procedure_list_add_procedure(p_j2k->m_validation_list, (opj_procedure)j2k_decoding_validation);
8988         /* DEVELOPER CORNER, add your custom validation procedure */
8989
8990 }
8991
8992
8993 /**
8994  * The mct encoding validation procedure.
8995  *
8996  * @param       p_j2k                   the jpeg2000 codec to validate.
8997  * @param       p_stream                                the input stream to validate.
8998  * @param       p_manager               the user event manager.
8999  *
9000  * @return true if the parameters are correct.
9001  */
9002 opj_bool j2k_mct_validation (   opj_j2k_v2_t * p_j2k,
9003                                                                 opj_stream_private_t *p_stream,
9004                                                                 opj_event_mgr_t * p_manager )
9005 {
9006         opj_bool l_is_valid = OPJ_TRUE;
9007         OPJ_UINT32 i,j;
9008
9009         /* preconditions */
9010         assert(p_j2k != 00);
9011         assert(p_stream != 00);
9012         assert(p_manager != 00);
9013
9014         if ((p_j2k->m_cp.rsiz & 0x8200) == 0x8200) {
9015                 OPJ_UINT32 l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw;
9016                 opj_tcp_v2_t * l_tcp = p_j2k->m_cp.tcps;
9017
9018                 for (i=0;i<l_nb_tiles;++i) {
9019                         if (l_tcp->mct == 2) {
9020                                 opj_tccp_t * l_tccp = l_tcp->tccps;
9021                                 l_is_valid &= (l_tcp->m_mct_coding_matrix != 00);
9022
9023                                 for (j=0;j<p_j2k->m_private_image->numcomps;++j) {
9024                                         l_is_valid &= ! (l_tccp->qmfbid & 1);
9025                                         ++l_tccp;
9026                                 }
9027                         }
9028                         ++l_tcp;
9029                 }
9030         }
9031
9032         return l_is_valid;
9033 }
9034
9035 opj_bool j2k_setup_mct_encoding(opj_tcp_v2_t * p_tcp, opj_image_t * p_image)
9036 {
9037         OPJ_UINT32 i;
9038         OPJ_UINT32 l_indix = 1;
9039         opj_mct_data_t * l_mct_deco_data = 00,* l_mct_offset_data = 00;
9040         opj_simple_mcc_decorrelation_data_t * l_mcc_data;
9041         OPJ_UINT32 l_mct_size,l_nb_elem;
9042         OPJ_FLOAT32 * l_data, * l_current_data;
9043         opj_tccp_t * l_tccp;
9044
9045         // preconditions
9046         assert(p_tcp != 00);
9047
9048         if (p_tcp->mct != 2) {
9049                 return OPJ_TRUE;
9050         }
9051
9052         if (p_tcp->m_mct_decoding_matrix) {
9053                 if (p_tcp->m_nb_mct_records == p_tcp->m_nb_max_mct_records) {
9054                         p_tcp->m_nb_max_mct_records += J2K_MCT_DEFAULT_NB_RECORDS;
9055
9056                         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));
9057                         if (! p_tcp->m_mct_records) {
9058                                 return OPJ_FALSE;
9059                         }
9060                         l_mct_deco_data = p_tcp->m_mct_records + p_tcp->m_nb_mct_records;
9061
9062                         memset(l_mct_deco_data ,0,(p_tcp->m_nb_max_mct_records - p_tcp->m_nb_mct_records) * sizeof(opj_mct_data_t));
9063                 }
9064                 l_mct_deco_data = p_tcp->m_mct_records + p_tcp->m_nb_mct_records;
9065
9066                 if (l_mct_deco_data->m_data) {
9067                         opj_free(l_mct_deco_data->m_data);
9068                         l_mct_deco_data->m_data = 00;
9069                 }
9070
9071                 l_mct_deco_data->m_index = l_indix++;
9072                 l_mct_deco_data->m_array_type = MCT_TYPE_DECORRELATION;
9073                 l_mct_deco_data->m_element_type = MCT_TYPE_FLOAT;
9074                 l_nb_elem = p_image->numcomps * p_image->numcomps;
9075                 l_mct_size = l_nb_elem * MCT_ELEMENT_SIZE[l_mct_deco_data->m_element_type];
9076                 l_mct_deco_data->m_data = (OPJ_BYTE*)opj_malloc(l_mct_size );
9077
9078                 if (! l_mct_deco_data->m_data) {
9079                         return OPJ_FALSE;
9080                 }
9081
9082                 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);
9083
9084                 l_mct_deco_data->m_data_size = l_mct_size;
9085                 ++p_tcp->m_nb_mct_records;
9086         }
9087
9088         if (p_tcp->m_nb_mct_records == p_tcp->m_nb_max_mct_records) {
9089                 p_tcp->m_nb_max_mct_records += J2K_MCT_DEFAULT_NB_RECORDS;
9090                 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));
9091
9092                 if (! p_tcp->m_mct_records) {
9093                         return OPJ_FALSE;
9094                 }
9095
9096                 l_mct_offset_data = p_tcp->m_mct_records + p_tcp->m_nb_mct_records;
9097                 memset(l_mct_offset_data ,0,(p_tcp->m_nb_max_mct_records - p_tcp->m_nb_mct_records) * sizeof(opj_mct_data_t));
9098
9099                 if (l_mct_deco_data) {
9100                         l_mct_deco_data = l_mct_offset_data - 1;
9101                 }
9102         }
9103
9104         l_mct_offset_data = p_tcp->m_mct_records + p_tcp->m_nb_mct_records;
9105
9106         if (l_mct_offset_data->m_data) {
9107                 opj_free(l_mct_offset_data->m_data);
9108                 l_mct_offset_data->m_data = 00;
9109         }
9110
9111         l_mct_offset_data->m_index = l_indix++;
9112         l_mct_offset_data->m_array_type = MCT_TYPE_OFFSET;
9113         l_mct_offset_data->m_element_type = MCT_TYPE_FLOAT;
9114         l_nb_elem = p_image->numcomps;
9115         l_mct_size = l_nb_elem * MCT_ELEMENT_SIZE[l_mct_offset_data->m_element_type];
9116         l_mct_offset_data->m_data = (OPJ_BYTE*)opj_malloc(l_mct_size );
9117
9118         if (! l_mct_offset_data->m_data) {
9119                 return OPJ_FALSE;
9120         }
9121
9122         l_data = (OPJ_FLOAT32*)opj_malloc(l_nb_elem * sizeof(OPJ_FLOAT32));
9123         if (! l_data) {
9124                 opj_free(l_mct_offset_data->m_data);
9125                 l_mct_offset_data->m_data = 00;
9126                 return OPJ_FALSE;
9127         }
9128
9129         l_tccp = p_tcp->tccps;
9130         l_current_data = l_data;
9131
9132         for (i=0;i<l_nb_elem;++i) {
9133                 *(l_current_data++) = (OPJ_FLOAT32) (l_tccp->m_dc_level_shift);
9134                 ++l_tccp;
9135         }
9136
9137         j2k_mct_write_functions_from_float[l_mct_offset_data->m_element_type](l_data,l_mct_offset_data->m_data,l_nb_elem);
9138
9139         opj_free(l_data);
9140
9141         l_mct_offset_data->m_data_size = l_mct_size;
9142
9143         ++p_tcp->m_nb_mct_records;
9144
9145         if (p_tcp->m_nb_mcc_records == p_tcp->m_nb_max_mcc_records) {
9146                 p_tcp->m_nb_max_mcc_records += J2K_MCT_DEFAULT_NB_RECORDS;
9147                 p_tcp->m_mcc_records = (opj_simple_mcc_decorrelation_data_t*)
9148                 opj_realloc(p_tcp->m_mcc_records,p_tcp->m_nb_max_mcc_records * sizeof(opj_simple_mcc_decorrelation_data_t));
9149
9150                 if (! p_tcp->m_mcc_records) {
9151                         return OPJ_FALSE;
9152                 }
9153                 l_mcc_data = p_tcp->m_mcc_records + p_tcp->m_nb_mcc_records;
9154                 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));
9155
9156         }
9157
9158         l_mcc_data = p_tcp->m_mcc_records + p_tcp->m_nb_mcc_records;
9159         l_mcc_data->m_decorrelation_array = l_mct_deco_data;
9160         l_mcc_data->m_is_irreversible = 1;
9161         l_mcc_data->m_nb_comps = p_image->numcomps;
9162         l_mcc_data->m_index = l_indix++;
9163         l_mcc_data->m_offset_array = l_mct_offset_data;
9164         ++p_tcp->m_nb_mcc_records;
9165
9166         return OPJ_TRUE;
9167 }
9168
9169 /**
9170  * Builds the cp decoder parameters to use to decode tile.
9171  */
9172 opj_bool j2k_build_decoder (opj_j2k_v2_t * p_j2k,
9173                                                         opj_stream_private_t *p_stream,
9174                                                         opj_event_mgr_t * p_manager )
9175 {
9176         /* add here initialization of cp
9177            copy paste of setup_decoder */
9178   (void)p_j2k;
9179   (void)p_stream;
9180   (void)p_manager;
9181         return OPJ_TRUE;
9182 }
9183
9184 /**
9185  * Builds the cp encoder parameters to use to encode tile.
9186  */
9187 opj_bool j2k_build_encoder (opj_j2k_v2_t * p_j2k,
9188                                                         opj_stream_private_t *p_stream,
9189                                                         opj_event_mgr_t * p_manager )
9190 {
9191         /* add here initialization of cp
9192            copy paste of setup_encoder */
9193   (void)p_j2k;
9194   (void)p_stream;
9195   (void)p_manager;
9196         return OPJ_TRUE;
9197 }
9198
9199 /**
9200  * The default encoding validation procedure without any extension.
9201  *
9202  * @param       p_j2k                   the jpeg2000 codec to validate.
9203  * @param       p_stream                                the input stream to validate.
9204  * @param       p_manager               the user event manager.
9205  *
9206  * @return true if the parameters are correct.
9207  */
9208 opj_bool j2k_encoding_validation (      opj_j2k_v2_t * p_j2k,
9209                                                                         opj_stream_private_t *p_stream,
9210                                                                         opj_event_mgr_t * p_manager )
9211 {
9212         opj_bool l_is_valid = OPJ_TRUE;
9213
9214         /* preconditions */
9215         assert(p_j2k != 00);
9216         assert(p_stream != 00);
9217         assert(p_manager != 00);
9218
9219         /* STATE checking */
9220         /* make sure the state is at 0 */
9221         l_is_valid &= (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_NONE);
9222
9223         /* POINTER validation */
9224         /* make sure a p_j2k codec is present */
9225         l_is_valid &= (p_j2k->m_procedure_list != 00);
9226         /* make sure a validation list is present */
9227         l_is_valid &= (p_j2k->m_validation_list != 00);
9228
9229         if ((p_j2k->m_cp.tdx) < (OPJ_UINT32) (1 << p_j2k->m_cp.tcps->tccps->numresolutions)) {
9230                 opj_event_msg_v2(p_manager, EVT_ERROR, "Number of resolutions is too high in comparison to the size of tiles\n");
9231                 return OPJ_FALSE;
9232         }
9233
9234         if ((p_j2k->m_cp.tdy) < (OPJ_UINT32) (1 << p_j2k->m_cp.tcps->tccps->numresolutions)) {
9235                 opj_event_msg_v2(p_manager, EVT_ERROR, "Number of resolutions is too high in comparison to the size of tiles\n");
9236                 return OPJ_FALSE;
9237         }
9238
9239         /* PARAMETER VALIDATION */
9240         return l_is_valid;
9241 }
9242
9243 /**
9244  * The default decoding validation procedure without any extension.
9245  *
9246  * @param       p_j2k                   the jpeg2000 codec to validate.
9247  * @param       p_stream                                the input stream to validate.
9248  * @param       p_manager               the user event manager.
9249  *
9250  * @return true if the parameters are correct.
9251  */
9252 opj_bool j2k_decoding_validation (
9253                                                                 opj_j2k_v2_t *p_j2k,
9254                                                                 opj_stream_private_t *p_stream,
9255                                                                 opj_event_mgr_t * p_manager
9256                                                           )
9257 {
9258         opj_bool l_is_valid = OPJ_TRUE;
9259
9260         /* preconditions*/
9261         assert(p_j2k != 00);
9262         assert(p_stream != 00);
9263         assert(p_manager != 00);
9264
9265
9266         /* STATE checking */
9267         /* make sure the state is at 0 */
9268 #ifdef TODO_MSD
9269         l_is_valid &= (p_j2k->m_specific_param.m_decoder.m_state == J2K_DEC_STATE_NONE);
9270 #endif
9271         l_is_valid &= (p_j2k->m_specific_param.m_decoder.m_state == 0x0000);
9272
9273         /* POINTER validation */
9274         /* make sure a p_j2k codec is present */
9275         /* make sure a procedure list is present */
9276         l_is_valid &= (p_j2k->m_procedure_list != 00);
9277         /* make sure a validation list is present */
9278         l_is_valid &= (p_j2k->m_validation_list != 00);
9279
9280         /* PARAMETER VALIDATION */
9281         return l_is_valid;
9282 }
9283
9284 opj_bool j2k_read_header_procedure(     opj_j2k_v2_t *p_j2k,
9285                                                                         struct opj_stream_private *p_stream,
9286                                                                         struct opj_event_mgr * p_manager)
9287 {
9288         OPJ_UINT32 l_current_marker;
9289         OPJ_UINT32 l_marker_size;
9290         const opj_dec_memory_marker_handler_t * l_marker_handler = 00;
9291
9292         /* preconditions */
9293         assert(p_stream != 00);
9294         assert(p_j2k != 00);
9295         assert(p_manager != 00);
9296
9297         /*  We enter in the main header */
9298         p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_MHSOC;
9299
9300         /* Try to read the SOC marker, the codestream must begin with SOC marker */
9301         if (! j2k_read_soc_v2(p_j2k,p_stream,p_manager)) {
9302                 opj_event_msg_v2(p_manager, EVT_ERROR, "Expected a SOC marker \n");
9303                 return OPJ_FALSE;
9304         }
9305
9306         /* Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer */
9307         if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
9308                 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
9309                 return OPJ_FALSE;
9310         }
9311
9312         /* Read 2 bytes as the new marker ID */
9313         opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_current_marker,2);
9314
9315         /* Try to read until the SOT is detected */
9316         while (l_current_marker != J2K_MS_SOT) {
9317
9318                 /* Check if the current marker ID is valid */
9319                 if (l_current_marker < 0xff00) {
9320                         opj_event_msg_v2(p_manager, EVT_ERROR, "We expected read a marker ID (0xff--) instead of %.8x\n", l_current_marker);
9321                         return OPJ_FALSE;
9322                 }
9323
9324                 /* Get the marker handler from the marker ID */
9325                 l_marker_handler = j2k_get_marker_handler(l_current_marker);
9326
9327                 /* Manage case where marker is unknown */
9328                 if (l_marker_handler->id == J2K_MS_UNK) {
9329                         if (! j2k_read_unk_v2(p_j2k, p_stream, &l_current_marker, p_manager)){
9330                                 opj_event_msg_v2(p_manager, EVT_ERROR, "Unknow marker have been detected and generated error.\n");
9331                                 return OPJ_FALSE;
9332                         }
9333
9334                         if (l_current_marker == J2K_MS_SOT)
9335                                 break; /* SOT marker is detected main header is completely read */
9336                         else    /* Get the marker handler from the marker ID */
9337                                 l_marker_handler = j2k_get_marker_handler(l_current_marker);
9338                 }
9339
9340                 /* Check if the marker is known and if it is the right place to find it */
9341                 if (! (p_j2k->m_specific_param.m_decoder.m_state & l_marker_handler->states) ) {
9342                         opj_event_msg_v2(p_manager, EVT_ERROR, "Marker is not compliant with its position\n");
9343                         return OPJ_FALSE;
9344                 }
9345
9346                 /* Try to read 2 bytes (the marker size) from stream and copy them into the buffer */
9347                 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
9348                         opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
9349                         return OPJ_FALSE;
9350                 }
9351
9352                 /* read 2 bytes as the marker size */
9353                 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_marker_size,2);
9354                 l_marker_size -= 2; /* Subtract the size of the marker ID already read */
9355
9356                 /* Check if the marker size is compatible with the header data size */
9357                 if (l_marker_size > p_j2k->m_specific_param.m_decoder.m_header_data_size) {
9358                         p_j2k->m_specific_param.m_decoder.m_header_data = (OPJ_BYTE*)
9359                                         opj_realloc(p_j2k->m_specific_param.m_decoder.m_header_data,l_marker_size);
9360                         if (p_j2k->m_specific_param.m_decoder.m_header_data == 00) {
9361                                 return OPJ_FALSE;
9362                         }
9363                         p_j2k->m_specific_param.m_decoder.m_header_data_size = l_marker_size;
9364                 }
9365
9366                 /* Try to read the rest of the marker segment from stream and copy them into the buffer */
9367                 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) {
9368                         opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
9369                         return OPJ_FALSE;
9370                 }
9371
9372                 /* Read the marker segment with the correct marker handler */
9373                 if (! (*(l_marker_handler->handler))(p_j2k,p_j2k->m_specific_param.m_decoder.m_header_data,l_marker_size,p_manager)) {
9374                         opj_event_msg_v2(p_manager, EVT_ERROR, "Marker handler function failed to read the marker segment\n");
9375                         return OPJ_FALSE;
9376                 }
9377
9378                 /* Add the marker to the codestream index*/
9379                 j2k_add_mhmarker_v2(p_j2k->cstr_index,
9380                                                         l_marker_handler->id,
9381                                                         (OPJ_UINT32) opj_stream_tell(p_stream) - l_marker_size - 4,
9382                                                         l_marker_size + 4 );
9383
9384                 /* Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer */
9385                 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
9386                         opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
9387                         return OPJ_FALSE;
9388                 }
9389
9390                 /* read 2 bytes as the new marker ID */
9391                 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_current_marker,2);
9392         }
9393
9394         opj_event_msg_v2(p_manager, EVT_INFO, "Main header has been correctly decoded.\n");
9395
9396         /* Position of the last element if the main header */
9397         p_j2k->cstr_index->main_head_end = (OPJ_UINT32) opj_stream_tell(p_stream) - 2;
9398
9399         /* Next step: read a tile-part header */
9400         p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_TPHSOT;
9401
9402         return OPJ_TRUE;
9403 }
9404
9405 /**
9406  * Excutes the given procedures on the given codec.
9407  *
9408  * @param       p_procedure_list        the list of procedures to execute
9409  * @param       p_j2k                                   the jpeg2000 codec to execute the procedures on.
9410  * @param       p_stream                                        the stream to execute the procedures on.
9411  * @param       p_manager                       the user manager.
9412  *
9413  * @return      true                            if all the procedures were successfully executed.
9414  */
9415 opj_bool j2k_exec (     opj_j2k_v2_t * p_j2k,
9416                                         opj_procedure_list_t * p_procedure_list,
9417                                         opj_stream_private_t *p_stream,
9418                                         opj_event_mgr_t * p_manager )
9419 {
9420         opj_bool (** l_procedure) (opj_j2k_v2_t * ,opj_stream_private_t *,opj_event_mgr_t *) = 00;
9421         opj_bool l_result = OPJ_TRUE;
9422         OPJ_UINT32 l_nb_proc, i;
9423
9424         /* preconditions*/
9425         assert(p_procedure_list != 00);
9426         assert(p_j2k != 00);
9427         assert(p_stream != 00);
9428         assert(p_manager != 00);
9429
9430
9431         l_nb_proc = opj_procedure_list_get_nb_procedures(p_procedure_list);
9432         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);
9433
9434         for     (i=0;i<l_nb_proc;++i) {
9435                 l_result = l_result && ((*l_procedure) (p_j2k,p_stream,p_manager));
9436                 ++l_procedure;
9437         }
9438
9439         /* and clear the procedure list at the end.*/
9440         opj_procedure_list_clear(p_procedure_list);
9441         return l_result;
9442 }
9443
9444 /* FIXME DOC*/
9445 opj_bool j2k_copy_default_tcp_and_create_tcd
9446                                                 (
9447                                                 opj_j2k_v2_t * p_j2k,
9448                                                 opj_stream_private_t *p_stream,
9449                                                 opj_event_mgr_t * p_manager
9450                                                 )
9451 {
9452         opj_tcp_v2_t * l_tcp = 00;
9453         opj_tcp_v2_t * l_default_tcp = 00;
9454         OPJ_UINT32 l_nb_tiles;
9455         OPJ_UINT32 i,j;
9456         opj_tccp_t *l_current_tccp = 00;
9457         OPJ_UINT32 l_tccp_size;
9458         OPJ_UINT32 l_mct_size;
9459         opj_image_t * l_image;
9460         OPJ_UINT32 l_mcc_records_size,l_mct_records_size;
9461         opj_mct_data_t * l_src_mct_rec, *l_dest_mct_rec;
9462         opj_simple_mcc_decorrelation_data_t * l_src_mcc_rec, *l_dest_mcc_rec;
9463         OPJ_UINT32 l_offset;
9464
9465         /* preconditions */
9466         assert(p_j2k != 00);
9467         assert(p_stream != 00);
9468         assert(p_manager != 00);
9469
9470         l_image = p_j2k->m_private_image;
9471         l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw;
9472         l_tcp = p_j2k->m_cp.tcps;
9473         l_tccp_size = l_image->numcomps * sizeof(opj_tccp_t);
9474         l_default_tcp = p_j2k->m_specific_param.m_decoder.m_default_tcp;
9475         l_mct_size = l_image->numcomps * l_image->numcomps * sizeof(OPJ_FLOAT32);
9476
9477         /* For each tile */
9478         for (i=0; i<l_nb_tiles; ++i) {
9479                 /* keep the tile-compo coding parameters pointer of the current tile coding parameters*/
9480                 l_current_tccp = l_tcp->tccps;
9481                 /*Copy default coding parameters into the current tile coding parameters*/
9482                 memcpy(l_tcp, l_default_tcp, sizeof(opj_tcp_v2_t));
9483                 /* Initialize some values of the current tile coding parameters*/
9484                 l_tcp->ppt = 0;
9485                 l_tcp->ppt_data = 00;
9486                 /* Reconnect the tile-compo coding parameters pointer to the current tile coding parameters*/
9487                 l_tcp->tccps = l_current_tccp;
9488
9489                 /* Get the mct_decoding_matrix of the dflt_tile_cp and copy them into the current tile cp*/
9490                 if (l_default_tcp->m_mct_decoding_matrix) {
9491                         l_tcp->m_mct_decoding_matrix = (OPJ_FLOAT32*)opj_malloc(l_mct_size);
9492                         if (! l_tcp->m_mct_decoding_matrix ) {
9493                                 return OPJ_FALSE;
9494                         }
9495                         memcpy(l_tcp->m_mct_decoding_matrix,l_default_tcp->m_mct_decoding_matrix,l_mct_size);
9496                 }
9497
9498                 /* Get the mct_record of the dflt_tile_cp and copy them into the current tile cp*/
9499                 l_mct_records_size = l_default_tcp->m_nb_max_mct_records * sizeof(opj_mct_data_t);
9500                 l_tcp->m_mct_records = (opj_mct_data_t*)opj_malloc(l_mct_records_size);
9501                 if (! l_tcp->m_mct_records) {
9502                         return OPJ_FALSE;
9503                 }
9504                 memcpy(l_tcp->m_mct_records, l_default_tcp->m_mct_records,l_mct_records_size);
9505
9506                 /* Copy the mct record data from dflt_tile_cp to the current tile*/
9507                 l_src_mct_rec = l_default_tcp->m_mct_records;
9508                 l_dest_mct_rec = l_tcp->m_mct_records;
9509
9510                 for (j=0;j<l_default_tcp->m_nb_mct_records;++j) {
9511
9512                         if (l_src_mct_rec->m_data) {
9513
9514                                 l_dest_mct_rec->m_data = (OPJ_BYTE*) opj_malloc(l_src_mct_rec->m_data_size);
9515                                 if(! l_dest_mct_rec->m_data) {
9516                                         return OPJ_FALSE;
9517                                 }
9518                                 memcpy(l_dest_mct_rec->m_data,l_src_mct_rec->m_data,l_src_mct_rec->m_data_size);
9519                         }
9520
9521                         ++l_src_mct_rec;
9522                         ++l_dest_mct_rec;
9523                 }
9524
9525                 /* Get the mcc_record of the dflt_tile_cp and copy them into the current tile cp*/
9526                 l_mcc_records_size = l_default_tcp->m_nb_max_mcc_records * sizeof(opj_simple_mcc_decorrelation_data_t);
9527                 l_tcp->m_mcc_records = (opj_simple_mcc_decorrelation_data_t*) opj_malloc(l_mcc_records_size);
9528                 if (! l_tcp->m_mcc_records) {
9529                         return OPJ_FALSE;
9530                 }
9531                 memcpy(l_tcp->m_mcc_records,l_default_tcp->m_mcc_records,l_mcc_records_size);
9532
9533                 /* Copy the mcc record data from dflt_tile_cp to the current tile*/
9534                 l_src_mcc_rec = l_default_tcp->m_mcc_records;
9535                 l_dest_mcc_rec = l_tcp->m_mcc_records;
9536
9537                 for (j=0;j<l_default_tcp->m_nb_max_mcc_records;++j) {
9538
9539                         if (l_src_mcc_rec->m_decorrelation_array) {
9540                                 l_offset = l_src_mcc_rec->m_decorrelation_array - l_default_tcp->m_mct_records;
9541                                 l_dest_mcc_rec->m_decorrelation_array = l_tcp->m_mct_records + l_offset;
9542                         }
9543
9544                         if (l_src_mcc_rec->m_offset_array) {
9545                                 l_offset = l_src_mcc_rec->m_offset_array - l_default_tcp->m_mct_records;
9546                                 l_dest_mcc_rec->m_offset_array = l_tcp->m_mct_records + l_offset;
9547                         }
9548
9549                         ++l_src_mcc_rec;
9550                         ++l_dest_mcc_rec;
9551                 }
9552
9553                 /* Copy all the dflt_tile_compo_cp to the current tile cp */
9554                 memcpy(l_current_tccp,l_default_tcp->tccps,l_tccp_size);
9555
9556                 /* Move to next tile cp*/
9557                 ++l_tcp;
9558         }
9559
9560         /* Create the current tile decoder*/
9561         p_j2k->m_tcd = (opj_tcd_v2_t*)tcd_create_v2(OPJ_TRUE); /* FIXME why a cast ? */
9562         if (! p_j2k->m_tcd ) {
9563                 return OPJ_FALSE;
9564         }
9565
9566         if ( !tcd_init_v2(p_j2k->m_tcd, l_image, &(p_j2k->m_cp)) ) {
9567                 tcd_destroy_v2(p_j2k->m_tcd);
9568                 p_j2k->m_tcd = 00;
9569                 opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot decode tile, memory error\n");
9570                 return OPJ_FALSE;
9571         }
9572
9573         return OPJ_TRUE;
9574 }
9575
9576 /**
9577  * Reads the lookup table containing all the marker, status and action, and returns the handler associated
9578  * with the marker value.
9579  * @param       p_id            Marker value to look up
9580  *
9581  * @return      the handler associated with the id.
9582 */
9583 const opj_dec_memory_marker_handler_t * j2k_get_marker_handler (OPJ_UINT32 p_id)
9584 {
9585         const opj_dec_memory_marker_handler_t *e;
9586         for (e = j2k_memory_marker_handler_tab; e->id != 0; ++e) {
9587                 if (e->id == p_id) {
9588                         break; /* we find a handler corresponding to the marker ID*/
9589                 }
9590         }
9591         return e;
9592 }
9593
9594
9595 /**
9596  * Destroys a jpeg2000 codec.
9597  *
9598  * @param       p_j2k   the jpeg20000 structure to destroy.
9599  */
9600 void j2k_destroy (opj_j2k_v2_t *p_j2k)
9601 {
9602         if (p_j2k == 00) {
9603                 return;
9604         }
9605
9606         if (p_j2k->m_is_decoder) {
9607
9608                 if (p_j2k->m_specific_param.m_decoder.m_default_tcp != 00) {
9609                         j2k_tcp_destroy(p_j2k->m_specific_param.m_decoder.m_default_tcp);
9610                         opj_free(p_j2k->m_specific_param.m_decoder.m_default_tcp);
9611                         p_j2k->m_specific_param.m_decoder.m_default_tcp = 00;
9612                 }
9613
9614                 if (p_j2k->m_specific_param.m_decoder.m_header_data != 00) {
9615                         opj_free(p_j2k->m_specific_param.m_decoder.m_header_data);
9616                         p_j2k->m_specific_param.m_decoder.m_header_data = 00;
9617                         p_j2k->m_specific_param.m_decoder.m_header_data_size = 0;
9618                 }
9619         }
9620         else {
9621
9622                 if (p_j2k->m_specific_param.m_encoder.m_encoded_tile_data) {
9623                         opj_free(p_j2k->m_specific_param.m_encoder.m_encoded_tile_data);
9624                         p_j2k->m_specific_param.m_encoder.m_encoded_tile_data = 00;
9625                 }
9626
9627                 if (p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer) {
9628                         opj_free(p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer);
9629                         p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer = 00;
9630                         p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current = 00;
9631                 }
9632
9633                 if (p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
9634                         opj_free(p_j2k->m_specific_param.m_encoder.m_header_tile_data);
9635                         p_j2k->m_specific_param.m_encoder.m_header_tile_data = 00;
9636                         p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = 0;
9637                 }
9638         }
9639
9640         tcd_destroy_v2(p_j2k->m_tcd);
9641
9642         j2k_cp_destroy(&(p_j2k->m_cp));
9643         memset(&(p_j2k->m_cp),0,sizeof(opj_cp_v2_t));
9644
9645         opj_procedure_list_destroy(p_j2k->m_procedure_list);
9646         p_j2k->m_procedure_list = 00;
9647
9648         opj_procedure_list_destroy(p_j2k->m_validation_list);
9649         p_j2k->m_procedure_list = 00;
9650
9651         j2k_destroy_cstr_index(p_j2k->cstr_index);
9652         p_j2k->cstr_index = NULL;
9653
9654         opj_image_destroy(p_j2k->m_private_image);
9655         p_j2k->m_private_image = NULL;
9656
9657         opj_image_destroy(p_j2k->m_output_image);
9658         p_j2k->m_output_image = NULL;
9659
9660         opj_free(p_j2k);
9661 }
9662
9663 void j2k_destroy_cstr_index (opj_codestream_index_t *p_cstr_ind)
9664 {
9665         if (p_cstr_ind) {
9666
9667                 if (p_cstr_ind->marker) {
9668                         opj_free(p_cstr_ind->marker);
9669                         p_cstr_ind->marker = NULL;
9670                 }
9671
9672                 if (p_cstr_ind->tile_index) {
9673                         OPJ_UINT32 it_tile = 0;
9674
9675                         for (it_tile=0; it_tile < p_cstr_ind->nb_of_tiles; it_tile++) {
9676
9677                                 if(p_cstr_ind->tile_index[it_tile].packet_index) {
9678                                         opj_free(p_cstr_ind->tile_index[it_tile].packet_index);
9679                                         p_cstr_ind->tile_index[it_tile].packet_index = NULL;
9680                                 }
9681
9682                                 if(p_cstr_ind->tile_index[it_tile].tp_index){
9683                                         opj_free(p_cstr_ind->tile_index[it_tile].tp_index);
9684                                         p_cstr_ind->tile_index[it_tile].tp_index = NULL;
9685                                 }
9686
9687                                 if(p_cstr_ind->tile_index[it_tile].marker){
9688                                         opj_free(p_cstr_ind->tile_index[it_tile].marker);
9689                                         p_cstr_ind->tile_index[it_tile].marker = NULL;
9690
9691                                 }
9692                         }
9693
9694                         opj_free( p_cstr_ind->tile_index);
9695                         p_cstr_ind->tile_index = NULL;
9696                 }
9697
9698                 opj_free(p_cstr_ind);
9699         }
9700 }
9701
9702
9703
9704 /**
9705  * Destroys a tile coding parameter structure.
9706  *
9707  * @param       p_tcp           the tile coding parameter to destroy.
9708  */
9709 void j2k_tcp_destroy (opj_tcp_v2_t *p_tcp)
9710 {
9711         if (p_tcp == 00) {
9712                 return;
9713         }
9714
9715         if (p_tcp->ppt_buffer != 00) {
9716                 opj_free(p_tcp->ppt_buffer);
9717                 p_tcp->ppt_buffer = 00;
9718         }
9719
9720         if (p_tcp->tccps != 00) {
9721                 opj_free(p_tcp->tccps);
9722                 p_tcp->tccps = 00;
9723         }
9724
9725         if (p_tcp->m_mct_coding_matrix != 00) {
9726                 opj_free(p_tcp->m_mct_coding_matrix);
9727                 p_tcp->m_mct_coding_matrix = 00;
9728         }
9729
9730         if (p_tcp->m_mct_decoding_matrix != 00) {
9731                 opj_free(p_tcp->m_mct_decoding_matrix);
9732                 p_tcp->m_mct_decoding_matrix = 00;
9733         }
9734
9735         if (p_tcp->m_mcc_records) {
9736                 opj_free(p_tcp->m_mcc_records);
9737                 p_tcp->m_mcc_records = 00;
9738                 p_tcp->m_nb_max_mcc_records = 0;
9739                 p_tcp->m_nb_mcc_records = 0;
9740         }
9741
9742         if (p_tcp->m_mct_records) {
9743                 opj_mct_data_t * l_mct_data = p_tcp->m_mct_records;
9744                 OPJ_UINT32 i;
9745
9746                 for (i=0;i<p_tcp->m_nb_mct_records;++i) {
9747                         if (l_mct_data->m_data) {
9748                                 opj_free(l_mct_data->m_data);
9749                                 l_mct_data->m_data = 00;
9750                         }
9751
9752                         ++l_mct_data;
9753                 }
9754
9755                 opj_free(p_tcp->m_mct_records);
9756                 p_tcp->m_mct_records = 00;
9757         }
9758
9759         if (p_tcp->mct_norms != 00) {
9760                 opj_free(p_tcp->mct_norms);
9761                 p_tcp->mct_norms = 00;
9762         }
9763
9764         j2k_tcp_data_destroy(p_tcp);
9765
9766 }
9767
9768 /**
9769  * Destroys the data inside a tile coding parameter structure.
9770  *
9771  * @param       p_tcp           the tile coding parameter which contain data to destroy.
9772  */
9773 void j2k_tcp_data_destroy (opj_tcp_v2_t *p_tcp)
9774 {
9775         if (p_tcp->m_data) {
9776                 opj_free(p_tcp->m_data);
9777                 p_tcp->m_data = NULL;
9778                 p_tcp->m_data_size = 0;
9779         }
9780 }
9781
9782 /**
9783  * Destroys a coding parameter structure.
9784  *
9785  * @param       p_cp            the coding parameter to destroy.
9786  */
9787 void j2k_cp_destroy (opj_cp_v2_t *p_cp)
9788 {
9789         OPJ_UINT32 l_nb_tiles;
9790         opj_tcp_v2_t * l_current_tile = 00;
9791         OPJ_UINT32 i;
9792
9793         if
9794                 (p_cp == 00)
9795         {
9796                 return;
9797         }
9798         if
9799                 (p_cp->tcps != 00)
9800         {
9801                 l_current_tile = p_cp->tcps;
9802                 l_nb_tiles = p_cp->th * p_cp->tw;
9803
9804                 for
9805                         (i = 0; i < l_nb_tiles; ++i)
9806                 {
9807                         j2k_tcp_destroy(l_current_tile);
9808                         ++l_current_tile;
9809                 }
9810                 opj_free(p_cp->tcps);
9811                 p_cp->tcps = 00;
9812         }
9813         if
9814                 (p_cp->ppm_buffer != 00)
9815         {
9816                 opj_free(p_cp->ppm_buffer);
9817                 p_cp->ppm_buffer = 00;
9818         }
9819         if
9820                 (p_cp->comment != 00)
9821         {
9822                 opj_free(p_cp->comment);
9823                 p_cp->comment = 00;
9824         }
9825         if
9826                 (! p_cp->m_is_decoder)
9827         {
9828                 if
9829                         (p_cp->m_specific_param.m_enc.m_matrice)
9830                 {
9831                         opj_free(p_cp->m_specific_param.m_enc.m_matrice);
9832                         p_cp->m_specific_param.m_enc.m_matrice = 00;
9833                 }
9834         }
9835 }
9836
9837
9838
9839 /**
9840  * Reads a tile header.
9841  * @param       p_j2k           the jpeg2000 codec.
9842  * @param       p_stream                        the stream to write data to.
9843  * @param       p_manager       the user event manager.
9844  */
9845 opj_bool j2k_read_tile_header(  opj_j2k_v2_t * p_j2k,
9846                                                                 OPJ_UINT32 * p_tile_index,
9847                                                                 OPJ_UINT32 * p_data_size,
9848                                                                 OPJ_INT32 * p_tile_x0, OPJ_INT32 * p_tile_y0,
9849                                                                 OPJ_INT32 * p_tile_x1, OPJ_INT32 * p_tile_y1,
9850                                                                 OPJ_UINT32 * p_nb_comps,
9851                                                                 opj_bool * p_go_on,
9852                                                                 opj_stream_private_t *p_stream,
9853                                                                 opj_event_mgr_t * p_manager )
9854 {
9855         OPJ_UINT32 l_current_marker = J2K_MS_SOT;
9856         OPJ_UINT32 l_marker_size;
9857         const opj_dec_memory_marker_handler_t * l_marker_handler = 00;
9858         opj_tcp_v2_t * l_tcp = NULL;
9859         OPJ_UINT32 l_nb_tiles;
9860
9861         /* preconditions */
9862         assert(p_stream != 00);
9863         assert(p_j2k != 00);
9864         assert(p_manager != 00);
9865
9866         /* Reach the End Of Codestream ?*/
9867         if (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_EOC){
9868                 l_current_marker = J2K_MS_EOC;
9869         }
9870         /* We need to encounter a SOT marker (a new tile-part header) */
9871         else if (p_j2k->m_specific_param.m_decoder.m_state != J2K_STATE_TPHSOT){
9872                 return OPJ_FALSE;
9873         }
9874
9875         /* Read into the codestream until reach the EOC or ! can_decode ??? FIXME */
9876         while ( (!p_j2k->m_specific_param.m_decoder.m_can_decode) && (l_current_marker != J2K_MS_EOC) ) {
9877
9878                 /* Try to read until the Start Of Data is detected */
9879                 while (l_current_marker != J2K_MS_SOD) {
9880
9881                         /* Try to read 2 bytes (the marker size) from stream and copy them into the buffer */
9882                         if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
9883                                 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
9884                                 return OPJ_FALSE;
9885                         }
9886
9887                         /* Read 2 bytes from the buffer as the marker size */
9888                         opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_marker_size,2);
9889
9890                         /* Why this condition? FIXME */
9891                         if (p_j2k->m_specific_param.m_decoder.m_state & J2K_STATE_TPH){
9892                                 p_j2k->m_specific_param.m_decoder.m_sot_length -= (l_marker_size + 2);
9893                         }
9894                         l_marker_size -= 2; /* Subtract the size of the marker ID already read */
9895
9896                         /* Get the marker handler from the marker ID */
9897                         l_marker_handler = j2k_get_marker_handler(l_current_marker);
9898
9899                         /* Check if the marker is known and if it is the right place to find it */
9900                         if (! (p_j2k->m_specific_param.m_decoder.m_state & l_marker_handler->states) ) {
9901                                 opj_event_msg_v2(p_manager, EVT_ERROR, "Marker is not compliant with its position\n");
9902                                 return OPJ_FALSE;
9903                         }
9904 /* FIXME manage case of unknown marker as in the main header ? */
9905
9906                         /* Check if the marker size is compatible with the header data size */
9907                         if (l_marker_size > p_j2k->m_specific_param.m_decoder.m_header_data_size) {
9908                                 p_j2k->m_specific_param.m_decoder.m_header_data = (OPJ_BYTE*)
9909                                         opj_realloc(p_j2k->m_specific_param.m_decoder.m_header_data,l_marker_size);
9910                                 if (p_j2k->m_specific_param.m_decoder.m_header_data == 00) {
9911                                         return OPJ_FALSE;
9912                                 }
9913                                 p_j2k->m_specific_param.m_decoder.m_header_data_size = l_marker_size;
9914                         }
9915
9916                         /* Try to read the rest of the marker segment from stream and copy them into the buffer */
9917                         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) {
9918                                 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
9919                                 return OPJ_FALSE;
9920                         }
9921
9922                         /* Read the marker segment with the correct marker handler */
9923                         if (! (*(l_marker_handler->handler))(p_j2k,p_j2k->m_specific_param.m_decoder.m_header_data,l_marker_size,p_manager)) {
9924                                 opj_event_msg_v2(p_manager, EVT_ERROR, "Fail to read the current marker segment (%#x)\n", l_current_marker);
9925                                 return OPJ_FALSE;
9926                         }
9927
9928                         /* Add the marker to the codestream index*/
9929                         j2k_add_tlmarker_v2(p_j2k->m_current_tile_number,
9930                                                                 p_j2k->cstr_index,
9931                                                                 l_marker_handler->id,
9932                                                                 (OPJ_UINT32) opj_stream_tell(p_stream) - l_marker_size - 4,
9933                                                                 l_marker_size + 4 );
9934
9935                         /* Keep the position of the last SOT marker read */
9936                         if ( l_marker_handler->id == J2K_MS_SOT ) {
9937                                 OPJ_UINT32 sot_pos = (OPJ_UINT32) opj_stream_tell(p_stream) - l_marker_size - 4 ;
9938                                 if (sot_pos > p_j2k->m_specific_param.m_decoder.m_last_sot_read_pos)
9939                                 {
9940                                         p_j2k->m_specific_param.m_decoder.m_last_sot_read_pos = sot_pos;
9941                                 }
9942                         }
9943
9944
9945                         if (p_j2k->m_specific_param.m_decoder.m_skip_data) {
9946                                 /* Skip the rest of the tile part header*/
9947                                 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) {
9948                                         opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
9949                                         return OPJ_FALSE;
9950                                 }
9951                                 l_current_marker = J2K_MS_SOD; /* Normally we reached a SOD */
9952                         }
9953                         else {
9954                                 /* Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer*/
9955                                 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
9956                                         opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
9957                                         return OPJ_FALSE;
9958                                 }
9959                                 /* Read 2 bytes from the buffer as the new marker ID */
9960                                 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_current_marker,2);
9961                         }
9962                 }
9963
9964                 /* If we didn't skip data before, we need to read the SOD marker*/
9965                 if (! p_j2k->m_specific_param.m_decoder.m_skip_data) {
9966                         /* Try to read the SOD marker and skip data ? FIXME */
9967                         if (! j2k_read_sod_v2(p_j2k, p_stream, p_manager)) {
9968                                 return OPJ_FALSE;
9969                         }
9970
9971
9972
9973                         if (! p_j2k->m_specific_param.m_decoder.m_can_decode){
9974                                 /* Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer */
9975                                 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
9976                                         opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
9977                                         return OPJ_FALSE;
9978                                 }
9979
9980                                 /* Read 2 bytes from buffer as the new marker ID */
9981                                 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_current_marker,2);
9982                         }
9983                 }
9984                 else {
9985                         /* Indicate we will try to read a new tile-part header*/
9986                         p_j2k->m_specific_param.m_decoder.m_skip_data = 0;
9987                         p_j2k->m_specific_param.m_decoder.m_can_decode = 0;
9988                         p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_TPHSOT;
9989
9990                         /* Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer */
9991                         if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
9992                                 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
9993                                 return OPJ_FALSE;
9994                         }
9995
9996                         /* Read 2 bytes from buffer as the new marker ID */
9997                         opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_current_marker,2);
9998                 }
9999         }
10000
10001         /* Current marker is the EOC marker ?*/
10002         if (l_current_marker == J2K_MS_EOC) {
10003                 if (p_j2k->m_specific_param.m_decoder.m_state != J2K_STATE_EOC ){
10004                         p_j2k->m_current_tile_number = 0;
10005                         p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_EOC;
10006                 }
10007         }
10008
10009         /* FIXME DOC ???*/
10010         if ( ! p_j2k->m_specific_param.m_decoder.m_can_decode) {
10011                 l_tcp = p_j2k->m_cp.tcps + p_j2k->m_current_tile_number;
10012                 l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw;
10013
10014                 while( (p_j2k->m_current_tile_number < l_nb_tiles) && (l_tcp->m_data == 00) ) {
10015                         ++p_j2k->m_current_tile_number;
10016                         ++l_tcp;
10017                 }
10018
10019                 if (p_j2k->m_current_tile_number == l_nb_tiles) {
10020                         *p_go_on = OPJ_FALSE;
10021                         return OPJ_TRUE;
10022                 }
10023         }
10024
10025         /*FIXME ???*/
10026         if (! tcd_init_decode_tile(p_j2k->m_tcd, p_j2k->m_current_tile_number)) {
10027                 opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot decode tile, memory error\n");
10028                 return OPJ_FALSE;
10029         }
10030
10031         opj_event_msg_v2(p_manager, EVT_INFO, "Header of tile %d / %d has been read.\n",
10032                         p_j2k->m_current_tile_number, (p_j2k->m_cp.th * p_j2k->m_cp.tw) - 1);
10033
10034         *p_tile_index = p_j2k->m_current_tile_number;
10035         *p_go_on = OPJ_TRUE;
10036         *p_data_size = tcd_get_decoded_tile_size(p_j2k->m_tcd);
10037         *p_tile_x0 = p_j2k->m_tcd->tcd_image->tiles->x0;
10038         *p_tile_y0 = p_j2k->m_tcd->tcd_image->tiles->y0;
10039         *p_tile_x1 = p_j2k->m_tcd->tcd_image->tiles->x1;
10040         *p_tile_y1 = p_j2k->m_tcd->tcd_image->tiles->y1;
10041         *p_nb_comps = p_j2k->m_tcd->tcd_image->tiles->numcomps;
10042
10043          p_j2k->m_specific_param.m_decoder.m_state |= 0x0080;/* FIXME J2K_DEC_STATE_DATA;*/
10044
10045         return OPJ_TRUE;
10046 }
10047
10048
10049 opj_bool j2k_decode_tile (      opj_j2k_v2_t * p_j2k,
10050                                                         OPJ_UINT32 p_tile_index,
10051                                                         OPJ_BYTE * p_data,
10052                                                         OPJ_UINT32 p_data_size,
10053                                                         opj_stream_private_t *p_stream,
10054                                                         opj_event_mgr_t * p_manager )
10055 {
10056         OPJ_UINT32 l_current_marker;
10057         OPJ_BYTE l_data [2];
10058         opj_tcp_v2_t * l_tcp;
10059
10060         /* preconditions */
10061         assert(p_stream != 00);
10062         assert(p_j2k != 00);
10063         assert(p_manager != 00);
10064
10065         if ( !(p_j2k->m_specific_param.m_decoder.m_state & 0x0080/*FIXME J2K_DEC_STATE_DATA*/)
10066                 || (p_tile_index != p_j2k->m_current_tile_number) ) {
10067                 return OPJ_FALSE;
10068         }
10069
10070         l_tcp = &(p_j2k->m_cp.tcps[p_tile_index]);
10071         if (! l_tcp->m_data) {
10072                 j2k_tcp_destroy(l_tcp);
10073                 return OPJ_FALSE;
10074         }
10075
10076         if (! tcd_decode_tile_v2(       p_j2k->m_tcd,
10077                                                                 l_tcp->m_data,
10078                                                                 l_tcp->m_data_size,
10079                                                                 p_tile_index,
10080                                                                 p_j2k->cstr_index) ) {
10081                 j2k_tcp_destroy(l_tcp);
10082                 p_j2k->m_specific_param.m_decoder.m_state |= 0x8000;/*FIXME J2K_DEC_STATE_ERR;*/
10083                 return OPJ_FALSE;
10084         }
10085
10086         if (! tcd_update_tile_data(p_j2k->m_tcd,p_data,p_data_size)) {
10087                 return OPJ_FALSE;
10088         }
10089
10090         /* To avoid to destroy the tcp which can be useful when we try to decode a tile decoded before (cf j2k_random_tile_access)
10091          * we destroy just the data which will be re-read in read_tile_header*/
10092         /*j2k_tcp_destroy(l_tcp);
10093         p_j2k->m_tcd->tcp = 0;*/
10094         j2k_tcp_data_destroy(l_tcp);
10095
10096         p_j2k->m_specific_param.m_decoder.m_can_decode = 0;
10097         p_j2k->m_specific_param.m_decoder.m_state &= (~ (0x0080));/* FIXME J2K_DEC_STATE_DATA);*/
10098
10099         if (p_j2k->m_specific_param.m_decoder.m_state != 0x0100){ /*FIXME J2K_DEC_STATE_EOC)*/
10100                 if (opj_stream_read_data(p_stream,l_data,2,p_manager) != 2) {
10101                         opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
10102                         return OPJ_FALSE;
10103                 }
10104
10105                 opj_read_bytes(l_data,&l_current_marker,2);
10106
10107                 if (l_current_marker == J2K_MS_EOC) {
10108                         p_j2k->m_current_tile_number = 0;
10109                         p_j2k->m_specific_param.m_decoder.m_state =  0x0100;/*FIXME J2K_DEC_STATE_EOC;*/
10110                 }
10111                 else if (l_current_marker != J2K_MS_SOT)
10112                 {
10113                         opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short, expected SOT\n");
10114                         return OPJ_FALSE;
10115                 }
10116         }
10117
10118         return OPJ_TRUE;
10119 }
10120
10121
10122 opj_bool j2k_update_image_data (opj_tcd_v2_t * p_tcd, OPJ_BYTE * p_data, opj_image_t* p_output_image)
10123 {
10124         OPJ_UINT32 i,j,k = 0;
10125         OPJ_UINT32 l_width_src,l_height_src;
10126         OPJ_UINT32 l_width_dest,l_height_dest;
10127         OPJ_INT32 l_offset_x0_src, l_offset_y0_src, l_offset_x1_src, l_offset_y1_src;
10128         OPJ_INT32 l_start_offset_src, l_line_offset_src, l_end_offset_src ;
10129         OPJ_UINT32 l_start_x_dest , l_start_y_dest;
10130         OPJ_UINT32 l_x0_dest, l_y0_dest, l_x1_dest, l_y1_dest;
10131         OPJ_INT32 l_start_offset_dest, l_line_offset_dest;
10132
10133         opj_image_comp_t * l_img_comp_src = 00;
10134         opj_image_comp_t * l_img_comp_dest = 00;
10135
10136         opj_tcd_tilecomp_v2_t * l_tilec = 00;
10137         opj_image_t * l_image_src = 00;
10138         OPJ_UINT32 l_size_comp, l_remaining;
10139         OPJ_INT32 * l_dest_ptr;
10140         opj_tcd_resolution_v2_t* l_res= 00;
10141
10142         l_tilec = p_tcd->tcd_image->tiles->comps;
10143         l_image_src = p_tcd->image;
10144         l_img_comp_src = l_image_src->comps;
10145
10146         l_img_comp_dest = p_output_image->comps;
10147
10148         for (i=0; i<l_image_src->numcomps; i++) {
10149
10150                 /* Allocate output component buffer if necessary */
10151                 if (!l_img_comp_dest->data) {
10152
10153                         l_img_comp_dest->data = (OPJ_INT32*) opj_calloc(l_img_comp_dest->w * l_img_comp_dest->h, sizeof(OPJ_INT32));
10154                         if (! l_img_comp_dest->data) {
10155                                 return OPJ_FALSE;
10156                         }
10157                 }
10158
10159                 /* Copy info from decoded comp image to output image */
10160                 l_img_comp_dest->resno_decoded = l_img_comp_src->resno_decoded;
10161
10162                 /*-----*/
10163                 /* Compute the precision of the output buffer */
10164                 l_size_comp = l_img_comp_src->prec >> 3; /*(/ 8)*/
10165                 l_remaining = l_img_comp_src->prec & 7;  /* (%8) */
10166                 l_res = l_tilec->resolutions + l_img_comp_src->resno_decoded;
10167
10168                 if (l_remaining) {
10169                         ++l_size_comp;
10170                 }
10171
10172                 if (l_size_comp == 3) {
10173                         l_size_comp = 4;
10174                 }
10175                 /*-----*/
10176
10177                 /* Current tile component size*/
10178                 /*if (i == 0) {
10179                 fprintf(stdout, "SRC: l_res_x0=%d, l_res_x1=%d, l_res_y0=%d, l_res_y1=%d\n",
10180                                 l_res->x0, l_res->x1, l_res->y0, l_res->y1);
10181                 }*/
10182
10183                 l_width_src = (l_res->x1 - l_res->x0);
10184                 l_height_src = (l_res->y1 - l_res->y0);
10185
10186                 /* Border of the current output component*/
10187                 l_x0_dest = int_ceildivpow2(l_img_comp_dest->x0, l_img_comp_dest->factor);
10188                 l_y0_dest = int_ceildivpow2(l_img_comp_dest->y0, l_img_comp_dest->factor);
10189                 l_x1_dest = l_x0_dest + l_img_comp_dest->w;
10190                 l_y1_dest = l_y0_dest + l_img_comp_dest->h;
10191
10192                 /*if (i == 0) {
10193                 fprintf(stdout, "DEST: l_x0_dest=%d, l_x1_dest=%d, l_y0_dest=%d, l_y1_dest=%d (%d)\n",
10194                                 l_x0_dest, l_x1_dest, l_y0_dest, l_y1_dest, l_img_comp_dest->factor );
10195                 }*/
10196
10197                 /*-----*/
10198                 /* Compute the area (l_offset_x0_src, l_offset_y0_src, l_offset_x1_src, l_offset_y1_src)
10199                  * of the input buffer (decoded tile component) which will be move
10200                  * in the output buffer. Compute the area of the output buffer (l_start_x_dest,
10201                  * l_start_y_dest, l_width_dest, l_height_dest)  which will be modified
10202                  * by this input area.
10203                  * */
10204                 assert( l_res->x0 >= 0);
10205                 assert( l_res->x1 >= 0);
10206                 if ( l_x0_dest < (OPJ_UINT32)l_res->x0 ) {
10207                         l_start_x_dest = l_res->x0 - l_x0_dest;
10208                         l_offset_x0_src = 0;
10209
10210                         if ( l_x1_dest >= (OPJ_UINT32)l_res->x1 ) {
10211                                 l_width_dest = l_width_src;
10212                                 l_offset_x1_src = 0;
10213                         }
10214                         else {
10215                                 l_width_dest = l_x1_dest - l_res->x0 ;
10216                                 l_offset_x1_src = l_width_src - l_width_dest;
10217                         }
10218                 }
10219                 else {
10220                         l_start_x_dest = 0 ;
10221                         l_offset_x0_src = l_x0_dest - l_res->x0;
10222
10223                         if ( l_x1_dest >= (OPJ_UINT32)l_res->x1 ) {
10224                                 l_width_dest = l_width_src - l_offset_x0_src;
10225                                 l_offset_x1_src = 0;
10226                         }
10227                         else {
10228                                 l_width_dest = l_img_comp_dest->w ;
10229                                 l_offset_x1_src = l_res->x1 - l_x1_dest;
10230                         }
10231                 }
10232
10233                 if ( l_y0_dest < (OPJ_UINT32)l_res->y0 ) {
10234                         l_start_y_dest = l_res->y0 - l_y0_dest;
10235                         l_offset_y0_src = 0;
10236
10237                         if ( l_y1_dest >= (OPJ_UINT32)l_res->y1 ) {
10238                                 l_height_dest = l_height_src;
10239                                 l_offset_y1_src = 0;
10240                         }
10241                         else {
10242                                 l_height_dest = l_y1_dest - l_res->y0 ;
10243                                 l_offset_y1_src =  l_height_src - l_height_dest;
10244                         }
10245                 }
10246                 else {
10247                         l_start_y_dest = 0 ;
10248                         l_offset_y0_src = l_y0_dest - l_res->y0;
10249
10250                         if ( l_y1_dest >= (OPJ_UINT32)l_res->y1 ) {
10251                                 l_height_dest = l_height_src - l_offset_y0_src;
10252                                 l_offset_y1_src = 0;
10253                         }
10254                         else {
10255                                 l_height_dest = l_img_comp_dest->h ;
10256                                 l_offset_y1_src = l_res->y1 - l_y1_dest;
10257                         }
10258                 }
10259
10260                 if( (l_offset_x0_src < 0 ) || (l_offset_y0_src < 0 ) || (l_offset_x1_src < 0 ) || (l_offset_y1_src < 0 ) ){
10261                         return OPJ_FALSE;
10262                 }
10263                 /*-----*/
10264
10265                 /* Compute the input buffer offset */
10266                 l_start_offset_src = l_offset_x0_src + l_offset_y0_src * l_width_src;
10267                 l_line_offset_src = l_offset_x1_src + l_offset_x0_src;
10268                 l_end_offset_src = l_offset_y1_src * l_width_src - l_offset_x0_src;
10269
10270                 /* Compute the output buffer offset */
10271                 l_start_offset_dest = l_start_x_dest + l_start_y_dest * l_img_comp_dest->w;
10272                 l_line_offset_dest = l_img_comp_dest->w - l_width_dest;
10273
10274                 /* Move the output buffer to the first place where we will write*/
10275                 l_dest_ptr = l_img_comp_dest->data + l_start_offset_dest;
10276
10277                 /*if (i == 0) {
10278                         fprintf(stdout, "COMPO[%d]:\n",i);
10279                         fprintf(stdout, "SRC: l_start_x_src=%d, l_start_y_src=%d, l_width_src=%d, l_height_src=%d\n"
10280                                         "\t tile offset:%d, %d, %d, %d\n"
10281                                         "\t buffer offset: %d; %d, %d\n",
10282                                         l_res->x0, l_res->y0, l_width_src, l_height_src,
10283                                         l_offset_x0_src, l_offset_y0_src, l_offset_x1_src, l_offset_y1_src,
10284                                         l_start_offset_src, l_line_offset_src, l_end_offset_src);
10285
10286                         fprintf(stdout, "DEST: l_start_x_dest=%d, l_start_y_dest=%d, l_width_dest=%d, l_height_dest=%d\n"
10287                                         "\t start offset: %d, line offset= %d\n",
10288                                         l_start_x_dest, l_start_y_dest, l_width_dest, l_height_dest, l_start_offset_dest, l_line_offset_dest);
10289                 }*/
10290
10291
10292                 switch (l_size_comp) {
10293                         case 1:
10294                                 {
10295                                         OPJ_CHAR * l_src_ptr = (OPJ_CHAR*) p_data;
10296                                         l_src_ptr += l_start_offset_src; /* Move to the first place where we will read*/
10297
10298                                         if (l_img_comp_src->sgnd) {
10299                                                 for (j = 0 ; j < l_height_dest ; ++j) {
10300                                                         for ( k = 0 ; k < l_width_dest ; ++k) {
10301                                                                 *(l_dest_ptr++) = (OPJ_INT32) (*(l_src_ptr++)); /* Copy only the data needed for the output image */
10302                                                         }
10303
10304                                                         l_dest_ptr+= l_line_offset_dest; /* Move to the next place where we will write */
10305                                                         l_src_ptr += l_line_offset_src ; /* Move to the next place where we will read */
10306                                                 }
10307                                         }
10308                                         else {
10309                                                 for ( j = 0 ; j < l_height_dest ; ++j ) {
10310                                                         for ( k = 0 ; k < l_width_dest ; ++k) {
10311                                                                 *(l_dest_ptr++) = (OPJ_INT32) ((*(l_src_ptr++))&0xff);
10312                                                         }
10313
10314                                                         l_dest_ptr+= l_line_offset_dest;
10315                                                         l_src_ptr += l_line_offset_src;
10316                                                 }
10317                                         }
10318
10319                                         l_src_ptr += l_end_offset_src; /* Move to the end of this component-part of the input buffer */
10320                                         p_data = (OPJ_BYTE*) l_src_ptr; /* Keep the current position for the next component-part */
10321                                 }
10322                                 break;
10323                         case 2:
10324                                 {
10325                                         OPJ_INT16 * l_src_ptr = (OPJ_INT16 *) p_data;
10326                                         l_src_ptr += l_start_offset_src;
10327
10328                                         if (l_img_comp_src->sgnd) {
10329                                                 for (j=0;j<l_height_dest;++j) {
10330                                                         for (k=0;k<l_width_dest;++k) {
10331                                                                 *(l_dest_ptr++) = *(l_src_ptr++);
10332                                                         }
10333
10334                                                         l_dest_ptr+= l_line_offset_dest;
10335                                                         l_src_ptr += l_line_offset_src ;
10336                                                 }
10337                                         }
10338                                         else {
10339                                                 for (j=0;j<l_height_dest;++j) {
10340                                                         for (k=0;k<l_width_dest;++k) {
10341                                                                 *(l_dest_ptr++) = (*(l_src_ptr++))&0xffff;
10342                                                         }
10343
10344                                                         l_dest_ptr+= l_line_offset_dest;
10345                                                         l_src_ptr += l_line_offset_src ;
10346                                                 }
10347                                         }
10348
10349                                         l_src_ptr += l_end_offset_src;
10350                                         p_data = (OPJ_BYTE*) l_src_ptr;
10351                                 }
10352                                 break;
10353                         case 4:
10354                                 {
10355                                         OPJ_INT32 * l_src_ptr = (OPJ_INT32 *) p_data;
10356                                         l_src_ptr += l_start_offset_src;
10357
10358                                         for (j=0;j<l_height_dest;++j) {
10359                                                 for (k=0;k<l_width_dest;++k) {
10360                                                         *(l_dest_ptr++) = (*(l_src_ptr++));
10361                                                 }
10362
10363                                                 l_dest_ptr+= l_line_offset_dest;
10364                                                 l_src_ptr += l_line_offset_src ;
10365                                         }
10366
10367                                         l_src_ptr += l_end_offset_src;
10368                                         p_data = (OPJ_BYTE*) l_src_ptr;
10369                                 }
10370                                 break;
10371                 }
10372
10373                 ++l_img_comp_dest;
10374                 ++l_img_comp_src;
10375                 ++l_tilec;
10376         }
10377
10378         return OPJ_TRUE;
10379 }
10380
10381 /**
10382  * Sets the given area to be decoded. This function should be called right after opj_read_header and before any tile header reading.
10383  *
10384  * @param       p_j2k                   the jpeg2000 codec.
10385  * @param       p_start_x               the left position of the rectangle to decode (in image coordinates).
10386  * @param       p_end_x                 the right position of the rectangle to decode (in image coordinates).
10387  * @param       p_start_y               the up position of the rectangle to decode (in image coordinates).
10388  * @param       p_end_y                 the bottom position of the rectangle to decode (in image coordinates).
10389  * @param       p_manager               the user event manager
10390  *
10391  * @return      true                    if the area could be set.
10392  */
10393 opj_bool j2k_set_decode_area(   opj_j2k_v2_t *p_j2k,
10394                                                                 opj_image_t* p_image,
10395                                                                 OPJ_INT32 p_start_x, OPJ_INT32 p_start_y,
10396                                                                 OPJ_INT32 p_end_x, OPJ_INT32 p_end_y,
10397                                                                 struct opj_event_mgr * p_manager )
10398 {
10399         opj_cp_v2_t * l_cp = &(p_j2k->m_cp);
10400         opj_image_t * l_image = p_j2k->m_private_image;
10401
10402         OPJ_UINT32 it_comp;
10403         OPJ_INT32 l_comp_x1, l_comp_y1;
10404         opj_image_comp_t* l_img_comp = NULL;
10405
10406         /* Check if we are read the main header */
10407         if (p_j2k->m_specific_param.m_decoder.m_state != J2K_STATE_TPHSOT) { /* FIXME J2K_DEC_STATE_TPHSOT)*/
10408                 opj_event_msg_v2(p_manager, EVT_ERROR, "Need to decode the main header before begin to decode the remaining codestream");
10409                 return OPJ_FALSE;
10410         }
10411
10412         if ( !p_start_x && !p_start_y && !p_end_x && !p_end_y){
10413                 opj_event_msg_v2(p_manager, EVT_INFO, "No decoded area parameters, set the decoded area to the whole image\n");
10414
10415                 p_j2k->m_specific_param.m_decoder.m_start_tile_x = 0;
10416                 p_j2k->m_specific_param.m_decoder.m_start_tile_y = 0;
10417                 p_j2k->m_specific_param.m_decoder.m_end_tile_x = l_cp->tw;
10418                 p_j2k->m_specific_param.m_decoder.m_end_tile_y = l_cp->th;
10419
10420                 return OPJ_TRUE;
10421         }
10422
10423         /* ----- */
10424         /* Check if the positions provided by the user are correct */
10425
10426         /* Left */
10427         assert(p_start_x >= 0 );
10428         assert(p_start_y >= 0 );
10429
10430         if ((OPJ_UINT32)p_start_x > l_image->x1 ) {
10431                 opj_event_msg_v2(p_manager, EVT_ERROR,
10432                         "Left position of the decoded area (region_x0=%d) is outside the image area (Xsiz=%d).\n",
10433                         p_start_x, l_image->x1);
10434                 return OPJ_FALSE;
10435         }
10436         else if ((OPJ_UINT32)p_start_x < l_image->x0){
10437                 opj_event_msg_v2(p_manager, EVT_WARNING,
10438                                 "Left position of the decoded area (region_x0=%d) is outside the image area (XOsiz=%d).\n",
10439                                 p_start_x, l_image->x0);
10440                 p_j2k->m_specific_param.m_decoder.m_start_tile_x = 0;
10441                 p_image->x0 = l_image->x0;
10442         }
10443         else {
10444                 p_j2k->m_specific_param.m_decoder.m_start_tile_x = (p_start_x - l_cp->tx0) / l_cp->tdx;
10445                 p_image->x0 = p_start_x;
10446         }
10447
10448         /* Up */
10449         if ((OPJ_UINT32)p_start_y > l_image->y1){
10450                 opj_event_msg_v2(p_manager, EVT_ERROR,
10451                                 "Up position of the decoded area (region_y0=%d) is outside the image area (Ysiz=%d).\n",
10452                                 p_start_y, l_image->y1);
10453                 return OPJ_FALSE;
10454         }
10455         else if ((OPJ_UINT32)p_start_y < l_image->y0){
10456                 opj_event_msg_v2(p_manager, EVT_WARNING,
10457                                 "Up position of the decoded area (region_y0=%d) is outside the image area (YOsiz=%d).\n",
10458                                 p_start_y, l_image->y0);
10459                 p_j2k->m_specific_param.m_decoder.m_start_tile_y = 0;
10460                 p_image->y0 = l_image->y0;
10461         }
10462         else {
10463                 p_j2k->m_specific_param.m_decoder.m_start_tile_y = (p_start_y - l_cp->ty0) / l_cp->tdy;
10464                 p_image->y0 = p_start_y;
10465         }
10466
10467         /* Right */
10468         assert((OPJ_UINT32)p_end_x > 0);
10469         assert((OPJ_UINT32)p_end_y > 0);
10470         if ((OPJ_UINT32)p_end_x < l_image->x0) {
10471                 opj_event_msg_v2(p_manager, EVT_ERROR,
10472                         "Right position of the decoded area (region_x1=%d) is outside the image area (XOsiz=%d).\n",
10473                         p_end_x, l_image->x0);
10474                 return OPJ_FALSE;
10475         }
10476         else if ((OPJ_UINT32)p_end_x > l_image->x1) {
10477                 opj_event_msg_v2(p_manager, EVT_WARNING,
10478                         "Right position of the decoded area (region_x1=%d) is outside the image area (Xsiz=%d).\n",
10479                         p_end_x, l_image->x1);
10480                 p_j2k->m_specific_param.m_decoder.m_end_tile_x = l_cp->tw;
10481                 p_image->x1 = l_image->x1;
10482         }
10483         else {
10484                 p_j2k->m_specific_param.m_decoder.m_end_tile_x = int_ceildiv((p_end_x - l_cp->tx0), l_cp->tdx);
10485                 p_image->x1 = p_end_x;
10486         }
10487
10488         /* Bottom */
10489         if ((OPJ_UINT32)p_end_y < l_image->y0) {
10490                 opj_event_msg_v2(p_manager, EVT_ERROR,
10491                         "Bottom position of the decoded area (region_y1=%d) is outside the image area (YOsiz=%d).\n",
10492                         p_end_y, l_image->y0);
10493                 return OPJ_FALSE;
10494         }
10495         if ((OPJ_UINT32)p_end_y > l_image->y1){
10496                 opj_event_msg_v2(p_manager, EVT_WARNING,
10497                         "Bottom position of the decoded area (region_y1=%d) is outside the image area (Ysiz=%d).\n",
10498                         p_end_y, l_image->y1);
10499                 p_j2k->m_specific_param.m_decoder.m_end_tile_y = l_cp->th;
10500                 p_image->y1 = l_image->y1;
10501         }
10502         else{
10503                 p_j2k->m_specific_param.m_decoder.m_end_tile_y = int_ceildiv((p_end_y - l_cp->ty0), l_cp->tdy);
10504                 p_image->y1 = p_end_y;
10505         }
10506         /* ----- */
10507
10508         p_j2k->m_specific_param.m_decoder.m_discard_tiles = 1;
10509
10510         l_img_comp = p_image->comps;
10511         for (it_comp=0; it_comp < p_image->numcomps; ++it_comp)
10512         {
10513                 OPJ_INT32 l_h,l_w;
10514
10515                 l_img_comp->x0 = int_ceildiv(p_image->x0, l_img_comp->dx);
10516                 l_img_comp->y0 = int_ceildiv(p_image->y0, l_img_comp->dy);
10517                 l_comp_x1 = int_ceildiv(p_image->x1, l_img_comp->dx);
10518                 l_comp_y1 = int_ceildiv(p_image->y1, l_img_comp->dy);
10519
10520                 l_w = int_ceildivpow2(l_comp_x1, l_img_comp->factor)
10521                                 - int_ceildivpow2(l_img_comp->x0, l_img_comp->factor);
10522                 if (l_w < 0){
10523                         opj_event_msg_v2(p_manager, EVT_ERROR,
10524                                 "Size x of the decoded component image is incorrect (comp[%d].w=%d).\n",
10525                                 it_comp, l_w);
10526                         return OPJ_FALSE;
10527                 }
10528                 l_img_comp->w = l_w;
10529
10530                 l_h = int_ceildivpow2(l_comp_y1, l_img_comp->factor)
10531                                 - int_ceildivpow2(l_img_comp->y0, l_img_comp->factor);
10532                 if (l_h < 0){
10533                         opj_event_msg_v2(p_manager, EVT_ERROR,
10534                                 "Size y of the decoded component image is incorrect (comp[%d].h=%d).\n",
10535                                 it_comp, l_h);
10536                         return OPJ_FALSE;
10537                 }
10538                 l_img_comp->h = l_h;
10539
10540                 l_img_comp++;
10541         }
10542
10543         opj_event_msg_v2( p_manager, EVT_INFO,"Setting decoding area to %d,%d,%d,%d\n",
10544                         p_image->x0, p_image->y0, p_image->x1, p_image->y1);
10545
10546
10547         return OPJ_TRUE;
10548 }
10549
10550
10551 /* ----------------------------------------------------------------------- */
10552 /* J2K / JPT decoder interface                                             */
10553 /* ----------------------------------------------------------------------- */
10554 /**
10555  * Creates a J2K decompression structure.
10556  *
10557  * @return a handle to a J2K decompressor if successful, NULL otherwise.
10558 */
10559 opj_j2k_v2_t* j2k_create_decompress_v2(void)
10560 {
10561         opj_j2k_v2_t *l_j2k = (opj_j2k_v2_t*) opj_malloc(sizeof(opj_j2k_v2_t));
10562         if (!l_j2k) {
10563                 return 00;
10564         }
10565         memset(l_j2k,0,sizeof(opj_j2k_v2_t));
10566
10567         l_j2k->m_is_decoder = 1;
10568         l_j2k->m_cp.m_is_decoder = 1;
10569
10570         l_j2k->m_specific_param.m_decoder.m_default_tcp = (opj_tcp_v2_t*) opj_malloc(sizeof(opj_tcp_v2_t));
10571         if (!l_j2k->m_specific_param.m_decoder.m_default_tcp) {
10572                 j2k_destroy(l_j2k);
10573                 return 00;
10574         }
10575         memset(l_j2k->m_specific_param.m_decoder.m_default_tcp,0,sizeof(opj_tcp_v2_t));
10576
10577         l_j2k->m_specific_param.m_decoder.m_header_data = (OPJ_BYTE *) opj_malloc(J2K_DEFAULT_HEADER_SIZE);
10578         if (! l_j2k->m_specific_param.m_decoder.m_header_data) {
10579                 j2k_destroy(l_j2k);
10580                 return 00;
10581         }
10582
10583         l_j2k->m_specific_param.m_decoder.m_header_data_size = J2K_DEFAULT_HEADER_SIZE;
10584
10585         l_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec = -1 ;
10586
10587         l_j2k->m_specific_param.m_decoder.m_last_sot_read_pos = 0 ;
10588
10589         /* codestream index creation */
10590         l_j2k->cstr_index = j2k_create_cstr_index();
10591
10592                         /*(opj_codestream_index_t*) opj_malloc(sizeof(opj_codestream_index_t));
10593         if (!l_j2k->cstr_index){
10594                 j2k_destroy(l_j2k);
10595                 return NULL;
10596         }
10597
10598         l_j2k->cstr_index->marker = (opj_marker_info_t*) opj_malloc(100 * sizeof(opj_marker_info_t));
10599 */
10600
10601         /* validation list creation */
10602         l_j2k->m_validation_list = opj_procedure_list_create();
10603         if (! l_j2k->m_validation_list) {
10604                 j2k_destroy(l_j2k);
10605                 return 00;
10606         }
10607
10608         /* execution list creation */
10609         l_j2k->m_procedure_list = opj_procedure_list_create();
10610         if (! l_j2k->m_procedure_list) {
10611                 j2k_destroy(l_j2k);
10612                 return 00;
10613         }
10614
10615         return l_j2k;
10616 }
10617
10618
10619 opj_codestream_index_t* j2k_create_cstr_index(void)
10620 {
10621         opj_codestream_index_t* cstr_index = (opj_codestream_index_t*)
10622                         opj_calloc(1,sizeof(opj_codestream_index_t));
10623         if (!cstr_index)
10624                 return NULL;
10625
10626         cstr_index->maxmarknum = 100;
10627         cstr_index->marknum = 0;
10628         cstr_index->marker = (opj_marker_info_t*)
10629                         opj_calloc(cstr_index->maxmarknum, sizeof(opj_marker_info_t));
10630         if (!cstr_index-> marker)
10631                 return NULL;
10632
10633         cstr_index->tile_index = NULL;
10634
10635         return cstr_index;
10636 }
10637
10638
10639 /**
10640  * Gets the size taken by writing a SPCod or SPCoc for the given tile and component.
10641  *
10642  * @param       p_tile_no               the tile index.
10643  * @param       p_comp_no               the component being outputted.
10644  * @param       p_j2k                   the J2K codec.
10645  *
10646  * @return      the number of bytes taken by the SPCod element.
10647  */
10648 OPJ_UINT32 j2k_get_SPCod_SPCoc_size (   opj_j2k_v2_t *p_j2k,
10649                                                                                 OPJ_UINT32 p_tile_no,
10650                                                                                 OPJ_UINT32 p_comp_no )
10651 {
10652         opj_cp_v2_t *l_cp = 00;
10653         opj_tcp_v2_t *l_tcp = 00;
10654         opj_tccp_t *l_tccp = 00;
10655
10656         /* preconditions */
10657         assert(p_j2k != 00);
10658
10659         l_cp = &(p_j2k->m_cp);
10660         l_tcp = &l_cp->tcps[p_tile_no];
10661         l_tccp = &l_tcp->tccps[p_comp_no];
10662
10663         /* preconditions again */
10664         assert(p_tile_no < (l_cp->tw * l_cp->th));
10665         assert(p_comp_no < p_j2k->m_private_image->numcomps);
10666
10667         if (l_tccp->csty & J2K_CCP_CSTY_PRT) {
10668                 return 5 + l_tccp->numresolutions;
10669         }
10670         else {
10671                 return 5;
10672         }
10673 }
10674
10675 /**
10676  * Writes a SPCod or SPCoc element, i.e. the coding style of a given component of a tile.
10677  *
10678  * @param       p_comp_no       the component number to output.
10679  * @param       p_stream                        the stream to write data to.
10680  * @param       p_j2k                   J2K codec.
10681  * @param       p_manager       the user event manager.
10682  *
10683 */
10684 opj_bool j2k_write_SPCod_SPCoc( opj_j2k_v2_t *p_j2k,
10685                                                                 OPJ_UINT32 p_tile_no,
10686                                                                 OPJ_UINT32 p_comp_no,
10687                                                                 OPJ_BYTE * p_data,
10688                                                                 OPJ_UINT32 * p_header_size,
10689                                                                 struct opj_event_mgr * p_manager )
10690 {
10691         OPJ_UINT32 i;
10692         opj_cp_v2_t *l_cp = 00;
10693         opj_tcp_v2_t *l_tcp = 00;
10694         opj_tccp_t *l_tccp = 00;
10695
10696         /* preconditions */
10697         assert(p_j2k != 00);
10698         assert(p_header_size != 00);
10699         assert(p_manager != 00);
10700         assert(p_data != 00);
10701
10702         l_cp = &(p_j2k->m_cp);
10703         l_tcp = &l_cp->tcps[p_tile_no];
10704         l_tccp = &l_tcp->tccps[p_comp_no];
10705
10706         /* preconditions again */
10707         assert(p_tile_no < (l_cp->tw * l_cp->th));
10708         assert(p_comp_no <(p_j2k->m_private_image->numcomps));
10709
10710         if (*p_header_size < 5) {
10711                 opj_event_msg_v2(p_manager, EVT_ERROR, "Error writing SPCod SPCoc element\n");
10712                 return OPJ_FALSE;
10713         }
10714
10715         opj_write_bytes(p_data,l_tccp->numresolutions - 1, 1);  /* SPcoc (D) */
10716         ++p_data;
10717
10718         opj_write_bytes(p_data,l_tccp->cblkw - 2, 1);                   /* SPcoc (E) */
10719         ++p_data;
10720
10721         opj_write_bytes(p_data,l_tccp->cblkh - 2, 1);                   /* SPcoc (F) */
10722         ++p_data;
10723
10724         opj_write_bytes(p_data,l_tccp->cblksty, 1);                             /* SPcoc (G) */
10725         ++p_data;
10726
10727         opj_write_bytes(p_data,l_tccp->qmfbid, 1);                              /* SPcoc (H) */
10728         ++p_data;
10729
10730         *p_header_size = *p_header_size - 5;
10731
10732         if (l_tccp->csty & J2K_CCP_CSTY_PRT) {
10733
10734                 if (*p_header_size < l_tccp->numresolutions) {
10735                         opj_event_msg_v2(p_manager, EVT_ERROR, "Error writting SPCod SPCoc element\n");
10736                         return OPJ_FALSE;
10737                 }
10738
10739                 for (i = 0; i < l_tccp->numresolutions; ++i) {
10740                         opj_write_bytes(p_data,l_tccp->prcw[i] + (l_tccp->prch[i] << 4), 1);    /* SPcoc (I_i) */
10741                         ++p_data;
10742                 }
10743
10744                 *p_header_size = *p_header_size - l_tccp->numresolutions;
10745         }
10746
10747         return OPJ_TRUE;
10748 }
10749
10750 /**
10751  * Reads a SPCod or SPCoc element, i.e. the coding style of a given component of a tile.
10752  * @param       p_header_data   the data contained in the COM box.
10753  * @param       p_j2k                   the jpeg2000 codec.
10754  * @param       p_header_size   the size of the data contained in the COM marker.
10755  * @param       p_manager               the user event manager.
10756 */
10757 opj_bool j2k_read_SPCod_SPCoc(
10758                                                     opj_j2k_v2_t *p_j2k,
10759                                                         OPJ_UINT32 compno,
10760                                                         OPJ_BYTE * p_header_data,
10761                                                         OPJ_UINT32 * p_header_size,
10762                                                         struct opj_event_mgr * p_manager
10763                                                         )
10764 {
10765         OPJ_UINT32 i, l_tmp;
10766         opj_cp_v2_t *l_cp = NULL;
10767         opj_tcp_v2_t *l_tcp = NULL;
10768         opj_tccp_t *l_tccp = NULL;
10769         OPJ_BYTE * l_current_ptr = NULL;
10770
10771         /* preconditions */
10772         assert(p_j2k != 00);
10773         assert(p_manager != 00);
10774         assert(p_header_data != 00);
10775
10776         l_cp = &(p_j2k->m_cp);
10777         l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ?
10778                                 &l_cp->tcps[p_j2k->m_current_tile_number] :
10779                                 p_j2k->m_specific_param.m_decoder.m_default_tcp;
10780
10781         /* precondition again */
10782         assert(compno < p_j2k->m_private_image->numcomps);
10783
10784         l_tccp = &l_tcp->tccps[compno];
10785         l_current_ptr = p_header_data;
10786
10787         /* make sure room is sufficient */
10788         if (*p_header_size < 5) {
10789                 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading SPCod SPCoc element\n");
10790                 return OPJ_FALSE;
10791         }
10792
10793         opj_read_bytes(l_current_ptr, &l_tccp->numresolutions ,1);              /* SPcox (D) */
10794         ++l_tccp->numresolutions;                                                                               /* tccp->numresolutions = read() + 1 */
10795         ++l_current_ptr;
10796
10797         /* If user wants to remove more resolutions than the codestream contains, return error */
10798         if (l_cp->m_specific_param.m_dec.m_reduce >= l_tccp->numresolutions) {
10799                 opj_event_msg_v2(p_manager, EVT_ERROR, "Error decoding component %d.\nThe number of resolutions to remove is higher than the number "
10800                                         "of resolutions of this component\nModify the cp_reduce parameter.\n\n", compno);
10801                 p_j2k->m_specific_param.m_decoder.m_state |= 0x8000;/* FIXME J2K_DEC_STATE_ERR;*/
10802                 return OPJ_FALSE;
10803         }
10804
10805         opj_read_bytes(l_current_ptr,&l_tccp->cblkw ,1);                /* SPcoc (E) */
10806         ++l_current_ptr;
10807         l_tccp->cblkw += 2;
10808
10809         opj_read_bytes(l_current_ptr,&l_tccp->cblkh ,1);                /* SPcoc (F) */
10810         ++l_current_ptr;
10811         l_tccp->cblkh += 2;
10812
10813         opj_read_bytes(l_current_ptr,&l_tccp->cblksty ,1);              /* SPcoc (G) */
10814         ++l_current_ptr;
10815
10816         opj_read_bytes(l_current_ptr,&l_tccp->qmfbid ,1);               /* SPcoc (H) */
10817         ++l_current_ptr;
10818
10819         *p_header_size = *p_header_size - 5;
10820
10821         /* use custom precinct size ? */
10822         if (l_tccp->csty & J2K_CCP_CSTY_PRT) {
10823                 if (*p_header_size < l_tccp->numresolutions) {
10824                         opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading SPCod SPCoc element\n");
10825                         return OPJ_FALSE;
10826                 }
10827
10828                 for     (i = 0; i < l_tccp->numresolutions; ++i) {
10829                         opj_read_bytes(l_current_ptr,&l_tmp ,1);                /* SPcoc (I_i) */
10830                         ++l_current_ptr;
10831                         l_tccp->prcw[i] = l_tmp & 0xf;
10832                         l_tccp->prch[i] = l_tmp >> 4;
10833                 }
10834
10835                 *p_header_size = *p_header_size - l_tccp->numresolutions;
10836         }
10837         else {
10838                 /* set default size for the precinct width and height */
10839                 for     (i = 0; i < l_tccp->numresolutions; ++i) {
10840                         l_tccp->prcw[i] = 15;
10841                         l_tccp->prch[i] = 15;
10842                 }
10843         }
10844
10845 #ifdef WIP_REMOVE_MSD
10846         /* INDEX >> */
10847         if (p_j2k->cstr_info && compno == 0) {
10848                 OPJ_UINT32 l_data_size = l_tccp->numresolutions * sizeof(OPJ_UINT32);
10849
10850                 p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].tccp_info[compno].cblkh = l_tccp->cblkh;
10851                 p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].tccp_info[compno].cblkw = l_tccp->cblkw;
10852                 p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].tccp_info[compno].numresolutions = l_tccp->numresolutions;
10853                 p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].tccp_info[compno].cblksty = l_tccp->cblksty;
10854                 p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].tccp_info[compno].qmfbid = l_tccp->qmfbid;
10855
10856
10857                 memcpy(p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].pdx,l_tccp->prcw, l_data_size);
10858                 memcpy(p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].pdy,l_tccp->prch, l_data_size);
10859         }
10860         /* << INDEX */
10861 #endif
10862
10863         return OPJ_TRUE;
10864 }
10865
10866 /**
10867  * Copies the tile component parameters of all the component from the first tile component.
10868  *
10869  * @param               p_j2k           the J2k codec.
10870  */
10871 void j2k_copy_tile_component_parameters( opj_j2k_v2_t *p_j2k )
10872 {
10873         /* loop */
10874         OPJ_UINT32 i;
10875         opj_cp_v2_t *l_cp = NULL;
10876         opj_tcp_v2_t *l_tcp = NULL;
10877         opj_tccp_t *l_ref_tccp = NULL, *l_copied_tccp = NULL;
10878         OPJ_UINT32 l_prc_size;
10879
10880         /* preconditions */
10881         assert(p_j2k != 00);
10882
10883         l_cp = &(p_j2k->m_cp);
10884         l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ? /* FIXME J2K_DEC_STATE_TPH*/
10885                                 &l_cp->tcps[p_j2k->m_current_tile_number] :
10886                                 p_j2k->m_specific_param.m_decoder.m_default_tcp;
10887
10888         l_ref_tccp = &l_tcp->tccps[0];
10889         l_copied_tccp = l_ref_tccp + 1;
10890         l_prc_size = l_ref_tccp->numresolutions * sizeof(OPJ_UINT32);
10891
10892         for     (i=1; i<p_j2k->m_private_image->numcomps; ++i) {
10893                 l_copied_tccp->numresolutions = l_ref_tccp->numresolutions;
10894                 l_copied_tccp->cblkw = l_ref_tccp->cblkw;
10895                 l_copied_tccp->cblkh = l_ref_tccp->cblkh;
10896                 l_copied_tccp->cblksty = l_ref_tccp->cblksty;
10897                 l_copied_tccp->qmfbid = l_ref_tccp->qmfbid;
10898                 memcpy(l_copied_tccp->prcw,l_ref_tccp->prcw,l_prc_size);
10899                 memcpy(l_copied_tccp->prch,l_ref_tccp->prch,l_prc_size);
10900                 ++l_copied_tccp;
10901         }
10902 }
10903
10904 /**
10905  * Gets the size taken by writing SQcd or SQcc element, i.e. the quantization values of a band in the QCD or QCC.
10906  *
10907  * @param       p_tile_no               the tile index.
10908  * @param       p_comp_no               the component being outputted.
10909  * @param       p_j2k                   the J2K codec.
10910  *
10911  * @return      the number of bytes taken by the SPCod element.
10912  */
10913 OPJ_UINT32 j2k_get_SQcd_SQcc_size (     opj_j2k_v2_t *p_j2k,
10914                                                                         OPJ_UINT32 p_tile_no,
10915                                                                         OPJ_UINT32 p_comp_no )
10916 {
10917         OPJ_UINT32 l_num_bands;
10918
10919         opj_cp_v2_t *l_cp = 00;
10920         opj_tcp_v2_t *l_tcp = 00;
10921         opj_tccp_t *l_tccp = 00;
10922
10923         /* preconditions */
10924         assert(p_j2k != 00);
10925
10926         l_cp = &(p_j2k->m_cp);
10927         l_tcp = &l_cp->tcps[p_tile_no];
10928         l_tccp = &l_tcp->tccps[p_comp_no];
10929
10930         /* preconditions again */
10931         assert(p_tile_no < l_cp->tw * l_cp->th);
10932         assert(p_comp_no < p_j2k->m_private_image->numcomps);
10933
10934         l_num_bands = (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) ? 1 : (l_tccp->numresolutions * 3 - 2);
10935
10936         if (l_tccp->qntsty == J2K_CCP_QNTSTY_NOQNT)  {
10937                 return 1 + l_num_bands;
10938         }
10939         else {
10940                 return 1 + 2*l_num_bands;
10941         }
10942 }
10943
10944 /**
10945  * Writes a SQcd or SQcc element, i.e. the quantization values of a band.
10946  *
10947  * @param       p_tile_no               the tile to output.
10948  * @param       p_comp_no               the component number to output.
10949  * @param       p_data                  the data buffer.
10950  * @param       p_header_size   pointer to the size of the data buffer, it is changed by the function.
10951  * @param       p_j2k                           J2K codec.
10952  * @param       p_manager               the user event manager.
10953  *
10954 */
10955 opj_bool j2k_write_SQcd_SQcc(   opj_j2k_v2_t *p_j2k,
10956                                                                 OPJ_UINT32 p_tile_no,
10957                                                                 OPJ_UINT32 p_comp_no,
10958                                                                 OPJ_BYTE * p_data,
10959                                                                 OPJ_UINT32 * p_header_size,
10960                                                                 struct opj_event_mgr * p_manager )
10961 {
10962         OPJ_UINT32 l_header_size;
10963         OPJ_UINT32 l_band_no, l_num_bands;
10964         OPJ_UINT32 l_expn,l_mant;
10965
10966         opj_cp_v2_t *l_cp = 00;
10967         opj_tcp_v2_t *l_tcp = 00;
10968         opj_tccp_t *l_tccp = 00;
10969
10970         /* preconditions */
10971         assert(p_j2k != 00);
10972         assert(p_header_size != 00);
10973         assert(p_manager != 00);
10974         assert(p_data != 00);
10975
10976         l_cp = &(p_j2k->m_cp);
10977         l_tcp = &l_cp->tcps[p_tile_no];
10978         l_tccp = &l_tcp->tccps[p_comp_no];
10979
10980         /* preconditions again */
10981         assert(p_tile_no < l_cp->tw * l_cp->th);
10982         assert(p_comp_no <p_j2k->m_private_image->numcomps);
10983
10984         l_num_bands = (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) ? 1 : (l_tccp->numresolutions * 3 - 2);
10985
10986         if (l_tccp->qntsty == J2K_CCP_QNTSTY_NOQNT)  {
10987                 l_header_size = 1 + l_num_bands;
10988
10989                 if (*p_header_size < l_header_size) {
10990                         opj_event_msg_v2(p_manager, EVT_ERROR, "Error writing SQcd SQcc element\n");
10991                         return OPJ_FALSE;
10992                 }
10993
10994                 opj_write_bytes(p_data,l_tccp->qntsty + (l_tccp->numgbits << 5), 1);    /* Sqcx */
10995                 ++p_data;
10996
10997                 for (l_band_no = 0; l_band_no < l_num_bands; ++l_band_no) {
10998                         l_expn = l_tccp->stepsizes[l_band_no].expn;
10999                         opj_write_bytes(p_data, l_expn << 3, 1);        /* SPqcx_i */
11000                         ++p_data;
11001                 }
11002         }
11003         else {
11004                 l_header_size = 1 + 2*l_num_bands;
11005
11006                 if (*p_header_size < l_header_size) {
11007                         opj_event_msg_v2(p_manager, EVT_ERROR, "Error writing SQcd SQcc element\n");
11008                         return OPJ_FALSE;
11009                 }
11010
11011                 opj_write_bytes(p_data,l_tccp->qntsty + (l_tccp->numgbits << 5), 1);    /* Sqcx */
11012                 ++p_data;
11013
11014                 for (l_band_no = 0; l_band_no < l_num_bands; ++l_band_no) {
11015                         l_expn = l_tccp->stepsizes[l_band_no].expn;
11016                         l_mant = l_tccp->stepsizes[l_band_no].mant;
11017
11018                         opj_write_bytes(p_data, (l_expn << 11) + l_mant, 2);    /* SPqcx_i */
11019                         p_data += 2;
11020                 }
11021         }
11022
11023         *p_header_size = *p_header_size - l_header_size;
11024
11025         return OPJ_TRUE;
11026 }
11027
11028 /**
11029  * Reads a SQcd or SQcc element, i.e. the quantization values of a band.
11030  *
11031  * @param       p_comp_no               the component being targeted.
11032  * @param       p_header_data   the data contained in the COM box.
11033  * @param       p_j2k                   the jpeg2000 codec.
11034  * @param       p_header_size   the size of the data contained in the COM marker.
11035  * @param       p_manager               the user event manager.
11036 */
11037 opj_bool j2k_read_SQcd_SQcc(
11038                                                         opj_j2k_v2_t *p_j2k,
11039                                                         OPJ_UINT32 p_comp_no,
11040                                                         OPJ_BYTE* p_header_data,
11041                                                         OPJ_UINT32 * p_header_size,
11042                                                         struct opj_event_mgr * p_manager
11043                                                         )
11044 {
11045         /* loop*/
11046         OPJ_UINT32 l_band_no;
11047         opj_cp_v2_t *l_cp = 00;
11048         opj_tcp_v2_t *l_tcp = 00;
11049         opj_tccp_t *l_tccp = 00;
11050         OPJ_BYTE * l_current_ptr = 00;
11051         OPJ_UINT32 l_tmp, l_num_band;
11052
11053         /* preconditions*/
11054         assert(p_j2k != 00);
11055         assert(p_manager != 00);
11056         assert(p_header_data != 00);
11057
11058         l_cp = &(p_j2k->m_cp);
11059         /* come from tile part header or main header ?*/
11060         l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ? /*FIXME J2K_DEC_STATE_TPH*/
11061                                 &l_cp->tcps[p_j2k->m_current_tile_number] :
11062                                 p_j2k->m_specific_param.m_decoder.m_default_tcp;
11063
11064         /* precondition again*/
11065         assert(p_comp_no <  p_j2k->m_private_image->numcomps);
11066
11067         l_tccp = &l_tcp->tccps[p_comp_no];
11068         l_current_ptr = p_header_data;
11069
11070         if (*p_header_size < 1) {
11071                 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading SQcd or SQcc element\n");
11072                 return OPJ_FALSE;
11073         }
11074         *p_header_size -= 1;
11075
11076         opj_read_bytes(l_current_ptr, &l_tmp ,1);                       /* Sqcx */
11077         ++l_current_ptr;
11078
11079         l_tccp->qntsty = l_tmp & 0x1f;
11080         l_tccp->numgbits = l_tmp >> 5;
11081         if (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) {
11082         l_num_band = 1;
11083         }
11084         else {
11085                 l_num_band = (l_tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) ?
11086                         (*p_header_size) :
11087                         (*p_header_size) / 2;
11088
11089                 if( l_num_band > J2K_MAXBANDS ) {
11090                         opj_event_msg_v2(p_manager, EVT_WARNING, "While reading CCP_QNTSTY element inside QCD or QCC marker segment, "
11091                                 "number of subbands (%d) is greater to J2K_MAXBANDS (%d). So we limit the number of elements stored to "
11092                                 "J2K_MAXBANDS (%d) and skip the rest. \n", l_num_band, J2K_MAXBANDS, J2K_MAXBANDS);
11093                         /*return OPJ_FALSE;*/
11094                 }
11095         }
11096
11097 #ifdef USE_JPWL
11098         if (l_cp->correct) {
11099
11100                 /* if JPWL is on, we check whether there are too many subbands */
11101                 if (/*(l_num_band < 0) ||*/ (l_num_band >= J2K_MAXBANDS)) {
11102                         opj_event_msg_v2(p_manager, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
11103                                 "JPWL: bad number of subbands in Sqcx (%d)\n",
11104                                 l_num_band);
11105                         if (!JPWL_ASSUME) {
11106                                 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
11107                                 return OPJ_FALSE;
11108                         }
11109                         /* we try to correct */
11110                         l_num_band = 1;
11111                         opj_event_msg_v2(p_manager, EVT_WARNING, "- trying to adjust them\n"
11112                                 "- setting number of bands to %d => HYPOTHESIS!!!\n",
11113                                 l_num_band);
11114                 };
11115
11116         };
11117 #endif /* USE_JPWL */
11118
11119         if (l_tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) {
11120                 for     (l_band_no = 0; l_band_no < l_num_band; l_band_no++) {
11121                         opj_read_bytes(l_current_ptr, &l_tmp ,1);                       /* SPqcx_i */
11122                         ++l_current_ptr;
11123                         if (l_band_no < J2K_MAXBANDS){
11124                                 l_tccp->stepsizes[l_band_no].expn = l_tmp>>3;
11125                                 l_tccp->stepsizes[l_band_no].mant = 0;
11126                         }
11127                 }
11128                 *p_header_size = *p_header_size - l_num_band;
11129         }
11130         else {
11131                 for     (l_band_no = 0; l_band_no < l_num_band; l_band_no++) {
11132                         opj_read_bytes(l_current_ptr, &l_tmp ,2);                       /* SPqcx_i */
11133                         l_current_ptr+=2;
11134                         if (l_band_no < J2K_MAXBANDS){
11135                                 l_tccp->stepsizes[l_band_no].expn = l_tmp >> 11;
11136                                 l_tccp->stepsizes[l_band_no].mant = l_tmp & 0x7ff;
11137                         }
11138                 }
11139                 *p_header_size = *p_header_size - 2*l_num_band;
11140         }
11141
11142         /* Add Antonin : if scalar_derived -> compute other stepsizes */
11143         if (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) {
11144                 for (l_band_no = 1; l_band_no < J2K_MAXBANDS; l_band_no++) {
11145                         l_tccp->stepsizes[l_band_no].expn =
11146                                 ((l_tccp->stepsizes[0].expn) - ((l_band_no - 1) / 3) > 0) ?
11147                                         (l_tccp->stepsizes[0].expn) - ((l_band_no - 1) / 3) : 0;
11148                         l_tccp->stepsizes[l_band_no].mant = l_tccp->stepsizes[0].mant;
11149                 }
11150         }
11151
11152         return OPJ_TRUE;
11153 }
11154
11155 /**
11156  * Copies the tile component parameters of all the component from the first tile component.
11157  *
11158  * @param               p_j2k           the J2k codec.
11159  */
11160 void j2k_copy_tile_quantization_parameters( opj_j2k_v2_t *p_j2k )
11161 {
11162         OPJ_UINT32 i;
11163         opj_cp_v2_t *l_cp = NULL;
11164         opj_tcp_v2_t *l_tcp = NULL;
11165         opj_tccp_t *l_ref_tccp = NULL;
11166         opj_tccp_t *l_copied_tccp = NULL;
11167         OPJ_UINT32 l_size;
11168
11169         /* preconditions */
11170         assert(p_j2k != 00);
11171
11172         l_cp = &(p_j2k->m_cp);
11173         l_tcp = p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH ?
11174                         &l_cp->tcps[p_j2k->m_current_tile_number] :
11175                         p_j2k->m_specific_param.m_decoder.m_default_tcp;
11176
11177         l_ref_tccp = &l_tcp->tccps[0];
11178         l_copied_tccp = l_ref_tccp + 1;
11179         l_size = J2K_MAXBANDS * sizeof(opj_stepsize_t);
11180
11181         for     (i=1;i<p_j2k->m_private_image->numcomps;++i) {
11182                 l_copied_tccp->qntsty = l_ref_tccp->qntsty;
11183                 l_copied_tccp->numgbits = l_ref_tccp->numgbits;
11184                 memcpy(l_copied_tccp->stepsizes,l_ref_tccp->stepsizes,l_size);
11185                 ++l_copied_tccp;
11186         }
11187 }
11188
11189 /**
11190  * Dump some elements from the J2K decompression structure .
11191  *
11192  *@param p_j2k                          the jpeg2000 codec.
11193  *@param flag                           flag to describe what elments are dump.
11194  *@param out_stream                     output stream where dump the elements.
11195  *
11196 */
11197 void j2k_dump (opj_j2k_v2_t* p_j2k, OPJ_INT32 flag, FILE* out_stream)
11198 {
11199         /* Check if the flag is compatible with j2k file*/
11200         if ( (flag & OPJ_JP2_INFO) || (flag & OPJ_JP2_IND)){
11201                 fprintf(out_stream, "Wrong flag\n");
11202                 return;
11203         }
11204
11205         /* Dump the image_header */
11206         if (flag & OPJ_IMG_INFO){
11207                 if (p_j2k->m_private_image)
11208                         j2k_dump_image_header(p_j2k->m_private_image, 0, out_stream);
11209         }
11210
11211         /* Dump the codestream info from main header */
11212         if (flag & OPJ_J2K_MH_INFO){
11213                 j2k_dump_MH_info(p_j2k, out_stream);
11214         }
11215
11216
11217         /* Dump the codestream info of the current tile */
11218         if (flag & OPJ_J2K_TH_INFO){
11219
11220         }
11221
11222         /* Dump the codestream index from main header */
11223         if (flag & OPJ_J2K_MH_IND){
11224                 j2k_dump_MH_index(p_j2k, out_stream);
11225         }
11226
11227         /* Dump the codestream index of the current tile */
11228         if (flag & OPJ_J2K_TH_IND){
11229
11230         }
11231
11232 }
11233
11234 /**
11235  * Dump index elements of the codestream extract from the main header.
11236  *
11237  *@param p_j2k                          the jpeg2000 codec.
11238  *@param out_stream                     output stream where dump the elements.
11239  *
11240 */
11241 void j2k_dump_MH_index(opj_j2k_v2_t* p_j2k, FILE* out_stream)
11242 {
11243         opj_codestream_index_t* cstr_index = p_j2k->cstr_index;
11244         OPJ_UINT32 it_marker, it_tile, it_tile_part;
11245
11246         fprintf(out_stream, "Codestream index from main header: {\n");
11247
11248         fprintf(out_stream, "\t Main header start position=%" PRIi64 "\n"
11249                                     "\t Main header end position=%" PRIi64 "\n",
11250                         cstr_index->main_head_start, cstr_index->main_head_end);
11251
11252         fprintf(out_stream, "\t Marker list: {\n");
11253
11254         if (cstr_index->marker){
11255                 for (it_marker=0; it_marker < cstr_index->marknum ; it_marker++){
11256                         fprintf(out_stream, "\t\t type=%#x, pos=%" PRIi64 ", len=%d\n",
11257                                         cstr_index->marker[it_marker].type,
11258                                         cstr_index->marker[it_marker].pos,
11259                                         cstr_index->marker[it_marker].len );
11260                 }
11261         }
11262
11263         fprintf(out_stream, "\t }\n");
11264
11265
11266         if (cstr_index->tile_index){
11267
11268         /* Simple test to avoid to write empty information*/
11269         OPJ_UINT32 l_acc_nb_of_tile_part = 0;
11270         for (it_tile=0; it_tile < cstr_index->nb_of_tiles ; it_tile++){
11271                         l_acc_nb_of_tile_part += cstr_index->tile_index[it_tile].nb_tps;
11272         }
11273
11274         if (l_acc_nb_of_tile_part)
11275         {
11276             fprintf(out_stream, "\t Tile index: {\n");
11277        
11278                     for (it_tile=0; it_tile < cstr_index->nb_of_tiles ; it_tile++){
11279                             OPJ_UINT32 nb_of_tile_part = cstr_index->tile_index[it_tile].nb_tps;
11280
11281                             fprintf(out_stream, "\t\t nb of tile-part in tile [%d]=%d\n", it_tile, nb_of_tile_part);
11282
11283                             if (cstr_index->tile_index[it_tile].tp_index){
11284                                     for (it_tile_part =0; it_tile_part < nb_of_tile_part; it_tile_part++){
11285                                             fprintf(out_stream, "\t\t\t tile-part[%d]: star_pos=%" PRIi64 ", end_header=%" PRIi64 ", end_pos=%" PRIi64 ".\n",
11286                                                             it_tile_part,
11287                                                             cstr_index->tile_index[it_tile].tp_index[it_tile_part].start_pos,
11288                                                             cstr_index->tile_index[it_tile].tp_index[it_tile_part].end_header,
11289                                                             cstr_index->tile_index[it_tile].tp_index[it_tile_part].end_pos);
11290                                     }
11291                             }
11292
11293                             if (cstr_index->tile_index[it_tile].marker){
11294                                     for (it_marker=0; it_marker < cstr_index->tile_index[it_tile].marknum ; it_marker++){
11295                                             fprintf(out_stream, "\t\t type=%#x, pos=%" PRIi64 ", len=%d\n",
11296                                                             cstr_index->tile_index[it_tile].marker[it_marker].type,
11297                                                             cstr_index->tile_index[it_tile].marker[it_marker].pos,
11298                                                             cstr_index->tile_index[it_tile].marker[it_marker].len );
11299                                     }
11300                             }
11301                     }
11302                     fprintf(out_stream,"\t }\n");
11303         }
11304         }
11305
11306         fprintf(out_stream,"}\n");
11307
11308 }
11309
11310 /**
11311  * Dump info elements of the codestream extract from the main header.
11312  *
11313  *@param p_j2k                          the jpeg2000 codec.
11314  *@param out_stream                     output stream where dump the elements.
11315  *
11316 */
11317 void j2k_dump_MH_info(opj_j2k_v2_t* p_j2k, FILE* out_stream)
11318 {
11319         opj_tcp_v2_t * l_default_tile=NULL;
11320
11321         fprintf(out_stream, "Codestream info from main header: {\n");
11322
11323         fprintf(out_stream, "\t tx0=%d, ty0=%d\n", p_j2k->m_cp.tx0, p_j2k->m_cp.ty0);
11324         fprintf(out_stream, "\t tdx=%d, tdy=%d\n", p_j2k->m_cp.tdx, p_j2k->m_cp.tdy);
11325         fprintf(out_stream, "\t tw=%d, th=%d\n", p_j2k->m_cp.tw, p_j2k->m_cp.th);
11326
11327         l_default_tile = p_j2k->m_specific_param.m_decoder.m_default_tcp;
11328         if (l_default_tile)
11329         {
11330                 OPJ_INT32 compno;
11331                 OPJ_INT32 numcomps = p_j2k->m_private_image->numcomps;
11332
11333                 fprintf(out_stream, "\t default tile {\n");
11334                 fprintf(out_stream, "\t\t csty=%#x\n", l_default_tile->csty);
11335                 fprintf(out_stream, "\t\t prg=%#x\n", l_default_tile->prg);
11336                 fprintf(out_stream, "\t\t numlayers=%d\n", l_default_tile->numlayers);
11337                 fprintf(out_stream, "\t\t mct=%x\n", l_default_tile->mct);
11338
11339                 for (compno = 0; compno < numcomps; compno++) {
11340                         opj_tccp_t *l_tccp = &(l_default_tile->tccps[compno]);
11341                         OPJ_UINT32 resno;
11342       OPJ_INT32 bandno, numbands;
11343
11344                         /* coding style*/
11345                         fprintf(out_stream, "\t\t comp %d {\n", compno);
11346                         fprintf(out_stream, "\t\t\t csty=%#x\n", l_tccp->csty);
11347                         fprintf(out_stream, "\t\t\t numresolutions=%d\n", l_tccp->numresolutions);
11348                         fprintf(out_stream, "\t\t\t cblkw=2^%d\n", l_tccp->cblkw);
11349                         fprintf(out_stream, "\t\t\t cblkh=2^%d\n", l_tccp->cblkh);
11350                         fprintf(out_stream, "\t\t\t cblksty=%#x\n", l_tccp->cblksty);
11351                         fprintf(out_stream, "\t\t\t qmfbid=%d\n", l_tccp->qmfbid);
11352
11353                         fprintf(out_stream, "\t\t\t preccintsize (w,h)=");
11354                         for (resno = 0; resno < l_tccp->numresolutions; resno++) {
11355                                 fprintf(out_stream, "(%d,%d) ", l_tccp->prcw[resno], l_tccp->prch[resno]);
11356                         }
11357                         fprintf(out_stream, "\n");
11358
11359                         /* quantization style*/
11360                         fprintf(out_stream, "\t\t\t qntsty=%d\n", l_tccp->qntsty);
11361                         fprintf(out_stream, "\t\t\t numgbits=%d\n", l_tccp->numgbits);
11362                         fprintf(out_stream, "\t\t\t stepsizes (m,e)=");
11363                         numbands = (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) ? 1 : l_tccp->numresolutions * 3 - 2;
11364                         for (bandno = 0; bandno < numbands; bandno++) {
11365                                 fprintf(out_stream, "(%d,%d) ", l_tccp->stepsizes[bandno].mant,
11366                                         l_tccp->stepsizes[bandno].expn);
11367                         }
11368                         fprintf(out_stream, "\n");
11369
11370                         /* RGN value*/
11371                         fprintf(out_stream, "\t\t\t roishift=%d\n", l_tccp->roishift);
11372
11373                         fprintf(out_stream, "\t\t }\n");
11374                 } /*end of component of default tile*/
11375                 fprintf(out_stream, "\t }\n"); /*end of default tile*/
11376
11377         }
11378
11379         fprintf(out_stream, "}\n");
11380
11381 }
11382
11383 /**
11384  * Dump an image header structure.
11385  *
11386  *@param img_header                     the image header to dump.
11387  *@param dev_dump_flag          flag to describe if we are in the case of this function is use outside j2k_dump function
11388  *@param out_stream                     output stream where dump the elements.
11389  */
11390 void j2k_dump_image_header(opj_image_t* img_header, opj_bool dev_dump_flag, FILE* out_stream)
11391 {
11392         char tab[2];
11393
11394         if (dev_dump_flag){
11395                 fprintf(stdout, "[DEV] Dump an image_header struct {\n");
11396                 tab[0] = '\0';
11397         }
11398         else {
11399                 fprintf(out_stream, "Image info {\n");
11400                 tab[0] = '\t';tab[1] = '\0';
11401         }
11402
11403         fprintf(out_stream, "%s x0=%d, y0=%d\n", tab, img_header->x0, img_header->y0);
11404         fprintf(out_stream,     "%s x1=%d, y1=%d\n", tab, img_header->x1, img_header->y1);
11405         fprintf(out_stream, "%s numcomps=%d\n", tab, img_header->numcomps);
11406
11407         if (img_header->comps){
11408                 OPJ_UINT32 compno;
11409                 for (compno = 0; compno < img_header->numcomps; compno++) {
11410                         fprintf(out_stream, "%s\t component %d {\n", tab, compno);
11411                         j2k_dump_image_comp_header(&(img_header->comps[compno]), dev_dump_flag, out_stream);
11412                         fprintf(out_stream,"%s}\n",tab);
11413                 }
11414         }
11415
11416         fprintf(out_stream, "}\n");
11417 }
11418
11419 /**
11420  * Dump a component image header structure.
11421  *
11422  *@param comp_header            the component image header to dump.
11423  *@param dev_dump_flag          flag to describe if we are in the case of this function is use outside j2k_dump function
11424  *@param out_stream                     output stream where dump the elements.
11425  */
11426 void j2k_dump_image_comp_header(opj_image_comp_t* comp_header, opj_bool dev_dump_flag, FILE* out_stream)
11427 {
11428         char tab[3];
11429
11430         if (dev_dump_flag){
11431                 fprintf(stdout, "[DEV] Dump an image_comp_header struct {\n");
11432                 tab[0] = '\0';
11433         }       else {
11434                 tab[0] = '\t';tab[1] = '\t';tab[2] = '\0';
11435         }
11436
11437         fprintf(out_stream, "%s dx=%d, dy=%d\n", tab, comp_header->dx, comp_header->dy);
11438         fprintf(out_stream, "%s prec=%d\n", tab, comp_header->prec);
11439         fprintf(out_stream, "%s sgnd=%d\n", tab, comp_header->sgnd);
11440
11441         if (dev_dump_flag)
11442                 fprintf(out_stream, "}\n");
11443 }
11444
11445
11446 /**
11447  * Get the codestream info from a JPEG2000 codec.
11448  *
11449  *@param        p_j2k                           the component image header to dump.
11450  *
11451  *@return       the codestream information extract from the jpg2000 codec
11452  */
11453 opj_codestream_info_v2_t* j2k_get_cstr_info(opj_j2k_v2_t* p_j2k)
11454 {
11455         OPJ_UINT16 compno;
11456         OPJ_UINT16 numcomps = p_j2k->m_private_image->numcomps;
11457         opj_tcp_v2_t *l_default_tile;
11458         opj_codestream_info_v2_t* cstr_info = (opj_codestream_info_v2_t*) opj_calloc(1,sizeof(opj_codestream_info_v2_t));
11459
11460         cstr_info->nbcomps = p_j2k->m_private_image->numcomps;
11461
11462         cstr_info->tx0 = p_j2k->m_cp.tx0;
11463         cstr_info->ty0 = p_j2k->m_cp.ty0;
11464         cstr_info->tdx = p_j2k->m_cp.tdx;
11465         cstr_info->tdy = p_j2k->m_cp.tdy;
11466         cstr_info->tw = p_j2k->m_cp.tw;
11467         cstr_info->th = p_j2k->m_cp.th;
11468
11469         cstr_info->tile_info = NULL; /* Not fill from the main header*/
11470
11471         l_default_tile = p_j2k->m_specific_param.m_decoder.m_default_tcp;
11472
11473         cstr_info->m_default_tile_info.csty = l_default_tile->csty;
11474         cstr_info->m_default_tile_info.prg = l_default_tile->prg;
11475         cstr_info->m_default_tile_info.numlayers = l_default_tile->numlayers;
11476         cstr_info->m_default_tile_info.mct = l_default_tile->mct;
11477
11478         cstr_info->m_default_tile_info.tccp_info = (opj_tccp_info_t*) opj_calloc(cstr_info->nbcomps, sizeof(opj_tccp_info_t));
11479
11480         for (compno = 0; compno < numcomps; compno++) {
11481                 opj_tccp_t *l_tccp = &(l_default_tile->tccps[compno]);
11482                 opj_tccp_info_t *l_tccp_info = &(cstr_info->m_default_tile_info.tccp_info[compno]);
11483                 OPJ_INT32 bandno, numbands;
11484
11485                 /* coding style*/
11486                 l_tccp_info->csty = l_tccp->csty;
11487                 l_tccp_info->numresolutions = l_tccp->numresolutions;
11488                 l_tccp_info->cblkw = l_tccp->cblkw;
11489                 l_tccp_info->cblkh = l_tccp->cblkh;
11490                 l_tccp_info->cblksty = l_tccp->cblksty;
11491                 l_tccp_info->qmfbid = l_tccp->qmfbid;
11492                 if (l_tccp->numresolutions < J2K_MAXRLVLS)
11493                 {
11494                         memcpy(l_tccp_info->prch, l_tccp->prch, l_tccp->numresolutions);
11495                         memcpy(l_tccp_info->prcw, l_tccp->prcw, l_tccp->numresolutions);
11496                 }
11497
11498                 /* quantization style*/
11499                 l_tccp_info->qntsty = l_tccp->qntsty;
11500                 l_tccp_info->numgbits = l_tccp->numgbits;
11501
11502                 numbands = (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) ? 1 : l_tccp->numresolutions * 3 - 2;
11503                 if (numbands < J2K_MAXBANDS) {
11504                         for (bandno = 0; bandno < numbands; bandno++) {
11505                                 l_tccp_info->stepsizes_mant[bandno] = l_tccp->stepsizes[bandno].mant;
11506                                 l_tccp_info->stepsizes_expn[bandno] = l_tccp->stepsizes[bandno].expn;
11507                         }
11508                 }
11509
11510                 /* RGN value*/
11511                 l_tccp_info->roishift = l_tccp->roishift;
11512         }
11513
11514
11515         return cstr_info;
11516 }
11517
11518 /**
11519  * Get the codestream index from a JPEG2000 codec.
11520  *
11521  *@param        p_j2k                           the component image header to dump.
11522  *
11523  *@return       the codestream index extract from the jpg2000 codec
11524  */
11525 opj_codestream_index_t* j2k_get_cstr_index(opj_j2k_v2_t* p_j2k)
11526 {
11527         opj_codestream_index_t* l_cstr_index = (opj_codestream_index_t*)
11528                         opj_calloc(1,sizeof(opj_codestream_index_t));
11529         if (!l_cstr_index)
11530                 return NULL;
11531
11532         l_cstr_index->main_head_start = p_j2k->cstr_index->main_head_start;
11533         l_cstr_index->main_head_end = p_j2k->cstr_index->main_head_end;
11534         l_cstr_index->codestream_size = p_j2k->cstr_index->codestream_size;
11535
11536         l_cstr_index->marknum = p_j2k->cstr_index->marknum;
11537         l_cstr_index->marker = (opj_marker_info_t*)opj_malloc(l_cstr_index->marknum*sizeof(opj_marker_info_t));
11538         if (!l_cstr_index->marker){
11539                 opj_free( l_cstr_index);
11540                 return NULL;
11541         }
11542
11543         if (p_j2k->cstr_index->marker)
11544                 memcpy(l_cstr_index->marker, p_j2k->cstr_index->marker, l_cstr_index->marknum * sizeof(opj_marker_info_t) );
11545         else{
11546                 opj_free(l_cstr_index->marker);
11547                 l_cstr_index->marker = NULL;
11548         }
11549
11550         l_cstr_index->nb_of_tiles = p_j2k->cstr_index->nb_of_tiles;
11551         l_cstr_index->tile_index = (opj_tile_index_t*)opj_calloc(l_cstr_index->nb_of_tiles, sizeof(opj_tile_index_t) );
11552         if (!l_cstr_index->tile_index){
11553                 opj_free( l_cstr_index->marker);
11554                 opj_free( l_cstr_index);
11555                 return NULL;
11556         }
11557
11558         if (!p_j2k->cstr_index->tile_index){
11559                 opj_free(l_cstr_index->tile_index);
11560                 l_cstr_index->tile_index = NULL;
11561         }
11562         else {
11563                 OPJ_UINT32 it_tile = 0;
11564                 for (it_tile = 0; it_tile < l_cstr_index->nb_of_tiles; it_tile++ ){
11565
11566                         /* Tile Marker*/
11567                         l_cstr_index->tile_index[it_tile].marknum = p_j2k->cstr_index->tile_index[it_tile].marknum;
11568
11569                         l_cstr_index->tile_index[it_tile].marker =
11570                                 (opj_marker_info_t*)opj_malloc(l_cstr_index->tile_index[it_tile].marknum*sizeof(opj_marker_info_t));
11571
11572                         if (!l_cstr_index->tile_index[it_tile].marker) {
11573                                 OPJ_UINT32 it_tile_free;
11574
11575                                 for (it_tile_free=0; it_tile_free < it_tile; it_tile_free++){
11576                                         opj_free(l_cstr_index->tile_index[it_tile_free].marker);
11577                                 }
11578
11579                                 opj_free( l_cstr_index->tile_index);
11580                                 opj_free( l_cstr_index->marker);
11581                                 opj_free( l_cstr_index);
11582                                 return NULL;
11583                         }
11584
11585                         if (p_j2k->cstr_index->tile_index[it_tile].marker)
11586                                 memcpy( l_cstr_index->tile_index[it_tile].marker,
11587                                                 p_j2k->cstr_index->tile_index[it_tile].marker,
11588                                                 l_cstr_index->tile_index[it_tile].marknum * sizeof(opj_marker_info_t) );
11589                         else{
11590                                 opj_free(l_cstr_index->tile_index[it_tile].marker);
11591                                 l_cstr_index->tile_index[it_tile].marker = NULL;
11592                         }
11593
11594                         /* Tile part index*/
11595                         l_cstr_index->tile_index[it_tile].nb_tps = p_j2k->cstr_index->tile_index[it_tile].nb_tps;
11596
11597                         l_cstr_index->tile_index[it_tile].tp_index =
11598                                 (opj_tp_index_t*)opj_malloc(l_cstr_index->tile_index[it_tile].nb_tps*sizeof(opj_tp_index_t));
11599
11600                         if(!l_cstr_index->tile_index[it_tile].tp_index){
11601                                 OPJ_UINT32 it_tile_free;
11602
11603                                 for (it_tile_free=0; it_tile_free < it_tile; it_tile_free++){
11604                                         opj_free(l_cstr_index->tile_index[it_tile_free].marker);
11605                                         opj_free(l_cstr_index->tile_index[it_tile_free].tp_index);
11606                                 }
11607
11608                                 opj_free( l_cstr_index->tile_index);
11609                                 opj_free( l_cstr_index->marker);
11610                                 opj_free( l_cstr_index);
11611                                 return NULL;
11612                         }
11613
11614                         if (p_j2k->cstr_index->tile_index[it_tile].tp_index){
11615                                 memcpy( l_cstr_index->tile_index[it_tile].tp_index,
11616                                                 p_j2k->cstr_index->tile_index[it_tile].tp_index,
11617                                                 l_cstr_index->tile_index[it_tile].nb_tps * sizeof(opj_tp_index_t) );
11618                         }
11619                         else{
11620                                 opj_free(l_cstr_index->tile_index[it_tile].tp_index);
11621                                 l_cstr_index->tile_index[it_tile].tp_index = NULL;
11622                         }
11623
11624                         /* Packet index (NOT USED)*/
11625                         l_cstr_index->tile_index[it_tile].nb_packet = 0;
11626                         l_cstr_index->tile_index[it_tile].packet_index = NULL;
11627
11628                 }
11629         }
11630
11631         return l_cstr_index;
11632 }
11633
11634 static opj_bool j2k_allocate_tile_element_cstr_index(opj_j2k_v2_t *p_j2k)
11635 {
11636         OPJ_UINT32 it_tile=0;
11637
11638         p_j2k->cstr_index->nb_of_tiles = p_j2k->m_cp.tw * p_j2k->m_cp.th;
11639         p_j2k->cstr_index->tile_index = (opj_tile_index_t*)opj_calloc(p_j2k->cstr_index->nb_of_tiles, sizeof(opj_tile_index_t));
11640         if (!p_j2k->cstr_index->tile_index)
11641                 return OPJ_FALSE;
11642
11643         for (it_tile=0; it_tile < p_j2k->cstr_index->nb_of_tiles; it_tile++){
11644                 p_j2k->cstr_index->tile_index[it_tile].maxmarknum = 100;
11645                 p_j2k->cstr_index->tile_index[it_tile].marknum = 0;
11646                 p_j2k->cstr_index->tile_index[it_tile].marker = (opj_marker_info_t*)
11647                                 opj_calloc(p_j2k->cstr_index->tile_index[it_tile].maxmarknum, sizeof(opj_marker_info_t));
11648                 if (!p_j2k->cstr_index->tile_index[it_tile].marker)
11649                         return OPJ_FALSE;
11650         }
11651
11652         return OPJ_TRUE;
11653 }
11654
11655 /**
11656  * Reads the tiles.
11657  */
11658 opj_bool j2k_decode_tiles (     opj_j2k_v2_t *p_j2k,
11659                                                         opj_stream_private_t *p_stream,
11660                                                         opj_event_mgr_t * p_manager)
11661 {
11662         opj_bool l_go_on = OPJ_TRUE;
11663         OPJ_UINT32 l_current_tile_no;
11664         OPJ_UINT32 l_data_size,l_max_data_size;
11665         OPJ_INT32 l_tile_x0,l_tile_y0,l_tile_x1,l_tile_y1;
11666         OPJ_UINT32 l_nb_comps;
11667         OPJ_BYTE * l_current_data;
11668
11669         l_current_data = (OPJ_BYTE*)opj_malloc(1000);
11670         if (! l_current_data) {
11671                 return OPJ_FALSE;
11672         }
11673         l_max_data_size = 1000;
11674
11675
11676
11677         while (OPJ_TRUE) {
11678                 if (! j2k_read_tile_header(     p_j2k,
11679                                                                         &l_current_tile_no,
11680                                                                         &l_data_size,
11681                                                                         &l_tile_x0, &l_tile_y0,
11682                                                                         &l_tile_x1, &l_tile_y1,
11683                                                                         &l_nb_comps,
11684                                                                         &l_go_on,
11685                                                                         p_stream,
11686                                                                         p_manager)) {
11687                         opj_free(l_current_data);
11688                         return OPJ_FALSE;
11689                 }
11690
11691                 if (! l_go_on) {
11692                         break;
11693                 }
11694
11695                 if (l_data_size > l_max_data_size) {
11696                         l_current_data = (OPJ_BYTE*)opj_realloc(l_current_data,l_data_size);
11697                         if (! l_current_data) {
11698                                 opj_free(l_current_data);
11699                                 return OPJ_FALSE;
11700                         }
11701
11702                         l_max_data_size = l_data_size;
11703                 }
11704
11705                 if (! j2k_decode_tile(p_j2k,l_current_tile_no,l_current_data,l_data_size,p_stream,p_manager)) {
11706                         opj_free(l_current_data);
11707                         return OPJ_FALSE;
11708                 }
11709                 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);
11710
11711                 if (! j2k_update_image_data(p_j2k->m_tcd,l_current_data, p_j2k->m_output_image)) {
11712                         opj_free(l_current_data);
11713                         return OPJ_FALSE;
11714                 }
11715                 opj_event_msg_v2(p_manager, EVT_INFO, "Image data has been updated with tile %d.\n\n", l_current_tile_no + 1);
11716
11717         }
11718
11719         opj_free(l_current_data);
11720
11721         return OPJ_TRUE;
11722 }
11723
11724 /**
11725  * Sets up the procedures to do on decoding data. Developpers wanting to extend the library can add their own reading procedures.
11726  */
11727 static void j2k_setup_decoding (opj_j2k_v2_t *p_j2k)
11728 {
11729         /* preconditions*/
11730         assert(p_j2k != 00);
11731
11732         opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_decode_tiles);
11733         /* DEVELOPER CORNER, add your custom procedures */
11734
11735 }
11736
11737 /*
11738  * Read and decode one tile.
11739  */
11740 static opj_bool j2k_decode_one_tile (   opj_j2k_v2_t *p_j2k,
11741                                                                 opj_stream_private_t *p_stream,
11742                                                                 opj_event_mgr_t * p_manager)
11743 {
11744         opj_bool l_go_on = OPJ_TRUE;
11745         OPJ_UINT32 l_current_tile_no;
11746         OPJ_UINT32 l_tile_no_to_dec;
11747         OPJ_UINT32 l_data_size,l_max_data_size;
11748         OPJ_INT32 l_tile_x0,l_tile_y0,l_tile_x1,l_tile_y1;
11749         OPJ_UINT32 l_nb_comps;
11750         OPJ_BYTE * l_current_data;
11751
11752         l_current_data = (OPJ_BYTE*)opj_malloc(1000);
11753         if (! l_current_data) {
11754                 return OPJ_FALSE;
11755         }
11756         l_max_data_size = 1000;
11757
11758         /*Allocate and initialize some elements of codestrem index if not already done*/
11759         if( !p_j2k->cstr_index->tile_index)
11760         {
11761                 if (!j2k_allocate_tile_element_cstr_index(p_j2k)){
11762                         opj_free(l_current_data);
11763                         return OPJ_FALSE;
11764                 }
11765         }
11766         /* Move into the codestream to the first SOT used to decode the desired tile */
11767         l_tile_no_to_dec = p_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec;
11768         if (p_j2k->cstr_index->tile_index)
11769                 if(p_j2k->cstr_index->tile_index->tp_index)
11770                 {
11771                         if ( ! p_j2k->cstr_index->tile_index[l_tile_no_to_dec].nb_tps) {
11772                                 /* the index for this tile has not been built,
11773                                  *  so move to the last SOT read */
11774                                 if ( opj_stream_read_seek(p_stream, p_j2k->m_specific_param.m_decoder.m_last_sot_read_pos+2, p_manager) ){
11775                                         opj_event_msg_v2(p_manager, EVT_ERROR, "Problem with seek function\n");
11776                                         return OPJ_FALSE;
11777                                 }
11778                         }
11779                         else{
11780                                 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)) {
11781                                         opj_event_msg_v2(p_manager, EVT_ERROR, "Problem with seek function\n");
11782                                         return OPJ_FALSE;
11783                                 }
11784                         }
11785                         /* Special case if we have previously read the EOC marker (if the previous tile getted is the last ) */
11786                         if(p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_EOC)
11787                                 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_TPHSOT;
11788                 }
11789
11790         while (OPJ_TRUE) {
11791                 if (! j2k_read_tile_header(     p_j2k,
11792                                                                         &l_current_tile_no,
11793                                                                         &l_data_size,
11794                                                                         &l_tile_x0, &l_tile_y0,
11795                                                                         &l_tile_x1, &l_tile_y1,
11796                                                                         &l_nb_comps,
11797                                                                         &l_go_on,
11798                                                                         p_stream,
11799                                                                         p_manager)) {
11800                         opj_free(l_current_data);
11801                         return OPJ_FALSE;
11802                 }
11803
11804
11805                 if (! l_go_on) {
11806                         break;
11807                 }
11808
11809                 if (l_data_size > l_max_data_size) {
11810                         l_current_data = (OPJ_BYTE*)opj_realloc(l_current_data,l_data_size);
11811                         if (! l_current_data) {
11812                                 opj_free(l_current_data);
11813                                 return OPJ_FALSE;
11814                         }
11815
11816                         l_max_data_size = l_data_size;
11817                 }
11818
11819
11820
11821                 if (! j2k_decode_tile(p_j2k,l_current_tile_no,l_current_data,l_data_size,p_stream,p_manager)) {
11822                         opj_free(l_current_data);
11823                         return OPJ_FALSE;
11824                 }
11825                 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);
11826
11827                 if (! j2k_update_image_data(p_j2k->m_tcd,l_current_data, p_j2k->m_output_image)) {
11828                         opj_free(l_current_data);
11829                         return OPJ_FALSE;
11830                 }
11831                 opj_event_msg_v2(p_manager, EVT_INFO, "Image data has been updated with tile %d.\n\n", l_current_tile_no);
11832
11833                 if(l_current_tile_no == l_tile_no_to_dec)
11834                 {
11835                         /* move into the codestream to the the first SOT (FIXME or not move?)*/
11836                         if (!(opj_stream_read_seek(p_stream, p_j2k->cstr_index->main_head_end + 2, p_manager) ) ) {
11837                                 opj_event_msg_v2(p_manager, EVT_ERROR, "Problem with seek function\n");
11838                                 return OPJ_FALSE;
11839                         }
11840                         break;
11841                 }
11842                 else {
11843                         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);
11844                 }
11845
11846         }
11847
11848         opj_free(l_current_data);
11849
11850         return OPJ_TRUE;
11851 }
11852
11853
11854 /**
11855  * Sets up the procedures to do on decoding one tile. Developpers wanting to extend the library can add their own reading procedures.
11856  */
11857 static void j2k_setup_decoding_tile (opj_j2k_v2_t *p_j2k)
11858 {
11859         /* preconditions*/
11860         assert(p_j2k != 00);
11861
11862         opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_decode_one_tile);
11863         /* DEVELOPER CORNER, add your custom procedures */
11864
11865 }
11866
11867
11868 /**
11869  * Decodes the tiles of the stream.
11870  */
11871 opj_bool j2k_decode_v2( opj_j2k_v2_t * p_j2k,
11872                                                 opj_stream_private_t * p_stream,
11873                                                 opj_image_t * p_image,
11874                                                 opj_event_mgr_t * p_manager)
11875 {
11876         OPJ_UINT32 compno;
11877
11878         if (!p_image)
11879                 return OPJ_FALSE;
11880
11881         p_j2k->m_output_image = opj_image_create0();
11882         if (! (p_j2k->m_output_image)) {
11883                 return OPJ_FALSE;
11884         }
11885         opj_copy_image_header(p_image, p_j2k->m_output_image);
11886
11887         /* customization of the decoding */
11888         j2k_setup_decoding(p_j2k);
11889
11890         /* Decode the codestream */
11891         if (! j2k_exec (p_j2k,p_j2k->m_procedure_list,p_stream,p_manager)) {
11892                 opj_image_destroy(p_j2k->m_private_image);
11893                 p_j2k->m_private_image = NULL;
11894                 return OPJ_FALSE;
11895         }
11896
11897         /* Move data and copy one information from codec to output image*/
11898         for (compno = 0; compno < p_image->numcomps; compno++) {
11899                 p_image->comps[compno].resno_decoded = p_j2k->m_output_image->comps[compno].resno_decoded;
11900                 p_image->comps[compno].data = p_j2k->m_output_image->comps[compno].data;
11901                 p_j2k->m_output_image->comps[compno].data = NULL;
11902         }
11903
11904         return OPJ_TRUE;
11905 }
11906
11907
11908 /**
11909  * Get the decoded tile.
11910  *
11911  * @param       p_j2k                   the jpeg2000 codestream codec.
11912  * @param       p_stream                input_stream
11913  * @param       p_image                 output image.   .
11914  * @param       p_manager               the user event manager
11915  * @param       tile_index              index of the tile we want decode
11916  *
11917  * @return      true                    if succeed.
11918  */
11919 opj_bool j2k_get_tile(  opj_j2k_v2_t *p_j2k,
11920                                                 opj_stream_private_t *p_stream,
11921                                                 opj_image_t* p_image,
11922                                                 struct opj_event_mgr * p_manager,
11923                                                 OPJ_UINT32 tile_index )
11924 {
11925         OPJ_UINT32 compno;
11926         OPJ_UINT32 l_tile_x, l_tile_y;
11927         opj_image_comp_t* l_img_comp;
11928
11929         if (!p_image) {
11930                 opj_event_msg_v2(p_manager, EVT_ERROR, "We need an image previously created.\n");
11931                 return OPJ_FALSE;
11932         }
11933
11934         if ( /*(tile_index < 0) &&*/ (tile_index >= p_j2k->m_cp.tw * p_j2k->m_cp.th) ){
11935                 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);
11936                 return OPJ_FALSE;
11937         }
11938
11939         /* Compute the dimension of the desired tile*/
11940         l_tile_x = tile_index % p_j2k->m_cp.tw;
11941         l_tile_y = tile_index / p_j2k->m_cp.tw;
11942
11943         p_image->x0 = l_tile_x * p_j2k->m_cp.tdx + p_j2k->m_cp.tx0;
11944         if (p_image->x0 < p_j2k->m_private_image->x0)
11945                 p_image->x0 = p_j2k->m_private_image->x0;
11946         p_image->x1 = (l_tile_x + 1) * p_j2k->m_cp.tdx + p_j2k->m_cp.tx0;
11947         if (p_image->x1 > p_j2k->m_private_image->x1)
11948                 p_image->x1 = p_j2k->m_private_image->x1;
11949
11950         p_image->y0 = l_tile_y * p_j2k->m_cp.tdy + p_j2k->m_cp.ty0;
11951         if (p_image->y0 < p_j2k->m_private_image->y0)
11952                 p_image->y0 = p_j2k->m_private_image->y0;
11953         p_image->y1 = (l_tile_y + 1) * p_j2k->m_cp.tdy + p_j2k->m_cp.ty0;
11954         if (p_image->y1 > p_j2k->m_private_image->y1)
11955                 p_image->y1 = p_j2k->m_private_image->y1;
11956
11957         l_img_comp = p_image->comps;
11958         for (compno=0; compno < p_image->numcomps; ++compno)
11959         {
11960                 OPJ_INT32 l_comp_x1, l_comp_y1;
11961
11962                 l_img_comp->factor = p_j2k->m_private_image->comps[compno].factor;
11963
11964                 l_img_comp->x0 = int_ceildiv(p_image->x0, l_img_comp->dx);
11965                 l_img_comp->y0 = int_ceildiv(p_image->y0, l_img_comp->dy);
11966                 l_comp_x1 = int_ceildiv(p_image->x1, l_img_comp->dx);
11967                 l_comp_y1 = int_ceildiv(p_image->y1, l_img_comp->dy);
11968
11969                 l_img_comp->w = int_ceildivpow2(l_comp_x1, l_img_comp->factor) - int_ceildivpow2(l_img_comp->x0, l_img_comp->factor);
11970                 l_img_comp->h = int_ceildivpow2(l_comp_y1, l_img_comp->factor) - int_ceildivpow2(l_img_comp->y0, l_img_comp->factor);
11971
11972                 l_img_comp++;
11973         }
11974
11975         /* Destroy the previous output image*/
11976         if (p_j2k->m_output_image)
11977                 opj_image_destroy(p_j2k->m_output_image);
11978
11979         /* Create the ouput image from the information previously computed*/
11980         p_j2k->m_output_image = opj_image_create0();
11981         if (! (p_j2k->m_output_image)) {
11982                 return OPJ_FALSE;
11983         }
11984         opj_copy_image_header(p_image, p_j2k->m_output_image);
11985
11986         p_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec = tile_index;
11987
11988         /* customization of the decoding */
11989         j2k_setup_decoding_tile(p_j2k);
11990
11991         /* Decode the codestream */
11992         if (! j2k_exec (p_j2k,p_j2k->m_procedure_list,p_stream,p_manager)) {
11993                 opj_image_destroy(p_j2k->m_private_image);
11994                 p_j2k->m_private_image = NULL;
11995                 return OPJ_FALSE;
11996         }
11997
11998         /* Move data and copy one information from codec to output image*/
11999         for (compno = 0; compno < p_image->numcomps; compno++) {
12000                 p_image->comps[compno].resno_decoded = p_j2k->m_output_image->comps[compno].resno_decoded;
12001
12002                 if (p_image->comps[compno].data)
12003                         opj_free(p_image->comps[compno].data);
12004
12005                 p_image->comps[compno].data = p_j2k->m_output_image->comps[compno].data;
12006
12007                 p_j2k->m_output_image->comps[compno].data = NULL;
12008         }
12009
12010         return OPJ_TRUE;
12011 }
12012
12013 opj_bool j2k_set_decoded_resolution_factor(opj_j2k_v2_t *p_j2k, OPJ_UINT32 res_factor, opj_event_mgr_t * p_manager)
12014 {
12015         OPJ_UINT32 it_comp;
12016
12017         p_j2k->m_cp.m_specific_param.m_dec.m_reduce = res_factor;
12018
12019         if (p_j2k->m_private_image) {
12020                 if (p_j2k->m_private_image->comps) {
12021                         if (p_j2k->m_specific_param.m_decoder.m_default_tcp) {
12022                                 if (p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps) {
12023                                         for (it_comp = 0 ; it_comp < p_j2k->m_private_image->numcomps; it_comp++) {
12024                                                 OPJ_UINT32 max_res = p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps[it_comp].numresolutions;
12025                                                 if ( res_factor >= max_res){
12026                                                         opj_event_msg_v2(p_manager, EVT_ERROR, "Resolution factor is greater than the maximum resolution in the component.\n");
12027                                                         return OPJ_FALSE;
12028                                                 }
12029                                                 p_j2k->m_private_image->comps[it_comp].factor = res_factor;
12030                                         }
12031                                         return OPJ_TRUE;
12032                                 }
12033                         }
12034                 }
12035         }
12036
12037         return OPJ_FALSE;
12038 }
12039
12040
12041 /**
12042  * Encodes all the tiles in a row.
12043  */
12044 opj_bool j2k_encode_v2( opj_j2k_v2_t * p_j2k,
12045                                                 opj_stream_private_t *p_stream,
12046                                                 opj_event_mgr_t * p_manager )
12047 {
12048         OPJ_UINT32 i;
12049         OPJ_UINT32 l_nb_tiles;
12050         OPJ_UINT32 l_max_tile_size, l_current_tile_size;
12051         OPJ_BYTE * l_current_data;
12052
12053         /* preconditions */
12054         assert(p_j2k != 00);
12055         assert(p_stream != 00);
12056         assert(p_manager != 00);
12057
12058         l_current_data = (OPJ_BYTE*)opj_malloc(1000);
12059         if (! l_current_data) {
12060                 return OPJ_FALSE;
12061         }
12062         l_max_tile_size = 1000;
12063
12064         l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw;
12065         for (i=0;i<l_nb_tiles;++i) {
12066                 if (! j2k_pre_write_tile(p_j2k,i,p_stream,p_manager)) {
12067                         opj_free(l_current_data);
12068                         return OPJ_FALSE;
12069                 }
12070
12071                 l_current_tile_size = tcd_get_encoded_tile_size(p_j2k->m_tcd);
12072                 if (l_current_tile_size > l_max_tile_size) {
12073                         l_current_data = (OPJ_BYTE*)opj_realloc(l_current_data,l_current_tile_size);
12074                         if (! l_current_data) {
12075                                 return OPJ_FALSE;
12076                         }
12077                         l_max_tile_size = l_current_tile_size;
12078                 }
12079
12080                 j2k_get_tile_data(p_j2k->m_tcd,l_current_data);
12081
12082                 if (! j2k_post_write_tile (p_j2k,l_current_data,l_current_tile_size,p_stream,p_manager)) {
12083                         return OPJ_FALSE;
12084                 }
12085         }
12086
12087         opj_free(l_current_data);
12088         return OPJ_TRUE;
12089 }
12090
12091 /**
12092  * Ends the compression procedures and possibility add data to be read after the
12093  * codestream.
12094  */
12095 opj_bool j2k_end_compress(      opj_j2k_v2_t *p_j2k,
12096                                                         opj_stream_private_t *p_stream,
12097                                                         struct opj_event_mgr * p_manager)
12098 {
12099         /* customization of the encoding */
12100         j2k_setup_end_compress(p_j2k);
12101
12102         if (! j2k_exec (p_j2k, p_j2k->m_procedure_list, p_stream, p_manager))
12103         {
12104                 return OPJ_FALSE;
12105         }
12106
12107         return OPJ_TRUE;
12108 }
12109
12110
12111 /**
12112  * Starts a compression scheme, i.e. validates the codec parameters, writes the header.
12113  *
12114  * @param       p_j2k           the jpeg2000 codec.
12115  * @param       p_stream        the stream object.
12116  * @param       p_manager       the user event manager.
12117  *
12118  * @return true if the codec is valid.
12119  */
12120 opj_bool j2k_start_compress(opj_j2k_v2_t *p_j2k,
12121                                                         opj_stream_private_t *p_stream,
12122                                                         opj_image_t * p_image,
12123                                                         opj_event_mgr_t * p_manager)
12124 {
12125         /* preconditions */
12126         assert(p_j2k != 00);
12127         assert(p_stream != 00);
12128         assert(p_manager != 00);
12129
12130         p_j2k->m_private_image = opj_image_create0();
12131         opj_copy_image_header(p_image, p_j2k->m_private_image);
12132
12133         // TODO_MSD: Find a better way
12134         if (p_image->comps) {
12135                 OPJ_UINT32 it_comp;
12136                 for (it_comp = 0 ; it_comp < p_image->numcomps; it_comp++) {
12137                         if (p_image->comps[it_comp].data) {
12138                                 p_j2k->m_private_image->comps[it_comp].data =p_image->comps[it_comp].data;
12139                                 p_image->comps[it_comp].data = NULL;
12140
12141                         }
12142                 }
12143         }
12144
12145         /* customization of the validation */
12146         j2k_setup_encoding_validation (p_j2k);
12147
12148         /* validation of the parameters codec */
12149         if (! j2k_exec(p_j2k,p_j2k->m_validation_list,p_stream,p_manager)) {
12150                 return OPJ_FALSE;
12151         }
12152
12153         /* customization of the encoding */
12154         j2k_setup_header_writting(p_j2k);
12155
12156         /* write header */
12157         if (! j2k_exec (p_j2k,p_j2k->m_procedure_list,p_stream,p_manager)) {
12158                 return OPJ_FALSE;
12159         }
12160
12161         return OPJ_TRUE;
12162 }
12163
12164 /*
12165  *
12166  */
12167 opj_bool j2k_pre_write_tile (   opj_j2k_v2_t * p_j2k,
12168                                                                 OPJ_UINT32 p_tile_index,
12169                                                                 opj_stream_private_t *p_stream,
12170                                                                 opj_event_mgr_t * p_manager )
12171 {
12172   (void)p_stream;
12173         if (p_tile_index != p_j2k->m_current_tile_number) {
12174                 opj_event_msg_v2(p_manager, EVT_ERROR, "The given tile index does not match." );
12175                 return OPJ_FALSE;
12176         }
12177
12178         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);
12179
12180         p_j2k->m_specific_param.m_encoder.m_current_tile_part_number = 0;
12181         p_j2k->m_tcd->cur_totnum_tp = p_j2k->m_cp.tcps[p_tile_index].m_nb_tile_parts;
12182         p_j2k->m_specific_param.m_encoder.m_current_poc_tile_part_number = 0;
12183
12184         /* initialisation before tile encoding  */
12185         if (! tcd_init_encode_tile(p_j2k->m_tcd, p_j2k->m_current_tile_number)) {
12186                 return OPJ_FALSE;
12187         }
12188
12189         return OPJ_TRUE;
12190 }
12191
12192 /*
12193  *
12194  */
12195 void j2k_get_tile_data (opj_tcd_v2_t * p_tcd, OPJ_BYTE * p_data)
12196 {
12197         OPJ_UINT32 i,j,k = 0;
12198         OPJ_UINT32 l_width,l_height,l_stride, l_offset_x,l_offset_y, l_image_width;
12199         opj_image_comp_t * l_img_comp = 00;
12200         opj_tcd_tilecomp_v2_t * l_tilec = 00;
12201         opj_image_t * l_image = 00;
12202         OPJ_UINT32 l_size_comp, l_remaining;
12203         OPJ_INT32 * l_src_ptr;
12204         l_tilec = p_tcd->tcd_image->tiles->comps;
12205         l_image = p_tcd->image;
12206         l_img_comp = l_image->comps;
12207
12208         for (i=0;i<p_tcd->image->numcomps;++i) {
12209                 l_size_comp = l_img_comp->prec >> 3; /* (/8) */
12210                 l_remaining = l_img_comp->prec & 7;  /* (%8) */
12211                 if (l_remaining) {
12212                         ++l_size_comp;
12213                 }
12214
12215                 if (l_size_comp == 3) {
12216                         l_size_comp = 4;
12217                 }
12218
12219                 l_width = (l_tilec->x1 - l_tilec->x0);
12220                 l_height = (l_tilec->y1 - l_tilec->y0);
12221                 l_offset_x = int_ceildiv(l_image->x0, l_img_comp->dx);
12222                 l_offset_y = int_ceildiv(l_image->y0, l_img_comp->dy);
12223                 l_image_width = int_ceildiv(l_image->x1 - l_image->x0, l_img_comp->dx);
12224                 l_stride = l_image_width - l_width;
12225                 l_src_ptr = l_img_comp->data + (l_tilec->x0 - l_offset_x) + (l_tilec->y0 - l_offset_y) * l_image_width;
12226
12227                 switch (l_size_comp) {
12228                         case 1:
12229                                 {
12230                                         OPJ_CHAR * l_dest_ptr = (OPJ_CHAR*) p_data;
12231                                         if (l_img_comp->sgnd) {
12232                                                 for     (j=0;j<l_height;++j) {
12233                                                         for (k=0;k<l_width;++k) {
12234                                                                 *(l_dest_ptr) = (OPJ_CHAR) (*l_src_ptr);
12235                                                                 ++l_dest_ptr;
12236                                                                 ++l_src_ptr;
12237                                                         }
12238                                                         l_src_ptr += l_stride;
12239                                                 }
12240                                         }
12241                                         else {
12242                                                 for (j=0;j<l_height;++j) {
12243                                                         for (k=0;k<l_width;++k) {
12244                                                                 *(l_dest_ptr) = (*l_src_ptr)&0xff;
12245                                                                 ++l_dest_ptr;
12246                                                                 ++l_src_ptr;
12247                                                         }
12248                                                         l_src_ptr += l_stride;
12249                                                 }
12250                                         }
12251
12252                                         p_data = (OPJ_BYTE*) l_dest_ptr;
12253                                 }
12254                                 break;
12255                         case 2:
12256                                 {
12257                                         OPJ_INT16 * l_dest_ptr = (OPJ_INT16 *) p_data;
12258                                         if (l_img_comp->sgnd) {
12259                                                 for (j=0;j<l_height;++j) {
12260                                                         for (k=0;k<l_width;++k) {
12261                                                                 *(l_dest_ptr++) = (OPJ_INT16) (*(l_src_ptr++));
12262                                                         }
12263                                                         l_src_ptr += l_stride;
12264                                                 }
12265                                         }
12266                                         else {
12267                                                 for (j=0;j<l_height;++j) {
12268                                                         for (k=0;k<l_width;++k) {
12269                                                                 *(l_dest_ptr++) = (*(l_src_ptr++))&0xffff;
12270                                                         }
12271                                                         l_src_ptr += l_stride;
12272                                                 }
12273                                         }
12274
12275                                         p_data = (OPJ_BYTE*) l_dest_ptr;
12276                                 }
12277                                 break;
12278                         case 4:
12279                                 {
12280                                         OPJ_INT32 * l_dest_ptr = (OPJ_INT32 *) p_data;
12281                                         for (j=0;j<l_height;++j) {
12282                                                 for (k=0;k<l_width;++k) {
12283                                                         *(l_dest_ptr++) = *(l_src_ptr++);
12284                                                 }
12285                                                 l_src_ptr += l_stride;
12286                                         }
12287
12288                                         p_data = (OPJ_BYTE*) l_dest_ptr;
12289                                 }
12290                                 break;
12291                 }
12292
12293                 ++l_img_comp;
12294                 ++l_tilec;
12295         }
12296 }
12297
12298
12299 /**
12300  * Write a tile.
12301  * @param       p_j2k           the jpeg2000 codec.
12302  * @param       p_stream        the stream to write data to.
12303  * @param       p_manager       the user event manager.
12304  */
12305 opj_bool j2k_post_write_tile (  opj_j2k_v2_t * p_j2k,
12306                                                                 OPJ_BYTE * p_data,
12307                                                                 OPJ_UINT32 p_data_size,
12308                                                                 opj_stream_private_t *p_stream,
12309                                                                 opj_event_mgr_t * p_manager )
12310 {
12311         opj_tcd_v2_t * l_tcd = 00;
12312         opj_cp_v2_t * l_cp = 00;
12313         opj_tcp_v2_t * l_tcp = 00;
12314         OPJ_UINT32 l_nb_bytes_written;
12315         OPJ_BYTE * l_current_data = 00;
12316         OPJ_UINT32 l_tile_size = 0;
12317         OPJ_UINT32 l_available_data;
12318
12319         /* preconditions */
12320         assert(p_j2k->m_specific_param.m_encoder.m_encoded_tile_data);
12321
12322         l_tcd = p_j2k->m_tcd;
12323         l_cp = &(p_j2k->m_cp);
12324         l_tcp = l_cp->tcps + p_j2k->m_current_tile_number;
12325
12326         l_tile_size = p_j2k->m_specific_param.m_encoder.m_encoded_tile_size;
12327         l_available_data = l_tile_size;
12328         l_current_data = p_j2k->m_specific_param.m_encoder.m_encoded_tile_data;
12329
12330         if (! tcd_copy_tile_data(l_tcd,p_data,p_data_size)) {
12331                 opj_event_msg_v2(p_manager, EVT_ERROR, "Size mismatch between tile data and sent data." );
12332                 return OPJ_FALSE;
12333         }
12334
12335         l_nb_bytes_written = 0;
12336         if (! j2k_write_first_tile_part(p_j2k,l_current_data,&l_nb_bytes_written,l_available_data,p_stream,p_manager)) {
12337                 return OPJ_FALSE;
12338         }
12339         l_current_data += l_nb_bytes_written;
12340         l_available_data -= l_nb_bytes_written;
12341
12342         l_nb_bytes_written = 0;
12343         if (! j2k_write_all_tile_parts(p_j2k,l_current_data,&l_nb_bytes_written,l_available_data,p_stream,p_manager)) {
12344                 return OPJ_FALSE;
12345         }
12346
12347         l_available_data -= l_nb_bytes_written;
12348         l_nb_bytes_written = l_tile_size - l_available_data;
12349
12350         if ( opj_stream_write_data(     p_stream,
12351                                                                 p_j2k->m_specific_param.m_encoder.m_encoded_tile_data,
12352                                                                 l_nb_bytes_written,p_manager) != l_nb_bytes_written) {
12353                 return OPJ_FALSE;
12354         }
12355
12356         ++p_j2k->m_current_tile_number;
12357
12358         return OPJ_TRUE;
12359 }
12360
12361
12362 /**
12363  * Sets up the validation ,i.e. adds the procedures to lauch to make sure the codec parameters
12364  * are valid. Developers wanting to extend the library can add their own validation procedures.
12365  */
12366 void j2k_setup_end_compress (opj_j2k_v2_t *p_j2k)
12367 {
12368         /* preconditions */
12369         assert(p_j2k != 00);
12370
12371         /* DEVELOPER CORNER, insert your custom procedures */
12372         opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_eoc_v2 );
12373
12374         if (p_j2k->m_cp.m_specific_param.m_enc.m_cinema) {
12375                 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_updated_tlm);
12376         }
12377
12378         opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_epc );
12379         opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_end_encoding );
12380         opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_destroy_header_memory);
12381 }
12382
12383 /**
12384  * Sets up the validation ,i.e. adds the procedures to lauch to make sure the codec parameters
12385  * are valid. Developers wanting to extend the library can add their own validation procedures.
12386  */
12387 void j2k_setup_encoding_validation (opj_j2k_v2_t *p_j2k)
12388 {
12389         /* preconditions */
12390         assert(p_j2k != 00);
12391
12392         opj_procedure_list_add_procedure(p_j2k->m_validation_list, (opj_procedure)j2k_build_encoder);
12393         opj_procedure_list_add_procedure(p_j2k->m_validation_list, (opj_procedure)j2k_encoding_validation);
12394
12395         /* DEVELOPER CORNER, add your custom validation procedure */
12396         opj_procedure_list_add_procedure(p_j2k->m_validation_list, (opj_procedure)j2k_mct_validation);
12397 }
12398
12399
12400 /**
12401  * Sets up the procedures to do on writing header.
12402  * Developers wanting to extend the library can add their own writing procedures.
12403  */
12404 void j2k_setup_header_writting (opj_j2k_v2_t *p_j2k)
12405 {
12406         /* preconditions */
12407         assert(p_j2k != 00);
12408
12409         opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_init_info );
12410         opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_soc_v2 );
12411         opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_siz_v2 );
12412         opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_cod_v2 );
12413         opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_qcd_v2 );
12414
12415
12416         if (p_j2k->m_cp.m_specific_param.m_enc.m_cinema) {
12417                 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_image_components );
12418                 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_tlm_v2 );
12419
12420                 if (p_j2k->m_cp.m_specific_param.m_enc.m_cinema == CINEMA4K_24) {
12421                         opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_poc_v2 );
12422                 }
12423         }
12424
12425         opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_regions);
12426
12427         if (p_j2k->m_cp.comment != 00)  {
12428                 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_com_v2);
12429         }
12430
12431         /* DEVELOPER CORNER, insert your custom procedures */
12432         if (p_j2k->m_cp.rsiz & MCT) {
12433                 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_mct_data_group );
12434         }
12435         /* End of Developer Corner */
12436
12437         if (p_j2k->cstr_index) {
12438                 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_get_end_header );
12439         }
12440
12441         opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_create_tcd);
12442         opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_update_rates);
12443 }
12444
12445
12446 opj_bool j2k_write_first_tile_part (opj_j2k_v2_t *p_j2k,
12447                                                                         OPJ_BYTE * p_data,
12448                                                                         OPJ_UINT32 * p_data_written,
12449                                                                         OPJ_UINT32 p_total_data_size,
12450                                                                         opj_stream_private_t *p_stream,
12451                                                                         struct opj_event_mgr * p_manager )
12452 {
12453         OPJ_UINT32 compno;
12454         OPJ_UINT32 l_nb_bytes_written = 0;
12455         OPJ_UINT32 l_current_nb_bytes_written;
12456         OPJ_BYTE * l_begin_data = 00;
12457
12458         opj_tcp_v2_t *l_tcp = 00;
12459         opj_tcd_v2_t * l_tcd = 00;
12460         opj_cp_v2_t * l_cp = 00;
12461
12462         l_tcd = p_j2k->m_tcd;
12463         l_cp = &(p_j2k->m_cp);
12464         l_tcp = l_cp->tcps + p_j2k->m_current_tile_number;
12465
12466         l_tcd->cur_pino = 0;
12467
12468         /*Get number of tile parts*/
12469         p_j2k->m_specific_param.m_encoder.m_current_poc_tile_part_number = 0;
12470
12471         /* INDEX >> */
12472         /* << INDEX */
12473
12474         l_current_nb_bytes_written = 0;
12475         l_begin_data = p_data;
12476         if (! j2k_write_sot_v2(p_j2k,p_data,&l_current_nb_bytes_written,p_stream,p_manager))
12477         {
12478                 return OPJ_FALSE;
12479         }
12480
12481         l_nb_bytes_written += l_current_nb_bytes_written;
12482         p_data += l_current_nb_bytes_written;
12483         p_total_data_size -= l_current_nb_bytes_written;
12484
12485         if (l_cp->m_specific_param.m_enc.m_cinema == 0) {
12486                 for (compno = 1; compno < p_j2k->m_private_image->numcomps; compno++) {
12487                         l_current_nb_bytes_written = 0;
12488                         j2k_write_coc_in_memory(p_j2k,compno,p_data,&l_current_nb_bytes_written,p_manager);
12489                         l_nb_bytes_written += l_current_nb_bytes_written;
12490                         p_data += l_current_nb_bytes_written;
12491                         p_total_data_size -= l_current_nb_bytes_written;
12492
12493                         l_current_nb_bytes_written = 0;
12494                         j2k_write_qcc_in_memory(p_j2k,compno,p_data,&l_current_nb_bytes_written,p_manager);
12495                         l_nb_bytes_written += l_current_nb_bytes_written;
12496                         p_data += l_current_nb_bytes_written;
12497                         p_total_data_size -= l_current_nb_bytes_written;
12498                 }
12499
12500                 if (l_cp->tcps[p_j2k->m_current_tile_number].numpocs) {
12501                         l_current_nb_bytes_written = 0;
12502                         j2k_write_poc_in_memory(p_j2k,p_data,&l_current_nb_bytes_written,p_manager);
12503                         l_nb_bytes_written += l_current_nb_bytes_written;
12504                         p_data += l_current_nb_bytes_written;
12505                         p_total_data_size -= l_current_nb_bytes_written;
12506                 }
12507         }
12508
12509         l_current_nb_bytes_written = 0;
12510         if (! j2k_write_sod_v2(p_j2k,l_tcd,p_data,&l_current_nb_bytes_written,p_total_data_size,p_stream,p_manager)) {
12511                 return OPJ_FALSE;
12512         }
12513
12514         l_nb_bytes_written += l_current_nb_bytes_written;
12515         * p_data_written = l_nb_bytes_written;
12516
12517         /* Writing Psot in SOT marker */
12518         opj_write_bytes(l_begin_data + 6,l_nb_bytes_written,4);                                 /* PSOT */
12519
12520         if (l_cp->m_specific_param.m_enc.m_cinema){
12521                 j2k_update_tlm(p_j2k,l_nb_bytes_written);
12522         }
12523
12524         return OPJ_TRUE;
12525 }
12526
12527 opj_bool j2k_write_all_tile_parts(      opj_j2k_v2_t *p_j2k,
12528                                                                         OPJ_BYTE * p_data,
12529                                                                         OPJ_UINT32 * p_data_written,
12530                                                                         OPJ_UINT32 p_total_data_size,
12531                                                                         opj_stream_private_t *p_stream,
12532                                                                         struct opj_event_mgr * p_manager
12533                                                                 )
12534 {
12535         OPJ_UINT32 tilepartno=0;
12536         OPJ_UINT32 l_nb_bytes_written = 0;
12537         OPJ_UINT32 l_current_nb_bytes_written;
12538         OPJ_UINT32 l_part_tile_size;
12539         OPJ_UINT32 tot_num_tp;
12540         OPJ_UINT32 pino;
12541
12542         OPJ_BYTE * l_begin_data;
12543         opj_tcp_v2_t *l_tcp = 00;
12544         opj_tcd_v2_t * l_tcd = 00;
12545         opj_cp_v2_t * l_cp = 00;
12546
12547
12548         l_tcd = p_j2k->m_tcd;
12549         l_cp = &(p_j2k->m_cp);
12550         l_tcp = l_cp->tcps + p_j2k->m_current_tile_number;
12551
12552         /*Get number of tile parts*/
12553         tot_num_tp = j2k_get_num_tp_v2(l_cp,0,p_j2k->m_current_tile_number);
12554
12555         for (tilepartno = 1; tilepartno < tot_num_tp ; ++tilepartno) {
12556                 p_j2k->m_specific_param.m_encoder.m_current_poc_tile_part_number = tilepartno;
12557                 l_current_nb_bytes_written = 0;
12558                 l_part_tile_size = 0;
12559                 l_begin_data = p_data;
12560
12561                 if (! j2k_write_sot_v2(p_j2k,p_data,&l_current_nb_bytes_written,p_stream,p_manager)) {
12562                         return OPJ_FALSE;
12563                 }
12564
12565                 l_nb_bytes_written += l_current_nb_bytes_written;
12566                 p_data += l_current_nb_bytes_written;
12567                 p_total_data_size -= l_current_nb_bytes_written;
12568                 l_part_tile_size += l_nb_bytes_written;
12569
12570                 l_current_nb_bytes_written = 0;
12571                 if (! j2k_write_sod_v2(p_j2k,l_tcd,p_data,&l_current_nb_bytes_written,p_total_data_size,p_stream,p_manager)) {
12572                         return OPJ_FALSE;
12573                 }
12574
12575                 p_data += l_current_nb_bytes_written;
12576                 l_nb_bytes_written += l_current_nb_bytes_written;
12577                 p_total_data_size -= l_current_nb_bytes_written;
12578                 l_part_tile_size += l_nb_bytes_written;
12579
12580                 /* Writing Psot in SOT marker */
12581                 opj_write_bytes(l_begin_data + 6,l_part_tile_size,4);                                   /* PSOT */
12582
12583                 if (l_cp->m_specific_param.m_enc.m_cinema) {
12584                         j2k_update_tlm(p_j2k,l_part_tile_size);
12585                 }
12586
12587                 ++p_j2k->m_specific_param.m_encoder.m_current_tile_part_number;
12588         }
12589
12590         for (pino = 1; pino <= l_tcp->numpocs; ++pino) {
12591                 l_tcd->cur_pino = pino;
12592
12593                 /*Get number of tile parts*/
12594                 tot_num_tp = j2k_get_num_tp_v2(l_cp,pino,p_j2k->m_current_tile_number);
12595                 for (tilepartno = 0; tilepartno < tot_num_tp ; ++tilepartno) {
12596                         p_j2k->m_specific_param.m_encoder.m_current_poc_tile_part_number = tilepartno;
12597                         l_current_nb_bytes_written = 0;
12598                         l_part_tile_size = 0;
12599                         l_begin_data = p_data;
12600
12601                         if (! j2k_write_sot_v2(p_j2k,p_data,&l_current_nb_bytes_written,p_stream,p_manager)) {
12602                                 return OPJ_FALSE;
12603                         }
12604
12605                         l_nb_bytes_written += l_current_nb_bytes_written;
12606                         p_data += l_current_nb_bytes_written;
12607                         p_total_data_size -= l_current_nb_bytes_written;
12608                         l_part_tile_size += l_current_nb_bytes_written;
12609
12610                         l_current_nb_bytes_written = 0;
12611
12612                         if (! j2k_write_sod_v2(p_j2k,l_tcd,p_data,&l_current_nb_bytes_written,p_total_data_size,p_stream,p_manager)) {
12613                                 return OPJ_FALSE;
12614                         }
12615
12616                         l_nb_bytes_written += l_current_nb_bytes_written;
12617                         p_data += l_current_nb_bytes_written;
12618                         p_total_data_size -= l_current_nb_bytes_written;
12619                         l_part_tile_size += l_current_nb_bytes_written;
12620
12621                         /* Writing Psot in SOT marker */
12622                         opj_write_bytes(l_begin_data + 6,l_part_tile_size,4);                                   /* PSOT */
12623
12624                         if (l_cp->m_specific_param.m_enc.m_cinema) {
12625                                 j2k_update_tlm(p_j2k,l_part_tile_size);
12626                         }
12627
12628                         ++p_j2k->m_specific_param.m_encoder.m_current_tile_part_number;
12629                 }
12630         }
12631
12632         *p_data_written = l_nb_bytes_written;
12633
12634         return OPJ_TRUE;
12635 }
12636
12637 /**
12638  * Writes the updated tlm.
12639  *
12640  * @param       p_stream                the stream to write data to.
12641  * @param       p_j2k                   J2K codec.
12642  * @param       p_manager               the user event manager.
12643 */
12644 opj_bool j2k_write_updated_tlm( opj_j2k_v2_t *p_j2k,
12645                                                                 struct opj_stream_private *p_stream,
12646                                                                 struct opj_event_mgr * p_manager )
12647 {
12648         OPJ_UINT32 l_tlm_size;
12649         OPJ_SIZE_T l_tlm_position, l_current_position;
12650
12651         /* preconditions */
12652         assert(p_j2k != 00);
12653         assert(p_manager != 00);
12654         assert(p_stream != 00);
12655
12656         l_tlm_size = 5 * p_j2k->m_specific_param.m_encoder.m_total_tile_parts;
12657         l_tlm_position = 6 + p_j2k->m_specific_param.m_encoder.m_tlm_start;
12658         l_current_position = opj_stream_tell(p_stream);
12659
12660         if (! opj_stream_seek(p_stream,l_tlm_position,p_manager)) {
12661                 return OPJ_FALSE;
12662         }
12663
12664         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) {
12665                 return OPJ_FALSE;
12666         }
12667
12668         if (! opj_stream_seek(p_stream,l_current_position,p_manager)) {
12669                 return OPJ_FALSE;
12670         }
12671
12672         return OPJ_TRUE;
12673 }
12674
12675
12676 /**
12677  * Ends the encoding, i.e. frees memory.
12678  *
12679  * @param       p_stream                                the stream to write data to.
12680  * @param       p_j2k                           J2K codec.
12681  * @param       p_manager               the user event manager.
12682 */
12683 opj_bool j2k_end_encoding(      opj_j2k_v2_t *p_j2k,
12684                                                         struct opj_stream_private *p_stream,
12685                                                         struct opj_event_mgr * p_manager )
12686 {
12687         /* preconditions */
12688         assert(p_j2k != 00);
12689         assert(p_manager != 00);
12690         assert(p_stream != 00);
12691
12692         tcd_destroy_v2(p_j2k->m_tcd);
12693         p_j2k->m_tcd = 00;
12694
12695         if (p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer) {
12696                 opj_free(p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer);
12697                 p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer = 0;
12698                 p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current = 0;
12699         }
12700
12701         if (p_j2k->m_specific_param.m_encoder.m_encoded_tile_data) {
12702                 opj_free(p_j2k->m_specific_param.m_encoder.m_encoded_tile_data);
12703                 p_j2k->m_specific_param.m_encoder.m_encoded_tile_data = 0;
12704         }
12705
12706         p_j2k->m_specific_param.m_encoder.m_encoded_tile_size = 0;
12707
12708         return OPJ_TRUE;
12709 }
12710
12711 /**
12712  * Destroys the memory associated with the decoding of headers.
12713  */
12714 opj_bool j2k_destroy_header_memory (opj_j2k_v2_t * p_j2k,
12715                                                                         opj_stream_private_t *p_stream,
12716                                                                         opj_event_mgr_t * p_manager )
12717 {
12718         /* preconditions */
12719         assert(p_j2k != 00);
12720         assert(p_stream != 00);
12721         assert(p_manager != 00);
12722
12723         if (p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
12724                 opj_free(p_j2k->m_specific_param.m_encoder.m_header_tile_data);
12725                 p_j2k->m_specific_param.m_encoder.m_header_tile_data = 0;
12726         }
12727
12728         p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = 0;
12729
12730         return OPJ_TRUE;
12731 }
12732
12733
12734 /**
12735  * Inits the Info
12736  *
12737  * @param       p_stream                                the stream to write data to.
12738  * @param       p_j2k                           J2K codec.
12739  * @param       p_manager               the user event manager.
12740 */
12741 opj_bool j2k_init_info( opj_j2k_v2_t *p_j2k,
12742                                                 struct opj_stream_private *p_stream,
12743                                                 struct opj_event_mgr * p_manager )
12744 {
12745         opj_codestream_info_t * l_cstr_info = 00;
12746
12747         /* preconditions */
12748         assert(p_j2k != 00);
12749         assert(p_manager != 00);
12750         assert(p_stream != 00);
12751   (void)l_cstr_info;
12752
12753         /* TODO mergeV2: check this part which use cstr_info */
12754         /*l_cstr_info = p_j2k->cstr_info;
12755
12756         if (l_cstr_info)  {
12757                 OPJ_UINT32 compno;
12758                 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));
12759
12760                 l_cstr_info->image_w = p_j2k->m_image->x1 - p_j2k->m_image->x0;
12761                 l_cstr_info->image_h = p_j2k->m_image->y1 - p_j2k->m_image->y0;
12762
12763                 l_cstr_info->prog = (&p_j2k->m_cp.tcps[0])->prg;
12764
12765                 l_cstr_info->tw = p_j2k->m_cp.tw;
12766                 l_cstr_info->th = p_j2k->m_cp.th;
12767
12768                 l_cstr_info->tile_x = p_j2k->m_cp.tdx;*/        /* new version parser */
12769                 /*l_cstr_info->tile_y = p_j2k->m_cp.tdy;*/      /* new version parser */
12770                 /*l_cstr_info->tile_Ox = p_j2k->m_cp.tx0;*/     /* new version parser */
12771                 /*l_cstr_info->tile_Oy = p_j2k->m_cp.ty0;*/     /* new version parser */
12772
12773                 /*l_cstr_info->numcomps = p_j2k->m_image->numcomps;
12774
12775                 l_cstr_info->numlayers = (&p_j2k->m_cp.tcps[0])->numlayers;
12776
12777                 l_cstr_info->numdecompos = (OPJ_INT32*) opj_malloc(p_j2k->m_image->numcomps * sizeof(OPJ_INT32));
12778
12779                 for (compno=0; compno < p_j2k->m_image->numcomps; compno++) {
12780                         l_cstr_info->numdecompos[compno] = (&p_j2k->m_cp.tcps[0])->tccps->numresolutions - 1;
12781                 }
12782
12783                 l_cstr_info->D_max = 0.0;       */      /* ADD Marcela */
12784
12785                 /*l_cstr_info->main_head_start = opj_stream_tell(p_stream);*/ /* position of SOC */
12786
12787                 /*l_cstr_info->maxmarknum = 100;
12788                 l_cstr_info->marker = (opj_marker_info_t *) opj_malloc(l_cstr_info->maxmarknum * sizeof(opj_marker_info_t));
12789                 l_cstr_info->marknum = 0;
12790         }*/
12791
12792         return j2k_calculate_tp_v2(p_j2k,&(p_j2k->m_cp),&p_j2k->m_specific_param.m_encoder.m_total_tile_parts,p_j2k->m_private_image,p_manager);
12793 }
12794
12795 /**
12796  * Creates a tile-coder decoder.
12797  *
12798  * @param       p_stream                the stream to write data to.
12799  * @param       p_j2k                   J2K codec.
12800  * @param       p_manager               the user event manager.
12801 */
12802 opj_bool j2k_create_tcd(opj_j2k_v2_t *p_j2k,
12803                                                 struct opj_stream_private *p_stream,
12804                                                 struct opj_event_mgr * p_manager )
12805 {
12806         /* preconditions */
12807         assert(p_j2k != 00);
12808         assert(p_manager != 00);
12809         assert(p_stream != 00);
12810
12811         p_j2k->m_tcd = tcd_create_v2(OPJ_FALSE);
12812
12813         if (! p_j2k->m_tcd) {
12814                 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to create Tile Coder\n");
12815                 return OPJ_FALSE;
12816         }
12817
12818         if (! tcd_init_v2(p_j2k->m_tcd,p_j2k->m_private_image,&p_j2k->m_cp)) {
12819                 tcd_destroy_v2(p_j2k->m_tcd);
12820                 p_j2k->m_tcd = 00;
12821                 return OPJ_FALSE;
12822         }
12823
12824         return OPJ_TRUE;
12825 }
12826
12827
12828 /**
12829  * Writes a tile.
12830  * @param       p_j2k           the jpeg2000 codec.
12831  * @param       p_stream                        the stream to write data to.
12832  * @param       p_manager       the user event manager.
12833  */
12834 opj_bool j2k_write_tile (opj_j2k_v2_t * p_j2k,
12835                                                  OPJ_UINT32 p_tile_index,
12836                                                  OPJ_BYTE * p_data,
12837                                                  OPJ_UINT32 p_data_size,
12838                                                  opj_stream_private_t *p_stream,
12839                                                  opj_event_mgr_t * p_manager )
12840 {
12841         if (! j2k_pre_write_tile(p_j2k,p_tile_index,p_stream,p_manager)) {
12842                 opj_event_msg_v2(p_manager, EVT_ERROR, "Error while j2k_pre_write_tile with tile index = %d\n", p_tile_index);
12843                 return OPJ_FALSE;
12844         }
12845         else {
12846                 if (! j2k_post_write_tile(p_j2k,p_data,p_data_size,p_stream,p_manager)) {
12847                         opj_event_msg_v2(p_manager, EVT_ERROR, "Error while j2k_post_write_tile with tile index = %d\n", p_tile_index);
12848                         return OPJ_FALSE;
12849                 }
12850         }
12851
12852         return OPJ_TRUE;
12853 }