Understanding Bitmap's

Started by
2 comments, last by McGrane 11 years, 2 months ago

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

Advertisement

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 ;)

One important thing to keep in mind about windows bitmaps is that the bitmap data is padded so that each row takes up a number of bytes that is a multiple of 4 (and that this padding occurs at the end of the row).

Ah, ok that explains a bit :) Thank you !

This topic is closed to new replies.

Advertisement