Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 1 | /* |
| 2 | * djpeg.c |
| 3 | * |
DRC | a73e870 | 2012-12-31 02:52:30 +0000 | [diff] [blame] | 4 | * This file was part of the Independent JPEG Group's software: |
Thomas G. Lane | 5ead57a | 1998-03-27 00:00:00 +0000 | [diff] [blame] | 5 | * Copyright (C) 1991-1997, Thomas G. Lane. |
DRC | a6ef282 | 2013-09-28 03:23:49 +0000 | [diff] [blame] | 6 | * libjpeg-turbo Modifications: |
DRC | eb32cc1 | 2015-06-25 03:44:36 +0000 | [diff] [blame] | 7 | * Copyright (C) 2010-2011, 2013-2015, D. R. Commander. |
| 8 | * Copyright (C) 2015, Google, Inc. |
DRC | 7e3acc0 | 2015-10-10 10:25:46 -0500 | [diff] [blame] | 9 | * For conditions of distribution and use, see the accompanying README.ijg |
| 10 | * file. |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 11 | * |
| 12 | * This file contains a command-line user interface for the JPEG decompressor. |
| 13 | * It should work on any system with Unix- or MS-DOS-style command lines. |
| 14 | * |
| 15 | * Two different command line styles are permitted, depending on the |
| 16 | * compile-time switch TWO_FILE_COMMANDLINE: |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 17 | * djpeg [options] inputfile outputfile |
| 18 | * djpeg [options] [inputfile] |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 19 | * In the second style, output is always to standard output, which you'd |
| 20 | * normally redirect to a file or pipe to some other program. Input is |
| 21 | * either from a named file or from standard input (typically redirected). |
| 22 | * The second style is convenient on Unix but is unhelpful on systems that |
| 23 | * don't support pipes. Also, you MUST use the first style if your system |
| 24 | * doesn't do binary I/O to stdin/stdout. |
| 25 | * To simplify script writing, the "-outfile" switch is provided. The syntax |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 26 | * djpeg [options] -outfile outputfile inputfile |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 27 | * works regardless of which command line style is used. |
| 28 | */ |
| 29 | |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 30 | #include "cdjpeg.h" /* Common decls for cjpeg/djpeg applications */ |
| 31 | #include "jversion.h" /* for version message */ |
DRC | ff6961f | 2014-04-20 09:17:11 +0000 | [diff] [blame] | 32 | #include "jconfigint.h" |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 33 | |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 34 | #include <ctype.h> /* to declare isprint() */ |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 35 | |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 36 | #ifdef USE_CCOMMAND /* command-line reader for Macintosh */ |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 37 | #ifdef __MWERKS__ |
Thomas G. Lane | 489583f | 1996-02-07 00:00:00 +0000 | [diff] [blame] | 38 | #include <SIOUX.h> /* Metrowerks needs this */ |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 39 | #include <console.h> /* ... and this */ |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 40 | #endif |
| 41 | #ifdef THINK_C |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 42 | #include <console.h> /* Think declares it here */ |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 43 | #endif |
| 44 | #endif |
| 45 | |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 46 | |
Thomas G. Lane | 9ba2f5e | 1994-12-07 00:00:00 +0000 | [diff] [blame] | 47 | /* Create the add-on message string table. */ |
| 48 | |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 49 | #define JMESSAGE(code,string) string , |
Thomas G. Lane | 9ba2f5e | 1994-12-07 00:00:00 +0000 | [diff] [blame] | 50 | |
| 51 | static const char * const cdjpeg_message_table[] = { |
| 52 | #include "cderror.h" |
| 53 | NULL |
| 54 | }; |
| 55 | |
| 56 | |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 57 | /* |
| 58 | * This list defines the known output image formats |
| 59 | * (not all of which need be supported by a given version). |
| 60 | * You can change the default output format by defining DEFAULT_FMT; |
| 61 | * indeed, you had better do so if you undefine PPM_SUPPORTED. |
| 62 | */ |
| 63 | |
| 64 | typedef enum { |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 65 | FMT_BMP, /* BMP format (Windows flavor) */ |
| 66 | FMT_GIF, /* GIF format */ |
| 67 | FMT_OS2, /* BMP format (OS/2 flavor) */ |
| 68 | FMT_PPM, /* PPM/PGM (PBMPLUS formats) */ |
| 69 | FMT_RLE, /* RLE format */ |
| 70 | FMT_TARGA, /* Targa format */ |
| 71 | FMT_TIFF /* TIFF format */ |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 72 | } IMAGE_FORMATS; |
| 73 | |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 74 | #ifndef DEFAULT_FMT /* so can override from CFLAGS in Makefile */ |
| 75 | #define DEFAULT_FMT FMT_PPM |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 76 | #endif |
| 77 | |
| 78 | static IMAGE_FORMATS requested_fmt; |
| 79 | |
| 80 | |
| 81 | /* |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 82 | * Argument-parsing code. |
| 83 | * The switch parser is designed to be useful with DOS-style command line |
| 84 | * syntax, ie, intermixed switches and file names, where only the switches |
| 85 | * to the left of a given file name affect processing of that file. |
| 86 | * The main program in this file doesn't actually use this capability... |
| 87 | */ |
| 88 | |
| 89 | |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 90 | static const char * progname; /* program name for error messages */ |
| 91 | static char * outfilename; /* for -outfile switch */ |
DRC | ab70623 | 2013-01-18 23:42:31 +0000 | [diff] [blame] | 92 | boolean memsrc; /* for -memsrc switch */ |
DRC | 7a7da94 | 2015-06-27 08:10:31 +0000 | [diff] [blame] | 93 | boolean strip, skip; |
DRC | eb32cc1 | 2015-06-25 03:44:36 +0000 | [diff] [blame] | 94 | JDIMENSION startY, endY; |
DRC | ab70623 | 2013-01-18 23:42:31 +0000 | [diff] [blame] | 95 | #define INPUT_BUF_SIZE 4096 |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 96 | |
| 97 | |
Thomas G. Lane | 489583f | 1996-02-07 00:00:00 +0000 | [diff] [blame] | 98 | LOCAL(void) |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 99 | usage (void) |
| 100 | /* complain about bad command line */ |
| 101 | { |
| 102 | fprintf(stderr, "usage: %s [switches] ", progname); |
| 103 | #ifdef TWO_FILE_COMMANDLINE |
| 104 | fprintf(stderr, "inputfile outputfile\n"); |
| 105 | #else |
| 106 | fprintf(stderr, "[inputfile]\n"); |
| 107 | #endif |
| 108 | |
| 109 | fprintf(stderr, "Switches (names may be abbreviated):\n"); |
| 110 | fprintf(stderr, " -colors N Reduce image to no more than N colors\n"); |
| 111 | fprintf(stderr, " -fast Fast, low-quality processing\n"); |
| 112 | fprintf(stderr, " -grayscale Force grayscale output\n"); |
DRC | 6a833a8 | 2011-03-03 16:58:47 +0000 | [diff] [blame] | 113 | fprintf(stderr, " -rgb Force RGB output\n"); |
DRC | 78df2e6 | 2014-05-12 09:23:57 +0000 | [diff] [blame] | 114 | fprintf(stderr, " -rgb565 Force RGB565 output\n"); |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 115 | #ifdef IDCT_SCALING_SUPPORTED |
| 116 | fprintf(stderr, " -scale M/N Scale output image by fraction M/N, eg, 1/8\n"); |
| 117 | #endif |
| 118 | #ifdef BMP_SUPPORTED |
| 119 | fprintf(stderr, " -bmp Select BMP output format (Windows style)%s\n", |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 120 | (DEFAULT_FMT == FMT_BMP ? " (default)" : "")); |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 121 | #endif |
| 122 | #ifdef GIF_SUPPORTED |
| 123 | fprintf(stderr, " -gif Select GIF output format%s\n", |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 124 | (DEFAULT_FMT == FMT_GIF ? " (default)" : "")); |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 125 | #endif |
| 126 | #ifdef BMP_SUPPORTED |
| 127 | fprintf(stderr, " -os2 Select BMP output format (OS/2 style)%s\n", |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 128 | (DEFAULT_FMT == FMT_OS2 ? " (default)" : "")); |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 129 | #endif |
| 130 | #ifdef PPM_SUPPORTED |
| 131 | fprintf(stderr, " -pnm Select PBMPLUS (PPM/PGM) output format%s\n", |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 132 | (DEFAULT_FMT == FMT_PPM ? " (default)" : "")); |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 133 | #endif |
| 134 | #ifdef RLE_SUPPORTED |
| 135 | fprintf(stderr, " -rle Select Utah RLE output format%s\n", |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 136 | (DEFAULT_FMT == FMT_RLE ? " (default)" : "")); |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 137 | #endif |
| 138 | #ifdef TARGA_SUPPORTED |
| 139 | fprintf(stderr, " -targa Select Targa output format%s\n", |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 140 | (DEFAULT_FMT == FMT_TARGA ? " (default)" : "")); |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 141 | #endif |
| 142 | fprintf(stderr, "Switches for advanced users:\n"); |
| 143 | #ifdef DCT_ISLOW_SUPPORTED |
| 144 | fprintf(stderr, " -dct int Use integer DCT method%s\n", |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 145 | (JDCT_DEFAULT == JDCT_ISLOW ? " (default)" : "")); |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 146 | #endif |
| 147 | #ifdef DCT_IFAST_SUPPORTED |
| 148 | fprintf(stderr, " -dct fast Use fast integer DCT (less accurate)%s\n", |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 149 | (JDCT_DEFAULT == JDCT_IFAST ? " (default)" : "")); |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 150 | #endif |
| 151 | #ifdef DCT_FLOAT_SUPPORTED |
| 152 | fprintf(stderr, " -dct float Use floating-point DCT method%s\n", |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 153 | (JDCT_DEFAULT == JDCT_FLOAT ? " (default)" : "")); |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 154 | #endif |
| 155 | fprintf(stderr, " -dither fs Use F-S dithering (default)\n"); |
| 156 | fprintf(stderr, " -dither none Don't use dithering in quantization\n"); |
| 157 | fprintf(stderr, " -dither ordered Use ordered dither (medium speed, quality)\n"); |
| 158 | #ifdef QUANT_2PASS_SUPPORTED |
| 159 | fprintf(stderr, " -map FILE Map to colors used in named image file\n"); |
| 160 | #endif |
| 161 | fprintf(stderr, " -nosmooth Don't use high-quality upsampling\n"); |
| 162 | #ifdef QUANT_1PASS_SUPPORTED |
| 163 | fprintf(stderr, " -onepass Use 1-pass quantization (fast, low quality)\n"); |
| 164 | #endif |
| 165 | fprintf(stderr, " -maxmemory N Maximum memory to use (in kbytes)\n"); |
| 166 | fprintf(stderr, " -outfile name Specify name for output file\n"); |
DRC | ab70623 | 2013-01-18 23:42:31 +0000 | [diff] [blame] | 167 | #if JPEG_LIB_VERSION >= 80 || defined(MEM_SRCDST_SUPPORTED) |
| 168 | fprintf(stderr, " -memsrc Load input file into memory before decompressing\n"); |
| 169 | #endif |
| 170 | |
DRC | 7a7da94 | 2015-06-27 08:10:31 +0000 | [diff] [blame] | 171 | fprintf(stderr, " -skip Y0,Y1 Decode all rows except those between Y0 and Y1 (inclusive)\n"); |
| 172 | fprintf(stderr, " -strip Y0,Y1 Decode only rows between Y0 and Y1 (inclusive)\n"); |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 173 | fprintf(stderr, " -verbose or -debug Emit debug output\n"); |
DRC | 9665f5e | 2014-11-22 04:04:38 +0000 | [diff] [blame] | 174 | fprintf(stderr, " -version Print version information and exit\n"); |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 175 | exit(EXIT_FAILURE); |
| 176 | } |
| 177 | |
| 178 | |
Thomas G. Lane | 489583f | 1996-02-07 00:00:00 +0000 | [diff] [blame] | 179 | LOCAL(int) |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 180 | parse_switches (j_decompress_ptr cinfo, int argc, char **argv, |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 181 | int last_file_arg_seen, boolean for_real) |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 182 | /* Parse optional switches. |
| 183 | * Returns argv[] index of first file-name argument (== argc if none). |
| 184 | * Any file names with indexes <= last_file_arg_seen are ignored; |
| 185 | * they have presumably been processed in a previous iteration. |
| 186 | * (Pass 0 for last_file_arg_seen on the first or only iteration.) |
| 187 | * for_real is FALSE on the first (dummy) pass; we may skip any expensive |
| 188 | * processing. |
| 189 | */ |
| 190 | { |
| 191 | int argn; |
| 192 | char * arg; |
| 193 | |
| 194 | /* Set up default JPEG parameters. */ |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 195 | requested_fmt = DEFAULT_FMT; /* set default output file format */ |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 196 | outfilename = NULL; |
DRC | ab70623 | 2013-01-18 23:42:31 +0000 | [diff] [blame] | 197 | memsrc = FALSE; |
DRC | 7a7da94 | 2015-06-27 08:10:31 +0000 | [diff] [blame] | 198 | strip = FALSE; |
DRC | 07afe8a | 2015-06-25 03:44:37 +0000 | [diff] [blame] | 199 | skip = FALSE; |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 200 | cinfo->err->trace_level = 0; |
| 201 | |
| 202 | /* Scan command line options, adjust parameters */ |
| 203 | |
| 204 | for (argn = 1; argn < argc; argn++) { |
| 205 | arg = argv[argn]; |
| 206 | if (*arg != '-') { |
| 207 | /* Not a switch, must be a file name argument */ |
| 208 | if (argn <= last_file_arg_seen) { |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 209 | outfilename = NULL; /* -outfile applies to just one input file */ |
| 210 | continue; /* ignore this name if previously processed */ |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 211 | } |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 212 | break; /* else done parsing switches */ |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 213 | } |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 214 | arg++; /* advance past switch marker character */ |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 215 | |
| 216 | if (keymatch(arg, "bmp", 1)) { |
| 217 | /* BMP output format. */ |
| 218 | requested_fmt = FMT_BMP; |
| 219 | |
| 220 | } else if (keymatch(arg, "colors", 1) || keymatch(arg, "colours", 1) || |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 221 | keymatch(arg, "quantize", 1) || keymatch(arg, "quantise", 1)) { |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 222 | /* Do color quantization. */ |
| 223 | int val; |
| 224 | |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 225 | if (++argn >= argc) /* advance to next argument */ |
| 226 | usage(); |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 227 | if (sscanf(argv[argn], "%d", &val) != 1) |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 228 | usage(); |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 229 | cinfo->desired_number_of_colors = val; |
| 230 | cinfo->quantize_colors = TRUE; |
| 231 | |
| 232 | } else if (keymatch(arg, "dct", 2)) { |
| 233 | /* Select IDCT algorithm. */ |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 234 | if (++argn >= argc) /* advance to next argument */ |
| 235 | usage(); |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 236 | if (keymatch(argv[argn], "int", 1)) { |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 237 | cinfo->dct_method = JDCT_ISLOW; |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 238 | } else if (keymatch(argv[argn], "fast", 2)) { |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 239 | cinfo->dct_method = JDCT_IFAST; |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 240 | } else if (keymatch(argv[argn], "float", 2)) { |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 241 | cinfo->dct_method = JDCT_FLOAT; |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 242 | } else |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 243 | usage(); |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 244 | |
| 245 | } else if (keymatch(arg, "dither", 2)) { |
| 246 | /* Select dithering algorithm. */ |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 247 | if (++argn >= argc) /* advance to next argument */ |
| 248 | usage(); |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 249 | if (keymatch(argv[argn], "fs", 2)) { |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 250 | cinfo->dither_mode = JDITHER_FS; |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 251 | } else if (keymatch(argv[argn], "none", 2)) { |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 252 | cinfo->dither_mode = JDITHER_NONE; |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 253 | } else if (keymatch(argv[argn], "ordered", 2)) { |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 254 | cinfo->dither_mode = JDITHER_ORDERED; |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 255 | } else |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 256 | usage(); |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 257 | |
| 258 | } else if (keymatch(arg, "debug", 1) || keymatch(arg, "verbose", 1)) { |
| 259 | /* Enable debug printouts. */ |
| 260 | /* On first -d, print version identification */ |
| 261 | static boolean printed_version = FALSE; |
| 262 | |
| 263 | if (! printed_version) { |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 264 | fprintf(stderr, "%s version %s (build %s)\n", |
| 265 | PACKAGE_NAME, VERSION, BUILD); |
| 266 | fprintf(stderr, "%s\n\n", JCOPYRIGHT); |
| 267 | fprintf(stderr, "Emulating The Independent JPEG Group's software, version %s\n\n", |
| 268 | JVERSION); |
| 269 | printed_version = TRUE; |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 270 | } |
| 271 | cinfo->err->trace_level++; |
| 272 | |
DRC | 9665f5e | 2014-11-22 04:04:38 +0000 | [diff] [blame] | 273 | } else if (keymatch(arg, "version", 4)) { |
| 274 | fprintf(stderr, "%s version %s (build %s)\n", |
| 275 | PACKAGE_NAME, VERSION, BUILD); |
DRC | 306add8 | 2014-11-22 04:25:04 +0000 | [diff] [blame] | 276 | exit(EXIT_SUCCESS); |
DRC | 9665f5e | 2014-11-22 04:04:38 +0000 | [diff] [blame] | 277 | |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 278 | } else if (keymatch(arg, "fast", 1)) { |
| 279 | /* Select recommended processing options for quick-and-dirty output. */ |
| 280 | cinfo->two_pass_quantize = FALSE; |
| 281 | cinfo->dither_mode = JDITHER_ORDERED; |
| 282 | if (! cinfo->quantize_colors) /* don't override an earlier -colors */ |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 283 | cinfo->desired_number_of_colors = 216; |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 284 | cinfo->dct_method = JDCT_FASTEST; |
| 285 | cinfo->do_fancy_upsampling = FALSE; |
| 286 | |
| 287 | } else if (keymatch(arg, "gif", 1)) { |
| 288 | /* GIF output format. */ |
| 289 | requested_fmt = FMT_GIF; |
| 290 | |
| 291 | } else if (keymatch(arg, "grayscale", 2) || keymatch(arg, "greyscale",2)) { |
| 292 | /* Force monochrome output. */ |
| 293 | cinfo->out_color_space = JCS_GRAYSCALE; |
| 294 | |
DRC | 6a833a8 | 2011-03-03 16:58:47 +0000 | [diff] [blame] | 295 | } else if (keymatch(arg, "rgb", 2)) { |
| 296 | /* Force RGB output. */ |
| 297 | cinfo->out_color_space = JCS_RGB; |
| 298 | |
DRC | 78df2e6 | 2014-05-12 09:23:57 +0000 | [diff] [blame] | 299 | } else if (keymatch(arg, "rgb565", 2)) { |
| 300 | /* Force RGB565 output. */ |
| 301 | cinfo->out_color_space = JCS_RGB565; |
| 302 | |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 303 | } else if (keymatch(arg, "map", 3)) { |
| 304 | /* Quantize to a color map taken from an input file. */ |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 305 | if (++argn >= argc) /* advance to next argument */ |
| 306 | usage(); |
| 307 | if (for_real) { /* too expensive to do twice! */ |
| 308 | #ifdef QUANT_2PASS_SUPPORTED /* otherwise can't quantize to supplied map */ |
| 309 | FILE * mapfile; |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 310 | |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 311 | if ((mapfile = fopen(argv[argn], READ_BINARY)) == NULL) { |
| 312 | fprintf(stderr, "%s: can't open %s\n", progname, argv[argn]); |
| 313 | exit(EXIT_FAILURE); |
| 314 | } |
| 315 | read_color_map(cinfo, mapfile); |
| 316 | fclose(mapfile); |
| 317 | cinfo->quantize_colors = TRUE; |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 318 | #else |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 319 | ERREXIT(cinfo, JERR_NOT_COMPILED); |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 320 | #endif |
| 321 | } |
| 322 | |
| 323 | } else if (keymatch(arg, "maxmemory", 3)) { |
| 324 | /* Maximum memory in Kb (or Mb with 'm'). */ |
| 325 | long lval; |
| 326 | char ch = 'x'; |
| 327 | |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 328 | if (++argn >= argc) /* advance to next argument */ |
| 329 | usage(); |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 330 | if (sscanf(argv[argn], "%ld%c", &lval, &ch) < 1) |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 331 | usage(); |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 332 | if (ch == 'm' || ch == 'M') |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 333 | lval *= 1000L; |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 334 | cinfo->mem->max_memory_to_use = lval * 1000L; |
| 335 | |
| 336 | } else if (keymatch(arg, "nosmooth", 3)) { |
| 337 | /* Suppress fancy upsampling */ |
| 338 | cinfo->do_fancy_upsampling = FALSE; |
| 339 | |
| 340 | } else if (keymatch(arg, "onepass", 3)) { |
| 341 | /* Use fast one-pass quantization. */ |
| 342 | cinfo->two_pass_quantize = FALSE; |
| 343 | |
| 344 | } else if (keymatch(arg, "os2", 3)) { |
| 345 | /* BMP output format (OS/2 flavor). */ |
| 346 | requested_fmt = FMT_OS2; |
| 347 | |
| 348 | } else if (keymatch(arg, "outfile", 4)) { |
| 349 | /* Set output file name. */ |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 350 | if (++argn >= argc) /* advance to next argument */ |
| 351 | usage(); |
| 352 | outfilename = argv[argn]; /* save it away for later use */ |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 353 | |
DRC | ab70623 | 2013-01-18 23:42:31 +0000 | [diff] [blame] | 354 | } else if (keymatch(arg, "memsrc", 2)) { |
| 355 | /* Use in-memory source manager */ |
| 356 | #if JPEG_LIB_VERSION >= 80 || defined(MEM_SRCDST_SUPPORTED) |
| 357 | memsrc = TRUE; |
| 358 | #else |
| 359 | fprintf(stderr, "%s: sorry, in-memory source manager was not compiled in\n", |
| 360 | progname); |
| 361 | exit(EXIT_FAILURE); |
| 362 | #endif |
| 363 | |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 364 | } else if (keymatch(arg, "pnm", 1) || keymatch(arg, "ppm", 1)) { |
| 365 | /* PPM/PGM output format. */ |
| 366 | requested_fmt = FMT_PPM; |
| 367 | |
| 368 | } else if (keymatch(arg, "rle", 1)) { |
| 369 | /* RLE output format. */ |
| 370 | requested_fmt = FMT_RLE; |
| 371 | |
DRC | eb32cc1 | 2015-06-25 03:44:36 +0000 | [diff] [blame] | 372 | } else if (keymatch(arg, "scale", 2)) { |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 373 | /* Scale the output image by a fraction M/N. */ |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 374 | if (++argn >= argc) /* advance to next argument */ |
| 375 | usage(); |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 376 | if (sscanf(argv[argn], "%d/%d", |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 377 | &cinfo->scale_num, &cinfo->scale_denom) != 2) |
| 378 | usage(); |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 379 | |
DRC | 7a7da94 | 2015-06-27 08:10:31 +0000 | [diff] [blame] | 380 | } else if (keymatch(arg, "strip", 2)) { |
DRC | eb32cc1 | 2015-06-25 03:44:36 +0000 | [diff] [blame] | 381 | if (++argn >= argc) |
| 382 | usage(); |
| 383 | if (sscanf(argv[argn], "%d,%d", &startY, &endY) != 2 || startY > endY) |
| 384 | usage(); |
DRC | 7a7da94 | 2015-06-27 08:10:31 +0000 | [diff] [blame] | 385 | strip = TRUE; |
DRC | eb32cc1 | 2015-06-25 03:44:36 +0000 | [diff] [blame] | 386 | |
DRC | 07afe8a | 2015-06-25 03:44:37 +0000 | [diff] [blame] | 387 | |
| 388 | } else if (keymatch(arg, "skip", 2)) { |
| 389 | if (++argn >= argc) |
| 390 | usage(); |
| 391 | if (sscanf(argv[argn], "%d,%d", &startY, &endY) != 2 || startY > endY) |
| 392 | usage(); |
| 393 | skip = TRUE; |
| 394 | |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 395 | } else if (keymatch(arg, "targa", 1)) { |
| 396 | /* Targa output format. */ |
| 397 | requested_fmt = FMT_TARGA; |
| 398 | |
| 399 | } else { |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 400 | usage(); /* bogus switch */ |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 401 | } |
| 402 | } |
| 403 | |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 404 | return argn; /* return index of next arg (file name) */ |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 405 | } |
| 406 | |
| 407 | |
| 408 | /* |
Thomas G. Lane | 5ead57a | 1998-03-27 00:00:00 +0000 | [diff] [blame] | 409 | * Marker processor for COM and interesting APPn markers. |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 410 | * This replaces the library's built-in processor, which just skips the marker. |
Thomas G. Lane | 5ead57a | 1998-03-27 00:00:00 +0000 | [diff] [blame] | 411 | * We want to print out the marker as text, to the extent possible. |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 412 | * Note this code relies on a non-suspending data source. |
| 413 | */ |
| 414 | |
Thomas G. Lane | 489583f | 1996-02-07 00:00:00 +0000 | [diff] [blame] | 415 | LOCAL(unsigned int) |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 416 | jpeg_getc (j_decompress_ptr cinfo) |
| 417 | /* Read next byte */ |
| 418 | { |
| 419 | struct jpeg_source_mgr * datasrc = cinfo->src; |
| 420 | |
| 421 | if (datasrc->bytes_in_buffer == 0) { |
| 422 | if (! (*datasrc->fill_input_buffer) (cinfo)) |
| 423 | ERREXIT(cinfo, JERR_CANT_SUSPEND); |
| 424 | } |
| 425 | datasrc->bytes_in_buffer--; |
| 426 | return GETJOCTET(*datasrc->next_input_byte++); |
| 427 | } |
| 428 | |
| 429 | |
Thomas G. Lane | 489583f | 1996-02-07 00:00:00 +0000 | [diff] [blame] | 430 | METHODDEF(boolean) |
Thomas G. Lane | 5ead57a | 1998-03-27 00:00:00 +0000 | [diff] [blame] | 431 | print_text_marker (j_decompress_ptr cinfo) |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 432 | { |
| 433 | boolean traceit = (cinfo->err->trace_level >= 1); |
DRC | 1e32fe3 | 2015-10-14 17:32:39 -0500 | [diff] [blame] | 434 | long length; |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 435 | unsigned int ch; |
| 436 | unsigned int lastch = 0; |
| 437 | |
| 438 | length = jpeg_getc(cinfo) << 8; |
| 439 | length += jpeg_getc(cinfo); |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 440 | length -= 2; /* discount the length word itself */ |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 441 | |
Thomas G. Lane | 5ead57a | 1998-03-27 00:00:00 +0000 | [diff] [blame] | 442 | if (traceit) { |
| 443 | if (cinfo->unread_marker == JPEG_COM) |
| 444 | fprintf(stderr, "Comment, length %ld:\n", (long) length); |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 445 | else /* assume it is an APPn otherwise */ |
Thomas G. Lane | 5ead57a | 1998-03-27 00:00:00 +0000 | [diff] [blame] | 446 | fprintf(stderr, "APP%d, length %ld:\n", |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 447 | cinfo->unread_marker - JPEG_APP0, (long) length); |
Thomas G. Lane | 5ead57a | 1998-03-27 00:00:00 +0000 | [diff] [blame] | 448 | } |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 449 | |
| 450 | while (--length >= 0) { |
| 451 | ch = jpeg_getc(cinfo); |
| 452 | if (traceit) { |
| 453 | /* Emit the character in a readable form. |
| 454 | * Nonprintables are converted to \nnn form, |
| 455 | * while \ is converted to \\. |
| 456 | * Newlines in CR, CR/LF, or LF form will be printed as one newline. |
| 457 | */ |
| 458 | if (ch == '\r') { |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 459 | fprintf(stderr, "\n"); |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 460 | } else if (ch == '\n') { |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 461 | if (lastch != '\r') |
| 462 | fprintf(stderr, "\n"); |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 463 | } else if (ch == '\\') { |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 464 | fprintf(stderr, "\\\\"); |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 465 | } else if (isprint(ch)) { |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 466 | putc(ch, stderr); |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 467 | } else { |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 468 | fprintf(stderr, "\\%03o", ch); |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 469 | } |
| 470 | lastch = ch; |
| 471 | } |
| 472 | } |
| 473 | |
| 474 | if (traceit) |
| 475 | fprintf(stderr, "\n"); |
| 476 | |
| 477 | return TRUE; |
| 478 | } |
| 479 | |
| 480 | |
| 481 | /* |
| 482 | * The main program. |
| 483 | */ |
| 484 | |
Thomas G. Lane | 489583f | 1996-02-07 00:00:00 +0000 | [diff] [blame] | 485 | int |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 486 | main (int argc, char **argv) |
| 487 | { |
| 488 | struct jpeg_decompress_struct cinfo; |
| 489 | struct jpeg_error_mgr jerr; |
| 490 | #ifdef PROGRESS_REPORT |
| 491 | struct cdjpeg_progress_mgr progress; |
| 492 | #endif |
| 493 | int file_index; |
| 494 | djpeg_dest_ptr dest_mgr = NULL; |
| 495 | FILE * input_file; |
| 496 | FILE * output_file; |
DRC | ab70623 | 2013-01-18 23:42:31 +0000 | [diff] [blame] | 497 | unsigned char *inbuffer = NULL; |
| 498 | unsigned long insize = 0; |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 499 | JDIMENSION num_scanlines; |
| 500 | |
| 501 | /* On Mac, fetch a command line. */ |
| 502 | #ifdef USE_CCOMMAND |
| 503 | argc = ccommand(&argv); |
| 504 | #endif |
| 505 | |
| 506 | progname = argv[0]; |
| 507 | if (progname == NULL || progname[0] == 0) |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 508 | progname = "djpeg"; /* in case C library doesn't provide it */ |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 509 | |
| 510 | /* Initialize the JPEG decompression object with default error handling. */ |
| 511 | cinfo.err = jpeg_std_error(&jerr); |
| 512 | jpeg_create_decompress(&cinfo); |
| 513 | /* Add some application-specific error messages (from cderror.h) */ |
Thomas G. Lane | 9ba2f5e | 1994-12-07 00:00:00 +0000 | [diff] [blame] | 514 | jerr.addon_message_table = cdjpeg_message_table; |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 515 | jerr.first_addon_message = JMSG_FIRSTADDONCODE; |
| 516 | jerr.last_addon_message = JMSG_LASTADDONCODE; |
Thomas G. Lane | 5ead57a | 1998-03-27 00:00:00 +0000 | [diff] [blame] | 517 | |
| 518 | /* Insert custom marker processor for COM and APP12. |
| 519 | * APP12 is used by some digital camera makers for textual info, |
| 520 | * so we provide the ability to display it as text. |
| 521 | * If you like, additional APPn marker types can be selected for display, |
DRC | 39ea562 | 2010-10-12 01:55:31 +0000 | [diff] [blame] | 522 | * but don't try to override APP0 or APP14 this way (see libjpeg.txt). |
Thomas G. Lane | 5ead57a | 1998-03-27 00:00:00 +0000 | [diff] [blame] | 523 | */ |
| 524 | jpeg_set_marker_processor(&cinfo, JPEG_COM, print_text_marker); |
| 525 | jpeg_set_marker_processor(&cinfo, JPEG_APP0+12, print_text_marker); |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 526 | |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 527 | /* Scan command line to find file names. */ |
| 528 | /* It is convenient to use just one switch-parsing routine, but the switch |
| 529 | * values read here are ignored; we will rescan the switches after opening |
| 530 | * the input file. |
| 531 | * (Exception: tracing level set here controls verbosity for COM markers |
| 532 | * found during jpeg_read_header...) |
| 533 | */ |
| 534 | |
| 535 | file_index = parse_switches(&cinfo, argc, argv, 0, FALSE); |
| 536 | |
| 537 | #ifdef TWO_FILE_COMMANDLINE |
| 538 | /* Must have either -outfile switch or explicit output file name */ |
| 539 | if (outfilename == NULL) { |
| 540 | if (file_index != argc-2) { |
| 541 | fprintf(stderr, "%s: must name one input and one output file\n", |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 542 | progname); |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 543 | usage(); |
| 544 | } |
| 545 | outfilename = argv[file_index+1]; |
| 546 | } else { |
| 547 | if (file_index != argc-1) { |
| 548 | fprintf(stderr, "%s: must name one input and one output file\n", |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 549 | progname); |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 550 | usage(); |
| 551 | } |
| 552 | } |
| 553 | #else |
| 554 | /* Unix style: expect zero or one file name */ |
| 555 | if (file_index < argc-1) { |
| 556 | fprintf(stderr, "%s: only one input file\n", progname); |
| 557 | usage(); |
| 558 | } |
| 559 | #endif /* TWO_FILE_COMMANDLINE */ |
| 560 | |
| 561 | /* Open the input file. */ |
| 562 | if (file_index < argc) { |
| 563 | if ((input_file = fopen(argv[file_index], READ_BINARY)) == NULL) { |
| 564 | fprintf(stderr, "%s: can't open %s\n", progname, argv[file_index]); |
| 565 | exit(EXIT_FAILURE); |
| 566 | } |
| 567 | } else { |
| 568 | /* default input file is stdin */ |
Thomas G. Lane | bc79e06 | 1995-08-02 00:00:00 +0000 | [diff] [blame] | 569 | input_file = read_stdin(); |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 570 | } |
| 571 | |
| 572 | /* Open the output file. */ |
| 573 | if (outfilename != NULL) { |
| 574 | if ((output_file = fopen(outfilename, WRITE_BINARY)) == NULL) { |
| 575 | fprintf(stderr, "%s: can't open %s\n", progname, outfilename); |
| 576 | exit(EXIT_FAILURE); |
| 577 | } |
| 578 | } else { |
| 579 | /* default output file is stdout */ |
Thomas G. Lane | bc79e06 | 1995-08-02 00:00:00 +0000 | [diff] [blame] | 580 | output_file = write_stdout(); |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 581 | } |
| 582 | |
| 583 | #ifdef PROGRESS_REPORT |
Thomas G. Lane | bc79e06 | 1995-08-02 00:00:00 +0000 | [diff] [blame] | 584 | start_progress_monitor((j_common_ptr) &cinfo, &progress); |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 585 | #endif |
| 586 | |
| 587 | /* Specify data source for decompression */ |
DRC | ab70623 | 2013-01-18 23:42:31 +0000 | [diff] [blame] | 588 | #if JPEG_LIB_VERSION >= 80 || defined(MEM_SRCDST_SUPPORTED) |
| 589 | if (memsrc) { |
| 590 | size_t nbytes; |
| 591 | do { |
| 592 | inbuffer = (unsigned char *)realloc(inbuffer, insize + INPUT_BUF_SIZE); |
| 593 | if (inbuffer == NULL) { |
| 594 | fprintf(stderr, "%s: memory allocation failure\n", progname); |
| 595 | exit(EXIT_FAILURE); |
| 596 | } |
| 597 | nbytes = JFREAD(input_file, &inbuffer[insize], INPUT_BUF_SIZE); |
DRC | c45653e | 2013-10-12 21:52:48 +0000 | [diff] [blame] | 598 | if (nbytes < INPUT_BUF_SIZE && ferror(input_file)) { |
DRC | ab70623 | 2013-01-18 23:42:31 +0000 | [diff] [blame] | 599 | if (file_index < argc) |
| 600 | fprintf(stderr, "%s: can't read from %s\n", progname, |
| 601 | argv[file_index]); |
| 602 | else |
| 603 | fprintf(stderr, "%s: can't read from stdin\n", progname); |
| 604 | } |
DRC | 736fb06 | 2013-01-18 23:45:06 +0000 | [diff] [blame] | 605 | insize += (unsigned long)nbytes; |
DRC | ab70623 | 2013-01-18 23:42:31 +0000 | [diff] [blame] | 606 | } while (nbytes == INPUT_BUF_SIZE); |
| 607 | fprintf(stderr, "Compressed size: %lu bytes\n", insize); |
| 608 | jpeg_mem_src(&cinfo, inbuffer, insize); |
| 609 | } else |
| 610 | #endif |
| 611 | jpeg_stdio_src(&cinfo, input_file); |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 612 | |
| 613 | /* Read file header, set default decompression parameters */ |
| 614 | (void) jpeg_read_header(&cinfo, TRUE); |
| 615 | |
| 616 | /* Adjust default decompression parameters by re-parsing the options */ |
| 617 | file_index = parse_switches(&cinfo, argc, argv, 0, TRUE); |
| 618 | |
| 619 | /* Initialize the output module now to let it override any crucial |
| 620 | * option settings (for instance, GIF wants to force color quantization). |
| 621 | */ |
| 622 | switch (requested_fmt) { |
| 623 | #ifdef BMP_SUPPORTED |
| 624 | case FMT_BMP: |
| 625 | dest_mgr = jinit_write_bmp(&cinfo, FALSE); |
| 626 | break; |
| 627 | case FMT_OS2: |
| 628 | dest_mgr = jinit_write_bmp(&cinfo, TRUE); |
| 629 | break; |
| 630 | #endif |
| 631 | #ifdef GIF_SUPPORTED |
| 632 | case FMT_GIF: |
| 633 | dest_mgr = jinit_write_gif(&cinfo); |
| 634 | break; |
| 635 | #endif |
| 636 | #ifdef PPM_SUPPORTED |
| 637 | case FMT_PPM: |
| 638 | dest_mgr = jinit_write_ppm(&cinfo); |
| 639 | break; |
| 640 | #endif |
| 641 | #ifdef RLE_SUPPORTED |
| 642 | case FMT_RLE: |
| 643 | dest_mgr = jinit_write_rle(&cinfo); |
| 644 | break; |
| 645 | #endif |
| 646 | #ifdef TARGA_SUPPORTED |
| 647 | case FMT_TARGA: |
| 648 | dest_mgr = jinit_write_targa(&cinfo); |
| 649 | break; |
| 650 | #endif |
| 651 | default: |
| 652 | ERREXIT(&cinfo, JERR_UNSUPPORTED_FORMAT); |
| 653 | break; |
| 654 | } |
| 655 | dest_mgr->output_file = output_file; |
| 656 | |
| 657 | /* Start decompressor */ |
Thomas G. Lane | bc79e06 | 1995-08-02 00:00:00 +0000 | [diff] [blame] | 658 | (void) jpeg_start_decompress(&cinfo); |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 659 | |
DRC | 7a7da94 | 2015-06-27 08:10:31 +0000 | [diff] [blame] | 660 | /* Strip decode */ |
| 661 | if (strip || skip) { |
DRC | eb32cc1 | 2015-06-25 03:44:36 +0000 | [diff] [blame] | 662 | JDIMENSION tmp; |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 663 | |
DRC | eb32cc1 | 2015-06-25 03:44:36 +0000 | [diff] [blame] | 664 | /* Check for valid endY. We cannot check this value until after |
| 665 | * jpeg_start_decompress() is called. Note that we have already verified |
| 666 | * that startY <= endY. |
| 667 | */ |
DRC | 7a7da94 | 2015-06-27 08:10:31 +0000 | [diff] [blame] | 668 | if (endY > cinfo.output_height - 1) { |
| 669 | fprintf(stderr, "%s: strip %d-%d exceeds image height %d\n", progname, |
DRC | eb32cc1 | 2015-06-25 03:44:36 +0000 | [diff] [blame] | 670 | startY, endY, cinfo.output_height); |
| 671 | exit(EXIT_FAILURE); |
| 672 | } |
| 673 | |
| 674 | /* Write output file header. This is a hack to ensure that the destination |
| 675 | * manager creates an image of the proper size for the partial decode. |
| 676 | */ |
| 677 | tmp = cinfo.output_height; |
DRC | 7a7da94 | 2015-06-27 08:10:31 +0000 | [diff] [blame] | 678 | cinfo.output_height = endY - startY + 1; |
DRC | 07afe8a | 2015-06-25 03:44:37 +0000 | [diff] [blame] | 679 | if (skip) |
| 680 | cinfo.output_height = tmp - cinfo.output_height; |
DRC | eb32cc1 | 2015-06-25 03:44:36 +0000 | [diff] [blame] | 681 | (*dest_mgr->start_output) (&cinfo, dest_mgr); |
| 682 | cinfo.output_height = tmp; |
| 683 | |
| 684 | /* Process data */ |
DRC | 07afe8a | 2015-06-25 03:44:37 +0000 | [diff] [blame] | 685 | if (skip) { |
| 686 | while (cinfo.output_scanline < startY) { |
| 687 | num_scanlines = jpeg_read_scanlines(&cinfo, dest_mgr->buffer, |
| 688 | dest_mgr->buffer_height); |
| 689 | (*dest_mgr->put_pixel_rows) (&cinfo, dest_mgr, num_scanlines); |
| 690 | } |
DRC | 7a7da94 | 2015-06-27 08:10:31 +0000 | [diff] [blame] | 691 | jpeg_skip_scanlines(&cinfo, endY - startY + 1); |
DRC | 07afe8a | 2015-06-25 03:44:37 +0000 | [diff] [blame] | 692 | while (cinfo.output_scanline < cinfo.output_height) { |
| 693 | num_scanlines = jpeg_read_scanlines(&cinfo, dest_mgr->buffer, |
| 694 | dest_mgr->buffer_height); |
| 695 | (*dest_mgr->put_pixel_rows) (&cinfo, dest_mgr, num_scanlines); |
| 696 | } |
| 697 | } else { |
| 698 | jpeg_skip_scanlines(&cinfo, startY); |
DRC | 7a7da94 | 2015-06-27 08:10:31 +0000 | [diff] [blame] | 699 | while (cinfo.output_scanline <= endY) { |
DRC | 07afe8a | 2015-06-25 03:44:37 +0000 | [diff] [blame] | 700 | num_scanlines = jpeg_read_scanlines(&cinfo, dest_mgr->buffer, |
| 701 | dest_mgr->buffer_height); |
| 702 | (*dest_mgr->put_pixel_rows) (&cinfo, dest_mgr, num_scanlines); |
| 703 | } |
DRC | 7a7da94 | 2015-06-27 08:10:31 +0000 | [diff] [blame] | 704 | jpeg_skip_scanlines(&cinfo, cinfo.output_height - endY + 1); |
DRC | eb32cc1 | 2015-06-25 03:44:36 +0000 | [diff] [blame] | 705 | } |
DRC | eb32cc1 | 2015-06-25 03:44:36 +0000 | [diff] [blame] | 706 | |
| 707 | /* Normal full image decode */ |
| 708 | } else { |
| 709 | /* Write output file header */ |
| 710 | (*dest_mgr->start_output) (&cinfo, dest_mgr); |
| 711 | |
| 712 | /* Process data */ |
| 713 | while (cinfo.output_scanline < cinfo.output_height) { |
| 714 | num_scanlines = jpeg_read_scanlines(&cinfo, dest_mgr->buffer, |
| 715 | dest_mgr->buffer_height); |
| 716 | (*dest_mgr->put_pixel_rows) (&cinfo, dest_mgr, num_scanlines); |
| 717 | } |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 718 | } |
| 719 | |
| 720 | #ifdef PROGRESS_REPORT |
| 721 | /* Hack: count final pass as done in case finish_output does an extra pass. |
| 722 | * The library won't have updated completed_passes. |
| 723 | */ |
| 724 | progress.pub.completed_passes = progress.pub.total_passes; |
| 725 | #endif |
| 726 | |
| 727 | /* Finish decompression and release memory. |
| 728 | * I must do it in this order because output module has allocated memory |
| 729 | * of lifespan JPOOL_IMAGE; it needs to finish before releasing memory. |
| 730 | */ |
| 731 | (*dest_mgr->finish_output) (&cinfo, dest_mgr); |
Thomas G. Lane | bc79e06 | 1995-08-02 00:00:00 +0000 | [diff] [blame] | 732 | (void) jpeg_finish_decompress(&cinfo); |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 733 | jpeg_destroy_decompress(&cinfo); |
| 734 | |
Thomas G. Lane | 9ba2f5e | 1994-12-07 00:00:00 +0000 | [diff] [blame] | 735 | /* Close files, if we opened them */ |
| 736 | if (input_file != stdin) |
| 737 | fclose(input_file); |
| 738 | if (output_file != stdout) |
| 739 | fclose(output_file); |
| 740 | |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 741 | #ifdef PROGRESS_REPORT |
Thomas G. Lane | bc79e06 | 1995-08-02 00:00:00 +0000 | [diff] [blame] | 742 | end_progress_monitor((j_common_ptr) &cinfo); |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 743 | #endif |
| 744 | |
DRC | ab70623 | 2013-01-18 23:42:31 +0000 | [diff] [blame] | 745 | if (memsrc && inbuffer != NULL) |
| 746 | free(inbuffer); |
| 747 | |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 748 | /* All done. */ |
| 749 | exit(jerr.num_warnings ? EXIT_WARNING : EXIT_SUCCESS); |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 750 | return 0; /* suppress no-return-value warnings */ |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 751 | } |