1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/dfb/bitmap.cpp
3 // Purpose: wxBitmap implementation
4 // Author: Vaclav Slavik
7 // Copyright: (c) 2006 REA Elektronik GmbH
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
11 // For compilers that support precompilation, includes "wx.h".
12 #include "wx/wxprec.h"
23 #include "wx/bitmap.h"
24 #include "wx/colour.h"
27 #warning "move this to common"
28 #include "wx/xpmdecod.h"
30 #include "wx/dfb/private.h"
32 //-----------------------------------------------------------------------------
34 //-----------------------------------------------------------------------------
36 // Convert wxColour into it's quantized value in lower-precision
37 // pixel format (needed for masking by colour).
38 static wxColour
wxQuantizeColour(const wxColour
& clr
, const wxBitmap
& bmp
)
41 pixel_format_t
*pf
= bmp
.GetMGLbitmap_t()->pf
;
43 if ( pf
->redAdjust
== 0 && pf
->greenAdjust
== 0 && pf
->blueAdjust
== 0 )
46 return wxColour((unsigned char)((clr
.Red() >> pf
->redAdjust
) << pf
->redAdjust
),
47 (unsigned char)((clr
.Green() >> pf
->greenAdjust
) << pf
->greenAdjust
),
48 (unsigned char)((clr
.Blue() >> pf
->blueAdjust
) << pf
->blueAdjust
));
53 //-----------------------------------------------------------------------------
55 //-----------------------------------------------------------------------------
57 IMPLEMENT_DYNAMIC_CLASS(wxMask
, wxObject
)
59 wxMask::wxMask() : m_bitmap(NULL
)
63 wxMask::wxMask(const wxBitmap
& bitmap
)
69 wxMask::wxMask(const wxBitmap
& bitmap
, const wxColour
& colour
)
72 Create(bitmap
, colour
);
76 wxMask::wxMask(const wxBitmap
& bitmap
, int paletteIndex
)
79 Create(bitmap
, paletteIndex
);
81 #endif // wxUSE_PALETTE
83 wxMask::wxMask(const wxMask
& mask
)
85 m_bitmap
= mask
.m_bitmap
? new wxBitmap(*mask
.m_bitmap
) : NULL
;
93 #warning "move this to common code"
94 bool wxMask::Create(const wxBitmap
& bitmap
, const wxColour
& colour
)
99 wxColour
clr(wxQuantizeColour(colour
, bitmap
));
101 wxImage
imgSrc(bitmap
.ConvertToImage());
102 imgSrc
.SetMask(false);
103 wxImage
image(imgSrc
.ConvertToMono(clr
.Red(), clr
.Green(), clr
.Blue()));
107 m_bitmap
= new wxBitmap(image
, 1);
109 return m_bitmap
->Ok();
113 bool wxMask::Create(const wxBitmap
& bitmap
, int paletteIndex
)
116 wxPalette
*pal
= bitmap
.GetPalette();
118 wxCHECK_MSG( pal
, false, wxT("Cannot create mask from bitmap without palette") );
120 pal
->GetRGB(paletteIndex
, &r
, &g
, &b
);
122 return Create(bitmap
, wxColour(r
, g
, b
));
124 #endif // wxUSE_PALETTE
126 bool wxMask::Create(const wxBitmap
& bitmap
)
131 wxCHECK_MSG( bitmap
.Ok(), false, wxT("Invalid bitmap") );
132 wxCHECK_MSG( bitmap
.GetDepth() == 1, false, wxT("Cannot create mask from colour bitmap") );
134 m_bitmap
= new wxBitmap(bitmap
);
138 const wxBitmap
& wxMask::GetBitmap() const
140 return m_bitmap
? *m_bitmap
: wxNullBitmap
;
144 //-----------------------------------------------------------------------------
146 //-----------------------------------------------------------------------------
148 class wxBitmapRefData
: public wxObjectRefData
159 wxBitmapRefData(const wxBitmapRefData
& data
)
161 m_surface
= wxDfbCloneSurface(data
.m_surface
,
162 wxDfbCloneSurface_NoPixels
);
163 m_mask
= data
.m_mask
? new wxMask(*data
.m_mask
) : NULL
;
165 m_palette
= data
.m_palette
? new wxPalette(*data
.m_palette
) : NULL
;
177 IDirectFBSurfacePtr m_surface
;
180 wxPalette
*m_palette
;
184 #define M_BITMAP ((wxBitmapRefData *)m_refData)
186 //-----------------------------------------------------------------------------
188 //-----------------------------------------------------------------------------
190 IMPLEMENT_ABSTRACT_CLASS(wxBitmapHandler
, wxObject
)
191 IMPLEMENT_DYNAMIC_CLASS(wxBitmap
, wxBitmapBase
)
193 wxBitmap::wxBitmap(int width
, int height
, int depth
)
195 Create(width
, height
, depth
);
198 bool wxBitmap::Create(int width
, int height
, int depth
)
202 wxCHECK_MSG( width
> 0 && height
> 0, false, wxT("invalid bitmap size") );
204 DFBSurfaceDescription desc
;
205 desc
.flags
= (DFBSurfaceDescriptionFlags
)(
206 DSDESC_CAPS
| DSDESC_WIDTH
| DSDESC_HEIGHT
);
207 desc
.caps
= DSCAPS_NONE
;
209 desc
.height
= height
;
211 IDirectFBSurfacePtr surface
;
212 IDirectFBPtr
dfb(wxTheApp
->GetDirectFBInterface());
213 if ( !DFB_CALL( dfb
->CreateSurface(dfb
, &desc
, &surface
) ) )
216 m_refData
= new wxBitmapRefData();
217 M_BITMAP
->m_surface
= surface
;
222 #warning "FIXME: move this to common code"
223 bool wxBitmap::CreateFromXpm(const char **bits
)
225 wxCHECK_MSG( bits
!= NULL
, false, wxT("invalid bitmap data") );
227 #if wxUSE_IMAGE && wxUSE_XPM
228 wxXPMDecoder decoder
;
229 wxImage img
= decoder
.ReadData(bits
);
230 wxCHECK_MSG( img
.Ok(), false, wxT("invalid bitmap data") );
232 *this = wxBitmap(img
);
236 wxFAIL_MSG( _T("creating bitmaps from XPMs not supported") );
238 #endif // wxUSE_IMAGE && wxUSE_XPM
242 wxBitmap::wxBitmap(const wxImage
& image
, int depth
)
244 wxCHECK_RET( image
.Ok(), wxT("invalid image") );
247 wxImage
wxBitmap::ConvertToImage() const
249 wxCHECK_MSG( Ok(), wxNullImage
, wxT("invalid bitmap") );
251 return wxNullImage
; // FIXME
253 #endif // wxUSE_IMAGE
255 wxBitmap::wxBitmap(const wxString
&filename
, wxBitmapType type
)
257 LoadFile(filename
, type
);
260 wxBitmap::wxBitmap(const char bits
[], int width
, int height
, int depth
)
262 wxCHECK_RET( depth
== 1, wxT("can only create mono bitmap from XBM data") );
265 bool wxBitmap::Ok() const
267 return (m_refData
!= NULL
&& M_BITMAP
->m_surface
);
270 bool wxBitmap::operator==(const wxBitmap
& bmp
) const
272 // FIXME: is this the right way to compare bitmaps?
273 return (m_refData
== bmp
.m_refData
);
276 int wxBitmap::GetHeight() const
278 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
281 DFB_CALL( M_BITMAP
->m_surface
->GetSize(M_BITMAP
->m_surface
, NULL
, &h
) );
285 int wxBitmap::GetWidth() const
287 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
290 DFB_CALL( M_BITMAP
->m_surface
->GetSize(M_BITMAP
->m_surface
, &w
, NULL
) );
294 int wxBitmap::GetDepth() const
296 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
298 return wxDfbGetSurfaceDepth(M_BITMAP
->m_surface
);
301 wxMask
*wxBitmap::GetMask() const
303 wxCHECK_MSG( Ok(), NULL
, wxT("invalid bitmap") );
305 return M_BITMAP
->m_mask
;
308 void wxBitmap::SetMask(wxMask
*mask
)
310 wxCHECK_RET( Ok(), wxT("invalid bitmap") );
312 delete M_BITMAP
->m_mask
;
313 M_BITMAP
->m_mask
= mask
;
316 bool wxBitmap::CopyFromIcon(const wxIcon
& icon
)
318 *this = *((wxBitmap
*)(&icon
));
322 wxBitmap
wxBitmap::GetSubBitmap(const wxRect
& rect
) const
325 rect
.x
>= 0 && rect
.y
>= 0 &&
326 rect
.x
+rect
.width
<= GetWidth() &&
327 rect
.y
+rect
.height
<= GetHeight(),
329 wxT("invalid bitmap or bitmap region") );
333 #warning "to common code"
334 bool wxBitmap::LoadFile(const wxString
&name
, wxBitmapType type
)
338 wxBitmapHandler
*handler
= FindHandler(type
);
340 if ( handler
== NULL
)
343 if ( !image
.LoadFile(name
, type
) || !image
.Ok() )
345 wxLogError("no bitmap handler for type %d defined.", type
);
350 *this = wxBitmap(image
);
355 m_refData
= new wxBitmapRefData();
357 return handler
->LoadFile(this, name
, type
, -1, -1);
360 #warning "to common code"
361 bool wxBitmap::SaveFile(const wxString
& filename
, wxBitmapType type
, const wxPalette
*palette
) const
363 wxCHECK_MSG( Ok(), false, wxT("invalid bitmap") );
365 wxBitmapHandler
*handler
= FindHandler(type
);
367 if ( handler
== NULL
)
369 wxImage image
= ConvertToImage();
372 image
.SetPalette(*palette
);
373 #endif // wxUSE_PALETTE
376 return image
.SaveFile(filename
, type
);
379 wxLogError("no bitmap handler for type %d defined.", type
);
384 return handler
->SaveFile(this, filename
, type
, palette
);
388 wxPalette
*wxBitmap::GetPalette() const
390 wxCHECK_MSG( Ok(), NULL
, wxT("invalid bitmap") );
392 return M_BITMAP
->m_palette
;
395 void wxBitmap::SetPalette(const wxPalette
& palette
)
397 wxCHECK_RET( Ok(), wxT("invalid bitmap") );
398 wxCHECK_RET( GetDepth() > 1 && GetDepth() <= 8, wxT("cannot set palette for bitmap of this depth") );
400 delete M_BITMAP
->m_palette
;
401 M_BITMAP
->m_palette
= NULL
;
403 if ( !palette
.Ok() ) return;
405 M_BITMAP
->m_palette
= new wxPalette(palette
);
407 #endif // wxUSE_PALETTE
409 void wxBitmap::SetHeight(int height
)
415 void wxBitmap::SetWidth(int width
)
421 void wxBitmap::SetDepth(int depth
)
427 IDirectFBSurfacePtr
wxBitmap::GetDirectFBSurface() const
429 wxCHECK_MSG( Ok(), NULL
, wxT("invalid bitmap") );
431 return M_BITMAP
->m_surface
;
434 wxObjectRefData
*wxBitmap::CreateRefData() const
436 return new wxBitmapRefData
;
439 wxObjectRefData
*wxBitmap::CloneRefData(const wxObjectRefData
*data
) const
441 return new wxBitmapRefData(*(wxBitmapRefData
*)data
);
446 void wxBitmap::InitStandardHandlers()
448 // not wxBitmap handlers, we rely on wxImage