etlegacy-libs/jpegturbo/cdjpeg.c

144 lines
3.9 KiB
C
Raw Normal View History

2014-11-21 20:32:31 +00:00
/*
* cdjpeg.c
*
2015-01-24 20:09:39 +00:00
* This file was part of the Independent JPEG Group's software:
2014-11-21 20:32:31 +00:00
* Copyright (C) 1991-1997, Thomas G. Lane.
2015-01-24 20:09:39 +00:00
* It was modified by The libjpeg-turbo Project to include only code relevant
* to libjpeg-turbo.
2014-11-21 20:32:31 +00:00
* For conditions of distribution and use, see the accompanying README file.
*
* This file contains common support routines used by the IJG application
* programs (cjpeg, djpeg, jpegtran).
*/
2015-01-24 20:09:39 +00:00
#include "cdjpeg.h" /* Common decls for cjpeg/djpeg applications */
#include <ctype.h> /* to declare isupper(), tolower() */
2014-11-21 20:32:31 +00:00
#ifdef USE_SETMODE
2015-01-24 20:09:39 +00:00
#include <fcntl.h> /* to declare setmode()'s parameter macros */
2014-11-21 20:32:31 +00:00
/* If you have setmode() but not <io.h>, just delete this line: */
2015-01-24 20:09:39 +00:00
#include <io.h> /* to declare setmode() */
2014-11-21 20:32:31 +00:00
#endif
/*
* Optional progress monitor: display a percent-done figure on stderr.
*/
#ifdef PROGRESS_REPORT
METHODDEF(void)
progress_monitor (j_common_ptr cinfo)
{
cd_progress_ptr prog = (cd_progress_ptr) cinfo->progress;
int total_passes = prog->pub.total_passes + prog->total_extra_passes;
int percent_done = (int) (prog->pub.pass_counter*100L/prog->pub.pass_limit);
if (percent_done != prog->percent_done) {
prog->percent_done = percent_done;
if (total_passes > 1) {
fprintf(stderr, "\rPass %d/%d: %3d%% ",
2015-01-24 20:09:39 +00:00
prog->pub.completed_passes + prog->completed_extra_passes + 1,
total_passes, percent_done);
2014-11-21 20:32:31 +00:00
} else {
fprintf(stderr, "\r %3d%% ", percent_done);
}
fflush(stderr);
}
}
GLOBAL(void)
start_progress_monitor (j_common_ptr cinfo, cd_progress_ptr progress)
{
/* Enable progress display, unless trace output is on */
if (cinfo->err->trace_level == 0) {
progress->pub.progress_monitor = progress_monitor;
progress->completed_extra_passes = 0;
progress->total_extra_passes = 0;
progress->percent_done = -1;
cinfo->progress = &progress->pub;
}
}
GLOBAL(void)
end_progress_monitor (j_common_ptr cinfo)
{
/* Clear away progress display */
if (cinfo->err->trace_level == 0) {
fprintf(stderr, "\r \r");
fflush(stderr);
}
}
#endif
/*
* Case-insensitive matching of possibly-abbreviated keyword switches.
* keyword is the constant keyword (must be lower case already),
* minchars is length of minimum legal abbreviation.
*/
GLOBAL(boolean)
keymatch (char * arg, const char * keyword, int minchars)
{
register int ca, ck;
register int nmatched = 0;
while ((ca = *arg++) != '\0') {
if ((ck = *keyword++) == '\0')
2015-01-24 20:09:39 +00:00
return FALSE; /* arg longer than keyword, no good */
if (isupper(ca)) /* force arg to lcase (assume ck is already) */
2014-11-21 20:32:31 +00:00
ca = tolower(ca);
if (ca != ck)
2015-01-24 20:09:39 +00:00
return FALSE; /* no good */
nmatched++; /* count matched characters */
2014-11-21 20:32:31 +00:00
}
/* reached end of argument; fail if it's too short for unique abbrev */
if (nmatched < minchars)
return FALSE;
2015-01-24 20:09:39 +00:00
return TRUE; /* A-OK */
2014-11-21 20:32:31 +00:00
}
/*
* Routines to establish binary I/O mode for stdin and stdout.
* Non-Unix systems often require some hacking to get out of text mode.
*/
GLOBAL(FILE *)
read_stdin (void)
{
FILE * input_file = stdin;
2015-01-24 20:09:39 +00:00
#ifdef USE_SETMODE /* need to hack file mode? */
2014-11-21 20:32:31 +00:00
setmode(fileno(stdin), O_BINARY);
#endif
2015-01-24 20:09:39 +00:00
#ifdef USE_FDOPEN /* need to re-open in binary mode? */
2014-11-21 20:32:31 +00:00
if ((input_file = fdopen(fileno(stdin), READ_BINARY)) == NULL) {
fprintf(stderr, "Cannot reopen stdin\n");
exit(EXIT_FAILURE);
}
#endif
return input_file;
}
GLOBAL(FILE *)
write_stdout (void)
{
FILE * output_file = stdout;
2015-01-24 20:09:39 +00:00
#ifdef USE_SETMODE /* need to hack file mode? */
2014-11-21 20:32:31 +00:00
setmode(fileno(stdout), O_BINARY);
#endif
2015-01-24 20:09:39 +00:00
#ifdef USE_FDOPEN /* need to re-open in binary mode? */
2014-11-21 20:32:31 +00:00
if ((output_file = fdopen(fileno(stdout), WRITE_BINARY)) == NULL) {
fprintf(stderr, "Cannot reopen stdout\n");
exit(EXIT_FAILURE);
}
#endif
return output_file;
}