rename j2k_create_decompress_v2 to opj_j2k_create_decompress
[openjpeg.git] / libopenjpeg / j2k.c
1 /*
2  * Copyright (c) 2002-2007, Communications and Remote Sensing Laboratory, Universite catholique de Louvain (UCL), Belgium
3  * Copyright (c) 2002-2007, Professor Benoit Macq
4  * Copyright (c) 2001-2003, David Janssens
5  * Copyright (c) 2002-2003, Yannick Verschueren
6  * Copyright (c) 2003-2007, Francois-Olivier Devaux and Antonin Descampe
7  * Copyright (c) 2005, Herve Drolon, FreeImage Team
8  * Copyright (c) 2008, Jerome Fimes, Communications & Systemes <jerome.fimes@c-s.fr>
9  * Copyright (c) 2006-2007, Parvatha Elangovan
10  * Copyright (c) 2010-2011, Kaori Hagihara
11  * Copyright (c) 2011-2012, Mickael Savinaud, Communications & Systemes <mickael.savinaud@c-s.fr>
12  * All rights reserved.
13  *
14  * Redistribution and use in source and binary forms, with or without
15  * modification, are permitted provided that the following conditions
16  * are met:
17  * 1. Redistributions of source code must retain the above copyright
18  *    notice, this list of conditions and the following disclaimer.
19  * 2. Redistributions in binary form must reproduce the above copyright
20  *    notice, this list of conditions and the following disclaimer in the
21  *    documentation and/or other materials provided with the distribution.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
24  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
27  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
30  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
31  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
32  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33  * POSSIBILITY OF SUCH DAMAGE.
34  */
35
36 #include "opj_includes.h"
37
38 /** @defgroup J2K J2K - JPEG-2000 codestream reader/writer */
39 /*@{*/
40
41 /** @name Local static functions */
42 /*@{*/
43
44 /**
45  * Sets up the procedures to do on reading header. Developpers wanting to extend the library can add their own reading procedures.
46  */
47 void j2k_setup_header_reading (opj_j2k_v2_t *p_j2k);
48
49 /**
50  * The read header procedure.
51  */
52 static opj_bool j2k_read_header_procedure(  opj_j2k_v2_t *p_j2k,
53                                             opj_stream_private_t *p_stream,
54                                             opj_event_mgr_t * p_manager);
55
56 /**
57  * The default encoding validation procedure without any extension.
58  *
59  * @param       p_j2k                   the jpeg2000 codec to validate.
60  * @param       p_stream                the input stream to validate.
61  * @param       p_manager               the user event manager.
62  *
63  * @return true if the parameters are correct.
64  */
65 static opj_bool j2k_encoding_validation (   opj_j2k_v2_t * p_j2k,
66                                             opj_stream_private_t *p_stream,
67                                             opj_event_mgr_t * p_manager );
68
69 /**
70  * The default decoding validation procedure without any extension.
71  *
72  * @param       p_j2k                   the jpeg2000 codec to validate.
73  * @param       p_stream                                the input stream to validate.
74  * @param       p_manager               the user event manager.
75  *
76  * @return true if the parameters are correct.
77  */
78 static opj_bool j2k_decoding_validation (   opj_j2k_v2_t * p_j2k,
79                                             opj_stream_private_t *p_stream,
80                                             opj_event_mgr_t * p_manager );
81
82 /**
83  * Sets up the validation ,i.e. adds the procedures to lauch to make sure the codec parameters
84  * are valid. Developpers wanting to extend the library can add their own validation procedures.
85  */
86 static void j2k_setup_encoding_validation (opj_j2k_v2_t *p_j2k);
87
88 /**
89  * Sets up the validation ,i.e. adds the procedures to lauch to make sure the codec parameters
90  * are valid. Developpers wanting to extend the library can add their own validation procedures.
91  */
92 static void j2k_setup_decoding_validation (opj_j2k_v2_t *p_j2k);
93
94 /**
95  * Sets up the validation ,i.e. adds the procedures to lauch to make sure the codec parameters
96  * are valid. Developpers wanting to extend the library can add their own validation procedures.
97  */
98 static void j2k_setup_end_compress (opj_j2k_v2_t *p_j2k);
99
100 /**
101  * The mct encoding validation procedure.
102  *
103  * @param       p_j2k                   the jpeg2000 codec to validate.
104  * @param       p_stream                                the input stream to validate.
105  * @param       p_manager               the user event manager.
106  *
107  * @return true if the parameters are correct.
108  */
109 static opj_bool j2k_mct_validation (opj_j2k_v2_t * p_j2k,
110                                     opj_stream_private_t *p_stream,
111                                     opj_event_mgr_t * p_manager );
112
113 /**
114  * Builds the tcd decoder to use to decode tile.
115  */
116 static opj_bool j2k_build_decoder ( opj_j2k_v2_t * p_j2k,
117                                     opj_stream_private_t *p_stream,
118                                     opj_event_mgr_t * p_manager );
119 /**
120  * Builds the tcd encoder to use to encode tile.
121  */
122 static opj_bool j2k_build_encoder ( opj_j2k_v2_t * p_j2k,
123                                     opj_stream_private_t *p_stream,
124                                     opj_event_mgr_t * p_manager );
125
126 /**
127  * Creates a tile-coder decoder.
128  *
129  * @param       p_stream                        the stream to write data to.
130  * @param       p_j2k                           J2K codec.
131  * @param       p_manager                   the user event manager.
132 */
133 static opj_bool j2k_create_tcd( opj_j2k_v2_t *p_j2k,
134                                                                 struct opj_stream_private *p_stream,
135                                                                 struct opj_event_mgr * p_manager );
136
137 /**
138  * Excutes the given procedures on the given codec.
139  *
140  * @param       p_procedure_list        the list of procedures to execute
141  * @param       p_j2k                           the jpeg2000 codec to execute the procedures on.
142  * @param       p_stream                        the stream to execute the procedures on.
143  * @param       p_manager                       the user manager.
144  *
145  * @return      true                            if all the procedures were successfully executed.
146  */
147 static opj_bool j2k_exec (  opj_j2k_v2_t * p_j2k,
148                             opj_procedure_list_t * p_procedure_list,
149                             opj_stream_private_t *p_stream,
150                             opj_event_mgr_t * p_manager);
151
152 /**
153  * Updates the rates of the tcp.
154  *
155  * @param       p_stream                                the stream to write data to.
156  * @param       p_j2k                           J2K codec.
157  * @param       p_manager               the user event manager.
158 */
159 static opj_bool j2k_update_rates(       opj_j2k_v2_t *p_j2k,
160                                                                         opj_stream_private_t *p_stream,
161                                                                         opj_event_mgr_t * p_manager );
162
163 /**
164  * Copies the decoding tile parameters onto all the tile parameters.
165  * Creates also the tile decoder.
166  */
167 static opj_bool j2k_copy_default_tcp_and_create_tcd (   opj_j2k_v2_t * p_j2k,
168                                                         opj_stream_private_t *p_stream,
169                                                         opj_event_mgr_t * p_manager );
170
171 /**
172  * Destroys the memory associated with the decoding of headers.
173  */
174 static opj_bool j2k_destroy_header_memory ( opj_j2k_v2_t * p_j2k,
175                                             opj_stream_private_t *p_stream,
176                                             opj_event_mgr_t * p_manager );
177
178 /**
179  * Reads the lookup table containing all the marker, status and action, and returns the handler associated
180  * with the marker value.
181  * @param       p_id            Marker value to look up
182  *
183  * @return      the handler associated with the id.
184 */
185 static const struct opj_dec_memory_marker_handler * j2k_get_marker_handler (OPJ_UINT32 p_id);
186
187 /**
188  * Destroys a tile coding parameter structure.
189  *
190  * @param       p_tcp           the tile coding parameter to destroy.
191  */
192 static void j2k_tcp_destroy (opj_tcp_v2_t *p_tcp);
193
194 /**
195  * Destroys the data inside a tile coding parameter structure.
196  *
197  * @param       p_tcp           the tile coding parameter which contain data to destroy.
198  */
199 static void j2k_tcp_data_destroy (opj_tcp_v2_t *p_tcp);
200
201 /**
202  * Destroys a coding parameter structure.
203  *
204  * @param       p_cp            the coding parameter to destroy.
205  */
206 static void j2k_cp_destroy (opj_cp_v2_t *p_cp);
207
208
209 /**
210  * Writes a SPCod or SPCoc element, i.e. the coding style of a given component of a tile.
211  *
212  * @param       p_comp_no       the component number to output.
213  * @param       p_stream                        the stream to write data to.
214  * @param       p_j2k                   J2K codec.
215  * @param       p_manager       the user event manager.
216  *
217 */
218 static opj_bool j2k_write_SPCod_SPCoc(  opj_j2k_v2_t *p_j2k,
219                                                                                 OPJ_UINT32 p_tile_no,
220                                                                                 OPJ_UINT32 p_comp_no,
221                                                                                 OPJ_BYTE * p_data,
222                                                                                 OPJ_UINT32 * p_header_size,
223                                                                                 struct opj_event_mgr * p_manager );
224
225 /**
226  * Gets the size taken by writing a SPCod or SPCoc for the given tile and component.
227  *
228  * @param       p_tile_no               the tile index.
229  * @param       p_comp_no               the component being outputted.
230  * @param       p_j2k                   the J2K codec.
231  *
232  * @return      the number of bytes taken by the SPCod element.
233  */
234 static OPJ_UINT32 j2k_get_SPCod_SPCoc_size (opj_j2k_v2_t *p_j2k,
235                                                                                         OPJ_UINT32 p_tile_no,
236                                                                                         OPJ_UINT32 p_comp_no );
237
238 /**
239  * Reads a SPCod or SPCoc element, i.e. the coding style of a given component of a tile.
240  * @param       p_header_data   the data contained in the COM box.
241  * @param       p_j2k                   the jpeg2000 codec.
242  * @param       p_header_size   the size of the data contained in the COM marker.
243  * @param       p_manager               the user event manager.
244 */
245 static opj_bool j2k_read_SPCod_SPCoc(   opj_j2k_v2_t *p_j2k,
246                                         OPJ_UINT32 compno,
247                                         OPJ_BYTE * p_header_data,
248                                         OPJ_UINT32 * p_header_size,
249                                         opj_event_mgr_t * p_manager );
250
251 /**
252  * Gets the size taken by writing SQcd or SQcc element, i.e. the quantization values of a band in the QCD or QCC.
253  *
254  * @param       p_tile_no               the tile index.
255  * @param       p_comp_no               the component being outputted.
256  * @param       p_j2k                   the J2K codec.
257  *
258  * @return      the number of bytes taken by the SPCod element.
259  */
260 static OPJ_UINT32 j2k_get_SQcd_SQcc_size (      opj_j2k_v2_t *p_j2k,
261                                                                                 OPJ_UINT32 p_tile_no,
262                                                                                 OPJ_UINT32 p_comp_no );
263
264 /**
265  * Writes a SQcd or SQcc element, i.e. the quantization values of a band in the QCD or QCC.
266  *
267  * @param       p_tile_no               the tile to output.
268  * @param       p_comp_no               the component number to output.
269  * @param       p_data                  the data buffer.
270  * @param       p_header_size   pointer to the size of the data buffer, it is changed by the function.
271  * @param       p_j2k                   J2K codec.
272  * @param       p_manager               the user event manager.
273  *
274 */
275 static opj_bool j2k_write_SQcd_SQcc(opj_j2k_v2_t *p_j2k,
276                                                                         OPJ_UINT32 p_tile_no,
277                                                                         OPJ_UINT32 p_comp_no,
278                                                                         OPJ_BYTE * p_data,
279                                                                         OPJ_UINT32 * p_header_size,
280                                                                         opj_event_mgr_t * p_manager);
281
282 /**
283  * Updates the Tile Length Marker.
284  */
285 static void j2k_update_tlm ( opj_j2k_v2_t * p_j2k, OPJ_UINT32 p_tile_part_size);
286
287 /**
288  * Reads a SQcd or SQcc element, i.e. the quantization values of a band in the QCD or QCC.
289  *
290  * @param       p_tile_no               the tile to output.
291  * @param       p_comp_no               the component number to output.
292  * @param       p_data                  the data buffer.
293  * @param       p_header_size   pointer to the size of the data buffer, it is changed by the function.
294  * @param       p_j2k                           J2K codec.
295  * @param       p_manager               the user event manager.
296  *
297 */
298 static opj_bool j2k_read_SQcd_SQcc( opj_j2k_v2_t *p_j2k,
299                                     OPJ_UINT32 compno,
300                                     OPJ_BYTE * p_header_data,
301                                     OPJ_UINT32 * p_header_size,
302                                     opj_event_mgr_t * p_manager );
303
304 /**
305  * Copies the tile component parameters of all the component from the first tile component.
306  *
307  * @param               p_j2k           the J2k codec.
308  */
309 static void j2k_copy_tile_component_parameters( opj_j2k_v2_t *p_j2k );
310
311 /**
312  * Copies the tile quantization parameters of all the component from the first tile component.
313  *
314  * @param               p_j2k           the J2k codec.
315  */
316 static void j2k_copy_tile_quantization_parameters( opj_j2k_v2_t *p_j2k );
317
318 /**
319  * Reads the tiles.
320  */
321 static opj_bool j2k_decode_tiles (      opj_j2k_v2_t *p_j2k,
322                                     opj_stream_private_t *p_stream,
323                                     opj_event_mgr_t * p_manager);
324
325
326 static opj_bool j2k_pre_write_tile ( opj_j2k_v2_t * p_j2k,
327                                                                          OPJ_UINT32 p_tile_index,
328                                                                          opj_stream_private_t *p_stream,
329                                                                          opj_event_mgr_t * p_manager );
330
331 static opj_bool j2k_update_image_data (opj_tcd_v2_t * p_tcd, OPJ_BYTE * p_data, opj_image_t* p_output_image);
332
333 static void j2k_get_tile_data (opj_tcd_v2_t * p_tcd, OPJ_BYTE * p_data);
334
335 static opj_bool j2k_post_write_tile (opj_j2k_v2_t * p_j2k,
336                                                                          OPJ_BYTE * p_data,
337                                                                          OPJ_UINT32 p_data_size,
338                                                                          opj_stream_private_t *p_stream,
339                                                                          opj_event_mgr_t * p_manager );
340
341 /**
342  * Sets up the procedures to do on writing header.
343  * Developers wanting to extend the library can add their own writing procedures.
344  */
345 static void j2k_setup_header_writting (opj_j2k_v2_t *p_j2k);
346
347 static opj_bool j2k_write_first_tile_part(      opj_j2k_v2_t *p_j2k,
348                                                                                         OPJ_BYTE * p_data,
349                                                                                         OPJ_UINT32 * p_data_written,
350                                                                                         OPJ_UINT32 p_total_data_size,
351                                                                                         opj_stream_private_t *p_stream,
352                                                                                         struct opj_event_mgr * p_manager );
353
354 static opj_bool j2k_write_all_tile_parts(       opj_j2k_v2_t *p_j2k,
355                                                                                         OPJ_BYTE * p_data,
356                                                                                         OPJ_UINT32 * p_data_written,
357                                                                                         OPJ_UINT32 p_total_data_size,
358                                                                                         opj_stream_private_t *p_stream,
359                                                                                         struct opj_event_mgr * p_manager );
360
361 /**
362  * Gets the offset of the header.
363  *
364  * @param       p_stream                the stream to write data to.
365  * @param       p_j2k                   J2K codec.
366  * @param       p_manager               the user event manager.
367 */
368 static opj_bool j2k_get_end_header(     opj_j2k_v2_t *p_j2k,
369                                                                         struct opj_stream_private *p_stream,
370                                                                         struct opj_event_mgr * p_manager );
371
372 /*
373  * -----------------------------------------------------------------------
374  * -----------------------------------------------------------------------
375  * -----------------------------------------------------------------------
376  */
377
378 /**
379 Write the SOC marker (Start Of Codestream)
380 @param j2k J2K handle
381 */
382 static void j2k_write_soc(opj_j2k_t *j2k);
383
384 /**
385  * Writes the SOC marker (Start Of Codestream)
386  *
387  * @param       p_stream                        the stream to write data to.
388  * @param       p_j2k                   J2K codec.
389  * @param       p_manager       the user event manager.
390 */
391 static opj_bool j2k_write_soc_v2(       opj_j2k_v2_t *p_j2k,
392                                                                         struct opj_stream_private *p_stream,
393                                                                         struct opj_event_mgr * p_manager );
394
395 /**
396 Read the SOC marker (Start of Codestream)
397 @param j2k J2K handle
398 */
399 static void j2k_read_soc(opj_j2k_t *j2k);
400
401 /**
402  * Reads a SOC marker (Start of Codestream)
403  * @param       p_header_data   the data contained in the SOC box.
404  * @param       jp2                             the jpeg2000 file codec.
405  * @param       p_header_size   the size of the data contained in the SOC marker.
406  * @param       p_manager               the user event manager.
407 */
408 static opj_bool j2k_read_soc_v2(
409                                         opj_j2k_v2_t *p_j2k,
410                                         struct opj_stream_private *p_stream,
411                                         struct opj_event_mgr * p_manager
412                                  );
413
414 /**
415 Write the SIZ marker (image and tile size)
416 @param j2k J2K handle
417 */
418 static void j2k_write_siz(opj_j2k_t *j2k);
419
420 /**
421  * Writes the SIZ marker (image and tile size)
422  *
423  * @param       p_stream                        the stream to write data to.
424  * @param       p_j2k                   J2K codec.
425  * @param       p_manager       the user event manager.
426 */
427 static opj_bool j2k_write_siz_v2(       opj_j2k_v2_t *p_j2k,
428                                                                         struct opj_stream_private *p_stream,
429                                                                         struct opj_event_mgr * p_manager );
430
431 /**
432 Read the SIZ marker (image and tile size)
433 @param j2k J2K handle
434 */
435 static void j2k_read_siz(opj_j2k_t *j2k);
436
437 /**
438  * Reads a SIZ marker (image and tile size)
439  * @param       p_header_data   the data contained in the SIZ box.
440  * @param       jp2                             the jpeg2000 file codec.
441  * @param       p_header_size   the size of the data contained in the SIZ marker.
442  * @param       p_manager               the user event manager.
443 */
444 static opj_bool j2k_read_siz_v2 (
445                                                   opj_j2k_v2_t *p_j2k,
446                                                   OPJ_BYTE * p_header_data,
447                                                   OPJ_UINT32 p_header_size,
448                                                   struct opj_event_mgr * p_manager
449                                         );
450
451 /**
452 Write the COM marker (comment)
453 @param j2k J2K handle
454 */
455 static void j2k_write_com(opj_j2k_t *j2k);
456
457 /**
458  * Writes the COM marker (comment)
459  * 
460  * @param       p_stream                        the stream to write data to.
461  * @param       p_j2k                   J2K codec.
462  * @param       p_manager       the user event manager.
463 */
464 static opj_bool j2k_write_com_v2(       opj_j2k_v2_t *p_j2k,
465                                                                         struct opj_stream_private *p_stream,
466                                                                         struct opj_event_mgr * p_manager );
467
468 /**
469 Read the COM marker (comment)
470 @param j2k J2K handle
471 */
472 static void j2k_read_com(opj_j2k_t *j2k);
473 /**
474  * Reads a COM marker (comments)
475  * @param       p_header_data   the data contained in the COM box.
476  * @param       jp2                             the jpeg2000 file codec.
477  * @param       p_header_size   the size of the data contained in the COM marker.
478  * @param       p_manager               the user event manager.
479 */
480 static opj_bool j2k_read_com_v2 (
481                                         opj_j2k_v2_t *p_j2k,
482                                         OPJ_BYTE * p_header_data,
483                                         OPJ_UINT32 p_header_size,
484                                         struct opj_event_mgr * p_manager
485                                         );
486 /**
487 Write the value concerning the specified component in the marker COD and COC
488 @param j2k J2K handle
489 @param compno Number of the component concerned by the information written
490 */
491 static void j2k_write_cox(opj_j2k_t *j2k, int compno);
492 /**
493 Read the value concerning the specified component in the marker COD and COC
494 @param j2k J2K handle
495 @param compno Number of the component concerned by the information read
496 */
497 static void j2k_read_cox(opj_j2k_t *j2k, int compno);
498 /**
499 Write the COD marker (coding style default)
500 @param j2k J2K handle
501 */
502 static void j2k_write_cod(opj_j2k_t *j2k);
503
504 /**
505  * Writes the COD marker (Coding style default)
506  *
507  * @param       p_stream                        the stream to write data to.
508  * @param       p_j2k                   J2K codec.
509  * @param       p_manager       the user event manager.
510 */
511 static opj_bool j2k_write_cod_v2(       opj_j2k_v2_t *p_j2k,
512                                                                         struct opj_stream_private *p_stream,
513                                                                         struct opj_event_mgr * p_manager );
514
515 /**
516 Read the COD marker (coding style default)
517 @param j2k J2K handle
518 */
519 static void j2k_read_cod(opj_j2k_t *j2k);
520
521 /**
522  * Reads a COD marker (Coding Styke defaults)
523  * @param       p_header_data   the data contained in the COD box.
524  * @param       p_j2k                   the jpeg2000 codec.
525  * @param       p_header_size   the size of the data contained in the COD marker.
526  * @param       p_manager               the user event manager.
527 */
528 static opj_bool j2k_read_cod_v2 (
529                                         opj_j2k_v2_t *p_j2k,
530                                         OPJ_BYTE * p_header_data,
531                                         OPJ_UINT32 p_header_size,
532                                         struct opj_event_mgr * p_manager
533                                         );
534
535 /**
536 Write the COC marker (coding style component)
537 @param j2k J2K handle
538 @param compno Number of the component concerned by the information written
539 */
540 static void j2k_write_coc(opj_j2k_t *j2k, int compno);
541
542 /**
543  * Writes the COC marker (Coding style component)
544  *
545  * @param       p_comp_number   the index of the component to output.
546  * @param       p_stream                                the stream to write data to.
547  * @param       p_j2k                           J2K codec.
548  * @param       p_manager               the user event manager.
549 */
550 static opj_bool j2k_write_coc_v2( opj_j2k_v2_t *p_j2k,
551                                                         OPJ_UINT32 p_comp_number,
552                                                         struct opj_stream_private *p_stream,
553                                                         struct opj_event_mgr * p_manager
554                                                   );
555 /**
556  * Writes the COC marker (Coding style component)
557  *
558  * @param       p_comp_no               the index of the component to output.
559  * @param       p_stream                the stream to write data to.
560  * @param       p_j2k                   J2K codec.
561  * @param       p_manager               the user event manager.
562 */
563 static void j2k_write_coc_in_memory(opj_j2k_v2_t *p_j2k,
564                                                                         OPJ_UINT32 p_comp_no,
565                                                                         OPJ_BYTE * p_data,
566                                                                         OPJ_UINT32 * p_data_written,
567                                                                         struct opj_event_mgr * p_manager );
568
569 /**
570  * Gets the maximum size taken by a coc.
571  *
572  * @param       p_j2k   the jpeg2000 codec to use.
573  */
574 static OPJ_UINT32 j2k_get_max_coc_size(opj_j2k_v2_t *p_j2k);
575
576 /**
577 Read the COC marker (coding style component)
578 @param j2k J2K handle
579 */
580 static void j2k_read_coc(opj_j2k_t *j2k);
581
582 /**
583  * Reads a COC marker (Coding Style Component)
584  * @param       p_header_data   the data contained in the COC box.
585  * @param       p_j2k                   the jpeg2000 codec.
586  * @param       p_header_size   the size of the data contained in the COC marker.
587  * @param       p_manager               the user event manager.
588 */
589 static opj_bool j2k_read_coc_v2 (
590                                         opj_j2k_v2_t *p_j2k,
591                                         OPJ_BYTE * p_header_data,
592                                         OPJ_UINT32 p_header_size,
593                                         struct opj_event_mgr * p_manager
594                                         );
595
596 /**
597 Write the value concerning the specified component in the marker QCD and QCC
598 @param j2k J2K handle
599 @param compno Number of the component concerned by the information written
600 */
601 static void j2k_write_qcx(opj_j2k_t *j2k, int compno);
602 /**
603 Read the value concerning the specified component in the marker QCD and QCC
604 @param j2k J2K handle
605 @param compno Number of the component concern by the information read
606 @param len Length of the information in the QCX part of the marker QCD/QCC
607 */
608 static void j2k_read_qcx(opj_j2k_t *j2k, int compno, int len);
609 /**
610 Write the QCD marker (quantization default)
611 @param j2k J2K handle
612 */
613 static void j2k_write_qcd(opj_j2k_t *j2k);
614
615 /**
616  * Writes the QCD marker (quantization default)
617  *
618  * @param       p_comp_number   the index of the component to output.
619  * @param       p_stream                the stream to write data to.
620  * @param       p_j2k                   J2K codec.
621  * @param       p_manager               the user event manager.
622 */
623 static opj_bool j2k_write_qcd_v2(       opj_j2k_v2_t *p_j2k,
624                                                                         struct opj_stream_private *p_stream,
625                                                                         struct opj_event_mgr * p_manager );
626
627 /**
628 Read the QCD marker (quantization default)
629 @param j2k J2K handle
630 */
631 static void j2k_read_qcd(opj_j2k_t *j2k);
632
633 /**
634  * Reads a QCD marker (Quantization defaults)
635  * @param       p_header_data   the data contained in the QCD box.
636  * @param       p_j2k                   the jpeg2000 codec.
637  * @param       p_header_size   the size of the data contained in the QCD marker.
638  * @param       p_manager               the user event manager.
639 */
640 static opj_bool j2k_read_qcd_v2 (
641                                         opj_j2k_v2_t *p_j2k,
642                                         OPJ_BYTE * p_header_data,
643                                         OPJ_UINT32 p_header_size,
644                                         struct opj_event_mgr * p_manager
645                                         );
646
647 /**
648 Write the QCC marker (quantization component)
649 @param j2k J2K handle
650 @param compno Number of the component concerned by the information written
651 */
652 static void j2k_write_qcc(opj_j2k_t *j2k, int compno);
653
654 /**
655  * Writes the QCC marker (quantization component)
656  *
657  * @param       p_comp_no       the index of the component to output.
658  * @param       p_stream                the stream to write data to.
659  * @param       p_j2k                   J2K codec.
660  * @param       p_manager               the user event manager.
661 */
662 static opj_bool j2k_write_qcc_v2(       opj_j2k_v2_t *p_j2k,
663                                                                         OPJ_UINT32 p_comp_no,
664                                                                         struct opj_stream_private *p_stream,
665                                                                         struct opj_event_mgr * p_manager );
666
667 /**
668  * Writes the QCC marker (quantization component)
669  *
670  * @param       p_comp_no       the index of the component to output.
671  * @param       p_stream                the stream to write data to.
672  * @param       p_j2k                   J2K codec.
673  * @param       p_manager               the user event manager.
674 */
675 static void j2k_write_qcc_in_memory(opj_j2k_v2_t *p_j2k,
676                                                                         OPJ_UINT32 p_comp_no,
677                                                                         OPJ_BYTE * p_data,
678                                                                         OPJ_UINT32 * p_data_written,
679                                                                         struct opj_event_mgr * p_manager );
680
681 /**
682  * Gets the maximum size taken by a qcc.
683  */
684 static OPJ_UINT32 j2k_get_max_qcc_size (opj_j2k_v2_t *p_j2k);
685
686 /**
687 Read the QCC marker (quantization component)
688 @param j2k J2K handle
689 */
690 static void j2k_read_qcc(opj_j2k_t *j2k);
691 /**
692  * Reads a QCC marker (Quantization component)
693  * @param       p_header_data   the data contained in the QCC box.
694  * @param       p_j2k                   the jpeg2000 codec.
695  * @param       p_header_size   the size of the data contained in the QCC marker.
696  * @param       p_manager               the user event manager.
697 */
698 static opj_bool j2k_read_qcc_v2(
699                                                         opj_j2k_v2_t *p_j2k,
700                                                         OPJ_BYTE * p_header_data,
701                                                         OPJ_UINT32 p_header_size,
702                                                         struct opj_event_mgr * p_manager);
703
704 /**
705 Write the POC marker (progression order change)
706 @param j2k J2K handle
707 */
708 static void j2k_write_poc(opj_j2k_t *j2k);
709
710 /**
711  * Writes the POC marker (Progression Order Change)
712  * 
713  * @param       p_stream                                the stream to write data to.
714  * @param       p_j2k                           J2K codec.
715  * @param       p_manager               the user event manager.
716 */
717 static opj_bool j2k_write_poc_v2(       opj_j2k_v2_t *p_j2k,
718                                                                         struct opj_stream_private *p_stream,
719                                                                         struct opj_event_mgr * p_manager );
720
721 /**
722  * Writes the POC marker (Progression Order Change)
723  *
724  * @param       p_stream                the stream to write data to.
725  * @param       p_j2k                   J2K codec.
726  * @param       p_manager               the user event manager.
727  */
728 static void j2k_write_poc_in_memory(opj_j2k_v2_t *p_j2k,
729                                                                         OPJ_BYTE * p_data,
730                                                                         OPJ_UINT32 * p_data_written,
731                                                                         struct opj_event_mgr * p_manager );
732
733 /**
734  * Gets the maximum size taken by the writting of a POC.
735  */
736 static OPJ_UINT32 j2k_get_max_poc_size(opj_j2k_v2_t *p_j2k);
737
738 /**
739  * Gets the maximum size taken by the toc headers of all the tile parts of any given tile.
740  */
741 static OPJ_UINT32 j2k_get_max_toc_size (opj_j2k_v2_t *p_j2k);
742
743 /**
744  * Gets the maximum size taken by the headers of the SOT.
745  *
746  * @param       p_j2k   the jpeg2000 codec to use.
747  */
748 static OPJ_UINT32 j2k_get_specific_header_sizes(opj_j2k_v2_t *p_j2k);
749
750 /**
751 Read the POC marker (progression order change)
752 @param j2k J2K handle
753 */
754 static void j2k_read_poc(opj_j2k_t *j2k);
755 /**
756  * Reads a POC marker (Progression Order Change)
757  *
758  * @param       p_header_data   the data contained in the POC box.
759  * @param       p_j2k                   the jpeg2000 codec.
760  * @param       p_header_size   the size of the data contained in the POC marker.
761  * @param       p_manager               the user event manager.
762 */
763 static opj_bool j2k_read_poc_v2 (
764                                                 opj_j2k_v2_t *p_j2k,
765                                                 OPJ_BYTE * p_header_data,
766                                                 OPJ_UINT32 p_header_size,
767                                                 struct opj_event_mgr * p_manager
768                                         );
769
770 /**
771 Read the CRG marker (component registration)
772 @param j2k J2K handle
773 */
774 static void j2k_read_crg(opj_j2k_t *j2k);
775 /**
776  * Reads a CRG marker (Component registration)
777  *
778  * @param       p_header_data   the data contained in the TLM box.
779  * @param       p_j2k                   the jpeg2000 codec.
780  * @param       p_header_size   the size of the data contained in the TLM marker.
781  * @param       p_manager               the user event manager.
782 */
783 static opj_bool j2k_read_crg_v2 (
784                                                 opj_j2k_v2_t *p_j2k,
785                                                 OPJ_BYTE * p_header_data,
786                                                 OPJ_UINT32 p_header_size,
787                                                 struct opj_event_mgr * p_manager
788                                         );
789 /**
790 Read the TLM marker (tile-part lengths)
791 @param j2k J2K handle
792 */
793 static void j2k_read_tlm(opj_j2k_t *j2k);
794 /**
795  * Reads a TLM marker (Tile Length Marker)
796  *
797  * @param       p_header_data   the data contained in the TLM box.
798  * @param       p_j2k                   the jpeg2000 codec.
799  * @param       p_header_size   the size of the data contained in the TLM marker.
800  * @param       p_manager               the user event manager.
801 */
802 static opj_bool j2k_read_tlm_v2 (
803                                                 opj_j2k_v2_t *p_j2k,
804                                                 OPJ_BYTE * p_header_data,
805                                                 OPJ_UINT32 p_header_size,
806                                                 struct opj_event_mgr * p_manager
807                                         );
808
809 /**
810  * Writes the updated tlm.
811  *
812  * @param       p_stream                the stream to write data to.
813  * @param       p_j2k                   J2K codec.
814  * @param       p_manager               the user event manager.
815 */
816 static opj_bool j2k_write_updated_tlm(  opj_j2k_v2_t *p_j2k,
817                                                                                 struct opj_stream_private *p_stream,
818                                                                                 struct opj_event_mgr * p_manager );
819
820 /**
821 Read the PLM marker (packet length, main header)
822 @param j2k J2K handle
823 */
824 static void j2k_read_plm(opj_j2k_t *j2k);
825
826 /**
827  * Reads a PLM marker (Packet length, main header marker)
828  *
829  * @param       p_header_data   the data contained in the TLM box.
830  * @param       p_j2k                   the jpeg2000 codec.
831  * @param       p_header_size   the size of the data contained in the TLM marker.
832  * @param       p_manager               the user event manager.
833 */
834 static opj_bool j2k_read_plm_v2 (
835                                                 opj_j2k_v2_t *p_j2k,
836                                                 OPJ_BYTE * p_header_data,
837                                                 OPJ_UINT32 p_header_size,
838                                                 struct opj_event_mgr * p_manager
839                                         );
840 /**
841 Read the PLT marker (packet length, tile-part header)
842 @param j2k J2K handle
843 */
844 static void j2k_read_plt(opj_j2k_t *j2k);
845 /**
846  * Reads a PLT marker (Packet length, tile-part header)
847  *
848  * @param       p_header_data   the data contained in the PLT box.
849  * @param       p_j2k                   the jpeg2000 codec.
850  * @param       p_header_size   the size of the data contained in the PLT marker.
851  * @param       p_manager               the user event manager.
852 */
853 static opj_bool j2k_read_plt_v2 (
854                                                 opj_j2k_v2_t *p_j2k,
855                                                 OPJ_BYTE * p_header_data,
856                                                 OPJ_UINT32 p_header_size,
857                                                 struct opj_event_mgr * p_manager
858                                         );
859 /**
860 Read the PPM marker (packet packet headers, main header)
861 @param j2k J2K handle
862 */
863 static void j2k_read_ppm(opj_j2k_t *j2k);
864 /**
865  * Reads a PPM marker (Packed packet headers, main header)
866  *
867  * @param       p_header_data   the data contained in the POC box.
868  * @param       p_j2k                   the jpeg2000 codec.
869  * @param       p_header_size   the size of the data contained in the POC marker.
870  * @param       p_manager               the user event manager.
871 */
872 #if 0
873 static opj_bool j2k_read_ppm_v2 (
874                                                 opj_j2k_v2_t *p_j2k,
875                                                 OPJ_BYTE * p_header_data,
876                                                 OPJ_UINT32 p_header_size,
877                                                 struct opj_event_mgr * p_manager
878                                         );
879 #endif
880
881 static opj_bool j2k_read_ppm_v3 (
882                                                 opj_j2k_v2_t *p_j2k,
883                                                 OPJ_BYTE * p_header_data,
884                                                 OPJ_UINT32 p_header_size,
885                                                 struct opj_event_mgr * p_manager
886                                         );
887
888 /**
889 Read the PPT marker (packet packet headers, tile-part header)
890 @param j2k J2K handle
891 */
892 static void j2k_read_ppt(opj_j2k_t *j2k);
893 /**
894  * Reads a PPT marker (Packed packet headers, tile-part header)
895  *
896  * @param       p_header_data   the data contained in the PPT box.
897  * @param       p_j2k                   the jpeg2000 codec.
898  * @param       p_header_size   the size of the data contained in the PPT marker.
899  * @param       p_manager               the user event manager.
900 */
901 static opj_bool j2k_read_ppt_v2 (
902                                                 opj_j2k_v2_t *p_j2k,
903                                                 OPJ_BYTE * p_header_data,
904                                                 OPJ_UINT32 p_header_size,
905                                                 struct opj_event_mgr * p_manager
906                                         );
907 /**
908 Write the TLM marker (Mainheader)
909 @param j2k J2K handle
910 */
911 static void j2k_write_tlm(opj_j2k_t *j2k);
912
913 /**
914  * Writes the TLM marker (Tile Length Marker)
915  * 
916  * @param       p_stream                                the stream to write data to.
917  * @param       p_j2k                           J2K codec.
918  * @param       p_manager               the user event manager.
919 */
920 static opj_bool j2k_write_tlm_v2(       opj_j2k_v2_t *p_j2k,
921                                                                         struct opj_stream_private *p_stream,
922                                                                         struct opj_event_mgr * p_manager );
923
924 /**
925 Write the SOT marker (start of tile-part)
926 @param j2k J2K handle
927 */
928 static void j2k_write_sot(opj_j2k_t *j2k);
929 /**
930 Read the SOT marker (start of tile-part)
931 @param j2k J2K handle
932 */
933 static void j2k_read_sot(opj_j2k_t *j2k);
934
935 /**
936  * Writes the SOT marker (Start of tile-part)
937  *
938  * @param       p_stream                the stream to write data to.
939  * @param       p_j2k                   J2K codec.
940  * @param       p_manager               the user event manager.
941 */
942 static opj_bool j2k_write_sot_v2(       opj_j2k_v2_t *p_j2k,
943                                                                         OPJ_BYTE * p_data,
944                                                                         OPJ_UINT32 * p_data_written,
945                                                                         const struct opj_stream_private *p_stream,
946                                                                         struct opj_event_mgr * p_manager );
947
948 /**
949  * Reads a PPT marker (Packed packet headers, tile-part header)
950  *
951  * @param       p_header_data   the data contained in the PPT box.
952  * @param       p_j2k                   the jpeg2000 codec.
953  * @param       p_header_size   the size of the data contained in the PPT marker.
954  * @param       p_manager               the user event manager.
955 */
956 static opj_bool j2k_read_sot_v2 (
957                                                 opj_j2k_v2_t *p_j2k,
958                                                 OPJ_BYTE * p_header_data,
959                                                 OPJ_UINT32 p_header_size,
960                                                 struct opj_event_mgr * p_manager
961                                         );
962 /**
963 Write the SOD marker (start of data)
964 @param j2k J2K handle
965 @param tile_coder Pointer to a TCD handle
966 */
967 static void j2k_write_sod(opj_j2k_t *j2k, void *tile_coder);
968
969 /**
970  * Writes the SOD marker (Start of data)
971  *
972  * @param       p_stream                                the stream to write data to.
973  * @param       p_j2k                           J2K codec.
974  * @param       p_manager               the user event manager.
975 */
976 static opj_bool j2k_write_sod_v2(       opj_j2k_v2_t *p_j2k,
977                                                                         struct opj_tcd_v2 * p_tile_coder,
978                                                                         OPJ_BYTE * p_data,
979                                                                         OPJ_UINT32 * p_data_written,
980                                                                         OPJ_UINT32 p_total_data_size,
981                                                                         const struct opj_stream_private *p_stream,
982                                                                         struct opj_event_mgr * p_manager );
983
984 /**
985 Read the SOD marker (start of data)
986 @param j2k J2K handle
987 */
988 static void j2k_read_sod(opj_j2k_t *j2k);
989
990 /**
991  * Reads a SOD marker (Start Of Data)
992  *
993  * @param       p_header_data   the data contained in the SOD box.
994  * @param       p_j2k                   the jpeg2000 codec.
995  * @param       p_header_size   the size of the data contained in the SOD marker.
996  * @param       p_manager               the user event manager.
997 */
998 static opj_bool j2k_read_sod_v2 (
999                                                 opj_j2k_v2_t *p_j2k,
1000                                                 struct opj_stream_private *p_stream,
1001                                                 struct opj_event_mgr * p_manager
1002                                         );
1003
1004 /**
1005  * Updates the Tile Length Marker.
1006  */
1007 void j2k_update_tlm (opj_j2k_v2_t * p_j2k, OPJ_UINT32 p_tile_part_size )
1008 {
1009         opj_write_bytes(p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current,p_j2k->m_current_tile_number,1);                                    /* PSOT */
1010         ++p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current;
1011
1012         opj_write_bytes(p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current,p_tile_part_size,4);                                        /* PSOT */
1013         p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current += 4;
1014 }
1015
1016 /**
1017 Write the RGN marker (region-of-interest)
1018 @param j2k J2K handle
1019 @param compno Number of the component concerned by the information written
1020 @param tileno Number of the tile concerned by the information written
1021 */
1022 static void j2k_write_rgn(opj_j2k_t *j2k, int compno, int tileno);
1023
1024 /**
1025  * Writes the RGN marker (Region Of Interest)
1026  *
1027  * @param       p_tile_no               the tile to output
1028  * @param       p_comp_no               the component to output
1029  * @param       p_stream                                the stream to write data to.
1030  * @param       p_j2k                           J2K codec.
1031  * @param       p_manager               the user event manager.
1032 */
1033 static opj_bool j2k_write_rgn_v2(       opj_j2k_v2_t *p_j2k,
1034                                                                         OPJ_UINT32 p_tile_no,
1035                                                                         OPJ_UINT32 p_comp_no,
1036                                                                         struct opj_stream_private *p_stream,
1037                                                                         struct opj_event_mgr * p_manager );
1038
1039 /**
1040 Read the RGN marker (region-of-interest)
1041 @param j2k J2K handle
1042 */
1043 static void j2k_read_rgn(opj_j2k_t *j2k);
1044
1045 /**
1046  * Reads a RGN marker (Region Of Interest)
1047  *
1048  * @param       p_header_data   the data contained in the POC box.
1049  * @param       p_j2k                   the jpeg2000 codec.
1050  * @param       p_header_size   the size of the data contained in the POC marker.
1051  * @param       p_manager               the user event manager.
1052 */
1053 static opj_bool j2k_read_rgn_v2 (
1054                                                 opj_j2k_v2_t *p_j2k,
1055                                                 OPJ_BYTE * p_header_data,
1056                                                 OPJ_UINT32 p_header_size,
1057                                                 struct opj_event_mgr * p_manager
1058                                         ) ;
1059
1060 /**
1061  * Writes the EOC marker (End of Codestream)
1062  * 
1063  * @param       p_stream                the stream to write data to.
1064  * @param       p_j2k                   J2K codec.
1065  * @param       p_manager               the user event manager.
1066 */
1067 static opj_bool j2k_write_eoc_v2(       opj_j2k_v2_t *p_j2k,
1068                                                                         struct opj_stream_private *p_stream,
1069                                                                         struct opj_event_mgr * p_manager );
1070
1071 /**
1072 Write the EOC marker (end of codestream)
1073 @param j2k J2K handle
1074 */
1075 static void j2k_write_eoc(opj_j2k_t *j2k);
1076 /**
1077 Read the EOC marker (end of codestream)
1078 @param j2k J2K handle
1079 */
1080 static void j2k_read_eoc(opj_j2k_t *j2k);
1081
1082 /**
1083  * Reads a EOC marker (End Of Codestream)
1084  *
1085  * @param       p_header_data   the data contained in the SOD box.
1086  * @param       p_j2k                   the jpeg2000 codec.
1087  * @param       p_header_size   the size of the data contained in the SOD marker.
1088  * @param       p_manager               the user event manager.
1089 */
1090 #if 0
1091 static opj_bool j2k_read_eoc_v2 (
1092                                             opj_j2k_v2_t *p_j2k,
1093                                                 struct opj_stream_private *p_stream,
1094                                                 struct opj_event_mgr * p_manager
1095                                         ) ;
1096 #endif
1097
1098
1099
1100 /**
1101  * Writes the CBD-MCT-MCC-MCO markers (Multi components transform)
1102  *
1103  * @param       p_stream                        the stream to write data to.
1104  * @param       p_j2k                   J2K codec.
1105  * @param       p_manager       the user event manager.
1106 */
1107 static opj_bool j2k_write_mct_data_group(       opj_j2k_v2_t *p_j2k,
1108                                                                                         struct opj_stream_private *p_stream,
1109                                                                                         struct opj_event_mgr * p_manager );
1110
1111 /**
1112  * Inits the Info
1113  *
1114  * @param       p_stream                the stream to write data to.
1115  * @param       p_j2k                   J2K codec.
1116  * @param       p_manager               the user event manager.
1117 */
1118 static opj_bool j2k_init_info(  opj_j2k_v2_t *p_j2k,
1119                                                                 struct opj_stream_private *p_stream,
1120                                                                 struct opj_event_mgr * p_manager );
1121
1122 /**
1123 Read an unknown marker
1124 @param j2k J2K handle
1125 */
1126 static void j2k_read_unk(opj_j2k_t *j2k);
1127 /**
1128 Add main header marker information
1129 @param cstr_info Codestream information structure
1130 @param type marker type
1131 @param pos byte offset of marker segment
1132 @param len length of marker segment
1133  */
1134 static void j2k_add_mhmarker(opj_codestream_info_t *cstr_info, unsigned short int type, int pos, int len);
1135
1136 static void j2k_add_mhmarker_v2(opj_codestream_index_t *cstr_index, OPJ_UINT32 type, OPJ_OFF_T pos, OPJ_UINT32 len) ;
1137 /**
1138 Add tile header marker information
1139 @param tileno tile index number
1140 @param cstr_info Codestream information structure
1141 @param type marker type
1142 @param pos byte offset of marker segment
1143 @param len length of marker segment
1144  */
1145 static void j2k_add_tlmarker( int tileno, opj_codestream_info_t *cstr_info, unsigned short int type, int pos, int len);
1146
1147 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);
1148
1149 /**
1150  * Reads an unknown marker
1151  *
1152  * @param       p_stream                the stream object to read from.
1153  * @param       p_j2k                   the jpeg2000 codec.
1154  * @param       p_manager               the user event manager.
1155  *
1156  * @return      true                    if the marker could be deduced.
1157 */
1158 static opj_bool j2k_read_unk_v2 (       opj_j2k_v2_t *p_j2k,
1159                                                                         struct opj_stream_private *p_stream,
1160                                                                         OPJ_UINT32 *output_marker,
1161                                                                         struct opj_event_mgr * p_manager );
1162
1163 /**
1164  * Writes the MCT marker (Multiple Component Transform)
1165  *
1166  * @param       p_stream                                the stream to write data to.
1167  * @param       p_j2k                           J2K codec.
1168  * @param       p_manager               the user event manager.
1169 */
1170 static opj_bool j2k_write_mct_record(   opj_j2k_v2_t *p_j2k,
1171                                                                                 opj_mct_data_t * p_mct_record,
1172                                                                                 struct opj_stream_private *p_stream,
1173                                                                                 struct opj_event_mgr * p_manager );
1174
1175 /**
1176  * Reads a MCT marker (Multiple Component Transform)
1177  *
1178  * @param       p_header_data   the data contained in the MCT box.
1179  * @param       p_j2k                   the jpeg2000 codec.
1180  * @param       p_header_size   the size of the data contained in the MCT marker.
1181  * @param       p_manager               the user event manager.
1182 */
1183 static opj_bool j2k_read_mct (  opj_j2k_v2_t *p_j2k,
1184                                                                 OPJ_BYTE * p_header_data,
1185                                                                 OPJ_UINT32 p_header_size,
1186                                                                 struct opj_event_mgr * p_manager );
1187
1188 /**
1189  * Writes the MCC marker (Multiple Component Collection)
1190  *
1191  * @param       p_stream                the stream to write data to.
1192  * @param       p_j2k                   J2K codec.
1193  * @param       p_manager               the user event manager.
1194 */
1195 static opj_bool j2k_write_mcc_record(   opj_j2k_v2_t *p_j2k,
1196                                                                                 struct opj_simple_mcc_decorrelation_data * p_mcc_record,
1197                                                                                 struct opj_stream_private *p_stream,
1198                                                                                 struct opj_event_mgr * p_manager );
1199
1200
1201 /**
1202  * Reads a MCC marker (Multiple Component Collection)
1203  *
1204  * @param       p_header_data   the data contained in the MCC box.
1205  * @param       p_j2k                   the jpeg2000 codec.
1206  * @param       p_header_size   the size of the data contained in the MCC marker.
1207  * @param       p_manager               the user event manager.
1208 */
1209 static opj_bool j2k_read_mcc (  opj_j2k_v2_t *p_j2k,
1210                                                                 OPJ_BYTE * p_header_data,
1211                                                                 OPJ_UINT32 p_header_size,
1212                                                                 struct opj_event_mgr * p_manager );
1213
1214 /**
1215  * Writes the MCO marker (Multiple component transformation ordering)
1216  *
1217  * @param       p_stream                                the stream to write data to.
1218  * @param       p_j2k                           J2K codec.
1219  * @param       p_manager               the user event manager.
1220 */
1221 static opj_bool j2k_write_mco(  opj_j2k_v2_t *p_j2k,
1222                                                                 struct opj_stream_private *p_stream,
1223                                                                 struct opj_event_mgr * p_manager );
1224
1225 /**
1226  * Reads a MCO marker (Multiple Component Transform Ordering)
1227  *
1228  * @param       p_header_data   the data contained in the MCO box.
1229  * @param       p_j2k                   the jpeg2000 codec.
1230  * @param       p_header_size   the size of the data contained in the MCO marker.
1231  * @param       p_manager               the user event manager.
1232 */
1233 static opj_bool j2k_read_mco (  opj_j2k_v2_t *p_j2k,
1234                                                                 OPJ_BYTE * p_header_data,
1235                                                                 OPJ_UINT32 p_header_size,
1236                                                                 struct opj_event_mgr * p_manager );
1237
1238 static opj_bool j2k_add_mct(opj_tcp_v2_t * p_tcp, opj_image_t * p_image, OPJ_UINT32 p_index);
1239
1240 static void  j2k_read_int16_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1241 static void  j2k_read_int32_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1242 static void  j2k_read_float32_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1243 static void  j2k_read_float64_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1244
1245 static void  j2k_read_int16_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1246 static void  j2k_read_int32_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1247 static void  j2k_read_float32_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1248 static void  j2k_read_float64_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1249
1250 static void  j2k_write_float_to_int16 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1251 static void  j2k_write_float_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1252 static void  j2k_write_float_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1253 static void  j2k_write_float_to_float64 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1254
1255
1256 /**
1257  * Ends the encoding, i.e. frees memory.
1258  *
1259  * @param       p_stream                the stream to write data to.
1260  * @param       p_j2k                   J2K codec.
1261  * @param       p_manager               the user event manager.
1262 */
1263 static opj_bool j2k_end_encoding(       opj_j2k_v2_t *p_j2k,
1264                                                                         struct opj_stream_private *p_stream,
1265                                                                         struct opj_event_mgr * p_manager );
1266
1267 /**
1268  * Writes the CBD marker (Component bit depth definition)
1269  *
1270  * @param       p_stream                                the stream to write data to.
1271  * @param       p_j2k                           J2K codec.
1272  * @param       p_manager               the user event manager.
1273 */
1274 static opj_bool j2k_write_cbd(  opj_j2k_v2_t *p_j2k,
1275                                                                 struct opj_stream_private *p_stream,
1276                                                                 struct opj_event_mgr * p_manager );
1277
1278 /**
1279  * Reads a CBD marker (Component bit depth definition)
1280  * @param       p_header_data   the data contained in the CBD box.
1281  * @param       p_j2k                   the jpeg2000 codec.
1282  * @param       p_header_size   the size of the data contained in the CBD marker.
1283  * @param       p_manager               the user event manager.
1284 */
1285 static opj_bool j2k_read_cbd (  opj_j2k_v2_t *p_j2k,
1286                                                         OPJ_BYTE * p_header_data,
1287                                                         OPJ_UINT32 p_header_size,
1288                                                         struct opj_event_mgr * p_manager);
1289
1290 /**
1291  * Writes the image components.
1292  *
1293  * @param       p_stream                the stream to write data to.
1294  * @param       p_j2k                   J2K codec.
1295  * @param       p_manager               the user event manager.
1296 */
1297 static opj_bool j2k_write_image_components(     opj_j2k_v2_t *p_j2k,
1298                                                                                         struct opj_stream_private *p_stream,
1299                                                                                         struct opj_event_mgr * p_manager );
1300
1301 /**
1302  * Writes regions of interests.
1303  *
1304  * @param       p_stream                the stream to write data to.
1305  * @param       p_j2k                   J2K codec.
1306  * @param       p_manager               the user event manager.
1307 */
1308 static opj_bool j2k_write_regions(      opj_j2k_v2_t *p_j2k,
1309                                                                         struct opj_stream_private *p_stream,
1310                                                                         struct opj_event_mgr * p_manager );
1311
1312 /**
1313  * Writes EPC ????
1314  *
1315  * @param       p_stream                the stream to write data to.
1316  * @param       p_j2k                   J2K codec.
1317  * @param       p_manager               the user event manager.
1318 */
1319 static opj_bool j2k_write_epc(  opj_j2k_v2_t *p_j2k,
1320                                                                 struct opj_stream_private *p_stream,
1321                                                                 struct opj_event_mgr * p_manager );
1322
1323 /**
1324  * Checks the progression order changes values. Tells of the poc given as input are valid.
1325  * A nice message is outputted at errors.
1326  *
1327  * @param       p_pocs                          the progression order changes.
1328  * @param       p_nb_pocs                       the number of progression order changes.
1329  * @param       p_nb_resolutions        the number of resolutions.
1330  * @param       numcomps                        the number of components
1331  * @param       numlayers                       the number of layers.
1332  *
1333  * @return      true if the pocs are valid.
1334  */
1335 static opj_bool j2k_check_poc_val(      const opj_poc_t *p_pocs,
1336                                                                         OPJ_UINT32 p_nb_pocs,
1337                                                                         OPJ_UINT32 p_nb_resolutions,
1338                                                                         OPJ_UINT32 numcomps,
1339                                                                         OPJ_UINT32 numlayers,
1340                                                                         opj_event_mgr_t * p_manager);
1341
1342 /**
1343  * Gets the number of tile parts used for the given change of progression (if any) and the given tile.
1344  *
1345  * @param               cp                      the coding parameters.
1346  * @param               pino            the offset of the given poc (i.e. its position in the coding parameter).
1347  * @param               tileno          the given tile.
1348  *
1349  * @return              the number of tile parts.
1350  */
1351 static OPJ_UINT32 j2k_get_num_tp_v2( opj_cp_v2_t *cp, OPJ_UINT32 pino, OPJ_UINT32 tileno);
1352
1353 /**     mem allocation for TLM marker*/
1354 static int j2k_calculate_tp(opj_cp_t *cp,int img_numcomp,opj_image_t *image,opj_j2k_t *j2k );
1355
1356 /**
1357  * Calculates the total number of tile parts needed by the encoder to
1358  * encode such an image. If not enough memory is available, then the function return false.
1359  *
1360  * @param       p_nb_tiles      pointer that will hold the number of tile parts.
1361  * @param       cp                      the coding parameters for the image.
1362  * @param       image           the image to encode.
1363  * @param       p_j2k                   the p_j2k encoder.
1364  * @param       p_manager       the user event manager.
1365  *
1366  * @return true if the function was successful, false else.
1367  */
1368 static opj_bool j2k_calculate_tp_v2(opj_j2k_v2_t *p_j2k,
1369                                                                         opj_cp_v2_t *cp,
1370                                                                         OPJ_UINT32 * p_nb_tiles,
1371                                                                         opj_image_t *image,
1372                                                                         opj_event_mgr_t * p_manager);
1373
1374 static void j2k_dump_MH_info(opj_j2k_v2_t* p_j2k, FILE* out_stream);
1375
1376 static void j2k_dump_MH_index(opj_j2k_v2_t* p_j2k, FILE* out_stream);
1377
1378 static opj_codestream_index_t* j2k_create_cstr_index(void);
1379
1380 /*@}*/
1381
1382 /*@}*/
1383
1384 /* ----------------------------------------------------------------------- */
1385 typedef struct j2k_prog_order{
1386         OPJ_PROG_ORDER enum_prog;
1387         char str_prog[5];
1388 }j2k_prog_order_t;
1389
1390 j2k_prog_order_t j2k_prog_order_list[] = {
1391         {CPRL, "CPRL"},
1392         {LRCP, "LRCP"},
1393         {PCRL, "PCRL"},
1394         {RLCP, "RLCP"},
1395         {RPCL, "RPCL"},
1396         {(OPJ_PROG_ORDER)-1, ""}
1397 };
1398
1399
1400
1401 /**
1402  * FIXME DOC
1403  */
1404 const OPJ_UINT32 MCT_ELEMENT_SIZE [] =
1405 {
1406         2,
1407         4,
1408         4,
1409         8
1410 };
1411
1412 typedef void (* j2k_mct_function) (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1413
1414 const j2k_mct_function j2k_mct_read_functions_to_float [] =
1415 {
1416         j2k_read_int16_to_float,
1417         j2k_read_int32_to_float,
1418         j2k_read_float32_to_float,
1419         j2k_read_float64_to_float
1420 };
1421
1422 const j2k_mct_function j2k_mct_read_functions_to_int32 [] =
1423 {
1424         j2k_read_int16_to_int32,
1425         j2k_read_int32_to_int32,
1426         j2k_read_float32_to_int32,
1427         j2k_read_float64_to_int32
1428 };
1429
1430 const j2k_mct_function j2k_mct_write_functions_from_float [] =
1431 {
1432         j2k_write_float_to_int16,
1433         j2k_write_float_to_int32,
1434         j2k_write_float_to_float,
1435         j2k_write_float_to_float64
1436 };
1437
1438 typedef struct opj_dec_memory_marker_handler
1439 {
1440         /** marker value */
1441         OPJ_UINT32 id;
1442         /** value of the state when the marker can appear */
1443         OPJ_UINT32 states;
1444         /** action linked to the marker */
1445         opj_bool (*handler) (
1446                                         opj_j2k_v2_t *p_j2k,
1447                                         OPJ_BYTE * p_header_data,
1448                                         OPJ_UINT32 p_header_size,
1449                                         struct opj_event_mgr * p_manager
1450                                                 );
1451 }
1452 opj_dec_memory_marker_handler_t;
1453
1454 const opj_dec_memory_marker_handler_t j2k_memory_marker_handler_tab [] =
1455 {
1456 #ifdef TODO_MS
1457   {J2K_MS_SOT, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPHSOT, j2k_read_sot},
1458   {J2K_MS_COD, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_cod},
1459   {J2K_MS_COC, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_coc},
1460   {J2K_MS_RGN, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_rgn},
1461   {J2K_MS_QCD, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_qcd},
1462   {J2K_MS_QCC, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_qcc},
1463   {J2K_MS_POC, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_poc},
1464   {J2K_MS_SIZ, J2K_DEC_STATE_MHSIZ , j2k_read_siz},
1465   {J2K_MS_TLM, J2K_DEC_STATE_MH, j2k_read_tlm},
1466   {J2K_MS_PLM, J2K_DEC_STATE_MH, j2k_read_plm},
1467   {J2K_MS_PLT, J2K_DEC_STATE_TPH, j2k_read_plt},
1468   {J2K_MS_PPM, J2K_DEC_STATE_MH, j2k_read_ppm},
1469   {J2K_MS_PPT, J2K_DEC_STATE_TPH, j2k_read_ppt},
1470   {J2K_MS_SOP, 0, 0},
1471   {J2K_MS_CRG, J2K_DEC_STATE_MH, j2k_read_crg},
1472   {J2K_MS_COM, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_com},
1473   {J2K_MS_MCT, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_mct},
1474   {J2K_MS_CBD, J2K_DEC_STATE_MH , j2k_read_cbd},
1475   {J2K_MS_MCC, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_mcc},
1476   {J2K_MS_MCO, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_mco},
1477 #endif
1478   {J2K_MS_SOT, J2K_STATE_MH | J2K_STATE_TPHSOT, j2k_read_sot_v2},
1479   {J2K_MS_COD, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_cod_v2},
1480   {J2K_MS_COC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_coc_v2},
1481   {J2K_MS_RGN, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_rgn_v2},
1482   {J2K_MS_QCD, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_qcd_v2},
1483   {J2K_MS_QCC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_qcc_v2},
1484   {J2K_MS_POC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_poc_v2},
1485   {J2K_MS_SIZ, J2K_STATE_MHSIZ , j2k_read_siz_v2},
1486   {J2K_MS_TLM, J2K_STATE_MH, j2k_read_tlm_v2},
1487   {J2K_MS_PLM, J2K_STATE_MH, j2k_read_plm_v2},
1488   {J2K_MS_PLT, J2K_STATE_TPH, j2k_read_plt_v2},
1489   {J2K_MS_PPM, J2K_STATE_MH, j2k_read_ppm_v3},
1490   {J2K_MS_PPT, J2K_STATE_TPH, j2k_read_ppt_v2},
1491   {J2K_MS_SOP, 0, 0},
1492   {J2K_MS_CRG, J2K_STATE_MH, j2k_read_crg_v2},
1493   {J2K_MS_COM, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_com_v2},
1494   {J2K_MS_MCT, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_mct},
1495   {J2K_MS_CBD, J2K_STATE_MH , j2k_read_cbd},
1496   {J2K_MS_MCC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_mcc},
1497   {J2K_MS_MCO, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_mco},
1498 #ifdef USE_JPWL
1499 #ifdef TODO_MS /* FIXME */
1500   {J2K_MS_EPC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_epc},
1501   {J2K_MS_EPB, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_epb},
1502   {J2K_MS_ESD, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_esd},
1503   {J2K_MS_RED, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_red},
1504 #endif
1505 #endif /* USE_JPWL */
1506 #ifdef USE_JPSEC
1507   {J2K_MS_SEC, J2K_DEC_STATE_MH, j2k_read_sec},
1508   {J2K_MS_INSEC, 0, j2k_read_insec}
1509 #endif /* USE_JPSEC */
1510   {J2K_MS_UNK, J2K_STATE_MH | J2K_STATE_TPH, 0}/*j2k_read_unk_v2}*/
1511 };
1512
1513
1514
1515 void  j2k_read_int16_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_UINT32 l_temp;
1521
1522         for (i=0;i<p_nb_elem;++i) {
1523                 opj_read_bytes(l_src_data,&l_temp,2);
1524
1525                 l_src_data+=sizeof(OPJ_INT16);
1526
1527                 *(l_dest_data++) = (OPJ_FLOAT32) l_temp;
1528         }
1529 }
1530
1531 void  j2k_read_int32_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_UINT32 l_temp;
1537
1538         for (i=0;i<p_nb_elem;++i) {
1539                 opj_read_bytes(l_src_data,&l_temp,4);
1540
1541                 l_src_data+=sizeof(OPJ_INT32);
1542
1543                 *(l_dest_data++) = (OPJ_FLOAT32) l_temp;
1544         }
1545 }
1546
1547 void  j2k_read_float32_to_float (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_FLOAT32 * l_dest_data = (OPJ_FLOAT32 *) p_dest_data;
1551         OPJ_UINT32 i;
1552         OPJ_FLOAT32 l_temp;
1553
1554         for (i=0;i<p_nb_elem;++i) {
1555                 opj_read_float(l_src_data,&l_temp);
1556
1557                 l_src_data+=sizeof(OPJ_FLOAT32);
1558
1559                 *(l_dest_data++) = l_temp;
1560         }
1561 }
1562
1563 void  j2k_read_float64_to_float (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_FLOAT32 * l_dest_data = (OPJ_FLOAT32 *) p_dest_data;
1567         OPJ_UINT32 i;
1568         OPJ_FLOAT64 l_temp;
1569
1570         for (i=0;i<p_nb_elem;++i) {
1571                 opj_read_double(l_src_data,&l_temp);
1572
1573                 l_src_data+=sizeof(OPJ_FLOAT64);
1574
1575                 *(l_dest_data++) = (OPJ_FLOAT32) l_temp;
1576         }
1577 }
1578
1579 void  j2k_read_int16_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_UINT32 l_temp;
1585
1586         for (i=0;i<p_nb_elem;++i) {
1587                 opj_read_bytes(l_src_data,&l_temp,2);
1588
1589                 l_src_data+=sizeof(OPJ_INT16);
1590
1591                 *(l_dest_data++) = (OPJ_INT32) l_temp;
1592         }
1593 }
1594
1595 void  j2k_read_int32_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_UINT32 l_temp;
1601
1602         for (i=0;i<p_nb_elem;++i) {
1603                 opj_read_bytes(l_src_data,&l_temp,4);
1604
1605                 l_src_data+=sizeof(OPJ_INT32);
1606
1607                 *(l_dest_data++) = (OPJ_INT32) l_temp;
1608         }
1609 }
1610
1611 void  j2k_read_float32_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1612 {
1613         OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
1614         OPJ_INT32 * l_dest_data = (OPJ_INT32 *) p_dest_data;
1615         OPJ_UINT32 i;
1616         OPJ_FLOAT32 l_temp;
1617
1618         for (i=0;i<p_nb_elem;++i) {
1619                 opj_read_float(l_src_data,&l_temp);
1620
1621                 l_src_data+=sizeof(OPJ_FLOAT32);
1622
1623                 *(l_dest_data++) = (OPJ_INT32) l_temp;
1624         }
1625 }
1626
1627 void  j2k_read_float64_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1628 {
1629         OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
1630         OPJ_INT32 * l_dest_data = (OPJ_INT32 *) p_dest_data;
1631         OPJ_UINT32 i;
1632         OPJ_FLOAT64 l_temp;
1633
1634         for (i=0;i<p_nb_elem;++i) {
1635                 opj_read_double(l_src_data,&l_temp);
1636
1637                 l_src_data+=sizeof(OPJ_FLOAT64);
1638
1639                 *(l_dest_data++) = (OPJ_INT32) l_temp;
1640         }
1641 }
1642
1643 void  j2k_write_float_to_int16 (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_UINT32 l_temp;
1649
1650         for (i=0;i<p_nb_elem;++i) {
1651                 l_temp = (OPJ_UINT32) *(l_src_data++);
1652
1653                 opj_write_bytes(l_dest_data,l_temp,sizeof(OPJ_INT16));
1654
1655                 l_dest_data+=sizeof(OPJ_INT16);
1656         }
1657 }
1658
1659 void  j2k_write_float_to_int32 (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_UINT32 l_temp;
1665
1666         for (i=0;i<p_nb_elem;++i) {
1667                 l_temp = (OPJ_UINT32) *(l_src_data++);
1668
1669                 opj_write_bytes(l_dest_data,l_temp,sizeof(OPJ_INT32));
1670
1671                 l_dest_data+=sizeof(OPJ_INT32);
1672         }
1673 }
1674
1675 void  j2k_write_float_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1676 {
1677         OPJ_BYTE * l_dest_data = (OPJ_BYTE *) p_dest_data;
1678         OPJ_FLOAT32 * l_src_data = (OPJ_FLOAT32 *) p_src_data;
1679         OPJ_UINT32 i;
1680         OPJ_FLOAT32 l_temp;
1681
1682         for (i=0;i<p_nb_elem;++i) {
1683                 l_temp = (OPJ_FLOAT32) *(l_src_data++);
1684
1685                 opj_write_float(l_dest_data,l_temp);
1686
1687                 l_dest_data+=sizeof(OPJ_FLOAT32);
1688         }
1689 }
1690
1691 void  j2k_write_float_to_float64 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1692 {
1693         OPJ_BYTE * l_dest_data = (OPJ_BYTE *) p_dest_data;
1694         OPJ_FLOAT32 * l_src_data = (OPJ_FLOAT32 *) p_src_data;
1695         OPJ_UINT32 i;
1696         OPJ_FLOAT64 l_temp;
1697
1698         for (i=0;i<p_nb_elem;++i) {
1699                 l_temp = (OPJ_FLOAT64) *(l_src_data++);
1700
1701                 opj_write_double(l_dest_data,l_temp);
1702
1703                 l_dest_data+=sizeof(OPJ_FLOAT64);
1704         }
1705 }
1706
1707
1708 /**
1709  * Converts an enum type progression order to string type.
1710  *
1711  * @param prg_order             the progression order to get.
1712  *
1713  * @return      the string representation of the given progression order.
1714  */
1715 char *j2k_convert_progression_order(OPJ_PROG_ORDER prg_order){
1716         j2k_prog_order_t *po;
1717         for(po = j2k_prog_order_list; po->enum_prog != -1; po++ ){
1718                 if(po->enum_prog == prg_order){
1719                         return po->str_prog;
1720                 }
1721         }
1722         return po->str_prog;
1723 }
1724
1725 /**
1726  * Checks the progression order changes values. Tells if the poc given as input are valid.
1727  *
1728  * @param       p_pocs                          the progression order changes.
1729  * @param       p_nb_pocs                       the number of progression order changes.
1730  * @param       p_nb_resolutions        the number of resolutions.
1731  * @param       numcomps                        the number of components
1732  * @param       numlayers                       the number of layers.
1733  * @param       p_manager                       the user event manager.
1734  *
1735  * @return      true if the pocs are valid.
1736  */
1737 opj_bool j2k_check_poc_val(     const opj_poc_t *p_pocs,
1738                                                         OPJ_UINT32 p_nb_pocs,
1739                                                         OPJ_UINT32 p_nb_resolutions,
1740                                                         OPJ_UINT32 p_num_comps,
1741                                                         OPJ_UINT32 p_num_layers,
1742                                                         opj_event_mgr_t * p_manager)
1743 {
1744         OPJ_UINT32* packet_array;
1745         OPJ_UINT32 index , resno, compno, layno;
1746         OPJ_UINT32 i;
1747         OPJ_UINT32 step_c = 1;
1748         OPJ_UINT32 step_r = p_num_comps * step_c;
1749         OPJ_UINT32 step_l = p_nb_resolutions * step_r;
1750         opj_bool loss = OPJ_FALSE;
1751         OPJ_UINT32 layno0 = 0;
1752
1753         packet_array = (OPJ_UINT32*) opj_calloc(step_l * p_num_layers, sizeof(OPJ_UINT32));
1754         if (packet_array == 00) {
1755                 opj_event_msg_v2(p_manager , EVT_ERROR, "Not enough memory for checking the poc values.\n");
1756                 return OPJ_FALSE;
1757         }
1758         memset(packet_array,0,step_l * p_num_layers* sizeof(OPJ_UINT32));
1759
1760         if (p_nb_pocs == 0) {
1761                 return OPJ_TRUE;
1762         }
1763
1764         index = step_r * p_pocs->resno0;
1765         // take each resolution for each poc
1766         for (resno = p_pocs->resno0 ; resno < p_pocs->resno1 ; ++resno)
1767         {
1768                 OPJ_UINT32 res_index = index + p_pocs->compno0 * step_c;
1769
1770                 // take each comp of each resolution for each poc
1771                 for (compno = p_pocs->compno0 ; compno < p_pocs->compno1 ; ++compno) {
1772                         OPJ_UINT32 comp_index = res_index + layno0 * step_l;
1773
1774                         // and finally take each layer of each res of ...
1775                         for (layno = layno0; layno < p_pocs->layno1 ; ++layno) {
1776                                 //index = step_r * resno + step_c * compno + step_l * layno;
1777                                 packet_array[comp_index] = 1;
1778                                 comp_index += step_l;
1779                         }
1780
1781                         res_index += step_c;
1782                 }
1783
1784                 index += step_r;
1785         }
1786         ++p_pocs;
1787
1788         // iterate through all the pocs
1789         for (i = 1; i < p_nb_pocs ; ++i) {
1790                 OPJ_UINT32 l_last_layno1 = (p_pocs-1)->layno1 ;
1791
1792                 layno0 = (p_pocs->layno1 > l_last_layno1)? l_last_layno1 : 0;
1793                 index = step_r * p_pocs->resno0;
1794
1795                 // take each resolution for each poc
1796                 for (resno = p_pocs->resno0 ; resno < p_pocs->resno1 ; ++resno) {
1797                         OPJ_UINT32 res_index = index + p_pocs->compno0 * step_c;
1798
1799                         // take each comp of each resolution for each poc
1800                         for (compno = p_pocs->compno0 ; compno < p_pocs->compno1 ; ++compno) {
1801                                 OPJ_UINT32 comp_index = res_index + layno0 * step_l;
1802
1803                                 // and finally take each layer of each res of ...
1804                                 for (layno = layno0; layno < p_pocs->layno1 ; ++layno) {
1805                                         //index = step_r * resno + step_c * compno + step_l * layno;
1806                                         packet_array[comp_index] = 1;
1807                                         comp_index += step_l;
1808                                 }
1809
1810                                 res_index += step_c;
1811                         }
1812
1813                         index += step_r;
1814                 }
1815
1816                 ++p_pocs;
1817         }
1818
1819         index = 0;
1820         for (layno = 0; layno < p_num_layers ; ++layno) {
1821                 for (resno = 0; resno < p_nb_resolutions; ++resno) {
1822                         for (compno = 0; compno < p_num_comps; ++compno) {
1823                                 loss |= (packet_array[index]!=1);
1824                                 //index = step_r * resno + step_c * compno + step_l * layno;
1825                                 index += step_c;
1826                         }
1827                 }
1828         }
1829
1830         if (loss) {
1831                 opj_event_msg_v2(p_manager , EVT_ERROR, "Missing packets possible loss of data\n");
1832         }
1833
1834         opj_free(packet_array);
1835
1836         return !loss;
1837 }
1838
1839 /* ----------------------------------------------------------------------- */
1840 static int j2k_get_num_tp(opj_cp_t *cp,int pino,int tileno){
1841         char *prog;
1842         int i;
1843         int tpnum=1,tpend=0;
1844         opj_tcp_t *tcp = &cp->tcps[tileno];
1845         prog = j2k_convert_progression_order(tcp->prg);
1846         
1847         if(cp->tp_on == 1){
1848                 for(i=0;i<4;i++){
1849                         if(tpend!=1){
1850                                 if( cp->tp_flag == prog[i] ){
1851                                         tpend=1;cp->tp_pos=i;
1852                                 }
1853                                 switch(prog[i]){
1854                                 case 'C':
1855                                         tpnum= tpnum * tcp->pocs[pino].compE;
1856                                         break;
1857                                 case 'R':
1858                                         tpnum= tpnum * tcp->pocs[pino].resE;
1859                                         break;
1860                                 case 'P':
1861                                         tpnum= tpnum * tcp->pocs[pino].prcE;
1862                                         break;
1863                                 case 'L':
1864                                         tpnum= tpnum * tcp->pocs[pino].layE;
1865                                         break;
1866                                 }
1867                         }
1868                 }
1869         }else{
1870                 tpnum=1;
1871         }
1872         return tpnum;
1873 }
1874
1875 /**
1876  * Gets the number of tile parts used for the given change of progression (if any) and the given tile.
1877  *
1878  * @param               cp                      the coding parameters.
1879  * @param               pino            the offset of the given poc (i.e. its position in the coding parameter).
1880  * @param               tileno          the given tile.
1881  *
1882  * @return              the number of tile parts.
1883  */
1884 OPJ_UINT32 j2k_get_num_tp_v2(opj_cp_v2_t *cp, OPJ_UINT32 pino, OPJ_UINT32 tileno)
1885 {
1886         const OPJ_CHAR *prog = 00;
1887         OPJ_UINT32 i;
1888         OPJ_UINT32 tpnum = 1;
1889         opj_tcp_v2_t *tcp = 00;
1890         opj_poc_t * l_current_poc = 00;
1891
1892         /*  preconditions */
1893         assert(tileno < (cp->tw * cp->th));
1894         assert(pino < (cp->tcps[tileno].numpocs + 1));
1895
1896         /* get the given tile coding parameter */
1897         tcp = &cp->tcps[tileno];
1898         assert(tcp != 00);
1899
1900         l_current_poc = &(tcp->pocs[pino]);
1901         assert(l_current_poc != 0);
1902
1903         /* get the progression order as a character string */
1904         prog = j2k_convert_progression_order(tcp->prg);
1905         assert(strlen(prog) > 0);
1906
1907         if (cp->m_specific_param.m_enc.m_tp_on == 1) {
1908                 for (i=0;i<4;++i) {
1909                         switch (prog[i])
1910                         {
1911                                 /* component wise */
1912                                 case 'C':
1913                                         tpnum *= l_current_poc->compE;
1914                                         break;
1915                                 /* resolution wise */
1916                                 case 'R':
1917                                         tpnum *= l_current_poc->resE;
1918                                         break;
1919                                 /* precinct wise */
1920                                 case 'P':
1921                                         tpnum *= l_current_poc->prcE;
1922                                         break;
1923                                 /* layer wise */
1924                                 case 'L':
1925                                         tpnum *= l_current_poc->layE;
1926                                         break;
1927                         }
1928                         /* whould we split here ? */
1929                         if ( cp->m_specific_param.m_enc.m_tp_flag == prog[i] ) {
1930                                 cp->m_specific_param.m_enc.m_tp_pos=i;
1931                                 break;
1932                         }
1933                 }
1934         }
1935         else {
1936                 tpnum=1;
1937         }
1938
1939         return tpnum;
1940 }
1941
1942 /**     mem allocation for TLM marker*/
1943 int j2k_calculate_tp(opj_cp_t *cp,int img_numcomp,opj_image_t *image,opj_j2k_t *j2k ){
1944         int pino,tileno,totnum_tp=0;
1945
1946         OPJ_ARG_NOT_USED(img_numcomp);
1947
1948         j2k->cur_totnum_tp = (int *) opj_malloc(cp->tw * cp->th * sizeof(int));
1949         for (tileno = 0; tileno < cp->tw * cp->th; tileno++) {
1950                 int cur_totnum_tp = 0;
1951                 opj_tcp_t *tcp = &cp->tcps[tileno];
1952                 for(pino = 0; pino <= tcp->numpocs; pino++) {
1953                         int tp_num=0;
1954                         opj_pi_iterator_t *pi = pi_initialise_encode(image, cp, tileno,FINAL_PASS);
1955                         if(!pi) { return -1;}
1956                         tp_num = j2k_get_num_tp(cp,pino,tileno);
1957                         totnum_tp = totnum_tp + tp_num;
1958                         cur_totnum_tp = cur_totnum_tp + tp_num;
1959                         pi_destroy(pi, cp, tileno);
1960                 }
1961                 j2k->cur_totnum_tp[tileno] = cur_totnum_tp;
1962                 /* INDEX >> */
1963                 if (j2k->cstr_info) {
1964                         j2k->cstr_info->tile[tileno].num_tps = cur_totnum_tp;
1965                         j2k->cstr_info->tile[tileno].tp = (opj_tp_info_t *) opj_malloc(cur_totnum_tp * sizeof(opj_tp_info_t));
1966                 }
1967                 /* << INDEX */
1968         }
1969         return totnum_tp;
1970 }
1971
1972 /**
1973  * Calculates the total number of tile parts needed by the encoder to
1974  * encode such an image. If not enough memory is available, then the function return false.
1975  *
1976  * @param       p_nb_tiles      pointer that will hold the number of tile parts.
1977  * @param       cp                      the coding parameters for the image.
1978  * @param       image           the image to encode.
1979  * @param       p_j2k                   the p_j2k encoder.
1980  * @param       p_manager       the user event manager.
1981  *
1982  * @return true if the function was successful, false else.
1983  */
1984 opj_bool j2k_calculate_tp_v2( opj_j2k_v2_t *p_j2k,
1985                                                           opj_cp_v2_t *cp,
1986                                                           OPJ_UINT32 * p_nb_tiles,
1987                                                           opj_image_t *image,
1988                                                           opj_event_mgr_t * p_manager)
1989 {
1990         OPJ_UINT32 pino,tileno;
1991         OPJ_UINT32 l_nb_tiles;
1992         opj_tcp_v2_t *tcp;
1993
1994         /* preconditions */
1995         assert(p_nb_tiles != 00);
1996         assert(cp != 00);
1997         assert(image != 00);
1998         assert(p_j2k != 00);
1999         assert(p_manager != 00);
2000
2001         l_nb_tiles = cp->tw * cp->th;
2002         * p_nb_tiles = 0;
2003         tcp = cp->tcps;
2004
2005         /* INDEX >> */
2006         /* TODO mergeV2: check this part which use cstr_info */
2007         /*if (p_j2k->cstr_info) {
2008                 opj_tile_info_t * l_info_tile_ptr = p_j2k->cstr_info->tile;
2009
2010                 for (tileno = 0; tileno < l_nb_tiles; ++tileno) {
2011                         OPJ_UINT32 cur_totnum_tp = 0;
2012
2013                         pi_update_encoding_parameters(image,cp,tileno);
2014
2015                         for (pino = 0; pino <= tcp->numpocs; ++pino)
2016                         {
2017                                 OPJ_UINT32 tp_num = j2k_get_num_tp_v2(cp,pino,tileno);
2018
2019                                 *p_nb_tiles = *p_nb_tiles + tp_num;
2020
2021                                 cur_totnum_tp += tp_num;
2022                         }
2023
2024                         tcp->m_nb_tile_parts = cur_totnum_tp;
2025
2026                         l_info_tile_ptr->tp = (opj_tp_info_t *) opj_malloc(cur_totnum_tp * sizeof(opj_tp_info_t));
2027                         if (l_info_tile_ptr->tp == 00) {
2028                                 return OPJ_FALSE;
2029                         }
2030
2031                         memset(l_info_tile_ptr->tp,0,cur_totnum_tp * sizeof(opj_tp_info_t));
2032
2033                         l_info_tile_ptr->num_tps = cur_totnum_tp;
2034
2035                         ++l_info_tile_ptr;
2036                         ++tcp;
2037                 }
2038         }
2039         else */{
2040                 for (tileno = 0; tileno < l_nb_tiles; ++tileno) {
2041                         OPJ_UINT32 cur_totnum_tp = 0;
2042
2043                         pi_update_encoding_parameters(image,cp,tileno);
2044
2045                         for (pino = 0; pino <= tcp->numpocs; ++pino) {
2046                                 OPJ_UINT32 tp_num = j2k_get_num_tp_v2(cp,pino,tileno);
2047
2048                                 *p_nb_tiles = *p_nb_tiles + tp_num;
2049
2050                                 cur_totnum_tp += tp_num;
2051                         }
2052                         tcp->m_nb_tile_parts = cur_totnum_tp;
2053
2054                         ++tcp;
2055                 }
2056         }
2057
2058         return OPJ_TRUE;
2059 }
2060
2061 static void j2k_write_soc(opj_j2k_t *j2k) {
2062         opj_cio_t *cio = j2k->cio;
2063         cio_write(cio, J2K_MS_SOC, 2);
2064
2065         if(j2k->cstr_info)
2066           j2k_add_mhmarker(j2k->cstr_info, J2K_MS_SOC, cio_tell(cio), 0);
2067
2068 /* UniPG>> */
2069 #ifdef USE_JPWL
2070
2071         /* update markers struct */
2072         j2k_add_marker(j2k->cstr_info, J2K_MS_SOC, cio_tell(cio) - 2, 2);
2073 #endif /* USE_JPWL */
2074 /* <<UniPG */
2075 }
2076
2077 /**
2078  * Writes the SOC marker (Start Of Codestream)
2079  *
2080  * @param       p_stream                        the stream to write data to.
2081  * @param       p_j2k                   J2K codec.
2082  * @param       p_manager       the user event manager.
2083 */
2084 opj_bool j2k_write_soc_v2(      opj_j2k_v2_t *p_j2k,
2085                                                         struct opj_stream_private *p_stream,
2086                                                         struct opj_event_mgr * p_manager )
2087 {
2088         /* 2 bytes will be written */
2089         OPJ_BYTE * l_start_stream = 00;
2090
2091         /* preconditions */
2092         assert(p_stream != 00);
2093         assert(p_j2k != 00);
2094         assert(p_manager != 00);
2095
2096         l_start_stream = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
2097
2098         /* write SOC identifier */
2099         opj_write_bytes(l_start_stream,J2K_MS_SOC,2);
2100
2101         if (opj_stream_write_data(p_stream,l_start_stream,2,p_manager) != 2) {
2102                 return OPJ_FALSE;
2103         }
2104
2105 /* UniPG>> */
2106 #ifdef USE_JPWL
2107         /* update markers struct */
2108 /*
2109         j2k_add_marker(p_j2k->cstr_info, J2K_MS_SOC, p_stream_tell(p_stream) - 2, 2);
2110 */
2111   assert( 0 && "TODO" );
2112 #endif /* USE_JPWL */
2113 /* <<UniPG */
2114
2115         return OPJ_TRUE;
2116 }
2117
2118 static void j2k_read_soc(opj_j2k_t *j2k) {      
2119         j2k->state = J2K_STATE_MHSIZ;
2120         /* Index */
2121         if (j2k->cstr_info) {
2122                 j2k->cstr_info->main_head_start = cio_tell(j2k->cio) - 2;
2123                 j2k->cstr_info->codestream_size = cio_numbytesleft(j2k->cio) + 2 - j2k->cstr_info->main_head_start;
2124         }
2125 }
2126
2127 /**
2128  * Reads a SOC marker (Start of Codestream)
2129  * @param       p_header_data   the data contained in the SOC box.
2130  * @param       jp2                             the jpeg2000 file codec.
2131  * @param       p_header_size   the size of the data contained in the SOC marker.
2132  * @param       p_manager               the user event manager.
2133 */
2134 static opj_bool j2k_read_soc_v2(        opj_j2k_v2_t *p_j2k,
2135                                                                         struct opj_stream_private *p_stream,
2136                                                                         struct opj_event_mgr * p_manager )
2137 {
2138         OPJ_BYTE l_data [2];
2139         OPJ_UINT32 l_marker;
2140
2141         /* preconditions */
2142         assert(p_j2k != 00);
2143         assert(p_manager != 00);
2144         assert(p_stream != 00);
2145
2146         if (opj_stream_read_data(p_stream,l_data,2,p_manager) != 2) {
2147                 return OPJ_FALSE;
2148         }
2149
2150         opj_read_bytes(l_data,&l_marker,2);
2151         if (l_marker != J2K_MS_SOC) {
2152                 return OPJ_FALSE;
2153         }
2154
2155         /* Next marker should be a SIZ marker in the main header */
2156         p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_MHSIZ;
2157
2158         /* FIXME move it in a index structure included in p_j2k*/
2159         p_j2k->cstr_index->main_head_start = opj_stream_tell(p_stream) - 2;
2160
2161         opj_event_msg_v2(p_manager, EVT_INFO, "Start to read j2k main header (%d).\n", p_j2k->cstr_index->main_head_start);
2162
2163         /* Add the marker to the codestream index*/
2164         j2k_add_mhmarker_v2(p_j2k->cstr_index, J2K_MS_SOC, p_j2k->cstr_index->main_head_start, 2);
2165
2166         return OPJ_TRUE;
2167 }
2168
2169 static void j2k_write_siz(opj_j2k_t *j2k) {
2170         OPJ_UINT32 i;
2171         int lenp, len;
2172
2173         opj_cio_t *cio = j2k->cio;
2174         opj_image_t *image = j2k->image;
2175         opj_cp_t *cp = j2k->cp;
2176
2177         cio_write(cio, J2K_MS_SIZ, 2);  /* SIZ */
2178         lenp = cio_tell(cio);
2179         cio_skip(cio, 2);
2180         cio_write(cio, cp->rsiz, 2);                    /* Rsiz (capabilities) */
2181         cio_write(cio, image->x1, 4);   /* Xsiz */
2182         cio_write(cio, image->y1, 4);   /* Ysiz */
2183         cio_write(cio, image->x0, 4);   /* X0siz */
2184         cio_write(cio, image->y0, 4);   /* Y0siz */
2185         cio_write(cio, cp->tdx, 4);             /* XTsiz */
2186         cio_write(cio, cp->tdy, 4);             /* YTsiz */
2187         cio_write(cio, cp->tx0, 4);             /* XT0siz */
2188         cio_write(cio, cp->ty0, 4);             /* YT0siz */
2189         cio_write(cio, image->numcomps, 2);     /* Csiz */
2190         for (i = 0; i < image->numcomps; i++) {
2191                 cio_write(cio, image->comps[i].prec - 1 + (image->comps[i].sgnd << 7), 1);      /* Ssiz_i */
2192                 cio_write(cio, image->comps[i].dx, 1);  /* XRsiz_i */
2193                 cio_write(cio, image->comps[i].dy, 1);  /* YRsiz_i */
2194         }
2195         len = cio_tell(cio) - lenp;
2196         cio_seek(cio, lenp);
2197         cio_write(cio, len, 2);         /* Lsiz */
2198         cio_seek(cio, lenp + len);
2199         
2200         if(j2k->cstr_info)
2201           j2k_add_mhmarker(j2k->cstr_info, J2K_MS_SIZ, lenp, len);
2202 }
2203
2204 /**
2205  * Writes the SIZ marker (image and tile size)
2206  *
2207  * @param       p_stream                        the stream to write data to.
2208  * @param       p_j2k                   J2K codec.
2209  * @param       p_manager       the user event manager.
2210 */
2211 opj_bool j2k_write_siz_v2(      opj_j2k_v2_t *p_j2k,
2212                                                         struct opj_stream_private *p_stream,
2213                                                         struct opj_event_mgr * p_manager )
2214 {
2215         OPJ_UINT32 i;
2216         OPJ_UINT32 l_size_len;
2217         OPJ_BYTE * l_current_ptr;
2218         opj_image_t * l_image = 00;
2219         opj_cp_v2_t *cp = 00;
2220         opj_image_comp_t * l_img_comp = 00;
2221
2222         /* preconditions */
2223         assert(p_stream != 00);
2224         assert(p_j2k != 00);
2225         assert(p_manager != 00);
2226
2227         l_image = p_j2k->m_private_image;
2228         cp = &(p_j2k->m_cp);
2229         l_size_len = 40 + 3 * l_image->numcomps;
2230         l_img_comp = l_image->comps;
2231
2232         if (l_size_len > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
2233
2234                 p_j2k->m_specific_param.m_encoder.m_header_tile_data
2235                         = (OPJ_BYTE*)opj_realloc(
2236                                 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
2237                                 l_size_len);
2238                 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
2239                         return OPJ_FALSE;
2240                 }
2241
2242                 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_size_len;
2243         }
2244
2245         l_current_ptr = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
2246
2247         /* write SOC identifier */
2248         opj_write_bytes(l_current_ptr,J2K_MS_SIZ,2);    /* SIZ */
2249         l_current_ptr+=2;
2250
2251         opj_write_bytes(l_current_ptr,l_size_len-2,2); /* L_SIZ */
2252         l_current_ptr+=2;
2253
2254         opj_write_bytes(l_current_ptr, cp->rsiz, 2);    /* Rsiz (capabilities) */
2255         l_current_ptr+=2;
2256
2257         opj_write_bytes(l_current_ptr, l_image->x1, 4); /* Xsiz */
2258         l_current_ptr+=4;
2259
2260         opj_write_bytes(l_current_ptr, l_image->y1, 4); /* Ysiz */
2261         l_current_ptr+=4;
2262
2263         opj_write_bytes(l_current_ptr, l_image->x0, 4); /* X0siz */
2264         l_current_ptr+=4;
2265
2266         opj_write_bytes(l_current_ptr, l_image->y0, 4); /* Y0siz */
2267         l_current_ptr+=4;
2268
2269         opj_write_bytes(l_current_ptr, cp->tdx, 4);             /* XTsiz */
2270         l_current_ptr+=4;
2271
2272         opj_write_bytes(l_current_ptr, cp->tdy, 4);             /* YTsiz */
2273         l_current_ptr+=4;
2274
2275         opj_write_bytes(l_current_ptr, cp->tx0, 4);             /* XT0siz */
2276         l_current_ptr+=4;
2277
2278         opj_write_bytes(l_current_ptr, cp->ty0, 4);             /* YT0siz */
2279         l_current_ptr+=4;
2280
2281         opj_write_bytes(l_current_ptr, l_image->numcomps, 2);   /* Csiz */
2282         l_current_ptr+=2;
2283
2284         for (i = 0; i < l_image->numcomps; ++i) {
2285                 /* TODO here with MCT ? */
2286                 opj_write_bytes(l_current_ptr, l_img_comp->prec - 1 + (l_img_comp->sgnd << 7), 1);      /* Ssiz_i */
2287                 ++l_current_ptr;
2288
2289                 opj_write_bytes(l_current_ptr, l_img_comp->dx, 1);      /* XRsiz_i */
2290                 ++l_current_ptr;
2291
2292                 opj_write_bytes(l_current_ptr, l_img_comp->dy, 1);      /* YRsiz_i */
2293                 ++l_current_ptr;
2294
2295                 ++l_img_comp;
2296         }
2297
2298         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) {
2299                 return OPJ_FALSE;
2300         }
2301
2302         return OPJ_TRUE;
2303 }
2304
2305 static void j2k_read_siz(opj_j2k_t *j2k) {
2306         int len;
2307         OPJ_UINT32 i;
2308         
2309         opj_cio_t *cio = j2k->cio;
2310         opj_image_t *image = j2k->image;
2311         opj_cp_t *cp = j2k->cp;
2312         
2313         len = cio_read(cio, 2);                 /* Lsiz */
2314         cio_read(cio, 2);                               /* Rsiz (capabilities) */
2315         image->x1 = cio_read(cio, 4);   /* Xsiz */
2316         image->y1 = cio_read(cio, 4);   /* Ysiz */
2317         image->x0 = cio_read(cio, 4);   /* X0siz */
2318         image->y0 = cio_read(cio, 4);   /* Y0siz */
2319         cp->tdx = cio_read(cio, 4);             /* XTsiz */
2320         cp->tdy = cio_read(cio, 4);             /* YTsiz */
2321         cp->tx0 = cio_read(cio, 4);             /* XT0siz */
2322         cp->ty0 = cio_read(cio, 4);             /* YT0siz */
2323         
2324   /* the following code triggers: */
2325   /* warning: comparison of unsigned expression < 0 is always false */
2326 #if 0
2327         if ((image->x0<0)||(image->x1<0)||(image->y0<0)||(image->y1<0)) {
2328                 opj_event_msg(j2k->cinfo, EVT_ERROR,
2329                                                                         "%s: invalid image size (x0:%d, x1:%d, y0:%d, y1:%d)\n",
2330                                                                         image->x0,image->x1,image->y0,image->y1);
2331                 return;
2332         }
2333 #endif
2334         
2335         image->numcomps = cio_read(cio, 2);     /* Csiz */
2336
2337 #ifdef USE_JPWL
2338         if (j2k->cp->correct) {
2339                 /* if JPWL is on, we check whether TX errors have damaged
2340                   too much the SIZ parameters */
2341                 if (!(image->x1 * image->y1)) {
2342                         opj_event_msg(j2k->cinfo, EVT_ERROR,
2343                                 "JPWL: bad image size (%d x %d)\n",
2344                                 image->x1, image->y1);
2345                         if (!JPWL_ASSUME || JPWL_ASSUME) {
2346                                 opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
2347                                 return;
2348                         }
2349                 }
2350     assert( len >= 38 );
2351                 if (image->numcomps != (OPJ_UINT32)((len - 38) / 3)) {
2352                         opj_event_msg(j2k->cinfo, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
2353                                 "JPWL: Csiz is %d => space in SIZ only for %d comps.!!!\n",
2354                                 image->numcomps, ((len - 38) / 3));
2355                         if (!JPWL_ASSUME) {
2356                                 opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
2357                                 return;
2358                         }
2359                         /* we try to correct */
2360                         opj_event_msg(j2k->cinfo, EVT_WARNING, "- trying to adjust this\n");
2361                         if (image->numcomps < (OPJ_UINT32)((len - 38) / 3)) {
2362                                 len = 38 + 3 * image->numcomps;
2363                                 opj_event_msg(j2k->cinfo, EVT_WARNING, "- setting Lsiz to %d => HYPOTHESIS!!!\n",
2364                                         len);                           
2365                         } else {
2366                                 image->numcomps = ((len - 38) / 3);
2367                                 opj_event_msg(j2k->cinfo, EVT_WARNING, "- setting Csiz to %d => HYPOTHESIS!!!\n",
2368                                         image->numcomps);                               
2369                         }
2370                 }
2371
2372                 /* update components number in the jpwl_exp_comps filed */
2373                 cp->exp_comps = image->numcomps;
2374         }
2375 #endif /* USE_JPWL */
2376
2377         image->comps = (opj_image_comp_t*) opj_calloc(image->numcomps, sizeof(opj_image_comp_t));
2378         for (i = 0; i < image->numcomps; i++) {
2379                 int tmp, w, h;
2380                 tmp = cio_read(cio, 1);         /* Ssiz_i */
2381                 image->comps[i].prec = (tmp & 0x7f) + 1;
2382                 image->comps[i].sgnd = tmp >> 7;
2383                 image->comps[i].dx = cio_read(cio, 1);  /* XRsiz_i */
2384                 image->comps[i].dy = cio_read(cio, 1);  /* YRsiz_i */
2385                 
2386 #ifdef USE_JPWL
2387                 if (j2k->cp->correct) {
2388                 /* if JPWL is on, we check whether TX errors have damaged
2389                         too much the SIZ parameters, again */
2390                         if (!(image->comps[i].dx * image->comps[i].dy)) {
2391                                 opj_event_msg(j2k->cinfo, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
2392                                         "JPWL: bad XRsiz_%d/YRsiz_%d (%d x %d)\n",
2393                                         i, i, image->comps[i].dx, image->comps[i].dy);
2394                                 if (!JPWL_ASSUME) {
2395                                         opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
2396                                         return;
2397                                 }
2398                                 /* we try to correct */
2399                                 opj_event_msg(j2k->cinfo, EVT_WARNING, "- trying to adjust them\n");
2400                                 if (!image->comps[i].dx) {
2401                                         image->comps[i].dx = 1;
2402                                         opj_event_msg(j2k->cinfo, EVT_WARNING, "- setting XRsiz_%d to %d => HYPOTHESIS!!!\n",
2403                                                 i, image->comps[i].dx);
2404                                 }
2405                                 if (!image->comps[i].dy) {
2406                                         image->comps[i].dy = 1;
2407                                         opj_event_msg(j2k->cinfo, EVT_WARNING, "- setting YRsiz_%d to %d => HYPOTHESIS!!!\n",
2408                                                 i, image->comps[i].dy);
2409                                 }
2410                         }
2411                         
2412                 }
2413 #endif /* USE_JPWL */
2414
2415                 /* TODO: unused ? */
2416                 w = int_ceildiv(image->x1 - image->x0, image->comps[i].dx);
2417                 h = int_ceildiv(image->y1 - image->y0, image->comps[i].dy);
2418
2419                 image->comps[i].resno_decoded = 0;      /* number of resolution decoded */
2420                 image->comps[i].factor = cp->reduce; /* reducing factor per component */
2421         }
2422         
2423         cp->tw = int_ceildiv(image->x1 - cp->tx0, cp->tdx);
2424         cp->th = int_ceildiv(image->y1 - cp->ty0, cp->tdy);
2425
2426 #ifdef USE_JPWL
2427         if (j2k->cp->correct) {
2428                 /* if JPWL is on, we check whether TX errors have damaged
2429                   too much the SIZ parameters */
2430                 if ((cp->tw < 1) || (cp->th < 1) || (cp->tw > cp->max_tiles) || (cp->th > cp->max_tiles)) {
2431                         opj_event_msg(j2k->cinfo, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
2432                                 "JPWL: bad number of tiles (%d x %d)\n",
2433                                 cp->tw, cp->th);
2434                         if (!JPWL_ASSUME) {
2435                                 opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
2436                                 return;
2437                         }
2438                         /* we try to correct */
2439                         opj_event_msg(j2k->cinfo, EVT_WARNING, "- trying to adjust them\n");
2440                         if (cp->tw < 1) {
2441                                 cp->tw= 1;
2442                                 opj_event_msg(j2k->cinfo, EVT_WARNING, "- setting %d tiles in x => HYPOTHESIS!!!\n",
2443                                         cp->tw);
2444                         }
2445                         if (cp->tw > cp->max_tiles) {
2446                                 cp->tw= 1;
2447                                 opj_event_msg(j2k->cinfo, EVT_WARNING, "- too large x, increase expectance of %d\n"
2448                                         "- setting %d tiles in x => HYPOTHESIS!!!\n",
2449                                         cp->max_tiles, cp->tw);
2450                         }
2451                         if (cp->th < 1) {
2452                                 cp->th= 1;
2453                                 opj_event_msg(j2k->cinfo, EVT_WARNING, "- setting %d tiles in y => HYPOTHESIS!!!\n",
2454                                         cp->th);
2455                         }
2456                         if (cp->th > cp->max_tiles) {
2457                                 cp->th= 1;
2458                                 opj_event_msg(j2k->cinfo, EVT_WARNING, "- too large y, increase expectance of %d to continue\n",
2459                                         "- setting %d tiles in y => HYPOTHESIS!!!\n",
2460                                         cp->max_tiles, cp->th);
2461                         }
2462                 }
2463         }
2464 #endif /* USE_JPWL */
2465
2466         cp->tcps = (opj_tcp_t*) opj_calloc(cp->tw * cp->th, sizeof(opj_tcp_t));
2467         cp->tileno = (int*) opj_malloc(cp->tw * cp->th * sizeof(int));
2468         cp->tileno_size = 0;
2469         
2470 #ifdef USE_JPWL
2471         if (j2k->cp->correct) {
2472                 if (!cp->tcps) {
2473                         opj_event_msg(j2k->cinfo, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
2474                                 "JPWL: could not alloc tcps field of cp\n");
2475                         if (!JPWL_ASSUME || JPWL_ASSUME) {
2476                                 opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
2477                                 return;
2478                         }
2479                 }
2480         }
2481 #endif /* USE_JPWL */
2482
2483         for (i = 0; i < (OPJ_UINT32)cp->tw * cp->th; i++) {
2484                 cp->tcps[i].POC = 0;
2485                 cp->tcps[i].numpocs = 0;
2486                 cp->tcps[i].first = 1;
2487         }
2488         
2489         /* Initialization for PPM marker */
2490         cp->ppm = 0;
2491         cp->ppm_data = NULL;
2492         cp->ppm_data_first = NULL;
2493         cp->ppm_previous = 0;
2494         cp->ppm_store = 0;
2495
2496         j2k->default_tcp->tccps = (opj_tccp_t*) opj_calloc(image->numcomps, sizeof(opj_tccp_t));
2497         for (i = 0; i < (OPJ_UINT32)cp->tw * cp->th; i++) {
2498                 cp->tcps[i].tccps = (opj_tccp_t*) opj_malloc(image->numcomps * sizeof(opj_tccp_t));
2499         }       
2500         j2k->tile_data = (unsigned char**) opj_calloc(cp->tw * cp->th, sizeof(unsigned char*));
2501         j2k->tile_len = (int*) opj_calloc(cp->tw * cp->th, sizeof(int));
2502         j2k->state = J2K_STATE_MH;
2503
2504         /* Index */
2505         if (j2k->cstr_info) {
2506                 opj_codestream_info_t *cstr_info = j2k->cstr_info;
2507                 cstr_info->image_w = image->x1 - image->x0;
2508                 cstr_info->image_h = image->y1 - image->y0;
2509                 cstr_info->numcomps = image->numcomps;
2510                 cstr_info->tw = cp->tw;
2511                 cstr_info->th = cp->th;
2512                 cstr_info->tile_x = cp->tdx;    
2513                 cstr_info->tile_y = cp->tdy;    
2514                 cstr_info->tile_Ox = cp->tx0;   
2515                 cstr_info->tile_Oy = cp->ty0;                   
2516                 cstr_info->tile = (opj_tile_info_t*) opj_calloc(cp->tw * cp->th, sizeof(opj_tile_info_t));              
2517         }
2518 }
2519
2520
2521 /**
2522  * Reads a SIZ marker (image and tile size)
2523  * @param       p_header_data   the data contained in the SIZ box.
2524  * @param       jp2                             the jpeg2000 file codec.
2525  * @param       p_header_size   the size of the data contained in the SIZ marker.
2526  * @param       p_manager               the user event manager.
2527 */
2528 opj_bool j2k_read_siz_v2 (
2529                                     opj_j2k_v2_t *p_j2k,
2530                                         OPJ_BYTE * p_header_data,
2531                                         OPJ_UINT32 p_header_size,
2532                                         struct opj_event_mgr * p_manager
2533                                         )
2534 {
2535         OPJ_UINT32 l_size, i;
2536         OPJ_UINT32 l_nb_comp;
2537         OPJ_UINT32 l_nb_comp_remain;
2538         OPJ_UINT32 l_remaining_size;
2539         OPJ_UINT32 l_nb_tiles;
2540         OPJ_UINT32 l_tmp;
2541         opj_image_t *l_image = 00;
2542         opj_cp_v2_t *l_cp = 00;
2543         opj_image_comp_t * l_img_comp = 00;
2544         opj_tcp_v2_t * l_current_tile_param = 00;
2545
2546         /* preconditions */
2547         assert(p_j2k != 00);
2548         assert(p_manager != 00);
2549         assert(p_header_data != 00);
2550
2551         l_image = p_j2k->m_private_image;
2552         l_cp = &(p_j2k->m_cp);
2553
2554         /* minimum size == 39 - 3 (= minimum component parameter) */
2555         if (p_header_size < 36) {
2556                 opj_event_msg_v2(p_manager, EVT_ERROR, "Error with SIZ marker size\n");
2557                 return OPJ_FALSE;
2558         }
2559
2560         l_remaining_size = p_header_size - 36;
2561         l_nb_comp = l_remaining_size / 3;
2562         l_nb_comp_remain = l_remaining_size % 3;
2563         if (l_nb_comp_remain != 0){
2564                 opj_event_msg_v2(p_manager, EVT_ERROR, "Error with SIZ marker size\n");
2565                 return OPJ_FALSE;
2566         }
2567
2568         l_size = p_header_size + 2;                                                                             /* Lsiz */
2569
2570         opj_read_bytes(p_header_data,&l_tmp ,2);                                                /* Rsiz (capabilities) */
2571         p_header_data+=2;
2572         l_cp->rsiz = (OPJ_RSIZ_CAPABILITIES) l_tmp;
2573         opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_image->x1, 4);   /* Xsiz */
2574         p_header_data+=4;
2575         opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_image->y1, 4);   /* Ysiz */
2576         p_header_data+=4;
2577         opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_image->x0, 4);   /* X0siz */
2578         p_header_data+=4;
2579         opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_image->y0, 4);   /* Y0siz */
2580         p_header_data+=4;
2581         opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_cp->tdx, 4);             /* XTsiz */
2582         p_header_data+=4;
2583         opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_cp->tdy, 4);             /* YTsiz */
2584         p_header_data+=4;
2585         opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_cp->tx0, 4);             /* XT0siz */
2586         p_header_data+=4;
2587         opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_cp->ty0, 4);             /* YT0siz */
2588         p_header_data+=4;
2589         opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_tmp, 2);                 /* Csiz */
2590         p_header_data+=2;
2591         if (l_tmp < 16385)
2592                 l_image->numcomps = (OPJ_UINT16) l_tmp;
2593         else {
2594                 opj_event_msg_v2(p_manager, EVT_ERROR, "Error with SIZ marker: number of component is illegal -> %d\n", l_tmp);
2595                 return OPJ_FALSE;
2596         }
2597
2598         if (l_image->numcomps != l_nb_comp) {
2599                 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);
2600                 return OPJ_FALSE;
2601         }
2602
2603 #ifdef USE_JPWL
2604         if (l_cp->correct) {
2605                 /* if JPWL is on, we check whether TX errors have damaged
2606                   too much the SIZ parameters */
2607                 if (!(l_image->x1 * l_image->y1)) {
2608                         opj_event_msg_v2(p_manager, EVT_ERROR,
2609                                 "JPWL: bad image size (%d x %d)\n",
2610                                 l_image->x1, l_image->y1);
2611                         if (!JPWL_ASSUME || JPWL_ASSUME) {
2612                                 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
2613                                 return OPJ_FALSE;
2614                         }
2615                 }
2616
2617         /* FIXME check previously in the function so why keep this piece of code ? Need by the norm ?
2618                 if (l_image->numcomps != ((len - 38) / 3)) {
2619                         opj_event_msg_v2(p_manager, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
2620                                 "JPWL: Csiz is %d => space in SIZ only for %d comps.!!!\n",
2621                                 l_image->numcomps, ((len - 38) / 3));
2622                         if (!JPWL_ASSUME) {
2623                                 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
2624                                 return OPJ_FALSE;
2625                         }
2626         */              /* we try to correct */
2627         /*              opj_event_msg_v2(p_manager, EVT_WARNING, "- trying to adjust this\n");
2628                         if (l_image->numcomps < ((len - 38) / 3)) {
2629                                 len = 38 + 3 * l_image->numcomps;
2630                                 opj_event_msg_v2(p_manager, EVT_WARNING, "- setting Lsiz to %d => HYPOTHESIS!!!\n",
2631                                         len);
2632                         } else {
2633                                 l_image->numcomps = ((len - 38) / 3);
2634                                 opj_event_msg_v2(p_manager, EVT_WARNING, "- setting Csiz to %d => HYPOTHESIS!!!\n",
2635                                         l_image->numcomps);
2636                         }
2637                 }
2638         */
2639
2640                 /* update components number in the jpwl_exp_comps filed */
2641                 l_cp->exp_comps = l_image->numcomps;
2642         }
2643 #endif /* USE_JPWL */
2644
2645         /* Allocate the resulting image components */
2646         l_image->comps = (opj_image_comp_t*) opj_calloc(l_image->numcomps, sizeof(opj_image_comp_t));
2647         if (l_image->comps == 00){
2648                 l_image->numcomps = 0;
2649                 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to take in charge SIZ marker\n");
2650                 return OPJ_FALSE;
2651         }
2652
2653         memset(l_image->comps,0,l_image->numcomps * sizeof(opj_image_comp_t));
2654         l_img_comp = l_image->comps;
2655
2656         /* Read the component information */
2657         for (i = 0; i < l_image->numcomps; ++i){
2658                 OPJ_UINT32 tmp;
2659                 opj_read_bytes(p_header_data,&tmp,1);   /* Ssiz_i */
2660                 ++p_header_data;
2661                 l_img_comp->prec = (tmp & 0x7f) + 1;
2662                 l_img_comp->sgnd = tmp >> 7;
2663                 opj_read_bytes(p_header_data,&tmp,1);   /* XRsiz_i */
2664                 ++p_header_data;
2665                 l_img_comp->dx = (OPJ_INT32)tmp; /* should be between 1 and 255 */
2666                 opj_read_bytes(p_header_data,&tmp,1);   /* YRsiz_i */
2667                 ++p_header_data;
2668                 l_img_comp->dy = (OPJ_INT32)tmp; /* should be between 1 and 255 */
2669
2670 #ifdef USE_JPWL
2671                 if (l_cp->correct) {
2672                 /* if JPWL is on, we check whether TX errors have damaged
2673                         too much the SIZ parameters, again */
2674                         if (!(l_image->comps[i].dx * l_image->comps[i].dy)) {
2675                                 opj_event_msg_v2(p_manager, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
2676                                         "JPWL: bad XRsiz_%d/YRsiz_%d (%d x %d)\n",
2677                                         i, i, l_image->comps[i].dx, l_image->comps[i].dy);
2678                                 if (!JPWL_ASSUME) {
2679                                         opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
2680                                         return OPJ_FALSE;
2681                                 }
2682                                 /* we try to correct */
2683                                 opj_event_msg_v2(p_manager, EVT_WARNING, "- trying to adjust them\n");
2684                                 if (!l_image->comps[i].dx) {
2685                                         l_image->comps[i].dx = 1;
2686                                         opj_event_msg_v2(p_manager, EVT_WARNING, "- setting XRsiz_%d to %d => HYPOTHESIS!!!\n",
2687                                                 i, l_image->comps[i].dx);
2688                                 }
2689                                 if (!l_image->comps[i].dy) {
2690                                         l_image->comps[i].dy = 1;
2691                                         opj_event_msg_v2(p_manager, EVT_WARNING, "- setting YRsiz_%d to %d => HYPOTHESIS!!!\n",
2692                                                 i, l_image->comps[i].dy);
2693                                 }
2694                         }
2695                 }
2696 #endif /* USE_JPWL */
2697                 l_img_comp->resno_decoded = 0;                                                          /* number of resolution decoded */
2698                 l_img_comp->factor = l_cp->m_specific_param.m_dec.m_reduce; /* reducing factor per component */
2699                 ++l_img_comp;
2700         }
2701
2702         /* Compute the number of tiles */
2703         l_cp->tw = int_ceildiv(l_image->x1 - l_cp->tx0, l_cp->tdx);
2704         l_cp->th = int_ceildiv(l_image->y1 - l_cp->ty0, l_cp->tdy);
2705         l_nb_tiles = l_cp->tw * l_cp->th;
2706
2707         /* Define the tiles which will be decoded */
2708         if (p_j2k->m_specific_param.m_decoder.m_discard_tiles) {
2709                 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;
2710                 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;
2711                 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);
2712                 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);
2713         }
2714         else {
2715                 p_j2k->m_specific_param.m_decoder.m_start_tile_x = 0;
2716                 p_j2k->m_specific_param.m_decoder.m_start_tile_y = 0;
2717                 p_j2k->m_specific_param.m_decoder.m_end_tile_x = l_cp->tw;
2718                 p_j2k->m_specific_param.m_decoder.m_end_tile_y = l_cp->th;
2719         }
2720
2721 #ifdef USE_JPWL
2722         if (l_cp->correct) {
2723                 /* if JPWL is on, we check whether TX errors have damaged
2724                   too much the SIZ parameters */
2725                 if ((l_cp->tw < 1) || (l_cp->th < 1) || (l_cp->tw > l_cp->max_tiles) || (l_cp->th > l_cp->max_tiles)) {
2726                         opj_event_msg_v2(p_manager, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
2727                                 "JPWL: bad number of tiles (%d x %d)\n",
2728                                 l_cp->tw, l_cp->th);
2729                         if (!JPWL_ASSUME) {
2730                                 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
2731                                 return OPJ_FALSE;
2732                         }
2733                         /* we try to correct */
2734                         opj_event_msg_v2(p_manager, EVT_WARNING, "- trying to adjust them\n");
2735                         if (l_cp->tw < 1) {
2736                                 l_cp->tw= 1;
2737                                 opj_event_msg_v2(p_manager, EVT_WARNING, "- setting %d tiles in x => HYPOTHESIS!!!\n",
2738                                                 l_cp->tw);
2739                         }
2740                         if (l_cp->tw > l_cp->max_tiles) {
2741                                 l_cp->tw= 1;
2742                                 opj_event_msg_v2(p_manager, EVT_WARNING, "- too large x, increase expectance of %d\n"
2743                                         "- setting %d tiles in x => HYPOTHESIS!!!\n",
2744                                         l_cp->max_tiles, l_cp->tw);
2745                         }
2746                         if (l_cp->th < 1) {
2747                                 l_cp->th= 1;
2748                                 opj_event_msg_v2(p_manager, EVT_WARNING, "- setting %d tiles in y => HYPOTHESIS!!!\n",
2749                                                 l_cp->th);
2750                         }
2751                         if (l_cp->th > l_cp->max_tiles) {
2752                                 l_cp->th= 1;
2753                                 opj_event_msg_v2(p_manager, EVT_WARNING, "- too large y, increase expectance of %d to continue\n",
2754                                         "- setting %d tiles in y => HYPOTHESIS!!!\n",
2755                                         l_cp->max_tiles, l_cp->th);
2756                         }
2757                 }
2758         }
2759 #endif /* USE_JPWL */
2760
2761         /* memory allocations */
2762         l_cp->tcps = (opj_tcp_v2_t*) opj_calloc(l_nb_tiles, sizeof(opj_tcp_v2_t));
2763         if (l_cp->tcps == 00) {
2764                 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to take in charge SIZ marker\n");
2765                 return OPJ_FALSE;
2766         }
2767         memset(l_cp->tcps,0,l_nb_tiles*sizeof(opj_tcp_t));
2768
2769 #ifdef USE_JPWL
2770         if (l_cp->correct) {
2771                 if (!l_cp->tcps) {
2772                         opj_event_msg_v2(p_manager, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
2773                                 "JPWL: could not alloc tcps field of cp\n");
2774                         if (!JPWL_ASSUME || JPWL_ASSUME) {
2775                                 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
2776                                 return OPJ_FALSE;
2777                         }
2778                 }
2779         }
2780 #endif /* USE_JPWL */
2781
2782         p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps =
2783                         (opj_tccp_t*) opj_calloc(l_image->numcomps, sizeof(opj_tccp_t));
2784         if(p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps  == 00) {
2785                 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to take in charge SIZ marker\n");
2786                 return OPJ_FALSE;
2787         }
2788         memset(p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps ,0,l_image->numcomps*sizeof(opj_tccp_t));
2789
2790         p_j2k->m_specific_param.m_decoder.m_default_tcp->m_mct_records =
2791                         (opj_mct_data_t*)opj_malloc(J2K_MCT_DEFAULT_NB_RECORDS * sizeof(opj_mct_data_t));
2792
2793         if (! p_j2k->m_specific_param.m_decoder.m_default_tcp->m_mct_records) {
2794                 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to take in charge SIZ marker\n");
2795                 return OPJ_FALSE;
2796         }
2797         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));
2798         p_j2k->m_specific_param.m_decoder.m_default_tcp->m_nb_max_mct_records = J2K_MCT_DEFAULT_NB_RECORDS;
2799
2800         p_j2k->m_specific_param.m_decoder.m_default_tcp->m_mcc_records =
2801                         (opj_simple_mcc_decorrelation_data_t*)
2802                         opj_malloc(J2K_MCC_DEFAULT_NB_RECORDS * sizeof(opj_simple_mcc_decorrelation_data_t));
2803
2804         if (! p_j2k->m_specific_param.m_decoder.m_default_tcp->m_mcc_records) {
2805                 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to take in charge SIZ marker\n");
2806                 return OPJ_FALSE;
2807         }
2808         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));
2809         p_j2k->m_specific_param.m_decoder.m_default_tcp->m_nb_max_mcc_records = J2K_MCC_DEFAULT_NB_RECORDS;
2810
2811         /* set up default dc level shift */
2812         for (i=0;i<l_image->numcomps;++i) {
2813                 if (! l_image->comps[i].sgnd) {
2814                         p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps[i].m_dc_level_shift = 1 << (l_image->comps[i].prec - 1);
2815                 }
2816         }
2817
2818         l_current_tile_param = l_cp->tcps;
2819         for     (i = 0; i < l_nb_tiles; ++i) {
2820                 l_current_tile_param->tccps = (opj_tccp_t*) opj_malloc(l_image->numcomps * sizeof(opj_tccp_t));
2821                 if (l_current_tile_param->tccps == 00) {
2822                         opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to take in charge SIZ marker\n");
2823                         return OPJ_FALSE;
2824                 }
2825                 memset(l_current_tile_param->tccps,0,l_image->numcomps * sizeof(opj_tccp_t));
2826
2827                 ++l_current_tile_param;
2828         }
2829
2830         p_j2k->m_specific_param.m_decoder.m_state =  J2K_STATE_MH; /* FIXME J2K_DEC_STATE_MH; */
2831         opj_image_comp_header_update(l_image,l_cp);
2832
2833         return OPJ_TRUE;
2834 }
2835
2836
2837
2838 static void j2k_write_com(opj_j2k_t *j2k) {
2839         unsigned int i;
2840         int lenp, len;
2841
2842         if(j2k->cp->comment) {
2843                 opj_cio_t *cio = j2k->cio;
2844                 char *comment = j2k->cp->comment;
2845
2846                 cio_write(cio, J2K_MS_COM, 2);
2847                 lenp = cio_tell(cio);
2848                 cio_skip(cio, 2);
2849                 cio_write(cio, 1, 2);           /* General use (IS 8859-15:1999 (Latin) values) */
2850                 for (i = 0; i < strlen(comment); i++) {
2851                         cio_write(cio, comment[i], 1);
2852                 }
2853                 len = cio_tell(cio) - lenp;
2854                 cio_seek(cio, lenp);
2855                 cio_write(cio, len, 2);
2856                 cio_seek(cio, lenp + len);
2857
2858                 
2859                 if(j2k->cstr_info)
2860                   j2k_add_mhmarker(j2k->cstr_info, J2K_MS_COM, lenp, len);
2861
2862         }
2863 }
2864
2865 /**
2866  * Writes the COM marker (comment)
2867  * 
2868  * @param       p_stream                        the stream to write data to.
2869  * @param       p_j2k                   J2K codec.
2870  * @param       p_manager       the user event manager.
2871 */
2872 opj_bool j2k_write_com_v2(      opj_j2k_v2_t *p_j2k,
2873                                                         struct opj_stream_private *p_stream,
2874                                                         struct opj_event_mgr * p_manager )
2875 {
2876         OPJ_UINT32 l_comment_size;
2877         OPJ_UINT32 l_total_com_size;
2878         const OPJ_CHAR *l_comment;
2879         OPJ_BYTE * l_current_ptr = 00;
2880
2881         // preconditions
2882         assert(p_j2k != 00);
2883         assert(p_stream != 00);
2884         assert(p_manager != 00);
2885         
2886         l_comment = p_j2k->m_cp.comment;
2887         l_comment_size = strlen(l_comment);
2888         l_total_com_size = l_comment_size + 6;
2889
2890         if (l_total_com_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
2891                 p_j2k->m_specific_param.m_encoder.m_header_tile_data 
2892                         = (OPJ_BYTE*)opj_realloc(       p_j2k->m_specific_param.m_encoder.m_header_tile_data,
2893                                                                                 l_total_com_size);
2894                 
2895                 if(! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
2896                         return OPJ_FALSE;
2897                 }
2898
2899                 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_total_com_size;
2900         }
2901
2902         l_current_ptr = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
2903         
2904         opj_write_bytes(l_current_ptr,J2K_MS_COM , 2);  /* COM */
2905         l_current_ptr+=2;
2906         
2907         opj_write_bytes(l_current_ptr,l_total_com_size - 2 , 2);        /* L_COM */
2908         l_current_ptr+=2;
2909         
2910         opj_write_bytes(l_current_ptr,1 , 2);   /* General use (IS 8859-15:1999 (Latin) values) */
2911         l_current_ptr+=2;
2912         
2913         memcpy( l_current_ptr,l_comment,l_comment_size);
2914         
2915         if (opj_stream_write_data(p_stream,p_j2k->m_specific_param.m_encoder.m_header_tile_data,l_total_com_size,p_manager) != l_total_com_size) {
2916                 return OPJ_FALSE;
2917         }
2918
2919         return OPJ_TRUE;
2920 }
2921
2922 static void j2k_read_com(opj_j2k_t *j2k) {
2923         int len;
2924         
2925         opj_cio_t *cio = j2k->cio;
2926
2927         len = cio_read(cio, 2);
2928         cio_skip(cio, len - 2);  
2929 }
2930 /**
2931  * Reads a COM marker (comments)
2932  * @param       p_header_data   the data contained in the COM box.
2933  * @param       jp2                             the jpeg2000 file codec.
2934  * @param       p_header_size   the size of the data contained in the COM marker.
2935  * @param       p_manager               the user event manager.
2936 */
2937 opj_bool j2k_read_com_v2 (
2938                                         opj_j2k_v2_t *p_j2k,
2939                                         OPJ_BYTE * p_header_data,
2940                                         OPJ_UINT32 p_header_size,
2941                                         struct opj_event_mgr * p_manager
2942                                         )
2943 {
2944         /* preconditions */
2945         assert(p_j2k != 00);
2946         assert(p_manager != 00);
2947         assert(p_header_data != 00);
2948   (void)p_header_size;
2949
2950         return OPJ_TRUE;
2951 }
2952
2953 static void j2k_write_cox(opj_j2k_t *j2k, int compno) {
2954         OPJ_UINT32 i;
2955
2956         opj_cp_t *cp = j2k->cp;
2957         opj_tcp_t *tcp = &cp->tcps[j2k->curtileno];
2958         opj_tccp_t *tccp = &tcp->tccps[compno];
2959         opj_cio_t *cio = j2k->cio;
2960         
2961         cio_write(cio, tccp->numresolutions - 1, 1);    /* SPcox (D) */
2962         cio_write(cio, tccp->cblkw - 2, 1);                             /* SPcox (E) */
2963         cio_write(cio, tccp->cblkh - 2, 1);                             /* SPcox (F) */
2964         cio_write(cio, tccp->cblksty, 1);                               /* SPcox (G) */
2965         cio_write(cio, tccp->qmfbid, 1);                                /* SPcox (H) */
2966         
2967         if (tccp->csty & J2K_CCP_CSTY_PRT) {
2968                 for (i = 0; i < tccp->numresolutions; i++) {
2969                         cio_write(cio, tccp->prcw[i] + (tccp->prch[i] << 4), 1);        /* SPcox (I_i) */
2970                 }
2971         }
2972 }
2973
2974 static void j2k_read_cox(opj_j2k_t *j2k, int compno) {
2975         OPJ_UINT32 i;
2976
2977         opj_cp_t *cp = j2k->cp;
2978         opj_tcp_t *tcp = j2k->state == J2K_STATE_TPH ? &cp->tcps[j2k->curtileno] : j2k->default_tcp;
2979         opj_tccp_t *tccp = &tcp->tccps[compno];
2980         opj_cio_t *cio = j2k->cio;
2981
2982         tccp->numresolutions = cio_read(cio, 1) + 1;    /* SPcox (D) */
2983
2984         /* If user wants to remove more resolutions than the codestream contains, return error*/
2985         assert(cp->reduce >= 0);
2986         if ((OPJ_UINT32)cp->reduce >= tccp->numresolutions) {
2987                 opj_event_msg(j2k->cinfo, EVT_ERROR, "Error decoding component %d.\nThe number of resolutions to remove is higher than the number "
2988                                         "of resolutions of this component\nModify the cp_reduce parameter.\n\n", compno);
2989                 j2k->state |= J2K_STATE_ERR;
2990         }
2991
2992         tccp->cblkw = cio_read(cio, 1) + 2;     /* SPcox (E) */
2993         tccp->cblkh = cio_read(cio, 1) + 2;     /* SPcox (F) */
2994         tccp->cblksty = cio_read(cio, 1);       /* SPcox (G) */
2995         tccp->qmfbid = cio_read(cio, 1);        /* SPcox (H) */
2996         if (tccp->csty & J2K_CP_CSTY_PRT) {
2997                 for (i = 0; i < tccp->numresolutions; i++) {
2998                         int tmp = cio_read(cio, 1);     /* SPcox (I_i) */
2999                         tccp->prcw[i] = tmp & 0xf;
3000                         tccp->prch[i] = tmp >> 4;
3001                 }
3002         }
3003
3004         /* INDEX >> */
3005         if(j2k->cstr_info && compno == 0) {
3006                 for (i = 0; i < tccp->numresolutions; i++) {
3007                         if (tccp->csty & J2K_CP_CSTY_PRT) {
3008                                 j2k->cstr_info->tile[j2k->curtileno].pdx[i] = tccp->prcw[i];
3009                                 j2k->cstr_info->tile[j2k->curtileno].pdy[i] = tccp->prch[i];
3010                         }
3011                         else {
3012                                 j2k->cstr_info->tile[j2k->curtileno].pdx[i] = 15;
3013                                 j2k->cstr_info->tile[j2k->curtileno].pdx[i] = 15;
3014                         }
3015                 }
3016         }
3017         /* << INDEX */
3018 }
3019
3020 static void j2k_write_cod(opj_j2k_t *j2k) {
3021         opj_cp_t *cp = NULL;
3022         opj_tcp_t *tcp = NULL;
3023         int lenp, len;
3024
3025         opj_cio_t *cio = j2k->cio;
3026         
3027         cio_write(cio, J2K_MS_COD, 2);  /* COD */
3028         
3029         lenp = cio_tell(cio);
3030         cio_skip(cio, 2);
3031         
3032         cp = j2k->cp;
3033         tcp = &cp->tcps[j2k->curtileno];
3034
3035         cio_write(cio, tcp->csty, 1);           /* Scod */
3036         cio_write(cio, tcp->prg, 1);            /* SGcod (A) */
3037         cio_write(cio, tcp->numlayers, 2);      /* SGcod (B) */
3038         cio_write(cio, tcp->mct, 1);            /* SGcod (C) */
3039         
3040         j2k_write_cox(j2k, 0);
3041         len = cio_tell(cio) - lenp;
3042         cio_seek(cio, lenp);
3043         cio_write(cio, len, 2);         /* Lcod */
3044         cio_seek(cio, lenp + len);
3045
3046         if(j2k->cstr_info)
3047           j2k_add_mhmarker(j2k->cstr_info, J2K_MS_COD, lenp, len);
3048
3049 }
3050
3051 /**
3052  * Writes the COD marker (Coding style default)
3053  *
3054  * @param       p_stream                        the stream to write data to.
3055  * @param       p_j2k                   J2K codec.
3056  * @param       p_manager       the user event manager.
3057 */
3058 opj_bool j2k_write_cod_v2(      opj_j2k_v2_t *p_j2k,
3059                                                         struct opj_stream_private *p_stream,
3060                                                         struct opj_event_mgr * p_manager )
3061 {
3062         opj_cp_v2_t *l_cp = 00;
3063         opj_tcp_v2_t *l_tcp = 00;
3064         OPJ_UINT32 l_code_size,l_remaining_size;
3065         OPJ_BYTE * l_current_data = 00;
3066
3067         /* preconditions */
3068         assert(p_j2k != 00);
3069         assert(p_manager != 00);
3070         assert(p_stream != 00);
3071
3072         l_cp = &(p_j2k->m_cp);
3073         l_tcp = &l_cp->tcps[p_j2k->m_current_tile_number];
3074         l_code_size = 9 + j2k_get_SPCod_SPCoc_size(p_j2k,p_j2k->m_current_tile_number,0);
3075         l_remaining_size = l_code_size;
3076
3077         if (l_code_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
3078                 p_j2k->m_specific_param.m_encoder.m_header_tile_data
3079                         = (OPJ_BYTE*)opj_realloc(
3080                                 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
3081                                 l_code_size);
3082
3083                 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
3084                         return OPJ_FALSE;
3085                 }
3086
3087                 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_code_size;
3088         }
3089
3090         l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
3091
3092         opj_write_bytes(l_current_data,J2K_MS_COD,2);           /* COD */
3093         l_current_data += 2;
3094
3095         opj_write_bytes(l_current_data,l_code_size-2,2);        /* L_COD */
3096         l_current_data += 2;
3097
3098         opj_write_bytes(l_current_data,l_tcp->csty,1);          /* Scod */
3099         ++l_current_data;
3100
3101         opj_write_bytes(l_current_data,l_tcp->prg,1);           /* SGcod (A) */
3102         ++l_current_data;
3103
3104         opj_write_bytes(l_current_data,l_tcp->numlayers,2);     /* SGcod (B) */
3105         l_current_data+=2;
3106
3107         opj_write_bytes(l_current_data,l_tcp->mct,1);           /* SGcod (C) */
3108         ++l_current_data;
3109
3110         l_remaining_size -= 9;
3111
3112         if (! j2k_write_SPCod_SPCoc(p_j2k,p_j2k->m_current_tile_number,0,l_current_data,&l_remaining_size,p_manager)) {
3113                 opj_event_msg_v2(p_manager, EVT_ERROR, "Error writting COD marker\n");
3114                 return OPJ_FALSE;
3115         }
3116
3117         if (l_remaining_size != 0) {
3118                 opj_event_msg_v2(p_manager, EVT_ERROR, "Error writting COD marker\n");
3119                 return OPJ_FALSE;
3120         }
3121
3122         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) {
3123                 return OPJ_FALSE;
3124         }
3125
3126         return OPJ_TRUE;
3127 }
3128
3129 static void j2k_read_cod(opj_j2k_t *j2k) {
3130         int len, pos;
3131   OPJ_UINT32 i;
3132         
3133         opj_cio_t *cio = j2k->cio;
3134         opj_cp_t *cp = j2k->cp;
3135         opj_tcp_t *tcp = j2k->state == J2K_STATE_TPH ? &cp->tcps[j2k->curtileno] : j2k->default_tcp;
3136         opj_image_t *image = j2k->image;
3137         
3138         len = cio_read(cio, 2);                         /* Lcod */
3139         tcp->csty = cio_read(cio, 1);           /* Scod */
3140         tcp->prg = (OPJ_PROG_ORDER)cio_read(cio, 1);            /* SGcod (A) */
3141         tcp->numlayers = cio_read(cio, 2);      /* SGcod (B) */
3142         tcp->mct = cio_read(cio, 1);            /* SGcod (C) */
3143         
3144         pos = cio_tell(cio);
3145         for (i = 0; i < image->numcomps; i++) {
3146                 tcp->tccps[i].csty = tcp->csty & J2K_CP_CSTY_PRT;
3147                 cio_seek(cio, pos);
3148                 j2k_read_cox(j2k, i);
3149         }
3150
3151         /* Index */
3152         if (j2k->cstr_info) {
3153                 opj_codestream_info_t *cstr_info = j2k->cstr_info;
3154                 cstr_info->prog = tcp->prg;
3155                 cstr_info->numlayers = tcp->numlayers;
3156                 cstr_info->numdecompos = (int*) opj_malloc(image->numcomps * sizeof(int));
3157                 for (i = 0; i < image->numcomps; i++) {
3158                         cstr_info->numdecompos[i] = tcp->tccps[i].numresolutions - 1;
3159                 }
3160         }
3161 }
3162
3163 /**
3164  * Reads a COD marker (Coding Styke defaults)
3165  * @param       p_header_data   the data contained in the COD box.
3166  * @param       p_j2k                   the jpeg2000 codec.
3167  * @param       p_header_size   the size of the data contained in the COD marker.
3168  * @param       p_manager               the user event manager.
3169 */
3170 opj_bool j2k_read_cod_v2 (
3171                                         opj_j2k_v2_t *p_j2k,
3172                                         OPJ_BYTE * p_header_data,
3173                                         OPJ_UINT32 p_header_size,
3174                                         struct opj_event_mgr * p_manager
3175                                         )
3176 {
3177         /* loop */
3178         OPJ_UINT32 i;
3179         OPJ_UINT32 l_tmp;
3180         opj_cp_v2_t *l_cp = 00;
3181         opj_tcp_v2_t *l_tcp = 00;
3182         opj_image_t *l_image = 00;
3183
3184         /* preconditions */
3185         assert(p_header_data != 00);
3186         assert(p_j2k != 00);
3187         assert(p_manager != 00);
3188
3189         l_image = p_j2k->m_private_image;
3190         l_cp = &(p_j2k->m_cp);
3191
3192         /* If we are in the first tile-part header of the current tile */
3193         l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ?
3194                                 &l_cp->tcps[p_j2k->m_current_tile_number] :
3195                                 p_j2k->m_specific_param.m_decoder.m_default_tcp;
3196
3197         /* Make sure room is sufficient */
3198         if (p_header_size < 5) {
3199                 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading COD marker\n");
3200                 return OPJ_FALSE;
3201         }
3202
3203         opj_read_bytes(p_header_data,&l_tcp->csty,1);           /* Scod */
3204         ++p_header_data;
3205         opj_read_bytes(p_header_data,&l_tmp,1);                         /* SGcod (A) */
3206         ++p_header_data;
3207         l_tcp->prg = (OPJ_PROG_ORDER) l_tmp;
3208         opj_read_bytes(p_header_data,&l_tcp->numlayers,2);      /* SGcod (B) */
3209         p_header_data+=2;
3210
3211         /* If user didn't set a number layer to decode take the max specify in the codestream. */
3212         if      (l_cp->m_specific_param.m_dec.m_layer) {
3213                 l_tcp->num_layers_to_decode = l_cp->m_specific_param.m_dec.m_layer;
3214         }
3215         else {
3216                 l_tcp->num_layers_to_decode = l_tcp->numlayers;
3217         }
3218
3219         opj_read_bytes(p_header_data,&l_tcp->mct,1);            /* SGcod (C) */
3220         ++p_header_data;
3221
3222         p_header_size -= 5;
3223         for     (i = 0; i < l_image->numcomps; ++i) {
3224                 l_tcp->tccps[i].csty = l_tcp->csty & J2K_CCP_CSTY_PRT;
3225         }
3226
3227         if (! j2k_read_SPCod_SPCoc(p_j2k,0,p_header_data,&p_header_size,p_manager)) {
3228                 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading COD marker\n");
3229                 return OPJ_FALSE;
3230         }
3231
3232         if (p_header_size != 0) {
3233                 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading COD marker\n");
3234                 return OPJ_FALSE;
3235         }
3236
3237         /* Apply the coding style to other components of the current tile or the m_default_tcp*/
3238         j2k_copy_tile_component_parameters(p_j2k);
3239
3240         /* Index */
3241 #ifdef WIP_REMOVE_MSD
3242         if (p_j2k->cstr_info) {
3243                 /*opj_codestream_info_t *l_cstr_info = p_j2k->cstr_info;*/
3244                 p_j2k->cstr_info->prog = l_tcp->prg;
3245                 p_j2k->cstr_info->numlayers = l_tcp->numlayers;
3246                 p_j2k->cstr_info->numdecompos = (OPJ_INT32*) opj_malloc(l_image->numcomps * sizeof(OPJ_UINT32));
3247                 for     (i = 0; i < l_image->numcomps; ++i) {
3248                         p_j2k->cstr_info->numdecompos[i] = l_tcp->tccps[i].numresolutions - 1;
3249                 }
3250         }
3251 #endif
3252
3253         return OPJ_TRUE;
3254 }
3255
3256 static void j2k_write_coc(opj_j2k_t *j2k, int compno) {
3257         int lenp, len;
3258
3259         opj_cp_t *cp = j2k->cp;
3260         opj_tcp_t *tcp = &cp->tcps[j2k->curtileno];
3261         opj_image_t *image = j2k->image;
3262         opj_cio_t *cio = j2k->cio;
3263         
3264         cio_write(cio, J2K_MS_COC, 2);  /* COC */
3265         lenp = cio_tell(cio);
3266         cio_skip(cio, 2);
3267         cio_write(cio, compno, image->numcomps <= 256 ? 1 : 2); /* Ccoc */
3268         cio_write(cio, tcp->tccps[compno].csty, 1);     /* Scoc */
3269         j2k_write_cox(j2k, compno);
3270         len = cio_tell(cio) - lenp;
3271         cio_seek(cio, lenp);
3272         cio_write(cio, len, 2);                 /* Lcoc */
3273         cio_seek(cio, lenp + len);
3274 }
3275
3276 /**
3277  * Writes the COC marker (Coding style component)
3278  *
3279  * @param       p_comp_no               the index of the component to output.
3280  * @param       p_stream                                the stream to write data to.
3281  * @param       p_j2k                           J2K codec.
3282  * @param       p_manager               the user event manager.
3283 */
3284 opj_bool j2k_write_coc_v2(      opj_j2k_v2_t *p_j2k,
3285                                                         OPJ_UINT32 p_comp_no,
3286                                                         struct opj_stream_private *p_stream,
3287                                                         struct opj_event_mgr * p_manager )
3288 {
3289         OPJ_UINT32 l_coc_size,l_remaining_size;
3290         OPJ_UINT32 l_comp_room;
3291
3292         /* preconditions */
3293         assert(p_j2k != 00);
3294         assert(p_manager != 00);
3295         assert(p_stream != 00);
3296
3297         l_comp_room = (p_j2k->m_private_image->numcomps <= 256) ? 1 : 2;
3298
3299         l_coc_size = 5 + l_comp_room + j2k_get_SPCod_SPCoc_size(p_j2k,p_j2k->m_current_tile_number,p_comp_no);
3300
3301         if (l_coc_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
3302                 p_j2k->m_specific_param.m_encoder.m_header_tile_data
3303                         = (OPJ_BYTE*)opj_realloc(
3304                                 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
3305                                 l_coc_size);
3306                 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
3307                         return OPJ_FALSE;
3308                 }
3309
3310                 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_coc_size;
3311         }
3312
3313         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);
3314
3315         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) {
3316                 return OPJ_FALSE;
3317         }
3318
3319         return OPJ_TRUE;
3320 }
3321
3322 /**
3323  * Writes the COC marker (Coding style component)
3324  *
3325  * @param       p_comp_no               the index of the component to output.
3326  * @param       p_stream                                the stream to write data to.
3327  * @param       p_j2k                           J2K codec.
3328  * @param       p_manager               the user event manager.
3329 */
3330 void j2k_write_coc_in_memory(   opj_j2k_v2_t *p_j2k,
3331                                                                 OPJ_UINT32 p_comp_no,
3332                                                                 OPJ_BYTE * p_data,
3333                                                                 OPJ_UINT32 * p_data_written,
3334                                                                 struct opj_event_mgr * p_manager )
3335 {
3336         opj_cp_v2_t *l_cp = 00;
3337         opj_tcp_v2_t *l_tcp = 00;
3338         OPJ_UINT32 l_coc_size,l_remaining_size;
3339         OPJ_BYTE * l_current_data = 00;
3340         opj_image_t *l_image = 00;
3341         OPJ_UINT32 l_comp_room;
3342
3343         /* preconditions */
3344         assert(p_j2k != 00);
3345         assert(p_manager != 00);
3346
3347         l_cp = &(p_j2k->m_cp);
3348         l_tcp = &l_cp->tcps[p_j2k->m_current_tile_number];
3349         l_image = p_j2k->m_private_image;
3350         l_comp_room = (l_image->numcomps <= 256) ? 1 : 2;
3351
3352         l_coc_size = 5 + l_comp_room + j2k_get_SPCod_SPCoc_size(p_j2k,p_j2k->m_current_tile_number,p_comp_no);
3353         l_remaining_size = l_coc_size;
3354
3355         l_current_data = p_data;
3356
3357         opj_write_bytes(l_current_data,J2K_MS_COC,2);                           /* COC */
3358         l_current_data += 2;
3359
3360         opj_write_bytes(l_current_data,l_coc_size-2,2);                         /* L_COC */
3361         l_current_data += 2;
3362
3363         opj_write_bytes(l_current_data,p_comp_no, l_comp_room);         /* Ccoc */
3364         l_current_data+=l_comp_room;
3365
3366         opj_write_bytes(l_current_data, l_tcp->tccps[p_comp_no].csty, 1);               /* Scoc */
3367         ++l_current_data;
3368
3369         l_remaining_size -= (5 + l_comp_room);
3370         j2k_write_SPCod_SPCoc(p_j2k,p_j2k->m_current_tile_number,0,l_current_data,&l_remaining_size,p_manager);
3371         * p_data_written = l_coc_size;
3372 }
3373
3374 /**
3375  * Gets the maximum size taken by a coc.
3376  *
3377  * @param       p_j2k   the jpeg2000 codec to use.
3378  */
3379 OPJ_UINT32 j2k_get_max_coc_size(opj_j2k_v2_t *p_j2k)
3380 {
3381         OPJ_UINT32 i,j;
3382         OPJ_UINT32 l_nb_comp;
3383         OPJ_UINT32 l_nb_tiles;
3384         OPJ_UINT32 l_max = 0;
3385
3386         /* preconditions */
3387
3388         l_nb_tiles = p_j2k->m_cp.tw * p_j2k->m_cp.th ;
3389         l_nb_comp = p_j2k->m_private_image->numcomps;
3390
3391         for (i=0;i<l_nb_tiles;++i) {
3392                 for (j=0;j<l_nb_comp;++j) {
3393                         l_max = uint_max(l_max,j2k_get_SPCod_SPCoc_size(p_j2k,i,j));
3394                 }
3395         }
3396
3397         return 6 + l_max;
3398 }
3399
3400 static void j2k_read_coc(opj_j2k_t *j2k) {
3401         int len, compno;
3402
3403         opj_cp_t *cp = j2k->cp;
3404         opj_tcp_t *tcp = j2k->state == J2K_STATE_TPH ? &cp->tcps[j2k->curtileno] : j2k->default_tcp;
3405         opj_image_t *image = j2k->image;
3406         opj_cio_t *cio = j2k->cio;
3407         
3408         len = cio_read(cio, 2);         /* Lcoc */
3409         compno = cio_read(cio, image->numcomps <= 256 ? 1 : 2); /* Ccoc */
3410         tcp->tccps[compno].csty = cio_read(cio, 1);     /* Scoc */
3411         j2k_read_cox(j2k, compno);
3412 }
3413
3414 /**
3415  * Reads a COC marker (Coding Style Component)
3416  * @param       p_header_data   the data contained in the COC box.
3417  * @param       p_j2k                   the jpeg2000 codec.
3418  * @param       p_header_size   the size of the data contained in the COC marker.
3419  * @param       p_manager               the user event manager.
3420 */
3421 opj_bool j2k_read_coc_v2 (
3422                                         opj_j2k_v2_t *p_j2k,
3423                                         OPJ_BYTE * p_header_data,
3424                                         OPJ_UINT32 p_header_size,
3425                                         struct opj_event_mgr * p_manager
3426                                         )
3427 {
3428         opj_cp_v2_t *l_cp = NULL;
3429         opj_tcp_v2_t *l_tcp = NULL;
3430         opj_image_t *l_image = NULL;
3431         OPJ_UINT32 l_comp_room;
3432         OPJ_UINT32 l_comp_no;
3433
3434         /* preconditions */
3435         assert(p_header_data != 00);
3436         assert(p_j2k != 00);
3437         assert(p_manager != 00);
3438
3439         l_cp = &(p_j2k->m_cp);
3440         l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH ) ? /*FIXME J2K_DEC_STATE_TPH*/
3441                                 &l_cp->tcps[p_j2k->m_current_tile_number] :
3442                                 p_j2k->m_specific_param.m_decoder.m_default_tcp;
3443         l_image = p_j2k->m_private_image;
3444
3445         l_comp_room = l_image->numcomps <= 256 ? 1 : 2;
3446
3447         /* make sure room is sufficient*/
3448         if (p_header_size < l_comp_room + 1) {
3449                 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading COC marker\n");
3450                 return OPJ_FALSE;
3451         }
3452         p_header_size -= l_comp_room + 1;
3453
3454         opj_read_bytes(p_header_data,&l_comp_no,l_comp_room);                   /* Ccoc */
3455         p_header_data += l_comp_room;
3456         if (l_comp_no >= l_image->numcomps) {
3457                 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading COC marker (bad number of components)\n");
3458                 return OPJ_FALSE;
3459         }
3460
3461         opj_read_bytes(p_header_data,&l_tcp->tccps[l_comp_no].csty,1);                  /* Scoc */
3462         ++p_header_data ;
3463
3464         if (! j2k_read_SPCod_SPCoc(p_j2k,l_comp_no,p_header_data,&p_header_size,p_manager)) {
3465                 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading COC marker\n");
3466                 return OPJ_FALSE;
3467         }
3468
3469         if (p_header_size != 0) {
3470                 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading COC marker\n");
3471                 return OPJ_FALSE;
3472         }
3473         return OPJ_TRUE;
3474 }
3475
3476 static void j2k_write_qcx(opj_j2k_t *j2k, int compno) {
3477         int bandno, numbands;
3478         int expn, mant;
3479         
3480         opj_cp_t *cp = j2k->cp;
3481         opj_tcp_t *tcp = &cp->tcps[j2k->curtileno];
3482         opj_tccp_t *tccp = &tcp->tccps[compno];
3483         opj_cio_t *cio = j2k->cio;
3484         
3485         cio_write(cio, tccp->qntsty + (tccp->numgbits << 5), 1);        /* Sqcx */
3486         numbands = tccp->qntsty == J2K_CCP_QNTSTY_SIQNT ? 1 : tccp->numresolutions * 3 - 2;
3487         
3488         for (bandno = 0; bandno < numbands; bandno++) {
3489                 expn = tccp->stepsizes[bandno].expn;
3490                 mant = tccp->stepsizes[bandno].mant;
3491                 
3492                 if (tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) {
3493                         cio_write(cio, expn << 3, 1);   /* SPqcx_i */
3494                 } else {
3495                         cio_write(cio, (expn << 11) + mant, 2); /* SPqcx_i */
3496                 }
3497         }
3498 }
3499
3500 static void j2k_read_qcx(opj_j2k_t *j2k, int compno, int len) {
3501         int tmp;
3502         int bandno, numbands;
3503
3504         opj_cp_t *cp = j2k->cp;
3505         opj_tcp_t *tcp = j2k->state == J2K_STATE_TPH ? &cp->tcps[j2k->curtileno] : j2k->default_tcp;
3506         opj_tccp_t *tccp = &tcp->tccps[compno];
3507         opj_cio_t *cio = j2k->cio;
3508
3509         tmp = cio_read(cio, 1);         /* Sqcx */
3510         tccp->qntsty = tmp & 0x1f;
3511         tccp->numgbits = tmp >> 5;
3512         numbands = (tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) ? 
3513                 1 : ((tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) ? len - 1 : (len - 1) / 2);
3514
3515 #ifdef USE_JPWL
3516         if (j2k->cp->correct) {
3517
3518                 /* if JPWL is on, we check whether there are too many subbands */
3519                 if ((numbands < 0) || (numbands >= J2K_MAXBANDS)) {
3520                         opj_event_msg(j2k->cinfo, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
3521                                 "JPWL: bad number of subbands in Sqcx (%d)\n",
3522                                 numbands);
3523                         if (!JPWL_ASSUME) {
3524                                 opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
3525                                 return;
3526                         }
3527                         /* we try to correct */
3528                         numbands = 1;
3529                         opj_event_msg(j2k->cinfo, EVT_WARNING, "- trying to adjust them\n"
3530                                 "- setting number of bands to %d => HYPOTHESIS!!!\n",
3531                                 numbands);
3532                 };
3533
3534         };
3535
3536 #else
3537         /* We check whether there are too many subbands */
3538         if ((numbands < 0) || (numbands >= J2K_MAXBANDS)) {
3539                 opj_event_msg(j2k->cinfo, EVT_WARNING ,
3540                                         "bad number of subbands in Sqcx (%d) regarding to J2K_MAXBANDS (%d) \n"
3541                                     "- limiting number of bands to J2K_MAXBANDS and try to move to the next markers\n", numbands, J2K_MAXBANDS);
3542         }
3543
3544 #endif /* USE_JPWL */
3545
3546         for (bandno = 0; bandno < numbands; bandno++) {
3547                 int expn, mant;
3548                 if (tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) {
3549                         expn = cio_read(cio, 1) >> 3;   /* SPqcx_i */
3550                         mant = 0;
3551                 } else {
3552                         tmp = cio_read(cio, 2); /* SPqcx_i */
3553                         expn = tmp >> 11;
3554                         mant = tmp & 0x7ff;
3555                 }
3556                 if (bandno < J2K_MAXBANDS){
3557                         tccp->stepsizes[bandno].expn = expn;
3558                         tccp->stepsizes[bandno].mant = mant;
3559                 }
3560         }
3561         
3562         /* Add Antonin : if scalar_derived -> compute other stepsizes */
3563         if (tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) {
3564                 for (bandno = 1; bandno < J2K_MAXBANDS; bandno++) {
3565                         tccp->stepsizes[bandno].expn = 
3566                                 ((tccp->stepsizes[0].expn) - ((bandno - 1) / 3) > 0) ? 
3567                                         (tccp->stepsizes[0].expn) - ((bandno - 1) / 3) : 0;
3568                         tccp->stepsizes[bandno].mant = tccp->stepsizes[0].mant;
3569                 }
3570         }
3571         /* ddA */
3572 }
3573
3574 static void j2k_write_qcd(opj_j2k_t *j2k) {
3575         int lenp, len;
3576
3577         opj_cio_t *cio = j2k->cio;
3578         
3579         cio_write(cio, J2K_MS_QCD, 2);  /* QCD */
3580         lenp = cio_tell(cio);
3581         cio_skip(cio, 2);
3582         j2k_write_qcx(j2k, 0);
3583         len = cio_tell(cio) - lenp;
3584         cio_seek(cio, lenp);
3585         cio_write(cio, len, 2);                 /* Lqcd */
3586         cio_seek(cio, lenp + len);
3587
3588         if(j2k->cstr_info)
3589           j2k_add_mhmarker(j2k->cstr_info, J2K_MS_QCD, lenp, len);
3590 }
3591
3592 /**
3593  * Writes the QCD marker (quantization default)
3594  *
3595  * @param       p_comp_number   the index of the component to output.
3596  * @param       p_stream                                the stream to write data to.
3597  * @param       p_j2k                           J2K codec.
3598  * @param       p_manager               the user event manager.
3599 */
3600 opj_bool j2k_write_qcd_v2(      opj_j2k_v2_t *p_j2k,
3601                                                         struct opj_stream_private *p_stream,
3602                                                         struct opj_event_mgr * p_manager )
3603 {
3604         opj_cp_v2_t *l_cp = 00;
3605         opj_tcp_v2_t *l_tcp = 00;
3606         OPJ_UINT32 l_qcd_size,l_remaining_size;
3607         OPJ_BYTE * l_current_data = 00;
3608
3609         /* preconditions */
3610         assert(p_j2k != 00);
3611         assert(p_manager != 00);
3612         assert(p_stream != 00);
3613
3614         l_cp = &(p_j2k->m_cp);
3615         l_tcp = &l_cp->tcps[p_j2k->m_current_tile_number];
3616         l_qcd_size = 4 + j2k_get_SQcd_SQcc_size(p_j2k,p_j2k->m_current_tile_number,0);
3617         l_remaining_size = l_qcd_size;
3618
3619         if (l_qcd_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
3620                 p_j2k->m_specific_param.m_encoder.m_header_tile_data
3621                         = (OPJ_BYTE*)opj_realloc(
3622                                 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
3623                                 l_qcd_size);
3624
3625                 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
3626                         return OPJ_FALSE;
3627                 }
3628
3629                 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_qcd_size;
3630         }
3631
3632         l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
3633
3634         opj_write_bytes(l_current_data,J2K_MS_QCD,2);           /* QCD */
3635         l_current_data += 2;
3636
3637         opj_write_bytes(l_current_data,l_qcd_size-2,2);         /* L_QCD */
3638         l_current_data += 2;
3639
3640         l_remaining_size -= 4;
3641
3642         if (! j2k_write_SQcd_SQcc(p_j2k,p_j2k->m_current_tile_number,0,l_current_data,&l_remaining_size,p_manager)) {
3643                 opj_event_msg_v2(p_manager, EVT_ERROR, "Error writting QCD marker\n");
3644                 return OPJ_FALSE;
3645         }
3646
3647         if (l_remaining_size != 0) {
3648                 opj_event_msg_v2(p_manager, EVT_ERROR, "Error writting QCD marker\n");
3649                 return OPJ_FALSE;
3650         }
3651
3652         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) {
3653                 return OPJ_FALSE;
3654         }
3655
3656         return OPJ_TRUE;
3657 }
3658
3659
3660 static void j2k_read_qcd(opj_j2k_t *j2k) {
3661         int len, pos;
3662   OPJ_UINT32 i;
3663
3664         opj_cio_t *cio = j2k->cio;
3665         opj_image_t *image = j2k->image;
3666         
3667         len = cio_read(cio, 2);         /* Lqcd */
3668         pos = cio_tell(cio);
3669         for (i = 0; i < image->numcomps; i++) {
3670                 cio_seek(cio, pos);
3671                 j2k_read_qcx(j2k, i, len - 2);
3672         }
3673 }
3674
3675 /**
3676  * Reads a QCD marker (Quantization defaults)
3677  * @param       p_header_data   the data contained in the QCD box.
3678  * @param       p_j2k                   the jpeg2000 codec.
3679  * @param       p_header_size   the size of the data contained in the QCD marker.
3680  * @param       p_manager               the user event manager.
3681 */
3682 opj_bool j2k_read_qcd_v2 (
3683                                     opj_j2k_v2_t *p_j2k,
3684                                         OPJ_BYTE * p_header_data,
3685                                         OPJ_UINT32 p_header_size,
3686                                         struct opj_event_mgr * p_manager
3687                                         )
3688 {
3689         /* preconditions */
3690         assert(p_header_data != 00);
3691         assert(p_j2k != 00);
3692         assert(p_manager != 00);
3693
3694         if (! j2k_read_SQcd_SQcc(p_j2k,0,p_header_data,&p_header_size,p_manager)) {
3695                 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading QCD marker\n");
3696                 return OPJ_FALSE;
3697         }
3698
3699         if (p_header_size != 0) {
3700                 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading QCD marker\n");
3701                 return OPJ_FALSE;
3702         }
3703
3704         /* Apply the quantization parameters to other components of the current tile or the m_default_tcp */
3705         j2k_copy_tile_quantization_parameters(p_j2k);
3706
3707         return OPJ_TRUE;
3708 }
3709
3710 static void j2k_write_qcc(opj_j2k_t *j2k, int compno) {
3711         int lenp, len;
3712
3713         opj_cio_t *cio = j2k->cio;
3714         
3715         cio_write(cio, J2K_MS_QCC, 2);  /* QCC */
3716         lenp = cio_tell(cio);
3717         cio_skip(cio, 2);
3718         cio_write(cio, compno, j2k->image->numcomps <= 256 ? 1 : 2);    /* Cqcc */
3719         j2k_write_qcx(j2k, compno);
3720         len = cio_tell(cio) - lenp;
3721         cio_seek(cio, lenp);
3722         cio_write(cio, len, 2);                 /* Lqcc */
3723         cio_seek(cio, lenp + len);
3724 }
3725
3726 /**
3727  * Writes the QCC marker (quantization component)
3728  *
3729  * @param       p_comp_no       the index of the component to output.
3730  * @param       p_stream                                the stream to write data to.
3731  * @param       p_j2k                           J2K codec.
3732  * @param       p_manager               the user event manager.
3733 */
3734 opj_bool j2k_write_qcc_v2(      opj_j2k_v2_t *p_j2k,
3735                                                         OPJ_UINT32 p_comp_no,
3736                                                         struct opj_stream_private *p_stream,
3737                                                         struct opj_event_mgr * p_manager )
3738 {
3739         OPJ_UINT32 l_qcc_size,l_remaining_size;
3740
3741         /* preconditions */
3742         assert(p_j2k != 00);
3743         assert(p_manager != 00);
3744         assert(p_stream != 00);
3745
3746         l_qcc_size = 6 + j2k_get_SQcd_SQcc_size(p_j2k,p_j2k->m_current_tile_number,p_comp_no);
3747         l_remaining_size = l_qcc_size;
3748
3749         if (l_qcc_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
3750                 p_j2k->m_specific_param.m_encoder.m_header_tile_data
3751                         = (OPJ_BYTE*)opj_realloc(
3752                                 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
3753                                 l_qcc_size);
3754                 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
3755                         return OPJ_FALSE;
3756                 }
3757
3758                 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_qcc_size;
3759         }
3760
3761         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);
3762
3763         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) {
3764                 return OPJ_FALSE;
3765         }
3766
3767         return OPJ_TRUE;
3768 }
3769
3770 /**
3771  * Writes the QCC marker (quantization component)
3772  *
3773  * @param       p_comp_no       the index of the component to output.
3774  * @param       p_stream                                the stream to write data to.
3775  * @param       p_j2k                           J2K codec.
3776  * @param       p_manager               the user event manager.
3777 */
3778 void j2k_write_qcc_in_memory(   opj_j2k_v2_t *p_j2k,
3779                                                                 OPJ_UINT32 p_comp_no,
3780                                                                 OPJ_BYTE * p_data,
3781                                                                 OPJ_UINT32 * p_data_written,
3782                                                                 struct opj_event_mgr * p_manager )
3783 {
3784         OPJ_UINT32 l_qcc_size,l_remaining_size;
3785         OPJ_BYTE * l_current_data = 00;
3786
3787         /* preconditions */
3788         assert(p_j2k != 00);
3789         assert(p_manager != 00);
3790
3791         l_qcc_size = 6 + j2k_get_SQcd_SQcc_size(p_j2k,p_j2k->m_current_tile_number,p_comp_no);
3792         l_remaining_size = l_qcc_size;
3793
3794         l_current_data = p_data;
3795
3796         opj_write_bytes(l_current_data,J2K_MS_QCC,2);           /* QCC */
3797         l_current_data += 2;
3798
3799         if (p_j2k->m_private_image->numcomps <= 256) {
3800                 --l_qcc_size;
3801
3802                 opj_write_bytes(l_current_data,l_qcc_size-2,2);         /* L_QCC */
3803                 l_current_data += 2;
3804
3805                 opj_write_bytes(l_current_data, p_comp_no, 1);  /* Cqcc */
3806                 ++l_current_data;
3807
3808                 /* in the case only one byte is sufficient the last byte allocated is useless -> still do -6 for available */
3809                 l_remaining_size -= 6;
3810         }
3811         else {
3812                 opj_write_bytes(l_current_data,l_qcc_size-2,2);         /* L_QCC */
3813                 l_current_data += 2;
3814
3815                 opj_write_bytes(l_current_data, p_comp_no, 2);  /* Cqcc */
3816                 l_current_data+=2;
3817
3818                 l_remaining_size -= 6;
3819         }
3820
3821         j2k_write_SQcd_SQcc(p_j2k,p_j2k->m_current_tile_number,p_comp_no,l_current_data,&l_remaining_size,p_manager);
3822
3823         *p_data_written = l_qcc_size;
3824 }
3825
3826 /**
3827  * Gets the maximum size taken by a qcc.
3828  */
3829 OPJ_UINT32 j2k_get_max_qcc_size (opj_j2k_v2_t *p_j2k)
3830 {
3831         return j2k_get_max_coc_size(p_j2k);
3832 }
3833
3834 static void j2k_read_qcc(opj_j2k_t *j2k) {
3835         int len, compno;
3836         int numcomp = j2k->image->numcomps;
3837         opj_cio_t *cio = j2k->cio;
3838
3839         len = cio_read(cio, 2); /* Lqcc */
3840         compno = cio_read(cio, numcomp <= 256 ? 1 : 2); /* Cqcc */
3841
3842 #ifdef USE_JPWL
3843         if (j2k->cp->correct) {
3844
3845                 static int backup_compno = 0;
3846
3847                 /* compno is negative or larger than the number of components!!! */
3848                 if ((compno < 0) || (compno >= numcomp)) {
3849                         opj_event_msg(j2k->cinfo, EVT_ERROR,
3850                                 "JPWL: bad component number in QCC (%d out of a maximum of %d)\n",
3851                                 compno, numcomp);
3852                         if (!JPWL_ASSUME) {
3853                                 opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
3854                                 return;
3855                         }
3856                         /* we try to correct */
3857                         compno = backup_compno % numcomp;
3858                         opj_event_msg(j2k->cinfo, EVT_WARNING, "- trying to adjust this\n"
3859                                 "- setting component number to %d\n",
3860                                 compno);
3861                 }
3862
3863                 /* keep your private count of tiles */
3864                 backup_compno++;
3865         };
3866 #endif /* USE_JPWL */
3867
3868         j2k_read_qcx(j2k, compno, len - 2 - (numcomp <= 256 ? 1 : 2));
3869 }
3870
3871 /**
3872  * Reads a QCC marker (Quantization component)
3873  * @param       p_header_data   the data contained in the QCC box.
3874  * @param       p_j2k                   the jpeg2000 codec.
3875  * @param       p_header_size   the size of the data contained in the QCC marker.
3876  * @param       p_manager               the user event manager.
3877 */
3878 opj_bool j2k_read_qcc_v2(       opj_j2k_v2_t *p_j2k,
3879                                                         OPJ_BYTE * p_header_data,
3880                                                         OPJ_UINT32 p_header_size,
3881                                                         struct opj_event_mgr * p_manager)
3882 {
3883         OPJ_UINT32 l_num_comp,l_comp_no;
3884
3885         /* preconditions */
3886         assert(p_header_data != 00);
3887         assert(p_j2k != 00);
3888         assert(p_manager != 00);
3889
3890         l_num_comp = p_j2k->m_private_image->numcomps;
3891
3892         if (l_num_comp <= 256) {
3893                 if (p_header_size < 1) {
3894                         opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading QCC marker\n");
3895                         return OPJ_FALSE;
3896                 }
3897                 opj_read_bytes(p_header_data,&l_comp_no,1);
3898                 ++p_header_data;
3899                 --p_header_size;
3900         }
3901         else {
3902                 if (p_header_size < 2) {
3903                         opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading QCC marker\n");
3904                         return OPJ_FALSE;
3905                 }
3906                 opj_read_bytes(p_header_data,&l_comp_no,2);
3907                 p_header_data+=2;
3908                 p_header_size-=2;
3909         }
3910
3911 #ifdef USE_JPWL
3912         if (p_j2k->m_cp.correct) {
3913
3914                 static OPJ_UINT32 backup_compno = 0;
3915
3916                 /* compno is negative or larger than the number of components!!! */
3917                 if (/*(l_comp_no < 0) ||*/ (l_comp_no >= l_num_comp)) {
3918                         opj_event_msg_v2(p_manager, EVT_ERROR,
3919                                 "JPWL: bad component number in QCC (%d out of a maximum of %d)\n",
3920                                 l_comp_no, l_num_comp);
3921                         if (!JPWL_ASSUME) {
3922                                 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
3923                                 return OPJ_FALSE;
3924                         }
3925                         /* we try to correct */
3926                         l_comp_no = backup_compno % l_num_comp;
3927                         opj_event_msg_v2(p_manager, EVT_WARNING, "- trying to adjust this\n"
3928                                 "- setting component number to %d\n",
3929                                 l_comp_no);
3930                 }
3931
3932                 /* keep your private count of tiles */
3933                 backup_compno++;
3934         };
3935 #endif /* USE_JPWL */
3936
3937         if (! j2k_read_SQcd_SQcc(p_j2k,l_comp_no,p_header_data,&p_header_size,p_manager)) {
3938                 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading QCC marker\n");
3939                 return OPJ_FALSE;
3940         }
3941
3942         if (p_header_size != 0) {
3943                 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading QCC marker\n");
3944                 return OPJ_FALSE;
3945         }
3946
3947         return OPJ_TRUE;
3948 }
3949
3950
3951 static void j2k_write_poc(opj_j2k_t *j2k) {
3952         int len, numpchgs, i;
3953
3954         int numcomps = j2k->image->numcomps;
3955         
3956         opj_cp_t *cp = j2k->cp;
3957         opj_tcp_t *tcp = &cp->tcps[j2k->curtileno];
3958         opj_tccp_t *tccp = &tcp->tccps[0];
3959         opj_cio_t *cio = j2k->cio;
3960
3961         numpchgs = 1 + tcp->numpocs;
3962         cio_write(cio, J2K_MS_POC, 2);  /* POC  */
3963         len = 2 + (5 + 2 * (numcomps <= 256 ? 1 : 2)) * numpchgs;
3964         cio_write(cio, len, 2);         /* Lpoc */
3965         for (i = 0; i < numpchgs; i++) {
3966                 opj_poc_t *poc = &tcp->pocs[i];
3967                 cio_write(cio, poc->resno0, 1); /* RSpoc_i */
3968                 cio_write(cio, poc->compno0, (numcomps <= 256 ? 1 : 2));        /* CSpoc_i */
3969                 cio_write(cio, poc->layno1, 2); /* LYEpoc_i */
3970                 poc->layno1 = int_min(poc->layno1, tcp->numlayers);
3971                 cio_write(cio, poc->resno1, 1); /* REpoc_i */
3972                 poc->resno1 = int_min(poc->resno1, tccp->numresolutions);
3973                 cio_write(cio, poc->compno1, (numcomps <= 256 ? 1 : 2));        /* CEpoc_i */
3974                 poc->compno1 = int_min(poc->compno1, numcomps);
3975                 cio_write(cio, poc->prg, 1);    /* Ppoc_i */
3976         }
3977 }
3978
3979 /**
3980  * Writes the POC marker (Progression Order Change)
3981  * 
3982  * @param       p_stream                                the stream to write data to.
3983  * @param       p_j2k                           J2K codec.
3984  * @param       p_manager               the user event manager.
3985 */
3986 opj_bool j2k_write_poc_v2(      opj_j2k_v2_t *p_j2k,
3987                                                         struct opj_stream_private *p_stream,
3988                                                         struct opj_event_mgr * p_manager )
3989 {
3990         OPJ_UINT32 l_nb_comp;
3991         OPJ_UINT32 l_nb_poc;
3992         OPJ_UINT32 l_poc_size;
3993         OPJ_UINT32 l_written_size = 0;
3994         opj_tcp_v2_t *l_tcp = 00;
3995         opj_tccp_t *l_tccp = 00;
3996         OPJ_UINT32 l_poc_room;
3997
3998         // preconditions
3999         assert(p_j2k != 00);
4000         assert(p_manager != 00);
4001         assert(p_stream != 00);
4002
4003         l_tcp = &p_j2k->m_cp.tcps[p_j2k->m_current_tile_number];
4004         l_tccp = &l_tcp->tccps[0];
4005         l_nb_comp = p_j2k->m_private_image->numcomps;
4006         l_nb_poc = 1 + l_tcp->numpocs;
4007         
4008         if (l_nb_comp <= 256) {
4009                 l_poc_room = 1;
4010         }
4011         else {
4012                 l_poc_room = 2;
4013         }
4014         l_poc_size = 4 + (5 + 2 * l_poc_room) * l_nb_poc;
4015         
4016         if (l_poc_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
4017                 p_j2k->m_specific_param.m_encoder.m_header_tile_data 
4018                         = (OPJ_BYTE*)opj_realloc(
4019                                 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
4020                                 l_poc_size);
4021                 
4022                 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
4023                         return OPJ_FALSE;
4024                 }
4025
4026                 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_poc_size;
4027         }
4028
4029         j2k_write_poc_in_memory(p_j2k,p_j2k->m_specific_param.m_encoder.m_header_tile_data,&l_written_size,p_manager);
4030
4031         if (opj_stream_write_data(p_stream,p_j2k->m_specific_param.m_encoder.m_header_tile_data,l_poc_size,p_manager) != l_poc_size) {
4032                 return OPJ_FALSE;
4033         }
4034
4035         return OPJ_TRUE;
4036 }
4037
4038
4039 /**
4040  * Writes the POC marker (Progression Order Change)
4041  *
4042  * @param       p_stream                                the stream to write data to.
4043  * @param       p_j2k                           J2K codec.
4044  * @param       p_manager               the user event manager.
4045 */
4046 void j2k_write_poc_in_memory(   opj_j2k_v2_t *p_j2k,
4047                                                                 OPJ_BYTE * p_data,
4048                                                                 OPJ_UINT32 * p_data_written,
4049                                                                 struct opj_event_mgr * p_manager )
4050 {
4051         OPJ_UINT32 i;
4052         OPJ_BYTE * l_current_data = 00;
4053         OPJ_UINT32 l_nb_comp;
4054         OPJ_UINT32 l_nb_poc;
4055         OPJ_UINT32 l_poc_size;
4056         opj_image_t *l_image = 00;
4057         opj_tcp_v2_t *l_tcp = 00;
4058         opj_tccp_t *l_tccp = 00;
4059         opj_poc_t *l_current_poc = 00;
4060         OPJ_UINT32 l_poc_room;
4061
4062         /* preconditions */
4063         assert(p_j2k != 00);
4064         assert(p_manager != 00);
4065
4066         l_tcp = &p_j2k->m_cp.tcps[p_j2k->m_current_tile_number];
4067         l_tccp = &l_tcp->tccps[0];
4068         l_image = p_j2k->m_private_image;
4069         l_nb_comp = l_image->numcomps;
4070         l_nb_poc = 1 + l_tcp->numpocs;
4071
4072         if (l_nb_comp <= 256) {
4073                 l_poc_room = 1;
4074         }
4075         else {
4076                 l_poc_room = 2;
4077         }
4078
4079         l_poc_size = 4 + (5 + 2 * l_poc_room) * l_nb_poc;
4080
4081         l_current_data = p_data;
4082
4083         opj_write_bytes(l_current_data,J2K_MS_POC,2);                                   /* POC  */
4084         l_current_data += 2;
4085
4086         opj_write_bytes(l_current_data,l_poc_size-2,2);                                 /* Lpoc */
4087         l_current_data += 2;
4088
4089         l_current_poc =  l_tcp->pocs;
4090         for (i = 0; i < l_nb_poc; ++i) {
4091                 opj_write_bytes(l_current_data,l_current_poc->resno0,1);                                /* RSpoc_i */
4092                 ++l_current_data;
4093
4094                 opj_write_bytes(l_current_data,l_current_poc->compno0,l_poc_room);              /* CSpoc_i */
4095                 l_current_data+=l_poc_room;
4096
4097                 opj_write_bytes(l_current_data,l_current_poc->layno1,2);                                /* LYEpoc_i */
4098                 l_current_data+=2;
4099
4100                 opj_write_bytes(l_current_data,l_current_poc->resno1,1);                                /* REpoc_i */
4101                 ++l_current_data;
4102
4103                 opj_write_bytes(l_current_data,l_current_poc->compno1,l_poc_room);              /* CEpoc_i */
4104                 l_current_data+=l_poc_room;
4105
4106                 opj_write_bytes(l_current_data,l_current_poc->prg,1);                                   /* Ppoc_i */
4107                 ++l_current_data;
4108
4109                 /* change the value of the max layer according to the actual number of layers in the file, components and resolutions*/
4110                 l_current_poc->layno1 = int_min(l_current_poc->layno1, l_tcp->numlayers);
4111                 l_current_poc->resno1 = int_min(l_current_poc->resno1, l_tccp->numresolutions);
4112                 l_current_poc->compno1 = int_min(l_current_poc->compno1, l_nb_comp);
4113
4114                 ++l_current_poc;
4115         }
4116
4117         *p_data_written = l_poc_size;
4118 }
4119
4120 /**
4121  * Gets the maximum size taken by the writing of a POC.
4122  */
4123 OPJ_UINT32 j2k_get_max_poc_size(opj_j2k_v2_t *p_j2k)
4124 {
4125         opj_tcp_v2_t * l_tcp = 00;
4126         OPJ_UINT32 l_nb_tiles = 0;
4127         OPJ_UINT32 l_max_poc = 0;
4128         OPJ_UINT32 i;
4129
4130         l_tcp = p_j2k->m_cp.tcps;
4131         l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw;
4132
4133         for (i=0;i<l_nb_tiles;++i) {
4134                 l_max_poc = uint_max(l_max_poc,l_tcp->numpocs);
4135                 ++l_tcp;
4136         }
4137
4138         ++l_max_poc;
4139
4140         return 4 + 9 * l_max_poc;
4141 }
4142
4143 /**
4144  * Gets the maximum size taken by the toc headers of all the tile parts of any given tile.
4145  */
4146 OPJ_UINT32 j2k_get_max_toc_size (opj_j2k_v2_t *p_j2k)
4147 {
4148         OPJ_UINT32 i;
4149         OPJ_UINT32 l_nb_tiles;
4150         OPJ_UINT32 l_max = 0;
4151         opj_tcp_v2_t * l_tcp = 00;
4152
4153         l_tcp = p_j2k->m_cp.tcps;
4154         l_nb_tiles = p_j2k->m_cp.tw * p_j2k->m_cp.th ;
4155
4156         for (i=0;i<l_nb_tiles;++i) {
4157                 l_max = uint_max(l_max,l_tcp->m_nb_tile_parts);
4158
4159                 ++l_tcp;
4160         }
4161
4162         return 12 * l_max;
4163 }
4164
4165
4166 /**
4167  * Gets the maximum size taken by the headers of the SOT.
4168  *
4169  * @param       p_j2k   the jpeg2000 codec to use.
4170  */
4171 OPJ_UINT32 j2k_get_specific_header_sizes(opj_j2k_v2_t *p_j2k)
4172 {
4173         OPJ_UINT32 l_nb_bytes = 0;
4174         OPJ_UINT32 l_nb_comps;
4175         OPJ_UINT32 l_coc_bytes,l_qcc_bytes;
4176
4177         l_nb_comps = p_j2k->m_private_image->numcomps - 1;
4178         l_nb_bytes += j2k_get_max_toc_size(p_j2k);
4179
4180         if (p_j2k->m_cp.m_specific_param.m_enc.m_cinema == 0) {
4181                 l_coc_bytes = j2k_get_max_coc_size(p_j2k);
4182                 l_nb_bytes += l_nb_comps * l_coc_bytes;
4183
4184                 l_qcc_bytes = j2k_get_max_qcc_size(p_j2k);
4185                 l_nb_bytes += l_nb_comps * l_qcc_bytes;
4186         }
4187
4188         l_nb_bytes += j2k_get_max_poc_size(p_j2k);
4189
4190         /*** DEVELOPER CORNER, Add room for your headers ***/
4191
4192         return l_nb_bytes;
4193 }
4194
4195 static void j2k_read_poc(opj_j2k_t *j2k) {
4196         int len, numpchgs, i, old_poc;
4197
4198         int numcomps = j2k->image->numcomps;
4199         
4200         opj_cp_t *cp = j2k->cp;
4201         opj_tcp_t *tcp = j2k->state == J2K_STATE_TPH ? &cp->tcps[j2k->curtileno] : j2k->default_tcp;
4202         opj_cio_t *cio = j2k->cio;
4203         
4204         old_poc = tcp->POC ? tcp->numpocs + 1 : 0;
4205         tcp->POC = 1;
4206         len = cio_read(cio, 2);         /* Lpoc */
4207         numpchgs = (len - 2) / (5 + 2 * (numcomps <= 256 ? 1 : 2));
4208         
4209         for (i = old_poc; i < numpchgs + old_poc; i++) {
4210                 opj_poc_t *poc;
4211                 poc = &tcp->pocs[i];
4212                 poc->resno0 = cio_read(cio, 1); /* RSpoc_i */
4213                 poc->compno0 = cio_read(cio, numcomps <= 256 ? 1 : 2);  /* CSpoc_i */
4214                 poc->layno1 = cio_read(cio, 2);    /* LYEpoc_i */
4215                 poc->resno1 = cio_read(cio, 1);    /* REpoc_i */
4216                 poc->compno1 = int_min(
4217                         cio_read(cio, numcomps <= 256 ? 1 : 2), (unsigned int) numcomps);       /* CEpoc_i */
4218                 poc->prg = (OPJ_PROG_ORDER)cio_read(cio, 1);    /* Ppoc_i */
4219         }
4220         
4221         tcp->numpocs = numpchgs + old_poc - 1;
4222 }
4223
4224 /**
4225  * Reads a POC marker (Progression Order Change)
4226  *
4227  * @param       p_header_data   the data contained in the POC box.
4228  * @param       p_j2k                   the jpeg2000 codec.
4229  * @param       p_header_size   the size of the data contained in the POC marker.
4230  * @param       p_manager               the user event manager.
4231 */
4232 opj_bool j2k_read_poc_v2 (
4233                                                 opj_j2k_v2_t *p_j2k,
4234                                                 OPJ_BYTE * p_header_data,
4235                                                 OPJ_UINT32 p_header_size,
4236                                                 struct opj_event_mgr * p_manager)
4237 {
4238         OPJ_UINT32 i, l_nb_comp, l_tmp;
4239         opj_image_t * l_image = 00;
4240         OPJ_UINT32 l_old_poc_nb, l_current_poc_nb, l_current_poc_remaining;
4241         OPJ_UINT32 l_chunk_size, l_comp_room;
4242
4243         opj_cp_v2_t *l_cp = 00;
4244         opj_tcp_v2_t *l_tcp = 00;
4245         opj_poc_t *l_current_poc = 00;
4246
4247         /* preconditions */
4248         assert(p_header_data != 00);
4249         assert(p_j2k != 00);
4250         assert(p_manager != 00);
4251
4252         l_image = p_j2k->m_private_image;
4253         l_nb_comp = l_image->numcomps;
4254         if (l_nb_comp <= 256) {
4255                 l_comp_room = 1;
4256         }
4257         else {
4258                 l_comp_room = 2;
4259         }
4260         l_chunk_size = 5 + 2 * l_comp_room;
4261         l_current_poc_nb = p_header_size / l_chunk_size;
4262         l_current_poc_remaining = p_header_size % l_chunk_size;
4263
4264         if ((l_current_poc_nb <= 0) || (l_current_poc_remaining != 0)) {
4265                 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading POC marker\n");
4266                 return OPJ_FALSE;
4267         }
4268
4269         l_cp = &(p_j2k->m_cp);
4270         l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ?
4271                                 &l_cp->tcps[p_j2k->m_current_tile_number] :
4272                                 p_j2k->m_specific_param.m_decoder.m_default_tcp;
4273         l_old_poc_nb = l_tcp->POC ? l_tcp->numpocs + 1 : 0;
4274         l_current_poc_nb += l_old_poc_nb;
4275
4276         assert(l_current_poc_nb < 32);
4277
4278         /* now poc is in use.*/
4279         l_tcp->POC = 1;
4280
4281         l_current_poc = &l_tcp->pocs[l_old_poc_nb];
4282         for     (i = l_old_poc_nb; i < l_current_poc_nb; ++i) {
4283                 opj_read_bytes(p_header_data,&(l_current_poc->resno0),1);                               /* RSpoc_i */
4284                 ++p_header_data;
4285                 opj_read_bytes(p_header_data,&(l_current_poc->compno0),l_comp_room);    /* CSpoc_i */
4286                 p_header_data+=l_comp_room;
4287                 opj_read_bytes(p_header_data,&(l_current_poc->layno1),2);                               /* LYEpoc_i */
4288                 p_header_data+=2;
4289                 opj_read_bytes(p_header_data,&(l_current_poc->resno1),1);                               /* REpoc_i */
4290                 ++p_header_data;
4291                 opj_read_bytes(p_header_data,&(l_current_poc->compno1),l_comp_room);    /* CEpoc_i */
4292                 p_header_data+=l_comp_room;
4293                 opj_read_bytes(p_header_data,&l_tmp,1);                                                                 /* Ppoc_i */
4294                 ++p_header_data;
4295                 l_current_poc->prg = (OPJ_PROG_ORDER) l_tmp;
4296                 /* make sure comp is in acceptable bounds */
4297                 l_current_poc->compno1 = uint_min(l_current_poc->compno1, l_nb_comp);
4298                 ++l_current_poc;
4299         }
4300
4301         l_tcp->numpocs = l_current_poc_nb - 1;
4302         return OPJ_TRUE;
4303 }
4304
4305 static void j2k_read_crg(opj_j2k_t *j2k) {
4306         int len, i, Xcrg_i, Ycrg_i;
4307         
4308         opj_cio_t *cio = j2k->cio;
4309         int numcomps = j2k->image->numcomps;
4310         
4311         len = cio_read(cio, 2);                 /* Lcrg */
4312         for (i = 0; i < numcomps; i++) {
4313                 Xcrg_i = cio_read(cio, 2);      /* Xcrg_i */
4314                 Ycrg_i = cio_read(cio, 2);      /* Ycrg_i */
4315         }
4316 }
4317
4318 /**
4319  * Reads a CRG marker (Component registration)
4320  *
4321  * @param       p_header_data   the data contained in the TLM box.
4322  * @param       p_j2k                   the jpeg2000 codec.
4323  * @param       p_header_size   the size of the data contained in the TLM marker.
4324  * @param       p_manager               the user event manager.
4325 */
4326 opj_bool j2k_read_crg_v2 (
4327                                                 opj_j2k_v2_t *p_j2k,
4328                                                 OPJ_BYTE * p_header_data,
4329                                                 OPJ_UINT32 p_header_size,
4330                                                 struct opj_event_mgr * p_manager
4331                                         )
4332 {
4333         OPJ_UINT32 l_nb_comp;
4334         /* preconditions */
4335         assert(p_header_data != 00);
4336         assert(p_j2k != 00);
4337         assert(p_manager != 00);
4338
4339         l_nb_comp = p_j2k->m_private_image->numcomps;
4340
4341         if (p_header_size != l_nb_comp *4) {
4342                 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading CRG marker\n");
4343                 return OPJ_FALSE;
4344         }
4345         /* Do not care of this at the moment since only local variables are set here */
4346         /*
4347         for
4348                 (i = 0; i < l_nb_comp; ++i)
4349         {
4350                 opj_read_bytes(p_header_data,&l_Xcrg_i,2);                              // Xcrg_i
4351                 p_header_data+=2;
4352                 opj_read_bytes(p_header_data,&l_Ycrg_i,2);                              // Xcrg_i
4353                 p_header_data+=2;
4354         }
4355         */
4356         return OPJ_TRUE;
4357 }
4358
4359 static void j2k_read_tlm(opj_j2k_t *j2k) {
4360         int len, Ztlm, Stlm, ST, SP, tile_tlm, i;
4361         long int Ttlm_i, Ptlm_i;
4362
4363         opj_cio_t *cio = j2k->cio;
4364         
4365         len = cio_read(cio, 2);         /* Ltlm */
4366         Ztlm = cio_read(cio, 1);        /* Ztlm */
4367         Stlm = cio_read(cio, 1);        /* Stlm */
4368         ST = ((Stlm >> 4) & 0x01) + ((Stlm >> 4) & 0x02);
4369         SP = (Stlm >> 6) & 0x01;
4370         tile_tlm = (len - 4) / ((SP + 1) * 2 + ST);
4371         for (i = 0; i < tile_tlm; i++) {
4372                 Ttlm_i = cio_read(cio, ST);     /* Ttlm_i */
4373                 Ptlm_i = cio_read(cio, SP ? 4 : 2);     /* Ptlm_i */
4374         }
4375 }
4376
4377 /**
4378  * Reads a TLM marker (Tile Length Marker)
4379  *
4380  * @param       p_header_data   the data contained in the TLM box.
4381  * @param       p_j2k                   the jpeg2000 codec.
4382  * @param       p_header_size   the size of the data contained in the TLM marker.
4383  * @param       p_manager               the user event manager.
4384 */
4385 opj_bool j2k_read_tlm_v2 (
4386                                                 opj_j2k_v2_t *p_j2k,
4387                                                 OPJ_BYTE * p_header_data,
4388                                                 OPJ_UINT32 p_header_size,
4389                                                 struct opj_event_mgr * p_manager
4390                                         )
4391 {
4392         OPJ_UINT32 l_Ztlm, l_Stlm, l_ST, l_SP, l_tot_num_tp, l_tot_num_tp_remaining, l_quotient, l_Ptlm_size;
4393         /* preconditions */
4394         assert(p_header_data != 00);
4395         assert(p_j2k != 00);
4396         assert(p_manager != 00);
4397
4398         if (p_header_size < 2) {
4399                 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading TLM marker\n");
4400                 return OPJ_FALSE;
4401         }
4402         p_header_size -= 2;
4403
4404         opj_read_bytes(p_header_data,&l_Ztlm,1);                                /* Ztlm */
4405         ++p_header_data;
4406         opj_read_bytes(p_header_data,&l_Stlm,1);                                /* Stlm */
4407         ++p_header_data;
4408
4409         l_ST = ((l_Stlm >> 4) & 0x3);
4410         l_SP = (l_Stlm >> 6) & 0x1;
4411
4412         l_Ptlm_size = (l_SP + 1) * 2;
4413         l_quotient = l_Ptlm_size + l_ST;
4414
4415         l_tot_num_tp = p_header_size / l_quotient;
4416         l_tot_num_tp_remaining = p_header_size % l_quotient;
4417
4418         if (l_tot_num_tp_remaining != 0) {
4419                 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading TLM marker\n");
4420                 return OPJ_FALSE;
4421         }
4422         /* FIXME Do not care of this at the moment since only local variables are set here */
4423         /*
4424         for
4425                 (i = 0; i < l_tot_num_tp; ++i)
4426         {
4427                 opj_read_bytes(p_header_data,&l_Ttlm_i,l_ST);                           // Ttlm_i
4428                 p_header_data += l_ST;
4429                 opj_read_bytes(p_header_data,&l_Ptlm_i,l_Ptlm_size);            // Ptlm_i
4430                 p_header_data += l_Ptlm_size;
4431         }*/
4432         return OPJ_TRUE;
4433 }
4434
4435 static void j2k_read_plm(opj_j2k_t *j2k) {
4436         int len, i, Zplm, Nplm, add, packet_len = 0;
4437         
4438         opj_cio_t *cio = j2k->cio;
4439
4440         len = cio_read(cio, 2);         /* Lplm */
4441         Zplm = cio_read(cio, 1);        /* Zplm */
4442         len -= 3;
4443         while (len > 0) {
4444                 Nplm = cio_read(cio, 4);                /* Nplm */
4445                 len -= 4;
4446                 for (i = Nplm; i > 0; i--) {
4447                         add = cio_read(cio, 1);
4448                         len--;
4449                         packet_len = (packet_len << 7) + add;   /* Iplm_ij */
4450                         if ((add & 0x80) == 0) {
4451                                 /* New packet */
4452                                 packet_len = 0;
4453                         }
4454                         if (len <= 0)
4455                                 break;
4456                 }
4457         }
4458 }
4459
4460 /**
4461  * Reads a PLM marker (Packet length, main header marker)
4462  *
4463  * @param       p_header_data   the data contained in the TLM box.
4464  * @param       p_j2k                   the jpeg2000 codec.
4465  * @param       p_header_size   the size of the data contained in the TLM marker.
4466  * @param       p_manager               the user event manager.
4467 */
4468 opj_bool j2k_read_plm_v2 (
4469                                                 opj_j2k_v2_t *p_j2k,
4470                                                 OPJ_BYTE * p_header_data,
4471                                                 OPJ_UINT32 p_header_size,
4472                                                 struct opj_event_mgr * p_manager
4473                                         )
4474 {
4475         /* preconditions */
4476         assert(p_header_data != 00);
4477         assert(p_j2k != 00);
4478         assert(p_manager != 00);
4479
4480         if (p_header_size < 1) {
4481                 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PLM marker\n");
4482                 return OPJ_FALSE;
4483         }
4484         /* Do not care of this at the moment since only local variables are set here */
4485         /*
4486         opj_read_bytes(p_header_data,&l_Zplm,1);                                        // Zplm
4487         ++p_header_data;
4488         --p_header_size;
4489
4490         while
4491                 (p_header_size > 0)
4492         {
4493                 opj_read_bytes(p_header_data,&l_Nplm,1);                                // Nplm
4494                 ++p_header_data;
4495                 p_header_size -= (1+l_Nplm);
4496                 if
4497                         (p_header_size < 0)
4498                 {
4499                         opj_event_msg(p_manager, EVT_ERROR, "Error reading PLM marker\n");
4500                         return false;
4501                 }
4502                 for
4503                         (i = 0; i < l_Nplm; ++i)
4504                 {
4505                         opj_read_bytes(p_header_data,&l_tmp,1);                         // Iplm_ij
4506                         ++p_header_data;
4507                         // take only the last seven bytes
4508                         l_packet_len |= (l_tmp & 0x7f);
4509                         if
4510                                 (l_tmp & 0x80)
4511                         {
4512                                 l_packet_len <<= 7;
4513                         }
4514                         else
4515                         {
4516                 // store packet length and proceed to next packet
4517                                 l_packet_len = 0;
4518                         }
4519                 }
4520                 if
4521                         (l_packet_len != 0)
4522                 {
4523                         opj_event_msg(p_manager, EVT_ERROR, "Error reading PLM marker\n");
4524                         return false;
4525                 }
4526         }
4527         */
4528         return OPJ_TRUE;
4529 }
4530
4531 static void j2k_read_plt(opj_j2k_t *j2k) {
4532         int len, i, Zplt, packet_len = 0, add;
4533         
4534         opj_cio_t *cio = j2k->cio;
4535         
4536         len = cio_read(cio, 2);         /* Lplt */
4537         Zplt = cio_read(cio, 1);        /* Zplt */
4538         for (i = len - 3; i > 0; i--) {
4539                 add = cio_read(cio, 1);
4540                 packet_len = (packet_len << 7) + add;   /* Iplt_i */
4541                 if ((add & 0x80) == 0) {
4542                         /* New packet */
4543                         packet_len = 0;
4544                 }
4545         }
4546 }
4547
4548 /**
4549  * Reads a PLT marker (Packet length, tile-part header)
4550  *
4551  * @param       p_header_data   the data contained in the PLT box.
4552  * @param       p_j2k                   the jpeg2000 codec.
4553  * @param       p_header_size   the size of the data contained in the PLT marker.
4554  * @param       p_manager               the user event manager.
4555 */
4556 opj_bool j2k_read_plt_v2 (
4557                                                 opj_j2k_v2_t *p_j2k,
4558                                                 OPJ_BYTE * p_header_data,
4559                                                 OPJ_UINT32 p_header_size,
4560                                                 struct opj_event_mgr * p_manager
4561                                         )
4562 {
4563         OPJ_UINT32 l_Zplt, l_tmp, l_packet_len = 0, i;
4564
4565         /* preconditions */
4566         assert(p_header_data != 00);
4567         assert(p_j2k != 00);
4568         assert(p_manager != 00);
4569
4570         if (p_header_size < 1) {
4571                 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PLT marker\n");
4572                 return OPJ_FALSE;
4573         }
4574
4575         opj_read_bytes(p_header_data,&l_Zplt,1);                /* Zplt */
4576         ++p_header_data;
4577         --p_header_size;
4578
4579         for (i = 0; i < p_header_size; ++i) {
4580                 opj_read_bytes(p_header_data,&l_tmp,1);         /* Iplt_ij */
4581                 ++p_header_data;
4582                 /* take only the last seven bytes */
4583                 l_packet_len |= (l_tmp & 0x7f);
4584                 if (l_tmp & 0x80) {
4585                         l_packet_len <<= 7;
4586                 }
4587                 else {
4588             /* store packet length and proceed to next packet */
4589                         l_packet_len = 0;
4590                 }
4591         }
4592
4593         if (l_packet_len != 0) {
4594                 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PLT marker\n");
4595                 return OPJ_FALSE;
4596         }
4597
4598         return OPJ_TRUE;
4599 }
4600
4601 static void j2k_read_ppm(opj_j2k_t *j2k) {
4602         int len, Z_ppm, i, j;
4603         int N_ppm;
4604
4605         opj_cp_t *cp = j2k->cp;
4606         opj_cio_t *cio = j2k->cio;
4607         
4608         len = cio_read(cio, 2);
4609         cp->ppm = 1;
4610         
4611         Z_ppm = cio_read(cio, 1);       /* Z_ppm */
4612         len -= 3;
4613         while (len > 0) {
4614                 if (cp->ppm_previous == 0) {
4615                         N_ppm = cio_read(cio, 4);       /* N_ppm */
4616                         len -= 4;
4617                 } else {
4618                         N_ppm = cp->ppm_previous;
4619                 }
4620                 j = cp->ppm_store;
4621                 if (Z_ppm == 0) {       /* First PPM marker */
4622                         cp->ppm_data = (unsigned char *) opj_malloc(N_ppm * sizeof(unsigned char));
4623                         cp->ppm_data_first = cp->ppm_data;
4624                         cp->ppm_len = N_ppm;
4625                 } else {                        /* NON-first PPM marker */
4626                         cp->ppm_data = (unsigned char *) opj_realloc(cp->ppm_data, (N_ppm +     cp->ppm_store) * sizeof(unsigned char));
4627
4628 #ifdef USE_JPWL
4629                         /* this memory allocation check could be done even in non-JPWL cases */
4630                         if (cp->correct) {
4631                                 if (!cp->ppm_data) {
4632                                         opj_event_msg(j2k->cinfo, EVT_ERROR,
4633                                                 "JPWL: failed memory allocation during PPM marker parsing (pos. %x)\n",
4634                                                 cio_tell(cio));
4635                                         if (!JPWL_ASSUME || JPWL_ASSUME) {
4636                                                 opj_free(cp->ppm_data);
4637                                                 opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
4638                                                 return;
4639                                         }
4640                                 }
4641                         }
4642 #endif
4643
4644                         cp->ppm_data_first = cp->ppm_data;
4645                         cp->ppm_len = N_ppm + cp->ppm_store;
4646                 }
4647                 for (i = N_ppm; i > 0; i--) {   /* Read packet header */
4648                         cp->ppm_data[j] = cio_read(cio, 1);
4649                         j++;
4650                         len--;
4651                         if (len == 0)
4652                                 break;                  /* Case of non-finished packet header in present marker but finished in next one */
4653                 }
4654                 cp->ppm_previous = i - 1;
4655                 cp->ppm_store = j;
4656         }
4657 }
4658 /**
4659  * Reads a PPM marker (Packed packet headers, main header)
4660  *
4661  * @param       p_header_data   the data contained in the POC box.
4662  * @param       p_j2k                   the jpeg2000 codec.
4663  * @param       p_header_size   the size of the data contained in the POC marker.
4664  * @param       p_manager               the user event manager.
4665 */
4666 #if 0
4667 opj_bool j2k_read_ppm_v2 (
4668                                                 opj_j2k_v2_t *p_j2k,
4669                                                 OPJ_BYTE * p_header_data,
4670                                                 OPJ_UINT32 p_header_size,
4671                                                 struct opj_event_mgr * p_manager
4672                                         )
4673 {
4674
4675         opj_cp_v2_t *l_cp = 00;
4676         OPJ_UINT32 l_remaining_data, l_Z_ppm, l_N_ppm;
4677
4678         /* preconditions */
4679         assert(p_header_data != 00);
4680         assert(p_j2k != 00);
4681         assert(p_manager != 00);
4682
4683         if (p_header_size < 1) {
4684                 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PPM marker\n");
4685                 return OPJ_FALSE;
4686         }
4687
4688         l_cp = &(p_j2k->m_cp);
4689         l_cp->ppm = 1;
4690
4691         opj_read_bytes(p_header_data,&l_Z_ppm,1);               /* Z_ppm */
4692         ++p_header_data;
4693         --p_header_size;
4694
4695         /* First PPM marker */
4696         if (l_Z_ppm == 0) {
4697                 if (p_header_size < 4) {
4698                         opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PPM marker\n");
4699                         return OPJ_FALSE;
4700                 }
4701
4702                 opj_read_bytes(p_header_data,&l_N_ppm,4);               /* N_ppm */
4703                 p_header_data+=4;
4704                 p_header_size-=4;
4705
4706                 /* First PPM marker: Initialization */
4707                 l_cp->ppm_len = l_N_ppm;
4708                 l_cp->ppm_data_size = 0;
4709
4710                 l_cp->ppm_buffer = (OPJ_BYTE *) opj_malloc(l_cp->ppm_len);
4711                 if (l_cp->ppm_buffer == 00) {
4712                         opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory reading ppm marker\n");
4713                         return OPJ_FALSE;
4714                 }
4715                 memset(l_cp->ppm_buffer,0,l_cp->ppm_len);
4716
4717                 l_cp->ppm_data = l_cp->ppm_buffer;
4718         }
4719
4720         while (1) {
4721                 if (l_cp->ppm_data_size == l_cp->ppm_len) {
4722                         if (p_header_size >= 4) {
4723                                 /* read a N_ppm */
4724                                 opj_read_bytes(p_header_data,&l_N_ppm,4);               /* N_ppm */
4725                                 p_header_data+=4;
4726                                 p_header_size-=4;
4727                                 l_cp->ppm_len += l_N_ppm ;
4728
4729                                 l_cp->ppm_buffer = (OPJ_BYTE *) opj_realloc(l_cp->ppm_buffer, l_cp->ppm_len);
4730                                 if (l_cp->ppm_buffer == 00) {
4731                                         opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory reading ppm marker\n");
4732                                         return OPJ_FALSE;
4733                                 }
4734                                 memset(l_cp->ppm_buffer+l_cp->ppm_data_size,0,l_N_ppm);
4735
4736                                 l_cp->ppm_data = l_cp->ppm_buffer;
4737                         }
4738                         else {
4739                                 return OPJ_FALSE;
4740                         }
4741                 }
4742
4743                 l_remaining_data = l_cp->ppm_len - l_cp->ppm_data_size;
4744
4745                 if (l_remaining_data <= p_header_size) {
4746                         /* we must store less information than available in the packet */
4747                         memcpy(l_cp->ppm_buffer + l_cp->ppm_data_size , p_header_data , l_remaining_data);
4748                         l_cp->ppm_data_size = l_cp->ppm_len;
4749                         p_header_size -= l_remaining_data;
4750                         p_header_data += l_remaining_data;
4751                 }
4752                 else {
4753                         memcpy(l_cp->ppm_buffer + l_cp->ppm_data_size , p_header_data , p_header_size);
4754                         l_cp->ppm_data_size += p_header_size;
4755                         p_header_data += p_header_size;
4756                         p_header_size = 0;
4757                         break;
4758                 }
4759         }
4760
4761         return OPJ_TRUE;
4762 }
4763 #endif
4764
4765
4766
4767 /**
4768  * Reads a PPM marker (Packed packet headers, main header)
4769  *
4770  * @param       p_header_data   the data contained in the POC box.
4771  * @param       p_j2k                   the jpeg2000 codec.
4772  * @param       p_header_size   the size of the data contained in the POC marker.
4773  * @param       p_manager               the user event manager.
4774 */
4775 opj_bool j2k_read_ppm_v3 (
4776                                                 opj_j2k_v2_t *p_j2k,
4777                                                 OPJ_BYTE * p_header_data,
4778                                                 OPJ_UINT32 p_header_size,
4779                                                 struct opj_event_mgr * p_manager
4780                                         )
4781 {
4782         opj_cp_v2_t *l_cp = 00;
4783         OPJ_UINT32 l_remaining_data, l_Z_ppm, l_N_ppm;
4784
4785         /* preconditions */
4786         assert(p_header_data != 00);
4787         assert(p_j2k != 00);
4788         assert(p_manager != 00);
4789
4790         /* Minimum size of PPM marker is equal to the size of Zppm element */
4791         if (p_header_size < 1) {
4792                 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PPM marker\n");
4793                 return OPJ_FALSE;
4794         }
4795
4796         l_cp = &(p_j2k->m_cp);
4797         l_cp->ppm = 1;
4798
4799         opj_read_bytes(p_header_data,&l_Z_ppm,1);               /* Z_ppm */
4800         ++p_header_data;
4801         --p_header_size;
4802
4803         /* First PPM marker */
4804         if (l_Z_ppm == 0) {
4805                 /* We need now at least the Nppm^0 element */
4806                 if (p_header_size < 4) {
4807                         opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PPM marker\n");
4808                         return OPJ_FALSE;
4809                 }
4810
4811                 opj_read_bytes(p_header_data,&l_N_ppm,4);               /* First N_ppm */
4812                 p_header_data+=4;
4813                 p_header_size-=4;
4814
4815                 /* First PPM marker: Initialization */
4816                 l_cp->ppm_len = l_N_ppm;
4817                 l_cp->ppm_data_read = 0;
4818
4819                 l_cp->ppm_data = (OPJ_BYTE *) opj_malloc(l_cp->ppm_len);
4820                 if (l_cp->ppm_data == 00) {
4821                         opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory reading ppm marker\n");
4822                         return OPJ_FALSE;
4823                 }
4824                 memset(l_cp->ppm_data,0,l_cp->ppm_len);
4825
4826                 l_cp->ppm_data_current = l_cp->ppm_data;
4827
4828                 /*l_cp->ppm_data = l_cp->ppm_buffer;*/
4829         }
4830         else {
4831                 if (p_header_size < 4) {
4832                         opj_event_msg_v2(p_manager, EVT_WARNING, "Empty PPM marker\n");
4833                         return OPJ_TRUE;
4834                 }
4835                 else {
4836                         /* Uncompleted Ippm series in the previous PPM marker?*/
4837                         if (l_cp->ppm_data_read < l_cp->ppm_len) {
4838                                 /* Get the place where add the remaining Ippm series*/
4839                                 l_cp->ppm_data_current = &(l_cp->ppm_data[l_cp->ppm_data_read]);
4840                                 l_N_ppm = l_cp->ppm_len - l_cp->ppm_data_read;
4841                         }
4842                         else {
4843                                 opj_read_bytes(p_header_data,&l_N_ppm,4);               /* First N_ppm */
4844                                 p_header_data+=4;
4845                                 p_header_size-=4;
4846
4847                                 /* Increase the size of ppm_data to add the new Ippm series*/
4848                                 l_cp->ppm_data = (OPJ_BYTE *) opj_realloc(l_cp->ppm_data, l_cp->ppm_len + l_N_ppm);
4849
4850                                 /* Keep the position of the place where concatenate the new series*/
4851                                 l_cp->ppm_data_current = &(l_cp->ppm_data[l_cp->ppm_len]);
4852                                 l_cp->ppm_len += l_N_ppm;
4853                         }
4854                 }
4855         }
4856
4857         l_remaining_data = p_header_size;
4858
4859         while (l_remaining_data >= l_N_ppm) {
4860                 /* read a complete Ippm series*/
4861                 memcpy(l_cp->ppm_data_current, p_header_data, l_N_ppm);
4862                 p_header_size -= l_N_ppm;
4863                 p_header_data += l_N_ppm;
4864
4865                 l_cp->ppm_data_read += l_N_ppm; /* Increase the number of data read*/
4866
4867                 if (p_header_size)
4868                 {
4869                         opj_read_bytes(p_header_data,&l_N_ppm,4);               /* N_ppm^i */
4870                         p_header_data+=4;
4871                         p_header_size-=4;
4872                 }
4873                 else {
4874                         l_remaining_data = p_header_size;
4875                         break;
4876                 }
4877
4878                 l_remaining_data = p_header_size;
4879
4880                 /* Next Ippm series is a complete series ?*/
4881                 if (l_remaining_data > l_N_ppm) {
4882                         /* Increase the size of ppm_data to add the new Ippm series*/
4883                         l_cp->ppm_data = (OPJ_BYTE *) opj_realloc(l_cp->ppm_data, l_cp->ppm_len + l_N_ppm);
4884
4885                         /* Keep the position of the place where concatenate the new series */
4886                         l_cp->ppm_data_current = &(l_cp->ppm_data[l_cp->ppm_len]);
4887                         l_cp->ppm_len += l_N_ppm;
4888                 }
4889
4890         }
4891
4892         /* Need to read an incomplete Ippm series*/
4893         if (l_remaining_data) {
4894                 l_cp->ppm_data = (OPJ_BYTE *) opj_realloc(l_cp->ppm_data, l_cp->ppm_len + l_N_ppm);
4895
4896                 /* Keep the position of the place where concatenate the new series*/
4897                 l_cp->ppm_data_current = &(l_cp->ppm_data[l_cp->ppm_len]);
4898                 l_cp->ppm_len += l_N_ppm;
4899
4900                 /* Read incomplete Ippm series*/
4901                 memcpy(l_cp->ppm_data_current, p_header_data, l_remaining_data);
4902                 p_header_size -= l_remaining_data;
4903                 p_header_data += l_remaining_data;
4904
4905                 l_cp->ppm_data_read += l_remaining_data; /* Increase the number of data read*/
4906         }
4907
4908 #ifdef CLEAN_MSD
4909
4910                 if (l_cp->ppm_data_size == l_cp->ppm_len) {
4911                         if (p_header_size >= 4) {
4912                                 /* read a N_ppm*/
4913                                 opj_read_bytes(p_header_data,&l_N_ppm,4);               /* N_ppm */
4914                                 p_header_data+=4;
4915                                 p_header_size-=4;
4916                                 l_cp->ppm_len += l_N_ppm ;
4917
4918                                 l_cp->ppm_buffer = (OPJ_BYTE *) opj_realloc(l_cp->ppm_buffer, l_cp->ppm_len);
4919                                 if (l_cp->ppm_buffer == 00) {
4920                                         opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory reading ppm marker\n");
4921                                         return OPJ_FALSE;
4922                                 }
4923                                 memset(l_cp->ppm_buffer+l_cp->ppm_data_size,0,l_N_ppm);
4924
4925                                 l_cp->ppm_data = l_cp->ppm_buffer;
4926                         }
4927                         else {
4928                                 return OPJ_FALSE;
4929                         }
4930                 }
4931
4932                 l_remaining_data = l_cp->ppm_len - l_cp->ppm_data_size;
4933
4934                 if (l_remaining_data <= p_header_size) {
4935                         /* we must store less information than available in the packet */
4936                         memcpy(l_cp->ppm_buffer + l_cp->ppm_data_size , p_header_data , l_remaining_data);
4937                         l_cp->ppm_data_size = l_cp->ppm_len;
4938                         p_header_size -= l_remaining_data;
4939                         p_header_data += l_remaining_data;
4940                 }
4941                 else {
4942                         memcpy(l_cp->ppm_buffer + l_cp->ppm_data_size , p_header_data , p_header_size);
4943                         l_cp->ppm_data_size += p_header_size;
4944                         p_header_data += p_header_size;
4945                         p_header_size = 0;
4946                         break;
4947                 }
4948         }
4949 #endif
4950         return OPJ_TRUE;
4951 }
4952
4953 static void j2k_read_ppt(opj_j2k_t *j2k) {
4954         int len, Z_ppt, i, j = 0;
4955
4956         opj_cp_t *cp = j2k->cp;
4957         opj_tcp_t *tcp = cp->tcps + j2k->curtileno;
4958         opj_cio_t *cio = j2k->cio;
4959
4960         len = cio_read(cio, 2);
4961         Z_ppt = cio_read(cio, 1);
4962         tcp->ppt = 1;
4963         if (Z_ppt == 0) {               /* First PPT marker */
4964                 tcp->ppt_data = (unsigned char *) opj_malloc((len - 3) * sizeof(unsigned char));
4965                 tcp->ppt_data_first = tcp->ppt_data;
4966                 tcp->ppt_store = 0;
4967                 tcp->ppt_len = len - 3;
4968         } else {                        /* NON-first PPT marker */
4969                 tcp->ppt_data = (unsigned char *) opj_realloc(tcp->ppt_data, (len - 3 + tcp->ppt_store) * sizeof(unsigned char));
4970                 tcp->ppt_data_first = tcp->ppt_data;
4971                 tcp->ppt_len = len - 3 + tcp->ppt_store;
4972         }
4973         j = tcp->ppt_store;
4974         for (i = len - 3; i > 0; i--) {
4975                 tcp->ppt_data[j] = cio_read(cio, 1);
4976                 j++;
4977         }
4978         tcp->ppt_store = j;
4979 }
4980
4981 /**
4982  * Reads a PPT marker (Packed packet headers, tile-part header)
4983  *
4984  * @param       p_header_data   the data contained in the PPT box.
4985  * @param       p_j2k                   the jpeg2000 codec.
4986  * @param       p_header_size   the size of the data contained in the PPT marker.
4987  * @param       p_manager               the user event manager.
4988 */
4989 opj_bool j2k_read_ppt_v2 (      opj_j2k_v2_t *p_j2k,
4990                                                         OPJ_BYTE * p_header_data,
4991                                                         OPJ_UINT32 p_header_size,
4992                                                         struct opj_event_mgr * p_manager )
4993 {
4994         opj_cp_v2_t *l_cp = 00;
4995         opj_tcp_v2_t *l_tcp = 00;
4996         OPJ_UINT32 l_Z_ppt;
4997
4998         /* preconditions */
4999         assert(p_header_data != 00);
5000         assert(p_j2k != 00);
5001         assert(p_manager != 00);
5002
5003         /* We need to have the Z_ppt element at minimum */
5004         if (p_header_size < 1) {
5005                 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PPT marker\n");
5006                 return OPJ_FALSE;
5007         }
5008
5009         l_cp = &(p_j2k->m_cp);
5010         if (l_cp->ppm){
5011                 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");
5012                 return OPJ_FALSE;
5013         }
5014
5015         l_tcp = &(l_cp->tcps[p_j2k->m_current_tile_number]);
5016         l_tcp->ppt = 1;
5017
5018         opj_read_bytes(p_header_data,&l_Z_ppt,1);               /* Z_ppt */
5019         ++p_header_data;
5020         --p_header_size;
5021
5022         /* Allocate buffer to read the packet header */
5023         if (l_Z_ppt == 0) {
5024                 /* First PPT marker */
5025                 l_tcp->ppt_data_size = 0;
5026                 l_tcp->ppt_len = p_header_size;
5027
5028                 l_tcp->ppt_buffer = (OPJ_BYTE *) opj_calloc(l_tcp->ppt_len, sizeof(OPJ_BYTE) );
5029                 if (l_tcp->ppt_buffer == 00) {
5030                         opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory reading PPT marker\n");
5031                         return OPJ_FALSE;
5032                 }
5033                 l_tcp->ppt_data = l_tcp->ppt_buffer;
5034
5035                 /* memset(l_tcp->ppt_buffer,0,l_tcp->ppt_len); */
5036         }
5037         else {
5038                 l_tcp->ppt_len += p_header_size;
5039
5040                 l_tcp->ppt_buffer = (OPJ_BYTE *) opj_realloc(l_tcp->ppt_buffer,l_tcp->ppt_len);
5041                 if (l_tcp->ppt_buffer == 00) {
5042                         opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory reading PPT marker\n");
5043                         return OPJ_FALSE;
5044                 }
5045                 l_tcp->ppt_data = l_tcp->ppt_buffer;
5046
5047                 memset(l_tcp->ppt_buffer+l_tcp->ppt_data_size,0,p_header_size);
5048         }
5049
5050         /* Read packet header from buffer */
5051         memcpy(l_tcp->ppt_buffer+l_tcp->ppt_data_size,p_header_data,p_header_size);
5052
5053         l_tcp->ppt_data_size += p_header_size;
5054
5055         return OPJ_TRUE;
5056 }
5057
5058 /**
5059  * Writes the TLM marker (Tile Length Marker)
5060  * 
5061  * @param       p_stream                                the stream to write data to.
5062  * @param       p_j2k                           J2K codec.
5063  * @param       p_manager               the user event manager.
5064 */
5065 opj_bool j2k_write_tlm_v2(      opj_j2k_v2_t *p_j2k,
5066                                                         struct opj_stream_private *p_stream,
5067                                                         struct opj_event_mgr * p_manager )
5068 {
5069         OPJ_BYTE * l_current_data = 00;
5070         OPJ_UINT32 l_tlm_size;
5071
5072         // preconditions
5073         assert(p_j2k != 00);
5074         assert(p_manager != 00);
5075         assert(p_stream != 00);
5076
5077         l_tlm_size = 6 + (5*p_j2k->m_specific_param.m_encoder.m_total_tile_parts);
5078         
5079         if (l_tlm_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
5080                 p_j2k->m_specific_param.m_encoder.m_header_tile_data 
5081                         = (OPJ_BYTE*)opj_realloc(
5082                                 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
5083                                 l_tlm_size);
5084                 
5085                 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
5086                         return OPJ_FALSE;
5087                 }
5088
5089                 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_tlm_size;
5090         }
5091
5092         l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
5093
5094         /* change the way data is written to avoid seeking if possible */
5095         // TODO
5096         p_j2k->m_specific_param.m_encoder.m_tlm_start = opj_stream_tell(p_stream);
5097         
5098         opj_write_bytes(l_current_data,J2K_MS_TLM,2);                                   /* TLM */
5099         l_current_data += 2;
5100         
5101         opj_write_bytes(l_current_data,l_tlm_size-2,2);                                 /* Lpoc */
5102         l_current_data += 2;
5103         
5104         opj_write_bytes(l_current_data,0,1);                                                    /* Ztlm=0*/
5105         ++l_current_data;
5106         
5107         opj_write_bytes(l_current_data,0x50,1);                                                 /* Stlm ST=1(8bits-255 tiles max),SP=1(Ptlm=32bits) */
5108         ++l_current_data;
5109         
5110         /* do nothing on the 5 * l_j2k->m_specific_param.m_encoder.m_total_tile_parts remaining data */
5111         if (opj_stream_write_data(p_stream,p_j2k->m_specific_param.m_encoder.m_header_tile_data,l_tlm_size,p_manager) != l_tlm_size) {
5112                 return OPJ_FALSE;
5113         }
5114
5115         return OPJ_TRUE;
5116 }
5117
5118 static void j2k_write_tlm(opj_j2k_t *j2k){
5119         int lenp;
5120         opj_cio_t *cio = j2k->cio;
5121         j2k->tlm_start = cio_tell(cio);
5122         cio_write(cio, J2K_MS_TLM, 2);/* TLM */
5123         lenp = 4 + (5*j2k->totnum_tp);
5124         cio_write(cio,lenp,2);                          /* Ltlm */
5125         cio_write(cio, 0,1);                                    /* Ztlm=0*/
5126         cio_write(cio,80,1);                                    /* Stlm ST=1(8bits-255 tiles max),SP=1(Ptlm=32bits) */
5127         cio_skip(cio,5*j2k->totnum_tp);
5128 }
5129
5130 static void j2k_write_sot(opj_j2k_t *j2k) {
5131         int lenp, len;
5132
5133         opj_cio_t *cio = j2k->cio;
5134
5135         j2k->sot_start = cio_tell(cio);
5136         cio_write(cio, J2K_MS_SOT, 2);          /* SOT */
5137         lenp = cio_tell(cio);
5138         cio_skip(cio, 2);                                       /* Lsot (further) */
5139         cio_write(cio, j2k->curtileno, 2);      /* Isot */
5140         cio_skip(cio, 4);                                       /* Psot (further in j2k_write_sod) */
5141         cio_write(cio, j2k->cur_tp_num , 1);    /* TPsot */
5142         cio_write(cio, j2k->cur_totnum_tp[j2k->curtileno], 1);          /* TNsot */
5143         len = cio_tell(cio) - lenp;
5144         cio_seek(cio, lenp);
5145         cio_write(cio, len, 2);                         /* Lsot */
5146         cio_seek(cio, lenp + len);
5147
5148         /* UniPG>> */
5149 #ifdef USE_JPWL
5150         /* update markers struct */
5151         j2k_add_marker(j2k->cstr_info, J2K_MS_SOT, j2k->sot_start, len + 2);
5152 #endif /* USE_JPWL */
5153         /* <<UniPG */
5154
5155         if( j2k->cstr_info && j2k->cur_tp_num==0){
5156           j2k_add_tlmarker( j2k->curtileno, j2k->cstr_info, J2K_MS_SOT, lenp, len);
5157         }
5158 }
5159
5160 /**
5161  * Writes the SOT marker (Start of tile-part)
5162  *
5163  * @param       p_stream                                the stream to write data to.
5164  * @param       p_j2k                           J2K codec.
5165  * @param       p_manager               the user event manager.
5166 */
5167 opj_bool j2k_write_sot_v2(      opj_j2k_v2_t *p_j2k,
5168                                                         OPJ_BYTE * p_data,
5169                                                         OPJ_UINT32 * p_data_written,
5170                                                         const struct opj_stream_private *p_stream,
5171                                                         struct opj_event_mgr * p_manager )
5172 {
5173         /* preconditions */
5174         assert(p_j2k != 00);
5175         assert(p_manager != 00);
5176         assert(p_stream != 00);
5177
5178         opj_write_bytes(p_data,J2K_MS_SOT,2);                                   /* SOT */
5179         p_data += 2;
5180
5181         opj_write_bytes(p_data,10,2);                                                   /* Lsot */
5182         p_data += 2;
5183
5184         opj_write_bytes(p_data, p_j2k->m_current_tile_number,2);                        /* Isot */
5185         p_data += 2;
5186
5187         /* Psot  */
5188         p_data += 4;
5189
5190         opj_write_bytes(p_data, p_j2k->m_specific_param.m_encoder.m_current_tile_part_number,1);                        /* TPsot */
5191         ++p_data;
5192
5193         opj_write_bytes(p_data, p_j2k->m_cp.tcps[p_j2k->m_current_tile_number].m_nb_tile_parts,1);                      /* TNsot */
5194         ++p_data;
5195
5196         /* UniPG>> */
5197 #ifdef USE_JPWL
5198         /* update markers struct */
5199 /*
5200         j2k_add_marker(p_j2k->cstr_info, J2K_MS_SOT, p_j2k->sot_start, len + 2);
5201 */
5202   assert( 0 && "TODO" );
5203 #endif /* USE_JPWL */
5204
5205         * p_data_written = 12;
5206
5207         return OPJ_TRUE;
5208 }
5209
5210
5211 static void j2k_read_sot(opj_j2k_t *j2k) {
5212         int len, tileno, totlen, partno, numparts;
5213   OPJ_UINT32 i;
5214         opj_tcp_t *tcp = NULL;
5215         char status = 0;
5216
5217         opj_cp_t *cp = j2k->cp;
5218         opj_cio_t *cio = j2k->cio;
5219
5220         len = cio_read(cio, 2);
5221         tileno = cio_read(cio, 2);
5222
5223 #ifdef USE_JPWL
5224         if (j2k->cp->correct) {
5225
5226                 static int backup_tileno = 0;
5227
5228                 /* tileno is negative or larger than the number of tiles!!! */
5229                 if ((tileno < 0) || (tileno >= (cp->tw * cp->th))) {
5230                         opj_event_msg(j2k->cinfo, EVT_ERROR,
5231                                 "JPWL: bad tile number (%d out of a maximum of %d)\n",
5232                                 tileno, (cp->tw * cp->th));
5233                         if (!JPWL_ASSUME) {
5234                                 opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
5235                                 return;
5236                         }
5237                         /* we try to correct */
5238                         tileno = backup_tileno;
5239                         opj_event_msg(j2k->cinfo, EVT_WARNING, "- trying to adjust this\n"
5240                                 "- setting tile number to %d\n",
5241                                 tileno);
5242                 }
5243
5244                 /* keep your private count of tiles */
5245                 backup_tileno++;
5246         }
5247   else
5248 #endif /* USE_JPWL */
5249   {
5250     /* tileno is negative or larger than the number of tiles!!! */
5251     if ((tileno < 0) || (tileno >= (cp->tw * cp->th))) {
5252       opj_event_msg(j2k->cinfo, EVT_ERROR,
5253         "JPWL: bad tile number (%d out of a maximum of %d)\n",
5254         tileno, (cp->tw * cp->th));
5255       return;
5256     }
5257   }
5258         
5259         if (cp->tileno_size == 0) {
5260                 cp->tileno[cp->tileno_size] = tileno;
5261                 cp->tileno_size++;
5262         } else {
5263                 i = 0;
5264                 assert(cp->tileno_size >= 0);
5265                 while (i < (OPJ_UINT32)cp->tileno_size && status == 0) {
5266                         status = cp->tileno[i] == tileno ? 1 : 0;
5267                         i++;
5268                 }
5269                 if (status == 0) {
5270                         cp->tileno[cp->tileno_size] = tileno;
5271                         cp->tileno_size++;
5272                 }
5273         }
5274         
5275         totlen = cio_read(cio, 4);
5276
5277 #ifdef USE_JPWL
5278         if (j2k->cp->correct) {
5279
5280                 /* totlen is negative or larger than the bytes left!!! */
5281                 if ((totlen < 0) || (totlen > (cio_numbytesleft(cio) + 8))) {
5282                         opj_event_msg(j2k->cinfo, EVT_ERROR,
5283                                 "JPWL: bad tile byte size (%d bytes against %d bytes left)\n",
5284                                 totlen, cio_numbytesleft(cio) + 8);
5285                         if (!JPWL_ASSUME) {
5286                                 opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
5287                                 return;
5288                         }
5289                         /* we try to correct */
5290                         totlen = 0;
5291                         opj_event_msg(j2k->cinfo, EVT_WARNING, "- trying to adjust this\n"
5292                                 "- setting Psot to %d => assuming it is the last tile\n",
5293                                 totlen);
5294                 }
5295
5296         }
5297   else
5298 #endif /* USE_JPWL */
5299   {
5300     /* totlen is negative or larger than the bytes left!!! */
5301     if ((totlen < 0) || (totlen > (cio_numbytesleft(cio) + 8))) {
5302       opj_event_msg(j2k->cinfo, EVT_ERROR,
5303         "JPWL: bad tile byte size (%d bytes against %d bytes left)\n",
5304         totlen, cio_numbytesleft(cio) + 8);
5305       return;
5306     }
5307   }
5308
5309         if (!totlen)
5310                 totlen = cio_numbytesleft(cio) + 8;
5311         
5312         partno = cio_read(cio, 1);
5313         numparts = cio_read(cio, 1);
5314   
5315   if (partno >= numparts) {
5316     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);
5317     numparts = partno+1;
5318   }
5319         
5320         j2k->curtileno = tileno;
5321         j2k->cur_tp_num = partno;
5322         j2k->eot = cio_getbp(cio) - 12 + totlen;
5323         j2k->state = J2K_STATE_TPH;
5324         tcp = &cp->tcps[j2k->curtileno];
5325
5326         /* Index */
5327         if (j2k->cstr_info) {
5328                 if (tcp->first) {
5329                         if (tileno == 0) 
5330                                 j2k->cstr_info->main_head_end = cio_tell(cio) - 13;
5331                         j2k->cstr_info->tile[tileno].tileno = tileno;
5332                         j2k->cstr_info->tile[tileno].start_pos = cio_tell(cio) - 12;
5333                         j2k->cstr_info->tile[tileno].end_pos = j2k->cstr_info->tile[tileno].start_pos + totlen - 1;                             
5334     } else {
5335                         j2k->cstr_info->tile[tileno].end_pos += totlen;
5336                 }
5337     j2k->cstr_info->tile[tileno].num_tps = numparts;
5338     if (numparts)
5339       j2k->cstr_info->tile[tileno].tp = (opj_tp_info_t *) opj_realloc(j2k->cstr_info->tile[tileno].tp, numparts * sizeof(opj_tp_info_t));
5340     else
5341       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)*/
5342                 j2k->cstr_info->tile[tileno].tp[partno].tp_start_pos = cio_tell(cio) - 12;
5343                 j2k->cstr_info->tile[tileno].tp[partno].tp_end_pos = 
5344                         j2k->cstr_info->tile[tileno].tp[partno].tp_start_pos + totlen - 1;
5345         }
5346         
5347         if (tcp->first == 1) {          
5348                 /* Initialization PPT */
5349                 opj_tccp_t *tmp = tcp->tccps;
5350                 memcpy(tcp, j2k->default_tcp, sizeof(opj_tcp_t));
5351                 tcp->ppt = 0;
5352                 tcp->ppt_data = NULL;
5353                 tcp->ppt_data_first = NULL;
5354                 tcp->tccps = tmp;
5355
5356                 for (i = 0; i < j2k->image->numcomps; i++) {
5357                         tcp->tccps[i] = j2k->default_tcp->tccps[i];
5358                 }
5359                 cp->tcps[j2k->curtileno].first = 0;
5360         }
5361 }
5362
5363 /**
5364  * Reads a PPT marker (Packed packet headers, tile-part header)
5365  *
5366  * @param       p_header_data   the data contained in the PPT box.
5367  * @param       p_j2k                   the jpeg2000 codec.
5368  * @param       p_header_size   the size of the data contained in the PPT marker.
5369  * @param       p_manager               the user event manager.
5370 */
5371 opj_bool j2k_read_sot_v2 (
5372                                                 opj_j2k_v2_t *p_j2k,
5373                                                 OPJ_BYTE * p_header_data,
5374                                                 OPJ_UINT32 p_header_size,
5375                                                 struct opj_event_mgr * p_manager
5376                                         )
5377 {
5378
5379         opj_cp_v2_t *l_cp = 00;
5380         opj_tcp_v2_t *l_tcp = 00;
5381         OPJ_UINT32 l_tot_len, l_num_parts = 0;
5382         OPJ_UINT32 l_current_part;
5383         OPJ_UINT32 l_tile_x,l_tile_y;
5384
5385         /* preconditions */
5386         assert(p_header_data != 00);
5387         assert(p_j2k != 00);
5388         assert(p_manager != 00);
5389
5390         /* Size of this marker is fixed = 12 (we have already read marker and its size)*/
5391         if (p_header_size != 8) {
5392                 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading SOT marker\n");
5393                 return OPJ_FALSE;
5394         }
5395
5396         l_cp = &(p_j2k->m_cp);
5397         opj_read_bytes(p_header_data,&(p_j2k->m_current_tile_number),2);                /* Isot */
5398         p_header_data+=2;
5399
5400         l_tcp = &l_cp->tcps[p_j2k->m_current_tile_number];
5401         l_tile_x = p_j2k->m_current_tile_number % l_cp->tw;
5402         l_tile_y = p_j2k->m_current_tile_number / l_cp->tw;
5403
5404 #ifdef USE_JPWL
5405         if (l_cp->correct) {
5406
5407                 int tileno = p_j2k->m_current_tile_number;
5408                 static int backup_tileno = 0;
5409
5410                 /* tileno is negative or larger than the number of tiles!!! */
5411                 if ((tileno < 0) || (tileno > (l_cp->tw * l_cp->th))) {
5412                         opj_event_msg_v2(p_manager, EVT_ERROR,
5413                                 "JPWL: bad tile number (%d out of a maximum of %d)\n",
5414                                 tileno, (l_cp->tw * l_cp->th));
5415                         if (!JPWL_ASSUME) {
5416                                 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
5417                                 return OPJ_FALSE;
5418                         }
5419                         /* we try to correct */
5420                         tileno = backup_tileno;
5421                         opj_event_msg_v2(p_manager, EVT_WARNING, "- trying to adjust this\n"
5422                                 "- setting tile number to %d\n",
5423                                 tileno);
5424                 }
5425
5426                 /* keep your private count of tiles */
5427                 backup_tileno++;
5428         };
5429 #endif /* USE_JPWL */
5430
5431         /* look for the tile in the list of already processed tile (in parts). */
5432         /* Optimization possible here with a more complex data structure and with the removing of tiles */
5433         /* since the time taken by this function can only grow at the time */
5434
5435         opj_read_bytes(p_header_data,&l_tot_len,4);             /* Psot */
5436         p_header_data+=4;
5437
5438         /* PSot should be equal to zero or >=14 or <= 2^32-1 */
5439         if ((l_tot_len !=0 ) && (l_tot_len < 14) )
5440         {
5441                 opj_event_msg_v2(p_manager, EVT_ERROR, "Psot value (%d) is not correct regards to the JPEG2000 norm!\n", l_tot_len);
5442                 return OPJ_FALSE;
5443         }
5444
5445
5446 #ifdef USE_JPWL
5447         if (l_cp->correct) {
5448
5449                 /* totlen is negative or larger than the bytes left!!! */
5450                 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))) { */
5451                         opj_event_msg_v2(p_manager, EVT_ERROR,
5452                                 "JPWL: bad tile byte size (%d bytes against %d bytes left)\n",
5453                                 l_tot_len, p_header_size ); /* FIXME it seems correct; for info in V1 -> p_stream_numbytesleft(p_stream) + 8); */
5454                         if (!JPWL_ASSUME) {
5455                                 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
5456                                 return OPJ_FALSE;
5457                         }
5458                         /* we try to correct */
5459                         l_tot_len = 0;
5460                         opj_event_msg_v2(p_manager, EVT_WARNING, "- trying to adjust this\n"
5461                                 "- setting Psot to %d => assuming it is the last tile\n",
5462                                 l_tot_len);
5463                 }
5464         };
5465 #endif /* USE_JPWL */
5466
5467         /* Ref A.4.2: Psot could be equal zero if it is the last tile-part of the codestream.*/
5468         if (!l_tot_len) {
5469                 opj_event_msg_v2(p_manager, EVT_INFO, "Psot value of the current tile-part is equal to zero, "
5470                                 "we assuming it is the last tile-part of the codestream.\n");
5471                 p_j2k->m_specific_param.m_decoder.m_last_tile_part = 1;
5472         }
5473
5474         opj_read_bytes(p_header_data,&l_current_part ,1);       /* TPsot */
5475         ++p_header_data;
5476
5477         opj_read_bytes(p_header_data,&l_num_parts ,1);          /* TNsot */
5478         ++p_header_data;
5479
5480         if (l_num_parts != 0) { /* Number of tile-part header is provided by this tile-part header */
5481                 /* Useful to manage the case of textGBR.jp2 file because two values of TNSot are allowed: the correct numbers of
5482                  * tile-parts for that tile and zero (A.4.2 of 15444-1 : 2002). */
5483                 if (l_tcp->m_nb_tile_parts) {
5484                         if (l_current_part >= l_tcp->m_nb_tile_parts){
5485                                 opj_event_msg_v2(p_manager, EVT_ERROR, "In SOT marker, TPSot (%d) is not valid regards to the current "
5486                                                 "number of tile-part (%d), giving up\n", l_current_part, l_tcp->m_nb_tile_parts );
5487                                 p_j2k->m_specific_param.m_decoder.m_last_tile_part = 1;
5488                                 return OPJ_FALSE;
5489                         }
5490                 }
5491                 l_tcp->m_nb_tile_parts = l_num_parts;
5492         }
5493
5494         /* If know the number of tile part header we will check if we didn't read the last*/
5495         if (l_tcp->m_nb_tile_parts) {
5496                 if (l_tcp->m_nb_tile_parts == (l_current_part + 1)) {
5497                         p_j2k->m_specific_param.m_decoder.m_can_decode = 1; /* Process the last tile-part header*/
5498                 }
5499         }
5500
5501         if (!p_j2k->m_specific_param.m_decoder.m_last_tile_part){
5502                 /* Keep the size of data to skip after this marker */
5503                 p_j2k->m_specific_param.m_decoder.m_sot_length = l_tot_len - 12; /* SOT_marker_size = 12 */
5504         }
5505         else {
5506                 /* FIXME: need to be computed from the number of bytes remaining in the codestream */
5507                 p_j2k->m_specific_param.m_decoder.m_sot_length = 0;
5508         }
5509
5510         p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_TPH;
5511
5512         /* Check if the current tile is outside the area we want decode or not corresponding to the tile index*/
5513         if (p_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec == -1) {
5514                 p_j2k->m_specific_param.m_decoder.m_skip_data =
5515                                 (l_tile_x < p_j2k->m_specific_param.m_decoder.m_start_tile_x)
5516                         ||      (l_tile_x >= p_j2k->m_specific_param.m_decoder.m_end_tile_x)
5517                         ||  (l_tile_y < p_j2k->m_specific_param.m_decoder.m_start_tile_y)
5518                         ||      (l_tile_y >= p_j2k->m_specific_param.m_decoder.m_end_tile_y);
5519         }
5520   else {
5521       assert( p_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec >= 0 );
5522     p_j2k->m_specific_param.m_decoder.m_skip_data =
5523       (p_j2k->m_current_tile_number != (OPJ_UINT32)p_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec);
5524     }
5525
5526         /* Index */
5527         if (p_j2k->cstr_index)
5528         {
5529     assert(p_j2k->cstr_index->tile_index != 00);
5530                 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tileno = p_j2k->m_current_tile_number;
5531                 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_tpsno = l_current_part;
5532
5533                 if (l_num_parts != 0){
5534                         p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].nb_tps = l_num_parts;
5535                         p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps = l_num_parts;
5536
5537
5538                         if (!p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index)
5539                                 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index =
5540                                         (opj_tp_index_t*)opj_calloc(l_num_parts, sizeof(opj_tp_index_t));
5541                         else
5542                                 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index =
5543                                         (opj_tp_index_t*)opj_realloc(
5544                                                         p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index,
5545                                                         l_num_parts* sizeof(opj_tp_index_t));
5546                 }
5547                 else{
5548                         /*if (!p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index)*/ {
5549
5550                                 if (!p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index) {
5551                                         p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps = 10;
5552                                         p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index =
5553                                                 (opj_tp_index_t*)opj_calloc( p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps,
5554                                                                                                          sizeof(opj_tp_index_t));
5555                                 }
5556
5557                                 if ( l_current_part >= p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps ){
5558                                         p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps += 10;
5559                                         p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index =
5560                                                 (opj_tp_index_t*)opj_realloc( p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index,
5561                                                                                                           p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps
5562                                                                                                           * sizeof(opj_tp_index_t));
5563                                 }
5564                         }
5565
5566                 }
5567
5568         }
5569
5570
5571         /* 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 */
5572         /* if (p_j2k->cstr_info) {
5573                 if (l_tcp->first) {
5574                         if (tileno == 0) {
5575                                 p_j2k->cstr_info->main_head_end = p_stream_tell(p_stream) - 13;
5576                         }
5577
5578                         p_j2k->cstr_info->tile[tileno].tileno = tileno;
5579                         p_j2k->cstr_info->tile[tileno].start_pos = p_stream_tell(p_stream) - 12;
5580                         p_j2k->cstr_info->tile[tileno].end_pos = p_j2k->cstr_info->tile[tileno].start_pos + totlen - 1;
5581                         p_j2k->cstr_info->tile[tileno].num_tps = numparts;
5582
5583                         if (numparts) {
5584                                 p_j2k->cstr_info->tile[tileno].tp = (opj_tp_info_t *) opj_malloc(numparts * sizeof(opj_tp_info_t));
5585                         }
5586                         else {
5587                                 p_j2k->cstr_info->tile[tileno].tp = (opj_tp_info_t *) opj_malloc(10 * sizeof(opj_tp_info_t)); // Fixme (10)
5588                         }
5589                 }
5590                 else {
5591                         p_j2k->cstr_info->tile[tileno].end_pos += totlen;
5592                 }
5593
5594                 p_j2k->cstr_info->tile[tileno].tp[partno].tp_start_pos = p_stream_tell(p_stream) - 12;
5595                 p_j2k->cstr_info->tile[tileno].tp[partno].tp_end_pos =
5596                 p_j2k->cstr_info->tile[tileno].tp[partno].tp_start_pos + totlen - 1;
5597         }*/
5598         return OPJ_TRUE;
5599 }
5600
5601 static void j2k_write_sod(opj_j2k_t *j2k, void *tile_coder) {
5602         int l, layno;
5603         int totlen;
5604         opj_tcp_t *tcp = NULL;
5605         opj_codestream_info_t *cstr_info = NULL;
5606         
5607         opj_tcd_t *tcd = (opj_tcd_t*)tile_coder;        /* cast is needed because of conflicts in header inclusions */
5608         opj_cp_t *cp = j2k->cp;
5609         opj_cio_t *cio = j2k->cio;
5610
5611         tcd->tp_num = j2k->tp_num ;
5612         tcd->cur_tp_num = j2k->cur_tp_num;
5613         
5614         cio_write(cio, J2K_MS_SOD, 2);
5615
5616         if( j2k->cstr_info && j2k->cur_tp_num==0){
5617           j2k_add_tlmarker( j2k->curtileno, j2k->cstr_info, J2K_MS_SOD, cio_tell(cio), 0);
5618         }
5619
5620         if (j2k->curtileno == 0) {
5621                 j2k->sod_start = cio_tell(cio) + j2k->pos_correction;
5622         }
5623
5624         /* INDEX >> */
5625         cstr_info = j2k->cstr_info;
5626         if (cstr_info) {
5627                 if (!j2k->cur_tp_num ) {
5628                         cstr_info->tile[j2k->curtileno].end_header = cio_tell(cio) + j2k->pos_correction - 1;
5629                         j2k->cstr_info->tile[j2k->curtileno].tileno = j2k->curtileno;
5630                 }
5631                 else{
5632                         if(cstr_info->tile[j2k->curtileno].packet[cstr_info->packno - 1].end_pos < cio_tell(cio))
5633                                 cstr_info->tile[j2k->curtileno].packet[cstr_info->packno].start_pos = cio_tell(cio);
5634                 }
5635                 /* UniPG>> */
5636 #ifdef USE_JPWL
5637                 /* update markers struct */
5638                 j2k_add_marker(j2k->cstr_info, J2K_MS_SOD, j2k->sod_start, 2);
5639 #endif /* USE_JPWL */
5640                 /* <<UniPG */
5641         }
5642         /* << INDEX */
5643         
5644         tcp = &cp->tcps[j2k->curtileno];
5645         for (layno = 0; layno < tcp->numlayers; layno++) {
5646                 if (tcp->rates[layno]>(j2k->sod_start / (cp->th * cp->tw))) {
5647                         tcp->rates[layno]-=(j2k->sod_start / (cp->th * cp->tw));
5648                 } else if (tcp->rates[layno]) {
5649                         tcp->rates[layno]=1;
5650                 }
5651         }
5652         if(j2k->cur_tp_num == 0){
5653                 tcd->tcd_image->tiles->packno = 0;
5654                 if(cstr_info)
5655                         cstr_info->packno = 0;
5656         }
5657         
5658         l = tcd_encode_tile(tcd, j2k->curtileno, cio_getbp(cio), cio_numbytesleft(cio) - 2, cstr_info);
5659         
5660         /* Writing Psot in SOT marker */
5661         totlen = cio_tell(cio) + l - j2k->sot_start;
5662         cio_seek(cio, j2k->sot_start + 6);
5663         cio_write(cio, totlen, 4);
5664         cio_seek(cio, j2k->sot_start + totlen);
5665         /* Writing Ttlm and Ptlm in TLM marker */
5666         if(cp->cinema){
5667                 cio_seek(cio, j2k->tlm_start + 6 + (5*j2k->cur_tp_num));
5668                 cio_write(cio, j2k->curtileno, 1);
5669                 cio_write(cio, totlen, 4);
5670         }
5671         cio_seek(cio, j2k->sot_start + totlen);
5672 }
5673
5674 /**
5675  * Writes the SOD marker (Start of data)
5676  *
5677  * @param       p_stream                                the stream to write data to.
5678  * @param       p_j2k                           J2K codec.
5679  * @param       p_manager               the user event manager.
5680 */
5681 opj_bool j2k_write_sod_v2(      opj_j2k_v2_t *p_j2k,
5682                                                         struct opj_tcd_v2 * p_tile_coder,
5683                                                         OPJ_BYTE * p_data,
5684                                                         OPJ_UINT32 * p_data_written,
5685                                                         OPJ_UINT32 p_total_data_size,
5686                                                         const struct opj_stream_private *p_stream,
5687                                                         struct opj_event_mgr * p_manager )
5688 {
5689         opj_tcp_v2_t *l_tcp = 00;
5690         opj_codestream_info_t *l_cstr_info = 00;
5691         opj_cp_v2_t *l_cp = 00;
5692
5693         OPJ_UINT32 l_size_tile;
5694         OPJ_UINT32 l_remaining_data;
5695
5696         /* preconditions */
5697         assert(p_j2k != 00);
5698         assert(p_manager != 00);
5699         assert(p_stream != 00);
5700
5701         opj_write_bytes(p_data,J2K_MS_SOD,2);                                   /* SOD */
5702         p_data += 2;
5703
5704         /* make room for the EOF marker */
5705         l_remaining_data =  p_total_data_size - 4;
5706
5707         l_cp = &(p_j2k->m_cp);
5708         l_tcp = &l_cp->tcps[p_j2k->m_current_tile_number];
5709
5710
5711         /* update tile coder */
5712         p_tile_coder->tp_num = p_j2k->m_specific_param.m_encoder.m_current_poc_tile_part_number ;
5713         p_tile_coder->cur_tp_num = p_j2k->m_specific_param.m_encoder.m_current_tile_part_number;
5714
5715         l_size_tile = l_cp->th * l_cp->tw;
5716
5717         /* INDEX >> */
5718         /* TODO mergeV2: check this part which use cstr_info */
5719         /*l_cstr_info = p_j2k->cstr_info;
5720         if (l_cstr_info) {
5721                 if (!p_j2k->m_specific_param.m_encoder.m_current_tile_part_number ) {
5722                         //TODO cstr_info->tile[p_j2k->m_current_tile_number].end_header = p_stream_tell(p_stream) + p_j2k->pos_correction - 1;
5723                         l_cstr_info->tile[p_j2k->m_current_tile_number].tileno = p_j2k->m_current_tile_number;
5724                 }
5725                 else {*/
5726                         /*
5727                         TODO
5728                         if
5729                                 (cstr_info->tile[p_j2k->m_current_tile_number].packet[cstr_info->packno - 1].end_pos < p_stream_tell(p_stream))
5730                         {
5731                                 cstr_info->tile[p_j2k->m_current_tile_number].packet[cstr_info->packno].start_pos = p_stream_tell(p_stream);
5732                         }*/
5733                 /*}*/
5734                 /* UniPG>> */
5735 #ifdef USE_JPWL
5736                 /* update markers struct */
5737                 /*j2k_add_marker(p_j2k->cstr_info, J2K_MS_SOD, p_j2k->sod_start, 2);
5738 */
5739   assert( 0 && "TODO" );
5740 #endif /* USE_JPWL */
5741                 /* <<UniPG */
5742         /*}*/
5743         /* << INDEX */
5744
5745         if (p_j2k->m_specific_param.m_encoder.m_current_tile_part_number == 0) {
5746                 p_tile_coder->tcd_image->tiles->packno = 0;
5747                 if (l_cstr_info) {
5748                         l_cstr_info->packno = 0;
5749                 }
5750         }
5751
5752         *p_data_written = 0;
5753
5754         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)) {
5755                 opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot encode tile\n");
5756                 return OPJ_FALSE;
5757         }
5758
5759         *p_data_written += 2;
5760
5761         return OPJ_TRUE;
5762 }
5763
5764
5765 static void j2k_read_sod(opj_j2k_t *j2k) {
5766         int len, truncate = 0, i;
5767         unsigned char *data = NULL, *data_ptr = NULL;
5768
5769         opj_cio_t *cio = j2k->cio;
5770         int curtileno = j2k->curtileno;
5771
5772         /* Index */
5773         if (j2k->cstr_info) {
5774                 j2k->cstr_info->tile[j2k->curtileno].tp[j2k->cur_tp_num].tp_end_header =
5775                         cio_tell(cio) + j2k->pos_correction - 1;
5776                 if (j2k->cur_tp_num == 0)
5777                         j2k->cstr_info->tile[j2k->curtileno].end_header = cio_tell(cio) + j2k->pos_correction - 1;
5778                 j2k->cstr_info->packno = 0;
5779         }
5780         
5781         len = int_min(j2k->eot - cio_getbp(cio), cio_numbytesleft(cio) + 1);
5782
5783         if (len == cio_numbytesleft(cio) + 1) {
5784                 truncate = 1;           /* Case of a truncate codestream */
5785         }       
5786
5787         data = j2k->tile_data[curtileno];
5788         data = (unsigned char*) opj_realloc(data, (j2k->tile_len[curtileno] + len) * sizeof(unsigned char));
5789
5790         data_ptr = data + j2k->tile_len[curtileno];
5791         for (i = 0; i < len; i++) {
5792                 data_ptr[i] = cio_read(cio, 1);
5793         }
5794
5795         j2k->tile_len[curtileno] += len;
5796         j2k->tile_data[curtileno] = data;
5797         
5798         if (!truncate) {
5799                 j2k->state = J2K_STATE_TPHSOT;
5800         } else {
5801                 j2k->state = J2K_STATE_NEOC;    /* RAJOUTE !! */
5802         }
5803         j2k->cur_tp_num++;
5804 }
5805
5806 /**
5807  * Reads a SOD marker (Start Of Data)
5808  *
5809  * @param       p_header_data   the data contained in the SOD box.
5810  * @param       p_j2k                   the jpeg2000 codec.
5811  * @param       p_header_size   the size of the data contained in the SOD marker.
5812  * @param       p_manager               the user event manager.
5813 */
5814 opj_bool j2k_read_sod_v2 (
5815                                                 opj_j2k_v2_t *p_j2k,
5816                                                 struct opj_stream_private *p_stream,
5817                                                 struct opj_event_mgr * p_manager
5818                                         )
5819 {
5820         OPJ_UINT32 l_current_read_size;
5821         opj_codestream_index_t * l_cstr_index = 00;
5822         OPJ_BYTE ** l_current_data = 00;
5823         opj_tcp_v2_t * l_tcp = 00;
5824         OPJ_UINT32 * l_tile_len = 00;
5825
5826         /* preconditions */
5827         assert(p_j2k != 00);
5828         assert(p_manager != 00);
5829         assert(p_stream != 00);
5830
5831         l_tcp = &(p_j2k->m_cp.tcps[p_j2k->m_current_tile_number]);
5832
5833         if (p_j2k->m_specific_param.m_decoder.m_last_tile_part) {
5834                 /* opj_stream_get_number_byte_left returns OPJ_OFF_T
5835                 // but we are in the last tile part,
5836                 // so its result will fit on OPJ_UINT32 unless we find
5837                 // a file with a single tile part of more than 4 GB...*/
5838                 p_j2k->m_specific_param.m_decoder.m_sot_length = (OPJ_UINT32)(opj_stream_get_number_byte_left(p_stream) - 2);
5839         }
5840         else
5841                 p_j2k->m_specific_param.m_decoder.m_sot_length -= 2;
5842
5843         l_current_data = &(l_tcp->m_data);
5844         l_tile_len = &l_tcp->m_data_size;
5845
5846         if (! *l_current_data) {
5847                 *l_current_data = (OPJ_BYTE*) opj_malloc(p_j2k->m_specific_param.m_decoder.m_sot_length);
5848         }
5849         else {
5850                 *l_current_data = (OPJ_BYTE*) opj_realloc(*l_current_data, *l_tile_len + p_j2k->m_specific_param.m_decoder.m_sot_length);
5851         }
5852
5853         if (*l_current_data == 00) {
5854                 opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot decode tile\n");
5855                 return OPJ_FALSE;
5856         }
5857
5858
5859         /* Index */
5860         l_cstr_index = p_j2k->cstr_index;
5861         if (l_cstr_index) {
5862                 OPJ_OFF_T l_current_pos = opj_stream_tell(p_stream) - 2;
5863
5864                 OPJ_UINT32 l_current_tile_part = l_cstr_index->tile_index[p_j2k->m_current_tile_number].current_tpsno;
5865                 l_cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index[l_current_tile_part].end_header =
5866                                 l_current_pos;
5867                 l_cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index[l_current_tile_part].end_pos =
5868                                 l_current_pos + p_j2k->m_specific_param.m_decoder.m_sot_length + 2;
5869
5870                 j2k_add_tlmarker_v2(p_j2k->m_current_tile_number,
5871                                                         l_cstr_index,
5872                                                         J2K_MS_SOD,
5873                                                         l_current_pos,
5874                                                         p_j2k->m_specific_param.m_decoder.m_sot_length + 2);
5875
5876                 /*l_cstr_index->packno = 0;*/
5877         }
5878
5879         l_current_read_size = opj_stream_read_data(     p_stream,
5880                                                                                                 *l_current_data + *l_tile_len,
5881                                                                                                 p_j2k->m_specific_param.m_decoder.m_sot_length,
5882                                                                                                 p_manager);
5883
5884         if (l_current_read_size != p_j2k->m_specific_param.m_decoder.m_sot_length) {
5885                 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_NEOC;
5886         }
5887         else {
5888                 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_TPHSOT;
5889         }
5890
5891         *l_tile_len +=  l_current_read_size;
5892
5893         return OPJ_TRUE;
5894 }
5895
5896
5897 static void j2k_write_rgn(opj_j2k_t *j2k, int compno, int tileno) {
5898         opj_cp_t *cp = j2k->cp;
5899         opj_tcp_t *tcp = &cp->tcps[tileno];
5900         opj_cio_t *cio = j2k->cio;
5901         int numcomps = j2k->image->numcomps;
5902         
5903         cio_write(cio, J2K_MS_RGN, 2);                                          /* RGN  */
5904         cio_write(cio, numcomps <= 256 ? 5 : 6, 2);                     /* Lrgn */
5905         cio_write(cio, compno, numcomps <= 256 ? 1 : 2);        /* Crgn */
5906         cio_write(cio, 0, 1);                                                           /* Srgn */
5907         cio_write(cio, tcp->tccps[compno].roishift, 1);         /* SPrgn */
5908 }
5909
5910 /**
5911  * Writes the RGN marker (Region Of Interest)
5912  *
5913  * @param       p_tile_no               the tile to output
5914  * @param       p_comp_no               the component to output
5915  * @param       p_stream                                the stream to write data to.
5916  * @param       p_j2k                           J2K codec.
5917  * @param       p_manager               the user event manager.
5918 */
5919 opj_bool j2k_write_rgn_v2(      opj_j2k_v2_t *p_j2k,
5920                                                         OPJ_UINT32 p_tile_no,
5921                                                         OPJ_UINT32 p_comp_no,
5922                                                         struct opj_stream_private *p_stream,
5923                                                         struct opj_event_mgr * p_manager )
5924 {
5925         OPJ_BYTE * l_current_data = 00;
5926         OPJ_UINT32 l_nb_comp;
5927         OPJ_UINT32 l_rgn_size;
5928         opj_image_t *l_image = 00;
5929         opj_cp_v2_t *l_cp = 00;
5930         opj_tcp_v2_t *l_tcp = 00;
5931         opj_tccp_t *l_tccp = 00;
5932         OPJ_UINT32 l_comp_room;
5933
5934         /* preconditions */
5935         assert(p_j2k != 00);
5936         assert(p_manager != 00);
5937         assert(p_stream != 00);
5938
5939         l_cp = &(p_j2k->m_cp);
5940         l_tcp = &l_cp->tcps[p_tile_no];
5941         l_tccp = &l_tcp->tccps[p_comp_no];
5942
5943         l_nb_comp = l_image->numcomps;
5944
5945         if (l_nb_comp <= 256) {
5946                 l_comp_room = 1;
5947         }
5948         else {
5949                 l_comp_room = 2;
5950         }
5951
5952         l_rgn_size = 6 + l_comp_room;
5953
5954         l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
5955
5956         opj_write_bytes(l_current_data,J2K_MS_RGN,2);                                   /* RGN  */
5957         l_current_data += 2;
5958
5959         opj_write_bytes(l_current_data,l_rgn_size-2,2);                                 /* Lrgn */
5960         l_current_data += 2;
5961
5962         opj_write_bytes(l_current_data,p_comp_no,l_comp_room);                  /* Crgn */
5963         l_current_data+=l_comp_room;
5964
5965         opj_write_bytes(l_current_data, 0,1);                                                   /* Srgn */
5966         ++l_current_data;
5967
5968         opj_write_bytes(l_current_data, l_tccp->roishift,1);                    /* SPrgn */
5969         ++l_current_data;
5970
5971         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) {
5972                 return OPJ_FALSE;
5973         }
5974
5975         return OPJ_TRUE;
5976 }
5977
5978 static void j2k_read_rgn(opj_j2k_t *j2k) {
5979         int len, compno, roisty;
5980
5981         opj_cp_t *cp = j2k->cp;
5982         opj_tcp_t *tcp = j2k->state == J2K_STATE_TPH ? &cp->tcps[j2k->curtileno] : j2k->default_tcp;
5983         opj_cio_t *cio = j2k->cio;
5984         int numcomps = j2k->image->numcomps;
5985
5986         len = cio_read(cio, 2);                                                                         /* Lrgn */
5987         compno = cio_read(cio, numcomps <= 256 ? 1 : 2);                        /* Crgn */
5988         roisty = cio_read(cio, 1);                                                                      /* Srgn */
5989
5990 #ifdef USE_JPWL
5991         if (j2k->cp->correct) {
5992                 /* totlen is negative or larger than the bytes left!!! */
5993                 if (compno >= numcomps) {
5994                         opj_event_msg(j2k->cinfo, EVT_ERROR,
5995                                 "JPWL: bad component number in RGN (%d when there are only %d)\n",
5996                                 compno, numcomps);
5997                         if (!JPWL_ASSUME || JPWL_ASSUME) {
5998                                 opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
5999                                 return;
6000                         }
6001                 }
6002         };
6003 #endif /* USE_JPWL */
6004
6005         tcp->tccps[compno].roishift = cio_read(cio, 1);                         /* SPrgn */
6006 }
6007
6008 static void j2k_write_eoc(opj_j2k_t *j2k) {
6009         opj_cio_t *cio = j2k->cio;
6010         /* opj_event_msg(j2k->cinfo, "%.8x: EOC\n", cio_tell(cio) + j2k->pos_correction); */
6011         cio_write(cio, J2K_MS_EOC, 2);
6012
6013 /* UniPG>> */
6014 #ifdef USE_JPWL
6015         /* update markers struct */
6016         j2k_add_marker(j2k->cstr_info, J2K_MS_EOC, cio_tell(cio) - 2, 2);
6017 #endif /* USE_JPWL */
6018 /* <<UniPG */
6019 }
6020
6021 /**
6022  * Writes the EOC marker (End of Codestream)
6023  * 
6024  * @param       p_stream                the stream to write data to.
6025  * @param       p_j2k                   J2K codec.
6026  * @param       p_manager               the user event manager.
6027 */
6028 opj_bool j2k_write_eoc_v2(      opj_j2k_v2_t *p_j2k,
6029                                                         struct opj_stream_private *p_stream,
6030                                                         struct opj_event_mgr * p_manager )
6031 {
6032         /* preconditions */
6033         assert(p_j2k != 00);
6034         assert(p_manager != 00);
6035         assert(p_stream != 00);
6036         
6037         opj_write_bytes(p_j2k->m_specific_param.m_encoder.m_header_tile_data,J2K_MS_EOC,2);                                     /* EOC */
6038         
6039
6040 /* UniPG>> */
6041 #ifdef USE_JPWL
6042         /* update markers struct */
6043         /*
6044         j2k_add_marker(p_j2k->cstr_info, J2K_MS_EOC, p_stream_tell(p_stream) - 2, 2);
6045 */
6046 #endif /* USE_JPWL */
6047
6048         if ( opj_stream_write_data(p_stream,p_j2k->m_specific_param.m_encoder.m_header_tile_data,2,p_manager) != 2) {
6049                 return OPJ_FALSE;
6050         }
6051
6052         if ( ! opj_stream_flush(p_stream,p_manager) ) {
6053                 return OPJ_FALSE;
6054         }
6055
6056         return OPJ_TRUE;
6057 }
6058
6059
6060 /**
6061  * Reads a RGN marker (Region Of Interest)
6062  *
6063  * @param       p_header_data   the data contained in the POC box.
6064  * @param       p_j2k                   the jpeg2000 codec.
6065  * @param       p_header_size   the size of the data contained in the POC marker.
6066  * @param       p_manager               the user event manager.
6067 */
6068 opj_bool j2k_read_rgn_v2 (
6069                                                 opj_j2k_v2_t *p_j2k,
6070                                                 OPJ_BYTE * p_header_data,
6071                                                 OPJ_UINT32 p_header_size,
6072                                                 struct opj_event_mgr * p_manager
6073                                         )
6074 {
6075         OPJ_UINT32 l_nb_comp;
6076         opj_image_t * l_image = 00;
6077
6078         opj_cp_v2_t *l_cp = 00;
6079         opj_tcp_v2_t *l_tcp = 00;
6080         OPJ_UINT32 l_comp_room, l_comp_no, l_roi_sty;
6081
6082         /* preconditions*/
6083         assert(p_header_data != 00);
6084         assert(p_j2k != 00);
6085         assert(p_manager != 00);
6086
6087         l_image = p_j2k->m_private_image;
6088         l_nb_comp = l_image->numcomps;
6089
6090         if (l_nb_comp <= 256) {
6091                 l_comp_room = 1; }
6092         else {
6093                 l_comp_room = 2; }
6094
6095         if (p_header_size != 2 + l_comp_room) {
6096                 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading RGN marker\n");
6097                 return OPJ_FALSE;
6098         }
6099
6100         l_cp = &(p_j2k->m_cp);
6101         l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ?
6102                                 &l_cp->tcps[p_j2k->m_current_tile_number] :
6103                                 p_j2k->m_specific_param.m_decoder.m_default_tcp;
6104
6105         opj_read_bytes(p_header_data,&l_comp_no,l_comp_room);           /* Crgn */
6106         p_header_data+=l_comp_room;
6107         opj_read_bytes(p_header_data,&l_roi_sty,1);                                     /* Srgn */
6108         ++p_header_data;
6109
6110 #ifdef USE_JPWL
6111         if (l_cp->correct) {
6112                 /* totlen is negative or larger than the bytes left!!! */
6113                 if (l_comp_room >= l_nb_comp) {
6114                         opj_event_msg_v2(p_manager, EVT_ERROR,
6115                                 "JPWL: bad component number in RGN (%d when there are only %d)\n",
6116                                 l_comp_room, l_nb_comp);
6117                         if (!JPWL_ASSUME || JPWL_ASSUME) {
6118                                 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
6119                                 return OPJ_FALSE;
6120                         }
6121                 }
6122         };
6123 #endif /* USE_JPWL */
6124
6125         opj_read_bytes(p_header_data,(OPJ_UINT32 *) (&(l_tcp->tccps[l_comp_no].roishift)),1);   /* SPrgn */
6126         ++p_header_data;
6127
6128         return OPJ_TRUE;
6129
6130 }
6131
6132 static OPJ_FLOAT32 get_tp_stride (opj_tcp_v2_t * p_tcp)
6133 {
6134         return (OPJ_FLOAT32) ((p_tcp->m_nb_tile_parts - 1) * 14);
6135 }
6136
6137 static OPJ_FLOAT32 get_default_stride (opj_tcp_v2_t * p_tcp)
6138 {
6139   (void)p_tcp;
6140         return 0;
6141 }
6142
6143 /**
6144  * Updates the rates of the tcp.
6145  *
6146  * @param       p_stream                the stream to write data to.
6147  * @param       p_j2k                   J2K codec.
6148  * @param       p_manager               the user event manager.
6149 */
6150 opj_bool j2k_update_rates(      opj_j2k_v2_t *p_j2k,
6151                                                         struct opj_stream_private *p_stream,
6152                                                         struct opj_event_mgr * p_manager )
6153 {
6154         opj_cp_v2_t * l_cp = 00;
6155         opj_image_t * l_image = 00;
6156         opj_tcp_v2_t * l_tcp = 00;
6157         opj_image_comp_t * l_img_comp = 00;
6158
6159         OPJ_UINT32 i,j,k;
6160         OPJ_INT32 l_x0,l_y0,l_x1,l_y1;
6161         OPJ_FLOAT32 * l_rates = 0;
6162         OPJ_FLOAT32 l_sot_remove;
6163         OPJ_UINT32 l_bits_empty, l_size_pixel;
6164         OPJ_UINT32 l_tile_size = 0;
6165         OPJ_UINT32 l_last_res;
6166         OPJ_FLOAT32 (* l_tp_stride_func)(opj_tcp_v2_t *) = 00;
6167
6168         /* preconditions */
6169         assert(p_j2k != 00);
6170         assert(p_manager != 00);
6171         assert(p_stream != 00);
6172
6173
6174         l_cp = &(p_j2k->m_cp);
6175         l_image = p_j2k->m_private_image;
6176         l_tcp = l_cp->tcps;
6177
6178         l_bits_empty = 8 * l_image->comps->dx * l_image->comps->dy;
6179         l_size_pixel = l_image->numcomps * l_image->comps->prec;
6180         l_sot_remove = ((OPJ_FLOAT32) opj_stream_tell(p_stream)) / (l_cp->th * l_cp->tw);
6181
6182         if (l_cp->m_specific_param.m_enc.m_tp_on) {
6183                 l_tp_stride_func = get_tp_stride;
6184         }
6185         else {
6186                 l_tp_stride_func = get_default_stride;
6187         }
6188
6189         for (i=0;i<l_cp->th;++i) {
6190                 for (j=0;j<l_cp->tw;++j) {
6191                         OPJ_FLOAT32 l_offset = ((*l_tp_stride_func)(l_tcp)) / l_tcp->numlayers;
6192
6193                         /* 4 borders of the tile rescale on the image if necessary */
6194                         l_x0 = int_max(l_cp->tx0 + j * l_cp->tdx, l_image->x0);
6195                         l_y0 = int_max(l_cp->ty0 + i * l_cp->tdy, l_image->y0);
6196                         l_x1 = int_min(l_cp->tx0 + (j + 1) * l_cp->tdx, l_image->x1);
6197                         l_y1 = int_min(l_cp->ty0 + (i + 1) * l_cp->tdy, l_image->y1);
6198
6199                         l_rates = l_tcp->rates;
6200
6201                         /* Modification of the RATE >> */
6202                         if (*l_rates) {
6203                                 *l_rates =              (( (float) (l_size_pixel * (l_x1 - l_x0) * (l_y1 - l_y0)))
6204                                                                 /
6205                                                                 ((*l_rates) * l_bits_empty)
6206                                                                 )
6207                                                                 -
6208                                                                 l_offset;
6209                         }
6210
6211                         ++l_rates;
6212
6213                         for (k = 1; k < l_tcp->numlayers; ++k) {
6214                                 if (*l_rates) {
6215                                         *l_rates =              (( (OPJ_FLOAT32) (l_size_pixel * (l_x1 - l_x0) * (l_y1 - l_y0)))
6216                                                                         /
6217                                                                                 ((*l_rates) * l_bits_empty)
6218                                                                         )
6219                                                                         -
6220                                                                         l_offset;
6221                                 }
6222
6223                                 ++l_rates;
6224                         }
6225
6226                         ++l_tcp;
6227
6228                 }
6229         }
6230
6231         l_tcp = l_cp->tcps;
6232
6233         for (i=0;i<l_cp->th;++i) {
6234                 for     (j=0;j<l_cp->tw;++j) {
6235                         l_rates = l_tcp->rates;
6236
6237                         if (*l_rates) {
6238                                 *l_rates -= l_sot_remove;
6239
6240                                 if (*l_rates < 30) {
6241                                         *l_rates = 30;
6242                                 }
6243                         }
6244
6245                         ++l_rates;
6246
6247                         l_last_res = l_tcp->numlayers - 1;
6248
6249                         for (k = 1; k < l_last_res; ++k) {
6250
6251                                 if (*l_rates) {
6252                                         *l_rates -= l_sot_remove;
6253
6254                                         if (*l_rates < *(l_rates - 1) + 10) {
6255                                                 *l_rates  = (*(l_rates - 1)) + 20;
6256                                         }
6257                                 }
6258
6259                                 ++l_rates;
6260                         }
6261
6262                         if (*l_rates) {
6263                                 *l_rates -= (l_sot_remove + 2.f);
6264
6265                                 if (*l_rates < *(l_rates - 1) + 10) {
6266                                         *l_rates  = (*(l_rates - 1)) + 20;
6267                                 }
6268                         }
6269
6270                         ++l_tcp;
6271                 }
6272         }
6273
6274         l_img_comp = l_image->comps;
6275         l_tile_size = 0;
6276
6277         for (i=0;i<l_image->numcomps;++i) {
6278                 l_tile_size += (        uint_ceildiv(l_cp->tdx,l_img_comp->dx)
6279                                                         *
6280                                                         uint_ceildiv(l_cp->tdy,l_img_comp->dy)
6281                                                         *
6282                                                         l_img_comp->prec
6283                                                 );
6284
6285                 ++l_img_comp;
6286         }
6287
6288         l_tile_size = (OPJ_UINT32) (l_tile_size * 0.1625); /* 1.3/8 = 0.1625 */
6289
6290         l_tile_size += j2k_get_specific_header_sizes(p_j2k);
6291
6292         p_j2k->m_specific_param.m_encoder.m_encoded_tile_size = l_tile_size;
6293         p_j2k->m_specific_param.m_encoder.m_encoded_tile_data =
6294                         (OPJ_BYTE *) opj_malloc(p_j2k->m_specific_param.m_encoder.m_encoded_tile_size);
6295         if (p_j2k->m_specific_param.m_encoder.m_encoded_tile_data == 00) {
6296                 return OPJ_FALSE;
6297         }
6298
6299         if (l_cp->m_specific_param.m_enc.m_cinema) {
6300                 p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer =
6301                                 (OPJ_BYTE *) opj_malloc(5*p_j2k->m_specific_param.m_encoder.m_total_tile_parts);
6302                 if (! p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer) {
6303                         return OPJ_FALSE;
6304                 }
6305
6306                 p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current =
6307                                 p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer;
6308         }
6309
6310         return OPJ_TRUE;
6311 }
6312
6313 static void j2k_read_eoc(opj_j2k_t *j2k) {
6314         int i, tileno;
6315         opj_bool success = OPJ_FALSE;
6316
6317         /* if packets should be decoded */
6318         if (j2k->cp->limit_decoding != DECODE_ALL_BUT_PACKETS) {
6319                 opj_tcd_t *tcd = tcd_create(j2k->cinfo);
6320                 tcd_malloc_decode(tcd, j2k->image, j2k->cp);
6321                 for (i = 0; i < j2k->cp->tileno_size; i++) {
6322                         tcd_malloc_decode_tile(tcd, j2k->image, j2k->cp, i, j2k->cstr_info);
6323                         if (j2k->cp->tileno[i] != -1)
6324                         {
6325                                 tileno = j2k->cp->tileno[i];
6326                                 success = tcd_decode_tile(tcd, j2k->tile_data[tileno], j2k->tile_len[tileno], tileno, j2k->cstr_info);
6327                                 opj_free(j2k->tile_data[tileno]);
6328                                 j2k->tile_data[tileno] = NULL;
6329                                 tcd_free_decode_tile(tcd, i);
6330                         }
6331                         else
6332                                 success = OPJ_FALSE;
6333                         if (success == OPJ_FALSE) {
6334                                 j2k->state |= J2K_STATE_ERR;
6335                                 break;
6336                         }
6337                 }
6338                 tcd_free_decode(tcd);
6339                 tcd_destroy(tcd);
6340         }
6341         /* if packets should not be decoded  */
6342         else {
6343                 for (i = 0; i < j2k->cp->tileno_size; i++) {
6344                         tileno = j2k->cp->tileno[i];
6345                         opj_free(j2k->tile_data[tileno]);
6346                         j2k->tile_data[tileno] = NULL;
6347                 }
6348         }       
6349         if (j2k->state & J2K_STATE_ERR)
6350                 j2k->state = J2K_STATE_MT + J2K_STATE_ERR;
6351         else
6352                 j2k->state = J2K_STATE_MT; 
6353 }
6354
6355 /**
6356  * Reads a EOC marker (End Of Codestream)
6357  *
6358  * @param       p_header_data   the data contained in the SOD box.
6359  * @param       p_j2k                   the jpeg2000 codec.
6360  * @param       p_header_size   the size of the data contained in the SOD marker.
6361  * @param       p_manager               the user event manager.
6362 */
6363 #if 0
6364 opj_bool j2k_read_eoc_v2 (      opj_j2k_v2_t *p_j2k,
6365                                                         struct opj_stream_private *p_stream,
6366                                                         struct opj_event_mgr * p_manager )
6367 {
6368         OPJ_UINT32 i;
6369         opj_tcd_v2_t * l_tcd = 00;
6370         OPJ_UINT32 l_nb_tiles;
6371         opj_tcp_v2_t * l_tcp = 00;
6372         opj_bool l_success;
6373
6374         /* preconditions */
6375         assert(p_j2k != 00);
6376         assert(p_manager != 00);
6377         assert(p_stream != 00);
6378
6379         l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw;
6380         l_tcp = p_j2k->m_cp.tcps;
6381
6382         l_tcd = tcd_create_v2(OPJ_TRUE);
6383         if (l_tcd == 00) {
6384                 opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot decode tile, memory error\n");
6385                 return OPJ_FALSE;
6386         }
6387
6388         for (i = 0; i < l_nb_tiles; ++i) {
6389                 if (l_tcp->m_data) {
6390                         if (! tcd_init_decode_tile(l_tcd, i)) {
6391                                 tcd_destroy_v2(l_tcd);
6392                                 opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot decode tile, memory error\n");
6393                                 return OPJ_FALSE;
6394                         }
6395
6396                         l_success = tcd_decode_tile_v2(l_tcd, l_tcp->m_data, l_tcp->m_data_size, i, p_j2k->cstr_index);
6397                         /* cleanup */
6398
6399                         if (! l_success) {
6400                                 p_j2k->m_specific_param.m_decoder.m_state |= J2K_STATE_ERR;
6401                                 break;
6402                         }
6403                 }
6404
6405                 j2k_tcp_destroy(l_tcp);
6406                 ++l_tcp;
6407         }
6408
6409         tcd_destroy_v2(l_tcd);
6410         return OPJ_TRUE;
6411 }
6412 #endif
6413
6414 /**
6415  * Gets the offset of the header.
6416  *
6417  * @param       p_stream                                the stream to write data to.
6418  * @param       p_j2k                           J2K codec.
6419  * @param       p_manager               the user event manager.
6420 */
6421 opj_bool j2k_get_end_header(opj_j2k_v2_t *p_j2k,
6422                                                         struct opj_stream_private *p_stream,
6423                                                         struct opj_event_mgr * p_manager )
6424 {
6425         /* preconditions */
6426         assert(p_j2k != 00);
6427         assert(p_manager != 00);
6428         assert(p_stream != 00);
6429
6430         p_j2k->cstr_index->main_head_end = opj_stream_tell(p_stream);
6431
6432         return OPJ_TRUE;
6433 }
6434
6435 /**
6436  * Writes the MCT marker (Multiple Component Transform)
6437  *
6438  * @param       p_stream                                the stream to write data to.
6439  * @param       p_j2k                           J2K codec.
6440  * @param       p_manager               the user event manager.
6441 */
6442 opj_bool j2k_write_mct_data_group(      opj_j2k_v2_t *p_j2k,
6443                                                                         struct opj_stream_private *p_stream,
6444                                                                         struct opj_event_mgr * p_manager )
6445 {
6446         OPJ_UINT32 i;
6447         opj_simple_mcc_decorrelation_data_t * l_mcc_record;
6448         opj_mct_data_t * l_mct_record;
6449         opj_tcp_v2_t * l_tcp;
6450
6451         /* preconditions */
6452         assert(p_j2k != 00);
6453         assert(p_stream != 00);
6454         assert(p_manager != 00);
6455
6456         if (! j2k_write_cbd(p_j2k,p_stream,p_manager)) {
6457                 return OPJ_FALSE;
6458         }
6459
6460         l_tcp = &(p_j2k->m_cp.tcps[p_j2k->m_current_tile_number]);
6461         l_mct_record = l_tcp->m_mct_records;
6462
6463         for (i=0;i<l_tcp->m_nb_mct_records;++i) {
6464
6465                 if (! j2k_write_mct_record(p_j2k,l_mct_record,p_stream,p_manager)) {
6466                         return OPJ_FALSE;
6467                 }
6468
6469                 ++l_mct_record;
6470         }
6471
6472         l_mcc_record = l_tcp->m_mcc_records;
6473
6474         for     (i=0;i<l_tcp->m_nb_mcc_records;++i) {
6475
6476                 if (! j2k_write_mcc_record(p_j2k,l_mcc_record,p_stream,p_manager)) {
6477                         return OPJ_FALSE;
6478                 }
6479
6480                 ++l_mcc_record;
6481         }
6482
6483         if (! j2k_write_mco(p_j2k,p_stream,p_manager)) {
6484                 return OPJ_FALSE;
6485         }
6486
6487         return OPJ_TRUE;
6488 }
6489
6490 /**
6491  * Writes the image components.
6492  *
6493  * @param       p_stream                                the stream to write data to.
6494  * @param       p_j2k                           J2K codec.
6495  * @param       p_manager               the user event manager.
6496 */
6497 opj_bool j2k_write_image_components(opj_j2k_v2_t *p_j2k,
6498                                                                         struct opj_stream_private *p_stream,
6499                                                                         struct opj_event_mgr * p_manager )
6500 {
6501         OPJ_UINT32 compno;
6502
6503         /* preconditions */
6504         assert(p_j2k != 00);
6505         assert(p_manager != 00);
6506         assert(p_stream != 00);
6507
6508         for (compno = 1; compno < p_j2k->m_private_image->numcomps; ++compno)
6509         {
6510                 if (! j2k_write_coc_v2(p_j2k,compno,p_stream, p_manager)) {
6511                         return OPJ_FALSE;
6512                 }
6513
6514                 if (! j2k_write_qcc_v2(p_j2k,compno,p_stream, p_manager)) {
6515                         return OPJ_FALSE;
6516                 }
6517         }
6518
6519         return OPJ_TRUE;
6520 }
6521
6522 /**
6523  * Writes regions of interests.
6524  *
6525  * @param       p_stream                                the stream to write data to.
6526  * @param       p_j2k                           J2K codec.
6527  * @param       p_manager               the user event manager.
6528 */
6529 opj_bool j2k_write_regions(     opj_j2k_v2_t *p_j2k,
6530                                                         struct opj_stream_private *p_stream,
6531                                                         struct opj_event_mgr * p_manager )
6532 {
6533         OPJ_UINT32 compno;
6534         const opj_tccp_t *l_tccp = 00;
6535
6536         /* preconditions */
6537         assert(p_j2k != 00);
6538         assert(p_manager != 00);
6539         assert(p_stream != 00);
6540
6541         l_tccp = p_j2k->m_cp.tcps->tccps;
6542
6543         for     (compno = 0; compno < p_j2k->m_private_image->numcomps; ++compno)  {
6544                 if (l_tccp->roishift) {
6545
6546                         if (! j2k_write_rgn_v2(p_j2k,0,compno,p_stream,p_manager)) {
6547                                 return OPJ_FALSE;
6548                         }
6549                 }
6550
6551                 ++l_tccp;
6552         }
6553
6554         return OPJ_TRUE;
6555 }
6556
6557 /**
6558  * Writes EPC ????
6559  *
6560  * @param       p_stream                the stream to write data to.
6561  * @param       p_j2k                   J2K codec.
6562  * @param       p_manager               the user event manager.
6563 */
6564 opj_bool j2k_write_epc( opj_j2k_v2_t *p_j2k,
6565                                                 struct opj_stream_private *p_stream,
6566                                                 struct opj_event_mgr * p_manager )
6567 {
6568         opj_codestream_index_t * l_cstr_index = 00;
6569
6570         /* preconditions */
6571         assert(p_j2k != 00);
6572         assert(p_manager != 00);
6573         assert(p_stream != 00);
6574
6575         l_cstr_index = p_j2k->cstr_index;
6576         if (l_cstr_index) {
6577                 l_cstr_index->codestream_size = opj_stream_tell(p_stream);
6578                 /* UniPG>> */
6579                 /* The following adjustment is done to adjust the codestream size */
6580                 /* if SOD is not at 0 in the buffer. Useful in case of JP2, where */
6581                 /* the first bunch of bytes is not in the codestream              */
6582                 l_cstr_index->codestream_size -= l_cstr_index->main_head_start;
6583                 /* <<UniPG */
6584         }
6585
6586 #ifdef USE_JPWL
6587         /* preparation of JPWL marker segments */
6588 #if 0
6589         if(cp->epc_on) {
6590
6591                 /* encode according to JPWL */
6592                 jpwl_encode(p_j2k, p_stream, image);
6593
6594         }
6595 #endif
6596   assert( 0 && "TODO" );
6597 #endif /* USE_JPWL */
6598
6599         return OPJ_TRUE;
6600 }
6601
6602 typedef struct opj_dec_mstabent {
6603         /** marker value */
6604         int id;
6605         /** value of the state when the marker can appear */
6606         int states;
6607         /** action linked to the marker */
6608         void (*handler) (opj_j2k_t *j2k);
6609 } opj_dec_mstabent_t;
6610
6611 opj_dec_mstabent_t j2k_dec_mstab[] = {
6612   {J2K_MS_SOC, J2K_STATE_MHSOC, j2k_read_soc},
6613   {J2K_MS_SOT, J2K_STATE_MH | J2K_STATE_TPHSOT, j2k_read_sot},
6614   {J2K_MS_SOD, J2K_STATE_TPH, j2k_read_sod},
6615   {J2K_MS_EOC, J2K_STATE_TPHSOT, j2k_read_eoc},
6616   {J2K_MS_SIZ, J2K_STATE_MHSIZ, j2k_read_siz},
6617   {J2K_MS_COD, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_cod},
6618   {J2K_MS_COC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_coc},
6619   {J2K_MS_RGN, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_rgn},
6620   {J2K_MS_QCD, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_qcd},
6621   {J2K_MS_QCC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_qcc},
6622   {J2K_MS_POC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_poc},
6623   {J2K_MS_TLM, J2K_STATE_MH, j2k_read_tlm},
6624   {J2K_MS_PLM, J2K_STATE_MH, j2k_read_plm},
6625   {J2K_MS_PLT, J2K_STATE_TPH, j2k_read_plt},
6626   {J2K_MS_PPM, J2K_STATE_MH, j2k_read_ppm},
6627   {J2K_MS_PPT, J2K_STATE_TPH, j2k_read_ppt},
6628   {J2K_MS_SOP, 0, 0},
6629   {J2K_MS_CRG, J2K_STATE_MH, j2k_read_crg},
6630   {J2K_MS_COM, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_com},
6631
6632 #ifdef USE_JPWL
6633   {J2K_MS_EPC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_epc},
6634   {J2K_MS_EPB, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_epb},
6635   {J2K_MS_ESD, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_esd},
6636   {J2K_MS_RED, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_red},
6637 #endif /* USE_JPWL */
6638 #ifdef USE_JPSEC
6639   {J2K_MS_SEC, J2K_STATE_MH, j2k_read_sec},
6640   {J2K_MS_INSEC, 0, j2k_read_insec},
6641 #endif /* USE_JPSEC */
6642
6643   {0, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_unk}
6644 };
6645
6646 static void j2k_read_unk(opj_j2k_t *j2k) {
6647         opj_event_msg(j2k->cinfo, EVT_WARNING, "Unknown marker\n");
6648
6649 #ifdef USE_JPWL
6650         if (j2k->cp->correct) {
6651                 int m = 0, id, i;
6652                 int min_id = 0, min_dist = 17, cur_dist = 0, tmp_id;
6653                 cio_seek(j2k->cio, cio_tell(j2k->cio) - 2);
6654                 id = cio_read(j2k->cio, 2);
6655                 opj_event_msg(j2k->cinfo, EVT_ERROR,
6656                         "JPWL: really don't know this marker %x\n",
6657                         id);
6658                 if (!JPWL_ASSUME) {
6659                         opj_event_msg(j2k->cinfo, EVT_ERROR,
6660                                 "- possible synch loss due to uncorrectable codestream errors => giving up\n");
6661                         return;
6662                 }
6663                 /* OK, activate this at your own risk!!! */
6664                 /* we look for the marker at the minimum hamming distance from this */
6665                 while (j2k_dec_mstab[m].id) {
6666                         
6667                         /* 1's where they differ */
6668                         tmp_id = j2k_dec_mstab[m].id ^ id;
6669
6670                         /* compute the hamming distance between our id and the current */
6671                         cur_dist = 0;
6672                         for (i = 0; i < 16; i++) {
6673                                 if ((tmp_id >> i) & 0x0001) {
6674                                         cur_dist++;
6675                                 }
6676                         }
6677
6678                         /* if current distance is smaller, set the minimum */
6679                         if (cur_dist < min_dist) {
6680                                 min_dist = cur_dist;
6681                                 min_id = j2k_dec_mstab[m].id;
6682                         }
6683                         
6684                         /* jump to the next marker */
6685                         m++;
6686                 }
6687
6688                 /* do we substitute the marker? */
6689                 if (min_dist < JPWL_MAXIMUM_HAMMING) {
6690                         opj_event_msg(j2k->cinfo, EVT_ERROR,
6691                                 "- marker %x is at distance %d from the read %x\n",
6692                                 min_id, min_dist, id);
6693                         opj_event_msg(j2k->cinfo, EVT_ERROR,
6694                                 "- trying to substitute in place and crossing fingers!\n");
6695                         cio_seek(j2k->cio, cio_tell(j2k->cio) - 2);
6696                         cio_write(j2k->cio, min_id, 2);
6697
6698                         /* rewind */
6699                         cio_seek(j2k->cio, cio_tell(j2k->cio) - 2);
6700
6701                 }
6702
6703         };
6704 #endif /* USE_JPWL */
6705
6706 }
6707
6708 /**
6709  * Reads an unknown marker
6710  *
6711  * @param       p_stream                                the stream object to read from.
6712  * @param       p_j2k                   the jpeg2000 codec.
6713  * @param       p_manager               the user event manager.
6714  *
6715  * @return      true                    if the marker could be deduced.
6716 */
6717 opj_bool j2k_read_unk_v2 (      opj_j2k_v2_t *p_j2k,
6718                                                         struct opj_stream_private *p_stream,
6719                                                         OPJ_UINT32 *output_marker,
6720                                                         struct opj_event_mgr * p_manager
6721                                                         )
6722 {
6723         OPJ_UINT32 l_unknown_marker;
6724         const opj_dec_memory_marker_handler_t * l_marker_handler;
6725         OPJ_UINT32 l_size_unk = 2;
6726
6727         /* preconditions*/
6728         assert(p_j2k != 00);
6729         assert(p_manager != 00);
6730         assert(p_stream != 00);
6731
6732         opj_event_msg_v2(p_manager, EVT_WARNING, "Unknown marker\n");
6733
6734         while(1) {
6735                 /* Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer*/
6736                 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
6737                         opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
6738                         return OPJ_FALSE;
6739                 }
6740
6741                 /* read 2 bytes as the new marker ID*/
6742                 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_unknown_marker,2);
6743
6744                 if (!(l_unknown_marker < 0xff00)) {
6745
6746                         /* Get the marker handler from the marker ID*/
6747                         l_marker_handler = j2k_get_marker_handler(l_unknown_marker);
6748
6749                         if (!(p_j2k->m_specific_param.m_decoder.m_state & l_marker_handler->states)) {
6750                                 opj_event_msg_v2(p_manager, EVT_ERROR, "Marker is not compliant with its position\n");
6751                                 return OPJ_FALSE;
6752                         }
6753                         else {
6754                                 if (l_marker_handler->id != J2K_MS_UNK) {
6755                                         /* Add the marker to the codestream index*/
6756                                         if (l_marker_handler->id != J2K_MS_SOT)
6757                                                 j2k_add_mhmarker_v2(p_j2k->cstr_index, J2K_MS_UNK,
6758                                                                                         (OPJ_UINT32) opj_stream_tell(p_stream) - l_size_unk,
6759                                                                                         l_size_unk);
6760                                         break; /* next marker is known and well located */
6761                                 }
6762                                 else
6763                                         l_size_unk += 2;
6764                         }
6765                 }
6766         }
6767
6768         *output_marker = l_marker_handler->id ;
6769
6770         return OPJ_TRUE;
6771 }
6772
6773 /**
6774 Read the lookup table containing all the marker, status and action
6775 @param id Marker value
6776 */
6777 static opj_dec_mstabent_t *j2k_dec_mstab_lookup(int id) {
6778         opj_dec_mstabent_t *e;
6779         for (e = j2k_dec_mstab; e->id != 0; e++) {
6780                 if (e->id == id) {
6781                         break;
6782                 }
6783         }
6784         return e;
6785 }
6786
6787 /**
6788  * Writes the MCT marker (Multiple Component Transform)
6789  *
6790  * @param       p_stream                                the stream to write data to.
6791  * @param       p_j2k                           J2K codec.
6792  * @param       p_manager               the user event manager.
6793 */
6794 opj_bool j2k_write_mct_record(  opj_j2k_v2_t *p_j2k,
6795                                                                 opj_mct_data_t * p_mct_record,
6796                                                                 struct opj_stream_private *p_stream,
6797                                                                 struct opj_event_mgr * p_manager )
6798 {
6799         OPJ_UINT32 l_mct_size;
6800         OPJ_BYTE * l_current_data = 00;
6801         OPJ_UINT32 l_tmp;
6802
6803         /* preconditions */
6804         assert(p_j2k != 00);
6805         assert(p_manager != 00);
6806         assert(p_stream != 00);
6807
6808         l_mct_size = 10 + p_mct_record->m_data_size;
6809
6810         if (l_mct_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
6811                 p_j2k->m_specific_param.m_encoder.m_header_tile_data
6812                         = (OPJ_BYTE*)opj_realloc(
6813                                 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
6814                                 l_mct_size);
6815
6816                 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
6817                         return OPJ_FALSE;
6818                 }
6819
6820                 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_mct_size;
6821         }
6822
6823         l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
6824
6825         opj_write_bytes(l_current_data,J2K_MS_MCT,2);                                   /* MCT */
6826         l_current_data += 2;
6827
6828         opj_write_bytes(l_current_data,l_mct_size-2,2);                                 /* Lmct */
6829         l_current_data += 2;
6830
6831         opj_write_bytes(l_current_data,0,2);                                                    /* Zmct */
6832         l_current_data += 2;
6833
6834         /* only one marker atm */
6835         l_tmp = (p_mct_record->m_index & 0xff) | (p_mct_record->m_array_type << 8) | (p_mct_record->m_element_type << 10);
6836
6837         opj_write_bytes(l_current_data,l_tmp,2);
6838         l_current_data += 2;
6839
6840         opj_write_bytes(l_current_data,0,2);                                                    /* Ymct */
6841         l_current_data+=2;
6842
6843         memcpy(l_current_data,p_mct_record->m_data,p_mct_record->m_data_size);
6844
6845         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) {
6846                 return OPJ_FALSE;
6847         }
6848
6849         return OPJ_TRUE;
6850 }
6851
6852 /**
6853  * Reads a MCT marker (Multiple Component Transform)
6854  *
6855  * @param       p_header_data   the data contained in the MCT box.
6856  * @param       p_j2k                   the jpeg2000 codec.
6857  * @param       p_header_size   the size of the data contained in the MCT marker.
6858  * @param       p_manager               the user event manager.
6859 */
6860 opj_bool j2k_read_mct ( opj_j2k_v2_t *p_j2k,
6861                                                 OPJ_BYTE * p_header_data,
6862                                                 OPJ_UINT32 p_header_size,
6863                                                 struct opj_event_mgr * p_manager )
6864 {
6865         OPJ_UINT32 i;
6866         opj_tcp_v2_t *l_tcp = 00;
6867         OPJ_UINT32 l_tmp;
6868         OPJ_UINT32 l_indix;
6869         opj_mct_data_t * l_mct_data;
6870
6871         /* preconditions */
6872         assert(p_header_data != 00);
6873         assert(p_j2k != 00);
6874
6875         l_tcp = p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH ?
6876                         &p_j2k->m_cp.tcps[p_j2k->m_current_tile_number] :
6877                         p_j2k->m_specific_param.m_decoder.m_default_tcp;
6878
6879         if (p_header_size < 2) {
6880                 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCT marker\n");
6881                 return OPJ_FALSE;
6882         }
6883
6884         /* first marker */
6885         opj_read_bytes(p_header_data,&l_tmp,2);                         /* Zmct */
6886         p_header_data += 2;
6887         if (l_tmp != 0) {
6888                 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge mct data within multiple MCT records\n");
6889                 return OPJ_TRUE;
6890         }
6891
6892         if(p_header_size <= 6) {
6893                 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCT marker\n");
6894                 return OPJ_FALSE;
6895         }
6896
6897         /* Imct -> no need for other values, take the first, type is double with decorrelation x0000 1101 0000 0000*/
6898         opj_read_bytes(p_header_data,&l_tmp,2);                         /* Imct */
6899         p_header_data += 2;
6900
6901         l_indix = l_tmp & 0xff;
6902         l_mct_data = l_tcp->m_mct_records;
6903
6904         for (i=0;i<l_tcp->m_nb_mct_records;++i) {
6905                 if (l_mct_data->m_index == l_indix) {
6906                         break;
6907                 }
6908                 ++l_mct_data;
6909         }
6910
6911         /* NOT FOUND */
6912         if (i == l_tcp->m_nb_mct_records) {
6913                 if (l_tcp->m_nb_mct_records == l_tcp->m_nb_max_mct_records) {
6914                         l_tcp->m_nb_max_mct_records += J2K_MCT_DEFAULT_NB_RECORDS;
6915
6916                         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));
6917                         if(! l_tcp->m_mct_records) {
6918                                 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCT marker\n");
6919                                 return OPJ_FALSE;
6920                         }
6921
6922                         l_mct_data = l_tcp->m_mct_records + l_tcp->m_nb_mct_records;
6923                         memset(l_mct_data ,0,(l_tcp->m_nb_max_mct_records - l_tcp->m_nb_mct_records) * sizeof(opj_mct_data_t));
6924                 }
6925
6926                 l_mct_data = l_tcp->m_mct_records + l_tcp->m_nb_mct_records;
6927         }
6928
6929         if (l_mct_data->m_data) {
6930                 opj_free(l_mct_data->m_data);
6931                 l_mct_data->m_data = 00;
6932         }
6933
6934         l_mct_data->m_index = l_indix;
6935         l_mct_data->m_array_type = (J2K_MCT_ARRAY_TYPE)((l_tmp  >> 8) & 3);
6936         l_mct_data->m_element_type = (J2K_MCT_ELEMENT_TYPE)((l_tmp  >> 10) & 3);
6937
6938         opj_read_bytes(p_header_data,&l_tmp,2);                         /* Ymct */
6939         p_header_data+=2;
6940         if (l_tmp != 0) {
6941                 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge multiple MCT markers\n");
6942                 return OPJ_TRUE;
6943         }
6944
6945         p_header_size -= 6;
6946
6947         l_mct_data->m_data = (OPJ_BYTE*)opj_malloc(p_header_size);
6948         if (! l_mct_data->m_data) {
6949                 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCT marker\n");
6950                 return OPJ_FALSE;
6951         }
6952         memcpy(l_mct_data->m_data,p_header_data,p_header_size);
6953
6954         l_mct_data->m_data_size = p_header_size;
6955         ++l_tcp->m_nb_mct_records;
6956
6957         return OPJ_TRUE;
6958 }
6959
6960 /**
6961  * Writes the MCC marker (Multiple Component Collection)
6962  *
6963  * @param       p_stream                the stream to write data to.
6964  * @param       p_j2k                   J2K codec.
6965  * @param       p_manager               the user event manager.
6966 */
6967 opj_bool j2k_write_mcc_record(  opj_j2k_v2_t *p_j2k,
6968                                                                 struct opj_simple_mcc_decorrelation_data * p_mcc_record,
6969                                                                 struct opj_stream_private *p_stream,
6970                                                                 struct opj_event_mgr * p_manager )
6971 {
6972         OPJ_UINT32 i;
6973         OPJ_UINT32 l_mcc_size;
6974         OPJ_BYTE * l_current_data = 00;
6975         OPJ_UINT32 l_nb_bytes_for_comp;
6976         OPJ_UINT32 l_mask;
6977         OPJ_UINT32 l_tmcc;
6978
6979         /* preconditions */
6980         assert(p_j2k != 00);
6981         assert(p_manager != 00);
6982         assert(p_stream != 00);
6983
6984         if (p_mcc_record->m_nb_comps > 255 ) {
6985         l_nb_bytes_for_comp = 2;
6986                 l_mask = 0x8000;
6987         }
6988         else {
6989                 l_nb_bytes_for_comp = 1;
6990                 l_mask = 0;
6991         }
6992
6993         l_mcc_size = p_mcc_record->m_nb_comps * 2 * l_nb_bytes_for_comp + 19;
6994         if (l_mcc_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size)
6995         {
6996                 p_j2k->m_specific_param.m_encoder.m_header_tile_data
6997                         = (OPJ_BYTE*)opj_realloc(
6998                                 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
6999                                 l_mcc_size);
7000                 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
7001                         return OPJ_FALSE;
7002                 }
7003
7004                 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_mcc_size;
7005         }
7006
7007         l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
7008
7009         opj_write_bytes(l_current_data,J2K_MS_MCC,2);                                   /* MCC */
7010         l_current_data += 2;
7011
7012         opj_write_bytes(l_current_data,l_mcc_size-2,2);                                 /* Lmcc */
7013         l_current_data += 2;
7014
7015         /* first marker */
7016         opj_write_bytes(l_current_data,0,2);                                    /* Zmcc */
7017         l_current_data += 2;
7018
7019         opj_write_bytes(l_current_data,p_mcc_record->m_index,1);                                        /* Imcc -> no need for other values, take the first */
7020         ++l_current_data;
7021
7022         /* only one marker atm */
7023         opj_write_bytes(l_current_data,0,2);                                    /* Ymcc */
7024         l_current_data+=2;
7025
7026         opj_write_bytes(l_current_data,1,2);                                    /* Qmcc -> number of collections -> 1 */
7027         l_current_data+=2;
7028
7029         opj_write_bytes(l_current_data,0x1,1);                                  /* Xmcci type of component transformation -> array based decorrelation */
7030         ++l_current_data;
7031
7032         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 */
7033         l_current_data+=2;
7034
7035         for (i=0;i<p_mcc_record->m_nb_comps;++i) {
7036                 opj_write_bytes(l_current_data,i,l_nb_bytes_for_comp);                          /* Cmccij Component offset*/
7037                 l_current_data+=l_nb_bytes_for_comp;
7038         }
7039
7040         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 */
7041         l_current_data+=2;
7042
7043         for (i=0;i<p_mcc_record->m_nb_comps;++i)
7044         {
7045                 opj_write_bytes(l_current_data,i,l_nb_bytes_for_comp);                          /* Wmccij Component offset*/
7046                 l_current_data+=l_nb_bytes_for_comp;
7047         }
7048
7049         l_tmcc = ((!p_mcc_record->m_is_irreversible)&1)<<16;
7050
7051         if (p_mcc_record->m_decorrelation_array) {
7052                 l_tmcc |= p_mcc_record->m_decorrelation_array->m_index;
7053         }
7054
7055         if (p_mcc_record->m_offset_array) {
7056                 l_tmcc |= ((p_mcc_record->m_offset_array->m_index)<<8);
7057         }
7058
7059         opj_write_bytes(l_current_data,l_tmcc,3);       /* Tmcci : use MCT defined as number 1 and irreversible array based. */
7060         l_current_data+=3;
7061
7062         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) {
7063                 return OPJ_FALSE;
7064         }
7065
7066         return OPJ_TRUE;
7067 }
7068
7069 /**
7070  * Reads a MCC marker (Multiple Component Collection)
7071  *
7072  * @param       p_header_data   the data contained in the MCC box.
7073  * @param       p_j2k                   the jpeg2000 codec.
7074  * @param       p_header_size   the size of the data contained in the MCC marker.
7075  * @param       p_manager               the user event manager.
7076 */
7077 opj_bool j2k_read_mcc ( opj_j2k_v2_t *p_j2k,
7078                                         OPJ_BYTE * p_header_data,
7079                                         OPJ_UINT32 p_header_size,
7080                                         struct opj_event_mgr * p_manager )
7081 {
7082         OPJ_UINT32 i,j;
7083         OPJ_UINT32 l_tmp;
7084         OPJ_UINT32 l_indix;
7085         opj_tcp_v2_t * l_tcp;
7086         opj_simple_mcc_decorrelation_data_t * l_mcc_record;
7087         opj_mct_data_t * l_mct_data;
7088         OPJ_UINT32 l_nb_collections;
7089         OPJ_UINT32 l_nb_comps;
7090         OPJ_UINT32 l_nb_bytes_by_comp;
7091
7092
7093         /* preconditions */
7094         assert(p_header_data != 00);
7095         assert(p_j2k != 00);
7096         assert(p_manager != 00);
7097
7098         l_tcp = p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH ?
7099                         &p_j2k->m_cp.tcps[p_j2k->m_current_tile_number] :
7100                         p_j2k->m_specific_param.m_decoder.m_default_tcp;
7101
7102         if (p_header_size < 2) {
7103                 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
7104                 return OPJ_FALSE;
7105         }
7106
7107         /* first marker */
7108         opj_read_bytes(p_header_data,&l_tmp,2);                         /* Zmcc */
7109         p_header_data += 2;
7110         if (l_tmp != 0) {
7111                 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge multiple data spanning\n");
7112                 return OPJ_TRUE;
7113         }
7114
7115         if (p_header_size < 7) {
7116                 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
7117                 return OPJ_FALSE;
7118         }
7119
7120         opj_read_bytes(p_header_data,&l_indix,1); /* Imcc -> no need for other values, take the first */
7121         ++p_header_data;
7122
7123         l_mcc_record = l_tcp->m_mcc_records;
7124
7125         for(i=0;i<l_tcp->m_nb_mcc_records;++i) {
7126                 if (l_mcc_record->m_index == l_indix) {
7127                         break;
7128                 }
7129                 ++l_mcc_record;
7130         }
7131
7132         /** NOT FOUND */
7133         if (i == l_tcp->m_nb_mcc_records) {
7134                 if (l_tcp->m_nb_mcc_records == l_tcp->m_nb_max_mcc_records) {
7135                         l_tcp->m_nb_max_mcc_records += J2K_MCC_DEFAULT_NB_RECORDS;
7136
7137                         l_tcp->m_mcc_records = (opj_simple_mcc_decorrelation_data_t*)
7138                                         opj_realloc(l_tcp->m_mcc_records,l_tcp->m_nb_max_mcc_records * sizeof(opj_simple_mcc_decorrelation_data_t));
7139                         if (! l_tcp->m_mcc_records) {
7140                                 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
7141                                 return OPJ_FALSE;
7142                         }
7143                         l_mcc_record = l_tcp->m_mcc_records + l_tcp->m_nb_mcc_records;
7144                         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));
7145                 }
7146                 l_mcc_record = l_tcp->m_mcc_records + l_tcp->m_nb_mcc_records;
7147         }
7148         l_mcc_record->m_index = l_indix;
7149
7150         /* only one marker atm */
7151         opj_read_bytes(p_header_data,&l_tmp,2);                         /* Ymcc */
7152         p_header_data+=2;
7153         if (l_tmp != 0) {
7154                 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge multiple data spanning\n");
7155                 return OPJ_TRUE;
7156         }
7157
7158         opj_read_bytes(p_header_data,&l_nb_collections,2);                              /* Qmcc -> number of collections -> 1 */
7159         p_header_data+=2;
7160
7161         if (l_nb_collections > 1) {
7162                 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge multiple collections\n");
7163                 return OPJ_TRUE;
7164         }
7165
7166         p_header_size -= 7;
7167
7168         for (i=0;i<l_nb_collections;++i) {
7169                 if (p_header_size < 3) {
7170                         opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
7171                         return OPJ_FALSE;
7172                 }
7173
7174                 opj_read_bytes(p_header_data,&l_tmp,1); /* Xmcci type of component transformation -> array based decorrelation */
7175                 ++p_header_data;
7176
7177                 if (l_tmp != 1) {
7178                         opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge collections other than array decorrelation\n");
7179                         return OPJ_TRUE;
7180                 }
7181
7182                 opj_read_bytes(p_header_data,&l_nb_comps,2);
7183
7184                 p_header_data+=2;
7185                 p_header_size-=3;
7186
7187                 l_nb_bytes_by_comp = 1 + (l_nb_comps>>15);
7188                 l_mcc_record->m_nb_comps = l_nb_comps & 0x7fff;
7189
7190                 if (p_header_size < (l_nb_bytes_by_comp * l_mcc_record->m_nb_comps + 2)) {
7191                         opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
7192                         return OPJ_FALSE;
7193                 }
7194
7195                 p_header_size -= (l_nb_bytes_by_comp * l_mcc_record->m_nb_comps + 2);
7196
7197                 for (j=0;j<l_mcc_record->m_nb_comps;++j) {
7198                         opj_read_bytes(p_header_data,&l_tmp,l_nb_bytes_by_comp);        /* Cmccij Component offset*/
7199                         p_header_data+=l_nb_bytes_by_comp;
7200
7201                         if (l_tmp != j) {
7202                                 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge collections with indix shuffle\n");
7203                                 return OPJ_TRUE;
7204                         }
7205                 }
7206
7207                 opj_read_bytes(p_header_data,&l_nb_comps,2);
7208                 p_header_data+=2;
7209
7210                 l_nb_bytes_by_comp = 1 + (l_nb_comps>>15);
7211                 l_nb_comps &= 0x7fff;
7212
7213                 if (l_nb_comps != l_mcc_record->m_nb_comps) {
7214                         opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge collections without same number of indixes\n");
7215                         return OPJ_TRUE;
7216                 }
7217
7218                 if (p_header_size < (l_nb_bytes_by_comp * l_mcc_record->m_nb_comps + 3)) {
7219                         opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
7220                         return OPJ_FALSE;
7221                 }
7222
7223                 p_header_size -= (l_nb_bytes_by_comp * l_mcc_record->m_nb_comps + 3);
7224
7225                 for (j=0;j<l_mcc_record->m_nb_comps;++j) {
7226                         opj_read_bytes(p_header_data,&l_tmp,l_nb_bytes_by_comp);        /* Wmccij Component offset*/
7227                         p_header_data+=l_nb_bytes_by_comp;
7228
7229                         if (l_tmp != j) {
7230                                 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge collections with indix shuffle\n");
7231                                 return OPJ_TRUE;
7232                         }
7233                 }
7234
7235                 opj_read_bytes(p_header_data,&l_tmp,3); /* Wmccij Component offset*/
7236                 p_header_data += 3;
7237
7238                 l_mcc_record->m_is_irreversible = ! ((l_tmp>>16) & 1);
7239                 l_mcc_record->m_decorrelation_array = 00;
7240                 l_mcc_record->m_offset_array = 00;
7241
7242                 l_indix = l_tmp & 0xff;
7243                 if (l_indix != 0) {
7244                         l_mct_data = l_tcp->m_mct_records;
7245                         for (j=0;j<l_tcp->m_nb_mct_records;++j) {
7246                                 if (l_mct_data->m_index == l_indix) {
7247                                         l_mcc_record->m_decorrelation_array = l_mct_data;
7248                                         break;
7249                                 }
7250                                 ++l_mct_data;
7251                         }
7252
7253                         if (l_mcc_record->m_decorrelation_array == 00) {
7254                                 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
7255                                 return OPJ_FALSE;
7256                         }
7257                 }
7258
7259                 l_indix = (l_tmp >> 8) & 0xff;
7260                 if (l_indix != 0) {
7261                         l_mct_data = l_tcp->m_mct_records;
7262                         for (j=0;j<l_tcp->m_nb_mct_records;++j) {
7263                                 if (l_mct_data->m_index == l_indix) {
7264                                         l_mcc_record->m_offset_array = l_mct_data;
7265                                         break;
7266                                 }
7267                                 ++l_mct_data;
7268                         }
7269
7270                         if (l_mcc_record->m_offset_array == 00) {
7271                                 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
7272                                 return OPJ_FALSE;
7273                         }
7274                 }
7275         }
7276
7277         if (p_header_size != 0) {
7278                 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
7279                 return OPJ_FALSE;
7280         }
7281
7282         ++l_tcp->m_nb_mcc_records;
7283
7284         return OPJ_TRUE;
7285 }
7286
7287
7288 /**
7289  * Writes the MCO marker (Multiple component transformation ordering)
7290  *
7291  * @param       p_stream                                the stream to write data to.
7292  * @param       p_j2k                           J2K codec.
7293  * @param       p_manager               the user event manager.
7294 */
7295 opj_bool j2k_write_mco( opj_j2k_v2_t *p_j2k,
7296                                                 struct opj_stream_private *p_stream,
7297                                                 struct opj_event_mgr * p_manager
7298                                   )
7299 {
7300         OPJ_BYTE * l_current_data = 00;
7301         OPJ_UINT32 l_mco_size;
7302         opj_tcp_v2_t * l_tcp = 00;
7303         opj_simple_mcc_decorrelation_data_t * l_mcc_record;
7304         OPJ_UINT32 i;
7305
7306         /* preconditions */
7307         assert(p_j2k != 00);
7308         assert(p_manager != 00);
7309         assert(p_stream != 00);
7310
7311         l_tcp =&(p_j2k->m_cp.tcps[p_j2k->m_current_tile_number]);
7312         l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
7313
7314         l_mco_size = 5 + l_tcp->m_nb_mcc_records;
7315         if (l_mco_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
7316
7317                 p_j2k->m_specific_param.m_encoder.m_header_tile_data
7318                         = (OPJ_BYTE*)opj_realloc(
7319                                 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
7320                                 l_mco_size);
7321                 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data)
7322                 {
7323                         return OPJ_FALSE;
7324                 }
7325
7326                 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_mco_size;
7327         }
7328
7329         opj_write_bytes(l_current_data,J2K_MS_MCO,2);                   /* MCO */
7330         l_current_data += 2;
7331
7332         opj_write_bytes(l_current_data,l_mco_size-2,2);                                 /* Lmco */
7333         l_current_data += 2;
7334
7335         opj_write_bytes(l_current_data,l_tcp->m_nb_mcc_records,1);                                      /* Nmco : only one tranform stage*/
7336         ++l_current_data;
7337
7338         l_mcc_record = l_tcp->m_mcc_records;
7339         for     (i=0;i<l_tcp->m_nb_mcc_records;++i) {
7340                 opj_write_bytes(l_current_data,l_mcc_record->m_index,1);                                        /* Imco -> use the mcc indicated by 1*/
7341                 ++l_current_data;
7342
7343                 ++l_mcc_record;
7344         }
7345
7346         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) {
7347                 return OPJ_FALSE;
7348         }
7349
7350         return OPJ_TRUE;
7351 }
7352
7353 /**
7354  * Reads a MCO marker (Multiple Component Transform Ordering)
7355  *
7356  * @param       p_header_data   the data contained in the MCO box.
7357  * @param       p_j2k                   the jpeg2000 codec.
7358  * @param       p_header_size   the size of the data contained in the MCO marker.
7359  * @param       p_manager               the user event manager.
7360 */
7361 opj_bool j2k_read_mco ( opj_j2k_v2_t *p_j2k,
7362                                                 OPJ_BYTE * p_header_data,
7363                                                 OPJ_UINT32 p_header_size,
7364                                                 struct opj_event_mgr * p_manager )
7365 {
7366         OPJ_UINT32 l_tmp, i;
7367         OPJ_UINT32 l_nb_stages;
7368         opj_tcp_v2_t * l_tcp;
7369         opj_tccp_t * l_tccp;
7370         opj_image_t * l_image;
7371         opj_image_comp_t * l_img_comp;
7372
7373         /* preconditions */
7374         assert(p_header_data != 00);
7375         assert(p_j2k != 00);
7376         assert(p_manager != 00);
7377
7378         l_image = p_j2k->m_private_image;
7379         l_tcp = p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH ?
7380                         &p_j2k->m_cp.tcps[p_j2k->m_current_tile_number] :
7381                         p_j2k->m_specific_param.m_decoder.m_default_tcp;
7382
7383         if (p_header_size < 1) {
7384                 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCO marker\n");
7385                 return OPJ_FALSE;
7386         }
7387
7388         opj_read_bytes(p_header_data,&l_nb_stages,1);                           /* Nmco : only one tranform stage*/
7389         ++p_header_data;
7390
7391         if (l_nb_stages > 1) {
7392                 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge multiple transformation stages.\n");
7393                 return OPJ_TRUE;
7394         }
7395
7396         if (p_header_size != l_nb_stages + 1) {
7397                 opj_event_msg_v2(p_manager, EVT_WARNING, "Error reading MCO marker\n");
7398                 return OPJ_FALSE;
7399         }
7400
7401         l_tccp = l_tcp->tccps;
7402         l_img_comp = l_image->comps;
7403
7404         for (i=0;i<l_image->numcomps;++i) {
7405                 l_tccp->m_dc_level_shift = 0;
7406                 ++l_tccp;
7407         }
7408
7409         if (l_tcp->m_mct_decoding_matrix) {
7410                 opj_free(l_tcp->m_mct_decoding_matrix);
7411                 l_tcp->m_mct_decoding_matrix = 00;
7412         }
7413
7414         for (i=0;i<l_nb_stages;++i) {
7415                 opj_read_bytes(p_header_data,&l_tmp,1);
7416                 ++p_header_data;
7417
7418                 if (! j2k_add_mct(l_tcp,p_j2k->m_private_image,l_tmp)) {
7419                         return OPJ_FALSE;
7420                 }
7421         }
7422
7423         return OPJ_TRUE;
7424 }
7425
7426 opj_bool j2k_add_mct(opj_tcp_v2_t * p_tcp, opj_image_t * p_image, OPJ_UINT32 p_index)
7427 {
7428         OPJ_UINT32 i;
7429         opj_simple_mcc_decorrelation_data_t * l_mcc_record;
7430         opj_mct_data_t * l_deco_array, * l_offset_array;
7431         OPJ_UINT32 l_data_size,l_mct_size, l_offset_size;
7432         OPJ_UINT32 l_nb_elem;
7433         OPJ_UINT32 * l_offset_data, * l_current_offset_data;
7434         opj_tccp_t * l_tccp;
7435
7436         /* preconditions */
7437         assert(p_tcp != 00);
7438
7439         l_mcc_record = p_tcp->m_mcc_records;
7440
7441         for (i=0;i<p_tcp->m_nb_mcc_records;++i) {
7442                 if (l_mcc_record->m_index == p_index) {
7443                         break;
7444                 }
7445         }
7446
7447         if (i==p_tcp->m_nb_mcc_records) {
7448                 /** element discarded **/
7449                 return OPJ_TRUE;
7450         }
7451
7452         if (l_mcc_record->m_nb_comps != p_image->numcomps) {
7453                 /** do not support number of comps != image */
7454                 return OPJ_TRUE;
7455         }
7456
7457         l_deco_array = l_mcc_record->m_decorrelation_array;
7458
7459         if (l_deco_array) {
7460                 l_data_size = MCT_ELEMENT_SIZE[l_deco_array->m_element_type] * p_image->numcomps * p_image->numcomps;
7461                 if (l_deco_array->m_data_size != l_data_size) {
7462                         return OPJ_FALSE;
7463                 }
7464
7465                 l_nb_elem = p_image->numcomps * p_image->numcomps;
7466                 l_mct_size = l_nb_elem * sizeof(OPJ_FLOAT32);
7467                 p_tcp->m_mct_decoding_matrix = (OPJ_FLOAT32*)opj_malloc(l_mct_size);
7468
7469                 if (! p_tcp->m_mct_decoding_matrix ) {
7470                         return OPJ_FALSE;
7471                 }
7472
7473                 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);
7474         }
7475
7476         l_offset_array = l_mcc_record->m_offset_array;
7477
7478         if (l_offset_array) {
7479                 l_data_size = MCT_ELEMENT_SIZE[l_offset_array->m_element_type] * p_image->numcomps;
7480                 if (l_offset_array->m_data_size != l_data_size) {
7481                         return OPJ_FALSE;
7482                 }
7483
7484                 l_nb_elem = p_image->numcomps;
7485                 l_offset_size = l_nb_elem * sizeof(OPJ_UINT32);
7486                 l_offset_data = (OPJ_UINT32*)opj_malloc(l_offset_size);
7487
7488                 if (! l_offset_data ) {
7489                         return OPJ_FALSE;
7490                 }
7491
7492                 j2k_mct_read_functions_to_int32[l_offset_array->m_element_type](l_offset_array->m_data,l_offset_data,l_nb_elem);
7493
7494                 l_tccp = p_tcp->tccps;
7495                 l_current_offset_data = l_offset_data;
7496
7497                 for (i=0;i<p_image->numcomps;++i) {
7498                         l_tccp->m_dc_level_shift = *(l_current_offset_data++);
7499                         ++l_tccp;
7500                 }
7501
7502                 opj_free(l_offset_data);
7503         }
7504
7505         return OPJ_TRUE;
7506 }
7507
7508 /**
7509  * Writes the CBD marker (Component bit depth definition)
7510  *
7511  * @param       p_stream                                the stream to write data to.
7512  * @param       p_j2k                           J2K codec.
7513  * @param       p_manager               the user event manager.
7514 */
7515 opj_bool j2k_write_cbd( opj_j2k_v2_t *p_j2k,
7516                                                 struct opj_stream_private *p_stream,
7517                                                 struct opj_event_mgr * p_manager )
7518 {
7519         OPJ_UINT32 i;
7520         OPJ_UINT32 l_cbd_size;
7521         OPJ_BYTE * l_current_data = 00;
7522         opj_image_t *l_image = 00;
7523         opj_image_comp_t * l_comp = 00;
7524
7525         /* preconditions */
7526         assert(p_j2k != 00);
7527         assert(p_manager != 00);
7528         assert(p_stream != 00);
7529
7530         l_image = p_j2k->m_private_image;
7531         l_cbd_size = 6 + p_j2k->m_private_image->numcomps;
7532
7533         if (l_cbd_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
7534                 p_j2k->m_specific_param.m_encoder.m_header_tile_data
7535                         = (OPJ_BYTE*)opj_realloc(
7536                                 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
7537                                 l_cbd_size);
7538
7539                 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
7540                         return OPJ_FALSE;
7541                 }
7542
7543                 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_cbd_size;
7544         }
7545
7546         l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
7547
7548         opj_write_bytes(l_current_data,J2K_MS_CBD,2);                                   /* CBD */
7549         l_current_data += 2;
7550
7551         opj_write_bytes(l_current_data,l_cbd_size-2,2);                                 /* L_CBD */
7552         l_current_data += 2;
7553
7554         opj_write_bytes(l_current_data,l_image->numcomps, 2);           /* Ncbd */
7555         l_current_data+=2;
7556
7557         l_comp = l_image->comps;
7558
7559         for (i=0;i<l_image->numcomps;++i) {
7560                 opj_write_bytes(l_current_data, (l_comp->sgnd << 7) | (l_comp->prec - 1), 1);           /* Component bit depth */
7561                 ++l_current_data;
7562
7563                 ++l_comp;
7564         }
7565
7566         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) {
7567                 return OPJ_FALSE;
7568         }
7569
7570         return OPJ_TRUE;
7571 }
7572
7573 /**
7574  * Reads a CBD marker (Component bit depth definition)
7575  * @param       p_header_data   the data contained in the CBD box.
7576  * @param       p_j2k                   the jpeg2000 codec.
7577  * @param       p_header_size   the size of the data contained in the CBD marker.
7578  * @param       p_manager               the user event manager.
7579 */
7580 opj_bool j2k_read_cbd ( opj_j2k_v2_t *p_j2k,
7581                                                 OPJ_BYTE * p_header_data,
7582                                                 OPJ_UINT32 p_header_size,
7583                                                 struct opj_event_mgr * p_manager)
7584 {
7585         OPJ_UINT32 l_nb_comp,l_num_comp;
7586         OPJ_UINT32 l_comp_def;
7587         OPJ_UINT32 i;
7588         opj_image_comp_t * l_comp = 00;
7589
7590         /* preconditions */
7591         assert(p_header_data != 00);
7592         assert(p_j2k != 00);
7593         assert(p_manager != 00);
7594
7595         l_num_comp = p_j2k->m_private_image->numcomps;
7596
7597         if (p_header_size != (p_j2k->m_private_image->numcomps + 2)) {
7598                 opj_event_msg_v2(p_manager, EVT_ERROR, "Crror reading CBD marker\n");
7599                 return OPJ_FALSE;
7600         }
7601
7602         opj_read_bytes(p_header_data,&l_nb_comp,2);                             /* Ncbd */
7603         p_header_data+=2;
7604
7605         if (l_nb_comp != l_num_comp) {
7606                 opj_event_msg_v2(p_manager, EVT_ERROR, "Crror reading CBD marker\n");
7607                 return OPJ_FALSE;
7608         }
7609
7610         l_comp = p_j2k->m_private_image->comps;
7611         for (i=0;i<l_num_comp;++i) {
7612                 opj_read_bytes(p_header_data,&l_comp_def,1);                    /* Component bit depth */
7613                 ++p_header_data;
7614         l_comp->sgnd = (l_comp_def>>7) & 1;
7615                 l_comp->prec = (l_comp_def&0x7f) + 1;
7616                 ++l_comp;
7617         }
7618
7619         return OPJ_TRUE;
7620 }
7621
7622
7623 /* ----------------------------------------------------------------------- */
7624 /* J2K / JPT decoder interface                                             */
7625 /* ----------------------------------------------------------------------- */
7626
7627
7628
7629 void j2k_destroy_decompress(opj_j2k_t *j2k) {
7630         int i = 0;
7631
7632         if(j2k->tile_len != NULL) {
7633                 opj_free(j2k->tile_len);
7634         }
7635         if(j2k->tile_data != NULL) {
7636                 opj_free(j2k->tile_data);
7637         }
7638         if(j2k->default_tcp != NULL) {
7639                 opj_tcp_t *default_tcp = j2k->default_tcp;
7640                 if(default_tcp->ppt_data_first != NULL) {
7641                         opj_free(default_tcp->ppt_data_first);
7642                 }
7643                 if(j2k->default_tcp->tccps != NULL) {
7644                         opj_free(j2k->default_tcp->tccps);
7645                 }
7646                 opj_free(j2k->default_tcp);
7647         }
7648         if(j2k->cp != NULL) {
7649                 opj_cp_t *cp = j2k->cp;
7650                 if(cp->tcps != NULL) {
7651                         for(i = 0; i < cp->tw * cp->th; i++) {
7652                                 if(cp->tcps[i].ppt_data_first != NULL) {
7653                                         opj_free(cp->tcps[i].ppt_data_first);
7654                                 }
7655                                 if(cp->tcps[i].tccps != NULL) {
7656                                         opj_free(cp->tcps[i].tccps);
7657                                 }
7658                         }
7659                         opj_free(cp->tcps);
7660                 }
7661                 if(cp->ppm_data_first != NULL) {
7662                         opj_free(cp->ppm_data_first);
7663                 }
7664                 if(cp->tileno != NULL) {
7665                         opj_free(cp->tileno);  
7666                 }
7667                 if(cp->comment != NULL) {
7668                         opj_free(cp->comment);
7669                 }
7670
7671                 opj_free(cp);
7672         }
7673         opj_free(j2k);
7674 }
7675
7676 void j2k_setup_decoder(opj_j2k_t *j2k, opj_dparameters_t *parameters) {
7677         if(j2k && parameters) {
7678                 /* create and initialize the coding parameters structure */
7679                 opj_cp_t *cp = (opj_cp_t*) opj_calloc(1, sizeof(opj_cp_t));
7680                 cp->reduce = parameters->cp_reduce;     
7681                 cp->layer = parameters->cp_layer;
7682                 cp->limit_decoding = parameters->cp_limit_decoding;
7683
7684 #ifdef USE_JPWL
7685                 cp->correct = parameters->jpwl_correct;
7686                 cp->exp_comps = parameters->jpwl_exp_comps;
7687                 cp->max_tiles = parameters->jpwl_max_tiles;
7688 #endif /* USE_JPWL */
7689
7690
7691                 /* keep a link to cp so that we can destroy it later in j2k_destroy_decompress */
7692                 j2k->cp = cp;
7693         }
7694 }
7695
7696 void j2k_setup_decoder_v2(opj_j2k_v2_t *j2k, opj_dparameters_t *parameters)
7697 {
7698         if(j2k && parameters) {
7699                 j2k->m_cp.m_specific_param.m_dec.m_layer = parameters->cp_layer;
7700                 j2k->m_cp.m_specific_param.m_dec.m_reduce = parameters->cp_reduce;
7701
7702 #ifdef USE_JPWL
7703                 j2k->m_cp.correct = parameters->jpwl_correct;
7704                 j2k->m_cp.exp_comps = parameters->jpwl_exp_comps;
7705                 j2k->m_cp.max_tiles = parameters->jpwl_max_tiles;
7706 #endif /* USE_JPWL */
7707         }
7708 }
7709
7710 opj_image_t* j2k_decode(opj_j2k_t *j2k, opj_cio_t *cio, opj_codestream_info_t *cstr_info) {
7711         opj_image_t *image = NULL;
7712
7713         opj_common_ptr cinfo = j2k->cinfo;      
7714
7715         j2k->cio = cio;
7716         j2k->cstr_info = cstr_info;
7717         if (cstr_info)
7718                 memset(cstr_info, 0, sizeof(opj_codestream_info_t));
7719
7720         /* create an empty image */
7721         image = opj_image_create0();
7722         j2k->image = image;
7723
7724         j2k->state = J2K_STATE_MHSOC;
7725
7726         for (;;) {
7727                 opj_dec_mstabent_t *e;
7728                 int id = cio_read(cio, 2);
7729
7730 #ifdef USE_JPWL
7731                 /* we try to honor JPWL correction power */
7732                 if (j2k->cp->correct) {
7733
7734                         int orig_pos = cio_tell(cio);
7735                         opj_bool status;
7736
7737                         /* call the corrector */
7738                         status = jpwl_correct(j2k);
7739
7740                         /* go back to where you were */
7741                         cio_seek(cio, orig_pos - 2);
7742
7743                         /* re-read the marker */
7744                         id = cio_read(cio, 2);
7745
7746                         /* check whether it begins with ff */
7747                         if (id >> 8 != 0xff) {
7748                                 opj_event_msg(cinfo, EVT_ERROR,
7749                                         "JPWL: possible bad marker %x at %d\n",
7750                                         id, cio_tell(cio) - 2);
7751                                 if (!JPWL_ASSUME) {
7752                                         opj_image_destroy(image);
7753                                         opj_event_msg(cinfo, EVT_ERROR, "JPWL: giving up\n");
7754                                         return 0;
7755                                 }
7756                                 /* we try to correct */
7757                                 id = id | 0xff00;
7758                                 cio_seek(cio, cio_tell(cio) - 2);
7759                                 cio_write(cio, id, 2);
7760                                 opj_event_msg(cinfo, EVT_WARNING, "- trying to adjust this\n"
7761                                         "- setting marker to %x\n",
7762                                         id);
7763                         }
7764
7765                 }
7766 #endif /* USE_JPWL */
7767
7768                 if (id >> 8 != 0xff) {
7769                         opj_image_destroy(image);
7770                         opj_event_msg(cinfo, EVT_ERROR, "%.8x: expected a marker instead of %x\n", cio_tell(cio) - 2, id);
7771                         return 0;
7772                 }
7773                 e = j2k_dec_mstab_lookup(id);
7774                 /* Check if the marker is known*/
7775                 if (!(j2k->state & e->states)) {
7776                         opj_image_destroy(image);
7777                         opj_event_msg(cinfo, EVT_ERROR, "%.8x: unexpected marker %x\n", cio_tell(cio) - 2, id);
7778                         return 0;
7779                 }
7780                 /* Check if the decoding is limited to the main header*/
7781                 if (e->id == J2K_MS_SOT && j2k->cp->limit_decoding == LIMIT_TO_MAIN_HEADER) {
7782                         opj_event_msg(cinfo, EVT_INFO, "Main Header decoded.\n");
7783                         return image;
7784                 }               
7785
7786                 if (e->handler) {
7787                         (*e->handler)(j2k);
7788                 }
7789                 if (j2k->state & J2K_STATE_ERR) 
7790                         return NULL;    
7791
7792                 if (j2k->state == J2K_STATE_MT) {
7793                         break;
7794                 }
7795                 if (j2k->state == J2K_STATE_NEOC) {
7796                         break;
7797                 }
7798         }
7799         if (j2k->state == J2K_STATE_NEOC) {
7800                 j2k_read_eoc(j2k);
7801         }
7802
7803         if (j2k->state != J2K_STATE_MT) {
7804                 opj_event_msg(cinfo, EVT_WARNING, "Incomplete bitstream\n");
7805         }
7806         return image;
7807 }
7808
7809 /*
7810 * Read a JPT-stream and decode file
7811 *
7812 */
7813 opj_image_t* j2k_decode_jpt_stream(opj_j2k_t *j2k, opj_cio_t *cio,  opj_codestream_info_t *cstr_info) {
7814         opj_image_t *image = NULL;
7815         opj_jpt_msg_header_t header;
7816         int position;
7817         opj_common_ptr cinfo = j2k->cinfo;
7818
7819         OPJ_ARG_NOT_USED(cstr_info);
7820
7821         j2k->cio = cio;
7822
7823         /* create an empty image */
7824         image = opj_image_create0();
7825         j2k->image = image;
7826
7827         j2k->state = J2K_STATE_MHSOC;
7828         
7829         /* Initialize the header */
7830         jpt_init_msg_header(&header);
7831         /* Read the first header of the message */
7832         jpt_read_msg_header(cinfo, cio, &header);
7833         
7834         position = cio_tell(cio);
7835         if (header.Class_Id != 6) {     /* 6 : Main header data-bin message */
7836                 opj_image_destroy(image);
7837                 opj_event_msg(cinfo, EVT_ERROR, "[JPT-stream] : Expecting Main header first [class_Id %d] !\n", header.Class_Id);
7838                 return 0;
7839         }
7840         
7841         for (;;) {
7842                 opj_dec_mstabent_t *e = NULL;
7843                 int id;
7844                 
7845                 if (!cio_numbytesleft(cio)) {
7846                         j2k_read_eoc(j2k);
7847                         return image;
7848                 }
7849                 /* data-bin read -> need to read a new header */
7850                 if ((unsigned int) (cio_tell(cio) - position) == header.Msg_length) {
7851                         jpt_read_msg_header(cinfo, cio, &header);
7852                         position = cio_tell(cio);
7853                         if (header.Class_Id != 4) {     /* 4 : Tile data-bin message */
7854                                 opj_image_destroy(image);
7855                                 opj_event_msg(cinfo, EVT_ERROR, "[JPT-stream] : Expecting Tile info !\n");
7856                                 return 0;
7857                         }
7858                 }
7859                 
7860                 id = cio_read(cio, 2);
7861                 if (id >> 8 != 0xff) {
7862                         opj_image_destroy(image);
7863                         opj_event_msg(cinfo, EVT_ERROR, "%.8x: expected a marker instead of %x\n", cio_tell(cio) - 2, id);
7864                         return 0;
7865                 }
7866                 e = j2k_dec_mstab_lookup(id);
7867                 if (!(j2k->state & e->states)) {
7868                         opj_image_destroy(image);
7869                         opj_event_msg(cinfo, EVT_ERROR, "%.8x: unexpected marker %x\n", cio_tell(cio) - 2, id);
7870                         return 0;
7871                 }
7872                 if (e->handler) {
7873                         (*e->handler)(j2k);
7874                 }
7875                 if (j2k->state == J2K_STATE_MT) {
7876                         break;
7877                 }
7878                 if (j2k->state == J2K_STATE_NEOC) {
7879                         break;
7880                 }
7881         }
7882         if (j2k->state == J2K_STATE_NEOC) {
7883                 j2k_read_eoc(j2k);
7884         }
7885         
7886         if (j2k->state != J2K_STATE_MT) {
7887                 opj_event_msg(cinfo, EVT_WARNING, "Incomplete bitstream\n");
7888         }
7889
7890         return image;
7891 }
7892
7893 /* ----------------------------------------------------------------------- */
7894 /* J2K encoder interface                                                       */
7895 /* ----------------------------------------------------------------------- */
7896
7897 opj_j2k_t* j2k_create_compress(opj_common_ptr cinfo) {
7898         opj_j2k_t *j2k = (opj_j2k_t*) opj_calloc(1, sizeof(opj_j2k_t));
7899         if(j2k) {
7900                 j2k->cinfo = cinfo;
7901         }
7902         return j2k;
7903 }
7904
7905 opj_j2k_v2_t* j2k_create_compress_v2(void)
7906 {
7907         opj_j2k_v2_t *l_j2k = (opj_j2k_v2_t*) opj_malloc(sizeof(opj_j2k_v2_t));
7908         if (!l_j2k) {
7909                 return NULL;
7910         }
7911
7912         memset(l_j2k,0,sizeof(opj_j2k_v2_t));
7913
7914         l_j2k->m_is_decoder = 0;
7915         l_j2k->m_cp.m_is_decoder = 0;
7916
7917         l_j2k->m_specific_param.m_encoder.m_header_tile_data = (OPJ_BYTE *) opj_malloc(J2K_DEFAULT_HEADER_SIZE);
7918         if (! l_j2k->m_specific_param.m_encoder.m_header_tile_data) {
7919                 j2k_destroy(l_j2k);
7920                 return NULL;
7921         }
7922
7923         l_j2k->m_specific_param.m_encoder.m_header_tile_data_size = J2K_DEFAULT_HEADER_SIZE;
7924
7925         /* validation list creation*/
7926         l_j2k->m_validation_list = opj_procedure_list_create();
7927         if (! l_j2k->m_validation_list) {
7928                 j2k_destroy(l_j2k);
7929                 return NULL;
7930         }
7931
7932         /* execution list creation*/
7933         l_j2k->m_procedure_list = opj_procedure_list_create();
7934         if (! l_j2k->m_procedure_list) {
7935                 j2k_destroy(l_j2k);
7936                 return NULL;
7937         }
7938
7939         return l_j2k;
7940 }
7941
7942 void j2k_destroy_compress(opj_j2k_t *j2k) {
7943         int tileno;
7944
7945         if(!j2k) return;
7946         if(j2k->cp != NULL) {
7947                 opj_cp_t *cp = j2k->cp;
7948
7949                 if(cp->comment) {
7950                         opj_free(cp->comment);
7951                 }
7952                 if(cp->matrice) {
7953                         opj_free(cp->matrice);
7954                 }
7955                 for (tileno = 0; tileno < cp->tw * cp->th; tileno++) {
7956                         opj_free(cp->tcps[tileno].tccps);
7957                 }
7958                 opj_free(cp->tcps);
7959                 opj_free(cp);
7960         }
7961
7962         opj_free(j2k);
7963 }
7964
7965 void j2k_setup_encoder(opj_j2k_t *j2k, opj_cparameters_t *parameters, opj_image_t *image) {
7966         OPJ_UINT32 i,j;
7967   int tileno, numpocs_tile;
7968         opj_cp_t *cp = NULL;
7969
7970         if(!j2k || !parameters || ! image) {
7971                 return;
7972         }
7973
7974         /* create and initialize the coding parameters structure */
7975         cp = (opj_cp_t*) opj_calloc(1, sizeof(opj_cp_t));
7976
7977         /* keep a link to cp so that we can destroy it later in j2k_destroy_compress */
7978         j2k->cp = cp;
7979
7980         /* set default values for cp */
7981         cp->tw = 1;
7982         cp->th = 1;
7983
7984         /* 
7985         copy user encoding parameters 
7986         */
7987         cp->cinema = parameters->cp_cinema;
7988         cp->max_comp_size =     parameters->max_comp_size;
7989         cp->rsiz   = parameters->cp_rsiz;
7990         cp->disto_alloc = parameters->cp_disto_alloc;
7991         cp->fixed_alloc = parameters->cp_fixed_alloc;
7992         cp->fixed_quality = parameters->cp_fixed_quality;
7993
7994         /* mod fixed_quality */
7995         if(parameters->cp_matrice) {
7996                 size_t array_size = parameters->tcp_numlayers * parameters->numresolution * 3 * sizeof(int);
7997                 cp->matrice = (int *) opj_malloc(array_size);
7998                 memcpy(cp->matrice, parameters->cp_matrice, array_size);
7999         }
8000
8001         /* tiles */
8002         cp->tdx = parameters->cp_tdx;
8003         cp->tdy = parameters->cp_tdy;
8004
8005         /* tile offset */
8006         cp->tx0 = parameters->cp_tx0;
8007         cp->ty0 = parameters->cp_ty0;
8008
8009         /* comment string */
8010         if(parameters->cp_comment) {
8011                 cp->comment = (char*)opj_malloc(strlen(parameters->cp_comment) + 1);
8012                 if(cp->comment) {
8013                         strcpy(cp->comment, parameters->cp_comment);
8014                 }
8015         }
8016
8017         /*
8018         calculate other encoding parameters
8019         */
8020
8021         if (parameters->tile_size_on) {
8022                 cp->tw = int_ceildiv(image->x1 - cp->tx0, cp->tdx);
8023                 cp->th = int_ceildiv(image->y1 - cp->ty0, cp->tdy);
8024         } else {
8025                 cp->tdx = image->x1 - cp->tx0;
8026                 cp->tdy = image->y1 - cp->ty0;
8027         }
8028
8029         if(parameters->tp_on){
8030                 cp->tp_flag = parameters->tp_flag;
8031                 cp->tp_on = 1;
8032         }
8033         
8034         cp->img_size = 0;
8035         for(i=0;i<image->numcomps ;i++){
8036         cp->img_size += (image->comps[i].w *image->comps[i].h * image->comps[i].prec);
8037         }
8038
8039
8040 #ifdef USE_JPWL
8041         /*
8042         calculate JPWL encoding parameters
8043         */
8044
8045         if (parameters->jpwl_epc_on) {
8046                 OPJ_UINT32 i;
8047
8048                 /* set JPWL on */
8049                 cp->epc_on = OPJ_TRUE;
8050                 cp->info_on = OPJ_FALSE; /* no informative technique */
8051
8052                 /* set EPB on */
8053                 if ((parameters->jpwl_hprot_MH > 0) || (parameters->jpwl_hprot_TPH[0] > 0)) {
8054                         cp->epb_on = OPJ_TRUE;
8055                         
8056                         cp->hprot_MH = parameters->jpwl_hprot_MH;
8057                         for (i = 0; i < JPWL_MAX_NO_TILESPECS; i++) {
8058                                 cp->hprot_TPH_tileno[i] = parameters->jpwl_hprot_TPH_tileno[i];
8059                                 cp->hprot_TPH[i] = parameters->jpwl_hprot_TPH[i];
8060                         }
8061                         /* if tile specs are not specified, copy MH specs */
8062                         if (cp->hprot_TPH[0] == -1) {
8063                                 cp->hprot_TPH_tileno[0] = 0;
8064                                 cp->hprot_TPH[0] = parameters->jpwl_hprot_MH;
8065                         }
8066                         for (i = 0; i < JPWL_MAX_NO_PACKSPECS; i++) {
8067                                 cp->pprot_tileno[i] = parameters->jpwl_pprot_tileno[i];
8068                                 cp->pprot_packno[i] = parameters->jpwl_pprot_packno[i];
8069                                 cp->pprot[i] = parameters->jpwl_pprot[i];
8070                         }
8071                 }
8072
8073                 /* set ESD writing */
8074                 if ((parameters->jpwl_sens_size == 1) || (parameters->jpwl_sens_size == 2)) {
8075                         cp->esd_on = OPJ_TRUE;
8076
8077                         cp->sens_size = parameters->jpwl_sens_size;
8078                         cp->sens_addr = parameters->jpwl_sens_addr;
8079                         cp->sens_range = parameters->jpwl_sens_range;
8080
8081                         cp->sens_MH = parameters->jpwl_sens_MH;
8082                         for (i = 0; i < JPWL_MAX_NO_TILESPECS; i++) {
8083                                 cp->sens_TPH_tileno[i] = parameters->jpwl_sens_TPH_tileno[i];
8084                                 cp->sens_TPH[i] = parameters->jpwl_sens_TPH[i];
8085                         }
8086                 }
8087
8088                 /* always set RED writing to false: we are at the encoder */
8089                 cp->red_on = OPJ_FALSE;
8090
8091         } else {
8092                 cp->epc_on = OPJ_FALSE;
8093         }
8094 #endif /* USE_JPWL */
8095
8096
8097         /* initialize the mutiple tiles */
8098         /* ---------------------------- */
8099         cp->tcps = (opj_tcp_t*) opj_calloc(cp->tw * cp->th, sizeof(opj_tcp_t));
8100
8101         for (tileno = 0; tileno < cp->tw * cp->th; tileno++) {
8102                 opj_tcp_t *tcp = &cp->tcps[tileno];
8103                 tcp->numlayers = parameters->tcp_numlayers;
8104                 assert ( tcp->numlayers >= 0 );
8105                 for (j = 0; j < (OPJ_UINT32)tcp->numlayers; j++) {
8106                         if(cp->cinema){
8107                                 if (cp->fixed_quality) {
8108                                         tcp->distoratio[j] = parameters->tcp_distoratio[j];
8109                                 }
8110                                 tcp->rates[j] = parameters->tcp_rates[j];
8111                         }else{
8112                                 if (cp->fixed_quality) {        /* add fixed_quality */
8113                                         tcp->distoratio[j] = parameters->tcp_distoratio[j];
8114                                 } else {
8115                                         tcp->rates[j] = parameters->tcp_rates[j];
8116                                 }
8117                         }
8118                 }
8119                 tcp->csty = parameters->csty;
8120                 tcp->prg = parameters->prog_order;
8121                 tcp->mct = parameters->tcp_mct; 
8122
8123                 numpocs_tile = 0;
8124                 tcp->POC = 0;
8125                 if (parameters->numpocs) {
8126                         /* initialisation of POC */
8127                         tcp->POC = 1;
8128       assert( parameters->numpocs >= 0 );
8129                         for (i = 0; i < (OPJ_UINT32)parameters->numpocs; i++) {
8130                                 if((tileno == parameters->POC[i].tile - 1) || (parameters->POC[i].tile == -1)) {
8131                                         opj_poc_t *tcp_poc = &tcp->pocs[numpocs_tile];
8132                                         tcp_poc->resno0         = parameters->POC[numpocs_tile].resno0;
8133                                         tcp_poc->compno0        = parameters->POC[numpocs_tile].compno0;
8134                                         tcp_poc->layno1         = parameters->POC[numpocs_tile].layno1;
8135                                         tcp_poc->resno1         = parameters->POC[numpocs_tile].resno1;
8136                                         tcp_poc->compno1        = parameters->POC[numpocs_tile].compno1;
8137                                         tcp_poc->prg1           = parameters->POC[numpocs_tile].prg1;
8138                                         tcp_poc->tile           = parameters->POC[numpocs_tile].tile;
8139                                         numpocs_tile++;
8140                                 }
8141                         }
8142                         tcp->numpocs = numpocs_tile -1 ;
8143                 }else{ 
8144                         tcp->numpocs = 0;
8145                 }
8146
8147                 tcp->tccps = (opj_tccp_t*) opj_calloc(image->numcomps, sizeof(opj_tccp_t));
8148
8149                 for (i = 0; i < image->numcomps; i++) {
8150                         opj_tccp_t *tccp = &tcp->tccps[i];
8151                         tccp->csty = parameters->csty & 0x01;   /* 0 => one precinct || 1 => custom precinct  */
8152                         tccp->numresolutions = parameters->numresolution;
8153                         tccp->cblkw = int_floorlog2(parameters->cblockw_init);
8154                         tccp->cblkh = int_floorlog2(parameters->cblockh_init);
8155                         tccp->cblksty = parameters->mode;
8156                         tccp->qmfbid = parameters->irreversible ? 0 : 1;
8157                         tccp->qntsty = parameters->irreversible ? J2K_CCP_QNTSTY_SEQNT : J2K_CCP_QNTSTY_NOQNT;
8158                         tccp->numgbits = 2;
8159                         assert(parameters->roi_compno >= 0);
8160                         if (i == (OPJ_UINT32)parameters->roi_compno) {
8161                                 tccp->roishift = parameters->roi_shift;
8162                         } else {
8163                                 tccp->roishift = 0;
8164                         }
8165
8166                         if(parameters->cp_cinema)
8167                         {
8168                                 /*Precinct size for lowest frequency subband=128*/
8169                                 tccp->prcw[0] = 7;
8170                                 tccp->prch[0] = 7;
8171                                 /*Precinct size at all other resolutions = 256*/
8172                                 for (j = 1; j < tccp->numresolutions; j++) {
8173                                         tccp->prcw[j] = 8;
8174                                         tccp->prch[j] = 8;
8175                                 }
8176                         }else{
8177                                 if (parameters->csty & J2K_CCP_CSTY_PRT) {
8178                                         int p = 0;
8179                                         assert(tccp->numresolutions > 0);
8180                                         for (j = (OPJ_UINT32)(tccp->numresolutions - 1); (int)j >= 0; j--) {
8181                                                 if (p < parameters->res_spec) {
8182                                                         
8183                                                         if (parameters->prcw_init[p] < 1) {
8184                                                                 tccp->prcw[j] = 1;
8185                                                         } else {
8186                                                                 tccp->prcw[j] = int_floorlog2(parameters->prcw_init[p]);
8187                                                         }
8188                                                         
8189                                                         if (parameters->prch_init[p] < 1) {
8190                                                                 tccp->prch[j] = 1;
8191                                                         }else {
8192                                                                 tccp->prch[j] = int_floorlog2(parameters->prch_init[p]);
8193                                                         }
8194
8195                                                 } else {
8196                                                         int res_spec = parameters->res_spec;
8197                                                         int size_prcw = parameters->prcw_init[res_spec - 1] >> (p - (res_spec - 1));
8198                                                         int size_prch = parameters->prch_init[res_spec - 1] >> (p - (res_spec - 1));
8199                                                         
8200                                                         if (size_prcw < 1) {
8201                                                                 tccp->prcw[j] = 1;
8202                                                         } else {
8203                                                                 tccp->prcw[j] = int_floorlog2(size_prcw);
8204                                                         }
8205                                                         
8206                                                         if (size_prch < 1) {
8207                                                                 tccp->prch[j] = 1;
8208                                                         } else {
8209                                                                 tccp->prch[j] = int_floorlog2(size_prch);
8210                                                         }
8211                                                 }
8212                                                 p++;
8213                                                 /*printf("\nsize precinct for level %d : %d,%d\n", j,tccp->prcw[j], tccp->prch[j]); */
8214                                         }       /*end for*/
8215                                 } else {
8216                                         for (j = 0; j < tccp->numresolutions; j++) {
8217                                                 tccp->prcw[j] = 15;
8218                                                 tccp->prch[j] = 15;
8219                                         }
8220                                 }
8221                         }
8222
8223                         dwt_calc_explicit_stepsizes(tccp, image->comps[i].prec);
8224                 }
8225         }
8226 }
8227
8228 void j2k_setup_encoder_v2(      opj_j2k_v2_t *p_j2k,
8229                                                         opj_cparameters_t *parameters,
8230                                                         opj_image_t *image,
8231                                                         struct opj_event_mgr * p_manager)
8232 {
8233         OPJ_UINT32 i, j, tileno, numpocs_tile;
8234         opj_cp_v2_t *cp = 00;
8235         opj_bool l_res;
8236
8237         if(!p_j2k || !parameters || ! image) {
8238                 return;
8239         }
8240
8241         /* keep a link to cp so that we can destroy it later in j2k_destroy_compress */
8242         cp = &(p_j2k->m_cp);
8243
8244         /* set default values for cp */
8245         cp->tw = 1;
8246         cp->th = 1;
8247
8248         /*
8249         copy user encoding parameters
8250         */
8251         cp->m_specific_param.m_enc.m_cinema = parameters->cp_cinema;
8252         cp->m_specific_param.m_enc.m_max_comp_size =    parameters->max_comp_size;
8253         cp->rsiz   = parameters->cp_rsiz;
8254         cp->m_specific_param.m_enc.m_disto_alloc = parameters->cp_disto_alloc;
8255         cp->m_specific_param.m_enc.m_fixed_alloc = parameters->cp_fixed_alloc;
8256         cp->m_specific_param.m_enc.m_fixed_quality = parameters->cp_fixed_quality;
8257
8258         /* mod fixed_quality */
8259         if (parameters->cp_matrice) {
8260                 size_t array_size = parameters->tcp_numlayers * parameters->numresolution * 3 * sizeof(OPJ_INT32);
8261                 cp->m_specific_param.m_enc.m_matrice = (OPJ_INT32 *) opj_malloc(array_size);
8262                 memcpy(cp->m_specific_param.m_enc.m_matrice, parameters->cp_matrice, array_size);
8263         }
8264
8265         /* tiles */
8266         cp->tdx = parameters->cp_tdx;
8267         cp->tdy = parameters->cp_tdy;
8268
8269         /* tile offset */
8270         cp->tx0 = parameters->cp_tx0;
8271         cp->ty0 = parameters->cp_ty0;
8272
8273         /* comment string */
8274         if(parameters->cp_comment) {
8275                 cp->comment = (char*)opj_malloc(strlen(parameters->cp_comment) + 1);
8276                 if(cp->comment) {
8277                         strcpy(cp->comment, parameters->cp_comment);
8278                 }
8279         }
8280
8281         /*
8282         calculate other encoding parameters
8283         */
8284
8285         if (parameters->tile_size_on) {
8286                 cp->tw = int_ceildiv(image->x1 - cp->tx0, cp->tdx);
8287                 cp->th = int_ceildiv(image->y1 - cp->ty0, cp->tdy);
8288         } else {
8289                 cp->tdx = image->x1 - cp->tx0;
8290                 cp->tdy = image->y1 - cp->ty0;
8291         }
8292
8293         if (parameters->tp_on) {
8294                 cp->m_specific_param.m_enc.m_tp_flag = parameters->tp_flag;
8295                 cp->m_specific_param.m_enc.m_tp_on = 1;
8296         }
8297
8298 #ifdef USE_JPWL
8299         /*
8300         calculate JPWL encoding parameters
8301         */
8302
8303         if (parameters->jpwl_epc_on) {
8304                 OPJ_INT32 i;
8305
8306                 /* set JPWL on */
8307                 cp->epc_on = OPJ_TRUE;
8308                 cp->info_on = OPJ_FALSE; /* no informative technique */
8309
8310                 /* set EPB on */
8311                 if ((parameters->jpwl_hprot_MH > 0) || (parameters->jpwl_hprot_TPH[0] > 0)) {
8312                         cp->epb_on = OPJ_TRUE;
8313
8314                         cp->hprot_MH = parameters->jpwl_hprot_MH;
8315                         for (i = 0; i < JPWL_MAX_NO_TILESPECS; i++) {
8316                                 cp->hprot_TPH_tileno[i] = parameters->jpwl_hprot_TPH_tileno[i];
8317                                 cp->hprot_TPH[i] = parameters->jpwl_hprot_TPH[i];
8318                         }
8319                         /* if tile specs are not specified, copy MH specs */
8320                         if (cp->hprot_TPH[0] == -1) {
8321                                 cp->hprot_TPH_tileno[0] = 0;
8322                                 cp->hprot_TPH[0] = parameters->jpwl_hprot_MH;
8323                         }
8324                         for (i = 0; i < JPWL_MAX_NO_PACKSPECS; i++) {
8325                                 cp->pprot_tileno[i] = parameters->jpwl_pprot_tileno[i];
8326                                 cp->pprot_packno[i] = parameters->jpwl_pprot_packno[i];
8327                                 cp->pprot[i] = parameters->jpwl_pprot[i];
8328                         }
8329                 }
8330
8331                 /* set ESD writing */
8332                 if ((parameters->jpwl_sens_size == 1) || (parameters->jpwl_sens_size == 2)) {
8333                         cp->esd_on = OPJ_TRUE;
8334
8335                         cp->sens_size = parameters->jpwl_sens_size;
8336                         cp->sens_addr = parameters->jpwl_sens_addr;
8337                         cp->sens_range = parameters->jpwl_sens_range;
8338
8339                         cp->sens_MH = parameters->jpwl_sens_MH;
8340                         for (i = 0; i < JPWL_MAX_NO_TILESPECS; i++) {
8341                                 cp->sens_TPH_tileno[i] = parameters->jpwl_sens_TPH_tileno[i];
8342                                 cp->sens_TPH[i] = parameters->jpwl_sens_TPH[i];
8343                         }
8344                 }
8345
8346                 /* always set RED writing to false: we are at the encoder */
8347                 cp->red_on = OPJ_FALSE;
8348
8349         } else {
8350                 cp->epc_on = OPJ_FALSE;
8351         }
8352 #endif /* USE_JPWL */
8353
8354
8355         /* initialize the mutiple tiles */
8356         /* ---------------------------- */
8357         cp->tcps = (opj_tcp_v2_t*) opj_calloc(cp->tw * cp->th, sizeof(opj_tcp_v2_t));
8358         if (parameters->numpocs) {
8359                 /* initialisation of POC */
8360                 l_res = j2k_check_poc_val(parameters->POC,parameters->numpocs, parameters->numresolution, image->numcomps, parameters->tcp_numlayers, p_manager);
8361                 // TODO
8362         }
8363
8364         for (tileno = 0; tileno < cp->tw * cp->th; tileno++) {
8365                 opj_tcp_v2_t *tcp = &cp->tcps[tileno];
8366                 tcp->numlayers = parameters->tcp_numlayers;
8367
8368                 for (j = 0; j < tcp->numlayers; j++) {
8369                         if(cp->m_specific_param.m_enc.m_cinema){
8370                                 if (cp->m_specific_param.m_enc.m_fixed_quality) {
8371                                         tcp->distoratio[j] = parameters->tcp_distoratio[j];
8372                                 }
8373                                 tcp->rates[j] = parameters->tcp_rates[j];
8374                         }else{
8375                                 if (cp->m_specific_param.m_enc.m_fixed_quality) {       /* add fixed_quality */
8376                                         tcp->distoratio[j] = parameters->tcp_distoratio[j];
8377                                 } else {
8378                                         tcp->rates[j] = parameters->tcp_rates[j];
8379                                 }
8380                         }
8381                 }
8382
8383                 tcp->csty = parameters->csty;
8384                 tcp->prg = parameters->prog_order;
8385                 tcp->mct = parameters->tcp_mct;
8386
8387                 numpocs_tile = 0;
8388                 tcp->POC = 0;
8389
8390                 if (parameters->numpocs) {
8391                         /* initialisation of POC */
8392                         tcp->POC = 1;
8393                         // TODO
8394                         for (i = 0; i < (unsigned int) parameters->numpocs; i++) {
8395                                 if((tileno == parameters->POC[i].tile - 1) || (parameters->POC[i].tile == -1)) {
8396                                         opj_poc_t *tcp_poc = &tcp->pocs[numpocs_tile];
8397
8398                                         tcp_poc->resno0         = parameters->POC[numpocs_tile].resno0;
8399                                         tcp_poc->compno0        = parameters->POC[numpocs_tile].compno0;
8400                                         tcp_poc->layno1         = parameters->POC[numpocs_tile].layno1;
8401                                         tcp_poc->resno1         = parameters->POC[numpocs_tile].resno1;
8402                                         tcp_poc->compno1        = parameters->POC[numpocs_tile].compno1;
8403                                         tcp_poc->prg1           = parameters->POC[numpocs_tile].prg1;
8404                                         tcp_poc->tile           = parameters->POC[numpocs_tile].tile;
8405
8406                                         numpocs_tile++;
8407                                 }
8408                         }
8409
8410                         tcp->numpocs = numpocs_tile -1 ;
8411                 }else{
8412                         tcp->numpocs = 0;
8413                 }
8414
8415                 tcp->tccps = (opj_tccp_t*) opj_calloc(image->numcomps, sizeof(opj_tccp_t));
8416
8417                 if (parameters->mct_data) {
8418
8419                         opj_event_msg_v2(p_manager, EVT_ERROR, "MCT not supported for now\n");
8420                         return;
8421
8422                         /* TODO MSD : merge v2 add invert.c or used a external lib ?
8423                         OPJ_UINT32 lMctSize = image->numcomps * image->numcomps * sizeof(OPJ_FLOAT32);
8424                         OPJ_FLOAT32 * lTmpBuf = (OPJ_FLOAT32*)opj_malloc(lMctSize);
8425                         OPJ_INT32 * l_dc_shift = (OPJ_INT32 *) ((OPJ_BYTE *) parameters->mct_data + lMctSize);
8426
8427                         tcp->mct = 2;
8428                         tcp->m_mct_coding_matrix = (OPJ_FLOAT32*)opj_malloc(lMctSize);
8429                         memcpy(tcp->m_mct_coding_matrix,parameters->mct_data,lMctSize);
8430                         memcpy(lTmpBuf,parameters->mct_data,lMctSize);
8431
8432                         tcp->m_mct_decoding_matrix = (OPJ_FLOAT32*)opj_malloc(lMctSize);
8433                         assert(opj_matrix_inversion_f(lTmpBuf,(tcp->m_mct_decoding_matrix),image->numcomps));
8434
8435                         tcp->mct_norms = (OPJ_FLOAT64*)
8436                                         opj_malloc(image->numcomps * sizeof(OPJ_FLOAT64));
8437
8438                         opj_calculate_norms(tcp->mct_norms,image->numcomps,tcp->m_mct_decoding_matrix);
8439                         opj_free(lTmpBuf);
8440
8441                         for (i = 0; i < image->numcomps; i++) {
8442                                 opj_tccp_t *tccp = &tcp->tccps[i];
8443                                 tccp->m_dc_level_shift = l_dc_shift[i];
8444                         }
8445
8446                         j2k_setup_mct_encoding(tcp,image);
8447                         */
8448                 }
8449                 else {
8450                         for (i = 0; i < image->numcomps; i++) {
8451                                 opj_tccp_t *tccp = &tcp->tccps[i];
8452                                 opj_image_comp_t * l_comp = &(image->comps[i]);
8453
8454                                 if (! l_comp->sgnd) {
8455                                         tccp->m_dc_level_shift = 1 << (l_comp->prec - 1);
8456                                 }
8457                         }
8458                 }
8459
8460                 for (i = 0; i < image->numcomps; i++) {
8461                         opj_tccp_t *tccp = &tcp->tccps[i];
8462
8463                         tccp->csty = parameters->csty & 0x01;   /* 0 => one precinct || 1 => custom precinct  */
8464                         tccp->numresolutions = parameters->numresolution;
8465                         tccp->cblkw = int_floorlog2(parameters->cblockw_init);
8466                         tccp->cblkh = int_floorlog2(parameters->cblockh_init);
8467                         tccp->cblksty = parameters->mode;
8468                         tccp->qmfbid = parameters->irreversible ? 0 : 1;
8469                         tccp->qntsty = parameters->irreversible ? J2K_CCP_QNTSTY_SEQNT : J2K_CCP_QNTSTY_NOQNT;
8470                         tccp->numgbits = 2;
8471
8472                         if (i == parameters->roi_compno) {
8473                                 tccp->roishift = parameters->roi_shift;
8474                         } else {
8475                                 tccp->roishift = 0;
8476                         }
8477
8478                         if(parameters->cp_cinema) {
8479                                 //Precinct size for lowest frequency subband=128
8480                                 tccp->prcw[0] = 7;
8481                                 tccp->prch[0] = 7;
8482                                 //Precinct size at all other resolutions = 256
8483                                 for (j = 1; j < tccp->numresolutions; j++) {
8484                                         tccp->prcw[j] = 8;
8485                                         tccp->prch[j] = 8;
8486                                 }
8487                         }else{
8488                                 if (parameters->csty & J2K_CCP_CSTY_PRT) {
8489                                         OPJ_INT32 p = 0, it_res;
8490                                         for (it_res = tccp->numresolutions - 1; it_res >= 0; it_res--) {
8491                                                 if (p < parameters->res_spec) {
8492
8493                                                         if (parameters->prcw_init[p] < 1) {
8494                                                                 tccp->prcw[it_res] = 1;
8495                                                         } else {
8496                                                                 tccp->prcw[it_res] = int_floorlog2(parameters->prcw_init[p]);
8497                                                         }
8498
8499                                                         if (parameters->prch_init[p] < 1) {
8500                                                                 tccp->prch[it_res] = 1;
8501                                                         }else {
8502                                                                 tccp->prch[it_res] = int_floorlog2(parameters->prch_init[p]);
8503                                                         }
8504
8505                                                 } else {
8506                                                         int res_spec = parameters->res_spec;
8507                                                         int size_prcw = parameters->prcw_init[res_spec - 1] >> (p - (res_spec - 1));
8508                                                         int size_prch = parameters->prch_init[res_spec - 1] >> (p - (res_spec - 1));
8509
8510                                                         if (size_prcw < 1) {
8511                                                                 tccp->prcw[it_res] = 1;
8512                                                         } else {
8513                                                                 tccp->prcw[it_res] = int_floorlog2(size_prcw);
8514                                                         }
8515
8516                                                         if (size_prch < 1) {
8517                                                                 tccp->prch[it_res] = 1;
8518                                                         } else {
8519                                                                 tccp->prch[it_res] = int_floorlog2(size_prch);
8520                                                         }
8521                                                 }
8522                                                 p++;
8523                                                 /*printf("\nsize precinct for level %d : %d,%d\n", it_res,tccp->prcw[it_res], tccp->prch[it_res]); */
8524                                         }       //end for
8525                                 } else {
8526                                         for (j = 0; j < tccp->numresolutions; j++) {
8527                                                 tccp->prcw[j] = 15;
8528                                                 tccp->prch[j] = 15;
8529                                         }
8530                                 }
8531                         }
8532
8533                         dwt_calc_explicit_stepsizes(tccp, image->comps[i].prec);
8534                 }
8535         }
8536
8537         if (parameters->mct_data) {
8538                 opj_free(parameters->mct_data);
8539                 parameters->mct_data = 00;
8540         }
8541 }
8542
8543
8544 opj_bool j2k_encode(opj_j2k_t *j2k, opj_cio_t *cio, opj_image_t *image, opj_codestream_info_t *cstr_info) {
8545         int tileno;
8546   OPJ_UINT32 compno;
8547         opj_cp_t *cp = NULL;
8548
8549         opj_tcd_t *tcd = NULL;  /* TCD component */
8550
8551         j2k->cio = cio; 
8552         j2k->image = image;
8553
8554         cp = j2k->cp;
8555
8556         /* INDEX >> */
8557         j2k->cstr_info = cstr_info;
8558         if (cstr_info) {
8559                 OPJ_UINT32 compno;
8560                 cstr_info->tile = (opj_tile_info_t *) opj_malloc(cp->tw * cp->th * sizeof(opj_tile_info_t));
8561                 cstr_info->image_w = image->x1 - image->x0;
8562                 cstr_info->image_h = image->y1 - image->y0;
8563                 cstr_info->prog = (&cp->tcps[0])->prg;
8564                 cstr_info->tw = cp->tw;
8565                 cstr_info->th = cp->th;
8566                 cstr_info->tile_x = cp->tdx;    /* new version parser */
8567                 cstr_info->tile_y = cp->tdy;    /* new version parser */
8568                 cstr_info->tile_Ox = cp->tx0;   /* new version parser */
8569                 cstr_info->tile_Oy = cp->ty0;   /* new version parser */
8570                 cstr_info->numcomps = image->numcomps;
8571                 cstr_info->numlayers = (&cp->tcps[0])->numlayers;
8572                 cstr_info->numdecompos = (int*) opj_malloc(image->numcomps * sizeof(int));
8573                 for (compno=0; compno < image->numcomps; compno++) {
8574                         cstr_info->numdecompos[compno] = (&cp->tcps[0])->tccps->numresolutions - 1;
8575                 }
8576                 cstr_info->D_max = 0.0;         /* ADD Marcela */
8577                 cstr_info->main_head_start = cio_tell(cio); /* position of SOC */
8578                 cstr_info->maxmarknum = 100;
8579                 cstr_info->marker = (opj_marker_info_t *) opj_malloc(cstr_info->maxmarknum * sizeof(opj_marker_info_t));
8580                 cstr_info->marknum = 0;
8581         }
8582         /* << INDEX */
8583
8584         j2k_write_soc(j2k);
8585         j2k_write_siz(j2k);
8586         j2k_write_cod(j2k);
8587         j2k_write_qcd(j2k);
8588
8589         if(cp->cinema){
8590                 for (compno = 1; compno < image->numcomps; compno++) {
8591                         j2k_write_coc(j2k, compno);
8592                         j2k_write_qcc(j2k, compno);
8593                 }
8594         }
8595
8596         for (compno = 0; compno < image->numcomps; compno++) {
8597                 opj_tcp_t *tcp = &cp->tcps[0];
8598                 if (tcp->tccps[compno].roishift)
8599                         j2k_write_rgn(j2k, compno, 0);
8600         }
8601         if (cp->comment != NULL) {
8602                 j2k_write_com(j2k);
8603         }
8604
8605         j2k->totnum_tp = j2k_calculate_tp(cp,image->numcomps,image,j2k);
8606         /* TLM Marker*/
8607         if(cp->cinema){
8608                 j2k_write_tlm(j2k);
8609                 if (cp->cinema == CINEMA4K_24) {
8610                         j2k_write_poc(j2k);
8611                 }
8612         }
8613
8614         /* uncomment only for testing JPSEC marker writing */
8615         /* j2k_write_sec(j2k); */
8616
8617         /* INDEX >> */
8618         if(cstr_info) {
8619                 cstr_info->main_head_end = cio_tell(cio) - 1;
8620         }
8621         /* << INDEX */
8622         /**** Main Header ENDS here ***/
8623
8624         /* create the tile encoder */
8625         tcd = tcd_create(j2k->cinfo);
8626
8627         /* encode each tile */
8628         for (tileno = 0; tileno < cp->tw * cp->th; tileno++) {
8629                 int pino;
8630                 int tilepartno=0;
8631                 /* UniPG>> */
8632                 int acc_pack_num = 0;
8633                 /* <<UniPG */
8634
8635
8636                 opj_tcp_t *tcp = &cp->tcps[tileno];
8637                 opj_event_msg(j2k->cinfo, EVT_INFO, "tile number %d / %d\n", tileno + 1, cp->tw * cp->th);
8638
8639                 j2k->curtileno = tileno;
8640                 j2k->cur_tp_num = 0;
8641                 tcd->cur_totnum_tp = j2k->cur_totnum_tp[j2k->curtileno];
8642                 /* initialisation before tile encoding  */
8643                 if (tileno == 0) {
8644                         tcd_malloc_encode(tcd, image, cp, j2k->curtileno);
8645                 } else {
8646                         tcd_init_encode(tcd, image, cp, j2k->curtileno);
8647                 }
8648
8649                 /* INDEX >> */
8650                 if(cstr_info) {
8651                         cstr_info->tile[j2k->curtileno].start_pos = cio_tell(cio) + j2k->pos_correction;
8652                         cstr_info->tile[j2k->curtileno].maxmarknum = 10;
8653                         cstr_info->tile[j2k->curtileno].marker = (opj_marker_info_t *) opj_malloc(cstr_info->tile[j2k->curtileno].maxmarknum * sizeof(opj_marker_info_t));
8654                         cstr_info->tile[j2k->curtileno].marknum = 0;
8655                 }
8656                 /* << INDEX */
8657
8658                 for(pino = 0; pino <= tcp->numpocs; pino++) {
8659                         int tot_num_tp;
8660                         tcd->cur_pino=pino;
8661
8662                         /*Get number of tile parts*/
8663                         tot_num_tp = j2k_get_num_tp(cp,pino,tileno);
8664                         tcd->tp_pos = cp->tp_pos;
8665
8666                         for(tilepartno = 0; tilepartno < tot_num_tp ; tilepartno++){
8667                                 j2k->tp_num = tilepartno;
8668                                 /* INDEX >> */
8669                                 if(cstr_info)
8670                                         cstr_info->tile[j2k->curtileno].tp[j2k->cur_tp_num].tp_start_pos =
8671                                         cio_tell(cio) + j2k->pos_correction;
8672                                 /* << INDEX */
8673                                 j2k_write_sot(j2k);
8674
8675                                 if(j2k->cur_tp_num == 0 && cp->cinema == 0){
8676                                         for (compno = 1; compno < image->numcomps; compno++) {
8677                                                 j2k_write_coc(j2k, compno);
8678                                                 j2k_write_qcc(j2k, compno);
8679                                         }
8680                                         if (cp->tcps[tileno].numpocs) {
8681                                                 j2k_write_poc(j2k);
8682                                         }
8683                                 }
8684
8685                                 /* INDEX >> */
8686                                 if(cstr_info)
8687                                         cstr_info->tile[j2k->curtileno].tp[j2k->cur_tp_num].tp_end_header =
8688                                         cio_tell(cio) + j2k->pos_correction + 1;
8689                                 /* << INDEX */
8690
8691                                 j2k_write_sod(j2k, tcd);
8692
8693                                 /* INDEX >> */
8694                                 if(cstr_info) {
8695                                         cstr_info->tile[j2k->curtileno].tp[j2k->cur_tp_num].tp_end_pos =
8696                                                 cio_tell(cio) + j2k->pos_correction - 1;
8697                                         cstr_info->tile[j2k->curtileno].tp[j2k->cur_tp_num].tp_start_pack =
8698                                                 acc_pack_num;
8699                                         cstr_info->tile[j2k->curtileno].tp[j2k->cur_tp_num].tp_numpacks =
8700                                                 cstr_info->packno - acc_pack_num;
8701                                         acc_pack_num = cstr_info->packno;
8702                                 }
8703                                 /* << INDEX */
8704
8705                                 j2k->cur_tp_num++;
8706                         }                       
8707                 }
8708                 if(cstr_info) {
8709                         cstr_info->tile[j2k->curtileno].end_pos = cio_tell(cio) + j2k->pos_correction - 1;
8710                 }
8711
8712
8713                 /*
8714                 if (tile->PPT) { // BAD PPT !!! 
8715                 FILE *PPT_file;
8716                 int i;
8717                 PPT_file=fopen("PPT","rb");
8718                 fprintf(stderr,"%c%c%c%c",255,97,tile->len_ppt/256,tile->len_ppt%256);
8719                 for (i=0;i<tile->len_ppt;i++) {
8720                 unsigned char elmt;
8721                 fread(&elmt, 1, 1, PPT_file);
8722                 fwrite(&elmt,1,1,f);
8723                 }
8724                 fclose(PPT_file);
8725                 unlink("PPT");
8726                 }
8727                 */
8728
8729         }
8730
8731         /* destroy the tile encoder */
8732         tcd_free_encode(tcd);
8733         tcd_destroy(tcd);
8734
8735         opj_free(j2k->cur_totnum_tp);
8736
8737         j2k_write_eoc(j2k);
8738
8739         if(cstr_info) {
8740                 cstr_info->codestream_size = cio_tell(cio) + j2k->pos_correction;
8741                 /* UniPG>> */
8742                 /* The following adjustment is done to adjust the codestream size */
8743                 /* if SOD is not at 0 in the buffer. Useful in case of JP2, where */
8744                 /* the first bunch of bytes is not in the codestream              */
8745                 cstr_info->codestream_size -= cstr_info->main_head_start;
8746                 /* <<UniPG */
8747         }
8748
8749 #ifdef USE_JPWL
8750         /*
8751         preparation of JPWL marker segments
8752         */
8753         if(cp->epc_on) {
8754
8755                 /* encode according to JPWL */
8756                 jpwl_encode(j2k, cio, image);
8757
8758         }
8759 #endif /* USE_JPWL */
8760
8761         return OPJ_TRUE;
8762 }
8763
8764 static void j2k_add_mhmarker(opj_codestream_info_t *cstr_info, unsigned short int type, int pos, int len)
8765 {
8766         assert(cstr_info != 00);
8767
8768         /* expand the list? */
8769         if ((cstr_info->marknum + 1) > cstr_info->maxmarknum) {
8770                 cstr_info->maxmarknum = 100 + (int) ((float) cstr_info->maxmarknum * 1.0F);
8771                 cstr_info->marker = (opj_marker_info_t*)opj_realloc(cstr_info->marker, cstr_info->maxmarknum);
8772         }
8773
8774         /* add the marker */
8775         cstr_info->marker[cstr_info->marknum].type = type;
8776         cstr_info->marker[cstr_info->marknum].pos = pos;
8777         cstr_info->marker[cstr_info->marknum].len = len;
8778         cstr_info->marknum++;
8779
8780 }
8781
8782 static void j2k_add_mhmarker_v2(opj_codestream_index_t *cstr_index, OPJ_UINT32 type, OPJ_OFF_T pos, OPJ_UINT32 len)
8783 {
8784         assert(cstr_index != 00);
8785
8786         /* expand the list? */
8787         if ((cstr_index->marknum + 1) > cstr_index->maxmarknum) {
8788                 cstr_index->maxmarknum = 100 + (int) ((float) cstr_index->maxmarknum * 1.0F);
8789                 cstr_index->marker = (opj_marker_info_t*)opj_realloc(cstr_index->marker, cstr_index->maxmarknum *sizeof(opj_marker_info_t));
8790         }
8791
8792         /* add the marker */
8793         cstr_index->marker[cstr_index->marknum].type = (OPJ_UINT16)type;
8794         cstr_index->marker[cstr_index->marknum].pos = (OPJ_INT32)pos;
8795         cstr_index->marker[cstr_index->marknum].len = (OPJ_INT32)len;
8796         cstr_index->marknum++;
8797
8798 }
8799
8800 static void j2k_add_tlmarker( int tileno, opj_codestream_info_t *cstr_info, unsigned short int type, int pos, int len)
8801 {
8802         opj_marker_info_t *marker;
8803
8804         assert(cstr_info != 00);
8805
8806         /* expand the list? */
8807         if ((cstr_info->tile[tileno].marknum + 1) > cstr_info->tile[tileno].maxmarknum) {
8808                 cstr_info->tile[tileno].maxmarknum = 100 + (int) ((float) cstr_info->tile[tileno].maxmarknum * 1.0F);
8809                 cstr_info->tile[tileno].marker = (opj_marker_info_t*)opj_realloc(cstr_info->tile[tileno].marker, cstr_info->maxmarknum);
8810         }
8811
8812         marker = &(cstr_info->tile[tileno].marker[cstr_info->tile[tileno].marknum]);
8813
8814         /* add the marker */
8815         marker->type = type;
8816         marker->pos = pos;
8817         marker->len = len;
8818         cstr_info->tile[tileno].marknum++;
8819 }
8820
8821 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)
8822 {
8823         assert(cstr_index != 00);
8824         assert(cstr_index->tile_index != 00);
8825
8826         /* expand the list? */
8827         if ((cstr_index->tile_index[tileno].marknum + 1) > cstr_index->tile_index[tileno].maxmarknum) {
8828                 cstr_index->tile_index[tileno].maxmarknum = 100 + (int) ((float) cstr_index->tile_index[tileno].maxmarknum * 1.0F);
8829                 cstr_index->tile_index[tileno].marker =
8830                                 (opj_marker_info_t*)opj_realloc(cstr_index->tile_index[tileno].marker,
8831                                                                                                 cstr_index->tile_index[tileno].maxmarknum *sizeof(opj_marker_info_t));
8832         }
8833
8834         /* add the marker */
8835         cstr_index->tile_index[tileno].marker[cstr_index->tile_index[tileno].marknum].type = (OPJ_UINT16)type;
8836         cstr_index->tile_index[tileno].marker[cstr_index->tile_index[tileno].marknum].pos = (OPJ_INT32)pos;
8837         cstr_index->tile_index[tileno].marker[cstr_index->tile_index[tileno].marknum].len = (OPJ_INT32)len;
8838         cstr_index->tile_index[tileno].marknum++;
8839
8840         if (type == J2K_MS_SOT) {
8841                 OPJ_UINT32 l_current_tile_part = cstr_index->tile_index[tileno].current_tpsno;
8842
8843                 if (cstr_index->tile_index[tileno].tp_index)
8844                         cstr_index->tile_index[tileno].tp_index[l_current_tile_part].start_pos = pos;
8845
8846         }
8847 }
8848
8849
8850 /*
8851  * -----------------------------------------------------------------------
8852  * -----------------------------------------------------------------------
8853  * -----------------------------------------------------------------------
8854  */
8855
8856 /**
8857  * Ends the decompression procedures and possibiliy add data to be read after the
8858  * codestream.
8859  */
8860 opj_bool j2k_end_decompress(
8861                                                 opj_j2k_v2_t *p_j2k,
8862                                                 opj_stream_private_t *p_stream,
8863                                                 opj_event_mgr_t * p_manager)
8864 {
8865   (void)p_j2k;
8866   (void)p_stream;
8867   (void)p_manager;
8868         return OPJ_TRUE;
8869 }
8870
8871 /**
8872  * Reads a jpeg2000 codestream header structure.
8873
8874  *
8875  * @param p_stream the stream to read data from.
8876  * @param p_j2k the jpeg2000 codec.
8877  * @param p_manager the user event manager.
8878  *
8879  * @return true if the box is valid.
8880  */
8881 opj_bool j2k_read_header(       struct opj_stream_private *p_stream,
8882                                                         opj_j2k_v2_t* p_j2k,
8883                                                         opj_image_t** p_image,
8884                                                         struct opj_event_mgr* p_manager )
8885 {
8886         /* preconditions */
8887         assert(p_j2k != 00);
8888         assert(p_stream != 00);
8889         assert(p_manager != 00);
8890
8891         /* create an empty image header */
8892         p_j2k->m_private_image = opj_image_create0();
8893         if (! p_j2k->m_private_image) {
8894                 return OPJ_FALSE;
8895         }
8896
8897         /* customization of the validation */
8898         j2k_setup_decoding_validation(p_j2k);
8899
8900         /* validation of the parameters codec */
8901         if (! j2k_exec(p_j2k, p_j2k->m_validation_list, p_stream,p_manager)) {
8902                 opj_image_destroy(p_j2k->m_private_image);
8903                 p_j2k->m_private_image = NULL;
8904                 return OPJ_FALSE;
8905         }
8906
8907         /* customization of the encoding */
8908         j2k_setup_header_reading(p_j2k);
8909
8910         /* read header */
8911         if (! j2k_exec (p_j2k,p_j2k->m_procedure_list,p_stream,p_manager)) {
8912                 opj_image_destroy(p_j2k->m_private_image);
8913                 p_j2k->m_private_image = NULL;
8914                 return OPJ_FALSE;
8915         }
8916
8917         *p_image = opj_image_create0();
8918         if (! (*p_image)) {
8919                 return OPJ_FALSE;
8920         }
8921
8922         /* Copy codestream image information to the output image */
8923         opj_copy_image_header(p_j2k->m_private_image, *p_image);
8924
8925     /*Allocate and initialize some elements of codestrem index*/
8926         if (!j2k_allocate_tile_element_cstr_index(p_j2k)){
8927                 return OPJ_FALSE;
8928         }
8929
8930         return OPJ_TRUE;
8931 }
8932
8933 /**
8934  * Sets up the procedures to do on reading header. Developpers wanting to extend the library can add their own reading procedures.
8935  */
8936 void j2k_setup_header_reading (opj_j2k_v2_t *p_j2k)
8937 {
8938         /* preconditions*/
8939         assert(p_j2k != 00);
8940
8941         opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_read_header_procedure);
8942
8943         /* DEVELOPER CORNER, add your custom procedures */
8944         opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_copy_default_tcp_and_create_tcd);
8945
8946 }
8947
8948 /**
8949  * Sets up the validation ,i.e. adds the procedures to lauch to make sure the codec parameters
8950  * are valid. Developpers wanting to extend the library can add their own validation procedures.
8951  */
8952 void j2k_setup_decoding_validation (opj_j2k_v2_t *p_j2k)
8953 {
8954         /* preconditions*/
8955         assert(p_j2k != 00);
8956
8957         opj_procedure_list_add_procedure(p_j2k->m_validation_list, (opj_procedure)j2k_build_decoder);
8958         opj_procedure_list_add_procedure(p_j2k->m_validation_list, (opj_procedure)j2k_decoding_validation);
8959         /* DEVELOPER CORNER, add your custom validation procedure */
8960
8961 }
8962
8963
8964 /**
8965  * The mct encoding validation procedure.
8966  *
8967  * @param       p_j2k                   the jpeg2000 codec to validate.
8968  * @param       p_stream                                the input stream to validate.
8969  * @param       p_manager               the user event manager.
8970  *
8971  * @return true if the parameters are correct.
8972  */
8973 opj_bool j2k_mct_validation (   opj_j2k_v2_t * p_j2k,
8974                                                                 opj_stream_private_t *p_stream,
8975                                                                 opj_event_mgr_t * p_manager )
8976 {
8977         opj_bool l_is_valid = OPJ_TRUE;
8978         OPJ_UINT32 i,j;
8979
8980         /* preconditions */
8981         assert(p_j2k != 00);
8982         assert(p_stream != 00);
8983         assert(p_manager != 00);
8984
8985         if ((p_j2k->m_cp.rsiz & 0x8200) == 0x8200) {
8986                 OPJ_UINT32 l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw;
8987                 opj_tcp_v2_t * l_tcp = p_j2k->m_cp.tcps;
8988
8989                 for (i=0;i<l_nb_tiles;++i) {
8990                         if (l_tcp->mct == 2) {
8991                                 opj_tccp_t * l_tccp = l_tcp->tccps;
8992                                 l_is_valid &= (l_tcp->m_mct_coding_matrix != 00);
8993
8994                                 for (j=0;j<p_j2k->m_private_image->numcomps;++j) {
8995                                         l_is_valid &= ! (l_tccp->qmfbid & 1);
8996                                         ++l_tccp;
8997                                 }
8998                         }
8999                         ++l_tcp;
9000                 }
9001         }
9002
9003         return l_is_valid;
9004 }
9005
9006 opj_bool j2k_setup_mct_encoding(opj_tcp_v2_t * p_tcp, opj_image_t * p_image)
9007 {
9008         OPJ_UINT32 i;
9009         OPJ_UINT32 l_indix = 1;
9010         opj_mct_data_t * l_mct_deco_data = 00,* l_mct_offset_data = 00;
9011         opj_simple_mcc_decorrelation_data_t * l_mcc_data;
9012         OPJ_UINT32 l_mct_size,l_nb_elem;
9013         OPJ_FLOAT32 * l_data, * l_current_data;
9014         opj_tccp_t * l_tccp;
9015
9016         // preconditions
9017         assert(p_tcp != 00);
9018
9019         if (p_tcp->mct != 2) {
9020                 return OPJ_TRUE;
9021         }
9022
9023         if (p_tcp->m_mct_decoding_matrix) {
9024                 if (p_tcp->m_nb_mct_records == p_tcp->m_nb_max_mct_records) {
9025                         p_tcp->m_nb_max_mct_records += J2K_MCT_DEFAULT_NB_RECORDS;
9026
9027                         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));
9028                         if (! p_tcp->m_mct_records) {
9029                                 return OPJ_FALSE;
9030                         }
9031                         l_mct_deco_data = p_tcp->m_mct_records + p_tcp->m_nb_mct_records;
9032
9033                         memset(l_mct_deco_data ,0,(p_tcp->m_nb_max_mct_records - p_tcp->m_nb_mct_records) * sizeof(opj_mct_data_t));
9034                 }
9035                 l_mct_deco_data = p_tcp->m_mct_records + p_tcp->m_nb_mct_records;
9036
9037                 if (l_mct_deco_data->m_data) {
9038                         opj_free(l_mct_deco_data->m_data);
9039                         l_mct_deco_data->m_data = 00;
9040                 }
9041
9042                 l_mct_deco_data->m_index = l_indix++;
9043                 l_mct_deco_data->m_array_type = MCT_TYPE_DECORRELATION;
9044                 l_mct_deco_data->m_element_type = MCT_TYPE_FLOAT;
9045                 l_nb_elem = p_image->numcomps * p_image->numcomps;
9046                 l_mct_size = l_nb_elem * MCT_ELEMENT_SIZE[l_mct_deco_data->m_element_type];
9047                 l_mct_deco_data->m_data = (OPJ_BYTE*)opj_malloc(l_mct_size );
9048
9049                 if (! l_mct_deco_data->m_data) {
9050                         return OPJ_FALSE;
9051                 }
9052
9053                 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);
9054
9055                 l_mct_deco_data->m_data_size = l_mct_size;
9056                 ++p_tcp->m_nb_mct_records;
9057         }
9058
9059         if (p_tcp->m_nb_mct_records == p_tcp->m_nb_max_mct_records) {
9060                 p_tcp->m_nb_max_mct_records += J2K_MCT_DEFAULT_NB_RECORDS;
9061                 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));
9062
9063                 if (! p_tcp->m_mct_records) {
9064                         return OPJ_FALSE;
9065                 }
9066
9067                 l_mct_offset_data = p_tcp->m_mct_records + p_tcp->m_nb_mct_records;
9068                 memset(l_mct_offset_data ,0,(p_tcp->m_nb_max_mct_records - p_tcp->m_nb_mct_records) * sizeof(opj_mct_data_t));
9069
9070                 if (l_mct_deco_data) {
9071                         l_mct_deco_data = l_mct_offset_data - 1;
9072                 }
9073         }
9074
9075         l_mct_offset_data = p_tcp->m_mct_records + p_tcp->m_nb_mct_records;
9076
9077         if (l_mct_offset_data->m_data) {
9078                 opj_free(l_mct_offset_data->m_data);
9079                 l_mct_offset_data->m_data = 00;
9080         }
9081
9082         l_mct_offset_data->m_index = l_indix++;
9083         l_mct_offset_data->m_array_type = MCT_TYPE_OFFSET;
9084         l_mct_offset_data->m_element_type = MCT_TYPE_FLOAT;
9085         l_nb_elem = p_image->numcomps;
9086         l_mct_size = l_nb_elem * MCT_ELEMENT_SIZE[l_mct_offset_data->m_element_type];
9087         l_mct_offset_data->m_data = (OPJ_BYTE*)opj_malloc(l_mct_size );
9088
9089         if (! l_mct_offset_data->m_data) {
9090                 return OPJ_FALSE;
9091         }
9092
9093         l_data = (OPJ_FLOAT32*)opj_malloc(l_nb_elem * sizeof(OPJ_FLOAT32));
9094         if (! l_data) {
9095                 opj_free(l_mct_offset_data->m_data);
9096                 l_mct_offset_data->m_data = 00;
9097                 return OPJ_FALSE;
9098         }
9099
9100         l_tccp = p_tcp->tccps;
9101         l_current_data = l_data;
9102
9103         for (i=0;i<l_nb_elem;++i) {
9104                 *(l_current_data++) = (OPJ_FLOAT32) (l_tccp->m_dc_level_shift);
9105                 ++l_tccp;
9106         }
9107
9108         j2k_mct_write_functions_from_float[l_mct_offset_data->m_element_type](l_data,l_mct_offset_data->m_data,l_nb_elem);
9109
9110         opj_free(l_data);
9111
9112         l_mct_offset_data->m_data_size = l_mct_size;
9113
9114         ++p_tcp->m_nb_mct_records;
9115
9116         if (p_tcp->m_nb_mcc_records == p_tcp->m_nb_max_mcc_records) {
9117                 p_tcp->m_nb_max_mcc_records += J2K_MCT_DEFAULT_NB_RECORDS;
9118                 p_tcp->m_mcc_records = (opj_simple_mcc_decorrelation_data_t*)
9119                 opj_realloc(p_tcp->m_mcc_records,p_tcp->m_nb_max_mcc_records * sizeof(opj_simple_mcc_decorrelation_data_t));
9120
9121                 if (! p_tcp->m_mcc_records) {
9122                         return OPJ_FALSE;
9123                 }
9124                 l_mcc_data = p_tcp->m_mcc_records + p_tcp->m_nb_mcc_records;
9125                 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));
9126
9127         }
9128
9129         l_mcc_data = p_tcp->m_mcc_records + p_tcp->m_nb_mcc_records;
9130         l_mcc_data->m_decorrelation_array = l_mct_deco_data;
9131         l_mcc_data->m_is_irreversible = 1;
9132         l_mcc_data->m_nb_comps = p_image->numcomps;
9133         l_mcc_data->m_index = l_indix++;
9134         l_mcc_data->m_offset_array = l_mct_offset_data;
9135         ++p_tcp->m_nb_mcc_records;
9136
9137         return OPJ_TRUE;
9138 }
9139
9140 /**
9141  * Builds the cp decoder parameters to use to decode tile.
9142  */
9143 opj_bool j2k_build_decoder (opj_j2k_v2_t * p_j2k,
9144                                                         opj_stream_private_t *p_stream,
9145                                                         opj_event_mgr_t * p_manager )
9146 {
9147         /* add here initialization of cp
9148            copy paste of setup_decoder */
9149   (void)p_j2k;
9150   (void)p_stream;
9151   (void)p_manager;
9152         return OPJ_TRUE;
9153 }
9154
9155 /**
9156  * Builds the cp encoder parameters to use to encode tile.
9157  */
9158 opj_bool j2k_build_encoder (opj_j2k_v2_t * p_j2k,
9159                                                         opj_stream_private_t *p_stream,
9160                                                         opj_event_mgr_t * p_manager )
9161 {
9162         /* add here initialization of cp
9163            copy paste of setup_encoder */
9164   (void)p_j2k;
9165   (void)p_stream;
9166   (void)p_manager;
9167         return OPJ_TRUE;
9168 }
9169
9170 /**
9171  * The default encoding validation procedure without any extension.
9172  *
9173  * @param       p_j2k                   the jpeg2000 codec to validate.
9174  * @param       p_stream                                the input stream to validate.
9175  * @param       p_manager               the user event manager.
9176  *
9177  * @return true if the parameters are correct.
9178  */
9179 opj_bool j2k_encoding_validation (      opj_j2k_v2_t * p_j2k,
9180                                                                         opj_stream_private_t *p_stream,
9181                                                                         opj_event_mgr_t * p_manager )
9182 {
9183         opj_bool l_is_valid = OPJ_TRUE;
9184
9185         /* preconditions */
9186         assert(p_j2k != 00);
9187         assert(p_stream != 00);
9188         assert(p_manager != 00);
9189
9190         /* STATE checking */
9191         /* make sure the state is at 0 */
9192         l_is_valid &= (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_NONE);
9193
9194         /* POINTER validation */
9195         /* make sure a p_j2k codec is present */
9196         l_is_valid &= (p_j2k->m_procedure_list != 00);
9197         /* make sure a validation list is present */
9198         l_is_valid &= (p_j2k->m_validation_list != 00);
9199
9200         if ((p_j2k->m_cp.tdx) < (OPJ_UINT32) (1 << p_j2k->m_cp.tcps->tccps->numresolutions)) {
9201                 opj_event_msg_v2(p_manager, EVT_ERROR, "Number of resolutions is too high in comparison to the size of tiles\n");
9202                 return OPJ_FALSE;
9203         }
9204
9205         if ((p_j2k->m_cp.tdy) < (OPJ_UINT32) (1 << p_j2k->m_cp.tcps->tccps->numresolutions)) {
9206                 opj_event_msg_v2(p_manager, EVT_ERROR, "Number of resolutions is too high in comparison to the size of tiles\n");
9207                 return OPJ_FALSE;
9208         }
9209
9210         /* PARAMETER VALIDATION */
9211         return l_is_valid;
9212 }
9213
9214 /**
9215  * The default decoding validation procedure without any extension.
9216  *
9217  * @param       p_j2k                   the jpeg2000 codec to validate.
9218  * @param       p_stream                                the input stream to validate.
9219  * @param       p_manager               the user event manager.
9220  *
9221  * @return true if the parameters are correct.
9222  */
9223 opj_bool j2k_decoding_validation (
9224                                                                 opj_j2k_v2_t *p_j2k,
9225                                                                 opj_stream_private_t *p_stream,
9226                                                                 opj_event_mgr_t * p_manager
9227                                                           )
9228 {
9229         opj_bool l_is_valid = OPJ_TRUE;
9230
9231         /* preconditions*/
9232         assert(p_j2k != 00);
9233         assert(p_stream != 00);
9234         assert(p_manager != 00);
9235
9236
9237         /* STATE checking */
9238         /* make sure the state is at 0 */
9239 #ifdef TODO_MSD
9240         l_is_valid &= (p_j2k->m_specific_param.m_decoder.m_state == J2K_DEC_STATE_NONE);
9241 #endif
9242         l_is_valid &= (p_j2k->m_specific_param.m_decoder.m_state == 0x0000);
9243
9244         /* POINTER validation */
9245         /* make sure a p_j2k codec is present */
9246         /* make sure a procedure list is present */
9247         l_is_valid &= (p_j2k->m_procedure_list != 00);
9248         /* make sure a validation list is present */
9249         l_is_valid &= (p_j2k->m_validation_list != 00);
9250
9251         /* PARAMETER VALIDATION */
9252         return l_is_valid;
9253 }
9254
9255 opj_bool j2k_read_header_procedure(     opj_j2k_v2_t *p_j2k,
9256                                                                         struct opj_stream_private *p_stream,
9257                                                                         struct opj_event_mgr * p_manager)
9258 {
9259         OPJ_UINT32 l_current_marker;
9260         OPJ_UINT32 l_marker_size;
9261         const opj_dec_memory_marker_handler_t * l_marker_handler = 00;
9262
9263         /* preconditions */
9264         assert(p_stream != 00);
9265         assert(p_j2k != 00);
9266         assert(p_manager != 00);
9267
9268         /*  We enter in the main header */
9269         p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_MHSOC;
9270
9271         /* Try to read the SOC marker, the codestream must begin with SOC marker */
9272         if (! j2k_read_soc_v2(p_j2k,p_stream,p_manager)) {
9273                 opj_event_msg_v2(p_manager, EVT_ERROR, "Expected a SOC marker \n");
9274                 return OPJ_FALSE;
9275         }
9276
9277         /* Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer */
9278         if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
9279                 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
9280                 return OPJ_FALSE;
9281         }
9282
9283         /* Read 2 bytes as the new marker ID */
9284         opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_current_marker,2);
9285
9286         /* Try to read until the SOT is detected */
9287         while (l_current_marker != J2K_MS_SOT) {
9288
9289                 /* Check if the current marker ID is valid */
9290                 if (l_current_marker < 0xff00) {
9291                         opj_event_msg_v2(p_manager, EVT_ERROR, "We expected read a marker ID (0xff--) instead of %.8x\n", l_current_marker);
9292                         return OPJ_FALSE;
9293                 }
9294
9295                 /* Get the marker handler from the marker ID */
9296                 l_marker_handler = j2k_get_marker_handler(l_current_marker);
9297
9298                 /* Manage case where marker is unknown */
9299                 if (l_marker_handler->id == J2K_MS_UNK) {
9300                         if (! j2k_read_unk_v2(p_j2k, p_stream, &l_current_marker, p_manager)){
9301                                 opj_event_msg_v2(p_manager, EVT_ERROR, "Unknow marker have been detected and generated error.\n");
9302                                 return OPJ_FALSE;
9303                         }
9304
9305                         if (l_current_marker == J2K_MS_SOT)
9306                                 break; /* SOT marker is detected main header is completely read */
9307                         else    /* Get the marker handler from the marker ID */
9308                                 l_marker_handler = j2k_get_marker_handler(l_current_marker);
9309                 }
9310
9311                 /* Check if the marker is known and if it is the right place to find it */
9312                 if (! (p_j2k->m_specific_param.m_decoder.m_state & l_marker_handler->states) ) {
9313                         opj_event_msg_v2(p_manager, EVT_ERROR, "Marker is not compliant with its position\n");
9314                         return OPJ_FALSE;
9315                 }
9316
9317                 /* Try to read 2 bytes (the marker size) from stream and copy them into the buffer */
9318                 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
9319                         opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
9320                         return OPJ_FALSE;
9321                 }
9322
9323                 /* read 2 bytes as the marker size */
9324                 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_marker_size,2);
9325                 l_marker_size -= 2; /* Subtract the size of the marker ID already read */
9326
9327                 /* Check if the marker size is compatible with the header data size */
9328                 if (l_marker_size > p_j2k->m_specific_param.m_decoder.m_header_data_size) {
9329                         p_j2k->m_specific_param.m_decoder.m_header_data = (OPJ_BYTE*)
9330                                         opj_realloc(p_j2k->m_specific_param.m_decoder.m_header_data,l_marker_size);
9331                         if (p_j2k->m_specific_param.m_decoder.m_header_data == 00) {
9332                                 return OPJ_FALSE;
9333                         }
9334                         p_j2k->m_specific_param.m_decoder.m_header_data_size = l_marker_size;
9335                 }
9336
9337                 /* Try to read the rest of the marker segment from stream and copy them into the buffer */
9338                 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) {
9339                         opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
9340                         return OPJ_FALSE;
9341                 }
9342
9343                 /* Read the marker segment with the correct marker handler */
9344                 if (! (*(l_marker_handler->handler))(p_j2k,p_j2k->m_specific_param.m_decoder.m_header_data,l_marker_size,p_manager)) {
9345                         opj_event_msg_v2(p_manager, EVT_ERROR, "Marker handler function failed to read the marker segment\n");
9346                         return OPJ_FALSE;
9347                 }
9348
9349                 /* Add the marker to the codestream index*/
9350                 j2k_add_mhmarker_v2(p_j2k->cstr_index,
9351                                                         l_marker_handler->id,
9352                                                         (OPJ_UINT32) opj_stream_tell(p_stream) - l_marker_size - 4,
9353                                                         l_marker_size + 4 );
9354
9355                 /* Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer */
9356                 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
9357                         opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
9358                         return OPJ_FALSE;
9359                 }
9360
9361                 /* read 2 bytes as the new marker ID */
9362                 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_current_marker,2);
9363         }
9364
9365         opj_event_msg_v2(p_manager, EVT_INFO, "Main header has been correctly decoded.\n");
9366
9367         /* Position of the last element if the main header */
9368         p_j2k->cstr_index->main_head_end = (OPJ_UINT32) opj_stream_tell(p_stream) - 2;
9369
9370         /* Next step: read a tile-part header */
9371         p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_TPHSOT;
9372
9373         return OPJ_TRUE;
9374 }
9375
9376 /**
9377  * Excutes the given procedures on the given codec.
9378  *
9379  * @param       p_procedure_list        the list of procedures to execute
9380  * @param       p_j2k                                   the jpeg2000 codec to execute the procedures on.
9381  * @param       p_stream                                        the stream to execute the procedures on.
9382  * @param       p_manager                       the user manager.
9383  *
9384  * @return      true                            if all the procedures were successfully executed.
9385  */
9386 opj_bool j2k_exec (     opj_j2k_v2_t * p_j2k,
9387                                         opj_procedure_list_t * p_procedure_list,
9388                                         opj_stream_private_t *p_stream,
9389                                         opj_event_mgr_t * p_manager )
9390 {
9391         opj_bool (** l_procedure) (opj_j2k_v2_t * ,opj_stream_private_t *,opj_event_mgr_t *) = 00;
9392         opj_bool l_result = OPJ_TRUE;
9393         OPJ_UINT32 l_nb_proc, i;
9394
9395         /* preconditions*/
9396         assert(p_procedure_list != 00);
9397         assert(p_j2k != 00);
9398         assert(p_stream != 00);
9399         assert(p_manager != 00);
9400
9401
9402         l_nb_proc = opj_procedure_list_get_nb_procedures(p_procedure_list);
9403         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);
9404
9405         for     (i=0;i<l_nb_proc;++i) {
9406                 l_result = l_result && ((*l_procedure) (p_j2k,p_stream,p_manager));
9407                 ++l_procedure;
9408         }
9409
9410         /* and clear the procedure list at the end.*/
9411         opj_procedure_list_clear(p_procedure_list);
9412         return l_result;
9413 }
9414
9415 /* FIXME DOC*/
9416 opj_bool j2k_copy_default_tcp_and_create_tcd
9417                                                 (
9418                                                 opj_j2k_v2_t * p_j2k,
9419                                                 opj_stream_private_t *p_stream,
9420                                                 opj_event_mgr_t * p_manager
9421                                                 )
9422 {
9423         opj_tcp_v2_t * l_tcp = 00;
9424         opj_tcp_v2_t * l_default_tcp = 00;
9425         OPJ_UINT32 l_nb_tiles;
9426         OPJ_UINT32 i,j;
9427         opj_tccp_t *l_current_tccp = 00;
9428         OPJ_UINT32 l_tccp_size;
9429         OPJ_UINT32 l_mct_size;
9430         opj_image_t * l_image;
9431         OPJ_UINT32 l_mcc_records_size,l_mct_records_size;
9432         opj_mct_data_t * l_src_mct_rec, *l_dest_mct_rec;
9433         opj_simple_mcc_decorrelation_data_t * l_src_mcc_rec, *l_dest_mcc_rec;
9434         OPJ_UINT32 l_offset;
9435
9436         /* preconditions */
9437         assert(p_j2k != 00);
9438         assert(p_stream != 00);
9439         assert(p_manager != 00);
9440
9441         l_image = p_j2k->m_private_image;
9442         l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw;
9443         l_tcp = p_j2k->m_cp.tcps;
9444         l_tccp_size = l_image->numcomps * sizeof(opj_tccp_t);
9445         l_default_tcp = p_j2k->m_specific_param.m_decoder.m_default_tcp;
9446         l_mct_size = l_image->numcomps * l_image->numcomps * sizeof(OPJ_FLOAT32);
9447
9448         /* For each tile */
9449         for (i=0; i<l_nb_tiles; ++i) {
9450                 /* keep the tile-compo coding parameters pointer of the current tile coding parameters*/
9451                 l_current_tccp = l_tcp->tccps;
9452                 /*Copy default coding parameters into the current tile coding parameters*/
9453                 memcpy(l_tcp, l_default_tcp, sizeof(opj_tcp_v2_t));
9454                 /* Initialize some values of the current tile coding parameters*/
9455                 l_tcp->ppt = 0;
9456                 l_tcp->ppt_data = 00;
9457                 /* Reconnect the tile-compo coding parameters pointer to the current tile coding parameters*/
9458                 l_tcp->tccps = l_current_tccp;
9459
9460                 /* Get the mct_decoding_matrix of the dflt_tile_cp and copy them into the current tile cp*/
9461                 if (l_default_tcp->m_mct_decoding_matrix) {
9462                         l_tcp->m_mct_decoding_matrix = (OPJ_FLOAT32*)opj_malloc(l_mct_size);
9463                         if (! l_tcp->m_mct_decoding_matrix ) {
9464                                 return OPJ_FALSE;
9465                         }
9466                         memcpy(l_tcp->m_mct_decoding_matrix,l_default_tcp->m_mct_decoding_matrix,l_mct_size);
9467                 }
9468
9469                 /* Get the mct_record of the dflt_tile_cp and copy them into the current tile cp*/
9470                 l_mct_records_size = l_default_tcp->m_nb_max_mct_records * sizeof(opj_mct_data_t);
9471                 l_tcp->m_mct_records = (opj_mct_data_t*)opj_malloc(l_mct_records_size);
9472                 if (! l_tcp->m_mct_records) {
9473                         return OPJ_FALSE;
9474                 }
9475                 memcpy(l_tcp->m_mct_records, l_default_tcp->m_mct_records,l_mct_records_size);
9476
9477                 /* Copy the mct record data from dflt_tile_cp to the current tile*/
9478                 l_src_mct_rec = l_default_tcp->m_mct_records;
9479                 l_dest_mct_rec = l_tcp->m_mct_records;
9480
9481                 for (j=0;j<l_default_tcp->m_nb_mct_records;++j) {
9482
9483                         if (l_src_mct_rec->m_data) {
9484
9485                                 l_dest_mct_rec->m_data = (OPJ_BYTE*) opj_malloc(l_src_mct_rec->m_data_size);
9486                                 if(! l_dest_mct_rec->m_data) {
9487                                         return OPJ_FALSE;
9488                                 }
9489                                 memcpy(l_dest_mct_rec->m_data,l_src_mct_rec->m_data,l_src_mct_rec->m_data_size);
9490                         }
9491
9492                         ++l_src_mct_rec;
9493                         ++l_dest_mct_rec;
9494                 }
9495
9496                 /* Get the mcc_record of the dflt_tile_cp and copy them into the current tile cp*/
9497                 l_mcc_records_size = l_default_tcp->m_nb_max_mcc_records * sizeof(opj_simple_mcc_decorrelation_data_t);
9498                 l_tcp->m_mcc_records = (opj_simple_mcc_decorrelation_data_t*) opj_malloc(l_mcc_records_size);
9499                 if (! l_tcp->m_mcc_records) {
9500                         return OPJ_FALSE;
9501                 }
9502                 memcpy(l_tcp->m_mcc_records,l_default_tcp->m_mcc_records,l_mcc_records_size);
9503
9504                 /* Copy the mcc record data from dflt_tile_cp to the current tile*/
9505                 l_src_mcc_rec = l_default_tcp->m_mcc_records;
9506                 l_dest_mcc_rec = l_tcp->m_mcc_records;
9507
9508                 for (j=0;j<l_default_tcp->m_nb_max_mcc_records;++j) {
9509
9510                         if (l_src_mcc_rec->m_decorrelation_array) {
9511                                 l_offset = l_src_mcc_rec->m_decorrelation_array - l_default_tcp->m_mct_records;
9512                                 l_dest_mcc_rec->m_decorrelation_array = l_tcp->m_mct_records + l_offset;
9513                         }
9514
9515                         if (l_src_mcc_rec->m_offset_array) {
9516                                 l_offset = l_src_mcc_rec->m_offset_array - l_default_tcp->m_mct_records;
9517                                 l_dest_mcc_rec->m_offset_array = l_tcp->m_mct_records + l_offset;
9518                         }
9519
9520                         ++l_src_mcc_rec;
9521                         ++l_dest_mcc_rec;
9522                 }
9523
9524                 /* Copy all the dflt_tile_compo_cp to the current tile cp */
9525                 memcpy(l_current_tccp,l_default_tcp->tccps,l_tccp_size);
9526
9527                 /* Move to next tile cp*/
9528                 ++l_tcp;
9529         }
9530
9531         /* Create the current tile decoder*/
9532         p_j2k->m_tcd = (opj_tcd_v2_t*)tcd_create_v2(OPJ_TRUE); /* FIXME why a cast ? */
9533         if (! p_j2k->m_tcd ) {
9534                 return OPJ_FALSE;
9535         }
9536
9537         if ( !tcd_init_v2(p_j2k->m_tcd, l_image, &(p_j2k->m_cp)) ) {
9538                 tcd_destroy_v2(p_j2k->m_tcd);
9539                 p_j2k->m_tcd = 00;
9540                 opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot decode tile, memory error\n");
9541                 return OPJ_FALSE;
9542         }
9543
9544         return OPJ_TRUE;
9545 }
9546
9547 /**
9548  * Reads the lookup table containing all the marker, status and action, and returns the handler associated
9549  * with the marker value.
9550  * @param       p_id            Marker value to look up
9551  *
9552  * @return      the handler associated with the id.
9553 */
9554 const opj_dec_memory_marker_handler_t * j2k_get_marker_handler (OPJ_UINT32 p_id)
9555 {
9556         const opj_dec_memory_marker_handler_t *e;
9557         for (e = j2k_memory_marker_handler_tab; e->id != 0; ++e) {
9558                 if (e->id == p_id) {
9559                         break; /* we find a handler corresponding to the marker ID*/
9560                 }
9561         }
9562         return e;
9563 }
9564
9565
9566 /**
9567  * Destroys a jpeg2000 codec.
9568  *
9569  * @param       p_j2k   the jpeg20000 structure to destroy.
9570  */
9571 void j2k_destroy (opj_j2k_v2_t *p_j2k)
9572 {
9573         if (p_j2k == 00) {
9574                 return;
9575         }
9576
9577         if (p_j2k->m_is_decoder) {
9578
9579                 if (p_j2k->m_specific_param.m_decoder.m_default_tcp != 00) {
9580                         j2k_tcp_destroy(p_j2k->m_specific_param.m_decoder.m_default_tcp);
9581                         opj_free(p_j2k->m_specific_param.m_decoder.m_default_tcp);
9582                         p_j2k->m_specific_param.m_decoder.m_default_tcp = 00;
9583                 }
9584
9585                 if (p_j2k->m_specific_param.m_decoder.m_header_data != 00) {
9586                         opj_free(p_j2k->m_specific_param.m_decoder.m_header_data);
9587                         p_j2k->m_specific_param.m_decoder.m_header_data = 00;
9588                         p_j2k->m_specific_param.m_decoder.m_header_data_size = 0;
9589                 }
9590         }
9591         else {
9592
9593                 if (p_j2k->m_specific_param.m_encoder.m_encoded_tile_data) {
9594                         opj_free(p_j2k->m_specific_param.m_encoder.m_encoded_tile_data);
9595                         p_j2k->m_specific_param.m_encoder.m_encoded_tile_data = 00;
9596                 }
9597
9598                 if (p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer) {
9599                         opj_free(p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer);
9600                         p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer = 00;
9601                         p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current = 00;
9602                 }
9603
9604                 if (p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
9605                         opj_free(p_j2k->m_specific_param.m_encoder.m_header_tile_data);
9606                         p_j2k->m_specific_param.m_encoder.m_header_tile_data = 00;
9607                         p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = 0;
9608                 }
9609         }
9610
9611         tcd_destroy_v2(p_j2k->m_tcd);
9612
9613         j2k_cp_destroy(&(p_j2k->m_cp));
9614         memset(&(p_j2k->m_cp),0,sizeof(opj_cp_v2_t));
9615
9616         opj_procedure_list_destroy(p_j2k->m_procedure_list);
9617         p_j2k->m_procedure_list = 00;
9618
9619         opj_procedure_list_destroy(p_j2k->m_validation_list);
9620         p_j2k->m_procedure_list = 00;
9621
9622         j2k_destroy_cstr_index(p_j2k->cstr_index);
9623         p_j2k->cstr_index = NULL;
9624
9625         opj_image_destroy(p_j2k->m_private_image);
9626         p_j2k->m_private_image = NULL;
9627
9628         opj_image_destroy(p_j2k->m_output_image);
9629         p_j2k->m_output_image = NULL;
9630
9631         opj_free(p_j2k);
9632 }
9633
9634 void j2k_destroy_cstr_index (opj_codestream_index_t *p_cstr_ind)
9635 {
9636         if (p_cstr_ind) {
9637
9638                 if (p_cstr_ind->marker) {
9639                         opj_free(p_cstr_ind->marker);
9640                         p_cstr_ind->marker = NULL;
9641                 }
9642
9643                 if (p_cstr_ind->tile_index) {
9644                         OPJ_UINT32 it_tile = 0;
9645
9646                         for (it_tile=0; it_tile < p_cstr_ind->nb_of_tiles; it_tile++) {
9647
9648                                 if(p_cstr_ind->tile_index[it_tile].packet_index) {
9649                                         opj_free(p_cstr_ind->tile_index[it_tile].packet_index);
9650                                         p_cstr_ind->tile_index[it_tile].packet_index = NULL;
9651                                 }
9652
9653                                 if(p_cstr_ind->tile_index[it_tile].tp_index){
9654                                         opj_free(p_cstr_ind->tile_index[it_tile].tp_index);
9655                                         p_cstr_ind->tile_index[it_tile].tp_index = NULL;
9656                                 }
9657
9658                                 if(p_cstr_ind->tile_index[it_tile].marker){
9659                                         opj_free(p_cstr_ind->tile_index[it_tile].marker);
9660                                         p_cstr_ind->tile_index[it_tile].marker = NULL;
9661
9662                                 }
9663                         }
9664
9665                         opj_free( p_cstr_ind->tile_index);
9666                         p_cstr_ind->tile_index = NULL;
9667                 }
9668
9669                 opj_free(p_cstr_ind);
9670         }
9671 }
9672
9673
9674
9675 /**
9676  * Destroys a tile coding parameter structure.
9677  *
9678  * @param       p_tcp           the tile coding parameter to destroy.
9679  */
9680 void j2k_tcp_destroy (opj_tcp_v2_t *p_tcp)
9681 {
9682         if (p_tcp == 00) {
9683                 return;
9684         }
9685
9686         if (p_tcp->ppt_buffer != 00) {
9687                 opj_free(p_tcp->ppt_buffer);
9688                 p_tcp->ppt_buffer = 00;
9689         }
9690
9691         if (p_tcp->tccps != 00) {
9692                 opj_free(p_tcp->tccps);
9693                 p_tcp->tccps = 00;
9694         }
9695
9696         if (p_tcp->m_mct_coding_matrix != 00) {
9697                 opj_free(p_tcp->m_mct_coding_matrix);
9698                 p_tcp->m_mct_coding_matrix = 00;
9699         }
9700
9701         if (p_tcp->m_mct_decoding_matrix != 00) {
9702                 opj_free(p_tcp->m_mct_decoding_matrix);
9703                 p_tcp->m_mct_decoding_matrix = 00;
9704         }
9705
9706         if (p_tcp->m_mcc_records) {
9707                 opj_free(p_tcp->m_mcc_records);
9708                 p_tcp->m_mcc_records = 00;
9709                 p_tcp->m_nb_max_mcc_records = 0;
9710                 p_tcp->m_nb_mcc_records = 0;
9711         }
9712
9713         if (p_tcp->m_mct_records) {
9714                 opj_mct_data_t * l_mct_data = p_tcp->m_mct_records;
9715                 OPJ_UINT32 i;
9716
9717                 for (i=0;i<p_tcp->m_nb_mct_records;++i) {
9718                         if (l_mct_data->m_data) {
9719                                 opj_free(l_mct_data->m_data);
9720                                 l_mct_data->m_data = 00;
9721                         }
9722
9723                         ++l_mct_data;
9724                 }
9725
9726                 opj_free(p_tcp->m_mct_records);
9727                 p_tcp->m_mct_records = 00;
9728         }
9729
9730         if (p_tcp->mct_norms != 00) {
9731                 opj_free(p_tcp->mct_norms);
9732                 p_tcp->mct_norms = 00;
9733         }
9734
9735         j2k_tcp_data_destroy(p_tcp);
9736
9737 }
9738
9739 /**
9740  * Destroys the data inside a tile coding parameter structure.
9741  *
9742  * @param       p_tcp           the tile coding parameter which contain data to destroy.
9743  */
9744 void j2k_tcp_data_destroy (opj_tcp_v2_t *p_tcp)
9745 {
9746         if (p_tcp->m_data) {
9747                 opj_free(p_tcp->m_data);
9748                 p_tcp->m_data = NULL;
9749                 p_tcp->m_data_size = 0;
9750         }
9751 }
9752
9753 /**
9754  * Destroys a coding parameter structure.
9755  *
9756  * @param       p_cp            the coding parameter to destroy.
9757  */
9758 void j2k_cp_destroy (opj_cp_v2_t *p_cp)
9759 {
9760         OPJ_UINT32 l_nb_tiles;
9761         opj_tcp_v2_t * l_current_tile = 00;
9762         OPJ_UINT32 i;
9763
9764         if
9765                 (p_cp == 00)
9766         {
9767                 return;
9768         }
9769         if
9770                 (p_cp->tcps != 00)
9771         {
9772                 l_current_tile = p_cp->tcps;
9773                 l_nb_tiles = p_cp->th * p_cp->tw;
9774
9775                 for
9776                         (i = 0; i < l_nb_tiles; ++i)
9777                 {
9778                         j2k_tcp_destroy(l_current_tile);
9779                         ++l_current_tile;
9780                 }
9781                 opj_free(p_cp->tcps);
9782                 p_cp->tcps = 00;
9783         }
9784         if
9785                 (p_cp->ppm_buffer != 00)
9786         {
9787                 opj_free(p_cp->ppm_buffer);
9788                 p_cp->ppm_buffer = 00;
9789         }
9790         if
9791                 (p_cp->comment != 00)
9792         {
9793                 opj_free(p_cp->comment);
9794                 p_cp->comment = 00;
9795         }
9796         if
9797                 (! p_cp->m_is_decoder)
9798         {
9799                 if
9800                         (p_cp->m_specific_param.m_enc.m_matrice)
9801                 {
9802                         opj_free(p_cp->m_specific_param.m_enc.m_matrice);
9803                         p_cp->m_specific_param.m_enc.m_matrice = 00;
9804                 }
9805         }
9806 }
9807
9808
9809
9810 /**
9811  * Reads a tile header.
9812  * @param       p_j2k           the jpeg2000 codec.
9813  * @param       p_stream                        the stream to write data to.
9814  * @param       p_manager       the user event manager.
9815  */
9816 opj_bool j2k_read_tile_header(  opj_j2k_v2_t * p_j2k,
9817                                                                 OPJ_UINT32 * p_tile_index,
9818                                                                 OPJ_UINT32 * p_data_size,
9819                                                                 OPJ_INT32 * p_tile_x0, OPJ_INT32 * p_tile_y0,
9820                                                                 OPJ_INT32 * p_tile_x1, OPJ_INT32 * p_tile_y1,
9821                                                                 OPJ_UINT32 * p_nb_comps,
9822                                                                 opj_bool * p_go_on,
9823                                                                 opj_stream_private_t *p_stream,
9824                                                                 opj_event_mgr_t * p_manager )
9825 {
9826         OPJ_UINT32 l_current_marker = J2K_MS_SOT;
9827         OPJ_UINT32 l_marker_size;
9828         const opj_dec_memory_marker_handler_t * l_marker_handler = 00;
9829         opj_tcp_v2_t * l_tcp = NULL;
9830         OPJ_UINT32 l_nb_tiles;
9831
9832         /* preconditions */
9833         assert(p_stream != 00);
9834         assert(p_j2k != 00);
9835         assert(p_manager != 00);
9836
9837         /* Reach the End Of Codestream ?*/
9838         if (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_EOC){
9839                 l_current_marker = J2K_MS_EOC;
9840         }
9841         /* We need to encounter a SOT marker (a new tile-part header) */
9842         else if (p_j2k->m_specific_param.m_decoder.m_state != J2K_STATE_TPHSOT){
9843                 return OPJ_FALSE;
9844         }
9845
9846         /* Read into the codestream until reach the EOC or ! can_decode ??? FIXME */
9847         while ( (!p_j2k->m_specific_param.m_decoder.m_can_decode) && (l_current_marker != J2K_MS_EOC) ) {
9848
9849                 /* Try to read until the Start Of Data is detected */
9850                 while (l_current_marker != J2K_MS_SOD) {
9851
9852                         /* Try to read 2 bytes (the marker size) from stream and copy them into the buffer */
9853                         if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
9854                                 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
9855                                 return OPJ_FALSE;
9856                         }
9857
9858                         /* Read 2 bytes from the buffer as the marker size */
9859                         opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_marker_size,2);
9860
9861                         /* Why this condition? FIXME */
9862                         if (p_j2k->m_specific_param.m_decoder.m_state & J2K_STATE_TPH){
9863                                 p_j2k->m_specific_param.m_decoder.m_sot_length -= (l_marker_size + 2);
9864                         }
9865                         l_marker_size -= 2; /* Subtract the size of the marker ID already read */
9866
9867                         /* Get the marker handler from the marker ID */
9868                         l_marker_handler = j2k_get_marker_handler(l_current_marker);
9869
9870                         /* Check if the marker is known and if it is the right place to find it */
9871                         if (! (p_j2k->m_specific_param.m_decoder.m_state & l_marker_handler->states) ) {
9872                                 opj_event_msg_v2(p_manager, EVT_ERROR, "Marker is not compliant with its position\n");
9873                                 return OPJ_FALSE;
9874                         }
9875 /* FIXME manage case of unknown marker as in the main header ? */
9876
9877                         /* Check if the marker size is compatible with the header data size */
9878                         if (l_marker_size > p_j2k->m_specific_param.m_decoder.m_header_data_size) {
9879                                 p_j2k->m_specific_param.m_decoder.m_header_data = (OPJ_BYTE*)
9880                                         opj_realloc(p_j2k->m_specific_param.m_decoder.m_header_data,l_marker_size);
9881                                 if (p_j2k->m_specific_param.m_decoder.m_header_data == 00) {
9882                                         return OPJ_FALSE;
9883                                 }
9884                                 p_j2k->m_specific_param.m_decoder.m_header_data_size = l_marker_size;
9885                         }
9886
9887                         /* Try to read the rest of the marker segment from stream and copy them into the buffer */
9888                         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) {
9889                                 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
9890                                 return OPJ_FALSE;
9891                         }
9892
9893                         /* Read the marker segment with the correct marker handler */
9894                         if (! (*(l_marker_handler->handler))(p_j2k,p_j2k->m_specific_param.m_decoder.m_header_data,l_marker_size,p_manager)) {
9895                                 opj_event_msg_v2(p_manager, EVT_ERROR, "Fail to read the current marker segment (%#x)\n", l_current_marker);
9896                                 return OPJ_FALSE;
9897                         }
9898
9899                         /* Add the marker to the codestream index*/
9900                         j2k_add_tlmarker_v2(p_j2k->m_current_tile_number,
9901                                                                 p_j2k->cstr_index,
9902                                                                 l_marker_handler->id,
9903                                                                 (OPJ_UINT32) opj_stream_tell(p_stream) - l_marker_size - 4,
9904                                                                 l_marker_size + 4 );
9905
9906                         /* Keep the position of the last SOT marker read */
9907                         if ( l_marker_handler->id == J2K_MS_SOT ) {
9908                                 OPJ_UINT32 sot_pos = (OPJ_UINT32) opj_stream_tell(p_stream) - l_marker_size - 4 ;
9909                                 if (sot_pos > p_j2k->m_specific_param.m_decoder.m_last_sot_read_pos)
9910                                 {
9911                                         p_j2k->m_specific_param.m_decoder.m_last_sot_read_pos = sot_pos;
9912                                 }
9913                         }
9914
9915
9916                         if (p_j2k->m_specific_param.m_decoder.m_skip_data) {
9917                                 /* Skip the rest of the tile part header*/
9918                                 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) {
9919                                         opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
9920                                         return OPJ_FALSE;
9921                                 }
9922                                 l_current_marker = J2K_MS_SOD; /* Normally we reached a SOD */
9923                         }
9924                         else {
9925                                 /* Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer*/
9926                                 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
9927                                         opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
9928                                         return OPJ_FALSE;
9929                                 }
9930                                 /* Read 2 bytes from the buffer as the new marker ID */
9931                                 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_current_marker,2);
9932                         }
9933                 }
9934
9935                 /* If we didn't skip data before, we need to read the SOD marker*/
9936                 if (! p_j2k->m_specific_param.m_decoder.m_skip_data) {
9937                         /* Try to read the SOD marker and skip data ? FIXME */
9938                         if (! j2k_read_sod_v2(p_j2k, p_stream, p_manager)) {
9939                                 return OPJ_FALSE;
9940                         }
9941
9942
9943
9944                         if (! p_j2k->m_specific_param.m_decoder.m_can_decode){
9945                                 /* Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer */
9946                                 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
9947                                         opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
9948                                         return OPJ_FALSE;
9949                                 }
9950
9951                                 /* Read 2 bytes from buffer as the new marker ID */
9952                                 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_current_marker,2);
9953                         }
9954                 }
9955                 else {
9956                         /* Indicate we will try to read a new tile-part header*/
9957                         p_j2k->m_specific_param.m_decoder.m_skip_data = 0;
9958                         p_j2k->m_specific_param.m_decoder.m_can_decode = 0;
9959                         p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_TPHSOT;
9960
9961                         /* Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer */
9962                         if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
9963                                 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
9964                                 return OPJ_FALSE;
9965                         }
9966
9967                         /* Read 2 bytes from buffer as the new marker ID */
9968                         opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_current_marker,2);
9969                 }
9970         }
9971
9972         /* Current marker is the EOC marker ?*/
9973         if (l_current_marker == J2K_MS_EOC) {
9974                 if (p_j2k->m_specific_param.m_decoder.m_state != J2K_STATE_EOC ){
9975                         p_j2k->m_current_tile_number = 0;
9976                         p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_EOC;
9977                 }
9978         }
9979
9980         /* FIXME DOC ???*/
9981         if ( ! p_j2k->m_specific_param.m_decoder.m_can_decode) {
9982                 l_tcp = p_j2k->m_cp.tcps + p_j2k->m_current_tile_number;
9983                 l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw;
9984
9985                 while( (p_j2k->m_current_tile_number < l_nb_tiles) && (l_tcp->m_data == 00) ) {
9986                         ++p_j2k->m_current_tile_number;
9987                         ++l_tcp;
9988                 }
9989
9990                 if (p_j2k->m_current_tile_number == l_nb_tiles) {
9991                         *p_go_on = OPJ_FALSE;
9992                         return OPJ_TRUE;
9993                 }
9994         }
9995
9996         /*FIXME ???*/
9997         if (! tcd_init_decode_tile(p_j2k->m_tcd, p_j2k->m_current_tile_number)) {
9998                 opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot decode tile, memory error\n");
9999                 return OPJ_FALSE;
10000         }
10001
10002         opj_event_msg_v2(p_manager, EVT_INFO, "Header of tile %d / %d has been read.\n",
10003                         p_j2k->m_current_tile_number, (p_j2k->m_cp.th * p_j2k->m_cp.tw) - 1);
10004
10005         *p_tile_index = p_j2k->m_current_tile_number;
10006         *p_go_on = OPJ_TRUE;
10007         *p_data_size = tcd_get_decoded_tile_size(p_j2k->m_tcd);
10008         *p_tile_x0 = p_j2k->m_tcd->tcd_image->tiles->x0;
10009         *p_tile_y0 = p_j2k->m_tcd->tcd_image->tiles->y0;
10010         *p_tile_x1 = p_j2k->m_tcd->tcd_image->tiles->x1;
10011         *p_tile_y1 = p_j2k->m_tcd->tcd_image->tiles->y1;
10012         *p_nb_comps = p_j2k->m_tcd->tcd_image->tiles->numcomps;
10013
10014          p_j2k->m_specific_param.m_decoder.m_state |= 0x0080;/* FIXME J2K_DEC_STATE_DATA;*/
10015
10016         return OPJ_TRUE;
10017 }
10018
10019
10020 opj_bool j2k_decode_tile (      opj_j2k_v2_t * p_j2k,
10021                                                         OPJ_UINT32 p_tile_index,
10022                                                         OPJ_BYTE * p_data,
10023                                                         OPJ_UINT32 p_data_size,
10024                                                         opj_stream_private_t *p_stream,
10025                                                         opj_event_mgr_t * p_manager )
10026 {
10027         OPJ_UINT32 l_current_marker;
10028         OPJ_BYTE l_data [2];
10029         opj_tcp_v2_t * l_tcp;
10030
10031         /* preconditions */
10032         assert(p_stream != 00);
10033         assert(p_j2k != 00);
10034         assert(p_manager != 00);
10035
10036         if ( !(p_j2k->m_specific_param.m_decoder.m_state & 0x0080/*FIXME J2K_DEC_STATE_DATA*/)
10037                 || (p_tile_index != p_j2k->m_current_tile_number) ) {
10038                 return OPJ_FALSE;
10039         }
10040
10041         l_tcp = &(p_j2k->m_cp.tcps[p_tile_index]);
10042         if (! l_tcp->m_data) {
10043                 j2k_tcp_destroy(l_tcp);
10044                 return OPJ_FALSE;
10045         }
10046
10047         if (! tcd_decode_tile_v2(       p_j2k->m_tcd,
10048                                                                 l_tcp->m_data,
10049                                                                 l_tcp->m_data_size,
10050                                                                 p_tile_index,
10051                                                                 p_j2k->cstr_index) ) {
10052                 j2k_tcp_destroy(l_tcp);
10053                 p_j2k->m_specific_param.m_decoder.m_state |= 0x8000;/*FIXME J2K_DEC_STATE_ERR;*/
10054                 return OPJ_FALSE;
10055         }
10056
10057         if (! tcd_update_tile_data(p_j2k->m_tcd,p_data,p_data_size)) {
10058                 return OPJ_FALSE;
10059         }
10060
10061         /* To avoid to destroy the tcp which can be useful when we try to decode a tile decoded before (cf j2k_random_tile_access)
10062          * we destroy just the data which will be re-read in read_tile_header*/
10063         /*j2k_tcp_destroy(l_tcp);
10064         p_j2k->m_tcd->tcp = 0;*/
10065         j2k_tcp_data_destroy(l_tcp);
10066
10067         p_j2k->m_specific_param.m_decoder.m_can_decode = 0;
10068         p_j2k->m_specific_param.m_decoder.m_state &= (~ (0x0080));/* FIXME J2K_DEC_STATE_DATA);*/
10069
10070         if (p_j2k->m_specific_param.m_decoder.m_state != 0x0100){ /*FIXME J2K_DEC_STATE_EOC)*/
10071                 if (opj_stream_read_data(p_stream,l_data,2,p_manager) != 2) {
10072                         opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
10073                         return OPJ_FALSE;
10074                 }
10075
10076                 opj_read_bytes(l_data,&l_current_marker,2);
10077
10078                 if (l_current_marker == J2K_MS_EOC) {
10079                         p_j2k->m_current_tile_number = 0;
10080                         p_j2k->m_specific_param.m_decoder.m_state =  0x0100;/*FIXME J2K_DEC_STATE_EOC;*/
10081                 }
10082                 else if (l_current_marker != J2K_MS_SOT)
10083                 {
10084                         opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short, expected SOT\n");
10085                         return OPJ_FALSE;
10086                 }
10087         }
10088
10089         return OPJ_TRUE;
10090 }
10091
10092
10093 opj_bool j2k_update_image_data (opj_tcd_v2_t * p_tcd, OPJ_BYTE * p_data, opj_image_t* p_output_image)
10094 {
10095         OPJ_UINT32 i,j,k = 0;
10096         OPJ_UINT32 l_width_src,l_height_src;
10097         OPJ_UINT32 l_width_dest,l_height_dest;
10098         OPJ_INT32 l_offset_x0_src, l_offset_y0_src, l_offset_x1_src, l_offset_y1_src;
10099         OPJ_INT32 l_start_offset_src, l_line_offset_src, l_end_offset_src ;
10100         OPJ_UINT32 l_start_x_dest , l_start_y_dest;
10101         OPJ_UINT32 l_x0_dest, l_y0_dest, l_x1_dest, l_y1_dest;
10102         OPJ_INT32 l_start_offset_dest, l_line_offset_dest;
10103
10104         opj_image_comp_t * l_img_comp_src = 00;
10105         opj_image_comp_t * l_img_comp_dest = 00;
10106
10107         opj_tcd_tilecomp_v2_t * l_tilec = 00;
10108         opj_image_t * l_image_src = 00;
10109         OPJ_UINT32 l_size_comp, l_remaining;
10110         OPJ_INT32 * l_dest_ptr;
10111         opj_tcd_resolution_v2_t* l_res= 00;
10112
10113         l_tilec = p_tcd->tcd_image->tiles->comps;
10114         l_image_src = p_tcd->image;
10115         l_img_comp_src = l_image_src->comps;
10116
10117         l_img_comp_dest = p_output_image->comps;
10118
10119         for (i=0; i<l_image_src->numcomps; i++) {
10120
10121                 /* Allocate output component buffer if necessary */
10122                 if (!l_img_comp_dest->data) {
10123
10124                         l_img_comp_dest->data = (OPJ_INT32*) opj_calloc(l_img_comp_dest->w * l_img_comp_dest->h, sizeof(OPJ_INT32));
10125                         if (! l_img_comp_dest->data) {
10126                                 return OPJ_FALSE;
10127                         }
10128                 }
10129
10130                 /* Copy info from decoded comp image to output image */
10131                 l_img_comp_dest->resno_decoded = l_img_comp_src->resno_decoded;
10132
10133                 /*-----*/
10134                 /* Compute the precision of the output buffer */
10135                 l_size_comp = l_img_comp_src->prec >> 3; /*(/ 8)*/
10136                 l_remaining = l_img_comp_src->prec & 7;  /* (%8) */
10137                 l_res = l_tilec->resolutions + l_img_comp_src->resno_decoded;
10138
10139                 if (l_remaining) {
10140                         ++l_size_comp;
10141                 }
10142
10143                 if (l_size_comp == 3) {
10144                         l_size_comp = 4;
10145                 }
10146                 /*-----*/
10147
10148                 /* Current tile component size*/
10149                 /*if (i == 0) {
10150                 fprintf(stdout, "SRC: l_res_x0=%d, l_res_x1=%d, l_res_y0=%d, l_res_y1=%d\n",
10151                                 l_res->x0, l_res->x1, l_res->y0, l_res->y1);
10152                 }*/
10153
10154                 l_width_src = (l_res->x1 - l_res->x0);
10155                 l_height_src = (l_res->y1 - l_res->y0);
10156
10157                 /* Border of the current output component*/
10158                 l_x0_dest = int_ceildivpow2(l_img_comp_dest->x0, l_img_comp_dest->factor);
10159                 l_y0_dest = int_ceildivpow2(l_img_comp_dest->y0, l_img_comp_dest->factor);
10160                 l_x1_dest = l_x0_dest + l_img_comp_dest->w;
10161                 l_y1_dest = l_y0_dest + l_img_comp_dest->h;
10162
10163                 /*if (i == 0) {
10164                 fprintf(stdout, "DEST: l_x0_dest=%d, l_x1_dest=%d, l_y0_dest=%d, l_y1_dest=%d (%d)\n",
10165                                 l_x0_dest, l_x1_dest, l_y0_dest, l_y1_dest, l_img_comp_dest->factor );
10166                 }*/
10167
10168                 /*-----*/
10169                 /* Compute the area (l_offset_x0_src, l_offset_y0_src, l_offset_x1_src, l_offset_y1_src)
10170                  * of the input buffer (decoded tile component) which will be move
10171                  * in the output buffer. Compute the area of the output buffer (l_start_x_dest,
10172                  * l_start_y_dest, l_width_dest, l_height_dest)  which will be modified
10173                  * by this input area.
10174                  * */
10175                 assert( l_res->x0 >= 0);
10176                 assert( l_res->x1 >= 0);
10177                 if ( l_x0_dest < (OPJ_UINT32)l_res->x0 ) {
10178                         l_start_x_dest = l_res->x0 - l_x0_dest;
10179                         l_offset_x0_src = 0;
10180
10181                         if ( l_x1_dest >= (OPJ_UINT32)l_res->x1 ) {
10182                                 l_width_dest = l_width_src;
10183                                 l_offset_x1_src = 0;
10184                         }
10185                         else {
10186                                 l_width_dest = l_x1_dest - l_res->x0 ;
10187                                 l_offset_x1_src = l_width_src - l_width_dest;
10188                         }
10189                 }
10190                 else {
10191                         l_start_x_dest = 0 ;
10192                         l_offset_x0_src = l_x0_dest - l_res->x0;
10193
10194                         if ( l_x1_dest >= (OPJ_UINT32)l_res->x1 ) {
10195                                 l_width_dest = l_width_src - l_offset_x0_src;
10196                                 l_offset_x1_src = 0;
10197                         }
10198                         else {
10199                                 l_width_dest = l_img_comp_dest->w ;
10200                                 l_offset_x1_src = l_res->x1 - l_x1_dest;
10201                         }
10202                 }
10203
10204                 if ( l_y0_dest < (OPJ_UINT32)l_res->y0 ) {
10205                         l_start_y_dest = l_res->y0 - l_y0_dest;
10206                         l_offset_y0_src = 0;
10207
10208                         if ( l_y1_dest >= (OPJ_UINT32)l_res->y1 ) {
10209                                 l_height_dest = l_height_src;
10210                                 l_offset_y1_src = 0;
10211                         }
10212                         else {
10213                                 l_height_dest = l_y1_dest - l_res->y0 ;
10214                                 l_offset_y1_src =  l_height_src - l_height_dest;
10215                         }
10216                 }
10217                 else {
10218                         l_start_y_dest = 0 ;
10219                         l_offset_y0_src = l_y0_dest - l_res->y0;
10220
10221                         if ( l_y1_dest >= (OPJ_UINT32)l_res->y1 ) {
10222                                 l_height_dest = l_height_src - l_offset_y0_src;
10223                                 l_offset_y1_src = 0;
10224                         }
10225                         else {
10226                                 l_height_dest = l_img_comp_dest->h ;
10227                                 l_offset_y1_src = l_res->y1 - l_y1_dest;
10228                         }
10229                 }
10230
10231                 if( (l_offset_x0_src < 0 ) || (l_offset_y0_src < 0 ) || (l_offset_x1_src < 0 ) || (l_offset_y1_src < 0 ) ){
10232                         return OPJ_FALSE;
10233                 }
10234                 /*-----*/
10235
10236                 /* Compute the input buffer offset */
10237                 l_start_offset_src = l_offset_x0_src + l_offset_y0_src * l_width_src;
10238                 l_line_offset_src = l_offset_x1_src + l_offset_x0_src;
10239                 l_end_offset_src = l_offset_y1_src * l_width_src - l_offset_x0_src;
10240
10241                 /* Compute the output buffer offset */
10242                 l_start_offset_dest = l_start_x_dest + l_start_y_dest * l_img_comp_dest->w;
10243                 l_line_offset_dest = l_img_comp_dest->w - l_width_dest;
10244
10245                 /* Move the output buffer to the first place where we will write*/
10246                 l_dest_ptr = l_img_comp_dest->data + l_start_offset_dest;
10247
10248                 /*if (i == 0) {
10249                         fprintf(stdout, "COMPO[%d]:\n",i);
10250                         fprintf(stdout, "SRC: l_start_x_src=%d, l_start_y_src=%d, l_width_src=%d, l_height_src=%d\n"
10251                                         "\t tile offset:%d, %d, %d, %d\n"
10252                                         "\t buffer offset: %d; %d, %d\n",
10253                                         l_res->x0, l_res->y0, l_width_src, l_height_src,
10254                                         l_offset_x0_src, l_offset_y0_src, l_offset_x1_src, l_offset_y1_src,
10255                                         l_start_offset_src, l_line_offset_src, l_end_offset_src);
10256
10257                         fprintf(stdout, "DEST: l_start_x_dest=%d, l_start_y_dest=%d, l_width_dest=%d, l_height_dest=%d\n"
10258                                         "\t start offset: %d, line offset= %d\n",
10259                                         l_start_x_dest, l_start_y_dest, l_width_dest, l_height_dest, l_start_offset_dest, l_line_offset_dest);
10260                 }*/
10261
10262
10263                 switch (l_size_comp) {
10264                         case 1:
10265                                 {
10266                                         OPJ_CHAR * l_src_ptr = (OPJ_CHAR*) p_data;
10267                                         l_src_ptr += l_start_offset_src; /* Move to the first place where we will read*/
10268
10269                                         if (l_img_comp_src->sgnd) {
10270                                                 for (j = 0 ; j < l_height_dest ; ++j) {
10271                                                         for ( k = 0 ; k < l_width_dest ; ++k) {
10272                                                                 *(l_dest_ptr++) = (OPJ_INT32) (*(l_src_ptr++)); /* Copy only the data needed for the output image */
10273                                                         }
10274
10275                                                         l_dest_ptr+= l_line_offset_dest; /* Move to the next place where we will write */
10276                                                         l_src_ptr += l_line_offset_src ; /* Move to the next place where we will read */
10277                                                 }
10278                                         }
10279                                         else {
10280                                                 for ( j = 0 ; j < l_height_dest ; ++j ) {
10281                                                         for ( k = 0 ; k < l_width_dest ; ++k) {
10282                                                                 *(l_dest_ptr++) = (OPJ_INT32) ((*(l_src_ptr++))&0xff);
10283                                                         }
10284
10285                                                         l_dest_ptr+= l_line_offset_dest;
10286                                                         l_src_ptr += l_line_offset_src;
10287                                                 }
10288                                         }
10289
10290                                         l_src_ptr += l_end_offset_src; /* Move to the end of this component-part of the input buffer */
10291                                         p_data = (OPJ_BYTE*) l_src_ptr; /* Keep the current position for the next component-part */
10292                                 }
10293                                 break;
10294                         case 2:
10295                                 {
10296                                         OPJ_INT16 * l_src_ptr = (OPJ_INT16 *) p_data;
10297                                         l_src_ptr += l_start_offset_src;
10298
10299                                         if (l_img_comp_src->sgnd) {
10300                                                 for (j=0;j<l_height_dest;++j) {
10301                                                         for (k=0;k<l_width_dest;++k) {
10302                                                                 *(l_dest_ptr++) = *(l_src_ptr++);
10303                                                         }
10304
10305                                                         l_dest_ptr+= l_line_offset_dest;
10306                                                         l_src_ptr += l_line_offset_src ;
10307                                                 }
10308                                         }
10309                                         else {
10310                                                 for (j=0;j<l_height_dest;++j) {
10311                                                         for (k=0;k<l_width_dest;++k) {
10312                                                                 *(l_dest_ptr++) = (*(l_src_ptr++))&0xffff;
10313                                                         }
10314
10315                                                         l_dest_ptr+= l_line_offset_dest;
10316                                                         l_src_ptr += l_line_offset_src ;
10317                                                 }
10318                                         }
10319
10320                                         l_src_ptr += l_end_offset_src;
10321                                         p_data = (OPJ_BYTE*) l_src_ptr;
10322                                 }
10323                                 break;
10324                         case 4:
10325                                 {
10326                                         OPJ_INT32 * l_src_ptr = (OPJ_INT32 *) p_data;
10327                                         l_src_ptr += l_start_offset_src;
10328
10329                                         for (j=0;j<l_height_dest;++j) {
10330                                                 for (k=0;k<l_width_dest;++k) {
10331                                                         *(l_dest_ptr++) = (*(l_src_ptr++));
10332                                                 }
10333
10334                                                 l_dest_ptr+= l_line_offset_dest;
10335                                                 l_src_ptr += l_line_offset_src ;
10336                                         }
10337
10338                                         l_src_ptr += l_end_offset_src;
10339                                         p_data = (OPJ_BYTE*) l_src_ptr;
10340                                 }
10341                                 break;
10342                 }
10343
10344                 ++l_img_comp_dest;
10345                 ++l_img_comp_src;
10346                 ++l_tilec;
10347         }
10348
10349         return OPJ_TRUE;
10350 }
10351
10352 /**
10353  * Sets the given area to be decoded. This function should be called right after opj_read_header and before any tile header reading.
10354  *
10355  * @param       p_j2k                   the jpeg2000 codec.
10356  * @param       p_start_x               the left position of the rectangle to decode (in image coordinates).
10357  * @param       p_end_x                 the right position of the rectangle to decode (in image coordinates).
10358  * @param       p_start_y               the up position of the rectangle to decode (in image coordinates).
10359  * @param       p_end_y                 the bottom position of the rectangle to decode (in image coordinates).
10360  * @param       p_manager               the user event manager
10361  *
10362  * @return      true                    if the area could be set.
10363  */
10364 opj_bool j2k_set_decode_area(   opj_j2k_v2_t *p_j2k,
10365                                                                 opj_image_t* p_image,
10366                                                                 OPJ_INT32 p_start_x, OPJ_INT32 p_start_y,
10367                                                                 OPJ_INT32 p_end_x, OPJ_INT32 p_end_y,
10368                                                                 struct opj_event_mgr * p_manager )
10369 {
10370         opj_cp_v2_t * l_cp = &(p_j2k->m_cp);
10371         opj_image_t * l_image = p_j2k->m_private_image;
10372
10373         OPJ_UINT32 it_comp;
10374         OPJ_INT32 l_comp_x1, l_comp_y1;
10375         opj_image_comp_t* l_img_comp = NULL;
10376
10377         /* Check if we are read the main header */
10378         if (p_j2k->m_specific_param.m_decoder.m_state != J2K_STATE_TPHSOT) { /* FIXME J2K_DEC_STATE_TPHSOT)*/
10379                 opj_event_msg_v2(p_manager, EVT_ERROR, "Need to decode the main header before begin to decode the remaining codestream");
10380                 return OPJ_FALSE;
10381         }
10382
10383         if ( !p_start_x && !p_start_y && !p_end_x && !p_end_y){
10384                 opj_event_msg_v2(p_manager, EVT_INFO, "No decoded area parameters, set the decoded area to the whole image\n");
10385
10386                 p_j2k->m_specific_param.m_decoder.m_start_tile_x = 0;
10387                 p_j2k->m_specific_param.m_decoder.m_start_tile_y = 0;
10388                 p_j2k->m_specific_param.m_decoder.m_end_tile_x = l_cp->tw;
10389                 p_j2k->m_specific_param.m_decoder.m_end_tile_y = l_cp->th;
10390
10391                 return OPJ_TRUE;
10392         }
10393
10394         /* ----- */
10395         /* Check if the positions provided by the user are correct */
10396
10397         /* Left */
10398         assert(p_start_x >= 0 );
10399         assert(p_start_y >= 0 );
10400
10401         if ((OPJ_UINT32)p_start_x > l_image->x1 ) {
10402                 opj_event_msg_v2(p_manager, EVT_ERROR,
10403                         "Left position of the decoded area (region_x0=%d) is outside the image area (Xsiz=%d).\n",
10404                         p_start_x, l_image->x1);
10405                 return OPJ_FALSE;
10406         }
10407         else if ((OPJ_UINT32)p_start_x < l_image->x0){
10408                 opj_event_msg_v2(p_manager, EVT_WARNING,
10409                                 "Left position of the decoded area (region_x0=%d) is outside the image area (XOsiz=%d).\n",
10410                                 p_start_x, l_image->x0);
10411                 p_j2k->m_specific_param.m_decoder.m_start_tile_x = 0;
10412                 p_image->x0 = l_image->x0;
10413         }
10414         else {
10415                 p_j2k->m_specific_param.m_decoder.m_start_tile_x = (p_start_x - l_cp->tx0) / l_cp->tdx;
10416                 p_image->x0 = p_start_x;
10417         }
10418
10419         /* Up */
10420         if ((OPJ_UINT32)p_start_y > l_image->y1){
10421                 opj_event_msg_v2(p_manager, EVT_ERROR,
10422                                 "Up position of the decoded area (region_y0=%d) is outside the image area (Ysiz=%d).\n",
10423                                 p_start_y, l_image->y1);
10424                 return OPJ_FALSE;
10425         }
10426         else if ((OPJ_UINT32)p_start_y < l_image->y0){
10427                 opj_event_msg_v2(p_manager, EVT_WARNING,
10428                                 "Up position of the decoded area (region_y0=%d) is outside the image area (YOsiz=%d).\n",
10429                                 p_start_y, l_image->y0);
10430                 p_j2k->m_specific_param.m_decoder.m_start_tile_y = 0;
10431                 p_image->y0 = l_image->y0;
10432         }
10433         else {
10434                 p_j2k->m_specific_param.m_decoder.m_start_tile_y = (p_start_y - l_cp->ty0) / l_cp->tdy;
10435                 p_image->y0 = p_start_y;
10436         }
10437
10438         /* Right */
10439         assert((OPJ_UINT32)p_end_x > 0);
10440         assert((OPJ_UINT32)p_end_y > 0);
10441         if ((OPJ_UINT32)p_end_x < l_image->x0) {
10442                 opj_event_msg_v2(p_manager, EVT_ERROR,
10443                         "Right position of the decoded area (region_x1=%d) is outside the image area (XOsiz=%d).\n",
10444                         p_end_x, l_image->x0);
10445                 return OPJ_FALSE;
10446         }
10447         else if ((OPJ_UINT32)p_end_x > l_image->x1) {
10448                 opj_event_msg_v2(p_manager, EVT_WARNING,
10449                         "Right position of the decoded area (region_x1=%d) is outside the image area (Xsiz=%d).\n",
10450                         p_end_x, l_image->x1);
10451                 p_j2k->m_specific_param.m_decoder.m_end_tile_x = l_cp->tw;
10452                 p_image->x1 = l_image->x1;
10453         }
10454         else {
10455                 p_j2k->m_specific_param.m_decoder.m_end_tile_x = int_ceildiv((p_end_x - l_cp->tx0), l_cp->tdx);
10456                 p_image->x1 = p_end_x;
10457         }
10458
10459         /* Bottom */
10460         if ((OPJ_UINT32)p_end_y < l_image->y0) {
10461                 opj_event_msg_v2(p_manager, EVT_ERROR,
10462                         "Bottom position of the decoded area (region_y1=%d) is outside the image area (YOsiz=%d).\n",
10463                         p_end_y, l_image->y0);
10464                 return OPJ_FALSE;
10465         }
10466         if ((OPJ_UINT32)p_end_y > l_image->y1){
10467                 opj_event_msg_v2(p_manager, EVT_WARNING,
10468                         "Bottom position of the decoded area (region_y1=%d) is outside the image area (Ysiz=%d).\n",
10469                         p_end_y, l_image->y1);
10470                 p_j2k->m_specific_param.m_decoder.m_end_tile_y = l_cp->th;
10471                 p_image->y1 = l_image->y1;
10472         }
10473         else{
10474                 p_j2k->m_specific_param.m_decoder.m_end_tile_y = int_ceildiv((p_end_y - l_cp->ty0), l_cp->tdy);
10475                 p_image->y1 = p_end_y;
10476         }
10477         /* ----- */
10478
10479         p_j2k->m_specific_param.m_decoder.m_discard_tiles = 1;
10480
10481         l_img_comp = p_image->comps;
10482         for (it_comp=0; it_comp < p_image->numcomps; ++it_comp)
10483         {
10484                 OPJ_INT32 l_h,l_w;
10485
10486                 l_img_comp->x0 = int_ceildiv(p_image->x0, l_img_comp->dx);
10487                 l_img_comp->y0 = int_ceildiv(p_image->y0, l_img_comp->dy);
10488                 l_comp_x1 = int_ceildiv(p_image->x1, l_img_comp->dx);
10489                 l_comp_y1 = int_ceildiv(p_image->y1, l_img_comp->dy);
10490
10491                 l_w = int_ceildivpow2(l_comp_x1, l_img_comp->factor)
10492                                 - int_ceildivpow2(l_img_comp->x0, l_img_comp->factor);
10493                 if (l_w < 0){
10494                         opj_event_msg_v2(p_manager, EVT_ERROR,
10495                                 "Size x of the decoded component image is incorrect (comp[%d].w=%d).\n",
10496                                 it_comp, l_w);
10497                         return OPJ_FALSE;
10498                 }
10499                 l_img_comp->w = l_w;
10500
10501                 l_h = int_ceildivpow2(l_comp_y1, l_img_comp->factor)
10502                                 - int_ceildivpow2(l_img_comp->y0, l_img_comp->factor);
10503                 if (l_h < 0){
10504                         opj_event_msg_v2(p_manager, EVT_ERROR,
10505                                 "Size y of the decoded component image is incorrect (comp[%d].h=%d).\n",
10506                                 it_comp, l_h);
10507                         return OPJ_FALSE;
10508                 }
10509                 l_img_comp->h = l_h;
10510
10511                 l_img_comp++;
10512         }
10513
10514         opj_event_msg_v2( p_manager, EVT_INFO,"Setting decoding area to %d,%d,%d,%d\n",
10515                         p_image->x0, p_image->y0, p_image->x1, p_image->y1);
10516
10517
10518         return OPJ_TRUE;
10519 }
10520
10521
10522 /* ----------------------------------------------------------------------- */
10523 /* J2K / JPT decoder interface                                             */
10524 /* ----------------------------------------------------------------------- */
10525 /**
10526  * Creates a J2K decompression structure.
10527  *
10528  * @return a handle to a J2K decompressor if successful, NULL otherwise.
10529 */
10530 opj_j2k_v2_t* opj_j2k_create_decompress(void)
10531 {
10532         opj_j2k_v2_t *l_j2k = (opj_j2k_v2_t*) opj_malloc(sizeof(opj_j2k_v2_t));
10533         if (!l_j2k) {
10534                 return 00;
10535         }
10536         memset(l_j2k,0,sizeof(opj_j2k_v2_t));
10537
10538         l_j2k->m_is_decoder = 1;
10539         l_j2k->m_cp.m_is_decoder = 1;
10540
10541         l_j2k->m_specific_param.m_decoder.m_default_tcp = (opj_tcp_v2_t*) opj_malloc(sizeof(opj_tcp_v2_t));
10542         if (!l_j2k->m_specific_param.m_decoder.m_default_tcp) {
10543                 j2k_destroy(l_j2k);
10544                 return 00;
10545         }
10546         memset(l_j2k->m_specific_param.m_decoder.m_default_tcp,0,sizeof(opj_tcp_v2_t));
10547
10548         l_j2k->m_specific_param.m_decoder.m_header_data = (OPJ_BYTE *) opj_malloc(J2K_DEFAULT_HEADER_SIZE);
10549         if (! l_j2k->m_specific_param.m_decoder.m_header_data) {
10550                 j2k_destroy(l_j2k);
10551                 return 00;
10552         }
10553
10554         l_j2k->m_specific_param.m_decoder.m_header_data_size = J2K_DEFAULT_HEADER_SIZE;
10555
10556         l_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec = -1 ;
10557
10558         l_j2k->m_specific_param.m_decoder.m_last_sot_read_pos = 0 ;
10559
10560         /* codestream index creation */
10561         l_j2k->cstr_index = j2k_create_cstr_index();
10562
10563                         /*(opj_codestream_index_t*) opj_malloc(sizeof(opj_codestream_index_t));
10564         if (!l_j2k->cstr_index){
10565                 j2k_destroy(l_j2k);
10566                 return NULL;
10567         }
10568
10569         l_j2k->cstr_index->marker = (opj_marker_info_t*) opj_malloc(100 * sizeof(opj_marker_info_t));
10570 */
10571
10572         /* validation list creation */
10573         l_j2k->m_validation_list = opj_procedure_list_create();
10574         if (! l_j2k->m_validation_list) {
10575                 j2k_destroy(l_j2k);
10576                 return 00;
10577         }
10578
10579         /* execution list creation */
10580         l_j2k->m_procedure_list = opj_procedure_list_create();
10581         if (! l_j2k->m_procedure_list) {
10582                 j2k_destroy(l_j2k);
10583                 return 00;
10584         }
10585
10586         return l_j2k;
10587 }
10588
10589
10590 opj_codestream_index_t* j2k_create_cstr_index(void)
10591 {
10592         opj_codestream_index_t* cstr_index = (opj_codestream_index_t*)
10593                         opj_calloc(1,sizeof(opj_codestream_index_t));
10594         if (!cstr_index)
10595                 return NULL;
10596
10597         cstr_index->maxmarknum = 100;
10598         cstr_index->marknum = 0;
10599         cstr_index->marker = (opj_marker_info_t*)
10600                         opj_calloc(cstr_index->maxmarknum, sizeof(opj_marker_info_t));
10601         if (!cstr_index-> marker)
10602                 return NULL;
10603
10604         cstr_index->tile_index = NULL;
10605
10606         return cstr_index;
10607 }
10608
10609
10610 /**
10611  * Gets the size taken by writing a SPCod or SPCoc for the given tile and component.
10612  *
10613  * @param       p_tile_no               the tile index.
10614  * @param       p_comp_no               the component being outputted.
10615  * @param       p_j2k                   the J2K codec.
10616  *
10617  * @return      the number of bytes taken by the SPCod element.
10618  */
10619 OPJ_UINT32 j2k_get_SPCod_SPCoc_size (   opj_j2k_v2_t *p_j2k,
10620                                                                                 OPJ_UINT32 p_tile_no,
10621                                                                                 OPJ_UINT32 p_comp_no )
10622 {
10623         opj_cp_v2_t *l_cp = 00;
10624         opj_tcp_v2_t *l_tcp = 00;
10625         opj_tccp_t *l_tccp = 00;
10626
10627         /* preconditions */
10628         assert(p_j2k != 00);
10629
10630         l_cp = &(p_j2k->m_cp);
10631         l_tcp = &l_cp->tcps[p_tile_no];
10632         l_tccp = &l_tcp->tccps[p_comp_no];
10633
10634         /* preconditions again */
10635         assert(p_tile_no < (l_cp->tw * l_cp->th));
10636         assert(p_comp_no < p_j2k->m_private_image->numcomps);
10637
10638         if (l_tccp->csty & J2K_CCP_CSTY_PRT) {
10639                 return 5 + l_tccp->numresolutions;
10640         }
10641         else {
10642                 return 5;
10643         }
10644 }
10645
10646 /**
10647  * Writes a SPCod or SPCoc element, i.e. the coding style of a given component of a tile.
10648  *
10649  * @param       p_comp_no       the component number to output.
10650  * @param       p_stream                        the stream to write data to.
10651  * @param       p_j2k                   J2K codec.
10652  * @param       p_manager       the user event manager.
10653  *
10654 */
10655 opj_bool j2k_write_SPCod_SPCoc( opj_j2k_v2_t *p_j2k,
10656                                                                 OPJ_UINT32 p_tile_no,
10657                                                                 OPJ_UINT32 p_comp_no,
10658                                                                 OPJ_BYTE * p_data,
10659                                                                 OPJ_UINT32 * p_header_size,
10660                                                                 struct opj_event_mgr * p_manager )
10661 {
10662         OPJ_UINT32 i;
10663         opj_cp_v2_t *l_cp = 00;
10664         opj_tcp_v2_t *l_tcp = 00;
10665         opj_tccp_t *l_tccp = 00;
10666
10667         /* preconditions */
10668         assert(p_j2k != 00);
10669         assert(p_header_size != 00);
10670         assert(p_manager != 00);
10671         assert(p_data != 00);
10672
10673         l_cp = &(p_j2k->m_cp);
10674         l_tcp = &l_cp->tcps[p_tile_no];
10675         l_tccp = &l_tcp->tccps[p_comp_no];
10676
10677         /* preconditions again */
10678         assert(p_tile_no < (l_cp->tw * l_cp->th));
10679         assert(p_comp_no <(p_j2k->m_private_image->numcomps));
10680
10681         if (*p_header_size < 5) {
10682                 opj_event_msg_v2(p_manager, EVT_ERROR, "Error writing SPCod SPCoc element\n");
10683                 return OPJ_FALSE;
10684         }
10685
10686         opj_write_bytes(p_data,l_tccp->numresolutions - 1, 1);  /* SPcoc (D) */
10687         ++p_data;
10688
10689         opj_write_bytes(p_data,l_tccp->cblkw - 2, 1);                   /* SPcoc (E) */
10690         ++p_data;
10691
10692         opj_write_bytes(p_data,l_tccp->cblkh - 2, 1);                   /* SPcoc (F) */
10693         ++p_data;
10694
10695         opj_write_bytes(p_data,l_tccp->cblksty, 1);                             /* SPcoc (G) */
10696         ++p_data;
10697
10698         opj_write_bytes(p_data,l_tccp->qmfbid, 1);                              /* SPcoc (H) */
10699         ++p_data;
10700
10701         *p_header_size = *p_header_size - 5;
10702
10703         if (l_tccp->csty & J2K_CCP_CSTY_PRT) {
10704
10705                 if (*p_header_size < l_tccp->numresolutions) {
10706                         opj_event_msg_v2(p_manager, EVT_ERROR, "Error writting SPCod SPCoc element\n");
10707                         return OPJ_FALSE;
10708                 }
10709
10710                 for (i = 0; i < l_tccp->numresolutions; ++i) {
10711                         opj_write_bytes(p_data,l_tccp->prcw[i] + (l_tccp->prch[i] << 4), 1);    /* SPcoc (I_i) */
10712                         ++p_data;
10713                 }
10714
10715                 *p_header_size = *p_header_size - l_tccp->numresolutions;
10716         }
10717
10718         return OPJ_TRUE;
10719 }
10720
10721 /**
10722  * Reads a SPCod or SPCoc element, i.e. the coding style of a given component of a tile.
10723  * @param       p_header_data   the data contained in the COM box.
10724  * @param       p_j2k                   the jpeg2000 codec.
10725  * @param       p_header_size   the size of the data contained in the COM marker.
10726  * @param       p_manager               the user event manager.
10727 */
10728 opj_bool j2k_read_SPCod_SPCoc(
10729                                                     opj_j2k_v2_t *p_j2k,
10730                                                         OPJ_UINT32 compno,
10731                                                         OPJ_BYTE * p_header_data,
10732                                                         OPJ_UINT32 * p_header_size,
10733                                                         struct opj_event_mgr * p_manager
10734                                                         )
10735 {
10736         OPJ_UINT32 i, l_tmp;
10737         opj_cp_v2_t *l_cp = NULL;
10738         opj_tcp_v2_t *l_tcp = NULL;
10739         opj_tccp_t *l_tccp = NULL;
10740         OPJ_BYTE * l_current_ptr = NULL;
10741
10742         /* preconditions */
10743         assert(p_j2k != 00);
10744         assert(p_manager != 00);
10745         assert(p_header_data != 00);
10746
10747         l_cp = &(p_j2k->m_cp);
10748         l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ?
10749                                 &l_cp->tcps[p_j2k->m_current_tile_number] :
10750                                 p_j2k->m_specific_param.m_decoder.m_default_tcp;
10751
10752         /* precondition again */
10753         assert(compno < p_j2k->m_private_image->numcomps);
10754
10755         l_tccp = &l_tcp->tccps[compno];
10756         l_current_ptr = p_header_data;
10757
10758         /* make sure room is sufficient */
10759         if (*p_header_size < 5) {
10760                 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading SPCod SPCoc element\n");
10761                 return OPJ_FALSE;
10762         }
10763
10764         opj_read_bytes(l_current_ptr, &l_tccp->numresolutions ,1);              /* SPcox (D) */
10765         ++l_tccp->numresolutions;                                                                               /* tccp->numresolutions = read() + 1 */
10766         ++l_current_ptr;
10767
10768         /* If user wants to remove more resolutions than the codestream contains, return error */
10769         if (l_cp->m_specific_param.m_dec.m_reduce >= l_tccp->numresolutions) {
10770                 opj_event_msg_v2(p_manager, EVT_ERROR, "Error decoding component %d.\nThe number of resolutions to remove is higher than the number "
10771                                         "of resolutions of this component\nModify the cp_reduce parameter.\n\n", compno);
10772                 p_j2k->m_specific_param.m_decoder.m_state |= 0x8000;/* FIXME J2K_DEC_STATE_ERR;*/
10773                 return OPJ_FALSE;
10774         }
10775
10776         opj_read_bytes(l_current_ptr,&l_tccp->cblkw ,1);                /* SPcoc (E) */
10777         ++l_current_ptr;
10778         l_tccp->cblkw += 2;
10779
10780         opj_read_bytes(l_current_ptr,&l_tccp->cblkh ,1);                /* SPcoc (F) */
10781         ++l_current_ptr;
10782         l_tccp->cblkh += 2;
10783
10784         opj_read_bytes(l_current_ptr,&l_tccp->cblksty ,1);              /* SPcoc (G) */
10785         ++l_current_ptr;
10786
10787         opj_read_bytes(l_current_ptr,&l_tccp->qmfbid ,1);               /* SPcoc (H) */
10788         ++l_current_ptr;
10789
10790         *p_header_size = *p_header_size - 5;
10791
10792         /* use custom precinct size ? */
10793         if (l_tccp->csty & J2K_CCP_CSTY_PRT) {
10794                 if (*p_header_size < l_tccp->numresolutions) {
10795                         opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading SPCod SPCoc element\n");
10796                         return OPJ_FALSE;
10797                 }
10798
10799                 for     (i = 0; i < l_tccp->numresolutions; ++i) {
10800                         opj_read_bytes(l_current_ptr,&l_tmp ,1);                /* SPcoc (I_i) */
10801                         ++l_current_ptr;
10802                         l_tccp->prcw[i] = l_tmp & 0xf;
10803                         l_tccp->prch[i] = l_tmp >> 4;
10804                 }
10805
10806                 *p_header_size = *p_header_size - l_tccp->numresolutions;
10807         }
10808         else {
10809                 /* set default size for the precinct width and height */
10810                 for     (i = 0; i < l_tccp->numresolutions; ++i) {
10811                         l_tccp->prcw[i] = 15;
10812                         l_tccp->prch[i] = 15;
10813                 }
10814         }
10815
10816 #ifdef WIP_REMOVE_MSD
10817         /* INDEX >> */
10818         if (p_j2k->cstr_info && compno == 0) {
10819                 OPJ_UINT32 l_data_size = l_tccp->numresolutions * sizeof(OPJ_UINT32);
10820
10821                 p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].tccp_info[compno].cblkh = l_tccp->cblkh;
10822                 p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].tccp_info[compno].cblkw = l_tccp->cblkw;
10823                 p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].tccp_info[compno].numresolutions = l_tccp->numresolutions;
10824                 p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].tccp_info[compno].cblksty = l_tccp->cblksty;
10825                 p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].tccp_info[compno].qmfbid = l_tccp->qmfbid;
10826
10827
10828                 memcpy(p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].pdx,l_tccp->prcw, l_data_size);
10829                 memcpy(p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].pdy,l_tccp->prch, l_data_size);
10830         }
10831         /* << INDEX */
10832 #endif
10833
10834         return OPJ_TRUE;
10835 }
10836
10837 /**
10838  * Copies the tile component parameters of all the component from the first tile component.
10839  *
10840  * @param               p_j2k           the J2k codec.
10841  */
10842 void j2k_copy_tile_component_parameters( opj_j2k_v2_t *p_j2k )
10843 {
10844         /* loop */
10845         OPJ_UINT32 i;
10846         opj_cp_v2_t *l_cp = NULL;
10847         opj_tcp_v2_t *l_tcp = NULL;
10848         opj_tccp_t *l_ref_tccp = NULL, *l_copied_tccp = NULL;
10849         OPJ_UINT32 l_prc_size;
10850
10851         /* preconditions */
10852         assert(p_j2k != 00);
10853
10854         l_cp = &(p_j2k->m_cp);
10855         l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ? /* FIXME J2K_DEC_STATE_TPH*/
10856                                 &l_cp->tcps[p_j2k->m_current_tile_number] :
10857                                 p_j2k->m_specific_param.m_decoder.m_default_tcp;
10858
10859         l_ref_tccp = &l_tcp->tccps[0];
10860         l_copied_tccp = l_ref_tccp + 1;
10861         l_prc_size = l_ref_tccp->numresolutions * sizeof(OPJ_UINT32);
10862
10863         for     (i=1; i<p_j2k->m_private_image->numcomps; ++i) {
10864                 l_copied_tccp->numresolutions = l_ref_tccp->numresolutions;
10865                 l_copied_tccp->cblkw = l_ref_tccp->cblkw;
10866                 l_copied_tccp->cblkh = l_ref_tccp->cblkh;
10867                 l_copied_tccp->cblksty = l_ref_tccp->cblksty;
10868                 l_copied_tccp->qmfbid = l_ref_tccp->qmfbid;
10869                 memcpy(l_copied_tccp->prcw,l_ref_tccp->prcw,l_prc_size);
10870                 memcpy(l_copied_tccp->prch,l_ref_tccp->prch,l_prc_size);
10871                 ++l_copied_tccp;
10872         }
10873 }
10874
10875 /**
10876  * Gets the size taken by writing SQcd or SQcc element, i.e. the quantization values of a band in the QCD or QCC.
10877  *
10878  * @param       p_tile_no               the tile index.
10879  * @param       p_comp_no               the component being outputted.
10880  * @param       p_j2k                   the J2K codec.
10881  *
10882  * @return      the number of bytes taken by the SPCod element.
10883  */
10884 OPJ_UINT32 j2k_get_SQcd_SQcc_size (     opj_j2k_v2_t *p_j2k,
10885                                                                         OPJ_UINT32 p_tile_no,
10886                                                                         OPJ_UINT32 p_comp_no )
10887 {
10888         OPJ_UINT32 l_num_bands;
10889
10890         opj_cp_v2_t *l_cp = 00;
10891         opj_tcp_v2_t *l_tcp = 00;
10892         opj_tccp_t *l_tccp = 00;
10893
10894         /* preconditions */
10895         assert(p_j2k != 00);
10896
10897         l_cp = &(p_j2k->m_cp);
10898         l_tcp = &l_cp->tcps[p_tile_no];
10899         l_tccp = &l_tcp->tccps[p_comp_no];
10900
10901         /* preconditions again */
10902         assert(p_tile_no < l_cp->tw * l_cp->th);
10903         assert(p_comp_no < p_j2k->m_private_image->numcomps);
10904
10905         l_num_bands = (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) ? 1 : (l_tccp->numresolutions * 3 - 2);
10906
10907         if (l_tccp->qntsty == J2K_CCP_QNTSTY_NOQNT)  {
10908                 return 1 + l_num_bands;
10909         }
10910         else {
10911                 return 1 + 2*l_num_bands;
10912         }
10913 }
10914
10915 /**
10916  * Writes a SQcd or SQcc element, i.e. the quantization values of a band.
10917  *
10918  * @param       p_tile_no               the tile to output.
10919  * @param       p_comp_no               the component number to output.
10920  * @param       p_data                  the data buffer.
10921  * @param       p_header_size   pointer to the size of the data buffer, it is changed by the function.
10922  * @param       p_j2k                           J2K codec.
10923  * @param       p_manager               the user event manager.
10924  *
10925 */
10926 opj_bool j2k_write_SQcd_SQcc(   opj_j2k_v2_t *p_j2k,
10927                                                                 OPJ_UINT32 p_tile_no,
10928                                                                 OPJ_UINT32 p_comp_no,
10929                                                                 OPJ_BYTE * p_data,
10930                                                                 OPJ_UINT32 * p_header_size,
10931                                                                 struct opj_event_mgr * p_manager )
10932 {
10933         OPJ_UINT32 l_header_size;
10934         OPJ_UINT32 l_band_no, l_num_bands;
10935         OPJ_UINT32 l_expn,l_mant;
10936
10937         opj_cp_v2_t *l_cp = 00;
10938         opj_tcp_v2_t *l_tcp = 00;
10939         opj_tccp_t *l_tccp = 00;
10940
10941         /* preconditions */
10942         assert(p_j2k != 00);
10943         assert(p_header_size != 00);
10944         assert(p_manager != 00);
10945         assert(p_data != 00);
10946
10947         l_cp = &(p_j2k->m_cp);
10948         l_tcp = &l_cp->tcps[p_tile_no];
10949         l_tccp = &l_tcp->tccps[p_comp_no];
10950
10951         /* preconditions again */
10952         assert(p_tile_no < l_cp->tw * l_cp->th);
10953         assert(p_comp_no <p_j2k->m_private_image->numcomps);
10954
10955         l_num_bands = (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) ? 1 : (l_tccp->numresolutions * 3 - 2);
10956
10957         if (l_tccp->qntsty == J2K_CCP_QNTSTY_NOQNT)  {
10958                 l_header_size = 1 + l_num_bands;
10959
10960                 if (*p_header_size < l_header_size) {
10961                         opj_event_msg_v2(p_manager, EVT_ERROR, "Error writing SQcd SQcc element\n");
10962                         return OPJ_FALSE;
10963                 }
10964
10965                 opj_write_bytes(p_data,l_tccp->qntsty + (l_tccp->numgbits << 5), 1);    /* Sqcx */
10966                 ++p_data;
10967
10968                 for (l_band_no = 0; l_band_no < l_num_bands; ++l_band_no) {
10969                         l_expn = l_tccp->stepsizes[l_band_no].expn;
10970                         opj_write_bytes(p_data, l_expn << 3, 1);        /* SPqcx_i */
10971                         ++p_data;
10972                 }
10973         }
10974         else {
10975                 l_header_size = 1 + 2*l_num_bands;
10976
10977                 if (*p_header_size < l_header_size) {
10978                         opj_event_msg_v2(p_manager, EVT_ERROR, "Error writing SQcd SQcc element\n");
10979                         return OPJ_FALSE;
10980                 }
10981
10982                 opj_write_bytes(p_data,l_tccp->qntsty + (l_tccp->numgbits << 5), 1);    /* Sqcx */
10983                 ++p_data;
10984
10985                 for (l_band_no = 0; l_band_no < l_num_bands; ++l_band_no) {
10986                         l_expn = l_tccp->stepsizes[l_band_no].expn;
10987                         l_mant = l_tccp->stepsizes[l_band_no].mant;
10988
10989                         opj_write_bytes(p_data, (l_expn << 11) + l_mant, 2);    /* SPqcx_i */
10990                         p_data += 2;
10991                 }
10992         }
10993
10994         *p_header_size = *p_header_size - l_header_size;
10995
10996         return OPJ_TRUE;
10997 }
10998
10999 /**
11000  * Reads a SQcd or SQcc element, i.e. the quantization values of a band.
11001  *
11002  * @param       p_comp_no               the component being targeted.
11003  * @param       p_header_data   the data contained in the COM box.
11004  * @param       p_j2k                   the jpeg2000 codec.
11005  * @param       p_header_size   the size of the data contained in the COM marker.
11006  * @param       p_manager               the user event manager.
11007 */
11008 opj_bool j2k_read_SQcd_SQcc(
11009                                                         opj_j2k_v2_t *p_j2k,
11010                                                         OPJ_UINT32 p_comp_no,
11011                                                         OPJ_BYTE* p_header_data,
11012                                                         OPJ_UINT32 * p_header_size,
11013                                                         struct opj_event_mgr * p_manager
11014                                                         )
11015 {
11016         /* loop*/
11017         OPJ_UINT32 l_band_no;
11018         opj_cp_v2_t *l_cp = 00;
11019         opj_tcp_v2_t *l_tcp = 00;
11020         opj_tccp_t *l_tccp = 00;
11021         OPJ_BYTE * l_current_ptr = 00;
11022         OPJ_UINT32 l_tmp, l_num_band;
11023
11024         /* preconditions*/
11025         assert(p_j2k != 00);
11026         assert(p_manager != 00);
11027         assert(p_header_data != 00);
11028
11029         l_cp = &(p_j2k->m_cp);
11030         /* come from tile part header or main header ?*/
11031         l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ? /*FIXME J2K_DEC_STATE_TPH*/
11032                                 &l_cp->tcps[p_j2k->m_current_tile_number] :
11033                                 p_j2k->m_specific_param.m_decoder.m_default_tcp;
11034
11035         /* precondition again*/
11036         assert(p_comp_no <  p_j2k->m_private_image->numcomps);
11037
11038         l_tccp = &l_tcp->tccps[p_comp_no];
11039         l_current_ptr = p_header_data;
11040
11041         if (*p_header_size < 1) {
11042                 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading SQcd or SQcc element\n");
11043                 return OPJ_FALSE;
11044         }
11045         *p_header_size -= 1;
11046
11047         opj_read_bytes(l_current_ptr, &l_tmp ,1);                       /* Sqcx */
11048         ++l_current_ptr;
11049
11050         l_tccp->qntsty = l_tmp & 0x1f;
11051         l_tccp->numgbits = l_tmp >> 5;
11052         if (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) {
11053         l_num_band = 1;
11054         }
11055         else {
11056                 l_num_band = (l_tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) ?
11057                         (*p_header_size) :
11058                         (*p_header_size) / 2;
11059
11060                 if( l_num_band > J2K_MAXBANDS ) {
11061                         opj_event_msg_v2(p_manager, EVT_WARNING, "While reading CCP_QNTSTY element inside QCD or QCC marker segment, "
11062                                 "number of subbands (%d) is greater to J2K_MAXBANDS (%d). So we limit the number of elements stored to "
11063                                 "J2K_MAXBANDS (%d) and skip the rest. \n", l_num_band, J2K_MAXBANDS, J2K_MAXBANDS);
11064                         /*return OPJ_FALSE;*/
11065                 }
11066         }
11067
11068 #ifdef USE_JPWL
11069         if (l_cp->correct) {
11070
11071                 /* if JPWL is on, we check whether there are too many subbands */
11072                 if (/*(l_num_band < 0) ||*/ (l_num_band >= J2K_MAXBANDS)) {
11073                         opj_event_msg_v2(p_manager, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
11074                                 "JPWL: bad number of subbands in Sqcx (%d)\n",
11075                                 l_num_band);
11076                         if (!JPWL_ASSUME) {
11077                                 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
11078                                 return OPJ_FALSE;
11079                         }
11080                         /* we try to correct */
11081                         l_num_band = 1;
11082                         opj_event_msg_v2(p_manager, EVT_WARNING, "- trying to adjust them\n"
11083                                 "- setting number of bands to %d => HYPOTHESIS!!!\n",
11084                                 l_num_band);
11085                 };
11086
11087         };
11088 #endif /* USE_JPWL */
11089
11090         if (l_tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) {
11091                 for     (l_band_no = 0; l_band_no < l_num_band; l_band_no++) {
11092                         opj_read_bytes(l_current_ptr, &l_tmp ,1);                       /* SPqcx_i */
11093                         ++l_current_ptr;
11094                         if (l_band_no < J2K_MAXBANDS){
11095                                 l_tccp->stepsizes[l_band_no].expn = l_tmp>>3;
11096                                 l_tccp->stepsizes[l_band_no].mant = 0;
11097                         }
11098                 }
11099                 *p_header_size = *p_header_size - l_num_band;
11100         }
11101         else {
11102                 for     (l_band_no = 0; l_band_no < l_num_band; l_band_no++) {
11103                         opj_read_bytes(l_current_ptr, &l_tmp ,2);                       /* SPqcx_i */
11104                         l_current_ptr+=2;
11105                         if (l_band_no < J2K_MAXBANDS){
11106                                 l_tccp->stepsizes[l_band_no].expn = l_tmp >> 11;
11107                                 l_tccp->stepsizes[l_band_no].mant = l_tmp & 0x7ff;
11108                         }
11109                 }
11110                 *p_header_size = *p_header_size - 2*l_num_band;
11111         }
11112
11113         /* Add Antonin : if scalar_derived -> compute other stepsizes */
11114         if (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) {
11115                 for (l_band_no = 1; l_band_no < J2K_MAXBANDS; l_band_no++) {
11116                         l_tccp->stepsizes[l_band_no].expn =
11117                                 ((l_tccp->stepsizes[0].expn) - ((l_band_no - 1) / 3) > 0) ?
11118                                         (l_tccp->stepsizes[0].expn) - ((l_band_no - 1) / 3) : 0;
11119                         l_tccp->stepsizes[l_band_no].mant = l_tccp->stepsizes[0].mant;
11120                 }
11121         }
11122
11123         return OPJ_TRUE;
11124 }
11125
11126 /**
11127  * Copies the tile component parameters of all the component from the first tile component.
11128  *
11129  * @param               p_j2k           the J2k codec.
11130  */
11131 void j2k_copy_tile_quantization_parameters( opj_j2k_v2_t *p_j2k )
11132 {
11133         OPJ_UINT32 i;
11134         opj_cp_v2_t *l_cp = NULL;
11135         opj_tcp_v2_t *l_tcp = NULL;
11136         opj_tccp_t *l_ref_tccp = NULL;
11137         opj_tccp_t *l_copied_tccp = NULL;
11138         OPJ_UINT32 l_size;
11139
11140         /* preconditions */
11141         assert(p_j2k != 00);
11142
11143         l_cp = &(p_j2k->m_cp);
11144         l_tcp = p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH ?
11145                         &l_cp->tcps[p_j2k->m_current_tile_number] :
11146                         p_j2k->m_specific_param.m_decoder.m_default_tcp;
11147
11148         l_ref_tccp = &l_tcp->tccps[0];
11149         l_copied_tccp = l_ref_tccp + 1;
11150         l_size = J2K_MAXBANDS * sizeof(opj_stepsize_t);
11151
11152         for     (i=1;i<p_j2k->m_private_image->numcomps;++i) {
11153                 l_copied_tccp->qntsty = l_ref_tccp->qntsty;
11154                 l_copied_tccp->numgbits = l_ref_tccp->numgbits;
11155                 memcpy(l_copied_tccp->stepsizes,l_ref_tccp->stepsizes,l_size);
11156                 ++l_copied_tccp;
11157         }
11158 }
11159
11160 /**
11161  * Dump some elements from the J2K decompression structure .
11162  *
11163  *@param p_j2k                          the jpeg2000 codec.
11164  *@param flag                           flag to describe what elments are dump.
11165  *@param out_stream                     output stream where dump the elements.
11166  *
11167 */
11168 void j2k_dump (opj_j2k_v2_t* p_j2k, OPJ_INT32 flag, FILE* out_stream)
11169 {
11170         /* Check if the flag is compatible with j2k file*/
11171         if ( (flag & OPJ_JP2_INFO) || (flag & OPJ_JP2_IND)){
11172                 fprintf(out_stream, "Wrong flag\n");
11173                 return;
11174         }
11175
11176         /* Dump the image_header */
11177         if (flag & OPJ_IMG_INFO){
11178                 if (p_j2k->m_private_image)
11179                         j2k_dump_image_header(p_j2k->m_private_image, 0, out_stream);
11180         }
11181
11182         /* Dump the codestream info from main header */
11183         if (flag & OPJ_J2K_MH_INFO){
11184                 j2k_dump_MH_info(p_j2k, out_stream);
11185         }
11186
11187
11188         /* Dump the codestream info of the current tile */
11189         if (flag & OPJ_J2K_TH_INFO){
11190
11191         }
11192
11193         /* Dump the codestream index from main header */
11194         if (flag & OPJ_J2K_MH_IND){
11195                 j2k_dump_MH_index(p_j2k, out_stream);
11196         }
11197
11198         /* Dump the codestream index of the current tile */
11199         if (flag & OPJ_J2K_TH_IND){
11200
11201         }
11202
11203 }
11204
11205 /**
11206  * Dump index elements of the codestream extract from the main header.
11207  *
11208  *@param p_j2k                          the jpeg2000 codec.
11209  *@param out_stream                     output stream where dump the elements.
11210  *
11211 */
11212 void j2k_dump_MH_index(opj_j2k_v2_t* p_j2k, FILE* out_stream)
11213 {
11214         opj_codestream_index_t* cstr_index = p_j2k->cstr_index;
11215         OPJ_UINT32 it_marker, it_tile, it_tile_part;
11216
11217         fprintf(out_stream, "Codestream index from main header: {\n");
11218
11219         fprintf(out_stream, "\t Main header start position=%" PRIi64 "\n"
11220                                     "\t Main header end position=%" PRIi64 "\n",
11221                         cstr_index->main_head_start, cstr_index->main_head_end);
11222
11223         fprintf(out_stream, "\t Marker list: {\n");
11224
11225         if (cstr_index->marker){
11226                 for (it_marker=0; it_marker < cstr_index->marknum ; it_marker++){
11227                         fprintf(out_stream, "\t\t type=%#x, pos=%" PRIi64 ", len=%d\n",
11228                                         cstr_index->marker[it_marker].type,
11229                                         cstr_index->marker[it_marker].pos,
11230                                         cstr_index->marker[it_marker].len );
11231                 }
11232         }
11233
11234         fprintf(out_stream, "\t }\n");
11235
11236
11237         if (cstr_index->tile_index){
11238
11239         /* Simple test to avoid to write empty information*/
11240         OPJ_UINT32 l_acc_nb_of_tile_part = 0;
11241         for (it_tile=0; it_tile < cstr_index->nb_of_tiles ; it_tile++){
11242                         l_acc_nb_of_tile_part += cstr_index->tile_index[it_tile].nb_tps;
11243         }
11244
11245         if (l_acc_nb_of_tile_part)
11246         {
11247             fprintf(out_stream, "\t Tile index: {\n");
11248        
11249                     for (it_tile=0; it_tile < cstr_index->nb_of_tiles ; it_tile++){
11250                             OPJ_UINT32 nb_of_tile_part = cstr_index->tile_index[it_tile].nb_tps;
11251
11252                             fprintf(out_stream, "\t\t nb of tile-part in tile [%d]=%d\n", it_tile, nb_of_tile_part);
11253
11254                             if (cstr_index->tile_index[it_tile].tp_index){
11255                                     for (it_tile_part =0; it_tile_part < nb_of_tile_part; it_tile_part++){
11256                                             fprintf(out_stream, "\t\t\t tile-part[%d]: star_pos=%" PRIi64 ", end_header=%" PRIi64 ", end_pos=%" PRIi64 ".\n",
11257                                                             it_tile_part,
11258                                                             cstr_index->tile_index[it_tile].tp_index[it_tile_part].start_pos,
11259                                                             cstr_index->tile_index[it_tile].tp_index[it_tile_part].end_header,
11260                                                             cstr_index->tile_index[it_tile].tp_index[it_tile_part].end_pos);
11261                                     }
11262                             }
11263
11264                             if (cstr_index->tile_index[it_tile].marker){
11265                                     for (it_marker=0; it_marker < cstr_index->tile_index[it_tile].marknum ; it_marker++){
11266                                             fprintf(out_stream, "\t\t type=%#x, pos=%" PRIi64 ", len=%d\n",
11267                                                             cstr_index->tile_index[it_tile].marker[it_marker].type,
11268                                                             cstr_index->tile_index[it_tile].marker[it_marker].pos,
11269                                                             cstr_index->tile_index[it_tile].marker[it_marker].len );
11270                                     }
11271                             }
11272                     }
11273                     fprintf(out_stream,"\t }\n");
11274         }
11275         }
11276
11277         fprintf(out_stream,"}\n");
11278
11279 }
11280
11281 /**
11282  * Dump info elements of the codestream extract from the main header.
11283  *
11284  *@param p_j2k                          the jpeg2000 codec.
11285  *@param out_stream                     output stream where dump the elements.
11286  *
11287 */
11288 void j2k_dump_MH_info(opj_j2k_v2_t* p_j2k, FILE* out_stream)
11289 {
11290         opj_tcp_v2_t * l_default_tile=NULL;
11291
11292         fprintf(out_stream, "Codestream info from main header: {\n");
11293
11294         fprintf(out_stream, "\t tx0=%d, ty0=%d\n", p_j2k->m_cp.tx0, p_j2k->m_cp.ty0);
11295         fprintf(out_stream, "\t tdx=%d, tdy=%d\n", p_j2k->m_cp.tdx, p_j2k->m_cp.tdy);
11296         fprintf(out_stream, "\t tw=%d, th=%d\n", p_j2k->m_cp.tw, p_j2k->m_cp.th);
11297
11298         l_default_tile = p_j2k->m_specific_param.m_decoder.m_default_tcp;
11299         if (l_default_tile)
11300         {
11301                 OPJ_INT32 compno;
11302                 OPJ_INT32 numcomps = p_j2k->m_private_image->numcomps;
11303
11304                 fprintf(out_stream, "\t default tile {\n");
11305                 fprintf(out_stream, "\t\t csty=%#x\n", l_default_tile->csty);
11306                 fprintf(out_stream, "\t\t prg=%#x\n", l_default_tile->prg);
11307                 fprintf(out_stream, "\t\t numlayers=%d\n", l_default_tile->numlayers);
11308                 fprintf(out_stream, "\t\t mct=%x\n", l_default_tile->mct);
11309
11310                 for (compno = 0; compno < numcomps; compno++) {
11311                         opj_tccp_t *l_tccp = &(l_default_tile->tccps[compno]);
11312                         OPJ_UINT32 resno;
11313       OPJ_INT32 bandno, numbands;
11314
11315                         /* coding style*/
11316                         fprintf(out_stream, "\t\t comp %d {\n", compno);
11317                         fprintf(out_stream, "\t\t\t csty=%#x\n", l_tccp->csty);
11318                         fprintf(out_stream, "\t\t\t numresolutions=%d\n", l_tccp->numresolutions);
11319                         fprintf(out_stream, "\t\t\t cblkw=2^%d\n", l_tccp->cblkw);
11320                         fprintf(out_stream, "\t\t\t cblkh=2^%d\n", l_tccp->cblkh);
11321                         fprintf(out_stream, "\t\t\t cblksty=%#x\n", l_tccp->cblksty);
11322                         fprintf(out_stream, "\t\t\t qmfbid=%d\n", l_tccp->qmfbid);
11323
11324                         fprintf(out_stream, "\t\t\t preccintsize (w,h)=");
11325                         for (resno = 0; resno < l_tccp->numresolutions; resno++) {
11326                                 fprintf(out_stream, "(%d,%d) ", l_tccp->prcw[resno], l_tccp->prch[resno]);
11327                         }
11328                         fprintf(out_stream, "\n");
11329
11330                         /* quantization style*/
11331                         fprintf(out_stream, "\t\t\t qntsty=%d\n", l_tccp->qntsty);
11332                         fprintf(out_stream, "\t\t\t numgbits=%d\n", l_tccp->numgbits);
11333                         fprintf(out_stream, "\t\t\t stepsizes (m,e)=");
11334                         numbands = (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) ? 1 : l_tccp->numresolutions * 3 - 2;
11335                         for (bandno = 0; bandno < numbands; bandno++) {
11336                                 fprintf(out_stream, "(%d,%d) ", l_tccp->stepsizes[bandno].mant,
11337                                         l_tccp->stepsizes[bandno].expn);
11338                         }
11339                         fprintf(out_stream, "\n");
11340
11341                         /* RGN value*/
11342                         fprintf(out_stream, "\t\t\t roishift=%d\n", l_tccp->roishift);
11343
11344                         fprintf(out_stream, "\t\t }\n");
11345                 } /*end of component of default tile*/
11346                 fprintf(out_stream, "\t }\n"); /*end of default tile*/
11347
11348         }
11349
11350         fprintf(out_stream, "}\n");
11351
11352 }
11353
11354 /**
11355  * Dump an image header structure.
11356  *
11357  *@param img_header                     the image header to dump.
11358  *@param dev_dump_flag          flag to describe if we are in the case of this function is use outside j2k_dump function
11359  *@param out_stream                     output stream where dump the elements.
11360  */
11361 void j2k_dump_image_header(opj_image_t* img_header, opj_bool dev_dump_flag, FILE* out_stream)
11362 {
11363         char tab[2];
11364
11365         if (dev_dump_flag){
11366                 fprintf(stdout, "[DEV] Dump an image_header struct {\n");
11367                 tab[0] = '\0';
11368         }
11369         else {
11370                 fprintf(out_stream, "Image info {\n");
11371                 tab[0] = '\t';tab[1] = '\0';
11372         }
11373
11374         fprintf(out_stream, "%s x0=%d, y0=%d\n", tab, img_header->x0, img_header->y0);
11375         fprintf(out_stream,     "%s x1=%d, y1=%d\n", tab, img_header->x1, img_header->y1);
11376         fprintf(out_stream, "%s numcomps=%d\n", tab, img_header->numcomps);
11377
11378         if (img_header->comps){
11379                 OPJ_UINT32 compno;
11380                 for (compno = 0; compno < img_header->numcomps; compno++) {
11381                         fprintf(out_stream, "%s\t component %d {\n", tab, compno);
11382                         j2k_dump_image_comp_header(&(img_header->comps[compno]), dev_dump_flag, out_stream);
11383                         fprintf(out_stream,"%s}\n",tab);
11384                 }
11385         }
11386
11387         fprintf(out_stream, "}\n");
11388 }
11389
11390 /**
11391  * Dump a component image header structure.
11392  *
11393  *@param comp_header            the component image header to dump.
11394  *@param dev_dump_flag          flag to describe if we are in the case of this function is use outside j2k_dump function
11395  *@param out_stream                     output stream where dump the elements.
11396  */
11397 void j2k_dump_image_comp_header(opj_image_comp_t* comp_header, opj_bool dev_dump_flag, FILE* out_stream)
11398 {
11399         char tab[3];
11400
11401         if (dev_dump_flag){
11402                 fprintf(stdout, "[DEV] Dump an image_comp_header struct {\n");
11403                 tab[0] = '\0';
11404         }       else {
11405                 tab[0] = '\t';tab[1] = '\t';tab[2] = '\0';
11406         }
11407
11408         fprintf(out_stream, "%s dx=%d, dy=%d\n", tab, comp_header->dx, comp_header->dy);
11409         fprintf(out_stream, "%s prec=%d\n", tab, comp_header->prec);
11410         fprintf(out_stream, "%s sgnd=%d\n", tab, comp_header->sgnd);
11411
11412         if (dev_dump_flag)
11413                 fprintf(out_stream, "}\n");
11414 }
11415
11416
11417 /**
11418  * Get the codestream info from a JPEG2000 codec.
11419  *
11420  *@param        p_j2k                           the component image header to dump.
11421  *
11422  *@return       the codestream information extract from the jpg2000 codec
11423  */
11424 opj_codestream_info_v2_t* j2k_get_cstr_info(opj_j2k_v2_t* p_j2k)
11425 {
11426         OPJ_UINT16 compno;
11427         OPJ_UINT16 numcomps = p_j2k->m_private_image->numcomps;
11428         opj_tcp_v2_t *l_default_tile;
11429         opj_codestream_info_v2_t* cstr_info = (opj_codestream_info_v2_t*) opj_calloc(1,sizeof(opj_codestream_info_v2_t));
11430
11431         cstr_info->nbcomps = p_j2k->m_private_image->numcomps;
11432
11433         cstr_info->tx0 = p_j2k->m_cp.tx0;
11434         cstr_info->ty0 = p_j2k->m_cp.ty0;
11435         cstr_info->tdx = p_j2k->m_cp.tdx;
11436         cstr_info->tdy = p_j2k->m_cp.tdy;
11437         cstr_info->tw = p_j2k->m_cp.tw;
11438         cstr_info->th = p_j2k->m_cp.th;
11439
11440         cstr_info->tile_info = NULL; /* Not fill from the main header*/
11441
11442         l_default_tile = p_j2k->m_specific_param.m_decoder.m_default_tcp;
11443
11444         cstr_info->m_default_tile_info.csty = l_default_tile->csty;
11445         cstr_info->m_default_tile_info.prg = l_default_tile->prg;
11446         cstr_info->m_default_tile_info.numlayers = l_default_tile->numlayers;
11447         cstr_info->m_default_tile_info.mct = l_default_tile->mct;
11448
11449         cstr_info->m_default_tile_info.tccp_info = (opj_tccp_info_t*) opj_calloc(cstr_info->nbcomps, sizeof(opj_tccp_info_t));
11450
11451         for (compno = 0; compno < numcomps; compno++) {
11452                 opj_tccp_t *l_tccp = &(l_default_tile->tccps[compno]);
11453                 opj_tccp_info_t *l_tccp_info = &(cstr_info->m_default_tile_info.tccp_info[compno]);
11454                 OPJ_INT32 bandno, numbands;
11455
11456                 /* coding style*/
11457                 l_tccp_info->csty = l_tccp->csty;
11458                 l_tccp_info->numresolutions = l_tccp->numresolutions;
11459                 l_tccp_info->cblkw = l_tccp->cblkw;
11460                 l_tccp_info->cblkh = l_tccp->cblkh;
11461                 l_tccp_info->cblksty = l_tccp->cblksty;
11462                 l_tccp_info->qmfbid = l_tccp->qmfbid;
11463                 if (l_tccp->numresolutions < J2K_MAXRLVLS)
11464                 {
11465                         memcpy(l_tccp_info->prch, l_tccp->prch, l_tccp->numresolutions);
11466                         memcpy(l_tccp_info->prcw, l_tccp->prcw, l_tccp->numresolutions);
11467                 }
11468
11469                 /* quantization style*/
11470                 l_tccp_info->qntsty = l_tccp->qntsty;
11471                 l_tccp_info->numgbits = l_tccp->numgbits;
11472
11473                 numbands = (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) ? 1 : l_tccp->numresolutions * 3 - 2;
11474                 if (numbands < J2K_MAXBANDS) {
11475                         for (bandno = 0; bandno < numbands; bandno++) {
11476                                 l_tccp_info->stepsizes_mant[bandno] = l_tccp->stepsizes[bandno].mant;
11477                                 l_tccp_info->stepsizes_expn[bandno] = l_tccp->stepsizes[bandno].expn;
11478                         }
11479                 }
11480
11481                 /* RGN value*/
11482                 l_tccp_info->roishift = l_tccp->roishift;
11483         }
11484
11485
11486         return cstr_info;
11487 }
11488
11489 /**
11490  * Get the codestream index from a JPEG2000 codec.
11491  *
11492  *@param        p_j2k                           the component image header to dump.
11493  *
11494  *@return       the codestream index extract from the jpg2000 codec
11495  */
11496 opj_codestream_index_t* j2k_get_cstr_index(opj_j2k_v2_t* p_j2k)
11497 {
11498         opj_codestream_index_t* l_cstr_index = (opj_codestream_index_t*)
11499                         opj_calloc(1,sizeof(opj_codestream_index_t));
11500         if (!l_cstr_index)
11501                 return NULL;
11502
11503         l_cstr_index->main_head_start = p_j2k->cstr_index->main_head_start;
11504         l_cstr_index->main_head_end = p_j2k->cstr_index->main_head_end;
11505         l_cstr_index->codestream_size = p_j2k->cstr_index->codestream_size;
11506
11507         l_cstr_index->marknum = p_j2k->cstr_index->marknum;
11508         l_cstr_index->marker = (opj_marker_info_t*)opj_malloc(l_cstr_index->marknum*sizeof(opj_marker_info_t));
11509         if (!l_cstr_index->marker){
11510                 opj_free( l_cstr_index);
11511                 return NULL;
11512         }
11513
11514         if (p_j2k->cstr_index->marker)
11515                 memcpy(l_cstr_index->marker, p_j2k->cstr_index->marker, l_cstr_index->marknum * sizeof(opj_marker_info_t) );
11516         else{
11517                 opj_free(l_cstr_index->marker);
11518                 l_cstr_index->marker = NULL;
11519         }
11520
11521         l_cstr_index->nb_of_tiles = p_j2k->cstr_index->nb_of_tiles;
11522         l_cstr_index->tile_index = (opj_tile_index_t*)opj_calloc(l_cstr_index->nb_of_tiles, sizeof(opj_tile_index_t) );
11523         if (!l_cstr_index->tile_index){
11524                 opj_free( l_cstr_index->marker);
11525                 opj_free( l_cstr_index);
11526                 return NULL;
11527         }
11528
11529         if (!p_j2k->cstr_index->tile_index){
11530                 opj_free(l_cstr_index->tile_index);
11531                 l_cstr_index->tile_index = NULL;
11532         }
11533         else {
11534                 OPJ_UINT32 it_tile = 0;
11535                 for (it_tile = 0; it_tile < l_cstr_index->nb_of_tiles; it_tile++ ){
11536
11537                         /* Tile Marker*/
11538                         l_cstr_index->tile_index[it_tile].marknum = p_j2k->cstr_index->tile_index[it_tile].marknum;
11539
11540                         l_cstr_index->tile_index[it_tile].marker =
11541                                 (opj_marker_info_t*)opj_malloc(l_cstr_index->tile_index[it_tile].marknum*sizeof(opj_marker_info_t));
11542
11543                         if (!l_cstr_index->tile_index[it_tile].marker) {
11544                                 OPJ_UINT32 it_tile_free;
11545
11546                                 for (it_tile_free=0; it_tile_free < it_tile; it_tile_free++){
11547                                         opj_free(l_cstr_index->tile_index[it_tile_free].marker);
11548                                 }
11549
11550                                 opj_free( l_cstr_index->tile_index);
11551                                 opj_free( l_cstr_index->marker);
11552                                 opj_free( l_cstr_index);
11553                                 return NULL;
11554                         }
11555
11556                         if (p_j2k->cstr_index->tile_index[it_tile].marker)
11557                                 memcpy( l_cstr_index->tile_index[it_tile].marker,
11558                                                 p_j2k->cstr_index->tile_index[it_tile].marker,
11559                                                 l_cstr_index->tile_index[it_tile].marknum * sizeof(opj_marker_info_t) );
11560                         else{
11561                                 opj_free(l_cstr_index->tile_index[it_tile].marker);
11562                                 l_cstr_index->tile_index[it_tile].marker = NULL;
11563                         }
11564
11565                         /* Tile part index*/
11566                         l_cstr_index->tile_index[it_tile].nb_tps = p_j2k->cstr_index->tile_index[it_tile].nb_tps;
11567
11568                         l_cstr_index->tile_index[it_tile].tp_index =
11569                                 (opj_tp_index_t*)opj_malloc(l_cstr_index->tile_index[it_tile].nb_tps*sizeof(opj_tp_index_t));
11570
11571                         if(!l_cstr_index->tile_index[it_tile].tp_index){
11572                                 OPJ_UINT32 it_tile_free;
11573
11574                                 for (it_tile_free=0; it_tile_free < it_tile; it_tile_free++){
11575                                         opj_free(l_cstr_index->tile_index[it_tile_free].marker);
11576                                         opj_free(l_cstr_index->tile_index[it_tile_free].tp_index);
11577                                 }
11578
11579                                 opj_free( l_cstr_index->tile_index);
11580                                 opj_free( l_cstr_index->marker);
11581                                 opj_free( l_cstr_index);
11582                                 return NULL;
11583                         }
11584
11585                         if (p_j2k->cstr_index->tile_index[it_tile].tp_index){
11586                                 memcpy( l_cstr_index->tile_index[it_tile].tp_index,
11587                                                 p_j2k->cstr_index->tile_index[it_tile].tp_index,
11588                                                 l_cstr_index->tile_index[it_tile].nb_tps * sizeof(opj_tp_index_t) );
11589                         }
11590                         else{
11591                                 opj_free(l_cstr_index->tile_index[it_tile].tp_index);
11592                                 l_cstr_index->tile_index[it_tile].tp_index = NULL;
11593                         }
11594
11595                         /* Packet index (NOT USED)*/
11596                         l_cstr_index->tile_index[it_tile].nb_packet = 0;
11597                         l_cstr_index->tile_index[it_tile].packet_index = NULL;
11598
11599                 }
11600         }
11601
11602         return l_cstr_index;
11603 }
11604
11605 static opj_bool j2k_allocate_tile_element_cstr_index(opj_j2k_v2_t *p_j2k)
11606 {
11607         OPJ_UINT32 it_tile=0;
11608
11609         p_j2k->cstr_index->nb_of_tiles = p_j2k->m_cp.tw * p_j2k->m_cp.th;
11610         p_j2k->cstr_index->tile_index = (opj_tile_index_t*)opj_calloc(p_j2k->cstr_index->nb_of_tiles, sizeof(opj_tile_index_t));
11611         if (!p_j2k->cstr_index->tile_index)
11612                 return OPJ_FALSE;
11613
11614         for (it_tile=0; it_tile < p_j2k->cstr_index->nb_of_tiles; it_tile++){
11615                 p_j2k->cstr_index->tile_index[it_tile].maxmarknum = 100;
11616                 p_j2k->cstr_index->tile_index[it_tile].marknum = 0;
11617                 p_j2k->cstr_index->tile_index[it_tile].marker = (opj_marker_info_t*)
11618                                 opj_calloc(p_j2k->cstr_index->tile_index[it_tile].maxmarknum, sizeof(opj_marker_info_t));
11619                 if (!p_j2k->cstr_index->tile_index[it_tile].marker)
11620                         return OPJ_FALSE;
11621         }
11622
11623         return OPJ_TRUE;
11624 }
11625
11626 /**
11627  * Reads the tiles.
11628  */
11629 opj_bool j2k_decode_tiles (     opj_j2k_v2_t *p_j2k,
11630                                                         opj_stream_private_t *p_stream,
11631                                                         opj_event_mgr_t * p_manager)
11632 {
11633         opj_bool l_go_on = OPJ_TRUE;
11634         OPJ_UINT32 l_current_tile_no;
11635         OPJ_UINT32 l_data_size,l_max_data_size;
11636         OPJ_INT32 l_tile_x0,l_tile_y0,l_tile_x1,l_tile_y1;
11637         OPJ_UINT32 l_nb_comps;
11638         OPJ_BYTE * l_current_data;
11639
11640         l_current_data = (OPJ_BYTE*)opj_malloc(1000);
11641         if (! l_current_data) {
11642                 return OPJ_FALSE;
11643         }
11644         l_max_data_size = 1000;
11645
11646
11647
11648         while (OPJ_TRUE) {
11649                 if (! j2k_read_tile_header(     p_j2k,
11650                                                                         &l_current_tile_no,
11651                                                                         &l_data_size,
11652                                                                         &l_tile_x0, &l_tile_y0,
11653                                                                         &l_tile_x1, &l_tile_y1,
11654                                                                         &l_nb_comps,
11655                                                                         &l_go_on,
11656                                                                         p_stream,
11657                                                                         p_manager)) {
11658                         opj_free(l_current_data);
11659                         return OPJ_FALSE;
11660                 }
11661
11662                 if (! l_go_on) {
11663                         break;
11664                 }
11665
11666                 if (l_data_size > l_max_data_size) {
11667                         l_current_data = (OPJ_BYTE*)opj_realloc(l_current_data,l_data_size);
11668                         if (! l_current_data) {
11669                                 opj_free(l_current_data);
11670                                 return OPJ_FALSE;
11671                         }
11672
11673                         l_max_data_size = l_data_size;
11674                 }
11675
11676                 if (! j2k_decode_tile(p_j2k,l_current_tile_no,l_current_data,l_data_size,p_stream,p_manager)) {
11677                         opj_free(l_current_data);
11678                         return OPJ_FALSE;
11679                 }
11680                 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);
11681
11682                 if (! j2k_update_image_data(p_j2k->m_tcd,l_current_data, p_j2k->m_output_image)) {
11683                         opj_free(l_current_data);
11684                         return OPJ_FALSE;
11685                 }
11686                 opj_event_msg_v2(p_manager, EVT_INFO, "Image data has been updated with tile %d.\n\n", l_current_tile_no + 1);
11687
11688         }
11689
11690         opj_free(l_current_data);
11691
11692         return OPJ_TRUE;
11693 }
11694
11695 /**
11696  * Sets up the procedures to do on decoding data. Developpers wanting to extend the library can add their own reading procedures.
11697  */
11698 static void j2k_setup_decoding (opj_j2k_v2_t *p_j2k)
11699 {
11700         /* preconditions*/
11701         assert(p_j2k != 00);
11702
11703         opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_decode_tiles);
11704         /* DEVELOPER CORNER, add your custom procedures */
11705
11706 }
11707
11708 /*
11709  * Read and decode one tile.
11710  */
11711 static opj_bool j2k_decode_one_tile (   opj_j2k_v2_t *p_j2k,
11712                                                                 opj_stream_private_t *p_stream,
11713                                                                 opj_event_mgr_t * p_manager)
11714 {
11715         opj_bool l_go_on = OPJ_TRUE;
11716         OPJ_UINT32 l_current_tile_no;
11717         OPJ_UINT32 l_tile_no_to_dec;
11718         OPJ_UINT32 l_data_size,l_max_data_size;
11719         OPJ_INT32 l_tile_x0,l_tile_y0,l_tile_x1,l_tile_y1;
11720         OPJ_UINT32 l_nb_comps;
11721         OPJ_BYTE * l_current_data;
11722
11723         l_current_data = (OPJ_BYTE*)opj_malloc(1000);
11724         if (! l_current_data) {
11725                 return OPJ_FALSE;
11726         }
11727         l_max_data_size = 1000;
11728
11729         /*Allocate and initialize some elements of codestrem index if not already done*/
11730         if( !p_j2k->cstr_index->tile_index)
11731         {
11732                 if (!j2k_allocate_tile_element_cstr_index(p_j2k)){
11733                         opj_free(l_current_data);
11734                         return OPJ_FALSE;
11735                 }
11736         }
11737         /* Move into the codestream to the first SOT used to decode the desired tile */
11738         l_tile_no_to_dec = p_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec;
11739         if (p_j2k->cstr_index->tile_index)
11740                 if(p_j2k->cstr_index->tile_index->tp_index)
11741                 {
11742                         if ( ! p_j2k->cstr_index->tile_index[l_tile_no_to_dec].nb_tps) {
11743                                 /* the index for this tile has not been built,
11744                                  *  so move to the last SOT read */
11745                                 if ( opj_stream_read_seek(p_stream, p_j2k->m_specific_param.m_decoder.m_last_sot_read_pos+2, p_manager) ){
11746                                         opj_event_msg_v2(p_manager, EVT_ERROR, "Problem with seek function\n");
11747                                         return OPJ_FALSE;
11748                                 }
11749                         }
11750                         else{
11751                                 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)) {
11752                                         opj_event_msg_v2(p_manager, EVT_ERROR, "Problem with seek function\n");
11753                                         return OPJ_FALSE;
11754                                 }
11755                         }
11756                         /* Special case if we have previously read the EOC marker (if the previous tile getted is the last ) */
11757                         if(p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_EOC)
11758                                 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_TPHSOT;
11759                 }
11760
11761         while (OPJ_TRUE) {
11762                 if (! j2k_read_tile_header(     p_j2k,
11763                                                                         &l_current_tile_no,
11764                                                                         &l_data_size,
11765                                                                         &l_tile_x0, &l_tile_y0,
11766                                                                         &l_tile_x1, &l_tile_y1,
11767                                                                         &l_nb_comps,
11768                                                                         &l_go_on,
11769                                                                         p_stream,
11770                                                                         p_manager)) {
11771                         opj_free(l_current_data);
11772                         return OPJ_FALSE;
11773                 }
11774
11775
11776                 if (! l_go_on) {
11777                         break;
11778                 }
11779
11780                 if (l_data_size > l_max_data_size) {
11781                         l_current_data = (OPJ_BYTE*)opj_realloc(l_current_data,l_data_size);
11782                         if (! l_current_data) {
11783                                 opj_free(l_current_data);
11784                                 return OPJ_FALSE;
11785                         }
11786
11787                         l_max_data_size = l_data_size;
11788                 }
11789
11790
11791
11792                 if (! j2k_decode_tile(p_j2k,l_current_tile_no,l_current_data,l_data_size,p_stream,p_manager)) {
11793                         opj_free(l_current_data);
11794                         return OPJ_FALSE;
11795                 }
11796                 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);
11797
11798                 if (! j2k_update_image_data(p_j2k->m_tcd,l_current_data, p_j2k->m_output_image)) {
11799                         opj_free(l_current_data);
11800                         return OPJ_FALSE;
11801                 }
11802                 opj_event_msg_v2(p_manager, EVT_INFO, "Image data has been updated with tile %d.\n\n", l_current_tile_no);
11803
11804                 if(l_current_tile_no == l_tile_no_to_dec)
11805                 {
11806                         /* move into the codestream to the the first SOT (FIXME or not move?)*/
11807                         if (!(opj_stream_read_seek(p_stream, p_j2k->cstr_index->main_head_end + 2, p_manager) ) ) {
11808                                 opj_event_msg_v2(p_manager, EVT_ERROR, "Problem with seek function\n");
11809                                 return OPJ_FALSE;
11810                         }
11811                         break;
11812                 }
11813                 else {
11814                         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);
11815                 }
11816
11817         }
11818
11819         opj_free(l_current_data);
11820
11821         return OPJ_TRUE;
11822 }
11823
11824
11825 /**
11826  * Sets up the procedures to do on decoding one tile. Developpers wanting to extend the library can add their own reading procedures.
11827  */
11828 static void j2k_setup_decoding_tile (opj_j2k_v2_t *p_j2k)
11829 {
11830         /* preconditions*/
11831         assert(p_j2k != 00);
11832
11833         opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_decode_one_tile);
11834         /* DEVELOPER CORNER, add your custom procedures */
11835
11836 }
11837
11838
11839 /**
11840  * Decodes the tiles of the stream.
11841  */
11842 opj_bool j2k_decode_v2( opj_j2k_v2_t * p_j2k,
11843                                                 opj_stream_private_t * p_stream,
11844                                                 opj_image_t * p_image,
11845                                                 opj_event_mgr_t * p_manager)
11846 {
11847         OPJ_UINT32 compno;
11848
11849         if (!p_image)
11850                 return OPJ_FALSE;
11851
11852         p_j2k->m_output_image = opj_image_create0();
11853         if (! (p_j2k->m_output_image)) {
11854                 return OPJ_FALSE;
11855         }
11856         opj_copy_image_header(p_image, p_j2k->m_output_image);
11857
11858         /* customization of the decoding */
11859         j2k_setup_decoding(p_j2k);
11860
11861         /* Decode the codestream */
11862         if (! j2k_exec (p_j2k,p_j2k->m_procedure_list,p_stream,p_manager)) {
11863                 opj_image_destroy(p_j2k->m_private_image);
11864                 p_j2k->m_private_image = NULL;
11865                 return OPJ_FALSE;
11866         }
11867
11868         /* Move data and copy one information from codec to output image*/
11869         for (compno = 0; compno < p_image->numcomps; compno++) {
11870                 p_image->comps[compno].resno_decoded = p_j2k->m_output_image->comps[compno].resno_decoded;
11871                 p_image->comps[compno].data = p_j2k->m_output_image->comps[compno].data;
11872                 p_j2k->m_output_image->comps[compno].data = NULL;
11873         }
11874
11875         return OPJ_TRUE;
11876 }
11877
11878
11879 /**
11880  * Get the decoded tile.
11881  *
11882  * @param       p_j2k                   the jpeg2000 codestream codec.
11883  * @param       p_stream                input_stream
11884  * @param       p_image                 output image.   .
11885  * @param       p_manager               the user event manager
11886  * @param       tile_index              index of the tile we want decode
11887  *
11888  * @return      true                    if succeed.
11889  */
11890 opj_bool j2k_get_tile(  opj_j2k_v2_t *p_j2k,
11891                                                 opj_stream_private_t *p_stream,
11892                                                 opj_image_t* p_image,
11893                                                 struct opj_event_mgr * p_manager,
11894                                                 OPJ_UINT32 tile_index )
11895 {
11896         OPJ_UINT32 compno;
11897         OPJ_UINT32 l_tile_x, l_tile_y;
11898         opj_image_comp_t* l_img_comp;
11899
11900         if (!p_image) {
11901                 opj_event_msg_v2(p_manager, EVT_ERROR, "We need an image previously created.\n");
11902                 return OPJ_FALSE;
11903         }
11904
11905         if ( /*(tile_index < 0) &&*/ (tile_index >= p_j2k->m_cp.tw * p_j2k->m_cp.th) ){
11906                 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);
11907                 return OPJ_FALSE;
11908         }
11909
11910         /* Compute the dimension of the desired tile*/
11911         l_tile_x = tile_index % p_j2k->m_cp.tw;
11912         l_tile_y = tile_index / p_j2k->m_cp.tw;
11913
11914         p_image->x0 = l_tile_x * p_j2k->m_cp.tdx + p_j2k->m_cp.tx0;
11915         if (p_image->x0 < p_j2k->m_private_image->x0)
11916                 p_image->x0 = p_j2k->m_private_image->x0;
11917         p_image->x1 = (l_tile_x + 1) * p_j2k->m_cp.tdx + p_j2k->m_cp.tx0;
11918         if (p_image->x1 > p_j2k->m_private_image->x1)
11919                 p_image->x1 = p_j2k->m_private_image->x1;
11920
11921         p_image->y0 = l_tile_y * p_j2k->m_cp.tdy + p_j2k->m_cp.ty0;
11922         if (p_image->y0 < p_j2k->m_private_image->y0)
11923                 p_image->y0 = p_j2k->m_private_image->y0;
11924         p_image->y1 = (l_tile_y + 1) * p_j2k->m_cp.tdy + p_j2k->m_cp.ty0;
11925         if (p_image->y1 > p_j2k->m_private_image->y1)
11926                 p_image->y1 = p_j2k->m_private_image->y1;
11927
11928         l_img_comp = p_image->comps;
11929         for (compno=0; compno < p_image->numcomps; ++compno)
11930         {
11931                 OPJ_INT32 l_comp_x1, l_comp_y1;
11932
11933                 l_img_comp->factor = p_j2k->m_private_image->comps[compno].factor;
11934
11935                 l_img_comp->x0 = int_ceildiv(p_image->x0, l_img_comp->dx);
11936                 l_img_comp->y0 = int_ceildiv(p_image->y0, l_img_comp->dy);
11937                 l_comp_x1 = int_ceildiv(p_image->x1, l_img_comp->dx);
11938                 l_comp_y1 = int_ceildiv(p_image->y1, l_img_comp->dy);
11939
11940                 l_img_comp->w = int_ceildivpow2(l_comp_x1, l_img_comp->factor) - int_ceildivpow2(l_img_comp->x0, l_img_comp->factor);
11941                 l_img_comp->h = int_ceildivpow2(l_comp_y1, l_img_comp->factor) - int_ceildivpow2(l_img_comp->y0, l_img_comp->factor);
11942
11943                 l_img_comp++;
11944         }
11945
11946         /* Destroy the previous output image*/
11947         if (p_j2k->m_output_image)
11948                 opj_image_destroy(p_j2k->m_output_image);
11949
11950         /* Create the ouput image from the information previously computed*/
11951         p_j2k->m_output_image = opj_image_create0();
11952         if (! (p_j2k->m_output_image)) {
11953                 return OPJ_FALSE;
11954         }
11955         opj_copy_image_header(p_image, p_j2k->m_output_image);
11956
11957         p_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec = tile_index;
11958
11959         /* customization of the decoding */
11960         j2k_setup_decoding_tile(p_j2k);
11961
11962         /* Decode the codestream */
11963         if (! j2k_exec (p_j2k,p_j2k->m_procedure_list,p_stream,p_manager)) {
11964                 opj_image_destroy(p_j2k->m_private_image);
11965                 p_j2k->m_private_image = NULL;
11966                 return OPJ_FALSE;
11967         }
11968
11969         /* Move data and copy one information from codec to output image*/
11970         for (compno = 0; compno < p_image->numcomps; compno++) {
11971                 p_image->comps[compno].resno_decoded = p_j2k->m_output_image->comps[compno].resno_decoded;
11972
11973                 if (p_image->comps[compno].data)
11974                         opj_free(p_image->comps[compno].data);
11975
11976                 p_image->comps[compno].data = p_j2k->m_output_image->comps[compno].data;
11977
11978                 p_j2k->m_output_image->comps[compno].data = NULL;
11979         }
11980
11981         return OPJ_TRUE;
11982 }
11983
11984 opj_bool j2k_set_decoded_resolution_factor(opj_j2k_v2_t *p_j2k, OPJ_UINT32 res_factor, opj_event_mgr_t * p_manager)
11985 {
11986         OPJ_UINT32 it_comp;
11987
11988         p_j2k->m_cp.m_specific_param.m_dec.m_reduce = res_factor;
11989
11990         if (p_j2k->m_private_image) {
11991                 if (p_j2k->m_private_image->comps) {
11992                         if (p_j2k->m_specific_param.m_decoder.m_default_tcp) {
11993                                 if (p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps) {
11994                                         for (it_comp = 0 ; it_comp < p_j2k->m_private_image->numcomps; it_comp++) {
11995                                                 OPJ_UINT32 max_res = p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps[it_comp].numresolutions;
11996                                                 if ( res_factor >= max_res){
11997                                                         opj_event_msg_v2(p_manager, EVT_ERROR, "Resolution factor is greater than the maximum resolution in the component.\n");
11998                                                         return OPJ_FALSE;
11999                                                 }
12000                                                 p_j2k->m_private_image->comps[it_comp].factor = res_factor;
12001                                         }
12002                                         return OPJ_TRUE;
12003                                 }
12004                         }
12005                 }
12006         }
12007
12008         return OPJ_FALSE;
12009 }
12010
12011
12012 /**
12013  * Encodes all the tiles in a row.
12014  */
12015 opj_bool j2k_encode_v2( opj_j2k_v2_t * p_j2k,
12016                                                 opj_stream_private_t *p_stream,
12017                                                 opj_event_mgr_t * p_manager )
12018 {
12019         OPJ_UINT32 i;
12020         OPJ_UINT32 l_nb_tiles;
12021         OPJ_UINT32 l_max_tile_size, l_current_tile_size;
12022         OPJ_BYTE * l_current_data;
12023
12024         /* preconditions */
12025         assert(p_j2k != 00);
12026         assert(p_stream != 00);
12027         assert(p_manager != 00);
12028
12029         l_current_data = (OPJ_BYTE*)opj_malloc(1000);
12030         if (! l_current_data) {
12031                 return OPJ_FALSE;
12032         }
12033         l_max_tile_size = 1000;
12034
12035         l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw;
12036         for (i=0;i<l_nb_tiles;++i) {
12037                 if (! j2k_pre_write_tile(p_j2k,i,p_stream,p_manager)) {
12038                         opj_free(l_current_data);
12039                         return OPJ_FALSE;
12040                 }
12041
12042                 l_current_tile_size = tcd_get_encoded_tile_size(p_j2k->m_tcd);
12043                 if (l_current_tile_size > l_max_tile_size) {
12044                         l_current_data = (OPJ_BYTE*)opj_realloc(l_current_data,l_current_tile_size);
12045                         if (! l_current_data) {
12046                                 return OPJ_FALSE;
12047                         }
12048                         l_max_tile_size = l_current_tile_size;
12049                 }
12050
12051                 j2k_get_tile_data(p_j2k->m_tcd,l_current_data);
12052
12053                 if (! j2k_post_write_tile (p_j2k,l_current_data,l_current_tile_size,p_stream,p_manager)) {
12054                         return OPJ_FALSE;
12055                 }
12056         }
12057
12058         opj_free(l_current_data);
12059         return OPJ_TRUE;
12060 }
12061
12062 /**
12063  * Ends the compression procedures and possibility add data to be read after the
12064  * codestream.
12065  */
12066 opj_bool j2k_end_compress(      opj_j2k_v2_t *p_j2k,
12067                                                         opj_stream_private_t *p_stream,
12068                                                         struct opj_event_mgr * p_manager)
12069 {
12070         /* customization of the encoding */
12071         j2k_setup_end_compress(p_j2k);
12072
12073         if (! j2k_exec (p_j2k, p_j2k->m_procedure_list, p_stream, p_manager))
12074         {
12075                 return OPJ_FALSE;
12076         }
12077
12078         return OPJ_TRUE;
12079 }
12080
12081
12082 /**
12083  * Starts a compression scheme, i.e. validates the codec parameters, writes the header.
12084  *
12085  * @param       p_j2k           the jpeg2000 codec.
12086  * @param       p_stream        the stream object.
12087  * @param       p_manager       the user event manager.
12088  *
12089  * @return true if the codec is valid.
12090  */
12091 opj_bool j2k_start_compress(opj_j2k_v2_t *p_j2k,
12092                                                         opj_stream_private_t *p_stream,
12093                                                         opj_image_t * p_image,
12094                                                         opj_event_mgr_t * p_manager)
12095 {
12096         /* preconditions */
12097         assert(p_j2k != 00);
12098         assert(p_stream != 00);
12099         assert(p_manager != 00);
12100
12101         p_j2k->m_private_image = opj_image_create0();
12102         opj_copy_image_header(p_image, p_j2k->m_private_image);
12103
12104         // TODO_MSD: Find a better way
12105         if (p_image->comps) {
12106                 OPJ_UINT32 it_comp;
12107                 for (it_comp = 0 ; it_comp < p_image->numcomps; it_comp++) {
12108                         if (p_image->comps[it_comp].data) {
12109                                 p_j2k->m_private_image->comps[it_comp].data =p_image->comps[it_comp].data;
12110                                 p_image->comps[it_comp].data = NULL;
12111
12112                         }
12113                 }
12114         }
12115
12116         /* customization of the validation */
12117         j2k_setup_encoding_validation (p_j2k);
12118
12119         /* validation of the parameters codec */
12120         if (! j2k_exec(p_j2k,p_j2k->m_validation_list,p_stream,p_manager)) {
12121                 return OPJ_FALSE;
12122         }
12123
12124         /* customization of the encoding */
12125         j2k_setup_header_writting(p_j2k);
12126
12127         /* write header */
12128         if (! j2k_exec (p_j2k,p_j2k->m_procedure_list,p_stream,p_manager)) {
12129                 return OPJ_FALSE;
12130         }
12131
12132         return OPJ_TRUE;
12133 }
12134
12135 /*
12136  *
12137  */
12138 opj_bool j2k_pre_write_tile (   opj_j2k_v2_t * p_j2k,
12139                                                                 OPJ_UINT32 p_tile_index,
12140                                                                 opj_stream_private_t *p_stream,
12141                                                                 opj_event_mgr_t * p_manager )
12142 {
12143   (void)p_stream;
12144         if (p_tile_index != p_j2k->m_current_tile_number) {
12145                 opj_event_msg_v2(p_manager, EVT_ERROR, "The given tile index does not match." );
12146                 return OPJ_FALSE;
12147         }
12148
12149         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);
12150
12151         p_j2k->m_specific_param.m_encoder.m_current_tile_part_number = 0;
12152         p_j2k->m_tcd->cur_totnum_tp = p_j2k->m_cp.tcps[p_tile_index].m_nb_tile_parts;
12153         p_j2k->m_specific_param.m_encoder.m_current_poc_tile_part_number = 0;
12154
12155         /* initialisation before tile encoding  */
12156         if (! tcd_init_encode_tile(p_j2k->m_tcd, p_j2k->m_current_tile_number)) {
12157                 return OPJ_FALSE;
12158         }
12159
12160         return OPJ_TRUE;
12161 }
12162
12163 /*
12164  *
12165  */
12166 void j2k_get_tile_data (opj_tcd_v2_t * p_tcd, OPJ_BYTE * p_data)
12167 {
12168         OPJ_UINT32 i,j,k = 0;
12169         OPJ_UINT32 l_width,l_height,l_stride, l_offset_x,l_offset_y, l_image_width;
12170         opj_image_comp_t * l_img_comp = 00;
12171         opj_tcd_tilecomp_v2_t * l_tilec = 00;
12172         opj_image_t * l_image = 00;
12173         OPJ_UINT32 l_size_comp, l_remaining;
12174         OPJ_INT32 * l_src_ptr;
12175         l_tilec = p_tcd->tcd_image->tiles->comps;
12176         l_image = p_tcd->image;
12177         l_img_comp = l_image->comps;
12178
12179         for (i=0;i<p_tcd->image->numcomps;++i) {
12180                 l_size_comp = l_img_comp->prec >> 3; /* (/8) */
12181                 l_remaining = l_img_comp->prec & 7;  /* (%8) */
12182                 if (l_remaining) {
12183                         ++l_size_comp;
12184                 }
12185
12186                 if (l_size_comp == 3) {
12187                         l_size_comp = 4;
12188                 }
12189
12190                 l_width = (l_tilec->x1 - l_tilec->x0);
12191                 l_height = (l_tilec->y1 - l_tilec->y0);
12192                 l_offset_x = int_ceildiv(l_image->x0, l_img_comp->dx);
12193                 l_offset_y = int_ceildiv(l_image->y0, l_img_comp->dy);
12194                 l_image_width = int_ceildiv(l_image->x1 - l_image->x0, l_img_comp->dx);
12195                 l_stride = l_image_width - l_width;
12196                 l_src_ptr = l_img_comp->data + (l_tilec->x0 - l_offset_x) + (l_tilec->y0 - l_offset_y) * l_image_width;
12197
12198                 switch (l_size_comp) {
12199                         case 1:
12200                                 {
12201                                         OPJ_CHAR * l_dest_ptr = (OPJ_CHAR*) p_data;
12202                                         if (l_img_comp->sgnd) {
12203                                                 for     (j=0;j<l_height;++j) {
12204                                                         for (k=0;k<l_width;++k) {
12205                                                                 *(l_dest_ptr) = (OPJ_CHAR) (*l_src_ptr);
12206                                                                 ++l_dest_ptr;
12207                                                                 ++l_src_ptr;
12208                                                         }
12209                                                         l_src_ptr += l_stride;
12210                                                 }
12211                                         }
12212                                         else {
12213                                                 for (j=0;j<l_height;++j) {
12214                                                         for (k=0;k<l_width;++k) {
12215                                                                 *(l_dest_ptr) = (*l_src_ptr)&0xff;
12216                                                                 ++l_dest_ptr;
12217                                                                 ++l_src_ptr;
12218                                                         }
12219                                                         l_src_ptr += l_stride;
12220                                                 }
12221                                         }
12222
12223                                         p_data = (OPJ_BYTE*) l_dest_ptr;
12224                                 }
12225                                 break;
12226                         case 2:
12227                                 {
12228                                         OPJ_INT16 * l_dest_ptr = (OPJ_INT16 *) p_data;
12229                                         if (l_img_comp->sgnd) {
12230                                                 for (j=0;j<l_height;++j) {
12231                                                         for (k=0;k<l_width;++k) {
12232                                                                 *(l_dest_ptr++) = (OPJ_INT16) (*(l_src_ptr++));
12233                                                         }
12234                                                         l_src_ptr += l_stride;
12235                                                 }
12236                                         }
12237                                         else {
12238                                                 for (j=0;j<l_height;++j) {
12239                                                         for (k=0;k<l_width;++k) {
12240                                                                 *(l_dest_ptr++) = (*(l_src_ptr++))&0xffff;
12241                                                         }
12242                                                         l_src_ptr += l_stride;
12243                                                 }
12244                                         }
12245
12246                                         p_data = (OPJ_BYTE*) l_dest_ptr;
12247                                 }
12248                                 break;
12249                         case 4:
12250                                 {
12251                                         OPJ_INT32 * l_dest_ptr = (OPJ_INT32 *) p_data;
12252                                         for (j=0;j<l_height;++j) {
12253                                                 for (k=0;k<l_width;++k) {
12254                                                         *(l_dest_ptr++) = *(l_src_ptr++);
12255                                                 }
12256                                                 l_src_ptr += l_stride;
12257                                         }
12258
12259                                         p_data = (OPJ_BYTE*) l_dest_ptr;
12260                                 }
12261                                 break;
12262                 }
12263
12264                 ++l_img_comp;
12265                 ++l_tilec;
12266         }
12267 }
12268
12269
12270 /**
12271  * Write a tile.
12272  * @param       p_j2k           the jpeg2000 codec.
12273  * @param       p_stream        the stream to write data to.
12274  * @param       p_manager       the user event manager.
12275  */
12276 opj_bool j2k_post_write_tile (  opj_j2k_v2_t * p_j2k,
12277                                                                 OPJ_BYTE * p_data,
12278                                                                 OPJ_UINT32 p_data_size,
12279                                                                 opj_stream_private_t *p_stream,
12280                                                                 opj_event_mgr_t * p_manager )
12281 {
12282         opj_tcd_v2_t * l_tcd = 00;
12283         opj_cp_v2_t * l_cp = 00;
12284         opj_tcp_v2_t * l_tcp = 00;
12285         OPJ_UINT32 l_nb_bytes_written;
12286         OPJ_BYTE * l_current_data = 00;
12287         OPJ_UINT32 l_tile_size = 0;
12288         OPJ_UINT32 l_available_data;
12289
12290         /* preconditions */
12291         assert(p_j2k->m_specific_param.m_encoder.m_encoded_tile_data);
12292
12293         l_tcd = p_j2k->m_tcd;
12294         l_cp = &(p_j2k->m_cp);
12295         l_tcp = l_cp->tcps + p_j2k->m_current_tile_number;
12296
12297         l_tile_size = p_j2k->m_specific_param.m_encoder.m_encoded_tile_size;
12298         l_available_data = l_tile_size;
12299         l_current_data = p_j2k->m_specific_param.m_encoder.m_encoded_tile_data;
12300
12301         if (! tcd_copy_tile_data(l_tcd,p_data,p_data_size)) {
12302                 opj_event_msg_v2(p_manager, EVT_ERROR, "Size mismatch between tile data and sent data." );
12303                 return OPJ_FALSE;
12304         }
12305
12306         l_nb_bytes_written = 0;
12307         if (! j2k_write_first_tile_part(p_j2k,l_current_data,&l_nb_bytes_written,l_available_data,p_stream,p_manager)) {
12308                 return OPJ_FALSE;
12309         }
12310         l_current_data += l_nb_bytes_written;
12311         l_available_data -= l_nb_bytes_written;
12312
12313         l_nb_bytes_written = 0;
12314         if (! j2k_write_all_tile_parts(p_j2k,l_current_data,&l_nb_bytes_written,l_available_data,p_stream,p_manager)) {
12315                 return OPJ_FALSE;
12316         }
12317
12318         l_available_data -= l_nb_bytes_written;
12319         l_nb_bytes_written = l_tile_size - l_available_data;
12320
12321         if ( opj_stream_write_data(     p_stream,
12322                                                                 p_j2k->m_specific_param.m_encoder.m_encoded_tile_data,
12323                                                                 l_nb_bytes_written,p_manager) != l_nb_bytes_written) {
12324                 return OPJ_FALSE;
12325         }
12326
12327         ++p_j2k->m_current_tile_number;
12328
12329         return OPJ_TRUE;
12330 }
12331
12332
12333 /**
12334  * Sets up the validation ,i.e. adds the procedures to lauch to make sure the codec parameters
12335  * are valid. Developers wanting to extend the library can add their own validation procedures.
12336  */
12337 void j2k_setup_end_compress (opj_j2k_v2_t *p_j2k)
12338 {
12339         /* preconditions */
12340         assert(p_j2k != 00);
12341
12342         /* DEVELOPER CORNER, insert your custom procedures */
12343         opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_eoc_v2 );
12344
12345         if (p_j2k->m_cp.m_specific_param.m_enc.m_cinema) {
12346                 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_updated_tlm);
12347         }
12348
12349         opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_epc );
12350         opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_end_encoding );
12351         opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_destroy_header_memory);
12352 }
12353
12354 /**
12355  * Sets up the validation ,i.e. adds the procedures to lauch to make sure the codec parameters
12356  * are valid. Developers wanting to extend the library can add their own validation procedures.
12357  */
12358 void j2k_setup_encoding_validation (opj_j2k_v2_t *p_j2k)
12359 {
12360         /* preconditions */
12361         assert(p_j2k != 00);
12362
12363         opj_procedure_list_add_procedure(p_j2k->m_validation_list, (opj_procedure)j2k_build_encoder);
12364         opj_procedure_list_add_procedure(p_j2k->m_validation_list, (opj_procedure)j2k_encoding_validation);
12365
12366         /* DEVELOPER CORNER, add your custom validation procedure */
12367         opj_procedure_list_add_procedure(p_j2k->m_validation_list, (opj_procedure)j2k_mct_validation);
12368 }
12369
12370
12371 /**
12372  * Sets up the procedures to do on writing header.
12373  * Developers wanting to extend the library can add their own writing procedures.
12374  */
12375 void j2k_setup_header_writting (opj_j2k_v2_t *p_j2k)
12376 {
12377         /* preconditions */
12378         assert(p_j2k != 00);
12379
12380         opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_init_info );
12381         opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_soc_v2 );
12382         opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_siz_v2 );
12383         opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_cod_v2 );
12384         opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_qcd_v2 );
12385
12386
12387         if (p_j2k->m_cp.m_specific_param.m_enc.m_cinema) {
12388                 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_image_components );
12389                 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_tlm_v2 );
12390
12391                 if (p_j2k->m_cp.m_specific_param.m_enc.m_cinema == CINEMA4K_24) {
12392                         opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_poc_v2 );
12393                 }
12394         }
12395
12396         opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_regions);
12397
12398         if (p_j2k->m_cp.comment != 00)  {
12399                 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_com_v2);
12400         }
12401
12402         /* DEVELOPER CORNER, insert your custom procedures */
12403         if (p_j2k->m_cp.rsiz & MCT) {
12404                 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_mct_data_group );
12405         }
12406         /* End of Developer Corner */
12407
12408         if (p_j2k->cstr_index) {
12409                 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_get_end_header );
12410         }
12411
12412         opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_create_tcd);
12413         opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_update_rates);
12414 }
12415
12416
12417 opj_bool j2k_write_first_tile_part (opj_j2k_v2_t *p_j2k,
12418                                                                         OPJ_BYTE * p_data,
12419                                                                         OPJ_UINT32 * p_data_written,
12420                                                                         OPJ_UINT32 p_total_data_size,
12421                                                                         opj_stream_private_t *p_stream,
12422                                                                         struct opj_event_mgr * p_manager )
12423 {
12424         OPJ_UINT32 compno;
12425         OPJ_UINT32 l_nb_bytes_written = 0;
12426         OPJ_UINT32 l_current_nb_bytes_written;
12427         OPJ_BYTE * l_begin_data = 00;
12428
12429         opj_tcp_v2_t *l_tcp = 00;
12430         opj_tcd_v2_t * l_tcd = 00;
12431         opj_cp_v2_t * l_cp = 00;
12432
12433         l_tcd = p_j2k->m_tcd;
12434         l_cp = &(p_j2k->m_cp);
12435         l_tcp = l_cp->tcps + p_j2k->m_current_tile_number;
12436
12437         l_tcd->cur_pino = 0;
12438
12439         /*Get number of tile parts*/
12440         p_j2k->m_specific_param.m_encoder.m_current_poc_tile_part_number = 0;
12441
12442         /* INDEX >> */
12443         /* << INDEX */
12444
12445         l_current_nb_bytes_written = 0;
12446         l_begin_data = p_data;
12447         if (! j2k_write_sot_v2(p_j2k,p_data,&l_current_nb_bytes_written,p_stream,p_manager))
12448         {
12449                 return OPJ_FALSE;
12450         }
12451
12452         l_nb_bytes_written += l_current_nb_bytes_written;
12453         p_data += l_current_nb_bytes_written;
12454         p_total_data_size -= l_current_nb_bytes_written;
12455
12456         if (l_cp->m_specific_param.m_enc.m_cinema == 0) {
12457                 for (compno = 1; compno < p_j2k->m_private_image->numcomps; compno++) {
12458                         l_current_nb_bytes_written = 0;
12459                         j2k_write_coc_in_memory(p_j2k,compno,p_data,&l_current_nb_bytes_written,p_manager);
12460                         l_nb_bytes_written += l_current_nb_bytes_written;
12461                         p_data += l_current_nb_bytes_written;
12462                         p_total_data_size -= l_current_nb_bytes_written;
12463
12464                         l_current_nb_bytes_written = 0;
12465                         j2k_write_qcc_in_memory(p_j2k,compno,p_data,&l_current_nb_bytes_written,p_manager);
12466                         l_nb_bytes_written += l_current_nb_bytes_written;
12467                         p_data += l_current_nb_bytes_written;
12468                         p_total_data_size -= l_current_nb_bytes_written;
12469                 }
12470
12471                 if (l_cp->tcps[p_j2k->m_current_tile_number].numpocs) {
12472                         l_current_nb_bytes_written = 0;
12473                         j2k_write_poc_in_memory(p_j2k,p_data,&l_current_nb_bytes_written,p_manager);
12474                         l_nb_bytes_written += l_current_nb_bytes_written;
12475                         p_data += l_current_nb_bytes_written;
12476                         p_total_data_size -= l_current_nb_bytes_written;
12477                 }
12478         }
12479
12480         l_current_nb_bytes_written = 0;
12481         if (! j2k_write_sod_v2(p_j2k,l_tcd,p_data,&l_current_nb_bytes_written,p_total_data_size,p_stream,p_manager)) {
12482                 return OPJ_FALSE;
12483         }
12484
12485         l_nb_bytes_written += l_current_nb_bytes_written;
12486         * p_data_written = l_nb_bytes_written;
12487
12488         /* Writing Psot in SOT marker */
12489         opj_write_bytes(l_begin_data + 6,l_nb_bytes_written,4);                                 /* PSOT */
12490
12491         if (l_cp->m_specific_param.m_enc.m_cinema){
12492                 j2k_update_tlm(p_j2k,l_nb_bytes_written);
12493         }
12494
12495         return OPJ_TRUE;
12496 }
12497
12498 opj_bool j2k_write_all_tile_parts(      opj_j2k_v2_t *p_j2k,
12499                                                                         OPJ_BYTE * p_data,
12500                                                                         OPJ_UINT32 * p_data_written,
12501                                                                         OPJ_UINT32 p_total_data_size,
12502                                                                         opj_stream_private_t *p_stream,
12503                                                                         struct opj_event_mgr * p_manager
12504                                                                 )
12505 {
12506         OPJ_UINT32 tilepartno=0;
12507         OPJ_UINT32 l_nb_bytes_written = 0;
12508         OPJ_UINT32 l_current_nb_bytes_written;
12509         OPJ_UINT32 l_part_tile_size;
12510         OPJ_UINT32 tot_num_tp;
12511         OPJ_UINT32 pino;
12512
12513         OPJ_BYTE * l_begin_data;
12514         opj_tcp_v2_t *l_tcp = 00;
12515         opj_tcd_v2_t * l_tcd = 00;
12516         opj_cp_v2_t * l_cp = 00;
12517
12518
12519         l_tcd = p_j2k->m_tcd;
12520         l_cp = &(p_j2k->m_cp);
12521         l_tcp = l_cp->tcps + p_j2k->m_current_tile_number;
12522
12523         /*Get number of tile parts*/
12524         tot_num_tp = j2k_get_num_tp_v2(l_cp,0,p_j2k->m_current_tile_number);
12525
12526         for (tilepartno = 1; tilepartno < tot_num_tp ; ++tilepartno) {
12527                 p_j2k->m_specific_param.m_encoder.m_current_poc_tile_part_number = tilepartno;
12528                 l_current_nb_bytes_written = 0;
12529                 l_part_tile_size = 0;
12530                 l_begin_data = p_data;
12531
12532                 if (! j2k_write_sot_v2(p_j2k,p_data,&l_current_nb_bytes_written,p_stream,p_manager)) {
12533                         return OPJ_FALSE;
12534                 }
12535
12536                 l_nb_bytes_written += l_current_nb_bytes_written;
12537                 p_data += l_current_nb_bytes_written;
12538                 p_total_data_size -= l_current_nb_bytes_written;
12539                 l_part_tile_size += l_nb_bytes_written;
12540
12541                 l_current_nb_bytes_written = 0;
12542                 if (! j2k_write_sod_v2(p_j2k,l_tcd,p_data,&l_current_nb_bytes_written,p_total_data_size,p_stream,p_manager)) {
12543                         return OPJ_FALSE;
12544                 }
12545
12546                 p_data += l_current_nb_bytes_written;
12547                 l_nb_bytes_written += l_current_nb_bytes_written;
12548                 p_total_data_size -= l_current_nb_bytes_written;
12549                 l_part_tile_size += l_nb_bytes_written;
12550
12551                 /* Writing Psot in SOT marker */
12552                 opj_write_bytes(l_begin_data + 6,l_part_tile_size,4);                                   /* PSOT */
12553
12554                 if (l_cp->m_specific_param.m_enc.m_cinema) {
12555                         j2k_update_tlm(p_j2k,l_part_tile_size);
12556                 }
12557
12558                 ++p_j2k->m_specific_param.m_encoder.m_current_tile_part_number;
12559         }
12560
12561         for (pino = 1; pino <= l_tcp->numpocs; ++pino) {
12562                 l_tcd->cur_pino = pino;
12563
12564                 /*Get number of tile parts*/
12565                 tot_num_tp = j2k_get_num_tp_v2(l_cp,pino,p_j2k->m_current_tile_number);
12566                 for (tilepartno = 0; tilepartno < tot_num_tp ; ++tilepartno) {
12567                         p_j2k->m_specific_param.m_encoder.m_current_poc_tile_part_number = tilepartno;
12568                         l_current_nb_bytes_written = 0;
12569                         l_part_tile_size = 0;
12570                         l_begin_data = p_data;
12571
12572                         if (! j2k_write_sot_v2(p_j2k,p_data,&l_current_nb_bytes_written,p_stream,p_manager)) {
12573                                 return OPJ_FALSE;
12574                         }
12575
12576                         l_nb_bytes_written += l_current_nb_bytes_written;
12577                         p_data += l_current_nb_bytes_written;
12578                         p_total_data_size -= l_current_nb_bytes_written;
12579                         l_part_tile_size += l_current_nb_bytes_written;
12580
12581                         l_current_nb_bytes_written = 0;
12582
12583                         if (! j2k_write_sod_v2(p_j2k,l_tcd,p_data,&l_current_nb_bytes_written,p_total_data_size,p_stream,p_manager)) {
12584                                 return OPJ_FALSE;
12585                         }
12586
12587                         l_nb_bytes_written += l_current_nb_bytes_written;
12588                         p_data += l_current_nb_bytes_written;
12589                         p_total_data_size -= l_current_nb_bytes_written;
12590                         l_part_tile_size += l_current_nb_bytes_written;
12591
12592                         /* Writing Psot in SOT marker */
12593                         opj_write_bytes(l_begin_data + 6,l_part_tile_size,4);                                   /* PSOT */
12594
12595                         if (l_cp->m_specific_param.m_enc.m_cinema) {
12596                                 j2k_update_tlm(p_j2k,l_part_tile_size);
12597                         }
12598
12599                         ++p_j2k->m_specific_param.m_encoder.m_current_tile_part_number;
12600                 }
12601         }
12602
12603         *p_data_written = l_nb_bytes_written;
12604
12605         return OPJ_TRUE;
12606 }
12607
12608 /**
12609  * Writes the updated tlm.
12610  *
12611  * @param       p_stream                the stream to write data to.
12612  * @param       p_j2k                   J2K codec.
12613  * @param       p_manager               the user event manager.
12614 */
12615 opj_bool j2k_write_updated_tlm( opj_j2k_v2_t *p_j2k,
12616                                                                 struct opj_stream_private *p_stream,
12617                                                                 struct opj_event_mgr * p_manager )
12618 {
12619         OPJ_UINT32 l_tlm_size;
12620         OPJ_SIZE_T l_tlm_position, l_current_position;
12621
12622         /* preconditions */
12623         assert(p_j2k != 00);
12624         assert(p_manager != 00);
12625         assert(p_stream != 00);
12626
12627         l_tlm_size = 5 * p_j2k->m_specific_param.m_encoder.m_total_tile_parts;
12628         l_tlm_position = 6 + p_j2k->m_specific_param.m_encoder.m_tlm_start;
12629         l_current_position = opj_stream_tell(p_stream);
12630
12631         if (! opj_stream_seek(p_stream,l_tlm_position,p_manager)) {
12632                 return OPJ_FALSE;
12633         }
12634
12635         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) {
12636                 return OPJ_FALSE;
12637         }
12638
12639         if (! opj_stream_seek(p_stream,l_current_position,p_manager)) {
12640                 return OPJ_FALSE;
12641         }
12642
12643         return OPJ_TRUE;
12644 }
12645
12646
12647 /**
12648  * Ends the encoding, i.e. frees memory.
12649  *
12650  * @param       p_stream                                the stream to write data to.
12651  * @param       p_j2k                           J2K codec.
12652  * @param       p_manager               the user event manager.
12653 */
12654 opj_bool j2k_end_encoding(      opj_j2k_v2_t *p_j2k,
12655                                                         struct opj_stream_private *p_stream,
12656                                                         struct opj_event_mgr * p_manager )
12657 {
12658         /* preconditions */
12659         assert(p_j2k != 00);
12660         assert(p_manager != 00);
12661         assert(p_stream != 00);
12662
12663         tcd_destroy_v2(p_j2k->m_tcd);
12664         p_j2k->m_tcd = 00;
12665
12666         if (p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer) {
12667                 opj_free(p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer);
12668                 p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer = 0;
12669                 p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current = 0;
12670         }
12671
12672         if (p_j2k->m_specific_param.m_encoder.m_encoded_tile_data) {
12673                 opj_free(p_j2k->m_specific_param.m_encoder.m_encoded_tile_data);
12674                 p_j2k->m_specific_param.m_encoder.m_encoded_tile_data = 0;
12675         }
12676
12677         p_j2k->m_specific_param.m_encoder.m_encoded_tile_size = 0;
12678
12679         return OPJ_TRUE;
12680 }
12681
12682 /**
12683  * Destroys the memory associated with the decoding of headers.
12684  */
12685 opj_bool j2k_destroy_header_memory (opj_j2k_v2_t * p_j2k,
12686                                                                         opj_stream_private_t *p_stream,
12687                                                                         opj_event_mgr_t * p_manager )
12688 {
12689         /* preconditions */
12690         assert(p_j2k != 00);
12691         assert(p_stream != 00);
12692         assert(p_manager != 00);
12693
12694         if (p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
12695                 opj_free(p_j2k->m_specific_param.m_encoder.m_header_tile_data);
12696                 p_j2k->m_specific_param.m_encoder.m_header_tile_data = 0;
12697         }
12698
12699         p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = 0;
12700
12701         return OPJ_TRUE;
12702 }
12703
12704
12705 /**
12706  * Inits the Info
12707  *
12708  * @param       p_stream                                the stream to write data to.
12709  * @param       p_j2k                           J2K codec.
12710  * @param       p_manager               the user event manager.
12711 */
12712 opj_bool j2k_init_info( opj_j2k_v2_t *p_j2k,
12713                                                 struct opj_stream_private *p_stream,
12714                                                 struct opj_event_mgr * p_manager )
12715 {
12716         opj_codestream_info_t * l_cstr_info = 00;
12717
12718         /* preconditions */
12719         assert(p_j2k != 00);
12720         assert(p_manager != 00);
12721         assert(p_stream != 00);
12722   (void)l_cstr_info;
12723
12724         /* TODO mergeV2: check this part which use cstr_info */
12725         /*l_cstr_info = p_j2k->cstr_info;
12726
12727         if (l_cstr_info)  {
12728                 OPJ_UINT32 compno;
12729                 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));
12730
12731                 l_cstr_info->image_w = p_j2k->m_image->x1 - p_j2k->m_image->x0;
12732                 l_cstr_info->image_h = p_j2k->m_image->y1 - p_j2k->m_image->y0;
12733
12734                 l_cstr_info->prog = (&p_j2k->m_cp.tcps[0])->prg;
12735
12736                 l_cstr_info->tw = p_j2k->m_cp.tw;
12737                 l_cstr_info->th = p_j2k->m_cp.th;
12738
12739                 l_cstr_info->tile_x = p_j2k->m_cp.tdx;*/        /* new version parser */
12740                 /*l_cstr_info->tile_y = p_j2k->m_cp.tdy;*/      /* new version parser */
12741                 /*l_cstr_info->tile_Ox = p_j2k->m_cp.tx0;*/     /* new version parser */
12742                 /*l_cstr_info->tile_Oy = p_j2k->m_cp.ty0;*/     /* new version parser */
12743
12744                 /*l_cstr_info->numcomps = p_j2k->m_image->numcomps;
12745
12746                 l_cstr_info->numlayers = (&p_j2k->m_cp.tcps[0])->numlayers;
12747
12748                 l_cstr_info->numdecompos = (OPJ_INT32*) opj_malloc(p_j2k->m_image->numcomps * sizeof(OPJ_INT32));
12749
12750                 for (compno=0; compno < p_j2k->m_image->numcomps; compno++) {
12751                         l_cstr_info->numdecompos[compno] = (&p_j2k->m_cp.tcps[0])->tccps->numresolutions - 1;
12752                 }
12753
12754                 l_cstr_info->D_max = 0.0;       */      /* ADD Marcela */
12755
12756                 /*l_cstr_info->main_head_start = opj_stream_tell(p_stream);*/ /* position of SOC */
12757
12758                 /*l_cstr_info->maxmarknum = 100;
12759                 l_cstr_info->marker = (opj_marker_info_t *) opj_malloc(l_cstr_info->maxmarknum * sizeof(opj_marker_info_t));
12760                 l_cstr_info->marknum = 0;
12761         }*/
12762
12763         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);
12764 }
12765
12766 /**
12767  * Creates a tile-coder decoder.
12768  *
12769  * @param       p_stream                the stream to write data to.
12770  * @param       p_j2k                   J2K codec.
12771  * @param       p_manager               the user event manager.
12772 */
12773 opj_bool j2k_create_tcd(opj_j2k_v2_t *p_j2k,
12774                                                 struct opj_stream_private *p_stream,
12775                                                 struct opj_event_mgr * p_manager )
12776 {
12777         /* preconditions */
12778         assert(p_j2k != 00);
12779         assert(p_manager != 00);
12780         assert(p_stream != 00);
12781
12782         p_j2k->m_tcd = tcd_create_v2(OPJ_FALSE);
12783
12784         if (! p_j2k->m_tcd) {
12785                 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to create Tile Coder\n");
12786                 return OPJ_FALSE;
12787         }
12788
12789         if (! tcd_init_v2(p_j2k->m_tcd,p_j2k->m_private_image,&p_j2k->m_cp)) {
12790                 tcd_destroy_v2(p_j2k->m_tcd);
12791                 p_j2k->m_tcd = 00;
12792                 return OPJ_FALSE;
12793         }
12794
12795         return OPJ_TRUE;
12796 }
12797
12798
12799 /**
12800  * Writes a tile.
12801  * @param       p_j2k           the jpeg2000 codec.
12802  * @param       p_stream                        the stream to write data to.
12803  * @param       p_manager       the user event manager.
12804  */
12805 opj_bool j2k_write_tile (opj_j2k_v2_t * p_j2k,
12806                                                  OPJ_UINT32 p_tile_index,
12807                                                  OPJ_BYTE * p_data,
12808                                                  OPJ_UINT32 p_data_size,
12809                                                  opj_stream_private_t *p_stream,
12810                                                  opj_event_mgr_t * p_manager )
12811 {
12812         if (! j2k_pre_write_tile(p_j2k,p_tile_index,p_stream,p_manager)) {
12813                 opj_event_msg_v2(p_manager, EVT_ERROR, "Error while j2k_pre_write_tile with tile index = %d\n", p_tile_index);
12814                 return OPJ_FALSE;
12815         }
12816         else {
12817                 if (! j2k_post_write_tile(p_j2k,p_data,p_data_size,p_stream,p_manager)) {
12818                         opj_event_msg_v2(p_manager, EVT_ERROR, "Error while j2k_post_write_tile with tile index = %d\n", p_tile_index);
12819                         return OPJ_FALSE;
12820                 }
12821         }
12822
12823         return OPJ_TRUE;
12824 }