Monday, May 26, 2014

GIF Image Format – 2



Graphics Control Extension Block controls how the bitmap or plain-text data found in a Graphics Rendering block is displayed. Such control information includes whether the graphic is to be overlaid in a transparent or opaque fashion over another graphic, whether the graphic is to be restored or deleted, and whether user input is expected before continuing with the display of the GIF file data.
Plain Text Extension Block allows the mixing of plain-text ASCII graphics with bitmapped image data. Many GIF images contain human-readable text that is actually part of the bitmap data itself. Using the Plain Text Extension, captions that are not actually part of the bitmapped image may be overlaid onto the image. This can be invaluable when it is necessary to display textual data over an image, but it is inconvenient to alter the bitmap to include this information. It is even possible to construct an 89a file that contains only plain-text data and no bitmap image data at all.
Comment Extension Block contains human-readable ASCII text embedded in the GIF data stream that is used in a manner similar to program comments in C language code.
Application Extension Block allows the storage of data that is understood only by the software application reading the GIF file. This data could be additional information used to help display the image data or to coordinate the way the image data is displayed with other GIF image files

Graphic Control Extension Block

struct GIFGCExtension{
            BYTE Introducer;
            BYTE Label;
            BYTE BlockSize;
            BYTE Packed;
            WORD DelayTime;
            BYTE ColorIndex;
            BYTE Terminator;
}

Introducer contains the value 21h and is used to identify the start of a Extension data block. 

Label contains the value F9h and is used to identify this block of data as a Graphics Control Extension. 

BlockSize contains the value 04h, which is the number of bytes in the Packed, DelayTime, and ColorIndex fields. 

Packed contains the following four subfields of information (bit 0 is the LSB):
·        BIT      0          Transparent Color Flag
·        BIT      1          User Input Flag
·        BITs    2-4      Disposal Method
·        BITs    5-7      Reserved 

If the Transparent Color Flag subfield is set to 1, the ColorIndex field of this extension contains a color transparency index. If no index is present, this bit is set to 0. 

The User Input Flag subfield is set to 1 if user input (key press, mouse click, and so forth) is expected before continuing to the next graphic sequence; otherwise, this bit is set to 0.

The Disposal Method subfield contains a value indicating how the graphic is to be disposed of once it has been displayed. The currently defined values for this field are 00h (disposal method not specified), 01h (do not dispose of graphic), 02h (overwrite graphic with background color), and 04h (overwrite graphic with previous graphic). 

The Reserved subfield is not used in GIF89a and is always set to 0. 

DelayTime in the Graphics Control Extension block contains a value equal to the number of hundredths of a second that must elapse before the graphics presentation continues. If this field is 0, then no delay is used. If both this delay and the user input bit is set, the graphic continues when either the delay expires or user input is received. 

ColorIndex is the color transparency index. This field contains a value only if the Transparent Color Flag subfield in the Packed field is set to 1. 

Terminator contains the value 0 and marks the end of the Graphics Control Extension block.



Plain Text Extension Block

struct GIFPTExtension{
            BYTE Introducer;
            BYTE Label;
            BYTE BlockSize;
            WORD TextGridLeft;
            WORD TextGridTop;
            WORD TextGridWidth;
            WORD TextGridHeight;
            BYTE CellWidth;
            BYTE CellHeight;
            BYTE TextFgColorIndex;
            BYTE TextBgColorIndex;
            BYTE *PlainTextData;
            BYTE Terminator;
}
Introducer contains the value 21h and is used to identify the start of a Extension data block. 

Label contains the value 01h and is used to identify this block of data as a Plain Text Extension. 

BlockSize contains the value 0Ch, which is the number of bytes contained in the fields following the BlockSize field. 

TextGridLeft and TextGridTop contain the X and Y coordinates (position) of the text grid with respect to the upper-left corner of the display screen (coordinate 0, 0). 

TextGridWidth and TextGridHeight contain the size of the text grid in pixels.

CellWidth and CellHeight contain the size in pixels of each character cell in the grid. 

TextFgColorIndex contains an index into the Global Color Table to retrieve the color of the text. 

TextBgColorIndex contains a Global Color Table index value to be used as the color for the background of the text. 

PlainTextData contains the actual textual information that is to be rendered as a graphic. This field contains one or more sub-blocks of data. Each sub-block begins with a byte that indicates the number of data bytes that follow. From 1 to 255 data bytes may follow this byte. There may be any number of sub-blocks in this field. 

Terminator contains the value 0 and marks the end of the Plain Text Extension block.


Application Extension Block

struct GIFAppExtension{
            BYTE Introducer;
            BYTE Label;
            BYTE BlockSize;
            CHAR Identifier[8];
            BYTE AuthentCode[3];
            BYTE *ApplicationData;
            BYTE Terminator;
}
Introducer contains the value 21h and is used to identify the start of a Extension data block. 

Label contains the value FFh and is used to identify this block of data as an Application Extension. 

BlockSize contains the value 0Bh, which is the number of bytes in the Identifier and AuthentCode fields. 

Identifier may contain up to eight printable 7-bit ASCII characters. These characters are used to identify the application that wrote the Application Extension block. If this identifier value is recognized, the remaining portion of the block is read and its data acted upon. If the identifier value is not recognized, the remaining portion of the block is read and its data is discarded. 

AuthentCode contains a value that is used to uniquely identify a software application that created the Application Extension block. This field may contain a serial number, a version number, or a unique binary or ASCII code used to identify an individual software application or computer platform. This field may be used to allow only specific copies or revisions of a particular software application to access the data in certain Application Extension blocks.

ApplicationData contains the information that is used by the software application. This field is structured in a series of sub-blocks identical to the data found in a Plain Text Extension block. 

Terminator contains the value 0 and marks the end of the Application Extension block.

Comment Extension Block

struct GIFCommExtension{
            BYTE Introducer;
            BYTE Label’
            BYTE *CommentData;
            BYTE Terminator;
}
Introducer contains the value 21h and is used to identify the start of a Extension data block. 

Label contains the value FEh and is used to identify this block of data as a Comment Extension. 

CommentData contains one or more sub-blocks of ASCII string data. The character strings stored in the CommentData field sub-blocks are not required to be NULL-terminated. 

Terminator contains the value 0 and marks the end of the Comment Extension block.

Image Data

Finally we get to the actual image data. The image data is composed of a series of output codes which tell the decoder which colors to spit out to the canvas. These codes are combined into the bytes that make up the block. The first byte of this block is the LZW minimum code size. This value is used to decode the compressed output codes. The rest of the bytes represent data sub-blocks. Data sub-blocks are groups of 1 - 256 bytes. The first byte in the sub-block tells us how many bytes of actual data follow. This can be a value from s (00) it 255 (FF).
After we have read those bytes, the next byte we read will tell us how many more bytes of data follow that one. We continue to read until we reach a sub-block that says that 0 bytes follow.

Trailer

The Trailer Block indicates that we reached the end of the image file. Its value is always 3B.