WIP: create a new framework to output file information
[openjpeg.git] / applications / codec / j2k_dump.c
index a7893e50d7092332425af2e9c0702c0861b96484..d1c1fb694bbf4aba41cc2352dcdb8f6a172f13c3 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
@@ -180,7 +181,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);
@@ -200,7 +201,7 @@ int parse_cmdline_decoder(int argc, char **argv, opj_dparameters_t *parameters,i
        opj_option_t long_option[]={
                {"ImgDir",REQ_ARG, NULL ,'y'},
        };
-       const char optlist[] = "i:o:h";
+       const char optlist[] = "i:o:d:h";
 
        totlen=sizeof(long_option);
        img_fol->set_out_format = 0;
@@ -215,7 +216,9 @@ int parse_cmdline_decoder(int argc, char **argv, opj_dparameters_t *parameters,i
                                parameters->decod_format = get_file_format(infile);
                                switch(parameters->decod_format) {
                                        case J2K_CFMT:
+                                               break;
                                        case JP2_CFMT:
+                                               break;
                                        case JPT_CFMT:
                                                break;
                                        default:
@@ -254,7 +257,19 @@ int parse_cmdline_decoder(int argc, char **argv, opj_dparameters_t *parameters,i
                                break;
 
                                /* ----------------------------------------------------- */
+                       case 'd':               /* Input decode ROI */
+                       {
+                               int size_optarg = (int)strlen(optarg) + 1;
+                               char *ROI_values = (char*) malloc(size_optarg);
+                               ROI_values[0] = '\0';
+                               strncpy(ROI_values, optarg, strlen(optarg));
+                               ROI_values[strlen(optarg)] = '\0';
+                               printf("ROI_values = %s [%d / %d]\n", ROI_values, strlen(ROI_values), size_optarg );
+                               parse_ROI_values( ROI_values, &parameters->ROI_x0, &parameters->ROI_y0, &parameters->ROI_x1, &parameters->ROI_y1);
+                       }
+                       break;
                        
+                               /* ----------------------------------------------------- */
                        default:
                                fprintf(stderr,"WARNING -> this option is not valid \"-%c %s\"\n",c, opj_optarg);
                                break;
@@ -287,6 +302,34 @@ int parse_cmdline_decoder(int argc, char **argv, opj_dparameters_t *parameters,i
        return 0;
 }
 
+/*******************************************************************************
+ * Parse ROI input values
+ * separator = ","
+ *******************************************************************************/
+int parse_ROI_values( char* inArg, unsigned int *ROI_x0, unsigned int *ROI_y0, unsigned int *ROI_x1, unsigned int *ROI_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{
+               *ROI_x0 = values[0]; *ROI_y0 = values[1];
+               *ROI_x1 = values[2]; *ROI_y1 = values[3];
+               return EXIT_SUCCESS;
+       }
+}
+
 /* -------------------------------------------------------------------------- */
 
 /**
@@ -320,16 +363,16 @@ int main(int argc, char *argv[])
        img_fol_t img_fol;
        opj_event_mgr_t event_mgr;              /* event manager */
        opj_image_header_t* image = NULL;
+       opj_file_info_t file_info;
        FILE *fsrc = NULL, *fout = NULL;
-       opj_bool bResult;
        int num_images;
        int i,imageno;
        dircnt_t *dirptr = NULL;
        opj_codec_t* dinfo = NULL;      /* handle to a decompressor */
        opj_stream_t *cio = NULL;
        opj_codestream_info_t* cstr_info =NULL;  /* Codestream information structure */
-       //OPJ_INT32 l_tile_x0,l_tile_y0;
-       //OPJ_UINT32 l_tile_width,l_tile_height,l_nb_tiles_x,l_nb_tiles_y;
+       /* OPJ_INT32 l_tile_x0,l_tile_y0; */
+       /* OPJ_UINT32 l_tile_width,l_tile_height,l_nb_tiles_x,l_nb_tiles_y; */
 
 
        /* FIXME configure the event callbacks (not required) */
@@ -356,7 +399,7 @@ int main(int argc, char *argv[])
 
                dirptr=(dircnt_t*)malloc(sizeof(dircnt_t));
                if(dirptr){
-                       dirptr->filename_buf = (char*)malloc(num_images*OPJ_PATH_LEN*sizeof(char));     // Stores at max 10 image file names
+                       dirptr->filename_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){
@@ -379,7 +422,7 @@ int main(int argc, char *argv[])
                num_images=1;
        }
 
-       //
+       /* Try to open for writing the output file if necessary */
        if (parameters.outfile[0] != 0){
                fout = fopen(parameters.outfile,"w");
                if (!fout){
@@ -390,9 +433,9 @@ int main(int argc, char *argv[])
        else
                fout = stdout;
 
-       /*Read the header of each image one by one*/
+       /* Read the header of each image one by one */
        for(imageno = 0; imageno < num_images ; imageno++){
-               image = NULL;
+
                fprintf(stderr,"\n");
 
                if(img_fol.set_imgdir==1){
@@ -402,7 +445,6 @@ int main(int argc, char *argv[])
                        }
                }
 
-               /*NEW V2 STYLE*/
                /* read the input file and put it in memory */
                /* ---------------------------------------- */
                fsrc = fopen(parameters.infile, "rb");
@@ -412,6 +454,10 @@ int main(int argc, char *argv[])
                }
 
                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;
+               }
 
                /* decode the code-stream */
                /* ---------------------- */
@@ -447,83 +493,43 @@ int main(int argc, char *argv[])
                /* setup the decoder decoding parameters using user parameters */
                opj_setup_decoder_v2(dinfo, &parameters, &event_mgr);
 
-               /* 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
-                       */
-               //opj_codestream_info_t *cstr_info = NULL;
-
-               /*bResult = opj_read_header(    dinfo,
-                                                                       &image,
-                                                                       &l_tile_x0,
-                                                                       &l_tile_y0,
-                                                                       &l_tile_width,
-                                                                       &l_tile_height,
-                                                                       &l_nb_tiles_x,
-                                                                       &l_nb_tiles_y,
-                                                                       cio);*/
-
-               bResult = opj_read_header(cio, dinfo, &image, &cstr_info);
-
-               if(!bResult){
-                       fprintf(stderr, "ERROR -> j2k_to_image: failed to read header\n");
+               if(! opj_read_header(cio, dinfo, &file_info, OPJ_IMG_INFO | OPJ_J2K_INFO)){
+                       fprintf(stderr, "ERROR -> j2k_dump: failed to read the header\n");
+                       opj_stream_destroy(cio);
+                       fclose(fsrc);
+                       opj_destroy_codec(dinfo);
                        return EXIT_FAILURE;
                }
 
-               if (parameters.decod_format == J2K_CFMT){
-                       /* dump image */
-                       j2k_dump_image(fout, image);
-                       /* dump cp */
-                       //j2k_dump_cp(stdout, image, ((opj_codec_private_t)dinfo)->m_codec);
-                       //j2k_dump_cp(fout, image, ((opj_j2k_t*)dinfo->j2k_handle)->cp);
-               }
-               else if (parameters.decod_format == JP2_CFMT){
-                       /* 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(stdout, image, dinfo->m_codec);
-                       //j2k_dump_cp(fout, image, ((opj_jp2_t*)dinfo->jp2_handle)->j2k->cp);
-               }
+               printf("Setting decoding area to %d,%d,%d,%d\n",
+                               parameters.ROI_x0, parameters.ROI_y0, parameters.ROI_x1, parameters.ROI_x1);
+               opj_set_decode_area(dinfo,
+                                                       parameters.ROI_x0, parameters.ROI_y0,
+                                                       parameters.ROI_x1, parameters.ROI_x1);
+
+               /* Dump file informations from header */
+               dump_file_info(fout, &file_info);
 
                /* close the byte stream */
                opj_stream_destroy(cio);
                fclose(fsrc);
 
-               /* 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");
-                               ret = EXIT_FAILURE;
-                       }
-                       else
-                               ret = EXIT_SUCCESS;
-               }
-               else*/
-                       ret = EXIT_SUCCESS;
-
                /* free remaining structures */
                if (dinfo) {
                        opj_destroy_codec(dinfo);
                }
-               /* free codestream information structure */
-               /*if (*indexfilename)
-                       FIXME A GARDER opj_destroy_cstr_info(&cstr_info);*/
-               /* free image data structure */
+
+
                opj_image_header_destroy(image);
+               //FIXME opj_file_info_destroy(file_info);
 
        }
-/*NEW V2 STYLE*/
 
+
+       /* Close the output file */
        fclose(fout);
 
-  return ret;
+  return EXIT_SUCCESS;
 }
 
 
@@ -537,10 +543,12 @@ static void j2k_dump_image(FILE *fd, opj_image_header_t * img) {
                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, "    bpp=%d\n", comp->bpp); */
                fprintf(fd, "    sgnd=%d\n", comp->sgnd);
                fprintf(fd, "  }\n");
        }
+       //fprintf(fd, "  XTOsiz=%d, YTOsiz=%d, XTsiz=%d, YTsiz=%d\n", img->tile_x0, img->tile_y0, img->tile_width, img->tile_height);
+       //fprintf(fd, "  Nb of tiles in x direction=%d, Nb of tiles in y direction=%d\n", img->nb_tiles_x, img->nb_tiles_y);
        fprintf(fd, "}\n");
 }