GIF89M.TXT (86146B)
1 2 3 4 5 6 Cover Sheet for the GIF89a Specification 7 8 9 DEFERRED CLEAR CODE IN LZW COMPRESSION 10 11 There has been confusion about where clear codes can be found in the 12 data stream. As the specification says, they may appear at anytime. There 13 is not a requirement to send a clear code when the string table is full. 14 15 It is the encoder's decision as to when the table should be cleared. When 16 the table is full, the encoder can chose to use the table as is, making no 17 changes to it until the encoder chooses to clear it. The encoder during 18 this time sends out codes that are of the maximum Code Size. 19 20 As we can see from the above, when the decoder's table is full, it must 21 not change the table until a clear code is received. The Code Size is that 22 of the maximum Code Size. Processing other than this is done normally. 23 24 Because of a large base of decoders that do not handle the decompression in 25 this manner, we ask developers of GIF encoding software to NOT implement 26 this feature until at least January 1991 and later if they see that their 27 particular market is not ready for it. This will give developers of GIF 28 decoding software time to implement this feature and to get it into the 29 hands of their clients before the decoders start "breaking" on the new 30 GIF's. It is not required that encoders change their software to take 31 advantage of the deferred clear code, but it is for decoders. 32 33 APPLICATION EXTENSION BLOCK - APPLICATION IDENTIFIER 34 35 There will be a Courtesy Directory file located on CompuServe in the PICS 36 forum. This directory will contain Application Identifiers for Application 37 Extension Blocks that have been used by developers of GIF applications. 38 This file is intended to help keep developers that wish to create 39 Application Extension Blocks from using the same Application Identifiers. 40 This is not an official directory; it is for voluntary participation only 41 and does not guarantee that someone will not use the same identifier. 42 43 E-Mail can be sent to Larry Wood (forum manager of PICS) indicating the 44 request for inclusion in this file with an identifier. 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 GRAPHICS INTERCHANGE FORMAT(sm) 87 88 Version 89a (modified) 89 90 (c)1987,1988,1989,1990 91 92 Copyright 93 CompuServe Incorporated 94 Columbus, Ohio 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 CompuServe Incorporated Graphics Interchange Format 126 Document Date : 9 January, 1995 Programming Reference 127 128 129 130 131 132 133 134 135 136 137 Table of Contents 138 139 Disclaimer................................................................. 1 140 141 Foreword................................................................... 1 142 143 Licensing.................................................................. 1 144 145 About the Document......................................................... 2 146 147 General Description........................................................ 2 148 149 Version Numbers............................................................ 2 150 151 The Encoder................................................................ 3 152 153 The Decoder................................................................ 3 154 155 Compliance................................................................. 3 156 157 About Recommendations...................................................... 4 158 159 About Color Tables......................................................... 4 160 161 Blocks, Extensions and Scope............................................... 4 162 163 Block Sizes................................................................ 5 164 165 Using GIF as an embedded protocol.......................................... 5 166 167 Data Sub-blocks............................................................ 5 168 169 Block Terminator........................................................... 6 170 171 Header..................................................................... 7 172 173 Logical Screen Descriptor.................................................. 8 174 175 Global Color Table......................................................... 10 176 177 Image Descriptor........................................................... 11 178 179 Local Color Table.......................................................... 13 180 181 Table Based Image Data..................................................... 14 182 183 Graphic Control Extension.................................................. 15 184 185 Comment Extension.......................................................... 17 186 187 Plain Text Extension....................................................... 18 188 189 Application Extension...................................................... 21 190 191 Trailer.................................................................... 23 192 193 194 195 196 197 198 199 200 201 202 203 Quick Reference Table...................................................... 24 204 205 GIF Grammar................................................................ 25 206 207 Glossary................................................................... 27 208 209 Conventions................................................................ 28 210 211 Interlaced Images.......................................................... 29 212 213 Variable-Length-Code LZW Compression....................................... 30 214 215 On-line Capabilities Dialogue.............................................. 33 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 1 267 268 269 1. Disclaimer. 270 271 The information provided herein is subject to change without notice. In no 272 event will CompuServe Incorporated be liable for damages, including any loss of 273 revenue, loss of profits or other incidental or consequential damages arising 274 out of the use or inability to use the information; CompuServe Incorporated 275 makes no claim as to the suitability of the information. 276 277 278 2. Foreword. 279 280 This document defines the Graphics Interchange Format(sm). The specification 281 given here defines version 89a, which is an extension of version 87a. 282 283 The Graphics Interchange Format(sm) as specified here should be considered 284 complete; any deviation from it should be considered invalid, including but not 285 limited to, the use of reserved or undefined fields within control or data 286 blocks, the inclusion of extraneous data within or between blocks, the use of 287 methods or algorithms not specifically listed as part of the format, etc. In 288 general, any and all deviations, extensions or modifications not specified in 289 this document should be considered to be in violation of the format and should 290 be avoided. 291 292 293 3. Licensing. 294 295 The Graphics Interchange Format(c) is the copyright property of CompuServe 296 Incorporated. Only CompuServe Incorporated is authorized to define, redefine, 297 enhance, alter, modify or change in any way the definition of the format. 298 299 CompuServe Incorporated hereby grants a limited, non-exclusive, royalty-free 300 license for the use of the Graphics Interchange Format(sm) in computer 301 software; computer software utilizing GIF(sm) must acknowledge ownership of the 302 Graphics Interchange Format and its Service Mark by CompuServe Incorporated, in 303 User and Technical Documentation. Computer software utilizing GIF, which is 304 distributed or may be distributed without User or Technical Documentation must 305 display to the screen or printer a message acknowledging ownership of the 306 Graphics Interchange Format and the Service Mark by CompuServe Incorporated; in 307 this case, the acknowledgement may be displayed in an opening screen or leading 308 banner, or a closing screen or trailing banner. A message such as the following 309 may be used: 310 311 "The Graphics Interchange Format(c) is the Copyright property of 312 CompuServe Incorporated. GIF(sm) is a Service Mark property of 313 CompuServe Incorporated." 314 315 For further information, please contact : 316 317 CompuServe Incorporated 318 Graphics Technology Department 319 5000 Arlington Center Boulevard 320 Columbus, Ohio 43220 321 U. S. A. 322 323 CompuServe Incorporated maintains a mailing list with all those individuals and 324 organizations who wish to receive copies of this document when it is corrected 325 326 327 328 329 330 331 332 2 333 334 335 or revised. This service is offered free of charge; please provide us with your 336 mailing address. 337 338 339 Users of this specification should note that the LZW compression and 340 decompression methods described in U.S. Patent No. 4,558,302 and certain 341 corresponding foreign patents are owned by Unisys Corporation. Software and 342 hardware developers may be required to obtain a license under this patent in 343 order to develop and market products using GIF LZW compression and 344 decompression. Unisys has agreed that developers may obtain such a license on 345 reasonable, non-discriminatory terms and conditions. Further information may 346 be obtained from: Welch Licensing Department, Office of the General Counsel, M/S 347 C1SW19, Unisys Corporation, Blue Bell, PA 19424. 348 349 350 4. About the Document. 351 352 This document describes in detail the definition of the Graphics Interchange 353 Format. This document is intended as a programming reference; it is 354 recommended that the entire document be read carefully before programming, 355 because of the interdependence of the various parts. There is an individual 356 section for each of the Format blocks. Within each section, the sub-section 357 labeled Required Version refers to the version number that an encoder will have 358 to use if the corresponding block is used in the Data Stream. Within each 359 section, a diagram describes the individual fields in the block; the diagrams 360 are drawn vertically; top bytes in the diagram appear first in the Data Stream. 361 Bits within a byte are drawn most significant on the left end. Multi-byte 362 numeric fields are ordered Least Significant Byte first. Numeric constants are 363 represented as Hexadecimal numbers, preceded by "0x". Bit fields within a byte 364 are described in order from most significant bits to least significant bits. 365 366 367 5. General Description. 368 369 The Graphics Interchange Format(sm) defines a protocol intended for the on-line 370 transmission and interchange of raster graphic data in a way that is 371 independent of the hardware used in their creation or display. 372 373 The Graphics Interchange Format is defined in terms of blocks and sub-blocks 374 which contain relevant parameters and data used in the reproduction of a 375 graphic. A GIF Data Stream is a sequence of protocol blocks and sub-blocks 376 representing a collection of graphics. In general, the graphics in a Data 377 Stream are assumed to be related to some degree, and to share some control 378 information; it is recommended that encoders attempt to group together related 379 graphics in order to minimize hardware changes during processing and to 380 minimize control information overhead. For the same reason, unrelated graphics 381 or graphics which require resetting hardware parameters should be encoded 382 separately to the extent possible. 383 384 A Data Stream may originate locally, as when read from a file, or it may 385 originate remotely, as when transmitted over a data communications line. The 386 Format is defined with the assumption that an error-free Transport Level 387 Protocol is used for communications; the Format makes no provisions for 388 error-detection and error-correction. 389 390 The GIF Data Stream must be interpreted in context, that is, the application 391 program must rely on information external to the Data Stream to invoke the 392 decoder process. 393 394 395 6. Version Numbers. 396 397 The version number in the Header of a Data Stream is intended to identify the 398 minimum set of capabilities required of a decoder in order to fully process the 399 Data Stream. An encoder should use the earliest possible version number that 400 includes all the blocks used in the Data Stream. Within each block section in 401 this document, there is an entry labeled Required Version which specifies the 402 403 404 405 406 407 408 409 3 410 411 412 earliest version number that includes the corresponding block. The encoder 413 should make every attempt to use the earliest version number covering all the 414 blocks in the Data Stream; the unnecessary use of later version numbers will 415 hinder processing by some decoders. 416 417 418 7. The Encoder. 419 420 The Encoder is the program used to create a GIF Data Stream. From raster data 421 and other information, the encoder produces the necessary control and data 422 blocks needed for reproducing the original graphics. 423 424 The encoder has the following primary responsibilities. 425 426 - Include in the Data Stream all the necessary information to 427 reproduce the graphics. 428 429 - Insure that a Data Stream is labeled with the earliest possible 430 Version Number that will cover the definition of all the blocks in 431 it; this is to ensure that the largest number of decoders can 432 process the Data Stream. 433 434 - Ensure encoding of the graphics in such a way that the decoding 435 process is optimized. Avoid redundant information as much as 436 possible. 437 438 - To the extent possible, avoid grouping graphics which might 439 require resetting hardware parameters during the decoding process. 440 441 - Set to zero (off) each of the bits of each and every field 442 designated as reserved. Note that some fields in the Logical Screen 443 Descriptor and the Image Descriptor were reserved under Version 444 87a, but are used under version 89a. 445 446 447 8. The Decoder. 448 449 The Decoder is the program used to process a GIF Data Stream. It processes the 450 Data Stream sequentially, parsing the various blocks and sub-blocks, using the 451 control information to set hardware and process parameters and interpreting the 452 data to render the graphics. 453 454 The decoder has the following primary responsibilities. 455 456 - Process each graphic in the Data Stream in sequence, without 457 delays other than those specified in the control information. 458 459 - Set its hardware parameters to fit, as closely as possible, the 460 control information contained in the Data Stream. 461 462 463 9. Compliance. 464 465 An encoder or a decoder is said to comply with a given version of the Graphics 466 Interchange Format if and only if it fully conforms with and correctly 467 implements the definition of the standard associated with that version. An 468 469 470 471 472 473 474 475 4 476 477 478 encoder or a decoder may be compliant with a given version number and not 479 compliant with some subsequent version. 480 481 482 10. About Recommendations. 483 484 Each block section in this document contains an entry labeled Recommendation; 485 this section lists a set of recommendations intended to guide and organize the 486 use of the particular blocks. Such recommendations are geared towards making 487 the functions of encoders and decoders more efficient, as well as making 488 optimal use of the communications bandwidth. It is advised that these 489 recommendations be followed. 490 491 492 11. About Color Tables. 493 494 The GIF format utilizes color tables to render raster-based graphics. A color 495 table can have one of two different scopes: global or local. A Global Color 496 Table is used by all those graphics in the Data Stream which do not have a 497 Local Color Table associated with them. The scope of the Global Color Table is 498 the entire Data Stream. A Local Color Table is always associated with the 499 graphic that immediately follows it; the scope of a Local Color Table is 500 limited to that single graphic. A Local Color Table supersedes a Global Color 501 Table, that is, if a Data Stream contains a Global Color Table, and an image 502 has a Local Color Table associated with it, the decoder must save the Global 503 Color Table, use the Local Color Table to render the image, and then restore 504 the Global Color Table. Both types of color tables are optional, making it 505 possible for a Data Stream to contain numerous graphics without a color table 506 at all. For this reason, it is recommended that the decoder save the last 507 Global Color Table used until another Global Color Table is encountered. In 508 this way, a Data Stream which does not contain either a Global Color Table or 509 a Local Color Table may be processed using the last Global Color Table saved. 510 If a Global Color Table from a previous Stream is used, that table becomes the 511 Global Color Table of the present Stream. This is intended to reduce the 512 overhead incurred by color tables. In particular, it is recommended that an 513 encoder use only one Global Color Table if all the images in related Data 514 Streams can be rendered with the same table. If no color table is available at 515 all, the decoder is free to use a system color table or a table of its own. In 516 that case, the decoder may use a color table with as many colors as its 517 hardware is able to support; it is recommended that such a table have black and 518 white as its first two entries, so that monochrome images can be rendered 519 adequately. 520 521 The Definition of the GIF Format allows for a Data Stream to contain only the 522 Header, the Logical Screen Descriptor, a Global Color Table and the GIF 523 Trailer. Such a Data Stream would be used to load a decoder with a Global Color 524 Table, in preparation for subsequent Data Streams without a color table at all. 525 526 527 12. Blocks, Extensions and Scope. 528 529 Blocks can be classified into three groups : Control, Graphic-Rendering and 530 Special Purpose. Control blocks, such as the Header, the Logical Screen 531 Descriptor, the Graphic Control Extension and the Trailer, contain information 532 used to control the process of the Data Stream or information used in setting 533 hardware parameters. Graphic-Rendering blocks such as the Image Descriptor and 534 535 536 537 538 539 540 541 5 542 543 544 the Plain Text Extension contain information and data used to render a graphic 545 on the display device. Special Purpose blocks such as the Comment Extension and 546 the Application Extension are neither used to control the process of the Data 547 Stream nor do they contain information or data used to render a graphic on the 548 display device. With the exception of the Logical Screen Descriptor and the 549 Global Color Table, whose scope is the entire Data Stream, all other Control 550 blocks have a limited scope, restricted to the Graphic-Rendering block that 551 follows them. Special Purpose blocks do not delimit the scope of any Control 552 blocks; Special Purpose blocks are transparent to the decoding process. 553 Graphic-Rendering blocks and extensions are used as scope delimiters for 554 Control blocks and extensions. The labels used to identify labeled blocks fall 555 into three ranges : 0x00-0x7F (0-127) are the Graphic Rendering blocks, 556 excluding the Trailer (0x3B); 0x80-0xF9 (128-249) are the Control blocks; 557 0xFA-0xFF (250-255) are the Special Purpose blocks. These ranges are defined so 558 that decoders can handle block scope by appropriately identifying block labels, 559 even when the block itself cannot be processed. 560 561 562 13. Block Sizes. 563 564 The Block Size field in a block, counts the number of bytes remaining in the 565 block, not counting the Block Size field itself, and not counting the Block 566 Terminator, if one is to follow. Blocks other than Data Blocks are intended to 567 be of fixed length; the Block Size field is provided in order to facilitate 568 skipping them, not to allow their size to change in the future. Data blocks 569 and sub-blocks are of variable length to accommodate the amount of data. 570 571 572 14. Using GIF as an embedded protocol. 573 574 As an embedded protocol, GIF may be part of larger application protocols, 575 within which GIF is used to render graphics. In such a case, the application 576 protocol could define a block within which the GIF Data Stream would be 577 contained. The application program would then invoke a GIF decoder upon 578 encountering a block of type GIF. This approach is recommended in favor of 579 using Application Extensions, which become overhead for all other applications 580 that do not process them. Because a GIF Data Stream must be processed in 581 context, the application must rely on some means of identifying the GIF Data 582 Stream outside of the Stream itself. 583 584 585 15. Data Sub-blocks. 586 587 a. Description. Data Sub-blocks are units containing data. They do not 588 have a label, these blocks are processed in the context of control 589 blocks, wherever data blocks are specified in the format. The first byte 590 of the Data sub-block indicates the number of data bytes to follow. A 591 data sub-block may contain from 0 to 255 data bytes. The size of the 592 block does not account for the size byte itself, therefore, the empty 593 sub-block is one whose size field contains 0x00. 594 595 b. Required Version. 87a. 596 597 598 599 600 601 602 603 604 605 606 607 6 608 609 610 c. Syntax. 611 612 7 6 5 4 3 2 1 0 Field Name Type 613 +---------------+ 614 0 | | Block Size Byte 615 +---------------+ 616 1 | | 617 +- -+ 618 2 | | 619 +- -+ 620 3 | | 621 +- -+ 622 | | Data Values Byte 623 +- -+ 624 up | | 625 +- . . . . -+ 626 to | | 627 +- -+ 628 | | 629 +- -+ 630 255 | | 631 +---------------+ 632 633 i) Block Size - Number of bytes in the Data Sub-block; the size 634 must be within 0 and 255 bytes, inclusive. 635 636 ii) Data Values - Any 8-bit value. There must be exactly as many 637 Data Values as specified by the Block Size field. 638 639 d. Extensions and Scope. This type of block always occurs as part of a 640 larger unit. It does not have a scope of itself. 641 642 e. Recommendation. None. 643 644 645 16. Block Terminator. 646 647 a. Description. This zero-length Data Sub-block is used to terminate a 648 sequence of Data Sub-blocks. It contains a single byte in the position of 649 the Block Size field and does not contain data. 650 651 b. Required Version. 87a. 652 653 c. Syntax. 654 655 7 6 5 4 3 2 1 0 Field Name Type 656 +---------------+ 657 0 | | Block Size Byte 658 +---------------+ 659 660 i) Block Size - Number of bytes in the Data Sub-block; this field 661 contains the fixed value 0x00. 662 663 ii) Data Values - This block does not contain any data. 664 665 666 667 668 669 670 671 672 673 7 674 675 676 d. Extensions and Scope. This block terminates the immediately preceding 677 sequence of Data Sub-blocks. This block cannot be modified by any 678 extension. 679 680 e. Recommendation. None. 681 682 683 17. Header. 684 685 a. Description. The Header identifies the GIF Data Stream in context. The 686 Signature field marks the beginning of the Data Stream, and the Version 687 field identifies the set of capabilities required of a decoder to fully 688 process the Data Stream. This block is REQUIRED; exactly one Header must 689 be present per Data Stream. 690 691 b. Required Version. Not applicable. This block is not subject to a 692 version number. This block must appear at the beginning of every Data 693 Stream. 694 695 c. Syntax. 696 697 698 7 6 5 4 3 2 1 0 Field Name Type 699 +---------------+ 700 0 | | Signature 3 Bytes 701 +- -+ 702 1 | | 703 +- -+ 704 2 | | 705 +---------------+ 706 3 | | Version 3 Bytes 707 +- -+ 708 4 | | 709 +- -+ 710 5 | | 711 +---------------+ 712 713 i) Signature - Identifies the GIF Data Stream. This field contains 714 the fixed value 'GIF'. 715 716 ii) Version - Version number used to format the data stream. 717 Identifies the minimum set of capabilities necessary to a decoder 718 to fully process the contents of the Data Stream. 719 720 Version Numbers as of 10 July 1990 : "87a" - May 1987 721 "89a" - July 1989 722 723 Version numbers are ordered numerically increasing on the first two 724 digits starting with 87 (87,88,...,99,00,...,85,86) and 725 alphabetically increasing on the third character (a,...,z). 726 727 iii) Extensions and Scope. The scope of this block is the entire 728 Data Stream. This block cannot be modified by any extension. 729 730 731 732 733 734 735 736 737 738 739 8 740 741 742 d. Recommendations. 743 744 i) Signature - This field identifies the beginning of the GIF Data 745 Stream; it is not intended to provide a unique signature for the 746 identification of the data. It is recommended that the GIF Data 747 Stream be identified externally by the application. (Refer to 748 Appendix G for on-line identification of the GIF Data Stream.) 749 750 ii) Version - ENCODER : An encoder should use the earliest possible 751 version number that defines all the blocks used in the Data Stream. 752 When two or more Data Streams are combined, the latest of the 753 individual version numbers should be used for the resulting Data 754 Stream. DECODER : A decoder should attempt to process the data 755 stream to the best of its ability; if it encounters a version 756 number which it is not capable of processing fully, it should 757 nevertheless, attempt to process the data stream to the best of its 758 ability, perhaps after warning the user that the data may be 759 incomplete. 760 761 762 18. Logical Screen Descriptor. 763 764 a. Description. The Logical Screen Descriptor contains the parameters 765 necessary to define the area of the display device within which the 766 images will be rendered. The coordinates in this block are given with 767 respect to the top-left corner of the virtual screen; they do not 768 necessarily refer to absolute coordinates on the display device. This 769 implies that they could refer to window coordinates in a window-based 770 environment or printer coordinates when a printer is used. 771 772 This block is REQUIRED; exactly one Logical Screen Descriptor must be 773 present per Data Stream. 774 775 b. Required Version. Not applicable. This block is not subject to a 776 version number. This block must appear immediately after the Header. 777 778 c. Syntax. 779 780 7 6 5 4 3 2 1 0 Field Name Type 781 +---------------+ 782 0 | | Logical Screen Width Unsigned 783 +- -+ 784 1 | | 785 +---------------+ 786 2 | | Logical Screen Height Unsigned 787 +- -+ 788 3 | | 789 +---------------+ 790 4 | | | | | <Packed Fields> See below 791 +---------------+ 792 5 | | Background Color Index Byte 793 +---------------+ 794 6 | | Pixel Aspect Ratio Byte 795 +---------------+ 796 797 798 799 800 801 802 803 804 805 9 806 807 808 <Packed Fields> = Global Color Table Flag 1 Bit 809 Color Resolution 3 Bits 810 Sort Flag 1 Bit 811 Size of Global Color Table 3 Bits 812 813 i) Logical Screen Width - Width, in pixels, of the Logical Screen 814 where the images will be rendered in the displaying device. 815 816 ii) Logical Screen Height - Height, in pixels, of the Logical 817 Screen where the images will be rendered in the displaying device. 818 819 iii) Global Color Table Flag - Flag indicating the presence of a 820 Global Color Table; if the flag is set, the Global Color Table will 821 immediately follow the Logical Screen Descriptor. This flag also 822 selects the interpretation of the Background Color Index; if the 823 flag is set, the value of the Background Color Index field should 824 be used as the table index of the background color. (This field is 825 the most significant bit of the byte.) 826 827 Values : 0 - No Global Color Table follows, the Background 828 Color Index field is meaningless. 829 1 - A Global Color Table will immediately follow, the 830 Background Color Index field is meaningful. 831 832 iv) Color Resolution - Number of bits per primary color available 833 to the original image, minus 1. This value represents the size of 834 the entire palette from which the colors in the graphic were 835 selected, not the number of colors actually used in the graphic. 836 For example, if the value in this field is 3, then the palette of 837 the original image had 4 bits per primary color available to create 838 the image. This value should be set to indicate the richness of 839 the original palette, even if not every color from the whole 840 palette is available on the source machine. 841 842 v) Sort Flag - Indicates whether the Global Color Table is sorted. 843 If the flag is set, the Global Color Table is sorted, in order of 844 decreasing importance. Typically, the order would be decreasing 845 frequency, with most frequent color first. This assists a decoder, 846 with fewer available colors, in choosing the best subset of colors; 847 the decoder may use an initial segment of the table to render the 848 graphic. 849 850 Values : 0 - Not ordered. 851 1 - Ordered by decreasing importance, most 852 important color first. 853 854 vi) Size of Global Color Table - If the Global Color Table Flag is 855 set to 1, the value in this field is used to calculate the number 856 of bytes contained in the Global Color Table. To determine that 857 actual size of the color table, raise 2 to [the value of the field 858 + 1]. Even if there is no Global Color Table specified, set this 859 field according to the above formula so that decoders can choose 860 the best graphics mode to display the stream in. (This field is 861 made up of the 3 least significant bits of the byte.) 862 863 vii) Background Color Index - Index into the Global Color Table for 864 865 866 867 868 869 870 871 10 872 873 874 the Background Color. The Background Color is the color used for 875 those pixels on the screen that are not covered by an image. If the 876 Global Color Table Flag is set to (zero), this field should be zero 877 and should be ignored. 878 879 viii) Pixel Aspect Ratio - Factor used to compute an approximation 880 of the aspect ratio of the pixel in the original image. If the 881 value of the field is not 0, this approximation of the aspect ratio 882 is computed based on the formula: 883 884 Aspect Ratio = (Pixel Aspect Ratio + 15) / 64 885 886 The Pixel Aspect Ratio is defined to be the quotient of the pixel's 887 width over its height. The value range in this field allows 888 specification of the widest pixel of 4:1 to the tallest pixel of 889 1:4 in increments of 1/64th. 890 891 Values : 0 - No aspect ratio information is given. 892 1..255 - Value used in the computation. 893 894 d. Extensions and Scope. The scope of this block is the entire Data 895 Stream. This block cannot be modified by any extension. 896 897 e. Recommendations. None. 898 899 900 19. Global Color Table. 901 902 a. Description. This block contains a color table, which is a sequence of 903 bytes representing red-green-blue color triplets. The Global Color Table 904 is used by images without a Local Color Table and by Plain Text 905 Extensions. Its presence is marked by the Global Color Table Flag being 906 set to 1 in the Logical Screen Descriptor; if present, it immediately 907 follows the Logical Screen Descriptor and contains a number of bytes 908 equal to 909 3 x 2^(Size of Global Color Table+1). 910 911 This block is OPTIONAL; at most one Global Color Table may be present 912 per Data Stream. 913 914 b. Required Version. 87a 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 11 938 939 940 c. Syntax. 941 942 7 6 5 4 3 2 1 0 Field Name Type 943 +===============+ 944 0 | | Red 0 Byte 945 +- -+ 946 1 | | Green 0 Byte 947 +- -+ 948 2 | | Blue 0 Byte 949 +- -+ 950 3 | | Red 1 Byte 951 +- -+ 952 | | Green 1 Byte 953 +- -+ 954 up | | 955 +- . . . . -+ ... 956 to | | 957 +- -+ 958 | | Green 255 Byte 959 +- -+ 960 767 | | Blue 255 Byte 961 +===============+ 962 963 964 d. Extensions and Scope. The scope of this block is the entire Data 965 Stream. This block cannot be modified by any extension. 966 967 e. Recommendation. None. 968 969 970 20. Image Descriptor. 971 972 a. Description. Each image in the Data Stream is composed of an Image 973 Descriptor, an optional Local Color Table, and the image data. Each 974 image must fit within the boundaries of the Logical Screen, as defined 975 in the Logical Screen Descriptor. 976 977 The Image Descriptor contains the parameters necessary to process a table 978 based image. The coordinates given in this block refer to coordinates 979 within the Logical Screen, and are given in pixels. This block is a 980 Graphic-Rendering Block, optionally preceded by one or more Control 981 blocks such as the Graphic Control Extension, and may be optionally 982 followed by a Local Color Table; the Image Descriptor is always followed 983 by the image data. 984 985 This block is REQUIRED for an image. Exactly one Image Descriptor must 986 be present per image in the Data Stream. An unlimited number of images 987 may be present per Data Stream. 988 989 b. Required Version. 87a. 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 12 1004 1005 1006 c. Syntax. 1007 1008 7 6 5 4 3 2 1 0 Field Name Type 1009 +---------------+ 1010 0 | | Image Separator Byte 1011 +---------------+ 1012 1 | | Image Left Position Unsigned 1013 +- -+ 1014 2 | | 1015 +---------------+ 1016 3 | | Image Top Position Unsigned 1017 +- -+ 1018 4 | | 1019 +---------------+ 1020 5 | | Image Width Unsigned 1021 +- -+ 1022 6 | | 1023 +---------------+ 1024 7 | | Image Height Unsigned 1025 +- -+ 1026 8 | | 1027 +---------------+ 1028 9 | | | | | | <Packed Fields> See below 1029 +---------------+ 1030 1031 <Packed Fields> = Local Color Table Flag 1 Bit 1032 Interlace Flag 1 Bit 1033 Sort Flag 1 Bit 1034 Reserved 2 Bits 1035 Size of Local Color Table 3 Bits 1036 1037 i) Image Separator - Identifies the beginning of an Image 1038 Descriptor. This field contains the fixed value 0x2C. 1039 1040 ii) Image Left Position - Column number, in pixels, of the left edge 1041 of the image, with respect to the left edge of the Logical Screen. 1042 Leftmost column of the Logical Screen is 0. 1043 1044 iii) Image Top Position - Row number, in pixels, of the top edge of 1045 the image with respect to the top edge of the Logical Screen. Top 1046 row of the Logical Screen is 0. 1047 1048 iv) Image Width - Width of the image in pixels. 1049 1050 v) Image Height - Height of the image in pixels. 1051 1052 vi) Local Color Table Flag - Indicates the presence of a Local Color 1053 Table immediately following this Image Descriptor. (This field is 1054 the most significant bit of the byte.) 1055 1056 1057 Values : 0 - Local Color Table is not present. Use 1058 Global Color Table if available. 1059 1 - Local Color Table present, and to follow 1060 immediately after this Image Descriptor. 1061 1062 1063 1064 1065 1066 1067 1068 1069 13 1070 1071 1072 vii) Interlace Flag - Indicates if the image is interlaced. An image 1073 is interlaced in a four-pass interlace pattern; see Appendix E for 1074 details. 1075 1076 Values : 0 - Image is not interlaced. 1077 1 - Image is interlaced. 1078 1079 viii) Sort Flag - Indicates whether the Local Color Table is 1080 sorted. If the flag is set, the Local Color Table is sorted, in 1081 order of decreasing importance. Typically, the order would be 1082 decreasing frequency, with most frequent color first. This assists 1083 a decoder, with fewer available colors, in choosing the best subset 1084 of colors; the decoder may use an initial segment of the table to 1085 render the graphic. 1086 1087 Values : 0 - Not ordered. 1088 1 - Ordered by decreasing importance, most 1089 important color first. 1090 1091 ix) Size of Local Color Table - If the Local Color Table Flag is 1092 set to 1, the value in this field is used to calculate the number 1093 of bytes contained in the Local Color Table. To determine that 1094 actual size of the color table, raise 2 to the value of the field 1095 + 1. This value should be 0 if there is no Local Color Table 1096 specified. (This field is made up of the 3 least significant bits 1097 of the byte.) 1098 1099 d. Extensions and Scope. The scope of this block is the Table-based Image 1100 Data Block that follows it. This block may be modified by the Graphic 1101 Control Extension. 1102 1103 e. Recommendation. None. 1104 1105 1106 21. Local Color Table. 1107 1108 a. Description. This block contains a color table, which is a sequence of 1109 bytes representing red-green-blue color triplets. The Local Color Table 1110 is used by the image that immediately follows. Its presence is marked by 1111 the Local Color Table Flag being set to 1 in the Image Descriptor; if 1112 present, the Local Color Table immediately follows the Image Descriptor 1113 and contains a number of bytes equal to 1114 3x2^(Size of Local Color Table+1). 1115 If present, this color table temporarily becomes the active color table 1116 and the following image should be processed using it. This block is 1117 OPTIONAL; at most one Local Color Table may be present per Image 1118 Descriptor and its scope is the single image associated with the Image 1119 Descriptor that precedes it. 1120 1121 b. Required Version. 87a. 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 14 1136 1137 1138 c. Syntax. 1139 1140 7 6 5 4 3 2 1 0 Field Name Type 1141 +===============+ 1142 0 | | Red 0 Byte 1143 +- -+ 1144 1 | | Green 0 Byte 1145 +- -+ 1146 2 | | Blue 0 Byte 1147 +- -+ 1148 3 | | Red 1 Byte 1149 +- -+ 1150 | | Green 1 Byte 1151 +- -+ 1152 up | | 1153 +- . . . . -+ ... 1154 to | | 1155 +- -+ 1156 | | Green 255 Byte 1157 +- -+ 1158 767 | | Blue 255 Byte 1159 +===============+ 1160 1161 1162 d. Extensions and Scope. The scope of this block is the Table-based Image 1163 Data Block that immediately follows it. This block cannot be modified by 1164 any extension. 1165 1166 e. Recommendations. None. 1167 1168 1169 22. Table Based Image Data. 1170 1171 a. Description. The image data for a table based image consists of a 1172 sequence of sub-blocks, of size at most 255 bytes each, containing an 1173 index into the active color table, for each pixel in the image. Pixel 1174 indices are in order of left to right and from top to bottom. Each index 1175 must be within the range of the size of the active color table, starting 1176 at 0. The sequence of indices is encoded using the LZW Algorithm with 1177 variable-length code, as described in Appendix F 1178 1179 b. Required Version. 87a. 1180 1181 c. Syntax. The image data format is as follows: 1182 1183 7 6 5 4 3 2 1 0 Field Name Type 1184 +---------------+ 1185 | | LZW Minimum Code Size Byte 1186 +---------------+ 1187 1188 +===============+ 1189 | | 1190 / / Image Data Data Sub-blocks 1191 | | 1192 +===============+ 1193 1194 1195 1196 1197 1198 1199 1200 1201 15 1202 1203 1204 i) LZW Minimum Code Size. This byte determines the initial number 1205 of bits used for LZW codes in the image data, as described in 1206 Appendix F. 1207 1208 d. Extensions and Scope. This block has no scope, it contains raster 1209 data. Extensions intended to modify a Table-based image must appear 1210 before the corresponding Image Descriptor. 1211 1212 e. Recommendations. None. 1213 1214 1215 23. Graphic Control Extension. 1216 1217 a. Description. The Graphic Control Extension contains parameters used 1218 when processing a graphic rendering block. The scope of this extension is 1219 the first graphic rendering block to follow. The extension contains only 1220 one data sub-block. 1221 1222 This block is OPTIONAL; at most one Graphic Control Extension may precede 1223 a graphic rendering block. This is the only limit to the number of 1224 Graphic Control Extensions that may be contained in a Data Stream. 1225 1226 b. Required Version. 89a. 1227 1228 c. Syntax. 1229 1230 7 6 5 4 3 2 1 0 Field Name Type 1231 +---------------+ 1232 0 | | Extension Introducer Byte 1233 +---------------+ 1234 1 | | Graphic Control Label Byte 1235 +---------------+ 1236 1237 +---------------+ 1238 0 | | Block Size Byte 1239 +---------------+ 1240 1 | | | | | <Packed Fields> See below 1241 +---------------+ 1242 2 | | Delay Time Unsigned 1243 +- -+ 1244 3 | | 1245 +---------------+ 1246 4 | | Transparent Color Index Byte 1247 +---------------+ 1248 1249 +---------------+ 1250 0 | | Block Terminator Byte 1251 +---------------+ 1252 1253 1254 <Packed Fields> = Reserved 3 Bits 1255 Disposal Method 3 Bits 1256 User Input Flag 1 Bit 1257 Transparent Color Flag 1 Bit 1258 1259 i) Extension Introducer - Identifies the beginning of an extension 1260 1261 1262 1263 1264 1265 1266 1267 16 1268 1269 1270 block. This field contains the fixed value 0x21. 1271 1272 ii) Graphic Control Label - Identifies the current block as a 1273 Graphic Control Extension. This field contains the fixed value 1274 0xF9. 1275 1276 iii) Block Size - Number of bytes in the block, after the Block 1277 Size field and up to but not including the Block Terminator. This 1278 field contains the fixed value 4. 1279 1280 iv) Disposal Method - Indicates the way in which the graphic is to 1281 be treated after being displayed. 1282 1283 Values : 0 - No disposal specified. The decoder is 1284 not required to take any action. 1285 1 - Do not dispose. The graphic is to be left 1286 in place. 1287 2 - Restore to background color. The area used by the 1288 graphic must be restored to the background color. 1289 3 - Restore to previous. The decoder is required to 1290 restore the area overwritten by the graphic with 1291 what was there prior to rendering the graphic. 1292 4-7 - To be defined. 1293 1294 v) User Input Flag - Indicates whether or not user input is 1295 expected before continuing. If the flag is set, processing will 1296 continue when user input is entered. The nature of the User input 1297 is determined by the application (Carriage Return, Mouse Button 1298 Click, etc.). 1299 1300 Values : 0 - User input is not expected. 1301 1 - User input is expected. 1302 1303 When a Delay Time is used and the User Input Flag is set, 1304 processing will continue when user input is received or when the 1305 delay time expires, whichever occurs first. 1306 1307 vi) Transparency Flag - Indicates whether a transparency index is 1308 given in the Transparent Index field. (This field is the least 1309 significant bit of the byte.) 1310 1311 Values : 0 - Transparent Index is not given. 1312 1 - Transparent Index is given. 1313 1314 vii) Delay Time - If not 0, this field specifies the number of 1315 hundredths (1/100) of a second to wait before continuing with the 1316 processing of the Data Stream. The clock starts ticking immediately 1317 after the graphic is rendered. This field may be used in 1318 conjunction with the User Input Flag field. 1319 1320 viii) Transparency Index - The Transparency Index is such that when 1321 encountered, the corresponding pixel of the display device is not 1322 modified and processing goes on to the next pixel. The index is 1323 present if and only if the Transparency Flag is set to 1. 1324 1325 ix) Block Terminator - This zero-length data block marks the end of 1326 1327 1328 1329 1330 1331 1332 1333 17 1334 1335 the Graphic Control Extension. 1336 1337 d. Extensions and Scope. The scope of this Extension is the graphic 1338 rendering block that follows it; it is possible for other extensions to 1339 be present between this block and its target. This block can modify the 1340 Image Descriptor Block and the Plain Text Extension. 1341 1342 e. Recommendations. 1343 1344 i) Disposal Method - The mode Restore To Previous is intended to be 1345 used in small sections of the graphic; the use of this mode imposes 1346 severe demands on the decoder to store the section of the graphic 1347 that needs to be saved. For this reason, this mode should be used 1348 sparingly. This mode is not intended to save an entire graphic or 1349 large areas of a graphic; when this is the case, the encoder should 1350 make every attempt to make the sections of the graphic to be 1351 restored be separate graphics in the data stream. In the case where 1352 a decoder is not capable of saving an area of a graphic marked as 1353 Restore To Previous, it is recommended that a decoder restore to 1354 the background color. 1355 1356 ii) User Input Flag - When the flag is set, indicating that user 1357 input is expected, the decoder may sound the bell (0x07) to alert 1358 the user that input is being expected. In the absence of a 1359 specified Delay Time, the decoder should wait for user input 1360 indefinitely. It is recommended that the encoder not set the User 1361 Input Flag without a Delay Time specified. 1362 1363 1364 24. Comment Extension. 1365 1366 a. Description. The Comment Extension contains textual information which 1367 is not part of the actual graphics in the GIF Data Stream. It is suitable 1368 for including comments about the graphics, credits, descriptions or any 1369 other type of non-control and non-graphic data. The Comment Extension 1370 may be ignored by the decoder, or it may be saved for later processing; 1371 under no circumstances should a Comment Extension disrupt or interfere 1372 with the processing of the Data Stream. 1373 1374 This block is OPTIONAL; any number of them may appear in the Data Stream. 1375 1376 b. Required Version. 89a. 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 18 1400 1401 1402 c. Syntax. 1403 1404 7 6 5 4 3 2 1 0 Field Name Type 1405 +---------------+ 1406 0 | | Extension Introducer Byte 1407 +---------------+ 1408 1 | | Comment Label Byte 1409 +---------------+ 1410 1411 +===============+ 1412 | | 1413 N | | Comment Data Data Sub-blocks 1414 | | 1415 +===============+ 1416 1417 +---------------+ 1418 0 | | Block Terminator Byte 1419 +---------------+ 1420 1421 i) Extension Introducer - Identifies the beginning of an extension 1422 block. This field contains the fixed value 0x21. 1423 1424 ii) Comment Label - Identifies the block as a Comment Extension. 1425 This field contains the fixed value 0xFE. 1426 1427 iii) Comment Data - Sequence of sub-blocks, each of size at most 1428 255 bytes and at least 1 byte, with the size in a byte preceding 1429 the data. The end of the sequence is marked by the Block 1430 Terminator. 1431 1432 iv) Block Terminator - This zero-length data block marks the end of 1433 the Comment Extension. 1434 1435 d. Extensions and Scope. This block does not have scope. This block 1436 cannot be modified by any extension. 1437 1438 e. Recommendations. 1439 1440 i) Data - This block is intended for humans. It should contain 1441 text using the 7-bit ASCII character set. This block should 1442 not be used to store control information for custom processing. 1443 1444 ii) Position - This block may appear at any point in the Data 1445 Stream at which a block can begin; however, it is recommended that 1446 Comment Extensions do not interfere with Control or Data blocks; 1447 they should be located at the beginning or at the end of the Data 1448 Stream to the extent possible. 1449 1450 1451 25. Plain Text Extension. 1452 1453 a. Description. The Plain Text Extension contains textual data and the 1454 parameters necessary to render that data as a graphic, in a simple form. 1455 The textual data will be encoded with the 7-bit printable ASCII 1456 characters. Text data are rendered using a grid of character cells 1457 1458 1459 1460 1461 1462 1463 1464 1465 19 1466 1467 1468 defined by the parameters in the block fields. Each character is rendered 1469 in an individual cell. The textual data in this block is to be rendered 1470 as mono-spaced characters, one character per cell, with a best fitting 1471 font and size. For further information, see the section on 1472 Recommendations below. The data characters are taken sequentially from 1473 the data portion of the block and rendered within a cell, starting with 1474 the upper left cell in the grid and proceeding from left to right and 1475 from top to bottom. Text data is rendered until the end of data is 1476 reached or the character grid is filled. The Character Grid contains an 1477 integral number of cells; in the case that the cell dimensions do not 1478 allow for an integral number, fractional cells must be discarded; an 1479 encoder must be careful to specify the grid dimensions accurately so that 1480 this does not happen. This block requires a Global Color Table to be 1481 available; the colors used by this block reference the Global Color Table 1482 in the Stream if there is one, or the Global Color Table from a previous 1483 Stream, if one was saved. This block is a graphic rendering block, 1484 therefore it may be modified by a Graphic Control Extension. This block 1485 is OPTIONAL; any number of them may appear in the Data Stream. 1486 1487 b. Required Version. 89a. 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 20 1532 1533 1534 c. Syntax. 1535 1536 7 6 5 4 3 2 1 0 Field Name Type 1537 +---------------+ 1538 0 | | Extension Introducer Byte 1539 +---------------+ 1540 1 | | Plain Text Label Byte 1541 +---------------+ 1542 1543 +---------------+ 1544 0 | | Block Size Byte 1545 +---------------+ 1546 1 | | Text Grid Left Position Unsigned 1547 +- -+ 1548 2 | | 1549 +---------------+ 1550 3 | | Text Grid Top Position Unsigned 1551 +- -+ 1552 4 | | 1553 +---------------+ 1554 5 | | Text Grid Width Unsigned 1555 +- -+ 1556 6 | | 1557 +---------------+ 1558 7 | | Text Grid Height Unsigned 1559 +- -+ 1560 8 | | 1561 +---------------+ 1562 9 | | Character Cell Width Byte 1563 +---------------+ 1564 10 | | Character Cell Height Byte 1565 +---------------+ 1566 11 | | Text Foreground Color Index Byte 1567 +---------------+ 1568 12 | | Text Background Color Index Byte 1569 +---------------+ 1570 1571 +===============+ 1572 | | 1573 N | | Plain Text Data Data Sub-blocks 1574 | | 1575 +===============+ 1576 1577 +---------------+ 1578 0 | | Block Terminator Byte 1579 +---------------+ 1580 1581 i) Extension Introducer - Identifies the beginning of an extension 1582 block. This field contains the fixed value 0x21. 1583 1584 ii) Plain Text Label - Identifies the current block as a Plain Text 1585 Extension. This field contains the fixed value 0x01. 1586 1587 iii) Block Size - Number of bytes in the extension, after the Block 1588 Size field and up to but not including the beginning of the data 1589 portion. This field contains the fixed value 12. 1590 1591 1592 1593 1594 1595 1596 1597 21 1598 1599 1600 iv) Text Grid Left Position - Column number, in pixels, of the left 1601 edge of the text grid, with respect to the left edge of the Logical 1602 Screen. 1603 1604 v) Text Grid Top Position - Row number, in pixels, of the top edge 1605 of the text grid, with respect to the top edge of the Logical 1606 Screen. 1607 1608 vi) Image Grid Width - Width of the text grid in pixels. 1609 1610 vii) Image Grid Height - Height of the text grid in pixels. 1611 1612 viii) Character Cell Width - Width, in pixels, of each cell in the 1613 grid. 1614 1615 ix) Character Cell Height - Height, in pixels, of each cell in the 1616 grid. 1617 1618 x) Text Foreground Color Index - Index into the Global Color Table 1619 to be used to render the text foreground. 1620 1621 xi) Text Background Color Index - Index into the Global Color Table 1622 to be used to render the text background. 1623 1624 xii) Plain Text Data - Sequence of sub-blocks, each of size at most 1625 255 bytes and at least 1 byte, with the size in a byte preceding 1626 the data. The end of the sequence is marked by the Block 1627 Terminator. 1628 1629 xiii) Block Terminator - This zero-length data block marks the end 1630 of the Plain Text Data Blocks. 1631 1632 d. Extensions and Scope. The scope of this block is the Plain Text Data 1633 Block contained in it. This block may be modified by the Graphic Control 1634 Extension. 1635 1636 e. Recommendations. The data in the Plain Text Extension is assumed to be 1637 preformatted. The selection of font and size is left to the discretion of 1638 the decoder. If characters less than 0x20 or greater than 0xf7 are 1639 encountered, it is recommended that the decoder display a Space character 1640 (0x20). The encoder should use grid and cell dimensions such that an 1641 integral number of cells fit in the grid both horizontally as well as 1642 vertically. For broadest compatibility, character cell dimensions should 1643 be around 8x8 or 8x16 (width x height); consider an image for unusual 1644 sized text. 1645 1646 1647 26. Application Extension. 1648 1649 a. Description. The Application Extension contains application-specific 1650 information; it conforms with the extension block syntax, as described 1651 below, and its block label is 0xFF. 1652 1653 b. Required Version. 89a. 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 22 1664 1665 1666 c. Syntax. 1667 1668 7 6 5 4 3 2 1 0 Field Name Type 1669 +---------------+ 1670 0 | | Extension Introducer Byte 1671 +---------------+ 1672 1 | | Extension Label Byte 1673 +---------------+ 1674 1675 +---------------+ 1676 0 | | Block Size Byte 1677 +---------------+ 1678 1 | | 1679 +- -+ 1680 2 | | 1681 +- -+ 1682 3 | | Application Identifier 8 Bytes 1683 +- -+ 1684 4 | | 1685 +- -+ 1686 5 | | 1687 +- -+ 1688 6 | | 1689 +- -+ 1690 7 | | 1691 +- -+ 1692 8 | | 1693 +---------------+ 1694 9 | | 1695 +- -+ 1696 10 | | Appl. Authentication Code 3 Bytes 1697 +- -+ 1698 11 | | 1699 +---------------+ 1700 1701 +===============+ 1702 | | 1703 | | Application Data Data Sub-blocks 1704 | | 1705 | | 1706 +===============+ 1707 1708 +---------------+ 1709 0 | | Block Terminator Byte 1710 +---------------+ 1711 1712 i) Extension Introducer - Defines this block as an extension. This 1713 field contains the fixed value 0x21. 1714 1715 ii) Application Extension Label - Identifies the block as an 1716 Application Extension. This field contains the fixed value 0xFF. 1717 1718 iii) Block Size - Number of bytes in this extension block, 1719 following the Block Size field, up to but not including the 1720 beginning of the Application Data. This field contains the fixed 1721 value 11. 1722 1723 1724 1725 1726 1727 1728 1729 23 1730 1731 1732 iv) Application Identifier - Sequence of eight printable ASCII 1733 characters used to identify the application owning the Application 1734 Extension. 1735 1736 v) Application Authentication Code - Sequence of three bytes used 1737 to authenticate the Application Identifier. An Application program 1738 may use an algorithm to compute a binary code that uniquely 1739 identifies it as the application owning the Application Extension. 1740 1741 1742 d. Extensions and Scope. This block does not have scope. This block 1743 cannot be modified by any extension. 1744 1745 e. Recommendation. None. 1746 1747 1748 27. Trailer. 1749 1750 a. Description. This block is a single-field block indicating the end of 1751 the GIF Data Stream. It contains the fixed value 0x3B. 1752 1753 b. Required Version. 87a. 1754 1755 c. Syntax. 1756 1757 7 6 5 4 3 2 1 0 Field Name Type 1758 +---------------+ 1759 0 | | GIF Trailer Byte 1760 +---------------+ 1761 1762 d. Extensions and Scope. This block does not have scope, it terminates 1763 the GIF Data Stream. This block may not be modified by any extension. 1764 1765 e. Recommendations. None. 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 24 1796 1797 1798 Appendix 1799 A. Quick Reference Table. 1800 1801 Block Name Required Label Ext. Vers. 1802 Application Extension Opt. (*) 0xFF (255) yes 89a 1803 Comment Extension Opt. (*) 0xFE (254) yes 89a 1804 Global Color Table Opt. (1) none no 87a 1805 Graphic Control Extension Opt. (*) 0xF9 (249) yes 89a 1806 Header Req. (1) none no N/A 1807 Image Descriptor Opt. (*) 0x2C (044) no 87a (89a) 1808 Local Color Table Opt. (*) none no 87a 1809 Logical Screen Descriptor Req. (1) none no 87a (89a) 1810 Plain Text Extension Opt. (*) 0x01 (001) yes 89a 1811 Trailer Req. (1) 0x3B (059) no 87a 1812 1813 Unlabeled Blocks 1814 Header Req. (1) none no N/A 1815 Logical Screen Descriptor Req. (1) none no 87a (89a) 1816 Global Color Table Opt. (1) none no 87a 1817 Local Color Table Opt. (*) none no 87a 1818 1819 Graphic-Rendering Blocks 1820 Plain Text Extension Opt. (*) 0x01 (001) yes 89a 1821 Image Descriptor Opt. (*) 0x2C (044) no 87a (89a) 1822 1823 Control Blocks 1824 Graphic Control Extension Opt. (*) 0xF9 (249) yes 89a 1825 1826 Special Purpose Blocks 1827 Trailer Req. (1) 0x3B (059) no 87a 1828 Comment Extension Opt. (*) 0xFE (254) yes 89a 1829 Application Extension Opt. (*) 0xFF (255) yes 89a 1830 1831 legend: (1) if present, at most one occurrence 1832 (*) zero or more occurrences 1833 (+) one or more occurrences 1834 1835 Notes : The Header is not subject to Version Numbers. 1836 (89a) The Logical Screen Descriptor and the Image Descriptor retained their 1837 syntax from version 87a to version 89a, but some fields reserved under version 1838 87a are used under version 89a. 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 25 1862 1863 1864 Appendix 1865 B. GIF Grammar. 1866 1867 A Grammar is a form of notation to represent the sequence in which certain 1868 objects form larger objects. A grammar is also used to represent the number of 1869 objects that can occur at a given position. The grammar given here represents 1870 the sequence of blocks that form the GIF Data Stream. A grammar is given by 1871 listing its rules. Each rule consists of the left-hand side, followed by some 1872 form of equals sign, followed by the right-hand side. In a rule, the 1873 right-hand side describes how the left-hand side is defined. The right-hand 1874 side consists of a sequence of entities, with the possible presence of special 1875 symbols. The following legend defines the symbols used in this grammar for GIF. 1876 1877 Legend: <> grammar word 1878 ::= defines symbol 1879 * zero or more occurrences 1880 + one or more occurrences 1881 | alternate element 1882 [] optional element 1883 1884 Example: 1885 1886 <GIF Data Stream> ::= Header <Logical Screen> <Data>* Trailer 1887 1888 This rule defines the entity <GIF Data Stream> as follows. It must begin with a 1889 Header. The Header is followed by an entity called Logical Screen, which is 1890 defined below by another rule. The Logical Screen is followed by the entity 1891 Data, which is also defined below by another rule. Finally, the entity Data is 1892 followed by the Trailer. Since there is no rule defining the Header or the 1893 Trailer, this means that these blocks are defined in the document. The entity 1894 Data has a special symbol (*) following it which means that, at this position, 1895 the entity Data may be repeated any number of times, including 0 times. For 1896 further reading on this subject, refer to a standard text on Programming 1897 Languages. 1898 1899 1900 The Grammar. 1901 1902 <GIF Data Stream> ::= Header <Logical Screen> <Data>* Trailer 1903 1904 <Logical Screen> ::= Logical Screen Descriptor [Global Color Table] 1905 1906 <Data> ::= <Graphic Block> | 1907 <Special-Purpose Block> 1908 1909 <Graphic Block> ::= [Graphic Control Extension] <Graphic-Rendering Block> 1910 1911 <Graphic-Rendering Block> ::= <Table-Based Image> | 1912 Plain Text Extension 1913 1914 <Table-Based Image> ::= Image Descriptor [Local Color Table] Image Data 1915 1916 <Special-Purpose Block> ::= Application Extension | 1917 Comment Extension 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 26 1928 1929 1930 NOTE : The grammar indicates that it is possible for a GIF Data Stream to 1931 contain the Header, the Logical Screen Descriptor, a Global Color Table and the 1932 GIF Trailer. This special case is used to load a GIF decoder with a Global 1933 Color Table, in preparation for subsequent Data Streams without color tables at 1934 all. 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 27 1994 1995 1996 Appendix 1997 C. Glossary. 1998 1999 Active Color Table - Color table used to render the next graphic. If the next 2000 graphic is an image which has a Local Color Table associated with it, the 2001 active color table becomes the Local Color Table associated with that image. 2002 If the next graphic is an image without a Local Color Table, or a Plain Text 2003 Extension, the active color table is the Global Color Table associated with the 2004 Data Stream, if there is one; if there is no Global Color Table in the Data 2005 Stream, the active color table is a color table saved from a previous Data 2006 Stream, or one supplied by the decoder. 2007 2008 Block - Collection of bytes forming a protocol unit. In general, the term 2009 includes labeled and unlabeled blocks, as well as Extensions. 2010 2011 Data Stream - The GIF Data Stream is composed of blocks and sub-blocks 2012 representing images and graphics, together with control information to render 2013 them on a display device. All control and data blocks in the Data Stream must 2014 follow the Header and must precede the Trailer. 2015 2016 Decoder - A program capable of processing a GIF Data Stream to render the 2017 images and graphics contained in it. 2018 2019 Encoder - A program capable of capturing and formatting image and graphic 2020 raster data, following the definitions of the Graphics Interchange Format. 2021 2022 Extension - A protocol block labeled by the Extension Introducer 0x21. 2023 2024 Extension Introducer - Label (0x21) defining an Extension. 2025 2026 Graphic - Data which can be rendered on the screen by virtue of some algorithm. 2027 The term graphic is more general than the term image; in addition to images, 2028 the term graphic also includes data such as text, which is rendered using 2029 character bit-maps. 2030 2031 Image - Data representing a picture or a drawing; an image is represented by an 2032 array of pixels called the raster of the image. 2033 2034 Raster - Array of pixel values representing an image. 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 28 2060 2061 2062 Appendix 2063 D. Conventions. 2064 2065 Animation - The Graphics Interchange Format is not intended as a platform for 2066 animation, even though it can be done in a limited way. 2067 2068 Byte Ordering - Unless otherwise stated, multi-byte numeric fields are ordered 2069 with the Least Significant Byte first. 2070 2071 Color Indices - Color indices always refer to the active color table, either 2072 the Global Color Table or the Local Color Table. 2073 2074 Color Order - Unless otherwise stated, all triple-component RGB color values 2075 are specified in Red-Green-Blue order. 2076 2077 Color Tables - Both color tables, the Global and the Local, are optional; if 2078 present, the Global Color Table is to be used with every image in the Data 2079 Stream for which a Local Color Table is not given; if present, a Local Color 2080 Table overrides the Global Color Table. However, if neither color table is 2081 present, the application program is free to use an arbitrary color table. If 2082 the graphics in several Data Streams are related and all use the same color 2083 table, an encoder could place the color table as the Global Color Table in the 2084 first Data Stream and leave subsequent Data Streams without a Global Color 2085 Table or any Local Color Tables; in this way, the overhead for the table is 2086 eliminated. It is recommended that the decoder save the previous Global Color 2087 Table to be used with the Data Stream that follows, in case it does not contain 2088 either a Global Color Table or any Local Color Tables. In general, this allows 2089 the application program to use past color tables, significantly reducing 2090 transmission overhead. 2091 2092 Extension Blocks - Extensions are defined using the Extension Introducer code 2093 to mark the beginning of the block, followed by a block label, identifying the 2094 type of extension. Extension Codes are numbers in the range from 0x00 to 0xFF, 2095 inclusive. Special purpose extensions are transparent to the decoder and may be 2096 omitted when transmitting the Data Stream on-line. The GIF capabilities 2097 dialogue makes the provision for the receiver to request the transmission of 2098 all blocks; the default state in this regard is no transmission of Special 2099 purpose blocks. 2100 2101 Reserved Fields - All Reserved Fields are expected to have each bit set to zero 2102 (off). 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 29 2126 2127 2128 Appendix 2129 E. Interlaced Images. 2130 2131 The rows of an Interlaced images are arranged in the following order: 2132 2133 Group 1 : Every 8th. row, starting with row 0. (Pass 1) 2134 Group 2 : Every 8th. row, starting with row 4. (Pass 2) 2135 Group 3 : Every 4th. row, starting with row 2. (Pass 3) 2136 Group 4 : Every 2nd. row, starting with row 1. (Pass 4) 2137 2138 The Following example illustrates how the rows of an interlaced image are 2139 ordered. 2140 2141 Row Number Interlace Pass 2142 2143 0 ----------------------------------------- 1 2144 1 ----------------------------------------- 4 2145 2 ----------------------------------------- 3 2146 3 ----------------------------------------- 4 2147 4 ----------------------------------------- 2 2148 5 ----------------------------------------- 4 2149 6 ----------------------------------------- 3 2150 7 ----------------------------------------- 4 2151 8 ----------------------------------------- 1 2152 9 ----------------------------------------- 4 2153 10 ----------------------------------------- 3 2154 11 ----------------------------------------- 4 2155 12 ----------------------------------------- 2 2156 13 ----------------------------------------- 4 2157 14 ----------------------------------------- 3 2158 15 ----------------------------------------- 4 2159 16 ----------------------------------------- 1 2160 17 ----------------------------------------- 4 2161 18 ----------------------------------------- 3 2162 19 ----------------------------------------- 4 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 30 2192 2193 2194 Appendix 2195 F. Variable-Length-Code LZW Compression. 2196 2197 The Variable-Length-Code LZW Compression is a variation of the Lempel-Ziv 2198 Compression algorithm in which variable-length codes are used to replace 2199 patterns detected in the original data. The algorithm uses a code or 2200 translation table constructed from the patterns encountered in the original 2201 data; each new pattern is entered into the table and its index is used to 2202 replace it in the compressed stream. 2203 2204 The compressor takes the data from the input stream and builds a code or 2205 translation table with the patterns as it encounters them; each new pattern is 2206 entered into the code table and its index is added to the output stream; when a 2207 pattern is encountered which had been detected since the last code table 2208 refresh, its index from the code table is put on the output stream, thus 2209 achieving the data compression. The expander takes input from the compressed 2210 data stream and builds the code or translation table from it; as the compressed 2211 data stream is processed, codes are used to index into the code table and the 2212 corresponding data is put on the decompressed output stream, thus achieving 2213 data decompression. The details of the algorithm are explained below. The 2214 Variable-Length-Code aspect of the algorithm is based on an initial code size 2215 (LZW-initial code size), which specifies the initial number of bits used for 2216 the compression codes. When the number of patterns detected by the compressor 2217 in the input stream exceeds the number of patterns encodable with the current 2218 number of bits, the number of bits per LZW code is increased by one. 2219 2220 The Raster Data stream that represents the actual output image can be 2221 represented as: 2222 2223 7 6 5 4 3 2 1 0 2224 +---------------+ 2225 | LZW code size | 2226 +---------------+ 2227 2228 +---------------+ ----+ 2229 | block size | | 2230 +---------------+ | 2231 | | +-- Repeated as many 2232 | data bytes | | times as necessary. 2233 | | | 2234 +---------------+ ----+ 2235 2236 . . . . . . ------- The code that terminates the LZW 2237 compressed data must appear before 2238 Block Terminator. 2239 +---------------+ 2240 |0 0 0 0 0 0 0 0| Block Terminator 2241 +---------------+ 2242 2243 The conversion of the image from a series of pixel values to a transmitted or 2244 stored character stream involves several steps. In brief these steps are: 2245 2246 1. Establish the Code Size - Define the number of bits needed to represent the 2247 actual data. 2248 2249 2. Compress the Data - Compress the series of image pixels to a series of 2250 2251 2252 2253 2254 2255 2256 2257 31 2258 2259 2260 compression codes. 2261 2262 3. Build a Series of Bytes - Take the set of compression codes and convert to a 2263 string of 8-bit bytes. 2264 2265 4. Package the Bytes - Package sets of bytes into blocks preceded by character 2266 counts and output. 2267 2268 ESTABLISH CODE SIZE 2269 2270 The first byte of the Compressed Data stream is a value indicating the minimum 2271 number of bits required to represent the set of actual pixel values. Normally 2272 this will be the same as the number of color bits. Because of some algorithmic 2273 constraints however, black & white images which have one color bit must be 2274 indicated as having a code size of 2. 2275 This code size value also implies that the compression codes must start out one 2276 bit longer. 2277 2278 COMPRESSION 2279 2280 The LZW algorithm converts a series of data values into a series of codes which 2281 may be raw values or a code designating a series of values. Using text 2282 characters as an analogy, the output code consists of a character or a code 2283 representing a string of characters. 2284 2285 The LZW algorithm used in GIF matches algorithmically with the standard LZW 2286 algorithm with the following differences: 2287 2288 1. A special Clear code is defined which resets all compression/decompression 2289 parameters and tables to a start-up state. The value of this code is 2**<code 2290 size>. For example if the code size indicated was 4 (image was 4 bits/pixel) 2291 the Clear code value would be 16 (10000 binary). The Clear code can appear at 2292 any point in the image data stream and therefore requires the LZW algorithm to 2293 process succeeding codes as if a new data stream was starting. Encoders should 2294 output a Clear code as the first code of each image data stream. 2295 2296 2. An End of Information code is defined that explicitly indicates the end of 2297 the image data stream. LZW processing terminates when this code is encountered. 2298 It must be the last code output by the encoder for an image. The value of this 2299 code is <Clear code>+1. 2300 2301 3. The first available compression code value is <Clear code>+2. 2302 2303 4. The output codes are of variable length, starting at <code size>+1 bits per 2304 code, up to 12 bits per code. This defines a maximum code value of 4095 2305 (0xFFF). Whenever the LZW code value would exceed the current code length, the 2306 code length is increased by one. The packing/unpacking of these codes must then 2307 be altered to reflect the new code length. 2308 2309 BUILD 8-BIT BYTES 2310 2311 Because the LZW compression used for GIF creates a series of variable length 2312 codes, of between 3 and 12 bits each, these codes must be reformed into a 2313 series of 8-bit bytes that will be the characters actually stored or 2314 transmitted. This provides additional compression of the image. The codes are 2315 formed into a stream of bits as if they were packed right to left and then 2316 2317 2318 2319 2320 2321 2322 2323 32 2324 2325 2326 picked off 8 bits at a time to be output. 2327 2328 Assuming a character array of 8 bits per character and using 5 bit codes to be 2329 packed, an example layout would be similar to: 2330 2331 2332 +---------------+ 2333 0 | | bbbaaaaa 2334 +---------------+ 2335 1 | | dcccccbb 2336 +---------------+ 2337 2 | | eeeedddd 2338 +---------------+ 2339 3 | | ggfffffe 2340 +---------------+ 2341 4 | | hhhhhggg 2342 +---------------+ 2343 . . . 2344 +---------------+ 2345 N | | 2346 +---------------+ 2347 2348 2349 Note that the physical packing arrangement will change as the number of bits 2350 per compression code change but the concept remains the same. 2351 2352 PACKAGE THE BYTES 2353 2354 Once the bytes have been created, they are grouped into blocks for output by 2355 preceding each block of 0 to 255 bytes with a character count byte. A block 2356 with a zero byte count terminates the Raster Data stream for a given image. 2357 These blocks are what are actually output for the GIF image. This block format 2358 has the side effect of allowing a decoding program the ability to read past the 2359 actual image data if necessary by reading block counts and then skipping over 2360 the data. 2361 2362 2363 2364 FURTHER READING 2365 2366 [1] Ziv, J. and Lempel, A. : "A Universal Algorithm for Sequential Data 2367 Compression", IEEE Transactions on Information Theory, May 1977. 2368 [2] Welch, T. : "A Technique for High-Performance Data Compression", Computer, 2369 June 1984. 2370 [3] Nelson, M.R. : "LZW Data Compression", Dr. Dobb's Journal, October 1989. 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 33 2390 2391 2392 Appendix 2393 G. On-line Capabilities Dialogue. 2394 2395 NOTE : This section is currently (10 July 1990) under revision; the information 2396 provided here should be used as general guidelines. Code written based on this 2397 information should be designed in a flexible way to accommodate any changes 2398 resulting from the revisions. 2399 2400 The following sequences are defined for use in mediating control between a GIF 2401 sender and GIF receiver over an interactive communications line. These 2402 sequences do not apply to applications that involve downloading of static GIF 2403 files and are not considered part of a GIF file. 2404 2405 GIF CAPABILITIES ENQUIRY 2406 2407 The GIF Capabilities Enquiry sequence is issued from a host and requests an 2408 interactive GIF decoder to return a response message that defines the graphics 2409 parameters for the decoder. This involves returning information about available 2410 screen sizes, number of bits/color supported and the amount of color detail 2411 supported. The escape sequence for the GIF Capabilities Enquiry is defined as: 2412 2413 ESC[>0g 0x1B 0x5B 0x3E 0x30 0x67 2414 2415 GIF CAPABILITIES RESPONSE 2416 2417 The GIF Capabilities Response message is returned by an interactive GIF decoder 2418 and defines the decoder's display capabilities for all graphics modes that are 2419 supported by the software. Note that this can also include graphics printers as 2420 well as a monitor screen. The general format of this message is: 2421 2422 #version;protocol{;dev, width, height, color-bits, color-res}...<CR> 2423 2424 2425 '#' GIF Capabilities Response identifier character. 2426 version GIF format version number; initially '87a'. 2427 protocol='0' No end-to-end protocol supported by decoder Transfer as direct 2428 8-bit data stream. 2429 protocol='1' Can use CIS B+ error correction protocol to transfer GIF data 2430 interactively from the host directly to the display. 2431 dev = '0' Screen parameter set follows. 2432 dev = '1' Printer parameter set follows. 2433 width Maximum supported display width in pixels. 2434 height Maximum supported display height in pixels. 2435 color-bits Number of bits per pixel supported. The number of supported 2436 colors is therefore 2**color-bits. 2437 color-res Number of bits per color component supported in the hardware 2438 color palette. If color-res is '0' then no hardware palette 2439 table is available. 2440 2441 Note that all values in the GIF Capabilities Response are returned as ASCII 2442 decimal numbers and the message is terminated by a Carriage Return character. 2443 2444 The following GIF Capabilities Response message describes three standard IBM PC 2445 Enhanced Graphics Adapter configurations with no printer; the GIF data stream 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 34 2456 2457 2458 can be processed within an error correcting protocol: 2459 2460 #87a;1;0,320,200,4,0;0,640,200,2,2;0,640,350,4,2<CR> 2461 2462 ENTER GIF GRAPHICS MODE 2463 2464 Two sequences are currently defined to invoke an interactive GIF decoder into 2465 action. The only difference between them is that different output media are 2466 selected. These sequences are: 2467 2468 ESC[>1g Display GIF image on screen 2469 2470 0x1B 0x5B 0x3E 0x31 0x67 2471 2472 ESC[>2g Display image directly to an attached graphics printer. The image may 2473 optionally be displayed on the screen as well. 2474 2475 0x1B 0x5B 0x3E 0x32 0x67 2476 2477 Note that the 'g' character terminating each sequence is in lowercase. 2478 2479 INTERACTIVE ENVIRONMENT 2480 2481 The assumed environment for the transmission of GIF image data from an 2482 interactive application is a full 8-bit data stream from host to micro. All 2483 256 character codes must be transferrable. The establishing of an 8-bit data 2484 path for communications will normally be taken care of by the host application 2485 programs. It is however up to the receiving communications programs supporting 2486 GIF to be able to receive and pass on all 256 8-bit codes to the GIF decoder 2487 software.