Example #1
0
void Level::load_from_file(const Filename& filename)
{
    clear();
    status = GOOD;

    level_filename = filename.get_rootless();

    FileFormat fmt = get_file_format(filename);
    if (fmt == FORMAT_BINARY) {
        // load an original .LVL file from L1/ONML/...
        load_from_binary(filename);
    }
    else if (fmt == FORMAT_LEMMINI) {
        // load an .INI file from Lemmini
        load_from_lemmini(filename);
    }
    else {
        // load the regular Lix format
        std::vector <IO::Line> lines;
        if (IO::fill_vector_from_file(lines, filename.get_rootful())) {
            load_from_vector(lines);
        }
        else status = BAD_FILE_NOT_FOUND;
    }
    load_finalize();
}
Example #2
0
int convert_fast_messy (FILE *infile, FILE *outfile) {
  unsigned char *in_bufend;
  enum file_format infile_type;
  
  in_bufend = in_buffer + my_fread(infile, BUFFERSIZE);
  *in_bufend = '\0';
  
  infile_type = get_file_format (in_buffer);
  
  switch (infile_type) {
  case dos:
    if (verbose)
      printf("%s: %s is already in MS-DOS format, skipping.\n",
	     program_name,current_file);
    return (FALSE);
    break;

  case tunix: /* drop through */
  case apple:
    /* Wasn't able to speed this up, call old routine */
    convert_messy (infile, outfile);
    return (TRUE);
    break;

  case binary:
    return (FALSE);
    break;

  default:
    fprintf(stderr,"%s: Fatal internal error\n",program_name);
    exit (EXIT_FAILURE);
    break;
  } /* switch */
}
Example #3
0
static int infile_format(const char *fname)
{
    FILE *reader;
	const char *s, *magic_s;
	int ext_format, magic_format;
	unsigned char buf[12];
	unsigned int l_nb_read;
	int i;

	reader = fopen(fname, "rb");

	if (reader == NULL) {
        fprintf(stderr, "File open error!\n");
		return -2;
	}

	memset(buf, 0, 12);
	l_nb_read = fread(buf, 1, 12, reader);
	fclose(reader);
	if (l_nb_read != 12) {
        fprintf(stderr, "File read error, only %d bytes read!\n", l_nb_read);
        return -1;
	}

	ext_format = get_file_format(fname);

	if (ext_format == JPT_CFMT)
		return JPT_CFMT;

	if (memcmp(buf, JP2_RFC3745_MAGIC, 12) == 0 || memcmp(buf, JP2_MAGIC, 4) == 0) {
		magic_format = JP2_CFMT;
		magic_s = ".jp2";
	}
	else if (memcmp(buf, J2K_CODESTREAM_MAGIC, 4) == 0) {
		magic_format = J2K_CFMT;
		magic_s = ".j2k or .jpc or .j2c";
	}
	else
    {
        // TODO GetCurrentThreadId() and GetTickCount() prevent compilation on linux (and possibly other platforms?)
        //fprintf(stderr, "DEBUG DEBUG DEBUG infile_format: file magic error %s, TID %d @ %d\n", fname, GetCurrentThreadId(), GetTickCount());
        for (i = 0; i < l_nb_read; i++) {
            fprintf(stderr, "%02X", buf[i]);
        }
        fprintf(stderr, "\n");
		return -1;
    }

	if (magic_format == ext_format)
		return ext_format;

	s = fname + strlen(fname) - 4;

	fputs("\n===========================================\n", stderr);
	fprintf(stderr, "The extension of this file is incorrect.\n"
					"FOUND %s. SHOULD BE %s\n", s, magic_s);
	fputs("===========================================\n", stderr);

	return magic_format;
}
Example #4
0
/* -------------------------------------------------------------------------- */
static char get_next_file(int imageno,dircnt_t *dirptr,img_fol_t *img_fol, opj_dparameters_t *parameters)
{
    char image_filename[OPJ_PATH_LEN], infilename[OPJ_PATH_LEN],outfilename[OPJ_PATH_LEN],temp_ofname[OPJ_PATH_LEN];
    char *temp_p, temp1[OPJ_PATH_LEN]="";

    strcpy(image_filename,dirptr->filename[imageno]);
    fprintf(stderr,"File Number %d \"%s\"\n",imageno,image_filename);
    parameters->decod_format = get_file_format(image_filename);
    if (parameters->decod_format == -1)
        return 1;
    sprintf(infilename,"%s/%s",img_fol->imgdirpath,image_filename);
    if (opj_strcpy_s(parameters->infile, sizeof(parameters->infile), infilename) != 0) {
        return 1;
    }

    /*Set output file*/
    strcpy(temp_ofname,strtok(image_filename,"."));
    while((temp_p = strtok(NULL,".")) != NULL) {
        strcat(temp_ofname,temp1);
        sprintf(temp1,".%s",temp_p);
    }
    if(img_fol->set_out_format==1) {
        sprintf(outfilename,"%s/%s.%s",img_fol->imgdirpath,temp_ofname,img_fol->out_format);
        if (opj_strcpy_s(parameters->outfile, sizeof(parameters->outfile), outfilename) != 0) {
            return 1;
        }
    }
    return 0;
}
Example #5
0
/* -------------------------------------------------------------------------- */
int parse_cmdline_decoder(opj_dparameters_t *parameters,img_fol_t *img_fol, char *infile) {
	img_fol->set_out_format = 0;
    parameters->decod_format = infile_format(infile);
    strncpy(parameters->infile, infile, sizeof(parameters->infile)-1);
    char *outfile = "file.raw";
    parameters->cod_format = get_file_format(outfile);
    strncpy(parameters->outfile, outfile, sizeof(parameters->outfile)-1);

	return 0;
}
static int infile_format(const char *fname)
{
	FILE *reader;
	const char *s, *magic_s;
	int ext_format, magic_format;
	unsigned char buf[12];
	OPJ_SIZE_T l_nb_read;

	reader = fopen(fname, "rb");

	if (reader == NULL)
		return -2;

	memset(buf, 0, 12);
	l_nb_read = fread(buf, 1, 12, reader);
	fclose(reader);
	if (l_nb_read != 12)
		return -1;



	ext_format = get_file_format(fname);

	if (ext_format == JPT_CFMT)
		return JPT_CFMT;

	if (memcmp(buf, JP2_RFC3745_MAGIC, 12) == 0 || memcmp(buf, JP2_MAGIC, 4) == 0) {
		magic_format = JP2_CFMT;
		magic_s = ".jp2";
	}
	else if (memcmp(buf, J2K_CODESTREAM_MAGIC, 4) == 0) {
		magic_format = J2K_CFMT;
		magic_s = ".j2k or .jpc or .j2c";
	}
	else
		return -1;

	if (magic_format == ext_format)
		return ext_format;

	s = fname + strlen(fname) - 4;

	fputs("\n===========================================\n", stderr);
	fprintf(stderr, "The extension of this file is incorrect.\n"
					"FOUND %s. SHOULD BE %s\n", s, magic_s);
	fputs("===========================================\n", stderr);

	return magic_format;
}
Example #7
0
flacsplit::Decoder::Decoder(const std::string &path, FILE *fp,
    enum file_format format) throw (Bad_format, Sox_error) :
	Basic_decoder(),
	_decoder()
{
	if (format == FF_UNKNOWN)
		format = get_file_format(fp);
	switch (format) {
	case FF_UNKNOWN:
		throw Bad_format();
	case FF_WAVE:
		_decoder.reset(new Wave_decoder(path, fp));
		break;
	case FF_FLAC:
		_decoder.reset(new Flac_decoder(fp));
		break;
	}
}
Example #8
0
int
main (int argc, char **argv)
{
  int status = 0;
  int i;
  char *filename = NULL;

  progname = argv[0];

  bufsiz = resolv_number (BGPDUMP_BUFSIZ_DEFAULT, NULL);
  nroutes = resolv_number (ROUTE_LIMIT_DEFAULT, NULL);

  status = bgpdump_getopt (argc, argv);

  argc -= optind;
  argv += optind;

  if (status)
    return status;

  if (argc == 0)
    {
      printf ("specify rib files.\n");
      usage ();
      exit (-1);
    }

  if (verbose)
    {
      printf ("bufsiz = %llu\n", bufsiz);
      printf ("nroutes = %llu\n", nroutes);
    }

  /* default cmd */
  if (! brief && ! show && ! route_count && ! plen_dist && ! udiff &&
      ! lookup && ! peer_table_only && ! stat && ! compat_mode &&
      ! autsiz && ! heatmap)
    show++;

  if (stat)
    peer_stat_init ();

  char *buf;
  buf = malloc (bufsiz);
  if (! buf)
    {
      printf ("can't malloc %lluB-size buf: %s\n",
              bufsiz, strerror (errno));
      exit (-1);
    }

  peer_table_init ();

  if (peer_spec_size)
    {
      for (i = 0; i < peer_spec_size; i++)
        {
          peer_route_table[i] = route_table_create ();
          peer_route_size[i] = 0;
          peer_ptree[i] = ptree_create ();
        }
    }

  if (lookup)
    {
      route_init ();
      ptree[AF_INET] = ptree_create ();
      ptree[AF_INET6] = ptree_create ();
    }

  if (udiff)
    {
      diff_table[0] = malloc (nroutes * sizeof (struct bgp_route));
      diff_table[1] = malloc (nroutes * sizeof (struct bgp_route));
      assert (diff_table[0] && diff_table[1]);
      memset (diff_table[0], 0, nroutes * sizeof (struct bgp_route));
      memset (diff_table[1], 0, nroutes * sizeof (struct bgp_route));

      if (udiff_lookup)
        {
          diff_ptree[0] = ptree_create ();
          diff_ptree[1] = ptree_create ();
        }
    }

  /* for each rib files. */
  for (i = 0; i < argc; i++)
    {
      filename = argv[i];

      file_format_t format;
      struct access_method *method;
      void *file;
      size_t ret;

      format = get_file_format (filename);
      method = get_access_method (format);
      file = method->fopen (filename, "r");
      if (! file)
        {
          fprintf (stderr, "# could not open file: %s\n", filename);
          continue;
        }

      size_t datalen = 0;

      while (1)
        {
          ret = method->fread (buf + datalen, bufsiz - datalen, 1, file);
          if (debug)
            printf ("read: %lu bytes to buf[%lu]. total %lu bytes\n",
                    ret, datalen, ret + datalen);
          datalen += ret;

          /* end of file. */
          if (ret == 0 && method->feof (file))
            {
              if (debug)
                printf ("read: end-of-file.\n");
              break;
            }

          bgpdump_process (buf, &datalen);

          if (debug)
            printf ("process rest: %lu bytes\n", datalen);
        }

      if (datalen)
        {
          printf ("warning: %lu bytes unprocessed data remains: %s\n",
                  datalen, filename);
        }
      method->fclose (file);

      /* For each end of the processing of files. */
      if (route_count)
        {
          peer_route_count_show ();
          peer_route_count_clear ();
        }

      if (plen_dist)
        {
          peer_route_count_by_plen_show ();
          peer_route_count_by_plen_clear ();
        }
    }

  /* query_table construction. */
  if (lookup)
    {
      query_limit = 0;

      if (lookup_file)
        query_limit = query_file_count (lookup_file);

      if (lookup_addr)
        query_limit++;

      query_init ();

      if (lookup_addr)
        query_addr (lookup_addr);

      if (lookup_file)
        query_file (lookup_file);

      if (debug)
        query_list ();
    }

  /* query to route_table (ptree). */
  if (lookup)
    {
      if (benchmark)
        benchmark_start ();

      if (peer_spec_size)
        {
          for (i = 0; i < peer_spec_size; i++)
            {
              printf ("peer %d:\n", peer_spec_index[i]);
              if (verbose)
                ptree_list (peer_ptree[i]);
              ptree_query (peer_ptree[i], query_table, query_size);
            }
        }
      else
        {
          if (verbose)
            ptree_list (ptree[qaf]);
          ptree_query (ptree[qaf], query_table, query_size);
        }

      if (benchmark)
        {
          benchmark_stop ();
          benchmark_print (query_size);
        }
    }

  if (heatmap)
    {
      for (i = 0; i < peer_spec_size; i++)
        {
          heatmap_image_hilbert_gplot (i);
          heatmap_image_hilbert_data (i);
          //heatmap_image_hilbert_data_aspath_max_distance (i);
        }
    }

  if (lookup)
    {
      free (query_table);
      ptree_delete (ptree[AF_INET]);
      ptree_delete (ptree[AF_INET6]);
      route_finish ();
    }

  if (udiff)
    {
      free (diff_table[0]);
      free (diff_table[1]);

      if (lookup)
        {
          ptree_delete (diff_ptree[0]);
          ptree_delete (diff_ptree[1]);
        }
    }

  if (stat)
    {
      peer_stat_show ();
      //peer_stat_finish ();
    }

  free (buf);

  return status;
}
/* -------------------------------------------------------------------------- */
int parse_cmdline_decoder(int argc, char **argv, opj_dparameters_t *parameters,img_fol_t *img_fol, char *indexfilename) {
	/* parse the command line */
	int totlen, c;
	opj_option_t long_option[]={
		{"ImgDir",REQ_ARG, NULL ,'y'},
		{"OutFor",REQ_ARG, NULL ,'O'},
	};

	const char optlist[] = "i:o:r:l:x:d:t:"

/* UniPG>> */
#ifdef USE_JPWL
					"W:"
#endif /* USE_JPWL */
/* <<UniPG */
			"h"		;
	totlen=sizeof(long_option);
	img_fol->set_out_format = 0;
	do {
		c = opj_getopt_long(argc, argv,optlist,long_option,totlen);
		if (c == -1)
			break;
		switch (c) {
			case 'i':			/* input file */
			{
				char *infile = opj_optarg;
				parameters->decod_format = infile_format(infile);
				switch(parameters->decod_format) {
					case J2K_CFMT:
						break;
					case JP2_CFMT:
						break;
					case JPT_CFMT:
						break;
                                        case -2:
						fprintf(stderr, 
							"!! infile cannot be read: %s !!\n\n", 
							infile);
						return 1;
					default:
						fprintf(stderr, 
							"!! Unrecognized format for infile: %s [accept only *.j2k, *.jp2, *.jpc or *.jpt] !!\n\n", 
							infile);
						return 1;
				}
				strncpy(parameters->infile, infile, sizeof(parameters->infile)-1);
			}
			break;
				
				/* ----------------------------------------------------- */

			case 'o':			/* output file */
			{
				char *outfile = opj_optarg;
				parameters->cod_format = get_file_format(outfile);
				switch(parameters->cod_format) {
					case PGX_DFMT:
						break;
					case PXM_DFMT:
						break;
					case BMP_DFMT:
						break;
					case TIF_DFMT:
						break;
					case RAW_DFMT:
						break;
					case RAWL_DFMT:
						break;
					case TGA_DFMT:
						break;
					case PNG_DFMT:
						break;
					default:
						fprintf(stderr, "Unknown output format image %s [only *.pnm, *.pgm, *.ppm, *.pgx, *.bmp, *.tif, *.raw or *.tga]!! \n", outfile);
						return 1;
				}
				strncpy(parameters->outfile, outfile, sizeof(parameters->outfile)-1);
			}
			break;
			
				/* ----------------------------------------------------- */

			case 'O':			/* output format */
			{
				char outformat[50];
				char *of = opj_optarg;
				sprintf(outformat,".%s",of);
				img_fol->set_out_format = 1;
				parameters->cod_format = get_file_format(outformat);
				switch(parameters->cod_format) {
					case PGX_DFMT:
						img_fol->out_format = "pgx";
						break;
					case PXM_DFMT:
						img_fol->out_format = "ppm";
						break;
					case BMP_DFMT:
						img_fol->out_format = "bmp";
						break;
					case TIF_DFMT:
						img_fol->out_format = "tif";
						break;
					case RAW_DFMT:
						img_fol->out_format = "raw";
						break;
					case RAWL_DFMT:
						img_fol->out_format = "rawl";
						break;
					case TGA_DFMT:
						img_fol->out_format = "raw";
						break;
					case PNG_DFMT:
						img_fol->out_format = "png";
						break;
					default:
						fprintf(stderr, "Unknown output format image %s [only *.pnm, *.pgm, *.ppm, *.pgx, *.bmp, *.tif, *.raw or *.tga]!! \n", outformat);
						return 1;
						break;
				}
			}
			break;

				/* ----------------------------------------------------- */


			case 'r':		/* reduce option */
			{
				sscanf(opj_optarg, "%ud", &parameters->cp_reduce);
			}
			break;
			
				/* ----------------------------------------------------- */
      

			case 'l':		/* layering option */
			{
				sscanf(opj_optarg, "%ud", &parameters->cp_layer);
			}
			break;
			
				/* ----------------------------------------------------- */

			case 'h': 			/* display an help description */
				decode_help_display();
				return 1;				

				/* ------------------------------------------------------ */

			case 'y':			/* Image Directory path */
				{
					img_fol->imgdirpath = (char*)malloc(strlen(opj_optarg) + 1);
					strcpy(img_fol->imgdirpath,opj_optarg);
					img_fol->set_imgdir=1;
				}
				break;

				/* ----------------------------------------------------- */

			case 'd':     		/* Input decode ROI */
			{
				int size_optarg = (int)strlen(opj_optarg) + 1;
				char *ROI_values = (char*) malloc((size_t)size_optarg);
				ROI_values[0] = '\0';
				strncpy(ROI_values, opj_optarg, strlen(opj_optarg));
				ROI_values[strlen(opj_optarg)] = '\0';
				/*printf("ROI_values = %s [%d / %d]\n", ROI_values, strlen(ROI_values), size_optarg ); */
				parse_DA_values( ROI_values, &parameters->DA_x0, &parameters->DA_y0, &parameters->DA_x1, &parameters->DA_y1);

				free(ROI_values);
			}
			break;

			/* ----------------------------------------------------- */

			case 't':     		/* Input tile index */
			{
				sscanf(opj_optarg, "%ud", &parameters->tile_index);
				parameters->nb_tile_to_decode = 1;
			}
			break;

				/* ----------------------------------------------------- */								

			case 'x':			/* Creation of index file */
				{
					char *index = opj_optarg;
					strncpy(indexfilename, index, OPJ_PATH_LEN);
				}
				break;
				/* ----------------------------------------------------- */
				/* UniPG>> */
#ifdef USE_JPWL
			
			case 'W': 			/* activate JPWL correction */
			{
				char *token = NULL;

				token = strtok(opj_optarg, ",");
				while(token != NULL) {

					/* search expected number of components */
					if (*token == 'c') {

						static int compno;

						compno = JPWL_EXPECTED_COMPONENTS; /* predefined no. of components */

						if(sscanf(token, "c=%d", &compno) == 1) {
							/* Specified */
							if ((compno < 1) || (compno > 256)) {
								fprintf(stderr, "ERROR -> invalid number of components c = %d\n", compno);
								return 1;
							}
							parameters->jpwl_exp_comps = compno;

						} else if (!strcmp(token, "c")) {
							/* default */
							parameters->jpwl_exp_comps = compno; /* auto for default size */

						} else {
							fprintf(stderr, "ERROR -> invalid components specified = %s\n", token);
							return 1;
						};
					}

					/* search maximum number of tiles */
					if (*token == 't') {

						static int tileno;

						tileno = JPWL_MAXIMUM_TILES; /* maximum no. of tiles */

						if(sscanf(token, "t=%d", &tileno) == 1) {
							/* Specified */
							if ((tileno < 1) || (tileno > JPWL_MAXIMUM_TILES)) {
								fprintf(stderr, "ERROR -> invalid number of tiles t = %d\n", tileno);
								return 1;
							}
							parameters->jpwl_max_tiles = tileno;

						} else if (!strcmp(token, "t")) {
							/* default */
							parameters->jpwl_max_tiles = tileno; /* auto for default size */

						} else {
							fprintf(stderr, "ERROR -> invalid tiles specified = %s\n", token);
							return 1;
						};
					}

					/* next token or bust */
					token = strtok(NULL, ",");
				};
				parameters->jpwl_correct = OPJ_TRUE;
				fprintf(stdout, "JPWL correction capability activated\n");
				fprintf(stdout, "- expecting %d components\n", parameters->jpwl_exp_comps);
			}
			break;	
#endif /* USE_JPWL */
/* <<UniPG */            

				/* ----------------------------------------------------- */
			
			default:
				fprintf(stderr,"WARNING -> this option is not valid \"-%c %s\"\n",c, opj_optarg);
				break;
		}
	}while(c != -1);

	/* check for possible errors */
	if(img_fol->set_imgdir==1){
		if(!(parameters->infile[0]==0)){
			fprintf(stderr, "Error: options -ImgDir and -i cannot be used together !!\n");
			return 1;
		}
		if(img_fol->set_out_format == 0){
			fprintf(stderr, "Error: When -ImgDir is used, -OutFor <FORMAT> must be used !!\n");
			fprintf(stderr, "Only one format allowed! Valid format PGM, PPM, PNM, PGX, BMP, TIF, RAW and TGA!!\n");
			return 1;
		}
		if(!((parameters->outfile[0] == 0))){
			fprintf(stderr, "Error: options -ImgDir and -o cannot be used together !!\n");
			return 1;
		}
	}else{
		if((parameters->infile[0] == 0) || (parameters->outfile[0] == 0)) {
			fprintf(stderr, "Example: %s -i image.j2k -o image.pgm\n",argv[0]);
			fprintf(stderr, "    Try: %s -h\n",argv[0]);
			return 1;
		}
	}

	return 0;
}
Example #10
0
/* -------------------------------------------------------------------------- */
int parse_cmdline_decoder(int argc, char **argv, opj_dparameters_t *parameters,img_fol_t *img_fol, char *indexfilename) {
  /* parse the command line */
  int totlen;
  option_t long_option[]={
    {"ImgDir",REQ_ARG, NULL ,'y'},
  };

  const char optlist[] = "i:h";
  totlen=sizeof(long_option);
  img_fol->set_out_format = 0;
  while (1) {
    int c = getopt_long(argc, argv,optlist,long_option,totlen);
    if (c == -1)
      break;
    switch (c) {
      case 'i':      /* input file */
      {
        char *infile = optarg;
        parameters->decod_format = get_file_format(infile);
        switch(parameters->decod_format) {
          case J2K_CFMT:
          case JP2_CFMT:
          case JPT_CFMT:
            break;
          default:
            fprintf(stderr,
              "!! Unrecognized format for infile : %s [accept only *.j2k, *.jp2, *.jpc or *.jpt] !!\n\n",
              infile);
            return 1;
        }
        strncpy(parameters->infile, infile, sizeof(parameters->infile)-1);
      }
      break;

        /* ----------------------------------------------------- */

      case 'h':       /* display an help description */
        decode_help_display();
        return 1;

        /* ------------------------------------------------------ */

      case 'y':      /* Image Directory path */
        {
          img_fol->imgdirpath = (char*)malloc(strlen(optarg) + 1);
          strcpy(img_fol->imgdirpath,optarg);
          img_fol->set_imgdir=1;
        }
        break;

        /* ----------------------------------------------------- */

      default:
        fprintf(stderr,"WARNING -> this option is not valid \"-%c %s\"\n",c, optarg);
        break;
    }
  }

  /* check for possible errors */
  if(img_fol->set_imgdir==1){
    if(!(parameters->infile[0]==0)){
      fprintf(stderr, "Error: options -ImgDir and -i cannot be used together !!\n");
      return 1;
    }
    if(img_fol->set_out_format == 0){
      fprintf(stderr, "Error: When -ImgDir is used, -OutFor <FORMAT> must be used !!\n");
      fprintf(stderr, "Only one format allowed! Valid format PGM, PPM, PNM, PGX, BMP, TIF, RAW and TGA!!\n");
      return 1;
    }
    if(!((parameters->outfile[0] == 0))){
      fprintf(stderr, "Error: options -ImgDir and -o cannot be used together !!\n");
      return 1;
    }
  }else{
    if((parameters->infile[0] == 0) ) {
      fprintf(stderr, "Error: One of the options -i or -ImgDir must be specified\n");
      fprintf(stderr, "usage: image_to_j2k -i *.j2k/jp2/j2c (+ options)\n");
      return 1;
    }
  }

  return 0;
}
Example #11
0
int convert_fast_tunix (FILE *infile, FILE *outfile) {
  unsigned char a;
  unsigned char *in_bufpos;
  unsigned char *in_bufend;
  size_t file_remain;
  enum file_format infile_type;
  
  in_bufpos = in_buffer;
  
  (void) fseek(infile,0L,SEEK_END);
  file_remain = ftell(infile);
  rewind(infile);
  
  in_bufend = in_buffer + my_fread(infile, BUFFERSIZE);
  *in_bufend = '\0';
  
  infile_type = get_file_format (in_buffer);
  
  switch (infile_type) {
  case tunix:
    if (verbose)
      printf("%s: %s is already in Unix format, skipping.\n",
	     program_name,current_file);
    return (FALSE);
    break;

  case apple:
    /* Replace "in-vitro", so out_buffer isn't used */
    while (file_remain != 0) {
      a = *in_bufpos;
      if (a == CR)
	*in_bufpos++ = LF;
      else if (a == '\0'){       /* End of buffer reached */

	/* Write changed buffer out */
	my_fwrite(in_buffer,outfile,(int)(in_bufend - in_buffer));

	/* And reload */
	file_remain -= in_bufend - in_buffer;
	in_bufend = in_buffer + my_fread(infile, BUFFERSIZE);
	*in_bufend = '\0';
	in_bufpos = in_buffer;
      } else in_bufpos++;
    }
    return (TRUE);
    break;
    
  case dos:
    /* Couldn't speed it up, so use old routine */
    convert_tunix (infile, outfile);
    return (TRUE);
    break;
    
  case binary:
    return (FALSE);
    break;

  default:
    fprintf(stderr,"%s: Fatal internal error\n", program_name);
    exit (EXIT_FAILURE);
    break;
  } /* switch */
}
Example #12
0
int parse_cmdline_decoder(int argc, char **argv, opj_dparameters_t *parameters,img_fol_t *img_fol) {
	/* parse the command line */
	int totlen;
	opj_option_t long_option[]={
		{"ImgDir",REQ_ARG, NULL ,'y'},
		{"OutFor",REQ_ARG, NULL ,'O'},
	};

/* UniPG>> */
	const char optlist[] = "i:o:r:l:hx:"

#ifdef USE_JPWL
					"W:"
#endif /* USE_JPWL */
					;
	/*for (i=0; i<argc; i++) {
		printf("[%s]",argv[i]);
	}
	printf("\n");*/

/* <<UniPG */
	totlen=sizeof(long_option);
	img_fol->set_out_format = 0;
	reset_options_reading();

	while (1) {
		int c = opj_getopt_long(argc, argv,optlist,long_option,totlen);
		if (c == -1)
			break;
		switch (c) {
			case 'i':			/* input file */
			{
				char *infile = opj_optarg;
				parameters->decod_format = get_file_format(infile);
				switch(parameters->decod_format) {
					case J2K_CFMT:
					case JP2_CFMT:
					case JPT_CFMT:
						break;
					default:
						fprintf(stderr, 
							"!! Unrecognized format for infile : %s [accept only *.j2k, *.jp2, *.jpc or *.jpt] !!\n\n", 
							infile);
						return 1;
				}
				strncpy(parameters->infile, infile, sizeof(parameters->infile)-1);
			}
			break;
				
				/* ----------------------------------------------------- */

			case 'o':			/* output file */
			{
				char *outfile = opj_optarg;
				parameters->cod_format = get_file_format(outfile);
				switch(parameters->cod_format) {
					case PGX_DFMT:
					case PXM_DFMT:
					case BMP_DFMT:
					case TIF_DFMT:
					case RAW_DFMT:
					case TGA_DFMT:
						break;
					default:
						fprintf(stderr, "Unknown output format image %s [only *.pnm, *.pgm, *.ppm, *.pgx, *.bmp, *.tif, *.raw or *.tga]!! \n", outfile);
						return 1;
				}
				strncpy(parameters->outfile, outfile, sizeof(parameters->outfile)-1);
			}
			break;
			
				/* ----------------------------------------------------- */

			case 'O':			/* output format */
			{
				char outformat[50];
				char *of = opj_optarg;
				sprintf(outformat,".%s",of);
				img_fol->set_out_format = 1;
				parameters->cod_format = get_file_format(outformat);
				switch(parameters->cod_format) {
					case PGX_DFMT:
						img_fol->out_format = "pgx";
						break;
					case PXM_DFMT:
						img_fol->out_format = "ppm";
						break;
					case BMP_DFMT:
						img_fol->out_format = "bmp";
						break;
					case TIF_DFMT:
						img_fol->out_format = "tif";
						break;
					case RAW_DFMT:
						img_fol->out_format = "raw";
						break;
					case TGA_DFMT:
						img_fol->out_format = "raw";
						break;
					default:
						fprintf(stderr, "Unknown output format image %s [only *.pnm, *.pgm, *.ppm, *.pgx, *.bmp, *.tif, *.raw or *.tga]!! \n", outformat);
						return 1;
						break;
				}
			}
			break;

				/* ----------------------------------------------------- */


			case 'r':		/* reduce option */
			{
				sscanf(opj_optarg, "%d", &parameters->cp_reduce);
			}
			break;
			
				/* ----------------------------------------------------- */
      

			case 'l':		/* layering option */
			{
				sscanf(opj_optarg, "%d", &parameters->cp_layer);
			}
			break;
			
				/* ----------------------------------------------------- */

			case 'h': 			/* display an help description */
				decode_help_display();
				return 1;				

				/* ------------------------------------------------------ */

			case 'y':			/* Image Directory path */
				{
					img_fol->imgdirpath = (char*)opj_malloc(strlen(opj_optarg) + 1);
					strcpy(img_fol->imgdirpath,opj_optarg);
					img_fol->set_imgdir=1;
				}
				break;
				/* ----------------------------------------------------- */
/* UniPG>> */
#ifdef USE_JPWL
			
			case 'W': 			/* activate JPWL correction */
			{
				char *token = NULL;

				token = strtok(opj_optarg, ",");
				while(token != NULL) {

					/* search expected number of components */
					if (*token == 'c') {

						static int compno;

						compno = JPWL_EXPECTED_COMPONENTS; /* predefined no. of components */

						if(sscanf(token, "c=%d", &compno) == 1) {
							/* Specified */
							if ((compno < 1) || (compno > 256)) {
								fprintf(stderr, "ERROR -> invalid number of components c = %d\n", compno);
								return 1;
							}
							parameters->jpwl_exp_comps = compno;

						} else if (!strcmp(token, "c")) {
							/* default */
							parameters->jpwl_exp_comps = compno; /* auto for default size */

						} else {
							fprintf(stderr, "ERROR -> invalid components specified = %s\n", token);
							return 1;
						};
					}

					/* search maximum number of tiles */
					if (*token == 't') {

						static int tileno;

						tileno = JPWL_MAXIMUM_TILES; /* maximum no. of tiles */

						if(sscanf(token, "t=%d", &tileno) == 1) {
							/* Specified */
							if ((tileno < 1) || (tileno > JPWL_MAXIMUM_TILES)) {
								fprintf(stderr, "ERROR -> invalid number of tiles t = %d\n", tileno);
								return 1;
							}
							parameters->jpwl_max_tiles = tileno;

						} else if (!strcmp(token, "t")) {
							/* default */
							parameters->jpwl_max_tiles = tileno; /* auto for default size */

						} else {
							fprintf(stderr, "ERROR -> invalid tiles specified = %s\n", token);
							return 1;
						};
					}

					/* next token or bust */
					token = strtok(NULL, ",");
				};
				parameters->jpwl_correct = true;
				fprintf(stdout, "JPWL correction capability activated\n");
				fprintf(stdout, "- expecting %d components\n", parameters->jpwl_exp_comps);
			}
			break;	
#endif /* USE_JPWL */
/* <<UniPG */            

				/* ----------------------------------------------------- */
			
			default:
				fprintf(stderr,"WARNING -> this option is not valid \"-%c %s\"\n",c, opj_optarg);
				break;
		}
	}

	/* No check for possible errors before the -i and -o options are of course not mandatory*/

	return 0;
}
Example #13
0
dt_imageio_retval_t dt_imageio_open_j2k(dt_image_t *img, const char *filename, dt_mipmap_cache_allocator_t a)
{
  opj_dparameters_t parameters;   /* decompression parameters */
  opj_event_mgr_t event_mgr;      /* event manager */
  opj_image_t *image = NULL;
  FILE *fsrc = NULL;
  unsigned char *src = NULL;
  int file_length;
  opj_dinfo_t* dinfo = NULL;      /* handle to a decompressor */
  opj_cio_t *cio = NULL;
  OPJ_CODEC_FORMAT codec;
  int ret = DT_IMAGEIO_FILE_CORRUPTED;

  int file_format = get_file_format(filename);
  if(file_format == -1) return DT_IMAGEIO_FILE_CORRUPTED;

  if(!img->exif_inited)
    (void) dt_exif_read(img, filename);

  /* read the input file and put it in memory */
  /* ---------------------------------------- */
  fsrc = fopen(filename, "rb");
  if(!fsrc)
  {
    fprintf(stderr, "[j2k_open] Error: failed to open `%s' for reading\n", filename);
    return DT_IMAGEIO_FILE_NOT_FOUND;
  }
  fseek(fsrc, 0, SEEK_END);
  file_length = ftell(fsrc);
  fseek(fsrc, 0, SEEK_SET);
  src = (unsigned char *) malloc(file_length);
  if(fread(src, 1, file_length, fsrc) != (size_t)file_length)
  {
    free(src);
    fclose(fsrc);
    fprintf(stderr, "[j2k_open] Error: fread returned a number of elements different from the expected.\n");
    return DT_IMAGEIO_FILE_NOT_FOUND;
  }
  fclose(fsrc);

  if(memcmp(JP2_HEAD, src, sizeof(JP2_HEAD)) == 0)
  {
    file_format = JP2_CFMT; // just in case someone used the wrong extension
  }
  else if(memcmp(J2K_HEAD, src, sizeof(J2K_HEAD)) == 0)
  {
    file_format = J2K_CFMT; // just in case someone used the wrong extension
  }
  else // this will also reject jpt files.
  {
    free(src);
    fprintf(stderr, "[j2k_open] Error: `%s' has unsupported file format.\n", filename);
    return DT_IMAGEIO_FILE_CORRUPTED;
  }

  /* configure the event callbacks (not required) */
  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;

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

  /* decode the code-stream */
  /* ---------------------- */
  if(file_format == J2K_CFMT)        /* JPEG-2000 codestream */
    codec = CODEC_J2K;
  else if(file_format == JP2_CFMT)   /* JPEG 2000 compressed image data */
    codec = CODEC_JP2;
  else if(file_format == JPT_CFMT)   /* JPEG 2000, JPIP */
    codec = CODEC_JPT;
  else
  {
    free(src);
    return DT_IMAGEIO_FILE_CORRUPTED; // can't happen
  }

  /* get a decoder handle */
  dinfo = opj_create_decompress(codec);

  /* 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 */
  opj_setup_decoder(dinfo, &parameters);

  /* open a byte stream */
  cio = opj_cio_open((opj_common_ptr)dinfo, src, file_length);

  /* decode the stream and fill the image structure */
  image = opj_decode(dinfo, cio);

  /* close the byte stream */
  opj_cio_close(cio);

  /* free the memory containing the code-stream */
  free(src);

  if(!image)
  {
    fprintf(stderr, "[j2k_open] Error: failed to decode image `%s'\n", filename);
    ret = DT_IMAGEIO_FILE_CORRUPTED;
    goto end_of_the_world;
  }

  if(image->color_space == CLRSPC_SYCC)
  {
    color_sycc_to_rgb(image);
  }

  //FIXME: openjpeg didn't have support for icc profiles before version 1.5
  // this needs some #ifdef magic and proper implementation
#ifdef HAVE_OPENJPEG_ICC
  if(image->icc_profile_buf)
  {
#if defined(HAVE_LIBLCMS1) || defined(HAVE_LIBLCMS2)
    color_apply_icc_profile(image);
#endif

    free(image->icc_profile_buf);
    image->icc_profile_buf = NULL;
    image->icc_profile_len = 0;
  }
#endif

  /* create output image */
  /* ------------------- */
  long signed_offsets[4] = {0, 0, 0, 0};
  int float_divs[4] = {1, 1, 1, 1};

  // some sanity checks
  if(image->numcomps == 0 || image->x1 == 0  || image->y1 == 0)
  {
    fprintf(stderr, "[j2k_open] Error: invalid raw image parameters in `%s'\n", filename);
    ret = DT_IMAGEIO_FILE_CORRUPTED;
    goto end_of_the_world;
  }

  for(int i = 0; i < image->numcomps; i++)
  {
    if(image->comps[i].w != image->x1 || image->comps[i].h != image->y1)
    {
      fprintf(stderr, "[j2k_open] Error: some component has different size in `%s'\n", filename);
      ret = DT_IMAGEIO_FILE_CORRUPTED;
      goto end_of_the_world;
    }
    if(image->comps[i].prec > 16)
    {
      fprintf(stderr,"[j2k_open] Error: precision %d is larger than 16 in `%s'\n", image->comps[1].prec, filename);
      ret = DT_IMAGEIO_FILE_CORRUPTED;
      goto end_of_the_world;
    }
  }

  img->width = image->x1;
  img->height = image->y1;
  img->bpp = 4*sizeof(float);

  float *buf = (float *)dt_mipmap_cache_alloc(img, DT_MIPMAP_FULL, a);
  if(!buf)
  {
    ret = DT_IMAGEIO_CACHE_FULL;
    goto end_of_the_world;
  }

  int i = image->numcomps;
  if(i > 4) i = 4;

  while(i)
  {
    i--;

    if(image->comps[i].sgnd)
      signed_offsets[i] =  1 << (image->comps[i].prec - 1);

    float_divs[i] = (1 << image->comps[i].prec) - 1;
  }

  // numcomps == 1 : grey  -> r = grey, g = grey, b = grey
  // numcomps == 2 : grey, alpha -> r = grey, g = grey, b = grey. put alpha into the mix?
  // numcomps == 3 : rgb -> rgb
  // numcomps == 4 : rgb, alpha -> rgb. put alpha into the mix?

  // first try: ignore alpha.
  if(image->numcomps < 3) // 1, 2 => grayscale
  {
    for(int i = 0; i < img->width * img->height; i++)
      buf[i*4 + 0] = buf[i*4 + 1] = buf[i*4 + 2] = (float)(image->comps[0].data[i] + signed_offsets[0]) / float_divs[0];
  }
  else // 3, 4 => rgb
  {
    for(int i = 0; i < img->width * img->height; i++)
      for(int k = 0; k < 3; k++) buf[i*4 + k] = (float)(image->comps[k].data[i] + signed_offsets[k]) / float_divs[k];
  }

  ret = DT_IMAGEIO_OK;

end_of_the_world:
  /* free remaining structures */
  if(dinfo)
    opj_destroy_decompress(dinfo);

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

  return ret;
}
Example #14
0
int openjpeg2k_decode_mem(IMG_DAT **oimg_dat, int *lossyflag,
                      unsigned char *idata, const int ilen)
{
   IMG_DAT *img_dat;
   opj_image_t *image = NULL;
   opj_event_mgr_t event_mgr;
   opj_dinfo_t* dinfo = NULL;
   opj_cio_t *cio = NULL;
   opj_dparameters_t parameters;
   opj_codestream_info_t cstr_info;
   signed char* sgnd_buf = NULL;
   unsigned char* unsgnd_buf = NULL;
   int size;

   /* configure the event handler */
   memset(&event_mgr, 0, sizeof(opj_event_mgr_t));

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

   /* set the decode and encode format */
   parameters.decod_format = get_file_format("b.jp2");
   parameters.cod_format = get_file_format("a.raw");

   /* get a decoder handle */
   dinfo = opj_create_decompress(CODEC_JP2);

   /* 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 */
   opj_setup_decoder(dinfo, &parameters);

   /* open a byte stream */
   cio = opj_cio_open((opj_common_ptr)dinfo, idata, ilen);

   /* decode the stream and fill the image structure */
   image = opj_decode(dinfo, cio);

   if (!image) 
   {
      fprintf(stderr, "ERROR -> failed to decode image!\n");
      opj_destroy_decompress(dinfo);
      opj_cio_close(cio);
      return(-1);
   }

   opj_cio_close(cio);

   if (dinfo) 
   {
      opj_destroy_decompress(dinfo);
   }

   size = image->numcomps * image->comps[0].w * image->comps[0].h;

   /* only support unsigned jp2 image */
   if (image->comps[0].sgnd == 1)
   {
      fprintf(stderr, "ERROR -> Can't decode signed buffer!\n");
      opj_image_destroy(image);
      return(-2);
   }

   /* allocate buf for the Raw image */
   unsgnd_buf = (unsigned char *) malloc(size * sizeof(unsigned char));

   /* convert JP2 to Raw */
   if (image_to_raw(image, unsgnd_buf))
   {
      fprintf(stderr, "ERROR -> image_to_raw!\n");
      opj_image_destroy(image);
      return(-3);
   }

   /* consturct img_dat format */
   if ((img_dat_generate_openjpeg(&img_dat, image, unsgnd_buf)) != 0){
      free(unsgnd_buf);
      opj_image_destroy(image);
      fprintf(stderr, "ERROR : openjpeg2k_decode_mem: failed to generate img_dat\n");
      return(-4);
   }

   free(unsgnd_buf);

   opj_image_destroy(image);

   *oimg_dat = img_dat;
   *lossyflag = 0;

   return(0);
}
Example #15
0
int parse_cmdline_decoder(int argc, char **argv, opj_dparameters_t *parameters) {
	/* parse the command line */

/* UniPG>> */
	const char optlist[] = "i:o:r:l:h"

#ifdef USE_JPWL
					"W:"
#endif /* USE_JPWL */
					;
/* <<UniPG */

	while (1) {
		int c = getopt(argc, argv, optlist); /* >>JPWL<< */
		if (c == -1)
			break;
		switch (c) {
			case 'i':			/* input file */
			{
				char *infile = optarg;
				parameters->decod_format = get_file_format(infile);
				switch(parameters->decod_format) {
					case J2K_CFMT:
					case JP2_CFMT:
					case JPT_CFMT:
						break;
					default:
						fprintf(stderr, 
							"!! Unrecognized format for infile : %s [accept only *.j2k, *.jp2, *.jpc or *.jpt] !!\n\n", 
							infile);
						return 1;
				}
				strncpy(parameters->infile, infile, OPJ_PATH_LEN);
			}
			break;
				
				/* ----------------------------------------------------- */

			case 'o':			/* output file */
			{
				char *outfile = optarg;
				parameters->cod_format = get_file_format(outfile);
				switch(parameters->cod_format) {
					case PGX_DFMT:
					case PXM_DFMT:
					case BMP_DFMT:
						break;
					default:
						fprintf(stderr, "Unknown output format image %s [only *.pnm, *.pgm, *.ppm, *.pgx or *.bmp]!! \n", outfile);
						return 1;
				}
				strncpy(parameters->outfile, outfile, OPJ_PATH_LEN);
			}
			break;
			
				/* ----------------------------------------------------- */
			
    
			case 'r':		/* reduce option */
			{
				sscanf(optarg, "%d", &parameters->cp_reduce);
			}
			break;
			
				/* ----------------------------------------------------- */
      

			case 'l':		/* layering option */
			{
				sscanf(optarg, "%d", &parameters->cp_layer);
			}
			break;
			
				/* ----------------------------------------------------- */
			
			case 'h': 			/* display an help description */
				decode_help_display();
				return 1;				
            
/* UniPG>> */
#ifdef USE_JPWL
				/* ----------------------------------------------------- */	
			
			case 'W': 			/* activate JPWL correction */
			{
				char *token = NULL;

				token = strtok(optarg, ",");
				while(token != NULL) {

					/* search expected number of components */
					if (*token == 'c') {

						static int compno;

						compno = JPWL_EXPECTED_COMPONENTS; /* predefined no. of components */

						if(sscanf(token, "c=%d", &compno) == 1) {
							/* Specified */
							if ((compno < 1) || (compno > 256)) {
								fprintf(stderr, "ERROR -> invalid number of components c = %d\n", compno);
								return 1;
							}
							parameters->jpwl_exp_comps = compno;

						} else if (!strcmp(token, "c")) {
							/* default */
							parameters->jpwl_exp_comps = compno; /* auto for default size */

						} else {
							fprintf(stderr, "ERROR -> invalid components specified = %s\n", token);
							return 1;
						};
					}

					/* search maximum number of tiles */
					if (*token == 't') {

						static int tileno;

						tileno = JPWL_MAXIMUM_TILES; /* maximum no. of tiles */

						if(sscanf(token, "t=%d", &tileno) == 1) {
							/* Specified */
							if ((tileno < 1) || (tileno > JPWL_MAXIMUM_TILES)) {
								fprintf(stderr, "ERROR -> invalid number of tiles t = %d\n", tileno);
								return 1;
							}
							parameters->jpwl_max_tiles = tileno;

						} else if (!strcmp(token, "t")) {
							/* default */
							parameters->jpwl_max_tiles = tileno; /* auto for default size */

						} else {
							fprintf(stderr, "ERROR -> invalid tiles specified = %s\n", token);
							return 1;
						};
					}

					/* next token or bust */
					token = strtok(NULL, ",");
				};
				parameters->jpwl_correct = true;
				fprintf(stdout, "JPWL correction capability activated\n");
				fprintf(stdout, "- expecting %d components\n", parameters->jpwl_exp_comps);
			}
			break;	
#endif /* USE_JPWL */
/* <<UniPG */            

				/* ----------------------------------------------------- */
			
			default:
				fprintf(stderr,"WARNING -> this option is not valid \"-%c %s\"\n",c, optarg);
				break;
		}
	}

	/* check for possible errors */

	if((parameters->infile[0] == 0) || (parameters->outfile[0] == 0)) {
		fprintf(stderr,"ERROR -> At least one required argument is missing\nCheck j2k_to_image -h for usage information\n");
		return 1;
	}

	return 0;
}
Example #16
0
/* -------------------------------------------------------------------------- */
int parse_cmdline_decoder(int argc, char **argv, opj_decompress_parameters *parameters,img_fol_t *img_fol) {
	/* parse the command line */
	int totlen, c;
	opj_option_t long_option[]={
		{"ImgDir",    REQ_ARG, NULL,'y'},
		{"OutFor",    REQ_ARG, NULL,'O'},
		{"force-rgb", NO_ARG,  NULL, 1},
		{"upsample",  NO_ARG,  NULL, 1},
		{"split-pnm", NO_ARG,  NULL, 1}
	};

	const char optlist[] = "i:o:r:l:x:d:t:p:"

/* UniPG>> */
#ifdef USE_JPWL
					"W:"
#endif /* USE_JPWL */
/* <<UniPG */
            "h"		;

	long_option[2].flag = &(parameters->force_rgb);
	long_option[3].flag = &(parameters->upsample);
	long_option[4].flag = &(parameters->split_pnm);
	totlen=sizeof(long_option);
	opj_reset_options_reading();
	img_fol->set_out_format = 0;
	do {
		c = opj_getopt_long(argc, argv,optlist,long_option,totlen);
		if (c == -1)
			break;
		switch (c) {
			case 0: /* long opt with flag */
				break;
			case 'i':			/* input file */
			{
				char *infile = opj_optarg;
				parameters->decod_format = infile_format(infile);
				switch(parameters->decod_format) {
					case J2K_CFMT:
						break;
					case JP2_CFMT:
						break;
					case JPT_CFMT:
						break;
                                        case -2:
						fprintf(stderr, 
							"!! infile cannot be read: %s !!\n\n", 
							infile);
						return 1;
					default:
						fprintf(stderr, 
                            "[ERROR] Unknown input file format: %s \n"
                            "        Known file formats are *.j2k, *.jp2, *.jpc or *.jpt\n",
							infile);
						return 1;
				}
				if (opj_strcpy_s(parameters->infile, sizeof(parameters->infile), infile) != 0) {
					fprintf(stderr, "[ERROR] Path is too long\n");
					return 1;
				}
			}
			break;
				
				/* ----------------------------------------------------- */

			case 'o':			/* output file */
			{
				char *outfile = opj_optarg;
				parameters->cod_format = get_file_format(outfile);
				switch(parameters->cod_format) {
					case PGX_DFMT:
						break;
					case PXM_DFMT:
						break;
					case BMP_DFMT:
						break;
					case TIF_DFMT:
						break;
					case RAW_DFMT:
						break;
					case RAWL_DFMT:
						break;
					case TGA_DFMT:
						break;
					case PNG_DFMT:
						break;
					default:
						fprintf(stderr, "Unknown output format image %s [only *.png, *.pnm, *.pgm, *.ppm, *.pgx, *.bmp, *.tif, *.raw or *.tga]!!\n", outfile);
						return 1;
				}
				if (opj_strcpy_s(parameters->outfile, sizeof(parameters->outfile), outfile) != 0) {
					fprintf(stderr, "[ERROR] Path is too long\n");
					return 1;
				}
			}
			break;
			
				/* ----------------------------------------------------- */

			case 'O':			/* output format */
			{
				char outformat[50];
				char *of = opj_optarg;
				sprintf(outformat,".%s",of);
				img_fol->set_out_format = 1;
				parameters->cod_format = get_file_format(outformat);
				switch(parameters->cod_format) {
					case PGX_DFMT:
						img_fol->out_format = "pgx";
						break;
					case PXM_DFMT:
						img_fol->out_format = "ppm";
						break;
					case BMP_DFMT:
						img_fol->out_format = "bmp";
						break;
					case TIF_DFMT:
						img_fol->out_format = "tif";
						break;
					case RAW_DFMT:
						img_fol->out_format = "raw";
						break;
					case RAWL_DFMT:
						img_fol->out_format = "rawl";
						break;
					case TGA_DFMT:
						img_fol->out_format = "raw";
						break;
					case PNG_DFMT:
						img_fol->out_format = "png";
						break;
					default:
						fprintf(stderr, "Unknown output format image %s [only *.png, *.pnm, *.pgm, *.ppm, *.pgx, *.bmp, *.tif, *.raw or *.tga]!!\n", outformat);
						return 1;
						break;
				}
			}
			break;

				/* ----------------------------------------------------- */


			case 'r':		/* reduce option */
			{
				sscanf(opj_optarg, "%u", &(parameters->core.cp_reduce));
			}
			break;
			
				/* ----------------------------------------------------- */
      

			case 'l':		/* layering option */
			{
				sscanf(opj_optarg, "%u", &(parameters->core.cp_layer));
			}
			break;
			
				/* ----------------------------------------------------- */

			case 'h': 			/* display an help description */
				decode_help_display();
				return 1;				

            /* ----------------------------------------------------- */

			case 'y':			/* Image Directory path */
                {
					img_fol->imgdirpath = (char*)malloc(strlen(opj_optarg) + 1);
					strcpy(img_fol->imgdirpath,opj_optarg);
					img_fol->set_imgdir=1;
				}
				break;

				/* ----------------------------------------------------- */

			case 'd':     		/* Input decode ROI */
			{
				size_t size_optarg = (size_t)strlen(opj_optarg) + 1U;
				char *ROI_values = (char*) malloc(size_optarg);
				if (ROI_values == NULL) {
					fprintf(stderr, "[ERROR] Couldn't allocate memory\n");
					return 1;
				}
				ROI_values[0] = '\0';
				memcpy(ROI_values, opj_optarg, size_optarg);
				/*printf("ROI_values = %s [%d / %d]\n", ROI_values, strlen(ROI_values), size_optarg ); */
				parse_DA_values( ROI_values, &parameters->DA_x0, &parameters->DA_y0, &parameters->DA_x1, &parameters->DA_y1);

				free(ROI_values);
			}
			break;

			/* ----------------------------------------------------- */

			case 't':     		/* Input tile index */
			{
				sscanf(opj_optarg, "%u", &parameters->tile_index);
				parameters->nb_tile_to_decode = 1;
			}
			break;

				/* ----------------------------------------------------- */								

			case 'x':			/* Creation of index file */
				{
					if (opj_strcpy_s(parameters->indexfilename, sizeof(parameters->indexfilename), opj_optarg) != 0) {
						fprintf(stderr, "[ERROR] Path is too long\n");
						return 1;
					}
				}
				break;
				
				/* ----------------------------------------------------- */
			case 'p': /* Force precision */
				{
					if (!parse_precision(opj_optarg, parameters))
					{
						return 1;
					}
				}
				break;
				/* ----------------------------------------------------- */
				
				/* UniPG>> */
#ifdef USE_JPWL
			
			case 'W': 			/* activate JPWL correction */
			{
				char *token = NULL;

				token = strtok(opj_optarg, ",");
				while(token != NULL) {

					/* search expected number of components */
					if (*token == 'c') {

						static int compno;

						compno = JPWL_EXPECTED_COMPONENTS; /* predefined no. of components */

						if(sscanf(token, "c=%d", &compno) == 1) {
							/* Specified */
							if ((compno < 1) || (compno > 256)) {
								fprintf(stderr, "ERROR -> invalid number of components c = %d\n", compno);
								return 1;
							}
							parameters->jpwl_exp_comps = compno;

						} else if (!strcmp(token, "c")) {
							/* default */
							parameters->jpwl_exp_comps = compno; /* auto for default size */

						} else {
							fprintf(stderr, "ERROR -> invalid components specified = %s\n", token);
							return 1;
						};
					}

					/* search maximum number of tiles */
					if (*token == 't') {

						static int tileno;

						tileno = JPWL_MAXIMUM_TILES; /* maximum no. of tiles */

						if(sscanf(token, "t=%d", &tileno) == 1) {
							/* Specified */
							if ((tileno < 1) || (tileno > JPWL_MAXIMUM_TILES)) {
								fprintf(stderr, "ERROR -> invalid number of tiles t = %d\n", tileno);
								return 1;
							}
							parameters->jpwl_max_tiles = tileno;

						} else if (!strcmp(token, "t")) {
							/* default */
							parameters->jpwl_max_tiles = tileno; /* auto for default size */

						} else {
							fprintf(stderr, "ERROR -> invalid tiles specified = %s\n", token);
							return 1;
						};
					}

					/* next token or bust */
					token = strtok(NULL, ",");
				};
				parameters->jpwl_correct = OPJ_TRUE;
				fprintf(stdout, "JPWL correction capability activated\n");
				fprintf(stdout, "- expecting %d components\n", parameters->jpwl_exp_comps);
			}
			break;	
#endif /* USE_JPWL */
/* <<UniPG */            

				/* ----------------------------------------------------- */
			
        default:
            fprintf(stderr, "[WARNING] An invalid option has been ignored.\n");
            break;
		}
	}while(c != -1);

	/* check for possible errors */
	if(img_fol->set_imgdir==1){
		if(!(parameters->infile[0]==0)){
            fprintf(stderr, "[ERROR] options -ImgDir and -i cannot be used together.\n");
			return 1;
		}
		if(img_fol->set_out_format == 0){
            fprintf(stderr, "[ERROR] When -ImgDir is used, -OutFor <FORMAT> must be used.\n");
            fprintf(stderr, "Only one format allowed.\n"
                            "Valid format are PGM, PPM, PNM, PGX, BMP, TIF, RAW and TGA.\n");
			return 1;
		}
		if(!((parameters->outfile[0] == 0))){
            fprintf(stderr, "[ERROR] options -ImgDir and -o cannot be used together.\n");
			return 1;
		}
	}else{
		if((parameters->infile[0] == 0) || (parameters->outfile[0] == 0)) {
            fprintf(stderr, "[ERROR] Required parameters are missing\n"
                            "Example: %s -i image.j2k -o image.pgm\n",argv[0]);
            fprintf(stderr, "   Help: %s -h\n",argv[0]);
			return 1;
		}
	}

	return 0;
}