X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/d07d2bc9d05408c58b76c53a0a291915b88e5064..238ba802f1a9f79cde6e3ebd44e5c4ab78af2055:/wxPython/src/_bitmap.i diff --git a/wxPython/src/_bitmap.i b/wxPython/src/_bitmap.i index 241c9ceada..8aa329e133 100644 --- a/wxPython/src/_bitmap.i +++ b/wxPython/src/_bitmap.i @@ -12,6 +12,9 @@ // Not a %module +%{ +#include <wx/rawbmp.h> +%} //--------------------------------------------------------------------------- @@ -62,9 +65,15 @@ converted to a wx.Bitmap, so any image file format supported by :todo: Find a way to do very efficient PIL Image <--> wx.Bitmap converstions. + +:see: `wx.EmptyBitmap`, `wx.BitmapFromIcon`, `wx.BitmapFromImage`, + `wx.BitmapFromXPMData`, `wx.BitmapFromBits`, `wx.BitmapFromBuffer`, + `wx.BitmapFromBufferRGBA`, `wx.Image` "); +MustHaveApp(wxBitmap); + class wxBitmap : public wxGDIObject { public: @@ -105,7 +114,7 @@ public: wxBitmap(int width, int height, int depth=-1), "Creates a new bitmap of the given size. A depth of -1 indicates the depth of the current screen or visual. Some platforms only support 1 -for monochrome and -1 for the current colour setting.", "", +for monochrome and -1 for the current display depth.", "", EmptyBitmap); DocCtorStrName( @@ -124,38 +133,39 @@ that a colour reduction may have to take place.", "", %extend { - DocStr(wxBitmap(PyObject* listOfStrings), - "Construct a Bitmap from a list of strings formatted as XPM data.", ""); - %name(BitmapFromXPMData) wxBitmap(PyObject* listOfStrings) { - char** cArray = NULL; - wxBitmap* bmp; - - cArray = ConvertListOfStrings(listOfStrings); - if (! cArray) - return NULL; - bmp = new wxBitmap(cArray); - delete [] cArray; - return bmp; - } - - DocStr(wxBitmap(PyObject* bits, int width, int height, int depth=1 ), - "Creates a bitmap from an array of bits. You should only use this + %RenameDocCtor( + BitmapFromXPMData, + "Construct a Bitmap from a list of strings formatted as XPM data.", "", + wxBitmap(PyObject* listOfStrings)) + { + char** cArray = NULL; + wxBitmap* bmp; + + cArray = ConvertListOfStrings(listOfStrings); + if (! cArray) + return NULL; + bmp = new wxBitmap(cArray); + delete [] cArray; + return bmp; + } + + + %RenameDocCtor( + BitmapFromBits, + "Creates a bitmap from an array of bits. You should only use this function for monochrome bitmaps (depth 1) in portable programs: in this case the bits parameter should contain an XBM image. For other -bit depths, the behaviour is platform dependent.", ""); - %name(BitmapFromBits) wxBitmap(PyObject* bits, int width, int height, int depth=1 ) { - char* buf; - int length; - PyString_AsStringAndSize(bits, &buf, &length); - return new wxBitmap(buf, width, height, depth); - } +bit depths, the behaviour is platform dependent.", "", + wxBitmap(PyObject* bits, int width, int height, int depth=1 )) + { + char* buf; + Py_ssize_t length; + PyString_AsStringAndSize(bits, &buf, &length); + return new wxBitmap(buf, width, height, depth); + } } -#ifdef __WXMSW__ - void SetPalette(wxPalette& palette); -#endif - // wxGDIImage methods #ifdef __WXMSW__ long GetHandle(); @@ -199,6 +209,7 @@ bitmap. This preserves mask information so that bitmaps and images can be converted back and forth without loss in that respect.", ""); + DocDeclStr( virtual wxMask* , GetMask() const, "Gets the associated mask (if any) which may have been loaded from a @@ -206,15 +217,17 @@ file or explpicitly set for the bitmap. :see: `SetMask`, `wx.Mask` ", ""); - + // MSW only? wxBitmap GetMaskBitmap() const; + + %disownarg(wxMask*); DocDeclStr( virtual void , SetMask(wxMask* mask), "Sets the mask for this bitmap. :see: `GetMask`, `wx.Mask` ", ""); - + %cleardisown(wxMask*); %extend { DocStr(SetMaskColour, @@ -247,8 +260,8 @@ the ``type`` parameter.", ""); -#if wxUSE_PALETTE virtual wxPalette *GetPalette() const; +#ifdef __WXMSW__ virtual void SetPalette(const wxPalette& palette); #endif @@ -280,19 +293,200 @@ the ``type`` parameter.", ""); #ifdef __WXMSW__ bool CopyFromCursor(const wxCursor& cursor); + +// WXWIN_COMPATIBILITY_2_4 + #if 0 int GetQuality(); void SetQuality(int q); + %pythoncode { GetQuality = wx._deprecated(GetQuality) } + %pythoncode { SetQuality = wx._deprecated(SetQuality) } + #endif #endif %pythoncode { def __nonzero__(self): return self.Ok() } %extend { - bool __eq__(const wxBitmap* other) { return other ? (*self == *other) : False; } - bool __ne__(const wxBitmap* other) { return other ? (*self != *other) : True; } + bool __eq__(const wxBitmap* other) { return other ? (*self == *other) : false; } + bool __ne__(const wxBitmap* other) { return other ? (*self != *other) : true; } } }; +//--------------------------------------------------------------------------- +// Factory functions for creating wxBitmaps from Python buffer objects. They +// use the Abstract Pixel API to be able to set RGB and A bytes directly into +// the wxBitmap's pixel buffer. + +%newobject _BitmapFromBufferAlpha; +%newobject _BitmapFromBuffer; +%inline %{ + wxBitmap* _BitmapFromBufferAlpha(int width, int height, + buffer data, int DATASIZE, + buffer alpha, int ALPHASIZE) + { + if (DATASIZE != width*height*3) { + wxPyErr_SetString(PyExc_ValueError, "Invalid data buffer size."); + return NULL; + } + + if (ALPHASIZE != width*height) { + wxPyErr_SetString(PyExc_ValueError, "Invalid alpha buffer size."); + return NULL; + } + + wxBitmap* bmp = new wxBitmap(width, height, 32); + wxAlphaPixelData pixels(*bmp, wxPoint(0,0), wxSize(width,height)); + if (! pixels) { + // raise an exception... + wxPyErr_SetString(PyExc_RuntimeError, + "Failed to gain raw access to bitmap data."); + return NULL; + } + + pixels.UseAlpha(); + wxAlphaPixelData::Iterator p(pixels); + for (int y=0; y<height; y++) { + wxAlphaPixelData::Iterator rowStart = p; + for (int x=0; x<width; x++) { + p.Red() = *(data++); + p.Green() = *(data++); + p.Blue() = *(data++); + p.Alpha() = *(alpha++); + ++p; + } + p = rowStart; + p.OffsetY(pixels, 1); + } + return bmp; + } + + wxBitmap* _BitmapFromBuffer(int width, int height, buffer data, int DATASIZE) + { + if (DATASIZE != width*height*3) { + wxPyErr_SetString(PyExc_ValueError, "Invalid data buffer size."); + return NULL; + } + + wxBitmap* bmp = new wxBitmap(width, height, 24); + wxNativePixelData pixels(*bmp, wxPoint(0,0), wxSize(width,height)); + if (! pixels) { + // raise an exception... + wxPyErr_SetString(PyExc_RuntimeError, + "Failed to gain raw access to bitmap data."); + return NULL; + } + + wxNativePixelData::Iterator p(pixels); + for (int y=0; y<height; y++) { + wxNativePixelData::Iterator rowStart = p; + for (int x=0; x<width; x++) { + p.Red() = *(data++); + p.Green() = *(data++); + p.Blue() = *(data++); + ++p; + } + p = rowStart; + p.OffsetY(pixels, 1); + } + return bmp; + } +%} + + +%pythoncode { +def BitmapFromBuffer(width, height, dataBuffer, alphaBuffer=None): + """ + Creates a `wx.Bitmap` from the data in dataBuffer. The dataBuffer + parameter must be a Python object that implements the buffer interface, or + is convertable to a buffer object, such as a string, array, etc. The + dataBuffer object is expected to contain a series of RGB bytes and be + width*height*3 bytes long. A buffer object can optionally be supplied for + the image's alpha channel data, and it is expected to be width*height + bytes long. + + Unlike `wx.ImageFromBuffer` the bitmap created with this function does not + share the memory buffer with the buffer object. This is because the + native pixel buffer format varies on different platforms, and so instead + an efficient as possible copy of the data is made from the buffer objects + to the bitmap's native pixel buffer. For direct access to a bitmap's + pixel buffer see `wx.NativePixelData` and `wx.AlphaPixelData`. + + :see: `wx.Bitmap`, `wx.BitmapFromBufferRGBA`, `wx.NativePixelData`, + `wx.AlphaPixelData`, `wx.ImageFromBuffer` + """ + if not isinstance(dataBuffer, buffer): + dataBuffer = buffer(dataBuffer) + if alphaBuffer is not None and not isinstance(alphaBuffer, buffer): + alphaBuffer = buffer(alphaBuffer) + if alphaBuffer is not None: + return _gdi_._BitmapFromBufferAlpha(width, height, dataBuffer, alphaBuffer) + else: + return _gdi_._BitmapFromBuffer(width, height, dataBuffer) +} + + + +%newobject _BitmapFromBufferRGBA; +%inline %{ + wxBitmap* _BitmapFromBufferRGBA(int width, int height, buffer data, int DATASIZE) + { + if (DATASIZE != width*height*4) { + wxPyErr_SetString(PyExc_ValueError, "Invalid data buffer size."); + return NULL; + } + + wxBitmap* bmp = new wxBitmap(width, height, 32); + wxAlphaPixelData pixels(*bmp, wxPoint(0,0), wxSize(width,height)); + if (! pixels) { + // raise an exception... + wxPyErr_SetString(PyExc_RuntimeError, + "Failed to gain raw access to bitmap data."); + return NULL; + } + + pixels.UseAlpha(); + wxAlphaPixelData::Iterator p(pixels); + for (int y=0; y<height; y++) { + wxAlphaPixelData::Iterator rowStart = p; + for (int x=0; x<width; x++) { + p.Red() = *(data++); + p.Green() = *(data++); + p.Blue() = *(data++); + p.Alpha() = *(data++); + ++p; + } + p = rowStart; + p.OffsetY(pixels, 1); + } + return bmp; + } +%} + +%pythoncode { +def BitmapFromBufferRGBA(width, height, dataBuffer): + """ + Creates a `wx.Bitmap` from the data in dataBuffer. The dataBuffer + parameter must be a Python object that implements the buffer interface, or + is convertable to a buffer object, such as a string, array, etc. The + dataBuffer object is expected to contain a series of RGBA bytes (red, + gree, blue and alpha) and be width*height*4 bytes long. + + Unlike `wx.ImageFromBuffer` the bitmap created with this function does not + share the memory buffer with the buffer object. This is because the + native pixel buffer format varies on different platforms, and so instead + an efficient as possible copy of the data is made from the buffer object + to the bitmap's native pixel buffer. For direct access to a bitmap's + pixel buffer see `wx.NativePixelData` and `wx.AlphaPixelData`. + + :see: `wx.Bitmap`, `wx.BitmapFromBuffer`, `wx.NativePixelData`, + `wx.AlphaPixelData`, `wx.ImageFromBuffer` + """ + if not isinstance(dataBuffer, buffer): + dataBuffer = buffer(dataBuffer) + return _gdi_._BitmapFromBufferRGBA(width, height, dataBuffer) +} + + //--------------------------------------------------------------------------- DocStr(wxMask, @@ -306,6 +500,8 @@ A mask may be associated with a `wx.Bitmap`. It is used in `wx.MemoryDC` with a `wx.Bitmap` selected into it that contains a mask.", ""); +MustHaveApp(wxMask); + class wxMask : public wxObject { public: @@ -327,7 +523,7 @@ passed then BLACK is used. } } - //~wxMask(); + ~wxMask(); }; %pythoncode { MaskColour = wx._deprecated(Mask, "wx.MaskColour is deprecated, use `wx.Mask` instead.") }