blob: 2438856ef66d96fc70629fc6ee6f39e7808089c9 [file] [log] [blame]
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +00001/*
2 * djpeg.c
3 *
Thomas G. Lane5ead57a1998-03-27 00:00:00 +00004 * Copyright (C) 1991-1997, Thomas G. Lane.
DRC6a833a82011-03-03 16:58:47 +00005 * Copyright (C) 2010-2011, D. R. Commander.
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +00006 * This file is part of the Independent JPEG Group's software.
7 * For conditions of distribution and use, see the accompanying README file.
8 *
9 * This file contains a command-line user interface for the JPEG decompressor.
10 * It should work on any system with Unix- or MS-DOS-style command lines.
11 *
12 * Two different command line styles are permitted, depending on the
13 * compile-time switch TWO_FILE_COMMANDLINE:
14 * djpeg [options] inputfile outputfile
15 * djpeg [options] [inputfile]
16 * In the second style, output is always to standard output, which you'd
17 * normally redirect to a file or pipe to some other program. Input is
18 * either from a named file or from standard input (typically redirected).
19 * The second style is convenient on Unix but is unhelpful on systems that
20 * don't support pipes. Also, you MUST use the first style if your system
21 * doesn't do binary I/O to stdin/stdout.
22 * To simplify script writing, the "-outfile" switch is provided. The syntax
23 * djpeg [options] -outfile outputfile inputfile
24 * works regardless of which command line style is used.
25 */
26
27#include "cdjpeg.h" /* Common decls for cjpeg/djpeg applications */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000028#include "jversion.h" /* for version message */
DRC2cdd2ae2010-10-10 06:54:21 +000029#include "config.h"
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000030
Thomas G. Lanebc79e061995-08-02 00:00:00 +000031#include <ctype.h> /* to declare isprint() */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000032
33#ifdef USE_CCOMMAND /* command-line reader for Macintosh */
34#ifdef __MWERKS__
Thomas G. Lane489583f1996-02-07 00:00:00 +000035#include <SIOUX.h> /* Metrowerks needs this */
36#include <console.h> /* ... and this */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000037#endif
38#ifdef THINK_C
39#include <console.h> /* Think declares it here */
40#endif
41#endif
42
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000043
Thomas G. Lane9ba2f5e1994-12-07 00:00:00 +000044/* Create the add-on message string table. */
45
46#define JMESSAGE(code,string) string ,
47
48static const char * const cdjpeg_message_table[] = {
49#include "cderror.h"
50 NULL
51};
52
53
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000054/*
55 * This list defines the known output image formats
56 * (not all of which need be supported by a given version).
57 * You can change the default output format by defining DEFAULT_FMT;
58 * indeed, you had better do so if you undefine PPM_SUPPORTED.
59 */
60
61typedef enum {
62 FMT_BMP, /* BMP format (Windows flavor) */
63 FMT_GIF, /* GIF format */
64 FMT_OS2, /* BMP format (OS/2 flavor) */
65 FMT_PPM, /* PPM/PGM (PBMPLUS formats) */
66 FMT_RLE, /* RLE format */
67 FMT_TARGA, /* Targa format */
68 FMT_TIFF /* TIFF format */
69} IMAGE_FORMATS;
70
71#ifndef DEFAULT_FMT /* so can override from CFLAGS in Makefile */
72#define DEFAULT_FMT FMT_PPM
73#endif
74
75static IMAGE_FORMATS requested_fmt;
76
77
78/*
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000079 * Argument-parsing code.
80 * The switch parser is designed to be useful with DOS-style command line
81 * syntax, ie, intermixed switches and file names, where only the switches
82 * to the left of a given file name affect processing of that file.
83 * The main program in this file doesn't actually use this capability...
84 */
85
86
87static const char * progname; /* program name for error messages */
88static char * outfilename; /* for -outfile switch */
89
90
Thomas G. Lane489583f1996-02-07 00:00:00 +000091LOCAL(void)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000092usage (void)
93/* complain about bad command line */
94{
95 fprintf(stderr, "usage: %s [switches] ", progname);
96#ifdef TWO_FILE_COMMANDLINE
97 fprintf(stderr, "inputfile outputfile\n");
98#else
99 fprintf(stderr, "[inputfile]\n");
100#endif
101
102 fprintf(stderr, "Switches (names may be abbreviated):\n");
103 fprintf(stderr, " -colors N Reduce image to no more than N colors\n");
104 fprintf(stderr, " -fast Fast, low-quality processing\n");
105 fprintf(stderr, " -grayscale Force grayscale output\n");
DRC6a833a82011-03-03 16:58:47 +0000106 fprintf(stderr, " -rgb Force RGB output\n");
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000107#ifdef IDCT_SCALING_SUPPORTED
108 fprintf(stderr, " -scale M/N Scale output image by fraction M/N, eg, 1/8\n");
109#endif
110#ifdef BMP_SUPPORTED
111 fprintf(stderr, " -bmp Select BMP output format (Windows style)%s\n",
112 (DEFAULT_FMT == FMT_BMP ? " (default)" : ""));
113#endif
114#ifdef GIF_SUPPORTED
115 fprintf(stderr, " -gif Select GIF output format%s\n",
116 (DEFAULT_FMT == FMT_GIF ? " (default)" : ""));
117#endif
118#ifdef BMP_SUPPORTED
119 fprintf(stderr, " -os2 Select BMP output format (OS/2 style)%s\n",
120 (DEFAULT_FMT == FMT_OS2 ? " (default)" : ""));
121#endif
122#ifdef PPM_SUPPORTED
123 fprintf(stderr, " -pnm Select PBMPLUS (PPM/PGM) output format%s\n",
124 (DEFAULT_FMT == FMT_PPM ? " (default)" : ""));
125#endif
126#ifdef RLE_SUPPORTED
127 fprintf(stderr, " -rle Select Utah RLE output format%s\n",
128 (DEFAULT_FMT == FMT_RLE ? " (default)" : ""));
129#endif
130#ifdef TARGA_SUPPORTED
131 fprintf(stderr, " -targa Select Targa output format%s\n",
132 (DEFAULT_FMT == FMT_TARGA ? " (default)" : ""));
133#endif
134 fprintf(stderr, "Switches for advanced users:\n");
135#ifdef DCT_ISLOW_SUPPORTED
136 fprintf(stderr, " -dct int Use integer DCT method%s\n",
137 (JDCT_DEFAULT == JDCT_ISLOW ? " (default)" : ""));
138#endif
139#ifdef DCT_IFAST_SUPPORTED
140 fprintf(stderr, " -dct fast Use fast integer DCT (less accurate)%s\n",
141 (JDCT_DEFAULT == JDCT_IFAST ? " (default)" : ""));
142#endif
143#ifdef DCT_FLOAT_SUPPORTED
144 fprintf(stderr, " -dct float Use floating-point DCT method%s\n",
145 (JDCT_DEFAULT == JDCT_FLOAT ? " (default)" : ""));
146#endif
147 fprintf(stderr, " -dither fs Use F-S dithering (default)\n");
148 fprintf(stderr, " -dither none Don't use dithering in quantization\n");
149 fprintf(stderr, " -dither ordered Use ordered dither (medium speed, quality)\n");
150#ifdef QUANT_2PASS_SUPPORTED
151 fprintf(stderr, " -map FILE Map to colors used in named image file\n");
152#endif
153 fprintf(stderr, " -nosmooth Don't use high-quality upsampling\n");
154#ifdef QUANT_1PASS_SUPPORTED
155 fprintf(stderr, " -onepass Use 1-pass quantization (fast, low quality)\n");
156#endif
157 fprintf(stderr, " -maxmemory N Maximum memory to use (in kbytes)\n");
158 fprintf(stderr, " -outfile name Specify name for output file\n");
159 fprintf(stderr, " -verbose or -debug Emit debug output\n");
160 exit(EXIT_FAILURE);
161}
162
163
Thomas G. Lane489583f1996-02-07 00:00:00 +0000164LOCAL(int)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000165parse_switches (j_decompress_ptr cinfo, int argc, char **argv,
166 int last_file_arg_seen, boolean for_real)
167/* Parse optional switches.
168 * Returns argv[] index of first file-name argument (== argc if none).
169 * Any file names with indexes <= last_file_arg_seen are ignored;
170 * they have presumably been processed in a previous iteration.
171 * (Pass 0 for last_file_arg_seen on the first or only iteration.)
172 * for_real is FALSE on the first (dummy) pass; we may skip any expensive
173 * processing.
174 */
175{
176 int argn;
177 char * arg;
178
179 /* Set up default JPEG parameters. */
180 requested_fmt = DEFAULT_FMT; /* set default output file format */
181 outfilename = NULL;
182 cinfo->err->trace_level = 0;
183
184 /* Scan command line options, adjust parameters */
185
186 for (argn = 1; argn < argc; argn++) {
187 arg = argv[argn];
188 if (*arg != '-') {
189 /* Not a switch, must be a file name argument */
190 if (argn <= last_file_arg_seen) {
191 outfilename = NULL; /* -outfile applies to just one input file */
192 continue; /* ignore this name if previously processed */
193 }
194 break; /* else done parsing switches */
195 }
196 arg++; /* advance past switch marker character */
197
198 if (keymatch(arg, "bmp", 1)) {
199 /* BMP output format. */
200 requested_fmt = FMT_BMP;
201
202 } else if (keymatch(arg, "colors", 1) || keymatch(arg, "colours", 1) ||
203 keymatch(arg, "quantize", 1) || keymatch(arg, "quantise", 1)) {
204 /* Do color quantization. */
205 int val;
206
207 if (++argn >= argc) /* advance to next argument */
208 usage();
209 if (sscanf(argv[argn], "%d", &val) != 1)
210 usage();
211 cinfo->desired_number_of_colors = val;
212 cinfo->quantize_colors = TRUE;
213
214 } else if (keymatch(arg, "dct", 2)) {
215 /* Select IDCT algorithm. */
216 if (++argn >= argc) /* advance to next argument */
217 usage();
218 if (keymatch(argv[argn], "int", 1)) {
219 cinfo->dct_method = JDCT_ISLOW;
220 } else if (keymatch(argv[argn], "fast", 2)) {
221 cinfo->dct_method = JDCT_IFAST;
222 } else if (keymatch(argv[argn], "float", 2)) {
223 cinfo->dct_method = JDCT_FLOAT;
224 } else
225 usage();
226
227 } else if (keymatch(arg, "dither", 2)) {
228 /* Select dithering algorithm. */
229 if (++argn >= argc) /* advance to next argument */
230 usage();
231 if (keymatch(argv[argn], "fs", 2)) {
232 cinfo->dither_mode = JDITHER_FS;
233 } else if (keymatch(argv[argn], "none", 2)) {
234 cinfo->dither_mode = JDITHER_NONE;
235 } else if (keymatch(argv[argn], "ordered", 2)) {
236 cinfo->dither_mode = JDITHER_ORDERED;
237 } else
238 usage();
239
240 } else if (keymatch(arg, "debug", 1) || keymatch(arg, "verbose", 1)) {
241 /* Enable debug printouts. */
242 /* On first -d, print version identification */
243 static boolean printed_version = FALSE;
244
245 if (! printed_version) {
DRC2cdd2ae2010-10-10 06:54:21 +0000246 fprintf(stderr, "%s version %s (build %s)\n",
247 PACKAGE_NAME, VERSION, BUILD);
DRC1d5b1cf2012-01-31 10:39:29 +0000248 fprintf(stderr, "%s\n\n", JCOPYRIGHT);
249 fprintf(stderr, "Emulating The Independent JPEG Group's libjpeg, version %s\n\n",
250 JVERSION);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000251 printed_version = TRUE;
252 }
253 cinfo->err->trace_level++;
254
255 } else if (keymatch(arg, "fast", 1)) {
256 /* Select recommended processing options for quick-and-dirty output. */
257 cinfo->two_pass_quantize = FALSE;
258 cinfo->dither_mode = JDITHER_ORDERED;
259 if (! cinfo->quantize_colors) /* don't override an earlier -colors */
260 cinfo->desired_number_of_colors = 216;
261 cinfo->dct_method = JDCT_FASTEST;
262 cinfo->do_fancy_upsampling = FALSE;
263
264 } else if (keymatch(arg, "gif", 1)) {
265 /* GIF output format. */
266 requested_fmt = FMT_GIF;
267
268 } else if (keymatch(arg, "grayscale", 2) || keymatch(arg, "greyscale",2)) {
269 /* Force monochrome output. */
270 cinfo->out_color_space = JCS_GRAYSCALE;
271
DRC6a833a82011-03-03 16:58:47 +0000272 } else if (keymatch(arg, "rgb", 2)) {
273 /* Force RGB output. */
274 cinfo->out_color_space = JCS_RGB;
275
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000276 } else if (keymatch(arg, "map", 3)) {
277 /* Quantize to a color map taken from an input file. */
278 if (++argn >= argc) /* advance to next argument */
279 usage();
280 if (for_real) { /* too expensive to do twice! */
281#ifdef QUANT_2PASS_SUPPORTED /* otherwise can't quantize to supplied map */
282 FILE * mapfile;
283
284 if ((mapfile = fopen(argv[argn], READ_BINARY)) == NULL) {
285 fprintf(stderr, "%s: can't open %s\n", progname, argv[argn]);
286 exit(EXIT_FAILURE);
287 }
288 read_color_map(cinfo, mapfile);
289 fclose(mapfile);
290 cinfo->quantize_colors = TRUE;
291#else
292 ERREXIT(cinfo, JERR_NOT_COMPILED);
293#endif
294 }
295
296 } else if (keymatch(arg, "maxmemory", 3)) {
297 /* Maximum memory in Kb (or Mb with 'm'). */
298 long lval;
299 char ch = 'x';
300
301 if (++argn >= argc) /* advance to next argument */
302 usage();
303 if (sscanf(argv[argn], "%ld%c", &lval, &ch) < 1)
304 usage();
305 if (ch == 'm' || ch == 'M')
306 lval *= 1000L;
307 cinfo->mem->max_memory_to_use = lval * 1000L;
308
309 } else if (keymatch(arg, "nosmooth", 3)) {
310 /* Suppress fancy upsampling */
311 cinfo->do_fancy_upsampling = FALSE;
312
313 } else if (keymatch(arg, "onepass", 3)) {
314 /* Use fast one-pass quantization. */
315 cinfo->two_pass_quantize = FALSE;
316
317 } else if (keymatch(arg, "os2", 3)) {
318 /* BMP output format (OS/2 flavor). */
319 requested_fmt = FMT_OS2;
320
321 } else if (keymatch(arg, "outfile", 4)) {
322 /* Set output file name. */
323 if (++argn >= argc) /* advance to next argument */
324 usage();
325 outfilename = argv[argn]; /* save it away for later use */
326
327 } else if (keymatch(arg, "pnm", 1) || keymatch(arg, "ppm", 1)) {
328 /* PPM/PGM output format. */
329 requested_fmt = FMT_PPM;
330
331 } else if (keymatch(arg, "rle", 1)) {
332 /* RLE output format. */
333 requested_fmt = FMT_RLE;
334
335 } else if (keymatch(arg, "scale", 1)) {
336 /* Scale the output image by a fraction M/N. */
337 if (++argn >= argc) /* advance to next argument */
338 usage();
339 if (sscanf(argv[argn], "%d/%d",
340 &cinfo->scale_num, &cinfo->scale_denom) != 2)
341 usage();
342
343 } else if (keymatch(arg, "targa", 1)) {
344 /* Targa output format. */
345 requested_fmt = FMT_TARGA;
346
347 } else {
348 usage(); /* bogus switch */
349 }
350 }
351
352 return argn; /* return index of next arg (file name) */
353}
354
355
356/*
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000357 * Marker processor for COM and interesting APPn markers.
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000358 * This replaces the library's built-in processor, which just skips the marker.
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000359 * We want to print out the marker as text, to the extent possible.
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000360 * Note this code relies on a non-suspending data source.
361 */
362
Thomas G. Lane489583f1996-02-07 00:00:00 +0000363LOCAL(unsigned int)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000364jpeg_getc (j_decompress_ptr cinfo)
365/* Read next byte */
366{
367 struct jpeg_source_mgr * datasrc = cinfo->src;
368
369 if (datasrc->bytes_in_buffer == 0) {
370 if (! (*datasrc->fill_input_buffer) (cinfo))
371 ERREXIT(cinfo, JERR_CANT_SUSPEND);
372 }
373 datasrc->bytes_in_buffer--;
374 return GETJOCTET(*datasrc->next_input_byte++);
375}
376
377
Thomas G. Lane489583f1996-02-07 00:00:00 +0000378METHODDEF(boolean)
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000379print_text_marker (j_decompress_ptr cinfo)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000380{
381 boolean traceit = (cinfo->err->trace_level >= 1);
382 INT32 length;
383 unsigned int ch;
384 unsigned int lastch = 0;
385
386 length = jpeg_getc(cinfo) << 8;
387 length += jpeg_getc(cinfo);
388 length -= 2; /* discount the length word itself */
389
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000390 if (traceit) {
391 if (cinfo->unread_marker == JPEG_COM)
392 fprintf(stderr, "Comment, length %ld:\n", (long) length);
393 else /* assume it is an APPn otherwise */
394 fprintf(stderr, "APP%d, length %ld:\n",
395 cinfo->unread_marker - JPEG_APP0, (long) length);
396 }
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000397
398 while (--length >= 0) {
399 ch = jpeg_getc(cinfo);
400 if (traceit) {
401 /* Emit the character in a readable form.
402 * Nonprintables are converted to \nnn form,
403 * while \ is converted to \\.
404 * Newlines in CR, CR/LF, or LF form will be printed as one newline.
405 */
406 if (ch == '\r') {
407 fprintf(stderr, "\n");
408 } else if (ch == '\n') {
409 if (lastch != '\r')
410 fprintf(stderr, "\n");
411 } else if (ch == '\\') {
412 fprintf(stderr, "\\\\");
413 } else if (isprint(ch)) {
414 putc(ch, stderr);
415 } else {
416 fprintf(stderr, "\\%03o", ch);
417 }
418 lastch = ch;
419 }
420 }
421
422 if (traceit)
423 fprintf(stderr, "\n");
424
425 return TRUE;
426}
427
428
429/*
430 * The main program.
431 */
432
Thomas G. Lane489583f1996-02-07 00:00:00 +0000433int
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000434main (int argc, char **argv)
435{
436 struct jpeg_decompress_struct cinfo;
437 struct jpeg_error_mgr jerr;
438#ifdef PROGRESS_REPORT
439 struct cdjpeg_progress_mgr progress;
440#endif
441 int file_index;
442 djpeg_dest_ptr dest_mgr = NULL;
443 FILE * input_file;
444 FILE * output_file;
445 JDIMENSION num_scanlines;
446
447 /* On Mac, fetch a command line. */
448#ifdef USE_CCOMMAND
449 argc = ccommand(&argv);
450#endif
451
452 progname = argv[0];
453 if (progname == NULL || progname[0] == 0)
454 progname = "djpeg"; /* in case C library doesn't provide it */
455
456 /* Initialize the JPEG decompression object with default error handling. */
457 cinfo.err = jpeg_std_error(&jerr);
458 jpeg_create_decompress(&cinfo);
459 /* Add some application-specific error messages (from cderror.h) */
Thomas G. Lane9ba2f5e1994-12-07 00:00:00 +0000460 jerr.addon_message_table = cdjpeg_message_table;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000461 jerr.first_addon_message = JMSG_FIRSTADDONCODE;
462 jerr.last_addon_message = JMSG_LASTADDONCODE;
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000463
464 /* Insert custom marker processor for COM and APP12.
465 * APP12 is used by some digital camera makers for textual info,
466 * so we provide the ability to display it as text.
467 * If you like, additional APPn marker types can be selected for display,
DRC39ea5622010-10-12 01:55:31 +0000468 * but don't try to override APP0 or APP14 this way (see libjpeg.txt).
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000469 */
470 jpeg_set_marker_processor(&cinfo, JPEG_COM, print_text_marker);
471 jpeg_set_marker_processor(&cinfo, JPEG_APP0+12, print_text_marker);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000472
473 /* Now safe to enable signal catcher. */
474#ifdef NEED_SIGNAL_CATCHER
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000475 enable_signal_catcher((j_common_ptr) &cinfo);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000476#endif
477
478 /* Scan command line to find file names. */
479 /* It is convenient to use just one switch-parsing routine, but the switch
480 * values read here are ignored; we will rescan the switches after opening
481 * the input file.
482 * (Exception: tracing level set here controls verbosity for COM markers
483 * found during jpeg_read_header...)
484 */
485
486 file_index = parse_switches(&cinfo, argc, argv, 0, FALSE);
487
488#ifdef TWO_FILE_COMMANDLINE
489 /* Must have either -outfile switch or explicit output file name */
490 if (outfilename == NULL) {
491 if (file_index != argc-2) {
492 fprintf(stderr, "%s: must name one input and one output file\n",
493 progname);
494 usage();
495 }
496 outfilename = argv[file_index+1];
497 } else {
498 if (file_index != argc-1) {
499 fprintf(stderr, "%s: must name one input and one output file\n",
500 progname);
501 usage();
502 }
503 }
504#else
505 /* Unix style: expect zero or one file name */
506 if (file_index < argc-1) {
507 fprintf(stderr, "%s: only one input file\n", progname);
508 usage();
509 }
510#endif /* TWO_FILE_COMMANDLINE */
511
512 /* Open the input file. */
513 if (file_index < argc) {
514 if ((input_file = fopen(argv[file_index], READ_BINARY)) == NULL) {
515 fprintf(stderr, "%s: can't open %s\n", progname, argv[file_index]);
516 exit(EXIT_FAILURE);
517 }
518 } else {
519 /* default input file is stdin */
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000520 input_file = read_stdin();
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000521 }
522
523 /* Open the output file. */
524 if (outfilename != NULL) {
525 if ((output_file = fopen(outfilename, WRITE_BINARY)) == NULL) {
526 fprintf(stderr, "%s: can't open %s\n", progname, outfilename);
527 exit(EXIT_FAILURE);
528 }
529 } else {
530 /* default output file is stdout */
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000531 output_file = write_stdout();
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000532 }
533
534#ifdef PROGRESS_REPORT
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000535 start_progress_monitor((j_common_ptr) &cinfo, &progress);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000536#endif
537
538 /* Specify data source for decompression */
539 jpeg_stdio_src(&cinfo, input_file);
540
541 /* Read file header, set default decompression parameters */
542 (void) jpeg_read_header(&cinfo, TRUE);
543
544 /* Adjust default decompression parameters by re-parsing the options */
545 file_index = parse_switches(&cinfo, argc, argv, 0, TRUE);
546
547 /* Initialize the output module now to let it override any crucial
548 * option settings (for instance, GIF wants to force color quantization).
549 */
550 switch (requested_fmt) {
551#ifdef BMP_SUPPORTED
552 case FMT_BMP:
553 dest_mgr = jinit_write_bmp(&cinfo, FALSE);
554 break;
555 case FMT_OS2:
556 dest_mgr = jinit_write_bmp(&cinfo, TRUE);
557 break;
558#endif
559#ifdef GIF_SUPPORTED
560 case FMT_GIF:
561 dest_mgr = jinit_write_gif(&cinfo);
562 break;
563#endif
564#ifdef PPM_SUPPORTED
565 case FMT_PPM:
566 dest_mgr = jinit_write_ppm(&cinfo);
567 break;
568#endif
569#ifdef RLE_SUPPORTED
570 case FMT_RLE:
571 dest_mgr = jinit_write_rle(&cinfo);
572 break;
573#endif
574#ifdef TARGA_SUPPORTED
575 case FMT_TARGA:
576 dest_mgr = jinit_write_targa(&cinfo);
577 break;
578#endif
579 default:
580 ERREXIT(&cinfo, JERR_UNSUPPORTED_FORMAT);
581 break;
582 }
583 dest_mgr->output_file = output_file;
584
585 /* Start decompressor */
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000586 (void) jpeg_start_decompress(&cinfo);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000587
588 /* Write output file header */
589 (*dest_mgr->start_output) (&cinfo, dest_mgr);
590
591 /* Process data */
592 while (cinfo.output_scanline < cinfo.output_height) {
593 num_scanlines = jpeg_read_scanlines(&cinfo, dest_mgr->buffer,
594 dest_mgr->buffer_height);
595 (*dest_mgr->put_pixel_rows) (&cinfo, dest_mgr, num_scanlines);
596 }
597
598#ifdef PROGRESS_REPORT
599 /* Hack: count final pass as done in case finish_output does an extra pass.
600 * The library won't have updated completed_passes.
601 */
602 progress.pub.completed_passes = progress.pub.total_passes;
603#endif
604
605 /* Finish decompression and release memory.
606 * I must do it in this order because output module has allocated memory
607 * of lifespan JPOOL_IMAGE; it needs to finish before releasing memory.
608 */
609 (*dest_mgr->finish_output) (&cinfo, dest_mgr);
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000610 (void) jpeg_finish_decompress(&cinfo);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000611 jpeg_destroy_decompress(&cinfo);
612
Thomas G. Lane9ba2f5e1994-12-07 00:00:00 +0000613 /* Close files, if we opened them */
614 if (input_file != stdin)
615 fclose(input_file);
616 if (output_file != stdout)
617 fclose(output_file);
618
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000619#ifdef PROGRESS_REPORT
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000620 end_progress_monitor((j_common_ptr) &cinfo);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000621#endif
622
623 /* All done. */
624 exit(jerr.num_warnings ? EXIT_WARNING : EXIT_SUCCESS);
625 return 0; /* suppress no-return-value warnings */
626}