From f0629cb1c4c485d905936413556deb2872ac51e5 Mon Sep 17 00:00:00 2001 From: Even Rouault Date: Mon, 7 Jun 2021 13:28:26 +0200 Subject: [PATCH] Fix various compiler warnings --- src/bin/jp2/convert.c | 4 +- src/bin/jp2/convertpng.c | 332 +++++++++++++++++++---------------- src/bin/jp2/opj_compress.c | 22 ++- src/bin/jp2/opj_decompress.c | 20 ++- tests/compare_images.c | 4 +- 5 files changed, 217 insertions(+), 165 deletions(-) diff --git a/src/bin/jp2/convert.c b/src/bin/jp2/convert.c index 23f820c0..7610e245 100644 --- a/src/bin/jp2/convert.c +++ b/src/bin/jp2/convert.c @@ -1384,7 +1384,7 @@ int imagetopgx(opj_image_t * image, const char *outfile) goto fin; } } - strncpy(name, outfile, dotpos); + memcpy(name, outfile, dotpos); sprintf(name + dotpos, "_%u.pgx", compno); fdest = fopen(name, "wb"); /* don't need name anymore */ @@ -2228,7 +2228,7 @@ int imagetopnm(opj_image_t * image, const char *outfile, int force_split) const size_t olen = strlen(outfile); const size_t dotpos = olen - 4; - strncpy(destname, outfile, dotpos); + memcpy(destname, outfile, dotpos); sprintf(destname + dotpos, "_%u.pgm", compno); } else { sprintf(destname, "%s", outfile); diff --git a/src/bin/jp2/convertpng.c b/src/bin/jp2/convertpng.c index 00f596e2..cd074001 100644 --- a/src/bin/jp2/convertpng.c +++ b/src/bin/jp2/convertpng.c @@ -65,26 +65,193 @@ static void convert_16u32s_C1R(const OPJ_BYTE* pSrc, OPJ_INT32* pDst, } } +static opj_image_t * pngtoimage_internal(opj_cparameters_t * params, + FILE *reader, + png_structp png, + png_infop info, + png_uint_32* pheight, + OPJ_BYTE*** prows, + OPJ_INT32** prow32s) +{ + *pheight = 0; + *prows = NULL; + *prow32s = NULL; + + if (setjmp(png_jmpbuf(png))) { + return NULL; + } + + { + opj_image_t *image = NULL; + opj_image_cmptparm_t cmptparm[4]; + OPJ_UINT32 nr_comp; + convert_XXx32s_C1R cvtXXTo32s = NULL; + convert_32s_CXPX cvtCxToPx = NULL; + OPJ_INT32* planes[4]; + double gamma; + int bit_depth, interlace_type, compression_type, filter_type; + OPJ_UINT32 i; + png_uint_32 width, height = 0U; + int color_type; + OPJ_BYTE** rows = NULL; + OPJ_INT32* row32s = NULL; + + png_init_io(png, reader); + png_set_sig_bytes(png, MAGIC_SIZE); + + png_read_info(png, info); + + if (png_get_IHDR(png, info, &width, &height, + &bit_depth, &color_type, &interlace_type, + &compression_type, &filter_type) == 0) { + return image; + } + *pheight = height; + + /* png_set_expand(): + * expand paletted images to RGB, expand grayscale images of + * less than 8-bit depth to 8-bit depth, and expand tRNS chunks + * to alpha channels. + */ + if (color_type == PNG_COLOR_TYPE_PALETTE) { + png_set_expand(png); + } + + if (png_get_valid(png, info, PNG_INFO_tRNS)) { + png_set_expand(png); + } + /* We might wan't to expand background */ + /* + if(png_get_valid(png, info, PNG_INFO_bKGD)) { + png_color_16p bgnd; + png_get_bKGD(png, info, &bgnd); + png_set_background(png, bgnd, PNG_BACKGROUND_GAMMA_FILE, 1, 1.0); + } + */ + + if (!png_get_gAMA(png, info, &gamma)) { + gamma = 1.0; + } + + /* we're not displaying but converting, screen gamma == 1.0 */ + png_set_gamma(png, 1.0, gamma); + + png_read_update_info(png, info); + + color_type = png_get_color_type(png, info); + + switch (color_type) { + case PNG_COLOR_TYPE_GRAY: + nr_comp = 1; + break; + case PNG_COLOR_TYPE_GRAY_ALPHA: + nr_comp = 2; + break; + case PNG_COLOR_TYPE_RGB: + nr_comp = 3; + break; + case PNG_COLOR_TYPE_RGB_ALPHA: + nr_comp = 4; + break; + default: + fprintf(stderr, "pngtoimage: colortype %d is not supported\n", color_type); + return image; + } + cvtCxToPx = convert_32s_CXPX_LUT[nr_comp]; + bit_depth = png_get_bit_depth(png, info); + + switch (bit_depth) { + case 1: + case 2: + case 4: + case 8: + cvtXXTo32s = convert_XXu32s_C1R_LUT[bit_depth]; + break; + case 16: /* 16 bpp is specific to PNG */ + cvtXXTo32s = convert_16u32s_C1R; + break; + default: + fprintf(stderr, "pngtoimage: bit depth %d is not supported\n", bit_depth); + return image; + } + + rows = (OPJ_BYTE**)calloc(height + 1, sizeof(OPJ_BYTE*)); + if (rows == NULL) { + fprintf(stderr, "pngtoimage: memory out\n"); + return image; + } + *prows = rows; + for (i = 0; i < height; ++i) { + rows[i] = (OPJ_BYTE*)malloc(png_get_rowbytes(png, info)); + if (rows[i] == NULL) { + fprintf(stderr, "pngtoimage: memory out\n"); + return image; + } + } + png_read_image(png, rows); + + /* Create image */ + memset(cmptparm, 0, sizeof(cmptparm)); + for (i = 0; i < nr_comp; ++i) { + cmptparm[i].prec = (OPJ_UINT32)bit_depth; + /* bits_per_pixel: 8 or 16 */ + cmptparm[i].bpp = (OPJ_UINT32)bit_depth; + cmptparm[i].sgnd = 0; + cmptparm[i].dx = (OPJ_UINT32)params->subsampling_dx; + cmptparm[i].dy = (OPJ_UINT32)params->subsampling_dy; + cmptparm[i].w = (OPJ_UINT32)width; + cmptparm[i].h = (OPJ_UINT32)height; + } + + image = opj_image_create(nr_comp, &cmptparm[0], + (nr_comp > 2U) ? OPJ_CLRSPC_SRGB : OPJ_CLRSPC_GRAY); + if (image == NULL) { + return image; + } + image->x0 = (OPJ_UINT32)params->image_offset_x0; + image->y0 = (OPJ_UINT32)params->image_offset_y0; + image->x1 = (OPJ_UINT32)(image->x0 + (width - 1) * (OPJ_UINT32) + params->subsampling_dx + 1); + image->y1 = (OPJ_UINT32)(image->y0 + (height - 1) * (OPJ_UINT32) + params->subsampling_dy + 1); + + row32s = (OPJ_INT32 *)malloc((size_t)width * nr_comp * sizeof(OPJ_INT32)); + if (row32s == NULL) { + return image; + } + *prow32s = row32s; + + /* Set alpha channel */ + image->comps[nr_comp - 1U].alpha = 1U - (nr_comp & 1U); + + for (i = 0; i < nr_comp; i++) { + planes[i] = image->comps[i].data; + } + + for (i = 0; i < height; ++i) { + cvtXXTo32s(rows[i], row32s, (OPJ_SIZE_T)width * nr_comp); + cvtCxToPx(row32s, planes, width); + planes[0] += width; + planes[1] += width; + planes[2] += width; + planes[3] += width; + } + + return image; + } +} + opj_image_t *pngtoimage(const char *read_idf, opj_cparameters_t * params) { png_structp png = NULL; png_infop info = NULL; - double gamma; - int bit_depth, interlace_type, compression_type, filter_type; OPJ_UINT32 i; - png_uint_32 width, height = 0U; - int color_type; + png_uint_32 height = 0U; FILE *reader = NULL; OPJ_BYTE** rows = NULL; OPJ_INT32* row32s = NULL; - /* j2k: */ - opj_image_t *image = NULL; - opj_image_cmptparm_t cmptparm[4]; - OPJ_UINT32 nr_comp; OPJ_BYTE sigbuf[8]; - convert_XXx32s_C1R cvtXXTo32s = NULL; - convert_32s_CXPX cvtCxToPx = NULL; - OPJ_INT32* planes[4]; + opj_image_t *image = NULL; if ((reader = fopen(read_idf, "rb")) == NULL) { fprintf(stderr, "pngtoimage: can not open %s\n", read_idf); @@ -105,148 +272,7 @@ opj_image_t *pngtoimage(const char *read_idf, opj_cparameters_t * params) goto fin; } - if (setjmp(png_jmpbuf(png))) { - goto fin; - } - - png_init_io(png, reader); - png_set_sig_bytes(png, MAGIC_SIZE); - - png_read_info(png, info); - - if (png_get_IHDR(png, info, &width, &height, - &bit_depth, &color_type, &interlace_type, - &compression_type, &filter_type) == 0) { - goto fin; - } - - /* png_set_expand(): - * expand paletted images to RGB, expand grayscale images of - * less than 8-bit depth to 8-bit depth, and expand tRNS chunks - * to alpha channels. - */ - if (color_type == PNG_COLOR_TYPE_PALETTE) { - png_set_expand(png); - } - - if (png_get_valid(png, info, PNG_INFO_tRNS)) { - png_set_expand(png); - } - /* We might wan't to expand background */ - /* - if(png_get_valid(png, info, PNG_INFO_bKGD)) { - png_color_16p bgnd; - png_get_bKGD(png, info, &bgnd); - png_set_background(png, bgnd, PNG_BACKGROUND_GAMMA_FILE, 1, 1.0); - } - */ - - if (!png_get_gAMA(png, info, &gamma)) { - gamma = 1.0; - } - - /* we're not displaying but converting, screen gamma == 1.0 */ - png_set_gamma(png, 1.0, gamma); - - png_read_update_info(png, info); - - color_type = png_get_color_type(png, info); - - switch (color_type) { - case PNG_COLOR_TYPE_GRAY: - nr_comp = 1; - break; - case PNG_COLOR_TYPE_GRAY_ALPHA: - nr_comp = 2; - break; - case PNG_COLOR_TYPE_RGB: - nr_comp = 3; - break; - case PNG_COLOR_TYPE_RGB_ALPHA: - nr_comp = 4; - break; - default: - fprintf(stderr, "pngtoimage: colortype %d is not supported\n", color_type); - goto fin; - } - cvtCxToPx = convert_32s_CXPX_LUT[nr_comp]; - bit_depth = png_get_bit_depth(png, info); - - switch (bit_depth) { - case 1: - case 2: - case 4: - case 8: - cvtXXTo32s = convert_XXu32s_C1R_LUT[bit_depth]; - break; - case 16: /* 16 bpp is specific to PNG */ - cvtXXTo32s = convert_16u32s_C1R; - break; - default: - fprintf(stderr, "pngtoimage: bit depth %d is not supported\n", bit_depth); - goto fin; - } - - - rows = (OPJ_BYTE**)calloc(height + 1, sizeof(OPJ_BYTE*)); - if (rows == NULL) { - fprintf(stderr, "pngtoimage: memory out\n"); - goto fin; - } - for (i = 0; i < height; ++i) { - rows[i] = (OPJ_BYTE*)malloc(png_get_rowbytes(png, info)); - if (rows[i] == NULL) { - fprintf(stderr, "pngtoimage: memory out\n"); - goto fin; - } - } - png_read_image(png, rows); - - /* Create image */ - memset(cmptparm, 0, sizeof(cmptparm)); - for (i = 0; i < nr_comp; ++i) { - cmptparm[i].prec = (OPJ_UINT32)bit_depth; - /* bits_per_pixel: 8 or 16 */ - cmptparm[i].bpp = (OPJ_UINT32)bit_depth; - cmptparm[i].sgnd = 0; - cmptparm[i].dx = (OPJ_UINT32)params->subsampling_dx; - cmptparm[i].dy = (OPJ_UINT32)params->subsampling_dy; - cmptparm[i].w = (OPJ_UINT32)width; - cmptparm[i].h = (OPJ_UINT32)height; - } - - image = opj_image_create(nr_comp, &cmptparm[0], - (nr_comp > 2U) ? OPJ_CLRSPC_SRGB : OPJ_CLRSPC_GRAY); - if (image == NULL) { - goto fin; - } - image->x0 = (OPJ_UINT32)params->image_offset_x0; - image->y0 = (OPJ_UINT32)params->image_offset_y0; - image->x1 = (OPJ_UINT32)(image->x0 + (width - 1) * (OPJ_UINT32) - params->subsampling_dx + 1); - image->y1 = (OPJ_UINT32)(image->y0 + (height - 1) * (OPJ_UINT32) - params->subsampling_dy + 1); - - row32s = (OPJ_INT32 *)malloc((size_t)width * nr_comp * sizeof(OPJ_INT32)); - if (row32s == NULL) { - goto fin; - } - - /* Set alpha channel */ - image->comps[nr_comp - 1U].alpha = 1U - (nr_comp & 1U); - - for (i = 0; i < nr_comp; i++) { - planes[i] = image->comps[i].data; - } - - for (i = 0; i < height; ++i) { - cvtXXTo32s(rows[i], row32s, (OPJ_SIZE_T)width * nr_comp); - cvtCxToPx(row32s, planes, width); - planes[0] += width; - planes[1] += width; - planes[2] += width; - planes[3] += width; - } + image = pngtoimage_internal(params, reader, png, info, &height, &rows, &row32s); fin: if (rows) { for (i = 0; i < height; ++i) diff --git a/src/bin/jp2/opj_compress.c b/src/bin/jp2/opj_compress.c index cc274a07..76ba8ec4 100644 --- a/src/bin/jp2/opj_compress.c +++ b/src/bin/jp2/opj_compress.c @@ -553,7 +553,13 @@ static char get_next_file(int imageno, dircnt_t *dirptr, img_fol_t *img_fol, if (parameters->decod_format == -1) { return 1; } - sprintf(infilename, "%s/%s", img_fol->imgdirpath, image_filename); + if (strlen(img_fol->imgdirpath) + 1 + strlen(image_filename) + 1 > sizeof( + infilename)) { + return 1; + } + strcpy(infilename, img_fol->imgdirpath); + strcat(infilename, "/"); + strcat(infilename, image_filename); if (opj_strcpy_s(parameters->infile, sizeof(parameters->infile), infilename) != 0) { return 1; @@ -566,8 +572,15 @@ static char get_next_file(int imageno, dircnt_t *dirptr, img_fol_t *img_fol, sprintf(temp1, ".%s", temp_p); } if (img_fol->set_out_format == 1) { - sprintf(outfilename, "%s/%s.%s", img_fol->imgdirpath, temp_ofname, - img_fol->out_format); + if (strlen(img_fol->imgdirpath) + 1 + strlen(temp_ofname) + 1 + strlen( + img_fol->out_format) + 1 > sizeof(outfilename)) { + return 1; + } + strcpy(outfilename, img_fol->imgdirpath); + strcat(outfilename, "/"); + strcat(outfilename, temp_ofname); + strcat(outfilename, "."); + strcat(outfilename, img_fol->out_format); if (opj_strcpy_s(parameters->outfile, sizeof(parameters->outfile), outfilename) != 0) { return 1; @@ -958,9 +971,10 @@ static int parse_cmdline_encoder(int argc, char **argv, /* ----------------------------------------------------- */ case 'p': { /* progression order */ - char progression[4]; + char progression[5]; strncpy(progression, opj_optarg, 4); + progression[4] = 0; parameters->prog_order = give_progression(progression); if (parameters->prog_order == -1) { fprintf(stderr, "Unrecognized progression order " diff --git a/src/bin/jp2/opj_decompress.c b/src/bin/jp2/opj_decompress.c index 2333f999..0e028735 100644 --- a/src/bin/jp2/opj_decompress.c +++ b/src/bin/jp2/opj_decompress.c @@ -461,8 +461,13 @@ char get_next_file(int imageno, dircnt_t *dirptr, img_fol_t *img_fol, strcpy(image_filename, dirptr->filename[imageno]); fprintf(stderr, "File Number %d \"%s\"\n", imageno, image_filename); - sprintf(infilename, "%s%s%s", img_fol->imgdirpath, path_separator, - image_filename); + if (strlen(img_fol->imgdirpath) + strlen(path_separator) + strlen( + image_filename) + 1 > sizeof(infilename)) { + return 1; + } + strcpy(infilename, img_fol->imgdirpath); + strcat(infilename, path_separator); + strcat(infilename, image_filename); parameters->decod_format = infile_format(infilename); if (parameters->decod_format == -1) { return 1; @@ -479,8 +484,15 @@ char get_next_file(int imageno, dircnt_t *dirptr, img_fol_t *img_fol, sprintf(temp1, ".%s", temp_p); } if (img_fol->set_out_format == 1) { - sprintf(outfilename, "%s/%s.%s", img_fol->imgdirpath, temp_ofname, - img_fol->out_format); + if (strlen(img_fol->imgdirpath) + 1 + strlen(temp_ofname) + 1 + strlen( + img_fol->out_format) + 1 > sizeof(outfilename)) { + return 1; + } + strcpy(outfilename, img_fol->imgdirpath); + strcat(outfilename, "/"); + strcat(outfilename, temp_ofname); + strcat(outfilename, "."); + strcat(outfilename, img_fol->out_format); if (opj_strcpy_s(parameters->outfile, sizeof(parameters->outfile), outfilename) != 0) { return 1; diff --git a/tests/compare_images.c b/tests/compare_images.c index c2d95e5b..6b34be34 100644 --- a/tests/compare_images.c +++ b/tests/compare_images.c @@ -142,9 +142,9 @@ static char* createMultiComponentsFilename(const char* inFilename, return outFilename; } - outFilename = (char*)malloc((posToken + 7) * sizeof(char)); /*6*/ + outFilename = (char*)malloc(posToken + 32); - strncpy(outFilename, inFilename, posToken); + memcpy(outFilename, inFilename, posToken); outFilename[posToken] = '\0'; strcat(outFilename, separator); sprintf(s, "%i", indexF);