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