1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/dfb/bitmap.cpp
3 // Purpose: wxBitmap implementation
4 // Author: Vaclav Slavik
6 // Copyright: (c) 2006 REA Elektronik GmbH
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
10 // For compilers that support precompilation, includes "wx.h".
11 #include "wx/wxprec.h"
22 #include "wx/bitmap.h"
23 #include "wx/colour.h"
25 #include "wx/rawbmp.h"
27 #include "wx/dfb/private.h"
29 //-----------------------------------------------------------------------------
30 // helpers for translating between wx and DFB pixel formats
31 //-----------------------------------------------------------------------------
36 // NB: Most of this conversion code is needed because of differences between
37 // wxImage and wxDFB's wxBitmap representations:
38 // (1) wxImage uses RGB order, while DirectFB uses BGR
39 // (2) wxImage has alpha channel in a separate plane, while DirectFB puts
40 // all components into single BGRA plane
42 // pitch = stride = # of bytes between the start of N-th line and (N+1)-th line
43 // {Src,Dst}PixSize = # of bytes used to represent one pixel
44 template<int SrcPixSize
, int DstPixSize
>
45 void CopyPixelsAndSwapRGB(unsigned w
, unsigned h
,
46 const unsigned char *src
,
51 unsigned src_advance
= src_pitch
- SrcPixSize
* w
;
52 unsigned dst_advance
= dst_pitch
- DstPixSize
* w
;
53 for ( unsigned y
= 0; y
< h
; y
++, src
+= src_advance
, dst
+= dst_advance
)
55 for ( unsigned x
= 0; x
< w
; x
++, src
+= SrcPixSize
, dst
+= DstPixSize
)
57 // copy with RGB -> BGR translation:
65 void CopySurfaceToImage(const wxIDirectFBSurfacePtr
& surface
, wxImage
& image
)
67 wxIDirectFBSurface::Locked
locked(surface
, DSLF_READ
);
68 wxCHECK_RET( locked
.ptr
, "failed to lock surface" );
70 const unsigned width
= image
.GetWidth();
71 const unsigned height
= image
.GetHeight();
72 const DFBSurfacePixelFormat format
= surface
->GetPixelFormat();
74 // copy RGB data from the surface:
78 CopyPixelsAndSwapRGB
<3,3>
81 (unsigned char*)locked
.ptr
, locked
.pitch
,
82 image
.GetData(), width
* 3
88 CopyPixelsAndSwapRGB
<4,3>
91 (unsigned char*)locked
.ptr
, locked
.pitch
,
92 image
.GetData(), width
* 3
97 wxFAIL_MSG( "unexpected pixel format" );
101 // extract alpha channel if the bitmap has it:
102 if ( format
== DSPF_ARGB
)
104 // create alpha plane:
107 // and copy alpha data to it:
108 const unsigned advance
= locked
.pitch
- 4 * width
;
109 unsigned char *alpha
= image
.GetAlpha();
110 // NB: "+3" is to get pointer to alpha component
111 const unsigned char *src
= ((unsigned char*)locked
.ptr
) + 3;
113 for ( unsigned y
= 0; y
< height
; y
++, src
+= advance
)
114 for ( unsigned x
= 0; x
< width
; x
++, src
+= 4 )
119 void CopyImageToSurface(const wxImage
& image
,
120 const wxIDirectFBSurfacePtr
& surface
)
122 wxIDirectFBSurface::Locked
locked(surface
, DSLF_WRITE
);
123 wxCHECK_RET( locked
.ptr
, "failed to lock surface" );
125 const unsigned width
= image
.GetWidth();
126 const unsigned height
= image
.GetHeight();
127 const DFBSurfacePixelFormat format
= surface
->GetPixelFormat();
129 // copy RGB data to the surface:
133 CopyPixelsAndSwapRGB
<3,3>
136 image
.GetData(), width
* 3,
137 (unsigned char*)locked
.ptr
, locked
.pitch
143 CopyPixelsAndSwapRGB
<3,4>
146 image
.GetData(), width
* 3,
147 (unsigned char*)locked
.ptr
, locked
.pitch
152 wxFAIL_MSG( "unexpected pixel format" );
156 // if the image has alpha channel, merge it in:
157 if ( format
== DSPF_ARGB
)
159 wxCHECK_RET( image
.HasAlpha(), "logic error - ARGB, but no alpha" );
161 const unsigned advance
= locked
.pitch
- 4 * width
;
162 const unsigned char *alpha
= image
.GetAlpha();
163 // NB: "+3" is to get pointer to alpha component
164 unsigned char *dest
= ((unsigned char*)locked
.ptr
) + 3;
166 for ( unsigned y
= 0; y
< height
; y
++, dest
+= advance
)
167 for ( unsigned x
= 0; x
< width
; x
++, dest
+= 4 )
172 wxIDirectFBSurfacePtr
173 CreateSurfaceWithFormat(int w
, int h
, DFBSurfacePixelFormat format
)
175 DFBSurfaceDescription desc
;
176 desc
.flags
= (DFBSurfaceDescriptionFlags
)
177 (DSDESC_CAPS
| DSDESC_WIDTH
| DSDESC_HEIGHT
);
178 desc
.caps
= DSCAPS_NONE
;
182 if ( format
!= DSPF_UNKNOWN
)
184 desc
.flags
= (DFBSurfaceDescriptionFlags
)(
185 desc
.flags
| DSDESC_PIXELFORMAT
);
186 desc
.pixelformat
= format
;
189 return wxIDirectFB::Get()->CreateSurface(&desc
);
192 // Creates a surface that will use wxImage's pixel data (RGB only)
193 wxIDirectFBSurfacePtr
CreateSurfaceForImage(const wxImage
& image
)
195 wxCHECK_MSG( image
.IsOk(), NULL
, "invalid image" );
196 // FIXME_DFB: implement alpha handling by merging alpha buffer with RGB
197 // into a temporary RGBA surface
198 wxCHECK_MSG( !image
.HasAlpha(), NULL
, "alpha channel not supported" );
200 // NB: wxImage uses RGB order of bytes while DirectFB uses BGR, so we
201 // cannot use preallocated surface that shares data with wxImage, we
202 // have to copy the data to temporary surface instead
203 return CreateSurfaceWithFormat(image
.GetWidth(), image
.GetHeight(),
207 bool ConvertSurfaceToFormat(wxIDirectFBSurfacePtr
& surface
,
208 DFBSurfacePixelFormat format
)
210 if ( surface
->GetPixelFormat() == format
)
214 surface
->GetSize(&w
, &h
);
215 wxIDirectFBSurfacePtr s
= CreateSurfaceWithFormat(w
, h
, format
);
219 if ( !s
->SetBlittingFlags(DSBLIT_NOFX
) )
221 if ( !s
->Blit(surface
->GetRaw(), NULL
, 0, 0) )
228 DFBSurfacePixelFormat
DepthToFormat(int depth
)
235 // NB: we treat depth=32 as requesting ARGB for consistency with
239 wxFAIL_MSG( "unsupported depth requested" );
246 // ----------------------------------------------------------------------------
247 // monochrome bitmap functions
248 // ----------------------------------------------------------------------------
250 // this function works with destination buffer of type T and not char (where T
251 // is typically wxUint32 for RGB32, wxUint16 for RGB16 &c) as we don't need
252 // access to the individual pixel components -- and so it's not suitable for
253 // the pixel formats with pixel size not equal to 8, 16 or 32
254 template <typename T
, int White
, int Black
>
258 const unsigned char *src
,
259 const wxIDirectFBSurface::Locked locked
)
261 static const int BITS_PER_BYTE
= 8;
263 // extra padding to add to dst at the end of each row: this works on the
264 // assumption that all rows are aligned at multiples of T (and usually 4
265 // bytes) boundary so check for it (and change the code if this assert is
267 wxASSERT_MSG( !(locked
.pitch
% sizeof(T
)), "image rows not aligned?" );
268 const int padDst
= (locked
.pitch
- width
*sizeof(T
))/sizeof(T
);
270 int x
= 0; // position in the current bitmap row
272 // a single char in src corresponds to 8 destination pixels and the last
273 // char in the row contains padding if necessary, i.e. there is always an
274 // integer number of chars per row
275 const unsigned char * const
276 srcEnd
= src
+ ((width
+ BITS_PER_BYTE
- 1)/BITS_PER_BYTE
)*height
;
278 // we operate with sizeof(T), not 1, bytes at once
279 T
*dst
= static_cast<T
*>(locked
.ptr
);
280 while ( src
< srcEnd
)
282 unsigned char val
= *src
++;
284 for ( int bit
= 0; bit
< BITS_PER_BYTE
; bit
++ )
286 *dst
++ = val
& 1 ? White
: Black
;
299 CopyBitsToSurface(const unsigned char *bits
,
302 wxIDirectFBSurfacePtr
& surface
)
304 wxIDirectFBSurface::Locked
locked(surface
, DSLF_WRITE
);
305 wxCHECK_MSG( locked
.ptr
, false, "failed to lock surface" );
307 const DFBSurfacePixelFormat format
= surface
->GetPixelFormat();
312 // we suppose that these indices correspond to the palette entries
313 // for white and black, respectively, but a better idea would be to
314 // use IDirectFBPalette::FindBestMatch() to determine them
315 CopyBits
<wxUint8
, 0xff, 0>(width
, height
, bits
, locked
);
319 CopyBits
<wxUint16
, 0xffff, 0>(width
, height
, bits
, locked
);
323 CopyBits
<wxUint32
, 0xffffffff, 0>(width
, height
, bits
, locked
);
327 // we don't really have time to implement efficient support for all
328 // the other formats so simply (and awfully slowly, of course...)
329 // convert everything else from RGB32
330 surface
= CreateSurfaceWithFormat(width
, height
, DSPF_RGB32
);
334 if ( !CopyBitsToSurface(bits
, width
, height
, surface
) )
337 if ( !ConvertSurfaceToFormat(surface
, format
) )
344 } // anonymous namespace
346 //-----------------------------------------------------------------------------
348 //-----------------------------------------------------------------------------
350 class wxBitmapRefData
: public wxGDIRefData
361 wxBitmapRefData(const wxBitmapRefData
& data
)
363 m_surface
= data
.m_surface
? data
.m_surface
->Clone() : NULL
;
365 m_mask
= data
.m_mask
? new wxMask(*data
.m_mask
) : NULL
;
367 m_palette
= data
.m_palette
? new wxPalette(*data
.m_palette
) : NULL
;
371 virtual ~wxBitmapRefData()
379 virtual bool IsOk() const { return m_surface
; }
381 wxIDirectFBSurfacePtr m_surface
;
384 wxPalette
*m_palette
;
388 #define M_BITMAP ((wxBitmapRefData *)m_refData)
390 //-----------------------------------------------------------------------------
392 //-----------------------------------------------------------------------------
394 IMPLEMENT_DYNAMIC_CLASS(wxBitmap
, wxBitmapBase
)
396 bool wxBitmap::Create(const wxIDirectFBSurfacePtr
& surface
)
400 wxCHECK_MSG( surface
, false, "invalid surface" );
402 m_refData
= new wxBitmapRefData();
403 M_BITMAP
->m_surface
= surface
;
407 bool wxBitmap::Create(int width
, int height
, int depth
)
409 return CreateWithFormat(width
, height
, DepthToFormat(depth
));
412 bool wxBitmap::CreateWithFormat(int width
, int height
, int dfbFormat
)
416 wxCHECK_MSG( width
> 0 && height
> 0, false, wxT("invalid bitmap size") );
418 return Create(CreateSurfaceWithFormat(width
, height
,
419 DFBSurfacePixelFormat(dfbFormat
)));
423 wxBitmap::wxBitmap(const wxImage
& imageOrig
, int depth
)
425 wxCHECK_RET( imageOrig
.IsOk(), wxT("invalid image") );
427 wxImage
image(imageOrig
);
429 // convert mask to alpha channel, because wxMask isn't implemented yet
430 // FIXME: don't do this, implement proper wxMask support
431 if ( image
.HasMask() )
434 DFBSurfacePixelFormat format
= DepthToFormat(depth
);
435 if ( format
== DSPF_UNKNOWN
&& image
.HasAlpha() )
438 // create surface in screen's format (unless we need alpha channel,
439 // in which case use ARGB):
440 if ( !CreateWithFormat(image
.GetWidth(), image
.GetHeight(), format
) )
443 // then copy the image to it:
444 wxIDirectFBSurfacePtr dst
= M_BITMAP
->m_surface
;
446 switch ( dst
->GetPixelFormat() )
451 CopyImageToSurface(image
, dst
);
456 // wxBitmap uses different pixel format, so we have to use a
457 // temporary surface and blit to the bitmap via it:
458 wxIDirectFBSurfacePtr
src(CreateSurfaceForImage(image
));
459 CopyImageToSurface(image
, src
);
461 if ( !dst
->SetBlittingFlags(DSBLIT_NOFX
) )
463 if ( !dst
->Blit(src
->GetRaw(), NULL
, 0, 0) )
469 wxImage
wxBitmap::ConvertToImage() const
471 wxCHECK_MSG( IsOk(), wxNullImage
, wxT("invalid bitmap") );
473 wxImage
img(GetWidth(), GetHeight());
474 wxIDirectFBSurfacePtr src
= M_BITMAP
->m_surface
;
476 switch ( src
->GetPixelFormat() )
481 CopySurfaceToImage(src
, img
);
485 // wxBitmap uses different pixel format, so we have to use a
486 // temporary surface and blit to the bitmap via it:
487 wxIDirectFBSurfacePtr
dst(CreateSurfaceForImage(img
));
489 if ( !dst
->SetBlittingFlags(DSBLIT_NOFX
) )
491 if ( !dst
->Blit(src
->GetRaw(), NULL
, 0, 0) )
494 CopySurfaceToImage(dst
, img
);
498 // FIXME: implement mask setting in the image
499 wxASSERT_MSG( GetMask() == NULL
, "bitmap masks are ignored for now" );
503 #endif // wxUSE_IMAGE
505 void *wxBitmap::GetRawData(wxPixelDataBase
& data
, int bpp
)
507 wxCHECK_MSG( IsOk(), NULL
, "invalid bitmap" );
511 DFBSurfacePixelFormat format
;
517 // convert the bitmap into format compatible with requested raw access;
518 // note that we don't bother converting the bitmap back in UngetRawData(),
519 // as unpacked formats (RGB24, RGB32) are the common case and converting
520 // between them while blitting is fast enough (FIXME?)
521 if ( !ConvertSurfaceToFormat(M_BITMAP
->m_surface
, format
) )
525 if ( !M_BITMAP
->m_surface
->Lock
527 (DFBSurfaceLockFlags
)(DSLF_READ
| DSLF_WRITE
),
533 M_BITMAP
->m_surface
->GetSize(&data
.m_width
, &data
.m_height
);
538 void wxBitmap::UngetRawData(wxPixelDataBase
& WXUNUSED(data
))
540 M_BITMAP
->m_surface
->Unlock();
543 bool wxBitmap::HasAlpha() const
545 wxCHECK_MSG( IsOk(), false, "invalid bitmap" );
547 return M_BITMAP
->m_surface
->GetPixelFormat() == DSPF_ARGB
;
550 wxBitmap::wxBitmap(const wxString
&filename
, wxBitmapType type
)
552 LoadFile(filename
, type
);
555 wxBitmap::wxBitmap(const char bits
[], int width
, int height
, int depth
)
557 wxCHECK_RET( depth
== 1, wxT("can only create mono bitmap from XBM data") );
559 // create bitmap in the device-dependent format
560 if ( !CreateWithFormat(width
, height
, DSPF_UNKNOWN
) )
563 if ( !CopyBitsToSurface((const unsigned char *)bits
,
564 width
, height
, M_BITMAP
->m_surface
) )
568 int wxBitmap::GetHeight() const
570 wxCHECK_MSG( IsOk(), -1, wxT("invalid bitmap") );
573 M_BITMAP
->m_surface
->GetSize(NULL
, &h
);
577 int wxBitmap::GetWidth() const
579 wxCHECK_MSG( IsOk(), -1, wxT("invalid bitmap") );
582 M_BITMAP
->m_surface
->GetSize(&w
, NULL
);
586 int wxBitmap::GetDepth() const
588 wxCHECK_MSG( IsOk(), -1, wxT("invalid bitmap") );
590 return M_BITMAP
->m_surface
->GetDepth();
593 wxMask
*wxBitmap::GetMask() const
595 wxCHECK_MSG( IsOk(), NULL
, wxT("invalid bitmap") );
597 return M_BITMAP
->m_mask
;
600 void wxBitmap::SetMask(wxMask
*mask
)
602 wxCHECK_RET( IsOk(), wxT("invalid bitmap") );
605 delete M_BITMAP
->m_mask
;
606 M_BITMAP
->m_mask
= mask
;
609 bool wxBitmap::CopyFromIcon(const wxIcon
& icon
)
611 *this = *((wxBitmap
*)(&icon
));
615 wxBitmap
wxBitmap::GetSubBitmap(const wxRect
& rect
) const
617 wxCHECK_MSG( IsOk() &&
618 rect
.x
>= 0 && rect
.y
>= 0 &&
619 rect
.x
+rect
.width
<= GetWidth() &&
620 rect
.y
+rect
.height
<= GetHeight(),
622 wxT("invalid bitmap or bitmap region") );
624 // NB: DirectFB subsurfaces share the same pixels buffer, so we must
625 // clone the obtained subsurface
626 DFBRectangle r
= { rect
.x
, rect
.y
, rect
.width
, rect
.height
};
627 return wxBitmap(M_BITMAP
->m_surface
->GetSubSurface(&r
)->Clone());
630 #warning "to common code"
631 bool wxBitmap::LoadFile(const wxString
&name
, wxBitmapType type
)
635 wxBitmapHandler
*handler
= FindHandler(type
);
637 if ( handler
== NULL
)
640 if ( !image
.LoadFile(name
, type
) || !image
.IsOk() )
642 wxLogError(_("No bitmap handler for type %d defined."), type
);
647 *this = wxBitmap(image
);
652 m_refData
= new wxBitmapRefData();
654 return handler
->LoadFile(this, name
, type
, -1, -1);
657 #warning "to common code"
658 bool wxBitmap::SaveFile(const wxString
& filename
, wxBitmapType type
, const wxPalette
*palette
) const
660 wxCHECK_MSG( IsOk(), false, wxT("invalid bitmap") );
662 wxBitmapHandler
*handler
= FindHandler(type
);
664 if ( handler
== NULL
)
666 wxImage image
= ConvertToImage();
669 image
.SetPalette(*palette
);
670 #endif // wxUSE_PALETTE
673 return image
.SaveFile(filename
, type
);
676 wxLogError(_("No bitmap handler for type %d defined."), type
);
681 return handler
->SaveFile(this, filename
, type
, palette
);
685 wxPalette
*wxBitmap::GetPalette() const
687 wxCHECK_MSG( IsOk(), NULL
, wxT("invalid bitmap") );
689 return M_BITMAP
->m_palette
;
692 void wxBitmap::SetPalette(const wxPalette
& palette
)
694 wxCHECK_RET( IsOk(), wxT("invalid bitmap") );
695 wxCHECK_RET( GetDepth() > 1 && GetDepth() <= 8, wxT("cannot set palette for bitmap of this depth") );
698 wxDELETE(M_BITMAP
->m_palette
);
700 if ( !palette
.IsOk() ) return;
702 M_BITMAP
->m_palette
= new wxPalette(palette
);
704 #endif // wxUSE_PALETTE
706 void wxBitmap::SetHeight(int height
)
710 wxFAIL_MSG( "SetHeight not implemented" );
713 void wxBitmap::SetWidth(int width
)
717 wxFAIL_MSG( "SetWidth not implemented" );
720 void wxBitmap::SetDepth(int depth
)
722 DFBSurfacePixelFormat format
= DepthToFormat(depth
);
723 if ( M_BITMAP
->m_surface
->GetPixelFormat() == format
)
729 M_BITMAP
->m_surface
->GetSize(&w
, &h
);
730 wxIDirectFBSurfacePtr s
= CreateSurfaceWithFormat(w
, h
, format
);
733 if ( !s
->SetBlittingFlags(DSBLIT_NOFX
) )
735 if ( !s
->Blit(M_BITMAP
->m_surface
->GetRaw(), NULL
, 0, 0) )
738 M_BITMAP
->m_surface
= s
;
741 wxIDirectFBSurfacePtr
wxBitmap::GetDirectFBSurface() const
743 wxCHECK_MSG( IsOk(), NULL
, wxT("invalid bitmap") );
745 return M_BITMAP
->m_surface
;
748 wxGDIRefData
*wxBitmap::CreateGDIRefData() const
750 return new wxBitmapRefData
;
753 wxGDIRefData
*wxBitmap::CloneGDIRefData(const wxGDIRefData
*data
) const
755 return new wxBitmapRefData(*(wxBitmapRefData
*)data
);
760 void wxBitmap::InitStandardHandlers()
762 // not wxBitmap handlers, we rely on wxImage