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