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