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__)
47 #include "wx/msw/dib.h"
50 #include "wx/msw/gnuwin32/extra.h"
54 /* flags for _lseek */
60 #define MAXREAD 32768 /* Number of bytes to be read during */
61 /* each read operation. */
63 /* Header signatutes for various resources */
64 #define BFT_ICON 0x4349 /* 'IC' */
65 #define BFT_BITMAP 0x4d42 /* 'BM' */
66 #define BFT_CURSOR 0x5450 /* 'PT' */
68 /* macro to determine if resource is a DIB */
69 #define ISDIB(bft) ((bft) == BFT_BITMAP)
71 /* Macro to align given value to the closest DWORD (unsigned long ) */
72 #define ALIGNULONG(i) ((i+3)/4*4)
74 /* Macro to determine to round off the given value to the closest byte */
75 #define WIDTHBYTES(i) ((i+31)/32*4)
77 #define PALVERSION 0x300
78 #define MAXPALETTE 256 /* max. # supported palette entries */
80 DWORD PASCAL
lread(int fh
, VOID FAR
*pv
, DWORD ul
);
81 DWORD PASCAL
lwrite(int fh
, VOID FAR
*pv
, DWORD ul
);
83 BOOL
WriteDIB (LPSTR szFile
,HANDLE hdib
);
84 WORD
PaletteSize (VOID FAR
* pv
);
85 WORD
DibNumColors (VOID FAR
* pv
);
86 // HANDLE DibFromBitmap (HBITMAP hbm, DWORD biStyle, WORD biBits, HPALETTE hpal);
87 BOOL PASCAL
MakeBitmapAndPalette(HDC
,HANDLE
,HPALETTE
*,HBITMAP
*);
88 HPALETTE
MakeDIBPalette(LPBITMAPINFOHEADER
);
89 BOOL
ReadDIB(LPSTR lpFileName
, HBITMAP
*bitmap
, HPALETTE
*palette
);
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 BOOL
WriteDIB(LPSTR szFile
, HANDLE hdib
)
104 BITMAPFILEHEADER hdr
;
105 LPBITMAPINFOHEADER lpbi
;
112 fh
= OpenFile(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 : PaletteSize(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
PaletteSize(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 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 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
+ PaletteSize(&bi
);
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
+ PaletteSize(&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
+ PaletteSize(lpbi
),
324 (LPBITMAPINFO
) lpbi
, DIB_RGB_COLORS
) == 0) {
327 SelectPalette(hdc
, hpal
, FALSE
);
328 ReleaseDC(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 DWORD PASCAL
lread(int fh
, void far
*pv
, DWORD ul
)
356 #if defined(WINNT) || defined(__WIN32__) || defined(__WIN32__)
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 DWORD PASCAL
lwrite(int fh
, VOID FAR
*pv
, DWORD ul
)
386 #if defined(WINNT) || defined(__WIN32__) || defined(__WIN32__)
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
ReadDIB(LPSTR lpFileName
, HBITMAP
*bitmap
, HPALETTE
*palette
)
420 LPBITMAPINFOHEADER lpbi
;
428 BOOL bCoreHead
= FALSE
;
431 /* Open the file and get a handle to it's BITMAPINFO */
433 fh
= OpenFile (lpFileName
, &of
, OF_READ
);
435 wsprintf(str
,"Can't open file '%ls'", (LPSTR
)lpFileName
);
436 MessageBox(NULL
, str
, "Error", MB_ICONSTOP
| MB_OK
);
440 hDIB
= GlobalAlloc(GHND
, (DWORD
)(sizeof(BITMAPINFOHEADER
) +
441 256 * sizeof(RGBQUAD
)));
445 #ifdef __WINDOWS_386__
446 lpbi
= (LPBITMAPINFOHEADER
)MK_FP32(GlobalLock(hDIB
));
448 lpbi
= (LPBITMAPINFOHEADER
)GlobalLock(hDIB
);
451 /* read the BITMAPFILEHEADER */
452 if (sizeof (bf
) != _lread (fh
, (LPSTR
)&bf
, sizeof (bf
)))
455 if (bf
.bfType
!= 0x4d42) /* 'BM' */
458 if (sizeof(BITMAPCOREHEADER
) != _lread (fh
, (LPSTR
)lpbi
, sizeof(BITMAPCOREHEADER
)))
461 if (lpbi
->biSize
== sizeof(BITMAPCOREHEADER
))
463 lpbi
->biSize
= sizeof(BITMAPINFOHEADER
);
464 lpbi
->biBitCount
= ((LPBITMAPCOREHEADER
)lpbi
)->bcBitCount
;
465 lpbi
->biPlanes
= ((LPBITMAPCOREHEADER
)lpbi
)->bcPlanes
;
466 lpbi
->biHeight
= ((LPBITMAPCOREHEADER
)lpbi
)->bcHeight
;
467 lpbi
->biWidth
= ((LPBITMAPCOREHEADER
)lpbi
)->bcWidth
;
472 // get to the start of the header and read INFOHEADER
473 _llseek(fh
,sizeof(BITMAPFILEHEADER
),SEEK_SET
);
474 if (sizeof(BITMAPINFOHEADER
) != _lread (fh
, (LPSTR
)lpbi
, sizeof(BITMAPINFOHEADER
)))
478 nNumColors
= (WORD
)lpbi
->biClrUsed
;
479 if ( nNumColors
== 0 )
481 /* no color table for 24-bit, default size otherwise */
482 if (lpbi
->biBitCount
!= 24)
483 nNumColors
= 1 << lpbi
->biBitCount
; /* standard size table */
486 /* fill in some default values if they are zero */
487 if (lpbi
->biClrUsed
== 0)
488 lpbi
->biClrUsed
= nNumColors
;
490 if (lpbi
->biSizeImage
== 0)
492 lpbi
->biSizeImage
= ((((lpbi
->biWidth
* (DWORD
)lpbi
->biBitCount
) + 31) & ~31) >> 3)
496 /* get a proper-sized buffer for header, color table and bits */
498 hDIB
= GlobalReAlloc(hDIB
, lpbi
->biSize
+
499 nNumColors
* sizeof(RGBQUAD
) +
500 lpbi
->biSizeImage
, 0);
501 if (!hDIB
) /* can't resize buffer for loading */
504 #ifdef __WINDOWS_386__
505 lpbi
= (LPBITMAPINFOHEADER
)MK_FP32(GlobalLock(hDIB
));
507 lpbi
= (LPBITMAPINFOHEADER
)GlobalLock(hDIB
);
510 /* read the color table */
512 _lread(fh
, (LPSTR
)(lpbi
) + lpbi
->biSize
, nNumColors
* sizeof(RGBQUAD
));
517 RGBTRIPLE FAR
*pTriple
;
519 _lread(fh
, (LPSTR
)(lpbi
) + lpbi
->biSize
, nNumColors
* sizeof(RGBTRIPLE
));
521 pQuad
= (RGBQUAD FAR
*)((LPSTR
)lpbi
+ lpbi
->biSize
);
522 pTriple
= (RGBTRIPLE FAR
*) pQuad
;
523 for (i
= nNumColors
- 1; i
>= 0; i
--)
525 pQuad
[i
].rgbRed
= pTriple
[i
].rgbtRed
;
526 pQuad
[i
].rgbBlue
= pTriple
[i
].rgbtBlue
;
527 pQuad
[i
].rgbGreen
= pTriple
[i
].rgbtGreen
;
528 pQuad
[i
].rgbReserved
= 0;
532 /* offset to the bits from start of DIB header */
533 offBits
= (WORD
)lpbi
->biSize
+ nNumColors
* sizeof(RGBQUAD
);
535 if (bf
.bfOffBits
!= 0L)
537 _llseek(fh
,bf
.bfOffBits
,SEEK_SET
);
540 if (lpbi
->biSizeImage
== lread(fh
, (LPSTR
)lpbi
+ offBits
, lpbi
->biSizeImage
))
545 if (!MakeBitmapAndPalette(hDC
, hDIB
, palette
,
570 /****************************************************************************
572 * FUNCTION : MakeBitmapAndPalette
574 * PURPOSE : Given a DIB, creates a bitmap and corresponding palette
575 * to be used for a device-dependent representation of
578 * RETURNS : TRUE --> success. phPal and phBitmap are filled with
579 * appropriate handles. Caller is responsible
580 * for freeing objects.
581 * FALSE --> unable to create objects. both pointer are
584 ****************************************************************************/
585 BOOL PASCAL
MakeBitmapAndPalette(HDC hDC
, HANDLE hDIB
,
586 HPALETTE
* phPal
, HBITMAP
* phBitmap
)
588 LPBITMAPINFOHEADER lpInfo
;
591 HPALETTE hPalette
, hOldPal
;
594 #ifdef __WINDOWS_386__
595 lpInfo
= (LPBITMAPINFOHEADER
) MK_FP32(GlobalLock(hDIB
));
597 lpInfo
= (LPBITMAPINFOHEADER
) GlobalLock(hDIB
);
600 hPalette
= MakeDIBPalette(lpInfo
);
603 // Need to realize palette for converting DIB to bitmap.
604 hOldPal
= SelectPalette(hDC
, hPalette
, TRUE
);
607 lpBits
= (LPSTR
)lpInfo
+ (WORD
)lpInfo
->biSize
+
608 (WORD
)lpInfo
->biClrUsed
* sizeof(RGBQUAD
);
609 hBitmap
= CreateDIBitmap(hDC
, lpInfo
, CBM_INIT
, lpBits
,
610 (LPBITMAPINFO
)lpInfo
, DIB_RGB_COLORS
);
612 SelectPalette(hDC
, hOldPal
, TRUE
);
616 DeleteObject(hPalette
);
627 /****************************************************************************
629 * FUNCTION : MakeDIBPalette(lpInfo) *
631 * PURPOSE : Given a BITMAPINFOHEADER, create a palette based on
635 * RETURNS : non-zero - handle of a corresponding palette
636 * zero - unable to create palette
638 ****************************************************************************/
639 HPALETTE
MakeDIBPalette(LPBITMAPINFOHEADER lpInfo
)
646 /* since biClrUsed field was filled during the loading of the DIB,
647 ** we know it contains the number of colors in the color table.
649 if (lpInfo
->biClrUsed
)
652 npPal = (NPLOGPALETTE)LocalAlloc(LMEM_FIXED, sizeof(LOGPALETTE) +
653 (WORD)lpInfo->biClrUsed * sizeof(PALETTEENTRY));
655 npPal
= (NPLOGPALETTE
)malloc(sizeof(LOGPALETTE
) +
656 (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
));
691 bool wxLoadIntoBitmap(char *filename
, wxBitmap
*bitmap
, wxColourMap
**pal
)
696 bool success
= (ReadDIB(filename
, &hBitmap
, &hPalette
) != 0);
700 DeleteObject(hPalette
);
708 *pal
= new wxColourMap
;
709 (*pal
)->SetHPALETTE((WXHPALETTE
) hPalette
);
712 DeleteObject(hPalette
);
720 GetObject(hBitmap
, sizeof(bm
), (LPSTR
)&bm
);
722 bitmap
->SetHBITMAP((WXHBITMAP
) hBitmap
);
723 bitmap
->SetWidth(bm
.bmWidth
);
724 bitmap
->SetHeight(bm
.bmHeight
);
725 bitmap
->SetDepth(bm
.bmPlanes
* bm
.bmBitsPixel
);
732 wxBitmap
*wxLoadBitmap(char *filename
, wxColourMap
**pal
)
734 wxBitmap
*bitmap
= new wxBitmap
;
735 if (wxLoadIntoBitmap(filename
, bitmap
, pal
))
744 //---------------------------------------------------------------------
746 // Function: InitBitmapInfoHeader
748 // Purpose: Does a "standard" initialization of a BITMAPINFOHEADER,
749 // given the Width, Height, and Bits per Pixel for the
752 // By standard, I mean that all the relevant fields are set
753 // to the specified values. biSizeImage is computed, the
754 // biCompression field is set to "no compression," and all
755 // other fields are 0.
757 // Note that DIBs only allow BitsPixel values of 1, 4, 8, or
758 // 24. This routine makes sure that one of these values is
759 // used (whichever is most appropriate for the specified
762 // Parms: lpBmInfoHdr == Far pointer to a BITMAPINFOHEADER structure
764 // dwWidth == Width of DIB (not in Win 3.0 & 3.1, high
766 // dwHeight == Height of DIB (not in Win 3.0 & 3.1, high
768 // nBPP == Bits per Pixel for the DIB.
770 // History: Date Reason
773 //---------------------------------------------------------------------
775 void InitBitmapInfoHeader (LPBITMAPINFOHEADER lpBmInfoHdr
,
780 // _fmemset (lpBmInfoHdr, 0, sizeof (BITMAPINFOHEADER));
781 memset (lpBmInfoHdr
, 0, sizeof (BITMAPINFOHEADER
));
783 lpBmInfoHdr
->biSize
= sizeof (BITMAPINFOHEADER
);
784 lpBmInfoHdr
->biWidth
= dwWidth
;
785 lpBmInfoHdr
->biHeight
= dwHeight
;
786 lpBmInfoHdr
->biPlanes
= 1;
801 lpBmInfoHdr
->biBitCount
= nBPP
;
802 lpBmInfoHdr
->biSizeImage
= WIDTHBYTES (dwWidth
* nBPP
) * dwHeight
;
808 LPSTR
FindDIBBits (LPSTR lpbi
)
810 return (lpbi
+ *(LPDWORD
)lpbi
+ PaletteSize (lpbi
));
813 //---------------------------------------------------------------------
815 // Function: BitmapToDIB
817 // Purpose: Given a device dependent bitmap and a palette, returns
818 // a handle to global memory with a DIB spec in it. The
819 // DIB is rendered using the colors of the palette passed in.
821 // Stolen almost verbatim from ShowDIB.
823 // Parms: hBitmap == Handle to device dependent bitmap compatible
824 // with default screen display device.
825 // hPal == Palette to render the DDB with. If it's NULL,
826 // use the default palette.
828 // History: Date Reason
831 //---------------------------------------------------------------------
833 HANDLE
BitmapToDIB (HBITMAP hBitmap
, HPALETTE hPal
)
836 BITMAPINFOHEADER bmInfoHdr
;
837 LPBITMAPINFOHEADER lpbmInfoHdr
;
841 HPALETTE hOldPal
= NULL
;
843 // Do some setup -- make sure the Bitmap passed in is valid,
844 // get info on the bitmap (like its height, width, etc.),
845 // then setup a BITMAPINFOHEADER.
850 if (!GetObject (hBitmap
, sizeof (Bitmap
), (LPSTR
) &Bitmap
))
853 InitBitmapInfoHeader (&bmInfoHdr
,
856 Bitmap
.bmPlanes
* Bitmap
.bmBitsPixel
);
859 // Now allocate memory for the DIB. Then, set the BITMAPINFOHEADER
860 // into this memory, and find out where the bitmap bits go.
862 hDIB
= GlobalAlloc (GHND
, sizeof (BITMAPINFOHEADER
) +
863 PaletteSize ((LPSTR
) &bmInfoHdr
) + bmInfoHdr
.biSizeImage
);
868 #ifdef __WINDOWS_386__
869 lpbmInfoHdr
= (LPBITMAPINFOHEADER
) MK_FP32(GlobalLock (hDIB
));
871 lpbmInfoHdr
= (LPBITMAPINFOHEADER
) GlobalLock (hDIB
);
874 *lpbmInfoHdr
= bmInfoHdr
;
875 lpBits
= FindDIBBits ((LPSTR
) lpbmInfoHdr
);
878 // Now, we need a DC to hold our bitmap. If the app passed us
879 // a palette, it should be selected into the DC.
881 hMemDC
= GetDC (NULL
);
885 hOldPal
= SelectPalette (hMemDC
, hPal
, FALSE
);
886 RealizePalette (hMemDC
);
891 // We're finally ready to get the DIB. Call the driver and let
892 // it party on our bitmap. It will fill in the color table,
893 // and bitmap bits of our global memory block.
895 if (!GetDIBits (hMemDC
,
900 (LPBITMAPINFO
) lpbmInfoHdr
,
911 // Finally, clean up and return.
914 SelectPalette (hMemDC
, hOldPal
, FALSE
);
916 ReleaseDC (NULL
, hMemDC
);
921 bool wxSaveBitmap(char *filename
, wxBitmap
*bitmap
, wxColourMap
*colourmap
)
923 HPALETTE hPalette
= 0;
925 hPalette
= (HPALETTE
) colourmap
->GetHPALETTE();
927 HANDLE dibHandle
= BitmapToDIB((HBITMAP
) bitmap
->GetHBITMAP(), hPalette
);
930 bool success
= (WriteDIB(filename
, dibHandle
) != 0);
931 GlobalFree(dibHandle
);