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