//bool LoadFile(wxImage* image, wxInputStream& stream);
//bool SaveFile(wxImage* image, wxOutputStream& stream);
//virtual int GetImageCount( wxInputStream& stream );
- //bool CanRead( wxInputStream& stream );
bool CanRead( const wxString& name );
-
+ %Rename(CanReadStream, bool, CanRead( wxInputStream& stream ));
+
void SetName(const wxString& name);
void SetExtension(const wxString& extension);
void SetType(long type);
//---------------------------------------------------------------------------
-%{
- typedef unsigned char* buffer;
-%}
-
-%typemap(in) (buffer data, int DATASIZE)
- { if (!PyArg_Parse($input, "t#", &$1, &$2)) SWIG_fail; }
-
-%typemap(in) (buffer alpha, int ALPHASIZE)
- { if (!PyArg_Parse($input, "t#", &$1, &$2)) SWIG_fail; }
-
-//---------------------------------------------------------------------------
-
-
DocStr(wxImage,
"A platform-independent image class. An image can be created from
data, or using `wx.Bitmap.ConvertToImage`, or loaded from a file in a
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();
// TODO: wxImage( char** xpmData );
- // Turn it back on again
+ // Turn the typemap back on again
%typemap(out) wxImage* { $result = wxPyMake_wxObject($1, $owner); }
- 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.", "");
}
- 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.", "");
+// Make an image from buffer objects. Not that this is here instead of in the
+// wxImage class (as a constructor) because there is already another one with
+// the exact same signature, so there woudl be ambiguities in the generated
+// C++. Doing it as an independent factory function like this accomplishes
+// the same thing however.
+%newobject _ImageFromBuffer;
+%inline %{
+ wxImage* _ImageFromBuffer(int width, int height,
+ buffer data, int DATASIZE,
+ buffer alpha=NULL, int ALPHASIZE=0)
+ {
+ if (DATASIZE != width*height*3) {
+ wxPyErr_SetString(PyExc_ValueError, "Invalid data buffer size.");
+ return NULL;
+ }
+ if (alpha != NULL) {
+ if (ALPHASIZE != width*height) {
+ wxPyErr_SetString(PyExc_ValueError, "Invalid alpha buffer size.");
+ return NULL;
+ }
+ return new wxImage(width, height, data, alpha, true);
+ }
+ return new wxImage(width, height, data, true);
+ }
+%}
+
+%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, 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.
+
+ The wx.Image will be created with its data and alpha pointers initialized
+ to the memory address pointed to by the buffer objects, thus saving the
+ time needed to copy the image data from the buffer object to the wx.Image.
+ While this has advantages, it also has the shoot-yourself-in-the-foot
+ risks associated with sharing a C pointer between two objects.
+
+ To help alleviate the risk 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 not isinstance(dataBuffer, buffer):
+ dataBuffer = buffer(dataBuffer)
+ if alphaBuffer is not None and not isinstance(alphaBuffer, buffer):
+ alphaBuffer = buffer(alphaBuffer)
+ image = _core_._ImageFromBuffer(width, height, dataBuffer, alphaBuffer)
+ image._buffer = dataBuffer
+ image._alpha = alphaBuffer
+ return image
+}
+
+
///void wxInitAllImageHandlers();
%pythoncode {
-// See also wxPy_ReinitStockObjects in helpers.cpp
%immutable;
const wxImage wxNullImage;
%mutable;