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