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

Skip to content

Commit 68492fb

Browse files
committed
flesh out bbox handling, fix mpl_PyFile_Dup for PyPy which is like python3
1 parent b4e2b8a commit 68492fb

3 files changed

Lines changed: 30 additions & 15 deletions

File tree

lib/matplotlib/backends/_tkagg.py

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,40 @@
66

77
app = None
88

9-
def PyAggImagePhoto(photoimage, data, mode):
9+
def PyAggImagePhoto(photoimage, data_as_str, mode, bbox_as_str=None):
1010
interp = PyAggImagePhoto.interp
1111
if not tklib.Tk_MainWindow(interp):
1212
raise _tkinter.TclError("this isn't a Tk application")
1313
photo = tklib.Tk_FindPhoto(interp, photoimage)
1414
if not photo:
15-
tklib.Tcl_AppendResult(interp, "destination photo must exist", 0)
15+
tklib.Tcl_AppendResult(interp, "destination photo must exist", 0)
1616
return tklib.TCL_ERROR
17-
img = FromTclStringNDArray(data)
18-
has_bbox = False
17+
data = FromTclStringNDArray(data_as_str)
18+
if bbox_as_str:
19+
try:
20+
bbox = FromTclStringNDArray(bbox_as_str)
21+
except:
22+
tklib.Tcl_AppendResult(interp, "bbox not valid", 0)
23+
return tklib.TCL_ERROR
24+
destx = int(bbox[0, 0])
25+
desty = data.shape[0] - int(bbox[1, 1])
26+
destwidth = int(bbox[1, 0] - bbox[0, 0])
27+
destheight = int(bbox[1, 1] - bbox[0, 1])
28+
deststride = 4 * destwidth;
29+
destbuffer = np.empty([destheight, destwidth, 4], dtype='uint8')
30+
has_bbox = True
31+
destbuffer[:,:,:] = data[desty:desty+destheight, destx:destx+destwidth,:]
32+
else:
33+
has_bbox = False
34+
destbuffer = None
35+
destx = desty = destwidth = destheight = deststride = 0;
1936
pBlock = tkffi.new('Tk_PhotoImageBlock[1]')
2037
block = pBlock[0]
2138
block.pixelSize = 1
2239
if mode == 0:
2340
block.offset[0] = block.offset[1] = block.offset[2] = 0
2441
nval = 1
25-
else:
42+
else:
2643
block.offset[0] = 0
2744
block.offset[1] = 1
2845
block.offset[2] = 2
@@ -37,25 +54,25 @@ def PyAggImagePhoto(photoimage, data, mode):
3754
block.width = destwidth
3855
block.height = destheight
3956
block.pitch = deststride
40-
block.pixelPtr = destbuffer
57+
block.pixelPtr = tkffi.from_buffer(destbuffer)
4158

4259
tklib.Tk_PhotoPutBlock_NoComposite(photo, pBlock, destx, desty,
4360
destwidth, destheight);
4461

4562
else:
46-
block.width = shape[1]
47-
block.height = shape[0]
48-
block.pitch = img.strides[0]
49-
block.pixelPtr = tkffi.from_buffer(img)
63+
block.width = data.shape[1]
64+
block.height = data.shape[0]
65+
block.pitch = data.strides[0]
66+
block.pixelPtr = tkffi.from_buffer(data)
5067

5168
#/* Clear current contents */
5269
tklib.Tk_PhotoBlank(photo);
5370
#/* Copy opaque block to photo image, and leave the rest to TK */
5471
tklib.Tk_PhotoPutBlock_NoComposite(photo, pBlock, 0, 0,
5572
block.width, block.height);
56-
return tklib.TCL_OK
73+
return tklib.TCL_OK
5774

5875
def tkinit(tk):
5976
tk.createcommand(b"PyAggImagePhoto", PyAggImagePhoto)
6077
PyAggImagePhoto.interp = tk.interp
61-
78+

lib/matplotlib/backends/tkagg.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,11 @@ def blit(photoimage, aggimage, bbox=None, colormode=1):
3737
bbox_array = None
3838
data = np.asarray(aggimage)
3939
try:
40-
import pdb;pdb.set_trace()
4140
tk.call(
4241
"PyAggImagePhoto", photoimage,
4342
id(data), colormode, id(bbox_array))
4443
except Tk.TclError:
4544
try:
46-
import pdb;pdb.set_trace()
4745
try:
4846
_tkagg.tkinit(tk.interpaddr(), 1)
4947
except AttributeError:

src/file_compat.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ extern "C" {
4848
/*
4949
* PyFile_* compatibility
5050
*/
51-
#if PY3K
51+
#if defined(PY3K) | defined(PYPY_VERSION)
5252

5353
/*
5454
* Get a FILE* handle to the file represented by the Python object

0 commit comments

Comments
 (0)