▼ |  | File: Racket File Format Libraries

1 | Convertible: Data-Conversion Protocol
2 | gzip Compression and File Creation
3 | gzip Decompression
4 | zip File Creation
5 | zip File Extraction
6 | tar File Creation
7 | tar File Extraction
8 | tar+gzip File Extraction
9 | MD5 Message Digest
10 | SHA1 Message Digest
11 | GIF File Writing
12 | ICO File Reading and Writing
13 | Windows Registry
| Bibliography
| Index

On this page:

gif-stream?
image-ready-gif-stream?
image-or-control-ready-gif-stream?
empty-gif-stream?
gif-colormap?
color?
dimension?
gif-state
gif-start
gif-add-image
gif-add-control
gif-add-loop-control
gif-add-comment
gif-end
quantize

top ← prev up next →

### 11`` GIF File Writing

(require file/gif)

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:

- 'init : no images or controls have been added to the stream

- 'image-or-control : another image or control can be written

- 'image : another image can be written (but not a control, since a control was written)

- 'done : nothing more can be added

> > procedure

> > (gif-stream? v) → boolean?

> v : any/c

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

> > procedure

> > (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.

> > procedure

> > (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.

> > procedure

> > (empty-gif-stream? v) → boolean?

> v : any/c

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

> > procedure

> > (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 2 1 and 2 8, and whose elements are vectors of size 3 containing colors (i.e., exact integers between 0 and 255 inclusive).

> > procedure

> > (color? v) → boolean?

> v : any/c

The same as byte?.

> > procedure

> > (dimension? v) → boolean?

> v : any/c

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

> > procedure

> > (gif-state stream) → symbol?

> stream : gif-stream?

Returns the state of stream.

> > procedure

> > (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 the bg-color is ill-defined if a global colormap is not provided.

> > procedure

> > (gif-add-image |  | stream |  |  |  |
> > |  | left |  |  |  |
> > |  | top |  |  |  |
> > |  | width |  |  |  |
> > |  | height |  |  |  |
> > |  | interlaced? |  |  |  |
> > |  | cmap |  |  |  |
> > |  | 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:

- every 8th row, starting with 0

- every 8th row, starting with 4

- every 4th row, starting with 2

- every 2nd row, starting with 1

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.

> > procedure

> > (gif-add-control |  | stream |  |  |  |
> > |  | disposal |  |  |  |
> > |  | wait-for-input? |  |  |  |
> > |  | delay |  |  |  |
> > |  | 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:

- 'any : doesn’t matter (perhaps because the next image completely overwrites the current one)

- 'keep : leave the image in place

- 'restore-bg : replace the image with the background color

- 'restore-prev : restore the overall image content to the content before the image is added

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.

> > procedure

> > (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.

> > procedure

> > (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).

> > procedure

> > (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).

> > procedure

> > (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

- bytes for the image (i.e., a array of colors, expressed as a byte string)

- a colormap

- either #f or a color index for the transparent “color”

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.

top ← prev up next →