4 * This file was part of the Independent JPEG Group's software:
5 * Copyright (C) 1991-1997, Thomas G. Lane.
6 * Modified 2013-2019 by Guido Vollbeding.
7 * libjpeg-turbo Modifications:
8 * Copyright (C) 2010-2011, 2013-2017, 2019-2020, 2022, D. R. Commander.
9 * Copyright (C) 2015, Google, Inc.
10 * For conditions of distribution and use, see the accompanying README.ijg
13 * This file contains a command-line user interface for the JPEG decompressor.
14 * It should work on any system with Unix- or MS-DOS-style command lines.
16 * Two different command line styles are permitted, depending on the
17 * compile-time switch TWO_FILE_COMMANDLINE:
18 * djpeg [options] inputfile outputfile
19 * djpeg [options] [inputfile]
20 * In the second style, output is always to standard output, which you'd
21 * normally redirect to a file or pipe to some other program. Input is
22 * either from a named file or from standard input (typically redirected).
23 * The second style is convenient on Unix but is unhelpful on systems that
24 * don't support pipes. Also, you MUST use the first style if your system
25 * doesn't do binary I/O to stdin/stdout.
26 * To simplify script writing, the "-outfile" switch is provided. The syntax
27 * djpeg [options] -outfile outputfile inputfile
28 * works regardless of which command line style is used.
32 #define _CRT_SECURE_NO_DEPRECATE
35 #include "cdjpeg.h" /* Common decls for cjpeg/djpeg applications */
36 #include "jversion.h" /* for version message */
37 #include "jconfigint.h"
39 #include <ctype.h> /* to declare isprint() */
42 /* Create the add-on message string table. */
44 #define JMESSAGE(code, string) string,
46 static const char * const cdjpeg_message_table[] = {
53 * This list defines the known output image formats
54 * (not all of which need be supported by a given version).
55 * You can change the default output format by defining DEFAULT_FMT;
56 * indeed, you had better do so if you undefine PPM_SUPPORTED.
60 FMT_BMP, /* BMP format (Windows flavor) */
61 FMT_GIF, /* GIF format (LZW-compressed) */
62 FMT_GIF0, /* GIF format (uncompressed) */
63 FMT_OS2, /* BMP format (OS/2 flavor) */
64 FMT_PPM, /* PPM/PGM (PBMPLUS formats) */
65 FMT_TARGA, /* Targa format */
66 FMT_TIFF /* TIFF format */
69 #ifndef DEFAULT_FMT /* so can override from CFLAGS in Makefile */
70 #define DEFAULT_FMT FMT_PPM
73 static IMAGE_FORMATS requested_fmt;
77 * Argument-parsing code.
78 * The switch parser is designed to be useful with DOS-style command line
79 * syntax, ie, intermixed switches and file names, where only the switches
80 * to the left of a given file name affect processing of that file.
81 * The main program in this file doesn't actually use this capability...
85 static const char *progname; /* program name for error messages */
86 static char *icc_filename; /* for -icc switch */
87 JDIMENSION max_scans; /* for -maxscans switch */
88 static char *outfilename; /* for -outfile switch */
89 boolean memsrc; /* for -memsrc switch */
90 boolean report; /* for -report switch */
92 JDIMENSION skip_start, skip_end;
93 JDIMENSION crop_x, crop_y, crop_width, crop_height;
94 boolean strict; /* for -strict switch */
95 #define INPUT_BUF_SIZE 4096
100 /* complain about bad command line */
102 fprintf(stderr, "usage: %s [switches] ", progname);
103 #ifdef TWO_FILE_COMMANDLINE
104 fprintf(stderr, "inputfile outputfile\n");
106 fprintf(stderr, "[inputfile]\n");
109 fprintf(stderr, "Switches (names may be abbreviated):\n");
110 fprintf(stderr, " -colors N Reduce image to no more than N colors\n");
111 fprintf(stderr, " -fast Fast, low-quality processing\n");
112 fprintf(stderr, " -grayscale Force grayscale output\n");
113 fprintf(stderr, " -rgb Force RGB output\n");
114 fprintf(stderr, " -rgb565 Force RGB565 output\n");
115 #ifdef IDCT_SCALING_SUPPORTED
116 fprintf(stderr, " -scale M/N Scale output image by fraction M/N, eg, 1/8\n");
119 fprintf(stderr, " -bmp Select BMP output format (Windows style)%s\n",
120 (DEFAULT_FMT == FMT_BMP ? " (default)" : ""));
123 fprintf(stderr, " -gif Select GIF output format (LZW-compressed)%s\n",
124 (DEFAULT_FMT == FMT_GIF ? " (default)" : ""));
125 fprintf(stderr, " -gif0 Select GIF output format (uncompressed)%s\n",
126 (DEFAULT_FMT == FMT_GIF0 ? " (default)" : ""));
129 fprintf(stderr, " -os2 Select BMP output format (OS/2 style)%s\n",
130 (DEFAULT_FMT == FMT_OS2 ? " (default)" : ""));
133 fprintf(stderr, " -pnm Select PBMPLUS (PPM/PGM) output format%s\n",
134 (DEFAULT_FMT == FMT_PPM ? " (default)" : ""));
136 #ifdef TARGA_SUPPORTED
137 fprintf(stderr, " -targa Select Targa output format%s\n",
138 (DEFAULT_FMT == FMT_TARGA ? " (default)" : ""));
140 fprintf(stderr, "Switches for advanced users:\n");
141 #ifdef DCT_ISLOW_SUPPORTED
142 fprintf(stderr, " -dct int Use accurate integer DCT method%s\n",
143 (JDCT_DEFAULT == JDCT_ISLOW ? " (default)" : ""));
145 #ifdef DCT_IFAST_SUPPORTED
146 fprintf(stderr, " -dct fast Use less accurate integer DCT method [legacy feature]%s\n",
147 (JDCT_DEFAULT == JDCT_IFAST ? " (default)" : ""));
149 #ifdef DCT_FLOAT_SUPPORTED
150 fprintf(stderr, " -dct float Use floating-point DCT method [legacy feature]%s\n",
151 (JDCT_DEFAULT == JDCT_FLOAT ? " (default)" : ""));
153 fprintf(stderr, " -dither fs Use F-S dithering (default)\n");
154 fprintf(stderr, " -dither none Don't use dithering in quantization\n");
155 fprintf(stderr, " -dither ordered Use ordered dither (medium speed, quality)\n");
156 fprintf(stderr, " -icc FILE Extract ICC profile to FILE\n");
157 #ifdef QUANT_2PASS_SUPPORTED
158 fprintf(stderr, " -map FILE Map to colors used in named image file\n");
160 fprintf(stderr, " -nosmooth Don't use high-quality upsampling\n");
161 #ifdef QUANT_1PASS_SUPPORTED
162 fprintf(stderr, " -onepass Use 1-pass quantization (fast, low quality)\n");
164 fprintf(stderr, " -maxmemory N Maximum memory to use (in kbytes)\n");
165 fprintf(stderr, " -maxscans N Maximum number of scans to allow in input file\n");
166 fprintf(stderr, " -outfile name Specify name for output file\n");
167 #if JPEG_LIB_VERSION >= 80 || defined(MEM_SRCDST_SUPPORTED)
168 fprintf(stderr, " -memsrc Load input file into memory before decompressing\n");
170 fprintf(stderr, " -report Report decompression progress\n");
171 fprintf(stderr, " -skip Y0,Y1 Decompress all rows except those between Y0 and Y1 (inclusive)\n");
172 fprintf(stderr, " -crop WxH+X+Y Decompress only a rectangular subregion of the image\n");
173 fprintf(stderr, " [requires PBMPLUS (PPM/PGM), GIF, or Targa output format]\n");
174 fprintf(stderr, " -strict Treat all warnings as fatal\n");
175 fprintf(stderr, " -verbose or -debug Emit debug output\n");
176 fprintf(stderr, " -version Print version information and exit\n");
182 parse_switches(j_decompress_ptr cinfo, int argc, char **argv,
183 int last_file_arg_seen, boolean for_real)
184 /* Parse optional switches.
185 * Returns argv[] index of first file-name argument (== argc if none).
186 * Any file names with indexes <= last_file_arg_seen are ignored;
187 * they have presumably been processed in a previous iteration.
188 * (Pass 0 for last_file_arg_seen on the first or only iteration.)
189 * for_real is FALSE on the first (dummy) pass; we may skip any expensive
196 /* Set up default JPEG parameters. */
197 requested_fmt = DEFAULT_FMT; /* set default output file format */
206 cinfo->err->trace_level = 0;
208 /* Scan command line options, adjust parameters */
210 for (argn = 1; argn < argc; argn++) {
213 /* Not a switch, must be a file name argument */
214 if (argn <= last_file_arg_seen) {
215 outfilename = NULL; /* -outfile applies to just one input file */
216 continue; /* ignore this name if previously processed */
218 break; /* else done parsing switches */
220 arg++; /* advance past switch marker character */
222 if (keymatch(arg, "bmp", 1)) {
223 /* BMP output format (Windows flavor). */
224 requested_fmt = FMT_BMP;
226 } else if (keymatch(arg, "colors", 1) || keymatch(arg, "colours", 1) ||
227 keymatch(arg, "quantize", 1) || keymatch(arg, "quantise", 1)) {
228 /* Do color quantization. */
231 if (++argn >= argc) /* advance to next argument */
233 if (sscanf(argv[argn], "%d", &val) != 1)
235 cinfo->desired_number_of_colors = val;
236 cinfo->quantize_colors = TRUE;
238 } else if (keymatch(arg, "dct", 2)) {
239 /* Select IDCT algorithm. */
240 if (++argn >= argc) /* advance to next argument */
242 if (keymatch(argv[argn], "int", 1)) {
243 cinfo->dct_method = JDCT_ISLOW;
244 } else if (keymatch(argv[argn], "fast", 2)) {
245 cinfo->dct_method = JDCT_IFAST;
246 } else if (keymatch(argv[argn], "float", 2)) {
247 cinfo->dct_method = JDCT_FLOAT;
251 } else if (keymatch(arg, "dither", 2)) {
252 /* Select dithering algorithm. */
253 if (++argn >= argc) /* advance to next argument */
255 if (keymatch(argv[argn], "fs", 2)) {
256 cinfo->dither_mode = JDITHER_FS;
257 } else if (keymatch(argv[argn], "none", 2)) {
258 cinfo->dither_mode = JDITHER_NONE;
259 } else if (keymatch(argv[argn], "ordered", 2)) {
260 cinfo->dither_mode = JDITHER_ORDERED;
264 } else if (keymatch(arg, "debug", 1) || keymatch(arg, "verbose", 1)) {
265 /* Enable debug printouts. */
266 /* On first -d, print version identification */
267 static boolean printed_version = FALSE;
269 if (!printed_version) {
270 fprintf(stderr, "%s version %s (build %s)\n",
271 PACKAGE_NAME, VERSION, BUILD);
272 fprintf(stderr, "%s\n\n", JCOPYRIGHT);
273 fprintf(stderr, "Emulating The Independent JPEG Group's software, version %s\n\n",
275 printed_version = TRUE;
277 cinfo->err->trace_level++;
279 } else if (keymatch(arg, "version", 4)) {
280 fprintf(stderr, "%s version %s (build %s)\n",
281 PACKAGE_NAME, VERSION, BUILD);
284 } else if (keymatch(arg, "fast", 1)) {
285 /* Select recommended processing options for quick-and-dirty output. */
286 cinfo->two_pass_quantize = FALSE;
287 cinfo->dither_mode = JDITHER_ORDERED;
288 if (!cinfo->quantize_colors) /* don't override an earlier -colors */
289 cinfo->desired_number_of_colors = 216;
290 cinfo->dct_method = JDCT_FASTEST;
291 cinfo->do_fancy_upsampling = FALSE;
293 } else if (keymatch(arg, "gif", 1)) {
294 /* GIF output format (LZW-compressed). */
295 requested_fmt = FMT_GIF;
297 } else if (keymatch(arg, "gif0", 4)) {
298 /* GIF output format (uncompressed). */
299 requested_fmt = FMT_GIF0;
301 } else if (keymatch(arg, "grayscale", 2) ||
302 keymatch(arg, "greyscale", 2)) {
303 /* Force monochrome output. */
304 cinfo->out_color_space = JCS_GRAYSCALE;
306 } else if (keymatch(arg, "rgb", 2)) {
307 /* Force RGB output. */
308 cinfo->out_color_space = JCS_RGB;
310 } else if (keymatch(arg, "rgb565", 2)) {
311 /* Force RGB565 output. */
312 cinfo->out_color_space = JCS_RGB565;
314 } else if (keymatch(arg, "icc", 1)) {
315 /* Set ICC filename. */
316 if (++argn >= argc) /* advance to next argument */
318 icc_filename = argv[argn];
319 jpeg_save_markers(cinfo, JPEG_APP0 + 2, 0xFFFF);
321 } else if (keymatch(arg, "map", 3)) {
322 /* Quantize to a color map taken from an input file. */
323 if (++argn >= argc) /* advance to next argument */
325 if (for_real) { /* too expensive to do twice! */
326 #ifdef QUANT_2PASS_SUPPORTED /* otherwise can't quantize to supplied map */
329 if ((mapfile = fopen(argv[argn], READ_BINARY)) == NULL) {
330 fprintf(stderr, "%s: can't open %s\n", progname, argv[argn]);
333 read_color_map(cinfo, mapfile);
335 cinfo->quantize_colors = TRUE;
337 ERREXIT(cinfo, JERR_NOT_COMPILED);
341 } else if (keymatch(arg, "maxmemory", 3)) {
342 /* Maximum memory in Kb (or Mb with 'm'). */
346 if (++argn >= argc) /* advance to next argument */
348 if (sscanf(argv[argn], "%ld%c", &lval, &ch) < 1)
350 if (ch == 'm' || ch == 'M')
352 cinfo->mem->max_memory_to_use = lval * 1000L;
354 } else if (keymatch(arg, "maxscans", 4)) {
355 if (++argn >= argc) /* advance to next argument */
357 if (sscanf(argv[argn], "%u", &max_scans) != 1)
360 } else if (keymatch(arg, "nosmooth", 3)) {
361 /* Suppress fancy upsampling */
362 cinfo->do_fancy_upsampling = FALSE;
364 } else if (keymatch(arg, "onepass", 3)) {
365 /* Use fast one-pass quantization. */
366 cinfo->two_pass_quantize = FALSE;
368 } else if (keymatch(arg, "os2", 3)) {
369 /* BMP output format (OS/2 flavor). */
370 requested_fmt = FMT_OS2;
372 } else if (keymatch(arg, "outfile", 4)) {
373 /* Set output file name. */
374 if (++argn >= argc) /* advance to next argument */
376 outfilename = argv[argn]; /* save it away for later use */
378 } else if (keymatch(arg, "memsrc", 2)) {
379 /* Use in-memory source manager */
380 #if JPEG_LIB_VERSION >= 80 || defined(MEM_SRCDST_SUPPORTED)
383 fprintf(stderr, "%s: sorry, in-memory source manager was not compiled in\n",
388 } else if (keymatch(arg, "pnm", 1) || keymatch(arg, "ppm", 1)) {
389 /* PPM/PGM output format. */
390 requested_fmt = FMT_PPM;
392 } else if (keymatch(arg, "report", 2)) {
395 } else if (keymatch(arg, "scale", 2)) {
396 /* Scale the output image by a fraction M/N. */
397 if (++argn >= argc) /* advance to next argument */
399 if (sscanf(argv[argn], "%u/%u",
400 &cinfo->scale_num, &cinfo->scale_denom) != 2)
403 } else if (keymatch(arg, "skip", 2)) {
406 if (sscanf(argv[argn], "%u,%u", &skip_start, &skip_end) != 2 ||
407 skip_start > skip_end)
411 } else if (keymatch(arg, "crop", 2)) {
415 if (sscanf(argv[argn], "%u%c%u+%u+%u", &crop_width, &c, &crop_height,
416 &crop_x, &crop_y) != 5 ||
417 (c != 'X' && c != 'x') || crop_width < 1 || crop_height < 1)
421 } else if (keymatch(arg, "strict", 2)) {
424 } else if (keymatch(arg, "targa", 1)) {
425 /* Targa output format. */
426 requested_fmt = FMT_TARGA;
429 usage(); /* bogus switch */
433 return argn; /* return index of next arg (file name) */
438 * Marker processor for COM and interesting APPn markers.
439 * This replaces the library's built-in processor, which just skips the marker.
440 * We want to print out the marker as text, to the extent possible.
441 * Note this code relies on a non-suspending data source.
445 jpeg_getc(j_decompress_ptr cinfo)
448 struct jpeg_source_mgr *datasrc = cinfo->src;
450 if (datasrc->bytes_in_buffer == 0) {
451 if (!(*datasrc->fill_input_buffer) (cinfo))
452 ERREXIT(cinfo, JERR_CANT_SUSPEND);
454 datasrc->bytes_in_buffer--;
455 return *datasrc->next_input_byte++;
460 print_text_marker(j_decompress_ptr cinfo)
462 boolean traceit = (cinfo->err->trace_level >= 1);
465 unsigned int lastch = 0;
467 length = jpeg_getc(cinfo) << 8;
468 length += jpeg_getc(cinfo);
469 length -= 2; /* discount the length word itself */
472 if (cinfo->unread_marker == JPEG_COM)
473 fprintf(stderr, "Comment, length %ld:\n", (long)length);
474 else /* assume it is an APPn otherwise */
475 fprintf(stderr, "APP%d, length %ld:\n",
476 cinfo->unread_marker - JPEG_APP0, (long)length);
479 while (--length >= 0) {
480 ch = jpeg_getc(cinfo);
482 /* Emit the character in a readable form.
483 * Nonprintables are converted to \nnn form,
484 * while \ is converted to \\.
485 * Newlines in CR, CR/LF, or LF form will be printed as one newline.
488 fprintf(stderr, "\n");
489 } else if (ch == '\n') {
491 fprintf(stderr, "\n");
492 } else if (ch == '\\') {
493 fprintf(stderr, "\\\\");
494 } else if (isprint(ch)) {
497 fprintf(stderr, "\\%03o", ch);
504 fprintf(stderr, "\n");
511 my_emit_message(j_common_ptr cinfo, int msg_level)
514 /* Treat warning as fatal */
515 cinfo->err->error_exit(cinfo);
517 if (cinfo->err->trace_level >= msg_level)
518 cinfo->err->output_message(cinfo);
528 main(int argc, char **argv)
530 struct jpeg_decompress_struct cinfo;
531 struct jpeg_error_mgr jerr;
532 struct cdjpeg_progress_mgr progress;
534 djpeg_dest_ptr dest_mgr = NULL;
537 unsigned char *inbuffer = NULL;
538 #if JPEG_LIB_VERSION >= 80 || defined(MEM_SRCDST_SUPPORTED)
539 unsigned long insize = 0;
541 JDIMENSION num_scanlines;
544 if (progname == NULL || progname[0] == 0)
545 progname = "djpeg"; /* in case C library doesn't provide it */
547 /* Initialize the JPEG decompression object with default error handling. */
548 cinfo.err = jpeg_std_error(&jerr);
549 jpeg_create_decompress(&cinfo);
550 /* Add some application-specific error messages (from cderror.h) */
551 jerr.addon_message_table = cdjpeg_message_table;
552 jerr.first_addon_message = JMSG_FIRSTADDONCODE;
553 jerr.last_addon_message = JMSG_LASTADDONCODE;
555 /* Insert custom marker processor for COM and APP12.
556 * APP12 is used by some digital camera makers for textual info,
557 * so we provide the ability to display it as text.
558 * If you like, additional APPn marker types can be selected for display,
559 * but don't try to override APP0 or APP14 this way (see libjpeg.txt).
561 jpeg_set_marker_processor(&cinfo, JPEG_COM, print_text_marker);
562 jpeg_set_marker_processor(&cinfo, JPEG_APP0 + 12, print_text_marker);
564 /* Scan command line to find file names. */
565 /* It is convenient to use just one switch-parsing routine, but the switch
566 * values read here are ignored; we will rescan the switches after opening
568 * (Exception: tracing level set here controls verbosity for COM markers
569 * found during jpeg_read_header...)
572 file_index = parse_switches(&cinfo, argc, argv, 0, FALSE);
575 jerr.emit_message = my_emit_message;
577 #ifdef TWO_FILE_COMMANDLINE
578 /* Must have either -outfile switch or explicit output file name */
579 if (outfilename == NULL) {
580 if (file_index != argc - 2) {
581 fprintf(stderr, "%s: must name one input and one output file\n",
585 outfilename = argv[file_index + 1];
587 if (file_index != argc - 1) {
588 fprintf(stderr, "%s: must name one input and one output file\n",
594 /* Unix style: expect zero or one file name */
595 if (file_index < argc - 1) {
596 fprintf(stderr, "%s: only one input file\n", progname);
599 #endif /* TWO_FILE_COMMANDLINE */
601 /* Open the input file. */
602 if (file_index < argc) {
603 if ((input_file = fopen(argv[file_index], READ_BINARY)) == NULL) {
604 fprintf(stderr, "%s: can't open %s\n", progname, argv[file_index]);
608 /* default input file is stdin */
609 input_file = read_stdin();
612 /* Open the output file. */
613 if (outfilename != NULL) {
614 if ((output_file = fopen(outfilename, WRITE_BINARY)) == NULL) {
615 fprintf(stderr, "%s: can't open %s\n", progname, outfilename);
619 /* default output file is stdout */
620 output_file = write_stdout();
623 if (report || max_scans != 0) {
624 start_progress_monitor((j_common_ptr)&cinfo, &progress);
625 progress.report = report;
626 progress.max_scans = max_scans;
629 /* Specify data source for decompression */
630 #if JPEG_LIB_VERSION >= 80 || defined(MEM_SRCDST_SUPPORTED)
634 inbuffer = (unsigned char *)realloc(inbuffer, insize + INPUT_BUF_SIZE);
635 if (inbuffer == NULL) {
636 fprintf(stderr, "%s: memory allocation failure\n", progname);
639 nbytes = fread(&inbuffer[insize], 1, INPUT_BUF_SIZE, input_file);
640 if (nbytes < INPUT_BUF_SIZE && ferror(input_file)) {
641 if (file_index < argc)
642 fprintf(stderr, "%s: can't read from %s\n", progname,
645 fprintf(stderr, "%s: can't read from stdin\n", progname);
647 insize += (unsigned long)nbytes;
648 } while (nbytes == INPUT_BUF_SIZE);
649 fprintf(stderr, "Compressed size: %lu bytes\n", insize);
650 jpeg_mem_src(&cinfo, inbuffer, insize);
653 jpeg_stdio_src(&cinfo, input_file);
655 /* Read file header, set default decompression parameters */
656 (void)jpeg_read_header(&cinfo, TRUE);
658 /* Adjust default decompression parameters by re-parsing the options */
659 file_index = parse_switches(&cinfo, argc, argv, 0, TRUE);
661 /* Initialize the output module now to let it override any crucial
662 * option settings (for instance, GIF wants to force color quantization).
664 switch (requested_fmt) {
667 dest_mgr = jinit_write_bmp(&cinfo, FALSE, TRUE);
670 dest_mgr = jinit_write_bmp(&cinfo, TRUE, TRUE);
675 dest_mgr = jinit_write_gif(&cinfo, TRUE);
678 dest_mgr = jinit_write_gif(&cinfo, FALSE);
683 dest_mgr = jinit_write_ppm(&cinfo);
686 #ifdef TARGA_SUPPORTED
688 dest_mgr = jinit_write_targa(&cinfo);
692 ERREXIT(&cinfo, JERR_UNSUPPORTED_FORMAT);
695 dest_mgr->output_file = output_file;
697 /* Start decompressor */
698 (void)jpeg_start_decompress(&cinfo);
704 /* Check for valid skip_end. We cannot check this value until after
705 * jpeg_start_decompress() is called. Note that we have already verified
706 * that skip_start <= skip_end.
708 if (skip_end > cinfo.output_height - 1) {
709 fprintf(stderr, "%s: skip region exceeds image height %u\n", progname,
710 cinfo.output_height);
714 /* Write output file header. This is a hack to ensure that the destination
715 * manager creates an output image of the proper size.
717 tmp = cinfo.output_height;
718 cinfo.output_height -= (skip_end - skip_start + 1);
719 (*dest_mgr->start_output) (&cinfo, dest_mgr);
720 cinfo.output_height = tmp;
723 while (cinfo.output_scanline < skip_start) {
724 num_scanlines = jpeg_read_scanlines(&cinfo, dest_mgr->buffer,
725 dest_mgr->buffer_height);
726 (*dest_mgr->put_pixel_rows) (&cinfo, dest_mgr, num_scanlines);
728 if ((tmp = jpeg_skip_scanlines(&cinfo, skip_end - skip_start + 1)) !=
729 skip_end - skip_start + 1) {
730 fprintf(stderr, "%s: jpeg_skip_scanlines() returned %u rather than %u\n",
731 progname, tmp, skip_end - skip_start + 1);
734 while (cinfo.output_scanline < cinfo.output_height) {
735 num_scanlines = jpeg_read_scanlines(&cinfo, dest_mgr->buffer,
736 dest_mgr->buffer_height);
737 (*dest_mgr->put_pixel_rows) (&cinfo, dest_mgr, num_scanlines);
740 /* Decompress a subregion */
744 /* Check for valid crop dimensions. We cannot check these values until
745 * after jpeg_start_decompress() is called.
747 if (crop_x + crop_width > cinfo.output_width ||
748 crop_y + crop_height > cinfo.output_height) {
749 fprintf(stderr, "%s: crop dimensions exceed image dimensions %u x %u\n",
750 progname, cinfo.output_width, cinfo.output_height);
754 jpeg_crop_scanline(&cinfo, &crop_x, &crop_width);
755 if (dest_mgr->calc_buffer_dimensions)
756 (*dest_mgr->calc_buffer_dimensions) (&cinfo, dest_mgr);
758 ERREXIT(&cinfo, JERR_UNSUPPORTED_FORMAT);
760 /* Write output file header. This is a hack to ensure that the destination
761 * manager creates an output image of the proper size.
763 tmp = cinfo.output_height;
764 cinfo.output_height = crop_height;
765 (*dest_mgr->start_output) (&cinfo, dest_mgr);
766 cinfo.output_height = tmp;
769 if ((tmp = jpeg_skip_scanlines(&cinfo, crop_y)) != crop_y) {
770 fprintf(stderr, "%s: jpeg_skip_scanlines() returned %u rather than %u\n",
771 progname, tmp, crop_y);
774 while (cinfo.output_scanline < crop_y + crop_height) {
775 num_scanlines = jpeg_read_scanlines(&cinfo, dest_mgr->buffer,
776 dest_mgr->buffer_height);
777 (*dest_mgr->put_pixel_rows) (&cinfo, dest_mgr, num_scanlines);
780 jpeg_skip_scanlines(&cinfo,
781 cinfo.output_height - crop_y - crop_height)) !=
782 cinfo.output_height - crop_y - crop_height) {
783 fprintf(stderr, "%s: jpeg_skip_scanlines() returned %u rather than %u\n",
784 progname, tmp, cinfo.output_height - crop_y - crop_height);
788 /* Normal full-image decompress */
790 /* Write output file header */
791 (*dest_mgr->start_output) (&cinfo, dest_mgr);
794 while (cinfo.output_scanline < cinfo.output_height) {
795 num_scanlines = jpeg_read_scanlines(&cinfo, dest_mgr->buffer,
796 dest_mgr->buffer_height);
797 (*dest_mgr->put_pixel_rows) (&cinfo, dest_mgr, num_scanlines);
801 /* Hack: count final pass as done in case finish_output does an extra pass.
802 * The library won't have updated completed_passes.
804 if (report || max_scans != 0)
805 progress.pub.completed_passes = progress.pub.total_passes;
807 if (icc_filename != NULL) {
810 unsigned int icc_len;
812 if ((icc_file = fopen(icc_filename, WRITE_BINARY)) == NULL) {
813 fprintf(stderr, "%s: can't open %s\n", progname, icc_filename);
816 if (jpeg_read_icc_profile(&cinfo, &icc_profile, &icc_len)) {
817 if (fwrite(icc_profile, icc_len, 1, icc_file) < 1) {
818 fprintf(stderr, "%s: can't read ICC profile from %s\n", progname,
826 } else if (cinfo.err->msg_code != JWRN_BOGUS_ICC)
827 fprintf(stderr, "%s: no ICC profile data in JPEG file\n", progname);
830 /* Finish decompression and release memory.
831 * I must do it in this order because output module has allocated memory
832 * of lifespan JPOOL_IMAGE; it needs to finish before releasing memory.
834 (*dest_mgr->finish_output) (&cinfo, dest_mgr);
835 (void)jpeg_finish_decompress(&cinfo);
836 jpeg_destroy_decompress(&cinfo);
838 /* Close files, if we opened them */
839 if (input_file != stdin)
841 if (output_file != stdout)
844 if (report || max_scans != 0)
845 end_progress_monitor((j_common_ptr)&cinfo);
851 exit(jerr.num_warnings ? EXIT_WARNING : EXIT_SUCCESS);
852 return 0; /* suppress no-return-value warnings */