From 20e6714a67e1188b9bbb8ca2bc0fb6321e2864fc Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Thu, 13 Sep 2012 17:15:00 +0000 Subject: [PATCH] Add wxBitmap::NewFromPNGData() for creating bitmaps from PNG data. This simple function can be used to create a wxBitmap from the raw image data in PNG format. It is just a thin wrapper around wxImage load functions under almost all platforms but has two advantages: 1. It can be implemented natively for some platforms (currently only OS X). 2. It can be used in a single expression as it doesn't require creating a temporary wxMemoryInputStream and this will be required by wxBITMAP_PNG() macro that will be added soon. git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@72476 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- include/wx/bitmap.h | 21 +++++++++++++++++++-- include/wx/cocoa/bitmap.h | 3 ++- include/wx/msw/bitmap.h | 3 ++- include/wx/os2/bitmap.h | 3 ++- interface/wx/bitmap.h | 25 +++++++++++++++++++++++++ src/common/bmpbase.cpp | 31 +++++++++++++++++++++++++++++++ src/osx/core/bitmap.cpp | 12 ++++++++++++ 7 files changed, 93 insertions(+), 5 deletions(-) diff --git a/include/wx/bitmap.h b/include/wx/bitmap.h index baf02d10ad..82943ca228 100644 --- a/include/wx/bitmap.h +++ b/include/wx/bitmap.h @@ -83,6 +83,23 @@ protected: #define wxBITMAP_SCREEN_DEPTH (-1) +// ---------------------------------------------------------------------------- +// wxBitmapHelpers: container for various bitmap methods common to all ports. +// ---------------------------------------------------------------------------- + +// Unfortunately, currently wxBitmap does not inherit from wxBitmapBase on all +// platforms and this is not easy to fix. So we extract at least some common +// methods into this class from which both wxBitmapBase (and hase wxBitmap on +// all platforms where it does inherit from it) and wxBitmap in wxMSW and other +// exceptional ports (only wxPM and old wxCocoa) inherit. +class WXDLLIMPEXP_CORE wxBitmapHelpers +{ +public: + // Create a new wxBitmap from the PNG data in the given buffer. + static wxBitmap NewFromPNGData(const void* data, size_t size); +}; + + // All ports except wxMSW and wxOS2 use wxBitmapHandler and wxBitmapBase as // base class for wxBitmapHandler; wxMSW and wxOS2 use wxGDIImageHandler as // base class since it allows some code reuse there. @@ -132,12 +149,12 @@ private: DECLARE_ABSTRACT_CLASS(wxBitmapHandler) }; - // ---------------------------------------------------------------------------- // wxBitmap: class which represents platform-dependent bitmap (unlike wxImage) // ---------------------------------------------------------------------------- -class WXDLLIMPEXP_CORE wxBitmapBase : public wxGDIObject +class WXDLLIMPEXP_CORE wxBitmapBase : public wxGDIObject, + public wxBitmapHelpers { public: /* diff --git a/include/wx/cocoa/bitmap.h b/include/wx/cocoa/bitmap.h index 94d7eb3140..59e7bbb732 100644 --- a/include/wx/cocoa/bitmap.h +++ b/include/wx/cocoa/bitmap.h @@ -64,7 +64,8 @@ protected: // wxBitmap // ======================================================================== -class WXDLLIMPEXP_CORE wxBitmap: public wxGDIObject +class WXDLLIMPEXP_CORE wxBitmap: public wxGDIObject, + public wxBitmapHelpers { // ------------------------------------------------------------------------ // initialization diff --git a/include/wx/msw/bitmap.h b/include/wx/msw/bitmap.h index ce3e841aee..ef2a73fb27 100644 --- a/include/wx/msw/bitmap.h +++ b/include/wx/msw/bitmap.h @@ -43,7 +43,8 @@ enum wxBitmapTransparency // NOTE: for wxMSW we don't use the wxBitmapBase base class declared in bitmap.h! // ---------------------------------------------------------------------------- -class WXDLLIMPEXP_CORE wxBitmap : public wxGDIImage +class WXDLLIMPEXP_CORE wxBitmap : public wxGDIImage, + public wxBitmapHelpers { public: // default ctor creates an invalid bitmap, you must Create() it later diff --git a/include/wx/os2/bitmap.h b/include/wx/os2/bitmap.h index 653dd9635a..6069f28580 100644 --- a/include/wx/os2/bitmap.h +++ b/include/wx/os2/bitmap.h @@ -63,7 +63,8 @@ public: // wxBitmap: a mono or colour bitmap // ---------------------------------------------------------------------------- -class WXDLLIMPEXP_CORE wxBitmap : public wxGDIImage +class WXDLLIMPEXP_CORE wxBitmap : public wxGDIImage, + public wxBitmapHelpers { public: // default ctor creates an invalid bitmap, you must Create() it later diff --git a/interface/wx/bitmap.h b/interface/wx/bitmap.h index 1b185d7f3c..9f6c3ffe41 100644 --- a/interface/wx/bitmap.h +++ b/interface/wx/bitmap.h @@ -584,6 +584,31 @@ public: */ virtual bool LoadFile(const wxString& name, wxBitmapType type = wxBITMAP_DEFAULT_TYPE); + /** + Loads a bitmap from the memory containing image data in PNG format. + + This helper function provides the simplest way to create a wxBitmap + from PNG image data. On most platforms, it's simply a wrapper around + wxImage loading functions and so requires the PNG image handler to be + registered by either calling wxInitAllImageHandlers() which also + registers all the other image formats or including the necessary + header: + @code + #include + @endcode + and calling + @code + wxImage::AddHandler(new wxPNGHandler); + @endcode + in your application startup code. + + However under OS X this function uses native image loading and so + doesn't require wxWidgets PNG support. + + @since 2.9.5 + */ + static wxBitmap NewFromPNGData(const void* data, size_t size); + /** Finds the handler with the given name, and removes it. The handler is not deleted. diff --git a/src/common/bmpbase.cpp b/src/common/bmpbase.cpp index 34bf85608e..46804febbd 100644 --- a/src/common/bmpbase.cpp +++ b/src/common/bmpbase.cpp @@ -23,6 +23,12 @@ #include "wx/image.h" #endif // WX_PRECOMP +#if wxUSE_IMAGE && wxUSE_LIBPNG && wxUSE_STREAMS + #define wxHAS_PNG_LOAD + + #include "wx/mstream.h" +#endif + // ---------------------------------------------------------------------------- // wxVariant support // ---------------------------------------------------------------------------- @@ -37,6 +43,31 @@ IMPLEMENT_VARIANT_OBJECT_EXPORTED_SHALLOWCMP(wxIcon,WXDLLEXPORT) //WX_IMPLEMENT_ANY_VALUE_TYPE(wxAnyValueTypeImpl) #endif +// ---------------------------------------------------------------------------- +// wxBitmapHelpers +// ---------------------------------------------------------------------------- + +// wxOSX has a native version and doesn't use this one. + +#ifndef __WXOSX__ + +/* static */ +wxBitmap wxBitmapHelpers::NewFromPNGData(const void* data, size_t size) +{ + wxBitmap bitmap; + +#ifdef wxHAS_PNG_LOAD + wxMemoryInputStream is(data, size); + wxImage image(is, wxBITMAP_TYPE_PNG); + if ( image.IsOk() ) + bitmap = wxBitmap(image); +#endif // wxHAS_PNG_LOAD + + return bitmap; +} + +#endif // !__WXOSX__ + // ---------------------------------------------------------------------------- // wxBitmapBase // ---------------------------------------------------------------------------- diff --git a/src/osx/core/bitmap.cpp b/src/osx/core/bitmap.cpp index b88d13ff95..f6442aa30a 100644 --- a/src/osx/core/bitmap.cpp +++ b/src/osx/core/bitmap.cpp @@ -1736,6 +1736,18 @@ bool wxBundleResourceHandler::LoadFile(wxBitmap *bitmap, return false ; } +/* static */ +wxBitmap wxBitmapHelpers::NewFromPNGData(const void* data, size_t size) +{ + wxCFRef + provider(CGDataProviderCreateWithData(NULL, data, size, NULL) ); + wxCFRef + image(CGImageCreateWithPNGDataProvider(provider, NULL, true, + kCGRenderingIntentDefault)); + + return wxBitmap(image); +} + void wxBitmap::InitStandardHandlers() { #if wxOSX_USE_COCOA_OR_CARBON -- 2.45.2