blob: acacfcb9fac34b5d069bd705e073029a508693fe [file] [log] [blame]
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +00001/*
2 * djpeg.c
3 *
noel@chromium.org3395bcc2014-04-14 06:56:00 +00004 * This file was part of the Independent JPEG Group's software:
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +00005 * Copyright (C) 1991-1997, Thomas G. Lane.
Tom Hudson0d47d2d2016-05-04 13:22:56 -04006 * Modified 2013 by Guido Vollbeding.
noel@chromium.org3395bcc2014-04-14 06:56:00 +00007 * libjpeg-turbo Modifications:
Chris Blumecca8c4d2019-03-01 01:09:50 -08008 * Copyright (C) 2010-2011, 2013-2017, D. R. Commander.
Aaron Gablec9c87552015-08-03 09:34:32 -07009 * Copyright (C) 2015, Google, Inc.
Tom Hudson0d47d2d2016-05-04 13:22:56 -040010 * For conditions of distribution and use, see the accompanying README.ijg
11 * file.
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +000012 *
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 Hudson0d47d2d2016-05-04 13:22:56 -040018 * djpeg [options] inputfile outputfile
19 * djpeg [options] [inputfile]
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +000020 * 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 Hudson0d47d2d2016-05-04 13:22:56 -040027 * djpeg [options] -outfile outputfile inputfile
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +000028 * works regardless of which command line style is used.
29 */
30
Tom Hudson0d47d2d2016-05-04 13:22:56 -040031#include "cdjpeg.h" /* Common decls for cjpeg/djpeg applications */
32#include "jversion.h" /* for version message */
33#include "jconfigint.h"
Chris Blumecca8c4d2019-03-01 01:09:50 -080034
35#ifndef HAVE_STDLIB_H /* <stdlib.h> should declare free() */
36extern void free(void *ptr);
37#endif
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +000038
Tom Hudson0d47d2d2016-05-04 13:22:56 -040039#include <ctype.h> /* to declare isprint() */
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +000040
Tom Hudson0d47d2d2016-05-04 13:22:56 -040041#ifdef USE_CCOMMAND /* command-line reader for Macintosh */
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +000042#ifdef __MWERKS__
43#include <SIOUX.h> /* Metrowerks needs this */
Tom Hudson0d47d2d2016-05-04 13:22:56 -040044#include <console.h> /* ... and this */
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +000045#endif
46#ifdef THINK_C
Tom Hudson0d47d2d2016-05-04 13:22:56 -040047#include <console.h> /* Think declares it here */
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +000048#endif
49#endif
50
51
52/* Create the add-on message string table. */
53
Chris Blumecca8c4d2019-03-01 01:09:50 -080054#define JMESSAGE(code, string) string,
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +000055
56static const char * const cdjpeg_message_table[] = {
57#include "cderror.h"
58 NULL
59};
60
61
62/*
63 * This list defines the known output image formats
64 * (not all of which need be supported by a given version).
65 * You can change the default output format by defining DEFAULT_FMT;
66 * indeed, you had better do so if you undefine PPM_SUPPORTED.
67 */
68
69typedef enum {
Chris Blumecca8c4d2019-03-01 01:09:50 -080070 FMT_BMP, /* BMP format (Windows flavor) */
71 FMT_GIF, /* GIF format */
72 FMT_OS2, /* BMP format (OS/2 flavor) */
73 FMT_PPM, /* PPM/PGM (PBMPLUS formats) */
74 FMT_RLE, /* RLE format */
75 FMT_TARGA, /* Targa format */
76 FMT_TIFF /* TIFF format */
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +000077} IMAGE_FORMATS;
78
Tom Hudson0d47d2d2016-05-04 13:22:56 -040079#ifndef DEFAULT_FMT /* so can override from CFLAGS in Makefile */
80#define DEFAULT_FMT FMT_PPM
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +000081#endif
82
83static IMAGE_FORMATS requested_fmt;
84
85
86/*
87 * Argument-parsing code.
88 * The switch parser is designed to be useful with DOS-style command line
89 * syntax, ie, intermixed switches and file names, where only the switches
90 * to the left of a given file name affect processing of that file.
91 * The main program in this file doesn't actually use this capability...
92 */
93
94
Tom Hudson0d47d2d2016-05-04 13:22:56 -040095static const char *progname; /* program name for error messages */
Chris Blumecca8c4d2019-03-01 01:09:50 -080096static char *icc_filename; /* for -icc switch */
Tom Hudson0d47d2d2016-05-04 13:22:56 -040097static char *outfilename; /* for -outfile switch */
98boolean memsrc; /* for -memsrc switch */
99boolean skip, crop;
100JDIMENSION skip_start, skip_end;
101JDIMENSION crop_x, crop_y, crop_width, crop_height;
noel@chromium.org3395bcc2014-04-14 06:56:00 +0000102#define INPUT_BUF_SIZE 4096
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000103
104
105LOCAL(void)
Chris Blumecca8c4d2019-03-01 01:09:50 -0800106usage(void)
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000107/* complain about bad command line */
108{
109 fprintf(stderr, "usage: %s [switches] ", progname);
110#ifdef TWO_FILE_COMMANDLINE
111 fprintf(stderr, "inputfile outputfile\n");
112#else
113 fprintf(stderr, "[inputfile]\n");
114#endif
115
116 fprintf(stderr, "Switches (names may be abbreviated):\n");
117 fprintf(stderr, " -colors N Reduce image to no more than N colors\n");
118 fprintf(stderr, " -fast Fast, low-quality processing\n");
119 fprintf(stderr, " -grayscale Force grayscale output\n");
hbono@chromium.org98626972011-08-03 03:13:08 +0000120 fprintf(stderr, " -rgb Force RGB output\n");
Aaron Gablefeec46f2015-08-06 09:54:48 -0700121 fprintf(stderr, " -rgb565 Force RGB565 output\n");
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000122#ifdef IDCT_SCALING_SUPPORTED
123 fprintf(stderr, " -scale M/N Scale output image by fraction M/N, eg, 1/8\n");
124#endif
125#ifdef BMP_SUPPORTED
126 fprintf(stderr, " -bmp Select BMP output format (Windows style)%s\n",
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400127 (DEFAULT_FMT == FMT_BMP ? " (default)" : ""));
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000128#endif
129#ifdef GIF_SUPPORTED
130 fprintf(stderr, " -gif Select GIF output format%s\n",
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400131 (DEFAULT_FMT == FMT_GIF ? " (default)" : ""));
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000132#endif
133#ifdef BMP_SUPPORTED
134 fprintf(stderr, " -os2 Select BMP output format (OS/2 style)%s\n",
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400135 (DEFAULT_FMT == FMT_OS2 ? " (default)" : ""));
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000136#endif
137#ifdef PPM_SUPPORTED
138 fprintf(stderr, " -pnm Select PBMPLUS (PPM/PGM) output format%s\n",
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400139 (DEFAULT_FMT == FMT_PPM ? " (default)" : ""));
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000140#endif
141#ifdef RLE_SUPPORTED
142 fprintf(stderr, " -rle Select Utah RLE output format%s\n",
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400143 (DEFAULT_FMT == FMT_RLE ? " (default)" : ""));
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000144#endif
145#ifdef TARGA_SUPPORTED
146 fprintf(stderr, " -targa Select Targa output format%s\n",
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400147 (DEFAULT_FMT == FMT_TARGA ? " (default)" : ""));
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000148#endif
149 fprintf(stderr, "Switches for advanced users:\n");
150#ifdef DCT_ISLOW_SUPPORTED
151 fprintf(stderr, " -dct int Use integer DCT method%s\n",
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400152 (JDCT_DEFAULT == JDCT_ISLOW ? " (default)" : ""));
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000153#endif
154#ifdef DCT_IFAST_SUPPORTED
155 fprintf(stderr, " -dct fast Use fast integer DCT (less accurate)%s\n",
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400156 (JDCT_DEFAULT == JDCT_IFAST ? " (default)" : ""));
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000157#endif
158#ifdef DCT_FLOAT_SUPPORTED
159 fprintf(stderr, " -dct float Use floating-point DCT method%s\n",
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400160 (JDCT_DEFAULT == JDCT_FLOAT ? " (default)" : ""));
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000161#endif
162 fprintf(stderr, " -dither fs Use F-S dithering (default)\n");
163 fprintf(stderr, " -dither none Don't use dithering in quantization\n");
164 fprintf(stderr, " -dither ordered Use ordered dither (medium speed, quality)\n");
Chris Blumecca8c4d2019-03-01 01:09:50 -0800165 fprintf(stderr, " -icc FILE Extract ICC profile to FILE\n");
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000166#ifdef QUANT_2PASS_SUPPORTED
167 fprintf(stderr, " -map FILE Map to colors used in named image file\n");
168#endif
169 fprintf(stderr, " -nosmooth Don't use high-quality upsampling\n");
170#ifdef QUANT_1PASS_SUPPORTED
171 fprintf(stderr, " -onepass Use 1-pass quantization (fast, low quality)\n");
172#endif
173 fprintf(stderr, " -maxmemory N Maximum memory to use (in kbytes)\n");
174 fprintf(stderr, " -outfile name Specify name for output file\n");
noel@chromium.org3395bcc2014-04-14 06:56:00 +0000175#if JPEG_LIB_VERSION >= 80 || defined(MEM_SRCDST_SUPPORTED)
176 fprintf(stderr, " -memsrc Load input file into memory before decompressing\n");
177#endif
178
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400179 fprintf(stderr, " -skip Y0,Y1 Decompress all rows except those between Y0 and Y1 (inclusive)\n");
180 fprintf(stderr, " -crop WxH+X+Y Decompress only a rectangular subregion of the image\n");
Chris Blumecca8c4d2019-03-01 01:09:50 -0800181 fprintf(stderr, " [requires PBMPLUS (PPM/PGM), GIF, or Targa output format]\n");
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000182 fprintf(stderr, " -verbose or -debug Emit debug output\n");
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400183 fprintf(stderr, " -version Print version information and exit\n");
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000184 exit(EXIT_FAILURE);
185}
186
187
188LOCAL(int)
Chris Blumecca8c4d2019-03-01 01:09:50 -0800189parse_switches(j_decompress_ptr cinfo, int argc, char **argv,
190 int last_file_arg_seen, boolean for_real)
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000191/* Parse optional switches.
192 * Returns argv[] index of first file-name argument (== argc if none).
193 * Any file names with indexes <= last_file_arg_seen are ignored;
194 * they have presumably been processed in a previous iteration.
195 * (Pass 0 for last_file_arg_seen on the first or only iteration.)
196 * for_real is FALSE on the first (dummy) pass; we may skip any expensive
197 * processing.
198 */
199{
200 int argn;
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400201 char *arg;
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000202
203 /* Set up default JPEG parameters. */
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400204 requested_fmt = DEFAULT_FMT; /* set default output file format */
Chris Blumecca8c4d2019-03-01 01:09:50 -0800205 icc_filename = NULL;
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000206 outfilename = NULL;
noel@chromium.org3395bcc2014-04-14 06:56:00 +0000207 memsrc = FALSE;
Aaron Gablec9c87552015-08-03 09:34:32 -0700208 skip = FALSE;
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400209 crop = FALSE;
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000210 cinfo->err->trace_level = 0;
211
212 /* Scan command line options, adjust parameters */
213
214 for (argn = 1; argn < argc; argn++) {
215 arg = argv[argn];
216 if (*arg != '-') {
217 /* Not a switch, must be a file name argument */
218 if (argn <= last_file_arg_seen) {
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400219 outfilename = NULL; /* -outfile applies to just one input file */
220 continue; /* ignore this name if previously processed */
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000221 }
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400222 break; /* else done parsing switches */
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000223 }
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400224 arg++; /* advance past switch marker character */
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000225
226 if (keymatch(arg, "bmp", 1)) {
227 /* BMP output format. */
228 requested_fmt = FMT_BMP;
229
230 } else if (keymatch(arg, "colors", 1) || keymatch(arg, "colours", 1) ||
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400231 keymatch(arg, "quantize", 1) || keymatch(arg, "quantise", 1)) {
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000232 /* Do color quantization. */
233 int val;
234
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400235 if (++argn >= argc) /* advance to next argument */
236 usage();
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000237 if (sscanf(argv[argn], "%d", &val) != 1)
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400238 usage();
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000239 cinfo->desired_number_of_colors = val;
240 cinfo->quantize_colors = TRUE;
241
242 } else if (keymatch(arg, "dct", 2)) {
243 /* Select IDCT algorithm. */
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400244 if (++argn >= argc) /* advance to next argument */
245 usage();
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000246 if (keymatch(argv[argn], "int", 1)) {
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400247 cinfo->dct_method = JDCT_ISLOW;
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000248 } else if (keymatch(argv[argn], "fast", 2)) {
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400249 cinfo->dct_method = JDCT_IFAST;
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000250 } else if (keymatch(argv[argn], "float", 2)) {
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400251 cinfo->dct_method = JDCT_FLOAT;
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000252 } else
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400253 usage();
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000254
255 } else if (keymatch(arg, "dither", 2)) {
256 /* Select dithering algorithm. */
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400257 if (++argn >= argc) /* advance to next argument */
258 usage();
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000259 if (keymatch(argv[argn], "fs", 2)) {
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400260 cinfo->dither_mode = JDITHER_FS;
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000261 } else if (keymatch(argv[argn], "none", 2)) {
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400262 cinfo->dither_mode = JDITHER_NONE;
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000263 } else if (keymatch(argv[argn], "ordered", 2)) {
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400264 cinfo->dither_mode = JDITHER_ORDERED;
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000265 } else
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400266 usage();
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000267
268 } else if (keymatch(arg, "debug", 1) || keymatch(arg, "verbose", 1)) {
269 /* Enable debug printouts. */
270 /* On first -d, print version identification */
271 static boolean printed_version = FALSE;
272
Chris Blumecca8c4d2019-03-01 01:09:50 -0800273 if (!printed_version) {
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400274 fprintf(stderr, "%s version %s (build %s)\n",
275 PACKAGE_NAME, VERSION, BUILD);
276 fprintf(stderr, "%s\n\n", JCOPYRIGHT);
277 fprintf(stderr, "Emulating The Independent JPEG Group's software, version %s\n\n",
278 JVERSION);
279 printed_version = TRUE;
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000280 }
281 cinfo->err->trace_level++;
282
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400283 } else if (keymatch(arg, "version", 4)) {
284 fprintf(stderr, "%s version %s (build %s)\n",
285 PACKAGE_NAME, VERSION, BUILD);
286 exit(EXIT_SUCCESS);
287
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000288 } else if (keymatch(arg, "fast", 1)) {
289 /* Select recommended processing options for quick-and-dirty output. */
290 cinfo->two_pass_quantize = FALSE;
291 cinfo->dither_mode = JDITHER_ORDERED;
Chris Blumecca8c4d2019-03-01 01:09:50 -0800292 if (!cinfo->quantize_colors) /* don't override an earlier -colors */
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400293 cinfo->desired_number_of_colors = 216;
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000294 cinfo->dct_method = JDCT_FASTEST;
295 cinfo->do_fancy_upsampling = FALSE;
296
297 } else if (keymatch(arg, "gif", 1)) {
298 /* GIF output format. */
299 requested_fmt = FMT_GIF;
300
Chris Blumecca8c4d2019-03-01 01:09:50 -0800301 } else if (keymatch(arg, "grayscale", 2) ||
302 keymatch(arg, "greyscale", 2)) {
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000303 /* Force monochrome output. */
304 cinfo->out_color_space = JCS_GRAYSCALE;
305
hbono@chromium.org98626972011-08-03 03:13:08 +0000306 } else if (keymatch(arg, "rgb", 2)) {
307 /* Force RGB output. */
308 cinfo->out_color_space = JCS_RGB;
309
Aaron Gablefeec46f2015-08-06 09:54:48 -0700310 } else if (keymatch(arg, "rgb565", 2)) {
311 /* Force RGB565 output. */
312 cinfo->out_color_space = JCS_RGB565;
313
Chris Blumecca8c4d2019-03-01 01:09:50 -0800314 } 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.orgf0c4f332010-11-01 05:14:55 +0000321 } else if (keymatch(arg, "map", 3)) {
322 /* Quantize to a color map taken from an input file. */
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400323 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.orgf0c4f332010-11-01 05:14:55 +0000328
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400329 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.orgf0c4f332010-11-01 05:14:55 +0000336#else
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400337 ERREXIT(cinfo, JERR_NOT_COMPILED);
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000338#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 Hudson0d47d2d2016-05-04 13:22:56 -0400346 if (++argn >= argc) /* advance to next argument */
347 usage();
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000348 if (sscanf(argv[argn], "%ld%c", &lval, &ch) < 1)
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400349 usage();
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000350 if (ch == 'm' || ch == 'M')
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400351 lval *= 1000L;
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000352 cinfo->mem->max_memory_to_use = lval * 1000L;
353
354 } else if (keymatch(arg, "nosmooth", 3)) {
355 /* Suppress fancy upsampling */
356 cinfo->do_fancy_upsampling = FALSE;
357
358 } else if (keymatch(arg, "onepass", 3)) {
359 /* Use fast one-pass quantization. */
360 cinfo->two_pass_quantize = FALSE;
361
362 } else if (keymatch(arg, "os2", 3)) {
363 /* BMP output format (OS/2 flavor). */
364 requested_fmt = FMT_OS2;
365
366 } else if (keymatch(arg, "outfile", 4)) {
367 /* Set output file name. */
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400368 if (++argn >= argc) /* advance to next argument */
369 usage();
370 outfilename = argv[argn]; /* save it away for later use */
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000371
noel@chromium.org3395bcc2014-04-14 06:56:00 +0000372 } else if (keymatch(arg, "memsrc", 2)) {
373 /* Use in-memory source manager */
374#if JPEG_LIB_VERSION >= 80 || defined(MEM_SRCDST_SUPPORTED)
375 memsrc = TRUE;
376#else
377 fprintf(stderr, "%s: sorry, in-memory source manager was not compiled in\n",
378 progname);
379 exit(EXIT_FAILURE);
380#endif
381
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000382 } else if (keymatch(arg, "pnm", 1) || keymatch(arg, "ppm", 1)) {
383 /* PPM/PGM output format. */
384 requested_fmt = FMT_PPM;
385
386 } else if (keymatch(arg, "rle", 1)) {
387 /* RLE output format. */
388 requested_fmt = FMT_RLE;
389
Aaron Gablec9c87552015-08-03 09:34:32 -0700390 } else if (keymatch(arg, "scale", 2)) {
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000391 /* Scale the output image by a fraction M/N. */
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400392 if (++argn >= argc) /* advance to next argument */
Aaron Gablec9c87552015-08-03 09:34:32 -0700393 usage();
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400394 if (sscanf(argv[argn], "%u/%u",
395 &cinfo->scale_num, &cinfo->scale_denom) != 2)
Aaron Gablec9c87552015-08-03 09:34:32 -0700396 usage();
Aaron Gablec9c87552015-08-03 09:34:32 -0700397
398 } else if (keymatch(arg, "skip", 2)) {
399 if (++argn >= argc)
400 usage();
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400401 if (sscanf(argv[argn], "%u,%u", &skip_start, &skip_end) != 2 ||
402 skip_start > skip_end)
Aaron Gablec9c87552015-08-03 09:34:32 -0700403 usage();
404 skip = TRUE;
405
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400406 } else if (keymatch(arg, "crop", 2)) {
407 char c;
408 if (++argn >= argc)
409 usage();
410 if (sscanf(argv[argn], "%u%c%u+%u+%u", &crop_width, &c, &crop_height,
411 &crop_x, &crop_y) != 5 ||
412 (c != 'X' && c != 'x') || crop_width < 1 || crop_height < 1)
413 usage();
414 crop = TRUE;
415
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000416 } else if (keymatch(arg, "targa", 1)) {
417 /* Targa output format. */
418 requested_fmt = FMT_TARGA;
419
420 } else {
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400421 usage(); /* bogus switch */
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000422 }
423 }
424
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400425 return argn; /* return index of next arg (file name) */
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000426}
427
428
429/*
430 * Marker processor for COM and interesting APPn markers.
431 * This replaces the library's built-in processor, which just skips the marker.
432 * We want to print out the marker as text, to the extent possible.
433 * Note this code relies on a non-suspending data source.
434 */
435
436LOCAL(unsigned int)
Chris Blumecca8c4d2019-03-01 01:09:50 -0800437jpeg_getc(j_decompress_ptr cinfo)
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000438/* Read next byte */
439{
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400440 struct jpeg_source_mgr *datasrc = cinfo->src;
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000441
442 if (datasrc->bytes_in_buffer == 0) {
Chris Blumecca8c4d2019-03-01 01:09:50 -0800443 if (!(*datasrc->fill_input_buffer) (cinfo))
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000444 ERREXIT(cinfo, JERR_CANT_SUSPEND);
445 }
446 datasrc->bytes_in_buffer--;
447 return GETJOCTET(*datasrc->next_input_byte++);
448}
449
450
451METHODDEF(boolean)
Chris Blumecca8c4d2019-03-01 01:09:50 -0800452print_text_marker(j_decompress_ptr cinfo)
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000453{
454 boolean traceit = (cinfo->err->trace_level >= 1);
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400455 long length;
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000456 unsigned int ch;
457 unsigned int lastch = 0;
458
459 length = jpeg_getc(cinfo) << 8;
460 length += jpeg_getc(cinfo);
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400461 length -= 2; /* discount the length word itself */
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000462
463 if (traceit) {
464 if (cinfo->unread_marker == JPEG_COM)
Chris Blumecca8c4d2019-03-01 01:09:50 -0800465 fprintf(stderr, "Comment, length %ld:\n", (long)length);
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400466 else /* assume it is an APPn otherwise */
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000467 fprintf(stderr, "APP%d, length %ld:\n",
Chris Blumecca8c4d2019-03-01 01:09:50 -0800468 cinfo->unread_marker - JPEG_APP0, (long)length);
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000469 }
470
471 while (--length >= 0) {
472 ch = jpeg_getc(cinfo);
473 if (traceit) {
474 /* Emit the character in a readable form.
475 * Nonprintables are converted to \nnn form,
476 * while \ is converted to \\.
477 * Newlines in CR, CR/LF, or LF form will be printed as one newline.
478 */
479 if (ch == '\r') {
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400480 fprintf(stderr, "\n");
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000481 } else if (ch == '\n') {
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400482 if (lastch != '\r')
483 fprintf(stderr, "\n");
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000484 } else if (ch == '\\') {
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400485 fprintf(stderr, "\\\\");
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000486 } else if (isprint(ch)) {
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400487 putc(ch, stderr);
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000488 } else {
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400489 fprintf(stderr, "\\%03o", ch);
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000490 }
491 lastch = ch;
492 }
493 }
494
495 if (traceit)
496 fprintf(stderr, "\n");
497
498 return TRUE;
499}
500
501
502/*
503 * The main program.
504 */
505
506int
Jonathan Wrightc69b17e2020-06-21 19:56:26 +0100507#ifdef GTEST
508djpeg(int argc, char **argv)
509#else
Chris Blumecca8c4d2019-03-01 01:09:50 -0800510main(int argc, char **argv)
Jonathan Wrightc69b17e2020-06-21 19:56:26 +0100511#endif
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000512{
513 struct jpeg_decompress_struct cinfo;
514 struct jpeg_error_mgr jerr;
515#ifdef PROGRESS_REPORT
516 struct cdjpeg_progress_mgr progress;
517#endif
518 int file_index;
519 djpeg_dest_ptr dest_mgr = NULL;
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400520 FILE *input_file;
521 FILE *output_file;
noel@chromium.org3395bcc2014-04-14 06:56:00 +0000522 unsigned char *inbuffer = NULL;
523 unsigned long insize = 0;
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000524 JDIMENSION num_scanlines;
525
526 /* On Mac, fetch a command line. */
527#ifdef USE_CCOMMAND
528 argc = ccommand(&argv);
529#endif
530
531 progname = argv[0];
532 if (progname == NULL || progname[0] == 0)
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400533 progname = "djpeg"; /* in case C library doesn't provide it */
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000534
535 /* Initialize the JPEG decompression object with default error handling. */
536 cinfo.err = jpeg_std_error(&jerr);
537 jpeg_create_decompress(&cinfo);
538 /* Add some application-specific error messages (from cderror.h) */
539 jerr.addon_message_table = cdjpeg_message_table;
540 jerr.first_addon_message = JMSG_FIRSTADDONCODE;
541 jerr.last_addon_message = JMSG_LASTADDONCODE;
542
543 /* Insert custom marker processor for COM and APP12.
544 * APP12 is used by some digital camera makers for textual info,
545 * so we provide the ability to display it as text.
546 * If you like, additional APPn marker types can be selected for display,
hbono@chromium.org98626972011-08-03 03:13:08 +0000547 * but don't try to override APP0 or APP14 this way (see libjpeg.txt).
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000548 */
549 jpeg_set_marker_processor(&cinfo, JPEG_COM, print_text_marker);
Chris Blumecca8c4d2019-03-01 01:09:50 -0800550 jpeg_set_marker_processor(&cinfo, JPEG_APP0 + 12, print_text_marker);
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000551
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000552 /* Scan command line to find file names. */
553 /* It is convenient to use just one switch-parsing routine, but the switch
554 * values read here are ignored; we will rescan the switches after opening
555 * the input file.
556 * (Exception: tracing level set here controls verbosity for COM markers
557 * found during jpeg_read_header...)
558 */
559
560 file_index = parse_switches(&cinfo, argc, argv, 0, FALSE);
561
562#ifdef TWO_FILE_COMMANDLINE
563 /* Must have either -outfile switch or explicit output file name */
564 if (outfilename == NULL) {
Chris Blumecca8c4d2019-03-01 01:09:50 -0800565 if (file_index != argc - 2) {
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000566 fprintf(stderr, "%s: must name one input and one output file\n",
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400567 progname);
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000568 usage();
569 }
Chris Blumecca8c4d2019-03-01 01:09:50 -0800570 outfilename = argv[file_index + 1];
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000571 } else {
Chris Blumecca8c4d2019-03-01 01:09:50 -0800572 if (file_index != argc - 1) {
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000573 fprintf(stderr, "%s: must name one input and one output file\n",
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400574 progname);
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000575 usage();
576 }
577 }
578#else
579 /* Unix style: expect zero or one file name */
Chris Blumecca8c4d2019-03-01 01:09:50 -0800580 if (file_index < argc - 1) {
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000581 fprintf(stderr, "%s: only one input file\n", progname);
582 usage();
583 }
584#endif /* TWO_FILE_COMMANDLINE */
585
586 /* Open the input file. */
587 if (file_index < argc) {
588 if ((input_file = fopen(argv[file_index], READ_BINARY)) == NULL) {
589 fprintf(stderr, "%s: can't open %s\n", progname, argv[file_index]);
Jonathan Wrightc69b17e2020-06-21 19:56:26 +0100590 return EXIT_FAILURE;
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000591 }
592 } else {
593 /* default input file is stdin */
594 input_file = read_stdin();
595 }
596
597 /* Open the output file. */
598 if (outfilename != NULL) {
599 if ((output_file = fopen(outfilename, WRITE_BINARY)) == NULL) {
600 fprintf(stderr, "%s: can't open %s\n", progname, outfilename);
Jonathan Wrightc69b17e2020-06-21 19:56:26 +0100601 return EXIT_FAILURE;
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000602 }
603 } else {
604 /* default output file is stdout */
605 output_file = write_stdout();
606 }
607
608#ifdef PROGRESS_REPORT
Chris Blumecca8c4d2019-03-01 01:09:50 -0800609 start_progress_monitor((j_common_ptr)&cinfo, &progress);
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000610#endif
611
612 /* Specify data source for decompression */
noel@chromium.org3395bcc2014-04-14 06:56:00 +0000613#if JPEG_LIB_VERSION >= 80 || defined(MEM_SRCDST_SUPPORTED)
614 if (memsrc) {
615 size_t nbytes;
616 do {
617 inbuffer = (unsigned char *)realloc(inbuffer, insize + INPUT_BUF_SIZE);
618 if (inbuffer == NULL) {
619 fprintf(stderr, "%s: memory allocation failure\n", progname);
Jonathan Wrightc69b17e2020-06-21 19:56:26 +0100620 return EXIT_FAILURE;
noel@chromium.org3395bcc2014-04-14 06:56:00 +0000621 }
622 nbytes = JFREAD(input_file, &inbuffer[insize], INPUT_BUF_SIZE);
623 if (nbytes < INPUT_BUF_SIZE && ferror(input_file)) {
624 if (file_index < argc)
625 fprintf(stderr, "%s: can't read from %s\n", progname,
626 argv[file_index]);
627 else
628 fprintf(stderr, "%s: can't read from stdin\n", progname);
629 }
630 insize += (unsigned long)nbytes;
631 } while (nbytes == INPUT_BUF_SIZE);
632 fprintf(stderr, "Compressed size: %lu bytes\n", insize);
633 jpeg_mem_src(&cinfo, inbuffer, insize);
634 } else
635#endif
636 jpeg_stdio_src(&cinfo, input_file);
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000637
638 /* Read file header, set default decompression parameters */
Chris Blumecca8c4d2019-03-01 01:09:50 -0800639 (void)jpeg_read_header(&cinfo, TRUE);
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000640
641 /* Adjust default decompression parameters by re-parsing the options */
642 file_index = parse_switches(&cinfo, argc, argv, 0, TRUE);
643
644 /* Initialize the output module now to let it override any crucial
645 * option settings (for instance, GIF wants to force color quantization).
646 */
647 switch (requested_fmt) {
648#ifdef BMP_SUPPORTED
649 case FMT_BMP:
Chris Blumecca8c4d2019-03-01 01:09:50 -0800650 dest_mgr = jinit_write_bmp(&cinfo, FALSE, TRUE);
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000651 break;
652 case FMT_OS2:
Chris Blumecca8c4d2019-03-01 01:09:50 -0800653 dest_mgr = jinit_write_bmp(&cinfo, TRUE, TRUE);
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000654 break;
655#endif
656#ifdef GIF_SUPPORTED
657 case FMT_GIF:
658 dest_mgr = jinit_write_gif(&cinfo);
659 break;
660#endif
661#ifdef PPM_SUPPORTED
662 case FMT_PPM:
663 dest_mgr = jinit_write_ppm(&cinfo);
664 break;
665#endif
666#ifdef RLE_SUPPORTED
667 case FMT_RLE:
668 dest_mgr = jinit_write_rle(&cinfo);
669 break;
670#endif
671#ifdef TARGA_SUPPORTED
672 case FMT_TARGA:
673 dest_mgr = jinit_write_targa(&cinfo);
674 break;
675#endif
676 default:
677 ERREXIT(&cinfo, JERR_UNSUPPORTED_FORMAT);
678 break;
679 }
680 dest_mgr->output_file = output_file;
681
682 /* Start decompressor */
Chris Blumecca8c4d2019-03-01 01:09:50 -0800683 (void)jpeg_start_decompress(&cinfo);
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000684
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400685 /* Skip rows */
686 if (skip) {
Aaron Gablec9c87552015-08-03 09:34:32 -0700687 JDIMENSION tmp;
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000688
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400689 /* Check for valid skip_end. We cannot check this value until after
Aaron Gablec9c87552015-08-03 09:34:32 -0700690 * jpeg_start_decompress() is called. Note that we have already verified
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400691 * that skip_start <= skip_end.
Aaron Gablec9c87552015-08-03 09:34:32 -0700692 */
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400693 if (skip_end > cinfo.output_height - 1) {
694 fprintf(stderr, "%s: skip region exceeds image height %d\n", progname,
695 cinfo.output_height);
Jonathan Wrightc69b17e2020-06-21 19:56:26 +0100696 return EXIT_FAILURE;
Aaron Gablec9c87552015-08-03 09:34:32 -0700697 }
698
699 /* Write output file header. This is a hack to ensure that the destination
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400700 * manager creates an output image of the proper size.
Aaron Gablec9c87552015-08-03 09:34:32 -0700701 */
702 tmp = cinfo.output_height;
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400703 cinfo.output_height -= (skip_end - skip_start + 1);
Aaron Gablec9c87552015-08-03 09:34:32 -0700704 (*dest_mgr->start_output) (&cinfo, dest_mgr);
705 cinfo.output_height = tmp;
706
707 /* Process data */
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400708 while (cinfo.output_scanline < skip_start) {
709 num_scanlines = jpeg_read_scanlines(&cinfo, dest_mgr->buffer,
710 dest_mgr->buffer_height);
711 (*dest_mgr->put_pixel_rows) (&cinfo, dest_mgr, num_scanlines);
712 }
713 jpeg_skip_scanlines(&cinfo, skip_end - skip_start + 1);
714 while (cinfo.output_scanline < cinfo.output_height) {
715 num_scanlines = jpeg_read_scanlines(&cinfo, dest_mgr->buffer,
716 dest_mgr->buffer_height);
717 (*dest_mgr->put_pixel_rows) (&cinfo, dest_mgr, num_scanlines);
Aaron Gablec9c87552015-08-03 09:34:32 -0700718 }
719
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400720 /* Decompress a subregion */
721 } else if (crop) {
722 JDIMENSION tmp;
723
724 /* Check for valid crop dimensions. We cannot check these values until
725 * after jpeg_start_decompress() is called.
726 */
727 if (crop_x + crop_width > cinfo.output_width ||
728 crop_y + crop_height > cinfo.output_height) {
729 fprintf(stderr, "%s: crop dimensions exceed image dimensions %d x %d\n",
730 progname, cinfo.output_width, cinfo.output_height);
Jonathan Wrightc69b17e2020-06-21 19:56:26 +0100731 return EXIT_FAILURE;
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400732 }
733
734 jpeg_crop_scanline(&cinfo, &crop_x, &crop_width);
Chris Blumecca8c4d2019-03-01 01:09:50 -0800735 if (dest_mgr->calc_buffer_dimensions)
736 (*dest_mgr->calc_buffer_dimensions) (&cinfo, dest_mgr);
737 else
738 ERREXIT(&cinfo, JERR_UNSUPPORTED_FORMAT);
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400739
740 /* Write output file header. This is a hack to ensure that the destination
741 * manager creates an output image of the proper size.
742 */
743 tmp = cinfo.output_height;
744 cinfo.output_height = crop_height;
745 (*dest_mgr->start_output) (&cinfo, dest_mgr);
746 cinfo.output_height = tmp;
747
748 /* Process data */
749 jpeg_skip_scanlines(&cinfo, crop_y);
750 while (cinfo.output_scanline < crop_y + crop_height) {
751 num_scanlines = jpeg_read_scanlines(&cinfo, dest_mgr->buffer,
752 dest_mgr->buffer_height);
753 (*dest_mgr->put_pixel_rows) (&cinfo, dest_mgr, num_scanlines);
754 }
755 jpeg_skip_scanlines(&cinfo, cinfo.output_height - crop_y - crop_height);
756
757 /* Normal full-image decompress */
Aaron Gablec9c87552015-08-03 09:34:32 -0700758 } else {
759 /* Write output file header */
760 (*dest_mgr->start_output) (&cinfo, dest_mgr);
761
762 /* Process data */
763 while (cinfo.output_scanline < cinfo.output_height) {
764 num_scanlines = jpeg_read_scanlines(&cinfo, dest_mgr->buffer,
765 dest_mgr->buffer_height);
766 (*dest_mgr->put_pixel_rows) (&cinfo, dest_mgr, num_scanlines);
767 }
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000768 }
769
770#ifdef PROGRESS_REPORT
771 /* Hack: count final pass as done in case finish_output does an extra pass.
772 * The library won't have updated completed_passes.
773 */
774 progress.pub.completed_passes = progress.pub.total_passes;
775#endif
776
Chris Blumecca8c4d2019-03-01 01:09:50 -0800777 if (icc_filename != NULL) {
778 FILE *icc_file;
779 JOCTET *icc_profile;
780 unsigned int icc_len;
781
782 if ((icc_file = fopen(icc_filename, WRITE_BINARY)) == NULL) {
783 fprintf(stderr, "%s: can't open %s\n", progname, icc_filename);
Jonathan Wrightc69b17e2020-06-21 19:56:26 +0100784 return EXIT_FAILURE;
Chris Blumecca8c4d2019-03-01 01:09:50 -0800785 }
786 if (jpeg_read_icc_profile(&cinfo, &icc_profile, &icc_len)) {
787 if (fwrite(icc_profile, icc_len, 1, icc_file) < 1) {
788 fprintf(stderr, "%s: can't read ICC profile from %s\n", progname,
789 icc_filename);
790 free(icc_profile);
791 fclose(icc_file);
Jonathan Wrightc69b17e2020-06-21 19:56:26 +0100792 return EXIT_FAILURE;
Chris Blumecca8c4d2019-03-01 01:09:50 -0800793 }
794 free(icc_profile);
795 fclose(icc_file);
796 } else if (cinfo.err->msg_code != JWRN_BOGUS_ICC)
797 fprintf(stderr, "%s: no ICC profile data in JPEG file\n", progname);
798 }
799
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000800 /* Finish decompression and release memory.
801 * I must do it in this order because output module has allocated memory
802 * of lifespan JPOOL_IMAGE; it needs to finish before releasing memory.
803 */
804 (*dest_mgr->finish_output) (&cinfo, dest_mgr);
Chris Blumecca8c4d2019-03-01 01:09:50 -0800805 (void)jpeg_finish_decompress(&cinfo);
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000806 jpeg_destroy_decompress(&cinfo);
807
808 /* Close files, if we opened them */
809 if (input_file != stdin)
810 fclose(input_file);
811 if (output_file != stdout)
812 fclose(output_file);
813
814#ifdef PROGRESS_REPORT
Chris Blumecca8c4d2019-03-01 01:09:50 -0800815 end_progress_monitor((j_common_ptr)&cinfo);
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000816#endif
817
noel@chromium.org3395bcc2014-04-14 06:56:00 +0000818 if (memsrc && inbuffer != NULL)
819 free(inbuffer);
820
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000821 /* All done. */
Jonathan Wrightc69b17e2020-06-21 19:56:26 +0100822 return (jerr.num_warnings ? EXIT_WARNING : EXIT_SUCCESS);
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000823}