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 | eb32cc1 | 2015-06-25 03:44:36 +0000 | [diff] [blame^] | 92 | boolean stripe; |
| 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 | eb32cc1 | 2015-06-25 03:44:36 +0000 | [diff] [blame^] | 170 | fprintf(stderr, " -stripe Y0,Y1 Decode a horizontal stripe of the image [Y0, Y1)\n"); |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 171 | fprintf(stderr, " -verbose or -debug Emit debug output\n"); |
DRC | 9665f5e | 2014-11-22 04:04:38 +0000 | [diff] [blame] | 172 | fprintf(stderr, " -version Print version information and exit\n"); |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 173 | exit(EXIT_FAILURE); |
| 174 | } |
| 175 | |
| 176 | |
Thomas G. Lane | 489583f | 1996-02-07 00:00:00 +0000 | [diff] [blame] | 177 | LOCAL(int) |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 178 | parse_switches (j_decompress_ptr cinfo, int argc, char **argv, |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 179 | int last_file_arg_seen, boolean for_real) |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 180 | /* Parse optional switches. |
| 181 | * Returns argv[] index of first file-name argument (== argc if none). |
| 182 | * Any file names with indexes <= last_file_arg_seen are ignored; |
| 183 | * they have presumably been processed in a previous iteration. |
| 184 | * (Pass 0 for last_file_arg_seen on the first or only iteration.) |
| 185 | * for_real is FALSE on the first (dummy) pass; we may skip any expensive |
| 186 | * processing. |
| 187 | */ |
| 188 | { |
| 189 | int argn; |
| 190 | char * arg; |
| 191 | |
| 192 | /* Set up default JPEG parameters. */ |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 193 | requested_fmt = DEFAULT_FMT; /* set default output file format */ |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 194 | outfilename = NULL; |
DRC | ab70623 | 2013-01-18 23:42:31 +0000 | [diff] [blame] | 195 | memsrc = FALSE; |
DRC | eb32cc1 | 2015-06-25 03:44:36 +0000 | [diff] [blame^] | 196 | stripe = FALSE; |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 197 | cinfo->err->trace_level = 0; |
| 198 | |
| 199 | /* Scan command line options, adjust parameters */ |
| 200 | |
| 201 | for (argn = 1; argn < argc; argn++) { |
| 202 | arg = argv[argn]; |
| 203 | if (*arg != '-') { |
| 204 | /* Not a switch, must be a file name argument */ |
| 205 | if (argn <= last_file_arg_seen) { |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 206 | outfilename = NULL; /* -outfile applies to just one input file */ |
| 207 | continue; /* ignore this name if previously processed */ |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 208 | } |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 209 | break; /* else done parsing switches */ |
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 | arg++; /* advance past switch marker character */ |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 212 | |
| 213 | if (keymatch(arg, "bmp", 1)) { |
| 214 | /* BMP output format. */ |
| 215 | requested_fmt = FMT_BMP; |
| 216 | |
| 217 | } else if (keymatch(arg, "colors", 1) || keymatch(arg, "colours", 1) || |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 218 | keymatch(arg, "quantize", 1) || keymatch(arg, "quantise", 1)) { |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 219 | /* Do color quantization. */ |
| 220 | int val; |
| 221 | |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 222 | if (++argn >= argc) /* advance to next argument */ |
| 223 | usage(); |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 224 | if (sscanf(argv[argn], "%d", &val) != 1) |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 225 | usage(); |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 226 | cinfo->desired_number_of_colors = val; |
| 227 | cinfo->quantize_colors = TRUE; |
| 228 | |
| 229 | } else if (keymatch(arg, "dct", 2)) { |
| 230 | /* Select IDCT algorithm. */ |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 231 | if (++argn >= argc) /* advance to next argument */ |
| 232 | usage(); |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 233 | if (keymatch(argv[argn], "int", 1)) { |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 234 | cinfo->dct_method = JDCT_ISLOW; |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 235 | } else if (keymatch(argv[argn], "fast", 2)) { |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 236 | cinfo->dct_method = JDCT_IFAST; |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 237 | } else if (keymatch(argv[argn], "float", 2)) { |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 238 | cinfo->dct_method = JDCT_FLOAT; |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 239 | } else |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 240 | usage(); |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 241 | |
| 242 | } else if (keymatch(arg, "dither", 2)) { |
| 243 | /* Select dithering algorithm. */ |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 244 | if (++argn >= argc) /* advance to next argument */ |
| 245 | usage(); |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 246 | if (keymatch(argv[argn], "fs", 2)) { |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 247 | cinfo->dither_mode = JDITHER_FS; |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 248 | } else if (keymatch(argv[argn], "none", 2)) { |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 249 | cinfo->dither_mode = JDITHER_NONE; |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 250 | } else if (keymatch(argv[argn], "ordered", 2)) { |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 251 | cinfo->dither_mode = JDITHER_ORDERED; |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 252 | } else |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 253 | usage(); |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 254 | |
| 255 | } else if (keymatch(arg, "debug", 1) || keymatch(arg, "verbose", 1)) { |
| 256 | /* Enable debug printouts. */ |
| 257 | /* On first -d, print version identification */ |
| 258 | static boolean printed_version = FALSE; |
| 259 | |
| 260 | if (! printed_version) { |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 261 | fprintf(stderr, "%s version %s (build %s)\n", |
| 262 | PACKAGE_NAME, VERSION, BUILD); |
| 263 | fprintf(stderr, "%s\n\n", JCOPYRIGHT); |
| 264 | fprintf(stderr, "Emulating The Independent JPEG Group's software, version %s\n\n", |
| 265 | JVERSION); |
| 266 | printed_version = TRUE; |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 267 | } |
| 268 | cinfo->err->trace_level++; |
| 269 | |
DRC | 9665f5e | 2014-11-22 04:04:38 +0000 | [diff] [blame] | 270 | } else if (keymatch(arg, "version", 4)) { |
| 271 | fprintf(stderr, "%s version %s (build %s)\n", |
| 272 | PACKAGE_NAME, VERSION, BUILD); |
DRC | 306add8 | 2014-11-22 04:25:04 +0000 | [diff] [blame] | 273 | exit(EXIT_SUCCESS); |
DRC | 9665f5e | 2014-11-22 04:04:38 +0000 | [diff] [blame] | 274 | |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 275 | } else if (keymatch(arg, "fast", 1)) { |
| 276 | /* Select recommended processing options for quick-and-dirty output. */ |
| 277 | cinfo->two_pass_quantize = FALSE; |
| 278 | cinfo->dither_mode = JDITHER_ORDERED; |
| 279 | if (! cinfo->quantize_colors) /* don't override an earlier -colors */ |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 280 | cinfo->desired_number_of_colors = 216; |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 281 | cinfo->dct_method = JDCT_FASTEST; |
| 282 | cinfo->do_fancy_upsampling = FALSE; |
| 283 | |
| 284 | } else if (keymatch(arg, "gif", 1)) { |
| 285 | /* GIF output format. */ |
| 286 | requested_fmt = FMT_GIF; |
| 287 | |
| 288 | } else if (keymatch(arg, "grayscale", 2) || keymatch(arg, "greyscale",2)) { |
| 289 | /* Force monochrome output. */ |
| 290 | cinfo->out_color_space = JCS_GRAYSCALE; |
| 291 | |
DRC | 6a833a8 | 2011-03-03 16:58:47 +0000 | [diff] [blame] | 292 | } else if (keymatch(arg, "rgb", 2)) { |
| 293 | /* Force RGB output. */ |
| 294 | cinfo->out_color_space = JCS_RGB; |
| 295 | |
DRC | 78df2e6 | 2014-05-12 09:23:57 +0000 | [diff] [blame] | 296 | } else if (keymatch(arg, "rgb565", 2)) { |
| 297 | /* Force RGB565 output. */ |
| 298 | cinfo->out_color_space = JCS_RGB565; |
| 299 | |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 300 | } else if (keymatch(arg, "map", 3)) { |
| 301 | /* Quantize to a color map taken from an input file. */ |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 302 | if (++argn >= argc) /* advance to next argument */ |
| 303 | usage(); |
| 304 | if (for_real) { /* too expensive to do twice! */ |
| 305 | #ifdef QUANT_2PASS_SUPPORTED /* otherwise can't quantize to supplied map */ |
| 306 | FILE * mapfile; |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 307 | |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 308 | if ((mapfile = fopen(argv[argn], READ_BINARY)) == NULL) { |
| 309 | fprintf(stderr, "%s: can't open %s\n", progname, argv[argn]); |
| 310 | exit(EXIT_FAILURE); |
| 311 | } |
| 312 | read_color_map(cinfo, mapfile); |
| 313 | fclose(mapfile); |
| 314 | cinfo->quantize_colors = TRUE; |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 315 | #else |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 316 | ERREXIT(cinfo, JERR_NOT_COMPILED); |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 317 | #endif |
| 318 | } |
| 319 | |
| 320 | } else if (keymatch(arg, "maxmemory", 3)) { |
| 321 | /* Maximum memory in Kb (or Mb with 'm'). */ |
| 322 | long lval; |
| 323 | char ch = 'x'; |
| 324 | |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 325 | if (++argn >= argc) /* advance to next argument */ |
| 326 | usage(); |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 327 | if (sscanf(argv[argn], "%ld%c", &lval, &ch) < 1) |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 328 | usage(); |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 329 | if (ch == 'm' || ch == 'M') |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 330 | lval *= 1000L; |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 331 | cinfo->mem->max_memory_to_use = lval * 1000L; |
| 332 | |
| 333 | } else if (keymatch(arg, "nosmooth", 3)) { |
| 334 | /* Suppress fancy upsampling */ |
| 335 | cinfo->do_fancy_upsampling = FALSE; |
| 336 | |
| 337 | } else if (keymatch(arg, "onepass", 3)) { |
| 338 | /* Use fast one-pass quantization. */ |
| 339 | cinfo->two_pass_quantize = FALSE; |
| 340 | |
| 341 | } else if (keymatch(arg, "os2", 3)) { |
| 342 | /* BMP output format (OS/2 flavor). */ |
| 343 | requested_fmt = FMT_OS2; |
| 344 | |
| 345 | } else if (keymatch(arg, "outfile", 4)) { |
| 346 | /* Set output file name. */ |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 347 | if (++argn >= argc) /* advance to next argument */ |
| 348 | usage(); |
| 349 | outfilename = argv[argn]; /* save it away for later use */ |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 350 | |
DRC | ab70623 | 2013-01-18 23:42:31 +0000 | [diff] [blame] | 351 | } else if (keymatch(arg, "memsrc", 2)) { |
| 352 | /* Use in-memory source manager */ |
| 353 | #if JPEG_LIB_VERSION >= 80 || defined(MEM_SRCDST_SUPPORTED) |
| 354 | memsrc = TRUE; |
| 355 | #else |
| 356 | fprintf(stderr, "%s: sorry, in-memory source manager was not compiled in\n", |
| 357 | progname); |
| 358 | exit(EXIT_FAILURE); |
| 359 | #endif |
| 360 | |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 361 | } else if (keymatch(arg, "pnm", 1) || keymatch(arg, "ppm", 1)) { |
| 362 | /* PPM/PGM output format. */ |
| 363 | requested_fmt = FMT_PPM; |
| 364 | |
| 365 | } else if (keymatch(arg, "rle", 1)) { |
| 366 | /* RLE output format. */ |
| 367 | requested_fmt = FMT_RLE; |
| 368 | |
DRC | eb32cc1 | 2015-06-25 03:44:36 +0000 | [diff] [blame^] | 369 | } else if (keymatch(arg, "scale", 2)) { |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 370 | /* Scale the output image by a fraction M/N. */ |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 371 | if (++argn >= argc) /* advance to next argument */ |
| 372 | usage(); |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 373 | if (sscanf(argv[argn], "%d/%d", |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 374 | &cinfo->scale_num, &cinfo->scale_denom) != 2) |
| 375 | usage(); |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 376 | |
DRC | eb32cc1 | 2015-06-25 03:44:36 +0000 | [diff] [blame^] | 377 | } else if (keymatch(arg, "stripe", 2)) { |
| 378 | if (++argn >= argc) |
| 379 | usage(); |
| 380 | if (sscanf(argv[argn], "%d,%d", &startY, &endY) != 2 || startY > endY) |
| 381 | usage(); |
| 382 | stripe = TRUE; |
| 383 | |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 384 | } else if (keymatch(arg, "targa", 1)) { |
| 385 | /* Targa output format. */ |
| 386 | requested_fmt = FMT_TARGA; |
| 387 | |
| 388 | } else { |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 389 | usage(); /* bogus switch */ |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 390 | } |
| 391 | } |
| 392 | |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 393 | return argn; /* return index of next arg (file name) */ |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 394 | } |
| 395 | |
| 396 | |
| 397 | /* |
Thomas G. Lane | 5ead57a | 1998-03-27 00:00:00 +0000 | [diff] [blame] | 398 | * Marker processor for COM and interesting APPn markers. |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 399 | * 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] | 400 | * 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] | 401 | * Note this code relies on a non-suspending data source. |
| 402 | */ |
| 403 | |
Thomas G. Lane | 489583f | 1996-02-07 00:00:00 +0000 | [diff] [blame] | 404 | LOCAL(unsigned int) |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 405 | jpeg_getc (j_decompress_ptr cinfo) |
| 406 | /* Read next byte */ |
| 407 | { |
| 408 | struct jpeg_source_mgr * datasrc = cinfo->src; |
| 409 | |
| 410 | if (datasrc->bytes_in_buffer == 0) { |
| 411 | if (! (*datasrc->fill_input_buffer) (cinfo)) |
| 412 | ERREXIT(cinfo, JERR_CANT_SUSPEND); |
| 413 | } |
| 414 | datasrc->bytes_in_buffer--; |
| 415 | return GETJOCTET(*datasrc->next_input_byte++); |
| 416 | } |
| 417 | |
| 418 | |
Thomas G. Lane | 489583f | 1996-02-07 00:00:00 +0000 | [diff] [blame] | 419 | METHODDEF(boolean) |
Thomas G. Lane | 5ead57a | 1998-03-27 00:00:00 +0000 | [diff] [blame] | 420 | print_text_marker (j_decompress_ptr cinfo) |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 421 | { |
| 422 | boolean traceit = (cinfo->err->trace_level >= 1); |
| 423 | INT32 length; |
| 424 | unsigned int ch; |
| 425 | unsigned int lastch = 0; |
| 426 | |
| 427 | length = jpeg_getc(cinfo) << 8; |
| 428 | length += jpeg_getc(cinfo); |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 429 | length -= 2; /* discount the length word itself */ |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 430 | |
Thomas G. Lane | 5ead57a | 1998-03-27 00:00:00 +0000 | [diff] [blame] | 431 | if (traceit) { |
| 432 | if (cinfo->unread_marker == JPEG_COM) |
| 433 | fprintf(stderr, "Comment, length %ld:\n", (long) length); |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 434 | else /* assume it is an APPn otherwise */ |
Thomas G. Lane | 5ead57a | 1998-03-27 00:00:00 +0000 | [diff] [blame] | 435 | fprintf(stderr, "APP%d, length %ld:\n", |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 436 | cinfo->unread_marker - JPEG_APP0, (long) length); |
Thomas G. Lane | 5ead57a | 1998-03-27 00:00:00 +0000 | [diff] [blame] | 437 | } |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 438 | |
| 439 | while (--length >= 0) { |
| 440 | ch = jpeg_getc(cinfo); |
| 441 | if (traceit) { |
| 442 | /* Emit the character in a readable form. |
| 443 | * Nonprintables are converted to \nnn form, |
| 444 | * while \ is converted to \\. |
| 445 | * Newlines in CR, CR/LF, or LF form will be printed as one newline. |
| 446 | */ |
| 447 | if (ch == '\r') { |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 448 | fprintf(stderr, "\n"); |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 449 | } else if (ch == '\n') { |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 450 | if (lastch != '\r') |
| 451 | fprintf(stderr, "\n"); |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 452 | } else if (ch == '\\') { |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 453 | fprintf(stderr, "\\\\"); |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 454 | } else if (isprint(ch)) { |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 455 | putc(ch, stderr); |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 456 | } else { |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 457 | fprintf(stderr, "\\%03o", ch); |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 458 | } |
| 459 | lastch = ch; |
| 460 | } |
| 461 | } |
| 462 | |
| 463 | if (traceit) |
| 464 | fprintf(stderr, "\n"); |
| 465 | |
| 466 | return TRUE; |
| 467 | } |
| 468 | |
| 469 | |
| 470 | /* |
| 471 | * The main program. |
| 472 | */ |
| 473 | |
Thomas G. Lane | 489583f | 1996-02-07 00:00:00 +0000 | [diff] [blame] | 474 | int |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 475 | main (int argc, char **argv) |
| 476 | { |
| 477 | struct jpeg_decompress_struct cinfo; |
| 478 | struct jpeg_error_mgr jerr; |
| 479 | #ifdef PROGRESS_REPORT |
| 480 | struct cdjpeg_progress_mgr progress; |
| 481 | #endif |
| 482 | int file_index; |
| 483 | djpeg_dest_ptr dest_mgr = NULL; |
| 484 | FILE * input_file; |
| 485 | FILE * output_file; |
DRC | ab70623 | 2013-01-18 23:42:31 +0000 | [diff] [blame] | 486 | unsigned char *inbuffer = NULL; |
| 487 | unsigned long insize = 0; |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 488 | JDIMENSION num_scanlines; |
| 489 | |
| 490 | /* On Mac, fetch a command line. */ |
| 491 | #ifdef USE_CCOMMAND |
| 492 | argc = ccommand(&argv); |
| 493 | #endif |
| 494 | |
| 495 | progname = argv[0]; |
| 496 | if (progname == NULL || progname[0] == 0) |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 497 | progname = "djpeg"; /* in case C library doesn't provide it */ |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 498 | |
| 499 | /* Initialize the JPEG decompression object with default error handling. */ |
| 500 | cinfo.err = jpeg_std_error(&jerr); |
| 501 | jpeg_create_decompress(&cinfo); |
| 502 | /* Add some application-specific error messages (from cderror.h) */ |
Thomas G. Lane | 9ba2f5e | 1994-12-07 00:00:00 +0000 | [diff] [blame] | 503 | jerr.addon_message_table = cdjpeg_message_table; |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 504 | jerr.first_addon_message = JMSG_FIRSTADDONCODE; |
| 505 | jerr.last_addon_message = JMSG_LASTADDONCODE; |
Thomas G. Lane | 5ead57a | 1998-03-27 00:00:00 +0000 | [diff] [blame] | 506 | |
| 507 | /* Insert custom marker processor for COM and APP12. |
| 508 | * APP12 is used by some digital camera makers for textual info, |
| 509 | * so we provide the ability to display it as text. |
| 510 | * If you like, additional APPn marker types can be selected for display, |
DRC | 39ea562 | 2010-10-12 01:55:31 +0000 | [diff] [blame] | 511 | * 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] | 512 | */ |
| 513 | jpeg_set_marker_processor(&cinfo, JPEG_COM, print_text_marker); |
| 514 | jpeg_set_marker_processor(&cinfo, JPEG_APP0+12, print_text_marker); |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 515 | |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 516 | /* Scan command line to find file names. */ |
| 517 | /* It is convenient to use just one switch-parsing routine, but the switch |
| 518 | * values read here are ignored; we will rescan the switches after opening |
| 519 | * the input file. |
| 520 | * (Exception: tracing level set here controls verbosity for COM markers |
| 521 | * found during jpeg_read_header...) |
| 522 | */ |
| 523 | |
| 524 | file_index = parse_switches(&cinfo, argc, argv, 0, FALSE); |
| 525 | |
| 526 | #ifdef TWO_FILE_COMMANDLINE |
| 527 | /* Must have either -outfile switch or explicit output file name */ |
| 528 | if (outfilename == NULL) { |
| 529 | if (file_index != argc-2) { |
| 530 | fprintf(stderr, "%s: must name one input and one output file\n", |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 531 | progname); |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 532 | usage(); |
| 533 | } |
| 534 | outfilename = argv[file_index+1]; |
| 535 | } else { |
| 536 | if (file_index != argc-1) { |
| 537 | fprintf(stderr, "%s: must name one input and one output file\n", |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 538 | progname); |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 539 | usage(); |
| 540 | } |
| 541 | } |
| 542 | #else |
| 543 | /* Unix style: expect zero or one file name */ |
| 544 | if (file_index < argc-1) { |
| 545 | fprintf(stderr, "%s: only one input file\n", progname); |
| 546 | usage(); |
| 547 | } |
| 548 | #endif /* TWO_FILE_COMMANDLINE */ |
| 549 | |
| 550 | /* Open the input file. */ |
| 551 | if (file_index < argc) { |
| 552 | if ((input_file = fopen(argv[file_index], READ_BINARY)) == NULL) { |
| 553 | fprintf(stderr, "%s: can't open %s\n", progname, argv[file_index]); |
| 554 | exit(EXIT_FAILURE); |
| 555 | } |
| 556 | } else { |
| 557 | /* default input file is stdin */ |
Thomas G. Lane | bc79e06 | 1995-08-02 00:00:00 +0000 | [diff] [blame] | 558 | input_file = read_stdin(); |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 559 | } |
| 560 | |
| 561 | /* Open the output file. */ |
| 562 | if (outfilename != NULL) { |
| 563 | if ((output_file = fopen(outfilename, WRITE_BINARY)) == NULL) { |
| 564 | fprintf(stderr, "%s: can't open %s\n", progname, outfilename); |
| 565 | exit(EXIT_FAILURE); |
| 566 | } |
| 567 | } else { |
| 568 | /* default output file is stdout */ |
Thomas G. Lane | bc79e06 | 1995-08-02 00:00:00 +0000 | [diff] [blame] | 569 | output_file = write_stdout(); |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 570 | } |
| 571 | |
| 572 | #ifdef PROGRESS_REPORT |
Thomas G. Lane | bc79e06 | 1995-08-02 00:00:00 +0000 | [diff] [blame] | 573 | start_progress_monitor((j_common_ptr) &cinfo, &progress); |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 574 | #endif |
| 575 | |
| 576 | /* Specify data source for decompression */ |
DRC | ab70623 | 2013-01-18 23:42:31 +0000 | [diff] [blame] | 577 | #if JPEG_LIB_VERSION >= 80 || defined(MEM_SRCDST_SUPPORTED) |
| 578 | if (memsrc) { |
| 579 | size_t nbytes; |
| 580 | do { |
| 581 | inbuffer = (unsigned char *)realloc(inbuffer, insize + INPUT_BUF_SIZE); |
| 582 | if (inbuffer == NULL) { |
| 583 | fprintf(stderr, "%s: memory allocation failure\n", progname); |
| 584 | exit(EXIT_FAILURE); |
| 585 | } |
| 586 | nbytes = JFREAD(input_file, &inbuffer[insize], INPUT_BUF_SIZE); |
DRC | c45653e | 2013-10-12 21:52:48 +0000 | [diff] [blame] | 587 | if (nbytes < INPUT_BUF_SIZE && ferror(input_file)) { |
DRC | ab70623 | 2013-01-18 23:42:31 +0000 | [diff] [blame] | 588 | if (file_index < argc) |
| 589 | fprintf(stderr, "%s: can't read from %s\n", progname, |
| 590 | argv[file_index]); |
| 591 | else |
| 592 | fprintf(stderr, "%s: can't read from stdin\n", progname); |
| 593 | } |
DRC | 736fb06 | 2013-01-18 23:45:06 +0000 | [diff] [blame] | 594 | insize += (unsigned long)nbytes; |
DRC | ab70623 | 2013-01-18 23:42:31 +0000 | [diff] [blame] | 595 | } while (nbytes == INPUT_BUF_SIZE); |
| 596 | fprintf(stderr, "Compressed size: %lu bytes\n", insize); |
| 597 | jpeg_mem_src(&cinfo, inbuffer, insize); |
| 598 | } else |
| 599 | #endif |
| 600 | jpeg_stdio_src(&cinfo, input_file); |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 601 | |
| 602 | /* Read file header, set default decompression parameters */ |
| 603 | (void) jpeg_read_header(&cinfo, TRUE); |
| 604 | |
| 605 | /* Adjust default decompression parameters by re-parsing the options */ |
| 606 | file_index = parse_switches(&cinfo, argc, argv, 0, TRUE); |
| 607 | |
| 608 | /* Initialize the output module now to let it override any crucial |
| 609 | * option settings (for instance, GIF wants to force color quantization). |
| 610 | */ |
| 611 | switch (requested_fmt) { |
| 612 | #ifdef BMP_SUPPORTED |
| 613 | case FMT_BMP: |
| 614 | dest_mgr = jinit_write_bmp(&cinfo, FALSE); |
| 615 | break; |
| 616 | case FMT_OS2: |
| 617 | dest_mgr = jinit_write_bmp(&cinfo, TRUE); |
| 618 | break; |
| 619 | #endif |
| 620 | #ifdef GIF_SUPPORTED |
| 621 | case FMT_GIF: |
| 622 | dest_mgr = jinit_write_gif(&cinfo); |
| 623 | break; |
| 624 | #endif |
| 625 | #ifdef PPM_SUPPORTED |
| 626 | case FMT_PPM: |
| 627 | dest_mgr = jinit_write_ppm(&cinfo); |
| 628 | break; |
| 629 | #endif |
| 630 | #ifdef RLE_SUPPORTED |
| 631 | case FMT_RLE: |
| 632 | dest_mgr = jinit_write_rle(&cinfo); |
| 633 | break; |
| 634 | #endif |
| 635 | #ifdef TARGA_SUPPORTED |
| 636 | case FMT_TARGA: |
| 637 | dest_mgr = jinit_write_targa(&cinfo); |
| 638 | break; |
| 639 | #endif |
| 640 | default: |
| 641 | ERREXIT(&cinfo, JERR_UNSUPPORTED_FORMAT); |
| 642 | break; |
| 643 | } |
| 644 | dest_mgr->output_file = output_file; |
| 645 | |
| 646 | /* Start decompressor */ |
Thomas G. Lane | bc79e06 | 1995-08-02 00:00:00 +0000 | [diff] [blame] | 647 | (void) jpeg_start_decompress(&cinfo); |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 648 | |
DRC | eb32cc1 | 2015-06-25 03:44:36 +0000 | [diff] [blame^] | 649 | /* Stripe decode */ |
| 650 | if (stripe) { |
| 651 | JDIMENSION tmp; |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 652 | |
DRC | eb32cc1 | 2015-06-25 03:44:36 +0000 | [diff] [blame^] | 653 | /* Check for valid endY. We cannot check this value until after |
| 654 | * jpeg_start_decompress() is called. Note that we have already verified |
| 655 | * that startY <= endY. |
| 656 | */ |
| 657 | if (endY > cinfo.output_height) { |
| 658 | fprintf(stderr, "%s: stripe %d-%d exceeds image height %d\n", progname, |
| 659 | startY, endY, cinfo.output_height); |
| 660 | exit(EXIT_FAILURE); |
| 661 | } |
| 662 | |
| 663 | /* Write output file header. This is a hack to ensure that the destination |
| 664 | * manager creates an image of the proper size for the partial decode. |
| 665 | */ |
| 666 | tmp = cinfo.output_height; |
| 667 | cinfo.output_height = endY - startY; |
| 668 | (*dest_mgr->start_output) (&cinfo, dest_mgr); |
| 669 | cinfo.output_height = tmp; |
| 670 | |
| 671 | /* Process data */ |
| 672 | (void) jpeg_skip_scanlines(&cinfo, startY); |
| 673 | while (cinfo.output_scanline < endY) { |
| 674 | num_scanlines = jpeg_read_scanlines(&cinfo, dest_mgr->buffer, |
| 675 | dest_mgr->buffer_height); |
| 676 | (*dest_mgr->put_pixel_rows) (&cinfo, dest_mgr, num_scanlines); |
| 677 | } |
| 678 | (void) jpeg_skip_scanlines(&cinfo, cinfo.output_height - endY); |
| 679 | |
| 680 | /* Normal full image decode */ |
| 681 | } else { |
| 682 | /* Write output file header */ |
| 683 | (*dest_mgr->start_output) (&cinfo, dest_mgr); |
| 684 | |
| 685 | /* Process data */ |
| 686 | while (cinfo.output_scanline < cinfo.output_height) { |
| 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 | } |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 691 | } |
| 692 | |
| 693 | #ifdef PROGRESS_REPORT |
| 694 | /* Hack: count final pass as done in case finish_output does an extra pass. |
| 695 | * The library won't have updated completed_passes. |
| 696 | */ |
| 697 | progress.pub.completed_passes = progress.pub.total_passes; |
| 698 | #endif |
| 699 | |
| 700 | /* Finish decompression and release memory. |
| 701 | * I must do it in this order because output module has allocated memory |
| 702 | * of lifespan JPOOL_IMAGE; it needs to finish before releasing memory. |
| 703 | */ |
| 704 | (*dest_mgr->finish_output) (&cinfo, dest_mgr); |
Thomas G. Lane | bc79e06 | 1995-08-02 00:00:00 +0000 | [diff] [blame] | 705 | (void) jpeg_finish_decompress(&cinfo); |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 706 | jpeg_destroy_decompress(&cinfo); |
| 707 | |
Thomas G. Lane | 9ba2f5e | 1994-12-07 00:00:00 +0000 | [diff] [blame] | 708 | /* Close files, if we opened them */ |
| 709 | if (input_file != stdin) |
| 710 | fclose(input_file); |
| 711 | if (output_file != stdout) |
| 712 | fclose(output_file); |
| 713 | |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 714 | #ifdef PROGRESS_REPORT |
Thomas G. Lane | bc79e06 | 1995-08-02 00:00:00 +0000 | [diff] [blame] | 715 | end_progress_monitor((j_common_ptr) &cinfo); |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 716 | #endif |
| 717 | |
DRC | ab70623 | 2013-01-18 23:42:31 +0000 | [diff] [blame] | 718 | if (memsrc && inbuffer != NULL) |
| 719 | free(inbuffer); |
| 720 | |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 721 | /* All done. */ |
| 722 | exit(jerr.num_warnings ? EXIT_WARNING : EXIT_SUCCESS); |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 723 | return 0; /* suppress no-return-value warnings */ |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 724 | } |