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 | 24e3105 | 2021-04-26 12:10:48 +0100 | [diff] [blame] | 8 | * Copyright (C) 2010, 2013-2014, 2017, 2019-2021, 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 | 24e3105 | 2021-04-26 12:10:48 +0100 | [diff] [blame] | 30 | #ifdef CJPEG_FUZZER |
| 31 | #define JPEG_INTERNALS |
| 32 | #endif |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame] | 33 | #include "cdjpeg.h" /* Common decls for cjpeg/djpeg applications */ |
| 34 | #include "jversion.h" /* for version message */ |
| 35 | #include "jconfigint.h" |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 36 | |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 37 | #ifndef HAVE_STDLIB_H /* <stdlib.h> should declare malloc(),free() */ |
| 38 | extern void *malloc(size_t size); |
| 39 | extern void free(void *ptr); |
| 40 | #endif |
| 41 | |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame] | 42 | #ifdef USE_CCOMMAND /* command-line reader for Macintosh */ |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 43 | #ifdef __MWERKS__ |
| 44 | #include <SIOUX.h> /* Metrowerks needs this */ |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame] | 45 | #include <console.h> /* ... and this */ |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 46 | #endif |
| 47 | #ifdef THINK_C |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame] | 48 | #include <console.h> /* Think declares it here */ |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 49 | #endif |
| 50 | #endif |
| 51 | |
| 52 | |
| 53 | /* Create the add-on message string table. */ |
| 54 | |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 55 | #define JMESSAGE(code, string) string, |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 56 | |
| 57 | static 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 Wright | bbb8282 | 2020-11-25 13:36:43 +0000 | [diff] [blame] | 75 | * stdio directly); |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 76 | * 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] | 77 | * they start reading after the start of file. |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 78 | * #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 Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame] | 89 | static boolean is_targa; /* records user -targa switch */ |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 90 | |
| 91 | |
| 92 | LOCAL(cjpeg_source_ptr) |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 93 | select_file_type(j_compress_ptr cinfo, FILE *infile) |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 94 | { |
| 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 Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 113 | return jinit_read_bmp(cinfo, TRUE); |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 114 | #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.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 123 | #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 Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame] | 132 | return NULL; /* suppress compiler warnings */ |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 133 | } |
| 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 Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame] | 145 | static const char *progname; /* program name for error messages */ |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 146 | static char *icc_filename; /* for -icc switch */ |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame] | 147 | static char *outfilename; /* for -outfile switch */ |
Jonathan Wright | 24e3105 | 2021-04-26 12:10:48 +0100 | [diff] [blame] | 148 | boolean memdst; /* for -memdst switch */ |
| 149 | boolean report; /* for -report switch */ |
| 150 | |
| 151 | |
| 152 | #ifdef CJPEG_FUZZER |
| 153 | |
| 154 | #include <setjmp.h> |
| 155 | |
| 156 | struct my_error_mgr { |
| 157 | struct jpeg_error_mgr pub; |
| 158 | jmp_buf setjmp_buffer; |
| 159 | }; |
| 160 | |
| 161 | void 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 | |
| 168 | static 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.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 189 | |
| 190 | |
| 191 | LOCAL(void) |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 192 | usage(void) |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 193 | /* 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 Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame] | 203 | fprintf(stderr, " -quality N[,...] Compression quality (0..100; 5-95 is most useful range,\n"); |
| 204 | fprintf(stderr, " default is 75)\n"); |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 205 | fprintf(stderr, " -grayscale Create monochrome JPEG file\n"); |
noel@chromium.org | 3395bcc | 2014-04-14 06:56:00 +0000 | [diff] [blame] | 206 | fprintf(stderr, " -rgb Create RGB JPEG file\n"); |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 207 | #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.org | df5ffdd | 2012-05-11 07:46:03 +0000 | [diff] [blame] | 217 | #ifdef C_ARITH_CODING_SUPPORTED |
| 218 | fprintf(stderr, " -arithmetic Use arithmetic coding\n"); |
| 219 | #endif |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 220 | #ifdef DCT_ISLOW_SUPPORTED |
Jonathan Wright | bbb8282 | 2020-11-25 13:36:43 +0000 | [diff] [blame] | 221 | fprintf(stderr, " -dct int Use accurate integer DCT method%s\n", |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame] | 222 | (JDCT_DEFAULT == JDCT_ISLOW ? " (default)" : "")); |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 223 | #endif |
| 224 | #ifdef DCT_IFAST_SUPPORTED |
Jonathan Wright | bbb8282 | 2020-11-25 13:36:43 +0000 | [diff] [blame] | 225 | 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] | 226 | (JDCT_DEFAULT == JDCT_IFAST ? " (default)" : "")); |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 227 | #endif |
| 228 | #ifdef DCT_FLOAT_SUPPORTED |
Jonathan Wright | bbb8282 | 2020-11-25 13:36:43 +0000 | [diff] [blame] | 229 | 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] | 230 | (JDCT_DEFAULT == JDCT_FLOAT ? " (default)" : "")); |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 231 | #endif |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 232 | fprintf(stderr, " -icc FILE Embed ICC profile contained in FILE\n"); |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 233 | 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.org | 3395bcc | 2014-04-14 06:56:00 +0000 | [diff] [blame] | 239 | #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 Wright | bbb8282 | 2020-11-25 13:36:43 +0000 | [diff] [blame] | 242 | fprintf(stderr, " -report Report compression progress\n"); |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 243 | fprintf(stderr, " -verbose or -debug Emit debug output\n"); |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame] | 244 | fprintf(stderr, " -version Print version information and exit\n"); |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 245 | fprintf(stderr, "Switches for wizards:\n"); |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 246 | fprintf(stderr, " -baseline Force baseline quantization tables\n"); |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 247 | fprintf(stderr, " -qtables FILE Use quantization tables given in FILE\n"); |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 248 | 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 Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 251 | 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] | 252 | #endif |
| 253 | exit(EXIT_FAILURE); |
| 254 | } |
| 255 | |
| 256 | |
| 257 | LOCAL(int) |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 258 | parse_switches(j_compress_ptr cinfo, int argc, char **argv, |
| 259 | int last_file_arg_seen, boolean for_real) |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 260 | /* 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 Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame] | 270 | char *arg; |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 271 | boolean force_baseline; |
| 272 | boolean simple_progressive; |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame] | 273 | 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.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 278 | |
| 279 | /* Set up default JPEG parameters. */ |
hbono@chromium.org | 9862697 | 2011-08-03 03:13:08 +0000 | [diff] [blame] | 280 | |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame] | 281 | force_baseline = FALSE; /* by default, allow 16-bit quantizers */ |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 282 | simple_progressive = FALSE; |
| 283 | is_targa = FALSE; |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 284 | icc_filename = NULL; |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 285 | outfilename = NULL; |
noel@chromium.org | 3395bcc | 2014-04-14 06:56:00 +0000 | [diff] [blame] | 286 | memdst = FALSE; |
Jonathan Wright | bbb8282 | 2020-11-25 13:36:43 +0000 | [diff] [blame] | 287 | report = FALSE; |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 288 | 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 Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame] | 297 | outfilename = NULL; /* -outfile applies to just one input file */ |
| 298 | continue; /* ignore this name if previously processed */ |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 299 | } |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame] | 300 | break; /* else done parsing switches */ |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 301 | } |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame] | 302 | arg++; /* advance past switch marker character */ |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 303 | |
| 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 Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame] | 310 | progname); |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 311 | 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 Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame] | 320 | if (++argn >= argc) /* advance to next argument */ |
| 321 | usage(); |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 322 | if (keymatch(argv[argn], "int", 1)) { |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame] | 323 | cinfo->dct_method = JDCT_ISLOW; |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 324 | } else if (keymatch(argv[argn], "fast", 2)) { |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame] | 325 | cinfo->dct_method = JDCT_IFAST; |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 326 | } else if (keymatch(argv[argn], "float", 2)) { |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame] | 327 | cinfo->dct_method = JDCT_FLOAT; |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 328 | } else |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame] | 329 | usage(); |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 330 | |
| 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 Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 336 | if (!printed_version) { |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame] | 337 | 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.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 343 | } |
| 344 | cinfo->err->trace_level++; |
| 345 | |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame] | 346 | } 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 Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 351 | } else if (keymatch(arg, "grayscale", 2) || |
| 352 | keymatch(arg, "greyscale", 2)) { |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 353 | /* Force a monochrome JPEG file to be generated. */ |
| 354 | jpeg_set_colorspace(cinfo, JCS_GRAYSCALE); |
| 355 | |
noel@chromium.org | 3395bcc | 2014-04-14 06:56:00 +0000 | [diff] [blame] | 356 | } else if (keymatch(arg, "rgb", 3)) { |
| 357 | /* Force an RGB JPEG file to be generated. */ |
| 358 | jpeg_set_colorspace(cinfo, JCS_RGB); |
| 359 | |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 360 | } 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.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 366 | } else if (keymatch(arg, "maxmemory", 3)) { |
| 367 | /* Maximum memory in Kb (or Mb with 'm'). */ |
| 368 | long lval; |
| 369 | char ch = 'x'; |
| 370 | |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame] | 371 | if (++argn >= argc) /* advance to next argument */ |
| 372 | usage(); |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 373 | if (sscanf(argv[argn], "%ld%c", &lval, &ch) < 1) |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame] | 374 | usage(); |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 375 | if (ch == 'm' || ch == 'M') |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame] | 376 | lval *= 1000L; |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 377 | 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.org | 3395bcc | 2014-04-14 06:56:00 +0000 | [diff] [blame] | 384 | fprintf(stderr, "%s: sorry, entropy optimization was not compiled in\n", |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame] | 385 | progname); |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 386 | exit(EXIT_FAILURE); |
| 387 | #endif |
| 388 | |
| 389 | } else if (keymatch(arg, "outfile", 4)) { |
| 390 | /* Set output file name. */ |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame] | 391 | if (++argn >= argc) /* advance to next argument */ |
| 392 | usage(); |
| 393 | outfilename = argv[argn]; /* save it away for later use */ |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 394 | |
| 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.org | 3395bcc | 2014-04-14 06:56:00 +0000 | [diff] [blame] | 401 | fprintf(stderr, "%s: sorry, progressive output was not compiled in\n", |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame] | 402 | progname); |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 403 | exit(EXIT_FAILURE); |
| 404 | #endif |
| 405 | |
noel@chromium.org | 3395bcc | 2014-04-14 06:56:00 +0000 | [diff] [blame] | 406 | } 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.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 416 | } else if (keymatch(arg, "quality", 1)) { |
hbono@chromium.org | 9862697 | 2011-08-03 03:13:08 +0000 | [diff] [blame] | 417 | /* Quality ratings (quantization table scaling factors). */ |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame] | 418 | if (++argn >= argc) /* advance to next argument */ |
| 419 | usage(); |
hbono@chromium.org | 9862697 | 2011-08-03 03:13:08 +0000 | [diff] [blame] | 420 | qualityarg = argv[argn]; |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 421 | |
| 422 | } else if (keymatch(arg, "qslots", 2)) { |
| 423 | /* Quantization table slot numbers. */ |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame] | 424 | if (++argn >= argc) /* advance to next argument */ |
| 425 | usage(); |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 426 | 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 Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame] | 434 | if (++argn >= argc) /* advance to next argument */ |
| 435 | usage(); |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 436 | qtablefile = argv[argn]; |
| 437 | /* We postpone actually reading the file in case -quality comes later. */ |
| 438 | |
Jonathan Wright | bbb8282 | 2020-11-25 13:36:43 +0000 | [diff] [blame] | 439 | } else if (keymatch(arg, "report", 3)) { |
| 440 | report = TRUE; |
| 441 | |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 442 | } 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 Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame] | 447 | if (++argn >= argc) /* advance to next argument */ |
| 448 | usage(); |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 449 | if (sscanf(argv[argn], "%ld%c", &lval, &ch) < 1) |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame] | 450 | usage(); |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 451 | if (lval < 0 || lval > 65535L) |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame] | 452 | usage(); |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 453 | if (ch == 'b' || ch == 'B') { |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 454 | cinfo->restart_interval = (unsigned int)lval; |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame] | 455 | cinfo->restart_in_rows = 0; /* else prior '-restart n' overrides me */ |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 456 | } else { |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 457 | cinfo->restart_in_rows = (int)lval; |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame] | 458 | /* restart_interval will be computed during startup */ |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 459 | } |
| 460 | |
| 461 | } else if (keymatch(arg, "sample", 2)) { |
| 462 | /* Set sampling factors. */ |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame] | 463 | if (++argn >= argc) /* advance to next argument */ |
| 464 | usage(); |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 465 | 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.org | 9862697 | 2011-08-03 03:13:08 +0000 | [diff] [blame] | 471 | } else if (keymatch(arg, "scans", 4)) { |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 472 | /* Set scan script. */ |
| 473 | #ifdef C_MULTISCAN_FILES_SUPPORTED |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame] | 474 | if (++argn >= argc) /* advance to next argument */ |
| 475 | usage(); |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 476 | scansarg = argv[argn]; |
| 477 | /* We must postpone reading the file in case -progressive appears. */ |
| 478 | #else |
noel@chromium.org | 3395bcc | 2014-04-14 06:56:00 +0000 | [diff] [blame] | 479 | fprintf(stderr, "%s: sorry, multi-scan output was not compiled in\n", |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame] | 480 | progname); |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 481 | exit(EXIT_FAILURE); |
| 482 | #endif |
| 483 | |
| 484 | } else if (keymatch(arg, "smooth", 2)) { |
| 485 | /* Set input smoothing factor. */ |
| 486 | int val; |
| 487 | |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame] | 488 | if (++argn >= argc) /* advance to next argument */ |
| 489 | usage(); |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 490 | if (sscanf(argv[argn], "%d", &val) != 1) |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame] | 491 | usage(); |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 492 | if (val < 0 || val > 100) |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame] | 493 | usage(); |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 494 | 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 Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame] | 501 | usage(); /* bogus switch */ |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 502 | } |
| 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 Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame] | 511 | if (qualityarg != NULL) /* process -quality if it was present */ |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 512 | if (!set_quality_ratings(cinfo, qualityarg, force_baseline)) |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame] | 513 | usage(); |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 514 | |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame] | 515 | if (qtablefile != NULL) /* process -qtables if it was present */ |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 516 | if (!read_quant_tables(cinfo, qtablefile, force_baseline)) |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame] | 517 | usage(); |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 518 | |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame] | 519 | if (qslotsarg != NULL) /* process -qslots if it was present */ |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 520 | if (!set_quant_slots(cinfo, qslotsarg)) |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame] | 521 | usage(); |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 522 | |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame] | 523 | if (samplearg != NULL) /* process -sample if it was present */ |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 524 | if (!set_sample_factors(cinfo, samplearg)) |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame] | 525 | usage(); |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 526 | |
| 527 | #ifdef C_PROGRESSIVE_SUPPORTED |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame] | 528 | if (simple_progressive) /* process -progressive; -scans can override */ |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 529 | jpeg_simple_progression(cinfo); |
| 530 | #endif |
| 531 | |
| 532 | #ifdef C_MULTISCAN_FILES_SUPPORTED |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame] | 533 | if (scansarg != NULL) /* process -scans if it was present */ |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 534 | if (!read_scan_script(cinfo, scansarg)) |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame] | 535 | usage(); |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 536 | #endif |
| 537 | } |
| 538 | |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame] | 539 | return argn; /* return index of next arg (file name) */ |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 540 | } |
| 541 | |
| 542 | |
| 543 | /* |
| 544 | * The main program. |
| 545 | */ |
| 546 | |
| 547 | int |
Jonathan Wright | 5961ab9 | 2020-06-21 13:11:49 +0100 | [diff] [blame] | 548 | #ifdef GTEST |
| 549 | cjpeg(int argc, char **argv) |
| 550 | #else |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 551 | main(int argc, char **argv) |
Jonathan Wright | 5961ab9 | 2020-06-21 13:11:49 +0100 | [diff] [blame] | 552 | #endif |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 553 | { |
| 554 | struct jpeg_compress_struct cinfo; |
Jonathan Wright | 24e3105 | 2021-04-26 12:10:48 +0100 | [diff] [blame] | 555 | #ifdef CJPEG_FUZZER |
| 556 | struct my_error_mgr myerr; |
| 557 | struct jpeg_error_mgr &jerr = myerr.pub; |
| 558 | #else |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 559 | struct jpeg_error_mgr jerr; |
Jonathan Wright | 24e3105 | 2021-04-26 12:10:48 +0100 | [diff] [blame] | 560 | #endif |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 561 | struct cdjpeg_progress_mgr progress; |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 562 | int file_index; |
| 563 | cjpeg_source_ptr src_mgr; |
Jonathan Wright | 24e3105 | 2021-04-26 12:10:48 +0100 | [diff] [blame] | 564 | FILE *input_file = NULL; |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 565 | FILE *icc_file; |
| 566 | JOCTET *icc_profile = NULL; |
| 567 | long icc_len = 0; |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame] | 568 | FILE *output_file = NULL; |
noel@chromium.org | 3395bcc | 2014-04-14 06:56:00 +0000 | [diff] [blame] | 569 | unsigned char *outbuffer = NULL; |
| 570 | unsigned long outsize = 0; |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 571 | 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 Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame] | 580 | progname = "cjpeg"; /* in case C library doesn't provide it */ |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 581 | |
| 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.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 590 | /* 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.org | 3395bcc | 2014-04-14 06:56:00 +0000 | [diff] [blame] | 608 | if (!memdst) { |
| 609 | /* Must have either -outfile switch or explicit output file name */ |
| 610 | if (outfilename == NULL) { |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 611 | if (file_index != argc - 2) { |
noel@chromium.org | 3395bcc | 2014-04-14 06:56:00 +0000 | [diff] [blame] | 612 | fprintf(stderr, "%s: must name one input and one output file\n", |
| 613 | progname); |
| 614 | usage(); |
| 615 | } |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 616 | outfilename = argv[file_index + 1]; |
noel@chromium.org | 3395bcc | 2014-04-14 06:56:00 +0000 | [diff] [blame] | 617 | } else { |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 618 | if (file_index != argc - 1) { |
noel@chromium.org | 3395bcc | 2014-04-14 06:56:00 +0000 | [diff] [blame] | 619 | fprintf(stderr, "%s: must name one input and one output file\n", |
| 620 | progname); |
| 621 | usage(); |
| 622 | } |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 623 | } |
| 624 | } |
| 625 | #else |
| 626 | /* Unix style: expect zero or one file name */ |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 627 | if (file_index < argc - 1) { |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 628 | 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 Wright | 5961ab9 | 2020-06-21 13:11:49 +0100 | [diff] [blame] | 637 | return EXIT_FAILURE; |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 638 | } |
| 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 Wright | 5961ab9 | 2020-06-21 13:11:49 +0100 | [diff] [blame] | 648 | return EXIT_FAILURE; |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 649 | } |
noel@chromium.org | 3395bcc | 2014-04-14 06:56:00 +0000 | [diff] [blame] | 650 | } else if (!memdst) { |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 651 | /* default output file is stdout */ |
| 652 | output_file = write_stdout(); |
| 653 | } |
| 654 | |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 655 | 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 Wright | 5961ab9 | 2020-06-21 13:11:49 +0100 | [diff] [blame] | 658 | return EXIT_FAILURE; |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 659 | } |
| 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 Wright | 5961ab9 | 2020-06-21 13:11:49 +0100 | [diff] [blame] | 665 | return EXIT_FAILURE; |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 666 | } |
| 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 Wright | 5961ab9 | 2020-06-21 13:11:49 +0100 | [diff] [blame] | 670 | return EXIT_FAILURE; |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 671 | } |
| 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 Wright | 5961ab9 | 2020-06-21 13:11:49 +0100 | [diff] [blame] | 677 | return EXIT_FAILURE; |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 678 | } |
| 679 | fclose(icc_file); |
| 680 | } |
| 681 | |
Jonathan Wright | 24e3105 | 2021-04-26 12:10:48 +0100 | [diff] [blame] | 682 | #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 Wright | bbb8282 | 2020-11-25 13:36:43 +0000 | [diff] [blame] | 689 | if (report) { |
| 690 | start_progress_monitor((j_common_ptr)&cinfo, &progress); |
| 691 | progress.report = report; |
| 692 | } |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 693 | |
| 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 Wright | 24e3105 | 2021-04-26 12:10:48 +0100 | [diff] [blame] | 697 | #ifdef CJPEG_FUZZER |
| 698 | src_mgr->max_pixels = 1048576; |
| 699 | #endif |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 700 | |
| 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.org | 3395bcc | 2014-04-14 06:56:00 +0000 | [diff] [blame] | 711 | #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.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 717 | |
Jonathan Wright | 24e3105 | 2021-04-26 12:10:48 +0100 | [diff] [blame] | 718 | #ifdef CJPEG_FUZZER |
| 719 | if (setjmp(myerr.setjmp_buffer)) |
| 720 | HANDLE_ERROR() |
| 721 | #endif |
| 722 | |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 723 | /* Start compressor */ |
| 724 | jpeg_start_compress(&cinfo, TRUE); |
| 725 | |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 726 | if (icc_profile != NULL) |
| 727 | jpeg_write_icc_profile(&cinfo, icc_profile, (unsigned int)icc_len); |
| 728 | |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 729 | /* Process data */ |
| 730 | while (cinfo.next_scanline < cinfo.image_height) { |
| 731 | num_scanlines = (*src_mgr->get_pixel_rows) (&cinfo, src_mgr); |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 732 | (void)jpeg_write_scanlines(&cinfo, src_mgr->buffer, num_scanlines); |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 733 | } |
| 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.org | 3395bcc | 2014-04-14 06:56:00 +0000 | [diff] [blame] | 743 | if (output_file != stdout && output_file != NULL) |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 744 | fclose(output_file); |
| 745 | |
Jonathan Wright | bbb8282 | 2020-11-25 13:36:43 +0000 | [diff] [blame] | 746 | if (report) |
| 747 | end_progress_monitor((j_common_ptr)&cinfo); |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 748 | |
noel@chromium.org | 3395bcc | 2014-04-14 06:56:00 +0000 | [diff] [blame] | 749 | if (memdst) { |
Jonathan Wright | 24e3105 | 2021-04-26 12:10:48 +0100 | [diff] [blame] | 750 | #ifndef CJPEG_FUZZER |
noel@chromium.org | 3395bcc | 2014-04-14 06:56:00 +0000 | [diff] [blame] | 751 | fprintf(stderr, "Compressed size: %lu bytes\n", outsize); |
Jonathan Wright | 24e3105 | 2021-04-26 12:10:48 +0100 | [diff] [blame] | 752 | #endif |
Jonathan Wright | db870df | 2020-08-05 11:42:22 +0100 | [diff] [blame] | 753 | free(outbuffer); |
noel@chromium.org | 3395bcc | 2014-04-14 06:56:00 +0000 | [diff] [blame] | 754 | } |
| 755 | |
Jonathan Wright | db870df | 2020-08-05 11:42:22 +0100 | [diff] [blame] | 756 | free(icc_profile); |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 757 | |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 758 | /* All done. */ |
Jonathan Wright | 5961ab9 | 2020-06-21 13:11:49 +0100 | [diff] [blame] | 759 | return (jerr.num_warnings ? EXIT_WARNING : EXIT_SUCCESS); |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 760 | } |