blob: 0475c023be09cf99d5b283bb50de5e37c599ac3e [file] [log] [blame]
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +00001/*
2 * cjpeg.c
3 *
Thomas G. Lane5ead57a1998-03-27 00:00:00 +00004 * Copyright (C) 1991-1998, Thomas G. Lane.
Guido Vollbeding5996a252009-06-27 00:00:00 +00005 * Modified 2003-2008 by Guido Vollbeding.
DRC2cdd2ae2010-10-10 06:54:21 +00006 * Copyright (C) 2010, D. R. Commander.
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +00007 * This file is part of the Independent JPEG Group's software.
8 * For conditions of distribution and use, see the accompanying README file.
9 *
10 * This file contains a command-line user interface for the JPEG compressor.
11 * It should work on any system with Unix- or MS-DOS-style command lines.
12 *
13 * Two different command line styles are permitted, depending on the
14 * compile-time switch TWO_FILE_COMMANDLINE:
15 * cjpeg [options] inputfile outputfile
16 * cjpeg [options] [inputfile]
17 * In the second style, output is always to standard output, which you'd
18 * normally redirect to a file or pipe to some other program. Input is
19 * either from a named file or from standard input (typically redirected).
20 * The second style is convenient on Unix but is unhelpful on systems that
21 * don't support pipes. Also, you MUST use the first style if your system
22 * doesn't do binary I/O to stdin/stdout.
23 * To simplify script writing, the "-outfile" switch is provided. The syntax
24 * cjpeg [options] -outfile outputfile inputfile
25 * works regardless of which command line style is used.
26 */
27
28#include "cdjpeg.h" /* Common decls for cjpeg/djpeg applications */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000029#include "jversion.h" /* for version message */
DRC2cdd2ae2010-10-10 06:54:21 +000030#include "config.h"
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000031
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000032#ifdef USE_CCOMMAND /* command-line reader for Macintosh */
33#ifdef __MWERKS__
Thomas G. Lane489583f1996-02-07 00:00:00 +000034#include <SIOUX.h> /* Metrowerks needs this */
35#include <console.h> /* ... and this */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000036#endif
37#ifdef THINK_C
38#include <console.h> /* Think declares it here */
39#endif
40#endif
41
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000042
Thomas G. Lane9ba2f5e1994-12-07 00:00:00 +000043/* Create the add-on message string table. */
44
45#define JMESSAGE(code,string) string ,
46
47static const char * const cdjpeg_message_table[] = {
48#include "cderror.h"
49 NULL
50};
51
52
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000053/*
54 * This routine determines what format the input file is,
55 * and selects the appropriate input-reading module.
56 *
57 * To determine which family of input formats the file belongs to,
58 * we may look only at the first byte of the file, since C does not
59 * guarantee that more than one character can be pushed back with ungetc.
60 * Looking at additional bytes would require one of these approaches:
61 * 1) assume we can fseek() the input file (fails for piped input);
62 * 2) assume we can push back more than one character (works in
63 * some C implementations, but unportable);
64 * 3) provide our own buffering (breaks input readers that want to use
65 * stdio directly, such as the RLE library);
66 * or 4) don't put back the data, and modify the input_init methods to assume
67 * they start reading after the start of file (also breaks RLE library).
68 * #1 is attractive for MS-DOS but is untenable on Unix.
69 *
70 * The most portable solution for file types that can't be identified by their
71 * first byte is to make the user tell us what they are. This is also the
72 * only approach for "raw" file types that contain only arbitrary values.
73 * We presently apply this method for Targa files. Most of the time Targa
74 * files start with 0x00, so we recognize that case. Potentially, however,
75 * a Targa file could start with any byte value (byte 0 is the length of the
76 * seldom-used ID field), so we provide a switch to force Targa input mode.
77 */
78
79static boolean is_targa; /* records user -targa switch */
80
81
Thomas G. Lane489583f1996-02-07 00:00:00 +000082LOCAL(cjpeg_source_ptr)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000083select_file_type (j_compress_ptr cinfo, FILE * infile)
84{
85 int c;
86
87 if (is_targa) {
88#ifdef TARGA_SUPPORTED
89 return jinit_read_targa(cinfo);
90#else
91 ERREXIT(cinfo, JERR_TGA_NOTCOMP);
92#endif
93 }
94
95 if ((c = getc(infile)) == EOF)
96 ERREXIT(cinfo, JERR_INPUT_EMPTY);
97 if (ungetc(c, infile) == EOF)
98 ERREXIT(cinfo, JERR_UNGETC_FAILED);
99
100 switch (c) {
101#ifdef BMP_SUPPORTED
102 case 'B':
103 return jinit_read_bmp(cinfo);
104#endif
105#ifdef GIF_SUPPORTED
106 case 'G':
107 return jinit_read_gif(cinfo);
108#endif
109#ifdef PPM_SUPPORTED
110 case 'P':
111 return jinit_read_ppm(cinfo);
112#endif
113#ifdef RLE_SUPPORTED
114 case 'R':
115 return jinit_read_rle(cinfo);
116#endif
117#ifdef TARGA_SUPPORTED
118 case 0x00:
119 return jinit_read_targa(cinfo);
120#endif
121 default:
122 ERREXIT(cinfo, JERR_UNKNOWN_FORMAT);
123 break;
124 }
125
126 return NULL; /* suppress compiler warnings */
127}
128
129
130/*
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000131 * Argument-parsing code.
132 * The switch parser is designed to be useful with DOS-style command line
133 * syntax, ie, intermixed switches and file names, where only the switches
134 * to the left of a given file name affect processing of that file.
135 * The main program in this file doesn't actually use this capability...
136 */
137
138
139static const char * progname; /* program name for error messages */
140static char * outfilename; /* for -outfile switch */
141
142
Thomas G. Lane489583f1996-02-07 00:00:00 +0000143LOCAL(void)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000144usage (void)
145/* complain about bad command line */
146{
147 fprintf(stderr, "usage: %s [switches] ", progname);
148#ifdef TWO_FILE_COMMANDLINE
149 fprintf(stderr, "inputfile outputfile\n");
150#else
151 fprintf(stderr, "[inputfile]\n");
152#endif
153
154 fprintf(stderr, "Switches (names may be abbreviated):\n");
Guido Vollbeding5996a252009-06-27 00:00:00 +0000155 fprintf(stderr, " -quality N[,...] Compression quality (0..100; 5-95 is useful range)\n");
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000156 fprintf(stderr, " -grayscale Create monochrome JPEG file\n");
157#ifdef ENTROPY_OPT_SUPPORTED
158 fprintf(stderr, " -optimize Optimize Huffman table (smaller file, but slow compression)\n");
159#endif
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000160#ifdef C_PROGRESSIVE_SUPPORTED
161 fprintf(stderr, " -progressive Create progressive JPEG file\n");
162#endif
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000163#ifdef TARGA_SUPPORTED
164 fprintf(stderr, " -targa Input file is Targa format (usually not needed)\n");
165#endif
166 fprintf(stderr, "Switches for advanced users:\n");
DRCd657ba62012-01-27 09:41:20 +0000167#ifdef C_ARITH_CODING_SUPPORTED
168 fprintf(stderr, " -arithmetic Use arithmetic coding\n");
169#endif
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000170#ifdef DCT_ISLOW_SUPPORTED
171 fprintf(stderr, " -dct int Use integer DCT method%s\n",
172 (JDCT_DEFAULT == JDCT_ISLOW ? " (default)" : ""));
173#endif
174#ifdef DCT_IFAST_SUPPORTED
175 fprintf(stderr, " -dct fast Use fast integer DCT (less accurate)%s\n",
176 (JDCT_DEFAULT == JDCT_IFAST ? " (default)" : ""));
177#endif
178#ifdef DCT_FLOAT_SUPPORTED
179 fprintf(stderr, " -dct float Use floating-point DCT method%s\n",
180 (JDCT_DEFAULT == JDCT_FLOAT ? " (default)" : ""));
181#endif
182 fprintf(stderr, " -restart N Set restart interval in rows, or in blocks with B\n");
183#ifdef INPUT_SMOOTHING_SUPPORTED
184 fprintf(stderr, " -smooth N Smooth dithered input (N=1..100 is strength)\n");
185#endif
186 fprintf(stderr, " -maxmemory N Maximum memory to use (in kbytes)\n");
187 fprintf(stderr, " -outfile name Specify name for output file\n");
188 fprintf(stderr, " -verbose or -debug Emit debug output\n");
189 fprintf(stderr, "Switches for wizards:\n");
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000190 fprintf(stderr, " -baseline Force baseline quantization tables\n");
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000191 fprintf(stderr, " -qtables file Use quantization tables given in file\n");
192 fprintf(stderr, " -qslots N[,...] Set component quantization tables\n");
193 fprintf(stderr, " -sample HxV[,...] Set component sampling factors\n");
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000194#ifdef C_MULTISCAN_FILES_SUPPORTED
195 fprintf(stderr, " -scans file Create multi-scan JPEG per script file\n");
196#endif
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000197 exit(EXIT_FAILURE);
198}
199
200
Thomas G. Lane489583f1996-02-07 00:00:00 +0000201LOCAL(int)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000202parse_switches (j_compress_ptr cinfo, int argc, char **argv,
203 int last_file_arg_seen, boolean for_real)
204/* Parse optional switches.
205 * Returns argv[] index of first file-name argument (== argc if none).
206 * Any file names with indexes <= last_file_arg_seen are ignored;
207 * they have presumably been processed in a previous iteration.
208 * (Pass 0 for last_file_arg_seen on the first or only iteration.)
209 * for_real is FALSE on the first (dummy) pass; we may skip any expensive
210 * processing.
211 */
212{
213 int argn;
214 char * arg;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000215 boolean force_baseline;
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000216 boolean simple_progressive;
Guido Vollbeding5996a252009-06-27 00:00:00 +0000217 char * qualityarg = NULL; /* saves -quality parm if any */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000218 char * qtablefile = NULL; /* saves -qtables filename if any */
219 char * qslotsarg = NULL; /* saves -qslots parm if any */
220 char * samplearg = NULL; /* saves -sample parm if any */
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000221 char * scansarg = NULL; /* saves -scans parm if any */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000222
223 /* Set up default JPEG parameters. */
Guido Vollbeding5996a252009-06-27 00:00:00 +0000224
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000225 force_baseline = FALSE; /* by default, allow 16-bit quantizers */
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000226 simple_progressive = FALSE;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000227 is_targa = FALSE;
228 outfilename = NULL;
229 cinfo->err->trace_level = 0;
230
231 /* Scan command line options, adjust parameters */
232
233 for (argn = 1; argn < argc; argn++) {
234 arg = argv[argn];
235 if (*arg != '-') {
236 /* Not a switch, must be a file name argument */
237 if (argn <= last_file_arg_seen) {
238 outfilename = NULL; /* -outfile applies to just one input file */
239 continue; /* ignore this name if previously processed */
240 }
241 break; /* else done parsing switches */
242 }
243 arg++; /* advance past switch marker character */
244
245 if (keymatch(arg, "arithmetic", 1)) {
246 /* Use arithmetic coding. */
247#ifdef C_ARITH_CODING_SUPPORTED
248 cinfo->arith_code = TRUE;
249#else
250 fprintf(stderr, "%s: sorry, arithmetic coding not supported\n",
251 progname);
252 exit(EXIT_FAILURE);
253#endif
254
255 } else if (keymatch(arg, "baseline", 1)) {
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000256 /* Force baseline-compatible output (8-bit quantizer values). */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000257 force_baseline = TRUE;
258
259 } else if (keymatch(arg, "dct", 2)) {
260 /* Select DCT algorithm. */
261 if (++argn >= argc) /* advance to next argument */
262 usage();
263 if (keymatch(argv[argn], "int", 1)) {
264 cinfo->dct_method = JDCT_ISLOW;
265 } else if (keymatch(argv[argn], "fast", 2)) {
266 cinfo->dct_method = JDCT_IFAST;
267 } else if (keymatch(argv[argn], "float", 2)) {
268 cinfo->dct_method = JDCT_FLOAT;
269 } else
270 usage();
271
272 } else if (keymatch(arg, "debug", 1) || keymatch(arg, "verbose", 1)) {
273 /* Enable debug printouts. */
274 /* On first -d, print version identification */
275 static boolean printed_version = FALSE;
276
277 if (! printed_version) {
DRC2cdd2ae2010-10-10 06:54:21 +0000278 fprintf(stderr, "%s version %s (build %s)\n",
279 PACKAGE_NAME, VERSION, BUILD);
DRC1d5b1cf2012-01-31 10:39:29 +0000280 fprintf(stderr, "%s\n\n", JCOPYRIGHT);
281 fprintf(stderr, "Emulating The Independent JPEG Group's libjpeg, version %s\n\n",
282 JVERSION);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000283 printed_version = TRUE;
284 }
285 cinfo->err->trace_level++;
286
287 } else if (keymatch(arg, "grayscale", 2) || keymatch(arg, "greyscale",2)) {
288 /* Force a monochrome JPEG file to be generated. */
289 jpeg_set_colorspace(cinfo, JCS_GRAYSCALE);
290
291 } else if (keymatch(arg, "maxmemory", 3)) {
292 /* Maximum memory in Kb (or Mb with 'm'). */
293 long lval;
294 char ch = 'x';
295
296 if (++argn >= argc) /* advance to next argument */
297 usage();
298 if (sscanf(argv[argn], "%ld%c", &lval, &ch) < 1)
299 usage();
300 if (ch == 'm' || ch == 'M')
301 lval *= 1000L;
302 cinfo->mem->max_memory_to_use = lval * 1000L;
303
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000304 } else if (keymatch(arg, "optimize", 1) || keymatch(arg, "optimise", 1)) {
305 /* Enable entropy parm optimization. */
306#ifdef ENTROPY_OPT_SUPPORTED
307 cinfo->optimize_coding = TRUE;
308#else
309 fprintf(stderr, "%s: sorry, entropy optimization was not compiled\n",
310 progname);
311 exit(EXIT_FAILURE);
312#endif
313
314 } else if (keymatch(arg, "outfile", 4)) {
315 /* Set output file name. */
316 if (++argn >= argc) /* advance to next argument */
317 usage();
318 outfilename = argv[argn]; /* save it away for later use */
319
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000320 } else if (keymatch(arg, "progressive", 1)) {
321 /* Select simple progressive mode. */
322#ifdef C_PROGRESSIVE_SUPPORTED
323 simple_progressive = TRUE;
324 /* We must postpone execution until num_components is known. */
325#else
326 fprintf(stderr, "%s: sorry, progressive output was not compiled\n",
327 progname);
328 exit(EXIT_FAILURE);
329#endif
330
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000331 } else if (keymatch(arg, "quality", 1)) {
Guido Vollbeding5996a252009-06-27 00:00:00 +0000332 /* Quality ratings (quantization table scaling factors). */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000333 if (++argn >= argc) /* advance to next argument */
334 usage();
Guido Vollbeding5996a252009-06-27 00:00:00 +0000335 qualityarg = argv[argn];
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000336
337 } else if (keymatch(arg, "qslots", 2)) {
338 /* Quantization table slot numbers. */
339 if (++argn >= argc) /* advance to next argument */
340 usage();
341 qslotsarg = argv[argn];
342 /* Must delay setting qslots until after we have processed any
343 * colorspace-determining switches, since jpeg_set_colorspace sets
344 * default quant table numbers.
345 */
346
347 } else if (keymatch(arg, "qtables", 2)) {
348 /* Quantization tables fetched from file. */
349 if (++argn >= argc) /* advance to next argument */
350 usage();
351 qtablefile = argv[argn];
352 /* We postpone actually reading the file in case -quality comes later. */
353
354 } else if (keymatch(arg, "restart", 1)) {
355 /* Restart interval in MCU rows (or in MCUs with 'b'). */
356 long lval;
357 char ch = 'x';
358
359 if (++argn >= argc) /* advance to next argument */
360 usage();
361 if (sscanf(argv[argn], "%ld%c", &lval, &ch) < 1)
362 usage();
363 if (lval < 0 || lval > 65535L)
364 usage();
365 if (ch == 'b' || ch == 'B') {
366 cinfo->restart_interval = (unsigned int) lval;
367 cinfo->restart_in_rows = 0; /* else prior '-restart n' overrides me */
368 } else {
369 cinfo->restart_in_rows = (int) lval;
370 /* restart_interval will be computed during startup */
371 }
372
373 } else if (keymatch(arg, "sample", 2)) {
374 /* Set sampling factors. */
375 if (++argn >= argc) /* advance to next argument */
376 usage();
377 samplearg = argv[argn];
378 /* Must delay setting sample factors until after we have processed any
379 * colorspace-determining switches, since jpeg_set_colorspace sets
380 * default sampling factors.
381 */
382
Guido Vollbeding5996a252009-06-27 00:00:00 +0000383 } else if (keymatch(arg, "scans", 4)) {
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000384 /* Set scan script. */
385#ifdef C_MULTISCAN_FILES_SUPPORTED
386 if (++argn >= argc) /* advance to next argument */
387 usage();
388 scansarg = argv[argn];
389 /* We must postpone reading the file in case -progressive appears. */
390#else
391 fprintf(stderr, "%s: sorry, multi-scan output was not compiled\n",
392 progname);
393 exit(EXIT_FAILURE);
394#endif
395
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000396 } else if (keymatch(arg, "smooth", 2)) {
397 /* Set input smoothing factor. */
398 int val;
399
400 if (++argn >= argc) /* advance to next argument */
401 usage();
402 if (sscanf(argv[argn], "%d", &val) != 1)
403 usage();
404 if (val < 0 || val > 100)
405 usage();
406 cinfo->smoothing_factor = val;
407
408 } else if (keymatch(arg, "targa", 1)) {
409 /* Input file is Targa format. */
410 is_targa = TRUE;
411
412 } else {
413 usage(); /* bogus switch */
414 }
415 }
416
417 /* Post-switch-scanning cleanup */
418
419 if (for_real) {
420
421 /* Set quantization tables for selected quality. */
422 /* Some or all may be overridden if -qtables is present. */
Guido Vollbeding5996a252009-06-27 00:00:00 +0000423 if (qualityarg != NULL) /* process -quality if it was present */
424 if (! set_quality_ratings(cinfo, qualityarg, force_baseline))
425 usage();
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000426
427 if (qtablefile != NULL) /* process -qtables if it was present */
Guido Vollbeding5996a252009-06-27 00:00:00 +0000428 if (! read_quant_tables(cinfo, qtablefile, force_baseline))
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000429 usage();
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000430
431 if (qslotsarg != NULL) /* process -qslots if it was present */
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000432 if (! set_quant_slots(cinfo, qslotsarg))
433 usage();
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000434
435 if (samplearg != NULL) /* process -sample if it was present */
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000436 if (! set_sample_factors(cinfo, samplearg))
437 usage();
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000438
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000439#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
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000449 }
450
451 return argn; /* return index of next arg (file name) */
452}
453
454
455/*
456 * The main program.
457 */
458
Thomas G. Lane489583f1996-02-07 00:00:00 +0000459int
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000460main (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) */
Thomas G. Lane9ba2f5e1994-12-07 00:00:00 +0000486 jerr.addon_message_table = cdjpeg_message_table;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000487 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
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000492 enable_signal_catcher((j_common_ptr) &cinfo);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000493#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 */
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000544 input_file = read_stdin();
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000545 }
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 */
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000555 output_file = write_stdout();
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000556 }
557
558#ifdef PROGRESS_REPORT
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000559 start_progress_monitor((j_common_ptr) &cinfo, &progress);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000560#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
Thomas G. Lane9ba2f5e1994-12-07 00:00:00 +0000592 /* 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
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000598#ifdef PROGRESS_REPORT
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000599 end_progress_monitor((j_common_ptr) &cinfo);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000600#endif
601
602 /* All done. */
603 exit(jerr.num_warnings ? EXIT_WARNING : EXIT_SUCCESS);
604 return 0; /* suppress no-return-value warnings */
605}