10000 The WXAgg C++ accelerator now uses the correct wxBitmap constructor. · matplotlib/matplotlib@744b7f8 · GitHub
[go: up one dir, main page]

Skip to content

Commit 744b7f8

Browse files
author
Ken McIvor
committed
The WXAgg C++ accelerator now uses the correct wxBitmap constructor.
Under wxPython 2.8, the accelerator has been replaced with a new, pure Python implementation of the agg-to-wx.Bitmap conversion routines. Very minor refactoring in animation_blit_wx.py svn path=/trunk/matplotlib/; revision=3028
1 parent d750c95 commit 744b7f8

File tree

6 files changed

+118
-24
lines changed

6 files changed

+118
-24
lines changed

CHANGELOG

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,18 @@
1+
2007-02-22 WXAgg accelerator updates - KM
2+
WXAgg's C++ accelerator has been fixed to use the correct wxBitmap
3+
constructor.
4+
5+
The backend has been updated to use new wxPython functionality to
6+
provide fast blit() animation without the C++ accelerator. This
7+
requires wxPython 2.8 or later. Previous versions of wxPython can
8+
use the C++ acclerator or the old pure Python routines.
9+
10+
setup.py no longer builds the C++ accelerator when wxPython >= 2.8
11+
is present.
12+
13+
The blit() method is now faster regardless of which agg/wxPython
14+
conversion routines are used.
15+
116
2007-02-21 Applied the PDF backend patch by Nicolas Grilly.
217
This impacts several files and directories in matplotlib:
318

examples/animation_blit_wx.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
# For detailed comments on animation and the techniqes used here, see
22
# the wiki entry
33
# http://www.scipy.org/wikis/topical_software/MatplotlibAnimation
4+
5+
# The number of blits() to make before exiting
6+
NBLITS = 1000
7+
48
import matplotlib
59
matplotlib.use('WXAgg')
610
matplotlib.rcParams['toolbar'] = None
@@ -51,14 +55,14 @@ def update_line(*args):
5155
canvas.blit(ax.bbox)
5256
blit_time += time.time() - t
5357

54-
if update_line.cnt==1000:
58+
if update_line.cnt == NBLITS:
5559
# print the timing info and quit
5660
frame_time = time.time() - tstart
57-
print '200 frames: %.2f seconds' % frame_time
58-
print '200 blits: %.2f seconds' % blit_time
61+
print '%d frames: %.2f seconds' % (NBLITS, frame_time)
62+
print '%d blits: %.2f seconds' % (NBLITS, blit_time)
5963
print
60-
print 'FPS: %.2f' % (1000/frame_time)
61-
print 'BPS: %.2f' % (1000/blit_time)
64+
print 'FPS: %.2f' % (NBLITS/frame_time)
65+
print 'BPS: %.2f' % (NBLITS/blit_time)
6266
sys.exit()
6367

6468
update_line.cnt += 1

lib/matplotlib/backends/backend_wxagg.py

Lines changed: 85 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -80,15 +80,15 @@ def blit(self, bbox=None):
8080
x = int(l)
8181
y = int(self.bitmap.GetHeight() - t)
8282

83-
srcBmp = _convert_agg_to_wx_bitmap(self.get_renderer(), bbox)
83+
srcBmp = _convert_agg_to_wx_bitmap(self.get_renderer(), None)
8484
srcDC = wx.MemoryDC()
8585
srcDC.SelectObject(srcBmp)
8686

8787
destDC = wx.MemoryDC()
8888
destDC.SelectObject(self.bitmap)
8989

9090
destDC.BeginDrawing()
91-
destDC.Blit(x, y, int(w), int(h), srcDC, 0, 0)
91+
destDC.Blit(x, y, int(w), int(h), srcDC, x, y)
9292
destDC.EndDrawing()
9393

9494
destDC.SelectObject(wx.NullBitmap)
@@ -145,7 +145,7 @@ def new_figure_manager(num, *args, **kwargs):
145145

146146

147147
#
148-
# agg/wxPython image conversion functions
148+
# agg/wxPython image conversion functions (wxPython <= 2.6)
149149
#
150150

