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