Jump to content

  • Log In with Google      Sign In   
  • Create Account

Understanding Bitmap's


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
3 replies to this topic

#1 McGrane   Members   -  Reputation: 1087

Like
0Likes
Like

Posted 02 February 2013 - 07:09 PM

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, 02 February 2013 - 07:19 PM.


Sponsor:

#2 McGrane   Members   -  Reputation: 1087

Like
0Likes
Like

Posted 02 February 2013 - 07:27 PM

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



#3 RulerOfNothing   Members   -  Reputation: 1160

Like
2Likes
Like

Posted 02 February 2013 - 07:44 PM

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

#4 McGrane   Members   -  Reputation: 1087

Like
0Likes
Like

Posted 03 February 2013 - 05:30 PM

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






Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS