]>
Commit | Line | Data |
---|---|---|
2bda0e17 | 1 | ///////////////////////////////////////////////////////////////////////////// |
6d167489 | 2 | // Name: wx/msw/dib.h |
53eff2a2 VZ |
3 | // Purpose: wxDIB class representing Win32 device independent bitmaps |
4 | // Author: Vadim Zeitlin | |
2bda0e17 | 5 | // Modified by: |
53eff2a2 | 6 | // Created: 03.03.03 (replaces the old file with the same name) |
77ffb593 | 7 | // Copyright: (c) 1997-2003 wxWidgets team |
65571936 | 8 | // Licence: wxWindows licence |
2bda0e17 KB |
9 | ///////////////////////////////////////////////////////////////////////////// |
10 | ||
53eff2a2 VZ |
11 | #ifndef _WX_MSW_DIB_H_ |
12 | #define _WX_MSW_DIB_H_ | |
8fb3a512 | 13 | |
b5dbe15d | 14 | class WXDLLIMPEXP_FWD_CORE wxPalette; |
6d167489 | 15 | |
53eff2a2 VZ |
16 | #include "wx/msw/private.h" |
17 | ||
086b3a5b JS |
18 | #if wxUSE_WXDIB |
19 | ||
69bacfaa VZ |
20 | #ifdef __WXMSW__ |
21 | #include "wx/bitmap.h" | |
22 | #endif // __WXMSW__ | |
23 | ||
53eff2a2 VZ |
24 | // ---------------------------------------------------------------------------- |
25 | // wxDIB: represents a DIB section | |
26 | // ---------------------------------------------------------------------------- | |
27 | ||
53a2db12 | 28 | class WXDLLIMPEXP_CORE wxDIB |
53eff2a2 VZ |
29 | { |
30 | public: | |
31 | // ctors and such | |
32 | // -------------- | |
33 | ||
34 | // create an uninitialized DIB with the given width, height and depth (only | |
35 | // 24 and 32 bpp DIBs are currently supported) | |
36 | // | |
37 | // after using this ctor, GetData() and GetHandle() may be used if IsOk() | |
38 | // returns true | |
39 | wxDIB(int width, int height, int depth) | |
40 | { Init(); (void)Create(width, height, depth); } | |
41 | ||
954fd051 | 42 | #ifdef __WXMSW__ |
2b254edf VZ |
43 | // create a DIB from the DDB |
44 | wxDIB(const wxBitmap& bmp) | |
45 | { Init(); (void)Create(bmp); } | |
954fd051 | 46 | #endif // __WXMSW__ |
2b254edf | 47 | |
dd779a9e VZ |
48 | // create a DIB from the Windows DDB |
49 | wxDIB(HBITMAP hbmp) | |
50 | { Init(); (void)Create(hbmp); } | |
51 | ||
c11bf842 VZ |
52 | // load a DIB from file (any depth is supoprted here unlike above) |
53 | // | |
54 | // as above, use IsOk() to see if the bitmap was loaded successfully | |
55 | wxDIB(const wxString& filename) | |
56 | { Init(); (void)Load(filename); } | |
57 | ||
58 | // same as the corresponding ctors but with return value | |
53eff2a2 | 59 | bool Create(int width, int height, int depth); |
954fd051 | 60 | #ifdef __WXMSW__ |
69060f4f | 61 | bool Create(const wxBitmap& bmp) { return Create(GetHbitmapOf(bmp)); } |
954fd051 | 62 | #endif |
dd779a9e | 63 | bool Create(HBITMAP hbmp); |
c11bf842 | 64 | bool Load(const wxString& filename); |
53eff2a2 VZ |
65 | |
66 | // dtor is not virtual, this class is not meant to be used polymorphically | |
c11bf842 | 67 | ~wxDIB(); |
53eff2a2 VZ |
68 | |
69 | ||
70 | // operations | |
71 | // ---------- | |
72 | ||
be69f971 | 73 | #ifndef __WXWINCE__ |
dd779a9e | 74 | // create a bitmap compatible with the given HDC (or screen by default) and |
c11bf842 VZ |
75 | // return its handle, the caller is responsible for freeing it (using |
76 | // DeleteObject()) | |
22be0335 | 77 | HBITMAP CreateDDB(HDC hdc = 0) const; |
be69f971 | 78 | #endif // !__WXWINCE__ |
c11bf842 | 79 | |
53eff2a2 VZ |
80 | // get the handle from the DIB and reset it, i.e. this object won't destroy |
81 | // the DIB after this (but the caller should do it) | |
82 | HBITMAP Detach() { HBITMAP hbmp = m_handle; m_handle = 0; return hbmp; } | |
83 | ||
c11bf842 VZ |
84 | #if wxUSE_PALETTE |
85 | // create a palette for this DIB (always a trivial/default one for 24bpp) | |
86 | wxPalette *CreatePalette() const; | |
87 | #endif // wxUSE_PALETTE | |
88 | ||
2b254edf VZ |
89 | // save the DIB as a .BMP file to the file with the given name |
90 | bool Save(const wxString& filename); | |
91 | ||
53eff2a2 VZ |
92 | |
93 | // accessors | |
94 | // --------- | |
95 | ||
96 | // return true if DIB was successfully created, false otherwise | |
97 | bool IsOk() const { return m_handle != 0; } | |
98 | ||
99 | // get the bitmap size | |
100 | wxSize GetSize() const { DoGetObject(); return wxSize(m_width, m_height); } | |
101 | int GetWidth() const { DoGetObject(); return m_width; } | |
102 | int GetHeight() const { DoGetObject(); return m_height; } | |
103 | ||
104 | // get the number of bits per pixel, or depth | |
105 | int GetDepth() const { DoGetObject(); return m_depth; } | |
106 | ||
107 | // get the DIB handle | |
108 | HBITMAP GetHandle() const { return m_handle; } | |
109 | ||
110 | // get raw pointer to bitmap bits, you should know what you do if you | |
111 | // decide to use it | |
dd779a9e VZ |
112 | unsigned char *GetData() const |
113 | { DoGetObject(); return (unsigned char *)m_data; } | |
53eff2a2 VZ |
114 | |
115 | ||
22be0335 VZ |
116 | // HBITMAP conversion |
117 | // ------------------ | |
118 | ||
77ffb593 | 119 | // these functions are only used by wxWidgets internally right now, please |
2b254edf | 120 | // don't use them directly if possible as they're subject to change |
22be0335 | 121 | |
be69f971 | 122 | #ifndef __WXWINCE__ |
22be0335 VZ |
123 | // creates a DDB compatible with the given (or screen) DC from either |
124 | // a plain DIB or a DIB section (in which case the last parameter must be | |
125 | // non NULL) | |
126 | static HBITMAP ConvertToBitmap(const BITMAPINFO *pbi, | |
127 | HDC hdc = 0, | |
128 | void *bits = NULL); | |
129 | ||
2b254edf VZ |
130 | // create a plain DIB (not a DIB section) from a DDB, the caller is |
131 | // responsable for freeing it using ::GlobalFree() | |
132 | static HGLOBAL ConvertFromBitmap(HBITMAP hbmp); | |
133 | ||
22be0335 VZ |
134 | // creates a DIB from the given DDB or calculates the space needed by it: |
135 | // if pbi is NULL, only the space is calculated, otherwise pbi is supposed | |
136 | // to point at BITMAPINFO of the correct size which is filled by this | |
2b254edf VZ |
137 | // function (this overload is needed for wxBitmapDataObject code in |
138 | // src/msw/ole/dataobj.cpp) | |
22be0335 | 139 | static size_t ConvertFromBitmap(BITMAPINFO *pbi, HBITMAP hbmp); |
be69f971 | 140 | #endif // __WXWINCE__ |
22be0335 | 141 | |
2b254edf | 142 | |
53eff2a2 VZ |
143 | // wxImage conversion |
144 | // ------------------ | |
145 | ||
146 | #if wxUSE_IMAGE | |
fd90675b VZ |
147 | // Possible formats for DIBs created by the functions below. |
148 | enum PixelFormat | |
149 | { | |
150 | PixelFormat_PreMultiplied = 0, | |
151 | PixelFormat_NotPreMultiplied = 1 | |
152 | }; | |
153 | ||
154 | // Create a DIB from the given image, the DIB will be either 24 or 32 (if | |
155 | // the image has alpha channel) bpp. | |
156 | // | |
157 | // By default the DIB stores pixel data in pre-multiplied format so that it | |
158 | // can be used with ::AlphaBlend() but it is also possible to disable | |
159 | // pre-multiplication for the DIB to be usable with ImageList_Draw() which | |
160 | // does pre-multiplication internally. | |
161 | wxDIB(const wxImage& image, PixelFormat pf = PixelFormat_PreMultiplied) | |
162 | { | |
163 | Init(); | |
164 | (void)Create(image, pf); | |
165 | } | |
53eff2a2 VZ |
166 | |
167 | // same as the above ctor but with the return code | |
fd90675b | 168 | bool Create(const wxImage& image, PixelFormat pf = PixelFormat_PreMultiplied); |
53eff2a2 VZ |
169 | |
170 | // create wxImage having the same data as this DIB | |
171 | wxImage ConvertToImage() const; | |
172 | #endif // wxUSE_IMAGE | |
173 | ||
174 | ||
175 | // helper functions | |
176 | // ---------------- | |
177 | ||
178 | // return the size of one line in a DIB with given width and depth: the | |
179 | // point here is that as the scan lines need to be DWORD aligned so we may | |
180 | // need to add some padding | |
181 | static unsigned long GetLineSize(int width, int depth) | |
182 | { | |
183 | return ((width*depth + 31) & ~31) >> 3; | |
184 | } | |
185 | ||
186 | private: | |
187 | // common part of all ctors | |
82ac3b0a | 188 | void Init(); |
53eff2a2 | 189 | |
c11bf842 | 190 | // free resources |
82ac3b0a | 191 | void Free(); |
c11bf842 | 192 | |
be69f971 VZ |
193 | // initialize the contents from the provided DDB (Create() must have been |
194 | // already called) | |
195 | bool CopyFromDDB(HBITMAP hbmp); | |
196 | ||
197 | ||
53eff2a2 VZ |
198 | // the DIB section handle, 0 if invalid |
199 | HBITMAP m_handle; | |
200 | ||
201 | // NB: we could store only m_handle and not any of the other fields as | |
202 | // we may always retrieve them from it using ::GetObject(), but we | |
203 | // decide to still store them for efficiency concerns -- however if we | |
204 | // don't have them from the very beginning (e.g. DIB constructed from a | |
205 | // bitmap), we only retrieve them when necessary and so these fields | |
206 | // should *never* be accessed directly, even from inside wxDIB code | |
207 | ||
208 | // function which must be called before accessing any members and which | |
209 | // gets their values from m_handle, if not done yet | |
210 | void DoGetObject() const; | |
211 | ||
212 | // pointer to DIB bits, may be NULL | |
213 | void *m_data; | |
214 | ||
215 | // size and depth of the image | |
216 | int m_width, | |
217 | m_height, | |
218 | m_depth; | |
219 | ||
82ac3b0a VZ |
220 | // in some cases we could be using a handle which we didn't create and in |
221 | // this case we shouldn't free it neither -- this flag tell us if this is | |
222 | // the case | |
223 | bool m_ownsHandle; | |
224 | ||
53eff2a2 VZ |
225 | |
226 | // DIBs can't be copied | |
227 | wxDIB(const wxDIB&); | |
228 | wxDIB& operator=(const wxDIB&); | |
229 | }; | |
230 | ||
c11bf842 VZ |
231 | // ---------------------------------------------------------------------------- |
232 | // inline functions implementation | |
233 | // ---------------------------------------------------------------------------- | |
234 | ||
82ac3b0a VZ |
235 | inline |
236 | void wxDIB::Init() | |
237 | { | |
238 | m_handle = 0; | |
239 | m_ownsHandle = true; | |
240 | ||
241 | m_data = NULL; | |
242 | ||
243 | m_width = | |
244 | m_height = | |
245 | m_depth = 0; | |
246 | } | |
247 | ||
248 | inline | |
249 | void wxDIB::Free() | |
250 | { | |
251 | if ( m_handle && m_ownsHandle ) | |
252 | { | |
253 | if ( !::DeleteObject(m_handle) ) | |
254 | { | |
255 | wxLogLastError(wxT("DeleteObject(hDIB)")); | |
256 | } | |
257 | ||
258 | Init(); | |
259 | } | |
260 | } | |
261 | ||
c11bf842 VZ |
262 | inline wxDIB::~wxDIB() |
263 | { | |
264 | Free(); | |
265 | } | |
6d56eb5c | 266 | |
086b3a5b JS |
267 | #endif |
268 | // wxUSE_WXDIB | |
269 | ||
53eff2a2 | 270 | #endif // _WX_MSW_DIB_H_ |
8fb3a512 | 271 |