kusano 7d535a
/*
kusano 7d535a
 * cjpeg.c
kusano 7d535a
 *
kusano 7d535a
 * Copyright (C) 1991-1998, Thomas G. Lane.
kusano 7d535a
 * Modified 2003-2013 by 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 the JPEG compressor.
kusano 7d535a
 * It should work on any system with Unix- or MS-DOS-style command lines.
kusano 7d535a
 *
kusano 7d535a
 * Two different command line styles are permitted, depending on the
kusano 7d535a
 * compile-time switch TWO_FILE_COMMANDLINE:
kusano 7d535a
 *	cjpeg [options]  inputfile outputfile
kusano 7d535a
 *	cjpeg [options]  [inputfile]
kusano 7d535a
 * In the second style, output is always to standard output, which you'd
kusano 7d535a
 * normally redirect to a file or pipe to some other program.  Input is
kusano 7d535a
 * either from a named file or from standard input (typically redirected).
kusano 7d535a
 * The second style is convenient on Unix but is unhelpful on systems that
kusano 7d535a
 * don't support pipes.  Also, you MUST use the first style if your system
kusano 7d535a
 * doesn't do binary I/O to stdin/stdout.
kusano 7d535a
 * To simplify script writing, the "-outfile" switch is provided.  The syntax
kusano 7d535a
 *	cjpeg [options]  -outfile outputfile  inputfile
kusano 7d535a
 * works regardless of which command line style is used.
kusano 7d535a
 */
kusano 7d535a
kusano 7d535a
#include "cdjpeg.h"		/* Common decls for cjpeg/djpeg applications */
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
/* Create the add-on message string table. */
kusano 7d535a
kusano 7d535a
#define JMESSAGE(code,string)	string ,
kusano 7d535a
kusano 7d535a
static const char * const cdjpeg_message_table[] = {
kusano 7d535a
#include "cderror.h"
kusano 7d535a
  NULL
kusano 7d535a
};
kusano 7d535a
kusano 7d535a
kusano 7d535a
/*
kusano 7d535a
 * This routine determines what format the input file is,
kusano 7d535a
 * and selects the appropriate input-reading module.
kusano 7d535a
 *
kusano 7d535a
 * To determine which family of input formats the file belongs to,
kusano 7d535a
 * we may look only at the first byte of the file, since C does not
kusano 7d535a
 * guarantee that more than one character can be pushed back with ungetc.
kusano 7d535a
 * Looking at additional bytes would require one of these approaches:
kusano 7d535a
 *     1) assume we can fseek() the input file (fails for piped input);
kusano 7d535a
 *     2) assume we can push back more than one character (works in
kusano 7d535a
 *        some C implementations, but unportable);
kusano 7d535a
 *     3) provide our own buffering (breaks input readers that want to use
kusano 7d535a
 *        stdio directly, such as the RLE library);
kusano 7d535a
 * or  4) don't put back the data, and modify the input_init methods to assume
kusano 7d535a
 *        they start reading after the start of file (also breaks RLE library).
kusano 7d535a
 * #1 is attractive for MS-DOS but is untenable on Unix.
kusano 7d535a
 *
kusano 7d535a
 * The most portable solution for file types that can't be identified by their
kusano 7d535a
 * first byte is to make the user tell us what they are.  This is also the
kusano 7d535a
 * only approach for "raw" file types that contain only arbitrary values.
kusano 7d535a
 * We presently apply this method for Targa files.  Most of the time Targa
kusano 7d535a
 * files start with 0x00, so we recognize that case.  Potentially, however,
kusano 7d535a
 * a Targa file could start with any byte value (byte 0 is the length of the
kusano 7d535a
 * seldom-used ID field), so we provide a switch to force Targa input mode.
kusano 7d535a
 */
