Newer
Older
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
// Setup for defining wrapped commands
#include "PyWrapCommand.h"
#define INSTANCE_COMMANDS
#include "PyWrapDef.h"
#include "../WrapCmds/CommandList.h"
#undef INSTANCE_COMMANDS
#define BUILD_COMMANDS
#include "PyWrapDef.h"
#include "../WrapCmds/CommandList.h"
#undef BUILD_COMMANDS
void setupDims(PyArrayObject* im, ImageDimensions& dimsOut)
{
dimsOut.dims = Vec<size_t>(1);
dimsOut.chan = 1;
dimsOut.frame = 1;
int numDims = PyArray_NDIM(im);
const npy_intp* DIMS = PyArray_DIMS(im);
for ( int i=0; i < std::min(numDims, 3); ++i )
dimsOut.dims.e[i] = (size_t)DIMS[i];
if ( numDims > 3 )
dimsOut.chan = (unsigned int)DIMS[3];
if ( numDims > 4 )
dimsOut.frame = (unsigned int)DIMS[4];
}
template <typename T, typename U>
void converter(void* in, void* out, size_t len)
{
for ( int i=0; i < len; ++i )
((U*)out)[i] = static_cast<U>(((T*)in)[i]);
}
bool pyarrayToVec(PyObject* ar, Vec<double>& outVec)
{
int ndim = PyArray_NDIM(ar);
if ( ndim > 1 )
return false;
int array_size = PyArray_DIM(ar, 0);
if ( array_size != 3 )
return false;
if ( PyArray_TYPE(ar) == NPY_UINT8 )
converter<uint8_t,double>(PyArray_DATA(ar), outVec.e, array_size);
else if ( PyArray_TYPE(ar) == NPY_UINT16 )
converter<uint16_t, double>(PyArray_DATA(ar), outVec.e, array_size);
else if ( PyArray_TYPE(ar) == NPY_INT16 )
converter<int16_t, double>(PyArray_DATA(ar), outVec.e, array_size);
else if ( PyArray_TYPE(ar) == NPY_UINT32 )
converter<uint32_t, double>(PyArray_DATA(ar), outVec.e, array_size);
else if ( PyArray_TYPE(ar) == NPY_INT32 )
converter<int32_t, double>(PyArray_DATA(ar), outVec.e, array_size);
else if ( PyArray_TYPE(ar) == NPY_FLOAT )
converter<float, double>(PyArray_DATA(ar), outVec.e, array_size);
else if ( PyArray_TYPE(ar) == NPY_DOUBLE )
converter<double, double>(PyArray_DATA(ar), outVec.e, array_size);
return true;
}
bool pylistToVec(PyObject* list, Vec<double>& outVec)
{
Py_ssize_t list_size = PyList_Size(list);
if ( list_size != 3 )
return false;
for ( int i=0; i < list_size; ++i )
{
PyObject* item = PyList_GetItem(list, i);
if ( PyLong_Check(item) )
outVec.e[i] = PyLong_AsDouble(item);
else if ( PyFloat_Check(item) )
outVec.e[i] = PyFloat_AsDouble(item);
else
return false;
}
return true;
}
bool pyobjToVec(PyObject* list_array, Vec<double>& outVec)
{
if ( PyList_Check(list_array) )
return pylistToVec(list_array, outVec);
else if ( PyArray_Check(list_array) )
return pyarrayToVec(list_array, outVec);
return false;
}
// Info Command (unimplemented)
const char PyWrapInfo::docString[] = "Not implemented for Python bindings.";
PyObject* PyWrapInfo::execute(PyObject* self, PyObject* args)
{
PyErr_SetString(PyExc_RuntimeWarning, "Info() not implemented for Python!");
return nullptr;
}
// Help Command (unimplemented)
const char PyWrapHelp::docString[] = "Not implemented for Python bindings.";
PyObject* PyWrapHelp::execute(PyObject* self, PyObject* args)
{
PyErr_SetString(PyExc_RuntimeWarning, "Help() not implemented for Python!");
return nullptr;
}