-//*****************************************************************************
-//* Function : ColorDDBToMonoDDB() *
-//* Purpose : Converts a color bitmap to a monochrome bitmap. *
-//* Parameters : HBITMAP hbm - The color bitmap. *
-//* Returns : A handle to a monochrome bitmap. *
-//*****************************************************************************
-HBITMAP ColorDDBToMonoDDB ( HBITMAP hbm)
-{ BITMAP bm;
- BITMAPINFOHEADER bi;
- LPBITMAPINFOHEADER lpbi;
- DWORD dwLen;
- HANDLE hdib;
- HANDLE h;
- HDC hdc;
- HBITMAP hbmMono;
-
- GetObject( hbm, sizeof( bm), (LPSTR )&bm);
-
- bi.biSize = sizeof( BITMAPINFOHEADER); // size of this structure
- bi.biWidth = bm.bmWidth; // bitmap width in pixels
- bi.biHeight = bm.bmHeight; // bitmap height in pixels
- bi.biPlanes = 1; // # of planes always 1 for DIBs
- bi.biBitCount = bm.bmPlanes * bm.bmBitsPixel; // color bits per pixel
- bi.biCompression = BI_RGB; // no compression
- bi.biSizeImage = 0; // 0 means default size
- bi.biXPelsPerMeter = 0; // not used
- bi.biYPelsPerMeter = 0; // not used
- bi.biClrUsed = 0; // 0 means default colors
- bi.biClrImportant = 0; // 0 means defaults
-
- dwLen = bi.biSize + PaletteSize((LPSTR)&bi);
-
- hdc = GetDC( (HWND) NULL);
-
- hdib = GlobalAlloc( GHND, dwLen);
- if (hdib == (HANDLE) NULL)
- {
- ReleaseDC( (HWND) NULL, hdc);
- return (HBITMAP) NULL;
- }
-
-#ifdef __WINDOWS_386__
- lpbi = (LPBITMAPINFOHEADER )MK_FP32(GlobalLock( hdib));
-#else
- lpbi = (LPBITMAPINFOHEADER )GlobalLock( hdib);
-#endif
-
- *lpbi = bi;
-
- // Call GetDIBits with a NULL lpBits parameter; it will calculate
- // the biSizeImage field.
- GetDIBits( hdc, hbm, 0, (WORD)bi.biHeight,
- NULL, (LPBITMAPINFO)lpbi, DIB_RGB_COLORS);
-
- bi = *lpbi;
- GlobalUnlock( hdib);
-
- // If the driver did not fill in the biSizeImage field, make one up.
- if(bi.biSizeImage == 0)
- bi.biSizeImage = WIDTHBYTES( (DWORD )bm.bmWidth * bi.biBitCount) * bm.bmHeight;
-
- // Reallocate the buffer big enough to hold all the bits.
- dwLen = bi.biSize + PaletteSize((LPSTR)&bi) + bi.biSizeImage;
- if( (h = GlobalReAlloc( hdib, dwLen, 0)) != 0)
- hdib = h;
- else
- {
- GlobalFree( hdib);
- ReleaseDC( (HWND) NULL, hdc);
- return (HBITMAP) NULL;
- }
-
- // Call GetDIBits with a NON-NULL lpBits parameter, to actually
- // get the bits this time.
-
-#ifdef __WINDOWS_386__
- lpbi = (LPBITMAPINFOHEADER )MK_FP32(GlobalLock( hdib));
-#else
- lpbi = (LPBITMAPINFOHEADER )GlobalLock( hdib);
-#endif
-
- if( GetDIBits( hdc, hbm, 0, (WORD)bi.biHeight,
- (LPSTR)lpbi + (WORD)lpbi->biSize + PaletteSize((LPSTR)lpbi),
- (LPBITMAPINFO)lpbi, DIB_RGB_COLORS) == 0)
- {
- GlobalUnlock( hdib);
- hdib = (HANDLE) NULL;
- ReleaseDC( (HWND) NULL, hdc);
- return (HBITMAP) NULL;
- }
-
- // Finally, create a monochrome DDB, and put the DIB into
- // it. SetDIBits does smart color conversion.
- hbmMono = CreateBitmap((WORD)lpbi->biWidth, (WORD)lpbi->biHeight, 1, 1, NULL);
- SetDIBits( hdc, hbmMono, (WORD)0, (WORD)lpbi->biHeight,
- (LPSTR)lpbi + (int )lpbi->biSize + PaletteSize((LPSTR)lpbi),
- (LPBITMAPINFO)lpbi, DIB_RGB_COLORS);
-
- bi = *lpbi;
- GlobalUnlock( hdib);
- GlobalFree( hdib);
-
- ReleaseDC((HWND) NULL, hdc);
- return hbmMono;
-}
-