X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/97c7d01d120b46a97a677edff10d497823828a88..5f2502393e042e6e2c871ff94b7ccaf7a78b264d:/wxPython/src/_bitmap.i diff --git a/wxPython/src/_bitmap.i b/wxPython/src/_bitmap.i index f6ee5a0c8d..f98e9daa7c 100644 --- a/wxPython/src/_bitmap.i +++ b/wxPython/src/_bitmap.i @@ -17,7 +17,8 @@ %} -// Turn off the aquisition of the Global Interpreter Lock for this file +// Turn off the aquisition of the Global Interpreter Lock for the classes and +// functions in this file %threadWrapperOff //--------------------------------------------------------------------------- @@ -108,8 +109,8 @@ public: * wx.BITMAP_TYPE_IFF :see: Alternate constructors `wx.EmptyBitmap`, `wx.BitmapFromIcon`, - `wx.BitmapFromImage`, `wx.BitmapFromXPMData`, - `wx.BitmapFromBits` + `wx.BitmapFromImage`, `wx.BitmapFromXPMData`, `wx.BitmapFromBits`, + `wx.BitmapFromBuffer`, `wx.BitmapFromBufferRGBA`, "); ~wxBitmap(); @@ -178,8 +179,9 @@ bit depths, the behaviour is platform dependent.", "", } #endif - bool Ok(); - + bool IsOk(); + %pythoncode { Ok = IsOk } + DocDeclStr( int , GetWidth(), "Gets the width of the bitmap in pixels.", ""); @@ -213,7 +215,6 @@ 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 @@ -307,12 +308,21 @@ the ``type`` parameter.", ""); #endif #endif - %pythoncode { def __nonzero__(self): return self.Ok() } + %pythoncode { def __nonzero__(self): return self.IsOk() } %extend { bool __eq__(const wxBitmap* other) { return other ? (*self == *other) : false; } bool __ne__(const wxBitmap* other) { return other ? (*self != *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`"); + }; @@ -326,8 +336,8 @@ the ``type`` parameter.", ""); // appears to me that the other platforms are already doing it, so I'll just // automatically do it for wxMSW here. #ifdef __WXMSW__ -#define wxPy_premultiply(p, a) ((p) * (a) / 256) -#define wxPy_unpremultiply(p, a) ((a) ? ((p) * 256 / (a)) : (p)) +#define wxPy_premultiply(p, a) ((p) * (a) / 0xff) +#define wxPy_unpremultiply(p, a) ((a) ? ((p) * 0xff / (a)) : (p)) #else #define wxPy_premultiply(p, a) (p) #define wxPy_unpremultiply(p, a) (p) @@ -416,29 +426,26 @@ the ``type`` parameter.", ""); 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. On Windows the RGB values are 'premultiplied' by the alpha - values. (The other platforms appear to already be premultiplying the - alpha.) - - 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`. + 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 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: @@ -488,25 +495,24 @@ def BitmapFromBuffer(width, height, dataBuffer, alphaBuffer=None): 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, - 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 - appear to already be premultiplying the alpha.) - - 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`. + 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` """ - if not isinstance(dataBuffer, buffer): - dataBuffer = buffer(dataBuffer) return _gdi_._BitmapFromBufferRGBA(width, height, dataBuffer) } @@ -528,6 +534,11 @@ public: // 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`"); }; @@ -559,53 +570,42 @@ public: %pythoncode { def __iter__(self): - """Create and return an iterator object for this pixel data object.""" - return self.PixelIterator(self) - - class PixelIterator(object): """ - Sequential iterator which returns pixel accessor objects starting at the - the top-left corner, and going row-by-row until the bottom-right - corner is reached. + 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.) """ - - class PixelAccessor(object): - """ - This class is what is returned by the iterator and allows the pixel - to be Get or Set. - """ - def __init__(self, data, pixels, x, y): - self.data = data - self.pixels = pixels - self.x = x - self.y = y - def Set(self, *args, **kw): - self.pixels.MoveTo(self.data, self.x, self.y) - return self.pixels.Set(*args, **kw) + 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): - self.pixels.MoveTo(self.data, self.x, self.y) - return self.pixels.Get() - - def __init__(self, pixelData): - self.x = self.y = 0 - self.w = pixelData.GetWidth() - self.h = pixelData.GetHeight() - self.data = pixelData - self.pixels = pixelData.GetPixels() - - def __iter__(self): - return self - - def next(self): - if self.y >= self.h: - raise StopIteration - p = self.PixelAccessor(self.data, self.pixels, self.x, self.y) - self.x += 1 - if self.x >= self.w: - self.x = 0 - self.y += 1 - return p + 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`"); }; @@ -634,10 +634,12 @@ public: 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. Instead I've added the Set and Get -// functions and put the puemultiplying in there. +// 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(); } @@ -745,7 +747,7 @@ passed then BLACK is used. %extend { wxMask(const wxBitmap& bitmap, const wxColour& colour = wxNullColour) { - if ( !colour.Ok() ) + if ( !colour.IsOk() ) return new wxMask(bitmap, *wxBLACK); else return new wxMask(bitmap, colour); @@ -759,4 +761,6 @@ passed then BLACK is used. //--------------------------------------------------------------------------- //--------------------------------------------------------------------------- + +// Turn GIL acquisition back on. %threadWrapperOn