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, D. R. Commander.
  8. * For conditions of distribution and use, see the accompanying README file.
  9. *
  10. * This file contains a command-line user interface for JPEG transcoding.
  11. * It is very similar to cjpeg.c, and partly to djpeg.c, but provides
  12. * lossless transcoding between different JPEG file formats. It also
  13. * provides some lossless and sort-of-lossless transformations of JPEG data.
  14. */
  15. #include "cdjpeg.h" /* Common decls for cjpeg/djpeg applications */
  16. #include "transupp.h" /* Support routines for jpegtran */
  17. #include "jversion.h" /* for version message */
  18. #include "config.h"
  19. #ifdef USE_CCOMMAND /* command-line reader for Macintosh */
  20. #ifdef __MWERKS__
  21. #include <SIOUX.h> /* Metrowerks needs this */
  22. #include <console.h> /* ... and this */
  23. #endif
  24. #ifdef THINK_C
  25. #include <console.h> /* Think declares it here */
  26. #endif
  27. #endif
  28. /*
  29. * Argument-parsing code.
  30. * The switch parser is designed to be useful with DOS-style command line
  31. * syntax, ie, intermixed switches and file names, where only the switches
  32. * to the left of a given file name affect processing of that file.
  33. * The main program in this file doesn't actually use this capability...
  34. */
  35. static const char * progname; /* program name for error messages */
  36. static char * outfilename; /* for -outfile 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 all Copy all extra markers\n");
  53. #ifdef ENTROPY_OPT_SUPPORTED
  54. fprintf(stderr, " -optimize Optimize Huffman table (smaller file, but slow compression)\n");
  55. #endif
  56. #ifdef C_PROGRESSIVE_SUPPORTED
  57. fprintf(stderr, " -progressive Create progressive JPEG file\n");
  58. #endif
  59. fprintf(stderr, "Switches for modifying the image:\n");
  60. #if TRANSFORMS_SUPPORTED
  61. fprintf(stderr, " -crop WxH+X+Y Crop to a rectangular subarea\n");
  62. fprintf(stderr, " -grayscale Reduce to grayscale (omit color data)\n");
  63. fprintf(stderr, " -flip [horizontal|vertical] Mirror image (left-right or top-bottom)\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. #if TRANSFORMS_SUPPORTED
  68. fprintf(stderr, " -transpose Transpose image\n");
  69. fprintf(stderr, " -transverse Transverse transpose image\n");
  70. fprintf(stderr, " -trim Drop non-transformable edge blocks\n");
  71. #endif
  72. fprintf(stderr, "Switches for advanced users:\n");
  73. #ifdef C_ARITH_CODING_SUPPORTED
  74. fprintf(stderr, " -arithmetic Use arithmetic coding\n");
  75. #endif
  76. fprintf(stderr, " -restart N Set restart interval in rows, or in blocks with B\n");
  77. fprintf(stderr, " -maxmemory N Maximum memory to use (in kbytes)\n");
  78. fprintf(stderr, " -outfile name Specify name for output file\n");
  79. fprintf(stderr, " -verbose or -debug Emit debug output\n");
  80. fprintf(stderr, "Switches for wizards:\n");
  81. #ifdef C_MULTISCAN_FILES_SUPPORTED
  82. fprintf(stderr, " -scans file Create multi-scan JPEG per script file\n");
  83. #endif
  84. exit(EXIT_FAILURE);
  85. }
  86. LOCAL(void)
  87. select_transform (JXFORM_CODE transform)
  88. /* Silly little routine to detect multiple transform options,
  89. * which we can't handle.
  90. */
  91. {
  92. #if TRANSFORMS_SUPPORTED
  93. if (transformoption.transform == JXFORM_NONE ||
  94. transformoption.transform == transform) {
  95. transformoption.transform = transform;
  96. } else {
  97. fprintf(stderr, "%s: can only do one image transformation at a time\n",
  98. progname);
  99. usage();
  100. }
  101. #else
  102. fprintf(stderr, "%s: sorry, image transformation was not compiled\n",
  103. progname);
  104. exit(EXIT_FAILURE);
  105. #endif
  106. }
  107. LOCAL(int)
  108. parse_switches (j_compress_ptr cinfo, int argc, char **argv,
  109. int last_file_arg_seen, boolean for_real)
  110. /* Parse optional switches.
  111. * Returns argv[] index of first file-name argument (== argc if none).
  112. * Any file names with indexes <= last_file_arg_seen are ignored;
  113. * they have presumably been processed in a previous iteration.
  114. * (Pass 0 for last_file_arg_seen on the first or only iteration.)
  115. * for_real is FALSE on the first (dummy) pass; we may skip any expensive
  116. * processing.
  117. */
  118. {
  119. int argn;
  120. char * arg;
  121. boolean simple_progressive;
  122. char * scansarg = NULL; /* saves -scans parm if any */
  123. /* Set up default JPEG parameters. */
  124. simple_progressive = FALSE;
  125. outfilename = NULL;
  126. copyoption = JCOPYOPT_DEFAULT;
  127. transformoption.transform = JXFORM_NONE;
  128. transformoption.perfect = FALSE;
  129. transformoption.trim = FALSE;
  130. transformoption.force_grayscale = FALSE;
  131. transformoption.crop = FALSE;
  132. transformoption.slow_hflip = FALSE;
  133. cinfo->err->trace_level = 0;
  134. /* Scan command line options, adjust parameters */
  135. for (argn = 1; argn < argc; argn++) {
  136. arg = argv[argn];
  137. if (*arg != '-') {
  138. /* Not a switch, must be a file name argument */
  139. if (argn <= last_file_arg_seen) {
  140. outfilename = NULL; /* -outfile applies to just one input file */
  141. continue; /* ignore this name if previously processed */
  142. }
  143. break; /* else done parsing switches */
  144. }
  145. arg++; /* advance past switch marker character */
  146. if (keymatch(arg, "arithmetic", 1)) {
  147. /* Use arithmetic coding. */
  148. #ifdef C_ARITH_CODING_SUPPORTED
  149. cinfo->arith_code = TRUE;
  150. #else
  151. fprintf(stderr, "%s: sorry, arithmetic coding not supported\n",
  152. progname);
  153. exit(EXIT_FAILURE);
  154. #endif
  155. } else if (keymatch(arg, "copy", 2)) {
  156. /* Select which extra markers to copy. */
  157. if (++argn >= argc) /* advance to next argument */
  158. usage();
  159. if (keymatch(argv[argn], "none", 1)) {
  160. copyoption = JCOPYOPT_NONE;
  161. } else if (keymatch(argv[argn], "comments", 1)) {
  162. copyoption = JCOPYOPT_COMMENTS;
  163. } else if (keymatch(argv[argn], "all", 1)) {
  164. copyoption = JCOPYOPT_ALL;
  165. } else
  166. usage();
  167. } else if (keymatch(arg, "crop", 2)) {
  168. /* Perform lossless cropping. */
  169. #if TRANSFORMS_SUPPORTED
  170. if (++argn >= argc) /* advance to next argument */
  171. usage();
  172. if (! jtransform_parse_crop_spec(&transformoption, argv[argn])) {
  173. fprintf(stderr, "%s: bogus -crop argument '%s'\n",
  174. progname, argv[argn]);
  175. exit(EXIT_FAILURE);
  176. }
  177. #else
  178. select_transform(JXFORM_NONE); /* force an error */
  179. #endif
  180. } else if (keymatch(arg, "debug", 1) || keymatch(arg, "verbose", 1)) {
  181. /* Enable debug printouts. */
  182. /* On first -d, print version identification */
  183. static boolean printed_version = FALSE;
  184. if (! printed_version) {
  185. fprintf(stderr, "%s version %s (build %s)\n",
  186. PACKAGE_NAME, VERSION, BUILD);
  187. fprintf(stderr, "%s\n\n", JCOPYRIGHT);
  188. fprintf(stderr, "Emulating The Independent JPEG Group's software, version %s\n\n",
  189. JVERSION);
  190. printed_version = TRUE;
  191. }
  192. cinfo->err->trace_level++;
  193. } else if (keymatch(arg, "flip", 1)) {
  194. /* Mirror left-right or top-bottom. */
  195. if (++argn >= argc) /* advance to next argument */
  196. usage();
  197. if (keymatch(argv[argn], "horizontal", 1))
  198. select_transform(JXFORM_FLIP_H);
  199. else if (keymatch(argv[argn], "vertical", 1))
  200. select_transform(JXFORM_FLIP_V);
  201. else
  202. usage();
  203. } else if (keymatch(arg, "grayscale", 1) || keymatch(arg, "greyscale",1)) {
  204. /* Force to grayscale. */
  205. #if TRANSFORMS_SUPPORTED
  206. transformoption.force_grayscale = TRUE;
  207. #else
  208. select_transform(JXFORM_NONE); /* force an error */
  209. #endif
  210. } else if (keymatch(arg, "maxmemory", 3)) {
  211. /* Maximum memory in Kb (or Mb with 'm'). */
  212. long lval;
  213. char ch = 'x';
  214. if (++argn >= argc) /* advance to next argument */
  215. usage();
  216. if (sscanf(argv[argn], "%ld%c", &lval, &ch) < 1)
  217. usage();
  218. if (ch == 'm' || ch == 'M')
  219. lval *= 1000L;
  220. cinfo->mem->max_memory_to_use = lval * 1000L;
  221. } else if (keymatch(arg, "optimize", 1) || keymatch(arg, "optimise", 1)) {
  222. /* Enable entropy parm optimization. */
  223. #ifdef ENTROPY_OPT_SUPPORTED
  224. cinfo->optimize_coding = TRUE;
  225. #else
  226. fprintf(stderr, "%s: sorry, entropy optimization was not compiled\n",
  227. progname);
  228. exit(EXIT_FAILURE);
  229. #endif
  230. } else if (keymatch(arg, "outfile", 4)) {
  231. /* Set output file name. */
  232. if (++argn >= argc) /* advance to next argument */
  233. usage();
  234. outfilename = argv[argn]; /* save it away for later use */
  235. } else if (keymatch(arg, "perfect", 2)) {
  236. /* Fail if there is any partial edge MCUs that the transform can't
  237. * handle. */
  238. transformoption.perfect = TRUE;
  239. } else if (keymatch(arg, "progressive", 2)) {
  240. /* Select simple progressive mode. */
  241. #ifdef C_PROGRESSIVE_SUPPORTED
  242. simple_progressive = TRUE;
  243. /* We must postpone execution until num_components is known. */
  244. #else
  245. fprintf(stderr, "%s: sorry, progressive output was not compiled\n",
  246. progname);
  247. exit(EXIT_FAILURE);
  248. #endif
  249. } else if (keymatch(arg, "restart", 1)) {
  250. /* Restart interval in MCU rows (or in MCUs with 'b'). */
  251. long lval;
  252. char ch = 'x';
  253. if (++argn >= argc) /* advance to next argument */
  254. usage();
  255. if (sscanf(argv[argn], "%ld%c", &lval, &ch) < 1)
  256. usage();
  257. if (lval < 0 || lval > 65535L)
  258. usage();
  259. if (ch == 'b' || ch == 'B') {
  260. cinfo->restart_interval = (unsigned int) lval;
  261. cinfo->restart_in_rows = 0; /* else prior '-restart n' overrides me */
  262. } else {
  263. cinfo->restart_in_rows = (int) lval;
  264. /* restart_interval will be computed during startup */
  265. }
  266. } else if (keymatch(arg, "rotate", 2)) {
  267. /* Rotate 90, 180, or 270 degrees (measured clockwise). */
  268. if (++argn >= argc) /* advance to next argument */
  269. usage();
  270. if (keymatch(argv[argn], "90", 2))
  271. select_transform(JXFORM_ROT_90);
  272. else if (keymatch(argv[argn], "180", 3))
  273. select_transform(JXFORM_ROT_180);
  274. else if (keymatch(argv[argn], "270", 3))
  275. select_transform(JXFORM_ROT_270);
  276. else
  277. usage();
  278. } else if (keymatch(arg, "scans", 1)) {
  279. /* Set scan script. */
  280. #ifdef C_MULTISCAN_FILES_SUPPORTED
  281. if (++argn >= argc) /* advance to next argument */
  282. usage();
  283. scansarg = argv[argn];
  284. /* We must postpone reading the file in case -progressive appears. */
  285. #else
  286. fprintf(stderr, "%s: sorry, multi-scan output was not compiled\n",
  287. progname);
  288. exit(EXIT_FAILURE);
  289. #endif
  290. } else if (keymatch(arg, "transpose", 1)) {
  291. /* Transpose (across UL-to-LR axis). */
  292. select_transform(JXFORM_TRANSPOSE);
  293. } else if (keymatch(arg, "transverse", 6)) {
  294. /* Transverse transpose (across UR-to-LL axis). */
  295. select_transform(JXFORM_TRANSVERSE);
  296. } else if (keymatch(arg, "trim", 3)) {
  297. /* Trim off any partial edge MCUs that the transform can't handle. */
  298. transformoption.trim = TRUE;
  299. } else {
  300. usage(); /* bogus switch */
  301. }
  302. }
  303. /* Post-switch-scanning cleanup */
  304. if (for_real) {
  305. #ifdef C_PROGRESSIVE_SUPPORTED
  306. if (simple_progressive) /* process -progressive; -scans can override */
  307. jpeg_simple_progression(cinfo);
  308. #endif
  309. #ifdef C_MULTISCAN_FILES_SUPPORTED
  310. if (scansarg != NULL) /* process -scans if it was present */
  311. if (! read_scan_script(cinfo, scansarg))
  312. usage();
  313. #endif
  314. }
  315. return argn; /* return index of next arg (file name) */
  316. }
  317. /*
  318. * The main program.
  319. */
  320. int
  321. main (int argc, char **argv)
  322. {
  323. struct jpeg_decompress_struct srcinfo;
  324. struct jpeg_compress_struct dstinfo;
  325. struct jpeg_error_mgr jsrcerr, jdsterr;
  326. #ifdef PROGRESS_REPORT
  327. struct cdjpeg_progress_mgr progress;
  328. #endif
  329. jvirt_barray_ptr * src_coef_arrays;
  330. jvirt_barray_ptr * dst_coef_arrays;
  331. int file_index;
  332. /* We assume all-in-memory processing and can therefore use only a
  333. * single file pointer for sequential input and output operation.
  334. */
  335. FILE * fp;
  336. /* On Mac, fetch a command line. */
  337. #ifdef USE_CCOMMAND
  338. argc = ccommand(&argv);
  339. #endif
  340. progname = argv[0];
  341. if (progname == NULL || progname[0] == 0)
  342. progname = "jpegtran"; /* in case C library doesn't provide it */
  343. /* Initialize the JPEG decompression object with default error handling. */
  344. srcinfo.err = jpeg_std_error(&jsrcerr);
  345. jpeg_create_decompress(&srcinfo);
  346. /* Initialize the JPEG compression object with default error handling. */
  347. dstinfo.err = jpeg_std_error(&jdsterr);
  348. jpeg_create_compress(&dstinfo);
  349. /* Now safe to enable signal catcher.
  350. * Note: we assume only the decompression object will have virtual arrays.
  351. */
  352. #ifdef NEED_SIGNAL_CATCHER
  353. enable_signal_catcher((j_common_ptr) &srcinfo);
  354. #endif
  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. }