blob: 66ac28f39e26a8fa554da0a8e570ccd2fd886fe7 [file] [log] [blame]
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +00001/*
2 * cjpeg.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-1998, Thomas G. Lane.
noel@chromium.org3395bcc2014-04-14 06:56:00 +00006 * Modified 2003-2011 by Guido Vollbeding.
7 * libjpeg-turbo Modifications:
Jonathan Wright24e31052021-04-26 12:10:48 +01008 * Copyright (C) 2010, 2013-2014, 2017, 2019-2021, D. R. Commander.
Tom Hudson0d47d2d2016-05-04 13:22:56 -04009 * For conditions of distribution and use, see the accompanying README.ijg
10 * file.
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +000011 *
12 * This file contains a command-line user interface for the JPEG compressor.
13 * It should work on any system with Unix- or MS-DOS-style command lines.
14 *
15 * Two different command line styles are permitted, depending on the
16 * compile-time switch TWO_FILE_COMMANDLINE:
Tom Hudson0d47d2d2016-05-04 13:22:56 -040017 * cjpeg [options] inputfile outputfile
18 * cjpeg [options] [inputfile]
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +000019 * In the second style, output is always to standard output, which you'd
20 * normally redirect to a file or pipe to some other program. Input is
21 * either from a named file or from standard input (typically redirected).
22 * The second style is convenient on Unix but is unhelpful on systems that
23 * don't support pipes. Also, you MUST use the first style if your system
24 * doesn't do binary I/O to stdin/stdout.
25 * To simplify script writing, the "-outfile" switch is provided. The syntax
Tom Hudson0d47d2d2016-05-04 13:22:56 -040026 * cjpeg [options] -outfile outputfile inputfile
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +000027 * works regardless of which command line style is used.
28 */
29
Jonathan Wright24e31052021-04-26 12:10:48 +010030#ifdef CJPEG_FUZZER
31#define JPEG_INTERNALS
32#endif
Tom Hudson0d47d2d2016-05-04 13:22:56 -040033#include "cdjpeg.h" /* Common decls for cjpeg/djpeg applications */
34#include "jversion.h" /* for version message */
35#include "jconfigint.h"
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +000036
Chris Blumecca8c4d2019-03-01 01:09:50 -080037#ifndef HAVE_STDLIB_H /* <stdlib.h> should declare malloc(),free() */
38extern void *malloc(size_t size);
39extern void free(void *ptr);
40#endif
41
Tom Hudson0d47d2d2016-05-04 13:22:56 -040042#ifdef USE_CCOMMAND /* command-line reader for Macintosh */
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +000043#ifdef __MWERKS__
44#include <SIOUX.h> /* Metrowerks needs this */
Tom Hudson0d47d2d2016-05-04 13:22:56 -040045#include <console.h> /* ... and this */
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +000046#endif
47#ifdef THINK_C
Tom Hudson0d47d2d2016-05-04 13:22:56 -040048#include <console.h> /* Think declares it here */
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +000049#endif
50#endif
51
52
53/* Create the add-on message string table. */
54
Chris Blumecca8c4d2019-03-01 01:09:50 -080055#define JMESSAGE(code, string) string,
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +000056
57static const char * const cdjpeg_message_table[] = {
58#include "cderror.h"
59 NULL
60};
61
62
63/*
64 * This routine determines what format the input file is,
65 * and selects the appropriate input-reading module.
66 *
67 * To determine which family of input formats the file belongs to,
68 * we may look only at the first byte of the file, since C does not
69 * guarantee that more than one character can be pushed back with ungetc.
70 * Looking at additional bytes would require one of these approaches:
71 * 1) assume we can fseek() the input file (fails for piped input);
72 * 2) assume we can push back more than one character (works in
73 * some C implementations, but unportable);
74 * 3) provide our own buffering (breaks input readers that want to use
Jonathan Wrightbbb82822020-11-25 13:36:43 +000075 * stdio directly);
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +000076 * or 4) don't put back the data, and modify the input_init methods to assume
Jonathan Wrightbbb82822020-11-25 13:36:43 +000077 * they start reading after the start of file.
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +000078 * #1 is attractive for MS-DOS but is untenable on Unix.
79 *
80 * The most portable solution for file types that can't be identified by their
81 * first byte is to make the user tell us what they are. This is also the
82 * only approach for "raw" file types that contain only arbitrary values.
83 * We presently apply this method for Targa files. Most of the time Targa
84 * files start with 0x00, so we recognize that case. Potentially, however,
85 * a Targa file could start with any byte value (byte 0 is the length of the
86 * seldom-used ID field), so we provide a switch to force Targa input mode.
87 */
88
Tom Hudson0d47d2d2016-05-04 13:22:56 -040089static boolean is_targa; /* records user -targa switch */
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +000090
91
92LOCAL(cjpeg_source_ptr)
Chris Blumecca8c4d2019-03-01 01:09:50 -080093select_file_type(j_compress_ptr cinfo, FILE *infile)
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +000094{
95 int c;
96
97 if (is_targa) {
98#ifdef TARGA_SUPPORTED
99 return jinit_read_targa(cinfo);
100#else
101 ERREXIT(cinfo, JERR_TGA_NOTCOMP);
102#endif
103 }
104
105 if ((c = getc(infile)) == EOF)
106 ERREXIT(cinfo, JERR_INPUT_EMPTY);
107 if (ungetc(c, infile) == EOF)
108 ERREXIT(cinfo, JERR_UNGETC_FAILED);
109
110 switch (c) {
111#ifdef BMP_SUPPORTED
112 case 'B':
Chris Blumecca8c4d2019-03-01 01:09:50 -0800113 return jinit_read_bmp(cinfo, TRUE);
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000114#endif
115#ifdef GIF_SUPPORTED
116 case 'G':
117 return jinit_read_gif(cinfo);
118#endif
119#ifdef PPM_SUPPORTED
120 case 'P':
121 return jinit_read_ppm(cinfo);
122#endif
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000123#ifdef TARGA_SUPPORTED
124 case 0x00:
125 return jinit_read_targa(cinfo);
126#endif
127 default:
128 ERREXIT(cinfo, JERR_UNKNOWN_FORMAT);
129 break;
130 }
131
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400132 return NULL; /* suppress compiler warnings */
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000133}
134
135
136/*
137 * Argument-parsing code.
138 * The switch parser is designed to be useful with DOS-style command line
139 * syntax, ie, intermixed switches and file names, where only the switches
140 * to the left of a given file name affect processing of that file.
141 * The main program in this file doesn't actually use this capability...
142 */
143
144
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400145static const char *progname; /* program name for error messages */
Chris Blumecca8c4d2019-03-01 01:09:50 -0800146static char *icc_filename; /* for -icc switch */
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400147static char *outfilename; /* for -outfile switch */
Jonathan Wright24e31052021-04-26 12:10:48 +0100148boolean memdst; /* for -memdst switch */
149boolean report; /* for -report switch */
150
151
152#ifdef CJPEG_FUZZER
153
154#include <setjmp.h>
155
156struct my_error_mgr {
157 struct jpeg_error_mgr pub;
158 jmp_buf setjmp_buffer;
159};
160
161void my_error_exit(j_common_ptr cinfo)
162{
163 struct my_error_mgr *myerr = (struct my_error_mgr *)cinfo->err;
164
165 longjmp(myerr->setjmp_buffer, 1);
166}
167
168static void my_emit_message(j_common_ptr cinfo, int msg_level)
169{
170 if (msg_level < 0)
171 cinfo->err->num_warnings++;
172}
173
174#define HANDLE_ERROR() { \
175 if (cinfo.global_state > CSTATE_START) { \
176 if (memdst && outbuffer) \
177 (*cinfo.dest->term_destination) (&cinfo); \
178 jpeg_abort_compress(&cinfo); \
179 } \
180 jpeg_destroy_compress(&cinfo); \
181 if (input_file != stdin && input_file != NULL) \
182 fclose(input_file); \
183 if (memdst) \
184 free(outbuffer); \
185 return EXIT_FAILURE; \
186}
187
188#endif
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000189
190
191LOCAL(void)
Chris Blumecca8c4d2019-03-01 01:09:50 -0800192usage(void)
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000193/* complain about bad command line */
194{
195 fprintf(stderr, "usage: %s [switches] ", progname);
196#ifdef TWO_FILE_COMMANDLINE
197 fprintf(stderr, "inputfile outputfile\n");
198#else
199 fprintf(stderr, "[inputfile]\n");
200#endif
201
202 fprintf(stderr, "Switches (names may be abbreviated):\n");
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400203 fprintf(stderr, " -quality N[,...] Compression quality (0..100; 5-95 is most useful range,\n");
204 fprintf(stderr, " default is 75)\n");
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000205 fprintf(stderr, " -grayscale Create monochrome JPEG file\n");
noel@chromium.org3395bcc2014-04-14 06:56:00 +0000206 fprintf(stderr, " -rgb Create RGB JPEG file\n");
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000207#ifdef ENTROPY_OPT_SUPPORTED
208 fprintf(stderr, " -optimize Optimize Huffman table (smaller file, but slow compression)\n");
209#endif
210#ifdef C_PROGRESSIVE_SUPPORTED
211 fprintf(stderr, " -progressive Create progressive JPEG file\n");
212#endif
213#ifdef TARGA_SUPPORTED
214 fprintf(stderr, " -targa Input file is Targa format (usually not needed)\n");
215#endif
216 fprintf(stderr, "Switches for advanced users:\n");
hbono@chromium.orgdf5ffdd2012-05-11 07:46:03 +0000217#ifdef C_ARITH_CODING_SUPPORTED
218 fprintf(stderr, " -arithmetic Use arithmetic coding\n");
219#endif
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000220#ifdef DCT_ISLOW_SUPPORTED
Jonathan Wrightbbb82822020-11-25 13:36:43 +0000221 fprintf(stderr, " -dct int Use accurate integer DCT method%s\n",
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400222 (JDCT_DEFAULT == JDCT_ISLOW ? " (default)" : ""));
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000223#endif
224#ifdef DCT_IFAST_SUPPORTED
Jonathan Wrightbbb82822020-11-25 13:36:43 +0000225 fprintf(stderr, " -dct fast Use less accurate integer DCT method [legacy feature]%s\n",
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400226 (JDCT_DEFAULT == JDCT_IFAST ? " (default)" : ""));
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000227#endif
228#ifdef DCT_FLOAT_SUPPORTED
Jonathan Wrightbbb82822020-11-25 13:36:43 +0000229 fprintf(stderr, " -dct float Use floating-point DCT method [legacy feature]%s\n",
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400230 (JDCT_DEFAULT == JDCT_FLOAT ? " (default)" : ""));
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000231#endif
Chris Blumecca8c4d2019-03-01 01:09:50 -0800232 fprintf(stderr, " -icc FILE Embed ICC profile contained in FILE\n");
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000233 fprintf(stderr, " -restart N Set restart interval in rows, or in blocks with B\n");
234#ifdef INPUT_SMOOTHING_SUPPORTED
235 fprintf(stderr, " -smooth N Smooth dithered input (N=1..100 is strength)\n");
236#endif
237 fprintf(stderr, " -maxmemory N Maximum memory to use (in kbytes)\n");
238 fprintf(stderr, " -outfile name Specify name for output file\n");
noel@chromium.org3395bcc2014-04-14 06:56:00 +0000239#if JPEG_LIB_VERSION >= 80 || defined(MEM_SRCDST_SUPPORTED)
240 fprintf(stderr, " -memdst Compress to memory instead of file (useful for benchmarking)\n");
241#endif
Jonathan Wrightbbb82822020-11-25 13:36:43 +0000242 fprintf(stderr, " -report Report compression progress\n");
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000243 fprintf(stderr, " -verbose or -debug Emit debug output\n");
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400244 fprintf(stderr, " -version Print version information and exit\n");
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000245 fprintf(stderr, "Switches for wizards:\n");
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000246 fprintf(stderr, " -baseline Force baseline quantization tables\n");
Chris Blumecca8c4d2019-03-01 01:09:50 -0800247 fprintf(stderr, " -qtables FILE Use quantization tables given in FILE\n");
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000248 fprintf(stderr, " -qslots N[,...] Set component quantization tables\n");
249 fprintf(stderr, " -sample HxV[,...] Set component sampling factors\n");
250#ifdef C_MULTISCAN_FILES_SUPPORTED
Chris Blumecca8c4d2019-03-01 01:09:50 -0800251 fprintf(stderr, " -scans FILE Create multi-scan JPEG per script FILE\n");
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000252#endif
253 exit(EXIT_FAILURE);
254}
255
256
257LOCAL(int)
Chris Blumecca8c4d2019-03-01 01:09:50 -0800258parse_switches(j_compress_ptr cinfo, int argc, char **argv,
259 int last_file_arg_seen, boolean for_real)
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000260/* Parse optional switches.
261 * Returns argv[] index of first file-name argument (== argc if none).
262 * Any file names with indexes <= last_file_arg_seen are ignored;
263 * they have presumably been processed in a previous iteration.
264 * (Pass 0 for last_file_arg_seen on the first or only iteration.)
265 * for_real is FALSE on the first (dummy) pass; we may skip any expensive
266 * processing.
267 */
268{
269 int argn;
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400270 char *arg;
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000271 boolean force_baseline;
272 boolean simple_progressive;
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400273 char *qualityarg = NULL; /* saves -quality parm if any */
274 char *qtablefile = NULL; /* saves -qtables filename if any */
275 char *qslotsarg = NULL; /* saves -qslots parm if any */
276 char *samplearg = NULL; /* saves -sample parm if any */
277 char *scansarg = NULL; /* saves -scans parm if any */
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000278
279 /* Set up default JPEG parameters. */
hbono@chromium.org98626972011-08-03 03:13:08 +0000280
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400281 force_baseline = FALSE; /* by default, allow 16-bit quantizers */
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000282 simple_progressive = FALSE;
283 is_targa = FALSE;
Chris Blumecca8c4d2019-03-01 01:09:50 -0800284 icc_filename = NULL;
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000285 outfilename = NULL;
noel@chromium.org3395bcc2014-04-14 06:56:00 +0000286 memdst = FALSE;
Jonathan Wrightbbb82822020-11-25 13:36:43 +0000287 report = FALSE;
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000288 cinfo->err->trace_level = 0;
289
290 /* Scan command line options, adjust parameters */
291
292 for (argn = 1; argn < argc; argn++) {
293 arg = argv[argn];
294 if (*arg != '-') {
295 /* Not a switch, must be a file name argument */
296 if (argn <= last_file_arg_seen) {
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400297 outfilename = NULL; /* -outfile applies to just one input file */
298 continue; /* ignore this name if previously processed */
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000299 }
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400300 break; /* else done parsing switches */
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000301 }
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400302 arg++; /* advance past switch marker character */
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000303
304 if (keymatch(arg, "arithmetic", 1)) {
305 /* Use arithmetic coding. */
306#ifdef C_ARITH_CODING_SUPPORTED
307 cinfo->arith_code = TRUE;
308#else
309 fprintf(stderr, "%s: sorry, arithmetic coding not supported\n",
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400310 progname);
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000311 exit(EXIT_FAILURE);
312#endif
313
314 } else if (keymatch(arg, "baseline", 1)) {
315 /* Force baseline-compatible output (8-bit quantizer values). */
316 force_baseline = TRUE;
317
318 } else if (keymatch(arg, "dct", 2)) {
319 /* Select DCT algorithm. */
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400320 if (++argn >= argc) /* advance to next argument */
321 usage();
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000322 if (keymatch(argv[argn], "int", 1)) {
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400323 cinfo->dct_method = JDCT_ISLOW;
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000324 } else if (keymatch(argv[argn], "fast", 2)) {
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400325 cinfo->dct_method = JDCT_IFAST;
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000326 } else if (keymatch(argv[argn], "float", 2)) {
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400327 cinfo->dct_method = JDCT_FLOAT;
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000328 } else
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400329 usage();
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000330
331 } else if (keymatch(arg, "debug", 1) || keymatch(arg, "verbose", 1)) {
332 /* Enable debug printouts. */
333 /* On first -d, print version identification */
334 static boolean printed_version = FALSE;
335
Chris Blumecca8c4d2019-03-01 01:09:50 -0800336 if (!printed_version) {
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400337 fprintf(stderr, "%s version %s (build %s)\n",
338 PACKAGE_NAME, VERSION, BUILD);
339 fprintf(stderr, "%s\n\n", JCOPYRIGHT);
340 fprintf(stderr, "Emulating The Independent JPEG Group's software, version %s\n\n",
341 JVERSION);
342 printed_version = TRUE;
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000343 }
344 cinfo->err->trace_level++;
345
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400346 } else if (keymatch(arg, "version", 4)) {
347 fprintf(stderr, "%s version %s (build %s)\n",
348 PACKAGE_NAME, VERSION, BUILD);
349 exit(EXIT_SUCCESS);
350
Chris Blumecca8c4d2019-03-01 01:09:50 -0800351 } else if (keymatch(arg, "grayscale", 2) ||
352 keymatch(arg, "greyscale", 2)) {
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000353 /* Force a monochrome JPEG file to be generated. */
354 jpeg_set_colorspace(cinfo, JCS_GRAYSCALE);
355
noel@chromium.org3395bcc2014-04-14 06:56:00 +0000356 } else if (keymatch(arg, "rgb", 3)) {
357 /* Force an RGB JPEG file to be generated. */
358 jpeg_set_colorspace(cinfo, JCS_RGB);
359
Chris Blumecca8c4d2019-03-01 01:09:50 -0800360 } else if (keymatch(arg, "icc", 1)) {
361 /* Set ICC filename. */
362 if (++argn >= argc) /* advance to next argument */
363 usage();
364 icc_filename = argv[argn];
365
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000366 } else if (keymatch(arg, "maxmemory", 3)) {
367 /* Maximum memory in Kb (or Mb with 'm'). */
368 long lval;
369 char ch = 'x';
370
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400371 if (++argn >= argc) /* advance to next argument */
372 usage();
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000373 if (sscanf(argv[argn], "%ld%c", &lval, &ch) < 1)
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400374 usage();
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000375 if (ch == 'm' || ch == 'M')
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400376 lval *= 1000L;
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000377 cinfo->mem->max_memory_to_use = lval * 1000L;
378
379 } else if (keymatch(arg, "optimize", 1) || keymatch(arg, "optimise", 1)) {
380 /* Enable entropy parm optimization. */
381#ifdef ENTROPY_OPT_SUPPORTED
382 cinfo->optimize_coding = TRUE;
383#else
noel@chromium.org3395bcc2014-04-14 06:56:00 +0000384 fprintf(stderr, "%s: sorry, entropy optimization was not compiled in\n",
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400385 progname);
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000386 exit(EXIT_FAILURE);
387#endif
388
389 } else if (keymatch(arg, "outfile", 4)) {
390 /* Set output file name. */
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400391 if (++argn >= argc) /* advance to next argument */
392 usage();
393 outfilename = argv[argn]; /* save it away for later use */
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000394
395 } else if (keymatch(arg, "progressive", 1)) {
396 /* Select simple progressive mode. */
397#ifdef C_PROGRESSIVE_SUPPORTED
398 simple_progressive = TRUE;
399 /* We must postpone execution until num_components is known. */
400#else
noel@chromium.org3395bcc2014-04-14 06:56:00 +0000401 fprintf(stderr, "%s: sorry, progressive output was not compiled in\n",
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400402 progname);
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000403 exit(EXIT_FAILURE);
404#endif
405
noel@chromium.org3395bcc2014-04-14 06:56:00 +0000406 } else if (keymatch(arg, "memdst", 2)) {
407 /* Use in-memory destination manager */
408#if JPEG_LIB_VERSION >= 80 || defined(MEM_SRCDST_SUPPORTED)
409 memdst = TRUE;
410#else
411 fprintf(stderr, "%s: sorry, in-memory destination manager was not compiled in\n",
412 progname);
413 exit(EXIT_FAILURE);
414#endif
415
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000416 } else if (keymatch(arg, "quality", 1)) {
hbono@chromium.org98626972011-08-03 03:13:08 +0000417 /* Quality ratings (quantization table scaling factors). */
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400418 if (++argn >= argc) /* advance to next argument */
419 usage();
hbono@chromium.org98626972011-08-03 03:13:08 +0000420 qualityarg = argv[argn];
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000421
422 } else if (keymatch(arg, "qslots", 2)) {
423 /* Quantization table slot numbers. */
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400424 if (++argn >= argc) /* advance to next argument */
425 usage();
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000426 qslotsarg = argv[argn];
427 /* Must delay setting qslots until after we have processed any
428 * colorspace-determining switches, since jpeg_set_colorspace sets
429 * default quant table numbers.
430 */
431
432 } else if (keymatch(arg, "qtables", 2)) {
433 /* Quantization tables fetched from file. */
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400434 if (++argn >= argc) /* advance to next argument */
435 usage();
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000436 qtablefile = argv[argn];
437 /* We postpone actually reading the file in case -quality comes later. */
438
Jonathan Wrightbbb82822020-11-25 13:36:43 +0000439 } else if (keymatch(arg, "report", 3)) {
440 report = TRUE;
441
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000442 } else if (keymatch(arg, "restart", 1)) {
443 /* Restart interval in MCU rows (or in MCUs with 'b'). */
444 long lval;
445 char ch = 'x';
446
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400447 if (++argn >= argc) /* advance to next argument */
448 usage();
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000449 if (sscanf(argv[argn], "%ld%c", &lval, &ch) < 1)
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400450 usage();
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000451 if (lval < 0 || lval > 65535L)
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400452 usage();
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000453 if (ch == 'b' || ch == 'B') {
Chris Blumecca8c4d2019-03-01 01:09:50 -0800454 cinfo->restart_interval = (unsigned int)lval;
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400455 cinfo->restart_in_rows = 0; /* else prior '-restart n' overrides me */
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000456 } else {
Chris Blumecca8c4d2019-03-01 01:09:50 -0800457 cinfo->restart_in_rows = (int)lval;
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400458 /* restart_interval will be computed during startup */
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000459 }
460
461 } else if (keymatch(arg, "sample", 2)) {
462 /* Set sampling factors. */
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400463 if (++argn >= argc) /* advance to next argument */
464 usage();
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000465 samplearg = argv[argn];
466 /* Must delay setting sample factors until after we have processed any
467 * colorspace-determining switches, since jpeg_set_colorspace sets
468 * default sampling factors.
469 */
470
hbono@chromium.org98626972011-08-03 03:13:08 +0000471 } else if (keymatch(arg, "scans", 4)) {
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000472 /* Set scan script. */
473#ifdef C_MULTISCAN_FILES_SUPPORTED
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400474 if (++argn >= argc) /* advance to next argument */
475 usage();
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000476 scansarg = argv[argn];
477 /* We must postpone reading the file in case -progressive appears. */
478#else
noel@chromium.org3395bcc2014-04-14 06:56:00 +0000479 fprintf(stderr, "%s: sorry, multi-scan output was not compiled in\n",
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400480 progname);
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000481 exit(EXIT_FAILURE);
482#endif
483
484 } else if (keymatch(arg, "smooth", 2)) {
485 /* Set input smoothing factor. */
486 int val;
487
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400488 if (++argn >= argc) /* advance to next argument */
489 usage();
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000490 if (sscanf(argv[argn], "%d", &val) != 1)
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400491 usage();
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000492 if (val < 0 || val > 100)
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400493 usage();
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000494 cinfo->smoothing_factor = val;
495
496 } else if (keymatch(arg, "targa", 1)) {
497 /* Input file is Targa format. */
498 is_targa = TRUE;
499
500 } else {
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400501 usage(); /* bogus switch */
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000502 }
503 }
504
505 /* Post-switch-scanning cleanup */
506
507 if (for_real) {
508
509 /* Set quantization tables for selected quality. */
510 /* Some or all may be overridden if -qtables is present. */
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400511 if (qualityarg != NULL) /* process -quality if it was present */
Chris Blumecca8c4d2019-03-01 01:09:50 -0800512 if (!set_quality_ratings(cinfo, qualityarg, force_baseline))
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400513 usage();
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000514
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400515 if (qtablefile != NULL) /* process -qtables if it was present */
Chris Blumecca8c4d2019-03-01 01:09:50 -0800516 if (!read_quant_tables(cinfo, qtablefile, force_baseline))
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400517 usage();
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000518
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400519 if (qslotsarg != NULL) /* process -qslots if it was present */
Chris Blumecca8c4d2019-03-01 01:09:50 -0800520 if (!set_quant_slots(cinfo, qslotsarg))
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400521 usage();
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000522
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400523 if (samplearg != NULL) /* process -sample if it was present */
Chris Blumecca8c4d2019-03-01 01:09:50 -0800524 if (!set_sample_factors(cinfo, samplearg))
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400525 usage();
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000526
527#ifdef C_PROGRESSIVE_SUPPORTED
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400528 if (simple_progressive) /* process -progressive; -scans can override */
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000529 jpeg_simple_progression(cinfo);
530#endif
531
532#ifdef C_MULTISCAN_FILES_SUPPORTED
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400533 if (scansarg != NULL) /* process -scans if it was present */
Chris Blumecca8c4d2019-03-01 01:09:50 -0800534 if (!read_scan_script(cinfo, scansarg))
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400535 usage();
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000536#endif
537 }
538
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400539 return argn; /* return index of next arg (file name) */
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000540}
541
542
543/*
544 * The main program.
545 */
546
547int
Jonathan Wright5961ab92020-06-21 13:11:49 +0100548#ifdef GTEST
549cjpeg(int argc, char **argv)
550#else
Chris Blumecca8c4d2019-03-01 01:09:50 -0800551main(int argc, char **argv)
Jonathan Wright5961ab92020-06-21 13:11:49 +0100552#endif
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000553{
554 struct jpeg_compress_struct cinfo;
Jonathan Wright24e31052021-04-26 12:10:48 +0100555#ifdef CJPEG_FUZZER
556 struct my_error_mgr myerr;
557 struct jpeg_error_mgr &jerr = myerr.pub;
558#else
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000559 struct jpeg_error_mgr jerr;
Jonathan Wright24e31052021-04-26 12:10:48 +0100560#endif
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000561 struct cdjpeg_progress_mgr progress;
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000562 int file_index;
563 cjpeg_source_ptr src_mgr;
Jonathan Wright24e31052021-04-26 12:10:48 +0100564 FILE *input_file = NULL;
Chris Blumecca8c4d2019-03-01 01:09:50 -0800565 FILE *icc_file;
566 JOCTET *icc_profile = NULL;
567 long icc_len = 0;
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400568 FILE *output_file = NULL;
noel@chromium.org3395bcc2014-04-14 06:56:00 +0000569 unsigned char *outbuffer = NULL;
570 unsigned long outsize = 0;
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000571 JDIMENSION num_scanlines;
572
573 /* On Mac, fetch a command line. */
574#ifdef USE_CCOMMAND
575 argc = ccommand(&argv);
576#endif
577
578 progname = argv[0];
579 if (progname == NULL || progname[0] == 0)
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400580 progname = "cjpeg"; /* in case C library doesn't provide it */
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000581
582 /* Initialize the JPEG compression object with default error handling. */
583 cinfo.err = jpeg_std_error(&jerr);
584 jpeg_create_compress(&cinfo);
585 /* Add some application-specific error messages (from cderror.h) */
586 jerr.addon_message_table = cdjpeg_message_table;
587 jerr.first_addon_message = JMSG_FIRSTADDONCODE;
588 jerr.last_addon_message = JMSG_LASTADDONCODE;
589
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000590 /* Initialize JPEG parameters.
591 * Much of this may be overridden later.
592 * In particular, we don't yet know the input file's color space,
593 * but we need to provide some value for jpeg_set_defaults() to work.
594 */
595
596 cinfo.in_color_space = JCS_RGB; /* arbitrary guess */
597 jpeg_set_defaults(&cinfo);
598
599 /* Scan command line to find file names.
600 * It is convenient to use just one switch-parsing routine, but the switch
601 * values read here are ignored; we will rescan the switches after opening
602 * the input file.
603 */
604
605 file_index = parse_switches(&cinfo, argc, argv, 0, FALSE);
606
607#ifdef TWO_FILE_COMMANDLINE
noel@chromium.org3395bcc2014-04-14 06:56:00 +0000608 if (!memdst) {
609 /* Must have either -outfile switch or explicit output file name */
610 if (outfilename == NULL) {
Chris Blumecca8c4d2019-03-01 01:09:50 -0800611 if (file_index != argc - 2) {
noel@chromium.org3395bcc2014-04-14 06:56:00 +0000612 fprintf(stderr, "%s: must name one input and one output file\n",
613 progname);
614 usage();
615 }
Chris Blumecca8c4d2019-03-01 01:09:50 -0800616 outfilename = argv[file_index + 1];
noel@chromium.org3395bcc2014-04-14 06:56:00 +0000617 } else {
Chris Blumecca8c4d2019-03-01 01:09:50 -0800618 if (file_index != argc - 1) {
noel@chromium.org3395bcc2014-04-14 06:56:00 +0000619 fprintf(stderr, "%s: must name one input and one output file\n",
620 progname);
621 usage();
622 }
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000623 }
624 }
625#else
626 /* Unix style: expect zero or one file name */
Chris Blumecca8c4d2019-03-01 01:09:50 -0800627 if (file_index < argc - 1) {
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000628 fprintf(stderr, "%s: only one input file\n", progname);
629 usage();
630 }
631#endif /* TWO_FILE_COMMANDLINE */
632
633 /* Open the input file. */
634 if (file_index < argc) {
635 if ((input_file = fopen(argv[file_index], READ_BINARY)) == NULL) {
636 fprintf(stderr, "%s: can't open %s\n", progname, argv[file_index]);
Jonathan Wright5961ab92020-06-21 13:11:49 +0100637 return EXIT_FAILURE;
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000638 }
639 } else {
640 /* default input file is stdin */
641 input_file = read_stdin();
642 }
643
644 /* Open the output file. */
645 if (outfilename != NULL) {
646 if ((output_file = fopen(outfilename, WRITE_BINARY)) == NULL) {
647 fprintf(stderr, "%s: can't open %s\n", progname, outfilename);
Jonathan Wright5961ab92020-06-21 13:11:49 +0100648 return EXIT_FAILURE;
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000649 }
noel@chromium.org3395bcc2014-04-14 06:56:00 +0000650 } else if (!memdst) {
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000651 /* default output file is stdout */
652 output_file = write_stdout();
653 }
654
Chris Blumecca8c4d2019-03-01 01:09:50 -0800655 if (icc_filename != NULL) {
656 if ((icc_file = fopen(icc_filename, READ_BINARY)) == NULL) {
657 fprintf(stderr, "%s: can't open %s\n", progname, icc_filename);
Jonathan Wright5961ab92020-06-21 13:11:49 +0100658 return EXIT_FAILURE;
Chris Blumecca8c4d2019-03-01 01:09:50 -0800659 }
660 if (fseek(icc_file, 0, SEEK_END) < 0 ||
661 (icc_len = ftell(icc_file)) < 1 ||
662 fseek(icc_file, 0, SEEK_SET) < 0) {
663 fprintf(stderr, "%s: can't determine size of %s\n", progname,
664 icc_filename);
Jonathan Wright5961ab92020-06-21 13:11:49 +0100665 return EXIT_FAILURE;
Chris Blumecca8c4d2019-03-01 01:09:50 -0800666 }
667 if ((icc_profile = (JOCTET *)malloc(icc_len)) == NULL) {
668 fprintf(stderr, "%s: can't allocate memory for ICC profile\n", progname);
669 fclose(icc_file);
Jonathan Wright5961ab92020-06-21 13:11:49 +0100670 return EXIT_FAILURE;
Chris Blumecca8c4d2019-03-01 01:09:50 -0800671 }
672 if (fread(icc_profile, icc_len, 1, icc_file) < 1) {
673 fprintf(stderr, "%s: can't read ICC profile from %s\n", progname,
674 icc_filename);
675 free(icc_profile);
676 fclose(icc_file);
Jonathan Wright5961ab92020-06-21 13:11:49 +0100677 return EXIT_FAILURE;
Chris Blumecca8c4d2019-03-01 01:09:50 -0800678 }
679 fclose(icc_file);
680 }
681
Jonathan Wright24e31052021-04-26 12:10:48 +0100682#ifdef CJPEG_FUZZER
683 jerr.error_exit = my_error_exit;
684 jerr.emit_message = my_emit_message;
685 if (setjmp(myerr.setjmp_buffer))
686 HANDLE_ERROR()
687#endif
688
Jonathan Wrightbbb82822020-11-25 13:36:43 +0000689 if (report) {
690 start_progress_monitor((j_common_ptr)&cinfo, &progress);
691 progress.report = report;
692 }
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000693
694 /* Figure out the input file format, and set up to read it. */
695 src_mgr = select_file_type(&cinfo, input_file);
696 src_mgr->input_file = input_file;
Jonathan Wright24e31052021-04-26 12:10:48 +0100697#ifdef CJPEG_FUZZER
698 src_mgr->max_pixels = 1048576;
699#endif
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000700
701 /* Read the input file header to obtain file size & colorspace. */
702 (*src_mgr->start_input) (&cinfo, src_mgr);
703
704 /* Now that we know input colorspace, fix colorspace-dependent defaults */
705 jpeg_default_colorspace(&cinfo);
706
707 /* Adjust default compression parameters by re-parsing the options */
708 file_index = parse_switches(&cinfo, argc, argv, 0, TRUE);
709
710 /* Specify data destination for compression */
noel@chromium.org3395bcc2014-04-14 06:56:00 +0000711#if JPEG_LIB_VERSION >= 80 || defined(MEM_SRCDST_SUPPORTED)
712 if (memdst)
713 jpeg_mem_dest(&cinfo, &outbuffer, &outsize);
714 else
715#endif
716 jpeg_stdio_dest(&cinfo, output_file);
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000717
Jonathan Wright24e31052021-04-26 12:10:48 +0100718#ifdef CJPEG_FUZZER
719 if (setjmp(myerr.setjmp_buffer))
720 HANDLE_ERROR()
721#endif
722
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000723 /* Start compressor */
724 jpeg_start_compress(&cinfo, TRUE);
725
Chris Blumecca8c4d2019-03-01 01:09:50 -0800726 if (icc_profile != NULL)
727 jpeg_write_icc_profile(&cinfo, icc_profile, (unsigned int)icc_len);
728
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000729 /* Process data */
730 while (cinfo.next_scanline < cinfo.image_height) {
731 num_scanlines = (*src_mgr->get_pixel_rows) (&cinfo, src_mgr);
Chris Blumecca8c4d2019-03-01 01:09:50 -0800732 (void)jpeg_write_scanlines(&cinfo, src_mgr->buffer, num_scanlines);
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000733 }
734
735 /* Finish compression and release memory */
736 (*src_mgr->finish_input) (&cinfo, src_mgr);
737 jpeg_finish_compress(&cinfo);
738 jpeg_destroy_compress(&cinfo);
739
740 /* Close files, if we opened them */
741 if (input_file != stdin)
742 fclose(input_file);
noel@chromium.org3395bcc2014-04-14 06:56:00 +0000743 if (output_file != stdout && output_file != NULL)
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000744 fclose(output_file);
745
Jonathan Wrightbbb82822020-11-25 13:36:43 +0000746 if (report)
747 end_progress_monitor((j_common_ptr)&cinfo);
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000748
noel@chromium.org3395bcc2014-04-14 06:56:00 +0000749 if (memdst) {
Jonathan Wright24e31052021-04-26 12:10:48 +0100750#ifndef CJPEG_FUZZER
noel@chromium.org3395bcc2014-04-14 06:56:00 +0000751 fprintf(stderr, "Compressed size: %lu bytes\n", outsize);
Jonathan Wright24e31052021-04-26 12:10:48 +0100752#endif
Jonathan Wrightdb870df2020-08-05 11:42:22 +0100753 free(outbuffer);
noel@chromium.org3395bcc2014-04-14 06:56:00 +0000754 }
755
Jonathan Wrightdb870df2020-08-05 11:42:22 +0100756 free(icc_profile);
Chris Blumecca8c4d2019-03-01 01:09:50 -0800757
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000758 /* All done. */
Jonathan Wright5961ab92020-06-21 13:11:49 +0100759 return (jerr.num_warnings ? EXIT_WARNING : EXIT_SUCCESS);
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000760}