Fixed premultiplying constant

Replaced my kludgy first attempt at a pixel iterator with a much more
efficient generator in PixelData.__init__.


git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@40920 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
Robin Dunn
2006-08-30 00:18:13 +00:00
parent 198d7c6cb9
commit 9bcdb6310e

View File

@@ -325,8 +325,8 @@ the ``type`` parameter.", "");
// appears to me that the other platforms are already doing it, so I'll just // appears to me that the other platforms are already doing it, so I'll just
// automatically do it for wxMSW here. // automatically do it for wxMSW here.
#ifdef __WXMSW__ #ifdef __WXMSW__
#define wxPy_premultiply(p, a) ((p) * (a) / 256) #define wxPy_premultiply(p, a) ((p) * (a) / 0xff)
#define wxPy_unpremultiply(p, a) ((a) ? ((p) * 256 / (a)) : (p)) #define wxPy_unpremultiply(p, a) ((a) ? ((p) * 0xff / (a)) : (p))
#else #else
#define wxPy_premultiply(p, a) (p) #define wxPy_premultiply(p, a) (p)
#define wxPy_unpremultiply(p, a) (p) #define wxPy_unpremultiply(p, a) (p)
@@ -554,52 +554,39 @@ public:
%pythoncode { %pythoncode {
def __iter__(self): 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 Create and return an iterator object for this pixel data
the top-left corner, and going row-by-row until the bottom-right object. (It's really a generator but I won't tell if you
corner is reached. don't tell.)
""" """
width = self.GetWidth()
class PixelAccessor(object): height = self.GetHeight()
""" pixels = self.GetPixels()
This class is what is returned by the iterator and allows the pixel
to be Get or Set. # This class is a facade over the pixels object (using the one
""" # in the enclosing scope) that only allows Get() and Set() to
def __init__(self, data, pixels, x, y): # be called.
self.data = data class PixelFacade(object):
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)
def Get(self): def Get(self):
self.pixels.MoveTo(self.data, self.x, self.y) return pixels.Get()
return self.pixels.Get() def Set(self, *args, **kw):
return pixels.Set(*args, **kw)
def __init__(self, pixelData): def __str__(self):
self.x = self.y = 0 return str(self.Get())
self.w = pixelData.GetWidth() def __repr__(self):
self.h = pixelData.GetHeight() return 'pixel(%d,%d): %s' % (x,y,self.Get())
self.data = pixelData X = property(lambda self: x)
self.pixels = pixelData.GetPixels() Y = property(lambda self: y)
def __iter__(self): pf = PixelFacade()
return self for y in xrange(height):
for x in xrange(width):
def next(self): # We always generate the same pf instance, but it
if self.y >= self.h: # accesses the pixels object which we use to iterate
raise StopIteration # over the pixel buffer.
p = self.PixelAccessor(self.data, self.pixels, self.x, self.y) yield pf
self.x += 1 pixels.nextPixel()
if self.x >= self.w: pixels.MoveTo(self, 0, y)
self.x = 0
self.y += 1
return p
} }
}; };
@@ -629,10 +616,12 @@ public:
void OffsetY(const PixelData& data, int y); void OffsetY(const PixelData& data, int y);
void MoveTo(const PixelData& data, int x, 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 // NOTE: For now I'm not wrapping the Red, Green, Blue and Alpha
// because I can't hide the premultiplying needed on wxMSW if only the // functions because I can't hide the premultiplying needed on wxMSW
// individual components are wrapped. Instead I've added the Set and Get // if only the individual components are wrapped, plus it would mean 3
// functions and put the premultiplying in there. // 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 { // %extend {
// byte _get_Red() { return self->Red(); } // byte _get_Red() { return self->Red(); }