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

Skip to content

Commit 359b501

Browse files
committed
Fix gtk3agg alpha channel.
The gtk3agg backend works by drawing the ARGB32 buffer (from Agg) onto a cairo context (passed by gtk3). However, cairo wants a *premultiplied* ARGB32 buffer (i.e., where 100% blue with 50% transparency is represented by (r=0%, g=0%, b=50%, a=50%) instead of (r=0%, g=0%, b=100%, a=50%), which we didn't do before. This is only apparent if the entire buffer contains some transparency, e.g. if the figure background is transparent. Consider e.g. under gtk3agg: from pylab import * rcParams["figure.facecolor"] = (0, 0, 0, 0) gca() show() Without the patch, the area surrounding the axes is white (because of the misinterpretation of premultiplied alpha). With the patch, it is (correctly) gray, which is the background color of the gtk widget. (Note that when running the example under qt5agg or tkagg, the situation is complicated by the fact that these backends themselves set the widget background color to white rather than gray.) As a comparison point, qt5agg builds the QImage using Format_ARGB32, instead of Format_ARB32_Premultiplied; i.e. Qt provides its own conversion from non-premultiplied to premultiplied. The premultiplication step involves allocating a full new buffer, so check whether there is actually any transparency before doing it.
1 parent a0994d3 commit 359b501

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)