add place-holder directory for the a3000 wd533c93 scsi controller implementation.
[AROS.git] / workbench / libs / jpeg / jpegtran.c
blob5269344e1716fc06da44066b2be467478a077686
1 /*
2 * jpegtran.c
4 * Copyright (C) 1995-2013, Thomas G. Lane, Guido Vollbeding.
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, and partly to djpeg.c, but provides
10 * lossless transcoding between different JPEG file formats. It also
11 * provides some lossless and sort-of-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 char * scaleoption; /* -scale switch */
41 static JCOPY_OPTION copyoption; /* -copy switch */
42 static jpeg_transform_info transformoption; /* image transformation options */
45 LOCAL(void)
46 usage (void)
47 /* complain about bad command line */
49 fprintf(stderr, "usage: %s [switches] ", progname);
50 #ifdef TWO_FILE_COMMANDLINE
51 fprintf(stderr, "inputfile outputfile\n");
52 #else
53 fprintf(stderr, "[inputfile]\n");
54 #endif
56 fprintf(stderr, "Switches (names may be abbreviated):\n");
57 fprintf(stderr, " -copy none Copy no extra markers from source file\n");
58 fprintf(stderr, " -copy comments Copy only comment markers (default)\n");
59 fprintf(stderr, " -copy all Copy all extra markers\n");
60 #ifdef ENTROPY_OPT_SUPPORTED
61 fprintf(stderr, " -optimize Optimize Huffman table (smaller file, but slow compression)\n");
62 #endif
63 #ifdef C_PROGRESSIVE_SUPPORTED
64 fprintf(stderr, " -progressive Create progressive JPEG file\n");
65 #endif
66 fprintf(stderr, "Switches for modifying the image:\n");
67 #if TRANSFORMS_SUPPORTED
68 fprintf(stderr, " -crop WxH+X+Y Crop to a rectangular subarea\n");
69 fprintf(stderr, " -flip [horizontal|vertical] Mirror image (left-right or top-bottom)\n");
70 fprintf(stderr, " -grayscale Reduce to grayscale (omit color data)\n");
71 fprintf(stderr, " -perfect Fail if there is non-transformable edge blocks\n");
72 fprintf(stderr, " -rotate [90|180|270] Rotate image (degrees clockwise)\n");
73 #endif
74 fprintf(stderr, " -scale M/N Scale output image by fraction M/N, eg, 1/8\n");
75 #if TRANSFORMS_SUPPORTED
76 fprintf(stderr, " -transpose Transpose image\n");
77 fprintf(stderr, " -transverse Transverse transpose image\n");
78 fprintf(stderr, " -trim Drop non-transformable edge blocks\n");
79 fprintf(stderr, " -wipe WxH+X+Y Wipe (gray out) a rectangular subarea\n");
80 #endif
81 fprintf(stderr, "Switches for advanced users:\n");
82 #ifdef C_ARITH_CODING_SUPPORTED
83 fprintf(stderr, " -arithmetic Use arithmetic coding\n");
84 #endif
85 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");
90 #ifdef C_MULTISCAN_FILES_SUPPORTED
91 fprintf(stderr, " -scans file Create multi-scan JPEG per script file\n");
92 #endif
93 exit(EXIT_FAILURE);
97 LOCAL(void)
98 select_transform (JXFORM_CODE transform)
99 /* Silly little routine to detect multiple transform options,
100 * which we can't handle.
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",
109 progname);
110 usage();
112 #else
113 fprintf(stderr, "%s: sorry, image transformation was not compiled\n",
114 progname);
115 exit(EXIT_FAILURE);
116 #endif
120 LOCAL(int)
121 parse_switches (j_compress_ptr cinfo, int argc, char **argv,
122 int last_file_arg_seen, boolean for_real)
123 /* 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.
132 int argn;
133 char * arg;
134 boolean simple_progressive;
135 char * scansarg = NULL; /* saves -scans parm if any */
137 /* Set up default JPEG parameters. */
138 simple_progressive = FALSE;
139 outfilename = NULL;
140 scaleoption = NULL;
141 copyoption = JCOPYOPT_DEFAULT;
142 transformoption.transform = JXFORM_NONE;
143 transformoption.perfect = FALSE;
144 transformoption.trim = FALSE;
145 transformoption.force_grayscale = FALSE;
146 transformoption.crop = FALSE;
147 cinfo->err->trace_level = 0;
149 /* Scan command line options, adjust parameters */
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) {
156 outfilename = NULL; /* -outfile applies to just one input file */
157 continue; /* ignore this name if previously processed */
159 break; /* else done parsing switches */
161 arg++; /* advance past switch marker character */
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",
169 progname);
170 exit(EXIT_FAILURE);
171 #endif
173 } else if (keymatch(arg, "copy", 2)) {
174 /* Select which extra markers to copy. */
175 if (++argn >= argc) /* advance to next argument */
176 usage();
177 if (keymatch(argv[argn], "none", 1)) {
178 copyoption = JCOPYOPT_NONE;
179 } else if (keymatch(argv[argn], "comments", 1)) {
180 copyoption = JCOPYOPT_COMMENTS;
181 } else if (keymatch(argv[argn], "all", 1)) {
182 copyoption = JCOPYOPT_ALL;
183 } else
184 usage();
186 } else if (keymatch(arg, "crop", 2)) {
187 /* Perform lossless cropping. */
188 #if TRANSFORMS_SUPPORTED
189 if (++argn >= argc) /* advance to next argument */
190 usage();
191 if (transformoption.crop /* reject multiple crop/wipe requests */ ||
192 ! jtransform_parse_crop_spec(&transformoption, argv[argn])) {
193 fprintf(stderr, "%s: bogus -crop argument '%s'\n",
194 progname, argv[argn]);
195 exit(EXIT_FAILURE);
197 #else
198 select_transform(JXFORM_NONE); /* force an error */
199 #endif
201 } else if (keymatch(arg, "debug", 1) || keymatch(arg, "verbose", 1)) {
202 /* Enable debug printouts. */
203 /* On first -d, print version identification */
204 static boolean printed_version = FALSE;
206 if (! printed_version) {
207 fprintf(stderr, "Independent JPEG Group's JPEGTRAN, version %s\n%s\n",
208 JVERSION, JCOPYRIGHT);
209 printed_version = TRUE;
211 cinfo->err->trace_level++;
213 } else if (keymatch(arg, "flip", 1)) {
214 /* Mirror left-right or top-bottom. */
215 if (++argn >= argc) /* advance to next argument */
216 usage();
217 if (keymatch(argv[argn], "horizontal", 1))
218 select_transform(JXFORM_FLIP_H);
219 else if (keymatch(argv[argn], "vertical", 1))
220 select_transform(JXFORM_FLIP_V);
221 else
222 usage();
224 } else if (keymatch(arg, "grayscale", 1) || keymatch(arg, "greyscale",1)) {
225 /* Force to grayscale. */
226 #if TRANSFORMS_SUPPORTED
227 transformoption.force_grayscale = TRUE;
228 #else
229 select_transform(JXFORM_NONE); /* force an error */
230 #endif
232 } else if (keymatch(arg, "maxmemory", 3)) {
233 /* Maximum memory in Kb (or Mb with 'm'). */
234 long lval;
235 char ch = 'x';
237 if (++argn >= argc) /* advance to next argument */
238 usage();
239 if (sscanf(argv[argn], "%ld%c", &lval, &ch) < 1)
240 usage();
241 if (ch == 'm' || ch == 'M')
242 lval *= 1000L;
243 cinfo->mem->max_memory_to_use = lval * 1000L;
245 } else if (keymatch(arg, "optimize", 1) || keymatch(arg, "optimise", 1)) {
246 /* Enable entropy parm optimization. */
247 #ifdef ENTROPY_OPT_SUPPORTED
248 cinfo->optimize_coding = TRUE;
249 #else
250 fprintf(stderr, "%s: sorry, entropy optimization was not compiled\n",
251 progname);
252 exit(EXIT_FAILURE);
253 #endif
255 } else if (keymatch(arg, "outfile", 4)) {
256 /* Set output file name. */
257 if (++argn >= argc) /* advance to next argument */
258 usage();
259 outfilename = argv[argn]; /* save it away for later use */
261 } else if (keymatch(arg, "perfect", 2)) {
262 /* Fail if there is any partial edge MCUs that the transform can't
263 * handle. */
264 transformoption.perfect = TRUE;
266 } else if (keymatch(arg, "progressive", 2)) {
267 /* Select simple progressive mode. */
268 #ifdef C_PROGRESSIVE_SUPPORTED
269 simple_progressive = TRUE;
270 /* We must postpone execution until num_components is known. */
271 #else
272 fprintf(stderr, "%s: sorry, progressive output was not compiled\n",
273 progname);
274 exit(EXIT_FAILURE);
275 #endif
277 } else if (keymatch(arg, "restart", 1)) {
278 /* Restart interval in MCU rows (or in MCUs with 'b'). */
279 long lval;
280 char ch = 'x';
282 if (++argn >= argc) /* advance to next argument */
283 usage();
284 if (sscanf(argv[argn], "%ld%c", &lval, &ch) < 1)
285 usage();
286 if (lval < 0 || lval > 65535L)
287 usage();
288 if (ch == 'b' || ch == 'B') {
289 cinfo->restart_interval = (unsigned int) lval;
290 cinfo->restart_in_rows = 0; /* else prior '-restart n' overrides me */
291 } else {
292 cinfo->restart_in_rows = (int) lval;
293 /* restart_interval will be computed during startup */
296 } else if (keymatch(arg, "rotate", 2)) {
297 /* Rotate 90, 180, or 270 degrees (measured clockwise). */
298 if (++argn >= argc) /* advance to next argument */
299 usage();
300 if (keymatch(argv[argn], "90", 2))
301 select_transform(JXFORM_ROT_90);
302 else if (keymatch(argv[argn], "180", 3))
303 select_transform(JXFORM_ROT_180);
304 else if (keymatch(argv[argn], "270", 3))
305 select_transform(JXFORM_ROT_270);
306 else
307 usage();
309 } else if (keymatch(arg, "scale", 4)) {
310 /* Scale the output image by a fraction M/N. */
311 if (++argn >= argc) /* advance to next argument */
312 usage();
313 scaleoption = argv[argn];
314 /* We must postpone processing until decompression startup. */
316 } else if (keymatch(arg, "scans", 1)) {
317 /* Set scan script. */
318 #ifdef C_MULTISCAN_FILES_SUPPORTED
319 if (++argn >= argc) /* advance to next argument */
320 usage();
321 scansarg = argv[argn];
322 /* We must postpone reading the file in case -progressive appears. */
323 #else
324 fprintf(stderr, "%s: sorry, multi-scan output was not compiled\n",
325 progname);
326 exit(EXIT_FAILURE);
327 #endif
329 } else if (keymatch(arg, "transpose", 1)) {
330 /* Transpose (across UL-to-LR axis). */
331 select_transform(JXFORM_TRANSPOSE);
333 } else if (keymatch(arg, "transverse", 6)) {
334 /* Transverse transpose (across UR-to-LL axis). */
335 select_transform(JXFORM_TRANSVERSE);
337 } else if (keymatch(arg, "trim", 3)) {
338 /* Trim off any partial edge MCUs that the transform can't handle. */
339 transformoption.trim = TRUE;
341 } else if (keymatch(arg, "wipe", 1)) {
342 #if TRANSFORMS_SUPPORTED
343 if (++argn >= argc) /* advance to next argument */
344 usage();
345 if (transformoption.crop /* reject multiple crop/wipe requests */ ||
346 ! jtransform_parse_crop_spec(&transformoption, argv[argn])) {
347 fprintf(stderr, "%s: bogus -wipe argument '%s'\n",
348 progname, argv[argn]);
349 exit(EXIT_FAILURE);
351 select_transform(JXFORM_WIPE);
352 #else
353 select_transform(JXFORM_NONE); /* force an error */
354 #endif
356 } else {
357 usage(); /* bogus switch */
361 /* Post-switch-scanning cleanup */
363 if (for_real) {
365 #ifdef C_PROGRESSIVE_SUPPORTED
366 if (simple_progressive) /* process -progressive; -scans can override */
367 jpeg_simple_progression(cinfo);
368 #endif
370 #ifdef C_MULTISCAN_FILES_SUPPORTED
371 if (scansarg != NULL) /* process -scans if it was present */
372 if (! read_scan_script(cinfo, scansarg))
373 usage();
374 #endif
377 return argn; /* return index of next arg (file name) */
382 * The main program.
386 main (int argc, char **argv)
388 struct jpeg_decompress_struct srcinfo;
389 struct jpeg_compress_struct dstinfo;
390 struct jpeg_error_mgr jsrcerr, jdsterr;
391 #ifdef PROGRESS_REPORT
392 struct cdjpeg_progress_mgr progress;
393 #endif
394 jvirt_barray_ptr * src_coef_arrays;
395 jvirt_barray_ptr * dst_coef_arrays;
396 int file_index;
397 /* We assume all-in-memory processing and can therefore use only a
398 * single file pointer for sequential input and output operation.
400 FILE * fp;
402 /* On Mac, fetch a command line. */
403 #ifdef USE_CCOMMAND
404 argc = ccommand(&argv);
405 #endif
407 progname = argv[0];
408 if (progname == NULL || progname[0] == 0)
409 progname = "jpegtran"; /* in case C library doesn't provide it */
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);
418 /* Now safe to enable signal catcher.
419 * Note: we assume only the decompression object will have virtual arrays.
421 #ifdef NEED_SIGNAL_CATCHER
422 enable_signal_catcher((j_common_ptr) &srcinfo);
423 #endif
425 /* Scan command line to find file names.
426 * It is convenient to use just one switch-parsing routine, but the switch
427 * values read here are mostly ignored; we will rescan the switches after
428 * opening the input file. Also note that most of the switches affect the
429 * destination JPEG object, so we parse into that and then copy over what
430 * needs to affects the source too.
433 file_index = parse_switches(&dstinfo, argc, argv, 0, FALSE);
434 jsrcerr.trace_level = jdsterr.trace_level;
435 srcinfo.mem->max_memory_to_use = dstinfo.mem->max_memory_to_use;
437 #ifdef TWO_FILE_COMMANDLINE
438 /* Must have either -outfile switch or explicit output file name */
439 if (outfilename == NULL) {
440 if (file_index != argc-2) {
441 fprintf(stderr, "%s: must name one input and one output file\n",
442 progname);
443 usage();
445 outfilename = argv[file_index+1];
446 } else {
447 if (file_index != argc-1) {
448 fprintf(stderr, "%s: must name one input and one output file\n",
449 progname);
450 usage();
453 #else
454 /* Unix style: expect zero or one file name */
455 if (file_index < argc-1) {
456 fprintf(stderr, "%s: only one input file\n", progname);
457 usage();
459 #endif /* TWO_FILE_COMMANDLINE */
461 /* Open the input file. */
462 if (file_index < argc) {
463 if ((fp = fopen(argv[file_index], READ_BINARY)) == NULL) {
464 fprintf(stderr, "%s: can't open %s for reading\n", progname, argv[file_index]);
465 exit(EXIT_FAILURE);
467 } else {
468 /* default input file is stdin */
469 fp = read_stdin();
472 #ifdef PROGRESS_REPORT
473 start_progress_monitor((j_common_ptr) &dstinfo, &progress);
474 #endif
476 /* Specify data source for decompression */
477 jpeg_stdio_src(&srcinfo, fp);
479 /* Enable saving of extra markers that we want to copy */
480 jcopy_markers_setup(&srcinfo, copyoption);
482 /* Read file header */
483 (void) jpeg_read_header(&srcinfo, TRUE);
485 /* Adjust default decompression parameters */
486 if (scaleoption != NULL)
487 if (sscanf(scaleoption, "%u/%u",
488 &srcinfo.scale_num, &srcinfo.scale_denom) < 1)
489 usage();
491 /* Any space needed by a transform option must be requested before
492 * jpeg_read_coefficients so that memory allocation will be done right.
494 #if TRANSFORMS_SUPPORTED
495 /* Fail right away if -perfect is given and transformation is not perfect.
497 if (!jtransform_request_workspace(&srcinfo, &transformoption)) {
498 fprintf(stderr, "%s: transformation is not perfect\n", progname);
499 exit(EXIT_FAILURE);
501 #endif
503 /* Read source file as DCT coefficients */
504 src_coef_arrays = jpeg_read_coefficients(&srcinfo);
506 /* Initialize destination compression parameters from source values */
507 jpeg_copy_critical_parameters(&srcinfo, &dstinfo);
509 /* Adjust destination parameters if required by transform options;
510 * also find out which set of coefficient arrays will hold the output.
512 #if TRANSFORMS_SUPPORTED
513 dst_coef_arrays = jtransform_adjust_parameters(&srcinfo, &dstinfo,
514 src_coef_arrays,
515 &transformoption);
516 #else
517 dst_coef_arrays = src_coef_arrays;
518 #endif
520 /* Close input file, if we opened it.
521 * Note: we assume that jpeg_read_coefficients consumed all input
522 * until JPEG_REACHED_EOI, and that jpeg_finish_decompress will
523 * only consume more while (! cinfo->inputctl->eoi_reached).
524 * We cannot call jpeg_finish_decompress here since we still need the
525 * virtual arrays allocated from the source object for processing.
527 if (fp != stdin)
528 fclose(fp);
530 /* Open the output file. */
531 if (outfilename != NULL) {
532 if ((fp = fopen(outfilename, WRITE_BINARY)) == NULL) {
533 fprintf(stderr, "%s: can't open %s for writing\n", progname, outfilename);
534 exit(EXIT_FAILURE);
536 } else {
537 /* default output file is stdout */
538 fp = write_stdout();
541 /* Adjust default compression parameters by re-parsing the options */
542 file_index = parse_switches(&dstinfo, argc, argv, 0, TRUE);
544 /* Specify data destination for compression */
545 jpeg_stdio_dest(&dstinfo, fp);
547 /* Start compressor (note no image data is actually written here) */
548 jpeg_write_coefficients(&dstinfo, dst_coef_arrays);
550 /* Copy to the output file any extra markers that we want to preserve */
551 jcopy_markers_execute(&srcinfo, &dstinfo, copyoption);
553 /* Execute image transformation, if any */
554 #if TRANSFORMS_SUPPORTED
555 jtransform_execute_transformation(&srcinfo, &dstinfo,
556 src_coef_arrays,
557 &transformoption);
558 #endif
560 /* Finish compression and release memory */
561 jpeg_finish_compress(&dstinfo);
562 jpeg_destroy_compress(&dstinfo);
563 (void) jpeg_finish_decompress(&srcinfo);
564 jpeg_destroy_decompress(&srcinfo);
566 /* Close output file, if we opened it */
567 if (fp != stdout)
568 fclose(fp);
570 #ifdef PROGRESS_REPORT
571 end_progress_monitor((j_common_ptr) &dstinfo);
572 #endif
574 /* All done. */
575 exit(jsrcerr.num_warnings + jdsterr.num_warnings ?EXIT_WARNING:EXIT_SUCCESS);
576 return 0; /* suppress no-return-value warnings */