DHCPv6-PD listen to WAN RA as per RFC 3633. Otherwise no default route set
[tomato.git] / release / src / router / jpeg / jpegtran.c
blob44c061a84b373e51301bc984425b103ccec7492a
1 /*
2 * jpegtran.c
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 */
19 #ifdef __MWERKS__
20 #include <SIOUX.h> /* Metrowerks needs this */
21 #include <console.h> /* ... and this */
22 #endif
23 #ifdef THINK_C
24 #include <console.h> /* Think declares it here */
25 #endif
26 #endif
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 */
44 LOCAL(void)
45 usage (void)
46 /* complain about bad command line */
48 fprintf(stderr, "usage: %s [switches] ", progname);
49 #ifdef TWO_FILE_COMMANDLINE
50 fprintf(stderr, "inputfile outputfile\n");
51 #else
52 fprintf(stderr, "[inputfile]\n");
53 #endif
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");
61 #endif
62 #ifdef C_PROGRESSIVE_SUPPORTED
63 fprintf(stderr, " -progressive Create progressive JPEG file\n");
64 #endif
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");
84 #endif
85 #ifdef C_MULTISCAN_FILES_SUPPORTED
86 fprintf(stderr, " -scans file Create multi-scan JPEG per script file\n");
87 #endif
88 exit(EXIT_FAILURE);
92 LOCAL(void)
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;
102 } else {
103 fprintf(stderr, "%s: can only do one image transformation at a time\n",
104 progname);
105 usage();
107 #else
108 fprintf(stderr, "%s: sorry, image transformation was not compiled\n",
109 progname);
110 exit(EXIT_FAILURE);
111 #endif
115 LOCAL(int)
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
124 * processing.
127 int argn;
128 char * arg;
129 boolean simple_progressive;
130 char * scansarg = NULL; /* saves -scans parm if any */
132 /* Set up default JPEG parameters. */
133 simple_progressive = FALSE;
134 outfilename = NULL;
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++) {
146 arg = argv[argn];
147 if (*arg != '-') {
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;
161 #else
162 fprintf(stderr, "%s: sorry, arithmetic coding not supported\n",
163 progname);
164 exit(EXIT_FAILURE);
165 #endif
167 } else if (keymatch(arg, "copy", 2)) {
168 /* Select which extra markers to copy. */
169 if (++argn >= argc) /* advance to next argument */
170 usage();
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;
177 } else
178 usage();
180 } else if (keymatch(arg, "crop", 2)) {
181 /* Perform lossless cropping. */
182 #if TRANSFORMS_SUPPORTED
183 if (++argn >= argc) /* advance to next argument */
184 usage();
185 if (! jtransform_parse_crop_spec(&transformoption, argv[argn])) {
186 fprintf(stderr, "%s: bogus -crop argument '%s'\n",
187 progname, argv[argn]);
188 exit(EXIT_FAILURE);
190 #else
191 select_transform(JXFORM_NONE); /* force an error */
192 #endif
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 */
209 usage();
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);
214 else
215 usage();
217 } else if (keymatch(arg, "grayscale", 1) || keymatch(arg, "greyscale",1)) {
218 /* Force to grayscale. */
219 #if TRANSFORMS_SUPPORTED
220 transformoption.force_grayscale = TRUE;
221 #else
222 select_transform(JXFORM_NONE); /* force an error */
223 #endif
225 } else if (keymatch(arg, "maxmemory", 3)) {
226 /* Maximum memory in Kb (or Mb with 'm'). */
227 long lval;
228 char ch = 'x';
230 if (++argn >= argc) /* advance to next argument */
231 usage();
232 if (sscanf(argv[argn], "%ld%c", &lval, &ch) < 1)
233 usage();
234 if (ch == 'm' || ch == 'M')
235 lval *= 1000L;
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;
242 #else
243 fprintf(stderr, "%s: sorry, entropy optimization was not compiled\n",
244 progname);
245 exit(EXIT_FAILURE);
246 #endif
248 } else if (keymatch(arg, "outfile", 4)) {
249 /* Set output file name. */
250 if (++argn >= argc) /* advance to next argument */
251 usage();
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
256 * handle. */
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. */
264 #else
265 fprintf(stderr, "%s: sorry, progressive output was not compiled\n",
266 progname);
267 exit(EXIT_FAILURE);
268 #endif
270 } else if (keymatch(arg, "restart", 1)) {
271 /* Restart interval in MCU rows (or in MCUs with 'b'). */
272 long lval;
273 char ch = 'x';
275 if (++argn >= argc) /* advance to next argument */
276 usage();
277 if (sscanf(argv[argn], "%ld%c", &lval, &ch) < 1)
278 usage();
279 if (lval < 0 || lval > 65535L)
280 usage();
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 */
284 } else {
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 */
292 usage();
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);
299 else
300 usage();
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 */
306 usage();
307 scansarg = argv[argn];
308 /* We must postpone reading the file in case -progressive appears. */
309 #else
310 fprintf(stderr, "%s: sorry, multi-scan output was not compiled\n",
311 progname);
312 exit(EXIT_FAILURE);
313 #endif
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;
327 } else {
328 usage(); /* bogus switch */
332 /* Post-switch-scanning cleanup */
334 if (for_real) {
336 #ifdef C_PROGRESSIVE_SUPPORTED
337 if (simple_progressive) /* process -progressive; -scans can override */
338 jpeg_simple_progression(cinfo);
339 #endif
341 #ifdef C_MULTISCAN_FILES_SUPPORTED
342 if (scansarg != NULL) /* process -scans if it was present */
343 if (! read_scan_script(cinfo, scansarg))
344 usage();
345 #endif
348 return argn; /* return index of next arg (file name) */
353 * The main program.
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;
364 #endif
365 jvirt_barray_ptr * src_coef_arrays;
366 jvirt_barray_ptr * dst_coef_arrays;
367 int file_index;
368 /* We assume all-in-memory processing and can therefore use only a
369 * single file pointer for sequential input and output operation.
371 FILE * fp;
373 /* On Mac, fetch a command line. */
374 #ifdef USE_CCOMMAND
375 argc = ccommand(&argv);
376 #endif
378 progname = argv[0];
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);
394 #endif
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",
413 progname);
414 usage();
416 outfilename = argv[file_index+1];
417 } else {
418 if (file_index != argc-1) {
419 fprintf(stderr, "%s: must name one input and one output file\n",
420 progname);
421 usage();
424 #else
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);
428 usage();
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]);
436 exit(EXIT_FAILURE);
438 } else {
439 /* default input file is stdin */
440 fp = read_stdin();
443 #ifdef PROGRESS_REPORT
444 start_progress_monitor((j_common_ptr) &dstinfo, &progress);
445 #endif
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);
467 exit(EXIT_FAILURE);
469 jtransform_request_workspace(&srcinfo, &transformoption);
470 #endif
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,
483 src_coef_arrays,
484 &transformoption);
485 #else
486 dst_coef_arrays = src_coef_arrays;
487 #endif
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.
496 if (fp != stdin)
497 fclose(fp);
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);
503 exit(EXIT_FAILURE);
505 } else {
506 /* default output file is stdout */
507 fp = write_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,
525 src_coef_arrays,
526 &transformoption);
527 #endif
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 */
536 if (fp != stdout)
537 fclose(fp);
539 #ifdef PROGRESS_REPORT
540 end_progress_monitor((j_common_ptr) &dstinfo);
541 #endif
543 /* All done. */
544 exit(jsrcerr.num_warnings + jdsterr.num_warnings ?EXIT_WARNING:EXIT_SUCCESS);
545 return 0; /* suppress no-return-value warnings */