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