blob: 6b40de39b7f1335f9aa621f2de290d4f7e57dfac [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:
DRC2cdd2ae2010-10-10 06:54:21 +00007 * Copyright (C) 2010, D. R. Commander.
Thomas G. Lanebc79e061995-08-02 00:00:00 +00008 * For conditions of distribution and use, see the accompanying README file.
9 *
10 * This file contains a command-line user interface for JPEG transcoding.
Guido Vollbeding989630f2010-01-10 00:00:00 +000011 * It is very similar to cjpeg.c, and partly to djpeg.c, but provides
12 * lossless transcoding between different JPEG file formats. It also
13 * provides some lossless and sort-of-lossless transformations of JPEG data.
Thomas G. Lanebc79e061995-08-02 00:00:00 +000014 */
15
DRCe5eaf372014-05-09 18:00:32 +000016#include "cdjpeg.h" /* Common decls for cjpeg/djpeg applications */
17#include "transupp.h" /* Support routines for jpegtran */
18#include "jversion.h" /* for version message */
DRCff6961f2014-04-20 09:17:11 +000019#include "jconfigint.h"
Thomas G. Lanebc79e061995-08-02 00:00:00 +000020
DRCe5eaf372014-05-09 18:00:32 +000021#ifdef USE_CCOMMAND /* command-line reader for Macintosh */
Thomas G. Lanebc79e061995-08-02 00:00:00 +000022#ifdef __MWERKS__
Thomas G. Lane489583f1996-02-07 00:00:00 +000023#include <SIOUX.h> /* Metrowerks needs this */
DRCe5eaf372014-05-09 18:00:32 +000024#include <console.h> /* ... and this */
Thomas G. Lanebc79e061995-08-02 00:00:00 +000025#endif
26#ifdef THINK_C
DRCe5eaf372014-05-09 18:00:32 +000027#include <console.h> /* Think declares it here */
Thomas G. Lanebc79e061995-08-02 00:00:00 +000028#endif
29#endif
30
31
32/*
33 * Argument-parsing code.
34 * The switch parser is designed to be useful with DOS-style command line
35 * syntax, ie, intermixed switches and file names, where only the switches
36 * to the left of a given file name affect processing of that file.
37 * The main program in this file doesn't actually use this capability...
38 */
39
40
DRCe5eaf372014-05-09 18:00:32 +000041static const char * progname; /* program name for error messages */
42static char * outfilename; /* for -outfile switch */
43static JCOPY_OPTION copyoption; /* -copy switch */
Thomas G. Lane5ead57a1998-03-27 00:00:00 +000044static jpeg_transform_info transformoption; /* image transformation options */
Thomas G. Lanebc79e061995-08-02 00:00:00 +000045
46
Thomas G. Lane489583f1996-02-07 00:00:00 +000047LOCAL(void)
Thomas G. Lanebc79e061995-08-02 00:00:00 +000048usage (void)
49/* complain about bad command line */
50{
51 fprintf(stderr, "usage: %s [switches] ", progname);
52#ifdef TWO_FILE_COMMANDLINE
53 fprintf(stderr, "inputfile outputfile\n");
54#else
55 fprintf(stderr, "[inputfile]\n");
56#endif
57
58 fprintf(stderr, "Switches (names may be abbreviated):\n");
Thomas G. Lane5ead57a1998-03-27 00:00:00 +000059 fprintf(stderr, " -copy none Copy no extra markers from source file\n");
60 fprintf(stderr, " -copy comments Copy only comment markers (default)\n");
61 fprintf(stderr, " -copy all Copy all extra markers\n");
Thomas G. Lanebc79e061995-08-02 00:00:00 +000062#ifdef ENTROPY_OPT_SUPPORTED
63 fprintf(stderr, " -optimize Optimize Huffman table (smaller file, but slow compression)\n");
64#endif
65#ifdef C_PROGRESSIVE_SUPPORTED
66 fprintf(stderr, " -progressive Create progressive JPEG file\n");
67#endif
Thomas G. Lane5ead57a1998-03-27 00:00:00 +000068 fprintf(stderr, "Switches for modifying the image:\n");
Guido Vollbedingf18f81b2010-02-28 00:00:00 +000069#if TRANSFORMS_SUPPORTED
Guido Vollbeding5996a252009-06-27 00:00:00 +000070 fprintf(stderr, " -crop WxH+X+Y Crop to a rectangular subarea\n");
Thomas G. Lane5ead57a1998-03-27 00:00:00 +000071 fprintf(stderr, " -grayscale Reduce to grayscale (omit color data)\n");
72 fprintf(stderr, " -flip [horizontal|vertical] Mirror image (left-right or top-bottom)\n");
Guido Vollbeding5996a252009-06-27 00:00:00 +000073 fprintf(stderr, " -perfect Fail if there is non-transformable edge blocks\n");
Thomas G. Lane5ead57a1998-03-27 00:00:00 +000074 fprintf(stderr, " -rotate [90|180|270] Rotate image (degrees clockwise)\n");
Guido Vollbedingf18f81b2010-02-28 00:00:00 +000075#endif
Guido Vollbedingf18f81b2010-02-28 00:00:00 +000076#if TRANSFORMS_SUPPORTED
Thomas G. Lane5ead57a1998-03-27 00:00:00 +000077 fprintf(stderr, " -transpose Transpose image\n");
78 fprintf(stderr, " -transverse Transverse transpose image\n");
79 fprintf(stderr, " -trim Drop non-transformable edge blocks\n");
Guido Vollbedingf18f81b2010-02-28 00:00:00 +000080#endif
Thomas G. Lanebc79e061995-08-02 00:00:00 +000081 fprintf(stderr, "Switches for advanced users:\n");
DRCd657ba62012-01-27 09:41:20 +000082#ifdef C_ARITH_CODING_SUPPORTED
83 fprintf(stderr, " -arithmetic Use arithmetic coding\n");
84#endif
Thomas G. Lanebc79e061995-08-02 00:00:00 +000085 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");
Thomas G. Lanebc79e061995-08-02 00:00:00 +000090#ifdef C_MULTISCAN_FILES_SUPPORTED
91 fprintf(stderr, " -scans file Create multi-scan JPEG per script file\n");
92#endif
93 exit(EXIT_FAILURE);
94}
95
96
Thomas G. Lane5ead57a1998-03-27 00:00:00 +000097LOCAL(void)
98select_transform (JXFORM_CODE transform)
99/* Silly little routine to detect multiple transform options,
100 * which we can't handle.
101 */
102{
103#if TRANSFORMS_SUPPORTED
104 if (transformoption.transform == JXFORM_NONE ||
105 transformoption.transform == transform) {
106 transformoption.transform = transform;
107 } else {
108 fprintf(stderr, "%s: can only do one image transformation at a time\n",
DRCe5eaf372014-05-09 18:00:32 +0000109 progname);
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000110 usage();
111 }
112#else
113 fprintf(stderr, "%s: sorry, image transformation was not compiled\n",
DRCe5eaf372014-05-09 18:00:32 +0000114 progname);
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000115 exit(EXIT_FAILURE);
116#endif
117}
118
119
Thomas G. Lane489583f1996-02-07 00:00:00 +0000120LOCAL(int)
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000121parse_switches (j_compress_ptr cinfo, int argc, char **argv,
DRCe5eaf372014-05-09 18:00:32 +0000122 int last_file_arg_seen, boolean for_real)
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000123/* 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
129 * processing.
130 */
131{
132 int argn;
133 char * arg;
134 boolean simple_progressive;
DRCe5eaf372014-05-09 18:00:32 +0000135 char * scansarg = NULL; /* saves -scans parm if any */
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000136
137 /* Set up default JPEG parameters. */
138 simple_progressive = FALSE;
139 outfilename = NULL;
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000140 copyoption = JCOPYOPT_DEFAULT;
141 transformoption.transform = JXFORM_NONE;
Guido Vollbeding5996a252009-06-27 00:00:00 +0000142 transformoption.perfect = FALSE;
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000143 transformoption.trim = FALSE;
144 transformoption.force_grayscale = FALSE;
Guido Vollbeding5996a252009-06-27 00:00:00 +0000145 transformoption.crop = FALSE;
DRCba5ea512011-03-04 03:20:34 +0000146 transformoption.slow_hflip = FALSE;
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000147 cinfo->err->trace_level = 0;
148
149 /* Scan command line options, adjust parameters */
150
151 for (argn = 1; argn < argc; argn++) {
152 arg = argv[argn];
153 if (*arg != '-') {
154 /* Not a switch, must be a file name argument */
155 if (argn <= last_file_arg_seen) {
DRCe5eaf372014-05-09 18:00:32 +0000156 outfilename = NULL; /* -outfile applies to just one input file */
157 continue; /* ignore this name if previously processed */
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000158 }
DRCe5eaf372014-05-09 18:00:32 +0000159 break; /* else done parsing switches */
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000160 }
DRCe5eaf372014-05-09 18:00:32 +0000161 arg++; /* advance past switch marker character */
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000162
163 if (keymatch(arg, "arithmetic", 1)) {
164 /* Use arithmetic coding. */
165#ifdef C_ARITH_CODING_SUPPORTED
166 cinfo->arith_code = TRUE;
167#else
168 fprintf(stderr, "%s: sorry, arithmetic coding not supported\n",
DRCe5eaf372014-05-09 18:00:32 +0000169 progname);
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000170 exit(EXIT_FAILURE);
171#endif
172
Guido Vollbeding5996a252009-06-27 00:00:00 +0000173 } else if (keymatch(arg, "copy", 2)) {
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000174 /* Select which extra markers to copy. */
DRCe5eaf372014-05-09 18:00:32 +0000175 if (++argn >= argc) /* advance to next argument */
176 usage();
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000177 if (keymatch(argv[argn], "none", 1)) {
DRCe5eaf372014-05-09 18:00:32 +0000178 copyoption = JCOPYOPT_NONE;
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000179 } else if (keymatch(argv[argn], "comments", 1)) {
DRCe5eaf372014-05-09 18:00:32 +0000180 copyoption = JCOPYOPT_COMMENTS;
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000181 } else if (keymatch(argv[argn], "all", 1)) {
DRCe5eaf372014-05-09 18:00:32 +0000182 copyoption = JCOPYOPT_ALL;
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000183 } else
DRCe5eaf372014-05-09 18:00:32 +0000184 usage();
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000185
Guido Vollbeding5996a252009-06-27 00:00:00 +0000186 } else if (keymatch(arg, "crop", 2)) {
187 /* Perform lossless cropping. */
188#if TRANSFORMS_SUPPORTED
DRCe5eaf372014-05-09 18:00:32 +0000189 if (++argn >= argc) /* advance to next argument */
190 usage();
Guido Vollbeding5996a252009-06-27 00:00:00 +0000191 if (! jtransform_parse_crop_spec(&transformoption, argv[argn])) {
DRCe5eaf372014-05-09 18:00:32 +0000192 fprintf(stderr, "%s: bogus -crop argument '%s'\n",
193 progname, argv[argn]);
194 exit(EXIT_FAILURE);
Guido Vollbeding5996a252009-06-27 00:00:00 +0000195 }
196#else
DRCe5eaf372014-05-09 18:00:32 +0000197 select_transform(JXFORM_NONE); /* force an error */
Guido Vollbeding5996a252009-06-27 00:00:00 +0000198#endif
199
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000200 } else if (keymatch(arg, "debug", 1) || keymatch(arg, "verbose", 1)) {
201 /* Enable debug printouts. */
202 /* On first -d, print version identification */
203 static boolean printed_version = FALSE;
204
205 if (! printed_version) {
DRCe5eaf372014-05-09 18:00:32 +0000206 fprintf(stderr, "%s version %s (build %s)\n",
207 PACKAGE_NAME, VERSION, BUILD);
208 fprintf(stderr, "%s\n\n", JCOPYRIGHT);
209 fprintf(stderr, "Emulating The Independent JPEG Group's software, version %s\n\n",
210 JVERSION);
211 printed_version = TRUE;
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000212 }
213 cinfo->err->trace_level++;
214
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000215 } else if (keymatch(arg, "flip", 1)) {
216 /* Mirror left-right or top-bottom. */
DRCe5eaf372014-05-09 18:00:32 +0000217 if (++argn >= argc) /* advance to next argument */
218 usage();
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000219 if (keymatch(argv[argn], "horizontal", 1))
DRCe5eaf372014-05-09 18:00:32 +0000220 select_transform(JXFORM_FLIP_H);
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000221 else if (keymatch(argv[argn], "vertical", 1))
DRCe5eaf372014-05-09 18:00:32 +0000222 select_transform(JXFORM_FLIP_V);
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000223 else
DRCe5eaf372014-05-09 18:00:32 +0000224 usage();
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000225
226 } else if (keymatch(arg, "grayscale", 1) || keymatch(arg, "greyscale",1)) {
227 /* Force to grayscale. */
228#if TRANSFORMS_SUPPORTED
229 transformoption.force_grayscale = TRUE;
230#else
DRCe5eaf372014-05-09 18:00:32 +0000231 select_transform(JXFORM_NONE); /* force an error */
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000232#endif
233
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000234 } else if (keymatch(arg, "maxmemory", 3)) {
235 /* Maximum memory in Kb (or Mb with 'm'). */
236 long lval;
237 char ch = 'x';
238
DRCe5eaf372014-05-09 18:00:32 +0000239 if (++argn >= argc) /* advance to next argument */
240 usage();
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000241 if (sscanf(argv[argn], "%ld%c", &lval, &ch) < 1)
DRCe5eaf372014-05-09 18:00:32 +0000242 usage();
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000243 if (ch == 'm' || ch == 'M')
DRCe5eaf372014-05-09 18:00:32 +0000244 lval *= 1000L;
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000245 cinfo->mem->max_memory_to_use = lval * 1000L;
246
247 } else if (keymatch(arg, "optimize", 1) || keymatch(arg, "optimise", 1)) {
248 /* Enable entropy parm optimization. */
249#ifdef ENTROPY_OPT_SUPPORTED
250 cinfo->optimize_coding = TRUE;
251#else
252 fprintf(stderr, "%s: sorry, entropy optimization was not compiled\n",
DRCe5eaf372014-05-09 18:00:32 +0000253 progname);
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000254 exit(EXIT_FAILURE);
255#endif
256
257 } else if (keymatch(arg, "outfile", 4)) {
258 /* Set output file name. */
DRCe5eaf372014-05-09 18:00:32 +0000259 if (++argn >= argc) /* advance to next argument */
260 usage();
261 outfilename = argv[argn]; /* save it away for later use */
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000262
Guido Vollbeding5996a252009-06-27 00:00:00 +0000263 } else if (keymatch(arg, "perfect", 2)) {
264 /* Fail if there is any partial edge MCUs that the transform can't
265 * handle. */
266 transformoption.perfect = TRUE;
267
268 } else if (keymatch(arg, "progressive", 2)) {
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000269 /* Select simple progressive mode. */
270#ifdef C_PROGRESSIVE_SUPPORTED
271 simple_progressive = TRUE;
272 /* We must postpone execution until num_components is known. */
273#else
274 fprintf(stderr, "%s: sorry, progressive output was not compiled\n",
DRCe5eaf372014-05-09 18:00:32 +0000275 progname);
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000276 exit(EXIT_FAILURE);
277#endif
278
279 } else if (keymatch(arg, "restart", 1)) {
280 /* Restart interval in MCU rows (or in MCUs with 'b'). */
281 long lval;
282 char ch = 'x';
283
DRCe5eaf372014-05-09 18:00:32 +0000284 if (++argn >= argc) /* advance to next argument */
285 usage();
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000286 if (sscanf(argv[argn], "%ld%c", &lval, &ch) < 1)
DRCe5eaf372014-05-09 18:00:32 +0000287 usage();
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000288 if (lval < 0 || lval > 65535L)
DRCe5eaf372014-05-09 18:00:32 +0000289 usage();
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000290 if (ch == 'b' || ch == 'B') {
DRCe5eaf372014-05-09 18:00:32 +0000291 cinfo->restart_interval = (unsigned int) lval;
292 cinfo->restart_in_rows = 0; /* else prior '-restart n' overrides me */
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000293 } else {
DRCe5eaf372014-05-09 18:00:32 +0000294 cinfo->restart_in_rows = (int) lval;
295 /* restart_interval will be computed during startup */
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000296 }
297
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000298 } else if (keymatch(arg, "rotate", 2)) {
299 /* Rotate 90, 180, or 270 degrees (measured clockwise). */
DRCe5eaf372014-05-09 18:00:32 +0000300 if (++argn >= argc) /* advance to next argument */
301 usage();
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000302 if (keymatch(argv[argn], "90", 2))
DRCe5eaf372014-05-09 18:00:32 +0000303 select_transform(JXFORM_ROT_90);
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000304 else if (keymatch(argv[argn], "180", 3))
DRCe5eaf372014-05-09 18:00:32 +0000305 select_transform(JXFORM_ROT_180);
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000306 else if (keymatch(argv[argn], "270", 3))
DRCe5eaf372014-05-09 18:00:32 +0000307 select_transform(JXFORM_ROT_270);
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000308 else
DRCe5eaf372014-05-09 18:00:32 +0000309 usage();
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000310
311 } else if (keymatch(arg, "scans", 1)) {
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000312 /* Set scan script. */
313#ifdef C_MULTISCAN_FILES_SUPPORTED
DRCe5eaf372014-05-09 18:00:32 +0000314 if (++argn >= argc) /* advance to next argument */
315 usage();
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000316 scansarg = argv[argn];
317 /* We must postpone reading the file in case -progressive appears. */
318#else
319 fprintf(stderr, "%s: sorry, multi-scan output was not compiled\n",
DRCe5eaf372014-05-09 18:00:32 +0000320 progname);
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000321 exit(EXIT_FAILURE);
322#endif
323
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000324 } else if (keymatch(arg, "transpose", 1)) {
325 /* Transpose (across UL-to-LR axis). */
326 select_transform(JXFORM_TRANSPOSE);
327
328 } else if (keymatch(arg, "transverse", 6)) {
329 /* Transverse transpose (across UR-to-LL axis). */
330 select_transform(JXFORM_TRANSVERSE);
331
332 } else if (keymatch(arg, "trim", 3)) {
333 /* Trim off any partial edge MCUs that the transform can't handle. */
334 transformoption.trim = TRUE;
335
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000336 } else {
DRCe5eaf372014-05-09 18:00:32 +0000337 usage(); /* bogus switch */
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000338 }
339 }
340
341 /* Post-switch-scanning cleanup */
342
343 if (for_real) {
344
345#ifdef C_PROGRESSIVE_SUPPORTED
DRCe5eaf372014-05-09 18:00:32 +0000346 if (simple_progressive) /* process -progressive; -scans can override */
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000347 jpeg_simple_progression(cinfo);
348#endif
349
350#ifdef C_MULTISCAN_FILES_SUPPORTED
DRCe5eaf372014-05-09 18:00:32 +0000351 if (scansarg != NULL) /* process -scans if it was present */
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000352 if (! read_scan_script(cinfo, scansarg))
DRCe5eaf372014-05-09 18:00:32 +0000353 usage();
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000354#endif
355 }
356
DRCe5eaf372014-05-09 18:00:32 +0000357 return argn; /* return index of next arg (file name) */
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000358}
359
360
361/*
362 * The main program.
363 */
364
Thomas G. Lane489583f1996-02-07 00:00:00 +0000365int
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000366main (int argc, char **argv)
367{
368 struct jpeg_decompress_struct srcinfo;
369 struct jpeg_compress_struct dstinfo;
370 struct jpeg_error_mgr jsrcerr, jdsterr;
371#ifdef PROGRESS_REPORT
372 struct cdjpeg_progress_mgr progress;
373#endif
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000374 jvirt_barray_ptr * src_coef_arrays;
375 jvirt_barray_ptr * dst_coef_arrays;
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000376 int file_index;
Guido Vollbeding5996a252009-06-27 00:00:00 +0000377 /* We assume all-in-memory processing and can therefore use only a
DRCff6961f2014-04-20 09:17:11 +0000378 * single file pointer for sequential input and output operation.
Guido Vollbeding5996a252009-06-27 00:00:00 +0000379 */
380 FILE * fp;
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000381
382 /* On Mac, fetch a command line. */
383#ifdef USE_CCOMMAND
384 argc = ccommand(&argv);
385#endif
386
387 progname = argv[0];
388 if (progname == NULL || progname[0] == 0)
DRCe5eaf372014-05-09 18:00:32 +0000389 progname = "jpegtran"; /* in case C library doesn't provide it */
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000390
391 /* Initialize the JPEG decompression object with default error handling. */
392 srcinfo.err = jpeg_std_error(&jsrcerr);
393 jpeg_create_decompress(&srcinfo);
394 /* Initialize the JPEG compression object with default error handling. */
395 dstinfo.err = jpeg_std_error(&jdsterr);
396 jpeg_create_compress(&dstinfo);
397
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000398 /* Scan command line to find file names.
399 * It is convenient to use just one switch-parsing routine, but the switch
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000400 * values read here are mostly ignored; we will rescan the switches after
401 * opening the input file. Also note that most of the switches affect the
402 * destination JPEG object, so we parse into that and then copy over what
403 * needs to affects the source too.
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000404 */
405
406 file_index = parse_switches(&dstinfo, argc, argv, 0, FALSE);
407 jsrcerr.trace_level = jdsterr.trace_level;
Thomas G. Lane489583f1996-02-07 00:00:00 +0000408 srcinfo.mem->max_memory_to_use = dstinfo.mem->max_memory_to_use;
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000409
410#ifdef TWO_FILE_COMMANDLINE
411 /* Must have either -outfile switch or explicit output file name */
412 if (outfilename == NULL) {
413 if (file_index != argc-2) {
414 fprintf(stderr, "%s: must name one input and one output file\n",
DRCe5eaf372014-05-09 18:00:32 +0000415 progname);
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000416 usage();
417 }
418 outfilename = argv[file_index+1];
419 } else {
420 if (file_index != argc-1) {
421 fprintf(stderr, "%s: must name one input and one output file\n",
DRCe5eaf372014-05-09 18:00:32 +0000422 progname);
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000423 usage();
424 }
425 }
426#else
427 /* Unix style: expect zero or one file name */
428 if (file_index < argc-1) {
429 fprintf(stderr, "%s: only one input file\n", progname);
430 usage();
431 }
432#endif /* TWO_FILE_COMMANDLINE */
433
434 /* Open the input file. */
435 if (file_index < argc) {
Guido Vollbeding5996a252009-06-27 00:00:00 +0000436 if ((fp = fopen(argv[file_index], READ_BINARY)) == NULL) {
437 fprintf(stderr, "%s: can't open %s for reading\n", progname, argv[file_index]);
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000438 exit(EXIT_FAILURE);
439 }
440 } else {
441 /* default input file is stdin */
Guido Vollbeding5996a252009-06-27 00:00:00 +0000442 fp = read_stdin();
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000443 }
444
445#ifdef PROGRESS_REPORT
446 start_progress_monitor((j_common_ptr) &dstinfo, &progress);
447#endif
448
449 /* Specify data source for decompression */
Guido Vollbeding5996a252009-06-27 00:00:00 +0000450 jpeg_stdio_src(&srcinfo, fp);
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000451
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000452 /* Enable saving of extra markers that we want to copy */
453 jcopy_markers_setup(&srcinfo, copyoption);
454
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000455 /* Read file header */
456 (void) jpeg_read_header(&srcinfo, TRUE);
457
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000458 /* Any space needed by a transform option must be requested before
459 * jpeg_read_coefficients so that memory allocation will be done right.
460 */
461#if TRANSFORMS_SUPPORTED
Guido Vollbeding989630f2010-01-10 00:00:00 +0000462 /* Fail right away if -perfect is given and transformation is not perfect.
Guido Vollbeding5996a252009-06-27 00:00:00 +0000463 */
Guido Vollbeding989630f2010-01-10 00:00:00 +0000464 if (!jtransform_request_workspace(&srcinfo, &transformoption)) {
Guido Vollbeding5996a252009-06-27 00:00:00 +0000465 fprintf(stderr, "%s: transformation is not perfect\n", progname);
466 exit(EXIT_FAILURE);
467 }
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000468#endif
469
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000470 /* Read source file as DCT coefficients */
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000471 src_coef_arrays = jpeg_read_coefficients(&srcinfo);
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000472
473 /* Initialize destination compression parameters from source values */
474 jpeg_copy_critical_parameters(&srcinfo, &dstinfo);
475
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000476 /* Adjust destination parameters if required by transform options;
477 * also find out which set of coefficient arrays will hold the output.
478 */
479#if TRANSFORMS_SUPPORTED
480 dst_coef_arrays = jtransform_adjust_parameters(&srcinfo, &dstinfo,
DRCe5eaf372014-05-09 18:00:32 +0000481 src_coef_arrays,
482 &transformoption);
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000483#else
484 dst_coef_arrays = src_coef_arrays;
485#endif
486
Guido Vollbeding5996a252009-06-27 00:00:00 +0000487 /* Close input file, if we opened it.
488 * Note: we assume that jpeg_read_coefficients consumed all input
489 * until JPEG_REACHED_EOI, and that jpeg_finish_decompress will
490 * only consume more while (! cinfo->inputctl->eoi_reached).
491 * We cannot call jpeg_finish_decompress here since we still need the
492 * virtual arrays allocated from the source object for processing.
493 */
494 if (fp != stdin)
495 fclose(fp);
496
497 /* Open the output file. */
498 if (outfilename != NULL) {
499 if ((fp = fopen(outfilename, WRITE_BINARY)) == NULL) {
500 fprintf(stderr, "%s: can't open %s for writing\n", progname, outfilename);
501 exit(EXIT_FAILURE);
502 }
503 } else {
504 /* default output file is stdout */
505 fp = write_stdout();
506 }
507
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000508 /* Adjust default compression parameters by re-parsing the options */
509 file_index = parse_switches(&dstinfo, argc, argv, 0, TRUE);
510
511 /* Specify data destination for compression */
Guido Vollbeding5996a252009-06-27 00:00:00 +0000512 jpeg_stdio_dest(&dstinfo, fp);
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000513
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000514 /* Start compressor (note no image data is actually written here) */
515 jpeg_write_coefficients(&dstinfo, dst_coef_arrays);
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000516
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000517 /* Copy to the output file any extra markers that we want to preserve */
518 jcopy_markers_execute(&srcinfo, &dstinfo, copyoption);
519
520 /* Execute image transformation, if any */
521#if TRANSFORMS_SUPPORTED
522 jtransform_execute_transformation(&srcinfo, &dstinfo,
DRCe5eaf372014-05-09 18:00:32 +0000523 src_coef_arrays,
524 &transformoption);
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000525#endif
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000526
527 /* Finish compression and release memory */
528 jpeg_finish_compress(&dstinfo);
529 jpeg_destroy_compress(&dstinfo);
530 (void) jpeg_finish_decompress(&srcinfo);
531 jpeg_destroy_decompress(&srcinfo);
532
Guido Vollbeding5996a252009-06-27 00:00:00 +0000533 /* Close output file, if we opened it */
534 if (fp != stdout)
535 fclose(fp);
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000536
537#ifdef PROGRESS_REPORT
538 end_progress_monitor((j_common_ptr) &dstinfo);
539#endif
540
541 /* All done. */
542 exit(jsrcerr.num_warnings + jdsterr.num_warnings ?EXIT_WARNING:EXIT_SUCCESS);
DRCe5eaf372014-05-09 18:00:32 +0000543 return 0; /* suppress no-return-value warnings */
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000544}