Jump to content
  • Advertisement
Sign in to follow this  
MiXen

Getting BITMAP structure in DirectX.

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

Advertisement

I don't know exactly what you want. Do you have a BMP image and want to extract the width and height?

	std::vector<BYTE>Buffer = LoadBmpFile("myimage.bmp");

	BITMAPFILEHEADER* pFHeader = (BITMAPFILEHEADER*)Buffer.data();
	BITMAPINFO* pIHeader = (BITMAPINFO*)(Buffer.data() + sizeof(BITMAPFILEHEADER));

	if (pFHeader->bfType != 0x4d42)		//BM = 0x4d42
		throw std::exception("File type does not match header description in file");

	if (pFHeader->bfSize != Buffer.size())
		throw std::exception("File size does not match header description in file");

	BYTE* pImageData = Buffer.data() + pFHeader->bfOffBits;
	UINT Width = pIHeader->bmiHeader.biWidth;
	UINT Height = pIHeader->bmiHeader.biHeight;

Share this post


Link to post
Share on other sites

You don't exacly understand me. I have that code:

IDWriteBitmapRenderTarget *pRenderTarget;
		hResult = pGDIInterop->CreateBitmapRenderTarget(
			NULL,
			m_renderTargetWidth,
			m_renderTargetHeight,
			&pRenderTarget
			);
		if (FAILED(hResult)) {
			m_lastError = L"Failed to create bitmap render target";
		}
		else {
			hResult = pRenderTarget->SetPixelsPerDip(1.0f);
			hResult = S_OK;

			HDC hDC = pRenderTarget->GetMemoryDC();
			if (hDC == NULL) {
				m_lastError = L"Failed to get render target DC";
				hResult = E_FAIL;
			}
			else {
				HBRUSH hBrush = CreateSolidBrush(RGB(0, 0, 0));
				if (hBrush == NULL) {
					m_lastError = L"Failed to create brush";
					hResult = E_FAIL;
				}
				else {
					HBITMAP hBitmap = static_cast<HBITMAP>(GetCurrentObject(hDC, OBJ_BITMAP));
					if (hBitmap == NULL) {
						m_lastError = L"GetCurrentObject failed";
						hResult = E_FAIL;
					}
					else {
						DIBSECTION dib;
						int iResult = GetObject(hBitmap, sizeof(dib), &dib);
						if (iResult < sizeof(dib)) {
							m_lastError = L"GetObject failed";
							hResult = E_FAIL;
						}
						else {
							// Store render target resources and info
							m_pRenderTarget = pRenderTarget;

							m_hDC = hDC;
							m_hBlackBrush = hBrush;

							m_bmBits = dib.dsBm.bmBits;
							m_bmWidthBytes = static_cast<UINT>(dib.dsBm.bmWidthBytes);
							m_bmBytesPixel = static_cast<UINT>(dib.dsBm.bmBitsPixel) / 8;

							hResult = S_OK;
						}
					}

					if (FAILED(hResult))
						DeleteObject(hBrush);
				}
			}

			if (FAILED(hResult))
				pRenderTarget->Release();
		}

		pGDIInterop->Release();

The API of my project didn't allow me to use functions: CreateSolidBrush, GetCurrentObject and GetObject. With solid brush I don't have a problem, because in directX i had a functions, that allow me to do it, but with the two others, I havea problem, because I cannot use it, so I cannot get a BITMAP structure, so I don't have a value for bmBits, bmWidthBytes and bmBitsPixel. I must get this values, so is there another way to get it?

Share this post


Link to post
Share on other sites
Why don’t you just make your own structures?

 
#pragma pack( push, 1 )
		/** The bitmap file header. */
		typedef struct LSI_BITMAPFILEHEADER {
			LSUINT16					ui16Header;
			LSUINT32					ui32Size;
			LSUINT16					ui16Reserved1;
			LSUINT16					ui16Reserved2;
			LSUINT32					ui32Offset;
		} * LPLSI_BITMAPFILEHEADER, * const LPCLSI_BITMAPFILEHEADER;

		/** The bitmap info header. */
		typedef struct LSI_BITMAPINFOHEADER {
			LSUINT32					ui32InfoSize;
			LSUINT32					ui32Width;
			LSUINT32					ui32Height;
			LSUINT16					ui16Planes;
			LSUINT16					ui16BitsPerPixel;
			LSUINT32					ui32Compression;
			LSUINT32					ui32ImageSize;
			LSUINT32					ui32PixelsPerMeterX;
			LSUINT32					ui32PixelsPerMeterY;
			LSUINT32					ui32ColorsInPalette;
			LSUINT32					ui32ImportantColors;
		} * LPLSI_BITMAPINFOHEADER, * const LPCLSI_BITMAPINFOHEADER;

		/** Bitmap color mask. */
		typedef struct LSI_BITMAPCOLORMASK {
			LSUINT32					ui32Red,
									ui32Green,
									ui32Blue,
									ui32Alpha;
		} * LPLSI_BITMAPCOLORMASK, * const LPCLSI_BITMAPCOLORMASK;

		/** Bitmap palette data. */
		typedef union LSI_BITMAPPALETTE {
			struct LSI_BM_COLOR {
				LSUINT8					ui8Red,
									ui8Green,
									ui8Blue,
									ui8Alpha;
			};
			LSUINT32					ui32Color;
		} * LPLSI_BITMAPPALETTE, * const LPCLSI_BITMAPPALETTE;
#pragma pack( pop )
 

L. Spiro Edited by L. Spiro

Share this post


Link to post
Share on other sites

Ok, but how can I use it? I repeat that I cannot put informations to this structures,becuase my api doesn't support this functions. I'm looking for the new functions replace old one.

Share this post


Link to post
Share on other sites

If Tispe had proposed a function named “LoadFileToMemory(),” rather than, “LoadBmpFile(),” it might have been more obvious to you.

 

Load the file to memory and then perform the same casts as he showed in his sample code.

 

 

L. Spiro

Share this post


Link to post
Share on other sites

Ok, I understand. But problem is in another place, because how can i convert IDWriteBitmapRenderTarget to something like that std::vector<BYTE>Buffer and then get some information about it?

Share this post


Link to post
Share on other sites

IDWriteBitmapRenderTarget::GetMemoryDC

 

"An application can use the device context to draw using GDI functions. An application can obtain the bitmap handle (HBITMAP) by calling GetCurrentObject. An application that wants information about the underlying bitmap, including a pointer to the pixel data, can call GetObject to fill in a DIBSECTION structure. The bitmap is always a 32-bit top-down DIB."

Share this post


Link to post
Share on other sites
Eee did you see my post with code? I use this function to get handle to bitmap, but I CANNOT USE THE GETCURRENTOBJECT FUNCTION BECAUSE OF MY API! I say it twice... I apreciate, that you want to help me, but please Read first my posts. So anybody can Tell me How can i get a bitmap handle (hbitmap) from idwritebitmaprendertarget without getcurrentobject function?

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!