blob: af946d28d77a62bd93354e5d93dcef230687727d [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 Vollbeding5829cb22012-01-15 00:00:00 +00006 * Modified 2003-2011 by Guido Vollbeding.
DRCa6ef2822013-09-28 03:23:49 +00007 * libjpeg-turbo Modifications:
DRC9665f5e2014-11-22 04:04:38 +00008 * Copyright (C) 2010, 2013-2014, 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:
DRCe5eaf372014-05-09 18:00:32 +000016 * cjpeg [options] inputfile outputfile
17 * cjpeg [options] [inputfile]
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000018 * 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
DRCe5eaf372014-05-09 18:00:32 +000025 * cjpeg [options] -outfile outputfile inputfile
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000026 * works regardless of which command line style is used.
27 */
28
DRCe5eaf372014-05-09 18:00:32 +000029#include "cdjpeg.h" /* Common decls for cjpeg/djpeg applications */
30#include "jversion.h" /* for version message */
DRCff6961f2014-04-20 09:17:11 +000031#include "jconfigint.h"
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000032
DRCe5eaf372014-05-09 18:00:32 +000033#ifdef USE_CCOMMAND /* command-line reader for Macintosh */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000034#ifdef __MWERKS__
Thomas G. Lane489583f1996-02-07 00:00:00 +000035#include <SIOUX.h> /* Metrowerks needs this */
DRCe5eaf372014-05-09 18:00:32 +000036#include <console.h> /* ... and this */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000037#endif
38#ifdef THINK_C
DRCe5eaf372014-05-09 18:00:32 +000039#include <console.h> /* Think declares it here */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000040#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
DRCe5eaf372014-05-09 18:00:32 +000046#define JMESSAGE(code,string) string ,
Thomas G. Lane9ba2f5e1994-12-07 00:00:00 +000047
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
DRCe5eaf372014-05-09 18:00:32 +000080static boolean is_targa; /* records user -targa switch */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000081
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
DRCe5eaf372014-05-09 18:00:32 +0000127 return NULL; /* suppress compiler warnings */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000128}
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
DRCe5eaf372014-05-09 18:00:32 +0000140static const char * progname; /* program name for error messages */
141static char * outfilename; /* for -outfile switch */
DRCab706232013-01-18 23:42:31 +0000142boolean memdst; /* for -memdst switch */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000143
144
Thomas G. Lane489583f1996-02-07 00:00:00 +0000145LOCAL(void)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000146usage (void)
147/* complain about bad command line */
148{
149 fprintf(stderr, "usage: %s [switches] ", progname);
150#ifdef TWO_FILE_COMMANDLINE
151 fprintf(stderr, "inputfile outputfile\n");
152#else
153 fprintf(stderr, "[inputfile]\n");
154#endif
155
156 fprintf(stderr, "Switches (names may be abbreviated):\n");
Guido Vollbeding5996a252009-06-27 00:00:00 +0000157 fprintf(stderr, " -quality N[,...] Compression quality (0..100; 5-95 is useful range)\n");
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000158 fprintf(stderr, " -grayscale Create monochrome JPEG file\n");
Guido Vollbeding5829cb22012-01-15 00:00:00 +0000159 fprintf(stderr, " -rgb Create RGB JPEG file\n");
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000160#ifdef ENTROPY_OPT_SUPPORTED
161 fprintf(stderr, " -optimize Optimize Huffman table (smaller file, but slow compression)\n");
162#endif
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000163#ifdef C_PROGRESSIVE_SUPPORTED
164 fprintf(stderr, " -progressive Create progressive JPEG file\n");
165#endif
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000166#ifdef TARGA_SUPPORTED
167 fprintf(stderr, " -targa Input file is Targa format (usually not needed)\n");
168#endif
169 fprintf(stderr, "Switches for advanced users:\n");
DRCd657ba62012-01-27 09:41:20 +0000170#ifdef C_ARITH_CODING_SUPPORTED
171 fprintf(stderr, " -arithmetic Use arithmetic coding\n");
172#endif
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000173#ifdef DCT_ISLOW_SUPPORTED
174 fprintf(stderr, " -dct int Use integer DCT method%s\n",
DRCe5eaf372014-05-09 18:00:32 +0000175 (JDCT_DEFAULT == JDCT_ISLOW ? " (default)" : ""));
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000176#endif
177#ifdef DCT_IFAST_SUPPORTED
178 fprintf(stderr, " -dct fast Use fast integer DCT (less accurate)%s\n",
DRCe5eaf372014-05-09 18:00:32 +0000179 (JDCT_DEFAULT == JDCT_IFAST ? " (default)" : ""));
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000180#endif
181#ifdef DCT_FLOAT_SUPPORTED
182 fprintf(stderr, " -dct float Use floating-point DCT method%s\n",
DRCe5eaf372014-05-09 18:00:32 +0000183 (JDCT_DEFAULT == JDCT_FLOAT ? " (default)" : ""));
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000184#endif
185 fprintf(stderr, " -restart N Set restart interval in rows, or in blocks with B\n");
186#ifdef INPUT_SMOOTHING_SUPPORTED
187 fprintf(stderr, " -smooth N Smooth dithered input (N=1..100 is strength)\n");
188#endif
189 fprintf(stderr, " -maxmemory N Maximum memory to use (in kbytes)\n");
190 fprintf(stderr, " -outfile name Specify name for output file\n");
DRCab706232013-01-18 23:42:31 +0000191#if JPEG_LIB_VERSION >= 80 || defined(MEM_SRCDST_SUPPORTED)
192 fprintf(stderr, " -memdst Compress to memory instead of file (useful for benchmarking)\n");
193#endif
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000194 fprintf(stderr, " -verbose or -debug Emit debug output\n");
DRC9665f5e2014-11-22 04:04:38 +0000195 fprintf(stderr, " -version Print version information and exit\n");
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000196 fprintf(stderr, "Switches for wizards:\n");
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000197 fprintf(stderr, " -baseline Force baseline quantization tables\n");
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000198 fprintf(stderr, " -qtables file Use quantization tables given in file\n");
199 fprintf(stderr, " -qslots N[,...] Set component quantization tables\n");
200 fprintf(stderr, " -sample HxV[,...] Set component sampling factors\n");
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000201#ifdef C_MULTISCAN_FILES_SUPPORTED
202 fprintf(stderr, " -scans file Create multi-scan JPEG per script file\n");
203#endif
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000204 exit(EXIT_FAILURE);
205}
206
207
Thomas G. Lane489583f1996-02-07 00:00:00 +0000208LOCAL(int)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000209parse_switches (j_compress_ptr cinfo, int argc, char **argv,
DRCe5eaf372014-05-09 18:00:32 +0000210 int last_file_arg_seen, boolean for_real)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000211/* Parse optional switches.
212 * Returns argv[] index of first file-name argument (== argc if none).
213 * Any file names with indexes <= last_file_arg_seen are ignored;
214 * they have presumably been processed in a previous iteration.
215 * (Pass 0 for last_file_arg_seen on the first or only iteration.)
216 * for_real is FALSE on the first (dummy) pass; we may skip any expensive
217 * processing.
218 */
219{
220 int argn;
221 char * arg;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000222 boolean force_baseline;
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000223 boolean simple_progressive;
DRCe5eaf372014-05-09 18:00:32 +0000224 char * qualityarg = NULL; /* saves -quality parm if any */
225 char * qtablefile = NULL; /* saves -qtables filename if any */
226 char * qslotsarg = NULL; /* saves -qslots parm if any */
227 char * samplearg = NULL; /* saves -sample parm if any */
228 char * scansarg = NULL; /* saves -scans parm if any */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000229
230 /* Set up default JPEG parameters. */
Guido Vollbeding5996a252009-06-27 00:00:00 +0000231
DRCe5eaf372014-05-09 18:00:32 +0000232 force_baseline = FALSE; /* by default, allow 16-bit quantizers */
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000233 simple_progressive = FALSE;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000234 is_targa = FALSE;
235 outfilename = NULL;
DRCab706232013-01-18 23:42:31 +0000236 memdst = FALSE;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000237 cinfo->err->trace_level = 0;
238
239 /* Scan command line options, adjust parameters */
240
241 for (argn = 1; argn < argc; argn++) {
242 arg = argv[argn];
243 if (*arg != '-') {
244 /* Not a switch, must be a file name argument */
245 if (argn <= last_file_arg_seen) {
DRCe5eaf372014-05-09 18:00:32 +0000246 outfilename = NULL; /* -outfile applies to just one input file */
247 continue; /* ignore this name if previously processed */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000248 }
DRCe5eaf372014-05-09 18:00:32 +0000249 break; /* else done parsing switches */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000250 }
DRCe5eaf372014-05-09 18:00:32 +0000251 arg++; /* advance past switch marker character */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000252
253 if (keymatch(arg, "arithmetic", 1)) {
254 /* Use arithmetic coding. */
255#ifdef C_ARITH_CODING_SUPPORTED
256 cinfo->arith_code = TRUE;
257#else
258 fprintf(stderr, "%s: sorry, arithmetic coding not supported\n",
DRCe5eaf372014-05-09 18:00:32 +0000259 progname);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000260 exit(EXIT_FAILURE);
261#endif
262
263 } else if (keymatch(arg, "baseline", 1)) {
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000264 /* Force baseline-compatible output (8-bit quantizer values). */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000265 force_baseline = TRUE;
266
267 } else if (keymatch(arg, "dct", 2)) {
268 /* Select DCT algorithm. */
DRCe5eaf372014-05-09 18:00:32 +0000269 if (++argn >= argc) /* advance to next argument */
270 usage();
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000271 if (keymatch(argv[argn], "int", 1)) {
DRCe5eaf372014-05-09 18:00:32 +0000272 cinfo->dct_method = JDCT_ISLOW;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000273 } else if (keymatch(argv[argn], "fast", 2)) {
DRCe5eaf372014-05-09 18:00:32 +0000274 cinfo->dct_method = JDCT_IFAST;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000275 } else if (keymatch(argv[argn], "float", 2)) {
DRCe5eaf372014-05-09 18:00:32 +0000276 cinfo->dct_method = JDCT_FLOAT;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000277 } else
DRCe5eaf372014-05-09 18:00:32 +0000278 usage();
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000279
280 } else if (keymatch(arg, "debug", 1) || keymatch(arg, "verbose", 1)) {
281 /* Enable debug printouts. */
282 /* On first -d, print version identification */
283 static boolean printed_version = FALSE;
284
285 if (! printed_version) {
DRCe5eaf372014-05-09 18:00:32 +0000286 fprintf(stderr, "%s version %s (build %s)\n",
287 PACKAGE_NAME, VERSION, BUILD);
288 fprintf(stderr, "%s\n\n", JCOPYRIGHT);
289 fprintf(stderr, "Emulating The Independent JPEG Group's software, version %s\n\n",
290 JVERSION);
291 printed_version = TRUE;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000292 }
DRC9665f5e2014-11-22 04:04:38 +0000293
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000294 cinfo->err->trace_level++;
295
DRC9665f5e2014-11-22 04:04:38 +0000296 } else if (keymatch(arg, "version", 4)) {
297 fprintf(stderr, "%s version %s (build %s)\n",
298 PACKAGE_NAME, VERSION, BUILD);
299 exit(EXIT_SUCCESS);
300
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000301 } else if (keymatch(arg, "grayscale", 2) || keymatch(arg, "greyscale",2)) {
302 /* Force a monochrome JPEG file to be generated. */
303 jpeg_set_colorspace(cinfo, JCS_GRAYSCALE);
304
Guido Vollbeding5829cb22012-01-15 00:00:00 +0000305 } else if (keymatch(arg, "rgb", 3)) {
306 /* Force an RGB JPEG file to be generated. */
307 jpeg_set_colorspace(cinfo, JCS_RGB);
308
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000309 } else if (keymatch(arg, "maxmemory", 3)) {
310 /* Maximum memory in Kb (or Mb with 'm'). */
311 long lval;
312 char ch = 'x';
313
DRCe5eaf372014-05-09 18:00:32 +0000314 if (++argn >= argc) /* advance to next argument */
315 usage();
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000316 if (sscanf(argv[argn], "%ld%c", &lval, &ch) < 1)
DRCe5eaf372014-05-09 18:00:32 +0000317 usage();
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000318 if (ch == 'm' || ch == 'M')
DRCe5eaf372014-05-09 18:00:32 +0000319 lval *= 1000L;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000320 cinfo->mem->max_memory_to_use = lval * 1000L;
321
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000322 } else if (keymatch(arg, "optimize", 1) || keymatch(arg, "optimise", 1)) {
323 /* Enable entropy parm optimization. */
324#ifdef ENTROPY_OPT_SUPPORTED
325 cinfo->optimize_coding = TRUE;
326#else
DRCab706232013-01-18 23:42:31 +0000327 fprintf(stderr, "%s: sorry, entropy optimization was not compiled in\n",
DRCe5eaf372014-05-09 18:00:32 +0000328 progname);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000329 exit(EXIT_FAILURE);
330#endif
331
332 } else if (keymatch(arg, "outfile", 4)) {
333 /* Set output file name. */
DRCe5eaf372014-05-09 18:00:32 +0000334 if (++argn >= argc) /* advance to next argument */
335 usage();
336 outfilename = argv[argn]; /* save it away for later use */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000337
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000338 } else if (keymatch(arg, "progressive", 1)) {
339 /* Select simple progressive mode. */
340#ifdef C_PROGRESSIVE_SUPPORTED
341 simple_progressive = TRUE;
342 /* We must postpone execution until num_components is known. */
343#else
DRCab706232013-01-18 23:42:31 +0000344 fprintf(stderr, "%s: sorry, progressive output was not compiled in\n",
DRCe5eaf372014-05-09 18:00:32 +0000345 progname);
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000346 exit(EXIT_FAILURE);
347#endif
348
DRCab706232013-01-18 23:42:31 +0000349 } else if (keymatch(arg, "memdst", 2)) {
350 /* Use in-memory destination manager */
351#if JPEG_LIB_VERSION >= 80 || defined(MEM_SRCDST_SUPPORTED)
352 memdst = TRUE;
353#else
354 fprintf(stderr, "%s: sorry, in-memory destination manager was not compiled in\n",
355 progname);
356 exit(EXIT_FAILURE);
357#endif
358
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000359 } else if (keymatch(arg, "quality", 1)) {
Guido Vollbeding5996a252009-06-27 00:00:00 +0000360 /* Quality ratings (quantization table scaling factors). */
DRCe5eaf372014-05-09 18:00:32 +0000361 if (++argn >= argc) /* advance to next argument */
362 usage();
Guido Vollbeding5996a252009-06-27 00:00:00 +0000363 qualityarg = argv[argn];
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000364
365 } else if (keymatch(arg, "qslots", 2)) {
366 /* Quantization table slot numbers. */
DRCe5eaf372014-05-09 18:00:32 +0000367 if (++argn >= argc) /* advance to next argument */
368 usage();
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000369 qslotsarg = argv[argn];
370 /* Must delay setting qslots until after we have processed any
371 * colorspace-determining switches, since jpeg_set_colorspace sets
372 * default quant table numbers.
373 */
374
375 } else if (keymatch(arg, "qtables", 2)) {
376 /* Quantization tables fetched from file. */
DRCe5eaf372014-05-09 18:00:32 +0000377 if (++argn >= argc) /* advance to next argument */
378 usage();
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000379 qtablefile = argv[argn];
380 /* We postpone actually reading the file in case -quality comes later. */
381
382 } else if (keymatch(arg, "restart", 1)) {
383 /* Restart interval in MCU rows (or in MCUs with 'b'). */
384 long lval;
385 char ch = 'x';
386
DRCe5eaf372014-05-09 18:00:32 +0000387 if (++argn >= argc) /* advance to next argument */
388 usage();
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000389 if (sscanf(argv[argn], "%ld%c", &lval, &ch) < 1)
DRCe5eaf372014-05-09 18:00:32 +0000390 usage();
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000391 if (lval < 0 || lval > 65535L)
DRCe5eaf372014-05-09 18:00:32 +0000392 usage();
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000393 if (ch == 'b' || ch == 'B') {
DRCe5eaf372014-05-09 18:00:32 +0000394 cinfo->restart_interval = (unsigned int) lval;
395 cinfo->restart_in_rows = 0; /* else prior '-restart n' overrides me */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000396 } else {
DRCe5eaf372014-05-09 18:00:32 +0000397 cinfo->restart_in_rows = (int) lval;
398 /* restart_interval will be computed during startup */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000399 }
400
401 } else if (keymatch(arg, "sample", 2)) {
402 /* Set sampling factors. */
DRCe5eaf372014-05-09 18:00:32 +0000403 if (++argn >= argc) /* advance to next argument */
404 usage();
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000405 samplearg = argv[argn];
406 /* Must delay setting sample factors until after we have processed any
407 * colorspace-determining switches, since jpeg_set_colorspace sets
408 * default sampling factors.
409 */
410
Guido Vollbeding5996a252009-06-27 00:00:00 +0000411 } else if (keymatch(arg, "scans", 4)) {
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000412 /* Set scan script. */
413#ifdef C_MULTISCAN_FILES_SUPPORTED
DRCe5eaf372014-05-09 18:00:32 +0000414 if (++argn >= argc) /* advance to next argument */
415 usage();
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000416 scansarg = argv[argn];
417 /* We must postpone reading the file in case -progressive appears. */
418#else
DRCab706232013-01-18 23:42:31 +0000419 fprintf(stderr, "%s: sorry, multi-scan output was not compiled in\n",
DRCe5eaf372014-05-09 18:00:32 +0000420 progname);
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000421 exit(EXIT_FAILURE);
422#endif
423
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000424 } else if (keymatch(arg, "smooth", 2)) {
425 /* Set input smoothing factor. */
426 int val;
427
DRCe5eaf372014-05-09 18:00:32 +0000428 if (++argn >= argc) /* advance to next argument */
429 usage();
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000430 if (sscanf(argv[argn], "%d", &val) != 1)
DRCe5eaf372014-05-09 18:00:32 +0000431 usage();
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000432 if (val < 0 || val > 100)
DRCe5eaf372014-05-09 18:00:32 +0000433 usage();
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000434 cinfo->smoothing_factor = val;
435
436 } else if (keymatch(arg, "targa", 1)) {
437 /* Input file is Targa format. */
438 is_targa = TRUE;
439
440 } else {
DRCe5eaf372014-05-09 18:00:32 +0000441 usage(); /* bogus switch */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000442 }
443 }
444
445 /* Post-switch-scanning cleanup */
446
447 if (for_real) {
448
449 /* Set quantization tables for selected quality. */
450 /* Some or all may be overridden if -qtables is present. */
DRCe5eaf372014-05-09 18:00:32 +0000451 if (qualityarg != NULL) /* process -quality if it was present */
Guido Vollbeding5996a252009-06-27 00:00:00 +0000452 if (! set_quality_ratings(cinfo, qualityarg, force_baseline))
DRCe5eaf372014-05-09 18:00:32 +0000453 usage();
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000454
DRCe5eaf372014-05-09 18:00:32 +0000455 if (qtablefile != NULL) /* process -qtables if it was present */
Guido Vollbeding5996a252009-06-27 00:00:00 +0000456 if (! read_quant_tables(cinfo, qtablefile, force_baseline))
DRCe5eaf372014-05-09 18:00:32 +0000457 usage();
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000458
DRCe5eaf372014-05-09 18:00:32 +0000459 if (qslotsarg != NULL) /* process -qslots if it was present */
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000460 if (! set_quant_slots(cinfo, qslotsarg))
DRCe5eaf372014-05-09 18:00:32 +0000461 usage();
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000462
DRCe5eaf372014-05-09 18:00:32 +0000463 if (samplearg != NULL) /* process -sample if it was present */
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000464 if (! set_sample_factors(cinfo, samplearg))
DRCe5eaf372014-05-09 18:00:32 +0000465 usage();
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000466
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000467#ifdef C_PROGRESSIVE_SUPPORTED
DRCe5eaf372014-05-09 18:00:32 +0000468 if (simple_progressive) /* process -progressive; -scans can override */
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000469 jpeg_simple_progression(cinfo);
470#endif
471
472#ifdef C_MULTISCAN_FILES_SUPPORTED
DRCe5eaf372014-05-09 18:00:32 +0000473 if (scansarg != NULL) /* process -scans if it was present */
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000474 if (! read_scan_script(cinfo, scansarg))
DRCe5eaf372014-05-09 18:00:32 +0000475 usage();
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000476#endif
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000477 }
478
DRCe5eaf372014-05-09 18:00:32 +0000479 return argn; /* return index of next arg (file name) */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000480}
481
482
483/*
484 * The main program.
485 */
486
Thomas G. Lane489583f1996-02-07 00:00:00 +0000487int
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000488main (int argc, char **argv)
489{
490 struct jpeg_compress_struct cinfo;
491 struct jpeg_error_mgr jerr;
492#ifdef PROGRESS_REPORT
493 struct cdjpeg_progress_mgr progress;
494#endif
495 int file_index;
496 cjpeg_source_ptr src_mgr;
497 FILE * input_file;
DRCab706232013-01-18 23:42:31 +0000498 FILE * output_file = NULL;
499 unsigned char *outbuffer = NULL;
500 unsigned long outsize = 0;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000501 JDIMENSION num_scanlines;
502
503 /* On Mac, fetch a command line. */
504#ifdef USE_CCOMMAND
505 argc = ccommand(&argv);
506#endif
507
508 progname = argv[0];
509 if (progname == NULL || progname[0] == 0)
DRCe5eaf372014-05-09 18:00:32 +0000510 progname = "cjpeg"; /* in case C library doesn't provide it */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000511
512 /* Initialize the JPEG compression object with default error handling. */
513 cinfo.err = jpeg_std_error(&jerr);
514 jpeg_create_compress(&cinfo);
515 /* Add some application-specific error messages (from cderror.h) */
Thomas G. Lane9ba2f5e1994-12-07 00:00:00 +0000516 jerr.addon_message_table = cdjpeg_message_table;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000517 jerr.first_addon_message = JMSG_FIRSTADDONCODE;
518 jerr.last_addon_message = JMSG_LASTADDONCODE;
519
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000520 /* Initialize JPEG parameters.
521 * Much of this may be overridden later.
522 * In particular, we don't yet know the input file's color space,
523 * but we need to provide some value for jpeg_set_defaults() to work.
524 */
525
526 cinfo.in_color_space = JCS_RGB; /* arbitrary guess */
527 jpeg_set_defaults(&cinfo);
528
529 /* Scan command line to find file names.
530 * It is convenient to use just one switch-parsing routine, but the switch
531 * values read here are ignored; we will rescan the switches after opening
532 * the input file.
533 */
534
535 file_index = parse_switches(&cinfo, argc, argv, 0, FALSE);
536
537#ifdef TWO_FILE_COMMANDLINE
DRCab706232013-01-18 23:42:31 +0000538 if (!memdst) {
539 /* Must have either -outfile switch or explicit output file name */
540 if (outfilename == NULL) {
541 if (file_index != argc-2) {
542 fprintf(stderr, "%s: must name one input and one output file\n",
543 progname);
544 usage();
545 }
546 outfilename = argv[file_index+1];
547 } else {
548 if (file_index != argc-1) {
549 fprintf(stderr, "%s: must name one input and one output file\n",
550 progname);
551 usage();
552 }
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000553 }
554 }
555#else
556 /* Unix style: expect zero or one file name */
557 if (file_index < argc-1) {
558 fprintf(stderr, "%s: only one input file\n", progname);
559 usage();
560 }
561#endif /* TWO_FILE_COMMANDLINE */
562
563 /* Open the input file. */
564 if (file_index < argc) {
565 if ((input_file = fopen(argv[file_index], READ_BINARY)) == NULL) {
566 fprintf(stderr, "%s: can't open %s\n", progname, argv[file_index]);
567 exit(EXIT_FAILURE);
568 }
569 } else {
570 /* default input file is stdin */
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000571 input_file = read_stdin();
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000572 }
573
574 /* Open the output file. */
575 if (outfilename != NULL) {
576 if ((output_file = fopen(outfilename, WRITE_BINARY)) == NULL) {
577 fprintf(stderr, "%s: can't open %s\n", progname, outfilename);
578 exit(EXIT_FAILURE);
579 }
DRCab706232013-01-18 23:42:31 +0000580 } else if (!memdst) {
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000581 /* default output file is stdout */
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000582 output_file = write_stdout();
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000583 }
584
585#ifdef PROGRESS_REPORT
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000586 start_progress_monitor((j_common_ptr) &cinfo, &progress);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000587#endif
588
589 /* Figure out the input file format, and set up to read it. */
590 src_mgr = select_file_type(&cinfo, input_file);
591 src_mgr->input_file = input_file;
592
593 /* Read the input file header to obtain file size & colorspace. */
594 (*src_mgr->start_input) (&cinfo, src_mgr);
595
596 /* Now that we know input colorspace, fix colorspace-dependent defaults */
597 jpeg_default_colorspace(&cinfo);
598
599 /* Adjust default compression parameters by re-parsing the options */
600 file_index = parse_switches(&cinfo, argc, argv, 0, TRUE);
601
602 /* Specify data destination for compression */
DRCab706232013-01-18 23:42:31 +0000603#if JPEG_LIB_VERSION >= 80 || defined(MEM_SRCDST_SUPPORTED)
604 if (memdst)
605 jpeg_mem_dest(&cinfo, &outbuffer, &outsize);
606 else
607#endif
608 jpeg_stdio_dest(&cinfo, output_file);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000609
610 /* Start compressor */
611 jpeg_start_compress(&cinfo, TRUE);
612
613 /* Process data */
614 while (cinfo.next_scanline < cinfo.image_height) {
615 num_scanlines = (*src_mgr->get_pixel_rows) (&cinfo, src_mgr);
616 (void) jpeg_write_scanlines(&cinfo, src_mgr->buffer, num_scanlines);
617 }
618
619 /* Finish compression and release memory */
620 (*src_mgr->finish_input) (&cinfo, src_mgr);
621 jpeg_finish_compress(&cinfo);
622 jpeg_destroy_compress(&cinfo);
623
Thomas G. Lane9ba2f5e1994-12-07 00:00:00 +0000624 /* Close files, if we opened them */
625 if (input_file != stdin)
626 fclose(input_file);
DRCab706232013-01-18 23:42:31 +0000627 if (output_file != stdout && output_file != NULL)
Thomas G. Lane9ba2f5e1994-12-07 00:00:00 +0000628 fclose(output_file);
629
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000630#ifdef PROGRESS_REPORT
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000631 end_progress_monitor((j_common_ptr) &cinfo);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000632#endif
633
DRCab706232013-01-18 23:42:31 +0000634 if (memdst) {
635 fprintf(stderr, "Compressed size: %lu bytes\n", outsize);
636 if (outbuffer != NULL)
637 free(outbuffer);
638 }
639
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000640 /* All done. */
641 exit(jerr.num_warnings ? EXIT_WARNING : EXIT_SUCCESS);
DRCe5eaf372014-05-09 18:00:32 +0000642 return 0; /* suppress no-return-value warnings */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000643}