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

Skip to content

Commit fa61bdf

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 ef5d01a commit fa61bdf

File tree

1 file changed

+21
-10
lines changed

1 file changed

+21
-10
lines changed

lib/matplotlib/backends/backend_gtk3agg.py

Lines changed: 21 additions & 10 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_gtk3
57
from .backend_cairo import cairo, HAS_CAIRO_CFFI
68
from .backend_gtk3 import _BackendGTK3
@@ -25,7 +27,7 @@ def _render_figure(self, width, height):
2527
backend_agg.FigureCanvasAgg.draw(self)
2628

2729
def on_draw_event(self, widget, ctx):
28-
""" GtkDrawable draw event, like expose_event in GTK 2.X
30+
"""GtkDrawable draw event, like expose_event in GTK 2.X.
2931
"""
3032
allocation = self.get_allocation()
3133
w, h = allocation.width, allocation.height
@@ -43,20 +45,29 @@ def on_draw_event(self, widget, ctx):
4345
incref=True)
4446

4547
for bbox in bbox_queue:
46-
area = self.copy_from_bbox(bbox)
47-
buf = np.fromstring(area.to_string_argb(), dtype='uint8')
48-
4948
x = int(bbox.x0)
5049
y = h - int(bbox.y1)
5150
width = int(bbox.x1) - int(bbox.x0)
5251
height = int(bbox.y1) - int(bbox.y0)
5352

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

0 commit comments

Comments
 (0)