[wxPython-users] Re: Improve performance
Robin Dunn
robin at alldunn.com
Mon Aug 28 14:03:29 PDT 2006
Christopher Barker wrote:
> This is what I'd like to see, and may even write the code for some day:
>
> numpy provides a data type for a homogeneous array of numbers. wxPython
> can be taught to understand the numpy array protocol. In that case, the
> type checking would only have to happen once, for all the points. If
> we're lucky, DrawPolygon could take a standard C array, and the pointer
> to the data could be passed through without even a copy. This could be a
> lot faster.
There is an overloaded method that can take a C array of wxPoints. That
class is designed to have the same memory layout as the Windows API
POINT, which is a simple struct of two integers, so I expect that we
could force it to take a 2 dimensional array of integers and be ok.
>
> Of course, this would require numpy, but it's the way to go for this
> kind of thing anyway.
>
> The code to be messed with is:
>
> wxPointListHelper (or something like that)
>
> the array protocol:
>
> http://numpy.scipy.org/array_interface.shtml
Will this protocol be in numpy 1.0? Is it in numarray?
>
> It would be great to use this interface for creating wxImages and the
> like as well.
You may want to take a look at my CVS commits for the last couple weeks.
I've now got some raw bitmap access code in place. Both 2.6 and 2.7
will have wx.BitmapFromBuffer and wx.BitmapFromBufferRGBA factory
functions which can copy from a buffer object directly into the bitmap's
pixel buffer, and 2.7 will also have wx.NativePixelData and
wx.AlphaPixelData which allow direct access to the pixel buffer from
Python. (The latter needed a bug fix that I'm not sure (yet) can be
backported to 2.6...) For example, I can now do this (in a PyShell):
>>> import wx
>>> import numarray
>>> f = wx.Frame(None)
>>> p = wx.Panel(f)
>>> dc = wx.ClientDC(p)
>>> f.Show()
>>> dim=100
>>> R=0; G=1; B=2; A=3
>>> arr = numarray.array(shape=(dim, dim, 4), typecode='u1')
>>> for row in xrange(dim):
... for col in xrange(dim):
... arr[row,col,R] = 0
... arr[row,col,G] = 0
... arr[row,col,B] = 255
... arr[row,col,A] = int(col * 255.0 / dim)
...
>>> bmp = wx.BitmapFromBufferRGBA(dim, dim, arr)
>>> dc.DrawBitmap(bmp, 20, 20, True)
BTW, I seem to recall you mentioning that there was some doubt whether
the buffer protocol would be included in numpy, is that still true?
--
Robin Dunn
Software Craftsman
http://wxPython.org Java give you jitters? Relax with wxPython!
More information about the wxpython-users
mailing list