8 * Copyright (C) 1995-1997, Thomas G. Lane.
9 * This file is part of the Independent JPEG Group's software.
10 * For conditions of distribution and use, see the accompanying README file.
12 * This file contains a command-line user interface for JPEG transcoding.
13 * It is very similar to cjpeg.c, but provides lossless transcoding between
14 * different JPEG file formats. It also provides some lossless and sort-of-
15 * lossless transformations of JPEG data.
18 #include "cdjpeg.h" /* Common decls for cjpeg/djpeg applications */
19 #include "transupp.h" /* Support routines for jpegtran */
20 #include "jversion.h" /* for version message */
22 #ifdef USE_CCOMMAND /* command-line reader for Macintosh */
24 #include <SIOUX.h> /* Metrowerks needs this */
25 #include <console.h> /* ... and this */
28 #include <console.h> /* Think declares it here */
34 * Argument-parsing code.
35 * The switch parser is designed to be useful with DOS-style command line
36 * syntax, ie, intermixed switches and file names, where only the switches
37 * to the left of a given file name affect processing of that file.
38 * The main program in this file doesn't actually use this capability...
42 static const char * progname
; /* program name for error messages */
43 static char * outfilename
; /* for -outfile switch */
44 static JCOPY_OPTION copyoption
; /* -copy switch */
45 static jpeg_transform_info transformoption
; /* image transformation options */
50 /* complain about bad command line */
52 fprintf(stderr
, "usage: %s [switches] ", progname
);
53 #ifdef TWO_FILE_COMMANDLINE
54 fprintf(stderr
, "inputfile outputfile\n");
56 fprintf(stderr
, "[inputfile]\n");
59 fprintf(stderr
, "Switches (names may be abbreviated):\n");
60 fprintf(stderr
, " -copy none Copy no extra markers from source file\n");
61 fprintf(stderr
, " -copy comments Copy only comment markers (default)\n");
62 fprintf(stderr
, " -copy all Copy all extra markers\n");
63 #ifdef ENTROPY_OPT_SUPPORTED
64 fprintf(stderr
, " -optimize Optimize Huffman table (smaller file, but slow compression)\n");
66 #ifdef C_PROGRESSIVE_SUPPORTED
67 fprintf(stderr
, " -progressive Create progressive JPEG file\n");
69 #if TRANSFORMS_SUPPORTED
70 fprintf(stderr
, "Switches for modifying the 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
, " -rotate [90|180|270] Rotate image (degrees clockwise)\n");
74 fprintf(stderr
, " -transpose Transpose image\n");
75 fprintf(stderr
, " -transverse Transverse transpose image\n");
76 fprintf(stderr
, " -trim Drop non-transformable edge blocks\n");
77 #endif /* TRANSFORMS_SUPPORTED */
78 fprintf(stderr
, "Switches for advanced users:\n");
79 fprintf(stderr
, " -restart N Set restart interval in rows, or in blocks with B\n");
80 fprintf(stderr
, " -maxmemory N Maximum memory to use (in kbytes)\n");
81 fprintf(stderr
, " -outfile name Specify name for output file\n");
82 fprintf(stderr
, " -verbose or -debug Emit debug output\n");
83 fprintf(stderr
, "Switches for wizards:\n");
84 #ifdef C_ARITH_CODING_SUPPORTED
85 fprintf(stderr
, " -arithmetic Use arithmetic coding\n");
87 #ifdef C_MULTISCAN_FILES_SUPPORTED
88 fprintf(stderr
, " -scans file Create multi-scan JPEG per script file\n");
95 select_transform (JXFORM_CODE transform
)
96 /* Silly little routine to detect multiple transform options,
97 * which we can't handle.
100 #if TRANSFORMS_SUPPORTED
101 if (transformoption
.transform
== JXFORM_NONE
||
102 transformoption
.transform
== transform
) {
103 transformoption
.transform
= transform
;
105 fprintf(stderr
, "%s: can only do one image transformation at a time\n",
110 fprintf(stderr
, "%s: sorry, image transformation was not compiled\n",
118 parse_switches (j_compress_ptr cinfo
, int argc
, char **argv
,
119 int last_file_arg_seen
, boolean for_real
)
120 /* Parse optional switches.
121 * Returns argv[] index of first file-name argument (== argc if none).
122 * Any file names with indexes <= last_file_arg_seen are ignored;
123 * they have presumably been processed in a previous iteration.
124 * (Pass 0 for last_file_arg_seen on the first or only iteration.)
125 * for_real is FALSE on the first (dummy) pass; we may skip any expensive
131 boolean simple_progressive
;
132 char * scansarg
= NULL
; /* saves -scans parm if any */
134 /* Set up default JPEG parameters. */
135 simple_progressive
= FALSE
;
137 copyoption
= JCOPYOPT_DEFAULT
;
138 transformoption
.transform
= JXFORM_NONE
;
139 transformoption
.trim
= FALSE
;
140 transformoption
.force_grayscale
= FALSE
;
141 cinfo
->err
->trace_level
= 0;
143 /* Scan command line options, adjust parameters */
145 for (argn
= 1; argn
< argc
; argn
++) {
148 /* Not a switch, must be a file name argument */
149 if (argn
<= last_file_arg_seen
) {
150 outfilename
= NULL
; /* -outfile applies to just one input file */
151 continue; /* ignore this name if previously processed */
153 break; /* else done parsing switches */
155 arg
++; /* advance past switch marker character */
157 if (keymatch(arg
, "arithmetic", 1)) {
158 /* Use arithmetic coding. */
159 #ifdef C_ARITH_CODING_SUPPORTED
160 cinfo
->arith_code
= TRUE
;
162 fprintf(stderr
, "%s: sorry, arithmetic coding not supported\n",
167 } else if (keymatch(arg
, "copy", 1)) {
168 /* Select which extra markers to copy. */
169 if (++argn
>= argc
) /* advance to next argument */
171 if (keymatch(argv
[argn
], "none", 1)) {
172 copyoption
= JCOPYOPT_NONE
;
173 } else if (keymatch(argv
[argn
], "comments", 1)) {
174 copyoption
= JCOPYOPT_COMMENTS
;
175 } else if (keymatch(argv
[argn
], "all", 1)) {
176 copyoption
= JCOPYOPT_ALL
;
180 } else if (keymatch(arg
, "debug", 1) || keymatch(arg
, "verbose", 1)) {
181 /* Enable debug printouts. */
182 /* On first -d, print version identification */
183 static boolean printed_version
= FALSE
;
185 if (! printed_version
) {
186 fprintf(stderr
, "Independent JPEG Group's JPEGTRAN, version %s\n%s\n",
187 JVERSION
, JCOPYRIGHT
);
188 printed_version
= TRUE
;
190 cinfo
->err
->trace_level
++;
192 } else if (keymatch(arg
, "flip", 1)) {
193 /* Mirror left-right or top-bottom. */
194 if (++argn
>= argc
) /* advance to next argument */
196 if (keymatch(argv
[argn
], "horizontal", 1))
197 select_transform(JXFORM_FLIP_H
);
198 else if (keymatch(argv
[argn
], "vertical", 1))
199 select_transform(JXFORM_FLIP_V
);
203 } else if (keymatch(arg
, "grayscale", 1) || keymatch(arg
, "greyscale",1)) {
204 /* Force to grayscale. */
205 #if TRANSFORMS_SUPPORTED
206 transformoption
.force_grayscale
= TRUE
;
208 select_transform(JXFORM_NONE
); /* force an error */
211 } else if (keymatch(arg
, "maxmemory", 3)) {
212 /* Maximum memory in Kb (or Mb with 'm'). */
216 if (++argn
>= argc
) /* advance to next argument */
218 if (sscanf(argv
[argn
], "%ld%c", &lval
, &ch
) < 1)
220 if (ch
== 'm' || ch
== 'M')
222 cinfo
->mem
->max_memory_to_use
= lval
* 1000L;
224 } else if (keymatch(arg
, "optimize", 1) || keymatch(arg
, "optimise", 1)) {
225 /* Enable entropy parm optimization. */
226 #ifdef ENTROPY_OPT_SUPPORTED
227 cinfo
->optimize_coding
= TRUE
;
229 fprintf(stderr
, "%s: sorry, entropy optimization was not compiled\n",
234 } else if (keymatch(arg
, "outfile", 4)) {
235 /* Set output file name. */
236 if (++argn
>= argc
) /* advance to next argument */
238 outfilename
= argv
[argn
]; /* save it away for later use */
240 } else if (keymatch(arg
, "progressive", 1)) {
241 /* Select simple progressive mode. */
242 #ifdef C_PROGRESSIVE_SUPPORTED
243 simple_progressive
= TRUE
;
244 /* We must postpone execution until num_components is known. */
246 fprintf(stderr
, "%s: sorry, progressive output was not compiled\n",
251 } else if (keymatch(arg
, "restart", 1)) {
252 /* Restart interval in MCU rows (or in MCUs with 'b'). */
256 if (++argn
>= argc
) /* advance to next argument */
258 if (sscanf(argv
[argn
], "%ld%c", &lval
, &ch
) < 1)
260 if (lval
< 0 || lval
> 65535L)
262 if (ch
== 'b' || ch
== 'B') {
263 cinfo
->restart_interval
= (unsigned int) lval
;
264 cinfo
->restart_in_rows
= 0; /* else prior '-restart n' overrides me */
266 cinfo
->restart_in_rows
= (int) lval
;
267 /* restart_interval will be computed during startup */
270 } else if (keymatch(arg
, "rotate", 2)) {
271 /* Rotate 90, 180, or 270 degrees (measured clockwise). */
272 if (++argn
>= argc
) /* advance to next argument */
274 if (keymatch(argv
[argn
], "90", 2))
275 select_transform(JXFORM_ROT_90
);
276 else if (keymatch(argv
[argn
], "180", 3))
277 select_transform(JXFORM_ROT_180
);
278 else if (keymatch(argv
[argn
], "270", 3))
279 select_transform(JXFORM_ROT_270
);
283 } else if (keymatch(arg
, "scans", 1)) {
284 /* Set scan script. */
285 #ifdef C_MULTISCAN_FILES_SUPPORTED
286 if (++argn
>= argc
) /* advance to next argument */
288 scansarg
= argv
[argn
];
289 /* We must postpone reading the file in case -progressive appears. */
291 fprintf(stderr
, "%s: sorry, multi-scan output was not compiled\n",
296 } else if (keymatch(arg
, "transpose", 1)) {
297 /* Transpose (across UL-to-LR axis). */
298 select_transform(JXFORM_TRANSPOSE
);
300 } else if (keymatch(arg
, "transverse", 6)) {
301 /* Transverse transpose (across UR-to-LL axis). */
302 select_transform(JXFORM_TRANSVERSE
);
304 } else if (keymatch(arg
, "trim", 3)) {
305 /* Trim off any partial edge MCUs that the transform can't handle. */
306 transformoption
.trim
= TRUE
;
309 usage(); /* bogus switch */
313 /* Post-switch-scanning cleanup */
317 #ifdef C_PROGRESSIVE_SUPPORTED
318 if (simple_progressive
) /* process -progressive; -scans can override */
319 jpeg_simple_progression(cinfo
);
322 #ifdef C_MULTISCAN_FILES_SUPPORTED
323 if (scansarg
!= NULL
) /* process -scans if it was present */
324 if (! read_scan_script(cinfo
, scansarg
))
329 return argn
; /* return index of next arg (file name) */
338 main (int argc
, char **argv
)
340 struct jpeg_decompress_struct srcinfo
;
341 struct jpeg_compress_struct dstinfo
;
342 struct jpeg_error_mgr jsrcerr
, jdsterr
;
343 #ifdef PROGRESS_REPORT
344 struct cdjpeg_progress_mgr progress
;
346 jvirt_barray_ptr
* src_coef_arrays
;
347 jvirt_barray_ptr
* dst_coef_arrays
;
352 /* On Mac, fetch a command line. */
354 argc
= ccommand(&argv
);
358 if (progname
== NULL
|| progname
[0] == 0)
359 progname
= "jpegtran"; /* in case C library doesn't provide it */
361 /* Initialize the JPEG decompression object with default error handling. */
362 srcinfo
.err
= jpeg_std_error(&jsrcerr
);
363 jpeg_create_decompress(&srcinfo
);
364 /* Initialize the JPEG compression object with default error handling. */
365 dstinfo
.err
= jpeg_std_error(&jdsterr
);
366 jpeg_create_compress(&dstinfo
);
368 /* Now safe to enable signal catcher.
369 * Note: we assume only the decompression object will have virtual arrays.
371 #ifdef NEED_SIGNAL_CATCHER
372 enable_signal_catcher((j_common_ptr
) &srcinfo
);
375 /* Scan command line to find file names.
376 * It is convenient to use just one switch-parsing routine, but the switch
377 * values read here are mostly ignored; we will rescan the switches after
378 * opening the input file. Also note that most of the switches affect the
379 * destination JPEG object, so we parse into that and then copy over what
380 * needs to affects the source too.
383 file_index
= parse_switches(&dstinfo
, argc
, argv
, 0, FALSE
);
384 jsrcerr
.trace_level
= jdsterr
.trace_level
;
385 srcinfo
.mem
->max_memory_to_use
= dstinfo
.mem
->max_memory_to_use
;
387 #ifdef TWO_FILE_COMMANDLINE
388 /* Must have either -outfile switch or explicit output file name */
389 if (outfilename
== NULL
) {
390 if (file_index
!= argc
-2) {
391 fprintf(stderr
, "%s: must name one input and one output file\n",
395 outfilename
= argv
[file_index
+1];
397 if (file_index
!= argc
-1) {
398 fprintf(stderr
, "%s: must name one input and one output file\n",
404 /* Unix style: expect zero or one file name */
405 if (file_index
< argc
-1) {
406 fprintf(stderr
, "%s: only one input file\n", progname
);
409 #endif /* TWO_FILE_COMMANDLINE */
411 /* Open the input file. */
412 if (file_index
< argc
) {
413 if ((input_file
= fopen(argv
[file_index
], READ_BINARY
)) == NULL
) {
414 fprintf(stderr
, "%s: can't open %s\n", progname
, argv
[file_index
]);
418 /* default input file is stdin */
419 input_file
= read_stdin();
422 /* Open the output file. */
423 if (outfilename
!= NULL
) {
424 if ((output_file
= fopen(outfilename
, WRITE_BINARY
)) == NULL
) {
425 fprintf(stderr
, "%s: can't open %s\n", progname
, outfilename
);
429 /* default output file is stdout */
430 output_file
= write_stdout();
433 #ifdef PROGRESS_REPORT
434 start_progress_monitor((j_common_ptr
) &dstinfo
, &progress
);
437 /* Specify data source for decompression */
438 jpeg_stdio_src(&srcinfo
, input_file
);
440 /* Enable saving of extra markers that we want to copy */
441 jcopy_markers_setup(&srcinfo
, copyoption
);
443 /* Read file header */
444 (void) jpeg_read_header(&srcinfo
, TRUE
);
446 /* Any space needed by a transform option must be requested before
447 * jpeg_read_coefficients so that memory allocation will be done right.
449 #if TRANSFORMS_SUPPORTED
450 jtransform_request_workspace(&srcinfo
, &transformoption
);
453 /* Read source file as DCT coefficients */
454 src_coef_arrays
= jpeg_read_coefficients(&srcinfo
);
456 /* Initialize destination compression parameters from source values */
457 jpeg_copy_critical_parameters(&srcinfo
, &dstinfo
);
459 /* Adjust destination parameters if required by transform options;
460 * also find out which set of coefficient arrays will hold the output.
462 #if TRANSFORMS_SUPPORTED
463 dst_coef_arrays
= jtransform_adjust_parameters(&srcinfo
, &dstinfo
,
467 dst_coef_arrays
= src_coef_arrays
;
470 /* Adjust default compression parameters by re-parsing the options */
471 file_index
= parse_switches(&dstinfo
, argc
, argv
, 0, TRUE
);
473 /* Specify data destination for compression */
474 jpeg_stdio_dest(&dstinfo
, output_file
);
476 /* Start compressor (note no image data is actually written here) */
477 jpeg_write_coefficients(&dstinfo
, dst_coef_arrays
);
479 /* Copy to the output file any extra markers that we want to preserve */
480 jcopy_markers_execute(&srcinfo
, &dstinfo
, copyoption
);
482 /* Execute image transformation, if any */
483 #if TRANSFORMS_SUPPORTED
484 jtransform_execute_transformation(&srcinfo
, &dstinfo
,
489 /* Finish compression and release memory */
490 jpeg_finish_compress(&dstinfo
);
491 jpeg_destroy_compress(&dstinfo
);
492 (void) jpeg_finish_decompress(&srcinfo
);
493 jpeg_destroy_decompress(&srcinfo
);
495 /* Close files, if we opened them */
496 if (input_file
!= stdin
)
498 if (output_file
!= stdout
)
501 #ifdef PROGRESS_REPORT
502 end_progress_monitor((j_common_ptr
) &dstinfo
);
506 exit(jsrcerr
.num_warnings
+ jdsterr
.num_warnings
?EXIT_WARNING
:EXIT_SUCCESS
);
507 return 0; /* suppress no-return-value warnings */