djpeg.c 20 KB

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