Updated PCI IDs to latest snapshot.
[tangerine.git] / compiler / libjpeg / test / djpeg.c
blobd39121585b1201663429621e2d4211cfe0cb21f1
1 /*
2 $Id$
3 */
5 /*
6 * djpeg.c
8 * Copyright (C) 1991-1997, Thomas G. Lane.
9 * This file is part of the Independent JPEG Group's software.
10 * For conditions of distribution and use, see the accompanying README file.
12 * This file contains a command-line user interface for the JPEG decompressor.
13 * It should work on any system with Unix- or MS-DOS-style command lines.
15 * Two different command line styles are permitted, depending on the
16 * compile-time switch TWO_FILE_COMMANDLINE:
17 * djpeg [options] inputfile outputfile
18 * djpeg [options] [inputfile]
19 * In the second style, output is always to standard output, which you'd
20 * normally redirect to a file or pipe to some other program. Input is
21 * either from a named file or from standard input (typically redirected).
22 * The second style is convenient on Unix but is unhelpful on systems that
23 * don't support pipes. Also, you MUST use the first style if your system
24 * doesn't do binary I/O to stdin/stdout.
25 * To simplify script writing, the "-outfile" switch is provided. The syntax
26 * djpeg [options] -outfile outputfile inputfile
27 * works regardless of which command line style is used.
30 #include "cdjpeg.h" /* Common decls for cjpeg/djpeg applications */
31 #include "jversion.h" /* for version message */
33 #include <ctype.h> /* to declare isprint() */
35 #ifdef USE_CCOMMAND /* command-line reader for Macintosh */
36 #ifdef __MWERKS__
37 #include <SIOUX.h> /* Metrowerks needs this */
38 #include <console.h> /* ... and this */
39 #endif
40 #ifdef THINK_C
41 #include <console.h> /* Think declares it here */
42 #endif
43 #endif
46 /* Create the add-on message string table. */
48 #define JMESSAGE(code,string) string ,
50 static const char * const cdjpeg_message_table[] = {
51 #include "cderror.h"
52 NULL
57 * This list defines the known output image formats
58 * (not all of which need be supported by a given version).
59 * You can change the default output format by defining DEFAULT_FMT;
60 * indeed, you had better do so if you undefine PPM_SUPPORTED.
63 typedef enum {
64 FMT_BMP, /* BMP format (Windows flavor) */
65 FMT_GIF, /* GIF format */
66 FMT_OS2, /* BMP format (OS/2 flavor) */
67 FMT_PPM, /* PPM/PGM (PBMPLUS formats) */
68 FMT_RLE, /* RLE format */
69 FMT_TARGA, /* Targa format */
70 FMT_TIFF /* TIFF format */
71 } IMAGE_FORMATS;
73 #ifndef DEFAULT_FMT /* so can override from CFLAGS in Makefile */
74 #define DEFAULT_FMT FMT_PPM
75 #endif
77 static IMAGE_FORMATS requested_fmt;
81 * Argument-parsing code.
82 * The switch parser is designed to be useful with DOS-style command line
83 * syntax, ie, intermixed switches and file names, where only the switches
84 * to the left of a given file name affect processing of that file.
85 * The main program in this file doesn't actually use this capability...
89 static const char * progname; /* program name for error messages */
90 static char * outfilename; /* for -outfile switch */
93 LOCAL(void)
94 usage (void)
95 /* complain about bad command line */
97 fprintf(stderr, "usage: %s [switches] ", progname);
98 #ifdef TWO_FILE_COMMANDLINE
99 fprintf(stderr, "inputfile outputfile\n");
100 #else
101 fprintf(stderr, "[inputfile]\n");
102 #endif
104 fprintf(stderr, "Switches (names may be abbreviated):\n");
105 fprintf(stderr, " -colors N Reduce image to no more than N colors\n");
106 fprintf(stderr, " -fast Fast, low-quality processing\n");
107 fprintf(stderr, " -grayscale Force grayscale output\n");
108 #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);
165 LOCAL(int)
166 parse_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.
177 int argn;
178 char * arg;
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;
185 /* Scan command line options, adjust parameters */
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 */
195 break; /* else done parsing switches */
197 arg++; /* advance past switch marker character */
199 if (keymatch(arg, "bmp", 1)) {
200 /* BMP output format. */
201 requested_fmt = FMT_BMP;
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;
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;
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();
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();
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;
246 if (! printed_version) {
247 fprintf(stderr, "Independent JPEG Group's DJPEG, version %s\n%s\n",
248 JVERSION, JCOPYRIGHT);
249 printed_version = TRUE;
251 cinfo->err->trace_level++;
253 } else if (keymatch(arg, "fast", 1)) {
254 /* Select recommended processing options for quick-and-dirty output. */
255 cinfo->two_pass_quantize = FALSE;
256 cinfo->dither_mode = JDITHER_ORDERED;
257 if (! cinfo->quantize_colors) /* don't override an earlier -colors */
258 cinfo->desired_number_of_colors = 216;
259 cinfo->dct_method = JDCT_FASTEST;
260 cinfo->do_fancy_upsampling = FALSE;
262 } else if (keymatch(arg, "gif", 1)) {
263 /* GIF output format. */
264 requested_fmt = FMT_GIF;
266 } else if (keymatch(arg, "grayscale", 2) || keymatch(arg, "greyscale",2)) {
267 /* Force monochrome output. */
268 cinfo->out_color_space = JCS_GRAYSCALE;
270 } else if (keymatch(arg, "map", 3)) {
271 /* Quantize to a color map taken from an input file. */
272 if (++argn >= argc) /* advance to next argument */
273 usage();
274 if (for_real) { /* too expensive to do twice! */
275 #ifdef QUANT_2PASS_SUPPORTED /* otherwise can't quantize to supplied map */
276 FILE * mapfile;
278 if ((mapfile = fopen(argv[argn], READ_BINARY)) == NULL) {
279 fprintf(stderr, "%s: can't open %s\n", progname, argv[argn]);
280 exit(EXIT_FAILURE);
282 read_color_map(cinfo, mapfile);
283 fclose(mapfile);
284 cinfo->quantize_colors = TRUE;
285 #else
286 ERREXIT(cinfo, JERR_NOT_COMPILED);
287 #endif
290 } else if (keymatch(arg, "maxmemory", 3)) {
291 /* Maximum memory in Kb (or Mb with 'm'). */
292 long lval;
293 char ch = 'x';
295 if (++argn >= argc) /* advance to next argument */
296 usage();
297 if (sscanf(argv[argn], "%ld%c", &lval, &ch) < 1)
298 usage();
299 if (ch == 'm' || ch == 'M')
300 lval *= 1000L;
301 cinfo->mem->max_memory_to_use = lval * 1000L;
303 } else if (keymatch(arg, "nosmooth", 3)) {
304 /* Suppress fancy upsampling */
305 cinfo->do_fancy_upsampling = FALSE;
307 } else if (keymatch(arg, "onepass", 3)) {
308 /* Use fast one-pass quantization. */
309 cinfo->two_pass_quantize = FALSE;
311 } else if (keymatch(arg, "os2", 3)) {
312 /* BMP output format (OS/2 flavor). */
313 requested_fmt = FMT_OS2;
315 } else if (keymatch(arg, "outfile", 4)) {
316 /* Set output file name. */
317 if (++argn >= argc) /* advance to next argument */
318 usage();
319 outfilename = argv[argn]; /* save it away for later use */
321 } else if (keymatch(arg, "pnm", 1) || keymatch(arg, "ppm", 1)) {
322 /* PPM/PGM output format. */
323 requested_fmt = FMT_PPM;
325 } else if (keymatch(arg, "rle", 1)) {
326 /* RLE output format. */
327 requested_fmt = FMT_RLE;
329 } else if (keymatch(arg, "scale", 1)) {
330 /* Scale the output image by a fraction M/N. */
331 if (++argn >= argc) /* advance to next argument */
332 usage();
333 if (sscanf(argv[argn], "%d/%d",
334 &cinfo->scale_num, &cinfo->scale_denom) != 2)
335 usage();
337 } else if (keymatch(arg, "targa", 1)) {
338 /* Targa output format. */
339 requested_fmt = FMT_TARGA;
341 } else {
342 usage(); /* bogus switch */
346 return argn; /* return index of next arg (file name) */
351 * Marker processor for COM and interesting APPn markers.
352 * This replaces the library's built-in processor, which just skips the marker.
353 * We want to print out the marker as text, to the extent possible.
354 * Note this code relies on a non-suspending data source.
357 LOCAL(unsigned int)
358 jpeg_getc (j_decompress_ptr cinfo)
359 /* Read next byte */
361 struct jpeg_source_mgr * datasrc = cinfo->src;
363 if (datasrc->bytes_in_buffer == 0) {
364 if (! (*datasrc->fill_input_buffer) (cinfo))
365 ERREXIT(cinfo, JERR_CANT_SUSPEND);
367 datasrc->bytes_in_buffer--;
368 return GETJOCTET(*datasrc->next_input_byte++);
372 METHODDEF(boolean)
373 print_text_marker (j_decompress_ptr cinfo)
375 boolean traceit = (cinfo->err->trace_level >= 1);
376 INT32 length;
377 unsigned int ch;
378 unsigned int lastch = 0;
380 length = jpeg_getc(cinfo) << 8;
381 length += jpeg_getc(cinfo);
382 length -= 2; /* discount the length word itself */
384 if (traceit) {
385 if (cinfo->unread_marker == JPEG_COM)
386 fprintf(stderr, "Comment, length %ld:\n", (long) length);
387 else /* assume it is an APPn otherwise */
388 fprintf(stderr, "APP%d, length %ld:\n",
389 cinfo->unread_marker - JPEG_APP0, (long) length);
392 while (--length >= 0) {
393 ch = jpeg_getc(cinfo);
394 if (traceit) {
395 /* Emit the character in a readable form.
396 * Nonprintables are converted to \nnn form,
397 * while \ is converted to \\.
398 * Newlines in CR, CR/LF, or LF form will be printed as one newline.
400 if (ch == '\r') {
401 fprintf(stderr, "\n");
402 } else if (ch == '\n') {
403 if (lastch != '\r')
404 fprintf(stderr, "\n");
405 } else if (ch == '\\') {
406 fprintf(stderr, "\\\\");
407 } else if (isprint(ch)) {
408 putc(ch, stderr);
409 } else {
410 fprintf(stderr, "\\%03o", ch);
412 lastch = ch;
416 if (traceit)
417 fprintf(stderr, "\n");
419 return TRUE;
424 * The main program.
428 main (int argc, char **argv)
430 struct jpeg_decompress_struct cinfo;
431 struct jpeg_error_mgr jerr;
432 #ifdef PROGRESS_REPORT
433 struct cdjpeg_progress_mgr progress;
434 #endif
435 int file_index;
436 djpeg_dest_ptr dest_mgr = NULL;
437 FILE * input_file;
438 FILE * output_file;
439 JDIMENSION num_scanlines;
441 /* On Mac, fetch a command line. */
442 #ifdef USE_CCOMMAND
443 argc = ccommand(&argv);
444 #endif
446 progname = argv[0];
447 if (progname == NULL || progname[0] == 0)
448 progname = "djpeg"; /* in case C library doesn't provide it */
450 /* Initialize the JPEG decompression object with default error handling. */
451 cinfo.err = jpeg_std_error(&jerr);
452 jpeg_create_decompress(&cinfo);
453 /* Add some application-specific error messages (from cderror.h) */
454 jerr.addon_message_table = cdjpeg_message_table;
455 jerr.first_addon_message = JMSG_FIRSTADDONCODE;
456 jerr.last_addon_message = JMSG_LASTADDONCODE;
458 /* Insert custom marker processor for COM and APP12.
459 * APP12 is used by some digital camera makers for textual info,
460 * so we provide the ability to display it as text.
461 * If you like, additional APPn marker types can be selected for display,
462 * but don't try to override APP0 or APP14 this way (see libjpeg.doc).
464 jpeg_set_marker_processor(&cinfo, JPEG_COM, print_text_marker);
465 jpeg_set_marker_processor(&cinfo, JPEG_APP0+12, print_text_marker);
467 /* Now safe to enable signal catcher. */
468 #ifdef NEED_SIGNAL_CATCHER
469 enable_signal_catcher((j_common_ptr) &cinfo);
470 #endif
472 /* Scan command line to find file names. */
473 /* It is convenient to use just one switch-parsing routine, but the switch
474 * values read here are ignored; we will rescan the switches after opening
475 * the input file.
476 * (Exception: tracing level set here controls verbosity for COM markers
477 * found during jpeg_read_header...)
480 file_index = parse_switches(&cinfo, argc, argv, 0, FALSE);
482 #ifdef TWO_FILE_COMMANDLINE
483 /* Must have either -outfile switch or explicit output file name */
484 if (outfilename == NULL) {
485 if (file_index != argc-2) {
486 fprintf(stderr, "%s: must name one input and one output file\n",
487 progname);
488 usage();
490 outfilename = argv[file_index+1];
491 } else {
492 if (file_index != argc-1) {
493 fprintf(stderr, "%s: must name one input and one output file\n",
494 progname);
495 usage();
498 #else
499 /* Unix style: expect zero or one file name */
500 if (file_index < argc-1) {
501 fprintf(stderr, "%s: only one input file\n", progname);
502 usage();
504 #endif /* TWO_FILE_COMMANDLINE */
506 /* Open the input file. */
507 if (file_index < argc) {
508 if ((input_file = fopen(argv[file_index], READ_BINARY)) == NULL) {
509 fprintf(stderr, "%s: can't open %s\n", progname, argv[file_index]);
510 exit(EXIT_FAILURE);
512 } else {
513 /* default input file is stdin */
514 input_file = read_stdin();
517 /* Open the output file. */
518 if (outfilename != NULL) {
519 if ((output_file = fopen(outfilename, WRITE_BINARY)) == NULL) {
520 fprintf(stderr, "%s: can't open %s\n", progname, outfilename);
521 exit(EXIT_FAILURE);
523 } else {
524 /* default output file is stdout */
525 output_file = write_stdout();
528 #ifdef PROGRESS_REPORT
529 start_progress_monitor((j_common_ptr) &cinfo, &progress);
530 #endif
532 /* Specify data source for decompression */
533 jpeg_stdio_src(&cinfo, input_file);
535 /* Read file header, set default decompression parameters */
536 (void) jpeg_read_header(&cinfo, TRUE);
538 /* Adjust default decompression parameters by re-parsing the options */
539 file_index = parse_switches(&cinfo, argc, argv, 0, TRUE);
541 /* Initialize the output module now to let it override any crucial
542 * option settings (for instance, GIF wants to force color quantization).
544 switch (requested_fmt) {
545 #ifdef BMP_SUPPORTED
546 case FMT_BMP:
547 dest_mgr = jinit_write_bmp(&cinfo, FALSE);
548 break;
549 case FMT_OS2:
550 dest_mgr = jinit_write_bmp(&cinfo, TRUE);
551 break;
552 #endif
553 #ifdef GIF_SUPPORTED
554 case FMT_GIF:
555 dest_mgr = jinit_write_gif(&cinfo);
556 break;
557 #endif
558 #ifdef PPM_SUPPORTED
559 case FMT_PPM:
560 dest_mgr = jinit_write_ppm(&cinfo);
561 break;
562 #endif
563 #ifdef RLE_SUPPORTED
564 case FMT_RLE:
565 dest_mgr = jinit_write_rle(&cinfo);
566 break;
567 #endif
568 #ifdef TARGA_SUPPORTED
569 case FMT_TARGA:
570 dest_mgr = jinit_write_targa(&cinfo);
571 break;
572 #endif
573 default:
574 ERREXIT(&cinfo, JERR_UNSUPPORTED_FORMAT);
575 break;
577 dest_mgr->output_file = output_file;
579 /* Start decompressor */
580 (void) jpeg_start_decompress(&cinfo);
582 /* Write output file header */
583 (*dest_mgr->start_output) (&cinfo, dest_mgr);
585 /* Process data */
586 while (cinfo.output_scanline < cinfo.output_height) {
587 num_scanlines = jpeg_read_scanlines(&cinfo, dest_mgr->buffer,
588 dest_mgr->buffer_height);
589 (*dest_mgr->put_pixel_rows) (&cinfo, dest_mgr, num_scanlines);
592 #ifdef PROGRESS_REPORT
593 /* Hack: count final pass as done in case finish_output does an extra pass.
594 * The library won't have updated completed_passes.
596 progress.pub.completed_passes = progress.pub.total_passes;
597 #endif
599 /* Finish decompression and release memory.
600 * I must do it in this order because output module has allocated memory
601 * of lifespan JPOOL_IMAGE; it needs to finish before releasing memory.
603 (*dest_mgr->finish_output) (&cinfo, dest_mgr);
604 (void) jpeg_finish_decompress(&cinfo);
605 jpeg_destroy_decompress(&cinfo);
607 /* Close files, if we opened them */
608 if (input_file != stdin)
609 fclose(input_file);
610 if (output_file != stdout)
611 fclose(output_file);
613 #ifdef PROGRESS_REPORT
614 end_progress_monitor((j_common_ptr) &cinfo);
615 #endif
617 /* All done. */
618 exit(jerr.num_warnings ? EXIT_WARNING : EXIT_SUCCESS);
619 return 0; /* suppress no-return-value warnings */