On this page:

11 GIF File Writing

 (require file/gif) package: draw-lib

The file/gif library provides functions for writing GIF files to a stream, including GIF files with multiple images and controls (such as animated GIFs).

A GIF stream is created by gif-start, and then individual images are written with gif-add-image. Optionally, gif-add-control inserts instructions for rendering the images. The gif-end function ends the GIF stream.

A GIF stream can be in any one of the following states:


(gif-stream? v)  boolean?

  v : any/c
Returns #t if v is a GIF stream created by gif-write, #f otherwise.


(image-ready-gif-stream? v)  boolean?

  v : any/c
Returns #t if v is a GIF stream that is not in 'done mode, #f otherwise.


(image-or-control-ready-gif-stream? v)  boolean?

  v : any/c
Returns #t if v is a GIF stream that is in 'init or 'image-or-control mode, #f otherwise.


(empty-gif-stream? v)  boolean?

  v : any/c
Returns #t if v is a GIF stream that in 'init mode, #f otherwise.


(gif-colormap? v)  boolean?

  v : any/c
Returns #t if v represets a colormap, #f otherwise. A colormap is a list whose size is a power of 2 between 21 and 28, and whose elements are vectors of size 3 containing colors (i.e., exact integers between 0 and 255 inclusive).


(color? v)  boolean?

  v : any/c
The same as byte?.


(dimension? v)  boolean?

  v : any/c
Returns #t if v is an exact integer between 0 and 65535 inclusive, #f otherwise.


(gif-state stream)  symbol?

  stream : gif-stream?
Returns the state of stream.


(gif-start out w h bg-color cmap)  gif-stream?

  out : output-port?
  w : dimension?
  h : dimension?
  bg-color : color?
  cmap : (or/c gif-colormap? #f)
Writes the start of a GIF file to the given output port, and returns a GIF stream that adds to the output port.

The width and height determine a virtual space for the overall GIF image. Individual images added to the GIF stream must fit within this virtual space. The space is initialized by the given background color.

Finally, the default meaning of color numbers (such as the background color) is determined by the given colormap, but individual images within the GIF file can have their own colormaps.

A global colormap need not be supplied, in which case a colormap must be supplied for each image. Beware that bg-color is ill-defined if a global colormap is not provided.


(gif-add-image stream    
  bstr)  void?
  stream : image-ready-gif-stream?
  left : dimension?
  top : dimension?
  width : dimension?
  height : dimension?
  interlaced? : any/c
  cmap : (or/c gif-colormap? #f)
  bstr : bytes?
Writes an image to the given GIF stream. The left, top, width, and height values specify the location and size of the image within the overall GIF image’s virtual space.

If interlaced? is true, then bstr should provide bytes ininterlaced order instead of top-to-bottom order. Interlaced order is:

If a global color is provided with gif-start, a #f value can be provided for cmap.

The bstr argument specifies the pixel content of the image. Each byte specifies a color (i.e., an index in the colormap). Each row is provided left-to-right, and the rows provided either top-to-bottom or in interlaced order (see above). If the image is prefixed with a control that specifies an transparent index (see gif-add-control), then the corresponding “color” doesn’t draw into the overall GIF image.

An exception is raised if any byte value in bstr is larger than the colormap’s length, if the bstr length is not width times height, or if the top, left, width, and height dimensions specify a region beyond the overall GIF image’s virtual space.


(gif-add-control stream    
  transparent)  void?
  stream : image-or-control-ready-gif-stream?
  disposal : (or/c 'any 'keep 'restore-bg 'restore-prev)
  wait-for-input? : any/c
  delay : dimension?
  transparent : (or/c color? #f)
Writes an image-control command to a GIF stream. Such a control must appear just before an image, and it applies to the following image.

The GIF image model involves processing images one by one, placing each image into the specified position within the overall image’s virtual space. An image-control command can specify a delay before an image is added (to create animated GIFs), and it also specifies how the image should be kept or removed from the overall image before proceeding to the next one (also for GIF animation).

The disposal argument specifies how to proceed:

If wait-for-input? is true, then the display program may wait for some cue from the user (perhaps a mouse click) before adding the image.

The delay argument specifies a delay in 1/100s of a second.

If the transparent argument is a color, then it determines an index that is used to represent transparent pixels in the follow image (as opposed to the color specified by the colormap for the index).

An exception is raised if a control is already added to stream without a corresponding image.


(gif-add-loop-control stream iteration)  void?

  stream : empty-gif-stream?
  iteration : dimension?
Writes a control command to a GIF stream for which no images or other commands have already been written. The command causes the animating sequence of images in the GIF to be repeated ‘iteration-dimension’ times, where 0 can be used to mean “infinity.”

An exception is raise if some control or image has been added to the stream already.


(gif-add-comment stream bstr)  void?

  stream : image-or-control-ready-gif-stream?
  bstr : bytes?
Adds a generic comment to the GIF stream.

An exception is raised if an image-control command was just written to the stream (so that an image is required next).


(gif-end stream)  void?

  stream : image-or-control-ready-gif-stream?
Finishes writing a GIF file. The GIF stream’s output port is not automatically closed.

An exception is raised if an image-control command was just written to the stream (so that an image is required next).


(quantize bstr)  
bytes? gif-colormap? (or/c color? #f)
  bstr : 
(and/c bytes?
       (lambda (bstr)
         (zero? (remainder (bytes-length bstr) 4))))
Each image in a GIF stream is limited to 256 colors, including the transparent “color,” if any. The quantize function converts a 24-bit image (plus alpha channel) into an indexed-color image, reducing the number of colors if necessary.

Given a set of pixels expressed in ARGB format (i.e., each four bytes is a set of values for one pixel: alpha, red, blue, and green), quantize produces produces

The conversion treats alpha values less than 128 as transparent pixels, and other alpha values as solid.

The quantization process uses Octrees [Gervautz1990] to construct an adaptive palette for all (non-transparent) colors in the image. This implementation is based on an article by Dean Clark [Clark1996].

To convert a collection of images all with the same quantization, simply append them for the input of a single call of quantize, and then break apart the result bytes.