cjpeg.c 19 KB

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