Archived

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

YABP ( Yet Another Bitmap Problem )

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

If you intended to correct an error in the post then please contact us.

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.
  
void cBitmap::loadBMP()
{
	FILE *in;

	if( ( in = fopen( file.c_str(), "rb" ) ) == NULL )
	{
		printf( "[cBitmap::loadBMP] - Cannot open the file. " );
		exit( 1 );
	}
	fread( &bmfh, sizeof( BITMAPFILEHEADER ), 1, in );
	fread( &bmih, sizeof( BITMAPINFOHEADER ), 1, in );
	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 this post


Link to post
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


Have some patience, please.

Share this post


Link to post
Share on other sites