# save 1bit windows-bitmap

## Recommended Posts

ehmdjii    238
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 on other sites
Endurion    5408
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-&gt;Width() );  MyFile.WriteU32( pData-&gt;Height() );  MyFile.WriteU16( 1 );  int iBits = pData-&gt;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-&gt;BitsProPixel() &lt;= 8 )  {    // Palette    for ( size_t i = 0; i &lt; pData-&gt;Palette()-&gt;Entries(); ++i )    {      MyFile.WriteU8( pData-&gt;Palette()-&gt;Blue( i ) );      MyFile.WriteU8( pData-&gt;Palette()-&gt;Green( i ) );      MyFile.WriteU8( pData-&gt;Palette()-&gt;Red( i ) );      MyFile.WriteU8( 0 );    }  }  for ( int j = pData-&gt;Height() - 1; j &gt;= 0; --j )  {    MyFile.WriteBlock( pData-&gt;GetRowData( j ), pData-&gt;BytesPerLine() );    int   iPadding = ( pData-&gt;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-&gt;Palette() )  {    MyFile.WriteU32( 14 + 40 + (GR::u32)pData-&gt;Palette()-&gt;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 on other sites
ehmdjii    238
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!

##### Share on other sites
Evil Steve    2017
Quote:
 Original post by ehmdjiiso 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 on other sites
Endurion    5408
Quote:
 Original post by ehmdjiithank 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.