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