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