#### Archived

This topic is now archived and is closed to further replies.

# YABP ( Yet Another Bitmap Problem )

This topic is 5743 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Here we go again. This is what I have so far. Works fine ... no errors. But, there is color distortion and the BMP file inflates itself another ~30%. I really don''t know why this is happening; the bitmap size and BPP are the same.

{
FILE *in;

if( ( in = fopen( file.c_str(), "rb" ) ) == NULL )
{
printf( "[cBitmap::loadBMP] - Cannot open the file. " );
exit( 1 );
}
if( bmih.biBitCount != 24 )
{
fclose( in );
printf( "[cBitmap::loadBMP] - Only handles 24-bit bitmaps." );
exit( 1 );
}
pData = new BYTE[ 3 * bmih.biHeight * bmih.biWidth ];
for( unsigned int i = 0; i < bmih.biHeight * bmih.biWidth; i++ )
{
for( int c = 0; c < 3; c++ )
fread( &pData[ i * 3 + c ], sizeof( BYTE ), 1, in );
fread( &temp, sizeof( BYTE ), 1, in );
}
fclose( in );
writeBMP();
}

void cBitmap::writeBMP()
{
FILE* out;

out = fopen( "test2.bmp", "wb" );
fwrite( &bmfh, sizeof( BITMAPFILEHEADER ), 1, out );
fwrite( &bmih, sizeof( BITMAPINFOHEADER ), 1, out );
for( unsigned int i = 0; i < bmih.biHeight * bmih.biWidth; i++ )
{
for( int c = 0; c < 3; c++ )
fwrite( &pData[ i * 3 + c ], sizeof( BYTE ), 1, out );
fwrite( &temp, sizeof( BYTE ), 1, out );
}
fclose( out );
}


------------------- The Reindeer Effect

##### Share on other sites
To The Top

-------------------
The Reindeer Effect
Insomniac Software

##### Share on other sites
Disclaimer: I prefer to use other people''s code to load bitmaps (LoadImage, in this case) and might be wrong.

quote:
Original post by The Reindeer Effect
there is color distortion

Probably because you are treating part of the bitmap header as bitmap data. See BITMAPV4HEADER, BITMAPV5HEADER: the header may be larger than you think it is. Use BITMAPFILEHEADER::bfOffBits to seek to the beginning of bitmap data.
quote:

the BMP file inflates itself another ~30%.

Probably because the file stores RGB triplets, not RGB quads. This should also contribute to the color distortion. Try fread''ing all bitmap data (3 * bmih.biHeight * bmih.biWidth) to pData in one call.
quote:

To The Top

##### Share on other sites
Yea ... It was because i thought that it was aligned to 32 bit boundaries, but instead it just was an RGB triplet. Header data was correct though, so when I took out the extra stuff it worked fine.

-------------------
The Reindeer Effect
Insomniac Software

##### Share on other sites
Handy Tips:

Bitmaps are stored upside down, Rows are DWORD alined, and RGB is stored as BGR (I can''t remember if thats only the colourtable or 24 bit bitmap byte data as well, I think it is).

,Jay