• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1<?xml version="1.0" encoding="UTF-8"?>
2<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /><title>The GIFLIB Library</title><meta name="generator" content="DocBook XSL Stylesheets V1.79.1" /></head><body><div class="article"><div class="titlepage"><div><div><h2 class="title"><a id="idm1"></a>The GIFLIB Library</h2></div><div><div class="author"><h3 class="author"><span class="firstname">Eric</span> <span class="othername">Steven</span> <span class="surname">Raymond</span></h3><div class="affiliation"><span class="orgname"><a class="ulink" href="http://catb.org/~esr/" target="_top">
3    Thyrsus Enterprises</a><br /></span><div class="address"><p><br />
4    <code class="email">&lt;<a class="email" href="mailto:esr@thyrsus.com">esr@thyrsus.com</a>&gt;</code><br />
5    </p></div></div></div></div><div><p class="copyright">Copyright © 2012 Eric S. Raymond</p></div></div><hr /></div><div class="toc"><p><strong>Table of Contents</strong></p><dl class="toc"><dt><span class="sect1"><a href="#idm16">Introduction</a></span></dt><dt><span class="sect1"><a href="#idm21">Credit and Blame</a></span></dt><dt><span class="sect1"><a href="#idm27">The GIF descriptor</a></span></dt><dt><span class="sect1"><a href="#idm55">Decoding (dgif_lib.c)</a></span></dt><dt><span class="sect1"><a href="#idm73">Encoding (egif_lib.c)</a></span></dt><dt><span class="sect1"><a href="#idm84">Color map handling and allocation routines</a></span></dt><dt><span class="sect1"><a href="#idm94">Graphics control extension handling</a></span></dt><dt><span class="sect1"><a href="#idm99">Error Handling (gif_err.c)</a></span></dt><dt><span class="sect1"><a href="#idm103">The GIF Utility Font</a></span></dt><dt><span class="sect1"><a href="#idm116">Error codes</a></span></dt><dd><dl><dt><span class="sect2"><a href="#idm119">Encoding errors</a></span></dt><dt><span class="sect2"><a href="#idm172">Decoding errors</a></span></dt></dl></dd><dt><span class="sect1"><a href="#idm240">Utility support library</a></span></dt><dd><dl><dt><span class="sect2"><a href="#idm243">Error Handling</a></span></dt><dt><span class="sect2"><a href="#idm247">Command Line Parsing</a></span></dt></dl></dd><dt><span class="sect1"><a href="#sequential">Sequential access</a></span></dt><dd><dl><dt><span class="sect2"><a href="#idm260">Sequential reading</a></span></dt><dt><span class="sect2"><a href="#idm319">Sequential writing</a></span></dt></dl></dd><dt><span class="sect1"><a href="#compatibility">Forward and Backward Compatibility</a></span></dt><dd><dl><dt><span class="sect2"><a href="#idm389">General behavior</a></span></dt><dt><span class="sect2"><a href="#idm399">In documented functions:</a></span></dt><dt><span class="sect2"><a href="#idm412">In undocumented functions:</a></span></dt><dt><span class="sect2"><a href="#idm419">Error handling:</a></span></dt></dl></dd><dt><span class="sect1"><a href="#idm428">Skeletons of GIF filters</a></span></dt><dt><span class="sect1"><a href="#idm433">Unimplemented features</a></span></dt></dl></div><div class="sect1"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a id="idm16"></a>Introduction</h2></div></div></div><p>The Graphics Interchange Format(c) is the Copyright property of
6CompuServe Incorporated.  GIF(sm) is a Service Mark property of CompuServe
7Incorporated.</p><p>This document explains the GIF library code in directory `lib'.  The
8code is collected into a service library which is used in all the utilities in
9`util'.  It can be used in any application needs to read/write the GIF
10file format.  This document does <span class="emphasis"><em>not</em></span> explain the GIF file
11format and assumes you know it, at least to the level of the GIF file
12structure.</p></div><div class="sect1"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a id="idm21"></a>Credit and Blame</h2></div></div></div><p>Gershon wrote: "This library was written because I couldn't find
13anything similar and I wanted one.  I was inspired by the RLE Utah tool kit,
14which I hoped to port to an IBM PC, but found it to be too machine specific,
15and its compression ratio too low.  I compromised on the GIF format, but I am
16not sure how long 8 bits per pixel will be enough."</p><p>During his first spell of maintainership between 1989 and 1994, Eric
17S. Raymond (aka "ESR") ported the code to Unix, wrote the high-level
18DGIfSlurp()/EGifSpew() interface, rationalized the internal data
19structures, and did a lot of general cleanup and refactoring to
20improve the code quality.</p><p>Between 1994 and 2012 Toshio Kuratomi fixed various tool bugs,
21build-recipe problems and rare segfaults. He partially untangled the
22somewhat misdesigned extension-block handling in earlier versions.
23The core code was very stable during this period.</p><p>During his second spell of maintainership, ESR fixed the
24extension API, made the library re-entrant and thread-safe, wrote a
25regression-test suite, greatly improved the documentation, and
26discarded a lot of obsolete code.</p></div><div class="sect1"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a id="idm27"></a>The GIF descriptor</h2></div></div></div><p>When a GIF file is opened, a GIF file descriptor is created which
27is a pointer to GifFileType structure as follows:</p><pre class="programlisting">
28typedef struct GifFileType {
29    GifWord SWidth, SHeight;         /* Size of virtual canvas */
30    GifWord SColorResolution;        /* How many colors can we generate? */
31    GifWord SBackGroundColor;        /* Background color for virtual canvas */
32    GifByteType AspectByte;	     /* Used to compute pixel aspect ratio */
33    ColorMapObject *SColorMap;       /* Global colormap, NULL if nonexistent. */
34    int ImageCount;                  /* Number of current image (both APIs) */
35    GifImageDesc Image;              /* Current image (low-level API) */
36    SavedImage *SavedImages;         /* Image sequence (high-level API) */
37    int ExtensionBlockCount;         /* Count extensions past last image */
38    ExtensionBlock *ExtensionBlocks; /* Extensions past last image */
39    int Error;			     /* Last error condition reported */
40    void *UserData;                  /* hook to attach user data (TVT) */
41    void *Private;                   /* Don't mess with this! */
42} GifFileType;
43</pre><p>This structure was copied from gif_lib.h - the header file for the GIF
44library.  Any application program that uses the libgif.a library should
45include it.  Members beginning with S refer to the GIF screen; others hold
46properties of the current image (a GIF file may have more than one image)
47or point to allocated store used by various routines.</p><p>The user almost never writes into this structure (exception: it
48may occasionally be useful to alter things in the SavedImages array and
49Trailing member), but can read any of these items at any time it is
50valid (Image information is invalid until the first image has been read;
51read; SavedImages information is valid only after a DGifSlurp() call).</p><p>As the library needs to keep its own internal data, a Private pointer
52to hidden data is included. Applications should ignore this.</p><p>The library allocates its own memory dynamically, on opening of files,
53and releases that once closed.  The user is never required to allocate
54any memory for any of the functions of this library, and is almost never
55required to free them directly. The "almost" in the latter clause is because
56one manual free() call may be required on a failed file close; see the
57documentation of DGifClose() and EGifClose() for details.</p><p>Here is a module summary:</p><div class="variablelist"><dl class="variablelist"><dt><span class="term">egif_lib.c</span></dt><dd><p>Encoding routines, all prefixed with E.</p></dd><dt><span class="term">dgif_lib.c</span></dt><dd><p>Decoding routines, all prefixed with D.</p></dd><dt><span class="term">gifalloc.c</span></dt><dd><p>Routines for colormap handling and GIF record allocation.</p></dd><dt><span class="term">gif_font.c</span></dt><dd><p>The 8x8 font table for the GIF utility font.</p></dd></dl></div><p>The library includes a sixth file of hash-function code which is accessed
58internally only.</p><p>Most of the routines return GIF_ERROR (see gif_lib.h) if something
59went wrong, GIF_OK otherwise.  After an error return, all routines that
60take a pointer-to-GifFileType argument set the Error member with a code that
61can be interpreted into an explanatory string with the function
62GifErrorString() in gif_err.c. (The exception to this is the
63DGifClose() and EGifClose() routines, which deallocate that structure
64and must therefore return any error code through a pointer argument.)</p></div><div class="sect1"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a id="idm55"></a>Decoding (dgif_lib.c)</h2></div></div></div><p>The following functions are used to set up input from a GIF:</p><a id="DGifOpenFileName"></a><pre class="programlisting">
65GifFileType *DGifOpenFileName(char *GifFileName, int *ErrorCode)
66</pre><p>Open a new GIF file (in binary mode, if under Windows) using the
67given GifFileName, and read its Screen information.</p><p>If any error occurs, NULL is returned and ErrorCode is set (if
68non-NULL).</p><a id="DGifOpenFileHandle"></a><pre class="programlisting">
69GifFileType *DGifOpenFileHandle(int FileHandle, int *ErrorCode)
70</pre><p>Open a new GIF file using the given FileHandle, and read its Screen
71information.</p><p>If any error occurs, NULL is returned and ErrorCode is set (if
72non-NULL).</p><p>Once you have acquired a handle on a GIF, the high-level
73function</p><a id="DGifSlurp"></a><pre class="programlisting">
74int DGifSlurp(GifFileType)
75</pre><p>reads the rest of a complete (possibly multi-image) GIF file from the
76indicated file handle into in-core allocated structures.  It returns
77GIF_OK on success, GIF_ERROR on failure; on failure, the Error member
78will be set.</p><p>Once you have done this, all image, raster, and extension-block
79data in the GIF is accessable in the SavedImages member (see the
80structures in gif_lib.h).  When you have modified the image to taste,
81write it out with EGifSpew().</p><p>One detail that may not be clear from just looking at the
82structures is how extension blocks and sub-blocks are stored.  Each
83ExtensionBlock structure represents an extension data block.  Those
84with a zero function code represent continuation data blocks attached
85to previous blocks with nonzero function codes.</p><p>You can read from a GIF file through a function hook. Initialize
86with </p><a id="DGifOpen"></a><pre class="programlisting">
87GifFileType *DGifOpen(void *userPtr, InputFunc readFunc, int *ErrorCode)
88</pre><p>and see the library header file for the type of InputFunc.</p><p>There is also a set of deprecated functions for sequential I/O,
89described in a later section.</p></div><div class="sect1"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a id="idm73"></a>Encoding (egif_lib.c)</h2></div></div></div><p>The high-level function</p><a id="EGifSpew"></a><pre class="programlisting">
90int EGifSpew(GifFileType *GifFile)
91</pre><p>writes a complete (possibly multi-image) GIF file to the indicated file
92handle from in-core allocated structures created by a previous DGifSlurp()
93or equivalent operations.  Its argument is a GIF file descriptor, which
94imnplicitly points to storage previously allocated by DGifSlurp().</p><p>The file is written with a GIF87 stamp unless it contains one of the four
95special extension blocks defined in GIF89, in which case it is written
96with a GIF89 stamp.</p><p>EGifSpew() finishes by closing the GIF (writing a termination
97record to it) and deallocating the associated storage.</p><p>You can write to a GIF file through a function hook. Initialize
98with </p><a id="EGifOpen"></a><pre class="programlisting">
99GifFileType *EGifOpen(void *userPtr, OutputFunc writeFunc, int *ErrorCode)
100</pre><p>and see the library header file for the type of OutputFunc.</p><p>There is also a set of deprecated functions for sequential I/O,
101described in a later section.</p></div><div class="sect1"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a id="idm84"></a>Color map handling and allocation routines</h2></div></div></div><a id="GifMakeMapObject"></a><pre class="programlisting">
102ColorMapObject *GifMakeMapObject(int ColorCount, GifColorType *ColorMap)
103</pre><p>Allocate storage for a color map object with the given number of
104RGB triplet slots.  If the second argument is non-NULL, initialize
105the color table portion of the new map from it.  Returns NULL if
106memory is exhausted or if the size is not a power of 2 &lt;= 256.</p><a id="GifFreeMapObject"></a><pre class="programlisting">
107void GifFreeMapObject(ColorMapObject *Object)
108</pre><p>Free the storage occupied by a ColorMapObject that is no longer
109needed.</p><a id="GifUnionColorMap"></a><pre class="programlisting">
110ColorMapObject *GifUnionColorMap(
111        ColorMapObject *ColorIn1, ColorMapObject *ColorIn2,
112        GifPixelType ColorTransIn2[])
113</pre><p>Create the union of two given color maps and return it.  If the result
114won't fit into 256 colors, NULL is returned, the allocated union
115otherwise.  ColorIn1 is copied as it to ColorUnion, while colors from
116ColorIn2 are copied iff they didn't exist before.  ColorTransIn2 maps
117the old ColorIn2 into ColorUnion color map table.</p><a id="GifAttachImage"></a><pre class="programlisting">
118SavedImage *GifAttachImage(GifFileType *GifFile)
119</pre><p>Add an image block to the SavedImages array.  The image block is
120initially zeroed out.  This image block will be seen by any following
121EGifSpew() calls.</p></div><div class="sect1"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a id="idm94"></a>Graphics control extension handling</h2></div></div></div><p>GIF89 added a graphics control extension block, but versions
122of GIFLIB before 5.0 weren't much help in reading or modifying them.
123This lack has been remedied with the following structure and functions:</p><pre class="programlisting">
124typedef struct GraphicsControlBlock {
125    int DisposalMode;
126#define DISPOSAL_UNSPECIFIED      0       /* No disposal specified. */
127#define DISPOSE_DO_NOT            1       /* Leave image in place */
128#define DISPOSE_BACKGROUND        2       /* Set area too background color */
129#define DISPOSE_PREVIOUS          3       /* Restore to previous content */
130    bool UserInputFlag;      /* User confirmation required before disposal */
131    int DelayTime;           /* pre-display delay in 0.01sec units */
132    int TransparentColor;    /* Palette index for transparency, -1 if none */
133#define NO_TRANSPARENT_COLOR	-1
134} GraphicsControlBlock;
135
136int DGifSavedExtensionToGCB(GifFileType *GifFile,
137			    int ImageIndex,
138			    GraphicsControlBlock *GCB);
139int EGifGCBToSavedExtension(const GraphicsControlBlock *GCB,
140			    GifFileType *GifFile,
141			    int ImageIndex);
142</pre><p>With these functions you can extract the data from a graphics
143control extension associated with a saved image into a
144GraphicsControlBlock, modify it, and write it back out.  Note that if
145the specified saved image doesn't have a graphics control extension,
146DGifSavedExtensionToGCB() will fill the GCB with default values and
147return GIF_ERROR (which can be ignored); EGifGCBToSavedExtension()
148will create a new leading extension block.</p></div><div class="sect1"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a id="idm99"></a>Error Handling (gif_err.c)</h2></div></div></div><pre class="programlisting">
149int GifErrorString(int ErrCode)
150</pre><p>Returns a sting describing the specified GIFLIB error code.
151Return NULL if the argument is not a valid error code.</p></div><div class="sect1"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a id="idm103"></a>The GIF Utility Font</h2></div></div></div><p>The 8x8 utility font used in gifecho and gifcolor lives in the library
152module gif_font.c, in a table called GifAsciiTable.  The library header file
153includes suitable externs and defines.</p><p>The GIF utility font support includes entry points for drawing legends
154on in-core images, drawing boxes and rectangles, and boxing text.
155These entry points are as follows:</p><a id="GifDrawText"></a><pre class="programlisting">
156void GifDrawText8x8(
157        SavedImage *Image,
158        const int x, const int y,
159        const char *legend,
160        const int color)
161</pre><p>Draw text using the 8x8 utility font on the saved image.  Upper
162left corner of the text is at image pixel (x, y).  Use the specified
163color index.</p><a id="GifDrawBox"></a><pre class="programlisting">
164void GifDrawBox(SavedImage *Image,
165        const int x, const int y,
166        const int w, const int h,
167        const int color)
168</pre><p>Draw a box on the saved image.  Upper left corner of the box is at
169image pixels (x, y), width is w, height is h.  Use the specified color
170index.</p><a id="GifDrawRectangle"></a><pre class="programlisting">
171void GifDrawRectangle(SavedImage *Image,
172        const int x, const int y,
173        const int w, const int h,
174        const int color)
175</pre><p>Draw a (filled) rectangle on the saved image.  Upper left corner of
176the box is at image pixels (x, y), width is w, height is h.  Use the
177specified color index.</p><a id="GifDrawBoxedText"></a><pre class="programlisting">
178void GifDrawBoxedText8x8(SavedImage *Image,
179        const int x, const int y,
180        const char *legend,
181        const int border,
182        const int bg, const int fg)
183</pre><p>Draw text on a filled rectangle.  The rectangle will be sized to fit
184the text, with upper left hand corner at (x, y) on the saved image.
185The `border' argument specifies a pixel margin around the text.  The
186`bg' argument is the color table index to fill the rectangle with;
187`fg' is the color table index to draw the text with.</p><p>This function interprets some characters in the legend string
188specially.  A tab (\t) is interpreted as a command to center the
189following text in the box.  A carriage return (\r) is interpreted
190as a request for a line break.</p></div><div class="sect1"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a id="idm116"></a>Error codes</h2></div></div></div><p>Errors as reported from the GIFLIB library are divided to two major
191categories: the encoder (errors prefixed by E_GIF_ERR), and the
192decoder (errors prefixed by D_GIF_ERR).  This document explains them
193briefly.</p><div class="sect2"><div class="titlepage"><div><div><h3 class="title"><a id="idm119"></a>Encoding errors</h3></div></div></div><div class="variablelist"><dl class="variablelist"><dt><span class="term"><span class="errorname">E_GIF_ERR_OPEN_FAILED</span></span></dt><dd><p>Message printed using PrintGifError: "Failed to open given file"
194   IO error result when attempt to open the given GIF file.</p></dd><dt><span class="term"><span class="errorname">E_GIF_ERR_WRITE_FAILED</span></span></dt><dd><p>Message printed using PrintGifError: "Failed to Write to given file"
195   IO error result when attempt to write to the given GIF file.</p></dd><dt><span class="term"><span class="errorname">E_GIF_ERR_HAS_SCRN_DSCR</span></span></dt><dd><p>Message printed using PrintGifError: "Screen Descriptor
196   already been set" Attempt to write second screen descriptor to same
197   GIF file. GIF file should have exactly one screen descriptor which
198   should be set directly after the file is opened.</p></dd><dt><span class="term"><span class="errorname">E_GIF_ERR_HAS_IMAG_DSCR</span></span></dt><dd><p>Message printed using PrintGifError: "Image Descriptor is still active"
199   Image descriptor should be sent before and image dump, and no second
200   image descriptor should be sent before current image dump ended. This error
201   occurred probably because current image was not complete.</p></dd><dt><span class="term"><span class="errorname">E_GIF_ERR_NO_COLOR_MAP</span></span></dt><dd><p>Message printed using PrintGifError: "Neither Global Nor
202   Local color map" An image must have either global (screen) or local
203   (image) color map.  Neither were given in this case.</p></dd><dt><span class="term"><span class="errorname">E_GIF_ERR_DATA_TOO_BIG</span></span></dt><dd><p>Message printed using PrintGifError: "#Pixels bigger than
204   Width * Height" The number of pixels dumped for this image is
205   bigger than specified by image Height times image Width.</p></dd><dt><span class="term"><span class="errorname">E_GIF_ERR_NOT_ENOUGH_MEM</span></span></dt><dd><p>Message printed using PrintGifError: "Fail to allocate
206   required memory" Once an attemp is made to open GIF file, special
207   structures are allocated to hold internal data for it. If
208   allocation fails this error is returned.</p></dd><dt><span class="term"><span class="errorname">E_GIF_ERR_DISK_IS_FULL</span></span></dt><dd><p>Message printed using PrintGifError: "Write failed (disk full?)"
209   Writing encoded data failed.</p></dd><dt><span class="term"><span class="errorname">E_GIF_ERR_CLOSE_FAILED</span></span></dt><dd><p>Message printed using PrintGifError: "Failed to close given file"
210   Closing file failed.</p></dd><dt><span class="term"><span class="errorname"> E_GIF_ERR_NOT_WRITEABLE</span></span></dt><dd><p>Message printed using PrintGifError: "Given file was not
211   opened for write" GIF files can be opened both for read (DGIF part
212   of library) and write (EGIF part of library). This error occurs
213   when a file is opened for read (using DGIF) is given to one of the
214   encoding (EGIF) routines.</p></dd></dl></div></div><div class="sect2"><div class="titlepage"><div><div><h3 class="title"><a id="idm172"></a>Decoding errors</h3></div></div></div><div class="variablelist"><dl class="variablelist"><dt><span class="term"><span class="errorname">D_GIF_ERR_OPEN_FAILED</span></span></dt><dd><p>Message printed using PrintGifError: "Failed to open given file"
215   IO error result when attempt to open the given GIF file.</p></dd><dt><span class="term"><span class="errorname">D_GIF_ERR_READ_FAILED</span></span></dt><dd><p>Message printed using PrintGifError: "Failed to read from given file"
216   IO error result when attempt to write to the given GIF file.</p></dd><dt><span class="term"><span class="errorname">D_GIF_ERR_NOT_GIF_FILE</span></span></dt><dd><p>Message printed using PrintGifError: "Data is not a GIF file"
217   GIF files should have special stamp identifies them as such, If that stamp
218   is not found, this error is issued.</p></dd><dt><span class="term"><span class="errorname">D_GIF_ERR_NO_SCRN_DSCR</span></span></dt><dd><p>Message printed using PrintGifError: "No screen descriptor detected"
219   Each GIF file should have screen descriptor in its header. This error will
220   be generated if no such descriptor was found.</p></dd><dt><span class="term"><span class="errorname">D_GIF_ERR_NO_IMAG_DSCR</span></span></dt><dd><p>Message printed using PrintGifError: "No image descriptor detected"
221   Each image should have image descriptor in its header. This error will
222   be generated if no such descriptor was found.</p></dd><dt><span class="term"><span class="errorname">D_GIF_ERR_NO_COLOR_MAP</span></span></dt><dd><p>Message printed using PrintGifError: "Neither global nor
223   local color map" An image must have either global (screen) or local
224   (image) color map.  Neither were given in this case.</p></dd><dt><span class="term"><span class="errorname">D_GIF_ERR_WRONG_RECORD</span></span></dt><dd><p>Message printed using PrintGifError: "Wrong record type detected"
225   Each record in a GIF file has a special identifier in its header. If the
226   record has an unrecognized identifier, this error is generated.</p></dd><dt><span class="term"><span class="errorname">D_GIF_ERR_DATA_TOO_BIG</span></span></dt><dd><p>Message printed using PrintGifError: "Number of pixels bigger than
227   width * height" The number of pixels dumped for this image is
228   bigger than specified by image Height times image Width.</p></dd><dt><span class="term"><span class="errorname">D_GIF_ERR_NOT_ENOUGH_MEM</span></span></dt><dd><p>Message printed using PrintGifError: "Failed to allocate
229   required memory" Once an attemp is made to open GIF file, special
230   structures are allocated to hold internal data for it. If
231   allocation fails this error is returned.</p></dd><dt><span class="term"><span class="errorname">D_GIF_ERR_CLOSE_FAILED</span></span></dt><dd><p>Message printed using PrintGifError: "Failed to close given file"
232   Closing file failed.</p></dd><dt><span class="term"><span class="errorname">D_GIF_ERR_NOT_READABLE</span></span></dt><dd><p>Message printed using PrintGifError: "Given file was not
233   opened for read" GIF files can be opened both for read (DGIF part
234   of library) and write (EGIF part of library). This error occurs
235   when a file is opened for write (using EGIF) is given to one of the
236   decoding (DGIF) routines.</p></dd><dt><span class="term"><span class="errorname">D_GIF_ERR_IMAGE_DEFECT</span></span></dt><dd><p>Message printed using PrintGifError: "Image is defective,
237   decoding aborted" This error is generated, once the decoding failed
238   - probably image is defect.</p></dd><dt><span class="term"><span class="errorname">D_GIF_ERR_EOF_TOO_SOON</span></span></dt><dd><p>Message printed using PrintGifError: "Image EOF detected,
239   before image complete" This error is generated once EOF errorname
240   is detected in encoded image before all the pixels (Width *
241   Height) has be decoded.</p></dd></dl></div></div></div><div class="sect1"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a id="idm240"></a>Utility support library</h2></div></div></div><p>These functions are not part of the core GIF library.  They are part
242of the getarg library that supports the utilities.</p><div class="sect2"><div class="titlepage"><div><div><h3 class="title"><a id="idm243"></a>Error Handling</h3></div></div></div><a id="PrintGifError"></a><pre class="programlisting">
243void PrintGifError(void)
244</pre><p>Print a one-line diagnostic on the last giflib error to stderr.</p></div><div class="sect2"><div class="titlepage"><div><div><h3 class="title"><a id="idm247"></a>Command Line Parsing</h3></div></div></div><a id="GAGetArgs"></a><pre class="programlisting">
245bool GAGetArgs(int argc, char **argv, char *CtrlStr, ...)
246</pre><p>Main routine of this module.  Given argc &amp; argv as received by
247the main procedure, the command line CtrlStr, and the addresses of
248all parameters, parse the command line, and update the parameters.</p><p>The CtrlStr defines what types of variables should follow. Look at the
249beginning of getarg.c for exact usage.</p><p>Returns false if successful, returns true on failure.</p><a id="GAPrintErrMsg"></a><pre class="programlisting">
250void GAPrintErrMsg(int Error)
251</pre><p>If an error occurred in GAGetARgs, this routine may be used to print
252one line diagnostic to stderr.</p><a id="GAPrintHowTo"></a><pre class="programlisting">
253void GAPrintHowTo(char *CtrlStr)
254</pre><p>Given the same CtrlStr as for GAGetArgs, can be used to print a one line
255'how to use'. </p></div></div><div class="sect1"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a id="sequential"></a>Sequential access</h2></div></div></div><p>If you are handling large images on an extremely memory-limited
256machine, you may need to use the following functions for sequential
257read and write. It's better to avoid them and use the simpler
258DGifSlurp()/EGifSpew() interface.</p><div class="sect2"><div class="titlepage"><div><div><h3 class="title"><a id="idm260"></a>Sequential reading</h3></div></div></div><a id="DGifGetScreenDesc"></a><pre class="programlisting">
259int DGifGetScreenDesc(GifFileType *GifFile)
260</pre><p>Reads the screen information into the GifFile structure. Note this
261routine is automatically called once a file is opened, and therefore
262usually need not be called explicitly.</p><p>Returns GIF_ERROR if something went wrong, GIF_OK otherwise.</p><a id="DGifGetRecordType"></a><pre class="programlisting">
263int DGifGetRecordType(GifFileType *GifFile, GifRecordType *GifType)
264</pre><p>As the GIF file can have different records in arbitrary order, this
265routine should be called once the file was open to detect the next
266record type, and act upon it.  It can return these types in GifType:</p><div class="variablelist"><dl class="variablelist"><dt><span class="term">1. UndefinedRecordType </span></dt><dd><p>something is wrong!</p></dd><dt><span class="term">2. ScreenDescRecordType </span></dt><dd><p>screen information.  As the screen info is automatically read in when the file is open, this should not happen.</p></dd><dt><span class="term">3. ImageDescRecordType </span></dt><dd><p> next record is an image descriptor.</p></dd><dt><span class="term">4. ExtensionRecordType</span></dt><dd><p> next record is extension block.</p></dd><dt><span class="term">5. TrailerRecordType</span></dt><dd><p>last record reached, can close the file.</p></dd></dl></div><p>The first two types can usually be ignored. The function
267returns GIF_ERROR if something went wrong, GIF_OK otherwise.</p><a id="DGifGetImageDesc"></a><pre class="programlisting">
268int DGifGetImageDesc(GifFileType *GifFile)
269</pre><p>Reads image information into the GifFile structure.
270Returns GIF_ERROR if something went wrong, GIF_OK otherwise.</p><a id="DGifGetLine"></a><pre class="programlisting">
271int DGifGetLine(GifFileType *GifFile, PixelType *GifLine, int GifLineLen)
272</pre><p>Load a block of pixels from the GIF file.  The line can be
273of any length.  More than that, this routine may be interleaved with
274DGifGetPixel until all pixels have been read.</p><p>Returns GIF_ERROR if something went wrong, GIF_OK otherwise.</p><pre class="programlisting">
275int DGifGetPixel(GifFileType *GifFile, PixelType GifPixel)
276</pre><p>Loads one pixel from the GIF file.  This routine may be interleaved
277with <a class="link" href="#DGifGetLine">DGifGetLine()</a>, until all pixels are
278read.  Because of the overhead per each call, use of this routine is
279not recommended.</p><p>Returns GIF_ERROR if something went wrong, GIF_OK otherwise.</p><pre class="programlisting">
280int DGifGetExtension(
281        GifFileType *GifFile,
282        int *GifExtCode,
283        ByteType **GifExtension)
284</pre><p>Loads an extension block from the GIF file.  Extension blocks
285are optional in GIF files.  This routine should be followed by
286<a class="link" href="#DGifGetExtensionNext">DGifGetExtensionNext</a>.</p><p>Returns GIF_ERROR if something went wrong, GIF_OK otherwise.</p><p><a id="DGifGetExtensionNext"></a></p><pre class="programlisting">
287int DGifGetExtensionNext(GifFileType *GifFile, ByteType **GifExtension)
288</pre><p>
289
290As extensions may contain more than one block, use this routine to
291continue after DGifGetExtension, until *GifExtension is NULL.</p><p>Returns GIF_ERROR if something went wrong, GIF_OK otherwise.</p><pre class="programlisting">
292int DGifGetCode(
293        GifFileType *GifFile,
294        int *GifCodeSize, ByteType **GifCodeBlock)
295</pre><p>It sometimes may be desired to read the compressed code as is
296without decoding it.  This routine does exactly that (with
297DGifGetCodeNext), and can be used instead of DGifGetLine.</p><p>This compressed code information can be written out using the
298EGifPutCode/EGifPutCodeNext sequence (see gifpos.c for example).
299Returns GIF_ERROR if something went wrong, GIF_OK otherwise.</p><a id="DGifGetCodeNext"></a><pre class="programlisting">
300int DGifGetCodeNext(GifFileType *GifFile, ByteType **GifCodeBlock)
301</pre><p>See DGifGetCode above.</p><a id="DGifGetLZCodes"></a><pre class="programlisting">
302int DGifGetLZCodes(GifFileType *GifFile, int *GifCode)
303</pre><p>This routine can be called instead of DGifGetLine/DGifGetPixel or
304DGifGetCode/DGifGetCodeNext to get the 12 bits LZ codes of the images.
305It will be used mainly for debugging purposes (see GifText.c for
306example).</p><p>Returns GIF_ERROR if something went wrong, GIF_OK otherwise.</p><a id="DGifCloseFile"></a><pre class="programlisting">
307int DGifCloseFile(GifFileType *GifFile, int *ErrorCode)
308</pre><p>Write a termination block to the GIF, close the GIF file and
309free all memory allocated for managing it. GifFile should not be used after
310this routine has been called.</p><p>Returns GIF_ERROR if something went wrong, GIF_OK otherwise. When
311GIF_ERROR is returned, the diagnostic error code is left in ErrorCode.
312The GifFile structure is unconditionally freed.</p><p>(Note: In versions before 5.1.0, the ErrorCode argument was
313absent and the GifFile structure was not freed so that the
314diagnostic error code will remain accessible in GifFile-&gt;Error.
315This behavior was changed because it caused problems for the
316implementation of library wrappers in dynamic languages.)</p><a id="DGetGifVersion"></a><pre class="programlisting">
317const char * DGifGetGifVersion(GifFileType *GifFile)
318</pre><p>Get the GIF version collected during sequential read. This is
319handy for sequential API users who want to set an encoder's version
320from a decoder (e.g. for gif resizing). For the all-at-once users this
321isn't necessary because gif encoder inspects all the extension blocks,
322but sequential users do not have that luxury.</p></div><div class="sect2"><div class="titlepage"><div><div><h3 class="title"><a id="idm319"></a>Sequential writing</h3></div></div></div><p>If you are handling large images on a memory-limited machine, you may need
323to use the following functions for sequential write.</p><a id="EGifOpenFileName"></a><pre class="programlisting">
324GifFileType *EGifOpenFileName(char *GifFileName, bool GifTestExistance, int *ErrorCode)
325</pre><p>Open a new GIF file using the given GifFileName (in binary mode,
326if under Windows).  If GifTestExistance is TRUE, and file exists, the
327file is not destroyed, and NULL returned.</p><p>If any error occurs, NULL is returned and the ErrorCode is set.</p><a id="EGifOpenFileHandle"></a><pre class="programlisting">
328GifFileType *EGifOpenFileHandle(int GifFileHandle, int *ErrorCode)
329</pre><p>Open a new GIF file using the given GifFileHandle.</p><p>If any error occurs, NULL is returned and ErrorCode is set.</p><p>The file is opened in binary mode, and its buffer size is set to
330FILE_BUFFER_SIZE bytes.</p><pre class="programlisting">
331char *EGifGetGifVersion(GifFileType *GifFile)
332</pre><p>That version computation is available through this function.</p><a id="EGifSetGifVersion"></a><pre class="programlisting">
333void EGifSetGifVersion(GifFileType *GifFile, bool gif89)
334</pre><p>Set the GIF type, to GIF89 if the argument is true and GIF87 if
335it is false.  The default type is GIF87. This function may be called
336aftert the GifFile record is allocated but before
337EGifPutScreenDesc().</p><pre class="programlisting">
338int EGifPutScreenDesc(GifFileType *GifFile,
339        const int GifWidth, const GifHeight,
340        const int GifColorRes, const int GifBackGround,
341        ColorMapObject *GifColorMap)
342</pre><p>Update the GifFile Screen parameters, in GifFile structure and in
343the real file.  If error occurs, returns GIF_ERROR (see gif_lib.h),
344otherwise GIF_OK.</p><p>This routine should be called immediately after the GIF file was
345opened.</p><pre class="programlisting">
346int EGifPutImageDesc(GifFileType *GifFile,
347        const int GifLeft, const int GifTop,
348        const int GifWidth, const GifHeight,
349        const bool GifInterlace,
350        ColorMapObject *GifColorMap)
351</pre><p>Update GifFile Image parameters, in GifFile structure and in the real
352file. if error occurs returns GIF_ERROR (see gif_lib.h), otherwise
353GIF_OK.</p><p>This routine should be called each time a new image must be
354dumped to the file.</p><a id="EGifPutLine"></a><pre class="programlisting">
355int EGifPutLine(GifFileType *GifFile, PixelType *GifLine, int GifLineLen)
356</pre><p>Dumps a block of pixels out to the GIF file.  The slab can be of
357any length.  More than that, this routine may be interleaved with
358<a class="link" href="#EGifPutPixel">EGifPutPixel()</a>, until all pixels
359have been sent.</p><p>Returns GIF_ERROR if something went wrong, GIF_OK otherwise.</p><a id="EGifPutPixel"></a><pre class="programlisting">
360int EGifPutPixel(GifFileType *GifFile, const PixelType GifPixel)
361</pre><p>Dumps one pixel to the GIF file.  This routine may be interleaved with
362<a class="link" href="#EGifPutLine">EGifPutLine()</a>, until all pixels were sent.
363Because of the overhead for each call, use of this routine is not
364recommended.</p><p>Returns GIF_ERROR if something went wrong, GIF_OK otherwise.</p><a id="EGifPutComment"></a><pre class="programlisting">
365int EGifPutComment(GifFileType *GifFile, char *GifComment)
366</pre><p>Uses extension GIF records to save a string as a comment is the file.
367The extension code is 'C' (for Comment).</p><p>Returns GIF_ERROR if something went wrong, GIF_OK otherwise.</p><a id="EGifPutExtension"></a><pre class="programlisting">
368int EGifPutExtension(
369        GifFileType *GifFile,
370        const int GifExtCode,
371        const int GifExtLen,
372        void *GifExtension)
373</pre><p>Dumps the given extension block into the GIF file. Extension blocks
374are optional in GIF file. Extension blocks of more than 255 bytes or
375more than one block are not supported in this function.  Please use
376EGifPutExtensionFirst, EGifPutExtensionBlock, and EGifPutExtensionTrailer
377if your extension blocks may fall into this category.</p><p>Returns GIF_ERROR if something went wrong, GIF_OK otherwise.</p><a id="EGifPutExtensionLeader"></a><pre class="programlisting">
378int EGifPutExtensionLeader(
379        GifFileType * GifFile,
380        const int GifExtCode)
381</pre><p>Dumps the beginning of a GIF extension block to a GIF file.
382Extension blocks are optional in GIF files.  This function outputs the
383type code information necessary for a GIF extension block.</p><p>Further blocks of the GIF Extension should be dumped using
384EGifPutExtensionBlock.  When finished with this extension block,
385EGifPutExtensionTrailer should be called to output the block termination.</p><p>Returns GIF_ERROR if something went wrong, GIF_OK otherwise.</p><a id="EGifPutExtensionBlock"></a><pre class="programlisting">
386int EGifPutExtensionBlock(
387        GifFileType * GifFile,
388        const int GifExtLen,
389        const VoidPtr GifExtension)
390</pre><p>Dumps a subblock of a GIF extension to a GIF File; should be
391used only following an initializing call to EGifPutExtensionLeader().
392Extension blocks are optional in GIF files.  This function will write
393the Extension Data in GifExtension to the file as a subblock of the
394preceding Extension Block.  Repeat calling of this function until all
395data subblocks have been output.</p><p>Note that EGifPutExtensionLeader needs to be called before any
396calls to this function.  EGifPutExtensionTrailer should be called to
397finish the Extension block after all data subblocks have been
398output.</p><p>Returns GIF_ERROR if something went wrong, GIF_OK otherwise.</p><a id="EGifPutExtensionTrailer"></a><pre class="programlisting">
399int EGifPutExtensionTrailer(
400        GifFileType * GifFile,
401        const VoidPtr GifExtension)
402</pre><p>Dumps the GIF extension block terminator to a GIF File to end
403the current Extension block.</p><p>Note that a call to EGifPutExtensionLeader is needed to open the GIF
404Extension Block prior to calling this function.</p><p>Returns GIF_ERROR if something went wrong, GIF_OK otherwise.</p><a id="EGifPutCode"></a><pre class="programlisting">
405int EGifPutCode(
406        GifFileType *GifFile,
407        int *GifCodeSize,
408        ByteType **GifCodeBlock)
409</pre><p>It sometimes may be desired to write the compressed code as is
410without decoding it.  For example a filter for a GIF file that change
411only screen size (GifPos), does not need the exact pixel values.
412Piping out the compressed image as is makes this process much
413faster.</p><p>This routine does exactly that (with EGifPutCodeNext), and can be
414used instead of EGifPutLine.  You'll usually use this with the
415DGifGetCode/DgifGetCodeNext routines, which reads the compressed
416code, while EGifPutCode/EGifPutCodeNext write it out.  See gifpos.c
417for example.</p><p>Returns GIF_ERROR if something went wrong, GIF_OK otherwise.</p><a id="EGifPutCodeNext"></a><pre class="programlisting">
418int EGifPutCodeNext(GifFileType *GifFile, ByteType **GifCodeBlock)
419</pre><p>See EGifPutCode above.</p><a id="EGifCloseFile"></a><pre class="programlisting">
420int EGifCloseFile(GifFileType *GifFile)
421</pre><p>Write a termination block to the GIF, close the GIF file, and
422free all memory allocated for managing it. GifFile should not be used
423after this routine has been called.</p><p>Returns GIF_ERROR if something went wrong, GIF_OK otherwise. When
424GIF_ERROR is returned, the diagnostic error code is left in ErrorCode.
425The GifFile structure is unconditionally freed.</p><p>(Note: In versions before 5.1.0, the ErrorCode argument was
426absent and the GifFile structure was not freed so that the
427diagnostic error code will remain accessible in GifFile-&gt;Error.
428This behavior was changed because it caused problems for the
429implementation of library wrappers in dynamic languages.)</p></div></div><div class="sect1"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a id="compatibility"></a>Forward and Backward Compatibility</h2></div></div></div><p>Except for some details of extension-block handling and the addition
430of read/write function hooks, the DGifSlurp()/EGifSpew() interface has
431been stable since 1990.  It is expected to remain so.</p><p>However, the signatures of the file-opener functions were changed in 5.0
432in order to make the library fully reentrant and thread-safe - earlier library
433versions did not feature the final pointer-to-error-code argument in
434DGifOpen() and friends.  For the same reason, the static storage queried by
435GifLastError() in older versions is gone, and that function abolished.</p><p>The library header contains some version #defines you can use if you
436need to condition your code so it can compile with different library
437versions</p><p>Versions up to 4.1.6 defined a GIF_LIB_VERSION macro that was
438string-valued with a tricky format to parse.  This macro has been
439retired.</p><p>Versions after 4.1.6 define integer-valued GIFLIB_MAJOR, GIFLIB_MINOR,
440and GIFLIB_RELEASE macros for the three components of the version. See the
441NEWS file in the GIFLIB distribution to track API changes.</p><p>The following functions are entirely new:</p><div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem"><p>New functions DGifSavedExtensionToGCB() and
442EGifGCBToSavedExtension() make it easy to read and edit GIF89 graphics
443control blocks in saved images.</p></li><li class="listitem"><p>The new function DGifGetGifVersion() is convenient
444for people doing sequential reads.</p></li></ul></div><p>A few changes in behavior were introduced in 5.0:</p><div class="sect2"><div class="titlepage"><div><div><h3 class="title"><a id="idm389"></a>General behavior</h3></div></div></div><div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem"><p>The library is now fully re-entrant and
445thread-safe.</p></li></ul></div><div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem"><p>  All functions exported by this library now have DGif,
446EGif, or Gif as a name prefix.</p></li><li class="listitem"><p>The default GIF version to write is now computed at
447write time from the types of an image's extension blocks. (Formerly
448EGifSpew() behaved this way, but the sequential-writing code didn't.)
449The result of this computation is available through the new function
450EGifGetGifVersion().</p></li></ul></div></div><div class="sect2"><div class="titlepage"><div><div><h3 class="title"><a id="idm399"></a>In documented functions:</h3></div></div></div><div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem"><p>GIF file openers and closers - DGifOpenFileName(),
451DGifOpenFileHandle(), DGifOpen(), DGifClose(), EGifOpenFileName(),
452EGifOpenFileHandle(), EGifOpen(), and EGifClose() - all now take a
453final integer address argument.  If non-null, this is used to pass
454back an error code when the function returns NULL.</p></li><li class="listitem"><p>EGifSlurp() and EGifSpew() read and write
455extension blocks trailing after the last image, handle interlaced
456images properly.</p></li><li class="listitem"><p>EGifPutExtensionFirst() has been replaced by
457EGifPutExtensionLeader(); the difference is the new function doesn't
458take an optional block, it just writes a block
459leader.</p></li><li class="listitem"><p>EGifPutExtensionNext() has been replaced by
460EGifPutExtensionBlock(); the difference is that the new function does
461not take and then discard a function code argument.</p></li><li class="listitem"><p>EGifPutExtensionLast() has been replaced by
462EGifPutExtensionTrailer(); all it does is write the terminator
463block.  Split your old EGifPutExtensionLast() calls into
464EGifPutExtensionBlock() followed by
465EGifPutExtensionTrailer().</p></li></ul></div></div><div class="sect2"><div class="titlepage"><div><div><h3 class="title"><a id="idm412"></a>In undocumented functions:</h3></div></div></div><div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem"><p>Some undocumented functions have been renamed.
466AddExtensionBlock() is now GifAddExtensionBlock(), and takes an additional
467function code argument.  ApplyTranslation() is now GifApplyTranslation();
468FreeExtension() has become GifFreeExtensions() and takes a different argument
469type; MakeSavedImage() is now GifMakeSavedImage(), FreeSavedImages() is
470now GifFreeSavedImages(), and BitSize() is now GifBitSize().</p></li><li class="listitem"><p>Three documented functions - MakeMapObject(),
471FreeMapObject(), and UnionColorMap() - have been renamed to
472GifMakeMapObject(), GifFreeMapObject(), and GifUnionColorMap()
473respectively.</p></li></ul></div></div><div class="sect2"><div class="titlepage"><div><div><h3 class="title"><a id="idm419"></a>Error handling:</h3></div></div></div><div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem"><p>Library error handling no longer uses a static cell to
474store the last error code registered; that made the library
475thread-unsafe.</p></li><li class="listitem"><p>For functions other than GIF file openers, the Error
476code is now put in an Error member of the GifFileType
477structure.</p></li><li class="listitem"><p>The GifError() and
478GifLastError() functions that referenced that static cell are gone,
479and the GifErrorString() function introduced in the 4.2 release now
480takes an explicit error code argument.</p></li></ul></div></div></div><div class="sect1"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a id="idm428"></a>Skeletons of GIF filters</h2></div></div></div><p>If you are developing on a virtual-memory OS such as most flavors of
481UNIX, or are otherwise sure of having enough memory to keep all of GIFs you
482need to operate in core, writing a filter is trivial.   See the file
483gifsponge.c in util.</p><p>A sequential filter skeleton will usually look like the example file
484giffilter.c in util.</p><p>Please look at the utilities in the util directory for more ideas once
485you feel comfortable with these skeletons.  Also try to follow the coding
486standards of this package if you want the maintainer to officially add your new
487utility to it.</p></div><div class="sect1"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a id="idm433"></a>Unimplemented features</h2></div></div></div><p>Some features of the original GIF specification have not stood the
488test of time.  This library mostly ignores them, but they are described
489here for completeness.</p><p>The GIF standard fails to be explicit about a small but crucial detail:
490the unsigned two-byte integer fields in it are little-endian.</p><p>The GIF format seems to have been designed with the idea that viewers
491would render multiple images in a GIF on a common canvas, giving an effect like
492a picture wall.  The 'logical screen descriptor block' (LSDB), 6 bytes right
493after the 6-byte GIF stamp and version header at the beginning of a
494GIF file, includes both two-byte canvas width and canvas height
495fields and a canvas background color.  Each image, besides height and
496width, also has 'left' and 'top' cordinates specifying where it is to
497be placed on the canvas.</p><p>GIFLIB can read and set these fields; the gifpos and giftool
498utilities will enable you to script such changes.  But browsers and
499modern image viewers ignore them. Nowadays multiple-image GIFs are
500generally used either as animations in which each sub-image is a frame
501or as image libraries, with the GIF client handling compositing into
502some canvas about which the GIF format holds no information.</p><p>Another feature of the LSDB that is generally ignored is the
503pixel aspect ratio byte.  Until 5.0, GIFLIB ignored this flag on input
504and zeroed it on output; now it is read and preserved if present.  The
505GIF standard doesn't give a rationale for it, but it seems likely that
506the designers intended it for representing image captures from the
507analog television of the day, which had rectangular pixel-equivalents.</p><p>Yet another ignored feature of both the LSDB and sub-images is
508the sort flag, which is supposed to signal whether the colors in the
509associated color map are sorted by decreasing importance in case the
510display device can only render a limited number of them.  This feature
511reflected the high cost of dual-port memory at the time the GIF
512specification was written in the late 1980s.  That kind of limit
513disappeared in the mid-1990s.  Until 5.0, GIFLIB ignored this flag on
514input and zeroed it on output; now it is read and preserved if
515present.</p><p>Finally, the plaintext extension block. This is an extension block
516that contains instructions for overlaying text captions on a following image.
517GIFLIB treats these blocks as raw data, not attempting to parse out the
518location and text data.</p></div></div></body></html>