kusano 7d535a
kusano 7d535a
static boolean is_targa;	/* records user -targa switch */
kusano 7d535a
kusano 7d535a
kusano 7d535a
LOCAL(cjpeg_source_ptr)
kusano 7d535a
select_file_type (j_compress_ptr cinfo, FILE * infile)
kusano 7d535a
{
kusano 7d535a
  int c;
kusano 7d535a
kusano 7d535a
  if (is_targa) {
kusano 7d535a
#ifdef TARGA_SUPPORTED
kusano 7d535a
    return jinit_read_targa(cinfo);
kusano 7d535a
#else
kusano 7d535a
    ERREXIT(cinfo, JERR_TGA_NOTCOMP);
kusano 7d535a
#endif
kusano 7d535a
  }
kusano 7d535a
kusano 7d535a
  if ((c = getc(infile)) == EOF)
kusano 7d535a
    ERREXIT(cinfo, JERR_INPUT_EMPTY);
kusano 7d535a
  if (ungetc(c, infile) == EOF)
kusano 7d535a
    ERREXIT(cinfo, JERR_UNGETC_FAILED);
kusano 7d535a
kusano 7d535a
  switch (c) {
kusano 7d535a
#ifdef BMP_SUPPORTED
kusano 7d535a
  case 'B':
kusano 7d535a
    return jinit_read_bmp(cinfo);
kusano 7d535a
#endif
kusano 7d535a
#ifdef GIF_SUPPORTED
kusano 7d535a
  case 'G':
kusano 7d535a
    return jinit_read_gif(cinfo);
kusano 7d535a
#endif
kusano 7d535a
#ifdef PPM_SUPPORTED
kusano 7d535a
  case 'P':
kusano 7d535a
    return jinit_read_ppm(cinfo);
kusano 7d535a
#endif
kusano 7d535a
#ifdef RLE_SUPPORTED
kusano 7d535a
  case 'R':
kusano 7d535a
    return jinit_read_rle(cinfo);
kusano 7d535a
#endif
kusano 7d535a
#ifdef TARGA_SUPPORTED
kusano 7d535a
  case 0x00:
kusano 7d535a
    return jinit_read_targa(cinfo);
kusano 7d535a
#endif
kusano 7d535a
  default:
kusano 7d535a
    ERREXIT(cinfo, JERR_UNKNOWN_FORMAT);
kusano 7d535a
    break;
kusano 7d535a
  }
kusano 7d535a
kusano 7d535a
  return NULL;			/* suppress compiler warnings */
kusano 7d535a
}
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
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, "  -quality N[,...]   Compression quality (0..100; 5-95 is useful range)\n");
kusano 7d535a
  fprintf(stderr, "  -grayscale     Create monochrome JPEG file\n");
