+static void CopySurfaceToImage(const wxIDirectFBSurfacePtr& surface,
+ wxImage& image)
+{
+ wxIDirectFBSurface::Locked locked(surface, DSLF_READ);
+ wxCHECK_RET( locked.ptr, "failed to lock surface" );
+
+ const unsigned width = image.GetWidth();
+ const unsigned height = image.GetHeight();
+ const DFBSurfacePixelFormat format = surface->GetPixelFormat();
+
+ // copy RGB data from the surface:
+ switch ( format )
+ {
+ case DSPF_RGB24:
+ CopyPixelsAndSwapRGB<3,3>
+ (
+ width, height,
+ (unsigned char*)locked.ptr, locked.pitch,
+ image.GetData(), width * 3
+ );
+ break;
+
+ case DSPF_RGB32:
+ case DSPF_ARGB:
+ CopyPixelsAndSwapRGB<4,3>
+ (
+ width, height,
+ (unsigned char*)locked.ptr, locked.pitch,
+ image.GetData(), width * 3
+ );
+ break;
+
+ default:
+ wxFAIL_MSG( "unexpected pixel format" );
+ return;
+ }
+
+ // extract alpha channel if the bitmap has it:
+ if ( format == DSPF_ARGB )
+ {
+ // create alpha plane:
+ image.SetAlpha();
+
+ // and copy alpha data to it:
+ const unsigned advance = locked.pitch - 4 * width;
+ unsigned char *alpha = image.GetAlpha();
+ // NB: "+3" is to get pointer to alpha component
+ const unsigned char *src = ((unsigned char*)locked.ptr) + 3;
+
+ for ( unsigned y = 0; y < height; y++, src += advance )
+ for ( unsigned x = 0; x < width; x++, src += 4 )
+ *(alpha++) = *src;
+ }
+}
+
+static void CopyImageToSurface(const wxImage& image,
+ const wxIDirectFBSurfacePtr& surface)
+{
+ wxIDirectFBSurface::Locked locked(surface, DSLF_WRITE);
+ wxCHECK_RET( locked.ptr, "failed to lock surface" );
+
+ const unsigned width = image.GetWidth();
+ const unsigned height = image.GetHeight();
+ const DFBSurfacePixelFormat format = surface->GetPixelFormat();
+
+ // copy RGB data to the surface:
+ switch ( format )
+ {
+ case DSPF_RGB24:
+ CopyPixelsAndSwapRGB<3,3>
+ (
+ width, height,
+ image.GetData(), width * 3,
+ (unsigned char*)locked.ptr, locked.pitch
+ );
+ break;
+
+ case DSPF_RGB32:
+ case DSPF_ARGB:
+ CopyPixelsAndSwapRGB<3,4>
+ (
+ width, height,
+ image.GetData(), width * 3,
+ (unsigned char*)locked.ptr, locked.pitch
+ );
+ break;
+
+ default:
+ wxFAIL_MSG( "unexpected pixel format" );
+ return;
+ }
+
+ // if the image has alpha channel, merge it in:
+ if ( format == DSPF_ARGB )
+ {
+ wxCHECK_RET( image.HasAlpha(), "logic error - ARGB, but no alpha" );
+
+ const unsigned advance = locked.pitch - 4 * width;
+ const unsigned char *alpha = image.GetAlpha();
+ // NB: "+3" is to get pointer to alpha component
+ unsigned char *dest = ((unsigned char*)locked.ptr) + 3;
+
+ for ( unsigned y = 0; y < height; y++, dest += advance )
+ for ( unsigned x = 0; x < width; x++, dest += 4 )
+ *dest = *(alpha++);
+ }
+}
+
+static wxIDirectFBSurfacePtr
+CreateSurfaceWithFormat(int w, int h, DFBSurfacePixelFormat format)
+{