1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxDIB class representing Win32 device independent bitmaps
4 // Author: Vadim Zeitlin
6 // Created: 03.03.03 (replaces the old file with the same name)
8 // Copyright: (c) 1997-2003 wxWindows team
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 #ifndef _WX_MSW_DIB_H_
13 #define _WX_MSW_DIB_H_
15 class WXDLLEXPORT wxBitmap
;
16 class WXDLLEXPORT wxPalette
;
18 #include "wx/msw/private.h"
20 // ----------------------------------------------------------------------------
21 // wxDIB: represents a DIB section
22 // ----------------------------------------------------------------------------
24 class WXDLLEXPORT wxDIB
30 // create an uninitialized DIB with the given width, height and depth (only
31 // 24 and 32 bpp DIBs are currently supported)
33 // after using this ctor, GetData() and GetHandle() may be used if IsOk()
35 wxDIB(int width
, int height
, int depth
)
36 { Init(); (void)Create(width
, height
, depth
); }
38 // load a DIB from file (any depth is supoprted here unlike above)
40 // as above, use IsOk() to see if the bitmap was loaded successfully
41 wxDIB(const wxString
& filename
)
42 { Init(); (void)Load(filename
); }
44 // same as the corresponding ctors but with return value
45 bool Create(int width
, int height
, int depth
);
46 bool Load(const wxString
& filename
);
48 // dtor is not virtual, this class is not meant to be used polymorphically
55 // create a bitmap compatiblr with the given HDC (or screen by default) and
56 // return its handle, the caller is responsible for freeing it (using
58 HBITMAP
CreateDDB(HDC hdc
= 0) const;
60 // get the handle from the DIB and reset it, i.e. this object won't destroy
61 // the DIB after this (but the caller should do it)
62 HBITMAP
Detach() { HBITMAP hbmp
= m_handle
; m_handle
= 0; return hbmp
; }
65 // create a palette for this DIB (always a trivial/default one for 24bpp)
66 wxPalette
*CreatePalette() const;
67 #endif // wxUSE_PALETTE
73 // return true if DIB was successfully created, false otherwise
74 bool IsOk() const { return m_handle
!= 0; }
76 // get the bitmap size
77 wxSize
GetSize() const { DoGetObject(); return wxSize(m_width
, m_height
); }
78 int GetWidth() const { DoGetObject(); return m_width
; }
79 int GetHeight() const { DoGetObject(); return m_height
; }
81 // get the number of bits per pixel, or depth
82 int GetDepth() const { DoGetObject(); return m_depth
; }
85 HBITMAP
GetHandle() const { return m_handle
; }
87 // get raw pointer to bitmap bits, you should know what you do if you
89 void *GetData() const { DoGetObject(); return m_data
; }
95 // these functions are only used by wxBitmapDataObject implementation in
96 // src/msw/ole/dataobj.cpp, don't use them directly if possible
98 // creates a DDB compatible with the given (or screen) DC from either
99 // a plain DIB or a DIB section (in which case the last parameter must be
101 static HBITMAP
ConvertToBitmap(const BITMAPINFO
*pbi
,
105 // creates a DIB from the given DDB or calculates the space needed by it:
106 // if pbi is NULL, only the space is calculated, otherwise pbi is supposed
107 // to point at BITMAPINFO of the correct size which is filled by this
109 static size_t ConvertFromBitmap(BITMAPINFO
*pbi
, HBITMAP hbmp
);
111 // wxImage conversion
112 // ------------------
115 // create a DIB from the given image, the DIB will be either 24 or 32 (if
116 // the image has alpha channel) bpp
117 wxDIB(const wxImage
& image
) { Init(); (void)Create(image
); }
119 // same as the above ctor but with the return code
120 bool Create(const wxImage
& image
);
122 // create wxImage having the same data as this DIB
123 wxImage
ConvertToImage() const;
124 #endif // wxUSE_IMAGE
130 // return the size of one line in a DIB with given width and depth: the
131 // point here is that as the scan lines need to be DWORD aligned so we may
132 // need to add some padding
133 static unsigned long GetLineSize(int width
, int depth
)
135 return ((width
*depth
+ 31) & ~31) >> 3;
139 // common part of all ctors
156 if ( !::DeleteObject(m_handle
) )
158 wxLogLastError(wxT("DeleteObject(hDIB)"));
165 // the DIB section handle, 0 if invalid
168 // NB: we could store only m_handle and not any of the other fields as
169 // we may always retrieve them from it using ::GetObject(), but we
170 // decide to still store them for efficiency concerns -- however if we
171 // don't have them from the very beginning (e.g. DIB constructed from a
172 // bitmap), we only retrieve them when necessary and so these fields
173 // should *never* be accessed directly, even from inside wxDIB code
175 // function which must be called before accessing any members and which
176 // gets their values from m_handle, if not done yet
177 void DoGetObject() const;
179 // pointer to DIB bits, may be NULL
182 // size and depth of the image
188 // DIBs can't be copied
190 wxDIB
& operator=(const wxDIB
&);
193 // ----------------------------------------------------------------------------
194 // inline functions implementation
195 // ----------------------------------------------------------------------------
197 inline wxDIB::~wxDIB()
202 // the rest is defined in dib.cpp
204 // Save (device dependent) wxBitmap as a DIB
205 bool wxSaveBitmap(wxChar
*filename
, wxBitmap
*bitmap
, wxPalette
*palette
= NULL
);
207 HANDLE
wxBitmapToDIB (HBITMAP hBitmap
, HPALETTE hPal
);
208 bool wxReadDIB(LPTSTR lpFileName
, HBITMAP
*bitmap
, HPALETTE
*palette
);
209 HANDLE
wxReadDIB2(LPTSTR lpFileName
);
210 LPSTR
wxFindDIBBits (LPSTR lpbi
);
211 HPALETTE
wxMakeDIBPalette(LPBITMAPINFOHEADER lpInfo
);
213 #endif // _WX_MSW_DIB_H_