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