+//---------------------------------------------------------------------------
+
+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`");
+};
+
+
+// Both wxNativePixelData and wxAlphaPixelData have the same interface, so
+// make a macro to declare them both.
+
+%define PIXELDATA(PixelData)
+%{
+ typedef PixelData##::Iterator PixelData##_Accessor;
+%}
+class PixelData##_Accessor;
+class PixelData : public wxPixelDataBase
+{
+public:
+ %nokwargs PixelData;
+
+ PixelData(wxBitmap& bmp);
+ PixelData(wxBitmap& bmp, const wxRect& rect);
+ PixelData(wxBitmap& bmp, const wxPoint& pt, const wxSize& sz);
+
+ ~PixelData();
+
+ PixelData##_Accessor GetPixels() const;
+ void UseAlpha();
+
+ %extend {
+ bool __nonzero__() { return self->operator bool(); }
+ }
+
+ %pythoncode {
+ def __iter__(self):
+ """
+ Create and return an iterator object for this pixel data
+ object. (It's really a generator but I won't tell if you
+ don't tell.)
+ """
+ width = self.GetWidth()
+ height = self.GetHeight()
+ pixels = self.GetPixels()
+
+ # This class is a facade over the pixels object (using the one
+ # in the enclosing scope) that only allows Get() and Set() to
+ # be called.
+ class PixelFacade(object):
+ def Get(self):
+ return pixels.Get()
+ def Set(self, *args, **kw):
+ return pixels.Set(*args, **kw)
+ def __str__(self):
+ return str(self.Get())
+ def __repr__(self):
+ return 'pixel(%d,%d): %s' % (x,y,self.Get())
+ X = property(lambda self: x)
+ Y = property(lambda self: y)
+
+ pf = PixelFacade()
+ for y in xrange(height):
+ for x in xrange(width):
+ # We always generate the same pf instance, but it
+ # accesses the pixels object which we use to iterate
+ # over the pixel buffer.
+ yield pf
+ pixels.nextPixel()
+ pixels.MoveTo(self, 0, y)
+ }
+
+ %property(Pixels, GetPixels, doc="See `GetPixels`");
+};
+
+
+
+class PixelData##_Accessor
+{
+public:
+ %nokwargs PixelData##_Accessor;
+
+ PixelData##_Accessor(PixelData& data);
+ PixelData##_Accessor(wxBitmap& bmp, PixelData& data);
+ PixelData##_Accessor();
+
+ ~PixelData##_Accessor();
+
+ void Reset(const PixelData& data);
+ bool IsOk() const;
+
+ %extend {
+ // PixelData##_Accessor& nextPixel() { return ++(*self); }
+ void nextPixel() { ++(*self); }
+ }
+
+ void Offset(const PixelData& data, int x, int y);
+ void OffsetX(const PixelData& data, int x);
+ void OffsetY(const PixelData& data, int y);
+ void MoveTo(const PixelData& data, int x, int y);
+
+// NOTE: For now I'm not wrapping the Red, Green, Blue and Alpha
+// functions because I can't hide the premultiplying needed on wxMSW
+// if only the individual components are wrapped, plus it would mean 3
+// or 4 trips per pixel from Python to C++ instead of just one.
+// Instead I've added the Set and Get functions and put the
+// premultiplying in there.
+
+// %extend {
+// byte _get_Red() { return self->Red(); }
+// byte _get_Green() { return self->Green(); }
+// byte _get_Blue() { return self->Blue(); }
+
+// void _set_Red(byte val) { self->Red() = val; }
+// void _set_Green(byte val) { self->Green() = val; }
+// void _set_Blue(byte val) { self->Blue() = val; }
+// }
+
+// %pythoncode {
+// Red = property(_get_Red, _set_Red)
+// Green = property(_get_Green, _set_Green)
+// Blue = property(_get_Blue, _set_Blue)
+// }
+};
+%enddef
+
+
+%pythonAppend wxAlphaPixelData::wxAlphaPixelData "self.UseAlpha()"
+
+// Make the classes
+PIXELDATA(wxNativePixelData)
+PIXELDATA(wxAlphaPixelData)
+
+
+// Add in a few things that are different between the wxNativePixelData and
+// wxAlphaPixelData and the iterator classes and so are not included in our
+// macro...
+
+%extend wxNativePixelData_Accessor {
+ void Set(byte red, byte green, byte blue) {
+ self->Red() = red;
+ self->Green() = green;
+ self->Blue() = blue;
+ }
+
+ PyObject* Get() {
+ PyObject* rv = PyTuple_New(3);
+ PyTuple_SetItem(rv, 0, PyInt_FromLong(self->Red()));
+ PyTuple_SetItem(rv, 1, PyInt_FromLong(self->Green()));
+ PyTuple_SetItem(rv, 2, PyInt_FromLong(self->Blue()));
+ return rv;
+ }
+}
+
+%extend wxAlphaPixelData_Accessor {
+// byte _get_Alpha() { return self->Alpha(); }
+// void _set_Alpha(byte val) { self->Alpha() = val; }
+
+// %pythoncode {
+// Alpha = property(_get_Alpha, _set_Alpha)
+// }
+
+ void Set(byte red, byte green, byte blue, byte alpha) {
+ self->Red() = wxPy_premultiply(red, alpha);
+ self->Green() = wxPy_premultiply(green, alpha);
+ self->Blue() = wxPy_premultiply(blue, alpha);
+ self->Alpha() = alpha;
+ }
+
+ PyObject* Get() {
+ PyObject* rv = PyTuple_New(4);
+ int red = self->Red();
+ int green = self->Green();
+ int blue = self->Blue();
+ int alpha = self->Alpha();
+
+ PyTuple_SetItem(rv, 0, PyInt_FromLong( wxPy_unpremultiply(red, alpha) ));
+ PyTuple_SetItem(rv, 1, PyInt_FromLong( wxPy_unpremultiply(green, alpha) ));
+ PyTuple_SetItem(rv, 2, PyInt_FromLong( wxPy_unpremultiply(blue, alpha) ));
+ PyTuple_SetItem(rv, 3, PyInt_FromLong( alpha ));
+ return rv;
+ }
+}
+
+