home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD2.mdf / c / library / dos / grafik / jpeg20 / jdmain.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-02-11  |  7.4 KB  |  272 lines

  1. /*
  2.  * jdmain.c
  3.  *
  4.  * Copyright (C) 1991, Thomas G. Lane.
  5.  * This file is part of the Independent JPEG Group's software.
  6.  * For conditions of distribution and use, see the accompanying README file.
  7.  *
  8.  * This file contains a trivial test user interface for the JPEG decompressor.
  9.  * It should work on any system with Unix- or MS-DOS-style command lines.
  10.  *
  11.  * Two different command line styles are permitted, depending on the
  12.  * compile-time switch TWO_FILE_COMMANDLINE:
  13.  *    djpeg [options]  inputfile outputfile
  14.  *    djpeg [options]  [inputfile]
  15.  * In the second style, output is always to standard output, which you'd
  16.  * normally redirect to a file or pipe to some other program.  Input is
  17.  * either from a named file or from standard input (typically redirected).
  18.  * The second style is convenient on Unix but is unhelpful on systems that
  19.  * don't support pipes.  Also, you MUST use the first style if your system
  20.  * doesn't do binary I/O to stdin/stdout.
  21.  */
  22.  
  23. #include "jinclude.h"
  24. #ifdef INCLUDES_ARE_ANSI
  25. #include <stdlib.h>        /* to declare exit() */
  26. #endif
  27.  
  28. #ifdef THINK_C
  29. #include <console.h>        /* command-line reader for Macintosh */
  30. #endif
  31.  
  32. #ifdef DONT_USE_B_MODE        /* define mode parameters for fopen() */
  33. #define READ_BINARY    "r"
  34. #define WRITE_BINARY    "w"
  35. #else
  36. #define READ_BINARY    "rb"
  37. #define WRITE_BINARY    "wb"
  38. #endif
  39.  
  40. #include "jversion.h"        /* for version message */
  41.  
  42.  
  43. /*
  44.  * PD version of getopt(3).
  45.  */
  46.  
  47. #include "egetopt.c"
  48.  
  49.  
  50. /*
  51.  * This list defines the known output image formats
  52.  * (not all of which need be supported by a given version).
  53.  * You can change the default output format by defining DEFAULT_FMT;
  54.  * indeed, you had better do so if you undefine PPM_SUPPORTED.
  55.  */
  56.  
  57. typedef enum {
  58.     FMT_GIF,        /* GIF format */
  59.     FMT_PPM,        /* PPM/PGM (PBMPLUS formats) */
  60.     FMT_RLE,        /* RLE format */
  61.     FMT_TARGA,        /* Targa format */
  62.     FMT_TIFF        /* TIFF format */
  63. } IMAGE_FORMATS;
  64.  
  65. #ifndef DEFAULT_FMT        /* so can override from CFLAGS in Makefile */
  66. #define DEFAULT_FMT    FMT_PPM
  67. #endif
  68.  
  69. static IMAGE_FORMATS requested_fmt;
  70.  
  71.  
  72. /*
  73.  * This routine gets control after the input file header has been read.
  74.  * It must determine what output file format is to be written,
  75.  * and make any other decompression parameter changes that are desirable.
  76.  */
  77.  
  78. METHODDEF void
  79. d_ui_method_selection (decompress_info_ptr cinfo)
  80. {
  81.   /* if grayscale or CMYK input, force similar output; */
  82.   /* else leave the output colorspace as set by options. */
  83.   if (cinfo->jpeg_color_space == CS_GRAYSCALE)
  84.     cinfo->out_color_space = CS_GRAYSCALE;
  85.   else if (cinfo->jpeg_color_space == CS_CMYK)
  86.     cinfo->out_color_space = CS_CMYK;
  87.  
  88.   /* select output file format */
  89.   /* Note: jselwxxx routine may make additional parameter changes,
  90.    * such as forcing color quantization if it's a colormapped format.
  91.    */
  92.   switch (requested_fmt) {
  93. #ifdef GIF_SUPPORTED
  94.   case FMT_GIF:
  95.     jselwgif(cinfo);
  96.     break;
  97. #endif
  98. #ifdef PPM_SUPPORTED
  99.   case FMT_PPM:
  100.     jselwppm(cinfo);
  101.     break;
  102. #endif
  103. #ifdef RLE_SUPPORTED
  104.   case FMT_RLE:
  105.     jselwrle(cinfo);
  106.     break;
  107. #endif
  108. #ifdef TARGA_SUPPORTED
  109.   case FMT_TARGA:
  110.     jselwtarga(cinfo);
  111.     break;
  112. #endif
  113.   default:
  114.     ERREXIT(cinfo->emethods, "Unsupported output file format");
  115.     break;
  116.   }
  117. }
  118.  
  119.  
  120. LOCAL void
  121. usage (char * progname)
  122. /* complain about bad command line */
  123. {
  124.   fprintf(stderr, "usage: %s ", progname);
  125.   fprintf(stderr, "[-G] [-P] [-R] [-T] [-b] [-g] [-q colors] [-2] [-D] [-d]");
  126. #ifdef TWO_FILE_COMMANDLINE
  127.   fprintf(stderr, " inputfile outputfile\n");
  128. #else
  129.   fprintf(stderr, " [inputfile]\n");
  130. #endif
  131.   exit(2);
  132. }
  133.  
  134.  
  135. /*
  136.  * The main program.
  137.  */
  138.  
  139. GLOBAL void
  140. main (int argc, char **argv)
  141. {
  142.   struct decompress_info_struct cinfo;
  143.   struct decompress_methods_struct dc_methods;
  144.   struct external_methods_struct e_methods;
  145.   int c;
  146.  
  147.   /* On Mac, fetch a command line. */
  148. #ifdef THINK_C
  149.   argc = ccommand(&argv);
  150. #endif
  151.  
  152.   /* Initialize the system-dependent method pointers. */
  153.   cinfo.methods = &dc_methods;
  154.   cinfo.emethods = &e_methods;
  155.   jselerror(&e_methods);    /* error/trace message routines */
  156.   jselvirtmem(&e_methods);    /* memory allocation routines */
  157.   dc_methods.d_ui_method_selection = d_ui_method_selection;
  158.  
  159.   /* Set up default JPEG parameters. */
  160.   j_d_defaults(&cinfo, TRUE);
  161.   requested_fmt = DEFAULT_FMT;    /* set default output file format */
  162.  
  163.   /* Scan command line options, adjust parameters */
  164.   
  165.   while ((c = egetopt(argc, argv, "GPRTbdgq:2D")) != EOF)
  166.     switch (c) {
  167.     case 'G':            /* GIF output format. */
  168.       requested_fmt = FMT_GIF;
  169.       break;
  170.     case 'P':            /* PPM output format. */
  171.       requested_fmt = FMT_PPM;
  172.       break;
  173.     case 'R':            /* RLE output format. */
  174.       requested_fmt = FMT_RLE;
  175.       break;
  176.     case 'T':            /* Targa output format. */
  177.       requested_fmt = FMT_TARGA;
  178.       break;
  179.     case 'b':            /* Enable cross-block smoothing. */
  180.       cinfo.do_block_smoothing = TRUE;
  181.       break;
  182.     case 'd':            /* Debugging. */
  183.       e_methods.trace_level++;
  184.       break;
  185.     case 'g':            /* Force grayscale output. */
  186.       cinfo.out_color_space = CS_GRAYSCALE;
  187.       break;
  188.     case 'q':            /* Do color quantization. */
  189.       { int val;
  190.     if (optarg == NULL)
  191.       usage(argv[0]);
  192.     if (sscanf(optarg, "%d", &val) != 1)
  193.       usage(argv[0]);
  194.     cinfo.desired_number_of_colors = val;
  195.       }
  196.       cinfo.quantize_colors = TRUE;
  197.       break;
  198.     case '2':            /* Use two-pass quantization. */
  199.       cinfo.two_pass_quantize = TRUE;
  200.       break;
  201.     case 'D':            /* Suppress dithering in color quantization. */
  202.       cinfo.use_dithering = FALSE;
  203.       break;
  204.     case '?':
  205.     default:
  206.       usage(argv[0]);
  207.       break;
  208.     }
  209.  
  210.   /* If -d appeared, print version identification */
  211.   if (e_methods.trace_level > 0)
  212.     fprintf(stderr, "Independent JPEG Group's DJPEG, version %s\n%s\n",
  213.         JVERSION, JCOPYRIGHT);
  214.  
  215.   /* Select the input and output files */
  216.  
  217. #ifdef TWO_FILE_COMMANDLINE
  218.  
  219.   if (optind != argc-2) {
  220.     fprintf(stderr, "%s: must name one input and one output file\n", argv[0]);
  221.     usage(argv[0]);
  222.   }
  223.   if ((cinfo.input_file = fopen(argv[optind], READ_BINARY)) == NULL) {
  224.     fprintf(stderr, "%s: can't open %s\n", argv[0], argv[optind]);
  225.     exit(2);
  226.   }
  227.   if ((cinfo.output_file = fopen(argv[optind+1], WRITE_BINARY)) == NULL) {
  228.     fprintf(stderr, "%s: can't open %s\n", argv[0], argv[optind+1]);
  229.     exit(2);
  230.   }
  231.  
  232. #else /* not TWO_FILE_COMMANDLINE -- use Unix style */
  233.  
  234.   cinfo.input_file = stdin;    /* default input file */
  235.   cinfo.output_file = stdout;    /* always the output file */
  236.  
  237.   if (optind < argc-1) {
  238.     fprintf(stderr, "%s: only one input file\n", argv[0]);
  239.     usage(argv[0]);
  240.   }
  241.   if (optind < argc) {
  242.     if ((cinfo.input_file = fopen(argv[optind], READ_BINARY)) == NULL) {
  243.       fprintf(stderr, "%s: can't open %s\n", argv[0], argv[optind]);
  244.       exit(2);
  245.     }
  246.   }
  247.  
  248. #endif /* TWO_FILE_COMMANDLINE */
  249.   
  250.   /* Set up to read a JFIF or baseline-JPEG file. */
  251.   /* A smarter UI would inspect the first few bytes of the input file */
  252.   /* to determine its type. */
  253. #ifdef JFIF_SUPPORTED
  254.   jselrjfif(&cinfo);
  255. #else
  256.   You shoulda defined JFIF_SUPPORTED.   /* deliberate syntax error */
  257. #endif
  258.  
  259.   /* Do it to it! */
  260.   jpeg_decompress(&cinfo);
  261.  
  262.   /* Release memory. */
  263.   j_d_free_defaults(&cinfo, TRUE);
  264. #ifdef MEM_STATS
  265.   if (e_methods.trace_level > 0) /* Optional memory-usage statistics */
  266.     j_mem_stats();
  267. #endif
  268.  
  269.   /* All done. */
  270.   exit(0);
  271. }
  272.