kusano 7d535a
  fprintf(stderr, "  -rgb           Create RGB JPEG file\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
#ifdef DCT_SCALING_SUPPORTED
kusano 7d535a
  fprintf(stderr, "  -scale M/N     Scale image by fraction M/N, eg, 1/2\n");
kusano 7d535a
#endif
kusano 7d535a
#ifdef TARGA_SUPPORTED
kusano 7d535a
  fprintf(stderr, "  -targa         Input file is Targa format (usually not needed)\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
#ifdef DCT_SCALING_SUPPORTED
kusano 7d535a
  fprintf(stderr, "  -block N       DCT block size (1..16; default is 8)\n");
kusano 7d535a
#endif
kusano 7d535a
#if JPEG_LIB_VERSION_MAJOR >= 9
kusano 7d535a
  fprintf(stderr, "  -rgb1          Create RGB JPEG file with reversible color transform\n");
kusano 7d535a
#endif
kusano 7d535a
#ifdef DCT_ISLOW_SUPPORTED
kusano 7d535a
  fprintf(stderr, "  -dct int       Use integer DCT method%s\n",
kusano 7d535a
	  (JDCT_DEFAULT == JDCT_ISLOW ? " (default)" : ""));
kusano 7d535a
#endif
kusano 7d535a
#ifdef DCT_IFAST_SUPPORTED
kusano 7d535a
  fprintf(stderr, "  -dct fast      Use fast integer DCT (less accurate)%s\n",
kusano 7d535a
	  (JDCT_DEFAULT == JDCT_IFAST ? " (default)" : ""));
kusano 7d535a
#endif
kusano 7d535a
#ifdef DCT_FLOAT_SUPPORTED
kusano 7d535a
  fprintf(stderr, "  -dct float     Use floating-point DCT method%s\n",
kusano 7d535a
	  (JDCT_DEFAULT == JDCT_FLOAT ? " (default)" : ""));
kusano 7d535a
#endif
kusano 7d535a
  fprintf(stderr, "  -nosmooth      Don't use high-quality downsampling\n");
kusano 7d535a
  fprintf(stderr, "  -restart N     Set restart interval in rows, or in blocks with B\n");
kusano 7d535a
#ifdef INPUT_SMOOTHING_SUPPORTED
kusano 7d535a
  fprintf(stderr, "  -smooth N      Smooth dithered input (N=1..100 is strength)\n");
kusano 7d535a
#endif
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
  fprintf(stderr, "  -baseline      Force baseline quantization tables\n");
kusano 7d535a
  fprintf(stderr, "  -qtables file  Use quantization tables given in file\n");
kusano 7d535a
  fprintf(stderr, "  -qslots N[,...]    Set component quantization tables\n");
kusano 7d535a
  fprintf(stderr, "  -sample HxV[,...]  Set component sampling factors\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(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 force_baseline;
kusano 7d535a
  boolean simple_progressive;
kusano 7d535a
  char * qualityarg = NULL;	/* saves -quality parm if any */
kusano 7d535a
  char * qtablefile = NULL;	/* saves -qtables filename if any */
kusano 7d535a
  char * qslotsarg = NULL;	/* saves -qslots parm if any */
kusano 7d535a
  char * samplearg = NULL;	/* saves -sample parm if any */
kusano 7d535a
  char * scansarg = NULL;	/* saves -scans parm if any */
kusano 7d535a
kusano 7d535a
  /* Set up default JPEG parameters. */
kusano 7d535a
kusano 7d535a
  force_baseline = FALSE;	/* by default, allow 16-bit quantizers */
kusano 7d535a
  simple_progressive = FALSE;
kusano 7d535a
  is_targa = FALSE;
kusano 7d535a
  outfilename = NULL;
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, "baseline", 2)) {
kusano 7d535a
      /* Force baseline-compatible output (8-bit quantizer values). */
kusano 7d535a
      force_baseline = TRUE;
kusano 7d535a
kusano 7d535a
    } else if (keymatch(arg, "block", 2)) {
kusano 7d535a
      /* Set DCT block size. */
kusano 7d535a
#if defined DCT_SCALING_SUPPORTED && JPEG_LIB_VERSION_MAJOR >= 8 && \
kusano 7d535a
      (JPEG_LIB_VERSION_MAJOR > 8 || JPEG_LIB_VERSION_MINOR >= 3)
kusano 7d535a
      int val;
kusano 7d535a
kusano 7d535a
      if (++argn >= argc)	/* advance to next argument */
kusano 7d535a
	usage();
kusano 7d535a
      if (sscanf(argv[argn], "%d", &val) != 1)
kusano 7d535a
	usage();
kusano 7d535a
      if (val < 1 || val > 16)
kusano 7d535a
	usage();
kusano 7d535a
      cinfo->block_size = val;
kusano 7d535a
#else
kusano 7d535a
      fprintf(stderr, "%s: sorry, block size setting not supported\n",
kusano 7d535a
	      progname);
kusano 7d535a
      exit(EXIT_FAILURE);
kusano 7d535a
#endif
kusano 7d535a
kusano 7d535a
    } else if (keymatch(arg, "dct", 2)) {
kusano 7d535a
      /* Select DCT algorithm. */
kusano 7d535a
      if (++argn >= argc)	/* advance to next argument */
kusano 7d535a
	usage();
kusano 7d535a
      if (keymatch(argv[argn], "int", 1)) {
kusano 7d535a
	cinfo->dct_method = JDCT_ISLOW;
kusano 7d535a
      } else if (keymatch(argv[argn], "fast", 2)) {
kusano 7d535a
	cinfo->dct_method = JDCT_IFAST;
kusano 7d535a
      } else if (keymatch(argv[argn], "float", 2)) {
kusano 7d535a
	cinfo->dct_method = JDCT_FLOAT;
kusano 7d535a
      } else
kusano 7d535a
	usage();
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 CJPEG, 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, "grayscale", 2) || keymatch(arg, "greyscale",2)) {
kusano 7d535a
      /* Force a monochrome JPEG file to be generated. */
kusano 7d535a
      jpeg_set_colorspace(cinfo, JCS_GRAYSCALE);
kusano 7d535a
kusano 7d535a
    } else if (keymatch(arg, "rgb", 3) || keymatch(arg, "rgb1", 4)) {
kusano 7d535a
      /* Force an RGB JPEG file to be generated. */
kusano 7d535a
#if JPEG_LIB_VERSION_MAJOR >= 9
kusano 7d535a
      /* Note: Entropy table assignment in jpeg_set_colorspace depends
kusano 7d535a
       * on color_transform.
kusano 7d535a
       */
kusano 7d535a
      cinfo->color_transform = arg[3] ? JCT_SUBTRACT_GREEN : JCT_NONE;
kusano 7d535a
#endif
kusano 7d535a
      jpeg_set_colorspace(cinfo, JCS_RGB);
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, "nosmooth", 3)) {
kusano 7d535a
      /* Suppress fancy downsampling. */
kusano 7d535a
      cinfo->do_fancy_downsampling = FALSE;
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, "progressive", 1)) {
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, "quality", 1)) {
kusano 7d535a
      /* Quality ratings (quantization table scaling factors). */
kusano 7d535a
      if (++argn >= argc)	/* advance to next argument */
kusano 7d535a
	usage();
kusano 7d535a
      qualityarg = argv[argn];
kusano 7d535a
kusano 7d535a
    } else if (keymatch(arg, "qslots", 2)) {
kusano 7d535a
      /* Quantization table slot numbers. */
kusano 7d535a
      if (++argn >= argc)	/* advance to next argument */
kusano 7d535a
	usage();
kusano 7d535a
      qslotsarg = argv[argn];
kusano 7d535a
      /* Must delay setting qslots until after we have processed any
kusano 7d535a
       * colorspace-determining switches, since jpeg_set_colorspace sets
kusano 7d535a
       * default quant table numbers.
kusano 7d535a
       */
kusano 7d535a
kusano 7d535a
    } else if (keymatch(arg, "qtables", 2)) {
kusano 7d535a
      /* Quantization tables fetched from file. */
kusano 7d535a
      if (++argn >= argc)	/* advance to next argument */
kusano 7d535a
	usage();
kusano 7d535a
      qtablefile = argv[argn];
kusano 7d535a
      /* We postpone actually reading the file in case -quality comes later. */
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, "sample", 2)) {
kusano 7d535a
      /* Set sampling factors. */
kusano 7d535a
      if (++argn >= argc)	/* advance to next argument */
kusano 7d535a
	usage();
kusano 7d535a
      samplearg = argv[argn];
kusano 7d535a
      /* Must delay setting sample factors until after we have processed any
kusano 7d535a
       * colorspace-determining switches, since jpeg_set_colorspace sets
kusano 7d535a
       * default sampling factors.
kusano 7d535a
       */
kusano 7d535a
kusano 7d535a
    } else if (keymatch(arg, "scale", 4)) {
kusano 7d535a
      /* Scale the image by a fraction M/N. */
kusano 7d535a
      if (++argn >= argc)	/* advance to next argument */
kusano 7d535a
	usage();
kusano 7d535a
      if (sscanf(argv[argn], "%u/%u",
kusano 7d535a
		 &cinfo->scale_num, &cinfo->scale_denom) != 2)
kusano 7d535a
	usage();
kusano 7d535a
kusano 7d535a
    } else if (keymatch(arg, "scans", 4)) {
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, "smooth", 2)) {
kusano 7d535a
      /* Set input smoothing factor. */
kusano 7d535a
      int val;
kusano 7d535a
kusano 7d535a
      if (++argn >= argc)	/* advance to next argument */
kusano 7d535a
	usage();
kusano 7d535a
      if (sscanf(argv[argn], "%d", &val) != 1)
kusano 7d535a
	usage();
kusano 7d535a
      if (val < 0 || val > 100)
kusano 7d535a
	usage();
kusano 7d535a
      cinfo->smoothing_factor = val;
kusano 7d535a
kusano 7d535a
    } else if (keymatch(arg, "targa", 1)) {
kusano 7d535a
      /* Input file is Targa format. */
kusano 7d535a
      is_targa = 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
    /* Set quantization tables for selected quality. */
kusano 7d535a
    /* Some or all may be overridden if -qtables is present. */
kusano 7d535a
    if (qualityarg != NULL)	/* process -quality if it was present */
kusano 7d535a
      if (! set_quality_ratings(cinfo, qualityarg, force_baseline))
kusano 7d535a
	usage();
kusano 7d535a
kusano 7d535a
    if (qtablefile != NULL)	/* process -qtables if it was present */
kusano 7d535a
      if (! read_quant_tables(cinfo, qtablefile, force_baseline))
kusano 7d535a
	usage();
kusano 7d535a
kusano 7d535a
    if (qslotsarg != NULL)	/* process -qslots if it was present */
kusano 7d535a
      if (! set_quant_slots(cinfo, qslotsarg))
kusano 7d535a
	usage();
kusano 7d535a
kusano 7d535a
    if (samplearg != NULL)	/* process -sample if it was present */
kusano 7d535a
      if (! set_sample_factors(cinfo, samplearg))
kusano 7d535a
	usage();
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_compress_struct cinfo;
kusano 7d535a
  struct jpeg_error_mgr jerr;
kusano 7d535a
#ifdef PROGRESS_REPORT
kusano 7d535a
  struct cdjpeg_progress_mgr progress;
kusano 7d535a
#endif
kusano 7d535a
  int file_index;
kusano 7d535a
  cjpeg_source_ptr src_mgr;
kusano 7d535a
  FILE * input_file;
kusano 7d535a
  FILE * output_file;
kusano 7d535a
  JDIMENSION num_scanlines;
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 = "cjpeg";		/* in case C library doesn't provide it */
kusano 7d535a
kusano 7d535a
  /* Initialize the JPEG compression object with default error handling. */
kusano 7d535a
  cinfo.err = jpeg_std_error(&jerr);
kusano 7d535a
  jpeg_create_compress(&cinfo);
kusano 7d535a
  /* Add some application-specific error messages (from cderror.h) */
kusano 7d535a
  jerr.addon_message_table = cdjpeg_message_table;
kusano 7d535a
  jerr.first_addon_message = JMSG_FIRSTADDONCODE;
kusano 7d535a
  jerr.last_addon_message = JMSG_LASTADDONCODE;
kusano 7d535a
kusano 7d535a
  /* Now safe to enable signal catcher. */
kusano 7d535a
#ifdef NEED_SIGNAL_CATCHER
kusano 7d535a
  enable_signal_catcher((j_common_ptr) &cinfo);
kusano 7d535a
#endif
kusano 7d535a
kusano 7d535a
  /* Initialize JPEG parameters.
kusano 7d535a
   * Much of this may be overridden later.
kusano 7d535a
   * In particular, we don't yet know the input file's color space,
kusano 7d535a
   * but we need to provide some value for jpeg_set_defaults() to work.
kusano 7d535a
   */
kusano 7d535a
kusano 7d535a
  cinfo.in_color_space = JCS_RGB; /* arbitrary guess */
kusano 7d535a
  jpeg_set_defaults(&cinfo);
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 ignored; we will rescan the switches after opening
kusano 7d535a
   * the input file.
kusano 7d535a
   */
kusano 7d535a
kusano 7d535a
  file_index = parse_switches(&cinfo, argc, argv, 0, FALSE);
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 ((input_file = fopen(argv[file_index], READ_BINARY)) == NULL) {
kusano 7d535a
      fprintf(stderr, "%s: can't open %s\n", progname, argv[file_index]);
kusano 7d535a
      exit(EXIT_FAILURE);
kusano 7d535a
    }
kusano 7d535a
  } else {
kusano 7d535a
    /* default input file is stdin */
kusano 7d535a
    input_file = read_stdin();
kusano 7d535a
  }
