4 * Copyright (C) 1995-2013, Thomas G. Lane, Guido Vollbeding.
5 * This file is part of the Independent JPEG Group's software.
6 * For conditions of distribution and use, see the accompanying README file.
8 * This file contains a command-line user interface for JPEG transcoding.
9 * It is very similar to cjpeg.c, and partly to djpeg.c, but provides
10 * lossless transcoding between different JPEG file formats. It also
11 * provides some lossless and sort-of-lossless transformations of JPEG data.
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 */
18 #ifdef USE_CCOMMAND /* command-line reader for Macintosh */
20 #include <SIOUX.h> /* Metrowerks needs this */
21 #include <console.h> /* ... and this */
24 #include <console.h> /* Think declares it here */
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...
38 static const char * progname
; /* program name for error messages */
39 static char * outfilename
; /* for -outfile switch */
40 static char * scaleoption
; /* -scale switch */
41 static JCOPY_OPTION copyoption
; /* -copy switch */
42 static jpeg_transform_info transformoption
; /* image transformation options */
47 /* complain about bad command line */
49 fprintf(stderr
, "usage: %s [switches] ", progname
);
50 #ifdef TWO_FILE_COMMANDLINE
51 fprintf(stderr
, "inputfile outputfile\n");
53 fprintf(stderr
, "[inputfile]\n");
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 #ifdef ENTROPY_OPT_SUPPORTED
61 fprintf(stderr
, " -optimize Optimize Huffman table (smaller file, but slow compression)\n");
63 #ifdef C_PROGRESSIVE_SUPPORTED
64 fprintf(stderr
, " -progressive Create progressive JPEG file\n");
66 fprintf(stderr
, "Switches for modifying the image:\n");
67 #if TRANSFORMS_SUPPORTED
68 fprintf(stderr
, " -crop WxH+X+Y Crop to a rectangular subarea\n");
69 fprintf(stderr
, " -flip [horizontal|vertical] Mirror image (left-right or top-bottom)\n");
70 fprintf(stderr
, " -grayscale Reduce to grayscale (omit color data)\n");
71 fprintf(stderr
, " -perfect Fail if there is non-transformable edge blocks\n");
72 fprintf(stderr
, " -rotate [90|180|270] Rotate image (degrees clockwise)\n");
74 fprintf(stderr
, " -scale M/N Scale output image by fraction M/N, eg, 1/8\n");
75 #if TRANSFORMS_SUPPORTED
76 fprintf(stderr
, " -transpose Transpose image\n");
77 fprintf(stderr
, " -transverse Transverse transpose image\n");
78 fprintf(stderr
, " -trim Drop non-transformable edge blocks\n");
79 fprintf(stderr
, " -wipe WxH+X+Y Wipe (gray out) a rectangular subarea\n");
81 fprintf(stderr
, "Switches for advanced users:\n");
82 #ifdef C_ARITH_CODING_SUPPORTED
83 fprintf(stderr
, " -arithmetic Use arithmetic coding\n");
85 fprintf(stderr
, " -restart N Set restart interval in rows, or in blocks with B\n");
86 fprintf(stderr
, " -maxmemory N Maximum memory to use (in kbytes)\n");
87 fprintf(stderr
, " -outfile name Specify name for output file\n");
88 fprintf(stderr
, " -verbose or -debug Emit debug output\n");
89 fprintf(stderr
, "Switches for wizards:\n");
90 #ifdef C_MULTISCAN_FILES_SUPPORTED
91 fprintf(stderr
, " -scans file Create multi-scan JPEG per script file\n");
98 select_transform (JXFORM_CODE transform
)
99 /* Silly little routine to detect multiple transform options,
100 * which we can't handle.
103 #if TRANSFORMS_SUPPORTED
104 if (transformoption
.transform
== JXFORM_NONE
||
105 transformoption
.transform
== transform
) {
106 transformoption
.transform
= transform
;
108 fprintf(stderr
, "%s: can only do one image transformation at a time\n",
113 fprintf(stderr
, "%s: sorry, image transformation was not compiled\n",
121 parse_switches (j_compress_ptr cinfo
, int argc
, char **argv
,
122 int last_file_arg_seen
, boolean for_real
)
123 /* Parse optional switches.
124 * Returns argv[] index of first file-name argument (== argc if none).
125 * Any file names with indexes <= last_file_arg_seen are ignored;
126 * they have presumably been processed in a previous iteration.
127 * (Pass 0 for last_file_arg_seen on the first or only iteration.)
128 * for_real is FALSE on the first (dummy) pass; we may skip any expensive
134 boolean simple_progressive
;
135 char * scansarg
= NULL
; /* saves -scans parm if any */
137 /* Set up default JPEG parameters. */
138 simple_progressive
= FALSE
;
141 copyoption
= JCOPYOPT_DEFAULT
;
142 transformoption
.transform
= JXFORM_NONE
;
143 transformoption
.perfect
= FALSE
;
144 transformoption
.trim
= FALSE
;
145 transformoption
.force_grayscale
= FALSE
;
146 transformoption
.crop
= FALSE
;
147 cinfo
->err
->trace_level
= 0;
149 /* Scan command line options, adjust parameters */
151 for (argn
= 1; argn
< argc
; argn
++) {
154 /* Not a switch, must be a file name argument */
155 if (argn
<= last_file_arg_seen
) {
156 outfilename
= NULL
; /* -outfile applies to just one input file */
157 continue; /* ignore this name if previously processed */
159 break; /* else done parsing switches */
161 arg
++; /* advance past switch marker character */
163 if (keymatch(arg
, "arithmetic", 1)) {
164 /* Use arithmetic coding. */
165 #ifdef C_ARITH_CODING_SUPPORTED
166 cinfo
->arith_code
= TRUE
;
168 fprintf(stderr
, "%s: sorry, arithmetic coding not supported\n",
173 } else if (keymatch(arg
, "copy", 2)) {
174 /* Select which extra markers to copy. */
175 if (++argn
>= argc
) /* advance to next argument */
177 if (keymatch(argv
[argn
], "none", 1)) {
178 copyoption
= JCOPYOPT_NONE
;
179 } else if (keymatch(argv
[argn
], "comments", 1)) {
180 copyoption
= JCOPYOPT_COMMENTS
;
181 } else if (keymatch(argv
[argn
], "all", 1)) {
182 copyoption
= JCOPYOPT_ALL
;
186 } else if (keymatch(arg
, "crop", 2)) {
187 /* Perform lossless cropping. */
188 #if TRANSFORMS_SUPPORTED
189 if (++argn
>= argc
) /* advance to next argument */
191 if (transformoption
.crop
/* reject multiple crop/wipe requests */ ||
192 ! jtransform_parse_crop_spec(&transformoption
, argv
[argn
])) {
193 fprintf(stderr
, "%s: bogus -crop argument '%s'\n",
194 progname
, argv
[argn
]);
198 select_transform(JXFORM_NONE
); /* force an error */
201 } else if (keymatch(arg
, "debug", 1) || keymatch(arg
, "verbose", 1)) {
202 /* Enable debug printouts. */
203 /* On first -d, print version identification */
204 static boolean printed_version
= FALSE
;
206 if (! printed_version
) {
207 fprintf(stderr
, "Independent JPEG Group's JPEGTRAN, version %s\n%s\n",
208 JVERSION
, JCOPYRIGHT
);
209 printed_version
= TRUE
;
211 cinfo
->err
->trace_level
++;
213 } else if (keymatch(arg
, "flip", 1)) {
214 /* Mirror left-right or top-bottom. */
215 if (++argn
>= argc
) /* advance to next argument */
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
);
224 } else if (keymatch(arg
, "grayscale", 1) || keymatch(arg
, "greyscale",1)) {
225 /* Force to grayscale. */
226 #if TRANSFORMS_SUPPORTED
227 transformoption
.force_grayscale
= TRUE
;
229 select_transform(JXFORM_NONE
); /* force an error */
232 } else if (keymatch(arg
, "maxmemory", 3)) {
233 /* Maximum memory in Kb (or Mb with 'm'). */
237 if (++argn
>= argc
) /* advance to next argument */
239 if (sscanf(argv
[argn
], "%ld%c", &lval
, &ch
) < 1)
241 if (ch
== 'm' || ch
== 'M')
243 cinfo
->mem
->max_memory_to_use
= lval
* 1000L;
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
;
250 fprintf(stderr
, "%s: sorry, entropy optimization was not compiled\n",
255 } else if (keymatch(arg
, "outfile", 4)) {
256 /* Set output file name. */
257 if (++argn
>= argc
) /* advance to next argument */
259 outfilename
= argv
[argn
]; /* save it away for later use */
261 } else if (keymatch(arg
, "perfect", 2)) {
262 /* Fail if there is any partial edge MCUs that the transform can't
264 transformoption
.perfect
= TRUE
;
266 } else if (keymatch(arg
, "progressive", 2)) {
267 /* Select simple progressive mode. */
268 #ifdef C_PROGRESSIVE_SUPPORTED
269 simple_progressive
= TRUE
;
270 /* We must postpone execution until num_components is known. */
272 fprintf(stderr
, "%s: sorry, progressive output was not compiled\n",
277 } else if (keymatch(arg
, "restart", 1)) {
278 /* Restart interval in MCU rows (or in MCUs with 'b'). */
282 if (++argn
>= argc
) /* advance to next argument */
284 if (sscanf(argv
[argn
], "%ld%c", &lval
, &ch
) < 1)
286 if (lval
< 0 || lval
> 65535L)
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 */
292 cinfo
->restart_in_rows
= (int) lval
;
293 /* restart_interval will be computed during startup */
296 } else if (keymatch(arg
, "rotate", 2)) {
297 /* Rotate 90, 180, or 270 degrees (measured clockwise). */
298 if (++argn
>= argc
) /* advance to next argument */
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
);
309 } else if (keymatch(arg
, "scale", 4)) {
310 /* Scale the output image by a fraction M/N. */
311 if (++argn
>= argc
) /* advance to next argument */
313 scaleoption
= argv
[argn
];
314 /* We must postpone processing until decompression startup. */
316 } else if (keymatch(arg
, "scans", 1)) {
317 /* Set scan script. */
318 #ifdef C_MULTISCAN_FILES_SUPPORTED
319 if (++argn
>= argc
) /* advance to next argument */
321 scansarg
= argv
[argn
];
322 /* We must postpone reading the file in case -progressive appears. */
324 fprintf(stderr
, "%s: sorry, multi-scan output was not compiled\n",
329 } else if (keymatch(arg
, "transpose", 1)) {
330 /* Transpose (across UL-to-LR axis). */
331 select_transform(JXFORM_TRANSPOSE
);
333 } else if (keymatch(arg
, "transverse", 6)) {
334 /* Transverse transpose (across UR-to-LL axis). */
335 select_transform(JXFORM_TRANSVERSE
);
337 } else if (keymatch(arg
, "trim", 3)) {
338 /* Trim off any partial edge MCUs that the transform can't handle. */
339 transformoption
.trim
= TRUE
;
341 } else if (keymatch(arg
, "wipe", 1)) {
342 #if TRANSFORMS_SUPPORTED
343 if (++argn
>= argc
) /* advance to next argument */
345 if (transformoption
.crop
/* reject multiple crop/wipe requests */ ||
346 ! jtransform_parse_crop_spec(&transformoption
, argv
[argn
])) {
347 fprintf(stderr
, "%s: bogus -wipe argument '%s'\n",
348 progname
, argv
[argn
]);
351 select_transform(JXFORM_WIPE
);
353 select_transform(JXFORM_NONE
); /* force an error */
357 usage(); /* bogus switch */
361 /* Post-switch-scanning cleanup */
365 #ifdef C_PROGRESSIVE_SUPPORTED
366 if (simple_progressive
) /* process -progressive; -scans can override */
367 jpeg_simple_progression(cinfo
);
370 #ifdef C_MULTISCAN_FILES_SUPPORTED
371 if (scansarg
!= NULL
) /* process -scans if it was present */
372 if (! read_scan_script(cinfo
, scansarg
))
377 return argn
; /* return index of next arg (file name) */
386 main (int argc
, char **argv
)
388 struct jpeg_decompress_struct srcinfo
;
389 struct jpeg_compress_struct dstinfo
;
390 struct jpeg_error_mgr jsrcerr
, jdsterr
;
391 #ifdef PROGRESS_REPORT
392 struct cdjpeg_progress_mgr progress
;
394 jvirt_barray_ptr
* src_coef_arrays
;
395 jvirt_barray_ptr
* dst_coef_arrays
;
397 /* We assume all-in-memory processing and can therefore use only a
398 * single file pointer for sequential input and output operation.
402 /* On Mac, fetch a command line. */
404 argc
= ccommand(&argv
);
408 if (progname
== NULL
|| progname
[0] == 0)
409 progname
= "jpegtran"; /* in case C library doesn't provide it */
411 /* Initialize the JPEG decompression object with default error handling. */
412 srcinfo
.err
= jpeg_std_error(&jsrcerr
);
413 jpeg_create_decompress(&srcinfo
);
414 /* Initialize the JPEG compression object with default error handling. */
415 dstinfo
.err
= jpeg_std_error(&jdsterr
);
416 jpeg_create_compress(&dstinfo
);
418 /* Now safe to enable signal catcher.
419 * Note: we assume only the decompression object will have virtual arrays.
421 #ifdef NEED_SIGNAL_CATCHER
422 enable_signal_catcher((j_common_ptr
) &srcinfo
);
425 /* Scan command line to find file names.
426 * It is convenient to use just one switch-parsing routine, but the switch
427 * values read here are mostly ignored; we will rescan the switches after
428 * opening the input file. Also note that most of the switches affect the
429 * destination JPEG object, so we parse into that and then copy over what
430 * needs to affects the source too.
433 file_index
= parse_switches(&dstinfo
, argc
, argv
, 0, FALSE
);
434 jsrcerr
.trace_level
= jdsterr
.trace_level
;
435 srcinfo
.mem
->max_memory_to_use
= dstinfo
.mem
->max_memory_to_use
;
437 #ifdef TWO_FILE_COMMANDLINE
438 /* Must have either -outfile switch or explicit output file name */
439 if (outfilename
== NULL
) {
440 if (file_index
!= argc
-2) {
441 fprintf(stderr
, "%s: must name one input and one output file\n",
445 outfilename
= argv
[file_index
+1];
447 if (file_index
!= argc
-1) {
448 fprintf(stderr
, "%s: must name one input and one output file\n",
454 /* Unix style: expect zero or one file name */
455 if (file_index
< argc
-1) {
456 fprintf(stderr
, "%s: only one input file\n", progname
);
459 #endif /* TWO_FILE_COMMANDLINE */
461 /* Open the input file. */
462 if (file_index
< argc
) {
463 if ((fp
= fopen(argv
[file_index
], READ_BINARY
)) == NULL
) {
464 fprintf(stderr
, "%s: can't open %s for reading\n", progname
, argv
[file_index
]);
468 /* default input file is stdin */
472 #ifdef PROGRESS_REPORT
473 start_progress_monitor((j_common_ptr
) &dstinfo
, &progress
);
476 /* Specify data source for decompression */
477 jpeg_stdio_src(&srcinfo
, fp
);
479 /* Enable saving of extra markers that we want to copy */
480 jcopy_markers_setup(&srcinfo
, copyoption
);
482 /* Read file header */
483 (void) jpeg_read_header(&srcinfo
, TRUE
);
485 /* Adjust default decompression parameters */
486 if (scaleoption
!= NULL
)
487 if (sscanf(scaleoption
, "%u/%u",
488 &srcinfo
.scale_num
, &srcinfo
.scale_denom
) < 1)
491 /* Any space needed by a transform option must be requested before
492 * jpeg_read_coefficients so that memory allocation will be done right.
494 #if TRANSFORMS_SUPPORTED
495 /* Fail right away if -perfect is given and transformation is not perfect.
497 if (!jtransform_request_workspace(&srcinfo
, &transformoption
)) {
498 fprintf(stderr
, "%s: transformation is not perfect\n", progname
);
503 /* Read source file as DCT coefficients */
504 src_coef_arrays
= jpeg_read_coefficients(&srcinfo
);
506 /* Initialize destination compression parameters from source values */
507 jpeg_copy_critical_parameters(&srcinfo
, &dstinfo
);
509 /* Adjust destination parameters if required by transform options;
510 * also find out which set of coefficient arrays will hold the output.
512 #if TRANSFORMS_SUPPORTED
513 dst_coef_arrays
= jtransform_adjust_parameters(&srcinfo
, &dstinfo
,
517 dst_coef_arrays
= src_coef_arrays
;
520 /* Close input file, if we opened it.
521 * Note: we assume that jpeg_read_coefficients consumed all input
522 * until JPEG_REACHED_EOI, and that jpeg_finish_decompress will
523 * only consume more while (! cinfo->inputctl->eoi_reached).
524 * We cannot call jpeg_finish_decompress here since we still need the
525 * virtual arrays allocated from the source object for processing.
530 /* Open the output file. */
531 if (outfilename
!= NULL
) {
532 if ((fp
= fopen(outfilename
, WRITE_BINARY
)) == NULL
) {
533 fprintf(stderr
, "%s: can't open %s for writing\n", progname
, outfilename
);
537 /* default output file is stdout */
541 /* Adjust default compression parameters by re-parsing the options */
542 file_index
= parse_switches(&dstinfo
, argc
, argv
, 0, TRUE
);
544 /* Specify data destination for compression */
545 jpeg_stdio_dest(&dstinfo
, fp
);
547 /* Start compressor (note no image data is actually written here) */
548 jpeg_write_coefficients(&dstinfo
, dst_coef_arrays
);
550 /* Copy to the output file any extra markers that we want to preserve */
551 jcopy_markers_execute(&srcinfo
, &dstinfo
, copyoption
);
553 /* Execute image transformation, if any */
554 #if TRANSFORMS_SUPPORTED
555 jtransform_execute_transformation(&srcinfo
, &dstinfo
,
560 /* Finish compression and release memory */
561 jpeg_finish_compress(&dstinfo
);
562 jpeg_destroy_compress(&dstinfo
);
563 (void) jpeg_finish_decompress(&srcinfo
);
564 jpeg_destroy_decompress(&srcinfo
);
566 /* Close output file, if we opened it */
570 #ifdef PROGRESS_REPORT
571 end_progress_monitor((j_common_ptr
) &dstinfo
);
575 exit(jsrcerr
.num_warnings
+ jdsterr
.num_warnings
?EXIT_WARNING
:EXIT_SUCCESS
);
576 return 0; /* suppress no-return-value warnings */