[trunk] WIP: add basis for a new output management of the codestream information...
[openjpeg.git] / applications / codec / j2k_dump.c
index b0f4404333af5626ba232dc715a5f086ebf78f78..37a74fd9144d24d6592e7431720cff106ff3e068 100644 (file)
@@ -1,5 +1,6 @@
 /*
- * Copyright (c) 20010, Mathieu Malaterre, GDCM
+ * Copyright (c) 2010, Mathieu Malaterre, GDCM
+ * Copyright (c) 2011, Mickael Savinaud, Communications & Systemes <mickael.savinaud@c-s.fr>
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
@@ -23,6 +24,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>
 #define _strnicmp strncasecmp
 #endif /* _WIN32 */
 
-#include "opj_config.h"
 #include "openjpeg.h"
 #include "j2k.h"
 #include "jp2.h"
-#include "getopt.h"
+#include "opj_getopt.h"
 #include "convert.h"
 #include "index.h"
 
@@ -73,6 +74,17 @@ typedef struct img_folder{
 
 }img_fol_t;
 
+/* -------------------------------------------------------------------------- */
+/* Declarations                                                               */
+int get_num_images(char *imgdirpath);
+int load_images(dircnt_t *dirptr, char *imgdirpath);
+int get_file_format(char *filename);
+char get_next_file(int imageno,dircnt_t *dirptr,img_fol_t *img_fol, opj_dparameters_t *parameters);
+
+int parse_cmdline_decoder(int argc, char **argv, opj_dparameters_t *parameters,img_fol_t *img_fol);
+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_dump\n----\n\n");
        fprintf(stdout,"- the -h option displays this help information on screen\n\n");
@@ -93,16 +105,21 @@ void decode_help_display(void) {
        fprintf(stdout,"    Currently accepts J2K-files, JP2-files and JPT-files. The file type\n");
        fprintf(stdout,"    is identified based on its suffix.\n");
        fprintf(stdout,"  -o <output file>\n");
-  fprintf(stdout,"    OPTIONAL\n");
-  fprintf(stdout,"    Output file where file info will be dump.\n");
-  fprintf(stdout,"    By default it will be in the stdout.\n");
+       fprintf(stdout,"    OPTIONAL\n");
+       fprintf(stdout,"    Output file where file info will be dump.\n");
+       fprintf(stdout,"    By default it will be in the stdout.\n");
+       fprintf(stdout,"  -d <x0,x1,y0,y1>\n"); /* FIXME WIP_MSD */
+       fprintf(stdout,"    OPTIONAL\n");
+       fprintf(stdout,"    Decoding area\n");
+       fprintf(stdout,"    By default all tiles header are read.\n");
+       fprintf(stdout,"  -v "); /* FIXME WIP_MSD */
+       fprintf(stdout,"    OPTIONAL\n");
+       fprintf(stdout,"    Activate or not the verbose mode (display info and warning message)\n");
+       fprintf(stdout,"    By default verbose mode is off.\n");
        fprintf(stdout,"\n");
 }
 
 /* -------------------------------------------------------------------------- */
