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