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