1 /*******************************************************************************
5 * DESCRIPTION : Routines for dealing with Device Independent Bitmaps. *
9 * wxReadDIB() - Reads a DIB *
11 * WriteDIB() - Writes a global handle in CF_DIB format*
14 * wxPaletteSize() - Calculates the palette size in bytes *
17 * DibNumColors() - Determines the number of colors in DIB *
19 * DibFromBitmap() - Creates a DIB repr. the DDB passed in. *
22 * lread() - Private routine to read more than 64k *
24 * lwrite() - Private routine to write more than 64k *
26 *******************************************************************************/
28 // For compilers that support precompilation, includes "wx.h".
29 #include "wx/wxprec.h"
31 #if defined(__BORLANDC__)
36 #include "wx/bitmap.h"
45 #if !defined(__MWERKS__) && !defined(__SALFORDC__)
49 #include "wx/msw/dib.h"
51 #ifdef __GNUWIN32_OLD__
52 #include "wx/msw/gnuwin32/extra.h"
56 /* flags for _lseek */
62 #define MAXREAD 32768 /* Number of bytes to be read during */
63 /* each read operation. */
65 /* Header signatutes for various resources */
66 #define BFT_ICON 0x4349 /* 'IC' */
67 #define BFT_BITMAP 0x4d42 /* 'BM' */
68 #define BFT_CURSOR 0x5450 /* 'PT(' */
70 /* macro to determine if resource is a DIB */
71 #define ISDIB(bft) ((bft) == BFT_BITMAP)
73 /* Macro to align given value to the closest DWORD (unsigned long ) */
74 #define ALIGNULONG(i) ((i+3)/4*4)
76 /* Macro to determine to round off the given value to the closest byte */
77 #define WIDTHBYTES(i) ((i+31)/32*4)
79 #define PALVERSION 0x300
80 #define MAXPALETTE 256 /* max. # supported palette entries */
82 static DWORD PASCAL
lread(int fh
, VOID FAR
*pv
, DWORD ul
);
83 static DWORD PASCAL
lwrite(int fh
, VOID FAR
*pv
, DWORD ul
);
85 static BOOL
WriteDIB (LPTSTR szFile
,HANDLE hdib
);
86 WORD
wxPaletteSize (VOID FAR
* pv
); // This is non-static as some apps use it externally
87 static WORD
DibNumColors (VOID FAR
* pv
);
88 // HANDLE DibFromBitmap (HBITMAP hbm, DWORD biStyle, WORD biBits, HPALETTE hpal);
89 static BOOL PASCAL
MakeBitmapAndPalette(HDC
,HANDLE
,HPALETTE
*,HBITMAP
*);
91 /****************************************************************************
93 * FUNCTION : WriteDIB(LPSTR szFile,HANDLE hdib) *
95 * PURPOSE : Write a global handle in CF_DIB format to a file. *
97 * RETURNS : TRUE - if successful. *
100 ****************************************************************************/
102 static BOOL
WriteDIB(LPTSTR szFile
, HANDLE hdib
)
104 BITMAPFILEHEADER hdr
;
105 LPBITMAPINFOHEADER lpbi
;
112 fh
= OpenFile(wxFNCONV(szFile
), &of
, OF_CREATE
| OF_READWRITE
);
116 #ifdef __WINDOWS_386__
117 lpbi
= (LPBITMAPINFOHEADER
) MK_FP32(GlobalLock(hdib
));
119 lpbi
= (LPBITMAPINFOHEADER
) GlobalLock(hdib
);
121 /* Fill in the fields of the file header */
122 hdr
.bfType
= BFT_BITMAP
;
123 hdr
.bfSize
= GlobalSize(hdib
) + sizeof(BITMAPFILEHEADER
);
126 hdr
.bfOffBits
= (DWORD
) sizeof(BITMAPFILEHEADER
) + lpbi
->biSize
+
129 /* Write the file header */
130 _lwrite(fh
, (LPSTR
) &hdr
, sizeof(BITMAPFILEHEADER
));
132 /* Write the DIB header and the bits */
133 lwrite(fh
, (LPSTR
) lpbi
, GlobalSize(hdib
));
140 /****************************************************************************
142 * FUNCTION : wxPaletteSize(VOID FAR * pv) *
144 * PURPOSE : Calculates the palette size in bytes. If the info. block *
145 * is of the BITMAPCOREHEADER type, the number of colors is *
146 * multiplied by 3 to give the palette size, otherwise the *
147 * number of colors is multiplied by 4. *
149 * RETURNS : Palette size in number of bytes. *
151 ****************************************************************************/
153 WORD
wxPaletteSize(VOID FAR
* pv
)
155 LPBITMAPINFOHEADER lpbi
;
158 lpbi
= (LPBITMAPINFOHEADER
) pv
;
159 NumColors
= DibNumColors(lpbi
);
161 if (lpbi
->biSize
== sizeof(BITMAPCOREHEADER
))
162 return NumColors
* sizeof(RGBTRIPLE
);
164 return NumColors
* sizeof(RGBQUAD
);
167 /****************************************************************************
169 * FUNCTION : DibNumColors(VOID FAR * pv) *
171 * PURPOSE : Determines the number of colors in the DIB by looking at *
172 * the BitCount filed in the info block. *
174 * RETURNS : The number of colors in the DIB. *
176 ****************************************************************************/
178 static WORD
DibNumColors(VOID FAR
*pv
)
181 BITMAPINFOHEADER
*lpbi
;
182 BITMAPCOREHEADER
*lpbc
;
184 lpbi
= ((BITMAPINFOHEADER
*) pv
);
185 lpbc
= ((BITMAPCOREHEADER
*) pv
);
187 /* With the BITMAPINFO format headers, the size of the palette
188 * is in biClrUsed, whereas in the BITMAPCORE - style headers, it
189 * is dependent on the bits per pixel ( = 2 raised to the power of
192 if (lpbi
->biSize
!= sizeof(BITMAPCOREHEADER
)) {
193 if (lpbi
->biClrUsed
!= 0)
194 return (WORD
) lpbi
->biClrUsed
;
195 bits
= lpbi
->biBitCount
;
198 bits
= lpbc
->bcBitCount
;
208 /* A 24 bitcount DIB has no color table */
213 /****************************************************************************
215 * FUNCTION : DibFromBitmap() *
217 * PURPOSE : Will create a global memory block in DIB format that *
218 * represents the Device-dependent bitmap (DDB) passed in. *
220 * RETURNS : A handle to the DIB *
222 ****************************************************************************/
225 static HANDLE
DibFromBitmap(HBITMAP hbm
, DWORD biStyle
, WORD biBits
, HPALETTE hpal
)
229 BITMAPINFOHEADER FAR
*lpbi
;
239 hpal
= GetStockObject(DEFAULT_PALETTE
);
241 GetObject(hbm
, sizeof (bm
), (LPSTR
) &bm
);
244 biBits
= bm
.bmPlanes
* bm
.bmBitsPixel
;
246 bi
.biSize
= sizeof(BITMAPINFOHEADER
);
247 bi
.biWidth
= bm
.bmWidth
;
248 bi
.biHeight
= bm
.bmHeight
;
250 bi
.biBitCount
= biBits
;
251 bi
.biCompression
= biStyle
;
253 bi
.biXPelsPerMeter
= 0;
254 bi
.biYPelsPerMeter
= 0;
256 bi
.biClrImportant
= 0;
258 dwLen
= bi
.biSize
+ wxPaletteSize(&bi
);
260 hdc
= GetDC((HWND
) NULL
);
261 hpal
= SelectPalette(hdc
, hpal
, FALSE
);
264 hdib
= GlobalAlloc(GHND
, dwLen
);
267 SelectPalette(hdc
, hpal
, FALSE
);
268 ReleaseDC(NULL
, hdc
);
272 #ifdef __WINDOWS_386__
273 lpbi
= (BITMAPINFOHEADER FAR
*) MK_FP32(GlobalLock(hdib
));
275 lpbi
= (BITMAPINFOHEADER FAR
*) GlobalLock(hdib
);
280 /* call GetDIBits with a NULL lpBits param, so it will calculate the
281 * biSizeImage field for us
283 GetDIBits(hdc
, hbm
, 0, (WORD
) bi
.biHeight
,
284 NULL
, (LPBITMAPINFO
) lpbi
, DIB_RGB_COLORS
);
289 /* If the driver did not fill in the biSizeImage field, make one up */
290 if (bi
.biSizeImage
== 0) {
291 bi
.biSizeImage
= WIDTHBYTES((DWORD
)bm
.bmWidth
* biBits
) * bm
.bmHeight
;
293 if (biStyle
!= BI_RGB
)
294 bi
.biSizeImage
= (bi
.biSizeImage
* 3) / 2;
297 /* realloc the buffer big enough to hold all the bits */
298 dwLen
= bi
.biSize
+ wxPaletteSize(&bi
) + bi
.biSizeImage
;
299 if (h
= GlobalReAlloc(hdib
, dwLen
, 0))
305 SelectPalette(hdc
, hpal
, FALSE
);
306 ReleaseDC(NULL
, hdc
);
310 /* call GetDIBits with a NON-NULL lpBits param, and actualy get the
313 #ifdef __WINDOWS_386__
314 lpbi
= (BITMAPINFOHEADER FAR
*) MK_FP32(GlobalLock(hdib
));
316 lpbi
= (BITMAPINFOHEADER FAR
*) GlobalLock(hdib
);
323 (LPSTR
) lpbi
+ (WORD
) lpbi
->biSize
+ wxPaletteSize(lpbi
),
324 (LPBITMAPINFO
) lpbi
, DIB_RGB_COLORS
) == 0) {
327 SelectPalette(hdc
, hpal
, FALSE
);
328 ReleaseDC((HWND
) NULL
, hdc
);
335 SelectPalette(hdc
, hpal
, FALSE
);
336 ReleaseDC(NULL
, hdc
);
341 /************* PRIVATE ROUTINES TO READ/WRITE MORE THAN 64K ***************/
342 /****************************************************************************
344 * FUNCTION : lread(int fh, VOID FAR *pv, DWORD ul) *
346 * PURPOSE : Reads data in steps of 32k till all the data has been read.*
348 * RETURNS : 0 - If read did not proceed correctly. *
349 * number of bytes read otherwise. *
351 ****************************************************************************/
353 static DWORD PASCAL
lread(int fh
, void far
*pv
, DWORD ul
)
356 #if defined(WINNT) || defined(__WIN32__) || defined(__WIN32__) || defined(__WXWINE__)
357 BYTE
*hp
= (BYTE
*) pv
;
359 BYTE huge
*hp
= (BYTE huge
*) pv
;
361 while (ul
> (DWORD
) MAXREAD
) {
362 if (_lread(fh
, (LPSTR
) hp
, (WORD
) MAXREAD
) != MAXREAD
)
367 if (_lread(fh
, (LPSTR
) hp
, (WORD
) ul
) != (WORD
) ul
)
372 /****************************************************************************
374 * FUNCTION : lwrite(int fh, VOID FAR *pv, DWORD ul) *
376 * PURPOSE : Writes data in steps of 32k till all the data is written. *
378 * RETURNS : 0 - If write did not proceed correctly. *
379 * number of bytes written otherwise. *
381 ****************************************************************************/
383 static DWORD PASCAL
lwrite(int fh
, VOID FAR
*pv
, DWORD ul
)
386 #if defined(WINNT) || defined(__WIN32__) || defined(__WIN32__) || defined(__WXWINE__)
387 BYTE
*hp
= (BYTE
*) pv
;
389 BYTE huge
*hp
= (BYTE huge
*) pv
;
391 while (ul
> MAXREAD
) {
392 if (_lwrite(fh
, (LPSTR
) hp
, (WORD
) MAXREAD
) != MAXREAD
)
397 if (_lwrite(fh
, (LPSTR
) hp
, (WORD
) ul
) != (WORD
) ul
)
402 /****************************************************************************
404 * FUNCTION : ReadDIB(hWnd)
406 * PURPOSE : Reads a DIB from a file, obtains a handle to its
407 * BITMAPINFO struct. and loads the DIB. Once the DIB
408 * is loaded, the function also creates a bitmap and
409 * palette out of the DIB for a device-dependent form.
411 * RETURNS : TRUE - DIB loaded and bitmap/palette created
412 * The DIBINIT structure pointed to by pInfo is
413 * filled with the appropriate handles.
416 ****************************************************************************/
417 BOOL
wxReadDIB(LPTSTR lpFileName
, HBITMAP
*bitmap
, HPALETTE
*palette
)
420 LPBITMAPINFOHEADER lpbi
;
427 BOOL bCoreHead
= FALSE
;
430 /* Open the file and get a handle to it's BITMAPINFO */
432 fh
= OpenFile (wxFNCONV(lpFileName
), &of
, OF_READ
);
434 wxLogError(_("Can't open file '%s'"), lpFileName
);
438 hDIB
= GlobalAlloc(GHND
, (DWORD
)(sizeof(BITMAPINFOHEADER
) +
439 256 * sizeof(RGBQUAD
)));
443 #ifdef __WINDOWS_386__
444 lpbi
= (LPBITMAPINFOHEADER
)MK_FP32(GlobalLock(hDIB
));
446 lpbi
= (LPBITMAPINFOHEADER
)GlobalLock(hDIB
);
449 /* read the BITMAPFILEHEADER */
450 if (sizeof (bf
) != _lread (fh
, (LPSTR
)&bf
, sizeof (bf
)))
453 if (bf
.bfType
!= 0x4d42) /* 'BM' */
456 if (sizeof(BITMAPCOREHEADER
) != _lread (fh
, (LPSTR
)lpbi
, sizeof(BITMAPCOREHEADER
)))
459 if (lpbi
->biSize
== sizeof(BITMAPCOREHEADER
))
461 lpbi
->biSize
= sizeof(BITMAPINFOHEADER
);
462 lpbi
->biBitCount
= ((LPBITMAPCOREHEADER
)lpbi
)->bcBitCount
;
463 lpbi
->biPlanes
= ((LPBITMAPCOREHEADER
)lpbi
)->bcPlanes
;
464 lpbi
->biHeight
= ((LPBITMAPCOREHEADER
)lpbi
)->bcHeight
;
465 lpbi
->biWidth
= ((LPBITMAPCOREHEADER
)lpbi
)->bcWidth
;
470 // get to the start of the header and read INFOHEADER
471 _llseek(fh
,sizeof(BITMAPFILEHEADER
),SEEK_SET
);
472 if (sizeof(BITMAPINFOHEADER
) != _lread (fh
, (LPSTR
)lpbi
, sizeof(BITMAPINFOHEADER
)))
476 nNumColors
= (WORD
)lpbi
->biClrUsed
;
477 if ( nNumColors
== 0 )
479 /* no color table for 24-bit, default size otherwise */
480 if (lpbi
->biBitCount
!= 24)
481 nNumColors
= 1 << lpbi
->biBitCount
; /* standard size table */
484 /* fill in some default values if they are zero */
485 if (lpbi
->biClrUsed
== 0)
486 lpbi
->biClrUsed
= nNumColors
;
488 if (lpbi
->biSizeImage
== 0)
490 lpbi
->biSizeImage
= ((((lpbi
->biWidth
* (DWORD
)lpbi
->biBitCount
) + 31) & ~31) >> 3)
494 /* get a proper-sized buffer for header, color table and bits */
496 hDIB
= GlobalReAlloc(hDIB
, lpbi
->biSize
+
497 nNumColors
* sizeof(RGBQUAD
) +
498 lpbi
->biSizeImage
, 0);
499 if (!hDIB
) /* can't resize buffer for loading */
502 #ifdef __WINDOWS_386__
503 lpbi
= (LPBITMAPINFOHEADER
)MK_FP32(GlobalLock(hDIB
));
505 lpbi
= (LPBITMAPINFOHEADER
)GlobalLock(hDIB
);
508 /* read the color table */
510 _lread(fh
, (LPSTR
)(lpbi
) + lpbi
->biSize
, nNumColors
* sizeof(RGBQUAD
));
515 RGBTRIPLE FAR
*pTriple
;
517 _lread(fh
, (LPSTR
)(lpbi
) + lpbi
->biSize
, nNumColors
* sizeof(RGBTRIPLE
));
519 pQuad
= (RGBQUAD FAR
*)((LPSTR
)lpbi
+ lpbi
->biSize
);
520 pTriple
= (RGBTRIPLE FAR
*) pQuad
;
521 for (i
= nNumColors
- 1; i
>= 0; i
--)
523 pQuad
[i
].rgbRed
= pTriple
[i
].rgbtRed
;
524 pQuad
[i
].rgbBlue
= pTriple
[i
].rgbtBlue
;
525 pQuad
[i
].rgbGreen
= pTriple
[i
].rgbtGreen
;
526 pQuad
[i
].rgbReserved
= 0;
530 /* offset to the bits from start of DIB header */
531 offBits
= (WORD
)lpbi
->biSize
+ nNumColors
* sizeof(RGBQUAD
);
533 if (bf
.bfOffBits
!= 0L)
535 _llseek(fh
,bf
.bfOffBits
,SEEK_SET
);
538 if (lpbi
->biSizeImage
== lread(fh
, (LPSTR
)lpbi
+ offBits
, lpbi
->biSizeImage
))
543 if (!MakeBitmapAndPalette(hDC
, hDIB
, palette
,
568 /****************************************************************************
570 * FUNCTION : MakeBitmapAndPalette
572 * PURPOSE : Given a DIB, creates a bitmap and corresponding palette
573 * to be used for a device-dependent representation of
576 * RETURNS : TRUE --> success. phPal and phBitmap are filled with
577 * appropriate handles. Caller is responsible
578 * for freeing objects.
579 * FALSE --> unable to create objects. both pointer are
582 ****************************************************************************/
583 static BOOL PASCAL
MakeBitmapAndPalette(HDC hDC
, HANDLE hDIB
,
584 HPALETTE
* phPal
, HBITMAP
* phBitmap
)
586 LPBITMAPINFOHEADER lpInfo
;
589 HPALETTE hPalette
, hOldPal
;
592 #ifdef __WINDOWS_386__
593 lpInfo
= (LPBITMAPINFOHEADER
) MK_FP32(GlobalLock(hDIB
));
595 lpInfo
= (LPBITMAPINFOHEADER
) GlobalLock(hDIB
);
598 hPalette
= wxMakeDIBPalette(lpInfo
);
601 // Need to realize palette for converting DIB to bitmap.
602 hOldPal
= SelectPalette(hDC
, hPalette
, TRUE
);
605 lpBits
= (LPSTR
)lpInfo
+ (WORD
)lpInfo
->biSize
+
606 (WORD
)lpInfo
->biClrUsed
* sizeof(RGBQUAD
);
607 hBitmap
= CreateDIBitmap(hDC
, lpInfo
, CBM_INIT
, lpBits
,
608 (LPBITMAPINFO
)lpInfo
, DIB_RGB_COLORS
);
610 SelectPalette(hDC
, hOldPal
, TRUE
);
614 DeleteObject(hPalette
);
625 /****************************************************************************
627 * FUNCTION : wxMakeDIBPalette(lpInfo) *
629 * PURPOSE : Given a BITMAPINFOHEADER, create a palette based on
633 * RETURNS : non-zero - handle of a corresponding palette
634 * zero - unable to create palette
636 ****************************************************************************/
637 HPALETTE
wxMakeDIBPalette(LPBITMAPINFOHEADER lpInfo
)
647 /* since biClrUsed field was filled during the loading of the DIB,
648 ** we know it contains the number of colors in the color table.
650 if (lpInfo
->biClrUsed
)
653 npPal = (NPLOGPALETTE)LocalAlloc(LMEM_FIXED, sizeof(LOGPALETTE) +
654 (WORD)lpInfo->biClrUsed * sizeof(PALETTEENTRY));
656 npPal
= (LPLOGPALETTE
)malloc(sizeof(LOGPALETTE
) +
657 (WORD
)lpInfo
->biClrUsed
* sizeof(PALETTEENTRY
));
661 npPal
->palVersion
= 0x300;
662 npPal
->palNumEntries
= (WORD
)lpInfo
->biClrUsed
;
664 /* get pointer to the color table */
665 lpRGB
= (RGBQUAD FAR
*)((LPSTR
)lpInfo
+ lpInfo
->biSize
);
667 /* copy colors from the color table to the LogPalette structure */
668 for (i
= 0; i
< lpInfo
->biClrUsed
; i
++, lpRGB
++)
670 npPal
->palPalEntry
[i
].peRed
= lpRGB
->rgbRed
;
671 npPal
->palPalEntry
[i
].peGreen
= lpRGB
->rgbGreen
;
672 npPal
->palPalEntry
[i
].peBlue
= lpRGB
->rgbBlue
;
673 npPal
->palPalEntry
[i
].peFlags
= 0;
676 hLogPal
= CreatePalette((LPLOGPALETTE
)npPal
);
677 // LocalFree((HANDLE)npPal);
683 /* 24-bit DIB with no color table. return default palette. Another
684 ** option would be to create a 256 color "rainbow" palette to provide
685 ** some good color choices.
688 return((HPALETTE
) GetStockObject(DEFAULT_PALETTE
));
693 bool wxLoadIntoBitmap(wxChar
*filename
, wxBitmap
*bitmap
, wxPalette
**pal
)
698 bool success
= (wxReadDIB(filename
, &hBitmap
, &hPalette
) != 0);
702 DeleteObject(hPalette
);
710 *pal
= new wxPalette
;
711 (*pal
)->SetHPALETTE((WXHPALETTE
) hPalette
);
714 DeleteObject(hPalette
);
722 GetObject(hBitmap
, sizeof(bm
), (LPSTR
)&bm
);
724 bitmap
->SetHBITMAP((WXHBITMAP
) hBitmap
);
725 bitmap
->SetWidth(bm
.bmWidth
);
726 bitmap
->SetHeight(bm
.bmHeight
);
727 bitmap
->SetDepth(bm
.bmPlanes
* bm
.bmBitsPixel
);
728 #if WXWIN_COMPATIBILITY_2
730 #endif // WXWIN_COMPATIBILITY_2
736 wxBitmap
*wxLoadBitmap(wxChar
*filename
, wxPalette
**pal
)
738 wxBitmap
*bitmap
= new wxBitmap
;
739 if (wxLoadIntoBitmap(filename
, bitmap
, pal
))
748 //---------------------------------------------------------------------
750 // Function: InitBitmapInfoHeader
752 // Purpose: Does a "standard" initialization of a BITMAPINFOHEADER,
753 // given the Width, Height, and Bits per Pixel for the
756 // By standard, I mean that all the relevant fields are set
757 // to the specified values. biSizeImage is computed, the
758 // biCompression field is set to "no compression," and all
759 // other fields are 0.
761 // Note that DIBs only allow BitsPixel values of 1, 4, 8, or
762 // 24. This routine makes sure that one of these values is
763 // used (whichever is most appropriate for the specified
766 // Parms: lpBmInfoHdr == Far pointer to a BITMAPINFOHEADER structure
768 // dwWidth == Width of DIB (not in Win 3.0 & 3.1, high
770 // dwHeight == Height of DIB (not in Win 3.0 & 3.1, high
772 // nBPP == Bits per Pixel for the DIB.
774 // History: Date Reason
777 //---------------------------------------------------------------------
779 static void InitBitmapInfoHeader (LPBITMAPINFOHEADER lpBmInfoHdr
,
784 // _fmemset (lpBmInfoHdr, 0, sizeof (BITMAPINFOHEADER));
785 memset (lpBmInfoHdr
, 0, sizeof (BITMAPINFOHEADER
));
787 lpBmInfoHdr
->biSize
= sizeof (BITMAPINFOHEADER
);
788 lpBmInfoHdr
->biWidth
= dwWidth
;
789 lpBmInfoHdr
->biHeight
= dwHeight
;
790 lpBmInfoHdr
->biPlanes
= 1;
805 lpBmInfoHdr
->biBitCount
= nBPP
;
806 lpBmInfoHdr
->biSizeImage
= WIDTHBYTES (dwWidth
* nBPP
) * dwHeight
;
812 LPSTR
wxFindDIBBits (LPSTR lpbi
)
814 return (lpbi
+ *(LPDWORD
)lpbi
+ wxPaletteSize (lpbi
));
817 //---------------------------------------------------------------------
819 // Function: BitmapToDIB
821 // Purpose: Given a device dependent bitmap and a palette, returns
822 // a handle to global memory with a DIB spec in it. The
823 // DIB is rendered using the colors of the palette passed in.
825 // Stolen almost verbatim from ShowDIB.
827 // Parms: hBitmap == Handle to device dependent bitmap compatible
828 // with default screen display device.
829 // hPal == Palette to render the DDB with. If it's NULL,
830 // use the default palette.
832 // History: Date Reason
835 //---------------------------------------------------------------------
837 HANDLE
wxBitmapToDIB (HBITMAP hBitmap
, HPALETTE hPal
)
840 BITMAPINFOHEADER bmInfoHdr
;
841 LPBITMAPINFOHEADER lpbmInfoHdr
;
845 HPALETTE hOldPal
= NULL
;
847 // Do some setup -- make sure the Bitmap passed in is valid,
848 // get info on the bitmap (like its height, width, etc.),
849 // then setup a BITMAPINFOHEADER.
854 if (!GetObject (hBitmap
, sizeof (Bitmap
), (LPSTR
) &Bitmap
))
857 InitBitmapInfoHeader (&bmInfoHdr
,
860 Bitmap
.bmPlanes
* Bitmap
.bmBitsPixel
);
863 // Now allocate memory for the DIB. Then, set the BITMAPINFOHEADER
864 // into this memory, and find out where the bitmap bits go.
866 hDIB
= GlobalAlloc (GHND
, sizeof (BITMAPINFOHEADER
) +
867 wxPaletteSize ((LPSTR
) &bmInfoHdr
) + bmInfoHdr
.biSizeImage
);
872 #ifdef __WINDOWS_386__
873 lpbmInfoHdr
= (LPBITMAPINFOHEADER
) MK_FP32(GlobalLock (hDIB
));
875 lpbmInfoHdr
= (LPBITMAPINFOHEADER
) GlobalLock (hDIB
);
878 *lpbmInfoHdr
= bmInfoHdr
;
879 lpBits
= wxFindDIBBits ((LPSTR
) lpbmInfoHdr
);
882 // Now, we need a DC to hold our bitmap. If the app passed us
883 // a palette, it should be selected into the DC.
885 hMemDC
= GetDC (NULL
);
889 hOldPal
= SelectPalette (hMemDC
, hPal
, FALSE
);
890 RealizePalette (hMemDC
);
895 // We're finally ready to get the DIB. Call the driver and let
896 // it party on our bitmap. It will fill in the color table,
897 // and bitmap bits of our global memory block.
899 if (!GetDIBits (hMemDC
,
904 (LPBITMAPINFO
) lpbmInfoHdr
,
915 // Finally, clean up and return.
918 SelectPalette (hMemDC
, hOldPal
, FALSE
);
920 ReleaseDC (NULL
, hMemDC
);
925 bool wxSaveBitmap(wxChar
*filename
, wxBitmap
*bitmap
, wxPalette
*colourmap
)
927 HPALETTE hPalette
= 0;
929 hPalette
= (HPALETTE
) colourmap
->GetHPALETTE();
931 HANDLE dibHandle
= wxBitmapToDIB((HBITMAP
) bitmap
->GetHBITMAP(), hPalette
);
934 bool success
= (WriteDIB(filename
, dibHandle
) != 0);
935 GlobalFree(dibHandle
);