Monday, May 26, 2014

Sample GIF Image




Actual Image                          




Enlarged Image

 

Bytes

47 49 46 38 39 61 || 0A 00 0A 00 91 00 00 || FF FF FF FF 00 00 00 00 FF 00 00 00 || 21 F9 04 00 00 00 00 00 || 2C 00 00 00 00 0A 00 0A 00 00 || 02 16 8C 2D 99 87 2A 1C DC 33 A0 02 75 EC 95 FA A8 DE 60 8C 04 91 4C 01 00 || 3B

Header Block:

47 49 46 38 39 61
47 = G, 49 = I, 46 = F
38 = 8, 39 = 9, 61 = a

 

Logical Screen Descriptor: 

0A 00 0A 00 91 00 00

Image Width            =          0A       =          10 in decimal
Image Height           =          0A       =          10 in decimal
Packed Byte             =          91       =          10010001 in binary
            First BIT is Global Color Table Flag. Here it is, 1.
            Next 3 BITs are Color Resolution. Here it is, 001 = (N + 1) = 2 BITs/pixel
            Next BIT is the Sort Flag. Here it is, 0.
            Next 3 BITs are Size of Global Color Table. Here it is, 001 = 2^(N + 1) = 4.
Background Color              =          00       =          0 in decimal
Aspect Ratio                         =          00       =          0 in decimal


Global Color Table:

 FF FF FF FF 00 00 00 00 FF 00 00 00 

Global Color Table contains the list of all the colors that can be in the image & cannot contain colors that are not in that list. Each color is stored in 3 Bytes in RGB Format. Size of the Global Color Table in our case is 2^(1 + 1) = 4. This means it will take 3*4 = 12 Bytes in the Image Stream.
So,
SIZE IN LOGICAL SCREEN DESCRIPTOR
NO. OF COLORs
BYTE LENGTH
0
2
8
1
4
12
2
8
24
3
16
48
4
32
96
5
64
192
6
128
384
7
256
768

In our Image,
First Color is                         FF FF FF         =         WHITE
Second Color is                   FF 00 00        =          RED
Third Color is                       00 00 FF        =          BLUE
Fourth Color is                    00 00 00       =          BLACK


Graphic Control Extension Block:

21 F9 04 00 00 00 00 00
21 is the Extension Introducer. All extension begins with 21 only.
F9 is a Graphic Control Label & says current Extension Block is a Graphic Control Extension.
04 denotes the size of the Extension Block.
00 denotes the Packed Byte. Here it is 0, so all the subfields will be 0.
00 00 denotes the Delay Time, which is 0.
00 finally is the Block Terminator.


Image Descriptor:

2C 00 00 00 00 0A 00 0A 00 00
A single GIF image can contain multiple images. Each image begins with an Image Descriptor Block. The block is exactly 10 Bytes long.
2C is the Image Separator. Every Image Descriptor begins with 2C only.
00 00 denote the Image Left Position.
00 00 denotes the Image Right Position.
0A 00 denotes the Image Width.
0A 00 denotes the Image Height.
00 Last Byte is Packed Byte. So, all the sub-values will be 0. So, no Local Color Table & no Interlacing.


Image Data:

02 16 8C 2D 99 87 2A 1C DC 33 A0 02 75 EC 95 FA A8 DE 60 8C 04 91 4C 01 00
This is actual image data. The first byte of the block is the LZW Minimum Code Size. This value is used to decode the compressed output codes.  Here,
LZW Minimum Code Size is 2
Next Byte 16, tells us that 22 Bytes of data follow it (16 hex = 22 decimal). After we have read 22 Bytes, next value is 00. It indicates the end of Data Sub-Blocks.

In our Image,
The actual Image Data will be (10 x 10):

1 1 1 1 1 2 2 2 2 2
1 1 1 1 1 2 2 2 2 2
1 1 1 1 1 2 2 2 2 2
1 1 1 0 0 0 0 2 2 2
1 1 1 0 0 0 0 2 2 2
2 2 2 0 0 0 0 1 1 1
2 2 2 0 0 0 0 1 1 1
2 2 2 2 2 1 1 1 1 1
2 2 2 2 2 1 1 1 1 1
2 2 2 2 2 1 1 1 1 1

LZW Minimum Code is 2. So, initial code table will be:

CODE
COLOR(s)
#0
0
#1
1
#2
2
#3
3
#4
CC (Clear Code)
#5
EOI (End Of Information Code)

We have to refer to the LZW Data Compression Technique Page to understand the LZW Technique.

Output Codes by LZW Compression are:
#4 #1 #6 #6 #2 #9 #9 #7 #8 #10 #2 #12 #1 #14 #15 #6 #0 #21 #0 #10 #7 #22 #23 #18 #26 #7 #10 #29 #13 #24 #12 #18 #16 #36 #12 #5

Now because Variable Length Encoding is used so the Output Codes will be arranged in BYTEs according to the Code Size.


Trailer:

3B
Finally, 3B marks the End of Image File.


C CODE:

3 C Code Files have been attached:
  • Main File (gif.c)
  • Header File (header.h)
  • File containing required Functions (function.c)
 function.c                                           header.h                                           gif.c