[2.0] Backport all changes from trunk
authorMathieu Malaterre <mathieu.malaterre@gmail.com>
Tue, 25 Mar 2014 15:16:07 +0000 (15:16 +0000)
committerMathieu Malaterre <mathieu.malaterre@gmail.com>
Tue, 25 Mar 2014 15:16:07 +0000 (15:16 +0000)
We only backport change that do not affect API or ABI
Update issue 313

15 files changed:
1  2 
CMakeLists.txt
src/bin/jp2/opj_compress.c
src/bin/jp2/opj_decompress.c
src/bin/jp2/opj_dump.c
src/lib/openjp2/cio.c
src/lib/openjp2/j2k.c
src/lib/openjp2/jp2.c
src/lib/openjp2/openjpeg.c
src/lib/openjp2/openjpeg.h
src/lib/openjpip/jp2k_decoder.c
tests/j2k_random_tile_access.c
tests/test_tile_decoder.c
tests/test_tile_encoder.c
tests/unit/testempty1.c
tests/unit/testempty2.c

diff --cc CMakeLists.txt
index c689843cc895e04e69675fd0c263025166eca5be,7d6b0b5575bf19afc48333e227bced3002f29ea6..dee84dbcbb49b2c88aef12c0559bd2bb83d514e8
@@@ -29,8 -29,8 +29,8 @@@ include_regular_expression("^.*$"
  #-----------------------------------------------------------------------------
  # OPENJPEG version number, useful for packaging and doxygen doc:
  set(OPENJPEG_VERSION_MAJOR 2)
 -set(OPENJPEG_VERSION_MINOR 1)
 -set(OPENJPEG_VERSION_BUILD 0)
 +set(OPENJPEG_VERSION_MINOR 0)
- set(OPENJPEG_VERSION_BUILD 0)
++set(OPENJPEG_VERSION_BUILD 1)
  set(OPENJPEG_VERSION
    "${OPENJPEG_VERSION_MAJOR}.${OPENJPEG_VERSION_MINOR}.${OPENJPEG_VERSION_BUILD}")
  set(PACKAGE_VERSION
@@@ -47,6 -47,7 +47,7 @@@
  #   1.5   |  5
  #   1.5.1 |  5
  #   2.0   |  6
 -#   2.1   |  7
++#   2.0.1 |  6
  # above is the recommendation by the OPJ team. If you really need to override this default,
  # you can specify your own OPENJPEG_SOVERSION at cmake configuration time:
  # cmake -DOPENJPEG_SOVERSION:STRING=42 /path/to/openjpeg
@@@ -253,9 -282,8 +282,8 @@@ if(BUILD_TESTING
      # They could be found via svn on the OpenJPEG google code project
      # svn checkout http://openjpeg.googlecode.com/svn/data (about 70 Mo)
      find_path(OPJ_DATA_ROOT README-OPJ-Data
-       PATHS
-       $ENV{OPJ_DATA_ROOT}
-       ${CMAKE_SOURCE_DIR}/../data
 -      PATHS $ENV{OPJ_DATA_ROOT} ${CMAKE_SOURCE_DIR}/../data
++      PATHS $ENV{OPJ_DATA_ROOT} ${CMAKE_SOURCE_DIR}/../../data
+       NO_DEFAULT_PATH NO_CMAKE_FIND_ROOT_PATH
        )
  
      # Add repository where to find tests
index 9c957502124f22f451917fa6bcc0d1bc1200a043,3cd63e2b9e84649d7895f27e8e6bad095bd0a81c..ba1d8374f1ddc67099f684e5df92e6c11c009c46
@@@ -1588,316 -1450,305 +1450,317 @@@ static void info_callback(const char *m
   */
  /* -------------------------------------------------------------------------- */
  int main(int argc, char **argv) {
 +      FILE *fout = NULL;
  
-       opj_cparameters_t parameters;   /* compression parameters */
+     opj_cparameters_t parameters;     /* compression parameters */
  
-       opj_stream_t *l_stream = 00;
-       opj_codec_t* l_codec = 00;
-       opj_image_t *image = NULL;
-       raw_cparameters_t raw_cp;
+     opj_stream_t *l_stream = 00;
+     opj_codec_t* l_codec = 00;
+     opj_image_t *image = NULL;
+     raw_cparameters_t raw_cp;
  
-       char indexfilename[OPJ_PATH_LEN];       /* index file name */
+     char indexfilename[OPJ_PATH_LEN]; /* index file name */
  
-       unsigned int i, num_images, imageno;
-       img_fol_t img_fol;
-       dircnt_t *dirptr = NULL;
+     unsigned int i, num_images, imageno;
+     img_fol_t img_fol;
+     dircnt_t *dirptr = NULL;
  
-       OPJ_BOOL bSuccess;
-   OPJ_BOOL bUseTiles = OPJ_FALSE; /* OPJ_TRUE */
-       OPJ_UINT32 l_nb_tiles = 4;
+     OPJ_BOOL bSuccess;
+     OPJ_BOOL bUseTiles = OPJ_FALSE; /* OPJ_TRUE */
+     OPJ_UINT32 l_nb_tiles = 4;
  
-       /* set encoding parameters to default values */
-       opj_set_default_encoder_parameters(&parameters);
+     /* set encoding parameters to default values */
+     opj_set_default_encoder_parameters(&parameters);
  
-       /* Initialize indexfilename and img_fol */
-       *indexfilename = 0;
-       memset(&img_fol,0,sizeof(img_fol_t));
+     /* Initialize indexfilename and img_fol */
+     *indexfilename = 0;
+     memset(&img_fol,0,sizeof(img_fol_t));
  
-       /* parse input and get user encoding parameters */
-       if(parse_cmdline_encoder(argc, argv, &parameters,&img_fol, &raw_cp, indexfilename) == 1) {
-               return 1;
-       }
+     /* parse input and get user encoding parameters */
+     parameters.tcp_mct = -1; /* This will be set later according to the input image or the provided option */
+     if(parse_cmdline_encoder(argc, argv, &parameters,&img_fol, &raw_cp, indexfilename) == 1) {
+         return 1;
+     }
  
-       if (parameters.cp_cinema){
-               img_fol.rates = (float*)malloc(parameters.tcp_numlayers * sizeof(float));
-               for(i=0; i< parameters.tcp_numlayers; i++){
-                       img_fol.rates[i] = parameters.tcp_rates[i];
-               }
-               cinema_parameters(&parameters);
-       }
-       /* Create comment for codestream */
-       if(parameters.cp_comment == NULL) {
-     const char comment[] = "Created by OpenJPEG version ";
-               const size_t clen = strlen(comment);
-     const char *version = opj_version();
- /* UniPG>> */
+     /* Create comment for codestream */
+     if(parameters.cp_comment == NULL) {
+         const char comment[] = "Created by OpenJPEG version ";
+         const size_t clen = strlen(comment);
+         const char *version = opj_version();
+         /* UniPG>> */
  #ifdef USE_JPWL
-               parameters.cp_comment = (char*)malloc(clen+strlen(version)+11);
-               sprintf(parameters.cp_comment,"%s%s with JPWL", comment, version);
+         parameters.cp_comment = (char*)malloc(clen+strlen(version)+11);
+         sprintf(parameters.cp_comment,"%s%s with JPWL", comment, version);
  #else
-               parameters.cp_comment = (char*)malloc(clen+strlen(version)+1);
-               sprintf(parameters.cp_comment,"%s%s", comment, version);
+         parameters.cp_comment = (char*)malloc(clen+strlen(version)+1);
+         sprintf(parameters.cp_comment,"%s%s", comment, version);
  #endif
- /* <<UniPG */
-       }
-       /* Read directory if necessary */
-       if(img_fol.set_imgdir==1){
-               num_images=get_num_images(img_fol.imgdirpath);
-               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*));
-                       if(!dirptr->filename_buf){
-                               return 0;
-                       }
-                       for(i=0;i<num_images;i++){
-                               dirptr->filename[i] = dirptr->filename_buf + i*OPJ_PATH_LEN;
-                       }
-               }
-               if(load_images(dirptr,img_fol.imgdirpath)==1){
-                       return 0;
-               }
-               if (num_images==0){
-                       fprintf(stdout,"Folder is empty\n");
-                       return 0;
-               }
-       }else{
-               num_images=1;
-       }
-       /*Encoding image one by one*/
-       for(imageno=0;imageno<num_images;imageno++)     {
-               image = NULL;
-               fprintf(stderr,"\n");
-               if(img_fol.set_imgdir==1){
-                       if (get_next_file(imageno, dirptr,&img_fol, &parameters)) {
-                               fprintf(stderr,"skipping file...\n");
-                               continue;
-                       }
-               }
+         /* <<UniPG */
+     }
  
-               switch(parameters.decod_format) {
-                       case PGX_DFMT:
-                               break;
-                       case PXM_DFMT:
-                               break;
-                       case BMP_DFMT:
-                               break;
-                       case TIF_DFMT:
-                               break;
-                       case RAW_DFMT:
-                       case RAWL_DFMT:
-                               break;
-                       case TGA_DFMT:
-                               break;
-                       case PNG_DFMT:
-                               break;
-                       default:
-                               fprintf(stderr,"skipping file...\n");
-                               continue;
-               }
+     /* Read directory if necessary */
+     if(img_fol.set_imgdir==1){
+         num_images=get_num_images(img_fol.imgdirpath);
+         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*));
+             if(!dirptr->filename_buf){
+                 return 0;
+             }
+             for(i=0;i<num_images;i++){
+                 dirptr->filename[i] = dirptr->filename_buf + i*OPJ_PATH_LEN;
+             }
+         }
+         if(load_images(dirptr,img_fol.imgdirpath)==1){
+             return 0;
+         }
+         if (num_images==0){
+             fprintf(stdout,"Folder is empty\n");
+             return 0;
+         }
+     }else{
+         num_images=1;
+     }
+     /*Encoding image one by one*/
+     for(imageno=0;imageno<num_images;imageno++)       {
+         image = NULL;
+         fprintf(stderr,"\n");
+         if(img_fol.set_imgdir==1){
+             if (get_next_file((int)imageno, dirptr,&img_fol, &parameters)) {
+                 fprintf(stderr,"skipping file...\n");
+                 continue;
+             }
+         }
  
-               /* decode the source image */
-               /* ----------------------- */
-               switch (parameters.decod_format) {
-                       case PGX_DFMT:
-                               image = pgxtoimage(parameters.infile, &parameters);
-                               if (!image) {
-                                       fprintf(stderr, "Unable to load pgx file\n");
-                                       return 1;
-                               }
-                               break;
-                       case PXM_DFMT:
-                               image = pnmtoimage(parameters.infile, &parameters);
-                               if (!image) {
-                                       fprintf(stderr, "Unable to load pnm file\n");
-                                       return 1;
-                               }
-                               break;
-                       case BMP_DFMT:
-                               image = bmptoimage(parameters.infile, &parameters);
-                               if (!image) {
-                                       fprintf(stderr, "Unable to load bmp file\n");
-                                       return 1;
-                               }
-                               break;
- #ifdef HAVE_LIBTIFF
-                       case TIF_DFMT:
-                               image = tiftoimage(parameters.infile, &parameters);
-                               if (!image) {
-                                       fprintf(stderr, "Unable to load tiff file\n");
-                                       return 1;
-                               }
-                       break;
- #endif /* HAVE_LIBTIFF */
-                       case RAW_DFMT:
-                               image = rawtoimage(parameters.infile, &parameters, &raw_cp);
-                               if (!image) {
-                                       fprintf(stderr, "Unable to load raw file\n");
-                                       return 1;
-                               }
-                       break;
-                       case RAWL_DFMT:
-                               image = rawltoimage(parameters.infile, &parameters, &raw_cp);
-                               if (!image) {
-                                       fprintf(stderr, "Unable to load raw file\n");
-                                       return 1;
-                               }
-                       break;
-                       case TGA_DFMT:
-                               image = tgatoimage(parameters.infile, &parameters);
-                               if (!image) {
-                                       fprintf(stderr, "Unable to load tga file\n");
-                                       return 1;
-                               }
-                       break;
- #ifdef HAVE_LIBPNG
-                       case PNG_DFMT:
-                               image = pngtoimage(parameters.infile, &parameters);
-                               if (!image) {
-                                       fprintf(stderr, "Unable to load png file\n");
-                                       return 1;
-                               }
-                               break;
- #endif /* HAVE_LIBPNG */
-               }
+         switch(parameters.decod_format) {
+         case PGX_DFMT:
+             break;
+         case PXM_DFMT:
+             break;
+         case BMP_DFMT:
+             break;
+         case TIF_DFMT:
+             break;
+         case RAW_DFMT:
+         case RAWL_DFMT:
+             break;
+         case TGA_DFMT:
+             break;
+         case PNG_DFMT:
+             break;
+         default:
+             fprintf(stderr,"skipping file...\n");
+             continue;
+         }
  
- /* Can happen if input file is TIFF or PNG 
-  * and HAVE_LIBTIF or HAVE_LIBPNG is undefined
+         /* decode the source image */
+         /* ----------------------- */
+         switch (parameters.decod_format) {
+         case PGX_DFMT:
+             image = pgxtoimage(parameters.infile, &parameters);
+             if (!image) {
+                 fprintf(stderr, "Unable to load pgx file\n");
+                 return 1;
+             }
+             break;
+         case PXM_DFMT:
+             image = pnmtoimage(parameters.infile, &parameters);
+             if (!image) {
+                 fprintf(stderr, "Unable to load pnm file\n");
+                 return 1;
+             }
+             break;
+         case BMP_DFMT:
+             image = bmptoimage(parameters.infile, &parameters);
+             if (!image) {
+                 fprintf(stderr, "Unable to load bmp file\n");
+                 return 1;
+             }
+             break;
+ #ifdef OPJ_HAVE_LIBTIFF
+         case TIF_DFMT:
+             image = tiftoimage(parameters.infile, &parameters);
+             if (!image) {
+                 fprintf(stderr, "Unable to load tiff file\n");
+                 return 1;
+             }
+             break;
+ #endif /* OPJ_HAVE_LIBTIFF */
+         case RAW_DFMT:
+             image = rawtoimage(parameters.infile, &parameters, &raw_cp);
+             if (!image) {
+                 fprintf(stderr, "Unable to load raw file\n");
+                 return 1;
+             }
+             break;
+         case RAWL_DFMT:
+             image = rawltoimage(parameters.infile, &parameters, &raw_cp);
+             if (!image) {
+                 fprintf(stderr, "Unable to load raw file\n");
+                 return 1;
+             }
+             break;
+         case TGA_DFMT:
+             image = tgatoimage(parameters.infile, &parameters);
+             if (!image) {
+                 fprintf(stderr, "Unable to load tga file\n");
+                 return 1;
+             }
+             break;
+ #ifdef OPJ_HAVE_LIBPNG
+         case PNG_DFMT:
+             image = pngtoimage(parameters.infile, &parameters);
+             if (!image) {
+                 fprintf(stderr, "Unable to load png file\n");
+                 return 1;
+             }
+             break;
+ #endif /* OPJ_HAVE_LIBPNG */
+         }
+         /* Can happen if input file is TIFF or PNG
+  * and OPJ_HAVE_LIBTIF or OPJ_HAVE_LIBPNG is undefined
  */
-               if( !image) {
-                       fprintf(stderr, "Unable to load file: got no image\n");
-                       return 1;
-               }
+         if( !image) {
+             fprintf(stderr, "Unable to load file: got no image\n");
+             return 1;
+         }
  
-               /* Decide if MCT should be used */
-               parameters.tcp_mct = image->numcomps == 3 ? 1 : 0;
+         /* Decide if MCT should be used */
+         if (parameters.tcp_mct == -1) { /* mct mode has not been set in commandline */
+             parameters.tcp_mct = image->numcomps >= 3 ? 1 : 0;
+         } else {            /* mct mode has been set in commandline */
+             if ((parameters.tcp_mct == 1) && (image->numcomps < 3)){
+                 fprintf(stderr, "RGB->YCC conversion cannot be used:\n");
+                 fprintf(stderr, "Input image has less than 3 components\n");
+                 return 1;
+             }
+             if ((parameters.tcp_mct == 2) && (!parameters.mct_data)){
+                 fprintf(stderr, "Custom MCT has been set but no array-based MCT\n");
+                 fprintf(stderr, "has been provided. Aborting.\n");
+                 return 1;
+             }
+         }
  
-               if(parameters.cp_cinema){
-                       cinema_setup_encoder(&parameters,image,&img_fol);
-               }
+         /* encode the destination image */
+         /* ---------------------------- */
  
-               /* encode the destination image */
-               /* ---------------------------- */
-               switch(parameters.cod_format) {
-                       case J2K_CFMT:  /* JPEG-2000 codestream */
-                       {
-                               /* Get a decoder handle */
-                               l_codec = opj_create_compress(OPJ_CODEC_J2K);
-                               break;
-                       }
-                       case JP2_CFMT:  /* JPEG 2000 compressed image data */
-                       {
-                               /* Get a decoder handle */
-                               l_codec = opj_create_compress(OPJ_CODEC_JP2);
-                               break;
-                       }
-                       default:
-                               fprintf(stderr, "skipping file..\n");
-                               opj_stream_destroy(l_stream);
-                               continue;
-               }
-               
-               /* catch events using our callbacks and give a local context */         
-               opj_set_info_handler(l_codec, info_callback,00);
-               opj_set_warning_handler(l_codec, warning_callback,00);
-               opj_set_error_handler(l_codec, error_callback,00);
-     if( bUseTiles ) {
-       parameters.cp_tx0 = 0;
-       parameters.cp_ty0 = 0;
-       parameters.tile_size_on = OPJ_TRUE;
-       parameters.cp_tdx = 512;
-       parameters.cp_tdy = 512;
-     }
-               opj_setup_encoder(l_codec, &parameters, image);
+         switch(parameters.cod_format) {
+         case J2K_CFMT:        /* JPEG-2000 codestream */
+         {
+             /* Get a decoder handle */
+             l_codec = opj_create_compress(OPJ_CODEC_J2K);
+             break;
+         }
+         case JP2_CFMT:        /* JPEG 2000 compressed image data */
+         {
+             /* Get a decoder handle */
+             l_codec = opj_create_compress(OPJ_CODEC_JP2);
+             break;
+         }
+         default:
+             fprintf(stderr, "skipping file..\n");
 -            opj_stream_destroy_v3(l_stream);
++            opj_stream_destroy(l_stream);
+             continue;
+         }
+         /* catch events using our callbacks and give a local context */
+         opj_set_info_handler(l_codec, info_callback,00);
+         opj_set_warning_handler(l_codec, warning_callback,00);
+         opj_set_error_handler(l_codec, error_callback,00);
+         if( bUseTiles ) {
+             parameters.cp_tx0 = 0;
+             parameters.cp_ty0 = 0;
+             parameters.tile_size_on = OPJ_TRUE;
+             parameters.cp_tdx = 512;
+             parameters.cp_tdy = 512;
+         }
+         opj_setup_encoder(l_codec, &parameters, image);
  
-               /* open a byte stream for writing and allocate memory for all tiles */
-               l_stream = opj_stream_create_default_file_stream(fout,OPJ_FALSE);
-               if (! l_stream){
-                       return 1;
-               }
 +              /* Open the output file*/
 +              fout = fopen(parameters.outfile, "wb");
 +              if (! fout) {
 +                      fprintf(stderr, "Not enable to create output file!\n");
 +                      opj_stream_destroy(l_stream);
 +                      return 1;
 +              }
 +
 -        l_stream = opj_stream_create_default_file_stream_v3(parameters.outfile,OPJ_FALSE);
+         /* open a byte stream for writing and allocate memory for all tiles */
++        l_stream = opj_stream_create_default_file_stream(fout,OPJ_FALSE);
+         if (! l_stream){
+             return 1;
+         }
  
-               /* encode the image */
-     bSuccess = opj_start_compress(l_codec,image,l_stream);
-     if (!bSuccess)  {
-       fprintf(stderr, "failed to encode image: opj_start_compress\n");
-     }
-     if( bUseTiles ) {
-       OPJ_BYTE *l_data;
-       OPJ_UINT32 l_data_size = 512*512*3;
-       l_data = (OPJ_BYTE*) malloc( l_data_size * sizeof(OPJ_BYTE));
-       memset(l_data, 0, l_data_size );
-       assert( l_data );
-       for (i=0;i<l_nb_tiles;++i) {
-         if (! opj_write_tile(l_codec,i,l_data,l_data_size,l_stream)) {
-           fprintf(stderr, "ERROR -> test_tile_encoder: failed to write the tile %d!\n",i);
-           opj_stream_destroy(l_stream);
+         /* encode the image */
+         bSuccess = opj_start_compress(l_codec,image,l_stream);
+         if (!bSuccess)  {
+             fprintf(stderr, "failed to encode image: opj_start_compress\n");
+         }
+         if( bUseTiles ) {
+             OPJ_BYTE *l_data;
+             OPJ_UINT32 l_data_size = 512*512*3;
+             l_data = (OPJ_BYTE*) malloc( l_data_size * sizeof(OPJ_BYTE));
+             memset(l_data, 0, l_data_size );
+             assert( l_data );
+             for (i=0;i<l_nb_tiles;++i) {
+                 if (! opj_write_tile(l_codec,i,l_data,l_data_size,l_stream)) {
+                     fprintf(stderr, "ERROR -> test_tile_encoder: failed to write the tile %d!\n",i);
 -                    opj_stream_destroy_v3(l_stream);
++                    opj_stream_destroy(l_stream);
 +          fclose(fout);
-           opj_destroy_codec(l_codec);
-           opj_image_destroy(image);
-           return 1;
+                     opj_destroy_codec(l_codec);
+                     opj_image_destroy(image);
+                     return 1;
+                 }
+             }
+             free(l_data);
+         }
+         else {
+             bSuccess = bSuccess && opj_encode(l_codec, l_stream);
+             if (!bSuccess)  {
+                 fprintf(stderr, "failed to encode image: opj_encode\n");
+             }
+         }
+         bSuccess = bSuccess && opj_end_compress(l_codec, l_stream);
+         if (!bSuccess)  {
+             fprintf(stderr, "failed to encode image: opj_end_compress\n");
          }
-       }
-       free(l_data);
-     }
-     else {
-       bSuccess = bSuccess && opj_encode(l_codec, l_stream);
-       if (!bSuccess)  {
-         fprintf(stderr, "failed to encode image: opj_encode\n");
-       }
-     }
-               bSuccess = bSuccess && opj_end_compress(l_codec, l_stream);
-               if (!bSuccess)  {
-                       fprintf(stderr, "failed to encode image: opj_end_compress\n");
-               }
  
-               if (!bSuccess)  {
-                       opj_stream_destroy(l_stream);
-                       fclose(fout);
-       opj_destroy_codec(l_codec);
-       opj_image_destroy(image);
-                       fprintf(stderr, "failed to encode image\n");
-                       return 1;
-               }
+         if (!bSuccess)  {
 -            opj_stream_destroy_v3(l_stream);
++            opj_stream_destroy(l_stream);
++          fclose(fout);
+             opj_destroy_codec(l_codec);
+             opj_image_destroy(image);
+             fprintf(stderr, "failed to encode image\n");
+             return 1;
+         }
  
-               fprintf(stderr,"Generated outfile %s\n",parameters.outfile);
-               /* close and free the byte stream */
-               opj_stream_destroy(l_stream);
+         fprintf(stderr,"Generated outfile %s\n",parameters.outfile);
+         /* close and free the byte stream */
 -        opj_stream_destroy_v3(l_stream);
++        opj_stream_destroy(l_stream);
 +              fclose(fout);
  
-               /* free remaining compression structures */
-               opj_destroy_codec(l_codec);
+         /* free remaining compression structures */
+         opj_destroy_codec(l_codec);
  
-               /* free image data */
-               opj_image_destroy(image);
+         /* free image data */
+         opj_image_destroy(image);
  
-       }
+     }
  
-       /* free user parameters structure */
-       if(parameters.cp_comment)   free(parameters.cp_comment);
-       if(parameters.cp_matrice)   free(parameters.cp_matrice);
-     if(parameters.cp_cinema)    free(img_fol.rates);
+     /* free user parameters structure */
+     if(parameters.cp_comment)   free(parameters.cp_comment);
+     if(parameters.cp_matrice)   free(parameters.cp_matrice);
  
-       return 0;
+     return 0;
  }
index df2978db74d949f461cab621109d0d29efd885e4,661a00f719ae43e24b0ddbac18c53bc50e8e5d60..9adebd91da44b3bc012f7a06f67a66bc0be04692
@@@ -794,9 -787,8 +795,9 @@@ 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");
+                       fprintf(stderr, "ERROR -> opj_compress: failed to setup the decoder\n");
 -                      opj_stream_destroy_v3(l_stream);
 +                      opj_stream_destroy(l_stream);
 +                      fclose(fsrc);
                        opj_destroy_codec(l_codec);
                        return EXIT_FAILURE;
                }
  
                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_v3(l_stream);
 +                              opj_stream_destroy(l_stream);
                                opj_destroy_codec(l_codec);
                                opj_image_destroy(image);
 +                              fclose(fsrc);
                                return EXIT_FAILURE;
                        }
  
Simple merge
Simple merge
index 3b439fc725245fa2048cf576a5f2e6de9348ae05,e03db41d425ab4369592225927ce055006be3796..92ec238558e43ccc18d212a79903c80cf329cd5d
@@@ -1994,17 -2049,17 +2049,24 @@@ static OPJ_BOOL opj_j2k_read_siz(opj_j2
                  l_img_comp->sgnd = tmp >> 7;
                  opj_read_bytes(p_header_data,&tmp,1);   /* XRsiz_i */
                  ++p_header_data;
-                 l_img_comp->dx = (OPJ_INT32)tmp; /* should be between 1 and 255 */
+                 l_img_comp->dx = (OPJ_UINT32)tmp; /* should be between 1 and 255 */
                  opj_read_bytes(p_header_data,&tmp,1);   /* YRsiz_i */
                  ++p_header_data;
-                 l_img_comp->dy = (OPJ_INT32)tmp; /* should be between 1 and 255 */
+                 l_img_comp->dy = (OPJ_UINT32)tmp; /* should be between 1 and 255 */
+                 if( l_img_comp->dx < 1 || l_img_comp->dx > 255 ||
+                     l_img_comp->dy < 1 || l_img_comp->dy > 255 ) {
+                     opj_event_msg(p_manager, EVT_ERROR,
+                                   "Invalid values for comp = %d : dx=%u dy=%u\n (should be between 1 and 255 according the JPEG2000 norm)",
+                                   i, l_img_comp->dx, l_img_comp->dy);
+                     return OPJ_FALSE;
+                 }
 +                if( l_img_comp->dx < 1 || l_img_comp->dx > 255 ||
 +                    l_img_comp->dy < 1 || l_img_comp->dy > 255 ) {
 +                    opj_event_msg(p_manager, EVT_ERROR,
 +                                  "Invalid values for comp = %d : dx=%u dy=%u\n (should be between 1 and 255 according the JPEG2000 norm)",
 +                                  i, l_img_comp->dx, l_img_comp->dy);
 +                    return OPJ_FALSE;
 +                }
  
  #ifdef USE_JPWL
                  if (l_cp->correct) {
          }
  
          /* Compute the number of tiles */
-         l_cp->tw = opj_int_ceildiv(l_image->x1 - l_cp->tx0, l_cp->tdx);
-         l_cp->th = opj_int_ceildiv(l_image->y1 - l_cp->ty0, l_cp->tdy);
+         l_cp->tw = (OPJ_UINT32)opj_int_ceildiv((OPJ_INT32)(l_image->x1 - l_cp->tx0), (OPJ_INT32)l_cp->tdx);
+         l_cp->th = (OPJ_UINT32)opj_int_ceildiv((OPJ_INT32)(l_image->y1 - l_cp->ty0), (OPJ_INT32)l_cp->tdy);
++        /* Check that the number of tiles is valid */
++        if (l_cp->tw == 0 || l_cp->th == 0 || l_cp->tw > 65535 / l_cp->th) {
++            opj_event_msg(  p_manager, EVT_ERROR, 
++                            "Invalid number of tiles : %u x %u (maximum fixed by jpeg2000 norm is 65535 tiles)\n",
++                            l_cp->tw, l_cp->th);
++            return OPJ_FALSE;
++        }
 +
          /* Check that the number of tiles is valid */
          if (l_cp->tw == 0 || l_cp->th == 0 || l_cp->tw > 65535 / l_cp->th) {
              opj_event_msg(  p_manager, EVT_ERROR, 
index 9fd785920950e7be0080e8b51ef8535fe274bbc6,32ddd4f4cd1b95d9040bb379f40c943c8a703144..b730d5ded3007bf27884d1f293632c7f4c769733
@@@ -970,13 -1085,24 +1085,22 @@@ void opj_jp2_apply_cdef(opj_image_t *im
        info = color->jp2_cdef->info;
        n = color->jp2_cdef->n;
  
-       for(i = 0; i < n; ++i)
-       {
-               /* WATCH: acn = asoc - 1 ! */
-               if((asoc = info[i].asoc) == 0) continue;
-               cn = info[i].cn; 
-         acn = asoc - 1;
+   for(i = 0; i < n; ++i)
+     {
+     /* WATCH: acn = asoc - 1 ! */
+     asoc = info[i].asoc;
+     if(asoc == 0 || asoc == 65535)
+       {
 -      if (i < image->numcomps)
 -        image->comps[i].alpha = info[i].typ;
+       continue;
+       }
+     cn = info[i].cn; 
+     acn = (OPJ_UINT16)(asoc - 1);
+     if( cn >= image->numcomps || acn >= image->numcomps )
+       {
+       fprintf(stderr, "cn=%d, acn=%d, numcomps=%d\n", cn, acn, image->numcomps);
+       continue;
+       }
  
                if(cn != acn)
                {
                        memcpy(&image->comps[cn], &image->comps[acn], sizeof(opj_image_comp_t));
                        memcpy(&image->comps[acn], &saved, sizeof(opj_image_comp_t));
  
-                       info[i].asoc = cn + 1;
-                       info[acn].asoc = info[acn].cn + 1;
+                       info[i].asoc = (OPJ_UINT16)(cn + 1);
+                       info[acn].asoc = (OPJ_UINT16)(info[acn].cn + 1);
                }
 -              image->comps[cn].alpha = info[i].typ;
        }
  
        if(color->jp2_cdef->info) opj_free(color->jp2_cdef->info);
Simple merge
index 9a2f107391ccb9027d011f308792df757345dd8d,466d07fdc951ff5f315010f940e236fa47825195..749e7d5ccf693ef929c0fe27150210bd7f3b33d8
@@@ -193,10 -210,11 +210,10 @@@ typedef enum PROG_ORDER 
  */
  typedef enum COLOR_SPACE {
        OPJ_CLRSPC_UNKNOWN = -1,        /**< not supported by the library */
-       OPJ_CLRSPC_UNSPECIFIED = 0, /**< not specified in the codestream */ 
+       OPJ_CLRSPC_UNSPECIFIED = 0,     /**< not specified in the codestream */ 
        OPJ_CLRSPC_SRGB = 1,            /**< sRGB */
        OPJ_CLRSPC_GRAY = 2,            /**< grayscale */
-       OPJ_CLRSPC_SYCC = 3                     /**< YUV */
 -      OPJ_CLRSPC_SYCC = 3,            /**< YUV */
 -  OPJ_CLRSPC_EYCC = 4         /**< e-YCC */
++      OPJ_CLRSPC_SYCC = 3             /**< YUV */
  } OPJ_COLOR_SPACE;
  
  /**
Simple merge
index afe4bd4184678cc2f5bd6b2ee19ca1601f5bf99f,b76bf35994a184cc7572c5a09f571b6e399e91bf..1c24a5cdec8a32b268c5c489536fdfec6bcbb2e6
@@@ -218,10 -209,9 +218,10 @@@ int main(int argc, char **argv
        opj_set_warning_handler(l_codec, warning_callback,00);
        opj_set_error_handler(l_codec, error_callback,00);
  
 -    l_stream = opj_stream_create_default_file_stream_v3(parameters.infile,1);
 +      l_stream = opj_stream_create_default_file_stream(fsrc,1);
        if (!l_stream){
 -        fprintf(stderr, "ERROR -> failed to create the stream from the file %s\n", parameters.infile);
 +              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\n");
                return EXIT_FAILURE;
        }
  
Simple merge
index 22ecb348220bb4709b35bca17bda3b76e36c2107,4330fb7d0317365219cf61a38c1cbbe70d467241..350890c4b8037e65cd67e99c113b0b5f9092311c
@@@ -278,15 -263,13 +264,22 @@@ int main (int argc, char *argv[]
                return 1;
        }
  
 -      l_stream = opj_stream_create_default_file_stream_v3(output_file, OPJ_FALSE);
 +      l_file = fopen(output_file,"wb");
 +      if (! l_file) {
 +              fprintf(stderr, "ERROR -> test_tile_encoder: failed to create the output file!\n");
 +              opj_destroy_codec(l_codec);
 +              opj_image_destroy(l_image);
 +              return 1;
 +      }
 +
 +      l_stream = opj_stream_create_default_file_stream(l_file, OPJ_FALSE);
+     if (! l_stream) {
+               fprintf(stderr, "ERROR -> test_tile_encoder: failed to create the stream from the output file %s !\n",output_file );
++              fclose(l_file);
+               opj_destroy_codec(l_codec);
+               opj_image_destroy(l_image);
+               return 1;
+       }
  
        if (! opj_start_compress(l_codec,l_image,l_stream)) {
                fprintf(stderr, "ERROR -> test_tile_encoder: failed to start compress!\n");
index add522a6f3397fa1ee14b1e6028bad326eaf0765,a2ae2d2d2ebb5518643434fb948cec9718be3a9b..8e30a5f4e1589e3d00f68f7373071b3272ad1767
@@@ -111,13 -110,16 +111,20 @@@ int main(int argc, char *argv[]
  
    opj_setup_encoder(l_codec, &parameters, image);
  
 -  l_stream = opj_stream_create_default_file_stream_v3("testempty1.j2k",OPJ_FALSE);
 +  strcpy(parameters.outfile, "testempty1.j2k");
 +  f = fopen(parameters.outfile, "wb");
 +  assert( f );
 +
 +  l_stream = opj_stream_create_default_file_stream(f,OPJ_FALSE);
    assert(l_stream);
    bSuccess = opj_start_compress(l_codec,image,l_stream);
 -    opj_stream_destroy_v3(l_stream);
+   if( !bSuccess )
+     {
++    opj_stream_destroy(l_stream);
+     opj_destroy_codec(l_codec);
+     opj_image_destroy(image);
+     return 0;
+     }
  
    assert( bSuccess );
    bSuccess = opj_encode(l_codec, l_stream);
index 47ef4cc2ac6651c6bfd80a013d38a53c7b85e2b9,abafa5b3571ed469d9d8f306b06c3d8e63558b37..8a5fe221a778c7114479385e171d3d57a2b2c32b
@@@ -73,8 -73,7 +73,8 @@@ int main(int argc, char *argv[]
    opj_image_t *image;
    opj_codec_t* l_codec = 00;
    OPJ_BOOL bSuccess;
-       opj_stream_t *l_stream = 00;
 +  FILE *f;
+   opj_stream_t *l_stream = 00;
    (void)argc;
    (void)argv;
  
  
    opj_setup_encoder(l_codec, &parameters, image);
  
 -  l_stream = opj_stream_create_default_file_stream_v3(parameters.outfile,OPJ_FALSE);
 +  strcpy(parameters.outfile, outputfile);
 +  f = fopen(parameters.outfile, "wb");
 +  assert( f );
 +
 +  l_stream = opj_stream_create_default_file_stream(f,OPJ_FALSE);
+   if( !l_stream )
+     {
+     fprintf( stderr, "Something went wrong during creation of stream\n" );
++  fclose(f);
+     opj_destroy_codec(l_codec);
+     opj_image_destroy(image);
 -    opj_stream_destroy_v3(l_stream);
++    opj_stream_destroy(l_stream);
+     return 1;
+     }
    assert(l_stream);
    bSuccess = opj_start_compress(l_codec,image,l_stream);
 -    opj_stream_destroy_v3(l_stream);
+   if( !bSuccess )
+     {
++  fclose(f);
++    opj_stream_destroy(l_stream);
+     opj_destroy_codec(l_codec);
+     opj_image_destroy(image);
+     return 0;
+     }
  
    assert( bSuccess );
    bSuccess = opj_encode(l_codec, l_stream);
  
    /* read back the generated file */
  {
 +  FILE *fsrc = fopen(outputfile, "rb");
    opj_codec_t* d_codec = 00;
-       opj_dparameters_t dparameters;
+   opj_dparameters_t dparameters;
 +  assert( fsrc );
  
    d_codec = opj_create_decompress(OPJ_CODEC_J2K);
    opj_set_info_handler(d_codec, info_callback,00);
    bSuccess = opj_decode(l_codec, l_stream, image);
    assert( bSuccess );
  
-   bSuccess = opj_end_decompress(l_codec,      l_stream);
+   bSuccess = opj_end_decompress(l_codec, l_stream);
    assert( bSuccess );
  
 -  opj_stream_destroy_v3(l_stream);
 -
 +  opj_stream_destroy(l_stream);
 +  fclose(fsrc);
    opj_destroy_codec(d_codec);
  
    opj_image_destroy(image);