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