[trunk] Another round of fixes for sign conversion warnings.
[openjpeg.git] / src / bin / jp2 / opj_decompress.c
index ac5e79c34a0cb5b0d886d35c73a89e162cf120ce..b19e01a98dd201051ffbd3f29022ae7a4541cbec 100644 (file)
@@ -6,6 +6,8 @@
  * Copyright (c) 2003-2007, Francois-Olivier Devaux and Antonin Descampe
  * Copyright (c) 2005, Herve Drolon, FreeImage Team
  * Copyright (c) 2006-2007, Parvatha Elangovan
+ * Copyright (c) 2008;2011-2012, Centre National d'Etudes Spatiales (CNES), France 
+ * Copyright (c) 2012, CS Systemes d'Information, France
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
@@ -29,7 +31,7 @@
  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  * POSSIBILITY OF SUCH DAMAGE.
  */
-#include "opj_config.h"
+#include "opj_apps_config.h"
 
 #include <stdio.h>
 #include <string.h>
 #include "convert.h"
 #include "index.h"
 
-#ifdef HAVE_LIBLCMS2
+#ifdef OPJ_HAVE_LIBLCMS2
 #include <lcms2.h>
 #endif
-#ifdef HAVE_LIBLCMS1
+#ifdef OPJ_HAVE_LIBLCMS1
 #include <lcms.h>
 #endif
 #include "color.h"
@@ -269,7 +271,7 @@ static int infile_format(const char *fname)
        const char *s, *magic_s;
        int ext_format, magic_format;
        unsigned char buf[12];
-       unsigned int l_nb_read;
+       OPJ_SIZE_T l_nb_read;
 
        reader = fopen(fname, "rb");
 
