GetDIBits() failing

This is a discussion on GetDIBits() failing within the Windows Programming forums, part of the Platform Specific Boards category; Hey all, I've been messing around with this for a while now but can't figure out why the call is ...

  1. #1
    Epo
    Epo is offline
    Registered User
    Join Date
    Jun 2003
    Posts
    361

    GetDIBits() failing

    Hey all, I've been messing around with this for a while now but can't figure out why the call is failing. The section of code looks like this:

    Code:
    if(GetDIBits(hDC, *hBitmap, 0, BitmapInfo->bmiHeader.biHeight,
    	Pixels, BitmapInfo, DIB_RGB_COLORS) == 0)
    {
    	printf("%ld ", GetLastError());
    	return NULL;
    }
    GetLastError is displaying Error Code 8: ERROR_NOT_ENOUGH_MEMORY

    If I removed LR_CREATEDIBSECTION from my LoadImage() function, below, then I get Error Code 0: ERROR_SUCCESS (The operation completed successfully.) That's the best Error I guess I could hope for, but none the less, it's still an error.

    This isn't very likely. So my guess is that I'm doing something horribly wrong and either it's interpretting it wrong, or I actually did something to waste all of my memory. Either way, I don't see where I'm going wrong.

    hBitmap has been initialized with:
    Code:
    hBitmap = (HBITMAP)LoadImage(NULL, FILE_PATH, IMAGE_BITMAP, 0, 0,
    	LR_DEFAULTCOLOR|LR_CREATEDIBSECTION|LR_DEFAULTSIZE|LR_LOADFROMFILE);
    BitmapInfo has been initialized with:
    Code:
    bool CompleteBitmapInfo(HBITMAP *hBitmap, BITMAPINFO *BitmapInfo)
    {
    	BITMAP Bitmap;
    
    	if(GetObject(*hBitmap, sizeof(BITMAP), &Bitmap) == 0)
    		return false;
    	
    	BitmapInfo->bmiHeader.biBitCount		= Bitmap.bmBitsPixel;
    	BitmapInfo->bmiHeader.biClrImportant	= 0;
    	BitmapInfo->bmiHeader.biClrUsed			= 0;
    	BitmapInfo->bmiHeader.biCompression		= BI_RGB;
    	BitmapInfo->bmiHeader.biHeight			= Bitmap.bmHeight;
    	BitmapInfo->bmiHeader.biPlanes			= Bitmap.bmPlanes;
    	BitmapInfo->bmiHeader.biSize			= sizeof(BITMAPINFOHEADER);
    	BitmapInfo->bmiHeader.biSizeImage		= 0;
    	BitmapInfo->bmiHeader.biWidth			= Bitmap.bmWidth;
    	//BitmapInfo.bmiHeader.biXPelsPerMeter =
    	//BitmapInfo.bmiHeader.biYPelsPerMeter =
    	BitmapInfo->bmiColors->rgbRed		= NULL;
    	BitmapInfo->bmiColors->rgbGreen		= NULL;
    	BitmapInfo->bmiColors->rgbBlue		= NULL;
    	BitmapInfo->bmiColors->rgbReserved	= NULL;
    
    	return true;
    }
    And the code leading up to the GetLastError line is:
    Code:
    HDC hDC = GetDC(NULL);
    if(hDC == NULL)
    	return NULL;
    
    unsigned char *Pixels = (unsigned char *)malloc(BitmapInfo->bmiHeader.biWidth * 
    	BitmapInfo->bmiHeader.biHeight * sizeof(unsigned char));
    	
    if(Pixels == NULL)
    	return NULL;
    
    if(GetDIBits(...
    Any thoughts on this would really be appreciated
    Last edited by Epo; 09-18-2005 at 10:36 PM.
    Pentium 4 - 2.0GHz, 512MB RAM
    NVIDIA GeForce4 MX 440
    WinXP
    Visual Studio .Net 2003
    DX9 October 2004 Update (R.I.P. VC++ 6.0 Compatability)

  2. #2
    Registered /usr
    Join Date
    Aug 2001
    Location
    Newport, South Wales, UK
    Posts
    1,266
    Code:
    int GetDIBits(HDC hdc, HBITMAP hbmp, UINT uStartScan, UINT cScanLines, LPVOID lpvBits, LPBITMAPINFO lpbi, UINT uUsage);
    What made you think that you needed to dereference hBitmap?
    Your Pixels allocation also seems to be contingent on the bitmap being 8bpp or less.

  3. #3
    Epo
    Epo is offline
    Registered User
    Join Date
    Jun 2003
    Posts
    361
    What made you think that you needed to dereference hBitmap?
    Sorry, I should've shown the whole function. It's actually a pointer to an HBITMAP that I'm using (which's been initialized with the code I showed before, in my main() function).
    Code:
    unsigned char * GetPixels(HBITMAP *hBitmap, BITMAPINFO *BitmapInfo)
    {
    	HDC hDC = GetDC(NULL);
    	if(hDC == NULL)
    		return NULL;
    
    	unsigned char *Pixels = (unsigned char *)malloc(BitmapInfo->bmiHeader.biWidth * 
    		BitmapInfo->bmiHeader.biHeight * sizeof(unsigned char));
    	if(Pixels == NULL)
    		return NULL;
    
    	if(GetDIBits(hDC, *hBitmap, 0, BitmapInfo->bmiHeader.biHeight, Pixels, BitmapInfo, DIB_RGB_COLORS) == 0)
    	{
    		printf("%ld ", GetLastError());
    		return NULL;
    	}
    
    	ReleaseDC(NULL, hDC);
    
    	return Pixels;
    }
    Your Pixels allocation also seems to be contingent on the bitmap being 8bpp or less.
    I didn't realize I was setting this restriction. I am trying to read the attributes of the Bitmap beforehand, and then use those to allocate the pixels. (I.e. setting up the BitmapInfo, etc., based off the values).

    This isn't really my strong suit, so I guess that's why I'm not seeing where I'm going wrong. Could you please explain which part of the code is causing the 8bpp restriction?
    Pentium 4 - 2.0GHz, 512MB RAM
    NVIDIA GeForce4 MX 440
    WinXP
    Visual Studio .Net 2003
    DX9 October 2004 Update (R.I.P. VC++ 6.0 Compatability)

  4. #4
    Registered /usr
    Join Date
    Aug 2001
    Location
    Newport, South Wales, UK
    Posts
    1,266
    Quote Originally Posted by Epo
    Sorry, I should've shown the whole function. It's actually a pointer to an HBITMAP that I'm using (which's been initialized with the code I showed before, in my main() function).
    Oh, right.

    As for the limitation:-
    Code:
    unsigned char *Pixels = (unsigned char *)malloc(BitmapInfo->bmiHeader.biWidth * 
    		BitmapInfo->bmiHeader.biHeight * sizeof(unsigned char));
    The key thing here is the "* sizeof(unsigned char)" bit. On most modern platforms sizeof(unsigned char) = 1 byte. So, you're allocating (width * height * 1) bytes of memory for your image. A byte is 8 bits. Can you see where I'm going here?

    To find out how many bytes you need per pixel:-
    Code:
    int iPixel = BitmapInfo->bmiHeader.biBitCount >> 3;
    Which shifts biBitCount to the right 3 times (Or divides by 8).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 09-23-2008, 04:32 AM
  2. pointer comparison failing
    By Bleech in forum C Programming
    Replies: 4
    Last Post: 08-11-2007, 07:33 PM
  3. CreateDevice failing
    By MadCow257 in forum Game Programming
    Replies: 6
    Last Post: 03-14-2006, 09:03 PM
  4. My library is failing and I cannot figure out why...
    By DerelictDream in forum C++ Programming
    Replies: 7
    Last Post: 08-15-2005, 04:47 PM
  5. initializes all components of failing to false
    By romeoz in forum C++ Programming
    Replies: 21
    Last Post: 08-01-2003, 10:30 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21