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.
163 lines
5.8 KiB
C
163 lines
5.8 KiB
C
/*
|
|
* jcapistd.c
|
|
*
|
|
* Copyright (C) 1994-1996, Thomas G. Lane.
|
|
* This file is part of the Independent JPEG Group's software.
|
|
* For conditions of distribution and use, see the accompanying README.ijg
|
|
* file.
|
|
*
|
|
* This file contains application interface code for the compression half
|
|
* of the JPEG library. These are the "standard" API routines that are
|
|
* used in the normal full-compression case. They are not used by a
|
|
* transcoding-only application. Note that if an application links in
|
|
* jpeg_start_compress, it will end up linking in the entire compressor.
|
|
* We thus must separate this file from jcapimin.c to avoid linking the
|
|
* whole compression library into a transcoder.
|
|
*/
|
|
|
|
#define JPEG_INTERNALS
|
|
#include "jinclude.h"
|
|
#include "jpeglib.h"
|
|
|
|
|
|
/*
|
|
* Compression initialization.
|
|
* Before calling this, all parameters and a data destination must be set up.
|
|
*
|
|
* We require a write_all_tables parameter as a failsafe check when writing
|
|
* multiple datastreams from the same compression object. Since prior runs
|
|
* will have left all the tables marked sent_table=TRUE, a subsequent run
|
|
* would emit an abbreviated stream (no tables) by default. This may be what
|
|
* is wanted, but for safety's sake it should not be the default behavior:
|
|
* programmers should have to make a deliberate choice to emit abbreviated
|
|
* images. Therefore the documentation and examples should encourage people
|
|
* to pass write_all_tables=TRUE; then it will take active thought to do the
|
|
* wrong thing.
|
|
*/
|
|
|
|
GLOBAL(void)
|
|
jpeg_start_compress(j_compress_ptr cinfo, boolean write_all_tables)
|
|
{
|
|
if (cinfo->global_state != CSTATE_START)
|
|
ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
|
|
|
|
if (write_all_tables)
|
|
jpeg_suppress_tables(cinfo, FALSE); /* mark all tables to be written */
|
|
|
|
/* (Re)initialize error mgr and destination modules */
|
|
(*cinfo->err->reset_error_mgr) ((j_common_ptr)cinfo);
|
|
(*cinfo->dest->init_destination) (cinfo);
|
|
/* Perform master selection of active modules */
|
|
jinit_compress_master(cinfo);
|
|
/* Set up for the first pass */
|
|
(*cinfo->master->prepare_for_pass) (cinfo);
|
|
/* Ready for application to drive first pass through jpeg_write_scanlines
|
|
* or jpeg_write_raw_data.
|
|
*/
|
|
cinfo->next_scanline = 0;
|
|
cinfo->global_state = (cinfo->raw_data_in ? CSTATE_RAW_OK : CSTATE_SCANNING);
|
|
}
|
|
|
|
|
|
/*
|
|
* Write some scanlines of data to the JPEG compressor.
|
|
*
|
|
* The return value will be the number of lines actually written.
|
|
* This should be less than the supplied num_lines only in case that
|
|
* the data destination module has requested suspension of the compressor,
|
|
* or if more than image_height scanlines are passed in.
|
|
*
|
|
* Note: we warn about excess calls to jpeg_write_scanlines() since
|
|
* this likely signals an application programmer error. However,
|
|
* excess scanlines passed in the last valid call are *silently* ignored,
|
|
* so that the application need not adjust num_lines for end-of-image
|
|
* when using a multiple-scanline buffer.
|
|
*/
|
|
|
|
GLOBAL(JDIMENSION)
|
|
jpeg_write_scanlines(j_compress_ptr cinfo, JSAMPARRAY scanlines,
|
|
JDIMENSION num_lines)
|
|
{
|
|
JDIMENSION row_ctr, rows_left;
|
|
|
|
if (cinfo->global_state != CSTATE_SCANNING)
|
|
ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
|
|
if (cinfo->next_scanline >= cinfo->image_height)
|
|
WARNMS(cinfo, JWRN_TOO_MUCH_DATA);
|
|
|
|
/* Call progress monitor hook if present */
|
|
if (cinfo->progress != NULL) {
|
|
cinfo->progress->pass_counter = (long)cinfo->next_scanline;
|
|
cinfo->progress->pass_limit = (long)cinfo->image_height;
|
|
(*cinfo->progress->progress_monitor) ((j_common_ptr)cinfo);
|
|
}
|
|
|
|
/* Give master control module another chance if this is first call to
|
|
* jpeg_write_scanlines. This lets output of the frame/scan headers be
|
|
* delayed so that application can write COM, etc, markers between
|
|
* jpeg_start_compress and jpeg_write_scanlines.
|
|
*/
|
|
if (cinfo->master->call_pass_startup)
|
|
(*cinfo->master->pass_startup) (cinfo);
|
|
|
|
/* Ignore any extra scanlines at bottom of image. */
|
|
rows_left = cinfo->image_height - cinfo->next_scanline;
|
|
if (num_lines > rows_left)
|
|
num_lines = rows_left;
|
|
|
|
row_ctr = 0;
|
|
(*cinfo->main->process_data) (cinfo, scanlines, &row_ctr, num_lines);
|
|
cinfo->next_scanline += row_ctr;
|
|
return row_ctr;
|
|
}
|
|
|
|
|
|
/*
|
|
* Alternate entry point to write raw data.
|
|
* Processes exactly one iMCU row per call, unless suspended.
|
|
*/
|
|
|
|
GLOBAL(JDIMENSION)
|
|
jpeg_write_raw_data(j_compress_ptr cinfo, JSAMPIMAGE data,
|
|
JDIMENSION num_lines)
|
|
{
|
|
JDIMENSION lines_per_iMCU_row;
|
|
|
|
if (cinfo->global_state != CSTATE_RAW_OK)
|
|
ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
|
|
if (cinfo->next_scanline >= cinfo->image_height) {
|
|
WARNMS(cinfo, JWRN_TOO_MUCH_DATA);
|
|
return 0;
|
|
}
|
|
|
|
/* Call progress monitor hook if present */
|
|
if (cinfo->progress != NULL) {
|
|
cinfo->progress->pass_counter = (long)cinfo->next_scanline;
|
|
cinfo->progress->pass_limit = (long)cinfo->image_height;
|
|
(*cinfo->progress->progress_monitor) ((j_common_ptr)cinfo);
|
|
}
|
|
|
|
/* Give master control module another chance if this is first call to
|
|
* jpeg_write_raw_data. This lets output of the frame/scan headers be
|
|
* delayed so that application can write COM, etc, markers between
|
|
* jpeg_start_compress and jpeg_write_raw_data.
|
|
*/
|
|
if (cinfo->master->call_pass_startup)
|
|
(*cinfo->master->pass_startup) (cinfo);
|
|
|
|
/* Verify that at least one iMCU row has been passed. */
|
|
lines_per_iMCU_row = cinfo->max_v_samp_factor * DCTSIZE;
|
|
if (num_lines < lines_per_iMCU_row)
|
|
ERREXIT(cinfo, JERR_BUFFER_SIZE);
|
|
|
|
/* Directly compress the row. */
|
|
if (!(*cinfo->coef->compress_data) (cinfo, data)) {
|
|
/* If compressor did not consume the whole row, suspend processing. */
|
|
return 0;
|
|
}
|
|
|
|
/* OK, we processed one iMCU row. */
|
|
cinfo->next_scanline += lines_per_iMCU_row;
|
|
return lines_per_iMCU_row;
|
|
}
|