Exemplo n.º 1
0
int main(int argc, char *argv[]) {

	opj_dinfo_t* dinfo; 
	opj_event_mgr_t event_mgr;		/* event manager */

  FILE *file, *xmlout;
/*  char xmloutname[50]; */
  opj_mj2_t *movie;

  char* infile = 0;
  char* outfile = 0;
  char* s, S1, S2, S3;
  int len;
  unsigned int sampleframe = 1; /* First frame */
  char* stringDTD = NULL;
  BOOL notes = TRUE;
  BOOL sampletables = FALSE;
  BOOL raw = TRUE;
  BOOL derived = TRUE;
	mj2_dparameters_t parameters;

  while (TRUE) {
	/* ':' after letter means it takes an argument */
    int c = getopt(argc, argv, "i:o:f:v:hntrd");
	/* FUTURE:  Reserve 'p' for pruning file (which will probably make -t redundant) */
    if (c == -1)
      break;
    switch (c) {
    case 'i':			/* IN file */
      infile = optarg;
      s = optarg;
      while (*s) { s++; } /* Run to filename end */
      s--;
      S3 = *s;
      s--;
      S2 = *s;
      s--;
      S1 = *s;
      
      if ((S1 == 'm' && S2 == 'j' && S3 == '2')
      || (S1 == 'M' && S2 == 'J' && S3 == '2')) {
       break;
      }
      fprintf(stderr, "Input file name must have .mj2 extension, not .%c%c%c.\n", S1, S2, S3);
      return 1;

      /* ----------------------------------------------------- */
    case 'o':			/* OUT file */
      outfile = optarg;
      while (*outfile) { outfile++; } /* Run to filename end */
      outfile--;
      S3 = *outfile;
      outfile--;
      S2 = *outfile;
      outfile--;
      S1 = *outfile;
      
      outfile = optarg;
      
      if ((S1 == 'x' && S2 == 'm' && S3 == 'l')
	  || (S1 == 'X' && S2 == 'M' && S3 == 'L'))
        break;
    
      fprintf(stderr,
	  "Output file name must have .xml extension, not .%c%c%c\n", S1, S2, S3);
	  return 1;

      /* ----------------------------------------------------- */
    case 'f':			/* Choose sample frame.  0 = none */
      sscanf(optarg, "%u", &sampleframe);
      break;

      /* ----------------------------------------------------- */
    case 'v':			/* Verification by DTD. */
      stringDTD = optarg;
	  /* We will not insist upon last 3 chars being "dtd", since non-file
	  access protocol may be used. */
	  if(strchr(stringDTD,'"') != NULL) {
        fprintf(stderr, "-D's string must not contain any embedded double-quote characters.\n");
	    return 1;
	  }

      if (strncmp(stringDTD,"PUBLIC ",7) == 0 || strncmp(stringDTD,"SYSTEM ",7) == 0)
        break;
    
      fprintf(stderr, "-D's string must start with \"PUBLIC \" or \"SYSTEM \"\n");
	  return 1;

    /* ----------------------------------------------------- */
    case 'n':			/* Suppress comments */
      notes = FALSE;
      break;

    /* ----------------------------------------------------- */
    case 't':			/* Show sample size and chunk offset tables */
      sampletables = TRUE;
      break;

    /* ----------------------------------------------------- */
    case 'h':			/* Display an help description */
      help_display();
      return 0;

    /* ----------------------------------------------------- */
    case 'r':			/* Suppress raw data */
      raw = FALSE;
      break;

    /* ----------------------------------------------------- */
    case 'd':			/* Suppress derived data */
      derived = FALSE;
      break;

   /* ----------------------------------------------------- */
    default:
      return 1;
    } /* switch */
  } /* while */

  if(!raw && !derived)
	  raw = TRUE; /* At least one of 'raw' and 'derived' must be true */

    /* Error messages */
  /* -------------- */
  if (!infile || !outfile) {
    fprintf(stderr,"Correct usage: mj2_to_metadata -i mj2-file -o xml-file (plus options)\n");
    return 1;
  }

/* was:
  if (argc != 3) {
    printf("Bad syntax: Usage: MJ2_to_metadata inputfile.mj2 outputfile.xml\n"); 
    printf("Example: MJ2_to_metadata foreman.mj2 foreman.xml\n");
    return 1;
  }
*/
  len = strlen(infile);
  if(infile[0] == ' ')
  {
    infile++; /* There may be a leading blank if user put space after -i */
  }
  
  file = fopen(infile, "rb"); /* was: argv[1] */
  
  if (!file) {
    fprintf(stderr, "Failed to open %s for reading.\n", infile); /* was: argv[1] */
    return 1;
  }

  len = strlen(outfile);
  if(outfile[0] == ' ')
  {
    outfile++; /* There may be a leading blank if user put space after -o */
  }

  // Checking output file
  xmlout = fopen(outfile, "w"); /* was: argv[2] */
  if (!xmlout) {
    fprintf(stderr, "Failed to open %s for writing.\n", outfile); /* was: argv[2] */
    return 1;
  }
  // Leave it open

	/*
	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, &event_mgr);
  fclose(xmlout);

	fprintf(stderr,"Metadata correctly extracted to XML file \n");;	

	/* free remaining structures */
	if(dinfo) {
		mj2_destroy_decompress((opj_mj2_t*)dinfo->mj2_handle);
	}

  return 0;
}
Exemplo n.º 2
0
int main(int argc, char *argv[]) {
  opj_dinfo_t* dinfo;
  opj_event_mgr_t event_mgr;    /* event manager */
  int tnum;
  unsigned int snum;
  opj_mj2_t *movie;
  mj2_tk_t *track;
  mj2_sample_t *sample;
  unsigned char* frame_codestream;
  FILE *file, *outfile;
  char outfilename[50];
  mj2_dparameters_t parameters;

  if (argc != 3) {
    printf("Bad syntax: Usage: MJ2_extractor mj2filename output_location\n");
    printf("Example: MJ2_extractor foreman.mj2 output/foreman\n");
    return 1;
  }

  file = fopen(argv[1], "rb");

  if (!file) {
    fprintf(stderr, "failed to open %s for reading\n", argv[1]);
    return 1;
  }

  /*
  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
    return 1;

  // Decode first video track
  tnum = 0;
  while (movie->tk[tnum].track_type != 0)
    tnum ++;

  track = &movie->tk[tnum];

  fprintf(stdout,"Extracting %d frames from file...\n",track->num_samples);

  for (snum=0; snum < track->num_samples; snum++)
  {
    sample = &track->sample[snum];
    frame_codestream = (unsigned char*) malloc (sample->sample_size-8); // Skipping JP2C marker
    fseek(file,sample->offset+8,SEEK_SET);
    fread(frame_codestream,sample->sample_size-8,1, file);  // Assuming that jp and ftyp markers size do

    sprintf(outfilename,"%s_%05d.j2k",argv[2],snum);
    outfile = fopen(outfilename, "wb");
    if (!outfile) {
      fprintf(stderr, "failed to open %s for writing\n",outfilename);
      return 1;
    }
    fwrite(frame_codestream,sample->sample_size-8,1,outfile);
    fclose(outfile);
    free(frame_codestream);
    }
  fclose(file);
  fprintf(stdout, "%d frames correctly extracted\n", snum);

  /* free remaining structures */
  if(dinfo) {
    mj2_destroy_decompress((opj_mj2_t*)dinfo->mj2_handle);
  }

  return 0;
}
Exemplo n.º 3
0
int main(int argc, char *argv[]) {
	mj2_dparameters_t mj2_parameters;			/* decompression parameters */
	opj_dinfo_t* dinfo; 
	opj_event_mgr_t event_mgr;		/* event manager */	
	opj_cio_t *cio = NULL;
  unsigned int tnum, snum;
  opj_mj2_t *movie;
  mj2_tk_t *track;
  mj2_sample_t *sample;
  unsigned char* frame_codestream;
  FILE *file, *outfile;
  char outfilename[50];
  opj_image_t *img = NULL;
	unsigned int max_codstrm_size = 0;
	double total_time = 0;
	unsigned int numframes = 0;
			
  if (argc != 3) {
    printf("Usage: %s inputfile.mj2 outputfile.yuv\n",argv[0]); 
    return 1;
  }
  
  file = fopen(argv[1], "rb");
  
  if (!file) {
    fprintf(stderr, "failed to open %s for reading\n", argv[1]);
    return 1;
  }
	
  /* Checking output file */
  outfile = fopen(argv[2], "w");
  if (!file) {
    fprintf(stderr, "failed to open %s for writing\n", argv[2]);
    return 1;
  }
  fclose(outfile);
	
	/*
	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 = NULL;
	
	/* get a MJ2 decompressor handle */
	dinfo = mj2_create_decompress();
	movie = (opj_mj2_t*)dinfo->mj2_handle;
	
	/* catch events using our callbacks and give a local context */
	opj_set_event_mgr((opj_common_ptr)dinfo, &event_mgr, stderr);		

	memset(&mj2_parameters, 0, sizeof(mj2_dparameters_t));
	/* set J2K decoding parameters to default values */
	opj_set_default_decoder_parameters(&mj2_parameters.j2k_parameters);
	
	/* setup the decoder decoding parameters using user parameters */
	mj2_setup_decoder(movie, &mj2_parameters);
			
  if (mj2_read_struct(file, movie)) /* Creating the movie structure */
    return 1;	
	
  /* Decode first video track */
	for (tnum=0; tnum < (unsigned int)(movie->num_htk + movie->num_stk + movie->num_vtk); tnum++) {
		if (movie->tk[tnum].track_type == 0) 
			break;
	}
	
	if (movie->tk[tnum].track_type != 0) {
		printf("Error. Movie does not contain any video track\n");
		return 1;
	}
	
  track = &movie->tk[tnum];
	
  /* Output info on first video tracl */
  fprintf(stdout,"The first video track contains %d frames.\nWidth: %d, Height: %d \n\n",
    track->num_samples, track->w, track->h);
	
	max_codstrm_size = track->sample[0].sample_size-8;
	frame_codestream = (unsigned char*) malloc(max_codstrm_size * sizeof(unsigned char)); 

	numframes = track->num_samples;
	
  for (snum=0; snum < numframes; snum++)
  {
		double init_time = opj_clock();
		double elapsed_time;

    sample = &track->sample[snum];
		if (sample->sample_size-8 > max_codstrm_size) {
			max_codstrm_size =  sample->sample_size-8;
			if ((frame_codestream = (unsigned char*)
				realloc(frame_codestream, max_codstrm_size)) == NULL) {
				printf("Error reallocation memory\n");
				return 1;
			}; 		
		}
    fseek(file,sample->offset+8,SEEK_SET);
    fread(frame_codestream, sample->sample_size-8, 1, file);  /* Assuming that jp and ftyp markers size do */
		
		/* open a byte stream */
		cio = opj_cio_open((opj_common_ptr)dinfo, frame_codestream, sample->sample_size-8);
		
		img = opj_decode(dinfo, cio); /* Decode J2K to image */

#ifdef WANT_SYCC_TO_RGB
	if(img->color_space == CLRSPC_SYCC)
  {
	color_sycc_to_rgb(img);
  }
#endif

	if(img->icc_profile_buf)
  {
#if defined(OPJ_HAVE_LIBLCMS1) || defined(OPJ_HAVE_LIBLCMS2)
	color_apply_icc_profile(img);
#endif

	free(img->icc_profile_buf);
	img->icc_profile_buf = NULL; img->icc_profile_len = 0;
  }

    if (((img->numcomps == 3) && (img->comps[0].dx == img->comps[1].dx / 2) 
      && (img->comps[0].dx == img->comps[2].dx / 2 ) && (img->comps[0].dx == 1)) 
      || (img->numcomps == 1)) {
      
      if (!imagetoyuv(img, argv[2]))	/* Convert image to YUV */
				return 1;
    }
    else if ((img->numcomps == 3) && 
      (img->comps[0].dx == 1) && (img->comps[1].dx == 1)&&
      (img->comps[2].dx == 1))/* If YUV 4:4:4 input --> to bmp */
    {
      fprintf(stdout,"The frames will be output in a bmp format (output_1.bmp, ...)\n");
      sprintf(outfilename,"output_%d.bmp",snum);
      if (imagetobmp(img, outfilename))	/* Convert image to BMP */
				return 1;
      
    }
    else {
      fprintf(stdout,"Image component dimensions are unknown. Unable to output image\n");
      fprintf(stdout,"The frames will be output in a j2k file (output_1.j2k, ...)\n");
			
      sprintf(outfilename,"output_%d.j2k",snum);
      outfile = fopen(outfilename, "wb");
      if (!outfile) {
				fprintf(stderr, "failed to open %s for writing\n",outfilename);
				return 1;
      }
      fwrite(frame_codestream,sample->sample_size-8,1,outfile);
      fclose(outfile);
    }
		/* close the byte stream */
		opj_cio_close(cio);	
		/* free image data structure */
		opj_image_destroy(img);
		elapsed_time = opj_clock()-init_time;
		fprintf(stderr, "Frame number %d/%d decoded in %.2f mseconds\n", snum + 1, numframes, elapsed_time*1000);
		total_time += elapsed_time;

  }
	
	free(frame_codestream);	
  fclose(file);	

	/* free remaining structures */
	if(dinfo) {
		mj2_destroy_decompress((opj_mj2_t*)dinfo->mj2_handle);
	}
	free(dinfo);
	
	fprintf(stdout, "%d frame(s) correctly decompressed\n", snum);
	fprintf(stdout,"Total decoding time: %.2f seconds (%.1f fps)\n", total_time, (float)numframes/total_time);
		
  return 0;
}