Victor Image Processing Library How-to Tips

Convert a TIFF Image to a JPEG

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, and save the JPEG.

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


VB Source Code | C/C++ Source Code | Java Source Code


TIFF to JPEG - the Visual Basic Source Code

Requires Victor Image Processing Library for 32-bit Windows v 5 or higher.
Private Sub mnuconvertTIFFtoJPG_Click()
   Dim tmpimage As imgdes    ' Image descriptors
   Dim tmp2image As imgdes
   Dim rcode As Long
   Dim quality As Long
   Dim fileinfo As TiffData ' Reserve space for TIFF struct
   Dim tif_fname As String
   Dim jpg_fname As String
   tif_fname = "test.tif"
   jpg_fname = "test.jpg"
   quality = 75
            ' Get info on the file we're to load
   rcode = tiffinfo(tif_fname, fileinfo)
   If (rcode <> NO_ERROR) Then
      MsgBox "Cannot find file", 0, "Error encountered!"
      Exit Sub
   End If
    
            ' Allocate space for an image
   rcode = allocimage(tmpimage, fileinfo.width, fileinfo.length, fileinfo.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 (fileinfo.vbitcount = 1) Then ' If we loaded a 1-bit image, convert to 8-bit grayscale
            ' because jpeg only supports 8-bit grayscale or 24-bit color images
     rcode = allocimage(tmp2image, fileinfo.width, fileinfo.length, 8)
     If (rcode = NO_ERROR) Then
         rcode = convert1bitto8bit(tmpimage, tmp2image)
         freeimage tmpimage  ' Replace 1-bit image with grayscale image
         copyimgdes tmp2image, tmpimage
     End If
   End If
   If (fileinfo.vbitcount = 16) Then ' If we loaded a 16-bit grayscale image, convert to 8-bit grayscale
            ' because jpeg only supports 8-bit grayscale
     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
            ' Save image
   rcode = savejpg(jpg_fname, tmpimage, quality)
   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
' 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 tiffinfo Lib "VIC32.DLL" (ByVal Fname As String, tdat As TiffData) As Long
Declare Function loadtif Lib "VIC32.DLL" (ByVal Fname As String, desimg As imgdes) 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 convert1bitto8bit Lib "VIC32.DLL" (srcimg As imgdes, desimg As imgdes) As Long
Declare Sub copyimgdes Lib "VIC32.DLL" (srcimg As imgdes, desimg As imgdes)
Declare Function savejpg Lib "VIC32.DLL" (ByVal Fname As String, srcimg As imgdes, ByVal quality As Long) As Long

TIFF to JPEG - the C Source Code

Requires Victor Image Processing Library v 5 or higher.
int tif2jpeg(char far *src_fname, char far *des_fname)
{
   imgdes tmpimage;
   int rcode, quality=75;
   TiffData fileinfo; // Reserve space for TIFF struct
            // Get info on the file we're to load 
   rcode = tiffinfo(src_fname, &fileinfo);
   if(rcode != NO_ERROR) {
      return(rcode);
      }
            // Allocate space for an image
   rcode = allocimage(&tmpimage, fileinfo.width, fileinfo.length, fileinfo.vbitcount);
   if(rcode != NO_ERROR) {
      return(rcode);
      }
            // Load image 
   rcode = loadtif(src_fname, &tmpimage);
   if(rcode != NO_ERROR) { 
      freeimage(&tmpimage); // Free image on error
      return(rcode);
      }
   if(fileinfo.vbitcount == 1) {  // If we loaded a 1-bit image, convert to 8-bit grayscale
            // because jpeg only supports 8-bit grayscale or 24-bit color images
      imgdes tmp2image;
      rcode = allocimage(&tmp2image, fileinfo.width, fileinfo.length, 8);
      if(rcode == NO_ERROR) {
         convert1bitto8bit(&tmpimage, &tmp2image);
         freeimage(&tmpimage);  // Replace 1-bit image with 8-bit grayscale image
         copyimgdes(&tmp2image, &tmpimage);
         }
      }
   If (fileinfo.vbitcount == 16) { // If we loaded a 16-bit grayscale image, convert to 8-bit grayscale
            // because jpeg only supports 8-bit grayscale 
     imgdes tmp2image;
     rcode = allocimage(&tmp2image, fileinfo.width, fileinfo.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);
         }
      }
            // Save image
   rcode = savejpg(des_fname, &tmpimage, quality);
   freeimage(&tmpimage);
   return(rcode);
}

