djpeg.c 19 KB

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