]> git.saurik.com Git - wxWidgets.git/blob - src/dfb/bitmap.cpp
simplified #ifs around the code
[wxWidgets.git] / src / dfb / bitmap.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/dfb/bitmap.cpp
3 // Purpose: wxBitmap implementation
4 // Author: Vaclav Slavik
5 // Created: 2006-08-04
6 // RCS-ID: $Id$
7 // Copyright: (c) 2006 REA Elektronik GmbH
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
10
11 // For compilers that support precompilation, includes "wx.h".
12 #include "wx/wxprec.h"
13
14 #ifdef __BORLANDC__
15 #pragma hdrstop
16 #endif
17
18 #ifndef WX_PRECOMP
19 #include "wx/app.h"
20 #include "wx/log.h"
21 #endif
22
23 #include "wx/bitmap.h"
24 #include "wx/colour.h"
25 #include "wx/image.h"
26
27 #warning "move this to common"
28 #include "wx/xpmdecod.h"
29
30 #include "wx/dfb/private.h"
31
32 //-----------------------------------------------------------------------------
33 // helpers
34 //-----------------------------------------------------------------------------
35
36 // Creates a surface that will use wxImage's pixel data (RGB only)
37 static wxIDirectFBSurfacePtr CreateSurfaceForImage(const wxImage& image)
38 {
39 wxCHECK_MSG( image.Ok(), NULL, _T("invalid image") );
40 // FIXME_DFB: implement alpha handling by merging alpha buffer with RGB
41 // into a temporary RGBA surface
42 wxCHECK_MSG( !image.HasAlpha(), NULL, _T("alpha channel not supported") );
43
44 DFBSurfaceDescription desc;
45 desc.flags = (DFBSurfaceDescriptionFlags)
46 (DSDESC_CAPS | DSDESC_WIDTH | DSDESC_HEIGHT | DSDESC_PIXELFORMAT |
47 DSDESC_PREALLOCATED);
48 desc.caps = DSCAPS_NONE;
49 desc.width = image.GetWidth();
50 desc.height = image.GetHeight();
51 desc.pixelformat = DSPF_RGB24;
52 desc.preallocated[0].data = image.GetData();
53 desc.preallocated[0].pitch = 3 * desc.width;
54
55 return wxIDirectFB::Get()->CreateSurface(&desc);
56 }
57
58 //-----------------------------------------------------------------------------
59 // wxBitmapRefData
60 //-----------------------------------------------------------------------------
61
62 class wxBitmapRefData: public wxObjectRefData
63 {
64 public:
65 wxBitmapRefData()
66 {
67 m_mask = NULL;
68 #if wxUSE_PALETTE
69 m_palette = NULL;
70 #endif
71 }
72
73 wxBitmapRefData(const wxBitmapRefData& data)
74 {
75 if ( data.m_surface )
76 m_surface = data.m_surface->Clone();
77
78 m_mask = data.m_mask ? new wxMask(*data.m_mask) : NULL;
79 #if wxUSE_PALETTE
80 m_palette = data.m_palette ? new wxPalette(*data.m_palette) : NULL;
81 #endif
82 }
83
84 ~wxBitmapRefData()
85 {
86 delete m_mask;
87 #if wxUSE_PALETTE
88 delete m_palette;
89 #endif
90 }
91
92 wxIDirectFBSurfacePtr m_surface;
93 wxMask *m_mask;
94 #if wxUSE_PALETTE
95 wxPalette *m_palette;
96 #endif
97 };
98
99 #define M_BITMAP ((wxBitmapRefData *)m_refData)
100
101 //-----------------------------------------------------------------------------
102 // wxBitmap
103 //-----------------------------------------------------------------------------
104
105 IMPLEMENT_ABSTRACT_CLASS(wxBitmapHandler, wxObject)
106 IMPLEMENT_DYNAMIC_CLASS(wxBitmap, wxBitmapBase)
107
108 wxBitmap::wxBitmap(int width, int height, int depth)
109 {
110 Create(width, height, depth);
111 }
112
113 bool wxBitmap::Create(const wxIDirectFBSurfacePtr& surface)
114 {
115 UnRef();
116
117 wxCHECK_MSG( surface, false, _T("invalid surface") );
118
119 m_refData = new wxBitmapRefData();
120 M_BITMAP->m_surface = surface;
121 return true;
122 }
123
124 bool wxBitmap::Create(int width, int height, int depth)
125 {
126 UnRef();
127
128 wxCHECK_MSG( width > 0 && height > 0, false, wxT("invalid bitmap size") );
129 wxCHECK_MSG( depth == -1, false, wxT("only default depth supported now") );
130
131 DFBSurfaceDescription desc;
132 desc.flags = (DFBSurfaceDescriptionFlags)(
133 DSDESC_CAPS | DSDESC_WIDTH | DSDESC_HEIGHT);
134 desc.caps = DSCAPS_NONE;
135 desc.width = width;
136 desc.height = height;
137
138 return Create(wxIDirectFB::Get()->CreateSurface(&desc));
139 }
140
141 #warning "FIXME: move this to common code"
142 bool wxBitmap::CreateFromXpm(const char **bits)
143 {
144 wxCHECK_MSG( bits != NULL, false, wxT("invalid bitmap data") );
145
146 #if wxUSE_IMAGE && wxUSE_XPM
147 wxXPMDecoder decoder;
148 wxImage img = decoder.ReadData(bits);
149 wxCHECK_MSG( img.Ok(), false, wxT("invalid bitmap data") );
150
151 *this = wxBitmap(img);
152
153 return true;
154 #else
155 wxFAIL_MSG( _T("creating bitmaps from XPMs not supported") );
156 return false;
157 #endif // wxUSE_IMAGE && wxUSE_XPM
158 }
159
160 #if wxUSE_IMAGE
161 wxBitmap::wxBitmap(const wxImage& image, int depth)
162 {
163 wxCHECK_RET( image.Ok(), wxT("invalid image") );
164
165 // create surface in screen's format:
166 if ( !Create(image.GetWidth(), image.GetHeight(), depth) )
167 return;
168
169 // then copy the image to it:
170 wxIDirectFBSurfacePtr src(CreateSurfaceForImage(image));
171 wxIDirectFBSurfacePtr dst = M_BITMAP->m_surface;
172
173 if ( !dst->SetBlittingFlags(DSBLIT_NOFX) )
174 return;
175 if ( !dst->Blit(src->GetRaw(), NULL, 0, 0) )
176 return;
177
178 // FIXME: implement mask creation from image's mask (or alpha channel?)
179 wxASSERT_MSG( !image.HasMask(), _T("image masks are ignored for now") );
180 }
181
182 wxImage wxBitmap::ConvertToImage() const
183 {
184 wxCHECK_MSG( Ok(), wxNullImage, wxT("invalid bitmap") );
185
186 wxImage img(GetWidth(), GetHeight());
187 wxIDirectFBSurfacePtr dst(CreateSurfaceForImage(img));
188 wxIDirectFBSurfacePtr src = M_BITMAP->m_surface;
189
190 if ( !dst->SetBlittingFlags(DSBLIT_NOFX) )
191 return wxNullImage;
192 if ( !dst->Blit(src->GetRaw(), NULL, 0, 0) )
193 return wxNullImage;
194
195 // FIXME: implement mask setting in the image
196 wxASSERT_MSG( GetMask() == NULL, _T("bitmap masks are ignored for now") );
197
198 return img;
199 }
200 #endif // wxUSE_IMAGE
201
202 wxBitmap::wxBitmap(const wxString &filename, wxBitmapType type)
203 {
204 LoadFile(filename, type);
205 }
206
207 wxBitmap::wxBitmap(const char bits[], int width, int height, int depth)
208 {
209 wxCHECK_RET( depth == 1, wxT("can only create mono bitmap from XBM data") );
210
211 wxFAIL_MSG( _T("not implemented") );
212 }
213
214 bool wxBitmap::Ok() const
215 {
216 return (m_refData != NULL && M_BITMAP->m_surface);
217 }
218
219 bool wxBitmap::operator==(const wxBitmap& bmp) const
220 {
221 // FIXME: is this the right way to compare bitmaps?
222 return (m_refData == bmp.m_refData);
223 }
224
225 int wxBitmap::GetHeight() const
226 {
227 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
228
229 int h = -1;
230 M_BITMAP->m_surface->GetSize(NULL, &h);
231 return h;
232 }
233
234 int wxBitmap::GetWidth() const
235 {
236 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
237
238 int w = -1;
239 M_BITMAP->m_surface->GetSize(&w, NULL);
240 return w;
241 }
242
243 int wxBitmap::GetDepth() const
244 {
245 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
246
247 return M_BITMAP->m_surface->GetDepth();
248 }
249
250 wxMask *wxBitmap::GetMask() const
251 {
252 wxCHECK_MSG( Ok(), NULL, wxT("invalid bitmap") );
253
254 return M_BITMAP->m_mask;
255 }
256
257 void wxBitmap::SetMask(wxMask *mask)
258 {
259 wxCHECK_RET( Ok(), wxT("invalid bitmap") );
260
261 delete M_BITMAP->m_mask;
262 M_BITMAP->m_mask = mask;
263 }
264
265 bool wxBitmap::CopyFromIcon(const wxIcon& icon)
266 {
267 *this = *((wxBitmap*)(&icon));
268 return true;
269 }
270
271 wxBitmap wxBitmap::GetSubBitmap(const wxRect& rect) const
272 {
273 wxCHECK_MSG( Ok() &&
274 rect.x >= 0 && rect.y >= 0 &&
275 rect.x+rect.width <= GetWidth() &&
276 rect.y+rect.height <= GetHeight(),
277 wxNullBitmap,
278 wxT("invalid bitmap or bitmap region") );
279
280 // NB: DirectFB subsurfaces share the same pixels buffer, so we must
281 // clone the obtained subsurface
282 DFBRectangle r = { rect.x, rect.y, rect.width, rect.height };
283 return wxBitmap(M_BITMAP->m_surface->GetSubSurface(&r)->Clone());
284 }
285
286 #warning "to common code"
287 bool wxBitmap::LoadFile(const wxString &name, wxBitmapType type)
288 {
289 UnRef();
290
291 wxBitmapHandler *handler = FindHandler(type);
292
293 if ( handler == NULL )
294 {
295 wxImage image;
296 if ( !image.LoadFile(name, type) || !image.Ok() )
297 {
298 wxLogError("no bitmap handler for type %d defined.", type);
299 return false;
300 }
301 else
302 {
303 *this = wxBitmap(image);
304 return true;
305 }
306 }
307
308 m_refData = new wxBitmapRefData();
309
310 return handler->LoadFile(this, name, type, -1, -1);
311 }
312
313 #warning "to common code"
314 bool wxBitmap::SaveFile(const wxString& filename, wxBitmapType type, const wxPalette *palette) const
315 {
316 wxCHECK_MSG( Ok(), false, wxT("invalid bitmap") );
317
318 wxBitmapHandler *handler = FindHandler(type);
319
320 if ( handler == NULL )
321 {
322 wxImage image = ConvertToImage();
323 #if wxUSE_PALETTE
324 if ( palette )
325 image.SetPalette(*palette);
326 #endif // wxUSE_PALETTE
327
328 if ( image.Ok() )
329 return image.SaveFile(filename, type);
330 else
331 {
332 wxLogError("no bitmap handler for type %d defined.", type);
333 return false;
334 }
335 }
336
337 return handler->SaveFile(this, filename, type, palette);
338 }
339
340 #if wxUSE_PALETTE
341 wxPalette *wxBitmap::GetPalette() const
342 {
343 wxCHECK_MSG( Ok(), NULL, wxT("invalid bitmap") );
344
345 return M_BITMAP->m_palette;
346 }
347
348 void wxBitmap::SetPalette(const wxPalette& palette)
349 {
350 wxCHECK_RET( Ok(), wxT("invalid bitmap") );
351 wxCHECK_RET( GetDepth() > 1 && GetDepth() <= 8, wxT("cannot set palette for bitmap of this depth") );
352
353 delete M_BITMAP->m_palette;
354 M_BITMAP->m_palette = NULL;
355
356 if ( !palette.Ok() ) return;
357
358 M_BITMAP->m_palette = new wxPalette(palette);
359 }
360 #endif // wxUSE_PALETTE
361
362 void wxBitmap::SetHeight(int height)
363 {
364 AllocExclusive();
365
366 wxFAIL_MSG( _T("SetHeight not implemented") );
367 }
368
369 void wxBitmap::SetWidth(int width)
370 {
371 AllocExclusive();
372
373 wxFAIL_MSG( _T("SetWidth not implemented") );
374 }
375
376 void wxBitmap::SetDepth(int depth)
377 {
378 AllocExclusive();
379
380 wxFAIL_MSG( _T("SetDepth not implemented") );
381 }
382
383 wxIDirectFBSurfacePtr wxBitmap::GetDirectFBSurface() const
384 {
385 wxCHECK_MSG( Ok(), NULL, wxT("invalid bitmap") );
386
387 return M_BITMAP->m_surface;
388 }
389
390 wxObjectRefData *wxBitmap::CreateRefData() const
391 {
392 return new wxBitmapRefData;
393 }
394
395 wxObjectRefData *wxBitmap::CloneRefData(const wxObjectRefData *data) const
396 {
397 return new wxBitmapRefData(*(wxBitmapRefData *)data);
398 }
399
400
401 /*static*/
402 void wxBitmap::InitStandardHandlers()
403 {
404 // not wxBitmap handlers, we rely on wxImage
405 }