blob: 5278c1dbef3fbf0c2dc7ebde76152d1cb873b974 [file] [log] [blame]
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +00001/*
2 * cdjpeg.c
3 *
Tom Hudson0d47d2d2016-05-04 13:22:56 -04004 * This file was part of the Independent JPEG Group's software:
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +00005 * Copyright (C) 1991-1997, Thomas G. Lane.
Jonathan Wrightbbb82822020-11-25 13:36:43 +00006 * libjpeg-turbo Modifications:
7 * Copyright (C) 2019, D. R. Commander.
Tom Hudson0d47d2d2016-05-04 13:22:56 -04008 * For conditions of distribution and use, see the accompanying README.ijg
9 * file.
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +000010 *
11 * This file contains common support routines used by the IJG application
12 * programs (cjpeg, djpeg, jpegtran).
13 */
14
Tom Hudson0d47d2d2016-05-04 13:22:56 -040015#include "cdjpeg.h" /* Common decls for cjpeg/djpeg applications */
16#include <ctype.h> /* to declare isupper(), tolower() */
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +000017#ifdef USE_SETMODE
Tom Hudson0d47d2d2016-05-04 13:22:56 -040018#include <fcntl.h> /* to declare setmode()'s parameter macros */
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +000019/* If you have setmode() but not <io.h>, just delete this line: */
Tom Hudson0d47d2d2016-05-04 13:22:56 -040020#include <io.h> /* to declare setmode() */
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +000021#endif
22
23
24/*
25 * Optional progress monitor: display a percent-done figure on stderr.
26 */
27
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +000028METHODDEF(void)
Chris Blumecca8c4d2019-03-01 01:09:50 -080029progress_monitor(j_common_ptr cinfo)
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +000030{
Chris Blumecca8c4d2019-03-01 01:09:50 -080031 cd_progress_ptr prog = (cd_progress_ptr)cinfo->progress;
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +000032
Jonathan Wrightbbb82822020-11-25 13:36:43 +000033 if (prog->max_scans != 0 && cinfo->is_decompressor) {
34 int scan_no = ((j_decompress_ptr)cinfo)->input_scan_number;
35
36 if (scan_no > (int)prog->max_scans) {
37 fprintf(stderr, "Scan number %d exceeds maximum scans (%d)\n", scan_no,
38 prog->max_scans);
39 exit(EXIT_FAILURE);
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +000040 }
Jonathan Wrightbbb82822020-11-25 13:36:43 +000041 }
42
43 if (prog->report) {
44 int total_passes = prog->pub.total_passes + prog->total_extra_passes;
45 int percent_done =
46 (int)(prog->pub.pass_counter * 100L / prog->pub.pass_limit);
47
48 if (percent_done != prog->percent_done) {
49 prog->percent_done = percent_done;
50 if (total_passes > 1) {
51 fprintf(stderr, "\rPass %d/%d: %3d%% ",
52 prog->pub.completed_passes + prog->completed_extra_passes + 1,
53 total_passes, percent_done);
54 } else {
55 fprintf(stderr, "\r %3d%% ", percent_done);
56 }
57 fflush(stderr);
58 }
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +000059 }
60}
61
62
63GLOBAL(void)
Chris Blumecca8c4d2019-03-01 01:09:50 -080064start_progress_monitor(j_common_ptr cinfo, cd_progress_ptr progress)
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +000065{
66 /* Enable progress display, unless trace output is on */
67 if (cinfo->err->trace_level == 0) {
68 progress->pub.progress_monitor = progress_monitor;
69 progress->completed_extra_passes = 0;
70 progress->total_extra_passes = 0;
Jonathan Wrightbbb82822020-11-25 13:36:43 +000071 progress->max_scans = 0;
72 progress->report = FALSE;
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +000073 progress->percent_done = -1;
74 cinfo->progress = &progress->pub;
75 }
76}
77
78
79GLOBAL(void)
Chris Blumecca8c4d2019-03-01 01:09:50 -080080end_progress_monitor(j_common_ptr cinfo)
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +000081{
82 /* Clear away progress display */
83 if (cinfo->err->trace_level == 0) {
84 fprintf(stderr, "\r \r");
85 fflush(stderr);
86 }
87}
88
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +000089
90/*
91 * Case-insensitive matching of possibly-abbreviated keyword switches.
92 * keyword is the constant keyword (must be lower case already),
93 * minchars is length of minimum legal abbreviation.
94 */
95
96GLOBAL(boolean)
Chris Blumecca8c4d2019-03-01 01:09:50 -080097keymatch(char *arg, const char *keyword, int minchars)
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +000098{
99 register int ca, ck;
100 register int nmatched = 0;
101
102 while ((ca = *arg++) != '\0') {
103 if ((ck = *keyword++) == '\0')
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400104 return FALSE; /* arg longer than keyword, no good */
105 if (isupper(ca)) /* force arg to lcase (assume ck is already) */
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000106 ca = tolower(ca);
107 if (ca != ck)
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400108 return FALSE; /* no good */
109 nmatched++; /* count matched characters */
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000110 }
111 /* reached end of argument; fail if it's too short for unique abbrev */
112 if (nmatched < minchars)
113 return FALSE;
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400114 return TRUE; /* A-OK */
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000115}
116
117
118/*
119 * Routines to establish binary I/O mode for stdin and stdout.
120 * Non-Unix systems often require some hacking to get out of text mode.
121 */
122
123GLOBAL(FILE *)
Chris Blumecca8c4d2019-03-01 01:09:50 -0800124read_stdin(void)
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000125{
Chris Blumecca8c4d2019-03-01 01:09:50 -0800126 FILE *input_file = stdin;
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000127
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400128#ifdef USE_SETMODE /* need to hack file mode? */
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000129 setmode(fileno(stdin), O_BINARY);
130#endif
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400131#ifdef USE_FDOPEN /* need to re-open in binary mode? */
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000132 if ((input_file = fdopen(fileno(stdin), READ_BINARY)) == NULL) {
133 fprintf(stderr, "Cannot reopen stdin\n");
134 exit(EXIT_FAILURE);
135 }
136#endif
137 return input_file;
138}
139
140
141GLOBAL(FILE *)
Chris Blumecca8c4d2019-03-01 01:09:50 -0800142write_stdout(void)
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000143{
Chris Blumecca8c4d2019-03-01 01:09:50 -0800144 FILE *output_file = stdout;
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000145
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400146#ifdef USE_SETMODE /* need to hack file mode? */
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000147 setmode(fileno(stdout), O_BINARY);
148#endif
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400149#ifdef USE_FDOPEN /* need to re-open in binary mode? */
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000150 if ((output_file = fdopen(fileno(stdout), WRITE_BINARY)) == NULL) {
151 fprintf(stderr, "Cannot reopen stdout\n");
152 exit(EXIT_FAILURE);
153 }
154#endif
155 return output_file;
156}