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

  1. /*
  2.  * jcmain.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 compressor.
  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.  *    cjpeg [options]  inputfile outputfile
  14.  *    cjpeg [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 routine determines what format the input file is,
  52.  * and selects the appropriate input-reading module.
  53.  *
  54.  * To determine which family of input formats the file belongs to,
  55.  * we may look only at the first byte of the file, since C does not
  56.  * guarantee that more than one character can be pushed back with ungetc.
  57.  * Looking at additional bytes would require one of these approaches:
  58.  *     1) assume we can fseek() the input file (fails for piped input);
  59.  *     2) assume we can push back more than one character (works in
  60.  *        some C implementations, but unportable);
  61.  *     3) provide our own buffering as is done in djpeg (breaks input readers
  62.  *        that want to use stdio directly, such as the RLE library);
  63.  * or  4) don't put back the data, and modify the input_init methods to assume
  64.  *        they start reading after the start of file (also breaks RLE library).
  65.  * #1 is attractive for MS-DOS but is untenable on Unix.
  66.  *
  67.  * The most portable solution for file types that can't be identified by their
  68.  * first byte is to make the user tell us what they are.  This is also the
  69.  * only approach for "raw" file types that contain only arbitrary values.
  70.  * We presently apply this method for Targa files.  Most of the time Targa
  71.  * files start with 0x00, so we recognize that case.  Potentially, however,
  72.  * a Targa file could start with any byte value (byte 0 is the length of the
  73.  * seldom-used ID field), so we accept a -T switch to force Targa input mode.
  74.  */
  75.  
  76. static boolean is_targa;    /* records user -T switch */
  77.  
  78.  
  79. LOCAL void
  80. select_file_type (compress_info_ptr cinfo)
  81. {
  82.   int c;
  83.  
  84.   if (is_targa) {
  85. #ifdef TARGA_SUPPORTED
  86.     jselrtarga(cinfo);
  87. #else
  88.     ERREXIT(cinfo->emethods, "Targa support was not compiled");
  89. #endif
  90.     return;
  91.   }
  92.  
  93.   if ((c = getc(cinfo->input_file)) == EOF)
  94.     ERREXIT(cinfo->emethods, "Empty input file");
  95.  
  96.   switch (c) {
  97. #ifdef GIF_SUPPORTED
  98.   case 'G':
  99.     jselrgif(cinfo);
  100.     break;
  101. #endif
  102. #ifdef PPM_SUPPORTED
  103.   case 'P':
  104.     jselrppm(cinfo);
  105.     break;
  106. #endif
  107. #ifdef RLE_SUPPORTED
  108.   case 'R':
  109.     jselrrle(cinfo);
  110.     break;
  111. #endif
  112. #ifdef TARGA_SUPPORTED
  113.   case 0x00:
  114.     jselrtarga(cinfo);
  115.     break;
  116. #endif
  117.   default:
  118. #ifdef TARGA_SUPPORTED
  119.     ERREXIT(cinfo->emethods, "Unrecognized input file format --- did you forget -T ?");
  120. #else
  121.     ERREXIT(cinfo->emethods, "Unrecognized input file format");
  122. #endif
  123.     break;
  124.   }
  125.  
  126.   if (ungetc(c, cinfo->input_file) == EOF)
  127.     ERREXIT(cinfo->emethods, "ungetc failed");
  128. }
  129.  
  130.  
  131. /*
  132.  * This routine gets control after the input file header has been read.
  133.  * It must determine what output JPEG file format is to be written,
  134.  * and make any other compression parameter changes that are desirable.
  135.  */
  136.  
  137. METHODDEF void
  138. c_ui_method_selection (compress_info_ptr cinfo)
  139. {
  140.   /* If the input is gray scale, generate a monochrome JPEG file. */
  141.   if (cinfo->in_color_space == CS_GRAYSCALE)
  142.     j_monochrome_default(cinfo);
  143.   /* For now, always select JFIF output format. */
  144. #ifdef JFIF_SUPPORTED
  145.   jselwjfif(cinfo);
  146. #else
  147.   You shoulda defined JFIF_SUPPORTED.   /* deliberate syntax error */
  148. #endif
  149. }
  150.  
  151.  
  152. LOCAL void
  153. usage (char * progname)
  154. /* complain about bad command line */
  155. {
  156.   fprintf(stderr, "usage: %s ", progname);
  157.   fprintf(stderr, "[-Q quality 0..100] [-o] [-T] [-I] [-a] [-d]");
  158. #ifdef TWO_FILE_COMMANDLINE
  159.   fprintf(stderr, " inputfile outputfile\n");
  160. #else
  161.   fprintf(stderr, " [inputfile]\n");
  162. #endif
  163.   exit(2);
  164. }
  165.  
  166.  
  167. /*
  168.  * The main program.
  169.  */
  170.  
  171. GLOBAL void
  172. main (int argc, char **argv)
  173. {
  174.   struct compress_info_struct cinfo;
  175.   struct compress_methods_struct c_methods;
  176.   struct external_methods_struct e_methods;
  177.   int c;
  178.  
  179.   /* On Mac, fetch a command line. */
  180. #ifdef THINK_C
  181.   argc = ccommand(&argv);
  182. #endif
  183.  
  184.   /* Initialize the system-dependent method pointers. */
  185.   cinfo.methods = &c_methods;
  186.   cinfo.emethods = &e_methods;
  187.   jselerror(&e_methods);    /* error/trace message routines */
  188.   jselvirtmem(&e_methods);    /* memory allocation routines */
  189.   c_methods.c_ui_method_selection = c_ui_method_selection;
  190.  
  191.   /* Set up default JPEG parameters. */
  192.   j_c_defaults(&cinfo, 75, FALSE); /* default quality level = 75 */
  193.   is_targa = FALSE;
  194.  
  195.   /* Scan command line options, adjust parameters */
  196.   
  197.   while ((c = egetopt(argc, argv, "IQ:Taod")) != EOF)
  198.     switch (c) {
  199.     case 'I':            /* Create noninterleaved file. */
  200. #ifdef MULTISCAN_FILES_SUPPORTED
  201.       cinfo.interleave = FALSE;
  202. #else
  203.       fprintf(stderr, "%s: sorry, multiple-scan support was not compiled\n",
  204.           argv[0]);
  205.       exit(2);
  206. #endif
  207.       break;
  208.     case 'Q':            /* Quality factor. */
  209.       { int val;
  210.     if (optarg == NULL)
  211.       usage(argv[0]);
  212.     if (sscanf(optarg, "%d", &val) != 1)
  213.       usage(argv[0]);
  214.     /* Note: for now, we make force_baseline FALSE.
  215.      * This means non-baseline JPEG files can be created with low Q values.
  216.      * To ensure only baseline files are generated, pass TRUE instead.
  217.      */
  218.     j_set_quality(&cinfo, val, FALSE);
  219.       }
  220.       break;
  221.     case 'T':            /* Input file is Targa format. */
  222.       is_targa = TRUE;
  223.       break;
  224.     case 'a':            /* Use arithmetic coding. */
  225. #ifdef ARITH_CODING_SUPPORTED
  226.       cinfo.arith_code = TRUE;
  227. #else
  228.       fprintf(stderr, "%s: sorry, arithmetic coding not supported\n",
  229.           argv[0]);
  230.       exit(2);
  231. #endif
  232.       break;
  233.     case 'o':            /* Enable entropy parm optimization. */
  234. #ifdef ENTROPY_OPT_SUPPORTED
  235.       cinfo.optimize_coding = TRUE;
  236. #else
  237.       fprintf(stderr, "%s: sorry, entropy optimization was not compiled\n",
  238.           argv[0]);
  239.       exit(2);
  240. #endif
  241.       break;
  242.     case 'd':            /* Debugging. */
  243.       e_methods.trace_level++;
  244.       break;
  245.     case '?':
  246.     default:
  247.       usage(argv[0]);
  248.       break;
  249.     }
  250.  
  251.   /* If -d appeared, print version identification */
  252.   if (e_methods.trace_level > 0)
  253.     fprintf(stderr, "Independent JPEG Group's CJPEG, version %s\n%s\n",
  254.         JVERSION, JCOPYRIGHT);
  255.  
  256.   /* Select the input and output files */
  257.  
  258. #ifdef TWO_FILE_COMMANDLINE
  259.  
  260.   if (optind != argc-2) {
  261.     fprintf(stderr, "%s: must name one input and one output file\n", argv[0]);
  262.     usage(argv[0]);
  263.   }
  264.   if ((cinfo.input_file = fopen(argv[optind], READ_BINARY)) == NULL) {
  265.     fprintf(stderr, "%s: can't open %s\n", argv[0], argv[optind]);
  266.     exit(2);
  267.   }
  268.   if ((cinfo.output_file = fopen(argv[optind+1], WRITE_BINARY)) == NULL) {
  269.     fprintf(stderr, "%s: can't open %s\n", argv[0], argv[optind+1]);
  270.     exit(2);
  271.   }
  272.  
  273. #else /* not TWO_FILE_COMMANDLINE -- use Unix style */
  274.  
  275.   cinfo.input_file = stdin;    /* default input file */
  276.   cinfo.output_file = stdout;    /* always the output file */
  277.  
  278.   if (optind < argc-1) {
  279.     fprintf(stderr, "%s: only one input file\n", argv[0]);
  280.     usage(argv[0]);
  281.   }
  282.   if (optind < argc) {
  283.     if ((cinfo.input_file = fopen(argv[optind], READ_BINARY)) == NULL) {
  284.       fprintf(stderr, "%s: can't open %s\n", argv[0], argv[optind]);
  285.       exit(2);
  286.     }
  287.   }
  288.  
  289. #endif /* TWO_FILE_COMMANDLINE */
  290.  
  291.   /* Figure out the input file format, and set up to read it. */
  292.   select_file_type(&cinfo);
  293.  
  294.   /* Do it to it! */
  295.   jpeg_compress(&cinfo);
  296.  
  297.   /* Release memory. */
  298.   j_c_free_defaults(&cinfo);
  299. #ifdef MEM_STATS
  300.   if (e_methods.trace_level > 0) /* Optional memory-usage statistics */
  301.     j_mem_stats();
  302. #endif
  303.  
  304.   /* All done. */
  305.   exit(0);
  306. }
  307.