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