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