Skip to content

Commit 766f03a

Browse files
authored
Merge pull request #10297 from anntzer/gtk3agg-alpha
FIX: gtk3agg alpha channel.
2 parents 4b316b9 + 359b501 commit 766f03a

File tree

1 file changed

+21
-7
lines changed

1 file changed

+21
-7
lines changed

lib/matplotlib/backends/backend_gtk3agg.py

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1-
import numpy as np
1+
import sys
22
import warnings
33

4+
import numpy as np
5+
46
from . import backend_agg, backend_cairo, backend_gtk3
57
from ._gtk3_compat import gi
68
from .backend_cairo import cairo
@@ -31,7 +33,7 @@ def _render_figure(self, width, height):
3133
backend_agg.FigureCanvasAgg.draw(self)
3234

3335
def on_draw_event(self, widget, ctx):
34-
""" GtkDrawable draw event, like expose_event in GTK 2.X
36+
"""GtkDrawable draw event, like expose_event in GTK 2.X.
3537
"""
3638
allocation = self.get_allocation()
3739
w, h = allocation.width, allocation.height
@@ -45,17 +47,29 @@ def on_draw_event(self, widget, ctx):
4547
ctx = backend_cairo._to_context(ctx)
4648

4749
for bbox in bbox_queue:
48-
area = self.copy_from_bbox(bbox)
49-
buf = np.fromstring(area.to_string_argb(), dtype='uint8')
50-
5150
x = int(bbox.x0)
5251
y = h - int(bbox.y1)
5352
width = int(bbox.x1) - int(bbox.x0)
5453
height = int(bbox.y1) - int(bbox.y0)
5554

55+
buf = (np.fromstring(self.copy_from_bbox(bbox).to_string_argb(),
56+
dtype='uint8')
57+
.reshape((width, height, 4)))
58+
# cairo wants premultiplied alpha. Only bother doing the
59+
# conversion when the alpha channel is not fully opaque, as the
60+
# cost is not negligible. (The unsafe cast is needed to do the
61+
# multiplication in-place in an integer buffer.)
62+
if sys.byteorder == "little":
63+
rgb24 = buf[..., :-1]
64+
alpha8 = buf[..., -1:]
65+
else:
66+
alpha8 = buf[..., :1]
67+
rgb24 = buf[..., 1:]
68+
if alpha8.min() != 0xff:
69+
np.multiply(rgb24, alpha8 / 0xff, out=rgb24, casting="unsafe")
70+
5671
image = cairo.ImageSurface.create_for_data(
57-
buf.ravel().data, cairo.FORMAT_ARGB32,
58-
width, height, width * 4)
72+
buf.ravel().data, cairo.FORMAT_ARGB32, width, height)
5973
ctx.set_source_surface(image, x, y)
6074
ctx.paint()
6175

0 commit comments

Comments
 (0)