forked from matplotlib/matplotlib
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path_tkagg.cpp
More file actions
146 lines (125 loc) · 3.47 KB
/
_tkagg.cpp
File metadata and controls
146 lines (125 loc) · 3.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
/*
* The Python Imaging Library.
* $Id$
*
*/
/* This is needed for (at least) Tk 8.4.1, otherwise the signature of
** Tk_PhotoPutBlock changes.
*/
#define USE_COMPOSITELESS_PHOTO_PUT_BLOCK
#include <Python.h>
#include <stdlib.h>
extern "C" {
#ifdef __APPLE__
# include <Tcl/tcl.h>
# include <Tk/tk.h>
#else
# include <tk.h>
#endif
};
#include "_backend_agg.h"
typedef struct {
PyObject_HEAD
Tcl_Interp* interp;
} TkappObject;
static int
PyAggImagePhoto(ClientData clientdata, Tcl_Interp* interp,
int argc, char **argv)
{
Tk_PhotoHandle photo;
Tk_PhotoImageBlock block;
RendererAggObject* aggRenderer;
long mode;
long nval;
if (argc != 4) {
Tcl_AppendResult(interp, "usage: ", argv[0],
" destPhoto srcImage", (char *) NULL);
return TCL_ERROR;
}
/* get Tcl PhotoImage handle */
photo = Tk_FindPhoto(interp, argv[1]);
if (photo == NULL) {
Tcl_AppendResult(interp, "destination photo must exist", (char *) NULL);
return TCL_ERROR;
}
/* get array (or object that can be converted to array) pointer */
aggRenderer = (RendererAggObject *) atol(argv[2]);
/* XXX insert aggRenderer type check */
/* get array mode (0=mono, 1=rgb, 2=rgba) */
mode = atol(argv[3]);
if ((mode != 0) && (mode != 1) && (mode != 2)) {
Tcl_AppendResult(interp, "illegal image mode", (char *) NULL);
return TCL_ERROR;
}
/* setup tkblock */
block.pixelSize = 1;
if (mode == 0) {
block.offset[0]= block.offset[1] = block.offset[2] =0;
nval = 1;
} else {
block.offset[0] = 0;
block.offset[1] = 1;
block.offset[2] = 2;
if (mode == 1) {
block.offset[3] = 0;
block.pixelSize = 3;
nval = 3;
} else {
block.offset[3] = 3;
block.pixelSize = 4;
nval = 4;
}
}
block.width = aggRenderer->rbase->width();
block.height = aggRenderer->rbase->height();
block.pitch = block.width * nval;
block.pixelPtr = aggRenderer->buffer;
/* Clear current contents */
Tk_PhotoBlank(photo);
/* Copy opaque block to photo image, and leave the rest to TK */
Tk_PhotoPutBlock(photo, &block, 0, 0, block.width, block.height);
return TCL_OK;
}
static PyObject *
_pyobj_addr(PyObject* self, PyObject* args)
{
PyObject *pyobj;
if (!PyArg_ParseTuple(args, "O", &pyobj))
return NULL;
return Py_BuildValue("l", (long) pyobj);
}
static PyObject*
_tkinit(PyObject* self, PyObject* args)
{
Tcl_Interp* interp;
TkappObject* app;
long arg;
int is_interp;
if (!PyArg_ParseTuple(args, "li", &arg, &is_interp))
return NULL;
if (is_interp) {
interp = (Tcl_Interp*) arg;
} else {
/* Do it the hard way. This will break if the TkappObject
layout changes */
app = (TkappObject*) arg;
interp = app->interp;
}
/* This will bomb if interp is invalid... */
Tcl_CreateCommand(interp, "PyAggImagePhoto",
(Tcl_CmdProc *) PyAggImagePhoto,
(ClientData) 0, (Tcl_CmdDeleteProc*) NULL);
Py_INCREF(Py_None);
return Py_None;
}
static PyMethodDef functions[] = {
/* Tkinter interface stuff */
{"_pyobj_addr", (PyCFunction)_pyobj_addr, 1},
{"tkinit", (PyCFunction)_tkinit, 1},
{NULL, NULL} /* sentinel */
};
extern "C"
DL_EXPORT(void) init_tkagg(void)
{
Py_InitModule("_tkagg", functions);
}