diff --git a/src/bin/common/color.c b/src/bin/common/color.c index d3a2f38d..27f15f13 100644 --- a/src/bin/common/color.c +++ b/src/bin/common/color.c @@ -488,6 +488,10 @@ void color_apply_icc_profile(opj_image_t *image) if (out_space == cmsSigRgbData) { /* enumCS 16 */ unsigned int i, nr_comp = image->numcomps; + if (nr_comp < 3) { /* GRAY or GRAYA, not RGB or RGBA */ + cmsCloseProfile(in_prof); + return; + } if (nr_comp > 4) { nr_comp = 4; } @@ -529,6 +533,10 @@ void color_apply_icc_profile(opj_image_t *image) out_prof = cmsCreate_sRGBProfile(); new_space = OPJ_CLRSPC_SRGB; } else if (out_space == cmsSigYCbCrData) { /* enumCS 18 */ + if (image->numcomps < 3) { + cmsCloseProfile(in_prof); + return; + } in_type = TYPE_YCbCr_16; out_type = TYPE_RGB_16; out_prof = cmsCreate_sRGBProfile(); diff --git a/src/bin/jp3d/convert.c b/src/bin/jp3d/convert.c index acad8f82..4e4bea9c 100644 --- a/src/bin/jp3d/convert.c +++ b/src/bin/jp3d/convert.c @@ -280,6 +280,7 @@ opj_volume_t* pgxtovolume(char *relpath, opj_cparameters_t *parameters) if (!sliceno) { fprintf(stdout, "[ERROR] No slices with this pattern founded !! Please check input volume name\n"); + closedir(dirp); return NULL; } /*if ( maxslice != sliceno) { @@ -317,6 +318,7 @@ opj_volume_t* pgxtovolume(char *relpath, opj_cparameters_t *parameters) } else { fprintf(stdout, "[ERROR] Bad pgx header, please check input file\n"); fclose(f); + closedir(dirp); return NULL; } @@ -396,9 +398,7 @@ opj_volume_t* pgxtovolume(char *relpath, opj_cparameters_t *parameters) fclose(f); } /* for s --> sliceno*/ comp->bpp = int_floorlog2(maxvalue) + 1; - if (sliceno != 1) { - closedir(dirp); - } + closedir(dirp); /*dump_volume(stdout, volume);*/ return volume; } @@ -581,6 +581,7 @@ opj_volume_t* bintovolume(char *filename, char *fileimg, f = fopen(filename, "rb"); if (!f) { fprintf(stdout, "[ERROR] Failed to open %s for reading !!\n", filename); + opj_free(volume); return 0; } @@ -918,7 +919,7 @@ opj_volume_t* imgtovolume(char *fileimg, opj_cparameters_t *parameters) f = fopen(filename, "rb"); if (!f) { fprintf(stderr, "[ERROR] Failed to open %s for reading !!\n", filename); - fclose(f); + opj_free(volume); return 0; } diff --git a/src/bin/jpip/opj_jpip_transcode.c b/src/bin/jpip/opj_jpip_transcode.c index 6cd2604d..bedd5cf9 100644 --- a/src/bin/jpip/opj_jpip_transcode.c +++ b/src/bin/jpip/opj_jpip_transcode.c @@ -49,12 +49,14 @@ static int jpip_to_jp2(char *argv[]) dec = init_jpipdecoder(OPJ_TRUE); if (!(fread_jpip(argv[1], dec))) { + destroy_jpipdecoder(&dec); return 1; } decode_jpip(dec); if (!(fwrite_jp2k(argv[2], dec))) { + destroy_jpipdecoder(&dec); return 1; } @@ -83,12 +85,14 @@ static int jpip_to_j2k(char *argv[]) dec = init_jpipdecoder(OPJ_FALSE); if (!(fread_jpip(argv[1], dec))) { + destroy_jpipdecoder(&dec); return 1; } decode_jpip(dec); if (!(fwrite_jp2k(argv[2], dec))) { + destroy_jpipdecoder(&dec); return 1; } diff --git a/src/bin/jpwl/convert.c b/src/bin/jpwl/convert.c index e6feea50..b7fb5b5c 100644 --- a/src/bin/jpwl/convert.c +++ b/src/bin/jpwl/convert.c @@ -465,6 +465,7 @@ int imagetotga(opj_image_t * image, const char *outfile) || (image->comps[0].prec != image->comps[i + 1].prec)) { fprintf(stderr, "Unable to create a tga file with such J2K image charateristics."); + fclose(fdest); return 1; } } @@ -478,6 +479,7 @@ int imagetotga(opj_image_t * image, const char *outfile) /* Write TGA header */ bpp = write_alpha ? 32 : 24; if (!tga_writeheader(fdest, bpp, width, height, OPJ_TRUE)) { + fclose(fdest); return 1; } @@ -510,6 +512,7 @@ int imagetotga(opj_image_t * image, const char *outfile) res = fwrite(&value, 1, 1, fdest); if (res < 1) { fprintf(stderr, "failed to write 1 byte for %s\n", outfile); + fclose(fdest); return 1; } @@ -517,6 +520,7 @@ int imagetotga(opj_image_t * image, const char *outfile) res = fwrite(&value, 1, 1, fdest); if (res < 1) { fprintf(stderr, "failed to write 1 byte for %s\n", outfile); + fclose(fdest); return 1; } @@ -524,6 +528,7 @@ int imagetotga(opj_image_t * image, const char *outfile) res = fwrite(&value, 1, 1, fdest); if (res < 1) { fprintf(stderr, "failed to write 1 byte for %s\n", outfile); + fclose(fdest); return 1; } @@ -533,12 +538,15 @@ int imagetotga(opj_image_t * image, const char *outfile) res = fwrite(&value, 1, 1, fdest); if (res < 1) { fprintf(stderr, "failed to write 1 byte for %s\n", outfile); + fclose(fdest); return 1; } } } } + fclose(fdest); + return 0; } @@ -832,6 +840,7 @@ opj_image_t* bmptoimage(const char *filename, opj_cparameters_t *parameters) RGB = (unsigned char *) malloc(W * H * sizeof(unsigned char)); if (fread(RGB, sizeof(unsigned char), W * H, IN) != W * H) { + fclose(IN); free(table_R); free(table_G); free(table_B); @@ -1505,6 +1514,7 @@ int imagetopgx(opj_image_t * image, const char *outfile) fdest = fopen(name, "wb"); if (!fdest) { fprintf(stderr, "ERROR -> failed to open %s for writing\n", name); + free(name); return 1; } /* don't need name anymore */ diff --git a/src/bin/jpwl/opj_jpwl_compress.c b/src/bin/jpwl/opj_jpwl_compress.c index c17486a0..ccf03615 100644 --- a/src/bin/jpwl/opj_jpwl_compress.c +++ b/src/bin/jpwl/opj_jpwl_compress.c @@ -1681,8 +1681,15 @@ int main(int argc, char **argv) if (dirptr) { dirptr->filename_buf = (char*)malloc(num_images * OPJ_PATH_LEN * sizeof( char)); /* Stores at max 10 image file names*/ - dirptr->filename = (char**) malloc(num_images * sizeof(char*)); if (!dirptr->filename_buf) { + free(parameters.cp_comment); + free(dirptr); + return 0; + } + dirptr->filename = (char**) malloc(num_images * sizeof(char*)); + if (!dirptr->filename) { + free(parameters.cp_comment); + free(dirptr); return 0; } for (i = 0; i < num_images; i++) { @@ -1690,9 +1697,13 @@ int main(int argc, char **argv) } } if (load_images(dirptr, img_fol.imgdirpath) == 1) { + free(parameters.cp_comment); + free(dirptr); return 0; } if (num_images == 0) { + free(parameters.cp_comment); + free(dirptr); fprintf(stdout, "Folder is empty\n"); return 0; } @@ -1850,6 +1861,7 @@ int main(int argc, char **argv) if (res < (size_t)codestream_length) { /* FIXME */ fprintf(stderr, "failed to write %d (%s)\n", codestream_length, parameters.outfile); + fclose(f); return 1; } fclose(f); @@ -1914,6 +1926,7 @@ int main(int argc, char **argv) if (res < (size_t)codestream_length) { /* FIXME */ fprintf(stderr, "failed to write %d (%s)\n", codestream_length, parameters.outfile); + fclose(f); return 1; } fclose(f); diff --git a/src/bin/jpwl/opj_jpwl_decompress.c b/src/bin/jpwl/opj_jpwl_decompress.c index ffe84e54..a73be1c3 100644 --- a/src/bin/jpwl/opj_jpwl_decompress.c +++ b/src/bin/jpwl/opj_jpwl_decompress.c @@ -597,11 +597,16 @@ int main(int argc, char **argv) if (dirptr) { dirptr->filename_buf = (char*)malloc(num_images * OPJ_PATH_LEN * sizeof( char)); /* Stores at max 10 image file names*/ - dirptr->filename = (char**) malloc(num_images * sizeof(char*)); - if (!dirptr->filename_buf) { + free(dirptr); return 1; } + dirptr->filename = (char**) malloc(num_images * sizeof(char*)); + if (!dirptr->filename) { + free(dirptr); + return 1; + } + for (i = 0; i < num_images; i++) { dirptr->filename[i] = dirptr->filename_buf + i * OPJ_PATH_LEN; } diff --git a/src/bin/mj2/mj2_to_metadata.c b/src/bin/mj2/mj2_to_metadata.c index 44858c86..27f164d7 100644 --- a/src/bin/mj2/mj2_to_metadata.c +++ b/src/bin/mj2/mj2_to_metadata.c @@ -304,6 +304,7 @@ int main(int argc, char *argv[]) xmlout = fopen(outfile, "w"); /* was: argv[2] */ if (!xmlout) { fprintf(stderr, "Failed to open %s for writing.\n", outfile); /* was: argv[2] */ + fclose(file); return 1; } // Leave it open diff --git a/src/bin/mj2/opj_mj2_compress.c b/src/bin/mj2/opj_mj2_compress.c index c61b4c92..8386ba0e 100644 --- a/src/bin/mj2/opj_mj2_compress.c +++ b/src/bin/mj2/opj_mj2_compress.c @@ -708,6 +708,7 @@ int main(int argc, char **argv) yuv_num_frames(&movie->tk[0], mj2_parameters.infile); if (movie->tk[0].num_samples == 0) { + fclose(mj2file); return 1; } diff --git a/src/bin/mj2/opj_mj2_decompress.c b/src/bin/mj2/opj_mj2_decompress.c index 6b26d563..6d1a9616 100644 --- a/src/bin/mj2/opj_mj2_decompress.c +++ b/src/bin/mj2/opj_mj2_decompress.c @@ -99,8 +99,9 @@ int main(int argc, char *argv[]) /* Checking output file */ outfile = fopen(argv[2], "w"); - if (!file) { + if (!outfile) { fprintf(stderr, "failed to open %s for writing\n", argv[2]); + fclose(file); return 1; } fclose(outfile); @@ -169,6 +170,7 @@ int main(int argc, char *argv[]) if ((frame_codestream = (unsigned char*) realloc(frame_codestream, max_codstrm_size)) == NULL) { printf("Error reallocation memory\n"); + free(frame_codestream); return 1; }; } diff --git a/src/lib/openjp2/j2k.c b/src/lib/openjp2/j2k.c index 8e59f8b5..b16b8fe0 100644 --- a/src/lib/openjp2/j2k.c +++ b/src/lib/openjp2/j2k.c @@ -11876,7 +11876,7 @@ OPJ_BOOL opj_j2k_encode(opj_j2k_t * p_j2k, } } } - l_current_tile_size = opj_tcd_get_encoded_tile_size(p_j2k->m_tcd); + l_current_tile_size = opj_tcd_get_encoder_input_buffer_size(p_j2k->m_tcd); if (!l_reuse_data) { if (l_current_tile_size > l_max_tile_size) { OPJ_BYTE *l_new_current_data = (OPJ_BYTE *) opj_realloc(l_current_data, diff --git a/src/lib/openjp2/j2k.h b/src/lib/openjp2/j2k.h index 5d393c98..3ea6395f 100644 --- a/src/lib/openjp2/j2k.h +++ b/src/lib/openjp2/j2k.h @@ -577,15 +577,16 @@ typedef struct opj_j2k { /** the current tile coder/decoder **/ struct opj_tcd * m_tcd; - /** Number of threads to use */ - int m_num_threads; - /** Thread pool */ opj_thread_pool_t* m_tp; + /** Image width coming from JP2 IHDR box. 0 from a pure codestream */ OPJ_UINT32 ihdr_w; + + /** Image height coming from JP2 IHDR box. 0 from a pure codestream */ OPJ_UINT32 ihdr_h; - OPJ_UINT32 enumcs; + + /** Set to 1 by the decoder initialization if OPJ_DPARAMETERS_DUMP_FLAG is set */ unsigned int dump_state; } opj_j2k_t; diff --git a/src/lib/openjp2/jp2.c b/src/lib/openjp2/jp2.c index 4402ffe3..a2363666 100644 --- a/src/lib/openjp2/jp2.c +++ b/src/lib/openjp2/jp2.c @@ -1584,9 +1584,7 @@ static OPJ_BOOL opj_jp2_read_colr(opj_jp2_t *jp2, "COLR BOX meth value is not a regular value (%d), " "so we will ignore the entire Colour Specification box. \n", jp2->meth); } - if (jp2->color.jp2_has_colr) { - jp2->j2k->enumcs = jp2->enumcs; - } + return OPJ_TRUE; } diff --git a/src/lib/openjp2/openjpeg.h b/src/lib/openjp2/openjpeg.h index 11b0e731..d0a38836 100644 --- a/src/lib/openjp2/openjpeg.h +++ b/src/lib/openjp2/openjpeg.h @@ -349,6 +349,10 @@ typedef void (*opj_msg_callback)(const char *msg, void *client_data); ========================================================== */ +#ifndef OPJ_UINT32_SEMANTICALLY_BUT_INT32 +#define OPJ_UINT32_SEMANTICALLY_BUT_INT32 OPJ_INT32 +#endif + /** * Progression order changes * @@ -367,7 +371,7 @@ typedef struct opj_poc { /** Tile number (starting at 1) */ OPJ_UINT32 tile; /** Start and end values for Tile width and height*/ - OPJ_INT32 tx0, tx1, ty0, ty1; + OPJ_UINT32_SEMANTICALLY_BUT_INT32 tx0, tx1, ty0, ty1; /** Start value, initialised in pi_initialise_encode*/ OPJ_UINT32 layS, resS, compS, prcS; /** End value, initialised in pi_initialise_encode */ diff --git a/src/lib/openjp2/opj_intmath.h b/src/lib/openjp2/opj_intmath.h index 754b5512..afe69d90 100644 --- a/src/lib/openjp2/opj_intmath.h +++ b/src/lib/openjp2/opj_intmath.h @@ -208,6 +208,16 @@ static INLINE OPJ_INT32 opj_int_floordivpow2(OPJ_INT32 a, OPJ_INT32 b) { return a >> b; } + +/** +Divide an integer by a power of 2 and round downwards +@return Returns a divided by 2^b +*/ +static INLINE OPJ_UINT32 opj_uint_floordivpow2(OPJ_UINT32 a, OPJ_UINT32 b) +{ + return a >> b; +} + /** Get logarithm of an integer and round downwards @return Returns log2(a) diff --git a/src/lib/openjp2/pi.c b/src/lib/openjp2/pi.c index 75431b7a..7debc774 100644 --- a/src/lib/openjp2/pi.c +++ b/src/lib/openjp2/pi.c @@ -36,6 +36,8 @@ * POSSIBILITY OF SUCH DAMAGE. */ +#define OPJ_UINT32_SEMANTICALLY_BUT_INT32 OPJ_UINT32 + #include "opj_includes.h" /** @defgroup PI PI - Implementation of a packet iterator */ @@ -91,10 +93,10 @@ static OPJ_BOOL opj_pi_next_cprl(opj_pi_iterator_t * pi); */ static void opj_pi_update_encode_poc_and_final(opj_cp_t *p_cp, OPJ_UINT32 p_tileno, - OPJ_INT32 p_tx0, - OPJ_INT32 p_tx1, - OPJ_INT32 p_ty0, - OPJ_INT32 p_ty1, + OPJ_UINT32 p_tx0, + OPJ_UINT32 p_tx1, + OPJ_UINT32 p_ty0, + OPJ_UINT32 p_ty1, OPJ_UINT32 p_max_prec, OPJ_UINT32 p_max_res, OPJ_UINT32 p_dx_min, @@ -118,10 +120,10 @@ static void opj_pi_update_encode_poc_and_final(opj_cp_t *p_cp, static void opj_pi_update_encode_not_poc(opj_cp_t *p_cp, OPJ_UINT32 p_num_comps, OPJ_UINT32 p_tileno, - OPJ_INT32 p_tx0, - OPJ_INT32 p_tx1, - OPJ_INT32 p_ty0, - OPJ_INT32 p_ty1, + OPJ_UINT32 p_tx0, + OPJ_UINT32 p_tx1, + OPJ_UINT32 p_ty0, + OPJ_UINT32 p_ty1, OPJ_UINT32 p_max_prec, OPJ_UINT32 p_max_res, OPJ_UINT32 p_dx_min, @@ -144,10 +146,10 @@ static void opj_pi_update_encode_not_poc(opj_cp_t *p_cp, static void opj_get_encoding_parameters(const opj_image_t *p_image, const opj_cp_t *p_cp, OPJ_UINT32 tileno, - OPJ_INT32 * p_tx0, - OPJ_INT32 * p_tx1, - OPJ_INT32 * p_ty0, - OPJ_INT32 * p_ty1, + OPJ_UINT32 * p_tx0, + OPJ_UINT32 * p_tx1, + OPJ_UINT32 * p_ty0, + OPJ_UINT32 * p_ty1, OPJ_UINT32 * p_dx_min, OPJ_UINT32 * p_dy_min, OPJ_UINT32 * p_max_prec, @@ -176,10 +178,10 @@ static void opj_get_encoding_parameters(const opj_image_t *p_image, static void opj_get_all_encoding_parameters(const opj_image_t *p_image, const opj_cp_t *p_cp, OPJ_UINT32 tileno, - OPJ_INT32 * p_tx0, - OPJ_INT32 * p_tx1, - OPJ_INT32 * p_ty0, - OPJ_INT32 * p_ty1, + OPJ_UINT32 * p_tx0, + OPJ_UINT32 * p_tx1, + OPJ_UINT32 * p_ty0, + OPJ_UINT32 * p_ty1, OPJ_UINT32 * p_dx_min, OPJ_UINT32 * p_dy_min, OPJ_UINT32 * p_max_prec, @@ -382,10 +384,10 @@ static OPJ_BOOL opj_pi_next_rpcl(opj_pi_iterator_t * pi) pi->x += (pi->dx - (pi->x % pi->dx))) { for (pi->compno = pi->poc.compno0; pi->compno < pi->poc.compno1; pi->compno++) { OPJ_UINT32 levelno; - OPJ_INT32 trx0, try0; - OPJ_INT32 trx1, try1; + OPJ_UINT32 trx0, try0; + OPJ_UINT32 trx1, try1; OPJ_UINT32 rpx, rpy; - OPJ_INT32 prci, prcj; + OPJ_UINT32 prci, prcj; comp = &pi->comps[pi->compno]; if (pi->resno >= comp->numresolutions) { continue; @@ -404,10 +406,10 @@ static OPJ_BOOL opj_pi_next_rpcl(opj_pi_iterator_t * pi) (comp->dy << levelno) > INT_MAX) { continue; } - trx0 = opj_int_ceildiv(pi->tx0, (OPJ_INT32)(comp->dx << levelno)); - try0 = opj_int_ceildiv(pi->ty0, (OPJ_INT32)(comp->dy << levelno)); - trx1 = opj_int_ceildiv(pi->tx1, (OPJ_INT32)(comp->dx << levelno)); - try1 = opj_int_ceildiv(pi->ty1, (OPJ_INT32)(comp->dy << levelno)); + trx0 = opj_uint_ceildiv(pi->tx0, (comp->dx << levelno)); + try0 = opj_uint_ceildiv(pi->ty0, (comp->dy << levelno)); + trx1 = opj_uint_ceildiv(pi->tx1, (comp->dx << levelno)); + try1 = opj_uint_ceildiv(pi->ty1, (comp->dy << levelno)); rpx = res->pdx + levelno; rpy = res->pdy + levelno; @@ -421,12 +423,12 @@ static OPJ_BOOL opj_pi_next_rpcl(opj_pi_iterator_t * pi) } /* See ISO-15441. B.12.1.3 Resolution level-position-component-layer progression */ - if (!((pi->y % (OPJ_INT32)(comp->dy << rpy) == 0) || ((pi->y == pi->ty0) && - ((try0 << levelno) % (1 << rpy))))) { + if (!((pi->y % (comp->dy << rpy) == 0) || ((pi->y == pi->ty0) && + ((try0 << levelno) % (1U << rpy))))) { continue; } - if (!((pi->x % (OPJ_INT32)(comp->dx << rpx) == 0) || ((pi->x == pi->tx0) && - ((trx0 << levelno) % (1 << rpx))))) { + if (!((pi->x % (comp->dx << rpx) == 0) || ((pi->x == pi->tx0) && + ((trx0 << levelno) % (1U << rpx))))) { continue; } @@ -438,13 +440,13 @@ static OPJ_BOOL opj_pi_next_rpcl(opj_pi_iterator_t * pi) continue; } - prci = opj_int_floordivpow2(opj_int_ceildiv(pi->x, - (OPJ_INT32)(comp->dx << levelno)), (OPJ_INT32)res->pdx) - - opj_int_floordivpow2(trx0, (OPJ_INT32)res->pdx); - prcj = opj_int_floordivpow2(opj_int_ceildiv(pi->y, - (OPJ_INT32)(comp->dy << levelno)), (OPJ_INT32)res->pdy) - - opj_int_floordivpow2(try0, (OPJ_INT32)res->pdy); - pi->precno = (OPJ_UINT32)(prci + prcj * (OPJ_INT32)res->pw); + prci = opj_uint_floordivpow2(opj_uint_ceildiv(pi->x, + (comp->dx << levelno)), res->pdx) + - opj_uint_floordivpow2(trx0, res->pdx); + prcj = opj_uint_floordivpow2(opj_uint_ceildiv(pi->y, + (comp->dy << levelno)), res->pdy) + - opj_uint_floordivpow2(try0, res->pdy); + pi->precno = prci + prcj * res->pw; for (pi->layno = pi->poc.layno0; pi->layno < pi->poc.layno1; pi->layno++) { index = pi->layno * pi->step_l + pi->resno * pi->step_r + pi->compno * pi->step_c + pi->precno * pi->step_p; @@ -517,10 +519,10 @@ static OPJ_BOOL opj_pi_next_pcrl(opj_pi_iterator_t * pi) for (pi->resno = pi->poc.resno0; pi->resno < opj_uint_min(pi->poc.resno1, comp->numresolutions); pi->resno++) { OPJ_UINT32 levelno; - OPJ_INT32 trx0, try0; - OPJ_INT32 trx1, try1; + OPJ_UINT32 trx0, try0; + OPJ_UINT32 trx1, try1; OPJ_UINT32 rpx, rpy; - OPJ_INT32 prci, prcj; + OPJ_UINT32 prci, prcj; res = &comp->resolutions[pi->resno]; levelno = comp->numresolutions - 1 - pi->resno; /* Avoids division by zero */ @@ -535,10 +537,10 @@ static OPJ_BOOL opj_pi_next_pcrl(opj_pi_iterator_t * pi) (comp->dy << levelno) > INT_MAX) { continue; } - trx0 = opj_int_ceildiv(pi->tx0, (OPJ_INT32)(comp->dx << levelno)); - try0 = opj_int_ceildiv(pi->ty0, (OPJ_INT32)(comp->dy << levelno)); - trx1 = opj_int_ceildiv(pi->tx1, (OPJ_INT32)(comp->dx << levelno)); - try1 = opj_int_ceildiv(pi->ty1, (OPJ_INT32)(comp->dy << levelno)); + trx0 = opj_uint_ceildiv(pi->tx0, (comp->dx << levelno)); + try0 = opj_uint_ceildiv(pi->ty0, (comp->dy << levelno)); + trx1 = opj_uint_ceildiv(pi->tx1, (comp->dx << levelno)); + try1 = opj_uint_ceildiv(pi->ty1, (comp->dy << levelno)); rpx = res->pdx + levelno; rpy = res->pdy + levelno; @@ -552,12 +554,12 @@ static OPJ_BOOL opj_pi_next_pcrl(opj_pi_iterator_t * pi) } /* See ISO-15441. B.12.1.4 Position-component-resolution level-layer progression */ - if (!((pi->y % (OPJ_INT32)(comp->dy << rpy) == 0) || ((pi->y == pi->ty0) && - ((try0 << levelno) % (1 << rpy))))) { + if (!((pi->y % (comp->dy << rpy) == 0) || ((pi->y == pi->ty0) && + ((try0 << levelno) % (1U << rpy))))) { continue; } - if (!((pi->x % (OPJ_INT32)(comp->dx << rpx) == 0) || ((pi->x == pi->tx0) && - ((trx0 << levelno) % (1 << rpx))))) { + if (!((pi->x % (comp->dx << rpx) == 0) || ((pi->x == pi->tx0) && + ((trx0 << levelno) % (1U << rpx))))) { continue; } @@ -569,13 +571,13 @@ static OPJ_BOOL opj_pi_next_pcrl(opj_pi_iterator_t * pi) continue; } - prci = opj_int_floordivpow2(opj_int_ceildiv(pi->x, - (OPJ_INT32)(comp->dx << levelno)), (OPJ_INT32)res->pdx) - - opj_int_floordivpow2(trx0, (OPJ_INT32)res->pdx); - prcj = opj_int_floordivpow2(opj_int_ceildiv(pi->y, - (OPJ_INT32)(comp->dy << levelno)), (OPJ_INT32)res->pdy) - - opj_int_floordivpow2(try0, (OPJ_INT32)res->pdy); - pi->precno = (OPJ_UINT32)(prci + prcj * (OPJ_INT32)res->pw); + prci = opj_uint_floordivpow2(opj_uint_ceildiv(pi->x, + (comp->dx << levelno)), res->pdx) + - opj_uint_floordivpow2(trx0, res->pdx); + prcj = opj_uint_floordivpow2(opj_uint_ceildiv(pi->y, + (comp->dy << levelno)), res->pdy) + - opj_uint_floordivpow2(try0, res->pdy); + pi->precno = prci + prcj * res->pw; for (pi->layno = pi->poc.layno0; pi->layno < pi->poc.layno1; pi->layno++) { index = pi->layno * pi->step_l + pi->resno * pi->step_r + pi->compno * pi->step_c + pi->precno * pi->step_p; @@ -646,10 +648,10 @@ static OPJ_BOOL opj_pi_next_cprl(opj_pi_iterator_t * pi) for (pi->resno = pi->poc.resno0; pi->resno < opj_uint_min(pi->poc.resno1, comp->numresolutions); pi->resno++) { OPJ_UINT32 levelno; - OPJ_INT32 trx0, try0; - OPJ_INT32 trx1, try1; + OPJ_UINT32 trx0, try0; + OPJ_UINT32 trx1, try1; OPJ_UINT32 rpx, rpy; - OPJ_INT32 prci, prcj; + OPJ_UINT32 prci, prcj; res = &comp->resolutions[pi->resno]; levelno = comp->numresolutions - 1 - pi->resno; /* Avoids division by zero on id_000004,sig_06,src_000679,op_arith8,pos_49,val_-17 */ @@ -663,10 +665,10 @@ static OPJ_BOOL opj_pi_next_cprl(opj_pi_iterator_t * pi) (comp->dy << levelno) > INT_MAX) { continue; } - trx0 = opj_int_ceildiv(pi->tx0, (OPJ_INT32)(comp->dx << levelno)); - try0 = opj_int_ceildiv(pi->ty0, (OPJ_INT32)(comp->dy << levelno)); - trx1 = opj_int_ceildiv(pi->tx1, (OPJ_INT32)(comp->dx << levelno)); - try1 = opj_int_ceildiv(pi->ty1, (OPJ_INT32)(comp->dy << levelno)); + trx0 = opj_uint_ceildiv(pi->tx0, (comp->dx << levelno)); + try0 = opj_uint_ceildiv(pi->ty0, (comp->dy << levelno)); + trx1 = opj_uint_ceildiv(pi->tx1, (comp->dx << levelno)); + try1 = opj_uint_ceildiv(pi->ty1, (comp->dy << levelno)); rpx = res->pdx + levelno; rpy = res->pdy + levelno; @@ -680,12 +682,12 @@ static OPJ_BOOL opj_pi_next_cprl(opj_pi_iterator_t * pi) } /* See ISO-15441. B.12.1.5 Component-position-resolution level-layer progression */ - if (!((pi->y % (OPJ_INT32)(comp->dy << rpy) == 0) || ((pi->y == pi->ty0) && - ((try0 << levelno) % (1 << rpy))))) { + if (!((pi->y % (comp->dy << rpy) == 0) || ((pi->y == pi->ty0) && + ((try0 << levelno) % (1U << rpy))))) { continue; } - if (!((pi->x % (OPJ_INT32)(comp->dx << rpx) == 0) || ((pi->x == pi->tx0) && - ((trx0 << levelno) % (1 << rpx))))) { + if (!((pi->x % (comp->dx << rpx) == 0) || ((pi->x == pi->tx0) && + ((trx0 << levelno) % (1U << rpx))))) { continue; } @@ -697,13 +699,13 @@ static OPJ_BOOL opj_pi_next_cprl(opj_pi_iterator_t * pi) continue; } - prci = opj_int_floordivpow2(opj_int_ceildiv(pi->x, - (OPJ_INT32)(comp->dx << levelno)), (OPJ_INT32)res->pdx) - - opj_int_floordivpow2(trx0, (OPJ_INT32)res->pdx); - prcj = opj_int_floordivpow2(opj_int_ceildiv(pi->y, - (OPJ_INT32)(comp->dy << levelno)), (OPJ_INT32)res->pdy) - - opj_int_floordivpow2(try0, (OPJ_INT32)res->pdy); - pi->precno = (OPJ_UINT32)(prci + prcj * (OPJ_INT32)res->pw); + prci = opj_uint_floordivpow2(opj_uint_ceildiv(pi->x, + (comp->dx << levelno)), res->pdx) + - opj_uint_floordivpow2(trx0, res->pdx); + prcj = opj_uint_floordivpow2(opj_uint_ceildiv(pi->y, + (comp->dy << levelno)), res->pdy) + - opj_uint_floordivpow2(try0, res->pdy); + pi->precno = (OPJ_UINT32)(prci + prcj * res->pw); for (pi->layno = pi->poc.layno0; pi->layno < pi->poc.layno1; pi->layno++) { index = pi->layno * pi->step_l + pi->resno * pi->step_r + pi->compno * pi->step_c + pi->precno * pi->step_p; @@ -729,10 +731,10 @@ LABEL_SKIP: static void opj_get_encoding_parameters(const opj_image_t *p_image, const opj_cp_t *p_cp, OPJ_UINT32 p_tileno, - OPJ_INT32 * p_tx0, - OPJ_INT32 * p_tx1, - OPJ_INT32 * p_ty0, - OPJ_INT32 * p_ty1, + OPJ_UINT32 * p_tx0, + OPJ_UINT32 * p_tx1, + OPJ_UINT32 * p_ty0, + OPJ_UINT32 * p_ty1, OPJ_UINT32 * p_dx_min, OPJ_UINT32 * p_dy_min, OPJ_UINT32 * p_max_prec, @@ -768,12 +770,12 @@ static void opj_get_encoding_parameters(const opj_image_t *p_image, /* find extent of tile */ l_tx0 = p_cp->tx0 + p * p_cp->tdx; /* can't be greater than p_image->x1 so won't overflow */ - *p_tx0 = (OPJ_INT32)opj_uint_max(l_tx0, p_image->x0); - *p_tx1 = (OPJ_INT32)opj_uint_min(opj_uint_adds(l_tx0, p_cp->tdx), p_image->x1); + *p_tx0 = opj_uint_max(l_tx0, p_image->x0); + *p_tx1 = opj_uint_min(opj_uint_adds(l_tx0, p_cp->tdx), p_image->x1); l_ty0 = p_cp->ty0 + q * p_cp->tdy; /* can't be greater than p_image->y1 so won't overflow */ - *p_ty0 = (OPJ_INT32)opj_uint_max(l_ty0, p_image->y0); - *p_ty1 = (OPJ_INT32)opj_uint_min(opj_uint_adds(l_ty0, p_cp->tdy), p_image->y1); + *p_ty0 = opj_uint_max(l_ty0, p_image->y0); + *p_ty1 = opj_uint_min(opj_uint_adds(l_ty0, p_cp->tdy), p_image->y1); /* max precision is 0 (can only grow) */ *p_max_prec = 0; @@ -786,17 +788,17 @@ static void opj_get_encoding_parameters(const opj_image_t *p_image, for (compno = 0; compno < p_image->numcomps; ++compno) { /* arithmetic variables to calculate */ OPJ_UINT32 l_level_no; - OPJ_INT32 l_rx0, l_ry0, l_rx1, l_ry1; - OPJ_INT32 l_px0, l_py0, l_px1, py1; + OPJ_UINT32 l_rx0, l_ry0, l_rx1, l_ry1; + OPJ_UINT32 l_px0, l_py0, l_px1, py1; OPJ_UINT32 l_pdx, l_pdy; OPJ_UINT32 l_pw, l_ph; OPJ_UINT32 l_product; - OPJ_INT32 l_tcx0, l_tcy0, l_tcx1, l_tcy1; + OPJ_UINT32 l_tcx0, l_tcy0, l_tcx1, l_tcy1; - l_tcx0 = opj_int_ceildiv(*p_tx0, (OPJ_INT32)l_img_comp->dx); - l_tcy0 = opj_int_ceildiv(*p_ty0, (OPJ_INT32)l_img_comp->dy); - l_tcx1 = opj_int_ceildiv(*p_tx1, (OPJ_INT32)l_img_comp->dx); - l_tcy1 = opj_int_ceildiv(*p_ty1, (OPJ_INT32)l_img_comp->dy); + l_tcx0 = opj_uint_ceildiv(*p_tx0, l_img_comp->dx); + l_tcy0 = opj_uint_ceildiv(*p_ty0, l_img_comp->dy); + l_tcx1 = opj_uint_ceildiv(*p_tx1, l_img_comp->dx); + l_tcy1 = opj_uint_ceildiv(*p_ty1, l_img_comp->dy); if (l_tccp->numresolutions > *p_max_res) { *p_max_res = l_tccp->numresolutions; @@ -820,19 +822,19 @@ static void opj_get_encoding_parameters(const opj_image_t *p_image, /* various calculations of extents */ l_level_no = l_tccp->numresolutions - 1 - resno; - l_rx0 = opj_int_ceildivpow2(l_tcx0, (OPJ_INT32)l_level_no); - l_ry0 = opj_int_ceildivpow2(l_tcy0, (OPJ_INT32)l_level_no); - l_rx1 = opj_int_ceildivpow2(l_tcx1, (OPJ_INT32)l_level_no); - l_ry1 = opj_int_ceildivpow2(l_tcy1, (OPJ_INT32)l_level_no); + l_rx0 = opj_uint_ceildivpow2(l_tcx0, l_level_no); + l_ry0 = opj_uint_ceildivpow2(l_tcy0, l_level_no); + l_rx1 = opj_uint_ceildivpow2(l_tcx1, l_level_no); + l_ry1 = opj_uint_ceildivpow2(l_tcy1, l_level_no); - l_px0 = opj_int_floordivpow2(l_rx0, (OPJ_INT32)l_pdx) << l_pdx; - l_py0 = opj_int_floordivpow2(l_ry0, (OPJ_INT32)l_pdy) << l_pdy; - l_px1 = opj_int_ceildivpow2(l_rx1, (OPJ_INT32)l_pdx) << l_pdx; + l_px0 = opj_uint_floordivpow2(l_rx0, l_pdx) << l_pdx; + l_py0 = opj_uint_floordivpow2(l_ry0, l_pdy) << l_pdy; + l_px1 = opj_uint_ceildivpow2(l_rx1, l_pdx) << l_pdx; - py1 = opj_int_ceildivpow2(l_ry1, (OPJ_INT32)l_pdy) << l_pdy; + py1 = opj_uint_ceildivpow2(l_ry1, l_pdy) << l_pdy; - l_pw = (l_rx0 == l_rx1) ? 0 : (OPJ_UINT32)((l_px1 - l_px0) >> l_pdx); - l_ph = (l_ry0 == l_ry1) ? 0 : (OPJ_UINT32)((py1 - l_py0) >> l_pdy); + l_pw = (l_rx0 == l_rx1) ? 0 : ((l_px1 - l_px0) >> l_pdx); + l_ph = (l_ry0 == l_ry1) ? 0 : ((py1 - l_py0) >> l_pdy); l_product = l_pw * l_ph; @@ -850,10 +852,10 @@ static void opj_get_encoding_parameters(const opj_image_t *p_image, static void opj_get_all_encoding_parameters(const opj_image_t *p_image, const opj_cp_t *p_cp, OPJ_UINT32 tileno, - OPJ_INT32 * p_tx0, - OPJ_INT32 * p_tx1, - OPJ_INT32 * p_ty0, - OPJ_INT32 * p_ty1, + OPJ_UINT32 * p_tx0, + OPJ_UINT32 * p_tx1, + OPJ_UINT32 * p_ty0, + OPJ_UINT32 * p_ty1, OPJ_UINT32 * p_dx_min, OPJ_UINT32 * p_dy_min, OPJ_UINT32 * p_max_prec, @@ -894,12 +896,12 @@ static void opj_get_all_encoding_parameters(const opj_image_t *p_image, /* here calculation of tx0, tx1, ty0, ty1, maxprec, l_dx and l_dy */ l_tx0 = p_cp->tx0 + p * p_cp->tdx; /* can't be greater than p_image->x1 so won't overflow */ - *p_tx0 = (OPJ_INT32)opj_uint_max(l_tx0, p_image->x0); - *p_tx1 = (OPJ_INT32)opj_uint_min(opj_uint_adds(l_tx0, p_cp->tdx), p_image->x1); + *p_tx0 = opj_uint_max(l_tx0, p_image->x0); + *p_tx1 = opj_uint_min(opj_uint_adds(l_tx0, p_cp->tdx), p_image->x1); l_ty0 = p_cp->ty0 + q * p_cp->tdy; /* can't be greater than p_image->y1 so won't overflow */ - *p_ty0 = (OPJ_INT32)opj_uint_max(l_ty0, p_image->y0); - *p_ty1 = (OPJ_INT32)opj_uint_min(opj_uint_adds(l_ty0, p_cp->tdy), p_image->y1); + *p_ty0 = opj_uint_max(l_ty0, p_image->y0); + *p_ty1 = opj_uint_min(opj_uint_adds(l_ty0, p_cp->tdy), p_image->y1); /* max precision and resolution is 0 (can only grow)*/ *p_max_prec = 0; @@ -912,18 +914,18 @@ static void opj_get_all_encoding_parameters(const opj_image_t *p_image, for (compno = 0; compno < p_image->numcomps; ++compno) { /* aritmetic variables to calculate*/ OPJ_UINT32 l_level_no; - OPJ_INT32 l_rx0, l_ry0, l_rx1, l_ry1; - OPJ_INT32 l_px0, l_py0, l_px1, py1; + OPJ_UINT32 l_rx0, l_ry0, l_rx1, l_ry1; + OPJ_UINT32 l_px0, l_py0, l_px1, py1; OPJ_UINT32 l_product; - OPJ_INT32 l_tcx0, l_tcy0, l_tcx1, l_tcy1; + OPJ_UINT32 l_tcx0, l_tcy0, l_tcx1, l_tcy1; OPJ_UINT32 l_pdx, l_pdy, l_pw, l_ph; lResolutionPtr = p_resolutions[compno]; - l_tcx0 = opj_int_ceildiv(*p_tx0, (OPJ_INT32)l_img_comp->dx); - l_tcy0 = opj_int_ceildiv(*p_ty0, (OPJ_INT32)l_img_comp->dy); - l_tcx1 = opj_int_ceildiv(*p_tx1, (OPJ_INT32)l_img_comp->dx); - l_tcy1 = opj_int_ceildiv(*p_ty1, (OPJ_INT32)l_img_comp->dy); + l_tcx0 = opj_uint_ceildiv(*p_tx0, l_img_comp->dx); + l_tcy0 = opj_uint_ceildiv(*p_ty0, l_img_comp->dy); + l_tcx1 = opj_uint_ceildiv(*p_tx1, l_img_comp->dx); + l_tcy1 = opj_uint_ceildiv(*p_ty1, l_img_comp->dy); if (l_tccp->numresolutions > *p_max_res) { *p_max_res = l_tccp->numresolutions; @@ -945,25 +947,25 @@ static void opj_get_all_encoding_parameters(const opj_image_t *p_image, l_img_comp->dx <= UINT_MAX / (1u << (l_pdx + l_level_no))) { l_dx = l_img_comp->dx * (1u << (l_pdx + l_level_no)); /* take the minimum size for l_dx for each comp and resolution*/ - *p_dx_min = (OPJ_UINT32)opj_int_min((OPJ_INT32) * p_dx_min, (OPJ_INT32)l_dx); + *p_dx_min = opj_uint_min(*p_dx_min, l_dx); } if (l_pdy + l_level_no < 32 && l_img_comp->dy <= UINT_MAX / (1u << (l_pdy + l_level_no))) { l_dy = l_img_comp->dy * (1u << (l_pdy + l_level_no)); - *p_dy_min = (OPJ_UINT32)opj_int_min((OPJ_INT32) * p_dy_min, (OPJ_INT32)l_dy); + *p_dy_min = opj_uint_min(*p_dy_min, l_dy); } /* various calculations of extents*/ - l_rx0 = opj_int_ceildivpow2(l_tcx0, (OPJ_INT32)l_level_no); - l_ry0 = opj_int_ceildivpow2(l_tcy0, (OPJ_INT32)l_level_no); - l_rx1 = opj_int_ceildivpow2(l_tcx1, (OPJ_INT32)l_level_no); - l_ry1 = opj_int_ceildivpow2(l_tcy1, (OPJ_INT32)l_level_no); - l_px0 = opj_int_floordivpow2(l_rx0, (OPJ_INT32)l_pdx) << l_pdx; - l_py0 = opj_int_floordivpow2(l_ry0, (OPJ_INT32)l_pdy) << l_pdy; - l_px1 = opj_int_ceildivpow2(l_rx1, (OPJ_INT32)l_pdx) << l_pdx; - py1 = opj_int_ceildivpow2(l_ry1, (OPJ_INT32)l_pdy) << l_pdy; - l_pw = (l_rx0 == l_rx1) ? 0 : (OPJ_UINT32)((l_px1 - l_px0) >> l_pdx); - l_ph = (l_ry0 == l_ry1) ? 0 : (OPJ_UINT32)((py1 - l_py0) >> l_pdy); + l_rx0 = opj_uint_ceildivpow2(l_tcx0, l_level_no); + l_ry0 = opj_uint_ceildivpow2(l_tcy0, l_level_no); + l_rx1 = opj_uint_ceildivpow2(l_tcx1, l_level_no); + l_ry1 = opj_uint_ceildivpow2(l_tcy1, l_level_no); + l_px0 = opj_uint_floordivpow2(l_rx0, l_pdx) << l_pdx; + l_py0 = opj_uint_floordivpow2(l_ry0, l_pdy) << l_pdy; + l_px1 = opj_uint_ceildivpow2(l_rx1, l_pdx) << l_pdx; + py1 = opj_uint_ceildivpow2(l_ry1, l_pdy) << l_pdy; + l_pw = (l_rx0 == l_rx1) ? 0 : ((l_px1 - l_px0) >> l_pdx); + l_ph = (l_ry0 == l_ry1) ? 0 : ((py1 - l_py0) >> l_pdy); *lResolutionPtr++ = l_pw; *lResolutionPtr++ = l_ph; l_product = l_pw * l_ph; @@ -1045,10 +1047,10 @@ static opj_pi_iterator_t * opj_pi_create(const opj_image_t *image, static void opj_pi_update_encode_poc_and_final(opj_cp_t *p_cp, OPJ_UINT32 p_tileno, - OPJ_INT32 p_tx0, - OPJ_INT32 p_tx1, - OPJ_INT32 p_ty0, - OPJ_INT32 p_ty1, + OPJ_UINT32 p_tx0, + OPJ_UINT32 p_tx1, + OPJ_UINT32 p_ty0, + OPJ_UINT32 p_ty1, OPJ_UINT32 p_max_prec, OPJ_UINT32 p_max_res, OPJ_UINT32 p_dx_min, @@ -1125,10 +1127,10 @@ static void opj_pi_update_encode_poc_and_final(opj_cp_t *p_cp, static void opj_pi_update_encode_not_poc(opj_cp_t *p_cp, OPJ_UINT32 p_num_comps, OPJ_UINT32 p_tileno, - OPJ_INT32 p_tx0, - OPJ_INT32 p_tx1, - OPJ_INT32 p_ty0, - OPJ_INT32 p_ty1, + OPJ_UINT32 p_tx0, + OPJ_UINT32 p_tx1, + OPJ_UINT32 p_ty0, + OPJ_UINT32 p_ty1, OPJ_UINT32 p_max_prec, OPJ_UINT32 p_max_res, OPJ_UINT32 p_dx_min, @@ -1167,10 +1169,10 @@ static void opj_pi_update_encode_not_poc(opj_cp_t *p_cp, l_current_poc->prg = l_tcp->prg; l_current_poc->prcS = 0; l_current_poc->prcE = p_max_prec; - l_current_poc->txS = (OPJ_UINT32)p_tx0; - l_current_poc->txE = (OPJ_UINT32)p_tx1; - l_current_poc->tyS = (OPJ_UINT32)p_ty0; - l_current_poc->tyE = (OPJ_UINT32)p_ty1; + l_current_poc->txS = p_tx0; + l_current_poc->txE = p_tx1; + l_current_poc->tyS = p_ty0; + l_current_poc->tyE = p_ty1; l_current_poc->dx = p_dx_min; l_current_poc->dy = p_dy_min; ++ l_current_poc; @@ -1367,7 +1369,7 @@ opj_pi_iterator_t *opj_pi_create_decode(opj_image_t *p_image, /* encoding prameters to set */ OPJ_UINT32 l_max_res; OPJ_UINT32 l_max_prec; - OPJ_INT32 l_tx0, l_tx1, l_ty0, l_ty1; + OPJ_UINT32 l_tx0, l_tx1, l_ty0, l_ty1; OPJ_UINT32 l_dx_min, l_dy_min; OPJ_UINT32 l_bound; OPJ_UINT32 l_step_p, l_step_c, l_step_r, l_step_l ; @@ -1567,7 +1569,7 @@ opj_pi_iterator_t *opj_pi_initialise_encode(const opj_image_t *p_image, /* encoding prameters to set*/ OPJ_UINT32 l_max_res; OPJ_UINT32 l_max_prec; - OPJ_INT32 l_tx0, l_tx1, l_ty0, l_ty1; + OPJ_UINT32 l_tx0, l_tx1, l_ty0, l_ty1; OPJ_UINT32 l_dx_min, l_dy_min; OPJ_UINT32 l_bound; OPJ_UINT32 l_step_p, l_step_c, l_step_r, l_step_l ; @@ -1771,10 +1773,10 @@ void opj_pi_create_encode(opj_pi_iterator_t *pi, pi[pino].poc.layno1 = tcp->layE; pi[pino].poc.precno0 = tcp->prcS; pi[pino].poc.precno1 = tcp->prcE; - pi[pino].poc.tx0 = (OPJ_INT32)tcp->txS; - pi[pino].poc.ty0 = (OPJ_INT32)tcp->tyS; - pi[pino].poc.tx1 = (OPJ_INT32)tcp->txE; - pi[pino].poc.ty1 = (OPJ_INT32)tcp->tyE; + pi[pino].poc.tx0 = tcp->txS; + pi[pino].poc.ty0 = tcp->tyS; + pi[pino].poc.tx1 = tcp->txE; + pi[pino].poc.ty1 = tcp->tyE; } else { for (i = tppos + 1; i < 4; i++) { switch (prog[i]) { @@ -1798,10 +1800,10 @@ void opj_pi_create_encode(opj_pi_iterator_t *pi, pi[pino].poc.precno1 = tcp->prcE; break; default: - pi[pino].poc.tx0 = (OPJ_INT32)tcp->txS; - pi[pino].poc.ty0 = (OPJ_INT32)tcp->tyS; - pi[pino].poc.tx1 = (OPJ_INT32)tcp->txE; - pi[pino].poc.ty1 = (OPJ_INT32)tcp->tyE; + pi[pino].poc.tx0 = tcp->txS; + pi[pino].poc.ty0 = tcp->tyS; + pi[pino].poc.tx1 = tcp->txE; + pi[pino].poc.ty1 = tcp->tyE; break; } break; @@ -1841,10 +1843,10 @@ void opj_pi_create_encode(opj_pi_iterator_t *pi, default: tcp->tx0_t = tcp->txS; tcp->ty0_t = tcp->tyS; - pi[pino].poc.tx0 = (OPJ_INT32)tcp->tx0_t; - pi[pino].poc.tx1 = (OPJ_INT32)(tcp->tx0_t + tcp->dx - (tcp->tx0_t % tcp->dx)); - pi[pino].poc.ty0 = (OPJ_INT32)tcp->ty0_t; - pi[pino].poc.ty1 = (OPJ_INT32)(tcp->ty0_t + tcp->dy - (tcp->ty0_t % tcp->dy)); + pi[pino].poc.tx0 = tcp->tx0_t; + pi[pino].poc.tx1 = tcp->tx0_t + tcp->dx - (tcp->tx0_t % tcp->dx); + pi[pino].poc.ty0 = tcp->ty0_t; + pi[pino].poc.ty1 = tcp->ty0_t + tcp->dy - (tcp->ty0_t % tcp->dy); tcp->tx0_t = (OPJ_UINT32)pi[pino].poc.tx1; tcp->ty0_t = (OPJ_UINT32)pi[pino].poc.ty1; break; @@ -1876,10 +1878,10 @@ void opj_pi_create_encode(opj_pi_iterator_t *pi, pi[pino].poc.precno1 = tcp->prc_t; break; default: - pi[pino].poc.tx0 = (OPJ_INT32)(tcp->tx0_t - tcp->dx - (tcp->tx0_t % tcp->dx)); - pi[pino].poc.tx1 = (OPJ_INT32)tcp->tx0_t ; - pi[pino].poc.ty0 = (OPJ_INT32)(tcp->ty0_t - tcp->dy - (tcp->ty0_t % tcp->dy)); - pi[pino].poc.ty1 = (OPJ_INT32)tcp->ty0_t ; + pi[pino].poc.tx0 = tcp->tx0_t - tcp->dx - (tcp->tx0_t % tcp->dx); + pi[pino].poc.tx1 = tcp->tx0_t ; + pi[pino].poc.ty0 = tcp->ty0_t - tcp->dy - (tcp->ty0_t % tcp->dy); + pi[pino].poc.ty1 = tcp->ty0_t ; break; } break; @@ -1966,8 +1968,8 @@ void opj_pi_create_encode(opj_pi_iterator_t *pi, if (tcp->ty0_t >= tcp->tyE) { if (opj_pi_check_next_level(i - 1, cp, tileno, pino, prog)) { tcp->ty0_t = tcp->tyS; - pi[pino].poc.ty0 = (OPJ_INT32)tcp->ty0_t; - pi[pino].poc.ty1 = (OPJ_INT32)(tcp->ty0_t + tcp->dy - (tcp->ty0_t % tcp->dy)); + pi[pino].poc.ty0 = tcp->ty0_t; + pi[pino].poc.ty1 = tcp->ty0_t + tcp->dy - (tcp->ty0_t % tcp->dy); tcp->ty0_t = (OPJ_UINT32)pi[pino].poc.ty1; incr_top = 1; resetX = 1; @@ -1976,21 +1978,21 @@ void opj_pi_create_encode(opj_pi_iterator_t *pi, resetX = 0; } } else { - pi[pino].poc.ty0 = (OPJ_INT32)tcp->ty0_t; - pi[pino].poc.ty1 = (OPJ_INT32)(tcp->ty0_t + tcp->dy - (tcp->ty0_t % tcp->dy)); + pi[pino].poc.ty0 = tcp->ty0_t; + pi[pino].poc.ty1 = tcp->ty0_t + tcp->dy - (tcp->ty0_t % tcp->dy); tcp->ty0_t = (OPJ_UINT32)pi[pino].poc.ty1; incr_top = 0; resetX = 1; } if (resetX == 1) { tcp->tx0_t = tcp->txS; - pi[pino].poc.tx0 = (OPJ_INT32)tcp->tx0_t; - pi[pino].poc.tx1 = (OPJ_INT32)(tcp->tx0_t + tcp->dx - (tcp->tx0_t % tcp->dx)); + pi[pino].poc.tx0 = tcp->tx0_t; + pi[pino].poc.tx1 = tcp->tx0_t + tcp->dx - (tcp->tx0_t % tcp->dx); tcp->tx0_t = (OPJ_UINT32)pi[pino].poc.tx1; } } else { - pi[pino].poc.tx0 = (OPJ_INT32)tcp->tx0_t; - pi[pino].poc.tx1 = (OPJ_INT32)(tcp->tx0_t + tcp->dx - (tcp->tx0_t % tcp->dx)); + pi[pino].poc.tx0 = tcp->tx0_t; + pi[pino].poc.tx1 = tcp->tx0_t + tcp->dx - (tcp->tx0_t % tcp->dx); tcp->tx0_t = (OPJ_UINT32)pi[pino].poc.tx1; incr_top = 0; } @@ -2043,7 +2045,7 @@ void opj_pi_update_encoding_parameters(const opj_image_t *p_image, /* encoding parameters to set */ OPJ_UINT32 l_max_res; OPJ_UINT32 l_max_prec; - OPJ_INT32 l_tx0, l_tx1, l_ty0, l_ty1; + OPJ_UINT32 l_tx0, l_tx1, l_ty0, l_ty1; OPJ_UINT32 l_dx_min, l_dy_min; /* pointers */ diff --git a/src/lib/openjp2/tcd.c b/src/lib/openjp2/tcd.c index c5640066..bca82423 100644 --- a/src/lib/openjp2/tcd.c +++ b/src/lib/openjp2/tcd.c @@ -2349,7 +2349,7 @@ static void opj_tcd_code_block_enc_deallocate(opj_tcd_precinct_t * p_precinct) } } -OPJ_SIZE_T opj_tcd_get_encoded_tile_size(opj_tcd_t *p_tcd) +OPJ_SIZE_T opj_tcd_get_encoder_input_buffer_size(opj_tcd_t *p_tcd) { OPJ_UINT32 i; OPJ_SIZE_T l_data_size = 0; @@ -2617,7 +2617,7 @@ OPJ_BOOL opj_tcd_copy_tile_data(opj_tcd_t *p_tcd, OPJ_UINT32 l_size_comp, l_remaining; OPJ_SIZE_T l_nb_elem; - l_data_size = opj_tcd_get_encoded_tile_size(p_tcd); + l_data_size = opj_tcd_get_encoder_input_buffer_size(p_tcd); if (l_data_size != p_src_length) { return OPJ_FALSE; } diff --git a/src/lib/openjp2/tcd.h b/src/lib/openjp2/tcd.h index e3214c1d..67739170 100644 --- a/src/lib/openjp2/tcd.h +++ b/src/lib/openjp2/tcd.h @@ -415,9 +415,11 @@ OPJ_BOOL opj_tcd_update_tile_data(opj_tcd_t *p_tcd, OPJ_UINT32 p_dest_length); /** - * + * Get the size in bytes of the input buffer provided before encoded. + * This must be the size provided to the p_src_length argument of + * opj_tcd_copy_tile_data() */ -OPJ_SIZE_T opj_tcd_get_encoded_tile_size(opj_tcd_t *p_tcd); +OPJ_SIZE_T opj_tcd_get_encoder_input_buffer_size(opj_tcd_t *p_tcd); /** * Initialize the tile coder and may reuse some meory. @@ -433,6 +435,8 @@ OPJ_BOOL opj_tcd_init_encode_tile(opj_tcd_t *p_tcd, /** * Copies tile data from the given memory block onto the system. + * + * p_src_length must be equal to opj_tcd_get_encoder_input_buffer_size() */ OPJ_BOOL opj_tcd_copy_tile_data(opj_tcd_t *p_tcd, OPJ_BYTE * p_src, diff --git a/src/lib/openjp3d/t2.c b/src/lib/openjp3d/t2.c index a31bf83f..9856d1d9 100644 --- a/src/lib/openjp3d/t2.c +++ b/src/lib/openjp3d/t2.c @@ -268,6 +268,7 @@ static int t2_encode_packet(opj_tcd_tile_t * tile, opj_tcp_t * tcp, if (bio_flush(bio)) { + bio_destroy(bio); return -999; /* modified to eliminate longjmp !! */ } diff --git a/src/lib/openjpip/auxtrans_manager.c b/src/lib/openjpip/auxtrans_manager.c index eba9870f..8f364b6d 100644 --- a/src/lib/openjpip/auxtrans_manager.c +++ b/src/lib/openjpip/auxtrans_manager.c @@ -232,6 +232,9 @@ void * aux_streaming(void *arg) } break; } + if (close_socket(connected_socket) != 0) { + perror("close"); + } } opj_free(chunk); diff --git a/src/lib/openjpip/box_manager.c b/src/lib/openjpip/box_manager.c index ce520640..eb6b9d4c 100644 --- a/src/lib/openjpip/box_manager.c +++ b/src/lib/openjpip/box_manager.c @@ -211,6 +211,7 @@ box_param_t * gene_boxbyType(int fd, OPJ_OFF_T offset, OPJ_SIZE_T length, } else { fprintf(FCGI_stderr, "Error: error in gene_boxbyType( %d, %" PRId64 ", %" PRId64 ", %s)\n", fd, offset, length, TBox); + free(data); return NULL; } } diff --git a/src/lib/openjpip/index_manager.c b/src/lib/openjpip/index_manager.c index 2d7d683b..89753752 100644 --- a/src/lib/openjpip/index_manager.c +++ b/src/lib/openjpip/index_manager.c @@ -270,6 +270,7 @@ OPJ_BOOL check_JP2boxidx(boxlist_param_t *toplev_boxlist) ni = fetch_DBox1byte(prxy, pos); if (ni != 1) { fprintf(FCGI_stderr, "Multiple indexes not supported\n"); + opj_free(prxy); return OPJ_FALSE; } pos += 1; @@ -361,6 +362,7 @@ OPJ_BOOL set_cidxdata(box_param_t *cidx_box, index_param_t *jp2idx) if (!search_boxheader("mhix", manf)) { fprintf(FCGI_stderr, "Error: mhix box not present in manfbox\n"); opj_free(jp2idx); + delete_manfbox(&manf); return OPJ_FALSE; } set_mainmhixdata(cidx_box, codestream, jp2idx); @@ -368,6 +370,7 @@ OPJ_BOOL set_cidxdata(box_param_t *cidx_box, index_param_t *jp2idx) if (!search_boxheader("tpix", manf)) { fprintf(FCGI_stderr, "Error: tpix box not present in manfbox\n"); opj_free(jp2idx); + delete_manfbox(&manf); return OPJ_FALSE; } set_tpixdata(cidx_box, jp2idx); @@ -375,6 +378,7 @@ OPJ_BOOL set_cidxdata(box_param_t *cidx_box, index_param_t *jp2idx) if (!search_boxheader("thix", manf)) { fprintf(FCGI_stderr, "Error: thix box not present in manfbox\n"); opj_free(jp2idx); + delete_manfbox(&manf); return OPJ_FALSE; } set_thixdata(cidx_box, jp2idx); @@ -382,6 +386,7 @@ OPJ_BOOL set_cidxdata(box_param_t *cidx_box, index_param_t *jp2idx) if (!search_boxheader("ppix", manf)) { fprintf(FCGI_stderr, "Error: ppix box not present in manfbox\n"); opj_free(jp2idx); + delete_manfbox(&manf); return OPJ_FALSE; } set_ppixdata(cidx_box, jp2idx); @@ -497,6 +502,7 @@ OPJ_BOOL set_tpixdata(box_param_t *cidx_box, index_param_t *jp2idx) if (!(faix_box = gene_boxbyType(tpix_box->fd, get_DBoxoff(tpix_box), get_DBoxlen(tpix_box), "faix"))) { fprintf(FCGI_stderr, "Error: faix box not present in tpix box\n"); + opj_free(tpix_box); return OPJ_FALSE; } @@ -602,11 +608,13 @@ OPJ_BOOL set_ppixdata(box_param_t *cidx_box, index_param_t *jp2idx) if (jp2idx->SIZ.Csiz <= comp_idx) { fprintf(FCGI_stderr, "Error: num of faix boxes is not identical to num of components in ppix box\n"); + delete_manfbox(&manf); return OPJ_FALSE; } if (!(faix_box = gene_boxbyOffset(cidx_box->fd, inbox_offset))) { fprintf(FCGI_stderr, "Error: faix box not present in ppix box\n"); + delete_manfbox(&manf); return OPJ_FALSE; } diff --git a/src/lib/openjpip/jp2k_decoder.c b/src/lib/openjpip/jp2k_decoder.c index e6d1973b..bb3f5eca 100644 --- a/src/lib/openjpip/jp2k_decoder.c +++ b/src/lib/openjpip/jp2k_decoder.c @@ -76,6 +76,8 @@ Byte_t * j2k_to_pnm(const char *fn, ihdrbox_param_t **ihdrbox) /* setup the decoder decoding parameters using user parameters */ if (!opj_setup_decoder(l_codec, ¶meters)) { fprintf(stderr, "ERROR -> j2k_dump: failed to setup the decoder\n"); + opj_stream_destroy(l_stream); + opj_destroy_codec(l_codec); return NULL; } diff --git a/src/lib/openjpip/openjpip.c b/src/lib/openjpip/openjpip.c index 7fa53136..f9eb04fd 100644 --- a/src/lib/openjpip/openjpip.c +++ b/src/lib/openjpip/openjpip.c @@ -357,6 +357,7 @@ OPJ_BOOL OPJ_CALLCONV fread_jpip(const char fname[], jpip_dec_param_t *dec) } if (!(dec->jpiplen = (Byte8_t)get_filesize(infd))) { + close(infd); return OPJ_FALSE; } @@ -365,6 +366,7 @@ OPJ_BOOL OPJ_CALLCONV fread_jpip(const char fname[], jpip_dec_param_t *dec) if (read(infd, dec->jpipstream, dec->jpiplen) != (int)dec->jpiplen) { fprintf(stderr, "file reading error\n"); opj_free(dec->jpipstream); + close(infd); return OPJ_FALSE; } diff --git a/src/lib/openmj2/mj2.c b/src/lib/openmj2/mj2.c index 8e036f18..3cbf83f5 100644 --- a/src/lib/openmj2/mj2.c +++ b/src/lib/openmj2/mj2.c @@ -2822,16 +2822,24 @@ int OPJ_CALLCONV mj2_read_struct(FILE *file, opj_mj2_t *movie) fseek(file, foffset, SEEK_SET); src = (unsigned char*)opj_realloc(src, box.length); + if (src == NULL) { + opj_event_msg(cio->cinfo, EVT_ERROR, "Error reallocation memory\n"); + opj_free(src); + return 1; + } + fsresult = fread(src, box.length, 1, file); if (fsresult != 1) { opj_event_msg(cio->cinfo, EVT_ERROR, "End of file reached while trying to read MOOV box\n"); + opj_free(src); return 1; } cio = opj_cio_open((opj_common_ptr)movie->cinfo, src, box.length); if (mj2_read_moov(movie, &img, cio)) { + opj_free(src); return 1; } diff --git a/src/lib/openmj2/mj2_convert.c b/src/lib/openmj2/mj2_convert.c index 65aa3bc6..0d7816b8 100644 --- a/src/lib/openmj2/mj2_convert.c +++ b/src/lib/openmj2/mj2_convert.c @@ -62,6 +62,7 @@ unsigned int OPJ_CALLCONV yuv_num_frames(mj2_tk_t * tk, char *infile) fprintf(stderr, "YUV does not contains any frame of %d x %d size\n", tk->w, tk->h); + fclose(f); return 0; } fclose(f);