Main Page | Class Hierarchy | Alphabetical List | Data Structures | Directories | File List | Data Fields | Globals

jpegtran.c

Go to the documentation of this file.
00001 /*
00002  * jpegtran.c
00003  *
00004  * Copyright (C) 1995, Thomas G. Lane.
00005  * This file is part of the Independent JPEG Group's software.
00006  * For conditions of distribution and use, see the accompanying README file.
00007  *
00008  * This file contains a command-line user interface for JPEG transcoding.
00009  * It is very similar to cjpeg.c, but provides lossless transcoding between
00010  * different JPEG file formats.
00011  */
00012 
00013 #include "cdjpeg.h"     /* Common decls for cjpeg/djpeg applications */
00014 #include "jversion.h"       /* for version message */
00015 
00016 #ifdef USE_CCOMMAND     /* command-line reader for Macintosh */
00017 #ifdef __MWERKS__
00018 #include <SIOUX.h>              /* Metrowerks declares it here */
00019 #endif
00020 #ifdef THINK_C
00021 #include <console.h>        /* Think declares it here */
00022 #endif
00023 #endif
00024 
00025 
00026 /*
00027  * Argument-parsing code.
00028  * The switch parser is designed to be useful with DOS-style command line
00029  * syntax, ie, intermixed switches and file names, where only the switches
00030  * to the left of a given file name affect processing of that file.
00031  * The main program in this file doesn't actually use this capability...
00032  */
00033 
00034 
00035 static const char * progname;   /* program name for error messages */
00036 static char * outfilename;  /* for -outfile switch */
00037 
00038 
00039 LOCAL void
00040 usage (void)
00041 /* complain about bad command line */
00042 {
00043   fprintf(stderr, "usage: %s [switches] ", progname);
00044 #ifdef TWO_FILE_COMMANDLINE
00045   fprintf(stderr, "inputfile outputfile\n");
00046 #else
00047   fprintf(stderr, "[inputfile]\n");
00048 #endif
00049 
00050   fprintf(stderr, "Switches (names may be abbreviated):\n");
00051 #ifdef ENTROPY_OPT_SUPPORTED
00052   fprintf(stderr, "  -optimize      Optimize Huffman table (smaller file, but slow compression)\n");
00053 #endif
00054 #ifdef C_PROGRESSIVE_SUPPORTED
00055   fprintf(stderr, "  -progressive   Create progressive JPEG file\n");
00056 #endif
00057   fprintf(stderr, "Switches for advanced users:\n");
00058   fprintf(stderr, "  -restart N     Set restart interval in rows, or in blocks with B\n");
00059   fprintf(stderr, "  -maxmemory N   Maximum memory to use (in kbytes)\n");
00060   fprintf(stderr, "  -outfile name  Specify name for output file\n");
00061   fprintf(stderr, "  -verbose  or  -debug   Emit debug output\n");
00062   fprintf(stderr, "Switches for wizards:\n");
00063 #ifdef C_ARITH_CODING_SUPPORTED
00064   fprintf(stderr, "  -arithmetic    Use arithmetic coding\n");
00065 #endif
00066 #ifdef C_MULTISCAN_FILES_SUPPORTED
00067   fprintf(stderr, "  -scans file    Create multi-scan JPEG per script file\n");
00068 #endif
00069   exit(EXIT_FAILURE);
00070 }
00071 
00072 
00073 LOCAL int
00074 parse_switches (j_compress_ptr cinfo, int argc, char **argv,
00075         int last_file_arg_seen, boolean for_real)
00076 /* Parse optional switches.
00077  * Returns argv[] index of first file-name argument (== argc if none).
00078  * Any file names with indexes <= last_file_arg_seen are ignored;
00079  * they have presumably been processed in a previous iteration.
00080  * (Pass 0 for last_file_arg_seen on the first or only iteration.)
00081  * for_real is FALSE on the first (dummy) pass; we may skip any expensive
00082  * processing.
00083  */
00084 {
00085   int argn;
00086   char * arg;
00087   boolean simple_progressive;
00088   char * scansarg = NULL;   /* saves -scans parm if any */
00089 
00090   /* Set up default JPEG parameters. */
00091   simple_progressive = FALSE;
00092   outfilename = NULL;
00093   cinfo->err->trace_level = 0;
00094 
00095   /* Scan command line options, adjust parameters */
00096 
00097   for (argn = 1; argn < argc; argn++) {
00098     arg = argv[argn];
00099     if (*arg != '-') {
00100       /* Not a switch, must be a file name argument */
00101       if (argn <= last_file_arg_seen) {
00102     outfilename = NULL; /* -outfile applies to just one input file */
00103     continue;       /* ignore this name if previously processed */
00104       }
00105       break;            /* else done parsing switches */
00106     }
00107     arg++;          /* advance past switch marker character */
00108 
00109     if (keymatch(arg, "arithmetic", 1)) {
00110       /* Use arithmetic coding. */
00111 #ifdef C_ARITH_CODING_SUPPORTED
00112       cinfo->arith_code = TRUE;
00113 #else
00114       fprintf(stderr, "%s: sorry, arithmetic coding not supported\n",
00115           progname);
00116       exit(EXIT_FAILURE);
00117 #endif
00118 
00119     } else if (keymatch(arg, "debug", 1) || keymatch(arg, "verbose", 1)) {
00120       /* Enable debug printouts. */
00121       /* On first -d, print version identification */
00122       static boolean printed_version = FALSE;
00123 
00124       if (! printed_version) {
00125     fprintf(stderr, "Independent JPEG Group's JPEGTRAN, version %s\n%s\n",
00126         JVERSION, JCOPYRIGHT);
00127     printed_version = TRUE;
00128       }
00129       cinfo->err->trace_level++;
00130 
00131     } else if (keymatch(arg, "maxmemory", 3)) {
00132       /* Maximum memory in Kb (or Mb with 'm'). */
00133       long lval;
00134       char ch = 'x';
00135 
00136       if (++argn >= argc)   /* advance to next argument */
00137     usage();
00138       if (sscanf(argv[argn], "%ld%c", &lval, &ch) < 1)
00139     usage();
00140       if (ch == 'm' || ch == 'M')
00141     lval *= 1000L;
00142       cinfo->mem->max_memory_to_use = lval * 1000L;
00143 
00144     } else if (keymatch(arg, "optimize", 1) || keymatch(arg, "optimise", 1)) {
00145       /* Enable entropy parm optimization. */
00146 #ifdef ENTROPY_OPT_SUPPORTED
00147       cinfo->optimize_coding = TRUE;
00148 #else
00149       fprintf(stderr, "%s: sorry, entropy optimization was not compiled\n",
00150           progname);
00151       exit(EXIT_FAILURE);
00152 #endif
00153 
00154     } else if (keymatch(arg, "outfile", 4)) {
00155       /* Set output file name. */
00156       if (++argn >= argc)   /* advance to next argument */
00157     usage();
00158       outfilename = argv[argn]; /* save it away for later use */
00159 
00160     } else if (keymatch(arg, "progressive", 1)) {
00161       /* Select simple progressive mode. */
00162 #ifdef C_PROGRESSIVE_SUPPORTED
00163       simple_progressive = TRUE;
00164       /* We must postpone execution until num_components is known. */
00165 #else
00166       fprintf(stderr, "%s: sorry, progressive output was not compiled\n",
00167           progname);
00168       exit(EXIT_FAILURE);
00169 #endif
00170 
00171     } else if (keymatch(arg, "restart", 1)) {
00172       /* Restart interval in MCU rows (or in MCUs with 'b'). */
00173       long lval;
00174       char ch = 'x';
00175 
00176       if (++argn >= argc)   /* advance to next argument */
00177     usage();
00178       if (sscanf(argv[argn], "%ld%c", &lval, &ch) < 1)
00179     usage();
00180       if (lval < 0 || lval > 65535L)
00181     usage();
00182       if (ch == 'b' || ch == 'B') {
00183     cinfo->restart_interval = (unsigned int) lval;
00184     cinfo->restart_in_rows = 0; /* else prior '-restart n' overrides me */
00185       } else {
00186     cinfo->restart_in_rows = (int) lval;
00187     /* restart_interval will be computed during startup */
00188       }
00189 
00190     } else if (keymatch(arg, "scans", 2)) {
00191       /* Set scan script. */
00192 #ifdef C_MULTISCAN_FILES_SUPPORTED
00193       if (++argn >= argc)   /* advance to next argument */
00194     usage();
00195       scansarg = argv[argn];
00196       /* We must postpone reading the file in case -progressive appears. */
00197 #else
00198       fprintf(stderr, "%s: sorry, multi-scan output was not compiled\n",
00199           progname);
00200       exit(EXIT_FAILURE);
00201 #endif
00202 
00203     } else {
00204       usage();          /* bogus switch */
00205     }
00206   }
00207 
00208   /* Post-switch-scanning cleanup */
00209 
00210   if (for_real) {
00211 
00212 #ifdef C_PROGRESSIVE_SUPPORTED
00213     if (simple_progressive) /* process -progressive; -scans can override */
00214       jpeg_simple_progression(cinfo);
00215 #endif
00216 
00217 #ifdef C_MULTISCAN_FILES_SUPPORTED
00218     if (scansarg != NULL)   /* process -scans if it was present */
00219       if (! read_scan_script(cinfo, scansarg))
00220     usage();
00221 #endif
00222   }
00223 
00224   return argn;          /* return index of next arg (file name) */
00225 }
00226 
00227 
00228 /*
00229  * The main program.
00230  */
00231 
00232 GLOBAL int
00233 main (int argc, char **argv)
00234 {
00235   struct jpeg_decompress_struct srcinfo;
00236   struct jpeg_compress_struct dstinfo;
00237   struct jpeg_error_mgr jsrcerr, jdsterr;
00238 #ifdef PROGRESS_REPORT
00239   struct cdjpeg_progress_mgr progress;
00240 #endif
00241   jvirt_barray_ptr * coef_arrays;
00242   int file_index;
00243   FILE * input_file;
00244   FILE * output_file;
00245 
00246   /* On Mac, fetch a command line. */
00247 #ifdef USE_CCOMMAND
00248   argc = ccommand(&argv);
00249 #endif
00250 
00251   progname = argv[0];
00252   if (progname == NULL || progname[0] == 0)
00253     progname = "jpegtran";  /* in case C library doesn't provide it */
00254 
00255   /* Initialize the JPEG decompression object with default error handling. */
00256   srcinfo.err = jpeg_std_error(&jsrcerr);
00257   jpeg_create_decompress(&srcinfo);
00258   /* Initialize the JPEG compression object with default error handling. */
00259   dstinfo.err = jpeg_std_error(&jdsterr);
00260   jpeg_create_compress(&dstinfo);
00261 
00262   /* Now safe to enable signal catcher.
00263    * Note: we assume only the decompression object will have virtual arrays.
00264    */
00265 #ifdef NEED_SIGNAL_CATCHER
00266   enable_signal_catcher((j_common_ptr) &srcinfo);
00267 #endif
00268 
00269   /* Scan command line to find file names.
00270    * It is convenient to use just one switch-parsing routine, but the switch
00271    * values read here are ignored; we will rescan the switches after opening
00272    * the input file.
00273    */
00274 
00275   file_index = parse_switches(&dstinfo, argc, argv, 0, FALSE);
00276   jsrcerr.trace_level = jdsterr.trace_level;
00277 
00278 #ifdef TWO_FILE_COMMANDLINE
00279   /* Must have either -outfile switch or explicit output file name */
00280   if (outfilename == NULL) {
00281     if (file_index != argc-2) {
00282       fprintf(stderr, "%s: must name one input and one output file\n",
00283           progname);
00284       usage();
00285     }
00286     outfilename = argv[file_index+1];
00287   } else {
00288     if (file_index != argc-1) {
00289       fprintf(stderr, "%s: must name one input and one output file\n",
00290           progname);
00291       usage();
00292     }
00293   }
00294 #else
00295   /* Unix style: expect zero or one file name */
00296   if (file_index < argc-1) {
00297     fprintf(stderr, "%s: only one input file\n", progname);
00298     usage();
00299   }
00300 #endif /* TWO_FILE_COMMANDLINE */
00301 
00302   /* Open the input file. */
00303   if (file_index < argc) {
00304     if ((input_file = fopen(argv[file_index], READ_BINARY)) == NULL) {
00305       fprintf(stderr, "%s: can't open %s\n", progname, argv[file_index]);
00306       exit(EXIT_FAILURE);
00307     }
00308   } else {
00309     /* default input file is stdin */
00310     input_file = read_stdin();
00311   }
00312 
00313   /* Open the output file. */
00314   if (outfilename != NULL) {
00315     if ((output_file = fopen(outfilename, WRITE_BINARY)) == NULL) {
00316       fprintf(stderr, "%s: can't open %s\n", progname, outfilename);
00317       exit(EXIT_FAILURE);
00318     }
00319   } else {
00320     /* default output file is stdout */
00321     output_file = write_stdout();
00322   }
00323 
00324 #ifdef PROGRESS_REPORT
00325   start_progress_monitor((j_common_ptr) &dstinfo, &progress);
00326 #endif
00327 
00328   /* Specify data source for decompression */
00329   jpeg_stdio_src(&srcinfo, input_file);
00330 
00331   /* Read file header */
00332   (void) jpeg_read_header(&srcinfo, TRUE);
00333 
00334   /* Read source file as DCT coefficients */
00335   coef_arrays = jpeg_read_coefficients(&srcinfo);
00336 
00337   /* Initialize destination compression parameters from source values */
00338   jpeg_copy_critical_parameters(&srcinfo, &dstinfo);
00339 
00340   /* Adjust default compression parameters by re-parsing the options */
00341   file_index = parse_switches(&dstinfo, argc, argv, 0, TRUE);
00342 
00343   /* Specify data destination for compression */
00344   jpeg_stdio_dest(&dstinfo, output_file);
00345 
00346   /* Start compressor */
00347   jpeg_write_coefficients(&dstinfo, coef_arrays);
00348 
00349   /* ought to copy source comments here... */
00350 
00351   /* Finish compression and release memory */
00352   jpeg_finish_compress(&dstinfo);
00353   jpeg_destroy_compress(&dstinfo);
00354   (void) jpeg_finish_decompress(&srcinfo);
00355   jpeg_destroy_decompress(&srcinfo);
00356 
00357   /* Close files, if we opened them */
00358   if (input_file != stdin)
00359     fclose(input_file);
00360   if (output_file != stdout)
00361     fclose(output_file);
00362 
00363 #ifdef PROGRESS_REPORT
00364   end_progress_monitor((j_common_ptr) &dstinfo);
00365 #endif
00366 
00367   /* All done. */
00368   exit(jsrcerr.num_warnings + jdsterr.num_warnings ?EXIT_WARNING:EXIT_SUCCESS);
00369   return 0;         /* suppress no-return-value warnings */
00370 }

Generated on Thu Aug 25 12:37:37 2005 for Quake III Arena by  doxygen 1.3.9.1