|
1 | | -from __future__ import (absolute_import, division, print_function, |
2 | | - unicode_literals) |
3 | | - |
4 | 1 | import six |
5 | 2 | from six.moves import tkinter as Tk |
6 | 3 |
|
| 4 | +import math |
7 | 5 | import logging |
8 | 6 | import os.path |
9 | 7 | import sys |
10 | 8 |
|
11 | | -# Paint image to Tk photo blitter extension |
12 | | -import matplotlib.backends.tkagg as tkagg |
| 9 | +import numpy as np |
13 | 10 |
|
| 11 | +from . import _tkagg |
14 | 12 | from matplotlib.backends.backend_agg import FigureCanvasAgg |
15 | 13 | import matplotlib.backends.windowing as windowing |
16 | 14 |
|
@@ -48,11 +46,37 @@ def raise_msg_to_str(msg): |
48 | 46 | msg = '\n'.join(map(str, msg)) |
49 | 47 | return msg |
50 | 48 |
|
| 49 | + |
51 | 50 | def error_msg_tkpaint(msg, parent=None): |
52 | 51 | from six.moves import tkinter_messagebox as tkMessageBox |
53 | 52 | tkMessageBox.showerror("matplotlib", msg) |
54 | 53 |
|
55 | 54 |
|
| 55 | +def blit(photoimage, aggimage, offsets, bbox=None): |
| 56 | + """ |
| 57 | + Blit *aggimage* to *photoimage*. |
| 58 | +
|
| 59 | + *offsets* is a tuple describing how to fill the ``offset`` field of the |
| 60 | + ``Tk_PhotoImageBlock`` struct: it should be (0, 1, 2, 3) for RGBA8888 data, |
| 61 | + (2, 1, 0, 3) for little-endian ARBG32 (i.e. GBRA8888) data and (1, 2, 3, 0) |
| 62 | + for big-endian ARGB32 (i.e. ARGB8888) data. |
| 63 | +
|
| 64 | + If *bbox* is passed, it defines the region that gets blitted. |
| 65 | + """ |
| 66 | + data = np.asarray(aggimage) |
| 67 | + height, width = data.shape[:2] |
| 68 | + dataptr = (height, width, data.ctypes.data) |
| 69 | + if bbox is not None: |
| 70 | + (x1, y1), (x2, y2) = bbox.__array__() |
| 71 | + bboxptr = (math.floor(x1), math.ceil(x2), |
| 72 | + math.floor(y1), math.ceil(y2)) |
| 73 | + else: |
| 74 | + photoimage.blank() |
| 75 | + bboxptr = (0, width, 0, height) |
| 76 | + _tkagg.blit( |
| 77 | + photoimage.tk.interpaddr(), str(photoimage), dataptr, offsets, bboxptr) |
| 78 | + |
| 79 | + |
56 | 80 | class TimerTk(TimerBase): |
57 | 81 | ''' |
58 | 82 | Subclass of :class:`backend_bases.TimerBase` that uses Tk's timer events. |
|
0 commit comments