blob: 156b67dadd7131564121cfc265ca93e99d3fc3cd [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 Wright22f1a222022-03-01 15:53:34 +00008 * Copyright (C) 2010, 2013-2014, 2017, 2019-2022, 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 Wright22f1a222022-03-01 15:53:34 +000030#ifdef _MSC_VER
31#define _CRT_SECURE_NO_DEPRECATE
32#endif
33
Jonathan Wright24e31052021-04-26 12:10:48 +010034#ifdef CJPEG_FUZZER
35#define JPEG_INTERNALS
36#endif
Tom Hudson0d47d2d2016-05-04 13:22:56 -040037#include "cdjpeg.h" /* Common decls for cjpeg/djpeg applications */
38#include "jversion.h" /* for version message */
39#include "jconfigint.h"
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 routine determines what format the input file is,
64 * and selects the appropriate input-reading module.
65 *
66 * To determine which family of input formats the file belongs to,
67 * we may look only at the first byte of the file, since C does not
68 * guarantee that more than one character can be pushed back with ungetc.
69 * Looking at additional bytes would require one of these approaches:
70 * 1) assume we can fseek() the input file (fails for piped input);
71 * 2) assume we can push back more than one character (works in
72 * some C implementations, but unportable);
73 * 3) provide our own buffering (breaks input readers that want to use
Jonathan Wrightbbb82822020-11-25 13:36:43 +000074 * stdio directly);
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +000075 * or 4) don't put back the data, and modify the input_init methods to assume
Jonathan Wrightbbb82822020-11-25 13:36:43 +000076 * they start reading after the start of file.
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +000077 * #1 is attractive for MS-DOS but is untenable on Unix.
78 *
79 * The most portable solution for file types that can't be identified by their
80 * first byte is to make the user tell us what they are. This is also the
81 * only approach for "raw" file types that contain only arbitrary values.
82 * We presently apply this method for Targa files. Most of the time Targa
83 * files start with 0x00, so we recognize that case. Potentially, however,
84 * a Targa file could start with any byte value (byte 0 is the length of the
85 * seldom-used ID field), so we provide a switch to force Targa input mode.
86 */
87
Tom Hudson0d47d2d2016-05-04 13:22:56 -040088static boolean is_targa; /* records user -targa switch */
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +000089
90
91LOCAL(cjpeg_source_ptr)
Chris Blumecca8c4d2019-03-01 01:09:50 -080092select_file_type(j_compress_ptr cinfo, FILE *infile)
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +000093{
94 int c;
95
96 if (is_targa) {
97#ifdef TARGA_SUPPORTED
98 return jinit_read_targa(cinfo);
99#else
100 ERREXIT(cinfo, JERR_TGA_NOTCOMP);
101#endif
102 }
103
104 if ((c = getc(infile)) == EOF)
105 ERREXIT(cinfo, JERR_INPUT_EMPTY);
106 if (ungetc(c, infile) == EOF)
107 ERREXIT(cinfo, JERR_UNGETC_FAILED);
108
109 switch (c) {
110#ifdef BMP_SUPPORTED
111 case 'B':
Chris Blumecca8c4d2019-03-01 01:09:50 -0800112 return jinit_read_bmp(cinfo, TRUE);
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000113#endif
114#ifdef GIF_SUPPORTED
115 case 'G':
116 return jinit_read_gif(cinfo);
117#endif
118#ifdef PPM_SUPPORTED
119 case 'P':
120 return jinit_read_ppm(cinfo);
121#endif
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000122#ifdef TARGA_SUPPORTED
123 case 0x00:
124 return jinit_read_targa(cinfo);
125#endif
126 default:
127 ERREXIT(cinfo, JERR_UNKNOWN_FORMAT);
128 break;
129 }
130
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400131 return NULL; /* suppress compiler warnings */
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000132}
133
134
135/*
136 * Argument-parsing code.
137 * The switch parser is designed to be useful with DOS-style command line
138 * syntax, ie, intermixed switches and file names, where only the switches
139 * to the left of a given file name affect processing of that file.
140 * The main program in this file doesn't actually use this capability...
141 */
142
143
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400144static const char *progname; /* program name for error messages */
Chris Blumecca8c4d2019-03-01 01:09:50 -0800145static char *icc_filename; /* for -icc switch */
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400146static char *outfilename; /* for -outfile switch */
Jonathan Wright24e31052021-04-26 12:10:48 +0100147boolean memdst; /* for -memdst switch */
148boolean report; /* for -report switch */
Jonathan Wright02959c32021-11-22 10:27:51 +0000149boolean strict; /* for -strict switch */
Jonathan Wright24e31052021-04-26 12:10:48 +0100150
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
Jonathan Wright22f1a222022-03-01 15:53:34 +0000168static void my_emit_message_fuzzer(j_common_ptr cinfo, int msg_level)
Jonathan Wright24e31052021-04-26 12:10:48 +0100169{
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");
Jonathan Wright02959c32021-11-22 10:27:51 +0000243 fprintf(stderr, " -strict Treat all warnings as fatal\n");
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000244 fprintf(stderr, " -verbose or -debug Emit debug output\n");
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400245 fprintf(stderr, " -version Print version information and exit\n");
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000246 fprintf(stderr, "Switches for wizards:\n");
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000247 fprintf(stderr, " -baseline Force baseline quantization tables\n");
Chris Blumecca8c4d2019-03-01 01:09:50 -0800248 fprintf(stderr, " -qtables FILE Use quantization tables given in FILE\n");
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000249 fprintf(stderr, " -qslots N[,...] Set component quantization tables\n");
250 fprintf(stderr, " -sample HxV[,...] Set component sampling factors\n");
251#ifdef C_MULTISCAN_FILES_SUPPORTED
Chris Blumecca8c4d2019-03-01 01:09:50 -0800252 fprintf(stderr, " -scans FILE Create multi-scan JPEG per script FILE\n");
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000253#endif
254 exit(EXIT_FAILURE);
255}
256
257
258LOCAL(int)
Chris Blumecca8c4d2019-03-01 01:09:50 -0800259parse_switches(j_compress_ptr cinfo, int argc, char **argv,
260 int last_file_arg_seen, boolean for_real)
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000261/* Parse optional switches.
262 * Returns argv[] index of first file-name argument (== argc if none).
263 * Any file names with indexes <= last_file_arg_seen are ignored;
264 * they have presumably been processed in a previous iteration.
265 * (Pass 0 for last_file_arg_seen on the first or only iteration.)
266 * for_real is FALSE on the first (dummy) pass; we may skip any expensive
267 * processing.
268 */
269{
270 int argn;
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400271 char *arg;
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000272 boolean force_baseline;
273 boolean simple_progressive;
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400274 char *qualityarg = NULL; /* saves -quality parm if any */
275 char *qtablefile = NULL; /* saves -qtables filename if any */
276 char *qslotsarg = NULL; /* saves -qslots parm if any */
277 char *samplearg = NULL; /* saves -sample parm if any */
278 char *scansarg = NULL; /* saves -scans parm if any */
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000279
280 /* Set up default JPEG parameters. */
hbono@chromium.org98626972011-08-03 03:13:08 +0000281
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400282 force_baseline = FALSE; /* by default, allow 16-bit quantizers */
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000283 simple_progressive = FALSE;
284 is_targa = FALSE;
Chris Blumecca8c4d2019-03-01 01:09:50 -0800285 icc_filename = NULL;
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000286 outfilename = NULL;
noel@chromium.org3395bcc2014-04-14 06:56:00 +0000287 memdst = FALSE;
Jonathan Wrightbbb82822020-11-25 13:36:43 +0000288 report = FALSE;
Jonathan Wright02959c32021-11-22 10:27:51 +0000289 strict = FALSE;
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000290 cinfo->err->trace_level = 0;
291
292 /* Scan command line options, adjust parameters */
293
294 for (argn = 1; argn < argc; argn++) {
295 arg = argv[argn];
296 if (*arg != '-') {
297 /* Not a switch, must be a file name argument */
298 if (argn <= last_file_arg_seen) {
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400299 outfilename = NULL; /* -outfile applies to just one input file */
300 continue; /* ignore this name if previously processed */
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000301 }
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400302 break; /* else done parsing switches */
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000303 }
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400304 arg++; /* advance past switch marker character */
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000305
306 if (keymatch(arg, "arithmetic", 1)) {
307 /* Use arithmetic coding. */
308#ifdef C_ARITH_CODING_SUPPORTED
309 cinfo->arith_code = TRUE;
310#else
311 fprintf(stderr, "%s: sorry, arithmetic coding not supported\n",
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400312 progname);
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000313 exit(EXIT_FAILURE);
314#endif
315
316 } else if (keymatch(arg, "baseline", 1)) {
317 /* Force baseline-compatible output (8-bit quantizer values). */
318 force_baseline = TRUE;
319
320 } else if (keymatch(arg, "dct", 2)) {
321 /* Select DCT algorithm. */
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400322 if (++argn >= argc) /* advance to next argument */
323 usage();
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000324 if (keymatch(argv[argn], "int", 1)) {
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400325 cinfo->dct_method = JDCT_ISLOW;
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000326 } else if (keymatch(argv[argn], "fast", 2)) {
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400327 cinfo->dct_method = JDCT_IFAST;
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000328 } else if (keymatch(argv[argn], "float", 2)) {
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400329 cinfo->dct_method = JDCT_FLOAT;
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000330 } else
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400331 usage();
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000332
333 } else if (keymatch(arg, "debug", 1) || keymatch(arg, "verbose", 1)) {
334 /* Enable debug printouts. */
335 /* On first -d, print version identification */
336 static boolean printed_version = FALSE;
337
Chris Blumecca8c4d2019-03-01 01:09:50 -0800338 if (!printed_version) {
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400339 fprintf(stderr, "%s version %s (build %s)\n",
340 PACKAGE_NAME, VERSION, BUILD);
341 fprintf(stderr, "%s\n\n", JCOPYRIGHT);
342 fprintf(stderr, "Emulating The Independent JPEG Group's software, version %s\n\n",
343 JVERSION);
344 printed_version = TRUE;
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000345 }
346 cinfo->err->trace_level++;
347
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400348 } else if (keymatch(arg, "version", 4)) {
349 fprintf(stderr, "%s version %s (build %s)\n",
350 PACKAGE_NAME, VERSION, BUILD);
351 exit(EXIT_SUCCESS);
352
Chris Blumecca8c4d2019-03-01 01:09:50 -0800353 } else if (keymatch(arg, "grayscale", 2) ||
354 keymatch(arg, "greyscale", 2)) {
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000355 /* Force a monochrome JPEG file to be generated. */
356 jpeg_set_colorspace(cinfo, JCS_GRAYSCALE);
357
noel@chromium.org3395bcc2014-04-14 06:56:00 +0000358 } else if (keymatch(arg, "rgb", 3)) {
359 /* Force an RGB JPEG file to be generated. */
360 jpeg_set_colorspace(cinfo, JCS_RGB);
361
Chris Blumecca8c4d2019-03-01 01:09:50 -0800362 } else if (keymatch(arg, "icc", 1)) {
363 /* Set ICC filename. */
364 if (++argn >= argc) /* advance to next argument */
365 usage();
366 icc_filename = argv[argn];
367
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000368 } else if (keymatch(arg, "maxmemory", 3)) {
369 /* Maximum memory in Kb (or Mb with 'm'). */
370 long lval;
371 char ch = 'x';
372
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400373 if (++argn >= argc) /* advance to next argument */
374 usage();
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000375 if (sscanf(argv[argn], "%ld%c", &lval, &ch) < 1)
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400376 usage();
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000377 if (ch == 'm' || ch == 'M')
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400378 lval *= 1000L;
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000379 cinfo->mem->max_memory_to_use = lval * 1000L;
380
381 } else if (keymatch(arg, "optimize", 1) || keymatch(arg, "optimise", 1)) {
382 /* Enable entropy parm optimization. */
383#ifdef ENTROPY_OPT_SUPPORTED
384 cinfo->optimize_coding = TRUE;
385#else
noel@chromium.org3395bcc2014-04-14 06:56:00 +0000386 fprintf(stderr, "%s: sorry, entropy optimization was not compiled in\n",
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400387 progname);
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000388 exit(EXIT_FAILURE);
389#endif
390
391 } else if (keymatch(arg, "outfile", 4)) {
392 /* Set output file name. */
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400393 if (++argn >= argc) /* advance to next argument */
394 usage();
395 outfilename = argv[argn]; /* save it away for later use */
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000396
397 } else if (keymatch(arg, "progressive", 1)) {
398 /* Select simple progressive mode. */
399#ifdef C_PROGRESSIVE_SUPPORTED
400 simple_progressive = TRUE;
401 /* We must postpone execution until num_components is known. */
402#else
noel@chromium.org3395bcc2014-04-14 06:56:00 +0000403 fprintf(stderr, "%s: sorry, progressive output was not compiled in\n",
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400404 progname);
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000405 exit(EXIT_FAILURE);
406#endif
407
noel@chromium.org3395bcc2014-04-14 06:56:00 +0000408 } else if (keymatch(arg, "memdst", 2)) {
409 /* Use in-memory destination manager */
410#if JPEG_LIB_VERSION >= 80 || defined(MEM_SRCDST_SUPPORTED)
411 memdst = TRUE;
412#else
413 fprintf(stderr, "%s: sorry, in-memory destination manager was not compiled in\n",
414 progname);
415 exit(EXIT_FAILURE);
416#endif
417
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000418 } else if (keymatch(arg, "quality", 1)) {
hbono@chromium.org98626972011-08-03 03:13:08 +0000419 /* Quality ratings (quantization table scaling factors). */
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400420 if (++argn >= argc) /* advance to next argument */
421 usage();
hbono@chromium.org98626972011-08-03 03:13:08 +0000422 qualityarg = argv[argn];
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000423
424 } else if (keymatch(arg, "qslots", 2)) {
425 /* Quantization table slot numbers. */
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400426 if (++argn >= argc) /* advance to next argument */
427 usage();
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000428 qslotsarg = argv[argn];
429 /* Must delay setting qslots until after we have processed any
430 * colorspace-determining switches, since jpeg_set_colorspace sets
431 * default quant table numbers.
432 */
433
434 } else if (keymatch(arg, "qtables", 2)) {
435 /* Quantization tables fetched from file. */
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400436 if (++argn >= argc) /* advance to next argument */
437 usage();
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000438 qtablefile = argv[argn];
439 /* We postpone actually reading the file in case -quality comes later. */
440
Jonathan Wrightbbb82822020-11-25 13:36:43 +0000441 } else if (keymatch(arg, "report", 3)) {
442 report = TRUE;
443
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000444 } else if (keymatch(arg, "restart", 1)) {
445 /* Restart interval in MCU rows (or in MCUs with 'b'). */
446 long lval;
447 char ch = 'x';
448
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400449 if (++argn >= argc) /* advance to next argument */
450 usage();
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000451 if (sscanf(argv[argn], "%ld%c", &lval, &ch) < 1)
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400452 usage();
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000453 if (lval < 0 || lval > 65535L)
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400454 usage();
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000455 if (ch == 'b' || ch == 'B') {
Chris Blumecca8c4d2019-03-01 01:09:50 -0800456 cinfo->restart_interval = (unsigned int)lval;
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400457 cinfo->restart_in_rows = 0; /* else prior '-restart n' overrides me */
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000458 } else {
Chris Blumecca8c4d2019-03-01 01:09:50 -0800459 cinfo->restart_in_rows = (int)lval;
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400460 /* restart_interval will be computed during startup */
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000461 }
462
463 } else if (keymatch(arg, "sample", 2)) {
464 /* Set sampling factors. */
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400465 if (++argn >= argc) /* advance to next argument */
466 usage();
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000467 samplearg = argv[argn];
468 /* Must delay setting sample factors until after we have processed any
469 * colorspace-determining switches, since jpeg_set_colorspace sets
470 * default sampling factors.
471 */
472
hbono@chromium.org98626972011-08-03 03:13:08 +0000473 } else if (keymatch(arg, "scans", 4)) {
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000474 /* Set scan script. */
475#ifdef C_MULTISCAN_FILES_SUPPORTED
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400476 if (++argn >= argc) /* advance to next argument */
477 usage();
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000478 scansarg = argv[argn];
479 /* We must postpone reading the file in case -progressive appears. */
480#else
noel@chromium.org3395bcc2014-04-14 06:56:00 +0000481 fprintf(stderr, "%s: sorry, multi-scan output was not compiled in\n",
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400482 progname);
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000483 exit(EXIT_FAILURE);
484#endif
485
486 } else if (keymatch(arg, "smooth", 2)) {
487 /* Set input smoothing factor. */
488 int val;
489
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400490 if (++argn >= argc) /* advance to next argument */
491 usage();
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000492 if (sscanf(argv[argn], "%d", &val) != 1)
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400493 usage();
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000494 if (val < 0 || val > 100)
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400495 usage();
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000496 cinfo->smoothing_factor = val;
497
Jonathan Wright02959c32021-11-22 10:27:51 +0000498 } else if (keymatch(arg, "strict", 2)) {
499 strict = TRUE;
500
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000501 } else if (keymatch(arg, "targa", 1)) {
502 /* Input file is Targa format. */
503 is_targa = TRUE;
504
505 } else {
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400506 usage(); /* bogus switch */
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000507 }
508 }
509
510 /* Post-switch-scanning cleanup */
511
512 if (for_real) {
513
514 /* Set quantization tables for selected quality. */
515 /* Some or all may be overridden if -qtables is present. */
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400516 if (qualityarg != NULL) /* process -quality if it was present */
Chris Blumecca8c4d2019-03-01 01:09:50 -0800517 if (!set_quality_ratings(cinfo, qualityarg, force_baseline))
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400518 usage();
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000519
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400520 if (qtablefile != NULL) /* process -qtables if it was present */
Chris Blumecca8c4d2019-03-01 01:09:50 -0800521 if (!read_quant_tables(cinfo, qtablefile, force_baseline))
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400522 usage();
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000523
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400524 if (qslotsarg != NULL) /* process -qslots if it was present */
Chris Blumecca8c4d2019-03-01 01:09:50 -0800525 if (!set_quant_slots(cinfo, qslotsarg))
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400526 usage();
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000527
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400528 if (samplearg != NULL) /* process -sample if it was present */
Chris Blumecca8c4d2019-03-01 01:09:50 -0800529 if (!set_sample_factors(cinfo, samplearg))
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400530 usage();
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000531
532#ifdef C_PROGRESSIVE_SUPPORTED
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400533 if (simple_progressive) /* process -progressive; -scans can override */
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000534 jpeg_simple_progression(cinfo);
535#endif
536
537#ifdef C_MULTISCAN_FILES_SUPPORTED
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400538 if (scansarg != NULL) /* process -scans if it was present */
Chris Blumecca8c4d2019-03-01 01:09:50 -0800539 if (!read_scan_script(cinfo, scansarg))
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400540 usage();
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000541#endif
542 }
543
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400544 return argn; /* return index of next arg (file name) */
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000545}
546
547
Jonathan Wright02959c32021-11-22 10:27:51 +0000548METHODDEF(void)
549my_emit_message(j_common_ptr cinfo, int msg_level)
550{
551 if (msg_level < 0) {
552 /* Treat warning as fatal */
553 cinfo->err->error_exit(cinfo);
554 } else {
555 if (cinfo->err->trace_level >= msg_level)
556 cinfo->err->output_message(cinfo);
557 }
558}
559
560
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000561/*
562 * The main program.
563 */
564
565int
Jonathan Wright5961ab92020-06-21 13:11:49 +0100566#ifdef GTEST
567cjpeg(int argc, char **argv)
568#else
Chris Blumecca8c4d2019-03-01 01:09:50 -0800569main(int argc, char **argv)
Jonathan Wright5961ab92020-06-21 13:11:49 +0100570#endif
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000571{
572 struct jpeg_compress_struct cinfo;
Jonathan Wright24e31052021-04-26 12:10:48 +0100573#ifdef CJPEG_FUZZER
574 struct my_error_mgr myerr;
575 struct jpeg_error_mgr &jerr = myerr.pub;
576#else
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000577 struct jpeg_error_mgr jerr;
Jonathan Wright24e31052021-04-26 12:10:48 +0100578#endif
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000579 struct cdjpeg_progress_mgr progress;
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000580 int file_index;
581 cjpeg_source_ptr src_mgr;
Jonathan Wright24e31052021-04-26 12:10:48 +0100582 FILE *input_file = NULL;
Chris Blumecca8c4d2019-03-01 01:09:50 -0800583 FILE *icc_file;
584 JOCTET *icc_profile = NULL;
585 long icc_len = 0;
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400586 FILE *output_file = NULL;
noel@chromium.org3395bcc2014-04-14 06:56:00 +0000587 unsigned char *outbuffer = NULL;
588 unsigned long outsize = 0;
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000589 JDIMENSION num_scanlines;
590
591 /* On Mac, fetch a command line. */
592#ifdef USE_CCOMMAND
593 argc = ccommand(&argv);
594#endif
595
596 progname = argv[0];
597 if (progname == NULL || progname[0] == 0)
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400598 progname = "cjpeg"; /* in case C library doesn't provide it */
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000599
600 /* Initialize the JPEG compression object with default error handling. */
601 cinfo.err = jpeg_std_error(&jerr);
602 jpeg_create_compress(&cinfo);
603 /* Add some application-specific error messages (from cderror.h) */
604 jerr.addon_message_table = cdjpeg_message_table;
605 jerr.first_addon_message = JMSG_FIRSTADDONCODE;
606 jerr.last_addon_message = JMSG_LASTADDONCODE;
607
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000608 /* Initialize JPEG parameters.
609 * Much of this may be overridden later.
610 * In particular, we don't yet know the input file's color space,
611 * but we need to provide some value for jpeg_set_defaults() to work.
612 */
613
614 cinfo.in_color_space = JCS_RGB; /* arbitrary guess */
615 jpeg_set_defaults(&cinfo);
616
617 /* Scan command line to find file names.
618 * It is convenient to use just one switch-parsing routine, but the switch
619 * values read here are ignored; we will rescan the switches after opening
620 * the input file.
621 */
622
623 file_index = parse_switches(&cinfo, argc, argv, 0, FALSE);
624
Jonathan Wright02959c32021-11-22 10:27:51 +0000625 if (strict)
626 jerr.emit_message = my_emit_message;
627
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000628#ifdef TWO_FILE_COMMANDLINE
noel@chromium.org3395bcc2014-04-14 06:56:00 +0000629 if (!memdst) {
630 /* Must have either -outfile switch or explicit output file name */
631 if (outfilename == NULL) {
Chris Blumecca8c4d2019-03-01 01:09:50 -0800632 if (file_index != argc - 2) {
noel@chromium.org3395bcc2014-04-14 06:56:00 +0000633 fprintf(stderr, "%s: must name one input and one output file\n",
634 progname);
635 usage();
636 }
Chris Blumecca8c4d2019-03-01 01:09:50 -0800637 outfilename = argv[file_index + 1];
noel@chromium.org3395bcc2014-04-14 06:56:00 +0000638 } else {
Chris Blumecca8c4d2019-03-01 01:09:50 -0800639 if (file_index != argc - 1) {
noel@chromium.org3395bcc2014-04-14 06:56:00 +0000640 fprintf(stderr, "%s: must name one input and one output file\n",
641 progname);
642 usage();
643 }
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000644 }
645 }
646#else
647 /* Unix style: expect zero or one file name */
Chris Blumecca8c4d2019-03-01 01:09:50 -0800648 if (file_index < argc - 1) {
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000649 fprintf(stderr, "%s: only one input file\n", progname);
650 usage();
651 }
652#endif /* TWO_FILE_COMMANDLINE */
653
654 /* Open the input file. */
655 if (file_index < argc) {
656 if ((input_file = fopen(argv[file_index], READ_BINARY)) == NULL) {
657 fprintf(stderr, "%s: can't open %s\n", progname, argv[file_index]);
Jonathan Wright5961ab92020-06-21 13:11:49 +0100658 return EXIT_FAILURE;
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000659 }
660 } else {
661 /* default input file is stdin */
662 input_file = read_stdin();
663 }
664
665 /* Open the output file. */
666 if (outfilename != NULL) {
667 if ((output_file = fopen(outfilename, WRITE_BINARY)) == NULL) {
668 fprintf(stderr, "%s: can't open %s\n", progname, outfilename);
Jonathan Wright5961ab92020-06-21 13:11:49 +0100669 return EXIT_FAILURE;
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000670 }
noel@chromium.org3395bcc2014-04-14 06:56:00 +0000671 } else if (!memdst) {
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000672 /* default output file is stdout */
673 output_file = write_stdout();
674 }
675
Chris Blumecca8c4d2019-03-01 01:09:50 -0800676 if (icc_filename != NULL) {
677 if ((icc_file = fopen(icc_filename, READ_BINARY)) == NULL) {
678 fprintf(stderr, "%s: can't open %s\n", progname, icc_filename);
Jonathan Wright5961ab92020-06-21 13:11:49 +0100679 return EXIT_FAILURE;
Chris Blumecca8c4d2019-03-01 01:09:50 -0800680 }
681 if (fseek(icc_file, 0, SEEK_END) < 0 ||
682 (icc_len = ftell(icc_file)) < 1 ||
683 fseek(icc_file, 0, SEEK_SET) < 0) {
684 fprintf(stderr, "%s: can't determine size of %s\n", progname,
685 icc_filename);
Jonathan Wright5961ab92020-06-21 13:11:49 +0100686 return EXIT_FAILURE;
Chris Blumecca8c4d2019-03-01 01:09:50 -0800687 }
688 if ((icc_profile = (JOCTET *)malloc(icc_len)) == NULL) {
689 fprintf(stderr, "%s: can't allocate memory for ICC profile\n", progname);
690 fclose(icc_file);
Jonathan Wright5961ab92020-06-21 13:11:49 +0100691 return EXIT_FAILURE;
Chris Blumecca8c4d2019-03-01 01:09:50 -0800692 }
693 if (fread(icc_profile, icc_len, 1, icc_file) < 1) {
694 fprintf(stderr, "%s: can't read ICC profile from %s\n", progname,
695 icc_filename);
696 free(icc_profile);
697 fclose(icc_file);
Jonathan Wright5961ab92020-06-21 13:11:49 +0100698 return EXIT_FAILURE;
Chris Blumecca8c4d2019-03-01 01:09:50 -0800699 }
700 fclose(icc_file);
701 }
702
Jonathan Wright24e31052021-04-26 12:10:48 +0100703#ifdef CJPEG_FUZZER
704 jerr.error_exit = my_error_exit;
Jonathan Wright22f1a222022-03-01 15:53:34 +0000705 jerr.emit_message = my_emit_message_fuzzer;
Jonathan Wright24e31052021-04-26 12:10:48 +0100706 if (setjmp(myerr.setjmp_buffer))
707 HANDLE_ERROR()
708#endif
709
Jonathan Wrightbbb82822020-11-25 13:36:43 +0000710 if (report) {
711 start_progress_monitor((j_common_ptr)&cinfo, &progress);
712 progress.report = report;
713 }
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000714
715 /* Figure out the input file format, and set up to read it. */
716 src_mgr = select_file_type(&cinfo, input_file);
717 src_mgr->input_file = input_file;
Jonathan Wright24e31052021-04-26 12:10:48 +0100718#ifdef CJPEG_FUZZER
719 src_mgr->max_pixels = 1048576;
720#endif
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000721
722 /* Read the input file header to obtain file size & colorspace. */
723 (*src_mgr->start_input) (&cinfo, src_mgr);
724
725 /* Now that we know input colorspace, fix colorspace-dependent defaults */
726 jpeg_default_colorspace(&cinfo);
727
728 /* Adjust default compression parameters by re-parsing the options */
729 file_index = parse_switches(&cinfo, argc, argv, 0, TRUE);
730
731 /* Specify data destination for compression */
noel@chromium.org3395bcc2014-04-14 06:56:00 +0000732#if JPEG_LIB_VERSION >= 80 || defined(MEM_SRCDST_SUPPORTED)
733 if (memdst)
734 jpeg_mem_dest(&cinfo, &outbuffer, &outsize);
735 else
736#endif
737 jpeg_stdio_dest(&cinfo, output_file);
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000738
Jonathan Wright24e31052021-04-26 12:10:48 +0100739#ifdef CJPEG_FUZZER
740 if (setjmp(myerr.setjmp_buffer))
741 HANDLE_ERROR()
742#endif
743
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000744 /* Start compressor */
745 jpeg_start_compress(&cinfo, TRUE);
746
Chris Blumecca8c4d2019-03-01 01:09:50 -0800747 if (icc_profile != NULL)
748 jpeg_write_icc_profile(&cinfo, icc_profile, (unsigned int)icc_len);
749
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000750 /* Process data */
751 while (cinfo.next_scanline < cinfo.image_height) {
752 num_scanlines = (*src_mgr->get_pixel_rows) (&cinfo, src_mgr);
Chris Blumecca8c4d2019-03-01 01:09:50 -0800753 (void)jpeg_write_scanlines(&cinfo, src_mgr->buffer, num_scanlines);
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000754 }
755
756 /* Finish compression and release memory */
757 (*src_mgr->finish_input) (&cinfo, src_mgr);
758 jpeg_finish_compress(&cinfo);
759 jpeg_destroy_compress(&cinfo);
760
761 /* Close files, if we opened them */
762 if (input_file != stdin)
763 fclose(input_file);
noel@chromium.org3395bcc2014-04-14 06:56:00 +0000764 if (output_file != stdout && output_file != NULL)
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000765 fclose(output_file);
766
Jonathan Wrightbbb82822020-11-25 13:36:43 +0000767 if (report)
768 end_progress_monitor((j_common_ptr)&cinfo);
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000769
noel@chromium.org3395bcc2014-04-14 06:56:00 +0000770 if (memdst) {
Jonathan Wright24e31052021-04-26 12:10:48 +0100771#ifndef CJPEG_FUZZER
noel@chromium.org3395bcc2014-04-14 06:56:00 +0000772 fprintf(stderr, "Compressed size: %lu bytes\n", outsize);
Jonathan Wright24e31052021-04-26 12:10:48 +0100773#endif
Jonathan Wrightdb870df2020-08-05 11:42:22 +0100774 free(outbuffer);
noel@chromium.org3395bcc2014-04-14 06:56:00 +0000775 }
776
Jonathan Wrightdb870df2020-08-05 11:42:22 +0100777 free(icc_profile);
Chris Blumecca8c4d2019-03-01 01:09:50 -0800778
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000779 /* All done. */
Jonathan Wright5961ab92020-06-21 13:11:49 +0100780 return (jerr.num_warnings ? EXIT_WARNING : EXIT_SUCCESS);
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000781}