Subject: Re: BMP Data Format

From: Sujin Han <[email-masked]>
Date: Sat, 02 Aug 1997 00:05:53 -0700

Max Kielland wrote:
> 
> Hi
> 
> I have the format description for the BMP format, but the documentation is
> very poor on the Black/White description.When i save my pictures and then
> reads them again, i sometimes get an GDI Error...
> 
> The documentation is very poor in this case, specialy on the description
> for the picture width. I have been experimenting with some padding to even
> multipples of 2 and 4, but i still got the GDI error on some pictures.
> 
> Max
> [email-masked]

Here is the part of c source code that i wrote some months ago when doing experiments on 
bmp formats. might not be the best way, but could help you start on right track.
BmInfoHeader.biWidth is the width of bitmap in PIXELS. For a black'n White picture, one 
pixel represents eight black or white dots in the picture. Be sure to have multiple of 
16bits per scan line(or multiple of 4bytes/scan line).

/********************************************************************************
/ write .bmp 
/ prep three Headers for .bmp 
/ determine total size of file; assume black'n White picture
*********************************************************************************/	
			
	totalDataSize = (DWORD)(loutbufSize);
	offsetToData = sizeof(BmFileHeader) + sizeof(BmInfoHeader) + 2*sizeof(RGBQUAD);
	totalFileSize = totalDataSize + offsetToData;

/* set parameters of BITMAPFILEHEADER */

	BmFileHeader.bfType = 0x4d42;
	BmFileHeader.bfSize = totalFileSize;
	BmFileHeader.bfReserved1 = 0;
	BmFileHeader.bfReserved2 = 0;
	BmFileHeader.bfOffBits = offsetToData;

/* set parameters of BITMAPINFOHEADER */
	
	BmInfoHeader.biSize = sizeof(BmInfoHeader);
	BmInfoHeader.biWidth = xSize;
	BmInfoHeader.biHeight = ySize;
	BmInfoHeader.biPlanes = 1;
	BmInfoHeader.biBitCount = 1;    /* color bits per pixel(1,4,8 or 24) */ 
	BmInfoHeader.biCompression = 0;	/* no compression */
	BmInfoHeader.biSizeImage = 0;	/* only requiered if compression is used */
	BmInfoHeader.biXPelsPerMeter = 0;
	BmInfoHeader.biYPelsPerMeter = 0;
	BmInfoHeader.biClrUsed = 2;
	BmInfoHeader.biClrImportant = 0;

/* open output file */
	if( (ofp = fopen( outFile, "wb")) == NULL )
		return	17; /* Error open output file   */

/* output Headers and Data to file */
	fwrite(&BmFileHeader, sizeof(BmFileHeader), 1, ofp);
	fwrite(&BmInfoHeader, sizeof(BmInfoHeader), 1, ofp);
	fwrite(&bmiColors, sizeof(RGBQUAD), 2, ofp);	
	fwrite(inbuf, sizeof(BYTE), (size_t)loutbufSize,ofp);

	fclose(ofp);

	_ffree(inbuf);
	_ffree(outbuf);

	return 1;
}