/src/libjpeg-turbo.3.0.x/cdjpeg.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * cdjpeg.c |
3 | | * |
4 | | * This file was part of the Independent JPEG Group's software: |
5 | | * Copyright (C) 1991-1997, Thomas G. Lane. |
6 | | * libjpeg-turbo Modifications: |
7 | | * Copyright (C) 2019, 2022, D. R. Commander. |
8 | | * For conditions of distribution and use, see the accompanying README.ijg |
9 | | * file. |
10 | | * |
11 | | * This file contains common support routines used by the IJG application |
12 | | * programs (cjpeg, djpeg, jpegtran). |
13 | | */ |
14 | | |
15 | | #include "cdjpeg.h" /* Common decls for cjpeg/djpeg applications */ |
16 | | #include <ctype.h> /* to declare isupper(), tolower() */ |
17 | | #ifdef USE_SETMODE |
18 | | #include <fcntl.h> /* to declare setmode()'s parameter macros */ |
19 | | /* If you have setmode() but not <io.h>, just delete this line: */ |
20 | | #include <io.h> /* to declare setmode() */ |
21 | | #endif |
22 | | |
23 | | |
24 | | /* |
25 | | * Optional progress monitor: display a percent-done figure on stderr. |
26 | | */ |
27 | | |
28 | | METHODDEF(void) |
29 | | progress_monitor(j_common_ptr cinfo) |
30 | 0 | { |
31 | 0 | cd_progress_ptr prog = (cd_progress_ptr)cinfo->progress; |
32 | |
|
33 | 0 | if (prog->max_scans != 0 && cinfo->is_decompressor) { |
34 | 0 | int scan_no = ((j_decompress_ptr)cinfo)->input_scan_number; |
35 | |
|
36 | 0 | if (scan_no > (int)prog->max_scans) { |
37 | 0 | fprintf(stderr, "Scan number %d exceeds maximum scans (%u)\n", scan_no, |
38 | 0 | prog->max_scans); |
39 | 0 | exit(EXIT_FAILURE); |
40 | 0 | } |
41 | 0 | } |
42 | | |
43 | 0 | if (prog->report) { |
44 | 0 | int total_passes = prog->pub.total_passes + prog->total_extra_passes; |
45 | 0 | int percent_done = |
46 | 0 | (int)(prog->pub.pass_counter * 100L / prog->pub.pass_limit); |
47 | |
|
48 | 0 | if (percent_done != prog->percent_done) { |
49 | 0 | prog->percent_done = percent_done; |
50 | 0 | if (total_passes > 1) { |
51 | 0 | fprintf(stderr, "\rPass %d/%d: %3d%% ", |
52 | 0 | prog->pub.completed_passes + prog->completed_extra_passes + 1, |
53 | 0 | total_passes, percent_done); |
54 | 0 | } else { |
55 | 0 | fprintf(stderr, "\r %3d%% ", percent_done); |
56 | 0 | } |
57 | 0 | fflush(stderr); |
58 | 0 | } |
59 | 0 | } |
60 | 0 | } |
61 | | |
62 | | |
63 | | GLOBAL(void) |
64 | | start_progress_monitor(j_common_ptr cinfo, cd_progress_ptr progress) |
65 | 0 | { |
66 | | /* Enable progress display, unless trace output is on */ |
67 | 0 | if (cinfo->err->trace_level == 0) { |
68 | 0 | progress->pub.progress_monitor = progress_monitor; |
69 | 0 | progress->completed_extra_passes = 0; |
70 | 0 | progress->total_extra_passes = 0; |
71 | 0 | progress->max_scans = 0; |
72 | 0 | progress->report = FALSE; |
73 | 0 | progress->percent_done = -1; |
74 | 0 | cinfo->progress = &progress->pub; |
75 | 0 | } |
76 | 0 | } |
77 | | |
78 | | |
79 | | GLOBAL(void) |
80 | | end_progress_monitor(j_common_ptr cinfo) |
81 | 0 | { |
82 | | /* Clear away progress display */ |
83 | 0 | if (cinfo->err->trace_level == 0) { |
84 | 0 | fprintf(stderr, "\r \r"); |
85 | 0 | fflush(stderr); |
86 | 0 | } |
87 | 0 | } |
88 | | |
89 | | |
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 | | |
96 | | GLOBAL(boolean) |
97 | | keymatch(char *arg, const char *keyword, int minchars) |
98 | 28.5k | { |
99 | 28.5k | register int ca, ck; |
100 | 28.5k | register int nmatched = 0; |
101 | | |
102 | 40.9k | while ((ca = *arg++) != '\0') { |
103 | 39.0k | if ((ck = *keyword++) == '\0') |
104 | 0 | return FALSE; /* arg longer than keyword, no good */ |
105 | 39.0k | if (isupper(ca)) /* force arg to lcase (assume ck is already) */ |
106 | 0 | ca = tolower(ca); |
107 | 39.0k | if (ca != ck) |
108 | 26.5k | return FALSE; /* no good */ |
109 | 12.4k | nmatched++; /* count matched characters */ |
110 | 12.4k | } |
111 | | /* reached end of argument; fail if it's too short for unique abbrev */ |
112 | 1.95k | if (nmatched < minchars) |
113 | 0 | return FALSE; |
114 | 1.95k | return TRUE; /* A-OK */ |
115 | 1.95k | } |
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 | | |
123 | | GLOBAL(FILE *) |
124 | | read_stdin(void) |
125 | 0 | { |
126 | 0 | FILE *input_file = stdin; |
127 | |
|
128 | | #ifdef USE_SETMODE /* need to hack file mode? */ |
129 | | setmode(fileno(stdin), O_BINARY); |
130 | | #endif |
131 | | #ifdef USE_FDOPEN /* need to re-open in binary mode? */ |
132 | | if ((input_file = fdopen(fileno(stdin), READ_BINARY)) == NULL) { |
133 | | fprintf(stderr, "Cannot reopen stdin\n"); |
134 | | exit(EXIT_FAILURE); |
135 | | } |
136 | | #endif |
137 | 0 | return input_file; |
138 | 0 | } |
139 | | |
140 | | |
141 | | GLOBAL(FILE *) |
142 | | write_stdout(void) |
143 | 0 | { |
144 | 0 | FILE *output_file = stdout; |
145 | |
|
146 | | #ifdef USE_SETMODE /* need to hack file mode? */ |
147 | | setmode(fileno(stdout), O_BINARY); |
148 | | #endif |
149 | | #ifdef USE_FDOPEN /* need to re-open in binary mode? */ |
150 | | if ((output_file = fdopen(fileno(stdout), WRITE_BINARY)) == NULL) { |
151 | | fprintf(stderr, "Cannot reopen stdout\n"); |
152 | | exit(EXIT_FAILURE); |
153 | | } |
154 | | #endif |
155 | 0 | return output_file; |
156 | 0 | } |