Exemplo n.º 1
0
	TargaLoader::TargaLoader(const IODevicePtr &iodevice, bool srgb)
		: file(iodevice), srgb(srgb)
	{
		read_header();
		read_image_id();
		read_color_map();
		read_image_data();
		decode_palette();
		decode_image();
	}
Exemplo n.º 2
0
	Image TGAReader::read_stream(std::istream &stream, const std::string &error_prefix)
	{
		this->stream = &stream;
		this->error_prefix = error_prefix;

		read_header();

		skip(header.id_field_length);

		if (header.color_map_type == ColorMapType::PRESENT)
			read_color_map();

		return read_image_data();
	}
Exemplo n.º 3
0
parse_switches (j_decompress_ptr cinfo, int argc, char **argv,
		int last_file_arg_seen, boolean for_real)
/* Parse optional switches.
 * Returns argv[] index of first file-name argument (== argc if none).
 * Any file names with indexes <= last_file_arg_seen are ignored;
 * they have presumably been processed in a previous iteration.
 * (Pass 0 for last_file_arg_seen on the first or only iteration.)
 * for_real is FALSE on the first (dummy) pass; we may skip any expensive
 * processing.
 */
{
  int argn;
  char * arg;

  /* Set up default JPEG parameters. */
  requested_fmt = DEFAULT_FMT;	/* set default output file format */
  outfilename = NULL;
  cinfo->err->trace_level = 0;

  /* Scan command line options, adjust parameters */

  for (argn = 1; argn < argc; argn++) {
    arg = argv[argn];
    if (*arg != '-') {
      /* Not a switch, must be a file name argument */
      if (argn <= last_file_arg_seen) {
	outfilename = NULL;	/* -outfile applies to just one input file */
	continue;		/* ignore this name if previously processed */
      }
      break;			/* else done parsing switches */
    }
    arg++;			/* advance past switch marker character */

    if (keymatch(arg, "bmp", 1)) {
      /* BMP output format. */
      requested_fmt = FMT_BMP;

    } else if (keymatch(arg, "colors", 1) || keymatch(arg, "colours", 1) ||
	       keymatch(arg, "quantize", 1) || keymatch(arg, "quantise", 1)) {
      /* Do color quantization. */
      int val;

      if (++argn >= argc)	/* advance to next argument */
	usage();
      if (sscanf(argv[argn], "%d", &val) != 1)
	usage();
      cinfo->desired_number_of_colors = val;
      cinfo->quantize_colors = TRUE;

    } else if (keymatch(arg, "dct", 2)) {
      /* Select IDCT algorithm. */
      if (++argn >= argc)	/* advance to next argument */
	usage();
      if (keymatch(argv[argn], "int", 1)) {
	cinfo->dct_method = JDCT_ISLOW;
      } else if (keymatch(argv[argn], "fast", 2)) {
	cinfo->dct_method = JDCT_IFAST;
      } else if (keymatch(argv[argn], "float", 2)) {
	cinfo->dct_method = JDCT_FLOAT;
      } else
	usage();

    } else if (keymatch(arg, "dither", 2)) {
      /* Select dithering algorithm. */
      if (++argn >= argc)	/* advance to next argument */
	usage();
      if (keymatch(argv[argn], "fs", 2)) {
	cinfo->dither_mode = JDITHER_FS;
      } else if (keymatch(argv[argn], "none", 2)) {
	cinfo->dither_mode = JDITHER_NONE;
      } else if (keymatch(argv[argn], "ordered", 2)) {
	cinfo->dither_mode = JDITHER_ORDERED;
      } else
	usage();

    } else if (keymatch(arg, "debug", 1) || keymatch(arg, "verbose", 1)) {
      /* Enable debug printouts. */
      /* On first -d, print version identification */
      static boolean printed_version = FALSE;

      if (! printed_version) {
	fprintf(stderr, "Independent JPEG Group's DJPEG, version %s\n%s\n",
		JVERSION, JCOPYRIGHT);
	printed_version = TRUE;
      }
      cinfo->err->trace_level++;

    } else if (keymatch(arg, "fast", 1)) {
      /* Select recommended processing options for quick-and-dirty output. */
      cinfo->two_pass_quantize = FALSE;
      cinfo->dither_mode = JDITHER_ORDERED;
      if (! cinfo->quantize_colors) /* don't override an earlier -colors */
	cinfo->desired_number_of_colors = 216;
      cinfo->dct_method = JDCT_FASTEST;
      cinfo->do_fancy_upsampling = FALSE;

    } else if (keymatch(arg, "gif", 1)) {
      /* GIF output format. */
      requested_fmt = FMT_GIF;

    } else if (keymatch(arg, "grayscale", 2) || keymatch(arg, "greyscale",2)) {
      /* Force monochrome output. */
      cinfo->out_color_space = JCS_GRAYSCALE;

    } else if (keymatch(arg, "map", 3)) {
      /* Quantize to a color map taken from an input file. */
      if (++argn >= argc)	/* advance to next argument */
	usage();
      if (for_real) {		/* too expensive to do twice! */
#ifdef QUANT_2PASS_SUPPORTED	/* otherwise can't quantize to supplied map */
	FILE * mapfile;

	if ((mapfile = fopen(argv[argn], READ_BINARY)) == NULL) {
	  fprintf(stderr, "%s: can't open %s\n", progname, argv[argn]);
	  exit(EXIT_FAILURE);
	}
	read_color_map(cinfo, mapfile);
	fclose(mapfile);
	cinfo->quantize_colors = TRUE;
#else
	ERREXIT(cinfo, JERR_NOT_COMPILED);
#endif
      }

    } else if (keymatch(arg, "maxmemory", 3)) {
      /* Maximum memory in Kb (or Mb with 'm'). */
      long lval;
      char ch = 'x';

      if (++argn >= argc)	/* advance to next argument */
	usage();
      if (sscanf(argv[argn], "%ld%c", &lval, &ch) < 1)
	usage();
      if (ch == 'm' || ch == 'M')
	lval *= 1000L;
      cinfo->mem->max_memory_to_use = lval * 1000L;

    } else if (keymatch(arg, "nosmooth", 3)) {
      /* Suppress fancy upsampling */
      cinfo->do_fancy_upsampling = FALSE;

    } else if (keymatch(arg, "onepass", 3)) {
      /* Use fast one-pass quantization. */
      cinfo->two_pass_quantize = FALSE;

    } else if (keymatch(arg, "os2", 3)) {
      /* BMP output format (OS/2 flavor). */
      requested_fmt = FMT_OS2;

    } else if (keymatch(arg, "outfile", 4)) {
      /* Set output file name. */
      if (++argn >= argc)	/* advance to next argument */
	usage();
      outfilename = argv[argn];	/* save it away for later use */

    } else if (keymatch(arg, "pnm", 1) || keymatch(arg, "ppm", 1)) {
      /* PPM/PGM output format. */
      requested_fmt = FMT_PPM;

    } else if (keymatch(arg, "rle", 1)) {
      /* RLE output format. */
      requested_fmt = FMT_RLE;

    } else if (keymatch(arg, "scale", 1)) {
      /* Scale the output image by a fraction M/N. */
      if (++argn >= argc)	/* advance to next argument */
	usage();
      if (sscanf(argv[argn], "%d/%d",
		 &cinfo->scale_num, &cinfo->scale_denom) < 1)
	usage();

    } else if (keymatch(arg, "targa", 1)) {
      /* Targa output format. */
      requested_fmt = FMT_TARGA;

    } else {
      usage();			/* bogus switch */
    }
  }

  return argn;			/* return index of next arg (file name) */
}
Exemplo n.º 4
0
Errcode read_tiftags(Tiff_file *tf)
/*****************************************************************************
 * load all the tags in an ifd, save the data we care about along the way.
 *	after reading everything in the ifd, this routine attempts to fudge in
 *	any data that was missing (eg, strip offsets, strip bytecounts).  it also
 *	verifies that the required fields were present (our idea of required
 *	fields, not tiff's idea.  we require image width and height; we can fake
 *	just about anything else.)
 ****************************************************************************/
{
	Errcode err;
	short	entry_count;
	Dirent	entry;
	int 	bps_count;

	/*
	 * plug in default values for things we care about...
	 */

	tf->width = 0;
	tf->height = 0;
	tf->longest_strip = 0;
	tf->strips_per_image = 0;
	tf->planar_configuration = 1;
	tf->rows_per_strip = 0x7fffffff;
	tf->samples_per_pixel = 1;
	tf->bits_per_sample[0] = 1;
	tf->bits_per_sample[1] = 0;
	tf->bits_per_sample[2] = 0;
	tf->compression = CMPRS_NONE;
	tf->photometric = PHMET_GREY_0ISBLACK;
	tf->min_sample_value = 0;
	tf->max_sample_value = 0;	/* default will be calc'd later if needed */

	/*
	 * the saved offset of the next ifd becomes the offset to the current
	 * ifd.  we seek to that location (just in case), and load the count
	 * of entries in the ifd, which will be used as a loop counter.
	 */

	tf->off_ifd_start = tf->off_ifd_cur = tf->off_ifd_next;
	if (Success != fseek(tf->file, tf->off_ifd_cur, SEEK_SET))
		return pj_errno_errcode();
	if (1 != fread(&entry_count, sizeof(entry_count), 1, tf->file))
		return pj_errno_errcode();
	SWAPW(&entry_count);
	tf->off_ifd_cur += sizeof(entry_count);

	/*
	 * loop through all the entries (tags) in the ifd, storing off the data
	 * we care about into our own structure as we go.
	 */

	while (entry_count--)
		{
		if (Success != (err = read_next_dirent(tf, &entry)))
			return err;

		switch (entry.tag)
			{

			case TAG_IMAGE_WIDTH:

				tf->width = entry.value.word;
				break;

			case TAG_IMAGE_LENGTH:

				tf->height = entry.value.word;
				break;

			case TAG_BITS_PER_SAMPLE:

				bps_count = entry.count;
				switch (entry.count)
					{
					case 1:
						tf->bits_per_sample[0] = entry.value.word;
						if (tf->bits_per_sample[0] > 8)
							return Err_wrong_type;
						break;
					case 3:
						if (Success != fseek(tf->file, entry.value.offset, SEEK_SET))
							return pj_errno_errcode();
						if (3 != fread(tf->bits_per_sample, sizeof(short), 3, tf->file))
							return Err_truncated;
						if (tf->swap_bytes)
							{
							swapw(&tf->bits_per_sample[0]);
							swapw(&tf->bits_per_sample[1]);
							swapw(&tf->bits_per_sample[2]);
							}
						if (tf->bits_per_sample[0] != 8 ||
							tf->bits_per_sample[1] != 8 ||
							tf->bits_per_sample[2] != 8 )
							return Err_wrong_type;
						break;
					default:
						return Err_wrong_type;
					}
				break;

			case TAG_COMPRESSION:

				tf->compression = entry.value.uword;
				break;

			case TAG_PHOTOMETRIC_INTERP:

				tf->photometric = entry.value.word;
				break;

			case TAG_STRIP_OFFSETS:
			case TAG_SHORT_STRIP_OFFSETS:	/* Aldus private tag 32768 */

				if (Success != (err = read_strip_offsets(tf, entry.value.offset,
									entry.type, entry.count)))
					return err;
				break;

			case TAG_SAMPLES_PER_PIXEL:

				tf->samples_per_pixel = entry.value.word;
				switch (tf->samples_per_pixel)
					{
					case 1:
						if (tf->photometric == PHMET_RGB)
							return Err_format;
						break;
					case 3:
						tf->photometric = PHMET_RGB;	/* force RGB interp */
						break;
					default:
						return Err_wrong_type;
					}
				break;

			case TAG_ROWS_PER_STRIP:

				tf->rows_per_strip =
					(entry.type == TYPE_SHORT) ? entry.value.word : entry.value.dword;
				break;

			case TAG_STRIP_BYTE_COUNTS:

				if (Success != (err = read_strip_bytecounts(tf, entry.value.offset,
									entry.type, entry.count)))
					return err;
				break;

			case TAG_MIN_SAMPLE_VALUE:
			case TAG_MAX_SAMPLE_VALUE:
				/* we ignore these fields and calc values below.
				 * we use the min/max values to calculate greyscale
				 * pallete entries, but on the advice of of the TIFF 5.0
				 * doc ('these values should not affect the visual display
				 * of the data, they are for statistical purposes only'),
				 * we use 2**bits_per_sample instead of max_sample_value.
				 */
				 break;

			case TAG_PLANAR_CONFIG:

				tf->planar_configuration = entry.value.word;
				break;

			case TAG_COLORMAP:

				if (Success != (err = read_color_map(tf, entry.value.offset, entry.count)))
					return err;
				break;

			default:
				break;
			}
		tf->off_ifd_cur += sizeof(entry);
		}

	/*
	 * read the offset to the next ifd; we may need it for multi-image file
	 */

	if (Success != fseek(tf->file, tf->off_ifd_cur, SEEK_SET))
		return pj_errno_errcode();
	if (1 != fread(&tf->off_ifd_next, sizeof(tf->off_ifd_next), 1, tf->file))
		return Err_truncated;
	SWAPD(&tf->off_ifd_next);

	/*
	 * if we have RGB data but we only got one bits_per_sample value,
	 * propogate it to the other two entries in the array.
	 * calculate the total pixel depth.
	 */

	if (tf->photometric == PHMET_RGB && bps_count != 3)
		{
		tf->bits_per_sample[1] = tf->bits_per_sample[0];
		tf->bits_per_sample[2] = tf->bits_per_sample[0];
		}

	tf->pixel_depth = tf->bits_per_sample[0] +
					  tf->bits_per_sample[1] +
					  tf->bits_per_sample[2];

	/*
	 * calculate max_sample_value as (2**bits_per_sample)-1...
	 */

	if (tf->max_sample_value == 0)
		tf->max_sample_value = (0x01 << tf->bits_per_sample[0]) - 1;

	/*
	 * hmmm.  it seems some tif files don't have strip bytecount entries.
	 * we'll see symptoms of that here if the longest strip size is zero
	 * after reading all the tags.	when this occurs, we call a routine
	 * to calculate the byte counts.  the routine does some sanity checking
	 * and will return an error if the counts look totally unreasonable.
	 */

	if (tf->longest_strip == 0)
		if (Success != (err = calc_strip_bytecounts(tf)))
			return err;

	/*
	 * all entries in this ifd have been dealt with, make sure we have
	 * the minimum required data to run the rest of the program...
	 *	 checking strips_per_image ensures that we got strip offsets.
	 *	 checking longest_strip ensures we got (or calc'd) strip bytecounts.
	 */

	 if (tf->width == 0 ||
		 tf->height == 0 ||
		 tf->strips_per_image == 0 ||
		 tf->longest_strip == 0)
		return Err_format;


	return Success;
}
Exemplo n.º 5
0
static Errcode read_256_color_frame(Rfile *rf)
/* Read and parse all the opcodes in a RND file. */
{
int opcode;
Rcel *screen = rf->screen;

	/* clear color map to zeros */
	stuff_bytes(0,screen->cmap->ctab,
				screen->cmap->num_colors * sizeof(Rgb3));

#ifdef DEBUG
	#define PRT(s) printf(s)
#else
	#define PRT(s)
#endif /* DEBUG */


	for(;;)
	{
		opcode = fgetc(rf->file);

		switch(opcode)
		{
			case EOF:
				return(Success);
   			case RND_CLEAR:   /* Clear entire display */
				pj_set_rast(screen,0);
				continue;
   			case RND_CMAPB:  /* Begin Color map */
				read_color_map(rf);
				break;
   			case RND_POLY:   /* Polygon */
				read_polygon(rf);
				break;
   			case RND_CRANGE:  /* Continous-color range */
				PRT("RND_CRANGE");
				goto bad_opcode;
   			case RND_CPOLY:   /* Continous-color polygon */
				PRT("RND_CPOLY");
				goto bad_opcode;
			case RND_WSLINE:  /* Output Scan line to driver */
				read_scanline(rf);
				break;
			case RND_RSLINE:  /* Input Scan line from driver */
				PRT("RND_RSLINE");
				goto bad_opcode;
			case RND_RCMAP:   /* Input color map rgb from driver */
				PRT("RND_RCMAP");
				goto bad_opcode;
			case RND_ETAIL:   /* i/o details for hard copy drivers */
				PRT("RND_ETAIL");
				goto bad_opcode;
			case RND_CFGREC:  /* execution time configuration record */
				PRT("RND_CFGREC");
				goto bad_opcode;
			case RND_NEWCFG:  /* new configuration record */
				PRT("RND_NEWCFG");
				goto bad_opcode;
			case RND_CHGCFG:  /* change configuration record */
				PRT("RND_CHGCFG");
				goto bad_opcode;
			case RND_SHOWCFG: /* show configuration record */
				PRT("RND_SHOWCFG");
				goto bad_opcode;
			case RND_FNAME:
				PRT("RND_FNAME");
				goto bad_opcode;
			default:
			bad_opcode:
#ifdef DEBUG
				printf("#%2d @ %06x", opcode, ftell(rf->file));
#endif /* DEBUG */
				rf->lasterr = Err_format;
				goto error;
		}
		if(rf->lasterr < Success)
			goto error;
	}
error:
	return(rf->lasterr);

#undef PRT
}