[2.0] Backport all changes from trunk
[openjpeg.git] / src / bin / jp2 / opj_dump.c
index 1742f3ebb76f1fb7a0cc4ed41b9b7c0c99b91e40..7d2200a13f5d75cfbd8599a195cb0941f3f027d6 100644 (file)
@@ -405,7 +405,7 @@ static void info_callback(const char *msg, void *client_data) {
 /* -------------------------------------------------------------------------- */
 int main(int argc, char *argv[])
 {
-       FILE *fout = NULL;
+       FILE *fsrc = NULL, *fout = NULL;
 
        opj_dparameters_t parameters;                   /* Decompression parameters */
        opj_image_t* image = NULL;                                      /* Image structure */
@@ -491,10 +491,16 @@ int main(int argc, char *argv[])
 
                /* 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_v3(parameters.infile,1);
+               l_stream = opj_stream_create_default_file_stream(fsrc,1);
                if (!l_stream){
-                       fprintf(stderr, "ERROR -> failed to create the stream from the file %s\n",parameters.infile);
+                       fclose(fsrc);
+                       fprintf(stderr, "ERROR -> failed to create the stream from the file\n");
                        return EXIT_FAILURE;
                }
 
@@ -522,7 +528,7 @@ int main(int argc, char *argv[])
                        }
                        default:
                                fprintf(stderr, "skipping file..\n");
-                               opj_stream_destroy_v3(l_stream);
+                               opj_stream_destroy(l_stream);
                                continue;
                }
 
@@ -534,7 +540,8 @@ int main(int argc, char *argv[])
                /* Setup the decoder decoding parameters using user parameters */
                if ( !opj_setup_decoder(l_codec, &parameters) ){
                        fprintf(stderr, "ERROR -> opj_dump: failed to setup the decoder\n");
-                       opj_stream_destroy_v3(l_stream);
+                       opj_stream_destroy(l_stream);
+                       fclose(fsrc);
                        opj_destroy_codec(l_codec);
                        fclose(fout);
                        return EXIT_FAILURE;
@@ -543,7 +550,8 @@ int main(int argc, char *argv[])
                /* 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_v3(l_stream);
+                       opj_stream_destroy(l_stream);
+                       fclose(fsrc);
                        opj_destroy_codec(l_codec);
                        opj_image_destroy(image);
                        fclose(fout);
@@ -557,7 +565,8 @@ int main(int argc, char *argv[])
                cstr_index = opj_get_cstr_index(l_codec);
 
                /* close the byte stream */
-               opj_stream_destroy_v3(l_stream);
+               opj_stream_destroy(l_stream);
+               fclose(fsrc);
 
                /* free remaining structures */
                if (l_codec) {