GIF file format Translation

Source: Internet
Author: User
Tags transparent color

GIF file format Translation

Author: sunqing

Posting time

GIF animation files are widely used in network transmission. Although there are currently some processing tools for GIF animation files, I think there are still many examples worth developing. To help readers with this intention to have a clear basis for development, I have interpreted some useful parts of GIF development in the English instruction document of gif89a, it is hoped that it will be helpful for the development of GIF. The following is a specific translation.

I. Encoder



An Encoder is a program used to generate GIF files. Through grating data and other information, the encoder generates control blocks and data blocks necessary to reproduce the original image. It is responsible for completing the following tasks:

-Include all the information required to regenerate the original image.

-Ensure that the data stream is marked with the earliest version number that contains all data block definitions. This is to ensure that as many decoders as possible can process the data stream.

-Optimize the image encoding method to the decoder and avoid information redundancy as much as possible.

-To facilitate expansion, you should avoid grouping images, because hardware parameters may need to be reset during decoding.

-Set each bit of the field defined as reserved to zero. Note that some fields in the logical video and description that conform to the Image Descriptor are retained in 87a, but are useful in 89a.

Ii. Decoder



Decoder is a program used to process GIF data streams. It processes data streams sequentially, analyzes various data blocks and sub-blocks, uses control information to set hardware and processing parameters, and interprets data to display images.

The decoder is responsible for completing the following tasks:

· Process each image in the data stream in sequence and process the image latency according to the settings in the control information.

· Set hardware parameters as close as possible based on the control information set in the data stream.

3. Color Table



GIF format uses a color table to display grating-based images. A color table consists of a global color table and a local color table. The global color table works for images that do not have a local color table set. The scope of the global color table is the entire data flow. The partial color table works for a single image that follows it. The two color tables are optional. As a result, many images with no color table may appear in a GIF data stream. The decoder usually retains the last global color table until a new global color table is obtained. If there is neither a global color table nor a local color table in a data stream, the Decoder uses the Global color table preserved from the previous data stream for execution. You can set only one global color table in a set of data streams to adapt to a series of images. If no color table exists, the Decoder uses the system color table or its own color table. It should be noted that such a color table uses black and white as two starting items, so the monochrome image can be fully displayed.

The GIF format specifies that a data stream can only contain headers, logical video screen descriptors, global color tables, and GIF tail records.

Iv. Block, expansion, and domain



Data blocks in GIF data streams can be divided into three groups: control blocks, imaging blocks, and special purpose blocks. Control blocks, such as headers, logical video description blocks, image control extensions, and tail records, contain information used to control processing data streams or set hardware parameters. Imaging blocks, such as image descriptors and plain text extensions, contain information and data for imaging on display devices. Special purpose blocks, such as annotation extensions and Application Extensions, contain information that is neither used to process data streams nor used to form images on display devices. In addition to the description block and global color table of the logical visual screen, the scope of the special purpose block is the entire data stream, while the scope of other control blocks is limited, and only works for the imaging blocks behind them. The special-purpose block does not constitute a limit on any control block. It is transparent to the decoding process. Imaging blocks and extensions are used to control blocks and expand scope limitations. Block tags are divided into three sections: 0x00 ~ 0x7f is used for imaging blocks; 0x80 ~ 0xf9 is used to control blocks; 0xfa ~ 0xff is used for special purpose blocks. The decoder identifies block tags to process block scopes.

5. Block Size



The block size field in a block is used to record the number of bytes contained in the block, excluding the block size field and block Terminator. It is easy to skip a block and cannot be changed after the size is set. The length of data blocks and sub-blocks is variable to adapt to changes in data volumes.

Vi. Using GIF as an embedded Protocol



As an embedded protocol, GIF is part of a larger application protocol, in which GIF is used for imaging. Therefore, the application protocol can define the GIF data stream in a data block. When an application encounters a GIF data block, it calls the GIF decoder. Using the GIF extension to identify GIF data streams is a more effective method.

VII. Data sub-blocks



A. Data sub-blocks are some units that contain data. These units have no tags and are processed as the content of the control block. The first byte in the Data sub-block indicates the Data Length (0 ~ 255). The length does not include this byte.

B. required version: 87a

C. Syntax

7 6 5 4 3 2 1 0 field name Type

+ --------------- +

0 | block size byte

+ --------------- +

1 |

+--+

2 |

+--+

3 |

+--+

| Data value byte

. +--+

. |

