Sign in to follow this  

save 1bit windows-bitmap

This topic is 3557 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

hello, does anyone have information on how to save a 1Bit Windows-Bitmap file? (binary image) especially what do i write in the RGBQUAD structure for the red green and blue values? thanks!

Share this post


Link to post
Share on other sites
It's pretty straightforward actually.

There are two RGBQUAD entries (palette), two times the usual 4 bytes: Blue, Green, Red, filler.

The image data contains 8 pixels per byte. Do not forget the padding of the lines to multiples of 4 bytes.

Here's my BMP save routine which works for all bit depths. It doesn't do any RLE however:


bool Save( const GR::tChar* szFileName, GR::Graphic::ImageData* pData )
{

GR::IO::FileStream MyFile;


if ( !MyFile.Open( szFileName, IIOStream::OT_WRITE_ONLY ) )
{
return false;
}

// BitmapFileHeader
MyFile.WriteU16( 0x4d42 );
MyFile.WriteU32( 0 ); // Size of Bitmap File
MyFile.WriteU32( 0 );
MyFile.WriteU32( 0 ); // Offset to Bitmap bits

// BitmapInfoHeader
MyFile.WriteU32( 40 ); // size of BITMAPINFOHEADER
MyFile.WriteU32( pData->Width() );
MyFile.WriteU32( pData->Height() );
MyFile.WriteU16( 1 );
int iBits = pData->BitsProPixel();
if ( iBits == 15 )
{
iBits = 16;
}
MyFile.WriteU16( iBits );
MyFile.WriteU32( 0 ); // BI_RGB
MyFile.WriteU32( 0 ); // sizeof Image
MyFile.WriteU32( 0 );
MyFile.WriteU32( 0 );
MyFile.WriteU32( 0 );
MyFile.WriteU32( 0 );

if ( pData->BitsProPixel() <= 8 )
{
// Palette
for ( size_t i = 0; i < pData->Palette()->Entries(); ++i )
{
MyFile.WriteU8( pData->Palette()->Blue( i ) );
MyFile.WriteU8( pData->Palette()->Green( i ) );
MyFile.WriteU8( pData->Palette()->Red( i ) );
MyFile.WriteU8( 0 );
}
}

for ( int j = pData->Height() - 1; j >= 0; --j )
{
MyFile.WriteBlock( pData->GetRowData( j ), pData->BytesPerLine() );
int iPadding = ( pData->BytesPerLine() % 4 );

while ( iPadding % 4 )
{
MyFile.WriteU8( 0 );
iPadding++;
}
}

MyFile.Flush();
GR::u32 dwSize = (GR::u32)MyFile.GetSize();

MyFile.SetPosition( 2, IIOStream::PT_SET );
MyFile.WriteU32( dwSize );
MyFile.SetPosition( 4, IIOStream::PT_CURRENT );
if ( pData->Palette() )
{
MyFile.WriteU32( 14 + 40 + (GR::u32)pData->Palette()->Entries() * 4 );
}
else
{
MyFile.WriteU32( 14 + 40 );
}
MyFile.Close();

return true;

}



ImageData is a class for holding an image in memory. GetRowData gets a pointer to the start of a line, everything else should be pretty clear from the names.

Share this post


Link to post
Share on other sites
Quote:
Original post by ehmdjii
so although it is only a 1bit image, it still needs to save as much as a full RGBA image? (4bytes per pixel?)
No, it's 1 bit per pixel. So 8 pixels in 1 byte.

However, the BMP file specification states that each scanline must end on a multiple of 4 bytes. So if your image is 10px wide, that's 2 bytes, so you'd need to have 2 bytes of padding at the end of each row in the file.

Share this post


Link to post
Share on other sites
Quote:
Original post by ehmdjii
thank you!

so although it is only a 1bit image, it still needs to save as much as a full RGBA image? (4bytes per pixel?)

thanks!


What Evil Steve said. Plus, the two RGBQUADs i mentioned are for the palette. Since the image data only allows for one bit per pixel you need to specify what color actually is used (there's not only black and white). This is the so-called palette.

This allows you to use less memory for the image. The image data only carries indices into that palette.

Share this post


Link to post
Share on other sites

This topic is 3557 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this