Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Path: blob/master/external/source/vncdll/winvnc/libjpeg/djpeg.c
Views: 11784
/*1* djpeg.c2*3* Copyright (C) 1991-1997, Thomas G. Lane.4* This file is part of the Independent JPEG Group's software.5* For conditions of distribution and use, see the accompanying README file.6*7* This file contains a command-line user interface for the JPEG decompressor.8* It should work on any system with Unix- or MS-DOS-style command lines.9*10* Two different command line styles are permitted, depending on the11* compile-time switch TWO_FILE_COMMANDLINE:12* djpeg [options] inputfile outputfile13* djpeg [options] [inputfile]14* In the second style, output is always to standard output, which you'd15* normally redirect to a file or pipe to some other program. Input is16* either from a named file or from standard input (typically redirected).17* The second style is convenient on Unix but is unhelpful on systems that18* don't support pipes. Also, you MUST use the first style if your system19* doesn't do binary I/O to stdin/stdout.20* To simplify script writing, the "-outfile" switch is provided. The syntax21* djpeg [options] -outfile outputfile inputfile22* works regardless of which command line style is used.23*/2425#include "cdjpeg.h" /* Common decls for cjpeg/djpeg applications */26#include "jversion.h" /* for version message */2728#include <ctype.h> /* to declare isprint() */2930#ifdef USE_CCOMMAND /* command-line reader for Macintosh */31#ifdef __MWERKS__32#include <SIOUX.h> /* Metrowerks needs this */33#include <console.h> /* ... and this */34#endif35#ifdef THINK_C36#include <console.h> /* Think declares it here */37#endif38#endif394041/* Create the add-on message string table. */4243#define JMESSAGE(code,string) string ,4445static const char * const cdjpeg_message_table[] = {46#include "cderror.h"47NULL48};495051/*52* This list defines the known output image formats53* (not all of which need be supported by a given version).54* You can change the default output format by defining DEFAULT_FMT;55* indeed, you had better do so if you undefine PPM_SUPPORTED.56*/5758typedef enum {59FMT_BMP, /* BMP format (Windows flavor) */60FMT_GIF, /* GIF format */61FMT_OS2, /* BMP format (OS/2 flavor) */62FMT_PPM, /* PPM/PGM (PBMPLUS formats) */63FMT_RLE, /* RLE format */64FMT_TARGA, /* Targa format */65FMT_TIFF /* TIFF format */66} IMAGE_FORMATS;6768#ifndef DEFAULT_FMT /* so can override from CFLAGS in Makefile */69#define DEFAULT_FMT FMT_PPM70#endif7172static IMAGE_FORMATS requested_fmt;737475/*76* Argument-parsing code.77* The switch parser is designed to be useful with DOS-style command line78* syntax, ie, intermixed switches and file names, where only the switches79* to the left of a given file name affect processing of that file.80* The main program in this file doesn't actually use this capability...81*/828384static const char * progname; /* program name for error messages */85static char * outfilename; /* for -outfile switch */868788LOCAL(void)89usage (void)90/* complain about bad command line */91{92fprintf(stderr, "usage: %s [switches] ", progname);93#ifdef TWO_FILE_COMMANDLINE94fprintf(stderr, "inputfile outputfile\n");95#else96fprintf(stderr, "[inputfile]\n");97#endif9899fprintf(stderr, "Switches (names may be abbreviated):\n");100fprintf(stderr, " -colors N Reduce image to no more than N colors\n");101fprintf(stderr, " -fast Fast, low-quality processing\n");102fprintf(stderr, " -grayscale Force grayscale output\n");103#ifdef IDCT_SCALING_SUPPORTED104fprintf(stderr, " -scale M/N Scale output image by fraction M/N, eg, 1/8\n");105#endif106#ifdef BMP_SUPPORTED107fprintf(stderr, " -bmp Select BMP output format (Windows style)%s\n",108(DEFAULT_FMT == FMT_BMP ? " (default)" : ""));109#endif110#ifdef GIF_SUPPORTED111fprintf(stderr, " -gif Select GIF output format%s\n",112(DEFAULT_FMT == FMT_GIF ? " (default)" : ""));113#endif114#ifdef BMP_SUPPORTED115fprintf(stderr, " -os2 Select BMP output format (OS/2 style)%s\n",116(DEFAULT_FMT == FMT_OS2 ? " (default)" : ""));117#endif118#ifdef PPM_SUPPORTED119fprintf(stderr, " -pnm Select PBMPLUS (PPM/PGM) output format%s\n",120(DEFAULT_FMT == FMT_PPM ? " (default)" : ""));121#endif122#ifdef RLE_SUPPORTED123fprintf(stderr, " -rle Select Utah RLE output format%s\n",124(DEFAULT_FMT == FMT_RLE ? " (default)" : ""));125#endif126#ifdef TARGA_SUPPORTED127fprintf(stderr, " -targa Select Targa output format%s\n",128(DEFAULT_FMT == FMT_TARGA ? " (default)" : ""));129#endif130fprintf(stderr, "Switches for advanced users:\n");131#ifdef DCT_ISLOW_SUPPORTED132fprintf(stderr, " -dct int Use integer DCT method%s\n",133(JDCT_DEFAULT == JDCT_ISLOW ? " (default)" : ""));134#endif135#ifdef DCT_IFAST_SUPPORTED136fprintf(stderr, " -dct fast Use fast integer DCT (less accurate)%s\n",137(JDCT_DEFAULT == JDCT_IFAST ? " (default)" : ""));138#endif139#ifdef DCT_FLOAT_SUPPORTED140fprintf(stderr, " -dct float Use floating-point DCT method%s\n",141(JDCT_DEFAULT == JDCT_FLOAT ? " (default)" : ""));142#endif143fprintf(stderr, " -dither fs Use F-S dithering (default)\n");144fprintf(stderr, " -dither none Don't use dithering in quantization\n");145fprintf(stderr, " -dither ordered Use ordered dither (medium speed, quality)\n");146#ifdef QUANT_2PASS_SUPPORTED147fprintf(stderr, " -map FILE Map to colors used in named image file\n");148#endif149fprintf(stderr, " -nosmooth Don't use high-quality upsampling\n");150#ifdef QUANT_1PASS_SUPPORTED151fprintf(stderr, " -onepass Use 1-pass quantization (fast, low quality)\n");152#endif153fprintf(stderr, " -maxmemory N Maximum memory to use (in kbytes)\n");154fprintf(stderr, " -outfile name Specify name for output file\n");155fprintf(stderr, " -verbose or -debug Emit debug output\n");156exit(EXIT_FAILURE);157}158159160LOCAL(int)161parse_switches (j_decompress_ptr cinfo, int argc, char **argv,162int last_file_arg_seen, boolean for_real)163/* Parse optional switches.164* Returns argv[] index of first file-name argument (== argc if none).165* Any file names with indexes <= last_file_arg_seen are ignored;166* they have presumably been processed in a previous iteration.167* (Pass 0 for last_file_arg_seen on the first or only iteration.)168* for_real is FALSE on the first (dummy) pass; we may skip any expensive169* processing.170*/171{172int argn;173char * arg;174175/* Set up default JPEG parameters. */176requested_fmt = DEFAULT_FMT; /* set default output file format */177outfilename = NULL;178cinfo->err->trace_level = 0;179180/* Scan command line options, adjust parameters */181182for (argn = 1; argn < argc; argn++) {183arg = argv[argn];184if (*arg != '-') {185/* Not a switch, must be a file name argument */186if (argn <= last_file_arg_seen) {187outfilename = NULL; /* -outfile applies to just one input file */188continue; /* ignore this name if previously processed */189}190break; /* else done parsing switches */191}192arg++; /* advance past switch marker character */193194if (keymatch(arg, "bmp", 1)) {195/* BMP output format. */196requested_fmt = FMT_BMP;197198} else if (keymatch(arg, "colors", 1) || keymatch(arg, "colours", 1) ||199keymatch(arg, "quantize", 1) || keymatch(arg, "quantise", 1)) {200/* Do color quantization. */201int val;202203if (++argn >= argc) /* advance to next argument */204usage();205if (sscanf(argv[argn], "%d", &val) != 1)206usage();207cinfo->desired_number_of_colors = val;208cinfo->quantize_colors = TRUE;209210} else if (keymatch(arg, "dct", 2)) {211/* Select IDCT algorithm. */212if (++argn >= argc) /* advance to next argument */213usage();214if (keymatch(argv[argn], "int", 1)) {215cinfo->dct_method = JDCT_ISLOW;216} else if (keymatch(argv[argn], "fast", 2)) {217cinfo->dct_method = JDCT_IFAST;218} else if (keymatch(argv[argn], "float", 2)) {219cinfo->dct_method = JDCT_FLOAT;220} else221usage();222223} else if (keymatch(arg, "dither", 2)) {224/* Select dithering algorithm. */225if (++argn >= argc) /* advance to next argument */226usage();227if (keymatch(argv[argn], "fs", 2)) {228cinfo->dither_mode = JDITHER_FS;229} else if (keymatch(argv[argn], "none", 2)) {230cinfo->dither_mode = JDITHER_NONE;231} else if (keymatch(argv[argn], "ordered", 2)) {232cinfo->dither_mode = JDITHER_ORDERED;233} else234usage();235236} else if (keymatch(arg, "debug", 1) || keymatch(arg, "verbose", 1)) {237/* Enable debug printouts. */238/* On first -d, print version identification */239static boolean printed_version = FALSE;240241if (! printed_version) {242fprintf(stderr, "Independent JPEG Group's DJPEG, version %s\n%s\n",243JVERSION, JCOPYRIGHT);244printed_version = TRUE;245}246cinfo->err->trace_level++;247248} else if (keymatch(arg, "fast", 1)) {249/* Select recommended processing options for quick-and-dirty output. */250cinfo->two_pass_quantize = FALSE;251cinfo->dither_mode = JDITHER_ORDERED;252if (! cinfo->quantize_colors) /* don't override an earlier -colors */253cinfo->desired_number_of_colors = 216;254cinfo->dct_method = JDCT_FASTEST;255cinfo->do_fancy_upsampling = FALSE;256257} else if (keymatch(arg, "gif", 1)) {258/* GIF output format. */259requested_fmt = FMT_GIF;260261} else if (keymatch(arg, "grayscale", 2) || keymatch(arg, "greyscale",2)) {262/* Force monochrome output. */263cinfo->out_color_space = JCS_GRAYSCALE;264265} else if (keymatch(arg, "map", 3)) {266/* Quantize to a color map taken from an input file. */267if (++argn >= argc) /* advance to next argument */268usage();269if (for_real) { /* too expensive to do twice! */270#ifdef QUANT_2PASS_SUPPORTED /* otherwise can't quantize to supplied map */271FILE * mapfile;272273if ((mapfile = fopen(argv[argn], READ_BINARY)) == NULL) {274fprintf(stderr, "%s: can't open %s\n", progname, argv[argn]);275exit(EXIT_FAILURE);276}277read_color_map(cinfo, mapfile);278fclose(mapfile);279cinfo->quantize_colors = TRUE;280#else281ERREXIT(cinfo, JERR_NOT_COMPILED);282#endif283}284285} else if (keymatch(arg, "maxmemory", 3)) {286/* Maximum memory in Kb (or Mb with 'm'). */287long lval;288char ch = 'x';289290if (++argn >= argc) /* advance to next argument */291usage();292if (sscanf(argv[argn], "%ld%c", &lval, &ch) < 1)293usage();294if (ch == 'm' || ch == 'M')295lval *= 1000L;296cinfo->mem->max_memory_to_use = lval * 1000L;297298} else if (keymatch(arg, "nosmooth", 3)) {299/* Suppress fancy upsampling */300cinfo->do_fancy_upsampling = FALSE;301302} else if (keymatch(arg, "onepass", 3)) {303/* Use fast one-pass quantization. */304cinfo->two_pass_quantize = FALSE;305306} else if (keymatch(arg, "os2", 3)) {307/* BMP output format (OS/2 flavor). */308requested_fmt = FMT_OS2;309310} else if (keymatch(arg, "outfile", 4)) {311/* Set output file name. */312if (++argn >= argc) /* advance to next argument */313usage();314outfilename = argv[argn]; /* save it away for later use */315316} else if (keymatch(arg, "pnm", 1) || keymatch(arg, "ppm", 1)) {317/* PPM/PGM output format. */318requested_fmt = FMT_PPM;319320} else if (keymatch(arg, "rle", 1)) {321/* RLE output format. */322requested_fmt = FMT_RLE;323324} else if (keymatch(arg, "scale", 1)) {325/* Scale the output image by a fraction M/N. */326if (++argn >= argc) /* advance to next argument */327usage();328if (sscanf(argv[argn], "%d/%d",329&cinfo->scale_num, &cinfo->scale_denom) != 2)330usage();331332} else if (keymatch(arg, "targa", 1)) {333/* Targa output format. */334requested_fmt = FMT_TARGA;335336} else {337usage(); /* bogus switch */338}339}340341return argn; /* return index of next arg (file name) */342}343344345/*346* Marker processor for COM and interesting APPn markers.347* This replaces the library's built-in processor, which just skips the marker.348* We want to print out the marker as text, to the extent possible.349* Note this code relies on a non-suspending data source.350*/351352LOCAL(unsigned int)353jpeg_getc (j_decompress_ptr cinfo)354/* Read next byte */355{356struct jpeg_source_mgr * datasrc = cinfo->src;357358if (datasrc->bytes_in_buffer == 0) {359if (! (*datasrc->fill_input_buffer) (cinfo))360ERREXIT(cinfo, JERR_CANT_SUSPEND);361}362datasrc->bytes_in_buffer--;363return GETJOCTET(*datasrc->next_input_byte++);364}365366367METHODDEF(boolean)368print_text_marker (j_decompress_ptr cinfo)369{370boolean traceit = (cinfo->err->trace_level >= 1);371INT32 length;372unsigned int ch;373unsigned int lastch = 0;374375length = jpeg_getc(cinfo) << 8;376length += jpeg_getc(cinfo);377length -= 2; /* discount the length word itself */378379if (traceit) {380if (cinfo->unread_marker == JPEG_COM)381fprintf(stderr, "Comment, length %ld:\n", (long) length);382else /* assume it is an APPn otherwise */383fprintf(stderr, "APP%d, length %ld:\n",384cinfo->unread_marker - JPEG_APP0, (long) length);385}386387while (--length >= 0) {388ch = jpeg_getc(cinfo);389if (traceit) {390/* Emit the character in a readable form.391* Nonprintables are converted to \nnn form,392* while \ is converted to \\.393* Newlines in CR, CR/LF, or LF form will be printed as one newline.394*/395if (ch == '\r') {396fprintf(stderr, "\n");397} else if (ch == '\n') {398if (lastch != '\r')399fprintf(stderr, "\n");400} else if (ch == '\\') {401fprintf(stderr, "\\\\");402} else if (isprint(ch)) {403putc(ch, stderr);404} else {405fprintf(stderr, "\\%03o", ch);406}407lastch = ch;408}409}410411if (traceit)412fprintf(stderr, "\n");413414return TRUE;415}416417418/*419* The main program.420*/421422int423main (int argc, char **argv)424{425struct jpeg_decompress_struct cinfo;426struct jpeg_error_mgr jerr;427#ifdef PROGRESS_REPORT428struct cdjpeg_progress_mgr progress;429#endif430int file_index;431djpeg_dest_ptr dest_mgr = NULL;432FILE * input_file;433FILE * output_file;434JDIMENSION num_scanlines;435436/* On Mac, fetch a command line. */437#ifdef USE_CCOMMAND438argc = ccommand(&argv);439#endif440441progname = argv[0];442if (progname == NULL || progname[0] == 0)443progname = "djpeg"; /* in case C library doesn't provide it */444445/* Initialize the JPEG decompression object with default error handling. */446cinfo.err = jpeg_std_error(&jerr);447jpeg_create_decompress(&cinfo);448/* Add some application-specific error messages (from cderror.h) */449jerr.addon_message_table = cdjpeg_message_table;450jerr.first_addon_message = JMSG_FIRSTADDONCODE;451jerr.last_addon_message = JMSG_LASTADDONCODE;452453/* Insert custom marker processor for COM and APP12.454* APP12 is used by some digital camera makers for textual info,455* so we provide the ability to display it as text.456* If you like, additional APPn marker types can be selected for display,457* but don't try to override APP0 or APP14 this way (see libjpeg.doc).458*/459jpeg_set_marker_processor(&cinfo, JPEG_COM, print_text_marker);460jpeg_set_marker_processor(&cinfo, JPEG_APP0+12, print_text_marker);461462/* Now safe to enable signal catcher. */463#ifdef NEED_SIGNAL_CATCHER464enable_signal_catcher((j_common_ptr) &cinfo);465#endif466467/* Scan command line to find file names. */468/* It is convenient to use just one switch-parsing routine, but the switch469* values read here are ignored; we will rescan the switches after opening470* the input file.471* (Exception: tracing level set here controls verbosity for COM markers472* found during jpeg_read_header...)473*/474475file_index = parse_switches(&cinfo, argc, argv, 0, FALSE);476477#ifdef TWO_FILE_COMMANDLINE478/* Must have either -outfile switch or explicit output file name */479if (outfilename == NULL) {480if (file_index != argc-2) {481fprintf(stderr, "%s: must name one input and one output file\n",482progname);483usage();484}485outfilename = argv[file_index+1];486} else {487if (file_index != argc-1) {488fprintf(stderr, "%s: must name one input and one output file\n",489progname);490usage();491}492}493#else494/* Unix style: expect zero or one file name */495if (file_index < argc-1) {496fprintf(stderr, "%s: only one input file\n", progname);497usage();498}499#endif /* TWO_FILE_COMMANDLINE */500501/* Open the input file. */502if (file_index < argc) {503if ((input_file = fopen(argv[file_index], READ_BINARY)) == NULL) {504fprintf(stderr, "%s: can't open %s\n", progname, argv[file_index]);505exit(EXIT_FAILURE);506}507} else {508/* default input file is stdin */509input_file = read_stdin();510}511512/* Open the output file. */513if (outfilename != NULL) {514if ((output_file = fopen(outfilename, WRITE_BINARY)) == NULL) {515fprintf(stderr, "%s: can't open %s\n", progname, outfilename);516exit(EXIT_FAILURE);517}518} else {519/* default output file is stdout */520output_file = write_stdout();521}522523#ifdef PROGRESS_REPORT524start_progress_monitor((j_common_ptr) &cinfo, &progress);525#endif526527/* Specify data source for decompression */528jpeg_stdio_src(&cinfo, input_file);529530/* Read file header, set default decompression parameters */531(void) jpeg_read_header(&cinfo, TRUE);532533/* Adjust default decompression parameters by re-parsing the options */534file_index = parse_switches(&cinfo, argc, argv, 0, TRUE);535536/* Initialize the output module now to let it override any crucial537* option settings (for instance, GIF wants to force color quantization).538*/539switch (requested_fmt) {540#ifdef BMP_SUPPORTED541case FMT_BMP:542dest_mgr = jinit_write_bmp(&cinfo, FALSE);543break;544case FMT_OS2:545dest_mgr = jinit_write_bmp(&cinfo, TRUE);546break;547#endif548#ifdef GIF_SUPPORTED549case FMT_GIF:550dest_mgr = jinit_write_gif(&cinfo);551break;552#endif553#ifdef PPM_SUPPORTED554case FMT_PPM:555dest_mgr = jinit_write_ppm(&cinfo);556break;557#endif558#ifdef RLE_SUPPORTED559case FMT_RLE:560dest_mgr = jinit_write_rle(&cinfo);561break;562#endif563#ifdef TARGA_SUPPORTED564case FMT_TARGA:565dest_mgr = jinit_write_targa(&cinfo);566break;567#endif568default:569ERREXIT(&cinfo, JERR_UNSUPPORTED_FORMAT);570break;571}572dest_mgr->output_file = output_file;573574/* Start decompressor */575(void) jpeg_start_decompress(&cinfo);576577/* Write output file header */578(*dest_mgr->start_output) (&cinfo, dest_mgr);579580/* Process data */581while (cinfo.output_scanline < cinfo.output_height) {582num_scanlines = jpeg_read_scanlines(&cinfo, dest_mgr->buffer,583dest_mgr->buffer_height);584(*dest_mgr->put_pixel_rows) (&cinfo, dest_mgr, num_scanlines);585}586587#ifdef PROGRESS_REPORT588/* Hack: count final pass as done in case finish_output does an extra pass.589* The library won't have updated completed_passes.590*/591progress.pub.completed_passes = progress.pub.total_passes;592#endif593594/* Finish decompression and release memory.595* I must do it in this order because output module has allocated memory596* of lifespan JPOOL_IMAGE; it needs to finish before releasing memory.597*/598(*dest_mgr->finish_output) (&cinfo, dest_mgr);599(void) jpeg_finish_decompress(&cinfo);600jpeg_destroy_decompress(&cinfo);601602/* Close files, if we opened them */603if (input_file != stdin)604fclose(input_file);605if (output_file != stdout)606fclose(output_file);607608#ifdef PROGRESS_REPORT609end_progress_monitor((j_common_ptr) &cinfo);610#endif611612/* All done. */613exit(jerr.num_warnings ? EXIT_WARNING : EXIT_SUCCESS);614return 0; /* suppress no-return-value warnings */615}616617618