kusano 7d535a
kusano 7d535a
  /* Open the output file. */
kusano 7d535a
  if (outfilename != NULL) {
kusano 7d535a
    if ((output_file = fopen(outfilename, WRITE_BINARY)) == NULL) {
kusano 7d535a
      fprintf(stderr, "%s: can't open %s\n", progname, outfilename);
kusano 7d535a
      exit(EXIT_FAILURE);
kusano 7d535a
    }
kusano 7d535a
  } else {
kusano 7d535a
    /* default output file is stdout */
kusano 7d535a
    output_file = write_stdout();
kusano 7d535a
  }
kusano 7d535a
kusano 7d535a
#ifdef PROGRESS_REPORT
kusano 7d535a
  start_progress_monitor((j_common_ptr) &cinfo, &progress);
kusano 7d535a
#endif
kusano 7d535a
kusano 7d535a
  /* Figure out the input file format, and set up to read it. */
kusano 7d535a
  src_mgr = select_file_type(&cinfo, input_file);
kusano 7d535a
  src_mgr->input_file = input_file;
kusano 7d535a
kusano 7d535a
  /* Read the input file header to obtain file size & colorspace. */
kusano 7d535a
  (*src_mgr->start_input) (&cinfo, src_mgr);
kusano 7d535a
kusano 7d535a
  /* Now that we know input colorspace, fix colorspace-dependent defaults */
