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 wxIDirectFBSurfacePtr 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 wxIDirectFBSurfacePtr
surface(wxIDirectFB::Get()->CreateSurface(&desc
));
215 m_refData
= new wxBitmapRefData();
216 M_BITMAP
->m_surface
= surface
;
221 #warning "FIXME: move this to common code"
222 bool wxBitmap::CreateFromXpm(const char **bits
)
224 wxCHECK_MSG( bits
!= NULL
, false, wxT("invalid bitmap data") );
226 #if wxUSE_IMAGE && wxUSE_XPM
227 wxXPMDecoder decoder
;
228 wxImage img
= decoder
.ReadData(bits
);
229 wxCHECK_MSG( img
.Ok(), false, wxT("invalid bitmap data") );
231 *this = wxBitmap(img
);
235 wxFAIL_MSG( _T("creating bitmaps from XPMs not supported") );
237 #endif // wxUSE_IMAGE && wxUSE_XPM
241 wxBitmap::wxBitmap(const wxImage
& image
, int depth
)
243 wxCHECK_RET( image
.Ok(), wxT("invalid image") );
246 wxImage
wxBitmap::ConvertToImage() const
248 wxCHECK_MSG( Ok(), wxNullImage
, wxT("invalid bitmap") );
250 return wxNullImage
; // FIXME
252 #endif // wxUSE_IMAGE
254 wxBitmap::wxBitmap(const wxString
&filename
, wxBitmapType type
)
256 LoadFile(filename
, type
);
259 wxBitmap::wxBitmap(const char bits
[], int width
, int height
, int depth
)
261 wxCHECK_RET( depth
== 1, wxT("can only create mono bitmap from XBM data") );
264 bool wxBitmap::Ok() const
266 return (m_refData
!= NULL
&& M_BITMAP
->m_surface
);
269 bool wxBitmap::operator==(const wxBitmap
& bmp
) const
271 // FIXME: is this the right way to compare bitmaps?
272 return (m_refData
== bmp
.m_refData
);
275 int wxBitmap::GetHeight() const
277 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
280 M_BITMAP
->m_surface
->GetSize(NULL
, &h
);
284 int wxBitmap::GetWidth() const
286 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
289 M_BITMAP
->m_surface
->GetSize(&w
, NULL
);
293 int wxBitmap::GetDepth() const
295 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
297 return wxDfbGetSurfaceDepth(M_BITMAP
->m_surface
);
300 wxMask
*wxBitmap::GetMask() const
302 wxCHECK_MSG( Ok(), NULL
, wxT("invalid bitmap") );
304 return M_BITMAP
->m_mask
;
307 void wxBitmap::SetMask(wxMask
*mask
)
309 wxCHECK_RET( Ok(), wxT("invalid bitmap") );
311 delete M_BITMAP
->m_mask
;
312 M_BITMAP
->m_mask
= mask
;
315 bool wxBitmap::CopyFromIcon(const wxIcon
& icon
)
317 *this = *((wxBitmap
*)(&icon
));
321 wxBitmap
wxBitmap::GetSubBitmap(const wxRect
& rect
) const
324 rect
.x
>= 0 && rect
.y
>= 0 &&
325 rect
.x
+rect
.width
<= GetWidth() &&
326 rect
.y
+rect
.height
<= GetHeight(),
328 wxT("invalid bitmap or bitmap region") );
332 #warning "to common code"
333 bool wxBitmap::LoadFile(const wxString
&name
, wxBitmapType type
)
337 wxBitmapHandler
*handler
= FindHandler(type
);
339 if ( handler
== NULL
)
342 if ( !image
.LoadFile(name
, type
) || !image
.Ok() )
344 wxLogError("no bitmap handler for type %d defined.", type
);
349 *this = wxBitmap(image
);
354 m_refData
= new wxBitmapRefData();
356 return handler
->LoadFile(this, name
, type
, -1, -1);
359 #warning "to common code"
360 bool wxBitmap::SaveFile(const wxString
& filename
, wxBitmapType type
, const wxPalette
*palette
) const
362 wxCHECK_MSG( Ok(), false, wxT("invalid bitmap") );
364 wxBitmapHandler
*handler
= FindHandler(type
);
366 if ( handler
== NULL
)
368 wxImage image
= ConvertToImage();
371 image
.SetPalette(*palette
);
372 #endif // wxUSE_PALETTE
375 return image
.SaveFile(filename
, type
);
378 wxLogError("no bitmap handler for type %d defined.", type
);
383 return handler
->SaveFile(this, filename
, type
, palette
);
387 wxPalette
*wxBitmap::GetPalette() const
389 wxCHECK_MSG( Ok(), NULL
, wxT("invalid bitmap") );
391 return M_BITMAP
->m_palette
;
394 void wxBitmap::SetPalette(const wxPalette
& palette
)
396 wxCHECK_RET( Ok(), wxT("invalid bitmap") );
397 wxCHECK_RET( GetDepth() > 1 && GetDepth() <= 8, wxT("cannot set palette for bitmap of this depth") );
399 delete M_BITMAP
->m_palette
;
400 M_BITMAP
->m_palette
= NULL
;
402 if ( !palette
.Ok() ) return;
404 M_BITMAP
->m_palette
= new wxPalette(palette
);
406 #endif // wxUSE_PALETTE
408 void wxBitmap::SetHeight(int height
)
414 void wxBitmap::SetWidth(int width
)
420 void wxBitmap::SetDepth(int depth
)
426 wxIDirectFBSurfacePtr
wxBitmap::GetDirectFBSurface() const
428 wxCHECK_MSG( Ok(), NULL
, wxT("invalid bitmap") );
430 return M_BITMAP
->m_surface
;
433 wxObjectRefData
*wxBitmap::CreateRefData() const
435 return new wxBitmapRefData
;
438 wxObjectRefData
*wxBitmap::CloneRefData(const wxObjectRefData
*data
) const
440 return new wxBitmapRefData(*(wxBitmapRefData
*)data
);
445 void wxBitmap::InitStandardHandlers()
447 // not wxBitmap handlers, we rely on wxImage