Wednesday, May 21, 2014

LZW Decompression

Before we proceed, below are the terms we will be using frequently.

  •          Code Table: Maintains the Codes & their Values.
  •          Index Stream: The O/P generated by the Decoder.
  •          Code Stream: This will be the list of codes as I/P.
  •          CODE: It will denote the Current Code in the Code Stream.
  •          CODE – 1: It will denote the Code just before CODE in the Code Stream.
Decompression ALGORITHM
  • Initialize Code Table
  • Let CODE be the first code in the Code Stream
  • Output {CODE} to Index Stream
  • <LOOP POINT>
  • Let CODE be the next code in the Code Stream
  • Is CODE in the Code Table?
  • Yes:
    • Output {CODE} to Index Stream
    • Let K be the first index in {CODE}
  • No:
    • Let K be the first index of {CODE-1}
    • Output {CODE-1}+K to Index Stream

  • Add {CODE-1}+K to the Code Table
  • Return to LOOP POINT


Now let us understand using an example.

  •          Suppose our Code Stream is: 
#256 #65 #66 #258 #67 #68 #69 #70 #260 #68 #261 #263 #258 #257
  •         Set code stream to point to the first Code of I/P Stream.
  •         Initialize the CODE Table.

CODE TABLE:

CODE
VALUE
.
.
.
.
A
#65
B
#66
C
#67
D
#68
E
#69
F
#70
.
.
.
.
ASCII value at 255
#255
CC
#256
EOI
#257


Working Procedure of the Algorithm:

WORKING PROCEDURE:

STEP
ACTION
CODE STREAM
CODE – 1
K
NEW CODE TABLE ENTRY
INDEX STREAM
0
Init
#65
-
-
-
A
1
Read
#66
#65
-
-
-
2
Found
#66
#65
#66
#258 - AB
B
3
Read
#258
#66
-
-
-
4
Found
#258
#66
#65
#259 - BA
AB
5
Read
#67
#258
-
-
-
6
Found
#67
#258
#67
#260 – ABC
C
7
Read
#68
#67
-
-
-
8
Found
#68
#67
#68
#261 - CD
D
9
Read
#69
#68
-
-
-
10
Found
#69
#68
#69
#262 - DE
E
11
Read
#70
#69
-
-
-
12
Found
#70
#69
#70
#263 - EF
F
13
Read
#260
#70
-
-
-
14
Found
#260
#70
#65
#264 - FA
ABC
15
Read
#68
#260
-
-
-
16
Found
#68
#260
#68
#265 - ABCD
D
17
Read
#261
#68
-
-
-
18
Found
#261
#68
#67
#266 - DC
CD
19
Read
#263
#261
-
-
-
20
Found
#263
#261
#69
#267 - CDE
EF
21
Read
#258
#263
-
-
-
22
Found
#258
#263
#65
#268 - EFA
AB
23
Read
EOI
-
-
-
-

Lets start reading through the Code Stream we have created while Compressing. The first value in the Code Stream should be a Clear Code. This means we should initialize our Code Table.

            The next step is to read the first Code, we then simply O/P its value to the Index Stream. Now we enter the Main LOOP of the algorithm. The next code gets assigned to CODE which now makes that value #65. Next we check whether it is in the Code Table. Yes it is. This means we must first O/P the value of CODE to Index Stream. Then we must find the first index in the value of CODE & call this K. Now we simply add {CODE - 1} + K to the Code Table.

We start the LOOP again reading the next code. CODE now would be #66. We do have it in our Code Table. So the same procedure will be repeated. Similarly, the Algorithm will continue.

Finally, our O/P Index Stream will be:

ABABCDEFABCDCDEFAB

A C Code File is attached in the last (lzwD.c)