Total Pageviews

Wednesday, December 2, 2026

LZW CODING

LZW CODING TECHNIQUE

Lempel–Ziv–Welch Algorithm

Lossless Data Compression in Digital Image Processing

Lossless Compression Dictionary Based Image Processing

1. Introduction to LZW Coding

LZW stands for Lempel–Ziv–Welch. It is a dictionary-based, lossless data compression technique developed by Terry Welch in 1984, based on the earlier work of Abraham Lempel and Jacob Ziv.

LZW coding is used to reduce the size of data by identifying frequently occurring patterns or sequences and replacing them with shorter numerical codewords.

Unlike lossy compression techniques, LZW does not remove any original information. The compressed data can be decoded to reconstruct the exact original data.

Important Concept: LZW creates a dictionary of frequently occurring symbol sequences. Whenever a sequence is encountered again, its dictionary code is used instead of storing the complete sequence.

Basic Characteristics

  • It is a lossless compression technique.
  • It uses a dynamically generated dictionary.
  • It does not require prior knowledge of symbol probabilities.
  • It identifies repeated sequences in the input data.
  • It uses the same dictionary-building principle during encoding and decoding.

2. Working Principle of LZW Coding

The basic idea of LZW coding is to replace repeated character sequences with dictionary indices.

The algorithm works in the following way:

  1. Initially, the dictionary contains all individual symbols of the input alphabet.
  2. The encoder reads the input sequence from left to right.
  3. It searches for the longest sequence already available in the dictionary.
  4. The code corresponding to that sequence is generated.
  5. A new dictionary entry is created by combining the current sequence with the next input symbol.
  6. The process continues until the complete input is encoded.

Basic Compression Flow

Original Data → Dictionary Search → Generate Code → Update Dictionary → Compressed Data
Repeated Sequence → Dictionary Code

For example, if the sequence AB appears repeatedly, the algorithm can assign a new dictionary code to AB and use that code whenever the sequence occurs again.

3. Dictionary Initialization

Before compression starts, LZW initializes its dictionary with all possible individual symbols from the input alphabet.

Consider a simple alphabet containing only A and B. The initial dictionary is:

Dictionary Code Symbol / String
0A
1B

The next available dictionary code is 2. New sequences will be added starting from this code.

Example: If the input contains A B A B A B A, new dictionary entries such as AB, BA and ABA can be created.

4. LZW Encoding Algorithm

Step-by-Step Algorithm

  1. Initialize the dictionary with all individual input symbols.
  2. Set the current string W to the first input symbol.
  3. Read the next input symbol K.
  4. Check whether the combined string WK exists in the dictionary.
  5. If WK exists, set W = WK.
  6. Otherwise:
    • Output the dictionary code of W.
    • Add WK to the dictionary with the next available code.
    • Set W = K.
  7. Repeat until all input symbols are processed.
  8. Finally, output the dictionary code of W.

Pseudocode for LZW Encoder

LZW_ENCODING(INPUT):

1. Initialize dictionary with all single symbols.

2. W = first symbol of INPUT

3. FOR each next symbol K in INPUT:

      IF W + K exists in dictionary:

          W = W + K

      ELSE:

          OUTPUT code(W)

          Add W + K to dictionary

          W = K

4. OUTPUT code(W)

5. END
Remember: The encoder adds a new dictionary entry only when the combined sequence is not already present.

5. Numerical Example of LZW Encoding

Consider the following input sequence:

Input = ABABABA

Step 1: Initialize Dictionary

Code Symbol
0A
1B

Next available code = 2.

Step 2: Perform Encoding

Step Current W Next K WK Exists? Output Code New Entry
1 A B No 0 AB = 2
2 B A No 1 BA = 3
3 A B Yes — —
4 AB A No 2 ABA = 4
5 A B Yes — —
6 AB A Yes — —

Step 3: Final Dictionary

Code Sequence Type
0AInitial
1BInitial
2ABNew
3BANew
4ABANew

Step 4: Final Compressed Output

Original Input = ABABABA

Encoded Output = 0, 1, 2, 4
The original seven symbols are represented by four dictionary codes. This illustrates how repeated patterns can be represented more compactly.

Note: This example illustrates the dictionary coding process. Actual compression ratio depends on the bit width of the codes and the size of the original representation.

6. LZW Decoding Algorithm

Decoding is the reverse process of encoding. It reconstructs the original input from the compressed code sequence.

Steps

  1. Initialize the dictionary with the original single symbols.
  2. Read the first compressed code.
  3. Find its corresponding string in the dictionary.
  4. Output that string.
  5. Read the next code and find its corresponding string.
  6. Add a new dictionary entry by joining the previous decoded string with the first character of the current decoded string.
  7. Continue until all codes are decoded.

Pseudocode for LZW Decoder

LZW_DECODING(CODES):

1. Initialize dictionary with all single symbols.

2. Read first code.

3. W = dictionary[first code]

4. OUTPUT W

5. FOR each next code K:

      IF K exists in dictionary:

          ENTRY = dictionary[K]

      ELSE:

          ENTRY = W + first_character(W)

      OUTPUT ENTRY

      Add W + first_character(ENTRY)
      to dictionary

      W = ENTRY

6. END
Special Case: During decoding, a code may occasionally refer to the next dictionary entry that has not yet been created. In that case, the decoder reconstructs the entry as W followed by the first character of W.

7. Applications of LZW Coding

1. GIF Image Format

GIF uses LZW compression to reduce image data size while preserving the original pixel information.

2. TIFF Image Format

LZW is supported as a compression option in TIFF files. It is useful for images containing repeated pixel patterns.

3. PDF Documents

LZW has also been supported as a compression filter in PDF document processing.

4. Text File Compression

Repeated words, characters and sequences can be stored using dictionary codes to reduce data size.

5. General Data Compression

LZW-based methods have been used in general-purpose compression utilities and other data storage systems.

Image Processing Application: In an image, repeated pixel intensity sequences can be replaced with dictionary codes. This helps reduce the amount of data required to represent the image.

8. Advantages of LZW Coding

  • Lossless Compression: The original data can be reconstructed exactly.
  • Simple Algorithm: It is relatively easy to understand and implement.
  • No Probability Calculation: It does not require prior knowledge of symbol probabilities.
  • Fast Processing: It can perform compression efficiently using dictionary lookup.
  • Repeated Pattern Detection: It efficiently represents recurring sequences.
  • Dynamic Dictionary: New patterns are automatically added during encoding.
  • Useful for Images: It is suitable for images containing repeated pixel patterns.

9. Disadvantages of LZW Coding

  • Dictionary Memory: The algorithm requires additional memory to maintain the dictionary.
  • Poor Compression on Random Data: Data without repeated patterns may not compress effectively.
  • Dictionary Growth: The dictionary may become large and require a size limit or reset strategy.
  • Unused Entries: Some dictionary entries may never be used again.
  • Code Size: Fixed-width implementations may require more bits as the dictionary grows.
  • Not Always Efficient: Compression performance depends on the characteristics of the input data.

10. LZW Coding: Important Properties

Property Description
Compression Type Lossless
Approach Dictionary Based
Dictionary Dynamic
Input Text, symbols or image data
Output Dictionary codewords
Probability Information Not required in advance
Common Applications GIF, TIFF and other compression systems

11. Conclusion

LZW coding is an important dictionary-based lossless compression algorithm used in data and image compression.

It works by identifying repeated sequences, storing them in a dynamically constructed dictionary, and replacing those sequences with numerical codes.

Because it preserves all original information and does not require prior probability calculations, LZW is an important technique to study in Digital Image Processing and Data Compression.


No comments:

Post a Comment