4 * This file was part of the Independent JPEG Group's software:
5 * Copyright (C) 1991-1997, Thomas G. Lane.
6 * It was modified by The libjpeg-turbo Project to include only code relevant
8 * For conditions of distribution and use, see the accompanying README.ijg
11 * This file contains common support routines used by the IJG application
12 * programs (cjpeg, djpeg, jpegtran).
15 #include "cdjpeg.h" /* Common decls for cjpeg/djpeg applications */
16 #include <ctype.h> /* to declare isupper(), tolower() */
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() */
25 * Optional progress monitor: display a percent-done figure on stderr.
28 #ifdef PROGRESS_REPORT
31 progress_monitor (j_common_ptr cinfo)
33 cd_progress_ptr prog = (cd_progress_ptr) cinfo->progress;
34 int total_passes = prog->pub.total_passes + prog->total_extra_passes;
35 int percent_done = (int) (prog->pub.pass_counter*100L/prog->pub.pass_limit);
37 if (percent_done != prog->percent_done) {
38 prog->percent_done = percent_done;
39 if (total_passes > 1) {
40 fprintf(stderr, "\rPass %d/%d: %3d%% ",
41 prog->pub.completed_passes + prog->completed_extra_passes + 1,
42 total_passes, percent_done);
44 fprintf(stderr, "\r %3d%% ", percent_done);
52 start_progress_monitor (j_common_ptr cinfo, cd_progress_ptr progress)
54 /* Enable progress display, unless trace output is on */
55 if (cinfo->err->trace_level == 0) {
56 progress->pub.progress_monitor = progress_monitor;
57 progress->completed_extra_passes = 0;
58 progress->total_extra_passes = 0;
59 progress->percent_done = -1;
60 cinfo->progress = &progress->pub;
66 end_progress_monitor (j_common_ptr cinfo)
68 /* Clear away progress display */
69 if (cinfo->err->trace_level == 0) {
70 fprintf(stderr, "\r \r");
79 * Case-insensitive matching of possibly-abbreviated keyword switches.
80 * keyword is the constant keyword (must be lower case already),
81 * minchars is length of minimum legal abbreviation.
85 keymatch (char *arg, const char *keyword, int minchars)
88 register int nmatched = 0;
90 while ((ca = *arg++) != '\0') {
91 if ((ck = *keyword++) == '\0')
92 return FALSE; /* arg longer than keyword, no good */
93 if (isupper(ca)) /* force arg to lcase (assume ck is already) */
96 return FALSE; /* no good */
97 nmatched++; /* count matched characters */
99 /* reached end of argument; fail if it's too short for unique abbrev */
100 if (nmatched < minchars)
102 return TRUE; /* A-OK */
107 * Routines to establish binary I/O mode for stdin and stdout.
108 * Non-Unix systems often require some hacking to get out of text mode.
114 FILE * input_file = stdin;
116 #ifdef USE_SETMODE /* need to hack file mode? */
117 setmode(fileno(stdin), O_BINARY);
119 #ifdef USE_FDOPEN /* need to re-open in binary mode? */
120 if ((input_file = fdopen(fileno(stdin), READ_BINARY)) == NULL) {
121 fprintf(stderr, "Cannot reopen stdin\n");
132 FILE * output_file = stdout;
134 #ifdef USE_SETMODE /* need to hack file mode? */
135 setmode(fileno(stdout), O_BINARY);
137 #ifdef USE_FDOPEN /* need to re-open in binary mode? */
138 if ((output_file = fdopen(fileno(stdout), WRITE_BINARY)) == NULL) {
139 fprintf(stderr, "Cannot reopen stdout\n");