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