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

  • The mobile market is white hot. Building a well-crafted product development plan that addresses market research, strategy, design, and development will provide the greatest chance for success. Each phase of an app's lifecycle is critical to the its overall success and feeds into the next step of the process of product development for the app. This white paper examines the five key phases of mobile app creation, one by one, to understand how they work together to help create a successful mobile app.

  • Companies undertaking an IT project need to find the right balance between cost and functionality. It's important to start by determining whether to build a solution from scratch, buy an out-of-the-box solution, or a combination of both. In reality, most projects will require some system tailoring to meet business requirements. Decision-makers must understand how much software development is enough and craft a detailed implementation plan to ensure the project's success. This white paper examines the different …

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date