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