GIF, known as Graphic Interchange Format, is widely used in World Wide Web (WWW).
It supports 256 colors & also
supports animation. It uses famous LZW
Data Compression technique to reduce the size of Image Data without
degrading the visual data.
A GIF Image
is made up of a bunch of different “blocks”
of data. Every block has its own significant role in defining the Image
Properties.
The File Organization is as follows:
- GIF Header
- GIF Extension Headers
- IMAGE Data
- GIF Trailer
GIF Header
All GIF files must start with a Header Block. The header
takes up the first 6 Bytes of the
file.
struct
GIFHeader{
/* Header Block */
BYTE Signature[3];
BYTE Version[3];
/* Logical Screen Descriptor */
WORD ScreenWidth;
WORD ScreenHeight;
BYTE Packed;
BYTE BackgroundColor;
BYTE AspectRatio;
}
Header Block
First 3 Bytes are
called the signature. These should
always be “GIF” (i.e. 47 = ‘G’, 49 = ‘I’, 46 = ‘F’).
The next 3 Bytes
specify the version of the
specifications that was used to encode the image. It may be 87a (i.e. 38 = ‘8’, 37 = ‘7’, 65 = ‘a’) or 89a (i.e. 38 = ‘8’, 39 = ‘9’, 65 = ‘a’). Generally, 89a is
used now.
Logical Screen Descriptor
It always immediately follows the Header. This block tells
the decoder how much room this image will take up. It is exactly 7 Bytes long.
ScreenWidth & ScreenHeight fields contain the Width
& Height of the image.
Now we have a packed
byte. It means that this byte has actually multiple values stored in its BITs. PACKED
contains following 4 subfields:
·
BITs 0-2 Size
of Global Color Table
·
BIT 3 Color
Table Sort Flag
·
BITs 4-6 Color
Resolution
·
BIT 7 Global
Color Table Flag
Size of
Global Color Table subfield contains the no. of BITs in each Global
Color Table entry minus 1. For
example, if image has 8 BITs per pixel, the value of this field is 7. Size of
Global Color Table is set the proper size even if there is Global Color Table.
If Color Table Sort
Flag is 1, then all the Global
Color Table entries are sorted from the most important (most frequently
occurring color) to the least important.
Color
Resolution subfield is set to the no. of BITs in an entry of the
original color palette – 1. This
value equates to the maximum size of the original color palette. For example,
if an image has 8 BITs per primary color, the value of this field would be 7.
The Global Color Table
Flag is set to 1 if Global Color
Table is present otherwise it is set to 0.
If Global Color Table is present it always follows the Logical Screen
Descriptor.
BackgroundColor contains
an index value into the Global Color Table of the color to use for the border
& background of the image. If there is no Global Color Table then the field
is unused & should be ignored.
AspectRatio contains
the aspect ratio value of the pixels of the image. It is the width of the pixel
divided by the height of the pixel.
Global Color Table
The Logical Screen Descriptor may be followed by an optional
Global Color Table. This color table, if present, is the color map used to
index the pixel color data contained within the image data. If a Global Color
Table is not present, each image stored in the GIF file contains a Local Color
Table that it uses in place of a Global Color Table. If every image in the GIF
file uses its own Local Color Table, then a Global Color Table may not be present in the GIF file.
struct GIFColorTable{
BYTE
Red;
BYTE
Green;
BYTE
Blue;
}
Global Color Table data always
follows the Logical Screen Descriptor and varies in size depending upon the
number of entries in the table. The Global Color Table is a series of
three-byte triples making up the elements of the color table. Each triple
contains the red, green, and blue primary color values of each color table
element.
The number
of entries in the Global Color Table is always a power of two (2, 4,
8, 16, and so on), up to a maximum of 256 entries. The size of the Global Color Table in bytes is
calculated by using bits 0, 1, and 2 in the Packed field of
the Logical Image Descriptor in the following way:
ColorTableSize
= 3 * (1 << (SizeOfGlobalColorTable + 1))
The Header,
Logical Screen Descriptor, and Global Color Map data are followed by one or
more sections of image data. Each image in a GIF file is stored separately,
with an Image Descriptor and possibly a Local Color Table. The Image Descriptor
is similar to a header and contains information only about the image data that
immediately follows it. The Local Color Table contains color information
specific only to that image data and may or may not be present.
Local Image Descriptor
struct GIFLocalImage{
BYTE Separator;
WORD Left;
WORD Top;
WORD Width;
WORD Height;
BYTE Packed;
}
Separator contains the value 2Ch and denotes the
beginning of the Image Descriptor data block.
Left and Top are the coordinates in pixels of the upper-left corner of the
image on the logical screen. The upper-left corner of the screen is considered
to be coordinates 0, 0.
Width and Height are the size of the image in pixels.
Packed contains the following five subfields
of data (bit 0 is the LSB):
·
BIT
0 Local Color Table Flag
·
BIT
1
Interlace Flag
·
BIT
2
Sort Flag
·
BITs
3-4
Reserved
·
BITs
5-7
Size
of Local Color Table
The Local
Color Table Flag subfield is 1
if a Local Color Table is associated with this image. If the value of this
subfield is 0, then there is no
Local Color Table present, and the Global Color Table data should be used
instead.
The Interlace
Flag subfield is 1 if the image
is interlaced and 0 if it is
non-interlaced.
The Sort
Flag subfield indicates whether the entries in the color table have been
sorted by their order of importance. Importance is usually decided by the
frequency of occurrence of the color in the image data. A value of 1 indicates a sorted color table, while
a value of 0 indicates a table with
unsorted color values.
The Size
of Local Color Table Entry subfield is the number of bits per entry in the
Local Color Table. If the Local Color Table Flag subfield is set to 0, then this subfield is also set to 0.
Local Color Table
If a Local Color Table is present, it immediately follows the
Local Image Descriptor and precedes the image data with which it is associated.
The format of all Local Color Tables is identical to that of the Global Color
Table. Each element is a series of 3-byte triples containing the red, green,
and blue primary color values of each element in the Local Color Table.
struct
GIFLocalColor{
BYTE Red;
BYTE Green;
BYTE Blue;
}
A Local
Color Table only affects the image it is associated with and, if it is present,
its data supersedes that of the Global Color Table. Each image may have no more
than one Local Color Table.
Between Image Header
& Local Image Descriptor, there
might be many Control Extension Blocks.
These Extensions add flexibility to the Image. We will be discussing these
Extensions in the next part of this Topic.