kusano 7d535a
Advanced usage instructions for the Independent JPEG Group's JPEG software
kusano 7d535a
==========================================================================
kusano 7d535a
kusano 7d535a
This file describes cjpeg's "switches for wizards".
kusano 7d535a
kusano 7d535a
The "wizard" switches are intended for experimentation with JPEG by persons
kusano 7d535a
who are reasonably knowledgeable about the JPEG standard.  If you don't know
kusano 7d535a
what you are doing, DON'T USE THESE SWITCHES.  You'll likely produce files
kusano 7d535a
with worse image quality and/or poorer compression than you'd get from the
kusano 7d535a
default settings.  Furthermore, these switches must be used with caution
kusano 7d535a
when making files intended for general use, because not all JPEG decoders
kusano 7d535a
will support unusual JPEG parameter settings.
kusano 7d535a
kusano 7d535a
kusano 7d535a
Quantization Table Adjustment
kusano 7d535a
-----------------------------
kusano 7d535a
kusano 7d535a
Ordinarily, cjpeg starts with a default set of tables (the same ones given
kusano 7d535a
as examples in the JPEG standard) and scales them up or down according to
kusano 7d535a
the -quality setting.  The details of the scaling algorithm can be found in
kusano 7d535a
jcparam.c.  At very low quality settings, some quantization table entries
kusano 7d535a
can get scaled up to values exceeding 255.  Although 2-byte quantization
kusano 7d535a
values are supported by the IJG software, this feature is not in baseline
kusano 7d535a
JPEG and is not supported by all implementations.  If you need to ensure
kusano 7d535a
wide compatibility of low-quality files, you can constrain the scaled
kusano 7d535a
quantization values to no more than 255 by giving the -baseline switch.
kusano 7d535a
Note that use of -baseline will result in poorer quality for the same file
kusano 7d535a
size, since more bits than necessary are expended on higher AC coefficients.
kusano 7d535a
kusano 7d535a
You can substitute a different set of quantization values by using the
kusano 7d535a
-qtables switch:
kusano 7d535a
shun-iwasawa 82a8f5
        -qtables file   Use the quantization tables given in the named file.
kusano 7d535a
kusano 7d535a
The specified file should be a text file containing decimal quantization
kusano 7d535a
values.  The file should contain one to four tables, each of 64 elements.
kusano 7d535a
The tables are implicitly numbered 0,1,etc. in order of appearance.  Table
kusano 7d535a
entries appear in normal array order (NOT in the zigzag order in which they
kusano 7d535a
will be stored in the JPEG file).
kusano 7d535a
kusano 7d535a
Quantization table files are free format, in that arbitrary whitespace can
kusano 7d535a
appear between numbers.  Also, comments can be included: a comment starts
kusano 7d535a
with '#' and extends to the end of the line.  Here is an example file that
kusano 7d535a
duplicates the default quantization tables:
kusano 7d535a
shun-iwasawa 82a8f5
        # Quantization tables given in Annex K (Clause K.1) of
shun-iwasawa 82a8f5
        # Recommendation ITU-T T.81 (1992) | ISO/IEC 10918-1:1994.
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
        # This is table 0 (the luminance table):
shun-iwasawa 82a8f5
          16  11  10  16  24  40  51  61
shun-iwasawa 82a8f5
          12  12  14  19  26  58  60  55
shun-iwasawa 82a8f5
          14  13  16  24  40  57  69  56
shun-iwasawa 82a8f5
          14  17  22  29  51  87  80  62
shun-iwasawa 82a8f5
          18  22  37  56  68 109 103  77
shun-iwasawa 82a8f5
          24  35  55  64  81 104 113  92
shun-iwasawa 82a8f5
          49  64  78  87 103 121 120 101
shun-iwasawa 82a8f5
          72  92  95  98 112 100 103  99
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
        # This is table 1 (the chrominance table):
shun-iwasawa 82a8f5
          17  18  24  47  99  99  99  99
shun-iwasawa 82a8f5
          18  21  26  66  99  99  99  99
shun-iwasawa 82a8f5
          24  26  56  99  99  99  99  99
shun-iwasawa 82a8f5
          47  66  99  99  99  99  99  99
shun-iwasawa 82a8f5
          99  99  99  99  99  99  99  99
shun-iwasawa 82a8f5
          99  99  99  99  99  99  99  99
shun-iwasawa 82a8f5
          99  99  99  99  99  99  99  99
shun-iwasawa 82a8f5
          99  99  99  99  99  99  99  99
kusano 7d535a
kusano 7d535a
If the -qtables switch is used without -quality, then the specified tables
kusano 7d535a
are used exactly as-is.  If both -qtables and -quality are used, then the
kusano 7d535a
tables taken from the file are scaled in the same fashion that the default
kusano 7d535a
tables would be scaled for that quality setting.  If -baseline appears, then
kusano 7d535a
the quantization values are constrained to the range 1-255.
kusano 7d535a
kusano 7d535a
By default, cjpeg will use quantization table 0 for luminance components and
kusano 7d535a
table 1 for chrominance components.  To override this choice, use the -qslots
kusano 7d535a
switch:
kusano 7d535a
shun-iwasawa 82a8f5
        -qslots N[,...]         Select which quantization table to use for
