Glossary
This part briefly explains the concepts behind the library. The rest of documentation will refer to this page as a knowledge base.
Table of Contents
JPEG compression
Description of JPEG compression
Discrete cosine transform
Description of discrete cosine transform
Forward DCT (also called DCT II)
Inverse DCT (also called DCT III)
References for JPEG compression
libjpeg C library
Description of libjpeg
Mention different version
JPEG sampling factor
As human eyes are better in seeing light intensity (luminance) than colors (chrominance), chrominance is another component that can be reduced with so called chroma subsampling. It is controlled using sampling factors, a pair of integer numbers per each channel, 3 pairs for YCbCr, [[h1,w1],[h2,w2],[h3,w3]].
An RGB image with 300px x 300px compressed with sampling factors [[2,2],[2,1],[1,2]] will have components Y of size 200 x 200, Cb of size 200 x 100 and Cr of size 100 x 200. You can specify this before writing a JPEG image
>>> # im is a jpeglib.JPEG object
>>>
>>> # Sampling factors as a list [[Y_v, Y_h], [Cb_v, Cb_h],[Cr_v, Cr_h]]
>>> im.samp_factor = [[2, 2], [2, 1], [1, 2]]
>>>
>>> # now you can write im to a file
Researchers often use J:a:b notation, which is shorter, but assumes same subsampling for both chrominances. No chroma subsampling, [[1,1],[1,1],[1,1]], is denoted as 4:4:4.
>>> # Variant 1: Per-channel sampling factors
>>> im.samp_factor = [[1, 1], [1, 1], [1, 1]]
>>>
>>> # Variant 2: Specify in J:a:b notation
>>> im.samp_factor = '4:4:4'
The following table contains chroma sampling notations of jpeglib and J:a:b notation.
J:a:b notation |
jpeglib notation |
Height ratio |
Width ratio |
|---|---|---|---|
4:4:4 |
[[1, 1], [1, 1], [1, 1]] |
\(1/1\) |
\(1/1\) |
4:4:0 |
[[2, 1], [1, 1], [1, 1]] |
\(1/2\) |
\(1/1\) |
4:2:2 |
[[1, 2], [1, 1], [1, 1]] |
\(1/1\) |
\(1/2\) |
4:2:0 |
[[2, 2], [1, 1], [1, 1]] |
\(1/2\) |
\(1/2\) |
4:1:1 |
[[1, 4], [1, 1], [1, 1]] |
\(1/1\) |
\(1/4\) |
4:1:0 |
[[2, 4], [1, 1], [1, 1]] |
\(1/2\) |
\(1/4\) |
Note
In cjpeg, ImageMagick, and this tutorial, the chroma sampling factors are defined in horizontal-vertical order. For consistency with the rest of the interface, jpeglib uses vertical-horizontal order.
Flags
Compression and decompression parameters have a crucial impact on the output JPEG. Boolean parameters are in jpeglib simply called flags, and contain following.
Compression
DO_FANCY_SAMPLING, DO_FANCY_DOWNSAMPLING
OPTIMIZE_CODING
PROGRESSIVE_MODE
ARITH_CODE
TRELLIS_QUANT (>= mozjpeg300)
TRELLIS_QUANT_DC (>= mozjpeg300)
OVERSHOOT_DERINGING (>= mozjpeg300)
FORCE_BASELINE
WRITE_JFIF_HEADER
WRITE_ADOBE_MARKER
Decompression
DO_FANCY_UPSAMPLING
QUANTIZE_COLORS
DO_BLOCK_SMOOTHING
TWO_PASS_QUANTIZE
ENABLE_1PASS_QUANT
ENABLE_2PASS_QUANT
ENABLE_EXTERNAL_QUANT
CCIR601_SAMPLING
The flags can be specified as a list of string, either enabling or disabling the option. Following code decompresses the input using simple upsampling, and then compresses it again into progressive JPEG, with explicitly disabling Huffman code optimization.
>>> jpeglib.version.set('6b')
>>> im = jpeglib.read_spatial("input.jpeg", flags=["-DO_FANCY_UPSAMPLING"])
>>> im.write_spatial("output.jpeg", flags=["+PROGRESSIVE_MODE", "-OPTIMIZE_CODING"])
The values of not-specified flags are kept to be defaultly set by the selected libjpeg version, or copied from the source image.
References
Progressive JPEG
Progressive JPEG arranges the data in the file by placing the low-level image first, and details later. On slow internet connection, progressive JPEG loads by gradually focusing, while sequential JPEG shows in full quality line-by-line.
Progressive JPEG consists of scans, which carry parts of the DCT coefficients. DCT coefficients can be split by subband (frequency) and by precision (bits). After full loading of all the scans, progressive image should be, in theory, identical to its sequential counterpart. However, MozJPEG uses Trellis optimization which optimizes the file size and allows introduction of a imperceptible distortion.
References