With rare exceptions ...
- Always separate line continuation characters by one space from
preceding code.
- Always use two-space indentation. Never use tabs.
- Always use K&R-style conditional blocks.
- Always surround operators with spaces, except in raw assembly code.
- Always put a space after, but not before, a comma.
- Never put a space between type casts and variables/function calls.
- Never put a space between the function name and the argument list in
function declarations and prototypes.
- Always surround braces ('{' and '}') with spaces.
- Always surround statements (if, for, else, catch, while, do, switch)
with spaces.
- Always attach pointer symbols ('*' and '**') to the variable or
function name.
- Always precede pointer symbols ('*' and '**') by a space in type
casts.
- Use the MIN() macro from jpegint.h within the libjpeg and TurboJPEG
API libraries (using min() from tjutil.h is still necessary for
TJBench.)
- Where it makes sense (particularly in the TurboJPEG code), put a blank
line after variable declaration blocks.
- Always separate statements in one-liners by two spaces.
The purpose of this was to ease maintenance on my part and also to make
it easier for contributors to figure out how to format patch
submissions. This was admittedly confusing (even to me sometimes) when
we had 3 or 4 different style conventions in the same source tree. The
new convention is more consistent with the formatting of other OSS code
bases.
This commit corrects deviations from the chosen formatting style in the
libjpeg API code and reformats the TurboJPEG API code such that it
conforms to the same standard.
NOTES:
- Although it is no longer necessary for the function name in function
declarations to begin in Column 1 (this was historically necessary
because of the ansi2knr utility, which allowed libjpeg to be built
with non-ANSI compilers), we retain that formatting for the libjpeg
code because it improves readability when using libjpeg's function
attribute macros (GLOBAL(), etc.)
- This reformatting project was accomplished with the help of AStyle and
Uncrustify, although neither was completely up to the task, and thus
a great deal of manual tweaking was required. Note to developers of
code formatting utilities: the libjpeg-turbo code base is an
excellent test bed, because AFAICT, it breaks every single one of the
utilities that are currently available.
- The legacy (MMX, SSE, 3DNow!) assembly code for i386 has been
formatted to match the SSE2 code (refer to
ff5685d5344273df321eb63a005eaae19d2496e3.) I hadn't intended to
bother with this, but the Loongson MMI implementation demonstrated
that there is still academic value to the MMX implementation, as an
algorithmic model for other 64-bit vector implementations. Thus, it
is desirable to improve its readability in the same manner as that of
the SSE2 implementation.
146 lines
3.9 KiB
C
146 lines
3.9 KiB
C
/*
|
|
* cdjpeg.c
|
|
*
|
|
* This file was part of the Independent JPEG Group's software:
|
|
* Copyright (C) 1991-1997, Thomas G. Lane.
|
|
* It was modified by The libjpeg-turbo Project to include only code relevant
|
|
* to libjpeg-turbo.
|
|
* For conditions of distribution and use, see the accompanying README.ijg
|
|
* file.
|
|
*
|
|
* This file contains common support routines used by the IJG application
|
|
* programs (cjpeg, djpeg, jpegtran).
|
|
*/
|
|
|
|
#include "cdjpeg.h" /* Common decls for cjpeg/djpeg applications */
|
|
#include <ctype.h> /* to declare isupper(), tolower() */
|
|
#ifdef USE_SETMODE
|
|
#include <fcntl.h> /* to declare setmode()'s parameter macros */
|
|
/* If you have setmode() but not <io.h>, just delete this line: */
|
|
#include <io.h> /* to declare setmode() */
|
|
#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%% ",
|
|
prog->pub.completed_passes + prog->completed_extra_passes + 1,
|
|
total_passes, percent_done);
|
|
} 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')
|
|
return FALSE; /* arg longer than keyword, no good */
|
|
if (isupper(ca)) /* force arg to lcase (assume ck is already) */
|
|
ca = tolower(ca);
|
|
if (ca != ck)
|
|
return FALSE; /* no good */
|
|
nmatched++; /* count matched characters */
|
|
}
|
|
/* reached end of argument; fail if it's too short for unique abbrev */
|
|
if (nmatched < minchars)
|
|
return FALSE;
|
|
return TRUE; /* A-OK */
|
|
}
|
|
|
|
|
|
/*
|
|
* 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;
|
|
|
|
#ifdef USE_SETMODE /* need to hack file mode? */
|
|
setmode(fileno(stdin), O_BINARY);
|
|
#endif
|
|
#ifdef USE_FDOPEN /* need to re-open in binary mode? */
|
|
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;
|
|
|
|
#ifdef USE_SETMODE /* need to hack file mode? */
|
|
setmode(fileno(stdout), O_BINARY);
|
|
#endif
|
|
#ifdef USE_FDOPEN /* need to re-open in binary mode? */
|
|
if ((output_file = fdopen(fileno(stdout), WRITE_BINARY)) == NULL) {
|
|
fprintf(stderr, "Cannot reopen stdout\n");
|
|
exit(EXIT_FAILURE);
|
|
}
|
|
#endif
|
|
return output_file;
|
|
}
|