| 1 |
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> |
| 2 |
<html> |
| 3 |
<head> |
| 4 |
<title>What's In A GIF - Bits and Bytes</title> |
| 5 |
<script type="text/javascript"></script> |
| 6 |
<link rel="stylesheet" href="../proj.css" /> |
| 7 |
<style type="text/css"> |
| 8 |
.byte {font-family: Courier, fixed; |
| 9 |
padding: .2em} |
| 10 |
.gif_header {background-color: #f9E89D} |
| 11 |
.gif_screen {background-color: #C8DBD9} |
| 12 |
.gif_color {background-color: #E1E1E1} |
| 13 |
.gif_graphic {background-color: #F9EB9D} |
| 14 |
.gif_imgdesc {background-color: #C2D1DC} |
| 15 |
.gif_imgdata {background-color: #D0C4C4} |
| 16 |
.gif_trailer {background-color: #f9E89D} |
| 17 |
.gif_ext {background-color: #D0CFAE} |
| 18 |
#global_color_size {margin-left: auto; margin-right:auto; border:1px solid black;} |
| 19 |
#global_color_size td {text-align:center;} |
| 20 |
</style> |
| 21 |
</head> |
| 22 |
<body> |
| 23 |
<table width='100%' cellpadding='0' summary='Canned page header' bgcolor="#ddd"> |
| 24 |
<tr> |
| 25 |
<td><h2>What's In A GIF</h2></td> |
| 26 |
<td align="center"><img src="../giflib-logo.gif"></td> |
| 27 |
<td align="right">(Bits and bytes)</td> |
| 28 |
</tr> |
| 29 |
</table> |
| 30 |
|
| 31 |
<div id="body"> |
| 32 |
<div style="text-align:center; margin-top: 10px; padding-top: 10px; border-top: #cecece 1px solid"> |
| 33 |
<p><a href="index.html">Back to the What's In A GIF index page.</a></p> |
| 34 |
</div> |
| 35 |
|
| 36 |
<p>The authority on the content of GIFs is the <a |
| 37 |
href="../gif89.txt">GIF89a specification</a>. Originally developed at |
| 38 |
CompuServe in the late 1980s, it is now a W3C standard.</p> |
| 39 |
|
| 40 |
<p>A GIF file is made up of a sequence of data blocks. The first two |
| 41 |
blocks are fixed length and fixed format. Later ones are variable |
| 42 |
length but self-describing; they consisting of a byte identifying the block |
| 43 |
type, followed by a payload length byte, followed by payload.</p> |
| 44 |
|
| 45 |
<p>The following railroad diagram shows all of the different types of |
| 46 |
blocks and where they can be in the file. Every path following the |
| 47 |
arrows corresponds to a valid block sequence. The large middle |
| 48 |
section, in particular, can be repeated any number of times.</p> |
| 49 |
|
| 50 |
<p style="text-align:center"><img src="gif_file_stream.gif" alt="GIF file stream diagram" style="border: 1px solid black" / WIDTH="700" HEIGHT="220"></p> |
| 51 |
|
| 52 |
<p>We will learn more by walking through a sample GIF file. You can |
| 53 |
see the sample file and its corresponding bytes below. </p> |
| 54 |
|
| 55 |
<table> |
| 56 |
<tr> |
| 57 |
<td style="text-align:center; vertical-align: top; padding: 5px; width:20%"><h3>Actual Size</h3><img src="sample_1.gif" alt="sample gif, actual size" title="Actual Size" style="padding: 20px" / WIDTH="10" HEIGHT="10"><br/>(10x10)</td> |
| 58 |
<td style="text-align:center; vertical-align: top; padding: 5px;; width:20%"><h3>Enlarged</h3><img src="sample_1_enlarged.gif" alt="sample gif, enlarged" title="Enlarged" / WIDTH="100" HEIGHT="100"><br/>(100x100)</td> |
| 59 |
<td style="vertical-align: top; padding: 5px; width:60%"><h3>Bytes</h3> |
| 60 |
|
| 61 |
<span class="byte gif_header"> 47 </span> |
| 62 |
<span class="byte gif_header"> 49 </span> |
| 63 |
<span class="byte gif_header"> 46 </span> |
| 64 |
<span class="byte gif_header"> 38 </span> |
| 65 |
<span class="byte gif_header"> 39 </span> |
| 66 |
<span class="byte gif_header"> 61 </span> |
| 67 |
<span class="byte gif_screen"> 0A </span> |
| 68 |
<span class="byte gif_screen"> 00 </span> |
| 69 |
<span class="byte gif_screen"> 0A </span> |
| 70 |
<span class="byte gif_screen"> 00 </span> |
| 71 |
<span class="byte gif_screen"> 91 </span> |
| 72 |
<span class="byte gif_screen"> 00 </span> |
| 73 |
<span class="byte gif_screen"> 00 </span> |
| 74 |
<span class="byte gif_color"> FF </span> |
| 75 |
<span class="byte gif_color"> FF </span> |
| 76 |
<span class="byte gif_color"> FF </span> |
| 77 |
<span class="byte gif_color"> FF </span> |
| 78 |
<span class="byte gif_color"> 00 </span> |
| 79 |
<span class="byte gif_color"> 00 </span> |
| 80 |
<span class="byte gif_color"> 00 </span> |
| 81 |
<span class="byte gif_color"> 00 </span> |
| 82 |
<span class="byte gif_color"> FF </span> |
| 83 |
<span class="byte gif_color"> 00 </span> |
| 84 |
<span class="byte gif_color"> 00 </span> |
| 85 |
<span class="byte gif_color"> 00 </span> |
| 86 |
<span class="byte gif_graphic"> 21 </span> |
| 87 |
<span class="byte gif_graphic"> F9 </span> |
| 88 |
<span class="byte gif_graphic"> 04 </span> |
| 89 |
<span class="byte gif_graphic"> 00 </span> |
| 90 |
<span class="byte gif_graphic"> 00 </span> |
| 91 |
<span class="byte gif_graphic"> 00 </span> |
| 92 |
<span class="byte gif_graphic"> 00 </span> |
| 93 |
<span class="byte gif_graphic"> 00 </span> |
| 94 |
<span class="byte gif_imgdesc"> 2C </span> |
| 95 |
<span class="byte gif_imgdesc"> 00 </span> |
| 96 |
<span class="byte gif_imgdesc"> 00 </span> |
| 97 |
<span class="byte gif_imgdesc"> 00 </span> |
| 98 |
<span class="byte gif_imgdesc"> 00 </span> |
| 99 |
<span class="byte gif_imgdesc"> 0A </span> |
| 100 |
<span class="byte gif_imgdesc"> 00 </span> |
| 101 |
<span class="byte gif_imgdesc"> 0A </span> |
| 102 |
<span class="byte gif_imgdesc"> 00 </span> |
| 103 |
<span class="byte gif_imgdesc"> 00 </span> |
| 104 |
<span class="byte gif_imgdata"> 02 </span> |
| 105 |
<span class="byte gif_imgdata"> 16 </span> |
| 106 |
<span class="byte gif_imgdata"> 8C </span> |
| 107 |
<span class="byte gif_imgdata"> 2D </span> |
| 108 |
<span class="byte gif_imgdata"> 99 </span> |
| 109 |
<span class="byte gif_imgdata"> 87 </span> |
| 110 |
<span class="byte gif_imgdata"> 2A </span> |
| 111 |
<span class="byte gif_imgdata"> 1C </span> |
| 112 |
<span class="byte gif_imgdata"> DC </span> |
| 113 |
<span class="byte gif_imgdata"> 33 </span> |
| 114 |
<span class="byte gif_imgdata"> A0 </span> |
| 115 |
<span class="byte gif_imgdata"> 02 </span> |
| 116 |
<span class="byte gif_imgdata"> 75 </span> |
| 117 |
<span class="byte gif_imgdata"> EC </span> |
| 118 |
<span class="byte gif_imgdata"> 95 </span> |
| 119 |
<span class="byte gif_imgdata"> FA </span> |
| 120 |
<span class="byte gif_imgdata"> A8 </span> |
| 121 |
<span class="byte gif_imgdata"> DE </span> |
| 122 |
<span class="byte gif_imgdata"> 60 </span> |
| 123 |
<span class="byte gif_imgdata"> 8C </span> |
| 124 |
<span class="byte gif_imgdata"> 04 </span> |
| 125 |
<span class="byte gif_imgdata"> 91 </span> |
| 126 |
<span class="byte gif_imgdata"> 4C </span> |
| 127 |
<span class="byte gif_imgdata"> 01 </span> |
| 128 |
<span class="byte gif_imgdata"> 00 </span> |
| 129 |
<span class="byte gif_trailer"> 3B </span> |
| 130 |
</td> |
| 131 |
</tr> |
| 132 |
</table> |
| 133 |
|
| 134 |
<p>Note that not all possible block types are represented in this |
| 135 |
sample file. Later we'll provide samples of missing block types where |
| 136 |
appropriate. The different types of blocks include: <a |
| 137 |
href="#header_block">header</a>, <a |
| 138 |
href="#logical_screen_descriptor_block">logical screen descriptor</a>, |
| 139 |
<a href="#global_color_table_block">global color table</a>, <a |
| 140 |
href="#graphics_control_extension_block">graphics control |
| 141 |
extension</a>, <a href="#image_descriptor_block">image descriptor</a>, |
| 142 |
<a href="#local_color_table_block">local color table</a>, <a |
| 143 |
href="#image_data_block">image data</a>, <a |
| 144 |
href="#plain_text_extension_block">plain text extension</a>, <a |
| 145 |
href="#application_extension_block">application extension</a>, <a |
| 146 |
href="#comment_extension_block">comment extension</a>, and <a |
| 147 |
href="#trailer_block">trailer</a>. Let's get started with the first |
| 148 |
block!</p> |
| 149 |
|
| 150 |
<h2><a name="header_block">Header Block</a></h2> |
| 151 |
|
| 152 |
<p>From the sample file: |
| 153 |
<span class="byte gif_header"> 47 </span> |
| 154 |
<span class="byte gif_header"> 49 </span> |
| 155 |
<span class="byte gif_header"> 46 </span> |
| 156 |
<span class="byte gif_header"> 38 </span> |
| 157 |
<span class="byte gif_header"> 39 </span> |
| 158 |
<span class="byte gif_header"> 61 </span> |
| 159 |
</p> |
| 160 |
|
| 161 |
<p>All GIF files must start with a header block. The header takes up |
| 162 |
the first six bytes of the file. These bytes should all correspond to |
| 163 |
<a href="http://www.ascii.cl/">ASCII character codes</a>. The first |
| 164 |
three bytes are called the <strong>signature</strong>. These should |
| 165 |
always be "GIF" (ie 47="G", 49="I", |
| 166 |
46="F"). The next three specify the <strong>version</strong> |
| 167 |
of the specification that was used to encode the image. |
| 168 |
|
| 169 |
<p>Normally the version string will be either "89a" (ie |
| 170 |
38="8", 39="9",61="a") or "87a" (ie |
| 171 |
38="8", 37="7",61="a"). All modern |
| 172 |
GIF-processing software recognizes both versions, For maximum |
| 173 |
compatibility, GIFLIB will normally write an 87a signature unless the |
| 174 |
file contains GIF89 features.</p> |
| 175 |
|
| 176 |
<p style="text-align:center"><img src="header_block.gif" alt="GIF header block layout" style="border: 1px solid black" /></p> |
| 177 |
|
| 178 |
<h2><a name="logical_screen_descriptor_block">Logical Screen Descriptor</a></h2> |
| 179 |
<p>From Sample File: <span class="byte gif_screen"> 0A </span> |
| 180 |
<span class="byte gif_screen"> 00 </span><span class="byte gif_screen"> 0A </span><span class="byte gif_screen"> 00 </span><span class="byte gif_screen"> 91 </span><span class="byte gif_screen"> 00 </span><span class="byte gif_screen"> 00 </span></p> |
| 181 |
|
| 182 |
<p>The logical screen descriptor always immediately follows the |
| 183 |
header. This block tells the decoder how much room this image will |
| 184 |
take up. It is exactly seven bytes long. It starts with the |
| 185 |
<strong>canvas width</strong> and <strong>canvas height</strong>. |
| 186 |
These value can be found in the first two pairs of two bytes each. |
| 187 |
Both are 16-bit, nonnegative integers (0-65,535).</p> |
| 188 |
|
| 189 |
<p>As with all the other multi-byte values in the GIF format, the |
| 190 |
least significant byte is stored first (little-endian format). This |
| 191 |
means where we would read <span class="byte"> 0A </span><span |
| 192 |
class="byte"> 00 </span> from the byte stream, we would normally write |
| 193 |
it as <span class="byte">000A</span> which is the same as 10. Thus the |
| 194 |
width of our sample image is 10 pixels. As a further example 255 would |
| 195 |
be stored as <span class="byte"> FF </span><span class="byte"> 00 |
| 196 |
</span> but 256 would be <span class="byte"> 00 </span><span |
| 197 |
class="byte"> 01 </span>.</p> |
| 198 |
|
| 199 |
<p>The canvas width and height are usually ignored by modern viewers. |
| 200 |
The GIF format seems to have been designed with the idea that viewers |
| 201 |
would render multiple images in a GIF on a common canvas, giving an |
| 202 |
effect like a picture wall. But nowadays multiple-image GIFs are |
| 203 |
generally used either as animations in which each sub-image is a frame |
| 204 |
or as image libraries, with the GIF client handling compositing into |
| 205 |
some canvas about which the GIF format holds no information.Thus, the |
| 206 |
canvas width and height are mainly fossils. GIFLIB does extract them |
| 207 |
and allow you to set them, however.</p> |
| 208 |
|
| 209 |
<p>The next byte contains four fields of packed data, the "logical |
| 210 |
screen descriptor". To understand these, we need to expand the byte |
| 211 |
<span class="byte"> 91 </span> to binary as <span |
| 212 |
class="byte">10010001</span> and look at the fields inside it.</p> |
| 213 |
|
| 214 |
<p style="text-align:center"><img src="logical_screen_desc_block.gif" alt="GIF logical screen descriptor block layout" style="border: 1px solid black" /></p> |
| 215 |
|
| 216 |
<p>The first (most-significant) bit is the <strong>global color table |
| 217 |
flag</strong>. If it's 0, then there is no global color table. If it's |
| 218 |
1, then a global color table will follow. In our sample image, we can |
| 219 |
see that we will have a global color table (as will usually be the |
| 220 |
case).<p> |
| 221 |
|
| 222 |
<p>The next three bits are the <strong>color resolution</strong>. They |
| 223 |
are only meaningful if there is a global color table, and allow you to |
| 224 |
compute its size. If the value of this field is N, the number of |
| 225 |
entries in the global color table will be 2 ^ (N+1) - that is, two |
| 226 |
raised to the power (N+1). Thus, the <span class="byte">001</span> in |
| 227 |
the sample image represents 2 bits/pixel; <span |
| 228 |
class="byte">111</span> would represent 8 bits/pixel.</p> |
| 229 |
|
| 230 |
<p>The GIF format shows its age here. A more modern design would |
| 231 |
simply have allocated a byte or two for color table length. But GIF |
| 232 |
was designed when memory was much more expensive than it is today, |
| 233 |
and the designers felt strong pressure to economize on every bit. |
| 234 |
The consequence is that color table lengths have to be an exact power |
| 235 |
of two. Perversely, this can force a waste of memory space in images |
| 236 |
with odd color counts.</p> |
| 237 |
|
| 238 |
<p>The next single bit is the <strong>sort flag</strong>. If the |
| 239 |
values is 1, then the colors in the global color table are sorted in |
| 240 |
order of "decreasing importance," which typically means |
| 241 |
"decreasing frequency" in the image. This can help the image |
| 242 |
decoder, but is not required. In the sample file this value has been |
| 243 |
left at 0.</p> |
| 244 |
|
| 245 |
<p>The sort flag reflected the high cost of dual-port memory at the |
| 246 |
time the GIF specification was written in the late 1980s. That kind |
| 247 |
of limit disappeared in the mid-1990s, and modern GIF software ignores |
| 248 |
this flag. Until version 5.0, GIFLIB ignored it on input and zeroed |
| 249 |
it on output; 5.0 and later versions read and preserve it.</p> |
| 250 |
|
| 251 |
<p>The next byte gives us the <strong>background color |
| 252 |
index</strong>. This byte is only meaningful if the global color table |
| 253 |
flag is 1, and if there is no global color table, this byte should be |
| 254 |
0.. To understand it you have to remember the original "picture wall" |
| 255 |
rendering model for GIFs in which sub-images are composited onto a |
| 256 |
larger canvas. It represents which index in the global color table |
| 257 |
should be used for pixels on the virtual canvas that aren't overlaid |
| 258 |
by an image. GIFLIB supports reading and setting this byte, but modern |
| 259 |
viewers and browsers generally have no use for it.</p> |
| 260 |
|
| 261 |
<p>The last byte of the logical screen descriptor is the <strong>pixel |
| 262 |
aspect ratio</strong>. Modern viewers don't use this. Until 5.0, |
| 263 |
GIFLIB ignored this flag on input and zeroed it on output; now it is |
| 264 |
read and preserved if present. The GIF standard doesn't give a |
| 265 |
rationale for it, but it seems likely that the designers intended it |
| 266 |
for representing image captures from the analog television of the day, |
| 267 |
which had rectangular pixel-equivalents. The GIF specification says |
| 268 |
that if there was a value specified in this byte, N, the actual ratio |
| 269 |
used would be (N + 15) / 64 for all N<>0.</p> |
| 270 |
|
| 271 |
<h2><a name="global_color_table_block">Global Color Table</a></h2> |
| 272 |
|
| 273 |
<p>From the sample file: |
| 274 |
<span class="byte gif_color"> FF </span> |
| 275 |
<span class="byte gif_color"> FF </span> |
| 276 |
<span class="byte gif_color"> FF </span> |
| 277 |
<span class="byte gif_color"> FF </span> |
| 278 |
<span class="byte gif_color"> 00 </span> |
| 279 |
<span class="byte gif_color"> 00 </span> |
| 280 |
<span class="byte gif_color"> 00 </span> |
| 281 |
<span class="byte gif_color"> 00 </span> |
| 282 |
<span class="byte gif_color"> FF </span> |
| 283 |
<span class="byte gif_color"> 00 </span> |
| 284 |
<span class="byte gif_color"> 00 </span> |
| 285 |
<span class="byte gif_color"> 00 </span> |
| 286 |
</p> |
| 287 |
|
| 288 |
<p>GIFs can have either a <strong>global color table</strong> or local |
| 289 |
color tables for each sub-image. Each color table consists of a list |
| 290 |
of RGB (Red-Green-Blue) color component intensities, three bytes for |
| 291 |
each color, with intensities ranging from 0 (least) to 255 (most). The |
| 292 |
color (0,0,0) is deepest black, the color (255,255,255) brightest |
| 293 |
white. The other extreme colors are red at (255,0,0), green at |
| 294 |
(0,255,0) and blue at (0,0,255).</p> |
| 295 |
|
| 296 |
<p>As previously noted, the length of the global color table is |
| 297 |
2^(N+1) entries where N is the value of the color depth field in the |
| 298 |
logical screen descriptor. The table will take up 3*2^(N+1) bytes in |
| 299 |
the stream.</p> |
| 300 |
|
| 301 |
<div style="text-align:center"> |
| 302 |
<table id="global_color_size"> |
| 303 |
<tr><th>Size In Logical<br/>Screen Desc</th><th>Number Of<br/>Colors</th><th>Byte<br/>Length</th></tr> |
| 304 |
<tr><td>0</td><td>2</td><td>6</td></tr> |
| 305 |
<tr><td>1</td><td>4</td><td>12</td></tr> |
| 306 |
<tr><td>2</td><td>8</td><td>24</td></tr> |
| 307 |
<tr><td>3</td><td>16</td><td>48</td></tr> |
| 308 |
<tr><td>4</td><td>32</td><td>96</td></tr> |
| 309 |
<tr><td>5</td><td>64</td><td>192</td></tr> |
| 310 |
<tr><td>6</td><td>128</td><td>384</td></tr> |
| 311 |
<tr><td>7</td><td>256</td><td>768</td></tr> |
| 312 |
</table> |
| 313 |
</div> |
| 314 |
|
| 315 |
<p>Our sample file has a global color table size of 1. This means it |
| 316 |
holds 2^(1+1)=2^2=4 colors. We can see that it takes up 12, (3*4), |
| 317 |
bytes as expected. We read the bytes three at a time to get each of |
| 318 |
the colors. The first color is #FFFFFF (white). This value is given an |
| 319 |
index of 0. The second color is #FF0000 (red). The color with an |
| 320 |
index value of 2 is #0000FF (blue). The last color is #000000 |
| 321 |
(black). The index numbers will be important when we decode the actual |
| 322 |
image data.</p> |
| 323 |
|
| 324 |
<p>Note that this block is labeled as "optional." Not every |
| 325 |
GIF has to specify a global color table. However, if the global color |
| 326 |
table flag is set to 1 in the logical screen descriptor block, the |
| 327 |
color table is then required to immediately follow that block. |
| 328 |
</p> |
| 329 |
|
| 330 |
<p style="text-align:center"><img src="global_color_table.gif" alt="GIF global color table block layout" style="border: 1px solid black" /></p> |
| 331 |
|
| 332 |
<h2><a name="graphics_control_extension_block">Graphics Control Extension</a></h2> |
| 333 |
|
| 334 |
<p>From the sample file: |
| 335 |
<span class="byte gif_graphic"> 21</span> |
| 336 |
<span class="byte gif_graphic"> F9 </span> |
| 337 |
<span class="byte gif_graphic"> 04 </span> |
| 338 |
<span class="byte gif_graphic"> 00</span> |
| 339 |
<span class="byte gif_graphic"> 00 </span> |
| 340 |
<span class="byte gif_graphic"> 00 </span> |
| 341 |
<span class="byte gif_graphic"> 00</span> |
| 342 |
<span class="byte gif_graphic"> 00 </span> |
| 343 |
</p> |
| 344 |
|
| 345 |
<p>Graphic control extension blocks are used to specify transparency |
| 346 |
settings and control animations. They are an optional GIF89 extension. |
| 347 |
The semantics of this extension will be described in detail in a later section |
| 348 |
(see <a href="animation_and_transparency.html">Transparency and Animation</a>); |
| 349 |
for completeness we'll describe the data fields here.</p> |
| 350 |
|
| 351 |
<p>The first byte is the <strong>extension introducer</strong>. All |
| 352 |
<em>extension</em> blocks begin with <span |
| 353 |
class="byte">21</span>. Next is the <strong>graphic control |
| 354 |
label</strong>, <span class="byte">F9</span>, which is the value that |
| 355 |
flags this as a graphic control extension. Third up is the total |
| 356 |
<strong>block size</strong> in bytes. Next is a packed field. Bits 1-3 |
| 357 |
are reserved for future use. Bits 4-6 indicate <strong>disposal |
| 358 |
method</strong>. The penultimate bit is the <strong>user input |
| 359 |
flag</strong> and the last is the <strong>transparent color |
| 360 |
flag</strong>. The <strong>delay time</strong> value follows in the |
| 361 |
next two bytes stored in unsigned format. After that we have the |
| 362 |
<strong>transparent color index</strong> byte. Finally we have the |
| 363 |
<strong>block terminator</strong> which is always <span |
| 364 |
class="byte">00</span>.</p> |
| 365 |
|
| 366 |
<p style="text-align:center"><img src="graphic_control_ext.gif" |
| 367 |
alt="GIF graphic control extension block layout" style="border: 1px |
| 368 |
solid black" /></p> |
| 369 |
|
| 370 |
<h2><a name="image_descriptor_block">Image Descriptor</a></h2> |
| 371 |
|
| 372 |
<p>From the sample file: |
| 373 |
|
| 374 |
<span class="byte gif_imgdesc"> 2C </span> |
| 375 |
<span class="byte gif_imgdesc"> 00 </span> |
| 376 |
<span class="byte gif_imgdesc"> 00 </span> |
| 377 |
<span class="byte gif_imgdesc"> 00 </span> |
| 378 |
<span class="byte gif_imgdesc"> 00 </span> |
| 379 |
<span class="byte gif_imgdesc"> 0A </span> |
| 380 |
<span class="byte gif_imgdesc"> 00 </span> |
| 381 |
<span class="byte gif_imgdesc"> 0A </span> |
| 382 |
<span class="byte gif_imgdesc"> 00 </span> |
| 383 |
<span class="byte gif_imgdesc"> 00 </span> |
| 384 |
</p> |
| 385 |
|
| 386 |
<p>A single GIF file may contain multiple images. In the original GIF |
| 387 |
rendering model these wwere meant to be composited onto a larger |
| 388 |
virtual canvas. Nowadays nultiple images are normally used for animations.</p> |
| 389 |
|
| 390 |
<p>Each image begins with an image descriptor block. This block is |
| 391 |
exactly 10 bytes long.</p> |
| 392 |
|
| 393 |
<p>The first byte is the <strong>image separator</strong>. Every image |
| 394 |
descriptor begins with the value <span class="byte">2C</span>. The |
| 395 |
next 8 bytes represent the location and size of the following |
| 396 |
image.</p> |
| 397 |
|
| 398 |
<p>An image in the stream may not necessarily take up the entire |
| 399 |
canvas size defined by the logical screen descriptor. Therefore, the |
| 400 |
image descriptor specifies the <strong>image left position</strong> |
| 401 |
and <strong>image top position</strong> of where the image should |
| 402 |
begin on the canvas. Both these fields are usually ignored by modern |
| 403 |
viewers and browsers.</p> |
| 404 |
|
| 405 |
<p>Next, this block specifies the <strong>image width</strong> and |
| 406 |
<strong>image height</strong>. Each of these values is in the |
| 407 |
two-byte, unsigned little-endian format. Our sample image indicates |
| 408 |
that the image starts at (0,0) and is 10 pixels wide by 10 pixels |
| 409 |
tall. (This image does take up the whole canvas size.)</p> |
| 410 |
|
| 411 |
<p>The last byte is another packed field. In our sample file this byte is 0 so |
| 412 |
all of the sub-values will be zero. The first (most significant) bit in |
| 413 |
the byte is the <strong>local color table flag</strong>. Setting this flag |
| 414 |
to 1 allows you to specify that the image data that follows uses a different |
| 415 |
color table than the global color table. (More information on the local |
| 416 |
color table follows.)</p> |
| 417 |
|
| 418 |
<p>The second bit is the <strong>interlace flag</strong>. Interlacing |
| 419 |
changes the way images are rendered onto the screen in a way that may |
| 420 |
reduce annoying visual flicker. The effect of interlacing on a display |
| 421 |
is that the first pass of appears immediately, displaying the graphic |
| 422 |
as a first as a blur and then sharpening it up as later passes fill |
| 423 |
in lines. That allows the human viewer to at least get an idea of |
| 424 |
what's coming up rather than waiting for the entire image to be |
| 425 |
painted, line by line. <a |
| 426 |
href="http://webtutor.tamu.edu/lesson6/interlace.html">See an |
| 427 |
example</a>. To support this, the scan lines of the image need to be |
| 428 |
stored in a different order than the normal top-down, separated into |
| 429 |
sections that will be rendered in four separate passes.</p> |
| 430 |
|
| 431 |
<p style="text-align:center"><img src="image_descriptor_block.gif" |
| 432 |
alt="GIF image descriptor block layout" style="border: 1px solid |
| 433 |
black" /></p> |
| 434 |
|
| 435 |
<h2><a name="local_color_table_block">Local Color Table</a></h2> |
| 436 |
<p>A local color table is organized the same as a global color table. The local |
| 437 |
color table would always immediately follow an image descriptor but will only |
| 438 |
be there if the local color table flag is set to 1. It is effective only for the |
| 439 |
block of image data that immediately follows it. If no local color table |
| 440 |
is specified, the global color table is used for the following image data.</p> |
| 441 |
|
| 442 |
<p>The size of the local color table can be calculated by the value |
| 443 |
given in the image descriptor. Just like with the global color table, |
| 444 |
if the image descriptor specifies a size of N, the color table will |
| 445 |
contain 2^(N+1) colors and will take up 3*2^(N+1) bytes.</p> |
| 446 |
|
| 447 |
<h2><a name="image_data_block">Image Data</a></h2> |
| 448 |
|
| 449 |
<p>From the sample file: |
| 450 |
<span class="byte gif_imgdata"> 02 </span> |
| 451 |
<span class="byte gif_imgdata"> 16 </span> |
| 452 |
<span class="byte gif_imgdata"> 8C </span> |
| 453 |
<span class="byte gif_imgdata"> 2D </span> |
| 454 |
<span class="byte gif_imgdata"> 99 </span> |
| 455 |
<span class="byte gif_imgdata"> 87 </span> |
| 456 |
<span class="byte gif_imgdata"> 2A </span> |
| 457 |
<span class="byte gif_imgdata"> 1C </span> |
| 458 |
<span class="byte gif_imgdata"> DC </span> |
| 459 |
<span class="byte gif_imgdata"> 33 </span> |
| 460 |
<span class="byte gif_imgdata"> A0 </span> |
| 461 |
<span class="byte gif_imgdata"> 02 </span> |
| 462 |
<span class="byte gif_imgdata"> 75 </span> |
| 463 |
<span class="byte gif_imgdata"> EC </span> |
| 464 |
<span class="byte gif_imgdata"> 95 </span> |
| 465 |
<span class="byte gif_imgdata"> FA </span> |
| 466 |
<span class="byte gif_imgdata"> A8 </span> |
| 467 |
<span class="byte gif_imgdata"> DE </span> |
| 468 |
<span class="byte gif_imgdata"> 60 </span> |
| 469 |
<span class="byte gif_imgdata"> 8C </span> |
| 470 |
<span class="byte gif_imgdata"> 04 </span> |
| 471 |
<span class="byte gif_imgdata"> 91 </span> |
| 472 |
<span class="byte gif_imgdata"> 4C </span> |
| 473 |
<span class="byte gif_imgdata"> 01 </span> |
| 474 |
<span class="byte gif_imgdata"> 00 </span></p> |
| 475 |
|
| 476 |
<p>Finally we get to the actual image data. The image data is composed of |
| 477 |
a series of output codes which tell the decoder which colors to emit |
| 478 |
to the canvas. These codes are combined into the bytes that make up |
| 479 |
the block.</p> |
| 480 |
|
| 481 |
<p>There's another section on decoding these output code into an image |
| 482 |
(see <a href="lzw_image_data.html">LZW Image Data</a>). Here we'll |
| 483 |
just see how to determine how long the block will be. </p> |
| 484 |
|
| 485 |
<p>The first byte of this block is the <strong>LZW minimum code |
| 486 |
size</strong>. This value is used to decode the compressed output |
| 487 |
codes. (Again, see the section on <a href="lzw_image_data.html">LZW |
| 488 |
compression</a> to see how this works.) The rest of the bytes |
| 489 |
represent <em>data sub-blocks</em>. Data sub-blocks are are groups of |
| 490 |
1 - 256 bytes. The first byte in the sub-block tells you how many |
| 491 |
bytes of actual data follow. This can be a value from 0 (<span |
| 492 |
class="byte">00</span>) to 255 (<span class="byte">FF</span>). After |
| 493 |
you've read those bytes, the next byte you read will tell you now many |
| 494 |
more bytes of data follow that one. You continue to read until you |
| 495 |
reach a sub-block that says that zero bytes follow. |
| 496 |
</p> |
| 497 |
|
| 498 |
<p>You can see our sample file has a LZW minimum code size of 2. The |
| 499 |
next byte tells us that 22 bytes of data follow it (<span |
| 500 |
class="byte">16</span> hex = 22). After we've read those 22 bytes, we |
| 501 |
see the next value is 0. This means that no bytes follow and we have |
| 502 |
read all the data in this block.</p> |
| 503 |
|
| 504 |
<p style="text-align:center"><img src="image_data_block.gif" alt="GIF image data block layout" style="border: 1px solid black" /></p> |
| 505 |
|
| 506 |
<h2><a name="plain_text_extension_block">Plain Text Extension</a></h2> |
| 507 |
<p>Example (not in the sample file): |
| 508 |
|
| 509 |
<span class="byte gif_ext"> 21 </span> |
| 510 |
<span class="byte gif_ext"> 01 </span> |
| 511 |
<span class="byte gif_ext"> 0C </span> |
| 512 |
<span class="byte gif_ext"> 00 </span> |
| 513 |
<span class="byte gif_ext"> 00 </span> |
| 514 |
<span class="byte gif_ext"> 00 </span> |
| 515 |
<span class="byte gif_ext"> 00 </span> |
| 516 |
<span class="byte gif_ext"> 64 </span> |
| 517 |
<span class="byte gif_ext"> 00 </span> |
| 518 |
<span class="byte gif_ext"> 64 </span> |
| 519 |
<span class="byte gif_ext"> 00 </span> |
| 520 |
<span class="byte gif_ext"> 14 </span> |
| 521 |
<span class="byte gif_ext"> 14 </span> |
| 522 |
<span class="byte gif_ext"> 01 </span> |
| 523 |
<span class="byte gif_ext"> 00 </span> |
| 524 |
<span class="byte gif_ext"> 0B </span> |
| 525 |
<span class="byte gif_ext"> 68 </span> |
| 526 |
<span class="byte gif_ext"> 65 </span> |
| 527 |
<span class="byte gif_ext"> 6C </span> |
| 528 |
<span class="byte gif_ext"> 6C </span> |
| 529 |
<span class="byte gif_ext"> 6F </span> |
| 530 |
<span class="byte gif_ext"> 20 </span> |
| 531 |
<span class="byte gif_ext"> 77 </span> |
| 532 |
<span class="byte gif_ext"> 6F </span> |
| 533 |
<span class="byte gif_ext"> 72 </span> |
| 534 |
<span class="byte gif_ext"> 6C </span> |
| 535 |
<span class="byte gif_ext"> 64 </span> |
| 536 |
<span class="byte gif_ext"> 00 </span></p> |
| 537 |
|
| 538 |
<p>The GIF89 specification allows you to specify text captions to be |
| 539 |
overlaid on the following image. This feature never took off; |
| 540 |
browsers and image-processing applications such as Photoshop ignore |
| 541 |
it, and GIFLIB doesn't try to interpret it.</p> |
| 542 |
|
| 543 |
<p>The block begins with an <strong>extension introducer</strong> as all |
| 544 |
extension block types do. This value is always <span class="byte">21</span>. |
| 545 |
The next byte is the <strong>plain text label</strong>. This value of |
| 546 |
<span class="byte">01</span> is used to distinguish plain text extensions |
| 547 |
from all other extensions. The next byte is the <strong>block size</strong>. |
| 548 |
This tells you how many bytes there are until the actual text data begins, or |
| 549 |
in other words, how many bytes you can now skip. The byte value will probably be |
| 550 |
<span class="byte">0C</span> which means you should jump forward 12 bytes. The |
| 551 |
text that follows is encoded in data sub-blocks |
| 552 |
(see <a href="#image_data_block">Image Data</a> to see how |
| 553 |
these sub-blocks are formed). The block ends when you reach a sub-block of |
| 554 |
length 0.</p> |
| 555 |
|
| 556 |
<h2><a name="application_extension_block">Application Extension</a></h2> |
| 557 |
<p>Example (not in sample file): |
| 558 |
<span class="byte gif_ext"> 21 </span> |
| 559 |
<span class="byte gif_ext"> FF </span> |
| 560 |
<span class="byte gif_ext"> 0B </span> |
| 561 |
<span class="byte gif_ext"> 4E </span> |
| 562 |
<span class="byte gif_ext"> 45 </span> |
| 563 |
<span class="byte gif_ext"> 54 </span> |
| 564 |
<span class="byte gif_ext"> 53 </span> |
| 565 |
<span class="byte gif_ext"> 43 </span> |
| 566 |
<span class="byte gif_ext"> 41 </span> |
| 567 |
<span class="byte gif_ext"> 50 </span> |
| 568 |
<span class="byte gif_ext"> 45 </span> |
| 569 |
<span class="byte gif_ext"> 32 </span> |
| 570 |
<span class="byte gif_ext"> 2E </span> |
| 571 |
<span class="byte gif_ext"> 30 </span> |
| 572 |
<span class="byte gif_ext"> 03 </span> |
| 573 |
<span class="byte gif_ext"> 01 </span> |
| 574 |
<span class="byte gif_ext"> 05 </span> |
| 575 |
<span class="byte gif_ext"> 00 </span> |
| 576 |
<span class="byte gif_ext"> 00 </span> |
| 577 |
</p> |
| 578 |
|
| 579 |
<p>The GIF89 specification allows for application-specific information |
| 580 |
to be embedded in the GIF file itself. This capability is not much |
| 581 |
used. About the only known public one is the Netscape 2.0</a> |
| 582 |
extension (described below) which is used to loop an animated GIF |
| 583 |
file. We'll go into more detail on looping in when we talk about <a |
| 584 |
href="animation_and_transparency.html">animation</a>.</p> |
| 585 |
|
| 586 |
<p>The Netscape 2.0 looping block must appear immediately after the |
| 587 |
global color table of the logical screen descriptor. It is |
| 588 |
19 bytes long. |
| 589 |
|
| 590 |
<pre> |
| 591 |
byte 1 : 33 (hex 0x21) GIF Extension code |
| 592 |
byte 2 : 255 (hex 0xFF) Application Extension Label |
| 593 |
byte 3 : 11 (hex 0x0B) Length of Application Block |
| 594 |
(eleven bytes of data to follow) |
| 595 |
bytes 4 to 11 : "NETSCAPE" |
| 596 |
bytes 12 to 14 : "2.0" |
| 597 |
byte 15 : 3 (hex 0x03) Length of Data Sub-Block |
| 598 |
(three bytes of data to follow) |
| 599 |
byte 16 : 1 (hex 0x01) |
| 600 |
bytes 17 to 18 : 0 to 65535, an unsigned integer in |
| 601 |
little-endian byte format. This specifies the |
| 602 |
number of times the loop should |
| 603 |
be executed. |
| 604 |
byte 19 : 0 (hex 0x00) a Data Sub-Block Terminator. |
| 605 |
</pre> |
| 606 |
</p> |
| 607 |
|
| 608 |
<p>As with all extensions, we start with <span class="byte">21</span> |
| 609 |
which is the <strong>extension introducer</strong>. Next is the |
| 610 |
<strong>extension label</strong> which for application extensions is |
| 611 |
<span class="byte">FF</span>. The next value is the <strong>block |
| 612 |
size</strong> which tells you how many bytes there are before the |
| 613 |
actual application data begins. This byte value should be <span |
| 614 |
class="byte">0B</span> which indicates 11 bytes. These 11 bytes hold |
| 615 |
two pieces of information. First is the <strong>application |
| 616 |
identifier</strong> which takes up the first 8 bytes. These bytes |
| 617 |
should contain ASCII character codes that identify to which |
| 618 |
application the extension belongs. In the case of the example above, |
| 619 |
the application identifier is "NETSCAPE" which is |
| 620 |
conveniently 8 characters long. The next three bytes are the |
| 621 |
<strong>application authentication code</strong>. The spec says these |
| 622 |
bytes can be used to "authenticate the application |
| 623 |
identifier." With the Netscape 2.0 extension, this value is simply |
| 624 |
a version number, "2.0", hence the extensions name. What |
| 625 |
follows is the application data broken into data sub-blocks. As with |
| 626 |
the other extensions, the block terminates when you read a sub-block |
| 627 |
that has zero bytes of data.</p> |
| 628 |
|
| 629 |
<h2><a name="comment_extension_block">Comment Extension</a></h2> |
| 630 |
|
| 631 |
<p>Example (not in sample file): |
| 632 |
<span class="byte gif_ext"> 21 </span> |
| 633 |
<span class="byte gif_ext"> FE </span> |
| 634 |
<span class="byte gif_ext"> 09 </span> |
| 635 |
<span class="byte gif_ext"> 62 </span> |
| 636 |
<span class="byte gif_ext"> 6C </span> |
| 637 |
<span class="byte gif_ext"> 75 </span> |
| 638 |
<span class="byte gif_ext"> 65 </span> |
| 639 |
<span class="byte gif_ext"> 62 </span> |
| 640 |
<span class="byte gif_ext"> 65 </span> |
| 641 |
<span class="byte gif_ext"> 72 </span> |
| 642 |
<span class="byte gif_ext"> 72 </span> |
| 643 |
<span class="byte gif_ext"> 79 </span> |
| 644 |
<span class="byte gif_ext"> 00 </span></p> |
| 645 |
|
| 646 |
<p>One last GIF89 extension type is the comment extension. This allows you |
| 647 |
to embed ASCII text in a GIF file, and is sometimes used to include an |
| 648 |
image description, image credit, or other human-readable metadata such as |
| 649 |
the GPS location of the image capture.</p> |
| 650 |
|
| 651 |
<p>It's probably no surprise by now that the first byte is the |
| 652 |
<strong>extension introducer</strong> which is <span |
| 653 |
class="byte">21</span>. The next byte is always <span |
| 654 |
class="byte">FE</span> which is the <strong>comment label</strong>. |
| 655 |
Then we jump right to data sub-blocks containing ASCII character codes |
| 656 |
for your comment. As you can see from the example we have one data |
| 657 |
sub-block that is 9 bytes long. If you translate the character codes |
| 658 |
you see that the comment is "blueberry." The final byte, |
| 659 |
<span class="byte">00</span>, indicates a sub-block with zero bytes |
| 660 |
that follow which lets us know we have reached the end of the block.</p> |
| 661 |
|
| 662 |
<p style="text-align:center"><img src="comment_ext.gif" alt="GIF comment extension block layout" style="border: 1px solid black" /></p> |
| 663 |
|
| 664 |
<h2><a name="trailer_block">Trailer</a></h2> |
| 665 |
|
| 666 |
<p>From sample file: <span class="byte gif_trailer"> 3B </span></p> |
| 667 |
|
| 668 |
<p>The trailer block indicates when you've reached the end of the file. |
| 669 |
It is always a byte with a value of <span class="byte">3B</span>.</p> |
| 670 |
|
| 671 |
<p style="text-align:center"><img src="trailer_block.gif" alt="GIF |
| 672 |
trailer block layout" style="border: 1px solid black" /></p> |
| 673 |
|
| 674 |
<h2>Next: LZW Image Data</h2> |
| 675 |
|
| 676 |
<p>Now that you know what the basic parts of a GIF file are, let's next |
| 677 |
focus our attention on how the actual image data is stored and compressed.<p> |
| 678 |
|
| 679 |
<p><a href="lzw_image_data.html">Continue...</a></p> |
| 680 |
</div> |
| 681 |
|
| 682 |
<div style="text-align:center; margin-top: 10px; padding-top: 10px; border-top: #cecece 1px solid"> |
| 683 |
<a href="../index.html">Back to GIFLIB documentation</a> |
| 684 |
</div> |
| 685 |
|
| 686 |
</body> |
| 687 |
</html> |