151151
def _py_convert_agg_to_wx_image(agg, bbox):
@@ -185,7 +185,7 @@ def _py_convert_agg_to_wx_bitmap(agg, bbox):
185185

186186
def _clipped_image_as_bitmap(image, bbox):
187187
"""
188-
Convert the region of a wx.Image described by bbox to a wx.Bitmap.
188+
Convert the region of a wx.Image bounded by bbox to a wx.Bitmap.
189189
"""
190190
l, b, width, height = bbox.get_bounds()
191191
r = l + width
@@ -211,19 +211,94 @@ def _clipped_image_as_bitmap(image, bbox):
211211
return destBmp
212212

213213

214+
#
215+
# agg/wxPython image conversion functions (wxPython >= 2.8)
216+
#
217+
218+
def _py_WX28_convert_agg_to_wx_image(agg, bbox):
219+
"""
220+
Convert the region of the agg buffer bounded by bbox to a wx.Image. If
221+
bbox is None, the entire buffer is converted.
222+
223+
Note: agg must be a backend_agg.RendererAgg instance.
224+
"""
225+
if bbox is None:
226+
# agg => rgb -> image
227+
image = wx.EmptyImage(int(agg.width), int(agg.height))
228+
image.SetData(agg.tostring_rgb())
229+
return image
230+
else:
231+
# agg => rgba buffer -> bitmap => clipped bitmap => image
232+
return wx.ImageFromBitmap(_WX28_clipped_agg_as_bitmap(agg, bbox))
233+
234+
235+
def _py_WX28_convert_agg_to_wx_bitmap(agg, bbox):
236+
"""
237+
Convert the region of the agg buffer bounded by bbox to a wx.Bitmap. If
238+
bbox is None, the entire buffer is converted.
239+
240+
Note: agg must be a backend_agg.RendererAgg instance.
241+
"""
242+
if bbox is None:
243+
# agg => rgba buffer -> bitmap
244+
return wx.BitmapFromBufferRGBA(int(agg.width), int(agg.height),
245+
agg.buffer_rgba(0, 0))
246+
else:
247+
# agg => rgba buffer -> bitmap => clipped bitmap
248+
return _WX28_clipped_agg_as_bitmap(agg, bbox)
249+
250+
251+
def _WX28_clipped_agg_as_bitmap(agg, bbox):
252+
"""
253+
Convert the region of a the agg buffer bounded by bbox to a wx.Bitmap.
254+
255+
Note: agg must be a backend_agg.RendererAgg instance.
256+
"""
257+
l, b, width, height = bbox.get_bounds()
258+
r = l + width
259+
t = b + height
260+
261+
srcBmp = wx.BitmapFromBufferRGBA(int(agg.width), int(agg.height),
262+
agg.buffer_rgba(0, 0))
263+
srcDC = wx.MemoryDC()
264+
srcDC.SelectObject(srcBmp)
265+
266+
destBmp = wx.EmptyBitmap(int(width), int(height))
267+
destDC = wx.MemoryDC()
268+
destDC.SelectObject(destBmp)
269+
270+
destDC.BeginDrawing()
271+
x = int(l)
272+
y = int(int(agg.height) - t)
273+
destDC.Blit(0, 0, int(width), int(height), srcDC, x, y)
274+
destDC.EndDrawing()
275+
276+
srcDC.SelectObject(wx.NullBitmap)
277+
destDC.SelectObject(wx.NullBitmap)
278+
279+
return destBmp
280+
281+
214282
def _use_accelerator(state):
215283
"""
216-
Enable or disable the WXAgg accelerator, if it is present.
284+
Enable or disable the WXAgg accelerator, if it is present and is also
285+
compatible with whatever version of wxPython is in use.
217286
"""
218287
global _convert_agg_to_wx_image
219288
global _convert_agg_to_wx_bitmap
220289

