]> git.saurik.com Git - wxWidgets.git/blob - include/wx/msw/dib.h
a72122254a3846ec0fe14f19c118ed10c5caa1df
[wxWidgets.git] / include / wx / msw / dib.h
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 // ----------------------------------------------------------------------------
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
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
45 bool Create(int width, int height, int depth);
46 bool Load(const wxString& filename);
47
48 // dtor is not virtual, this class is not meant to be used polymorphically
49 ~wxDIB();
50
51
52 // operations
53 // ----------
54
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())
58 HBITMAP CreateDDB(HDC hdc = NULL) const;
59
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
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
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
92 // wxImage conversion
93 // ------------------
94
95 #if wxUSE_IMAGE
96 // create a DIB from the given image, the DIB will be either 24 or 32 (if
97 // the image has alpha channel) bpp
98 wxDIB(const wxImage& image) { Init(); (void)Create(image); }
99
100 // same as the above ctor but with the return code
101 bool Create(const wxImage& image);
102
103 // create wxImage having the same data as this DIB
104 wxImage ConvertToImage() const;
105 #endif // wxUSE_IMAGE
106
107
108 // helper functions
109 // ----------------
110
111 // return the size of one line in a DIB with given width and depth: the
112 // point here is that as the scan lines need to be DWORD aligned so we may
113 // need to add some padding
114 static unsigned long GetLineSize(int width, int depth)
115 {
116 return ((width*depth + 31) & ~31) >> 3;
117 }
118
119 private:
120 // common part of all ctors
121 void Init()
122 {
123 m_handle = 0;
124
125 m_data = NULL;
126
127 m_width =
128 m_height =
129 m_depth = 0;
130 }
131
132 // free resources
133 void Free()
134 {
135 if ( m_handle )
136 {
137 if ( !::DeleteObject(m_handle) )
138 {
139 wxLogLastError(wxT("DeleteObject(hDIB)"));
140 }
141
142 Init();
143 }
144 }
145
146 // the DIB section handle, 0 if invalid
147 HBITMAP m_handle;
148
149 // NB: we could store only m_handle and not any of the other fields as
150 // we may always retrieve them from it using ::GetObject(), but we
151 // decide to still store them for efficiency concerns -- however if we
152 // don't have them from the very beginning (e.g. DIB constructed from a
153 // bitmap), we only retrieve them when necessary and so these fields
154 // should *never* be accessed directly, even from inside wxDIB code
155
156 // function which must be called before accessing any members and which
157 // gets their values from m_handle, if not done yet
158 void DoGetObject() const;
159
160 // pointer to DIB bits, may be NULL
161 void *m_data;
162
163 // size and depth of the image
164 int m_width,
165 m_height,
166 m_depth;
167
168
169 // DIBs can't be copied
170 wxDIB(const wxDIB&);
171 wxDIB& operator=(const wxDIB&);
172 };
173
174 // ----------------------------------------------------------------------------
175 // inline functions implementation
176 // ----------------------------------------------------------------------------
177
178 inline wxDIB::~wxDIB()
179 {
180 Free();
181 }
182
183 // ----------------------------------------------------------------------------
184 // Functions for working with DIBs
185 // ----------------------------------------------------------------------------
186
187 // WARNING: these functions are private to wxWindows and shouldn't be used
188 // by the user code, they risk to disappear in the next versions!
189
190 // VZ: we have 3 different sets of functions: from bitmap.cpp (wxCreateDIB and
191 // wxFreeDIB), from dib.cpp and from dataobj.cpp - surely there is some
192 // redundancy between them? (FIXME)
193
194 // defined in ole/dataobj.cpp
195 extern WXDLLEXPORT size_t wxConvertBitmapToDIB(LPBITMAPINFO pbi, const wxBitmap& bitmap);
196 extern WXDLLEXPORT wxBitmap wxConvertDIBToBitmap(const LPBITMAPINFO pbi);
197
198 // the rest is defined in dib.cpp
199
200 // Save (device dependent) wxBitmap as a DIB
201 bool wxSaveBitmap(wxChar *filename, wxBitmap *bitmap, wxPalette *palette = NULL);
202
203 // Load device independent bitmap into device dependent bitmap
204 wxBitmap *wxLoadBitmap(wxChar *filename, wxPalette **palette = NULL);
205
206 // Load into existing bitmap;
207 bool wxLoadIntoBitmap(wxChar *filename, wxBitmap *bitmap, wxPalette **pal = NULL);
208
209 HANDLE wxBitmapToDIB (HBITMAP hBitmap, HPALETTE hPal);
210 bool wxReadDIB(LPTSTR lpFileName, HBITMAP *bitmap, HPALETTE *palette);
211 HANDLE wxReadDIB2(LPTSTR lpFileName);
212 LPSTR wxFindDIBBits (LPSTR lpbi);
213 HPALETTE wxMakeDIBPalette(LPBITMAPINFOHEADER lpInfo);
214
215 #endif // _WX_MSW_DIB_H_
216