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