| 6 |
| * Modified 2003-2011 by Guido Vollbeding. |
| 7 | * libjpeg-turbo Modifications: |
| 8 | * Copyright (C) 2010, 2013-2014, 2017, D. R. Commander. |
| 9 | * mozjpeg Modifications: |
| 10 | * Copyright (C) 2014, Mozilla Corporation. |
| 11 | * For conditions of distribution and use, see the accompanying README file. |
| 12 | * |
| 13 | * This file contains a command-line user interface for the JPEG compressor. |
| 14 | * It should work on any system with Unix- or MS-DOS-style command lines. |
| 15 | * |
| 16 | * Two different command line styles are permitted, depending on the |
| 17 | * compile-time switch TWO_FILE_COMMANDLINE: |
| 18 | * cjpeg [options] inputfile outputfile |
| 19 | * cjpeg [options] [inputfile] |
| 20 | * In the second style, output is always to standard output, which you'd |
| 21 | * normally redirect to a file or pipe to some other program. Input is |
| 22 | * either from a named file or from standard input (typically redirected). |
| 23 | * The second style is convenient on Unix but is unhelpful on systems that |
| 24 | * don't support pipes. Also, you MUST use the first style if your system |
| 25 | * doesn't do binary I/O to stdin/stdout. |
| 26 | * To simplify script writing, the "-outfile" switch is provided. The syntax |
| 27 | * cjpeg [options] -outfile outputfile inputfile |
| 28 | * works regardless of which command line style is used. |
| 29 | */ |
| 30 |
| 31 | #include "cdjpeg.h" /* Common decls for cjpeg/djpeg applications */ |
| 32 | #include "jversion.h" /* for version message */ |
| 33 | #include "jconfigint.h" |
| 34 |
| 35 | #ifndef HAVE_STDLIB_H /* <stdlib.h> should declare malloc(),free() */ |
| 36 | extern void *malloc(size_t size); |
| 37 | extern void free(void *ptr); |
| 38 | #endif |
| 39 |
| 40 | #ifdef USE_CCOMMAND /* command-line reader for Macintosh */ |
| 41 | #ifdef __MWERKS__ |
| 42 | #include <SIOUX.h> /* Metrowerks needs this */ |
| 43 | #include <console.h> /* ... and this */ |
| 44 | #endif |
| 45 | #ifdef THINK_C |
| 46 | #include <console.h> /* Think declares it here */ |
| 47 | #endif |
| 48 | #endif |
| 49 |
| 50 |
| 51 | /* Create the add-on message string table. */ |
| 52 |
| 53 | #define JMESSAGE(code, string) string, |
| 54 |
| 55 | static const char * const cdjpeg_message_table[] = { |
| 56 | #include "cderror.h" |
| 57 | NULL |
| 58 | }; |
| 59 |
| 60 |
| 61 | /* |
| 62 | * This routine determines what format the input file is, |
| 63 | * and selects the appropriate input-reading module. |
| 64 | * |
| 65 | * To determine which family of input formats the file belongs to, |
| 66 | * we may look only at the first byte of the file, since C does not |
| 67 | * guarantee that more than one character can be pushed back with ungetc. |
| 68 | * Looking at additional bytes would require one of these approaches: |
| 69 | * 1) assume we can fseek() the input file (fails for piped input); |
| 70 | * 2) assume we can push back more than one character (works in |
| 71 | * some C implementations, but unportable); |
| 72 | * 3) provide our own buffering (breaks input readers that want to use |
| 73 | * stdio directly, such as the RLE library); |
| 74 | * or 4) don't put back the data, and modify the input_init methods to assume |
| 75 | * they start reading after the start of file (also breaks RLE library). |
| 76 | * #1 is attractive for MS-DOS but is untenable on Unix. |
| 77 | * |
| 78 | * The most portable solution for file types that can't be identified by their |
| 79 | * first byte is to make the user tell us what they are. This is also the |
| 80 | * only approach for "raw" file types that contain only arbitrary values. |
| 81 | * We presently apply this method for Targa files. Most of the time Targa |
| 82 | * files start with 0x00, so we recognize that case. Potentially, however, |
| 83 | * a Targa file could start with any byte value (byte 0 is the length of the |
| 84 | * seldom-used ID field), so we provide a switch to force Targa input mode. |
| 85 | */ |
| 86 |
| 87 | static boolean is_targa; /* records user -targa switch */ |
| 88 | static boolean is_jpeg; |
| 89 | static boolean copy_markers; |
| 90 |
| 91 | LOCAL(cjpeg_source_ptr) |
| 92 | select_file_type(j_compress_ptr cinfo, FILE *infile) |
| 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': |
| 112 | return jinit_read_bmp(cinfo, TRUE); |
| 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 |
| 122 | #ifdef PNG_SUPPORTED |
| 123 | case 0x89: |
| 124 | copy_markers = TRUE; |
| 125 | return jinit_read_png(cinfo); |
| 126 | #endif |
| 127 | #ifdef RLE_SUPPORTED |
| 128 | case 'R': |
| 129 | return jinit_read_rle(cinfo); |
| 130 | #endif |
| 131 | #ifdef TARGA_SUPPORTED |
| 132 | case 0x00: |
| 133 | return jinit_read_targa(cinfo); |
| 134 | #endif |
| 135 | case 0xff: |
| 136 | is_jpeg = TRUE; |
| 137 | copy_markers = TRUE; |
| 138 | return jinit_read_jpeg(cinfo); |
| 139 | default: |
| 140 | ERREXIT(cinfo, JERR_UNKNOWN_FORMAT); |
| 141 | break; |
| 142 | } |
| 143 |
| 144 | return NULL; /* suppress compiler warnings */ |
| 145 | } |
| 146 |
| 147 |
| 148 | /* |
| 149 | * Argument-parsing code. |
| 150 | * The switch parser is designed to be useful with DOS-style command line |
| 151 | * syntax, ie, intermixed switches and file names, where only the switches |
| 152 | * to the left of a given file name affect processing of that file. |
| 153 | * The main program in this file doesn't actually use this capability... |
| 154 | */ |
| 155 |
| 156 |
| 157 | static const char *progname; /* program name for error messages */ |
| 158 | static char *icc_filename; /* for -icc switch */ |
| 159 | static char *outfilename; /* for -outfile switch */ |
| 160 | boolean memdst; /* for -memdst switch */ |
| 161 |
| 162 |
| 163 | LOCAL(void) |
| 164 | usage(void) |
| 165 | /* complain about bad command line */ |
| 166 | { |
| 167 | fprintf(stderr, "usage: %s [switches] ", progname); |
| 168 | #ifdef TWO_FILE_COMMANDLINE |
| 169 | fprintf(stderr, "inputfile outputfile\n"); |
| 170 | #else |
| 171 | fprintf(stderr, "[inputfile]\n"); |
| 172 | #endif |
| 173 |
| 174 | fprintf(stderr, "Switches (names may be abbreviated):\n"); |
| 175 | fprintf(stderr, " -quality N[,...] Compression quality (0..100; 5-95 is most useful range,\n"); |
| 176 | fprintf(stderr, " default is 75)\n"); |
| 177 | fprintf(stderr, " -grayscale Create monochrome JPEG file\n"); |
| 178 | fprintf(stderr, " -rgb Create RGB JPEG file\n"); |
| 179 | #ifdef ENTROPY_OPT_SUPPORTED |
| 180 | fprintf(stderr, " -optimize Optimize Huffman table (smaller file, but slow compression, enabled by default)\n"); |
| 181 | #endif |
| 182 | #ifdef C_PROGRESSIVE_SUPPORTED |
| 183 | fprintf(stderr, " -progressive Create progressive JPEG file (enabled by default)\n"); |
| 184 | #endif |
| 185 | fprintf(stderr, " -baseline Create baseline JPEG file (disable progressive coding)\n"); |
| 186 | #ifdef TARGA_SUPPORTED |
| 187 | fprintf(stderr, " -targa Input file is Targa format (usually not needed)\n"); |
| 188 | #endif |
| 189 | fprintf(stderr, " -revert Revert to standard defaults (instead of mozjpeg defaults)\n"); |
| 190 | fprintf(stderr, " -fastcrush Disable progressive scan optimization\n"); |
| 191 | fprintf(stderr, " -dc-scan-opt DC scan optimization mode\n"); |
| 192 | fprintf(stderr, " - 0 One scan for all components\n"); |
| 193 | fprintf(stderr, " - 1 One scan per component (default)\n"); |
| 194 | fprintf(stderr, " - 2 Optimize between one scan for all components and one scan for 1st component\n"); |
| 195 | fprintf(stderr, " plus one scan for remaining components\n"); |
| 196 | fprintf(stderr, " -notrellis Disable trellis optimization\n"); |
| 197 | fprintf(stderr, " -trellis-dc Enable trellis optimization of DC coefficients (default)\n"); |
| 198 | fprintf(stderr, " -notrellis-dc Disable trellis optimization of DC coefficients\n"); |
| 199 | fprintf(stderr, " -tune-psnr Tune trellis optimization for PSNR\n"); |
| 200 | fprintf(stderr, " -tune-hvs-psnr Tune trellis optimization for PSNR-HVS (default)\n"); |
| 201 | fprintf(stderr, " -tune-ssim Tune trellis optimization for SSIM\n"); |
| 202 | fprintf(stderr, " -tune-ms-ssim Tune trellis optimization for MS-SSIM\n"); |
| 203 | fprintf(stderr, "Switches for advanced users:\n"); |
| 204 | fprintf(stderr, " -noovershoot Disable black-on-white deringing via overshoot\n"); |
| 205 | fprintf(stderr, " -nojfif Do not write JFIF (reduces size by 18 bytes but breaks standards; no known problems in Web browsers)\n"); |
| 206 | #ifdef C_ARITH_CODING_SUPPORTED |
| 207 | fprintf(stderr, " -arithmetic Use arithmetic coding\n"); |
| 208 | #endif |
| 209 | #ifdef DCT_ISLOW_SUPPORTED |
| 210 | fprintf(stderr, " -dct int Use integer DCT method%s\n", |
| 211 | (JDCT_DEFAULT == JDCT_ISLOW ? " (default)" : "")); |
| 212 | #endif |
| 213 | #ifdef DCT_IFAST_SUPPORTED |
| 214 | fprintf(stderr, " -dct fast Use fast integer DCT (less accurate)%s\n", |
| 215 | (JDCT_DEFAULT == JDCT_IFAST ? " (default)" : "")); |
| 216 | #endif |
| 217 | #ifdef DCT_FLOAT_SUPPORTED |
| 218 | fprintf(stderr, " -dct float Use floating-point DCT method%s\n", |
| 219 | (JDCT_DEFAULT == JDCT_FLOAT ? " (default)" : "")); |
| 220 | #endif |
| 221 | fprintf(stderr, " -quant-baseline Use 8-bit quantization table entries for baseline JPEG compatibility\n"); |
| 222 | fprintf(stderr, " -quant-table N Use predefined quantization table N:\n"); |
| 223 | fprintf(stderr, " - 0 JPEG Annex K\n"); |
| 224 | fprintf(stderr, " - 1 Flat\n"); |
| 225 | fprintf(stderr, " - 2 Custom, tuned for MS-SSIM\n"); |
| 226 | fprintf(stderr, " - 3 ImageMagick table by N. Robidoux\n"); |
| 227 | fprintf(stderr, " - 4 Custom, tuned for PSNR-HVS\n"); |
| 228 | fprintf(stderr, " - 5 Table from paper by Klein, Silverstein and Carney\n"); |
| 229 | fprintf(stderr, " -icc FILE Embed ICC profile contained in FILE\n"); |
| 230 | fprintf(stderr, " -restart N Set restart interval in rows, or in blocks with B\n"); |
| 231 | #ifdef INPUT_SMOOTHING_SUPPORTED |
| 232 | fprintf(stderr, " -smooth N Smooth dithered input (N=1..100 is strength)\n"); |
| 233 | #endif |
| 234 | fprintf(stderr, " -maxmemory N Maximum memory to use (in kbytes)\n"); |
| 235 | fprintf(stderr, " -outfile name Specify name for output file\n"); |
| 236 | #if JPEG_LIB_VERSION >= 80 || defined(MEM_SRCDST_SUPPORTED) |
| 237 | fprintf(stderr, " -memdst Compress to memory instead of file (useful for benchmarking)\n"); |
| 238 | #endif |
| 239 | fprintf(stderr, " -verbose or -debug Emit debug output\n"); |
| 240 | fprintf(stderr, " -version Print version information and exit\n"); |
| 241 | fprintf(stderr, "Switches for wizards:\n"); |
| 242 | fprintf(stderr, " -qtables FILE Use quantization tables given in FILE\n"); |
| 243 | fprintf(stderr, " -qslots N[,...] Set component quantization tables\n"); |
| 244 | fprintf(stderr, " -sample HxV[,...] Set component sampling factors\n"); |
| 245 | #ifdef C_MULTISCAN_FILES_SUPPORTED |
| 246 | fprintf(stderr, " -scans FILE Create multi-scan JPEG per script FILE\n"); |
| 247 | #endif |
| 248 | exit(EXIT_FAILURE); |
| 249 | } |
| 250 |
| 251 |
| 252 | LOCAL(int) |
| 253 | parse_switches(j_compress_ptr cinfo, int argc, char **argv, |
| 254 | int last_file_arg_seen, boolean for_real) |
| 255 | /* Parse optional switches. |
| 256 | * Returns argv[] index of first file-name argument (== argc if none). |
| 257 | * Any file names with indexes <= last_file_arg_seen are ignored; |
| 258 | * they have presumably been processed in a previous iteration. |
| 259 | * (Pass 0 for last_file_arg_seen on the first or only iteration.) |
| 260 | * for_real is FALSE on the first (dummy) pass; we may skip any expensive |
| 261 | * processing. |
| 262 | */ |
| 263 | { |
| 264 | int argn; |
| 265 | char *arg; |
| 266 | boolean force_baseline; |
| 267 | boolean simple_progressive; |
| 268 | char *qualityarg = NULL; /* saves -quality parm if any */ |
| 269 | char *qtablefile = NULL; /* saves -qtables filename if any */ |
| 270 | char *qslotsarg = NULL; /* saves -qslots parm if any */ |
| 271 | char *samplearg = NULL; /* saves -sample parm if any */ |
| 272 | char *scansarg = NULL; /* saves -scans parm if any */ |
| 273 |
| 274 | /* Set up default JPEG parameters. */ |
| 275 |
| 276 | force_baseline = FALSE; /* by default, allow 16-bit quantizers */ |
| 277 | #ifdef C_PROGRESSIVE_SUPPORTED |
| 278 | simple_progressive = cinfo->num_scans == 0 ? FALSE : TRUE; |
| 279 | #else |
| 280 | simple_progressive = FALSE; |
| 281 | #endif |
| 282 | is_targa = FALSE; |
| 283 | icc_filename = NULL; |
| 284 | outfilename = NULL; |
| 285 | memdst = FALSE; |
| 286 | cinfo->err->trace_level = 0; |
| 287 |
| 288 | /* Scan command line options, adjust parameters */ |
| 289 |
| 290 | for (argn = 1; argn < argc; argn++) { |
| 291 | arg = argv[argn]; |
| 292 | if (*arg != '-') { |
| 293 | /* Not a switch, must be a file name argument */ |
| 294 | if (argn <= last_file_arg_seen) { |
| 295 | outfilename = NULL; /* -outfile applies to just one input file */ |
| 296 | continue; /* ignore this name if previously processed */ |
| 297 | } |
| 298 | break; /* else done parsing switches */ |
| 299 | } |
| 300 | arg++; /* advance past switch marker character */ |
| 301 |
| 302 | if (keymatch(arg, "arithmetic", 1)) { |
| 303 | /* Use arithmetic coding. */ |
| 304 | #ifdef C_ARITH_CODING_SUPPORTED |
| 305 | cinfo->arith_code = TRUE; |
| 306 |
| 307 | /* No table optimization required for AC */ |
| 308 | cinfo->optimize_coding = FALSE; |
| 309 | #else |
| 310 | fprintf(stderr, "%s: sorry, arithmetic coding not supported\n", |
| 311 | progname); |
| 312 | exit(EXIT_FAILURE); |
| 313 | #endif |
| 314 |
| 315 | } else if (keymatch(arg, "baseline", 1)) { |
| 316 | /* Force baseline-compatible output (8-bit quantizer values). */ |
| 317 | force_baseline = TRUE; |
| 318 | /* Disable multiple scans */ |
| 319 | simple_progressive = FALSE; |
| 320 | cinfo->num_scans = 0; |
| 321 | cinfo->scan_info = NULL; |
| 322 |
| 323 | } else if (keymatch(arg, "dct", 2)) { |
| 324 | /* Select DCT algorithm. */ |
| 325 | if (++argn >= argc) { /* advance to next argument */ |
| 326 | fprintf(stderr, "%s: missing argument for dct\n", progname); |
| 327 | usage(); |
| 328 | } |
| 329 | if (keymatch(argv[argn], "int", 1)) { |
| 330 | cinfo->dct_method = JDCT_ISLOW; |
| 331 | } else if (keymatch(argv[argn], "fast", 2)) { |
| 332 | cinfo->dct_method = JDCT_IFAST; |
| 333 | } else if (keymatch(argv[argn], "float", 2)) { |
| 334 | cinfo->dct_method = JDCT_FLOAT; |
| 335 | } else { |
| 336 | fprintf(stderr, "%s: invalid argument for dct\n", progname); |
| 337 | usage(); |
| 338 | } |
| 339 |
| 340 | } else if (keymatch(arg, "debug", 1) || keymatch(arg, "verbose", 1)) { |
| 341 | /* Enable debug printouts. */ |
| 342 | /* On first -d, print version identification */ |
| 343 | static boolean printed_version = FALSE; |
| 344 |
| 345 | if (!printed_version) { |
| 346 | fprintf(stderr, "%s version %s (build %s)\n", |
| 347 | PACKAGE_NAME, VERSION, BUILD); |
| 348 | fprintf(stderr, "%s\n\n", JCOPYRIGHT); |
| 349 | fprintf(stderr, "Emulating The Independent JPEG Group's software, version %s\n\n", |
| 350 | JVERSION); |
| 351 | printed_version = TRUE; |
| 352 | } |
| 353 | cinfo->err->trace_level++; |
| 354 |
| 355 | } else if (keymatch(arg, "version", 4)) { |
| 356 | fprintf(stderr, "%s version %s (build %s)\n", |
| 357 | PACKAGE_NAME, VERSION, BUILD); |
| 358 | exit(EXIT_SUCCESS); |
| 359 |
| 360 | } else if (keymatch(arg, "fastcrush", 4)) { |
| 361 | jpeg_c_set_bool_param(cinfo, JBOOLEAN_OPTIMIZE_SCANS, FALSE); |
| 362 |
| 363 | } else if (keymatch(arg, "grayscale", 2) || keymatch(arg, "greyscale",2)) { |
| 364 | /* Force a monochrome JPEG file to be generated. */ |
| 365 | jpeg_set_colorspace(cinfo, JCS_GRAYSCALE); |
| 366 |
| 367 | } else if (keymatch(arg, "rgb", 3)) { |
| 368 | /* Force an RGB JPEG file to be generated. */ |
| 369 | jpeg_set_colorspace(cinfo, JCS_RGB); |
| 370 |
| 371 | } else if (keymatch(arg, "lambda1", 7)) { |
| 372 | if (++argn >= argc) /* advance to next argument */ |
| 373 | usage(); |
| 374 | jpeg_c_set_float_param(cinfo, JFLOAT_LAMBDA_LOG_SCALE1, |
| 375 | atof(argv[argn])); |
| 376 |
| 377 | } else if (keymatch(arg, "lambda2", 7)) { |
| 378 | if (++argn >= argc) /* advance to next argument */ |
| 379 | usage(); |
| 380 | jpeg_c_set_float_param(cinfo, JFLOAT_LAMBDA_LOG_SCALE2, |
| 381 | atof(argv[argn])); |
| 382 |
| 383 | } else if (keymatch(arg, "icc", 1)) { |
| 384 | /* Set ICC filename. */ |
| 385 | if (++argn >= argc) /* advance to next argument */ |
| 386 | usage(); |
| 387 | icc_filename = argv[argn]; |
| 388 |
| 389 | } else if (keymatch(arg, "maxmemory", 3)) { |
| 390 | /* Maximum memory in Kb (or Mb with 'm'). */ |
| 391 | long lval; |
| 392 | char ch = 'x'; |
| 393 |
| 394 | if (++argn >= argc) /* advance to next argument */ |
| 395 | usage(); |
| 396 | if (sscanf(argv[argn], "%ld%c", &lval, &ch) < 1) |
| 397 | usage(); |
| 398 | if (ch == 'm' || ch == 'M') |
| 399 | lval *= 1000L; |
| 400 | cinfo->mem->max_memory_to_use = lval * 1000L; |
| 401 |
| 402 | } else if (keymatch(arg, "dc-scan-opt", 3)) { |
| 403 | if (++argn >= argc) { /* advance to next argument */ |
| 404 | fprintf(stderr, "%s: missing argument for dc-scan-opt\n", progname); |
| 405 | usage(); |
| 406 | } |
| 407 | jpeg_c_set_int_param(cinfo, JINT_DC_SCAN_OPT_MODE, atoi(argv[argn])); |
| 408 |
| 409 | } else if (keymatch(arg, "optimize", 1) || keymatch(arg, "optimise", 1)) { |
| 410 | /* Enable entropy parm optimization. */ |
| 411 | #ifdef ENTROPY_OPT_SUPPORTED |
| 412 | cinfo->optimize_coding = TRUE; |
| 413 | #else |
| 414 | fprintf(stderr, "%s: sorry, entropy optimization was not compiled in\n", |
| 415 | progname); |
| 416 | exit(EXIT_FAILURE); |
| 417 | #endif |
| 418 |
| 419 | } else if (keymatch(arg, "outfile", 4)) { |
| 420 | /* Set output file name. */ |
| 421 | if (++argn >= argc) { /* advance to next argument */ |
| 422 | fprintf(stderr, "%s: missing argument for outfile\n", progname); |
| 423 | usage(); |
| 424 | } |
| 425 | outfilename = argv[argn]; /* save it away for later use */ |
| 426 |
| 427 | } else if (keymatch(arg, "progressive", 1)) { |
| 428 | /* Select simple progressive mode. */ |
| 429 | #ifdef C_PROGRESSIVE_SUPPORTED |
| 430 | simple_progressive = TRUE; |
| 431 | /* We must postpone execution until num_components is known. */ |
| 432 | #else |
| 433 | fprintf(stderr, "%s: sorry, progressive output was not compiled in\n", |
| 434 | progname); |
| 435 | exit(EXIT_FAILURE); |
| 436 | #endif |
| 437 |
| 438 | } else if (keymatch(arg, "memdst", 2)) { |
| 439 | /* Use in-memory destination manager */ |
| 440 | #if JPEG_LIB_VERSION >= 80 || defined(MEM_SRCDST_SUPPORTED) |
| 441 | memdst = TRUE; |
| 442 | #else |
| 443 | fprintf(stderr, "%s: sorry, in-memory destination manager was not compiled in\n", |
| 444 | progname); |
| 445 | exit(EXIT_FAILURE); |
| 446 | #endif |
| 447 |
| 448 | } else if (keymatch(arg, "quality", 1)) { |
| 449 | /* Quality ratings (quantization table scaling factors). */ |
| 450 | if (++argn >= argc) { /* advance to next argument */ |
| 451 | fprintf(stderr, "%s: missing argument for quality\n", progname); |
| 452 | usage(); |
| 453 | } |
| 454 | qualityarg = argv[argn]; |
| 455 |
| 456 | } else if (keymatch(arg, "qslots", 2)) { |
| 457 | /* Quantization table slot numbers. */ |
| 458 | if (++argn >= argc) /* advance to next argument */ |
| 459 | usage(); |
| 460 | qslotsarg = argv[argn]; |
| 461 | /* Must delay setting qslots until after we have processed any |
| 462 | * colorspace-determining switches, since jpeg_set_colorspace sets |
| 463 | * default quant table numbers. |
| 464 | */ |
| 465 |
| 466 | } else if (keymatch(arg, "qtables", 2)) { |
| 467 | /* Quantization tables fetched from file. */ |
| 468 | if (++argn >= argc) /* advance to next argument */ |
| 469 | usage(); |
| 470 | qtablefile = argv[argn]; |
| 471 | /* We postpone actually reading the file in case -quality comes later. */ |
| 472 |
| 473 | } else if (keymatch(arg, "quant-table", 7)) { |
| 474 | int val; |
| 475 | if (++argn >= argc) /* advance to next argument */ |
| 476 | usage(); |
| 477 | val = atoi(argv[argn]); |
| 478 | jpeg_c_set_int_param(cinfo, JINT_BASE_QUANT_TBL_IDX, val); |
| 479 | if (jpeg_c_get_int_param(cinfo, JINT_BASE_QUANT_TBL_IDX) != val) { |
| 480 | fprintf(stderr, "%s: %d is invalid argument for quant-table\n", progname, val); |
| 481 | usage(); |
| 482 | } |
| 483 | jpeg_set_quality(cinfo, 75, TRUE); |
| 484 |
| 485 | } else if (keymatch(arg, "quant-baseline", 7)) { |
| 486 | /* Force quantization table to meet baseline requirements */ |
| 487 | force_baseline = TRUE; |
| 488 |
| 489 | } else if (keymatch(arg, "restart", 1)) { |
| 490 | /* Restart interval in MCU rows (or in MCUs with 'b'). */ |
| 491 | long lval; |
| 492 | char ch = 'x'; |
| 493 |
| 494 | if (++argn >= argc) /* advance to next argument */ |
| 495 | usage(); |
| 496 | if (sscanf(argv[argn], "%ld%c", &lval, &ch) < 1) |
| 497 | usage(); |
| 498 | if (lval < 0 || lval > 65535L) |
| 499 | usage(); |
| 500 | if (ch == 'b' || ch == 'B') { |
| 501 | cinfo->restart_interval = (unsigned int)lval; |
| 502 | cinfo->restart_in_rows = 0; /* else prior '-restart n' overrides me */ |
| 503 | } else { |
| 504 | cinfo->restart_in_rows = (int)lval; |
| 505 | /* restart_interval will be computed during startup */ |
| 506 | } |
| 507 |
| 508 | } else if (keymatch(arg, "revert", 3)) { |
| 509 | /* revert to old JPEG default */ |
| 510 | jpeg_c_set_int_param(cinfo, JINT_COMPRESS_PROFILE, JCP_FASTEST); |
| 511 | jpeg_set_defaults(cinfo); |
| 512 |
| 513 | } else if (keymatch(arg, "sample", 2)) { |
| 514 | /* Set sampling factors. */ |
| 515 | if (++argn >= argc) /* advance to next argument */ |
| 516 | usage(); |
| 517 | samplearg = argv[argn]; |
| 518 | /* Must delay setting sample factors until after we have processed any |
| 519 | * colorspace-determining switches, since jpeg_set_colorspace sets |
| 520 | * default sampling factors. |
| 521 | */ |
| 522 |
| 523 | } else if (keymatch(arg, "scans", 4)) { |
| 524 | /* Set scan script. */ |
| 525 | #ifdef C_MULTISCAN_FILES_SUPPORTED |
| 526 | if (++argn >= argc) /* advance to next argument */ |
| 527 | usage(); |
| 528 | scansarg = argv[argn]; |
| 529 | /* We must postpone reading the file in case -progressive appears. */ |
| 530 | #else |
| 531 | fprintf(stderr, "%s: sorry, multi-scan output was not compiled in\n", |
| 532 | progname); |
| 533 | exit(EXIT_FAILURE); |
| 534 | #endif |
| 535 |
| 536 | } else if (keymatch(arg, "smooth", 2)) { |
| 537 | /* Set input smoothing factor. */ |
| 538 | int val; |
| 539 |
| 540 | if (++argn >= argc) /* advance to next argument */ |
| 541 | usage(); |
| 542 | if (sscanf(argv[argn], "%d", &val) != 1) |
| 543 | usage(); |
| 544 | if (val < 0 || val > 100) |
| 545 | usage(); |
| 546 | cinfo->smoothing_factor = val; |
| 547 |
| 548 | } else if (keymatch(arg, "targa", 1)) { |
| 549 | /* Input file is Targa format. */ |
| 550 | is_targa = TRUE; |
| 551 |
| 552 | } else if (keymatch(arg, "notrellis-dc", 11)) { |
| 553 | /* disable trellis quantization */ |
| 554 | jpeg_c_set_bool_param(cinfo, JBOOLEAN_TRELLIS_QUANT_DC, FALSE); |
| 555 |
| 556 | } else if (keymatch(arg, "notrellis", 1)) { |
| 557 | /* disable trellis quantization */ |
| 558 | jpeg_c_set_bool_param(cinfo, JBOOLEAN_TRELLIS_QUANT, FALSE); |
| 559 |
| 560 | } else if (keymatch(arg, "trellis-dc-ver-weight", 12)) { |
| 561 | if (++argn >= argc) { /* advance to next argument */ |
| 562 | fprintf(stderr, "%s: missing argument for trellis-dc-ver-weight\n", progname); |
| 563 | usage(); |
| 564 | } |
| 565 | jpeg_c_set_float_param(cinfo, JFLOAT_TRELLIS_DELTA_DC_WEIGHT, atof(argv[argn])); |
| 566 |
| 567 | } else if (keymatch(arg, "trellis-dc", 9)) { |
| 568 | /* enable DC trellis quantization */ |
| 569 | jpeg_c_set_bool_param(cinfo, JBOOLEAN_TRELLIS_QUANT_DC, TRUE); |
| 570 |
| 571 | } else if (keymatch(arg, "tune-psnr", 6)) { |
| 572 | jpeg_c_set_int_param(cinfo, JINT_BASE_QUANT_TBL_IDX, 1); |
| 573 | jpeg_c_set_float_param(cinfo, JFLOAT_LAMBDA_LOG_SCALE1, 9.0); |
| 574 | jpeg_c_set_float_param(cinfo, JFLOAT_LAMBDA_LOG_SCALE2, 0.0); |
| 575 | jpeg_c_set_bool_param(cinfo, JBOOLEAN_USE_LAMBDA_WEIGHT_TBL, FALSE); |
| 576 | jpeg_set_quality(cinfo, 75, TRUE); |
| 577 |
| 578 | } else if (keymatch(arg, "tune-ssim", 6)) { |
| 579 | jpeg_c_set_int_param(cinfo, JINT_BASE_QUANT_TBL_IDX, 1); |
| 580 | jpeg_c_set_float_param(cinfo, JFLOAT_LAMBDA_LOG_SCALE1, 11.5); |
| 581 | jpeg_c_set_float_param(cinfo, JFLOAT_LAMBDA_LOG_SCALE2, 12.75); |
| 582 | jpeg_c_set_bool_param(cinfo, JBOOLEAN_USE_LAMBDA_WEIGHT_TBL, FALSE); |
| 583 | jpeg_set_quality(cinfo, 75, TRUE); |
| 584 |
| 585 | } else if (keymatch(arg, "tune-ms-ssim", 6)) { |
| 586 | jpeg_c_set_int_param(cinfo, JINT_BASE_QUANT_TBL_IDX, 3); |
| 587 | jpeg_c_set_float_param(cinfo, JFLOAT_LAMBDA_LOG_SCALE1, 12.0); |
| 588 | jpeg_c_set_float_param(cinfo, JFLOAT_LAMBDA_LOG_SCALE2, 13.0); |
| 589 | jpeg_c_set_bool_param(cinfo, JBOOLEAN_USE_LAMBDA_WEIGHT_TBL, TRUE); |
| 590 | jpeg_set_quality(cinfo, 75, TRUE); |
| 591 |
| 592 | } else if (keymatch(arg, "tune-hvs-psnr", 6)) { |
| 593 | jpeg_c_set_int_param(cinfo, JINT_BASE_QUANT_TBL_IDX, 3); |
| 594 | jpeg_c_set_float_param(cinfo, JFLOAT_LAMBDA_LOG_SCALE1, 14.75); |
| 595 | jpeg_c_set_float_param(cinfo, JFLOAT_LAMBDA_LOG_SCALE2, 16.5); |
| 596 | jpeg_c_set_bool_param(cinfo, JBOOLEAN_USE_LAMBDA_WEIGHT_TBL, TRUE); |
| 597 | jpeg_set_quality(cinfo, 75, TRUE); |
| 598 |
| 599 | } else if (keymatch(arg, "noovershoot", 11)) { |
| 600 | jpeg_c_set_bool_param(cinfo, JBOOLEAN_OVERSHOOT_DERINGING, FALSE); |
| 601 |
| 602 | } else if (keymatch(arg, "nojfif", 6)) { |
| 603 | cinfo->write_JFIF_header = 0; |
| 604 | } else { |
| 605 | fprintf(stderr, "%s: unknown option '%s'\n", progname, arg); |
| 606 | usage(); /* bogus switch */ |
| 607 | } |
| 608 | } |
| 609 |
| 610 | /* Post-switch-scanning cleanup */ |
| 611 |
| 612 | if (for_real) { |
| 613 |
| 614 | /* Set quantization tables for selected quality. */ |
| 615 | /* Some or all may be overridden if -qtables is present. */ |
| 616 | if (qualityarg != NULL) /* process -quality if it was present */ |
| 617 | if (! set_quality_ratings(cinfo, qualityarg, force_baseline)) { |
| 618 | fprintf(stderr, "%s: can't set quality ratings\n", progname); |
| 619 | usage(); |
| 620 | } |
| 621 |
| 622 | if (qtablefile != NULL) /* process -qtables if it was present */ |
| 623 | if (! read_quant_tables(cinfo, qtablefile, force_baseline)) { |
| 624 | fprintf(stderr, "%s: can't read qtable file\n", progname); |
| 625 | usage(); |
| 626 | } |
| 627 |
| 628 | if (qslotsarg != NULL) /* process -qslots if it was present */ |
| 629 | if (!set_quant_slots(cinfo, qslotsarg)) |
| 630 | usage(); |
| 631 |
| 632 | /* set_quality_ratings sets default subsampling, so the explicit |
| 633 | subsampling must be set after it */ |
| 634 | if (samplearg != NULL) /* process -sample if it was present */ |
| 635 | if (! set_sample_factors(cinfo, samplearg)) { |
| 636 | fprintf(stderr, "%s: can't set sample factors\n", progname); |
| 637 | usage(); |
| 638 | } |
| 639 |
| 640 | #ifdef C_PROGRESSIVE_SUPPORTED |
| 641 | if (simple_progressive) /* process -progressive; -scans can override */ |
| 642 | jpeg_simple_progression(cinfo); |
| 643 | #endif |
| 644 |
| 645 | #ifdef C_MULTISCAN_FILES_SUPPORTED |
| 646 | if (scansarg != NULL) /* process -scans if it was present */ |
| 647 | if (!read_scan_script(cinfo, scansarg)) |
| 648 | usage(); |
| 649 | #endif |
| 650 | } |
| 651 |
| 652 | return argn; /* return index of next arg (file name) */ |
| 653 | } |
| 654 |
| 655 |
| 656 | /* |
| 657 | * The main program. |
| 658 | */ |
| 659 |
| 660 | int |
| 661 | main(int argc, char **argv) |
| 662 | { |
| 663 | struct jpeg_compress_struct cinfo; |
| 664 | struct jpeg_error_mgr jerr; |
| 665 | #ifdef PROGRESS_REPORT |
| 666 | struct cdjpeg_progress_mgr progress; |
| 667 | #endif |
| 668 | int file_index; |
| 669 | cjpeg_source_ptr src_mgr; |
| 670 | FILE *input_file; |
| 671 | FILE *icc_file; |
| 672 | JOCTET *icc_profile = NULL; |
| 673 | long icc_len = 0; |
| 674 | FILE *output_file = NULL; |
| 675 | unsigned char *outbuffer = NULL; |
| 676 | unsigned long outsize = 0; |
| 677 | JDIMENSION num_scanlines; |
| 678 |
| 679 | /* On Mac, fetch a command line. */ |
| 680 | #ifdef USE_CCOMMAND |
| 681 | argc = ccommand(&argv); |
| 682 | #endif |
| 683 |
| 684 | progname = argv[0]; |
| 685 | if (progname == NULL || progname[0] == 0) |
| 686 | progname = "cjpeg"; /* in case C library doesn't provide it */ |
| 687 |
| 688 | /* Initialize the JPEG compression object with default error handling. */ |
| 689 | cinfo.err = jpeg_std_error(&jerr); |
| 690 | jpeg_create_compress(&cinfo); |
| 691 | /* Add some application-specific error messages (from cderror.h) */ |
| 692 | jerr.addon_message_table = cdjpeg_message_table; |
| 693 | jerr.first_addon_message = JMSG_FIRSTADDONCODE; |
| 694 | jerr.last_addon_message = JMSG_LASTADDONCODE; |
| 695 |
| 696 | /* Initialize JPEG parameters. |
| 697 | * Much of this may be overridden later. |
| 698 | * In particular, we don't yet know the input file's color space, |
| 699 | * but we need to provide some value for jpeg_set_defaults() to work. |
| 700 | */ |
| 701 |
| 702 | cinfo.in_color_space = JCS_RGB; /* arbitrary guess */ |
| 703 | jpeg_set_defaults(&cinfo); |
| 704 |
| 705 | /* Scan command line to find file names. |
| 706 | * It is convenient to use just one switch-parsing routine, but the switch |
| 707 | * values read here are ignored; we will rescan the switches after opening |
| 708 | * the input file. |
| 709 | */ |
| 710 |
| 711 | file_index = parse_switches(&cinfo, argc, argv, 0, FALSE); |
| 712 |
| 713 | #ifdef TWO_FILE_COMMANDLINE |
| 714 | if (!memdst) { |
| 715 | /* Must have either -outfile switch or explicit output file name */ |
| 716 | if (outfilename == NULL) { |
| 717 | if (file_index != argc - 2) { |
| 718 | fprintf(stderr, "%s: must name one input and one output file\n", |
| 719 | progname); |
| 720 | usage(); |
| 721 | } |
| 722 | outfilename = argv[file_index + 1]; |
| 723 | } else { |
| 724 | if (file_index != argc - 1) { |
| 725 | fprintf(stderr, "%s: must name one input and one output file\n", |
| 726 | progname); |
| 727 | usage(); |
| 728 | } |
| 729 | } |
| 730 | } |
| 731 | #else |
| 732 | /* Unix style: expect zero or one file name */ |
| 733 | if (file_index < argc - 1) { |
| 734 | fprintf(stderr, "%s: only one input file\n", progname); |
| 735 | usage(); |
| 736 | } |
| 737 | #endif /* TWO_FILE_COMMANDLINE */ |
| 738 |
| 739 | /* Open the input file. */ |
| 740 | if (file_index < argc) { |
| 741 | if ((input_file = fopen(argv[file_index], READ_BINARY)) == NULL) { |
| 742 | fprintf(stderr, "%s: can't open %s\n", progname, argv[file_index]); |
| 743 | exit(EXIT_FAILURE); |
| 744 | } |
| 745 | } else { |
| 746 | /* default input file is stdin */ |
| 747 | input_file = read_stdin(); |
| 748 | } |
| 749 |
| 750 | /* Open the output file. */ |
| 751 | if (outfilename != NULL) { |
| 752 | if ((output_file = fopen(outfilename, WRITE_BINARY)) == NULL) { |
| 753 | fprintf(stderr, "%s: can't open %s\n", progname, outfilename); |
| 754 | exit(EXIT_FAILURE); |
| 755 | } |
| 756 | } else if (!memdst) { |
| 757 | /* default output file is stdout */ |
| 758 | output_file = write_stdout(); |
| 759 | } |
| 760 |
| 761 | if (icc_filename != NULL) { |
| 762 | if ((icc_file = fopen(icc_filename, READ_BINARY)) == NULL) { |
| 763 | fprintf(stderr, "%s: can't open %s\n", progname, icc_filename); |
| 764 | exit(EXIT_FAILURE); |
| 765 | } |
| 766 | if (fseek(icc_file, 0, SEEK_END) < 0 || |
| 767 | (icc_len = ftell(icc_file)) < 1 || |
| 768 | fseek(icc_file, 0, SEEK_SET) < 0) { |
| 769 | fprintf(stderr, "%s: can't determine size of %s\n", progname, |
| 770 | icc_filename); |
| 771 | exit(EXIT_FAILURE); |
| 772 | } |
| 773 | if ((icc_profile = (JOCTET *)malloc(icc_len)) == NULL) { |
| 774 | fprintf(stderr, "%s: can't allocate memory for ICC profile\n", progname); |
| 775 | fclose(icc_file); |
| 776 | exit(EXIT_FAILURE); |
| 777 | } |
| 778 | if (fread(icc_profile, icc_len, 1, icc_file) < 1) { |
| 779 | fprintf(stderr, "%s: can't read ICC profile from %s\n", progname, |
| 780 | icc_filename); |
| 781 | free(icc_profile); |
| 782 | fclose(icc_file); |
| 783 | exit(EXIT_FAILURE); |
| 784 | } |
| 785 | fclose(icc_file); |
| 786 | } |
| 787 |
| 788 | #ifdef PROGRESS_REPORT |
| 789 | start_progress_monitor((j_common_ptr)&cinfo, &progress); |
| 790 | #endif |
| 791 |
| 792 | /* Figure out the input file format, and set up to read it. */ |
| 793 | src_mgr = select_file_type(&cinfo, input_file); |
| 794 | src_mgr->input_file = input_file; |
| 795 |
| 796 | /* Read the input file header to obtain file size & colorspace. */ |
| 797 | (*src_mgr->start_input) (&cinfo, src_mgr); |
| 798 |
| 799 | /* Now that we know input colorspace, fix colorspace-dependent defaults */ |
| 800 | #if JPEG_RAW_READER |
| 801 | if (!is_jpeg) |
| 802 | #endif |
| 803 | jpeg_default_colorspace(&cinfo); |
| 804 |
| 805 | /* Adjust default compression parameters by re-parsing the options */ |
| 806 | file_index = parse_switches(&cinfo, argc, argv, 0, TRUE); |
| 807 |
| 808 | /* Specify data destination for compression */ |
| 809 | #if JPEG_LIB_VERSION >= 80 || defined(MEM_SRCDST_SUPPORTED) |
| 810 | if (memdst) |
| 811 | jpeg_mem_dest(&cinfo, &outbuffer, &outsize); |
| 812 | else |
| 813 | #endif |
| 814 | jpeg_stdio_dest(&cinfo, output_file); |
| 815 |
| 816 | /* Start compressor */ |
| 817 | jpeg_start_compress(&cinfo, TRUE); |
| 818 |
| 819 | /* Copy metadata */ |
| 820 | if (copy_markers) { |
| 821 | jpeg_saved_marker_ptr marker; |
| 822 |
| 823 | /* In the current implementation, we don't actually need to examine the |
| 824 | * option flag here; we just copy everything that got saved. |
| 825 | * But to avoid confusion, we do not output JFIF and Adobe APP14 markers |
| 826 | * if the encoder library already wrote one. |
| 827 | */ |
| 828 | for (marker = src_mgr->marker_list; marker != NULL; marker = marker->next) { |
| 829 | if (cinfo.write_JFIF_header && |
| 830 | marker->marker == JPEG_APP0 && |
| 831 | marker->data_length >= 5 && |
| 832 | GETJOCTET(marker->data[0]) == 0x4A && |
| 833 | GETJOCTET(marker->data[1]) == 0x46 && |
| 834 | GETJOCTET(marker->data[2]) == 0x49 && |
| 835 | GETJOCTET(marker->data[3]) == 0x46 && |
| 836 | GETJOCTET(marker->data[4]) == 0) |
| 837 | continue; /* reject duplicate JFIF */ |
| 838 | if (cinfo.write_Adobe_marker && |
| 839 | marker->marker == JPEG_APP0+14 && |
| 840 | marker->data_length >= 5 && |
| 841 | GETJOCTET(marker->data[0]) == 0x41 && |
| 842 | GETJOCTET(marker->data[1]) == 0x64 && |
| 843 | GETJOCTET(marker->data[2]) == 0x6F && |
| 844 | GETJOCTET(marker->data[3]) == 0x62 && |
| 845 | GETJOCTET(marker->data[4]) == 0x65) |
| 846 | continue; /* reject duplicate Adobe */ |
| 847 | jpeg_write_marker(&cinfo, marker->marker, marker->data, |
| 848 | marker->data_length); |
| 849 | } |
| 850 | } |
| 851 | if (icc_profile != NULL) |
| 852 | jpeg_write_icc_profile(&cinfo, icc_profile, (unsigned int)icc_len); |
| 853 |
| 854 | /* Process data */ |
| 855 | while (cinfo.next_scanline < cinfo.image_height) { |
| 856 | num_scanlines = (*src_mgr->get_pixel_rows) (&cinfo, src_mgr); |
| 857 | #if JPEG_RAW_READER |
| 858 | if (is_jpeg) |
| 859 | (void) jpeg_write_raw_data(&cinfo, src_mgr->plane_pointer, num_scanlines); |
| 860 | else |
| 861 | #endif |
| 862 | (void) jpeg_write_scanlines(&cinfo, src_mgr->buffer, num_scanlines); |
| 863 | } |
| 864 |
| 865 | /* Finish compression and release memory */ |
| 866 | (*src_mgr->finish_input) (&cinfo, src_mgr); |
| 867 | jpeg_finish_compress(&cinfo); |
| 868 | jpeg_destroy_compress(&cinfo); |
| 869 |
| 870 | /* Close files, if we opened them */ |
| 871 | if (input_file != stdin) |
| 872 | fclose(input_file); |
| 873 | if (output_file != stdout && output_file != NULL) |
| 874 | fclose(output_file); |
| 875 |
| 876 | #ifdef PROGRESS_REPORT |
| 877 | end_progress_monitor((j_common_ptr)&cinfo); |
| 878 | #endif |
| 879 |
| 880 | if (memdst) { |
| 881 | fprintf(stderr, "Compressed size: %lu bytes\n", outsize); |
| 882 | free(outbuffer); |
| 883 | } |
| 884 |
| 885 | free(icc_profile); |
| 886 |
| 887 | /* All done. */ |
| 888 | exit(jerr.num_warnings ? EXIT_WARNING : EXIT_SUCCESS); |
| 889 | return 0; /* suppress no-return-value warnings */ |
| 890 | } |
| 891 |