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