blob: 73dbe530a9a7be207b8a8964c28e4e64ebb854a1 [file] [log] [blame]
Thomas G. Lanebc79e061995-08-02 00:00:00 +00001/*
2 * jpegtran.c
3 *
Guido Vollbedingf18f81b2010-02-28 00:00:00 +00004 * Copyright (C) 1995-2010, Thomas G. Lane, Guido Vollbeding.
DRC2cdd2ae2010-10-10 06:54:21 +00005 * Copyright (C) 2010, D. R. Commander.
Thomas G. Lanebc79e061995-08-02 00:00:00 +00006 * This file is part of the Independent JPEG Group's software.
7 * For conditions of distribution and use, see the accompanying README file.
8 *
9 * This file contains a command-line user interface for JPEG transcoding.
Guido Vollbeding989630f2010-01-10 00:00:00 +000010 * It is very similar to cjpeg.c, and partly to djpeg.c, but provides
11 * lossless transcoding between different JPEG file formats. It also
12 * provides some lossless and sort-of-lossless transformations of JPEG data.
Thomas G. Lanebc79e061995-08-02 00:00:00 +000013 */
14
15#include "cdjpeg.h" /* Common decls for cjpeg/djpeg applications */
Thomas G. Lane5ead57a1998-03-27 00:00:00 +000016#include "transupp.h" /* Support routines for jpegtran */
Thomas G. Lanebc79e061995-08-02 00:00:00 +000017#include "jversion.h" /* for version message */
DRC2cdd2ae2010-10-10 06:54:21 +000018#include "config.h"
Thomas G. Lanebc79e061995-08-02 00:00:00 +000019
20#ifdef USE_CCOMMAND /* command-line reader for Macintosh */
21#ifdef __MWERKS__
Thomas G. Lane489583f1996-02-07 00:00:00 +000022#include <SIOUX.h> /* Metrowerks needs this */
23#include <console.h> /* ... and this */
Thomas G. Lanebc79e061995-08-02 00:00:00 +000024#endif
25#ifdef THINK_C
26#include <console.h> /* Think declares it here */
27#endif
28#endif
29
30
31/*
32 * Argument-parsing code.
33 * The switch parser is designed to be useful with DOS-style command line
34 * syntax, ie, intermixed switches and file names, where only the switches
35 * to the left of a given file name affect processing of that file.
36 * The main program in this file doesn't actually use this capability...
37 */
38
39
40static const char * progname; /* program name for error messages */
41static char * outfilename; /* for -outfile switch */
Thomas G. Lane5ead57a1998-03-27 00:00:00 +000042static JCOPY_OPTION copyoption; /* -copy switch */
43static jpeg_transform_info transformoption; /* image transformation options */
Thomas G. Lanebc79e061995-08-02 00:00:00 +000044
45
Thomas G. Lane489583f1996-02-07 00:00:00 +000046LOCAL(void)
Thomas G. Lanebc79e061995-08-02 00:00:00 +000047usage (void)
48/* complain about bad command line */
49{
50 fprintf(stderr, "usage: %s [switches] ", progname);
51#ifdef TWO_FILE_COMMANDLINE
52 fprintf(stderr, "inputfile outputfile\n");
53#else
54 fprintf(stderr, "[inputfile]\n");
55#endif
56
57 fprintf(stderr, "Switches (names may be abbreviated):\n");
Thomas G. Lane5ead57a1998-03-27 00:00:00 +000058 fprintf(stderr, " -copy none Copy no extra markers from source file\n");
59 fprintf(stderr, " -copy comments Copy only comment markers (default)\n");
60 fprintf(stderr, " -copy all Copy all extra markers\n");
Thomas G. Lanebc79e061995-08-02 00:00:00 +000061#ifdef ENTROPY_OPT_SUPPORTED
62 fprintf(stderr, " -optimize Optimize Huffman table (smaller file, but slow compression)\n");
63#endif
64#ifdef C_PROGRESSIVE_SUPPORTED
65 fprintf(stderr, " -progressive Create progressive JPEG file\n");
66#endif
Thomas G. Lane5ead57a1998-03-27 00:00:00 +000067 fprintf(stderr, "Switches for modifying the image:\n");
Guido Vollbedingf18f81b2010-02-28 00:00:00 +000068#if TRANSFORMS_SUPPORTED
Guido Vollbeding5996a252009-06-27 00:00:00 +000069 fprintf(stderr, " -crop WxH+X+Y Crop to a rectangular subarea\n");
Thomas G. Lane5ead57a1998-03-27 00:00:00 +000070 fprintf(stderr, " -grayscale Reduce to grayscale (omit color data)\n");
71 fprintf(stderr, " -flip [horizontal|vertical] Mirror image (left-right or top-bottom)\n");
Guido Vollbeding5996a252009-06-27 00:00:00 +000072 fprintf(stderr, " -perfect Fail if there is non-transformable edge blocks\n");
Thomas G. Lane5ead57a1998-03-27 00:00:00 +000073 fprintf(stderr, " -rotate [90|180|270] Rotate image (degrees clockwise)\n");
Guido Vollbedingf18f81b2010-02-28 00:00:00 +000074#endif
Guido Vollbedingf18f81b2010-02-28 00:00:00 +000075#if TRANSFORMS_SUPPORTED
Thomas G. Lane5ead57a1998-03-27 00:00:00 +000076 fprintf(stderr, " -transpose Transpose image\n");
77 fprintf(stderr, " -transverse Transverse transpose image\n");
78 fprintf(stderr, " -trim Drop non-transformable edge blocks\n");
Guido Vollbedingf18f81b2010-02-28 00:00:00 +000079#endif
Thomas G. Lanebc79e061995-08-02 00:00:00 +000080 fprintf(stderr, "Switches for advanced users:\n");
81 fprintf(stderr, " -restart N Set restart interval in rows, or in blocks with B\n");
82 fprintf(stderr, " -maxmemory N Maximum memory to use (in kbytes)\n");
83 fprintf(stderr, " -outfile name Specify name for output file\n");
84 fprintf(stderr, " -verbose or -debug Emit debug output\n");
85 fprintf(stderr, "Switches for wizards:\n");
86#ifdef C_ARITH_CODING_SUPPORTED
87 fprintf(stderr, " -arithmetic Use arithmetic coding\n");
88#endif
89#ifdef C_MULTISCAN_FILES_SUPPORTED
90 fprintf(stderr, " -scans file Create multi-scan JPEG per script file\n");
91#endif
92 exit(EXIT_FAILURE);
93}
94
95
Thomas G. Lane5ead57a1998-03-27 00:00:00 +000096LOCAL(void)
97select_transform (JXFORM_CODE transform)
98/* Silly little routine to detect multiple transform options,
99 * which we can't handle.
100 */
101{
102#if TRANSFORMS_SUPPORTED
103 if (transformoption.transform == JXFORM_NONE ||
104 transformoption.transform == transform) {
105 transformoption.transform = transform;
106 } else {
107 fprintf(stderr, "%s: can only do one image transformation at a time\n",
108 progname);
109 usage();
110 }
111#else
112 fprintf(stderr, "%s: sorry, image transformation was not compiled\n",
113 progname);
114 exit(EXIT_FAILURE);
115#endif
116}
117
118
Thomas G. Lane489583f1996-02-07 00:00:00 +0000119LOCAL(int)
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000120parse_switches (j_compress_ptr cinfo, int argc, char **argv,
121 int last_file_arg_seen, boolean for_real)
122/* Parse optional switches.
123 * Returns argv[] index of first file-name argument (== argc if none).
124 * Any file names with indexes <= last_file_arg_seen are ignored;
125 * they have presumably been processed in a previous iteration.
126 * (Pass 0 for last_file_arg_seen on the first or only iteration.)
127 * for_real is FALSE on the first (dummy) pass; we may skip any expensive
128 * processing.
129 */
130{
131 int argn;
132 char * arg;
133 boolean simple_progressive;
134 char * scansarg = NULL; /* saves -scans parm if any */
135
136 /* Set up default JPEG parameters. */
137 simple_progressive = FALSE;
138 outfilename = NULL;
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000139 copyoption = JCOPYOPT_DEFAULT;
140 transformoption.transform = JXFORM_NONE;
Guido Vollbeding5996a252009-06-27 00:00:00 +0000141 transformoption.perfect = FALSE;
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000142 transformoption.trim = FALSE;
143 transformoption.force_grayscale = FALSE;
Guido Vollbeding5996a252009-06-27 00:00:00 +0000144 transformoption.crop = FALSE;
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000145 cinfo->err->trace_level = 0;
146
147 /* Scan command line options, adjust parameters */
148
149 for (argn = 1; argn < argc; argn++) {
150 arg = argv[argn];
151 if (*arg != '-') {
152 /* Not a switch, must be a file name argument */
153 if (argn <= last_file_arg_seen) {
154 outfilename = NULL; /* -outfile applies to just one input file */
155 continue; /* ignore this name if previously processed */
156 }
157 break; /* else done parsing switches */
158 }
159 arg++; /* advance past switch marker character */
160
161 if (keymatch(arg, "arithmetic", 1)) {
162 /* Use arithmetic coding. */
163#ifdef C_ARITH_CODING_SUPPORTED
164 cinfo->arith_code = TRUE;
165#else
166 fprintf(stderr, "%s: sorry, arithmetic coding not supported\n",
167 progname);
168 exit(EXIT_FAILURE);
169#endif
170
Guido Vollbeding5996a252009-06-27 00:00:00 +0000171 } else if (keymatch(arg, "copy", 2)) {
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000172 /* Select which extra markers to copy. */
173 if (++argn >= argc) /* advance to next argument */
174 usage();
175 if (keymatch(argv[argn], "none", 1)) {
176 copyoption = JCOPYOPT_NONE;
177 } else if (keymatch(argv[argn], "comments", 1)) {
178 copyoption = JCOPYOPT_COMMENTS;
179 } else if (keymatch(argv[argn], "all", 1)) {
180 copyoption = JCOPYOPT_ALL;
181 } else
182 usage();
183
Guido Vollbeding5996a252009-06-27 00:00:00 +0000184 } else if (keymatch(arg, "crop", 2)) {
185 /* Perform lossless cropping. */
186#if TRANSFORMS_SUPPORTED
187 if (++argn >= argc) /* advance to next argument */
188 usage();
189 if (! jtransform_parse_crop_spec(&transformoption, argv[argn])) {
190 fprintf(stderr, "%s: bogus -crop argument '%s'\n",
191 progname, argv[argn]);
192 exit(EXIT_FAILURE);
193 }
194#else
195 select_transform(JXFORM_NONE); /* force an error */
196#endif
197
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000198 } else if (keymatch(arg, "debug", 1) || keymatch(arg, "verbose", 1)) {
199 /* Enable debug printouts. */
200 /* On first -d, print version identification */
201 static boolean printed_version = FALSE;
202
203 if (! printed_version) {
DRC2cdd2ae2010-10-10 06:54:21 +0000204 fprintf(stderr, "%s version %s (build %s)\n",
205 PACKAGE_NAME, VERSION, BUILD);
206 fprintf(stderr, "%s\n\n", LJTCOPYRIGHT);
207 fprintf(stderr, "Based on Independent JPEG Group's libjpeg, version %s\n%s\n\n",
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000208 JVERSION, JCOPYRIGHT);
209 printed_version = TRUE;
210 }
211 cinfo->err->trace_level++;
212
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000213 } else if (keymatch(arg, "flip", 1)) {
214 /* Mirror left-right or top-bottom. */
215 if (++argn >= argc) /* advance to next argument */
216 usage();
217 if (keymatch(argv[argn], "horizontal", 1))
218 select_transform(JXFORM_FLIP_H);
219 else if (keymatch(argv[argn], "vertical", 1))
220 select_transform(JXFORM_FLIP_V);
221 else
222 usage();
223
224 } else if (keymatch(arg, "grayscale", 1) || keymatch(arg, "greyscale",1)) {
225 /* Force to grayscale. */
226#if TRANSFORMS_SUPPORTED
227 transformoption.force_grayscale = TRUE;
228#else
229 select_transform(JXFORM_NONE); /* force an error */
230#endif
231
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000232 } else if (keymatch(arg, "maxmemory", 3)) {
233 /* Maximum memory in Kb (or Mb with 'm'). */
234 long lval;
235 char ch = 'x';
236
237 if (++argn >= argc) /* advance to next argument */
238 usage();
239 if (sscanf(argv[argn], "%ld%c", &lval, &ch) < 1)
240 usage();
241 if (ch == 'm' || ch == 'M')
242 lval *= 1000L;
243 cinfo->mem->max_memory_to_use = lval * 1000L;
244
245 } else if (keymatch(arg, "optimize", 1) || keymatch(arg, "optimise", 1)) {
246 /* Enable entropy parm optimization. */
247#ifdef ENTROPY_OPT_SUPPORTED
248 cinfo->optimize_coding = TRUE;
249#else
250 fprintf(stderr, "%s: sorry, entropy optimization was not compiled\n",
251 progname);
252 exit(EXIT_FAILURE);
253#endif
254
255 } else if (keymatch(arg, "outfile", 4)) {
256 /* Set output file name. */
257 if (++argn >= argc) /* advance to next argument */
258 usage();
259 outfilename = argv[argn]; /* save it away for later use */
260
Guido Vollbeding5996a252009-06-27 00:00:00 +0000261 } else if (keymatch(arg, "perfect", 2)) {
262 /* Fail if there is any partial edge MCUs that the transform can't
263 * handle. */
264 transformoption.perfect = TRUE;
265
266 } else if (keymatch(arg, "progressive", 2)) {
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000267 /* Select simple progressive mode. */
268#ifdef C_PROGRESSIVE_SUPPORTED
269 simple_progressive = TRUE;
270 /* We must postpone execution until num_components is known. */
271#else
272 fprintf(stderr, "%s: sorry, progressive output was not compiled\n",
273 progname);
274 exit(EXIT_FAILURE);
275#endif
276
277 } else if (keymatch(arg, "restart", 1)) {
278 /* Restart interval in MCU rows (or in MCUs with 'b'). */
279 long lval;
280 char ch = 'x';
281
282 if (++argn >= argc) /* advance to next argument */
283 usage();
284 if (sscanf(argv[argn], "%ld%c", &lval, &ch) < 1)
285 usage();
286 if (lval < 0 || lval > 65535L)
287 usage();
288 if (ch == 'b' || ch == 'B') {
289 cinfo->restart_interval = (unsigned int) lval;
290 cinfo->restart_in_rows = 0; /* else prior '-restart n' overrides me */
291 } else {
292 cinfo->restart_in_rows = (int) lval;
293 /* restart_interval will be computed during startup */
294 }
295
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000296 } else if (keymatch(arg, "rotate", 2)) {
297 /* Rotate 90, 180, or 270 degrees (measured clockwise). */
298 if (++argn >= argc) /* advance to next argument */
299 usage();
300 if (keymatch(argv[argn], "90", 2))
301 select_transform(JXFORM_ROT_90);
302 else if (keymatch(argv[argn], "180", 3))
303 select_transform(JXFORM_ROT_180);
304 else if (keymatch(argv[argn], "270", 3))
305 select_transform(JXFORM_ROT_270);
306 else
307 usage();
308
309 } else if (keymatch(arg, "scans", 1)) {
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000310 /* Set scan script. */
311#ifdef C_MULTISCAN_FILES_SUPPORTED
312 if (++argn >= argc) /* advance to next argument */
313 usage();
314 scansarg = argv[argn];
315 /* We must postpone reading the file in case -progressive appears. */
316#else
317 fprintf(stderr, "%s: sorry, multi-scan output was not compiled\n",
318 progname);
319 exit(EXIT_FAILURE);
320#endif
321
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000322 } else if (keymatch(arg, "transpose", 1)) {
323 /* Transpose (across UL-to-LR axis). */
324 select_transform(JXFORM_TRANSPOSE);
325
326 } else if (keymatch(arg, "transverse", 6)) {
327 /* Transverse transpose (across UR-to-LL axis). */
328 select_transform(JXFORM_TRANSVERSE);
329
330 } else if (keymatch(arg, "trim", 3)) {
331 /* Trim off any partial edge MCUs that the transform can't handle. */
332 transformoption.trim = TRUE;
333
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000334 } else {
335 usage(); /* bogus switch */
336 }
337 }
338
339 /* Post-switch-scanning cleanup */
340
341 if (for_real) {
342
343#ifdef C_PROGRESSIVE_SUPPORTED
344 if (simple_progressive) /* process -progressive; -scans can override */
345 jpeg_simple_progression(cinfo);
346#endif
347
348#ifdef C_MULTISCAN_FILES_SUPPORTED
349 if (scansarg != NULL) /* process -scans if it was present */
350 if (! read_scan_script(cinfo, scansarg))
351 usage();
352#endif
353 }
354
355 return argn; /* return index of next arg (file name) */
356}
357
358
359/*
360 * The main program.
361 */
362
Thomas G. Lane489583f1996-02-07 00:00:00 +0000363int
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000364main (int argc, char **argv)
365{
366 struct jpeg_decompress_struct srcinfo;
367 struct jpeg_compress_struct dstinfo;
368 struct jpeg_error_mgr jsrcerr, jdsterr;
369#ifdef PROGRESS_REPORT
370 struct cdjpeg_progress_mgr progress;
371#endif
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000372 jvirt_barray_ptr * src_coef_arrays;
373 jvirt_barray_ptr * dst_coef_arrays;
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000374 int file_index;
Guido Vollbeding5996a252009-06-27 00:00:00 +0000375 /* We assume all-in-memory processing and can therefore use only a
376 * single file pointer for sequential input and output operation.
377 */
378 FILE * fp;
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000379
380 /* On Mac, fetch a command line. */
381#ifdef USE_CCOMMAND
382 argc = ccommand(&argv);
383#endif
384
385 progname = argv[0];
386 if (progname == NULL || progname[0] == 0)
387 progname = "jpegtran"; /* in case C library doesn't provide it */
388
389 /* Initialize the JPEG decompression object with default error handling. */
390 srcinfo.err = jpeg_std_error(&jsrcerr);
391 jpeg_create_decompress(&srcinfo);
392 /* Initialize the JPEG compression object with default error handling. */
393 dstinfo.err = jpeg_std_error(&jdsterr);
394 jpeg_create_compress(&dstinfo);
395
396 /* Now safe to enable signal catcher.
397 * Note: we assume only the decompression object will have virtual arrays.
398 */
399#ifdef NEED_SIGNAL_CATCHER
400 enable_signal_catcher((j_common_ptr) &srcinfo);
401#endif
402
403 /* Scan command line to find file names.
404 * It is convenient to use just one switch-parsing routine, but the switch
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000405 * values read here are mostly ignored; we will rescan the switches after
406 * opening the input file. Also note that most of the switches affect the
407 * destination JPEG object, so we parse into that and then copy over what
408 * needs to affects the source too.
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000409 */
410
411 file_index = parse_switches(&dstinfo, argc, argv, 0, FALSE);
412 jsrcerr.trace_level = jdsterr.trace_level;
Thomas G. Lane489583f1996-02-07 00:00:00 +0000413 srcinfo.mem->max_memory_to_use = dstinfo.mem->max_memory_to_use;
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000414
415#ifdef TWO_FILE_COMMANDLINE
416 /* Must have either -outfile switch or explicit output file name */
417 if (outfilename == NULL) {
418 if (file_index != argc-2) {
419 fprintf(stderr, "%s: must name one input and one output file\n",
420 progname);
421 usage();
422 }
423 outfilename = argv[file_index+1];
424 } else {
425 if (file_index != argc-1) {
426 fprintf(stderr, "%s: must name one input and one output file\n",
427 progname);
428 usage();
429 }
430 }
431#else
432 /* Unix style: expect zero or one file name */
433 if (file_index < argc-1) {
434 fprintf(stderr, "%s: only one input file\n", progname);
435 usage();
436 }
437#endif /* TWO_FILE_COMMANDLINE */
438
439 /* Open the input file. */
440 if (file_index < argc) {
Guido Vollbeding5996a252009-06-27 00:00:00 +0000441 if ((fp = fopen(argv[file_index], READ_BINARY)) == NULL) {
442 fprintf(stderr, "%s: can't open %s for reading\n", progname, argv[file_index]);
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000443 exit(EXIT_FAILURE);
444 }
445 } else {
446 /* default input file is stdin */
Guido Vollbeding5996a252009-06-27 00:00:00 +0000447 fp = read_stdin();
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000448 }
449
450#ifdef PROGRESS_REPORT
451 start_progress_monitor((j_common_ptr) &dstinfo, &progress);
452#endif
453
454 /* Specify data source for decompression */
Guido Vollbeding5996a252009-06-27 00:00:00 +0000455 jpeg_stdio_src(&srcinfo, fp);
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000456
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000457 /* Enable saving of extra markers that we want to copy */
458 jcopy_markers_setup(&srcinfo, copyoption);
459
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000460 /* Read file header */
461 (void) jpeg_read_header(&srcinfo, TRUE);
462
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000463 /* Any space needed by a transform option must be requested before
464 * jpeg_read_coefficients so that memory allocation will be done right.
465 */
466#if TRANSFORMS_SUPPORTED
Guido Vollbeding989630f2010-01-10 00:00:00 +0000467 /* Fail right away if -perfect is given and transformation is not perfect.
Guido Vollbeding5996a252009-06-27 00:00:00 +0000468 */
Guido Vollbeding989630f2010-01-10 00:00:00 +0000469 if (!jtransform_request_workspace(&srcinfo, &transformoption)) {
Guido Vollbeding5996a252009-06-27 00:00:00 +0000470 fprintf(stderr, "%s: transformation is not perfect\n", progname);
471 exit(EXIT_FAILURE);
472 }
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000473#endif
474
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000475 /* Read source file as DCT coefficients */
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000476 src_coef_arrays = jpeg_read_coefficients(&srcinfo);
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000477
478 /* Initialize destination compression parameters from source values */
479 jpeg_copy_critical_parameters(&srcinfo, &dstinfo);
480
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000481 /* Adjust destination parameters if required by transform options;
482 * also find out which set of coefficient arrays will hold the output.
483 */
484#if TRANSFORMS_SUPPORTED
485 dst_coef_arrays = jtransform_adjust_parameters(&srcinfo, &dstinfo,
486 src_coef_arrays,
487 &transformoption);
488#else
489 dst_coef_arrays = src_coef_arrays;
490#endif
491
Guido Vollbeding5996a252009-06-27 00:00:00 +0000492 /* Close input file, if we opened it.
493 * Note: we assume that jpeg_read_coefficients consumed all input
494 * until JPEG_REACHED_EOI, and that jpeg_finish_decompress will
495 * only consume more while (! cinfo->inputctl->eoi_reached).
496 * We cannot call jpeg_finish_decompress here since we still need the
497 * virtual arrays allocated from the source object for processing.
498 */
499 if (fp != stdin)
500 fclose(fp);
501
502 /* Open the output file. */
503 if (outfilename != NULL) {
504 if ((fp = fopen(outfilename, WRITE_BINARY)) == NULL) {
505 fprintf(stderr, "%s: can't open %s for writing\n", progname, outfilename);
506 exit(EXIT_FAILURE);
507 }
508 } else {
509 /* default output file is stdout */
510 fp = write_stdout();
511 }
512
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000513 /* Adjust default compression parameters by re-parsing the options */
514 file_index = parse_switches(&dstinfo, argc, argv, 0, TRUE);
515
516 /* Specify data destination for compression */
Guido Vollbeding5996a252009-06-27 00:00:00 +0000517 jpeg_stdio_dest(&dstinfo, fp);
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000518
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000519 /* Start compressor (note no image data is actually written here) */
520 jpeg_write_coefficients(&dstinfo, dst_coef_arrays);
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000521
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000522 /* Copy to the output file any extra markers that we want to preserve */
523 jcopy_markers_execute(&srcinfo, &dstinfo, copyoption);
524
525 /* Execute image transformation, if any */
526#if TRANSFORMS_SUPPORTED
527 jtransform_execute_transformation(&srcinfo, &dstinfo,
528 src_coef_arrays,
529 &transformoption);
530#endif
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000531
532 /* Finish compression and release memory */
533 jpeg_finish_compress(&dstinfo);
534 jpeg_destroy_compress(&dstinfo);
535 (void) jpeg_finish_decompress(&srcinfo);
536 jpeg_destroy_decompress(&srcinfo);
537
Guido Vollbeding5996a252009-06-27 00:00:00 +0000538 /* Close output file, if we opened it */
539 if (fp != stdout)
540 fclose(fp);
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000541
542#ifdef PROGRESS_REPORT
543 end_progress_monitor((j_common_ptr) &dstinfo);
544#endif
545
546 /* All done. */
547 exit(jsrcerr.num_warnings + jdsterr.num_warnings ?EXIT_WARNING:EXIT_SUCCESS);
548 return 0; /* suppress no-return-value warnings */
549}