[trunk] refactoring of rsiz, profiles, and extensions management
[openjpeg.git] / src / lib / openjp2 / j2k.c
index f90e6eb972f9425996b2fa21db4e35569c4d7d1d..1e6024eb68f3a04725adcfeb24c1df231f57d53c 100644 (file)
@@ -1,14 +1,21 @@
 /*
- * Copyright (c) 2002-2007, Communications and Remote Sensing Laboratory, Universite catholique de Louvain (UCL), Belgium
- * Copyright (c) 2002-2007, Professor Benoit Macq
+ * The copyright in this software is being made available under the 2-clauses 
+ * BSD License, included below. This software may be subject to other third 
+ * party and contributor rights, including patent rights, and no such rights
+ * are granted under this license.
+ *
+ * Copyright (c) 2002-2014, Universite catholique de Louvain (UCL), Belgium
+ * Copyright (c) 2002-2014, Professor Benoit Macq
  * Copyright (c) 2001-2003, David Janssens
  * Copyright (c) 2002-2003, Yannick Verschueren
- * Copyright (c) 2003-2007, Francois-Olivier Devaux and Antonin Descampe
+ * Copyright (c) 2003-2007, Francois-Olivier Devaux 
+ * Copyright (c) 2003-2014, Antonin Descampe
  * Copyright (c) 2005, Herve Drolon, FreeImage Team
  * Copyright (c) 2008, Jerome Fimes, Communications & Systemes <jerome.fimes@c-s.fr>
  * Copyright (c) 2006-2007, Parvatha Elangovan
  * Copyright (c) 2010-2011, Kaori Hagihara
- * Copyright (c) 2011-2012, Mickael Savinaud, Communications & Systemes <mickael.savinaud@c-s.fr>
+ * Copyright (c) 2011-2012, Centre National d'Etudes Spatiales (CNES), France 
+ * Copyright (c) 2012, CS Systemes d'Information, France
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
@@ -49,7 +56,7 @@ static void opj_j2k_setup_header_reading (opj_j2k_t *p_j2k);
 /**
  * The read header procedure.
  */
