Victor Image Processing Library How-to Tips

Create an animated GIF



growing.gif
Growing image created with Victor functions resizeex and savegifframe.


The animation is contained in a series of frames in a single GIF file. For this example the original image is resized into each frame. The smallest size is placed in first and each successive frame receives a larger size. The savegifframe function appends each frame to the file. There are 10 resized image frames in the final GIF image file, growing.gif.

Create a Growing Image Animation

Requires Victor Image Processing Library v 5.3 or higher.
// A helper function to find the white color in the palette.
int findwhite(imgdes *srcimg)
{
   int j;
   int r, g, b;
   int maxwhite = 0;
   int maxwhiteindex = -1;
   for(j = 0; j <= srcimg->colors; j++) {
      r = srcimg->palette[j].rgbRed;
      g = srcimg->palette[j].rgbGreen;
      b = srcimg->palette[j].rgbBlue;
      if(r == g && r == b)
         if (r > maxwhite) {
            maxwhiteindex = j;
            maxwhite = r;
            }
      }
   return maxwhiteindex;
}
// Make an animated gif of a growing image
int makegrowingGIF(int frames, imgdes *srcimg)
{
   int rcode = NO_ERROR;
   int cols, rows;
   int j;
   int wd, ht;
   imgdes desimg;
   int foundwhite;
   GifGlobalSaveData gdata;
   GifFrameSaveData fdata;
   if(srcimg->bmh->biBitCount != 8)
      return(BAD_BPP);
   
   cols = CALC_WIDTH(srcimg);
   rows = CALC_HEIGHT(srcimg);
   foundwhite = findwhite(srcimg);
   if(foundwhite < 0)          // Didn't find white 
      foundwhite = 0;          // Use color number zero as background 
            // GIF global data used by savegifframe(), etc.
   gdata.scrwidth = cols;
   gdata.scrlength = rows;
   gdata.hasColorMap = TRUE;   // Global color table is present!
   gdata.bckColor = foundwhite;// Color index of screen backgnd
   gdata.loop = 1000;          // Number of iterations
            // GIF frame data used by savegifframe(), etc.
   fdata.startx = 0;
   fdata.starty = 0;          // X,Y pixel position with respect to scrwidth, scrlength
   fdata.hasColorMap = FALSE;  // Local color table present?
   fdata.delay = 1;           // 100ths of a second to display frame
   fdata.transColor = -1;     // Transparent color index, -1 => none
   fdata.removeBy = 0;        // How graphic is to be treated after display
   fdata.waitForUserInput = FALSE; // If true, expect user input
   wd = cols/frames/2;
   ht = rows/frames/2;
   rcode = allocimage(&desimg, cols, rows, 8);
   copyimagepalette(srcimg, &desimg);
   zeroimage(foundwhite, &desimg);
            // First frame, plain white
   rcode = savegifframe("growing.gif", &desimg, &gdata, &fdata, GIFNOCOMP);
   for(j = frames-1; j >= 0; j--) {
      fdata.startx = wd*j;
      fdata.starty = ht*j;    // X,Y pixel position with respect to scrwidth, scrlength
      fdata.removeBy = 3;     // How graphic is to be treated after display
            //NOTHING    0  The image is left unremoved
            //ASIS       1  The image is left unremoved
            //PREVIOUS   2  The image is replaced by the previous image
            //BACKGROUND 3  The image is replaced by the background
            // Create a growing image 
      setimagearea(&desimg, fdata.startx, fdata.starty, cols-fdata.startx-1, rows-fdata.starty-1);
      resizeex(srcimg, &desimg, RESIZEFAST); /*1 = RESAMPLEBILINEAR*/
      rcode = savegifframe("growing.gif", &desimg, &gdata, &fdata, GIFNOCOMP);
      }
   freeimage(&desimg);
   return (rcode);
}

Copyright © 2001 Catenary Systems Inc. All rights reserved. Victor Image Processing Library is a trademark of Catenary Systems.


Victor Image Processing Library homepage | Victor Sample Code