jpegtran.c 18 KB

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