. +-...-+

. |

+--+

|

+--+

255 |

+ --------------- +

D. Expansion and domain: This type of block is always part of other larger blocks, and it does not have its own scope.

8. Block Terminator

A. This is a zero-length data sub-block, used to indicate the end of the previous sub-block. It contains only one byte block size field. Its value is 0 and there is no data.

B. required version: 87a

9. Header

A. the header is used to identify GIF data streams in the context. The marking field indicates the beginning of the data stream. The version field is used to identify the function set required by a decoder to fully process the data stream. This block is required, and each data stream has only one header. It must appear at the beginning of the data stream.

B. Syntax

7 6 5 4 3 2 1 0 field name Type

+ --------------- +

0 | identifier 3 bytes

+--+

1 |

+--+

2 |

+ --------------- +

3 | version 3 bytes

+--+

4 |

+--+

5 |

+ --------------- +

I) identifier-contains three characters in GIF.

Ii) version-version: 87a or 89a

Iii) Expansion and domain-the domain of this block is the entire data stream. This block cannot be changed by any expansion.

10. Logical video description block



A. The logical video descriptor contains parameters used to define the area of the display device. The coordinates in the block are based on the upper-left corner of the virtual screen, instead of the absolute coordinates of the display device. This block is required, and each data stream can have only one logical video description block. This block is followed by the header.

B. Syntax

7 6 5 4 3 2 1 0 field name Type

+ --------------- +

0 | logical screen width unsigned

+--+

1 |

+ --------------- +

2 | logic screen height unsigned

+--+

3 |

+ --------------- +

4 | <packed fields> see below

+ --------------- +

5 | background color index byte

+ --------------- +

6 | pixel aspect ratio byte

+ --------------- +

<Packed fields> = global color table flag 1 bit

Color Scheme 3 bits

1 bit short mark

Global color table size 3 bits

I) Global color table flag-Indicates whether there is a global color table. If the position of this indicator is 1, the global color table will appear immediately after this block. This field is also used to explain whether to use the background color index field. If this position is 1, the value of the background color index field points to the background color table.

Ii) color scheme-the number of digits provided to each color of the original image minus 1. This value indicates the size of the entire palette used in the image, rather than the number of colors used in the image. For example, if the value of this field is 3, each color value in the color palette used in the image occupies four places.

Iii) Short flag-indicates whether the global color table is sorted. If this position is 1, the global color table is sorted according to the principle of decreasing importance. Typically, the color is sorted in descending order based on the color usage frequency. The color with the highest usage frequency ranks first in the color table. This helps the decoder select the best color subset to form an image.

Iv) Size of the global color table-if the position of the global color table is 1, the value of this field records the number of bytes occupied by the global color table.

V) background color index-indicates that the background color points to the global color table. Background Color refers to the color of the part of the video that is not covered by the back image. If the flag position of the global color table is 0, the field is also 0 and ignored.

Vi) pixel aspect ratio-used to calculate the approximate aspect ratio of pixels in the original image. If the value of this field is not 0, the aspect ratio of the pixel is calculated by the following formula.

Aspect Ratio = (pixel aspect ratio + 15)/64

The value range of this field ranges from to, and the increment step is 1/64.

Value: 0-No Ratio

1 ~ 255-value used for calculation

C. Expansion and domain. The block scope is the entire data flow. It cannot be changed by any extension.

11. Global color table

A. This block contains a red-green-blue color table in byte order. The global color table is used to expand images and plain text without a local color table. When the position of the global color table flag in the logical video description block is 1, a global color table exists. The global color table occupies the following number of bytes after the logical video description block:

3*2 ^ (Global color table size + 1)

B. required version: 87a

C. Syntax

7 6 5 4 3 2 1 0 field name Type

+ =====================+

0 | red 0 byte

+--+

1 | green 0 byte

+--+

2 | blue 0 byte

+--+

3 | red 1 byte

+--+

| Green 1 byte

+--+

Up |

+-...-+...

To |

+--+

| Green 255 byte

+--+

767 | blue 255 byte

+ =====================+

D. Expansion and domain: the block scope is the entire data stream. It cannot be changed by any extension.

12. Image Descriptor



A. Each image in the data stream is composed of an Image Descriptor, an optional local color table, and image data. Each image must be within the boundary of the logical video block defined in the logical video block. Image descriptors include necessary parameters for processing an image-based table. The given coordinates in this block refer to the pixel coordinates of the logical video screen. This block is an imaging block. You can add one or more control blocks before it, for example, image control expansion, or a local color table; image descriptors are always followed by image data. It is required for a graph. An image corresponds to an image descriptor. The number of images in a data stream is not limited.

