The JPEG Algorithm and Implementation


 

Demonstration of Baseline sequential Lossy JPEG implementation on a sample image

 

The following example shows the different steps of compression using the Baseline sequential Lossy JPEG standard. It starts with a 8x8 image block and a corresponding bit pattern is generated at the end.

INPUT 8x8(8 bit precision) Image Matrix


117 118 120 121 121 121 120 119
118 119 121 122 122 121 120 120
120 120 121 122 121 121 121 120
119 120 120 120 119 119 119 119
119 118 118 118 117 117 117 117
118 118 117 116 115 115 115 116
120 119 117 116 114 115 116 116
121 120 118 117 115 115 116 117
This is a 8x8 block of a sample image

The input matrix is level shifted(ie 128 substracted from each element values) and a 2-D Discrete Cosine Transform is performed on all the elements. The following 8x8 matrix is generated.


Discrete Cosine Transformed Matrix


-75 3 0 -1 0 0 0 0
11 -6 -0 0 0 0 0 0
0 0 0 0 0 0 0 0
-6 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0

This matrix is in frequency domain and the leftmost top element is the zero frequency or DC element. As can be seen except for a few low frequency coefficients, the amplitudes of all other elements are quite small or zero.

Sample Quantization Table Used


8, 6, 5, 8, 12, 20, 26, 31,
6 6, 7, 10, 13, 29, 30, 28,
7, 7, 8, 12, 20, 29, 35, 28,
7, 9, 11, 15, 26, 44, 40, 31,
9, 11, 19, 28, 34, 55, 52, 39,
12, 18, 28, 32, 41, 52, 57, 46,
25, 32, 39, 44, 52, 61, 60, 51,
36, 46, 48, 49, 56, 50, 52, 50,
This step achieves further compression by representing DCT coefficients with no greater precesion than is necessary to achieve the desired image quality and is a fundamentally LOSSY step.

Quantized and Rounded off Matrix


-9 0 0 0 0 0 0 0
1 -1 -0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
Each element in the DCT matrix is divided by the corresponding element of the Quantization matrix and the results rounded off to the nearest integer.

This quantized values are now Zic-Zac ordered in the following way[{0, 0}, {0, 1}, {1, 0}, {2, 0}, {1, 1}, {0, 2}..] to generate a element stream

ZicZac Ordered values

-9 0 1 0 -1 0 0 -1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0


Entropy Encoding

STEP 1. Intermediate Symbol Generation

The first term is a DC term which is differentially encoded. If the DC term of the preceeding block was for e.g. -14 then the difference is 5 (-9-(-14)).

The DC symbol format is [ Size ] [Amplitude]

DC symbol is ( 5 ) , ( 3 )

The AC symbol format is [ Runlength, Size ] [ Amplitude ]

So the Runlength Coded AC Symbols are

AC symbol is ( 1 , 1 ) , ( 1 )
AC symbol is ( 1 , 1 ) , ( -1 )
AC symbol is ( 2 , 1 ) , ( -1 )

After this all elements are zero and this is represented by a standard End Of Block symbol (0,0)

AC symbol is ( 0 , 0 )

Hence the intermediate symbol sequence are ( 5 )( 3 ), ( 1 , 1 )( 1 ), ( 1 , 1 )( -1 ), ( 2 , 1 )( -1 ), ( 0 , 0 )

STEP 2. BitStream Generation

The required luminance DC coefficient code are:

(5) 110

The required luminance AC coefficient code are:

(1,1) 1100
(2,1) 11100
(0,0) 1010

The amplitudes are related to complement representation

(3) 11
(1) 1
(-1) 0

Thus the JPEG bit-stream for this 8x8 image block is

11011 11001 11000 111000 1010

So 25 bits are required to represent the 64 coefficients(ie 512 bits) !!
And the resulting compression is around 0.4 bits/sample !!!

 

 


 

Contact:  

Tapan Samaddar

  Note: Many of the above information is copyrighted content. Just contact me at the above email to use any of the above content, for reference.