jpegtran.c 21 KB

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