I moved this to the devel list -- that seemed more appropriate.
Stefan van der Walt wrote:
Another (or additional) option is for both MPL and wx to support
the new array interface protocol in numpy.
Travis posted a patch to PIL for support a while back, I don't know
if it's going to get applied or not, but it's worth looking at.
Looks like it has been added already.
Very cool. Now we just need MPL and wx to support it...
In the meantime, this note from Robin Dunn on the wxpython-users list:
wx.Image.SetData makes a copy and gives ownership of the copy to the
image. This allows it to work even when the source buffer doesn't
live as long as the image does.
This is probably how the original wx back-end worked.
There is also the
wx.Image.SetDataBuffer method, which will have the wxImage use the
buffer passed in without making a copy, but if the buffer doesn't
live as long as the image does it will likely cause a crash.
This would probably be the easiest way to get top performance at the moment. If we can make the Agg data a buffer, and somehow do the reference counting right so it doesn't get deleted while the wxImage is still around (that may not be hard -- the wxImage has to be converted to a wxBitmap to be drawn anyway)
Another issue is the data layout of the Agg buffer: is it RGB or RGBA (or something else?) wxImage requires 24bit RGB. It can support an alpha channel, but as far as I can tell, the alpha data is stored in a separate array, rather than as an RGBA array.
I'm poking into the code a bit now, and see:
def _py_convert_agg_to_wx_image(agg, bbox):
image = wx.EmptyImage(int(agg.width), int(agg.height))
We might be able to just change that to:
Does the agg.tostring_rgb() call make a copy?
If so, then maybe a agg.Getbuffer() method is in order.
Also, if it does, then that string will get garbage collected right away, and delete the memory buffer. We'd need to keep that around somehow. maybe a subclass of wx.Image that holds a reference to the string. Then they'll both get deleted when the wx.Image is deleted..
I also took a look at:
This is way too ugly!
1) maybe it would be a good idea to have a:
This must be used in all the back-ends
2) If you do need to do the sub-sampling with wx code, I think you could do:
def _clipped_image_as_bitmap(image, bbox):
Convert the region of a wx.Image described by bbox to a wx.Bitmap.
l, b, width, height = bbox.get_bounds()
r = l + width
t = b + height
subImage = image.GetSubImage( wx.Rect( (l,b),(width,height) ) )
destBmp = wx.BitmapFromImage(subImage)
Much simpler (and probably faster)
3) Another option would be to use numpy arrays to pass this kind of data around. We could then use numpy to sub-sample the image. MPL depends on numpy anyway, so it's not an added dependency. That may have to wait until the 'grand unification" is complete, and we can drop support for Numeric and numarray.
Sorry I don't have time to build and test this code right now.
On Mon, Aug 14, 2006 at 11:12:29AM -0700, Christopher Barker wrote:
Christopher Barker, Ph.D.
NOAA/OR&R/HAZMAT (206) 526-6959 voice
7600 Sand Point Way NE (206) 526-6329 fax
Seattle, WA 98115 (206) 526-6317 main reception