Пример #1
0
struct opj_res opj_init(const char *fname, opj_dparameters_t *parameters) {

    struct opj_res resources;
    resources.status = 0;
    resources.image = NULL;
    FILE *fptr = fopen(fname, "rb");
    resources.l_stream = opj_stream_create_default_file_stream(fptr,1);
    resources.l_codec = opj_create_decompress(OPJ_CODEC_JP2);
    if(!resources.l_stream) { resources.status = 1; }
    if(!opj_setup_decoder(resources.l_codec, parameters)) {
        opj_stream_destroy(resources.l_stream);
        opj_destroy_codec(resources.l_codec);
        resources.status = 2;
    }

    if(!opj_read_header(resources.l_stream, resources.l_codec, &(resources.image))) {
        opj_stream_destroy(resources.l_stream);
        opj_destroy_codec(resources.l_codec);
        opj_image_destroy(resources.image);
        resources.status = 3;
    }

/*    opj_set_info_handler(resources.l_codec, info_callback,00);
    opj_set_warning_handler(resources.l_codec, warning_callback,00);
    opj_set_error_handler(resources.l_codec, error_callback,00);*/
    return resources;
}
Пример #2
0
struct opj_res opj_init(const char *fname, opj_dparameters_t *parameters) {

	struct opj_res resources = opj_init_res();

	resources.l_stream = opj_stream_create_default_file_stream(fname,1);
	if(!resources.l_stream) { 
		resources.status = 1; 
		return resources;
	}
	if(strstr(fname, ".jp2") != NULL) {
		resources.status = opj_init_from_stream(parameters, &resources, OPJ_CODEC_JP2);
	} else if(strstr(fname, ".j2k") != NULL) {
		resources.status = opj_init_from_stream(parameters, &resources, OPJ_CODEC_J2K);
	} else {
		resources.status = 1;
	}
	return resources;
}
Пример #3
0
struct opj_res opj_init(const char *fname, opj_dparameters_t *parameters) {

	struct opj_res resources = opj_init_res();
	FILE *fptr = fopen(fname, "rb");
	if(fptr == NULL) {
		resources.status = 1;
		return resources;
	}