TIFF to JPEG - the Java Source Code

Requires Victor Image Processing Library v 5.3 or higher, the Victor Java Native Interface vic.zip and Java 2.

Place the dlls:

vic32.dll in x:\java\bin
victw32.dll in x:\java\bin
vic32jni.dll in the same subdirectory as the application

To compile and run bmp2jpeg.class:

javac -classpath x:\java Tif2jpeg.java
java -classpath x:\java;x:\java\vic Tif2jpeg

 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

/*
   Tif2jpeg, a Java application to convert a tiff image file to a jpeg image file
   javac -classpath e:\java Tif2jpeg.java
   java -classpath e:\java;e:\java\vic Tif2jpeg
*/
import vic.*;     
class Tif2jpeg {
   public static void main(String args[]) {
   String loadfilename;
   String savefilename;
   int rcode = 0; 
   vic.imgdes timage = new vic.imgdes();
   vic.TiffData filedata = new vic.TiffData();
   rcode = vic.vic32jni.enableunicode(1);  // Turn on Unicode if available
            // load tif save as jpeg
   loadfilename = "test.tif";
   savefilename = "test.jpg";
   rcode = vic.vic32jni.tiffinfo(loadfilename, filedata);
   if(rcode == vic.vic32jni.NO_ERROR) {
      rcode = vic.vic32jni.allocimage(timage, filedata.width, filedata.length, filedata.vbitcount);
      if(rcode == vic.vic32jni.NO_ERROR) {
         System.out.println("Allocation successful, loading tif file");
         System.out.println("  image width in pixels = " + filedata.width);
         System.out.println("  image length in pixels = " + filedata.length);
         System.out.println("  buffer width in bytes = " + timage.buffwidth);
         rcode = vic32jni.loadtif(loadfilename, timage);
         if(rcode == vic.vic32jni.NO_ERROR) {
            System.out.println("File load successful, saving jpeg file");
            if(timage.bibitcount == 1 || timage.bibitcount == 16) 
               rcode = convertto8bitgrayscale(timage);
            rcode = vic.vic32jni.savejpg(savefilename, timage, 75);
            System.out.println("Filesave rcode = " + rcode);
            }
         }
      if(rcode == 0)
         vic.vic32jni.freeimage(timage);
      }
   } // end of main()
   public static void displayimgdes(vic.imgdes rimage) {
   System.out.println("     Image buffer address        " + rimage.ibuff);        
   System.out.println("     Image area to be processed  " + rimage.stx +", "+ rimage.sty +", "+ rimage.endx +", "+ rimage.endy); 
   System.out.println("     Image buffer width in bytes " + rimage.buffwidth);    
   System.out.println("     Address of palette          " + rimage.palette);      
   System.out.println("     Palette colors              " + rimage.colors);       
   System.out.println("     Image type                  " + rimage.imgtype);      
   System.out.println("     BITMAPINFOHEADER address    " + rimage.bmh);          
   System.out.println("     DIB handle                  " + rimage.hBitmap);      
   System.out.println("     Pixel width of image        " + rimage.biwidth);      
   System.out.println("     Pixel length of image       " + rimage.biheight);     
   System.out.println("     Pixel depth of image        " + rimage.bibitcount);   
   }
   public static int convertto8bitgrayscale(vic.imgdes rimage) {
   vic.imgdes grayimage = new vic.imgdes();
   int rcode;
   System.out.println("Converting from 1- to 8-bit");
   rcode = vic.vic32jni.allocimage(grayimage, rimage.biwidth, rimage.biheight, 8);
   if(rcode == vic.vic32jni.NO_ERROR) {
      if(rimage.bibitcount == 1)
         rcode = vic.vic32jni.convert1bitto8bit(rimage, grayimage);
      
      if(rimage.bibitcount == 16)
         rcode = vic.vic32jni.convertgray16to8(rimage, grayimage);
      if(rcode == vic.vic32jni.NO_ERROR) {
         System.out.println("Conversion successful");
         System.out.println("original image:");
         displayimgdes(rimage);
         System.out.println("8-bit grayscale image:");
         displayimgdes(grayimage);
         vic.vic32jni.freeimage(rimage);
         vic.vic32jni.copyimgdes(grayimage, rimage);
         System.out.println("Replacing original image with:");
         displayimgdes(rimage);
         }
      }
   return(rcode); // 8-bit image replaces the original 1-bit image
   }
}
Victor Image Processing Library

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




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