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 if (!(nNumColors
= (WORD
)lpbi
->biClrUsed
))
478 /* no color table for 24-bit, default size otherwise */
479 if (lpbi
->biBitCount
!= 24)
480 nNumColors
= 1 << lpbi
->biBitCount
; /* standard size table */
483 /* fill in some default values if they are zero */
484 if (lpbi
->biClrUsed
== 0)
485 lpbi
->biClrUsed
= nNumColors
;
487 if (lpbi
->biSizeImage
== 0)
489 lpbi
->biSizeImage
= ((((lpbi
->biWidth
* (DWORD
)lpbi
->biBitCount
) + 31) & ~31) >> 3)
493 /* get a proper-sized buffer for header, color table and bits */
495 hDIB
= GlobalReAlloc(hDIB
, lpbi
->biSize
+
496 nNumColors
* sizeof(RGBQUAD
) +
497 lpbi
->biSizeImage
, 0);
498 if (!hDIB
) /* can't resize buffer for loading */
501 #ifdef __WINDOWS_386__
502 lpbi
= (LPBITMAPINFOHEADER
)MK_FP32(GlobalLock(hDIB
));
504 lpbi
= (LPBITMAPINFOHEADER
)GlobalLock(hDIB
);
507 /* read the color table */
509 _lread(fh
, (LPSTR
)(lpbi
) + lpbi
->biSize
, nNumColors
* sizeof(RGBQUAD
));
514 RGBTRIPLE FAR
*pTriple
;
516 _lread(fh
, (LPSTR
)(lpbi
) + lpbi
->biSize
, nNumColors
* sizeof(RGBTRIPLE
));
518 pQuad
= (RGBQUAD FAR
*)((LPSTR
)lpbi
+ lpbi
->biSize
);
519 pTriple
= (RGBTRIPLE FAR
*) pQuad
;
520 for (i
= nNumColors
- 1; i
>= 0; i
--)
522 pQuad
[i
].rgbRed
= pTriple
[i
].rgbtRed
;
523 pQuad
[i
].rgbBlue
= pTriple
[i
].rgbtBlue
;
524 pQuad
[i
].rgbGreen
= pTriple
[i
].rgbtGreen
;
525 pQuad
[i
].rgbReserved
= 0;
529 /* offset to the bits from start of DIB header */
530 offBits
= (WORD
)lpbi
->biSize
+ nNumColors
* sizeof(RGBQUAD
);
532 if (bf
.bfOffBits
!= 0L)
534 _llseek(fh
,bf
.bfOffBits
,SEEK_SET
);
537 if (lpbi
->biSizeImage
== lread(fh
, (LPSTR
)lpbi
+ offBits
, lpbi
->biSizeImage
))
542 if (!MakeBitmapAndPalette(hDC
, hDIB
, palette
,
567 /****************************************************************************
569 * FUNCTION : MakeBitmapAndPalette
571 * PURPOSE : Given a DIB, creates a bitmap and corresponding palette
572 * to be used for a device-dependent representation of
575 * RETURNS : TRUE --> success. phPal and phBitmap are filled with
576 * appropriate handles. Caller is responsible
577 * for freeing objects.
578 * FALSE --> unable to create objects. both pointer are
581 ****************************************************************************/
582 BOOL PASCAL
MakeBitmapAndPalette(HDC hDC
, HANDLE hDIB
,
583 HPALETTE
* phPal
, HBITMAP
* phBitmap
)
585 LPBITMAPINFOHEADER lpInfo
;
588 HPALETTE hPalette
, hOldPal
;
591 #ifdef __WINDOWS_386__
592 lpInfo
= (LPBITMAPINFOHEADER
) MK_FP32(GlobalLock(hDIB
));
594 lpInfo
= (LPBITMAPINFOHEADER
) GlobalLock(hDIB
);
597 if ((hPalette
= MakeDIBPalette(lpInfo
)))
599 // Need to realize palette for converting DIB to bitmap.
600 hOldPal
= SelectPalette(hDC
, hPalette
, TRUE
);
603 lpBits
= (LPSTR
)lpInfo
+ (WORD
)lpInfo
->biSize
+
604 (WORD
)lpInfo
->biClrUsed
* sizeof(RGBQUAD
);
605 hBitmap
= CreateDIBitmap(hDC
, lpInfo
, CBM_INIT
, lpBits
,
606 (LPBITMAPINFO
)lpInfo
, DIB_RGB_COLORS
);
608 SelectPalette(hDC
, hOldPal
, TRUE
);
612 DeleteObject(hPalette
);
623 /****************************************************************************
625 * FUNCTION : MakeDIBPalette(lpInfo) *
627 * PURPOSE : Given a BITMAPINFOHEADER, create a palette based on
631 * RETURNS : non-zero - handle of a corresponding palette
632 * zero - unable to create palette
634 ****************************************************************************/
635 HPALETTE
MakeDIBPalette(LPBITMAPINFOHEADER lpInfo
)
642 /* since biClrUsed field was filled during the loading of the DIB,
643 ** we know it contains the number of colors in the color table.
645 if (lpInfo
->biClrUsed
)
648 npPal = (NPLOGPALETTE)LocalAlloc(LMEM_FIXED, sizeof(LOGPALETTE) +
649 (WORD)lpInfo->biClrUsed * sizeof(PALETTEENTRY));
651 npPal
= (NPLOGPALETTE
)malloc(sizeof(LOGPALETTE
) +
652 (WORD
)lpInfo
->biClrUsed
* sizeof(PALETTEENTRY
));
657 npPal
->palVersion
= 0x300;
658 npPal
->palNumEntries
= (WORD
)lpInfo
->biClrUsed
;
660 /* get pointer to the color table */
661 lpRGB
= (RGBQUAD FAR
*)((LPSTR
)lpInfo
+ lpInfo
->biSize
);
663 /* copy colors from the color table to the LogPalette structure */
664 for (i
= 0; i
< lpInfo
->biClrUsed
; i
++, lpRGB
++)
666 npPal
->palPalEntry
[i
].peRed
= lpRGB
->rgbRed
;
667 npPal
->palPalEntry
[i
].peGreen
= lpRGB
->rgbGreen
;
668 npPal
->palPalEntry
[i
].peBlue
= lpRGB
->rgbBlue
;
669 npPal
->palPalEntry
[i
].peFlags
= 0;
672 hLogPal
= CreatePalette((LPLOGPALETTE
)npPal
);
673 // LocalFree((HANDLE)npPal);
679 /* 24-bit DIB with no color table. return default palette. Another
680 ** option would be to create a 256 color "rainbow" palette to provide
681 ** some good color choices.
684 return((HPALETTE
) GetStockObject(DEFAULT_PALETTE
));
687 bool wxLoadIntoBitmap(char *filename
, wxBitmap
*bitmap
, wxColourMap
**pal
)
692 bool success
= (ReadDIB(filename
, &hBitmap
, &hPalette
) != 0);
696 DeleteObject(hPalette
);
704 *pal
= new wxColourMap
;
705 (*pal
)->SetHPALETTE((WXHPALETTE
) hPalette
);
708 DeleteObject(hPalette
);
716 GetObject(hBitmap
, sizeof(bm
), (LPSTR
)&bm
);
718 bitmap
->SetHBITMAP((WXHBITMAP
) hBitmap
);
719 bitmap
->SetWidth(bm
.bmWidth
);
720 bitmap
->SetHeight(bm
.bmHeight
);
721 bitmap
->SetDepth(bm
.bmPlanes
* bm
.bmBitsPixel
);
728 wxBitmap
*wxLoadBitmap(char *filename
, wxColourMap
**pal
)
730 wxBitmap
*bitmap
= new wxBitmap
;
731 if (wxLoadIntoBitmap(filename
, bitmap
, pal
))
740 //---------------------------------------------------------------------
742 // Function: InitBitmapInfoHeader
744 // Purpose: Does a "standard" initialization of a BITMAPINFOHEADER,
745 // given the Width, Height, and Bits per Pixel for the
748 // By standard, I mean that all the relevant fields are set
749 // to the specified values. biSizeImage is computed, the
750 // biCompression field is set to "no compression," and all
751 // other fields are 0.
753 // Note that DIBs only allow BitsPixel values of 1, 4, 8, or
754 // 24. This routine makes sure that one of these values is
755 // used (whichever is most appropriate for the specified
758 // Parms: lpBmInfoHdr == Far pointer to a BITMAPINFOHEADER structure
760 // dwWidth == Width of DIB (not in Win 3.0 & 3.1, high
762 // dwHeight == Height of DIB (not in Win 3.0 & 3.1, high
764 // nBPP == Bits per Pixel for the DIB.
766 // History: Date Reason
769 //---------------------------------------------------------------------
771 void InitBitmapInfoHeader (LPBITMAPINFOHEADER lpBmInfoHdr
,
776 // _fmemset (lpBmInfoHdr, 0, sizeof (BITMAPINFOHEADER));
777 memset (lpBmInfoHdr
, 0, sizeof (BITMAPINFOHEADER
));
779 lpBmInfoHdr
->biSize
= sizeof (BITMAPINFOHEADER
);
780 lpBmInfoHdr
->biWidth
= dwWidth
;
781 lpBmInfoHdr
->biHeight
= dwHeight
;
782 lpBmInfoHdr
->biPlanes
= 1;
797 lpBmInfoHdr
->biBitCount
= nBPP
;
798 lpBmInfoHdr
->biSizeImage
= WIDTHBYTES (dwWidth
* nBPP
) * dwHeight
;
804 LPSTR
FindDIBBits (LPSTR lpbi
)
806 return (lpbi
+ *(LPDWORD
)lpbi
+ PaletteSize (lpbi
));
809 //---------------------------------------------------------------------
811 // Function: BitmapToDIB
813 // Purpose: Given a device dependent bitmap and a palette, returns
814 // a handle to global memory with a DIB spec in it. The
815 // DIB is rendered using the colors of the palette passed in.
817 // Stolen almost verbatim from ShowDIB.
819 // Parms: hBitmap == Handle to device dependent bitmap compatible
820 // with default screen display device.
821 // hPal == Palette to render the DDB with. If it's NULL,
822 // use the default palette.
824 // History: Date Reason
827 //---------------------------------------------------------------------
829 HANDLE
BitmapToDIB (HBITMAP hBitmap
, HPALETTE hPal
)
832 BITMAPINFOHEADER bmInfoHdr
;
833 LPBITMAPINFOHEADER lpbmInfoHdr
;
837 HPALETTE hOldPal
= NULL
;
839 // Do some setup -- make sure the Bitmap passed in is valid,
840 // get info on the bitmap (like its height, width, etc.),
841 // then setup a BITMAPINFOHEADER.
846 if (!GetObject (hBitmap
, sizeof (Bitmap
), (LPSTR
) &Bitmap
))
849 InitBitmapInfoHeader (&bmInfoHdr
,
852 Bitmap
.bmPlanes
* Bitmap
.bmBitsPixel
);
855 // Now allocate memory for the DIB. Then, set the BITMAPINFOHEADER
856 // into this memory, and find out where the bitmap bits go.
858 hDIB
= GlobalAlloc (GHND
, sizeof (BITMAPINFOHEADER
) +
859 PaletteSize ((LPSTR
) &bmInfoHdr
) + bmInfoHdr
.biSizeImage
);
864 #ifdef __WINDOWS_386__
865 lpbmInfoHdr
= (LPBITMAPINFOHEADER
) MK_FP32(GlobalLock (hDIB
));
867 lpbmInfoHdr
= (LPBITMAPINFOHEADER
) GlobalLock (hDIB
);
870 *lpbmInfoHdr
= bmInfoHdr
;
871 lpBits
= FindDIBBits ((LPSTR
) lpbmInfoHdr
);
874 // Now, we need a DC to hold our bitmap. If the app passed us
875 // a palette, it should be selected into the DC.
877 hMemDC
= GetDC (NULL
);
881 hOldPal
= SelectPalette (hMemDC
, hPal
, FALSE
);
882 RealizePalette (hMemDC
);
887 // We're finally ready to get the DIB. Call the driver and let
888 // it party on our bitmap. It will fill in the color table,
889 // and bitmap bits of our global memory block.
891 if (!GetDIBits (hMemDC
,
896 (LPBITMAPINFO
) lpbmInfoHdr
,
907 // Finally, clean up and return.
910 SelectPalette (hMemDC
, hOldPal
, FALSE
);
912 ReleaseDC (NULL
, hMemDC
);
917 bool wxSaveBitmap(char *filename
, wxBitmap
*bitmap
, wxColourMap
*colourmap
)
919 HPALETTE hPalette
= 0;
921 hPalette
= (HPALETTE
) colourmap
->GetHPALETTE();
923 HANDLE dibHandle
= BitmapToDIB((HBITMAP
) bitmap
->GetHBITMAP(), hPalette
);
926 bool success
= (WriteDIB(filename
, dibHandle
) != 0);
927 GlobalFree(dibHandle
);