Victor Image Processing Library How-to Tips

Convert a TIFF 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 TIFF, save the GIF, and free the memory.

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



TIFF to GIF - the Visual Basic Source Code

Requires Victor Image Processing Library for 32-bit Windows v 5 or higher.
........... Defines and declarations  ...........
' 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
' TIFF file info 
Type TiffData
   ByteOrder As Long
   width As Long
   length As Long
   BitsPSample As Long
   comp As Long
   SamplesPPixel As Long
   PhotoInt As Long
   PlanarCfg As Long
   vbitcount As Long
End Type
Declare Function loadtif Lib "VIC32.DLL" (ByVal Fname As String, desimg As imgdes) As Long
Declare Function tiffinfo Lib "VIC32.DLL" (ByVal Fname As String, tdat As TiffData) As Long
Declare Function convertgray16to8 Lib "VIC32.DLL" (srcimg As imgdes, resimg As imgdes) 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 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
'................................................................................................
Private Sub mnuconvertTIFFtoGIF_Click()
   Dim tmpimage As imgdes
   Dim tmp2image As imgdes
   Dim rcode As Long
   Dim vbitcount As Long
   Dim colors As Long
   Dim filedat As TiffData ' Reserve space for file info struct
   Dim tif_fname As String
   Dim gif_fname As String
   Const CR_TSDDIFF = 3
   tif_fname = "test.tif"
   gif_fname = "test.gif"
            ' Get info on the file we're to load
   rcode = tiffinfo(tif_fname, filedat)
   If (rcode <> NO_ERROR) Then
      MsgBox "Cannot find file", 0, "Error encountered!"
      Exit Sub
   End If
    
   vbitcount = filedat.vbitcount
            ' Allocate space for an image
   rcode = allocimage(tmpimage, filedat.width, filedat.Length, vbitcount)
   If (rcode <> NO_ERROR) Then
     MsgBox "Not enough memory", 0, "Error encountered!"
     Exit Sub
   End If
   
            ' Load image
   rcode = loadtif(tif_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 = 16) Then ' If we loaded a 16-bit grayscale image, convert to 8-bit grayscale
            ' because gif only supports up to 8-bit pixel depth
     rcode = allocimage(tmp2image, filedat.width, filedat.Length, 8)
     If (rcode = NO_ERROR) Then
         rcode = convertgray16to8(tmpimage, tmp2image)
         freeimage tmpimage  ' Replace 16-bit grayscale image with 8-bit grayscale image
         copyimgdes tmp2image, tmpimage
     End If
   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, filedat.width, filedat.length, 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 palette color image
         copyimgdes tmp2image, tmpimage
     End If
   End If
            ' Save image
   rcode = savegifex(gif_fname, tmpimage, 8, 0)
   freeimage tmpimage
   
            ' Handle any errors
    If rcode <> NO_ERROR Then
        MainWnd.error_handler rcode, ""
    End If
End Sub
Victor Image Processing Library

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




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