Fix various compiler warnings 1358/head
authorEven Rouault <even.rouault@spatialys.com>
Mon, 7 Jun 2021 11:28:26 +0000 (13:28 +0200)
committerEven Rouault <even.rouault@spatialys.com>
Mon, 7 Jun 2021 11:28:26 +0000 (13:28 +0200)
src/bin/jp2/convert.c
src/bin/jp2/convertpng.c
src/bin/jp2/opj_compress.c
src/bin/jp2/opj_decompress.c
tests/compare_images.c

index 23f820c0d707ab356247c8f5f843867f5582bfb2..7610e245bfbf35839a6ff518d8ab5c76f7d4f6b3 100644 (file)
@@ -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);
index 00f596e2745a1729737fd05914f82cb0056bb561..cd07400103771ac50877d270ca6395dc46b6d929 100644 (file)
@@ -65,188 +65,214 @@ static void convert_16u32s_C1R(const OPJ_BYTE* pSrc, OPJ_INT32* pDst,
     }
 }
 
-opj_image_t *pngtoimage(const char *read_idf, opj_cparameters_t * params)
+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)
 {
-    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;
-    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];
+    *pheight = 0;
+    *prows = NULL;
+    *prow32s = NULL;
 
-    if ((reader = fopen(read_idf, "rb")) == NULL) {
-        fprintf(stderr, "pngtoimage: can not open %s\n", read_idf);
+    if (setjmp(png_jmpbuf(png))) {
         return NULL;
     }
 
-    if (fread(sigbuf, 1, MAGIC_SIZE, reader) != MAGIC_SIZE
-            || memcmp(sigbuf, PNG_MAGIC, MAGIC_SIZE) != 0) {
-        fprintf(stderr, "pngtoimage: %s is no valid PNG file\n", read_idf);
-        goto fin;
-    }
-
-    if ((png = png_create_read_struct(PNG_LIBPNG_VER_STRING,
-                                      NULL, NULL, NULL)) == NULL) {
-        goto fin;
-    }
-    if ((info = png_create_info_struct(png)) == NULL) {
-        goto fin;
-    }
+    {
+        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 (setjmp(png_jmpbuf(png))) {
-        goto fin;
-    }
+        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);
+        }
+        */
 
-    png_init_io(png, reader);
-    png_set_sig_bytes(png, MAGIC_SIZE);
+        if (!png_get_gAMA(png, info, &gamma)) {
+            gamma = 1.0;
+        }
 
-    png_read_info(png, info);
+        /* we're not displaying but converting, screen gamma == 1.0 */
+        png_set_gamma(png, 1.0, gamma);
 
-    if (png_get_IHDR(png, info, &width, &height,
-                     &bit_depth, &color_type, &interlace_type,
-                     &compression_type, &filter_type) == 0) {
-        goto fin;
-    }
+        png_read_update_info(png, info);
 
-    /* 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);
-    }
+        color_type = png_get_color_type(png, info);
 
-    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);
-    }
-    */
+        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);
 
-    if (!png_get_gAMA(png, info, &gamma)) {
-        gamma = 1.0;
-    }
+        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;
+        }
 
-    /* we're not displaying but converting, screen gamma == 1.0 */
-    png_set_gamma(png, 1.0, gamma);
+        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);
 
-    png_read_update_info(png, info);
+        /* 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;
+        }
 
-    color_type = png_get_color_type(png, info);
+        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;
 
-    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;
-    }
+        /* 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;
+        }
 
-    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;
+        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;
     }
-    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;
+opj_image_t *pngtoimage(const char *read_idf, opj_cparameters_t * params)
+{
+    png_structp  png = NULL;
+    png_infop    info = NULL;
+    OPJ_UINT32 i;
+    png_uint_32  height = 0U;
+    FILE *reader = NULL;
+    OPJ_BYTE** rows = NULL;
+    OPJ_INT32* row32s = NULL;
+    OPJ_BYTE sigbuf[8];
+    opj_image_t *image = NULL;
+
+    if ((reader = fopen(read_idf, "rb")) == NULL) {
+        fprintf(stderr, "pngtoimage: can not open %s\n", read_idf);
+        return NULL;
     }
 
-    image = opj_image_create(nr_comp, &cmptparm[0],
-                             (nr_comp > 2U) ? OPJ_CLRSPC_SRGB : OPJ_CLRSPC_GRAY);
-    if (image == NULL) {
+    if (fread(sigbuf, 1, MAGIC_SIZE, reader) != MAGIC_SIZE
+            || memcmp(sigbuf, PNG_MAGIC, MAGIC_SIZE) != 0) {
+        fprintf(stderr, "pngtoimage: %s is no valid PNG file\n", read_idf);
         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) {
+
+    if ((png = png_create_read_struct(PNG_LIBPNG_VER_STRING,
+                                      NULL, NULL, NULL)) == 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;
+    if ((info = png_create_info_struct(png)) == NULL) {
+        goto fin;
     }
 
-    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)
index cc274a071d90a641dece586c960d9a66df9e1d62..76ba8ec4aef27bf31110e7b7835d5c38a49fa3d5 100644 (file)
@@ -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 "
index 2333f99962ccdf03cf1e9479104bb63bb4d47375..0e0287350ce4ccf0cd170c5b95bdaf0ffe07e770 100644 (file)
@@ -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;
index c2d95e5bab32f889c5620d3c0b8712c64ba4ba06..6b34be346e863d809e6fac2b4564479f496ac7bc 100644 (file)
@@ -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);