home *** CD-ROM | disk | FTP | other *** search
/ Photo CD Demo 1 / Demo.bin / compresn / jpegv3sr / jcmain.c < prev    next >
C/C++ Source or Header  |  1992-03-17  |  10KB  |  359 lines

  1. /*
  2.  * jcmain.c
  3.  *
  4.  * Copyright (C) 1991, 1992, 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. #ifdef NEED_SIGNAL_CATCHER
  28. #include <signal.h>        /* to declare signal() */
  29. #endif
  30.  
  31. #ifdef THINK_C
  32. #include <console.h>        /* command-line reader for Macintosh */
  33. #endif
  34.  
  35. #ifdef DONT_USE_B_MODE        /* define mode parameters for fopen() */
  36. #define READ_BINARY    "r"
  37. #define WRITE_BINARY    "w"
  38. #else
  39. #define READ_BINARY    "rb"
  40. #define WRITE_BINARY    "wb"
  41. #endif
  42.  
  43. #ifndef EXIT_FAILURE        /* define exit() codes if not provided */
  44. #define EXIT_FAILURE  1
  45. #endif
  46. #ifndef EXIT_SUCCESS
  47. #ifdef VMS
  48. #define EXIT_SUCCESS  1        /* VMS is very nonstandard */
  49. #else
  50. #define EXIT_SUCCESS  0
  51. #endif
  52. #endif
  53.  
  54.  
  55. #include "jversion.h"        /* for version message */
  56.  
  57.  
  58. /*
  59.  * PD version of getopt(3).
  60.  */
  61.  
  62. #include "egetopt.c"
  63.  
  64.  
  65. /*
  66.  * This routine determines what format the input file is,
  67.  * and selects the appropriate input-reading module.
  68.  *
  69.  * To determine which family of input formats the file belongs to,
  70.  * we may look only at the first byte of the file, since C does not
  71.  * guarantee that more than one character can be pushed back with ungetc.
  72.  * Looking at additional bytes would require one of these approaches:
  73.  *     1) assume we can fseek() the input file (fails for piped input);
  74.  *     2) assume we can push back more than one character (works in
  75.  *        some C implementations, but unportable);
  76.  *     3) provide our own buffering as is done in djpeg (breaks input readers
  77.  *        that want to use stdio directly, such as the RLE library);
  78.  * or  4) don't put back the data, and modify the input_init methods to assume
  79.  *        they start reading after the start of file (also breaks RLE library).
  80.  * #1 is attractive for MS-DOS but is untenable on Unix.
  81.  *
  82.  * The most portable solution for file types that can't be identified by their
  83.  * first byte is to make the user tell us what they are.  This is also the
  84.  * only approach for "raw" file types that contain only arbitrary values.
  85.  * We presently apply this method for Targa files.  Most of the time Targa
  86.  * files start with 0x00, so we recognize that case.  Potentially, however,
  87.  * a Targa file could start with any byte value (byte 0 is the length of the
  88.  * seldom-used ID field), so we accept a -T switch to force Targa input mode.
  89.  */
  90.  
  91. static boolean is_targa;    /* records user -T switch */
  92.  
  93.  
  94. LOCAL void
  95. select_file_type (compress_info_ptr cinfo)
  96. {
  97.   int c;
  98.  
  99.   if (is_targa) {
  100. #ifdef TARGA_SUPPORTED
  101.     jselrtarga(cinfo);
  102. #else
  103.     ERREXIT(cinfo->emethods, "Targa support was not compiled");
  104. #endif
  105.     return;
  106.   }
  107.  
  108.   if ((c = getc(cinfo->input_file)) == EOF)
  109.     ERREXIT(cinfo->emethods, "Empty input file");
  110.  
  111.   switch (c) {
  112. #ifdef GIF_SUPPORTED
  113.   case 'G':
  114.     jselrgif(cinfo);
  115.     break;
  116. #endif
  117. #ifdef PPM_SUPPORTED
  118.   case 'P':
  119.     jselrppm(cinfo);
  120.     break;
  121. #endif
  122. #ifdef RLE_SUPPORTED
  123.   case 'R':
  124.     jselrrle(cinfo);
  125.     break;
  126. #endif
  127. #ifdef TARGA_SUPPORTED
  128.   case 0x00:
  129.     jselrtarga(cinfo);
  130.     break;
  131. #endif
  132.   default:
  133. #ifdef TARGA_SUPPORTED
  134.     ERREXIT(cinfo->emethods, "Unrecognized input file format --- did you forget -T ?");
  135. #else
  136.     ERREXIT(cinfo->emethods, "Unrecognized input file format");
  137. #endif
  138.     break;
  139.   }
  140.  
  141.   if (ungetc(c, cinfo->input_file) == EOF)
  142.     ERREXIT(cinfo->emethods, "ungetc failed");
  143. }
  144.  
  145.  
  146. /*
  147.  * This routine gets control after the input file header has been read.
  148.  * It must determine what output JPEG file format is to be written,
  149.  * and make any other compression parameter changes that are desirable.
  150.  */
  151.  
  152. METHODDEF void
  153. c_ui_method_selection (compress_info_ptr cinfo)
  154. {
  155.   /* If the input is gray scale, generate a monochrome JPEG file. */
  156.   if (cinfo->in_color_space == CS_GRAYSCALE)
  157.     j_monochrome_default(cinfo);
  158.   /* For now, always select JFIF output format. */
  159. #ifdef JFIF_SUPPORTED
  160.   jselwjfif(cinfo);
  161. #else
  162.   You shoulda defined JFIF_SUPPORTED.   /* deliberate syntax error */
  163. #endif
  164. }
  165.  
  166.  
  167. /*
  168.  * Signal catcher to ensure that temporary files are removed before aborting.
  169.  * NB: for Amiga Manx C this is actually a global routine named _abort();
  170.  * see -Dsignal_catcher=_abort in CFLAGS.  Talk about bogus...
  171.  */
  172.  
  173. #ifdef NEED_SIGNAL_CATCHER
  174.  
  175. static external_methods_ptr emethods; /* for access to free_all */
  176.  
  177. GLOBAL void
  178. signal_catcher (int signum)
  179. {
  180.   emethods->trace_level = 0;    /* turn off trace output */
  181.   (*emethods->free_all) ();    /* clean up memory allocation & temp files */
  182.   exit(EXIT_FAILURE);
  183. }
  184.  
  185. #endif
  186.  
  187.  
  188. LOCAL void
  189. usage (char * progname)
  190. /* complain about bad command line */
  191. {
  192.   fprintf(stderr, "usage: %s ", progname);
  193.   fprintf(stderr, "[-Q quality 0..100] [-o] [-T] [-I] [-a] [-d] [-m mem]");
  194. #ifdef TWO_FILE_COMMANDLINE
  195.   fprintf(stderr, " inputfile outputfile\n");
  196. #else
  197.   fprintf(stderr, " [inputfile]\n");
  198. #endif
  199.   exit(EXIT_FAILURE);
  200. }
  201.  
  202.  
  203. /*
  204.  * The main program.
  205.  */
  206.  
  207. GLOBAL int
  208. main (int argc, char **argv)
  209. {
  210.   struct compress_info_struct cinfo;
  211.   struct compress_methods_struct c_methods;
  212.   struct external_methods_struct e_methods;
  213.   int c;
  214.  
  215.   /* On Mac, fetch a command line. */
  216. #ifdef THINK_C
  217.   argc = ccommand(&argv);
  218. #endif
  219.  
  220.   /* Initialize the system-dependent method pointers. */
  221.   cinfo.methods = &c_methods;
  222.   cinfo.emethods = &e_methods;
  223.   jselerror(&e_methods);    /* error/trace message routines */
  224.   jselmemmgr(&e_methods);    /* memory allocation routines */
  225.   c_methods.c_ui_method_selection = c_ui_method_selection;
  226.  
  227.   /* Now OK to enable signal catcher. */
  228. #ifdef NEED_SIGNAL_CATCHER
  229.   emethods = &e_methods;
  230.   signal(SIGINT, signal_catcher);
  231. #ifdef SIGTERM            /* not all systems have SIGTERM */
  232.   signal(SIGTERM, signal_catcher);
  233. #endif
  234. #endif
  235.  
  236.   /* Set up default JPEG parameters. */
  237.   j_c_defaults(&cinfo, 75, FALSE); /* default quality level = 75 */
  238.   is_targa = FALSE;
  239.  
  240.   /* Scan command line options, adjust parameters */
  241.   
  242.   while ((c = egetopt(argc, argv, "IQ:Taom:d")) != EOF)
  243.     switch (c) {
  244.     case 'I':            /* Create noninterleaved file. */
  245. #ifdef MULTISCAN_FILES_SUPPORTED
  246.       cinfo.interleave = FALSE;
  247. #else
  248.       fprintf(stderr, "%s: sorry, multiple-scan support was not compiled\n",
  249.           argv[0]);
  250.       exit(EXIT_FAILURE);
  251. #endif
  252.       break;
  253.     case 'Q':            /* Quality factor. */
  254.       { int val;
  255.     if (optarg == NULL)
  256.       usage(argv[0]);
  257.     if (sscanf(optarg, "%d", &val) != 1)
  258.       usage(argv[0]);
  259.     /* Note: for now, we make force_baseline FALSE.
  260.      * This means non-baseline JPEG files can be created with low Q values.
  261.      * To ensure only baseline files are generated, pass TRUE instead.
  262.      */
  263.     j_set_quality(&cinfo, val, FALSE);
  264.       }
  265.       break;
  266.     case 'T':            /* Input file is Targa format. */
  267.       is_targa = TRUE;
  268.       break;
  269.     case 'a':            /* Use arithmetic coding. */
  270. #ifdef ARITH_CODING_SUPPORTED
  271.       cinfo.arith_code = TRUE;
  272. #else
  273.       fprintf(stderr, "%s: sorry, arithmetic coding not supported\n",
  274.           argv[0]);
  275.       exit(EXIT_FAILURE);
  276. #endif
  277.       break;
  278.     case 'o':            /* Enable entropy parm optimization. */
  279. #ifdef ENTROPY_OPT_SUPPORTED
  280.       cinfo.optimize_coding = TRUE;
  281. #else
  282.       fprintf(stderr, "%s: sorry, entropy optimization was not compiled\n",
  283.           argv[0]);
  284.       exit(EXIT_FAILURE);
  285. #endif
  286.       break;
  287.     case 'm':            /* Maximum memory in Kb (or Mb with 'm'). */
  288.       { long lval;
  289.     char ch = 'x';
  290.  
  291.     if (optarg == NULL)
  292.       usage(argv[0]);
  293.     if (sscanf(optarg, "%ld%c", &lval, &ch) < 1)
  294.       usage(argv[0]);
  295.     if (ch == 'm' || ch == 'M')
  296.       lval *= 1000L;
  297.     e_methods.max_memory_to_use = lval * 1000L;
  298.       }
  299.       break;
  300.     case 'd':            /* Debugging. */
  301.       e_methods.trace_level++;
  302.       break;
  303.     case '?':
  304.     default:
  305.       usage(argv[0]);
  306.       break;
  307.     }
  308.  
  309.   /* If -d appeared, print version identification */
  310.   if (e_methods.trace_level > 0)
  311.     fprintf(stderr, "Independent JPEG Group's CJPEG, version %s\n%s\n",
  312.         JVERSION, JCOPYRIGHT);
  313.  
  314.   /* Select the input and output files */
  315.  
  316. #ifdef TWO_FILE_COMMANDLINE
  317.  
  318.   if (optind != argc-2) {
  319.     fprintf(stderr, "%s: must name one input and one output file\n", argv[0]);
  320.     usage(argv[0]);
  321.   }
  322.   if ((cinfo.input_file = fopen(argv[optind], READ_BINARY)) == NULL) {
  323.     fprintf(stderr, "%s: can't open %s\n", argv[0], argv[optind]);
  324.     exit(EXIT_FAILURE);
  325.   }
  326.   if ((cinfo.output_file = fopen(argv[optind+1], WRITE_BINARY)) == NULL) {
  327.     fprintf(stderr, "%s: can't open %s\n", argv[0], argv[optind+1]);
  328.     exit(EXIT_FAILURE);
  329.   }
  330.  
  331. #else /* not TWO_FILE_COMMANDLINE -- use Unix style */
  332.  
  333.   cinfo.input_file = stdin;    /* default input file */
  334.   cinfo.output_file = stdout;    /* always the output file */
  335.  
  336.   if (optind < argc-1) {
  337.     fprintf(stderr, "%s: only one input file\n", argv[0]);
  338.     usage(argv[0]);
  339.   }
  340.   if (optind < argc) {
  341.     if ((cinfo.input_file = fopen(argv[optind], READ_BINARY)) == NULL) {
  342.       fprintf(stderr, "%s: can't open %s\n", argv[0], argv[optind]);
  343.       exit(EXIT_FAILURE);
  344.     }
  345.   }
  346.  
  347. #endif /* TWO_FILE_COMMANDLINE */
  348.  
  349.   /* Figure out the input file format, and set up to read it. */
  350.   select_file_type(&cinfo);
  351.  
  352.   /* Do it to it! */
  353.   jpeg_compress(&cinfo);
  354.  
  355.   /* All done. */
  356.   exit(EXIT_SUCCESS);
  357.   return 0;            /* suppress no-return-value warnings */
  358. }
  359.