blob: 7c63f250163bac2a37ff26327d7f889b74e7fcd9 [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:
DRC78df2e62014-05-12 09:23:57 +00007 * Copyright (C) 2010-2011, 2013-2014, D. R. Commander.
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +00008 * For conditions of distribution and use, see the accompanying README file.
9 *
10 * This file contains a command-line user interface for the JPEG decompressor.
11 * It should work on any system with Unix- or MS-DOS-style command lines.
12 *
13 * Two different command line styles are permitted, depending on the
14 * compile-time switch TWO_FILE_COMMANDLINE:
DRCe5eaf372014-05-09 18:00:32 +000015 * djpeg [options] inputfile outputfile
16 * djpeg [options] [inputfile]
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000017 * In the second style, output is always to standard output, which you'd
18 * normally redirect to a file or pipe to some other program. Input is
19 * either from a named file or from standard input (typically redirected).
20 * The second style is convenient on Unix but is unhelpful on systems that
21 * don't support pipes. Also, you MUST use the first style if your system
22 * doesn't do binary I/O to stdin/stdout.
23 * To simplify script writing, the "-outfile" switch is provided. The syntax
DRCe5eaf372014-05-09 18:00:32 +000024 * djpeg [options] -outfile outputfile inputfile
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000025 * works regardless of which command line style is used.
26 */
27
DRCe5eaf372014-05-09 18:00:32 +000028#include "cdjpeg.h" /* Common decls for cjpeg/djpeg applications */
29#include "jversion.h" /* for version message */
DRCff6961f2014-04-20 09:17:11 +000030#include "jconfigint.h"
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000031
DRCe5eaf372014-05-09 18:00:32 +000032#include <ctype.h> /* to declare isprint() */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000033
DRCe5eaf372014-05-09 18:00:32 +000034#ifdef USE_CCOMMAND /* command-line reader for Macintosh */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000035#ifdef __MWERKS__
Thomas G. Lane489583f1996-02-07 00:00:00 +000036#include <SIOUX.h> /* Metrowerks needs this */
DRCe5eaf372014-05-09 18:00:32 +000037#include <console.h> /* ... and this */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000038#endif
39#ifdef THINK_C
DRCe5eaf372014-05-09 18:00:32 +000040#include <console.h> /* Think declares it here */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000041#endif
42#endif
43
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000044
Thomas G. Lane9ba2f5e1994-12-07 00:00:00 +000045/* Create the add-on message string table. */
46
DRCe5eaf372014-05-09 18:00:32 +000047#define JMESSAGE(code,string) string ,
Thomas G. Lane9ba2f5e1994-12-07 00:00:00 +000048
49static const char * const cdjpeg_message_table[] = {
50#include "cderror.h"
51 NULL
52};
53
54
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000055/*
56 * This list defines the known output image formats
57 * (not all of which need be supported by a given version).
58 * You can change the default output format by defining DEFAULT_FMT;
59 * indeed, you had better do so if you undefine PPM_SUPPORTED.
60 */
61
62typedef enum {
DRCe5eaf372014-05-09 18:00:32 +000063 FMT_BMP, /* BMP format (Windows flavor) */
64 FMT_GIF, /* GIF format */
65 FMT_OS2, /* BMP format (OS/2 flavor) */
66 FMT_PPM, /* PPM/PGM (PBMPLUS formats) */
67 FMT_RLE, /* RLE format */
68 FMT_TARGA, /* Targa format */
69 FMT_TIFF /* TIFF format */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000070} IMAGE_FORMATS;
71
DRCe5eaf372014-05-09 18:00:32 +000072#ifndef DEFAULT_FMT /* so can override from CFLAGS in Makefile */
73#define DEFAULT_FMT FMT_PPM
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000074#endif
75
76static IMAGE_FORMATS requested_fmt;
77
78
79/*
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000080 * Argument-parsing code.
81 * The switch parser is designed to be useful with DOS-style command line
82 * syntax, ie, intermixed switches and file names, where only the switches
83 * to the left of a given file name affect processing of that file.
84 * The main program in this file doesn't actually use this capability...
85 */
86
87
DRCe5eaf372014-05-09 18:00:32 +000088static const char * progname; /* program name for error messages */
89static char * outfilename; /* for -outfile switch */
DRCab706232013-01-18 23:42:31 +000090boolean memsrc; /* for -memsrc switch */
91#define INPUT_BUF_SIZE 4096
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000092
93
Thomas G. Lane489583f1996-02-07 00:00:00 +000094LOCAL(void)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000095usage (void)
96/* complain about bad command line */
97{
98 fprintf(stderr, "usage: %s [switches] ", progname);
99#ifdef TWO_FILE_COMMANDLINE
100 fprintf(stderr, "inputfile outputfile\n");
101#else
102 fprintf(stderr, "[inputfile]\n");
103#endif
104
105 fprintf(stderr, "Switches (names may be abbreviated):\n");
106 fprintf(stderr, " -colors N Reduce image to no more than N colors\n");
107 fprintf(stderr, " -fast Fast, low-quality processing\n");
108 fprintf(stderr, " -grayscale Force grayscale output\n");
DRC6a833a82011-03-03 16:58:47 +0000109 fprintf(stderr, " -rgb Force RGB output\n");
DRC78df2e62014-05-12 09:23:57 +0000110 fprintf(stderr, " -rgb565 Force RGB565 output\n");
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000111#ifdef IDCT_SCALING_SUPPORTED
112 fprintf(stderr, " -scale M/N Scale output image by fraction M/N, eg, 1/8\n");
113#endif
114#ifdef BMP_SUPPORTED
115 fprintf(stderr, " -bmp Select BMP output format (Windows style)%s\n",
DRCe5eaf372014-05-09 18:00:32 +0000116 (DEFAULT_FMT == FMT_BMP ? " (default)" : ""));
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000117#endif
118#ifdef GIF_SUPPORTED
119 fprintf(stderr, " -gif Select GIF output format%s\n",
DRCe5eaf372014-05-09 18:00:32 +0000120 (DEFAULT_FMT == FMT_GIF ? " (default)" : ""));
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000121#endif
122#ifdef BMP_SUPPORTED
123 fprintf(stderr, " -os2 Select BMP output format (OS/2 style)%s\n",
DRCe5eaf372014-05-09 18:00:32 +0000124 (DEFAULT_FMT == FMT_OS2 ? " (default)" : ""));
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000125#endif
126#ifdef PPM_SUPPORTED
127 fprintf(stderr, " -pnm Select PBMPLUS (PPM/PGM) output format%s\n",
DRCe5eaf372014-05-09 18:00:32 +0000128 (DEFAULT_FMT == FMT_PPM ? " (default)" : ""));
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000129#endif
130#ifdef RLE_SUPPORTED
131 fprintf(stderr, " -rle Select Utah RLE output format%s\n",
DRCe5eaf372014-05-09 18:00:32 +0000132 (DEFAULT_FMT == FMT_RLE ? " (default)" : ""));
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000133#endif
134#ifdef TARGA_SUPPORTED
135 fprintf(stderr, " -targa Select Targa output format%s\n",
DRCe5eaf372014-05-09 18:00:32 +0000136 (DEFAULT_FMT == FMT_TARGA ? " (default)" : ""));
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000137#endif
138 fprintf(stderr, "Switches for advanced users:\n");
139#ifdef DCT_ISLOW_SUPPORTED
140 fprintf(stderr, " -dct int Use integer DCT method%s\n",
DRCe5eaf372014-05-09 18:00:32 +0000141 (JDCT_DEFAULT == JDCT_ISLOW ? " (default)" : ""));
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000142#endif
143#ifdef DCT_IFAST_SUPPORTED
144 fprintf(stderr, " -dct fast Use fast integer DCT (less accurate)%s\n",
DRCe5eaf372014-05-09 18:00:32 +0000145 (JDCT_DEFAULT == JDCT_IFAST ? " (default)" : ""));
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000146#endif
147#ifdef DCT_FLOAT_SUPPORTED
148 fprintf(stderr, " -dct float Use floating-point DCT method%s\n",
DRCe5eaf372014-05-09 18:00:32 +0000149 (JDCT_DEFAULT == JDCT_FLOAT ? " (default)" : ""));
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000150#endif
151 fprintf(stderr, " -dither fs Use F-S dithering (default)\n");
152 fprintf(stderr, " -dither none Don't use dithering in quantization\n");
153 fprintf(stderr, " -dither ordered Use ordered dither (medium speed, quality)\n");
154#ifdef QUANT_2PASS_SUPPORTED
155 fprintf(stderr, " -map FILE Map to colors used in named image file\n");
156#endif
157 fprintf(stderr, " -nosmooth Don't use high-quality upsampling\n");
158#ifdef QUANT_1PASS_SUPPORTED
159 fprintf(stderr, " -onepass Use 1-pass quantization (fast, low quality)\n");
160#endif
161 fprintf(stderr, " -maxmemory N Maximum memory to use (in kbytes)\n");
162 fprintf(stderr, " -outfile name Specify name for output file\n");
DRCab706232013-01-18 23:42:31 +0000163#if JPEG_LIB_VERSION >= 80 || defined(MEM_SRCDST_SUPPORTED)
164 fprintf(stderr, " -memsrc Load input file into memory before decompressing\n");
165#endif
166
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000167 fprintf(stderr, " -verbose or -debug Emit debug output\n");
168 exit(EXIT_FAILURE);
169}
170
171
Thomas G. Lane489583f1996-02-07 00:00:00 +0000172LOCAL(int)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000173parse_switches (j_decompress_ptr cinfo, int argc, char **argv,
DRCe5eaf372014-05-09 18:00:32 +0000174 int last_file_arg_seen, boolean for_real)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000175/* Parse optional switches.
176 * Returns argv[] index of first file-name argument (== argc if none).
177 * Any file names with indexes <= last_file_arg_seen are ignored;
178 * they have presumably been processed in a previous iteration.
179 * (Pass 0 for last_file_arg_seen on the first or only iteration.)
180 * for_real is FALSE on the first (dummy) pass; we may skip any expensive
181 * processing.
182 */
183{
184 int argn;
185 char * arg;
186
187 /* Set up default JPEG parameters. */
DRCe5eaf372014-05-09 18:00:32 +0000188 requested_fmt = DEFAULT_FMT; /* set default output file format */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000189 outfilename = NULL;
DRCab706232013-01-18 23:42:31 +0000190 memsrc = FALSE;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000191 cinfo->err->trace_level = 0;
192
193 /* Scan command line options, adjust parameters */
194
195 for (argn = 1; argn < argc; argn++) {
196 arg = argv[argn];
197 if (*arg != '-') {
198 /* Not a switch, must be a file name argument */
199 if (argn <= last_file_arg_seen) {
DRCe5eaf372014-05-09 18:00:32 +0000200 outfilename = NULL; /* -outfile applies to just one input file */
201 continue; /* ignore this name if previously processed */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000202 }
DRCe5eaf372014-05-09 18:00:32 +0000203 break; /* else done parsing switches */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000204 }
DRCe5eaf372014-05-09 18:00:32 +0000205 arg++; /* advance past switch marker character */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000206
207 if (keymatch(arg, "bmp", 1)) {
208 /* BMP output format. */
209 requested_fmt = FMT_BMP;
210
211 } else if (keymatch(arg, "colors", 1) || keymatch(arg, "colours", 1) ||
DRCe5eaf372014-05-09 18:00:32 +0000212 keymatch(arg, "quantize", 1) || keymatch(arg, "quantise", 1)) {
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000213 /* Do color quantization. */
214 int val;
215
DRCe5eaf372014-05-09 18:00:32 +0000216 if (++argn >= argc) /* advance to next argument */
217 usage();
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000218 if (sscanf(argv[argn], "%d", &val) != 1)
DRCe5eaf372014-05-09 18:00:32 +0000219 usage();
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000220 cinfo->desired_number_of_colors = val;
221 cinfo->quantize_colors = TRUE;
222
223 } else if (keymatch(arg, "dct", 2)) {
224 /* Select IDCT algorithm. */
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 (keymatch(argv[argn], "int", 1)) {
DRCe5eaf372014-05-09 18:00:32 +0000228 cinfo->dct_method = JDCT_ISLOW;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000229 } else if (keymatch(argv[argn], "fast", 2)) {
DRCe5eaf372014-05-09 18:00:32 +0000230 cinfo->dct_method = JDCT_IFAST;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000231 } else if (keymatch(argv[argn], "float", 2)) {
DRCe5eaf372014-05-09 18:00:32 +0000232 cinfo->dct_method = JDCT_FLOAT;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000233 } else
DRCe5eaf372014-05-09 18:00:32 +0000234 usage();
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000235
236 } else if (keymatch(arg, "dither", 2)) {
237 /* Select dithering algorithm. */
DRCe5eaf372014-05-09 18:00:32 +0000238 if (++argn >= argc) /* advance to next argument */
239 usage();
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000240 if (keymatch(argv[argn], "fs", 2)) {
DRCe5eaf372014-05-09 18:00:32 +0000241 cinfo->dither_mode = JDITHER_FS;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000242 } else if (keymatch(argv[argn], "none", 2)) {
DRCe5eaf372014-05-09 18:00:32 +0000243 cinfo->dither_mode = JDITHER_NONE;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000244 } else if (keymatch(argv[argn], "ordered", 2)) {
DRCe5eaf372014-05-09 18:00:32 +0000245 cinfo->dither_mode = JDITHER_ORDERED;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000246 } else
DRCe5eaf372014-05-09 18:00:32 +0000247 usage();
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000248
249 } else if (keymatch(arg, "debug", 1) || keymatch(arg, "verbose", 1)) {
250 /* Enable debug printouts. */
251 /* On first -d, print version identification */
252 static boolean printed_version = FALSE;
253
254 if (! printed_version) {
DRCe5eaf372014-05-09 18:00:32 +0000255 fprintf(stderr, "%s version %s (build %s)\n",
256 PACKAGE_NAME, VERSION, BUILD);
257 fprintf(stderr, "%s\n\n", JCOPYRIGHT);
258 fprintf(stderr, "Emulating The Independent JPEG Group's software, version %s\n\n",
259 JVERSION);
260 printed_version = TRUE;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000261 }
262 cinfo->err->trace_level++;
263
264 } else if (keymatch(arg, "fast", 1)) {
265 /* Select recommended processing options for quick-and-dirty output. */
266 cinfo->two_pass_quantize = FALSE;
267 cinfo->dither_mode = JDITHER_ORDERED;
268 if (! cinfo->quantize_colors) /* don't override an earlier -colors */
DRCe5eaf372014-05-09 18:00:32 +0000269 cinfo->desired_number_of_colors = 216;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000270 cinfo->dct_method = JDCT_FASTEST;
271 cinfo->do_fancy_upsampling = FALSE;
272
273 } else if (keymatch(arg, "gif", 1)) {
274 /* GIF output format. */
275 requested_fmt = FMT_GIF;
276
277 } else if (keymatch(arg, "grayscale", 2) || keymatch(arg, "greyscale",2)) {
278 /* Force monochrome output. */
279 cinfo->out_color_space = JCS_GRAYSCALE;
280
DRC6a833a82011-03-03 16:58:47 +0000281 } else if (keymatch(arg, "rgb", 2)) {
282 /* Force RGB output. */
283 cinfo->out_color_space = JCS_RGB;
284
DRC78df2e62014-05-12 09:23:57 +0000285 } else if (keymatch(arg, "rgb565", 2)) {
286 /* Force RGB565 output. */
287 cinfo->out_color_space = JCS_RGB565;
288
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000289 } else if (keymatch(arg, "map", 3)) {
290 /* Quantize to a color map taken from an input file. */
DRCe5eaf372014-05-09 18:00:32 +0000291 if (++argn >= argc) /* advance to next argument */
292 usage();
293 if (for_real) { /* too expensive to do twice! */
294#ifdef QUANT_2PASS_SUPPORTED /* otherwise can't quantize to supplied map */
295 FILE * mapfile;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000296
DRCe5eaf372014-05-09 18:00:32 +0000297 if ((mapfile = fopen(argv[argn], READ_BINARY)) == NULL) {
298 fprintf(stderr, "%s: can't open %s\n", progname, argv[argn]);
299 exit(EXIT_FAILURE);
300 }
301 read_color_map(cinfo, mapfile);
302 fclose(mapfile);
303 cinfo->quantize_colors = TRUE;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000304#else
DRCe5eaf372014-05-09 18:00:32 +0000305 ERREXIT(cinfo, JERR_NOT_COMPILED);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000306#endif
307 }
308
309 } else if (keymatch(arg, "maxmemory", 3)) {
310 /* Maximum memory in Kb (or Mb with 'm'). */
311 long lval;
312 char ch = 'x';
313
DRCe5eaf372014-05-09 18:00:32 +0000314 if (++argn >= argc) /* advance to next argument */
315 usage();
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000316 if (sscanf(argv[argn], "%ld%c", &lval, &ch) < 1)
DRCe5eaf372014-05-09 18:00:32 +0000317 usage();
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000318 if (ch == 'm' || ch == 'M')
DRCe5eaf372014-05-09 18:00:32 +0000319 lval *= 1000L;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000320 cinfo->mem->max_memory_to_use = lval * 1000L;
321
322 } else if (keymatch(arg, "nosmooth", 3)) {
323 /* Suppress fancy upsampling */
324 cinfo->do_fancy_upsampling = FALSE;
325
326 } else if (keymatch(arg, "onepass", 3)) {
327 /* Use fast one-pass quantization. */
328 cinfo->two_pass_quantize = FALSE;
329
330 } else if (keymatch(arg, "os2", 3)) {
331 /* BMP output format (OS/2 flavor). */
332 requested_fmt = FMT_OS2;
333
334 } else if (keymatch(arg, "outfile", 4)) {
335 /* Set output file name. */
DRCe5eaf372014-05-09 18:00:32 +0000336 if (++argn >= argc) /* advance to next argument */
337 usage();
338 outfilename = argv[argn]; /* save it away for later use */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000339
DRCab706232013-01-18 23:42:31 +0000340 } else if (keymatch(arg, "memsrc", 2)) {
341 /* Use in-memory source manager */
342#if JPEG_LIB_VERSION >= 80 || defined(MEM_SRCDST_SUPPORTED)
343 memsrc = TRUE;
344#else
345 fprintf(stderr, "%s: sorry, in-memory source manager was not compiled in\n",
346 progname);
347 exit(EXIT_FAILURE);
348#endif
349
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000350 } else if (keymatch(arg, "pnm", 1) || keymatch(arg, "ppm", 1)) {
351 /* PPM/PGM output format. */
352 requested_fmt = FMT_PPM;
353
354 } else if (keymatch(arg, "rle", 1)) {
355 /* RLE output format. */
356 requested_fmt = FMT_RLE;
357
358 } else if (keymatch(arg, "scale", 1)) {
359 /* Scale the output image by a fraction M/N. */
DRCe5eaf372014-05-09 18:00:32 +0000360 if (++argn >= argc) /* advance to next argument */
361 usage();
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000362 if (sscanf(argv[argn], "%d/%d",
DRCe5eaf372014-05-09 18:00:32 +0000363 &cinfo->scale_num, &cinfo->scale_denom) != 2)
364 usage();
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000365
366 } else if (keymatch(arg, "targa", 1)) {
367 /* Targa output format. */
368 requested_fmt = FMT_TARGA;
369
370 } else {
DRCe5eaf372014-05-09 18:00:32 +0000371 usage(); /* bogus switch */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000372 }
373 }
374
DRCe5eaf372014-05-09 18:00:32 +0000375 return argn; /* return index of next arg (file name) */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000376}
377
378
379/*
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000380 * Marker processor for COM and interesting APPn markers.
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000381 * This replaces the library's built-in processor, which just skips the marker.
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000382 * We want to print out the marker as text, to the extent possible.
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000383 * Note this code relies on a non-suspending data source.
384 */
385
Thomas G. Lane489583f1996-02-07 00:00:00 +0000386LOCAL(unsigned int)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000387jpeg_getc (j_decompress_ptr cinfo)
388/* Read next byte */
389{
390 struct jpeg_source_mgr * datasrc = cinfo->src;
391
392 if (datasrc->bytes_in_buffer == 0) {
393 if (! (*datasrc->fill_input_buffer) (cinfo))
394 ERREXIT(cinfo, JERR_CANT_SUSPEND);
395 }
396 datasrc->bytes_in_buffer--;
397 return GETJOCTET(*datasrc->next_input_byte++);
398}
399
400
Thomas G. Lane489583f1996-02-07 00:00:00 +0000401METHODDEF(boolean)
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000402print_text_marker (j_decompress_ptr cinfo)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000403{
404 boolean traceit = (cinfo->err->trace_level >= 1);
405 INT32 length;
406 unsigned int ch;
407 unsigned int lastch = 0;
408
409 length = jpeg_getc(cinfo) << 8;
410 length += jpeg_getc(cinfo);
DRCe5eaf372014-05-09 18:00:32 +0000411 length -= 2; /* discount the length word itself */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000412
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000413 if (traceit) {
414 if (cinfo->unread_marker == JPEG_COM)
415 fprintf(stderr, "Comment, length %ld:\n", (long) length);
DRCe5eaf372014-05-09 18:00:32 +0000416 else /* assume it is an APPn otherwise */
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000417 fprintf(stderr, "APP%d, length %ld:\n",
DRCe5eaf372014-05-09 18:00:32 +0000418 cinfo->unread_marker - JPEG_APP0, (long) length);
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000419 }
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000420
421 while (--length >= 0) {
422 ch = jpeg_getc(cinfo);
423 if (traceit) {
424 /* Emit the character in a readable form.
425 * Nonprintables are converted to \nnn form,
426 * while \ is converted to \\.
427 * Newlines in CR, CR/LF, or LF form will be printed as one newline.
428 */
429 if (ch == '\r') {
DRCe5eaf372014-05-09 18:00:32 +0000430 fprintf(stderr, "\n");
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000431 } else if (ch == '\n') {
DRCe5eaf372014-05-09 18:00:32 +0000432 if (lastch != '\r')
433 fprintf(stderr, "\n");
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000434 } else if (ch == '\\') {
DRCe5eaf372014-05-09 18:00:32 +0000435 fprintf(stderr, "\\\\");
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000436 } else if (isprint(ch)) {
DRCe5eaf372014-05-09 18:00:32 +0000437 putc(ch, stderr);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000438 } else {
DRCe5eaf372014-05-09 18:00:32 +0000439 fprintf(stderr, "\\%03o", ch);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000440 }
441 lastch = ch;
442 }
443 }
444
445 if (traceit)
446 fprintf(stderr, "\n");
447
448 return TRUE;
449}
450
451
452/*
453 * The main program.
454 */
455
Thomas G. Lane489583f1996-02-07 00:00:00 +0000456int
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000457main (int argc, char **argv)
458{
459 struct jpeg_decompress_struct cinfo;
460 struct jpeg_error_mgr jerr;
461#ifdef PROGRESS_REPORT
462 struct cdjpeg_progress_mgr progress;
463#endif
464 int file_index;
465 djpeg_dest_ptr dest_mgr = NULL;
466 FILE * input_file;
467 FILE * output_file;
DRCab706232013-01-18 23:42:31 +0000468 unsigned char *inbuffer = NULL;
469 unsigned long insize = 0;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000470 JDIMENSION num_scanlines;
471
472 /* On Mac, fetch a command line. */
473#ifdef USE_CCOMMAND
474 argc = ccommand(&argv);
475#endif
476
477 progname = argv[0];
478 if (progname == NULL || progname[0] == 0)
DRCe5eaf372014-05-09 18:00:32 +0000479 progname = "djpeg"; /* in case C library doesn't provide it */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000480
481 /* Initialize the JPEG decompression object with default error handling. */
482 cinfo.err = jpeg_std_error(&jerr);
483 jpeg_create_decompress(&cinfo);
484 /* Add some application-specific error messages (from cderror.h) */
Thomas G. Lane9ba2f5e1994-12-07 00:00:00 +0000485 jerr.addon_message_table = cdjpeg_message_table;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000486 jerr.first_addon_message = JMSG_FIRSTADDONCODE;
487 jerr.last_addon_message = JMSG_LASTADDONCODE;
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000488
489 /* Insert custom marker processor for COM and APP12.
490 * APP12 is used by some digital camera makers for textual info,
491 * so we provide the ability to display it as text.
492 * If you like, additional APPn marker types can be selected for display,
DRC39ea5622010-10-12 01:55:31 +0000493 * but don't try to override APP0 or APP14 this way (see libjpeg.txt).
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000494 */
495 jpeg_set_marker_processor(&cinfo, JPEG_COM, print_text_marker);
496 jpeg_set_marker_processor(&cinfo, JPEG_APP0+12, print_text_marker);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000497
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000498 /* Scan command line to find file names. */
499 /* It is convenient to use just one switch-parsing routine, but the switch
500 * values read here are ignored; we will rescan the switches after opening
501 * the input file.
502 * (Exception: tracing level set here controls verbosity for COM markers
503 * found during jpeg_read_header...)
504 */
505
506 file_index = parse_switches(&cinfo, argc, argv, 0, FALSE);
507
508#ifdef TWO_FILE_COMMANDLINE
509 /* Must have either -outfile switch or explicit output file name */
510 if (outfilename == NULL) {
511 if (file_index != argc-2) {
512 fprintf(stderr, "%s: must name one input and one output file\n",
DRCe5eaf372014-05-09 18:00:32 +0000513 progname);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000514 usage();
515 }
516 outfilename = argv[file_index+1];
517 } else {
518 if (file_index != argc-1) {
519 fprintf(stderr, "%s: must name one input and one output file\n",
DRCe5eaf372014-05-09 18:00:32 +0000520 progname);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000521 usage();
522 }
523 }
524#else
525 /* Unix style: expect zero or one file name */
526 if (file_index < argc-1) {
527 fprintf(stderr, "%s: only one input file\n", progname);
528 usage();
529 }
530#endif /* TWO_FILE_COMMANDLINE */
531
532 /* Open the input file. */
533 if (file_index < argc) {
534 if ((input_file = fopen(argv[file_index], READ_BINARY)) == NULL) {
535 fprintf(stderr, "%s: can't open %s\n", progname, argv[file_index]);
536 exit(EXIT_FAILURE);
537 }
538 } else {
539 /* default input file is stdin */
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000540 input_file = read_stdin();
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000541 }
542
543 /* Open the output file. */
544 if (outfilename != NULL) {
545 if ((output_file = fopen(outfilename, WRITE_BINARY)) == NULL) {
546 fprintf(stderr, "%s: can't open %s\n", progname, outfilename);
547 exit(EXIT_FAILURE);
548 }
549 } else {
550 /* default output file is stdout */
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000551 output_file = write_stdout();
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000552 }
553
554#ifdef PROGRESS_REPORT
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000555 start_progress_monitor((j_common_ptr) &cinfo, &progress);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000556#endif
557
558 /* Specify data source for decompression */
DRCab706232013-01-18 23:42:31 +0000559#if JPEG_LIB_VERSION >= 80 || defined(MEM_SRCDST_SUPPORTED)
560 if (memsrc) {
561 size_t nbytes;
562 do {
563 inbuffer = (unsigned char *)realloc(inbuffer, insize + INPUT_BUF_SIZE);
564 if (inbuffer == NULL) {
565 fprintf(stderr, "%s: memory allocation failure\n", progname);
566 exit(EXIT_FAILURE);
567 }
568 nbytes = JFREAD(input_file, &inbuffer[insize], INPUT_BUF_SIZE);
DRCc45653e2013-10-12 21:52:48 +0000569 if (nbytes < INPUT_BUF_SIZE && ferror(input_file)) {
DRCab706232013-01-18 23:42:31 +0000570 if (file_index < argc)
571 fprintf(stderr, "%s: can't read from %s\n", progname,
572 argv[file_index]);
573 else
574 fprintf(stderr, "%s: can't read from stdin\n", progname);
575 }
DRC736fb062013-01-18 23:45:06 +0000576 insize += (unsigned long)nbytes;
DRCab706232013-01-18 23:42:31 +0000577 } while (nbytes == INPUT_BUF_SIZE);
578 fprintf(stderr, "Compressed size: %lu bytes\n", insize);
579 jpeg_mem_src(&cinfo, inbuffer, insize);
580 } else
581#endif
582 jpeg_stdio_src(&cinfo, input_file);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000583
584 /* Read file header, set default decompression parameters */
585 (void) jpeg_read_header(&cinfo, TRUE);
586
587 /* Adjust default decompression parameters by re-parsing the options */
588 file_index = parse_switches(&cinfo, argc, argv, 0, TRUE);
589
590 /* Initialize the output module now to let it override any crucial
591 * option settings (for instance, GIF wants to force color quantization).
592 */
593 switch (requested_fmt) {
594#ifdef BMP_SUPPORTED
595 case FMT_BMP:
596 dest_mgr = jinit_write_bmp(&cinfo, FALSE);
597 break;
598 case FMT_OS2:
599 dest_mgr = jinit_write_bmp(&cinfo, TRUE);
600 break;
601#endif
602#ifdef GIF_SUPPORTED
603 case FMT_GIF:
604 dest_mgr = jinit_write_gif(&cinfo);
605 break;
606#endif
607#ifdef PPM_SUPPORTED
608 case FMT_PPM:
609 dest_mgr = jinit_write_ppm(&cinfo);
610 break;
611#endif
612#ifdef RLE_SUPPORTED
613 case FMT_RLE:
614 dest_mgr = jinit_write_rle(&cinfo);
615 break;
616#endif
617#ifdef TARGA_SUPPORTED
618 case FMT_TARGA:
619 dest_mgr = jinit_write_targa(&cinfo);
620 break;
621#endif
622 default:
623 ERREXIT(&cinfo, JERR_UNSUPPORTED_FORMAT);
624 break;
625 }
626 dest_mgr->output_file = output_file;
627
628 /* Start decompressor */
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000629 (void) jpeg_start_decompress(&cinfo);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000630
631 /* Write output file header */
632 (*dest_mgr->start_output) (&cinfo, dest_mgr);
633
634 /* Process data */
635 while (cinfo.output_scanline < cinfo.output_height) {
636 num_scanlines = jpeg_read_scanlines(&cinfo, dest_mgr->buffer,
DRCe5eaf372014-05-09 18:00:32 +0000637 dest_mgr->buffer_height);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000638 (*dest_mgr->put_pixel_rows) (&cinfo, dest_mgr, num_scanlines);
639 }
640
641#ifdef PROGRESS_REPORT
642 /* Hack: count final pass as done in case finish_output does an extra pass.
643 * The library won't have updated completed_passes.
644 */
645 progress.pub.completed_passes = progress.pub.total_passes;
646#endif
647
648 /* Finish decompression and release memory.
649 * I must do it in this order because output module has allocated memory
650 * of lifespan JPOOL_IMAGE; it needs to finish before releasing memory.
651 */
652 (*dest_mgr->finish_output) (&cinfo, dest_mgr);
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000653 (void) jpeg_finish_decompress(&cinfo);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000654 jpeg_destroy_decompress(&cinfo);
655
Thomas G. Lane9ba2f5e1994-12-07 00:00:00 +0000656 /* Close files, if we opened them */
657 if (input_file != stdin)
658 fclose(input_file);
659 if (output_file != stdout)
660 fclose(output_file);
661
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000662#ifdef PROGRESS_REPORT
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000663 end_progress_monitor((j_common_ptr) &cinfo);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000664#endif
665
DRCab706232013-01-18 23:42:31 +0000666 if (memsrc && inbuffer != NULL)
667 free(inbuffer);
668
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000669 /* All done. */
670 exit(jerr.num_warnings ? EXIT_WARNING : EXIT_SUCCESS);
DRCe5eaf372014-05-09 18:00:32 +0000671 return 0; /* suppress no-return-value warnings */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000672}