Home | History | Annotate | Download | only in libopenjpeg20
      1 /*
      2  * The copyright in this software is being made available under the 2-clauses
      3  * BSD License, included below. This software may be subject to other third
      4  * party and contributor rights, including patent rights, and no such rights
      5  * are granted under this license.
      6  *
      7  * Copyright (c) 2002-2014, Universite catholique de Louvain (UCL), Belgium
      8  * Copyright (c) 2002-2014, Professor Benoit Macq
      9  * Copyright (c) 2001-2003, David Janssens
     10  * Copyright (c) 2002-2003, Yannick Verschueren
     11  * Copyright (c) 2003-2007, Francois-Olivier Devaux
     12  * Copyright (c) 2003-2014, Antonin Descampe
     13  * Copyright (c) 2005, Herve Drolon, FreeImage Team
     14  * Copyright (c) 2006-2007, Parvatha Elangovan
     15  * All rights reserved.
     16  *
     17  * Redistribution and use in source and binary forms, with or without
     18  * modification, are permitted provided that the following conditions
     19  * are met:
     20  * 1. Redistributions of source code must retain the above copyright
     21  *    notice, this list of conditions and the following disclaimer.
     22  * 2. Redistributions in binary form must reproduce the above copyright
     23  *    notice, this list of conditions and the following disclaimer in the
     24  *    documentation and/or other materials provided with the distribution.
     25  *
     26  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
     27  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     28  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     29  * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
     30  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  * POSSIBILITY OF SUCH DAMAGE.
     37  */
     38 
     39 #include "opj_includes.h"
     40 
     41 /** @defgroup PI PI - Implementation of a packet iterator */
     42 /*@{*/
     43 
     44 /** @name Local static functions */
     45 /*@{*/
     46 
     47 /**
     48 Get next packet in layer-resolution-component-precinct order.
     49 @param pi packet iterator to modify
     50 @return returns false if pi pointed to the last packet or else returns true
     51 */
     52 static OPJ_BOOL opj_pi_next_lrcp(opj_pi_iterator_t * pi);
     53 /**
     54 Get next packet in resolution-layer-component-precinct order.
     55 @param pi packet iterator to modify
     56 @return returns false if pi pointed to the last packet or else returns true
     57 */
     58 static OPJ_BOOL opj_pi_next_rlcp(opj_pi_iterator_t * pi);
     59 /**
     60 Get next packet in resolution-precinct-component-layer order.
     61 @param pi packet iterator to modify
     62 @return returns false if pi pointed to the last packet or else returns true
     63 */
     64 static OPJ_BOOL opj_pi_next_rpcl(opj_pi_iterator_t * pi);
     65 /**
     66 Get next packet in precinct-component-resolution-layer order.
     67 @param pi packet iterator to modify
     68 @return returns false if pi pointed to the last packet or else returns true
     69 */
     70 static OPJ_BOOL opj_pi_next_pcrl(opj_pi_iterator_t * pi);
     71 /**
     72 Get next packet in component-precinct-resolution-layer order.
     73 @param pi packet iterator to modify
     74 @return returns false if pi pointed to the last packet or else returns true
     75 */
     76 static OPJ_BOOL opj_pi_next_cprl(opj_pi_iterator_t * pi);
     77 
     78 /**
     79  * Updates the coding parameters if the encoding is used with Progression order changes and final (or cinema parameters are used).
     80  *
     81  * @param   p_cp        the coding parameters to modify
     82  * @param   p_tileno    the tile index being concerned.
     83  * @param   p_tx0       X0 parameter for the tile
     84  * @param   p_tx1       X1 parameter for the tile
     85  * @param   p_ty0       Y0 parameter for the tile
     86  * @param   p_ty1       Y1 parameter for the tile
     87  * @param   p_max_prec  the maximum precision for all the bands of the tile
     88  * @param   p_max_res   the maximum number of resolutions for all the poc inside the tile.
     89  * @param   p_dx_min        the minimum dx of all the components of all the resolutions for the tile.
     90  * @param   p_dy_min        the minimum dy of all the components of all the resolutions for the tile.
     91  */
     92 static void opj_pi_update_encode_poc_and_final(opj_cp_t *p_cp,
     93         OPJ_UINT32 p_tileno,
     94         OPJ_INT32 p_tx0,
     95         OPJ_INT32 p_tx1,
     96         OPJ_INT32 p_ty0,
     97         OPJ_INT32 p_ty1,
     98         OPJ_UINT32 p_max_prec,
     99         OPJ_UINT32 p_max_res,
    100         OPJ_UINT32 p_dx_min,
    101         OPJ_UINT32 p_dy_min);
    102 
    103 /**
    104  * Updates the coding parameters if the encoding is not used with Progression order changes and final (and cinema parameters are used).
    105  *
    106  * @param   p_cp        the coding parameters to modify
    107  * @param   p_num_comps     the number of components
    108  * @param   p_tileno    the tile index being concerned.
    109  * @param   p_tx0       X0 parameter for the tile
    110  * @param   p_tx1       X1 parameter for the tile
    111  * @param   p_ty0       Y0 parameter for the tile
    112  * @param   p_ty1       Y1 parameter for the tile
    113  * @param   p_max_prec  the maximum precision for all the bands of the tile
    114  * @param   p_max_res   the maximum number of resolutions for all the poc inside the tile.
    115  * @param   p_dx_min        the minimum dx of all the components of all the resolutions for the tile.
    116  * @param   p_dy_min        the minimum dy of all the components of all the resolutions for the tile.
    117  */
    118 static void opj_pi_update_encode_not_poc(opj_cp_t *p_cp,
    119         OPJ_UINT32 p_num_comps,
    120         OPJ_UINT32 p_tileno,
    121         OPJ_INT32 p_tx0,
    122         OPJ_INT32 p_tx1,
    123         OPJ_INT32 p_ty0,
    124         OPJ_INT32 p_ty1,
    125         OPJ_UINT32 p_max_prec,
    126         OPJ_UINT32 p_max_res,
    127         OPJ_UINT32 p_dx_min,
    128         OPJ_UINT32 p_dy_min);
    129 /**
    130  * Gets the encoding parameters needed to update the coding parameters and all the pocs.
    131  *
    132  * @param   p_image         the image being encoded.
    133  * @param   p_cp            the coding parameters.
    134  * @param   tileno          the tile index of the tile being encoded.
    135  * @param   p_tx0           pointer that will hold the X0 parameter for the tile
    136  * @param   p_tx1           pointer that will hold the X1 parameter for the tile
    137  * @param   p_ty0           pointer that will hold the Y0 parameter for the tile
    138  * @param   p_ty1           pointer that will hold the Y1 parameter for the tile
    139  * @param   p_max_prec      pointer that will hold the maximum precision for all the bands of the tile
    140  * @param   p_max_res       pointer that will hold the maximum number of resolutions for all the poc inside the tile.
    141  * @param   p_dx_min            pointer that will hold the minimum dx of all the components of all the resolutions for the tile.
    142  * @param   p_dy_min            pointer that will hold the minimum dy of all the components of all the resolutions for the tile.
    143  */
    144 static void opj_get_encoding_parameters(const opj_image_t *p_image,
    145                                         const opj_cp_t *p_cp,
    146                                         OPJ_UINT32  tileno,
    147                                         OPJ_INT32  * p_tx0,
    148                                         OPJ_INT32 * p_tx1,
    149                                         OPJ_INT32 * p_ty0,
    150                                         OPJ_INT32 * p_ty1,
    151                                         OPJ_UINT32 * p_dx_min,
    152                                         OPJ_UINT32 * p_dy_min,
    153                                         OPJ_UINT32 * p_max_prec,
    154                                         OPJ_UINT32 * p_max_res);
    155 
    156 /**
    157  * Gets the encoding parameters needed to update the coding parameters and all the pocs.
    158  * The precinct widths, heights, dx and dy for each component at each resolution will be stored as well.
    159  * the last parameter of the function should be an array of pointers of size nb components, each pointer leading
    160  * to an area of size 4 * max_res. The data is stored inside this area with the following pattern :
    161  * dx_compi_res0 , dy_compi_res0 , w_compi_res0, h_compi_res0 , dx_compi_res1 , dy_compi_res1 , w_compi_res1, h_compi_res1 , ...
    162  *
    163  * @param   p_image         the image being encoded.
    164  * @param   p_cp            the coding parameters.
    165  * @param   tileno          the tile index of the tile being encoded.
    166  * @param   p_tx0           pointer that will hold the X0 parameter for the tile
    167  * @param   p_tx1           pointer that will hold the X1 parameter for the tile
    168  * @param   p_ty0           pointer that will hold the Y0 parameter for the tile
    169  * @param   p_ty1           pointer that will hold the Y1 parameter for the tile
    170  * @param   p_max_prec      pointer that will hold the maximum precision for all the bands of the tile
    171  * @param   p_max_res       pointer that will hold the maximum number of resolutions for all the poc inside the tile.
    172  * @param   p_dx_min        pointer that will hold the minimum dx of all the components of all the resolutions for the tile.
    173  * @param   p_dy_min        pointer that will hold the minimum dy of all the components of all the resolutions for the tile.
    174  * @param   p_resolutions   pointer to an area corresponding to the one described above.
    175  */
    176 static void opj_get_all_encoding_parameters(const opj_image_t *p_image,
    177         const opj_cp_t *p_cp,
    178         OPJ_UINT32 tileno,
    179         OPJ_INT32 * p_tx0,
    180         OPJ_INT32 * p_tx1,
    181         OPJ_INT32 * p_ty0,
    182         OPJ_INT32 * p_ty1,
    183         OPJ_UINT32 * p_dx_min,
    184         OPJ_UINT32 * p_dy_min,
    185         OPJ_UINT32 * p_max_prec,
    186         OPJ_UINT32 * p_max_res,
    187         OPJ_UINT32 ** p_resolutions);
    188 /**
    189  * Allocates memory for a packet iterator. Data and data sizes are set by this operation.
    190  * No other data is set. The include section of the packet  iterator is not allocated.
    191  *
    192  * @param   p_image     the image used to initialize the packet iterator (in fact only the number of components is relevant.
    193  * @param   p_cp        the coding parameters.
    194  * @param   tileno  the index of the tile from which creating the packet iterator.
    195  */
    196 static opj_pi_iterator_t * opj_pi_create(const opj_image_t *p_image,
    197         const opj_cp_t *p_cp,
    198         OPJ_UINT32 tileno);
    199 /**
    200  * FIXME DOC
    201  */
    202 static void opj_pi_update_decode_not_poc(opj_pi_iterator_t * p_pi,
    203         opj_tcp_t * p_tcp,
    204         OPJ_UINT32 p_max_precision,
    205         OPJ_UINT32 p_max_res);
    206 /**
    207  * FIXME DOC
    208  */
    209 static void opj_pi_update_decode_poc(opj_pi_iterator_t * p_pi,
    210                                      opj_tcp_t * p_tcp,
    211                                      OPJ_UINT32 p_max_precision,
    212                                      OPJ_UINT32 p_max_res);
    213 
    214 /**
    215  * FIXME DOC
    216  */
    217 static OPJ_BOOL opj_pi_check_next_level(OPJ_INT32 pos,
    218                                         opj_cp_t *cp,
    219                                         OPJ_UINT32 tileno,
    220                                         OPJ_UINT32 pino,
    221                                         const OPJ_CHAR *prog);
    222 
    223 /*@}*/
    224 
    225 /*@}*/
    226 
    227 /*
    228 ==========================================================
    229    local functions
    230 ==========================================================
    231 */
    232 
    233 static void opj_pi_emit_error(opj_pi_iterator_t * pi, const char* msg)
    234 {
    235     (void)pi;
    236     (void)msg;
    237 }
    238 
    239 static OPJ_BOOL opj_pi_next_lrcp(opj_pi_iterator_t * pi)
    240 {
    241     opj_pi_comp_t *comp = NULL;
    242     opj_pi_resolution_t *res = NULL;
    243     OPJ_UINT32 index = 0;
    244 
    245     if (!pi->first) {
    246         comp = &pi->comps[pi->compno];
    247         res = &comp->resolutions[pi->resno];
    248         goto LABEL_SKIP;
    249     } else {
    250         pi->first = 0;
    251     }
    252 
    253     for (pi->layno = pi->poc.layno0; pi->layno < pi->poc.layno1; pi->layno++) {
    254         for (pi->resno = pi->poc.resno0; pi->resno < pi->poc.resno1;
    255                 pi->resno++) {
    256             for (pi->compno = pi->poc.compno0; pi->compno < pi->poc.compno1; pi->compno++) {
    257                 comp = &pi->comps[pi->compno];
    258                 if (pi->resno >= comp->numresolutions) {
    259                     continue;
    260                 }
    261                 res = &comp->resolutions[pi->resno];
    262                 if (!pi->tp_on) {
    263                     pi->poc.precno1 = res->pw * res->ph;
    264                 }
    265                 for (pi->precno = pi->poc.precno0; pi->precno < pi->poc.precno1; pi->precno++) {
    266                     index = pi->layno * pi->step_l + pi->resno * pi->step_r + pi->compno *
    267                             pi->step_c + pi->precno * pi->step_p;
    268                     /* Avoids index out of bounds access with */
    269                     /* id_000098,sig_11,src_005411,op_havoc,rep_2 of */
    270                     /* https://github.com/uclouvain/openjpeg/issues/938 */
    271                     /* Not sure if this is the most clever fix. Perhaps */
    272                     /* include should be resized when a POC arises, or */
    273                     /* the POC should be rejected */
    274                     if (index >= pi->include_size) {
    275                         opj_pi_emit_error(pi, "Invalid access to pi->include");
    276                         return OPJ_FALSE;
    277                     }
    278                     if (!pi->include[index]) {
    279                         pi->include[index] = 1;
    280                         return OPJ_TRUE;
    281                     }
    282 LABEL_SKIP:
    283                     ;
    284                 }
    285             }
    286         }
    287     }
    288 
    289     return OPJ_FALSE;
    290 }
    291 
    292 static OPJ_BOOL opj_pi_next_rlcp(opj_pi_iterator_t * pi)
    293 {
    294     opj_pi_comp_t *comp = NULL;
    295     opj_pi_resolution_t *res = NULL;
    296     OPJ_UINT32 index = 0;
    297 
    298     if (!pi->first) {
    299         comp = &pi->comps[pi->compno];
    300         res = &comp->resolutions[pi->resno];
    301         goto LABEL_SKIP;
    302     } else {
    303         pi->first = 0;
    304     }
    305 
    306     for (pi->resno = pi->poc.resno0; pi->resno < pi->poc.resno1; pi->resno++) {
    307         for (pi->layno = pi->poc.layno0; pi->layno < pi->poc.layno1; pi->layno++) {
    308             for (pi->compno = pi->poc.compno0; pi->compno < pi->poc.compno1; pi->compno++) {
    309                 comp = &pi->comps[pi->compno];
    310                 if (pi->resno >= comp->numresolutions) {
    311                     continue;
    312                 }
    313                 res = &comp->resolutions[pi->resno];
    314                 if (!pi->tp_on) {
    315                     pi->poc.precno1 = res->pw * res->ph;
    316                 }
    317                 for (pi->precno = pi->poc.precno0; pi->precno < pi->poc.precno1; pi->precno++) {
    318                     index = pi->layno * pi->step_l + pi->resno * pi->step_r + pi->compno *
    319                             pi->step_c + pi->precno * pi->step_p;
    320                     if (index >= pi->include_size) {
    321                         opj_pi_emit_error(pi, "Invalid access to pi->include");
    322                         return OPJ_FALSE;
    323                     }
    324                     if (!pi->include[index]) {
    325                         pi->include[index] = 1;
    326                         return OPJ_TRUE;
    327                     }
    328 LABEL_SKIP:
    329                     ;
    330                 }
    331             }
    332         }
    333     }
    334 
    335     return OPJ_FALSE;
    336 }
    337 
    338 static OPJ_BOOL opj_pi_next_rpcl(opj_pi_iterator_t * pi)
    339 {
    340     opj_pi_comp_t *comp = NULL;
    341     opj_pi_resolution_t *res = NULL;
    342     OPJ_UINT32 index = 0;
    343 
    344     if (!pi->first) {
    345         goto LABEL_SKIP;
    346     } else {
    347         OPJ_UINT32 compno, resno;
    348         pi->first = 0;
    349         pi->dx = 0;
    350         pi->dy = 0;
    351         for (compno = 0; compno < pi->numcomps; compno++) {
    352             comp = &pi->comps[compno];
    353             for (resno = 0; resno < comp->numresolutions; resno++) {
    354                 OPJ_UINT32 dx, dy;
    355                 res = &comp->resolutions[resno];
    356                 if (res->pdx + comp->numresolutions - 1 - resno < 32 &&
    357                         comp->dx <= UINT_MAX / (1u << (res->pdx + comp->numresolutions - 1 - resno))) {
    358                     dx = comp->dx * (1u << (res->pdx + comp->numresolutions - 1 - resno));
    359                     pi->dx = !pi->dx ? dx : opj_uint_min(pi->dx, dx);
    360                 }
    361                 if (res->pdy + comp->numresolutions - 1 - resno < 32 &&
    362                         comp->dy <= UINT_MAX / (1u << (res->pdy + comp->numresolutions - 1 - resno))) {
    363                     dy = comp->dy * (1u << (res->pdy + comp->numresolutions - 1 - resno));
    364                     pi->dy = !pi->dy ? dy : opj_uint_min(pi->dy, dy);
    365                 }
    366             }
    367         }
    368         if (pi->dx == 0 || pi->dy == 0) {
    369             return OPJ_FALSE;
    370         }
    371     }
    372     if (!pi->tp_on) {
    373         pi->poc.ty0 = pi->ty0;
    374         pi->poc.tx0 = pi->tx0;
    375         pi->poc.ty1 = pi->ty1;
    376         pi->poc.tx1 = pi->tx1;
    377     }
    378     for (pi->resno = pi->poc.resno0; pi->resno < pi->poc.resno1; pi->resno++) {
    379         for (pi->y = pi->poc.ty0; pi->y < pi->poc.ty1;
    380                 pi->y += (OPJ_INT32)(pi->dy - (OPJ_UINT32)(pi->y % (OPJ_INT32)pi->dy))) {
    381             for (pi->x = pi->poc.tx0; pi->x < pi->poc.tx1;
    382                     pi->x += (OPJ_INT32)(pi->dx - (OPJ_UINT32)(pi->x % (OPJ_INT32)pi->dx))) {
    383                 for (pi->compno = pi->poc.compno0; pi->compno < pi->poc.compno1; pi->compno++) {
    384                     OPJ_UINT32 levelno;
    385                     OPJ_INT32 trx0, try0;
    386                     OPJ_INT32  trx1, try1;
    387                     OPJ_UINT32  rpx, rpy;
    388                     OPJ_INT32  prci, prcj;
    389                     comp = &pi->comps[pi->compno];
    390                     if (pi->resno >= comp->numresolutions) {
    391                         continue;
    392                     }
    393                     res = &comp->resolutions[pi->resno];
    394                     levelno = comp->numresolutions - 1 - pi->resno;
    395                     /* Avoids division by zero */
    396                     /* Relates to id_000004,sig_06,src_000679,op_arith8,pos_49,val_-17 */
    397                     /* of  https://github.com/uclouvain/openjpeg/issues/938 */
    398                     if (levelno >= 32 ||
    399                             ((comp->dx << levelno) >> levelno) != comp->dx ||
    400                             ((comp->dy << levelno) >> levelno) != comp->dy) {
    401                         continue;
    402                     }
    403                     if ((comp->dx << levelno) > INT_MAX ||
    404                             (comp->dy << levelno) > INT_MAX) {
    405                         continue;
    406                     }
    407                     trx0 = opj_int_ceildiv(pi->tx0, (OPJ_INT32)(comp->dx << levelno));
    408                     try0 = opj_int_ceildiv(pi->ty0, (OPJ_INT32)(comp->dy << levelno));
    409                     trx1 = opj_int_ceildiv(pi->tx1, (OPJ_INT32)(comp->dx << levelno));
    410                     try1 = opj_int_ceildiv(pi->ty1, (OPJ_INT32)(comp->dy << levelno));
    411                     rpx = res->pdx + levelno;
    412                     rpy = res->pdy + levelno;
    413 
    414                     /* To avoid divisions by zero / undefined behaviour on shift */
    415                     /* in below tests */
    416                     /* Fixes reading id:000026,sig:08,src:002419,op:int32,pos:60,val:+32 */
    417                     /* of https://github.com/uclouvain/openjpeg/issues/938 */
    418                     if (rpx >= 31 || ((comp->dx << rpx) >> rpx) != comp->dx ||
    419                             rpy >= 31 || ((comp->dy << rpy) >> rpy) != comp->dy) {
    420                         continue;
    421                     }
    422 
    423                     /* See ISO-15441. B.12.1.3 Resolution level-position-component-layer progression */
    424                     if (!((pi->y % (OPJ_INT32)(comp->dy << rpy) == 0) || ((pi->y == pi->ty0) &&
    425                             ((try0 << levelno) % (1 << rpy))))) {
    426                         continue;
    427                     }
    428                     if (!((pi->x % (OPJ_INT32)(comp->dx << rpx) == 0) || ((pi->x == pi->tx0) &&
    429                             ((trx0 << levelno) % (1 << rpx))))) {
    430                         continue;
    431                     }
    432 
    433                     if ((res->pw == 0) || (res->ph == 0)) {
    434                         continue;
    435                     }
    436 
    437                     if ((trx0 == trx1) || (try0 == try1)) {
    438                         continue;
    439                     }
    440 
    441                     prci = opj_int_floordivpow2(opj_int_ceildiv(pi->x,
    442                                                 (OPJ_INT32)(comp->dx << levelno)), (OPJ_INT32)res->pdx)
    443                            - opj_int_floordivpow2(trx0, (OPJ_INT32)res->pdx);
    444                     prcj = opj_int_floordivpow2(opj_int_ceildiv(pi->y,
    445                                                 (OPJ_INT32)(comp->dy << levelno)), (OPJ_INT32)res->pdy)
    446                            - opj_int_floordivpow2(try0, (OPJ_INT32)res->pdy);
    447                     pi->precno = (OPJ_UINT32)(prci + prcj * (OPJ_INT32)res->pw);
    448                     if (pi->precno >= res->pw * res->ph) {
    449                       return OPJ_FALSE;
    450                     }
    451                     for (pi->layno = pi->poc.layno0; pi->layno < pi->poc.layno1; pi->layno++) {
    452                         index = pi->layno * pi->step_l + pi->resno * pi->step_r + pi->compno *
    453                                 pi->step_c + pi->precno * pi->step_p;
    454                         if (index >= pi->include_size) {
    455                             opj_pi_emit_error(pi, "Invalid access to pi->include");
    456                             return OPJ_FALSE;
    457                         }
    458                         if (!pi->include[index]) {
    459                             pi->include[index] = 1;
    460                             return OPJ_TRUE;
    461                         }
    462 LABEL_SKIP:
    463                         ;
    464                     }
    465                 }
    466             }
    467         }
    468     }
    469 
    470     return OPJ_FALSE;
    471 }
    472 
    473 static OPJ_BOOL opj_pi_next_pcrl(opj_pi_iterator_t * pi)
    474 {
    475     opj_pi_comp_t *comp = NULL;
    476     opj_pi_resolution_t *res = NULL;
    477     OPJ_UINT32 index = 0;
    478 
    479     if (!pi->first) {
    480         comp = &pi->comps[pi->compno];
    481         goto LABEL_SKIP;
    482     } else {
    483         OPJ_UINT32 compno, resno;
    484         pi->first = 0;
    485         pi->dx = 0;
    486         pi->dy = 0;
    487         for (compno = 0; compno < pi->numcomps; compno++) {
    488             comp = &pi->comps[compno];
    489             for (resno = 0; resno < comp->numresolutions; resno++) {
    490                 OPJ_UINT32 dx, dy;
    491                 res = &comp->resolutions[resno];
    492                 if (res->pdx + comp->numresolutions - 1 - resno < 32 &&
    493                         comp->dx <= UINT_MAX / (1u << (res->pdx + comp->numresolutions - 1 - resno))) {
    494                     dx = comp->dx * (1u << (res->pdx + comp->numresolutions - 1 - resno));
    495                     pi->dx = !pi->dx ? dx : opj_uint_min(pi->dx, dx);
    496                 }
    497                 if (res->pdy + comp->numresolutions - 1 - resno < 32 &&
    498                         comp->dy <= UINT_MAX / (1u << (res->pdy + comp->numresolutions - 1 - resno))) {
    499                     dy = comp->dy * (1u << (res->pdy + comp->numresolutions - 1 - resno));
    500                     pi->dy = !pi->dy ? dy : opj_uint_min(pi->dy, dy);
    501                 }
    502             }
    503         }
    504         if (pi->dx == 0 || pi->dy == 0) {
    505             return OPJ_FALSE;
    506         }
    507     }
    508     if (!pi->tp_on) {
    509         pi->poc.ty0 = pi->ty0;
    510         pi->poc.tx0 = pi->tx0;
    511         pi->poc.ty1 = pi->ty1;
    512         pi->poc.tx1 = pi->tx1;
    513     }
    514     for (pi->y = pi->poc.ty0; pi->y < pi->poc.ty1;
    515             pi->y += (OPJ_INT32)(pi->dy - (OPJ_UINT32)(pi->y % (OPJ_INT32)pi->dy))) {
    516         for (pi->x = pi->poc.tx0; pi->x < pi->poc.tx1;
    517                 pi->x += (OPJ_INT32)(pi->dx - (OPJ_UINT32)(pi->x % (OPJ_INT32)pi->dx))) {
    518             for (pi->compno = pi->poc.compno0; pi->compno < pi->poc.compno1; pi->compno++) {
    519                 comp = &pi->comps[pi->compno];
    520                 for (pi->resno = pi->poc.resno0;
    521                         pi->resno < opj_uint_min(pi->poc.resno1, comp->numresolutions); pi->resno++) {
    522                     OPJ_UINT32 levelno;
    523                     OPJ_INT32 trx0, try0;
    524                     OPJ_INT32 trx1, try1;
    525                     OPJ_UINT32 rpx, rpy;
    526                     OPJ_INT32 prci, prcj;
    527                     res = &comp->resolutions[pi->resno];
    528                     levelno = comp->numresolutions - 1 - pi->resno;
    529                     /* Avoids division by zero */
    530                     /* Relates to id_000004,sig_06,src_000679,op_arith8,pos_49,val_-17 */
    531                     /* of  https://github.com/uclouvain/openjpeg/issues/938 */
    532                     if (levelno >= 32 ||
    533                             ((comp->dx << levelno) >> levelno) != comp->dx ||
    534                             ((comp->dy << levelno) >> levelno) != comp->dy) {
    535                         continue;
    536                     }
    537                     if ((comp->dx << levelno) > INT_MAX ||
    538                             (comp->dy << levelno) > INT_MAX) {
    539                         continue;
    540                     }
    541                     trx0 = opj_int_ceildiv(pi->tx0, (OPJ_INT32)(comp->dx << levelno));
    542                     try0 = opj_int_ceildiv(pi->ty0, (OPJ_INT32)(comp->dy << levelno));
    543                     trx1 = opj_int_ceildiv(pi->tx1, (OPJ_INT32)(comp->dx << levelno));
    544                     try1 = opj_int_ceildiv(pi->ty1, (OPJ_INT32)(comp->dy << levelno));
    545                     rpx = res->pdx + levelno;
    546                     rpy = res->pdy + levelno;
    547 
    548                     /* To avoid divisions by zero / undefined behaviour on shift */
    549                     /* in below tests */
    550                     /* Relates to id:000019,sig:08,src:001098,op:flip1,pos:49 */
    551                     /* of https://github.com/uclouvain/openjpeg/issues/938 */
    552                     if (rpx >= 31 || ((comp->dx << rpx) >> rpx) != comp->dx ||
    553                             rpy >= 31 || ((comp->dy << rpy) >> rpy) != comp->dy) {
    554                         continue;
    555                     }
    556 
    557                     /* See ISO-15441. B.12.1.4 Position-component-resolution level-layer progression */
    558                     if (!((pi->y % (OPJ_INT32)(comp->dy << rpy) == 0) || ((pi->y == pi->ty0) &&
    559                             ((try0 << levelno) % (1 << rpy))))) {
    560                         continue;
    561                     }
    562                     if (!((pi->x % (OPJ_INT32)(comp->dx << rpx) == 0) || ((pi->x == pi->tx0) &&
    563                             ((trx0 << levelno) % (1 << rpx))))) {
    564                         continue;
    565                     }
    566 
    567                     if ((res->pw == 0) || (res->ph == 0)) {
    568                         continue;
    569                     }
    570 
    571                     if ((trx0 == trx1) || (try0 == try1)) {
    572                         continue;
    573                     }
    574 
    575                     prci = opj_int_floordivpow2(opj_int_ceildiv(pi->x,
    576                                                 (OPJ_INT32)(comp->dx << levelno)), (OPJ_INT32)res->pdx)
    577                            - opj_int_floordivpow2(trx0, (OPJ_INT32)res->pdx);
    578                     prcj = opj_int_floordivpow2(opj_int_ceildiv(pi->y,
    579                                                 (OPJ_INT32)(comp->dy << levelno)), (OPJ_INT32)res->pdy)
    580                            - opj_int_floordivpow2(try0, (OPJ_INT32)res->pdy);
    581                     pi->precno = (OPJ_UINT32)(prci + prcj * (OPJ_INT32)res->pw);
    582                     if (pi->precno >= res->pw * res->ph) {
    583                       return OPJ_FALSE;
    584                     }
    585                     for (pi->layno = pi->poc.layno0; pi->layno < pi->poc.layno1; pi->layno++) {
    586                         index = pi->layno * pi->step_l + pi->resno * pi->step_r + pi->compno *
    587                                 pi->step_c + pi->precno * pi->step_p;
    588                         if (index >= pi->include_size) {
    589                             opj_pi_emit_error(pi, "Invalid access to pi->include");
    590                             return OPJ_FALSE;
    591                         }
    592                         if (!pi->include[index]) {
    593                             pi->include[index] = 1;
    594                             return OPJ_TRUE;
    595                         }
    596 LABEL_SKIP:
    597                         ;
    598                     }
    599                 }
    600             }
    601         }
    602     }
    603 
    604     return OPJ_FALSE;
    605 }
    606 
    607 static OPJ_BOOL opj_pi_next_cprl(opj_pi_iterator_t * pi)
    608 {
    609     opj_pi_comp_t *comp = NULL;
    610     opj_pi_resolution_t *res = NULL;
    611     OPJ_UINT32 index = 0;
    612 
    613     if (!pi->first) {
    614         comp = &pi->comps[pi->compno];
    615         goto LABEL_SKIP;
    616     } else {
    617         pi->first = 0;
    618     }
    619 
    620     for (pi->compno = pi->poc.compno0; pi->compno < pi->poc.compno1; pi->compno++) {
    621         OPJ_UINT32 resno;
    622         comp = &pi->comps[pi->compno];
    623         pi->dx = 0;
    624         pi->dy = 0;
    625         for (resno = 0; resno < comp->numresolutions; resno++) {
    626             OPJ_UINT32 dx, dy;
    627             res = &comp->resolutions[resno];
    628             if (res->pdx + comp->numresolutions - 1 - resno < 32 &&
    629                     comp->dx <= UINT_MAX / (1u << (res->pdx + comp->numresolutions - 1 - resno))) {
    630                 dx = comp->dx * (1u << (res->pdx + comp->numresolutions - 1 - resno));
    631                 pi->dx = !pi->dx ? dx : opj_uint_min(pi->dx, dx);
    632             }
    633             if (res->pdy + comp->numresolutions - 1 - resno < 32 &&
    634                     comp->dy <= UINT_MAX / (1u << (res->pdy + comp->numresolutions - 1 - resno))) {
    635                 dy = comp->dy * (1u << (res->pdy + comp->numresolutions - 1 - resno));
    636                 pi->dy = !pi->dy ? dy : opj_uint_min(pi->dy, dy);
    637             }
    638         }
    639         if (pi->dx == 0 || pi->dy == 0) {
    640             return OPJ_FALSE;
    641         }
    642         if (!pi->tp_on) {
    643             pi->poc.ty0 = pi->ty0;
    644             pi->poc.tx0 = pi->tx0;
    645             pi->poc.ty1 = pi->ty1;
    646             pi->poc.tx1 = pi->tx1;
    647         }
    648         for (pi->y = pi->poc.ty0; pi->y < pi->poc.ty1;
    649                 pi->y += (OPJ_INT32)(pi->dy - (OPJ_UINT32)(pi->y % (OPJ_INT32)pi->dy))) {
    650             for (pi->x = pi->poc.tx0; pi->x < pi->poc.tx1;
    651                     pi->x += (OPJ_INT32)(pi->dx - (OPJ_UINT32)(pi->x % (OPJ_INT32)pi->dx))) {
    652                 for (pi->resno = pi->poc.resno0;
    653                         pi->resno < opj_uint_min(pi->poc.resno1, comp->numresolutions); pi->resno++) {
    654                     OPJ_UINT32 levelno;
    655                     OPJ_INT32 trx0, try0;
    656                     OPJ_INT32 trx1, try1;
    657                     OPJ_UINT32 rpx, rpy;
    658                     OPJ_INT32 prci, prcj;
    659                     res = &comp->resolutions[pi->resno];
    660                     levelno = comp->numresolutions - 1 - pi->resno;
    661                     /* Avoids division by zero on id_000004,sig_06,src_000679,op_arith8,pos_49,val_-17 */
    662                     /* of  https://github.com/uclouvain/openjpeg/issues/938 */
    663                     if (levelno >= 32 ||
    664                             ((comp->dx << levelno) >> levelno) != comp->dx ||
    665                             ((comp->dy << levelno) >> levelno) != comp->dy) {
    666                         continue;
    667                     }
    668                     if ((comp->dx << levelno) > INT_MAX ||
    669                             (comp->dy << levelno) > INT_MAX) {
    670                         continue;
    671                     }
    672                     trx0 = opj_int_ceildiv(pi->tx0, (OPJ_INT32)(comp->dx << levelno));
    673                     try0 = opj_int_ceildiv(pi->ty0, (OPJ_INT32)(comp->dy << levelno));
    674                     trx1 = opj_int_ceildiv(pi->tx1, (OPJ_INT32)(comp->dx << levelno));
    675                     try1 = opj_int_ceildiv(pi->ty1, (OPJ_INT32)(comp->dy << levelno));
    676                     rpx = res->pdx + levelno;
    677                     rpy = res->pdy + levelno;
    678 
    679                     /* To avoid divisions by zero / undefined behaviour on shift */
    680                     /* in below tests */
    681                     /* Fixes reading id:000019,sig:08,src:001098,op:flip1,pos:49 */
    682                     /* of https://github.com/uclouvain/openjpeg/issues/938 */
    683                     if (rpx >= 31 || ((comp->dx << rpx) >> rpx) != comp->dx ||
    684                             rpy >= 31 || ((comp->dy << rpy) >> rpy) != comp->dy) {
    685                         continue;
    686                     }
    687 
    688                     /* See ISO-15441. B.12.1.5 Component-position-resolution level-layer progression */
    689                     if (!((pi->y % (OPJ_INT32)(comp->dy << rpy) == 0) || ((pi->y == pi->ty0) &&
    690                             ((try0 << levelno) % (1 << rpy))))) {
    691                         continue;
    692                     }
    693                     if (!((pi->x % (OPJ_INT32)(comp->dx << rpx) == 0) || ((pi->x == pi->tx0) &&
    694                             ((trx0 << levelno) % (1 << rpx))))) {
    695                         continue;
    696                     }
    697 
    698                     if ((res->pw == 0) || (res->ph == 0)) {
    699                         continue;
    700                     }
    701 
    702                     if ((trx0 == trx1) || (try0 == try1)) {
    703                         continue;
    704                     }
    705 
    706                     prci = opj_int_floordivpow2(opj_int_ceildiv(pi->x,
    707                                                 (OPJ_INT32)(comp->dx << levelno)), (OPJ_INT32)res->pdx)
    708                            - opj_int_floordivpow2(trx0, (OPJ_INT32)res->pdx);
    709                     prcj = opj_int_floordivpow2(opj_int_ceildiv(pi->y,
    710                                                 (OPJ_INT32)(comp->dy << levelno)), (OPJ_INT32)res->pdy)
    711                            - opj_int_floordivpow2(try0, (OPJ_INT32)res->pdy);
    712                     pi->precno = (OPJ_UINT32)(prci + prcj * (OPJ_INT32)res->pw);
    713                     if (pi->precno >= res->pw * res->ph) {
    714                       return OPJ_FALSE;
    715                     }
    716                     for (pi->layno = pi->poc.layno0; pi->layno < pi->poc.layno1; pi->layno++) {
    717                         index = pi->layno * pi->step_l + pi->resno * pi->step_r + pi->compno *
    718                                 pi->step_c + pi->precno * pi->step_p;
    719                         if (index >= pi->include_size) {
    720                             opj_pi_emit_error(pi, "Invalid access to pi->include");
    721                             return OPJ_FALSE;
    722                         }
    723                         if (!pi->include[index]) {
    724                             pi->include[index] = 1;
    725                             return OPJ_TRUE;
    726                         }
    727 LABEL_SKIP:
    728                         ;
    729                     }
    730                 }
    731             }
    732         }
    733     }
    734 
    735     return OPJ_FALSE;
    736 }
    737 
    738 static void opj_get_encoding_parameters(const opj_image_t *p_image,
    739                                         const opj_cp_t *p_cp,
    740                                         OPJ_UINT32 p_tileno,
    741                                         OPJ_INT32 * p_tx0,
    742                                         OPJ_INT32  * p_tx1,
    743                                         OPJ_INT32  * p_ty0,
    744                                         OPJ_INT32  * p_ty1,
    745                                         OPJ_UINT32 * p_dx_min,
    746                                         OPJ_UINT32 * p_dy_min,
    747                                         OPJ_UINT32 * p_max_prec,
    748                                         OPJ_UINT32 * p_max_res)
    749 {
    750     /* loop */
    751     OPJ_UINT32  compno, resno;
    752     /* pointers */
    753     const opj_tcp_t *l_tcp = 00;
    754     const opj_tccp_t * l_tccp = 00;
    755     const opj_image_comp_t * l_img_comp = 00;
    756 
    757     /* position in x and y of tile */
    758     OPJ_UINT32 p, q;
    759 
    760     /* preconditions */
    761     assert(p_cp != 00);
    762     assert(p_image != 00);
    763     assert(p_tileno < p_cp->tw * p_cp->th);
    764 
    765     /* initializations */
    766     l_tcp = &p_cp->tcps [p_tileno];
    767     l_img_comp = p_image->comps;
    768     l_tccp = l_tcp->tccps;
    769 
    770     /* here calculation of tx0, tx1, ty0, ty1, maxprec, dx and dy */
    771     p = p_tileno % p_cp->tw;
    772     q = p_tileno / p_cp->tw;
    773 
    774     /* find extent of tile */
    775     *p_tx0 = opj_int_max((OPJ_INT32)(p_cp->tx0 + p * p_cp->tdx),
    776                          (OPJ_INT32)p_image->x0);
    777     *p_tx1 = opj_int_min((OPJ_INT32)(p_cp->tx0 + (p + 1) * p_cp->tdx),
    778                          (OPJ_INT32)p_image->x1);
    779     *p_ty0 = opj_int_max((OPJ_INT32)(p_cp->ty0 + q * p_cp->tdy),
    780                          (OPJ_INT32)p_image->y0);
    781     *p_ty1 = opj_int_min((OPJ_INT32)(p_cp->ty0 + (q + 1) * p_cp->tdy),
    782                          (OPJ_INT32)p_image->y1);
    783 
    784     /* max precision is 0 (can only grow) */
    785     *p_max_prec = 0;
    786     *p_max_res = 0;
    787 
    788     /* take the largest value for dx_min and dy_min */
    789     *p_dx_min = 0x7fffffff;
    790     *p_dy_min  = 0x7fffffff;
    791 
    792     for (compno = 0; compno < p_image->numcomps; ++compno) {
    793         /* arithmetic variables to calculate */
    794         OPJ_UINT32 l_level_no;
    795         OPJ_INT32 l_rx0, l_ry0, l_rx1, l_ry1;
    796         OPJ_INT32 l_px0, l_py0, l_px1, py1;
    797         OPJ_UINT32 l_pdx, l_pdy;
    798         OPJ_UINT32 l_pw, l_ph;
    799         OPJ_UINT32 l_product;
    800         OPJ_INT32 l_tcx0, l_tcy0, l_tcx1, l_tcy1;
    801 
    802         l_tcx0 = opj_int_ceildiv(*p_tx0, (OPJ_INT32)l_img_comp->dx);
    803         l_tcy0 = opj_int_ceildiv(*p_ty0, (OPJ_INT32)l_img_comp->dy);
    804         l_tcx1 = opj_int_ceildiv(*p_tx1, (OPJ_INT32)l_img_comp->dx);
    805         l_tcy1 = opj_int_ceildiv(*p_ty1, (OPJ_INT32)l_img_comp->dy);
    806 
    807         if (l_tccp->numresolutions > *p_max_res) {
    808             *p_max_res = l_tccp->numresolutions;
    809         }
    810 
    811         /* use custom size for precincts */
    812         for (resno = 0; resno < l_tccp->numresolutions; ++resno) {
    813             OPJ_UINT32 l_dx, l_dy;
    814 
    815             /* precinct width and height */
    816             l_pdx = l_tccp->prcw[resno];
    817             l_pdy = l_tccp->prch[resno];
    818 
    819             l_dx = l_img_comp->dx * (1u << (l_pdx + l_tccp->numresolutions - 1 - resno));
    820             l_dy = l_img_comp->dy * (1u << (l_pdy + l_tccp->numresolutions - 1 - resno));
    821 
    822             /* take the minimum size for dx for each comp and resolution */
    823             *p_dx_min = opj_uint_min(*p_dx_min, l_dx);
    824             *p_dy_min = opj_uint_min(*p_dy_min, l_dy);
    825 
    826             /* various calculations of extents */
    827             l_level_no = l_tccp->numresolutions - 1 - resno;
    828 
    829             l_rx0 = opj_int_ceildivpow2(l_tcx0, (OPJ_INT32)l_level_no);
    830             l_ry0 = opj_int_ceildivpow2(l_tcy0, (OPJ_INT32)l_level_no);
    831             l_rx1 = opj_int_ceildivpow2(l_tcx1, (OPJ_INT32)l_level_no);
    832             l_ry1 = opj_int_ceildivpow2(l_tcy1, (OPJ_INT32)l_level_no);
    833 
    834             l_px0 = opj_int_floordivpow2(l_rx0, (OPJ_INT32)l_pdx) << l_pdx;
    835             l_py0 = opj_int_floordivpow2(l_ry0, (OPJ_INT32)l_pdy) << l_pdy;
    836             l_px1 = opj_int_ceildivpow2(l_rx1, (OPJ_INT32)l_pdx) << l_pdx;
    837 
    838             py1 = opj_int_ceildivpow2(l_ry1, (OPJ_INT32)l_pdy) << l_pdy;
    839 
    840             l_pw = (l_rx0 == l_rx1) ? 0 : (OPJ_UINT32)((l_px1 - l_px0) >> l_pdx);
    841             l_ph = (l_ry0 == l_ry1) ? 0 : (OPJ_UINT32)((py1 - l_py0) >> l_pdy);
    842 
    843             l_product = l_pw * l_ph;
    844 
    845             /* update precision */
    846             if (l_product > *p_max_prec) {
    847                 *p_max_prec = l_product;
    848             }
    849         }
    850         ++l_img_comp;
    851         ++l_tccp;
    852     }
    853 }
    854 
    855 
    856 static void opj_get_all_encoding_parameters(const opj_image_t *p_image,
    857         const opj_cp_t *p_cp,
    858         OPJ_UINT32 tileno,
    859         OPJ_INT32 * p_tx0,
    860         OPJ_INT32 * p_tx1,
    861         OPJ_INT32 * p_ty0,
    862         OPJ_INT32 * p_ty1,
    863         OPJ_UINT32 * p_dx_min,
    864         OPJ_UINT32 * p_dy_min,
    865         OPJ_UINT32 * p_max_prec,
    866         OPJ_UINT32 * p_max_res,
    867         OPJ_UINT32 ** p_resolutions)
    868 {
    869     /* loop*/
    870     OPJ_UINT32 compno, resno;
    871 
    872     /* pointers*/
    873     const opj_tcp_t *tcp = 00;
    874     const opj_tccp_t * l_tccp = 00;
    875     const opj_image_comp_t * l_img_comp = 00;
    876 
    877     /* to store l_dx, l_dy, w and h for each resolution and component.*/
    878     OPJ_UINT32 * lResolutionPtr;
    879 
    880     /* position in x and y of tile*/
    881     OPJ_UINT32 p, q;
    882 
    883     /* non-corrected (in regard to image offset) tile offset */
    884     OPJ_UINT32 l_tx0, l_ty0;
    885 
    886     /* preconditions in debug*/
    887     assert(p_cp != 00);
    888     assert(p_image != 00);
    889     assert(tileno < p_cp->tw * p_cp->th);
    890 
    891     /* initializations*/
    892     tcp = &p_cp->tcps [tileno];
    893     l_tccp = tcp->tccps;
    894     l_img_comp = p_image->comps;
    895 
    896     /* position in x and y of tile*/
    897     p = tileno % p_cp->tw;
    898     q = tileno / p_cp->tw;
    899 
    900     /* here calculation of tx0, tx1, ty0, ty1, maxprec, l_dx and l_dy */
    901     l_tx0 = p_cp->tx0 + p *
    902             p_cp->tdx; /* can't be greater than p_image->x1 so won't overflow */
    903     *p_tx0 = (OPJ_INT32)opj_uint_max(l_tx0, p_image->x0);
    904     *p_tx1 = (OPJ_INT32)opj_uint_min(opj_uint_adds(l_tx0, p_cp->tdx), p_image->x1);
    905     l_ty0 = p_cp->ty0 + q *
    906             p_cp->tdy; /* can't be greater than p_image->y1 so won't overflow */
    907     *p_ty0 = (OPJ_INT32)opj_uint_max(l_ty0, p_image->y0);
    908     *p_ty1 = (OPJ_INT32)opj_uint_min(opj_uint_adds(l_ty0, p_cp->tdy), p_image->y1);
    909 
    910     /* max precision and resolution is 0 (can only grow)*/
    911     *p_max_prec = 0;
    912     *p_max_res = 0;
    913 
    914     /* take the largest value for dx_min and dy_min*/
    915     *p_dx_min = 0x7fffffff;
    916     *p_dy_min = 0x7fffffff;
    917 
    918     for (compno = 0; compno < p_image->numcomps; ++compno) {
    919         /* aritmetic variables to calculate*/
    920         OPJ_UINT32 l_level_no;
    921         OPJ_INT32 l_rx0, l_ry0, l_rx1, l_ry1;
    922         OPJ_INT32 l_px0, l_py0, l_px1, py1;
    923         OPJ_UINT32 l_product;
    924         OPJ_INT32 l_tcx0, l_tcy0, l_tcx1, l_tcy1;
    925         OPJ_UINT32 l_pdx, l_pdy, l_pw, l_ph;
    926 
    927         lResolutionPtr = p_resolutions[compno];
    928 
    929         l_tcx0 = opj_int_ceildiv(*p_tx0, (OPJ_INT32)l_img_comp->dx);
    930         l_tcy0 = opj_int_ceildiv(*p_ty0, (OPJ_INT32)l_img_comp->dy);
    931         l_tcx1 = opj_int_ceildiv(*p_tx1, (OPJ_INT32)l_img_comp->dx);
    932         l_tcy1 = opj_int_ceildiv(*p_ty1, (OPJ_INT32)l_img_comp->dy);
    933 
    934         if (l_tccp->numresolutions > *p_max_res) {
    935             *p_max_res = l_tccp->numresolutions;
    936         }
    937 
    938         /* use custom size for precincts*/
    939         l_level_no = l_tccp->numresolutions;
    940         for (resno = 0; resno < l_tccp->numresolutions; ++resno) {
    941             OPJ_UINT32 l_dx, l_dy;
    942 
    943             --l_level_no;
    944 
    945             /* precinct width and height*/
    946             l_pdx = l_tccp->prcw[resno];
    947             l_pdy = l_tccp->prch[resno];
    948             *lResolutionPtr++ = l_pdx;
    949             *lResolutionPtr++ = l_pdy;
    950             if (l_pdx + l_level_no < 32 &&
    951                     l_img_comp->dx <= UINT_MAX / (1u << (l_pdx + l_level_no))) {
    952                 l_dx = l_img_comp->dx * (1u << (l_pdx + l_level_no));
    953                 /* take the minimum size for l_dx for each comp and resolution*/
    954                 *p_dx_min = (OPJ_UINT32)opj_int_min((OPJ_INT32) * p_dx_min, (OPJ_INT32)l_dx);
    955             }
    956             if (l_pdy + l_level_no < 32 &&
    957                     l_img_comp->dy <= UINT_MAX / (1u << (l_pdy + l_level_no))) {
    958                 l_dy = l_img_comp->dy * (1u << (l_pdy + l_level_no));
    959                 *p_dy_min = (OPJ_UINT32)opj_int_min((OPJ_INT32) * p_dy_min, (OPJ_INT32)l_dy);
    960             }
    961 
    962             /* various calculations of extents*/
    963             l_rx0 = opj_int_ceildivpow2(l_tcx0, (OPJ_INT32)l_level_no);
    964             l_ry0 = opj_int_ceildivpow2(l_tcy0, (OPJ_INT32)l_level_no);
    965             l_rx1 = opj_int_ceildivpow2(l_tcx1, (OPJ_INT32)l_level_no);
    966             l_ry1 = opj_int_ceildivpow2(l_tcy1, (OPJ_INT32)l_level_no);
    967             l_px0 = opj_int_floordivpow2(l_rx0, (OPJ_INT32)l_pdx) << l_pdx;
    968             l_py0 = opj_int_floordivpow2(l_ry0, (OPJ_INT32)l_pdy) << l_pdy;
    969             l_px1 = opj_int_ceildivpow2(l_rx1, (OPJ_INT32)l_pdx) << l_pdx;
    970             py1 = opj_int_ceildivpow2(l_ry1, (OPJ_INT32)l_pdy) << l_pdy;
    971             l_pw = (l_rx0 == l_rx1) ? 0 : (OPJ_UINT32)((l_px1 - l_px0) >> l_pdx);
    972             l_ph = (l_ry0 == l_ry1) ? 0 : (OPJ_UINT32)((py1 - l_py0) >> l_pdy);
    973             *lResolutionPtr++ = l_pw;
    974             *lResolutionPtr++ = l_ph;
    975             l_product = l_pw * l_ph;
    976 
    977             /* update precision*/
    978             if (l_product > *p_max_prec) {
    979                 *p_max_prec = l_product;
    980             }
    981 
    982         }
    983         ++l_tccp;
    984         ++l_img_comp;
    985     }
    986 }
    987 
    988 static opj_pi_iterator_t * opj_pi_create(const opj_image_t *image,
    989         const opj_cp_t *cp,
    990         OPJ_UINT32 tileno)
    991 {
    992     /* loop*/
    993     OPJ_UINT32 pino, compno;
    994     /* number of poc in the p_pi*/
    995     OPJ_UINT32 l_poc_bound;
    996 
    997     /* pointers to tile coding parameters and components.*/
    998     opj_pi_iterator_t *l_pi = 00;
    999     opj_tcp_t *tcp = 00;
   1000     const opj_tccp_t *tccp = 00;
   1001 
   1002     /* current packet iterator being allocated*/
   1003     opj_pi_iterator_t *l_current_pi = 00;
   1004 
   1005     /* preconditions in debug*/
   1006     assert(cp != 00);
   1007     assert(image != 00);
   1008     assert(tileno < cp->tw * cp->th);
   1009 
   1010     /* initializations*/
   1011     tcp = &cp->tcps[tileno];
   1012     l_poc_bound = tcp->numpocs + 1;
   1013 
   1014     /* memory allocations*/
   1015     l_pi = (opj_pi_iterator_t*) opj_calloc((l_poc_bound),
   1016                                            sizeof(opj_pi_iterator_t));
   1017     if (!l_pi) {
   1018         return NULL;
   1019     }
   1020 
   1021     l_current_pi = l_pi;
   1022     for (pino = 0; pino < l_poc_bound ; ++pino) {
   1023 
   1024         l_current_pi->comps = (opj_pi_comp_t*) opj_calloc(image->numcomps,
   1025                               sizeof(opj_pi_comp_t));
   1026         if (! l_current_pi->comps) {
   1027             opj_pi_destroy(l_pi, l_poc_bound);
   1028             return NULL;
   1029         }
   1030 
   1031         l_current_pi->numcomps = image->numcomps;
   1032 
   1033         for (compno = 0; compno < image->numcomps; ++compno) {
   1034             opj_pi_comp_t *comp = &l_current_pi->comps[compno];
   1035 
   1036             tccp = &tcp->tccps[compno];
   1037 
   1038             comp->resolutions = (opj_pi_resolution_t*) opj_calloc(tccp->numresolutions,
   1039                                 sizeof(opj_pi_resolution_t));
   1040             if (!comp->resolutions) {
   1041                 opj_pi_destroy(l_pi, l_poc_bound);
   1042                 return 00;
   1043             }
   1044 
   1045             comp->numresolutions = tccp->numresolutions;
   1046         }
   1047         ++l_current_pi;
   1048     }
   1049     return l_pi;
   1050 }
   1051 
   1052 static void opj_pi_update_encode_poc_and_final(opj_cp_t *p_cp,
   1053         OPJ_UINT32 p_tileno,
   1054         OPJ_INT32 p_tx0,
   1055         OPJ_INT32 p_tx1,
   1056         OPJ_INT32 p_ty0,
   1057         OPJ_INT32 p_ty1,
   1058         OPJ_UINT32 p_max_prec,
   1059         OPJ_UINT32 p_max_res,
   1060         OPJ_UINT32 p_dx_min,
   1061         OPJ_UINT32 p_dy_min)
   1062 {
   1063     /* loop*/
   1064     OPJ_UINT32 pino;
   1065     /* tile coding parameter*/
   1066     opj_tcp_t *l_tcp = 00;
   1067     /* current poc being updated*/
   1068     opj_poc_t * l_current_poc = 00;
   1069 
   1070     /* number of pocs*/
   1071     OPJ_UINT32 l_poc_bound;
   1072 
   1073     OPJ_ARG_NOT_USED(p_max_res);
   1074 
   1075     /* preconditions in debug*/
   1076     assert(p_cp != 00);
   1077     assert(p_tileno < p_cp->tw * p_cp->th);
   1078 
   1079     /* initializations*/
   1080     l_tcp = &p_cp->tcps [p_tileno];
   1081     /* number of iterations in the loop */
   1082     l_poc_bound = l_tcp->numpocs + 1;
   1083 
   1084     /* start at first element, and to make sure the compiler will not make a calculation each time in the loop
   1085        store a pointer to the current element to modify rather than l_tcp->pocs[i]*/
   1086     l_current_poc = l_tcp->pocs;
   1087 
   1088     l_current_poc->compS = l_current_poc->compno0;
   1089     l_current_poc->compE = l_current_poc->compno1;
   1090     l_current_poc->resS = l_current_poc->resno0;
   1091     l_current_poc->resE = l_current_poc->resno1;
   1092     l_current_poc->layE = l_current_poc->layno1;
   1093 
   1094     /* special treatment for the first element*/
   1095     l_current_poc->layS = 0;
   1096     l_current_poc->prg  = l_current_poc->prg1;
   1097     l_current_poc->prcS = 0;
   1098 
   1099     l_current_poc->prcE = p_max_prec;
   1100     l_current_poc->txS = (OPJ_UINT32)p_tx0;
   1101     l_current_poc->txE = (OPJ_UINT32)p_tx1;
   1102     l_current_poc->tyS = (OPJ_UINT32)p_ty0;
   1103     l_current_poc->tyE = (OPJ_UINT32)p_ty1;
   1104     l_current_poc->dx = p_dx_min;
   1105     l_current_poc->dy = p_dy_min;
   1106 
   1107     ++ l_current_poc;
   1108     for (pino = 1; pino < l_poc_bound ; ++pino) {
   1109         l_current_poc->compS = l_current_poc->compno0;
   1110         l_current_poc->compE = l_current_poc->compno1;
   1111         l_current_poc->resS = l_current_poc->resno0;
   1112         l_current_poc->resE = l_current_poc->resno1;
   1113         l_current_poc->layE = l_current_poc->layno1;
   1114         l_current_poc->prg  = l_current_poc->prg1;
   1115         l_current_poc->prcS = 0;
   1116         /* special treatment here different from the first element*/
   1117         l_current_poc->layS = (l_current_poc->layE > (l_current_poc - 1)->layE) ?
   1118                               l_current_poc->layE : 0;
   1119 
   1120         l_current_poc->prcE = p_max_prec;
   1121         l_current_poc->txS = (OPJ_UINT32)p_tx0;
   1122         l_current_poc->txE = (OPJ_UINT32)p_tx1;
   1123         l_current_poc->tyS = (OPJ_UINT32)p_ty0;
   1124         l_current_poc->tyE = (OPJ_UINT32)p_ty1;
   1125         l_current_poc->dx = p_dx_min;
   1126         l_current_poc->dy = p_dy_min;
   1127         ++ l_current_poc;
   1128     }
   1129 }
   1130 
   1131 static void opj_pi_update_encode_not_poc(opj_cp_t *p_cp,
   1132         OPJ_UINT32 p_num_comps,
   1133         OPJ_UINT32 p_tileno,
   1134         OPJ_INT32 p_tx0,
   1135         OPJ_INT32 p_tx1,
   1136         OPJ_INT32 p_ty0,
   1137         OPJ_INT32 p_ty1,
   1138         OPJ_UINT32 p_max_prec,
   1139         OPJ_UINT32 p_max_res,
   1140         OPJ_UINT32 p_dx_min,
   1141         OPJ_UINT32 p_dy_min)
   1142 {
   1143     /* loop*/
   1144     OPJ_UINT32 pino;
   1145     /* tile coding parameter*/
   1146     opj_tcp_t *l_tcp = 00;
   1147     /* current poc being updated*/
   1148     opj_poc_t * l_current_poc = 00;
   1149     /* number of pocs*/
   1150     OPJ_UINT32 l_poc_bound;
   1151 
   1152     /* preconditions in debug*/
   1153     assert(p_cp != 00);
   1154     assert(p_tileno < p_cp->tw * p_cp->th);
   1155 
   1156     /* initializations*/
   1157     l_tcp = &p_cp->tcps [p_tileno];
   1158 
   1159     /* number of iterations in the loop */
   1160     l_poc_bound = l_tcp->numpocs + 1;
   1161 
   1162     /* start at first element, and to make sure the compiler will not make a calculation each time in the loop
   1163        store a pointer to the current element to modify rather than l_tcp->pocs[i]*/
   1164     l_current_poc = l_tcp->pocs;
   1165 
   1166     for (pino = 0; pino < l_poc_bound ; ++pino) {
   1167         l_current_poc->compS = 0;
   1168         l_current_poc->compE = p_num_comps;/*p_image->numcomps;*/
   1169         l_current_poc->resS = 0;
   1170         l_current_poc->resE = p_max_res;
   1171         l_current_poc->layS = 0;
   1172         l_current_poc->layE = l_tcp->numlayers;
   1173         l_current_poc->prg  = l_tcp->prg;
   1174         l_current_poc->prcS = 0;
   1175         l_current_poc->prcE = p_max_prec;
   1176         l_current_poc->txS = (OPJ_UINT32)p_tx0;
   1177         l_current_poc->txE = (OPJ_UINT32)p_tx1;
   1178         l_current_poc->tyS = (OPJ_UINT32)p_ty0;
   1179         l_current_poc->tyE = (OPJ_UINT32)p_ty1;
   1180         l_current_poc->dx = p_dx_min;
   1181         l_current_poc->dy = p_dy_min;
   1182         ++ l_current_poc;
   1183     }
   1184 }
   1185 
   1186 static void opj_pi_update_decode_poc(opj_pi_iterator_t * p_pi,
   1187                                      opj_tcp_t * p_tcp,
   1188                                      OPJ_UINT32 p_max_precision,
   1189                                      OPJ_UINT32 p_max_res)
   1190 {
   1191     /* loop*/
   1192     OPJ_UINT32 pino;
   1193 
   1194     /* encoding prameters to set*/
   1195     OPJ_UINT32 l_bound;
   1196 
   1197     opj_pi_iterator_t * l_current_pi = 00;
   1198     opj_poc_t* l_current_poc = 0;
   1199 
   1200     OPJ_ARG_NOT_USED(p_max_res);
   1201 
   1202     /* preconditions in debug*/
   1203     assert(p_pi != 00);
   1204     assert(p_tcp != 00);
   1205 
   1206     /* initializations*/
   1207     l_bound = p_tcp->numpocs + 1;
   1208     l_current_pi = p_pi;
   1209     l_current_poc = p_tcp->pocs;
   1210 
   1211     for (pino = 0; pino < l_bound; ++pino) {
   1212         l_current_pi->poc.prg = l_current_poc->prg; /* Progression Order #0 */
   1213         l_current_pi->first = 1;
   1214 
   1215         l_current_pi->poc.resno0 =
   1216             l_current_poc->resno0; /* Resolution Level Index #0 (Start) */
   1217         l_current_pi->poc.compno0 =
   1218             l_current_poc->compno0; /* Component Index #0 (Start) */
   1219         l_current_pi->poc.layno0 = 0;
   1220         l_current_pi->poc.precno0 = 0;
   1221         l_current_pi->poc.resno1 =
   1222             l_current_poc->resno1; /* Resolution Level Index #0 (End) */
   1223         l_current_pi->poc.compno1 =
   1224             l_current_poc->compno1; /* Component Index #0 (End) */
   1225         l_current_pi->poc.layno1 = opj_uint_min(l_current_poc->layno1,
   1226                                                 p_tcp->numlayers); /* Layer Index #0 (End) */
   1227         l_current_pi->poc.precno1 = p_max_precision;
   1228         ++l_current_pi;
   1229         ++l_current_poc;
   1230     }
   1231 }
   1232 
   1233 static void opj_pi_update_decode_not_poc(opj_pi_iterator_t * p_pi,
   1234         opj_tcp_t * p_tcp,
   1235         OPJ_UINT32 p_max_precision,
   1236         OPJ_UINT32 p_max_res)
   1237 {
   1238     /* loop*/
   1239     OPJ_UINT32 pino;
   1240 
   1241     /* encoding prameters to set*/
   1242     OPJ_UINT32 l_bound;
   1243 
   1244     opj_pi_iterator_t * l_current_pi = 00;
   1245     /* preconditions in debug*/
   1246     assert(p_tcp != 00);
   1247     assert(p_pi != 00);
   1248 
   1249     /* initializations*/
   1250     l_bound = p_tcp->numpocs + 1;
   1251     l_current_pi = p_pi;
   1252 
   1253     for (pino = 0; pino < l_bound; ++pino) {
   1254         l_current_pi->poc.prg = p_tcp->prg;
   1255         l_current_pi->first = 1;
   1256         l_current_pi->poc.resno0 = 0;
   1257         l_current_pi->poc.compno0 = 0;
   1258         l_current_pi->poc.layno0 = 0;
   1259         l_current_pi->poc.precno0 = 0;
   1260         l_current_pi->poc.resno1 = p_max_res;
   1261         l_current_pi->poc.compno1 = l_current_pi->numcomps;
   1262         l_current_pi->poc.layno1 = p_tcp->numlayers;
   1263         l_current_pi->poc.precno1 = p_max_precision;
   1264         ++l_current_pi;
   1265     }
   1266 }
   1267 
   1268 
   1269 
   1270 static OPJ_BOOL opj_pi_check_next_level(OPJ_INT32 pos,
   1271                                         opj_cp_t *cp,
   1272                                         OPJ_UINT32 tileno,
   1273                                         OPJ_UINT32 pino,
   1274                                         const OPJ_CHAR *prog)
   1275 {
   1276     OPJ_INT32 i;
   1277     opj_tcp_t *tcps = &cp->tcps[tileno];
   1278     opj_poc_t *tcp = &tcps->pocs[pino];
   1279 
   1280     if (pos >= 0) {
   1281         for (i = pos; pos >= 0; i--) {
   1282             switch (prog[i]) {
   1283             case 'R':
   1284                 if (tcp->res_t == tcp->resE) {
   1285                     if (opj_pi_check_next_level(pos - 1, cp, tileno, pino, prog)) {
   1286                         return OPJ_TRUE;
   1287                     } else {
   1288                         return OPJ_FALSE;
   1289                     }
   1290                 } else {
   1291                     return OPJ_TRUE;
   1292                 }
   1293                 break;
   1294             case 'C':
   1295                 if (tcp->comp_t == tcp->compE) {
   1296                     if (opj_pi_check_next_level(pos - 1, cp, tileno, pino, prog)) {
   1297                         return OPJ_TRUE;
   1298                     } else {
   1299                         return OPJ_FALSE;
   1300                     }
   1301                 } else {
   1302                     return OPJ_TRUE;
   1303                 }
   1304                 break;
   1305             case 'L':
   1306                 if (tcp->lay_t == tcp->layE) {
   1307                     if (opj_pi_check_next_level(pos - 1, cp, tileno, pino, prog)) {
   1308                         return OPJ_TRUE;
   1309                     } else {
   1310                         return OPJ_FALSE;
   1311                     }
   1312                 } else {
   1313                     return OPJ_TRUE;
   1314                 }
   1315                 break;
   1316             case 'P':
   1317                 switch (tcp->prg) {
   1318                 case OPJ_LRCP: /* fall through */
   1319                 case OPJ_RLCP:
   1320                     if (tcp->prc_t == tcp->prcE) {
   1321                         if (opj_pi_check_next_level(i - 1, cp, tileno, pino, prog)) {
   1322                             return OPJ_TRUE;
   1323                         } else {
   1324                             return OPJ_FALSE;
   1325                         }
   1326                     } else {
   1327                         return OPJ_TRUE;
   1328                     }
   1329                     break;
   1330                 default:
   1331                     if (tcp->tx0_t == tcp->txE) {
   1332                         /*TY*/
   1333                         if (tcp->ty0_t == tcp->tyE) {
   1334                             if (opj_pi_check_next_level(i - 1, cp, tileno, pino, prog)) {
   1335                                 return OPJ_TRUE;
   1336                             } else {
   1337                                 return OPJ_FALSE;
   1338                             }
   1339                         } else {
   1340                             return OPJ_TRUE;
   1341                         }/*TY*/
   1342                     } else {
   1343                         return OPJ_TRUE;
   1344                     }
   1345                     break;
   1346                 }/*end case P*/
   1347             }/*end switch*/
   1348         }/*end for*/
   1349     }/*end if*/
   1350     return OPJ_FALSE;
   1351 }
   1352 
   1353 
   1354 /*
   1355 ==========================================================
   1356    Packet iterator interface
   1357 ==========================================================
   1358 */
   1359 opj_pi_iterator_t *opj_pi_create_decode(opj_image_t *p_image,
   1360                                         opj_cp_t *p_cp,
   1361                                         OPJ_UINT32 p_tile_no)
   1362 {
   1363     OPJ_UINT32 numcomps = p_image->numcomps;
   1364 
   1365     /* loop */
   1366     OPJ_UINT32 pino;
   1367     OPJ_UINT32 compno, resno;
   1368 
   1369     /* to store w, h, dx and dy fro all components and resolutions */
   1370     OPJ_UINT32 * l_tmp_data;
   1371     OPJ_UINT32 ** l_tmp_ptr;
   1372 
   1373     /* encoding prameters to set */
   1374     OPJ_UINT32 l_max_res;
   1375     OPJ_UINT32 l_max_prec;
   1376     OPJ_INT32 l_tx0, l_tx1, l_ty0, l_ty1;
   1377     OPJ_UINT32 l_dx_min, l_dy_min;
   1378     OPJ_UINT32 l_bound;
   1379     OPJ_UINT32 l_step_p, l_step_c, l_step_r, l_step_l ;
   1380     OPJ_UINT32 l_data_stride;
   1381 
   1382     /* pointers */
   1383     opj_pi_iterator_t *l_pi = 00;
   1384     opj_tcp_t *l_tcp = 00;
   1385     const opj_tccp_t *l_tccp = 00;
   1386     opj_pi_comp_t *l_current_comp = 00;
   1387     opj_image_comp_t * l_img_comp = 00;
   1388     opj_pi_iterator_t * l_current_pi = 00;
   1389     OPJ_UINT32 * l_encoding_value_ptr = 00;
   1390 
   1391     /* preconditions in debug */
   1392     assert(p_cp != 00);
   1393     assert(p_image != 00);
   1394     assert(p_tile_no < p_cp->tw * p_cp->th);
   1395 
   1396     /* initializations */
   1397     l_tcp = &p_cp->tcps[p_tile_no];
   1398     l_bound = l_tcp->numpocs + 1;
   1399 
   1400     l_data_stride = 4 * OPJ_J2K_MAXRLVLS;
   1401     l_tmp_data = (OPJ_UINT32*)opj_malloc(
   1402                      l_data_stride * numcomps * sizeof(OPJ_UINT32));
   1403     if
   1404     (! l_tmp_data) {
   1405         return 00;
   1406     }
   1407     l_tmp_ptr = (OPJ_UINT32**)opj_malloc(
   1408                     numcomps * sizeof(OPJ_UINT32 *));
   1409     if
   1410     (! l_tmp_ptr) {
   1411         opj_free(l_tmp_data);
   1412         return 00;
   1413     }
   1414 
   1415     /* memory allocation for pi */
   1416     l_pi = opj_pi_create(p_image, p_cp, p_tile_no);
   1417     if (!l_pi) {
   1418         opj_free(l_tmp_data);
   1419         opj_free(l_tmp_ptr);
   1420         return 00;
   1421     }
   1422 
   1423     l_encoding_value_ptr = l_tmp_data;
   1424     /* update pointer array */
   1425     for
   1426     (compno = 0; compno < numcomps; ++compno) {
   1427         l_tmp_ptr[compno] = l_encoding_value_ptr;
   1428         l_encoding_value_ptr += l_data_stride;
   1429     }
   1430     /* get encoding parameters */
   1431     opj_get_all_encoding_parameters(p_image, p_cp, p_tile_no, &l_tx0, &l_tx1,
   1432                                     &l_ty0, &l_ty1, &l_dx_min, &l_dy_min, &l_max_prec, &l_max_res, l_tmp_ptr);
   1433 
   1434     /* step calculations */
   1435     l_step_p = 1;
   1436     l_step_c = l_max_prec * l_step_p;
   1437     l_step_r = numcomps * l_step_c;
   1438     l_step_l = l_max_res * l_step_r;
   1439 
   1440     /* set values for first packet iterator */
   1441     l_current_pi = l_pi;
   1442 
   1443     /* memory allocation for include */
   1444     /* prevent an integer overflow issue */
   1445     /* 0 < l_tcp->numlayers < 65536 c.f. opj_j2k_read_cod in j2k.c */
   1446     l_current_pi->include = 00;
   1447     if (l_step_l <= (UINT_MAX / (l_tcp->numlayers + 1U))) {
   1448         l_current_pi->include_size = (l_tcp->numlayers + 1U) * l_step_l;
   1449         l_current_pi->include = (OPJ_INT16*) opj_calloc(
   1450                                     l_current_pi->include_size, sizeof(OPJ_INT16));
   1451     }
   1452 
   1453     if (!l_current_pi->include) {
   1454         opj_free(l_tmp_data);
   1455         opj_free(l_tmp_ptr);
   1456         opj_pi_destroy(l_pi, l_bound);
   1457         return 00;
   1458     }
   1459 
   1460     /* special treatment for the first packet iterator */
   1461     l_current_comp = l_current_pi->comps;
   1462     l_img_comp = p_image->comps;
   1463     l_tccp = l_tcp->tccps;
   1464 
   1465     l_current_pi->tx0 = l_tx0;
   1466     l_current_pi->ty0 = l_ty0;
   1467     l_current_pi->tx1 = l_tx1;
   1468     l_current_pi->ty1 = l_ty1;
   1469 
   1470     /*l_current_pi->dx = l_img_comp->dx;*/
   1471     /*l_current_pi->dy = l_img_comp->dy;*/
   1472 
   1473     l_current_pi->step_p = l_step_p;
   1474     l_current_pi->step_c = l_step_c;
   1475     l_current_pi->step_r = l_step_r;
   1476     l_current_pi->step_l = l_step_l;
   1477 
   1478     /* allocation for components and number of components has already been calculated by opj_pi_create */
   1479     for
   1480     (compno = 0; compno < numcomps; ++compno) {
   1481         opj_pi_resolution_t *l_res = l_current_comp->resolutions;
   1482         l_encoding_value_ptr = l_tmp_ptr[compno];
   1483 
   1484         l_current_comp->dx = l_img_comp->dx;
   1485         l_current_comp->dy = l_img_comp->dy;
   1486         /* resolutions have already been initialized */
   1487         for
   1488         (resno = 0; resno < l_current_comp->numresolutions; resno++) {
   1489             l_res->pdx = *(l_encoding_value_ptr++);
   1490             l_res->pdy = *(l_encoding_value_ptr++);
   1491             l_res->pw =  *(l_encoding_value_ptr++);
   1492             l_res->ph =  *(l_encoding_value_ptr++);
   1493             ++l_res;
   1494         }
   1495         ++l_current_comp;
   1496         ++l_img_comp;
   1497         ++l_tccp;
   1498     }
   1499     ++l_current_pi;
   1500 
   1501     for (pino = 1 ; pino < l_bound ; ++pino) {
   1502         l_current_comp = l_current_pi->comps;
   1503         l_img_comp = p_image->comps;
   1504         l_tccp = l_tcp->tccps;
   1505 
   1506         l_current_pi->tx0 = l_tx0;
   1507         l_current_pi->ty0 = l_ty0;
   1508         l_current_pi->tx1 = l_tx1;
   1509         l_current_pi->ty1 = l_ty1;
   1510         /*l_current_pi->dx = l_dx_min;*/
   1511         /*l_current_pi->dy = l_dy_min;*/
   1512         l_current_pi->step_p = l_step_p;
   1513         l_current_pi->step_c = l_step_c;
   1514         l_current_pi->step_r = l_step_r;
   1515         l_current_pi->step_l = l_step_l;
   1516 
   1517         /* allocation for components and number of components has already been calculated by opj_pi_create */
   1518         for
   1519         (compno = 0; compno < numcomps; ++compno) {
   1520             opj_pi_resolution_t *l_res = l_current_comp->resolutions;
   1521             l_encoding_value_ptr = l_tmp_ptr[compno];
   1522 
   1523             l_current_comp->dx = l_img_comp->dx;
   1524             l_current_comp->dy = l_img_comp->dy;
   1525             /* resolutions have already been initialized */
   1526             for
   1527             (resno = 0; resno < l_current_comp->numresolutions; resno++) {
   1528                 l_res->pdx = *(l_encoding_value_ptr++);
   1529                 l_res->pdy = *(l_encoding_value_ptr++);
   1530                 l_res->pw =  *(l_encoding_value_ptr++);
   1531                 l_res->ph =  *(l_encoding_value_ptr++);
   1532                 ++l_res;
   1533             }
   1534             ++l_current_comp;
   1535             ++l_img_comp;
   1536             ++l_tccp;
   1537         }
   1538         /* special treatment*/
   1539         l_current_pi->include = (l_current_pi - 1)->include;
   1540         l_current_pi->include_size = (l_current_pi - 1)->include_size;
   1541         ++l_current_pi;
   1542     }
   1543     opj_free(l_tmp_data);
   1544     l_tmp_data = 00;
   1545     opj_free(l_tmp_ptr);
   1546     l_tmp_ptr = 00;
   1547     if
   1548     (l_tcp->POC) {
   1549         opj_pi_update_decode_poc(l_pi, l_tcp, l_max_prec, l_max_res);
   1550     } else {
   1551         opj_pi_update_decode_not_poc(l_pi, l_tcp, l_max_prec, l_max_res);
   1552     }
   1553     return l_pi;
   1554 }
   1555 
   1556 
   1557 
   1558 opj_pi_iterator_t *opj_pi_initialise_encode(const opj_image_t *p_image,
   1559         opj_cp_t *p_cp,
   1560         OPJ_UINT32 p_tile_no,
   1561         J2K_T2_MODE p_t2_mode)
   1562 {
   1563     OPJ_UINT32 numcomps = p_image->numcomps;
   1564 
   1565     /* loop*/
   1566     OPJ_UINT32 pino;
   1567     OPJ_UINT32 compno, resno;
   1568 
   1569     /* to store w, h, dx and dy fro all components and resolutions*/
   1570     OPJ_UINT32 * l_tmp_data;
   1571     OPJ_UINT32 ** l_tmp_ptr;
   1572 
   1573     /* encoding prameters to set*/
   1574     OPJ_UINT32 l_max_res;
   1575     OPJ_UINT32 l_max_prec;
   1576     OPJ_INT32 l_tx0, l_tx1, l_ty0, l_ty1;
   1577     OPJ_UINT32 l_dx_min, l_dy_min;
   1578     OPJ_UINT32 l_bound;
   1579     OPJ_UINT32 l_step_p, l_step_c, l_step_r, l_step_l ;
   1580     OPJ_UINT32 l_data_stride;
   1581 
   1582     /* pointers*/
   1583     opj_pi_iterator_t *l_pi = 00;
   1584     opj_tcp_t *l_tcp = 00;
   1585     const opj_tccp_t *l_tccp = 00;
   1586     opj_pi_comp_t *l_current_comp = 00;
   1587     opj_image_comp_t * l_img_comp = 00;
   1588     opj_pi_iterator_t * l_current_pi = 00;
   1589     OPJ_UINT32 * l_encoding_value_ptr = 00;
   1590 
   1591     /* preconditions in debug*/
   1592     assert(p_cp != 00);
   1593     assert(p_image != 00);
   1594     assert(p_tile_no < p_cp->tw * p_cp->th);
   1595 
   1596     /* initializations*/
   1597     l_tcp = &p_cp->tcps[p_tile_no];
   1598     l_bound = l_tcp->numpocs + 1;
   1599 
   1600     l_data_stride = 4 * OPJ_J2K_MAXRLVLS;
   1601     l_tmp_data = (OPJ_UINT32*)opj_malloc(
   1602                      l_data_stride * numcomps * sizeof(OPJ_UINT32));
   1603     if (! l_tmp_data) {
   1604         return 00;
   1605     }
   1606 
   1607     l_tmp_ptr = (OPJ_UINT32**)opj_malloc(
   1608                     numcomps * sizeof(OPJ_UINT32 *));
   1609     if (! l_tmp_ptr) {
   1610         opj_free(l_tmp_data);
   1611         return 00;
   1612     }
   1613 
   1614     /* memory allocation for pi*/
   1615     l_pi = opj_pi_create(p_image, p_cp, p_tile_no);
   1616     if (!l_pi) {
   1617         opj_free(l_tmp_data);
   1618         opj_free(l_tmp_ptr);
   1619         return 00;
   1620     }
   1621 
   1622     l_encoding_value_ptr = l_tmp_data;
   1623     /* update pointer array*/
   1624     for (compno = 0; compno < numcomps; ++compno) {
   1625         l_tmp_ptr[compno] = l_encoding_value_ptr;
   1626         l_encoding_value_ptr += l_data_stride;
   1627     }
   1628 
   1629     /* get encoding parameters*/
   1630     opj_get_all_encoding_parameters(p_image, p_cp, p_tile_no, &l_tx0, &l_tx1,
   1631                                     &l_ty0, &l_ty1, &l_dx_min, &l_dy_min, &l_max_prec, &l_max_res, l_tmp_ptr);
   1632 
   1633     /* step calculations*/
   1634     l_step_p = 1;
   1635     l_step_c = l_max_prec * l_step_p;
   1636     l_step_r = numcomps * l_step_c;
   1637     l_step_l = l_max_res * l_step_r;
   1638 
   1639     /* set values for first packet iterator*/
   1640     l_pi->tp_on = (OPJ_BYTE)p_cp->m_specific_param.m_enc.m_tp_on;
   1641     l_current_pi = l_pi;
   1642 
   1643     /* memory allocation for include*/
   1644     l_current_pi->include_size = l_tcp->numlayers * l_step_l;
   1645     l_current_pi->include = (OPJ_INT16*) opj_calloc(l_current_pi->include_size,
   1646                             sizeof(OPJ_INT16));
   1647     if (!l_current_pi->include) {
   1648         opj_free(l_tmp_data);
   1649         opj_free(l_tmp_ptr);
   1650         opj_pi_destroy(l_pi, l_bound);
   1651         return 00;
   1652     }
   1653 
   1654     /* special treatment for the first packet iterator*/
   1655     l_current_comp = l_current_pi->comps;
   1656     l_img_comp = p_image->comps;
   1657     l_tccp = l_tcp->tccps;
   1658     l_current_pi->tx0 = l_tx0;
   1659     l_current_pi->ty0 = l_ty0;
   1660     l_current_pi->tx1 = l_tx1;
   1661     l_current_pi->ty1 = l_ty1;
   1662     l_current_pi->dx = l_dx_min;
   1663     l_current_pi->dy = l_dy_min;
   1664     l_current_pi->step_p = l_step_p;
   1665     l_current_pi->step_c = l_step_c;
   1666     l_current_pi->step_r = l_step_r;
   1667     l_current_pi->step_l = l_step_l;
   1668 
   1669     /* allocation for components and number of components has already been calculated by opj_pi_create */
   1670     for (compno = 0; compno < numcomps; ++compno) {
   1671         opj_pi_resolution_t *l_res = l_current_comp->resolutions;
   1672         l_encoding_value_ptr = l_tmp_ptr[compno];
   1673 
   1674         l_current_comp->dx = l_img_comp->dx;
   1675         l_current_comp->dy = l_img_comp->dy;
   1676 
   1677         /* resolutions have already been initialized */
   1678         for (resno = 0; resno < l_current_comp->numresolutions; resno++) {
   1679             l_res->pdx = *(l_encoding_value_ptr++);
   1680             l_res->pdy = *(l_encoding_value_ptr++);
   1681             l_res->pw =  *(l_encoding_value_ptr++);
   1682             l_res->ph =  *(l_encoding_value_ptr++);
   1683             ++l_res;
   1684         }
   1685 
   1686         ++l_current_comp;
   1687         ++l_img_comp;
   1688         ++l_tccp;
   1689     }
   1690     ++l_current_pi;
   1691 
   1692     for (pino = 1 ; pino < l_bound ; ++pino) {
   1693         l_current_comp = l_current_pi->comps;
   1694         l_img_comp = p_image->comps;
   1695         l_tccp = l_tcp->tccps;
   1696 
   1697         l_current_pi->tx0 = l_tx0;
   1698         l_current_pi->ty0 = l_ty0;
   1699         l_current_pi->tx1 = l_tx1;
   1700         l_current_pi->ty1 = l_ty1;
   1701         l_current_pi->dx = l_dx_min;
   1702         l_current_pi->dy = l_dy_min;
   1703         l_current_pi->step_p = l_step_p;
   1704         l_current_pi->step_c = l_step_c;
   1705         l_current_pi->step_r = l_step_r;
   1706         l_current_pi->step_l = l_step_l;
   1707 
   1708         /* allocation for components and number of components has already been calculated by opj_pi_create */
   1709         for (compno = 0; compno < numcomps; ++compno) {
   1710             opj_pi_resolution_t *l_res = l_current_comp->resolutions;
   1711             l_encoding_value_ptr = l_tmp_ptr[compno];
   1712 
   1713             l_current_comp->dx = l_img_comp->dx;
   1714             l_current_comp->dy = l_img_comp->dy;
   1715             /* resolutions have already been initialized */
   1716             for (resno = 0; resno < l_current_comp->numresolutions; resno++) {
   1717                 l_res->pdx = *(l_encoding_value_ptr++);
   1718                 l_res->pdy = *(l_encoding_value_ptr++);
   1719                 l_res->pw =  *(l_encoding_value_ptr++);
   1720                 l_res->ph =  *(l_encoding_value_ptr++);
   1721                 ++l_res;
   1722             }
   1723             ++l_current_comp;
   1724             ++l_img_comp;
   1725             ++l_tccp;
   1726         }
   1727 
   1728         /* special treatment*/
   1729         l_current_pi->include = (l_current_pi - 1)->include;
   1730         l_current_pi->include_size = (l_current_pi - 1)->include_size;
   1731         ++l_current_pi;
   1732     }
   1733 
   1734     opj_free(l_tmp_data);
   1735     l_tmp_data = 00;
   1736     opj_free(l_tmp_ptr);
   1737     l_tmp_ptr = 00;
   1738 
   1739     if (l_tcp->POC && (OPJ_IS_CINEMA(p_cp->rsiz) || p_t2_mode == FINAL_PASS)) {
   1740         opj_pi_update_encode_poc_and_final(p_cp, p_tile_no, l_tx0, l_tx1, l_ty0, l_ty1,
   1741                                            l_max_prec, l_max_res, l_dx_min, l_dy_min);
   1742     } else {
   1743         opj_pi_update_encode_not_poc(p_cp, numcomps, p_tile_no, l_tx0, l_tx1,
   1744                                      l_ty0, l_ty1, l_max_prec, l_max_res, l_dx_min, l_dy_min);
   1745     }
   1746 
   1747     return l_pi;
   1748 }
   1749 
   1750 void opj_pi_create_encode(opj_pi_iterator_t *pi,
   1751                           opj_cp_t *cp,
   1752                           OPJ_UINT32 tileno,
   1753                           OPJ_UINT32 pino,
   1754                           OPJ_UINT32 tpnum,
   1755                           OPJ_INT32 tppos,
   1756                           J2K_T2_MODE t2_mode)
   1757 {
   1758     const OPJ_CHAR *prog;
   1759     OPJ_INT32 i;
   1760     OPJ_UINT32 incr_top = 1, resetX = 0;
   1761     opj_tcp_t *tcps = &cp->tcps[tileno];
   1762     opj_poc_t *tcp = &tcps->pocs[pino];
   1763 
   1764     prog = opj_j2k_convert_progression_order(tcp->prg);
   1765 
   1766     pi[pino].first = 1;
   1767     pi[pino].poc.prg = tcp->prg;
   1768 
   1769     if (!(cp->m_specific_param.m_enc.m_tp_on && ((!OPJ_IS_CINEMA(cp->rsiz) &&
   1770             (t2_mode == FINAL_PASS)) || OPJ_IS_CINEMA(cp->rsiz)))) {
   1771         pi[pino].poc.resno0 = tcp->resS;
   1772         pi[pino].poc.resno1 = tcp->resE;
   1773         pi[pino].poc.compno0 = tcp->compS;
   1774         pi[pino].poc.compno1 = tcp->compE;
   1775         pi[pino].poc.layno0 = tcp->layS;
   1776         pi[pino].poc.layno1 = tcp->layE;
   1777         pi[pino].poc.precno0 = tcp->prcS;
   1778         pi[pino].poc.precno1 = tcp->prcE;
   1779         pi[pino].poc.tx0 = (OPJ_INT32)tcp->txS;
   1780         pi[pino].poc.ty0 = (OPJ_INT32)tcp->tyS;
   1781         pi[pino].poc.tx1 = (OPJ_INT32)tcp->txE;
   1782         pi[pino].poc.ty1 = (OPJ_INT32)tcp->tyE;
   1783     } else {
   1784         for (i = tppos + 1; i < 4; i++) {
   1785             switch (prog[i]) {
   1786             case 'R':
   1787                 pi[pino].poc.resno0 = tcp->resS;
   1788                 pi[pino].poc.resno1 = tcp->resE;
   1789                 break;
   1790             case 'C':
   1791                 pi[pino].poc.compno0 = tcp->compS;
   1792                 pi[pino].poc.compno1 = tcp->compE;
   1793                 break;
   1794             case 'L':
   1795                 pi[pino].poc.layno0 = tcp->layS;
   1796                 pi[pino].poc.layno1 = tcp->layE;
   1797                 break;
   1798             case 'P':
   1799                 switch (tcp->prg) {
   1800                 case OPJ_LRCP:
   1801                 case OPJ_RLCP:
   1802                     pi[pino].poc.precno0 = tcp->prcS;
   1803                     pi[pino].poc.precno1 = tcp->prcE;
   1804                     break;
   1805                 default:
   1806                     pi[pino].poc.tx0 = (OPJ_INT32)tcp->txS;
   1807                     pi[pino].poc.ty0 = (OPJ_INT32)tcp->tyS;
   1808                     pi[pino].poc.tx1 = (OPJ_INT32)tcp->txE;
   1809                     pi[pino].poc.ty1 = (OPJ_INT32)tcp->tyE;
   1810                     break;
   1811                 }
   1812                 break;
   1813             }
   1814         }
   1815 
   1816         if (tpnum == 0) {
   1817             for (i = tppos; i >= 0; i--) {
   1818                 switch (prog[i]) {
   1819                 case 'C':
   1820                     tcp->comp_t = tcp->compS;
   1821                     pi[pino].poc.compno0 = tcp->comp_t;
   1822                     pi[pino].poc.compno1 = tcp->comp_t + 1;
   1823                     tcp->comp_t += 1;
   1824                     break;
   1825                 case 'R':
   1826                     tcp->res_t = tcp->resS;
   1827                     pi[pino].poc.resno0 = tcp->res_t;
   1828                     pi[pino].poc.resno1 = tcp->res_t + 1;
   1829                     tcp->res_t += 1;
   1830                     break;
   1831                 case 'L':
   1832                     tcp->lay_t = tcp->layS;
   1833                     pi[pino].poc.layno0 = tcp->lay_t;
   1834                     pi[pino].poc.layno1 = tcp->lay_t + 1;
   1835                     tcp->lay_t += 1;
   1836                     break;
   1837                 case 'P':
   1838                     switch (tcp->prg) {
   1839                     case OPJ_LRCP:
   1840                     case OPJ_RLCP:
   1841                         tcp->prc_t = tcp->prcS;
   1842                         pi[pino].poc.precno0 = tcp->prc_t;
   1843                         pi[pino].poc.precno1 = tcp->prc_t + 1;
   1844                         tcp->prc_t += 1;
   1845                         break;
   1846                     default:
   1847                         tcp->tx0_t = tcp->txS;
   1848                         tcp->ty0_t = tcp->tyS;
   1849                         pi[pino].poc.tx0 = (OPJ_INT32)tcp->tx0_t;
   1850                         pi[pino].poc.tx1 = (OPJ_INT32)(tcp->tx0_t + tcp->dx - (tcp->tx0_t % tcp->dx));
   1851                         pi[pino].poc.ty0 = (OPJ_INT32)tcp->ty0_t;
   1852                         pi[pino].poc.ty1 = (OPJ_INT32)(tcp->ty0_t + tcp->dy - (tcp->ty0_t % tcp->dy));
   1853                         tcp->tx0_t = (OPJ_UINT32)pi[pino].poc.tx1;
   1854                         tcp->ty0_t = (OPJ_UINT32)pi[pino].poc.ty1;
   1855                         break;
   1856                     }
   1857                     break;
   1858                 }
   1859             }
   1860             incr_top = 1;
   1861         } else {
   1862             for (i = tppos; i >= 0; i--) {
   1863                 switch (prog[i]) {
   1864                 case 'C':
   1865                     pi[pino].poc.compno0 = tcp->comp_t - 1;
   1866                     pi[pino].poc.compno1 = tcp->comp_t;
   1867                     break;
   1868                 case 'R':
   1869                     pi[pino].poc.resno0 = tcp->res_t - 1;
   1870                     pi[pino].poc.resno1 = tcp->res_t;
   1871                     break;
   1872                 case 'L':
   1873                     pi[pino].poc.layno0 = tcp->lay_t - 1;
   1874                     pi[pino].poc.layno1 = tcp->lay_t;
   1875                     break;
   1876                 case 'P':
   1877                     switch (tcp->prg) {
   1878                     case OPJ_LRCP:
   1879                     case OPJ_RLCP:
   1880                         pi[pino].poc.precno0 = tcp->prc_t - 1;
   1881                         pi[pino].poc.precno1 = tcp->prc_t;
   1882                         break;
   1883                     default:
   1884                         pi[pino].poc.tx0 = (OPJ_INT32)(tcp->tx0_t - tcp->dx - (tcp->tx0_t % tcp->dx));
   1885                         pi[pino].poc.tx1 = (OPJ_INT32)tcp->tx0_t ;
   1886                         pi[pino].poc.ty0 = (OPJ_INT32)(tcp->ty0_t - tcp->dy - (tcp->ty0_t % tcp->dy));
   1887                         pi[pino].poc.ty1 = (OPJ_INT32)tcp->ty0_t ;
   1888                         break;
   1889                     }
   1890                     break;
   1891                 }
   1892                 if (incr_top == 1) {
   1893                     switch (prog[i]) {
   1894                     case 'R':
   1895                         if (tcp->res_t == tcp->resE) {
   1896                             if (opj_pi_check_next_level(i - 1, cp, tileno, pino, prog)) {
   1897                                 tcp->res_t = tcp->resS;
   1898                                 pi[pino].poc.resno0 = tcp->res_t;
   1899                                 pi[pino].poc.resno1 = tcp->res_t + 1;
   1900                                 tcp->res_t += 1;
   1901                                 incr_top = 1;
   1902                             } else {
   1903                                 incr_top = 0;
   1904                             }
   1905                         } else {
   1906                             pi[pino].poc.resno0 = tcp->res_t;
   1907                             pi[pino].poc.resno1 = tcp->res_t + 1;
   1908                             tcp->res_t += 1;
   1909                             incr_top = 0;
   1910                         }
   1911                         break;
   1912                     case 'C':
   1913                         if (tcp->comp_t == tcp->compE) {
   1914                             if (opj_pi_check_next_level(i - 1, cp, tileno, pino, prog)) {
   1915                                 tcp->comp_t = tcp->compS;
   1916                                 pi[pino].poc.compno0 = tcp->comp_t;
   1917                                 pi[pino].poc.compno1 = tcp->comp_t + 1;
   1918                                 tcp->comp_t += 1;
   1919                                 incr_top = 1;
   1920                             } else {
   1921                                 incr_top = 0;
   1922                             }
   1923                         } else {
   1924                             pi[pino].poc.compno0 = tcp->comp_t;
   1925                             pi[pino].poc.compno1 = tcp->comp_t + 1;
   1926                             tcp->comp_t += 1;
   1927                             incr_top = 0;
   1928                         }
   1929                         break;
   1930                     case 'L':
   1931                         if (tcp->lay_t == tcp->layE) {
   1932                             if (opj_pi_check_next_level(i - 1, cp, tileno, pino, prog)) {
   1933                                 tcp->lay_t = tcp->layS;
   1934                                 pi[pino].poc.layno0 = tcp->lay_t;
   1935                                 pi[pino].poc.layno1 = tcp->lay_t + 1;
   1936                                 tcp->lay_t += 1;
   1937                                 incr_top = 1;
   1938                             } else {
   1939                                 incr_top = 0;
   1940                             }
   1941                         } else {
   1942                             pi[pino].poc.layno0 = tcp->lay_t;
   1943                             pi[pino].poc.layno1 = tcp->lay_t + 1;
   1944                             tcp->lay_t += 1;
   1945                             incr_top = 0;
   1946                         }
   1947                         break;
   1948                     case 'P':
   1949                         switch (tcp->prg) {
   1950                         case OPJ_LRCP:
   1951                         case OPJ_RLCP:
   1952                             if (tcp->prc_t == tcp->prcE) {
   1953                                 if (opj_pi_check_next_level(i - 1, cp, tileno, pino, prog)) {
   1954                                     tcp->prc_t = tcp->prcS;
   1955                                     pi[pino].poc.precno0 = tcp->prc_t;
   1956                                     pi[pino].poc.precno1 = tcp->prc_t + 1;
   1957                                     tcp->prc_t += 1;
   1958                                     incr_top = 1;
   1959                                 } else {
   1960                                     incr_top = 0;
   1961                                 }
   1962                             } else {
   1963                                 pi[pino].poc.precno0 = tcp->prc_t;
   1964                                 pi[pino].poc.precno1 = tcp->prc_t + 1;
   1965                                 tcp->prc_t += 1;
   1966                                 incr_top = 0;
   1967                             }
   1968                             break;
   1969                         default:
   1970                             if (tcp->tx0_t >= tcp->txE) {
   1971                                 if (tcp->ty0_t >= tcp->tyE) {
   1972                                     if (opj_pi_check_next_level(i - 1, cp, tileno, pino, prog)) {
   1973                                         tcp->ty0_t = tcp->tyS;
   1974                                         pi[pino].poc.ty0 = (OPJ_INT32)tcp->ty0_t;
   1975                                         pi[pino].poc.ty1 = (OPJ_INT32)(tcp->ty0_t + tcp->dy - (tcp->ty0_t % tcp->dy));
   1976                                         tcp->ty0_t = (OPJ_UINT32)pi[pino].poc.ty1;
   1977                                         incr_top = 1;
   1978                                         resetX = 1;
   1979                                     } else {
   1980                                         incr_top = 0;
   1981                                         resetX = 0;
   1982                                     }
   1983                                 } else {
   1984                                     pi[pino].poc.ty0 = (OPJ_INT32)tcp->ty0_t;
   1985                                     pi[pino].poc.ty1 = (OPJ_INT32)(tcp->ty0_t + tcp->dy - (tcp->ty0_t % tcp->dy));
   1986                                     tcp->ty0_t = (OPJ_UINT32)pi[pino].poc.ty1;
   1987                                     incr_top = 0;
   1988                                     resetX = 1;
   1989                                 }
   1990                                 if (resetX == 1) {
   1991                                     tcp->tx0_t = tcp->txS;
   1992                                     pi[pino].poc.tx0 = (OPJ_INT32)tcp->tx0_t;
   1993                                     pi[pino].poc.tx1 = (OPJ_INT32)(tcp->tx0_t + tcp->dx - (tcp->tx0_t % tcp->dx));
   1994                                     tcp->tx0_t = (OPJ_UINT32)pi[pino].poc.tx1;
   1995                                 }
   1996                             } else {
   1997                                 pi[pino].poc.tx0 = (OPJ_INT32)tcp->tx0_t;
   1998                                 pi[pino].poc.tx1 = (OPJ_INT32)(tcp->tx0_t + tcp->dx - (tcp->tx0_t % tcp->dx));
   1999                                 tcp->tx0_t = (OPJ_UINT32)pi[pino].poc.tx1;
   2000                                 incr_top = 0;
   2001                             }
   2002                             break;
   2003                         }
   2004                         break;
   2005                     }
   2006                 }
   2007             }
   2008         }
   2009     }
   2010 }
   2011 
   2012 void opj_pi_destroy(opj_pi_iterator_t *p_pi,
   2013                     OPJ_UINT32 p_nb_elements)
   2014 {
   2015     OPJ_UINT32 compno, pino;
   2016     opj_pi_iterator_t *l_current_pi = p_pi;
   2017     if (p_pi) {
   2018         if (p_pi->include) {
   2019             opj_free(p_pi->include);
   2020             p_pi->include = 00;
   2021         }
   2022         for (pino = 0; pino < p_nb_elements; ++pino) {
   2023             if (l_current_pi->comps) {
   2024                 opj_pi_comp_t *l_current_component = l_current_pi->comps;
   2025                 for (compno = 0; compno < l_current_pi->numcomps; compno++) {
   2026                     if (l_current_component->resolutions) {
   2027                         opj_free(l_current_component->resolutions);
   2028                         l_current_component->resolutions = 00;
   2029                     }
   2030 
   2031                     ++l_current_component;
   2032                 }
   2033                 opj_free(l_current_pi->comps);
   2034                 l_current_pi->comps = 0;
   2035             }
   2036             ++l_current_pi;
   2037         }
   2038         opj_free(p_pi);
   2039     }
   2040 }
   2041 
   2042 
   2043 
   2044 void opj_pi_update_encoding_parameters(const opj_image_t *p_image,
   2045                                        opj_cp_t *p_cp,
   2046                                        OPJ_UINT32 p_tile_no)
   2047 {
   2048     /* encoding parameters to set */
   2049     OPJ_UINT32 l_max_res;
   2050     OPJ_UINT32 l_max_prec;
   2051     OPJ_INT32 l_tx0, l_tx1, l_ty0, l_ty1;
   2052     OPJ_UINT32 l_dx_min, l_dy_min;
   2053 
   2054     /* pointers */
   2055     opj_tcp_t *l_tcp = 00;
   2056 
   2057     /* preconditions */
   2058     assert(p_cp != 00);
   2059     assert(p_image != 00);
   2060     assert(p_tile_no < p_cp->tw * p_cp->th);
   2061 
   2062     l_tcp = &(p_cp->tcps[p_tile_no]);
   2063 
   2064     /* get encoding parameters */
   2065     opj_get_encoding_parameters(p_image, p_cp, p_tile_no, &l_tx0, &l_tx1, &l_ty0,
   2066                                 &l_ty1, &l_dx_min, &l_dy_min, &l_max_prec, &l_max_res);
   2067 
   2068     if (l_tcp->POC) {
   2069         opj_pi_update_encode_poc_and_final(p_cp, p_tile_no, l_tx0, l_tx1, l_ty0, l_ty1,
   2070                                            l_max_prec, l_max_res, l_dx_min, l_dy_min);
   2071     } else {
   2072         opj_pi_update_encode_not_poc(p_cp, p_image->numcomps, p_tile_no, l_tx0, l_tx1,
   2073                                      l_ty0, l_ty1, l_max_prec, l_max_res, l_dx_min, l_dy_min);
   2074     }
   2075 }
   2076 
   2077 OPJ_BOOL opj_pi_next(opj_pi_iterator_t * pi)
   2078 {
   2079     switch (pi->poc.prg) {
   2080     case OPJ_LRCP:
   2081         return opj_pi_next_lrcp(pi);
   2082     case OPJ_RLCP:
   2083         return opj_pi_next_rlcp(pi);
   2084     case OPJ_RPCL:
   2085         return opj_pi_next_rpcl(pi);
   2086     case OPJ_PCRL:
   2087         return opj_pi_next_pcrl(pi);
   2088     case OPJ_CPRL:
   2089         return opj_pi_next_cprl(pi);
   2090     case OPJ_PROG_UNKNOWN:
   2091         return OPJ_FALSE;
   2092     }
   2093 
   2094     return OPJ_FALSE;
   2095 }
   2096