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