djpeg.c 27 KB

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