jpegtran.c 17 KB

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