- hLogPal = CreatePalette((LPLOGPALETTE)npPal);
- free(npPal);
-
- return(hLogPal);
- }
-
- /* 24-bit DIB with no color table. Return default palette. Another
- * option would be to create a 256 color "rainbow" palette to provide
- * some good color choices.
- */
- else
- return((HPALETTE) GetStockObject(DEFAULT_PALETTE));
-}
-
-/*
- *
- * Function: InitBitmapInfoHeader
- *
- * Purpose: Does a "standard" initialization of a BITMAPINFOHEADER,
- * given the Width, Height, and Bits per Pixel for the
- * DIB.
- *
- * By standard, I mean that all the relevant fields are set
- * to the specified values. biSizeImage is computed, the
- * biCompression field is set to "no compression," and all
- * other fields are 0.
- *
- * Note that DIBs only allow BitsPixel values of 1, 4, 8, or
- * 24. This routine makes sure that one of these values is
- * used (whichever is most appropriate for the specified
- * nBPP).
- *
- * Parms: lpBmInfoHdr == Far pointer to a BITMAPINFOHEADER structure
- * to be filled in.
- * dwWidth == Width of DIB (not in Win 3.0 & 3.1, high
- * word MUST be 0).
- * dwHeight == Height of DIB (not in Win 3.0 & 3.1, high
- * word MUST be 0).
- * nBPP == Bits per Pixel for the DIB.
- *
- */
-
-static void InitBitmapInfoHeader (LPBITMAPINFOHEADER lpBmInfoHdr,
- DWORD dwWidth,
- DWORD dwHeight,
- int nBPP)
-{
- // _fmemset (lpBmInfoHdr, 0, sizeof (BITMAPINFOHEADER));
- memset (lpBmInfoHdr, 0, sizeof (BITMAPINFOHEADER));
-
- lpBmInfoHdr->biSize = sizeof (BITMAPINFOHEADER);
- lpBmInfoHdr->biWidth = dwWidth;
- lpBmInfoHdr->biHeight = dwHeight;
- lpBmInfoHdr->biPlanes = 1;
-
- if (nBPP <= 1)
- nBPP = 1;
- else if (nBPP <= 4)
- nBPP = 4;
- else if (nBPP <= 8)
- nBPP = 8;
- /* Doesn't work
- else if (nBPP <= 16)
- nBPP = 16;
- */
- else
- nBPP = 24;
-
- lpBmInfoHdr->biBitCount = nBPP;
- lpBmInfoHdr->biSizeImage = WIDTHBYTES (dwWidth * nBPP) * dwHeight;
-}
-
-LPSTR wxFindDIBBits (LPSTR lpbi)
-{
- return (lpbi + *(LPDWORD)lpbi + wxPaletteSize (lpbi));
-}
-
-/*
- * Function: BitmapToDIB
- *
- * Purpose: Given a device dependent bitmap and a palette, returns
- * a handle to global memory with a DIB spec in it. The
- * DIB is rendered using the colors of the palette passed in.
- *
- * Parms: hBitmap == Handle to device dependent bitmap compatible
- * with default screen display device.
- * hPal == Palette to render the DDB with. If it's NULL,
- * use the default palette.
- */
-
-HANDLE wxBitmapToDIB (HBITMAP hBitmap, HPALETTE hPal)
-{
- BITMAP Bitmap;
- BITMAPINFOHEADER bmInfoHdr;
- LPBITMAPINFOHEADER lpbmInfoHdr;
- LPSTR lpBits;
- HDC hMemDC;
- HANDLE hDIB;
- HPALETTE hOldPal = NULL;
-
- // Do some setup -- make sure the Bitmap passed in is valid,
- // get info on the bitmap (like its height, width, etc.),
- // then setup a BITMAPINFOHEADER.
-
- if (!hBitmap)
- return NULL;
-
- if (!GetObject (hBitmap, sizeof (Bitmap), (LPSTR) &Bitmap))
- return NULL;
-
- InitBitmapInfoHeader (&bmInfoHdr,
- Bitmap.bmWidth,
- Bitmap.bmHeight,
- Bitmap.bmPlanes * Bitmap.bmBitsPixel);
-
- // Now allocate memory for the DIB. Then, set the BITMAPINFOHEADER
- // into this memory, and find out where the bitmap bits go.
-
- hDIB = GlobalAlloc (GHND, sizeof (BITMAPINFOHEADER) +
- wxPaletteSize ((LPSTR) &bmInfoHdr) + bmInfoHdr.biSizeImage);
-
- if (!hDIB)
- return NULL;
-
- lpbmInfoHdr = (LPBITMAPINFOHEADER) GlobalLock (hDIB);
-
- *lpbmInfoHdr = bmInfoHdr;
- lpBits = wxFindDIBBits ((LPSTR) lpbmInfoHdr);
-
-
- // Now, we need a DC to hold our bitmap. If the app passed us
- // a palette, it should be selected into the DC.
-
- hMemDC = GetDC (NULL);
-
- if (hPal)
- {
- hOldPal = SelectPalette (hMemDC, hPal, FALSE);
- RealizePalette (hMemDC);
- }
-
- // We're finally ready to get the DIB. Call the driver and let
- // it party on our bitmap. It will fill in the color table,
- // and bitmap bits of our global memory block.