FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Rewrite the tk C blitting code by anntzer · Pull Request #10680 · matplotlib/matplotlib · GitHub

Repository navigation

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

Filter by extension

Filter by extension .cpp  (1) .py  (4) All 2 file types selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
34 changes: 29 additions & 5 deletions lib/matplotlib/backends/_backend_tk.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
from __future__ import (absolute_import, division, print_function,
unicode_literals)

import six
from six.moves import tkinter as Tk

import math
import logging
import os.path
import sys

# Paint image to Tk photo blitter extension
import matplotlib.backends.tkagg as tkagg
import numpy as np

from . import _tkagg
from matplotlib.backends.backend_agg import FigureCanvasAgg
import matplotlib.backends.windowing as windowing

Expand Down Expand Up @@ -48,11 +46,37 @@ def raise_msg_to_str(msg):
msg = '\n'.join(map(str, msg))
return msg


def error_msg_tkpaint(msg, parent=None):
from six.moves import tkinter_messagebox as tkMessageBox
tkMessageBox.showerror("matplotlib", msg)


def blit(photoimage, aggimage, offsets, bbox=None):
"""
Blit *aggimage* to *photoimage*.

*offsets* is a tuple describing how to fill the ``offset`` field of the
``Tk_PhotoImageBlock`` struct: it should be (0, 1, 2, 3) for RGBA8888 data,
(2, 1, 0, 3) for little-endian ARBG32 (i.e. GBRA8888) data and (1, 2, 3, 0)
for big-endian ARGB32 (i.e. ARGB8888) data.

If *bbox* is passed, it defines the region that gets blitted.
"""
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__()
bboxptr = (math.floor(x1), math.ceil(x2),
math.floor(y1), math.ceil(y2))
else:
photoimage.blank()
bboxptr = (0, width, 0, height)
_tkagg.blit(
photoimage.tk.interpaddr(), str(photoimage), dataptr, offsets, bboxptr)


class TimerTk(TimerBase):
'''
Subclass of :class:`backend_bases.TimerBase` that uses Tk's timer events.
Expand Down
8 changes: 4 additions & 4 deletions lib/matplotlib/backends/backend_tkagg.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from . import tkagg # Paint image to Tk photo blitter extension.
from . import _backend_tk
from .backend_agg import FigureCanvasAgg
from ._backend_tk import (
_BackendTk, FigureCanvasTk, FigureManagerTk, NavigationToolbar2Tk)
Expand All @@ -7,12 +7,12 @@
class FigureCanvasTkAgg(FigureCanvasAgg, FigureCanvasTk):
def draw(self):
super(FigureCanvasTkAgg, self).draw()
tkagg.blit(self._tkphoto, self.renderer._renderer, colormode=2)
_backend_tk.blit(self._tkphoto, self.renderer._renderer, (0, 1, 2, 3))
self._master.update_idletasks()

def blit(self, bbox=None):
tkagg.blit(
self._tkphoto, self.renderer._renderer, bbox=bbox, colormode=2)
_backend_tk.blit(
self._tkphoto, self.renderer._renderer, (0, 1, 2, 3), bbox=bbox)
self._master.update_idletasks()


Expand Down
12 changes: 4 additions & 8 deletions lib/matplotlib/backends/backend_tkcairo.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import numpy as np

from . import tkagg # Paint image to Tk photo blitter extension.
from . import _backend_tk
from .backend_cairo import cairo, FigureCanvasCairo, RendererCairo
from ._backend_tk import _BackendTk, FigureCanvasTk

Expand All @@ -20,13 +20,9 @@ def draw(self):
self._renderer.set_width_height(width, height)
self.figure.draw(self._renderer)
buf = np.reshape(surface.get_data(), (height, width, 4))
# Convert from ARGB32 to RGBA8888. Using .take() instead of directly
# indexing ensures C-contiguity of the result, which is needed by
# tkagg.
buf = buf.take(
[2, 1, 0, 3] if sys.byteorder == "little" else [1, 2, 3, 0],
axis=2)
tkagg.blit(self._tkphoto, buf, colormode=2)
_backend_tk.blit(
self._tkphoto, buf,
(2, 1, 0, 3) if sys.byteorder == "little" else (1, 2, 3, 0))
self._master.update_idletasks()


Expand Down
6 changes: 6 additions & 0 deletions lib/matplotlib/backends/tkagg.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,14 @@

import numpy as np

from matplotlib import cbook
from matplotlib.backends import _tkagg


cbook.warn_deprecated(
"3.0", "The matplotlib.backends.tkagg module is deprecated.")


def blit(photoimage, aggimage, bbox=None, colormode=1):
tk = photoimage.tk

Expand Down
46 changes: 44 additions & 2 deletions src/_tkagg.cpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -203,9 +203,50 @@ static PyObject *_tkinit(PyObject *self, PyObject *args)
return Py_None;
}

static PyObject *mpl_tk_blit(PyObject *self, PyObject *args)
{
Tcl_Interp *interp;
char const *photo_name;
int height, width;
unsigned char *data_ptr;
int o0, o1, o2, o3;
int x1, x2, y1, y2;
Tk_PhotoHandle photo;
Tk_PhotoImageBlock block;
if (!PyArg_ParseTuple(args, "ns(iin)(iiii)(iiii):blit",
&interp, &photo_name,
&height, &width, &data_ptr,
&o0, &o1, &o2, &o3,
&x1, &x2, &y1, &y2)) {
goto exit;
}
if (!(photo = TK_FIND_PHOTO(interp, photo_name))) {
PyErr_SetString(PyExc_ValueError, "Failed to extract Tk_PhotoHandle");
goto exit;
}
block.pixelPtr = data_ptr + 4 * ((height - y2) * width + x1);
block.width = x2 - x1;
block.height = y2 - y1;
block.pitch = 4 * width;
block.pixelSize = 4;
block.offset[0] = o0;
block.offset[1] = o1;
block.offset[2] = o2;
block.offset[3] = o3;
TK_PHOTO_PUT_BLOCK_NO_COMPOSITE(
photo, &block, x1, height - y2, x2 - x1, y2 - y1);
exit:
if (PyErr_Occurred()) {
return NULL;
} else {
Py_RETURN_NONE;
}
}

static PyMethodDef functions[] = {
/* Tkinter interface stuff */
{ "tkinit", (PyCFunction)_tkinit, 1 },
{ "blit", (PyCFunction)mpl_tk_blit, 1 },
{ NULL, NULL } /* sentinel */
};

Expand Down Expand Up @@ -444,8 +485,9 @@ int load_tkinter_funcs(void)
}
#endif // end not Windows

static PyModuleDef _tkagg_module = { PyModuleDef_HEAD_INIT, "_tkagg", "", -1, functions,
NULL, NULL, NULL, NULL };
static PyModuleDef _tkagg_module = {
PyModuleDef_HEAD_INIT, "_tkagg", "", -1, functions, NULL, NULL, NULL, NULL
};

PyMODINIT_FUNC PyInit__tkagg(void)
{
Expand Down

Back | FazBrowse Home | New Git URL