kusano 7d535a
/*
kusano 7d535a
 * jpegtran.c
kusano 7d535a
 *
kusano 7d535a
 * Copyright (C) 1995-2012, Thomas G. Lane, Guido Vollbeding.
kusano 7d535a
 * This file is part of the Independent JPEG Group's software.
kusano 7d535a
 * For conditions of distribution and use, see the accompanying README file.
kusano 7d535a
 *
kusano 7d535a
 * This file contains a command-line user interface for JPEG transcoding.
kusano 7d535a
 * It is very similar to cjpeg.c, and partly to djpeg.c, but provides
kusano 7d535a
 * lossless transcoding between different JPEG file formats.  It also
kusano 7d535a
 * provides some lossless and sort-of-lossless transformations of JPEG data.
kusano 7d535a
 */
kusano 7d535a
kusano 7d535a
#include "cdjpeg.h"		/* Common decls for cjpeg/djpeg applications */
kusano 7d535a
#include "transupp.h"		/* Support routines for jpegtran */
kusano 7d535a
#include "jversion.h"		/* for version message */
kusano 7d535a
kusano 7d535a
#ifdef USE_CCOMMAND		/* command-line reader for Macintosh */
kusano 7d535a
#ifdef __MWERKS__
kusano 7d535a
#include <sioux.h>              /* Metrowerks needs this */</sioux.h>
kusano 7d535a
#include <console.h>		/* ... and this */</console.h>
kusano 7d535a
#endif
kusano 7d535a
#ifdef THINK_C
kusano 7d535a
#include <console.h>		/* Think declares it here */</console.h>
kusano 7d535a
#endif
kusano 7d535a
#endif
kusano 7d535a
kusano 7d535a
kusano 7d535a
/*
kusano 7d535a
 * Argument-parsing code.
kusano 7d535a
 * The switch parser is designed to be useful with DOS-style command line
kusano 7d535a
 * syntax, ie, intermixed switches and file names, where only the switches
kusano 7d535a
 * to the left of a given file name affect processing of that file.
kusano 7d535a
 * The main program in this file doesn't actually use this capability...
kusano 7d535a
 */
