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: September 10, 2014 Modern mobile applications connect systems-of-engagement (mobile apps) with systems-of-record (traditional IT) to deliver new and innovative business value. But the lifecycle for development of mobile apps is also new and different. Emerging trends in mobile development call for faster delivery of incremental features, coupled with feedback from the users of the app "in the wild." This loop of continuous delivery and continuous feedback is how the best mobile …

  • Java developers know that testing code changes can be a huge pain, and waiting for an application to redeploy after a code fix can take an eternity. Wouldn't it be great if you could see your code changes immediately, fine-tune, debug, explore and deploy code without waiting for ages? In this white paper, find out how that's possible with a Java plugin that drastically changes the way you develop, test and run Java applications. Discover the advantages of this plugin, and the changes you can expect to see …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds