| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -7,49 +7,32 @@ | |||
| 7 | 7 | import numpy as np | |
| 8 | 8 | ||
| 9 | 9 | from matplotlib.backends import _tkagg | |
| 10 | - from platform import python_implementation | ||
| 11 | 10 | ||
| 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: | ||
| 39 | 27 | try: | |
| 40 | - tk.call( | ||
| 41 | - "PyAggImagePhoto", photoimage, | ||
| 42 | - id(data), colormode, id(bbox_array)) | ||
| 43 | - except Tk.TclError: | ||
| 44 | 28 | 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 | ||
| 53 | 36 | ||
| 54 | 37 | def test(aggimage): | |
| 55 | 38 | import time | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1513,13 +1513,11 @@ def runtime_check(self): | |||
| 1513 | 1513 | ||
| 1514 | 1514 | def get_extension(self): | |
| 1515 | 1515 | sources = [ | |
| 1516 | - 'src/py_converters.cpp', | ||
| 1517 | 1516 | 'src/_tkagg.cpp' | |
| 1518 | 1517 | ] | |
| 1519 | 1518 | ||
| 1520 | 1519 | ext = make_extension('matplotlib.backends._tkagg', sources) | |
| 1521 | 1520 | self.add_flags(ext) | |
| 1522 | - Numpy().add_flags(ext) | ||
| 1523 | 1521 | LibAgg().add_flags(ext, add_sources=False) | |
| 1524 | 1522 | return ext | |
| 1525 | 1523 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -13,16 +13,15 @@ | |||
| 13 | 13 | #include <cstdio> | |
| 14 | 14 | #include <sstream> | |
| 15 | 15 | ||
| 16 | - #include "py_converters.h" | ||
| 17 | - | ||
| 18 | 16 | // Include our own excerpts from the Tcl / Tk headers | |
| 19 | 17 | #include "_tkmini.h" | |
| 20 | 18 | ||
| 21 | 19 | #if defined(_MSC_VER) | |
| 22 | - # define SIZE_T_FORMAT "%Iu" | ||
| 20 | + # define IMG_FORMAT "%Iu %d %d" | ||
| 23 | 21 | #else | |
| 24 | - # define SIZE_T_FORMAT "%zu" | ||
| 22 | + # define IMG_FORMAT "%zu %d %d" | ||
| 25 | 23 | #endif | |
| 24 | + # define BBOX_FORMAT "%f %f %f %f" | ||
| 26 | 25 | ||
| 27 | 26 | typedef struct | |
| 28 | 27 | { | |
@@ -44,16 +43,15 @@ static int PyAggImagePhoto(ClientData clientdata, Tcl_Interp *interp, int | |||
| 44 | 43 | { | |
| 45 | 44 | Tk_PhotoHandle photo; | |
| 46 | 45 | Tk_PhotoImageBlock block; | |
| 47 | - PyObject *bufferobj; | ||
| 48 | 46 | ||
| 49 | 47 | // vars for blitting | |
| 50 | - PyObject *bboxo; | ||
| 51 | 48 | ||
| 52 | - size_t aggl, bboxl; | ||
| 49 | + size_t pdata; | ||
| 50 | + int wdata, hdata; | ||
| 51 | + float x1, x2, y1, y2; | ||
| 53 | 52 | bool has_bbox; | |
| 54 | - uint8_t *destbuffer; | ||
| 53 | + uint8_t *destbuffer, *buffer; | ||
| 55 | 54 | int destx, desty, destwidth, destheight, deststride; | |
| 56 | - //unsigned long tmp_ptr; | ||
| 57 | 55 | ||
| 58 | 56 | long mode; | |
| 59 | 57 | long nval; | |
@@ -73,24 +71,14 @@ static int PyAggImagePhoto(ClientData clientdata, Tcl_Interp *interp, int | |||
| 73 | 71 | TCL_APPEND_RESULT(interp, "destination photo must exist", (char *)NULL); | |
| 74 | 72 | return TCL_ERROR; | |
| 75 | 73 | } | |
| 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); | ||
| 89 | 79 | return TCL_ERROR; | |
| 90 | 80 | } | |
| 91 | - int srcheight = buffer.dim(0); | ||
| 92 | - | ||
| 93 | - /* XXX insert aggRenderer type check */ | ||
| 81 | + buffer = (uint8_t*)pdata; | ||
| 94 | 82 | ||
| 95 | 83 | /* get array mode (0=mono, 1=rgb, 2=rgba) */ | |
| 96 | 84 | mode = atol(argv[3]); | |
@@ -100,39 +88,33 @@ static int PyAggImagePhoto(ClientData clientdata, Tcl_Interp *interp, int | |||
| 100 | 88 | } | |
| 101 | 89 | ||
| 102 | 90 | /* 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) { | ||
| 115 | 92 | has_bbox = true; | |
| 93 | + } | ||
| 94 | + else { | ||
| 95 | + has_bbox = false; | ||
| 96 | + } | ||
| 116 | 97 | ||
| 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; | ||
| 121 | 104 | deststride = 4 * destwidth; | |
| 122 | 105 | ||
| 123 | - destbuffer = new agg::int8u[deststride * destheight]; | ||
| 106 | + destbuffer = new uint8_t[deststride * destheight]; | ||
| 124 | 107 | if (destbuffer == NULL) { | |
| 125 | 108 | TCL_APPEND_RESULT(interp, "could not allocate memory", (char *)NULL); | |
| 126 | 109 | return TCL_ERROR; | |
| 127 | 110 | } | |
| 128 | 111 | ||
| 129 | 112 | for (int i = 0; i < destheight; ++i) { | |
| 130 | 113 | memcpy(destbuffer + (deststride * i), | |
| 131 | - &buffer(i + desty, destx, 0), | ||
| 114 | + &buffer[(i + desty) * srcstride + (destx * 4)], | ||
| 132 | 115 | deststride); | |
| 133 | 116 | } | |
| 134 | 117 | } else { | |
| 135 | - has_bbox = false; | ||
| 136 | 118 | destbuffer = NULL; | |
| 137 | 119 | destx = desty = destwidth = destheight = deststride = 0; | |
| 138 | 120 | } | |
@@ -168,10 +150,10 @@ static int PyAggImagePhoto(ClientData clientdata, Tcl_Interp *interp, int | |||
| 168 | 150 | delete[] destbuffer; | |
| 169 | 151 | ||
| 170 | 152 | } else { | |
| 171 | - block.width = buffer.dim(1); | ||
| 172 | - block.height = buffer.dim(0); | ||
| 153 | + block.width = wdata; | ||
| 154 | + block.height = hdata; | ||
| 173 | 155 | block.pitch = (int)block.width * nval; | |
| 174 | - block.pixelPtr = buffer.data(); | ||
| 156 | + block.pixelPtr = buffer; | ||
| 175 | 157 | ||
| 176 | 158 | /* Clear current contents */ | |
| 177 | 159 | TK_PHOTO_BLANK(photo); | |
@@ -457,15 +439,11 @@ PyMODINIT_FUNC PyInit__tkagg(void) | |||
| 457 | 439 | ||
| 458 | 440 | m = PyModule_Create(&_tkagg_module); | |
| 459 | 441 | ||
| 460 | - import_array(); | ||
| 461 | - | ||
| 462 | 442 | return (load_tkinter_funcs() == 0) ? m : NULL; | |
| 463 | 443 | } | |
| 464 | 444 | #else | |
| 465 | 445 | PyMODINIT_FUNC init_tkagg(void) | |
| 466 | 446 | { | |
| 467 | - import_array(); | ||
| 468 | - | ||
| 469 | 447 | Py_InitModule("_tkagg", functions); | |
| 470 | 448 | ||
| 471 | 449 | load_tkinter_funcs(); | |
| Back | FazBrowse Home | New Git URL |
0 commit comments