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 #include "wx/wxprec.h"
31 #if defined(__BORLANDC__)
45 #include "wx/msw/dib.h"
48 #include "wx/msw/gnuwin32/extra.h"
52 /* flags for _lseek */
58 #define MAXREAD 32768 /* Number of bytes to be read during */
59 /* each read operation. */
61 /* Header signatutes for various resources */
62 #define BFT_ICON 0x4349 /* 'IC' */
63 #define BFT_BITMAP 0x4d42 /* 'BM' */
64 #define BFT_CURSOR 0x5450 /* 'PT' */
66 /* macro to determine if resource is a DIB */
67 #define ISDIB(bft) ((bft) == BFT_BITMAP)
69 /* Macro to align given value to the closest DWORD (unsigned long ) */
70 #define ALIGNULONG(i) ((i+3)/4*4)
72 /* Macro to determine to round off the given value to the closest byte */
73 #define WIDTHBYTES(i) ((i+31)/32*4)
75 #define PALVERSION 0x300
76 #define MAXPALETTE 256 /* max. # supported palette entries */
78 DWORD PASCAL
lread(int fh
, VOID FAR
*pv
, DWORD ul
);
79 DWORD PASCAL
lwrite(int fh
, VOID FAR
*pv
, DWORD ul
);
81 BOOL
WriteDIB (LPSTR szFile
,HANDLE hdib
);
82 WORD
PaletteSize (VOID FAR
* pv
);
83 WORD
DibNumColors (VOID FAR
* pv
);
84 // HANDLE DibFromBitmap (HBITMAP hbm, DWORD biStyle, WORD biBits, HPALETTE hpal);
85 BOOL PASCAL
MakeBitmapAndPalette(HDC
,HANDLE
,HPALETTE
*,HBITMAP
*);
86 HPALETTE
MakeDIBPalette(LPBITMAPINFOHEADER
);
87 BOOL
ReadDIB(LPSTR lpFileName
, HBITMAP
*bitmap
, HPALETTE
*palette
);
89 /****************************************************************************
91 * FUNCTION : WriteDIB(LPSTR szFile,HANDLE hdib) *
93 * PURPOSE : Write a global handle in CF_DIB format to a file. *
95 * RETURNS : TRUE - if successful. *
98 ****************************************************************************/
100 BOOL
WriteDIB(LPSTR szFile
, HANDLE hdib
)
102 BITMAPFILEHEADER hdr
;
103 LPBITMAPINFOHEADER lpbi
;
110 fh
= OpenFile(szFile
, &of
, OF_CREATE
| OF_READWRITE
);
114 #ifdef __WINDOWS_386__
115 lpbi
= (LPBITMAPINFOHEADER
) MK_FP32(GlobalLock(hdib
));
117 lpbi
= (LPBITMAPINFOHEADER
) GlobalLock(hdib
);
119 /* Fill in the fields of the file header */
120 hdr
.bfType
= BFT_BITMAP
;
121 hdr
.bfSize
= GlobalSize(hdib
) + sizeof(BITMAPFILEHEADER
);
124 hdr
.bfOffBits
= (DWORD
) sizeof(BITMAPFILEHEADER
) + lpbi
->biSize
+
127 /* Write the file header */
128 _lwrite(fh
, (LPSTR
) &hdr
, sizeof(BITMAPFILEHEADER
));
130 /* Write the DIB header and the bits */
131 lwrite(fh
, (LPSTR
) lpbi
, GlobalSize(hdib
));
138 /****************************************************************************
140 * FUNCTION : PaletteSize(VOID FAR * pv) *
142 * PURPOSE : Calculates the palette size in bytes. If the info. block *
143 * is of the BITMAPCOREHEADER type, the number of colors is *
144 * multiplied by 3 to give the palette size, otherwise the *
145 * number of colors is multiplied by 4. *
147 * RETURNS : Palette size in number of bytes. *
149 ****************************************************************************/
151 WORD
PaletteSize(VOID FAR
* pv
)
153 LPBITMAPINFOHEADER lpbi
;
156 lpbi
= (LPBITMAPINFOHEADER
) pv
;
157 NumColors
= DibNumColors(lpbi
);
159 if (lpbi
->biSize
== sizeof(BITMAPCOREHEADER
))
160 return NumColors
* sizeof(RGBTRIPLE
);
162 return NumColors
* sizeof(RGBQUAD
);
165 /****************************************************************************
167 * FUNCTION : DibNumColors(VOID FAR * pv) *
169 * PURPOSE : Determines the number of colors in the DIB by looking at *
170 * the BitCount filed in the info block. *
172 * RETURNS : The number of colors in the DIB. *
174 ****************************************************************************/
176 WORD
DibNumColors(VOID FAR
*pv
)
179 BITMAPINFOHEADER
*lpbi
;
180 BITMAPCOREHEADER
*lpbc
;
182 lpbi
= ((BITMAPINFOHEADER
*) pv
);
183 lpbc
= ((BITMAPCOREHEADER
*) pv
);
185 /* With the BITMAPINFO format headers, the size of the palette
186 * is in biClrUsed, whereas in the BITMAPCORE - style headers, it
187 * is dependent on the bits per pixel ( = 2 raised to the power of
190 if (lpbi
->biSize
!= sizeof(BITMAPCOREHEADER
)) {
191 if (lpbi
->biClrUsed
!= 0)
192 return (WORD
) lpbi
->biClrUsed
;
193 bits
= lpbi
->biBitCount
;
196 bits
= lpbc
->bcBitCount
;
206 /* A 24 bitcount DIB has no color table */
211 /****************************************************************************
213 * FUNCTION : DibFromBitmap() *
215 * PURPOSE : Will create a global memory block in DIB format that *
216 * represents the Device-dependent bitmap (DDB) passed in. *
218 * RETURNS : A handle to the DIB *
220 ****************************************************************************/
223 HANDLE
DibFromBitmap(HBITMAP hbm
, DWORD biStyle
, WORD biBits
, HPALETTE hpal
)
227 BITMAPINFOHEADER FAR
*lpbi
;
237 hpal
= GetStockObject(DEFAULT_PALETTE
);
239 GetObject(hbm
, sizeof (bm
), (LPSTR
) &bm
);
242 biBits
= bm
.bmPlanes
* bm
.bmBitsPixel
;
244 bi
.biSize
= sizeof(BITMAPINFOHEADER
);
245 bi
.biWidth
= bm
.bmWidth
;
246 bi
.biHeight
= bm
.bmHeight
;
248 bi
.biBitCount
= biBits
;
249 bi
.biCompression
= biStyle
;
251 bi
.biXPelsPerMeter
= 0;
252 bi
.biYPelsPerMeter
= 0;
254 bi
.biClrImportant
= 0;
256 dwLen
= bi
.biSize
+ PaletteSize(&bi
);
259 hpal
= SelectPalette(hdc
, hpal
, FALSE
);
262 hdib
= GlobalAlloc(GHND
, dwLen
);
265 SelectPalette(hdc
, hpal
, FALSE
);
266 ReleaseDC(NULL
, hdc
);
270 #ifdef __WINDOWS_386__
271 lpbi
= (BITMAPINFOHEADER FAR
*) MK_FP32(GlobalLock(hdib
));
273 lpbi
= (BITMAPINFOHEADER FAR
*) GlobalLock(hdib
);
278 /* call GetDIBits with a NULL lpBits param, so it will calculate the
279 * biSizeImage field for us
281 GetDIBits(hdc
, hbm
, 0, (WORD
) bi
.biHeight
,
282 NULL
, (LPBITMAPINFO
) lpbi
, DIB_RGB_COLORS
);
287 /* If the driver did not fill in the biSizeImage field, make one up */
288 if (bi
.biSizeImage
== 0) {
289 bi
.biSizeImage
= WIDTHBYTES((DWORD
)bm
.bmWidth
* biBits
) * bm
.bmHeight
;
291 if (biStyle
!= BI_RGB
)
292 bi
.biSizeImage
= (bi
.biSizeImage
* 3) / 2;
295 /* realloc the buffer big enough to hold all the bits */
296 dwLen
= bi
.biSize
+ PaletteSize(&bi
) + bi
.biSizeImage
;
297 if (h
= GlobalReAlloc(hdib
, dwLen
, 0))
303 SelectPalette(hdc
, hpal
, FALSE
);
304 ReleaseDC(NULL
, hdc
);
308 /* call GetDIBits with a NON-NULL lpBits param, and actualy get the
311 #ifdef __WINDOWS_386__
312 lpbi
= (BITMAPINFOHEADER FAR
*) MK_FP32(GlobalLock(hdib
));
314 lpbi
= (BITMAPINFOHEADER FAR
*) GlobalLock(hdib
);
321 (LPSTR
) lpbi
+ (WORD
) lpbi
->biSize
+ PaletteSize(lpbi
),
322 (LPBITMAPINFO
) lpbi
, DIB_RGB_COLORS
) == 0) {
325 SelectPalette(hdc
, hpal
, FALSE
);
326 ReleaseDC(NULL
, hdc
);
333 SelectPalette(hdc
, hpal
, FALSE
);
334 ReleaseDC(NULL
, hdc
);
339 /************* PRIVATE ROUTINES TO READ/WRITE MORE THAN 64K ***************/
340 /****************************************************************************
342 * FUNCTION : lread(int fh, VOID FAR *pv, DWORD ul) *
344 * PURPOSE : Reads data in steps of 32k till all the data has been read.*
346 * RETURNS : 0 - If read did not proceed correctly. *
347 * number of bytes read otherwise. *
349 ****************************************************************************/
351 DWORD PASCAL
lread(int fh
, void far
*pv
, DWORD ul
)
354 #if defined(WINNT) || defined(__WIN32__) || defined(__WIN32__)
355 BYTE
*hp
= (BYTE
*) pv
;
357 BYTE huge
*hp
= (BYTE huge
*) pv
;
359 while (ul
> (DWORD
) MAXREAD
) {
360 if (_lread(fh
, (LPSTR
) hp
, (WORD
) MAXREAD
) != MAXREAD
)
365 if (_lread(fh
, (LPSTR
) hp
, (WORD
) ul
) != (WORD
) ul
)
370 /****************************************************************************
372 * FUNCTION : lwrite(int fh, VOID FAR *pv, DWORD ul) *
374 * PURPOSE : Writes data in steps of 32k till all the data is written. *
376 * RETURNS : 0 - If write did not proceed correctly. *
377 * number of bytes written otherwise. *
379 ****************************************************************************/
381 DWORD PASCAL
lwrite(int fh
, VOID FAR
*pv
, DWORD ul
)
384 #if defined(WINNT) || defined(__WIN32__) || defined(__WIN32__)
385 BYTE
*hp
= (BYTE
*) pv
;
387 BYTE huge
*hp
= (BYTE huge
*) pv
;
389 while (ul
> MAXREAD
) {
390 if (_lwrite(fh
, (LPSTR
) hp
, (WORD
) MAXREAD
) != MAXREAD
)
395 if (_lwrite(fh
, (LPSTR
) hp
, (WORD
) ul
) != (WORD
) ul
)
400 /****************************************************************************
402 * FUNCTION : ReadDIB(hWnd)
404 * PURPOSE : Reads a DIB from a file, obtains a handle to its
405 * BITMAPINFO struct. and loads the DIB. Once the DIB
406 * is loaded, the function also creates a bitmap and
407 * palette out of the DIB for a device-dependent form.
409 * RETURNS : TRUE - DIB loaded and bitmap/palette created
410 * The DIBINIT structure pointed to by pInfo is
411 * filled with the appropriate handles.
414 ****************************************************************************/
415 BOOL
ReadDIB(LPSTR lpFileName
, HBITMAP
*bitmap
, HPALETTE
*palette
)
418 LPBITMAPINFOHEADER lpbi
;
426 BOOL bCoreHead
= FALSE
;
429 /* Open the file and get a handle to it's BITMAPINFO */
431 fh
= OpenFile (lpFileName
, &of
, OF_READ
);
433 wsprintf(str
,"Can't open file '%ls'", (LPSTR
)lpFileName
);
434 MessageBox(NULL
, str
, "Error", MB_ICONSTOP
| MB_OK
);
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 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
= MakeDIBPalette(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 : MakeDIBPalette(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
MakeDIBPalette(LPBITMAPINFOHEADER lpInfo
)
644 /* since biClrUsed field was filled during the loading of the DIB,
645 ** we know it contains the number of colors in the color table.
647 if (lpInfo
->biClrUsed
)
650 npPal = (NPLOGPALETTE)LocalAlloc(LMEM_FIXED, sizeof(LOGPALETTE) +
651 (WORD)lpInfo->biClrUsed * sizeof(PALETTEENTRY));
653 npPal
= (NPLOGPALETTE
)malloc(sizeof(LOGPALETTE
) +
654 (WORD
)lpInfo
->biClrUsed
* sizeof(PALETTEENTRY
));
659 npPal
->palVersion
= 0x300;
660 npPal
->palNumEntries
= (WORD
)lpInfo
->biClrUsed
;
662 /* get pointer to the color table */
663 lpRGB
= (RGBQUAD FAR
*)((LPSTR
)lpInfo
+ lpInfo
->biSize
);
665 /* copy colors from the color table to the LogPalette structure */
666 for (i
= 0; i
< lpInfo
->biClrUsed
; i
++, lpRGB
++)
668 npPal
->palPalEntry
[i
].peRed
= lpRGB
->rgbRed
;
669 npPal
->palPalEntry
[i
].peGreen
= lpRGB
->rgbGreen
;
670 npPal
->palPalEntry
[i
].peBlue
= lpRGB
->rgbBlue
;
671 npPal
->palPalEntry
[i
].peFlags
= 0;
674 hLogPal
= CreatePalette((LPLOGPALETTE
)npPal
);
675 // LocalFree((HANDLE)npPal);
681 /* 24-bit DIB with no color table. return default palette. Another
682 ** option would be to create a 256 color "rainbow" palette to provide
683 ** some good color choices.
686 return((HPALETTE
) GetStockObject(DEFAULT_PALETTE
));
689 bool wxLoadIntoBitmap(char *filename
, wxBitmap
*bitmap
, wxColourMap
**pal
)
694 bool success
= (ReadDIB(filename
, &hBitmap
, &hPalette
) != 0);
698 DeleteObject(hPalette
);
706 *pal
= new wxColourMap
;
707 (*pal
)->SetHPALETTE((WXHPALETTE
) hPalette
);
710 DeleteObject(hPalette
);
718 GetObject(hBitmap
, sizeof(bm
), (LPSTR
)&bm
);
720 bitmap
->SetHBITMAP((WXHBITMAP
) hBitmap
);
721 bitmap
->SetWidth(bm
.bmWidth
);
722 bitmap
->SetHeight(bm
.bmHeight
);
723 bitmap
->SetDepth(bm
.bmPlanes
* bm
.bmBitsPixel
);
730 wxBitmap
*wxLoadBitmap(char *filename
, wxColourMap
**pal
)
732 wxBitmap
*bitmap
= new wxBitmap
;
733 if (wxLoadIntoBitmap(filename
, bitmap
, pal
))
742 //---------------------------------------------------------------------
744 // Function: InitBitmapInfoHeader
746 // Purpose: Does a "standard" initialization of a BITMAPINFOHEADER,
747 // given the Width, Height, and Bits per Pixel for the
750 // By standard, I mean that all the relevant fields are set
751 // to the specified values. biSizeImage is computed, the
752 // biCompression field is set to "no compression," and all
753 // other fields are 0.
755 // Note that DIBs only allow BitsPixel values of 1, 4, 8, or
756 // 24. This routine makes sure that one of these values is
757 // used (whichever is most appropriate for the specified
760 // Parms: lpBmInfoHdr == Far pointer to a BITMAPINFOHEADER structure
762 // dwWidth == Width of DIB (not in Win 3.0 & 3.1, high
764 // dwHeight == Height of DIB (not in Win 3.0 & 3.1, high
766 // nBPP == Bits per Pixel for the DIB.
768 // History: Date Reason
771 //---------------------------------------------------------------------
773 void InitBitmapInfoHeader (LPBITMAPINFOHEADER lpBmInfoHdr
,
778 // _fmemset (lpBmInfoHdr, 0, sizeof (BITMAPINFOHEADER));
779 memset (lpBmInfoHdr
, 0, sizeof (BITMAPINFOHEADER
));
781 lpBmInfoHdr
->biSize
= sizeof (BITMAPINFOHEADER
);
782 lpBmInfoHdr
->biWidth
= dwWidth
;
783 lpBmInfoHdr
->biHeight
= dwHeight
;
784 lpBmInfoHdr
->biPlanes
= 1;
799 lpBmInfoHdr
->biBitCount
= nBPP
;
800 lpBmInfoHdr
->biSizeImage
= WIDTHBYTES (dwWidth
* nBPP
) * dwHeight
;
806 LPSTR
FindDIBBits (LPSTR lpbi
)
808 return (lpbi
+ *(LPDWORD
)lpbi
+ PaletteSize (lpbi
));
811 //---------------------------------------------------------------------
813 // Function: BitmapToDIB
815 // Purpose: Given a device dependent bitmap and a palette, returns
816 // a handle to global memory with a DIB spec in it. The
817 // DIB is rendered using the colors of the palette passed in.
819 // Stolen almost verbatim from ShowDIB.
821 // Parms: hBitmap == Handle to device dependent bitmap compatible
822 // with default screen display device.
823 // hPal == Palette to render the DDB with. If it's NULL,
824 // use the default palette.
826 // History: Date Reason
829 //---------------------------------------------------------------------
831 HANDLE
BitmapToDIB (HBITMAP hBitmap
, HPALETTE hPal
)
834 BITMAPINFOHEADER bmInfoHdr
;
835 LPBITMAPINFOHEADER lpbmInfoHdr
;
839 HPALETTE hOldPal
= NULL
;
841 // Do some setup -- make sure the Bitmap passed in is valid,
842 // get info on the bitmap (like its height, width, etc.),
843 // then setup a BITMAPINFOHEADER.
848 if (!GetObject (hBitmap
, sizeof (Bitmap
), (LPSTR
) &Bitmap
))
851 InitBitmapInfoHeader (&bmInfoHdr
,
854 Bitmap
.bmPlanes
* Bitmap
.bmBitsPixel
);
857 // Now allocate memory for the DIB. Then, set the BITMAPINFOHEADER
858 // into this memory, and find out where the bitmap bits go.
860 hDIB
= GlobalAlloc (GHND
, sizeof (BITMAPINFOHEADER
) +
861 PaletteSize ((LPSTR
) &bmInfoHdr
) + bmInfoHdr
.biSizeImage
);
866 #ifdef __WINDOWS_386__
867 lpbmInfoHdr
= (LPBITMAPINFOHEADER
) MK_FP32(GlobalLock (hDIB
));
869 lpbmInfoHdr
= (LPBITMAPINFOHEADER
) GlobalLock (hDIB
);
872 *lpbmInfoHdr
= bmInfoHdr
;
873 lpBits
= FindDIBBits ((LPSTR
) lpbmInfoHdr
);
876 // Now, we need a DC to hold our bitmap. If the app passed us
877 // a palette, it should be selected into the DC.
879 hMemDC
= GetDC (NULL
);
883 hOldPal
= SelectPalette (hMemDC
, hPal
, FALSE
);
884 RealizePalette (hMemDC
);
889 // We're finally ready to get the DIB. Call the driver and let
890 // it party on our bitmap. It will fill in the color table,
891 // and bitmap bits of our global memory block.
893 if (!GetDIBits (hMemDC
,
898 (LPBITMAPINFO
) lpbmInfoHdr
,
909 // Finally, clean up and return.
912 SelectPalette (hMemDC
, hOldPal
, FALSE
);
914 ReleaseDC (NULL
, hMemDC
);
919 bool wxSaveBitmap(char *filename
, wxBitmap
*bitmap
, wxColourMap
*colourmap
)
921 HPALETTE hPalette
= 0;
923 hPalette
= (HPALETTE
) colourmap
->GetHPALETTE();
925 HANDLE dibHandle
= BitmapToDIB((HBITMAP
) bitmap
->GetHBITMAP(), hPalette
);
928 bool success
= (WriteDIB(filename
, dibHandle
) != 0);
929 GlobalFree(dibHandle
);