Jump to content
  • Advertisement
Sign in to follow this  
godsenddeath

problem with bitmap loading code

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

I wrote a bitmap loading class, but the bitmaps aren't showing up properly, It's written in c++, and i'm rendering with openGL here's the main loading functions

//bmInfo, and bmFile are structs representing the internal structure of the //bitmap header

bool Bitmap::Load(char *Filename)
{
	std::ifstream File(Filename,std::ios::binary);

	File.read((char*)&bmFile.bfType,2);//read header info
	File.read((char*)&bmFile.bfSize,4);
	File.read((char*)&bmFile.bfReserved1,2);
	File.read((char*)&bmFile.bfReserved2,2);
	File.read((char*)&bmFile.bfOffBits,4);


	File.read((char*)&bmInfo,sizeof(bmInfo)); //read the rest of teh header info

	if(bmInfo.biBitCount < 24) //if indexed
	{
		if(bmInfo.biBitCount == 8) // if 8 bit
		{

		SecureZeroMemory(&Quad,sizeof(BMRGBQuad));//read color index

		File.read((char*)&Quad,sizeof(Quad));
		}

		else
		{
			MessageBox(0,"Bitmap Is In Unsupported Format, Sorry","Error",0);
			return false;
		}

	}

	BufferSize = bmFile.bfSize - bmFile.bfOffBits; //total filesize minus header size

	if(BufferSize <= 0)
	{
		MessageBox(0,"File Contains No Data","Error",0);
		return false;
	}

	Buffer = new unsigned char[BufferSize];

	File.read((char*)Buffer,BufferSize); // read pixel data

	File.close();

	ArrangeImage(Buffer); //eliminate padding,  and re-order

	if(Buffer == 0)
	{
		MessageBox(0,"File Failed To Load","Error",0);
		return false;
	}

	return true;
}

void Bitmap::ArrangeImage(unsigned char *RawData)
{


	if(RawData == 0)
	{
		MessageBox(0,"Failed","Error",0);
		RawData = 0;
	}

	unsigned char* tempbuffer = 0;
	tempbuffer = new unsigned char[BufferSize];
	memcpy(tempbuffer,RawData,BufferSize); //copy data to temp buffer 
	delete[] RawData;//clear original buffer
	RawData = 0;
	

	int WidthInBytes = 0,PaddedWidthInBytes = 0,ImageSizeInBytes = 0;

	WidthInBytes = PaddedWidthInBytes = (bmInfo.biWidth * bmInfo.biBitCount)/8;
	BufferSize = ImageSizeInBytes = WidthInBytes * bmInfo.biHeight;

	RawData = new unsigned char[BufferSize]; // reallocate original buffer


	while(PaddedWidthInBytes%4 != 0)
	{
		PaddedWidthInBytes++; 
	}

	if(bmInfo.biHeight < 0)
	{
		//Inverted

	}

	int offset = 0;
	int Padding = PaddedWidthInBytes - WidthInBytes;

	for(unsigned int i = 0;i<bmInfo.biHeight;i++) //copy the data line by line, skipping the padding
	{

		memcpy((RawData + offset),(tempbuffer + (offset + (i * Padding))),WidthInBytes);

		offset += WidthInBytes;
	}

	delete[] tempbuffer;

}






i'm not sure if it's this code, or the flags i'm passing to glDrawPixels, but i wanna check this first the 2 flags I pass to glDrawPixels are GL_RGB, and GL_UNSIGNED_BYTE thanks EDIT: I added comments to the code [Edited by - godsenddeath on June 20, 2008 5:56:14 PM]

Share this post


Link to post
Share on other sites
Advertisement
Can you define in more detail what you mean by "bitmaps aren't showing up properly"?

Share this post


Link to post
Share on other sites
yeah they're 32x32, and what i meant was that if I lead a white square, it shows up as white with red lines, and when i was loading a sprite, the color was reversed(which i fixed when i realized that bitmaps order their components BGR

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!