blob: 93567c6879b1fa2d44d62937c7dbe70de48e2788 [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:
DRCeb32cc12015-06-25 03:44:36 +00007 * Copyright (C) 2010-2011, 2013-2015, D. R. Commander.
8 * Copyright (C) 2015, Google, Inc.
DRC7e3acc02015-10-10 10:25:46 -05009 * For conditions of distribution and use, see the accompanying README.ijg
10 * file.
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000011 *
12 * This file contains a command-line user interface for the JPEG decompressor.
13 * It should work on any system with Unix- or MS-DOS-style command lines.
14 *
15 * Two different command line styles are permitted, depending on the
16 * compile-time switch TWO_FILE_COMMANDLINE:
DRCe5eaf372014-05-09 18:00:32 +000017 * djpeg [options] inputfile outputfile
18 * djpeg [options] [inputfile]
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000019 * In the second style, output is always to standard output, which you'd
20 * normally redirect to a file or pipe to some other program. Input is
21 * either from a named file or from standard input (typically redirected).
22 * The second style is convenient on Unix but is unhelpful on systems that
23 * don't support pipes. Also, you MUST use the first style if your system
24 * doesn't do binary I/O to stdin/stdout.
25 * To simplify script writing, the "-outfile" switch is provided. The syntax
DRCe5eaf372014-05-09 18:00:32 +000026 * djpeg [options] -outfile outputfile inputfile
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000027 * works regardless of which command line style is used.
28 */
29
DRCe5eaf372014-05-09 18:00:32 +000030#include "cdjpeg.h" /* Common decls for cjpeg/djpeg applications */
31#include "jversion.h" /* for version message */
DRCff6961f2014-04-20 09:17:11 +000032#include "jconfigint.h"
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000033
DRCe5eaf372014-05-09 18:00:32 +000034#include <ctype.h> /* to declare isprint() */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000035
DRCe5eaf372014-05-09 18:00:32 +000036#ifdef USE_CCOMMAND /* command-line reader for Macintosh */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000037#ifdef __MWERKS__
Thomas G. Lane489583f1996-02-07 00:00:00 +000038#include <SIOUX.h> /* Metrowerks needs this */
DRCe5eaf372014-05-09 18:00:32 +000039#include <console.h> /* ... and this */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000040#endif
41#ifdef THINK_C
DRCe5eaf372014-05-09 18:00:32 +000042#include <console.h> /* Think declares it here */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000043#endif
44#endif
45
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000046
Thomas G. Lane9ba2f5e1994-12-07 00:00:00 +000047/* Create the add-on message string table. */
48
DRCe5eaf372014-05-09 18:00:32 +000049#define JMESSAGE(code,string) string ,
Thomas G. Lane9ba2f5e1994-12-07 00:00:00 +000050
51static const char * const cdjpeg_message_table[] = {
52#include "cderror.h"
53 NULL
54};
55
56
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000057/*
58 * This list defines the known output image formats
59 * (not all of which need be supported by a given version).
60 * You can change the default output format by defining DEFAULT_FMT;
61 * indeed, you had better do so if you undefine PPM_SUPPORTED.
62 */
63
64typedef enum {
DRCe5eaf372014-05-09 18:00:32 +000065 FMT_BMP, /* BMP format (Windows flavor) */
66 FMT_GIF, /* GIF format */
67 FMT_OS2, /* BMP format (OS/2 flavor) */
68 FMT_PPM, /* PPM/PGM (PBMPLUS formats) */
69 FMT_RLE, /* RLE format */
70 FMT_TARGA, /* Targa format */
71 FMT_TIFF /* TIFF format */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000072} IMAGE_FORMATS;
73
DRCe5eaf372014-05-09 18:00:32 +000074#ifndef DEFAULT_FMT /* so can override from CFLAGS in Makefile */
75#define DEFAULT_FMT FMT_PPM
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000076#endif
77
78static IMAGE_FORMATS requested_fmt;
79
80
81/*
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000082 * Argument-parsing code.
83 * The switch parser is designed to be useful with DOS-style command line
84 * syntax, ie, intermixed switches and file names, where only the switches
85 * to the left of a given file name affect processing of that file.
86 * The main program in this file doesn't actually use this capability...
87 */
88
89
DRCe5eaf372014-05-09 18:00:32 +000090static const char * progname; /* program name for error messages */
91static char * outfilename; /* for -outfile switch */
DRCab706232013-01-18 23:42:31 +000092boolean memsrc; /* for -memsrc switch */
DRC7a7da942015-06-27 08:10:31 +000093boolean strip, skip;
DRCeb32cc12015-06-25 03:44:36 +000094JDIMENSION startY, endY;
DRCab706232013-01-18 23:42:31 +000095#define INPUT_BUF_SIZE 4096
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000096
97
Thomas G. Lane489583f1996-02-07 00:00:00 +000098LOCAL(void)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000099usage (void)
100/* complain about bad command line */
101{
102 fprintf(stderr, "usage: %s [switches] ", progname);
103#ifdef TWO_FILE_COMMANDLINE
104 fprintf(stderr, "inputfile outputfile\n");
105#else
106 fprintf(stderr, "[inputfile]\n");
107#endif
108
109 fprintf(stderr, "Switches (names may be abbreviated):\n");
110 fprintf(stderr, " -colors N Reduce image to no more than N colors\n");
111 fprintf(stderr, " -fast Fast, low-quality processing\n");
112 fprintf(stderr, " -grayscale Force grayscale output\n");
DRC6a833a82011-03-03 16:58:47 +0000113 fprintf(stderr, " -rgb Force RGB output\n");
DRC78df2e62014-05-12 09:23:57 +0000114 fprintf(stderr, " -rgb565 Force RGB565 output\n");
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000115#ifdef IDCT_SCALING_SUPPORTED
116 fprintf(stderr, " -scale M/N Scale output image by fraction M/N, eg, 1/8\n");
117#endif
118#ifdef BMP_SUPPORTED
119 fprintf(stderr, " -bmp Select BMP output format (Windows style)%s\n",
DRCe5eaf372014-05-09 18:00:32 +0000120 (DEFAULT_FMT == FMT_BMP ? " (default)" : ""));
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000121#endif
122#ifdef GIF_SUPPORTED
123 fprintf(stderr, " -gif Select GIF output format%s\n",
DRCe5eaf372014-05-09 18:00:32 +0000124 (DEFAULT_FMT == FMT_GIF ? " (default)" : ""));
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000125#endif
126#ifdef BMP_SUPPORTED
127 fprintf(stderr, " -os2 Select BMP output format (OS/2 style)%s\n",
DRCe5eaf372014-05-09 18:00:32 +0000128 (DEFAULT_FMT == FMT_OS2 ? " (default)" : ""));
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000129#endif
130#ifdef PPM_SUPPORTED
131 fprintf(stderr, " -pnm Select PBMPLUS (PPM/PGM) output format%s\n",
DRCe5eaf372014-05-09 18:00:32 +0000132 (DEFAULT_FMT == FMT_PPM ? " (default)" : ""));
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000133#endif
134#ifdef RLE_SUPPORTED
135 fprintf(stderr, " -rle Select Utah RLE output format%s\n",
DRCe5eaf372014-05-09 18:00:32 +0000136 (DEFAULT_FMT == FMT_RLE ? " (default)" : ""));
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000137#endif
138#ifdef TARGA_SUPPORTED
139 fprintf(stderr, " -targa Select Targa output format%s\n",
DRCe5eaf372014-05-09 18:00:32 +0000140 (DEFAULT_FMT == FMT_TARGA ? " (default)" : ""));
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000141#endif
142 fprintf(stderr, "Switches for advanced users:\n");
143#ifdef DCT_ISLOW_SUPPORTED
144 fprintf(stderr, " -dct int Use integer DCT method%s\n",
DRCe5eaf372014-05-09 18:00:32 +0000145 (JDCT_DEFAULT == JDCT_ISLOW ? " (default)" : ""));
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000146#endif
147#ifdef DCT_IFAST_SUPPORTED
148 fprintf(stderr, " -dct fast Use fast integer DCT (less accurate)%s\n",
DRCe5eaf372014-05-09 18:00:32 +0000149 (JDCT_DEFAULT == JDCT_IFAST ? " (default)" : ""));
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000150#endif
151#ifdef DCT_FLOAT_SUPPORTED
152 fprintf(stderr, " -dct float Use floating-point DCT method%s\n",
DRCe5eaf372014-05-09 18:00:32 +0000153 (JDCT_DEFAULT == JDCT_FLOAT ? " (default)" : ""));
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000154#endif
155 fprintf(stderr, " -dither fs Use F-S dithering (default)\n");
156 fprintf(stderr, " -dither none Don't use dithering in quantization\n");
157 fprintf(stderr, " -dither ordered Use ordered dither (medium speed, quality)\n");
158#ifdef QUANT_2PASS_SUPPORTED
159 fprintf(stderr, " -map FILE Map to colors used in named image file\n");
160#endif
161 fprintf(stderr, " -nosmooth Don't use high-quality upsampling\n");
162#ifdef QUANT_1PASS_SUPPORTED
163 fprintf(stderr, " -onepass Use 1-pass quantization (fast, low quality)\n");
164#endif
165 fprintf(stderr, " -maxmemory N Maximum memory to use (in kbytes)\n");
166 fprintf(stderr, " -outfile name Specify name for output file\n");
DRCab706232013-01-18 23:42:31 +0000167#if JPEG_LIB_VERSION >= 80 || defined(MEM_SRCDST_SUPPORTED)
168 fprintf(stderr, " -memsrc Load input file into memory before decompressing\n");
169#endif
170
DRC7a7da942015-06-27 08:10:31 +0000171 fprintf(stderr, " -skip Y0,Y1 Decode all rows except those between Y0 and Y1 (inclusive)\n");
172 fprintf(stderr, " -strip Y0,Y1 Decode only rows between Y0 and Y1 (inclusive)\n");
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000173 fprintf(stderr, " -verbose or -debug Emit debug output\n");
DRC9665f5e2014-11-22 04:04:38 +0000174 fprintf(stderr, " -version Print version information and exit\n");
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000175 exit(EXIT_FAILURE);
176}
177
178
Thomas G. Lane489583f1996-02-07 00:00:00 +0000179LOCAL(int)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000180parse_switches (j_decompress_ptr cinfo, int argc, char **argv,
DRCe5eaf372014-05-09 18:00:32 +0000181 int last_file_arg_seen, boolean for_real)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000182/* Parse optional switches.
183 * Returns argv[] index of first file-name argument (== argc if none).
184 * Any file names with indexes <= last_file_arg_seen are ignored;
185 * they have presumably been processed in a previous iteration.
186 * (Pass 0 for last_file_arg_seen on the first or only iteration.)
187 * for_real is FALSE on the first (dummy) pass; we may skip any expensive
188 * processing.
189 */
190{
191 int argn;
192 char * arg;
193
194 /* Set up default JPEG parameters. */
DRCe5eaf372014-05-09 18:00:32 +0000195 requested_fmt = DEFAULT_FMT; /* set default output file format */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000196 outfilename = NULL;
DRCab706232013-01-18 23:42:31 +0000197 memsrc = FALSE;
DRC7a7da942015-06-27 08:10:31 +0000198 strip = FALSE;
DRC07afe8a2015-06-25 03:44:37 +0000199 skip = FALSE;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000200 cinfo->err->trace_level = 0;
201
202 /* Scan command line options, adjust parameters */
203
204 for (argn = 1; argn < argc; argn++) {
205 arg = argv[argn];
206 if (*arg != '-') {
207 /* Not a switch, must be a file name argument */
208 if (argn <= last_file_arg_seen) {
DRCe5eaf372014-05-09 18:00:32 +0000209 outfilename = NULL; /* -outfile applies to just one input file */
210 continue; /* ignore this name if previously processed */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000211 }
DRCe5eaf372014-05-09 18:00:32 +0000212 break; /* else done parsing switches */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000213 }
DRCe5eaf372014-05-09 18:00:32 +0000214 arg++; /* advance past switch marker character */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000215
216 if (keymatch(arg, "bmp", 1)) {
217 /* BMP output format. */
218 requested_fmt = FMT_BMP;
219
220 } else if (keymatch(arg, "colors", 1) || keymatch(arg, "colours", 1) ||
DRCe5eaf372014-05-09 18:00:32 +0000221 keymatch(arg, "quantize", 1) || keymatch(arg, "quantise", 1)) {
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000222 /* Do color quantization. */
223 int val;
224
DRCe5eaf372014-05-09 18:00:32 +0000225 if (++argn >= argc) /* advance to next argument */
226 usage();
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000227 if (sscanf(argv[argn], "%d", &val) != 1)
DRCe5eaf372014-05-09 18:00:32 +0000228 usage();
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000229 cinfo->desired_number_of_colors = val;
230 cinfo->quantize_colors = TRUE;
231
232 } else if (keymatch(arg, "dct", 2)) {
233 /* Select IDCT algorithm. */
DRCe5eaf372014-05-09 18:00:32 +0000234 if (++argn >= argc) /* advance to next argument */
235 usage();
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000236 if (keymatch(argv[argn], "int", 1)) {
DRCe5eaf372014-05-09 18:00:32 +0000237 cinfo->dct_method = JDCT_ISLOW;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000238 } else if (keymatch(argv[argn], "fast", 2)) {
DRCe5eaf372014-05-09 18:00:32 +0000239 cinfo->dct_method = JDCT_IFAST;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000240 } else if (keymatch(argv[argn], "float", 2)) {
DRCe5eaf372014-05-09 18:00:32 +0000241 cinfo->dct_method = JDCT_FLOAT;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000242 } else
DRCe5eaf372014-05-09 18:00:32 +0000243 usage();
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000244
245 } else if (keymatch(arg, "dither", 2)) {
246 /* Select dithering algorithm. */
DRCe5eaf372014-05-09 18:00:32 +0000247 if (++argn >= argc) /* advance to next argument */
248 usage();
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000249 if (keymatch(argv[argn], "fs", 2)) {
DRCe5eaf372014-05-09 18:00:32 +0000250 cinfo->dither_mode = JDITHER_FS;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000251 } else if (keymatch(argv[argn], "none", 2)) {
DRCe5eaf372014-05-09 18:00:32 +0000252 cinfo->dither_mode = JDITHER_NONE;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000253 } else if (keymatch(argv[argn], "ordered", 2)) {
DRCe5eaf372014-05-09 18:00:32 +0000254 cinfo->dither_mode = JDITHER_ORDERED;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000255 } else
DRCe5eaf372014-05-09 18:00:32 +0000256 usage();
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000257
258 } else if (keymatch(arg, "debug", 1) || keymatch(arg, "verbose", 1)) {
259 /* Enable debug printouts. */
260 /* On first -d, print version identification */
261 static boolean printed_version = FALSE;
262
263 if (! printed_version) {
DRCe5eaf372014-05-09 18:00:32 +0000264 fprintf(stderr, "%s version %s (build %s)\n",
265 PACKAGE_NAME, VERSION, BUILD);
266 fprintf(stderr, "%s\n\n", JCOPYRIGHT);
267 fprintf(stderr, "Emulating The Independent JPEG Group's software, version %s\n\n",
268 JVERSION);
269 printed_version = TRUE;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000270 }
271 cinfo->err->trace_level++;
272
DRC9665f5e2014-11-22 04:04:38 +0000273 } else if (keymatch(arg, "version", 4)) {
274 fprintf(stderr, "%s version %s (build %s)\n",
275 PACKAGE_NAME, VERSION, BUILD);
DRC306add82014-11-22 04:25:04 +0000276 exit(EXIT_SUCCESS);
DRC9665f5e2014-11-22 04:04:38 +0000277
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000278 } else if (keymatch(arg, "fast", 1)) {
279 /* Select recommended processing options for quick-and-dirty output. */
280 cinfo->two_pass_quantize = FALSE;
281 cinfo->dither_mode = JDITHER_ORDERED;
282 if (! cinfo->quantize_colors) /* don't override an earlier -colors */
DRCe5eaf372014-05-09 18:00:32 +0000283 cinfo->desired_number_of_colors = 216;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000284 cinfo->dct_method = JDCT_FASTEST;
285 cinfo->do_fancy_upsampling = FALSE;
286
287 } else if (keymatch(arg, "gif", 1)) {
288 /* GIF output format. */
289 requested_fmt = FMT_GIF;
290
291 } else if (keymatch(arg, "grayscale", 2) || keymatch(arg, "greyscale",2)) {
292 /* Force monochrome output. */
293 cinfo->out_color_space = JCS_GRAYSCALE;
294
DRC6a833a82011-03-03 16:58:47 +0000295 } else if (keymatch(arg, "rgb", 2)) {
296 /* Force RGB output. */
297 cinfo->out_color_space = JCS_RGB;
298
DRC78df2e62014-05-12 09:23:57 +0000299 } else if (keymatch(arg, "rgb565", 2)) {
300 /* Force RGB565 output. */
301 cinfo->out_color_space = JCS_RGB565;
302
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000303 } else if (keymatch(arg, "map", 3)) {
304 /* Quantize to a color map taken from an input file. */
DRCe5eaf372014-05-09 18:00:32 +0000305 if (++argn >= argc) /* advance to next argument */
306 usage();
307 if (for_real) { /* too expensive to do twice! */
308#ifdef QUANT_2PASS_SUPPORTED /* otherwise can't quantize to supplied map */
309 FILE * mapfile;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000310
DRCe5eaf372014-05-09 18:00:32 +0000311 if ((mapfile = fopen(argv[argn], READ_BINARY)) == NULL) {
312 fprintf(stderr, "%s: can't open %s\n", progname, argv[argn]);
313 exit(EXIT_FAILURE);
314 }
315 read_color_map(cinfo, mapfile);
316 fclose(mapfile);
317 cinfo->quantize_colors = TRUE;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000318#else
DRCe5eaf372014-05-09 18:00:32 +0000319 ERREXIT(cinfo, JERR_NOT_COMPILED);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000320#endif
321 }
322
323 } else if (keymatch(arg, "maxmemory", 3)) {
324 /* Maximum memory in Kb (or Mb with 'm'). */
325 long lval;
326 char ch = 'x';
327
DRCe5eaf372014-05-09 18:00:32 +0000328 if (++argn >= argc) /* advance to next argument */
329 usage();
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000330 if (sscanf(argv[argn], "%ld%c", &lval, &ch) < 1)
DRCe5eaf372014-05-09 18:00:32 +0000331 usage();
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000332 if (ch == 'm' || ch == 'M')
DRCe5eaf372014-05-09 18:00:32 +0000333 lval *= 1000L;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000334 cinfo->mem->max_memory_to_use = lval * 1000L;
335
336 } else if (keymatch(arg, "nosmooth", 3)) {
337 /* Suppress fancy upsampling */
338 cinfo->do_fancy_upsampling = FALSE;
339
340 } else if (keymatch(arg, "onepass", 3)) {
341 /* Use fast one-pass quantization. */
342 cinfo->two_pass_quantize = FALSE;
343
344 } else if (keymatch(arg, "os2", 3)) {
345 /* BMP output format (OS/2 flavor). */
346 requested_fmt = FMT_OS2;
347
348 } else if (keymatch(arg, "outfile", 4)) {
349 /* Set output file name. */
DRCe5eaf372014-05-09 18:00:32 +0000350 if (++argn >= argc) /* advance to next argument */
351 usage();
352 outfilename = argv[argn]; /* save it away for later use */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000353
DRCab706232013-01-18 23:42:31 +0000354 } else if (keymatch(arg, "memsrc", 2)) {
355 /* Use in-memory source manager */
356#if JPEG_LIB_VERSION >= 80 || defined(MEM_SRCDST_SUPPORTED)
357 memsrc = TRUE;
358#else
359 fprintf(stderr, "%s: sorry, in-memory source manager was not compiled in\n",
360 progname);
361 exit(EXIT_FAILURE);
362#endif
363
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000364 } else if (keymatch(arg, "pnm", 1) || keymatch(arg, "ppm", 1)) {
365 /* PPM/PGM output format. */
366 requested_fmt = FMT_PPM;
367
368 } else if (keymatch(arg, "rle", 1)) {
369 /* RLE output format. */
370 requested_fmt = FMT_RLE;
371
DRCeb32cc12015-06-25 03:44:36 +0000372 } else if (keymatch(arg, "scale", 2)) {
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000373 /* Scale the output image by a fraction M/N. */
DRCe5eaf372014-05-09 18:00:32 +0000374 if (++argn >= argc) /* advance to next argument */
375 usage();
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000376 if (sscanf(argv[argn], "%d/%d",
DRCe5eaf372014-05-09 18:00:32 +0000377 &cinfo->scale_num, &cinfo->scale_denom) != 2)
378 usage();
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000379
DRC7a7da942015-06-27 08:10:31 +0000380 } else if (keymatch(arg, "strip", 2)) {
DRCeb32cc12015-06-25 03:44:36 +0000381 if (++argn >= argc)
382 usage();
383 if (sscanf(argv[argn], "%d,%d", &startY, &endY) != 2 || startY > endY)
384 usage();
DRC7a7da942015-06-27 08:10:31 +0000385 strip = TRUE;
DRCeb32cc12015-06-25 03:44:36 +0000386
DRC07afe8a2015-06-25 03:44:37 +0000387
388 } else if (keymatch(arg, "skip", 2)) {
389 if (++argn >= argc)
390 usage();
391 if (sscanf(argv[argn], "%d,%d", &startY, &endY) != 2 || startY > endY)
392 usage();
393 skip = TRUE;
394
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000395 } else if (keymatch(arg, "targa", 1)) {
396 /* Targa output format. */
397 requested_fmt = FMT_TARGA;
398
399 } else {
DRCe5eaf372014-05-09 18:00:32 +0000400 usage(); /* bogus switch */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000401 }
402 }
403
DRCe5eaf372014-05-09 18:00:32 +0000404 return argn; /* return index of next arg (file name) */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000405}
406
407
408/*
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000409 * Marker processor for COM and interesting APPn markers.
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000410 * This replaces the library's built-in processor, which just skips the marker.
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000411 * We want to print out the marker as text, to the extent possible.
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000412 * Note this code relies on a non-suspending data source.
413 */
414
Thomas G. Lane489583f1996-02-07 00:00:00 +0000415LOCAL(unsigned int)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000416jpeg_getc (j_decompress_ptr cinfo)
417/* Read next byte */
418{
419 struct jpeg_source_mgr * datasrc = cinfo->src;
420
421 if (datasrc->bytes_in_buffer == 0) {
422 if (! (*datasrc->fill_input_buffer) (cinfo))
423 ERREXIT(cinfo, JERR_CANT_SUSPEND);
424 }
425 datasrc->bytes_in_buffer--;
426 return GETJOCTET(*datasrc->next_input_byte++);
427}
428
429
Thomas G. Lane489583f1996-02-07 00:00:00 +0000430METHODDEF(boolean)
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000431print_text_marker (j_decompress_ptr cinfo)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000432{
433 boolean traceit = (cinfo->err->trace_level >= 1);
DRC1e32fe32015-10-14 17:32:39 -0500434 long length;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000435 unsigned int ch;
436 unsigned int lastch = 0;
437
438 length = jpeg_getc(cinfo) << 8;
439 length += jpeg_getc(cinfo);
DRCe5eaf372014-05-09 18:00:32 +0000440 length -= 2; /* discount the length word itself */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000441
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000442 if (traceit) {
443 if (cinfo->unread_marker == JPEG_COM)
444 fprintf(stderr, "Comment, length %ld:\n", (long) length);
DRCe5eaf372014-05-09 18:00:32 +0000445 else /* assume it is an APPn otherwise */
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000446 fprintf(stderr, "APP%d, length %ld:\n",
DRCe5eaf372014-05-09 18:00:32 +0000447 cinfo->unread_marker - JPEG_APP0, (long) length);
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000448 }
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000449
450 while (--length >= 0) {
451 ch = jpeg_getc(cinfo);
452 if (traceit) {
453 /* Emit the character in a readable form.
454 * Nonprintables are converted to \nnn form,
455 * while \ is converted to \\.
456 * Newlines in CR, CR/LF, or LF form will be printed as one newline.
457 */
458 if (ch == '\r') {
DRCe5eaf372014-05-09 18:00:32 +0000459 fprintf(stderr, "\n");
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000460 } else if (ch == '\n') {
DRCe5eaf372014-05-09 18:00:32 +0000461 if (lastch != '\r')
462 fprintf(stderr, "\n");
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000463 } else if (ch == '\\') {
DRCe5eaf372014-05-09 18:00:32 +0000464 fprintf(stderr, "\\\\");
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000465 } else if (isprint(ch)) {
DRCe5eaf372014-05-09 18:00:32 +0000466 putc(ch, stderr);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000467 } else {
DRCe5eaf372014-05-09 18:00:32 +0000468 fprintf(stderr, "\\%03o", ch);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000469 }
470 lastch = ch;
471 }
472 }
473
474 if (traceit)
475 fprintf(stderr, "\n");
476
477 return TRUE;
478}
479
480
481/*
482 * The main program.
483 */
484
Thomas G. Lane489583f1996-02-07 00:00:00 +0000485int
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000486main (int argc, char **argv)
487{
488 struct jpeg_decompress_struct cinfo;
489 struct jpeg_error_mgr jerr;
490#ifdef PROGRESS_REPORT
491 struct cdjpeg_progress_mgr progress;
492#endif
493 int file_index;
494 djpeg_dest_ptr dest_mgr = NULL;
495 FILE * input_file;
496 FILE * output_file;
DRCab706232013-01-18 23:42:31 +0000497 unsigned char *inbuffer = NULL;
498 unsigned long insize = 0;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000499 JDIMENSION num_scanlines;
500
501 /* On Mac, fetch a command line. */
502#ifdef USE_CCOMMAND
503 argc = ccommand(&argv);
504#endif
505
506 progname = argv[0];
507 if (progname == NULL || progname[0] == 0)
DRCe5eaf372014-05-09 18:00:32 +0000508 progname = "djpeg"; /* in case C library doesn't provide it */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000509
510 /* Initialize the JPEG decompression object with default error handling. */
511 cinfo.err = jpeg_std_error(&jerr);
512 jpeg_create_decompress(&cinfo);
513 /* Add some application-specific error messages (from cderror.h) */
Thomas G. Lane9ba2f5e1994-12-07 00:00:00 +0000514 jerr.addon_message_table = cdjpeg_message_table;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000515 jerr.first_addon_message = JMSG_FIRSTADDONCODE;
516 jerr.last_addon_message = JMSG_LASTADDONCODE;
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000517
518 /* Insert custom marker processor for COM and APP12.
519 * APP12 is used by some digital camera makers for textual info,
520 * so we provide the ability to display it as text.
521 * If you like, additional APPn marker types can be selected for display,
DRC39ea5622010-10-12 01:55:31 +0000522 * but don't try to override APP0 or APP14 this way (see libjpeg.txt).
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000523 */
524 jpeg_set_marker_processor(&cinfo, JPEG_COM, print_text_marker);
525 jpeg_set_marker_processor(&cinfo, JPEG_APP0+12, print_text_marker);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000526
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000527 /* Scan command line to find file names. */
528 /* It is convenient to use just one switch-parsing routine, but the switch
529 * values read here are ignored; we will rescan the switches after opening
530 * the input file.
531 * (Exception: tracing level set here controls verbosity for COM markers
532 * found during jpeg_read_header...)
533 */
534
535 file_index = parse_switches(&cinfo, argc, argv, 0, FALSE);
536
537#ifdef TWO_FILE_COMMANDLINE
538 /* Must have either -outfile switch or explicit output file name */
539 if (outfilename == NULL) {
540 if (file_index != argc-2) {
541 fprintf(stderr, "%s: must name one input and one output file\n",
DRCe5eaf372014-05-09 18:00:32 +0000542 progname);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000543 usage();
544 }
545 outfilename = argv[file_index+1];
546 } else {
547 if (file_index != argc-1) {
548 fprintf(stderr, "%s: must name one input and one output file\n",
DRCe5eaf372014-05-09 18:00:32 +0000549 progname);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000550 usage();
551 }
552 }
553#else
554 /* Unix style: expect zero or one file name */
555 if (file_index < argc-1) {
556 fprintf(stderr, "%s: only one input file\n", progname);
557 usage();
558 }
559#endif /* TWO_FILE_COMMANDLINE */
560
561 /* Open the input file. */
562 if (file_index < argc) {
563 if ((input_file = fopen(argv[file_index], READ_BINARY)) == NULL) {
564 fprintf(stderr, "%s: can't open %s\n", progname, argv[file_index]);
565 exit(EXIT_FAILURE);
566 }
567 } else {
568 /* default input file is stdin */
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000569 input_file = read_stdin();
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000570 }
571
572 /* Open the output file. */
573 if (outfilename != NULL) {
574 if ((output_file = fopen(outfilename, WRITE_BINARY)) == NULL) {
575 fprintf(stderr, "%s: can't open %s\n", progname, outfilename);
576 exit(EXIT_FAILURE);
577 }
578 } else {
579 /* default output file is stdout */
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000580 output_file = write_stdout();
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000581 }
582
583#ifdef PROGRESS_REPORT
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000584 start_progress_monitor((j_common_ptr) &cinfo, &progress);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000585#endif
586
587 /* Specify data source for decompression */
DRCab706232013-01-18 23:42:31 +0000588#if JPEG_LIB_VERSION >= 80 || defined(MEM_SRCDST_SUPPORTED)
589 if (memsrc) {
590 size_t nbytes;
591 do {
592 inbuffer = (unsigned char *)realloc(inbuffer, insize + INPUT_BUF_SIZE);
593 if (inbuffer == NULL) {
594 fprintf(stderr, "%s: memory allocation failure\n", progname);
595 exit(EXIT_FAILURE);
596 }
597 nbytes = JFREAD(input_file, &inbuffer[insize], INPUT_BUF_SIZE);
DRCc45653e2013-10-12 21:52:48 +0000598 if (nbytes < INPUT_BUF_SIZE && ferror(input_file)) {
DRCab706232013-01-18 23:42:31 +0000599 if (file_index < argc)
600 fprintf(stderr, "%s: can't read from %s\n", progname,
601 argv[file_index]);
602 else
603 fprintf(stderr, "%s: can't read from stdin\n", progname);
604 }
DRC736fb062013-01-18 23:45:06 +0000605 insize += (unsigned long)nbytes;
DRCab706232013-01-18 23:42:31 +0000606 } while (nbytes == INPUT_BUF_SIZE);
607 fprintf(stderr, "Compressed size: %lu bytes\n", insize);
608 jpeg_mem_src(&cinfo, inbuffer, insize);
609 } else
610#endif
611 jpeg_stdio_src(&cinfo, input_file);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000612
613 /* Read file header, set default decompression parameters */
614 (void) jpeg_read_header(&cinfo, TRUE);
615
616 /* Adjust default decompression parameters by re-parsing the options */
617 file_index = parse_switches(&cinfo, argc, argv, 0, TRUE);
618
619 /* Initialize the output module now to let it override any crucial
620 * option settings (for instance, GIF wants to force color quantization).
621 */
622 switch (requested_fmt) {
623#ifdef BMP_SUPPORTED
624 case FMT_BMP:
625 dest_mgr = jinit_write_bmp(&cinfo, FALSE);
626 break;
627 case FMT_OS2:
628 dest_mgr = jinit_write_bmp(&cinfo, TRUE);
629 break;
630#endif
631#ifdef GIF_SUPPORTED
632 case FMT_GIF:
633 dest_mgr = jinit_write_gif(&cinfo);
634 break;
635#endif
636#ifdef PPM_SUPPORTED
637 case FMT_PPM:
638 dest_mgr = jinit_write_ppm(&cinfo);
639 break;
640#endif
641#ifdef RLE_SUPPORTED
642 case FMT_RLE:
643 dest_mgr = jinit_write_rle(&cinfo);
644 break;
645#endif
646#ifdef TARGA_SUPPORTED
647 case FMT_TARGA:
648 dest_mgr = jinit_write_targa(&cinfo);
649 break;
650#endif
651 default:
652 ERREXIT(&cinfo, JERR_UNSUPPORTED_FORMAT);
653 break;
654 }
655 dest_mgr->output_file = output_file;
656
657 /* Start decompressor */
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000658 (void) jpeg_start_decompress(&cinfo);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000659
DRC7a7da942015-06-27 08:10:31 +0000660 /* Strip decode */
661 if (strip || skip) {
DRCeb32cc12015-06-25 03:44:36 +0000662 JDIMENSION tmp;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000663
DRCeb32cc12015-06-25 03:44:36 +0000664 /* Check for valid endY. We cannot check this value until after
665 * jpeg_start_decompress() is called. Note that we have already verified
666 * that startY <= endY.
667 */
DRC7a7da942015-06-27 08:10:31 +0000668 if (endY > cinfo.output_height - 1) {
669 fprintf(stderr, "%s: strip %d-%d exceeds image height %d\n", progname,
DRCeb32cc12015-06-25 03:44:36 +0000670 startY, endY, cinfo.output_height);
671 exit(EXIT_FAILURE);
672 }
673
674 /* Write output file header. This is a hack to ensure that the destination
675 * manager creates an image of the proper size for the partial decode.
676 */
677 tmp = cinfo.output_height;
DRC7a7da942015-06-27 08:10:31 +0000678 cinfo.output_height = endY - startY + 1;
DRC07afe8a2015-06-25 03:44:37 +0000679 if (skip)
680 cinfo.output_height = tmp - cinfo.output_height;
DRCeb32cc12015-06-25 03:44:36 +0000681 (*dest_mgr->start_output) (&cinfo, dest_mgr);
682 cinfo.output_height = tmp;
683
684 /* Process data */
DRC07afe8a2015-06-25 03:44:37 +0000685 if (skip) {
686 while (cinfo.output_scanline < startY) {
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 }
DRC7a7da942015-06-27 08:10:31 +0000691 jpeg_skip_scanlines(&cinfo, endY - startY + 1);
DRC07afe8a2015-06-25 03:44:37 +0000692 while (cinfo.output_scanline < cinfo.output_height) {
693 num_scanlines = jpeg_read_scanlines(&cinfo, dest_mgr->buffer,
694 dest_mgr->buffer_height);
695 (*dest_mgr->put_pixel_rows) (&cinfo, dest_mgr, num_scanlines);
696 }
697 } else {
698 jpeg_skip_scanlines(&cinfo, startY);
DRC7a7da942015-06-27 08:10:31 +0000699 while (cinfo.output_scanline <= endY) {
DRC07afe8a2015-06-25 03:44:37 +0000700 num_scanlines = jpeg_read_scanlines(&cinfo, dest_mgr->buffer,
701 dest_mgr->buffer_height);
702 (*dest_mgr->put_pixel_rows) (&cinfo, dest_mgr, num_scanlines);
703 }
DRC7a7da942015-06-27 08:10:31 +0000704 jpeg_skip_scanlines(&cinfo, cinfo.output_height - endY + 1);
DRCeb32cc12015-06-25 03:44:36 +0000705 }
DRCeb32cc12015-06-25 03:44:36 +0000706
707 /* Normal full image decode */
708 } else {
709 /* Write output file header */
710 (*dest_mgr->start_output) (&cinfo, dest_mgr);
711
712 /* Process data */
713 while (cinfo.output_scanline < cinfo.output_height) {
714 num_scanlines = jpeg_read_scanlines(&cinfo, dest_mgr->buffer,
715 dest_mgr->buffer_height);
716 (*dest_mgr->put_pixel_rows) (&cinfo, dest_mgr, num_scanlines);
717 }
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000718 }
719
720#ifdef PROGRESS_REPORT
721 /* Hack: count final pass as done in case finish_output does an extra pass.
722 * The library won't have updated completed_passes.
723 */
724 progress.pub.completed_passes = progress.pub.total_passes;
725#endif
726
727 /* Finish decompression and release memory.
728 * I must do it in this order because output module has allocated memory
729 * of lifespan JPOOL_IMAGE; it needs to finish before releasing memory.
730 */
731 (*dest_mgr->finish_output) (&cinfo, dest_mgr);
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000732 (void) jpeg_finish_decompress(&cinfo);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000733 jpeg_destroy_decompress(&cinfo);
734
Thomas G. Lane9ba2f5e1994-12-07 00:00:00 +0000735 /* Close files, if we opened them */
736 if (input_file != stdin)
737 fclose(input_file);
738 if (output_file != stdout)
739 fclose(output_file);
740
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000741#ifdef PROGRESS_REPORT
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000742 end_progress_monitor((j_common_ptr) &cinfo);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000743#endif
744
DRCab706232013-01-18 23:42:31 +0000745 if (memsrc && inbuffer != NULL)
746 free(inbuffer);
747
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000748 /* All done. */
749 exit(jerr.num_warnings ? EXIT_WARNING : EXIT_SUCCESS);
DRCe5eaf372014-05-09 18:00:32 +0000750 return 0; /* suppress no-return-value warnings */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000751}