From a86960861fb8824b4b39aa62c04c2689a6163f61 Mon Sep 17 00:00:00 2001 From: Robin Dunn Date: Sun, 20 Aug 2006 04:19:39 +0000 Subject: [PATCH] Add ImageFromBuffer git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@40692 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- wxPython/src/_image.i | 71 ++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 66 insertions(+), 5 deletions(-) diff --git a/wxPython/src/_image.i b/wxPython/src/_image.i index cafe72b758..d87eae375b 100644 --- a/wxPython/src/_image.i +++ b/wxPython/src/_image.i @@ -268,8 +268,8 @@ public: largest and most colourful one by the ICO handler. :see: `wx.ImageFromMime`, `wx.ImageFromStream`, `wx.ImageFromStreamMime`, - `wx.EmptyImage`, `wx.ImageFromBitmap`, `wx.ImageFromData`, - `wx.ImageFromDataWithAlpha` + `wx.EmptyImage`, `wx.ImageFromBitmap`, `wx.ImageFromBuffer`, + `wx.ImageFromData`, `wx.ImageFromDataWithAlpha` "); ~wxImage(); @@ -388,11 +388,42 @@ alpha data must be width*height bytes.", " return new wxImage(width, height, dcopy, acopy, false); } + + // NOTE: We need the junk parameter so the compiler will be able + // differentiate from the two functions above. It isn't used for + // anything else. + %RenameDocCtor( + _ImageFromBuffer, ":see: `wx.ImageFromBuffer`", "", + wxImage(int width, int height, buffer data, int DATASIZE, PyObject* junk)) + { + wxUnusedVar(junk); + if (DATASIZE != width*height*3) { + wxPyErr_SetString(PyExc_ValueError, "Invalid data buffer size."); + return NULL; + } + return new wxImage(width, height, data, true); + } + + %RenameDocCtor( + _ImageFromBufferWithAlpha, ":see: `wx.ImageFromBuffer`", "", + wxImage(int width, int height, buffer data, int DATASIZE, buffer alpha, int ALPHASIZE, PyObject* junk)) + { + wxUnusedVar(junk); + 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; + } + return new wxImage(width, height, data, alpha, true); + } } // TODO: wxImage( char** xpmData ); - // Turn it back on again + // Turn the typemap back on again %typemap(out) wxImage* { $result = wxPyMake_wxObject($1, $owner); } @@ -779,7 +810,7 @@ data must be width*height.", ""); - DocStr(GetDataBuffer, + DocStr(GetAlphaBuffer, "Returns a writable Python buffer object that is pointing at the Alpha data buffer inside the wx.Image. You need to ensure that you do not use this buffer object after the image has been destroyed.", ""); @@ -793,7 +824,7 @@ use this buffer object after the image has been destroyed.", ""); } - DocStr(SetDataBuffer, + DocStr(SetAlphaBuffer, "Sets the internal image alpha pointer to point at a Python buffer object. This can save making an extra copy of the data but you must ensure that the buffer object lives as long as the wx.Image does.", ""); @@ -999,6 +1030,36 @@ range -1.0..1.0 where -1.0 is -360 degrees and 1.0 is 360 degrees", ""); }; +%pythoncode { +def ImageFromBuffer(width, height, dataBuffer, alphaBuffer=None): + """ + Creates a `wx.Image` from the data in dataBuffer. The dataBuffer + parameter must be a Python object that implements the buffer + interface, 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. + + A reference to the data and alpha buffer objects are kept with the + wx.Image, so that they won't get deleted until after the wx.Image + is deleted. However please be aware that it is not guaranteed that + an object won't move its memory buffer to a new location when it + needs to resize its contents. If that happens then the wx.Image + will end up referring to an invalid memory location and could cause + the application to crash. Therefore care should be taken to not + manipulate the objects used for the data and alpha buffers in a + way that would cause them to change size. + """ + if alphaBuffer is not None: + image = _ImageFromBufferWithAlpha(width, height, dataBuffer, alphaBuffer, None) + else: + image = _ImageFromBuffer(width, height, dataBuffer, None) + image._buffer = dataBuffer + image._alpha = alphaBuffer + return image +} + ///void wxInitAllImageHandlers(); -- 2.47.2