Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 63498fd

Browse files
author
Steve Chaplin
committed
SC 04/11/2004
svn path=/trunk/matplotlib/; revision=658
1 parent 51f94c3 commit 63498fd

2 files changed

Lines changed: 36 additions & 6 deletions

File tree

lib/matplotlib/backends/backend_cairo.py

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,9 @@
3333
def _fn_name(): return sys._getframe(1).f_code.co_name
3434

3535
from matplotlib import verbose
36-
from matplotlib.numerix import asarray, pi #, fromstring, UInt8, zeros, where, transpose, nonzero, indices, ones, nxfrom matplotlib._matlab_helpers import Gcf
36+
from matplotlib.numerix import asarray, pi, fromstring, UInt8 #, zeros, where,
37+
#transpose, nonzero, indices, ones, nx
38+
#from matplotlib._matlab_helpers import Gcf
3739
from matplotlib.backend_bases import RendererBase, GraphicsContextBase,\
3840
FigureManagerBase, FigureCanvasBase
3941
from matplotlib.cbook import enumerate, True, False
@@ -179,9 +181,33 @@ def draw_image(self, x, y, im, origin, bbox):
179181
None
180182
"""
181183
if DEBUG: print 'backend_cairo.RendererCairo.%s()' % _fn_name()
182-
pass
183-
# how does cairo do this?
184-
184+
# why is there no gc arg?
185+
186+
try: import cairo.numpy
187+
except:
188+
verbose.report_error("cairo.numpy module required for draw_image()")
189+
return
190+
191+
ctx = cairo.Context()
192+
ctx.set_target_surface (self.surface)
193+
ctx.set_matrix (self.matrix)
194+
195+
# bbox - not used
196+
flipud = origin=='lower'
197+
rows, cols, s = im.as_str(flipud)
198+
199+
X = fromstring(s, UInt8)
200+
X.shape = rows, cols, 4
201+
# ARGB32 is incompatible with Image format? - colors are wrong
202+
surface = cairo.numpy.surface_create_for_array (X)
203+
204+
# Alternative
205+
#surface = cairo.surface_create_for_image(buffer, cairo.FORMAT_ARGB32, cols, rows) #, stride)
206+
# error: TypeError: Cannot use string as modifiable buffer
207+
208+
ctx.translate (x,y)
209+
ctx.show_surface (surface, cols, rows)
210+
185211

186212
def draw_line(self, gc, x1, y1, x2, y2):
187213
"""
@@ -485,6 +511,7 @@ def print_figure_fn(figure, filename, dpi=150, facecolor='w', edgecolor='w',
485511
override this on hardcopy
486512
487513
orientation - only currently applies to PostScript printing.
514+
filename - can also be a file object, png format is assumed
488515
"""
489516
if DEBUG: print 'backend_cairo.FigureCanvasCairo.%s()' % _fn_name()
490517

@@ -507,7 +534,10 @@ def print_figure_fn(figure, filename, dpi=150, facecolor='w', edgecolor='w',
507534
figure.set_edgecolor(edgecolor)
508535

509536
ext = ext.lower()
510-
if ext in ('png', 'ps'):
537+
if isinstance(filename, file):
538+
# for fileobjects assume PNG format
539+
_save_png (figure, filename)
540+
elif ext in ('png', 'ps'):
511541
try:
512542
fileObject = file(filename,'wb')
513543
except IOError, exc:

lib/matplotlib/backends/backend_gtkcairo.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ def _fn_name(): return sys._getframe(1).f_code.co_name
1515
IMAGE_FORMAT_DEFAULT, print_figure_fn
1616
from backend_gtk import gtk, FigureManagerGTK, FigureCanvasGTK, show, \
1717
draw_if_interactive, error_msg, NavigationToolbar, backend_version, \
18-
raise_msg_to_str, gdk_pixmap_save
18+
gdk_pixmap_save
1919

2020
import gobject
2121

0 commit comments

Comments
 (0)