| 6 |
| * libjpeg-turbo Modifications: |
| 7 | * Copyright (C) 2010, 2014, 2017, D. R. Commander. |
| 8 | * mozjpeg Modifications: |
| 9 | * Copyright (C) 2014, Mozilla Corporation. |
| 10 | * For conditions of distribution and use, see the accompanying README file. |
| 11 | * |
| 12 | * This file contains a command-line user interface for JPEG transcoding. |
| 13 | * It is very similar to cjpeg.c, and partly to djpeg.c, but provides |
| 14 | * lossless transcoding between different JPEG file formats. It also |
| 15 | * provides some lossless and sort-of-lossless transformations of JPEG data. |
| 16 | */ |
| 17 |
| 18 | #include "cdjpeg.h" /* Common decls for cjpeg/djpeg applications */ |
| 19 | #include "transupp.h" /* Support routines for jpegtran */ |
| 20 | #include "jversion.h" /* for version message */ |
| 21 | #include "jconfigint.h" |
| 22 |
| 23 | #ifdef USE_CCOMMAND /* command-line reader for Macintosh */ |
| 24 | #ifdef __MWERKS__ |
| 25 | #include <SIOUX.h> /* Metrowerks needs this */ |
| 26 | #include <console.h> /* ... and this */ |
| 27 | #endif |
| 28 | #ifdef THINK_C |
| 29 | #include <console.h> /* Think declares it here */ |
| 30 | #endif |
| 31 | #endif |
| 32 |
| 33 |
| 34 | /* |
| 35 | * Argument-parsing code. |
| 36 | * The switch parser is designed to be useful with DOS-style command line |
| 37 | * syntax, ie, intermixed switches and file names, where only the switches |
| 38 | * to the left of a given file name affect processing of that file. |
| 39 | * The main program in this file doesn't actually use this capability... |
| 40 | */ |
| 41 |
| 42 |
| 43 | static const char *progname; /* program name for error messages */ |
| 44 | static char *icc_filename; /* for -icc switch */ |
| 45 | static char *outfilename; /* for -outfile switch */ |
| 46 | static boolean prefer_smallest; /* use smallest of input or result file (if no image-changing options supplied) */ |
| 47 | static JCOPY_OPTION copyoption; /* -copy switch */ |
| 48 | static jpeg_transform_info transformoption; /* image transformation options */ |
| 49 | boolean memsrc = FALSE; /* for -memsrc switch */ |
| 50 | #define INPUT_BUF_SIZE 4096 |
| 51 |
| 52 |
| 53 | LOCAL(void) |
| 54 | usage(void) |
| 55 | /* complain about bad command line */ |
| 56 | { |
| 57 | fprintf(stderr, "usage: %s [switches] ", progname); |
| 58 | #ifdef TWO_FILE_COMMANDLINE |
| 59 | fprintf(stderr, "inputfile outputfile\n"); |
| 60 | #else |
| 61 | fprintf(stderr, "[inputfile]\n"); |
| 62 | #endif |
| 63 |
| 64 | fprintf(stderr, "Switches (names may be abbreviated):\n"); |
| 65 | fprintf(stderr, " -copy none Copy no extra markers from source file\n"); |
| 66 | fprintf(stderr, " -copy comments Copy only comment markers (default)\n"); |
| 67 | fprintf(stderr, " -copy all Copy all extra markers\n"); |
| 68 | #ifdef ENTROPY_OPT_SUPPORTED |
| 69 | fprintf(stderr, " -optimize Optimize Huffman table (smaller file, but slow compression, enabled by default)\n"); |
| 70 | #endif |
| 71 | #ifdef C_PROGRESSIVE_SUPPORTED |
| 72 | fprintf(stderr, " -progressive Create progressive JPEG file (enabled by default)\n"); |
| 73 | #endif |
| 74 | fprintf(stderr, " -revert Revert to standard defaults (instead of mozjpeg defaults)\n"); |
| 75 | fprintf(stderr, " -fastcrush Disable progressive scan optimization\n"); |
| 76 | fprintf(stderr, "Switches for modifying the image:\n"); |
| 77 | #if TRANSFORMS_SUPPORTED |
| 78 | fprintf(stderr, " -crop WxH+X+Y Crop to a rectangular subarea\n"); |
| 79 | fprintf(stderr, " -grayscale Reduce to grayscale (omit color data)\n"); |
| 80 | fprintf(stderr, " -flip [horizontal|vertical] Mirror image (left-right or top-bottom)\n"); |
| 81 | fprintf(stderr, " -perfect Fail if there is non-transformable edge blocks\n"); |
| 82 | fprintf(stderr, " -rotate [90|180|270] Rotate image (degrees clockwise)\n"); |
| 83 | #endif |
| 84 | #if TRANSFORMS_SUPPORTED |
| 85 | fprintf(stderr, " -transpose Transpose image\n"); |
| 86 | fprintf(stderr, " -transverse Transverse transpose image\n"); |
| 87 | fprintf(stderr, " -trim Drop non-transformable edge blocks\n"); |
| 88 | #endif |
| 89 | fprintf(stderr, "Switches for advanced users:\n"); |
| 90 | #ifdef C_ARITH_CODING_SUPPORTED |
| 91 | fprintf(stderr, " -arithmetic Use arithmetic coding\n"); |
| 92 | #endif |
| 93 | fprintf(stderr, " -icc FILE Embed ICC profile contained in FILE\n"); |
| 94 | fprintf(stderr, " -restart N Set restart interval in rows, or in blocks with B\n"); |
| 95 | fprintf(stderr, " -maxmemory N Maximum memory to use (in kbytes)\n"); |
| 96 | fprintf(stderr, " -outfile name Specify name for output file\n"); |
| 97 | fprintf(stderr, " -verbose or -debug Emit debug output\n"); |
| 98 | fprintf(stderr, " -version Print version information and exit\n"); |
| 99 | fprintf(stderr, "Switches for wizards:\n"); |
| 100 | #ifdef C_MULTISCAN_FILES_SUPPORTED |
| 101 | fprintf(stderr, " -scans FILE Create multi-scan JPEG per script FILE\n"); |
| 102 | #endif |
| 103 | exit(EXIT_FAILURE); |
| 104 | } |
| 105 |
| 106 |
| 107 | LOCAL(void) |
| 108 | select_transform(JXFORM_CODE transform) |
| 109 | /* Silly little routine to detect multiple transform options, |
| 110 | * which we can't handle. |
| 111 | */ |
| 112 | { |
| 113 | #if TRANSFORMS_SUPPORTED |
| 114 | if (transformoption.transform == JXFORM_NONE || |
| 115 | transformoption.transform == transform) { |
| 116 | transformoption.transform = transform; |
| 117 | } else { |
| 118 | fprintf(stderr, "%s: can only do one image transformation at a time\n", |
| 119 | progname); |
| 120 | usage(); |
| 121 | } |
| 122 | #else |
| 123 | fprintf(stderr, "%s: sorry, image transformation was not compiled\n", |
| 124 | progname); |
| 125 | exit(EXIT_FAILURE); |
| 126 | #endif |
| 127 | } |
| 128 |
| 129 |
| 130 | LOCAL(int) |
| 131 | parse_switches(j_compress_ptr cinfo, int argc, char **argv, |
| 132 | int last_file_arg_seen, boolean for_real) |
| 133 | /* Parse optional switches. |
| 134 | * Returns argv[] index of first file-name argument (== argc if none). |
| 135 | * Any file names with indexes <= last_file_arg_seen are ignored; |
| 136 | * they have presumably been processed in a previous iteration. |
| 137 | * (Pass 0 for last_file_arg_seen on the first or only iteration.) |
| 138 | * for_real is FALSE on the first (dummy) pass; we may skip any expensive |
| 139 | * processing. |
| 140 | */ |
| 141 | { |
| 142 | int argn; |
| 143 | char *arg; |
| 144 | boolean simple_progressive; |
| 145 | char *scansarg = NULL; /* saves -scans parm if any */ |
| 146 |
| 147 | /* Set up default JPEG parameters. */ |
| 148 | #ifdef C_PROGRESSIVE_SUPPORTED |
| 149 | simple_progressive = cinfo->num_scans == 0 ? FALSE : TRUE; |
| 150 | #else |
| 151 | simple_progressive = FALSE; |
| 152 | #endif |
| 153 | icc_filename = NULL; |
| 154 | outfilename = NULL; |
| 155 | copyoption = JCOPYOPT_DEFAULT; |
| 156 | transformoption.transform = JXFORM_NONE; |
| 157 | transformoption.perfect = FALSE; |
| 158 | transformoption.trim = FALSE; |
| 159 | transformoption.force_grayscale = FALSE; |
| 160 | transformoption.crop = FALSE; |
| 161 | transformoption.slow_hflip = FALSE; |
| 162 | cinfo->err->trace_level = 0; |
| 163 | prefer_smallest = TRUE; |
| 164 |
| 165 | /* Scan command line options, adjust parameters */ |
| 166 |
| 167 | for (argn = 1; argn < argc; argn++) { |
| 168 | arg = argv[argn]; |
| 169 | if (*arg != '-') { |
| 170 | /* Not a switch, must be a file name argument */ |
| 171 | if (argn <= last_file_arg_seen) { |
| 172 | outfilename = NULL; /* -outfile applies to just one input file */ |
| 173 | continue; /* ignore this name if previously processed */ |
| 174 | } |
| 175 | break; /* else done parsing switches */ |
| 176 | } |
| 177 | arg++; /* advance past switch marker character */ |
| 178 |
| 179 | if (keymatch(arg, "arithmetic", 1)) { |
| 180 | /* Use arithmetic coding. */ |
| 181 | #ifdef C_ARITH_CODING_SUPPORTED |
| 182 | cinfo->arith_code = TRUE; |
| 183 |
| 184 | /* No table optimization required for AC */ |
| 185 | cinfo->optimize_coding = FALSE; |
| 186 | prefer_smallest = FALSE; |
| 187 | #else |
| 188 | fprintf(stderr, "%s: sorry, arithmetic coding not supported\n", |
| 189 | progname); |
| 190 | exit(EXIT_FAILURE); |
| 191 | #endif |
| 192 |
| 193 | } else if (keymatch(arg, "copy", 2)) { |
| 194 | /* Select which extra markers to copy. */ |
| 195 | if (++argn >= argc) /* advance to next argument */ |
| 196 | usage(); |
| 197 | if (keymatch(argv[argn], "none", 1)) { |
| 198 | copyoption = JCOPYOPT_NONE; |
| 199 | } else if (keymatch(argv[argn], "comments", 1)) { |
| 200 | copyoption = JCOPYOPT_COMMENTS; |
| 201 | } else if (keymatch(argv[argn], "all", 1)) { |
| 202 | copyoption = JCOPYOPT_ALL; |
| 203 | } else |
| 204 | usage(); |
| 205 |
| 206 | } else if (keymatch(arg, "crop", 2)) { |
| 207 | /* Perform lossless cropping. */ |
| 208 | #if TRANSFORMS_SUPPORTED |
| 209 | if (++argn >= argc) /* advance to next argument */ |
| 210 | usage(); |
| 211 | if (!jtransform_parse_crop_spec(&transformoption, argv[argn])) { |
| 212 | fprintf(stderr, "%s: bogus -crop argument '%s'\n", |
| 213 | progname, argv[argn]); |
| 214 | exit(EXIT_FAILURE); |
| 215 | } |
| 216 | prefer_smallest = FALSE; |
| 217 | #else |
| 218 | select_transform(JXFORM_NONE); /* force an error */ |
| 219 | #endif |
| 220 |
| 221 | } else if (keymatch(arg, "debug", 1) || keymatch(arg, "verbose", 1)) { |
| 222 | /* Enable debug printouts. */ |
| 223 | /* On first -d, print version identification */ |
| 224 | static boolean printed_version = FALSE; |
| 225 |
| 226 | if (!printed_version) { |
| 227 | fprintf(stderr, "%s version %s (build %s)\n", |
| 228 | PACKAGE_NAME, VERSION, BUILD); |
| 229 | fprintf(stderr, "%s\n\n", JCOPYRIGHT); |
| 230 | fprintf(stderr, "Emulating The Independent JPEG Group's software, version %s\n\n", |
| 231 | JVERSION); |
| 232 | printed_version = TRUE; |
| 233 | } |
| 234 | cinfo->err->trace_level++; |
| 235 |
| 236 | } else if (keymatch(arg, "version", 4)) { |
| 237 | fprintf(stderr, "%s version %s (build %s)\n", |
| 238 | PACKAGE_NAME, VERSION, BUILD); |
| 239 | exit(EXIT_SUCCESS); |
| 240 |
| 241 | } else if (keymatch(arg, "flip", 1)) { |
| 242 | /* Mirror left-right or top-bottom. */ |
| 243 | if (++argn >= argc) /* advance to next argument */ |
| 244 | usage(); |
| 245 | if (keymatch(argv[argn], "horizontal", 1)) |
| 246 | select_transform(JXFORM_FLIP_H); |
| 247 | else if (keymatch(argv[argn], "vertical", 1)) |
| 248 | select_transform(JXFORM_FLIP_V); |
| 249 | else |
| 250 | usage(); |
| 251 |
| 252 | prefer_smallest = FALSE; |
| 253 |
| 254 | } else if (keymatch(arg, "fastcrush", 4)) { |
| 255 | jpeg_c_set_bool_param(cinfo, JBOOLEAN_OPTIMIZE_SCANS, FALSE); |
| 256 |
| 257 | } else if (keymatch(arg, "grayscale", 1) || keymatch(arg, "greyscale",1)) { |
| 258 | /* Force to grayscale. */ |
| 259 | #if TRANSFORMS_SUPPORTED |
| 260 | transformoption.force_grayscale = TRUE; |
| 261 | prefer_smallest = FALSE; |
| 262 | #else |
| 263 | select_transform(JXFORM_NONE); /* force an error */ |
| 264 | #endif |
| 265 |
| 266 | } else if (keymatch(arg, "icc", 1)) { |
| 267 | /* Set ICC filename. */ |
| 268 | if (++argn >= argc) /* advance to next argument */ |
| 269 | usage(); |
| 270 | icc_filename = argv[argn]; |
| 271 |
| 272 | } else if (keymatch(arg, "maxmemory", 3)) { |
| 273 | /* Maximum memory in Kb (or Mb with 'm'). */ |
| 274 | long lval; |
| 275 | char ch = 'x'; |
| 276 |
| 277 | if (++argn >= argc) /* advance to next argument */ |
| 278 | usage(); |
| 279 | if (sscanf(argv[argn], "%ld%c", &lval, &ch) < 1) |
| 280 | usage(); |
| 281 | if (ch == 'm' || ch == 'M') |
| 282 | lval *= 1000L; |
| 283 | cinfo->mem->max_memory_to_use = lval * 1000L; |
| 284 |
| 285 | } else if (keymatch(arg, "optimize", 1) || keymatch(arg, "optimise", 1)) { |
| 286 | /* Enable entropy parm optimization. */ |
| 287 | #ifdef ENTROPY_OPT_SUPPORTED |
| 288 | cinfo->optimize_coding = TRUE; |
| 289 | #else |
| 290 | fprintf(stderr, "%s: sorry, entropy optimization was not compiled\n", |
| 291 | progname); |
| 292 | exit(EXIT_FAILURE); |
| 293 | #endif |
| 294 |
| 295 | } else if (keymatch(arg, "outfile", 4)) { |
| 296 | /* Set output file name. */ |
| 297 | if (++argn >= argc) /* advance to next argument */ |
| 298 | usage(); |
| 299 | outfilename = argv[argn]; /* save it away for later use */ |
| 300 |
| 301 | } else if (keymatch(arg, "perfect", 2)) { |
| 302 | /* Fail if there is any partial edge MCUs that the transform can't |
| 303 | * handle. */ |
| 304 | transformoption.perfect = TRUE; |
| 305 |
| 306 | } else if (keymatch(arg, "progressive", 2)) { |
| 307 | /* Select simple progressive mode. */ |
| 308 | #ifdef C_PROGRESSIVE_SUPPORTED |
| 309 | simple_progressive = TRUE; |
| 310 | prefer_smallest = FALSE; |
| 311 | /* We must postpone execution until num_components is known. */ |
| 312 | #else |
| 313 | fprintf(stderr, "%s: sorry, progressive output was not compiled\n", |
| 314 | progname); |
| 315 | exit(EXIT_FAILURE); |
| 316 | #endif |
| 317 |
| 318 | } else if (keymatch(arg, "restart", 1)) { |
| 319 | /* Restart interval in MCU rows (or in MCUs with 'b'). */ |
| 320 | long lval; |
| 321 | char ch = 'x'; |
| 322 |
| 323 | if (++argn >= argc) /* advance to next argument */ |
| 324 | usage(); |
| 325 | if (sscanf(argv[argn], "%ld%c", &lval, &ch) < 1) |
| 326 | usage(); |
| 327 | if (lval < 0 || lval > 65535L) |
| 328 | usage(); |
| 329 | if (ch == 'b' || ch == 'B') { |
| 330 | cinfo->restart_interval = (unsigned int)lval; |
| 331 | cinfo->restart_in_rows = 0; /* else prior '-restart n' overrides me */ |
| 332 | } else { |
| 333 | cinfo->restart_in_rows = (int)lval; |
| 334 | /* restart_interval will be computed during startup */ |
| 335 | } |
| 336 |
| 337 | } else if (keymatch(arg, "revert", 3)) { |
| 338 | /* revert to old JPEG default */ |
| 339 | jpeg_c_set_int_param(cinfo, JINT_COMPRESS_PROFILE, JCP_FASTEST); |
| 340 | prefer_smallest = FALSE; |
| 341 |
| 342 | } else if (keymatch(arg, "rotate", 2)) { |
| 343 | /* Rotate 90, 180, or 270 degrees (measured clockwise). */ |
| 344 | if (++argn >= argc) /* advance to next argument */ |
| 345 | usage(); |
| 346 | if (keymatch(argv[argn], "90", 2)) |
| 347 | select_transform(JXFORM_ROT_90); |
| 348 | else if (keymatch(argv[argn], "180", 3)) |
| 349 | select_transform(JXFORM_ROT_180); |
| 350 | else if (keymatch(argv[argn], "270", 3)) |
| 351 | select_transform(JXFORM_ROT_270); |
| 352 | else |
| 353 | usage(); |
| 354 |
| 355 | prefer_smallest = FALSE; |
| 356 |
| 357 | } else if (keymatch(arg, "scans", 1)) { |
| 358 | /* Set scan script. */ |
| 359 | #ifdef C_MULTISCAN_FILES_SUPPORTED |
| 360 | if (++argn >= argc) /* advance to next argument */ |
| 361 | usage(); |
| 362 | prefer_smallest = FALSE; |
| 363 | scansarg = argv[argn]; |
| 364 | /* We must postpone reading the file in case -progressive appears. */ |
| 365 | #else |
| 366 | fprintf(stderr, "%s: sorry, multi-scan output was not compiled\n", |
| 367 | progname); |
| 368 | exit(EXIT_FAILURE); |
| 369 | #endif |
| 370 |
| 371 | } else if (keymatch(arg, "transpose", 1)) { |
| 372 | /* Transpose (across UL-to-LR axis). */ |
| 373 | select_transform(JXFORM_TRANSPOSE); |
| 374 | prefer_smallest = FALSE; |
| 375 |
| 376 | } else if (keymatch(arg, "transverse", 6)) { |
| 377 | /* Transverse transpose (across UR-to-LL axis). */ |
| 378 | select_transform(JXFORM_TRANSVERSE); |
| 379 | prefer_smallest = FALSE; |
| 380 |
| 381 | } else if (keymatch(arg, "trim", 3)) { |
| 382 | /* Trim off any partial edge MCUs that the transform can't handle. */ |
| 383 | transformoption.trim = TRUE; |
| 384 | prefer_smallest = FALSE; |
| 385 |
| 386 | } else { |
| 387 | usage(); /* bogus switch */ |
| 388 | } |
| 389 | } |
| 390 |
| 391 | /* Post-switch-scanning cleanup */ |
| 392 |
| 393 | if (for_real) { |
| 394 |
| 395 | #ifdef C_PROGRESSIVE_SUPPORTED |
| 396 | if (simple_progressive) /* process -progressive; -scans can override */ |
| 397 | jpeg_simple_progression(cinfo); |
| 398 | #endif |
| 399 |
| 400 | #ifdef C_MULTISCAN_FILES_SUPPORTED |
| 401 | if (scansarg != NULL) /* process -scans if it was present */ |
| 402 | if (!read_scan_script(cinfo, scansarg)) |
| 403 | usage(); |
| 404 | #endif |
| 405 |
| 406 | } |
| 407 |
| 408 | return argn; /* return index of next arg (file name) */ |
| 409 | } |
| 410 |
| 411 |
| 412 | /* |
| 413 | * The main program. |
| 414 | */ |
| 415 |
| 416 | int |
| 417 | main(int argc, char **argv) |
| 418 | { |
| 419 | struct jpeg_decompress_struct srcinfo; |
| 420 | struct jpeg_compress_struct dstinfo; |
| 421 | struct jpeg_error_mgr jsrcerr, jdsterr; |
| 422 | #ifdef PROGRESS_REPORT |
| 423 | struct cdjpeg_progress_mgr progress; |
| 424 | #endif |
| 425 | jvirt_barray_ptr *src_coef_arrays; |
| 426 | jvirt_barray_ptr *dst_coef_arrays; |
| 427 | int file_index; |
| 428 | /* We assume all-in-memory processing and can therefore use only a |
| 429 | * single file pointer for sequential input and output operation. |
| 430 | */ |
| 431 | FILE *fp; |
| 432 | unsigned char *inbuffer = NULL; |
| 433 | unsigned long insize = 0; |
| 434 | unsigned char *outbuffer = NULL; |
| 435 | unsigned long outsize = 0; |
| 436 | FILE *icc_file; |
| 437 | JOCTET *icc_profile = NULL; |
| 438 | long icc_len = 0; |
| 439 |
| 440 | /* On Mac, fetch a command line. */ |
| 441 | #ifdef USE_CCOMMAND |
| 442 | argc = ccommand(&argv); |
| 443 | #endif |
| 444 |
| 445 | progname = argv[0]; |
| 446 | if (progname == NULL || progname[0] == 0) |
| 447 | progname = "jpegtran"; /* in case C library doesn't provide it */ |
| 448 |
| 449 | /* Initialize the JPEG decompression object with default error handling. */ |
| 450 | srcinfo.err = jpeg_std_error(&jsrcerr); |
| 451 | jpeg_create_decompress(&srcinfo); |
| 452 | /* Initialize the JPEG compression object with default error handling. */ |
| 453 | dstinfo.err = jpeg_std_error(&jdsterr); |
| 454 | jpeg_create_compress(&dstinfo); |
| 455 |
| 456 | /* Scan command line to find file names. |
| 457 | * It is convenient to use just one switch-parsing routine, but the switch |
| 458 | * values read here are mostly ignored; we will rescan the switches after |
| 459 | * opening the input file. Also note that most of the switches affect the |
| 460 | * destination JPEG object, so we parse into that and then copy over what |
| 461 | * needs to affects the source too. |
| 462 | */ |
| 463 |
| 464 | file_index = parse_switches(&dstinfo, argc, argv, 0, FALSE); |
| 465 | jsrcerr.trace_level = jdsterr.trace_level; |
| 466 | srcinfo.mem->max_memory_to_use = dstinfo.mem->max_memory_to_use; |
| 467 |
| 468 | #ifdef TWO_FILE_COMMANDLINE |
| 469 | /* Must have either -outfile switch or explicit output file name */ |
| 470 | if (outfilename == NULL) { |
| 471 | if (file_index != argc - 2) { |
| 472 | fprintf(stderr, "%s: must name one input and one output file\n", |
| 473 | progname); |
| 474 | usage(); |
| 475 | } |
| 476 | outfilename = argv[file_index + 1]; |
| 477 | } else { |
| 478 | if (file_index != argc - 1) { |
| 479 | fprintf(stderr, "%s: must name one input and one output file\n", |
| 480 | progname); |
| 481 | usage(); |
| 482 | } |
| 483 | } |
| 484 | #else |
| 485 | /* Unix style: expect zero or one file name */ |
| 486 | if (file_index < argc - 1) { |
| 487 | fprintf(stderr, "%s: only one input file\n", progname); |
| 488 | usage(); |
| 489 | } |
| 490 | #endif /* TWO_FILE_COMMANDLINE */ |
| 491 |
| 492 | /* Open the input file. */ |
| 493 | if (file_index < argc) { |
| 494 | if ((fp = fopen(argv[file_index], READ_BINARY)) == NULL) { |
| 495 | fprintf(stderr, "%s: can't open %s for reading\n", progname, |
| 496 | argv[file_index]); |
| 497 | exit(EXIT_FAILURE); |
| 498 | } |
| 499 | } else { |
| 500 | /* default input file is stdin */ |
| 501 | fp = read_stdin(); |
| 502 | } |
| 503 |
| 504 | if (icc_filename != NULL) { |
| 505 | if ((icc_file = fopen(icc_filename, READ_BINARY)) == NULL) { |
| 506 | fprintf(stderr, "%s: can't open %s\n", progname, icc_filename); |
| 507 | exit(EXIT_FAILURE); |
| 508 | } |
| 509 | if (fseek(icc_file, 0, SEEK_END) < 0 || |
| 510 | (icc_len = ftell(icc_file)) < 1 || |
| 511 | fseek(icc_file, 0, SEEK_SET) < 0) { |
| 512 | fprintf(stderr, "%s: can't determine size of %s\n", progname, |
| 513 | icc_filename); |
| 514 | exit(EXIT_FAILURE); |
| 515 | } |
| 516 | if ((icc_profile = (JOCTET *)malloc(icc_len)) == NULL) { |
| 517 | fprintf(stderr, "%s: can't allocate memory for ICC profile\n", progname); |
| 518 | fclose(icc_file); |
| 519 | exit(EXIT_FAILURE); |
| 520 | } |
| 521 | if (fread(icc_profile, icc_len, 1, icc_file) < 1) { |
| 522 | fprintf(stderr, "%s: can't read ICC profile from %s\n", progname, |
| 523 | icc_filename); |
| 524 | free(icc_profile); |
| 525 | fclose(icc_file); |
| 526 | exit(EXIT_FAILURE); |
| 527 | } |
| 528 | fclose(icc_file); |
| 529 | if (copyoption == JCOPYOPT_ALL) |
| 530 | copyoption = JCOPYOPT_ALL_EXCEPT_ICC; |
| 531 | } |
| 532 |
| 533 | #ifdef PROGRESS_REPORT |
| 534 | start_progress_monitor((j_common_ptr)&dstinfo, &progress); |
| 535 | #endif |
| 536 |
| 537 | /* Specify data source for decompression */ |
| 538 | if (jpeg_c_int_param_supported(&dstinfo, JINT_COMPRESS_PROFILE) && |
| 539 | jpeg_c_get_int_param(&dstinfo, JINT_COMPRESS_PROFILE) |
| 540 | == JCP_MAX_COMPRESSION) |
| 541 | memsrc = TRUE; /* needed to revert to original */ |
| 542 | #if JPEG_LIB_VERSION >= 80 || defined(MEM_SRCDST_SUPPORTED) |
| 543 | if (memsrc) { |
| 544 | size_t nbytes; |
| 545 | do { |
| 546 | inbuffer = (unsigned char *)realloc(inbuffer, insize + INPUT_BUF_SIZE); |
| 547 | if (inbuffer == NULL) { |
| 548 | fprintf(stderr, "%s: memory allocation failure\n", progname); |
| 549 | exit(EXIT_FAILURE); |
| 550 | } |
| 551 | nbytes = JFREAD(fp, &inbuffer[insize], INPUT_BUF_SIZE); |
| 552 | if (nbytes < INPUT_BUF_SIZE && ferror(fp)) { |
| 553 | if (file_index < argc) |
| 554 | fprintf(stderr, "%s: can't read from %s\n", progname, |
| 555 | argv[file_index]); |
| 556 | else |
| 557 | fprintf(stderr, "%s: can't read from stdin\n", progname); |
| 558 | } |
| 559 | insize += (unsigned long)nbytes; |
| 560 | } while (nbytes == INPUT_BUF_SIZE); |
| 561 | jpeg_mem_src(&srcinfo, inbuffer, insize); |
| 562 | } else |
| 563 | #endif |
| 564 | jpeg_stdio_src(&srcinfo, fp); |
| 565 |
| 566 | /* Enable saving of extra markers that we want to copy */ |
| 567 | jcopy_markers_setup(&srcinfo, copyoption); |
| 568 |
| 569 | /* Read file header */ |
| 570 | (void)jpeg_read_header(&srcinfo, TRUE); |
| 571 |
| 572 | /* Any space needed by a transform option must be requested before |
| 573 | * jpeg_read_coefficients so that memory allocation will be done right. |
| 574 | */ |
| 575 | #if TRANSFORMS_SUPPORTED |
| 576 | /* Fail right away if -perfect is given and transformation is not perfect. |
| 577 | */ |
| 578 | if (!jtransform_request_workspace(&srcinfo, &transformoption)) { |
| 579 | fprintf(stderr, "%s: transformation is not perfect\n", progname); |
| 580 | exit(EXIT_FAILURE); |
| 581 | } |
| 582 | #endif |
| 583 |
| 584 | /* Read source file as DCT coefficients */ |
| 585 | src_coef_arrays = jpeg_read_coefficients(&srcinfo); |
| 586 |
| 587 | /* Initialize destination compression parameters from source values */ |
| 588 | jpeg_copy_critical_parameters(&srcinfo, &dstinfo); |
| 589 |
| 590 | /* Adjust destination parameters if required by transform options; |
| 591 | * also find out which set of coefficient arrays will hold the output. |
| 592 | */ |
| 593 | #if TRANSFORMS_SUPPORTED |
| 594 | dst_coef_arrays = jtransform_adjust_parameters(&srcinfo, &dstinfo, |
| 595 | src_coef_arrays, |
| 596 | &transformoption); |
| 597 | #else |
| 598 | dst_coef_arrays = src_coef_arrays; |
| 599 | #endif |
| 600 |
| 601 | /* Close input file, if we opened it. |
| 602 | * Note: we assume that jpeg_read_coefficients consumed all input |
| 603 | * until JPEG_REACHED_EOI, and that jpeg_finish_decompress will |
| 604 | * only consume more while (!cinfo->inputctl->eoi_reached). |
| 605 | * We cannot call jpeg_finish_decompress here since we still need the |
| 606 | * virtual arrays allocated from the source object for processing. |
| 607 | */ |
| 608 | if (fp != stdin) |
| 609 | fclose(fp); |
| 610 |
| 611 | /* Open the output file. */ |
| 612 | if (outfilename != NULL) { |
| 613 | if ((fp = fopen(outfilename, WRITE_BINARY)) == NULL) { |
| 614 | fprintf(stderr, "%s: can't open %s for writing\n", progname, |
| 615 | outfilename); |
| 616 | exit(EXIT_FAILURE); |
| 617 | } |
| 618 | } else { |
| 619 | /* default output file is stdout */ |
| 620 | fp = write_stdout(); |
| 621 | } |
| 622 |
| 623 | /* Adjust default compression parameters by re-parsing the options */ |
| 624 | file_index = parse_switches(&dstinfo, argc, argv, 0, TRUE); |
| 625 |
| 626 | /* Specify data destination for compression */ |
| 627 | #if JPEG_LIB_VERSION >= 80 || defined(MEM_SRCDST_SUPPORTED) |
| 628 | if (jpeg_c_int_param_supported(&dstinfo, JINT_COMPRESS_PROFILE) && |
| 629 | jpeg_c_get_int_param(&dstinfo, JINT_COMPRESS_PROFILE) |
| 630 | == JCP_MAX_COMPRESSION) |
| 631 | jpeg_mem_dest(&dstinfo, &outbuffer, &outsize); |
| 632 | else |
| 633 | #endif |
| 634 | jpeg_stdio_dest(&dstinfo, fp); |
| 635 |
| 636 | /* Start compressor (note no image data is actually written here) */ |
| 637 | jpeg_write_coefficients(&dstinfo, dst_coef_arrays); |
| 638 |
| 639 | /* Copy to the output file any extra markers that we want to preserve */ |
| 640 | jcopy_markers_execute(&srcinfo, &dstinfo, copyoption); |
| 641 |
| 642 | if (icc_profile != NULL) |
| 643 | jpeg_write_icc_profile(&dstinfo, icc_profile, (unsigned int)icc_len); |
| 644 |
| 645 | /* Execute image transformation, if any */ |
| 646 | #if TRANSFORMS_SUPPORTED |
| 647 | jtransform_execute_transformation(&srcinfo, &dstinfo, src_coef_arrays, |
| 648 | &transformoption); |
| 649 | #endif |
| 650 |
| 651 | /* Finish compression and release memory */ |
| 652 | jpeg_finish_compress(&dstinfo); |
| 653 |
| 654 | #if JPEG_LIB_VERSION >= 80 || defined(MEM_SRCDST_SUPPORTED) |
| 655 | if (jpeg_c_int_param_supported(&dstinfo, JINT_COMPRESS_PROFILE) && |
| 656 | jpeg_c_get_int_param(&dstinfo, JINT_COMPRESS_PROFILE) |
| 657 | == JCP_MAX_COMPRESSION) { |
| 658 | size_t nbytes; |
| 659 |
| 660 | unsigned char *buffer = outbuffer; |
| 661 | unsigned long size = outsize; |
| 662 | if (prefer_smallest && insize < size) { |
| 663 | size = insize; |
| 664 | buffer = inbuffer; |
| 665 | } |
| 666 |
| 667 | nbytes = JFWRITE(fp, buffer, size); |
| 668 | if (nbytes < size && ferror(fp)) { |
| 669 | if (file_index < argc) |
| 670 | fprintf(stderr, "%s: can't write to %s\n", progname, |
| 671 | argv[file_index]); |
| 672 | else |
| 673 | fprintf(stderr, "%s: can't write to stdout\n", progname); |
| 674 | } |
| 675 | } |
| 676 | #endif |
| 677 |
| 678 | jpeg_destroy_compress(&dstinfo); |
| 679 | (void)jpeg_finish_decompress(&srcinfo); |
| 680 | jpeg_destroy_decompress(&srcinfo); |
| 681 |
| 682 | /* Close output file, if we opened it */ |
| 683 | if (fp != stdout) |
| 684 | fclose(fp); |
| 685 |
| 686 | #ifdef PROGRESS_REPORT |
| 687 | end_progress_monitor((j_common_ptr)&dstinfo); |
| 688 | #endif |
| 689 |
| 690 | free(inbuffer); |
| 691 | free(outbuffer); |
| 692 |
| 693 | free(icc_profile); |
| 694 |
| 695 | /* All done. */ |
| 696 | exit(jsrcerr.num_warnings + jdsterr.num_warnings ? |
| 697 | EXIT_WARNING : EXIT_SUCCESS); |
| 698 | return 0; /* suppress no-return-value warnings */ |
| 699 | } |
| 700 |