221-
if state and _wxagg is not None:
222-
_convert_agg_to_wx_image = _wxagg.convert_agg_to_wx_image
223-
_convert_agg_to_wx_bitmap = _wxagg.convert_agg_to_wx_bitmap
290+
if getattr(wx, '__version__', '0.0')[0:3] < '2.8':
291+
# wxPython < 2.8, so use the C++ accelerator or the Python routines
292+
if state and _wxagg is not None:
293+
_convert_agg_to_wx_image = _wxagg.convert_agg_to_wx_image
294+
_convert_agg_to_wx_bitmap = _wxagg.convert_agg_to_wx_bitmap
295+
else:
296+
_convert_agg_to_wx_image = _py_convert_agg_to_wx_image
297+
_convert_agg_to_wx_bitmap = _py_convert_agg_to_wx_bitmap
224298
else:
225-
_convert_agg_to_wx_image = _py_convert_agg_to_wx_image
226-
_convert_agg_to_wx_bitmap = _py_convert_agg_to_wx_bitmap
299+
# wxPython >= 2.8, so use the accelerated Python routines
300+
_convert_agg_to_wx_image = _py_WX28_convert_agg_to_wx_image
301+
_convert_agg_to_wx_bitmap = _py_WX28_convert_agg_to_wx_bitmap
227302

228303

229304
# try to load the WXAgg accelerator
@@ -234,4 +309,3 @@ def _use_accelerator(state):
234309

235310
# if it's present, use it
236311
_use_accelerator(True)
237-

lib/matplotlib/mpl-data/matplotlibrc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
#### CONFIGURATION BEGINS HERE
2727
# the default backend; one of GTK GTKAgg GTKCairo FltkAgg QtAgg TkAgg
2828
# Agg Cairo GD GDK Paint PS PDF SVG Template
29-
backend : TkAgg
29+
backend : WXAgg
3030
numerix : numpy # numpy, Numeric or numarray
3131
#interactive : False # see http://matplotlib.sourceforge.net/interactive.html
3232
#toolbar : toolbar2 # None | classic | toolbar2

setup.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -245,16 +245,17 @@ def havegtk():
245245

246246
if BUILD_WXAGG:
247247
try:
248-
import wxPython
248+
import wx
249249
except ImportError:
250250
if BUILD_WXAGG != 'auto':
251251
print 'WXAgg\'s accelerator requires wxPython'
252252
BUILD_WXAGG = 0
253253
else:
254-
BUILD_AGG = 1
255-
build_wxagg(ext_modules, packages, NUMERIX,
256-
not (isinstance(BUILD_WXAGG, str) # don't about if BUILD_WXAGG
257-
and BUILD_WXAGG.lower() == 'auto')) # is "auto"
254+
if getattr(wx, '__version__', '0.0')[0:3] < '2.8':
255+
BUILD_AGG = 1
256+
build_wxagg(ext_modules, packages, NUMERIX,
257+
not (isinstance(BUILD_WXAGG, str) # don't abort if BUILD_WXAGG
258+
and BUILD_WXAGG.lower() == 'auto')) # is "auto"
258259
rc['backend'] = 'WXAgg'
259260

260261
if BUILD_AGG:

src/_wxagg.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ static wxBitmap *convert_agg2bitmap(RendererAgg *aggRenderer, Bbox *clipbox)
235235
// Convert everything: rgba => rgb -> image => bitmap
236236
// Convert a region: rgba => clipped rgba => rgb -> image => bitmap
237237
wxImage *image = convert_agg2image(aggRenderer, clipbox);
238-
wxBitmap *bitmap = new wxBitmap(image);
238+
wxBitmap *bitmap = new wxBitmap(*image);
239239

240240
image->Destroy();
241241
delete image;
@@ -258,7 +258,7 @@ DL_EXPORT(void)
258258
init_wxagg(void)
259259
{
260260
wxPyCoreAPI_IMPORT();
261-
//suppress unused warning by creating in two lines
261+
//suppress an unused variable warning by creating _wxagg_module in two lines
262262
static _wxagg_module* _wxagg = NULL;
263263
_wxagg = new _wxagg_module;
264264
};

0 commit comments

Comments
 (0)
0