Sign in to follow this  
McGrane

Understanding Bitmap's

Recommended Posts

McGrane    1743

Im trying to understand how bitmaps are stored.

Im using this code to load bitmaps...

unsigned char* Load( char *fileName, BITMAPINFOHEADER *bitmapInfoHeader ) {

      FILE                      *filePtr;          // The File Pointer
      BITMAPFILEHEADER           bitmapFileHeader; // Bitmap File Header
      unsigned char             *bitmapImage;      // Bitmap Image Data
      int                        imageIdx = 0;     // Image Index Counter
      unsigned char              tempRGB;          // Swap Variable BGR -> RGB
     
      filePtr  =  fopen( fileName, "rb" );
      if( filePtr == NULL )
           return NULL;
	  
	  fread( &bitmapFileHeader, sizeof(BITMAPFILEHEADER), 1, filePtr );

      if(bitmapFileHeader.bfType != 0x4D42 ) {
           fclose(filePtr);
           return NULL;
      }
      
	  fread( bitmapInfoHeader, sizeof( BITMAPINFOHEADER ), 1, filePtr );
	  fseek( filePtr, bitmapFileHeader.bfOffBits, SEEK_SET );
      
	  bitmapImage = ( unsigned char* ) malloc( bitmapInfoHeader->biSizeImage );
      
	  if( !bitmapImage ) {
           free(bitmapImage);
           fclose(filePtr);
           return NULL;
      }
	  fread( bitmapImage, 1, bitmapInfoHeader->biSizeImage, filePtr );
      
            
      for( imageIdx = 0; imageIdx < bitmapInfoHeader->biSizeImage; imageIdx += 3 ) {
           tempRGB = bitmapImage[imageIdx];
           bitmapImage[ imageIdx ] = bitmapImage[ imageIdx + 2 ];
           bitmapImage[imageIdx+2]=tempRGB;
      }
     fclose(filePtr);     
     
	 return bitmapImage;
}

 

And this to read them...

BITMAPINFOHEADER    bitmapInfoHeader;
    unsigned char       *bitmapImage;
    
    bitmapImage = Load( "test.bmp", &bitmapInfoHeader );
    
    int index = 0;
    
    for( index = 0; index < 16; index+=3 ) {
           cout<< (int)bitmapImage[index+0]<<", ";
           cout<< (int)bitmapImage[index+1]<<", ";
           cout<< (int)bitmapImage[index+2]<<endl;
    }
    

 

What i dont understand is that when i pass a 2x2 block, say:

All red, i get:

255, 0, 0

255, 0, 0

0,0,0

0,255,0

0,255,0

 

Green:

0, 255, 0

0, 255, 0,

0,0,0

0,0,255,

0,0,255

 

and Blue:

0,0,255

0,0,255

255,0,0

255,0,0

0,0,0

Edited by McGrane

Share this post


Link to post
Share on other sites
McGrane    1743

Last time i edited this post, the formatting went crazy and i had to rewrite it so im just gona post here.

Also, after each of those blocks i have shown, they are also followed by 3 more numbers that seem to change each time it is run.

I know this code works, as the BGR is swoped to RGB and looks fine. I just dont get why them seem to shift right each time after a line of 0's or in that case, why the position of those 0's change in blue.

Any help would be greatly appreciated ;)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this