home *** CD-ROM | disk | FTP | other *** search
/ The Datafile PD-CD 4 / DATAFILE_PDCD4.iso / utilities / utilsf / jpegv6 / c / cjpeg < prev    next >
Encoding:
Text File  |  1995-09-29  |  19.3 KB  |  606 lines

  1. /*
  2.  * cjpeg.c
  3.  *
  4.  * Copyright (C) 1991-1995, 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 command-line 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.  * To simplify script writing, the "-outfile" switch is provided.  The syntax
  22.  *    cjpeg [options]  -outfile outputfile  inputfile
  23.  * works regardless of which command line style is used.
  24.  */
  25.  
  26. #include "cdjpeg.h"        /* Common decls for cjpeg/djpeg applications */
  27. #include "jversion.h"        /* for version message */
  28.  
  29. #ifdef USE_CCOMMAND        /* command-line reader for Macintosh */
  30. #ifdef __MWERKS__
  31. #include <SIOUX.h>              /* Metrowerks declares it here */
  32. #endif
  33. #ifdef THINK_C
  34. #include <console.h>        /* Think declares it here */
  35. #endif
  36. #endif
  37.  
  38.  
  39. /* Create the add-on message string table. */
  40.  
  41. #define JMESSAGE(code,string)    string ,
  42.  
  43. static const char * const cdjpeg_message_table[] = {
  44. #include "cderror.h"
  45.   NULL
  46. };
  47.  
  48.  
  49. /*
  50.  * This routine determines what format the input file is,
  51.  * and selects the appropriate input-reading module.
  52.  *
  53.  * To determine which family of input formats the file belongs to,
  54.  * we may look only at the first byte of the file, since C does not
  55.  * guarantee that more than one character can be pushed back with ungetc.
  56.  * Looking at additional bytes would require one of these approaches:
  57.  *     1) assume we can fseek() the input file (fails for piped input);
  58.  *     2) assume we can push back more than one character (works in
  59.  *        some C implementations, but unportable);
  60.  *     3) provide our own buffering (breaks input readers that want to use
  61.  *        stdio directly, such as the RLE library);
  62.  * or  4) don't put back the data, and modify the input_init methods to assume
  63.  *        they start reading after the start of file (also breaks RLE library).
  64.  * #1 is attractive for MS-DOS but is untenable on Unix.
  65.  *
  66.  * The most portable solution for file types that can't be identified by their
  67.  * first byte is to make the user tell us what they are.  This is also the
  68.  * only approach for "raw" file types that contain only arbitrary values.
  69.  * We presently apply this method for Targa files.  Most of the time Targa
  70.  * files start with 0x00, so we recognize that case.  Potentially, however,
  71.  * a Targa file could start with any byte value (byte 0 is the length of the
  72.  * seldom-used ID field), so we provide a switch to force Targa input mode.
  73.  */
  74.  
  75. static boolean is_targa;    /* records user -targa switch */
  76.  
  77.  
  78. LOCAL cjpeg_source_ptr
  79. select_file_type (j_compress_ptr cinfo, FILE * infile)
  80. {
  81.   int c;
  82.  
  83.   if (is_targa) {
  84. #ifdef TARGA_SUPPORTED
  85.     return jinit_read_targa(cinfo);
  86. #else
  87.     ERREXIT(cinfo, JERR_TGA_NOTCOMP);
  88. #endif
  89.   }
  90.  
  91.   if ((c = getc(infile)) == EOF)
  92.     ERREXIT(cinfo, JERR_INPUT_EMPTY);
  93.   if (ungetc(c, infile) == EOF)
  94.     ERREXIT(cinfo, JERR_UNGETC_FAILED);
  95.  
  96.   switch (c) {
  97. #ifdef BMP_SUPPORTED
  98.   case 'B':
  99.     return jinit_read_bmp(cinfo);
  100. #endif
  101. #ifdef GIF_SUPPORTED
  102.   case 'G':
  103.     return jinit_read_gif(cinfo);
  104. #endif
  105. #ifdef PPM_SUPPORTED
  106.   case 'P':
  107.     return jinit_read_ppm(cinfo);
  108. #endif
  109. #ifdef RLE_SUPPORTED
  110.   case 'R':
  111.     return jinit_read_rle(cinfo);
  112. #endif
  113. #ifdef TARGA_SUPPORTED
  114.   case 0x00:
  115.     return jinit_read_targa(cinfo);
  116. #endif
  117.   default:
  118.     ERREXIT(cinfo, JERR_UNKNOWN_FORMAT);
  119.     break;
  120.   }
  121.  
  122.   return NULL;            /* suppress compiler warnings */
  123. }
  124.  
  125.  
  126. /*
  127.  * Argument-parsing code.
  128.  * The switch parser is designed to be useful with DOS-style command line
  129.  * syntax, ie, intermixed switches and file names, where only the switches
  130.  * to the left of a given file name affect processing of that file.
  131.  * The main program in this file doesn't actually use this capability...
  132.  */
  133.  
  134.  
  135. static const char * progname;    /* program name for error messages */
  136. static char * outfilename;    /* for -outfile switch */
  137.  
  138.  
  139. LOCAL void
  140. usage (void)
  141. /* complain about bad command line */
  142. {
  143.   fprintf(stderr, "usage: %s [switches] ", progname);
  144. #ifdef TWO_FILE_COMMANDLINE
  145.   fprintf(stderr, "inputfile outputfile\n");
  146. #else
  147.   fprintf(stderr, "[inputfile]\n");
  148. #endif
  149.  
  150.   fprintf(stderr, "Switches (names may be abbreviated):\n");
  151.   fprintf(stderr, "  -quality N     Compression quality (0..100; 5-95 is useful range)\n");
  152.   fprintf(stderr, "  -grayscale     Create monochrome JPEG file\n");
  153. #ifdef ENTROPY_OPT_SUPPORTED
  154.   fprintf(stderr, "  -optimize      Optimize Huffman table (smaller file, but slow compression)\n");
  155. #endif
  156. #ifdef C_PROGRESSIVE_SUPPORTED
  157.   fprintf(stderr, "  -progressive   Create progressive JPEG file\n");
  158. #endif
  159. #ifdef TARGA_SUPPORTED
  160.   fprintf(stderr, "  -targa         Input file is Targa format (usually not needed)\n");
  161. #endif
  162.   fprintf(stderr, "Switches for advanced users:\n");
  163. #ifdef DCT_ISLOW_SUPPORTED
  164.   fprintf(stderr, "  -dct int       Use integer DCT method%s\n",
  165.       (JDCT_DEFAULT == JDCT_ISLOW ? " (default)" : ""));
  166. #endif
  167. #ifdef DCT_IFAST_SUPPORTED
  168.   fprintf(stderr, "  -dct fast      Use fast integer DCT (less accurate)%s\n",
  169.       (JDCT_DEFAULT == JDCT_IFAST ? " (default)" : ""));
  170. #endif
  171. #ifdef DCT_FLOAT_SUPPORTED
  172.   fprintf(stderr, "  -dct float     Use floating-point DCT method%s\n",
  173.       (JDCT_DEFAULT == JDCT_FLOAT ? " (default)" : ""));
  174. #endif
  175.   fprintf(stderr, "  -restart N     Set restart interval in rows, or in blocks with B\n");
  176. #ifdef INPUT_SMOOTHING_SUPPORTED
  177.   fprintf(stderr, "  -smooth N      Smooth dithered input (N=1..100 is strength)\n");
  178. #endif
  179.   fprintf(stderr, "  -maxmemory N   Maximum memory to use (in kbytes)\n");
  180.   fprintf(stderr, "  -outfile name  Specify name for output file\n");
  181.   fprintf(stderr, "  -verbose  or  -debug   Emit debug output\n");
  182.   fprintf(stderr, "Switches for wizards:\n");
  183. #ifdef C_ARITH_CODING_SUPPORTED
  184.   fprintf(stderr, "  -arithmetic    Use arithmetic coding\n");
  185. #endif
  186.   fprintf(stderr, "  -baseline      Force baseline output\n");
  187.   fprintf(stderr, "  -qtables file  Use quantization tables given in file\n");
  188.   fprintf(stderr, "  -qslots N[,...]    Set component quantization tables\n");
  189.   fprintf(stderr, "  -sample HxV[,...]  Set component sampling factors\n");
  190. #ifdef C_MULTISCAN_FILES_SUPPORTED
  191.   fprintf(stderr, "  -scans file    Create multi-scan JPEG per script file\n");
  192. #endif
  193.   exit(EXIT_FAILURE);
  194. }
  195.  
  196.  
  197. LOCAL int
  198. parse_switches (j_compress_ptr cinfo, int argc, char **argv,
  199.         int last_file_arg_seen, boolean for_real)
  200. /* Parse optional switches.
  201.  * Returns argv[] index of first file-name argument (== argc if none).
  202.  * Any file names with indexes <= last_file_arg_seen are ignored;
  203.  * they have presumably been processed in a previous iteration.
  204.  * (Pass 0 for last_file_arg_seen on the first or only iteration.)
  205.  * for_real is FALSE on the first (dummy) pass; we may skip any expensive
  206.  * processing.
  207.  */
  208. {
  209.   int argn;
  210.   char * arg;
  211.   int quality;            /* -quality parameter */
  212.   int q_scale_factor;        /* scaling percentage for -qtables */
  213.   boolean force_baseline;
  214.   boolean simple_progressive;
  215.   char * qtablefile = NULL;    /* saves -qtables filename if any */
  216.   char * qslotsarg = NULL;    /* saves -qslots parm if any */
  217.   char * samplearg = NULL;    /* saves -sample parm if any */
  218.   char * scansarg = NULL;    /* saves -scans parm if any */
  219.  
  220.   /* Set up default JPEG parameters. */
  221.   /* Note that default -quality level need not, and does not,
  222.    * match the default scaling for an explicit -qtables argument.
  223.    */
  224.   quality = 75;            /* default -quality value */
  225.   q_scale_factor = 100;        /* default to no scaling for -qtables */
  226.   force_baseline = FALSE;    /* by default, allow 16-bit quantizers */
  227.   simple_progressive = FALSE;
  228.   is_targa = FALSE;
  229.   outfilename = NULL;
  230.   cinfo->err->trace_level = 0;
  231.  
  232.   /* Scan command line options, adjust parameters */
  233.  
  234.   for (argn = 1; argn < argc; argn++) {
  235.     arg = argv[argn];
  236.     if (*arg != '-') {
  237.       /* Not a switch, must be a file name argument */
  238.       if (argn <= last_file_arg_seen) {
  239.     outfilename = NULL;    /* -outfile applies to just one input file */
  240.     continue;        /* ignore this name if previously processed */
  241.       }
  242.       break;            /* else done parsing switches */
  243.     }
  244.     arg++;            /* advance past switch marker character */
  245.  
  246.     if (keymatch(arg, "arithmetic", 1)) {
  247.       /* Use arithmetic coding. */
  248. #ifdef C_ARITH_CODING_SUPPORTED
  249.       cinfo->arith_code = TRUE;
  250. #else
  251.       fprintf(stderr, "%s: sorry, arithmetic coding not supported\n",
  252.           progname);
  253.       exit(EXIT_FAILURE);
  254. #endif
  255.  
  256.     } else if (keymatch(arg, "baseline", 1)) {
  257.       /* Force baseline output (8-bit quantizer values). */
  258.       force_baseline = TRUE;
  259.  
  260.     } else if (keymatch(arg, "dct", 2)) {
  261.       /* Select DCT algorithm. */
  262.       if (++argn >= argc)    /* advance to next argument */
  263.     usage();
  264.       if (keymatch(argv[argn], "int", 1)) {
  265.     cinfo->dct_method = JDCT_ISLOW;
  266.       } else if (keymatch(argv[argn], "fast", 2)) {
  267.     cinfo->dct_method = JDCT_IFAST;
  268.       } else if (keymatch(argv[argn], "float", 2)) {
  269.     cinfo->dct_method = JDCT_FLOAT;
  270.       } else
  271.     usage();
  272.  
  273.     } else if (keymatch(arg, "debug", 1) || keymatch(arg, "verbose", 1)) {
  274.       /* Enable debug printouts. */
  275.       /* On first -d, print version identification */
  276.       static boolean printed_version = FALSE;
  277.  
  278.       if (! printed_version) {
  279.     fprintf(stderr, "Independent JPEG Group's CJPEG, version %s\n%s\n",
  280.         JVERSION, JCOPYRIGHT);
  281.     printed_version = TRUE;
  282.       }
  283.       cinfo->err->trace_level++;
  284.  
  285.     } else if (keymatch(arg, "grayscale", 2) || keymatch(arg, "greyscale",2)) {
  286.       /* Force a monochrome JPEG file to be generated. */
  287.       jpeg_set_colorspace(cinfo, JCS_GRAYSCALE);
  288.  
  289.     } else if (keymatch(arg, "maxmemory", 3)) {
  290.       /* Maximum memory in Kb (or Mb with 'm'). */
  291.       long lval;
  292.       char ch = 'x';
  293.  
  294.       if (++argn >= argc)    /* advance to next argument */
  295.     usage();
  296.       if (sscanf(argv[argn], "%ld%c", &lval, &ch) < 1)
  297.     usage();
  298.       if (ch == 'm' || ch == 'M')
  299.     lval *= 1000L;
  300.       cinfo->mem->max_memory_to_use = lval * 1000L;
  301.  
  302.     } else if (keymatch(arg, "optimize", 1) || keymatch(arg, "optimise", 1)) {
  303.       /* Enable entropy parm optimization. */
  304. #ifdef ENTROPY_OPT_SUPPORTED
  305.       cinfo->optimize_coding = TRUE;
  306. #else
  307.       fprintf(stderr, "%s: sorry, entropy optimization was not compiled\n",
  308.           progname);
  309.       exit(EXIT_FAILURE);
  310. #endif
  311.  
  312.     } else if (keymatch(arg, "outfile", 4)) {
  313.       /* Set output file name. */
  314.       if (++argn >= argc)    /* advance to next argument */
  315.     usage();
  316.       outfilename = argv[argn];    /* save it away for later use */
  317.  
  318.     } else if (keymatch(arg, "progressive", 1)) {
  319.       /* Select simple progressive mode. */
  320. #ifdef C_PROGRESSIVE_SUPPORTED
  321.       simple_progressive = TRUE;
  322.       /* We must postpone execution until num_components is known. */
  323. #else
  324.       fprintf(stderr, "%s: sorry, progressive output was not compiled\n",
  325.           progname);
  326.       exit(EXIT_FAILURE);
  327. #endif
  328.  
  329.     } else if (keymatch(arg, "quality", 1)) {
  330.       /* Quality factor (quantization table scaling factor). */
  331.       if (++argn >= argc)    /* advance to next argument */
  332.     usage();
  333.       if (sscanf(argv[argn], "%d", &quality) != 1)
  334.     usage();
  335.       /* Change scale factor in case -qtables is present. */
  336.       q_scale_factor = jpeg_quality_scaling(quality);
  337.  
  338.     } else if (keymatch(arg, "qslots", 2)) {
  339.       /* Quantization table slot numbers. */
  340.       if (++argn >= argc)    /* advance to next argument */
  341.     usage();
  342.       qslotsarg = argv[argn];
  343.       /* Must delay setting qslots until after we have processed any
  344.        * colorspace-determining switches, since jpeg_set_colorspace sets
  345.        * default quant table numbers.
  346.        */
  347.  
  348.     } else if (keymatch(arg, "qtables", 2)) {
  349.       /* Quantization tables fetched from file. */
  350.       if (++argn >= argc)    /* advance to next argument */
  351.     usage();
  352.       qtablefile = argv[argn];
  353.       /* We postpone actually reading the file in case -quality comes later. */
  354.  
  355.     } else if (keymatch(arg, "restart", 1)) {
  356.       /* Restart interval in MCU rows (or in MCUs with 'b'). */
  357.       long lval;
  358.       char ch = 'x';
  359.  
  360.       if (++argn >= argc)    /* advance to next argument */
  361.     usage();
  362.       if (sscanf(argv[argn], "%ld%c", &lval, &ch) < 1)
  363.     usage();
  364.       if (lval < 0 || lval > 65535L)
  365.     usage();
  366.       if (ch == 'b' || ch == 'B') {
  367.     cinfo->restart_interval = (unsigned int) lval;
  368.     cinfo->restart_in_rows = 0; /* else prior '-restart n' overrides me */
  369.       } else {
  370.     cinfo->restart_in_rows = (int) lval;
  371.     /* restart_interval will be computed during startup */
  372.       }
  373.  
  374.     } else if (keymatch(arg, "sample", 2)) {
  375.       /* Set sampling factors. */
  376.       if (++argn >= argc)    /* advance to next argument */
  377.     usage();
  378.       samplearg = argv[argn];
  379.       /* Must delay setting sample factors until after we have processed any
  380.        * colorspace-determining switches, since jpeg_set_colorspace sets
  381.        * default sampling factors.
  382.        */
  383.  
  384.     } else if (keymatch(arg, "scans", 2)) {
  385.       /* Set scan script. */
  386. #ifdef C_MULTISCAN_FILES_SUPPORTED
  387.       if (++argn >= argc)    /* advance to next argument */
  388.     usage();
  389.       scansarg = argv[argn];
  390.       /* We must postpone reading the file in case -progressive appears. */
  391. #else
  392.       fprintf(stderr, "%s: sorry, multi-scan output was not compiled\n",
  393.           progname);
  394.       exit(EXIT_FAILURE);
  395. #endif
  396.  
  397.     } else if (keymatch(arg, "smooth", 2)) {
  398.       /* Set input smoothing factor. */
  399.       int val;
  400.  
  401.       if (++argn >= argc)    /* advance to next argument */
  402.     usage();
  403.       if (sscanf(argv[argn], "%d", &val) != 1)
  404.     usage();
  405.       if (val < 0 || val > 100)
  406.     usage();
  407.       cinfo->smoothing_factor = val;
  408.  
  409.     } else if (keymatch(arg, "targa", 1)) {
  410.       /* Input file is Targa format. */
  411.       is_targa = TRUE;
  412.  
  413.     } else {
  414.       usage();            /* bogus switch */
  415.     }
  416.   }
  417.  
  418.   /* Post-switch-scanning cleanup */
  419.  
  420.   if (for_real) {
  421.  
  422.     /* Set quantization tables for selected quality. */
  423.     /* Some or all may be overridden if -qtables is present. */
  424.     jpeg_set_quality(cinfo, quality, force_baseline);
  425.  
  426.     if (qtablefile != NULL)    /* process -qtables if it was present */
  427.       if (! read_quant_tables(cinfo, qtablefile,
  428.                   q_scale_factor, force_baseline))
  429.     usage();
  430.  
  431.     if (qslotsarg != NULL)    /* process -qslots if it was present */
  432.       if (! set_quant_slots(cinfo, qslotsarg))
  433.     usage();
  434.  
  435.     if (samplearg != NULL)    /* process -sample if it was present */
  436.       if (! set_sample_factors(cinfo, samplearg))
  437.     usage();
  438.  
  439. #ifdef C_PROGRESSIVE_SUPPORTED
  440.     if (simple_progressive)    /* process -progressive; -scans can override */
  441.       jpeg_simple_progression(cinfo);
  442. #endif
  443.  
  444. #ifdef C_MULTISCAN_FILES_SUPPORTED
  445.     if (scansarg != NULL)    /* process -scans if it was present */
  446.       if (! read_scan_script(cinfo, scansarg))
  447.     usage();
  448. #endif
  449.   }
  450.  
  451.   return argn;            /* return index of next arg (file name) */
  452. }
  453.  
  454.  
  455. /*
  456.  * The main program.
  457.  */
  458.  
  459. GLOBAL int
  460. main (int argc, char **argv)
  461. {
  462.   struct jpeg_compress_struct cinfo;
  463.   struct jpeg_error_mgr jerr;
  464. #ifdef PROGRESS_REPORT
  465.   struct cdjpeg_progress_mgr progress;
  466. #endif
  467.   int file_index;
  468.   cjpeg_source_ptr src_mgr;
  469.   FILE * input_file;
  470.   FILE * output_file;
  471.   JDIMENSION num_scanlines;
  472.  
  473.   /* On Mac, fetch a command line. */
  474. #ifdef USE_CCOMMAND
  475.   argc = ccommand(&argv);
  476. #endif
  477.  
  478.   progname = argv[0];
  479.   if (progname == NULL || progname[0] == 0)
  480.     progname = "cjpeg";        /* in case C library doesn't provide it */
  481.  
  482.   /* Initialize the JPEG compression object with default error handling. */
  483.   cinfo.err = jpeg_std_error(&jerr);
  484.   jpeg_create_compress(&cinfo);
  485.   /* Add some application-specific error messages (from cderror.h) */
  486.   jerr.addon_message_table = cdjpeg_message_table;
  487.   jerr.first_addon_message = JMSG_FIRSTADDONCODE;
  488.   jerr.last_addon_message = JMSG_LASTADDONCODE;
  489.  
  490.   /* Now safe to enable signal catcher. */
  491. #ifdef NEED_SIGNAL_CATCHER
  492.   enable_signal_catcher((j_common_ptr) &cinfo);
  493. #endif
  494.  
  495.   /* Initialize JPEG parameters.
  496.    * Much of this may be overridden later.
  497.    * In particular, we don't yet know the input file's color space,
  498.    * but we need to provide some value for jpeg_set_defaults() to work.
  499.    */
  500.  
  501.   cinfo.in_color_space = JCS_RGB; /* arbitrary guess */
  502.   jpeg_set_defaults(&cinfo);
  503.  
  504.   /* Scan command line to find file names.
  505.    * It is convenient to use just one switch-parsing routine, but the switch
  506.    * values read here are ignored; we will rescan the switches after opening
  507.    * the input file.
  508.    */
  509.  
  510.   file_index = parse_switches(&cinfo, argc, argv, 0, FALSE);
  511.  
  512. #ifdef TWO_FILE_COMMANDLINE
  513.   /* Must have either -outfile switch or explicit output file name */
  514.   if (outfilename == NULL) {
  515.     if (file_index != argc-2) {
  516.       fprintf(stderr, "%s: must name one input and one output file\n",
  517.           progname);
  518.       usage();
  519.     }
  520.     outfilename = argv[file_index+1];
  521.   } else {
  522.     if (file_index != argc-1) {
  523.       fprintf(stderr, "%s: must name one input and one output file\n",
  524.           progname);
  525.       usage();
  526.     }
  527.   }
  528. #else
  529.   /* Unix style: expect zero or one file name */
  530.   if (file_index < argc-1) {
  531.     fprintf(stderr, "%s: only one input file\n", progname);
  532.     usage();
  533.   }
  534. #endif /* TWO_FILE_COMMANDLINE */
  535.  
  536.   /* Open the input file. */
  537.   if (file_index < argc) {
  538.     if ((input_file = fopen(argv[file_index], READ_BINARY)) == NULL) {
  539.       fprintf(stderr, "%s: can't open %s\n", progname, argv[file_index]);
  540.       exit(EXIT_FAILURE);
  541.     }
  542.   } else {
  543.     /* default input file is stdin */
  544.     input_file = read_stdin();
  545.   }
  546.  
  547.   /* Open the output file. */
  548.   if (outfilename != NULL) {
  549.     if ((output_file = fopen(outfilename, WRITE_BINARY)) == NULL) {
  550.       fprintf(stderr, "%s: can't open %s\n", progname, outfilename);
  551.       exit(EXIT_FAILURE);
  552.     }
  553.   } else {
  554.     /* default output file is stdout */
  555.     output_file = write_stdout();
  556.   }
  557.  
  558. #ifdef PROGRESS_REPORT
  559.   start_progress_monitor((j_common_ptr) &cinfo, &progress);
  560. #endif
  561.  
  562.   /* Figure out the input file format, and set up to read it. */
  563.   src_mgr = select_file_type(&cinfo, input_file);
  564.   src_mgr->input_file = input_file;
  565.  
  566.   /* Read the input file header to obtain file size & colorspace. */
  567.   (*src_mgr->start_input) (&cinfo, src_mgr);
  568.  
  569.   /* Now that we know input colorspace, fix colorspace-dependent defaults */
  570.   jpeg_default_colorspace(&cinfo);
  571.  
  572.   /* Adjust default compression parameters by re-parsing the options */
  573.   file_index = parse_switches(&cinfo, argc, argv, 0, TRUE);
  574.  
  575.   /* Specify data destination for compression */
  576.   jpeg_stdio_dest(&cinfo, output_file);
  577.  
  578.   /* Start compressor */
  579.   jpeg_start_compress(&cinfo, TRUE);
  580.  
  581.   /* Process data */
  582.   while (cinfo.next_scanline < cinfo.image_height) {
  583.     num_scanlines = (*src_mgr->get_pixel_rows) (&cinfo, src_mgr);
  584.     (void) jpeg_write_scanlines(&cinfo, src_mgr->buffer, num_scanlines);
  585.   }
  586.  
  587.   /* Finish compression and release memory */
  588.   (*src_mgr->finish_input) (&cinfo, src_mgr);
  589.   jpeg_finish_compress(&cinfo);
  590.   jpeg_destroy_compress(&cinfo);
  591.  
  592.   /* Close files, if we opened them */
  593.   if (input_file != stdin)
  594.     fclose(input_file);
  595.   if (output_file != stdout)
  596.     fclose(output_file);
  597.  
  598. #ifdef PROGRESS_REPORT
  599.   end_progress_monitor((j_common_ptr) &cinfo);
  600. #endif
  601.  
  602.   /* All done. */
  603.   exit(jerr.num_warnings ? EXIT_WARNING : EXIT_SUCCESS);
  604.   return 0;            /* suppress no-return-value warnings */
  605. }
  606.