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

Skip to content

Convert TkAgg utilities to pybind11 #26992

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 2 commits into from
Nov 20, 2023
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
13 changes: 5 additions & 8 deletions lib/matplotlib/backends/_backend_tk.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
CloseEvent, KeyEvent, LocationEvent, MouseEvent, ResizeEvent)
from matplotlib._pylab_helpers import Gcf
from . import _tkagg
from ._tkagg import TK_PHOTO_COMPOSITE_OVERLAY, TK_PHOTO_COMPOSITE_SET


_log = logging.getLogger(__name__)
Expand Down Expand Up @@ -51,9 +52,6 @@ def _restore_foreground_window_at_end():
# Initialize to a non-empty string that is not a Tcl command
_blit_tcl_name = "mpl_blit_" + uuid.uuid4().hex

TK_PHOTO_COMPOSITE_OVERLAY = 0 # apply transparency rules pixel-wise
TK_PHOTO_COMPOSITE_SET = 1 # set image buffer directly


def _blit(argsid):
"""
Expand All @@ -62,11 +60,11 @@ def _blit(argsid):
*argsid* is a unique string identifier to fetch the correct arguments from
the ``_blit_args`` dict, since arguments cannot be passed directly.
"""
photoimage, dataptr, offsets, bboxptr, comp_rule = _blit_args.pop(argsid)
photoimage, data, offsets, bbox, comp_rule = _blit_args.pop(argsid)
if not photoimage.tk.call("info", "commands", photoimage):
return
_tkagg.blit(photoimage.tk.interpaddr(), str(photoimage), dataptr,
comp_rule, offsets, bboxptr)
_tkagg.blit(photoimage.tk.interpaddr(), str(photoimage), data, comp_rule, offsets,
bbox)


def blit(photoimage, aggimage, offsets, bbox=None):
Expand All @@ -87,7 +85,6 @@ def blit(photoimage, aggimage, offsets, bbox=None):
"""
data = np.asarray(aggimage)
height, width = data.shape[:2]
dataptr = (height, width, data.ctypes.data)
if bbox is not None:
(x1, y1), (x2, y2) = bbox.__array__()
x1 = max(math.floor(x1), 0)
Expand All @@ -109,7 +106,7 @@ def blit(photoimage, aggimage, offsets, bbox=None):

# tkapp.call coerces all arguments to strings, so to avoid string parsing
# within _blit, pack up the arguments into a global data structure.
args = photoimage, dataptr, offsets, bboxptr, comp_rule
args = photoimage, data, offsets, bboxptr, comp_rule
# Need a unique key to avoid thread races.
# Again, make the key a string to avoid string parsing in _blit.
argsid = str(id(args))
Expand Down
15 changes: 15 additions & 0 deletions lib/matplotlib/backends/_tkagg.pyi
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import numpy as np
from numpy.typing import NDArray

TK_PHOTO_COMPOSITE_OVERLAY: int
TK_PHOTO_COMPOSITE_SET: int

def blit(
interp: int,
photo_name: str,
data: NDArray[np.uint8],
comp_rule: int,
offset: tuple[int, int, int, int],
bbox: tuple[int, int, int, int],
) -> None: ...
def enable_dpi_awareness(frame_handle: int, interp: int) -> bool | None: ...
8 changes: 3 additions & 5 deletions lib/matplotlib/tests/test_backend_tk.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,7 @@ def test_blit():

fig, ax = plt.subplots()
photoimage = fig.canvas._tkphoto
data = np.ones((4, 4, 4))
height, width = data.shape[:2]
dataptr = (height, width, data.ctypes.data)
data = np.ones((4, 4, 4), dtype=np.uint8)
# Test out of bounds blitting.
bad_boxes = ((-1, 2, 0, 2),
(2, 0, 0, 2),
Expand All @@ -94,8 +92,8 @@ def test_blit():
for bad_box in bad_boxes:
try:
_tkagg.blit(
photoimage.tk.interpaddr(), str(photoimage), dataptr, 0,
(0, 1, 2, 3), bad_box)
photoimage.tk.interpaddr(), str(photoimage), data,
_tkagg.TK_PHOTO_COMPOSITE_OVERLAY, (0, 1, 2, 3), bad_box)
except ValueError:
print("success")

Expand Down
Loading