- Sync with trunk up to r46941.
[reactos.git] / dll / 3rdparty / libjpeg / jpegtran.c
1 /*
2 * jpegtran.c
3 *
4 * Copyright (C) 1995-2001, Thomas G. Lane.
5 * This file is part of the Independent JPEG Group's software.
6 * For conditions of distribution and use, see the accompanying README file.
7 *
8 * This file contains a command-line user interface for JPEG transcoding.
9 * It is very similar to cjpeg.c, but provides lossless transcoding between
10 * different JPEG file formats. It also provides some lossless and sort-of-
11 * lossless transformations of JPEG data.
12 */
13
14 #include "cdjpeg.h" /* Common decls for cjpeg/djpeg applications */
15 #include "transupp.h" /* Support routines for jpegtran */
16 #include "jversion.h" /* for version message */
17
18 #ifdef USE_CCOMMAND /* command-line reader for Macintosh */
19 #ifdef __MWERKS__
20 #include <SIOUX.h> /* Metrowerks needs this */
21 #include <console.h> /* ... and this */
22 #endif
23 #ifdef THINK_C
24 #include <console.h> /* Think declares it here */
25 #endif
26 #endif
27
28
29 /*
30 * Argument-parsing code.
31 * The switch parser is designed to be useful with DOS-style command line
32 * syntax, ie, intermixed switches and file names, where only the switches
33 * to the left of a given file name affect processing of that file.
34 * The main program in this file doesn't actually use this capability...
35 */
36
37
38 static const char * progname; /* program name for error messages */
39 static char * outfilename; /* for -outfile switch */
40 static char * dropfilename; /* for -drop switch */
41 static JCOPY_OPTION copyoption; /* -copy switch */
42 static jpeg_transform_info transformoption; /* image transformation options */
43
44
45 LOCAL(void)
46 usage (void)
47 /* complain about bad command line */
48 {
49 fprintf(stderr, "usage: %s [switches] ", progname);
50 #ifdef TWO_FILE_COMMANDLINE
51 fprintf(stderr, "inputfile outputfile\n");
52 #else
53 fprintf(stderr, "[inputfile]\n");
54 #endif
55
56 fprintf(stderr, "Switches (names may be abbreviated):\n");
57 fprintf(stderr, " -copy none Copy no extra markers from source file\n");
58 fprintf(stderr, " -copy comments Copy only comment markers (default)\n");
59 fprintf(stderr, " -copy all Copy all extra markers\n");
60 fprintf(stderr, " -copy exif Copy EXIF marker and omit JFIF if EXIF\n");
61 #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
67 #if TRANSFORMS_SUPPORTED
68 fprintf(stderr, "Switches for modifying the image:\n");
69 fprintf(stderr, " -crop WxH+X+Y Crop to a rectangular subarea\n");
70 fprintf(stderr, " -drop +X+Y filename Drop another image\n");
71 fprintf(stderr, " -grayscale Reduce to grayscale (omit color data)\n");
72 fprintf(stderr, " -flip [horizontal|vertical] Mirror image (left-right or top-bottom)\n");
73 fprintf(stderr, " -perfect Fail if there is non-transformable edge blocks\n");
74 fprintf(stderr, " -rotate [90|180|270] Rotate image (degrees clockwise)\n");
75 fprintf(stderr, " -transpose Transpose image\n");
76 fprintf(stderr, " -transverse Transverse transpose image\n");
77 fprintf(stderr, " -trim Drop non-transformable edge blocks or\n");
78 fprintf(stderr, " with -drop: Requantize drop file to source file\n");
79 #endif /* TRANSFORMS_SUPPORTED */
80 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
96 LOCAL(void)
97 select_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 LOCAL(void)
119 handle_exif (j_decompress_ptr srcinfo, j_compress_ptr dstinfo,
120 JCOPY_OPTION *copyoption)
121 /* Adjust the marker writing options to create an EXIF file, instead of JFIF,
122 * if so requested or an EXIF file is detected as input. Must be called after
123 * jpeg_copy_critical_parameters() as that sets the defaults. */
124 {
125 jpeg_saved_marker_ptr cur_marker, prev_marker;
126
127 /* Look for an exif marker */
128 prev_marker = NULL;
129 cur_marker = srcinfo->marker_list;
130 while (cur_marker != NULL) {
131 if (cur_marker->marker == JPEG_APP0+1 &&
132 cur_marker->data_length >= 6 &&
133 GETJOCTET(cur_marker->data[0]) == 0x45 &&
134 GETJOCTET(cur_marker->data[1]) == 0x78 &&
135 GETJOCTET(cur_marker->data[2]) == 0x69 &&
136 GETJOCTET(cur_marker->data[3]) == 0x66 &&
137 GETJOCTET(cur_marker->data[4]) == 0 &&
138 GETJOCTET(cur_marker->data[5]) == 0)
139 break; /* found an EXIF marker */
140 prev_marker = cur_marker;
141 cur_marker = cur_marker->next;
142 }
143 /* If we've found an EXIF marker but not JFIF this is an EXIF file. Unless
144 * explicitely requested, make sure we keep the EXIF marker and do not
145 * emit a JFIF marker (which would come before). EXIF requires that the
146 * first marker be EXIF. */
147 if (cur_marker != NULL &&
148 ((*copyoption != JCOPYOPT_NONE && !srcinfo->saw_JFIF_marker) ||
149 (*copyoption == JCOPYOPT_EXIF))) {
150 dstinfo->write_JFIF_header = FALSE;
151 if (*copyoption == JCOPYOPT_COMMENTS)
152 *copyoption = JCOPYOPT_EXIF;
153 }
154 /* If making an EXIF file, make sure that EXIF is first marker */
155 if (cur_marker != NULL && prev_marker != NULL &&
156 *copyoption == JCOPYOPT_EXIF) {
157 prev_marker->next = cur_marker->next;
158 cur_marker->next = srcinfo->marker_list;
159 srcinfo->marker_list = cur_marker;
160 }
161 }
162
163 LOCAL(int)
164 parse_switches (j_compress_ptr cinfo, int argc, char **argv,
165 int last_file_arg_seen, boolean for_real)
166 /* Parse optional switches.
167 * Returns argv[] index of first file-name argument (== argc if none).
168 * Any file names with indexes <= last_file_arg_seen are ignored;
169 * they have presumably been processed in a previous iteration.
170 * (Pass 0 for last_file_arg_seen on the first or only iteration.)
171 * for_real is FALSE on the first (dummy) pass; we may skip any expensive
172 * processing.
173 */
174 {
175 int argn;
176 char * arg;
177 boolean simple_progressive;
178 char * scansarg = NULL; /* saves -scans parm if any */
179
180 /* Set up default JPEG parameters. */
181 simple_progressive = FALSE;
182 outfilename = NULL;
183 dropfilename = NULL;
184 copyoption = JCOPYOPT_DEFAULT;
185 transformoption.transform = JXFORM_NONE;
186 transformoption.trim = FALSE;
187 transformoption.perfect = FALSE;
188 transformoption.force_grayscale = FALSE;
189 transformoption.crop = FALSE;
190 cinfo->err->trace_level = 0;
191
192 /* Scan command line options, adjust parameters */
193
194 for (argn = 1; argn < argc; argn++) {
195 arg = argv[argn];
196 if (*arg != '-') {
197 /* Not a switch, must be a file name argument */
198 if (argn <= last_file_arg_seen) {
199 outfilename = NULL; /* -outfile applies to just one input file */
200 continue; /* ignore this name if previously processed */
201 }
202 break; /* else done parsing switches */
203 }
204 arg++; /* advance past switch marker character */
205
206 if (keymatch(arg, "arithmetic", 1)) {
207 /* Use arithmetic coding. */
208 #ifdef C_ARITH_CODING_SUPPORTED
209 cinfo->arith_code = TRUE;
210 #else
211 fprintf(stderr, "%s: sorry, arithmetic coding not supported\n",
212 progname);
213 exit(EXIT_FAILURE);
214 #endif
215
216 } else if (keymatch(arg, "copy", 2)) {
217 /* Select which extra markers to copy. */
218 if (++argn >= argc) /* advance to next argument */
219 usage();
220 if (keymatch(argv[argn], "none", 1)) {
221 copyoption = JCOPYOPT_NONE;
222 } else if (keymatch(argv[argn], "comments", 1)) {
223 copyoption = JCOPYOPT_COMMENTS;
224 } else if (keymatch(argv[argn], "all", 1)) {
225 copyoption = JCOPYOPT_ALL;
226 } else if (keymatch(argv[argn], "exif", 1)) {
227 copyoption = JCOPYOPT_EXIF;
228 } else
229 usage();
230
231 } else if (keymatch(arg, "crop", 2)) {
232 /* Perform lossless cropping. */
233 #if TRANSFORMS_SUPPORTED
234 if (++argn >= argc) /* advance to next argument */
235 usage();
236 if (transformoption.crop /* reject multiple crop/drop requests */ ||
237 ! jtransform_parse_crop_spec(&transformoption, argv[argn])) {
238 fprintf(stderr, "%s: bogus -crop argument '%s'\n",
239 progname, argv[argn]);
240 exit(EXIT_FAILURE);
241 }
242 #else
243 select_transform(JXFORM_NONE); /* force an error */
244 #endif
245
246 } else if (keymatch(arg, "drop", 2)) {
247 #if TRANSFORMS_SUPPORTED
248 if (++argn >= argc) /* advance to next argument */
249 usage();
250 if (transformoption.crop /* reject multiple crop/drop requests */ ||
251 ! jtransform_parse_crop_spec(&transformoption, argv[argn]) ||
252 transformoption.crop_width_set != JCROP_UNSET ||
253 transformoption.crop_height_set != JCROP_UNSET) {
254 fprintf(stderr, "%s: bogus -drop argument '%s'\n",
255 progname, argv[argn]);
256 exit(EXIT_FAILURE);
257 }
258 if (++argn >= argc) /* advance to next argument */
259 usage();
260 dropfilename = argv[argn];
261 select_transform(JXFORM_DROP);
262 #else
263 select_transform(JXFORM_NONE); /* force an error */
264 #endif
265
266
267 } else if (keymatch(arg, "debug", 1) || keymatch(arg, "verbose", 1)) {
268 /* Enable debug printouts. */
269 /* On first -d, print version identification */
270 static boolean printed_version = FALSE;
271
272 if (! printed_version) {
273 fprintf(stderr, "Independent JPEG Group's JPEGTRAN, version %s\n%s\n",
274 JVERSION, JCOPYRIGHT);
275 fprintf(stderr, "EXIF support v 0.1 added 29-Sep-2003\n");
276 printed_version = TRUE;
277 }
278 cinfo->err->trace_level++;
279
280 } else if (keymatch(arg, "flip", 1)) {
281 /* Mirror left-right or top-bottom. */
282 if (++argn >= argc) /* advance to next argument */
283 usage();
284 if (keymatch(argv[argn], "horizontal", 1))
285 select_transform(JXFORM_FLIP_H);
286 else if (keymatch(argv[argn], "vertical", 1))
287 select_transform(JXFORM_FLIP_V);
288 else
289 usage();
290
291 } else if (keymatch(arg, "grayscale", 1) || keymatch(arg, "greyscale",1)) {
292 /* Force to grayscale. */
293 #if TRANSFORMS_SUPPORTED
294 transformoption.force_grayscale = TRUE;
295 #else
296 select_transform(JXFORM_NONE); /* force an error */
297 #endif
298
299 } else if (keymatch(arg, "maxmemory", 3)) {
300 /* Maximum memory in Kb (or Mb with 'm'). */
301 long lval;
302 char ch = 'x';
303
304 if (++argn >= argc) /* advance to next argument */
305 usage();
306 if (sscanf(argv[argn], "%ld%c", &lval, &ch) < 1)
307 usage();
308 if (ch == 'm' || ch == 'M')
309 lval *= 1000L;
310 cinfo->mem->max_memory_to_use = lval * 1000L;
311
312 } else if (keymatch(arg, "optimize", 1) || keymatch(arg, "optimise", 1)) {
313 /* Enable entropy parm optimization. */
314 #ifdef ENTROPY_OPT_SUPPORTED
315 cinfo->optimize_coding = TRUE;
316 #else
317 fprintf(stderr, "%s: sorry, entropy optimization was not compiled\n",
318 progname);
319 exit(EXIT_FAILURE);
320 #endif
321
322 } else if (keymatch(arg, "outfile", 4)) {
323 /* Set output file name. */
324 if (++argn >= argc) /* advance to next argument */
325 usage();
326 outfilename = argv[argn]; /* save it away for later use */
327
328 } else if (keymatch(arg, "perfect", 2)) {
329 /* Fail if there is any partial edge MCUs that the transform can't
330 * handle. */
331 transformoption.perfect = TRUE;
332
333 } else if (keymatch(arg, "progressive", 2)) {
334 /* Select simple progressive mode. */
335 #ifdef C_PROGRESSIVE_SUPPORTED
336 simple_progressive = TRUE;
337 /* We must postpone execution until num_components is known. */
338 #else
339 fprintf(stderr, "%s: sorry, progressive output was not compiled\n",
340 progname);
341 exit(EXIT_FAILURE);
342 #endif
343
344 } else if (keymatch(arg, "restart", 1)) {
345 /* Restart interval in MCU rows (or in MCUs with 'b'). */
346 long lval;
347 char ch = 'x';
348
349 if (++argn >= argc) /* advance to next argument */
350 usage();
351 if (sscanf(argv[argn], "%ld%c", &lval, &ch) < 1)
352 usage();
353 if (lval < 0 || lval > 65535L)
354 usage();
355 if (ch == 'b' || ch == 'B') {
356 cinfo->restart_interval = (unsigned int) lval;
357 cinfo->restart_in_rows = 0; /* else prior '-restart n' overrides me */
358 } else {
359 cinfo->restart_in_rows = (int) lval;
360 /* restart_interval will be computed during startup */
361 }
362
363 } else if (keymatch(arg, "rotate", 2)) {
364 /* Rotate 90, 180, or 270 degrees (measured clockwise). */
365 if (++argn >= argc) /* advance to next argument */
366 usage();
367 if (keymatch(argv[argn], "90", 2))
368 select_transform(JXFORM_ROT_90);
369 else if (keymatch(argv[argn], "180", 3))
370 select_transform(JXFORM_ROT_180);
371 else if (keymatch(argv[argn], "270", 3))
372 select_transform(JXFORM_ROT_270);
373 else
374 usage();
375
376 } else if (keymatch(arg, "scans", 1)) {
377 /* Set scan script. */
378 #ifdef C_MULTISCAN_FILES_SUPPORTED
379 if (++argn >= argc) /* advance to next argument */
380 usage();
381 scansarg = argv[argn];
382 /* We must postpone reading the file in case -progressive appears. */
383 #else
384 fprintf(stderr, "%s: sorry, multi-scan output was not compiled\n",
385 progname);
386 exit(EXIT_FAILURE);
387 #endif
388
389 } else if (keymatch(arg, "transpose", 1)) {
390 /* Transpose (across UL-to-LR axis). */
391 select_transform(JXFORM_TRANSPOSE);
392
393 } else if (keymatch(arg, "transverse", 6)) {
394 /* Transverse transpose (across UR-to-LL axis). */
395 select_transform(JXFORM_TRANSVERSE);
396
397 } else if (keymatch(arg, "trim", 3)) {
398 /* Trim off any partial edge MCUs that the transform can't handle. */
399 transformoption.trim = TRUE;
400
401 } else {
402 usage(); /* bogus switch */
403 }
404 }
405
406 /* Post-switch-scanning cleanup */
407
408 if (for_real) {
409
410 #ifdef C_PROGRESSIVE_SUPPORTED
411 if (simple_progressive) /* process -progressive; -scans can override */
412 jpeg_simple_progression(cinfo);
413 #endif
414
415 #ifdef C_MULTISCAN_FILES_SUPPORTED
416 if (scansarg != NULL) /* process -scans if it was present */
417 if (! read_scan_script(cinfo, scansarg))
418 usage();
419 #endif
420 }
421
422 return argn; /* return index of next arg (file name) */
423 }
424
425
426 /*
427 * The main program.
428 */
429
430 int
431 main (int argc, char **argv)
432 {
433 struct jpeg_decompress_struct srcinfo;
434 struct jpeg_error_mgr jsrcerr;
435 #if TRANSFORMS_SUPPORTED
436 struct jpeg_decompress_struct dropinfo;
437 struct jpeg_error_mgr jdroperr;
438 FILE * drop_file;
439 #endif
440 struct jpeg_compress_struct dstinfo;
441 struct jpeg_error_mgr jdsterr;
442 #ifdef PROGRESS_REPORT
443 struct cdjpeg_progress_mgr progress;
444 #endif
445 jvirt_barray_ptr * src_coef_arrays;
446 jvirt_barray_ptr * dst_coef_arrays;
447 int file_index;
448 /* We assume all-in-memory processing and can therefore use only a
449 * single file pointer for sequential input and output operation.
450 */
451 FILE * fp;
452
453 /* On Mac, fetch a command line. */
454 #ifdef USE_CCOMMAND
455 argc = ccommand(&argv);
456 #endif
457
458 progname = argv[0];
459 if (progname == NULL || progname[0] == 0)
460 progname = "jpegtran"; /* in case C library doesn't provide it */
461
462 /* Initialize the JPEG decompression object with default error handling. */
463 srcinfo.err = jpeg_std_error(&jsrcerr);
464 jpeg_create_decompress(&srcinfo);
465 /* Initialize the JPEG compression object with default error handling. */
466 dstinfo.err = jpeg_std_error(&jdsterr);
467 jpeg_create_compress(&dstinfo);
468
469 /* Now safe to enable signal catcher.
470 * Note: we assume only the decompression object will have virtual arrays.
471 */
472 #ifdef NEED_SIGNAL_CATCHER
473 enable_signal_catcher((j_common_ptr) &srcinfo);
474 #endif
475
476 /* Scan command line to find file names.
477 * It is convenient to use just one switch-parsing routine, but the switch
478 * values read here are mostly ignored; we will rescan the switches after
479 * opening the input file. Also note that most of the switches affect the
480 * destination JPEG object, so we parse into that and then copy over what
481 * needs to affects the source too.
482 */
483
484 file_index = parse_switches(&dstinfo, argc, argv, 0, FALSE);
485 jsrcerr.trace_level = jdsterr.trace_level;
486 srcinfo.mem->max_memory_to_use = dstinfo.mem->max_memory_to_use;
487
488 #ifdef TWO_FILE_COMMANDLINE
489 /* Must have either -outfile switch or explicit output file name */
490 if (outfilename == NULL) {
491 if (file_index != argc-2) {
492 fprintf(stderr, "%s: must name one input and one output file\n",
493 progname);
494 usage();
495 }
496 outfilename = argv[file_index+1];
497 } else {
498 if (file_index != argc-1) {
499 fprintf(stderr, "%s: must name one input and one output file\n",
500 progname);
501 usage();
502 }
503 }
504 #else
505 /* Unix style: expect zero or one file name */
506 if (file_index < argc-1) {
507 fprintf(stderr, "%s: only one input file\n", progname);
508 usage();
509 }
510 #endif /* TWO_FILE_COMMANDLINE */
511
512 /* Open the input file. */
513 if (file_index < argc) {
514 if ((fp = fopen(argv[file_index], READ_BINARY)) == NULL) {
515 fprintf(stderr, "%s: can't open %s for reading\n", progname, argv[file_index]);
516 exit(EXIT_FAILURE);
517 }
518 } else {
519 /* default input file is stdin */
520 fp = read_stdin();
521 }
522 #if TRANSFORMS_SUPPORTED
523 /* Open the drop file. */
524 if (dropfilename != NULL) {
525 if ((drop_file = fopen(dropfilename, READ_BINARY)) == NULL) {
526 fprintf(stderr, "%s: can't open %s for reading\n", progname, dropfilename);
527 exit(EXIT_FAILURE);
528 }
529 dropinfo.err = jpeg_std_error(&jdroperr);
530 jpeg_create_decompress(&dropinfo);
531 jpeg_stdio_src(&dropinfo, drop_file);
532 } else {
533 drop_file = NULL;
534 }
535 #endif
536
537
538 #ifdef PROGRESS_REPORT
539 start_progress_monitor((j_common_ptr) &dstinfo, &progress);
540 #endif
541
542 /* Specify data source for decompression */
543 jpeg_stdio_src(&srcinfo, fp);
544
545 /* Enable saving of extra markers that we want to copy */
546 jcopy_markers_setup(&srcinfo, copyoption);
547
548 /* Read file header */
549 (void) jpeg_read_header(&srcinfo, TRUE);
550
551 #if TRANSFORMS_SUPPORTED
552 if (dropfilename != NULL) {
553 (void) jpeg_read_header(&dropinfo, TRUE);
554 transformoption.crop_width = dropinfo.image_width;
555 transformoption.crop_width_set = JCROP_POS;
556 transformoption.crop_height = dropinfo.image_height;
557 transformoption.crop_height_set = JCROP_POS;
558 transformoption.drop_ptr = &dropinfo;
559 }
560 #endif
561
562 /* Any space needed by a transform option must be requested before
563 * jpeg_read_coefficients so that memory allocation will be done right.
564 */
565 #if TRANSFORMS_SUPPORTED
566 /* Fails right away if -perfect is given and transformation is not perfect.
567 */
568 if (transformoption.perfect &&
569 !jtransform_perfect_transform(srcinfo.image_width, srcinfo.image_height,
570 srcinfo.max_h_samp_factor * DCTSIZE, srcinfo.max_v_samp_factor * DCTSIZE,
571 transformoption.transform)) {
572 fprintf(stderr, "%s: transformation is not perfect\n", progname);
573 exit(EXIT_FAILURE);
574 }
575 jtransform_request_workspace(&srcinfo, &transformoption);
576 #endif
577
578 /* Read source file as DCT coefficients */
579 src_coef_arrays = jpeg_read_coefficients(&srcinfo);
580
581 #if TRANSFORMS_SUPPORTED
582 if (dropfilename != NULL) {
583 transformoption.drop_coef_arrays = jpeg_read_coefficients(&dropinfo);
584 }
585 #endif
586
587 /* Initialize destination compression parameters from source values */
588 jpeg_copy_critical_parameters(&srcinfo, &dstinfo);
589
590
591 /* Adjust destination parameters if required by transform options;
592 * also find out which set of coefficient arrays will hold the output.
593 */
594 #if TRANSFORMS_SUPPORTED
595 dst_coef_arrays = jtransform_adjust_parameters(&srcinfo, &dstinfo,
596 src_coef_arrays,
597 &transformoption);
598 #else
599 dst_coef_arrays = src_coef_arrays;
600 #endif
601
602 /* Close input file, if we opened it.
603 * Note: we assume that jpeg_read_coefficients consumed all input
604 * until JPEG_REACHED_EOI, and that jpeg_finish_decompress will
605 * only consume more while (! cinfo->inputctl->eoi_reached).
606 * We cannot call jpeg_finish_decompress here since we still need the
607 * virtual arrays allocated from the source object for processing.
608 */
609 if (fp != stdin)
610 fclose(fp);
611
612 /* Open the output file. */
613 if (outfilename != NULL) {
614 if ((fp = fopen(outfilename, WRITE_BINARY)) == NULL) {
615 fprintf(stderr, "%s: can't open %s for writing\n", progname, outfilename);
616 exit(EXIT_FAILURE);
617 }
618 } else {
619 /* default output file is stdout */
620 fp = write_stdout();
621 }
622
623 /* Adjust default compression parameters by re-parsing the options */
624 /* Save value of copyoption */
625 file_index = parse_switches(&dstinfo, argc, argv, 0, TRUE);
626
627 /* If we want EXIF, make sure we do not write incompatible markers */
628 handle_exif(&srcinfo,&dstinfo,&copyoption);
629
630 /* Specify data destination for compression */
631 jpeg_stdio_dest(&dstinfo, fp);
632
633 /* Start compressor (note no image data is actually written here) */
634 jpeg_write_coefficients(&dstinfo, dst_coef_arrays);
635
636 /* Copy to the output file any extra markers that we want to preserve */
637 jcopy_markers_execute(&srcinfo, &dstinfo, copyoption);
638
639 /* Execute image transformation, if any */
640 #if TRANSFORMS_SUPPORTED
641 jtransform_execute_transform(&srcinfo, &dstinfo,
642 src_coef_arrays,
643 &transformoption);
644 #endif
645
646 /* Finish compression and release memory */
647 jpeg_finish_compress(&dstinfo);
648 jpeg_destroy_compress(&dstinfo);
649 #if TRANSFORMS_SUPPORTED
650 if (dropfilename != NULL) {
651 (void) jpeg_finish_decompress(&dropinfo);
652 jpeg_destroy_decompress(&dropinfo);
653 }
654 #endif
655 (void) jpeg_finish_decompress(&srcinfo);
656 jpeg_destroy_decompress(&srcinfo);
657
658 /* Close output file, if we opened it */
659 if (fp != stdout)
660 fclose(fp);
661 #if TRANSFORMS_SUPPORTED
662 if (drop_file != NULL)
663 fclose(drop_file);
664 #endif
665
666 #ifdef PROGRESS_REPORT
667 end_progress_monitor((j_common_ptr) &dstinfo);
668 #endif
669
670 /* All done. */
671 #if TRANSFORMS_SUPPORTED
672 if (dropfilename != NULL)
673 exit(jsrcerr.num_warnings + jdroperr.num_warnings + jdsterr.num_warnings ?
674 EXIT_WARNING : EXIT_SUCCESS);
675 #endif
676 exit(jsrcerr.num_warnings + jdsterr.num_warnings ?EXIT_WARNING:EXIT_SUCCESS);
677 return 0; /* suppress no-return-value warnings */
678 }