Fixed binary search: no more infinite loops when vendor is unknown.
[tangerine.git] / compiler / libjpeg / test / jpegtran.c
blob6618859e8bea39073a9b8f6af5792f1eef315a8a
1 /*
2 $Id$
3 */
5 /*
6 * jpegtran.c
8 * Copyright (C) 1995-1997, Thomas G. Lane.
9 * This file is part of the Independent JPEG Group's software.
10 * For conditions of distribution and use, see the accompanying README file.
12 * This file contains a command-line user interface for JPEG transcoding.
13 * It is very similar to cjpeg.c, but provides lossless transcoding between
14 * different JPEG file formats. It also provides some lossless and sort-of-
15 * lossless transformations of JPEG data.
18 #include "cdjpeg.h" /* Common decls for cjpeg/djpeg applications */
19 #include "transupp.h" /* Support routines for jpegtran */
20 #include "jversion.h" /* for version message */
22 #ifdef USE_CCOMMAND /* command-line reader for Macintosh */
23 #ifdef __MWERKS__
24 #include <SIOUX.h> /* Metrowerks needs this */
25 #include <console.h> /* ... and this */
26 #endif
27 #ifdef THINK_C
28 #include <console.h> /* Think declares it here */
29 #endif
30 #endif
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...
42 static const char * progname; /* program name for error messages */
43 static char * outfilename; /* for -outfile switch */
44 static JCOPY_OPTION copyoption; /* -copy switch */
45 static jpeg_transform_info transformoption; /* image transformation options */
48 LOCAL(void)
49 usage (void)
50 /* complain about bad command line */
52 fprintf(stderr, "usage: %s [switches] ", progname);
53 #ifdef TWO_FILE_COMMANDLINE
54 fprintf(stderr, "inputfile outputfile\n");
55 #else
56 fprintf(stderr, "[inputfile]\n");
57 #endif
59 fprintf(stderr, "Switches (names may be abbreviated):\n");
60 fprintf(stderr, " -copy none Copy no extra markers from source file\n");
61 fprintf(stderr, " -copy comments Copy only comment markers (default)\n");
62 fprintf(stderr, " -copy all Copy all extra markers\n");
63 #ifdef ENTROPY_OPT_SUPPORTED
64 fprintf(stderr, " -optimize Optimize Huffman table (smaller file, but slow compression)\n");
65 #endif
66 #ifdef C_PROGRESSIVE_SUPPORTED
67 fprintf(stderr, " -progressive Create progressive JPEG file\n");
68 #endif
69 #if TRANSFORMS_SUPPORTED
70 fprintf(stderr, "Switches for modifying the image:\n");
71 fprintf(stderr, " -grayscale Reduce to grayscale (omit color data)\n");
72 fprintf(stderr, " -flip [horizontal|vertical] Mirror image (left-right or top-bottom)\n");
73 fprintf(stderr, " -rotate [90|180|270] Rotate image (degrees clockwise)\n");
74 fprintf(stderr, " -transpose Transpose image\n");
75 fprintf(stderr, " -transverse Transverse transpose image\n");
76 fprintf(stderr, " -trim Drop non-transformable edge blocks\n");
77 #endif /* TRANSFORMS_SUPPORTED */
78 fprintf(stderr, "Switches for advanced users:\n");
79 fprintf(stderr, " -restart N Set restart interval in rows, or in blocks with B\n");
80 fprintf(stderr, " -maxmemory N Maximum memory to use (in kbytes)\n");
81 fprintf(stderr, " -outfile name Specify name for output file\n");
82 fprintf(stderr, " -verbose or -debug Emit debug output\n");
83 fprintf(stderr, "Switches for wizards:\n");
84 #ifdef C_ARITH_CODING_SUPPORTED
85 fprintf(stderr, " -arithmetic Use arithmetic coding\n");
86 #endif
87 #ifdef C_MULTISCAN_FILES_SUPPORTED
88 fprintf(stderr, " -scans file Create multi-scan JPEG per script file\n");
89 #endif
90 exit(EXIT_FAILURE);
94 LOCAL(void)
95 select_transform (JXFORM_CODE transform)
96 /* Silly little routine to detect multiple transform options,
97 * which we can't handle.
100 #if TRANSFORMS_SUPPORTED
101 if (transformoption.transform == JXFORM_NONE ||
102 transformoption.transform == transform) {
103 transformoption.transform = transform;
104 } else {
105 fprintf(stderr, "%s: can only do one image transformation at a time\n",
106 progname);
107 usage();
109 #else
110 fprintf(stderr, "%s: sorry, image transformation was not compiled\n",
111 progname);
112 exit(EXIT_FAILURE);
113 #endif
117 LOCAL(int)
118 parse_switches (j_compress_ptr cinfo, int argc, char **argv,
119 int last_file_arg_seen, boolean for_real)
120 /* Parse optional switches.
121 * Returns argv[] index of first file-name argument (== argc if none).
122 * Any file names with indexes <= last_file_arg_seen are ignored;
123 * they have presumably been processed in a previous iteration.
124 * (Pass 0 for last_file_arg_seen on the first or only iteration.)
125 * for_real is FALSE on the first (dummy) pass; we may skip any expensive
126 * processing.
129 int argn;
130 char * arg;
131 boolean simple_progressive;
132 char * scansarg = NULL; /* saves -scans parm if any */
134 /* Set up default JPEG parameters. */
135 simple_progressive = FALSE;
136 outfilename = NULL;
137 copyoption = JCOPYOPT_DEFAULT;
138 transformoption.transform = JXFORM_NONE;
139 transformoption.trim = FALSE;
140 transformoption.force_grayscale = 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", 1)) {
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, "debug", 1) || keymatch(arg, "verbose", 1)) {
181 /* Enable debug printouts. */
182 /* On first -d, print version identification */
183 static boolean printed_version = FALSE;
185 if (! printed_version) {
186 fprintf(stderr, "Independent JPEG Group's JPEGTRAN, version %s\n%s\n",
187 JVERSION, JCOPYRIGHT);
188 printed_version = TRUE;
190 cinfo->err->trace_level++;
192 } else if (keymatch(arg, "flip", 1)) {
193 /* Mirror left-right or top-bottom. */
194 if (++argn >= argc) /* advance to next argument */
195 usage();
196 if (keymatch(argv[argn], "horizontal", 1))
197 select_transform(JXFORM_FLIP_H);
198 else if (keymatch(argv[argn], "vertical", 1))
199 select_transform(JXFORM_FLIP_V);
200 else
201 usage();
203 } else if (keymatch(arg, "grayscale", 1) || keymatch(arg, "greyscale",1)) {
204 /* Force to grayscale. */
205 #if TRANSFORMS_SUPPORTED
206 transformoption.force_grayscale = TRUE;
207 #else
208 select_transform(JXFORM_NONE); /* force an error */
209 #endif
211 } else if (keymatch(arg, "maxmemory", 3)) {
212 /* Maximum memory in Kb (or Mb with 'm'). */
213 long lval;
214 char ch = 'x';
216 if (++argn >= argc) /* advance to next argument */
217 usage();
218 if (sscanf(argv[argn], "%ld%c", &lval, &ch) < 1)
219 usage();
220 if (ch == 'm' || ch == 'M')
221 lval *= 1000L;
222 cinfo->mem->max_memory_to_use = lval * 1000L;
224 } else if (keymatch(arg, "optimize", 1) || keymatch(arg, "optimise", 1)) {
225 /* Enable entropy parm optimization. */
226 #ifdef ENTROPY_OPT_SUPPORTED
227 cinfo->optimize_coding = TRUE;
228 #else
229 fprintf(stderr, "%s: sorry, entropy optimization was not compiled\n",
230 progname);
231 exit(EXIT_FAILURE);
232 #endif
234 } else if (keymatch(arg, "outfile", 4)) {
235 /* Set output file name. */
236 if (++argn >= argc) /* advance to next argument */
237 usage();
238 outfilename = argv[argn]; /* save it away for later use */
240 } else if (keymatch(arg, "progressive", 1)) {
241 /* Select simple progressive mode. */
242 #ifdef C_PROGRESSIVE_SUPPORTED
243 simple_progressive = TRUE;
244 /* We must postpone execution until num_components is known. */
245 #else
246 fprintf(stderr, "%s: sorry, progressive output was not compiled\n",
247 progname);
248 exit(EXIT_FAILURE);
249 #endif
251 } else if (keymatch(arg, "restart", 1)) {
252 /* Restart interval in MCU rows (or in MCUs with 'b'). */
253 long lval;
254 char ch = 'x';
256 if (++argn >= argc) /* advance to next argument */
257 usage();
258 if (sscanf(argv[argn], "%ld%c", &lval, &ch) < 1)
259 usage();
260 if (lval < 0 || lval > 65535L)
261 usage();
262 if (ch == 'b' || ch == 'B') {
263 cinfo->restart_interval = (unsigned int) lval;
264 cinfo->restart_in_rows = 0; /* else prior '-restart n' overrides me */
265 } else {
266 cinfo->restart_in_rows = (int) lval;
267 /* restart_interval will be computed during startup */
270 } else if (keymatch(arg, "rotate", 2)) {
271 /* Rotate 90, 180, or 270 degrees (measured clockwise). */
272 if (++argn >= argc) /* advance to next argument */
273 usage();
274 if (keymatch(argv[argn], "90", 2))
275 select_transform(JXFORM_ROT_90);
276 else if (keymatch(argv[argn], "180", 3))
277 select_transform(JXFORM_ROT_180);
278 else if (keymatch(argv[argn], "270", 3))
279 select_transform(JXFORM_ROT_270);
280 else
281 usage();
283 } else if (keymatch(arg, "scans", 1)) {
284 /* Set scan script. */
285 #ifdef C_MULTISCAN_FILES_SUPPORTED
286 if (++argn >= argc) /* advance to next argument */
287 usage();
288 scansarg = argv[argn];
289 /* We must postpone reading the file in case -progressive appears. */
290 #else
291 fprintf(stderr, "%s: sorry, multi-scan output was not compiled\n",
292 progname);
293 exit(EXIT_FAILURE);
294 #endif
296 } else if (keymatch(arg, "transpose", 1)) {
297 /* Transpose (across UL-to-LR axis). */
298 select_transform(JXFORM_TRANSPOSE);
300 } else if (keymatch(arg, "transverse", 6)) {
301 /* Transverse transpose (across UR-to-LL axis). */
302 select_transform(JXFORM_TRANSVERSE);
304 } else if (keymatch(arg, "trim", 3)) {
305 /* Trim off any partial edge MCUs that the transform can't handle. */
306 transformoption.trim = TRUE;
308 } else {
309 usage(); /* bogus switch */
313 /* Post-switch-scanning cleanup */
315 if (for_real) {
317 #ifdef C_PROGRESSIVE_SUPPORTED
318 if (simple_progressive) /* process -progressive; -scans can override */
319 jpeg_simple_progression(cinfo);
320 #endif
322 #ifdef C_MULTISCAN_FILES_SUPPORTED
323 if (scansarg != NULL) /* process -scans if it was present */
324 if (! read_scan_script(cinfo, scansarg))
325 usage();
326 #endif
329 return argn; /* return index of next arg (file name) */
334 * The main program.
338 main (int argc, char **argv)
340 struct jpeg_decompress_struct srcinfo;
341 struct jpeg_compress_struct dstinfo;
342 struct jpeg_error_mgr jsrcerr, jdsterr;
343 #ifdef PROGRESS_REPORT
344 struct cdjpeg_progress_mgr progress;
345 #endif
346 jvirt_barray_ptr * src_coef_arrays;
347 jvirt_barray_ptr * dst_coef_arrays;
348 int file_index;
349 FILE * input_file;
350 FILE * output_file;
352 /* On Mac, fetch a command line. */
353 #ifdef USE_CCOMMAND
354 argc = ccommand(&argv);
355 #endif
357 progname = argv[0];
358 if (progname == NULL || progname[0] == 0)
359 progname = "jpegtran"; /* in case C library doesn't provide it */
361 /* Initialize the JPEG decompression object with default error handling. */
362 srcinfo.err = jpeg_std_error(&jsrcerr);
363 jpeg_create_decompress(&srcinfo);
364 /* Initialize the JPEG compression object with default error handling. */
365 dstinfo.err = jpeg_std_error(&jdsterr);
366 jpeg_create_compress(&dstinfo);
368 /* Now safe to enable signal catcher.
369 * Note: we assume only the decompression object will have virtual arrays.
371 #ifdef NEED_SIGNAL_CATCHER
372 enable_signal_catcher((j_common_ptr) &srcinfo);
373 #endif
375 /* Scan command line to find file names.
376 * It is convenient to use just one switch-parsing routine, but the switch
377 * values read here are mostly ignored; we will rescan the switches after
378 * opening the input file. Also note that most of the switches affect the
379 * destination JPEG object, so we parse into that and then copy over what
380 * needs to affects the source too.
383 file_index = parse_switches(&dstinfo, argc, argv, 0, FALSE);
384 jsrcerr.trace_level = jdsterr.trace_level;
385 srcinfo.mem->max_memory_to_use = dstinfo.mem->max_memory_to_use;
387 #ifdef TWO_FILE_COMMANDLINE
388 /* Must have either -outfile switch or explicit output file name */
389 if (outfilename == NULL) {
390 if (file_index != argc-2) {
391 fprintf(stderr, "%s: must name one input and one output file\n",
392 progname);
393 usage();
395 outfilename = argv[file_index+1];
396 } else {
397 if (file_index != argc-1) {
398 fprintf(stderr, "%s: must name one input and one output file\n",
399 progname);
400 usage();
403 #else
404 /* Unix style: expect zero or one file name */
405 if (file_index < argc-1) {
406 fprintf(stderr, "%s: only one input file\n", progname);
407 usage();
409 #endif /* TWO_FILE_COMMANDLINE */
411 /* Open the input file. */
412 if (file_index < argc) {
413 if ((input_file = fopen(argv[file_index], READ_BINARY)) == NULL) {
414 fprintf(stderr, "%s: can't open %s\n", progname, argv[file_index]);
415 exit(EXIT_FAILURE);
417 } else {
418 /* default input file is stdin */
419 input_file = read_stdin();
422 /* Open the output file. */
423 if (outfilename != NULL) {
424 if ((output_file = fopen(outfilename, WRITE_BINARY)) == NULL) {
425 fprintf(stderr, "%s: can't open %s\n", progname, outfilename);
426 exit(EXIT_FAILURE);
428 } else {
429 /* default output file is stdout */
430 output_file = write_stdout();
433 #ifdef PROGRESS_REPORT
434 start_progress_monitor((j_common_ptr) &dstinfo, &progress);
435 #endif
437 /* Specify data source for decompression */
438 jpeg_stdio_src(&srcinfo, input_file);
440 /* Enable saving of extra markers that we want to copy */
441 jcopy_markers_setup(&srcinfo, copyoption);
443 /* Read file header */
444 (void) jpeg_read_header(&srcinfo, TRUE);
446 /* Any space needed by a transform option must be requested before
447 * jpeg_read_coefficients so that memory allocation will be done right.
449 #if TRANSFORMS_SUPPORTED
450 jtransform_request_workspace(&srcinfo, &transformoption);
451 #endif
453 /* Read source file as DCT coefficients */
454 src_coef_arrays = jpeg_read_coefficients(&srcinfo);
456 /* Initialize destination compression parameters from source values */
457 jpeg_copy_critical_parameters(&srcinfo, &dstinfo);
459 /* Adjust destination parameters if required by transform options;
460 * also find out which set of coefficient arrays will hold the output.
462 #if TRANSFORMS_SUPPORTED
463 dst_coef_arrays = jtransform_adjust_parameters(&srcinfo, &dstinfo,
464 src_coef_arrays,
465 &transformoption);
466 #else
467 dst_coef_arrays = src_coef_arrays;
468 #endif
470 /* Adjust default compression parameters by re-parsing the options */
471 file_index = parse_switches(&dstinfo, argc, argv, 0, TRUE);
473 /* Specify data destination for compression */
474 jpeg_stdio_dest(&dstinfo, output_file);
476 /* Start compressor (note no image data is actually written here) */
477 jpeg_write_coefficients(&dstinfo, dst_coef_arrays);
479 /* Copy to the output file any extra markers that we want to preserve */
480 jcopy_markers_execute(&srcinfo, &dstinfo, copyoption);
482 /* Execute image transformation, if any */
483 #if TRANSFORMS_SUPPORTED
484 jtransform_execute_transformation(&srcinfo, &dstinfo,
485 src_coef_arrays,
486 &transformoption);
487 #endif
489 /* Finish compression and release memory */
490 jpeg_finish_compress(&dstinfo);
491 jpeg_destroy_compress(&dstinfo);
492 (void) jpeg_finish_decompress(&srcinfo);
493 jpeg_destroy_decompress(&srcinfo);
495 /* Close files, if we opened them */
496 if (input_file != stdin)
497 fclose(input_file);
498 if (output_file != stdout)
499 fclose(output_file);
501 #ifdef PROGRESS_REPORT
502 end_progress_monitor((j_common_ptr) &dstinfo);
503 #endif
505 /* All done. */
506 exit(jsrcerr.num_warnings + jdsterr.num_warnings ?EXIT_WARNING:EXIT_SUCCESS);
507 return 0; /* suppress no-return-value warnings */