jpegtran.c 16 KB

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