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

Skip to content

Commit 8e80a20

Browse files
committed
change interface in PyAggImagePhoto to avoid id()
1 parent 68492fb commit 8e80a20

3 files changed

Lines changed: 52 additions & 93 deletions

File tree

lib/matplotlib/backends/tkagg.py

Lines changed: 23 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -7,49 +7,32 @@
77
import numpy as np
88

99
from matplotlib.backends import _tkagg
10-
from platform import python_implementation
1110

12-
if python_implementation() == 'PyPy':
13-
def blit(photoimage, aggimage, bbox=None, colormode=1):
14-
tk = photoimage.tk
15-
16-
if bbox is not None:
17-
bbox_array = bbox.__array__()
18-
else:
19-
bbox_array = None
20-
data = np.asarray(aggimage)
21-
try:
22-
tk.call(
23-
"PyAggImagePhoto", photoimage,
24-
data, colormode, bbox_array)
25-
except Tk.TclError:
26-
_tkagg.tkinit(tk)
27-
tk.call("PyAggImagePhoto", photoimage,
28-
data, colormode, bbox_array)
29-
30-
else:
31-
def blit(photoimage, aggimage, bbox=None, colormode=1):
32-
tk = photoimage.tk
33-
34-
if bbox is not None:
35-
bbox_array = bbox.__array__()
36-
else:
37-
bbox_array = None
38-
data = np.asarray(aggimage)
11+
def blit(photoimage, aggimage, bbox=None, colormode=1):
12+
tk = photoimage.tk
13+
14+
if bbox is not None:
15+
bbox_array = bbox.__array__()
16+
# x1, x2, y1, y2
17+
bboxptr = (bbox_array[0, 0], bbox_array[1, 0], bbox_array[0, 1], bbox_array[1, 1])
18+
else:
19+
bboxptr = 0
20+
data = np.asarray(aggimage)
21+
dataptr = (data.ctypes.data, data.shape[0], data.shape[1])
22+
try:
23+
tk.call(
24+
"PyAggImagePhoto", photoimage,
25+
dataptr, colormode, bboxptr)
26+
except Tk.TclError:
3927
try:
40-
tk.call(
41-
"PyAggImagePhoto", photoimage,
42-
id(data), colormode, id(bbox_array))
43-
except Tk.TclError:
4428
try:
45-
try:
46-
_tkagg.tkinit(tk.interpaddr(), 1)
47-
except AttributeError:
48-
_tkagg.tkinit(id(tk), 0)
49-
tk.call("PyAggImagePhoto", photoimage,
50-
id(data), colormode, id(bbox_array))
51-
except (ImportError, AttributeError, Tk.TclError):
52-
raise
29+
_tkagg.tkinit(tk.interpaddr(), 1)
30+
except AttributeError:
31+
_tkagg.tkinit(tk, 0)
32+
tk.call("PyAggImagePhoto", photoimage,
33+
dataptr, colormode, bboxptr)
34+
except (ImportError, AttributeError, Tk.TclError):
35+
raise
5336

5437
def test(aggimage):
5538
import time

setupext.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1513,13 +1513,11 @@ def runtime_check(self):
15131513

15141514
def get_extension(self):
15151515
sources = [
1516-
'src/py_converters.cpp',
15171516
'src/_tkagg.cpp'
15181517
]
15191518

15201519
ext = make_extension('matplotlib.backends._tkagg', sources)
15211520
self.add_flags(ext)
1522-
Numpy().add_flags(ext)
15231521
LibAgg().add_flags(ext, add_sources=False)
15241522
return ext
15251523

src/_tkagg.cpp

Lines changed: 29 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,15 @@
1313
#include <cstdio>
1414
#include <sstream>
1515

16-
#include "py_converters.h"
17-
1816
// Include our own excerpts from the Tcl / Tk headers
1917
#include "_tkmini.h"
2018

2119
#if defined(_MSC_VER)
22-
# define SIZE_T_FORMAT "%Iu"
20+
# define IMG_FORMAT "%Iu %d %d"
2321
#else
24-
# define SIZE_T_FORMAT "%zu"
22+
# define IMG_FORMAT "%zu %d %d"
2523
#endif
24+
# define BBOX_FORMAT "%f %f %f %f"
2625

