Go to the first, previous, next, last section, table of contents.


Animated GIFs in C

Using libplot to create pseudo-GIF files, including animated pseudo-GIFs, is straightforward. A GIF Plotter is a Plotter like any other, and it supports the same drawing operations. However, it has two special properties. (1) It can draw only a single page of graphics, i.e., only the graphics contained in the first openpl...closepl appear in the output file. In this, it resembles other Plotters that do not plot in real time. (2) Within this page, each invocation of erase is normally treated as the beginning of a new image in the output file. There is an exception to this: the first invocation of erase begins a new image only if something has already been drawn.

The reason for the exception is that many programmers who use libplot are in the habit of invoking erase immediately after a Plotter is opened. This is not a bad habit, since a few types of Plotter (e.g., X Drawable and Tektronix Plotters) are `persistent' in the sense that previously drawn graphics remain visible.

The following program creates a simple animated pseudo-GIF, 150 pixels wide and 100 pixels high.

#include <stdio.h>
#include <plot.h>

int main()
{
  int i, handle;

  /* set Plotter parameters */
  pl_parampl ("BITMAPSIZE", "150x100");
  pl_parampl ("BG_COLOR", "orange");
  pl_parampl ("TRANSPARENT_COLOR", "orange");
  pl_parampl ("GIF_ITERATIONS", "100");
  pl_parampl ("GIF_DELAY", "5");

  /* create a GIF Plotter with the specified parameters */
  handle = pl_newpl ("gif", stdin, stdout, stderr);
  pl_selectpl (handle);         /* select the Plotter for use */
     
  pl_openpl();			/* begin page of graphics */
  pl_space (0, 0, 149, 99);	/* specify user coordinate system */
  
  pl_pencolorname ("red");      /* objects will be drawn in red */
  pl_linewidth (5);             /* set the line thickness */
  pl_filltype (1);              /* objects will be filled */
  pl_fillcolorname ("black");   /* set the fill color */

  for (i = 0; i < 180 ; i += 15)
    {
      pl_erase ();              /* begin new GIF image */
      pl_ellipse (75, 50, 40, 20, i); /* draw an ellipse */
    }

  pl_closepl ();                /* end page of graphics */
  pl_selectpl (0);              /* select default Plotter */
  pl_deletepl (handle);         /* delete Plotter we used */
  return 0;
}

The animated pseudo-GIF will be written to standard output. It will consist of twelve images, showing the counterclockwise rotation of a black-filled red ellipse through 180 degrees. The pseudo-GIF will be `looped' (see below), so the ellipse will rotate repeatedly.

The parameters of the ellipse are expressed in terms of user coordinates, not pixel coordinates. But the call to pl_space defines user and pixel coordinates to be effectively the same. User coordinates are chosen so that the lower left corner is (0,0) and the upper right corner is (149,99). Since this agrees with the image size, individual pixels may be addressed in terms of integer user coordinates. For example, pl_point(149,99) would set the pixel in the upper right hand corner of the image to the current pen color.

Besides BITMAPSIZE and BG_COLOR, there are several important GIF Plotter parameters that may be set with the pl_parampl function. The TRANSPARENT_COLOR parameter may be set to the name of a color. Pixels in a pseudo-GIF that have that color will be treated as transparent by most software. This is usually used to create a transparent background. In the example above, the background color is specified as orange, but the transparent color is also specified as orange. So the background will not actually be displayed.

The GIF_ITERATIONS parameter, if set, specifies the number of times that a multi-frame pseudo-GIF should be looped. The GIF_DELAY parameter specifies the number of hundredths of a seconds that should elapse between successive images.

The INTERLACE parameter is sometimes useful. If it is set to "yes", the pseudo-GIF will be interlaced. This is of greatest value for single-frame GIFs. For full details on Plotter parameters, see section Device driver parameters.


Go to the first, previous, next, last section, table of contents.