Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 34 additions & 3 deletions lib/matplotlib/backends/backend_cairo.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,21 @@ def _fn_name(): return sys._getframe(1).f_code.co_name
BYTE_FORMAT = 1 # ARGB


class ArrayWrapper:
"""Thin wrapper around numpy ndarray to expose the interface
expected by cairocffi. Basically replicates the
array.array interface.
"""
def __init__(self, myarray):
self.__array = myarray
self.__data = myarray.ctypes.data
self.__size = len(myarray.flatten())
self.itemsize = myarray.itemsize

def buffer_info(self):
return (self.__data, self.__size)


class RendererCairo(RendererBase):
fontweights = {
100 : cairo.FONT_WEIGHT_NORMAL,
Expand Down Expand Up @@ -175,9 +190,25 @@ def draw_image(self, gc, x, y, im):
im = im[:, :, (2, 1, 0, 3)]
else:
im = im[:, :, (3, 0, 1, 2)]
surface = cairo.ImageSurface.create_for_data(
memoryview(im.flatten()), cairo.FORMAT_ARGB32, im.shape[1], im.shape[0],
im.shape[1]*4)
if HAS_CAIRO_CFFI:
# cairocffi tries to use the buffer_info from array.array
# that we replicate in ArrayWrapper and alternatively falls back
# on ctypes to get a pointer to the numpy array. This works
# correctly on a numpy array in python3 but not 2.7. We replicate
# the array.array functionality here to get cross version support.
imbuffer = ArrayWrapper(im.flatten())
else:
# py2cairo uses PyObject_AsWriteBuffer
# to get a pointer to the numpy array this works correctly
# on a regular numpy array but not on a memory view.
# At the time of writing the latest release version of
# py3cairo still does not support create_for_data
imbuffer = im.flatten()
surface = cairo.ImageSurface.create_for_data(imbuffer,
cairo.FORMAT_ARGB32,
im.shape[1],
im.shape[0],
im.shape[1]*4)
ctx = gc.ctx
y = self.height - y - im.shape[0]

Expand Down