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

Skip to content

Backport PR #24692 on branch v3.6.x (Avoid rgba8888->argb32 conversion if qt can do it for us.) #24694

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 12 additions & 11 deletions lib/matplotlib/backends/backend_qtagg.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion lib/matplotlib/cbook/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.


Expand Down