kusano 7d535a
  jpeg_default_colorspace(&cinfo);
kusano 7d535a
kusano 7d535a
  /* Adjust default compression parameters by re-parsing the options */
kusano 7d535a
  file_index = parse_switches(&cinfo, argc, argv, 0, TRUE);
kusano 7d535a
kusano 7d535a
  /* Specify data destination for compression */
kusano 7d535a
  jpeg_stdio_dest(&cinfo, output_file);
kusano 7d535a
kusano 7d535a
  /* Start compressor */
kusano 7d535a
  jpeg_start_compress(&cinfo, TRUE);
kusano 7d535a
kusano 7d535a
  /* Process data */
kusano 7d535a
  while (cinfo.next_scanline < cinfo.image_height) {
kusano 7d535a
    num_scanlines = (*src_mgr->get_pixel_rows) (&cinfo, src_mgr);
kusano 7d535a
    (void) jpeg_write_scanlines(&cinfo, src_mgr->buffer, num_scanlines);
kusano 7d535a
  }
kusano 7d535a
kusano 7d535a
  /* Finish compression and release memory */
kusano 7d535a
  (*src_mgr->finish_input) (&cinfo, src_mgr);
kusano 7d535a
  jpeg_finish_compress(&cinfo);
kusano 7d535a
  jpeg_destroy_compress(&cinfo);
kusano 7d535a
kusano 7d535a
  /* Close files, if we opened them */
kusano 7d535a
  if (input_file != stdin)
kusano 7d535a
    fclose(input_file);
kusano 7d535a
  if (output_file != stdout)
kusano 7d535a
    fclose(output_file);
kusano 7d535a
kusano 7d535a
#ifdef PROGRESS_REPORT
kusano 7d535a
  end_progress_monitor((j_common_ptr) &cinfo);
kusano 7d535a
#endif
kusano 7d535a
kusano 7d535a
  /* All done. */
kusano 7d535a
  exit(jerr.num_warnings ? EXIT_WARNING : EXIT_SUCCESS);
kusano 7d535a
  return 0;			/* suppress no-return-value warnings */
kusano 7d535a
}