2726
typedef struct
2827
{
@@ -44,16 +43,15 @@ static int PyAggImagePhoto(ClientData clientdata, Tcl_Interp *interp, int
4443
{
4544
Tk_PhotoHandle photo;
4645
Tk_PhotoImageBlock block;
47-
PyObject *bufferobj;
4846

4947
// vars for blitting
50-
PyObject *bboxo;
5148

52-
size_t aggl, bboxl;
49+
size_t pdata;
50+
int wdata, hdata;
51+
float x1, x2, y1, y2;
5352
bool has_bbox;
54-
uint8_t *destbuffer;
53+
uint8_t *destbuffer, *buffer;
5554
int destx, desty, destwidth, destheight, deststride;
56-
//unsigned long tmp_ptr;
5755

5856
long mode;
5957
long nval;
@@ -73,24 +71,14 @@ static int PyAggImagePhoto(ClientData clientdata, Tcl_Interp *interp, int
7371
TCL_APPEND_RESULT(interp, "destination photo must exist", (char *)NULL);
7472
return TCL_ERROR;
7573
}
76-
/* get array (or object that can be converted to array) pointer */
77-
if (sscanf(argv[2], SIZE_T_FORMAT, &aggl) != 1) {
78-
TCL_APPEND_RESULT(interp, "error casting pointer", (char *)NULL);
79-
return TCL_ERROR;
80-
}
81-
bufferobj = (PyObject *)aggl;
82-
83-
numpy::array_view<uint8_t, 3> buffer;
84-
try {
85-
buffer = numpy::array_view<uint8_t, 3>(bufferobj);
86-
} catch (...) {
87-
TCL_APPEND_RESULT(interp, "buffer is of wrong type", (char *)NULL);
88-
PyErr_Clear();
74+
/* get buffer from str which is "ptr height width" */
75+
if (sscanf(argv[2], IMG_FORMAT, &pdata, &hdata, &wdata) != 3) {
76+
TCL_APPEND_RESULT(interp,
77+
"error reading data, expected ptr height width",
78+
(char *)NULL);
8979
return TCL_ERROR;
9080
}
91-
int srcheight = buffer.dim(0);
92-
93-
/* XXX insert aggRenderer type check */
81+
buffer = (uint8_t*)pdata;
9482

9583
/* get array mode (0=mono, 1=rgb, 2=rgba) */
9684
mode = atol(argv[3]);
@@ -100,39 +88,33 @@ static int PyAggImagePhoto(ClientData clientdata, Tcl_Interp *interp, int
10088
}
10189

10290
/* check for bbox/blitting */
103-
if (sscanf(argv[4], SIZE_T_FORMAT, &bboxl) != 1) {
104-
TCL_APPEND_RESULT(interp, "error casting pointer", (char *)NULL);
105-
return TCL_ERROR;
106-
}
107-
bboxo = (PyObject *)bboxl;
108-
109-
if (bboxo != NULL && bboxo != Py_None) {
110-
agg::rect_d rect;
111-
if (!convert_rect(bboxo, &rect)) {
112-
return TCL_ERROR;
113-
}
114-
91+
if (sscanf(argv[4], BBOX_FORMAT, &x1, &x2, &y1, &y2) == 4) {
11592
has_bbox = true;
93+
}
94+
else {
95+
has_bbox = false;
96+
}
11697

117-
destx = (int)rect.x1;
118-
desty = srcheight - (int)rect.y2;
119-
destwidth = (int)(rect.x2 - rect.x1);
120-
destheight = (int)(rect.y2 - rect.y1);
98+
if (has_bbox) {
99+
int srcstride = wdata * 4;
100+
destx = x1;
101+
desty = hdata - y2;
102+
destwidth = x2 - x1;
103+
destheight = y2 - y1;
121104
deststride = 4 * destwidth;
122105

123-
destbuffer = new agg::int8u[deststride * destheight];
106+
destbuffer = new uint8_t[deststride * destheight];
124107
if (destbuffer == NULL) {
125108
TCL_APPEND_RESULT(interp, "could not allocate memory", (char *)NULL);
126109
return TCL_ERROR;
127110
}
128111

129112
for (int i = 0; i < destheight; ++i) {
130113
memcpy(destbuffer + (deststride * i),
131-
&buffer(i + desty, destx, 0),
114+
&buffer[(i + desty) * srcstride + (destx * 4)],
132115
deststride);
133116
}
134117
} else {
135-
has_bbox = false;
136118
destbuffer = NULL;
137119
destx = desty = destwidth = destheight = deststride = 0;
138120
}
@@ -168,10 +150,10 @@ static int PyAggImagePhoto(ClientData clientdata, Tcl_Interp *interp, int
168150
delete[] destbuffer;
169151

170152
} else {
171-
block.width = buffer.dim(1);
172-
block.height = buffer.dim(0);
153+
block.width = wdata;
154+
block.height = hdata;
173155
block.pitch = (int)block.width * nval;
174-
block.pixelPtr = buffer.data();
156+
block.pixelPtr = buffer;
175157

176158
/* Clear current contents */
177159
TK_PHOTO_BLANK(photo);
@@ -457,15 +439,11 @@ PyMODINIT_FUNC PyInit__tkagg(void)
457439

458440
m = PyModule_Create(&_tkagg_module);
459441

460-
import_array();
461-
462442
return (load_tkinter_funcs() == 0) ? m : NULL;
463443
}
464444
#else
465445
PyMODINIT_FUNC init_tkagg(void)
466446
{
467-
import_array();
468-
469447
Py_InitModule("_tkagg", functions);
470448

471449
load_tkinter_funcs();

0 commit comments

Comments
 (0)