B. required version: 87a

C. Syntax

7 6 5 4 3 2 1 0 field name Type

+ --------------- +

0 | image separator byte

+ --------------- +

1 | image left coordinate unsigned

+--+

2 |

+ --------------- +

3 | image top coordinate unsigned

+--+

4 |

+ --------------- +

5 | image width unsigned

+--+

6 |

+ --------------- +

7 | Image Height unsigned

+--+

8 |

+ --------------- +

9 | <packed fields> see below

+ --------------- +

<Packed fields> = Local color table flag 1 bit

1 bit of line separation mark

1 bit short mark

Retain 2 bits

Local color table size 3 bits

I) image separator-used to identify the beginning of the Image Descriptor. Take the fixed value 0x2c.

D. Expansion and domain: the domain of the block is a table-based image. This block can be changed by image control expansion.

13. Local color table

A. This block contains a color table in the red-green-blue primary color sequence. This table acts on the image following it. When the hidden table marks position 1, The table appears, followed by the Image Descriptor, and the number of bytes it contains is equal

3*2 ^ (local color table size + 1)

B. required version: 87a

C. Syntax

7 6 5 4 3 2 1 0 field name Type

+ =====================+

0 | red 0 byte

+--+

1 | green 0 byte

+--+

2 | blue 0 byte

+--+

3 | red 1 byte

+--+

| Green 1 byte

+--+

Up |

+-...-+...

To |

+--+

| Green 255 byte

+--+

767 | blue 255 byte

+ =====================+

D. Expansion and domain: the domain of this block is a table-based image data block followed by it. This block cannot be changed by any expansion.

Iv. Table-based image data



A. A table-based image data is composed of a series of sub-blocks, each of which can contain up to 255

Byte, which contains an index for the valid color table specified by each pixel in the graph. The order of indexes is encoded using the LZW algorithm.

B. required version: 87a

C. Syntax

Shows the format of image data.

7 6 5 4 3 2 1 0 field name Type

+ --------------- +

| LZW minimum size bytes

+ --------------- +

+ =====================+

|

// Image data sub-block

|

+ =====================+

I) LZW minimum encoding size: This byte is used to determine the initial number of digits encoded with LZW in image data.

E. Expansion and domain: the block has no domain, and it includes grid data. To change the extension of table-based images, it must appear before the corresponding image descriptor.

15. Image Control Expansion

A. Image Control expansion includes parameters required for processing an image block. Expansion only includes one data sub-block. This block is optional. Generally, only one image is controlled before the image block is expanded. This is also the only restriction on image control expansion in a data stream.

B. required version: 89a

C. Syntax

7 6 5 4 3 2 1 0 field name Type

+ --------------- +

0 | extended import character byte

+ --------------- +

1 | image control mark byte

+ --------------- +

+ --------------- +

0 | block size byte

+ --------------- +

1 | <packed fields> see below

+ --------------- +

2 | extended period: Unsigned

+--+

3 |

+ --------------- +

4 | transparent color index byte

+ --------------- +

+ --------------- +

0 | block end byte

+ --------------- +

<Packed fields> = retain 3 bits

Configuration method 3 bits

User input flag 1 bit

Transparent Color Flag 1 bit

I) extended introduction-used to identify the start of an extended block. This field is fixed with a value of 0x21.

Ii) image control labels-identify whether the current block is extended by graphic control. This field is a fixed value of 0xf9.

Iii) block size-the number of bytes contained in the block. From the block size field to the fast terminator (excluding the terminator ). This field contains a fixed value of 4.

Iv) configuration method-indicates the processing method after the image is displayed.

Value: 0-no specified configuration. No processing is required for the decoder.

1-No configuration value. The image will be left in the original position.

2-restore the background color. The area occupied by the image must be restored to the background color.

3-restore the previous color. The decoder must restore the image area to the color of the original image.

4-7-undefined.

V) user input flag-Indicates whether user input is required before further processing. It can be used with the input latency.

Vi) Transparent flag-Indicates whether a transparent index is specified in the transparent index field.

Vii) latency-if not 0, this field specifies the number of latencies in 1/100 seconds.

Viii) Transparent index-if a transparent index is encountered, the related pixels of the device are not changed and the next pixel is processed.