shun-iwasawa 82a8f5
                                each color component.
kusano 7d535a
kusano 7d535a
The -qslots switch specifies a quantization table number for each color
kusano 7d535a
component, in the order in which the components appear in the JPEG SOF marker.
kusano 7d535a
For example, to create a separate table for each of Y,Cb,Cr, you could
kusano 7d535a
provide a -qtables file that defines three quantization tables and say
kusano 7d535a
"-qslots 0,1,2".  If -qslots gives fewer table numbers than there are color
kusano 7d535a
components, then the last table number is repeated as necessary.
kusano 7d535a
kusano 7d535a
kusano 7d535a
Sampling Factor Adjustment
kusano 7d535a
--------------------------
kusano 7d535a
kusano 7d535a
By default, cjpeg uses 2:1 horizontal and vertical downsampling when
kusano 7d535a
compressing YCbCr data, and no downsampling for all other color spaces.
kusano 7d535a
You can override this default with the -sample switch:
kusano 7d535a
shun-iwasawa 82a8f5
        -sample HxV[,...]       Set JPEG sampling factors for each color
shun-iwasawa 82a8f5
                                component.
kusano 7d535a
kusano 7d535a
The -sample switch specifies the JPEG sampling factors for each color
kusano 7d535a
component, in the order in which they appear in the JPEG SOF marker.
kusano 7d535a
If you specify fewer HxV pairs than there are components, the remaining
kusano 7d535a
components are set to 1x1 sampling.  For example, the default YCbCr setting
kusano 7d535a
is equivalent to "-sample 2x2,1x1,1x1", which can be abbreviated to
kusano 7d535a
"-sample 2x2".
kusano 7d535a
kusano 7d535a
There are still some JPEG decoders in existence that support only 2x1
kusano 7d535a
sampling (also called 4:2:2 sampling).  Compatibility with such decoders can
kusano 7d535a
be achieved by specifying "-sample 2x1".  This is not recommended unless
kusano 7d535a
really necessary, since it increases file size and encoding/decoding time
kusano 7d535a
with very little quality gain.
kusano 7d535a
kusano 7d535a
kusano 7d535a
Multiple Scan / Progression Control
kusano 7d535a
-----------------------------------
kusano 7d535a
kusano 7d535a
By default, cjpeg emits a single-scan sequential JPEG file.  The
kusano 7d535a
-progressive switch generates a progressive JPEG file using a default series
kusano 7d535a
of progression parameters.  You can create multiple-scan sequential JPEG
kusano 7d535a
files or progressive JPEG files with custom progression parameters by using
kusano 7d535a
the -scans switch:
kusano 7d535a
shun-iwasawa 82a8f5
        -scans file     Use the scan sequence given in the named file.
kusano 7d535a
kusano 7d535a
The specified file should be a text file containing a "scan script".
kusano 7d535a
The script specifies the contents and ordering of the scans to be emitted.
kusano 7d535a
Each entry in the script defines one scan.  A scan definition specifies
kusano 7d535a
the components to be included in the scan, and for progressive JPEG it also
kusano 7d535a
specifies the progression parameters Ss,Se,Ah,Al for the scan.  Scan
kusano 7d535a
definitions are separated by semicolons (';').  A semicolon after the last
kusano 7d535a
scan definition is optional.
kusano 7d535a
kusano 7d535a
Each scan definition contains one to four component indexes, optionally
kusano 7d535a
followed by a colon (':') and the four progressive-JPEG parameters.  The
kusano 7d535a
component indexes denote which color component(s) are to be transmitted in
kusano 7d535a
the scan.  Components are numbered in the order in which they appear in the
kusano 7d535a
JPEG SOF marker, with the first component being numbered 0.  (Note that these
kusano 7d535a
indexes are not the "component ID" codes assigned to the components, just
kusano 7d535a
positional indexes.)
kusano 7d535a
kusano 7d535a
The progression parameters for each scan are:
shun-iwasawa 82a8f5
        Ss      Zigzag index of first coefficient included in scan
shun-iwasawa 82a8f5
        Se      Zigzag index of last coefficient included in scan
shun-iwasawa 82a8f5
        Ah      Zero for first scan of a coefficient, else Al of prior scan
shun-iwasawa 82a8f5
        Al      Successive approximation low bit position for scan
