ITK  4.8.0
Insight Segmentation and Registration Toolkit
itkPyBuffer.h
Go to the documentation of this file.
1 /*=========================================================================
2  *
3  * Copyright Insight Software Consortium
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0.txt
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  *=========================================================================*/
18 
19 #ifndef __itkPyBuffer_h
20 #define __itkPyBuffer_h
21 
22 #include "itkObject.h"
23 #include "itkObjectFactory.h"
24 #include "itkImportImageFilter.h"
26 
27 
28 // The python header defines _POSIX_C_SOURCE without a preceding #undef
29 #undef _POSIX_C_SOURCE
30 #include <Python.h>
31 // This should eventually be replaced with something that uses the buffer-api
32 // like the implementation in SimpleITK
33 #include <numpy/arrayobject.h>
34 
35 
36 namespace itk
37 {
38 
49 template <typename TImage>
50 class PyBuffer
51 {
52 public:
54  typedef PyBuffer Self;
55 
57  typedef TImage ImageType;
58  typedef typename ImageType::PixelType PixelType;
59  typedef typename ImageType::SizeType SizeType;
60  typedef typename ImageType::IndexType IndexType;
61  typedef typename ImageType::RegionType RegionType;
62  typedef typename ImageType::PointType PointType;
63  typedef typename ImageType::SpacingType SpacingType;
64  typedef typename ImageType::Pointer ImagePointer;
66 
68  itkStaticConstMacro(ImageDimension, unsigned int, ImageType::ImageDimension);
69 
71 
75  static PyObject * GetArrayFromImage( ImageType * image, bool keepAxes = false);
76 
80  static const OutputImagePointer GetImageFromArray( PyObject *obj );
81 
82 protected:
83 
84 private:
85  PyBuffer(const Self&); // Purposely not implemented.
86  void operator=(const Self&); // Purposely not implemented.
87 };
88 
89 // This declaration is void of a definition, so that unsupported types
90 // will generate a compile time error.
91 template <class T> struct PyTypeTraits;
92 
93 template<> struct PyTypeTraits<double> { enum { value = NPY_DOUBLE }; };
94 template<> struct PyTypeTraits<float> { enum { value = NPY_FLOAT }; };
95 
96 template<> struct PyTypeTraits<std::complex<float> > { enum { value = NPY_COMPLEX64 }; };
97 template<> struct PyTypeTraits<std::complex<double> > { enum { value = NPY_COMPLEX128 }; };
98 
99 template<> struct PyTypeTraits<long long>{ enum { value = NPY_LONGLONG }; };
100 template<> struct PyTypeTraits<unsigned long long>{ enum { value = NPY_ULONGLONG }; };
101 
102 template<> struct PyTypeTraits<long>{ enum { value = NPY_LONG }; };
103 template<> struct PyTypeTraits<unsigned long>{ enum { value = NPY_ULONG }; };
104 
105 template<> struct PyTypeTraits<int>{ enum { value = NPY_INT }; };
106 template<> struct PyTypeTraits<unsigned int>{ enum { value = NPY_UINT }; };
107 
108 template<> struct PyTypeTraits<short>{ enum { value = NPY_SHORT }; };
109 template<> struct PyTypeTraits<unsigned short>{ enum { value = NPY_USHORT }; };
110 
111 template<> struct PyTypeTraits<signed char>{ enum { value = NPY_BYTE }; };
112 template<> struct PyTypeTraits<unsigned char>{ enum { value = NPY_UBYTE }; };
113 
114 } // namespace itk
115 
116 #ifndef ITK_MANUAL_INSTANTIATION
117 #include "itkPyBuffer.hxx"
118 #endif
119 
120 #endif // _itkPyBuffer_h
void operator=(const Self &)
ImageType::RegionType RegionType
Definition: itkPyBuffer.h:61
ImageType::PointType PointType
Definition: itkPyBuffer.h:62
PyBuffer(const Self &)
PyBuffer Self
Definition: itkPyBuffer.h:54
DefaultConvertPixelTraits< PixelType >::ComponentType ComponentType
Definition: itkPyBuffer.h:65
static const unsigned int ImageDimension
Definition: itkPyBuffer.h:68
TImage ImageType
Definition: itkPyBuffer.h:57
ImageType::SizeType SizeType
Definition: itkPyBuffer.h:59
Helper class for converting C buffers into python arrays.
Definition: itkPyBuffer.h:50
static const OutputImagePointer GetImageFromArray(PyObject *obj)
ImageType::Pointer ImagePointer
Definition: itkPyBuffer.h:64
static PyObject * GetArrayFromImage(ImageType *image, bool keepAxes=false)
ImageType::PixelType PixelType
Definition: itkPyBuffer.h:58
Image< ComponentType, ImageDimension >::Pointer OutputImagePointer
Definition: itkPyBuffer.h:70
ImageType::IndexType IndexType
Definition: itkPyBuffer.h:60
ImageType::SpacingType SpacingType
Definition: itkPyBuffer.h:63