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(wxConvertWX2MB(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
, (WXUINT
) ul
) != (WXUINT
) 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
, (WXUINT
) ul
) != (WXUINT
) 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 (wxConvertWX2MB(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
);
623 GlobalUnlock (hDIB
); // glt
628 /****************************************************************************
630 * FUNCTION : wxMakeDIBPalette(lpInfo) *
632 * PURPOSE : Given a BITMAPINFOHEADER, create a palette based on
636 * RETURNS : non-zero - handle of a corresponding palette
637 * zero - unable to create palette
639 ****************************************************************************/
640 HPALETTE
wxMakeDIBPalette(LPBITMAPINFOHEADER lpInfo
)
650 /* since biClrUsed field was filled during the loading of the DIB,
651 ** we know it contains the number of colors in the color table.
653 if (lpInfo
->biClrUsed
)
656 npPal = (NPLOGPALETTE)LocalAlloc(LMEM_FIXED, sizeof(LOGPALETTE) +
657 (WORD)lpInfo->biClrUsed * sizeof(PALETTEENTRY));
659 npPal
= (LPLOGPALETTE
)malloc(sizeof(LOGPALETTE
) +
660 (WORD
)lpInfo
->biClrUsed
* sizeof(PALETTEENTRY
));
664 npPal
->palVersion
= 0x300;
665 npPal
->palNumEntries
= (WORD
)lpInfo
->biClrUsed
;
667 /* get pointer to the color table */
668 lpRGB
= (RGBQUAD FAR
*)((LPSTR
)lpInfo
+ lpInfo
->biSize
);
670 /* copy colors from the color table to the LogPalette structure */
671 for (i
= 0; (DWORD
)i
< lpInfo
->biClrUsed
; i
++, lpRGB
++)
673 npPal
->palPalEntry
[i
].peRed
= lpRGB
->rgbRed
;
674 npPal
->palPalEntry
[i
].peGreen
= lpRGB
->rgbGreen
;
675 npPal
->palPalEntry
[i
].peBlue
= lpRGB
->rgbBlue
;
676 npPal
->palPalEntry
[i
].peFlags
= 0;
679 hLogPal
= CreatePalette((LPLOGPALETTE
)npPal
);
680 // LocalFree((HANDLE)npPal);
686 /* 24-bit DIB with no color table. return default palette. Another
687 ** option would be to create a 256 color "rainbow" palette to provide
688 ** some good color choices.
691 return((HPALETTE
) GetStockObject(DEFAULT_PALETTE
));
696 bool wxLoadIntoBitmap(wxChar
*filename
, wxBitmap
*bitmap
, wxPalette
**pal
)
701 bool success
= (wxReadDIB(filename
, &hBitmap
, &hPalette
) != 0);
705 DeleteObject(hPalette
);
713 *pal
= new wxPalette
;
714 (*pal
)->SetHPALETTE((WXHPALETTE
) hPalette
);
717 DeleteObject(hPalette
);
725 GetObject(hBitmap
, sizeof(bm
), (LPSTR
)&bm
);
727 bitmap
->SetHBITMAP((WXHBITMAP
) hBitmap
);
728 bitmap
->SetWidth(bm
.bmWidth
);
729 bitmap
->SetHeight(bm
.bmHeight
);
730 bitmap
->SetDepth(bm
.bmPlanes
* bm
.bmBitsPixel
);
731 #if WXWIN_COMPATIBILITY_2
733 #endif // WXWIN_COMPATIBILITY_2
739 wxBitmap
*wxLoadBitmap(wxChar
*filename
, wxPalette
**pal
)
741 wxBitmap
*bitmap
= new wxBitmap
;
742 if (wxLoadIntoBitmap(filename
, bitmap
, pal
))
751 //---------------------------------------------------------------------
753 // Function: InitBitmapInfoHeader
755 // Purpose: Does a "standard" initialization of a BITMAPINFOHEADER,
756 // given the Width, Height, and Bits per Pixel for the
759 // By standard, I mean that all the relevant fields are set
760 // to the specified values. biSizeImage is computed, the
761 // biCompression field is set to "no compression," and all
762 // other fields are 0.
764 // Note that DIBs only allow BitsPixel values of 1, 4, 8, or
765 // 24. This routine makes sure that one of these values is
766 // used (whichever is most appropriate for the specified
769 // Parms: lpBmInfoHdr == Far pointer to a BITMAPINFOHEADER structure
771 // dwWidth == Width of DIB (not in Win 3.0 & 3.1, high
773 // dwHeight == Height of DIB (not in Win 3.0 & 3.1, high
775 // nBPP == Bits per Pixel for the DIB.
777 // History: Date Reason
780 //---------------------------------------------------------------------
782 static void InitBitmapInfoHeader (LPBITMAPINFOHEADER lpBmInfoHdr
,
787 // _fmemset (lpBmInfoHdr, 0, sizeof (BITMAPINFOHEADER));
788 memset (lpBmInfoHdr
, 0, sizeof (BITMAPINFOHEADER
));
790 lpBmInfoHdr
->biSize
= sizeof (BITMAPINFOHEADER
);
791 lpBmInfoHdr
->biWidth
= dwWidth
;
792 lpBmInfoHdr
->biHeight
= dwHeight
;
793 lpBmInfoHdr
->biPlanes
= 1;
808 lpBmInfoHdr
->biBitCount
= nBPP
;
809 lpBmInfoHdr
->biSizeImage
= WIDTHBYTES (dwWidth
* nBPP
) * dwHeight
;
815 LPSTR
wxFindDIBBits (LPSTR lpbi
)
817 return (lpbi
+ *(LPDWORD
)lpbi
+ wxPaletteSize (lpbi
));
820 //---------------------------------------------------------------------
822 // Function: BitmapToDIB
824 // Purpose: Given a device dependent bitmap and a palette, returns
825 // a handle to global memory with a DIB spec in it. The
826 // DIB is rendered using the colors of the palette passed in.
828 // Stolen almost verbatim from ShowDIB.
830 // Parms: hBitmap == Handle to device dependent bitmap compatible
831 // with default screen display device.
832 // hPal == Palette to render the DDB with. If it's NULL,
833 // use the default palette.
835 // History: Date Reason
838 //---------------------------------------------------------------------
840 HANDLE
wxBitmapToDIB (HBITMAP hBitmap
, HPALETTE hPal
)
843 BITMAPINFOHEADER bmInfoHdr
;
844 LPBITMAPINFOHEADER lpbmInfoHdr
;
848 HPALETTE hOldPal
= NULL
;
850 // Do some setup -- make sure the Bitmap passed in is valid,
851 // get info on the bitmap (like its height, width, etc.),
852 // then setup a BITMAPINFOHEADER.
857 if (!GetObject (hBitmap
, sizeof (Bitmap
), (LPSTR
) &Bitmap
))
860 InitBitmapInfoHeader (&bmInfoHdr
,
863 Bitmap
.bmPlanes
* Bitmap
.bmBitsPixel
);
866 // Now allocate memory for the DIB. Then, set the BITMAPINFOHEADER
867 // into this memory, and find out where the bitmap bits go.
869 hDIB
= GlobalAlloc (GHND
, sizeof (BITMAPINFOHEADER
) +
870 wxPaletteSize ((LPSTR
) &bmInfoHdr
) + bmInfoHdr
.biSizeImage
);
875 #ifdef __WINDOWS_386__
876 lpbmInfoHdr
= (LPBITMAPINFOHEADER
) MK_FP32(GlobalLock (hDIB
));
878 lpbmInfoHdr
= (LPBITMAPINFOHEADER
) GlobalLock (hDIB
);
881 *lpbmInfoHdr
= bmInfoHdr
;
882 lpBits
= wxFindDIBBits ((LPSTR
) lpbmInfoHdr
);
885 // Now, we need a DC to hold our bitmap. If the app passed us
886 // a palette, it should be selected into the DC.
888 hMemDC
= GetDC (NULL
);
892 hOldPal
= SelectPalette (hMemDC
, hPal
, FALSE
);
893 RealizePalette (hMemDC
);
898 // We're finally ready to get the DIB. Call the driver and let
899 // it party on our bitmap. It will fill in the color table,
900 // and bitmap bits of our global memory block.
902 if (!GetDIBits (hMemDC
,
907 (LPBITMAPINFO
) lpbmInfoHdr
,
918 // Finally, clean up and return.
921 SelectPalette (hMemDC
, hOldPal
, FALSE
);
923 ReleaseDC (NULL
, hMemDC
);
928 bool wxSaveBitmap(wxChar
*filename
, wxBitmap
*bitmap
, wxPalette
*colourmap
)
930 HPALETTE hPalette
= 0;
932 hPalette
= (HPALETTE
) colourmap
->GetHPALETTE();
934 HANDLE dibHandle
= wxBitmapToDIB((HBITMAP
) bitmap
->GetHBITMAP(), hPalette
);
937 bool success
= (WriteDIB(filename
, dibHandle
) != 0);
938 GlobalFree(dibHandle
);