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