jpegtran.c 24 KB

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