djpeg.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838
  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, 2020, 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. #if JPEG_LIB_VERSION >= 80 || defined(MEM_SRCDST_SUPPORTED)
  449. unsigned long insize = 0;
  450. #endif
  451. JDIMENSION num_scanlines;
  452. /* On Mac, fetch a command line. */
  453. #ifdef USE_CCOMMAND
  454. argc = ccommand(&argv);
  455. #endif
  456. progname = argv[0];
  457. if (progname == NULL || progname[0] == 0)
  458. progname = "djpeg"; /* in case C library doesn't provide it */
  459. /* Initialize the JPEG decompression object with default error handling. */
  460. cinfo.err = jpeg_std_error(&jerr);
  461. jpeg_create_decompress(&cinfo);
  462. /* Add some application-specific error messages (from cderror.h) */
  463. jerr.addon_message_table = cdjpeg_message_table;
  464. jerr.first_addon_message = JMSG_FIRSTADDONCODE;
  465. jerr.last_addon_message = JMSG_LASTADDONCODE;
  466. /* Insert custom marker processor for COM and APP12.
  467. * APP12 is used by some digital camera makers for textual info,
  468. * so we provide the ability to display it as text.
  469. * If you like, additional APPn marker types can be selected for display,
  470. * but don't try to override APP0 or APP14 this way (see libjpeg.txt).
  471. */
  472. jpeg_set_marker_processor(&cinfo, JPEG_COM, print_text_marker);
  473. jpeg_set_marker_processor(&cinfo, JPEG_APP0 + 12, print_text_marker);
  474. /* Scan command line to find file names. */
  475. /* It is convenient to use just one switch-parsing routine, but the switch
  476. * values read here are ignored; we will rescan the switches after opening
  477. * the input file.
  478. * (Exception: tracing level set here controls verbosity for COM markers
  479. * found during jpeg_read_header...)
  480. */
  481. file_index = parse_switches(&cinfo, argc, argv, 0, FALSE);
  482. #ifdef TWO_FILE_COMMANDLINE
  483. /* Must have either -outfile switch or explicit output file name */
  484. if (outfilename == NULL) {
  485. if (file_index != argc - 2) {
  486. fprintf(stderr, "%s: must name one input and one output file\n",
  487. progname);
  488. usage();
  489. }
  490. outfilename = argv[file_index + 1];
  491. } else {
  492. if (file_index != argc - 1) {
  493. fprintf(stderr, "%s: must name one input and one output file\n",
  494. progname);
  495. usage();
  496. }
  497. }
  498. #else
  499. /* Unix style: expect zero or one file name */
  500. if (file_index < argc - 1) {
  501. fprintf(stderr, "%s: only one input file\n", progname);
  502. usage();
  503. }
  504. #endif /* TWO_FILE_COMMANDLINE */
  505. /* Open the input file. */
  506. if (file_index < argc) {
  507. if ((input_file = fopen(argv[file_index], READ_BINARY)) == NULL) {
  508. fprintf(stderr, "%s: can't open %s\n", progname, argv[file_index]);
  509. exit(EXIT_FAILURE);
  510. }
  511. } else {
  512. /* default input file is stdin */
  513. input_file = read_stdin();
  514. }
  515. /* Open the output file. */
  516. if (outfilename != NULL) {
  517. if ((output_file = fopen(outfilename, WRITE_BINARY)) == NULL) {
  518. fprintf(stderr, "%s: can't open %s\n", progname, outfilename);
  519. exit(EXIT_FAILURE);
  520. }
  521. } else {
  522. /* default output file is stdout */
  523. output_file = write_stdout();
  524. }
  525. #ifdef PROGRESS_REPORT
  526. start_progress_monitor((j_common_ptr)&cinfo, &progress);
  527. #endif
  528. /* Specify data source for decompression */
  529. #if JPEG_LIB_VERSION >= 80 || defined(MEM_SRCDST_SUPPORTED)
  530. if (memsrc) {
  531. size_t nbytes;
  532. do {
  533. inbuffer = (unsigned char *)realloc(inbuffer, insize + INPUT_BUF_SIZE);
  534. if (inbuffer == NULL) {
  535. fprintf(stderr, "%s: memory allocation failure\n", progname);
  536. exit(EXIT_FAILURE);
  537. }
  538. nbytes = JFREAD(input_file, &inbuffer[insize], INPUT_BUF_SIZE);
  539. if (nbytes < INPUT_BUF_SIZE && ferror(input_file)) {
  540. if (file_index < argc)
  541. fprintf(stderr, "%s: can't read from %s\n", progname,
  542. argv[file_index]);
  543. else
  544. fprintf(stderr, "%s: can't read from stdin\n", progname);
  545. }
  546. insize += (unsigned long)nbytes;
  547. } while (nbytes == INPUT_BUF_SIZE);
  548. fprintf(stderr, "Compressed size: %lu bytes\n", insize);
  549. jpeg_mem_src(&cinfo, inbuffer, insize);
  550. } else
  551. #endif
  552. jpeg_stdio_src(&cinfo, input_file);
  553. /* Read file header, set default decompression parameters */
  554. (void)jpeg_read_header(&cinfo, TRUE);
  555. /* Adjust default decompression parameters by re-parsing the options */
  556. file_index = parse_switches(&cinfo, argc, argv, 0, TRUE);
  557. /* Initialize the output module now to let it override any crucial
  558. * option settings (for instance, GIF wants to force color quantization).
  559. */
  560. switch (requested_fmt) {
  561. #ifdef BMP_SUPPORTED
  562. case FMT_BMP:
  563. dest_mgr = jinit_write_bmp(&cinfo, FALSE, TRUE);
  564. break;
  565. case FMT_OS2:
  566. dest_mgr = jinit_write_bmp(&cinfo, TRUE, TRUE);
  567. break;
  568. #endif
  569. #ifdef GIF_SUPPORTED
  570. case FMT_GIF:
  571. dest_mgr = jinit_write_gif(&cinfo);
  572. break;
  573. #endif
  574. #ifdef PPM_SUPPORTED
  575. case FMT_PPM:
  576. dest_mgr = jinit_write_ppm(&cinfo);
  577. break;
  578. #endif
  579. #ifdef RLE_SUPPORTED
  580. case FMT_RLE:
  581. dest_mgr = jinit_write_rle(&cinfo);
  582. break;
  583. #endif
  584. #ifdef TARGA_SUPPORTED
  585. case FMT_TARGA:
  586. dest_mgr = jinit_write_targa(&cinfo);
  587. break;
  588. #endif
  589. default:
  590. ERREXIT(&cinfo, JERR_UNSUPPORTED_FORMAT);
  591. break;
  592. }
  593. dest_mgr->output_file = output_file;
  594. /* Start decompressor */
  595. (void)jpeg_start_decompress(&cinfo);
  596. /* Skip rows */
  597. if (skip) {
  598. JDIMENSION tmp;
  599. /* Check for valid skip_end. We cannot check this value until after
  600. * jpeg_start_decompress() is called. Note that we have already verified
  601. * that skip_start <= skip_end.
  602. */
  603. if (skip_end > cinfo.output_height - 1) {
  604. fprintf(stderr, "%s: skip region exceeds image height %d\n", progname,
  605. cinfo.output_height);
  606. exit(EXIT_FAILURE);
  607. }
  608. /* Write output file header. This is a hack to ensure that the destination
  609. * manager creates an output image of the proper size.
  610. */
  611. tmp = cinfo.output_height;
  612. cinfo.output_height -= (skip_end - skip_start + 1);
  613. (*dest_mgr->start_output) (&cinfo, dest_mgr);
  614. cinfo.output_height = tmp;
  615. /* Process data */
  616. while (cinfo.output_scanline < skip_start) {
  617. num_scanlines = jpeg_read_scanlines(&cinfo, dest_mgr->buffer,
  618. dest_mgr->buffer_height);
  619. (*dest_mgr->put_pixel_rows) (&cinfo, dest_mgr, num_scanlines);
  620. }
  621. if ((tmp = jpeg_skip_scanlines(&cinfo, skip_end - skip_start + 1)) !=
  622. skip_end - skip_start + 1) {
  623. fprintf(stderr, "%s: jpeg_skip_scanlines() returned %d rather than %d\n",
  624. progname, tmp, skip_end - skip_start + 1);
  625. exit(EXIT_FAILURE);
  626. }
  627. while (cinfo.output_scanline < cinfo.output_height) {
  628. num_scanlines = jpeg_read_scanlines(&cinfo, dest_mgr->buffer,
  629. dest_mgr->buffer_height);
  630. (*dest_mgr->put_pixel_rows) (&cinfo, dest_mgr, num_scanlines);
  631. }
  632. /* Decompress a subregion */
  633. } else if (crop) {
  634. JDIMENSION tmp;
  635. /* Check for valid crop dimensions. We cannot check these values until
  636. * after jpeg_start_decompress() is called.
  637. */
  638. if (crop_x + crop_width > cinfo.output_width ||
  639. crop_y + crop_height > cinfo.output_height) {
  640. fprintf(stderr, "%s: crop dimensions exceed image dimensions %d x %d\n",
  641. progname, cinfo.output_width, cinfo.output_height);
  642. exit(EXIT_FAILURE);
  643. }
  644. jpeg_crop_scanline(&cinfo, &crop_x, &crop_width);
  645. if (dest_mgr->calc_buffer_dimensions)
  646. (*dest_mgr->calc_buffer_dimensions) (&cinfo, dest_mgr);
  647. else
  648. ERREXIT(&cinfo, JERR_UNSUPPORTED_FORMAT);
  649. /* Write output file header. This is a hack to ensure that the destination
  650. * manager creates an output image of the proper size.
  651. */
  652. tmp = cinfo.output_height;
  653. cinfo.output_height = crop_height;
  654. (*dest_mgr->start_output) (&cinfo, dest_mgr);
  655. cinfo.output_height = tmp;
  656. /* Process data */
  657. if ((tmp = jpeg_skip_scanlines(&cinfo, crop_y)) != crop_y) {
  658. fprintf(stderr, "%s: jpeg_skip_scanlines() returned %d rather than %d\n",
  659. progname, tmp, crop_y);
  660. exit(EXIT_FAILURE);
  661. }
  662. while (cinfo.output_scanline < crop_y + crop_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. if ((tmp =
  668. jpeg_skip_scanlines(&cinfo,
  669. cinfo.output_height - crop_y - crop_height)) !=
  670. cinfo.output_height - crop_y - crop_height) {
  671. fprintf(stderr, "%s: jpeg_skip_scanlines() returned %d rather than %d\n",
  672. progname, tmp, cinfo.output_height - crop_y - crop_height);
  673. exit(EXIT_FAILURE);
  674. }
  675. /* Normal full-image decompress */
  676. } else {
  677. /* Write output file header */
  678. (*dest_mgr->start_output) (&cinfo, dest_mgr);
  679. /* Process data */
  680. while (cinfo.output_scanline < cinfo.output_height) {
  681. num_scanlines = jpeg_read_scanlines(&cinfo, dest_mgr->buffer,
  682. dest_mgr->buffer_height);
  683. (*dest_mgr->put_pixel_rows) (&cinfo, dest_mgr, num_scanlines);
  684. }
  685. }
  686. #ifdef PROGRESS_REPORT
  687. /* Hack: count final pass as done in case finish_output does an extra pass.
  688. * The library won't have updated completed_passes.
  689. */
  690. progress.pub.completed_passes = progress.pub.total_passes;
  691. #endif
  692. if (icc_filename != NULL) {
  693. FILE *icc_file;
  694. JOCTET *icc_profile;
  695. unsigned int icc_len;
  696. if ((icc_file = fopen(icc_filename, WRITE_BINARY)) == NULL) {
  697. fprintf(stderr, "%s: can't open %s\n", progname, icc_filename);
  698. exit(EXIT_FAILURE);
  699. }
  700. if (jpeg_read_icc_profile(&cinfo, &icc_profile, &icc_len)) {
  701. if (fwrite(icc_profile, icc_len, 1, icc_file) < 1) {
  702. fprintf(stderr, "%s: can't read ICC profile from %s\n", progname,
  703. icc_filename);
  704. free(icc_profile);
  705. fclose(icc_file);
  706. exit(EXIT_FAILURE);
  707. }
  708. free(icc_profile);
  709. fclose(icc_file);
  710. } else if (cinfo.err->msg_code != JWRN_BOGUS_ICC)
  711. fprintf(stderr, "%s: no ICC profile data in JPEG file\n", progname);
  712. }
  713. /* Finish decompression and release memory.
  714. * I must do it in this order because output module has allocated memory
  715. * of lifespan JPOOL_IMAGE; it needs to finish before releasing memory.
  716. */
  717. (*dest_mgr->finish_output) (&cinfo, dest_mgr);
  718. (void)jpeg_finish_decompress(&cinfo);
  719. jpeg_destroy_decompress(&cinfo);
  720. /* Close files, if we opened them */
  721. if (input_file != stdin)
  722. fclose(input_file);
  723. if (output_file != stdout)
  724. fclose(output_file);
  725. #ifdef PROGRESS_REPORT
  726. end_progress_monitor((j_common_ptr)&cinfo);
  727. #endif
  728. if (memsrc)
  729. free(inbuffer);
  730. /* All done. */
  731. exit(jerr.num_warnings ? EXIT_WARNING : EXIT_SUCCESS);
  732. return 0; /* suppress no-return-value warnings */
  733. }