Simple BitMap (SBM) is an image format designed for easy reading and writing. There is no compression, no re-ordering of colour values and no complex header.

SBM files have a 5 byte header:

OffsetSizeDescription
02Image Width
22Image Height
41BPP

Image data follows the header at offset 5. The image data will be either 24 bit (RGB) or 32 bit (RGBA) as specified by the header.
An quick consistency check for SBM files is to read the header and check the filesize is equal to (Width*Height*(BPP/8))+5.


'C' Example SBM Loader

     // SBM Example.c - A quick and dirty SBM loader

     #include <stdio.h>
     #include <stdlib.h>
     #include <malloc.h>

     // A couple of structures to make our life easier
     typedef struct SBM_HEADER
      {
       unsigned short Width, Height;
       char BPP;
      }SBM_Header;

     typedef struct SBM_IMAGE
      {
       SBM_Header Head;
       unsigned char *imgdata;
      }SBM_Image;

     SBM_Image* LoadSBM(char *fName);



     int main(int argc, char *argv[])
      {
       SBM_Image *Image;

       Image=LoadSBM("TestImage.sbm");
        
        if(Image)
         printf("Loaded OK\n");
        else
         printf("Load failed!\n");
        
       // Clean up
        if(Image)
         {
           if(Image->Imgdata)
            free(Image->imgdata);
            
          free(Image);
         }


       return 0;
      }


     // SBM Loader
     SBM_Image* LoadSBM(char *fName)
      {
       FILE *in;
       SBM_Image *SBM=NULL;
       long fSize;

       // Open the file
       in=fopen(fName,"rb");

        if(!in)
         return NULL;
    
       // Allocate structure
       SBM=malloc(sizeof(SBM_Image));

       // Get filesize
       fseek(in,0,SEEK_END);
       fSize=ftell(in);
       fseek(in,0,SEEK_SET);

       // Grab the header
       fread(&(SBM->Head),sizeof(SBM_Header),1,in);

       // Validate filesize and allocate space for image
        if(fSize==(SBM->Head.Width * SBM->Head.Height) * (SBM->Head.BPP / 8) + 5)
         SBM->imgdata=malloc(fSize-5);
        else
         {
          free(SBM);
          return NULL;
         }

       // Read in image data
       fread(SBM->imgdata,fSize-5,1,in);

       // Close file and return image
       fclose(in);
       return SBM;
      }

    

Example C++ SBM Class

     // SBM Example.cpp - A quick and dirty SBM class

     #include <iostream>
     #include <fstream>

     // A header structure to make our life easier
     typedef struct SBM_HEADER
      {
       unsigned short Width, Height;
       char BPP;
      }SBM_Header;

     class SBM_Image
      {
       public: 
        SBM_Image();
        ~SBM_Image();
        bool Load(char *fName);
        unsigned char* GetImage();
        SBM_Header GetImgInfo();

       protected:
        SBM_Header Head;
        unsigned char *imgdata;
      };


     int main(int argc, char *argv[])
      {
       SBM_Image Image;

        if(Image.Load("TestImage.sbm"))
         std::cout<<"Image Loaded"<<std::endl;
        else
         std::cout<<"Load Failed!"<<std::endl;

       return 0;
      }

     SBM_Image::SBM_Image()
      {
       imgdata=NULL;
      }

     SBM_Image::~SBM_Image()
      {
        if(imgdata)
         delete [] imgdata;
      }

     unsigned char* SBM_Image::GetImage()
      {
       return imgdata;
      }

     SBM_HEADER SBM_Image::GetImgInfo()
      {
       return Head;
      }

     bool SBM_Image::Load(char *fName)
      {
       using namespace std;
       ifstream fIn;
       unsigned long ulSize;

       // Clear out any existing image data
        if(imgdata)
         {
          delete [] imgdata;
          imgdata=NULL;
         }

       // Open the specified file
       fIn.open(fName,ios::binary);
    
        if(fIn==NULL)
         return false;

       // Get file size
       fIn.seekg(0,ios_base::end);
       ulSize=fIn.tellg();
       fIn.seekg(0,ios_base::beg);

       // Read the header
       fIn.read((char*)&Head,sizeof(SBM_Header));

       // Validate filesize
        if(ulSize==(Head.Width * Head.Height) * (Head.BPP / 8) + 5)
         imgdata = new unsigned char[ulSize-5];
        else
         return false;

       // Read the image data
       fIn.read((char*)imgdata,ulSize-5);
  
       fIn.close();
       return true;
      }

    

Problems, ideas, better routines ?
Mail me or drop into the forums


Home EMail Forum