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