-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpython_filter.cpp
More file actions
371 lines (332 loc) · 11.3 KB
/
python_filter.cpp
File metadata and controls
371 lines (332 loc) · 11.3 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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
/*a Copyright
This file 'python_filter.cpp' copyright Gavin J Stark 2016
This is free software; you can redistribute it and/or modify it however you wish,
with no obligations
This software is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even implied warranty of MERCHANTABILITY
or FITNESS FOR A PARTICULAR PURPOSE.
*/
/*a Includes
*/
#include <Python.h>
#include "python_filter.h"
#include "python_lens_projection.h"
#include "python_texture.h"
#include "filter.h"
/*a Defines
*/
/*a Types
*/
/*t t_PyObject_filter
*/
typedef struct {
PyObject_HEAD
c_filter *filter;
t_exec_context ec;
} t_PyObject_filter;
/*a Forward function declarations
*/
static int python_filter_init(PyObject *self, PyObject *args, PyObject *kwds);
static PyObject *python_filter_new(PyTypeObject *type, PyObject *args, PyObject *kwds);
static void python_filter_dealloc(PyObject *self);
static PyObject *python_filter_getattr(PyObject *self, char *attr);
static PyObject *python_filter_method_compile(PyObject* self);
static PyObject *python_filter_method_exec(PyObject* self);
static PyObject *python_filter_method_define(PyObject* self, PyObject* args, PyObject *kwds);
static PyObject *python_filter_method_parameter(PyObject* self, PyObject* args, PyObject *kwds);
static PyObject *python_filter_method_textures(PyObject* self, PyObject* args, PyObject *kwds);
static PyObject *python_filter_method_projections(PyObject* self, PyObject* args, PyObject *kwds);
/*a Static variables
*/
/*v python_filter_methods
*/
PyMethodDef python_filter_methods[] = {
{"projections", (PyCFunction)python_filter_method_projections, METH_VARARGS|METH_KEYWORDS},
{"textures", (PyCFunction)python_filter_method_textures, METH_VARARGS|METH_KEYWORDS},
{"define", (PyCFunction)python_filter_method_define, METH_VARARGS|METH_KEYWORDS},
{"parameter", (PyCFunction)python_filter_method_parameter, METH_VARARGS|METH_KEYWORDS},
{"compile", (PyCFunction)python_filter_method_compile, METH_NOARGS},
{"execute", (PyCFunction)python_filter_method_exec, METH_NOARGS},
{NULL, NULL},
};
/*v PyTypeObject_filter
*/
static PyTypeObject PyTypeObject_filter = {
PyObject_HEAD_INIT(NULL)
0, // variable size
"filter", // type name
sizeof(t_PyObject_filter), // basic size
0, // item size - zero for static sized object types
python_filter_dealloc, //py_engine_dealloc, /*tp_dealloc*/
0, /*tp_print - basically deprecated */
python_filter_getattr, /*tp_getattr*/
0, /*tp_setattr*/
0, /*tp_compare*/
0, /*tp_repr - ideally a represenation that is python that recreates this object */
0, /*tp_as_number*/
0, /*tp_as_sequence*/
0, /*tp_as_mapping*/
0, /*tp_hash */
0, /* tp_call - called if the object itself is invoked as a method */
0, /* tp_str */
0, /*tp_getattro*/
0, /*tp_setattro*/
0, /*tp_as_buffer*/
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/
"Filter object", /* tp_doc */
0, /* tp_traverse */
0, /* tp_clear */
0, /* tp_richcompare */
0, /* tp_weaklistoffset */
0, /* tp_iter */
0, /* tp_iternext */
python_filter_methods, /* tp_methods */
0, //python_quaternion_members, /* tp_members */
0, /* tp_getset */
0, /* tp_base */
0, /* tp_dict */
0, /* tp_descr_get */
0, /* tp_descr_set */
0, /* tp_dictoffset */
python_filter_init, /* tp_init */
0, /* tp_alloc */
python_filter_new, /* tp_new */
};
/*a Python filter methods
*/
/*f python_filter_method_compile
*/
static PyObject *
python_filter_method_compile(PyObject* self)
{
t_PyObject_filter *py_obj = (t_PyObject_filter *)self;
if (py_obj->filter) {
int err;
err = py_obj->filter->compile();
if (err!=0) {
PyErr_SetString(PyExc_RuntimeError, py_obj->filter->parse_error);
return NULL;
}
}
Py_RETURN_NONE;
}
/*f python_filter_method_exec
*/
static PyObject *
python_filter_method_exec(PyObject* self)
{
t_PyObject_filter *py_obj = (t_PyObject_filter *)self;
if (py_obj->filter) {
int err;
py_obj->ec.use_ids = 0;
err = py_obj->filter->execute(&py_obj->ec);
gl_get_errors("Filter executed");
}
Py_RETURN_NONE;
}
/*f python_filter_method_define
*/
static PyObject *
python_filter_method_define(PyObject* self, PyObject* args, PyObject *kwds)
{
t_PyObject_filter *py_obj = (t_PyObject_filter *)self;
const char *name = NULL;
const char *value = NULL;
int remove=0;
static const char *kwlist[] = {"name", "value", "remove", NULL};
if (!PyArg_ParseTupleAndKeywords(args, kwds, "s|si", (char **)kwlist,
&name, &value, &remove))
return NULL;
if (py_obj->filter) {
char buffer[256];
snprintf(buffer, sizeof(buffer), "-D%s", name);
buffer[sizeof(buffer)-1] = 0;
if (remove) {
py_obj->filter->unset_parameter(buffer);
} else {
py_obj->filter->set_parameter(buffer, value?value:"");
}
}
Py_RETURN_NONE;
}
/*f python_filter_method_parameter
*/
static PyObject *
python_filter_method_parameter(PyObject* self, PyObject* args, PyObject *kwds)
{
t_PyObject_filter *py_obj = (t_PyObject_filter *)self;
const char *name = NULL;
PyObject *value = NULL;
static const char *kwlist[] = {"name", "value", NULL};
if (!PyArg_ParseTupleAndKeywords(args, kwds, "sO", (char **)kwlist,
&name, &value))
return NULL;
if (py_obj->filter) {
if (PyFloat_Check(value)) {
py_obj->filter->set_parameter(name, PyFloat_AsDouble(value));
} else if (PyInt_Check(value)) {
py_obj->filter->set_parameter(name, (int)PyInt_AsLong(value));
} else if (PyLong_Check(value)) {
py_obj->filter->set_parameter(name, (int)PyLong_AsLong(value));
} else {
PyErr_SetString(PyExc_RuntimeError, "Need float or int to set parameter");
}
}
Py_RETURN_NONE;
}
/*f python_filter_method_textures
*/
static PyObject *
python_filter_method_textures(PyObject* self, PyObject* args, PyObject *kwds)
{
t_PyObject_filter *py_obj = (t_PyObject_filter *)self;
PyObject *textures;
static const char *kwlist[] = {"textures", NULL};
if (!PyArg_ParseTupleAndKeywords(args, kwds, "O", (char **)kwlist,
&textures))
return NULL;
if (py_obj->filter) {
PyObject *tuple = PySequence_Fast(textures, "textures must be a tuple or list");
int len = PySequence_Size(textures);
for (int i=0; i<len; i++) {
PyObject *item = PySequence_Fast_GET_ITEM(tuple, i);
t_texture_ptr texture;
if (python_texture_data(item, 0, (void *)&texture)) {
py_obj->filter->bind_texture(i, texture);
} else {
PyErr_SetString(PyExc_RuntimeError, "Non-texture object in tuple");
Py_DECREF(tuple);
return NULL;
}
}
Py_DECREF(tuple);
}
Py_RETURN_NONE;
}
/*f python_filter_method_projections
*/
static PyObject *
python_filter_method_projections(PyObject* self, PyObject* args, PyObject *kwds)
{
t_PyObject_filter *py_obj = (t_PyObject_filter *)self;
PyObject *from, *to;
static const char *kwlist[] = {"from", "to", NULL};
if (!PyArg_ParseTupleAndKeywords(args, kwds, "OO", (char **)kwlist,
&from, &to))
return NULL;
if (py_obj->filter) {
class c_lens_projection *to_proj, *from_proj;
if ( (python_lens_projection_data(from, 0, (void *)&from_proj)) &&
(python_lens_projection_data(to, 0, (void *)&to_proj)) ) {
py_obj->filter->bind_projection(0, from_proj);
py_obj->filter->bind_projection(1, to_proj);
} else {
PyErr_SetString(PyExc_RuntimeError, "Non-projection object in tuple");
return NULL;
}
}
Py_RETURN_NONE;
}
/*f python_filter_dealloc
*/
static void
python_filter_dealloc(PyObject *self)
{
t_PyObject_filter *py_obj = (t_PyObject_filter *)self;
if (py_obj->filter) {
delete(py_obj->filter);
py_obj->filter = NULL;
}
}
/*f python_filter_getattr
*/
static PyObject *
python_filter_getattr(PyObject *self, char *attr)
{
t_PyObject_filter *py_obj = (t_PyObject_filter *)self;
if (py_obj->filter) {
c_filter *f = py_obj->filter;
if (!strcmp(attr, "times")) {
unsigned long long int times[MAX_FILTER_TIMERS];
for (int i=0; i<MAX_FILTER_TIMERS; i++) {
times[i] = SL_TIMER_VALUE(f->timers[i]);
}
return Py_BuildValue("KKKK", times[0], times[1], times[2], times[3]);
}
}
if (!strcmp(attr, "num_points")) {
return PyInt_FromLong(py_obj->ec.num_points);
}
if (!strcmp(attr, "points")) {
if (py_obj->ec.points) {
PyObject *list;
list = PyList_New(0);
for (int i=0; i<py_obj->ec.num_points; i++) {
t_point_value *pv;
pv = &(py_obj->ec.points[i]);
PyList_Append(list,
Py_BuildValue("iifff", pv->x, pv->y, (double)pv->value, (double)pv->vec_x, (double)pv->vec_y)
); // CAN FAIL
}
return list;
}
Py_RETURN_NONE;
}
return Py_FindMethod(python_filter_methods, self, attr);
}
/*a Python object
*/
/*f python_filter_new
*/
static PyObject *
python_filter_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
{
t_PyObject_filter *py_obj;
py_obj = (t_PyObject_filter *)type->tp_alloc(type, 0);
if (py_obj) {
py_obj->filter = NULL;
}
return (PyObject *)py_obj;
}
/*f python_filter_init
*/
static int
python_filter_init(PyObject *self, PyObject *args, PyObject *kwds)
{
t_PyObject_filter *py_obj = (t_PyObject_filter *)self;
const char *filter = NULL;
static const char *kwlist[] = {"filter", NULL};
if (!PyArg_ParseTupleAndKeywords(args, kwds, "s", (char **)kwlist,
&filter))
return -1;
py_obj->filter = NULL;
if (filter) {
py_obj->filter = filter_from_string(filter);
if (!py_obj->filter) {
PyErr_SetString(PyExc_RuntimeError, "Failed to create filter");
return -1;
}
}
if (py_obj->filter) {
if (py_obj->filter->parse_error) {
PyErr_SetString(PyExc_RuntimeError, py_obj->filter->parse_error);
return -1;
}
}
return 0;
}
/*f python_filter_init_premodule
*/
int python_filter_init_premodule(void)
{
if (PyType_Ready(&PyTypeObject_filter) < 0)
return -1;
return 0;
}
/*f python_filter_init_postmodule
*/
void python_filter_init_postmodule(PyObject *module)
{
Py_INCREF(&PyTypeObject_filter);
PyModule_AddObject(module, "filter", (PyObject *)&PyTypeObject_filter);
}