	resources.open_file = fptr;
	resources.l_stream = opj_stream_create_default_file_stream(fptr,1);
	if(!resources.l_stream) { 
		resources.status = 1; 
		return resources;
	}
	resources.status = opj_init_from_stream(parameters, &resources);
	return resources;
}
/* -------------------------------------------------------------------------- */
int main(int argc, char **argv)
{
	FILE *fsrc = NULL;

	opj_dparameters_t parameters;			/* decompression parameters */
	opj_image_t* image = NULL;
	opj_stream_t *l_stream = NULL;				/* Stream */
	opj_codec_t* l_codec = NULL;				/* Handle to a decompressor */
	opj_codestream_index_t* cstr_index = NULL;

	char indexfilename[OPJ_PATH_LEN];	/* index file name */

	OPJ_INT32 num_images, imageno;
	img_fol_t img_fol;
	dircnt_t *dirptr = NULL;
  int failed = 0;

	/* set decoding parameters to default values */
	opj_set_default_decoder_parameters(&parameters);

	/* FIXME Initialize indexfilename and img_fol */
	*indexfilename = 0;

	/* Initialize img_fol */
	memset(&img_fol,0,sizeof(img_fol_t));

	/* parse input and get user encoding parameters */
	if(parse_cmdline_decoder(argc, argv, &parameters,&img_fol, indexfilename) == 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((size_t)num_images*OPJ_PATH_LEN*sizeof(char));	/* Stores at max 10 image file names*/
			dirptr->filename = (char**) malloc((size_t)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;
	}

	/*Decoding 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, &parameters)) {
				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;
		}

		/* decode 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, &parameters) ){
			fprintf(stderr, "ERROR -> opj_compress: failed to setup the decoder\n");
			opj_stream_destroy(l_stream);
			fclose(fsrc);
			opj_destroy_codec(l_codec);
			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_decompress: failed to read the header\n");
			opj_stream_destroy(l_stream);
			fclose(fsrc);
			opj_destroy_codec(l_codec);
			opj_image_destroy(image);
			return EXIT_FAILURE;
		}

		if (!parameters.nb_tile_to_decode) {
			/* Optional if you want decode the entire image */
			if (!opj_set_decode_area(l_codec, image, (OPJ_INT32)parameters.DA_x0,
					(OPJ_INT32)parameters.DA_y0, (OPJ_INT32)parameters.DA_x1, (OPJ_INT32)parameters.DA_y1)){
				fprintf(stderr,	"ERROR -> opj_decompress: failed to set the decoded area\n");
				opj_stream_destroy(l_stream);
				opj_destroy_codec(l_codec);
				opj_image_destroy(image);
				fclose(fsrc);
				return EXIT_FAILURE;
			}

			/* Get the decoded image */
			if (!(opj_decode(l_codec, l_stream, image) && opj_end_decompress(l_codec,	l_stream))) {
				fprintf(stderr,"ERROR -> opj_decompress: failed to decode image!\n");
				opj_destroy_codec(l_codec);
				opj_stream_destroy(l_stream);
				opj_image_destroy(image);
				fclose(fsrc);
				return EXIT_FAILURE;
			}
		}
		else {

			/* It is just here to illustrate how to use the resolution after set parameters */
			/*if (!opj_set_decoded_resolution_factor(l_codec, 5)) {
				fprintf(stderr, "ERROR -> opj_decompress: failed to set the resolution factor tile!\n");
				opj_destroy_codec(l_codec);
				opj_stream_destroy(l_stream);
				opj_image_destroy(image);
				fclose(fsrc);
				return EXIT_FAILURE;
			}*/

			if (!opj_get_decoded_tile(l_codec, l_stream, image, parameters.tile_index)) {
				fprintf(stderr, "ERROR -> opj_decompress: failed to decode tile!\n");
				opj_destroy_codec(l_codec);
				opj_stream_destroy(l_stream);
				opj_image_destroy(image);
				fclose(fsrc);
				return EXIT_FAILURE;
			}
			fprintf(stdout, "tile %d is decoded!\n\n", parameters.tile_index);
		}

		/* Close the byte stream */
		opj_stream_destroy(l_stream);
		fclose(fsrc);

		if(image->color_space == OPJ_CLRSPC_SYCC){
			color_sycc_to_rgb(image); /* FIXME */
		}
		
		if( image->color_space != OPJ_CLRSPC_SYCC 
			&& image->numcomps == 3 && image->comps[0].dx == image->comps[0].dy
			&& image->comps[1].dx != 1 )
			image->color_space = OPJ_CLRSPC_SYCC;
		else if (image->numcomps <= 2)
			image->color_space = OPJ_CLRSPC_GRAY;

		if(image->icc_profile_buf) {
#if defined(OPJ_HAVE_LIBLCMS1) || defined(OPJ_HAVE_LIBLCMS2)
			color_apply_icc_profile(image); /* FIXME */
#endif
			free(image->icc_profile_buf);
			image->icc_profile_buf = NULL; image->icc_profile_len = 0;
		}

		/* create output image */
		/* ------------------- */
		switch (parameters.cod_format) {
		case PXM_DFMT:			/* PNM PGM PPM */
			if (imagetopnm(image, parameters.outfile)) {
				fprintf(stderr,"Outfile %s not generated\n",parameters.outfile);
        failed = 1;
			}
			else {
				fprintf(stdout,"Generated Outfile %s\n",parameters.outfile);
			}
			break;

		case PGX_DFMT:			/* PGX */
			if(imagetopgx(image, parameters.outfile)){
				fprintf(stderr,"Outfile %s not generated\n",parameters.outfile);
        failed = 1;
			}
			else {
				fprintf(stdout,"Generated Outfile %s\n",parameters.outfile);
			}
			break;

		case BMP_DFMT:			/* BMP */
			if(imagetobmp(image, parameters.outfile)){
				fprintf(stderr,"Outfile %s not generated\n",parameters.outfile);
        failed = 1;
			}
			else {
				fprintf(stdout,"Generated Outfile %s\n",parameters.outfile);
			}
			break;
#ifdef OPJ_HAVE_LIBTIFF
		case TIF_DFMT:			/* TIFF */
			if(imagetotif(image, parameters.outfile)){
				fprintf(stderr,"Outfile %s not generated\n",parameters.outfile);
        failed = 1;
			}
			else {
				fprintf(stdout,"Generated Outfile %s\n",parameters.outfile);
			}
			break;
#endif /* OPJ_HAVE_LIBTIFF */
		case RAW_DFMT:			/* RAW */
			if(imagetoraw(image, parameters.outfile)){
				fprintf(stderr,"Error generating raw file. Outfile %s not generated\n",parameters.outfile);
        failed = 1;
			}
			else {
				fprintf(stdout,"Successfully generated Outfile %s\n",parameters.outfile);
			}
			break;

		case RAWL_DFMT:			/* RAWL */
			if(imagetorawl(image, parameters.outfile)){
				fprintf(stderr,"Error generating rawl file. Outfile %s not generated\n",parameters.outfile);
        failed = 1;
			}
			else {
				fprintf(stdout,"Successfully generated Outfile %s\n",parameters.outfile);
			}
			break;

		case TGA_DFMT:			/* TGA */
			if(imagetotga(image, parameters.outfile)){
				fprintf(stderr,"Error generating tga file. Outfile %s not generated\n",parameters.outfile);
        failed = 1;
			}
			else {
				fprintf(stdout,"Successfully generated Outfile %s\n",parameters.outfile);
			}
			break;
#ifdef OPJ_HAVE_LIBPNG
		case PNG_DFMT:			/* PNG */
			if(imagetopng(image, parameters.outfile)){
				fprintf(stderr,"Error generating png file. Outfile %s not generated\n",parameters.outfile);
        failed = 1;
			}
			else {
				fprintf(stdout,"Successfully generated Outfile %s\n",parameters.outfile);
			}
			break;
#endif /* OPJ_HAVE_LIBPNG */
/* Can happen if output file is TIFF or PNG
 * and OPJ_HAVE_LIBTIF or OPJ_HAVE_LIBPNG is undefined
*/
			default:
				fprintf(stderr,"Outfile %s not generated\n",parameters.outfile);
        failed = 1;
		}

		/* free remaining structures */
		if (l_codec) {
			opj_destroy_codec(l_codec);
		}


		/* free image data structure */
		opj_image_destroy(image);

		/* destroy the codestream index */
		opj_destroy_cstr_index(&cstr_index);

	}
	return failed ? EXIT_FAILURE : EXIT_SUCCESS;
}
Пример #5
0
int main(int argc, char *argv[])
{
  int ret;
  opj_dparameters_t parameters;  /* decompression parameters */
  img_fol_t img_fol;
  opj_image_t *image = NULL;
  FILE *fsrc = NULL;
  bool bResult;
  int num_images;
  int i,imageno;
  dircnt_t *dirptr;
  opj_codec_t* dinfo = NULL;  /* handle to a decompressor */
  opj_stream_t *cio = NULL;
  opj_codestream_info_t cstr_info;  /* Codestream information structure */
  char indexfilename[OPJ_PATH_LEN];  /* index file name */
  OPJ_INT32 l_tile_x0,l_tile_y0;
  OPJ_UINT32 l_tile_width,l_tile_height,l_nb_tiles_x,l_nb_tiles_y;

  /* configure the event callbacks (not required) */

  /* set decoding parameters to default values */
  opj_set_default_decoder_parameters(&parameters);

  /* Initialize indexfilename and img_fol */
  *indexfilename = 0;
  memset(&img_fol,0,sizeof(img_fol_t));

  /* parse input and get user encoding parameters */
  if(parse_cmdline_decoder(argc, argv, &parameters,&img_fol, indexfilename) == 1) {
    return EXIT_FAILURE;
  }

  /* Initialize reading of directory */
  if(img_fol.set_imgdir==1){
    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(i=0;i<num_images;i++){
        dirptr->filename[i] = dirptr->filename_buf + i*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;
  }

  /*Encoding image one by one*/
  for(imageno = 0; imageno < num_images ; imageno++)
  {
    image = NULL;
    fprintf(stderr,"\n");

    if(img_fol.set_imgdir==1){
      if (get_next_file(imageno, dirptr,&img_fol, &parameters)) {
        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;
    }
    cio = opj_stream_create_default_file_stream(fsrc,true);
    /* decode the code-stream */
    /* ---------------------- */

    switch (parameters.decod_format)
    {
      case J2K_CFMT:
      {
        /* JPEG-2000 codestream */

        /* get a decoder handle */
        dinfo = opj_create_decompress(CODEC_J2K);
        break;
      }
      case JP2_CFMT:
      {
        /* JPEG 2000 compressed image data */
        /* get a decoder handle */
        dinfo = opj_create_decompress(CODEC_JP2);
        break;
      }
      case JPT_CFMT:
      {
        /* JPEG 2000, JPIP */
        /* get a decoder handle */
        dinfo = opj_create_decompress(CODEC_JPT);
        break;
      }
      default:
        fprintf(stderr, "skipping file..\n");
        opj_stream_destroy(cio);
        continue;
    }
    /* catch events using our callbacks and give a local context */

    /* setup the decoder decoding parameters using user parameters */
    opj_setup_decoder(dinfo, &parameters);

    /* 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
      */
    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);
    //image = opj_decode(dinfo, cio);
    //bResult = bResult && (image != 00);
    //bResult = bResult && opj_end_decompress(dinfo,cio);
    //if
    //  (!image)
    //{
    //  fprintf(stderr, "ERROR -> j2k_to_image: failed to decode image!\n");
    //  opj_destroy_codec(dinfo);
    //  opj_stream_destroy(cio);
    //  fclose(fsrc);
    //  return EXIT_FAILURE;
    //}
    /* dump image */
    if(!image)
      {
      fprintf(stderr, "ERROR -> j2k_to_image: failed to read header\n");
      return EXIT_FAILURE;
      }
    j2k_dump_image(stdout, image);

    /* dump cp */
    //j2k_dump_cp(stdout, image, dinfo->m_codec);

    /* 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;
      }
    }

    /* free remaining structures */
    if (dinfo) {
      opj_destroy_codec(dinfo);
    }
    /* free codestream information structure */
    if (*indexfilename)
      opj_destroy_cstr_info(&cstr_info);
    /* free image data structure */
    opj_image_destroy(image);

  }

  return ret;
}
Пример #6
0
JP2KImage  read_jp2k_data(const char* fname, int format)
#endif
{
	JP2KImage jp;
	init_jp2k(&jp);

	opj_stream_t* stream = NULL;
	opj_codec_t*  codec  = NULL;

	opj_dparameters_t parameters;	
	opj_set_default_decoder_parameters(&parameters);

#if OPENJPEG_VER < JP2K_VER_21
	stream = opj_stream_create_default_file_stream(fp, 1);		// 2.0.0
#else
	stream = opj_stream_create_default_file_stream(fname, 1);	// 2.1.0
#endif

	if (stream==NULL){
		jp.state = ERROR_GRAPH_RDFILE;
		return jp;
	}

	if (format==JP2K_FMT_J2K) {			// JPEG 2000 codestream
		codec = opj_create_decompress(OPJ_CODEC_J2K);
	}	
	else if (format==JP2K_FMT_JP2) {	// JPEG 2000 compressed image data
		codec = opj_create_decompress(OPJ_CODEC_JP2);
	}
	else if (format==JP2K_FMT_JPT) {	// JPEG 2000 JPIP
		codec = opj_create_decompress(OPJ_CODEC_JPT);
	}
	else {
		print_message("JBXL::readJPEG2KData: ERROR: unknown file format!\n");
		opj_stream_destroy(stream);
		return jp;
	}

	if (!opj_setup_decoder(codec, &parameters) ){
		opj_stream_destroy(stream);
		opj_destroy_codec(codec);
		jp.state = ERROR_GRAPH;
		return jp;
	}
	if (!opj_read_header(stream, codec, &jp.image)){
		opj_stream_destroy(stream);
		opj_destroy_codec(codec);
		jp.state = ERROR_GRAPH;
		return jp;
	}
	if (!opj_set_decode_area(codec, jp.image, 0, 0, 0, 0)){
		opj_stream_destroy(stream);
		opj_destroy_codec(codec);
		free_jp2k(&jp);
		jp.state = ERROR_GRAPH;
		return jp;
	}
	if (!(opj_decode(codec, stream, jp.image) && opj_end_decompress(codec, stream))) {
		opj_destroy_codec(codec);
		opj_stream_destroy(stream);
		free_jp2k(&jp);
		jp.state = ERROR_GRAPH;
		return jp;
	}

	setup_jp2k(&jp);

	opj_stream_destroy(stream);
	opj_destroy_codec(codec);

	return jp;
}
Пример #7
0
int main (int argc, char *argv[])
{
	opj_dparameters_t l_param;
	opj_codec_t * l_codec;
	opj_image_t * l_image;
	FILE * l_file;
	opj_stream_t * l_stream;
	OPJ_UINT32 l_data_size;
	OPJ_UINT32 l_max_data_size = 1000;
	OPJ_UINT32 l_tile_index;
	OPJ_BYTE * l_data = (OPJ_BYTE *) malloc(1000);
	opj_bool l_go_on = OPJ_TRUE;
	OPJ_INT32 l_tile_x0,l_tile_y0;
	OPJ_UINT32 l_tile_width,l_tile_height,l_nb_tiles_x,l_nb_tiles_y,l_nb_comps;
	OPJ_INT32 l_current_tile_x0,l_current_tile_y0,l_current_tile_x1,l_current_tile_y1;

  int da_x0=0;
  int da_y0=0;
  int da_x1=1000;
  int da_y1=1000;
  char input_file[64];
	
  /* should be test_tile_decoder 0 0 1000 1000 tte1.j2k */
  if( argc == 6 )
    {
    da_x0=atoi(argv[1]);
    da_y0=atoi(argv[2]);
    da_x1=atoi(argv[3]);
    da_y1=atoi(argv[4]);
    strcpy(input_file,argv[5]);
    }
  else
    {
    da_x0=0;
    da_y0=0;
    da_x1=1000;
    da_y1=1000;
    strcpy(input_file,"test.j2k");
    }

	if
		(! l_data)
	{
		return 1;
	}
	opj_set_default_decoder_parameters(&l_param);

	/** you may here add custom decoding parameters */
	/* do not use layer decoding limitations */
	l_param.cp_layer = 0;

	/* do not use resolutions reductions */
	l_param.cp_reduce = 0;

	/* to decode only a part of the image data */
	//opj_restrict_decoding(&l_param,0,0,1000,1000);
	
	l_codec = opj_create_decompress_v2(CODEC_J2K);
	if
		(! l_codec)
	{
		free(l_data);
		return 1;
	}

	/* 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);
	
	if
		(! opj_setup_decoder_v2(l_codec,&l_param))
	{
		free(l_data);
		opj_destroy_codec(l_codec);
		return 1;
	}
	
	l_file = fopen(input_file,"rb");
	if
		(! l_file)
	{
		fprintf(stdout, "Error opening input file\n");
		free(l_data);
		opj_destroy_codec(l_codec);
		return 1;
	}

	l_stream = opj_stream_create_default_file_stream(l_file,OPJ_TRUE);

	if
		(! opj_read_header(l_stream, l_codec, &l_image))
	{
		free(l_data);
		opj_stream_destroy(l_stream);
		fclose(l_file);
		opj_destroy_codec(l_codec);
		return 1;
	}
	printf("Setting decoding area to %d,%d,%d,%d\n", da_x0, da_y0, da_x1, da_y1);
	opj_set_decode_area(l_codec, l_image, da_x0, da_y0, da_x1, da_y1);
	while
		(l_go_on)
	{
		if
			(! opj_read_tile_header(
						l_codec,
            l_stream,
						&l_tile_index,
						&l_data_size,
						&l_current_tile_x0,
						&l_current_tile_y0,
						&l_current_tile_x1,
						&l_current_tile_y1,
						&l_nb_comps,
						&l_go_on))
		{
			free(l_data);
			opj_stream_destroy(l_stream);
			fclose(l_file);
			opj_destroy_codec(l_codec);
			opj_image_destroy(l_image);
			return 1;
		}
		if
			(l_go_on)
		{
			if
				(l_data_size > l_max_data_size)
			{
				l_data = (OPJ_BYTE *) realloc(l_data,l_data_size);
				if
					(! l_data)
				{
					opj_stream_destroy(l_stream);
					fclose(l_file);
					opj_destroy_codec(l_codec);
					opj_image_destroy(l_image);
					return 1;
				}
				l_max_data_size = l_data_size;
			}

			if
				(! opj_decode_tile_data(l_codec,l_tile_index,l_data,l_data_size,l_stream))
			{
				free(l_data);
				opj_stream_destroy(l_stream);
				fclose(l_file);
				opj_destroy_codec(l_codec);
				opj_image_destroy(l_image);
				return 1;
			}
			/** now should inspect image to know the reduction factor and then how to behave with data */
		}
	}
	if
		(! opj_end_decompress(l_codec,l_stream))
	{
		free(l_data);
		opj_stream_destroy(l_stream);
		fclose(l_file);
		opj_destroy_codec(l_codec);
		opj_image_destroy(l_image);
		return 1;
	}
	free(l_data);
	opj_stream_destroy(l_stream);
	fclose(l_file);
	opj_destroy_codec(l_codec);
	opj_image_destroy(l_image);

	// Print profiling
	//PROFPRINT();

	return 0;
}
int main (int argc, char *argv[])
{
	opj_cparameters_t l_param;
	opj_codec_t * l_codec;
	opj_image_t * l_image;
	opj_image_cmptparm_t l_params [NUM_COMPS_MAX];
	FILE * l_file;
	opj_stream_t * l_stream;
	OPJ_UINT32 l_nb_tiles;
	OPJ_UINT32 l_data_size;
	unsigned char len;

#ifdef USING_MCT
	const OPJ_FLOAT32 l_mct [] =
	{
		1 , 0 , 0 ,
		0 , 1 , 0 ,
		0 , 0 , 1
	};

	const OPJ_INT32 l_offsets [] =
	{
		128 , 128 , 128
	};
#endif

	opj_image_cmptparm_t * l_current_param_ptr;
	OPJ_UINT32 i;
	OPJ_BYTE *l_data;

  OPJ_UINT32 num_comps;
  int image_width;
  int image_height;
  int tile_width;
  int tile_height;
  int comp_prec;
  int irreversible;
  char output_file[64];

  /* should be test_tile_encoder 3 2000 2000 1000 1000 8 tte1.j2k */
  if( argc == 9 )
    {
    num_comps = (OPJ_UINT32)atoi( argv[1] );
    image_width = atoi( argv[2] );
    image_height = atoi( argv[3] );
    tile_width = atoi( argv[4] );
    tile_height = atoi( argv[5] );
    comp_prec = atoi( argv[6] );
    irreversible = atoi( argv[7] );
    strcpy(output_file, argv[8] );
    }
  else
    {
    num_comps = 3;
    image_width = 2000;
    image_height = 2000;
    tile_width = 1000;
    tile_height = 1000;
    comp_prec = 8;
    irreversible = 1;
    strcpy(output_file, "test.j2k" );
    }
  if( num_comps > NUM_COMPS_MAX )
    {
    return 1;
    }
	l_nb_tiles = (OPJ_UINT32)(image_width/tile_width) * (OPJ_UINT32)(image_height/tile_height);
	l_data_size = (OPJ_UINT32)tile_width * (OPJ_UINT32)tile_height * (OPJ_UINT32)num_comps * (OPJ_UINT32)(comp_prec/8);

	l_data = (OPJ_BYTE*) malloc(l_data_size * sizeof(OPJ_BYTE));

	fprintf(stdout, "Encoding random values -> keep in mind that this is very hard to compress\n");
	for (i=0;i<l_data_size;++i)	{
		l_data[i] = (OPJ_BYTE)i; /*rand();*/
	}

	opj_set_default_encoder_parameters(&l_param);
	/** you may here add custom encoding parameters */
	/* rate specifications */
	/** number of quality layers in the stream */
	l_param.tcp_numlayers = 1;
	l_param.cp_fixed_quality = 1;
	l_param.tcp_distoratio[0] = 20;
	/* is using others way of calculation */
	/* l_param.cp_disto_alloc = 1 or l_param.cp_fixed_alloc = 1 */
	/* l_param.tcp_rates[0] = ... */


	/* tile definitions parameters */
	/* position of the tile grid aligned with the image */
	l_param.cp_tx0 = 0;
	l_param.cp_ty0 = 0;
	/* tile size, we are using tile based encoding */
	l_param.tile_size_on = OPJ_TRUE;
	l_param.cp_tdx = tile_width;
	l_param.cp_tdy = tile_height;

	/* use irreversible encoding ?*/
	l_param.irreversible = irreversible;

	/* do not bother with mct, the rsiz is set when calling opj_set_MCT*/
	/*l_param.cp_rsiz = OPJ_STD_RSIZ;*/

	/* no cinema */
	/*l_param.cp_cinema = 0;*/

	/* no not bother using SOP or EPH markers, do not use custom size precinct */
	/* number of precincts to specify */
	/* l_param.csty = 0;*/
	/* l_param.res_spec = ... */
	/* l_param.prch_init[i] = .. */
	/* l_param.prcw_init[i] = .. */


	/* do not use progression order changes */
	/*l_param.numpocs = 0;*/
	/* l_param.POC[i].... */

	/* do not restrain the size for a component.*/
	/* l_param.max_comp_size = 0; */

	/** block encoding style for each component, do not use at the moment */
	/** J2K_CCP_CBLKSTY_TERMALL, J2K_CCP_CBLKSTY_LAZY, J2K_CCP_CBLKSTY_VSC, J2K_CCP_CBLKSTY_SEGSYM, J2K_CCP_CBLKSTY_RESET */
	/* l_param.mode = 0;*/

	/** number of resolutions */
	l_param.numresolution = 6;

	/** progression order to use*/
	/** OPJ_LRCP, OPJ_RLCP, OPJ_RPCL, PCRL, CPRL */
	l_param.prog_order = OPJ_LRCP;

	/** no "region" of interest, more precisally component */
	/* l_param.roi_compno = -1; */
	/* l_param.roi_shift = 0; */

	/* we are not using multiple tile parts for a tile. */
	/* l_param.tp_on = 0; */
	/* l_param.tp_flag = 0; */

	/* if we are using mct */
#ifdef USING_MCT
	opj_set_MCT(&l_param,l_mct,l_offsets,NUM_COMPS);
#endif


	/* image definition */
	l_current_param_ptr = l_params;
	for (i=0;i<num_comps;++i) {
		/* do not bother bpp useless */
		/*l_current_param_ptr->bpp = COMP_PREC;*/
		l_current_param_ptr->dx = 1;
		l_current_param_ptr->dy = 1;

		l_current_param_ptr->h = (OPJ_UINT32)image_height;
		l_current_param_ptr->w = (OPJ_UINT32)image_width;

		l_current_param_ptr->sgnd = 0;
		l_current_param_ptr->prec = (OPJ_UINT32)comp_prec;

		l_current_param_ptr->x0 = 0;
		l_current_param_ptr->y0 = 0;

		++l_current_param_ptr;
	}

  /* should we do j2k or jp2 ?*/
  len = (unsigned char)strlen( output_file );
  if( strcmp( output_file + len - 4, ".jp2" ) == 0 )
    {
    l_codec = opj_create_compress(OPJ_CODEC_JP2);
    }
  else
    {
    l_codec = opj_create_compress(OPJ_CODEC_J2K);
    }
	if (!l_codec) {
		return 1;
	}

	/* 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);

	l_image = opj_image_tile_create(num_comps,l_params,OPJ_CLRSPC_SRGB);
	if (! l_image) {
		opj_destroy_codec(l_codec);
		return 1;
	}

	l_image->x0 = 0;
	l_image->y0 = 0;
	l_image->x1 = (OPJ_UINT32)image_width;
	l_image->y1 = (OPJ_UINT32)image_height;
	l_image->color_space = OPJ_CLRSPC_SRGB;

	if (! opj_setup_encoder(l_codec,&l_param,l_image)) {
		fprintf(stderr, "ERROR -> test_tile_encoder: failed to setup the codec!\n");
		opj_destroy_codec(l_codec);
		opj_image_destroy(l_image);
		return 1;
	}

	l_file = fopen(output_file,"wb");
	if (! l_file) {
		fprintf(stderr, "ERROR -> test_tile_encoder: failed to create the output file!\n");
		opj_destroy_codec(l_codec);
		opj_image_destroy(l_image);
		return 1;
	}

	l_stream = opj_stream_create_default_file_stream(l_file, OPJ_FALSE);
    if (! l_stream) {
		fprintf(stderr, "ERROR -> test_tile_encoder: failed to create the stream from the output file %s !\n",output_file );
		fclose(l_file);
		opj_destroy_codec(l_codec);
		opj_image_destroy(l_image);
		return 1;
	}

	if (! opj_start_compress(l_codec,l_image,l_stream)) {
		fprintf(stderr, "ERROR -> test_tile_encoder: failed to start compress!\n");
		opj_stream_destroy(l_stream);
		fclose(l_file);
		opj_destroy_codec(l_codec);
		opj_image_destroy(l_image);
		return 1;
	}

	for (i=0;i<l_nb_tiles;++i) {
		if (! opj_write_tile(l_codec,i,l_data,l_data_size,l_stream)) {
			fprintf(stderr, "ERROR -> test_tile_encoder: failed to write the tile %d!\n",i);
			opj_stream_destroy(l_stream);
			fclose(l_file);
			opj_destroy_codec(l_codec);
			opj_image_destroy(l_image);
			return 1;
		}
	}

	if (! opj_end_compress(l_codec,l_stream)) {
		fprintf(stderr, "ERROR -> test_tile_encoder: failed to end compress!\n");
		opj_stream_destroy(l_stream);
		fclose(l_file);
		opj_destroy_codec(l_codec);
		opj_image_destroy(l_image);
		return 1;
	}

	opj_stream_destroy(l_stream);
	fclose(l_file);
	opj_destroy_codec(l_codec);
	opj_image_destroy(l_image);

	free(l_data);

	/* Print profiling*/
	/*PROFPRINT();*/

	return 0;
}
Пример #9
0
/* -------------------------------------------------------------------------- */
int main(int argc, char **argv)
{
	opj_decompress_parameters parameters;			/* decompression parameters */
	opj_image_t* image = NULL;
	opj_stream_t *l_stream = NULL;				/* Stream */
	opj_codec_t* l_codec = NULL;				/* Handle to a decompressor */
	opj_codestream_index_t* cstr_index = NULL;

	char indexfilename[OPJ_PATH_LEN];	/* index file name */

	OPJ_INT32 num_images, imageno;
	img_fol_t img_fol;
	dircnt_t *dirptr = NULL;
  int failed = 0;
  OPJ_FLOAT64 t, tCumulative = 0;
  OPJ_UINT32 numDecompressedImages = 0;

	/* set decoding parameters to default values */
	set_default_parameters(&parameters);

	/* FIXME Initialize indexfilename and img_fol */
	*indexfilename = 0;

	/* Initialize img_fol */
	memset(&img_fol,0,sizeof(img_fol_t));

	/* parse input and get user encoding parameters */
	if(parse_cmdline_decoder(argc, argv, &parameters,&img_fol, indexfilename) == 1) {
		destroy_parameters(&parameters);
		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((size_t)num_images*OPJ_PATH_LEN*sizeof(char));	/* Stores at max 10 image file names*/
			dirptr->filename = (char**) malloc((size_t)num_images*sizeof(char*));

			if(!dirptr->filename_buf){
				destroy_parameters(&parameters);
				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){
			destroy_parameters(&parameters);
			return EXIT_FAILURE;
		}
		if (num_images==0){
			fprintf(stdout,"Folder is empty\n");
			destroy_parameters(&parameters);
			return EXIT_FAILURE;
		}
	}else{
		num_images=1;
	}

	/*Decoding 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, &parameters)) {
				fprintf(stderr,"skipping file...\n");
				destroy_parameters(&parameters);
				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);
			destroy_parameters(&parameters);
			return EXIT_FAILURE;
		}

		/* decode 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");
				destroy_parameters(&parameters);
				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);

		t = opj_clock();

		/* Setup the decoder decoding parameters using user parameters */
		if ( !opj_setup_decoder(l_codec, &(parameters.core)) ){
			fprintf(stderr, "ERROR -> opj_decompress: failed to setup the decoder\n");
			destroy_parameters(&parameters);
			opj_stream_destroy(l_stream);
			opj_destroy_codec(l_codec);
			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_decompress: failed to read the header\n");
			destroy_parameters(&parameters);
			opj_stream_destroy(l_stream);
			opj_destroy_codec(l_codec);
			opj_image_destroy(image);
			return EXIT_FAILURE;
		}

		if (!parameters.nb_tile_to_decode) {
			/* Optional if you want decode the entire image */
			if (!opj_set_decode_area(l_codec, image, (OPJ_INT32)parameters.DA_x0,
					(OPJ_INT32)parameters.DA_y0, (OPJ_INT32)parameters.DA_x1, (OPJ_INT32)parameters.DA_y1)){
				fprintf(stderr,	"ERROR -> opj_decompress: failed to set the decoded area\n");
				destroy_parameters(&parameters);
				opj_stream_destroy(l_stream);
				opj_destroy_codec(l_codec);
				opj_image_destroy(image);
				return EXIT_FAILURE;
			}

			/* Get the decoded image */
			if (!(opj_decode(l_codec, l_stream, image) && opj_end_decompress(l_codec,	l_stream))) {
				fprintf(stderr,"ERROR -> opj_decompress: failed to decode image!\n");
				destroy_parameters(&parameters);
				opj_destroy_codec(l_codec);
				opj_stream_destroy(l_stream);
				opj_image_destroy(image);
				return EXIT_FAILURE;
			}
		}
		else {

			/* It is just here to illustrate how to use the resolution after set parameters */
			/*if (!opj_set_decoded_resolution_factor(l_codec, 5)) {
				fprintf(stderr, "ERROR -> opj_decompress: failed to set the resolution factor tile!\n");
				opj_destroy_codec(l_codec);
				opj_stream_destroy(l_stream);
				opj_image_destroy(image);
				return EXIT_FAILURE;
			}*/

			if (!opj_get_decoded_tile(l_codec, l_stream, image, parameters.tile_index)) {
				fprintf(stderr, "ERROR -> opj_decompress: failed to decode tile!\n");
				destroy_parameters(&parameters);
				opj_destroy_codec(l_codec);
				opj_stream_destroy(l_stream);
				opj_image_destroy(image);
				return EXIT_FAILURE;
			}
			fprintf(stdout, "tile %d is decoded!\n\n", parameters.tile_index);
		}

		tCumulative += opj_clock() - t;
		numDecompressedImages++;

		/* Close the byte stream */
		opj_stream_destroy(l_stream);

		if( image->color_space != OPJ_CLRSPC_SYCC 
			&& image->numcomps == 3 && image->comps[0].dx == image->comps[0].dy
			&& image->comps[1].dx != 1 )
			image->color_space = OPJ_CLRSPC_SYCC;
		else if (image->numcomps <= 2)
			image->color_space = OPJ_CLRSPC_GRAY;

		if(image->color_space == OPJ_CLRSPC_SYCC){
			color_sycc_to_rgb(image);
		}
		else if((image->color_space == OPJ_CLRSPC_CMYK) && (parameters.cod_format != TIF_DFMT)){
			color_cmyk_to_rgb(image);
		}
		else if(image->color_space == OPJ_CLRSPC_EYCC){
			color_esycc_to_rgb(image);
		}
		
		if(image->icc_profile_buf) {
#if defined(OPJ_HAVE_LIBLCMS1) || defined(OPJ_HAVE_LIBLCMS2)
			if(image->icc_profile_len)
			 color_apply_icc_profile(image);
			else
			 color_cielab_to_rgb(image);
#endif
			free(image->icc_profile_buf);
			image->icc_profile_buf = NULL; image->icc_profile_len = 0;
		}
		
		/* Force output precision */
		/* ---------------------- */
		if (parameters.precision != NULL)
		{
			OPJ_UINT32 compno;
			for (compno = 0; compno < image->numcomps; ++compno)
			{
				OPJ_UINT32 precno = compno;
				OPJ_UINT32 prec;
				
				if (precno >= parameters.nb_precision) {
					precno = parameters.nb_precision - 1U;
				}
				
				prec = parameters.precision[precno].prec;
				if (prec == 0) {
					prec = image->comps[compno].prec;
				}
				
				switch (parameters.precision[precno].mode) {
					case OPJ_PREC_MODE_CLIP:
						clip_component(&(image->comps[compno]), prec);
						break;
					case OPJ_PREC_MODE_SCALE:
						scale_component(&(image->comps[compno]), prec);
						break;
					default:
						break;
				}
				
			}
		}
		
		/* Upsample components */
		/* ------------------- */
		if (parameters.upsample)
		{
			image = upsample_image_components(image);
			if (image == NULL) {
				fprintf(stderr, "ERROR -> opj_decompress: failed to upsample image components!\n");
				destroy_parameters(&parameters);
				opj_destroy_codec(l_codec);
				return EXIT_FAILURE;
			}
		}
		
		/* Force RGB output */
		/* ---------------- */
		if (parameters.force_rgb)
		{
			switch (image->color_space) {
				case OPJ_CLRSPC_SRGB:
					break;
				case OPJ_CLRSPC_GRAY:
					image = convert_gray_to_rgb(image);
					break;
				default:
					fprintf(stderr, "ERROR -> opj_decompress: don't know how to convert image to RGB colorspace!\n");
					opj_image_destroy(image);
					image = NULL;
					break;
			}
			if (image == NULL) {
				fprintf(stderr, "ERROR -> opj_decompress: failed to convert to RGB image!\n");
				destroy_parameters(&parameters);
				opj_destroy_codec(l_codec);
				return EXIT_FAILURE;
			}
		}

		/* create output image */
		/* ------------------- */
		switch (parameters.cod_format) {
		case PXM_DFMT:			/* PNM PGM PPM */
			if (imagetopnm(image, parameters.outfile, parameters.split_pnm)) {
                fprintf(stderr,"[ERROR] Outfile %s not generated\n",parameters.outfile);
        failed = 1;
			}
			else {
                fprintf(stdout,"[INFO] Generated Outfile %s\n",parameters.outfile);
			}
			break;

		case PGX_DFMT:			/* PGX */
			if(imagetopgx(image, parameters.outfile)){
                fprintf(stderr,"[ERROR] Outfile %s not generated\n",parameters.outfile);
        failed = 1;
			}
			else {
                fprintf(stdout,"[INFO] Generated Outfile %s\n",parameters.outfile);
			}
			break;

		case BMP_DFMT:			/* BMP */
			if(imagetobmp(image, parameters.outfile)){
                fprintf(stderr,"[ERROR] Outfile %s not generated\n",parameters.outfile);
        failed = 1;
			}
			else {
                fprintf(stdout,"[INFO] Generated Outfile %s\n",parameters.outfile);
			}
			break;
#ifdef OPJ_HAVE_LIBTIFF
		case TIF_DFMT:			/* TIFF */
			if(imagetotif(image, parameters.outfile)){
                fprintf(stderr,"[ERROR] Outfile %s not generated\n",parameters.outfile);
        failed = 1;
			}
			else {
                fprintf(stdout,"[INFO] Generated Outfile %s\n",parameters.outfile);
			}
			break;
#endif /* OPJ_HAVE_LIBTIFF */
		case RAW_DFMT:			/* RAW */
			if(imagetoraw(image, parameters.outfile)){
                fprintf(stderr,"[ERROR] Error generating raw file. Outfile %s not generated\n",parameters.outfile);
        failed = 1;
			}
			else {
                fprintf(stdout,"[INFO] Generated Outfile %s\n",parameters.outfile);
			}
			break;

		case RAWL_DFMT:			/* RAWL */
			if(imagetorawl(image, parameters.outfile)){
                fprintf(stderr,"[ERROR] Error generating rawl file. Outfile %s not generated\n",parameters.outfile);
        failed = 1;
			}
			else {
                fprintf(stdout,"[INFO] Generated Outfile %s\n",parameters.outfile);
			}
			break;

		case TGA_DFMT:			/* TGA */
			if(imagetotga(image, parameters.outfile)){
                fprintf(stderr,"[ERROR] Error generating tga file. Outfile %s not generated\n",parameters.outfile);
        failed = 1;
			}
			else {
                fprintf(stdout,"[INFO] Generated Outfile %s\n",parameters.outfile);
			}
			break;
#ifdef OPJ_HAVE_LIBPNG
		case PNG_DFMT:			/* PNG */
			if(imagetopng(image, parameters.outfile)){
                fprintf(stderr,"[ERROR] Error generating png file. Outfile %s not generated\n",parameters.outfile);
        failed = 1;
			}
			else {
                fprintf(stdout,"[INFO] Generated Outfile %s\n",parameters.outfile);
			}
			break;
#endif /* OPJ_HAVE_LIBPNG */
/* Can happen if output file is TIFF or PNG
 * and OPJ_HAVE_LIBTIF or OPJ_HAVE_LIBPNG is undefined
*/
			default:
                fprintf(stderr,"[ERROR] Outfile %s not generated\n",parameters.outfile);
        failed = 1;
		}

		/* free remaining structures */
		if (l_codec) {
			opj_destroy_codec(l_codec);
		}


		/* free image data structure */
		opj_image_destroy(image);

		/* destroy the codestream index */
		opj_destroy_cstr_index(&cstr_index);

		if(failed) remove(parameters.outfile);
	}
	destroy_parameters(&parameters);
	if (numDecompressedImages) {
		fprintf(stdout, "decode time: %d ms\n", (int)( (tCumulative * 1000.0) / (OPJ_FLOAT64)numDecompressedImages));
	}
	return failed ? EXIT_FAILURE : EXIT_SUCCESS;
}
Пример #10
0
/* -------------------------------------------------------------------------- */
int main(int argc, char *argv[])
{
    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;

    int32_t num_images, imageno;
    img_fol_t img_fol;
    dircnt_t *dirptr = NULL;

#ifdef MSD
    bool l_go_on = true;
    uint32_t l_max_data_size = 1000;
    uint8_t * l_data = (uint8_t *) malloc(1000);
#endif

    /* Set decoding parameters to default values */
    opj_set_default_decoder_parameters(&parameters);

    /* 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, &parameters,&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((size_t)num_images*OPJ_PATH_LEN*sizeof(char));	/* Stores at max 10 image file names*/
            dirptr->filename = (char**) malloc((size_t)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, &parameters)) {
                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);
            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, &parameters) ) {
            fprintf(stderr, "ERROR -> opj_dump: failed to setup the decoder\n");
            opj_stream_destroy(l_stream);
            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);
            opj_destroy_codec(l_codec);
            opj_image_destroy(image);
            fclose(fout);
            return EXIT_FAILURE;
        }

        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;
}
Пример #11
0
int main (int argc, char *argv[])
{
        opj_dparameters_t l_param;
        opj_codec_t * l_codec;
        opj_image_t * l_image;
        opj_stream_t * l_stream;
        OPJ_UINT32 l_data_size;
        OPJ_UINT32 l_max_data_size = 1000;
        OPJ_UINT32 l_tile_index;
        OPJ_BYTE * l_data = (OPJ_BYTE *) malloc(1000);
        OPJ_BOOL l_go_on = OPJ_TRUE;
        OPJ_UINT32 l_nb_comps=0 ;
        OPJ_INT32 l_current_tile_x0,l_current_tile_y0,l_current_tile_x1,l_current_tile_y1;

        int da_x0=0;
        int da_y0=0;
        int da_x1=1000;
        int da_y1=1000;
        char input_file[64];

        /* should be test_tile_decoder 0 0 1000 1000 tte1.j2k */
        if( argc == 6 )
        {
                da_x0=atoi(argv[1]);
                da_y0=atoi(argv[2]);
                da_x1=atoi(argv[3]);
                da_y1=atoi(argv[4]);
                strcpy(input_file,argv[5]);

        }
        else
        {
                da_x0=0;
                da_y0=0;
                da_x1=1000;
                da_y1=1000;
                strcpy(input_file,"test.j2k");
        }

        if (! l_data) {
                return EXIT_FAILURE;
        }

        l_stream = opj_stream_create_default_file_stream(input_file,OPJ_TRUE);
        if (!l_stream){
                free(l_data);
                fprintf(stderr, "ERROR -> failed to create the stream from the file\n");
                return EXIT_FAILURE;
        }

        /* Set the default decoding parameters */
        opj_set_default_decoder_parameters(&l_param);

        /* */
        l_param.decod_format = infile_format(input_file);

        /** you may here add custom decoding parameters */
        /* do not use layer decoding limitations */
        l_param.cp_layer = 0;

        /* do not use resolutions reductions */
        l_param.cp_reduce = 0;

        /* to decode only a part of the image data */
        /*opj_restrict_decoding(&l_param,0,0,1000,1000);*/


        switch(l_param.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;
                        }
                default:
                        {
                                fprintf(stderr, "ERROR -> Not a valid JPEG2000 file!\n");
                                free(l_data);
                                opj_stream_destroy(l_stream);
                                return EXIT_FAILURE;
                        }
        }

        /* 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, &l_param))
        {
                fprintf(stderr, "ERROR -> j2k_dump: failed to setup the decoder\n");
                free(l_data);
                opj_stream_destroy(l_stream);
                opj_destroy_codec(l_codec);
                return EXIT_FAILURE;
        }

        /* Read the main header of the codestream and if necessary the JP2 boxes*/
        if (! opj_read_header(l_stream, l_codec, &l_image))
        {
                fprintf(stderr, "ERROR -> j2k_to_image: failed to read the header\n");
                free(l_data);
                opj_stream_destroy(l_stream);
                opj_destroy_codec(l_codec);
                return EXIT_FAILURE;
        }

        if (!opj_set_decode_area(l_codec, l_image, da_x0, da_y0,da_x1, da_y1)){
                fprintf(stderr,	"ERROR -> j2k_to_image: failed to set the decoded area\n");
                free(l_data);
                opj_stream_destroy(l_stream);
                opj_destroy_codec(l_codec);
                opj_image_destroy(l_image);
                return EXIT_FAILURE;
        }


        while (l_go_on)
        {
                if (! opj_read_tile_header( l_codec,
                                        l_stream,
                                        &l_tile_index,
                                        &l_data_size,
                                        &l_current_tile_x0,
                                        &l_current_tile_y0,
                                        &l_current_tile_x1,
                                        &l_current_tile_y1,
                                        &l_nb_comps,
                                        &l_go_on))
                {
                        free(l_data);
                        opj_stream_destroy(l_stream);
                        opj_destroy_codec(l_codec);
                        opj_image_destroy(l_image);
                        return EXIT_FAILURE;
                }

                if (l_go_on)
                {
                        if (l_data_size > l_max_data_size)
                        {
                                OPJ_BYTE *l_new_data = (OPJ_BYTE *) realloc(l_data, l_data_size);
                                if (! l_new_data)
                                {
                                        free(l_new_data);
                                        opj_stream_destroy(l_stream);
                                        opj_destroy_codec(l_codec);
                                        opj_image_destroy(l_image);
                                        return EXIT_FAILURE;
                                }
                                l_data = l_new_data;
                                l_max_data_size = l_data_size;
                        }

                        if (! opj_decode_tile_data(l_codec,l_tile_index,l_data,l_data_size,l_stream))
                        {
                                free(l_data);
                                opj_stream_destroy(l_stream);
                                opj_destroy_codec(l_codec);
                                opj_image_destroy(l_image);
                                return EXIT_FAILURE;
                        }
                        /** now should inspect image to know the reduction factor and then how to behave with data */
                }
        }

        if (! opj_end_decompress(l_codec,l_stream))
        {
                free(l_data);
                opj_stream_destroy(l_stream);
                opj_destroy_codec(l_codec);
                opj_image_destroy(l_image);
                return EXIT_FAILURE;
        }

        /* Free memory */
        free(l_data);
        opj_stream_destroy(l_stream);
        opj_destroy_codec(l_codec);
        opj_image_destroy(l_image);

        /* Print profiling*/
        /*PROFPRINT();*/

        return EXIT_SUCCESS;
}
Пример #12
0
unsigned char *
OPJSupport::compressJPEG2K(void *data,
                           int samplesPerPixel,
                           int rows,
                           int columns,
                           int bitsstored, //precision,
                           unsigned char bitsAllocated,
                           bool sign,
                           int rate,
                           long *compressedDataSize)
{
    //enconderMutex.lock();
    
    opj_cparameters_t parameters;
    
    opj_stream_t *l_stream = 00;
    opj_codec_t* l_codec = 00;
    opj_image_t *image = NULL;
    
    OPJ_BOOL bSuccess;
    OPJ_BOOL bUseTiles = OPJ_FALSE; /* OPJ_TRUE */
    OPJ_UINT32 l_nb_tiles = 4;
    
    OPJ_BOOL fails = OPJ_FALSE;
    OPJ_CODEC_FORMAT codec_format;
    
    memset(&parameters, 0, sizeof(parameters));
    opj_set_default_encoder_parameters(&parameters);
    parameters.outfile[0] = '\0';
    parameters.tcp_numlayers = 1;
    parameters.cp_disto_alloc = 1;
    parameters.tcp_rates[0] = rate;
    parameters.cod_format = JP2_CFMT; //JP2_CFMT; //J2K_CFMT;
    OPJ_BOOL forceJ2K = (parameters.cod_format == J2K_CFMT ? OPJ_FALSE:(((OPJ_TRUE /*force here*/))));
    
#ifdef WITH_OPJ_FILE_STREAM
    tmpnam(parameters.outfile);
#endif
    
    image = rawtoimage( (char*) data,
                       &parameters,
                       static_cast<int>( columns*rows*samplesPerPixel*bitsAllocated/8), // [data length], fragment_size
                       columns, rows,
                       samplesPerPixel,
                       bitsAllocated,
                       bitsstored,
                       sign,
                       /*quality,*/ 0);
    
    if (image == NULL)
    {
        /* close and free the byte stream */
        if (l_stream) opj_stream_destroy(l_stream);
        
        /* free remaining compression structures */
        if (l_codec) opj_destroy_codec(l_codec);
        
        /* free image data */
        if (image) opj_image_destroy(image);
        
        *compressedDataSize = 0;
        
        //enconderMutex.unlock();
        
        return NULL;
    }
    
    /*-----------------------------------------------*/
    
    switch (parameters.cod_format)
    {
        case J2K_CFMT:                      /* JPEG-2000 codestream */
            codec_format = OPJ_CODEC_J2K;
            break;
            
        case JP2_CFMT:                      /* JPEG 2000 compressed image data */
            codec_format = OPJ_CODEC_JP2;
            break;
            
        case JPT_CFMT:                      /* JPEG 2000, JPIP */
            codec_format = OPJ_CODEC_JPT;
            break;
            
        case -1:
        default:
            fprintf(stderr,"%s:%d: encode format missing\n",__FILE__,__LINE__);
            
            /* close and free the byte stream */
            if (l_stream) opj_stream_destroy(l_stream);
            
            /* free remaining compression structures */
            if (l_codec) opj_destroy_codec(l_codec);
            
            /* free image data */
            if (image) opj_image_destroy(image);
            
            *compressedDataSize = 0;
            
            //enconderMutex.unlock();
            
            return NULL;
    }
    
    /* see test_tile_encoder.c:232 and opj_compress.c:1746 */
    l_codec = opj_create_compress(codec_format);
    if (!l_codec)
    {
        fprintf(stderr,"%s:%d:\n\tNO codec\n",__FILE__,__LINE__);
        
        /* close and free the byte stream */
        if (l_stream) opj_stream_destroy(l_stream);
        
        /* free remaining compression structures */
        if (l_codec) opj_destroy_codec(l_codec);
        
        /* free image data */
        if (image) opj_image_destroy(image);
        
        *compressedDataSize = 0;
        
        //enconderMutex.unlock();
        
        return NULL;
    }
    
    
#ifdef OPJ_VERBOSE
    opj_set_info_handler(l_codec, info_callback, this);
    opj_set_warning_handler(l_codec, warning_callback, this);
#endif
    
    opj_set_error_handler(l_codec, error_callback, this);
    
    if ( !opj_setup_encoder(l_codec, &parameters, image))
    {
        fprintf(stderr,"%s:%d:\n\topj_setup_encoder failed\n",__FILE__,__LINE__);
        
        /* close and free the byte stream */
        if (l_stream) opj_stream_destroy(l_stream);
        
        /* free remaining compression structures */
        if (l_codec) opj_destroy_codec(l_codec);
        
        /* free image data */
        if (image) opj_image_destroy(image);
        
        *compressedDataSize = 0;
        
        //enconderMutex.unlock();
        
        return NULL;
    }
    
    
    // Create the stream
#ifdef WITH_OPJ_BUFFER_STREAM
    opj_buffer_info_t bufferInfo;
    bufferInfo.cur = bufferInfo.buf = (OPJ_BYTE *)data;
    bufferInfo.len = (OPJ_SIZE_T) rows * columns;
    l_stream = opj_stream_create_buffer_stream(&bufferInfo, OPJ_STREAM_WRITE);
    
    //printf("%p\n",bufferInfo.buf);
    //printf("%lu\n",bufferInfo.len);
#endif
    
    
#ifdef WITH_OPJ_FILE_STREAM
    l_stream = opj_stream_create_default_file_stream(parameters.outfile, OPJ_STREAM_WRITE);
#endif
    
    
    if (!l_stream)
    {
        fprintf(stderr,"%s:%d:\n\tstream creation failed\n",__FILE__,__LINE__);
        
        /* close and free the byte stream */
        if (l_stream) opj_stream_destroy(l_stream);
        
        /* free remaining compression structures */
        if (l_codec) opj_destroy_codec(l_codec);
        
        /* free image data */
        if (image) opj_image_destroy(image);
        
        *compressedDataSize = 0;
        
        //enconderMutex.unlock();
        
        return NULL;
    }
    
    
    while(1)
    {
        //        int tile_index=-1, user_changed_tile=0, user_changed_reduction=0;
        //        int max_tiles=0, max_reduction=0;
        fails = OPJ_TRUE;
        
        /* encode the image */
        bSuccess = opj_start_compress(l_codec, image, l_stream);
        
        if (!bSuccess)
        {
            fprintf(stderr,"%s:%d:\n\topj_start_compress failed\n",__FILE__,__LINE__);
            break;
        }
        
        if ( bSuccess && bUseTiles )
        {
            OPJ_BYTE *l_data = NULL;
            OPJ_UINT32 l_data_size = 512*512*3; //FIXME
            l_data = (OPJ_BYTE*) malloc(l_data_size * sizeof(OPJ_BYTE));
            memset(l_data, 0, l_data_size * sizeof(OPJ_BYTE));
            
            //assert( l_data );
            if (!l_data)
            {
                /* close and free the byte stream */
                if (l_stream) opj_stream_destroy(l_stream);
                
                /* free remaining compression structures */
                if (l_codec) opj_destroy_codec(l_codec);
                
                /* free image data */
                if (image) opj_image_destroy(image);
                
                *compressedDataSize = 0;
                
                //enconderMutex.unlock();
                
                return NULL;
            }
            
            for (int i=0;i<l_nb_tiles;++i)
            {
                if (! opj_write_tile(l_codec,i,l_data,l_data_size,l_stream))
                {
                    fprintf(stderr, "\nERROR -> test_tile_encoder: failed to write the tile %d!\n",i);
                    /* close and free the byte stream */
                    if (l_stream) opj_stream_destroy(l_stream);
                    
                    /* free remaining compression structures */
                    if (l_codec) opj_destroy_codec(l_codec);
                    
                    /* free image data */
                    if (image) opj_image_destroy(image);
                    
                    free(l_data);
                    
                    *compressedDataSize = 0;
                    
                    //enconderMutex.unlock();
                    
                    return NULL;
                }
            }
            
            free(l_data);
        }
        else
        {
            if (!opj_encode(l_codec, l_stream))
            {
                fprintf(stderr,"%s:%d:\n\topj_encode failed\n",__FILE__,__LINE__);
                break;
            }
        }
        
        if (!opj_end_compress(l_codec, l_stream))
        {
            fprintf(stderr,"%s:%d:\n\topj_end_compress failed\n",__FILE__,__LINE__);
            break;
        }
        
        fails = OPJ_FALSE;
        break;
        
    } // while
    
    *compressedDataSize = 0;
    unsigned char *to = NULL;
    
    /* close and free the byte stream */
    if (l_stream) opj_stream_destroy(l_stream);
    
    /* free remaining compression structures */
    if (l_codec) opj_destroy_codec(l_codec);
    
    /* free image data */
    if (image) opj_image_destroy(image);
    
    if (fails)
    {
#ifdef WITH_OPJ_FILE_STREAM
        if (parameters.outfile[0] != '\0')
            remove(parameters.outfile);
#endif
    }
    else
    {
#ifdef WITH_OPJ_BUFFER_STREAM
        //printf("%p\n",bufferInfo.buf);
        //printf("%lu\n",bufferInfo.len);
        //to=(unsigned char *) malloc(bufferInfo.len);
        //memcpy(to,l_stream,bufferInfo.len);
#endif
        
#ifdef WITH_OPJ_FILE_STREAM
        // Open the temp file and get the encoded data into 'to'
        // and the length into 'length'
        FILE *f = NULL;
        if (parameters.outfile[0] != '\0')
        {
            f = fopen(parameters.outfile, "rb");
        }
        
        long length = 0;
        
        if (f != NULL)
        {
            fseek(f, 0, SEEK_END);
            length = ftell(f);
            fseek(f, 0, SEEK_SET);
            if (forceJ2K)
            {
                length -= 85;
                fseek(f, 85, SEEK_SET);
            }
            
            if (length % 2)
            {
                length++; // ensure even length
                //fprintf(stdout,"Padded to %li\n", length);
            }
            
            to = (unsigned char *) malloc(length);
            
            fread(to, length, 1, f);
            
            //printf("%s %lu\n",parameters.outfile,length);;
            
            fclose(f);
        }
        
        *compressedDataSize = length;
        
        if (parameters.outfile[0] != '\0')
        {
            remove(parameters.outfile);
        }
#endif
    }
    
    //enconderMutex.unlock();
    
    return to;
}
Пример #13
0
/* -------------------------------------------------------------------------- */
int main(int argc, char **argv)
{
	FILE *fsrc = NULL;

	opj_dparameters_t parameters;			/* decompression parameters */
	opj_image_t* image = NULL;
	opj_stream_t *l_stream = NULL;				/* Stream */
	opj_codec_t* l_codec = NULL;				/* Handle to a decompressor */
	opj_codestream_info_v2_t* cstr_info = NULL;

	/* Index of corner tiles */
	OPJ_UINT32 tile_ul = 0;
	OPJ_UINT32 tile_ur = 0;
	OPJ_UINT32 tile_lr = 0;
	OPJ_UINT32 tile_ll = 0;

	if (argc != 2) {
		fprintf(stderr, "Usage: %s <input_file>\n", argv[0]);
		return EXIT_FAILURE;
	}

	/* Set decoding parameters to default values */
	opj_set_default_decoder_parameters(&parameters);

	strncpy(parameters.infile, argv[1], OPJ_PATH_LEN - 1);

	/* read the input file */
	/* ------------------- */
	fsrc = fopen(parameters.infile, "rb");
	if (!fsrc) {
		fprintf(stderr, "ERROR -> failed to open %s for reading\n", parameters.infile);
		return EXIT_FAILURE;
	}

	/* decode the JPEG2000 stream */
	/* -------------------------- */
	parameters.decod_format = infile_format(parameters.infile);

	switch(parameters.decod_format) {
		case J2K_CFMT:	/* JPEG-2000 codestream */
		{
			/* Get a decoder handle */
			l_codec = opj_create_decompress_v2(CODEC_J2K);
			break;
		}
		case JP2_CFMT:	/* JPEG 2000 compressed image data */
		{
			/* Get a decoder handle */
			l_codec = opj_create_decompress_v2(CODEC_JP2);
			break;
		}
		case JPT_CFMT:	/* JPEG 2000, JPIP */
		{
			/* Get a decoder handle */
			l_codec = opj_create_decompress_v2(CODEC_JPT);
			break;
		}
		default:
			fprintf(stderr,
				"Unrecognized format for input %s [accept only *.j2k, *.jp2, *.jpc or *.jpt]\n\n",
				parameters.infile);
			return EXIT_FAILURE;
	}

	/* 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);

	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;
	}

	/* Setup the decoder decoding parameters using user parameters */
	if ( !opj_setup_decoder_v2(l_codec, &parameters) ){
		fprintf(stderr, "ERROR -> j2k_dump: failed to setup the decoder\n");
		opj_stream_destroy(l_stream);
		fclose(fsrc);
		opj_destroy_codec(l_codec);
		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 -> j2k_to_image: failed to read the header\n");
		opj_stream_destroy(l_stream);
		fclose(fsrc);
		opj_destroy_codec(l_codec);
		opj_image_destroy(image);
		return EXIT_FAILURE;
	}

	/* Extract some info from the code stream */
	cstr_info = opj_get_cstr_info(l_codec);

	fprintf(stdout, "The file contains %dx%d tiles\n", cstr_info->tw, cstr_info->th);

	tile_ul = 0;
	tile_ur = cstr_info->tw - 1;
	tile_lr = cstr_info->tw * cstr_info->th - 1;
	tile_ll = tile_lr - cstr_info->tw;

#define TEST_TILE( tile_index ) \
	fprintf(stdout, "Decoding tile %d ...\n", tile_index); \
	if(!opj_get_decoded_tile(l_codec, l_stream, image, tile_index )){ \
		fprintf(stderr, "ERROR -> j2k_to_image: failed to decode tile %d\n", tile_index); \
		opj_stream_destroy(l_stream); \
		opj_destroy_cstr_info_v2(&cstr_info); \
		opj_destroy_codec(l_codec); \
		opj_image_destroy(image); \
		fclose(fsrc); \
		return EXIT_FAILURE; \
	} \
	fprintf(stdout, "Tile %d is decoded successfully\n", tile_index);

	TEST_TILE(tile_ul)
	TEST_TILE(tile_lr)
	TEST_TILE(tile_ul)
	TEST_TILE(tile_ll)
	TEST_TILE(tile_ur)
	TEST_TILE(tile_lr)

	/* Close the byte stream */
	opj_stream_destroy(l_stream);

	/* Destroy code stream info */
	opj_destroy_cstr_info_v2(&cstr_info);

	/* Free remaining structures */
	opj_destroy_codec(l_codec);

	/* Free image data structure */
	opj_image_destroy(image);

	/* Close the input file */
	fclose(fsrc);

	return EXIT_SUCCESS;
}
Пример #14
0
int main(int argc, char *argv[])
{
    const char * v = opj_version();

    const OPJ_COLOR_SPACE color_space = OPJ_CLRSPC_GRAY;
    unsigned int numcomps = 1;
    unsigned int i;
    unsigned int image_width = 256;
    unsigned int image_height = 256;

    opj_cparameters_t parameters;

    unsigned int subsampling_dx;
    unsigned int subsampling_dy;
    const char outputfile[] = "testempty2.j2k";

    opj_image_cmptparm_t cmptparm;
    opj_image_t *image;
    opj_codec_t* l_codec = 00;
    bool bSuccess;
    opj_stream_t *l_stream = 00;
    (void)argc;
    (void)argv;

    opj_set_default_encoder_parameters(&parameters);
    parameters.cod_format = J2K_CFMT;
    puts(v);
    subsampling_dx = (unsigned int)parameters.subsampling_dx;
    subsampling_dy = (unsigned int)parameters.subsampling_dy;
    cmptparm.prec = 8;
    cmptparm.sgnd = 0;
    cmptparm.dx = subsampling_dx;
    cmptparm.dy = subsampling_dy;
    cmptparm.w = image_width;
    cmptparm.h = image_height;
    strncpy(parameters.outfile, outputfile, sizeof(parameters.outfile)-1);

    image = opj_image_create(numcomps, &cmptparm, color_space);
    assert( image );

    for (i = 0; i < image_width * image_height; i++) {
        unsigned int compno;
        for(compno = 0; compno < numcomps; compno++) {
            image->comps[compno].data[i] = 0;
        }
    }

    /* 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);

    l_codec = opj_create_compress(OPJ_CODEC_J2K);
    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);

    opj_setup_encoder(l_codec, &parameters, image);

    l_stream = opj_stream_create_default_file_stream(parameters.outfile,false);
    if( !l_stream ) {
        fprintf( stderr, "Something went wrong during creation of stream\n" );
        opj_destroy_codec(l_codec);
        opj_image_destroy(image);
        opj_stream_destroy(l_stream);
        return 1;
    }
    assert(l_stream);
    bSuccess = opj_start_compress(l_codec,image,l_stream);
    if( !bSuccess ) {
        opj_stream_destroy(l_stream);
        opj_destroy_codec(l_codec);
        opj_image_destroy(image);
        return 0;
    }

    assert( bSuccess );
    bSuccess = opj_encode(l_codec, l_stream);
    assert( bSuccess );
    bSuccess = opj_end_compress(l_codec, l_stream);
    assert( bSuccess );

    opj_stream_destroy(l_stream);

    opj_destroy_codec(l_codec);
    opj_image_destroy(image);


    /* read back the generated file */
    {
        opj_codec_t* d_codec = 00;
        opj_dparameters_t dparameters;

        d_codec = opj_create_decompress(OPJ_CODEC_J2K);
        opj_set_info_handler(d_codec, info_callback,00);
        opj_set_warning_handler(d_codec, warning_callback,00);
        opj_set_error_handler(d_codec, error_callback,00);

        bSuccess = opj_setup_decoder(d_codec, &dparameters);
        assert( bSuccess );

        l_stream = opj_stream_create_default_file_stream(outputfile,1);
        assert( l_stream );

        bSuccess = opj_read_header(l_stream, d_codec,&image);
        assert( bSuccess );

        bSuccess = opj_decode(l_codec, l_stream, image);
        assert( bSuccess );

        bSuccess = opj_end_decompress(l_codec, l_stream);
        assert( bSuccess );

        opj_stream_destroy(l_stream);

        opj_destroy_codec(d_codec);

        opj_image_destroy(image);
    }

    puts( "end" );
    return 0;
}
Пример #15
0
bool Jpeg2000::load (const QString &filename,
                     QImage &imageResult) const
{
  LOG4CPP_INFO_S ((*mainCat)) << "Jpeg2000::load"
                              << " filename=" << filename.toLatin1().data();

  if (invalidFileExtension (filename)) {
    return false;
  }

  opj_dparameters_t parameters;
  initializeParameters (parameters);

  parameters.decod_format = inputFormat (filename.toLatin1().data());

  opj_stream_t *inStream = opj_stream_create_default_file_stream (filename.toLatin1().data(), 1);
  if (!inStream) {
    LOG4CPP_ERROR_S ((*mainCat)) << "Jpeg2000::load encountered error opening stream";
    return false;
  }

  // Create decoder
  opj_codec_t *inCodec = decode (parameters.decod_format);
  if (!inCodec) {
    LOG4CPP_ERROR_S ((*mainCat)) << "Jpeg2000::load encountered error creating decoding stream";
    opj_stream_destroy (inStream);
    return false;
  }

  // Callbacks for local handling of errors
  opj_set_info_handler (inCodec, infoCallback, 0);
  opj_set_warning_handler (inCodec, warningCallback, 0);
  opj_set_error_handler (inCodec, errorCallback, 0);

  if (!opj_setup_decoder (inCodec,
                          &parameters)) {
    LOG4CPP_ERROR_S ((*mainCat)) << "Jpeg2000::load encountered error decoding stream";
    opj_stream_destroy (inStream);
    opj_destroy_codec (inCodec);
    return false;
  }

  // Read header and, if necessary, the JP2 boxes
  opj_image_t *image;
  if (!opj_read_header (inStream,
                        inCodec,
                        &image)) {
    LOG4CPP_ERROR_S ((*mainCat)) << "Jpeg2000::load encountered error reading header";
    opj_stream_destroy (inStream);
    opj_destroy_codec (inCodec);
    opj_image_destroy (image);
    return false;
  }

  // Get the decoded image
  if (!(opj_decode (inCodec,
                    inStream,
                    image) &&
       opj_end_decompress (inCodec,
                           inStream))) {
    LOG4CPP_ERROR_S ((*mainCat)) << "Jpeg2000::load failed to decode image";
    opj_destroy_codec (inCodec);
    opj_stream_destroy (inStream);
    opj_image_destroy (image);
    return false;
  }

  // Close the byte stream
  opj_stream_destroy (inStream);

  applyImageTweaks (image);

  // Transform into ppm image in memory
  bool success = true;
  QBuffer buffer;
  buffer.open (QBuffer::WriteOnly);
  if (imagetopnm (image,
                  buffer)) {
    LOG4CPP_ERROR_S ((*mainCat)) << "Jpeg2000::load failed to generate new image";
    success = false;

  } else {

    // Intermediate file for debugging
//    QFile file ("jpeg2000.ppm");
//    file.open (QIODevice::WriteOnly);
//    file.write (buffer.data());
//    file.close ();

    // Create output
    imageResult.loadFromData(buffer.data());

  }

  // Deallocate
  if (inCodec) {
    opj_destroy_codec (inCodec);
  }
  opj_image_destroy (image);

  return success;
}