blob: f861464b7c8bdbe084cab33bff1449ab5aa760c2 [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;
DRCba5ea512011-03-04 03:20:34 +0000145 transformoption.slow_hflip = FALSE;
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000146 cinfo->err->trace_level = 0;
147
148 /* Scan command line options, adjust parameters */
149
150 for (argn = 1; argn < argc; argn++) {
151 arg = argv[argn];
152 if (*arg != '-') {
153 /* Not a switch, must be a file name argument */
154 if (argn <= last_file_arg_seen) {
155 outfilename = NULL; /* -outfile applies to just one input file */
156 continue; /* ignore this name if previously processed */
157 }
158 break; /* else done parsing switches */
159 }
160 arg++; /* advance past switch marker character */
161
162 if (keymatch(arg, "arithmetic", 1)) {
163 /* Use arithmetic coding. */
164#ifdef C_ARITH_CODING_SUPPORTED
165 cinfo->arith_code = TRUE;
166#else
167 fprintf(stderr, "%s: sorry, arithmetic coding not supported\n",
168 progname);
169 exit(EXIT_FAILURE);
170#endif
171
Guido Vollbeding5996a252009-06-27 00:00:00 +0000172 } else if (keymatch(arg, "copy", 2)) {
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000173 /* Select which extra markers to copy. */
174 if (++argn >= argc) /* advance to next argument */
175 usage();
176 if (keymatch(argv[argn], "none", 1)) {
177 copyoption = JCOPYOPT_NONE;
178 } else if (keymatch(argv[argn], "comments", 1)) {
179 copyoption = JCOPYOPT_COMMENTS;
180 } else if (keymatch(argv[argn], "all", 1)) {
181 copyoption = JCOPYOPT_ALL;
182 } else
183 usage();
184
Guido Vollbeding5996a252009-06-27 00:00:00 +0000185 } else if (keymatch(arg, "crop", 2)) {
186 /* Perform lossless cropping. */
187#if TRANSFORMS_SUPPORTED
188 if (++argn >= argc) /* advance to next argument */
189 usage();
190 if (! jtransform_parse_crop_spec(&transformoption, argv[argn])) {
191 fprintf(stderr, "%s: bogus -crop argument '%s'\n",
192 progname, argv[argn]);
193 exit(EXIT_FAILURE);
194 }
195#else
196 select_transform(JXFORM_NONE); /* force an error */
197#endif
198
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000199 } else if (keymatch(arg, "debug", 1) || keymatch(arg, "verbose", 1)) {
200 /* Enable debug printouts. */
201 /* On first -d, print version identification */
202 static boolean printed_version = FALSE;
203
204 if (! printed_version) {
DRC2cdd2ae2010-10-10 06:54:21 +0000205 fprintf(stderr, "%s version %s (build %s)\n",
206 PACKAGE_NAME, VERSION, BUILD);
207 fprintf(stderr, "%s\n\n", LJTCOPYRIGHT);
208 fprintf(stderr, "Based on Independent JPEG Group's libjpeg, version %s\n%s\n\n",
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000209 JVERSION, JCOPYRIGHT);
210 printed_version = TRUE;
211 }
212 cinfo->err->trace_level++;
213
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000214 } else if (keymatch(arg, "flip", 1)) {
215 /* Mirror left-right or top-bottom. */
216 if (++argn >= argc) /* advance to next argument */
217 usage();
218 if (keymatch(argv[argn], "horizontal", 1))
219 select_transform(JXFORM_FLIP_H);
220 else if (keymatch(argv[argn], "vertical", 1))
221 select_transform(JXFORM_FLIP_V);
222 else
223 usage();
224
225 } else if (keymatch(arg, "grayscale", 1) || keymatch(arg, "greyscale",1)) {
226 /* Force to grayscale. */
227#if TRANSFORMS_SUPPORTED
228 transformoption.force_grayscale = TRUE;
229#else
230 select_transform(JXFORM_NONE); /* force an error */
231#endif
232
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000233 } else if (keymatch(arg, "maxmemory", 3)) {
234 /* Maximum memory in Kb (or Mb with 'm'). */
235 long lval;
236 char ch = 'x';
237
238 if (++argn >= argc) /* advance to next argument */
239 usage();
240 if (sscanf(argv[argn], "%ld%c", &lval, &ch) < 1)
241 usage();
242 if (ch == 'm' || ch == 'M')
243 lval *= 1000L;
244 cinfo->mem->max_memory_to_use = lval * 1000L;
245
246 } else if (keymatch(arg, "optimize", 1) || keymatch(arg, "optimise", 1)) {
247 /* Enable entropy parm optimization. */
248#ifdef ENTROPY_OPT_SUPPORTED
249 cinfo->optimize_coding = TRUE;
250#else
251 fprintf(stderr, "%s: sorry, entropy optimization was not compiled\n",
252 progname);
253 exit(EXIT_FAILURE);
254#endif
255
256 } else if (keymatch(arg, "outfile", 4)) {
257 /* Set output file name. */
258 if (++argn >= argc) /* advance to next argument */
259 usage();
260 outfilename = argv[argn]; /* save it away for later use */
261
Guido Vollbeding5996a252009-06-27 00:00:00 +0000262 } else if (keymatch(arg, "perfect", 2)) {
263 /* Fail if there is any partial edge MCUs that the transform can't
264 * handle. */
265 transformoption.perfect = TRUE;
266
267 } else if (keymatch(arg, "progressive", 2)) {
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000268 /* Select simple progressive mode. */
269#ifdef C_PROGRESSIVE_SUPPORTED
270 simple_progressive = TRUE;
271 /* We must postpone execution until num_components is known. */
272#else
273 fprintf(stderr, "%s: sorry, progressive output was not compiled\n",
274 progname);
275 exit(EXIT_FAILURE);
276#endif
277
278 } else if (keymatch(arg, "restart", 1)) {
279 /* Restart interval in MCU rows (or in MCUs with 'b'). */
280 long lval;
281 char ch = 'x';
282
283 if (++argn >= argc) /* advance to next argument */
284 usage();
285 if (sscanf(argv[argn], "%ld%c", &lval, &ch) < 1)
286 usage();
287 if (lval < 0 || lval > 65535L)
288 usage();
289 if (ch == 'b' || ch == 'B') {
290 cinfo->restart_interval = (unsigned int) lval;
291 cinfo->restart_in_rows = 0; /* else prior '-restart n' overrides me */
292 } else {
293 cinfo->restart_in_rows = (int) lval;
294 /* restart_interval will be computed during startup */
295 }
296
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000297 } else if (keymatch(arg, "rotate", 2)) {
298 /* Rotate 90, 180, or 270 degrees (measured clockwise). */
299 if (++argn >= argc) /* advance to next argument */
300 usage();
301 if (keymatch(argv[argn], "90", 2))
302 select_transform(JXFORM_ROT_90);
303 else if (keymatch(argv[argn], "180", 3))
304 select_transform(JXFORM_ROT_180);
305 else if (keymatch(argv[argn], "270", 3))
306 select_transform(JXFORM_ROT_270);
307 else
308 usage();
309
310 } else if (keymatch(arg, "scans", 1)) {
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000311 /* Set scan script. */
312#ifdef C_MULTISCAN_FILES_SUPPORTED
313 if (++argn >= argc) /* advance to next argument */
314 usage();
315 scansarg = argv[argn];
316 /* We must postpone reading the file in case -progressive appears. */
317#else
318 fprintf(stderr, "%s: sorry, multi-scan output was not compiled\n",
319 progname);
320 exit(EXIT_FAILURE);
321#endif
322
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000323 } else if (keymatch(arg, "transpose", 1)) {
324 /* Transpose (across UL-to-LR axis). */
325 select_transform(JXFORM_TRANSPOSE);
326
327 } else if (keymatch(arg, "transverse", 6)) {
328 /* Transverse transpose (across UR-to-LL axis). */
329 select_transform(JXFORM_TRANSVERSE);
330
331 } else if (keymatch(arg, "trim", 3)) {
332 /* Trim off any partial edge MCUs that the transform can't handle. */
333 transformoption.trim = TRUE;
334
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000335 } else {
336 usage(); /* bogus switch */
337 }
338 }
339
340 /* Post-switch-scanning cleanup */
341
342 if (for_real) {
343
344#ifdef C_PROGRESSIVE_SUPPORTED
345 if (simple_progressive) /* process -progressive; -scans can override */
346 jpeg_simple_progression(cinfo);
347#endif
348
349#ifdef C_MULTISCAN_FILES_SUPPORTED
350 if (scansarg != NULL) /* process -scans if it was present */
351 if (! read_scan_script(cinfo, scansarg))
352 usage();
353#endif
354 }
355
356 return argn; /* return index of next arg (file name) */
357}
358
359
360/*
361 * The main program.
362 */
363
Thomas G. Lane489583f1996-02-07 00:00:00 +0000364int
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000365main (int argc, char **argv)
366{
367 struct jpeg_decompress_struct srcinfo;
368 struct jpeg_compress_struct dstinfo;
369 struct jpeg_error_mgr jsrcerr, jdsterr;
370#ifdef PROGRESS_REPORT
371 struct cdjpeg_progress_mgr progress;
372#endif
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000373 jvirt_barray_ptr * src_coef_arrays;
374 jvirt_barray_ptr * dst_coef_arrays;
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000375 int file_index;
Guido Vollbeding5996a252009-06-27 00:00:00 +0000376 /* We assume all-in-memory processing and can therefore use only a
377 * single file pointer for sequential input and output operation.
378 */
379 FILE * fp;
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000380
381 /* On Mac, fetch a command line. */
382#ifdef USE_CCOMMAND
383 argc = ccommand(&argv);
384#endif
385
386 progname = argv[0];
387 if (progname == NULL || progname[0] == 0)
388 progname = "jpegtran"; /* in case C library doesn't provide it */
389
390 /* Initialize the JPEG decompression object with default error handling. */
391 srcinfo.err = jpeg_std_error(&jsrcerr);
392 jpeg_create_decompress(&srcinfo);
393 /* Initialize the JPEG compression object with default error handling. */
394 dstinfo.err = jpeg_std_error(&jdsterr);
395 jpeg_create_compress(&dstinfo);
396
397 /* Now safe to enable signal catcher.
398 * Note: we assume only the decompression object will have virtual arrays.
399 */
400#ifdef NEED_SIGNAL_CATCHER
401 enable_signal_catcher((j_common_ptr) &srcinfo);
402#endif
403
404 /* Scan command line to find file names.
405 * It is convenient to use just one switch-parsing routine, but the switch
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000406 * values read here are mostly ignored; we will rescan the switches after
407 * opening the input file. Also note that most of the switches affect the
408 * destination JPEG object, so we parse into that and then copy over what
409 * needs to affects the source too.
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000410 */
411
412 file_index = parse_switches(&dstinfo, argc, argv, 0, FALSE);
413 jsrcerr.trace_level = jdsterr.trace_level;
Thomas G. Lane489583f1996-02-07 00:00:00 +0000414 srcinfo.mem->max_memory_to_use = dstinfo.mem->max_memory_to_use;
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000415
416#ifdef TWO_FILE_COMMANDLINE
417 /* Must have either -outfile switch or explicit output file name */
418 if (outfilename == NULL) {
419 if (file_index != argc-2) {
420 fprintf(stderr, "%s: must name one input and one output file\n",
421 progname);
422 usage();
423 }
424 outfilename = argv[file_index+1];
425 } else {
426 if (file_index != argc-1) {
427 fprintf(stderr, "%s: must name one input and one output file\n",
428 progname);
429 usage();
430 }
431 }
432#else
433 /* Unix style: expect zero or one file name */
434 if (file_index < argc-1) {
435 fprintf(stderr, "%s: only one input file\n", progname);
436 usage();
437 }
438#endif /* TWO_FILE_COMMANDLINE */
439
440 /* Open the input file. */
441 if (file_index < argc) {
Guido Vollbeding5996a252009-06-27 00:00:00 +0000442 if ((fp = fopen(argv[file_index], READ_BINARY)) == NULL) {
443 fprintf(stderr, "%s: can't open %s for reading\n", progname, argv[file_index]);
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000444 exit(EXIT_FAILURE);
445 }
446 } else {
447 /* default input file is stdin */
Guido Vollbeding5996a252009-06-27 00:00:00 +0000448 fp = read_stdin();
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000449 }
450
451#ifdef PROGRESS_REPORT
452 start_progress_monitor((j_common_ptr) &dstinfo, &progress);
453#endif
454
455 /* Specify data source for decompression */
Guido Vollbeding5996a252009-06-27 00:00:00 +0000456 jpeg_stdio_src(&srcinfo, fp);
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000457
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000458 /* Enable saving of extra markers that we want to copy */
459 jcopy_markers_setup(&srcinfo, copyoption);
460
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000461 /* Read file header */
462 (void) jpeg_read_header(&srcinfo, TRUE);
463
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000464 /* Any space needed by a transform option must be requested before
465 * jpeg_read_coefficients so that memory allocation will be done right.
466 */
467#if TRANSFORMS_SUPPORTED
Guido Vollbeding989630f2010-01-10 00:00:00 +0000468 /* Fail right away if -perfect is given and transformation is not perfect.
Guido Vollbeding5996a252009-06-27 00:00:00 +0000469 */
Guido Vollbeding989630f2010-01-10 00:00:00 +0000470 if (!jtransform_request_workspace(&srcinfo, &transformoption)) {
Guido Vollbeding5996a252009-06-27 00:00:00 +0000471 fprintf(stderr, "%s: transformation is not perfect\n", progname);
472 exit(EXIT_FAILURE);
473 }
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000474#endif
475
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000476 /* Read source file as DCT coefficients */
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000477 src_coef_arrays = jpeg_read_coefficients(&srcinfo);
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000478
479 /* Initialize destination compression parameters from source values */
480 jpeg_copy_critical_parameters(&srcinfo, &dstinfo);
481
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000482 /* Adjust destination parameters if required by transform options;
483 * also find out which set of coefficient arrays will hold the output.
484 */
485#if TRANSFORMS_SUPPORTED
486 dst_coef_arrays = jtransform_adjust_parameters(&srcinfo, &dstinfo,
487 src_coef_arrays,
488 &transformoption);
489#else
490 dst_coef_arrays = src_coef_arrays;
491#endif
492
Guido Vollbeding5996a252009-06-27 00:00:00 +0000493 /* Close input file, if we opened it.
494 * Note: we assume that jpeg_read_coefficients consumed all input
495 * until JPEG_REACHED_EOI, and that jpeg_finish_decompress will
496 * only consume more while (! cinfo->inputctl->eoi_reached).
497 * We cannot call jpeg_finish_decompress here since we still need the
498 * virtual arrays allocated from the source object for processing.
499 */
500 if (fp != stdin)
501 fclose(fp);
502
503 /* Open the output file. */
504 if (outfilename != NULL) {
505 if ((fp = fopen(outfilename, WRITE_BINARY)) == NULL) {
506 fprintf(stderr, "%s: can't open %s for writing\n", progname, outfilename);
507 exit(EXIT_FAILURE);
508 }
509 } else {
510 /* default output file is stdout */
511 fp = write_stdout();
512 }
513
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000514 /* Adjust default compression parameters by re-parsing the options */
515 file_index = parse_switches(&dstinfo, argc, argv, 0, TRUE);
516
517 /* Specify data destination for compression */
Guido Vollbeding5996a252009-06-27 00:00:00 +0000518 jpeg_stdio_dest(&dstinfo, fp);
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000519
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000520 /* Start compressor (note no image data is actually written here) */
521 jpeg_write_coefficients(&dstinfo, dst_coef_arrays);
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000522
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000523 /* Copy to the output file any extra markers that we want to preserve */
524 jcopy_markers_execute(&srcinfo, &dstinfo, copyoption);
525
526 /* Execute image transformation, if any */
527#if TRANSFORMS_SUPPORTED
528 jtransform_execute_transformation(&srcinfo, &dstinfo,
529 src_coef_arrays,
530 &transformoption);
531#endif
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000532
533 /* Finish compression and release memory */
534 jpeg_finish_compress(&dstinfo);
535 jpeg_destroy_compress(&dstinfo);
536 (void) jpeg_finish_decompress(&srcinfo);
537 jpeg_destroy_decompress(&srcinfo);
538
Guido Vollbeding5996a252009-06-27 00:00:00 +0000539 /* Close output file, if we opened it */
540 if (fp != stdout)
541 fclose(fp);
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000542
543#ifdef PROGRESS_REPORT
544 end_progress_monitor((j_common_ptr) &dstinfo);
545#endif
546
547 /* All done. */
548 exit(jsrcerr.num_warnings + jdsterr.num_warnings ?EXIT_WARNING:EXIT_SUCCESS);
549 return 0; /* suppress no-return-value warnings */
550}