djpeg.c

来自「JPEG解压源码」· C语言 代码 · 共 751 行 · 第 1/2 页

C
751
字号
/* * alternate djpeg.c * * Copyright (C) 1991-1997, Thomas G. Lane. * This file is part of the Independent JPEG Group's software. * For conditions of distribution and use, see the accompanying README file. * * This file contains an alternate user interface for the JPEG decompressor. * One or more input files are named on the command line, and output file * names are created by substituting an appropriate extension. */#include "cdjpeg.h"		/* Common decls for cjpeg/djpeg applications */#include "jversion.h"		/* for version message */#include <ctype.h>		/* to declare isprint() */#ifdef USE_CCOMMAND		/* command-line reader for Macintosh */#ifdef __MWERKS__#include <SIOUX.h>              /* Metrowerks needs this */#include <console.h>		/* ... and this */#endif#ifdef THINK_C#include <console.h>		/* Think declares it here */#endif#endif#ifndef PATH_MAX		/* ANSI maximum-pathname-length constant */#define PATH_MAX 256#endif/* Create the add-on message string table. */#define JMESSAGE(code,string)	string ,static const char * const cdjpeg_message_table[] = {#include "cderror.h"  NULL};/* * Automatic determination of available memory. */static long default_maxmem;	/* saves value determined at startup, or 0 */#ifndef FREE_MEM_ESTIMATE	/* may be defined from command line */#ifdef MSDOS			/* For MS-DOS (unless flat-memory model) */#include <dos.h>		/* for access to intdos() call */LOCAL(long)unused_dos_memory (void)/* Obtain total amount of unallocated DOS memory */{  union REGS regs;  long nparas;  regs.h.ah = 0x48;		/* DOS function Allocate Memory Block */  regs.x.bx = 0xFFFF;		/* Ask for more memory than DOS can have */  (void) intdos(&regs, &regs);  /* DOS will fail and return # of paragraphs actually available in BX. */  nparas = (unsigned int) regs.x.bx;  /* Times 16 to convert to bytes. */  return nparas << 4;}/* The default memory setting is 95% of the available space. */#define FREE_MEM_ESTIMATE  ((unused_dos_memory() * 95L) / 100L)#endif /* MSDOS */#ifdef ATARI			/* For Atari ST/STE/TT, Pure C or Turbo C */#include <ext.h>/* The default memory setting is 90% of the available space. */#define FREE_MEM_ESTIMATE  (((long) coreleft() * 90L) / 100L)#endif /* ATARI *//* Add memory-estimation procedures for other operating systems here, * with appropriate #ifdef's around them. */#endif /* !FREE_MEM_ESTIMATE *//* * This list defines the known output image formats * (not all of which need be supported by a given version). * You can change the default output format by defining DEFAULT_FMT; * indeed, you had better do so if you undefine PPM_SUPPORTED. */typedef enum {	FMT_BMP,		/* BMP format (Windows flavor) */	FMT_GIF,		/* GIF format */	FMT_OS2,		/* BMP format (OS/2 flavor) */	FMT_PPM,		/* PPM/PGM (PBMPLUS formats) */	FMT_RLE,		/* RLE format */	FMT_TARGA,		/* Targa format */	FMT_TIFF		/* TIFF format */} IMAGE_FORMATS;#ifndef DEFAULT_FMT		/* so can override from CFLAGS in Makefile */#define DEFAULT_FMT	FMT_GIF#endifstatic IMAGE_FORMATS requested_fmt;/* * Argument-parsing code. * The switch parser is designed to be useful with DOS-style command line * syntax, ie, intermixed switches and file names, where only the switches * to the left of a given file name affect processing of that file. */static const char * progname;	/* program name for error messages */static char * outfilename;	/* for -outfile switch */LOCAL(void)usage (void)/* complain about bad command line */{  fprintf(stderr, "usage: %s [switches] inputfile(s)\n", progname);  fprintf(stderr, "List of input files may use wildcards (* and ?)\n");  fprintf(stderr, "Output filename is same as input filename except for extension\n");  fprintf(stderr, "Switches (names may be abbreviated):\n");  fprintf(stderr, "  -colors N      Reduce image to no more than N colors\n");  fprintf(stderr, "  -fast          Fast, low-quality processing\n");  fprintf(stderr, "  -grayscale     Force grayscale output\n");#ifdef IDCT_SCALING_SUPPORTED  fprintf(stderr, "  -scale M/N     Scale output image by fraction M/N, eg, 1/8\n");#endif#ifdef BMP_SUPPORTED  fprintf(stderr, "  -bmp           Select BMP output format (Windows style)%s\n",	  (DEFAULT_FMT == FMT_BMP ? " (default)" : ""));#endif#ifdef GIF_SUPPORTED  fprintf(stderr, "  -gif           Select GIF output format%s\n",	  (DEFAULT_FMT == FMT_GIF ? " (default)" : ""));#endif#ifdef BMP_SUPPORTED  fprintf(stderr, "  -os2           Select BMP output format (OS/2 style)%s\n",	  (DEFAULT_FMT == FMT_OS2 ? " (default)" : ""));#endif#ifdef PPM_SUPPORTED  fprintf(stderr, "  -pnm           Select PBMPLUS (PPM/PGM) output format%s\n",	  (DEFAULT_FMT == FMT_PPM ? " (default)" : ""));#endif#ifdef RLE_SUPPORTED  fprintf(stderr, "  -rle           Select Utah RLE output format%s\n",	  (DEFAULT_FMT == FMT_RLE ? " (default)" : ""));#endif#ifdef TARGA_SUPPORTED  fprintf(stderr, "  -targa         Select Targa output format%s\n",	  (DEFAULT_FMT == FMT_TARGA ? " (default)" : ""));#endif  fprintf(stderr, "Switches for advanced users:\n");#ifdef DCT_ISLOW_SUPPORTED  fprintf(stderr, "  -dct int       Use integer DCT method%s\n",	  (JDCT_DEFAULT == JDCT_ISLOW ? " (default)" : ""));#endif#ifdef DCT_IFAST_SUPPORTED  fprintf(stderr, "  -dct fast      Use fast integer DCT (less accurate)%s\n",	  (JDCT_DEFAULT == JDCT_IFAST ? " (default)" : ""));#endif#ifdef DCT_FLOAT_SUPPORTED  fprintf(stderr, "  -dct float     Use floating-point DCT method%s\n",	  (JDCT_DEFAULT == JDCT_FLOAT ? " (default)" : ""));#endif  fprintf(stderr, "  -dither fs     Use F-S dithering (default)\n");  fprintf(stderr, "  -dither none   Don't use dithering in quantization\n");  fprintf(stderr, "  -dither ordered  Use ordered dither (medium speed, quality)\n");#ifdef QUANT_2PASS_SUPPORTED  fprintf(stderr, "  -map FILE      Map to colors used in named image file\n");#endif  fprintf(stderr, "  -nosmooth      Don't use high-quality upsampling\n");#ifdef QUANT_1PASS_SUPPORTED  fprintf(stderr, "  -onepass       Use 1-pass quantization (fast, low quality)\n");#endif#ifndef FREE_MEM_ESTIMATE  fprintf(stderr, "  -maxmemory N   Maximum memory to use (in kbytes)\n");#endif  fprintf(stderr, "  -outfile name  Specify name for output file\n");  fprintf(stderr, "  -verbose  or  -debug   Emit debug output\n");  exit(EXIT_FAILURE);}LOCAL(int)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;  if (default_maxmem > 0)	/* override library's default value */    cinfo->mem->max_memory_to_use = default_maxmem;  /* 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) != 2)	usage();    } else if (keymatch(arg, "targa", 1)) {      /* Targa output format. */      requested_fmt = FMT_TARGA;

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?