Update the README.cmake file : an instruction was missing to run the tests correctly.
[openjpeg.git] / mj2 / mj2_to_metadata.c
index 48304167e9ba513e0f8e431b86bb0bb0def39149..a9c8893b6ec0ef8a744aa97355146661900f666b 100644 (file)
@@ -10,28 +10,41 @@ A non-exclusive copy of this code has been contributed to the Open JPEG project.
 Except for copyright, inclusion of the code within Open JPEG for distribution and use
 can be bound by the Open JPEG open-source license and disclaimer, expressed elsewhere.
 */
-#include <stdio.h>
-#include <malloc.h>
-#include <setjmp.h>
 
+#include "opj_includes.h"
 #include "mj2.h"
-#include <openjpeg.h>
-
-//MEMORY LEAK
-#ifdef _DEBUG
-#define _CRTDBG_MAP_ALLOC
-#include <stdlib.h>  // Must be included first
-#include <crtdbg.h>
-#endif
-//MEM
 
 #include "mj2_to_metadata.h"
 #include <string.h>
-#ifndef DONT_HAVE_GETOPT
-#include <getopt.h>
-#else
 #include "compat/getopt.h"
-#endif
+
+/* -------------------------------------------------------------------------- */
+
+/**
+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 a FILE* client object
+*/
+void info_callback(const char *msg, void *client_data) {
+       FILE *stream = (FILE*)client_data;
+       fprintf(stream, "[INFO] %s", msg);
+}
+
+/* -------------------------------------------------------------------------- */
+
+
 
 /* ------------- */
 
@@ -96,9 +109,12 @@ void help_display()
 
 int main(int argc, char *argv[]) {
 
+       opj_dinfo_t* dinfo; 
+       opj_event_mgr_t event_mgr;              /* event manager */
+
   FILE *file, *xmlout;
 /*  char xmloutname[50]; */
-  mj2_movie_t movie;
+  opj_mj2_t *movie;
 
   char* infile = 0;
   char* outfile = 0;
@@ -110,11 +126,7 @@ int main(int argc, char *argv[]) {
   BOOL sampletables = FALSE;
   BOOL raw = TRUE;
   BOOL derived = TRUE;
-
-#ifndef NO_PACKETS_DECODING
-  fprintf(stdout,"NO_PACKETS_DECODING should be defined in preprocessing. Exiting...\n");
-  return 1;
-#endif
+       mj2_dparameters_t parameters;
 
   while (TRUE) {
        /* ':' after letter means it takes an argument */
@@ -258,23 +270,41 @@ int main(int argc, char *argv[]) {
   }
   // Leave it open
 
-  if (mj2_read_struct(file, &movie)) // Creating the movie structure
+       /*
+       configure the event callbacks (not required)
+       setting of each callback is optionnal
+       */
+       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;
+
+       /* get a MJ2 decompressor handle */
+       dinfo = mj2_create_decompress();
+
+       /* 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 */
+       movie = (opj_mj2_t*) dinfo->mj2_handle;
+       mj2_setup_decoder(dinfo->mj2_handle, &parameters);
+
+  if (mj2_read_struct(file, movie)) // Creating the movie structure
   {
     fclose(xmlout);
     return 1;
   }
 
   xml_write_init(notes, sampletables, raw, derived);
-  xml_write_struct(file, xmlout, &movie, sampleframe, stringDTD);
+  xml_write_struct(file, xmlout, movie, sampleframe, stringDTD, &event_mgr);
   fclose(xmlout);
 
-  mj2_memory_free(&movie);
+       fprintf(stderr,"Metadata correctly extracted to XML file \n");; 
 
-  //MEMORY LEAK
-  #ifdef _DEBUG
-    _CrtDumpMemoryLeaks();
-  #endif
-  //MEM
+       /* free remaining structures */
+       if(dinfo) {
+               mj2_destroy_decompress((opj_mj2_t*)dinfo->mj2_handle);
+       }
 
   return 0;
 }