Exemple #1
0
CPixelBufferView::TexturePtr CPixelBufferView::CreateTextureFromBitmap(const Framework::CBitmap& bitmap)
{
	TexturePtr texture;

	if(!bitmap.IsEmpty())
	{
		HRESULT result = S_OK;
		result = m_device->CreateTexture(bitmap.GetWidth(), bitmap.GetHeight(), 1, D3DUSAGE_DYNAMIC, D3DFMT_A8R8G8B8, D3DPOOL_DEFAULT, &texture, nullptr);
		assert(SUCCEEDED(result));

		D3DLOCKED_RECT lockedRect = {};

		result = texture->LockRect(0, &lockedRect, nullptr, 0);
		assert(SUCCEEDED(result));
		
		uint32* dstPtr = reinterpret_cast<uint32*>(lockedRect.pBits);
		uint32* srcPtr = reinterpret_cast<uint32*>(bitmap.GetPixels());
		for(unsigned int y = 0; y < bitmap.GetHeight(); y++)
		{
			memcpy(dstPtr, srcPtr, bitmap.GetWidth() * sizeof(uint32));
			dstPtr += lockedRect.Pitch / sizeof(uint32);
			srcPtr += bitmap.GetPitch() / sizeof(uint32);
		}

		result = texture->UnlockRect(0);
		assert(SUCCEEDED(result));
	}

	return texture;
}
Framework::CBitmap CPixelBufferView::ConvertBGRToRGB(Framework::CBitmap bitmap)
{
	for(int32 y = 0; y < bitmap.GetHeight(); y++)
	{
		for(int32 x = 0; x < bitmap.GetWidth(); x++)
		{
			auto color = bitmap.GetPixel(x, y);
			std::swap(color.r, color.b);
			bitmap.SetPixel(x, y, color);
		}
	}
	return std::move(bitmap);
}
CPixelBufferView::TexturePtr CPixelBufferView::CreateTextureFromBitmap(const Framework::CBitmap& bitmap)
{
	TexturePtr texture;

	if(!bitmap.IsEmpty())
	{
		D3DFORMAT textureFormat =
		    [bitmap]() {
			    switch(bitmap.GetBitsPerPixel())
			    {
			    case 8:
				    return D3DFMT_L8;
			    case 16:
				    return D3DFMT_A1R5G5B5;
			    case 32:
			    default:
				    return D3DFMT_A8R8G8B8;
			    }
		    }();

		HRESULT result = S_OK;
		result = m_device->CreateTexture(bitmap.GetWidth(), bitmap.GetHeight(), 1, D3DUSAGE_DYNAMIC, textureFormat, D3DPOOL_DEFAULT, &texture, nullptr);
		assert(SUCCEEDED(result));

		switch(bitmap.GetBitsPerPixel())
		{
		case 8:
			CopyBitmapToTexture<uint8>(texture, 0, bitmap);
			break;
		case 16:
			CopyBitmapToTexture<uint16>(texture, 0, bitmap);
			break;
		case 32:
			CopyBitmapToTexture<uint32>(texture, 0, bitmap);
			break;
		default:
			assert(false);
			break;
		}
	}

	return texture;
}