1 /*******************************************************************************
5 * DESCRIPTION : Routines for dealing with Device Independent Bitmaps. *
9 * ReadDIB() - Reads a DIB *
11 * WriteDIB() - Writes a global handle in CF_DIB format*
14 * PaletteSize() - 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 #define IN_WX_MAIN_CPP
30 #include "wx/wxprec.h"
32 #if defined(__BORLANDC__)
46 #include "wx/msw/dib.h"
49 #include "wx/msw/gnuwin32/extra.h"
53 /* flags for _lseek */
59 #define MAXREAD 32768 /* Number of bytes to be read during */
60 /* each read operation. */
62 /* Header signatutes for various resources */
63 #define BFT_ICON 0x4349 /* 'IC' */
64 #define BFT_BITMAP 0x4d42 /* 'BM' */
65 #define BFT_CURSOR 0x5450 /* 'PT' */
67 /* macro to determine if resource is a DIB */
68 #define ISDIB(bft) ((bft) == BFT_BITMAP)
70 /* Macro to align given value to the closest DWORD (unsigned long ) */
71 #define ALIGNULONG(i) ((i+3)/4*4)
73 /* Macro to determine to round off the given value to the closest byte */
74 #define WIDTHBYTES(i) ((i+31)/32*4)
76 #define PALVERSION 0x300
77 #define MAXPALETTE 256 /* max. # supported palette entries */
79 DWORD PASCAL
lread(int fh
, VOID FAR
*pv
, DWORD ul
);
80 DWORD PASCAL
lwrite(int fh
, VOID FAR
*pv
, DWORD ul
);
82 BOOL
WriteDIB (LPSTR szFile
,HANDLE hdib
);
83 WORD
PaletteSize (VOID FAR
* pv
);
84 WORD
DibNumColors (VOID FAR
* pv
);
85 // HANDLE DibFromBitmap (HBITMAP hbm, DWORD biStyle, WORD biBits, HPALETTE hpal);
86 BOOL PASCAL
MakeBitmapAndPalette(HDC
,HANDLE
,HPALETTE
*,HBITMAP
*);
87 HPALETTE
MakeDIBPalette(LPBITMAPINFOHEADER
);
88 BOOL
ReadDIB(LPSTR lpFileName
, HBITMAP
*bitmap
, HPALETTE
*palette
);
90 /****************************************************************************
92 * FUNCTION : WriteDIB(LPSTR szFile,HANDLE hdib) *
94 * PURPOSE : Write a global handle in CF_DIB format to a file. *
96 * RETURNS : TRUE - if successful. *
99 ****************************************************************************/
101 BOOL
WriteDIB(LPSTR szFile
, HANDLE hdib
)
103 BITMAPFILEHEADER hdr
;
104 LPBITMAPINFOHEADER lpbi
;
111 fh
= OpenFile(szFile
, &of
, OF_CREATE
| OF_READWRITE
);
115 #ifdef __WINDOWS_386__
116 lpbi
= (LPBITMAPINFOHEADER
) MK_FP32(GlobalLock(hdib
));
118 lpbi
= (LPBITMAPINFOHEADER
) GlobalLock(hdib
);
120 /* Fill in the fields of the file header */
121 hdr
.bfType
= BFT_BITMAP
;
122 hdr
.bfSize
= GlobalSize(hdib
) + sizeof(BITMAPFILEHEADER
);
125 hdr
.bfOffBits
= (DWORD
) sizeof(BITMAPFILEHEADER
) + lpbi
->biSize
+
128 /* Write the file header */
129 _lwrite(fh
, (LPSTR
) &hdr
, sizeof(BITMAPFILEHEADER
));
131 /* Write the DIB header and the bits */
132 lwrite(fh
, (LPSTR
) lpbi
, GlobalSize(hdib
));
139 /****************************************************************************
141 * FUNCTION : PaletteSize(VOID FAR * pv) *
143 * PURPOSE : Calculates the palette size in bytes. If the info. block *
144 * is of the BITMAPCOREHEADER type, the number of colors is *
145 * multiplied by 3 to give the palette size, otherwise the *
146 * number of colors is multiplied by 4. *
148 * RETURNS : Palette size in number of bytes. *
150 ****************************************************************************/
152 WORD
PaletteSize(VOID FAR
* pv
)
154 LPBITMAPINFOHEADER lpbi
;
157 lpbi
= (LPBITMAPINFOHEADER
) pv
;
158 NumColors
= DibNumColors(lpbi
);
160 if (lpbi
->biSize
== sizeof(BITMAPCOREHEADER
))
161 return NumColors
* sizeof(RGBTRIPLE
);
163 return NumColors
* sizeof(RGBQUAD
);
166 /****************************************************************************
168 * FUNCTION : DibNumColors(VOID FAR * pv) *
170 * PURPOSE : Determines the number of colors in the DIB by looking at *
171 * the BitCount filed in the info block. *
173 * RETURNS : The number of colors in the DIB. *
175 ****************************************************************************/
177 WORD
DibNumColors(VOID FAR
*pv
)
180 BITMAPINFOHEADER
*lpbi
;
181 BITMAPCOREHEADER
*lpbc
;
183 lpbi
= ((BITMAPINFOHEADER
*) pv
);
184 lpbc
= ((BITMAPCOREHEADER
*) pv
);
186 /* With the BITMAPINFO format headers, the size of the palette
187 * is in biClrUsed, whereas in the BITMAPCORE - style headers, it
188 * is dependent on the bits per pixel ( = 2 raised to the power of
191 if (lpbi
->biSize
!= sizeof(BITMAPCOREHEADER
)) {
192 if (lpbi
->biClrUsed
!= 0)
193 return (WORD
) lpbi
->biClrUsed
;
194 bits
= lpbi
->biBitCount
;
197 bits
= lpbc
->bcBitCount
;
207 /* A 24 bitcount DIB has no color table */
212 /****************************************************************************
214 * FUNCTION : DibFromBitmap() *
216 * PURPOSE : Will create a global memory block in DIB format that *
217 * represents the Device-dependent bitmap (DDB) passed in. *
219 * RETURNS : A handle to the DIB *
221 ****************************************************************************/
224 HANDLE
DibFromBitmap(HBITMAP hbm
, DWORD biStyle
, WORD biBits
, HPALETTE hpal
)
228 BITMAPINFOHEADER FAR
*lpbi
;
238 hpal
= GetStockObject(DEFAULT_PALETTE
);
240 GetObject(hbm
, sizeof (bm
), (LPSTR
) &bm
);
243 biBits
= bm
.bmPlanes
* bm
.bmBitsPixel
;
245 bi
.biSize
= sizeof(BITMAPINFOHEADER
);
246 bi
.biWidth
= bm
.bmWidth
;
247 bi
.biHeight
= bm
.bmHeight
;
249 bi
.biBitCount
= biBits
;
250 bi
.biCompression
= biStyle
;
252 bi
.biXPelsPerMeter
= 0;
253 bi
.biYPelsPerMeter
= 0;
255 bi
.biClrImportant
= 0;
257 dwLen
= bi
.biSize
+ PaletteSize(&bi
);
260 hpal
= SelectPalette(hdc
, hpal
, FALSE
);
263 hdib
= GlobalAlloc(GHND
, dwLen
);
266 SelectPalette(hdc
, hpal
, FALSE
);
267 ReleaseDC(NULL
, hdc
);
271 #ifdef __WINDOWS_386__
272 lpbi
= (BITMAPINFOHEADER FAR
*) MK_FP32(GlobalLock(hdib
));
274 lpbi
= (BITMAPINFOHEADER FAR
*) GlobalLock(hdib
);
279 /* call GetDIBits with a NULL lpBits param, so it will calculate the
280 * biSizeImage field for us
282 GetDIBits(hdc
, hbm
, 0, (WORD
) bi
.biHeight
,
283 NULL
, (LPBITMAPINFO
) lpbi
, DIB_RGB_COLORS
);
288 /* If the driver did not fill in the biSizeImage field, make one up */
289 if (bi
.biSizeImage
== 0) {
290 bi
.biSizeImage
= WIDTHBYTES((DWORD
)bm
.bmWidth
* biBits
) * bm
.bmHeight
;
292 if (biStyle
!= BI_RGB
)
293 bi
.biSizeImage
= (bi
.biSizeImage
* 3) / 2;
296 /* realloc the buffer big enough to hold all the bits */
297 dwLen
= bi
.biSize
+ PaletteSize(&bi
) + bi
.biSizeImage
;
298 if (h
= GlobalReAlloc(hdib
, dwLen
, 0))
304 SelectPalette(hdc
, hpal
, FALSE
);
305 ReleaseDC(NULL
, hdc
);
309 /* call GetDIBits with a NON-NULL lpBits param, and actualy get the
312 #ifdef __WINDOWS_386__
313 lpbi
= (BITMAPINFOHEADER FAR
*) MK_FP32(GlobalLock(hdib
));
315 lpbi
= (BITMAPINFOHEADER FAR
*) GlobalLock(hdib
);
322 (LPSTR
) lpbi
+ (WORD
) lpbi
->biSize
+ PaletteSize(lpbi
),
323 (LPBITMAPINFO
) lpbi
, DIB_RGB_COLORS
) == 0) {
326 SelectPalette(hdc
, hpal
, FALSE
);
327 ReleaseDC(NULL
, hdc
);
334 SelectPalette(hdc
, hpal
, FALSE
);
335 ReleaseDC(NULL
, hdc
);
340 /************* PRIVATE ROUTINES TO READ/WRITE MORE THAN 64K ***************/
341 /****************************************************************************
343 * FUNCTION : lread(int fh, VOID FAR *pv, DWORD ul) *
345 * PURPOSE : Reads data in steps of 32k till all the data has been read.*
347 * RETURNS : 0 - If read did not proceed correctly. *
348 * number of bytes read otherwise. *
350 ****************************************************************************/
352 DWORD PASCAL
lread(int fh
, void far
*pv
, DWORD ul
)
355 #if defined(WINNT) || defined(__WIN32__) || defined(__WIN32__)
356 BYTE
*hp
= (BYTE
*) pv
;
358 BYTE huge
*hp
= (BYTE huge
*) pv
;
360 while (ul
> (DWORD
) MAXREAD
) {
361 if (_lread(fh
, (LPSTR
) hp
, (WORD
) MAXREAD
) != MAXREAD
)
366 if (_lread(fh
, (LPSTR
) hp
, (WORD
) ul
) != (WORD
) ul
)
371 /****************************************************************************
373 * FUNCTION : lwrite(int fh, VOID FAR *pv, DWORD ul) *
375 * PURPOSE : Writes data in steps of 32k till all the data is written. *
377 * RETURNS : 0 - If write did not proceed correctly. *
378 * number of bytes written otherwise. *
380 ****************************************************************************/
382 DWORD PASCAL
lwrite(int fh
, VOID FAR
*pv
, DWORD ul
)
385 #if defined(WINNT) || defined(__WIN32__) || defined(__WIN32__)
386 BYTE
*hp
= (BYTE
*) pv
;
388 BYTE huge
*hp
= (BYTE huge
*) pv
;
390 while (ul
> MAXREAD
) {
391 if (_lwrite(fh
, (LPSTR
) hp
, (WORD
) MAXREAD
) != MAXREAD
)
396 if (_lwrite(fh
, (LPSTR
) hp
, (WORD
) ul
) != (WORD
) ul
)
401 /****************************************************************************
403 * FUNCTION : ReadDIB(hWnd)
405 * PURPOSE : Reads a DIB from a file, obtains a handle to its
406 * BITMAPINFO struct. and loads the DIB. Once the DIB
407 * is loaded, the function also creates a bitmap and
408 * palette out of the DIB for a device-dependent form.
410 * RETURNS : TRUE - DIB loaded and bitmap/palette created
411 * The DIBINIT structure pointed to by pInfo is
412 * filled with the appropriate handles.
415 ****************************************************************************/
416 BOOL
ReadDIB(LPSTR lpFileName
, HBITMAP
*bitmap
, HPALETTE
*palette
)
419 LPBITMAPINFOHEADER lpbi
;
427 BOOL bCoreHead
= FALSE
;
430 /* Open the file and get a handle to it's BITMAPINFO */
432 fh
= OpenFile (lpFileName
, &of
, OF_READ
);
434 wsprintf(str
,"Can't open file '%ls'", (LPSTR
)lpFileName
);
435 MessageBox(NULL
, str
, "Error", MB_ICONSTOP
| MB_OK
);
439 hDIB
= GlobalAlloc(GHND
, (DWORD
)(sizeof(BITMAPINFOHEADER
) +
440 256 * sizeof(RGBQUAD
)));
444 #ifdef __WINDOWS_386__
445 lpbi
= (LPBITMAPINFOHEADER
)MK_FP32(GlobalLock(hDIB
));
447 lpbi
= (LPBITMAPINFOHEADER
)GlobalLock(hDIB
);
450 /* read the BITMAPFILEHEADER */
451 if (sizeof (bf
) != _lread (fh
, (LPSTR
)&bf
, sizeof (bf
)))
454 if (bf
.bfType
!= 0x4d42) /* 'BM' */
457 if (sizeof(BITMAPCOREHEADER
) != _lread (fh
, (LPSTR
)lpbi
, sizeof(BITMAPCOREHEADER
)))
460 if (lpbi
->biSize
== sizeof(BITMAPCOREHEADER
))
462 lpbi
->biSize
= sizeof(BITMAPINFOHEADER
);
463 lpbi
->biBitCount
= ((LPBITMAPCOREHEADER
)lpbi
)->bcBitCount
;
464 lpbi
->biPlanes
= ((LPBITMAPCOREHEADER
)lpbi
)->bcPlanes
;
465 lpbi
->biHeight
= ((LPBITMAPCOREHEADER
)lpbi
)->bcHeight
;
466 lpbi
->biWidth
= ((LPBITMAPCOREHEADER
)lpbi
)->bcWidth
;
471 // get to the start of the header and read INFOHEADER
472 _llseek(fh
,sizeof(BITMAPFILEHEADER
),SEEK_SET
);
473 if (sizeof(BITMAPINFOHEADER
) != _lread (fh
, (LPSTR
)lpbi
, sizeof(BITMAPINFOHEADER
)))
477 if (!(nNumColors
= (WORD
)lpbi
->biClrUsed
))
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 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 if ((hPalette
= MakeDIBPalette(lpInfo
)))
600 // Need to realize palette for converting DIB to bitmap.
601 hOldPal
= SelectPalette(hDC
, hPalette
, TRUE
);
604 lpBits
= (LPSTR
)lpInfo
+ (WORD
)lpInfo
->biSize
+
605 (WORD
)lpInfo
->biClrUsed
* sizeof(RGBQUAD
);
606 hBitmap
= CreateDIBitmap(hDC
, lpInfo
, CBM_INIT
, lpBits
,
607 (LPBITMAPINFO
)lpInfo
, DIB_RGB_COLORS
);
609 SelectPalette(hDC
, hOldPal
, TRUE
);
613 DeleteObject(hPalette
);
624 /****************************************************************************
626 * FUNCTION : MakeDIBPalette(lpInfo) *
628 * PURPOSE : Given a BITMAPINFOHEADER, create a palette based on
632 * RETURNS : non-zero - handle of a corresponding palette
633 * zero - unable to create palette
635 ****************************************************************************/
636 HPALETTE
MakeDIBPalette(LPBITMAPINFOHEADER lpInfo
)
643 /* since biClrUsed field was filled during the loading of the DIB,
644 ** we know it contains the number of colors in the color table.
646 if (lpInfo
->biClrUsed
)
649 npPal = (NPLOGPALETTE)LocalAlloc(LMEM_FIXED, sizeof(LOGPALETTE) +
650 (WORD)lpInfo->biClrUsed * sizeof(PALETTEENTRY));
652 npPal
= (NPLOGPALETTE
)malloc(sizeof(LOGPALETTE
) +
653 (WORD
)lpInfo
->biClrUsed
* sizeof(PALETTEENTRY
));
658 npPal
->palVersion
= 0x300;
659 npPal
->palNumEntries
= (WORD
)lpInfo
->biClrUsed
;
661 /* get pointer to the color table */
662 lpRGB
= (RGBQUAD FAR
*)((LPSTR
)lpInfo
+ lpInfo
->biSize
);
664 /* copy colors from the color table to the LogPalette structure */
665 for (i
= 0; i
< lpInfo
->biClrUsed
; i
++, lpRGB
++)
667 npPal
->palPalEntry
[i
].peRed
= lpRGB
->rgbRed
;
668 npPal
->palPalEntry
[i
].peGreen
= lpRGB
->rgbGreen
;
669 npPal
->palPalEntry
[i
].peBlue
= lpRGB
->rgbBlue
;
670 npPal
->palPalEntry
[i
].peFlags
= 0;
673 hLogPal
= CreatePalette((LPLOGPALETTE
)npPal
);
674 // LocalFree((HANDLE)npPal);
680 /* 24-bit DIB with no color table. return default palette. Another
681 ** option would be to create a 256 color "rainbow" palette to provide
682 ** some good color choices.
685 return((HPALETTE
) GetStockObject(DEFAULT_PALETTE
));
688 bool wxLoadIntoBitmap(char *filename
, wxBitmap
*bitmap
, wxColourMap
**pal
)
693 bool success
= (ReadDIB(filename
, &hBitmap
, &hPalette
) != 0);
697 DeleteObject(hPalette
);
705 *pal
= new wxColourMap
;
706 (*pal
)->SetHPALETTE((WXHPALETTE
) hPalette
);
709 DeleteObject(hPalette
);
717 GetObject(hBitmap
, sizeof(bm
), (LPSTR
)&bm
);
719 bitmap
->SetHBITMAP((WXHBITMAP
) hBitmap
);
720 bitmap
->SetWidth(bm
.bmWidth
);
721 bitmap
->SetHeight(bm
.bmHeight
);
722 bitmap
->SetDepth(bm
.bmPlanes
* bm
.bmBitsPixel
);
729 wxBitmap
*wxLoadBitmap(char *filename
, wxColourMap
**pal
)
731 wxBitmap
*bitmap
= new wxBitmap
;
732 if (wxLoadIntoBitmap(filename
, bitmap
, pal
))
741 //---------------------------------------------------------------------
743 // Function: InitBitmapInfoHeader
745 // Purpose: Does a "standard" initialization of a BITMAPINFOHEADER,
746 // given the Width, Height, and Bits per Pixel for the
749 // By standard, I mean that all the relevant fields are set
750 // to the specified values. biSizeImage is computed, the
751 // biCompression field is set to "no compression," and all
752 // other fields are 0.
754 // Note that DIBs only allow BitsPixel values of 1, 4, 8, or
755 // 24. This routine makes sure that one of these values is
756 // used (whichever is most appropriate for the specified
759 // Parms: lpBmInfoHdr == Far pointer to a BITMAPINFOHEADER structure
761 // dwWidth == Width of DIB (not in Win 3.0 & 3.1, high
763 // dwHeight == Height of DIB (not in Win 3.0 & 3.1, high
765 // nBPP == Bits per Pixel for the DIB.
767 // History: Date Reason
770 //---------------------------------------------------------------------
772 void InitBitmapInfoHeader (LPBITMAPINFOHEADER lpBmInfoHdr
,
777 // _fmemset (lpBmInfoHdr, 0, sizeof (BITMAPINFOHEADER));
778 memset (lpBmInfoHdr
, 0, sizeof (BITMAPINFOHEADER
));
780 lpBmInfoHdr
->biSize
= sizeof (BITMAPINFOHEADER
);
781 lpBmInfoHdr
->biWidth
= dwWidth
;
782 lpBmInfoHdr
->biHeight
= dwHeight
;
783 lpBmInfoHdr
->biPlanes
= 1;
798 lpBmInfoHdr
->biBitCount
= nBPP
;
799 lpBmInfoHdr
->biSizeImage
= WIDTHBYTES (dwWidth
* nBPP
) * dwHeight
;
805 LPSTR
FindDIBBits (LPSTR lpbi
)
807 return (lpbi
+ *(LPDWORD
)lpbi
+ PaletteSize (lpbi
));
810 //---------------------------------------------------------------------
812 // Function: BitmapToDIB
814 // Purpose: Given a device dependent bitmap and a palette, returns
815 // a handle to global memory with a DIB spec in it. The
816 // DIB is rendered using the colors of the palette passed in.
818 // Stolen almost verbatim from ShowDIB.
820 // Parms: hBitmap == Handle to device dependent bitmap compatible
821 // with default screen display device.
822 // hPal == Palette to render the DDB with. If it's NULL,
823 // use the default palette.
825 // History: Date Reason
828 //---------------------------------------------------------------------
830 HANDLE
BitmapToDIB (HBITMAP hBitmap
, HPALETTE hPal
)
833 BITMAPINFOHEADER bmInfoHdr
;
834 LPBITMAPINFOHEADER lpbmInfoHdr
;
838 HPALETTE hOldPal
= NULL
;
840 // Do some setup -- make sure the Bitmap passed in is valid,
841 // get info on the bitmap (like its height, width, etc.),
842 // then setup a BITMAPINFOHEADER.
847 if (!GetObject (hBitmap
, sizeof (Bitmap
), (LPSTR
) &Bitmap
))
850 InitBitmapInfoHeader (&bmInfoHdr
,
853 Bitmap
.bmPlanes
* Bitmap
.bmBitsPixel
);
856 // Now allocate memory for the DIB. Then, set the BITMAPINFOHEADER
857 // into this memory, and find out where the bitmap bits go.
859 hDIB
= GlobalAlloc (GHND
, sizeof (BITMAPINFOHEADER
) +
860 PaletteSize ((LPSTR
) &bmInfoHdr
) + bmInfoHdr
.biSizeImage
);
865 #ifdef __WINDOWS_386__
866 lpbmInfoHdr
= (LPBITMAPINFOHEADER
) MK_FP32(GlobalLock (hDIB
));
868 lpbmInfoHdr
= (LPBITMAPINFOHEADER
) GlobalLock (hDIB
);
871 *lpbmInfoHdr
= bmInfoHdr
;
872 lpBits
= FindDIBBits ((LPSTR
) lpbmInfoHdr
);
875 // Now, we need a DC to hold our bitmap. If the app passed us
876 // a palette, it should be selected into the DC.
878 hMemDC
= GetDC (NULL
);
882 hOldPal
= SelectPalette (hMemDC
, hPal
, FALSE
);
883 RealizePalette (hMemDC
);
888 // We're finally ready to get the DIB. Call the driver and let
889 // it party on our bitmap. It will fill in the color table,
890 // and bitmap bits of our global memory block.
892 if (!GetDIBits (hMemDC
,
897 (LPBITMAPINFO
) lpbmInfoHdr
,
908 // Finally, clean up and return.
911 SelectPalette (hMemDC
, hOldPal
, FALSE
);
913 ReleaseDC (NULL
, hMemDC
);
918 bool wxSaveBitmap(char *filename
, wxBitmap
*bitmap
, wxColourMap
*colourmap
)
920 HPALETTE hPalette
= 0;
922 hPalette
= (HPALETTE
) colourmap
->GetHPALETTE();
924 HANDLE dibHandle
= BitmapToDIB((HBITMAP
) bitmap
->GetHBITMAP(), hPalette
);
927 bool success
= (WriteDIB(filename
, dibHandle
) != 0);
928 GlobalFree(dibHandle
);