blob: 058e8443e074847dcc534a0261c4886f396a6257 [file] [log] [blame]
Thomas G. Lanebc79e061995-08-02 00:00:00 +00001/*
2 * jpegtran.c
3 *
DRCa73e8702012-12-31 02:52:30 +00004 * This file was part of the Independent JPEG Group's software:
Guido Vollbedingf18f81b2010-02-28 00:00:00 +00005 * Copyright (C) 1995-2010, Thomas G. Lane, Guido Vollbeding.
DRCa6ef2822013-09-28 03:23:49 +00006 * libjpeg-turbo Modifications:
Leon Scroggins IIIbd7903e2018-02-28 14:05:04 -05007 * Copyright (C) 2010, 2014, 2017, D. R. Commander.
Alex Naidis6eb7d372016-10-16 23:10:08 +02008 * For conditions of distribution and use, see the accompanying README.ijg
9 * file.
Thomas G. Lanebc79e061995-08-02 00:00:00 +000010 *
11 * This file contains a command-line user interface for JPEG transcoding.
Guido Vollbeding989630f2010-01-10 00:00:00 +000012 * It is very similar to cjpeg.c, and partly to djpeg.c, but provides
13 * lossless transcoding between different JPEG file formats. It also
14 * provides some lossless and sort-of-lossless transformations of JPEG data.
Thomas G. Lanebc79e061995-08-02 00:00:00 +000015 */
16
DRCe5eaf372014-05-09 18:00:32 +000017#include "cdjpeg.h" /* Common decls for cjpeg/djpeg applications */
18#include "transupp.h" /* Support routines for jpegtran */
19#include "jversion.h" /* for version message */
DRCff6961f2014-04-20 09:17:11 +000020#include "jconfigint.h"
Thomas G. Lanebc79e061995-08-02 00:00:00 +000021
DRCe5eaf372014-05-09 18:00:32 +000022#ifdef USE_CCOMMAND /* command-line reader for Macintosh */
Thomas G. Lanebc79e061995-08-02 00:00:00 +000023#ifdef __MWERKS__
Thomas G. Lane489583f1996-02-07 00:00:00 +000024#include <SIOUX.h> /* Metrowerks needs this */
DRCe5eaf372014-05-09 18:00:32 +000025#include <console.h> /* ... and this */
Thomas G. Lanebc79e061995-08-02 00:00:00 +000026#endif
27#ifdef THINK_C
DRCe5eaf372014-05-09 18:00:32 +000028#include <console.h> /* Think declares it here */
Thomas G. Lanebc79e061995-08-02 00:00:00 +000029#endif
30#endif
31
32
33/*
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...
39 */
40
41
Alex Naidis6eb7d372016-10-16 23:10:08 +020042static const char *progname; /* program name for error messages */
Leon Scroggins III3993b372018-07-16 10:43:45 -040043static char *icc_filename; /* for -icc switch */
Alex Naidis6eb7d372016-10-16 23:10:08 +020044static char *outfilename; /* for -outfile switch */
DRCe5eaf372014-05-09 18:00:32 +000045static JCOPY_OPTION copyoption; /* -copy switch */
Thomas G. Lane5ead57a1998-03-27 00:00:00 +000046static jpeg_transform_info transformoption; /* image transformation options */
Thomas G. Lanebc79e061995-08-02 00:00:00 +000047
48
Thomas G. Lane489583f1996-02-07 00:00:00 +000049LOCAL(void)
Leon Scroggins III3993b372018-07-16 10:43:45 -040050usage(void)
Thomas G. Lanebc79e061995-08-02 00:00:00 +000051/* complain about bad command line */
52{
53 fprintf(stderr, "usage: %s [switches] ", progname);
54#ifdef TWO_FILE_COMMANDLINE
55 fprintf(stderr, "inputfile outputfile\n");
56#else
57 fprintf(stderr, "[inputfile]\n");
58#endif
59
60 fprintf(stderr, "Switches (names may be abbreviated):\n");
Thomas G. Lane5ead57a1998-03-27 00:00:00 +000061 fprintf(stderr, " -copy none Copy no extra markers from source file\n");
62 fprintf(stderr, " -copy comments Copy only comment markers (default)\n");
63 fprintf(stderr, " -copy all Copy all extra markers\n");
Thomas G. Lanebc79e061995-08-02 00:00:00 +000064#ifdef ENTROPY_OPT_SUPPORTED
65 fprintf(stderr, " -optimize Optimize Huffman table (smaller file, but slow compression)\n");
66#endif
67#ifdef C_PROGRESSIVE_SUPPORTED
68 fprintf(stderr, " -progressive Create progressive JPEG file\n");
69#endif
Thomas G. Lane5ead57a1998-03-27 00:00:00 +000070 fprintf(stderr, "Switches for modifying the image:\n");
Guido Vollbedingf18f81b2010-02-28 00:00:00 +000071#if TRANSFORMS_SUPPORTED
Guido Vollbeding5996a252009-06-27 00:00:00 +000072 fprintf(stderr, " -crop WxH+X+Y Crop to a rectangular subarea\n");
Thomas G. Lane5ead57a1998-03-27 00:00:00 +000073 fprintf(stderr, " -grayscale Reduce to grayscale (omit color data)\n");
74 fprintf(stderr, " -flip [horizontal|vertical] Mirror image (left-right or top-bottom)\n");
Guido Vollbeding5996a252009-06-27 00:00:00 +000075 fprintf(stderr, " -perfect Fail if there is non-transformable edge blocks\n");
Thomas G. Lane5ead57a1998-03-27 00:00:00 +000076 fprintf(stderr, " -rotate [90|180|270] Rotate image (degrees clockwise)\n");
Guido Vollbedingf18f81b2010-02-28 00:00:00 +000077#endif
Guido Vollbedingf18f81b2010-02-28 00:00:00 +000078#if TRANSFORMS_SUPPORTED
Thomas G. Lane5ead57a1998-03-27 00:00:00 +000079 fprintf(stderr, " -transpose Transpose image\n");
80 fprintf(stderr, " -transverse Transverse transpose image\n");
81 fprintf(stderr, " -trim Drop non-transformable edge blocks\n");
Guido Vollbedingf18f81b2010-02-28 00:00:00 +000082#endif
Thomas G. Lanebc79e061995-08-02 00:00:00 +000083 fprintf(stderr, "Switches for advanced users:\n");
DRCd657ba62012-01-27 09:41:20 +000084#ifdef C_ARITH_CODING_SUPPORTED
85 fprintf(stderr, " -arithmetic Use arithmetic coding\n");
86#endif
Leon Scroggins III3993b372018-07-16 10:43:45 -040087 fprintf(stderr, " -icc FILE Embed ICC profile contained in FILE\n");
Thomas G. Lanebc79e061995-08-02 00:00:00 +000088 fprintf(stderr, " -restart N Set restart interval in rows, or in blocks with B\n");
89 fprintf(stderr, " -maxmemory N Maximum memory to use (in kbytes)\n");
90 fprintf(stderr, " -outfile name Specify name for output file\n");
91 fprintf(stderr, " -verbose or -debug Emit debug output\n");
DRC9665f5e2014-11-22 04:04:38 +000092 fprintf(stderr, " -version Print version information and exit\n");
Thomas G. Lanebc79e061995-08-02 00:00:00 +000093 fprintf(stderr, "Switches for wizards:\n");
Thomas G. Lanebc79e061995-08-02 00:00:00 +000094#ifdef C_MULTISCAN_FILES_SUPPORTED
Leon Scroggins IIIbd7903e2018-02-28 14:05:04 -050095 fprintf(stderr, " -scans FILE Create multi-scan JPEG per script FILE\n");
Thomas G. Lanebc79e061995-08-02 00:00:00 +000096#endif
97 exit(EXIT_FAILURE);
98}
99
100
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000101LOCAL(void)
Leon Scroggins III3993b372018-07-16 10:43:45 -0400102select_transform(JXFORM_CODE transform)
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000103/* Silly little routine to detect multiple transform options,
104 * which we can't handle.
105 */
106{
107#if TRANSFORMS_SUPPORTED
108 if (transformoption.transform == JXFORM_NONE ||
109 transformoption.transform == transform) {
110 transformoption.transform = transform;
111 } else {
112 fprintf(stderr, "%s: can only do one image transformation at a time\n",
DRCe5eaf372014-05-09 18:00:32 +0000113 progname);
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000114 usage();
115 }
116#else
117 fprintf(stderr, "%s: sorry, image transformation was not compiled\n",
DRCe5eaf372014-05-09 18:00:32 +0000118 progname);
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000119 exit(EXIT_FAILURE);
120#endif
121}
122
123
Thomas G. Lane489583f1996-02-07 00:00:00 +0000124LOCAL(int)
Leon Scroggins III3993b372018-07-16 10:43:45 -0400125parse_switches(j_compress_ptr cinfo, int argc, char **argv,
126 int last_file_arg_seen, boolean for_real)
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000127/* Parse optional switches.
128 * Returns argv[] index of first file-name argument (== argc if none).
129 * Any file names with indexes <= last_file_arg_seen are ignored;
130 * they have presumably been processed in a previous iteration.
131 * (Pass 0 for last_file_arg_seen on the first or only iteration.)
132 * for_real is FALSE on the first (dummy) pass; we may skip any expensive
133 * processing.
134 */
135{
136 int argn;
Alex Naidis6eb7d372016-10-16 23:10:08 +0200137 char *arg;
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000138 boolean simple_progressive;
Alex Naidis6eb7d372016-10-16 23:10:08 +0200139 char *scansarg = NULL; /* saves -scans parm if any */
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000140
141 /* Set up default JPEG parameters. */
142 simple_progressive = FALSE;
Leon Scroggins III3993b372018-07-16 10:43:45 -0400143 icc_filename = NULL;
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000144 outfilename = NULL;
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000145 copyoption = JCOPYOPT_DEFAULT;
146 transformoption.transform = JXFORM_NONE;
Guido Vollbeding5996a252009-06-27 00:00:00 +0000147 transformoption.perfect = FALSE;
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000148 transformoption.trim = FALSE;
149 transformoption.force_grayscale = FALSE;
Guido Vollbeding5996a252009-06-27 00:00:00 +0000150 transformoption.crop = FALSE;
DRCba5ea512011-03-04 03:20:34 +0000151 transformoption.slow_hflip = FALSE;
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000152 cinfo->err->trace_level = 0;
153
154 /* Scan command line options, adjust parameters */
155
156 for (argn = 1; argn < argc; argn++) {
157 arg = argv[argn];
158 if (*arg != '-') {
159 /* Not a switch, must be a file name argument */
160 if (argn <= last_file_arg_seen) {
DRCe5eaf372014-05-09 18:00:32 +0000161 outfilename = NULL; /* -outfile applies to just one input file */
162 continue; /* ignore this name if previously processed */
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000163 }
DRCe5eaf372014-05-09 18:00:32 +0000164 break; /* else done parsing switches */
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000165 }
DRCe5eaf372014-05-09 18:00:32 +0000166 arg++; /* advance past switch marker character */
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000167
168 if (keymatch(arg, "arithmetic", 1)) {
169 /* Use arithmetic coding. */
170#ifdef C_ARITH_CODING_SUPPORTED
171 cinfo->arith_code = TRUE;
172#else
173 fprintf(stderr, "%s: sorry, arithmetic coding not supported\n",
DRCe5eaf372014-05-09 18:00:32 +0000174 progname);
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000175 exit(EXIT_FAILURE);
176#endif
177
Guido Vollbeding5996a252009-06-27 00:00:00 +0000178 } else if (keymatch(arg, "copy", 2)) {
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000179 /* Select which extra markers to copy. */
DRCe5eaf372014-05-09 18:00:32 +0000180 if (++argn >= argc) /* advance to next argument */
181 usage();
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000182 if (keymatch(argv[argn], "none", 1)) {
DRCe5eaf372014-05-09 18:00:32 +0000183 copyoption = JCOPYOPT_NONE;
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000184 } else if (keymatch(argv[argn], "comments", 1)) {
DRCe5eaf372014-05-09 18:00:32 +0000185 copyoption = JCOPYOPT_COMMENTS;
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000186 } else if (keymatch(argv[argn], "all", 1)) {
DRCe5eaf372014-05-09 18:00:32 +0000187 copyoption = JCOPYOPT_ALL;
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000188 } else
DRCe5eaf372014-05-09 18:00:32 +0000189 usage();
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000190
Guido Vollbeding5996a252009-06-27 00:00:00 +0000191 } else if (keymatch(arg, "crop", 2)) {
192 /* Perform lossless cropping. */
193#if TRANSFORMS_SUPPORTED
DRCe5eaf372014-05-09 18:00:32 +0000194 if (++argn >= argc) /* advance to next argument */
195 usage();
Leon Scroggins III3993b372018-07-16 10:43:45 -0400196 if (!jtransform_parse_crop_spec(&transformoption, argv[argn])) {
DRCe5eaf372014-05-09 18:00:32 +0000197 fprintf(stderr, "%s: bogus -crop argument '%s'\n",
198 progname, argv[argn]);
199 exit(EXIT_FAILURE);
Guido Vollbeding5996a252009-06-27 00:00:00 +0000200 }
201#else
DRCe5eaf372014-05-09 18:00:32 +0000202 select_transform(JXFORM_NONE); /* force an error */
Guido Vollbeding5996a252009-06-27 00:00:00 +0000203#endif
204
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000205 } else if (keymatch(arg, "debug", 1) || keymatch(arg, "verbose", 1)) {
206 /* Enable debug printouts. */
207 /* On first -d, print version identification */
208 static boolean printed_version = FALSE;
209
Leon Scroggins III3993b372018-07-16 10:43:45 -0400210 if (!printed_version) {
DRCe5eaf372014-05-09 18:00:32 +0000211 fprintf(stderr, "%s version %s (build %s)\n",
212 PACKAGE_NAME, VERSION, BUILD);
213 fprintf(stderr, "%s\n\n", JCOPYRIGHT);
214 fprintf(stderr, "Emulating The Independent JPEG Group's software, version %s\n\n",
215 JVERSION);
216 printed_version = TRUE;
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000217 }
218 cinfo->err->trace_level++;
219
DRC9665f5e2014-11-22 04:04:38 +0000220 } else if (keymatch(arg, "version", 4)) {
221 fprintf(stderr, "%s version %s (build %s)\n",
222 PACKAGE_NAME, VERSION, BUILD);
DRC306add82014-11-22 04:25:04 +0000223 exit(EXIT_SUCCESS);
DRC9665f5e2014-11-22 04:04:38 +0000224
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000225 } else if (keymatch(arg, "flip", 1)) {
226 /* Mirror left-right or top-bottom. */
DRCe5eaf372014-05-09 18:00:32 +0000227 if (++argn >= argc) /* advance to next argument */
228 usage();
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000229 if (keymatch(argv[argn], "horizontal", 1))
DRCe5eaf372014-05-09 18:00:32 +0000230 select_transform(JXFORM_FLIP_H);
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000231 else if (keymatch(argv[argn], "vertical", 1))
DRCe5eaf372014-05-09 18:00:32 +0000232 select_transform(JXFORM_FLIP_V);
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000233 else
DRCe5eaf372014-05-09 18:00:32 +0000234 usage();
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000235
Leon Scroggins III3993b372018-07-16 10:43:45 -0400236 } else if (keymatch(arg, "grayscale", 1) ||
237 keymatch(arg, "greyscale", 1)) {
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000238 /* Force to grayscale. */
239#if TRANSFORMS_SUPPORTED
240 transformoption.force_grayscale = TRUE;
241#else
DRCe5eaf372014-05-09 18:00:32 +0000242 select_transform(JXFORM_NONE); /* force an error */
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000243#endif
244
Leon Scroggins III3993b372018-07-16 10:43:45 -0400245 } else if (keymatch(arg, "icc", 1)) {
246 /* Set ICC filename. */
247 if (++argn >= argc) /* advance to next argument */
248 usage();
249 icc_filename = argv[argn];
250
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000251 } else if (keymatch(arg, "maxmemory", 3)) {
252 /* Maximum memory in Kb (or Mb with 'm'). */
253 long lval;
254 char ch = 'x';
255
DRCe5eaf372014-05-09 18:00:32 +0000256 if (++argn >= argc) /* advance to next argument */
257 usage();
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000258 if (sscanf(argv[argn], "%ld%c", &lval, &ch) < 1)
DRCe5eaf372014-05-09 18:00:32 +0000259 usage();
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000260 if (ch == 'm' || ch == 'M')
DRCe5eaf372014-05-09 18:00:32 +0000261 lval *= 1000L;
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000262 cinfo->mem->max_memory_to_use = lval * 1000L;
263
264 } else if (keymatch(arg, "optimize", 1) || keymatch(arg, "optimise", 1)) {
265 /* Enable entropy parm optimization. */
266#ifdef ENTROPY_OPT_SUPPORTED
267 cinfo->optimize_coding = TRUE;
268#else
269 fprintf(stderr, "%s: sorry, entropy optimization was not compiled\n",
DRCe5eaf372014-05-09 18:00:32 +0000270 progname);
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000271 exit(EXIT_FAILURE);
272#endif
273
274 } else if (keymatch(arg, "outfile", 4)) {
275 /* Set output file name. */
DRCe5eaf372014-05-09 18:00:32 +0000276 if (++argn >= argc) /* advance to next argument */
277 usage();
278 outfilename = argv[argn]; /* save it away for later use */
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000279
Guido Vollbeding5996a252009-06-27 00:00:00 +0000280 } else if (keymatch(arg, "perfect", 2)) {
281 /* Fail if there is any partial edge MCUs that the transform can't
282 * handle. */
283 transformoption.perfect = TRUE;
284
285 } else if (keymatch(arg, "progressive", 2)) {
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000286 /* Select simple progressive mode. */
287#ifdef C_PROGRESSIVE_SUPPORTED
288 simple_progressive = TRUE;
289 /* We must postpone execution until num_components is known. */
290#else
291 fprintf(stderr, "%s: sorry, progressive output was not compiled\n",
DRCe5eaf372014-05-09 18:00:32 +0000292 progname);
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000293 exit(EXIT_FAILURE);
294#endif
295
296 } else if (keymatch(arg, "restart", 1)) {
297 /* Restart interval in MCU rows (or in MCUs with 'b'). */
298 long lval;
299 char ch = 'x';
300
DRCe5eaf372014-05-09 18:00:32 +0000301 if (++argn >= argc) /* advance to next argument */
302 usage();
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000303 if (sscanf(argv[argn], "%ld%c", &lval, &ch) < 1)
DRCe5eaf372014-05-09 18:00:32 +0000304 usage();
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000305 if (lval < 0 || lval > 65535L)
DRCe5eaf372014-05-09 18:00:32 +0000306 usage();
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000307 if (ch == 'b' || ch == 'B') {
Leon Scroggins III3993b372018-07-16 10:43:45 -0400308 cinfo->restart_interval = (unsigned int)lval;
DRCe5eaf372014-05-09 18:00:32 +0000309 cinfo->restart_in_rows = 0; /* else prior '-restart n' overrides me */
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000310 } else {
Leon Scroggins III3993b372018-07-16 10:43:45 -0400311 cinfo->restart_in_rows = (int)lval;
DRCe5eaf372014-05-09 18:00:32 +0000312 /* restart_interval will be computed during startup */
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000313 }
314
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000315 } else if (keymatch(arg, "rotate", 2)) {
316 /* Rotate 90, 180, or 270 degrees (measured clockwise). */
DRCe5eaf372014-05-09 18:00:32 +0000317 if (++argn >= argc) /* advance to next argument */
318 usage();
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000319 if (keymatch(argv[argn], "90", 2))
DRCe5eaf372014-05-09 18:00:32 +0000320 select_transform(JXFORM_ROT_90);
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000321 else if (keymatch(argv[argn], "180", 3))
DRCe5eaf372014-05-09 18:00:32 +0000322 select_transform(JXFORM_ROT_180);
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000323 else if (keymatch(argv[argn], "270", 3))
DRCe5eaf372014-05-09 18:00:32 +0000324 select_transform(JXFORM_ROT_270);
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000325 else
DRCe5eaf372014-05-09 18:00:32 +0000326 usage();
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000327
328 } else if (keymatch(arg, "scans", 1)) {
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000329 /* Set scan script. */
330#ifdef C_MULTISCAN_FILES_SUPPORTED
DRCe5eaf372014-05-09 18:00:32 +0000331 if (++argn >= argc) /* advance to next argument */
332 usage();
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000333 scansarg = argv[argn];
334 /* We must postpone reading the file in case -progressive appears. */
335#else
336 fprintf(stderr, "%s: sorry, multi-scan output was not compiled\n",
DRCe5eaf372014-05-09 18:00:32 +0000337 progname);
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000338 exit(EXIT_FAILURE);
339#endif
340
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000341 } else if (keymatch(arg, "transpose", 1)) {
342 /* Transpose (across UL-to-LR axis). */
343 select_transform(JXFORM_TRANSPOSE);
344
345 } else if (keymatch(arg, "transverse", 6)) {
346 /* Transverse transpose (across UR-to-LL axis). */
347 select_transform(JXFORM_TRANSVERSE);
348
349 } else if (keymatch(arg, "trim", 3)) {
350 /* Trim off any partial edge MCUs that the transform can't handle. */
351 transformoption.trim = TRUE;
352
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000353 } else {
DRCe5eaf372014-05-09 18:00:32 +0000354 usage(); /* bogus switch */
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000355 }
356 }
357
358 /* Post-switch-scanning cleanup */
359
360 if (for_real) {
361
362#ifdef C_PROGRESSIVE_SUPPORTED
DRCe5eaf372014-05-09 18:00:32 +0000363 if (simple_progressive) /* process -progressive; -scans can override */
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000364 jpeg_simple_progression(cinfo);
365#endif
366
367#ifdef C_MULTISCAN_FILES_SUPPORTED
DRCe5eaf372014-05-09 18:00:32 +0000368 if (scansarg != NULL) /* process -scans if it was present */
Leon Scroggins III3993b372018-07-16 10:43:45 -0400369 if (!read_scan_script(cinfo, scansarg))
DRCe5eaf372014-05-09 18:00:32 +0000370 usage();
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000371#endif
372 }
373
DRCe5eaf372014-05-09 18:00:32 +0000374 return argn; /* return index of next arg (file name) */
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000375}
376
377
378/*
379 * The main program.
380 */
381
Thomas G. Lane489583f1996-02-07 00:00:00 +0000382int
Leon Scroggins III3993b372018-07-16 10:43:45 -0400383main(int argc, char **argv)
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000384{
385 struct jpeg_decompress_struct srcinfo;
386 struct jpeg_compress_struct dstinfo;
387 struct jpeg_error_mgr jsrcerr, jdsterr;
388#ifdef PROGRESS_REPORT
389 struct cdjpeg_progress_mgr progress;
390#endif
Alex Naidis6eb7d372016-10-16 23:10:08 +0200391 jvirt_barray_ptr *src_coef_arrays;
392 jvirt_barray_ptr *dst_coef_arrays;
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000393 int file_index;
Guido Vollbeding5996a252009-06-27 00:00:00 +0000394 /* We assume all-in-memory processing and can therefore use only a
DRCff6961f2014-04-20 09:17:11 +0000395 * single file pointer for sequential input and output operation.
Guido Vollbeding5996a252009-06-27 00:00:00 +0000396 */
Alex Naidis6eb7d372016-10-16 23:10:08 +0200397 FILE *fp;
Leon Scroggins III3993b372018-07-16 10:43:45 -0400398 FILE *icc_file;
399 JOCTET *icc_profile = NULL;
400 long icc_len = 0;
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000401
402 /* On Mac, fetch a command line. */
403#ifdef USE_CCOMMAND
404 argc = ccommand(&argv);
405#endif
406
407 progname = argv[0];
408 if (progname == NULL || progname[0] == 0)
DRCe5eaf372014-05-09 18:00:32 +0000409 progname = "jpegtran"; /* in case C library doesn't provide it */
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000410
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);
417
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000418 /* Scan command line to find file names.
419 * It is convenient to use just one switch-parsing routine, but the switch
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000420 * values read here are mostly ignored; we will rescan the switches after
421 * opening the input file. Also note that most of the switches affect the
422 * destination JPEG object, so we parse into that and then copy over what
423 * needs to affects the source too.
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000424 */
425
426 file_index = parse_switches(&dstinfo, argc, argv, 0, FALSE);
427 jsrcerr.trace_level = jdsterr.trace_level;
Thomas G. Lane489583f1996-02-07 00:00:00 +0000428 srcinfo.mem->max_memory_to_use = dstinfo.mem->max_memory_to_use;
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000429
430#ifdef TWO_FILE_COMMANDLINE
431 /* Must have either -outfile switch or explicit output file name */
432 if (outfilename == NULL) {
Leon Scroggins III3993b372018-07-16 10:43:45 -0400433 if (file_index != argc - 2) {
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000434 fprintf(stderr, "%s: must name one input and one output file\n",
DRCe5eaf372014-05-09 18:00:32 +0000435 progname);
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000436 usage();
437 }
Leon Scroggins III3993b372018-07-16 10:43:45 -0400438 outfilename = argv[file_index + 1];
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000439 } else {
Leon Scroggins III3993b372018-07-16 10:43:45 -0400440 if (file_index != argc - 1) {
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000441 fprintf(stderr, "%s: must name one input and one output file\n",
DRCe5eaf372014-05-09 18:00:32 +0000442 progname);
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000443 usage();
444 }
445 }
446#else
447 /* Unix style: expect zero or one file name */
Leon Scroggins III3993b372018-07-16 10:43:45 -0400448 if (file_index < argc - 1) {
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000449 fprintf(stderr, "%s: only one input file\n", progname);
450 usage();
451 }
452#endif /* TWO_FILE_COMMANDLINE */
453
454 /* Open the input file. */
455 if (file_index < argc) {
Guido Vollbeding5996a252009-06-27 00:00:00 +0000456 if ((fp = fopen(argv[file_index], READ_BINARY)) == NULL) {
Leon Scroggins III3993b372018-07-16 10:43:45 -0400457 fprintf(stderr, "%s: can't open %s for reading\n", progname,
458 argv[file_index]);
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000459 exit(EXIT_FAILURE);
460 }
461 } else {
462 /* default input file is stdin */
Guido Vollbeding5996a252009-06-27 00:00:00 +0000463 fp = read_stdin();
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000464 }
465
Leon Scroggins III3993b372018-07-16 10:43:45 -0400466 if (icc_filename != NULL) {
467 if ((icc_file = fopen(icc_filename, READ_BINARY)) == NULL) {
468 fprintf(stderr, "%s: can't open %s\n", progname, icc_filename);
469 exit(EXIT_FAILURE);
470 }
471 if (fseek(icc_file, 0, SEEK_END) < 0 ||
472 (icc_len = ftell(icc_file)) < 1 ||
473 fseek(icc_file, 0, SEEK_SET) < 0) {
474 fprintf(stderr, "%s: can't determine size of %s\n", progname,
475 icc_filename);
476 exit(EXIT_FAILURE);
477 }
478 if ((icc_profile = (JOCTET *)malloc(icc_len)) == NULL) {
479 fprintf(stderr, "%s: can't allocate memory for ICC profile\n", progname);
480 fclose(icc_file);
481 exit(EXIT_FAILURE);
482 }
483 if (fread(icc_profile, icc_len, 1, icc_file) < 1) {
484 fprintf(stderr, "%s: can't read ICC profile from %s\n", progname,
485 icc_filename);
486 free(icc_profile);
487 fclose(icc_file);
488 exit(EXIT_FAILURE);
489 }
490 fclose(icc_file);
491 if (copyoption == JCOPYOPT_ALL)
492 copyoption = JCOPYOPT_ALL_EXCEPT_ICC;
493 }
494
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000495#ifdef PROGRESS_REPORT
Leon Scroggins III3993b372018-07-16 10:43:45 -0400496 start_progress_monitor((j_common_ptr)&dstinfo, &progress);
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000497#endif
498
499 /* Specify data source for decompression */
Guido Vollbeding5996a252009-06-27 00:00:00 +0000500 jpeg_stdio_src(&srcinfo, fp);
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000501
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000502 /* Enable saving of extra markers that we want to copy */
503 jcopy_markers_setup(&srcinfo, copyoption);
504
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000505 /* Read file header */
Leon Scroggins III3993b372018-07-16 10:43:45 -0400506 (void)jpeg_read_header(&srcinfo, TRUE);
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000507
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000508 /* Any space needed by a transform option must be requested before
509 * jpeg_read_coefficients so that memory allocation will be done right.
510 */
511#if TRANSFORMS_SUPPORTED
Guido Vollbeding989630f2010-01-10 00:00:00 +0000512 /* Fail right away if -perfect is given and transformation is not perfect.
Guido Vollbeding5996a252009-06-27 00:00:00 +0000513 */
Guido Vollbeding989630f2010-01-10 00:00:00 +0000514 if (!jtransform_request_workspace(&srcinfo, &transformoption)) {
Guido Vollbeding5996a252009-06-27 00:00:00 +0000515 fprintf(stderr, "%s: transformation is not perfect\n", progname);
516 exit(EXIT_FAILURE);
517 }
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000518#endif
519
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000520 /* Read source file as DCT coefficients */
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000521 src_coef_arrays = jpeg_read_coefficients(&srcinfo);
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000522
523 /* Initialize destination compression parameters from source values */
524 jpeg_copy_critical_parameters(&srcinfo, &dstinfo);
525
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000526 /* Adjust destination parameters if required by transform options;
527 * also find out which set of coefficient arrays will hold the output.
528 */
529#if TRANSFORMS_SUPPORTED
530 dst_coef_arrays = jtransform_adjust_parameters(&srcinfo, &dstinfo,
DRCe5eaf372014-05-09 18:00:32 +0000531 src_coef_arrays,
532 &transformoption);
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000533#else
534 dst_coef_arrays = src_coef_arrays;
535#endif
536
Guido Vollbeding5996a252009-06-27 00:00:00 +0000537 /* Close input file, if we opened it.
538 * Note: we assume that jpeg_read_coefficients consumed all input
539 * until JPEG_REACHED_EOI, and that jpeg_finish_decompress will
Leon Scroggins III3993b372018-07-16 10:43:45 -0400540 * only consume more while (!cinfo->inputctl->eoi_reached).
Guido Vollbeding5996a252009-06-27 00:00:00 +0000541 * We cannot call jpeg_finish_decompress here since we still need the
542 * virtual arrays allocated from the source object for processing.
543 */
544 if (fp != stdin)
545 fclose(fp);
546
547 /* Open the output file. */
548 if (outfilename != NULL) {
549 if ((fp = fopen(outfilename, WRITE_BINARY)) == NULL) {
Leon Scroggins III3993b372018-07-16 10:43:45 -0400550 fprintf(stderr, "%s: can't open %s for writing\n", progname,
551 outfilename);
Guido Vollbeding5996a252009-06-27 00:00:00 +0000552 exit(EXIT_FAILURE);
553 }
554 } else {
555 /* default output file is stdout */
556 fp = write_stdout();
557 }
558
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000559 /* Adjust default compression parameters by re-parsing the options */
560 file_index = parse_switches(&dstinfo, argc, argv, 0, TRUE);
561
562 /* Specify data destination for compression */
Guido Vollbeding5996a252009-06-27 00:00:00 +0000563 jpeg_stdio_dest(&dstinfo, fp);
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000564
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000565 /* Start compressor (note no image data is actually written here) */
566 jpeg_write_coefficients(&dstinfo, dst_coef_arrays);
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000567
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000568 /* Copy to the output file any extra markers that we want to preserve */
569 jcopy_markers_execute(&srcinfo, &dstinfo, copyoption);
570
Leon Scroggins III3993b372018-07-16 10:43:45 -0400571 if (icc_profile != NULL)
572 jpeg_write_icc_profile(&dstinfo, icc_profile, (unsigned int)icc_len);
573
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000574 /* Execute image transformation, if any */
575#if TRANSFORMS_SUPPORTED
Leon Scroggins III3993b372018-07-16 10:43:45 -0400576 jtransform_execute_transformation(&srcinfo, &dstinfo, src_coef_arrays,
DRCe5eaf372014-05-09 18:00:32 +0000577 &transformoption);
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000578#endif
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000579
580 /* Finish compression and release memory */
581 jpeg_finish_compress(&dstinfo);
582 jpeg_destroy_compress(&dstinfo);
Leon Scroggins III3993b372018-07-16 10:43:45 -0400583 (void)jpeg_finish_decompress(&srcinfo);
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000584 jpeg_destroy_decompress(&srcinfo);
585
Guido Vollbeding5996a252009-06-27 00:00:00 +0000586 /* Close output file, if we opened it */
587 if (fp != stdout)
588 fclose(fp);
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000589
590#ifdef PROGRESS_REPORT
Leon Scroggins III3993b372018-07-16 10:43:45 -0400591 end_progress_monitor((j_common_ptr)&dstinfo);
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000592#endif
593
Leon Scroggins III3993b372018-07-16 10:43:45 -0400594 if (icc_profile != NULL)
595 free(icc_profile);
596
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000597 /* All done. */
Leon Scroggins III3993b372018-07-16 10:43:45 -0400598 exit(jsrcerr.num_warnings + jdsterr.num_warnings ?
599 EXIT_WARNING : EXIT_SUCCESS);
DRCe5eaf372014-05-09 18:00:32 +0000600 return 0; /* suppress no-return-value warnings */
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000601}