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