Chapter 7 Lossless Compression Algorithms
Chapter 7 Lossless Compression Algorithms
7.1 Introduction
Iran University of Science and Technology, Computer Engineering Department,
Compression: the process of coding that will effectively reduce the total number of bits needed to represent certain information.
w w.jntuworld.com
FundamentalsofMultimedia,Chapter7
FundamentalsofMultimedia,Chapter7
Introduction (contd)
If the compression and decompression processes induce no information loss, then the compression scheme is lossless; otherwise, it is lossy. Compression ratio:
compression ratio = 0B B1
= H (S ) =
p log
n i i =1 n i =1
1 pi
(7.2) (7.3)
(7.1)
log 1 indicates the amount of information ( self2 pi information as defined by Shannon) contained in si, which corresponds to the number of bits needed to encode si.
Multimedia Systems (eadeli@iust.ac.ir)
FundamentalsofMultimedia,Chapter7
FundamentalsofMultimedia,Chapter7
Fig. 7.2 Histograms for Two Gray-level Images. Fig. 7.2(a) shows the histogram of an image with uniform distribution of gray-level intensities, i.e.,i pi = 1/256. Hence, the entropy of this image is: log2256 = 8 (7.4)
The entropy specifies the lower bound for the average number of bits to code each symbol in S, i.e.,
(7.5)
Fig. 7.2(b) shows the histogram of an image with two possible values. Its entropy is 0.92.
Multimedia Systems (eadeli@iust.ac.ir)
l - the average length (measured in bits) of the codewords produced by the encoder.
FundamentalsofMultimedia,Chapter7
FundamentalsofMultimedia,Chapter7
Count
w w.jntuworld.com
FundamentalsofMultimedia,Chapter7
FundamentalsofMultimedia,Chapter7
w w.jntuworld.com
FundamentalsofMultimedia,Chapter7
FundamentalsofMultimedia,Chapter7
Table 7.2: Another Result of Performing Shannon-Fano on HELLO (see Fig. 7.4)
Symbol L H E O Count 2 1 1 1 Log2 1 pi 1.32 2.32 2.32 2.32 Code 00 01 10 11 TOTAL#ofbits: #ofbitsused 4 2 2 2 10
11
12
FundamentalsofMultimedia,Chapter7
FundamentalsofMultimedia,Chapter7
Huffman Coding
ALGORITHM 7.1 Huffman Coding Algorithm a bottom-up approach 1. Initialization: Put all symbols on a list sorted according to their frequency counts. 2. Repeat until the list has only one symbol left:
(1) From the list pick two symbols with the lowest frequency counts. Form a Huffman subtree that has these two symbols as child nodes and create a parent node. (2) Assign the sum of the childrens frequency counts to the parent and insert it into the list such that the order is maintained. (3) Delete the children from the list.
w w.jntuworld.com
3. Assign a codeword for each leaf based on the path from the root.
Fig. 7.5: Coding Tree for HELLO using the Huffman Algorithm.
13
14
FundamentalsofMultimedia,Chapter7
FundamentalsofMultimedia,Chapter7
H P1 P2
E H
l<+1
16
-63(7.6)
Multimedia Systems (eadeli@iust.ac.ir)
FundamentalsofMultimedia,Chapter7
FundamentalsofMultimedia,Chapter7
It can be proven that the average # of bits for each symbol is:
l<+1 k
w w.jntuworld.com
(7.7)
Why not group several symbols together and assign a single codeword to the group as a whole? Extended Alphabet: For alphabet S = {s1, s2, . . . , sn}, if k symbols are grouped together, then the extended alphabet is:
k symbols ) {s1s1 S ( k=
An improvement over the original Huffman coding, but not much. Problem: If k is relatively large (e.g., k 3), then for most practical applications where n 1, nk implies a huge symbol table impractical.
18 Multimedia Systems (eadeli@iust.ac.ir)
17
FundamentalsofMultimedia,Chapter7
FundamentalsofMultimedia,Chapter7
19
FundamentalsofMultimedia,Chapter7
FundamentalsofMultimedia,Chapter7
w w.jntuworld.com
21
22
FundamentalsofMultimedia,Chapter7
FundamentalsofMultimedia,Chapter7
Table 7.3: Initial code assignment for AADCCDD using adaptive Huffman coding.
InitialCode --------------------NEW:0 A:00001 B:00010 C: 00011 D: 00100 .. .. ..
FundamentalsofMultimedia,Chapter7
FundamentalsofMultimedia,Chapter7
w w.jntuworld.com
FundamentalsofMultimedia,Chapter7
FundamentalsofMultimedia,Chapter7
It is important to emphasize that the code for a particular symbol changes during the adaptive Huffman coding process. For example, after AADCCDD, when the character D overtakes A as the most frequent symbol, its code changes from 101 to 0.
27 Multimedia Systems (eadeli@iust.ac.ir)
FundamentalsofMultimedia,Chapter7
FundamentalsofMultimedia,Chapter7
Example 72 LZW compression for string ABABBABCABABBA Lets start with a very simple dictionary (also referred to as a string table), initially containing only 3 characters, with codes as follows:
Code 1 2 3 String A B C
Now if the input string is ABABBABCABABBA, the LZW compression algorithm works as follows:
Multimedia Systems (eadeli@iust.ac.ir) Multimedia Systems (eadeli@iust.ac.ir)
FundamentalsofMultimedia,Chapter7
AB
10
ABA
END
AB ABB A
B A EOF
6 1
11
ABBA
Example 7.3: LZW decompression for string ABABBABCABABBA. Input codes to the decoder are 1 2 4 5 2 3 4 6 1. The initial string table is identical to what is used by the encoder.
The output codes are: 1 2 4 5 2 3 4 6 1. Instead of sending 14 characters, only 9 codes need to be sent (compression ratio = 14/9 = 1.56).
Multimedia Systems (eadeli@iust.ac.ir)
7.2
w w.jntuworld.com
END
29
30
31
32
FundamentalsofMultimedia,Chapter7
FundamentalsofMultimedia,Chapter7
TheLZWdecompressionalgorithmthenworksasfollows:
S K Entry/output Code 1 2 3 NIL A B AB BA 1 2 4 5 2 A B AB BA B 4 5 6 7 String A B C AB BA ABB BAB
B C AB ABB A
3 4 6 1 EOF
C AB ABB A
8 9 10 11
BC CA ABA ABBA }
output entry; if (s != NIL) add string s + entry[0] to dictionary with a new code; s = entry;
w w.jntuworld.com
FundamentalsofMultimedia,Chapter7 FundamentalsofMultimedia,Chapter7
When lmax is reached and the dictionary is filled up, it needs to be flushed (as in Unix compress, or to have the LRU (least recently used) entries removed.
Multimedia Systems (eadeli@iust.ac.ir)
35
36
FundamentalsofMultimedia,Chapter7
FundamentalsofMultimedia,Chapter7
while (symbol != terminator) { get (symbol); low = low + range * Range_low(symbol); high = low + range * Range_high(symbol); range = high - low;
FundamentalsofMultimedia,Chapter7
FundamentalsofMultimedia,Chapter7
Symbol C A
w w.jntuworld.com
END
37 38
E E $
39
40
FundamentalsofMultimedia,Chapter7
FundamentalsofMultimedia,Chapter7
END The final step in Arithmetic encoding calls for the generation of a number that falls within the range [low, high). The above algorithm will ensure that the shortest binary codeword is found.
Multimedia Systems (eadeli@iust.ac.ir)
END
FundamentalsofMultimedia,Chapter7
FundamentalsofMultimedia,Chapter7
Output Symbol C A
Due to spatial redundancy existed in normal images I, the difference image d will have a narrower histogram and hence a smaller entropy, as shown in Fig. 7.9.
Multimedia Systems (eadeli@iust.ac.ir) Multimedia Systems (eadeli@iust.ac.ir)
Do
w w.jntuworld.com
41
42
E E $
0.3
43
44
FundamentalsofMultimedia,Chapter7
FundamentalsofMultimedia,Chapter7
Lossless JPEG
Lossless JPEG: A special case of the JPEG image compression. The Predictive method 1. Forming a differential prediction: A predictor combines the values of up to three neighboring pixels as the predicted value for the current pixel, indicated by X in Fig. 7.10. The predictor can use any one of the seven schemes listed in Table 7.6.
Fig. 7.9: Distributions for Original versus Derivative Images. (a,b): Original gray-level image and its partial derivative image; (c,d): Histograms for original and derivative images. (This figure uses a commonly employed image called Barb.)
45 Multimedia Systems (eadeli@iust.ac.ir) 46 Multimedia Systems (eadeli@iust.ac.ir)
w w.jntuworld.com
2. Encoding: The encoder compares the prediction with the actual pixel value at the position X and encodes the difference using one of the lossless compression techniques we have discussed, e.g., the Huffman coding scheme.
FundamentalsofMultimedia,Chapter7
FundamentalsofMultimedia,Chapter7
Fig. 7.10: Neighboring Pixels for Predictors in Lossless JPEG. Note: Any of A, B, or C has already been decoded before it is used in the predictor, on the decoder side of an encode-decode cycle.
Multimedia Systems (eadeli@iust.ac.ir) Multimedia Systems (eadeli@iust.ac.ir)
47
48
FundamentalsofMultimedia,Chapter7
FundamentalsofMultimedia,Chapter7
w w.jntuworld.com
49
50