kusano 7d535a
kusano 7d535a
kusano 7d535a
static const char * progname;	/* program name for error messages */
kusano 7d535a
static char * outfilename;	/* for -outfile switch */
kusano 7d535a
static char * scaleoption;	/* -scale switch */
kusano 7d535a
static JCOPY_OPTION copyoption;	/* -copy switch */
kusano 7d535a
static jpeg_transform_info transformoption; /* image transformation options */
kusano 7d535a
kusano 7d535a
kusano 7d535a
LOCAL(void)
kusano 7d535a
usage (void)
kusano 7d535a
/* complain about bad command line */
kusano 7d535a
{
kusano 7d535a
  fprintf(stderr, "usage: %s [switches] ", progname);
kusano 7d535a
#ifdef TWO_FILE_COMMANDLINE
kusano 7d535a
  fprintf(stderr, "inputfile outputfile\n");
kusano 7d535a
#else
kusano 7d535a
  fprintf(stderr, "[inputfile]\n");
kusano 7d535a
#endif
kusano 7d535a
kusano 7d535a
  fprintf(stderr, "Switches (names may be abbreviated):\n");
kusano 7d535a
  fprintf(stderr, "  -copy none     Copy no extra markers from source file\n");
kusano 7d535a
  fprintf(stderr, "  -copy comments Copy only comment markers (default)\n");
kusano 7d535a
  fprintf(stderr, "  -copy all      Copy all extra markers\n");
kusano 7d535a
#ifdef ENTROPY_OPT_SUPPORTED
kusano 7d535a
  fprintf(stderr, "  -optimize      Optimize Huffman table (smaller file, but slow compression)\n");
kusano 7d535a
#endif
kusano 7d535a
#ifdef C_PROGRESSIVE_SUPPORTED
kusano 7d535a
  fprintf(stderr, "  -progressive   Create progressive JPEG file\n");
kusano 7d535a
#endif
kusano 7d535a
  fprintf(stderr, "Switches for modifying the image:\n");
kusano 7d535a
#if TRANSFORMS_SUPPORTED
kusano 7d535a
  fprintf(stderr, "  -crop WxH+X+Y  Crop to a rectangular subarea\n");
kusano 7d535a
  fprintf(stderr, "  -grayscale     Reduce to grayscale (omit color data)\n");
kusano 7d535a
  fprintf(stderr, "  -flip [horizontal|vertical]  Mirror image (left-right or top-bottom)\n");
kusano 7d535a
  fprintf(stderr, "  -perfect       Fail if there is non-transformable edge blocks\n");
kusano 7d535a
  fprintf(stderr, "  -rotate [90|180|270]         Rotate image (degrees clockwise)\n");
kusano 7d535a
#endif
kusano 7d535a
  fprintf(stderr, "  -scale M/N     Scale output image by fraction M/N, eg, 1/8\n");
kusano 7d535a
#if TRANSFORMS_SUPPORTED
kusano 7d535a
  fprintf(stderr, "  -transpose     Transpose image\n");
kusano 7d535a
  fprintf(stderr, "  -transverse    Transverse transpose image\n");
kusano 7d535a
  fprintf(stderr, "  -trim          Drop non-transformable edge blocks\n");
kusano 7d535a
#endif
kusano 7d535a
  fprintf(stderr, "Switches for advanced users:\n");
kusano 7d535a
#ifdef C_ARITH_CODING_SUPPORTED
kusano 7d535a
  fprintf(stderr, "  -arithmetic    Use arithmetic coding\n");
kusano 7d535a
#endif
kusano 7d535a
  fprintf(stderr, "  -restart N     Set restart interval in rows, or in blocks with B\n");
kusano 7d535a
  fprintf(stderr, "  -maxmemory N   Maximum memory to use (in kbytes)\n");
kusano 7d535a
  fprintf(stderr, "  -outfile name  Specify name for output file\n");
kusano 7d535a
  fprintf(stderr, "  -verbose  or  -debug   Emit debug output\n");
kusano 7d535a
  fprintf(stderr, "Switches for wizards:\n");
kusano 7d535a
#ifdef C_MULTISCAN_FILES_SUPPORTED
kusano 7d535a
  fprintf(stderr, "  -scans file    Create multi-scan JPEG per script file\n");
kusano 7d535a
#endif
kusano 7d535a
  exit(EXIT_FAILURE);
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
kusano 7d535a
LOCAL(void)
kusano 7d535a
select_transform (JXFORM_CODE transform)
kusano 7d535a
/* Silly little routine to detect multiple transform options,
kusano 7d535a
 * which we can't handle.
kusano 7d535a
 */
kusano 7d535a
{
kusano 7d535a
#if TRANSFORMS_SUPPORTED
kusano 7d535a
  if (transformoption.transform == JXFORM_NONE ||
kusano 7d535a
      transformoption.transform == transform) {
kusano 7d535a
    transformoption.transform = transform;
kusano 7d535a
  } else {
kusano 7d535a
    fprintf(stderr, "%s: can only do one image transformation at a time\n",
kusano 7d535a
	    progname);
kusano 7d535a
    usage();
kusano 7d535a
  }
kusano 7d535a
#else
kusano 7d535a
  fprintf(stderr, "%s: sorry, image transformation was not compiled\n",
kusano 7d535a
	  progname);
kusano 7d535a
  exit(EXIT_FAILURE);
kusano 7d535a
#endif
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
kusano 7d535a
LOCAL(int)
kusano 7d535a
parse_switches (j_compress_ptr cinfo, int argc, char **argv,
kusano 7d535a
		int last_file_arg_seen, boolean for_real)
kusano 7d535a
/* Parse optional switches.
kusano 7d535a
 * Returns argv[] index of first file-name argument (== argc if none).
kusano 7d535a
 * Any file names with indexes <= last_file_arg_seen are ignored;
kusano 7d535a
 * they have presumably been processed in a previous iteration.
kusano 7d535a
 * (Pass 0 for last_file_arg_seen on the first or only iteration.)
kusano 7d535a
 * for_real is FALSE on the first (dummy) pass; we may skip any expensive
kusano 7d535a
 * processing.
kusano 7d535a
 */
kusano 7d535a
{
kusano 7d535a
  int argn;
kusano 7d535a
  char * arg;
kusano 7d535a
  boolean simple_progressive;
kusano 7d535a
  char * scansarg = NULL;	/* saves -scans parm if any */
kusano 7d535a
kusano 7d535a
  /* Set up default JPEG parameters. */
kusano 7d535a
  simple_progressive = FALSE;
kusano 7d535a
  outfilename = NULL;
kusano 7d535a
  scaleoption = NULL;
kusano 7d535a
  copyoption = JCOPYOPT_DEFAULT;
kusano 7d535a
  transformoption.transform = JXFORM_NONE;
kusano 7d535a
  transformoption.perfect = FALSE;
kusano 7d535a
  transformoption.trim = FALSE;
kusano 7d535a
  transformoption.force_grayscale = FALSE;
kusano 7d535a
  transformoption.crop = FALSE;
kusano 7d535a
  cinfo->err->trace_level = 0;
kusano 7d535a
kusano 7d535a
  /* Scan command line options, adjust parameters */
kusano 7d535a
kusano 7d535a
  for (argn = 1; argn < argc; argn++) {
kusano 7d535a
    arg = argv[argn];
kusano 7d535a
    if (*arg != '-') {
kusano 7d535a
      /* Not a switch, must be a file name argument */
kusano 7d535a
      if (argn <= last_file_arg_seen) {
kusano 7d535a
	outfilename = NULL;	/* -outfile applies to just one input file */
kusano 7d535a
	continue;		/* ignore this name if previously processed */
kusano 7d535a
      }
kusano 7d535a
      break;			/* else done parsing switches */
kusano 7d535a
    }
kusano 7d535a
    arg++;			/* advance past switch marker character */
kusano 7d535a
kusano 7d535a
    if (keymatch(arg, "arithmetic", 1)) {
kusano 7d535a
      /* Use arithmetic coding. */
kusano 7d535a
#ifdef C_ARITH_CODING_SUPPORTED
kusano 7d535a
      cinfo->arith_code = TRUE;
kusano 7d535a
#else
kusano 7d535a
      fprintf(stderr, "%s: sorry, arithmetic coding not supported\n",
kusano 7d535a
	      progname);
kusano 7d535a
      exit(EXIT_FAILURE);
kusano 7d535a
#endif
kusano 7d535a
kusano 7d535a
    } else if (keymatch(arg, "copy", 2)) {
kusano 7d535a
      /* Select which extra markers to copy. */
kusano 7d535a
      if (++argn >= argc)	/* advance to next argument */
kusano 7d535a
	usage();
kusano 7d535a
      if (keymatch(argv[argn], "none", 1)) {
kusano 7d535a
	copyoption = JCOPYOPT_NONE;
kusano 7d535a
      } else if (keymatch(argv[argn], "comments", 1)) {
kusano 7d535a
	copyoption = JCOPYOPT_COMMENTS;
kusano 7d535a
      } else if (keymatch(argv[argn], "all", 1)) {
kusano 7d535a
	copyoption = JCOPYOPT_ALL;
kusano 7d535a
      } else
kusano 7d535a
	usage();
kusano 7d535a
kusano 7d535a
    } else if (keymatch(arg, "crop", 2)) {
kusano 7d535a
      /* Perform lossless cropping. */
kusano 7d535a
#if TRANSFORMS_SUPPORTED
kusano 7d535a
      if (++argn >= argc)	/* advance to next argument */
kusano 7d535a
	usage();
kusano 7d535a
      if (! jtransform_parse_crop_spec(&transformoption, argv[argn])) {
kusano 7d535a
	fprintf(stderr, "%s: bogus -crop argument '%s'\n",
kusano 7d535a
		progname, argv[argn]);
kusano 7d535a
	exit(EXIT_FAILURE);
kusano 7d535a
      }
kusano 7d535a
#else
kusano 7d535a
      select_transform(JXFORM_NONE);	/* force an error */
kusano 7d535a
#endif
kusano 7d535a
kusano 7d535a
    } else if (keymatch(arg, "debug", 1) || keymatch(arg, "verbose", 1)) {
kusano 7d535a
      /* Enable debug printouts. */
kusano 7d535a
      /* On first -d, print version identification */
kusano 7d535a
      static boolean printed_version = FALSE;
kusano 7d535a
kusano 7d535a
      if (! printed_version) {
kusano 7d535a
	fprintf(stderr, "Independent JPEG Group's JPEGTRAN, version %s\n%s\n",
kusano 7d535a
		JVERSION, JCOPYRIGHT);
kusano 7d535a
	printed_version = TRUE;
kusano 7d535a
      }
kusano 7d535a
      cinfo->err->trace_level++;
kusano 7d535a
kusano 7d535a
    } else if (keymatch(arg, "flip", 1)) {
kusano 7d535a
      /* Mirror left-right or top-bottom. */
kusano 7d535a
      if (++argn >= argc)	/* advance to next argument */
kusano 7d535a
	usage();
kusano 7d535a
      if (keymatch(argv[argn], "horizontal", 1))
kusano 7d535a
	select_transform(JXFORM_FLIP_H);
kusano 7d535a
      else if (keymatch(argv[argn], "vertical", 1))
kusano 7d535a
	select_transform(JXFORM_FLIP_V);
kusano 7d535a
      else
kusano 7d535a
	usage();
kusano 7d535a
kusano 7d535a
    } else if (keymatch(arg, "grayscale", 1) || keymatch(arg, "greyscale",1)) {
kusano 7d535a
      /* Force to grayscale. */
kusano 7d535a
#if TRANSFORMS_SUPPORTED
kusano 7d535a
      transformoption.force_grayscale = TRUE;
kusano 7d535a
#else
kusano 7d535a
      select_transform(JXFORM_NONE);	/* force an error */
kusano 7d535a
#endif
kusano 7d535a
kusano 7d535a
    } else if (keymatch(arg, "maxmemory", 3)) {
kusano 7d535a
      /* Maximum memory in Kb (or Mb with 'm'). */
kusano 7d535a
      long lval;
kusano 7d535a
      char ch = 'x';
kusano 7d535a
kusano 7d535a
      if (++argn >= argc)	/* advance to next argument */
kusano 7d535a
	usage();
kusano 7d535a
      if (sscanf(argv[argn], "%ld%c", &lval, &ch) < 1)
kusano 7d535a
	usage();
kusano 7d535a
      if (ch == 'm' || ch == 'M')
kusano 7d535a
	lval *= 1000L;
kusano 7d535a
      cinfo->mem->max_memory_to_use = lval * 1000L;
kusano 7d535a
kusano 7d535a
    } else if (keymatch(arg, "optimize", 1) || keymatch(arg, "optimise", 1)) {
kusano 7d535a
      /* Enable entropy parm optimization. */
kusano 7d535a
#ifdef ENTROPY_OPT_SUPPORTED
kusano 7d535a
      cinfo->optimize_coding = TRUE;
kusano 7d535a
#else
kusano 7d535a
      fprintf(stderr, "%s: sorry, entropy optimization was not compiled\n",
kusano 7d535a
	      progname);
kusano 7d535a
      exit(EXIT_FAILURE);
kusano 7d535a
#endif
kusano 7d535a
kusano 7d535a
    } else if (keymatch(arg, "outfile", 4)) {
kusano 7d535a
      /* Set output file name. */
kusano 7d535a
      if (++argn >= argc)	/* advance to next argument */
kusano 7d535a
	usage();
kusano 7d535a
      outfilename = argv[argn];	/* save it away for later use */
kusano 7d535a
kusano 7d535a
    } else if (keymatch(arg, "perfect", 2)) {
kusano 7d535a
      /* Fail if there is any partial edge MCUs that the transform can't
kusano 7d535a
       * handle. */
kusano 7d535a
      transformoption.perfect = TRUE;
kusano 7d535a
kusano 7d535a
    } else if (keymatch(arg, "progressive", 2)) {
kusano 7d535a
      /* Select simple progressive mode. */
kusano 7d535a
#ifdef C_PROGRESSIVE_SUPPORTED
kusano 7d535a
      simple_progressive = TRUE;
kusano 7d535a
      /* We must postpone execution until num_components is known. */
kusano 7d535a
#else
kusano 7d535a
      fprintf(stderr, "%s: sorry, progressive output was not compiled\n",
kusano 7d535a
	      progname);
kusano 7d535a
      exit(EXIT_FAILURE);
kusano 7d535a
#endif
kusano 7d535a
kusano 7d535a
    } else if (keymatch(arg, "restart", 1)) {
kusano 7d535a
      /* Restart interval in MCU rows (or in MCUs with 'b'). */
kusano 7d535a
      long lval;
kusano 7d535a
      char ch = 'x';
kusano 7d535a
kusano 7d535a
      if (++argn >= argc)	/* advance to next argument */
kusano 7d535a
	usage();
kusano 7d535a
      if (sscanf(argv[argn], "%ld%c", &lval, &ch) < 1)
kusano 7d535a
	usage();
kusano 7d535a
      if (lval < 0 || lval > 65535L)
kusano 7d535a
	usage();
kusano 7d535a
      if (ch == 'b' || ch == 'B') {
kusano 7d535a
	cinfo->restart_interval = (unsigned int) lval;
kusano 7d535a
	cinfo->restart_in_rows = 0; /* else prior '-restart n' overrides me */
kusano 7d535a
      } else {
kusano 7d535a
	cinfo->restart_in_rows = (int) lval;
kusano 7d535a
	/* restart_interval will be computed during startup */
kusano 7d535a
      }
kusano 7d535a
kusano 7d535a
    } else if (keymatch(arg, "rotate", 2)) {
kusano 7d535a
      /* Rotate 90, 180, or 270 degrees (measured clockwise). */
kusano 7d535a
      if (++argn >= argc)	/* advance to next argument */
kusano 7d535a
	usage();
kusano 7d535a
      if (keymatch(argv[argn], "90", 2))
kusano 7d535a
	select_transform(JXFORM_ROT_90);
kusano 7d535a
      else if (keymatch(argv[argn], "180", 3))
kusano 7d535a
	select_transform(JXFORM_ROT_180);
kusano 7d535a
      else if (keymatch(argv[argn], "270", 3))
kusano 7d535a
	select_transform(JXFORM_ROT_270);
kusano 7d535a
      else
kusano 7d535a
	usage();
kusano 7d535a
kusano 7d535a
    } else if (keymatch(arg, "scale", 4)) {
kusano 7d535a
      /* Scale the output image by a fraction M/N. */
kusano 7d535a
      if (++argn >= argc)	/* advance to next argument */
kusano 7d535a
	usage();
kusano 7d535a
      scaleoption = argv[argn];
kusano 7d535a
      /* We must postpone processing until decompression startup. */
kusano 7d535a
kusano 7d535a
    } else if (keymatch(arg, "scans", 1)) {
kusano 7d535a
      /* Set scan script. */
kusano 7d535a
#ifdef C_MULTISCAN_FILES_SUPPORTED
kusano 7d535a
      if (++argn >= argc)	/* advance to next argument */
kusano 7d535a
	usage();
kusano 7d535a
      scansarg = argv[argn];
kusano 7d535a
      /* We must postpone reading the file in case -progressive appears. */
kusano 7d535a
#else
kusano 7d535a
      fprintf(stderr, "%s: sorry, multi-scan output was not compiled\n",
kusano 7d535a
	      progname);
kusano 7d535a
      exit(EXIT_FAILURE);
kusano 7d535a
#endif
kusano 7d535a
kusano 7d535a
    } else if (keymatch(arg, "transpose", 1)) {
kusano 7d535a
      /* Transpose (across UL-to-LR axis). */
kusano 7d535a
      select_transform(JXFORM_TRANSPOSE);
kusano 7d535a
kusano 7d535a
    } else if (keymatch(arg, "transverse", 6)) {
kusano 7d535a
      /* Transverse transpose (across UR-to-LL axis). */
kusano 7d535a
      select_transform(JXFORM_TRANSVERSE);
kusano 7d535a
kusano 7d535a
    } else if (keymatch(arg, "trim", 3)) {
kusano 7d535a
      /* Trim off any partial edge MCUs that the transform can't handle. */
kusano 7d535a
      transformoption.trim = TRUE;
kusano 7d535a
kusano 7d535a
    } else {
kusano 7d535a
      usage();			/* bogus switch */
kusano 7d535a
    }
kusano 7d535a
  }
kusano 7d535a
kusano 7d535a
  /* Post-switch-scanning cleanup */
kusano 7d535a
kusano 7d535a
  if (for_real) {
kusano 7d535a
kusano 7d535a
#ifdef C_PROGRESSIVE_SUPPORTED
kusano 7d535a
    if (simple_progressive)	/* process -progressive; -scans can override */
kusano 7d535a
      jpeg_simple_progression(cinfo);
kusano 7d535a
#endif
kusano 7d535a
kusano 7d535a
#ifdef C_MULTISCAN_FILES_SUPPORTED
kusano 7d535a
    if (scansarg != NULL)	/* process -scans if it was present */
kusano 7d535a
      if (! read_scan_script(cinfo, scansarg))
kusano 7d535a
	usage();
kusano 7d535a
#endif
kusano 7d535a
  }
kusano 7d535a
kusano 7d535a
  return argn;			/* return index of next arg (file name) */
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
kusano 7d535a
/*
kusano 7d535a
 * The main program.
kusano 7d535a
 */
kusano 7d535a
kusano 7d535a
int
kusano 7d535a
main (int argc, char **argv)
kusano 7d535a
{
kusano 7d535a
  struct jpeg_decompress_struct srcinfo;
kusano 7d535a
  struct jpeg_compress_struct dstinfo;
kusano 7d535a
  struct jpeg_error_mgr jsrcerr, jdsterr;
kusano 7d535a
#ifdef PROGRESS_REPORT
kusano 7d535a
  struct cdjpeg_progress_mgr progress;
kusano 7d535a
#endif
kusano 7d535a
  jvirt_barray_ptr * src_coef_arrays;
kusano 7d535a
  jvirt_barray_ptr * dst_coef_arrays;
kusano 7d535a
  int file_index;
kusano 7d535a
  /* We assume all-in-memory processing and can therefore use only a
kusano 7d535a
   * single file pointer for sequential input and output operation. 
kusano 7d535a
   */
kusano 7d535a
  FILE * fp;
kusano 7d535a
kusano 7d535a
  /* On Mac, fetch a command line. */
kusano 7d535a
#ifdef USE_CCOMMAND
kusano 7d535a
  argc = ccommand(&argv);
kusano 7d535a
#endif
kusano 7d535a
kusano 7d535a
  progname = argv[0];
kusano 7d535a
  if (progname == NULL || progname[0] == 0)
kusano 7d535a
    progname = "jpegtran";	/* in case C library doesn't provide it */
kusano 7d535a
kusano 7d535a
  /* Initialize the JPEG decompression object with default error handling. */
kusano 7d535a
  srcinfo.err = jpeg_std_error(&jsrcerr);
kusano 7d535a
  jpeg_create_decompress(&srcinfo);
kusano 7d535a
  /* Initialize the JPEG compression object with default error handling. */
kusano 7d535a
  dstinfo.err = jpeg_std_error(&jdsterr);
kusano 7d535a
  jpeg_create_compress(&dstinfo);
kusano 7d535a
kusano 7d535a
  /* Now safe to enable signal catcher.
kusano 7d535a
   * Note: we assume only the decompression object will have virtual arrays.
kusano 7d535a
   */
kusano 7d535a
#ifdef NEED_SIGNAL_CATCHER
kusano 7d535a
  enable_signal_catcher((j_common_ptr) &srcinfo);
kusano 7d535a
#endif
kusano 7d535a
kusano 7d535a
  /* Scan command line to find file names.
kusano 7d535a
   * It is convenient to use just one switch-parsing routine, but the switch
kusano 7d535a
   * values read here are mostly ignored; we will rescan the switches after
kusano 7d535a
   * opening the input file.  Also note that most of the switches affect the
kusano 7d535a
   * destination JPEG object, so we parse into that and then copy over what
kusano 7d535a
   * needs to affects the source too.
kusano 7d535a
   */
kusano 7d535a
kusano 7d535a
  file_index = parse_switches(&dstinfo, argc, argv, 0, FALSE);
kusano 7d535a
  jsrcerr.trace_level = jdsterr.trace_level;
kusano 7d535a
  srcinfo.mem->max_memory_to_use = dstinfo.mem->max_memory_to_use;
kusano 7d535a
kusano 7d535a
#ifdef TWO_FILE_COMMANDLINE
kusano 7d535a
  /* Must have either -outfile switch or explicit output file name */
kusano 7d535a
  if (outfilename == NULL) {
kusano 7d535a
    if (file_index != argc-2) {
kusano 7d535a
      fprintf(stderr, "%s: must name one input and one output file\n",
kusano 7d535a
	      progname);
kusano 7d535a
      usage();
kusano 7d535a
    }
kusano 7d535a
    outfilename = argv[file_index+1];
kusano 7d535a
  } else {
kusano 7d535a
    if (file_index != argc-1) {
kusano 7d535a
      fprintf(stderr, "%s: must name one input and one output file\n",
kusano 7d535a
	      progname);
kusano 7d535a
      usage();
kusano 7d535a
    }
kusano 7d535a
  }
kusano 7d535a
#else
kusano 7d535a
  /* Unix style: expect zero or one file name */
kusano 7d535a
  if (file_index < argc-1) {
kusano 7d535a
    fprintf(stderr, "%s: only one input file\n", progname);
kusano 7d535a
    usage();
kusano 7d535a
  }
kusano 7d535a
#endif /* TWO_FILE_COMMANDLINE */
kusano 7d535a
kusano 7d535a
  /* Open the input file. */
kusano 7d535a
  if (file_index < argc) {
kusano 7d535a
    if ((fp = fopen(argv[file_index], READ_BINARY)) == NULL) {
kusano 7d535a
      fprintf(stderr, "%s: can't open %s for reading\n", progname, argv[file_index]);
kusano 7d535a
      exit(EXIT_FAILURE);
kusano 7d535a
    }
kusano 7d535a
  } else {
kusano 7d535a
    /* default input file is stdin */
kusano 7d535a
    fp = read_stdin();
kusano 7d535a
  }
kusano 7d535a
kusano 7d535a
#ifdef PROGRESS_REPORT
kusano 7d535a
  start_progress_monitor((j_common_ptr) &dstinfo, &progress);
kusano 7d535a
#endif
kusano 7d535a
kusano 7d535a
  /* Specify data source for decompression */
kusano 7d535a
  jpeg_stdio_src(&srcinfo, fp);
kusano 7d535a
kusano 7d535a
  /* Enable saving of extra markers that we want to copy */
kusano 7d535a
  jcopy_markers_setup(&srcinfo, copyoption);
kusano 7d535a
kusano 7d535a
  /* Read file header */
kusano 7d535a
  (void) jpeg_read_header(&srcinfo, TRUE);
kusano 7d535a
kusano 7d535a
  /* Adjust default decompression parameters */
kusano 7d535a
  if (scaleoption != NULL)
kusano 7d535a
    if (sscanf(scaleoption, "%u/%u",
kusano 7d535a
	&srcinfo.scale_num, &srcinfo.scale_denom) < 1)
kusano 7d535a
      usage();
kusano 7d535a
kusano 7d535a
  /* Any space needed by a transform option must be requested before
kusano 7d535a
   * jpeg_read_coefficients so that memory allocation will be done right.
kusano 7d535a
   */
kusano 7d535a
#if TRANSFORMS_SUPPORTED
kusano 7d535a
  /* Fail right away if -perfect is given and transformation is not perfect.
kusano 7d535a
   */
kusano 7d535a
  if (!jtransform_request_workspace(&srcinfo, &transformoption)) {
kusano 7d535a
    fprintf(stderr, "%s: transformation is not perfect\n", progname);
kusano 7d535a
    exit(EXIT_FAILURE);
kusano 7d535a
  }
kusano 7d535a
#endif
kusano 7d535a
kusano 7d535a
  /* Read source file as DCT coefficients */
kusano 7d535a
  src_coef_arrays = jpeg_read_coefficients(&srcinfo);
kusano 7d535a
kusano 7d535a
  /* Initialize destination compression parameters from source values */
kusano 7d535a
  jpeg_copy_critical_parameters(&srcinfo, &dstinfo);
kusano 7d535a
kusano 7d535a
  /* Adjust destination parameters if required by transform options;
kusano 7d535a
   * also find out which set of coefficient arrays will hold the output.
kusano 7d535a
   */
kusano 7d535a
#if TRANSFORMS_SUPPORTED
kusano 7d535a
  dst_coef_arrays = jtransform_adjust_parameters(&srcinfo, &dstinfo,
kusano 7d535a
						 src_coef_arrays,
kusano 7d535a
						 &transformoption);
kusano 7d535a
#else
kusano 7d535a
  dst_coef_arrays = src_coef_arrays;
kusano 7d535a
#endif
kusano 7d535a
kusano 7d535a
  /* Close input file, if we opened it.
kusano 7d535a
   * Note: we assume that jpeg_read_coefficients consumed all input
kusano 7d535a
   * until JPEG_REACHED_EOI, and that jpeg_finish_decompress will
kusano 7d535a
   * only consume more while (! cinfo->inputctl->eoi_reached).
kusano 7d535a
   * We cannot call jpeg_finish_decompress here since we still need the
kusano 7d535a
   * virtual arrays allocated from the source object for processing.
kusano 7d535a
   */
kusano 7d535a
  if (fp != stdin)
kusano 7d535a
    fclose(fp);
kusano 7d535a
kusano 7d535a
  /* Open the output file. */
kusano 7d535a
  if (outfilename != NULL) {
kusano 7d535a
    if ((fp = fopen(outfilename, WRITE_BINARY)) == NULL) {
kusano 7d535a
      fprintf(stderr, "%s: can't open %s for writing\n", progname, outfilename);
kusano 7d535a
      exit(EXIT_FAILURE);
kusano 7d535a
    }
kusano 7d535a
  } else {
kusano 7d535a
    /* default output file is stdout */
kusano 7d535a
    fp = write_stdout();
kusano 7d535a
  }
kusano 7d535a
kusano 7d535a
  /* Adjust default compression parameters by re-parsing the options */
kusano 7d535a
  file_index = parse_switches(&dstinfo, argc, argv, 0, TRUE);
kusano 7d535a
kusano 7d535a
  /* Specify data destination for compression */
kusano 7d535a
  jpeg_stdio_dest(&dstinfo, fp);
kusano 7d535a
kusano 7d535a
  /* Start compressor (note no image data is actually written here) */
kusano 7d535a
  jpeg_write_coefficients(&dstinfo, dst_coef_arrays);
kusano 7d535a
kusano 7d535a
  /* Copy to the output file any extra markers that we want to preserve */
kusano 7d535a
  jcopy_markers_execute(&srcinfo, &dstinfo, copyoption);
kusano 7d535a
kusano 7d535a
  /* Execute image transformation, if any */
kusano 7d535a
#if TRANSFORMS_SUPPORTED
kusano 7d535a
  jtransform_execute_transformation(&srcinfo, &dstinfo,
kusano 7d535a
				    src_coef_arrays,
kusano 7d535a
				    &transformoption);
kusano 7d535a
#endif
kusano 7d535a
kusano 7d535a
  /* Finish compression and release memory */
kusano 7d535a
  jpeg_finish_compress(&dstinfo);
kusano 7d535a
  jpeg_destroy_compress(&dstinfo);
kusano 7d535a
  (void) jpeg_finish_decompress(&srcinfo);
kusano 7d535a
  jpeg_destroy_decompress(&srcinfo);
kusano 7d535a
kusano 7d535a
  /* Close output file, if we opened it */
kusano 7d535a
  if (fp != stdout)
kusano 7d535a
    fclose(fp);
kusano 7d535a
kusano 7d535a
#ifdef PROGRESS_REPORT
kusano 7d535a
  end_progress_monitor((j_common_ptr) &dstinfo);
kusano 7d535a
#endif
kusano 7d535a
kusano 7d535a
  /* All done. */
kusano 7d535a
  exit(jsrcerr.num_warnings + jdsterr.num_warnings ?EXIT_WARNING:EXIT_SUCCESS);
kusano 7d535a
  return 0;			/* suppress no-return-value warnings */
kusano 7d535a
}