Alien-FreeImage
view release on metacpan or search on metacpan
src/Source/LibJPEG/libjpeg.txt view on Meta::CPAN
ADVANCED FEATURES
=================
Compression parameter selection
-------------------------------
This section describes all the optional parameters you can set for JPEG
compression, as well as the "helper" routines provided to assist in this
task. Proper setting of some parameters requires detailed understanding
of the JPEG standard; if you don't know what a parameter is for, it's best
not to mess with it! See REFERENCES in the README file for pointers to
more info about JPEG.
It's a good idea to call jpeg_set_defaults() first, even if you plan to set
all the parameters; that way your code is more likely to work with future JPEG
libraries that have additional parameters. For the same reason, we recommend
you use a helper routine where one is provided, in preference to twiddling
cinfo fields directly.
The helper routines are:
jpeg_set_defaults (j_compress_ptr cinfo)
This routine sets all JPEG parameters to reasonable defaults, using
only the input image's color space (field in_color_space, which must
already be set in cinfo). Many applications will only need to use
this routine and perhaps jpeg_set_quality().
jpeg_set_colorspace (j_compress_ptr cinfo, J_COLOR_SPACE colorspace)
Sets the JPEG file's colorspace (field jpeg_color_space) as specified,
and sets other color-space-dependent parameters appropriately. See
"Special color spaces", below, before using this. A large number of
parameters, including all per-component parameters, are set by this
routine; if you want to twiddle individual parameters you should call
jpeg_set_colorspace() before rather than after.
jpeg_default_colorspace (j_compress_ptr cinfo)
Selects an appropriate JPEG colorspace based on cinfo->in_color_space,
and calls jpeg_set_colorspace(). This is actually a subroutine of
jpeg_set_defaults(). It's broken out in case you want to change
just the colorspace-dependent JPEG parameters.
jpeg_set_quality (j_compress_ptr cinfo, int quality, boolean force_baseline)
Constructs JPEG quantization tables appropriate for the indicated
quality setting. The quality value is expressed on the 0..100 scale
recommended by IJG (cjpeg's "-quality" switch uses this routine).
Note that the exact mapping from quality values to tables may change
in future IJG releases as more is learned about DCT quantization.
If the force_baseline parameter is TRUE, then the quantization table
entries are constrained to the range 1..255 for full JPEG baseline
compatibility. In the current implementation, this only makes a
difference for quality settings below 25, and it effectively prevents
very small/low quality files from being generated. The IJG decoder
is capable of reading the non-baseline files generated at low quality
settings when force_baseline is FALSE, but other decoders may not be.
jpeg_set_linear_quality (j_compress_ptr cinfo, int scale_factor,
boolean force_baseline)
Same as jpeg_set_quality() except that the generated tables are the
sample tables given in the JPEC spec section K.1, multiplied by the
specified scale factor (which is expressed as a percentage; thus
scale_factor = 100 reproduces the spec's tables). Note that larger
scale factors give lower quality. This entry point is useful for
conforming to the Adobe PostScript DCT conventions, but we do not
recommend linear scaling as a user-visible quality scale otherwise.
force_baseline again constrains the computed table entries to 1..255.
int jpeg_quality_scaling (int quality)
Converts a value on the IJG-recommended quality scale to a linear
scaling percentage. Note that this routine may change or go away
in future releases --- IJG may choose to adopt a scaling method that
can't be expressed as a simple scalar multiplier, in which case the
premise of this routine collapses. Caveat user.
jpeg_default_qtables (j_compress_ptr cinfo, boolean force_baseline)
Set default quantization tables with linear q_scale_factor[] values
(see below).
jpeg_add_quant_table (j_compress_ptr cinfo, int which_tbl,
const unsigned int *basic_table,
int scale_factor, boolean force_baseline)
Allows an arbitrary quantization table to be created. which_tbl
indicates which table slot to fill. basic_table points to an array
of 64 unsigned ints given in normal array order. These values are
multiplied by scale_factor/100 and then clamped to the range 1..65535
(or to 1..255 if force_baseline is TRUE).
CAUTION: prior to library version 6a, jpeg_add_quant_table expected
the basic table to be given in JPEG zigzag order. If you need to
write code that works with either older or newer versions of this
routine, you must check the library version number. Something like
"#if JPEG_LIB_VERSION >= 61" is the right test.
jpeg_simple_progression (j_compress_ptr cinfo)
Generates a default scan script for writing a progressive-JPEG file.
This is the recommended method of creating a progressive file,
unless you want to make a custom scan sequence. You must ensure that
the JPEG color space is set correctly before calling this routine.
Compression parameters (cinfo fields) include:
boolean arith_code
If TRUE, use arithmetic coding.
If FALSE, use Huffman coding.
int block_size
Set DCT block size. All N from 1 to 16 are possible.
Default is 8 (baseline format).
Larger values produce higher compression,
smaller values produce higher quality.
An exact DCT stage is possible with 1 or 2.
With the default quality of 75 and default Luminance qtable
the DCT+Quantization stage is lossless for value 1.
Note that values other than 8 require a SmartScale capable decoder,
introduced with IJG JPEG 8. Setting the block_size parameter for
compression works with version 8c and later.
J_DCT_METHOD dct_method
Selects the algorithm used for the DCT step. Choices are:
JDCT_ISLOW: slow but accurate integer algorithm
JDCT_IFAST: faster, less accurate integer method
JDCT_FLOAT: floating-point method
JDCT_DEFAULT: default method (normally JDCT_ISLOW)
JDCT_FASTEST: fastest method (normally JDCT_IFAST)
The FLOAT method is very slightly more accurate than the ISLOW method,
but may give different results on different machines due to varying
roundoff behavior. The integer methods should give the same results
on all machines. On machines with sufficiently fast FP hardware, the
floating-point method may also be the fastest. The IFAST method is
considerably less accurate than the other two; its use is not
recommended if high quality is a concern. JDCT_DEFAULT and
JDCT_FASTEST are macros configurable by each installation.
unsigned int scale_num, scale_denom
Scale the image by the fraction scale_num/scale_denom. Default is
1/1, or no scaling. Currently, the supported scaling ratios are
M/N with all N from 1 to 16, where M is the destination DCT size,
which is 8 by default (see block_size parameter above).
(The library design allows for arbitrary scaling ratios but this
is not likely to be implemented any time soon.)
J_COLOR_SPACE jpeg_color_space
int num_components
The JPEG color space and corresponding number of components; see
"Special color spaces", below, for more info. We recommend using
jpeg_set_colorspace() if you want to change these.
J_COLOR_TRANSFORM color_transform
Internal color transform identifier, writes LSE marker if nonzero
(requires decoder with inverse color transform support, introduced
with IJG JPEG 9).
Two values are currently possible: JCT_NONE and JCT_SUBTRACT_GREEN.
Set this value for lossless RGB application *before* calling
jpeg_set_colorspace(), because entropy table assignment in
jpeg_set_colorspace() depends on color_transform.
boolean optimize_coding
TRUE causes the compressor to compute optimal Huffman coding tables
for the image. This requires an extra pass over the data and
therefore costs a good deal of space and time. The default is
FALSE, which tells the compressor to use the supplied or default
Huffman tables. In most cases optimal tables save only a few percent
of file size compared to the default tables. Note that when this is
TRUE, you need not supply Huffman tables at all, and any you do
supply will be overwritten.
unsigned int restart_interval
int restart_in_rows
To emit restart markers in the JPEG file, set one of these nonzero.
Set restart_interval to specify the exact interval in MCU blocks.
Set restart_in_rows to specify the interval in MCU rows. (If
restart_in_rows is not 0, then restart_interval is set after the
image width in MCUs is computed.) Defaults are zero (no restarts).
One restart marker per MCU row is often a good choice.
NOTE: the overhead of restart markers is higher in grayscale JPEG
files than in color files, and MUCH higher in progressive JPEGs.
If you use restarts, you may want to use larger intervals in those
cases.
const jpeg_scan_info * scan_info
int num_scans
By default, scan_info is NULL; this causes the compressor to write a
single-scan sequential JPEG file. If not NULL, scan_info points to
an array of scan definition records of length num_scans. The
compressor will then write a JPEG file having one scan for each scan
definition record. This is used to generate noninterleaved or
progressive JPEG files. The library checks that the scan array
defines a valid JPEG scan sequence. (jpeg_simple_progression creates
a suitable scan definition array for progressive JPEG.) This is
discussed further under "Progressive JPEG support".
boolean do_fancy_downsampling
If TRUE, use direct DCT scaling with DCT size > 8 for downsampling
of chroma components.
If FALSE, use only DCT size <= 8 and simple separate downsampling.
Default is TRUE.
For better image stability in multiple generation compression cycles
it is preferable that this value matches the corresponding
do_fancy_upsampling value in decompression.
int smoothing_factor
If non-zero, the input image is smoothed; the value should be 1 for
minimal smoothing to 100 for maximum smoothing. Consult jcsample.c
for details of the smoothing algorithm. The default is zero.
boolean write_JFIF_header
If TRUE, a JFIF APP0 marker is emitted. jpeg_set_defaults() and
jpeg_set_colorspace() set this TRUE if a JFIF-legal JPEG color space
(ie, YCbCr or grayscale) is selected, otherwise FALSE.
UINT8 JFIF_major_version
UINT8 JFIF_minor_version
The version number to be written into the JFIF marker.
jpeg_set_defaults() initializes the version to 1.01 (major=minor=1).
You should set it to 1.02 (major=1, minor=2) if you plan to write
any JFIF 1.02 extension markers.
UINT8 density_unit
UINT16 X_density
UINT16 Y_density
The resolution information to be written into the JFIF marker;
not used otherwise. density_unit may be 0 for unknown,
1 for dots/inch, or 2 for dots/cm. The default values are 0,1,1
indicating square pixels of unknown size.
boolean write_Adobe_marker
If TRUE, an Adobe APP14 marker is emitted. jpeg_set_defaults() and
jpeg_set_colorspace() set this TRUE if JPEG color space RGB, CMYK,
or YCCK is selected, otherwise FALSE. It is generally a bad idea
to set both write_JFIF_header and write_Adobe_marker. In fact,
you probably shouldn't change the default settings at all --- the
default behavior ensures that the JPEG file's color space can be
recognized by the decoder.
JQUANT_TBL * quant_tbl_ptrs[NUM_QUANT_TBLS]
Pointers to coefficient quantization tables, one per table slot,
or NULL if no table is defined for a slot. Usually these should
be set via one of the above helper routines; jpeg_add_quant_table()
is general enough to define any quantization table. The other
routines will set up table slot 0 for luminance quality and table
slot 1 for chrominance.
int q_scale_factor[NUM_QUANT_TBLS]
Linear quantization scaling factors (percentage, initialized 100)
for use with jpeg_default_qtables().
See rdswitch.c and cjpeg.c for an example of usage.
Note that the q_scale_factor[] fields are the "linear" scales, so you
have to convert from user-defined ratings via jpeg_quality_scaling().
Here is an example code which corresponds to cjpeg -quality 90,70:
jpeg_set_defaults(cinfo);
/* Set luminance quality 90. */
cinfo->q_scale_factor[0] = jpeg_quality_scaling(90);
/* Set chrominance quality 70. */
cinfo->q_scale_factor[1] = jpeg_quality_scaling(70);
jpeg_default_qtables(cinfo, force_baseline);
CAUTION: You must also set 1x1 subsampling for efficient separate
color quality selection, since the default value used by library
is 2x2:
cinfo->comp_info[0].v_samp_factor = 1;
cinfo->comp_info[0].h_samp_factor = 1;
JHUFF_TBL * dc_huff_tbl_ptrs[NUM_HUFF_TBLS]
JHUFF_TBL * ac_huff_tbl_ptrs[NUM_HUFF_TBLS]
Pointers to Huffman coding tables, one per table slot, or NULL if
no table is defined for a slot. Slots 0 and 1 are filled with the
JPEG sample tables by jpeg_set_defaults(). If you need to allocate
more table structures, jpeg_alloc_huff_table() may be used.
Note that optimal Huffman tables can be computed for an image
by setting optimize_coding, as discussed above; there's seldom
any need to mess with providing your own Huffman tables.
The actual dimensions of the JPEG image that will be written to the file are
given by the following fields. These are computed from the input image
dimensions and the compression parameters by jpeg_start_compress(). You can
also call jpeg_calc_jpeg_dimensions() to obtain the values that will result
from the current parameter settings. This can be useful if you are trying
to pick a scaling ratio that will get close to a desired target size.
JDIMENSION jpeg_width Actual dimensions of output image.
JDIMENSION jpeg_height
Per-component parameters are stored in the struct cinfo.comp_info[i] for
component number i. Note that components here refer to components of the
JPEG color space, *not* the source image color space. A suitably large
comp_info[] array is allocated by jpeg_set_defaults(); if you choose not
to use that routine, it's up to you to allocate the array.
int component_id
The one-byte identifier code to be recorded in the JPEG file for
this component. For the standard color spaces, we recommend you
leave the default values alone.
int h_samp_factor
int v_samp_factor
Horizontal and vertical sampling factors for the component; must
be 1..4 according to the JPEG standard. Note that larger sampling
factors indicate a higher-resolution component; many people find
src/Source/LibJPEG/libjpeg.txt view on Meta::CPAN
If you use a suspending data source, jpeg_read_coefficients() will return
NULL if it is forced to suspend; a non-NULL return value indicates successful
completion. You need not test for a NULL return value when using a
non-suspending data source.
It is also possible to call jpeg_read_coefficients() to obtain access to the
decoder's coefficient arrays during a normal decode cycle in buffered-image
mode. This frammish might be useful for progressively displaying an incoming
image and then re-encoding it without loss. To do this, decode in buffered-
image mode as discussed previously, then call jpeg_read_coefficients() after
the last jpeg_finish_output() call. The arrays will be available for your use
until you call jpeg_finish_decompress().
To write the contents of a JPEG file as DCT coefficients, you must provide
the DCT coefficients stored in virtual block arrays. You can either pass
block arrays read from an input JPEG file by jpeg_read_coefficients(), or
allocate virtual arrays from the JPEG compression object and fill them
yourself. In either case, jpeg_write_coefficients() is substituted for
jpeg_start_compress() and jpeg_write_scanlines(). Thus the sequence is
* Create compression object
* Set all compression parameters as necessary
* Request virtual arrays if needed
* jpeg_write_coefficients()
* jpeg_finish_compress()
* Destroy or re-use compression object
jpeg_write_coefficients() is passed a pointer to an array of virtual block
array descriptors; the number of arrays is equal to cinfo.num_components.
The virtual arrays need only have been requested, not realized, before
jpeg_write_coefficients() is called. A side-effect of
jpeg_write_coefficients() is to realize any virtual arrays that have been
requested from the compression object's memory manager. Thus, when obtaining
the virtual arrays from the compression object, you should fill the arrays
after calling jpeg_write_coefficients(). The data is actually written out
when you call jpeg_finish_compress(); jpeg_write_coefficients() only writes
the file header.
When writing raw DCT coefficients, it is crucial that the JPEG quantization
tables and sampling factors match the way the data was encoded, or the
resulting file will be invalid. For transcoding from an existing JPEG file,
we recommend using jpeg_copy_critical_parameters(). This routine initializes
all the compression parameters to default values (like jpeg_set_defaults()),
then copies the critical information from a source decompression object.
The decompression object should have just been used to read the entire
JPEG input file --- that is, it should be awaiting jpeg_finish_decompress().
jpeg_write_coefficients() marks all tables stored in the compression object
as needing to be written to the output file (thus, it acts like
jpeg_start_compress(cinfo, TRUE)). This is for safety's sake, to avoid
emitting abbreviated JPEG files by accident. If you really want to emit an
abbreviated JPEG file, call jpeg_suppress_tables(), or set the tables'
individual sent_table flags, between calling jpeg_write_coefficients() and
jpeg_finish_compress().
Progress monitoring
-------------------
Some applications may need to regain control from the JPEG library every so
often. The typical use of this feature is to produce a percent-done bar or
other progress display. (For a simple example, see cjpeg.c or djpeg.c.)
Although you do get control back frequently during the data-transferring pass
(the jpeg_read_scanlines or jpeg_write_scanlines loop), any additional passes
will occur inside jpeg_finish_compress or jpeg_start_decompress; those
routines may take a long time to execute, and you don't get control back
until they are done.
You can define a progress-monitor routine which will be called periodically
by the library. No guarantees are made about how often this call will occur,
so we don't recommend you use it for mouse tracking or anything like that.
At present, a call will occur once per MCU row, scanline, or sample row
group, whichever unit is convenient for the current processing mode; so the
wider the image, the longer the time between calls. During the data
transferring pass, only one call occurs per call of jpeg_read_scanlines or
jpeg_write_scanlines, so don't pass a large number of scanlines at once if
you want fine resolution in the progress count. (If you really need to use
the callback mechanism for time-critical tasks like mouse tracking, you could
insert additional calls inside some of the library's inner loops.)
To establish a progress-monitor callback, create a struct jpeg_progress_mgr,
fill in its progress_monitor field with a pointer to your callback routine,
and set cinfo->progress to point to the struct. The callback will be called
whenever cinfo->progress is non-NULL. (This pointer is set to NULL by
jpeg_create_compress or jpeg_create_decompress; the library will not change
it thereafter. So if you allocate dynamic storage for the progress struct,
make sure it will live as long as the JPEG object does. Allocating from the
JPEG memory manager with lifetime JPOOL_PERMANENT will work nicely.) You
can use the same callback routine for both compression and decompression.
The jpeg_progress_mgr struct contains four fields which are set by the library:
long pass_counter; /* work units completed in this pass */
long pass_limit; /* total number of work units in this pass */
int completed_passes; /* passes completed so far */
int total_passes; /* total number of passes expected */
During any one pass, pass_counter increases from 0 up to (not including)
pass_limit; the step size is usually but not necessarily 1. The pass_limit
value may change from one pass to another. The expected total number of
passes is in total_passes, and the number of passes already completed is in
completed_passes. Thus the fraction of work completed may be estimated as
completed_passes + (pass_counter/pass_limit)
--------------------------------------------
total_passes
ignoring the fact that the passes may not be equal amounts of work.
When decompressing, pass_limit can even change within a pass, because it
depends on the number of scans in the JPEG file, which isn't always known in
advance. The computed fraction-of-work-done may jump suddenly (if the library
discovers it has overestimated the number of scans) or even decrease (in the
opposite case). It is not wise to put great faith in the work estimate.
When using the decompressor's buffered-image mode, the progress monitor work
estimate is likely to be completely unhelpful, because the library has no way
to know how many output passes will be demanded of it. Currently, the library
sets total_passes based on the assumption that there will be one more output
pass if the input file end hasn't yet been read (jpeg_input_complete() isn't
TRUE), but no more output passes if the file end has been reached when the
output pass is started. This means that total_passes will rise as additional
output passes are requested. If you have a way of determining the input file
size, estimating progress based on the fraction of the file that's been read
will probably be more useful than using the library's value.
( run in 1.721 second using v1.01-cache-2.11-cpan-0c5ce583b80 )