What Is The Size Of An Animated Gif Image In Bytes
The authority on the content of GIFs is the GIF89a specification. Originally developed at CompuServe in the late 1980s, it is now a W3C standard.
A GIF file is made upward of a sequence of data blocks. The start ii blocks are stock-still length and fixed format. Later ones are variable length but self-describing; they consisting of a byte identifying the block blazon, followed by a payload length byte, followed by payload.
The post-obit railroad diagram shows all of the different types of blocks and where they can be in the file. Every path post-obit the arrows corresponds to a valid block sequence. The large middle secction, in item, tin can be repeated any number of times.
Nosotros will learn more by walking through a sample GIF file. Y'all can see the sample file and its corresponding bytes below.
Annotation that not all possible block types are represented in this sample file. Later on we'll provide samples of missing cake types where advisable. The different types of blocks include: header, logical screen descriptor, global color tabular array, graphics control extension, image descriptor, local color tabular array, image data, plain text extension, application extension, comment extension, and trailer. Permit's get started with the first block!
Header Block
From the sample file:
All GIF files must start with a header block. The header takes upward the first 6 bytes of the file. These bytes should all correspond to ASCII graphic symbol codes. The beginning iii bytes are called the signature. These should ever be "GIF" (ie 47="Thousand", 49="I", 46="F"). The next three specify the version of the specification that was used to encode the image.
Normally the version cord will be either "89a" (ie 38="8", 39="9",61="a") or "87a" (ie 38="8", 37="vii",61="a"). All modern GIF-processing software recognizes both versions, For maximum compatibility, GIFLIB volition normally write an 87a signature unless the file contains GIF89 features.
Logical Screen Descriptor
From Sample File: 0A 00 0A 00 91 00 00
The logical screen descriptor e'er immediately follows the header. This cake tells the decoder how much room this paradigm will take up. It is exactly 7 bytes long. It starts with the canvas width and canvas height. These value tin can exist institute in the first 2 pairs of two bytes each. Both are sixteen-bit, nonnegative integers (0-65,535).
Equally with all the other multi-byte values in the GIF format, the least significant byte is stored first (petty-endian format). This ways where we would read 0A 00 from the byte stream, we would usually write it equally 000A which is the same as ten. Thus the width of our sample image is 10 pixels. Equally a farther example 255 would be stored as FF 00 but 256 would be 00 01 .
The canvas width and height are ordinarily ignored past mod viewers. The GIF format seems to take been designed with the idea that viewers would render multiple images in a GIF on a common sail, giving an consequence like a picture wall. But nowadays multiple-epitome GIFs are generally used either equally animations in which each sub-image is a frame or every bit paradigm libraries, with the GIF client handling compositing into some canvas about which the GIF format holds no information.Thus, the canvass width and height are mainly fossils. GIFLIB does extract them and permit you to set them, however.
The next byte contains four fields of packed data, the "logical screen descriptor". To sympathize these, we demand to expand the byte 91 to binary as 10010001 and expect at the fields within information technology.
The start (about-significant) fleck is the global color table flag. If information technology'south 0, so there is no global colour table. If it's ane, then a global colour tabular array will follow. In our sample image, nosotros can see that we will have a global color table (as will ordinarily be the case).
The next 3 bits are the color resolution. They are simply meaningful if in that location is a global color table, and allow you to compute its size. If the value of this filed is North, the number of entries in the global color tabular array volition exist 2 ^ (N+1) - that is, two raised to the power (N+i). Thus, the 001 in the sample image represents 2 bits/pixel; 111 would represent eight bits/pixel.
The GIF format shows its age here. A more modern design would simply have allocated a byte or two for color table length. Simply GIF was designed when retention was much more expensive than it is today, and the designers felt strong pressure to economize on every bit. The consequence is that colour table lengths accept to be an exact power of ii. Perversely, this tin can force a waste of memory space in images with odd color counts.
The next single bit is the sort flag. If the values is 1, then the colors in the global color tabular array are sorted in society of "decreasing importance," which typically means "decreasing frequency" in the image. This tin help the image decoder, but is not required. In the sample file this value has been left at 0.
The sort flag reflected the high price of dual-port retention at the fourth dimension the GIF specification was written in the late 1980s. That kind of limit disappeared in the mid-1990s, and modern GIF software ignores this flag. Until version 5.0, GIFLIB ignored it on input and zeroed it on output; five.0 and after versions read and preserve it.
The adjacent byte gives us the background color index. This byte is only meaningful if the global color table flag is 1, and if there is no global color tabular array, this byte should exist 0.. To empathize it you have to remember the original "flick wall" rendering model for GIFs in which sub-images are composited onto a larger sail. It represents which index in the global color table should exist used for pixels on the virtual canvas that aren't overlayed by an image. GIFLIB supports reading and setting this byte, but modernistic viewers and browsers by and large take no utilize for it.
The last byte of the logical screen descriptor is the pixel attribute ratio. Modern viewers don't use this. Until five.0, GIFLIB ignored this flag on input and zeroed it on output; now it is read and preserved if present. The GIF standard doesn't requite a rationale for information technology, but information technology seems probable that the designers intended information technology for representing image captures from the analog television receiver of the twenty-four hour period, which had rectangular pixel-equivalents. The GIF specification says that if in that location was a value specified in this byte, N, the bodily ratio used would be (Northward + 15) / 64 for all Northward<>0.
Global Colour Table
From the sample file: FF FF FF FF 00 00 00 00 FF 00 00 00
GIFs can have either a global color table or local color tables for each sub-image. Each colour tabular array consists of a list of RGB (Scarlet-Green-Bluish) colour component intensities, three bytes for each color, with intensities ranging from 0 (least) to 255 (virtually). The color (0,0,0) is deepest black, the color (255,255,255) brightest white. The other extreme colors are red at (255,0,0), green at (0,255,0) and bluish at (0,0,255).
Every bit previously noted, the length of the global colour table is ii^(N+1) entries where N is the value of the color depth field in the logical screen descriptor. The table will take up three*two^(N+1) bytes in the stream.
Size In Logical Screen Desc | Number Of Colors | Byte Length |
---|---|---|
0 | 2 | 6 |
1 | iv | 12 |
2 | viii | 24 |
3 | 16 | 48 |
four | 32 | 96 |
5 | 64 | 192 |
half-dozen | 128 | 384 |
vii | 256 | 768 |
Our sample file has a global color table size of 1. This means it holds 2^(one+1)=2^2=4 colors. We tin can encounter that it takes up 12, (3*4), bytes as expected. Nosotros read the bytes 3 at a time to go each of the colors. The beginning colour is #FFFFFF (white). This value is given an index of 0. The 2nd color is #FF0000 (cerise). The colour with an index value of ii is #0000FF (blueish). The last color is #000000 (black). The index numbers volition be of import when we decode the actual image data.
Annotation that this block is labeled as "optional." Non every GIF has to specify a global color tabular array. Withal, if the global color table flag is set to one in the logical screen descriptor block, the color tabular array is then required to immediately follow that block.
Graphics Command Extension
From the sample file: 21 F9 04 00 00 00 00 00
Graphic control extension blocks are used to specify transparency settings and command animations. They are an optional GIF89 extension. The semantics of this extension will exist described in detail in a subsequently section (see Transparency and Animation); for completeness we'll depict the data fields here.
The first byte is the extension introducer. All extension blocks begin with 21. Adjacent is the graphic control characterization, F9, which is the value that flags this as a graphic control extension. 3rd upwards is the total block size in bytes. Adjacent is a packed field. Bits 1-3 are reserved for future use. $.25 4-six point disposal method. The penultimate bit is the user input flag and the last is the transparent colour flag. The filibuster time value follows in the next ii bytes stored in unsigned format. After that we have the transparent color index byte. Finally we have the block terminator which is always 00.
Epitome Descriptor
From the sample file: 2C 00 00 00 00 0A 00 0A 00 00
A single GIF file may incorporate multiple images. In the original GIF rendering model these wwere meant to be composited onto a larger virtual canvas. Nowadays nultiple images are normally used for animations.
Each prototype begins with an paradigm descriptor block. This cake is exactly 10 bytes long.
The kickoff byte is the image separator. Every prototype descriptor begins with the value 2C. The side by side 8 bytes stand for the location and size of the post-obit image.
An image in the stream may not necessarily take up the entire canvas size defined by the logical screen descriptor. Therefore, the prototype descriptor specifies the image left position and paradigm top position of where the paradigm should begin on the canvass. Both these fields are ordinarily ignored by modernistic viewers and browsers.
Next, this block specifies the image width and paradigm height. Each of these values is in the ii-byte, unsigned footling-endian format. Our sample image indicates that the epitome starts at (0,0) and is ten pixels wide past 10 pixels alpine. (This epitome does take up the whole canvas size.)
The concluding byte is some other packed field. In our sample file this byte is 0 and so all of the sub-values will be zero. The get-go (most pregnant) flake in the byte is the local color table flag. Setting this flag to 1 allows you lot to specify that the image data that follows uses a different color table than the global color table. (More information on the local color table follows.)
The second bit is the interlace flag. Interlacing changes the manner images are rendered onto the screen in a style that may reduce abrasive visual flicker. The effect of interlacing on a display is that the kickoff pass of appears immediately, displaying the graphic as a first as a mistiness and then sharpenining it up as later on passes fill in lines. That allows the man viewer to at least get an idea of what's coming up rather than waiting for the entire image to be painted, line by line. See an example. To support this, the scan lines of the image need to exist stored in a dissimilar order than the normal tiptop-downward, separated into sections that will be rendered in 4 separate passes.
Local Color Table
A local color tabular array is organized the same every bit a global color tabular array. The local color table would ever immediately follow an epitome descriptor but will just be in that location if the local color table flag is fix to 1. It is constructive just for the cake of image information that immediately follows it. If no local color table is specified, the global colour table is used for the following paradigm information.
The size of the local color tabular array tin can exist calculated by the value given in the image descriptor. Just like with the global colour table, if the image descriptor specifies a size of Due north, the color tabular array volition comprise 2^(Due north+1) colors and volition have up 3*ii^(N+1) bytes.
Image Data
From the sample file: 02 16 8C 2D 99 87 2A 1C DC 33 A0 02 75 EC 95 FA A8 DE sixty 8C 04 91 4C 01 00
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 emit to the sail. These codes are combined into the bytes that make upwardly the cake.
In that location's another section on decoding these output lawmaking into an image (see LZW Paradigm Information). Here we'll just see how to determine how long the block volition be.
The first byte of this block is the LZW minimum code size. This value is used to decode the compressed output codes. (Again, encounter the section on LZW compression to see how this works.) The rest of the bytes correspond data sub-blocks. Information sub-blocks are are groups of 1 - 256 bytes. The offset byte in the sub-block tells y'all how many bytes of actual data follow. This tin can exist a value from 0 (00) it 255 (FF). After yous've read those bytes, the next byte you read volition tell you now many more bytes of information follow that one. You lot continue to read until you attain a sub-block that says that zero bytes follow.
You can see our sample file has a LZW minimum code size of two. The side by side byte tells u.s. that 22 bytes of data follow it (16 hex = 22). After we've read those 22 bytes, we see the next value is 0. This ways that no bytes follow and we have read all the data in this block.
Plain Text Extension
Example (non in the sample file): 21 01 0C 00 00 00 00 64 00 64 00 14 14 01 00 0B 68 65 6C 6C 6F 20 77 6F 72 6C 64 00
The GIF89 specification allows you to specify text captions to exist overlayed on the following epitome. This feature never took off; browsers and prototype-processing applications such as Photoshop ignore it, and GIFLIB doesn't endeavour to interpret it.
The block begins with an extension introducer equally all extension cake types do. This value is always 21. The next byte is the manifestly text label. This value of 01 is used to distinguish plainly text extensions from all other extensions. The next byte is the cake size. This tells you lot how many bytes there are until the bodily text data begins, or in other words, how many bytes you tin can now skip. The byte value will probably be 0C which means you should jump forward 12 bytes. The text that follows is encoded in information sub-blocks (see Image Data to see how these sub-blocks are formed). The block ends when you attain a sub-cake of length 0.
Awarding Extension
Case (not in sample file): 21 FF 0B 4E 45 54 53 43 41 fifty 45 32 2E 30 03 01 05 00 00
The GIF89 specification allows for application-specific information to exist embedded in the GIF file itself. This adequacy is not much used. About the only known public one is the Netscape ii.0 extension (described beneath) which is used to loop an animated GIF file. We'll become into more item on looping in when we talk about animation.
The Netscape 2.0 looping block must appear immediately after the global color tabular array of the logical screen descriptor. Information technology is 19 bytes long.
byte one : 33 (hex 0x21) GIF Extension code byte 2 : 255 (hex 0xFF) Application Extension Label byte three : 11 (hex 0x0B) Length of Application Block (11 bytes of information to follow) bytes four to eleven : "NETSCAPE" bytes 12 to 14 : "ii.0" byte 15 : iii (hex 0x03) Length of Data Sub-Block (three bytes of data to follow) byte 16 : one (hex 0x01) bytes 17 to 18 : 0 to 65535, an unsigned integer in little-endian byte format. This specifies the number of times the loop should be executed. byte nineteen : 0 (hex 0x00) a Data Sub-Block Terminator.
As with all extensions, we start with 21 which is the extension introducer. Adjacent is the extension label which for application extensions is FF. The next value is the block size which tells yous how many bytes in that location are before the bodily awarding data begins. This byte value should be 0B which indicates eleven bytes. These 11 bytes hold two pieces of information. Start is the awarding identifier which takes upwardly the beginning eight bytes. These bytes should comprise ASCII character codes that identify to which application the extension belongs. In the case of the example above, the application identifier is "NETSCAPE" which is conveniently eight characters long. The next three bytes are the application authentication code. The spec says these bytes can be used to "authenticate the application identifier." With the Netscape 2.0 extension, this value is simply a version number, "ii.0", hence the extensions proper name. What follows is the application data broken into data sub-blocks. Equally with the other extensions, the cake terminates when you read a sub-block that has zero bytes of data.
Comment Extension
Example (not in sample file): 21 Fe 09 62 6C 75 65 62 65 72 72 79 00
One concluding GIF89 extension type is the comment extension. This allows you lot to embed ASCII text in a GIF file, and is sometimes used to include an image description, image credit, or other human-readable metadata such as the GPS location of the epitome capture.
It's probably no surprise past now that the beginning byte is the extension introducer which is 21. The next byte is always FE which is the comment label. Then nosotros jump right to data sub-blocks containing ASCII grapheme codes for your comment. As you can run into from the example we have i data sub-block that is 9 bytes long. If you lot interpret the character codes y'all meet that the comment is "blueberry." The final byte, 00, indicates a sub-block with goose egg bytes that follow which let'southward us know we have reached the end of the block.
Trailer
From sample file: 3B
The trailer block indicates when you've reached the end of the file. It is always a byte with a value of 3B.
Next: LZW Image Data
Now that you know what the basic parts of a GIF file are, let's next focus our attention on how the actual image information is stored and compressed.
Continue...
Source: http://giflib.sourceforge.net/whatsinagif/bits_and_bytes.html
Posted by: aguilaronoten.blogspot.com
0 Response to "What Is The Size Of An Animated Gif Image In Bytes"
Post a Comment