Skip to content

Commit 199104d

Browse files
committed
Convert TkAgg utilities to pybind11
1 parent 1536245 commit 199104d

File tree

5 files changed

+137
-166
lines changed

5 files changed

+137
-166
lines changed

lib/matplotlib/backends/_backend_tk.py

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
CloseEvent, KeyEvent, LocationEvent, MouseEvent, ResizeEvent)
2424
from matplotlib._pylab_helpers import Gcf
2525
from . import _tkagg
26+
from ._tkagg import TK_PHOTO_COMPOSITE_OVERLAY, TK_PHOTO_COMPOSITE_SET
2627

2728

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

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

5856
def _blit(argsid):
5957
"""
@@ -62,11 +60,11 @@ def _blit(argsid):
6260
*argsid* is a unique string identifier to fetch the correct arguments from
6361
the ``_blit_args`` dict, since arguments cannot be passed directly.
6462
"""
65-
photoimage, dataptr, offsets, bboxptr, comp_rule = _blit_args.pop(argsid)
63+
photoimage, data, offsets, bbox, comp_rule = _blit_args.pop(argsid)
6664
if not photoimage.tk.call("info", "commands", photoimage):
6765
return
68-
_tkagg.blit(photoimage.tk.interpaddr(), str(photoimage), dataptr,
69-
comp_rule, offsets, bboxptr)
66+
_tkagg.blit(photoimage.tk.interpaddr(), str(photoimage), data, comp_rule, offsets,
67+
bbox)
7068

7169

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

110107
# tkapp.call coerces all arguments to strings, so to avoid string parsing
111108
# within _blit, pack up the arguments into a global data structure.
112-
args = photoimage, dataptr, offsets, bboxptr, comp_rule
109+
args = photoimage, data, offsets, bboxptr, comp_rule
113110
# Need a unique key to avoid thread races.
114111
# Again, make the key a string to avoid string parsing in _blit.
115112
argsid = str(id(args))

lib/matplotlib/backends/_tkagg.pyi

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import numpy as np
2+
from numpy.typing import NDArray
3+
4+
TK_PHOTO_COMPOSITE_OVERLAY: int
5+
TK_PHOTO_COMPOSITE_SET: int
6+
7+
def blit(
8+
interp: int,
9+
photo_name: str,
10+
data: NDArray[np.uint8],
11+
comp_rule: int,
12+
offset: tuple[int, int, int, int],
13+
bbox: tuple[int, int, int, int],
14+
) -> None: ...
15+
def enable_dpi_awareness(frame_handle: int, interp: int) -> bool | None: ...

lib/matplotlib/tests/test_backend_tk.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -81,9 +81,7 @@ def test_blit():
8181

8282
fig, ax = plt.subplots()
8383
photoimage = fig.canvas._tkphoto
84-
data = np.ones((4, 4, 4))
85-
height, width = data.shape[:2]
86-
dataptr = (height, width, data.ctypes.data)
84+
data = np.ones((4, 4, 4), dtype=np.uint8)
8785
# Test out of bounds blitting.
8886
bad_boxes = ((-1, 2, 0, 2),
8987
(2, 0, 0, 2),
@@ -94,8 +92,8 @@ def test_blit():
9492
for bad_box in bad_boxes:
9593
try:
9694
_tkagg.blit(
97-
photoimage.tk.interpaddr(), str(photoimage), dataptr, 0,
98-
(0, 1, 2, 3), bad_box)
95+
photoimage.tk.interpaddr(), str(photoimage), data,
96+
_tkagg.TK_PHOTO_COMPOSITE_OVERLAY, (0, 1, 2, 3), bad_box)
9997
except ValueError:
10098
print("success")
10199

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy