[trunk] modify image_to_j2k and the lib to support functionalities given by the v2...
[openjpeg.git] / applications / codec / j2k_to_image.c
index b8f237bd54f4e5cdb8d473f94cdceaa14590e406..30d808628e187eaa36688edf7c5445f3f13da137 100644 (file)
@@ -29,6 +29,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 <stdio.h>
 #include <string.h>
@@ -49,9 +50,8 @@
 #define _strnicmp strncasecmp
 #endif /* _WIN32 */
 
-#include "opj_config.h"
 #include "openjpeg.h"
-#include "getopt.h"
+#include "opj_getopt.h"
 #include "convert.h"
 #include "index.h"
 
@@ -85,7 +85,19 @@ typedef struct img_folder{
 
 }img_fol_t;
 
-void decode_help_display() {
+/* -------------------------------------------------------------------------- */
+/* Declarations                                                               */
+int get_num_images(char *imgdirpath);
+int load_images(dircnt_t *dirptr, char *imgdirpath);
+int get_file_format(const char *filename);
+char get_next_file(int imageno,dircnt_t *dirptr,img_fol_t *img_fol, opj_dparameters_t *parameters);
+static int infile_format(const char *fname);
+
+int parse_cmdline_decoder(int argc, char **argv, opj_dparameters_t *parameters,img_fol_t *img_fol, char *indexfilename);
+int parse_DA_values( char* inArg, unsigned int *DA_x0, unsigned int *DA_y0, unsigned int *DA_x1, unsigned int *DA_y1);
+
+/* -------------------------------------------------------------------------- */
+void decode_help_display(void) {
        fprintf(stdout,"HELP for j2k_to_image\n----\n\n");
        fprintf(stdout,"- the -h option displays this help information on screen\n\n");
 
@@ -128,6 +140,14 @@ void decode_help_display() {
        fprintf(stdout,"    are decoded.\n");
        fprintf(stdout,"  -x  \n"); 
        fprintf(stdout,"    Create an index file *.Idx (-x index_name.Idx) \n");
+       fprintf(stdout,"  -d <x0,y0,x1,y1>\n");
+       fprintf(stdout,"    OPTIONAL\n");
+       fprintf(stdout,"    Decoding area\n");
+       fprintf(stdout,"    By default all the image is decoded.\n");
+       fprintf(stdout,"  -t <tile_number>\n");
+       fprintf(stdout,"    OPTIONAL\n");
+       fprintf(stdout,"    Set the tile number of the decoded tile. Follow the JPEG2000 convention from left-up to bottom-up\n");
+       fprintf(stdout,"    By default all tiles are decoded.\n");
        fprintf(stdout,"\n");
 /* UniPG>> */
 #ifdef USE_JPWL
@@ -165,6 +185,7 @@ int get_num_images(char *imgdirpath){
        return num_images;
 }
 
+/* -------------------------------------------------------------------------- */
 int load_images(dircnt_t *dirptr, char *imgdirpath){
        DIR *dir;
        struct dirent* content; 
@@ -190,7 +211,8 @@ int load_images(dircnt_t *dirptr, char *imgdirpath){
        return 0;       
 }
 
-int get_file_format(char *filename) {
+/* -------------------------------------------------------------------------- */
+int get_file_format(const char *filename) {
        unsigned int i;
        static const char *extension[] = {"pgx", "pnm", "pgm", "ppm", "bmp","tif", "raw", "tga", "png", "j2k", "jp2", "jpt", "j2c", "jpc" };
        static const int format[] = { PGX_DFMT, PXM_DFMT, PXM_DFMT, PXM_DFMT, BMP_DFMT, TIF_DFMT, RAW_DFMT, TGA_DFMT, PNG_DFMT, J2K_CFMT, JP2_CFMT, JPT_CFMT, J2K_CFMT, J2K_CFMT };
@@ -209,19 +231,20 @@ int get_file_format(char *filename) {
        return -1;
 }
 
+/* -------------------------------------------------------------------------- */
 char get_next_file(int imageno,dircnt_t *dirptr,img_fol_t *img_fol, opj_dparameters_t *parameters){
        char image_filename[OPJ_PATH_LEN], infilename[OPJ_PATH_LEN],outfilename[OPJ_PATH_LEN],temp_ofname[OPJ_PATH_LEN];
        char *temp_p, temp1[OPJ_PATH_LEN]="";
 
        strcpy(image_filename,dirptr->filename[imageno]);
        fprintf(stderr,"File Number %d \"%s\"\n",imageno,image_filename);
-       parameters->decod_format = get_file_format(image_filename);
+       parameters->decod_format = infile_format(image_filename);
        if (parameters->decod_format == -1)
                return 1;
        sprintf(infilename,"%s/%s",img_fol->imgdirpath,image_filename);
        strncpy(parameters->infile, infilename, sizeof(infilename));
 
-       //Set output file
+       /*Set output file*/
        strcpy(temp_ofname,strtok(image_filename,"."));
        while((temp_p = strtok(NULL,".")) != NULL){
                strcat(temp_ofname,temp1);
@@ -234,16 +257,76 @@ char get_next_file(int imageno,dircnt_t *dirptr,img_fol_t *img_fol, opj_dparamet
        return 0;
 }
 
+/* -------------------------------------------------------------------------- */
+#define JP2_RFC3745_MAGIC "\x00\x00\x00\x0c\x6a\x50\x20\x20\x0d\x0a\x87\x0a"
+#define JP2_MAGIC "\x0d\x0a\x87\x0a"
+/* position 45: "\xff\x52" */
+#define J2K_CODESTREAM_MAGIC "\xff\x4f\xff\x51"
+
+static int infile_format(const char *fname)
+{
+       FILE *reader;
+       const char *s, *magic_s;
+       int ext_format, magic_format;
+       unsigned char buf[12];
+       unsigned int l_nb_read;
+
+       reader = fopen(fname, "rb");
+
+       if (reader == NULL)
+               return -1;
+
+       memset(buf, 0, 12);
+       l_nb_read = fread(buf, 1, 12, reader);
+       fclose(reader);
+       if (l_nb_read != 12)
+               return -1;
+
+
+
+       ext_format = get_file_format(fname);
+
+       if (ext_format == JPT_CFMT)
+               return JPT_CFMT;
+
+       if (memcmp(buf, JP2_RFC3745_MAGIC, 12) == 0 || memcmp(buf, JP2_MAGIC, 4) == 0) {
+               magic_format = JP2_CFMT;
+               magic_s = ".jp2";
+       }
+       else if (memcmp(buf, J2K_CODESTREAM_MAGIC, 4) == 0) {
+               magic_format = J2K_CFMT;
+               magic_s = ".j2k or .jpc or .j2c";
+       }
+       else
+               return -1;
+
+       if (magic_format == ext_format)
+               return ext_format;
+
+       s = fname + strlen(fname) - 4;
+
+       fputs("\n===========================================\n", stderr);
+       fprintf(stderr, "The extension of this file is incorrect.\n"
+                                       "FOUND %s. SHOULD BE %s\n", s, magic_s);
+       fputs("===========================================\n", stderr);
+
+       return magic_format;
+}
+
+/* -------------------------------------------------------------------------- */
+/**
+ * Parse the command line
+ */
 /* -------------------------------------------------------------------------- */
 int parse_cmdline_decoder(int argc, char **argv, opj_dparameters_t *parameters,img_fol_t *img_fol, char *indexfilename) {
        /* parse the command line */
-       int totlen;
-       option_t long_option[]={
+       int totlen, c;
+       opj_option_t long_option[]={
                {"ImgDir",REQ_ARG, NULL ,'y'},
                {"OutFor",REQ_ARG, NULL ,'O'},
        };
 
-       const char optlist[] = "i:o:r:l:x:"
+       const char optlist[] = "i:o:r:l:x:d:t:"
 
 /* UniPG>> */
 #ifdef USE_JPWL
@@ -253,18 +336,20 @@ int parse_cmdline_decoder(int argc, char **argv, opj_dparameters_t *parameters,i
                        "h"             ;
        totlen=sizeof(long_option);
        img_fol->set_out_format = 0;
-       while (1) {
-               int c = getopt_long(argc, argv,optlist,long_option,totlen);
+       do {
+               c = opj_getopt_long(argc, argv,optlist,long_option,totlen);
                if (c == -1)
                        break;
                switch (c) {
                        case 'i':                       /* input file */
                        {
-                               char *infile = optarg;
-                               parameters->decod_format = get_file_format(infile);
+                               char *infile = opj_optarg;
+                               parameters->decod_format = infile_format(infile);
                                switch(parameters->decod_format) {
                                        case J2K_CFMT:
+                                               break;
                                        case JP2_CFMT:
+                                               break;
                                        case JPT_CFMT:
                                                break;
                                        default:
@@ -281,15 +366,21 @@ int parse_cmdline_decoder(int argc, char **argv, opj_dparameters_t *parameters,i
 
                        case 'o':                       /* output file */
                        {
-                               char *outfile = optarg;
+                               char *outfile = opj_optarg;
                                parameters->cod_format = get_file_format(outfile);
                                switch(parameters->cod_format) {
                                        case PGX_DFMT:
+                                               break;
                                        case PXM_DFMT:
+                                               break;
                                        case BMP_DFMT:
+                                               break;
                                        case TIF_DFMT:
+                                               break;
                                        case RAW_DFMT:
+                                               break;
                                        case TGA_DFMT:
+                                               break;
                                        case PNG_DFMT:
                                                break;
                                        default:
@@ -305,7 +396,7 @@ int parse_cmdline_decoder(int argc, char **argv, opj_dparameters_t *parameters,i
                        case 'O':                       /* output format */
                        {
                                char outformat[50];
-                               char *of = optarg;
+                               char *of = opj_optarg;
                                sprintf(outformat,".%s",of);
                                img_fol->set_out_format = 1;
                                parameters->cod_format = get_file_format(outformat);
@@ -344,7 +435,7 @@ int parse_cmdline_decoder(int argc, char **argv, opj_dparameters_t *parameters,i
 
                        case 'r':               /* reduce option */
                        {
-                               sscanf(optarg, "%d", &parameters->cp_reduce);
+                               sscanf(opj_optarg, "%d", &parameters->cp_reduce);
                        }
                        break;
                        
@@ -353,7 +444,7 @@ int parse_cmdline_decoder(int argc, char **argv, opj_dparameters_t *parameters,i
 
                        case 'l':               /* layering option */
                        {
-                               sscanf(optarg, "%d", &parameters->cp_layer);
+                               sscanf(opj_optarg, "%d", &parameters->cp_layer);
                        }
                        break;
                        
@@ -367,15 +458,42 @@ int parse_cmdline_decoder(int argc, char **argv, opj_dparameters_t *parameters,i
 
                        case 'y':                       /* Image Directory path */
                                {
-                                       img_fol->imgdirpath = (char*)malloc(strlen(optarg) + 1);
-                                       strcpy(img_fol->imgdirpath,optarg);
+                                       img_fol->imgdirpath = (char*)malloc(strlen(opj_optarg) + 1);
+                                       strcpy(img_fol->imgdirpath,opj_optarg);
                                        img_fol->set_imgdir=1;
                                }
                                break;
+
+                               /* ----------------------------------------------------- */
+
+                       case 'd':               /* Input decode ROI */
+                       {
+                               int size_optarg = (int)strlen(opj_optarg) + 1;
+                               char *ROI_values = (char*) malloc(size_optarg);
+                               ROI_values[0] = '\0';
+                               strncpy(ROI_values, opj_optarg, strlen(opj_optarg));
+                               ROI_values[strlen(opj_optarg)] = '\0';
+                               /*printf("ROI_values = %s [%d / %d]\n", ROI_values, strlen(ROI_values), size_optarg ); */
+                               parse_DA_values( ROI_values, &parameters->DA_x0, &parameters->DA_y0, &parameters->DA_x1, &parameters->DA_y1);
+
+                               free(ROI_values);
+                       }
+                       break;
+
+                       /* ----------------------------------------------------- */
+
+                       case 't':               /* Input tile index */
+                       {
+                               sscanf(opj_optarg, "%d", &parameters->tile_index);
+                               parameters->nb_tile_to_decode = 1;
+                       }
+                       break;
+
                                /* ----------------------------------------------------- */                                                             
+
                        case 'x':                       /* Creation of index file */
                                {
-                                       char *index = optarg;
+                                       char *index = opj_optarg;
                                        strncpy(indexfilename, index, OPJ_PATH_LEN);
                                }
                                break;
@@ -387,7 +505,7 @@ int parse_cmdline_decoder(int argc, char **argv, opj_dparameters_t *parameters,i
                        {
                                char *token = NULL;
 
-                               token = strtok(optarg, ",");
+                               token = strtok(opj_optarg, ",");
                                while(token != NULL) {
 
                                        /* search expected number of components */
@@ -454,10 +572,10 @@ int parse_cmdline_decoder(int argc, char **argv, opj_dparameters_t *parameters,i
                                /* ----------------------------------------------------- */
                        
                        default:
-                               fprintf(stderr,"WARNING -> this option is not valid \"-%c %s\"\n",c, optarg);
+                               fprintf(stderr,"WARNING -> this option is not valid \"-%c %s\"\n",c, opj_optarg);
                                break;
                }
-       }
+       }while(c != -1);
 
        /* check for possible errors */
        if(img_fol->set_imgdir==1){
@@ -485,6 +603,36 @@ int parse_cmdline_decoder(int argc, char **argv, opj_dparameters_t *parameters,i
        return 0;
 }
 
+/* -------------------------------------------------------------------------- */
+/**
+ * Parse decoding area input values
+ * separator = ","
+ */
+/* -------------------------------------------------------------------------- */
+int parse_DA_values( char* inArg, unsigned int *DA_x0, unsigned int *DA_y0, unsigned int *DA_x1, unsigned int *DA_y1)
+{
+       int it = 0;
+       int values[4];
+       char delims[] = ",";
+       char *result = NULL;
+       result = strtok( inArg, delims );
+
+       while( (result != NULL) && (it < 4 ) ) {
+               values[it] = atoi(result);
+               result = strtok( NULL, delims );
+               it++;
+       }
+
+       if (it != 4) {
+               return EXIT_FAILURE;
+       }
+       else{
+               *DA_x0 = values[0]; *DA_y0 = values[1];
+               *DA_x1 = values[2]; *DA_y1 = values[3];
+               return EXIT_SUCCESS;
+       }
+}
+
 /* -------------------------------------------------------------------------- */
 
 /**
@@ -510,22 +658,26 @@ void info_callback(const char *msg, void *client_data) {
 }
 
 /* -------------------------------------------------------------------------- */
+/**
+ * J2K_TO_IMAGE MAIN
+ */
+/* -------------------------------------------------------------------------- */
+int main(int argc, char **argv)
+{
+       FILE *fsrc = NULL;
+
+       opj_dparameters_t parameters;                   /* decompression parameters */
+       opj_event_mgr_t event_mgr;                              /* event manager */
+       opj_image_t* image = NULL;
+       opj_stream_t *cio = NULL;                               /* Stream */
+       opj_codec_t* dinfo = NULL;                              /* Handle to a decompressor */
+       opj_codestream_index_t* cstr_index = NULL;
+
+       char indexfilename[OPJ_PATH_LEN];       /* index file name */
 
-int main(int argc, char **argv) {
-       opj_dparameters_t parameters;   /* decompression parameters */
+       OPJ_INT32 num_images, imageno;
        img_fol_t img_fol;
-       opj_event_mgr_t event_mgr;              /* event manager */
-       opj_image_t *image = NULL;
-       FILE *fsrc = NULL;
-       unsigned char *src = NULL;
-       int file_length;
-       int num_images;
-       int i,imageno;
        dircnt_t *dirptr = NULL;
-       opj_dinfo_t* dinfo = NULL;      /* handle to a decompressor */
-       opj_cio_t *cio = NULL;
-       opj_codestream_info_t cstr_info;  /* Codestream information structure */
-       char indexfilename[OPJ_PATH_LEN];       /* index file name */
 
        /* configure the event callbacks (not required) */
        memset(&event_mgr, 0, sizeof(opj_event_mgr_t));
@@ -536,45 +688,51 @@ int main(int argc, char **argv) {
        /* set decoding parameters to default values */
        opj_set_default_decoder_parameters(&parameters);
 
-       /* Initialize indexfilename and img_fol */
+       /* FIXME Initialize indexfilename and img_fol */
        *indexfilename = 0;
+
+       /* Initialize img_fol */
        memset(&img_fol,0,sizeof(img_fol_t));
 
        /* parse input and get user encoding parameters */
        if(parse_cmdline_decoder(argc, argv, &parameters,&img_fol, indexfilename) == 1) {
-               return 1;
+               return EXIT_FAILURE;
        }
 
+       /* Set default event mgr */
+       opj_initialize_default_event_handler(&event_mgr, 1);
+
        /* Initialize reading of directory */
        if(img_fol.set_imgdir==1){      
+               int it_image;
                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_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 1;
+                               return EXIT_FAILURE;
                        }
-                       for(i=0;i<num_images;i++){
-                               dirptr->filename[i] = dirptr->filename_buf + i*OPJ_PATH_LEN;
+                       for(it_image=0;it_image<num_images;it_image++){
+                               dirptr->filename[it_image] = dirptr->filename_buf + it_image*OPJ_PATH_LEN;
                        }
                }
                if(load_images(dirptr,img_fol.imgdirpath)==1){
-                       return 1;
+                       return EXIT_FAILURE;
                }
                if (num_images==0){
                        fprintf(stdout,"Folder is empty\n");
-                       return 1;
+                       return EXIT_FAILURE;
                }
        }else{
                num_images=1;
        }
 
-       /*Encoding image one by one*/
+       /*Decoding image one by one*/
        for(imageno = 0; imageno < num_images ; imageno++)      {
-               image = NULL;
+
                fprintf(stderr,"\n");
 
                if(img_fol.set_imgdir==1){
@@ -589,174 +747,124 @@ int main(int argc, char **argv) {
                fsrc = fopen(parameters.infile, "rb");
                if (!fsrc) {
                        fprintf(stderr, "ERROR -> failed to open %s for reading\n", parameters.infile);
-                       return 1;
+                       return EXIT_FAILURE;
                }
-               fseek(fsrc, 0, SEEK_END);
-               file_length = ftell(fsrc);
-               fseek(fsrc, 0, SEEK_SET);
-               src = (unsigned char *) malloc(file_length);
-               if (fread(src, 1, file_length, fsrc) != file_length)
-               {
-                       free(src);
+
+               cio = opj_stream_create_default_file_stream(fsrc,1);
+               if (!cio){
                        fclose(fsrc);
-                       fprintf(stderr, "\nERROR: fread return a number of element different from the expected.\n");
-                       return 1;
+                       fprintf(stderr, "ERROR -> failed to create the stream from the file\n");
+                       return EXIT_FAILURE;
                }
-               fclose(fsrc);
 
-               /* decode the code-stream */
+               /* decode the JPEG2000 stream */
                /* ---------------------- */
 
                switch(parameters.decod_format) {
-               case J2K_CFMT:
-               {
-                       /* JPEG-2000 codestream */
-
-                       /* get a decoder handle */
-                       dinfo = opj_create_decompress(CODEC_J2K);
-
-                       /* catch events using our callbacks and give a local context */
-                       opj_set_event_mgr((opj_common_ptr)dinfo, &event_mgr, stderr);
-
-                       /* setup the decoder decoding parameters using user parameters */
-                       opj_setup_decoder(dinfo, &parameters);
-
-                       /* open a byte stream */
-                       cio = opj_cio_open((opj_common_ptr)dinfo, src, file_length);
-
-                       /* decode the stream and fill the image structure */
-                       if (*indexfilename)                             // If need to extract codestream information
-                               image = opj_decode_with_info(dinfo, cio, &cstr_info);
-                       else
-                               image = opj_decode(dinfo, cio);
-                       if(!image) {
-                               fprintf(stderr, "ERROR -> j2k_to_image: failed to decode image!\n");
-                               opj_destroy_decompress(dinfo);
-                               opj_cio_close(cio);
-                               return 1;
-                       }
-
-                       /* close the byte stream */
-                       opj_cio_close(cio);
-
-                       /* Write the index to disk */
-                       if (*indexfilename) {
-                               char bSuccess;
-                               bSuccess = write_index_file(&cstr_info, indexfilename);
-                               if (bSuccess) {
-                                       fprintf(stderr, "Failed to output index file\n");
-                               }
+                       case J2K_CFMT:  /* JPEG-2000 codestream */
+                       {
+                               /* Get a decoder handle */
+                               dinfo = opj_create_decompress_v2(CODEC_J2K);
+                               break;
                        }
-               }
-               break;
-
-               case JP2_CFMT:
-               {
-                       /* JPEG 2000 compressed image data */
-
-                       /* get a decoder handle */
-                       dinfo = opj_create_decompress(CODEC_JP2);
-
-                       /* catch events using our callbacks and give a local context */
-                       opj_set_event_mgr((opj_common_ptr)dinfo, &event_mgr, stderr);
-
-                       /* setup the decoder decoding parameters using the current image and user parameters */
-                       opj_setup_decoder(dinfo, &parameters);
-
-                       /* open a byte stream */
-                       cio = opj_cio_open((opj_common_ptr)dinfo, src, file_length);
-
-                       /* decode the stream and fill the image structure */
-                       if (*indexfilename)                             // If need to extract codestream information
-                               image = opj_decode_with_info(dinfo, cio, &cstr_info);
-                       else
-                               image = opj_decode(dinfo, cio);                 
-                       if(!image) {
-                               fprintf(stderr, "ERROR -> j2k_to_image: failed to decode image!\n");
-                               opj_destroy_decompress(dinfo);
-                               opj_cio_close(cio);
-                               return 1;
+                       case JP2_CFMT:  /* JPEG 2000 compressed image data */
+                       {
+                               /* Get a decoder handle */
+                               dinfo = opj_create_decompress_v2(CODEC_JP2);
+                               break;
                        }
-
-                       /* close the byte stream */
-                       opj_cio_close(cio);
-
-                       /* Write the index to disk */
-                       if (*indexfilename) {
-                               char bSuccess;
-                               bSuccess = write_index_file(&cstr_info, indexfilename);
-                               if (bSuccess) {
-                                       fprintf(stderr, "Failed to output index file\n");
-                               }
+                       case JPT_CFMT:  /* JPEG 2000, JPIP */
+                       {
+                               /* Get a decoder handle */
+                               dinfo = opj_create_decompress_v2(CODEC_JPT);
+                               break;
                        }
+                       default:
+                               fprintf(stderr, "skipping file..\n");
+                               opj_stream_destroy(cio);
+                               continue;
                }
-               break;
-
-               case JPT_CFMT:
-               {
-                       /* JPEG 2000, JPIP */
 
-                       /* get a decoder handle */
-                       dinfo = opj_create_decompress(CODEC_JPT);
-
-                       /* catch events using our callbacks and give a local context */
-                       opj_set_event_mgr((opj_common_ptr)dinfo, &event_mgr, stderr);
+               /* Setup the decoder decoding parameters using user parameters */
+               if ( !opj_setup_decoder_v2(dinfo, &parameters, &event_mgr) ){
+                       fprintf(stderr, "ERROR -> j2k_dump: failed to setup the decoder\n");
+                       opj_stream_destroy(cio);
+                       fclose(fsrc);
+                       opj_destroy_codec(dinfo);
+                       return EXIT_FAILURE;
+               }
 
-                       /* setup the decoder decoding parameters using user parameters */
-                       opj_setup_decoder(dinfo, &parameters);
 
-                       /* open a byte stream */
-                       cio = opj_cio_open((opj_common_ptr)dinfo, src, file_length);
+               /* Read the main header of the codestream and if necessary the JP2 boxes*/
+               if(! opj_read_header(cio, dinfo, &image)){
+                       fprintf(stderr, "ERROR -> j2k_to_image: failed to read the header\n");
+                       opj_stream_destroy(cio);
+                       fclose(fsrc);
+                       opj_destroy_codec(dinfo);
+                       opj_image_destroy(image);
+                       return EXIT_FAILURE;
+               }
 
-                       /* decode the stream and fill the image structure */
-                       if (*indexfilename)                             // If need to extract codestream information
-                               image = opj_decode_with_info(dinfo, cio, &cstr_info);
-                       else
-                               image = opj_decode(dinfo, cio);
-                       if(!image) {
-                               fprintf(stderr, "ERROR -> j2k_to_image: failed to decode image!\n");
-                               opj_destroy_decompress(dinfo);
-                               opj_cio_close(cio);
-                               return 1;
+               if (!parameters.nb_tile_to_decode) {
+                       // Optional if you want decode the entire image
+                       if (!opj_set_decode_area(dinfo, image, parameters.DA_x0,
+                                       parameters.DA_y0, parameters.DA_x1, parameters.DA_y1)){
+                               fprintf(stderr, "ERROR -> j2k_to_image: failed to set the decoded area\n");
+                               opj_stream_destroy(cio);
+                               opj_destroy_codec(dinfo);
+                               opj_image_destroy(image);
+                               fclose(fsrc);
+                               return EXIT_FAILURE;
                        }
 
-                       /* close the byte stream */
-                       opj_cio_close(cio);
-
-                       /* Write the index to disk */
-                       if (*indexfilename) {
-                               char bSuccess;
-                               bSuccess = write_index_file(&cstr_info, indexfilename);
-                               if (bSuccess) {
-                                       fprintf(stderr, "Failed to output index file\n");
-                               }
+                       /* Get the decoded image */
+                       if (!(opj_decode_v2(dinfo, cio, image) && opj_end_decompress(dinfo,     cio))) {
+                               fprintf(stderr,"ERROR -> j2k_to_image: failed to decode image!\n");
+                               opj_destroy_codec(dinfo);
+                               opj_stream_destroy(cio);
+                               opj_image_destroy(image);
+                               fclose(fsrc);
+                               return EXIT_FAILURE;
                        }
                }
-               break;
-
-               default:
-                       fprintf(stderr, "skipping file..\n");
-                       continue;
-       }
+               else {
+
+                       // It is just here to illustrate how to use the resolution after set parameters
+                       /*if (!opj_set_decoded_resolution_factor(dinfo, 5)) {
+                               fprintf(stderr, "ERROR -> j2k_to_image: failed to set the resolution factor tile!\n");
+                               opj_destroy_codec(dinfo);
+                               opj_stream_destroy(cio);
+                               opj_image_destroy(image);
+                               fclose(fsrc);
+                               return EXIT_FAILURE;
+                       }*/
+
+                       if (!opj_get_decoded_tile(dinfo, cio, image, parameters.tile_index)) {
+                               fprintf(stderr, "ERROR -> j2k_to_image: failed to decode tile!\n");
+                               opj_destroy_codec(dinfo);
+                               opj_stream_destroy(cio);
+                               opj_image_destroy(image);
+                               fclose(fsrc);
+                               return EXIT_FAILURE;
+                       }
+                       fprintf(stdout, "tile %d is decoded!\n\n", parameters.tile_index);
+               }
 
-               /* free the memory containing the code-stream */
-               free(src);
-               src = NULL;
+               /* Close the byte stream */
+               opj_stream_destroy(cio);
+               fclose(fsrc);
 
-       if(image->color_space == CLRSPC_SYCC)
-   {
-       color_sycc_to_rgb(image);
-   }
+               if(image->color_space == CLRSPC_SYCC){
+                       color_sycc_to_rgb(image); /* FIXME */
+               }
 
-       if(image->icc_profile_buf)
-   {
+               if(image->icc_profile_buf) {
 #if defined(HAVE_LIBLCMS1) || defined(HAVE_LIBLCMS2)
-       color_apply_icc_profile(image);
+                       color_apply_icc_profile(image); /* FIXME */
 #endif
-
-       free(image->icc_profile_buf);
-       image->icc_profile_buf = NULL; image->icc_profile_len = 0;
-   }
+                       free(image->icc_profile_buf);
+                       image->icc_profile_buf = NULL; image->icc_profile_len = 0;
+               }
 
                /* create output image */
                /* ------------------- */
@@ -832,19 +940,21 @@ int main(int argc, char **argv) {
                }
 
                /* free remaining structures */
-               if(dinfo) {
-                       opj_destroy_decompress(dinfo);
+               if (dinfo) {
+                       opj_destroy_codec(dinfo);
                }
-               /* free codestream information structure */
-               if (*indexfilename)     
-                       opj_destroy_cstr_info(&cstr_info);
+
+
                /* free image data structure */
                opj_image_destroy(image);
 
+               /* destroy the codestream index */
+               opj_destroy_cstr_index(&cstr_index);
+
        }
-       return 0;
+       return EXIT_SUCCESS;
 }
-//end main
+/*end main*/