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__)
38 #include "wx/bitmap.h"
48 #include "wx/msw/dib.h"
51 #include "wx/msw/gnuwin32/extra.h"
55 /* flags for _lseek */
61 #define MAXREAD 32768 /* Number of bytes to be read during */
62 /* each read operation. */
64 /* Header signatutes for various resources */
65 #define BFT_ICON 0x4349 /* 'IC' */
66 #define BFT_BITMAP 0x4d42 /* 'BM' */
67 #define BFT_CURSOR 0x5450 /* 'PT' */
69 /* macro to determine if resource is a DIB */
70 #define ISDIB(bft) ((bft) == BFT_BITMAP)
72 /* Macro to align given value to the closest DWORD (unsigned long ) */
73 #define ALIGNULONG(i) ((i+3)/4*4)
75 /* Macro to determine to round off the given value to the closest byte */
76 #define WIDTHBYTES(i) ((i+31)/32*4)
78 #define PALVERSION 0x300
79 #define MAXPALETTE 256 /* max. # supported palette entries */
81 DWORD PASCAL
lread(int fh
, VOID FAR
*pv
, DWORD ul
);
82 DWORD PASCAL
lwrite(int fh
, VOID FAR
*pv
, DWORD ul
);
84 BOOL
WriteDIB (LPSTR szFile
,HANDLE hdib
);
85 WORD
PaletteSize (VOID FAR
* pv
);
86 WORD
DibNumColors (VOID FAR
* pv
);
87 // HANDLE DibFromBitmap (HBITMAP hbm, DWORD biStyle, WORD biBits, HPALETTE hpal);
88 BOOL PASCAL
MakeBitmapAndPalette(HDC
,HANDLE
,HPALETTE
*,HBITMAP
*);
89 HPALETTE
MakeDIBPalette(LPBITMAPINFOHEADER
);
90 BOOL
ReadDIB(LPSTR lpFileName
, HBITMAP
*bitmap
, HPALETTE
*palette
);
92 /****************************************************************************
94 * FUNCTION : WriteDIB(LPSTR szFile,HANDLE hdib) *
96 * PURPOSE : Write a global handle in CF_DIB format to a file. *
98 * RETURNS : TRUE - if successful. *
101 ****************************************************************************/
103 BOOL
WriteDIB(LPSTR szFile
, HANDLE hdib
)
105 BITMAPFILEHEADER hdr
;
106 LPBITMAPINFOHEADER lpbi
;
113 fh
= OpenFile(szFile
, &of
, OF_CREATE
| OF_READWRITE
);
117 #ifdef __WINDOWS_386__
118 lpbi
= (LPBITMAPINFOHEADER
) MK_FP32(GlobalLock(hdib
));
120 lpbi
= (LPBITMAPINFOHEADER
) GlobalLock(hdib
);
122 /* Fill in the fields of the file header */
123 hdr
.bfType
= BFT_BITMAP
;
124 hdr
.bfSize
= GlobalSize(hdib
) + sizeof(BITMAPFILEHEADER
);
127 hdr
.bfOffBits
= (DWORD
) sizeof(BITMAPFILEHEADER
) + lpbi
->biSize
+
130 /* Write the file header */
131 _lwrite(fh
, (LPSTR
) &hdr
, sizeof(BITMAPFILEHEADER
));
133 /* Write the DIB header and the bits */
134 lwrite(fh
, (LPSTR
) lpbi
, GlobalSize(hdib
));
141 /****************************************************************************
143 * FUNCTION : PaletteSize(VOID FAR * pv) *
145 * PURPOSE : Calculates the palette size in bytes. If the info. block *
146 * is of the BITMAPCOREHEADER type, the number of colors is *
147 * multiplied by 3 to give the palette size, otherwise the *
148 * number of colors is multiplied by 4. *
150 * RETURNS : Palette size in number of bytes. *
152 ****************************************************************************/
154 WORD
PaletteSize(VOID FAR
* pv
)
156 LPBITMAPINFOHEADER lpbi
;
159 lpbi
= (LPBITMAPINFOHEADER
) pv
;
160 NumColors
= DibNumColors(lpbi
);
162 if (lpbi
->biSize
== sizeof(BITMAPCOREHEADER
))
163 return NumColors
* sizeof(RGBTRIPLE
);
165 return NumColors
* sizeof(RGBQUAD
);
168 /****************************************************************************
170 * FUNCTION : DibNumColors(VOID FAR * pv) *
172 * PURPOSE : Determines the number of colors in the DIB by looking at *
173 * the BitCount filed in the info block. *
175 * RETURNS : The number of colors in the DIB. *
177 ****************************************************************************/
179 WORD
DibNumColors(VOID FAR
*pv
)
182 BITMAPINFOHEADER
*lpbi
;
183 BITMAPCOREHEADER
*lpbc
;
185 lpbi
= ((BITMAPINFOHEADER
*) pv
);
186 lpbc
= ((BITMAPCOREHEADER
*) pv
);
188 /* With the BITMAPINFO format headers, the size of the palette
189 * is in biClrUsed, whereas in the BITMAPCORE - style headers, it
190 * is dependent on the bits per pixel ( = 2 raised to the power of
193 if (lpbi
->biSize
!= sizeof(BITMAPCOREHEADER
)) {
194 if (lpbi
->biClrUsed
!= 0)
195 return (WORD
) lpbi
->biClrUsed
;
196 bits
= lpbi
->biBitCount
;
199 bits
= lpbc
->bcBitCount
;
209 /* A 24 bitcount DIB has no color table */
214 /****************************************************************************
216 * FUNCTION : DibFromBitmap() *
218 * PURPOSE : Will create a global memory block in DIB format that *
219 * represents the Device-dependent bitmap (DDB) passed in. *
221 * RETURNS : A handle to the DIB *
223 ****************************************************************************/
226 HANDLE
DibFromBitmap(HBITMAP hbm
, DWORD biStyle
, WORD biBits
, HPALETTE hpal
)
230 BITMAPINFOHEADER FAR
*lpbi
;
240 hpal
= GetStockObject(DEFAULT_PALETTE
);
242 GetObject(hbm
, sizeof (bm
), (LPSTR
) &bm
);
245 biBits
= bm
.bmPlanes
* bm
.bmBitsPixel
;
247 bi
.biSize
= sizeof(BITMAPINFOHEADER
);
248 bi
.biWidth
= bm
.bmWidth
;
249 bi
.biHeight
= bm
.bmHeight
;
251 bi
.biBitCount
= biBits
;
252 bi
.biCompression
= biStyle
;
254 bi
.biXPelsPerMeter
= 0;
255 bi
.biYPelsPerMeter
= 0;
257 bi
.biClrImportant
= 0;
259 dwLen
= bi
.biSize
+ PaletteSize(&bi
);
262 hpal
= SelectPalette(hdc
, hpal
, FALSE
);
265 hdib
= GlobalAlloc(GHND
, dwLen
);
268 SelectPalette(hdc
, hpal
, FALSE
);
269 ReleaseDC(NULL
, hdc
);
273 #ifdef __WINDOWS_386__
274 lpbi
= (BITMAPINFOHEADER FAR
*) MK_FP32(GlobalLock(hdib
));
276 lpbi
= (BITMAPINFOHEADER FAR
*) GlobalLock(hdib
);
281 /* call GetDIBits with a NULL lpBits param, so it will calculate the
282 * biSizeImage field for us
284 GetDIBits(hdc
, hbm
, 0, (WORD
) bi
.biHeight
,
285 NULL
, (LPBITMAPINFO
) lpbi
, DIB_RGB_COLORS
);
290 /* If the driver did not fill in the biSizeImage field, make one up */
291 if (bi
.biSizeImage
== 0) {
292 bi
.biSizeImage
= WIDTHBYTES((DWORD
)bm
.bmWidth
* biBits
) * bm
.bmHeight
;
294 if (biStyle
!= BI_RGB
)
295 bi
.biSizeImage
= (bi
.biSizeImage
* 3) / 2;
298 /* realloc the buffer big enough to hold all the bits */
299 dwLen
= bi
.biSize
+ PaletteSize(&bi
) + bi
.biSizeImage
;
300 if (h
= GlobalReAlloc(hdib
, dwLen
, 0))
306 SelectPalette(hdc
, hpal
, FALSE
);
307 ReleaseDC(NULL
, hdc
);
311 /* call GetDIBits with a NON-NULL lpBits param, and actualy get the
314 #ifdef __WINDOWS_386__
315 lpbi
= (BITMAPINFOHEADER FAR
*) MK_FP32(GlobalLock(hdib
));
317 lpbi
= (BITMAPINFOHEADER FAR
*) GlobalLock(hdib
);
324 (LPSTR
) lpbi
+ (WORD
) lpbi
->biSize
+ PaletteSize(lpbi
),
325 (LPBITMAPINFO
) lpbi
, DIB_RGB_COLORS
) == 0) {
328 SelectPalette(hdc
, hpal
, FALSE
);
329 ReleaseDC(NULL
, hdc
);
336 SelectPalette(hdc
, hpal
, FALSE
);
337 ReleaseDC(NULL
, hdc
);
342 /************* PRIVATE ROUTINES TO READ/WRITE MORE THAN 64K ***************/
343 /****************************************************************************
345 * FUNCTION : lread(int fh, VOID FAR *pv, DWORD ul) *
347 * PURPOSE : Reads data in steps of 32k till all the data has been read.*
349 * RETURNS : 0 - If read did not proceed correctly. *
350 * number of bytes read otherwise. *
352 ****************************************************************************/
354 DWORD PASCAL
lread(int fh
, void far
*pv
, DWORD ul
)
357 #if defined(WINNT) || defined(__WIN32__) || defined(__WIN32__)
358 BYTE
*hp
= (BYTE
*) pv
;
360 BYTE huge
*hp
= (BYTE huge
*) pv
;
362 while (ul
> (DWORD
) MAXREAD
) {
363 if (_lread(fh
, (LPSTR
) hp
, (WORD
) MAXREAD
) != MAXREAD
)
368 if (_lread(fh
, (LPSTR
) hp
, (WORD
) ul
) != (WORD
) ul
)
373 /****************************************************************************
375 * FUNCTION : lwrite(int fh, VOID FAR *pv, DWORD ul) *
377 * PURPOSE : Writes data in steps of 32k till all the data is written. *
379 * RETURNS : 0 - If write did not proceed correctly. *
380 * number of bytes written otherwise. *
382 ****************************************************************************/
384 DWORD PASCAL
lwrite(int fh
, VOID FAR
*pv
, DWORD ul
)
387 #if defined(WINNT) || defined(__WIN32__) || defined(__WIN32__)
388 BYTE
*hp
= (BYTE
*) pv
;
390 BYTE huge
*hp
= (BYTE huge
*) pv
;
392 while (ul
> MAXREAD
) {
393 if (_lwrite(fh
, (LPSTR
) hp
, (WORD
) MAXREAD
) != MAXREAD
)
398 if (_lwrite(fh
, (LPSTR
) hp
, (WORD
) ul
) != (WORD
) ul
)
403 /****************************************************************************
405 * FUNCTION : ReadDIB(hWnd)
407 * PURPOSE : Reads a DIB from a file, obtains a handle to its
408 * BITMAPINFO struct. and loads the DIB. Once the DIB
409 * is loaded, the function also creates a bitmap and
410 * palette out of the DIB for a device-dependent form.
412 * RETURNS : TRUE - DIB loaded and bitmap/palette created
413 * The DIBINIT structure pointed to by pInfo is
414 * filled with the appropriate handles.
417 ****************************************************************************/
418 BOOL
ReadDIB(LPSTR lpFileName
, HBITMAP
*bitmap
, HPALETTE
*palette
)
421 LPBITMAPINFOHEADER lpbi
;
429 BOOL bCoreHead
= FALSE
;
432 /* Open the file and get a handle to it's BITMAPINFO */
434 fh
= OpenFile (lpFileName
, &of
, OF_READ
);
436 wsprintf(str
,"Can't open file '%ls'", (LPSTR
)lpFileName
);
437 MessageBox(NULL
, str
, "Error", MB_ICONSTOP
| MB_OK
);
441 hDIB
= GlobalAlloc(GHND
, (DWORD
)(sizeof(BITMAPINFOHEADER
) +
442 256 * sizeof(RGBQUAD
)));
446 #ifdef __WINDOWS_386__
447 lpbi
= (LPBITMAPINFOHEADER
)MK_FP32(GlobalLock(hDIB
));
449 lpbi
= (LPBITMAPINFOHEADER
)GlobalLock(hDIB
);
452 /* read the BITMAPFILEHEADER */
453 if (sizeof (bf
) != _lread (fh
, (LPSTR
)&bf
, sizeof (bf
)))
456 if (bf
.bfType
!= 0x4d42) /* 'BM' */
459 if (sizeof(BITMAPCOREHEADER
) != _lread (fh
, (LPSTR
)lpbi
, sizeof(BITMAPCOREHEADER
)))
462 if (lpbi
->biSize
== sizeof(BITMAPCOREHEADER
))
464 lpbi
->biSize
= sizeof(BITMAPINFOHEADER
);
465 lpbi
->biBitCount
= ((LPBITMAPCOREHEADER
)lpbi
)->bcBitCount
;
466 lpbi
->biPlanes
= ((LPBITMAPCOREHEADER
)lpbi
)->bcPlanes
;
467 lpbi
->biHeight
= ((LPBITMAPCOREHEADER
)lpbi
)->bcHeight
;
468 lpbi
->biWidth
= ((LPBITMAPCOREHEADER
)lpbi
)->bcWidth
;
473 // get to the start of the header and read INFOHEADER
474 _llseek(fh
,sizeof(BITMAPFILEHEADER
),SEEK_SET
);
475 if (sizeof(BITMAPINFOHEADER
) != _lread (fh
, (LPSTR
)lpbi
, sizeof(BITMAPINFOHEADER
)))
479 nNumColors
= (WORD
)lpbi
->biClrUsed
;
480 if ( nNumColors
== 0 )
482 /* no color table for 24-bit, default size otherwise */
483 if (lpbi
->biBitCount
!= 24)
484 nNumColors
= 1 << lpbi
->biBitCount
; /* standard size table */
487 /* fill in some default values if they are zero */
488 if (lpbi
->biClrUsed
== 0)
489 lpbi
->biClrUsed
= nNumColors
;
491 if (lpbi
->biSizeImage
== 0)
493 lpbi
->biSizeImage
= ((((lpbi
->biWidth
* (DWORD
)lpbi
->biBitCount
) + 31) & ~31) >> 3)
497 /* get a proper-sized buffer for header, color table and bits */
499 hDIB
= GlobalReAlloc(hDIB
, lpbi
->biSize
+
500 nNumColors
* sizeof(RGBQUAD
) +
501 lpbi
->biSizeImage
, 0);
502 if (!hDIB
) /* can't resize buffer for loading */
505 #ifdef __WINDOWS_386__
506 lpbi
= (LPBITMAPINFOHEADER
)MK_FP32(GlobalLock(hDIB
));
508 lpbi
= (LPBITMAPINFOHEADER
)GlobalLock(hDIB
);
511 /* read the color table */
513 _lread(fh
, (LPSTR
)(lpbi
) + lpbi
->biSize
, nNumColors
* sizeof(RGBQUAD
));
518 RGBTRIPLE FAR
*pTriple
;
520 _lread(fh
, (LPSTR
)(lpbi
) + lpbi
->biSize
, nNumColors
* sizeof(RGBTRIPLE
));
522 pQuad
= (RGBQUAD FAR
*)((LPSTR
)lpbi
+ lpbi
->biSize
);
523 pTriple
= (RGBTRIPLE FAR
*) pQuad
;
524 for (i
= nNumColors
- 1; i
>= 0; i
--)
526 pQuad
[i
].rgbRed
= pTriple
[i
].rgbtRed
;
527 pQuad
[i
].rgbBlue
= pTriple
[i
].rgbtBlue
;
528 pQuad
[i
].rgbGreen
= pTriple
[i
].rgbtGreen
;
529 pQuad
[i
].rgbReserved
= 0;
533 /* offset to the bits from start of DIB header */
534 offBits
= (WORD
)lpbi
->biSize
+ nNumColors
* sizeof(RGBQUAD
);
536 if (bf
.bfOffBits
!= 0L)
538 _llseek(fh
,bf
.bfOffBits
,SEEK_SET
);
541 if (lpbi
->biSizeImage
== lread(fh
, (LPSTR
)lpbi
+ offBits
, lpbi
->biSizeImage
))
546 if (!MakeBitmapAndPalette(hDC
, hDIB
, palette
,
571 /****************************************************************************
573 * FUNCTION : MakeBitmapAndPalette
575 * PURPOSE : Given a DIB, creates a bitmap and corresponding palette
576 * to be used for a device-dependent representation of
579 * RETURNS : TRUE --> success. phPal and phBitmap are filled with
580 * appropriate handles. Caller is responsible
581 * for freeing objects.
582 * FALSE --> unable to create objects. both pointer are
585 ****************************************************************************/
586 BOOL PASCAL
MakeBitmapAndPalette(HDC hDC
, HANDLE hDIB
,
587 HPALETTE
* phPal
, HBITMAP
* phBitmap
)
589 LPBITMAPINFOHEADER lpInfo
;
592 HPALETTE hPalette
, hOldPal
;
595 #ifdef __WINDOWS_386__
596 lpInfo
= (LPBITMAPINFOHEADER
) MK_FP32(GlobalLock(hDIB
));
598 lpInfo
= (LPBITMAPINFOHEADER
) GlobalLock(hDIB
);
601 hPalette
= MakeDIBPalette(lpInfo
);
604 // Need to realize palette for converting DIB to bitmap.
605 hOldPal
= SelectPalette(hDC
, hPalette
, TRUE
);
608 lpBits
= (LPSTR
)lpInfo
+ (WORD
)lpInfo
->biSize
+
609 (WORD
)lpInfo
->biClrUsed
* sizeof(RGBQUAD
);
610 hBitmap
= CreateDIBitmap(hDC
, lpInfo
, CBM_INIT
, lpBits
,
611 (LPBITMAPINFO
)lpInfo
, DIB_RGB_COLORS
);
613 SelectPalette(hDC
, hOldPal
, TRUE
);
617 DeleteObject(hPalette
);
628 /****************************************************************************
630 * FUNCTION : MakeDIBPalette(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
MakeDIBPalette(LPBITMAPINFOHEADER lpInfo
)
647 /* since biClrUsed field was filled during the loading of the DIB,
648 ** we know it contains the number of colors in the color table.
650 if (lpInfo
->biClrUsed
)
653 npPal = (NPLOGPALETTE)LocalAlloc(LMEM_FIXED, sizeof(LOGPALETTE) +
654 (WORD)lpInfo->biClrUsed * sizeof(PALETTEENTRY));
656 npPal
= (NPLOGPALETTE
)malloc(sizeof(LOGPALETTE
) +
657 (WORD
)lpInfo
->biClrUsed
* sizeof(PALETTEENTRY
));
662 npPal
->palVersion
= 0x300;
663 npPal
->palNumEntries
= (WORD
)lpInfo
->biClrUsed
;
665 /* get pointer to the color table */
666 lpRGB
= (RGBQUAD FAR
*)((LPSTR
)lpInfo
+ lpInfo
->biSize
);
668 /* copy colors from the color table to the LogPalette structure */
669 for (i
= 0; i
< lpInfo
->biClrUsed
; i
++, lpRGB
++)
671 npPal
->palPalEntry
[i
].peRed
= lpRGB
->rgbRed
;
672 npPal
->palPalEntry
[i
].peGreen
= lpRGB
->rgbGreen
;
673 npPal
->palPalEntry
[i
].peBlue
= lpRGB
->rgbBlue
;
674 npPal
->palPalEntry
[i
].peFlags
= 0;
677 hLogPal
= CreatePalette((LPLOGPALETTE
)npPal
);
678 // LocalFree((HANDLE)npPal);
684 /* 24-bit DIB with no color table. return default palette. Another
685 ** option would be to create a 256 color "rainbow" palette to provide
686 ** some good color choices.
689 return((HPALETTE
) GetStockObject(DEFAULT_PALETTE
));
692 bool wxLoadIntoBitmap(char *filename
, wxBitmap
*bitmap
, wxPalette
**pal
)
697 bool success
= (ReadDIB(filename
, &hBitmap
, &hPalette
) != 0);
701 DeleteObject(hPalette
);
709 *pal
= new wxPalette
;
710 (*pal
)->SetHPALETTE((WXHPALETTE
) hPalette
);
713 DeleteObject(hPalette
);
721 GetObject(hBitmap
, sizeof(bm
), (LPSTR
)&bm
);
723 bitmap
->SetHBITMAP((WXHBITMAP
) hBitmap
);
724 bitmap
->SetWidth(bm
.bmWidth
);
725 bitmap
->SetHeight(bm
.bmHeight
);
726 bitmap
->SetDepth(bm
.bmPlanes
* bm
.bmBitsPixel
);
733 wxBitmap
*wxLoadBitmap(char *filename
, wxPalette
**pal
)
735 wxBitmap
*bitmap
= new wxBitmap
;
736 if (wxLoadIntoBitmap(filename
, bitmap
, pal
))
745 //---------------------------------------------------------------------
747 // Function: InitBitmapInfoHeader
749 // Purpose: Does a "standard" initialization of a BITMAPINFOHEADER,
750 // given the Width, Height, and Bits per Pixel for the
753 // By standard, I mean that all the relevant fields are set
754 // to the specified values. biSizeImage is computed, the
755 // biCompression field is set to "no compression," and all
756 // other fields are 0.
758 // Note that DIBs only allow BitsPixel values of 1, 4, 8, or
759 // 24. This routine makes sure that one of these values is
760 // used (whichever is most appropriate for the specified
763 // Parms: lpBmInfoHdr == Far pointer to a BITMAPINFOHEADER structure
765 // dwWidth == Width of DIB (not in Win 3.0 & 3.1, high
767 // dwHeight == Height of DIB (not in Win 3.0 & 3.1, high
769 // nBPP == Bits per Pixel for the DIB.
771 // History: Date Reason
774 //---------------------------------------------------------------------
776 void InitBitmapInfoHeader (LPBITMAPINFOHEADER lpBmInfoHdr
,
781 // _fmemset (lpBmInfoHdr, 0, sizeof (BITMAPINFOHEADER));
782 memset (lpBmInfoHdr
, 0, sizeof (BITMAPINFOHEADER
));
784 lpBmInfoHdr
->biSize
= sizeof (BITMAPINFOHEADER
);
785 lpBmInfoHdr
->biWidth
= dwWidth
;
786 lpBmInfoHdr
->biHeight
= dwHeight
;
787 lpBmInfoHdr
->biPlanes
= 1;
802 lpBmInfoHdr
->biBitCount
= nBPP
;
803 lpBmInfoHdr
->biSizeImage
= WIDTHBYTES (dwWidth
* nBPP
) * dwHeight
;
809 LPSTR
FindDIBBits (LPSTR lpbi
)
811 return (lpbi
+ *(LPDWORD
)lpbi
+ PaletteSize (lpbi
));
814 //---------------------------------------------------------------------
816 // Function: BitmapToDIB
818 // Purpose: Given a device dependent bitmap and a palette, returns
819 // a handle to global memory with a DIB spec in it. The
820 // DIB is rendered using the colors of the palette passed in.
822 // Stolen almost verbatim from ShowDIB.
824 // Parms: hBitmap == Handle to device dependent bitmap compatible
825 // with default screen display device.
826 // hPal == Palette to render the DDB with. If it's NULL,
827 // use the default palette.
829 // History: Date Reason
832 //---------------------------------------------------------------------
834 HANDLE
BitmapToDIB (HBITMAP hBitmap
, HPALETTE hPal
)
837 BITMAPINFOHEADER bmInfoHdr
;
838 LPBITMAPINFOHEADER lpbmInfoHdr
;
842 HPALETTE hOldPal
= NULL
;
844 // Do some setup -- make sure the Bitmap passed in is valid,
845 // get info on the bitmap (like its height, width, etc.),
846 // then setup a BITMAPINFOHEADER.
851 if (!GetObject (hBitmap
, sizeof (Bitmap
), (LPSTR
) &Bitmap
))
854 InitBitmapInfoHeader (&bmInfoHdr
,
857 Bitmap
.bmPlanes
* Bitmap
.bmBitsPixel
);
860 // Now allocate memory for the DIB. Then, set the BITMAPINFOHEADER
861 // into this memory, and find out where the bitmap bits go.
863 hDIB
= GlobalAlloc (GHND
, sizeof (BITMAPINFOHEADER
) +
864 PaletteSize ((LPSTR
) &bmInfoHdr
) + bmInfoHdr
.biSizeImage
);
869 #ifdef __WINDOWS_386__
870 lpbmInfoHdr
= (LPBITMAPINFOHEADER
) MK_FP32(GlobalLock (hDIB
));
872 lpbmInfoHdr
= (LPBITMAPINFOHEADER
) GlobalLock (hDIB
);
875 *lpbmInfoHdr
= bmInfoHdr
;
876 lpBits
= FindDIBBits ((LPSTR
) lpbmInfoHdr
);
879 // Now, we need a DC to hold our bitmap. If the app passed us
880 // a palette, it should be selected into the DC.
882 hMemDC
= GetDC (NULL
);
886 hOldPal
= SelectPalette (hMemDC
, hPal
, FALSE
);
887 RealizePalette (hMemDC
);
892 // We're finally ready to get the DIB. Call the driver and let
893 // it party on our bitmap. It will fill in the color table,
894 // and bitmap bits of our global memory block.
896 if (!GetDIBits (hMemDC
,
901 (LPBITMAPINFO
) lpbmInfoHdr
,
912 // Finally, clean up and return.
915 SelectPalette (hMemDC
, hOldPal
, FALSE
);
917 ReleaseDC (NULL
, hMemDC
);
922 bool wxSaveBitmap(char *filename
, wxBitmap
*bitmap
, wxPalette
*colourmap
)
924 HPALETTE hPalette
= 0;
926 hPalette
= (HPALETTE
) colourmap
->GetHPALETTE();
928 HANDLE dibHandle
= BitmapToDIB((HBITMAP
) bitmap
->GetHBITMAP(), hPalette
);
931 bool success
= (WriteDIB(filename
, dibHandle
) != 0);
932 GlobalFree(dibHandle
);