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"
45 #if !defined(__MWERKS__) && !defined(__SALFORDC__)
49 #include "wx/msw/dib.h"
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 DWORD PASCAL
lread(int fh
, VOID FAR
*pv
, DWORD ul
);
83 DWORD PASCAL
lwrite(int fh
, VOID FAR
*pv
, DWORD ul
);
85 BOOL
WriteDIB (LPSTR szFile
,HANDLE hdib
);
86 WORD
PaletteSize (VOID FAR
* pv
);
87 WORD
DibNumColors (VOID FAR
* pv
);
88 // HANDLE DibFromBitmap (HBITMAP hbm, DWORD biStyle, WORD biBits, HPALETTE hpal);
89 BOOL PASCAL
MakeBitmapAndPalette(HDC
,HANDLE
,HPALETTE
*,HBITMAP
*);
90 HPALETTE
MakeDIBPalette(LPBITMAPINFOHEADER
);
91 BOOL
ReadDIB(LPSTR lpFileName
, HBITMAP
*bitmap
, HPALETTE
*palette
);
93 /****************************************************************************
95 * FUNCTION : WriteDIB(LPSTR szFile,HANDLE hdib) *
97 * PURPOSE : Write a global handle in CF_DIB format to a file. *
99 * RETURNS : TRUE - if successful. *
100 * FALSE - otherwise *
102 ****************************************************************************/
104 BOOL
WriteDIB(LPSTR szFile
, HANDLE hdib
)
106 BITMAPFILEHEADER hdr
;
107 LPBITMAPINFOHEADER lpbi
;
114 fh
= OpenFile(szFile
, &of
, OF_CREATE
| OF_READWRITE
);
118 #ifdef __WINDOWS_386__
119 lpbi
= (LPBITMAPINFOHEADER
) MK_FP32(GlobalLock(hdib
));
121 lpbi
= (LPBITMAPINFOHEADER
) GlobalLock(hdib
);
123 /* Fill in the fields of the file header */
124 hdr
.bfType
= BFT_BITMAP
;
125 hdr
.bfSize
= GlobalSize(hdib
) + sizeof(BITMAPFILEHEADER
);
128 hdr
.bfOffBits
= (DWORD
) sizeof(BITMAPFILEHEADER
) + lpbi
->biSize
+
131 /* Write the file header */
132 _lwrite(fh
, (LPSTR
) &hdr
, sizeof(BITMAPFILEHEADER
));
134 /* Write the DIB header and the bits */
135 lwrite(fh
, (LPSTR
) lpbi
, GlobalSize(hdib
));
142 /****************************************************************************
144 * FUNCTION : PaletteSize(VOID FAR * pv) *
146 * PURPOSE : Calculates the palette size in bytes. If the info. block *
147 * is of the BITMAPCOREHEADER type, the number of colors is *
148 * multiplied by 3 to give the palette size, otherwise the *
149 * number of colors is multiplied by 4. *
151 * RETURNS : Palette size in number of bytes. *
153 ****************************************************************************/
155 WORD
PaletteSize(VOID FAR
* pv
)
157 LPBITMAPINFOHEADER lpbi
;
160 lpbi
= (LPBITMAPINFOHEADER
) pv
;
161 NumColors
= DibNumColors(lpbi
);
163 if (lpbi
->biSize
== sizeof(BITMAPCOREHEADER
))
164 return NumColors
* sizeof(RGBTRIPLE
);
166 return NumColors
* sizeof(RGBQUAD
);
169 /****************************************************************************
171 * FUNCTION : DibNumColors(VOID FAR * pv) *
173 * PURPOSE : Determines the number of colors in the DIB by looking at *
174 * the BitCount filed in the info block. *
176 * RETURNS : The number of colors in the DIB. *
178 ****************************************************************************/
180 WORD
DibNumColors(VOID FAR
*pv
)
183 BITMAPINFOHEADER
*lpbi
;
184 BITMAPCOREHEADER
*lpbc
;
186 lpbi
= ((BITMAPINFOHEADER
*) pv
);
187 lpbc
= ((BITMAPCOREHEADER
*) pv
);
189 /* With the BITMAPINFO format headers, the size of the palette
190 * is in biClrUsed, whereas in the BITMAPCORE - style headers, it
191 * is dependent on the bits per pixel ( = 2 raised to the power of
194 if (lpbi
->biSize
!= sizeof(BITMAPCOREHEADER
)) {
195 if (lpbi
->biClrUsed
!= 0)
196 return (WORD
) lpbi
->biClrUsed
;
197 bits
= lpbi
->biBitCount
;
200 bits
= lpbc
->bcBitCount
;
210 /* A 24 bitcount DIB has no color table */
215 /****************************************************************************
217 * FUNCTION : DibFromBitmap() *
219 * PURPOSE : Will create a global memory block in DIB format that *
220 * represents the Device-dependent bitmap (DDB) passed in. *
222 * RETURNS : A handle to the DIB *
224 ****************************************************************************/
227 HANDLE
DibFromBitmap(HBITMAP hbm
, DWORD biStyle
, WORD biBits
, HPALETTE hpal
)
231 BITMAPINFOHEADER FAR
*lpbi
;
241 hpal
= GetStockObject(DEFAULT_PALETTE
);
243 GetObject(hbm
, sizeof (bm
), (LPSTR
) &bm
);
246 biBits
= bm
.bmPlanes
* bm
.bmBitsPixel
;
248 bi
.biSize
= sizeof(BITMAPINFOHEADER
);
249 bi
.biWidth
= bm
.bmWidth
;
250 bi
.biHeight
= bm
.bmHeight
;
252 bi
.biBitCount
= biBits
;
253 bi
.biCompression
= biStyle
;
255 bi
.biXPelsPerMeter
= 0;
256 bi
.biYPelsPerMeter
= 0;
258 bi
.biClrImportant
= 0;
260 dwLen
= bi
.biSize
+ PaletteSize(&bi
);
263 hpal
= SelectPalette(hdc
, hpal
, FALSE
);
266 hdib
= GlobalAlloc(GHND
, dwLen
);
269 SelectPalette(hdc
, hpal
, FALSE
);
270 ReleaseDC(NULL
, hdc
);
274 #ifdef __WINDOWS_386__
275 lpbi
= (BITMAPINFOHEADER FAR
*) MK_FP32(GlobalLock(hdib
));
277 lpbi
= (BITMAPINFOHEADER FAR
*) GlobalLock(hdib
);
282 /* call GetDIBits with a NULL lpBits param, so it will calculate the
283 * biSizeImage field for us
285 GetDIBits(hdc
, hbm
, 0, (WORD
) bi
.biHeight
,
286 NULL
, (LPBITMAPINFO
) lpbi
, DIB_RGB_COLORS
);
291 /* If the driver did not fill in the biSizeImage field, make one up */
292 if (bi
.biSizeImage
== 0) {
293 bi
.biSizeImage
= WIDTHBYTES((DWORD
)bm
.bmWidth
* biBits
) * bm
.bmHeight
;
295 if (biStyle
!= BI_RGB
)
296 bi
.biSizeImage
= (bi
.biSizeImage
* 3) / 2;
299 /* realloc the buffer big enough to hold all the bits */
300 dwLen
= bi
.biSize
+ PaletteSize(&bi
) + bi
.biSizeImage
;
301 if (h
= GlobalReAlloc(hdib
, dwLen
, 0))
307 SelectPalette(hdc
, hpal
, FALSE
);
308 ReleaseDC(NULL
, hdc
);
312 /* call GetDIBits with a NON-NULL lpBits param, and actualy get the
315 #ifdef __WINDOWS_386__
316 lpbi
= (BITMAPINFOHEADER FAR
*) MK_FP32(GlobalLock(hdib
));
318 lpbi
= (BITMAPINFOHEADER FAR
*) GlobalLock(hdib
);
325 (LPSTR
) lpbi
+ (WORD
) lpbi
->biSize
+ PaletteSize(lpbi
),
326 (LPBITMAPINFO
) lpbi
, DIB_RGB_COLORS
) == 0) {
329 SelectPalette(hdc
, hpal
, FALSE
);
330 ReleaseDC(NULL
, hdc
);
337 SelectPalette(hdc
, hpal
, FALSE
);
338 ReleaseDC(NULL
, hdc
);
343 /************* PRIVATE ROUTINES TO READ/WRITE MORE THAN 64K ***************/
344 /****************************************************************************
346 * FUNCTION : lread(int fh, VOID FAR *pv, DWORD ul) *
348 * PURPOSE : Reads data in steps of 32k till all the data has been read.*
350 * RETURNS : 0 - If read did not proceed correctly. *
351 * number of bytes read otherwise. *
353 ****************************************************************************/
355 DWORD PASCAL
lread(int fh
, void far
*pv
, DWORD ul
)
358 #if defined(WINNT) || defined(__WIN32__) || defined(__WIN32__)
359 BYTE
*hp
= (BYTE
*) pv
;
361 BYTE huge
*hp
= (BYTE huge
*) pv
;
363 while (ul
> (DWORD
) MAXREAD
) {
364 if (_lread(fh
, (LPSTR
) hp
, (WORD
) MAXREAD
) != MAXREAD
)
369 if (_lread(fh
, (LPSTR
) hp
, (WORD
) ul
) != (WORD
) ul
)
374 /****************************************************************************
376 * FUNCTION : lwrite(int fh, VOID FAR *pv, DWORD ul) *
378 * PURPOSE : Writes data in steps of 32k till all the data is written. *
380 * RETURNS : 0 - If write did not proceed correctly. *
381 * number of bytes written otherwise. *
383 ****************************************************************************/
385 DWORD PASCAL
lwrite(int fh
, VOID FAR
*pv
, DWORD ul
)
388 #if defined(WINNT) || defined(__WIN32__) || defined(__WIN32__)
389 BYTE
*hp
= (BYTE
*) pv
;
391 BYTE huge
*hp
= (BYTE huge
*) pv
;
393 while (ul
> MAXREAD
) {
394 if (_lwrite(fh
, (LPSTR
) hp
, (WORD
) MAXREAD
) != MAXREAD
)
399 if (_lwrite(fh
, (LPSTR
) hp
, (WORD
) ul
) != (WORD
) ul
)
404 /****************************************************************************
406 * FUNCTION : ReadDIB(hWnd)
408 * PURPOSE : Reads a DIB from a file, obtains a handle to its
409 * BITMAPINFO struct. and loads the DIB. Once the DIB
410 * is loaded, the function also creates a bitmap and
411 * palette out of the DIB for a device-dependent form.
413 * RETURNS : TRUE - DIB loaded and bitmap/palette created
414 * The DIBINIT structure pointed to by pInfo is
415 * filled with the appropriate handles.
418 ****************************************************************************/
419 BOOL
ReadDIB(LPSTR lpFileName
, HBITMAP
*bitmap
, HPALETTE
*palette
)
422 LPBITMAPINFOHEADER lpbi
;
430 BOOL bCoreHead
= FALSE
;
433 /* Open the file and get a handle to it's BITMAPINFO */
435 fh
= OpenFile (lpFileName
, &of
, OF_READ
);
437 wsprintf(str
,"Can't open file '%ls'", (LPSTR
)lpFileName
);
438 MessageBox(NULL
, str
, "Error", MB_ICONSTOP
| MB_OK
);
442 hDIB
= GlobalAlloc(GHND
, (DWORD
)(sizeof(BITMAPINFOHEADER
) +
443 256 * sizeof(RGBQUAD
)));
447 #ifdef __WINDOWS_386__
448 lpbi
= (LPBITMAPINFOHEADER
)MK_FP32(GlobalLock(hDIB
));
450 lpbi
= (LPBITMAPINFOHEADER
)GlobalLock(hDIB
);
453 /* read the BITMAPFILEHEADER */
454 if (sizeof (bf
) != _lread (fh
, (LPSTR
)&bf
, sizeof (bf
)))
457 if (bf
.bfType
!= 0x4d42) /* 'BM' */
460 if (sizeof(BITMAPCOREHEADER
) != _lread (fh
, (LPSTR
)lpbi
, sizeof(BITMAPCOREHEADER
)))
463 if (lpbi
->biSize
== sizeof(BITMAPCOREHEADER
))
465 lpbi
->biSize
= sizeof(BITMAPINFOHEADER
);
466 lpbi
->biBitCount
= ((LPBITMAPCOREHEADER
)lpbi
)->bcBitCount
;
467 lpbi
->biPlanes
= ((LPBITMAPCOREHEADER
)lpbi
)->bcPlanes
;
468 lpbi
->biHeight
= ((LPBITMAPCOREHEADER
)lpbi
)->bcHeight
;
469 lpbi
->biWidth
= ((LPBITMAPCOREHEADER
)lpbi
)->bcWidth
;
474 // get to the start of the header and read INFOHEADER
475 _llseek(fh
,sizeof(BITMAPFILEHEADER
),SEEK_SET
);
476 if (sizeof(BITMAPINFOHEADER
) != _lread (fh
, (LPSTR
)lpbi
, sizeof(BITMAPINFOHEADER
)))
480 nNumColors
= (WORD
)lpbi
->biClrUsed
;
481 if ( nNumColors
== 0 )
483 /* no color table for 24-bit, default size otherwise */
484 if (lpbi
->biBitCount
!= 24)
485 nNumColors
= 1 << lpbi
->biBitCount
; /* standard size table */
488 /* fill in some default values if they are zero */
489 if (lpbi
->biClrUsed
== 0)
490 lpbi
->biClrUsed
= nNumColors
;
492 if (lpbi
->biSizeImage
== 0)
494 lpbi
->biSizeImage
= ((((lpbi
->biWidth
* (DWORD
)lpbi
->biBitCount
) + 31) & ~31) >> 3)
498 /* get a proper-sized buffer for header, color table and bits */
500 hDIB
= GlobalReAlloc(hDIB
, lpbi
->biSize
+
501 nNumColors
* sizeof(RGBQUAD
) +
502 lpbi
->biSizeImage
, 0);
503 if (!hDIB
) /* can't resize buffer for loading */
506 #ifdef __WINDOWS_386__
507 lpbi
= (LPBITMAPINFOHEADER
)MK_FP32(GlobalLock(hDIB
));
509 lpbi
= (LPBITMAPINFOHEADER
)GlobalLock(hDIB
);
512 /* read the color table */
514 _lread(fh
, (LPSTR
)(lpbi
) + lpbi
->biSize
, nNumColors
* sizeof(RGBQUAD
));
519 RGBTRIPLE FAR
*pTriple
;
521 _lread(fh
, (LPSTR
)(lpbi
) + lpbi
->biSize
, nNumColors
* sizeof(RGBTRIPLE
));
523 pQuad
= (RGBQUAD FAR
*)((LPSTR
)lpbi
+ lpbi
->biSize
);
524 pTriple
= (RGBTRIPLE FAR
*) pQuad
;
525 for (i
= nNumColors
- 1; i
>= 0; i
--)
527 pQuad
[i
].rgbRed
= pTriple
[i
].rgbtRed
;
528 pQuad
[i
].rgbBlue
= pTriple
[i
].rgbtBlue
;
529 pQuad
[i
].rgbGreen
= pTriple
[i
].rgbtGreen
;
530 pQuad
[i
].rgbReserved
= 0;
534 /* offset to the bits from start of DIB header */
535 offBits
= (WORD
)lpbi
->biSize
+ nNumColors
* sizeof(RGBQUAD
);
537 if (bf
.bfOffBits
!= 0L)
539 _llseek(fh
,bf
.bfOffBits
,SEEK_SET
);
542 if (lpbi
->biSizeImage
== lread(fh
, (LPSTR
)lpbi
+ offBits
, lpbi
->biSizeImage
))
547 if (!MakeBitmapAndPalette(hDC
, hDIB
, palette
,
572 /****************************************************************************
574 * FUNCTION : MakeBitmapAndPalette
576 * PURPOSE : Given a DIB, creates a bitmap and corresponding palette
577 * to be used for a device-dependent representation of
580 * RETURNS : TRUE --> success. phPal and phBitmap are filled with
581 * appropriate handles. Caller is responsible
582 * for freeing objects.
583 * FALSE --> unable to create objects. both pointer are
586 ****************************************************************************/
587 BOOL PASCAL
MakeBitmapAndPalette(HDC hDC
, HANDLE hDIB
,
588 HPALETTE
* phPal
, HBITMAP
* phBitmap
)
590 LPBITMAPINFOHEADER lpInfo
;
593 HPALETTE hPalette
, hOldPal
;
596 #ifdef __WINDOWS_386__
597 lpInfo
= (LPBITMAPINFOHEADER
) MK_FP32(GlobalLock(hDIB
));
599 lpInfo
= (LPBITMAPINFOHEADER
) GlobalLock(hDIB
);
602 hPalette
= MakeDIBPalette(lpInfo
);
605 // Need to realize palette for converting DIB to bitmap.
606 hOldPal
= SelectPalette(hDC
, hPalette
, TRUE
);
609 lpBits
= (LPSTR
)lpInfo
+ (WORD
)lpInfo
->biSize
+
610 (WORD
)lpInfo
->biClrUsed
* sizeof(RGBQUAD
);
611 hBitmap
= CreateDIBitmap(hDC
, lpInfo
, CBM_INIT
, lpBits
,
612 (LPBITMAPINFO
)lpInfo
, DIB_RGB_COLORS
);
614 SelectPalette(hDC
, hOldPal
, TRUE
);
618 DeleteObject(hPalette
);
629 /****************************************************************************
631 * FUNCTION : MakeDIBPalette(lpInfo) *
633 * PURPOSE : Given a BITMAPINFOHEADER, create a palette based on
637 * RETURNS : non-zero - handle of a corresponding palette
638 * zero - unable to create palette
640 ****************************************************************************/
641 HPALETTE
MakeDIBPalette(LPBITMAPINFOHEADER lpInfo
)
648 /* since biClrUsed field was filled during the loading of the DIB,
649 ** we know it contains the number of colors in the color table.
651 if (lpInfo
->biClrUsed
)
654 npPal = (NPLOGPALETTE)LocalAlloc(LMEM_FIXED, sizeof(LOGPALETTE) +
655 (WORD)lpInfo->biClrUsed * sizeof(PALETTEENTRY));
657 npPal
= (NPLOGPALETTE
)malloc(sizeof(LOGPALETTE
) +
658 (WORD
)lpInfo
->biClrUsed
* sizeof(PALETTEENTRY
));
663 npPal
->palVersion
= 0x300;
664 npPal
->palNumEntries
= (WORD
)lpInfo
->biClrUsed
;
666 /* get pointer to the color table */
667 lpRGB
= (RGBQUAD FAR
*)((LPSTR
)lpInfo
+ lpInfo
->biSize
);
669 /* copy colors from the color table to the LogPalette structure */
670 for (i
= 0; i
< lpInfo
->biClrUsed
; i
++, lpRGB
++)
672 npPal
->palPalEntry
[i
].peRed
= lpRGB
->rgbRed
;
673 npPal
->palPalEntry
[i
].peGreen
= lpRGB
->rgbGreen
;
674 npPal
->palPalEntry
[i
].peBlue
= lpRGB
->rgbBlue
;
675 npPal
->palPalEntry
[i
].peFlags
= 0;
678 hLogPal
= CreatePalette((LPLOGPALETTE
)npPal
);
679 // LocalFree((HANDLE)npPal);
685 /* 24-bit DIB with no color table. return default palette. Another
686 ** option would be to create a 256 color "rainbow" palette to provide
687 ** some good color choices.
690 return((HPALETTE
) GetStockObject(DEFAULT_PALETTE
));
693 bool wxLoadIntoBitmap(char *filename
, wxBitmap
*bitmap
, wxPalette
**pal
)
698 bool success
= (ReadDIB(filename
, &hBitmap
, &hPalette
) != 0);
702 DeleteObject(hPalette
);
710 *pal
= new wxPalette
;
711 (*pal
)->SetHPALETTE((WXHPALETTE
) hPalette
);
714 DeleteObject(hPalette
);
722 GetObject(hBitmap
, sizeof(bm
), (LPSTR
)&bm
);
724 bitmap
->SetHBITMAP((WXHBITMAP
) hBitmap
);
725 bitmap
->SetWidth(bm
.bmWidth
);
726 bitmap
->SetHeight(bm
.bmHeight
);
727 bitmap
->SetDepth(bm
.bmPlanes
* bm
.bmBitsPixel
);
734 wxBitmap
*wxLoadBitmap(char *filename
, wxPalette
**pal
)
736 wxBitmap
*bitmap
= new wxBitmap
;
737 if (wxLoadIntoBitmap(filename
, bitmap
, pal
))
746 //---------------------------------------------------------------------
748 // Function: InitBitmapInfoHeader
750 // Purpose: Does a "standard" initialization of a BITMAPINFOHEADER,
751 // given the Width, Height, and Bits per Pixel for the
754 // By standard, I mean that all the relevant fields are set
755 // to the specified values. biSizeImage is computed, the
756 // biCompression field is set to "no compression," and all
757 // other fields are 0.
759 // Note that DIBs only allow BitsPixel values of 1, 4, 8, or
760 // 24. This routine makes sure that one of these values is
761 // used (whichever is most appropriate for the specified
764 // Parms: lpBmInfoHdr == Far pointer to a BITMAPINFOHEADER structure
766 // dwWidth == Width of DIB (not in Win 3.0 & 3.1, high
768 // dwHeight == Height of DIB (not in Win 3.0 & 3.1, high
770 // nBPP == Bits per Pixel for the DIB.
772 // History: Date Reason
775 //---------------------------------------------------------------------
777 void InitBitmapInfoHeader (LPBITMAPINFOHEADER lpBmInfoHdr
,
782 // _fmemset (lpBmInfoHdr, 0, sizeof (BITMAPINFOHEADER));
783 memset (lpBmInfoHdr
, 0, sizeof (BITMAPINFOHEADER
));
785 lpBmInfoHdr
->biSize
= sizeof (BITMAPINFOHEADER
);
786 lpBmInfoHdr
->biWidth
= dwWidth
;
787 lpBmInfoHdr
->biHeight
= dwHeight
;
788 lpBmInfoHdr
->biPlanes
= 1;
803 lpBmInfoHdr
->biBitCount
= nBPP
;
804 lpBmInfoHdr
->biSizeImage
= WIDTHBYTES (dwWidth
* nBPP
) * dwHeight
;
810 LPSTR
FindDIBBits (LPSTR lpbi
)
812 return (lpbi
+ *(LPDWORD
)lpbi
+ PaletteSize (lpbi
));
815 //---------------------------------------------------------------------
817 // Function: BitmapToDIB
819 // Purpose: Given a device dependent bitmap and a palette, returns
820 // a handle to global memory with a DIB spec in it. The
821 // DIB is rendered using the colors of the palette passed in.
823 // Stolen almost verbatim from ShowDIB.
825 // Parms: hBitmap == Handle to device dependent bitmap compatible
826 // with default screen display device.
827 // hPal == Palette to render the DDB with. If it's NULL,
828 // use the default palette.
830 // History: Date Reason
833 //---------------------------------------------------------------------
835 HANDLE
BitmapToDIB (HBITMAP hBitmap
, HPALETTE hPal
)
838 BITMAPINFOHEADER bmInfoHdr
;
839 LPBITMAPINFOHEADER lpbmInfoHdr
;
843 HPALETTE hOldPal
= NULL
;
845 // Do some setup -- make sure the Bitmap passed in is valid,
846 // get info on the bitmap (like its height, width, etc.),
847 // then setup a BITMAPINFOHEADER.
852 if (!GetObject (hBitmap
, sizeof (Bitmap
), (LPSTR
) &Bitmap
))
855 InitBitmapInfoHeader (&bmInfoHdr
,
858 Bitmap
.bmPlanes
* Bitmap
.bmBitsPixel
);
861 // Now allocate memory for the DIB. Then, set the BITMAPINFOHEADER
862 // into this memory, and find out where the bitmap bits go.
864 hDIB
= GlobalAlloc (GHND
, sizeof (BITMAPINFOHEADER
) +
865 PaletteSize ((LPSTR
) &bmInfoHdr
) + bmInfoHdr
.biSizeImage
);
870 #ifdef __WINDOWS_386__
871 lpbmInfoHdr
= (LPBITMAPINFOHEADER
) MK_FP32(GlobalLock (hDIB
));
873 lpbmInfoHdr
= (LPBITMAPINFOHEADER
) GlobalLock (hDIB
);
876 *lpbmInfoHdr
= bmInfoHdr
;
877 lpBits
= FindDIBBits ((LPSTR
) lpbmInfoHdr
);
880 // Now, we need a DC to hold our bitmap. If the app passed us
881 // a palette, it should be selected into the DC.
883 hMemDC
= GetDC (NULL
);
887 hOldPal
= SelectPalette (hMemDC
, hPal
, FALSE
);
888 RealizePalette (hMemDC
);
893 // We're finally ready to get the DIB. Call the driver and let
894 // it party on our bitmap. It will fill in the color table,
895 // and bitmap bits of our global memory block.
897 if (!GetDIBits (hMemDC
,
902 (LPBITMAPINFO
) lpbmInfoHdr
,
913 // Finally, clean up and return.
916 SelectPalette (hMemDC
, hOldPal
, FALSE
);
918 ReleaseDC (NULL
, hMemDC
);
923 bool wxSaveBitmap(char *filename
, wxBitmap
*bitmap
, wxPalette
*colourmap
)
925 HPALETTE hPalette
= 0;
927 hPalette
= (HPALETTE
) colourmap
->GetHPALETTE();
929 HANDLE dibHandle
= BitmapToDIB((HBITMAP
) bitmap
->GetHBITMAP(), hPalette
);
932 bool success
= (WriteDIB(filename
, dibHandle
) != 0);
933 GlobalFree(dibHandle
);