@@ -445,7 +447,7 @@ int parse_cmdline_decoder(int argc, char **argv, opj_dparameters_t *parameters,i
 
                        case 'r':               /* reduce option */
                        {
-                               sscanf(opj_optarg, "%d", &parameters->cp_reduce);
+                               sscanf(opj_optarg, "%ud", &parameters->cp_reduce);
                        }
                        break;
                        
@@ -454,7 +456,7 @@ int parse_cmdline_decoder(int argc, char **argv, opj_dparameters_t *parameters,i
 
                        case 'l':               /* layering option */
                        {
-                               sscanf(opj_optarg, "%d", &parameters->cp_layer);
+                               sscanf(opj_optarg, "%ud", &parameters->cp_layer);
                        }
                        break;
                        
@@ -479,7 +481,7 @@ int parse_cmdline_decoder(int argc, char **argv, opj_dparameters_t *parameters,i
                        case 'd':               /* Input decode ROI */
                        {
                                int size_optarg = (int)strlen(opj_optarg) + 1;
-                               char *ROI_values = (char*) malloc(size_optarg);
+                               char *ROI_values = (char*) malloc((size_t)size_optarg);
                                ROI_values[0] = '\0';
                                strncpy(ROI_values, opj_optarg, strlen(opj_optarg));
                                ROI_values[strlen(opj_optarg)] = '\0';
@@ -494,7 +496,7 @@ int parse_cmdline_decoder(int argc, char **argv, opj_dparameters_t *parameters,i
 
                        case 't':               /* Input tile index */
                        {
-                               sscanf(opj_optarg, "%d", &parameters->tile_index);
+                               sscanf(opj_optarg, "%ud", &parameters->tile_index);
                                parameters->nb_tile_to_decode = 1;
                        }
                        break;
@@ -637,8 +639,8 @@ int parse_DA_values( char* inArg, unsigned int *DA_x0, unsigned int *DA_y0, unsi
                return EXIT_FAILURE;
        }
        else{
-               *DA_x0 = values[0]; *DA_y0 = values[1];
-               *DA_x1 = values[2]; *DA_y1 = values[3];
+               *DA_x0 = (OPJ_UINT32)values[0]; *DA_y0 = (OPJ_UINT32)values[1];
+               *DA_x1 = (OPJ_UINT32)values[2]; *DA_y1 = (OPJ_UINT32)values[3];
                return EXIT_SUCCESS;
        }
 }
@@ -674,7 +676,6 @@ static void info_callback(const char *msg, void *client_data) {
 /* -------------------------------------------------------------------------- */
 int main(int argc, char **argv)
 {
-       FILE *fsrc = NULL;
 
        opj_dparameters_t parameters;                   /* decompression parameters */
        opj_image_t* image = NULL;
@@ -709,8 +710,8 @@ int main(int argc, char **argv)
 
                dirptr=(dircnt_t*)malloc(sizeof(dircnt_t));
                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*));
+                       dirptr->filename_buf = (char*)malloc((size_t)num_images*OPJ_PATH_LEN*sizeof(char));     /* Stores at max 10 image file names*/
+                       dirptr->filename = (char**) malloc((size_t)num_images*sizeof(char*));
 
                        if(!dirptr->filename_buf){
                                return EXIT_FAILURE;
@@ -744,16 +745,10 @@ int main(int argc, char **argv)
 
                /* read the input file and put it in memory */
                /* ---------------------------------------- */
-               fsrc = fopen(parameters.infile, "rb");
-               if (!fsrc) {
-                       fprintf(stderr, "ERROR -> failed to open %s for reading\n", parameters.infile);
-                       return EXIT_FAILURE;
-               }
 
-               l_stream = opj_stream_create_default_file_stream(fsrc,1);
+               l_stream = opj_stream_create_default_file_stream_v3(parameters.infile,1);
                if (!l_stream){
-                       fclose(fsrc);
-                       fprintf(stderr, "ERROR -> failed to create the stream from the file\n");
+                       fprintf(stderr, "ERROR -> failed to create the stream from the file %s\n", parameters.infile);
                        return EXIT_FAILURE;
                }
 
@@ -764,24 +759,24 @@ int main(int argc, char **argv)
                        case J2K_CFMT:  /* JPEG-2000 codestream */
                        {
                                /* Get a decoder handle */
-                               l_codec = opj_create_decompress(CODEC_J2K);
+                               l_codec = opj_create_decompress(OPJ_CODEC_J2K);
                                break;
                        }
                        case JP2_CFMT:  /* JPEG 2000 compressed image data */
                        {
                                /* Get a decoder handle */
-                               l_codec = opj_create_decompress(CODEC_JP2);
+                               l_codec = opj_create_decompress(OPJ_CODEC_JP2);
                                break;
                        }
                        case JPT_CFMT:  /* JPEG 2000, JPIP */
                        {
                                /* Get a decoder handle */
-                               l_codec = opj_create_decompress(CODEC_JPT);
+                               l_codec = opj_create_decompress(OPJ_CODEC_JPT);
                                break;
                        }
                        default:
                                fprintf(stderr, "skipping file..\n");
-                               opj_stream_destroy(l_stream);
+                               opj_stream_destroy_v3(l_stream);
                                continue;
                }
 
@@ -793,8 +788,7 @@ int main(int argc, char **argv)
                /* Setup the decoder decoding parameters using user parameters */
                if ( !opj_setup_decoder(l_codec, &parameters) ){
                        fprintf(stderr, "ERROR -> j2k_dump: failed to setup the decoder\n");
-                       opj_stream_destroy(l_stream);
-                       fclose(fsrc);
+                       opj_stream_destroy_v3(l_stream);
                        opj_destroy_codec(l_codec);
                        return EXIT_FAILURE;
                }
@@ -803,8 +797,7 @@ int main(int argc, char **argv)
                /* Read the main header of the codestream and if necessary the JP2 boxes*/
                if(! opj_read_header(l_stream, l_codec, &image)){
                        fprintf(stderr, "ERROR -> opj_decompress: failed to read the header\n");
-                       opj_stream_destroy(l_stream);
-                       fclose(fsrc);
+                       opj_stream_destroy_v3(l_stream);
                        opj_destroy_codec(l_codec);
                        opj_image_destroy(image);
                        return EXIT_FAILURE;
@@ -812,13 +805,12 @@ int main(int argc, char **argv)
 
                if (!parameters.nb_tile_to_decode) {
                        /* Optional if you want decode the entire image */
-                       if (!opj_set_decode_area(l_codec, image, parameters.DA_x0,
-                                       parameters.DA_y0, parameters.DA_x1, parameters.DA_y1)){
+                       if (!opj_set_decode_area(l_codec, image, (OPJ_INT32)parameters.DA_x0,
+                                       (OPJ_INT32)parameters.DA_y0, (OPJ_INT32)parameters.DA_x1, (OPJ_INT32)parameters.DA_y1)){
                                fprintf(stderr, "ERROR -> opj_decompress: failed to set the decoded area\n");
-                               opj_stream_destroy(l_stream);
+                               opj_stream_destroy_v3(l_stream);
                                opj_destroy_codec(l_codec);
                                opj_image_destroy(image);
-                               fclose(fsrc);
                                return EXIT_FAILURE;
                        }
 
@@ -826,9 +818,8 @@ int main(int argc, char **argv)
                        if (!(opj_decode(l_codec, l_stream, image) && opj_end_decompress(l_codec,       l_stream))) {
                                fprintf(stderr,"ERROR -> opj_decompress: failed to decode image!\n");
                                opj_destroy_codec(l_codec);
-                               opj_stream_destroy(l_stream);
+                               opj_stream_destroy_v3(l_stream);
                                opj_image_destroy(image);
-                               fclose(fsrc);
                                return EXIT_FAILURE;
                        }
                }
@@ -838,33 +829,37 @@ int main(int argc, char **argv)
                        /*if (!opj_set_decoded_resolution_factor(l_codec, 5)) {
                                fprintf(stderr, "ERROR -> opj_decompress: failed to set the resolution factor tile!\n");
                                opj_destroy_codec(l_codec);
-                               opj_stream_destroy(l_stream);
+                               opj_stream_destroy_v3(l_stream);
                                opj_image_destroy(image);
-                               fclose(fsrc);
                                return EXIT_FAILURE;
                        }*/
 
                        if (!opj_get_decoded_tile(l_codec, l_stream, image, parameters.tile_index)) {
                                fprintf(stderr, "ERROR -> opj_decompress: failed to decode tile!\n");
                                opj_destroy_codec(l_codec);
-                               opj_stream_destroy(l_stream);
+                               opj_stream_destroy_v3(l_stream);
                                opj_image_destroy(image);
-                               fclose(fsrc);
                                return EXIT_FAILURE;
                        }
                        fprintf(stdout, "tile %d is decoded!\n\n", parameters.tile_index);
                }
 
                /* Close the byte stream */
-               opj_stream_destroy(l_stream);
-               fclose(fsrc);
+               opj_stream_destroy_v3(l_stream);
 
-               if(image->color_space == CLRSPC_SYCC){
+               if(image->color_space == OPJ_CLRSPC_SYCC){
                        color_sycc_to_rgb(image); /* FIXME */
                }
+               
+               if( image->color_space != OPJ_CLRSPC_SYCC 
+                       && image->numcomps == 3 && image->comps[0].dx == image->comps[0].dy
+                       && image->comps[1].dx != 1 )
+                       image->color_space = OPJ_CLRSPC_SYCC;
+               else if (image->numcomps <= 2)
+                       image->color_space = OPJ_CLRSPC_GRAY;
 
                if(image->icc_profile_buf) {
-#if defined(HAVE_LIBLCMS1) || defined(HAVE_LIBLCMS2)
+#if defined(OPJ_HAVE_LIBLCMS1) || defined(OPJ_HAVE_LIBLCMS2)
                        color_apply_icc_profile(image); /* FIXME */
 #endif
                        free(image->icc_profile_buf);
@@ -900,7 +895,7 @@ int main(int argc, char **argv)
                                fprintf(stdout,"Generated Outfile %s\n",parameters.outfile);
                        }
                        break;
-#ifdef HAVE_LIBTIFF
+#ifdef OPJ_HAVE_LIBTIFF
                case TIF_DFMT:                  /* TIFF */
                        if(imagetotif(image, parameters.outfile)){
                                fprintf(stdout,"Outfile %s not generated\n",parameters.outfile);
@@ -909,7 +904,7 @@ int main(int argc, char **argv)
                                fprintf(stdout,"Generated Outfile %s\n",parameters.outfile);
                        }
                        break;
-#endif /* HAVE_LIBTIFF */
+#endif /* OPJ_HAVE_LIBTIFF */
                case RAW_DFMT:                  /* RAW */
                        if(imagetoraw(image, parameters.outfile)){
                                fprintf(stdout,"Error generating raw file. Outfile %s not generated\n",parameters.outfile);
@@ -936,7 +931,7 @@ int main(int argc, char **argv)
                                fprintf(stdout,"Successfully generated Outfile %s\n",parameters.outfile);
                        }
                        break;
-#ifdef HAVE_LIBPNG
+#ifdef OPJ_HAVE_LIBPNG
                case PNG_DFMT:                  /* PNG */
                        if(imagetopng(image, parameters.outfile)){
                                fprintf(stdout,"Error generating png file. Outfile %s not generated\n",parameters.outfile);
@@ -945,9 +940,9 @@ int main(int argc, char **argv)
                                fprintf(stdout,"Successfully generated Outfile %s\n",parameters.outfile);
                        }
                        break;
-#endif /* HAVE_LIBPNG */
+#endif /* OPJ_HAVE_LIBPNG */
 /* Can happen if output file is TIFF or PNG
- * and HAVE_LIBTIF or HAVE_LIBPNG is undefined
+ * and OPJ_HAVE_LIBTIF or OPJ_HAVE_LIBPNG is undefined
 */
                        default:
                                fprintf(stderr,"Outfile %s not generated\n",parameters.outfile);