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

Skip to content

Fix gtk3agg alpha channel. #10297

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

Merged
merged 1 commit into from
Jul 1, 2018
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
28 changes: 21 additions & 7 deletions lib/matplotlib/backends/backend_gtk3agg.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import numpy as np
import sys
import warnings

import numpy as np

from . import backend_agg, backend_cairo, backend_gtk3
from ._gtk3_compat import gi
from .backend_cairo import cairo
Expand Down Expand Up @@ -31,7 +33,7 @@ def _render_figure(self, width, height):
backend_agg.FigureCanvasAgg.draw(self)

def on_draw_event(self, widget, ctx):
""" GtkDrawable draw event, like expose_event in GTK 2.X
"""GtkDrawable draw event, like expose_event in GTK 2.X.
"""
allocation = self.get_allocation()
w, h = allocation.width, allocation.height
Expand All @@ -45,17 +47,29 @@ def on_draw_event(self, widget, ctx):
ctx = backend_cairo._to_context(ctx)

for bbox in bbox_queue:
area = self.copy_from_bbox(bbox)
buf = np.fromstring(area.to_string_argb(), dtype='uint8')

x = int(bbox.x0)
y = h - int(bbox.y1)
width = int(bbox.x1) - int(bbox.x0)
height = int(bbox.y1) - int(bbox.y0)

buf = (np.fromstring(self.copy_from_bbox(bbox).to_string_argb(),
dtype='uint8')
.reshape((width, height, 4)))
# cairo wants premultiplied alpha. Only bother doing the
# conversion when the alpha channel is not fully opaque, as the
# cost is not negligible. (The unsafe cast is needed to do the
# multiplication in-place in an integer buffer.)
if sys.byteorder == "little":
rgb24 = buf[..., :-1]
alpha8 = buf[..., -1:]
else:
alpha8 = buf[..., :1]
rgb24 = buf[..., 1:]
if alpha8.min() != 0xff:
np.multiply(rgb24, alpha8 / 0xff, out=rgb24, casting="unsafe")

image = cairo.ImageSurface.create_for_data(
buf.ravel().data, cairo.FORMAT_ARGB32,
width, height, width * 4)
buf.ravel().data, cairo.FORMAT_ARGB32, width, height)
ctx.set_source_surface(image, x, y)
ctx.paint()

Expand Down