Você está na página 1de 19

LZW

Compression
Compression:

It is a process of reducing size by encoding its information


more efficiently.
Due to this there is a reduction in the number of bits and bytes
used to store the information.
A smaller file size is generated in order to achieve a faster
transmission of electronic files and a smaller space required
for its downloading.
LZW compression:

LZW is the first letter of the names of the scientists Abraham


Lempel, Jakob Ziv, and Terry Welch, who developed this
algorithm.
Is a lossless compression algorithm.
It is simple and is dictionary based.
Working of LZW compression:

It is a simple process.
It replaces strings of characters with single codes.
A new string of characters is added every time it sees to a
table of strings.
Compression occurs when a single code is output instead of a
string of characters.
The first 256 codes are by default assigned to the standard
character set.
Continue

Starts with a dictionary of all the single characters and


gradually builds the dictionary as the information is sent
through.

Lossless compression hence works good for text compression.

Uses a code table with 4096 as a common choice for number


entries.
Why to use LZW compression:

Two reasons . . .

For most palette color images LZW yields the highest


compression efficiencywithout sacrificing image data.

TheGIFimage file format is thede factostandard for images


on the web. Most GIF files use the LZW compression.
LZW Algorithm:

The LZW Compression Algorithm can summarised as follows:


w = NIL;
while ( read a character k )
{
if wk exists in the dictionary
w = wk;
else
add wk to the dictionary;
output the code for w;
w = k;
}
LZW Encoding:

If the message to be encoded consists of only one character,


LZW outputs the code for this character; otherwise it inserts
two-or multi-character, overlapping.

The last character of a pattern is the first character of the next


pattern.
LZW Encoding algorithm:

Prefixfirst input character;


CodeWord256;
while(not end of character stream)
{ Char next input character;
if(Prefix+ Char exists in the Dictionary)
Prefix Prefix + Char;
Else
{
Output : the code for Prefix;
insertInDictionary( (CodeWord, Prefix + Char) ) ;
CodeWord++;
PrefixChar;
}
}
Example:
LZW Decoding:

The LZW decompressor creates the same string table during


decompression.

Initialize Dictionary with 256 ASCII codes and corresponding


single character strings as their translations.
LZW Decoding algorithm:

output: string(firstCodeWord);

while(there are more CodeWords)


{
If(Current code word is in the dictionary)
Output: String(Current Code Word);
Else
Ouput: PreviousOutput+Previous Output First character;
Insert in the dictionary: Previous output+ current output First
character.
}
Example:
Advantages :

This is a lossless compression technique, none of the contents


in the file are lost during or after compression.

LZW algorithm is efficient because it does not need to pass the


string table to the decompression code.

The table can be recreated as it was during compression, using


the input stream as data. This avoids insertion of large string
translation table with the compression data.
Disadvantages:

What happens when the dictionary becomes too large?

One approach is to throw the dictionary away when it reaches


a certain size.

Useful only for large amount of text data when redundancy is


high.
Applications:

A largeEnglishtext file can typically be compressed via LZW


to about half its original size.

LZW became very widely used when it became part of theGIF


image format in 1987. It may also (optionally) be used inTIFF
andPDFfiles.

Você também pode gostar