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

  • Live Event Date: October 29, 2014 @ 11:00 a.m. ET / 8:00 a.m. PT Are you interested in building a cognitive application using the power of IBM Watson? Need a platform that provides speed and ease for rapidly deploying this application? Join Chris Madison, Watson Solution Architect, as he walks through the process of building a Watson powered application on IBM Bluemix. Chris will talk about the new Watson Services just released on IBM bluemix, but more importantly he will do a step by step cognitive …

  • In support of their business continuity and disaster recovery plans, many midsized companies endeavor to avoid putting all their eggs in one basket. Understanding the critical role of last-mile connectivity and always available Internet access for their enterprises, savvy firms utilize redundant connections from multiple service providers. Despite the good intentions, their Internet connectivity risk may still be in a single basket. That is because internet service providers (ISPs) and competitive local …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds