Victor Image Processing Library How-to Tips

Determine the Image Format of a Disk File and Load the Image

To determine the file format test for successful gathering of image data with the fileinfo functions. Use a function pointer to select the correct loadfile function to call.

In the example function below, we test for TIFF, BMP, PCX, and then GIF. The function can be easily modified to extend to all supported file types.

This function successfully loads even misnamed files, e.g., PCX files incorrectly sporting the "TIF" extension.

Detect and Load - the Source Code

Requires Victor Image Processing Library for Windows v 4.1x or higher.
// Data structure for file info functions 
typedef union tagFILEDATA {
	TiffData          tiffdat;
	BITMAPINFOHEADER  bmpdat;
	JpegData          jpegdat;
	PcxData           pcxdat;
	GifData           gifdat;
	TgaData           tgadat;
	} FILEDATA;                     
	
// Determine type of file and load it
int detect_and_load(LPSTR fname, imgdes *image1)
{
   FILEDATA fdata;
   int rcode;
   imgdes timage;
   UINT width, length, bpp;
   int (WINAPI *loadfilefunction)(char *, imgdes *);
   rcode = tiffinfo(fname, &fdata.tiffdat);
   if(rcode == NO_ERROR) {
      loadfilefunction = loadtif;
      width  = fdata.tiffdat.width;
      length = fdata.tiffdat.length;
      bpp    = fdata.tiffdat.vbitcount;
      }
   else {
      rcode = bmpinfo(fname, &fdata.bmpdat);
   	if(rcode == NO_ERROR) {
         loadfilefunction = loadbmp;
         width = (UINT)fdata.bmpdat.biWidth;
         length = (UINT)fdata.bmpdat.biHeight;
         bpp = fdata.bmpdat.biBitCount;
         }
	else {
	   rcode = jpeginfo(fname, &fdata.jpegdat);
	   if(rcode == NO_ERROR) {
            loadfilefunction = loadjpg;
            width = fdata.jpegdat.width;
            length = fdata.jpegdat.length;
            bpp = fdata.jpegdat.vbitcount;
            }
         else {
            rcode = pcxinfo(fname, &fdata.pcxdat);
            if(rcode == NO_ERROR) {
               loadfilefunction = loadpcx;
               width = fdata.pcxdat.width;
               length = fdata.pcxdat.length;
               bpp = fdata.pcxdat.vbitcount;
               }
            else {
               rcode = gifinfo(fname, &fdata.gifdat);
               if(rcode == NO_ERROR) {
                  loadfilefunction = loadgif;
                  width = fdata.gifdat.width;
                  length = fdata.gifdat.length;
                  bpp = fdata.gifdat.vbitcount;
                  }
               }
            }
         }
      }
   if(rcode == NO_ERROR) { // Successfully determined file format
      rcode = allocimage(&timage, width, length, bpp);
      if(rcode == NO_ERROR) {
         rcode = loadfilefunction(fname, &timage);
         if(rcode == NO_ERROR) {
            // Success, free source image
            freeimage(image1);
            // Assign timage to image1
            copyimgdes(&timage, image1);
            }
         }
   }
   return(rcode);
}

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


Victor Image Processing Library homepage | Victor Sample Code