jpegtran.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  1. /*
  2. * jpegtran.c
  3. *
  4. * Copyright (C) 1995, 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.
  11. */
  12. #include "cdjpeg.h" /* Common decls for cjpeg/djpeg applications */
  13. #include "jversion.h" /* for version message */
  14. #ifdef USE_CCOMMAND /* command-line reader for Macintosh */
  15. #ifdef __MWERKS__
  16. #include <SIOUX.h> /* Metrowerks declares it here */
  17. #endif
  18. #ifdef THINK_C
  19. #include <console.h> /* Think declares it here */
  20. #endif
  21. #endif
  22. /*
  23. * Argument-parsing code.
  24. * The switch parser is designed to be useful with DOS-style command line
  25. * syntax, ie, intermixed switches and file names, where only the switches
  26. * to the left of a given file name affect processing of that file.
  27. * The main program in this file doesn't actually use this capability...
  28. */
  29. static const char * progname; /* program name for error messages */
  30. static char * outfilename; /* for -outfile switch */
  31. LOCAL void
  32. usage (void)
  33. /* complain about bad command line */
  34. {
  35. fprintf(stderr, "usage: %s [switches] ", progname);
  36. #ifdef TWO_FILE_COMMANDLINE
  37. fprintf(stderr, "inputfile outputfile\n");
  38. #else
  39. fprintf(stderr, "[inputfile]\n");
  40. #endif
  41. fprintf(stderr, "Switches (names may be abbreviated):\n");
  42. #ifdef ENTROPY_OPT_SUPPORTED
  43. fprintf(stderr, " -optimize Optimize Huffman table (smaller file, but slow compression)\n");
  44. #endif
  45. #ifdef C_PROGRESSIVE_SUPPORTED
  46. fprintf(stderr, " -progressive Create progressive JPEG file\n");
  47. #endif
  48. fprintf(stderr, "Switches for advanced users:\n");
  49. fprintf(stderr, " -restart N Set restart interval in rows, or in blocks with B\n");
  50. fprintf(stderr, " -maxmemory N Maximum memory to use (in kbytes)\n");
  51. fprintf(stderr, " -outfile name Specify name for output file\n");
  52. fprintf(stderr, " -verbose or -debug Emit debug output\n");
  53. fprintf(stderr, "Switches for wizards:\n");
  54. #ifdef C_ARITH_CODING_SUPPORTED
  55. fprintf(stderr, " -arithmetic Use arithmetic coding\n");
  56. #endif
  57. #ifdef C_MULTISCAN_FILES_SUPPORTED
  58. fprintf(stderr, " -scans file Create multi-scan JPEG per script file\n");
  59. #endif
  60. exit(EXIT_FAILURE);
  61. }
  62. LOCAL int
  63. parse_switches (j_compress_ptr cinfo, int argc, char **argv,
  64. int last_file_arg_seen, boolean for_real)
  65. /* Parse optional switches.
  66. * Returns argv[] index of first file-name argument (== argc if none).
  67. * Any file names with indexes <= last_file_arg_seen are ignored;
  68. * they have presumably been processed in a previous iteration.
  69. * (Pass 0 for last_file_arg_seen on the first or only iteration.)
  70. * for_real is FALSE on the first (dummy) pass; we may skip any expensive
  71. * processing.
  72. */
  73. {
  74. int argn;
  75. char * arg;
  76. boolean simple_progressive;
  77. char * scansarg = NULL; /* saves -scans parm if any */
  78. /* Set up default JPEG parameters. */
  79. simple_progressive = FALSE;
  80. outfilename = NULL;
  81. cinfo->err->trace_level = 0;
  82. /* Scan command line options, adjust parameters */
  83. for (argn = 1; argn < argc; argn++) {
  84. arg = argv[argn];
  85. if (*arg != '-') {
  86. /* Not a switch, must be a file name argument */
  87. if (argn <= last_file_arg_seen) {
  88. outfilename = NULL; /* -outfile applies to just one input file */
  89. continue; /* ignore this name if previously processed */
  90. }
  91. break; /* else done parsing switches */
  92. }
  93. arg++; /* advance past switch marker character */
  94. if (keymatch(arg, "arithmetic", 1)) {
  95. /* Use arithmetic coding. */
  96. #ifdef C_ARITH_CODING_SUPPORTED
  97. cinfo->arith_code = TRUE;
  98. #else
  99. fprintf(stderr, "%s: sorry, arithmetic coding not supported\n",
  100. progname);
  101. exit(EXIT_FAILURE);
  102. #endif
  103. } else if (keymatch(arg, "debug", 1) || keymatch(arg, "verbose", 1)) {
  104. /* Enable debug printouts. */
  105. /* On first -d, print version identification */
  106. static boolean printed_version = FALSE;
  107. if (! printed_version) {
  108. fprintf(stderr, "Independent JPEG Group's JPEGTRAN, version %s\n%s\n",
  109. JVERSION, JCOPYRIGHT);
  110. printed_version = TRUE;
  111. }
  112. cinfo->err->trace_level++;
  113. } else if (keymatch(arg, "maxmemory", 3)) {
  114. /* Maximum memory in Kb (or Mb with 'm'). */
  115. long lval;
  116. char ch = 'x';
  117. if (++argn >= argc) /* advance to next argument */
  118. usage();
  119. if (sscanf(argv[argn], "%ld%c", &lval, &ch) < 1)
  120. usage();
  121. if (ch == 'm' || ch == 'M')
  122. lval *= 1000L;
  123. cinfo->mem->max_memory_to_use = lval * 1000L;
  124. } else if (keymatch(arg, "optimize", 1) || keymatch(arg, "optimise", 1)) {
  125. /* Enable entropy parm optimization. */
  126. #ifdef ENTROPY_OPT_SUPPORTED
  127. cinfo->optimize_coding = TRUE;
  128. #else
  129. fprintf(stderr, "%s: sorry, entropy optimization was not compiled\n",
  130. progname);
  131. exit(EXIT_FAILURE);
  132. #endif
  133. } else if (keymatch(arg, "outfile", 4)) {
  134. /* Set output file name. */
  135. if (++argn >= argc) /* advance to next argument */
  136. usage();
  137. outfilename = argv[argn]; /* save it away for later use */
  138. } else if (keymatch(arg, "progressive", 1)) {
  139. /* Select simple progressive mode. */
  140. #ifdef C_PROGRESSIVE_SUPPORTED
  141. simple_progressive = TRUE;
  142. /* We must postpone execution until num_components is known. */
  143. #else
  144. fprintf(stderr, "%s: sorry, progressive output was not compiled\n",
  145. progname);
  146. exit(EXIT_FAILURE);
  147. #endif
  148. } else if (keymatch(arg, "restart", 1)) {
  149. /* Restart interval in MCU rows (or in MCUs with 'b'). */
  150. long lval;
  151. char ch = 'x';
  152. if (++argn >= argc) /* advance to next argument */
  153. usage();
  154. if (sscanf(argv[argn], "%ld%c", &lval, &ch) < 1)
  155. usage();
  156. if (lval < 0 || lval > 65535L)
  157. usage();
  158. if (ch == 'b' || ch == 'B') {
  159. cinfo->restart_interval = (unsigned int) lval;
  160. cinfo->restart_in_rows = 0; /* else prior '-restart n' overrides me */
  161. } else {
  162. cinfo->restart_in_rows = (int) lval;
  163. /* restart_interval will be computed during startup */
  164. }
  165. } else if (keymatch(arg, "scans", 2)) {
  166. /* Set scan script. */
  167. #ifdef C_MULTISCAN_FILES_SUPPORTED
  168. if (++argn >= argc) /* advance to next argument */
  169. usage();
  170. scansarg = argv[argn];
  171. /* We must postpone reading the file in case -progressive appears. */
  172. #else
  173. fprintf(stderr, "%s: sorry, multi-scan output was not compiled\n",
  174. progname);
  175. exit(EXIT_FAILURE);
  176. #endif
  177. } else {
  178. usage(); /* bogus switch */
  179. }
  180. }
  181. /* Post-switch-scanning cleanup */
  182. if (for_real) {
  183. #ifdef C_PROGRESSIVE_SUPPORTED
  184. if (simple_progressive) /* process -progressive; -scans can override */
  185. jpeg_simple_progression(cinfo);
  186. #endif
  187. #ifdef C_MULTISCAN_FILES_SUPPORTED
  188. if (scansarg != NULL) /* process -scans if it was present */
  189. if (! read_scan_script(cinfo, scansarg))
  190. usage();
  191. #endif
  192. }
  193. return argn; /* return index of next arg (file name) */
  194. }
  195. /*
  196. * The main program.
  197. */
  198. GLOBAL int
  199. main (int argc, char **argv)
  200. {
  201. struct jpeg_decompress_struct srcinfo;
  202. struct jpeg_compress_struct dstinfo;
  203. struct jpeg_error_mgr jsrcerr, jdsterr;
  204. #ifdef PROGRESS_REPORT
  205. struct cdjpeg_progress_mgr progress;
  206. #endif
  207. jvirt_barray_ptr * coef_arrays;
  208. int file_index;
  209. FILE * input_file;
  210. FILE * output_file;
  211. /* On Mac, fetch a command line. */
  212. #ifdef USE_CCOMMAND
  213. argc = ccommand(&argv);
  214. #endif
  215. progname = argv[0];
  216. if (progname == NULL || progname[0] == 0)
  217. progname = "jpegtran"; /* in case C library doesn't provide it */
  218. /* Initialize the JPEG decompression object with default error handling. */
  219. srcinfo.err = jpeg_std_error(&jsrcerr);
  220. jpeg_create_decompress(&srcinfo);
  221. /* Initialize the JPEG compression object with default error handling. */
  222. dstinfo.err = jpeg_std_error(&jdsterr);
  223. jpeg_create_compress(&dstinfo);
  224. /* Now safe to enable signal catcher.
  225. * Note: we assume only the decompression object will have virtual arrays.
  226. */
  227. #ifdef NEED_SIGNAL_CATCHER
  228. enable_signal_catcher((j_common_ptr) &srcinfo);
  229. #endif
  230. /* Scan command line to find file names.
  231. * It is convenient to use just one switch-parsing routine, but the switch
  232. * values read here are ignored; we will rescan the switches after opening
  233. * the input file.
  234. */
  235. file_index = parse_switches(&dstinfo, argc, argv, 0, FALSE);
  236. jsrcerr.trace_level = jdsterr.trace_level;
  237. #ifdef TWO_FILE_COMMANDLINE
  238. /* Must have either -outfile switch or explicit output file name */
  239. if (outfilename == NULL) {
  240. if (file_index != argc-2) {
  241. fprintf(stderr, "%s: must name one input and one output file\n",
  242. progname);
  243. usage();
  244. }
  245. outfilename = argv[file_index+1];
  246. } else {
  247. if (file_index != argc-1) {
  248. fprintf(stderr, "%s: must name one input and one output file\n",
  249. progname);
  250. usage();
  251. }
  252. }
  253. #else
  254. /* Unix style: expect zero or one file name */
  255. if (file_index < argc-1) {
  256. fprintf(stderr, "%s: only one input file\n", progname);
  257. usage();
  258. }
  259. #endif /* TWO_FILE_COMMANDLINE */
  260. /* Open the input file. */
  261. if (file_index < argc) {
  262. if ((input_file = fopen(argv[file_index], READ_BINARY)) == NULL) {
  263. fprintf(stderr, "%s: can't open %s\n", progname, argv[file_index]);
  264. exit(EXIT_FAILURE);
  265. }
  266. } else {
  267. /* default input file is stdin */
  268. input_file = read_stdin();
  269. }
  270. /* Open the output file. */
  271. if (outfilename != NULL) {
  272. if ((output_file = fopen(outfilename, WRITE_BINARY)) == NULL) {
  273. fprintf(stderr, "%s: can't open %s\n", progname, outfilename);
  274. exit(EXIT_FAILURE);
  275. }
  276. } else {
  277. /* default output file is stdout */
  278. output_file = write_stdout();
  279. }
  280. #ifdef PROGRESS_REPORT
  281. start_progress_monitor((j_common_ptr) &dstinfo, &progress);
  282. #endif
  283. /* Specify data source for decompression */
  284. jpeg_stdio_src(&srcinfo, input_file);
  285. /* Read file header */
  286. (void) jpeg_read_header(&srcinfo, TRUE);
  287. /* Read source file as DCT coefficients */
  288. coef_arrays = jpeg_read_coefficients(&srcinfo);
  289. /* Initialize destination compression parameters from source values */
  290. jpeg_copy_critical_parameters(&srcinfo, &dstinfo);
  291. /* Adjust default compression parameters by re-parsing the options */
  292. file_index = parse_switches(&dstinfo, argc, argv, 0, TRUE);
  293. /* Specify data destination for compression */
  294. jpeg_stdio_dest(&dstinfo, output_file);
  295. /* Start compressor */
  296. jpeg_write_coefficients(&dstinfo, coef_arrays);
  297. /* ought to copy source comments here... */
  298. /* Finish compression and release memory */
  299. jpeg_finish_compress(&dstinfo);
  300. jpeg_destroy_compress(&dstinfo);
  301. (void) jpeg_finish_decompress(&srcinfo);
  302. jpeg_destroy_decompress(&srcinfo);
  303. /* Close files, if we opened them */
  304. if (input_file != stdin)
  305. fclose(input_file);
  306. if (output_file != stdout)
  307. fclose(output_file);
  308. #ifdef PROGRESS_REPORT
  309. end_progress_monitor((j_common_ptr) &dstinfo);
  310. #endif
  311. /* All done. */
  312. exit(jsrcerr.num_warnings + jdsterr.num_warnings ?EXIT_WARNING:EXIT_SUCCESS);
  313. return 0; /* suppress no-return-value warnings */
  314. }