-static opj_bool opj_j2k_read_header_procedure(  opj_j2k_t *p_j2k,
+static OPJ_BOOL opj_j2k_read_header_procedure(  opj_j2k_t *p_j2k,
                                                 opj_stream_private_t *p_stream,
                                                 opj_event_mgr_t * p_manager);
 
@@ -62,7 +69,7 @@ static opj_bool opj_j2k_read_header_procedure(  opj_j2k_t *p_j2k,
  *
  * @return true if the parameters are correct.
  */
-static opj_bool opj_j2k_encoding_validation (   opj_j2k_t * p_j2k,
+static OPJ_BOOL opj_j2k_encoding_validation (   opj_j2k_t * p_j2k,
                                                 opj_stream_private_t *p_stream,
                                                 opj_event_mgr_t * p_manager );
 
@@ -75,7 +82,7 @@ static opj_bool opj_j2k_encoding_validation (   opj_j2k_t * p_j2k,
  *
  * @return true if the parameters are correct.
  */
-static opj_bool opj_j2k_decoding_validation (   opj_j2k_t * p_j2k,
+static OPJ_BOOL opj_j2k_decoding_validation (   opj_j2k_t * p_j2k,
                                                 opj_stream_private_t *p_stream,
                                                 opj_event_mgr_t * p_manager );
 
@@ -106,20 +113,20 @@ static void opj_j2k_setup_end_compress (opj_j2k_t *p_j2k);
  *
  * @return true if the parameters are correct.
  */
-static opj_bool opj_j2k_mct_validation (opj_j2k_t * p_j2k,
+static OPJ_BOOL opj_j2k_mct_validation (opj_j2k_t * p_j2k,
                                         opj_stream_private_t *p_stream,
                                         opj_event_mgr_t * p_manager );
 
 /**
  * Builds the tcd decoder to use to decode tile.
  */
-static opj_bool opj_j2k_build_decoder ( opj_j2k_t * p_j2k,
+static OPJ_BOOL opj_j2k_build_decoder ( opj_j2k_t * p_j2k,
                                         opj_stream_private_t *p_stream,
                                         opj_event_mgr_t * p_manager );
 /**
  * Builds the tcd encoder to use to encode tile.
  */
-static opj_bool opj_j2k_build_encoder ( opj_j2k_t * p_j2k,
+static OPJ_BOOL opj_j2k_build_encoder ( opj_j2k_t * p_j2k,
                                         opj_stream_private_t *p_stream,
                                         opj_event_mgr_t * p_manager );
 
@@ -130,7 +137,7 @@ static opj_bool opj_j2k_build_encoder ( opj_j2k_t * p_j2k,
  * @param       p_j2k                           J2K codec.
  * @param       p_manager                   the user event manager.
 */
-static opj_bool opj_j2k_create_tcd(     opj_j2k_t *p_j2k,
+static OPJ_BOOL opj_j2k_create_tcd(     opj_j2k_t *p_j2k,
                                                                     opj_stream_private_t *p_stream,
                                                                     opj_event_mgr_t * p_manager );
 
@@ -144,7 +151,7 @@ static opj_bool opj_j2k_create_tcd(     opj_j2k_t *p_j2k,
  *
  * @return      true                            if all the procedures were successfully executed.
  */
-static opj_bool opj_j2k_exec (  opj_j2k_t * p_j2k,
+static OPJ_BOOL opj_j2k_exec (  opj_j2k_t * p_j2k,
                             opj_procedure_list_t * p_procedure_list,
                             opj_stream_private_t *p_stream,
                             opj_event_mgr_t * p_manager);
@@ -156,7 +163,7 @@ static opj_bool opj_j2k_exec (  opj_j2k_t * p_j2k,
  * @param       p_j2k                           J2K codec.
  * @param       p_manager               the user event manager.
 */
-static opj_bool opj_j2k_update_rates(   opj_j2k_t *p_j2k,
+static OPJ_BOOL opj_j2k_update_rates(   opj_j2k_t *p_j2k,
                                                                             opj_stream_private_t *p_stream,
                                                                             opj_event_mgr_t * p_manager );
 
@@ -164,14 +171,14 @@ static opj_bool opj_j2k_update_rates(   opj_j2k_t *p_j2k,
  * Copies the decoding tile parameters onto all the tile parameters.
  * Creates also the tile decoder.
  */
-static opj_bool opj_j2k_copy_default_tcp_and_create_tcd (       opj_j2k_t * p_j2k,
+static OPJ_BOOL opj_j2k_copy_default_tcp_and_create_tcd (       opj_j2k_t * p_j2k,
                                                             opj_stream_private_t *p_stream,
                                                             opj_event_mgr_t * p_manager );
 
 /**
  * Destroys the memory associated with the decoding of headers.
  */
-static opj_bool opj_j2k_destroy_header_memory ( opj_j2k_t * p_j2k,
+static OPJ_BOOL opj_j2k_destroy_header_memory ( opj_j2k_t * p_j2k,
                                                 opj_stream_private_t *p_stream,
                                                 opj_event_mgr_t * p_manager );
 
@@ -217,7 +224,7 @@ static void opj_j2k_cp_destroy (opj_cp_t *p_cp);
  *
  * @return FIXME DOC
 */
-static opj_bool opj_j2k_write_SPCod_SPCoc(      opj_j2k_t *p_j2k,
+static OPJ_BOOL opj_j2k_write_SPCod_SPCoc(      opj_j2k_t *p_j2k,
                                                                                     OPJ_UINT32 p_tile_no,
                                                                                     OPJ_UINT32 p_comp_no,
                                                                                     OPJ_BYTE * p_data,
@@ -245,7 +252,7 @@ static OPJ_UINT32 opj_j2k_get_SPCod_SPCoc_size (opj_j2k_t *p_j2k,
  * @param       p_header_size   the size of the data contained in the COM marker.
  * @param       p_manager       the user event manager.
 */
-static opj_bool opj_j2k_read_SPCod_SPCoc(   opj_j2k_t *p_j2k,
+static OPJ_BOOL opj_j2k_read_SPCod_SPCoc(   opj_j2k_t *p_j2k,
                                             OPJ_UINT32 compno,
                                             OPJ_BYTE * p_header_data,
                                             OPJ_UINT32 * p_header_size,
@@ -275,7 +282,7 @@ static OPJ_UINT32 opj_j2k_get_SQcd_SQcc_size (  opj_j2k_t *p_j2k,
  * @param       p_manager               the user event manager.
  *
 */
-static opj_bool opj_j2k_write_SQcd_SQcc(opj_j2k_t *p_j2k,
+static OPJ_BOOL opj_j2k_write_SQcd_SQcc(opj_j2k_t *p_j2k,
                                                                             OPJ_UINT32 p_tile_no,
                                                                             OPJ_UINT32 p_comp_no,
                                                                             OPJ_BYTE * p_data,
@@ -297,7 +304,7 @@ static void opj_j2k_update_tlm ( opj_j2k_t * p_j2k, OPJ_UINT32 p_tile_part_size)
  * @param       p_manager       the user event manager.
  *
 */
-static opj_bool opj_j2k_read_SQcd_SQcc( opj_j2k_t *p_j2k,
+static OPJ_BOOL opj_j2k_read_SQcd_SQcc( opj_j2k_t *p_j2k,
                                         OPJ_UINT32 compno,
                                         OPJ_BYTE * p_header_data,
                                         OPJ_UINT32 * p_header_size,
@@ -320,20 +327,20 @@ static void opj_j2k_copy_tile_quantization_parameters( opj_j2k_t *p_j2k );
 /**
  * Reads the tiles.
  */
-static opj_bool opj_j2k_decode_tiles (  opj_j2k_t *p_j2k,
+static OPJ_BOOL opj_j2k_decode_tiles (  opj_j2k_t *p_j2k,
                                         opj_stream_private_t *p_stream,
                                         opj_event_mgr_t * p_manager);
 
-static opj_bool opj_j2k_pre_write_tile ( opj_j2k_t * p_j2k,
+static OPJ_BOOL opj_j2k_pre_write_tile ( opj_j2k_t * p_j2k,
                                                                              OPJ_UINT32 p_tile_index,
                                                                              opj_stream_private_t *p_stream,
                                                                              opj_event_mgr_t * p_manager );
 
-static opj_bool opj_j2k_update_image_data (opj_tcd_t * p_tcd, OPJ_BYTE * p_data, opj_image_t* p_output_image);
+static OPJ_BOOL opj_j2k_update_image_data (opj_tcd_t * p_tcd, OPJ_BYTE * p_data, opj_image_t* p_output_image);
 
 static void opj_j2k_get_tile_data (opj_tcd_t * p_tcd, OPJ_BYTE * p_data);
 
-static opj_bool opj_j2k_post_write_tile (opj_j2k_t * p_j2k,
+static OPJ_BOOL opj_j2k_post_write_tile (opj_j2k_t * p_j2k,
                                                                              OPJ_BYTE * p_data,
                                                                              OPJ_UINT32 p_data_size,
                                                                              opj_stream_private_t *p_stream,
@@ -345,14 +352,14 @@ static opj_bool opj_j2k_post_write_tile (opj_j2k_t * p_j2k,
  */
 static void opj_j2k_setup_header_writing (opj_j2k_t *p_j2k);
 
-static opj_bool opj_j2k_write_first_tile_part(  opj_j2k_t *p_j2k,
+static OPJ_BOOL opj_j2k_write_first_tile_part(  opj_j2k_t *p_j2k,
                                                                                             OPJ_BYTE * p_data,
                                                                                             OPJ_UINT32 * p_data_written,
                                                                                             OPJ_UINT32 p_total_data_size,
                                                                                             opj_stream_private_t *p_stream,
                                                                                             struct opj_event_mgr * p_manager );
 
-static opj_bool opj_j2k_write_all_tile_parts(   opj_j2k_t *p_j2k,
+static OPJ_BOOL opj_j2k_write_all_tile_parts(   opj_j2k_t *p_j2k,
                                                                                             OPJ_BYTE * p_data,
                                                                                             OPJ_UINT32 * p_data_written,
                                                                                             OPJ_UINT32 p_total_data_size,
@@ -366,11 +373,11 @@ static opj_bool opj_j2k_write_all_tile_parts(   opj_j2k_t *p_j2k,
  * @param       p_j2k                   J2K codec.
  * @param       p_manager               the user event manager.
 */
-static opj_bool opj_j2k_get_end_header( opj_j2k_t *p_j2k,
+static OPJ_BOOL opj_j2k_get_end_header( opj_j2k_t *p_j2k,
                                         opj_stream_private_t *p_stream,
                                         opj_event_mgr_t * p_manager );
 
-static opj_bool opj_j2k_allocate_tile_element_cstr_index(opj_j2k_t *p_j2k);
+static OPJ_BOOL opj_j2k_allocate_tile_element_cstr_index(opj_j2k_t *p_j2k);
 
 /*
  * -----------------------------------------------------------------------
@@ -385,7 +392,7 @@ static opj_bool opj_j2k_allocate_tile_element_cstr_index(opj_j2k_t *p_j2k);
  * @param       p_j2k                   J2K codec.
  * @param       p_manager       the user event manager.
 */
-static opj_bool opj_j2k_write_soc(      opj_j2k_t *p_j2k,
+static OPJ_BOOL opj_j2k_write_soc(      opj_j2k_t *p_j2k,
                                                         opj_stream_private_t *p_stream,
                                                             opj_event_mgr_t * p_manager );
 
@@ -395,7 +402,7 @@ static opj_bool opj_j2k_write_soc(      opj_j2k_t *p_j2k,
  * @param       p_stream        XXX needs data
  * @param       p_manager       the user event manager.
 */
-static opj_bool opj_j2k_read_soc(   opj_j2k_t *p_j2k,
+static OPJ_BOOL opj_j2k_read_soc(   opj_j2k_t *p_j2k,
                                     opj_stream_private_t *p_stream,
                                     opj_event_mgr_t * p_manager );
 
@@ -406,7 +413,7 @@ static opj_bool opj_j2k_read_soc(   opj_j2k_t *p_j2k,
  * @param       p_stream        the stream to write data to.
  * @param       p_manager       the user event manager.
 */
-static opj_bool opj_j2k_write_siz(      opj_j2k_t *p_j2k,
+static OPJ_BOOL opj_j2k_write_siz(      opj_j2k_t *p_j2k,
                                                                 opj_stream_private_t *p_stream,
                                                                 opj_event_mgr_t * p_manager );
 
@@ -417,7 +424,7 @@ static opj_bool opj_j2k_write_siz(      opj_j2k_t *p_j2k,
  * @param       p_header_size   the size of the data contained in the SIZ marker.
  * @param       p_manager       the user event manager.
 */
-static opj_bool opj_j2k_read_siz(opj_j2k_t *p_j2k,
+static OPJ_BOOL opj_j2k_read_siz(opj_j2k_t *p_j2k,
                                  OPJ_BYTE * p_header_data,
                                  OPJ_UINT32 p_header_size,
                                  opj_event_mgr_t * p_manager);
@@ -429,7 +436,7 @@ static opj_bool opj_j2k_read_siz(opj_j2k_t *p_j2k,
  * @param       p_j2k                   J2K codec.
  * @param       p_manager       the user event manager.
 */
-static opj_bool opj_j2k_write_com(      opj_j2k_t *p_j2k,
+static OPJ_BOOL opj_j2k_write_com(      opj_j2k_t *p_j2k,
                                                                         opj_stream_private_t *p_stream,
                                                                         opj_event_mgr_t * p_manager );
 
@@ -440,7 +447,7 @@ static opj_bool opj_j2k_write_com(      opj_j2k_t *p_j2k,
  * @param       p_header_size   the size of the data contained in the COM marker.
  * @param       p_manager       the user event manager.
 */
-static opj_bool opj_j2k_read_com (  opj_j2k_t *p_j2k,
+static OPJ_BOOL opj_j2k_read_com (  opj_j2k_t *p_j2k,
                                     OPJ_BYTE * p_header_data,
                                     OPJ_UINT32 p_header_size,
                                     opj_event_mgr_t * p_manager );
@@ -451,7 +458,7 @@ static opj_bool opj_j2k_read_com (  opj_j2k_t *p_j2k,
  * @param       p_j2k                   J2K codec.
  * @param       p_manager       the user event manager.
 */
-static opj_bool opj_j2k_write_cod(      opj_j2k_t *p_j2k,
+static OPJ_BOOL opj_j2k_write_cod(      opj_j2k_t *p_j2k,
                                                                         opj_stream_private_t *p_stream,
                                                                         opj_event_mgr_t * p_manager );
 
@@ -462,11 +469,12 @@ static opj_bool opj_j2k_write_cod(      opj_j2k_t *p_j2k,
  * @param       p_header_size   the size of the data contained in the COD marker.
  * @param       p_manager               the user event manager.
 */
-static opj_bool opj_j2k_read_cod (  opj_j2k_t *p_j2k,
+static OPJ_BOOL opj_j2k_read_cod (  opj_j2k_t *p_j2k,
                                     OPJ_BYTE * p_header_data,
                                     OPJ_UINT32 p_header_size,
                                     opj_event_mgr_t * p_manager);
 
+#if 0
 /**
  * Writes the COC marker (Coding style component)
  *
@@ -475,10 +483,13 @@ static opj_bool opj_j2k_read_cod (  opj_j2k_t *p_j2k,
  * @param       p_stream    the stream to write data to.
  * @param       p_manager   the user event manager.
 */
-static opj_bool opj_j2k_write_coc(  opj_j2k_t *p_j2k,
+static OPJ_BOOL opj_j2k_write_coc(  opj_j2k_t *p_j2k,
                                                                 OPJ_UINT32 p_comp_no,
                                                                 opj_stream_private_t *p_stream,
                                                                 opj_event_mgr_t * p_manager );
+#endif
+
+#if 0
 /**
  * Writes the COC marker (Coding style component)
  *
@@ -493,6 +504,7 @@ static void opj_j2k_write_coc_in_memory(opj_j2k_t *p_j2k,
                                                                             OPJ_BYTE * p_data,
                                                                             OPJ_UINT32 * p_data_written,
                                                                             opj_event_mgr_t * p_manager );
+#endif
 
 /**
  * Gets the maximum size taken by a coc.
@@ -508,7 +520,7 @@ static OPJ_UINT32 opj_j2k_get_max_coc_size(opj_j2k_t *p_j2k);
  * @param       p_header_size   the size of the data contained in the COC marker.
  * @param       p_manager               the user event manager.
 */
-static opj_bool opj_j2k_read_coc (  opj_j2k_t *p_j2k,
+static OPJ_BOOL opj_j2k_read_coc (  opj_j2k_t *p_j2k,
                                     OPJ_BYTE * p_header_data,
                                     OPJ_UINT32 p_header_size,
                                     opj_event_mgr_t * p_manager );
@@ -520,7 +532,7 @@ static opj_bool opj_j2k_read_coc (  opj_j2k_t *p_j2k,
  * @param       p_stream                the stream to write data to.
  * @param       p_manager               the user event manager.
 */
-static opj_bool opj_j2k_write_qcd(      opj_j2k_t *p_j2k,
+static OPJ_BOOL opj_j2k_write_qcd(      opj_j2k_t *p_j2k,
                                                                         opj_stream_private_t *p_stream,
                                                                         opj_event_mgr_t * p_manager );
 
@@ -531,10 +543,11 @@ static opj_bool opj_j2k_write_qcd(      opj_j2k_t *p_j2k,
  * @param       p_header_size   the size of the data contained in the QCD marker.
  * @param       p_manager               the user event manager.
 */
-static opj_bool opj_j2k_read_qcd (  opj_j2k_t *p_j2k,
+static OPJ_BOOL opj_j2k_read_qcd (  opj_j2k_t *p_j2k,
                                     OPJ_BYTE * p_header_data,
                                     OPJ_UINT32 p_header_size,
                                     opj_event_mgr_t * p_manager );
+#if 0
 /**
  * Writes the QCC marker (quantization component)
  *
@@ -543,11 +556,13 @@ static opj_bool opj_j2k_read_qcd (  opj_j2k_t *p_j2k,
  * @param       p_j2k                   J2K codec.
  * @param       p_manager               the user event manager.
 */
-static opj_bool opj_j2k_write_qcc(      opj_j2k_t *p_j2k,
+static OPJ_BOOL opj_j2k_write_qcc(      opj_j2k_t *p_j2k,
                                                                         OPJ_UINT32 p_comp_no,
                                                                         opj_stream_private_t *p_stream,
                                                                         opj_event_mgr_t * p_manager );
+#endif
 
+#if 0
 /**
  * Writes the QCC marker (quantization component)
  *
@@ -562,6 +577,7 @@ static void opj_j2k_write_qcc_in_memory(opj_j2k_t *p_j2k,
                                                                             OPJ_BYTE * p_data,
                                                                             OPJ_UINT32 * p_data_written,
                                                                             opj_event_mgr_t * p_manager );
+#endif
 
 /**
  * Gets the maximum size taken by a qcc.
@@ -575,7 +591,7 @@ static OPJ_UINT32 opj_j2k_get_max_qcc_size (opj_j2k_t *p_j2k);
  * @param       p_header_size   the size of the data contained in the QCC marker.
  * @param       p_manager               the user event manager.
 */
-static opj_bool opj_j2k_read_qcc(   opj_j2k_t *p_j2k,
+static OPJ_BOOL opj_j2k_read_qcc(   opj_j2k_t *p_j2k,
                                     OPJ_BYTE * p_header_data,
                                     OPJ_UINT32 p_header_size,
                                     opj_event_mgr_t * p_manager);
@@ -586,7 +602,7 @@ static opj_bool opj_j2k_read_qcc(   opj_j2k_t *p_j2k,
  * @param       p_j2k                           J2K codec.
  * @param       p_manager               the user event manager.
 */
-static opj_bool opj_j2k_write_poc(      opj_j2k_t *p_j2k,
+static OPJ_BOOL opj_j2k_write_poc(      opj_j2k_t *p_j2k,
                                                                         opj_stream_private_t *p_stream,
                                                                         opj_event_mgr_t * p_manager );
 /**
@@ -614,7 +630,7 @@ static OPJ_UINT32 opj_j2k_get_max_poc_size(opj_j2k_t *p_j2k);
  * @param       p_header_size   the size of the data contained in the POC marker.
  * @param       p_manager               the user event manager.
 */
-static opj_bool opj_j2k_read_poc (  opj_j2k_t *p_j2k,
+static OPJ_BOOL opj_j2k_read_poc (  opj_j2k_t *p_j2k,
                                     OPJ_BYTE * p_header_data,
                                     OPJ_UINT32 p_header_size,
                                     opj_event_mgr_t * p_manager );
@@ -639,7 +655,7 @@ static OPJ_UINT32 opj_j2k_get_specific_header_sizes(opj_j2k_t *p_j2k);
  * @param       p_header_size   the size of the data contained in the TLM marker.
  * @param       p_manager               the user event manager.
 */
-static opj_bool opj_j2k_read_crg (  opj_j2k_t *p_j2k,
+static OPJ_BOOL opj_j2k_read_crg (  opj_j2k_t *p_j2k,
                                     OPJ_BYTE * p_header_data,
                                     OPJ_UINT32 p_header_size,
                                     opj_event_mgr_t * p_manager );
@@ -651,7 +667,7 @@ static opj_bool opj_j2k_read_crg (  opj_j2k_t *p_j2k,
  * @param       p_header_size   the size of the data contained in the TLM marker.
  * @param       p_manager               the user event manager.
 */
-static opj_bool opj_j2k_read_tlm (  opj_j2k_t *p_j2k,
+static OPJ_BOOL opj_j2k_read_tlm (  opj_j2k_t *p_j2k,
                                     OPJ_BYTE * p_header_data,
                                     OPJ_UINT32 p_header_size,
                                     opj_event_mgr_t * p_manager);
@@ -663,7 +679,7 @@ static opj_bool opj_j2k_read_tlm (  opj_j2k_t *p_j2k,
  * @param       p_j2k                   J2K codec.
  * @param       p_manager               the user event manager.
 */
-static opj_bool opj_j2k_write_updated_tlm(      opj_j2k_t *p_j2k,
+static OPJ_BOOL opj_j2k_write_updated_tlm(      opj_j2k_t *p_j2k,
                                             opj_stream_private_t *p_stream,
                                             opj_event_mgr_t * p_manager );
 
@@ -675,7 +691,7 @@ static opj_bool opj_j2k_write_updated_tlm(      opj_j2k_t *p_j2k,
  * @param       p_header_size   the size of the data contained in the TLM marker.
  * @param       p_manager               the user event manager.
 */
-static opj_bool opj_j2k_read_plm (  opj_j2k_t *p_j2k,
+static OPJ_BOOL opj_j2k_read_plm (  opj_j2k_t *p_j2k,
                                     OPJ_BYTE * p_header_data,
                                     OPJ_UINT32 p_header_size,
                                     opj_event_mgr_t * p_manager);
@@ -687,7 +703,7 @@ static opj_bool opj_j2k_read_plm (  opj_j2k_t *p_j2k,
  * @param       p_header_size   the size of the data contained in the PLT marker.
  * @param       p_manager               the user event manager.
 */
-static opj_bool opj_j2k_read_plt (  opj_j2k_t *p_j2k,
+static OPJ_BOOL opj_j2k_read_plt (  opj_j2k_t *p_j2k,
                                     OPJ_BYTE * p_header_data,
                                     OPJ_UINT32 p_header_size,
                                     opj_event_mgr_t * p_manager );
@@ -701,7 +717,7 @@ static opj_bool opj_j2k_read_plt (  opj_j2k_t *p_j2k,
  * @param       p_header_size   the size of the data contained in the POC marker.
  * @param       p_manager               the user event manager.
 */
-static opj_bool j2k_read_ppm_v2 (
+static OPJ_BOOL j2k_read_ppm_v2 (
                                                 opj_j2k_t *p_j2k,
                                                 OPJ_BYTE * p_header_data,
                                                 OPJ_UINT32 p_header_size,
@@ -709,7 +725,7 @@ static opj_bool j2k_read_ppm_v2 (
                                         );
 #endif
 
-static opj_bool j2k_read_ppm_v3 (
+static OPJ_BOOL j2k_read_ppm_v3 (
                                                 opj_j2k_t *p_j2k,
                                                 OPJ_BYTE * p_header_data,
                                                 OPJ_UINT32 p_header_size,
@@ -723,7 +739,7 @@ static opj_bool j2k_read_ppm_v3 (
  * @param       p_header_size   the size of the data contained in the PPT marker.
  * @param       p_manager               the user event manager.
 */
-static opj_bool opj_j2k_read_ppt (  opj_j2k_t *p_j2k,
+static OPJ_BOOL opj_j2k_read_ppt (  opj_j2k_t *p_j2k,
                                     OPJ_BYTE * p_header_data,
                                     OPJ_UINT32 p_header_size,
                                     opj_event_mgr_t * p_manager );
@@ -734,7 +750,7 @@ static opj_bool opj_j2k_read_ppt (  opj_j2k_t *p_j2k,
  * @param       p_j2k                           J2K codec.
  * @param       p_manager               the user event manager.
 */
-static opj_bool opj_j2k_write_tlm(      opj_j2k_t *p_j2k,
+static OPJ_BOOL opj_j2k_write_tlm(      opj_j2k_t *p_j2k,
                                                                         opj_stream_private_t *p_stream,
                                                                         opj_event_mgr_t * p_manager );
 
@@ -747,7 +763,7 @@ static opj_bool opj_j2k_write_tlm(      opj_j2k_t *p_j2k,
  * @param       p_stream         the stream to write data to.
  * @param       p_manager        the user event manager.
 */
-static opj_bool opj_j2k_write_sot(      opj_j2k_t *p_j2k,
+static OPJ_BOOL opj_j2k_write_sot(      opj_j2k_t *p_j2k,
                                                                         OPJ_BYTE * p_data,
                                                                         OPJ_UINT32 * p_data_written,
                                                                         const opj_stream_private_t *p_stream,
@@ -761,7 +777,7 @@ static opj_bool opj_j2k_write_sot(      opj_j2k_t *p_j2k,
  * @param       p_header_size   the size of the data contained in the PPT marker.
  * @param       p_manager               the user event manager.
 */
-static opj_bool opj_j2k_read_sot (  opj_j2k_t *p_j2k,
+static OPJ_BOOL opj_j2k_read_sot (  opj_j2k_t *p_j2k,
                                     OPJ_BYTE * p_header_data,
                                     OPJ_UINT32 p_header_size,
                                     opj_event_mgr_t * p_manager );
@@ -776,7 +792,7 @@ static opj_bool opj_j2k_read_sot (  opj_j2k_t *p_j2k,
  * @param       p_stream            the stream to write data to.
  * @param       p_manager           the user event manager.
 */
-static opj_bool opj_j2k_write_sod(      opj_j2k_t *p_j2k,
+static OPJ_BOOL opj_j2k_write_sod(      opj_j2k_t *p_j2k,
                                                                         opj_tcd_t * p_tile_coder,
                                                                         OPJ_BYTE * p_data,
                                                                         OPJ_UINT32 * p_data_written,
@@ -791,7 +807,7 @@ static opj_bool opj_j2k_write_sod(      opj_j2k_t *p_j2k,
  * @param       p_stream                FIXME DOC
  * @param       p_manager               the user event manager.
 */
-static opj_bool opj_j2k_read_sod(   opj_j2k_t *p_j2k,
+static OPJ_BOOL opj_j2k_read_sod(   opj_j2k_t *p_j2k,
                                     opj_stream_private_t *p_stream,
                                     opj_event_mgr_t * p_manager );
 
@@ -814,7 +830,7 @@ void opj_j2k_update_tlm (opj_j2k_t * p_j2k, OPJ_UINT32 p_tile_part_size )
  * @param       p_j2k                   J2K codec.
  * @param       p_manager               the user event manager.
 */
-static opj_bool opj_j2k_write_rgn(  opj_j2k_t *p_j2k,
+static OPJ_BOOL opj_j2k_write_rgn(  opj_j2k_t *p_j2k,
                                     OPJ_UINT32 p_tile_no,
                                     OPJ_UINT32 p_comp_no,
                                     OPJ_UINT32 nb_comps,
@@ -829,7 +845,7 @@ static opj_bool opj_j2k_write_rgn(  opj_j2k_t *p_j2k,
  * @param       p_header_size   the size of the data contained in the POC marker.
  * @param       p_manager               the user event manager.
 */
-static opj_bool opj_j2k_read_rgn (opj_j2k_t *p_j2k,
+static OPJ_BOOL opj_j2k_read_rgn (opj_j2k_t *p_j2k,
                                   OPJ_BYTE * p_header_data,
                                   OPJ_UINT32 p_header_size,
                                   opj_event_mgr_t * p_manager );
@@ -841,10 +857,11 @@ static opj_bool opj_j2k_read_rgn (opj_j2k_t *p_j2k,
  * @param       p_j2k                   J2K codec.
  * @param       p_manager               the user event manager.
 */
-static opj_bool opj_j2k_write_eoc(      opj_j2k_t *p_j2k,
+static OPJ_BOOL opj_j2k_write_eoc(      opj_j2k_t *p_j2k,
                                     opj_stream_private_t *p_stream,
                                     opj_event_mgr_t * p_manager );
 
+#if 0
 /**
  * Reads a EOC marker (End Of Codestream)
  *
@@ -852,9 +869,10 @@ static opj_bool opj_j2k_write_eoc(      opj_j2k_t *p_j2k,
  * @param       p_stream                FIXME DOC
  * @param       p_manager               the user event manager.
 */
-static opj_bool opj_j2k_read_eoc (      opj_j2k_t *p_j2k,
+static OPJ_BOOL opj_j2k_read_eoc (      opj_j2k_t *p_j2k,
                                                                 opj_stream_private_t *p_stream,
                                                                 opj_event_mgr_t * p_manager );
+#endif
 
 /**
  * Writes the CBD-MCT-MCC-MCO markers (Multi components transform)
@@ -863,7 +881,7 @@ static opj_bool opj_j2k_read_eoc (      opj_j2k_t *p_j2k,
  * @param       p_j2k                   J2K codec.
  * @param       p_manager       the user event manager.
 */
-static opj_bool opj_j2k_write_mct_data_group(   opj_j2k_t *p_j2k,
+static OPJ_BOOL opj_j2k_write_mct_data_group(   opj_j2k_t *p_j2k,
                                                 opj_stream_private_t *p_stream,
                                                 opj_event_mgr_t * p_manager );
 
@@ -874,7 +892,7 @@ static opj_bool opj_j2k_write_mct_data_group(   opj_j2k_t *p_j2k,
  * @param       p_j2k                   J2K codec.
  * @param       p_manager               the user event manager.
 */
-static opj_bool opj_j2k_init_info(      opj_j2k_t *p_j2k,
+static OPJ_BOOL opj_j2k_init_info(      opj_j2k_t *p_j2k,
                                     opj_stream_private_t *p_stream,
                                     opj_event_mgr_t * p_manager );
 
@@ -885,7 +903,7 @@ Add main header marker information
 @param pos          byte offset of marker segment
 @param len          length of marker segment
  */
-static opj_bool opj_j2k_add_mhmarker(opj_codestream_index_t *cstr_index, OPJ_UINT32 type, OPJ_OFF_T pos, OPJ_UINT32 len) ;
+static OPJ_BOOL opj_j2k_add_mhmarker(opj_codestream_index_t *cstr_index, OPJ_UINT32 type, OPJ_OFF_T pos, OPJ_UINT32 len) ;
 /**
 Add tile header marker information
 @param tileno       tile index number
@@ -894,7 +912,7 @@ Add tile header marker information
 @param pos          byte offset of marker segment
 @param len          length of marker segment
  */
-static opj_bool opj_j2k_add_tlmarker(OPJ_UINT32 tileno, opj_codestream_index_t *cstr_index, OPJ_UINT32 type, OPJ_OFF_T pos, OPJ_UINT32 len);
+static OPJ_BOOL opj_j2k_add_tlmarker(OPJ_UINT32 tileno, opj_codestream_index_t *cstr_index, OPJ_UINT32 type, OPJ_OFF_T pos, OPJ_UINT32 len);
 
 /**
  * Reads an unknown marker
@@ -906,7 +924,7 @@ static opj_bool opj_j2k_add_tlmarker(OPJ_UINT32 tileno, opj_codestream_index_t *
  *
  * @return      true                    if the marker could be deduced.
 */
-static opj_bool opj_j2k_read_unk( opj_j2k_t *p_j2k,
+static OPJ_BOOL opj_j2k_read_unk( opj_j2k_t *p_j2k,
                                   opj_stream_private_t *p_stream,
                                   OPJ_UINT32 *output_marker,
                                   opj_event_mgr_t * p_manager );
@@ -919,7 +937,7 @@ static opj_bool opj_j2k_read_unk( opj_j2k_t *p_j2k,
  * @param       p_stream        the stream to write data to.
  * @param       p_manager       the user event manager.
 */
-static opj_bool opj_j2k_write_mct_record(       opj_j2k_t *p_j2k,
+static OPJ_BOOL opj_j2k_write_mct_record(       opj_j2k_t *p_j2k,
                                                                                     opj_mct_data_t * p_mct_record,
                                             opj_stream_private_t *p_stream,
                                             opj_event_mgr_t * p_manager );
@@ -932,7 +950,7 @@ static opj_bool opj_j2k_write_mct_record(       opj_j2k_t *p_j2k,
  * @param       p_header_size   the size of the data contained in the MCT marker.
  * @param       p_manager               the user event manager.
 */
-static opj_bool opj_j2k_read_mct (      opj_j2k_t *p_j2k,
+static OPJ_BOOL opj_j2k_read_mct (      opj_j2k_t *p_j2k,
                                                                     OPJ_BYTE * p_header_data,
                                                                     OPJ_UINT32 p_header_size,
                                                                     opj_event_mgr_t * p_manager );
@@ -945,7 +963,7 @@ static opj_bool opj_j2k_read_mct (      opj_j2k_t *p_j2k,
  * @param       p_stream                the stream to write data to.
  * @param       p_manager               the user event manager.
 */
-static opj_bool opj_j2k_write_mcc_record(   opj_j2k_t *p_j2k,
+static OPJ_BOOL opj_j2k_write_mcc_record(   opj_j2k_t *p_j2k,
                                             opj_simple_mcc_decorrelation_data_t * p_mcc_record,
                                             opj_stream_private_t *p_stream,
                                             opj_event_mgr_t * p_manager );
@@ -958,7 +976,7 @@ static opj_bool opj_j2k_write_mcc_record(   opj_j2k_t *p_j2k,
  * @param       p_header_size   the size of the data contained in the MCC marker.
  * @param       p_manager               the user event manager.
 */
-static opj_bool opj_j2k_read_mcc (      opj_j2k_t *p_j2k,
+static OPJ_BOOL opj_j2k_read_mcc (      opj_j2k_t *p_j2k,
                                                                     OPJ_BYTE * p_header_data,
                                                                     OPJ_UINT32 p_header_size,
                                                                     opj_event_mgr_t * p_manager );
@@ -970,7 +988,7 @@ static opj_bool opj_j2k_read_mcc (      opj_j2k_t *p_j2k,
  * @param       p_j2k                           J2K codec.
  * @param       p_manager               the user event manager.
 */
-static opj_bool opj_j2k_write_mco(      opj_j2k_t *p_j2k,
+static OPJ_BOOL opj_j2k_write_mco(      opj_j2k_t *p_j2k,
                                     opj_stream_private_t *p_stream,
                                     opj_event_mgr_t * p_manager );
 
@@ -982,12 +1000,12 @@ static opj_bool opj_j2k_write_mco(      opj_j2k_t *p_j2k,
  * @param       p_header_size   the size of the data contained in the MCO marker.
  * @param       p_manager               the user event manager.
 */
-static opj_bool opj_j2k_read_mco (      opj_j2k_t *p_j2k,
+static OPJ_BOOL opj_j2k_read_mco (      opj_j2k_t *p_j2k,
                                                                     OPJ_BYTE * p_header_data,
                                                                     OPJ_UINT32 p_header_size,
                                                                     opj_event_mgr_t * p_manager );
 
-static opj_bool opj_j2k_add_mct(opj_tcp_t * p_tcp, opj_image_t * p_image, OPJ_UINT32 p_index);
+static OPJ_BOOL opj_j2k_add_mct(opj_tcp_t * p_tcp, opj_image_t * p_image, OPJ_UINT32 p_index);
 
 static void  opj_j2k_read_int16_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
 static void  opj_j2k_read_int32_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
@@ -1011,7 +1029,7 @@ static void  opj_j2k_write_float_to_float64 (const void * p_src_data, void * p_d
  * @param       p_j2k                   J2K codec.
  * @param       p_manager               the user event manager.
 */
-static opj_bool opj_j2k_end_encoding(   opj_j2k_t *p_j2k,
+static OPJ_BOOL opj_j2k_end_encoding(   opj_j2k_t *p_j2k,
                                                                             opj_stream_private_t *p_stream,
                                                                             opj_event_mgr_t * p_manager );
 
@@ -1022,7 +1040,7 @@ static opj_bool opj_j2k_end_encoding(   opj_j2k_t *p_j2k,
  * @param       p_j2k                           J2K codec.
  * @param       p_manager               the user event manager.
 */
-static opj_bool opj_j2k_write_cbd(      opj_j2k_t *p_j2k,
+static OPJ_BOOL opj_j2k_write_cbd(      opj_j2k_t *p_j2k,
                                                                     opj_stream_private_t *p_stream,
                                                                         opj_event_mgr_t * p_manager );
 
@@ -1033,21 +1051,36 @@ static opj_bool opj_j2k_write_cbd(      opj_j2k_t *p_j2k,
  * @param       p_header_size   the size of the data contained in the CBD marker.
  * @param       p_manager               the user event manager.
 */
-static opj_bool opj_j2k_read_cbd (      opj_j2k_t *p_j2k,
+static OPJ_BOOL opj_j2k_read_cbd (      opj_j2k_t *p_j2k,
                                                                 OPJ_BYTE * p_header_data,
                                                                 OPJ_UINT32 p_header_size,
                                                                 opj_event_mgr_t * p_manager);
 
+#if 0
 /**
- * Writes the image components.
+ * Writes COC marker for each component.
  *
  * @param       p_stream                the stream to write data to.
  * @param       p_j2k                   J2K codec.
  * @param       p_manager               the user event manager.
 */
-static opj_bool opj_j2k_write_image_components( opj_j2k_t *p_j2k,
+static OPJ_BOOL opj_j2k_write_all_coc( opj_j2k_t *p_j2k,
                                                                         opj_stream_private_t *p_stream,
                                                                         opj_event_mgr_t * p_manager );
+#endif
+
+#if 0
+/**
+ * Writes QCC marker for each component.
+ *
+ * @param       p_stream                the stream to write data to.
+ * @param       p_j2k                   J2K codec.
+ * @param       p_manager               the user event manager.
+*/
+static OPJ_BOOL opj_j2k_write_all_qcc( opj_j2k_t *p_j2k,
+                                                                        opj_stream_private_t *p_stream,
+                                                                        opj_event_mgr_t * p_manager );
+#endif
 
 /**
  * Writes regions of interests.
@@ -1056,7 +1089,7 @@ static opj_bool opj_j2k_write_image_components( opj_j2k_t *p_j2k,
  * @param       p_j2k                   J2K codec.
  * @param       p_manager               the user event manager.
 */
-static opj_bool opj_j2k_write_regions(  opj_j2k_t *p_j2k,
+static OPJ_BOOL opj_j2k_write_regions(  opj_j2k_t *p_j2k,
                                                                         opj_stream_private_t *p_stream,
                                                                         opj_event_mgr_t * p_manager );
 
@@ -1067,7 +1100,7 @@ static opj_bool opj_j2k_write_regions(  opj_j2k_t *p_j2k,
  * @param       p_j2k                   J2K codec.
  * @param       p_manager               the user event manager.
 */
-static opj_bool opj_j2k_write_epc(      opj_j2k_t *p_j2k,
+static OPJ_BOOL opj_j2k_write_epc(      opj_j2k_t *p_j2k,
                                                                     opj_stream_private_t *p_stream,
                                                                     opj_event_mgr_t * p_manager );
 
@@ -1084,7 +1117,7 @@ static opj_bool opj_j2k_write_epc(      opj_j2k_t *p_j2k,
  *
  * @return      true if the pocs are valid.
  */
-static opj_bool opj_j2k_check_poc_val(  const opj_poc_t *p_pocs,
+static OPJ_BOOL opj_j2k_check_poc_val(  const opj_poc_t *p_pocs,
                                                                             OPJ_UINT32 p_nb_pocs,
                                                                             OPJ_UINT32 p_nb_resolutions,
                                                                             OPJ_UINT32 numcomps,
@@ -1114,7 +1147,7 @@ static OPJ_UINT32 opj_j2k_get_num_tp( opj_cp_t *cp, OPJ_UINT32 pino, OPJ_UINT32
  *
  * @return true if the function was successful, false else.
  */
-static opj_bool opj_j2k_calculate_tp(   opj_j2k_t *p_j2k,
+static OPJ_BOOL opj_j2k_calculate_tp(   opj_j2k_t *p_j2k,
                                                                             opj_cp_t *cp,
                                                                             OPJ_UINT32 * p_nb_tiles,
                                                                             opj_image_t *image,
@@ -1130,6 +1163,12 @@ static OPJ_FLOAT32 opj_j2k_get_tp_stride (opj_tcp_t * p_tcp);
 
 static OPJ_FLOAT32 opj_j2k_get_default_stride (opj_tcp_t * p_tcp);
 
+static int opj_j2k_initialise_4K_poc(opj_poc_t *POC, int numres);
+
+static void opj_j2k_set_cinema_parameters(opj_cparameters_t *parameters, opj_image_t *image, opj_event_mgr_t *p_manager);
+
+static OPJ_BOOL opj_j2k_is_cinema_compliant(opj_image_t *image, OPJ_UINT16 rsiz, opj_event_mgr_t *p_manager);
+
 /*@}*/
 
 /*@}*/
@@ -1193,7 +1232,7 @@ typedef struct opj_dec_memory_marker_handler
         /** value of the state when the marker can appear */
         OPJ_UINT32 states;
         /** action linked to the marker */
-        opj_bool (*handler) (   opj_j2k_t *p_j2k,
+        OPJ_BOOL (*handler) (   opj_j2k_t *p_j2k,
                             OPJ_BYTE * p_header_data,
                             OPJ_UINT32 p_header_size,
                             opj_event_mgr_t * p_manager );
@@ -1439,7 +1478,7 @@ char *opj_j2k_convert_progression_order(OPJ_PROG_ORDER prg_order){
         return po->str_prog;
 }
 
-opj_bool opj_j2k_check_poc_val( const opj_poc_t *p_pocs,
+OPJ_BOOL opj_j2k_check_poc_val( const opj_poc_t *p_pocs,
                                                         OPJ_UINT32 p_nb_pocs,
                                                         OPJ_UINT32 p_nb_resolutions,
                                                         OPJ_UINT32 p_num_comps,
@@ -1452,7 +1491,7 @@ opj_bool opj_j2k_check_poc_val( const opj_poc_t *p_pocs,
         OPJ_UINT32 step_c = 1;
         OPJ_UINT32 step_r = p_num_comps * step_c;
         OPJ_UINT32 step_l = p_nb_resolutions * step_r;
-        opj_bool loss = OPJ_FALSE;
+        OPJ_BOOL loss = OPJ_FALSE;
         OPJ_UINT32 layno0 = 0;
 
         packet_array = (OPJ_UINT32*) opj_calloc(step_l * p_num_layers, sizeof(OPJ_UINT32));
@@ -1463,6 +1502,7 @@ opj_bool opj_j2k_check_poc_val( const opj_poc_t *p_pocs,
         memset(packet_array,0,step_l * p_num_layers* sizeof(OPJ_UINT32));
 
         if (p_nb_pocs == 0) {
+        opj_free(packet_array);
                 return OPJ_TRUE;
         }
 
@@ -1546,7 +1586,7 @@ opj_bool opj_j2k_check_poc_val( const opj_poc_t *p_pocs,
 OPJ_UINT32 opj_j2k_get_num_tp(opj_cp_t *cp, OPJ_UINT32 pino, OPJ_UINT32 tileno)
 {
         const OPJ_CHAR *prog = 00;
-        OPJ_UINT32 i;
+        OPJ_INT32 i;
         OPJ_UINT32 tpnum = 1;
         opj_tcp_t *tcp = 00;
         opj_poc_t * l_current_poc = 00;
@@ -1601,7 +1641,7 @@ OPJ_UINT32 opj_j2k_get_num_tp(opj_cp_t *cp, OPJ_UINT32 pino, OPJ_UINT32 tileno)
         return tpnum;
 }
 
-opj_bool opj_j2k_calculate_tp(  opj_j2k_t *p_j2k,
+OPJ_BOOL opj_j2k_calculate_tp(  opj_j2k_t *p_j2k,
                                                         opj_cp_t *cp,
                                                         OPJ_UINT32 * p_nb_tiles,
                                                         opj_image_t *image,
@@ -1679,7 +1719,7 @@ opj_bool opj_j2k_calculate_tp(  opj_j2k_t *p_j2k,
         return OPJ_TRUE;
 }
 
-opj_bool opj_j2k_write_soc(     opj_j2k_t *p_j2k,
+OPJ_BOOL opj_j2k_write_soc(     opj_j2k_t *p_j2k,
                                                 opj_stream_private_t *p_stream,
                                                     opj_event_mgr_t * p_manager )
 {
@@ -1704,7 +1744,7 @@ opj_bool opj_j2k_write_soc(     opj_j2k_t *p_j2k,
 #ifdef USE_JPWL
         /* update markers struct */
 /*
-        opj_bool res = j2k_add_marker(p_j2k->cstr_info, J2K_MS_SOC, p_stream_tell(p_stream) - 2, 2);
+        OPJ_BOOL res = j2k_add_marker(p_j2k->cstr_info, J2K_MS_SOC, p_stream_tell(p_stream) - 2, 2);
 */
   assert( 0 && "TODO" );
 #endif /* USE_JPWL */
@@ -1719,7 +1759,7 @@ opj_bool opj_j2k_write_soc(     opj_j2k_t *p_j2k,
  * @param       p_stream        FIXME DOC
  * @param       p_manager       the user event manager.
 */
-static opj_bool opj_j2k_read_soc(   opj_j2k_t *p_j2k,
+static OPJ_BOOL opj_j2k_read_soc(   opj_j2k_t *p_j2k,
                                     opj_stream_private_t *p_stream,
                                     opj_event_mgr_t * p_manager
                                     )
@@ -1757,7 +1797,7 @@ static opj_bool opj_j2k_read_soc(   opj_j2k_t *p_j2k,
         return OPJ_TRUE;
 }
 
-opj_bool opj_j2k_write_siz(     opj_j2k_t *p_j2k,
+OPJ_BOOL opj_j2k_write_siz(     opj_j2k_t *p_j2k,
                                                         opj_stream_private_t *p_stream,
                                                         opj_event_mgr_t * p_manager )
 {
@@ -1859,7 +1899,7 @@ opj_bool opj_j2k_write_siz(     opj_j2k_t *p_j2k,
  * @param       p_header_size   the size of the data contained in the SIZ marker.
  * @param       p_manager       the user event manager.
 */
-static opj_bool opj_j2k_read_siz(opj_j2k_t *p_j2k,
+static OPJ_BOOL opj_j2k_read_siz(opj_j2k_t *p_j2k,
                                  OPJ_BYTE * p_header_data,
                                  OPJ_UINT32 p_header_size,
                                  opj_event_mgr_t * p_manager
@@ -1900,7 +1940,7 @@ static opj_bool opj_j2k_read_siz(opj_j2k_t *p_j2k,
 
         opj_read_bytes(p_header_data,&l_tmp ,2);                                                /* Rsiz (capabilities) */
         p_header_data+=2;
-        l_cp->rsiz = (OPJ_RSIZ_CAPABILITIES) l_tmp;
+        l_cp->rsiz = (OPJ_UINT16) l_tmp;
         opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_image->x1, 4);   /* Xsiz */
         p_header_data+=4;
         opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_image->y1, 4);   /* Ysiz */
@@ -1931,6 +1971,23 @@ static opj_bool opj_j2k_read_siz(opj_j2k_t *p_j2k,
                 return OPJ_FALSE;
         }
 
+        /* testcase 4035.pdf.SIGSEGV.d8b.3375 */
+        if (l_image->x0 > l_image->x1 || l_image->y0 > l_image->y1) {
+                opj_event_msg(p_manager, EVT_ERROR, "Error with SIZ marker: negative image size (%d x %d)\n", l_image->x1 - l_image->x0, l_image->y1 - l_image->y0);
+                return OPJ_FALSE;
+        }
+        /* testcase 2539.pdf.SIGFPE.706.1712 (also 3622.pdf.SIGFPE.706.2916 and 4008.pdf.SIGFPE.706.3345 and maybe more) */
+        if (!(l_cp->tdx * l_cp->tdy)) {
+                opj_event_msg(p_manager, EVT_ERROR, "Error with SIZ marker: invalid tile size (tdx: %d, tdy: %d)\n", l_cp->tdx, l_cp->tdy);
+                return OPJ_FALSE;
+        }
+
+        /* testcase 1610.pdf.SIGSEGV.59c.681 */
+        if (((OPJ_UINT64)l_image->x1) * ((OPJ_UINT64)l_image->y1) != (l_image->x1 * l_image->y1)) {
+                opj_event_msg(p_manager, EVT_ERROR, "Prevent buffer overflow (x1: %d, y1: %d)\n", l_image->x1, l_image->y1);
+                return OPJ_FALSE;
+        }
+
 #ifdef USE_JPWL
         if (l_cp->correct) {
                 /* if JPWL is on, we check whether TX errors have damaged
@@ -1993,10 +2050,17 @@ static opj_bool opj_j2k_read_siz(opj_j2k_t *p_j2k,
                 l_img_comp->sgnd = tmp >> 7;
                 opj_read_bytes(p_header_data,&tmp,1);   /* XRsiz_i */
                 ++p_header_data;
-                l_img_comp->dx = (OPJ_INT32)tmp; /* should be between 1 and 255 */
+                l_img_comp->dx = (OPJ_UINT32)tmp; /* should be between 1 and 255 */
                 opj_read_bytes(p_header_data,&tmp,1);   /* YRsiz_i */
                 ++p_header_data;
-                l_img_comp->dy = (OPJ_INT32)tmp; /* should be between 1 and 255 */
+                l_img_comp->dy = (OPJ_UINT32)tmp; /* should be between 1 and 255 */
+                if( l_img_comp->dx < 1 || l_img_comp->dx > 255 ||
+                    l_img_comp->dy < 1 || l_img_comp->dy > 255 ) {
+                    opj_event_msg(p_manager, EVT_ERROR,
+                                  "Invalid values for comp = %d : dx=%u dy=%u\n (should be between 1 and 255 according the JPEG2000 norm)",
+                                  i, l_img_comp->dx, l_img_comp->dy);
+                    return OPJ_FALSE;
+                }
 
 #ifdef USE_JPWL
                 if (l_cp->correct) {
@@ -2031,16 +2095,24 @@ static opj_bool opj_j2k_read_siz(opj_j2k_t *p_j2k,
         }
 
         /* Compute the number of tiles */
-        l_cp->tw = opj_int_ceildiv(l_image->x1 - l_cp->tx0, l_cp->tdx);
-        l_cp->th = opj_int_ceildiv(l_image->y1 - l_cp->ty0, l_cp->tdy);
+        l_cp->tw = (OPJ_UINT32)opj_int_ceildiv((OPJ_INT32)(l_image->x1 - l_cp->tx0), (OPJ_INT32)l_cp->tdx);
+        l_cp->th = (OPJ_UINT32)opj_int_ceildiv((OPJ_INT32)(l_image->y1 - l_cp->ty0), (OPJ_INT32)l_cp->tdy);
+
+        /* Check that the number of tiles is valid */
+        if (l_cp->tw == 0 || l_cp->th == 0 || l_cp->tw > 65535 / l_cp->th) {
+            opj_event_msg(  p_manager, EVT_ERROR, 
+                            "Invalid number of tiles : %u x %u (maximum fixed by jpeg2000 norm is 65535 tiles)\n",
+                            l_cp->tw, l_cp->th);
+            return OPJ_FALSE;
+        }
         l_nb_tiles = l_cp->tw * l_cp->th;
 
         /* Define the tiles which will be decoded */
         if (p_j2k->m_specific_param.m_decoder.m_discard_tiles) {
                 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;
                 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;
-                p_j2k->m_specific_param.m_decoder.m_end_tile_x = opj_int_ceildiv((p_j2k->m_specific_param.m_decoder.m_end_tile_x - l_cp->tx0), l_cp->tdx);
-                p_j2k->m_specific_param.m_decoder.m_end_tile_y = opj_int_ceildiv((p_j2k->m_specific_param.m_decoder.m_end_tile_y - l_cp->ty0), l_cp->tdy);
+                p_j2k->m_specific_param.m_decoder.m_end_tile_x = (OPJ_UINT32)opj_int_ceildiv((OPJ_INT32)(p_j2k->m_specific_param.m_decoder.m_end_tile_x - l_cp->tx0), (OPJ_INT32)l_cp->tdx);
+                p_j2k->m_specific_param.m_decoder.m_end_tile_y = (OPJ_UINT32)opj_int_ceildiv((OPJ_INT32)(p_j2k->m_specific_param.m_decoder.m_end_tile_y - l_cp->ty0), (OPJ_INT32)l_cp->tdy);
         }
         else {
                 p_j2k->m_specific_param.m_decoder.m_start_tile_x = 0;
@@ -2164,7 +2236,7 @@ static opj_bool opj_j2k_read_siz(opj_j2k_t *p_j2k,
         return OPJ_TRUE;
 }
 
-opj_bool opj_j2k_write_com(     opj_j2k_t *p_j2k,
+OPJ_BOOL opj_j2k_write_com(     opj_j2k_t *p_j2k,
                                                         opj_stream_private_t *p_stream,
                                                         opj_event_mgr_t * p_manager
                             )
@@ -2180,7 +2252,7 @@ opj_bool opj_j2k_write_com(     opj_j2k_t *p_j2k,
         assert(p_manager != 00);
 
         l_comment = p_j2k->m_cp.comment;
-        l_comment_size = strlen(l_comment);
+        l_comment_size = (OPJ_UINT32)strlen(l_comment);
         l_total_com_size = l_comment_size + 6;
 
         if (l_total_com_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
@@ -2223,7 +2295,7 @@ opj_bool opj_j2k_write_com(     opj_j2k_t *p_j2k,
  * @param       p_header_size   the size of the data contained in the COM marker.
  * @param       p_manager               the user event manager.
 */
-static opj_bool opj_j2k_read_com (  opj_j2k_t *p_j2k,
+static OPJ_BOOL opj_j2k_read_com (  opj_j2k_t *p_j2k,
                                     OPJ_BYTE * p_header_data,
                                     OPJ_UINT32 p_header_size,
                                     opj_event_mgr_t * p_manager
@@ -2238,7 +2310,7 @@ static opj_bool opj_j2k_read_com (  opj_j2k_t *p_j2k,
         return OPJ_TRUE;
 }
 
-opj_bool opj_j2k_write_cod(     opj_j2k_t *p_j2k,
+OPJ_BOOL opj_j2k_write_cod(     opj_j2k_t *p_j2k,
                                                         opj_stream_private_t *p_stream,
                                                         opj_event_mgr_t * p_manager )
 {
@@ -2316,7 +2388,7 @@ opj_bool opj_j2k_write_cod(     opj_j2k_t *p_j2k,
  * @param       p_header_size   the size of the data contained in the COD marker.
  * @param       p_manager               the user event manager.
 */
-static opj_bool opj_j2k_read_cod (  opj_j2k_t *p_j2k,
+static OPJ_BOOL opj_j2k_read_cod (  opj_j2k_t *p_j2k,
                                     OPJ_BYTE * p_header_data,
                                     OPJ_UINT32 p_header_size,
                                     opj_event_mgr_t * p_manager
@@ -2401,7 +2473,8 @@ static opj_bool opj_j2k_read_cod (  opj_j2k_t *p_j2k,
         return OPJ_TRUE;
 }
 
-opj_bool opj_j2k_write_coc( opj_j2k_t *p_j2k,
+#if 0
+OPJ_BOOL opj_j2k_write_coc( opj_j2k_t *p_j2k,
                                                 OPJ_UINT32 p_comp_no,
                                                 opj_stream_private_t *p_stream,
                                                 opj_event_mgr_t * p_manager )
@@ -2445,7 +2518,9 @@ opj_bool opj_j2k_write_coc( opj_j2k_t *p_j2k,
 
         return OPJ_TRUE;
 }
+#endif
 
+#if 0
 void opj_j2k_write_coc_in_memory(   opj_j2k_t *p_j2k,
                                                 OPJ_UINT32 p_comp_no,
                                                 OPJ_BYTE * p_data,
@@ -2490,6 +2565,7 @@ void opj_j2k_write_coc_in_memory(   opj_j2k_t *p_j2k,
         opj_j2k_write_SPCod_SPCoc(p_j2k,p_j2k->m_current_tile_number,0,l_current_data,&l_remaining_size,p_manager);
         * p_data_written = l_coc_size;
 }
+#endif
 
 OPJ_UINT32 opj_j2k_get_max_coc_size(opj_j2k_t *p_j2k)
 {
@@ -2519,7 +2595,7 @@ OPJ_UINT32 opj_j2k_get_max_coc_size(opj_j2k_t *p_j2k)
  * @param       p_header_size   the size of the data contained in the COC marker.
  * @param       p_manager               the user event manager.
 */
-static opj_bool opj_j2k_read_coc (  opj_j2k_t *p_j2k,
+static OPJ_BOOL opj_j2k_read_coc (  opj_j2k_t *p_j2k,
                                     OPJ_BYTE * p_header_data,
                                     OPJ_UINT32 p_header_size,
                                     opj_event_mgr_t * p_manager
@@ -2573,7 +2649,7 @@ static opj_bool opj_j2k_read_coc (  opj_j2k_t *p_j2k,
         return OPJ_TRUE;
 }
 
-opj_bool opj_j2k_write_qcd(     opj_j2k_t *p_j2k,
+OPJ_BOOL opj_j2k_write_qcd(     opj_j2k_t *p_j2k,
                                                         opj_stream_private_t *p_stream,
                                                         opj_event_mgr_t * p_manager
                             )
@@ -2636,7 +2712,7 @@ opj_bool opj_j2k_write_qcd(     opj_j2k_t *p_j2k,
  * @param       p_header_size   the size of the data contained in the QCD marker.
  * @param       p_manager               the user event manager.
 */
-static opj_bool opj_j2k_read_qcd (  opj_j2k_t *p_j2k,
+static OPJ_BOOL opj_j2k_read_qcd (  opj_j2k_t *p_j2k,
                                     OPJ_BYTE * p_header_data,
                                     OPJ_UINT32 p_header_size,
                                     opj_event_mgr_t * p_manager
@@ -2663,7 +2739,8 @@ static opj_bool opj_j2k_read_qcd (  opj_j2k_t *p_j2k,
         return OPJ_TRUE;
 }
 
-opj_bool opj_j2k_write_qcc(     opj_j2k_t *p_j2k,
+#if 0
+OPJ_BOOL opj_j2k_write_qcc(     opj_j2k_t *p_j2k,
                                                 OPJ_UINT32 p_comp_no,
                                                 opj_stream_private_t *p_stream,
                                                 opj_event_mgr_t * p_manager
@@ -2676,7 +2753,8 @@ opj_bool opj_j2k_write_qcc(     opj_j2k_t *p_j2k,
         assert(p_manager != 00);
         assert(p_stream != 00);
 
-        l_qcc_size = 6 + opj_j2k_get_SQcd_SQcc_size(p_j2k,p_j2k->m_current_tile_number,p_comp_no);
+        l_qcc_size = 5 + opj_j2k_get_SQcd_SQcc_size(p_j2k,p_j2k->m_current_tile_number,p_comp_no);
+        l_qcc_size += p_j2k->m_private_image->numcomps <= 256 ? 0:1;
         l_remaining_size = l_qcc_size;
 
         if (l_qcc_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
@@ -2700,7 +2778,9 @@ opj_bool opj_j2k_write_qcc(     opj_j2k_t *p_j2k,
 
         return OPJ_TRUE;
 }
+#endif
 
+#if 0
 void opj_j2k_write_qcc_in_memory(   opj_j2k_t *p_j2k,
                                                                 OPJ_UINT32 p_comp_no,
                                                                 OPJ_BYTE * p_data,
@@ -2749,6 +2829,7 @@ void opj_j2k_write_qcc_in_memory(   opj_j2k_t *p_j2k,
 
         *p_data_written = l_qcc_size;
 }
+#endif
 
 OPJ_UINT32 opj_j2k_get_max_qcc_size (opj_j2k_t *p_j2k)
 {
@@ -2762,7 +2843,7 @@ OPJ_UINT32 opj_j2k_get_max_qcc_size (opj_j2k_t *p_j2k)
  * @param       p_header_size   the size of the data contained in the QCC marker.
  * @param       p_manager               the user event manager.
 */
-static opj_bool opj_j2k_read_qcc(   opj_j2k_t *p_j2k,
+static OPJ_BOOL opj_j2k_read_qcc(   opj_j2k_t *p_j2k,
                                     OPJ_BYTE * p_header_data,
                                     OPJ_UINT32 p_header_size,
                                     opj_event_mgr_t * p_manager
@@ -2822,6 +2903,13 @@ static opj_bool opj_j2k_read_qcc(   opj_j2k_t *p_j2k,
         };
 #endif /* USE_JPWL */
 
+        if (l_comp_no >= p_j2k->m_private_image->numcomps) {
+                opj_event_msg(p_manager, EVT_ERROR,
+                              "Invalid component number: %d, regarding the number of components %d\n",
+                              l_comp_no, p_j2k->m_private_image->numcomps);
+                return OPJ_FALSE;
+        }
+
         if (! opj_j2k_read_SQcd_SQcc(p_j2k,l_comp_no,p_header_data,&p_header_size,p_manager)) {
                 opj_event_msg(p_manager, EVT_ERROR, "Error reading QCC marker\n");
                 return OPJ_FALSE;
@@ -2835,7 +2923,7 @@ static opj_bool opj_j2k_read_qcc(   opj_j2k_t *p_j2k,
         return OPJ_TRUE;
 }
 
-opj_bool opj_j2k_write_poc(     opj_j2k_t *p_j2k,
+OPJ_BOOL opj_j2k_write_poc(     opj_j2k_t *p_j2k,
                                                         opj_stream_private_t *p_stream,
                                                         opj_event_mgr_t * p_manager
                             )
@@ -2951,9 +3039,9 @@ void opj_j2k_write_poc_in_memory(   opj_j2k_t *p_j2k,
                 ++l_current_data;
 
                 /* change the value of the max layer according to the actual number of layers in the file, components and resolutions*/
-                l_current_poc->layno1 = opj_int_min(l_current_poc->layno1, l_tcp->numlayers);
-                l_current_poc->resno1 = opj_int_min(l_current_poc->resno1, l_tccp->numresolutions);
-                l_current_poc->compno1 = opj_int_min(l_current_poc->compno1, l_nb_comp);
+                l_current_poc->layno1 = (OPJ_UINT32)opj_int_min((OPJ_INT32)l_current_poc->layno1, (OPJ_INT32)l_tcp->numlayers);
+                l_current_poc->resno1 = (OPJ_UINT32)opj_int_min((OPJ_INT32)l_current_poc->resno1, (OPJ_INT32)l_tccp->numresolutions);
+                l_current_poc->compno1 = (OPJ_UINT32)opj_int_min((OPJ_INT32)l_current_poc->compno1, (OPJ_INT32)l_nb_comp);
 
                 ++l_current_poc;
         }
@@ -3009,7 +3097,7 @@ OPJ_UINT32 opj_j2k_get_specific_header_sizes(opj_j2k_t *p_j2k)
         l_nb_comps = p_j2k->m_private_image->numcomps - 1;
         l_nb_bytes += opj_j2k_get_max_toc_size(p_j2k);
 
-        if (p_j2k->m_cp.m_specific_param.m_enc.m_cinema == 0) {
+        if (!(OPJ_IS_CINEMA(p_j2k->m_cp.rsiz))) {
                 l_coc_bytes = opj_j2k_get_max_coc_size(p_j2k);
                 l_nb_bytes += l_nb_comps * l_coc_bytes;
 
@@ -3032,7 +3120,7 @@ OPJ_UINT32 opj_j2k_get_specific_header_sizes(opj_j2k_t *p_j2k)
  * @param       p_header_size   the size of the data contained in the POC marker.
  * @param       p_manager               the user event manager.
 */
-static opj_bool opj_j2k_read_poc (  opj_j2k_t *p_j2k,
+static OPJ_BOOL opj_j2k_read_poc (  opj_j2k_t *p_j2k,
                                     OPJ_BYTE * p_header_data,
                                     OPJ_UINT32 p_header_size,
                                     opj_event_mgr_t * p_manager
@@ -3076,6 +3164,11 @@ static opj_bool opj_j2k_read_poc (  opj_j2k_t *p_j2k,
         l_old_poc_nb = l_tcp->POC ? l_tcp->numpocs + 1 : 0;
         l_current_poc_nb += l_old_poc_nb;
 
+        if(l_current_poc_nb >= 32)
+          {
+          opj_event_msg(p_manager, EVT_ERROR, "Too many POCs %d\n", l_current_poc_nb);
+          return OPJ_FALSE;
+          }
         assert(l_current_poc_nb < 32);
 
         /* now poc is in use.*/
@@ -3088,6 +3181,8 @@ static opj_bool opj_j2k_read_poc (  opj_j2k_t *p_j2k,
                 opj_read_bytes(p_header_data,&(l_current_poc->compno0),l_comp_room);    /* CSpoc_i */
                 p_header_data+=l_comp_room;
                 opj_read_bytes(p_header_data,&(l_current_poc->layno1),2);                               /* LYEpoc_i */
+                /* make sure layer end is in acceptable bounds */
+                l_current_poc->layno1 = opj_uint_min(l_current_poc->layno1, l_tcp->numlayers);
                 p_header_data+=2;
                 opj_read_bytes(p_header_data,&(l_current_poc->resno1),1);                               /* REpoc_i */
                 ++p_header_data;
@@ -3113,7 +3208,7 @@ static opj_bool opj_j2k_read_poc (  opj_j2k_t *p_j2k,
  * @param       p_header_size   the size of the data contained in the TLM marker.
  * @param       p_manager               the user event manager.
 */
-static opj_bool opj_j2k_read_crg (  opj_j2k_t *p_j2k,
+static OPJ_BOOL opj_j2k_read_crg (  opj_j2k_t *p_j2k,
                                     OPJ_BYTE * p_header_data,
                                     OPJ_UINT32 p_header_size,
                                     opj_event_mgr_t * p_manager
@@ -3153,7 +3248,7 @@ static opj_bool opj_j2k_read_crg (  opj_j2k_t *p_j2k,
  * @param       p_header_size   the size of the data contained in the TLM marker.
  * @param       p_manager               the user event manager.
 */
-static opj_bool opj_j2k_read_tlm (  opj_j2k_t *p_j2k,
+static OPJ_BOOL opj_j2k_read_tlm (  opj_j2k_t *p_j2k,
                                     OPJ_BYTE * p_header_data,
                                     OPJ_UINT32 p_header_size,
                                     opj_event_mgr_t * p_manager
@@ -3209,7 +3304,7 @@ static opj_bool opj_j2k_read_tlm (  opj_j2k_t *p_j2k,
  * @param       p_header_size   the size of the data contained in the TLM marker.
  * @param       p_manager               the user event manager.
 */
-static opj_bool opj_j2k_read_plm (  opj_j2k_t *p_j2k,
+static OPJ_BOOL opj_j2k_read_plm (  opj_j2k_t *p_j2k,
                                     OPJ_BYTE * p_header_data,
                                     OPJ_UINT32 p_header_size,
                                     opj_event_mgr_t * p_manager
@@ -3279,7 +3374,7 @@ static opj_bool opj_j2k_read_plm (  opj_j2k_t *p_j2k,
  * @param       p_header_size   the size of the data contained in the PLT marker.
  * @param       p_manager               the user event manager.
 */
-static opj_bool opj_j2k_read_plt (  opj_j2k_t *p_j2k,
+static OPJ_BOOL opj_j2k_read_plt (  opj_j2k_t *p_j2k,
                                     OPJ_BYTE * p_header_data,
                                     OPJ_UINT32 p_header_size,
                                     opj_event_mgr_t * p_manager
@@ -3324,7 +3419,7 @@ static opj_bool opj_j2k_read_plt (  opj_j2k_t *p_j2k,
 }
 
 #if 0
-opj_bool j2k_read_ppm_v2 (
+OPJ_BOOL j2k_read_ppm_v2 (
                                                 opj_j2k_t *p_j2k,
                                                 OPJ_BYTE * p_header_data,
                                                 OPJ_UINT32 p_header_size,
@@ -3426,7 +3521,7 @@ opj_bool j2k_read_ppm_v2 (
 }
 #endif
 
-opj_bool j2k_read_ppm_v3 (
+OPJ_BOOL j2k_read_ppm_v3 (
                                                 opj_j2k_t *p_j2k,
                                                 OPJ_BYTE * p_header_data,
                                                 OPJ_UINT32 p_header_size,
@@ -3466,6 +3561,17 @@ opj_bool j2k_read_ppm_v3 (
                 p_header_data+=4;
                 p_header_size-=4;
 
+                /* sanity check: how much bytes is left for Ippm */
+                if( p_header_size < l_N_ppm )
+                  {
+                  opj_event_msg(p_manager, EVT_ERROR, "Not enough bytes (%u) to hold Ippm series (%u), Index (%d)\n", p_header_size, l_N_ppm, l_Z_ppm );
+                  opj_free(l_cp->ppm_data);
+                  l_cp->ppm_data = NULL;
+                  l_cp->ppm_buffer = NULL;
+                  l_cp->ppm = 0; /* do not use PPM */
+                  return OPJ_FALSE;
+                  }
+
                 /* First PPM marker: Initialization */
                 l_cp->ppm_len = l_N_ppm;
                 l_cp->ppm_data_read = 0;
@@ -3500,6 +3606,16 @@ opj_bool j2k_read_ppm_v3 (
                                 p_header_data+=4;
                                 p_header_size-=4;
 
+                                /* sanity check: how much bytes is left for Ippm */
+                                if( p_header_size < l_N_ppm )
+                                  {
+                                  opj_event_msg(p_manager, EVT_ERROR, "Not enough bytes (%u) to hold Ippm series (%u), Index (%d)\n", p_header_size, l_N_ppm, l_Z_ppm );
+                                  opj_free(l_cp->ppm_data);
+                                  l_cp->ppm_data = NULL;
+                                  l_cp->ppm_buffer = NULL;
+                                  l_cp->ppm = 0; /* do not use PPM */
+                                  return OPJ_FALSE;
+                                  }
                                 /* Increase the size of ppm_data to add the new Ippm series*/
                                 assert(l_cp->ppm_data == l_cp->ppm_buffer && "We need ppm_data and ppm_buffer to be the same when reallocating");
                                 new_ppm_data = (OPJ_BYTE *) opj_realloc(l_cp->ppm_data, l_cp->ppm_len + l_N_ppm);
@@ -3545,7 +3661,7 @@ opj_bool j2k_read_ppm_v3 (
                 l_remaining_data = p_header_size;
 
                 /* Next Ippm series is a complete series ?*/
-                if (l_remaining_data > l_N_ppm) {
+                if (l_remaining_data >= l_N_ppm) {
                         OPJ_BYTE *new_ppm_data;
                         /* Increase the size of ppm_data to add the new Ippm series*/
                         assert(l_cp->ppm_data == l_cp->ppm_buffer && "We need ppm_data and ppm_buffer to be the same when reallocating");
@@ -3653,7 +3769,7 @@ opj_bool j2k_read_ppm_v3 (
  * @param       p_header_size   the size of the data contained in the PPT marker.
  * @param       p_manager               the user event manager.
 */
-static opj_bool opj_j2k_read_ppt (  opj_j2k_t *p_j2k,
+static OPJ_BOOL opj_j2k_read_ppt (  opj_j2k_t *p_j2k,
                                     OPJ_BYTE * p_header_data,
                                     OPJ_UINT32 p_header_size,
                                     opj_event_mgr_t * p_manager
@@ -3693,6 +3809,7 @@ static opj_bool opj_j2k_read_ppt (  opj_j2k_t *p_j2k,
                 l_tcp->ppt_data_size = 0;
                 l_tcp->ppt_len = p_header_size;
 
+                opj_free(l_tcp->ppt_buffer);
                 l_tcp->ppt_buffer = (OPJ_BYTE *) opj_calloc(l_tcp->ppt_len, sizeof(OPJ_BYTE) );
                 if (l_tcp->ppt_buffer == 00) {
                         opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to read PPT marker\n");
@@ -3728,7 +3845,7 @@ static opj_bool opj_j2k_read_ppt (  opj_j2k_t *p_j2k,
         return OPJ_TRUE;
 }
 
-opj_bool opj_j2k_write_tlm(     opj_j2k_t *p_j2k,
+OPJ_BOOL opj_j2k_write_tlm(     opj_j2k_t *p_j2k,
                                                         opj_stream_private_t *p_stream,
                                                         opj_event_mgr_t * p_manager
                             )
@@ -3782,7 +3899,7 @@ opj_bool opj_j2k_write_tlm(     opj_j2k_t *p_j2k,
         return OPJ_TRUE;
 }
 
-opj_bool opj_j2k_write_sot(     opj_j2k_t *p_j2k,
+OPJ_BOOL opj_j2k_write_sot(     opj_j2k_t *p_j2k,
                                                         OPJ_BYTE * p_data,
                                                         OPJ_UINT32 * p_data_written,
                                                         const opj_stream_private_t *p_stream,
@@ -3816,7 +3933,7 @@ opj_bool opj_j2k_write_sot(     opj_j2k_t *p_j2k,
 #ifdef USE_JPWL
         /* update markers struct */
 /*
-        opj_bool res = j2k_add_marker(p_j2k->cstr_info, J2K_MS_SOT, p_j2k->sot_start, len + 2);
+        OPJ_BOOL res = j2k_add_marker(p_j2k->cstr_info, J2K_MS_SOT, p_j2k->sot_start, len + 2);
 */
   assert( 0 && "TODO" );
 #endif /* USE_JPWL */
@@ -3826,7 +3943,7 @@ opj_bool opj_j2k_write_sot(     opj_j2k_t *p_j2k,
         return OPJ_TRUE;
 }
 
-opj_bool opj_j2k_read_sot ( opj_j2k_t *p_j2k,
+OPJ_BOOL opj_j2k_read_sot ( opj_j2k_t *p_j2k,
                             OPJ_BYTE * p_header_data,
                             OPJ_UINT32 p_header_size,
                             opj_event_mgr_t * p_manager )
@@ -3852,6 +3969,12 @@ opj_bool opj_j2k_read_sot ( opj_j2k_t *p_j2k,
         opj_read_bytes(p_header_data,&(p_j2k->m_current_tile_number),2);                /* Isot */
         p_header_data+=2;
 
+        /* testcase 2.pdf.SIGFPE.706.1112 */
+        if (p_j2k->m_current_tile_number >= l_cp->tw * l_cp->th) {
+                opj_event_msg(p_manager, EVT_ERROR, "Invalid tile number %d\n", p_j2k->m_current_tile_number);
+                return OPJ_FALSE;
+        }
+
         l_tcp = &l_cp->tcps[p_j2k->m_current_tile_number];
         l_tile_x = p_j2k->m_current_tile_number % l_cp->tw;
         l_tile_y = p_j2k->m_current_tile_number / l_cp->tw;
@@ -3949,12 +4072,19 @@ opj_bool opj_j2k_read_sot ( opj_j2k_t *p_j2k,
                                         return OPJ_FALSE;
                                 }
                         }
+                        if( l_current_part >= l_num_parts ) {
+                          /* testcase 451.pdf.SIGSEGV.ce9.3723 */
+                          opj_event_msg(p_manager, EVT_ERROR, "In SOT marker, TPSot (%d) is not valid regards to the current "
+                            "number of tile-part (header) (%d), giving up\n", l_current_part, l_num_parts );
+                          p_j2k->m_specific_param.m_decoder.m_last_tile_part = 1;
+                          return OPJ_FALSE;
+                        }
                         l_tcp->m_nb_tile_parts = l_num_parts;
                 }
 
                 /* If know the number of tile part header we will check if we didn't read the last*/
                 if (l_tcp->m_nb_tile_parts) {
-                        if (l_tcp->m_nb_tile_parts == (l_current_part + 1)) {
+                        if (l_tcp->m_nb_tile_parts == (l_current_part+1)) {
                                 p_j2k->m_specific_param.m_decoder.m_can_decode = 1; /* Process the last tile-part header*/
                         }
                 }
@@ -4023,7 +4153,7 @@ opj_bool opj_j2k_read_sot ( opj_j2k_t *p_j2k,
 
                                         if ( l_current_part >= p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps ){
                                                 opj_tp_index_t *new_tp_index;
-                                                p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps += 10;
+                                                p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps = l_current_part + 1;
                                                 new_tp_index = (opj_tp_index_t *) opj_realloc(
                                                                 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index,
                                                                 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps * sizeof(opj_tp_index_t));
@@ -4072,7 +4202,7 @@ opj_bool opj_j2k_read_sot ( opj_j2k_t *p_j2k,
                 return OPJ_TRUE;
         }
 
-opj_bool opj_j2k_write_sod(     opj_j2k_t *p_j2k,
+OPJ_BOOL opj_j2k_write_sod(     opj_j2k_t *p_j2k,
                                                         opj_tcd_t * p_tile_coder,
                                                         OPJ_BYTE * p_data,
                                                         OPJ_UINT32 * p_data_written,
@@ -4119,7 +4249,7 @@ opj_bool opj_j2k_write_sod(     opj_j2k_t *p_j2k,
                 /* UniPG>> */
 #ifdef USE_JPWL
                 /* update markers struct */
-                /*opj_bool res = j2k_add_marker(p_j2k->cstr_info, J2K_MS_SOD, p_j2k->sod_start, 2);
+                /*OPJ_BOOL res = j2k_add_marker(p_j2k->cstr_info, J2K_MS_SOD, p_j2k->sod_start, 2);
 */
   assert( 0 && "TODO" );
 #endif /* USE_JPWL */
@@ -4146,17 +4276,17 @@ opj_bool opj_j2k_write_sod(     opj_j2k_t *p_j2k,
         return OPJ_TRUE;
 }
 
-opj_bool opj_j2k_read_sod (opj_j2k_t *p_j2k,
+OPJ_BOOL opj_j2k_read_sod (opj_j2k_t *p_j2k,
                            opj_stream_private_t *p_stream,
                                                    opj_event_mgr_t * p_manager
                            )
 {
-        OPJ_UINT32 l_current_read_size;
+        OPJ_SIZE_T l_current_read_size;
         opj_codestream_index_t * l_cstr_index = 00;
         OPJ_BYTE ** l_current_data = 00;
         opj_tcp_t * l_tcp = 00;
         OPJ_UINT32 * l_tile_len = 00;
-        opj_bool l_sot_length_pb_detected = OPJ_FALSE;
+        OPJ_BOOL l_sot_length_pb_detected = OPJ_FALSE;
 
         /* preconditions */
         assert(p_j2k != 00);
@@ -4256,12 +4386,12 @@ opj_bool opj_j2k_read_sod (opj_j2k_t *p_j2k,
                 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_TPHSOT;
         }
 
-        *l_tile_len +=  l_current_read_size;
+        *l_tile_len += (OPJ_UINT32)l_current_read_size;
 
         return OPJ_TRUE;
 }
 
opj_bool opj_j2k_write_rgn(opj_j2k_t *p_j2k,
OPJ_BOOL opj_j2k_write_rgn(opj_j2k_t *p_j2k,
                             OPJ_UINT32 p_tile_no,
                             OPJ_UINT32 p_comp_no,
                             OPJ_UINT32 nb_comps,
@@ -4308,7 +4438,7 @@ opj_bool opj_j2k_read_sod (opj_j2k_t *p_j2k,
         opj_write_bytes(l_current_data, 0,1);                                           /* Srgn */
         ++l_current_data;
 
-        opj_write_bytes(l_current_data, l_tccp->roishift,1);                            /* SPrgn */
+        opj_write_bytes(l_current_data, (OPJ_UINT32)l_tccp->roishift,1);                            /* SPrgn */
         ++l_current_data;
 
         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) {
@@ -4318,7 +4448,7 @@ opj_bool opj_j2k_read_sod (opj_j2k_t *p_j2k,
         return OPJ_TRUE;
 }
 
-opj_bool opj_j2k_write_eoc(     opj_j2k_t *p_j2k,
+OPJ_BOOL opj_j2k_write_eoc(     opj_j2k_t *p_j2k,
                             opj_stream_private_t *p_stream,
                             opj_event_mgr_t * p_manager
                             )
@@ -4334,7 +4464,7 @@ opj_bool opj_j2k_write_eoc(     opj_j2k_t *p_j2k,
 #ifdef USE_JPWL
         /* update markers struct */
         /*
-        opj_bool res = j2k_add_marker(p_j2k->cstr_info, J2K_MS_EOC, p_stream_tell(p_stream) - 2, 2);
+        OPJ_BOOL res = j2k_add_marker(p_j2k->cstr_info, J2K_MS_EOC, p_stream_tell(p_stream) - 2, 2);
 */
 #endif /* USE_JPWL */
 
@@ -4357,7 +4487,7 @@ opj_bool opj_j2k_write_eoc(     opj_j2k_t *p_j2k,
  * @param       p_header_size   the size of the data contained in the POC marker.
  * @param       p_manager               the user event manager.
 */
-static opj_bool opj_j2k_read_rgn (opj_j2k_t *p_j2k,
+static OPJ_BOOL opj_j2k_read_rgn (opj_j2k_t *p_j2k,
                                   OPJ_BYTE * p_header_data,
                                   OPJ_UINT32 p_header_size,
                                   opj_event_mgr_t * p_manager
@@ -4413,6 +4543,14 @@ static opj_bool opj_j2k_read_rgn (opj_j2k_t *p_j2k,
         };
 #endif /* USE_JPWL */
 
+        /* testcase 3635.pdf.asan.77.2930 */
+        if (l_comp_no >= l_nb_comp) {
+                opj_event_msg(p_manager, EVT_ERROR,
+                        "bad component number in RGN (%d when there are only %d)\n",
+                        l_comp_no, l_nb_comp);
+                return OPJ_FALSE;
+        }
+
         opj_read_bytes(p_header_data,(OPJ_UINT32 *) (&(l_tcp->tccps[l_comp_no].roishift)),1);   /* SPrgn */
         ++p_header_data;
 
@@ -4431,7 +4569,7 @@ OPJ_FLOAT32 opj_j2k_get_default_stride (opj_tcp_t * p_tcp)
     return 0;
 }
 
-opj_bool opj_j2k_update_rates(  opj_j2k_t *p_j2k,
+OPJ_BOOL opj_j2k_update_rates(  opj_j2k_t *p_j2k,
                                                             opj_stream_private_t *p_stream,
                                                             opj_event_mgr_t * p_manager )
 {
@@ -4460,7 +4598,7 @@ opj_bool opj_j2k_update_rates(  opj_j2k_t *p_j2k,
 
         l_bits_empty = 8 * l_image->comps->dx * l_image->comps->dy;
         l_size_pixel = l_image->numcomps * l_image->comps->prec;
-        l_sot_remove = ((OPJ_FLOAT32) opj_stream_tell(p_stream)) / (l_cp->th * l_cp->tw);
+        l_sot_remove = (OPJ_FLOAT32) opj_stream_tell(p_stream) / (OPJ_FLOAT32)(l_cp->th * l_cp->tw);
 
         if (l_cp->m_specific_param.m_enc.m_tp_on) {
                 l_tp_stride_func = opj_j2k_get_tp_stride;
@@ -4471,21 +4609,21 @@ opj_bool opj_j2k_update_rates(  opj_j2k_t *p_j2k,
 
         for (i=0;i<l_cp->th;++i) {
                 for (j=0;j<l_cp->tw;++j) {
-                        OPJ_FLOAT32 l_offset = ((*l_tp_stride_func)(l_tcp)) / l_tcp->numlayers;
+                        OPJ_FLOAT32 l_offset = (OPJ_FLOAT32)(*l_tp_stride_func)(l_tcp) / (OPJ_FLOAT32)l_tcp->numlayers;
 
                         /* 4 borders of the tile rescale on the image if necessary */
-                        l_x0 = opj_int_max(l_cp->tx0 + j * l_cp->tdx, l_image->x0);
-                        l_y0 = opj_int_max(l_cp->ty0 + i * l_cp->tdy, l_image->y0);
-                        l_x1 = opj_int_min(l_cp->tx0 + (j + 1) * l_cp->tdx, l_image->x1);
-                        l_y1 = opj_int_min(l_cp->ty0 + (i + 1) * l_cp->tdy, l_image->y1);
+                        l_x0 = opj_int_max((OPJ_INT32)(l_cp->tx0 + j * l_cp->tdx), (OPJ_INT32)l_image->x0);
+                        l_y0 = opj_int_max((OPJ_INT32)(l_cp->ty0 + i * l_cp->tdy), (OPJ_INT32)l_image->y0);
+                        l_x1 = opj_int_min((OPJ_INT32)(l_cp->tx0 + (j + 1) * l_cp->tdx), (OPJ_INT32)l_image->x1);
+                        l_y1 = opj_int_min((OPJ_INT32)(l_cp->ty0 + (i + 1) * l_cp->tdy), (OPJ_INT32)l_image->y1);
 
                         l_rates = l_tcp->rates;
 
                         /* Modification of the RATE >> */
                         if (*l_rates) {
-                                *l_rates =              (( (float) (l_size_pixel * (l_x1 - l_x0) * (l_y1 - l_y0)))
+                                *l_rates =              (( (OPJ_FLOAT32) (l_size_pixel * (OPJ_UINT32)(l_x1 - l_x0) * (OPJ_UINT32)(l_y1 - l_y0)))
                                                                 /
-                                                                ((*l_rates) * l_bits_empty)
+                                                                ((*l_rates) * (OPJ_FLOAT32)l_bits_empty)
                                                                 )
                                                                 -
                                                                 l_offset;
@@ -4495,9 +4633,9 @@ opj_bool opj_j2k_update_rates(  opj_j2k_t *p_j2k,
 
                         for (k = 1; k < l_tcp->numlayers; ++k) {
                                 if (*l_rates) {
-                                        *l_rates =              (( (OPJ_FLOAT32) (l_size_pixel * (l_x1 - l_x0) * (l_y1 - l_y0)))
+                                        *l_rates =              (( (OPJ_FLOAT32) (l_size_pixel * (OPJ_UINT32)(l_x1 - l_x0) * (OPJ_UINT32)(l_y1 - l_y0)))
                                                                         /
-                                                                                ((*l_rates) * l_bits_empty)
+                                                                                ((*l_rates) * (OPJ_FLOAT32)l_bits_empty)
                                                                         )
                                                                         -
                                                                         l_offset;
@@ -4579,7 +4717,7 @@ opj_bool opj_j2k_update_rates(  opj_j2k_t *p_j2k,
                 return OPJ_FALSE;
         }
 
-        if (l_cp->m_specific_param.m_enc.m_cinema) {
+        if (OPJ_IS_CINEMA(l_cp->rsiz)) {
                 p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer =
                                 (OPJ_BYTE *) opj_malloc(5*p_j2k->m_specific_param.m_encoder.m_total_tile_parts);
                 if (! p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer) {
@@ -4593,7 +4731,8 @@ opj_bool opj_j2k_update_rates(  opj_j2k_t *p_j2k,
         return OPJ_TRUE;
 }
 
-opj_bool opj_j2k_read_eoc (     opj_j2k_t *p_j2k,
+#if 0
+OPJ_BOOL opj_j2k_read_eoc (     opj_j2k_t *p_j2k,
                                                         opj_stream_private_t *p_stream,
                                                         opj_event_mgr_t * p_manager )
 {
@@ -4601,7 +4740,7 @@ opj_bool opj_j2k_read_eoc (     opj_j2k_t *p_j2k,
         opj_tcd_t * l_tcd = 00;
         OPJ_UINT32 l_nb_tiles;
         opj_tcp_t * l_tcp = 00;
-        opj_bool l_success;
+        OPJ_BOOL l_success;
 
         /* preconditions */
         assert(p_j2k != 00);
@@ -4641,8 +4780,9 @@ opj_bool opj_j2k_read_eoc (     opj_j2k_t *p_j2k,
         opj_tcd_destroy(l_tcd);
         return OPJ_TRUE;
 }
+#endif
 
-opj_bool opj_j2k_get_end_header(opj_j2k_t *p_j2k,
+OPJ_BOOL opj_j2k_get_end_header(opj_j2k_t *p_j2k,
                                                         struct opj_stream_private *p_stream,
                                                         struct opj_event_mgr * p_manager )
 {
@@ -4656,7 +4796,7 @@ opj_bool opj_j2k_get_end_header(opj_j2k_t *p_j2k,
         return OPJ_TRUE;
 }
 
-opj_bool opj_j2k_write_mct_data_group(  opj_j2k_t *p_j2k,
+OPJ_BOOL opj_j2k_write_mct_data_group(  opj_j2k_t *p_j2k,
                                                                         struct opj_stream_private *p_stream,
                                                                         struct opj_event_mgr * p_manager )
 {
@@ -4704,7 +4844,8 @@ opj_bool opj_j2k_write_mct_data_group(  opj_j2k_t *p_j2k,
         return OPJ_TRUE;
 }
 
-opj_bool opj_j2k_write_image_components(opj_j2k_t *p_j2k,
+#if 0
+OPJ_BOOL opj_j2k_write_all_coc(opj_j2k_t *p_j2k,
                                                                         struct opj_stream_private *p_stream,
                                                                         struct opj_event_mgr * p_manager )
 {
@@ -4715,12 +4856,31 @@ opj_bool opj_j2k_write_image_components(opj_j2k_t *p_j2k,
         assert(p_manager != 00);
         assert(p_stream != 00);
 
-        for (compno = 1; compno < p_j2k->m_private_image->numcomps; ++compno)
+        for (compno = 0; compno < p_j2k->m_private_image->numcomps; ++compno)
         {
                 if (! opj_j2k_write_coc(p_j2k,compno,p_stream, p_manager)) {
                         return OPJ_FALSE;
                 }
+        }
+
+        return OPJ_TRUE;
+}
+#endif
+
+#if 0
+OPJ_BOOL opj_j2k_write_all_qcc(opj_j2k_t *p_j2k,
+                                                                        struct opj_stream_private *p_stream,
+                                                                        struct opj_event_mgr * p_manager )
+{
+        OPJ_UINT32 compno;
 
+        /* preconditions */
+        assert(p_j2k != 00);
+        assert(p_manager != 00);
+        assert(p_stream != 00);
+
+        for (compno = 0; compno < p_j2k->m_private_image->numcomps; ++compno)
+        {
                 if (! opj_j2k_write_qcc(p_j2k,compno,p_stream, p_manager)) {
                         return OPJ_FALSE;
                 }
@@ -4728,8 +4888,10 @@ opj_bool opj_j2k_write_image_components(opj_j2k_t *p_j2k,
 
         return OPJ_TRUE;
 }
+#endif
+
 
-opj_bool opj_j2k_write_regions( opj_j2k_t *p_j2k,
+OPJ_BOOL opj_j2k_write_regions( opj_j2k_t *p_j2k,
                                                         struct opj_stream_private *p_stream,
                                                         struct opj_event_mgr * p_manager )
 {
@@ -4757,7 +4919,7 @@ opj_bool opj_j2k_write_regions( opj_j2k_t *p_j2k,
         return OPJ_TRUE;
 }
 
-opj_bool opj_j2k_write_epc(     opj_j2k_t *p_j2k,
+OPJ_BOOL opj_j2k_write_epc(     opj_j2k_t *p_j2k,
                                                 struct opj_stream_private *p_stream,
                                                 struct opj_event_mgr * p_manager )
 {
@@ -4770,12 +4932,12 @@ opj_bool opj_j2k_write_epc(     opj_j2k_t *p_j2k,
 
         l_cstr_index = p_j2k->cstr_index;
         if (l_cstr_index) {
-                l_cstr_index->codestream_size = opj_stream_tell(p_stream);
+                l_cstr_index->codestream_size = (OPJ_UINT64)opj_stream_tell(p_stream);
                 /* UniPG>> */
                 /* The following adjustment is done to adjust the codestream size */
                 /* if SOD is not at 0 in the buffer. Useful in case of JP2, where */
                 /* the first bunch of bytes is not in the codestream              */
-                l_cstr_index->codestream_size -= l_cstr_index->main_head_start;
+                l_cstr_index->codestream_size -= (OPJ_UINT64)l_cstr_index->main_head_start;
                 /* <<UniPG */
         }
 
@@ -4795,7 +4957,7 @@ opj_bool opj_j2k_write_epc(     opj_j2k_t *p_j2k,
         return OPJ_TRUE;
 }
 
-opj_bool opj_j2k_read_unk (     opj_j2k_t *p_j2k,
+OPJ_BOOL opj_j2k_read_unk (     opj_j2k_t *p_j2k,
                                                         opj_stream_private_t *p_stream,
                                                         OPJ_UINT32 *output_marker,
                                                         opj_event_mgr_t * p_manager
@@ -4836,7 +4998,7 @@ opj_bool opj_j2k_read_unk (     opj_j2k_t *p_j2k,
                                         /* Add the marker to the codestream index*/
                                         if (l_marker_handler->id != J2K_MS_SOT)
                                         {
-                                                opj_bool res = opj_j2k_add_mhmarker(p_j2k->cstr_index, J2K_MS_UNK,
+                                                OPJ_BOOL res = opj_j2k_add_mhmarker(p_j2k->cstr_index, J2K_MS_UNK,
                                                                 (OPJ_UINT32) opj_stream_tell(p_stream) - l_size_unk,
                                                                 l_size_unk);
                                                 if (res == OPJ_FALSE) {
@@ -4857,7 +5019,7 @@ opj_bool opj_j2k_read_unk (     opj_j2k_t *p_j2k,
         return OPJ_TRUE;
 }
 
-opj_bool opj_j2k_write_mct_record(      opj_j2k_t *p_j2k,
+OPJ_BOOL opj_j2k_write_mct_record(      opj_j2k_t *p_j2k,
                                                                 opj_mct_data_t * p_mct_record,
                                                                 struct opj_stream_private *p_stream,
                                                                 struct opj_event_mgr * p_manager )
@@ -4923,7 +5085,7 @@ opj_bool opj_j2k_write_mct_record(      opj_j2k_t *p_j2k,
  * @param       p_header_size   the size of the data contained in the MCT marker.
  * @param       p_manager               the user event manager.
 */
-static opj_bool opj_j2k_read_mct (      opj_j2k_t *p_j2k,
+static OPJ_BOOL opj_j2k_read_mct (      opj_j2k_t *p_j2k,
                                                                     OPJ_BYTE * p_header_data,
                                                                     OPJ_UINT32 p_header_size,
                                                                     opj_event_mgr_t * p_manager
@@ -5029,7 +5191,7 @@ static opj_bool opj_j2k_read_mct (      opj_j2k_t *p_j2k,
         return OPJ_TRUE;
 }
 
-opj_bool opj_j2k_write_mcc_record(      opj_j2k_t *p_j2k,
+OPJ_BOOL opj_j2k_write_mcc_record(      opj_j2k_t *p_j2k,
                                                                 struct opj_simple_mcc_decorrelation_data * p_mcc_record,
                                                                 struct opj_stream_private *p_stream,
                                                                 struct opj_event_mgr * p_manager )
@@ -5132,7 +5294,7 @@ opj_bool opj_j2k_write_mcc_record(      opj_j2k_t *p_j2k,
         return OPJ_TRUE;
 }
 
-opj_bool opj_j2k_read_mcc (     opj_j2k_t *p_j2k,
+OPJ_BOOL opj_j2k_read_mcc (     opj_j2k_t *p_j2k,
                                                 OPJ_BYTE * p_header_data,
                                                 OPJ_UINT32 p_header_size,
                                                 opj_event_mgr_t * p_manager )
@@ -5347,7 +5509,7 @@ opj_bool opj_j2k_read_mcc (     opj_j2k_t *p_j2k,
         return OPJ_TRUE;
 }
 
-opj_bool opj_j2k_write_mco(     opj_j2k_t *p_j2k,
+OPJ_BOOL opj_j2k_write_mco(     opj_j2k_t *p_j2k,
                                                 struct opj_stream_private *p_stream,
                                                 struct opj_event_mgr * p_manager
                                   )
@@ -5413,7 +5575,7 @@ opj_bool opj_j2k_write_mco(     opj_j2k_t *p_j2k,
  * @param       p_header_size   the size of the data contained in the MCO marker.
  * @param       p_manager               the user event manager.
 */
-static opj_bool opj_j2k_read_mco (      opj_j2k_t *p_j2k,
+static OPJ_BOOL opj_j2k_read_mco (      opj_j2k_t *p_j2k,
                                                                     OPJ_BYTE * p_header_data,
                                                                     OPJ_UINT32 p_header_size,
                                                                     opj_event_mgr_t * p_manager
@@ -5477,7 +5639,7 @@ static opj_bool opj_j2k_read_mco (      opj_j2k_t *p_j2k,
         return OPJ_TRUE;
 }
 
-opj_bool opj_j2k_add_mct(opj_tcp_t * p_tcp, opj_image_t * p_image, OPJ_UINT32 p_index)
+OPJ_BOOL opj_j2k_add_mct(opj_tcp_t * p_tcp, opj_image_t * p_image, OPJ_UINT32 p_index)
 {
         OPJ_UINT32 i;
         opj_simple_mcc_decorrelation_data_t * l_mcc_record;
@@ -5517,7 +5679,7 @@ opj_bool opj_j2k_add_mct(opj_tcp_t * p_tcp, opj_image_t * p_image, OPJ_UINT32 p_
                 }
 
                 l_nb_elem = p_image->numcomps * p_image->numcomps;
-                l_mct_size = l_nb_elem * sizeof(OPJ_FLOAT32);
+                l_mct_size = l_nb_elem * (OPJ_UINT32)sizeof(OPJ_FLOAT32);
                 p_tcp->m_mct_decoding_matrix = (OPJ_FLOAT32*)opj_malloc(l_mct_size);
 
                 if (! p_tcp->m_mct_decoding_matrix ) {
@@ -5536,7 +5698,7 @@ opj_bool opj_j2k_add_mct(opj_tcp_t * p_tcp, opj_image_t * p_image, OPJ_UINT32 p_
                 }
 
                 l_nb_elem = p_image->numcomps;
-                l_offset_size = l_nb_elem * sizeof(OPJ_UINT32);
+                l_offset_size = l_nb_elem * (OPJ_UINT32)sizeof(OPJ_UINT32);
                 l_offset_data = (OPJ_UINT32*)opj_malloc(l_offset_size);
 
                 if (! l_offset_data ) {
@@ -5549,7 +5711,7 @@ opj_bool opj_j2k_add_mct(opj_tcp_t * p_tcp, opj_image_t * p_image, OPJ_UINT32 p_
                 l_current_offset_data = l_offset_data;
 
                 for (i=0;i<p_image->numcomps;++i) {
-                        l_tccp->m_dc_level_shift = *(l_current_offset_data++);
+                        l_tccp->m_dc_level_shift = (OPJ_INT32)*(l_current_offset_data++);
                         ++l_tccp;
                 }
 
@@ -5559,7 +5721,7 @@ opj_bool opj_j2k_add_mct(opj_tcp_t * p_tcp, opj_image_t * p_image, OPJ_UINT32 p_
         return OPJ_TRUE;
 }
 
-opj_bool opj_j2k_write_cbd( opj_j2k_t *p_j2k,
+OPJ_BOOL opj_j2k_write_cbd( opj_j2k_t *p_j2k,
                                                 struct opj_stream_private *p_stream,
                                                 struct opj_event_mgr * p_manager )
 {
@@ -5624,7 +5786,7 @@ opj_bool opj_j2k_write_cbd( opj_j2k_t *p_j2k,
  * @param       p_header_size   the size of the data contained in the CBD marker.
  * @param       p_manager               the user event manager.
 */
-static opj_bool opj_j2k_read_cbd (      opj_j2k_t *p_j2k,
+static OPJ_BOOL opj_j2k_read_cbd (      opj_j2k_t *p_j2k,
                                                                 OPJ_BYTE * p_header_data,
                                                                 OPJ_UINT32 p_header_size,
                                                                 opj_event_mgr_t * p_manager
@@ -5726,6 +5888,223 @@ opj_j2k_t* opj_j2k_create_compress(void)
         return l_j2k;
 }
 
+int opj_j2k_initialise_4K_poc(opj_poc_t *POC, int numres){
+    POC[0].tile  = 1;
+    POC[0].resno0  = 0;
+    POC[0].compno0 = 0;
+    POC[0].layno1  = 1;
+    POC[0].resno1  = (OPJ_UINT32)(numres-1);
+    POC[0].compno1 = 3;
+    POC[0].prg1 = OPJ_CPRL;
+    POC[1].tile  = 1;
+    POC[1].resno0  = (OPJ_UINT32)(numres-1);
+    POC[1].compno0 = 0;
+    POC[1].layno1  = 1;
+    POC[1].resno1  = (OPJ_UINT32)numres;
+    POC[1].compno1 = 3;
+    POC[1].prg1 = OPJ_CPRL;
+    return 2;
+}
+
+void opj_j2k_set_cinema_parameters(opj_cparameters_t *parameters, opj_image_t *image, opj_event_mgr_t *p_manager)
+{
+    /* Configure cinema parameters */
+    int i;
+
+    /* No tiling */
+    parameters->tile_size_on = OPJ_FALSE;
+    parameters->cp_tdx=1;
+    parameters->cp_tdy=1;
+
+    /* One tile part for each component */
+    parameters->tp_flag = 'C';
+    parameters->tp_on = 1;
+
+    /* Tile and Image shall be at (0,0) */
+    parameters->cp_tx0 = 0;
+    parameters->cp_ty0 = 0;
+    parameters->image_offset_x0 = 0;
+    parameters->image_offset_y0 = 0;
+
+    /* Codeblock size= 32*32 */
+    parameters->cblockw_init = 32;
+    parameters->cblockh_init = 32;
+
+    /* Codeblock style: no mode switch enabled */
+    parameters->mode = 0;
+
+    /* No ROI */
+    parameters->roi_compno = -1;
+
+    /* No subsampling */
+    parameters->subsampling_dx = 1;
+    parameters->subsampling_dy = 1;
+
+    /* 9-7 transform */
+    parameters->irreversible = 1;
+
+    /* Number of layers */
+    if (parameters->tcp_numlayers > 1){
+        opj_event_msg(p_manager, EVT_WARNING,
+                "JPEG 2000 Profile-3 and 4 (2k/4k dc profile) requires:\n"
+                "1 single quality layer"
+                "-> Number of layers forced to 1 (rather than %d)\n"
+                "-> Rate of the last layer (%3.1f) will be used",
+                parameters->tcp_numlayers, parameters->tcp_rates[parameters->tcp_numlayers-1]);
+        parameters->tcp_rates[0] = parameters->tcp_rates[parameters->tcp_numlayers-1];
+        parameters->tcp_numlayers = 1;
+    }
+
+    /* Resolution levels */
+    switch (parameters->rsiz){
+    case OPJ_PROFILE_CINEMA_2K:
+        if(parameters->numresolution > 6){
+            opj_event_msg(p_manager, EVT_WARNING,
+                    "JPEG 2000 Profile-3 (2k dc profile) requires:\n"
+                    "Number of decomposition levels <= 5\n"
+                    "-> Number of decomposition levels forced to 5 (rather than %d)\n",
+                    parameters->numresolution+1);
+            parameters->numresolution = 6;
+        }
+        break;
+    case OPJ_PROFILE_CINEMA_4K:
+        if(parameters->numresolution < 2){
+            opj_event_msg(p_manager, EVT_WARNING,
+                    "JPEG 2000 Profile-4 (4k dc profile) requires:\n"
+                    "Number of decomposition levels >= 1 && <= 6\n"
+                    "-> Number of decomposition levels forced to 1 (rather than %d)\n",
+                    parameters->numresolution+1);
+            parameters->numresolution = 1;
+        }else if(parameters->numresolution > 7){
+            opj_event_msg(p_manager, EVT_WARNING,
+                    "JPEG 2000 Profile-4 (4k dc profile) requires:\n"
+                    "Number of decomposition levels >= 1 && <= 6\n"
+                    "-> Number of decomposition levels forced to 6 (rather than %d)\n",
+                    parameters->numresolution+1);
+            parameters->numresolution = 7;
+        }
+        break;
+    default :
+        break;
+    }
+
+    /* Precincts */
+    parameters->csty |= 0x01;
+    parameters->res_spec = parameters->numresolution-1;
+    for (i = 0; i<parameters->res_spec; i++) {
+        parameters->prcw_init[i] = 256;
+        parameters->prch_init[i] = 256;
+    }
+
+    /* The progression order shall be CPRL */
+    parameters->prog_order = OPJ_CPRL;
+
+    /* Progression order changes for 4K, disallowed for 2K */
+    if (parameters->rsiz == OPJ_PROFILE_CINEMA_4K) {
+        parameters->numpocs = (OPJ_UINT32)opj_j2k_initialise_4K_poc(parameters->POC,parameters->numresolution);
+    } else {
+        parameters->numpocs = 0;
+    }
+
+    /* Limited bit-rate */
+    parameters->cp_disto_alloc = 1;
+    if (parameters->max_cs_size <= 0) {
+        /* No rate has been introduced, 24 fps is assumed */
+        parameters->max_cs_size = OPJ_CINEMA_24_CS;
+        opj_event_msg(p_manager, EVT_WARNING,
+                      "JPEG 2000 Profile-3 and 4 (2k/4k dc profile) requires:\n"
+                      "Maximum 1302083 compressed bytes @ 24fps\n"
+                      "As no rate has been given, this limit will be used.\n");
+    } else if (parameters->max_cs_size > OPJ_CINEMA_24_CS) {
+        opj_event_msg(p_manager, EVT_WARNING,
+                      "JPEG 2000 Profile-3 and 4 (2k/4k dc profile) requires:\n"
+                      "Maximum 1302083 compressed bytes @ 24fps\n"
+                      "-> Specified rate exceeds this limit. Rate will be forced to 1302083 bytes.\n");
+        parameters->max_cs_size = OPJ_CINEMA_24_CS;
+    }
+
+    if (parameters->max_comp_size <= 0) {
+        /* No rate has been introduced, 24 fps is assumed */
+        parameters->max_comp_size = OPJ_CINEMA_24_COMP;
+        opj_event_msg(p_manager, EVT_WARNING,
+                      "JPEG 2000 Profile-3 and 4 (2k/4k dc profile) requires:\n"
+                      "Maximum 1041666 compressed bytes @ 24fps\n"
+                      "As no rate has been given, this limit will be used.\n");
+    } else if (parameters->max_comp_size > OPJ_CINEMA_24_COMP) {
+        opj_event_msg(p_manager, EVT_WARNING,
+                      "JPEG 2000 Profile-3 and 4 (2k/4k dc profile) requires:\n"
+                      "Maximum 1041666 compressed bytes @ 24fps\n"
+                      "-> Specified rate exceeds this limit. Rate will be forced to 1041666 bytes.\n");
+        parameters->max_comp_size = OPJ_CINEMA_24_COMP;
+    }
+
+    parameters->tcp_rates[0] = (OPJ_FLOAT32) (image->numcomps * image->comps[0].w * image->comps[0].h * image->comps[0].prec)/
+            (OPJ_FLOAT32)(parameters->max_cs_size * 8 * image->comps[0].dx * image->comps[0].dy);
+
+}
+
+OPJ_BOOL opj_j2k_is_cinema_compliant(opj_image_t *image, OPJ_UINT16 rsiz, opj_event_mgr_t *p_manager)
+{
+    OPJ_UINT32 i;
+
+    /* Number of components */
+    if (image->numcomps != 3){
+        opj_event_msg(p_manager, EVT_WARNING,
+                "JPEG 2000 Profile-3 (2k dc profile) requires:\n"
+                "3 components"
+                "-> Number of components of input image (%d) is not compliant\n"
+                "-> Non-profile-3 codestream will be generated\n",
+                image->numcomps);
+        return OPJ_FALSE;
+    }
+
+    /* Bitdepth */
+    for (i = 0; i < image->numcomps; i++) {
+        if ((image->comps[i].bpp != 12) | (image->comps[i].sgnd)){
+            char signed_str[] = "signed";
+            char unsigned_str[] = "unsigned";
+            char *tmp_str = image->comps[i].sgnd?signed_str:unsigned_str;
+            opj_event_msg(p_manager, EVT_WARNING,
+                    "JPEG 2000 Profile-3 (2k dc profile) requires:\n"
+                    "Precision of each component shall be 12 bits unsigned"
+                    "-> At least component %d of input image (%d bits, %s) is not compliant\n"
+                    "-> Non-profile-3 codestream will be generated\n",
+                    i,image->comps[i].bpp, tmp_str);
+            return OPJ_FALSE;
+        }
+    }
+
+    /* Image size */
+    switch (rsiz){
+    case OPJ_PROFILE_CINEMA_2K:
+        if (((image->comps[0].w > 2048) | (image->comps[0].h > 1080))){
+            opj_event_msg(p_manager, EVT_WARNING,
+                    "JPEG 2000 Profile-3 (2k dc profile) requires:\n"
+                    "width <= 2048 and height <= 1080\n"
+                    "-> Input image size %d x %d is not compliant\n"
+                    "-> Non-profile-3 codestream will be generated\n",
+                    image->comps[0].w,image->comps[0].h);
+            return OPJ_FALSE;
+        }
+        break;
+    case OPJ_PROFILE_CINEMA_4K:
+        if (((image->comps[0].w > 4096) | (image->comps[0].h > 2160))){
+            opj_event_msg(p_manager, EVT_WARNING,
+                    "JPEG 2000 Profile-4 (4k dc profile) requires:\n"
+                    "width <= 4096 and height <= 2160\n"
+                    "-> Image size %d x %d is not compliant\n"
+                    "-> Non-profile-4 codestream will be generated\n",
+                    image->comps[0].w,image->comps[0].h);
+            return OPJ_FALSE;
+        }
+        break;
+    default :
+        break;
+    }
+
+    return OPJ_TRUE;
+}
+
 void opj_j2k_setup_encoder(     opj_j2k_t *p_j2k,
                                                     opj_cparameters_t *parameters,
                                                     opj_image_t *image,
@@ -5745,30 +6124,135 @@ void opj_j2k_setup_encoder(     opj_j2k_t *p_j2k,
         cp->tw = 1;
         cp->th = 1;
 
+        /* FIXME ADE: to be removed once deprecated cp_cinema and cp_rsiz have been removed */
+        if (parameters->rsiz == OPJ_PROFILE_NONE) { /* consider deprecated fields only if RSIZ has not been set */
+            switch (parameters->cp_cinema){
+            case OPJ_CINEMA2K_24:
+                parameters->rsiz = OPJ_PROFILE_CINEMA_2K;
+                parameters->max_cs_size = OPJ_CINEMA_24_CS;
+                parameters->max_comp_size = OPJ_CINEMA_24_COMP;
+                break;
+            case OPJ_CINEMA2K_48:
+                parameters->rsiz = OPJ_PROFILE_CINEMA_2K;
+                parameters->max_cs_size = OPJ_CINEMA_48_CS;
+                parameters->max_comp_size = OPJ_CINEMA_48_COMP;
+                break;
+            case OPJ_CINEMA4K_24:
+                parameters->rsiz = OPJ_PROFILE_CINEMA_4K;
+                parameters->max_cs_size = OPJ_CINEMA_24_CS;
+                parameters->max_comp_size = OPJ_CINEMA_24_COMP;
+                break;
+            case OPJ_OFF:
+            default:
+                break;
+            }
+            switch (parameters->cp_rsiz){
+            case OPJ_CINEMA2K:
+                parameters->rsiz = OPJ_PROFILE_CINEMA_2K;
+                break;
+            case OPJ_CINEMA4K:
+                parameters->rsiz = OPJ_PROFILE_CINEMA_4K;
+                break;
+            case OPJ_MCT:
+                parameters->rsiz = OPJ_PROFILE_PART2 | OPJ_EXTENSION_MCT;
+            case OPJ_STD_RSIZ:
+            default:
+                break;
+            }
+        }
+
+        /* see if max_codestream_size does limit input rate */
+        if (parameters->max_cs_size <= 0) {
+            if (parameters->tcp_rates[parameters->tcp_numlayers-1] > 0) {
+                OPJ_FLOAT32 temp_size;
+                temp_size =(OPJ_FLOAT32)(image->numcomps * image->comps[0].w * image->comps[0].h * image->comps[0].prec)/
+                        (parameters->tcp_rates[parameters->tcp_numlayers-1] * 8 * (OPJ_FLOAT32)image->comps[0].dx * (OPJ_FLOAT32)image->comps[0].dy);
+                parameters->max_cs_size = (int) floor(temp_size);
+            } else {
+                parameters->max_cs_size = 0;
+            }
+        } else {
+            OPJ_FLOAT32 temp_rate;
+            OPJ_BOOL cap = OPJ_FALSE;
+            temp_rate = (OPJ_FLOAT32) (image->numcomps * image->comps[0].w * image->comps[0].h * image->comps[0].prec)/
+                    (OPJ_FLOAT32)(parameters->max_cs_size * 8 * image->comps[0].dx * image->comps[0].dy);
+            for (i = 0; i < (OPJ_UINT32) parameters->tcp_numlayers; i++) {
+                if (parameters->tcp_rates[i] < temp_rate) {
+                    parameters->tcp_rates[i] = temp_rate;
+                    cap = OPJ_TRUE;
+                }
+            }
+            if (cap) {
+                opj_event_msg(p_manager, EVT_WARNING,
+                        "The desired maximum codestream size has limited\n"
+                        "at least one of the desired quality layers\n");
+            }
+        }
+
+        /* Manage profiles and applications and set RSIZ */
+        /* set cinema parameters if required */
+        if (OPJ_IS_CINEMA(parameters->rsiz)){
+            if ((parameters->rsiz == OPJ_PROFILE_CINEMA_S2K)
+                    || (parameters->rsiz == OPJ_PROFILE_CINEMA_S4K)){
+                opj_event_msg(p_manager, EVT_WARNING,
+                        "JPEG 2000 Scalable Digital Cinema profiles not yet supported\n");
+                parameters->rsiz = OPJ_PROFILE_NONE;
+            } else {
+                opj_j2k_set_cinema_parameters(parameters,image,p_manager);
+                if (!opj_j2k_is_cinema_compliant(image,parameters->rsiz,p_manager)) {
+                    parameters->rsiz = OPJ_PROFILE_NONE;
+                }
+            }
+        } else if (OPJ_IS_STORAGE(parameters->rsiz)) {
+            opj_event_msg(p_manager, EVT_WARNING,
+                    "JPEG 2000 Long Term Storage profile not yet supported\n");
+            parameters->rsiz = OPJ_PROFILE_NONE;
+        } else if (OPJ_IS_BROADCAST(parameters->rsiz)) {
+            opj_event_msg(p_manager, EVT_WARNING,
+                    "JPEG 2000 Broadcast profiles not yet supported\n");
+            parameters->rsiz = OPJ_PROFILE_NONE;
+        } else if (OPJ_IS_IMF(parameters->rsiz)) {
+            opj_event_msg(p_manager, EVT_WARNING,
+                    "JPEG 2000 IMF profiles not yet supported\n");
+            parameters->rsiz = OPJ_PROFILE_NONE;
+        } else if (OPJ_IS_PART2(parameters->rsiz)) {
+            if (parameters->rsiz == ((OPJ_PROFILE_PART2) | (OPJ_EXTENSION_NONE))) {
+                opj_event_msg(p_manager, EVT_WARNING,
+                              "JPEG 2000 Part-2 profile defined\n"
+                              "but no Part-2 extension enabled.\n"
+                              "Profile set to NONE.\n");
+                parameters->rsiz = OPJ_PROFILE_NONE;
+            } else if (parameters->rsiz != ((OPJ_PROFILE_PART2) | (OPJ_EXTENSION_MCT))) {
+                opj_event_msg(p_manager, EVT_WARNING,
+                              "Unsupported Part-2 extension enabled\n"
+                              "Profile set to NONE.\n");
+                parameters->rsiz = OPJ_PROFILE_NONE;
+            }
+        }
+
         /*
         copy user encoding parameters
         */
-        cp->m_specific_param.m_enc.m_cinema = parameters->cp_cinema;
-        cp->m_specific_param.m_enc.m_max_comp_size =    parameters->max_comp_size;
-        cp->rsiz   = parameters->cp_rsiz;
-        cp->m_specific_param.m_enc.m_disto_alloc = parameters->cp_disto_alloc;
-        cp->m_specific_param.m_enc.m_fixed_alloc = parameters->cp_fixed_alloc;
-        cp->m_specific_param.m_enc.m_fixed_quality = parameters->cp_fixed_quality;
+        cp->m_specific_param.m_enc.m_max_comp_size = (OPJ_UINT32)parameters->max_comp_size;
+        cp->rsiz = parameters->rsiz;
+        cp->m_specific_param.m_enc.m_disto_alloc = (OPJ_UINT32)parameters->cp_disto_alloc & 1u;
+        cp->m_specific_param.m_enc.m_fixed_alloc = (OPJ_UINT32)parameters->cp_fixed_alloc & 1u;
+        cp->m_specific_param.m_enc.m_fixed_quality = (OPJ_UINT32)parameters->cp_fixed_quality & 1u;
 
         /* mod fixed_quality */
-        if (parameters->cp_matrice) {
-                size_t array_size = parameters->tcp_numlayers * parameters->numresolution * 3 * sizeof(OPJ_INT32);
+        if (parameters->cp_fixed_alloc && parameters->cp_matrice) {
+                size_t array_size = (size_t)parameters->tcp_numlayers * (size_t)parameters->numresolution * 3 * sizeof(OPJ_INT32);
                 cp->m_specific_param.m_enc.m_matrice = (OPJ_INT32 *) opj_malloc(array_size);
                 memcpy(cp->m_specific_param.m_enc.m_matrice, parameters->cp_matrice, array_size);
         }
 
         /* tiles */
-        cp->tdx = parameters->cp_tdx;
-        cp->tdy = parameters->cp_tdy;
+        cp->tdx = (OPJ_UINT32)parameters->cp_tdx;
+        cp->tdy = (OPJ_UINT32)parameters->cp_tdy;
 
         /* tile offset */
-        cp->tx0 = parameters->cp_tx0;
-        cp->ty0 = parameters->cp_ty0;
+        cp->tx0 = (OPJ_UINT32)parameters->cp_tx0;
+        cp->ty0 = (OPJ_UINT32)parameters->cp_ty0;
 
         /* comment string */
         if(parameters->cp_comment) {
@@ -5783,15 +6267,15 @@ void opj_j2k_setup_encoder(     opj_j2k_t *p_j2k,
         */
 
         if (parameters->tile_size_on) {
-                cp->tw = opj_int_ceildiv(image->x1 - cp->tx0, cp->tdx);
-                cp->th = opj_int_ceildiv(image->y1 - cp->ty0, cp->tdy);
+                cp->tw = (OPJ_UINT32)opj_int_ceildiv((OPJ_INT32)(image->x1 - cp->tx0), (OPJ_INT32)cp->tdx);
+                cp->th = (OPJ_UINT32)opj_int_ceildiv((OPJ_INT32)(image->y1 - cp->ty0), (OPJ_INT32)cp->tdy);
         } else {
                 cp->tdx = image->x1 - cp->tx0;
                 cp->tdy = image->y1 - cp->ty0;
         }
 
         if (parameters->tp_on) {
-                cp->m_specific_param.m_enc.m_tp_flag = parameters->tp_flag;
+                cp->m_specific_param.m_enc.m_tp_flag = (OPJ_BYTE)parameters->tp_flag;
                 cp->m_specific_param.m_enc.m_tp_on = 1;
         }
 
@@ -5856,16 +6340,16 @@ void opj_j2k_setup_encoder(     opj_j2k_t *p_j2k,
         cp->tcps = (opj_tcp_t*) opj_calloc(cp->tw * cp->th, sizeof(opj_tcp_t));
         if (parameters->numpocs) {
                 /* initialisation of POC */
-                opj_j2k_check_poc_val(parameters->POC,parameters->numpocs, parameters->numresolution, image->numcomps, parameters->tcp_numlayers, p_manager);
+                opj_j2k_check_poc_val(parameters->POC,parameters->numpocs, (OPJ_UINT32)parameters->numresolution, image->numcomps, (OPJ_UINT32)parameters->tcp_numlayers, p_manager);
                 /* TODO MSD use the return value*/
         }
 
         for (tileno = 0; tileno < cp->tw * cp->th; tileno++) {
                 opj_tcp_t *tcp = &cp->tcps[tileno];
-                tcp->numlayers = parameters->tcp_numlayers;
+                tcp->numlayers = (OPJ_UINT32)parameters->tcp_numlayers;
 
                 for (j = 0; j < tcp->numlayers; j++) {
-                        if(cp->m_specific_param.m_enc.m_cinema){
+                        if(OPJ_IS_CINEMA(cp->rsiz)){
                                 if (cp->m_specific_param.m_enc.m_fixed_quality) {
                                         tcp->distoratio[j] = parameters->tcp_distoratio[j];
                                 }
@@ -5879,9 +6363,9 @@ void opj_j2k_setup_encoder(     opj_j2k_t *p_j2k,
                         }
                 }
 
-                tcp->csty = parameters->csty;
+                tcp->csty = (OPJ_UINT32)parameters->csty;
                 tcp->prg = parameters->prog_order;
-                tcp->mct = parameters->tcp_mct;
+                tcp->mct = (OPJ_UINT32)parameters->tcp_mct;
 
                 numpocs_tile = 0;
                 tcp->POC = 0;
@@ -5914,7 +6398,7 @@ void opj_j2k_setup_encoder(     opj_j2k_t *p_j2k,
 
                 if (parameters->mct_data) {
                       
-                    OPJ_UINT32 lMctSize = image->numcomps * image->numcomps * sizeof(OPJ_FLOAT32);
+                    OPJ_UINT32 lMctSize = image->numcomps * image->numcomps * (OPJ_UINT32)sizeof(OPJ_FLOAT32);
                     OPJ_FLOAT32 * lTmpBuf = (OPJ_FLOAT32*)opj_malloc(lMctSize);
                     OPJ_INT32 * l_dc_shift = (OPJ_INT32 *) ((OPJ_BYTE *) parameters->mct_data + lMctSize);
 
@@ -5954,10 +6438,10 @@ void opj_j2k_setup_encoder(     opj_j2k_t *p_j2k,
                         opj_tccp_t *tccp = &tcp->tccps[i];
 
                         tccp->csty = parameters->csty & 0x01;   /* 0 => one precinct || 1 => custom precinct  */
-                        tccp->numresolutions = parameters->numresolution;
-                        tccp->cblkw = opj_int_floorlog2(parameters->cblockw_init);
-                        tccp->cblkh = opj_int_floorlog2(parameters->cblockh_init);
-                        tccp->cblksty = parameters->mode;
+                        tccp->numresolutions = (OPJ_UINT32)parameters->numresolution;
+                        tccp->cblkw = (OPJ_UINT32)opj_int_floorlog2(parameters->cblockw_init);
+                        tccp->cblkh = (OPJ_UINT32)opj_int_floorlog2(parameters->cblockh_init);
+                        tccp->cblksty = (OPJ_UINT32)parameters->mode;
                         tccp->qmfbid = parameters->irreversible ? 0 : 1;
                         tccp->qntsty = parameters->irreversible ? J2K_CCP_QNTSTY_SEQNT : J2K_CCP_QNTSTY_NOQNT;
                         tccp->numgbits = 2;
@@ -5968,48 +6452,44 @@ void opj_j2k_setup_encoder(     opj_j2k_t *p_j2k,
                                 tccp->roishift = 0;
                         }
 
-                        if(parameters->cp_cinema) {
-                                /*Precinct size for lowest frequency subband=128*/
-                                tccp->prcw[0] = 7;
-                                tccp->prch[0] = 7;
-                                /*Precinct size at all other resolutions = 256*/
-                                for (j = 1; j < tccp->numresolutions; j++) {
-                                        tccp->prcw[j] = 8;
-                                        tccp->prch[j] = 8;
-                                }
-                        }else{
                                 if (parameters->csty & J2K_CCP_CSTY_PRT) {
                                         OPJ_INT32 p = 0, it_res;
-                                        for (it_res = tccp->numresolutions - 1; it_res >= 0; it_res--) {
+                                        assert( tccp->numresolutions > 0 );
+                                        for (it_res = (OPJ_INT32)tccp->numresolutions - 1; it_res >= 0; it_res--) {
                                                 if (p < parameters->res_spec) {
 
                                                         if (parameters->prcw_init[p] < 1) {
                                                                 tccp->prcw[it_res] = 1;
                                                         } else {
-                                                                tccp->prcw[it_res] = opj_int_floorlog2(parameters->prcw_init[p]);
+                                                                tccp->prcw[it_res] = (OPJ_UINT32)opj_int_floorlog2(parameters->prcw_init[p]);
                                                         }
 
                                                         if (parameters->prch_init[p] < 1) {
                                                                 tccp->prch[it_res] = 1;
                                                         }else {
-                                                                tccp->prch[it_res] = opj_int_floorlog2(parameters->prch_init[p]);
+                                                                tccp->prch[it_res] = (OPJ_UINT32)opj_int_floorlog2(parameters->prch_init[p]);
                                                         }
 
                                                 } else {
-                                                        int res_spec = parameters->res_spec;
-                                                        int size_prcw = parameters->prcw_init[res_spec - 1] >> (p - (res_spec - 1));
-                                                        int size_prch = parameters->prch_init[res_spec - 1] >> (p - (res_spec - 1));
+                                                        OPJ_INT32 res_spec = parameters->res_spec;
+                                                        OPJ_INT32 size_prcw = 0;
+                                                        OPJ_INT32 size_prch = 0;
+
+                                                        assert(res_spec>0); /* issue 189 */
+                                                        size_prcw = parameters->prcw_init[res_spec - 1] >> (p - (res_spec - 1));
+                                                        size_prch = parameters->prch_init[res_spec - 1] >> (p - (res_spec - 1));
+
 
                                                         if (size_prcw < 1) {
                                                                 tccp->prcw[it_res] = 1;
                                                         } else {
-                                                                tccp->prcw[it_res] = opj_int_floorlog2(size_prcw);
+                                                                tccp->prcw[it_res] = (OPJ_UINT32)opj_int_floorlog2(size_prcw);
                                                         }
 
                                                         if (size_prch < 1) {
                                                                 tccp->prch[it_res] = 1;
                                                         } else {
-                                                                tccp->prch[it_res] = opj_int_floorlog2(size_prch);
+                                                                tccp->prch[it_res] = (OPJ_UINT32)opj_int_floorlog2(size_prch);
                                                         }
                                                 }
                                                 p++;
@@ -6021,7 +6501,6 @@ void opj_j2k_setup_encoder(     opj_j2k_t *p_j2k,
                                                 tccp->prch[j] = 15;
                                         }
                                 }
-                        }
 
                         opj_dwt_calc_explicit_stepsizes(tccp, image->comps[i].prec);
                 }
@@ -6033,14 +6512,14 @@ void opj_j2k_setup_encoder(     opj_j2k_t *p_j2k,
         }
 }
 
-static opj_bool opj_j2k_add_mhmarker(opj_codestream_index_t *cstr_index, OPJ_UINT32 type, OPJ_OFF_T pos, OPJ_UINT32 len)
+static OPJ_BOOL opj_j2k_add_mhmarker(opj_codestream_index_t *cstr_index, OPJ_UINT32 type, OPJ_OFF_T pos, OPJ_UINT32 len)
 {
         assert(cstr_index != 00);
 
         /* expand the list? */
         if ((cstr_index->marknum + 1) > cstr_index->maxmarknum) {
                 opj_marker_info_t *new_marker;
-                cstr_index->maxmarknum = 100 + (int) ((float) cstr_index->maxmarknum * 1.0F);
+                cstr_index->maxmarknum = (OPJ_UINT32)(100 + (OPJ_FLOAT32) cstr_index->maxmarknum);
                 new_marker = (opj_marker_info_t *) opj_realloc(cstr_index->marker, cstr_index->maxmarknum *sizeof(opj_marker_info_t));
                 if (! new_marker) {
                         opj_free(cstr_index->marker);
@@ -6061,7 +6540,7 @@ static opj_bool opj_j2k_add_mhmarker(opj_codestream_index_t *cstr_index, OPJ_UIN
         return OPJ_TRUE;
 }
 
-static opj_bool opj_j2k_add_tlmarker(OPJ_UINT32 tileno, opj_codestream_index_t *cstr_index, OPJ_UINT32 type, OPJ_OFF_T pos, OPJ_UINT32 len)
+static OPJ_BOOL opj_j2k_add_tlmarker(OPJ_UINT32 tileno, opj_codestream_index_t *cstr_index, OPJ_UINT32 type, OPJ_OFF_T pos, OPJ_UINT32 len)
 {
         assert(cstr_index != 00);
         assert(cstr_index->tile_index != 00);
@@ -6069,7 +6548,7 @@ static opj_bool opj_j2k_add_tlmarker(OPJ_UINT32 tileno, opj_codestream_index_t *
         /* expand the list? */
         if ((cstr_index->tile_index[tileno].marknum + 1) > cstr_index->tile_index[tileno].maxmarknum) {
                 opj_marker_info_t *new_marker;
-                cstr_index->tile_index[tileno].maxmarknum = 100 + (int) ((float) cstr_index->tile_index[tileno].maxmarknum * 1.0F);
+                cstr_index->tile_index[tileno].maxmarknum = (OPJ_UINT32)(100 + (OPJ_FLOAT32) cstr_index->tile_index[tileno].maxmarknum);
                 new_marker = (opj_marker_info_t *) opj_realloc(
                                 cstr_index->tile_index[tileno].marker,
                                 cstr_index->tile_index[tileno].maxmarknum *sizeof(opj_marker_info_t));
@@ -6106,7 +6585,7 @@ static opj_bool opj_j2k_add_tlmarker(OPJ_UINT32 tileno, opj_codestream_index_t *
  * -----------------------------------------------------------------------
  */
 
-opj_bool opj_j2k_end_decompress(opj_j2k_t *p_j2k,
+OPJ_BOOL opj_j2k_end_decompress(opj_j2k_t *p_j2k,
                                 opj_stream_private_t *p_stream,
                                 opj_event_mgr_t * p_manager
                                 )
@@ -6117,7 +6596,7 @@ opj_bool opj_j2k_end_decompress(opj_j2k_t *p_j2k,
     return OPJ_TRUE;
 }
 
-opj_bool opj_j2k_read_header(   opj_stream_private_t *p_stream,
+OPJ_BOOL opj_j2k_read_header(   opj_stream_private_t *p_stream,
                                                             opj_j2k_t* p_j2k,
                                                             opj_image_t** p_image,
                                                             opj_event_mgr_t* p_manager )
@@ -6192,11 +6671,11 @@ void opj_j2k_setup_decoding_validation (opj_j2k_t *p_j2k)
 
 }
 
-opj_bool opj_j2k_mct_validation (       opj_j2k_t * p_j2k,
+OPJ_BOOL opj_j2k_mct_validation (       opj_j2k_t * p_j2k,
                                                                 opj_stream_private_t *p_stream,
                                                                 opj_event_mgr_t * p_manager )
 {
-        opj_bool l_is_valid = OPJ_TRUE;
+        OPJ_BOOL l_is_valid = OPJ_TRUE;
         OPJ_UINT32 i,j;
 
         /* preconditions */
@@ -6225,7 +6704,7 @@ opj_bool opj_j2k_mct_validation (       opj_j2k_t * p_j2k,
         return l_is_valid;
 }
 
-opj_bool opj_j2k_setup_mct_encoding(opj_tcp_t * p_tcp, opj_image_t * p_image)
+OPJ_BOOL opj_j2k_setup_mct_encoding(opj_tcp_t * p_tcp, opj_image_t * p_image)
 {
         OPJ_UINT32 i;
         OPJ_UINT32 l_indix = 1;
@@ -6378,7 +6857,7 @@ opj_bool opj_j2k_setup_mct_encoding(opj_tcp_t * p_tcp, opj_image_t * p_image)
         return OPJ_TRUE;
 }
 
-opj_bool opj_j2k_build_decoder (opj_j2k_t * p_j2k,
+OPJ_BOOL opj_j2k_build_decoder (opj_j2k_t * p_j2k,
                                                             opj_stream_private_t *p_stream,
                                                             opj_event_mgr_t * p_manager )
 {
@@ -6390,7 +6869,7 @@ opj_bool opj_j2k_build_decoder (opj_j2k_t * p_j2k,
         return OPJ_TRUE;
 }
 
-opj_bool opj_j2k_build_encoder (opj_j2k_t * p_j2k,
+OPJ_BOOL opj_j2k_build_encoder (opj_j2k_t * p_j2k,
                                                         opj_stream_private_t *p_stream,
                                                         opj_event_mgr_t * p_manager )
 {
@@ -6402,11 +6881,11 @@ opj_bool opj_j2k_build_encoder (opj_j2k_t * p_j2k,
         return OPJ_TRUE;
 }
 
-opj_bool opj_j2k_encoding_validation (  opj_j2k_t * p_j2k,
+OPJ_BOOL opj_j2k_encoding_validation (  opj_j2k_t * p_j2k,
                                                                             opj_stream_private_t *p_stream,
                                                                             opj_event_mgr_t * p_manager )
 {
-        opj_bool l_is_valid = OPJ_TRUE;
+        OPJ_BOOL l_is_valid = OPJ_TRUE;
 
         /* preconditions */
         assert(p_j2k != 00);
@@ -6437,12 +6916,12 @@ opj_bool opj_j2k_encoding_validation (  opj_j2k_t * p_j2k,
         return l_is_valid;
 }
 
-opj_bool opj_j2k_decoding_validation (  opj_j2k_t *p_j2k,
+OPJ_BOOL opj_j2k_decoding_validation (  opj_j2k_t *p_j2k,
                                         opj_stream_private_t *p_stream,
                                         opj_event_mgr_t * p_manager
                                         )
 {
-        opj_bool l_is_valid = OPJ_TRUE;
+        OPJ_BOOL l_is_valid = OPJ_TRUE;
 
         /* preconditions*/
         assert(p_j2k != 00);
@@ -6467,7 +6946,7 @@ opj_bool opj_j2k_decoding_validation (  opj_j2k_t *p_j2k,
         return l_is_valid;
 }
 
-opj_bool opj_j2k_read_header_procedure( opj_j2k_t *p_j2k,
+OPJ_BOOL opj_j2k_read_header_procedure( opj_j2k_t *p_j2k,
                                                                             opj_stream_private_t *p_stream,
                                                                             opj_event_mgr_t * p_manager)
 {
@@ -6596,13 +7075,13 @@ opj_bool opj_j2k_read_header_procedure( opj_j2k_t *p_j2k,
         return OPJ_TRUE;
 }
 
-opj_bool opj_j2k_exec ( opj_j2k_t * p_j2k,
+OPJ_BOOL opj_j2k_exec ( opj_j2k_t * p_j2k,
                                         opj_procedure_list_t * p_procedure_list,
                                         opj_stream_private_t *p_stream,
                                         opj_event_mgr_t * p_manager )
 {
-        opj_bool (** l_procedure) (opj_j2k_t * ,opj_stream_private_t *,opj_event_mgr_t *) = 00;
-        opj_bool l_result = OPJ_TRUE;
+        OPJ_BOOL (** l_procedure) (opj_j2k_t * ,opj_stream_private_t *,opj_event_mgr_t *) = 00;
+        OPJ_BOOL l_result = OPJ_TRUE;
         OPJ_UINT32 l_nb_proc, i;
 
         /* preconditions*/
@@ -6612,7 +7091,7 @@ opj_bool opj_j2k_exec ( opj_j2k_t * p_j2k,
         assert(p_manager != 00);
 
         l_nb_proc = opj_procedure_list_get_nb_procedures(p_procedure_list);
-        l_procedure = (opj_bool (**) (opj_j2k_t * ,opj_stream_private_t *,opj_event_mgr_t *)) opj_procedure_list_get_first_procedure(p_procedure_list);
+        l_procedure = (OPJ_BOOL (**) (opj_j2k_t * ,opj_stream_private_t *,opj_event_mgr_t *)) opj_procedure_list_get_first_procedure(p_procedure_list);
 
         for     (i=0;i<l_nb_proc;++i) {
                 l_result = l_result && ((*l_procedure) (p_j2k,p_stream,p_manager));
@@ -6625,7 +7104,7 @@ opj_bool opj_j2k_exec ( opj_j2k_t * p_j2k,
 }
 
 /* FIXME DOC*/
-static opj_bool opj_j2k_copy_default_tcp_and_create_tcd (       opj_j2k_t * p_j2k,
+static OPJ_BOOL opj_j2k_copy_default_tcp_and_create_tcd (       opj_j2k_t * p_j2k,
                                                             opj_stream_private_t *p_stream,
                                                             opj_event_mgr_t * p_manager
                                                             )
@@ -6651,9 +7130,9 @@ static opj_bool opj_j2k_copy_default_tcp_and_create_tcd (       opj_j2k_t * p_j2
         l_image = p_j2k->m_private_image;
         l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw;
         l_tcp = p_j2k->m_cp.tcps;
-        l_tccp_size = l_image->numcomps * sizeof(opj_tccp_t);
+        l_tccp_size = l_image->numcomps * (OPJ_UINT32)sizeof(opj_tccp_t);
         l_default_tcp = p_j2k->m_specific_param.m_decoder.m_default_tcp;
-        l_mct_size = l_image->numcomps * l_image->numcomps * sizeof(OPJ_FLOAT32);
+        l_mct_size = l_image->numcomps * l_image->numcomps * (OPJ_UINT32)sizeof(OPJ_FLOAT32);
 
         /* For each tile */
         for (i=0; i<l_nb_tiles; ++i) {
@@ -6677,7 +7156,7 @@ static opj_bool opj_j2k_copy_default_tcp_and_create_tcd (       opj_j2k_t * p_j2
                 }
 
                 /* Get the mct_record of the dflt_tile_cp and copy them into the current tile cp*/
-                l_mct_records_size = l_default_tcp->m_nb_max_mct_records * sizeof(opj_mct_data_t);
+                l_mct_records_size = l_default_tcp->m_nb_max_mct_records * (OPJ_UINT32)sizeof(opj_mct_data_t);
                 l_tcp->m_mct_records = (opj_mct_data_t*)opj_malloc(l_mct_records_size);
                 if (! l_tcp->m_mct_records) {
                         return OPJ_FALSE;
@@ -6704,7 +7183,7 @@ static opj_bool opj_j2k_copy_default_tcp_and_create_tcd (       opj_j2k_t * p_j2
                 }
 
                 /* Get the mcc_record of the dflt_tile_cp and copy them into the current tile cp*/
-                l_mcc_records_size = l_default_tcp->m_nb_max_mcc_records * sizeof(opj_simple_mcc_decorrelation_data_t);
+                l_mcc_records_size = l_default_tcp->m_nb_max_mcc_records * (OPJ_UINT32)sizeof(opj_simple_mcc_decorrelation_data_t);
                 l_tcp->m_mcc_records = (opj_simple_mcc_decorrelation_data_t*) opj_malloc(l_mcc_records_size);
                 if (! l_tcp->m_mcc_records) {
                         return OPJ_FALSE;
@@ -6718,12 +7197,12 @@ static opj_bool opj_j2k_copy_default_tcp_and_create_tcd (       opj_j2k_t * p_j2
                 for (j=0;j<l_default_tcp->m_nb_max_mcc_records;++j) {
 
                         if (l_src_mcc_rec->m_decorrelation_array) {
-                                l_offset = l_src_mcc_rec->m_decorrelation_array - l_default_tcp->m_mct_records;
+                                l_offset = (OPJ_UINT32)(l_src_mcc_rec->m_decorrelation_array - l_default_tcp->m_mct_records);
                                 l_dest_mcc_rec->m_decorrelation_array = l_tcp->m_mct_records + l_offset;
                         }
 
                         if (l_src_mcc_rec->m_offset_array) {
-                                l_offset = l_src_mcc_rec->m_offset_array - l_default_tcp->m_mct_records;
+                                l_offset = (OPJ_UINT32)(l_src_mcc_rec->m_offset_array - l_default_tcp->m_mct_records);
                                 l_dest_mcc_rec->m_offset_array = l_tcp->m_mct_records + l_offset;
                         }
 
@@ -6970,13 +7449,13 @@ void opj_j2k_cp_destroy (opj_cp_t *p_cp)
         }
 }
 
-opj_bool opj_j2k_read_tile_header(      opj_j2k_t * p_j2k,
+OPJ_BOOL opj_j2k_read_tile_header(      opj_j2k_t * p_j2k,
                                                                     OPJ_UINT32 * p_tile_index,
                                                                     OPJ_UINT32 * p_data_size,
                                                                     OPJ_INT32 * p_tile_x0, OPJ_INT32 * p_tile_y0,
                                                                     OPJ_INT32 * p_tile_x1, OPJ_INT32 * p_tile_y1,
                                                                     OPJ_UINT32 * p_nb_comps,
-                                                                    opj_bool * p_go_on,
+                                                                    OPJ_BOOL * p_go_on,
                                                                     opj_stream_private_t *p_stream,
                                                                     opj_event_mgr_t * p_manager )
 {
@@ -7005,6 +7484,12 @@ opj_bool opj_j2k_read_tile_header(      opj_j2k_t * p_j2k,
 
                 /* Try to read until the Start Of Data is detected */
                 while (l_current_marker != J2K_MS_SOD) {
+                    
+                    if(opj_stream_get_number_byte_left(p_stream) == 0)
+                    {
+                        p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_NEOC;
+                        break;
+                    }
 
                         /* Try to read 2 bytes (the marker size) from stream and copy them into the buffer */
                         if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
@@ -7015,6 +7500,12 @@ opj_bool opj_j2k_read_tile_header(      opj_j2k_t * p_j2k,
                         /* Read 2 bytes from the buffer as the marker size */
                         opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_marker_size,2);
 
+                        /* cf. https://code.google.com/p/openjpeg/issues/detail?id=226 */
+                        if (l_current_marker == 0x8080 && opj_stream_get_number_byte_left(p_stream) == 0) {
+                                p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_NEOC;
+                                break;
+                        }
+
                         /* Why this condition? FIXME */
                         if (p_j2k->m_specific_param.m_decoder.m_state & J2K_STATE_TPH){
                                 p_j2k->m_specific_param.m_decoder.m_sot_length -= (l_marker_size + 2);
@@ -7099,6 +7590,9 @@ opj_bool opj_j2k_read_tile_header(      opj_j2k_t * p_j2k,
                                 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_current_marker,2);
                         }
                 }
+                if(opj_stream_get_number_byte_left(p_stream) == 0
+                    && p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_NEOC)
+                    break;
 
                 /* If we didn't skip data before, we need to read the SOD marker*/
                 if (! p_j2k->m_specific_param.m_decoder.m_skip_data) {
@@ -7182,7 +7676,7 @@ opj_bool opj_j2k_read_tile_header(      opj_j2k_t * p_j2k,
         return OPJ_TRUE;
 }
 
-opj_bool opj_j2k_decode_tile (  opj_j2k_t * p_j2k,
+OPJ_BOOL opj_j2k_decode_tile (  opj_j2k_t * p_j2k,
                                                         OPJ_UINT32 p_tile_index,
                                                         OPJ_BYTE * p_data,
                                                         OPJ_UINT32 p_data_size,
@@ -7216,6 +7710,7 @@ opj_bool opj_j2k_decode_tile (  opj_j2k_t * p_j2k,
                                                                 p_j2k->cstr_index) ) {
                 opj_j2k_tcp_destroy(l_tcp);
                 p_j2k->m_specific_param.m_decoder.m_state |= 0x8000;/*FIXME J2K_DEC_STATE_ERR;*/
+                opj_event_msg(p_manager, EVT_ERROR, "Failed to decode.\n");
                 return OPJ_FALSE;
         }
 
@@ -7230,7 +7725,12 @@ opj_bool opj_j2k_decode_tile (  opj_j2k_t * p_j2k,
         opj_j2k_tcp_data_destroy(l_tcp);
 
         p_j2k->m_specific_param.m_decoder.m_can_decode = 0;
-        p_j2k->m_specific_param.m_decoder.m_state &= (~ (0x0080));/* FIXME J2K_DEC_STATE_DATA);*/
+        p_j2k->m_specific_param.m_decoder.m_state &= (~ (0x0080u));/* FIXME J2K_DEC_STATE_DATA);*/
+
+        if(opj_stream_get_number_byte_left(p_stream) == 0 
+            && p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_NEOC){
+            return OPJ_TRUE;
+        }
 
         if (p_j2k->m_specific_param.m_decoder.m_state != 0x0100){ /*FIXME J2K_DEC_STATE_EOC)*/
                 if (opj_stream_read_data(p_stream,l_data,2,p_manager) != 2) {
@@ -7247,6 +7747,11 @@ opj_bool opj_j2k_decode_tile (  opj_j2k_t * p_j2k,
                 else if (l_current_marker != J2K_MS_SOT)
                 {
                         opj_event_msg(p_manager, EVT_ERROR, "Stream too short, expected SOT\n");
+                        
+                        if(opj_stream_get_number_byte_left(p_stream) == 0) {
+                            p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_NEOC;
+                            return OPJ_TRUE;
+                        }
                         return OPJ_FALSE;
                 }
         }
@@ -7254,7 +7759,7 @@ opj_bool opj_j2k_decode_tile (  opj_j2k_t * p_j2k,
         return OPJ_TRUE;
 }
 
-opj_bool opj_j2k_update_image_data (opj_tcd_t * p_tcd, OPJ_BYTE * p_data, opj_image_t* p_output_image)
+OPJ_BOOL opj_j2k_update_image_data (opj_tcd_t * p_tcd, OPJ_BYTE * p_data, opj_image_t* p_output_image)
 {
         OPJ_UINT32 i,j,k = 0;
         OPJ_UINT32 l_width_src,l_height_src;
@@ -7315,12 +7820,12 @@ opj_bool opj_j2k_update_image_data (opj_tcd_t * p_tcd, OPJ_BYTE * p_data, opj_im
                                 l_res->x0, l_res->x1, l_res->y0, l_res->y1);
                 }*/
 
-                l_width_src = (l_res->x1 - l_res->x0);
-                l_height_src = (l_res->y1 - l_res->y0);
+                l_width_src = (OPJ_UINT32)(l_res->x1 - l_res->x0);
+                l_height_src = (OPJ_UINT32)(l_res->y1 - l_res->y0);
 
                 /* Border of the current output component*/
-                l_x0_dest = opj_int_ceildivpow2(l_img_comp_dest->x0, l_img_comp_dest->factor);
-                l_y0_dest = opj_int_ceildivpow2(l_img_comp_dest->y0, l_img_comp_dest->factor);
+                l_x0_dest = (OPJ_UINT32)opj_int_ceildivpow2((OPJ_INT32)l_img_comp_dest->x0, (OPJ_INT32)l_img_comp_dest->factor);
+                l_y0_dest = (OPJ_UINT32)opj_int_ceildivpow2((OPJ_INT32)l_img_comp_dest->y0, (OPJ_INT32)l_img_comp_dest->factor);
                 l_x1_dest = l_x0_dest + l_img_comp_dest->w;
                 l_y1_dest = l_y0_dest + l_img_comp_dest->h;
 
@@ -7339,7 +7844,7 @@ opj_bool opj_j2k_update_image_data (opj_tcd_t * p_tcd, OPJ_BYTE * p_data, opj_im
                 assert( l_res->x0 >= 0);
                 assert( l_res->x1 >= 0);
                 if ( l_x0_dest < (OPJ_UINT32)l_res->x0 ) {
-                        l_start_x_dest = l_res->x0 - l_x0_dest;
+                        l_start_x_dest = (OPJ_UINT32)l_res->x0 - l_x0_dest;
                         l_offset_x0_src = 0;
 
                         if ( l_x1_dest >= (OPJ_UINT32)l_res->x1 ) {
@@ -7347,26 +7852,26 @@ opj_bool opj_j2k_update_image_data (opj_tcd_t * p_tcd, OPJ_BYTE * p_data, opj_im
                                 l_offset_x1_src = 0;
                         }
                         else {
-                                l_width_dest = l_x1_dest - l_res->x0 ;
-                                l_offset_x1_src = l_width_src - l_width_dest;
+                                l_width_dest = l_x1_dest - (OPJ_UINT32)l_res->x0 ;
+                                l_offset_x1_src = (OPJ_INT32)(l_width_src - l_width_dest);
                         }
                 }
                 else {
                         l_start_x_dest = 0 ;
-                        l_offset_x0_src = l_x0_dest - l_res->x0;
+                        l_offset_x0_src = (OPJ_INT32)l_x0_dest - l_res->x0;
 
                         if ( l_x1_dest >= (OPJ_UINT32)l_res->x1 ) {
-                                l_width_dest = l_width_src - l_offset_x0_src;
+                                l_width_dest = l_width_src - (OPJ_UINT32)l_offset_x0_src;
                                 l_offset_x1_src = 0;
                         }
                         else {
                                 l_width_dest = l_img_comp_dest->w ;
-                                l_offset_x1_src = l_res->x1 - l_x1_dest;
+                                l_offset_x1_src = l_res->x1 - (OPJ_INT32)l_x1_dest;
                         }
                 }
 
                 if ( l_y0_dest < (OPJ_UINT32)l_res->y0 ) {
-                        l_start_y_dest = l_res->y0 - l_y0_dest;
+                        l_start_y_dest = (OPJ_UINT32)l_res->y0 - l_y0_dest;
                         l_offset_y0_src = 0;
 
                         if ( l_y1_dest >= (OPJ_UINT32)l_res->y1 ) {
@@ -7374,37 +7879,41 @@ opj_bool opj_j2k_update_image_data (opj_tcd_t * p_tcd, OPJ_BYTE * p_data, opj_im
                                 l_offset_y1_src = 0;
                         }
                         else {
-                                l_height_dest = l_y1_dest - l_res->y0 ;
-                                l_offset_y1_src =  l_height_src - l_height_dest;
+                                l_height_dest = l_y1_dest - (OPJ_UINT32)l_res->y0 ;
+                                l_offset_y1_src =  (OPJ_INT32)(l_height_src - l_height_dest);
                         }
                 }
                 else {
                         l_start_y_dest = 0 ;
-                        l_offset_y0_src = l_y0_dest - l_res->y0;
+                        l_offset_y0_src = (OPJ_INT32)l_y0_dest - l_res->y0;
 
                         if ( l_y1_dest >= (OPJ_UINT32)l_res->y1 ) {
-                                l_height_dest = l_height_src - l_offset_y0_src;
+                                l_height_dest = l_height_src - (OPJ_UINT32)l_offset_y0_src;
                                 l_offset_y1_src = 0;
                         }
                         else {
                                 l_height_dest = l_img_comp_dest->h ;
-                                l_offset_y1_src = l_res->y1 - l_y1_dest;
+                                l_offset_y1_src = l_res->y1 - (OPJ_INT32)l_y1_dest;
                         }
                 }
 
                 if( (l_offset_x0_src < 0 ) || (l_offset_y0_src < 0 ) || (l_offset_x1_src < 0 ) || (l_offset_y1_src < 0 ) ){
                         return OPJ_FALSE;
                 }
+                /* testcase 2977.pdf.asan.67.2198 */
+                if ((OPJ_INT32)l_width_dest < 0 || (OPJ_INT32)l_height_dest < 0) {
+                        return OPJ_FALSE;
+                }
                 /*-----*/
 
                 /* Compute the input buffer offset */
-                l_start_offset_src = l_offset_x0_src + l_offset_y0_src * l_width_src;
+                l_start_offset_src = l_offset_x0_src + l_offset_y0_src * (OPJ_INT32)l_width_src;
                 l_line_offset_src = l_offset_x1_src + l_offset_x0_src;
-                l_end_offset_src = l_offset_y1_src * l_width_src - l_offset_x0_src;
+                l_end_offset_src = l_offset_y1_src * (OPJ_INT32)l_width_src - l_offset_x0_src;
 
                 /* Compute the output buffer offset */
-                l_start_offset_dest = l_start_x_dest + l_start_y_dest * l_img_comp_dest->w;
-                l_line_offset_dest = l_img_comp_dest->w - l_width_dest;
+                l_start_offset_dest = (OPJ_INT32)(l_start_x_dest + l_start_y_dest * l_img_comp_dest->w);
+                l_line_offset_dest = (OPJ_INT32)(l_img_comp_dest->w - l_width_dest);
 
                 /* Move the output buffer to the first place where we will write*/
                 l_dest_ptr = l_img_comp_dest->data + l_start_offset_dest;
@@ -7512,7 +8021,7 @@ opj_bool opj_j2k_update_image_data (opj_tcd_t * p_tcd, OPJ_BYTE * p_data, opj_im
         return OPJ_TRUE;
 }
 
-opj_bool opj_j2k_set_decode_area(       opj_j2k_t *p_j2k,
+OPJ_BOOL opj_j2k_set_decode_area(       opj_j2k_t *p_j2k,
                                                                     opj_image_t* p_image,
                                                                     OPJ_INT32 p_start_x, OPJ_INT32 p_start_y,
                                                                     OPJ_INT32 p_end_x, OPJ_INT32 p_end_y,
@@ -7563,8 +8072,8 @@ opj_bool opj_j2k_set_decode_area(       opj_j2k_t *p_j2k,
                 p_image->x0 = l_image->x0;
         }
         else {
-                p_j2k->m_specific_param.m_decoder.m_start_tile_x = (p_start_x - l_cp->tx0) / l_cp->tdx;
-                p_image->x0 = p_start_x;
+                p_j2k->m_specific_param.m_decoder.m_start_tile_x = ((OPJ_UINT32)p_start_x - l_cp->tx0) / l_cp->tdx;
+                p_image->x0 = (OPJ_UINT32)p_start_x;
         }
 
         /* Up */
@@ -7582,8 +8091,8 @@ opj_bool opj_j2k_set_decode_area(       opj_j2k_t *p_j2k,
                 p_image->y0 = l_image->y0;
         }
         else {
-                p_j2k->m_specific_param.m_decoder.m_start_tile_y = (p_start_y - l_cp->ty0) / l_cp->tdy;
-                p_image->y0 = p_start_y;
+                p_j2k->m_specific_param.m_decoder.m_start_tile_y = ((OPJ_UINT32)p_start_y - l_cp->ty0) / l_cp->tdy;
+                p_image->y0 = (OPJ_UINT32)p_start_y;
         }
 
         /* Right */
@@ -7603,8 +8112,8 @@ opj_bool opj_j2k_set_decode_area(       opj_j2k_t *p_j2k,
                 p_image->x1 = l_image->x1;
         }
         else {
-                p_j2k->m_specific_param.m_decoder.m_end_tile_x = opj_int_ceildiv((p_end_x - l_cp->tx0), l_cp->tdx);
-                p_image->x1 = p_end_x;
+                p_j2k->m_specific_param.m_decoder.m_end_tile_x = (OPJ_UINT32)opj_int_ceildiv(p_end_x - (OPJ_INT32)l_cp->tx0, (OPJ_INT32)l_cp->tdx);
+                p_image->x1 = (OPJ_UINT32)p_end_x;
         }
 
         /* Bottom */
@@ -7622,8 +8131,8 @@ opj_bool opj_j2k_set_decode_area(       opj_j2k_t *p_j2k,
                 p_image->y1 = l_image->y1;
         }
         else{
-                p_j2k->m_specific_param.m_decoder.m_end_tile_y = opj_int_ceildiv((p_end_y - l_cp->ty0), l_cp->tdy);
-                p_image->y1 = p_end_y;
+                p_j2k->m_specific_param.m_decoder.m_end_tile_y = (OPJ_UINT32)opj_int_ceildiv(p_end_y - (OPJ_INT32)l_cp->ty0, (OPJ_INT32)l_cp->tdy);
+                p_image->y1 = (OPJ_UINT32)p_end_y;
         }
         /* ----- */
 
@@ -7634,30 +8143,30 @@ opj_bool opj_j2k_set_decode_area(       opj_j2k_t *p_j2k,
         {
                 OPJ_INT32 l_h,l_w;
 
-                l_img_comp->x0 = opj_int_ceildiv(p_image->x0, l_img_comp->dx);
-                l_img_comp->y0 = opj_int_ceildiv(p_image->y0, l_img_comp->dy);
-                l_comp_x1 = opj_int_ceildiv(p_image->x1, l_img_comp->dx);
-                l_comp_y1 = opj_int_ceildiv(p_image->y1, l_img_comp->dy);
+                l_img_comp->x0 = (OPJ_UINT32)opj_int_ceildiv((OPJ_INT32)p_image->x0, (OPJ_INT32)l_img_comp->dx);
+                l_img_comp->y0 = (OPJ_UINT32)opj_int_ceildiv((OPJ_INT32)p_image->y0, (OPJ_INT32)l_img_comp->dy);
+                l_comp_x1 = opj_int_ceildiv((OPJ_INT32)p_image->x1, (OPJ_INT32)l_img_comp->dx);
+                l_comp_y1 = opj_int_ceildiv((OPJ_INT32)p_image->y1, (OPJ_INT32)l_img_comp->dy);
 
-                l_w = opj_int_ceildivpow2(l_comp_x1, l_img_comp->factor)
-                                - opj_int_ceildivpow2(l_img_comp->x0, l_img_comp->factor);
+                l_w = opj_int_ceildivpow2(l_comp_x1, (OPJ_INT32)l_img_comp->factor)
+                                - opj_int_ceildivpow2((OPJ_INT32)l_img_comp->x0, (OPJ_INT32)l_img_comp->factor);
                 if (l_w < 0){
                         opj_event_msg(p_manager, EVT_ERROR,
                                 "Size x of the decoded component image is incorrect (comp[%d].w=%d).\n",
                                 it_comp, l_w);
                         return OPJ_FALSE;
                 }
-                l_img_comp->w = l_w;
+                l_img_comp->w = (OPJ_UINT32)l_w;
 
-                l_h = opj_int_ceildivpow2(l_comp_y1, l_img_comp->factor)
-                                - opj_int_ceildivpow2(l_img_comp->y0, l_img_comp->factor);
+                l_h = opj_int_ceildivpow2(l_comp_y1, (OPJ_INT32)l_img_comp->factor)
+                                - opj_int_ceildivpow2((OPJ_INT32)l_img_comp->y0, (OPJ_INT32)l_img_comp->factor);
                 if (l_h < 0){
                         opj_event_msg(p_manager, EVT_ERROR,
                                 "Size y of the decoded component image is incorrect (comp[%d].h=%d).\n",
                                 it_comp, l_h);
                         return OPJ_FALSE;
                 }
-                l_img_comp->h = l_h;
+                l_img_comp->h = (OPJ_UINT32)l_h;
 
                 l_img_comp++;
         }
@@ -7773,7 +8282,7 @@ OPJ_UINT32 opj_j2k_get_SPCod_SPCoc_size (       opj_j2k_t *p_j2k,
         }
 }
 
-opj_bool opj_j2k_write_SPCod_SPCoc(     opj_j2k_t *p_j2k,
+OPJ_BOOL opj_j2k_write_SPCod_SPCoc(     opj_j2k_t *p_j2k,
                                                                     OPJ_UINT32 p_tile_no,
                                                                     OPJ_UINT32 p_comp_no,
                                                                     OPJ_BYTE * p_data,
@@ -7839,7 +8348,7 @@ opj_bool opj_j2k_write_SPCod_SPCoc(     opj_j2k_t *p_j2k,
         return OPJ_TRUE;
 }
 
-opj_bool opj_j2k_read_SPCod_SPCoc(  opj_j2k_t *p_j2k,
+OPJ_BOOL opj_j2k_read_SPCod_SPCoc(  opj_j2k_t *p_j2k,
                                                                 OPJ_UINT32 compno,
                                                                 OPJ_BYTE * p_header_data,
                                                                 OPJ_UINT32 * p_header_size,
@@ -7875,6 +8384,12 @@ opj_bool opj_j2k_read_SPCod_SPCoc(  opj_j2k_t *p_j2k,
 
         opj_read_bytes(l_current_ptr, &l_tccp->numresolutions ,1);              /* SPcox (D) */
         ++l_tccp->numresolutions;                                                                               /* tccp->numresolutions = read() + 1 */
+        if (l_tccp->numresolutions > OPJ_J2K_MAXRLVLS) {
+                opj_event_msg(p_manager, EVT_ERROR,
+                              "Invalid value for numresolutions : %d, max value is set in openjpeg.h at %d\n",
+                              l_tccp->numresolutions, OPJ_J2K_MAXRLVLS);
+                return OPJ_FALSE;
+        }
         ++l_current_ptr;
 
         /* If user wants to remove more resolutions than the codestream contains, return error */
@@ -7964,7 +8479,7 @@ void opj_j2k_copy_tile_component_parameters( opj_j2k_t *p_j2k )
 
         l_ref_tccp = &l_tcp->tccps[0];
         l_copied_tccp = l_ref_tccp + 1;
-        l_prc_size = l_ref_tccp->numresolutions * sizeof(OPJ_UINT32);
+        l_prc_size = l_ref_tccp->numresolutions * (OPJ_UINT32)sizeof(OPJ_UINT32);
 
         for     (i=1; i<p_j2k->m_private_image->numcomps; ++i) {
                 l_copied_tccp->numresolutions = l_ref_tccp->numresolutions;
@@ -8009,7 +8524,7 @@ OPJ_UINT32 opj_j2k_get_SQcd_SQcc_size ( opj_j2k_t *p_j2k,
         }
 }
 
-opj_bool opj_j2k_write_SQcd_SQcc(       opj_j2k_t *p_j2k,
+OPJ_BOOL opj_j2k_write_SQcd_SQcc(       opj_j2k_t *p_j2k,
                                                                 OPJ_UINT32 p_tile_no,
                                                                 OPJ_UINT32 p_comp_no,
                                                                 OPJ_BYTE * p_data,
@@ -8052,7 +8567,7 @@ opj_bool opj_j2k_write_SQcd_SQcc(       opj_j2k_t *p_j2k,
                 ++p_data;
 
                 for (l_band_no = 0; l_band_no < l_num_bands; ++l_band_no) {
-                        l_expn = l_tccp->stepsizes[l_band_no].expn;
+                        l_expn = (OPJ_UINT32)l_tccp->stepsizes[l_band_no].expn;
                         opj_write_bytes(p_data, l_expn << 3, 1);        /* SPqcx_i */
                         ++p_data;
                 }
@@ -8069,8 +8584,8 @@ opj_bool opj_j2k_write_SQcd_SQcc(       opj_j2k_t *p_j2k,
                 ++p_data;
 
                 for (l_band_no = 0; l_band_no < l_num_bands; ++l_band_no) {
-                        l_expn = l_tccp->stepsizes[l_band_no].expn;
-                        l_mant = l_tccp->stepsizes[l_band_no].mant;
+                        l_expn = (OPJ_UINT32)l_tccp->stepsizes[l_band_no].expn;
+                        l_mant = (OPJ_UINT32)l_tccp->stepsizes[l_band_no].mant;
 
                         opj_write_bytes(p_data, (l_expn << 11) + l_mant, 2);    /* SPqcx_i */
                         p_data += 2;
@@ -8082,7 +8597,7 @@ opj_bool opj_j2k_write_SQcd_SQcc(       opj_j2k_t *p_j2k,
         return OPJ_TRUE;
 }
 
-opj_bool opj_j2k_read_SQcd_SQcc(opj_j2k_t *p_j2k,
+OPJ_BOOL opj_j2k_read_SQcd_SQcc(opj_j2k_t *p_j2k,
                                                             OPJ_UINT32 p_comp_no,
                                                             OPJ_BYTE* p_header_data,
                                                             OPJ_UINT32 * p_header_size,
@@ -8168,7 +8683,7 @@ opj_bool opj_j2k_read_SQcd_SQcc(opj_j2k_t *p_j2k,
                         opj_read_bytes(l_current_ptr, &l_tmp ,1);                       /* SPqcx_i */
                         ++l_current_ptr;
                         if (l_band_no < OPJ_J2K_MAXBANDS){
-                                l_tccp->stepsizes[l_band_no].expn = l_tmp>>3;
+                                l_tccp->stepsizes[l_band_no].expn = (OPJ_INT32)(l_tmp >> 3);
                                 l_tccp->stepsizes[l_band_no].mant = 0;
                         }
                 }
@@ -8179,7 +8694,7 @@ opj_bool opj_j2k_read_SQcd_SQcc(opj_j2k_t *p_j2k,
                         opj_read_bytes(l_current_ptr, &l_tmp ,2);                       /* SPqcx_i */
                         l_current_ptr+=2;
                         if (l_band_no < OPJ_J2K_MAXBANDS){
-                                l_tccp->stepsizes[l_band_no].expn = l_tmp >> 11;
+                                l_tccp->stepsizes[l_band_no].expn = (OPJ_INT32)(l_tmp >> 11);
                                 l_tccp->stepsizes[l_band_no].mant = l_tmp & 0x7ff;
                         }
                 }
@@ -8190,8 +8705,8 @@ opj_bool opj_j2k_read_SQcd_SQcc(opj_j2k_t *p_j2k,
         if (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) {
                 for (l_band_no = 1; l_band_no < OPJ_J2K_MAXBANDS; l_band_no++) {
                         l_tccp->stepsizes[l_band_no].expn =
-                                ((l_tccp->stepsizes[0].expn) - ((l_band_no - 1) / 3) > 0) ?
-                                        (l_tccp->stepsizes[0].expn) - ((l_band_no - 1) / 3) : 0;
+                                ((OPJ_INT32)(l_tccp->stepsizes[0].expn) - (OPJ_INT32)((l_band_no - 1) / 3) > 0) ?
+                                        (OPJ_INT32)(l_tccp->stepsizes[0].expn) - (OPJ_INT32)((l_band_no - 1) / 3) : 0;
                         l_tccp->stepsizes[l_band_no].mant = l_tccp->stepsizes[0].mant;
                 }
         }
@@ -8228,6 +8743,58 @@ void opj_j2k_copy_tile_quantization_parameters( opj_j2k_t *p_j2k )
         }
 }
 
+static void opj_j2k_dump_tile_info( opj_tcp_t * l_default_tile,OPJ_INT32 numcomps,FILE* out_stream)
+{
+        if (l_default_tile)
+        {
+                OPJ_INT32 compno;
+
+                fprintf(out_stream, "\t default tile {\n");
+                fprintf(out_stream, "\t\t csty=%#x\n", l_default_tile->csty);
+                fprintf(out_stream, "\t\t prg=%#x\n", l_default_tile->prg);
+                fprintf(out_stream, "\t\t numlayers=%d\n", l_default_tile->numlayers);
+                fprintf(out_stream, "\t\t mct=%x\n", l_default_tile->mct);
+
+                for (compno = 0; compno < numcomps; compno++) {
+                        opj_tccp_t *l_tccp = &(l_default_tile->tccps[compno]);
+                        OPJ_UINT32 resno;
+      OPJ_INT32 bandno, numbands;
+
+                        /* coding style*/
+                        fprintf(out_stream, "\t\t comp %d {\n", compno);
+                        fprintf(out_stream, "\t\t\t csty=%#x\n", l_tccp->csty);
+                        fprintf(out_stream, "\t\t\t numresolutions=%d\n", l_tccp->numresolutions);
+                        fprintf(out_stream, "\t\t\t cblkw=2^%d\n", l_tccp->cblkw);
+                        fprintf(out_stream, "\t\t\t cblkh=2^%d\n", l_tccp->cblkh);
+                        fprintf(out_stream, "\t\t\t cblksty=%#x\n", l_tccp->cblksty);
+                        fprintf(out_stream, "\t\t\t qmfbid=%d\n", l_tccp->qmfbid);
+
+                        fprintf(out_stream, "\t\t\t preccintsize (w,h)=");
+                        for (resno = 0; resno < l_tccp->numresolutions; resno++) {
+                                fprintf(out_stream, "(%d,%d) ", l_tccp->prcw[resno], l_tccp->prch[resno]);
+                        }
+                        fprintf(out_stream, "\n");
+
+                        /* quantization style*/
+                        fprintf(out_stream, "\t\t\t qntsty=%d\n", l_tccp->qntsty);
+                        fprintf(out_stream, "\t\t\t numgbits=%d\n", l_tccp->numgbits);
+                        fprintf(out_stream, "\t\t\t stepsizes (m,e)=");
+                        numbands = (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) ? 1 : (OPJ_INT32)l_tccp->numresolutions * 3 - 2;
+                        for (bandno = 0; bandno < numbands; bandno++) {
+                                fprintf(out_stream, "(%d,%d) ", l_tccp->stepsizes[bandno].mant,
+                                        l_tccp->stepsizes[bandno].expn);
+                        }
+                        fprintf(out_stream, "\n");
+
+                        /* RGN value*/
+                        fprintf(out_stream, "\t\t\t roishift=%d\n", l_tccp->roishift);
+
+                        fprintf(out_stream, "\t\t }\n");
+                } /*end of component of default tile*/
+                fprintf(out_stream, "\t }\n"); /*end of default tile*/
+            }
+}
+
 void j2k_dump (opj_j2k_t* p_j2k, OPJ_INT32 flag, FILE* out_stream)
 {
         /* Check if the flag is compatible with j2k file*/
@@ -8246,6 +8813,16 @@ void j2k_dump (opj_j2k_t* p_j2k, OPJ_INT32 flag, FILE* out_stream)
         if (flag & OPJ_J2K_MH_INFO){
                 opj_j2k_dump_MH_info(p_j2k, out_stream);
         }
+        /* Dump all tile/codestream info */
+        if (flag & OPJ_J2K_TCH_INFO){
+          OPJ_UINT32 l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw;
+          OPJ_UINT32 i;
+          opj_tcp_t * l_tcp = p_j2k->m_cp.tcps;
+          for (i=0;i<l_nb_tiles;++i) {
+            opj_j2k_dump_tile_info( l_tcp,(OPJ_INT32)p_j2k->m_private_image->numcomps, out_stream);
+            ++l_tcp;
+          }
+        }
 
         /* Dump the codestream info of the current tile */
         if (flag & OPJ_J2K_TH_INFO){
@@ -8332,73 +8909,20 @@ void opj_j2k_dump_MH_index(opj_j2k_t* p_j2k, FILE* out_stream)
 
 }
 
+
 void opj_j2k_dump_MH_info(opj_j2k_t* p_j2k, FILE* out_stream)
 {
-        opj_tcp_t * l_default_tile=NULL;
 
         fprintf(out_stream, "Codestream info from main header: {\n");
 
         fprintf(out_stream, "\t tx0=%d, ty0=%d\n", p_j2k->m_cp.tx0, p_j2k->m_cp.ty0);
         fprintf(out_stream, "\t tdx=%d, tdy=%d\n", p_j2k->m_cp.tdx, p_j2k->m_cp.tdy);
         fprintf(out_stream, "\t tw=%d, th=%d\n", p_j2k->m_cp.tw, p_j2k->m_cp.th);
-
-        l_default_tile = p_j2k->m_specific_param.m_decoder.m_default_tcp;
-        if (l_default_tile)
-        {
-                OPJ_INT32 compno;
-                OPJ_INT32 numcomps = p_j2k->m_private_image->numcomps;
-
-                fprintf(out_stream, "\t default tile {\n");
-                fprintf(out_stream, "\t\t csty=%#x\n", l_default_tile->csty);
-                fprintf(out_stream, "\t\t prg=%#x\n", l_default_tile->prg);
-                fprintf(out_stream, "\t\t numlayers=%d\n", l_default_tile->numlayers);
-                fprintf(out_stream, "\t\t mct=%x\n", l_default_tile->mct);
-
-                for (compno = 0; compno < numcomps; compno++) {
-                        opj_tccp_t *l_tccp = &(l_default_tile->tccps[compno]);
-                        OPJ_UINT32 resno;
-      OPJ_INT32 bandno, numbands;
-
-                        /* coding style*/
-                        fprintf(out_stream, "\t\t comp %d {\n", compno);
-                        fprintf(out_stream, "\t\t\t csty=%#x\n", l_tccp->csty);
-                        fprintf(out_stream, "\t\t\t numresolutions=%d\n", l_tccp->numresolutions);
-                        fprintf(out_stream, "\t\t\t cblkw=2^%d\n", l_tccp->cblkw);
-                        fprintf(out_stream, "\t\t\t cblkh=2^%d\n", l_tccp->cblkh);
-                        fprintf(out_stream, "\t\t\t cblksty=%#x\n", l_tccp->cblksty);
-                        fprintf(out_stream, "\t\t\t qmfbid=%d\n", l_tccp->qmfbid);
-
-                        fprintf(out_stream, "\t\t\t preccintsize (w,h)=");
-                        for (resno = 0; resno < l_tccp->numresolutions; resno++) {
-                                fprintf(out_stream, "(%d,%d) ", l_tccp->prcw[resno], l_tccp->prch[resno]);
-                        }
-                        fprintf(out_stream, "\n");
-
-                        /* quantization style*/
-                        fprintf(out_stream, "\t\t\t qntsty=%d\n", l_tccp->qntsty);
-                        fprintf(out_stream, "\t\t\t numgbits=%d\n", l_tccp->numgbits);
-                        fprintf(out_stream, "\t\t\t stepsizes (m,e)=");
-                        numbands = (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) ? 1 : l_tccp->numresolutions * 3 - 2;
-                        for (bandno = 0; bandno < numbands; bandno++) {
-                                fprintf(out_stream, "(%d,%d) ", l_tccp->stepsizes[bandno].mant,
-                                        l_tccp->stepsizes[bandno].expn);
-                        }
-                        fprintf(out_stream, "\n");
-
-                        /* RGN value*/
-                        fprintf(out_stream, "\t\t\t roishift=%d\n", l_tccp->roishift);
-
-                        fprintf(out_stream, "\t\t }\n");
-                } /*end of component of default tile*/
-                fprintf(out_stream, "\t }\n"); /*end of default tile*/
-
-        }
-
+        opj_j2k_dump_tile_info(p_j2k->m_specific_param.m_decoder.m_default_tcp,(OPJ_INT32)p_j2k->m_private_image->numcomps, out_stream);
         fprintf(out_stream, "}\n");
-
 }
 
-void j2k_dump_image_header(opj_image_t* img_header, opj_bool dev_dump_flag, FILE* out_stream)
+void j2k_dump_image_header(opj_image_t* img_header, OPJ_BOOL dev_dump_flag, FILE* out_stream)
 {
         char tab[2];
 
@@ -8427,7 +8951,7 @@ void j2k_dump_image_header(opj_image_t* img_header, opj_bool dev_dump_flag, FILE
         fprintf(out_stream, "}\n");
 }
 
-void j2k_dump_image_comp_header(opj_image_comp_t* comp_header, opj_bool dev_dump_flag, FILE* out_stream)
+void j2k_dump_image_comp_header(opj_image_comp_t* comp_header, OPJ_BOOL dev_dump_flag, FILE* out_stream)
 {
         char tab[3];
 
@@ -8502,11 +9026,11 @@ opj_codestream_info_v2_t* j2k_get_cstr_info(opj_j2k_t* p_j2k)
                 l_tccp_info->qntsty = l_tccp->qntsty;
                 l_tccp_info->numgbits = l_tccp->numgbits;
 
-                numbands = (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) ? 1 : l_tccp->numresolutions * 3 - 2;
+                numbands = (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) ? 1 : (OPJ_INT32)l_tccp->numresolutions * 3 - 2;
                 if (numbands < OPJ_J2K_MAXBANDS) {
                         for (bandno = 0; bandno < numbands; bandno++) {
-                                l_tccp_info->stepsizes_mant[bandno] = l_tccp->stepsizes[bandno].mant;
-                                l_tccp_info->stepsizes_expn[bandno] = l_tccp->stepsizes[bandno].expn;
+                                l_tccp_info->stepsizes_mant[bandno] = (OPJ_UINT32)l_tccp->stepsizes[bandno].mant;
+                                l_tccp_info->stepsizes_expn[bandno] = (OPJ_UINT32)l_tccp->stepsizes[bandno].expn;
                         }
                 }
 
@@ -8626,7 +9150,7 @@ opj_codestream_index_t* j2k_get_cstr_index(opj_j2k_t* p_j2k)
         return l_cstr_index;
 }
 
-opj_bool opj_j2k_allocate_tile_element_cstr_index(opj_j2k_t *p_j2k)
+OPJ_BOOL opj_j2k_allocate_tile_element_cstr_index(opj_j2k_t *p_j2k)
 {
         OPJ_UINT32 it_tile=0;
 
@@ -8647,16 +9171,17 @@ opj_bool opj_j2k_allocate_tile_element_cstr_index(opj_j2k_t *p_j2k)
         return OPJ_TRUE;
 }
 
-opj_bool opj_j2k_decode_tiles ( opj_j2k_t *p_j2k,
+OPJ_BOOL opj_j2k_decode_tiles ( opj_j2k_t *p_j2k,
                                                             opj_stream_private_t *p_stream,
                                                             opj_event_mgr_t * p_manager)
 {
-        opj_bool l_go_on = OPJ_TRUE;
+        OPJ_BOOL l_go_on = OPJ_TRUE;
         OPJ_UINT32 l_current_tile_no;
         OPJ_UINT32 l_data_size,l_max_data_size;
         OPJ_INT32 l_tile_x0,l_tile_y0,l_tile_x1,l_tile_y1;
         OPJ_UINT32 l_nb_comps;
         OPJ_BYTE * l_current_data;
+        OPJ_UINT32 nr_tiles = 0;
 
         l_current_data = (OPJ_BYTE*)opj_malloc(1000);
         if (! l_current_data) {
@@ -8696,6 +9221,7 @@ opj_bool opj_j2k_decode_tiles ( opj_j2k_t *p_j2k,
 
                 if (! opj_j2k_decode_tile(p_j2k,l_current_tile_no,l_current_data,l_data_size,p_stream,p_manager)) {
                         opj_free(l_current_data);
+                        opj_event_msg(p_manager, EVT_ERROR, "Failed to decode tile %d/%d\n", l_current_tile_no +1, p_j2k->m_cp.th * p_j2k->m_cp.tw);
                         return OPJ_FALSE;
                 }
                 opj_event_msg(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);
@@ -8705,7 +9231,12 @@ opj_bool opj_j2k_decode_tiles ( opj_j2k_t *p_j2k,
                         return OPJ_FALSE;
                 }
                 opj_event_msg(p_manager, EVT_INFO, "Image data has been updated with tile %d.\n\n", l_current_tile_no + 1);
-
+                
+                if(opj_stream_get_number_byte_left(p_stream) == 0  
+                    && p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_NEOC)
+                    break;
+                if(++nr_tiles ==  p_j2k->m_cp.th * p_j2k->m_cp.tw) 
+                    break;
         }
 
         opj_free(l_current_data);
@@ -8729,11 +9260,11 @@ static void opj_j2k_setup_decoding (opj_j2k_t *p_j2k)
 /*
  * Read and decode one tile.
  */
-static opj_bool opj_j2k_decode_one_tile (       opj_j2k_t *p_j2k,
+static OPJ_BOOL opj_j2k_decode_one_tile (       opj_j2k_t *p_j2k,
                                                                             opj_stream_private_t *p_stream,
                                                                             opj_event_mgr_t * p_manager)
 {
-        opj_bool l_go_on = OPJ_TRUE;
+        OPJ_BOOL l_go_on = OPJ_TRUE;
         OPJ_UINT32 l_current_tile_no;
         OPJ_UINT32 l_tile_no_to_dec;
         OPJ_UINT32 l_data_size,l_max_data_size;
@@ -8757,7 +9288,7 @@ static opj_bool opj_j2k_decode_one_tile (       opj_j2k_t *p_j2k,
                 }
         }
         /* Move into the codestream to the first SOT used to decode the desired tile */
-        l_tile_no_to_dec = p_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec;
+        l_tile_no_to_dec = (OPJ_UINT32)p_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec;
         if (p_j2k->cstr_index->tile_index)
                 if(p_j2k->cstr_index->tile_index->tp_index)
                 {
@@ -8766,12 +9297,14 @@ static opj_bool opj_j2k_decode_one_tile (       opj_j2k_t *p_j2k,
                                  *  so move to the last SOT read */
                                 if ( !(opj_stream_read_seek(p_stream, p_j2k->m_specific_param.m_decoder.m_last_sot_read_pos+2, p_manager)) ){
                                         opj_event_msg(p_manager, EVT_ERROR, "Problem with seek function\n");
+                        opj_free(l_current_data);
                                         return OPJ_FALSE;
                                 }
                         }
                         else{
                                 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)) ) {
                                         opj_event_msg(p_manager, EVT_ERROR, "Problem with seek function\n");
+                        opj_free(l_current_data);
                                         return OPJ_FALSE;
                                 }
                         }
@@ -8857,7 +9390,7 @@ static void opj_j2k_setup_decoding_tile (opj_j2k_t *p_j2k)
 
 }
 
-opj_bool opj_j2k_decode(opj_j2k_t * p_j2k,
+OPJ_BOOL opj_j2k_decode(opj_j2k_t * p_j2k,
                                                 opj_stream_private_t * p_stream,
                                                 opj_image_t * p_image,
                                                 opj_event_mgr_t * p_manager)
@@ -8887,13 +9420,20 @@ opj_bool opj_j2k_decode(opj_j2k_t * p_j2k,
         for (compno = 0; compno < p_image->numcomps; compno++) {
                 p_image->comps[compno].resno_decoded = p_j2k->m_output_image->comps[compno].resno_decoded;
                 p_image->comps[compno].data = p_j2k->m_output_image->comps[compno].data;
+#if 0
+                char fn[256];
+                sprintf( fn, "/tmp/%d.raw", compno );
+                FILE *debug = fopen( fn, "wb" );
+                fwrite( p_image->comps[compno].data, sizeof(OPJ_INT32), p_image->comps[compno].w * p_image->comps[compno].h, debug );
+                fclose( debug );
+#endif
                 p_j2k->m_output_image->comps[compno].data = NULL;
         }
 
         return OPJ_TRUE;
 }
 
-opj_bool opj_j2k_get_tile(      opj_j2k_t *p_j2k,
+OPJ_BOOL opj_j2k_get_tile(      opj_j2k_t *p_j2k,
                                                     opj_stream_private_t *p_stream,
                                                     opj_image_t* p_image,
                                                     opj_event_mgr_t * p_manager,
@@ -8938,13 +9478,13 @@ opj_bool opj_j2k_get_tile(      opj_j2k_t *p_j2k,
 
                 l_img_comp->factor = p_j2k->m_private_image->comps[compno].factor;
 
-                l_img_comp->x0 = opj_int_ceildiv(p_image->x0, l_img_comp->dx);
-                l_img_comp->y0 = opj_int_ceildiv(p_image->y0, l_img_comp->dy);
-                l_comp_x1 = opj_int_ceildiv(p_image->x1, l_img_comp->dx);
-                l_comp_y1 = opj_int_ceildiv(p_image->y1, l_img_comp->dy);
+                l_img_comp->x0 = (OPJ_UINT32)opj_int_ceildiv((OPJ_INT32)p_image->x0, (OPJ_INT32)l_img_comp->dx);
+                l_img_comp->y0 = (OPJ_UINT32)opj_int_ceildiv((OPJ_INT32)p_image->y0, (OPJ_INT32)l_img_comp->dy);
+                l_comp_x1 = opj_int_ceildiv((OPJ_INT32)p_image->x1, (OPJ_INT32)l_img_comp->dx);
+                l_comp_y1 = opj_int_ceildiv((OPJ_INT32)p_image->y1, (OPJ_INT32)l_img_comp->dy);
 
-                l_img_comp->w = opj_int_ceildivpow2(l_comp_x1, l_img_comp->factor) - opj_int_ceildivpow2(l_img_comp->x0, l_img_comp->factor);
-                l_img_comp->h = opj_int_ceildivpow2(l_comp_y1, l_img_comp->factor) - opj_int_ceildivpow2(l_img_comp->y0, l_img_comp->factor);
+                l_img_comp->w = (OPJ_UINT32)(opj_int_ceildivpow2(l_comp_x1, (OPJ_INT32)l_img_comp->factor) - opj_int_ceildivpow2((OPJ_INT32)l_img_comp->x0, (OPJ_INT32)l_img_comp->factor));
+                l_img_comp->h = (OPJ_UINT32)(opj_int_ceildivpow2(l_comp_y1, (OPJ_INT32)l_img_comp->factor) - opj_int_ceildivpow2((OPJ_INT32)l_img_comp->y0, (OPJ_INT32)l_img_comp->factor));
 
                 l_img_comp++;
         }
@@ -8960,7 +9500,7 @@ opj_bool opj_j2k_get_tile(      opj_j2k_t *p_j2k,
         }
         opj_copy_image_header(p_image, p_j2k->m_output_image);
 
-        p_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec = tile_index;
+        p_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec = (OPJ_INT32)tile_index;
 
         /* customization of the decoding */
         opj_j2k_setup_decoding_tile(p_j2k);
@@ -8987,7 +9527,7 @@ opj_bool opj_j2k_get_tile(      opj_j2k_t *p_j2k,
         return OPJ_TRUE;
 }
 
-opj_bool opj_j2k_set_decoded_resolution_factor(opj_j2k_t *p_j2k,
+OPJ_BOOL opj_j2k_set_decoded_resolution_factor(opj_j2k_t *p_j2k,
                                                OPJ_UINT32 res_factor,
                                                opj_event_mgr_t * p_manager)
 {
@@ -9016,9 +9556,9 @@ opj_bool opj_j2k_set_decoded_resolution_factor(opj_j2k_t *p_j2k,
         return OPJ_FALSE;
 }
 
-opj_bool opj_j2k_encode_v2(     opj_j2k_t * p_j2k,
-                                                opj_stream_private_t *p_stream,
-                                                opj_event_mgr_t * p_manager )
+OPJ_BOOL opj_j2k_encode(opj_j2k_t * p_j2k,
+                        opj_stream_private_t *p_stream,
+                        opj_event_mgr_t * p_manager )
 {
         OPJ_UINT32 i;
         OPJ_UINT32 l_nb_tiles;
@@ -9067,7 +9607,7 @@ opj_bool opj_j2k_encode_v2(     opj_j2k_t * p_j2k,
         return OPJ_TRUE;
 }
 
-opj_bool opj_j2k_end_compress(  opj_j2k_t *p_j2k,
+OPJ_BOOL opj_j2k_end_compress(  opj_j2k_t *p_j2k,
                                                         opj_stream_private_t *p_stream,
                                                         opj_event_mgr_t * p_manager)
 {
@@ -9082,7 +9622,7 @@ opj_bool opj_j2k_end_compress(  opj_j2k_t *p_j2k,
         return OPJ_TRUE;
 }
 
-opj_bool opj_j2k_start_compress(opj_j2k_t *p_j2k,
+OPJ_BOOL opj_j2k_start_compress(opj_j2k_t *p_j2k,
                                                             opj_stream_private_t *p_stream,
                                                             opj_image_t * p_image,
                                                             opj_event_mgr_t * p_manager)
@@ -9126,7 +9666,7 @@ opj_bool opj_j2k_start_compress(opj_j2k_t *p_j2k,
         return OPJ_TRUE;
 }
 
-opj_bool opj_j2k_pre_write_tile (       opj_j2k_t * p_j2k,
+OPJ_BOOL opj_j2k_pre_write_tile (       opj_j2k_t * p_j2k,
                                                                 OPJ_UINT32 p_tile_index,
                                                                 opj_stream_private_t *p_stream,
                                                                 opj_event_mgr_t * p_manager )
@@ -9175,13 +9715,13 @@ void opj_j2k_get_tile_data (opj_tcd_t * p_tcd, OPJ_BYTE * p_data)
                         l_size_comp = 4;
                 }
 
-                l_width (l_tilec->x1 - l_tilec->x0);
-                l_height = (l_tilec->y1 - l_tilec->y0);
-                l_offset_x = opj_int_ceildiv(l_image->x0, l_img_comp->dx);
-                l_offset_y = opj_int_ceildiv(l_image->y0, l_img_comp->dy);
-                l_image_width = opj_int_ceildiv(l_image->x1 - l_image->x0, l_img_comp->dx);
+                l_width  = (OPJ_UINT32)(l_tilec->x1 - l_tilec->x0);
+                l_height = (OPJ_UINT32)(l_tilec->y1 - l_tilec->y0);
+                l_offset_x = (OPJ_UINT32)opj_int_ceildiv((OPJ_INT32)l_image->x0, (OPJ_INT32)l_img_comp->dx);
+                l_offset_y = (OPJ_UINT32)opj_int_ceildiv((OPJ_INT32)l_image->y0, (OPJ_INT32)l_img_comp->dy);
+                l_image_width = (OPJ_UINT32)opj_int_ceildiv((OPJ_INT32)l_image->x1 - (OPJ_INT32)l_image->x0, (OPJ_INT32)l_img_comp->dx);
                 l_stride = l_image_width - l_width;
-                l_src_ptr = l_img_comp->data + (l_tilec->x0 - l_offset_x) + (l_tilec->y0 - l_offset_y) * l_image_width;
+                l_src_ptr = l_img_comp->data + ((OPJ_UINT32)l_tilec->x0 - l_offset_x) + ((OPJ_UINT32)l_tilec->y0 - l_offset_y) * l_image_width;
 
                 switch (l_size_comp) {
                         case 1:
@@ -9200,7 +9740,7 @@ void opj_j2k_get_tile_data (opj_tcd_t * p_tcd, OPJ_BYTE * p_data)
                                         else {
                                                 for (j=0;j<l_height;++j) {
                                                         for (k=0;k<l_width;++k) {
-                                                                *(l_dest_ptr) = (*l_src_ptr)&0xff;
+                                                                *(l_dest_ptr) = (OPJ_CHAR)((*l_src_ptr)&0xff);
                                                                 ++l_dest_ptr;
                                                                 ++l_src_ptr;
                                                         }
@@ -9225,7 +9765,7 @@ void opj_j2k_get_tile_data (opj_tcd_t * p_tcd, OPJ_BYTE * p_data)
                                         else {
                                                 for (j=0;j<l_height;++j) {
                                                         for (k=0;k<l_width;++k) {
-                                                                *(l_dest_ptr++) = (*(l_src_ptr++))&0xffff;
+                                                                *(l_dest_ptr++) = (OPJ_INT16)((*(l_src_ptr++)) & 0xffff);
                                                         }
                                                         l_src_ptr += l_stride;
                                                 }
@@ -9254,7 +9794,7 @@ void opj_j2k_get_tile_data (opj_tcd_t * p_tcd, OPJ_BYTE * p_data)
         }
 }
 
-opj_bool opj_j2k_post_write_tile (      opj_j2k_t * p_j2k,
+OPJ_BOOL opj_j2k_post_write_tile (      opj_j2k_t * p_j2k,
                                                                 OPJ_BYTE * p_data,
                                                                 OPJ_UINT32 p_data_size,
                                                                 opj_stream_private_t *p_stream,
@@ -9314,7 +9854,7 @@ void opj_j2k_setup_end_compress (opj_j2k_t *p_j2k)
         /* DEVELOPER CORNER, insert your custom procedures */
         opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)opj_j2k_write_eoc );
 
-        if (p_j2k->m_cp.m_specific_param.m_enc.m_cinema) {
+        if (OPJ_IS_CINEMA(p_j2k->m_cp.rsiz)) {
                 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)opj_j2k_write_updated_tlm);
         }
 
@@ -9346,11 +9886,14 @@ void opj_j2k_setup_header_writing (opj_j2k_t *p_j2k)
         opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)opj_j2k_write_cod );
         opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)opj_j2k_write_qcd );
 
-        if (p_j2k->m_cp.m_specific_param.m_enc.m_cinema) {
-                opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)opj_j2k_write_image_components );
+        if (OPJ_IS_CINEMA(p_j2k->m_cp.rsiz)) {
+                /* No need for COC or QCC, QCD and COD are used
+                opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)opj_j2k_write_all_coc );
+                opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)opj_j2k_write_all_qcc );
+                */
                 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)opj_j2k_write_tlm );
 
-                if (p_j2k->m_cp.m_specific_param.m_enc.m_cinema == OPJ_CINEMA4K_24) {
+                if (p_j2k->m_cp.rsiz == OPJ_PROFILE_CINEMA_4K) {
                         opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)opj_j2k_write_poc );
                 }
         }
@@ -9362,7 +9905,7 @@ void opj_j2k_setup_header_writing (opj_j2k_t *p_j2k)
         }
 
         /* DEVELOPER CORNER, insert your custom procedures */
-        if (p_j2k->m_cp.rsiz & OPJ_MCT) {
+        if (p_j2k->m_cp.rsiz & OPJ_EXTENSION_MCT) {
                 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)opj_j2k_write_mct_data_group );
         }
         /* End of Developer Corner */
@@ -9375,14 +9918,13 @@ void opj_j2k_setup_header_writing (opj_j2k_t *p_j2k)
         opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)opj_j2k_update_rates);
 }
 
-opj_bool opj_j2k_write_first_tile_part (opj_j2k_t *p_j2k,
+OPJ_BOOL opj_j2k_write_first_tile_part (opj_j2k_t *p_j2k,
                                                                         OPJ_BYTE * p_data,
                                                                         OPJ_UINT32 * p_data_written,
                                                                         OPJ_UINT32 p_total_data_size,
                                                                         opj_stream_private_t *p_stream,
                                                                         struct opj_event_mgr * p_manager )
 {
-        OPJ_UINT32 compno;
         OPJ_UINT32 l_nb_bytes_written = 0;
         OPJ_UINT32 l_current_nb_bytes_written;
         OPJ_BYTE * l_begin_data = 00;
@@ -9412,7 +9954,8 @@ opj_bool opj_j2k_write_first_tile_part (opj_j2k_t *p_j2k,
         p_data += l_current_nb_bytes_written;
         p_total_data_size -= l_current_nb_bytes_written;
 
-        if (l_cp->m_specific_param.m_enc.m_cinema == 0) {
+        if (!OPJ_IS_CINEMA(l_cp->rsiz)) {
+#if 0
                 for (compno = 1; compno < p_j2k->m_private_image->numcomps; compno++) {
                         l_current_nb_bytes_written = 0;
                         opj_j2k_write_coc_in_memory(p_j2k,compno,p_data,&l_current_nb_bytes_written,p_manager);
@@ -9426,6 +9969,7 @@ opj_bool opj_j2k_write_first_tile_part (opj_j2k_t *p_j2k,
                         p_data += l_current_nb_bytes_written;
                         p_total_data_size -= l_current_nb_bytes_written;
                 }
+#endif
 
                 if (l_cp->tcps[p_j2k->m_current_tile_number].numpocs) {
                         l_current_nb_bytes_written = 0;
@@ -9447,14 +9991,14 @@ opj_bool opj_j2k_write_first_tile_part (opj_j2k_t *p_j2k,
         /* Writing Psot in SOT marker */
         opj_write_bytes(l_begin_data + 6,l_nb_bytes_written,4);                                 /* PSOT */
 
-        if (l_cp->m_specific_param.m_enc.m_cinema){
+        if (OPJ_IS_CINEMA(l_cp->rsiz)){
                 opj_j2k_update_tlm(p_j2k,l_nb_bytes_written);
         }
 
         return OPJ_TRUE;
 }
 
-opj_bool opj_j2k_write_all_tile_parts(  opj_j2k_t *p_j2k,
+OPJ_BOOL opj_j2k_write_all_tile_parts(  opj_j2k_t *p_j2k,
                                                                         OPJ_BYTE * p_data,
                                                                         OPJ_UINT32 * p_data_written,
                                                                         OPJ_UINT32 p_total_data_size,
@@ -9481,6 +10025,8 @@ opj_bool opj_j2k_write_all_tile_parts(  opj_j2k_t *p_j2k,
         /*Get number of tile parts*/
         tot_num_tp = opj_j2k_get_num_tp(l_cp,0,p_j2k->m_current_tile_number);
 
+        /* start writing remaining tile parts */
+        ++p_j2k->m_specific_param.m_encoder.m_current_tile_part_number;
         for (tilepartno = 1; tilepartno < tot_num_tp ; ++tilepartno) {
                 p_j2k->m_specific_param.m_encoder.m_current_poc_tile_part_number = tilepartno;
                 l_current_nb_bytes_written = 0;
@@ -9494,7 +10040,7 @@ opj_bool opj_j2k_write_all_tile_parts(  opj_j2k_t *p_j2k,
                 l_nb_bytes_written += l_current_nb_bytes_written;
                 p_data += l_current_nb_bytes_written;
                 p_total_data_size -= l_current_nb_bytes_written;
-                l_part_tile_size += l_nb_bytes_written;
+                l_part_tile_size += l_current_nb_bytes_written;
 
                 l_current_nb_bytes_written = 0;
                 if (! opj_j2k_write_sod(p_j2k,l_tcd,p_data,&l_current_nb_bytes_written,p_total_data_size,p_stream,p_manager)) {
@@ -9504,12 +10050,12 @@ opj_bool opj_j2k_write_all_tile_parts(  opj_j2k_t *p_j2k,
                 p_data += l_current_nb_bytes_written;
                 l_nb_bytes_written += l_current_nb_bytes_written;
                 p_total_data_size -= l_current_nb_bytes_written;
-                l_part_tile_size += l_nb_bytes_written;
+                l_part_tile_size += l_current_nb_bytes_written;
 
                 /* Writing Psot in SOT marker */
                 opj_write_bytes(l_begin_data + 6,l_part_tile_size,4);                                   /* PSOT */
 
-                if (l_cp->m_specific_param.m_enc.m_cinema) {
+                if (OPJ_IS_CINEMA(l_cp->rsiz)) {
                         opj_j2k_update_tlm(p_j2k,l_part_tile_size);
                 }
 
@@ -9550,7 +10096,7 @@ opj_bool opj_j2k_write_all_tile_parts(  opj_j2k_t *p_j2k,
                         /* Writing Psot in SOT marker */
                         opj_write_bytes(l_begin_data + 6,l_part_tile_size,4);                                   /* PSOT */
 
-                        if (l_cp->m_specific_param.m_enc.m_cinema) {
+                        if (OPJ_IS_CINEMA(l_cp->rsiz)) {
                                 opj_j2k_update_tlm(p_j2k,l_part_tile_size);
                         }
 
@@ -9563,7 +10109,7 @@ opj_bool opj_j2k_write_all_tile_parts(  opj_j2k_t *p_j2k,
         return OPJ_TRUE;
 }
 
-opj_bool opj_j2k_write_updated_tlm( opj_j2k_t *p_j2k,
+OPJ_BOOL opj_j2k_write_updated_tlm( opj_j2k_t *p_j2k,
                                                                     struct opj_stream_private *p_stream,
                                                                     struct opj_event_mgr * p_manager )
 {
@@ -9594,7 +10140,7 @@ opj_bool opj_j2k_write_updated_tlm( opj_j2k_t *p_j2k,
         return OPJ_TRUE;
 }
 
-opj_bool opj_j2k_end_encoding(  opj_j2k_t *p_j2k,
+OPJ_BOOL opj_j2k_end_encoding(  opj_j2k_t *p_j2k,
                                                         struct opj_stream_private *p_stream,
                                                         struct opj_event_mgr * p_manager )
 {
@@ -9625,7 +10171,7 @@ opj_bool opj_j2k_end_encoding(  opj_j2k_t *p_j2k,
 /**
  * Destroys the memory associated with the decoding of headers.
  */
-static opj_bool opj_j2k_destroy_header_memory ( opj_j2k_t * p_j2k,
+static OPJ_BOOL opj_j2k_destroy_header_memory ( opj_j2k_t * p_j2k,
                                                 opj_stream_private_t *p_stream,
                                                 opj_event_mgr_t * p_manager
                                                 )
@@ -9645,7 +10191,7 @@ static opj_bool opj_j2k_destroy_header_memory ( opj_j2k_t * p_j2k,
         return OPJ_TRUE;
 }
 
-opj_bool opj_j2k_init_info(     opj_j2k_t *p_j2k,
+OPJ_BOOL opj_j2k_init_info(     opj_j2k_t *p_j2k,
                                                 struct opj_stream_private *p_stream,
                                                 struct opj_event_mgr * p_manager )
 {
@@ -9706,7 +10252,7 @@ opj_bool opj_j2k_init_info(     opj_j2k_t *p_j2k,
  * @param       p_j2k                   J2K codec.
  * @param       p_manager               the user event manager.
 */
-static opj_bool opj_j2k_create_tcd(     opj_j2k_t *p_j2k,
+static OPJ_BOOL opj_j2k_create_tcd(     opj_j2k_t *p_j2k,
                                                                     opj_stream_private_t *p_stream,
                                                                     opj_event_mgr_t * p_manager
                                     )
@@ -9732,7 +10278,7 @@ static opj_bool opj_j2k_create_tcd(     opj_j2k_t *p_j2k,
         return OPJ_TRUE;
 }
 
-opj_bool opj_j2k_write_tile (opj_j2k_t * p_j2k,
+OPJ_BOOL opj_j2k_write_tile (opj_j2k_t * p_j2k,
                                                  OPJ_UINT32 p_tile_index,
                                                  OPJ_BYTE * p_data,
                                                  OPJ_UINT32 p_data_size,