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