kusano 7d535a
If the progression parameters are omitted, the values 0,63,0,0 are used,
kusano 7d535a
producing a sequential JPEG file.  cjpeg automatically determines whether
kusano 7d535a
the script represents a progressive or sequential file, by observing whether
kusano 7d535a
Ss and Se values other than 0 and 63 appear.  (The -progressive switch is
kusano 7d535a
not needed to specify this; in fact, it is ignored when -scans appears.)
kusano 7d535a
The scan script must meet the JPEG restrictions on progression sequences.
kusano 7d535a
(cjpeg checks that the spec's requirements are obeyed.)
kusano 7d535a
kusano 7d535a
Scan script files are free format, in that arbitrary whitespace can appear
kusano 7d535a
between numbers and around punctuation.  Also, comments can be included: a
kusano 7d535a
comment starts with '#' and extends to the end of the line.  For additional
kusano 7d535a
legibility, commas or dashes can be placed between values.  (Actually, any
kusano 7d535a
single punctuation character other than ':' or ';' can be inserted.)  For
kusano 7d535a
example, the following two scan definitions are equivalent:
shun-iwasawa 82a8f5
        0 1 2: 0 63 0 0;
shun-iwasawa 82a8f5
        0,1,2 : 0-63, 0,0 ;
kusano 7d535a
kusano 7d535a
Here is an example of a scan script that generates a partially interleaved
kusano 7d535a
sequential JPEG file:
kusano 7d535a
shun-iwasawa 82a8f5
        0;                      # Y only in first scan
shun-iwasawa 82a8f5
        1 2;                    # Cb and Cr in second scan
kusano 7d535a
kusano 7d535a
Here is an example of a progressive scan script using only spectral selection
kusano 7d535a
(no successive approximation):
kusano 7d535a
shun-iwasawa 82a8f5
        # Interleaved DC scan for Y,Cb,Cr:
shun-iwasawa 82a8f5
        0,1,2: 0-0,   0, 0 ;
shun-iwasawa 82a8f5
        # AC scans:
shun-iwasawa 82a8f5
        0:     1-2,   0, 0 ;    # First two Y AC coefficients
shun-iwasawa 82a8f5
        0:     3-5,   0, 0 ;    # Three more
shun-iwasawa 82a8f5
        1:     1-63,  0, 0 ;    # All AC coefficients for Cb
shun-iwasawa 82a8f5
        2:     1-63,  0, 0 ;    # All AC coefficients for Cr
shun-iwasawa 82a8f5
        0:     6-9,   0, 0 ;    # More Y coefficients
shun-iwasawa 82a8f5
        0:     10-63, 0, 0 ;    # Remaining Y coefficients
kusano 7d535a
kusano 7d535a
Here is an example of a successive-approximation script.  This is equivalent
kusano 7d535a
to the default script used by "cjpeg -progressive" for YCbCr images:
kusano 7d535a
shun-iwasawa 82a8f5
        # Initial DC scan for Y,Cb,Cr (lowest bit not sent)
shun-iwasawa 82a8f5
        0,1,2: 0-0,   0, 1 ;
shun-iwasawa 82a8f5
        # First AC scan: send first 5 Y AC coefficients, minus 2 lowest bits:
shun-iwasawa 82a8f5
        0:     1-5,   0, 2 ;
shun-iwasawa 82a8f5
        # Send all Cr,Cb AC coefficients, minus lowest bit:
shun-iwasawa 82a8f5
        # (chroma data is usually too small to be worth subdividing further;
shun-iwasawa 82a8f5
        #  but note we send Cr first since eye is least sensitive to Cb)
shun-iwasawa 82a8f5
        2:     1-63,  0, 1 ;
shun-iwasawa 82a8f5
        1:     1-63,  0, 1 ;
shun-iwasawa 82a8f5
        # Send remaining Y AC coefficients, minus 2 lowest bits:
shun-iwasawa 82a8f5
        0:     6-63,  0, 2 ;
shun-iwasawa 82a8f5
        # Send next-to-lowest bit of all Y AC coefficients:
shun-iwasawa 82a8f5
        0:     1-63,  2, 1 ;
shun-iwasawa 82a8f5
        # At this point we've sent all but the lowest bit of all coefficients.
shun-iwasawa 82a8f5
        # Send lowest bit of DC coefficients
shun-iwasawa 82a8f5
        0,1,2: 0-0,   1, 0 ;
shun-iwasawa 82a8f5
        # Send lowest bit of AC coefficients
shun-iwasawa 82a8f5
        2:     1-63,  1, 0 ;
shun-iwasawa 82a8f5
        1:     1-63,  1, 0 ;
shun-iwasawa 82a8f5
        # Y AC lowest bit scan is last; it's usually the largest scan
shun-iwasawa 82a8f5
        0:     1-63,  1, 0 ;
kusano 7d535a
kusano 7d535a
It may be worth pointing out that this script is tuned for quality settings
kusano 7d535a
of around 50 to 75.  For lower quality settings, you'd probably want to use
kusano 7d535a
a script with fewer stages of successive approximation (otherwise the
kusano 7d535a
initial scans will be really bad).  For higher quality settings, you might
kusano 7d535a
want to use more stages of successive approximation (so that the initial
kusano 7d535a
scans are not too large).