djpeg.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616
  1. /*
  2. * djpeg.c
  3. *
  4. * Copyright (C) 1991-1997, 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 decompressor.
  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. * djpeg [options] inputfile outputfile
  14. * djpeg [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. * djpeg [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. #include <ctype.h> /* to declare isprint() */
  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 list defines the known output image formats
  45. * (not all of which need be supported by a given version).
  46. * You can change the default output format by defining DEFAULT_FMT;
  47. * indeed, you had better do so if you undefine PPM_SUPPORTED.
  48. */
  49. typedef enum {
  50. FMT_BMP, /* BMP format (Windows flavor) */
  51. FMT_GIF, /* GIF format */
  52. FMT_OS2, /* BMP format (OS/2 flavor) */
  53. FMT_PPM, /* PPM/PGM (PBMPLUS formats) */
  54. FMT_RLE, /* RLE format */
  55. FMT_TARGA, /* Targa format */
  56. FMT_TIFF /* TIFF format */
  57. } IMAGE_FORMATS;
  58. #ifndef DEFAULT_FMT /* so can override from CFLAGS in Makefile */
  59. #define DEFAULT_FMT FMT_PPM
  60. #endif
  61. static IMAGE_FORMATS requested_fmt;
  62. /*
  63. * Argument-parsing code.
  64. * The switch parser is designed to be useful with DOS-style command line
  65. * syntax, ie, intermixed switches and file names, where only the switches
  66. * to the left of a given file name affect processing of that file.
  67. * The main program in this file doesn't actually use this capability...
  68. */
  69. static const char * progname; /* program name for error messages */
  70. static char * outfilename; /* for -outfile switch */
  71. LOCAL(void)
  72. usage (void)
  73. /* complain about bad command line */
  74. {
  75. fprintf(stderr, "usage: %s [switches] ", progname);
  76. #ifdef TWO_FILE_COMMANDLINE
  77. fprintf(stderr, "inputfile outputfile\n");
  78. #else
  79. fprintf(stderr, "[inputfile]\n");
  80. #endif
  81. fprintf(stderr, "Switches (names may be abbreviated):\n");
  82. fprintf(stderr, " -colors N Reduce image to no more than N colors\n");
  83. fprintf(stderr, " -fast Fast, low-quality processing\n");
  84. fprintf(stderr, " -grayscale Force grayscale output\n");
  85. #ifdef IDCT_SCALING_SUPPORTED
  86. fprintf(stderr, " -scale M/N Scale output image by fraction M/N, eg, 1/8\n");
  87. #endif
  88. #ifdef BMP_SUPPORTED
  89. fprintf(stderr, " -bmp Select BMP output format (Windows style)%s\n",
  90. (DEFAULT_FMT == FMT_BMP ? " (default)" : ""));
  91. #endif
  92. #ifdef GIF_SUPPORTED
  93. fprintf(stderr, " -gif Select GIF output format%s\n",
  94. (DEFAULT_FMT == FMT_GIF ? " (default)" : ""));
  95. #endif
  96. #ifdef BMP_SUPPORTED
  97. fprintf(stderr, " -os2 Select BMP output format (OS/2 style)%s\n",
  98. (DEFAULT_FMT == FMT_OS2 ? " (default)" : ""));
  99. #endif
  100. #ifdef PPM_SUPPORTED
  101. fprintf(stderr, " -pnm Select PBMPLUS (PPM/PGM) output format%s\n",
  102. (DEFAULT_FMT == FMT_PPM ? " (default)" : ""));
  103. #endif
  104. #ifdef RLE_SUPPORTED
  105. fprintf(stderr, " -rle Select Utah RLE output format%s\n",
  106. (DEFAULT_FMT == FMT_RLE ? " (default)" : ""));
  107. #endif
  108. #ifdef TARGA_SUPPORTED
  109. fprintf(stderr, " -targa Select Targa output format%s\n",
  110. (DEFAULT_FMT == FMT_TARGA ? " (default)" : ""));
  111. #endif
  112. fprintf(stderr, "Switches for advanced users:\n");
  113. #ifdef DCT_ISLOW_SUPPORTED
  114. fprintf(stderr, " -dct int Use integer DCT method%s\n",
  115. (JDCT_DEFAULT == JDCT_ISLOW ? " (default)" : ""));
  116. #endif
  117. #ifdef DCT_IFAST_SUPPORTED
  118. fprintf(stderr, " -dct fast Use fast integer DCT (less accurate)%s\n",
  119. (JDCT_DEFAULT == JDCT_IFAST ? " (default)" : ""));
  120. #endif
  121. #ifdef DCT_FLOAT_SUPPORTED
  122. fprintf(stderr, " -dct float Use floating-point DCT method%s\n",
  123. (JDCT_DEFAULT == JDCT_FLOAT ? " (default)" : ""));
  124. #endif
  125. fprintf(stderr, " -dither fs Use F-S dithering (default)\n");
  126. fprintf(stderr, " -dither none Don't use dithering in quantization\n");
  127. fprintf(stderr, " -dither ordered Use ordered dither (medium speed, quality)\n");
  128. #ifdef QUANT_2PASS_SUPPORTED
  129. fprintf(stderr, " -map FILE Map to colors used in named image file\n");
  130. #endif
  131. fprintf(stderr, " -nosmooth Don't use high-quality upsampling\n");
  132. #ifdef QUANT_1PASS_SUPPORTED
  133. fprintf(stderr, " -onepass Use 1-pass quantization (fast, low quality)\n");
  134. #endif
  135. fprintf(stderr, " -maxmemory N Maximum memory to use (in kbytes)\n");
  136. fprintf(stderr, " -outfile name Specify name for output file\n");
  137. fprintf(stderr, " -verbose or -debug Emit debug output\n");
  138. exit(EXIT_FAILURE);
  139. }
  140. LOCAL(int)
  141. parse_switches (j_decompress_ptr cinfo, int argc, char **argv,
  142. int last_file_arg_seen, boolean for_real)
  143. /* Parse optional switches.
  144. * Returns argv[] index of first file-name argument (== argc if none).
  145. * Any file names with indexes <= last_file_arg_seen are ignored;
  146. * they have presumably been processed in a previous iteration.
  147. * (Pass 0 for last_file_arg_seen on the first or only iteration.)
  148. * for_real is FALSE on the first (dummy) pass; we may skip any expensive
  149. * processing.
  150. */
  151. {
  152. int argn;
  153. char * arg;
  154. /* Set up default JPEG parameters. */
  155. requested_fmt = DEFAULT_FMT; /* set default output file format */
  156. outfilename = NULL;
  157. cinfo->err->trace_level = 0;
  158. /* Scan command line options, adjust parameters */
  159. for (argn = 1; argn < argc; argn++) {
  160. arg = argv[argn];
  161. if (*arg != '-') {
  162. /* Not a switch, must be a file name argument */
  163. if (argn <= last_file_arg_seen) {
  164. outfilename = NULL; /* -outfile applies to just one input file */
  165. continue; /* ignore this name if previously processed */
  166. }
  167. break; /* else done parsing switches */
  168. }
  169. arg++; /* advance past switch marker character */
  170. if (keymatch(arg, "bmp", 1)) {
  171. /* BMP output format. */
  172. requested_fmt = FMT_BMP;
  173. } else if (keymatch(arg, "colors", 1) || keymatch(arg, "colours", 1) ||
  174. keymatch(arg, "quantize", 1) || keymatch(arg, "quantise", 1)) {
  175. /* Do color quantization. */
  176. int val;
  177. if (++argn >= argc) /* advance to next argument */
  178. usage();
  179. if (sscanf(argv[argn], "%d", &val) != 1)
  180. usage();
  181. cinfo->desired_number_of_colors = val;
  182. cinfo->quantize_colors = TRUE;
  183. } else if (keymatch(arg, "dct", 2)) {
  184. /* Select IDCT algorithm. */
  185. if (++argn >= argc) /* advance to next argument */
  186. usage();
  187. if (keymatch(argv[argn], "int", 1)) {
  188. cinfo->dct_method = JDCT_ISLOW;
  189. } else if (keymatch(argv[argn], "fast", 2)) {
  190. cinfo->dct_method = JDCT_IFAST;
  191. } else if (keymatch(argv[argn], "float", 2)) {
  192. cinfo->dct_method = JDCT_FLOAT;
  193. } else
  194. usage();
  195. } else if (keymatch(arg, "dither", 2)) {
  196. /* Select dithering algorithm. */
  197. if (++argn >= argc) /* advance to next argument */
  198. usage();
  199. if (keymatch(argv[argn], "fs", 2)) {
  200. cinfo->dither_mode = JDITHER_FS;
  201. } else if (keymatch(argv[argn], "none", 2)) {
  202. cinfo->dither_mode = JDITHER_NONE;
  203. } else if (keymatch(argv[argn], "ordered", 2)) {
  204. cinfo->dither_mode = JDITHER_ORDERED;
  205. } else
  206. usage();
  207. } else if (keymatch(arg, "debug", 1) || keymatch(arg, "verbose", 1)) {
  208. /* Enable debug printouts. */
  209. /* On first -d, print version identification */
  210. static boolean printed_version = FALSE;
  211. if (! printed_version) {
  212. fprintf(stderr, "Independent JPEG Group's DJPEG, version %s\n%s\n",
  213. JVERSION, JCOPYRIGHT);
  214. printed_version = TRUE;
  215. }
  216. cinfo->err->trace_level++;
  217. } else if (keymatch(arg, "fast", 1)) {
  218. /* Select recommended processing options for quick-and-dirty output. */
  219. cinfo->two_pass_quantize = FALSE;
  220. cinfo->dither_mode = JDITHER_ORDERED;
  221. if (! cinfo->quantize_colors) /* don't override an earlier -colors */
  222. cinfo->desired_number_of_colors = 216;
  223. cinfo->dct_method = JDCT_FASTEST;
  224. cinfo->do_fancy_upsampling = FALSE;
  225. } else if (keymatch(arg, "gif", 1)) {
  226. /* GIF output format. */
  227. requested_fmt = FMT_GIF;
  228. } else if (keymatch(arg, "grayscale", 2) || keymatch(arg, "greyscale",2)) {
  229. /* Force monochrome output. */
  230. cinfo->out_color_space = JCS_GRAYSCALE;
  231. } else if (keymatch(arg, "map", 3)) {
  232. /* Quantize to a color map taken from an input file. */
  233. if (++argn >= argc) /* advance to next argument */
  234. usage();
  235. if (for_real) { /* too expensive to do twice! */
  236. #ifdef QUANT_2PASS_SUPPORTED /* otherwise can't quantize to supplied map */
  237. FILE * mapfile;
  238. if ((mapfile = fopen(argv[argn], READ_BINARY)) == NULL) {
  239. fprintf(stderr, "%s: can't open %s\n", progname, argv[argn]);
  240. exit(EXIT_FAILURE);
  241. }
  242. read_color_map(cinfo, mapfile);
  243. fclose(mapfile);
  244. cinfo->quantize_colors = TRUE;
  245. #else
  246. ERREXIT(cinfo, JERR_NOT_COMPILED);
  247. #endif
  248. }
  249. } else if (keymatch(arg, "maxmemory", 3)) {
  250. /* Maximum memory in Kb (or Mb with 'm'). */
  251. long lval;
  252. char ch = 'x';
  253. if (++argn >= argc) /* advance to next argument */
  254. usage();
  255. if (sscanf(argv[argn], "%ld%c", &lval, &ch) < 1)
  256. usage();
  257. if (ch == 'm' || ch == 'M')
  258. lval *= 1000L;
  259. cinfo->mem->max_memory_to_use = lval * 1000L;
  260. } else if (keymatch(arg, "nosmooth", 3)) {
  261. /* Suppress fancy upsampling */
  262. cinfo->do_fancy_upsampling = FALSE;
  263. } else if (keymatch(arg, "onepass", 3)) {
  264. /* Use fast one-pass quantization. */
  265. cinfo->two_pass_quantize = FALSE;
  266. } else if (keymatch(arg, "os2", 3)) {
  267. /* BMP output format (OS/2 flavor). */
  268. requested_fmt = FMT_OS2;
  269. } else if (keymatch(arg, "outfile", 4)) {
  270. /* Set output file name. */
  271. if (++argn >= argc) /* advance to next argument */
  272. usage();
  273. outfilename = argv[argn]; /* save it away for later use */
  274. } else if (keymatch(arg, "pnm", 1) || keymatch(arg, "ppm", 1)) {
  275. /* PPM/PGM output format. */
  276. requested_fmt = FMT_PPM;
  277. } else if (keymatch(arg, "rle", 1)) {
  278. /* RLE output format. */
  279. requested_fmt = FMT_RLE;
  280. } else if (keymatch(arg, "scale", 1)) {
  281. /* Scale the output image by a fraction M/N. */
  282. if (++argn >= argc) /* advance to next argument */
  283. usage();
  284. if (sscanf(argv[argn], "%d/%d",
  285. &cinfo->scale_num, &cinfo->scale_denom) != 2)
  286. usage();
  287. } else if (keymatch(arg, "targa", 1)) {
  288. /* Targa output format. */
  289. requested_fmt = FMT_TARGA;
  290. } else {
  291. usage(); /* bogus switch */
  292. }
  293. }
  294. return argn; /* return index of next arg (file name) */
  295. }
  296. /*
  297. * Marker processor for COM and interesting APPn markers.
  298. * This replaces the library's built-in processor, which just skips the marker.
  299. * We want to print out the marker as text, to the extent possible.
  300. * Note this code relies on a non-suspending data source.
  301. */
  302. LOCAL(unsigned int)
  303. jpeg_getc (j_decompress_ptr cinfo)
  304. /* Read next byte */
  305. {
  306. struct jpeg_source_mgr * datasrc = cinfo->src;
  307. if (datasrc->bytes_in_buffer == 0) {
  308. if (! (*datasrc->fill_input_buffer) (cinfo))
  309. ERREXIT(cinfo, JERR_CANT_SUSPEND);
  310. }
  311. datasrc->bytes_in_buffer--;
  312. return GETJOCTET(*datasrc->next_input_byte++);
  313. }
  314. METHODDEF(boolean)
  315. print_text_marker (j_decompress_ptr cinfo)
  316. {
  317. boolean traceit = (cinfo->err->trace_level >= 1);
  318. INT32 length;
  319. unsigned int ch;
  320. unsigned int lastch = 0;
  321. length = jpeg_getc(cinfo) << 8;
  322. length += jpeg_getc(cinfo);
  323. length -= 2; /* discount the length word itself */
  324. if (traceit) {
  325. if (cinfo->unread_marker == JPEG_COM)
  326. fprintf(stderr, "Comment, length %ld:\n", (long) length);
  327. else /* assume it is an APPn otherwise */
  328. fprintf(stderr, "APP%d, length %ld:\n",
  329. cinfo->unread_marker - JPEG_APP0, (long) length);
  330. }
  331. while (--length >= 0) {
  332. ch = jpeg_getc(cinfo);
  333. if (traceit) {
  334. /* Emit the character in a readable form.
  335. * Nonprintables are converted to \nnn form,
  336. * while \ is converted to \\.
  337. * Newlines in CR, CR/LF, or LF form will be printed as one newline.
  338. */
  339. if (ch == '\r') {
  340. fprintf(stderr, "\n");
  341. } else if (ch == '\n') {
  342. if (lastch != '\r')
  343. fprintf(stderr, "\n");
  344. } else if (ch == '\\') {
  345. fprintf(stderr, "\\\\");
  346. } else if (isprint(ch)) {
  347. putc(ch, stderr);
  348. } else {
  349. fprintf(stderr, "\\%03o", ch);
  350. }
  351. lastch = ch;
  352. }
  353. }
  354. if (traceit)
  355. fprintf(stderr, "\n");
  356. return TRUE;
  357. }
  358. /*
  359. * The main program.
  360. */
  361. int
  362. main (int argc, char **argv)
  363. {
  364. struct jpeg_decompress_struct cinfo;
  365. struct jpeg_error_mgr jerr;
  366. #ifdef PROGRESS_REPORT
  367. struct cdjpeg_progress_mgr progress;
  368. #endif
  369. int file_index;
  370. djpeg_dest_ptr dest_mgr = NULL;
  371. FILE * input_file;
  372. FILE * output_file;
  373. JDIMENSION num_scanlines;
  374. /* On Mac, fetch a command line. */
  375. #ifdef USE_CCOMMAND
  376. argc = ccommand(&argv);
  377. #endif
  378. progname = argv[0];
  379. if (progname == NULL || progname[0] == 0)
  380. progname = "djpeg"; /* in case C library doesn't provide it */
  381. /* Initialize the JPEG decompression object with default error handling. */
  382. cinfo.err = jpeg_std_error(&jerr);
  383. jpeg_create_decompress(&cinfo);
  384. /* Add some application-specific error messages (from cderror.h) */
  385. jerr.addon_message_table = cdjpeg_message_table;
  386. jerr.first_addon_message = JMSG_FIRSTADDONCODE;
  387. jerr.last_addon_message = JMSG_LASTADDONCODE;
  388. /* Insert custom marker processor for COM and APP12.
  389. * APP12 is used by some digital camera makers for textual info,
  390. * so we provide the ability to display it as text.
  391. * If you like, additional APPn marker types can be selected for display,
  392. * but don't try to override APP0 or APP14 this way (see libjpeg.doc).
  393. */
  394. jpeg_set_marker_processor(&cinfo, JPEG_COM, print_text_marker);
  395. jpeg_set_marker_processor(&cinfo, JPEG_APP0+12, print_text_marker);
  396. /* Now safe to enable signal catcher. */
  397. #ifdef NEED_SIGNAL_CATCHER
  398. enable_signal_catcher((j_common_ptr) &cinfo);
  399. #endif
  400. /* Scan command line to find file names. */
  401. /* It is convenient to use just one switch-parsing routine, but the switch
  402. * values read here are ignored; we will rescan the switches after opening
  403. * the input file.
  404. * (Exception: tracing level set here controls verbosity for COM markers
  405. * found during jpeg_read_header...)
  406. */
  407. file_index = parse_switches(&cinfo, argc, argv, 0, FALSE);
  408. #ifdef TWO_FILE_COMMANDLINE
  409. /* Must have either -outfile switch or explicit output file name */
  410. if (outfilename == NULL) {
  411. if (file_index != argc-2) {
  412. fprintf(stderr, "%s: must name one input and one output file\n",
  413. progname);
  414. usage();
  415. }
  416. outfilename = argv[file_index+1];
  417. } else {
  418. if (file_index != argc-1) {
  419. fprintf(stderr, "%s: must name one input and one output file\n",
  420. progname);
  421. usage();
  422. }
  423. }
  424. #else
  425. /* Unix style: expect zero or one file name */
  426. if (file_index < argc-1) {
  427. fprintf(stderr, "%s: only one input file\n", progname);
  428. usage();
  429. }
  430. #endif /* TWO_FILE_COMMANDLINE */
  431. /* Open the input file. */
  432. if (file_index < argc) {
  433. if ((input_file = fopen(argv[file_index], READ_BINARY)) == NULL) {
  434. fprintf(stderr, "%s: can't open %s\n", progname, argv[file_index]);
  435. exit(EXIT_FAILURE);
  436. }
  437. } else {
  438. /* default input file is stdin */
  439. input_file = read_stdin();
  440. }
  441. /* Open the output file. */
  442. if (outfilename != NULL) {
  443. if ((output_file = fopen(outfilename, WRITE_BINARY)) == NULL) {
  444. fprintf(stderr, "%s: can't open %s\n", progname, outfilename);
  445. exit(EXIT_FAILURE);
  446. }
  447. } else {
  448. /* default output file is stdout */
  449. output_file = write_stdout();
  450. }
  451. #ifdef PROGRESS_REPORT
  452. start_progress_monitor((j_common_ptr) &cinfo, &progress);
  453. #endif
  454. /* Specify data source for decompression */
  455. jpeg_stdio_src(&cinfo, input_file);
  456. /* Read file header, set default decompression parameters */
  457. (void) jpeg_read_header(&cinfo, TRUE);
  458. /* Adjust default decompression parameters by re-parsing the options */
  459. file_index = parse_switches(&cinfo, argc, argv, 0, TRUE);
  460. /* Initialize the output module now to let it override any crucial
  461. * option settings (for instance, GIF wants to force color quantization).
  462. */
  463. switch (requested_fmt) {
  464. #ifdef BMP_SUPPORTED
  465. case FMT_BMP:
  466. dest_mgr = jinit_write_bmp(&cinfo, FALSE);
  467. break;
  468. case FMT_OS2:
  469. dest_mgr = jinit_write_bmp(&cinfo, TRUE);
  470. break;
  471. #endif
  472. #ifdef GIF_SUPPORTED
  473. case FMT_GIF:
  474. dest_mgr = jinit_write_gif(&cinfo);
  475. break;
  476. #endif
  477. #ifdef PPM_SUPPORTED
  478. case FMT_PPM:
  479. dest_mgr = jinit_write_ppm(&cinfo);
  480. break;
  481. #endif
  482. #ifdef RLE_SUPPORTED
  483. case FMT_RLE:
  484. dest_mgr = jinit_write_rle(&cinfo);
  485. break;
  486. #endif
  487. #ifdef TARGA_SUPPORTED
  488. case FMT_TARGA:
  489. dest_mgr = jinit_write_targa(&cinfo);
  490. break;
  491. #endif
  492. default:
  493. ERREXIT(&cinfo, JERR_UNSUPPORTED_FORMAT);
  494. break;
  495. }
  496. dest_mgr->output_file = output_file;
  497. /* Start decompressor */
  498. (void) jpeg_start_decompress(&cinfo);
  499. /* Write output file header */
  500. (*dest_mgr->start_output) (&cinfo, dest_mgr);
  501. /* Process data */
  502. while (cinfo.output_scanline < cinfo.output_height) {
  503. num_scanlines = jpeg_read_scanlines(&cinfo, dest_mgr->buffer,
  504. dest_mgr->buffer_height);
  505. (*dest_mgr->put_pixel_rows) (&cinfo, dest_mgr, num_scanlines);
  506. }
  507. #ifdef PROGRESS_REPORT
  508. /* Hack: count final pass as done in case finish_output does an extra pass.
  509. * The library won't have updated completed_passes.
  510. */
  511. progress.pub.completed_passes = progress.pub.total_passes;
  512. #endif
  513. /* Finish decompression and release memory.
  514. * I must do it in this order because output module has allocated memory
  515. * of lifespan JPOOL_IMAGE; it needs to finish before releasing memory.
  516. */
  517. (*dest_mgr->finish_output) (&cinfo, dest_mgr);
  518. (void) jpeg_finish_decompress(&cinfo);
  519. jpeg_destroy_decompress(&cinfo);
  520. /* Close files, if we opened them */
  521. if (input_file != stdin)
  522. fclose(input_file);
  523. if (output_file != stdout)
  524. fclose(output_file);
  525. #ifdef PROGRESS_REPORT
  526. end_progress_monitor((j_common_ptr) &cinfo);
  527. #endif
  528. /* All done. */
  529. exit(jerr.num_warnings ? EXIT_WARNING : EXIT_SUCCESS);
  530. return 0; /* suppress no-return-value warnings */
  531. }