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