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