Huffman Coding, RLE, LZW
Huffman Coding, RLE, LZW
Huffman Coding, RLE, LZW
i 1
Compression Achieved
Original image requires 3 bits per pixel (in total - 8x8x3=192 bits).
Compressed image has 29 runs and needs 3+3=6 bits per
run (in total - 174 bits or 2.72 bits per pixel).
LZW - CODING
Lempel-Ziv-Welch
The History of LZW
1977 (LZ77) is published and improved in 1978 (LZ78)
1981 LZ file for US patent 4,464,650 on LZ78 (granted 1984)
for Sperry Corp
1983 Welch improves on LZ78 before leaving Sperry, who file
for US patent 4,558,302 June 20, 1983 (granted Dec. 10, 1985)
1984 Welch, publishes "A Technique for High Performance
Data Compression," IEEE Computer, vol. 17, no. 6, June 1984.
1986 Sperry, Burroughs form Unisys, who assumed ownership
of US 4,558,302
17
Lempel-Ziv-Welch (LZW) Compression: http://netghost.narod.ru/gff/graphics/book/ch09_04.htm
LZW Compression
code 0 1
key a b
LZW Compression
code 0 1 2
key a b ab
Original text = abababbabaabbabbaabba
p=a
pCode = 0
c=b
Represent a by 0 and enter ab into the code table.
Compressed text = 0
LZW Compression
code 0 1 2 3
key a b ab ba
• p = abba
• pCode = 8
• c = null
• Represent abba by 8.
• Compressed text = 012233588
Code Table Representation
code 0 1 2 3 4 5 6 7 8 9
key a b ab ba aba abb bab baa abbaabbaa
Dictionary.
Pairs are (key, element) = (key,code).
Operations are : get(key) and put(key, code)
Limit number of codes to 212.
Use a hash table.
Convert variable length keys into fixed length keys.
Each key has the form pc, where the string p is a key that is already in
the table.
Replace pc with (pCode)c.
Code Table Representation
code 0 1 2 3 4 5 6 7 8 9
key a b ab ba aba abb bab baa abbaabbaa
code 0 1 2 3 4 5 6 7 8 9
key a b 0b 1a 2a 2b 3b 3a 5a 8a
LZW Decompression
code 0 1
key a b
Original text = abababbabaabbabbaabba
Compressed text = 012233588
• 1 represents b.
• Decompressed text = ab
• pCode = 1 and p = b.
• lastP = a followed by first character of p is entered
into the code table.
LZW Decompression
code 0 1 2 3
key a b ab ba
Original text = abababbabaabbabbaabba
Compressed text = 012233588
• 2 represents ab.
• Decompressed text = abab
• pCode = 2 and p = ab.
• lastP = b followed by first character of p is entered
into the code table.
LZW Decompression
code 0 1 2 3 4
key a b ab ba aba
Original text = abababbabaabbabbaabba
Compressed text = 012233588
• 2 represents ab
• Decompressed text = ababab.
• pCode = 2 and p = ab.
• lastP = ab followed by first character of p is entered
into the code table.
LZW Decompression
code 0 1 2 3 4 5
key a b ab ba aba abb
Original text = abababbabaabbabbaabba
Compressed text = 012233588
• 3 represents ba
• Decompressed text = abababba.
• pCode = 3 and p = ba.
• lastP = ab followed by first character of p is entered
into the code table.
LZW Decompression
code 0 1 2 3 4 5 6
key a b ab ba aba abb bab
Original text = abababbabaabbabbaabba
Compressed text = 012233588
• 3 represents ba
• Decompressed text = abababbaba.
• pCode = 3 and p = ba.
• lastP = ba followed by first character of p is entered
into the code table.
LZW Decompression
code 0 1 2 3 4 5 6 7
key a b ab ba aba abb bab baa
Original text = abababbabaabbabbaabba
Compressed text = 012233588
• 5 represents abb
• Decompressed text = abababbabaabb.
• pCode = 5 and p = abb.
• lastP = ba followed by first character of p is entered
into the code table.
LZW Decompression
code 0 1 2 3 4 5 6 7 8
key a b ab ba aba abb bab baa abba
Original text = abababbabaabbabbaabba
Compressed text = 012233588
• 8 represents ???
• When a code is not in the table, its key is
lastP followed by first character of lastP.
• lastP = abb
• So 8 represents abba.
LZW Decompression
code 0 1 2 3 4 5 6 7 8 9
key a b ab ba aba abb bab baa abbaabbaa
Original text = abababbabaabbabbaabba
Compressed text = 012233588
• 8 represents abba
• Decompressed text = abababbabaabbabbaabba.
• pCode = 8 and p = abba.
• lastP = abba followed by first character of p is
entered into the code table.
Code Table Representation
code 0 1 2 3 4 5 6 7 8 9
key a b ab ba aba abb bab baa abbaabbaa
Dictionary.
Pairs are (key, element) = (code, what the code represents) = (code,
codeKey).
Operations are : get(key) and put(key, code)
Keys are integers 0, 1, 2, …
Use a 1D array codeTable.
codeTable[code] = codeKey.
Each code key has the form pc, where the string p is a code key that
is already in the table.
Replace pc with (pCode)c.
Time Complexity
Compression.
O(n) expected time, where n is the length of the text that is
being compressed.
Decompression.
O(n) time, where n is the length of the decompressed text.