8 * Copyright (C) 1991-1998, Thomas G. Lane.
9 * This file is part of the Independent JPEG Group's software.
10 * For conditions of distribution and use, see the accompanying README file.
12 * This file contains a command-line user interface for the JPEG compressor.
13 * It should work on any system with Unix- or MS-DOS-style command lines.
15 * Two different command line styles are permitted, depending on the
16 * compile-time switch TWO_FILE_COMMANDLINE:
17 * cjpeg [options] inputfile outputfile
18 * cjpeg [options] [inputfile]
19 * In the second style, output is always to standard output, which you'd
20 * normally redirect to a file or pipe to some other program. Input is
21 * either from a named file or from standard input (typically redirected).
22 * The second style is convenient on Unix but is unhelpful on systems that
23 * don't support pipes. Also, you MUST use the first style if your system
24 * doesn't do binary I/O to stdin/stdout.
25 * To simplify script writing, the "-outfile" switch is provided. The syntax
26 * cjpeg [options] -outfile outputfile inputfile
27 * works regardless of which command line style is used.
31 #include "cdjpeg.h" /* Common decls for cjpeg/djpeg applications */
32 #include "jversion.h" /* for version message */
34 #ifdef USE_CCOMMAND /* command-line reader for Macintosh */
36 #include <SIOUX.h> /* Metrowerks needs this */
37 #include <console.h> /* ... and this */
40 #include <console.h> /* Think declares it here */
45 /* Create the add-on message string table. */
47 #define JMESSAGE(code,string) string ,
49 static const char * const cdjpeg_message_table
[] = {
56 * This routine determines what format the input file is,
57 * and selects the appropriate input-reading module.
59 * To determine which family of input formats the file belongs to,
60 * we may look only at the first byte of the file, since C does not
61 * guarantee that more than one character can be pushed back with ungetc.
62 * Looking at additional bytes would require one of these approaches:
63 * 1) assume we can fseek() the input file (fails for piped input);
64 * 2) assume we can push back more than one character (works in
65 * some C implementations, but unportable);
66 * 3) provide our own buffering (breaks input readers that want to use
67 * stdio directly, such as the RLE library);
68 * or 4) don't put back the data, and modify the input_init methods to assume
69 * they start reading after the start of file (also breaks RLE library).
70 * #1 is attractive for MS-DOS but is untenable on Unix.
72 * The most portable solution for file types that can't be identified by their
73 * first byte is to make the user tell us what they are. This is also the
74 * only approach for "raw" file types that contain only arbitrary values.
75 * We presently apply this method for Targa files. Most of the time Targa
76 * files start with 0x00, so we recognize that case. Potentially, however,
77 * a Targa file could start with any byte value (byte 0 is the length of the
78 * seldom-used ID field), so we provide a switch to force Targa input mode.
81 static boolean is_targa
; /* records user -targa switch */
84 LOCAL(cjpeg_source_ptr
)
85 select_file_type (j_compress_ptr cinfo
, FILE * infile
)
90 #ifdef TARGA_SUPPORTED
91 return jinit_read_targa(cinfo
);
93 ERREXIT(cinfo
, JERR_TGA_NOTCOMP
);
97 if ((c
= getc(infile
)) == EOF
)
98 ERREXIT(cinfo
, JERR_INPUT_EMPTY
);
99 if (ungetc(c
, infile
) == EOF
)
100 ERREXIT(cinfo
, JERR_UNGETC_FAILED
);
105 return jinit_read_bmp(cinfo
);
109 return jinit_read_gif(cinfo
);
113 return jinit_read_ppm(cinfo
);
117 return jinit_read_rle(cinfo
);
119 #ifdef TARGA_SUPPORTED
121 return jinit_read_targa(cinfo
);
124 ERREXIT(cinfo
, JERR_UNKNOWN_FORMAT
);
128 return NULL
; /* suppress compiler warnings */
133 * Argument-parsing code.
134 * The switch parser is designed to be useful with DOS-style command line
135 * syntax, ie, intermixed switches and file names, where only the switches
136 * to the left of a given file name affect processing of that file.
137 * The main program in this file doesn't actually use this capability...
141 static const char * progname
; /* program name for error messages */
142 static char * outfilename
; /* for -outfile switch */
147 /* complain about bad command line */
149 fprintf(stderr
, "usage: %s [switches] ", progname
);
150 #ifdef TWO_FILE_COMMANDLINE
151 fprintf(stderr
, "inputfile outputfile\n");
153 fprintf(stderr
, "[inputfile]\n");
156 fprintf(stderr
, "Switches (names may be abbreviated):\n");
157 fprintf(stderr
, " -quality N Compression quality (0..100; 5-95 is useful range)\n");
158 fprintf(stderr
, " -grayscale Create monochrome JPEG file\n");
159 #ifdef ENTROPY_OPT_SUPPORTED
160 fprintf(stderr
, " -optimize Optimize Huffman table (smaller file, but slow compression)\n");
162 #ifdef C_PROGRESSIVE_SUPPORTED
163 fprintf(stderr
, " -progressive Create progressive JPEG file\n");
165 #ifdef C_LOSSLESS_SUPPORTED
166 fprintf(stderr
, " -lossless psv[,Pt] Create lossless JPEG file\n");
168 #ifdef TARGA_SUPPORTED
169 fprintf(stderr
, " -targa Input file is Targa format (usually not needed)\n");
171 fprintf(stderr
, "Switches for advanced users:\n");
172 #ifdef DCT_ISLOW_SUPPORTED
173 fprintf(stderr
, " -dct int Use integer DCT method%s\n",
174 (JDCT_DEFAULT
== JDCT_ISLOW
? " (default)" : ""));
176 #ifdef DCT_IFAST_SUPPORTED
177 fprintf(stderr
, " -dct fast Use fast integer DCT (less accurate)%s\n",
178 (JDCT_DEFAULT
== JDCT_IFAST
? " (default)" : ""));
180 #ifdef DCT_FLOAT_SUPPORTED
181 fprintf(stderr
, " -dct float Use floating-point DCT method%s\n",
182 (JDCT_DEFAULT
== JDCT_FLOAT
? " (default)" : ""));
184 fprintf(stderr
, " -restart N Set restart interval in rows, or in blocks with B\n");
185 #ifdef INPUT_SMOOTHING_SUPPORTED
186 fprintf(stderr
, " -smooth N Smooth dithered input (N=1..100 is strength)\n");
188 fprintf(stderr
, " -maxmemory N Maximum memory to use (in kbytes)\n");
189 fprintf(stderr
, " -outfile name Specify name for output file\n");
190 fprintf(stderr
, " -verbose or -debug Emit debug output\n");
191 fprintf(stderr
, "Switches for wizards:\n");
192 #ifdef C_ARITH_CODING_SUPPORTED
193 fprintf(stderr
, " -arithmetic Use arithmetic coding\n");
195 fprintf(stderr
, " -baseline Force baseline quantization tables\n");
196 fprintf(stderr
, " -qtables file Use quantization tables given in file\n");
197 fprintf(stderr
, " -qslots N[,...] Set component quantization tables\n");
198 fprintf(stderr
, " -sample HxV[,...] Set component sampling factors\n");
199 #ifdef C_MULTISCAN_FILES_SUPPORTED
200 fprintf(stderr
, " -scans file Create multi-scan JPEG per script file\n");
207 parse_switches (j_compress_ptr cinfo
, int argc
, char **argv
,
208 int last_file_arg_seen
, boolean for_real
)
209 /* Parse optional switches.
210 * Returns argv[] index of first file-name argument (== argc if none).
211 * Any file names with indexes <= last_file_arg_seen are ignored;
212 * they have presumably been processed in a previous iteration.
213 * (Pass 0 for last_file_arg_seen on the first or only iteration.)
214 * for_real is FALSE on the first (dummy) pass; we may skip any expensive
220 int quality
; /* -quality parameter */
221 int q_scale_factor
; /* scaling percentage for -qtables */
222 boolean force_baseline
;
223 boolean simple_progressive
;
224 char * qtablefile
= NULL
; /* saves -qtables filename if any */
225 char * qslotsarg
= NULL
; /* saves -qslots parm if any */
226 char * samplearg
= NULL
; /* saves -sample parm if any */
227 char * scansarg
= NULL
; /* saves -scans parm if any */
228 char * losslsarg
= NULL
; /* saves -lossless parm if any */
230 /* Set up default JPEG parameters. */
231 /* Note that default -quality level need not, and does not,
232 * match the default scaling for an explicit -qtables argument.
234 quality
= 75; /* default -quality value */
235 q_scale_factor
= 100; /* default to no scaling for -qtables */
236 force_baseline
= FALSE
; /* by default, allow 16-bit quantizers */
237 simple_progressive
= FALSE
;
240 cinfo
->err
->trace_level
= 0;
242 /* Scan command line options, adjust parameters */
244 for (argn
= 1; argn
< argc
; argn
++) {
247 /* Not a switch, must be a file name argument */
248 if (argn
<= last_file_arg_seen
) {
249 outfilename
= NULL
; /* -outfile applies to just one input file */
250 continue; /* ignore this name if previously processed */
252 break; /* else done parsing switches */
254 arg
++; /* advance past switch marker character */
256 if (keymatch(arg
, "arithmetic", 1)) {
257 /* Use arithmetic coding. */
258 #ifdef C_ARITH_CODING_SUPPORTED
259 cinfo
->arith_code
= TRUE
;
261 fprintf(stderr
, "%s: sorry, arithmetic coding not supported\n",
266 } else if (keymatch(arg
, "baseline", 1)) {
267 /* Force baseline-compatible output (8-bit quantizer values). */
268 force_baseline
= TRUE
;
270 } else if (keymatch(arg
, "dct", 2)) {
271 /* Select DCT algorithm. */
272 if (++argn
>= argc
) /* advance to next argument */
274 if (keymatch(argv
[argn
], "int", 1)) {
275 cinfo
->dct_method
= JDCT_ISLOW
;
276 } else if (keymatch(argv
[argn
], "fast", 2)) {
277 cinfo
->dct_method
= JDCT_IFAST
;
278 } else if (keymatch(argv
[argn
], "float", 2)) {
279 cinfo
->dct_method
= JDCT_FLOAT
;
283 } else if (keymatch(arg
, "debug", 1) || keymatch(arg
, "verbose", 1)) {
284 /* Enable debug printouts. */
285 /* On first -d, print version identification */
286 static boolean printed_version
= FALSE
;
288 if (! printed_version
) {
289 fprintf(stderr
, "Independent JPEG Group's CJPEG, version %s\n%s\n",
290 JVERSION
, JCOPYRIGHT
);
291 printed_version
= TRUE
;
293 cinfo
->err
->trace_level
++;
295 } else if (keymatch(arg
, "grayscale", 2) || keymatch(arg
, "greyscale",2)) {
296 /* Force a monochrome JPEG file to be generated. */
297 jpeg_set_colorspace(cinfo
, JCS_GRAYSCALE
);
299 } else if (keymatch(arg
, "lossless", 1)) {
300 /* Select simple lossless mode. */
301 #ifdef C_LOSSLESS_SUPPORTED
302 if (++argn
>= argc
) /* advance to next argument */
304 losslsarg
= argv
[argn
];
305 /* We must postpone execution until num_components is known. */
307 fprintf(stderr
, "%s: sorry, lossless output was not compiled\n",
312 } else if (keymatch(arg
, "maxmemory", 3)) {
313 /* Maximum memory in Kb (or Mb with 'm'). */
317 if (++argn
>= argc
) /* advance to next argument */
319 if (sscanf(argv
[argn
], "%ld%c", &lval
, &ch
) < 1)
321 if (ch
== 'm' || ch
== 'M')
323 cinfo
->mem
->max_memory_to_use
= lval
* 1000L;
325 } else if (keymatch(arg
, "optimize", 1) || keymatch(arg
, "optimise", 1)) {
326 /* Enable entropy parm optimization. */
327 #ifdef ENTROPY_OPT_SUPPORTED
328 cinfo
->optimize_coding
= TRUE
;
330 fprintf(stderr
, "%s: sorry, entropy optimization was not compiled\n",
335 } else if (keymatch(arg
, "outfile", 4)) {
336 /* Set output file name. */
337 if (++argn
>= argc
) /* advance to next argument */
339 outfilename
= argv
[argn
]; /* save it away for later use */
341 } else if (keymatch(arg
, "progressive", 1)) {
342 /* Select simple progressive mode. */
343 #ifdef C_PROGRESSIVE_SUPPORTED
344 simple_progressive
= TRUE
;
345 /* We must postpone execution until num_components is known. */
347 fprintf(stderr
, "%s: sorry, progressive output was not compiled\n",
352 } else if (keymatch(arg
, "quality", 1)) {
353 /* Quality factor (quantization table scaling factor). */
354 if (++argn
>= argc
) /* advance to next argument */
356 if (sscanf(argv
[argn
], "%d", &quality
) != 1)
358 /* Change scale factor in case -qtables is present. */
359 q_scale_factor
= jpeg_quality_scaling(quality
);
361 } else if (keymatch(arg
, "qslots", 2)) {
362 /* Quantization table slot numbers. */
363 if (++argn
>= argc
) /* advance to next argument */
365 qslotsarg
= argv
[argn
];
366 /* Must delay setting qslots until after we have processed any
367 * colorspace-determining switches, since jpeg_set_colorspace sets
368 * default quant table numbers.
371 } else if (keymatch(arg
, "qtables", 2)) {
372 /* Quantization tables fetched from file. */
373 if (++argn
>= argc
) /* advance to next argument */
375 qtablefile
= argv
[argn
];
376 /* We postpone actually reading the file in case -quality comes later. */
378 } else if (keymatch(arg
, "restart", 1)) {
379 /* Restart interval in MCU rows (or in MCUs with 'b'). */
383 if (++argn
>= argc
) /* advance to next argument */
385 if (sscanf(argv
[argn
], "%ld%c", &lval
, &ch
) < 1)
387 if (lval
< 0 || lval
> 65535L)
389 if (ch
== 'b' || ch
== 'B') {
390 cinfo
->restart_interval
= (unsigned int) lval
;
391 cinfo
->restart_in_rows
= 0; /* else prior '-restart n' overrides me */
393 cinfo
->restart_in_rows
= (int) lval
;
394 /* restart_interval will be computed during startup */
397 } else if (keymatch(arg
, "sample", 2)) {
398 /* Set sampling factors. */
399 if (++argn
>= argc
) /* advance to next argument */
401 samplearg
= argv
[argn
];
402 /* Must delay setting sample factors until after we have processed any
403 * colorspace-determining switches, since jpeg_set_colorspace sets
404 * default sampling factors.
407 } else if (keymatch(arg
, "scans", 2)) {
408 /* Set scan script. */
409 #ifdef C_MULTISCAN_FILES_SUPPORTED
410 if (++argn
>= argc
) /* advance to next argument */
412 scansarg
= argv
[argn
];
413 /* We must postpone reading the file in case -progressive appears. */
415 fprintf(stderr
, "%s: sorry, multi-scan output was not compiled\n",
420 } else if (keymatch(arg
, "smooth", 2)) {
421 /* Set input smoothing factor. */
424 if (++argn
>= argc
) /* advance to next argument */
426 if (sscanf(argv
[argn
], "%d", &val
) != 1)
428 if (val
< 0 || val
> 100)
430 cinfo
->smoothing_factor
= val
;
432 } else if (keymatch(arg
, "targa", 1)) {
433 /* Input file is Targa format. */
437 usage(); /* bogus switch */
441 /* Post-switch-scanning cleanup */
445 /* Set quantization tables for selected quality. */
446 /* Some or all may be overridden if -qtables is present. */
447 jpeg_set_quality(cinfo
, quality
, force_baseline
);
449 if (qtablefile
!= NULL
) /* process -qtables if it was present */
450 if (! read_quant_tables(cinfo
, qtablefile
,
451 q_scale_factor
, force_baseline
))
454 if (qslotsarg
!= NULL
) /* process -qslots if it was present */
455 if (! set_quant_slots(cinfo
, qslotsarg
))
458 if (samplearg
!= NULL
) /* process -sample if it was present */
459 if (! set_sample_factors(cinfo
, samplearg
))
462 #ifdef C_PROGRESSIVE_SUPPORTED
463 if (simple_progressive
) /* process -progressive; -scans can override */
464 jpeg_simple_progression(cinfo
);
467 #ifdef C_LOSSLESS_SUPPORTED
468 if (losslsarg
!= NULL
) /* process -lossless if it was present */
469 if (! set_simple_lossless(cinfo
, losslsarg
))
473 #ifdef C_MULTISCAN_FILES_SUPPORTED
474 if (scansarg
!= NULL
) /* process -scans if it was present */
475 if (! read_scan_script(cinfo
, scansarg
))
480 return argn
; /* return index of next arg (file name) */
489 main (int argc
, char **argv
)
491 struct jpeg_compress_struct cinfo
;
492 struct jpeg_error_mgr jerr
;
493 #ifdef PROGRESS_REPORT
494 struct cdjpeg_progress_mgr progress
;
497 cjpeg_source_ptr src_mgr
;
500 JDIMENSION num_scanlines
;
502 /* On Mac, fetch a command line. */
504 argc
= ccommand(&argv
);
508 if (progname
== NULL
|| progname
[0] == 0)
509 progname
= "cjpeg"; /* in case C library doesn't provide it */
511 /* Initialize the JPEG compression object with default error handling. */
512 cinfo
.err
= jpeg_std_error(&jerr
);
513 jpeg_create_compress(&cinfo
);
514 /* Add some application-specific error messages (from cderror.h) */
515 jerr
.addon_message_table
= cdjpeg_message_table
;
516 jerr
.first_addon_message
= JMSG_FIRSTADDONCODE
;
517 jerr
.last_addon_message
= JMSG_LASTADDONCODE
;
519 /* Now safe to enable signal catcher. */
520 #ifdef NEED_SIGNAL_CATCHER
521 enable_signal_catcher((j_common_ptr
) &cinfo
);
524 /* Initialize JPEG parameters.
525 * Much of this may be overridden later.
526 * In particular, we don't yet know the input file's color space,
527 * but we need to provide some value for jpeg_set_defaults() to work.
530 cinfo
.in_color_space
= JCS_RGB
; /* arbitrary guess */
531 jpeg_set_defaults(&cinfo
);
533 /* Scan command line to find file names.
534 * It is convenient to use just one switch-parsing routine, but the switch
535 * values read here are ignored; we will rescan the switches after opening
539 file_index
= parse_switches(&cinfo
, argc
, argv
, 0, FALSE
);
541 #ifdef TWO_FILE_COMMANDLINE
542 /* Must have either -outfile switch or explicit output file name */
543 if (outfilename
== NULL
) {
544 if (file_index
!= argc
-2) {
545 fprintf(stderr
, "%s: must name one input and one output file\n",
549 outfilename
= argv
[file_index
+1];
551 if (file_index
!= argc
-1) {
552 fprintf(stderr
, "%s: must name one input and one output file\n",
558 /* Unix style: expect zero or one file name */
559 if (file_index
< argc
-1) {
560 fprintf(stderr
, "%s: only one input file\n", progname
);
563 #endif /* TWO_FILE_COMMANDLINE */
565 /* Open the input file. */
566 if (file_index
< argc
) {
567 if ((input_file
= fopen(argv
[file_index
], READ_BINARY
)) == NULL
) {
568 fprintf(stderr
, "%s: can't open %s\n", progname
, argv
[file_index
]);
572 /* default input file is stdin */
573 input_file
= read_stdin();
576 /* Open the output file. */
577 if (outfilename
!= NULL
) {
578 if ((output_file
= fopen(outfilename
, WRITE_BINARY
)) == NULL
) {
579 fprintf(stderr
, "%s: can't open %s\n", progname
, outfilename
);
583 /* default output file is stdout */
584 output_file
= write_stdout();
587 #ifdef PROGRESS_REPORT
588 start_progress_monitor((j_common_ptr
) &cinfo
, &progress
);
591 /* Figure out the input file format, and set up to read it. */
592 src_mgr
= select_file_type(&cinfo
, input_file
);
593 src_mgr
->input_file
= input_file
;
595 /* Read the input file header to obtain file size & colorspace. */
596 (*src_mgr
->start_input
) (&cinfo
, src_mgr
);
598 /* Now that we know input colorspace, fix colorspace-dependent defaults */
599 jpeg_default_colorspace(&cinfo
);
601 /* Adjust default compression parameters by re-parsing the options */
602 file_index
= parse_switches(&cinfo
, argc
, argv
, 0, TRUE
);
604 /* Specify data destination for compression */
605 jpeg_stdio_dest(&cinfo
, output_file
);
607 /* Start compressor */
608 jpeg_start_compress(&cinfo
, TRUE
);
611 while (cinfo
.next_scanline
< cinfo
.image_height
) {
612 num_scanlines
= (*src_mgr
->get_pixel_rows
) (&cinfo
, src_mgr
);
613 (void) jpeg_write_scanlines(&cinfo
, src_mgr
->buffer
, num_scanlines
);
616 /* Finish compression and release memory */
617 (*src_mgr
->finish_input
) (&cinfo
, src_mgr
);
618 jpeg_finish_compress(&cinfo
);
619 jpeg_destroy_compress(&cinfo
);
621 /* Close files, if we opened them */
622 if (input_file
!= stdin
)
624 if (output_file
!= stdout
)
627 #ifdef PROGRESS_REPORT
628 end_progress_monitor((j_common_ptr
) &cinfo
);
632 exit(jerr
.num_warnings
? EXIT_WARNING
: EXIT_SUCCESS
);
633 return 0; /* suppress no-return-value warnings */