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.
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.
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 JCOPY_OPTION copyoption
; /* -copy switch */
41 static jpeg_transform_info transformoption
; /* image transformation options */
46 /* complain about bad command line */
48 fprintf(stderr
, "usage: %s [switches] ", progname
);
49 #ifdef TWO_FILE_COMMANDLINE
50 fprintf(stderr
, "inputfile outputfile\n");
52 fprintf(stderr
, "[inputfile]\n");
55 fprintf(stderr
, "Switches (names may be abbreviated):\n");
56 fprintf(stderr
, " -copy none Copy no extra markers from source file\n");
57 fprintf(stderr
, " -copy comments Copy only comment markers (default)\n");
58 fprintf(stderr
, " -copy all Copy all extra markers\n");
59 #ifdef ENTROPY_OPT_SUPPORTED
60 fprintf(stderr
, " -optimize Optimize Huffman table (smaller file, but slow compression)\n");
62 #ifdef C_PROGRESSIVE_SUPPORTED
63 fprintf(stderr
, " -progressive Create progressive JPEG file\n");
65 #if TRANSFORMS_SUPPORTED
66 fprintf(stderr
, "Switches for modifying the image:\n");
67 fprintf(stderr
, " -crop WxH+X+Y Crop to a rectangular subarea\n");
68 fprintf(stderr
, " -grayscale Reduce to grayscale (omit color data)\n");
69 fprintf(stderr
, " -flip [horizontal|vertical] Mirror image (left-right or top-bottom)\n");
70 fprintf(stderr
, " -perfect Fail if there is non-transformable edge blocks\n");
71 fprintf(stderr
, " -rotate [90|180|270] Rotate image (degrees clockwise)\n");
72 fprintf(stderr
, " -transpose Transpose image\n");
73 fprintf(stderr
, " -transverse Transverse transpose image\n");
74 fprintf(stderr
, " -trim Drop non-transformable edge blocks\n");
75 #endif /* TRANSFORMS_SUPPORTED */
76 fprintf(stderr
, "Switches for advanced users:\n");
77 fprintf(stderr
, " -restart N Set restart interval in rows, or in blocks with B\n");
78 fprintf(stderr
, " -maxmemory N Maximum memory to use (in kbytes)\n");
79 fprintf(stderr
, " -outfile name Specify name for output file\n");
80 fprintf(stderr
, " -verbose or -debug Emit debug output\n");
81 fprintf(stderr
, "Switches for wizards:\n");
82 #ifdef C_ARITH_CODING_SUPPORTED
83 fprintf(stderr
, " -arithmetic Use arithmetic coding\n");
85 #ifdef C_MULTISCAN_FILES_SUPPORTED
86 fprintf(stderr
, " -scans file Create multi-scan JPEG per script file\n");
93 select_transform (JXFORM_CODE transform
)
94 /* Silly little routine to detect multiple transform options,
95 * which we can't handle.
98 #if TRANSFORMS_SUPPORTED
99 if (transformoption
.transform
== JXFORM_NONE
||
100 transformoption
.transform
== transform
) {
101 transformoption
.transform
= transform
;
103 fprintf(stderr
, "%s: can only do one image transformation at a time\n",
108 fprintf(stderr
, "%s: sorry, image transformation was not compiled\n",
116 parse_switches (j_compress_ptr cinfo
, int argc
, char **argv
,
117 int last_file_arg_seen
, boolean for_real
)
118 /* Parse optional switches.
119 * Returns argv[] index of first file-name argument (== argc if none).
120 * Any file names with indexes <= last_file_arg_seen are ignored;
121 * they have presumably been processed in a previous iteration.
122 * (Pass 0 for last_file_arg_seen on the first or only iteration.)
123 * for_real is FALSE on the first (dummy) pass; we may skip any expensive
129 boolean simple_progressive
;
130 char * scansarg
= NULL
; /* saves -scans parm if any */
132 /* Set up default JPEG parameters. */
133 simple_progressive
= FALSE
;
135 copyoption
= JCOPYOPT_DEFAULT
;
136 transformoption
.transform
= JXFORM_NONE
;
137 transformoption
.trim
= FALSE
;
138 transformoption
.perfect
= FALSE
;
139 transformoption
.force_grayscale
= FALSE
;
140 transformoption
.crop
= 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", 2)) {
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
, "crop", 2)) {
181 /* Perform lossless cropping. */
182 #if TRANSFORMS_SUPPORTED
183 if (++argn
>= argc
) /* advance to next argument */
185 if (! jtransform_parse_crop_spec(&transformoption
, argv
[argn
])) {
186 fprintf(stderr
, "%s: bogus -crop argument '%s'\n",
187 progname
, argv
[argn
]);
191 select_transform(JXFORM_NONE
); /* force an error */
194 } else if (keymatch(arg
, "debug", 1) || keymatch(arg
, "verbose", 1)) {
195 /* Enable debug printouts. */
196 /* On first -d, print version identification */
197 static boolean printed_version
= FALSE
;
199 if (! printed_version
) {
200 fprintf(stderr
, "Independent JPEG Group's JPEGTRAN, version %s\n%s\n",
201 JVERSION
, JCOPYRIGHT
);
202 printed_version
= TRUE
;
204 cinfo
->err
->trace_level
++;
206 } else if (keymatch(arg
, "flip", 1)) {
207 /* Mirror left-right or top-bottom. */
208 if (++argn
>= argc
) /* advance to next argument */
210 if (keymatch(argv
[argn
], "horizontal", 1))
211 select_transform(JXFORM_FLIP_H
);
212 else if (keymatch(argv
[argn
], "vertical", 1))
213 select_transform(JXFORM_FLIP_V
);
217 } else if (keymatch(arg
, "grayscale", 1) || keymatch(arg
, "greyscale",1)) {
218 /* Force to grayscale. */
219 #if TRANSFORMS_SUPPORTED
220 transformoption
.force_grayscale
= TRUE
;
222 select_transform(JXFORM_NONE
); /* force an error */
225 } else if (keymatch(arg
, "maxmemory", 3)) {
226 /* Maximum memory in Kb (or Mb with 'm'). */
230 if (++argn
>= argc
) /* advance to next argument */
232 if (sscanf(argv
[argn
], "%ld%c", &lval
, &ch
) < 1)
234 if (ch
== 'm' || ch
== 'M')
236 cinfo
->mem
->max_memory_to_use
= lval
* 1000L;
238 } else if (keymatch(arg
, "optimize", 1) || keymatch(arg
, "optimise", 1)) {
239 /* Enable entropy parm optimization. */
240 #ifdef ENTROPY_OPT_SUPPORTED
241 cinfo
->optimize_coding
= TRUE
;
243 fprintf(stderr
, "%s: sorry, entropy optimization was not compiled\n",
248 } else if (keymatch(arg
, "outfile", 4)) {
249 /* Set output file name. */
250 if (++argn
>= argc
) /* advance to next argument */
252 outfilename
= argv
[argn
]; /* save it away for later use */
254 } else if (keymatch(arg
, "perfect", 2)) {
255 /* Fail if there is any partial edge MCUs that the transform can't
257 transformoption
.perfect
= TRUE
;
259 } else if (keymatch(arg
, "progressive", 2)) {
260 /* Select simple progressive mode. */
261 #ifdef C_PROGRESSIVE_SUPPORTED
262 simple_progressive
= TRUE
;
263 /* We must postpone execution until num_components is known. */
265 fprintf(stderr
, "%s: sorry, progressive output was not compiled\n",
270 } else if (keymatch(arg
, "restart", 1)) {
271 /* Restart interval in MCU rows (or in MCUs with 'b'). */
275 if (++argn
>= argc
) /* advance to next argument */
277 if (sscanf(argv
[argn
], "%ld%c", &lval
, &ch
) < 1)
279 if (lval
< 0 || lval
> 65535L)
281 if (ch
== 'b' || ch
== 'B') {
282 cinfo
->restart_interval
= (unsigned int) lval
;
283 cinfo
->restart_in_rows
= 0; /* else prior '-restart n' overrides me */
285 cinfo
->restart_in_rows
= (int) lval
;
286 /* restart_interval will be computed during startup */
289 } else if (keymatch(arg
, "rotate", 2)) {
290 /* Rotate 90, 180, or 270 degrees (measured clockwise). */
291 if (++argn
>= argc
) /* advance to next argument */
293 if (keymatch(argv
[argn
], "90", 2))
294 select_transform(JXFORM_ROT_90
);
295 else if (keymatch(argv
[argn
], "180", 3))
296 select_transform(JXFORM_ROT_180
);
297 else if (keymatch(argv
[argn
], "270", 3))
298 select_transform(JXFORM_ROT_270
);
302 } else if (keymatch(arg
, "scans", 1)) {
303 /* Set scan script. */
304 #ifdef C_MULTISCAN_FILES_SUPPORTED
305 if (++argn
>= argc
) /* advance to next argument */
307 scansarg
= argv
[argn
];
308 /* We must postpone reading the file in case -progressive appears. */
310 fprintf(stderr
, "%s: sorry, multi-scan output was not compiled\n",
315 } else if (keymatch(arg
, "transpose", 1)) {
316 /* Transpose (across UL-to-LR axis). */
317 select_transform(JXFORM_TRANSPOSE
);
319 } else if (keymatch(arg
, "transverse", 6)) {
320 /* Transverse transpose (across UR-to-LL axis). */
321 select_transform(JXFORM_TRANSVERSE
);
323 } else if (keymatch(arg
, "trim", 3)) {
324 /* Trim off any partial edge MCUs that the transform can't handle. */
325 transformoption
.trim
= TRUE
;
328 usage(); /* bogus switch */
332 /* Post-switch-scanning cleanup */
336 #ifdef C_PROGRESSIVE_SUPPORTED
337 if (simple_progressive
) /* process -progressive; -scans can override */
338 jpeg_simple_progression(cinfo
);
341 #ifdef C_MULTISCAN_FILES_SUPPORTED
342 if (scansarg
!= NULL
) /* process -scans if it was present */
343 if (! read_scan_script(cinfo
, scansarg
))
348 return argn
; /* return index of next arg (file name) */
357 main (int argc
, char **argv
)
359 struct jpeg_decompress_struct srcinfo
;
360 struct jpeg_compress_struct dstinfo
;
361 struct jpeg_error_mgr jsrcerr
, jdsterr
;
362 #ifdef PROGRESS_REPORT
363 struct cdjpeg_progress_mgr progress
;
365 jvirt_barray_ptr
* src_coef_arrays
;
366 jvirt_barray_ptr
* dst_coef_arrays
;
368 /* We assume all-in-memory processing and can therefore use only a
369 * single file pointer for sequential input and output operation.
373 /* On Mac, fetch a command line. */
375 argc
= ccommand(&argv
);
379 if (progname
== NULL
|| progname
[0] == 0)
380 progname
= "jpegtran"; /* in case C library doesn't provide it */
382 /* Initialize the JPEG decompression object with default error handling. */
383 srcinfo
.err
= jpeg_std_error(&jsrcerr
);
384 jpeg_create_decompress(&srcinfo
);
385 /* Initialize the JPEG compression object with default error handling. */
386 dstinfo
.err
= jpeg_std_error(&jdsterr
);
387 jpeg_create_compress(&dstinfo
);
389 /* Now safe to enable signal catcher.
390 * Note: we assume only the decompression object will have virtual arrays.
392 #ifdef NEED_SIGNAL_CATCHER
393 enable_signal_catcher((j_common_ptr
) &srcinfo
);
396 /* Scan command line to find file names.
397 * It is convenient to use just one switch-parsing routine, but the switch
398 * values read here are mostly ignored; we will rescan the switches after
399 * opening the input file. Also note that most of the switches affect the
400 * destination JPEG object, so we parse into that and then copy over what
401 * needs to affects the source too.
404 file_index
= parse_switches(&dstinfo
, argc
, argv
, 0, FALSE
);
405 jsrcerr
.trace_level
= jdsterr
.trace_level
;
406 srcinfo
.mem
->max_memory_to_use
= dstinfo
.mem
->max_memory_to_use
;
408 #ifdef TWO_FILE_COMMANDLINE
409 /* Must have either -outfile switch or explicit output file name */
410 if (outfilename
== NULL
) {
411 if (file_index
!= argc
-2) {
412 fprintf(stderr
, "%s: must name one input and one output file\n",
416 outfilename
= argv
[file_index
+1];
418 if (file_index
!= argc
-1) {
419 fprintf(stderr
, "%s: must name one input and one output file\n",
425 /* Unix style: expect zero or one file name */
426 if (file_index
< argc
-1) {
427 fprintf(stderr
, "%s: only one input file\n", progname
);
430 #endif /* TWO_FILE_COMMANDLINE */
432 /* Open the input file. */
433 if (file_index
< argc
) {
434 if ((fp
= fopen(argv
[file_index
], READ_BINARY
)) == NULL
) {
435 fprintf(stderr
, "%s: can't open %s for reading\n", progname
, argv
[file_index
]);
439 /* default input file is stdin */
443 #ifdef PROGRESS_REPORT
444 start_progress_monitor((j_common_ptr
) &dstinfo
, &progress
);
447 /* Specify data source for decompression */
448 jpeg_stdio_src(&srcinfo
, fp
);
450 /* Enable saving of extra markers that we want to copy */
451 jcopy_markers_setup(&srcinfo
, copyoption
);
453 /* Read file header */
454 (void) jpeg_read_header(&srcinfo
, TRUE
);
456 /* Any space needed by a transform option must be requested before
457 * jpeg_read_coefficients so that memory allocation will be done right.
459 #if TRANSFORMS_SUPPORTED
460 /* Fails right away if -perfect is given and transformation is not perfect.
462 if (transformoption
.perfect
&&
463 !jtransform_perfect_transform(srcinfo
.image_width
, srcinfo
.image_height
,
464 srcinfo
.max_h_samp_factor
* DCTSIZE
, srcinfo
.max_v_samp_factor
* DCTSIZE
,
465 transformoption
.transform
)) {
466 fprintf(stderr
, "%s: transformation is not perfect\n", progname
);
469 jtransform_request_workspace(&srcinfo
, &transformoption
);
472 /* Read source file as DCT coefficients */
473 src_coef_arrays
= jpeg_read_coefficients(&srcinfo
);
475 /* Initialize destination compression parameters from source values */
476 jpeg_copy_critical_parameters(&srcinfo
, &dstinfo
);
478 /* Adjust destination parameters if required by transform options;
479 * also find out which set of coefficient arrays will hold the output.
481 #if TRANSFORMS_SUPPORTED
482 dst_coef_arrays
= jtransform_adjust_parameters(&srcinfo
, &dstinfo
,
486 dst_coef_arrays
= src_coef_arrays
;
489 /* Close input file, if we opened it.
490 * Note: we assume that jpeg_read_coefficients consumed all input
491 * until JPEG_REACHED_EOI, and that jpeg_finish_decompress will
492 * only consume more while (! cinfo->inputctl->eoi_reached).
493 * We cannot call jpeg_finish_decompress here since we still need the
494 * virtual arrays allocated from the source object for processing.
499 /* Open the output file. */
500 if (outfilename
!= NULL
) {
501 if ((fp
= fopen(outfilename
, WRITE_BINARY
)) == NULL
) {
502 fprintf(stderr
, "%s: can't open %s for writing\n", progname
, outfilename
);
506 /* default output file is stdout */
510 /* Adjust default compression parameters by re-parsing the options */
511 file_index
= parse_switches(&dstinfo
, argc
, argv
, 0, TRUE
);
513 /* Specify data destination for compression */
514 jpeg_stdio_dest(&dstinfo
, fp
);
516 /* Start compressor (note no image data is actually written here) */
517 jpeg_write_coefficients(&dstinfo
, dst_coef_arrays
);
519 /* Copy to the output file any extra markers that we want to preserve */
520 jcopy_markers_execute(&srcinfo
, &dstinfo
, copyoption
);
522 /* Execute image transformation, if any */
523 #if TRANSFORMS_SUPPORTED
524 jtransform_execute_transformation(&srcinfo
, &dstinfo
,
529 /* Finish compression and release memory */
530 jpeg_finish_compress(&dstinfo
);
531 jpeg_destroy_compress(&dstinfo
);
532 (void) jpeg_finish_decompress(&srcinfo
);
533 jpeg_destroy_decompress(&srcinfo
);
535 /* Close output file, if we opened it */
539 #ifdef PROGRESS_REPORT
540 end_progress_monitor((j_common_ptr
) &dstinfo
);
544 exit(jsrcerr
.num_warnings
+ jdsterr
.num_warnings
?EXIT_WARNING
:EXIT_SUCCESS
);
545 return 0; /* suppress no-return-value warnings */