Victor Image Processing Library How-to Tips

Convert a BMP Image to a GIF

Converting from one image file format to another is easy with Victor. Determine the size of the image and allocate space for it in memory, load the BMP, save the GIF, and free the memory.

In general, to convert from one image format to another, the steps are:


VB Source Code | C Source Code


BMP to GIF - the Visual Basic Source Code

Requires Victor Image Processing Library for 32-bit Windows v 5 or higher.
Private Sub mnuconvertBMPtoGIF_Click()
   Dim tmpimage As imgdes
   Dim tmp2image As imgdes
   Dim rcode As Long
   Dim vbitcount As Long
   Dim colors As Long
   Dim bdat As BITMAPINFOHEADER ' Reserve space for BMP struct
   Dim bmp_fname As String
   Dim gif_fname As String
   Const CR_TSDDIFF = 3
   bmp_fname = "test.bmp"
   gif_fname = "test.gif"
            ' Get info on the file we're to load
   rcode = bmpinfo(bmp_fname, bdat)
   If (rcode <> NO_ERROR) Then
      MsgBox "Cannot find file", 0, "Error encountered!"
      Exit Sub
   End If
    
   vbitcount = bdat.biBitCount
   If (vbitcount >= 16) Then  ' 16-, 24-, or 32-bit image is loaded into 24-bit buffer
      vbitcount = 24
   End If
   
            ' Allocate space for an image
   rcode = allocimage(tmpimage, bdat.biWidth, bdat.biHeight, vbitcount)
   If (rcode <> NO_ERROR) Then
     MsgBox "Not enough memory", 0, "Error encountered!"
     Exit Sub
   End If
   
            ' Load image
   rcode = loadbmp(bmp_fname, tmpimage)
   If (rcode <> NO_ERROR) Then
      freeimage tmpimage ' Free image on error
      MsgBox "Cannot load file", 0, "Error encountered!"
      Exit Sub
   End If
   If (vbitcount = 24) Then ' If we loaded a 24-bit image, convert to 8-bit palette color
            ' because gif only supports up to 8-bit pixel depth
     rcode = allocimage(tmp2image, bdat.biWidth, bdat.biHeight, 8)
     If (rcode = NO_ERROR) Then
         colors = 256
         reduction_mode = CR_TSDDIFF   ' For the best image quality
         rcode = convertrgbtopalex(colors, tmpimage, tmp2image, reduction_mode)
         freeimage tmpimage  ' Replace 24-bit image with 8-bit version of image
         copyimgdes tmp2image, tmpimage
     End If
   End If
            ' Save image
   rcode = savegifex(gif_fname, tmpimage, 8, 0)
   freeimage tmpimage
End Sub
........... 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 BITMAPINFOHEADER
    biSize As Long
    biWidth As Long
    biHeight As Long
    biPlanes As Integer
    biBitCount As Integer
    biCompression As Long
    biSizeImage As Long
    biXPelsPerMeter As Long
    biYPelsPerMeter As Long
    biClrUsed As Long
    biClrImportant As Long
End Type
Declare Function bmpinfo Lib "VIC32.DLL" (ByVal Fname As String, bdat As BITMAPINFOHEADER) As Long
Declare Function allocimage Lib "VIC32.DLL" (image As imgdes, ByVal wid As Long, ByVal leng As Long, ByVal BPPixel As Long) As Long
Declare Function loadbmp Lib "VIC32.DLL" (ByVal Fname As String, desimg As imgdes) As Long
Declare Sub freeimage Lib "VIC32.DLL" (image As imgdes)
Declare Function convertrgbtopalex Lib "VIC32.DLL" (ByVal palcolors As Long, srcimg As imgdes, desimg As imgdes, ByVal mode As Long) As Long
Declare Sub copyimgdes Lib "VIC32.DLL" (srcimg As imgdes, desimg As imgdes)
Declare Function savegif Lib "VIC32.DLL" (ByVal Fname As String, srcimg As imgdes) As Long
Declare Function savegifex Lib "VIC32.DLL" (ByVal Fname As String, srcimg As imgdes, ByVal savemode As Long, ByVal transcolor As Long) As Long

BMP to GIF - the C Source Code

Requires Victor Image Processing Library v 5.0 or higher.
int bmp2gif()
{
   imgdes tmpimage;
   int rcode, vbitcount;
   BITMAPINFOHEADER bdat; // Reserve space for BMP struct
   char *src_fname = "test.bmp"; 
   char far *des_fname = "test.gif";
            // Get info on the file we're to load 
   rcode = bmpinfo(src_fname, &bdat);
   if(rcode != NO_ERROR) {
      return(rcode);
      }
   vbitcount = bdat.biBitCount;
   if(vbitcount >= 16)   // 16-, 24-, or 32-bit image is loaded into 24-bit buffer
      vbitcount = 24;
            // Allocate space for an image
   rcode = allocimage(&tmpimage, (int)bdat.biWidth, (int)bdat.biHeight, vbitcount);
   if(rcode != NO_ERROR) {
      return(rcode);
      }
            // Load image 
   rcode = loadbmp(src_fname, &tmpimage);
   if(rcode != NO_ERROR) { 
      freeimage(&tmpimage); // Release the memory and return if there's an error
      return(rcode);
      }
            // If we loaded a 24-bit image, convert to 8-bit
            // because the GIF format does not support 24-bit images
   if(vbitcount == 24) {  
      imgdes tmp2image;
      rcode = allocimage(&tmp2image, (int)bdat.biWidth, (int)bdat.biHeight, 8);
      if(rcode == NO_ERROR) {
         int colors = 256;
         int reduction_mode = CR_TSDDIFF;  // For the best image quality
         convertrgbtopalex(colors, &tmpimage, &tmp2image, reduction_mode);
         freeimage(&tmpimage);  // Replace 24-bit image with 8-bit image
         copyimgdes(&tmp2image, &tmpimage);
         }
      }
            // Save image
   rcode = savegifex(des_fname, &tmpimage, 8, 0);
   freeimage(&tmpimage);
   return(rcode);
}
Victor Image Processing Library

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




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