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