1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/msw/dib.cpp
3 // Purpose: implements wxDIB class
4 // Author: Vadim Zeitlin
6 // Created: 03.03.03 (replaces the old file with the same name)
8 // Copyright: (c) 2003 Vadim Zeitlin <vadim@wxwindows.org>
9 // License: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
28 #include "wx/string.h"
32 #include "wx/bitmap.h"
38 #if !defined(__MWERKS__) && !defined(__SALFORDC__)
42 #ifdef __GNUWIN32_OLD__
43 #include "wx/msw/gnuwin32/extra.h"
47 #include "wx/msw/dib.h"
49 // ============================================================================
51 // ============================================================================
53 // ----------------------------------------------------------------------------
55 // ----------------------------------------------------------------------------
57 bool wxDIB::Create(int width
, int height
, int depth
)
59 // we don't handle the palette yet
60 wxASSERT_MSG( depth
== 24 || depth
== 32,
61 _T("unsupported image depth in wxDIB::Create()") );
63 static const int infosize
= sizeof(BITMAPINFOHEADER
);
65 BITMAPINFO
*info
= (BITMAPINFO
*)malloc(infosize
);
66 wxCHECK_MSG( info
, NULL
, _T("malloc(BITMAPINFO) failed") );
68 memset(info
, 0, infosize
);
70 info
->bmiHeader
.biSize
= infosize
;
71 info
->bmiHeader
.biWidth
= width
;
72 info
->bmiHeader
.biHeight
= -height
;
73 info
->bmiHeader
.biPlanes
= 1;
74 info
->bmiHeader
.biBitCount
= depth
;
75 info
->bmiHeader
.biCompression
= BI_RGB
;
76 info
->bmiHeader
.biSizeImage
= GetLineSize(width
, depth
)*height
;
78 // No need to report an error here. If it fails, we just won't use a
79 // file mapping and CreateDIBSection will just allocate memory for us.
80 m_handle
= ::CreateDIBSection
82 0, // hdc (unused with DIB_RGB_COLORS)
83 info
, // bitmap description
84 DIB_RGB_COLORS
, // use RGB, not palette
85 &m_data
, // [out] DIB bits
86 NULL
, // don't use file mapping
87 0 // file mapping offset (not used here)
94 wxLogLastError(wxT("CreateDIBSection"));
106 // ----------------------------------------------------------------------------
108 // ----------------------------------------------------------------------------
110 void wxDIB::DoGetObject() const
112 // only do something if we have a valid DIB but we don't [yet] have valid
114 if ( m_handle
&& !m_data
)
116 // although all the info we need is in BITMAP and so we don't really
117 // need DIBSECTION we still ask for it as modifying the bit values only
118 // works for the real DIBs and not for the bitmaps and it's better to
119 // check for this now rather than trying to find out why it doesn't
122 if ( !::GetObject(m_handle
, sizeof(ds
), &ds
) )
124 wxLogLastError(_T("GetObject(hDIB)"));
129 wxDIB
*self
= wxConstCast(this, wxDIB
);
131 self
->m_width
= ds
.dsBm
.bmWidth
;
132 self
->m_height
= ds
.dsBm
.bmHeight
;
133 self
->m_depth
= ds
.dsBm
.bmBitsPixel
;
134 self
->m_data
= ds
.dsBm
.bmBits
;
138 // ----------------------------------------------------------------------------
140 // ----------------------------------------------------------------------------
144 bool wxDIB::Create(const wxImage
& image
)
146 wxCHECK_MSG( image
.Ok(), false, _T("invalid wxImage in wxDIB ctor") );
148 const int h
= image
.GetHeight();
149 const int w
= image
.GetWidth();
151 // if we have alpha channel, we need to create a 32bpp RGBA DIB, otherwise
152 // a 24bpp RGB is sufficient
153 const bool hasAlpha
= image
.HasAlpha();
154 const int bpp
= hasAlpha
? 32 : 24;
156 if ( !Create(w
, h
, bpp
) )
159 // DIBs are stored in bottom to top order so we need to copy bits line by
160 // line and starting from the end
161 const int srcBytesPerLine
= w
* 3;
162 const int dstBytesPerLine
= GetLineSize(w
, bpp
);
163 const unsigned char *src
= image
.GetData() + ((h
- 1) * srcBytesPerLine
);
164 const unsigned char *alpha
= hasAlpha
? image
.GetAlpha() + (h
- 1)*w
: NULL
;
165 unsigned char *dstLineStart
= (unsigned char *)m_data
;
166 for ( int y
= 0; y
< h
; y
++ )
169 unsigned char *dst
= dstLineStart
;
170 for ( int x
= 0; x
< w
; x
++ )
172 // also, the order of RGB is inversed for DIBs
183 // pass to the previous line in the image
184 src
-= 2*srcBytesPerLine
;
188 // and to the next one in the DIB
189 dstLineStart
+= dstBytesPerLine
;
195 #endif // wxUSE_IMAGE
197 // ============================================================================
198 // old DIB code, to be integrated in wxDIB class
199 // ============================================================================
202 * Routines for dealing with Device Independent Bitmaps.
204 * wxReadDIB() - Reads a DIB
205 * wxWriteDIB() - Writes a global handle in CF_DIB format
207 * wxPaletteSize() - Calculates the palette size in bytes
209 * wxDibNumColors() - Determines the number of colors in DIB
210 * wxDibFromBitmap() - Creates a DIB repr. the DDB passed in.
211 * lread() - Private routine to read more than 64k
212 * lwrite() - Private routine to write more than 64k
216 /* flags for _lseek */
222 #define MAXREAD 32768 /* Number of bytes to be read during */
223 /* each read operation. */
225 /* Header signatutes for various resources */
226 #define BFT_ICON 0x4349 /* 'IC' */
227 #define BFT_BITMAP 0x4d42 /* 'BM' */
228 #define BFT_CURSOR 0x5450 /* 'PT(' */
230 /* macro to determine if resource is a DIB */
231 #define ISDIB(bft) ((bft) == BFT_BITMAP)
233 /* Macro to align given value to the closest DWORD (unsigned long ) */
234 #define ALIGNULONG(i) ((i+3)/4*4)
236 /* Macro to determine to round off the given value to the closest byte */
237 #define WIDTHBYTES(i) ((i+31)/32*4)
239 #define PALVERSION 0x300
240 #define MAXPALETTE 256 /* max. # supported palette entries */
242 static DWORD
lread(int fh
, VOID FAR
*pv
, DWORD ul
);
243 static DWORD
lwrite(int fh
, VOID FAR
*pv
, DWORD ul
);
245 static bool wxWriteDIB (LPTSTR szFile
,HANDLE hdib
);
246 WORD
wxPaletteSize (VOID FAR
* pv
); // This is non-static as some apps use it externally
247 static WORD
wxDibNumColors (VOID FAR
* pv
);
248 static bool wxMakeBitmapAndPalette(HDC
,HANDLE
,HPALETTE
*,HBITMAP
*);
251 * FUNCTION : wxWriteDIB(LPSTR szFile,HANDLE hdib)
252 * PURPOSE : Write a global handle in CF_DIB format to a file.
253 * RETURNS : TRUE - if successful.
257 static bool wxWriteDIB(LPTSTR szFile
, HANDLE hdib
)
259 BITMAPFILEHEADER hdr
;
260 LPBITMAPINFOHEADER lpbi
;
267 fh
= OpenFile(wxConvertWX2MB(szFile
), &of
, OF_CREATE
| OF_READWRITE
);
271 lpbi
= (LPBITMAPINFOHEADER
) GlobalLock(hdib
);
273 /* Fill in the fields of the file header */
274 hdr
.bfType
= BFT_BITMAP
;
275 hdr
.bfSize
= GlobalSize(hdib
) + sizeof(BITMAPFILEHEADER
);
278 hdr
.bfOffBits
= (DWORD
) sizeof(BITMAPFILEHEADER
) + lpbi
->biSize
+
281 /* Write the file header */
282 _lwrite(fh
, (LPSTR
) &hdr
, sizeof(BITMAPFILEHEADER
));
284 /* Write the DIB header and the bits */
285 lwrite(fh
, (LPSTR
) lpbi
, GlobalSize(hdib
));
293 * FUNCTION : wxPaletteSize(VOID FAR * pv)
294 * PURPOSE : Calculates the palette size in bytes. If the info. block
295 * is of the BITMAPCOREHEADER type, the number of colors is
296 * multiplied by 3 to give the palette size, otherwise the
297 * number of colors is multiplied by 4.
298 * RETURNS : Palette size in number of bytes.
301 WORD
wxPaletteSize(VOID FAR
* pv
)
303 LPBITMAPINFOHEADER lpbi
;
306 lpbi
= (LPBITMAPINFOHEADER
) pv
;
307 NumColors
= wxDibNumColors(lpbi
);
309 if (lpbi
->biSize
== sizeof(BITMAPCOREHEADER
))
310 return (WORD
)(NumColors
* sizeof(RGBTRIPLE
));
312 return (WORD
)(NumColors
* sizeof(RGBQUAD
));
316 * FUNCTION : wxDibNumColors(VOID FAR * pv)
317 * PURPOSE : Determines the number of colors in the DIB by looking at
318 * the BitCount filed in the info block.
319 * RETURNS : The number of colors in the DIB. *
322 static WORD
wxDibNumColors(VOID FAR
*pv
)
325 BITMAPINFOHEADER
*lpbi
;
326 BITMAPCOREHEADER
*lpbc
;
328 lpbi
= ((BITMAPINFOHEADER
*) pv
);
329 lpbc
= ((BITMAPCOREHEADER
*) pv
);
331 /* With the BITMAPINFO format headers, the size of the palette
332 * is in biClrUsed, whereas in the BITMAPCORE - style headers, it
333 * is dependent on the bits per pixel ( = 2 raised to the power of
336 if (lpbi
->biSize
!= sizeof(BITMAPCOREHEADER
)) {
337 if (lpbi
->biClrUsed
!= 0)
338 return (WORD
) lpbi
->biClrUsed
;
339 bits
= lpbi
->biBitCount
;
342 bits
= lpbc
->bcBitCount
;
352 /* A 24 bitcount DIB has no color table */
358 * FUNCTION : lread(int fh, VOID FAR *pv, DWORD ul)
359 * PURPOSE : Reads data in steps of 32k till all the data has been read.
360 * RETURNS : 0 - If read did not proceed correctly.
361 * number of bytes read otherwise.
364 static DWORD
lread(int fh
, void far
*pv
, DWORD ul
)
367 #if defined(WINNT) || defined(__WIN32__) || defined(__WIN32__)
368 BYTE
*hp
= (BYTE
*) pv
;
370 BYTE huge
*hp
= (BYTE huge
*) pv
;
372 while (ul
> (DWORD
) MAXREAD
) {
373 if (_lread(fh
, (LPSTR
) hp
, (WORD
) MAXREAD
) != MAXREAD
)
378 if (_lread(fh
, (LPSTR
) hp
, (WXUINT
) ul
) != (WXUINT
) ul
)
384 * FUNCTION : lwrite(int fh, VOID FAR *pv, DWORD ul)
385 * PURPOSE : Writes data in steps of 32k till all the data is written.
386 * RETURNS : 0 - If write did not proceed correctly.
387 * number of bytes written otherwise.
390 static DWORD
lwrite(int fh
, VOID FAR
*pv
, DWORD ul
)
393 #if defined(WINNT) || defined(__WIN32__) || defined(__WIN32__)
394 BYTE
*hp
= (BYTE
*) pv
;
396 BYTE huge
*hp
= (BYTE huge
*) pv
;
398 while (ul
> MAXREAD
) {
399 if (_lwrite(fh
, (LPSTR
) hp
, (WORD
) MAXREAD
) != MAXREAD
)
404 if (_lwrite(fh
, (LPSTR
) hp
, (WXUINT
) ul
) != (WXUINT
) ul
)
410 * FUNCTION : wxReadDIB(hWnd)
411 * PURPOSE : Reads a DIB from a file, obtains a handle to its
412 * BITMAPINFO struct. and loads the DIB. Once the DIB
413 * is loaded, the function also creates a bitmap and
414 * palette out of the DIB for a device-dependent form.
415 * RETURNS : TRUE - DIB loaded and bitmap/palette created
416 * The DIBINIT structure pointed to by pInfo is
417 * filled with the appropriate handles.
421 bool wxReadDIB(LPTSTR lpFileName
, HBITMAP
*bitmap
, HPALETTE
*palette
)
424 LPBITMAPINFOHEADER lpbi
;
431 bool bCoreHead
= FALSE
;
434 /* Open the file and get a handle to it's BITMAPINFO */
436 fh
= OpenFile (wxConvertWX2MB(lpFileName
), &of
, OF_READ
);
438 wxLogError(_("Can't open file '%s'"), lpFileName
);
442 hDIB
= GlobalAlloc(GHND
, (DWORD
)(sizeof(BITMAPINFOHEADER
) +
443 256 * sizeof(RGBQUAD
)));
447 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 lpbi
= (LPBITMAPINFOHEADER
)GlobalLock(hDIB
);
504 /* read the color table */
506 _lread(fh
, (LPSTR
)(lpbi
) + lpbi
->biSize
, nNumColors
* sizeof(RGBQUAD
));
511 RGBTRIPLE FAR
*pTriple
;
513 _lread(fh
, (LPSTR
)(lpbi
) + lpbi
->biSize
, nNumColors
* sizeof(RGBTRIPLE
));
515 pQuad
= (RGBQUAD FAR
*)((LPSTR
)lpbi
+ lpbi
->biSize
);
516 pTriple
= (RGBTRIPLE FAR
*) pQuad
;
517 for (i
= nNumColors
- 1; i
>= 0; i
--)
519 pQuad
[i
].rgbRed
= pTriple
[i
].rgbtRed
;
520 pQuad
[i
].rgbBlue
= pTriple
[i
].rgbtBlue
;
521 pQuad
[i
].rgbGreen
= pTriple
[i
].rgbtGreen
;
522 pQuad
[i
].rgbReserved
= 0;
526 /* offset to the bits from start of DIB header */
527 offBits
= (WORD
)(lpbi
->biSize
+ nNumColors
* sizeof(RGBQUAD
));
529 if (bf
.bfOffBits
!= 0L)
531 _llseek(fh
,bf
.bfOffBits
,SEEK_SET
);
534 if (lpbi
->biSizeImage
== lread(fh
, (LPSTR
)lpbi
+ offBits
, lpbi
->biSizeImage
))
539 if (!wxMakeBitmapAndPalette(hDC
, hDIB
, palette
,
565 * FUNCTION : wxMakeBitmapAndPalette
566 * PURPOSE : Given a DIB, creates a bitmap and corresponding palette
567 * to be used for a device-dependent representation of
569 * RETURNS : TRUE --> success. phPal and phBitmap are filled with
570 * appropriate handles. Caller is responsible
571 * for freeing objects.
572 * FALSE --> unable to create objects. both pointer are
576 static bool wxMakeBitmapAndPalette(HDC hDC
, HANDLE hDIB
,
577 HPALETTE
* phPal
, HBITMAP
* phBitmap
)
579 LPBITMAPINFOHEADER lpInfo
;
582 HPALETTE hPalette
, hOldPal
;
585 lpInfo
= (LPBITMAPINFOHEADER
) GlobalLock(hDIB
);
587 hPalette
= wxMakeDIBPalette(lpInfo
);
590 // Need to realize palette for converting DIB to bitmap.
591 hOldPal
= SelectPalette(hDC
, hPalette
, TRUE
);
594 lpBits
= (LPSTR
)lpInfo
+ (WORD
)lpInfo
->biSize
+
595 (WORD
)lpInfo
->biClrUsed
* sizeof(RGBQUAD
);
596 hBitmap
= CreateDIBitmap(hDC
, lpInfo
, CBM_INIT
, lpBits
,
597 (LPBITMAPINFO
)lpInfo
, DIB_RGB_COLORS
);
599 SelectPalette(hDC
, hOldPal
, TRUE
);
603 DeleteObject(hPalette
);
612 GlobalUnlock (hDIB
); // glt
618 * FUNCTION : wxMakeDIBPalette(lpInfo)
619 * PURPOSE : Given a BITMAPINFOHEADER, create a palette based on
621 * RETURNS : non-zero - handle of a corresponding palette
622 * zero - unable to create palette
625 HPALETTE
wxMakeDIBPalette(LPBITMAPINFOHEADER lpInfo
)
632 /* since biClrUsed field was filled during the loading of the DIB,
633 * we know it contains the number of colors in the color table.
635 if (lpInfo
->biClrUsed
)
637 npPal
= (LPLOGPALETTE
)malloc(sizeof(LOGPALETTE
) +
638 (WORD
)lpInfo
->biClrUsed
* sizeof(PALETTEENTRY
));
642 npPal
->palVersion
= 0x300;
643 npPal
->palNumEntries
= (WORD
)lpInfo
->biClrUsed
;
645 /* get pointer to the color table */
646 lpRGB
= (RGBQUAD FAR
*)((LPSTR
)lpInfo
+ lpInfo
->biSize
);
648 /* copy colors from the color table to the LogPalette structure */
649 for (i
= 0; (DWORD
)i
< lpInfo
->biClrUsed
; i
++, lpRGB
++)
651 npPal
->palPalEntry
[i
].peRed
= lpRGB
->rgbRed
;
652 npPal
->palPalEntry
[i
].peGreen
= lpRGB
->rgbGreen
;
653 npPal
->palPalEntry
[i
].peBlue
= lpRGB
->rgbBlue
;
654 npPal
->palPalEntry
[i
].peFlags
= 0;
657 hLogPal
= CreatePalette((LPLOGPALETTE
)npPal
);
663 /* 24-bit DIB with no color table. Return default palette. Another
664 * option would be to create a 256 color "rainbow" palette to provide
665 * some good color choices.
668 return((HPALETTE
) GetStockObject(DEFAULT_PALETTE
));
671 bool wxLoadIntoBitmap(wxChar
*filename
, wxBitmap
*bitmap
, wxPalette
**pal
)
673 HBITMAP hBitmap
= NULL
;
674 HPALETTE hPalette
= NULL
;
676 bool success
= (wxReadDIB(filename
, &hBitmap
, &hPalette
) != 0);
681 DeleteObject(hPalette
);
690 *pal
= new wxPalette
;
691 (*pal
)->SetHPALETTE((WXHPALETTE
) hPalette
);
694 #endif // wxUSE_PALETTE
695 DeleteObject(hPalette
);
703 GetObject(hBitmap
, sizeof(bm
), (LPSTR
)&bm
);
705 bitmap
->SetHBITMAP((WXHBITMAP
) hBitmap
);
706 bitmap
->SetWidth(bm
.bmWidth
);
707 bitmap
->SetHeight(bm
.bmHeight
);
708 bitmap
->SetDepth(bm
.bmPlanes
* bm
.bmBitsPixel
);
709 #if WXWIN_COMPATIBILITY_2
711 #endif // WXWIN_COMPATIBILITY_2
717 wxBitmap
*wxLoadBitmap(wxChar
*filename
, wxPalette
**pal
)
719 wxBitmap
*bitmap
= new wxBitmap
;
720 if (wxLoadIntoBitmap(filename
, bitmap
, pal
))
731 * Function: InitBitmapInfoHeader
733 * Purpose: Does a "standard" initialization of a BITMAPINFOHEADER,
734 * given the Width, Height, and Bits per Pixel for the
737 * By standard, I mean that all the relevant fields are set
738 * to the specified values. biSizeImage is computed, the
739 * biCompression field is set to "no compression," and all
740 * other fields are 0.
742 * Note that DIBs only allow BitsPixel values of 1, 4, 8, or
743 * 24. This routine makes sure that one of these values is
744 * used (whichever is most appropriate for the specified
747 * Parms: lpBmInfoHdr == Far pointer to a BITMAPINFOHEADER structure
749 * dwWidth == Width of DIB (not in Win 3.0 & 3.1, high
751 * dwHeight == Height of DIB (not in Win 3.0 & 3.1, high
753 * nBPP == Bits per Pixel for the DIB.
757 static void InitBitmapInfoHeader (LPBITMAPINFOHEADER lpBmInfoHdr
,
762 // _fmemset (lpBmInfoHdr, 0, sizeof (BITMAPINFOHEADER));
763 memset (lpBmInfoHdr
, 0, sizeof (BITMAPINFOHEADER
));
765 lpBmInfoHdr
->biSize
= sizeof (BITMAPINFOHEADER
);
766 lpBmInfoHdr
->biWidth
= dwWidth
;
767 lpBmInfoHdr
->biHeight
= dwHeight
;
768 lpBmInfoHdr
->biPlanes
= 1;
783 lpBmInfoHdr
->biBitCount
= nBPP
;
784 lpBmInfoHdr
->biSizeImage
= WIDTHBYTES (dwWidth
* nBPP
) * dwHeight
;
787 LPSTR
wxFindDIBBits (LPSTR lpbi
)
789 return (lpbi
+ *(LPDWORD
)lpbi
+ wxPaletteSize (lpbi
));
793 * Function: BitmapToDIB
795 * Purpose: Given a device dependent bitmap and a palette, returns
796 * a handle to global memory with a DIB spec in it. The
797 * DIB is rendered using the colors of the palette passed in.
799 * Parms: hBitmap == Handle to device dependent bitmap compatible
800 * with default screen display device.
801 * hPal == Palette to render the DDB with. If it's NULL,
802 * use the default palette.
805 HANDLE
wxBitmapToDIB (HBITMAP hBitmap
, HPALETTE hPal
)
808 BITMAPINFOHEADER bmInfoHdr
;
809 LPBITMAPINFOHEADER lpbmInfoHdr
;
813 HPALETTE hOldPal
= NULL
;
815 // Do some setup -- make sure the Bitmap passed in is valid,
816 // get info on the bitmap (like its height, width, etc.),
817 // then setup a BITMAPINFOHEADER.
822 if (!GetObject (hBitmap
, sizeof (Bitmap
), (LPSTR
) &Bitmap
))
825 InitBitmapInfoHeader (&bmInfoHdr
,
828 Bitmap
.bmPlanes
* Bitmap
.bmBitsPixel
);
830 // Now allocate memory for the DIB. Then, set the BITMAPINFOHEADER
831 // into this memory, and find out where the bitmap bits go.
833 hDIB
= GlobalAlloc (GHND
, sizeof (BITMAPINFOHEADER
) +
834 wxPaletteSize ((LPSTR
) &bmInfoHdr
) + bmInfoHdr
.biSizeImage
);
839 lpbmInfoHdr
= (LPBITMAPINFOHEADER
) GlobalLock (hDIB
);
841 *lpbmInfoHdr
= bmInfoHdr
;
842 lpBits
= wxFindDIBBits ((LPSTR
) lpbmInfoHdr
);
845 // Now, we need a DC to hold our bitmap. If the app passed us
846 // a palette, it should be selected into the DC.
848 hMemDC
= GetDC (NULL
);
852 hOldPal
= SelectPalette (hMemDC
, hPal
, FALSE
);
853 RealizePalette (hMemDC
);
856 // We're finally ready to get the DIB. Call the driver and let
857 // it party on our bitmap. It will fill in the color table,
858 // and bitmap bits of our global memory block.
860 if (!GetDIBits (hMemDC
, hBitmap
, 0, Bitmap
.bmHeight
, lpBits
,
861 (LPBITMAPINFO
) lpbmInfoHdr
, DIB_RGB_COLORS
))
870 // Finally, clean up and return.
873 SelectPalette (hMemDC
, hOldPal
, FALSE
);
875 ReleaseDC (NULL
, hMemDC
);
880 bool wxSaveBitmap(wxChar
*filename
, wxBitmap
*bitmap
, wxPalette
*palette
)
882 HPALETTE hPalette
= 0;
885 hPalette
= (HPALETTE
) palette
->GetHPALETTE();
886 #endif // wxUSE_PALETTE
888 HANDLE dibHandle
= wxBitmapToDIB((HBITMAP
) bitmap
->GetHBITMAP(), hPalette
);
891 bool success
= (wxWriteDIB(filename
, dibHandle
) != 0);
892 GlobalFree(dibHandle
);