hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 1 | /* |
| 2 | * cjpeg.c |
| 3 | * |
noel@chromium.org | 3395bcc | 2014-04-14 06:56:00 +0000 | [diff] [blame] | 4 | * This file was part of the Independent JPEG Group's software: |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 5 | * Copyright (C) 1991-1998, Thomas G. Lane. |
noel@chromium.org | 3395bcc | 2014-04-14 06:56:00 +0000 | [diff] [blame] | 6 | * Modified 2003-2011 by Guido Vollbeding. |
| 7 | * libjpeg-turbo Modifications: |
Jonathan Wright | 22f1a22 | 2022-03-01 15:53:34 +0000 | [diff] [blame] | 8 | * Copyright (C) 2010, 2013-2014, 2017, 2019-2022, D. R. Commander. |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame] | 9 | * For conditions of distribution and use, see the accompanying README.ijg |
| 10 | * file. |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 11 | * |
| 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 Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame] | 17 | * cjpeg [options] inputfile outputfile |
| 18 | * cjpeg [options] [inputfile] |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 19 | * 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 Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame] | 26 | * cjpeg [options] -outfile outputfile inputfile |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 27 | * works regardless of which command line style is used. |
| 28 | */ |
| 29 | |
Jonathan Wright | 22f1a22 | 2022-03-01 15:53:34 +0000 | [diff] [blame] | 30 | #ifdef _MSC_VER |
| 31 | #define _CRT_SECURE_NO_DEPRECATE |
| 32 | #endif |
| 33 | |
Jonathan Wright | 24e3105 | 2021-04-26 12:10:48 +0100 | [diff] [blame] | 34 | #ifdef CJPEG_FUZZER |
| 35 | #define JPEG_INTERNALS |
| 36 | #endif |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame] | 37 | #include "cdjpeg.h" /* Common decls for cjpeg/djpeg applications */ |
| 38 | #include "jversion.h" /* for version message */ |
| 39 | #include "jconfigint.h" |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 40 | |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 41 | |
| 42 | /* Create the add-on message string table. */ |
| 43 | |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 44 | #define JMESSAGE(code, string) string, |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 45 | |
| 46 | static const char * const cdjpeg_message_table[] = { |
| 47 | #include "cderror.h" |
| 48 | NULL |
| 49 | }; |
| 50 | |
| 51 | |
| 52 | /* |
| 53 | * This routine determines what format the input file is, |
| 54 | * and selects the appropriate input-reading module. |
| 55 | * |
| 56 | * To determine which family of input formats the file belongs to, |
| 57 | * we may look only at the first byte of the file, since C does not |
| 58 | * guarantee that more than one character can be pushed back with ungetc. |
| 59 | * Looking at additional bytes would require one of these approaches: |
| 60 | * 1) assume we can fseek() the input file (fails for piped input); |
| 61 | * 2) assume we can push back more than one character (works in |
| 62 | * some C implementations, but unportable); |
| 63 | * 3) provide our own buffering (breaks input readers that want to use |
Jonathan Wright | bbb8282 | 2020-11-25 13:36:43 +0000 | [diff] [blame] | 64 | * stdio directly); |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 65 | * or 4) don't put back the data, and modify the input_init methods to assume |
Jonathan Wright | bbb8282 | 2020-11-25 13:36:43 +0000 | [diff] [blame] | 66 | * they start reading after the start of file. |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 67 | * #1 is attractive for MS-DOS but is untenable on Unix. |
| 68 | * |
| 69 | * The most portable solution for file types that can't be identified by their |
| 70 | * first byte is to make the user tell us what they are. This is also the |
| 71 | * only approach for "raw" file types that contain only arbitrary values. |
| 72 | * We presently apply this method for Targa files. Most of the time Targa |
| 73 | * files start with 0x00, so we recognize that case. Potentially, however, |
| 74 | * a Targa file could start with any byte value (byte 0 is the length of the |
| 75 | * seldom-used ID field), so we provide a switch to force Targa input mode. |
| 76 | */ |
| 77 | |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame] | 78 | static boolean is_targa; /* records user -targa switch */ |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 79 | |
| 80 | |
| 81 | LOCAL(cjpeg_source_ptr) |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 82 | select_file_type(j_compress_ptr cinfo, FILE *infile) |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 83 | { |
| 84 | int c; |
| 85 | |
| 86 | if (is_targa) { |
| 87 | #ifdef TARGA_SUPPORTED |
| 88 | return jinit_read_targa(cinfo); |
| 89 | #else |
| 90 | ERREXIT(cinfo, JERR_TGA_NOTCOMP); |
| 91 | #endif |
| 92 | } |
| 93 | |
| 94 | if ((c = getc(infile)) == EOF) |
| 95 | ERREXIT(cinfo, JERR_INPUT_EMPTY); |
| 96 | if (ungetc(c, infile) == EOF) |
| 97 | ERREXIT(cinfo, JERR_UNGETC_FAILED); |
| 98 | |
| 99 | switch (c) { |
| 100 | #ifdef BMP_SUPPORTED |
| 101 | case 'B': |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 102 | return jinit_read_bmp(cinfo, TRUE); |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 103 | #endif |
| 104 | #ifdef GIF_SUPPORTED |
| 105 | case 'G': |
| 106 | return jinit_read_gif(cinfo); |
| 107 | #endif |
| 108 | #ifdef PPM_SUPPORTED |
| 109 | case 'P': |
| 110 | return jinit_read_ppm(cinfo); |
| 111 | #endif |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 112 | #ifdef TARGA_SUPPORTED |
| 113 | case 0x00: |
| 114 | return jinit_read_targa(cinfo); |
| 115 | #endif |
| 116 | default: |
| 117 | ERREXIT(cinfo, JERR_UNKNOWN_FORMAT); |
| 118 | break; |
| 119 | } |
| 120 | |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame] | 121 | return NULL; /* suppress compiler warnings */ |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | |
| 125 | /* |
| 126 | * Argument-parsing code. |
| 127 | * The switch parser is designed to be useful with DOS-style command line |
| 128 | * syntax, ie, intermixed switches and file names, where only the switches |
| 129 | * to the left of a given file name affect processing of that file. |
| 130 | * The main program in this file doesn't actually use this capability... |
| 131 | */ |
| 132 | |
| 133 | |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame] | 134 | static const char *progname; /* program name for error messages */ |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 135 | static char *icc_filename; /* for -icc switch */ |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame] | 136 | static char *outfilename; /* for -outfile switch */ |
Jonathan Wright | 24e3105 | 2021-04-26 12:10:48 +0100 | [diff] [blame] | 137 | boolean memdst; /* for -memdst switch */ |
| 138 | boolean report; /* for -report switch */ |
Jonathan Wright | 02959c3 | 2021-11-22 10:27:51 +0000 | [diff] [blame] | 139 | boolean strict; /* for -strict switch */ |
Jonathan Wright | 24e3105 | 2021-04-26 12:10:48 +0100 | [diff] [blame] | 140 | |
| 141 | |
| 142 | #ifdef CJPEG_FUZZER |
| 143 | |
| 144 | #include <setjmp.h> |
| 145 | |
| 146 | struct my_error_mgr { |
| 147 | struct jpeg_error_mgr pub; |
| 148 | jmp_buf setjmp_buffer; |
| 149 | }; |
| 150 | |
| 151 | void my_error_exit(j_common_ptr cinfo) |
| 152 | { |
| 153 | struct my_error_mgr *myerr = (struct my_error_mgr *)cinfo->err; |
| 154 | |
| 155 | longjmp(myerr->setjmp_buffer, 1); |
| 156 | } |
| 157 | |
Jonathan Wright | 22f1a22 | 2022-03-01 15:53:34 +0000 | [diff] [blame] | 158 | static void my_emit_message_fuzzer(j_common_ptr cinfo, int msg_level) |
Jonathan Wright | 24e3105 | 2021-04-26 12:10:48 +0100 | [diff] [blame] | 159 | { |
| 160 | if (msg_level < 0) |
| 161 | cinfo->err->num_warnings++; |
| 162 | } |
| 163 | |
| 164 | #define HANDLE_ERROR() { \ |
| 165 | if (cinfo.global_state > CSTATE_START) { \ |
| 166 | if (memdst && outbuffer) \ |
| 167 | (*cinfo.dest->term_destination) (&cinfo); \ |
| 168 | jpeg_abort_compress(&cinfo); \ |
| 169 | } \ |
| 170 | jpeg_destroy_compress(&cinfo); \ |
| 171 | if (input_file != stdin && input_file != NULL) \ |
| 172 | fclose(input_file); \ |
| 173 | if (memdst) \ |
| 174 | free(outbuffer); \ |
| 175 | return EXIT_FAILURE; \ |
| 176 | } |
| 177 | |
| 178 | #endif |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 179 | |
| 180 | |
| 181 | LOCAL(void) |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 182 | usage(void) |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 183 | /* complain about bad command line */ |
| 184 | { |
| 185 | fprintf(stderr, "usage: %s [switches] ", progname); |
| 186 | #ifdef TWO_FILE_COMMANDLINE |
| 187 | fprintf(stderr, "inputfile outputfile\n"); |
| 188 | #else |
| 189 | fprintf(stderr, "[inputfile]\n"); |
| 190 | #endif |
| 191 | |
| 192 | fprintf(stderr, "Switches (names may be abbreviated):\n"); |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame] | 193 | fprintf(stderr, " -quality N[,...] Compression quality (0..100; 5-95 is most useful range,\n"); |
| 194 | fprintf(stderr, " default is 75)\n"); |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 195 | fprintf(stderr, " -grayscale Create monochrome JPEG file\n"); |
noel@chromium.org | 3395bcc | 2014-04-14 06:56:00 +0000 | [diff] [blame] | 196 | fprintf(stderr, " -rgb Create RGB JPEG file\n"); |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 197 | #ifdef ENTROPY_OPT_SUPPORTED |
| 198 | fprintf(stderr, " -optimize Optimize Huffman table (smaller file, but slow compression)\n"); |
| 199 | #endif |
| 200 | #ifdef C_PROGRESSIVE_SUPPORTED |
| 201 | fprintf(stderr, " -progressive Create progressive JPEG file\n"); |
| 202 | #endif |
| 203 | #ifdef TARGA_SUPPORTED |
| 204 | fprintf(stderr, " -targa Input file is Targa format (usually not needed)\n"); |
| 205 | #endif |
| 206 | fprintf(stderr, "Switches for advanced users:\n"); |
hbono@chromium.org | df5ffdd | 2012-05-11 07:46:03 +0000 | [diff] [blame] | 207 | #ifdef C_ARITH_CODING_SUPPORTED |
| 208 | fprintf(stderr, " -arithmetic Use arithmetic coding\n"); |
| 209 | #endif |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 210 | #ifdef DCT_ISLOW_SUPPORTED |
Jonathan Wright | bbb8282 | 2020-11-25 13:36:43 +0000 | [diff] [blame] | 211 | fprintf(stderr, " -dct int Use accurate integer DCT method%s\n", |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame] | 212 | (JDCT_DEFAULT == JDCT_ISLOW ? " (default)" : "")); |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 213 | #endif |
| 214 | #ifdef DCT_IFAST_SUPPORTED |
Jonathan Wright | bbb8282 | 2020-11-25 13:36:43 +0000 | [diff] [blame] | 215 | fprintf(stderr, " -dct fast Use less accurate integer DCT method [legacy feature]%s\n", |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame] | 216 | (JDCT_DEFAULT == JDCT_IFAST ? " (default)" : "")); |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 217 | #endif |
| 218 | #ifdef DCT_FLOAT_SUPPORTED |
Jonathan Wright | bbb8282 | 2020-11-25 13:36:43 +0000 | [diff] [blame] | 219 | fprintf(stderr, " -dct float Use floating-point DCT method [legacy feature]%s\n", |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame] | 220 | (JDCT_DEFAULT == JDCT_FLOAT ? " (default)" : "")); |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 221 | #endif |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 222 | fprintf(stderr, " -icc FILE Embed ICC profile contained in FILE\n"); |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 223 | fprintf(stderr, " -restart N Set restart interval in rows, or in blocks with B\n"); |
| 224 | #ifdef INPUT_SMOOTHING_SUPPORTED |
| 225 | fprintf(stderr, " -smooth N Smooth dithered input (N=1..100 is strength)\n"); |
| 226 | #endif |
| 227 | fprintf(stderr, " -maxmemory N Maximum memory to use (in kbytes)\n"); |
| 228 | fprintf(stderr, " -outfile name Specify name for output file\n"); |
noel@chromium.org | 3395bcc | 2014-04-14 06:56:00 +0000 | [diff] [blame] | 229 | #if JPEG_LIB_VERSION >= 80 || defined(MEM_SRCDST_SUPPORTED) |
| 230 | fprintf(stderr, " -memdst Compress to memory instead of file (useful for benchmarking)\n"); |
| 231 | #endif |
Jonathan Wright | bbb8282 | 2020-11-25 13:36:43 +0000 | [diff] [blame] | 232 | fprintf(stderr, " -report Report compression progress\n"); |
Jonathan Wright | 02959c3 | 2021-11-22 10:27:51 +0000 | [diff] [blame] | 233 | fprintf(stderr, " -strict Treat all warnings as fatal\n"); |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 234 | fprintf(stderr, " -verbose or -debug Emit debug output\n"); |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame] | 235 | fprintf(stderr, " -version Print version information and exit\n"); |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 236 | fprintf(stderr, "Switches for wizards:\n"); |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 237 | fprintf(stderr, " -baseline Force baseline quantization tables\n"); |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 238 | fprintf(stderr, " -qtables FILE Use quantization tables given in FILE\n"); |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 239 | fprintf(stderr, " -qslots N[,...] Set component quantization tables\n"); |
| 240 | fprintf(stderr, " -sample HxV[,...] Set component sampling factors\n"); |
| 241 | #ifdef C_MULTISCAN_FILES_SUPPORTED |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 242 | fprintf(stderr, " -scans FILE Create multi-scan JPEG per script FILE\n"); |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 243 | #endif |
| 244 | exit(EXIT_FAILURE); |
| 245 | } |
| 246 | |
| 247 | |
| 248 | LOCAL(int) |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 249 | parse_switches(j_compress_ptr cinfo, int argc, char **argv, |
| 250 | int last_file_arg_seen, boolean for_real) |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 251 | /* Parse optional switches. |
| 252 | * Returns argv[] index of first file-name argument (== argc if none). |
| 253 | * Any file names with indexes <= last_file_arg_seen are ignored; |
| 254 | * they have presumably been processed in a previous iteration. |
| 255 | * (Pass 0 for last_file_arg_seen on the first or only iteration.) |
| 256 | * for_real is FALSE on the first (dummy) pass; we may skip any expensive |
| 257 | * processing. |
| 258 | */ |
| 259 | { |
| 260 | int argn; |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame] | 261 | char *arg; |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 262 | boolean force_baseline; |
| 263 | boolean simple_progressive; |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame] | 264 | char *qualityarg = NULL; /* saves -quality parm if any */ |
| 265 | char *qtablefile = NULL; /* saves -qtables filename if any */ |
| 266 | char *qslotsarg = NULL; /* saves -qslots parm if any */ |
| 267 | char *samplearg = NULL; /* saves -sample parm if any */ |
| 268 | char *scansarg = NULL; /* saves -scans parm if any */ |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 269 | |
| 270 | /* Set up default JPEG parameters. */ |
hbono@chromium.org | 9862697 | 2011-08-03 03:13:08 +0000 | [diff] [blame] | 271 | |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame] | 272 | force_baseline = FALSE; /* by default, allow 16-bit quantizers */ |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 273 | simple_progressive = FALSE; |
| 274 | is_targa = FALSE; |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 275 | icc_filename = NULL; |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 276 | outfilename = NULL; |
noel@chromium.org | 3395bcc | 2014-04-14 06:56:00 +0000 | [diff] [blame] | 277 | memdst = FALSE; |
Jonathan Wright | bbb8282 | 2020-11-25 13:36:43 +0000 | [diff] [blame] | 278 | report = FALSE; |
Jonathan Wright | 02959c3 | 2021-11-22 10:27:51 +0000 | [diff] [blame] | 279 | strict = FALSE; |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 280 | cinfo->err->trace_level = 0; |
| 281 | |
| 282 | /* Scan command line options, adjust parameters */ |
| 283 | |
| 284 | for (argn = 1; argn < argc; argn++) { |
| 285 | arg = argv[argn]; |
| 286 | if (*arg != '-') { |
| 287 | /* Not a switch, must be a file name argument */ |
| 288 | if (argn <= last_file_arg_seen) { |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame] | 289 | outfilename = NULL; /* -outfile applies to just one input file */ |
| 290 | continue; /* ignore this name if previously processed */ |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 291 | } |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame] | 292 | break; /* else done parsing switches */ |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 293 | } |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame] | 294 | arg++; /* advance past switch marker character */ |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 295 | |
| 296 | if (keymatch(arg, "arithmetic", 1)) { |
| 297 | /* Use arithmetic coding. */ |
| 298 | #ifdef C_ARITH_CODING_SUPPORTED |
| 299 | cinfo->arith_code = TRUE; |
| 300 | #else |
| 301 | fprintf(stderr, "%s: sorry, arithmetic coding not supported\n", |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame] | 302 | progname); |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 303 | exit(EXIT_FAILURE); |
| 304 | #endif |
| 305 | |
| 306 | } else if (keymatch(arg, "baseline", 1)) { |
| 307 | /* Force baseline-compatible output (8-bit quantizer values). */ |
| 308 | force_baseline = TRUE; |
| 309 | |
| 310 | } else if (keymatch(arg, "dct", 2)) { |
| 311 | /* Select DCT algorithm. */ |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame] | 312 | if (++argn >= argc) /* advance to next argument */ |
| 313 | usage(); |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 314 | if (keymatch(argv[argn], "int", 1)) { |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame] | 315 | cinfo->dct_method = JDCT_ISLOW; |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 316 | } else if (keymatch(argv[argn], "fast", 2)) { |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame] | 317 | cinfo->dct_method = JDCT_IFAST; |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 318 | } else if (keymatch(argv[argn], "float", 2)) { |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame] | 319 | cinfo->dct_method = JDCT_FLOAT; |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 320 | } else |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame] | 321 | usage(); |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 322 | |
| 323 | } else if (keymatch(arg, "debug", 1) || keymatch(arg, "verbose", 1)) { |
| 324 | /* Enable debug printouts. */ |
| 325 | /* On first -d, print version identification */ |
| 326 | static boolean printed_version = FALSE; |
| 327 | |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 328 | if (!printed_version) { |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame] | 329 | fprintf(stderr, "%s version %s (build %s)\n", |
| 330 | PACKAGE_NAME, VERSION, BUILD); |
| 331 | fprintf(stderr, "%s\n\n", JCOPYRIGHT); |
| 332 | fprintf(stderr, "Emulating The Independent JPEG Group's software, version %s\n\n", |
| 333 | JVERSION); |
| 334 | printed_version = TRUE; |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 335 | } |
| 336 | cinfo->err->trace_level++; |
| 337 | |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame] | 338 | } else if (keymatch(arg, "version", 4)) { |
| 339 | fprintf(stderr, "%s version %s (build %s)\n", |
| 340 | PACKAGE_NAME, VERSION, BUILD); |
| 341 | exit(EXIT_SUCCESS); |
| 342 | |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 343 | } else if (keymatch(arg, "grayscale", 2) || |
| 344 | keymatch(arg, "greyscale", 2)) { |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 345 | /* Force a monochrome JPEG file to be generated. */ |
| 346 | jpeg_set_colorspace(cinfo, JCS_GRAYSCALE); |
| 347 | |
noel@chromium.org | 3395bcc | 2014-04-14 06:56:00 +0000 | [diff] [blame] | 348 | } else if (keymatch(arg, "rgb", 3)) { |
| 349 | /* Force an RGB JPEG file to be generated. */ |
| 350 | jpeg_set_colorspace(cinfo, JCS_RGB); |
| 351 | |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 352 | } else if (keymatch(arg, "icc", 1)) { |
| 353 | /* Set ICC filename. */ |
| 354 | if (++argn >= argc) /* advance to next argument */ |
| 355 | usage(); |
| 356 | icc_filename = argv[argn]; |
| 357 | |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 358 | } else if (keymatch(arg, "maxmemory", 3)) { |
| 359 | /* Maximum memory in Kb (or Mb with 'm'). */ |
| 360 | long lval; |
| 361 | char ch = 'x'; |
| 362 | |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame] | 363 | if (++argn >= argc) /* advance to next argument */ |
| 364 | usage(); |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 365 | if (sscanf(argv[argn], "%ld%c", &lval, &ch) < 1) |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame] | 366 | usage(); |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 367 | if (ch == 'm' || ch == 'M') |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame] | 368 | lval *= 1000L; |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 369 | cinfo->mem->max_memory_to_use = lval * 1000L; |
| 370 | |
| 371 | } else if (keymatch(arg, "optimize", 1) || keymatch(arg, "optimise", 1)) { |
| 372 | /* Enable entropy parm optimization. */ |
| 373 | #ifdef ENTROPY_OPT_SUPPORTED |
| 374 | cinfo->optimize_coding = TRUE; |
| 375 | #else |
noel@chromium.org | 3395bcc | 2014-04-14 06:56:00 +0000 | [diff] [blame] | 376 | fprintf(stderr, "%s: sorry, entropy optimization was not compiled in\n", |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame] | 377 | progname); |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 378 | exit(EXIT_FAILURE); |
| 379 | #endif |
| 380 | |
| 381 | } else if (keymatch(arg, "outfile", 4)) { |
| 382 | /* Set output file name. */ |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame] | 383 | if (++argn >= argc) /* advance to next argument */ |
| 384 | usage(); |
| 385 | outfilename = argv[argn]; /* save it away for later use */ |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 386 | |
| 387 | } else if (keymatch(arg, "progressive", 1)) { |
| 388 | /* Select simple progressive mode. */ |
| 389 | #ifdef C_PROGRESSIVE_SUPPORTED |
| 390 | simple_progressive = TRUE; |
| 391 | /* We must postpone execution until num_components is known. */ |
| 392 | #else |
noel@chromium.org | 3395bcc | 2014-04-14 06:56:00 +0000 | [diff] [blame] | 393 | fprintf(stderr, "%s: sorry, progressive output was not compiled in\n", |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame] | 394 | progname); |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 395 | exit(EXIT_FAILURE); |
| 396 | #endif |
| 397 | |
noel@chromium.org | 3395bcc | 2014-04-14 06:56:00 +0000 | [diff] [blame] | 398 | } else if (keymatch(arg, "memdst", 2)) { |
| 399 | /* Use in-memory destination manager */ |
| 400 | #if JPEG_LIB_VERSION >= 80 || defined(MEM_SRCDST_SUPPORTED) |
| 401 | memdst = TRUE; |
| 402 | #else |
| 403 | fprintf(stderr, "%s: sorry, in-memory destination manager was not compiled in\n", |
| 404 | progname); |
| 405 | exit(EXIT_FAILURE); |
| 406 | #endif |
| 407 | |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 408 | } else if (keymatch(arg, "quality", 1)) { |
hbono@chromium.org | 9862697 | 2011-08-03 03:13:08 +0000 | [diff] [blame] | 409 | /* Quality ratings (quantization table scaling factors). */ |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame] | 410 | if (++argn >= argc) /* advance to next argument */ |
| 411 | usage(); |
hbono@chromium.org | 9862697 | 2011-08-03 03:13:08 +0000 | [diff] [blame] | 412 | qualityarg = argv[argn]; |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 413 | |
| 414 | } else if (keymatch(arg, "qslots", 2)) { |
| 415 | /* Quantization table slot numbers. */ |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame] | 416 | if (++argn >= argc) /* advance to next argument */ |
| 417 | usage(); |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 418 | qslotsarg = argv[argn]; |
| 419 | /* Must delay setting qslots until after we have processed any |
| 420 | * colorspace-determining switches, since jpeg_set_colorspace sets |
| 421 | * default quant table numbers. |
| 422 | */ |
| 423 | |
| 424 | } else if (keymatch(arg, "qtables", 2)) { |
| 425 | /* Quantization tables fetched from file. */ |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame] | 426 | if (++argn >= argc) /* advance to next argument */ |
| 427 | usage(); |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 428 | qtablefile = argv[argn]; |
| 429 | /* We postpone actually reading the file in case -quality comes later. */ |
| 430 | |
Jonathan Wright | bbb8282 | 2020-11-25 13:36:43 +0000 | [diff] [blame] | 431 | } else if (keymatch(arg, "report", 3)) { |
| 432 | report = TRUE; |
| 433 | |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 434 | } else if (keymatch(arg, "restart", 1)) { |
| 435 | /* Restart interval in MCU rows (or in MCUs with 'b'). */ |
| 436 | long lval; |
| 437 | char ch = 'x'; |
| 438 | |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame] | 439 | if (++argn >= argc) /* advance to next argument */ |
| 440 | usage(); |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 441 | if (sscanf(argv[argn], "%ld%c", &lval, &ch) < 1) |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame] | 442 | usage(); |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 443 | if (lval < 0 || lval > 65535L) |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame] | 444 | usage(); |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 445 | if (ch == 'b' || ch == 'B') { |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 446 | cinfo->restart_interval = (unsigned int)lval; |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame] | 447 | cinfo->restart_in_rows = 0; /* else prior '-restart n' overrides me */ |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 448 | } else { |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 449 | cinfo->restart_in_rows = (int)lval; |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame] | 450 | /* restart_interval will be computed during startup */ |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 451 | } |
| 452 | |
| 453 | } else if (keymatch(arg, "sample", 2)) { |
| 454 | /* Set sampling factors. */ |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame] | 455 | if (++argn >= argc) /* advance to next argument */ |
| 456 | usage(); |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 457 | samplearg = argv[argn]; |
| 458 | /* Must delay setting sample factors until after we have processed any |
| 459 | * colorspace-determining switches, since jpeg_set_colorspace sets |
| 460 | * default sampling factors. |
| 461 | */ |
| 462 | |
hbono@chromium.org | 9862697 | 2011-08-03 03:13:08 +0000 | [diff] [blame] | 463 | } else if (keymatch(arg, "scans", 4)) { |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 464 | /* Set scan script. */ |
| 465 | #ifdef C_MULTISCAN_FILES_SUPPORTED |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame] | 466 | if (++argn >= argc) /* advance to next argument */ |
| 467 | usage(); |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 468 | scansarg = argv[argn]; |
| 469 | /* We must postpone reading the file in case -progressive appears. */ |
| 470 | #else |
noel@chromium.org | 3395bcc | 2014-04-14 06:56:00 +0000 | [diff] [blame] | 471 | fprintf(stderr, "%s: sorry, multi-scan output was not compiled in\n", |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame] | 472 | progname); |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 473 | exit(EXIT_FAILURE); |
| 474 | #endif |
| 475 | |
| 476 | } else if (keymatch(arg, "smooth", 2)) { |
| 477 | /* Set input smoothing factor. */ |
| 478 | int val; |
| 479 | |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame] | 480 | if (++argn >= argc) /* advance to next argument */ |
| 481 | usage(); |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 482 | if (sscanf(argv[argn], "%d", &val) != 1) |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame] | 483 | usage(); |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 484 | if (val < 0 || val > 100) |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame] | 485 | usage(); |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 486 | cinfo->smoothing_factor = val; |
| 487 | |
Jonathan Wright | 02959c3 | 2021-11-22 10:27:51 +0000 | [diff] [blame] | 488 | } else if (keymatch(arg, "strict", 2)) { |
| 489 | strict = TRUE; |
| 490 | |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 491 | } else if (keymatch(arg, "targa", 1)) { |
| 492 | /* Input file is Targa format. */ |
| 493 | is_targa = TRUE; |
| 494 | |
| 495 | } else { |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame] | 496 | usage(); /* bogus switch */ |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 497 | } |
| 498 | } |
| 499 | |
| 500 | /* Post-switch-scanning cleanup */ |
| 501 | |
| 502 | if (for_real) { |
| 503 | |
| 504 | /* Set quantization tables for selected quality. */ |
| 505 | /* Some or all may be overridden if -qtables is present. */ |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame] | 506 | if (qualityarg != NULL) /* process -quality if it was present */ |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 507 | if (!set_quality_ratings(cinfo, qualityarg, force_baseline)) |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame] | 508 | usage(); |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 509 | |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame] | 510 | if (qtablefile != NULL) /* process -qtables if it was present */ |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 511 | if (!read_quant_tables(cinfo, qtablefile, force_baseline)) |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame] | 512 | usage(); |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 513 | |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame] | 514 | if (qslotsarg != NULL) /* process -qslots if it was present */ |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 515 | if (!set_quant_slots(cinfo, qslotsarg)) |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame] | 516 | usage(); |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 517 | |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame] | 518 | if (samplearg != NULL) /* process -sample if it was present */ |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 519 | if (!set_sample_factors(cinfo, samplearg)) |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame] | 520 | usage(); |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 521 | |
| 522 | #ifdef C_PROGRESSIVE_SUPPORTED |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame] | 523 | if (simple_progressive) /* process -progressive; -scans can override */ |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 524 | jpeg_simple_progression(cinfo); |
| 525 | #endif |
| 526 | |
| 527 | #ifdef C_MULTISCAN_FILES_SUPPORTED |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame] | 528 | if (scansarg != NULL) /* process -scans if it was present */ |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 529 | if (!read_scan_script(cinfo, scansarg)) |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame] | 530 | usage(); |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 531 | #endif |
| 532 | } |
| 533 | |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame] | 534 | return argn; /* return index of next arg (file name) */ |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 535 | } |
| 536 | |
| 537 | |
Jonathan Wright | 02959c3 | 2021-11-22 10:27:51 +0000 | [diff] [blame] | 538 | METHODDEF(void) |
| 539 | my_emit_message(j_common_ptr cinfo, int msg_level) |
| 540 | { |
| 541 | if (msg_level < 0) { |
| 542 | /* Treat warning as fatal */ |
| 543 | cinfo->err->error_exit(cinfo); |
| 544 | } else { |
| 545 | if (cinfo->err->trace_level >= msg_level) |
| 546 | cinfo->err->output_message(cinfo); |
| 547 | } |
| 548 | } |
| 549 | |
| 550 | |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 551 | /* |
| 552 | * The main program. |
| 553 | */ |
| 554 | |
| 555 | int |
Jonathan Wright | 5961ab9 | 2020-06-21 13:11:49 +0100 | [diff] [blame] | 556 | #ifdef GTEST |
| 557 | cjpeg(int argc, char **argv) |
| 558 | #else |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 559 | main(int argc, char **argv) |
Jonathan Wright | 5961ab9 | 2020-06-21 13:11:49 +0100 | [diff] [blame] | 560 | #endif |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 561 | { |
| 562 | struct jpeg_compress_struct cinfo; |
Jonathan Wright | 24e3105 | 2021-04-26 12:10:48 +0100 | [diff] [blame] | 563 | #ifdef CJPEG_FUZZER |
| 564 | struct my_error_mgr myerr; |
| 565 | struct jpeg_error_mgr &jerr = myerr.pub; |
| 566 | #else |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 567 | struct jpeg_error_mgr jerr; |
Jonathan Wright | 24e3105 | 2021-04-26 12:10:48 +0100 | [diff] [blame] | 568 | #endif |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 569 | struct cdjpeg_progress_mgr progress; |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 570 | int file_index; |
| 571 | cjpeg_source_ptr src_mgr; |
Jonathan Wright | 24e3105 | 2021-04-26 12:10:48 +0100 | [diff] [blame] | 572 | FILE *input_file = NULL; |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 573 | FILE *icc_file; |
| 574 | JOCTET *icc_profile = NULL; |
| 575 | long icc_len = 0; |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame] | 576 | FILE *output_file = NULL; |
noel@chromium.org | 3395bcc | 2014-04-14 06:56:00 +0000 | [diff] [blame] | 577 | unsigned char *outbuffer = NULL; |
| 578 | unsigned long outsize = 0; |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 579 | JDIMENSION num_scanlines; |
| 580 | |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 581 | progname = argv[0]; |
| 582 | if (progname == NULL || progname[0] == 0) |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame] | 583 | progname = "cjpeg"; /* in case C library doesn't provide it */ |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 584 | |
| 585 | /* Initialize the JPEG compression object with default error handling. */ |
| 586 | cinfo.err = jpeg_std_error(&jerr); |
| 587 | jpeg_create_compress(&cinfo); |
| 588 | /* Add some application-specific error messages (from cderror.h) */ |
| 589 | jerr.addon_message_table = cdjpeg_message_table; |
| 590 | jerr.first_addon_message = JMSG_FIRSTADDONCODE; |
| 591 | jerr.last_addon_message = JMSG_LASTADDONCODE; |
| 592 | |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 593 | /* Initialize JPEG parameters. |
| 594 | * Much of this may be overridden later. |
| 595 | * In particular, we don't yet know the input file's color space, |
| 596 | * but we need to provide some value for jpeg_set_defaults() to work. |
| 597 | */ |
| 598 | |
| 599 | cinfo.in_color_space = JCS_RGB; /* arbitrary guess */ |
| 600 | jpeg_set_defaults(&cinfo); |
| 601 | |
| 602 | /* Scan command line to find file names. |
| 603 | * It is convenient to use just one switch-parsing routine, but the switch |
| 604 | * values read here are ignored; we will rescan the switches after opening |
| 605 | * the input file. |
| 606 | */ |
| 607 | |
| 608 | file_index = parse_switches(&cinfo, argc, argv, 0, FALSE); |
| 609 | |
Jonathan Wright | 02959c3 | 2021-11-22 10:27:51 +0000 | [diff] [blame] | 610 | if (strict) |
| 611 | jerr.emit_message = my_emit_message; |
| 612 | |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 613 | #ifdef TWO_FILE_COMMANDLINE |
noel@chromium.org | 3395bcc | 2014-04-14 06:56:00 +0000 | [diff] [blame] | 614 | if (!memdst) { |
| 615 | /* Must have either -outfile switch or explicit output file name */ |
| 616 | if (outfilename == NULL) { |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 617 | if (file_index != argc - 2) { |
noel@chromium.org | 3395bcc | 2014-04-14 06:56:00 +0000 | [diff] [blame] | 618 | fprintf(stderr, "%s: must name one input and one output file\n", |
| 619 | progname); |
| 620 | usage(); |
| 621 | } |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 622 | outfilename = argv[file_index + 1]; |
noel@chromium.org | 3395bcc | 2014-04-14 06:56:00 +0000 | [diff] [blame] | 623 | } else { |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 624 | if (file_index != argc - 1) { |
noel@chromium.org | 3395bcc | 2014-04-14 06:56:00 +0000 | [diff] [blame] | 625 | fprintf(stderr, "%s: must name one input and one output file\n", |
| 626 | progname); |
| 627 | usage(); |
| 628 | } |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 629 | } |
| 630 | } |
| 631 | #else |
| 632 | /* Unix style: expect zero or one file name */ |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 633 | if (file_index < argc - 1) { |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 634 | fprintf(stderr, "%s: only one input file\n", progname); |
| 635 | usage(); |
| 636 | } |
| 637 | #endif /* TWO_FILE_COMMANDLINE */ |
| 638 | |
| 639 | /* Open the input file. */ |
| 640 | if (file_index < argc) { |
| 641 | if ((input_file = fopen(argv[file_index], READ_BINARY)) == NULL) { |
| 642 | fprintf(stderr, "%s: can't open %s\n", progname, argv[file_index]); |
Jonathan Wright | 5961ab9 | 2020-06-21 13:11:49 +0100 | [diff] [blame] | 643 | return EXIT_FAILURE; |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 644 | } |
| 645 | } else { |
| 646 | /* default input file is stdin */ |
| 647 | input_file = read_stdin(); |
| 648 | } |
| 649 | |
| 650 | /* Open the output file. */ |
| 651 | if (outfilename != NULL) { |
| 652 | if ((output_file = fopen(outfilename, WRITE_BINARY)) == NULL) { |
| 653 | fprintf(stderr, "%s: can't open %s\n", progname, outfilename); |
Jonathan Wright | 5961ab9 | 2020-06-21 13:11:49 +0100 | [diff] [blame] | 654 | return EXIT_FAILURE; |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 655 | } |
noel@chromium.org | 3395bcc | 2014-04-14 06:56:00 +0000 | [diff] [blame] | 656 | } else if (!memdst) { |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 657 | /* default output file is stdout */ |
| 658 | output_file = write_stdout(); |
| 659 | } |
| 660 | |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 661 | if (icc_filename != NULL) { |
| 662 | if ((icc_file = fopen(icc_filename, READ_BINARY)) == NULL) { |
| 663 | fprintf(stderr, "%s: can't open %s\n", progname, icc_filename); |
Jonathan Wright | 5961ab9 | 2020-06-21 13:11:49 +0100 | [diff] [blame] | 664 | return EXIT_FAILURE; |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 665 | } |
| 666 | if (fseek(icc_file, 0, SEEK_END) < 0 || |
| 667 | (icc_len = ftell(icc_file)) < 1 || |
| 668 | fseek(icc_file, 0, SEEK_SET) < 0) { |
| 669 | fprintf(stderr, "%s: can't determine size of %s\n", progname, |
| 670 | icc_filename); |
Jonathan Wright | 5961ab9 | 2020-06-21 13:11:49 +0100 | [diff] [blame] | 671 | return EXIT_FAILURE; |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 672 | } |
| 673 | if ((icc_profile = (JOCTET *)malloc(icc_len)) == NULL) { |
| 674 | fprintf(stderr, "%s: can't allocate memory for ICC profile\n", progname); |
| 675 | fclose(icc_file); |
Jonathan Wright | 5961ab9 | 2020-06-21 13:11:49 +0100 | [diff] [blame] | 676 | return EXIT_FAILURE; |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 677 | } |
| 678 | if (fread(icc_profile, icc_len, 1, icc_file) < 1) { |
| 679 | fprintf(stderr, "%s: can't read ICC profile from %s\n", progname, |
| 680 | icc_filename); |
| 681 | free(icc_profile); |
| 682 | fclose(icc_file); |
Jonathan Wright | 5961ab9 | 2020-06-21 13:11:49 +0100 | [diff] [blame] | 683 | return EXIT_FAILURE; |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 684 | } |
| 685 | fclose(icc_file); |
| 686 | } |
| 687 | |
Jonathan Wright | 24e3105 | 2021-04-26 12:10:48 +0100 | [diff] [blame] | 688 | #ifdef CJPEG_FUZZER |
| 689 | jerr.error_exit = my_error_exit; |
Jonathan Wright | 22f1a22 | 2022-03-01 15:53:34 +0000 | [diff] [blame] | 690 | jerr.emit_message = my_emit_message_fuzzer; |
Jonathan Wright | 24e3105 | 2021-04-26 12:10:48 +0100 | [diff] [blame] | 691 | if (setjmp(myerr.setjmp_buffer)) |
| 692 | HANDLE_ERROR() |
| 693 | #endif |
| 694 | |
Jonathan Wright | bbb8282 | 2020-11-25 13:36:43 +0000 | [diff] [blame] | 695 | if (report) { |
| 696 | start_progress_monitor((j_common_ptr)&cinfo, &progress); |
| 697 | progress.report = report; |
| 698 | } |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 699 | |
| 700 | /* Figure out the input file format, and set up to read it. */ |
| 701 | src_mgr = select_file_type(&cinfo, input_file); |
| 702 | src_mgr->input_file = input_file; |
Jonathan Wright | 24e3105 | 2021-04-26 12:10:48 +0100 | [diff] [blame] | 703 | #ifdef CJPEG_FUZZER |
| 704 | src_mgr->max_pixels = 1048576; |
| 705 | #endif |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 706 | |
| 707 | /* Read the input file header to obtain file size & colorspace. */ |
| 708 | (*src_mgr->start_input) (&cinfo, src_mgr); |
| 709 | |
| 710 | /* Now that we know input colorspace, fix colorspace-dependent defaults */ |
| 711 | jpeg_default_colorspace(&cinfo); |
| 712 | |
| 713 | /* Adjust default compression parameters by re-parsing the options */ |
| 714 | file_index = parse_switches(&cinfo, argc, argv, 0, TRUE); |
| 715 | |
| 716 | /* Specify data destination for compression */ |
noel@chromium.org | 3395bcc | 2014-04-14 06:56:00 +0000 | [diff] [blame] | 717 | #if JPEG_LIB_VERSION >= 80 || defined(MEM_SRCDST_SUPPORTED) |
| 718 | if (memdst) |
| 719 | jpeg_mem_dest(&cinfo, &outbuffer, &outsize); |
| 720 | else |
| 721 | #endif |
| 722 | jpeg_stdio_dest(&cinfo, output_file); |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 723 | |
Jonathan Wright | 24e3105 | 2021-04-26 12:10:48 +0100 | [diff] [blame] | 724 | #ifdef CJPEG_FUZZER |
| 725 | if (setjmp(myerr.setjmp_buffer)) |
| 726 | HANDLE_ERROR() |
| 727 | #endif |
| 728 | |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 729 | /* Start compressor */ |
| 730 | jpeg_start_compress(&cinfo, TRUE); |
| 731 | |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 732 | if (icc_profile != NULL) |
| 733 | jpeg_write_icc_profile(&cinfo, icc_profile, (unsigned int)icc_len); |
| 734 | |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 735 | /* Process data */ |
| 736 | while (cinfo.next_scanline < cinfo.image_height) { |
| 737 | num_scanlines = (*src_mgr->get_pixel_rows) (&cinfo, src_mgr); |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 738 | (void)jpeg_write_scanlines(&cinfo, src_mgr->buffer, num_scanlines); |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 739 | } |
| 740 | |
| 741 | /* Finish compression and release memory */ |
| 742 | (*src_mgr->finish_input) (&cinfo, src_mgr); |
| 743 | jpeg_finish_compress(&cinfo); |
| 744 | jpeg_destroy_compress(&cinfo); |
| 745 | |
| 746 | /* Close files, if we opened them */ |
| 747 | if (input_file != stdin) |
| 748 | fclose(input_file); |
noel@chromium.org | 3395bcc | 2014-04-14 06:56:00 +0000 | [diff] [blame] | 749 | if (output_file != stdout && output_file != NULL) |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 750 | fclose(output_file); |
| 751 | |
Jonathan Wright | bbb8282 | 2020-11-25 13:36:43 +0000 | [diff] [blame] | 752 | if (report) |
| 753 | end_progress_monitor((j_common_ptr)&cinfo); |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 754 | |
noel@chromium.org | 3395bcc | 2014-04-14 06:56:00 +0000 | [diff] [blame] | 755 | if (memdst) { |
Jonathan Wright | 24e3105 | 2021-04-26 12:10:48 +0100 | [diff] [blame] | 756 | #ifndef CJPEG_FUZZER |
noel@chromium.org | 3395bcc | 2014-04-14 06:56:00 +0000 | [diff] [blame] | 757 | fprintf(stderr, "Compressed size: %lu bytes\n", outsize); |
Jonathan Wright | 24e3105 | 2021-04-26 12:10:48 +0100 | [diff] [blame] | 758 | #endif |
Jonathan Wright | db870df | 2020-08-05 11:42:22 +0100 | [diff] [blame] | 759 | free(outbuffer); |
noel@chromium.org | 3395bcc | 2014-04-14 06:56:00 +0000 | [diff] [blame] | 760 | } |
| 761 | |
Jonathan Wright | db870df | 2020-08-05 11:42:22 +0100 | [diff] [blame] | 762 | free(icc_profile); |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 763 | |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 764 | /* All done. */ |
Jonathan Wright | 5961ab9 | 2020-06-21 13:11:49 +0100 | [diff] [blame] | 765 | return (jerr.num_warnings ? EXIT_WARNING : EXIT_SUCCESS); |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 766 | } |