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