Skip to main content
gamedev.net
🔒 Locked

Bitmap file format

Started by BlitzKrieg 4 replies 850+ views
Original Post
BlitzKrieg
BlitzKrieg
Hey, I''m trying to output to a bitmap file, but it ain''t working! Paint tells me it''s not a valid bitmap file, and photoshop tells me it can''t parse the file. I''m using the format described at: http://www.dcs.ed.ac.uk/home/mxr/gfx/2d/BMP.txt and this is the code i''m using to do it.
  
// create headers

BITMAPFILEHEADER	bmfheader;
BITMAPINFO			bmi;

// init headers

bmfheader.bfType = (''M'' * 256) + ''B'';
bmfheader.bfSize = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFO)
				+ 3 * height * (width + (width % 4));
bmfheader.bfReserved1 = 0;
bmfheader.bfReserved2 = 0;
bmfheader.bfOffBits = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFO);

bmi.bmiColors[0] = 0;
bmi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
bmi.bmiHeader.biWidth = width;
bmi.bmiHeader.biHeight = height;
bmi.bmiHeader.biPlanes = 1;
bmi.bmiHeader.biBitCount = 24;
bmi.bmiHeader.biCompression = 0;
bmi.bmiHeader.biSizeImage = 0;
bmi.bmiHeader.biXPelsPerMeter = 0;
bmi.bmiHeader.biYPelsPerMeter = 0;
bmi.bmiHeader.biClrUsed = 0;
bmi.bmiHeader.biClrImportant = 0;

// write headers

outputStm << bmfheader.bfType << bmfheader.bfSize << bmfheader.bfReserved1 << bmfheader.bfReserved2 << bmfheader.bfOffBits;
outputStm << bmi.bmiHeader.biSize << bmi.bmiHeader.biWidth
		<< bmi.bmiHeader.biHeight << bmi.bmiHeader.biPlanes << bmi.bmiHeader.biBitCount
		<< bmi.bmiHeader.biCompression << bmi.bmiHeader.biSizeImage 
		<< bmi.bmiHeader.biXPelsPerMeter << bmi.bmiHeader.biYPelsPerMeter
		<< bmi.bmiHeader.biClrUsed << bmi.bmiHeader.biClrImportant;	outputStm << bmi.bmiColors[0];


// Write each pixel

	for (int y = height-1; y >= 0; y--)
	{
		for (int x = 0; x < width; x++)
		{
			outputStm << blue[index(x,y)] << green[index(x,y)] 
				<< red[index(x,y)];
					}
		for ( int n = 0; n < width % 4; ++n ) // set scanline on a LONG boundary

		{
			outputStm << ''\0'';
		}
	}

  
Hope that source displays ok. Any help greatly appreciated! Thanks.
johnnie2
johnnie2
Why is bmfheader.bfSize being set to something greater than the sizeof(BITMAPFILEHEADER)?

RapscallionGL - arriving soon.
____________"Optimal decisions, once made, do not need to be changed." - Robert Sedgewick, Algorithms in C
Burning_Ice
Burning_Ice
Hi

your bmfheader.bfOffBits have to be sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) (0x36)
same in the bfSize calculation: use BITMAPINFOHEADER instead of BITMAPINFO

also, dont output the bmi.bmiColors[0]

and why do you only write single int values to your file??
just use some binary filewrite function and so something like
fostream << fileheader;
fostream << infoheader;
fostream << paddedimagedata;

would make your code much more readable


hope that helps


Edit: Johnnie2: the bfSize (Bitmap File Size) member is supposed to be the size of the whole file, not the size of the header, so he's correct there



Runicsoft -- home of my open source Function Parser and more

[edited by - Burning_Ice on October 29, 2002 6:13:19 PM]
Burning_Ice
Burning_Ice
quote:
Original post by Anonymous Poster
Another thing occured to me...you say the offset should be 0x36, i assume that it''s from the start of the file. But the info header is 40 bytes, and the file header is 20 bytes = 60 bytes, and 0x36 translates to 54 i believe...so theres an extra 6 bytes there?!? Is the file format described on that webpage wrong?
(And if so where can i find a correct one!)




if i do a sizeof(BITMAPFILEHEADER) i get 14.
when writing my bmp code i also looked at .bmps created by various paint programs in a hex editor and it was always 0x36.


Blitzkrieg: are you sure that your file output function writes the right variable types to the file? e.g. are the red[..], blue[..], green[..] all unsigned chars?

i really don''t see any other errors now...but if you want you can check out the bmp tutorial on my homepage, it comes with working code




Runicsoft -- home of my open source Function Parser and more

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.