Ix) block Terminator-This 0-length field marks the end of image control expansion.

D. Expansion and domain: the extended domain is the subsequent imaging block; other extensions can be made between the block and its functional block. This block can change the image description block and expand the plain text.

E. Suggestion: I) configuration method-restore to some small areas used by the original method factory in the image; use this method to force the decoder to store the image areas to be retained, so this method should be used with caution. This method is not set for images in the entire image or in a large area.

Ii) user input flag-when this flag is set, the Decoder uses (0x07) to activate the sound to remind the user to input data. If no latency is set, the decoder waits for user input.

16. annotation Expansion



A. the annotation extension contains text information that is not actually part of the GIF data stream. It is applicable to include image comments, descriptions, or any other uncontrolled and non-image data. The annotation extension will be ignored by the decoder or retained for future processing. In any case, comment extension cannot interrupt the processing of data streams. This block is optional; there is no limit on the number of data streams.

B. required version: 89a

C. Syntax

7 6 5 4 3 2 1 0 field name Type

+ --------------- +

0 | extended import character byte

+ --------------- +

1 | comment mark byte

+ --------------- +

+ =====================+

|

N | annotate data sub-blocks

|

+ =====================+

+ --------------- +

0 | block Terminator byte

+ --------------- +

I) extended import operator-start of the extension of the identity. This field is a fixed value of 0x21.

Ii) annotation label-indicates a part as annotation extension. This segment is the fixed value 0xfe.

Iii) Comment data-in the subblock order, each comment must be at least 1 byte and at most 255 byte. The end of a sequence is identified by a block Terminator.

Iv) block Terminator-this zero-length data block is used to identify the end of the comment extension.

D. Expansion and domain: the block does not have a domain and cannot be modified by any expansion.

E. Suggestions

I) Data-This block is easy to use. It uses the 7-bit ASCII character set. Cannot be used to store control information.

Ii) location-this block can appear anywhere in the data stream. It is recommended that the block be at the beginning or end of the data stream.

VII. plain text Extension



A. plain text extensions include plain text data and parameters required to display data as images. Text data will be encoded with seven printable ASCII characters. Text data is represented by cell character elements defined in block fields. Each character is represented by a unit. Is a monochrome character, with the most appropriate font, font size. Number of characters in a grid. If the number of characters is insufficient, the number of characters in the grid must be discarded. The encoder should be accurately calculated to avoid surplus. This block uses a global color table. It can be modified by the graphic control extension. This block is optional.

B. required version: 89a.

C. Syntax

7 6 5 4 3 2 1 0 field name Type

+ --------------- +

0 | extended import character byte

+ --------------- +

1 | Mark byte in plain text

+ --------------- +

+ --------------- +

0 | block size byte

+ --------------- +

1 | unsigned, left coordinate of a text Grid

+--+

2 |

+ --------------- +

3 | text lattice coordinate unsigned

+--+

4 |

+ --------------- +

5 | text lattice width unsigned

+--+

6 |

+ --------------- +

7 | text grid height unsigned

+--+

8 |

+ --------------- +

9 | character unit width byte

+ --------------- +

10 | character unit height byte

+ --------------- +

11 | text foreground color index byte

+ --------------- +

12 | text background color index byte

+ --------------- +

+ =====================+

|

N | text-only data sub-block

|

+ =====================+

+ --------------- +

0 | block Terminator byte

+ --------------- +

I) extended import operator-identifies the start of an extended block. Take the fixed value 0x21.

Ii) Text Markup-the current block is expanded to the original text. Take the fixed value 0x01.

Iii) block size-number of bytes in the expansion, with a fixed value of 12.

Iv) left position of the text grid-left column of the logic video screen, represented by pixels,

V) The top position of the text Lattice-the top line of the logic video screen, represented by pixels.

Xii) original data-sub-block sequence. Each sub-block must be at least 1 byte and at most 255 bytes. This sequence ends when it encounters a block Terminator.

Xiii) block Terminator-This 0-length data block is used to indicate block termination.

D. Expansion and domain: the domain of the block contains the original text data. It can be modified by image control extension.

E. Suggestions

The data in the original extension is formatted in advance. The decoder determines the font and font size. If a character is less than 0x20 or greater than 0xf7, the decoder will process it by space 0x20. For the widest compatibility, the character unit should be 8*8 or 8*16 lattice.

18. Application Expansion



A. the application extension contains the description of the application. It complies with the syntax of the extended block described below and marks the block as 0xff.

