Next: , Previous: Drawing on a Page, Up: C Programming


9.2.7 Animated GIFs in C

Using GNU 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 pair 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. That 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()
     {
       plPlotter *plotter;
       plPlotterParams *plotter_params;
       int i;
     
       /* set Plotter parameters */
       plotter_params = pl_newplparams ();
       pl_setplparam (plotter_params, "BITMAPSIZE", "150x100");
       pl_setplparam (plotter_params, "BG_COLOR", "orange");
       pl_setplparam (plotter_params, "TRANSPARENT_COLOR", "orange");
       pl_setplparam (plotter_params, "GIF_ITERATIONS", "100");
       pl_setplparam (plotter_params, "GIF_DELAY", "5");
     
       /* create a GIF Plotter with the specified parameters */
       plotter = pl_newpl_r ("gif", stdin, stdout, stderr, plotter_params);
     
       pl_openpl_r (plotter);                 /* begin page of graphics */
       pl_fspace_r (plotter,
                    -0.5, -0.5, 149.5, 99.5); /* set user coor system */
     
       pl_pencolorname_r (plotter, "red");    /* use red pen */
       pl_linewidth_r (plotter, 5);           /* set line thickness */
       pl_filltype_r (plotter, 1);            /* objects will be filled */
       pl_fillcolorname_r (plotter, "black"); /* set the fill color */
     
       for (i = 0; i < 180 ; i += 15)
         {
           pl_erase_r (plotter);              /* begin new GIF image */
           pl_ellipse_r (plotter, 75, 50, 40, 20, i); /* draw an ellipse */
         }
     
       pl_closepl_r (plotter);                /* end page of graphics */
       pl_deletepl_r (plotter);               /* delete Plotter */
       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_fspace_r defines user coordinates that are effectively the same as pixel coordinates. In the user coordinate system, the lower left corner of the rectangle mapped into the 150x100 pseudo-GIF image is given coordinates (−0.5,−0.5), and the upper right corner is given coordinates (149.5,99.5). So individual pixels may be addressed in terms of integer user coordinates. For example, invoking pl_point_r(plotter,0,0) and pl_point_r(plotter,149,99) would set the pixels in the lower left and upper right corners 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_setplparam 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 Plotter Parameters.