Victor Image Processing Library How-to Tips

Load an Animated GIF

(To see how this animation was created visit rotate.html.)
An animated gif file consists of multiple frames. To load them all into a single image the sequence is:
  • determine how many frames are in the file
  • get the information describing each frame
  • allocate an image buffer to hold all the frames top to bottom
  • set the background color
  • load each frame into the correct position in the buffer.


VB Source Code | C Source Code



Load an Animated GIF - the Visual Basic Source Code

Requires Victor Image Processing Library for 32-bit Windows v 5.3 or higher.
Public Function load_animated_gif(fname As String, jimage As imgdes) As Long
 ' jimage will be replaced by this animated gif
   Dim rcode As Long
   Dim tempimage As imgdes
   Dim totalFrames As Long
   Dim gdata As GifGlobalData
   Dim fdatarray() As GifFrameData
   Dim size As Long
                        ' Count the number of frames
   rcode = gifframecount(fname, totalFrames)
   ReDim fdatarray(0 To totalFrames - 1)
                        ' Get all the frames' info
   rcode = gifinfoallframes(fname, gdata, fdatarray(0), totalFrames)
                        ' Load all frames into a single vertical image
   If (rcode = NO_ERROR) Then
      Dim j As Long
      
                        ' Allocate an image buffer
      rcode = allocimage(tempimage, gdata.saveData.scrwidth, gdata.saveData.scrlength * totalFrames, fdatarray(0).vbitcount)
      
      If (rcode = NO_ERROR) Then
        Dim dummy As Long
                        ' Set the background color
         dummy = zeroimage(gdata.saveData.bckColor, tempimage)
                        ' Load all frames
         For j = 0 To totalFrames - 1
            tempimage.stx = fdatarray(j).saveData.startx
            tempimage.sty = j * gdata.saveData.scrlength + fdatarray(j).saveData.starty
            rcode = loadgifframe(fname, tempimage, gdata, fdatarray(j))
            Next j
      End If
         tempimage.stx = 0
         tempimage.sty = 0
         freeimage jimage
         copyimgdes tempimage, jimage   ' Replace previous image with this animated gif
   End If
   If (rcode = NO_ERROR Or rcode = BAD_DATA) Then
      rcode = NO_ERROR  Set rcode to NO_ERROR if file contained invalid data
   End If
   
   load_animated_gif = rcode
End Function
........... Add these defines and declarations to your Global module ...........
' Image descriptor
Type imgdes
   ibuff As Long
   stx As Long
   sty As Long
   endx As Long
   endy As Long
   buffwidth As Long
   palette As Long
   colors As Long
   imgtype As Long
   bmh As Long
   hBitmap As Long
End Type
Type GifGlobalSaveData
   scrwidth As Long
   scrlength As Long
   hasColorMap As Long
   bckColor As Long  
   loop As Long
End Type
Type GifGlobalData
   saveData as GifGlobalSaveData 
   BitsPPixel As Long
   colorRes As Long
   pixelAspectRatio As Long
   commentOffset As Long
   colors As Long
   colorMapOffset As Long
End Type
Type GifFrameSaveData
   startx As Long
   starty As Long
   hasColorMap As Long
   delay As Long        ' 100ths of a second to display frame
   transColor As Long   ' Transparent color index, -1 => none
   removeBy As Long     ' How graphic is to be treated after display
   waitForUserInput As Long  'If true, expect user input
End Type
Type GifFrameData
   saveData as GifFrameSaveData 
   vbitcount As Long
   width As Long
   length As Long
   frame As Long
   interlace As Long
   codesize As Long
   colors As Long
   colorMapOffset As Long
   rasterDataOffset As Long
End Type
Declare Function allocimage Lib "VIC32.DLL" (image As imgdes, ByVal wid As Long, ByVal leng As Long, ByVal BPPixel As Long) As Long
Declare Sub copyimgdes Lib "VIC32.DLL" (srcimg As imgdes, desimg As imgdes)
Declare Sub freeimage Lib "VIC32.DLL" (image As imgdes)
Declare Function gifframecount Lib "VIC32.DLL" (ByVal fname As String, ByRef totalFrames As Long) As Long
Declare Function gifinfoallframes Lib "VIC32.DLL" (ByVal fname As String, gdata As GifGlobalData, fdata As GifFrameData, ByVal frameElem As Long) As Long
Declare Function loadgifframe Lib "VIC32.DLL" (ByVal fname As String, desimg As imgdes, gdata As GifGlobalData, fdata As GifFrameData) As Long

Load an Animated GIF - the C Source Code

Requires Victor Image Processing Library v 5.3 or higher.
int load_animated_gif(LPTSTR fname, imgdes *image)
{// image will be replaced by this animated gif
   int rcode;
   imgdes tempimage;
   int totalFrames;
   GifGlobalData gdata;
   GifFrameData *fdatarray;
   int size;
                        // Count the number of frames
   rcode = gifframecount(fname, &totalFrames);
   size = sizeof(GifFrameData);
   fdatarray = (GifFrameData *)calloc(totalFrames, size);
                        // Get all the frames' info
   rcode = gifinfoallframes(fname, &gdata, fdatarray, totalFrames);
                        // Load all frames into a single vertical image
   if(rcode == NO_ERROR) {
      int j;
                        // Allocate an image buffer
      if((rcode = allocimage(&tempimage, gdata.saveData.scrwidth, gdata.saveData.scrlength * totalFrames, fdatarray[0].vbitcount)) == NO_ERROR)
                        // Set the background color
         zeroimage(gdata.saveData.bckColor, &tempimage);
                        // Load all frames
         for(j=0; j < totalFrames; j++) {
            tempimage.stx = fdatarray[j].saveData.startx ;
            tempimage.sty = j * gdata.saveData.scrlength + fdatarray[j].saveData.starty ;
            rcode = loadgifframe(fname, &tempimage, &gdata, &fdatarray[j]);
         }
         tempimage.stx = 0;
         tempimage.sty = 0;
         freeimage(image);
         copyimgdes(&tempimage, image);  // Replace previous image with this animated gif
      }
   if(rcode == NO_ERROR || rcode == BAD_DATA) {
      rcode = NO_ERROR; // Set rcode to NO_ERROR if file contained invalid data
      }
   return(rcode);
}
Victor Image Processing Library

Victor Image Processing Library homepage | Victor Product Summary | more source code




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