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