# Understanding Bitmap's

## Recommended Posts

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

fclose(filePtr);
return NULL;
}

bitmapImage = ( unsigned char* ) malloc( bitmapInfoHeader->biSizeImage );

if( !bitmapImage ) {
free(bitmapImage);
fclose(filePtr);
return NULL;
}

for( imageIdx = 0; imageIdx < bitmapInfoHeader->biSizeImage; imageIdx += 3 ) {
tempRGB = bitmapImage[imageIdx];
bitmapImage[ imageIdx ] = bitmapImage[ imageIdx + 2 ];
bitmapImage[imageIdx+2]=tempRGB;
}
fclose(filePtr);

return bitmapImage;
}


BITMAPINFOHEADER    bitmapInfoHeader;
unsigned char       *bitmapImage;

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 on other sites

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 on other sites
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).

##### Share on other sites

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

## Create an account

Register a new account

• ### Forum Statistics

• Total Topics
628337
• Total Posts
2982164

• 9
• 24
• 9
• 9
• 13