File 0001-Fix-cairo-s-image-inversion-and-alpha-misapplication.patch of Package python-matplotlib.2344

From fbb94a5169b909fffa077db4d90478d64710fab4 Mon Sep 17 00:00:00 2001
From: Antony Lee <anntzer.lee@gmail.com>
Date: Sat, 27 Oct 2018 20:30:07 +0300
Subject: [PATCH] Fix cairo's image inversion and alpha misapplication.

(cherry picked from commit fde8971aa04b4fc41ee5775a1b4573473350a0e3)
---
 lib/matplotlib/backends/backend_cairo.py | 28 ++++++++++++++++++++--------
 1 file changed, 20 insertions(+), 8 deletions(-)

diff --git a/lib/matplotlib/backends/backend_cairo.py b/lib/matplotlib/backends/backend_cairo.py
index 30b8eaa7..ace8974d 100644
--- a/lib/matplotlib/backends/backend_cairo.py
+++ b/lib/matplotlib/backends/backend_cairo.py
@@ -67,6 +67,24 @@ if sys.byteorder == 'little':
 else:
     BYTE_FORMAT = 1 # ARGB
 
+def _unmultipled_rgba8888_to_premultiplied_argb32(rgba8888):
+    """
+    Convert an unmultiplied RGBA8888 buffer to a premultiplied ARGB32 buffer.
+    """
+    if sys.byteorder == "little":
+        argb32 = np.take(rgba8888, [2, 1, 0, 3], axis=2)
+        rgb24 = argb32[..., :-1]
+        alpha8 = argb32[..., -1:]
+    else:
+        argb32 = np.take(rgba8888, [3, 0, 1, 2], axis=2)
+        alpha8 = argb32[..., :1]
+        rgb24 = argb32[..., 1:]
+    # Only bother premultiplying 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 alpha8.min() != 0xff:
+        np.multiply(rgb24, alpha8 / 0xff, out=rgb24, casting="unsafe")
+    return argb32
 
 class ArrayWrapper:
     """Thin wrapper around numpy ndarray to expose the interface
@@ -186,10 +204,7 @@ class RendererCairo(RendererBase):
         # bbox - not currently used
         if _debug: print('%s.%s()' % (self.__class__.__name__, _fn_name()))
 
-        if sys.byteorder == 'little':
-            im = im[:, :, (2, 1, 0, 3)]
-        else:
-            im = im[:, :, (3, 0, 1, 2)]
+        im = _unmultipled_rgba8888_to_premultiplied_argb32(im[::-1])
         if HAS_CAIRO_CFFI:
             # cairocffi tries to use the buffer_info from array.array
             # that we replicate in ArrayWrapper and alternatively falls back
@@ -214,10 +229,7 @@ class RendererCairo(RendererBase):
 
         ctx.save()
         ctx.set_source_surface(surface, float(x), float(y))
-        if gc.get_alpha() != 1.0:
-            ctx.paint_with_alpha(gc.get_alpha())
-        else:
-            ctx.paint()
+        ctx.paint()
         ctx.restore()
 
     def draw_text(self, gc, x, y, s, prop, angle, ismath=False, mtext=None):
-- 
2.16.4

openSUSE Build Service is sponsored by