Jump to content
  • Advertisement
Sign in to follow this  
MitchellH

Copying Bitmap To Surface Without GDI

This topic is 4748 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, So I think I managed to load a 16-bit bitmap correctly using the following code:
bool CBitmap::Load(char *szFilename)
{
	BITMAPFILEHEADER bmpHeader;
	BITMAPINFOHEADER bmpInfoHeader;
	unsigned char* pucPixels;

	char *szPath = new char[MAX_PATH];
	GetModuleFileName(NULL, szPath, MAX_PATH);
	PathRemoveFileSpec(szPath);

	char *szFullPath = new char[strlen(szPath) + strlen(szFilename) + 2];
	strcpy(szFullPath, szPath);
	strcat(szFullPath, "\\");
	strcat(szFullPath, szFilename);

	delete[] szPath;

	std::ifstream isBitmap(szFullPath, std::ios::in | std::ios::binary);
	delete[] szFullPath;

	if (!isBitmap.is_open())
	{
		(CError::GetInstance())->Notify("Failed to open bitmap for read.", "!isBitmap.is_open()");
		return false;
	}

	isBitmap.read(reinterpret_cast<char *>(&bmpHeader), sizeof(bmpHeader));

	if (bmpHeader.bfType != 0x4D42)
	{
		(CError::GetInstance())->Notify("Invalid Bitmap", "bfType != Valid Bitmap");
		isBitmap.close();
		return false;
	}

	isBitmap.read(reinterpret_cast<char *>(&bmpInfoHeader), sizeof(bmpInfoHeader));

	if (bmpInfoHeader.biBitCount != 16)
	{
		(CError::GetInstance())->Notify("Invalid Bitmap Depth.", "Bitmap is not 16-bit.");
		isBitmap.close();
		return false;
	}

	if (!(pucPixels = new unsigned char[bmpHeader.bfSize - bmpHeader.bfOffBits]))
	{
		(CError::GetInstance())->Notify("Not enough memory to load bitmap.", "Failed to allocate pucPixels.");
		isBitmap.close();
		return false;
	}

	isBitmap.read(reinterpret_cast<char *>(pucPixels), bmpHeader.bfSize - bmpHeader.bfOffBits);
	isBitmap.close();

	for (int nIndex = 0; nIndex < static_cast<int>(bmpHeader.bfSize); nIndex += 3)
	{
		unsigned char ucTempRGB = pucPixels[nIndex];
		pucPixels[nIndex] = pucPixels[nIndex + 2];
		pucPixels[nIndex + 2] = ucTempRGB;
	}

	m_pcPixels = pucPixels;
	m_nWidth = bmpInfoHeader.biWidth;
	m_nHeight = bmpInfoHeader.biHeight;
	return true;
}



Right? Well how, now, do I copy those pixels onto a surface? Help is much appreciated, thank you :) I used something such as this just to test out this method...
bool CBitmap::Draw(unsigned char *pcDest)
{
	unsigned char *pcPixels = m_pcPixels;
	for (int x = 0; x < m_nWidth; x++)
	{
		for (int y = 0; y < m_nHeight; y++)
		{
			pcDest[y] = pcPixels[y];
		}

		pcPixels += m_nWidth;
		pcDest += m_nWidth;
	}

	return true;
}


And also, when I quit the program, I get an error when I attempt to delete[] m_pcPixels. I can't figure out why...

Share this post


Link to post
Share on other sites
Advertisement
Guest Anonymous Poster
Be sure to invert the bitmap (most bitmaps are writen upside-down) and consider the "padding" bits thing.

Example: if you have a 250x250 bitmap, it's writen 256x250, because the weight must be a product of 2 or something like that. There's a tutorial here on gamedev about loading bitmaps, it should help you out.


// lock

surface->LockRect(&lockedRect, NULL, D3DLOCK_DISCARD)

// get pointer

unsigned char * pcDest = static_cast<unsigned char*>(lockedRect.pBits);

// everthing else is alike

unsigned char *pcPixels = m_pcPixels;
for (int x = 0; x < m_nWidth; x++)
{
for (int y = 0; y < m_nHeight; y++)
{
pcDest[y * lockedRect.Pitch + x * 4 + 0] = (pcPixels[y * m_nWidth + x * 3 + 0]);
pcDest[y * lockedRect.Pitch + x * 4 + 1] = (pcPixels[y * m_nWidth + x * 3 + 1]);
pcDest[y * lockedRect.Pitch + x * 4 + 2] = (pcPixels[y * m_nWidth + x * 3 + 2]);
pcDest[y * lockedRect.Pitch + x * 4 + 3] = 255;
}
}

// unlock

surface->UnlockRect(0);



if you want a "color key", just check the three values and set the fourth to 255 (opaque) or 0 (transparent) accordingly.

See ya

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!