← 返回首页
#define PY_SSIZE_T_CLEAN
#include
#include
#include
#include
#define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION
#include "numpy/arrayobject.h"
#include
extern "C" {
// Simple addition test function (return = in1 + in2)
static PyObject* test_func(PyObject* self, PyObject* args) {
int input1, input2;
if (!PyArg_ParseTuple(args, "ii", &input1, &input2)) { // Arguments are 2 integer values ("ii")
return nullptr;
}
PyObject *return_value = PyLong_FromLong(input1 + input2); // Create object to return
return return_value;
}
// Simple image processing function (Inverse image color)
static PyObject* test_image_processing(PyObject* self, PyObject* args) {
PyArrayObject* input_image;
PyObject* output_image;
if(!PyArg_ParseTuple(args, "O!", &PyArray_Type, &input_image)) { // Argument is an Numpy array object
return nullptr;
}
output_image = PyArray_NewLikeArray(input_image, NPY_ANYORDER, NULL, 0); // Create object to return
int ndim = PyArray_NDIM(input_image); // Number of dimensions of the input Numpy array
npy_intp *shape;
shape = PyArray_SHAPE(input_image); // Shape of the input Numpy array (npy_intp[])
// Obtain data buffer pointers
char* in_buf = static_cast(PyArray_DATA(input_image)); // PyArray_DATA() will return void*
char* out_buf = static_cast(PyArray_DATA(reinterpret_cast(output_image)));
// Image processing
size_t C = shape[2];
size_t W = shape[1];
size_t H = shape[0];
for(size_t y=0; y