From e14c7af67b6d0607962f0bd9367a36c66486ec64 Mon Sep 17 00:00:00 2001 From: Tim Hoffmann <2836374+timhoffm@users.noreply.github.com> Date: Sun, 11 Dec 2022 23:28:47 +0100 Subject: [PATCH] Backport PR #24692: Avoid rgba8888->argb32 conversion if qt can do it for us. --- lib/matplotlib/backends/backend_qtagg.py | 23 ++++++++++++----------- lib/matplotlib/cbook/__init__.py | 2 +- 2 files changed, 13 insertions(+), 12 deletions(-) diff --git a/lib/matplotlib/backends/backend_qtagg.py b/lib/matplotlib/backends/backend_qtagg.py index dde185107e98..5804b433490a 100644 --- a/lib/matplotlib/backends/backend_qtagg.py +++ b/lib/matplotlib/backends/backend_qtagg.py @@ -48,30 +48,31 @@ def paintEvent(self, event): right = left + width # create a buffer using the image bounding box bbox = Bbox([[left, bottom], [right, top]]) - reg = self.copy_from_bbox(bbox) - buf = cbook._unmultiplied_rgba8888_to_premultiplied_argb32( - memoryview(reg)) + buf = memoryview(self.copy_from_bbox(bbox)) - # clear the widget canvas - painter.eraseRect(rect) + fmts = _enum("QtGui.QImage.Format") + if hasattr(fmts, "Format_RGBA8888"): + fmt = fmts.Format_RGBA8888 + else: # Qt<=5.1 support. + fmt = fmts.Format_ARGB32_Premultiplied + buf = cbook._unmultiplied_rgba8888_to_premultiplied_argb32(buf) if QT_API == "PyQt6": from PyQt6 import sip ptr = int(sip.voidptr(buf)) else: ptr = buf - qimage = QtGui.QImage( - ptr, buf.shape[1], buf.shape[0], - _enum("QtGui.QImage.Format").Format_ARGB32_Premultiplied) + + painter.eraseRect(rect) # clear the widget canvas + qimage = QtGui.QImage(ptr, buf.shape[1], buf.shape[0], fmt) _setDevicePixelRatio(qimage, self.device_pixel_ratio) # set origin using original QT coordinates origin = QtCore.QPoint(rect.left(), rect.top()) painter.drawImage(origin, qimage) # Adjust the buf reference count to work around a memory # leak bug in QImage under PySide. - if QT_API in ('PySide', 'PySide2'): - if QtCore.__version_info__ < (5, 12): - ctypes.c_long.from_address(id(buf)).value = 1 + if QT_API == "PySide2" and QtCore.__version_info__ < (5, 12): + ctypes.c_long.from_address(id(buf)).value = 1 self._draw_rect_callback(painter) finally: diff --git a/lib/matplotlib/cbook/__init__.py b/lib/matplotlib/cbook/__init__.py index abb6c6df4fab..44bea8cf4961 100644 --- a/lib/matplotlib/cbook/__init__.py +++ b/lib/matplotlib/cbook/__init__.py @@ -2090,7 +2090,7 @@ def discard(self, key): self._od.pop(key, None) -# Agg's buffers are unmultiplied RGBA8888, which neither PyQt5 nor cairo +# Agg's buffers are unmultiplied RGBA8888, which neither PyQt<=5.1 nor cairo # support; however, both do support premultiplied ARGB32.