Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

2.x: Cairo backends cannot render images #6562

Closed
QuLogic opened this issue Jun 8, 2016 · 5 comments
Closed

2.x: Cairo backends cannot render images #6562

QuLogic opened this issue Jun 8, 2016 · 5 comments

Comments

@QuLogic
Copy link
Member

QuLogic commented Jun 8, 2016

In testing #6540, I can see that neither Cairo backend is able to render images correctly.

With GTKCairo:

Traceback (most recent call last):
  File ".../lib/matplotlib/backends/backend_gtk.py", line 438, in expose_event
    self._render_figure(self._pixmap, w, h)
  File ".../lib/matplotlib/backends/backend_gtk.py", line 426, in _render_figure
    self.figure.draw (self._renderer)
  File ".../lib/matplotlib/artist.py", line 63, in draw_wrapper
    draw(artist, renderer, *args, **kwargs)
  File ".../lib/matplotlib/figure.py", line 1129, in draw
    renderer, self, dsu, self.suppressComposite)
  File ".../lib/matplotlib/image.py", line 139, in _draw_list_compositing_images
    a.draw(renderer)
  File ".../lib/matplotlib/artist.py", line 63, in draw_wrapper
    draw(artist, renderer, *args, **kwargs)
  File ".../lib/matplotlib/axes/_base.py", line 2353, in draw
    mimage._draw_list_compositing_images(renderer, self, dsu)
  File ".../lib/matplotlib/image.py", line 163, in _draw_list_compositing_images
    flush_images()
  File ".../lib/matplotlib/image.py", line 147, in flush_images
    image_group[0].draw(renderer)
  File ".../lib/matplotlib/artist.py", line 63, in draw_wrapper
    draw(artist, renderer, *args, **kwargs)
  File ".../lib/matplotlib/image.py", line 464, in draw
    renderer.draw_image(gc, l, b, im)
  File ".../lib/matplotlib/backends/backend_cairo.py", line 180, in draw_image
    im.shape[1]*4)
TypeError: expected a writeable buffer object

With GTK3Cairo:

Traceback (most recent call last):
  File ".../lib/matplotlib/backends/backend_gtk3cairo.py", line 45, in on_draw_event
    self._render_figure(w, h)
  File ".../lib/matplotlib/backends/backend_gtk3cairo.py", line 34, in _render_figure
    self.figure.draw (self._renderer)
  File ".../lib/matplotlib/artist.py", line 63, in draw_wrapper
    draw(artist, renderer, *args, **kwargs)
  File ".../lib/matplotlib/figure.py", line 1129, in draw
    renderer, self, dsu, self.suppressComposite)
  File ".../lib/matplotlib/image.py", line 139, in _draw_list_compositing_images
    a.draw(renderer)
  File ".../lib/matplotlib/artist.py", line 63, in draw_wrapper
    draw(artist, renderer, *args, **kwargs)
  File ".../lib/matplotlib/axes/_base.py", line 2353, in draw
    mimage._draw_list_compositing_images(renderer, self, dsu)
  File ".../lib/matplotlib/image.py", line 163, in _draw_list_compositing_images
    flush_images()
  File ".../lib/matplotlib/image.py", line 147, in flush_images
    image_group[0].draw(renderer)
  File ".../lib/matplotlib/artist.py", line 63, in draw_wrapper
    draw(artist, renderer, *args, **kwargs)
  File ".../lib/matplotlib/image.py", line 464, in draw
    renderer.draw_image(gc, l, b, im)
  File ".../lib/matplotlib/backends/backend_cairo.py", line 180, in draw_image
    im.shape[1]*4)
TypeError: expected a writeable buffer object

Bisect points to 6595139, the backport of #5718.

@QuLogic QuLogic added this to the 2.0 (style change major release) milestone Jun 8, 2016
@QuLogic
Copy link
Member Author

QuLogic commented Jun 8, 2016

Bisect on the original branch points to one of c9cae50, 3b7d1bb, 4a05302, 0aa222a, cf11aea, 0793aa2; perhaps that's narrow enough to figure it out.

cc @mdboom

@jenshnielsen
Copy link
Member

Interestingly enough this seems to be a python2 issue only. I can reproduce the error on python 2.7 but not on 3.5

The following reproduces the error without matplotlib

try:
    import cairocffi as cairo
except:
    import cairo
import numpy as np
im = np.random.rand(100,100,4)
cairo.ImageSurface.create_for_data(memoryview(im.flatten()),
                                   cairo.FORMAT_ARGB32,
                                   im.shape[1],
                                   im.shape[0], im.shape[1]*4)

@jenshnielsen
Copy link
Member

So a in cairocffi this is fundamentally because ctypes.c_char.from_buffer_copy(memoryview(data))
doesn't work. Regardless of the type of data. Numpy array, bytesarray ...

@jenshnielsen
Copy link
Member

This stackoverflow question is an example of the same issue http://stackoverflow.com/questions/28984692/ctypes-from-buffer-with-memoryviews-in-python-2-7-and-python-3-4

jenshnielsen added a commit to jenshnielsen/matplotlib that referenced this issue Jun 18, 2016
Call cairo.ImageSurface.create_for_data with an array that both py2cairo and cairocffi knows how to get a pointer too and size of
@QuLogic
Copy link
Member Author

QuLogic commented Jun 22, 2016

Fixed by #6602.

@QuLogic QuLogic closed this as completed Jun 22, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants