Skip to content

Commit

Permalink
Improve code formatting consistency
Browse files Browse the repository at this point in the history
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
  ff5685d.)  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.
  • Loading branch information
dcommander committed Mar 16, 2018
1 parent 4508ab3 commit 19c791c
Show file tree
Hide file tree
Showing 264 changed files with 30,484 additions and 30,722 deletions.
8 changes: 4 additions & 4 deletions README.ijg
Original file line number Diff line number Diff line change
Expand Up @@ -179,8 +179,8 @@ We recommend reading one or more of these references before trying to
understand the innards of the JPEG software.

The best short technical introduction to the JPEG compression algorithm is
Wallace, Gregory K. "The JPEG Still Picture Compression Standard",
Communications of the ACM, April 1991 (vol. 34 no. 4), pp. 30-44.
Wallace, Gregory K. "The JPEG Still Picture Compression Standard",
Communications of the ACM, April 1991 (vol. 34 no. 4), pp. 30-44.
(Adjacent articles in that issue discuss MPEG motion picture compression,
applications of JPEG, and related topics.) If you don't have the CACM issue
handy, a PDF file containing a revised version of Wallace's article is
Expand Down Expand Up @@ -249,8 +249,8 @@ and other news.answers archive sites, including the official news.answers
archive at rtfm.mit.edu: ftp://rtfm.mit.edu/pub/usenet/news.answers/jpeg-faq/.
If you don't have Web or FTP access, send e-mail to [email protected]
with body
send usenet/news.answers/jpeg-faq/part1
send usenet/news.answers/jpeg-faq/part2
send usenet/news.answers/jpeg-faq/part1
send usenet/news.answers/jpeg-faq/part2


FILE FORMAT WARS
Expand Down
6 changes: 3 additions & 3 deletions cderror.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,19 @@
#define JMAKE_ENUM_LIST
#else
/* Repeated inclusions of this file are no-ops unless JMESSAGE is defined */
#define JMESSAGE(code,string)
#define JMESSAGE(code, string)
#endif /* CDERROR_H */
#endif /* JMESSAGE */

#ifdef JMAKE_ENUM_LIST

