Team Fortress 2 Source Code as on 22/4/2020
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

560 lines
18 KiB

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