+ %extend {
+ DocStr(CopyFromBuffer,
+ "Copy data from a RGB buffer object to replace the bitmap pixel data.
+See `wxBitmapFromBuffer` for more details.", "");
+ void CopyFromBuffer(buffer data, int DATASIZE)
+ {
+ int height=self->GetHeight();
+ int width=self->GetWidth();
+
+ if (DATASIZE != width * height * 3) {
+ wxPyErr_SetString(PyExc_ValueError, "Invalid data buffer size.");
+ }
+ wxNativePixelData pixData(*self, wxPoint(0,0), wxSize(width, height));
+ if (! pixData) {
+ // raise an exception...
+ wxPyErr_SetString(PyExc_RuntimeError,
+ "Failed to gain raw access to bitmap data.");
+ return;
+ }
+
+ wxNativePixelData::Iterator p(pixData);
+ 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(pixData, 1);
+ }
+ }
+
+ DocStr(CopyFromBufferRGBA,
+ "Copy data from a RGBA buffer object to replace the bitmap pixel data.
+See `wxBitmapFromBufferRGBA` for more details.", "");
+ void CopyFromBufferRGBA(buffer data, int DATASIZE)
+ {
+ int height=self->GetHeight();
+ int width=self->GetWidth();
+
+ if (DATASIZE != width * height * 4) {
+ wxPyErr_SetString(PyExc_ValueError, "Invalid data buffer size.");
+ }
+ wxAlphaPixelData pixData(*self, wxPoint(0,0), wxSize(width, height));
+ if (! pixData) {
+ // raise an exception...
+ wxPyErr_SetString(PyExc_RuntimeError,
+ "Failed to gain raw access to bitmap data.");
+ return;
+ }
+
+ pixData.UseAlpha();
+ wxAlphaPixelData::Iterator p(pixData);
+ for (int y=0; y<height; y++) {
+ wxAlphaPixelData::Iterator rowStart = p;
+ for (int x=0; x<width; x++) {
+ byte a = data[3];
+ p.Red() = wxPy_premultiply(*(data++), a);
+ p.Green() = wxPy_premultiply(*(data++), a);
+ p.Blue() = wxPy_premultiply(*(data++), a);
+ p.Alpha() = a; data++;
+ ++p;
+ }
+ p = rowStart;
+ p.OffsetY(pixData, 1);
+ }
+ }
+ }
+
+
+ %pythoncode { def __nonzero__(self): return self.IsOk() }
+
+ // TODO: Should these just be removed since the C++ operators are
+ // gone? Or is using IsSameAs for wxPython ok?
+ %extend {
+ bool __eq__(const wxBitmap* other) { return other ? self->IsSameAs(*other) : false; }
+ bool __ne__(const wxBitmap* other) { return other ? !self->IsSameAs(*other) : true; }
+ }
+
+ %property(Depth, GetDepth, SetDepth, doc="See `GetDepth` and `SetDepth`");
+ %property(Height, GetHeight, SetHeight, doc="See `GetHeight` and `SetHeight`");
+ %property(Mask, GetMask, SetMask, doc="See `GetMask` and `SetMask`");
+ %property(Palette, GetPalette, doc="See `GetPalette`");
+ %property(Size, GetSize, SetSize, doc="See `GetSize` and `SetSize`");
+ %property(SubBitmap, GetSubBitmap, doc="See `GetSubBitmap`");
+ %property(Width, GetWidth, SetWidth, doc="See `GetWidth` and `SetWidth`");
+
+};
+
+
+//---------------------------------------------------------------------------
+// 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 pixData(*bmp, wxPoint(0,0), wxSize(width,height));
+ if (! pixData) {
+ // raise an exception...
+ wxPyErr_SetString(PyExc_RuntimeError,
+ "Failed to gain raw access to bitmap data.");
+ return NULL;
+ }
+
+ pixData.UseAlpha();
+ wxAlphaPixelData::Iterator p(pixData);
+ for (int y=0; y<height; y++) {
+ wxAlphaPixelData::Iterator rowStart = p;
+ for (int x=0; x<width; x++) {
+ byte a = *(alpha++);
+ p.Red() = wxPy_premultiply(*(data++), a);
+ p.Green() = wxPy_premultiply(*(data++), a);
+ p.Blue() = wxPy_premultiply(*(data++), a);
+ p.Alpha() = a;
+ ++p;
+ }
+ p = rowStart;
+ p.OffsetY(pixData, 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 pixData(*bmp, wxPoint(0,0), wxSize(width,height));
+ if (! pixData) {
+ // raise an exception...
+ wxPyErr_SetString(PyExc_RuntimeError,
+ "Failed to gain raw access to bitmap data.");
+ return NULL;
+ }
+
+ wxNativePixelData::Iterator p(pixData);
+ 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(pixData, 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, 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. On Windows the RGB values are 'premultiplied' by the
+ alpha values. (The other platforms do the multiplication
+ themselves.)
+
+ 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 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 pixData(*bmp, wxPoint(0,0), wxSize(width,height));
+ if (! pixData) {
+ // raise an exception...
+ wxPyErr_SetString(PyExc_RuntimeError,
+ "Failed to gain raw access to bitmap data.");
+ return NULL;
+ }
+
+ pixData.UseAlpha();
+ wxAlphaPixelData::Iterator p(pixData);
+ for (int y=0; y<height; y++) {
+ wxAlphaPixelData::Iterator rowStart = p;
+ for (int x=0; x<width; x++) {
+ byte a = data[3];
+ p.Red() = wxPy_premultiply(*(data++), a);
+ p.Green() = wxPy_premultiply(*(data++), a);
+ p.Blue() = wxPy_premultiply(*(data++), a);
+ p.Alpha() = a; data++;
+ ++p;
+ }
+ p = rowStart;
+ p.OffsetY(pixData, 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, such as a string, array, etc. The dataBuffer object is
+ expected to contain a series of RGBA bytes (red, green, blue and
+ alpha) and be width*height*4 bytes long. On Windows the RGB
+ values are 'premultiplied' by the alpha values. (The other
+ platforms do the multiplication themselves.)
+
+ 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`
+ """
+ return _gdi_._BitmapFromBufferRGBA(width, height, dataBuffer)
+}
+
+
+//---------------------------------------------------------------------------
+
+class wxPixelDataBase
+{
+public:
+ // origin of the rectangular region we represent
+ wxPoint GetOrigin() const { return m_ptOrigin; }
+
+ // width and height of the region we represent
+ int GetWidth() const { return m_width; }
+ int GetHeight() const { return m_height; }
+
+ wxSize GetSize() const { return wxSize(m_width, m_height); }
+
+ // the distance between two rows
+ int GetRowStride() const { return m_stride; }
+
+ %property(Height, GetHeight, doc="See `GetHeight`");
+ %property(Origin, GetOrigin, doc="See `GetOrigin`");
+ %property(RowStride, GetRowStride, doc="See `GetRowStride`");
+ %property(Size, GetSize, doc="See `GetSize`");
+ %property(Width, GetWidth, doc="See `GetWidth`");