Fix unsigned int overflow reported by UBSan (#761)
[openjpeg.git] / src / lib / openjp2 / pi.c
index ea8a22853f8ad58f9bcc005ccc405bf9cfea38bf..cffad6684f0fc1f7521461c15c5135c19fb57bc6 100644 (file)
@@ -1,9 +1,15 @@
 /*
- * 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) 2006-2007, Parvatha Elangovan
  * All rights reserved.
@@ -43,31 +49,31 @@ Get next packet in layer-resolution-component-precinct order.
 @param pi packet iterator to modify
 @return returns false if pi pointed to the last packet or else returns true
 */
-static opj_bool opj_pi_next_lrcp(opj_pi_iterator_t * pi);
+static OPJ_BOOL opj_pi_next_lrcp(opj_pi_iterator_t * pi);
 /**
 Get next packet in resolution-layer-component-precinct order.
 @param pi packet iterator to modify
 @return returns false if pi pointed to the last packet or else returns true
 */
-static opj_bool opj_pi_next_rlcp(opj_pi_iterator_t * pi);
+static OPJ_BOOL opj_pi_next_rlcp(opj_pi_iterator_t * pi);
 /**
 Get next packet in resolution-precinct-component-layer order.
 @param pi packet iterator to modify
 @return returns false if pi pointed to the last packet or else returns true
 */
-static opj_bool opj_pi_next_rpcl(opj_pi_iterator_t * pi);
+static OPJ_BOOL opj_pi_next_rpcl(opj_pi_iterator_t * pi);
 /**
 Get next packet in precinct-component-resolution-layer order.
 @param pi packet iterator to modify
 @return returns false if pi pointed to the last packet or else returns true
 */
-static opj_bool opj_pi_next_pcrl(opj_pi_iterator_t * pi);
+static OPJ_BOOL opj_pi_next_pcrl(opj_pi_iterator_t * pi);
 /**
 Get next packet in component-precinct-resolution-layer order.
 @param pi packet iterator to modify
 @return returns false if pi pointed to the last packet or else returns true
 */
-static opj_bool opj_pi_next_cprl(opj_pi_iterator_t * pi);
+static OPJ_BOOL opj_pi_next_cprl(opj_pi_iterator_t * pi);
 
 /**
  * Updates the coding parameters if the encoding is used with Progression order changes and final (or cinema parameters are used).
@@ -98,6 +104,7 @@ static void opj_pi_update_encode_poc_and_final ( opj_cp_t *p_cp,
  * Updates the coding parameters if the encoding is not used with Progression order changes and final (and cinema parameters are used).
  *
  * @param      p_cp            the coding parameters to modify
+ * @param      p_num_comps             the number of components
  * @param      p_tileno        the tile index being concerned.
  * @param      p_tx0           X0 parameter for the tile
  * @param      p_tx1           X1 parameter for the tile
@@ -105,8 +112,8 @@ static void opj_pi_update_encode_poc_and_final ( opj_cp_t *p_cp,
  * @param      p_ty1           Y1 parameter for the tile
  * @param      p_max_prec      the maximum precision for all the bands of the tile
  * @param      p_max_res       the maximum number of resolutions for all the poc inside the tile.
- * @param      dx_min          the minimum dx of all the components of all the resolutions for the tile.
- * @param      dy_min          the minimum dy of all the components of all the resolutions for the tile.
+ * @param      p_dx_min                the minimum dx of all the components of all the resolutions for the tile.
+ * @param      p_dy_min                the minimum dy of all the components of all the resolutions for the tile.
  */
 static void opj_pi_update_encode_not_poc (  opj_cp_t *p_cp,
                                             OPJ_UINT32 p_num_comps,
@@ -124,15 +131,15 @@ static void opj_pi_update_encode_not_poc (  opj_cp_t *p_cp,
  * 
  * @param      p_image                 the image being encoded.
  * @param      p_cp                    the coding parameters.
- * @param      p_tileno                        the tile index of the tile being encoded.
+ * @param      tileno                  the tile index of the tile being encoded.
  * @param      p_tx0                   pointer that will hold the X0 parameter for the tile
  * @param      p_tx1                   pointer that will hold the X1 parameter for the tile
  * @param      p_ty0                   pointer that will hold the Y0 parameter for the tile
  * @param      p_ty1                   pointer that will hold the Y1 parameter for the tile
- * @param      p_max_prec              pointer that will hold the the maximum precision for all the bands of the tile
- * @param      p_max_res               pointer that will hold the the maximum number of resolutions for all the poc inside the tile.
- * @param      dx_min                  pointer that will hold the the minimum dx of all the components of all the resolutions for the tile.
- * @param      dy_min                  pointer that will hold the the minimum dy of all the components of all the resolutions for the tile.
+ * @param      p_max_prec              pointer that will hold the maximum precision for all the bands of the tile
+ * @param      p_max_res               pointer that will hold the maximum number of resolutions for all the poc inside the tile.
+ * @param      p_dx_min                        pointer that will hold the minimum dx of all the components of all the resolutions for the tile.
+ * @param      p_dy_min                        pointer that will hold the minimum dy of all the components of all the resolutions for the tile.
  */
 static void opj_get_encoding_parameters(const opj_image_t *p_image,
                                         const opj_cp_t *p_cp,
@@ -160,10 +167,10 @@ static void opj_get_encoding_parameters(const opj_image_t *p_image,
  * @param      p_tx1                   pointer that will hold the X1 parameter for the tile
  * @param      p_ty0                   pointer that will hold the Y0 parameter for the tile
  * @param      p_ty1                   pointer that will hold the Y1 parameter for the tile
- * @param      p_max_prec              pointer that will hold the the maximum precision for all the bands of the tile
- * @param      p_max_res               pointer that will hold the the maximum number of resolutions for all the poc inside the tile.
- * @param      p_dx_min                pointer that will hold the the minimum dx of all the components of all the resolutions for the tile.
- * @param      p_dy_min                pointer that will hold the the minimum dy of all the components of all the resolutions for the tile.
+ * @param      p_max_prec              pointer that will hold the maximum precision for all the bands of the tile
+ * @param      p_max_res               pointer that will hold the maximum number of resolutions for all the poc inside the tile.
+ * @param      p_dx_min                pointer that will hold the minimum dx of all the components of all the resolutions for the tile.
+ * @param      p_dy_min                pointer that will hold the minimum dy of all the components of all the resolutions for the tile.
  * @param      p_resolutions   pointer to an area corresponding to the one described above.
  */
 static void opj_get_all_encoding_parameters(const opj_image_t *p_image,
@@ -184,10 +191,10 @@ static void opj_get_all_encoding_parameters(const opj_image_t *p_image,
  * 
  * @param      p_image         the image used to initialize the packet iterator (in fact only the number of components is relevant.
  * @param      p_cp            the coding parameters.
- * @param      p_tile_no       the index of the tile from which creating the packet iterator.
+ * @param      tileno  the index of the tile from which creating the packet iterator.
  */
-static opj_pi_iterator_t * opj_pi_create(      const opj_image_t *image,
-                                            const opj_cp_t *cp,
+static opj_pi_iterator_t * opj_pi_create(      const opj_image_t *p_image,
+                                            const opj_cp_t *p_cp,
                                             OPJ_UINT32 tileno );
 /**
  * FIXME DOC
@@ -207,7 +214,7 @@ static void opj_pi_update_decode_poc (  opj_pi_iterator_t * p_pi,
 /**
  * FIXME DOC
  */
-opj_bool opj_pi_check_next_level(      OPJ_INT32 pos,
+static OPJ_BOOL opj_pi_check_next_level(       OPJ_INT32 pos,
                                                                opj_cp_t *cp,
                                                                OPJ_UINT32 tileno,
                                                                OPJ_UINT32 pino,
@@ -223,7 +230,7 @@ opj_bool opj_pi_check_next_level(   OPJ_INT32 pos,
 ==========================================================
 */
 
-opj_bool opj_pi_next_lrcp(opj_pi_iterator_t * pi) {
+static OPJ_BOOL opj_pi_next_lrcp(opj_pi_iterator_t * pi) {
        opj_pi_comp_t *comp = NULL;
        opj_pi_resolution_t *res = NULL;
        OPJ_UINT32 index = 0;
@@ -263,7 +270,7 @@ LABEL_SKIP:;
        return OPJ_FALSE;
 }
 
-opj_bool opj_pi_next_rlcp(opj_pi_iterator_t * pi) {
+static OPJ_BOOL opj_pi_next_rlcp(opj_pi_iterator_t * pi) {
        opj_pi_comp_t *comp = NULL;
        opj_pi_resolution_t *res = NULL;
        OPJ_UINT32 index = 0;
@@ -302,7 +309,7 @@ LABEL_SKIP:;
        return OPJ_FALSE;
 }
 
-opj_bool opj_pi_next_rpcl(opj_pi_iterator_t * pi) {
+static OPJ_BOOL opj_pi_next_rpcl(opj_pi_iterator_t * pi) {
        opj_pi_comp_t *comp = NULL;
        opj_pi_resolution_t *res = NULL;
        OPJ_UINT32 index = 0;
@@ -319,10 +326,10 @@ opj_bool opj_pi_next_rpcl(opj_pi_iterator_t * pi) {
                        for (resno = 0; resno < comp->numresolutions; resno++) {
                                OPJ_UINT32 dx, dy;
                                res = &comp->resolutions[resno];
-                               dx = comp->dx * (1 << (res->pdx + comp->numresolutions - 1 - resno));
-                               dy = comp->dy * (1 << (res->pdy + comp->numresolutions - 1 - resno));
-                               pi->dx = !pi->dx ? dx : opj_int_min(pi->dx, dx);
-                               pi->dy = !pi->dy ? dy : opj_int_min(pi->dy, dy);
+                               dx = comp->dx * (1u << (res->pdx + comp->numresolutions - 1 - resno));
+                               dy = comp->dy * (1u << (res->pdy + comp->numresolutions - 1 - resno));
+                               pi->dx = !pi->dx ? dx : opj_uint_min(pi->dx, dx);
+                               pi->dy = !pi->dy ? dy : opj_uint_min(pi->dy, dy);
                        }
                }
        }
@@ -333,8 +340,8 @@ if (!pi->tp_on){
                        pi->poc.tx1 = pi->tx1;
                }
        for (pi->resno = pi->poc.resno0; pi->resno < pi->poc.resno1; pi->resno++) {
-               for (pi->y = pi->poc.ty0; pi->y < pi->poc.ty1; pi->y += pi->dy - (pi->y % pi->dy)) {
-                       for (pi->x = pi->poc.tx0; pi->x < pi->poc.tx1; pi->x += pi->dx - (pi->x % pi->dx)) {
+               for (pi->y = pi->poc.ty0; pi->y < pi->poc.ty1; pi->y += (OPJ_INT32)(pi->dy - (OPJ_UINT32)(pi->y % (OPJ_INT32)pi->dy))) {
+                       for (pi->x = pi->poc.tx0; pi->x < pi->poc.tx1; pi->x += (OPJ_INT32)(pi->dx - (OPJ_UINT32)(pi->x % (OPJ_INT32)pi->dx))) {
                                for (pi->compno = pi->poc.compno0; pi->compno < pi->poc.compno1; pi->compno++) {
                                        OPJ_UINT32 levelno;
                                        OPJ_INT32 trx0, try0;
@@ -347,16 +354,16 @@ if (!pi->tp_on){
                                        }
                                        res = &comp->resolutions[pi->resno];
                                        levelno = comp->numresolutions - 1 - pi->resno;
-                                       trx0 = opj_int_ceildiv(pi->tx0, comp->dx << levelno);
-                                       try0 = opj_int_ceildiv(pi->ty0, comp->dy << levelno);
-                                       trx1 = opj_int_ceildiv(pi->tx1, comp->dx << levelno);
-                                       try1 = opj_int_ceildiv(pi->ty1, comp->dy << levelno);
+                                       trx0 = opj_int_ceildiv(pi->tx0, (OPJ_INT32)(comp->dx << levelno));
+                                       try0 = opj_int_ceildiv(pi->ty0, (OPJ_INT32)(comp->dy << levelno));
+                                       trx1 = opj_int_ceildiv(pi->tx1, (OPJ_INT32)(comp->dx << levelno));
+                                       try1 = opj_int_ceildiv(pi->ty1, (OPJ_INT32)(comp->dy << levelno));
                                        rpx = res->pdx + levelno;
                                        rpy = res->pdy + levelno;
-                                       if (!((pi->y % (comp->dy << rpy) == 0) || ((pi->y == pi->ty0) && ((try0 << levelno) % (1 << rpy))))){
+                                       if (!((pi->y % (OPJ_INT32)(comp->dy << rpy) == 0) || ((pi->y == pi->ty0) && ((try0 << levelno) % (1 << rpy))))){
                                                continue;       
                                        }
-                                       if (!((pi->x % (comp->dx << rpx) == 0) || ((pi->x == pi->tx0) && ((trx0 << levelno) % (1 << rpx))))){
+                                       if (!((pi->x % (OPJ_INT32)(comp->dx << rpx) == 0) || ((pi->x == pi->tx0) && ((trx0 << levelno) % (1 << rpx))))){
                                                continue;
                                        }
                                        
@@ -364,11 +371,11 @@ if (!pi->tp_on){
                                        
                                        if ((trx0==trx1)||(try0==try1)) continue;
                                        
-                                       prci = opj_int_floordivpow2(opj_int_ceildiv(pi->x, comp->dx << levelno), res->pdx)
-                                                - opj_int_floordivpow2(trx0, res->pdx);
-                                       prcj = opj_int_floordivpow2(opj_int_ceildiv(pi->y, comp->dy << levelno), res->pdy)
-                                                - opj_int_floordivpow2(try0, res->pdy);
-                                       pi->precno = prci + prcj * res->pw;
+                                       prci = opj_int_floordivpow2(opj_int_ceildiv(pi->x, (OPJ_INT32)(comp->dx << levelno)), (OPJ_INT32)res->pdx)
+                                                - opj_int_floordivpow2(trx0, (OPJ_INT32)res->pdx);
+                                       prcj = opj_int_floordivpow2(opj_int_ceildiv(pi->y, (OPJ_INT32)(comp->dy << levelno)), (OPJ_INT32)res->pdy)
+                                                - opj_int_floordivpow2(try0, (OPJ_INT32)res->pdy);
+                                       pi->precno = (OPJ_UINT32)(prci + prcj * (OPJ_INT32)res->pw);
                                        for (pi->layno = pi->poc.layno0; pi->layno < pi->poc.layno1; pi->layno++) {
                                                index = pi->layno * pi->step_l + pi->resno * pi->step_r + pi->compno * pi->step_c + pi->precno * pi->step_p;
                                                if (!pi->include[index]) {
@@ -385,7 +392,7 @@ LABEL_SKIP:;
        return OPJ_FALSE;
 }
 
-opj_bool opj_pi_next_pcrl(opj_pi_iterator_t * pi) {
+static OPJ_BOOL opj_pi_next_pcrl(opj_pi_iterator_t * pi) {
        opj_pi_comp_t *comp = NULL;
        opj_pi_resolution_t *res = NULL;
        OPJ_UINT32 index = 0;
@@ -403,10 +410,10 @@ opj_bool opj_pi_next_pcrl(opj_pi_iterator_t * pi) {
                        for (resno = 0; resno < comp->numresolutions; resno++) {
                                OPJ_UINT32 dx, dy;
                                res = &comp->resolutions[resno];
-                               dx = comp->dx * (1 << (res->pdx + comp->numresolutions - 1 - resno));
-                               dy = comp->dy * (1 << (res->pdy + comp->numresolutions - 1 - resno));
-                               pi->dx = !pi->dx ? dx : opj_int_min(pi->dx, dx);
-                               pi->dy = !pi->dy ? dy : opj_int_min(pi->dy, dy);
+                               dx = comp->dx * (1u << (res->pdx + comp->numresolutions - 1 - resno));
+                               dy = comp->dy * (1u << (res->pdy + comp->numresolutions - 1 - resno));
+                               pi->dx = !pi->dx ? dx : opj_uint_min(pi->dx, dx);
+                               pi->dy = !pi->dy ? dy : opj_uint_min(pi->dy, dy);
                        }
                }
        }
@@ -416,8 +423,8 @@ opj_bool opj_pi_next_pcrl(opj_pi_iterator_t * pi) {
                        pi->poc.ty1 = pi->ty1;
                        pi->poc.tx1 = pi->tx1;
                }
-       for (pi->y = pi->poc.ty0; pi->y < pi->poc.ty1; pi->y += pi->dy - (pi->y % pi->dy)) {
-               for (pi->x = pi->poc.tx0; pi->x < pi->poc.tx1; pi->x += pi->dx - (pi->x % pi->dx)) {
+       for (pi->y = pi->poc.ty0; pi->y < pi->poc.ty1; pi->y += (OPJ_INT32)(pi->dy - (OPJ_UINT32)(pi->y % (OPJ_INT32)pi->dy))) {
+               for (pi->x = pi->poc.tx0; pi->x < pi->poc.tx1; pi->x += (OPJ_INT32)(pi->dx - (OPJ_UINT32)(pi->x % (OPJ_INT32)pi->dx))) {
                        for (pi->compno = pi->poc.compno0; pi->compno < pi->poc.compno1; pi->compno++) {
                                comp = &pi->comps[pi->compno];
                                for (pi->resno = pi->poc.resno0; pi->resno < opj_uint_min(pi->poc.resno1, comp->numresolutions); pi->resno++) {
@@ -428,16 +435,16 @@ opj_bool opj_pi_next_pcrl(opj_pi_iterator_t * pi) {
                                        OPJ_INT32 prci, prcj;
                                        res = &comp->resolutions[pi->resno];
                                        levelno = comp->numresolutions - 1 - pi->resno;
-                                       trx0 = opj_int_ceildiv(pi->tx0, comp->dx << levelno);
-                                       try0 = opj_int_ceildiv(pi->ty0, comp->dy << levelno);
-                                       trx1 = opj_int_ceildiv(pi->tx1, comp->dx << levelno);
-                                       try1 = opj_int_ceildiv(pi->ty1, comp->dy << levelno);
+                                       trx0 = opj_int_ceildiv(pi->tx0, (OPJ_INT32)(comp->dx << levelno));
+                                       try0 = opj_int_ceildiv(pi->ty0, (OPJ_INT32)(comp->dy << levelno));
+                                       trx1 = opj_int_ceildiv(pi->tx1, (OPJ_INT32)(comp->dx << levelno));
+                                       try1 = opj_int_ceildiv(pi->ty1, (OPJ_INT32)(comp->dy << levelno));
                                        rpx = res->pdx + levelno;
                                        rpy = res->pdy + levelno;
-                                       if (!((pi->y % (comp->dy << rpy) == 0) || ((pi->y == pi->ty0) && ((try0 << levelno) % (1 << rpy))))){
+                                       if (!((pi->y % (OPJ_INT32)(comp->dy << rpy) == 0) || ((pi->y == pi->ty0) && ((try0 << levelno) % (1 << rpy))))){
                                                continue;       
                                        }
-                                       if (!((pi->x % (comp->dx << rpx) == 0) || ((pi->x == pi->tx0) && ((trx0 << levelno) % (1 << rpx))))){
+                                       if (!((pi->x % (OPJ_INT32)(comp->dx << rpx) == 0) || ((pi->x == pi->tx0) && ((trx0 << levelno) % (1 << rpx))))){
                                                continue;
                                        }
                                        
@@ -445,11 +452,11 @@ opj_bool opj_pi_next_pcrl(opj_pi_iterator_t * pi) {
                                        
                                        if ((trx0==trx1)||(try0==try1)) continue;
                                        
-                                       prci = opj_int_floordivpow2(opj_int_ceildiv(pi->x, comp->dx << levelno), res->pdx)
-                                                - opj_int_floordivpow2(trx0, res->pdx);
-                                       prcj = opj_int_floordivpow2(opj_int_ceildiv(pi->y, comp->dy << levelno), res->pdy)
-                                                - opj_int_floordivpow2(try0, res->pdy);
-                                       pi->precno = prci + prcj * res->pw;
+                                       prci = opj_int_floordivpow2(opj_int_ceildiv(pi->x, (OPJ_INT32)(comp->dx << levelno)), (OPJ_INT32)res->pdx)
+                                                - opj_int_floordivpow2(trx0, (OPJ_INT32)res->pdx);
+                                       prcj = opj_int_floordivpow2(opj_int_ceildiv(pi->y, (OPJ_INT32)(comp->dy << levelno)), (OPJ_INT32)res->pdy)
+                                                - opj_int_floordivpow2(try0, (OPJ_INT32)res->pdy);
+                                       pi->precno = (OPJ_UINT32)(prci + prcj * (OPJ_INT32)res->pw);
                                        for (pi->layno = pi->poc.layno0; pi->layno < pi->poc.layno1; pi->layno++) {
                                                index = pi->layno * pi->step_l + pi->resno * pi->step_r + pi->compno * pi->step_c + pi->precno * pi->step_p;
                                                if (!pi->include[index]) {
@@ -466,7 +473,7 @@ LABEL_SKIP:;
        return OPJ_FALSE;
 }
 
-opj_bool opj_pi_next_cprl(opj_pi_iterator_t * pi) {
+static OPJ_BOOL opj_pi_next_cprl(opj_pi_iterator_t * pi) {
        opj_pi_comp_t *comp = NULL;
        opj_pi_resolution_t *res = NULL;
        OPJ_UINT32 index = 0;
@@ -486,10 +493,10 @@ opj_bool opj_pi_next_cprl(opj_pi_iterator_t * pi) {
                for (resno = 0; resno < comp->numresolutions; resno++) {
                        OPJ_UINT32 dx, dy;
                        res = &comp->resolutions[resno];
-                       dx = comp->dx * (1 << (res->pdx + comp->numresolutions - 1 - resno));
-                       dy = comp->dy * (1 << (res->pdy + comp->numresolutions - 1 - resno));
-                       pi->dx = !pi->dx ? dx : opj_int_min(pi->dx, dx);
-                       pi->dy = !pi->dy ? dy : opj_int_min(pi->dy, dy);
+                       dx = comp->dx * (1u << (res->pdx + comp->numresolutions - 1 - resno));
+                       dy = comp->dy * (1u << (res->pdy + comp->numresolutions - 1 - resno));
+                       pi->dx = !pi->dx ? dx : opj_uint_min(pi->dx, dx);
+                       pi->dy = !pi->dy ? dy : opj_uint_min(pi->dy, dy);
                }
                if (!pi->tp_on){
                        pi->poc.ty0 = pi->ty0;
@@ -497,8 +504,8 @@ opj_bool opj_pi_next_cprl(opj_pi_iterator_t * pi) {
                        pi->poc.ty1 = pi->ty1;
                        pi->poc.tx1 = pi->tx1;
                }
-               for (pi->y = pi->poc.ty0; pi->y < pi->poc.ty1; pi->y += pi->dy - (pi->y % pi->dy)) {
-                       for (pi->x = pi->poc.tx0; pi->x < pi->poc.tx1; pi->x += pi->dx - (pi->x % pi->dx)) {
+               for (pi->y = pi->poc.ty0; pi->y < pi->poc.ty1; pi->y += (OPJ_INT32)(pi->dy - (OPJ_UINT32)(pi->y % (OPJ_INT32)pi->dy))) {
+                       for (pi->x = pi->poc.tx0; pi->x < pi->poc.tx1; pi->x += (OPJ_INT32)(pi->dx - (OPJ_UINT32)(pi->x % (OPJ_INT32)pi->dx))) {
                                for (pi->resno = pi->poc.resno0; pi->resno < opj_uint_min(pi->poc.resno1, comp->numresolutions); pi->resno++) {
                                        OPJ_UINT32 levelno;
                                        OPJ_INT32 trx0, try0;
@@ -507,16 +514,16 @@ opj_bool opj_pi_next_cprl(opj_pi_iterator_t * pi) {
                                        OPJ_INT32 prci, prcj;
                                        res = &comp->resolutions[pi->resno];
                                        levelno = comp->numresolutions - 1 - pi->resno;
-                                       trx0 = opj_int_ceildiv(pi->tx0, comp->dx << levelno);
-                                       try0 = opj_int_ceildiv(pi->ty0, comp->dy << levelno);
-                                       trx1 = opj_int_ceildiv(pi->tx1, comp->dx << levelno);
-                                       try1 = opj_int_ceildiv(pi->ty1, comp->dy << levelno);
+                                       trx0 = opj_int_ceildiv(pi->tx0, (OPJ_INT32)(comp->dx << levelno));
+                                       try0 = opj_int_ceildiv(pi->ty0, (OPJ_INT32)(comp->dy << levelno));
+                                       trx1 = opj_int_ceildiv(pi->tx1, (OPJ_INT32)(comp->dx << levelno));
+                                       try1 = opj_int_ceildiv(pi->ty1, (OPJ_INT32)(comp->dy << levelno));
                                        rpx = res->pdx + levelno;
                                        rpy = res->pdy + levelno;
-                                       if (!((pi->y % (comp->dy << rpy) == 0) || ((pi->y == pi->ty0) && ((try0 << levelno) % (1 << rpy))))){
+                                       if (!((pi->y % (OPJ_INT32)(comp->dy << rpy) == 0) || ((pi->y == pi->ty0) && ((try0 << levelno) % (1 << rpy))))){
                                                continue;       
                                        }
-                                       if (!((pi->x % (comp->dx << rpx) == 0) || ((pi->x == pi->tx0) && ((trx0 << levelno) % (1 << rpx))))){
+                                       if (!((pi->x % (OPJ_INT32)(comp->dx << rpx) == 0) || ((pi->x == pi->tx0) && ((trx0 << levelno) % (1 << rpx))))){
                                                continue;
                                        }
                                        
@@ -524,11 +531,11 @@ opj_bool opj_pi_next_cprl(opj_pi_iterator_t * pi) {
                                        
                                        if ((trx0==trx1)||(try0==try1)) continue;
                                        
-                                       prci = opj_int_floordivpow2(opj_int_ceildiv(pi->x, comp->dx << levelno), res->pdx)
-                                                - opj_int_floordivpow2(trx0, res->pdx);
-                                       prcj = opj_int_floordivpow2(opj_int_ceildiv(pi->y, comp->dy << levelno), res->pdy)
-                                                - opj_int_floordivpow2(try0, res->pdy);
-                                       pi->precno = prci + prcj * res->pw;
+                                       prci = opj_int_floordivpow2(opj_int_ceildiv(pi->x, (OPJ_INT32)(comp->dx << levelno)), (OPJ_INT32)res->pdx)
+                                                - opj_int_floordivpow2(trx0, (OPJ_INT32)res->pdx);
+                                       prcj = opj_int_floordivpow2(opj_int_ceildiv(pi->y, (OPJ_INT32)(comp->dy << levelno)), (OPJ_INT32)res->pdy)
+                                                - opj_int_floordivpow2(try0, (OPJ_INT32)res->pdy);
+                                       pi->precno = (OPJ_UINT32)(prci + prcj * (OPJ_INT32)res->pw);
                                        for (pi->layno = pi->poc.layno0; pi->layno < pi->poc.layno1; pi->layno++) {
                                                index = pi->layno * pi->step_l + pi->resno * pi->step_r + pi->compno * pi->step_c + pi->precno * pi->step_p;
                                                if (!pi->include[index]) {
@@ -545,7 +552,7 @@ LABEL_SKIP:;
        return OPJ_FALSE;
 }
 
-void opj_get_encoding_parameters(      const opj_image_t *p_image,
+static void opj_get_encoding_parameters(       const opj_image_t *p_image,
                                     const opj_cp_t *p_cp,
                                     OPJ_UINT32 p_tileno,
                                     OPJ_INT32 * p_tx0,
@@ -582,10 +589,10 @@ void opj_get_encoding_parameters( const opj_image_t *p_image,
        q = p_tileno / p_cp->tw;
 
        /* find extent of tile */
-       *p_tx0 = opj_int_max(p_cp->tx0 + p * p_cp->tdx, p_image->x0);
-       *p_tx1 = opj_int_min(p_cp->tx0 + (p + 1) * p_cp->tdx, p_image->x1);
-       *p_ty0 = opj_int_max(p_cp->ty0 + q * p_cp->tdy, p_image->y0);
-       *p_ty1 = opj_int_min(p_cp->ty0 + (q + 1) * p_cp->tdy, p_image->y1);
+       *p_tx0 = opj_int_max((OPJ_INT32)(p_cp->tx0 + p * p_cp->tdx), (OPJ_INT32)p_image->x0);
+       *p_tx1 = opj_int_min((OPJ_INT32)(p_cp->tx0 + (p + 1) * p_cp->tdx), (OPJ_INT32)p_image->x1);
+       *p_ty0 = opj_int_max((OPJ_INT32)(p_cp->ty0 + q * p_cp->tdy), (OPJ_INT32)p_image->y0);
+       *p_ty1 = opj_int_min((OPJ_INT32)(p_cp->ty0 + (q + 1) * p_cp->tdy), (OPJ_INT32)p_image->y1);
 
        /* max precision is 0 (can only grow) */
        *p_max_prec = 0;
@@ -605,10 +612,10 @@ void opj_get_encoding_parameters( const opj_image_t *p_image,
                OPJ_UINT32 l_product;
                OPJ_INT32 l_tcx0, l_tcy0, l_tcx1, l_tcy1;
 
-               l_tcx0 = opj_int_ceildiv(*p_tx0, l_img_comp->dx);
-               l_tcy0 = opj_int_ceildiv(*p_ty0, l_img_comp->dy);
-               l_tcx1 = opj_int_ceildiv(*p_tx1, l_img_comp->dx);
-               l_tcy1 = opj_int_ceildiv(*p_ty1, l_img_comp->dy);
+               l_tcx0 = opj_int_ceildiv(*p_tx0, (OPJ_INT32)l_img_comp->dx);
+               l_tcy0 = opj_int_ceildiv(*p_ty0, (OPJ_INT32)l_img_comp->dy);
+               l_tcx1 = opj_int_ceildiv(*p_tx1, (OPJ_INT32)l_img_comp->dx);
+               l_tcy1 = opj_int_ceildiv(*p_ty1, (OPJ_INT32)l_img_comp->dy);
 
                if (l_tccp->numresolutions > *p_max_res) {
                        *p_max_res = l_tccp->numresolutions;
@@ -622,8 +629,8 @@ void opj_get_encoding_parameters(   const opj_image_t *p_image,
                        l_pdx = l_tccp->prcw[resno];
                        l_pdy = l_tccp->prch[resno];
 
-                       l_dx = l_img_comp->dx * (1 << (l_pdx + l_tccp->numresolutions - 1 - resno));
-                       l_dy = l_img_comp->dy * (1 << (l_pdy + l_tccp->numresolutions - 1 - resno));
+                       l_dx = l_img_comp->dx * (1u << (l_pdx + l_tccp->numresolutions - 1 - resno));
+                       l_dy = l_img_comp->dy * (1u << (l_pdy + l_tccp->numresolutions - 1 - resno));
 
                        /* take the minimum size for dx for each comp and resolution */
                        *p_dx_min = opj_uint_min(*p_dx_min, l_dx);
@@ -632,19 +639,19 @@ void opj_get_encoding_parameters( const opj_image_t *p_image,
                        /* various calculations of extents */
                        l_level_no = l_tccp->numresolutions - 1 - resno;
 
-                       l_rx0 = opj_int_ceildivpow2(l_tcx0, l_level_no);
-                       l_ry0 = opj_int_ceildivpow2(l_tcy0, l_level_no);
-                       l_rx1 = opj_int_ceildivpow2(l_tcx1, l_level_no);
-                       l_ry1 = opj_int_ceildivpow2(l_tcy1, l_level_no);
+                       l_rx0 = opj_int_ceildivpow2(l_tcx0, (OPJ_INT32)l_level_no);
+                       l_ry0 = opj_int_ceildivpow2(l_tcy0, (OPJ_INT32)l_level_no);
+                       l_rx1 = opj_int_ceildivpow2(l_tcx1, (OPJ_INT32)l_level_no);
+                       l_ry1 = opj_int_ceildivpow2(l_tcy1, (OPJ_INT32)l_level_no);
 
-                       l_px0 = opj_int_floordivpow2(l_rx0, l_pdx) << l_pdx;
-                       l_py0 = opj_int_floordivpow2(l_ry0, l_pdy) << l_pdy;
-                       l_px1 = opj_int_ceildivpow2(l_rx1, l_pdx) << l_pdx;
+                       l_px0 = opj_int_floordivpow2(l_rx0, (OPJ_INT32)l_pdx) << l_pdx;
+                       l_py0 = opj_int_floordivpow2(l_ry0, (OPJ_INT32)l_pdy) << l_pdy;
+                       l_px1 = opj_int_ceildivpow2(l_rx1, (OPJ_INT32)l_pdx) << l_pdx;
 
-                       py1 = opj_int_ceildivpow2(l_ry1, l_pdy) << l_pdy;
+                       py1 = opj_int_ceildivpow2(l_ry1, (OPJ_INT32)l_pdy) << l_pdy;
 
-                       l_pw = (l_rx0==l_rx1)?0:((l_px1 - l_px0) >> l_pdx);
-                       l_ph = (l_ry0==l_ry1)?0:((py1 - l_py0) >> l_pdy);
+                       l_pw = (l_rx0==l_rx1)?0:(OPJ_UINT32)((l_px1 - l_px0) >> l_pdx);
+                       l_ph = (l_ry0==l_ry1)?0:(OPJ_UINT32)((py1 - l_py0) >> l_pdy);
 
                        l_product = l_pw * l_ph;
 
@@ -659,7 +666,7 @@ void opj_get_encoding_parameters(   const opj_image_t *p_image,
 }
 
 
-void opj_get_all_encoding_parameters(   const opj_image_t *p_image,
+static void opj_get_all_encoding_parameters(   const opj_image_t *p_image,
                                         const opj_cp_t *p_cp,
                                         OPJ_UINT32 tileno,
                                         OPJ_INT32 * p_tx0,
@@ -686,6 +693,9 @@ void opj_get_all_encoding_parameters(   const opj_image_t *p_image,
        /* position in x and y of tile*/
        OPJ_UINT32 p, q;
 
+       /* non-corrected (in regard to image offset) tile offset */
+       OPJ_UINT32 l_tx0, l_ty0;
+
        /* preconditions in debug*/
        assert(p_cp != 00);
        assert(p_image != 00);
@@ -701,10 +711,12 @@ void opj_get_all_encoding_parameters(   const opj_image_t *p_image,
        q = tileno / p_cp->tw;
 
        /* here calculation of tx0, tx1, ty0, ty1, maxprec, l_dx and l_dy */
-       *p_tx0 = opj_int_max(p_cp->tx0 + p * p_cp->tdx, p_image->x0);
-       *p_tx1 = opj_int_min(p_cp->tx0 + (p + 1) * p_cp->tdx, p_image->x1);
-       *p_ty0 = opj_int_max(p_cp->ty0 + q * p_cp->tdy, p_image->y0);
-       *p_ty1 = opj_int_min(p_cp->ty0 + (q + 1) * p_cp->tdy, p_image->y1);
+       l_tx0 = p_cp->tx0 + p * p_cp->tdx; /* can't be greater than p_image->x1 so won't overflow */
+       *p_tx0 = (OPJ_INT32)opj_uint_max(l_tx0, p_image->x0);
+       *p_tx1 = (OPJ_INT32)opj_uint_min(opj_uint_adds(l_tx0, p_cp->tdx), p_image->x1);
+       l_ty0 = p_cp->ty0 + q * p_cp->tdy; /* can't be greater than p_image->y1 so won't overflow */
+       *p_ty0 = (OPJ_INT32)opj_uint_max(l_ty0, p_image->y0);
+       *p_ty1 = (OPJ_INT32)opj_uint_min(opj_uint_adds(l_ty0, p_cp->tdy), p_image->y1);
 
        /* max precision and resolution is 0 (can only grow)*/
        *p_max_prec = 0;
@@ -725,42 +737,44 @@ void opj_get_all_encoding_parameters(   const opj_image_t *p_image,
 
                lResolutionPtr = p_resolutions[compno];
 
-               l_tcx0 = opj_int_ceildiv(*p_tx0, l_img_comp->dx);
-               l_tcy0 = opj_int_ceildiv(*p_ty0, l_img_comp->dy);
-               l_tcx1 = opj_int_ceildiv(*p_tx1, l_img_comp->dx);
-               l_tcy1 = opj_int_ceildiv(*p_ty1, l_img_comp->dy);
+               l_tcx0 = opj_int_ceildiv(*p_tx0, (OPJ_INT32)l_img_comp->dx);
+               l_tcy0 = opj_int_ceildiv(*p_ty0, (OPJ_INT32)l_img_comp->dy);
+               l_tcx1 = opj_int_ceildiv(*p_tx1, (OPJ_INT32)l_img_comp->dx);
+               l_tcy1 = opj_int_ceildiv(*p_ty1, (OPJ_INT32)l_img_comp->dy);
 
                if (l_tccp->numresolutions > *p_max_res) {
                        *p_max_res = l_tccp->numresolutions;
                }
 
                /* use custom size for precincts*/
-               l_level_no = l_tccp->numresolutions - 1;
+               l_level_no = l_tccp->numresolutions;
                for (resno = 0; resno < l_tccp->numresolutions; ++resno) {
                        OPJ_UINT32 l_dx, l_dy;
 
+                       --l_level_no;
+                       
                        /* precinct width and height*/
                        l_pdx = l_tccp->prcw[resno];
                        l_pdy = l_tccp->prch[resno];
                        *lResolutionPtr++ = l_pdx;
                        *lResolutionPtr++ = l_pdy;
-                       l_dx = l_img_comp->dx * (1 << (l_pdx + l_level_no));
-                       l_dy = l_img_comp->dy * (1 << (l_pdy + l_level_no));
+                       l_dx = l_img_comp->dx * (1u << (l_pdx + l_level_no));
+                       l_dy = l_img_comp->dy * (1u << (l_pdy + l_level_no));
                        /* take the minimum size for l_dx for each comp and resolution*/
-                       *p_dx_min = opj_int_min(*p_dx_min, l_dx);
-                       *p_dy_min = opj_int_min(*p_dy_min, l_dy);
+                       *p_dx_min = (OPJ_UINT32)opj_int_min((OPJ_INT32)*p_dx_min, (OPJ_INT32)l_dx);
+                       *p_dy_min = (OPJ_UINT32)opj_int_min((OPJ_INT32)*p_dy_min, (OPJ_INT32)l_dy);
 
                        /* various calculations of extents*/
-                       l_rx0 = opj_int_ceildivpow2(l_tcx0, l_level_no);
-                       l_ry0 = opj_int_ceildivpow2(l_tcy0, l_level_no);
-                       l_rx1 = opj_int_ceildivpow2(l_tcx1, l_level_no);
-                       l_ry1 = opj_int_ceildivpow2(l_tcy1, l_level_no);
-                       l_px0 = opj_int_floordivpow2(l_rx0, l_pdx) << l_pdx;
-                       l_py0 = opj_int_floordivpow2(l_ry0, l_pdy) << l_pdy;
-                       l_px1 = opj_int_ceildivpow2(l_rx1, l_pdx) << l_pdx;
-                       py1 = opj_int_ceildivpow2(l_ry1, l_pdy) << l_pdy;
-                       l_pw = (l_rx0==l_rx1)?0:((l_px1 - l_px0) >> l_pdx);
-                       l_ph = (l_ry0==l_ry1)?0:((py1 - l_py0) >> l_pdy);
+                       l_rx0 = opj_int_ceildivpow2(l_tcx0, (OPJ_INT32)l_level_no);
+                       l_ry0 = opj_int_ceildivpow2(l_tcy0, (OPJ_INT32)l_level_no);
+                       l_rx1 = opj_int_ceildivpow2(l_tcx1, (OPJ_INT32)l_level_no);
+                       l_ry1 = opj_int_ceildivpow2(l_tcy1, (OPJ_INT32)l_level_no);
+                       l_px0 = opj_int_floordivpow2(l_rx0, (OPJ_INT32)l_pdx) << l_pdx;
+                       l_py0 = opj_int_floordivpow2(l_ry0, (OPJ_INT32)l_pdy) << l_pdy;
+                       l_px1 = opj_int_ceildivpow2(l_rx1, (OPJ_INT32)l_pdx) << l_pdx;
+                       py1 = opj_int_ceildivpow2(l_ry1, (OPJ_INT32)l_pdy) << l_pdy;
+                       l_pw = (l_rx0==l_rx1)?0:(OPJ_UINT32)((l_px1 - l_px0) >> l_pdx);
+                       l_ph = (l_ry0==l_ry1)?0:(OPJ_UINT32)((py1 - l_py0) >> l_pdy);
                        *lResolutionPtr++ = l_pw;
                        *lResolutionPtr++ = l_ph;
                        l_product = l_pw * l_ph;
@@ -770,14 +784,13 @@ void opj_get_all_encoding_parameters(   const opj_image_t *p_image,
                                *p_max_prec = l_product;
                        }
 
-                       --l_level_no;
                }
                ++l_tccp;
                ++l_img_comp;
        }
 }
 
-opj_pi_iterator_t * opj_pi_create(     const opj_image_t *image,
+static opj_pi_iterator_t * opj_pi_create(      const opj_image_t *image,
                                     const opj_cp_t *cp,
                                     OPJ_UINT32 tileno )
 {
@@ -808,7 +821,6 @@ opj_pi_iterator_t * opj_pi_create(  const opj_image_t *image,
        if (!l_pi) {
                return NULL;
        }
-       memset(l_pi,0,l_poc_bound * sizeof(opj_pi_iterator_t));
 
        l_current_pi = l_pi;
        for (pino = 0; pino < l_poc_bound ; ++pino) {
@@ -820,28 +832,26 @@ opj_pi_iterator_t * opj_pi_create(        const opj_image_t *image,
                }
 
                l_current_pi->numcomps = image->numcomps;
-               memset(l_current_pi->comps,0,image->numcomps * sizeof(opj_pi_comp_t));
 
                for (compno = 0; compno < image->numcomps; ++compno) {
                        opj_pi_comp_t *comp = &l_current_pi->comps[compno];
 
                        tccp = &tcp->tccps[compno];
 
-                       comp->resolutions = (opj_pi_resolution_t*) opj_malloc(tccp->numresolutions * sizeof(opj_pi_resolution_t));
+                       comp->resolutions = (opj_pi_resolution_t*) opj_calloc(tccp->numresolutions, sizeof(opj_pi_resolution_t));
                        if (!comp->resolutions) {
                                opj_pi_destroy(l_pi, l_poc_bound);
                                return 00;
                        }
 
                        comp->numresolutions = tccp->numresolutions;
-                       memset(comp->resolutions,0,tccp->numresolutions * sizeof(opj_pi_resolution_t));
                }
                ++l_current_pi;
        }
        return l_pi;
 }
 
-void opj_pi_update_encode_poc_and_final (   opj_cp_t *p_cp,
+static void opj_pi_update_encode_poc_and_final (   opj_cp_t *p_cp,
                                             OPJ_UINT32 p_tileno,
                                             OPJ_INT32 p_tx0,
                                             OPJ_INT32 p_tx1,
@@ -889,10 +899,10 @@ void opj_pi_update_encode_poc_and_final (   opj_cp_t *p_cp,
        l_current_poc->prcS = 0;
 
        l_current_poc->prcE = p_max_prec;
-       l_current_poc->txS = p_tx0;
-       l_current_poc->txE = p_tx1;
-       l_current_poc->tyS = p_ty0;
-       l_current_poc->tyE = p_ty1;
+       l_current_poc->txS = (OPJ_UINT32)p_tx0;
+       l_current_poc->txE = (OPJ_UINT32)p_tx1;
+       l_current_poc->tyS = (OPJ_UINT32)p_ty0;
+       l_current_poc->tyE = (OPJ_UINT32)p_ty1;
        l_current_poc->dx = p_dx_min;
        l_current_poc->dy = p_dy_min;
 
@@ -909,17 +919,17 @@ void opj_pi_update_encode_poc_and_final (   opj_cp_t *p_cp,
                l_current_poc->layS = (l_current_poc->layE > (l_current_poc-1)->layE) ? l_current_poc->layE : 0;
 
                l_current_poc->prcE = p_max_prec;
-               l_current_poc->txS = p_tx0;
-               l_current_poc->txE = p_tx1;
-               l_current_poc->tyS = p_ty0;
-               l_current_poc->tyE = p_ty1;
+               l_current_poc->txS = (OPJ_UINT32)p_tx0;
+               l_current_poc->txE = (OPJ_UINT32)p_tx1;
+               l_current_poc->tyS = (OPJ_UINT32)p_ty0;
+               l_current_poc->tyE = (OPJ_UINT32)p_ty1;
                l_current_poc->dx = p_dx_min;
                l_current_poc->dy = p_dy_min;
                ++ l_current_poc;
        }
 }
 
-void opj_pi_update_encode_not_poc (    opj_cp_t *p_cp,
+static void opj_pi_update_encode_not_poc (     opj_cp_t *p_cp,
                                     OPJ_UINT32 p_num_comps,
                                     OPJ_UINT32 p_tileno,
                                     OPJ_INT32 p_tx0,
@@ -964,17 +974,17 @@ void opj_pi_update_encode_not_poc (       opj_cp_t *p_cp,
                l_current_poc->prg  = l_tcp->prg;
                l_current_poc->prcS = 0;
                l_current_poc->prcE = p_max_prec;
-               l_current_poc->txS = p_tx0;
-               l_current_poc->txE = p_tx1;
-               l_current_poc->tyS = p_ty0;
-               l_current_poc->tyE = p_ty1;
+               l_current_poc->txS = (OPJ_UINT32)p_tx0;
+               l_current_poc->txE = (OPJ_UINT32)p_tx1;
+               l_current_poc->tyS = (OPJ_UINT32)p_ty0;
+               l_current_poc->tyE = (OPJ_UINT32)p_ty1;
                l_current_poc->dx = p_dx_min;
                l_current_poc->dy = p_dy_min;
                ++ l_current_poc;
        }
 }
 
-void opj_pi_update_decode_poc (opj_pi_iterator_t * p_pi,
+static void opj_pi_update_decode_poc (opj_pi_iterator_t * p_pi,
                                opj_tcp_t * p_tcp,
                                OPJ_UINT32 p_max_precision,
                                OPJ_UINT32 p_max_res)
@@ -1000,23 +1010,23 @@ void opj_pi_update_decode_poc (opj_pi_iterator_t * p_pi,
        l_current_poc = p_tcp->pocs;
 
        for     (pino = 0;pino<l_bound;++pino) {
-               l_current_pi->poc.prg = l_current_poc->prg;
+               l_current_pi->poc.prg = l_current_poc->prg; /* Progression Order #0 */
                l_current_pi->first = 1;
 
-               l_current_pi->poc.resno0 = l_current_poc->resno0;
-               l_current_pi->poc.compno0 = l_current_poc->compno0;
+               l_current_pi->poc.resno0 = l_current_poc->resno0; /* Resolution Level Index #0 (Start) */
+               l_current_pi->poc.compno0 = l_current_poc->compno0; /* Component Index #0 (Start) */
                l_current_pi->poc.layno0 = 0;
                l_current_pi->poc.precno0 = 0;
-               l_current_pi->poc.resno1 = l_current_poc->resno1;
-               l_current_pi->poc.compno1 = l_current_poc->compno1;
-               l_current_pi->poc.layno1 = l_current_poc->layno1;
+               l_current_pi->poc.resno1 = l_current_poc->resno1; /* Resolution Level Index #0 (End) */
+               l_current_pi->poc.compno1 = l_current_poc->compno1; /* Component Index #0 (End) */
+               l_current_pi->poc.layno1 = l_current_poc->layno1; /* Layer Index #0 (End) */
                l_current_pi->poc.precno1 = p_max_precision;
                ++l_current_pi;
                ++l_current_poc;
        }
 }
 
-void opj_pi_update_decode_not_poc (opj_pi_iterator_t * p_pi,
+static void opj_pi_update_decode_not_poc (opj_pi_iterator_t * p_pi,
                                    opj_tcp_t * p_tcp,
                                    OPJ_UINT32 p_max_precision,
                                    OPJ_UINT32 p_max_res)
@@ -1053,7 +1063,7 @@ void opj_pi_update_decode_not_poc (opj_pi_iterator_t * p_pi,
 
 
 
-opj_bool opj_pi_check_next_level(      OPJ_INT32 pos,
+static OPJ_BOOL opj_pi_check_next_level(       OPJ_INT32 pos,
                                                                opj_cp_t *cp,
                                                                OPJ_UINT32 tileno,
                                                                OPJ_UINT32 pino,
@@ -1101,7 +1111,8 @@ opj_bool opj_pi_check_next_level( OPJ_INT32 pos,
                            break;
                    case 'P':
                            switch(tcp->prg){
-                                   case LRCP||RLCP:
+                    case OPJ_LRCP: /* fall through */
+                    case OPJ_RLCP:
                                            if(tcp->prc_t == tcp->prcE){
                                                    if(opj_pi_check_next_level(i-1,cp,tileno,pino,prog)){
                                                            return OPJ_TRUE;
@@ -1180,7 +1191,7 @@ opj_pi_iterator_t *opj_pi_create_decode(opj_image_t *p_image,
        l_tcp = &p_cp->tcps[p_tile_no];
        l_bound = l_tcp->numpocs+1;
 
-       l_data_stride = 4 * J2K_MAXRLVLS;
+       l_data_stride = 4 * OPJ_J2K_MAXRLVLS;
        l_tmp_data = (OPJ_UINT32*)opj_malloc(
                l_data_stride * p_image->numcomps * sizeof(OPJ_UINT32));
        if
@@ -1235,7 +1246,6 @@ opj_pi_iterator_t *opj_pi_create_decode(opj_image_t *p_image,
                opj_pi_destroy(l_pi, l_bound);
                return 00;
        }
-       memset(l_current_pi->include,0, (l_tcp->numlayers + 1) * l_step_l* sizeof(OPJ_INT16));
 
        /* special treatment for the first packet iterator */
        l_current_comp = l_current_pi->comps;
@@ -1280,11 +1290,10 @@ opj_pi_iterator_t *opj_pi_create_decode(opj_image_t *p_image,
        }
        ++l_current_pi;
 
-       for
-               (pino = 1 ; pino<l_bound ; ++pino )
+       for (pino = 1 ; pino<l_bound ; ++pino )
        {
-               opj_pi_comp_t *l_current_comp = l_current_pi->comps;
-               opj_image_comp_t * l_img_comp = p_image->comps;
+               l_current_comp = l_current_pi->comps;
+               l_img_comp = p_image->comps;
                l_tccp = l_tcp->tccps;
 
                l_current_pi->tx0 = l_tx0;
@@ -1383,7 +1392,7 @@ opj_pi_iterator_t *opj_pi_initialise_encode(const opj_image_t *p_image,
        l_tcp = &p_cp->tcps[p_tile_no];
        l_bound = l_tcp->numpocs+1;
 
-       l_data_stride = 4 * J2K_MAXRLVLS;
+       l_data_stride = 4 * OPJ_J2K_MAXRLVLS;
        l_tmp_data = (OPJ_UINT32*)opj_malloc(
                l_data_stride * p_image->numcomps * sizeof(OPJ_UINT32));
        if (! l_tmp_data) {
@@ -1422,7 +1431,7 @@ opj_pi_iterator_t *opj_pi_initialise_encode(const opj_image_t *p_image,
        l_step_l = l_max_res * l_step_r;
 
        /* set values for first packet iterator*/
-       l_pi->tp_on = p_cp->m_specific_param.m_enc.m_tp_on;
+       l_pi->tp_on = (OPJ_BYTE)p_cp->m_specific_param.m_enc.m_tp_on;
        l_current_pi = l_pi;
 
        /* memory allocation for include*/
@@ -1433,7 +1442,6 @@ opj_pi_iterator_t *opj_pi_initialise_encode(const opj_image_t *p_image,
                opj_pi_destroy(l_pi, l_bound);
                return 00;
        }
-       memset(l_current_pi->include,0,l_tcp->numlayers * l_step_l* sizeof(OPJ_INT16));
 
        /* special treatment for the first packet iterator*/
        l_current_comp = l_current_pi->comps;
@@ -1474,8 +1482,8 @@ opj_pi_iterator_t *opj_pi_initialise_encode(const opj_image_t *p_image,
        ++l_current_pi;
 
        for (pino = 1 ; pino<l_bound ; ++pino ) {
-               opj_pi_comp_t *l_current_comp = l_current_pi->comps;
-               opj_image_comp_t * l_img_comp = p_image->comps;
+               l_current_comp = l_current_pi->comps;
+               l_img_comp = p_image->comps;
                l_tccp = l_tcp->tccps;
 
                l_current_pi->tx0 = l_tx0;
@@ -1519,7 +1527,7 @@ opj_pi_iterator_t *opj_pi_initialise_encode(const opj_image_t *p_image,
        opj_free(l_tmp_ptr);
        l_tmp_ptr = 00;
 
-       if (l_tcp->POC && ( p_cp->m_specific_param.m_enc.m_cinema || p_t2_mode == FINAL_PASS)) {
+    if (l_tcp->POC && (OPJ_IS_CINEMA(p_cp->rsiz) || p_t2_mode == FINAL_PASS)) {
                opj_pi_update_encode_poc_and_final(p_cp,p_tile_no,l_tx0,l_tx1,l_ty0,l_ty1,l_max_prec,l_max_res,l_dx_min,l_dy_min);
        }
        else {
@@ -1548,7 +1556,7 @@ void opj_pi_create_encode(        opj_pi_iterator_t *pi,
        pi[pino].first = 1;
        pi[pino].poc.prg = tcp->prg;
 
-       if(!(cp->m_specific_param.m_enc.m_tp_on && ((!cp->m_specific_param.m_enc.m_cinema && (t2_mode == FINAL_PASS)) || cp->m_specific_param.m_enc.m_cinema))){
+    if(!(cp->m_specific_param.m_enc.m_tp_on && ((!OPJ_IS_CINEMA(cp->rsiz) && (t2_mode == FINAL_PASS)) || OPJ_IS_CINEMA(cp->rsiz)))){
                pi[pino].poc.resno0 = tcp->resS;
                pi[pino].poc.resno1 = tcp->resE;
                pi[pino].poc.compno0 = tcp->compS;
@@ -1557,10 +1565,10 @@ void opj_pi_create_encode(      opj_pi_iterator_t *pi,
                pi[pino].poc.layno1 = tcp->layE;
                pi[pino].poc.precno0 = tcp->prcS;
                pi[pino].poc.precno1 = tcp->prcE;
-               pi[pino].poc.tx0 = tcp->txS;
-               pi[pino].poc.ty0 = tcp->tyS;
-               pi[pino].poc.tx1 = tcp->txE;
-               pi[pino].poc.ty1 = tcp->tyE;
+               pi[pino].poc.tx0 = (OPJ_INT32)tcp->txS;
+               pi[pino].poc.ty0 = (OPJ_INT32)tcp->tyS;
+               pi[pino].poc.tx1 = (OPJ_INT32)tcp->txE;
+               pi[pino].poc.ty1 = (OPJ_INT32)tcp->tyE;
        }else {
                for(i=tppos+1;i<4;i++){
                        switch(prog[i]){
@@ -1578,16 +1586,16 @@ void opj_pi_create_encode(      opj_pi_iterator_t *pi,
                                break;
                        case 'P':
                                switch(tcp->prg){
-                               case LRCP:
-                               case RLCP:
+                               case OPJ_LRCP:
+                               case OPJ_RLCP:
                                        pi[pino].poc.precno0 = tcp->prcS;
                                        pi[pino].poc.precno1 = tcp->prcE;
                                        break;
                                default:
-                                       pi[pino].poc.tx0 = tcp->txS;
-                                       pi[pino].poc.ty0 = tcp->tyS;
-                                       pi[pino].poc.tx1 = tcp->txE;
-                                       pi[pino].poc.ty1 = tcp->tyE;
+                                       pi[pino].poc.tx0 = (OPJ_INT32)tcp->txS;
+                                       pi[pino].poc.ty0 = (OPJ_INT32)tcp->tyS;
+                                       pi[pino].poc.tx1 = (OPJ_INT32)tcp->txE;
+                                       pi[pino].poc.ty1 = (OPJ_INT32)tcp->tyE;
                                        break;
                                }
                                break;
@@ -1617,8 +1625,8 @@ void opj_pi_create_encode(        opj_pi_iterator_t *pi,
                                        break;
                                case 'P':
                                        switch(tcp->prg){
-                                       case LRCP:
-                                       case RLCP:
+                                       case OPJ_LRCP:
+                                       case OPJ_RLCP:
                                                tcp->prc_t = tcp->prcS;
                                                pi[pino].poc.precno0 = tcp->prc_t;
                                                pi[pino].poc.precno1 = tcp->prc_t+1;
@@ -1627,12 +1635,12 @@ void opj_pi_create_encode(      opj_pi_iterator_t *pi,
                                        default:
                                                tcp->tx0_t = tcp->txS;
                                                tcp->ty0_t = tcp->tyS;
-                                               pi[pino].poc.tx0 = tcp->tx0_t;
-                                               pi[pino].poc.tx1 = tcp->tx0_t + tcp->dx - (tcp->tx0_t % tcp->dx);
-                                               pi[pino].poc.ty0 = tcp->ty0_t;
-                                               pi[pino].poc.ty1 = tcp->ty0_t + tcp->dy - (tcp->ty0_t % tcp->dy);
-                                               tcp->tx0_t = pi[pino].poc.tx1;
-                                               tcp->ty0_t = pi[pino].poc.ty1;
+                                               pi[pino].poc.tx0 = (OPJ_INT32)tcp->tx0_t;
+                                               pi[pino].poc.tx1 = (OPJ_INT32)(tcp->tx0_t + tcp->dx - (tcp->tx0_t % tcp->dx));
+                                               pi[pino].poc.ty0 = (OPJ_INT32)tcp->ty0_t;
+                                               pi[pino].poc.ty1 = (OPJ_INT32)(tcp->ty0_t + tcp->dy - (tcp->ty0_t % tcp->dy));
+                                               tcp->tx0_t = (OPJ_UINT32)pi[pino].poc.tx1;
+                                               tcp->ty0_t = (OPJ_UINT32)pi[pino].poc.ty1;
                                                break;
                                        }
                                        break;
@@ -1656,16 +1664,16 @@ void opj_pi_create_encode(      opj_pi_iterator_t *pi,
                                        break;
                                case 'P':
                                        switch(tcp->prg){
-                                       case LRCP:
-                                       case RLCP:
+                                       case OPJ_LRCP:
+                                       case OPJ_RLCP:
                                                pi[pino].poc.precno0 = tcp->prc_t-1;
                                                pi[pino].poc.precno1 = tcp->prc_t;
                                                break;
                                        default:
-                                               pi[pino].poc.tx0 = tcp->tx0_t - tcp->dx - (tcp->tx0_t % tcp->dx);
-                                               pi[pino].poc.tx1 = tcp->tx0_t ;
-                                               pi[pino].poc.ty0 = tcp->ty0_t - tcp->dy - (tcp->ty0_t % tcp->dy);
-                                               pi[pino].poc.ty1 = tcp->ty0_t ;
+                                               pi[pino].poc.tx0 = (OPJ_INT32)(tcp->tx0_t - tcp->dx - (tcp->tx0_t % tcp->dx));
+                                               pi[pino].poc.tx1 = (OPJ_INT32)tcp->tx0_t ;
+                                               pi[pino].poc.ty0 = (OPJ_INT32)(tcp->ty0_t - tcp->dy - (tcp->ty0_t % tcp->dy));
+                                               pi[pino].poc.ty1 = (OPJ_INT32)tcp->ty0_t ;
                                                break;
                                        }
                                        break;
@@ -1728,8 +1736,8 @@ void opj_pi_create_encode(        opj_pi_iterator_t *pi,
                                                break;
                                        case 'P':
                                                switch(tcp->prg){
-                                               case LRCP:
-                                               case RLCP:
+                                               case OPJ_LRCP:
+                                               case OPJ_RLCP:
                                                        if(tcp->prc_t == tcp->prcE){
                                                                if(opj_pi_check_next_level(i-1,cp,tileno,pino,prog)){
                                                                        tcp->prc_t = tcp->prcS;
@@ -1752,29 +1760,29 @@ void opj_pi_create_encode(      opj_pi_iterator_t *pi,
                                                                if(tcp->ty0_t >= tcp->tyE){
                                                                        if(opj_pi_check_next_level(i-1,cp,tileno,pino,prog)){
                                                                                tcp->ty0_t = tcp->tyS;
-                                                                               pi[pino].poc.ty0 = tcp->ty0_t;
-                                                                               pi[pino].poc.ty1 = tcp->ty0_t + tcp->dy - (tcp->ty0_t % tcp->dy);
-                                                                               tcp->ty0_t = pi[pino].poc.ty1;
+                                                                               pi[pino].poc.ty0 = (OPJ_INT32)tcp->ty0_t;
+                                                                               pi[pino].poc.ty1 = (OPJ_INT32)(tcp->ty0_t + tcp->dy - (tcp->ty0_t % tcp->dy));
+                                                                               tcp->ty0_t = (OPJ_UINT32)pi[pino].poc.ty1;
                                                                                incr_top=1;resetX=1;
                                                                        }else{
                                                                                incr_top=0;resetX=0;
                                                                        }
                                                                }else{
-                                                                       pi[pino].poc.ty0 = tcp->ty0_t;
-                                                                       pi[pino].poc.ty1 = tcp->ty0_t + tcp->dy - (tcp->ty0_t % tcp->dy);
-                                                                       tcp->ty0_t = pi[pino].poc.ty1;
+                                                                       pi[pino].poc.ty0 = (OPJ_INT32)tcp->ty0_t;
+                                                                       pi[pino].poc.ty1 = (OPJ_INT32)(tcp->ty0_t + tcp->dy - (tcp->ty0_t % tcp->dy));
+                                                                       tcp->ty0_t = (OPJ_UINT32)pi[pino].poc.ty1;
                                                                        incr_top=0;resetX=1;
                                                                }
                                                                if(resetX==1){
                                                                        tcp->tx0_t = tcp->txS;
-                                                                       pi[pino].poc.tx0 = tcp->tx0_t;
-                                                                       pi[pino].poc.tx1 = tcp->tx0_t + tcp->dx- (tcp->tx0_t % tcp->dx);
-                                                                       tcp->tx0_t = pi[pino].poc.tx1;
+                                                                       pi[pino].poc.tx0 = (OPJ_INT32)tcp->tx0_t;
+                                                                       pi[pino].poc.tx1 = (OPJ_INT32)(tcp->tx0_t + tcp->dx- (tcp->tx0_t % tcp->dx));
+                                                                       tcp->tx0_t = (OPJ_UINT32)pi[pino].poc.tx1;
                                                                }
                                                        }else{
-                                                               pi[pino].poc.tx0 = tcp->tx0_t;
-                                                               pi[pino].poc.tx1 = tcp->tx0_t + tcp->dx- (tcp->tx0_t % tcp->dx);
-                                                               tcp->tx0_t = pi[pino].poc.tx1;
+                                                               pi[pino].poc.tx0 = (OPJ_INT32)tcp->tx0_t;
+                                                               pi[pino].poc.tx1 = (OPJ_INT32)(tcp->tx0_t + tcp->dx- (tcp->tx0_t % tcp->dx));
+                                                               tcp->tx0_t = (OPJ_UINT32)pi[pino].poc.tx1;
                                                                incr_top=0;
                                                        }
                                                        break;
@@ -1850,19 +1858,19 @@ void opj_pi_update_encoding_parameters( const opj_image_t *p_image,
        }
 }
 
-opj_bool opj_pi_next(opj_pi_iterator_t * pi) {
+OPJ_BOOL opj_pi_next(opj_pi_iterator_t * pi) {
        switch (pi->poc.prg) {
-               case LRCP:
+               case OPJ_LRCP:
                        return opj_pi_next_lrcp(pi);
-               case RLCP:
+               case OPJ_RLCP:
                        return opj_pi_next_rlcp(pi);
-               case RPCL:
+               case OPJ_RPCL:
                        return opj_pi_next_rpcl(pi);
-               case PCRL:
+               case OPJ_PCRL:
                        return opj_pi_next_pcrl(pi);
-               case CPRL:
+               case OPJ_CPRL:
                        return opj_pi_next_cprl(pi);
-               case PROG_UNKNOWN:
+               case OPJ_PROG_UNKNOWN:
                        return OPJ_FALSE;
        }