cjpeg.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665
  1. /*
  2. * cjpeg.c
  3. *
  4. * Copyright (C) 1991-1998, Thomas G. Lane.
  5. * Modified 2003-2013 by Guido Vollbeding.
  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 the JPEG compressor.
  10. * It should work on any system with Unix- or MS-DOS-style command lines.
  11. *
  12. * Two different command line styles are permitted, depending on the
  13. * compile-time switch TWO_FILE_COMMANDLINE:
  14. * cjpeg [options] inputfile outputfile
  15. * cjpeg [options] [inputfile]
  16. * In the second style, output is always to standard output, which you'd
  17. * normally redirect to a file or pipe to some other program. Input is
  18. * either from a named file or from standard input (typically redirected).
  19. * The second style is convenient on Unix but is unhelpful on systems that
  20. * don't support pipes. Also, you MUST use the first style if your system
  21. * doesn't do binary I/O to stdin/stdout.
  22. * To simplify script writing, the "-outfile" switch is provided. The syntax
  23. * cjpeg [options] -outfile outputfile inputfile
  24. * works regardless of which command line style is used.
  25. */
  26. #include "cdjpeg.h" /* Common decls for cjpeg/djpeg applications */
  27. #include "jversion.h" /* for version message */
  28. #ifdef USE_CCOMMAND /* command-line reader for Macintosh */
  29. #ifdef __MWERKS__
  30. #include <SIOUX.h> /* Metrowerks needs this */
  31. #include <console.h> /* ... and this */
  32. #endif
  33. #ifdef THINK_C
  34. #include <console.h> /* Think declares it here */
  35. #endif
  36. #endif
  37. /* Create the add-on message string table. */
  38. #define JMESSAGE(code,string) string ,
  39. static const char * const cdjpeg_message_table[] = {
  40. #include "cderror.h"
  41. NULL
  42. };
  43. /*
  44. * This routine determines what format the input file is,
  45. * and selects the appropriate input-reading module.
  46. *
  47. * To determine which family of input formats the file belongs to,
  48. * we may look only at the first byte of the file, since C does not
  49. * guarantee that more than one character can be pushed back with ungetc.
  50. * Looking at additional bytes would require one of these approaches:
  51. * 1) assume we can fseek() the input file (fails for piped input);
  52. * 2) assume we can push back more than one character (works in
  53. * some C implementations, but unportable);
  54. * 3) provide our own buffering (breaks input readers that want to use
  55. * stdio directly, such as the RLE library);
  56. * or 4) don't put back the data, and modify the input_init methods to assume
  57. * they start reading after the start of file (also breaks RLE library).
  58. * #1 is attractive for MS-DOS but is untenable on Unix.
  59. *
  60. * The most portable solution for file types that can't be identified by their
  61. * first byte is to make the user tell us what they are. This is also the
  62. * only approach for "raw" file types that contain only arbitrary values.
  63. * We presently apply this method for Targa files. Most of the time Targa
  64. * files start with 0x00, so we recognize that case. Potentially, however,
  65. * a Targa file could start with any byte value (byte 0 is the length of the
  66. * seldom-used ID field), so we provide a switch to force Targa input mode.
  67. */
  68. static boolean is_targa; /* records user -targa switch */
  69. LOCAL(cjpeg_source_ptr)
  70. select_file_type (j_compress_ptr cinfo, FILE * infile)
  71. {
  72. int c;
  73. if (is_targa) {
  74. #ifdef TARGA_SUPPORTED
  75. return jinit_read_targa(cinfo);
  76. #else
  77. ERREXIT(cinfo, JERR_TGA_NOTCOMP);
  78. #endif
  79. }
  80. if ((c = getc(infile)) == EOF)
  81. ERREXIT(cinfo, JERR_INPUT_EMPTY);
  82. if (ungetc(c, infile) == EOF)
  83. ERREXIT(cinfo, JERR_UNGETC_FAILED);
  84. switch (c) {
  85. #ifdef BMP_SUPPORTED
  86. case 'B':
  87. return jinit_read_bmp(cinfo);
  88. #endif
  89. #ifdef GIF_SUPPORTED
  90. case 'G':
  91. return jinit_read_gif(cinfo);
  92. #endif
  93. #ifdef PPM_SUPPORTED
  94. case 'P':
  95. return jinit_read_ppm(cinfo);
  96. #endif
  97. #ifdef RLE_SUPPORTED
  98. case 'R':
  99. return jinit_read_rle(cinfo);
  100. #endif
  101. #ifdef TARGA_SUPPORTED
  102. case 0x00:
  103. return jinit_read_targa(cinfo);
  104. #endif
  105. default:
  106. ERREXIT(cinfo, JERR_UNKNOWN_FORMAT);
  107. break;
  108. }
  109. return NULL; /* suppress compiler warnings */
  110. }
  111. /*
  112. * Argument-parsing code.
  113. * The switch parser is designed to be useful with DOS-style command line
  114. * syntax, ie, intermixed switches and file names, where only the switches
  115. * to the left of a given file name affect processing of that file.
  116. * The main program in this file doesn't actually use this capability...
  117. */
  118. static const char * progname; /* program name for error messages */
  119. static char * outfilename; /* for -outfile switch */
  120. LOCAL(void)
  121. usage (void)
  122. /* complain about bad command line */
  123. {
  124. fprintf(stderr, "usage: %s [switches] ", progname);
  125. #ifdef TWO_FILE_COMMANDLINE
  126. fprintf(stderr, "inputfile outputfile\n");
  127. #else
  128. fprintf(stderr, "[inputfile]\n");
  129. #endif
  130. fprintf(stderr, "Switches (names may be abbreviated):\n");
  131. fprintf(stderr, " -quality N[,...] Compression quality (0..100; 5-95 is useful range)\n");
  132. fprintf(stderr, " -grayscale Create monochrome JPEG file\n");
  133. fprintf(stderr, " -rgb Create RGB JPEG file\n");
  134. #ifdef ENTROPY_OPT_SUPPORTED
  135. fprintf(stderr, " -optimize Optimize Huffman table (smaller file, but slow compression)\n");
  136. #endif
  137. #ifdef C_PROGRESSIVE_SUPPORTED
  138. fprintf(stderr, " -progressive Create progressive JPEG file\n");
  139. #endif
  140. #ifdef DCT_SCALING_SUPPORTED
  141. fprintf(stderr, " -scale M/N Scale image by fraction M/N, eg, 1/2\n");
  142. #endif
  143. #ifdef TARGA_SUPPORTED
  144. fprintf(stderr, " -targa Input file is Targa format (usually not needed)\n");
  145. #endif
  146. fprintf(stderr, "Switches for advanced users:\n");
  147. #ifdef C_ARITH_CODING_SUPPORTED
  148. fprintf(stderr, " -arithmetic Use arithmetic coding\n");
  149. #endif
  150. #ifdef DCT_SCALING_SUPPORTED
  151. fprintf(stderr, " -block N DCT block size (1..16; default is 8)\n");
  152. #endif
  153. #if JPEG_LIB_VERSION_MAJOR >= 9
  154. fprintf(stderr, " -rgb1 Create RGB JPEG file with reversible color transform\n");
  155. fprintf(stderr, " -bgycc Create big gamut YCC JPEG file\n");
  156. #endif
  157. #ifdef DCT_ISLOW_SUPPORTED
  158. fprintf(stderr, " -dct int Use integer DCT method%s\n",
  159. (JDCT_DEFAULT == JDCT_ISLOW ? " (default)" : ""));
  160. #endif
  161. #ifdef DCT_IFAST_SUPPORTED
  162. fprintf(stderr, " -dct fast Use fast integer DCT (less accurate)%s\n",
  163. (JDCT_DEFAULT == JDCT_IFAST ? " (default)" : ""));
  164. #endif
  165. #ifdef DCT_FLOAT_SUPPORTED
  166. fprintf(stderr, " -dct float Use floating-point DCT method%s\n",
  167. (JDCT_DEFAULT == JDCT_FLOAT ? " (default)" : ""));
  168. #endif
  169. fprintf(stderr, " -nosmooth Don't use high-quality downsampling\n");
  170. fprintf(stderr, " -restart N Set restart interval in rows, or in blocks with B\n");
  171. #ifdef INPUT_SMOOTHING_SUPPORTED
  172. fprintf(stderr, " -smooth N Smooth dithered input (N=1..100 is strength)\n");
  173. #endif
  174. fprintf(stderr, " -maxmemory N Maximum memory to use (in kbytes)\n");
  175. fprintf(stderr, " -outfile name Specify name for output file\n");
  176. fprintf(stderr, " -verbose or -debug Emit debug output\n");
  177. fprintf(stderr, "Switches for wizards:\n");
  178. fprintf(stderr, " -baseline Force baseline quantization tables\n");
  179. fprintf(stderr, " -qtables file Use quantization tables given in file\n");
  180. fprintf(stderr, " -qslots N[,...] Set component quantization tables\n");
  181. fprintf(stderr, " -sample HxV[,...] Set component sampling factors\n");
  182. #ifdef C_MULTISCAN_FILES_SUPPORTED
  183. fprintf(stderr, " -scans file Create multi-scan JPEG per script file\n");
  184. #endif
  185. exit(EXIT_FAILURE);
  186. }
  187. LOCAL(int)
  188. parse_switches (j_compress_ptr cinfo, int argc, char **argv,
  189. int last_file_arg_seen, boolean for_real)
  190. /* Parse optional switches.
  191. * Returns argv[] index of first file-name argument (== argc if none).
  192. * Any file names with indexes <= last_file_arg_seen are ignored;
  193. * they have presumably been processed in a previous iteration.
  194. * (Pass 0 for last_file_arg_seen on the first or only iteration.)
  195. * for_real is FALSE on the first (dummy) pass; we may skip any expensive
  196. * processing.
  197. */
  198. {
  199. int argn;
  200. char * arg;
  201. boolean force_baseline;
  202. boolean simple_progressive;
  203. char * qualityarg = NULL; /* saves -quality parm if any */
  204. char * qtablefile = NULL; /* saves -qtables filename if any */
  205. char * qslotsarg = NULL; /* saves -qslots parm if any */
  206. char * samplearg = NULL; /* saves -sample parm if any */
  207. char * scansarg = NULL; /* saves -scans parm if any */
  208. /* Set up default JPEG parameters. */
  209. force_baseline = FALSE; /* by default, allow 16-bit quantizers */
  210. simple_progressive = FALSE;
  211. is_targa = FALSE;
  212. outfilename = NULL;
  213. cinfo->err->trace_level = 0;
  214. /* Scan command line options, adjust parameters */
  215. for (argn = 1; argn < argc; argn++) {
  216. arg = argv[argn];
  217. if (*arg != '-') {
  218. /* Not a switch, must be a file name argument */
  219. if (argn <= last_file_arg_seen) {
  220. outfilename = NULL; /* -outfile applies to just one input file */
  221. continue; /* ignore this name if previously processed */
  222. }
  223. break; /* else done parsing switches */
  224. }
  225. arg++; /* advance past switch marker character */
  226. if (keymatch(arg, "arithmetic", 1)) {
  227. /* Use arithmetic coding. */
  228. #ifdef C_ARITH_CODING_SUPPORTED
  229. cinfo->arith_code = TRUE;
  230. #else
  231. fprintf(stderr, "%s: sorry, arithmetic coding not supported\n",
  232. progname);
  233. exit(EXIT_FAILURE);
  234. #endif
  235. } else if (keymatch(arg, "baseline", 2)) {
  236. /* Force baseline-compatible output (8-bit quantizer values). */
  237. force_baseline = TRUE;
  238. } else if (keymatch(arg, "block", 2)) {
  239. /* Set DCT block size. */
  240. #if defined DCT_SCALING_SUPPORTED && JPEG_LIB_VERSION_MAJOR >= 8 && \
  241. (JPEG_LIB_VERSION_MAJOR > 8 || JPEG_LIB_VERSION_MINOR >= 3)
  242. int val;
  243. if (++argn >= argc) /* advance to next argument */
  244. usage();
  245. if (sscanf(argv[argn], "%d", &val) != 1)
  246. usage();
  247. if (val < 1 || val > 16)
  248. usage();
  249. cinfo->block_size = val;
  250. #else
  251. fprintf(stderr, "%s: sorry, block size setting not supported\n",
  252. progname);
  253. exit(EXIT_FAILURE);
  254. #endif
  255. } else if (keymatch(arg, "dct", 2)) {
  256. /* Select DCT algorithm. */
  257. if (++argn >= argc) /* advance to next argument */
  258. usage();
  259. if (keymatch(argv[argn], "int", 1)) {
  260. cinfo->dct_method = JDCT_ISLOW;
  261. } else if (keymatch(argv[argn], "fast", 2)) {
  262. cinfo->dct_method = JDCT_IFAST;
  263. } else if (keymatch(argv[argn], "float", 2)) {
  264. cinfo->dct_method = JDCT_FLOAT;
  265. } else
  266. usage();
  267. } else if (keymatch(arg, "debug", 1) || keymatch(arg, "verbose", 1)) {
  268. /* Enable debug printouts. */
  269. /* On first -d, print version identification */
  270. static boolean printed_version = FALSE;
  271. if (! printed_version) {
  272. fprintf(stderr, "Independent JPEG Group's CJPEG, version %s\n%s\n",
  273. JVERSION, JCOPYRIGHT);
  274. printed_version = TRUE;
  275. }
  276. cinfo->err->trace_level++;
  277. } else if (keymatch(arg, "grayscale", 2) || keymatch(arg, "greyscale",2)) {
  278. /* Force a monochrome JPEG file to be generated. */
  279. jpeg_set_colorspace(cinfo, JCS_GRAYSCALE);
  280. } else if (keymatch(arg, "rgb", 3) || keymatch(arg, "rgb1", 4)) {
  281. /* Force an RGB JPEG file to be generated. */
  282. #if JPEG_LIB_VERSION_MAJOR >= 9
  283. /* Note: Entropy table assignment in jpeg_set_colorspace depends
  284. * on color_transform.
  285. */
  286. cinfo->color_transform = arg[3] ? JCT_SUBTRACT_GREEN : JCT_NONE;
  287. #endif
  288. jpeg_set_colorspace(cinfo, JCS_RGB);
  289. } else if (keymatch(arg, "bgycc", 5)) {
  290. /* Force a big gamut YCC JPEG file to be generated. */
  291. #if JPEG_LIB_VERSION_MAJOR >= 9 && \
  292. (JPEG_LIB_VERSION_MAJOR > 9 || JPEG_LIB_VERSION_MINOR >= 1)
  293. jpeg_set_colorspace(cinfo, JCS_BG_YCC);
  294. #else
  295. fprintf(stderr, "%s: sorry, BG_YCC colorspace not supported\n",
  296. progname);
  297. exit(EXIT_FAILURE);
  298. #endif
  299. } else if (keymatch(arg, "maxmemory", 3)) {
  300. /* Maximum memory in Kb (or Mb with 'm'). */
  301. long lval;
  302. char ch = 'x';
  303. if (++argn >= argc) /* advance to next argument */
  304. usage();
  305. if (sscanf(argv[argn], "%ld%c", &lval, &ch) < 1)
  306. usage();
  307. if (ch == 'm' || ch == 'M')
  308. lval *= 1000L;
  309. cinfo->mem->max_memory_to_use = lval * 1000L;
  310. } else if (keymatch(arg, "nosmooth", 3)) {
  311. /* Suppress fancy downsampling. */
  312. cinfo->do_fancy_downsampling = FALSE;
  313. } else if (keymatch(arg, "optimize", 1) || keymatch(arg, "optimise", 1)) {
  314. /* Enable entropy parm optimization. */
  315. #ifdef ENTROPY_OPT_SUPPORTED
  316. cinfo->optimize_coding = TRUE;
  317. #else
  318. fprintf(stderr, "%s: sorry, entropy optimization was not compiled\n",
  319. progname);
  320. exit(EXIT_FAILURE);
  321. #endif
  322. } else if (keymatch(arg, "outfile", 4)) {
  323. /* Set output file name. */
  324. if (++argn >= argc) /* advance to next argument */
  325. usage();
  326. outfilename = argv[argn]; /* save it away for later use */
  327. } else if (keymatch(arg, "progressive", 1)) {
  328. /* Select simple progressive mode. */
  329. #ifdef C_PROGRESSIVE_SUPPORTED
  330. simple_progressive = TRUE;
  331. /* We must postpone execution until num_components is known. */
  332. #else
  333. fprintf(stderr, "%s: sorry, progressive output was not compiled\n",
  334. progname);
  335. exit(EXIT_FAILURE);
  336. #endif
  337. } else if (keymatch(arg, "quality", 1)) {
  338. /* Quality ratings (quantization table scaling factors). */
  339. if (++argn >= argc) /* advance to next argument */
  340. usage();
  341. qualityarg = argv[argn];
  342. } else if (keymatch(arg, "qslots", 2)) {
  343. /* Quantization table slot numbers. */
  344. if (++argn >= argc) /* advance to next argument */
  345. usage();
  346. qslotsarg = argv[argn];
  347. /* Must delay setting qslots until after we have processed any
  348. * colorspace-determining switches, since jpeg_set_colorspace sets
  349. * default quant table numbers.
  350. */
  351. } else if (keymatch(arg, "qtables", 2)) {
  352. /* Quantization tables fetched from file. */
  353. if (++argn >= argc) /* advance to next argument */
  354. usage();
  355. qtablefile = argv[argn];
  356. /* We postpone actually reading the file in case -quality comes later. */
  357. } else if (keymatch(arg, "restart", 1)) {
  358. /* Restart interval in MCU rows (or in MCUs with 'b'). */
  359. long lval;
  360. char ch = 'x';
  361. if (++argn >= argc) /* advance to next argument */
  362. usage();
  363. if (sscanf(argv[argn], "%ld%c", &lval, &ch) < 1)
  364. usage();
  365. if (lval < 0 || lval > 65535L)
  366. usage();
  367. if (ch == 'b' || ch == 'B') {
  368. cinfo->restart_interval = (unsigned int) lval;
  369. cinfo->restart_in_rows = 0; /* else prior '-restart n' overrides me */
  370. } else {
  371. cinfo->restart_in_rows = (int) lval;
  372. /* restart_interval will be computed during startup */
  373. }
  374. } else if (keymatch(arg, "sample", 2)) {
  375. /* Set sampling factors. */
  376. if (++argn >= argc) /* advance to next argument */
  377. usage();
  378. samplearg = argv[argn];
  379. /* Must delay setting sample factors until after we have processed any
  380. * colorspace-determining switches, since jpeg_set_colorspace sets
  381. * default sampling factors.
  382. */
  383. } else if (keymatch(arg, "scale", 4)) {
  384. /* Scale the image by a fraction M/N. */
  385. if (++argn >= argc) /* advance to next argument */
  386. usage();
  387. if (sscanf(argv[argn], "%u/%u",
  388. &cinfo->scale_num, &cinfo->scale_denom) != 2)
  389. usage();
  390. } else if (keymatch(arg, "scans", 4)) {
  391. /* Set scan script. */
  392. #ifdef C_MULTISCAN_FILES_SUPPORTED
  393. if (++argn >= argc) /* advance to next argument */
  394. usage();
  395. scansarg = argv[argn];
  396. /* We must postpone reading the file in case -progressive appears. */
  397. #else
  398. fprintf(stderr, "%s: sorry, multi-scan output was not compiled\n",
  399. progname);
  400. exit(EXIT_FAILURE);
  401. #endif
  402. } else if (keymatch(arg, "smooth", 2)) {
  403. /* Set input smoothing factor. */
  404. int val;
  405. if (++argn >= argc) /* advance to next argument */
  406. usage();
  407. if (sscanf(argv[argn], "%d", &val) != 1)
  408. usage();
  409. if (val < 0 || val > 100)
  410. usage();
  411. cinfo->smoothing_factor = val;
  412. } else if (keymatch(arg, "targa", 1)) {
  413. /* Input file is Targa format. */
  414. is_targa = TRUE;
  415. } else {
  416. usage(); /* bogus switch */
  417. }
  418. }
  419. /* Post-switch-scanning cleanup */
  420. if (for_real) {
  421. /* Set quantization tables for selected quality. */
  422. /* Some or all may be overridden if -qtables is present. */
  423. if (qualityarg != NULL) /* process -quality if it was present */
  424. if (! set_quality_ratings(cinfo, qualityarg, force_baseline))
  425. usage();
  426. if (qtablefile != NULL) /* process -qtables if it was present */
  427. if (! read_quant_tables(cinfo, qtablefile, force_baseline))
  428. usage();
  429. if (qslotsarg != NULL) /* process -qslots if it was present */
  430. if (! set_quant_slots(cinfo, qslotsarg))
  431. usage();
  432. if (samplearg != NULL) /* process -sample if it was present */
  433. if (! set_sample_factors(cinfo, samplearg))
  434. usage();
  435. #ifdef C_PROGRESSIVE_SUPPORTED
  436. if (simple_progressive) /* process -progressive; -scans can override */
  437. jpeg_simple_progression(cinfo);
  438. #endif
  439. #ifdef C_MULTISCAN_FILES_SUPPORTED
  440. if (scansarg != NULL) /* process -scans if it was present */
  441. if (! read_scan_script(cinfo, scansarg))
  442. usage();
  443. #endif
  444. }
  445. return argn; /* return index of next arg (file name) */
  446. }
  447. /*
  448. * The main program.
  449. */
  450. int
  451. main (int argc, char **argv)
  452. {
  453. struct jpeg_compress_struct cinfo;
  454. struct jpeg_error_mgr jerr;
  455. #ifdef PROGRESS_REPORT
  456. struct cdjpeg_progress_mgr progress;
  457. #endif
  458. int file_index;
  459. cjpeg_source_ptr src_mgr;
  460. FILE * input_file;
  461. FILE * output_file;
  462. JDIMENSION num_scanlines;
  463. /* On Mac, fetch a command line. */
  464. #ifdef USE_CCOMMAND
  465. argc = ccommand(&argv);
  466. #endif
  467. progname = argv[0];
  468. if (progname == NULL || progname[0] == 0)
  469. progname = "cjpeg"; /* in case C library doesn't provide it */
  470. /* Initialize the JPEG compression object with default error handling. */
  471. cinfo.err = jpeg_std_error(&jerr);
  472. jpeg_create_compress(&cinfo);
  473. /* Add some application-specific error messages (from cderror.h) */
  474. jerr.addon_message_table = cdjpeg_message_table;
  475. jerr.first_addon_message = JMSG_FIRSTADDONCODE;
  476. jerr.last_addon_message = JMSG_LASTADDONCODE;
  477. /* Now safe to enable signal catcher. */
  478. #ifdef NEED_SIGNAL_CATCHER
  479. enable_signal_catcher((j_common_ptr) &cinfo);
  480. #endif
  481. /* Initialize JPEG parameters.
  482. * Much of this may be overridden later.
  483. * In particular, we don't yet know the input file's color space,
  484. * but we need to provide some value for jpeg_set_defaults() to work.
  485. */
  486. cinfo.in_color_space = JCS_RGB; /* arbitrary guess */
  487. jpeg_set_defaults(&cinfo);
  488. /* Scan command line to find file names.
  489. * It is convenient to use just one switch-parsing routine, but the switch
  490. * values read here are ignored; we will rescan the switches after opening
  491. * the input file.
  492. */
  493. file_index = parse_switches(&cinfo, argc, argv, 0, FALSE);
  494. #ifdef TWO_FILE_COMMANDLINE
  495. /* Must have either -outfile switch or explicit output file name */
  496. if (outfilename == NULL) {
  497. if (file_index != argc-2) {
  498. fprintf(stderr, "%s: must name one input and one output file\n",
  499. progname);
  500. usage();
  501. }
  502. outfilename = argv[file_index+1];
  503. } else {
  504. if (file_index != argc-1) {
  505. fprintf(stderr, "%s: must name one input and one output file\n",
  506. progname);
  507. usage();
  508. }
  509. }
  510. #else
  511. /* Unix style: expect zero or one file name */
  512. if (file_index < argc-1) {
  513. fprintf(stderr, "%s: only one input file\n", progname);
  514. usage();
  515. }
  516. #endif /* TWO_FILE_COMMANDLINE */
  517. /* Open the input file. */
  518. if (file_index < argc) {
  519. if ((input_file = fopen(argv[file_index], READ_BINARY)) == NULL) {
  520. fprintf(stderr, "%s: can't open %s\n", progname, argv[file_index]);
  521. exit(EXIT_FAILURE);
  522. }
  523. } else {
  524. /* default input file is stdin */
  525. input_file = read_stdin();
  526. }
  527. /* Open the output file. */
  528. if (outfilename != NULL) {
  529. if ((output_file = fopen(outfilename, WRITE_BINARY)) == NULL) {
  530. fprintf(stderr, "%s: can't open %s\n", progname, outfilename);
  531. exit(EXIT_FAILURE);
  532. }
  533. } else {
  534. /* default output file is stdout */
  535. output_file = write_stdout();
  536. }
  537. #ifdef PROGRESS_REPORT
  538. start_progress_monitor((j_common_ptr) &cinfo, &progress);
  539. #endif
  540. /* Figure out the input file format, and set up to read it. */
  541. src_mgr = select_file_type(&cinfo, input_file);
  542. src_mgr->input_file = input_file;
  543. /* Read the input file header to obtain file size & colorspace. */
  544. (*src_mgr->start_input) (&cinfo, src_mgr);
  545. /* Now that we know input colorspace, fix colorspace-dependent defaults */
  546. jpeg_default_colorspace(&cinfo);
  547. /* Adjust default compression parameters by re-parsing the options */
  548. file_index = parse_switches(&cinfo, argc, argv, 0, TRUE);
  549. /* Specify data destination for compression */
  550. jpeg_stdio_dest(&cinfo, output_file);
  551. /* Start compressor */
  552. jpeg_start_compress(&cinfo, TRUE);
  553. /* Process data */
  554. while (cinfo.next_scanline < cinfo.image_height) {
  555. num_scanlines = (*src_mgr->get_pixel_rows) (&cinfo, src_mgr);
  556. (void) jpeg_write_scanlines(&cinfo, src_mgr->buffer, num_scanlines);
  557. }
  558. /* Finish compression and release memory */
  559. (*src_mgr->finish_input) (&cinfo, src_mgr);
  560. jpeg_finish_compress(&cinfo);
  561. jpeg_destroy_compress(&cinfo);
  562. /* Close files, if we opened them */
  563. if (input_file != stdin)
  564. fclose(input_file);
  565. if (output_file != stdout)
  566. fclose(output_file);
  567. #ifdef PROGRESS_REPORT
  568. end_progress_monitor((j_common_ptr) &cinfo);
  569. #endif
  570. /* All done. */
  571. exit(jerr.num_warnings ? EXIT_WARNING : EXIT_SUCCESS);
  572. return 0; /* suppress no-return-value warnings */
  573. }