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
)
73 m_surface
= data
.m_surface
->Clone();
75 m_mask
= data
.m_mask
? new wxMask(*data
.m_mask
) : NULL
;
77 m_palette
= data
.m_palette
? new wxPalette(*data
.m_palette
) : NULL
;
89 wxIDirectFBSurfacePtr m_surface
;
96 #define M_BITMAP ((wxBitmapRefData *)m_refData)
98 //-----------------------------------------------------------------------------
100 //-----------------------------------------------------------------------------
102 IMPLEMENT_ABSTRACT_CLASS(wxBitmapHandler
, wxBitmapHandlerBase
)
103 IMPLEMENT_DYNAMIC_CLASS(wxBitmap
, wxBitmapBase
)
105 wxBitmap::wxBitmap(int width
, int height
, int depth
)
107 Create(width
, height
, depth
);
110 bool wxBitmap::Create(const wxIDirectFBSurfacePtr
& surface
)
114 wxCHECK_MSG( surface
, false, _T("invalid surface") );
116 m_refData
= new wxBitmapRefData();
117 M_BITMAP
->m_surface
= surface
;
121 bool wxBitmap::Create(int width
, int height
, int depth
)
125 wxCHECK_MSG( width
> 0 && height
> 0, false, wxT("invalid bitmap size") );
126 wxCHECK_MSG( depth
== -1, false, wxT("only default depth supported now") );
128 DFBSurfaceDescription desc
;
129 desc
.flags
= (DFBSurfaceDescriptionFlags
)(
130 DSDESC_CAPS
| DSDESC_WIDTH
| DSDESC_HEIGHT
);
131 desc
.caps
= DSCAPS_NONE
;
133 desc
.height
= height
;
135 return Create(wxIDirectFB::Get()->CreateSurface(&desc
));
139 wxBitmap::wxBitmap(const wxImage
& image
, int depth
)
141 wxCHECK_RET( image
.Ok(), wxT("invalid image") );
143 // create surface in screen's format:
144 if ( !Create(image
.GetWidth(), image
.GetHeight(), depth
) )
147 // then copy the image to it:
148 wxIDirectFBSurfacePtr
src(CreateSurfaceForImage(image
));
149 wxIDirectFBSurfacePtr dst
= M_BITMAP
->m_surface
;
151 if ( !dst
->SetBlittingFlags(DSBLIT_NOFX
) )
153 if ( !dst
->Blit(src
->GetRaw(), NULL
, 0, 0) )
156 // FIXME: implement mask creation from image's mask (or alpha channel?)
157 wxASSERT_MSG( !image
.HasMask(), _T("image masks are ignored for now") );
160 wxImage
wxBitmap::ConvertToImage() const
162 wxCHECK_MSG( Ok(), wxNullImage
, wxT("invalid bitmap") );
164 wxImage
img(GetWidth(), GetHeight());
165 wxIDirectFBSurfacePtr
dst(CreateSurfaceForImage(img
));
166 wxIDirectFBSurfacePtr src
= M_BITMAP
->m_surface
;
168 if ( !dst
->SetBlittingFlags(DSBLIT_NOFX
) )
170 if ( !dst
->Blit(src
->GetRaw(), NULL
, 0, 0) )
173 // FIXME: implement mask setting in the image
174 wxASSERT_MSG( GetMask() == NULL
, _T("bitmap masks are ignored for now") );
178 #endif // wxUSE_IMAGE
180 wxBitmap::wxBitmap(const wxString
&filename
, wxBitmapType type
)
182 LoadFile(filename
, type
);
185 wxBitmap::wxBitmap(const char bits
[], int width
, int height
, int depth
)
187 wxCHECK_RET( depth
== 1, wxT("can only create mono bitmap from XBM data") );
189 wxFAIL_MSG( _T("not implemented") );
192 bool wxBitmap::IsOk() const
194 return (m_refData
!= NULL
&& M_BITMAP
->m_surface
);
197 bool wxBitmap::operator==(const wxBitmap
& bmp
) const
199 // FIXME: is this the right way to compare bitmaps?
200 return (m_refData
== bmp
.m_refData
);
203 int wxBitmap::GetHeight() const
205 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
208 M_BITMAP
->m_surface
->GetSize(NULL
, &h
);
212 int wxBitmap::GetWidth() const
214 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
217 M_BITMAP
->m_surface
->GetSize(&w
, NULL
);
221 int wxBitmap::GetDepth() const
223 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
225 return M_BITMAP
->m_surface
->GetDepth();
228 wxMask
*wxBitmap::GetMask() const
230 wxCHECK_MSG( Ok(), NULL
, wxT("invalid bitmap") );
232 return M_BITMAP
->m_mask
;
235 void wxBitmap::SetMask(wxMask
*mask
)
237 wxCHECK_RET( Ok(), wxT("invalid bitmap") );
239 delete M_BITMAP
->m_mask
;
240 M_BITMAP
->m_mask
= mask
;
243 bool wxBitmap::CopyFromIcon(const wxIcon
& icon
)
245 *this = *((wxBitmap
*)(&icon
));
249 wxBitmap
wxBitmap::GetSubBitmap(const wxRect
& rect
) const
252 rect
.x
>= 0 && rect
.y
>= 0 &&
253 rect
.x
+rect
.width
<= GetWidth() &&
254 rect
.y
+rect
.height
<= GetHeight(),
256 wxT("invalid bitmap or bitmap region") );
258 // NB: DirectFB subsurfaces share the same pixels buffer, so we must
259 // clone the obtained subsurface
260 DFBRectangle r
= { rect
.x
, rect
.y
, rect
.width
, rect
.height
};
261 return wxBitmap(M_BITMAP
->m_surface
->GetSubSurface(&r
)->Clone());
264 #warning "to common code"
265 bool wxBitmap::LoadFile(const wxString
&name
, wxBitmapType type
)
269 wxBitmapHandler
*handler
= FindHandler(type
);
271 if ( handler
== NULL
)
274 if ( !image
.LoadFile(name
, type
) || !image
.Ok() )
276 wxLogError("no bitmap handler for type %d defined.", type
);
281 *this = wxBitmap(image
);
286 m_refData
= new wxBitmapRefData();
288 return handler
->LoadFile(this, name
, type
, -1, -1);
291 #warning "to common code"
292 bool wxBitmap::SaveFile(const wxString
& filename
, wxBitmapType type
, const wxPalette
*palette
) const
294 wxCHECK_MSG( Ok(), false, wxT("invalid bitmap") );
296 wxBitmapHandler
*handler
= FindHandler(type
);
298 if ( handler
== NULL
)
300 wxImage image
= ConvertToImage();
303 image
.SetPalette(*palette
);
304 #endif // wxUSE_PALETTE
307 return image
.SaveFile(filename
, type
);
310 wxLogError("no bitmap handler for type %d defined.", type
);
315 return handler
->SaveFile(this, filename
, type
, palette
);
319 wxPalette
*wxBitmap::GetPalette() const
321 wxCHECK_MSG( Ok(), NULL
, wxT("invalid bitmap") );
323 return M_BITMAP
->m_palette
;
326 void wxBitmap::SetPalette(const wxPalette
& palette
)
328 wxCHECK_RET( Ok(), wxT("invalid bitmap") );
329 wxCHECK_RET( GetDepth() > 1 && GetDepth() <= 8, wxT("cannot set palette for bitmap of this depth") );
331 delete M_BITMAP
->m_palette
;
332 M_BITMAP
->m_palette
= NULL
;
334 if ( !palette
.Ok() ) return;
336 M_BITMAP
->m_palette
= new wxPalette(palette
);
338 #endif // wxUSE_PALETTE
340 void wxBitmap::SetHeight(int height
)
344 wxFAIL_MSG( _T("SetHeight not implemented") );
347 void wxBitmap::SetWidth(int width
)
351 wxFAIL_MSG( _T("SetWidth not implemented") );
354 void wxBitmap::SetDepth(int depth
)
358 wxFAIL_MSG( _T("SetDepth not implemented") );
361 wxIDirectFBSurfacePtr
wxBitmap::GetDirectFBSurface() const
363 wxCHECK_MSG( Ok(), NULL
, wxT("invalid bitmap") );
365 return M_BITMAP
->m_surface
;
368 wxObjectRefData
*wxBitmap::CreateRefData() const
370 return new wxBitmapRefData
;
373 wxObjectRefData
*wxBitmap::CloneRefData(const wxObjectRefData
*data
) const
375 return new wxBitmapRefData(*(wxBitmapRefData
*)data
);
380 void wxBitmap::InitStandardHandlers()
382 // not wxBitmap handlers, we rely on wxImage