blob: 703ce100e2fad76bc9102242fce38928fa275fc7 [file] [log] [blame]
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +00001/*
2 * cjpeg.c
3 *
DRCa73e8702012-12-31 02:52:30 +00004 * This file was part of the Independent JPEG Group's software:
Thomas G. Lane5ead57a1998-03-27 00:00:00 +00005 * Copyright (C) 1991-1998, Thomas G. Lane.
Guido Vollbeding5996a252009-06-27 00:00:00 +00006 * Modified 2003-2008 by Guido Vollbeding.
DRCa73e8702012-12-31 02:52:30 +00007 * Modifications:
DRC2cdd2ae2010-10-10 06:54:21 +00008 * Copyright (C) 2010, D. R. Commander.
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +00009 * For conditions of distribution and use, see the accompanying README file.
10 *
11 * This file contains a command-line user interface for the JPEG compressor.
12 * It should work on any system with Unix- or MS-DOS-style command lines.
13 *
14 * Two different command line styles are permitted, depending on the
15 * compile-time switch TWO_FILE_COMMANDLINE:
16 * cjpeg [options] inputfile outputfile
17 * cjpeg [options] [inputfile]
18 * In the second style, output is always to standard output, which you'd
19 * normally redirect to a file or pipe to some other program. Input is
20 * either from a named file or from standard input (typically redirected).
21 * The second style is convenient on Unix but is unhelpful on systems that
22 * don't support pipes. Also, you MUST use the first style if your system
23 * doesn't do binary I/O to stdin/stdout.
24 * To simplify script writing, the "-outfile" switch is provided. The syntax
25 * cjpeg [options] -outfile outputfile inputfile
26 * works regardless of which command line style is used.
27 */
28
29#include "cdjpeg.h" /* Common decls for cjpeg/djpeg applications */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000030#include "jversion.h" /* for version message */
DRC2cdd2ae2010-10-10 06:54:21 +000031#include "config.h"
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000032
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000033#ifdef USE_CCOMMAND /* command-line reader for Macintosh */
34#ifdef __MWERKS__
Thomas G. Lane489583f1996-02-07 00:00:00 +000035#include <SIOUX.h> /* Metrowerks needs this */
36#include <console.h> /* ... and this */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000037#endif
38#ifdef THINK_C
39#include <console.h> /* Think declares it here */
40#endif
41#endif
42
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000043
Thomas G. Lane9ba2f5e1994-12-07 00:00:00 +000044/* Create the add-on message string table. */
45
46#define JMESSAGE(code,string) string ,
47
48static const char * const cdjpeg_message_table[] = {
49#include "cderror.h"
50 NULL
51};
52
53
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000054/*
55 * This routine determines what format the input file is,
56 * and selects the appropriate input-reading module.
57 *
58 * To determine which family of input formats the file belongs to,
59 * we may look only at the first byte of the file, since C does not
60 * guarantee that more than one character can be pushed back with ungetc.
61 * Looking at additional bytes would require one of these approaches:
62 * 1) assume we can fseek() the input file (fails for piped input);
63 * 2) assume we can push back more than one character (works in
64 * some C implementations, but unportable);
65 * 3) provide our own buffering (breaks input readers that want to use
66 * stdio directly, such as the RLE library);
67 * or 4) don't put back the data, and modify the input_init methods to assume
68 * they start reading after the start of file (also breaks RLE library).
69 * #1 is attractive for MS-DOS but is untenable on Unix.
70 *
71 * The most portable solution for file types that can't be identified by their
72 * first byte is to make the user tell us what they are. This is also the
73 * only approach for "raw" file types that contain only arbitrary values.
74 * We presently apply this method for Targa files. Most of the time Targa
75 * files start with 0x00, so we recognize that case. Potentially, however,
76 * a Targa file could start with any byte value (byte 0 is the length of the
77 * seldom-used ID field), so we provide a switch to force Targa input mode.
78 */
79
80static boolean is_targa; /* records user -targa switch */
81
82
Thomas G. Lane489583f1996-02-07 00:00:00 +000083LOCAL(cjpeg_source_ptr)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000084select_file_type (j_compress_ptr cinfo, FILE * infile)
85{
86 int c;
87
88 if (is_targa) {
89#ifdef TARGA_SUPPORTED
90 return jinit_read_targa(cinfo);
91#else
92 ERREXIT(cinfo, JERR_TGA_NOTCOMP);
93#endif
94 }
95
96 if ((c = getc(infile)) == EOF)
97 ERREXIT(cinfo, JERR_INPUT_EMPTY);
98 if (ungetc(c, infile) == EOF)
99 ERREXIT(cinfo, JERR_UNGETC_FAILED);
100
101 switch (c) {
102#ifdef BMP_SUPPORTED
103 case 'B':
104 return jinit_read_bmp(cinfo);
105#endif
106#ifdef GIF_SUPPORTED
107 case 'G':
108 return jinit_read_gif(cinfo);
109#endif
110#ifdef PPM_SUPPORTED
111 case 'P':
112 return jinit_read_ppm(cinfo);
113#endif
114#ifdef RLE_SUPPORTED
115 case 'R':
116 return jinit_read_rle(cinfo);
117#endif
118#ifdef TARGA_SUPPORTED
119 case 0x00:
120 return jinit_read_targa(cinfo);
121#endif
122 default:
123 ERREXIT(cinfo, JERR_UNKNOWN_FORMAT);
124 break;
125 }
126
127 return NULL; /* suppress compiler warnings */
128}
129
130
131/*
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000132 * Argument-parsing code.
133 * The switch parser is designed to be useful with DOS-style command line
134 * syntax, ie, intermixed switches and file names, where only the switches
135 * to the left of a given file name affect processing of that file.
136 * The main program in this file doesn't actually use this capability...
137 */
138
139
140static const char * progname; /* program name for error messages */
141static char * outfilename; /* for -outfile switch */
142
143
Thomas G. Lane489583f1996-02-07 00:00:00 +0000144LOCAL(void)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000145usage (void)
146/* complain about bad command line */
147{
148 fprintf(stderr, "usage: %s [switches] ", progname);
149#ifdef TWO_FILE_COMMANDLINE
150 fprintf(stderr, "inputfile outputfile\n");
151#else
152 fprintf(stderr, "[inputfile]\n");
153#endif
154
155 fprintf(stderr, "Switches (names may be abbreviated):\n");
Guido Vollbeding5996a252009-06-27 00:00:00 +0000156 fprintf(stderr, " -quality N[,...] Compression quality (0..100; 5-95 is useful range)\n");
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000157 fprintf(stderr, " -grayscale Create monochrome JPEG file\n");
158#ifdef ENTROPY_OPT_SUPPORTED
159 fprintf(stderr, " -optimize Optimize Huffman table (smaller file, but slow compression)\n");
160#endif
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000161#ifdef C_PROGRESSIVE_SUPPORTED
162 fprintf(stderr, " -progressive Create progressive JPEG file\n");
163#endif
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000164#ifdef TARGA_SUPPORTED
165 fprintf(stderr, " -targa Input file is Targa format (usually not needed)\n");
166#endif
167 fprintf(stderr, "Switches for advanced users:\n");
DRCd657ba62012-01-27 09:41:20 +0000168#ifdef C_ARITH_CODING_SUPPORTED
169 fprintf(stderr, " -arithmetic Use arithmetic coding\n");
170#endif
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000171#ifdef DCT_ISLOW_SUPPORTED
172 fprintf(stderr, " -dct int Use integer DCT method%s\n",
173 (JDCT_DEFAULT == JDCT_ISLOW ? " (default)" : ""));
174#endif
175#ifdef DCT_IFAST_SUPPORTED
176 fprintf(stderr, " -dct fast Use fast integer DCT (less accurate)%s\n",
177 (JDCT_DEFAULT == JDCT_IFAST ? " (default)" : ""));
178#endif
179#ifdef DCT_FLOAT_SUPPORTED
180 fprintf(stderr, " -dct float Use floating-point DCT method%s\n",
181 (JDCT_DEFAULT == JDCT_FLOAT ? " (default)" : ""));
182#endif
183 fprintf(stderr, " -restart N Set restart interval in rows, or in blocks with B\n");
184#ifdef INPUT_SMOOTHING_SUPPORTED
185 fprintf(stderr, " -smooth N Smooth dithered input (N=1..100 is strength)\n");
186#endif
187 fprintf(stderr, " -maxmemory N Maximum memory to use (in kbytes)\n");
188 fprintf(stderr, " -outfile name Specify name for output file\n");
189 fprintf(stderr, " -verbose or -debug Emit debug output\n");
190 fprintf(stderr, "Switches for wizards:\n");
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000191 fprintf(stderr, " -baseline Force baseline quantization tables\n");
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000192 fprintf(stderr, " -qtables file Use quantization tables given in file\n");
193 fprintf(stderr, " -qslots N[,...] Set component quantization tables\n");
194 fprintf(stderr, " -sample HxV[,...] Set component sampling factors\n");
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000195#ifdef C_MULTISCAN_FILES_SUPPORTED
196 fprintf(stderr, " -scans file Create multi-scan JPEG per script file\n");
197#endif
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000198 exit(EXIT_FAILURE);
199}
200
201
Thomas G. Lane489583f1996-02-07 00:00:00 +0000202LOCAL(int)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000203parse_switches (j_compress_ptr cinfo, int argc, char **argv,
204 int last_file_arg_seen, boolean for_real)
205/* Parse optional switches.
206 * Returns argv[] index of first file-name argument (== argc if none).
207 * Any file names with indexes <= last_file_arg_seen are ignored;
208 * they have presumably been processed in a previous iteration.
209 * (Pass 0 for last_file_arg_seen on the first or only iteration.)
210 * for_real is FALSE on the first (dummy) pass; we may skip any expensive
211 * processing.
212 */
213{
214 int argn;
215 char * arg;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000216 boolean force_baseline;
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000217 boolean simple_progressive;
Guido Vollbeding5996a252009-06-27 00:00:00 +0000218 char * qualityarg = NULL; /* saves -quality parm if any */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000219 char * qtablefile = NULL; /* saves -qtables filename if any */
220 char * qslotsarg = NULL; /* saves -qslots parm if any */
221 char * samplearg = NULL; /* saves -sample parm if any */
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000222 char * scansarg = NULL; /* saves -scans parm if any */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000223
224 /* Set up default JPEG parameters. */
Guido Vollbeding5996a252009-06-27 00:00:00 +0000225
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000226 force_baseline = FALSE; /* by default, allow 16-bit quantizers */
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000227 simple_progressive = FALSE;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000228 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)) {
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000257 /* Force baseline-compatible output (8-bit quantizer values). */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000258 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) {
DRC2cdd2ae2010-10-10 06:54:21 +0000279 fprintf(stderr, "%s version %s (build %s)\n",
280 PACKAGE_NAME, VERSION, BUILD);
DRC1d5b1cf2012-01-31 10:39:29 +0000281 fprintf(stderr, "%s\n\n", JCOPYRIGHT);
282 fprintf(stderr, "Emulating The Independent JPEG Group's libjpeg, version %s\n\n",
283 JVERSION);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000284 printed_version = TRUE;
285 }
286 cinfo->err->trace_level++;
287
288 } else if (keymatch(arg, "grayscale", 2) || keymatch(arg, "greyscale",2)) {
289 /* Force a monochrome JPEG file to be generated. */
290 jpeg_set_colorspace(cinfo, JCS_GRAYSCALE);
291
292 } else if (keymatch(arg, "maxmemory", 3)) {
293 /* Maximum memory in Kb (or Mb with 'm'). */
294 long lval;
295 char ch = 'x';
296
297 if (++argn >= argc) /* advance to next argument */
298 usage();
299 if (sscanf(argv[argn], "%ld%c", &lval, &ch) < 1)
300 usage();
301 if (ch == 'm' || ch == 'M')
302 lval *= 1000L;
303 cinfo->mem->max_memory_to_use = lval * 1000L;
304
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000305 } else if (keymatch(arg, "optimize", 1) || keymatch(arg, "optimise", 1)) {
306 /* Enable entropy parm optimization. */
307#ifdef ENTROPY_OPT_SUPPORTED
308 cinfo->optimize_coding = TRUE;
309#else
310 fprintf(stderr, "%s: sorry, entropy optimization was not compiled\n",
311 progname);
312 exit(EXIT_FAILURE);
313#endif
314
315 } else if (keymatch(arg, "outfile", 4)) {
316 /* Set output file name. */
317 if (++argn >= argc) /* advance to next argument */
318 usage();
319 outfilename = argv[argn]; /* save it away for later use */
320
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000321 } else if (keymatch(arg, "progressive", 1)) {
322 /* Select simple progressive mode. */
323#ifdef C_PROGRESSIVE_SUPPORTED
324 simple_progressive = TRUE;
325 /* We must postpone execution until num_components is known. */
326#else
327 fprintf(stderr, "%s: sorry, progressive output was not compiled\n",
328 progname);
329 exit(EXIT_FAILURE);
330#endif
331
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000332 } else if (keymatch(arg, "quality", 1)) {
Guido Vollbeding5996a252009-06-27 00:00:00 +0000333 /* Quality ratings (quantization table scaling factors). */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000334 if (++argn >= argc) /* advance to next argument */
335 usage();
Guido Vollbeding5996a252009-06-27 00:00:00 +0000336 qualityarg = argv[argn];
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000337
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
Guido Vollbeding5996a252009-06-27 00:00:00 +0000384 } else if (keymatch(arg, "scans", 4)) {
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000385 /* 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
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000397 } 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. */
Guido Vollbeding5996a252009-06-27 00:00:00 +0000424 if (qualityarg != NULL) /* process -quality if it was present */
425 if (! set_quality_ratings(cinfo, qualityarg, force_baseline))
426 usage();
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000427
428 if (qtablefile != NULL) /* process -qtables if it was present */
Guido Vollbeding5996a252009-06-27 00:00:00 +0000429 if (! read_quant_tables(cinfo, qtablefile, force_baseline))
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000430 usage();
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000431
432 if (qslotsarg != NULL) /* process -qslots if it was present */
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000433 if (! set_quant_slots(cinfo, qslotsarg))
434 usage();
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000435
436 if (samplearg != NULL) /* process -sample if it was present */
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000437 if (! set_sample_factors(cinfo, samplearg))
438 usage();
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000439
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000440#ifdef C_PROGRESSIVE_SUPPORTED
441 if (simple_progressive) /* process -progressive; -scans can override */
442 jpeg_simple_progression(cinfo);
443#endif
444
445#ifdef C_MULTISCAN_FILES_SUPPORTED
446 if (scansarg != NULL) /* process -scans if it was present */
447 if (! read_scan_script(cinfo, scansarg))
448 usage();
449#endif
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000450 }
451
452 return argn; /* return index of next arg (file name) */
453}
454
455
456/*
457 * The main program.
458 */
459
Thomas G. Lane489583f1996-02-07 00:00:00 +0000460int
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000461main (int argc, char **argv)
462{
463 struct jpeg_compress_struct cinfo;
464 struct jpeg_error_mgr jerr;
465#ifdef PROGRESS_REPORT
466 struct cdjpeg_progress_mgr progress;
467#endif
468 int file_index;
469 cjpeg_source_ptr src_mgr;
470 FILE * input_file;
471 FILE * output_file;
472 JDIMENSION num_scanlines;
473
474 /* On Mac, fetch a command line. */
475#ifdef USE_CCOMMAND
476 argc = ccommand(&argv);
477#endif
478
479 progname = argv[0];
480 if (progname == NULL || progname[0] == 0)
481 progname = "cjpeg"; /* in case C library doesn't provide it */
482
483 /* Initialize the JPEG compression object with default error handling. */
484 cinfo.err = jpeg_std_error(&jerr);
485 jpeg_create_compress(&cinfo);
486 /* Add some application-specific error messages (from cderror.h) */
Thomas G. Lane9ba2f5e1994-12-07 00:00:00 +0000487 jerr.addon_message_table = cdjpeg_message_table;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000488 jerr.first_addon_message = JMSG_FIRSTADDONCODE;
489 jerr.last_addon_message = JMSG_LASTADDONCODE;
490
491 /* Now safe to enable signal catcher. */
492#ifdef NEED_SIGNAL_CATCHER
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000493 enable_signal_catcher((j_common_ptr) &cinfo);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000494#endif
495
496 /* Initialize JPEG parameters.
497 * Much of this may be overridden later.
498 * In particular, we don't yet know the input file's color space,
499 * but we need to provide some value for jpeg_set_defaults() to work.
500 */
501
502 cinfo.in_color_space = JCS_RGB; /* arbitrary guess */
503 jpeg_set_defaults(&cinfo);
504
505 /* Scan command line to find file names.
506 * It is convenient to use just one switch-parsing routine, but the switch
507 * values read here are ignored; we will rescan the switches after opening
508 * the input file.
509 */
510
511 file_index = parse_switches(&cinfo, argc, argv, 0, FALSE);
512
513#ifdef TWO_FILE_COMMANDLINE
514 /* Must have either -outfile switch or explicit output file name */
515 if (outfilename == NULL) {
516 if (file_index != argc-2) {
517 fprintf(stderr, "%s: must name one input and one output file\n",
518 progname);
519 usage();
520 }
521 outfilename = argv[file_index+1];
522 } else {
523 if (file_index != argc-1) {
524 fprintf(stderr, "%s: must name one input and one output file\n",
525 progname);
526 usage();
527 }
528 }
529#else
530 /* Unix style: expect zero or one file name */
531 if (file_index < argc-1) {
532 fprintf(stderr, "%s: only one input file\n", progname);
533 usage();
534 }
535#endif /* TWO_FILE_COMMANDLINE */
536
537 /* Open the input file. */
538 if (file_index < argc) {
539 if ((input_file = fopen(argv[file_index], READ_BINARY)) == NULL) {
540 fprintf(stderr, "%s: can't open %s\n", progname, argv[file_index]);
541 exit(EXIT_FAILURE);
542 }
543 } else {
544 /* default input file is stdin */
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000545 input_file = read_stdin();
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000546 }
547
548 /* Open the output file. */
549 if (outfilename != NULL) {
550 if ((output_file = fopen(outfilename, WRITE_BINARY)) == NULL) {
551 fprintf(stderr, "%s: can't open %s\n", progname, outfilename);
552 exit(EXIT_FAILURE);
553 }
554 } else {
555 /* default output file is stdout */
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000556 output_file = write_stdout();
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000557 }
558
559#ifdef PROGRESS_REPORT
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000560 start_progress_monitor((j_common_ptr) &cinfo, &progress);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000561#endif
562
563 /* Figure out the input file format, and set up to read it. */
564 src_mgr = select_file_type(&cinfo, input_file);
565 src_mgr->input_file = input_file;
566
567 /* Read the input file header to obtain file size & colorspace. */
568 (*src_mgr->start_input) (&cinfo, src_mgr);
569
570 /* Now that we know input colorspace, fix colorspace-dependent defaults */
571 jpeg_default_colorspace(&cinfo);
572
573 /* Adjust default compression parameters by re-parsing the options */
574 file_index = parse_switches(&cinfo, argc, argv, 0, TRUE);
575
576 /* Specify data destination for compression */
577 jpeg_stdio_dest(&cinfo, output_file);
578
579 /* Start compressor */
580 jpeg_start_compress(&cinfo, TRUE);
581
582 /* Process data */
583 while (cinfo.next_scanline < cinfo.image_height) {
584 num_scanlines = (*src_mgr->get_pixel_rows) (&cinfo, src_mgr);
585 (void) jpeg_write_scanlines(&cinfo, src_mgr->buffer, num_scanlines);
586 }
587
588 /* Finish compression and release memory */
589 (*src_mgr->finish_input) (&cinfo, src_mgr);
590 jpeg_finish_compress(&cinfo);
591 jpeg_destroy_compress(&cinfo);
592
Thomas G. Lane9ba2f5e1994-12-07 00:00:00 +0000593 /* Close files, if we opened them */
594 if (input_file != stdin)
595 fclose(input_file);
596 if (output_file != stdout)
597 fclose(output_file);
598
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000599#ifdef PROGRESS_REPORT
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000600 end_progress_monitor((j_common_ptr) &cinfo);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000601#endif
602
603 /* All done. */
604 exit(jerr.num_warnings ? EXIT_WARNING : EXIT_SUCCESS);
605 return 0; /* suppress no-return-value warnings */
606}