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