blob: 20821cece6903055cf8af963235be6cf826120b9 [file] [log] [blame]
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +00001/*
2 * djpeg.c
3 *
DRCa73e8702012-12-31 02:52:30 +00004 * This file was part of the Independent JPEG Group's software:
Thomas G. Lane5ead57a1998-03-27 00:00:00 +00005 * Copyright (C) 1991-1997, Thomas G. Lane.
DRCa6ef2822013-09-28 03:23:49 +00006 * libjpeg-turbo Modifications:
DRCac30a1b2015-06-25 03:44:36 +00007 * Copyright (C) 2010-2011, 2013-2015, D. R. Commander.
8 * Copyright (C) 2015, Google, Inc.
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +00009 * For conditions of distribution and use, see the accompanying README file.
10 *
11 * This file contains a command-line user interface for the JPEG decompressor.
12 * It should work on any system with Unix- or MS-DOS-style command lines.
13 *
14 * Two different command line styles are permitted, depending on the
15 * compile-time switch TWO_FILE_COMMANDLINE:
DRCe5eaf372014-05-09 18:00:32 +000016 * djpeg [options] inputfile outputfile
17 * djpeg [options] [inputfile]
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000018 * In the second style, output is always to standard output, which you'd
19 * normally redirect to a file or pipe to some other program. Input is
20 * either from a named file or from standard input (typically redirected).
21 * The second style is convenient on Unix but is unhelpful on systems that
22 * don't support pipes. Also, you MUST use the first style if your system
23 * doesn't do binary I/O to stdin/stdout.
24 * To simplify script writing, the "-outfile" switch is provided. The syntax
DRCe5eaf372014-05-09 18:00:32 +000025 * djpeg [options] -outfile outputfile inputfile
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000026 * works regardless of which command line style is used.
27 */
28
DRCe5eaf372014-05-09 18:00:32 +000029#include "cdjpeg.h" /* Common decls for cjpeg/djpeg applications */
30#include "jversion.h" /* for version message */
DRCff6961f2014-04-20 09:17:11 +000031#include "jconfigint.h"
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000032
DRCe5eaf372014-05-09 18:00:32 +000033#include <ctype.h> /* to declare isprint() */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000034
DRCe5eaf372014-05-09 18:00:32 +000035#ifdef USE_CCOMMAND /* command-line reader for Macintosh */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000036#ifdef __MWERKS__
Thomas G. Lane489583f1996-02-07 00:00:00 +000037#include <SIOUX.h> /* Metrowerks needs this */
DRCe5eaf372014-05-09 18:00:32 +000038#include <console.h> /* ... and this */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000039#endif
40#ifdef THINK_C
DRCe5eaf372014-05-09 18:00:32 +000041#include <console.h> /* Think declares it here */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000042#endif
43#endif
44
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000045
Thomas G. Lane9ba2f5e1994-12-07 00:00:00 +000046/* Create the add-on message string table. */
47
DRCe5eaf372014-05-09 18:00:32 +000048#define JMESSAGE(code,string) string ,
Thomas G. Lane9ba2f5e1994-12-07 00:00:00 +000049
50static const char * const cdjpeg_message_table[] = {
51#include "cderror.h"
52 NULL
53};
54
55
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000056/*
57 * This list defines the known output image formats
58 * (not all of which need be supported by a given version).
59 * You can change the default output format by defining DEFAULT_FMT;
60 * indeed, you had better do so if you undefine PPM_SUPPORTED.
61 */
62
63typedef enum {
DRCe5eaf372014-05-09 18:00:32 +000064 FMT_BMP, /* BMP format (Windows flavor) */
65 FMT_GIF, /* GIF format */
66 FMT_OS2, /* BMP format (OS/2 flavor) */
67 FMT_PPM, /* PPM/PGM (PBMPLUS formats) */
68 FMT_RLE, /* RLE format */
69 FMT_TARGA, /* Targa format */
70 FMT_TIFF /* TIFF format */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000071} IMAGE_FORMATS;
72
DRCe5eaf372014-05-09 18:00:32 +000073#ifndef DEFAULT_FMT /* so can override from CFLAGS in Makefile */
74#define DEFAULT_FMT FMT_PPM
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000075#endif
76
77static IMAGE_FORMATS requested_fmt;
78
79
80/*
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000081 * Argument-parsing code.
82 * The switch parser is designed to be useful with DOS-style command line
83 * syntax, ie, intermixed switches and file names, where only the switches
84 * to the left of a given file name affect processing of that file.
85 * The main program in this file doesn't actually use this capability...
86 */
87
88
DRCe5eaf372014-05-09 18:00:32 +000089static const char * progname; /* program name for error messages */
90static char * outfilename; /* for -outfile switch */
DRCab706232013-01-18 23:42:31 +000091boolean memsrc; /* for -memsrc switch */
DRCac30a1b2015-06-25 03:44:36 +000092boolean stripe;
93JDIMENSION startY, endY;
DRCab706232013-01-18 23:42:31 +000094#define INPUT_BUF_SIZE 4096
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000095
96
Thomas G. Lane489583f1996-02-07 00:00:00 +000097LOCAL(void)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000098usage (void)
99/* complain about bad command line */
100{
101 fprintf(stderr, "usage: %s [switches] ", progname);
102#ifdef TWO_FILE_COMMANDLINE
103 fprintf(stderr, "inputfile outputfile\n");
104#else
105 fprintf(stderr, "[inputfile]\n");
106#endif
107
108 fprintf(stderr, "Switches (names may be abbreviated):\n");
109 fprintf(stderr, " -colors N Reduce image to no more than N colors\n");
110 fprintf(stderr, " -fast Fast, low-quality processing\n");
111 fprintf(stderr, " -grayscale Force grayscale output\n");
DRC6a833a82011-03-03 16:58:47 +0000112 fprintf(stderr, " -rgb Force RGB output\n");
DRC78df2e62014-05-12 09:23:57 +0000113 fprintf(stderr, " -rgb565 Force RGB565 output\n");
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000114#ifdef IDCT_SCALING_SUPPORTED
115 fprintf(stderr, " -scale M/N Scale output image by fraction M/N, eg, 1/8\n");
116#endif
117#ifdef BMP_SUPPORTED
118 fprintf(stderr, " -bmp Select BMP output format (Windows style)%s\n",
DRCe5eaf372014-05-09 18:00:32 +0000119 (DEFAULT_FMT == FMT_BMP ? " (default)" : ""));
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000120#endif
121#ifdef GIF_SUPPORTED
122 fprintf(stderr, " -gif Select GIF output format%s\n",
DRCe5eaf372014-05-09 18:00:32 +0000123 (DEFAULT_FMT == FMT_GIF ? " (default)" : ""));
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000124#endif
125#ifdef BMP_SUPPORTED
126 fprintf(stderr, " -os2 Select BMP output format (OS/2 style)%s\n",
DRCe5eaf372014-05-09 18:00:32 +0000127 (DEFAULT_FMT == FMT_OS2 ? " (default)" : ""));
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000128#endif
129#ifdef PPM_SUPPORTED
130 fprintf(stderr, " -pnm Select PBMPLUS (PPM/PGM) output format%s\n",
DRCe5eaf372014-05-09 18:00:32 +0000131 (DEFAULT_FMT == FMT_PPM ? " (default)" : ""));
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000132#endif
133#ifdef RLE_SUPPORTED
134 fprintf(stderr, " -rle Select Utah RLE output format%s\n",
DRCe5eaf372014-05-09 18:00:32 +0000135 (DEFAULT_FMT == FMT_RLE ? " (default)" : ""));
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000136#endif
137#ifdef TARGA_SUPPORTED
138 fprintf(stderr, " -targa Select Targa output format%s\n",
DRCe5eaf372014-05-09 18:00:32 +0000139 (DEFAULT_FMT == FMT_TARGA ? " (default)" : ""));
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000140#endif
141 fprintf(stderr, "Switches for advanced users:\n");
142#ifdef DCT_ISLOW_SUPPORTED
143 fprintf(stderr, " -dct int Use integer DCT method%s\n",
DRCe5eaf372014-05-09 18:00:32 +0000144 (JDCT_DEFAULT == JDCT_ISLOW ? " (default)" : ""));
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000145#endif
146#ifdef DCT_IFAST_SUPPORTED
147 fprintf(stderr, " -dct fast Use fast integer DCT (less accurate)%s\n",
DRCe5eaf372014-05-09 18:00:32 +0000148 (JDCT_DEFAULT == JDCT_IFAST ? " (default)" : ""));
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000149#endif
150#ifdef DCT_FLOAT_SUPPORTED
151 fprintf(stderr, " -dct float Use floating-point DCT method%s\n",
DRCe5eaf372014-05-09 18:00:32 +0000152 (JDCT_DEFAULT == JDCT_FLOAT ? " (default)" : ""));
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000153#endif
154 fprintf(stderr, " -dither fs Use F-S dithering (default)\n");
155 fprintf(stderr, " -dither none Don't use dithering in quantization\n");
156 fprintf(stderr, " -dither ordered Use ordered dither (medium speed, quality)\n");
157#ifdef QUANT_2PASS_SUPPORTED
158 fprintf(stderr, " -map FILE Map to colors used in named image file\n");
159#endif
160 fprintf(stderr, " -nosmooth Don't use high-quality upsampling\n");
161#ifdef QUANT_1PASS_SUPPORTED
162 fprintf(stderr, " -onepass Use 1-pass quantization (fast, low quality)\n");
163#endif
164 fprintf(stderr, " -maxmemory N Maximum memory to use (in kbytes)\n");
165 fprintf(stderr, " -outfile name Specify name for output file\n");
DRCab706232013-01-18 23:42:31 +0000166#if JPEG_LIB_VERSION >= 80 || defined(MEM_SRCDST_SUPPORTED)
167 fprintf(stderr, " -memsrc Load input file into memory before decompressing\n");
168#endif
169
DRCac30a1b2015-06-25 03:44:36 +0000170 fprintf(stderr, " -stripe Y0,Y1 Decode a horizontal stripe of the image [Y0, Y1)\n");
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000171 fprintf(stderr, " -verbose or -debug Emit debug output\n");
DRC9665f5e2014-11-22 04:04:38 +0000172 fprintf(stderr, " -version Print version information and exit\n");
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000173 exit(EXIT_FAILURE);
174}
175
176
Thomas G. Lane489583f1996-02-07 00:00:00 +0000177LOCAL(int)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000178parse_switches (j_decompress_ptr cinfo, int argc, char **argv,
DRCe5eaf372014-05-09 18:00:32 +0000179 int last_file_arg_seen, boolean for_real)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000180/* Parse optional switches.
181 * Returns argv[] index of first file-name argument (== argc if none).
182 * Any file names with indexes <= last_file_arg_seen are ignored;
183 * they have presumably been processed in a previous iteration.
184 * (Pass 0 for last_file_arg_seen on the first or only iteration.)
185 * for_real is FALSE on the first (dummy) pass; we may skip any expensive
186 * processing.
187 */
188{
189 int argn;
190 char * arg;
191
192 /* Set up default JPEG parameters. */
DRCe5eaf372014-05-09 18:00:32 +0000193 requested_fmt = DEFAULT_FMT; /* set default output file format */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000194 outfilename = NULL;
DRCab706232013-01-18 23:42:31 +0000195 memsrc = FALSE;
DRCac30a1b2015-06-25 03:44:36 +0000196 stripe = FALSE;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000197 cinfo->err->trace_level = 0;
198
199 /* Scan command line options, adjust parameters */
200
201 for (argn = 1; argn < argc; argn++) {
202 arg = argv[argn];
203 if (*arg != '-') {
204 /* Not a switch, must be a file name argument */
205 if (argn <= last_file_arg_seen) {
DRCe5eaf372014-05-09 18:00:32 +0000206 outfilename = NULL; /* -outfile applies to just one input file */
207 continue; /* ignore this name if previously processed */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000208 }
DRCe5eaf372014-05-09 18:00:32 +0000209 break; /* else done parsing switches */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000210 }
DRCe5eaf372014-05-09 18:00:32 +0000211 arg++; /* advance past switch marker character */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000212
213 if (keymatch(arg, "bmp", 1)) {
214 /* BMP output format. */
215 requested_fmt = FMT_BMP;
216
217 } else if (keymatch(arg, "colors", 1) || keymatch(arg, "colours", 1) ||
DRCe5eaf372014-05-09 18:00:32 +0000218 keymatch(arg, "quantize", 1) || keymatch(arg, "quantise", 1)) {
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000219 /* Do color quantization. */
220 int val;
221
DRCe5eaf372014-05-09 18:00:32 +0000222 if (++argn >= argc) /* advance to next argument */
223 usage();
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000224 if (sscanf(argv[argn], "%d", &val) != 1)
DRCe5eaf372014-05-09 18:00:32 +0000225 usage();
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000226 cinfo->desired_number_of_colors = val;
227 cinfo->quantize_colors = TRUE;
228
229 } else if (keymatch(arg, "dct", 2)) {
230 /* Select IDCT algorithm. */
DRCe5eaf372014-05-09 18:00:32 +0000231 if (++argn >= argc) /* advance to next argument */
232 usage();
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000233 if (keymatch(argv[argn], "int", 1)) {
DRCe5eaf372014-05-09 18:00:32 +0000234 cinfo->dct_method = JDCT_ISLOW;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000235 } else if (keymatch(argv[argn], "fast", 2)) {
DRCe5eaf372014-05-09 18:00:32 +0000236 cinfo->dct_method = JDCT_IFAST;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000237 } else if (keymatch(argv[argn], "float", 2)) {
DRCe5eaf372014-05-09 18:00:32 +0000238 cinfo->dct_method = JDCT_FLOAT;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000239 } else
DRCe5eaf372014-05-09 18:00:32 +0000240 usage();
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000241
242 } else if (keymatch(arg, "dither", 2)) {
243 /* Select dithering algorithm. */
DRCe5eaf372014-05-09 18:00:32 +0000244 if (++argn >= argc) /* advance to next argument */
245 usage();
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000246 if (keymatch(argv[argn], "fs", 2)) {
DRCe5eaf372014-05-09 18:00:32 +0000247 cinfo->dither_mode = JDITHER_FS;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000248 } else if (keymatch(argv[argn], "none", 2)) {
DRCe5eaf372014-05-09 18:00:32 +0000249 cinfo->dither_mode = JDITHER_NONE;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000250 } else if (keymatch(argv[argn], "ordered", 2)) {
DRCe5eaf372014-05-09 18:00:32 +0000251 cinfo->dither_mode = JDITHER_ORDERED;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000252 } else
DRCe5eaf372014-05-09 18:00:32 +0000253 usage();
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000254
255 } else if (keymatch(arg, "debug", 1) || keymatch(arg, "verbose", 1)) {
256 /* Enable debug printouts. */
257 /* On first -d, print version identification */
258 static boolean printed_version = FALSE;
259
260 if (! printed_version) {
DRCe5eaf372014-05-09 18:00:32 +0000261 fprintf(stderr, "%s version %s (build %s)\n",
262 PACKAGE_NAME, VERSION, BUILD);
263 fprintf(stderr, "%s\n\n", JCOPYRIGHT);
264 fprintf(stderr, "Emulating The Independent JPEG Group's software, version %s\n\n",
265 JVERSION);
266 printed_version = TRUE;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000267 }
268 cinfo->err->trace_level++;
269
DRC9665f5e2014-11-22 04:04:38 +0000270 } else if (keymatch(arg, "version", 4)) {
271 fprintf(stderr, "%s version %s (build %s)\n",
272 PACKAGE_NAME, VERSION, BUILD);
DRC306add82014-11-22 04:25:04 +0000273 exit(EXIT_SUCCESS);
DRC9665f5e2014-11-22 04:04:38 +0000274
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000275 } else if (keymatch(arg, "fast", 1)) {
276 /* Select recommended processing options for quick-and-dirty output. */
277 cinfo->two_pass_quantize = FALSE;
278 cinfo->dither_mode = JDITHER_ORDERED;
279 if (! cinfo->quantize_colors) /* don't override an earlier -colors */
DRCe5eaf372014-05-09 18:00:32 +0000280 cinfo->desired_number_of_colors = 216;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000281 cinfo->dct_method = JDCT_FASTEST;
282 cinfo->do_fancy_upsampling = FALSE;
283
284 } else if (keymatch(arg, "gif", 1)) {
285 /* GIF output format. */
286 requested_fmt = FMT_GIF;
287
288 } else if (keymatch(arg, "grayscale", 2) || keymatch(arg, "greyscale",2)) {
289 /* Force monochrome output. */
290 cinfo->out_color_space = JCS_GRAYSCALE;
291
DRC6a833a82011-03-03 16:58:47 +0000292 } else if (keymatch(arg, "rgb", 2)) {
293 /* Force RGB output. */
294 cinfo->out_color_space = JCS_RGB;
295
DRC78df2e62014-05-12 09:23:57 +0000296 } else if (keymatch(arg, "rgb565", 2)) {
297 /* Force RGB565 output. */
298 cinfo->out_color_space = JCS_RGB565;
299
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000300 } else if (keymatch(arg, "map", 3)) {
301 /* Quantize to a color map taken from an input file. */
DRCe5eaf372014-05-09 18:00:32 +0000302 if (++argn >= argc) /* advance to next argument */
303 usage();
304 if (for_real) { /* too expensive to do twice! */
305#ifdef QUANT_2PASS_SUPPORTED /* otherwise can't quantize to supplied map */
306 FILE * mapfile;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000307
DRCe5eaf372014-05-09 18:00:32 +0000308 if ((mapfile = fopen(argv[argn], READ_BINARY)) == NULL) {
309 fprintf(stderr, "%s: can't open %s\n", progname, argv[argn]);
310 exit(EXIT_FAILURE);
311 }
312 read_color_map(cinfo, mapfile);
313 fclose(mapfile);
314 cinfo->quantize_colors = TRUE;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000315#else
DRCe5eaf372014-05-09 18:00:32 +0000316 ERREXIT(cinfo, JERR_NOT_COMPILED);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000317#endif
318 }
319
320 } else if (keymatch(arg, "maxmemory", 3)) {
321 /* Maximum memory in Kb (or Mb with 'm'). */
322 long lval;
323 char ch = 'x';
324
DRCe5eaf372014-05-09 18:00:32 +0000325 if (++argn >= argc) /* advance to next argument */
326 usage();
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000327 if (sscanf(argv[argn], "%ld%c", &lval, &ch) < 1)
DRCe5eaf372014-05-09 18:00:32 +0000328 usage();
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000329 if (ch == 'm' || ch == 'M')
DRCe5eaf372014-05-09 18:00:32 +0000330 lval *= 1000L;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000331 cinfo->mem->max_memory_to_use = lval * 1000L;
332
333 } else if (keymatch(arg, "nosmooth", 3)) {
334 /* Suppress fancy upsampling */
335 cinfo->do_fancy_upsampling = FALSE;
336
337 } else if (keymatch(arg, "onepass", 3)) {
338 /* Use fast one-pass quantization. */
339 cinfo->two_pass_quantize = FALSE;
340
341 } else if (keymatch(arg, "os2", 3)) {
342 /* BMP output format (OS/2 flavor). */
343 requested_fmt = FMT_OS2;
344
345 } else if (keymatch(arg, "outfile", 4)) {
346 /* Set output file name. */
DRCe5eaf372014-05-09 18:00:32 +0000347 if (++argn >= argc) /* advance to next argument */
348 usage();
349 outfilename = argv[argn]; /* save it away for later use */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000350
DRCab706232013-01-18 23:42:31 +0000351 } else if (keymatch(arg, "memsrc", 2)) {
352 /* Use in-memory source manager */
353#if JPEG_LIB_VERSION >= 80 || defined(MEM_SRCDST_SUPPORTED)
354 memsrc = TRUE;
355#else
356 fprintf(stderr, "%s: sorry, in-memory source manager was not compiled in\n",
357 progname);
358 exit(EXIT_FAILURE);
359#endif
360
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000361 } else if (keymatch(arg, "pnm", 1) || keymatch(arg, "ppm", 1)) {
362 /* PPM/PGM output format. */
363 requested_fmt = FMT_PPM;
364
365 } else if (keymatch(arg, "rle", 1)) {
366 /* RLE output format. */
367 requested_fmt = FMT_RLE;
368
DRCac30a1b2015-06-25 03:44:36 +0000369 } else if (keymatch(arg, "scale", 2)) {
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000370 /* Scale the output image by a fraction M/N. */
DRCe5eaf372014-05-09 18:00:32 +0000371 if (++argn >= argc) /* advance to next argument */
372 usage();
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000373 if (sscanf(argv[argn], "%d/%d",
DRCe5eaf372014-05-09 18:00:32 +0000374 &cinfo->scale_num, &cinfo->scale_denom) != 2)
375 usage();
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000376
DRCac30a1b2015-06-25 03:44:36 +0000377 } else if (keymatch(arg, "stripe", 2)) {
378 if (++argn >= argc)
379 usage();
380 if (sscanf(argv[argn], "%d,%d", &startY, &endY) != 2 || startY > endY)
381 usage();
382 stripe = TRUE;
383
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000384 } else if (keymatch(arg, "targa", 1)) {
385 /* Targa output format. */
386 requested_fmt = FMT_TARGA;
387
388 } else {
DRCe5eaf372014-05-09 18:00:32 +0000389 usage(); /* bogus switch */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000390 }
391 }
392
DRCe5eaf372014-05-09 18:00:32 +0000393 return argn; /* return index of next arg (file name) */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000394}
395
396
397/*
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000398 * Marker processor for COM and interesting APPn markers.
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000399 * This replaces the library's built-in processor, which just skips the marker.
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000400 * We want to print out the marker as text, to the extent possible.
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000401 * Note this code relies on a non-suspending data source.
402 */
403
Thomas G. Lane489583f1996-02-07 00:00:00 +0000404LOCAL(unsigned int)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000405jpeg_getc (j_decompress_ptr cinfo)
406/* Read next byte */
407{
408 struct jpeg_source_mgr * datasrc = cinfo->src;
409
410 if (datasrc->bytes_in_buffer == 0) {
411 if (! (*datasrc->fill_input_buffer) (cinfo))
412 ERREXIT(cinfo, JERR_CANT_SUSPEND);
413 }
414 datasrc->bytes_in_buffer--;
415 return GETJOCTET(*datasrc->next_input_byte++);
416}
417
418
Thomas G. Lane489583f1996-02-07 00:00:00 +0000419METHODDEF(boolean)
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000420print_text_marker (j_decompress_ptr cinfo)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000421{
422 boolean traceit = (cinfo->err->trace_level >= 1);
423 INT32 length;
424 unsigned int ch;
425 unsigned int lastch = 0;
426
427 length = jpeg_getc(cinfo) << 8;
428 length += jpeg_getc(cinfo);
DRCe5eaf372014-05-09 18:00:32 +0000429 length -= 2; /* discount the length word itself */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000430
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000431 if (traceit) {
432 if (cinfo->unread_marker == JPEG_COM)
433 fprintf(stderr, "Comment, length %ld:\n", (long) length);
DRCe5eaf372014-05-09 18:00:32 +0000434 else /* assume it is an APPn otherwise */
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000435 fprintf(stderr, "APP%d, length %ld:\n",
DRCe5eaf372014-05-09 18:00:32 +0000436 cinfo->unread_marker - JPEG_APP0, (long) length);
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000437 }
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000438
439 while (--length >= 0) {
440 ch = jpeg_getc(cinfo);
441 if (traceit) {
442 /* Emit the character in a readable form.
443 * Nonprintables are converted to \nnn form,
444 * while \ is converted to \\.
445 * Newlines in CR, CR/LF, or LF form will be printed as one newline.
446 */
447 if (ch == '\r') {
DRCe5eaf372014-05-09 18:00:32 +0000448 fprintf(stderr, "\n");
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000449 } else if (ch == '\n') {
DRCe5eaf372014-05-09 18:00:32 +0000450 if (lastch != '\r')
451 fprintf(stderr, "\n");
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000452 } else if (ch == '\\') {
DRCe5eaf372014-05-09 18:00:32 +0000453 fprintf(stderr, "\\\\");
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000454 } else if (isprint(ch)) {
DRCe5eaf372014-05-09 18:00:32 +0000455 putc(ch, stderr);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000456 } else {
DRCe5eaf372014-05-09 18:00:32 +0000457 fprintf(stderr, "\\%03o", ch);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000458 }
459 lastch = ch;
460 }
461 }
462
463 if (traceit)
464 fprintf(stderr, "\n");
465
466 return TRUE;
467}
468
469
470/*
471 * The main program.
472 */
473
Thomas G. Lane489583f1996-02-07 00:00:00 +0000474int
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000475main (int argc, char **argv)
476{
477 struct jpeg_decompress_struct cinfo;
478 struct jpeg_error_mgr jerr;
479#ifdef PROGRESS_REPORT
480 struct cdjpeg_progress_mgr progress;
481#endif
482 int file_index;
483 djpeg_dest_ptr dest_mgr = NULL;
484 FILE * input_file;
485 FILE * output_file;
DRCab706232013-01-18 23:42:31 +0000486 unsigned char *inbuffer = NULL;
487 unsigned long insize = 0;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000488 JDIMENSION num_scanlines;
489
490 /* On Mac, fetch a command line. */
491#ifdef USE_CCOMMAND
492 argc = ccommand(&argv);
493#endif
494
495 progname = argv[0];
496 if (progname == NULL || progname[0] == 0)
DRCe5eaf372014-05-09 18:00:32 +0000497 progname = "djpeg"; /* in case C library doesn't provide it */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000498
499 /* Initialize the JPEG decompression object with default error handling. */
500 cinfo.err = jpeg_std_error(&jerr);
501 jpeg_create_decompress(&cinfo);
502 /* Add some application-specific error messages (from cderror.h) */
Thomas G. Lane9ba2f5e1994-12-07 00:00:00 +0000503 jerr.addon_message_table = cdjpeg_message_table;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000504 jerr.first_addon_message = JMSG_FIRSTADDONCODE;
505 jerr.last_addon_message = JMSG_LASTADDONCODE;
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000506
507 /* Insert custom marker processor for COM and APP12.
508 * APP12 is used by some digital camera makers for textual info,
509 * so we provide the ability to display it as text.
510 * If you like, additional APPn marker types can be selected for display,
DRC39ea5622010-10-12 01:55:31 +0000511 * but don't try to override APP0 or APP14 this way (see libjpeg.txt).
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000512 */
513 jpeg_set_marker_processor(&cinfo, JPEG_COM, print_text_marker);
514 jpeg_set_marker_processor(&cinfo, JPEG_APP0+12, print_text_marker);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000515
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000516 /* Scan command line to find file names. */
517 /* It is convenient to use just one switch-parsing routine, but the switch
518 * values read here are ignored; we will rescan the switches after opening
519 * the input file.
520 * (Exception: tracing level set here controls verbosity for COM markers
521 * found during jpeg_read_header...)
522 */
523
524 file_index = parse_switches(&cinfo, argc, argv, 0, FALSE);
525
526#ifdef TWO_FILE_COMMANDLINE
527 /* Must have either -outfile switch or explicit output file name */
528 if (outfilename == NULL) {
529 if (file_index != argc-2) {
530 fprintf(stderr, "%s: must name one input and one output file\n",
DRCe5eaf372014-05-09 18:00:32 +0000531 progname);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000532 usage();
533 }
534 outfilename = argv[file_index+1];
535 } else {
536 if (file_index != argc-1) {
537 fprintf(stderr, "%s: must name one input and one output file\n",
DRCe5eaf372014-05-09 18:00:32 +0000538 progname);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000539 usage();
540 }
541 }
542#else
543 /* Unix style: expect zero or one file name */
544 if (file_index < argc-1) {
545 fprintf(stderr, "%s: only one input file\n", progname);
546 usage();
547 }
548#endif /* TWO_FILE_COMMANDLINE */
549
550 /* Open the input file. */
551 if (file_index < argc) {
552 if ((input_file = fopen(argv[file_index], READ_BINARY)) == NULL) {
553 fprintf(stderr, "%s: can't open %s\n", progname, argv[file_index]);
554 exit(EXIT_FAILURE);
555 }
556 } else {
557 /* default input file is stdin */
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000558 input_file = read_stdin();
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000559 }
560
561 /* Open the output file. */
562 if (outfilename != NULL) {
563 if ((output_file = fopen(outfilename, WRITE_BINARY)) == NULL) {
564 fprintf(stderr, "%s: can't open %s\n", progname, outfilename);
565 exit(EXIT_FAILURE);
566 }
567 } else {
568 /* default output file is stdout */
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000569 output_file = write_stdout();
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000570 }
571
572#ifdef PROGRESS_REPORT
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000573 start_progress_monitor((j_common_ptr) &cinfo, &progress);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000574#endif
575
576 /* Specify data source for decompression */
DRCab706232013-01-18 23:42:31 +0000577#if JPEG_LIB_VERSION >= 80 || defined(MEM_SRCDST_SUPPORTED)
578 if (memsrc) {
579 size_t nbytes;
580 do {
581 inbuffer = (unsigned char *)realloc(inbuffer, insize + INPUT_BUF_SIZE);
582 if (inbuffer == NULL) {
583 fprintf(stderr, "%s: memory allocation failure\n", progname);
584 exit(EXIT_FAILURE);
585 }
586 nbytes = JFREAD(input_file, &inbuffer[insize], INPUT_BUF_SIZE);
DRCc45653e2013-10-12 21:52:48 +0000587 if (nbytes < INPUT_BUF_SIZE && ferror(input_file)) {
DRCab706232013-01-18 23:42:31 +0000588 if (file_index < argc)
589 fprintf(stderr, "%s: can't read from %s\n", progname,
590 argv[file_index]);
591 else
592 fprintf(stderr, "%s: can't read from stdin\n", progname);
593 }
DRC736fb062013-01-18 23:45:06 +0000594 insize += (unsigned long)nbytes;
DRCab706232013-01-18 23:42:31 +0000595 } while (nbytes == INPUT_BUF_SIZE);
596 fprintf(stderr, "Compressed size: %lu bytes\n", insize);
597 jpeg_mem_src(&cinfo, inbuffer, insize);
598 } else
599#endif
600 jpeg_stdio_src(&cinfo, input_file);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000601
602 /* Read file header, set default decompression parameters */
603 (void) jpeg_read_header(&cinfo, TRUE);
604
605 /* Adjust default decompression parameters by re-parsing the options */
606 file_index = parse_switches(&cinfo, argc, argv, 0, TRUE);
607
608 /* Initialize the output module now to let it override any crucial
609 * option settings (for instance, GIF wants to force color quantization).
610 */
611 switch (requested_fmt) {
612#ifdef BMP_SUPPORTED
613 case FMT_BMP:
614 dest_mgr = jinit_write_bmp(&cinfo, FALSE);
615 break;
616 case FMT_OS2:
617 dest_mgr = jinit_write_bmp(&cinfo, TRUE);
618 break;
619#endif
620#ifdef GIF_SUPPORTED
621 case FMT_GIF:
622 dest_mgr = jinit_write_gif(&cinfo);
623 break;
624#endif
625#ifdef PPM_SUPPORTED
626 case FMT_PPM:
627 dest_mgr = jinit_write_ppm(&cinfo);
628 break;
629#endif
630#ifdef RLE_SUPPORTED
631 case FMT_RLE:
632 dest_mgr = jinit_write_rle(&cinfo);
633 break;
634#endif
635#ifdef TARGA_SUPPORTED
636 case FMT_TARGA:
637 dest_mgr = jinit_write_targa(&cinfo);
638 break;
639#endif
640 default:
641 ERREXIT(&cinfo, JERR_UNSUPPORTED_FORMAT);
642 break;
643 }
644 dest_mgr->output_file = output_file;
645
646 /* Start decompressor */
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000647 (void) jpeg_start_decompress(&cinfo);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000648
DRCac30a1b2015-06-25 03:44:36 +0000649 /* Stripe decode */
650 if (stripe) {
651 JDIMENSION tmp;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000652
DRCac30a1b2015-06-25 03:44:36 +0000653 /* Check for valid endY. We cannot check this value until after
654 * jpeg_start_decompress() is called. Note that we have already verified
655 * that startY <= endY.
656 */
657 if (endY > cinfo.output_height) {
658 fprintf(stderr, "%s: stripe %d-%d exceeds image height %d\n", progname,
659 startY, endY, cinfo.output_height);
660 exit(EXIT_FAILURE);
661 }
662
663 /* Write output file header. This is a hack to ensure that the destination
664 * manager creates an image of the proper size for the partial decode.
665 */
666 tmp = cinfo.output_height;
667 cinfo.output_height = endY - startY;
668 (*dest_mgr->start_output) (&cinfo, dest_mgr);
669 cinfo.output_height = tmp;
670
671 /* Process data */
672 (void) jpeg_skip_scanlines(&cinfo, startY);
673 while (cinfo.output_scanline < endY) {
674 num_scanlines = jpeg_read_scanlines(&cinfo, dest_mgr->buffer,
675 dest_mgr->buffer_height);
676 (*dest_mgr->put_pixel_rows) (&cinfo, dest_mgr, num_scanlines);
677 }
678 (void) jpeg_skip_scanlines(&cinfo, cinfo.output_height - endY);
679
680 /* Normal full image decode */
681 } else {
682 /* Write output file header */
683 (*dest_mgr->start_output) (&cinfo, dest_mgr);
684
685 /* Process data */
686 while (cinfo.output_scanline < cinfo.output_height) {
687 num_scanlines = jpeg_read_scanlines(&cinfo, dest_mgr->buffer,
688 dest_mgr->buffer_height);
689 (*dest_mgr->put_pixel_rows) (&cinfo, dest_mgr, num_scanlines);
690 }
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000691 }
692
693#ifdef PROGRESS_REPORT
694 /* Hack: count final pass as done in case finish_output does an extra pass.
695 * The library won't have updated completed_passes.
696 */
697 progress.pub.completed_passes = progress.pub.total_passes;
698#endif
699
700 /* Finish decompression and release memory.
701 * I must do it in this order because output module has allocated memory
702 * of lifespan JPOOL_IMAGE; it needs to finish before releasing memory.
703 */
704 (*dest_mgr->finish_output) (&cinfo, dest_mgr);
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000705 (void) jpeg_finish_decompress(&cinfo);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000706 jpeg_destroy_decompress(&cinfo);
707
Thomas G. Lane9ba2f5e1994-12-07 00:00:00 +0000708 /* Close files, if we opened them */
709 if (input_file != stdin)
710 fclose(input_file);
711 if (output_file != stdout)
712 fclose(output_file);
713
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000714#ifdef PROGRESS_REPORT
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000715 end_progress_monitor((j_common_ptr) &cinfo);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000716#endif
717
DRCab706232013-01-18 23:42:31 +0000718 if (memsrc && inbuffer != NULL)
719 free(inbuffer);
720
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000721 /* All done. */
722 exit(jerr.num_warnings ? EXIT_WARNING : EXIT_SUCCESS);
DRCe5eaf372014-05-09 18:00:32 +0000723 return 0; /* suppress no-return-value warnings */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000724}