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