00001
00026 #ifndef PYCVD_INTERFACE_HPP
00027 #define PYCVD_INTERFACE_HPP
00028
00029 #include <Python.h>
00030 #include <numpy/arrayobject.h>
00031 #include <sstream>
00032 #include <string>
00033 #include <cvd/image.h>
00034 #include <cvd/image_io.h>
00035 #include <cvd/convolution.h>
00036
00037 #include "types.h"
00038 #include "selector.h"
00039
00041
00042
00043
00045
00046 namespace PyCVD {
00047
00048 template <class T>
00049 PyArrayObject *fromBasicImageToNumpy(const CVD::BasicImage<T> &image) {
00050 npy_intp a[] = {image.size().y, image.size().x};
00051 PyArrayObject *retval = (PyArrayObject*)PyArray_SimpleNew(2, a, NumpyType<T>::num);
00052 T *data = (T*)retval->data;
00053 copy(image.begin(), image.end(), data);
00054 return retval;
00055 }
00056
00057 template <class T>
00058 CVD::BasicImage<T> allocateNumpyCVDImageSiblings(int width, int height,
00060 PyArrayObject **numpy) {
00061 npy_intp a[] = {height, width};
00062 *numpy = (PyArrayObject*)PyArray_SimpleNew(2, a, NumpyType<T>::num);
00063 return CVD::BasicImage<T>((T*)(*numpy)->data, CVD::ImageRef(width, height));
00064 }
00065
00066 template<class I>
00067 CVD::BasicImage<I> fromNumpyToBasicImage(PyObject *p, const std::string &n="") {
00068
00069 if (!PyArray_Check(p)
00070 || PyArray_NDIM(p) != 2
00071 || !PyArray_ISCONTIGUOUS(p)
00072 || PyArray_TYPE(p) != NumpyType<I>::num) {
00073 throw std::string(n + " must be a contiguous array of " + NumpyType<I>::name() + " (type code " + NumpyType<I>::code() + ")!");
00074 }
00075
00076 PyArrayObject* image = (PyArrayObject*)p;
00077
00078 int sm = image->dimensions[1];
00079 int sn = image->dimensions[0];
00080 CVD::BasicImage <I> img((I*)image->data, CVD::ImageRef(sm, sn));
00081 return img;
00082 }
00083
00084 template<class I>
00085 CVD::BasicImage<I> fromNumpyToBasicImage(PyArrayObject *image, const std::string& n="") {
00086
00087 if (PyArray_NDIM(image) != 2
00088 || !PyArray_ISCONTIGUOUS(image)
00089 || PyArray_TYPE(image) != NumpyType<I>::num) {
00090 throw std::string(n + " must be a contiguous array of " + NumpyType<I>::name() + " (type code " + NumpyType<I>::code() + ")!");
00091 }
00092
00093 int sm = image->dimensions[1];
00094 int sn = image->dimensions[0];
00095 CVD::BasicImage <I> img((I*)image->data, CVD::ImageRef(sm, sn));
00096 return img;
00097 }
00098
00099
00100 }
00101
00102
00103 #endif