Chap 7
Chap 7
Chapter 7
Lossless Compression Algorithms
7.1 Introduction
7.1 Introduction
Introduction (cont’d)
• Compression ratio:
B0
compression ratio = (7.1)
B1
For example:
WWWWWWWWWWWWBWWWWWWWWWWWWBBBWWWWWWWWW
WWWWWWWWWWWWWWWBWWWWWWWWWWWWWW
With a run-length encoding (RLE) data compression algorithm applied to the above
hypothetical scan line, it can be rendered as follows:
12W1B12W3B24W1B14W
This can be interpreted as a sequence of twelve Ws, one B, twelve Ws, three Bs, etc.,
The run-length code represents the original 67 characters in only 18
Fundamentals of Multimedia, Chapter 7
2. Recursively divide the symbols into two parts, each with ap-
proximately the same number of counts, until all parts con-
tain only one symbol.
Symbol H E L O
Count 1 2
1 1
Frequency count of the symbols in ”HELLO”.
(5) (5)
0 1 0 1
(3)
L:(2) H,E,O:(3) L:(2) 0 1
H:(1) E,O:(2)
(a) (b)
(5)
0 1
(3)
L:(2) 0 1
(2)
H:(1) 0 1
E:(1) O:(1)
(c)
(5) (5)
0 1 0 1
(2) (3)
L,H:(3) E,O:(2) 0 1 0 1
Huffman Coding
3. Assign a codeword for each leaf based on the path from the
root.
P1:(2) P2:(3)
0 1 0 1
P1:(2)
E:(1) O:(1) H:(1) 0 1
E:(1) O:(1)
(a) (b)
P3:(5)
0 1
P2:(3)
L:(2) 0 1
P1:(2)
H:(1) 0 1
E:(1) O:(1)
(c)
Fig. 7.5: Coding Tree for “HELLO” using the Huffman Algorithm.
In Fig. 7.5, new symbols P1, P2, P3 are created to refer to the
parent nodes in the Huffman coding tree. The contents in the
list are illustrated below:
¯l < η + 1 (7.6)
1
η ≤ ¯l < η + (7.7)
k
ENCODER DECODER
------- -------
Initial_code(); Initial_code();
while not EOF while not EOF
{ {
get(c); decode(c);
encode(c); output(c);
update_tree(c); update_tree(c);
} }
• The encoder and decoder must use exactly the same ini-
tial code and update tree routines.
• The tree must always maintain its sibling property, i.e., all
nodes (internal and leaf) are arranged in the order of increas-
ing counts.
9. (9) 9. (10)
7. (4) 7. (5)
8. P:(5) 8. P:(5)
5. (2) 6. (2) 5. (2) 6. (3)
9. (10) 9. (11)
9. (10) 7. (5+1)
8. (6)
8. P:(5) 7. P:(5)
7. (5) 6. (3) 6. (3)
8. P:(5) 5. A:(3) 5. A:(3)
5. (2) 6. (3) 4. (2) 4. (2)
3. C:(1) 3. C:(1)
(c−1) A swap is needed after receiving 3rd ’A’ (c−2) Another swap is needed (c−3) The Huffman tree after receiving 3rd ’A’
Initial Code
NEW: 0
A: 00001
B: 00010
C:00011
D: 00100
. .
. .
. .
(1) (2)
0 1 0 1
"A" "AA"
(3) (4)
0 1 0 1
(1) (2)
0 1 A:(2) (1) 0 1 A:(2)
0 1
NEW:(0) D:(1) D:(1)
NEW:(0) C:(1)
"AAD"
"AADC"
(4) (4)
0 1 0 1
(2) (2+1)
1 A:(2) (1) 0 1 A:(2)
(1) 0
0 1 0 1
D:(1) C:(2)
BEGIN
s = next input character;
while not EOF
{ c = next input character;
code string
---------------
1 A
2 B
3 C
BEGIN
s = NIL;
while not EOF
{
k = next input code;
entry = dictionary entry for k;
output entry;
if (s != NIL)
add string s + entry[0] to dictionary with a new code;
s = entry;
}
END
/* exception handler */
if (entry == NULL)
entry = s + s[0];
output entry;
if (s != NIL)
add string s + entry[0] to dictionary with a new code;
s = entry;
}
END
34 Li & Drew c Prentice Hall 2003
Fundamentals of Multimedia, Chapter 7
BEGIN
low = 0.0; high = 1.0; range = 1.0;
$ $ $ $ $ $
0.9
F F F F F F
0.85
E E E E E E
0.55
D D D D D D
0.5
C C C C C C
0.3
B B B B B B
0.2
A A A A A A
BEGIN
code = 0;
k = 1;
while (value(code) < low)
{ assign 1 to the kth binary fraction bit
if (value(code) > high)
replace the kth bit by 0
k = k + 1;
}
END
BEGIN
get binary code and convert to
decimal value = value(code);
Do
{ find a symbol s so that
Range_low(s) <= value < Range_high(s);
output s;
low = Rang_low(s);
high = Range_high(s);
range = high - low;
value = [value - low] / range;
}
Until symbol s is a terminator
END
(a) (b)
× 104 × 104
4 15
3.5
3 10
2.5
2
5
1.5
0 0
1 0 50 100 150 200 250 −80 −60 −40 −20 0 20 40 60 80
0.5
(c) (d)
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.
Lossless JPEG
C B
A X
Predictor Prediction
P1 A
P2 B
P3 C
P4 A+B-C
P5 A + (B - C) / 2
P6 B + (A - C) / 2
P7 (A + B) / 2
• Text books:
– The Data Compression Book by M. Nelson
– Introduction to Data Compression by K. Sayood