-static void j2k_dump_image(FILE *fd, opj_image_t * img);
-static void j2k_dump_cp(FILE *fd, opj_image_t * img, opj_cp_t * cp);
-
 int get_num_images(char *imgdirpath){
        DIR *dir;
        struct dirent* content; 
@@ -124,6 +141,7 @@ int get_num_images(char *imgdirpath){
        return num_images;
 }
 
+/* -------------------------------------------------------------------------- */
 int load_images(dircnt_t *dirptr, char *imgdirpath){
        DIR *dir;
        struct dirent* content; 
@@ -149,6 +167,7 @@ int load_images(dircnt_t *dirptr, char *imgdirpath){
        return 0;       
 }
 
+/* -------------------------------------------------------------------------- */
 int get_file_format(char *filename) {
        unsigned int i;
        static const char *extension[] = {"pgx", "pnm", "pgm", "ppm", "bmp","tif", "raw", "tga", "png", "j2k", "jp2", "jpt", "j2c", "jpc"  };
@@ -168,6 +187,7 @@ 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]="";
@@ -180,7 +200,7 @@ char get_next_file(int imageno,dircnt_t *dirptr,img_fol_t *img_fol, opj_dparamet
        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);
@@ -194,28 +214,33 @@ char get_next_file(int imageno,dircnt_t *dirptr,img_fol_t *img_fol, opj_dparamet
 }
 
 /* -------------------------------------------------------------------------- */
-int parse_cmdline_decoder(int argc, char **argv, opj_dparameters_t *parameters,img_fol_t *img_fol, char *indexfilename) {
-       /* parse the command line */
+/**
+ * Parse the command line
+ */
+/* -------------------------------------------------------------------------- */
+int parse_cmdline_decoder(int argc, char **argv, opj_dparameters_t *parameters,img_fol_t *img_fol) {
        int totlen, c;
-       option_t long_option[]={
+       opj_option_t long_option[]={
                {"ImgDir",REQ_ARG, NULL ,'y'},
        };
+       const char optlist[] = "i:o:d:hv";
 
-       const char optlist[] = "i:o:h";
        totlen=sizeof(long_option);
        img_fol->set_out_format = 0;
        do {
-               c = getopt_long(argc, argv,optlist,long_option,totlen);
+               c = opj_getopt_long(argc, argv,optlist,long_option,totlen);
                if (c == -1)
                        break;
                switch (c) {
                        case 'i':                       /* input file */
                        {
-                               char *infile = optarg;
+                               char *infile = opj_optarg;
                                parameters->decod_format = get_file_format(infile);
                                switch(parameters->decod_format) {
                                        case J2K_CFMT:
+                                               break;
                                        case JP2_CFMT:
+                                               break;
                                        case JPT_CFMT:
                                                break;
                                        default:
@@ -228,11 +253,11 @@ int parse_cmdline_decoder(int argc, char **argv, opj_dparameters_t *parameters,i
                        }
                        break;
 
-                       /* ------------------------------------------------------ */
+                               /* ------------------------------------------------------ */
 
                        case 'o':     /* output file */
                        {
-                         char *outfile = optarg;
+                         char *outfile = opj_optarg;
                          strncpy(parameters->outfile, outfile, sizeof(parameters->outfile)-1);
                        }
                        break;
@@ -246,17 +271,37 @@ 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->set_imgdir=1;
-                               }
-                               break;
+                       {
+                               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);
+                       }
+                       break;
+                       /* ----------------------------------------------------- */
+
+                       case 'v':               /* Verbose mode */
+                       {
+                               parameters->m_verbose = 1;
+                       }
+                       break;
                        
+                               /* ----------------------------------------------------- */
                        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);
@@ -288,110 +333,119 @@ int parse_cmdline_decoder(int argc, char **argv, opj_dparameters_t *parameters,i
 }
 
 /* -------------------------------------------------------------------------- */
-
-/**
-sample error callback expecting a FILE* client object
-*/
-void error_callback(const char *msg, void *client_data) {
-       FILE *stream = (FILE*)client_data;
-       fprintf(stream, "[ERROR] %s", msg);
-}
 /**
-sample warning callback expecting a FILE* client object
-*/
-void warning_callback(const char *msg, void *client_data) {
-       FILE *stream = (FILE*)client_data;
-       fprintf(stream, "[WARNING] %s", msg);
-}
-/**
-sample debug callback expecting no client object
-*/
-void info_callback(const char *msg, void *client_data) {
-       (void)client_data;
-       fprintf(stdout, "[INFO] %s", msg);
+ * 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;
+       }
 }
 
-/* -------------------------------------------------------------------------- */
 
+/* -------------------------------------------------------------------------- */
+/**
+ * J2K_DUMP MAIN
+ */
+/* -------------------------------------------------------------------------- */
 int main(int argc, char *argv[])
 {
-       opj_dparameters_t parameters;   /* decompression parameters */
-       img_fol_t img_fol;
-       opj_event_mgr_t event_mgr;              /* event manager */
-       opj_image_t *image = NULL;
        FILE *fsrc = NULL, *fout = NULL;
-       unsigned char *src = NULL;
-       int file_length;
-       int num_images;
-       int i,imageno;
+
+       opj_dparameters_t parameters;                   /* Decompression parameters */
+       opj_event_mgr_t event_mgr;                              /* Event manager */
+       opj_image_header_t img_header;                  /* Image info structure */
+       opj_codec_t* dinfo = NULL;                              /* Handle to a decompressor */
+       opj_stream_t *cio = NULL;                               /* Stream */
+       opj_codestream_info_v2_t* cstr_info;
+       opj_codestream_index_t* cstr_index;
+
+       OPJ_INT32 num_images, imageno;
+       img_fol_t img_fol;
        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));
-       event_mgr.error_handler = error_callback;
-       event_mgr.warning_handler = warning_callback;
-       event_mgr.info_handler = info_callback;
-
-       /* set decoding parameters to default values */
+
+       opj_bool l_go_on = OPJ_TRUE;
+       OPJ_UINT32 l_max_data_size = 1000;
+       OPJ_BYTE * l_data = (OPJ_BYTE *) malloc(1000);
+
+       /* Set decoding parameters to default values */
        opj_set_default_decoder_parameters(&parameters);
 
-       /* 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;
+       /* Parse input and get user encoding parameters */
+       if(parse_cmdline_decoder(argc, argv, &parameters,&img_fol) == 1) {
+               return EXIT_FAILURE;
        }
 
+       /* Set default event mgr */
+       opj_set_default_event_handler(&event_mgr, parameters.m_verbose);
+
        /* 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;
        }
 
-       //
-       if (parameters.outfile[0] != 0)
-         {
-         fout = fopen(parameters.outfile,"w");
-    if (!fout)
-      {
-      fprintf(stderr, "ERROR -> failed to open %s for reading\n", parameters.outfile);
-      return 1;
-      }
-         }
+       /* Try to open for writing the output file if necessary */
+       if (parameters.outfile[0] != 0){
+               fout = fopen(parameters.outfile,"w");
+               if (!fout){
+                       fprintf(stderr, "ERROR -> failed to open %s for writing\n", parameters.outfile);
+                       return EXIT_FAILURE;
+               }
+       }
        else
-         fout = stdout;
+               fout = stdout;
+
+       /* Read the header of each image one by one */
+       for(imageno = 0; imageno < num_images ; imageno++){
 
-       /*Encoding image one by one*/
-       for(imageno = 0; imageno < num_images ; imageno++)
-  {
-               image = NULL;
                fprintf(stderr,"\n");
 
                if(img_fol.set_imgdir==1){
@@ -401,268 +455,160 @@ int main(int argc, char *argv[])
                        }
                }
 
-               /* read the input file and put it in memory */
+               /* 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 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) != (size_t)file_length)
-               {
-                       free(src);
-                       fclose(fsrc);
-                       fprintf(stderr, "\nERROR: fread return a number of element different from the expected.\n");
-                       return 1;
+
+               cio = opj_stream_create_default_file_stream(fsrc,1);
+               if (!cio){
+                       fprintf(stderr, "ERROR -> failed to create the stream from the file\n");
+                       return EXIT_FAILURE;
                }
-               fclose(fsrc);
 
-               /* decode the code-stream */
-               /* ---------------------- */
+               /* Read 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;
-                       }
-                       /* dump image */
-      j2k_dump_image(fout, image);
-
-                       /* dump cp */
-      j2k_dump_cp(fout, image, ((opj_j2k_t*)dinfo->j2k_handle)->cp);
-
-                       /* close the byte stream */
-                       opj_cio_close(cio);
-
-                       /* Write the index to disk */
-                       if (*indexfilename) {
-                               opj_bool 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;
                        }
-                       /* dump image */
-         if(image->icc_profile_buf)
-        {
-         free(image->icc_profile_buf); image->icc_profile_buf = NULL;
-        }      
-      j2k_dump_image(fout, image);
-
-                       /* dump cp */
-      j2k_dump_cp(fout, image, ((opj_jp2_t*)dinfo->jp2_handle)->j2k->cp);
-
-                       /* close the byte stream */
-                       opj_cio_close(cio);
-
-                       /* Write the index to disk */
-                       if (*indexfilename) {
-                               opj_bool 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 */
-                       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);
+               /* 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);
+                       fclose(fout);
+                       return EXIT_FAILURE;
+               }
 
-                       /* Write the index to disk */
-                       if (*indexfilename) {
-                               opj_bool bSuccess;
-                               bSuccess = write_index_file(&cstr_info, indexfilename);
-                               if (bSuccess) {
-                                       fprintf(stderr, "Failed to output index file\n");
-                               }
-                       }
+               /* Read the main header of the codestream and if necessary the JP2 boxes*/
+               if(! opj_read_header(cio, dinfo, &img_header)){
+                       fprintf(stderr, "ERROR -> j2k_dump: failed to read the header\n");
+                       opj_stream_destroy(cio);
+                       fclose(fsrc);
+                       opj_destroy_codec(dinfo);
+                       fclose(fout);
+                       return EXIT_FAILURE;
                }
-               break;
 
-               default:
-                       fprintf(stderr, "skipping file..\n");
-                       continue;
-       }
+               opj_dump_codec(dinfo, OPJ_IMG_INFO | OPJ_J2K_MH_INFO | OPJ_J2K_MH_IND, stdout );
 
-               /* free the memory containing the code-stream */
-               free(src);
-               src = NULL;
+               cstr_info = opj_get_cstr_info(dinfo);
 
-               /* free remaining structures */
-               if(dinfo) {
-                       opj_destroy_decompress(dinfo);
-               }
-               /* free codestream information structure */
-               if (*indexfilename)     
-                       opj_destroy_cstr_info(&cstr_info);
-               /* free image data structure */
-               opj_image_destroy(image);
+               cstr_index = opj_get_cstr_index(dinfo);
 
-       }
+               fprintf(stdout,"Setting decoding area to %d,%d,%d,%d\n",
+                               parameters.DA_x0, parameters.DA_y0, parameters.DA_x1, parameters.DA_y1);
 
-       fclose(fout);
+#ifdef MSD
+               /* FIXME WIP_MSD <*/
+               if (! opj_set_decode_area(      dinfo,
+                                                                       parameters.DA_x0, parameters.DA_y0,
+                                                                       parameters.DA_x1, parameters.DA_y1)){
+                       fprintf(stderr, "ERROR -> j2k_dump: failed to set the decoded area\n");
+                       opj_stream_destroy(cio);
+                       opj_destroy_codec(dinfo);
+                       fclose(fsrc);
+                       fclose(fout);
+                       return EXIT_FAILURE;
+               }
 
-  return EXIT_SUCCESS;
-}
+               while (l_go_on) {
+                       OPJ_INT32 l_current_tile_x0,l_current_tile_y0,l_current_tile_x1,l_current_tile_y1;
+                       OPJ_UINT32 l_nb_comps, l_tile_index, l_data_size;
+
+
+                       if (! opj_read_tile_header(     dinfo,
+                                                                               cio,
+                                                                               &l_tile_index,
+                                                                               &l_data_size,
+                                                                               &l_current_tile_x0,
+                                                                               &l_current_tile_y0,
+                                                                               &l_current_tile_x1,
+                                                                               &l_current_tile_y1,
+                                                                               &l_nb_comps,
+                                                                               &l_go_on
+                                                                               )) {
+                               fprintf(stderr, "ERROR -> j2k_dump: failed read the tile header\n");
+                               opj_stream_destroy(cio);
+                               fclose(fsrc);
+                               opj_destroy_codec(dinfo);
+                               return EXIT_FAILURE;
+                       }
 
+                       if (l_go_on) {
 
-static void j2k_dump_image(FILE *fd, opj_image_t * img) {
-       int compno;
-       fprintf(fd, "image {\n");
-       fprintf(fd, "  x0=%d, y0=%d, x1=%d, y1=%d\n", img->x0, img->y0, img->x1, img->y1);
-       fprintf(fd, "  numcomps=%d\n", img->numcomps);
-       for (compno = 0; compno < img->numcomps; compno++) {
-               opj_image_comp_t *comp = &img->comps[compno];
-               fprintf(fd, "  comp %d {\n", compno);
-               fprintf(fd, "    dx=%d, dy=%d\n", comp->dx, comp->dy);
-               fprintf(fd, "    prec=%d\n", comp->prec);
-               //fprintf(fd, "    bpp=%d\n", comp->bpp);
-               fprintf(fd, "    sgnd=%d\n", comp->sgnd);
-               fprintf(fd, "  }\n");
-       }
-       fprintf(fd, "}\n");
-}
+                               if (l_data_size > l_max_data_size) {
 
-static void j2k_dump_cp(FILE *fd, opj_image_t * img, opj_cp_t * cp) {
-       int tileno, compno, layno, bandno, resno, numbands;
-       fprintf(fd, "coding parameters {\n");
-       fprintf(fd, "  tx0=%d, ty0=%d\n", cp->tx0, cp->ty0);
-       fprintf(fd, "  tdx=%d, tdy=%d\n", cp->tdx, cp->tdy);
-       fprintf(fd, "  tw=%d, th=%d\n", cp->tw, cp->th);
-       for (tileno = 0; tileno < cp->tw * cp->th; tileno++) {
-               opj_tcp_t *tcp = &cp->tcps[tileno];
-               fprintf(fd, "  tile %d {\n", tileno);
-               fprintf(fd, "    csty=%x\n", tcp->csty);
-               fprintf(fd, "    prg=%d\n", tcp->prg);
-               fprintf(fd, "    numlayers=%d\n", tcp->numlayers);
-               fprintf(fd, "    mct=%d\n", tcp->mct);
-               fprintf(fd, "    rates=");
-               for (layno = 0; layno < tcp->numlayers; layno++) {
-                       fprintf(fd, "%.1f ", tcp->rates[layno]);
-               }
-               fprintf(fd, "\n");
-               for (compno = 0; compno < img->numcomps; compno++) {
-                       opj_tccp_t *tccp = &tcp->tccps[compno];
-                       fprintf(fd, "    comp %d {\n", compno);
-                       fprintf(fd, "      csty=%x\n", tccp->csty);
-                       fprintf(fd, "      numresolutions=%d\n", tccp->numresolutions);
-                       fprintf(fd, "      cblkw=%d\n", tccp->cblkw);
-                       fprintf(fd, "      cblkh=%d\n", tccp->cblkh);
-                       fprintf(fd, "      cblksty=%x\n", tccp->cblksty);
-                       fprintf(fd, "      qmfbid=%d\n", tccp->qmfbid);
-                       fprintf(fd, "      qntsty=%d\n", tccp->qntsty);
-                       fprintf(fd, "      numgbits=%d\n", tccp->numgbits);
-                       fprintf(fd, "      roishift=%d\n", tccp->roishift);
-                       fprintf(fd, "      stepsizes=");
-                       numbands = tccp->qntsty == J2K_CCP_QNTSTY_SIQNT ? 1 : tccp->numresolutions * 3 - 2;
-                       for (bandno = 0; bandno < numbands; bandno++) {
-                               fprintf(fd, "(%d,%d) ", tccp->stepsizes[bandno].mant,
-                                       tccp->stepsizes[bandno].expn);
-                       }
-                       fprintf(fd, "\n");
-                       
-                       if (tccp->csty & J2K_CCP_CSTY_PRT) {
-                               fprintf(fd, "      prcw=");
-                               for (resno = 0; resno < tccp->numresolutions; resno++) {
-                                       fprintf(fd, "%d ", tccp->prcw[resno]);
+                                       l_data = (OPJ_BYTE *) realloc(l_data,l_data_size);
+                                       if (! l_data) {
+                                               opj_stream_destroy(cio);
+                                               opj_destroy_codec(dinfo);
+                                               fclose(fsrc);
+                                               fclose(fout);
+                                               return EXIT_FAILURE;
+                                       }
+
+                                       l_max_data_size = l_data_size;
                                }
-                               fprintf(fd, "\n");
-                               fprintf(fd, "      prch=");
-                               for (resno = 0; resno < tccp->numresolutions; resno++) {
-                                       fprintf(fd, "%d ", tccp->prch[resno]);
+
+                               if (! opj_decode_tile_data(dinfo,l_tile_index,l_data,l_data_size,cio))
+                               {
+                                       free(l_data);
+                                       opj_stream_destroy(cio);
+                                       opj_destroy_codec(dinfo);
+                                       fclose(fsrc);
+                                       fclose(fout);
+                                       return EXIT_FAILURE;
                                }
-                               fprintf(fd, "\n");
+                               /** now should inspect image to know the reduction factor and then how to behave with data */
                        }
-                       fprintf(fd, "    }\n");
                }
-               fprintf(fd, "  }\n");
+               /* FIXME WIP_MSD >*/
+#endif
+
+               /* close the byte stream */
+               opj_stream_destroy(cio);
+               fclose(fsrc);
+
+               /* free remaining structures */
+               if (dinfo) {
+                       opj_destroy_codec(dinfo);
+               }
+
+               /* destroy the image header */
+               opj_image_header_destroy(&img_header);
+
        }
-       fprintf(fd, "}\n");
-}
 
+       /* Close the output file */
+       fclose(fout);
+
+  return EXIT_SUCCESS;
+}