Jump to content
  • Advertisement
Sign in to follow this  
Toolmaker

MFC/Win32: Loading images as 24-bit bitmaps...

This topic is 3669 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'm currently looking into a way to load different image types into memory as 24-bit bitmaps that allow me to access the RAW pixel data without too much hassle. I currently use the IPicture interface, but the damn thing refuses to return the final pixel data as 24-bit. I have no use for any of the pixel data if it's not in 24-bit data, because I need to come up with all sorts of constructs to handle the different pixel formats. What would be the best approach to this? Draw the returned image to an offscreen buffer and use that as buffer data? Or is there library(Non (L)GPL) that could handle this for me? Toolmaker [Edited by - Toolmaker on December 2, 2008 5:04:20 AM]

Share this post


Link to post
Share on other sites
Advertisement
Alright, I've worked out a little code, that doesn't work quite like I want it to work... I'm using GetDIBits to get an device independant bitmap back in 24-bit RGB format.

However, it returns 0 scanlines and pData is still 0. Any ideas what's going wrong here?


HBITMAP hBitmap;
DWORD size = m_xBitmapPixels * m_yBitmapPixels * 3;
BYTE *pData = new BYTE[size]; // Returned bitmap is 24-bit!
ZeroMemory(pData, size * sizeof(BYTE));

if (SUCCEEDED(pictureFile.m_IPicture->get_Handle((OLE_HANDLE*)&hBitmap)))
{
CBitmap bmp;
CDC *pDC = new CDC();
if (pDC->CreateCompatibleDC(NULL) && bmp.Attach(hBitmap))
{
pDC->SelectObject(&bmp);
BITMAPINFO bmpInfo;

ZeroMemory(&bmpInfo.bmiHeader, sizeof(BITMAPINFOHEADER));
bmpInfo.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
bmpInfo.bmiHeader.biWidth = m_xBitmapPixels;
bmpInfo.bmiHeader.biHeight = m_yBitmapPixels;
bmpInfo.bmiHeader.biPlanes = 1;
bmpInfo.bmiHeader.biBitCount = 24;
bmpInfo.bmiHeader.biCompression = BI_RGB;

int scanLines = GetDIBits(pDC->m_hDC, hBitmap, 0, m_yBitmapPixels, (LPVOID) pData, &bmpInfo, DIB_RGB_COLORS);
scanLines = 0; // Debug stuff
}
delete pDC;
}



Toolmaker

Share this post


Link to post
Share on other sites
If you use GetObject on the HBITMAP, what does it report the dimensions and bit count as?

E.g.

BITMAP bmp;
GetObject(hBitmap, sizeof(bmp), &bmp);
// Check contents of bmp here

Share this post


Link to post
Share on other sites
The returned size/bitcount/etc. are all correct. However, even if I use standard Win32 code for this, it still returns 0 scanlines.


HDC hdc = CreateCompatibleDC(NULL);

if (hdc != NULL)
{
BITMAPINFO bmpInfo;
ZeroMemory(&bmpInfo.bmiHeader, sizeof(BITMAPINFOHEADER));
bmpInfo.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
bmpInfo.bmiHeader.biWidth = m_xBitmapPixels;
bmpInfo.bmiHeader.biHeight = m_yBitmapPixels;
bmpInfo.bmiHeader.biPlanes = 1;
bmpInfo.bmiHeader.biBitCount = 24;
bmpInfo.bmiHeader.biCompression = BI_RGB;

HGDIOBJ obj = SelectObject(hdc, hBitmap);
int scanLines = GetDIBits(hdc, hBitmap, 0, m_yBitmapPixels, (LPVOID) pData, &bmpInfo, DIB_RGB_COLORS);
scanLines = 0; // Debug stuff
}



Kinda confused right now

Share this post


Link to post
Share on other sites
What does the following give?


HDC hdc = CreateCompatibleDC(NULL);
if (hdc != NULL)
{
HBITMAP hBitmap = CreateBitmap(m_xBitmapPixels, m_yBitmapPixels, 1, 24, NULL);
if(hBitmap != NULL)
{
BITMAPINFO bmpInfo;
ZeroMemory(&bmpInfo.bmiHeader, sizeof(BITMAPINFOHEADER));
bmpInfo.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
bmpInfo.bmiHeader.biWidth = m_xBitmapPixels;
bmpInfo.bmiHeader.biHeight = m_yBitmapPixels;
bmpInfo.bmiHeader.biPlanes = 1;
bmpInfo.bmiHeader.biBitCount = 24;
bmpInfo.bmiHeader.biCompression = BI_RGB;

HGDIOBJ obj = SelectObject(hdc, hBitmap);
int scanLines = GetDIBits(hdc, hBitmap, 0, m_yBitmapPixels, (LPVOID) pData, &bmpInfo, DIB_RGB_COLORS);
scanLines = 0; // Debug stuff
}
}


Share this post


Link to post
Share on other sites
Ah, I see - from the Docs:
Quote:
hbmp [in] Handle to the bitmap. This must be a compatible bitmap (DDB).

The bitmap in the code I posted and the bitmap from IPicture are both non-compatible bitmaps. You'd need to use CreateCompatibleBitmap() to create a compatible bitmap for GetDIBits() to work. Or use a DIB section, which might be easier.

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.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!