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 #include "wx/dfb/private.h"
29 //-----------------------------------------------------------------------------
31 //-----------------------------------------------------------------------------
33 // Creates a surface that will use wxImage's pixel data (RGB only)
34 static wxIDirectFBSurfacePtr
CreateSurfaceForImage(const wxImage
& image
)
36 wxCHECK_MSG( image
.Ok(), NULL
, _T("invalid image") );
37 // FIXME_DFB: implement alpha handling by merging alpha buffer with RGB
38 // into a temporary RGBA surface
39 wxCHECK_MSG( !image
.HasAlpha(), NULL
, _T("alpha channel not supported") );
41 DFBSurfaceDescription desc
;
42 desc
.flags
= (DFBSurfaceDescriptionFlags
)
43 (DSDESC_CAPS
| DSDESC_WIDTH
| DSDESC_HEIGHT
| DSDESC_PIXELFORMAT
|
45 desc
.caps
= DSCAPS_NONE
;
46 desc
.width
= image
.GetWidth();
47 desc
.height
= image
.GetHeight();
48 desc
.pixelformat
= DSPF_RGB24
;
49 desc
.preallocated
[0].data
= image
.GetData();
50 desc
.preallocated
[0].pitch
= 3 * desc
.width
;
52 return wxIDirectFB::Get()->CreateSurface(&desc
);
55 //-----------------------------------------------------------------------------
57 //-----------------------------------------------------------------------------
59 class wxBitmapRefData
: public wxObjectRefData
70 wxBitmapRefData(const wxBitmapRefData
& data
)
72 m_surface
= data
.m_surface
? data
.m_surface
->Clone() : NULL
;
74 m_mask
= data
.m_mask
? new wxMask(*data
.m_mask
) : NULL
;
76 m_palette
= data
.m_palette
? new wxPalette(*data
.m_palette
) : NULL
;
80 virtual ~wxBitmapRefData()
88 wxIDirectFBSurfacePtr m_surface
;
95 #define M_BITMAP ((wxBitmapRefData *)m_refData)
97 //-----------------------------------------------------------------------------
99 //-----------------------------------------------------------------------------
101 IMPLEMENT_ABSTRACT_CLASS(wxBitmapHandler
, wxBitmapHandlerBase
)
102 IMPLEMENT_DYNAMIC_CLASS(wxBitmap
, wxBitmapBase
)
104 wxBitmap::wxBitmap(int width
, int height
, int depth
)
106 Create(width
, height
, depth
);
109 bool wxBitmap::Create(const wxIDirectFBSurfacePtr
& surface
)
113 wxCHECK_MSG( surface
, false, _T("invalid surface") );
115 m_refData
= new wxBitmapRefData();
116 M_BITMAP
->m_surface
= surface
;
120 bool wxBitmap::Create(int width
, int height
, int depth
)
124 wxCHECK_MSG( width
> 0 && height
> 0, false, wxT("invalid bitmap size") );
125 wxCHECK_MSG( depth
== -1, false, wxT("only default depth supported now") );
127 DFBSurfaceDescription desc
;
128 desc
.flags
= (DFBSurfaceDescriptionFlags
)(
129 DSDESC_CAPS
| DSDESC_WIDTH
| DSDESC_HEIGHT
);
130 desc
.caps
= DSCAPS_NONE
;
132 desc
.height
= height
;
134 return Create(wxIDirectFB::Get()->CreateSurface(&desc
));
138 wxBitmap::wxBitmap(const wxImage
& image
, int depth
)
140 wxCHECK_RET( image
.Ok(), wxT("invalid image") );
142 // create surface in screen's format:
143 if ( !Create(image
.GetWidth(), image
.GetHeight(), depth
) )
146 // then copy the image to it:
147 wxIDirectFBSurfacePtr
src(CreateSurfaceForImage(image
));
148 wxIDirectFBSurfacePtr dst
= M_BITMAP
->m_surface
;
150 if ( !dst
->SetBlittingFlags(DSBLIT_NOFX
) )
152 if ( !dst
->Blit(src
->GetRaw(), NULL
, 0, 0) )
155 // FIXME: implement mask creation from image's mask (or alpha channel?)
156 wxASSERT_MSG( !image
.HasMask(), _T("image masks are ignored for now") );
159 wxImage
wxBitmap::ConvertToImage() const
161 wxCHECK_MSG( Ok(), wxNullImage
, wxT("invalid bitmap") );
163 wxImage
img(GetWidth(), GetHeight());
164 wxIDirectFBSurfacePtr
dst(CreateSurfaceForImage(img
));
165 wxIDirectFBSurfacePtr src
= M_BITMAP
->m_surface
;
167 if ( !dst
->SetBlittingFlags(DSBLIT_NOFX
) )
169 if ( !dst
->Blit(src
->GetRaw(), NULL
, 0, 0) )
172 // FIXME: implement mask setting in the image
173 wxASSERT_MSG( GetMask() == NULL
, _T("bitmap masks are ignored for now") );
177 #endif // wxUSE_IMAGE
179 wxBitmap::wxBitmap(const wxString
&filename
, wxBitmapType type
)
181 LoadFile(filename
, type
);
184 wxBitmap::wxBitmap(const char bits
[], int width
, int height
, int depth
)
186 wxCHECK_RET( depth
== 1, wxT("can only create mono bitmap from XBM data") );
188 wxFAIL_MSG( _T("not implemented") );
191 bool wxBitmap::IsOk() const
193 return (m_refData
!= NULL
&& M_BITMAP
->m_surface
);
196 int wxBitmap::GetHeight() const
198 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
201 M_BITMAP
->m_surface
->GetSize(NULL
, &h
);
205 int wxBitmap::GetWidth() const
207 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
210 M_BITMAP
->m_surface
->GetSize(&w
, NULL
);
214 int wxBitmap::GetDepth() const
216 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
218 return M_BITMAP
->m_surface
->GetDepth();
221 wxMask
*wxBitmap::GetMask() const
223 wxCHECK_MSG( Ok(), NULL
, wxT("invalid bitmap") );
225 return M_BITMAP
->m_mask
;
228 void wxBitmap::SetMask(wxMask
*mask
)
230 wxCHECK_RET( Ok(), wxT("invalid bitmap") );
233 delete M_BITMAP
->m_mask
;
234 M_BITMAP
->m_mask
= mask
;
237 bool wxBitmap::CopyFromIcon(const wxIcon
& icon
)
239 *this = *((wxBitmap
*)(&icon
));
243 wxBitmap
wxBitmap::GetSubBitmap(const wxRect
& rect
) const
246 rect
.x
>= 0 && rect
.y
>= 0 &&
247 rect
.x
+rect
.width
<= GetWidth() &&
248 rect
.y
+rect
.height
<= GetHeight(),
250 wxT("invalid bitmap or bitmap region") );
252 // NB: DirectFB subsurfaces share the same pixels buffer, so we must
253 // clone the obtained subsurface
254 DFBRectangle r
= { rect
.x
, rect
.y
, rect
.width
, rect
.height
};
255 return wxBitmap(M_BITMAP
->m_surface
->GetSubSurface(&r
)->Clone());
258 #warning "to common code"
259 bool wxBitmap::LoadFile(const wxString
&name
, wxBitmapType type
)
263 wxBitmapHandler
*handler
= FindHandler(type
);
265 if ( handler
== NULL
)
268 if ( !image
.LoadFile(name
, type
) || !image
.Ok() )
270 wxLogError("no bitmap handler for type %d defined.", type
);
275 *this = wxBitmap(image
);
280 m_refData
= new wxBitmapRefData();
282 return handler
->LoadFile(this, name
, type
, -1, -1);
285 #warning "to common code"
286 bool wxBitmap::SaveFile(const wxString
& filename
, wxBitmapType type
, const wxPalette
*palette
) const
288 wxCHECK_MSG( Ok(), false, wxT("invalid bitmap") );
290 wxBitmapHandler
*handler
= FindHandler(type
);
292 if ( handler
== NULL
)
294 wxImage image
= ConvertToImage();
297 image
.SetPalette(*palette
);
298 #endif // wxUSE_PALETTE
301 return image
.SaveFile(filename
, type
);
304 wxLogError("no bitmap handler for type %d defined.", type
);
309 return handler
->SaveFile(this, filename
, type
, palette
);
313 wxPalette
*wxBitmap::GetPalette() const
315 wxCHECK_MSG( Ok(), NULL
, wxT("invalid bitmap") );
317 return M_BITMAP
->m_palette
;
320 void wxBitmap::SetPalette(const wxPalette
& palette
)
322 wxCHECK_RET( Ok(), wxT("invalid bitmap") );
323 wxCHECK_RET( GetDepth() > 1 && GetDepth() <= 8, wxT("cannot set palette for bitmap of this depth") );
326 delete M_BITMAP
->m_palette
;
327 M_BITMAP
->m_palette
= NULL
;
329 if ( !palette
.Ok() ) return;
331 M_BITMAP
->m_palette
= new wxPalette(palette
);
333 #endif // wxUSE_PALETTE
335 void wxBitmap::SetHeight(int height
)
339 wxFAIL_MSG( _T("SetHeight not implemented") );
342 void wxBitmap::SetWidth(int width
)
346 wxFAIL_MSG( _T("SetWidth not implemented") );
349 void wxBitmap::SetDepth(int depth
)
353 wxFAIL_MSG( _T("SetDepth not implemented") );
356 wxIDirectFBSurfacePtr
wxBitmap::GetDirectFBSurface() const
358 wxCHECK_MSG( Ok(), NULL
, wxT("invalid bitmap") );
360 return M_BITMAP
->m_surface
;
363 wxObjectRefData
*wxBitmap::CreateRefData() const
365 return new wxBitmapRefData
;
368 wxObjectRefData
*wxBitmap::CloneRefData(const wxObjectRefData
*data
) const
370 return new wxBitmapRefData(*(wxBitmapRefData
*)data
);
375 void wxBitmap::InitStandardHandlers()
377 // not wxBitmap handlers, we rely on wxImage