djpeg.c 21 KB

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