home *** CD-ROM | disk | FTP | other *** search
/ Encyclopedia of Graphics File Formats Companion / GFF_CD.ISO / software / unix / jpeg / jpegv4a.tar / jdmain.c < prev    next >
C/C++ Source or Header  |  1993-02-16  |  15KB  |  515 lines

  1. /*
  2.  * jdmain.c
  3.  *
  4.  * Copyright (C) 1991, 1992, 1993, Thomas G. Lane.
  5.  * This file is part of the Independent JPEG Group's software.
  6.  * For conditions of distribution and use, see the accompanying README file.
  7.  *
  8.  * This file contains a command-line user interface for the JPEG decompressor.
  9.  * It should work on any system with Unix- or MS-DOS-style command lines.
  10.  *
  11.  * Two different command line styles are permitted, depending on the
  12.  * compile-time switch TWO_FILE_COMMANDLINE:
  13.  *    djpeg [options]  inputfile outputfile
  14.  *    djpeg [options]  [inputfile]
  15.  * In the second style, output is always to standard output, which you'd
  16.  * normally redirect to a file or pipe to some other program.  Input is
  17.  * either from a named file or from standard input (typically redirected).
  18.  * The second style is convenient on Unix but is unhelpful on systems that
  19.  * don't support pipes.  Also, you MUST use the first style if your system
  20.  * doesn't do binary I/O to stdin/stdout.
  21.  * To simplify script writing, the "-outfile" switch is provided.  The syntax
  22.  *    djpeg [options]  -outfile outputfile  inputfile
  23.  * works regardless of which command line style is used.
  24.  */
  25.  
  26. #include "jinclude.h"
  27. #ifdef INCLUDES_ARE_ANSI
  28. #include <stdlib.h>        /* to declare exit() */
  29. #endif
  30. #include <ctype.h>        /* to declare isupper(), tolower() */
  31. #ifdef NEED_SIGNAL_CATCHER
  32. #include <signal.h>        /* to declare signal() */
  33. #endif
  34. #ifdef USE_SETMODE
  35. #include <fcntl.h>        /* to declare setmode() */
  36. #endif
  37.  
  38. #ifdef THINK_C
  39. #include <console.h>        /* command-line reader for Macintosh */
  40. #endif
  41.  
  42. #ifdef DONT_USE_B_MODE        /* define mode parameters for fopen() */
  43. #define READ_BINARY    "r"
  44. #define WRITE_BINARY    "w"
  45. #else
  46. #define READ_BINARY    "rb"
  47. #define WRITE_BINARY    "wb"
  48. #endif
  49.  
  50. #ifndef EXIT_FAILURE        /* define exit() codes if not provided */
  51. #define EXIT_FAILURE  1
  52. #endif
  53. #ifndef EXIT_SUCCESS
  54. #ifdef VMS
  55. #define EXIT_SUCCESS  1        /* VMS is very nonstandard */
  56. #else
  57. #define EXIT_SUCCESS  0
  58. #endif
  59. #endif
  60.  
  61.  
  62. #include "jversion.h"        /* for version message */
  63.  
  64.  
  65. /*
  66.  * This list defines the known output image formats
  67.  * (not all of which need be supported by a given version).
  68.  * You can change the default output format by defining DEFAULT_FMT;
  69.  * indeed, you had better do so if you undefine PPM_SUPPORTED.
  70.  */
  71.  
  72. typedef enum {
  73.     FMT_GIF,        /* GIF format */
  74.     FMT_PPM,        /* PPM/PGM (PBMPLUS formats) */
  75.     FMT_RLE,        /* RLE format */
  76.     FMT_TARGA,        /* Targa format */
  77.     FMT_TIFF        /* TIFF format */
  78. } IMAGE_FORMATS;
  79.  
  80. #ifndef DEFAULT_FMT        /* so can override from CFLAGS in Makefile */
  81. #define DEFAULT_FMT    FMT_PPM
  82. #endif
  83.  
  84. static IMAGE_FORMATS requested_fmt;
  85.  
  86.  
  87. /*
  88.  * This routine gets control after the input file header has been read.
  89.  * It must determine what output file format is to be written,
  90.  * and make any other decompression parameter changes that are desirable.
  91.  */
  92.  
  93. METHODDEF void
  94. d_ui_method_selection (decompress_info_ptr cinfo)
  95. {
  96.   /* if grayscale or CMYK input, force similar output; */
  97.   /* else leave the output colorspace as set by options. */
  98.   if (cinfo->jpeg_color_space == CS_GRAYSCALE)
  99.     cinfo->out_color_space = CS_GRAYSCALE;
  100.   else if (cinfo->jpeg_color_space == CS_CMYK)
  101.     cinfo->out_color_space = CS_CMYK;
  102.  
  103.   /* select output file format */
  104.   /* Note: jselwxxx routine may make additional parameter changes,
  105.    * such as forcing color quantization if it's a colormapped format.
  106.    */
  107.   switch (requested_fmt) {
  108. #ifdef GIF_SUPPORTED
  109.   case FMT_GIF:
  110.     jselwgif(cinfo);
  111.     break;
  112. #endif
  113. #ifdef PPM_SUPPORTED
  114.   case FMT_PPM:
  115.     jselwppm(cinfo);
  116.     break;
  117. #endif
  118. #ifdef RLE_SUPPORTED
  119.   case FMT_RLE:
  120.     jselwrle(cinfo);
  121.     break;
  122. #endif
  123. #ifdef TARGA_SUPPORTED
  124.   case FMT_TARGA:
  125.     jselwtarga(cinfo);
  126.     break;
  127. #endif
  128.   default:
  129.     ERREXIT(cinfo->emethods, "Unsupported output file format");
  130.     break;
  131.   }
  132. }
  133.  
  134.  
  135. /*
  136.  * Signal catcher to ensure that temporary files are removed before aborting.
  137.  * NB: for Amiga Manx C this is actually a global routine named _abort();
  138.  * see -Dsignal_catcher=_abort in CFLAGS.  Talk about bogus...
  139.  */
  140.  
  141. #ifdef NEED_SIGNAL_CATCHER
  142.  
  143. static external_methods_ptr emethods; /* for access to free_all */
  144.  
  145. GLOBAL void
  146. signal_catcher (int signum)
  147. {
  148.   if (emethods != NULL) {
  149.     emethods->trace_level = 0;    /* turn off trace output */
  150.     (*emethods->free_all) ();    /* clean up memory allocation & temp files */
  151.   }
  152.   exit(EXIT_FAILURE);
  153. }
  154.  
  155. #endif
  156.  
  157.  
  158. /*
  159.  * Optional routine to display a percent-done figure on stderr.
  160.  * See jddeflts.c for explanation of the information used.
  161.  */
  162.  
  163. #ifdef PROGRESS_REPORT
  164.  
  165. METHODDEF void
  166. progress_monitor (decompress_info_ptr cinfo, long loopcounter, long looplimit)
  167. {
  168.   if (cinfo->total_passes > 1) {
  169.     fprintf(stderr, "\rPass %d/%d: %3d%% ",
  170.         cinfo->completed_passes+1, cinfo->total_passes,
  171.         (int) (loopcounter*100L/looplimit));
  172.   } else {
  173.     fprintf(stderr, "\r %3d%% ",
  174.         (int) (loopcounter*100L/looplimit));
  175.   }
  176.   fflush(stderr);
  177. }
  178.  
  179. #endif
  180.  
  181.  
  182. /*
  183.  * Argument-parsing code.
  184.  * The switch parser is designed to be useful with DOS-style command line
  185.  * syntax, ie, intermixed switches and file names, where only the switches
  186.  * to the left of a given file name affect processing of that file.
  187.  * The main program in this file doesn't actually use this capability...
  188.  */
  189.  
  190.  
  191. static char * progname;        /* program name for error messages */
  192. static char * outfilename;    /* for -outfile switch */
  193.  
  194.  
  195. LOCAL void
  196. usage (void)
  197. /* complain about bad command line */
  198. {
  199.   fprintf(stderr, "usage: %s [switches] ", progname);
  200. #ifdef TWO_FILE_COMMANDLINE
  201.   fprintf(stderr, "inputfile outputfile\n");
  202. #else
  203.   fprintf(stderr, "[inputfile]\n");
  204. #endif
  205.  
  206.   fprintf(stderr, "Switches (names may be abbreviated):\n");
  207.   fprintf(stderr, "  -colors N      Reduce image to no more than N colors\n");
  208. #ifdef GIF_SUPPORTED
  209.   fprintf(stderr, "  -gif           Select GIF output format\n");
  210. #endif
  211. #ifdef PPM_SUPPORTED
  212.   fprintf(stderr, "  -pnm           Select PBMPLUS (PPM/PGM) output format (default)\n");
  213. #endif
  214.   fprintf(stderr, "  -quantize N    Same as -colors N\n");
  215. #ifdef RLE_SUPPORTED
  216.   fprintf(stderr, "  -rle           Select Utah RLE output format\n");
  217. #endif
  218. #ifdef TARGA_SUPPORTED
  219.   fprintf(stderr, "  -targa         Select Targa output format\n");
  220. #endif
  221.   fprintf(stderr, "Switches for advanced users:\n");
  222. #ifdef BLOCK_SMOOTHING_SUPPORTED
  223.   fprintf(stderr, "  -blocksmooth   Apply cross-block smoothing\n");
  224. #endif
  225.   fprintf(stderr, "  -grayscale     Force grayscale output\n");
  226.   fprintf(stderr, "  -nodither      Don't use dithering in quantization\n");
  227. #ifdef QUANT_1PASS_SUPPORTED
  228.   fprintf(stderr, "  -onepass       Use 1-pass quantization (fast, low quality)\n");
  229. #endif
  230.   fprintf(stderr, "  -maxmemory N   Maximum memory to use (in kbytes)\n");
  231.   fprintf(stderr, "  -verbose  or  -debug   Emit debug output\n");
  232.   exit(EXIT_FAILURE);
  233. }
  234.  
  235.  
  236. LOCAL boolean
  237. keymatch (char * arg, const char * keyword, int minchars)
  238. /* Case-insensitive matching of (possibly abbreviated) keyword switches. */
  239. /* keyword is the constant keyword (must be lower case already), */
  240. /* minchars is length of minimum legal abbreviation. */
  241. {
  242.   register int ca, ck;
  243.   register int nmatched = 0;
  244.  
  245.   while ((ca = *arg++) != '\0') {
  246.     if ((ck = *keyword++) == '\0')
  247.       return FALSE;        /* arg longer than keyword, no good */
  248.     if (isupper(ca))        /* force arg to lcase (assume ck is already) */
  249.       ca = tolower(ca);
  250.     if (ca != ck)
  251.       return FALSE;        /* no good */
  252.     nmatched++;            /* count matched characters */
  253.   }
  254.   /* reached end of argument; fail if it's too short for unique abbrev */
  255.   if (nmatched < minchars)
  256.     return FALSE;
  257.   return TRUE;            /* A-OK */
  258. }
  259.  
  260.  
  261. LOCAL int
  262. parse_switches (decompress_info_ptr cinfo, int last_file_arg_seen,
  263.         int argc, char **argv)
  264. /* Initialize cinfo with default switch settings, then parse option switches.
  265.  * Returns argv[] index of first file-name argument (== argc if none).
  266.  * Any file names with indexes <= last_file_arg_seen are ignored;
  267.  * they have presumably been processed in a previous iteration.
  268.  * (Pass 0 for last_file_arg_seen on the first or only iteration.)
  269.  */
  270. {
  271.   int argn;
  272.   char * arg;
  273.  
  274.   /* (Re-)initialize the system-dependent error and memory managers. */
  275.   jselerror(cinfo->emethods);    /* error/trace message routines */
  276.   jselmemmgr(cinfo->emethods);    /* memory allocation routines */
  277.   cinfo->methods->d_ui_method_selection = d_ui_method_selection;
  278.  
  279.   /* Now OK to enable signal catcher. */
  280. #ifdef NEED_SIGNAL_CATCHER
  281.   emethods = cinfo->emethods;
  282. #endif
  283.  
  284.   /* Set up default JPEG parameters. */
  285.   j_d_defaults(cinfo, TRUE);
  286.   requested_fmt = DEFAULT_FMT;    /* set default output file format */
  287.   outfilename = NULL;
  288.  
  289.   /* Scan command line options, adjust parameters */
  290.  
  291.   for (argn = 1; argn < argc; argn++) {
  292.     arg = argv[argn];
  293.     if (*arg != '-') {
  294.       /* Not a switch, must be a file name argument */
  295.       if (argn <= last_file_arg_seen) {
  296.     outfilename = NULL;    /* -outfile applies to just one input file */
  297.     continue;        /* ignore this name if previously processed */
  298.       }
  299.       break;            /* else done parsing switches */
  300.     }
  301.     arg++;            /* advance past switch marker character */
  302.  
  303.     if (keymatch(arg, "blocksmooth", 1)) {
  304.       /* Enable cross-block smoothing. */
  305.       cinfo->do_block_smoothing = TRUE;
  306.  
  307.     } else if (keymatch(arg, "colors", 1) || keymatch(arg, "colours", 1) ||
  308.            keymatch(arg, "quantize", 1) || keymatch(arg, "quantise", 1)) {
  309.       /* Do color quantization. */
  310.       int val;
  311.  
  312.       if (++argn >= argc)    /* advance to next argument */
  313.     usage();
  314.       if (sscanf(argv[argn], "%d", &val) != 1)
  315.     usage();
  316.       cinfo->desired_number_of_colors = val;
  317.       cinfo->quantize_colors = TRUE;
  318.  
  319.     } else if (keymatch(arg, "debug", 1) || keymatch(arg, "verbose", 1)) {
  320.       /* Enable debug printouts. */
  321.       /* On first -d, print version identification */
  322.       if (last_file_arg_seen == 0 && cinfo->emethods->trace_level == 0)
  323.     fprintf(stderr, "Independent JPEG Group's DJPEG, version %s\n%s\n",
  324.         JVERSION, JCOPYRIGHT);
  325.       cinfo->emethods->trace_level++;
  326.  
  327.     } else if (keymatch(arg, "gif", 1)) {
  328.       /* GIF output format. */
  329.       requested_fmt = FMT_GIF;
  330.  
  331.     } else if (keymatch(arg, "grayscale", 2) || keymatch(arg, "greyscale",2)) {
  332.       /* Force monochrome output. */
  333.       cinfo->out_color_space = CS_GRAYSCALE;
  334.  
  335.     } else if (keymatch(arg, "maxmemory", 1)) {
  336.       /* Maximum memory in Kb (or Mb with 'm'). */
  337.       long lval;
  338.       char ch = 'x';
  339.  
  340.       if (++argn >= argc)    /* advance to next argument */
  341.     usage();
  342.       if (sscanf(argv[argn], "%ld%c", &lval, &ch) < 1)
  343.     usage();
  344.       if (ch == 'm' || ch == 'M')
  345.     lval *= 1000L;
  346.       cinfo->emethods->max_memory_to_use = lval * 1000L;
  347.  
  348.     } else if (keymatch(arg, "nodither", 3)) {
  349.       /* Suppress dithering in color quantization. */
  350.       cinfo->use_dithering = FALSE;
  351.  
  352.     } else if (keymatch(arg, "onepass", 1)) {
  353.       /* Use fast one-pass quantization. */
  354.       cinfo->two_pass_quantize = FALSE;
  355.  
  356.     } else if (keymatch(arg, "outfile", 3)) {
  357.       /* Set output file name. */
  358.       if (++argn >= argc)    /* advance to next argument */
  359.     usage();
  360.       outfilename = argv[argn];    /* save it away for later use */
  361.  
  362.     } else if (keymatch(arg, "pnm", 1) || keymatch(arg, "ppm", 1)) {
  363.       /* PPM/PGM output format. */
  364.       requested_fmt = FMT_PPM;
  365.  
  366.     } else if (keymatch(arg, "rle", 1)) {
  367.       /* RLE output format. */
  368.       requested_fmt = FMT_RLE;
  369.  
  370.     } else if (keymatch(arg, "targa", 1)) {
  371.       /* Targa output format. */
  372.       requested_fmt = FMT_TARGA;
  373.  
  374.     } else {
  375.       usage();            /* bogus switch */
  376.     }
  377.   }
  378.  
  379.   return argn;            /* return index of next arg (file name) */
  380. }
  381.  
  382.  
  383. /*
  384.  * The main program.
  385.  */
  386.  
  387. GLOBAL int
  388. main (int argc, char **argv)
  389. {
  390.   struct Decompress_info_struct cinfo;
  391.   struct Decompress_methods_struct dc_methods;
  392.   struct External_methods_struct e_methods;
  393.   int file_index;
  394.  
  395.   /* On Mac, fetch a command line. */
  396. #ifdef THINK_C
  397.   argc = ccommand(&argv);
  398. #endif
  399.  
  400.   progname = argv[0];
  401.  
  402.   /* Set up links to method structures. */
  403.   cinfo.methods = &dc_methods;
  404.   cinfo.emethods = &e_methods;
  405.  
  406.   /* Install, but don't yet enable signal catcher. */
  407. #ifdef NEED_SIGNAL_CATCHER
  408.   emethods = NULL;
  409.   signal(SIGINT, signal_catcher);
  410. #ifdef SIGTERM            /* not all systems have SIGTERM */
  411.   signal(SIGTERM, signal_catcher);
  412. #endif
  413. #endif
  414.  
  415.   /* Scan command line: set up compression parameters, find file names. */
  416.  
  417.   file_index = parse_switches(&cinfo, 0, argc, argv);
  418.  
  419. #ifdef TWO_FILE_COMMANDLINE
  420.   /* Must have either -outfile switch or explicit output file name */
  421.   if (outfilename == NULL) {
  422.     if (file_index != argc-2) {
  423.       fprintf(stderr, "%s: must name one input and one output file\n",
  424.           progname);
  425.       usage();
  426.     }
  427.     outfilename = argv[file_index+1];
  428.   } else {
  429.     if (file_index != argc-1) {
  430.       fprintf(stderr, "%s: must name one input and one output file\n",
  431.           progname);
  432.       usage();
  433.     }
  434.   }
  435. #else
  436.   /* Unix style: expect zero or one file name */
  437.   if (file_index < argc-1) {
  438.     fprintf(stderr, "%s: only one input file\n", progname);
  439.     usage();
  440.   }
  441. #endif /* TWO_FILE_COMMANDLINE */
  442.  
  443.   /* Open the input file. */
  444.   if (file_index < argc) {
  445.     if ((cinfo.input_file = fopen(argv[file_index], READ_BINARY)) == NULL) {
  446.       fprintf(stderr, "%s: can't open %s\n", progname, argv[file_index]);
  447.       exit(EXIT_FAILURE);
  448.     }
  449.   } else {
  450.     /* default input file is stdin */
  451. #ifdef USE_SETMODE        /* need to hack file mode? */
  452.     setmode(fileno(stdin), O_BINARY);
  453. #endif
  454. #ifdef USE_FDOPEN        /* need to re-open in binary mode? */
  455.     if ((cinfo.input_file = fdopen(fileno(stdin), READ_BINARY)) == NULL) {
  456.       fprintf(stderr, "%s: can't open stdin\n", progname);
  457.       exit(EXIT_FAILURE);
  458.     }
  459. #else
  460.     cinfo.input_file = stdin;
  461. #endif
  462.   }
  463.  
  464.   /* Open the output file. */
  465.   if (outfilename != NULL) {
  466.     if ((cinfo.output_file = fopen(outfilename, WRITE_BINARY)) == NULL) {
  467.       fprintf(stderr, "%s: can't open %s\n", progname, outfilename);
  468.       exit(EXIT_FAILURE);
  469.     }
  470.   } else {
  471.     /* default output file is stdout */
  472. #ifdef USE_SETMODE        /* need to hack file mode? */
  473.     setmode(fileno(stdout), O_BINARY);
  474. #endif
  475. #ifdef USE_FDOPEN        /* need to re-open in binary mode? */
  476.     if ((cinfo.output_file = fdopen(fileno(stdout), WRITE_BINARY)) == NULL) {
  477.       fprintf(stderr, "%s: can't open stdout\n", progname);
  478.       exit(EXIT_FAILURE);
  479.     }
  480. #else
  481.     cinfo.output_file = stdout;
  482. #endif
  483.   }
  484.   
  485.   /* Set up to read a JFIF or baseline-JPEG file. */
  486.   /* A smarter UI would inspect the first few bytes of the input file */
  487.   /* to determine its type. */
  488. #ifdef JFIF_SUPPORTED
  489.   jselrjfif(&cinfo);
  490. #else
  491.   You shoulda defined JFIF_SUPPORTED.   /* deliberate syntax error */
  492. #endif
  493.  
  494. #ifdef PROGRESS_REPORT
  495.   /* Start up progress display, unless trace output is on */
  496.   if (e_methods.trace_level == 0)
  497.     dc_methods.progress_monitor = progress_monitor;
  498. #endif
  499.  
  500.   /* Do it to it! */
  501.   jpeg_decompress(&cinfo);
  502.  
  503. #ifdef PROGRESS_REPORT
  504.   /* Clear away progress display */
  505.   if (e_methods.trace_level == 0) {
  506.     fprintf(stderr, "\r                \r");
  507.     fflush(stderr);
  508.   }
  509. #endif
  510.  
  511.   /* All done. */
  512.   exit(EXIT_SUCCESS);
  513.   return 0;            /* suppress no-return-value warnings */
  514. }
  515.