cjpeg.c 20 KB

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