B. required version: 89a

C. Syntax

7 6 5 4 3 2 1 0 field name Type

+ --------------- +

0 | extended import character byte

+ --------------- +

1 | expanded byte

+ --------------- +

+ --------------- +

0 | block size byte

+ --------------- +

1 |

+--+

2 |

+--+

3 | Application ID 8 bytes

+--+

4 |

+--+

5 |

+--+

6 |

+--+

7 |

+--+

8 |

+ --------------- +

9 |

+--+

10 | application verification code 3 bytes

+--+

11 |

+ --------------- +

+ =====================+

|

| Application Data sub-block

|

|

+ =====================+

+ --------------- +

0 | block Terminator byte

+ --------------- +

I) extended introduction operator-defines the extended block of the block bit. Take the fixed value 0x21.

Ii) application expansion tag-identifies the application expansion block. Take the fixed value 0xff.

Iii) block size-indicates the number of bytes contained in the block. Take the fixed value 11.

Iv) The Application Identifier-8 GB printable ASCII characters are used to identify the application expansion to which the application belongs.

V) The sequence of three-byte proof codes is used to prove the application identifier. An application can use an algorithm to calculate a binary code to uniquely identify whether the application is larger than the application.

D. Expansion and domain: the block does not have a domain and cannot be modified by other extensions.

19th. End record



A. This block is a single field block to indicate the end of the data stream. Take the fixed value 0x3b.

B. required version: 87a.

C. Syntax

7 6 5 4 3 2 1 0 field name Type

+ --------------- +

0 | byte of the GIF tail record

+ --------------- +

D. Expansion and domain: This block has no domain and is used to end the GIF data stream. It cannot be modified by any extension.

Appendix A. Quick Reference Table.

The block name must be an extended version.

The optional value of application expansion (*) 0xff (255) is 89a.

Annotation extension optional (*) 0xfe (254) is 89a

Global color table (1) No 87a

Image Control expansion optional (*) 0xf9 (249) is 89a

The header must be (1) N/

Image Descriptor optional (*) 0x2c (044) No 87a (89a)

Local color table (optional) No 87a

The logical video description block must be (1) No 87a (89a)

Expanded plain text (optional) 0x01 (001) is 89a

The end record must be (1) 0x3b (059) No 87a

Unlabeled Block

The header must be (1) N/

The logical video descriptor must be (1) No 87a (89a)

Global color table (1) No 87a

Local color table (optional) No 87a

Imaging Block

Expanded plain text (optional) 0x01 (001) is 89a

Image Descriptor optional (*) 0x2c (044) No 87a (89a)

Control Block

Image Control expansion optional (*) 0xf9 (249) is 89a

Special purpose Block

The end record must be (1) 0x3b (059) No 87a

Annotation extension optional (*) 0xfe (254) is 89a

The optional value of application expansion (*) 0xff (255) is 89a.

Legend: (1) a maximum of one

(*) Zero or multiple occurrences

(+) One or more occurrences

Note: The header is not restricted by the version number.

The description blocks and image descriptors of the logical visual screens of version 89a retain their syntaxes from version 87a to version 89a. However, some fields are reserved for version 87a, but they are useful for version 89a.

Appendix B. gif syntax.

Syntax is a symbolic form used to express a sequence, and some objects in the expression sequence are used to form larger objects. The syntax is also used to express the number of objects that appear at a given position. The syntax provided here is used to describe the block sequence of the GIF data stream, which is expressed by some rule lists. The symbol definitions used for GIF syntax are listed below.

Legend: <> syntax

::= Symbol Definition

* Zero or more events occur

+ 1 or more events

| Substitution Element

[] Optional elements

Example:

<GIF data stream >::= header <logical video screen> <DATA> * tail record

This rule defines the <GIF data stream> object as follows. It must start with a head, followed by a logical visual object, which is defined by other medicinal rules. Finally, the data object is connected to the Terminator. * After the data entity indicates that the data entity can appear 0 or multiple times at this position.

Syntax

<GIF data stream >::= header <logical video screen> <DATA> * tail record

<Logical video screen>: = logical video description block [global color table]

<Data >::= <image block> |

<Special Purpose block>

<Image block>: = [image control expansion] <image block>

<Image block >::= <Table-based image> |

Plain text Extension

<Table-based image >:: = Image Descriptor [local color table] image data

<Special Purpose block>: = application expansion |

Annotation Extension

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.