jpegtran.c 16 KB

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