/src/libjpeg-turbo.2.0.x/cjpeg.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * cjpeg.c |
3 | | * |
4 | | * This file was part of the Independent JPEG Group's software: |
5 | | * Copyright (C) 1991-1998, Thomas G. Lane. |
6 | | * Modified 2003-2011 by Guido Vollbeding. |
7 | | * libjpeg-turbo Modifications: |
8 | | * Copyright (C) 2010, 2013-2014, 2017, 2020-2021, D. R. Commander. |
9 | | * For conditions of distribution and use, see the accompanying README.ijg |
10 | | * file. |
11 | | * |
12 | | * This file contains a command-line user interface for the JPEG compressor. |
13 | | * It should work on any system with Unix- or MS-DOS-style command lines. |
14 | | * |
15 | | * Two different command line styles are permitted, depending on the |
16 | | * compile-time switch TWO_FILE_COMMANDLINE: |
17 | | * cjpeg [options] inputfile outputfile |
18 | | * cjpeg [options] [inputfile] |
19 | | * In the second style, output is always to standard output, which you'd |
20 | | * normally redirect to a file or pipe to some other program. Input is |
21 | | * either from a named file or from standard input (typically redirected). |
22 | | * The second style is convenient on Unix but is unhelpful on systems that |
23 | | * don't support pipes. Also, you MUST use the first style if your system |
24 | | * doesn't do binary I/O to stdin/stdout. |
25 | | * To simplify script writing, the "-outfile" switch is provided. The syntax |
26 | | * cjpeg [options] -outfile outputfile inputfile |
27 | | * works regardless of which command line style is used. |
28 | | */ |
29 | | |
30 | | #ifdef CJPEG_FUZZER |
31 | | #define JPEG_INTERNALS |
32 | | #endif |
33 | | #include "cdjpeg.h" /* Common decls for cjpeg/djpeg applications */ |
34 | | #include "jversion.h" /* for version message */ |
35 | | #include "jconfigint.h" |
36 | | |
37 | | #ifndef HAVE_STDLIB_H /* <stdlib.h> should declare malloc(),free() */ |
38 | | extern void *malloc(size_t size); |
39 | | extern void free(void *ptr); |
40 | | #endif |
41 | | |
42 | | #ifdef USE_CCOMMAND /* command-line reader for Macintosh */ |
43 | | #ifdef __MWERKS__ |
44 | | #include <SIOUX.h> /* Metrowerks needs this */ |
45 | | #include <console.h> /* ... and this */ |
46 | | #endif |
47 | | #ifdef THINK_C |
48 | | #include <console.h> /* Think declares it here */ |
49 | | #endif |
50 | | #endif |
51 | | |
52 | | |
53 | | /* Create the add-on message string table. */ |
54 | | |
55 | | #define JMESSAGE(code, string) string, |
56 | | |
57 | | static const char * const cdjpeg_message_table[] = { |
58 | | #include "cderror.h" |
59 | | NULL |
60 | | }; |
61 | | |
62 | | |
63 | | /* |
64 | | * This routine determines what format the input file is, |
65 | | * and selects the appropriate input-reading module. |
66 | | * |
67 | | * To determine which family of input formats the file belongs to, |
68 | | * we may look only at the first byte of the file, since C does not |
69 | | * guarantee that more than one character can be pushed back with ungetc. |
70 | | * Looking at additional bytes would require one of these approaches: |
71 | | * 1) assume we can fseek() the input file (fails for piped input); |
72 | | * 2) assume we can push back more than one character (works in |
73 | | * some C implementations, but unportable); |
74 | | * 3) provide our own buffering (breaks input readers that want to use |
75 | | * stdio directly, such as the RLE library); |
76 | | * or 4) don't put back the data, and modify the input_init methods to assume |
77 | | * they start reading after the start of file (also breaks RLE library). |
78 | | * #1 is attractive for MS-DOS but is untenable on Unix. |
79 | | * |
80 | | * The most portable solution for file types that can't be identified by their |
81 | | * first byte is to make the user tell us what they are. This is also the |
82 | | * only approach for "raw" file types that contain only arbitrary values. |
83 | | * We presently apply this method for Targa files. Most of the time Targa |
84 | | * files start with 0x00, so we recognize that case. Potentially, however, |
85 | | * a Targa file could start with any byte value (byte 0 is the length of the |
86 | | * seldom-used ID field), so we provide a switch to force Targa input mode. |
87 | | */ |
88 | | |
89 | | static boolean is_targa; /* records user -targa switch */ |
90 | | |
91 | | |
92 | | LOCAL(cjpeg_source_ptr) |
93 | | select_file_type(j_compress_ptr cinfo, FILE *infile) |
94 | 12.3k | { |
95 | 12.3k | int c; |
96 | | |
97 | 12.3k | if (is_targa) { |
98 | 6.18k | #ifdef TARGA_SUPPORTED |
99 | 6.18k | return jinit_read_targa(cinfo); |
100 | | #else |
101 | | ERREXIT(cinfo, JERR_TGA_NOTCOMP); |
102 | | #endif |
103 | 6.18k | } |
104 | | |
105 | 6.18k | if ((c = getc(infile)) == EOF) |
106 | 0 | ERREXIT(cinfo, JERR_INPUT_EMPTY); |
107 | 6.18k | if (ungetc(c, infile) == EOF) |
108 | 0 | ERREXIT(cinfo, JERR_UNGETC_FAILED); |
109 | | |
110 | 6.18k | switch (c) { |
111 | 0 | #ifdef BMP_SUPPORTED |
112 | 1.46k | case 'B': |
113 | 1.46k | return jinit_read_bmp(cinfo, TRUE); |
114 | 0 | #endif |
115 | | #ifdef GIF_SUPPORTED |
116 | | case 'G': |
117 | | return jinit_read_gif(cinfo); |
118 | | #endif |
119 | 0 | #ifdef PPM_SUPPORTED |
120 | 2.34k | case 'P': |
121 | 2.34k | return jinit_read_ppm(cinfo); |
122 | 0 | #endif |
123 | | #ifdef RLE_SUPPORTED |
124 | | case 'R': |
125 | | return jinit_read_rle(cinfo); |
126 | | #endif |
127 | 0 | #ifdef TARGA_SUPPORTED |
128 | 1.57k | case 0x00: |
129 | 1.57k | return jinit_read_targa(cinfo); |
130 | 0 | #endif |
131 | 804 | default: |
132 | 804 | ERREXIT(cinfo, JERR_UNKNOWN_FORMAT); |
133 | 804 | break; |
134 | 6.18k | } |
135 | | |
136 | 0 | return NULL; /* suppress compiler warnings */ |
137 | 6.18k | } |
138 | | |
139 | | |
140 | | /* |
141 | | * Argument-parsing code. |
142 | | * The switch parser is designed to be useful with DOS-style command line |
143 | | * syntax, ie, intermixed switches and file names, where only the switches |
144 | | * to the left of a given file name affect processing of that file. |
145 | | * The main program in this file doesn't actually use this capability... |
146 | | */ |
147 | | |
148 | | |
149 | | static const char *progname; /* program name for error messages */ |
150 | | static char *icc_filename; /* for -icc switch */ |
151 | | static char *outfilename; /* for -outfile switch */ |
152 | | boolean memdst; /* for -memdst switch */ |
153 | | |
154 | | |
155 | | #ifdef CJPEG_FUZZER |
156 | | |
157 | | #include <setjmp.h> |
158 | | |
159 | | struct my_error_mgr { |
160 | | struct jpeg_error_mgr pub; |
161 | | jmp_buf setjmp_buffer; |
162 | | }; |
163 | | |
164 | | void my_error_exit(j_common_ptr cinfo) |
165 | 20.7k | { |
166 | 20.7k | struct my_error_mgr *myerr = (struct my_error_mgr *)cinfo->err; |
167 | | |
168 | 20.7k | longjmp(myerr->setjmp_buffer, 1); |
169 | 20.7k | } |
170 | | |
171 | | static void my_emit_message(j_common_ptr cinfo, int msg_level) |
172 | 6.89k | { |
173 | 6.89k | if (msg_level < 0) |
174 | 0 | cinfo->err->num_warnings++; |
175 | 6.89k | } |
176 | | |
177 | 9.46k | #define HANDLE_ERROR() { \ |
178 | 9.46k | if (cinfo.global_state > CSTATE_START) { \ |
179 | 1.98k | if (memdst && outbuffer) \ |
180 | 1.98k | (*cinfo.dest->term_destination) (&cinfo); \ |
181 | 1.98k | jpeg_abort_compress(&cinfo); \ |
182 | 1.98k | } \ |
183 | 9.46k | jpeg_destroy_compress(&cinfo); \ |
184 | 9.46k | if (input_file != stdin && input_file != NULL) \ |
185 | 9.46k | fclose(input_file); \ |
186 | 9.46k | if (memdst) \ |
187 | 9.46k | free(outbuffer); \ |
188 | 9.46k | return EXIT_FAILURE; \ |
189 | 9.46k | } |
190 | | |
191 | | #endif |
192 | | |
193 | | |
194 | | LOCAL(void) |
195 | | usage(void) |
196 | | /* complain about bad command line */ |
197 | 0 | { |
198 | 0 | fprintf(stderr, "usage: %s [switches] ", progname); |
199 | | #ifdef TWO_FILE_COMMANDLINE |
200 | | fprintf(stderr, "inputfile outputfile\n"); |
201 | | #else |
202 | 0 | fprintf(stderr, "[inputfile]\n"); |
203 | 0 | #endif |
204 | |
|
205 | 0 | fprintf(stderr, "Switches (names may be abbreviated):\n"); |
206 | 0 | fprintf(stderr, " -quality N[,...] Compression quality (0..100; 5-95 is most useful range,\n"); |
207 | 0 | fprintf(stderr, " default is 75)\n"); |
208 | 0 | fprintf(stderr, " -grayscale Create monochrome JPEG file\n"); |
209 | 0 | fprintf(stderr, " -rgb Create RGB JPEG file\n"); |
210 | 0 | #ifdef ENTROPY_OPT_SUPPORTED |
211 | 0 | fprintf(stderr, " -optimize Optimize Huffman table (smaller file, but slow compression)\n"); |
212 | 0 | #endif |
213 | 0 | #ifdef C_PROGRESSIVE_SUPPORTED |
214 | 0 | fprintf(stderr, " -progressive Create progressive JPEG file\n"); |
215 | 0 | #endif |
216 | 0 | #ifdef TARGA_SUPPORTED |
217 | 0 | fprintf(stderr, " -targa Input file is Targa format (usually not needed)\n"); |
218 | 0 | #endif |
219 | 0 | fprintf(stderr, "Switches for advanced users:\n"); |
220 | 0 | #ifdef C_ARITH_CODING_SUPPORTED |
221 | 0 | fprintf(stderr, " -arithmetic Use arithmetic coding\n"); |
222 | 0 | #endif |
223 | 0 | #ifdef DCT_ISLOW_SUPPORTED |
224 | 0 | fprintf(stderr, " -dct int Use accurate integer DCT method%s\n", |
225 | 0 | (JDCT_DEFAULT == JDCT_ISLOW ? " (default)" : "")); |
226 | 0 | #endif |
227 | 0 | #ifdef DCT_IFAST_SUPPORTED |
228 | 0 | fprintf(stderr, " -dct fast Use less accurate integer DCT method [legacy feature]%s\n", |
229 | 0 | (JDCT_DEFAULT == JDCT_IFAST ? " (default)" : "")); |
230 | 0 | #endif |
231 | 0 | #ifdef DCT_FLOAT_SUPPORTED |
232 | 0 | fprintf(stderr, " -dct float Use floating-point DCT method [legacy feature]%s\n", |
233 | 0 | (JDCT_DEFAULT == JDCT_FLOAT ? " (default)" : "")); |
234 | 0 | #endif |
235 | 0 | fprintf(stderr, " -icc FILE Embed ICC profile contained in FILE\n"); |
236 | 0 | fprintf(stderr, " -restart N Set restart interval in rows, or in blocks with B\n"); |
237 | 0 | #ifdef INPUT_SMOOTHING_SUPPORTED |
238 | 0 | fprintf(stderr, " -smooth N Smooth dithered input (N=1..100 is strength)\n"); |
239 | 0 | #endif |
240 | 0 | fprintf(stderr, " -maxmemory N Maximum memory to use (in kbytes)\n"); |
241 | 0 | fprintf(stderr, " -outfile name Specify name for output file\n"); |
242 | 0 | #if JPEG_LIB_VERSION >= 80 || defined(MEM_SRCDST_SUPPORTED) |
243 | 0 | fprintf(stderr, " -memdst Compress to memory instead of file (useful for benchmarking)\n"); |
244 | 0 | #endif |
245 | 0 | fprintf(stderr, " -verbose or -debug Emit debug output\n"); |
246 | 0 | fprintf(stderr, " -version Print version information and exit\n"); |
247 | 0 | fprintf(stderr, "Switches for wizards:\n"); |
248 | 0 | fprintf(stderr, " -baseline Force baseline quantization tables\n"); |
249 | 0 | fprintf(stderr, " -qtables FILE Use quantization tables given in FILE\n"); |
250 | 0 | fprintf(stderr, " -qslots N[,...] Set component quantization tables\n"); |
251 | 0 | fprintf(stderr, " -sample HxV[,...] Set component sampling factors\n"); |
252 | 0 | #ifdef C_MULTISCAN_FILES_SUPPORTED |
253 | 0 | fprintf(stderr, " -scans FILE Create multi-scan JPEG per script FILE\n"); |
254 | 0 | #endif |
255 | 0 | exit(EXIT_FAILURE); |
256 | 0 | } |
257 | | |
258 | | |
259 | | LOCAL(int) |
260 | | parse_switches(j_compress_ptr cinfo, int argc, char **argv, |
261 | | int last_file_arg_seen, boolean for_real) |
262 | | /* Parse optional switches. |
263 | | * Returns argv[] index of first file-name argument (== argc if none). |
264 | | * Any file names with indexes <= last_file_arg_seen are ignored; |
265 | | * they have presumably been processed in a previous iteration. |
266 | | * (Pass 0 for last_file_arg_seen on the first or only iteration.) |
267 | | * for_real is FALSE on the first (dummy) pass; we may skip any expensive |
268 | | * processing. |
269 | | */ |
270 | 18.0k | { |
271 | 18.0k | int argn; |
272 | 18.0k | char *arg; |
273 | 18.0k | boolean force_baseline; |
274 | 18.0k | boolean simple_progressive; |
275 | 18.0k | char *qualityarg = NULL; /* saves -quality parm if any */ |
276 | 18.0k | char *qtablefile = NULL; /* saves -qtables filename if any */ |
277 | 18.0k | char *qslotsarg = NULL; /* saves -qslots parm if any */ |
278 | 18.0k | char *samplearg = NULL; /* saves -sample parm if any */ |
279 | 18.0k | char *scansarg = NULL; /* saves -scans parm if any */ |
280 | | |
281 | | /* Set up default JPEG parameters. */ |
282 | | |
283 | 18.0k | force_baseline = FALSE; /* by default, allow 16-bit quantizers */ |
284 | 18.0k | simple_progressive = FALSE; |
285 | 18.0k | is_targa = FALSE; |
286 | 18.0k | icc_filename = NULL; |
287 | 18.0k | outfilename = NULL; |
288 | 18.0k | memdst = FALSE; |
289 | 18.0k | cinfo->err->trace_level = 0; |
290 | | |
291 | | /* Scan command line options, adjust parameters */ |
292 | | |
293 | 143k | for (argn = 1; argn < argc; argn++) { |
294 | 143k | arg = argv[argn]; |
295 | 143k | if (*arg != '-') { |
296 | | /* Not a switch, must be a file name argument */ |
297 | 18.0k | if (argn <= last_file_arg_seen) { |
298 | 0 | outfilename = NULL; /* -outfile applies to just one input file */ |
299 | 0 | continue; /* ignore this name if previously processed */ |
300 | 0 | } |
301 | 18.0k | break; /* else done parsing switches */ |
302 | 18.0k | } |
303 | 125k | arg++; /* advance past switch marker character */ |
304 | | |
305 | 125k | if (keymatch(arg, "arithmetic", 1)) { |
306 | | /* Use arithmetic coding. */ |
307 | 9.01k | #ifdef C_ARITH_CODING_SUPPORTED |
308 | 9.01k | cinfo->arith_code = TRUE; |
309 | | #else |
310 | | fprintf(stderr, "%s: sorry, arithmetic coding not supported\n", |
311 | | progname); |
312 | | exit(EXIT_FAILURE); |
313 | | #endif |
314 | | |
315 | 116k | } else if (keymatch(arg, "baseline", 1)) { |
316 | | /* Force baseline-compatible output (8-bit quantizer values). */ |
317 | 0 | force_baseline = TRUE; |
318 | |
|
319 | 116k | } else if (keymatch(arg, "dct", 2)) { |
320 | | /* Select DCT algorithm. */ |
321 | 18.0k | if (++argn >= argc) /* advance to next argument */ |
322 | 0 | usage(); |
323 | 18.0k | if (keymatch(argv[argn], "int", 1)) { |
324 | 0 | cinfo->dct_method = JDCT_ISLOW; |
325 | 18.0k | } else if (keymatch(argv[argn], "fast", 2)) { |
326 | 0 | cinfo->dct_method = JDCT_IFAST; |
327 | 18.0k | } else if (keymatch(argv[argn], "float", 2)) { |
328 | 18.0k | cinfo->dct_method = JDCT_FLOAT; |
329 | 18.0k | } else |
330 | 0 | usage(); |
331 | | |
332 | 98.4k | } else if (keymatch(arg, "debug", 1) || keymatch(arg, "verbose", 1)) { |
333 | | /* Enable debug printouts. */ |
334 | | /* On first -d, print version identification */ |
335 | 0 | static boolean printed_version = FALSE; |
336 | |
|
337 | 0 | if (!printed_version) { |
338 | 0 | fprintf(stderr, "%s version %s (build %s)\n", |
339 | 0 | PACKAGE_NAME, VERSION, BUILD); |
340 | 0 | fprintf(stderr, "%s\n\n", JCOPYRIGHT); |
341 | 0 | fprintf(stderr, "Emulating The Independent JPEG Group's software, version %s\n\n", |
342 | 0 | JVERSION); |
343 | 0 | printed_version = TRUE; |
344 | 0 | } |
345 | 0 | cinfo->err->trace_level++; |
346 | |
|
347 | 98.4k | } else if (keymatch(arg, "version", 4)) { |
348 | 0 | fprintf(stderr, "%s version %s (build %s)\n", |
349 | 0 | PACKAGE_NAME, VERSION, BUILD); |
350 | 0 | exit(EXIT_SUCCESS); |
351 | |
|
352 | 98.4k | } else if (keymatch(arg, "grayscale", 2) || |
353 | 98.4k | keymatch(arg, "greyscale", 2)) { |
354 | | /* Force a monochrome JPEG file to be generated. */ |
355 | 0 | jpeg_set_colorspace(cinfo, JCS_GRAYSCALE); |
356 | |
|
357 | 98.4k | } else if (keymatch(arg, "rgb", 3)) { |
358 | | /* Force an RGB JPEG file to be generated. */ |
359 | 9.01k | jpeg_set_colorspace(cinfo, JCS_RGB); |
360 | | |
361 | 89.3k | } else if (keymatch(arg, "icc", 1)) { |
362 | | /* Set ICC filename. */ |
363 | 0 | if (++argn >= argc) /* advance to next argument */ |
364 | 0 | usage(); |
365 | 0 | icc_filename = argv[argn]; |
366 | |
|
367 | 89.3k | } else if (keymatch(arg, "maxmemory", 3)) { |
368 | | /* Maximum memory in Kb (or Mb with 'm'). */ |
369 | 0 | long lval; |
370 | 0 | char ch = 'x'; |
371 | |
|
372 | 0 | if (++argn >= argc) /* advance to next argument */ |
373 | 0 | usage(); |
374 | 0 | if (sscanf(argv[argn], "%ld%c", &lval, &ch) < 1) |
375 | 0 | usage(); |
376 | 0 | if (ch == 'm' || ch == 'M') |
377 | 0 | lval *= 1000L; |
378 | 0 | cinfo->mem->max_memory_to_use = lval * 1000L; |
379 | |
|
380 | 89.3k | } else if (keymatch(arg, "optimize", 1) || keymatch(arg, "optimise", 1)) { |
381 | | /* Enable entropy parm optimization. */ |
382 | 9.01k | #ifdef ENTROPY_OPT_SUPPORTED |
383 | 9.01k | cinfo->optimize_coding = TRUE; |
384 | | #else |
385 | | fprintf(stderr, "%s: sorry, entropy optimization was not compiled in\n", |
386 | | progname); |
387 | | exit(EXIT_FAILURE); |
388 | | #endif |
389 | | |
390 | 80.3k | } else if (keymatch(arg, "outfile", 4)) { |
391 | | /* Set output file name. */ |
392 | 0 | if (++argn >= argc) /* advance to next argument */ |
393 | 0 | usage(); |
394 | 0 | outfilename = argv[argn]; /* save it away for later use */ |
395 | |
|
396 | 80.3k | } else if (keymatch(arg, "progressive", 1)) { |
397 | | /* Select simple progressive mode. */ |
398 | 0 | #ifdef C_PROGRESSIVE_SUPPORTED |
399 | 0 | simple_progressive = TRUE; |
400 | | /* We must postpone execution until num_components is known. */ |
401 | | #else |
402 | | fprintf(stderr, "%s: sorry, progressive output was not compiled in\n", |
403 | | progname); |
404 | | exit(EXIT_FAILURE); |
405 | | #endif |
406 | |
|
407 | 80.3k | } else if (keymatch(arg, "memdst", 2)) { |
408 | | /* Use in-memory destination manager */ |
409 | 18.0k | #if JPEG_LIB_VERSION >= 80 || defined(MEM_SRCDST_SUPPORTED) |
410 | 18.0k | memdst = TRUE; |
411 | | #else |
412 | | fprintf(stderr, "%s: sorry, in-memory destination manager was not compiled in\n", |
413 | | progname); |
414 | | exit(EXIT_FAILURE); |
415 | | #endif |
416 | | |
417 | 62.3k | } else if (keymatch(arg, "quality", 1)) { |
418 | | /* Quality ratings (quantization table scaling factors). */ |
419 | 18.0k | if (++argn >= argc) /* advance to next argument */ |
420 | 0 | usage(); |
421 | 18.0k | qualityarg = argv[argn]; |
422 | | |
423 | 44.2k | } else if (keymatch(arg, "qslots", 2)) { |
424 | | /* Quantization table slot numbers. */ |
425 | 0 | if (++argn >= argc) /* advance to next argument */ |
426 | 0 | usage(); |
427 | 0 | qslotsarg = argv[argn]; |
428 | | /* Must delay setting qslots until after we have processed any |
429 | | * colorspace-determining switches, since jpeg_set_colorspace sets |
430 | | * default quant table numbers. |
431 | | */ |
432 | |
|
433 | 44.2k | } else if (keymatch(arg, "qtables", 2)) { |
434 | | /* Quantization tables fetched from file. */ |
435 | 0 | if (++argn >= argc) /* advance to next argument */ |
436 | 0 | usage(); |
437 | 0 | qtablefile = argv[argn]; |
438 | | /* We postpone actually reading the file in case -quality comes later. */ |
439 | |
|
440 | 44.2k | } else if (keymatch(arg, "restart", 1)) { |
441 | | /* Restart interval in MCU rows (or in MCUs with 'b'). */ |
442 | 9.01k | long lval; |
443 | 9.01k | char ch = 'x'; |
444 | | |
445 | 9.01k | if (++argn >= argc) /* advance to next argument */ |
446 | 0 | usage(); |
447 | 9.01k | if (sscanf(argv[argn], "%ld%c", &lval, &ch) < 1) |
448 | 0 | usage(); |
449 | 9.01k | if (lval < 0 || lval > 65535L) |
450 | 0 | usage(); |
451 | 9.01k | if (ch == 'b' || ch == 'B') { |
452 | 0 | cinfo->restart_interval = (unsigned int)lval; |
453 | 0 | cinfo->restart_in_rows = 0; /* else prior '-restart n' overrides me */ |
454 | 9.01k | } else { |
455 | 9.01k | cinfo->restart_in_rows = (int)lval; |
456 | | /* restart_interval will be computed during startup */ |
457 | 9.01k | } |
458 | | |
459 | 35.2k | } else if (keymatch(arg, "sample", 2)) { |
460 | | /* Set sampling factors. */ |
461 | 18.0k | if (++argn >= argc) /* advance to next argument */ |
462 | 0 | usage(); |
463 | 18.0k | samplearg = argv[argn]; |
464 | | /* Must delay setting sample factors until after we have processed any |
465 | | * colorspace-determining switches, since jpeg_set_colorspace sets |
466 | | * default sampling factors. |
467 | | */ |
468 | | |
469 | 18.0k | } else if (keymatch(arg, "scans", 4)) { |
470 | | /* Set scan script. */ |
471 | 0 | #ifdef C_MULTISCAN_FILES_SUPPORTED |
472 | 0 | if (++argn >= argc) /* advance to next argument */ |
473 | 0 | usage(); |
474 | 0 | scansarg = argv[argn]; |
475 | | /* We must postpone reading the file in case -progressive appears. */ |
476 | | #else |
477 | | fprintf(stderr, "%s: sorry, multi-scan output was not compiled in\n", |
478 | | progname); |
479 | | exit(EXIT_FAILURE); |
480 | | #endif |
481 | |
|
482 | 17.2k | } else if (keymatch(arg, "smooth", 2)) { |
483 | | /* Set input smoothing factor. */ |
484 | 9.01k | int val; |
485 | | |
486 | 9.01k | if (++argn >= argc) /* advance to next argument */ |
487 | 0 | usage(); |
488 | 9.01k | if (sscanf(argv[argn], "%d", &val) != 1) |
489 | 0 | usage(); |
490 | 9.01k | if (val < 0 || val > 100) |
491 | 0 | usage(); |
492 | 9.01k | cinfo->smoothing_factor = val; |
493 | | |
494 | 9.01k | } else if (keymatch(arg, "targa", 1)) { |
495 | | /* Input file is Targa format. */ |
496 | 8.23k | is_targa = TRUE; |
497 | | |
498 | 8.23k | } else { |
499 | 0 | usage(); /* bogus switch */ |
500 | 0 | } |
501 | 125k | } |
502 | | |
503 | | /* Post-switch-scanning cleanup */ |
504 | | |
505 | 18.0k | if (for_real) { |
506 | | |
507 | | /* Set quantization tables for selected quality. */ |
508 | | /* Some or all may be overridden if -qtables is present. */ |
509 | 5.66k | if (qualityarg != NULL) /* process -quality if it was present */ |
510 | 5.66k | if (!set_quality_ratings(cinfo, qualityarg, force_baseline)) |
511 | 0 | usage(); |
512 | | |
513 | 5.66k | if (qtablefile != NULL) /* process -qtables if it was present */ |
514 | 0 | if (!read_quant_tables(cinfo, qtablefile, force_baseline)) |
515 | 0 | usage(); |
516 | | |
517 | 5.66k | if (qslotsarg != NULL) /* process -qslots if it was present */ |
518 | 0 | if (!set_quant_slots(cinfo, qslotsarg)) |
519 | 0 | usage(); |
520 | | |
521 | 5.66k | if (samplearg != NULL) /* process -sample if it was present */ |
522 | 5.66k | if (!set_sample_factors(cinfo, samplearg)) |
523 | 0 | usage(); |
524 | | |
525 | 5.66k | #ifdef C_PROGRESSIVE_SUPPORTED |
526 | 5.66k | if (simple_progressive) /* process -progressive; -scans can override */ |
527 | 0 | jpeg_simple_progression(cinfo); |
528 | 5.66k | #endif |
529 | | |
530 | 5.66k | #ifdef C_MULTISCAN_FILES_SUPPORTED |
531 | 5.66k | if (scansarg != NULL) /* process -scans if it was present */ |
532 | 0 | if (!read_scan_script(cinfo, scansarg)) |
533 | 0 | usage(); |
534 | 5.66k | #endif |
535 | 5.66k | } |
536 | | |
537 | 18.0k | return argn; /* return index of next arg (file name) */ |
538 | 18.0k | } |
539 | | |
540 | | |
541 | | /* |
542 | | * The main program. |
543 | | */ |
544 | | |
545 | | int |
546 | | main(int argc, char **argv) |
547 | 12.3k | { |
548 | 12.3k | struct jpeg_compress_struct cinfo; |
549 | 12.3k | #ifdef CJPEG_FUZZER |
550 | 12.3k | struct my_error_mgr myerr; |
551 | 12.3k | struct jpeg_error_mgr &jerr = myerr.pub; |
552 | | #else |
553 | | struct jpeg_error_mgr jerr; |
554 | | #endif |
555 | | #ifdef PROGRESS_REPORT |
556 | | struct cdjpeg_progress_mgr progress; |
557 | | #endif |
558 | 12.3k | int file_index; |
559 | 12.3k | cjpeg_source_ptr src_mgr; |
560 | 12.3k | FILE *input_file = NULL; |
561 | 12.3k | FILE *icc_file; |
562 | 12.3k | JOCTET *icc_profile = NULL; |
563 | 12.3k | long icc_len = 0; |
564 | 12.3k | FILE *output_file = NULL; |
565 | 12.3k | unsigned char *outbuffer = NULL; |
566 | 12.3k | unsigned long outsize = 0; |
567 | 12.3k | JDIMENSION num_scanlines; |
568 | | |
569 | | /* On Mac, fetch a command line. */ |
570 | | #ifdef USE_CCOMMAND |
571 | | argc = ccommand(&argv); |
572 | | #endif |
573 | | |
574 | 12.3k | progname = argv[0]; |
575 | 12.3k | if (progname == NULL || progname[0] == 0) |
576 | 0 | progname = "cjpeg"; /* in case C library doesn't provide it */ |
577 | | |
578 | | /* Initialize the JPEG compression object with default error handling. */ |
579 | 12.3k | cinfo.err = jpeg_std_error(&jerr); |
580 | 12.3k | jpeg_create_compress(&cinfo); |
581 | | /* Add some application-specific error messages (from cderror.h) */ |
582 | 12.3k | jerr.addon_message_table = cdjpeg_message_table; |
583 | 12.3k | jerr.first_addon_message = JMSG_FIRSTADDONCODE; |
584 | 12.3k | jerr.last_addon_message = JMSG_LASTADDONCODE; |
585 | | |
586 | | /* Initialize JPEG parameters. |
587 | | * Much of this may be overridden later. |
588 | | * In particular, we don't yet know the input file's color space, |
589 | | * but we need to provide some value for jpeg_set_defaults() to work. |
590 | | */ |
591 | | |
592 | 12.3k | cinfo.in_color_space = JCS_RGB; /* arbitrary guess */ |
593 | 12.3k | jpeg_set_defaults(&cinfo); |
594 | | |
595 | | /* Scan command line to find file names. |
596 | | * It is convenient to use just one switch-parsing routine, but the switch |
597 | | * values read here are ignored; we will rescan the switches after opening |
598 | | * the input file. |
599 | | */ |
600 | | |
601 | 12.3k | file_index = parse_switches(&cinfo, argc, argv, 0, FALSE); |
602 | | |
603 | | #ifdef TWO_FILE_COMMANDLINE |
604 | | if (!memdst) { |
605 | | /* Must have either -outfile switch or explicit output file name */ |
606 | | if (outfilename == NULL) { |
607 | | if (file_index != argc - 2) { |
608 | | fprintf(stderr, "%s: must name one input and one output file\n", |
609 | | progname); |
610 | | usage(); |
611 | | } |
612 | | outfilename = argv[file_index + 1]; |
613 | | } else { |
614 | | if (file_index != argc - 1) { |
615 | | fprintf(stderr, "%s: must name one input and one output file\n", |
616 | | progname); |
617 | | usage(); |
618 | | } |
619 | | } |
620 | | } |
621 | | #else |
622 | | /* Unix style: expect zero or one file name */ |
623 | 12.3k | if (file_index < argc - 1) { |
624 | 0 | fprintf(stderr, "%s: only one input file\n", progname); |
625 | 0 | usage(); |
626 | 0 | } |
627 | 12.3k | #endif /* TWO_FILE_COMMANDLINE */ |
628 | | |
629 | | /* Open the input file. */ |
630 | 12.3k | if (file_index < argc) { |
631 | 12.3k | if ((input_file = fopen(argv[file_index], READ_BINARY)) == NULL) { |
632 | 0 | fprintf(stderr, "%s: can't open %s\n", progname, argv[file_index]); |
633 | 0 | exit(EXIT_FAILURE); |
634 | 0 | } |
635 | 12.3k | } else { |
636 | | /* default input file is stdin */ |
637 | 0 | input_file = read_stdin(); |
638 | 0 | } |
639 | | |
640 | | /* Open the output file. */ |
641 | 12.3k | if (outfilename != NULL) { |
642 | 0 | if ((output_file = fopen(outfilename, WRITE_BINARY)) == NULL) { |
643 | 0 | fprintf(stderr, "%s: can't open %s\n", progname, outfilename); |
644 | 0 | exit(EXIT_FAILURE); |
645 | 0 | } |
646 | 12.3k | } else if (!memdst) { |
647 | | /* default output file is stdout */ |
648 | 0 | output_file = write_stdout(); |
649 | 0 | } |
650 | | |
651 | 12.3k | if (icc_filename != NULL) { |
652 | 0 | if ((icc_file = fopen(icc_filename, READ_BINARY)) == NULL) { |
653 | 0 | fprintf(stderr, "%s: can't open %s\n", progname, icc_filename); |
654 | 0 | exit(EXIT_FAILURE); |
655 | 0 | } |
656 | 0 | if (fseek(icc_file, 0, SEEK_END) < 0 || |
657 | 0 | (icc_len = ftell(icc_file)) < 1 || |
658 | 0 | fseek(icc_file, 0, SEEK_SET) < 0) { |
659 | 0 | fprintf(stderr, "%s: can't determine size of %s\n", progname, |
660 | 0 | icc_filename); |
661 | 0 | exit(EXIT_FAILURE); |
662 | 0 | } |
663 | 0 | if ((icc_profile = (JOCTET *)malloc(icc_len)) == NULL) { |
664 | 0 | fprintf(stderr, "%s: can't allocate memory for ICC profile\n", progname); |
665 | 0 | fclose(icc_file); |
666 | 0 | exit(EXIT_FAILURE); |
667 | 0 | } |
668 | 0 | if (fread(icc_profile, icc_len, 1, icc_file) < 1) { |
669 | 0 | fprintf(stderr, "%s: can't read ICC profile from %s\n", progname, |
670 | 0 | icc_filename); |
671 | 0 | free(icc_profile); |
672 | 0 | fclose(icc_file); |
673 | 0 | exit(EXIT_FAILURE); |
674 | 0 | } |
675 | 0 | fclose(icc_file); |
676 | 0 | } |
677 | | |
678 | | #ifdef PROGRESS_REPORT |
679 | | start_progress_monitor((j_common_ptr)&cinfo, &progress); |
680 | | #endif |
681 | 12.3k | #ifdef CJPEG_FUZZER |
682 | 12.3k | jerr.error_exit = my_error_exit; |
683 | 12.3k | jerr.emit_message = my_emit_message; |
684 | 12.3k | if (setjmp(myerr.setjmp_buffer)) |
685 | 6.70k | HANDLE_ERROR() |
686 | 5.66k | #endif |
687 | | |
688 | | /* Figure out the input file format, and set up to read it. */ |
689 | 5.66k | src_mgr = select_file_type(&cinfo, input_file); |
690 | 5.66k | src_mgr->input_file = input_file; |
691 | 5.66k | #ifdef CJPEG_FUZZER |
692 | 5.66k | src_mgr->max_pixels = 1048576; |
693 | 5.66k | #endif |
694 | | |
695 | | /* Read the input file header to obtain file size & colorspace. */ |
696 | 5.66k | (*src_mgr->start_input) (&cinfo, src_mgr); |
697 | | |
698 | | /* Now that we know input colorspace, fix colorspace-dependent defaults */ |
699 | 5.66k | jpeg_default_colorspace(&cinfo); |
700 | | |
701 | | /* Adjust default compression parameters by re-parsing the options */ |
702 | 5.66k | file_index = parse_switches(&cinfo, argc, argv, 0, TRUE); |
703 | | |
704 | | /* Specify data destination for compression */ |
705 | 5.66k | #if JPEG_LIB_VERSION >= 80 || defined(MEM_SRCDST_SUPPORTED) |
706 | 5.66k | if (memdst) |
707 | 5.66k | jpeg_mem_dest(&cinfo, &outbuffer, &outsize); |
708 | 0 | else |
709 | 0 | #endif |
710 | 0 | jpeg_stdio_dest(&cinfo, output_file); |
711 | | |
712 | 5.66k | #ifdef CJPEG_FUZZER |
713 | 5.66k | if (setjmp(myerr.setjmp_buffer)) |
714 | 2.75k | HANDLE_ERROR() |
715 | 2.90k | #endif |
716 | | |
717 | | /* Start compressor */ |
718 | 2.90k | jpeg_start_compress(&cinfo, TRUE); |
719 | | |
720 | 2.90k | if (icc_profile != NULL) |
721 | 0 | jpeg_write_icc_profile(&cinfo, icc_profile, (unsigned int)icc_len); |
722 | | |
723 | | /* Process data */ |
724 | 5.71M | while (cinfo.next_scanline < cinfo.image_height) { |
725 | 5.71M | num_scanlines = (*src_mgr->get_pixel_rows) (&cinfo, src_mgr); |
726 | 5.71M | (void)jpeg_write_scanlines(&cinfo, src_mgr->buffer, num_scanlines); |
727 | 5.71M | } |
728 | | |
729 | | /* Finish compression and release memory */ |
730 | 2.90k | (*src_mgr->finish_input) (&cinfo, src_mgr); |
731 | 2.90k | jpeg_finish_compress(&cinfo); |
732 | 2.90k | jpeg_destroy_compress(&cinfo); |
733 | | |
734 | | /* Close files, if we opened them */ |
735 | 2.90k | if (input_file != stdin) |
736 | 2.90k | fclose(input_file); |
737 | 2.90k | if (output_file != stdout && output_file != NULL) |
738 | 0 | fclose(output_file); |
739 | | |
740 | | #ifdef PROGRESS_REPORT |
741 | | end_progress_monitor((j_common_ptr)&cinfo); |
742 | | #endif |
743 | | |
744 | 2.90k | if (memdst) { |
745 | | #ifndef CJPEG_FUZZER |
746 | | fprintf(stderr, "Compressed size: %lu bytes\n", outsize); |
747 | | #endif |
748 | 2.90k | free(outbuffer); |
749 | 2.90k | } |
750 | | |
751 | 2.90k | free(icc_profile); |
752 | | |
753 | | /* All done. */ |
754 | 2.90k | return (jerr.num_warnings ? EXIT_WARNING : EXIT_SUCCESS); |
755 | 5.66k | } |