Converting DIB to DDB


You can render a device-independent bitmap (DIB) onto a device without having to convert it into a device-dependent bitmap (DDB). However, the process is slower when using a DIB and you also don't have access to few very versatile functions such as BitBlt() which handle only DDBs.

The basic steps involved in creating a device-dependent bitmap from a device-independent bitmap are:

  1. Create a logical palette from the information in the DIB color table. You need to do this only if the device supports palettes. To create the palette, allocate enough memory for a LOGPALETTE structure with memory for the color table as well. Initialize the palVersion and palNumEntries fields and copy the color values from the color table in the DIB. Then call CreatePalette() using the LOGPALETTE structure we initialized.
  2. Select the logical palette into the device for which we are creating the DDB, then realize the palette.
  3. Create the DDB using the CreateDIBitmap() function.
  4. Don't forget to release any memory allocated for the LOGPALETTE structure.

Although this function creates a logical palette, it doesn't return this information to the calling code. If the DIB represents a 256 color bitmap and the device supports only 256 colors, then rendering the DDB onto the device will probably not result in the proper image. That's because the system palette colors may not match the colors that are being used by the bitmap. You may, therefore, want to modify the function so that it returns the logical palette as well. You would have to select and realize this palette onto the device context before drawing the bitmap image.

HBITMAP DIBToDDB( HANDLE hDIB )
{
	LPBITMAPINFOHEADER	lpbi;
	HBITMAP 		hbm;
	CPalette		pal;
	CPalette*		pOldPal;
	CClientDC		dc(NULL);

	if (hDIB == NULL)
		return NULL;

	lpbi = (LPBITMAPINFOHEADER)hDIB;

	int nColors = lpbi->biClrUsed ? lpbi->biClrUsed : 
						1 << lpbi->biBitCount;

	BITMAPINFO &bmInfo = *(LPBITMAPINFO)hDIB ;
	LPVOID lpDIBBits;
	if( bmInfo.bmiHeader.biBitCount > 8 )
		lpDIBBits = (LPVOID)((LPDWORD)(bmInfo.bmiColors + 
			bmInfo.bmiHeader.biClrUsed) + 
			((bmInfo.bmiHeader.biCompression == BI_BITFIELDS) ? 3 : 0));
	else
		lpDIBBits = (LPVOID)(bmInfo.bmiColors + nColors);

	// Create and select a logical palette if needed
	if( nColors <= 256 && dc.GetDeviceCaps(RASTERCAPS) & RC_PALETTE)
	{
		UINT nSize = sizeof(LOGPALETTE) + (sizeof(PALETTEENTRY) * nColors);
		LOGPALETTE *pLP = (LOGPALETTE *) new BYTE[nSize];

		pLP->palVersion = 0x300;
		pLP->palNumEntries = nColors;

		for( int i=0; i < nColors; i++)
		{
			pLP->palPalEntry[i].peRed = bmInfo.bmiColors[i].rgbRed;
			pLP->palPalEntry[i].peGreen = bmInfo.bmiColors[i].rgbGreen;
			pLP->palPalEntry[i].peBlue = bmInfo.bmiColors[i].rgbBlue;
			pLP->palPalEntry[i].peFlags = 0;
		}

		pal.CreatePalette( pLP );

		delete[] pLP;

		// Select and realize the palette
		pOldPal = dc.SelectPalette( &pal, FALSE );
		dc.RealizePalette();
	}

	
	hbm = CreateDIBitmap(dc.GetSafeHdc(),		// handle to device context
			(LPBITMAPINFOHEADER)lpbi,	// pointer to bitmap info header 
			(LONG)CBM_INIT,			// initialization flag
			lpDIBBits,			// pointer to initialization data 
			(LPBITMAPINFO)lpbi,		// pointer to bitmap info
			DIB_RGB_COLORS );		// color-data usage 

	if (pal.GetSafeHandle())
		dc.SelectPalette(pOldPal,FALSE);

	return hbm;
}



Comments

  • Problem with hdib parameter

    Posted by Legacy on 02/19/2004 12:00am

    Originally posted by: SteH

    Is it only me? When debugging this code the lpbi-> values are all rubbish since hdib as I have it is a double pointer. That means the line 
    
    lpbi = (LPBITMAPINFOHEADER)hdib;
    should read
    lpbi = (LPBITMAPINFOHEADER)(*hdib);

    Reply
  • how to use API loadresource

    Posted by Legacy on 04/18/2001 12:00am

    Originally posted by: Nance

    from MSDN, i use the function "loadresource",but the return handle is always NULL, i can't understand why? Thanks

    Reply
  • Converting DIB to DDB

    Posted by Legacy on 02/11/1999 12:00am

    Originally posted by: Ted Shaneyfelt

    hDIB is being used as a pointer, but it's declared as a HANDLE.
    
    

    HBITMAP DIBToDDB( HANDLE hDIB )
    {
    LPBITMAPINFO lpbi;
    <snip>
    lpbi = (LPBITMAPINFO)hDIB; // Wrong???

    Perhaps the function should be coded either to take a HANDLE
    to an object allocated by GlobalAlloc or the function should
    be declared to take a pointer, because that's what it expects.
    Here's how to declare it to take a pointer:

    HBITMAP DIBToDDB( void *pDIB )
    {
    LPBITMAPINFO lpbi;
    <snip>
    lpbi = (LPBITMAPINFO)pDIB; // Ok

    Here's how to declare it to take a handle:

    HBITMAP DIBToDDB( HANDLE hDIB )
    LPBITMAPINFO lpbi;
    <snip>
    lpbi = (LPBITMAPINFO)GlobalLock(hDIB); // Ok
    <snip>
    GlobalUnlock( hDIB ); // Done with lpbi

    Reply
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • On-demand Event Event Date: October 29, 2014 It's well understood how critical version control is for code. However, its importance to DevOps isn't always recognized. The 2014 DevOps Survey of Practice shows that one of the key predictors of DevOps success is putting all production environment artifacts into version control. In this webcast, Gene Kim discusses these survey findings and shares woeful tales of artifact management gone wrong! Gene also shares examples of how high-performing DevOps …

  • Live Event Date: December 11, 2014 @ 1:00 p.m. ET / 10:00 a.m. PT Market pressures to move more quickly and develop innovative applications are forcing organizations to rethink how they develop and release applications. The combination of public clouds and physical back-end infrastructures are a means to get applications out faster. However, these hybrid solutions complicate DevOps adoption, with application delivery pipelines that span across complex hybrid cloud and non-cloud environments. Check out this …

Most Popular Programming Stories

More for Developers

RSS Feeds