typedef enum {

#define JMESSAGE(code,string) code ,
#define JMESSAGE(code, string) code,

#endif /* JMAKE_ENUM_LIST */

JMESSAGE(JMSG_FIRSTADDONCODE=1000, NULL) /* Must be first entry! */
JMESSAGE(JMSG_FIRSTADDONCODE = 1000, NULL) /* Must be first entry! */

#ifdef BMP_SUPPORTED
JMESSAGE(JERR_BMP_BADCMAP, "Unsupported BMP colormap format")
Expand Down
21 changes: 11 additions & 10 deletions cdjpeg.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,12 @@
#ifdef PROGRESS_REPORT

METHODDEF(void)
progress_monitor (j_common_ptr cinfo)
progress_monitor(j_common_ptr cinfo)
{
cd_progress_ptr prog = (cd_progress_ptr) cinfo->progress;
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);
int percent_done =
(int)(prog->pub.pass_counter * 100L / prog->pub.pass_limit);

if (percent_done != prog->percent_done) {
prog->percent_done = percent_done;
Expand All @@ -49,7 +50,7 @@ progress_monitor (j_common_ptr cinfo)


GLOBAL(void)
start_progress_monitor (j_common_ptr cinfo, cd_progress_ptr progress)
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) {
Expand All @@ -63,7 +64,7 @@ start_progress_monitor (j_common_ptr cinfo, cd_progress_ptr progress)


GLOBAL(void)
end_progress_monitor (j_common_ptr cinfo)
end_progress_monitor(j_common_ptr cinfo)
{
/* Clear away progress display */
if (cinfo->err->trace_level == 0) {
Expand All @@ -82,7 +83,7 @@ end_progress_monitor (j_common_ptr cinfo)
*/

GLOBAL(boolean)
keymatch (char *arg, const char *keyword, int minchars)
keymatch(char *arg, const char *keyword, int minchars)
{
register int ca, ck;
register int nmatched = 0;
Expand All @@ -109,9 +110,9 @@ keymatch (char *arg, const char *keyword, int minchars)
*/

GLOBAL(FILE *)
read_stdin (void)
read_stdin(void)
{
FILE * input_file = stdin;
FILE *input_file = stdin;

#ifdef USE_SETMODE /* need to hack file mode? */
setmode(fileno(stdin), O_BINARY);
Expand All @@ -127,9 +128,9 @@ read_stdin (void)


GLOBAL(FILE *)
write_stdout (void)
write_stdout(void)
{
FILE * output_file = stdout;
FILE *output_file = stdout;

#ifdef USE_SETMODE /* need to hack file mode? */
setmode(fileno(stdout), O_BINARY);
Expand Down
55 changes: 27 additions & 28 deletions cdjpeg.h
Original file line number Diff line number Diff line change
Expand Up @@ -96,43 +96,42 @@ typedef struct cdjpeg_progress_mgr *cd_progress_ptr;

/* Module selection routines for I/O modules. */

EXTERN(cjpeg_source_ptr) jinit_read_bmp (j_compress_ptr cinfo,
boolean use_inversion_array);
EXTERN(djpeg_dest_ptr) jinit_write_bmp (j_decompress_ptr cinfo,
boolean is_os2,
EXTERN(cjpeg_source_ptr) jinit_read_bmp(j_compress_ptr cinfo,
boolean use_inversion_array);
EXTERN(cjpeg_source_ptr) jinit_read_gif (j_compress_ptr cinfo);
EXTERN(djpeg_dest_ptr) jinit_write_gif (j_decompress_ptr cinfo);
EXTERN(cjpeg_source_ptr) jinit_read_ppm (j_compress_ptr cinfo);
EXTERN(djpeg_dest_ptr) jinit_write_ppm (j_decompress_ptr cinfo);
EXTERN(cjpeg_source_ptr) jinit_read_rle (j_compress_ptr cinfo);
EXTERN(djpeg_dest_ptr) jinit_write_rle (j_decompress_ptr cinfo);
EXTERN(cjpeg_source_ptr) jinit_read_targa (j_compress_ptr cinfo);
EXTERN(djpeg_dest_ptr) jinit_write_targa (j_decompress_ptr cinfo);
EXTERN(djpeg_dest_ptr) jinit_write_bmp(j_decompress_ptr cinfo, boolean is_os2,
boolean use_inversion_array);
EXTERN(cjpeg_source_ptr) jinit_read_gif(j_compress_ptr cinfo);
EXTERN(djpeg_dest_ptr) jinit_write_gif(j_decompress_ptr cinfo);
EXTERN(cjpeg_source_ptr) jinit_read_ppm(j_compress_ptr cinfo);
EXTERN(djpeg_dest_ptr) jinit_write_ppm(j_decompress_ptr cinfo);
EXTERN(cjpeg_source_ptr) jinit_read_rle(j_compress_ptr cinfo);
EXTERN(djpeg_dest_ptr) jinit_write_rle(j_decompress_ptr cinfo);
EXTERN(cjpeg_source_ptr) jinit_read_targa(j_compress_ptr cinfo);
EXTERN(djpeg_dest_ptr) jinit_write_targa(j_decompress_ptr cinfo);

/* cjpeg support routines (in rdswitch.c) */

EXTERN(boolean) read_quant_tables (j_compress_ptr cinfo, char *filename,
boolean force_baseline);
EXTERN(boolean) read_scan_script (j_compress_ptr cinfo, char *filename);
EXTERN(boolean) set_quality_ratings (j_compress_ptr cinfo, char *arg,
boolean force_baseline);
EXTERN(boolean) set_quant_slots (j_compress_ptr cinfo, char *arg);
EXTERN(boolean) set_sample_factors (j_compress_ptr cinfo, char *arg);
EXTERN(boolean) read_quant_tables(j_compress_ptr cinfo, char *filename,
boolean force_baseline);
EXTERN(boolean) read_scan_script(j_compress_ptr cinfo, char *filename);
EXTERN(boolean) set_quality_ratings(j_compress_ptr cinfo, char *arg,
boolean force_baseline);
EXTERN(boolean) set_quant_slots(j_compress_ptr cinfo, char *arg);
EXTERN(boolean) set_sample_factors(j_compress_ptr cinfo, char *arg);

/* djpeg support routines (in rdcolmap.c) */

EXTERN(void) read_color_map (j_decompress_ptr cinfo, FILE *infile);
EXTERN(void) read_color_map(j_decompress_ptr cinfo, FILE *infile);

/* common support routines (in cdjpeg.c) */

EXTERN(void) enable_signal_catcher (j_common_ptr cinfo);
EXTERN(void) start_progress_monitor (j_common_ptr cinfo,
cd_progress_ptr progress);
EXTERN(void) end_progress_monitor (j_common_ptr cinfo);
EXTERN(boolean) keymatch (char *arg, const char *keyword, int minchars);
EXTERN(FILE *) read_stdin (void);
EXTERN(FILE *) write_stdout (void);
EXTERN(void) enable_signal_catcher(j_common_ptr cinfo);
EXTERN(void) start_progress_monitor(j_common_ptr cinfo,
cd_progress_ptr progress);
EXTERN(void) end_progress_monitor(j_common_ptr cinfo);
EXTERN(boolean) keymatch(char *arg, const char *keyword, int minchars);
EXTERN(FILE *) read_stdin(void);
EXTERN(FILE *) write_stdout(void);

/* miscellaneous useful macros */

Expand All @@ -154,5 +153,5 @@ EXTERN(FILE *) write_stdout (void);
#define EXIT_WARNING 2
#endif

#define IsExtRGB(cs) \
#define IsExtRGB(cs) \
(cs == JCS_RGB || (cs >= JCS_EXT_RGB && cs <= JCS_EXT_ARGB))
2 changes: 1 addition & 1 deletion cjpeg.1
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ compressing a grayscale BMP file, because
.B cjpeg
isn't bright enough to notice whether a BMP file uses only shades of gray.
By saying
.BR \-grayscale ,
.BR \-grayscale,
you'll get a smaller JPEG file that takes less time to process.
.TP
.B \-rgb
Expand Down
49 changes: 25 additions & 24 deletions cjpeg.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@
#include "jconfigint.h"

#ifndef HAVE_STDLIB_H /* <stdlib.h> should declare malloc(),free() */
extern void *malloc (size_t size);
extern void free (void *ptr);
extern void *malloc(size_t size);
extern void free(void *ptr);
#endif

#ifdef USE_CCOMMAND /* command-line reader for Macintosh */
Expand All @@ -49,7 +49,7 @@ extern void free (void *ptr);

/* Create the add-on message string table. */

#define JMESSAGE(code,string) string ,
#define JMESSAGE(code, string) string,

static const char * const cdjpeg_message_table[] = {
#include "cderror.h"
Expand Down Expand Up @@ -87,7 +87,7 @@ static boolean is_targa; /* records user -targa switch */


LOCAL(cjpeg_source_ptr)
select_file_type (j_compress_ptr cinfo, FILE *infile)
select_file_type(j_compress_ptr cinfo, FILE *infile)
{
int c;

Expand Down Expand Up @@ -150,7 +150,7 @@ boolean memdst; /* for -memdst switch */


LOCAL(void)
usage (void)
usage(void)
/* complain about bad command line */
{
fprintf(stderr, "usage: %s [switches] ", progname);
Expand Down Expand Up @@ -215,8 +215,8 @@ usage (void)


LOCAL(int)
parse_switches (j_compress_ptr cinfo, int argc, char **argv,
int last_file_arg_seen, boolean for_real)
parse_switches(j_compress_ptr cinfo, int argc, char **argv,
int last_file_arg_seen, boolean for_real)
/* Parse optional switches.
* Returns argv[] index of first file-name argument (== argc if none).
* Any file names with indexes <= last_file_arg_seen are ignored;
Expand Down Expand Up @@ -292,7 +292,7 @@ parse_switches (j_compress_ptr cinfo, int argc, char **argv,
/* On first -d, print version identification */
static boolean printed_version = FALSE;

if (! printed_version) {
if (!printed_version) {
fprintf(stderr, "%s version %s (build %s)\n",
PACKAGE_NAME, VERSION, BUILD);
fprintf(stderr, "%s\n\n", JCOPYRIGHT);
Expand All @@ -307,7 +307,8 @@ parse_switches (j_compress_ptr cinfo, int argc, char **argv,
PACKAGE_NAME, VERSION, BUILD);
exit(EXIT_SUCCESS);

} else if (keymatch(arg, "grayscale", 2) || keymatch(arg, "greyscale",2)) {
} else if (keymatch(arg, "grayscale", 2) ||
keymatch(arg, "greyscale", 2)) {
/* Force a monochrome JPEG file to be generated. */
jpeg_set_colorspace(cinfo, JCS_GRAYSCALE);

Expand Down Expand Up @@ -406,10 +407,10 @@ parse_switches (j_compress_ptr cinfo, int argc, char **argv,
if (lval < 0 || lval > 65535L)
usage();
if (ch == 'b' || ch == 'B') {
cinfo->restart_interval = (unsigned int) lval;
cinfo->restart_interval = (unsigned int)lval;
cinfo->restart_in_rows = 0; /* else prior '-restart n' overrides me */
} else {
cinfo->restart_in_rows = (int) lval;
cinfo->restart_in_rows = (int)lval;
/* restart_interval will be computed during startup */
}

Expand Down Expand Up @@ -464,19 +465,19 @@ parse_switches (j_compress_ptr cinfo, int argc, char **argv,
/* Set quantization tables for selected quality. */
/* Some or all may be overridden if -qtables is present. */
if (qualityarg != NULL) /* process -quality if it was present */
if (! set_quality_ratings(cinfo, qualityarg, force_baseline))
if (!set_quality_ratings(cinfo, qualityarg, force_baseline))
usage();

if (qtablefile != NULL) /* process -qtables if it was present */
if (! read_quant_tables(cinfo, qtablefile, force_baseline))
if (!read_quant_tables(cinfo, qtablefile, force_baseline))
usage();

if (qslotsarg != NULL) /* process -qslots if it was present */
if (! set_quant_slots(cinfo, qslotsarg))
if (!set_quant_slots(cinfo, qslotsarg))
usage();

if (samplearg != NULL) /* process -sample if it was present */
if (! set_sample_factors(cinfo, samplearg))
if (!set_sample_factors(cinfo, samplearg))
usage();

#ifdef C_PROGRESSIVE_SUPPORTED
Expand All @@ -486,7 +487,7 @@ parse_switches (j_compress_ptr cinfo, int argc, char **argv,

#ifdef C_MULTISCAN_FILES_SUPPORTED
if (scansarg != NULL) /* process -scans if it was present */
if (! read_scan_script(cinfo, scansarg))
if (!read_scan_script(cinfo, scansarg))
usage();
#endif
}
Expand All @@ -500,7 +501,7 @@ parse_switches (j_compress_ptr cinfo, int argc, char **argv,
*/

int
main (int argc, char **argv)
main(int argc, char **argv)
{
struct jpeg_compress_struct cinfo;
struct jpeg_error_mgr jerr;
Expand Down Expand Up @@ -556,14 +557,14 @@ main (int argc, char **argv)
if (!memdst) {
/* Must have either -outfile switch or explicit output file name */
if (outfilename == NULL) {
if (file_index != argc-2) {
if (file_index != argc - 2) {
fprintf(stderr, "%s: must name one input and one output file\n",
progname);
usage();
}
outfilename = argv[file_index+1];
outfilename = argv[file_index + 1];
} else {
if (file_index != argc-1) {
if (file_index != argc - 1) {
fprintf(stderr, "%s: must name one input and one output file\n",
progname);
usage();
Expand All @@ -572,7 +573,7 @@ main (int argc, char **argv)
}
#else
/* Unix style: expect zero or one file name */
if (file_index < argc-1) {
if (file_index < argc - 1) {
fprintf(stderr, "%s: only one input file\n", progname);
usage();
}
Expand Down Expand Up @@ -628,7 +629,7 @@ main (int argc, char **argv)
}

#ifdef PROGRESS_REPORT
start_progress_monitor((j_common_ptr) &cinfo, &progress);
start_progress_monitor((j_common_ptr)&cinfo, &progress);
#endif

/* Figure out the input file format, and set up to read it. */
Expand Down Expand Up @@ -661,7 +662,7 @@ main (int argc, char **argv)
/* Process data */
while (cinfo.next_scanline < cinfo.image_height) {
num_scanlines = (*src_mgr->get_pixel_rows) (&cinfo, src_mgr);
(void) jpeg_write_scanlines(&cinfo, src_mgr->buffer, num_scanlines);
(void)jpeg_write_scanlines(&cinfo, src_mgr->buffer, num_scanlines);
}

/* Finish compression and release memory */
Expand All @@ -676,7 +677,7 @@ main (int argc, char **argv)
fclose(output_file);

#ifdef PROGRESS_REPORT
end_progress_monitor((j_common_ptr) &cinfo);
end_progress_monitor((j_common_ptr)&cinfo);
#endif

if (memdst) {
Expand Down
Loading

0 comments on commit 19c791c

Please sign in to comment.