- FILE *fsrc = NULL, *fout = NULL;
-
- opj_dparameters_t parameters; /* Decompression parameters */
- opj_image_t* image = NULL; /* Image structure */
- opj_codec_t* l_codec = NULL; /* Handle to a decompressor */
- opj_stream_t *l_stream = NULL; /* Stream */
- opj_codestream_info_v2_t* cstr_info = NULL;
- opj_codestream_index_t* cstr_index = NULL;
-
- OPJ_INT32 num_images, imageno;
- img_fol_t img_fol;
- dircnt_t *dirptr = NULL;
-
-#ifdef MSD
- OPJ_BOOL l_go_on = OPJ_TRUE;
- OPJ_UINT32 l_max_data_size = 1000;
- OPJ_BYTE * l_data = (OPJ_BYTE *) malloc(1000);
-#endif
-
- /* Set decoding parameters to default values */
- opj_set_default_decoder_parameters(¶meters);
-
- /* Initialize img_fol */
- memset(&img_fol,0,sizeof(img_fol_t));
-
- /* Parse input and get user encoding parameters */
- if(parse_cmdline_decoder(argc, argv, ¶meters,&img_fol) == 1) {
- return EXIT_FAILURE;
- }
-
- /* 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 = (char**) malloc(num_images*sizeof(char*));
-
- if(!dirptr->filename_buf){
- return EXIT_FAILURE;
- }
-
- 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 EXIT_FAILURE;
- }
-
- if (num_images==0){
- fprintf(stdout,"Folder is empty\n");
- return EXIT_FAILURE;
- }
- }else{
- 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){
- fprintf(stderr, "ERROR -> failed to open %s for writing\n", parameters.outfile);
- return EXIT_FAILURE;
- }
- }
- else
- fout = stdout;
-
- /* Read the header of each image one by one */
- for(imageno = 0; imageno < num_images ; imageno++){
-
- fprintf(stderr,"\n");
-
- if(img_fol.set_imgdir==1){
- if (get_next_file(imageno, dirptr,&img_fol, ¶meters)) {
- fprintf(stderr,"skipping file...\n");
- continue;
- }
- }
-
- /* Read the input file and put it in memory */
- /* ---------------------------------------- */
- fsrc = fopen(parameters.infile, "rb");
- if (!fsrc) {
- fprintf(stderr, "ERROR -> failed to open %s for reading\n", parameters.infile);
- return EXIT_FAILURE;
- }
-
- l_stream = opj_stream_create_default_file_stream(fsrc,1);
- if (!l_stream){
- fclose(fsrc);
- fprintf(stderr, "ERROR -> failed to create the stream from the file\n");
- return EXIT_FAILURE;
- }
-
- /* Read the JPEG2000 stream */
- /* ------------------------ */
-
- switch(parameters.decod_format) {
- case J2K_CFMT: /* JPEG-2000 codestream */
- {
- /* Get a decoder handle */
- l_codec = opj_create_decompress(OPJ_CODEC_J2K);
- break;
- }
- case JP2_CFMT: /* JPEG 2000 compressed image data */
- {
- /* Get a decoder handle */
- l_codec = opj_create_decompress(OPJ_CODEC_JP2);
- break;
- }
- case JPT_CFMT: /* JPEG 2000, JPIP */
- {
- /* Get a decoder handle */
- l_codec = opj_create_decompress(OPJ_CODEC_JPT);
- 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);
-
- /* Setup the decoder decoding parameters using user parameters */
- if ( !opj_setup_decoder(l_codec, ¶meters) ){
- fprintf(stderr, "ERROR -> opj_dump: failed to setup the decoder\n");
- opj_stream_destroy(l_stream);
- fclose(fsrc);
- opj_destroy_codec(l_codec);
- fclose(fout);
- return EXIT_FAILURE;
- }
-
- /* Read the main header of the codestream and if necessary the JP2 boxes*/
- if(! opj_read_header(l_stream, l_codec, &image)){
- fprintf(stderr, "ERROR -> opj_dump: failed to read the header\n");
- opj_stream_destroy(l_stream);
- fclose(fsrc);
- opj_destroy_codec(l_codec);
- opj_image_destroy(image);
- fclose(fout);
- return EXIT_FAILURE;
- }
-
- opj_dump_codec(l_codec, OPJ_IMG_INFO | OPJ_J2K_MH_INFO | OPJ_J2K_MH_IND, fout );
-
- cstr_info = opj_get_cstr_info(l_codec);
-
- cstr_index = opj_get_cstr_index(l_codec);
-
- /* close the byte stream */
- opj_stream_destroy(l_stream);
- fclose(fsrc);
-
- /* free remaining structures */
- if (l_codec) {
- opj_destroy_codec(l_codec);
- }
-
- /* destroy the image header */
- opj_image_destroy(image);
-
- /* destroy the codestream index */
- opj_destroy_cstr_index(&cstr_index);
-
- /* destroy the codestream info */
- opj_destroy_cstr_info(&cstr_info);
-
- }
-
- /* Close the output file */
- fclose(fout);
-
- return EXIT_SUCCESS;
+ FILE *fout = NULL;
+
+ opj_dparameters_t parameters; /* Decompression parameters */
+ opj_image_t* image = NULL; /* Image structure */
+ opj_codec_t* l_codec = NULL; /* Handle to a decompressor */
+ opj_stream_t *l_stream = NULL; /* Stream */
+ opj_codestream_info_v2_t* cstr_info = NULL;
+ opj_codestream_index_t* cstr_index = NULL;
+
+ OPJ_INT32 num_images, imageno;
+ img_fol_t img_fol;
+ dircnt_t *dirptr = NULL;
+
+ /* Set decoding parameters to default values */
+ opj_set_default_decoder_parameters(¶meters);
+
+ /* Initialize img_fol */
+ memset(&img_fol, 0, sizeof(img_fol_t));
+ img_fol.flag = OPJ_IMG_INFO | OPJ_J2K_MH_INFO | OPJ_J2K_MH_IND;
+
+ /* Parse input and get user encoding parameters */
+ if (parse_cmdline_decoder(argc, argv, ¶meters, &img_fol) == 1) {
+ if (img_fol.imgdirpath) {
+ free(img_fol.imgdirpath);
+ }
+
+ return EXIT_FAILURE;
+ }
+
+ /* 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) {
+ return EXIT_FAILURE;
+ }
+ dirptr->filename_buf = (char*)malloc((size_t)num_images * OPJ_PATH_LEN * sizeof(
+ char)); /* Stores at max 10 image file names*/
+ if (!dirptr->filename_buf) {
+ free(dirptr);
+ return EXIT_FAILURE;
+ }
+ dirptr->filename = (char**) malloc((size_t)num_images * sizeof(char*));
+
+ if (!dirptr->filename) {
+ goto fails;
+ }
+
+ 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) {
+ goto fails;
+ }
+
+ if (num_images == 0) {
+ fprintf(stdout, "Folder is empty\n");
+ goto fails;
+ }
+ } else {
+ 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) {
+ fprintf(stderr, "ERROR -> failed to open %s for writing\n", parameters.outfile);
+ goto fails;
+ }
+ } else {
+ fout = stdout;
+ }
+
+ /* Read the header of each image one by one */
+ for (imageno = 0; imageno < num_images ; imageno++) {
+
+ fprintf(stderr, "\n");
+
+ if (img_fol.set_imgdir == 1) {
+ if (get_next_file(imageno, dirptr, &img_fol, ¶meters)) {
+ fprintf(stderr, "skipping file...\n");
+ continue;
+ }
+ }
+
+ /* Read the input file and put it in memory */
+ /* ---------------------------------------- */
+
+ l_stream = opj_stream_create_default_file_stream(parameters.infile, 1);
+ if (!l_stream) {
+ fprintf(stderr, "ERROR -> failed to create the stream from the file %s\n",
+ parameters.infile);
+ goto fails;
+ }
+
+ /* Read the JPEG2000 stream */
+ /* ------------------------ */
+
+ switch (parameters.decod_format) {
+ case J2K_CFMT: { /* JPEG-2000 codestream */
+ /* Get a decoder handle */
+ l_codec = opj_create_decompress(OPJ_CODEC_J2K);
+ break;
+ }
+ case JP2_CFMT: { /* JPEG 2000 compressed image data */
+ /* Get a decoder handle */
+ l_codec = opj_create_decompress(OPJ_CODEC_JP2);
+ break;
+ }
+ case JPT_CFMT: { /* JPEG 2000, JPIP */
+ /* Get a decoder handle */
+ l_codec = opj_create_decompress(OPJ_CODEC_JPT);
+ 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);
+
+ parameters.flags |= OPJ_DPARAMETERS_DUMP_FLAG;
+
+ /* Setup the decoder decoding parameters using user parameters */
+ if (!opj_setup_decoder(l_codec, ¶meters)) {
+ fprintf(stderr, "ERROR -> opj_dump: failed to setup the decoder\n");
+ opj_stream_destroy(l_stream);
+ opj_destroy_codec(l_codec);
+ fclose(fout);
+ goto fails;
+ }
+
+ /* Read the main header of the codestream and if necessary the JP2 boxes*/
+ if (! opj_read_header(l_stream, l_codec, &image)) {
+ fprintf(stderr, "ERROR -> opj_dump: failed to read the header\n");
+ opj_stream_destroy(l_stream);
+ opj_destroy_codec(l_codec);
+ opj_image_destroy(image);
+ fclose(fout);
+ goto fails;
+ }
+
+ opj_dump_codec(l_codec, img_fol.flag, fout);
+
+ cstr_info = opj_get_cstr_info(l_codec);
+
+ cstr_index = opj_get_cstr_index(l_codec);
+
+ /* close the byte stream */
+ opj_stream_destroy(l_stream);
+
+ /* free remaining structures */
+ if (l_codec) {
+ opj_destroy_codec(l_codec);
+ }
+
+ /* destroy the image header */
+ opj_image_destroy(image);
+
+ /* destroy the codestream index */
+ opj_destroy_cstr_index(&cstr_index);
+
+ /* destroy the codestream info */
+ opj_destroy_cstr_info(&cstr_info);
+
+ }
+
+ /* Close the output file */
+ fclose(fout);
+
+ return EXIT_SUCCESS;
+
+fails:
+ if (dirptr) {
+ if (dirptr->filename) {
+ free(dirptr->filename);
+ }
+ if (dirptr->filename_buf) {
+ free(dirptr->filename_buf);
+ }
+ free(dirptr);
+ }
+ return EXIT_FAILURE;