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