42 const void * mitkBufferPtr;
44 std::vector< unsigned int > size;
47 unsigned int dimension;
55 PyObject * memoryView = NULL;
57 memset(&pyBuffer, 0,
sizeof(Py_buffer));
59 unsigned int accumulatorValue = 1;
61 if( !PyArg_ParseTuple( args,
"O", &pyImage ) )
65 res = SWIG_ConvertPtr( pyImage, &voidImage, SWIGTYPE_p_mitk__Image, 0 );
69 res = SWIG_ConvertPtr(pyImage, &voidImage, SWIGTYPE_p_itk__SmartPointerT_mitk__Image_t, 0);
72 SWIG_exception_fail(SWIG_ArgError(res),
"in method 'GetByteArrayFromImage', argument needs to be of type 'sitk::Image *'");
75 voidImage =
reinterpret_cast<void*
>(tmpImage.GetPointer());
77 mitkImage =
reinterpret_cast< mitk::Image *
>( voidImage );
79 mitkBufferPtr = mitkImage->
GetData();
83 for (
int i = 0; i < dimension; ++i)
94 len = std::accumulate( size.begin(), size.end(), accumulatorValue, std::multiplies<unsigned int>() );
97 if (PyBuffer_FillInfo(&pyBuffer, NULL, (
void*)mitkBufferPtr, len,
true, PyBUF_CONTIG_RO)!=0)
101 memoryView = PyMemoryView_FromBuffer(&pyBuffer);
103 PyBuffer_Release(&pyBuffer);
107 Py_XDECREF( memoryView );
118 PyObject * pyImage = NULL;
121 Py_ssize_t buffer_len;
123 memset(&pyBuffer, 0,
sizeof(Py_buffer));
126 void * mitkBufferPtr = NULL;
127 size_t pixelSize = 1;
129 unsigned int dimension = 0;
130 std::vector< unsigned int > size;
133 unsigned int accuValue = 1;
138 if (!PyArg_ParseTuple( args,
"s*O", &pyBuffer, &pyImage ) )
142 #ifdef PY_SSIZE_T_CLEAN 143 typedef Py_ssize_t bufSizeType;
145 typedef int bufSizeType;
151 if( !PyArg_ParseTuple( args,
"s#O", &buffer, &_len, &pyImage ) )
159 if ( PyBuffer_IsContiguous( &pyBuffer,
'C' ) != 1 )
161 PyBuffer_Release( &pyBuffer );
162 PyErr_SetString( PyExc_TypeError,
"A C Contiguous buffer object is required." );
165 buffer_len = pyBuffer.len;
166 buffer = pyBuffer.buf;
173 res = SWIG_ConvertPtr( pyImage, &voidImage, SWIGTYPE_p_mitk__Image, 0 );
177 res = SWIG_ConvertPtr(pyImage, &voidImage, SWIGTYPE_p_itk__SmartPointerT_mitk__Image_t, 0);
180 SWIG_exception_fail(SWIG_ArgError(res),
"in method 'GetByteArrayFromImage', argument needs to be of type 'sitk::Image *'");
183 voidImage =
reinterpret_cast<void*
>(tmpImage.GetPointer());
185 mitkImage =
reinterpret_cast< mitk::Image *
>(voidImage);
190 mitkBufferPtr = mitkImage->
GetData();
193 catch(
const std::exception &e )
195 std::string msg =
"Exception thrown in MITK new Image: ";
197 PyErr_SetString( PyExc_RuntimeError, msg.c_str() );
202 for (
int i = 0; i < dimension; ++i)
213 len = std::accumulate(size.begin(), size.end(), accuValue, std::multiplies<unsigned int>());
216 if ( buffer_len != len )
218 PyErr_SetString( PyExc_RuntimeError,
"Size mismatch of image and Buffer." );
222 memcpy( (
void *)mitkBufferPtr, buffer, len );
225 PyBuffer_Release( &pyBuffer );
229 PyBuffer_Release( &pyBuffer );
vcl_size_t GetBitsPerComponent() const
Get the number of bits per components.
const mitk::PixelType GetPixelType(int n=0) const
Returns the PixelType of channel n.
vcl_size_t GetNumberOfComponents() const
Get the number of components of which each element consists.
static PyObject * mitk_GetMemoryViewFromImage(PyObject *SWIGUNUSEDPARM(self), PyObject *args)
static PyObject * mitk_SetImageFromArray(PyObject *SWIGUNUSEDPARM(self), PyObject *args)
unsigned int GetDimension() const
Get dimension of the image.
Image class for storing images.
virtual void * GetData()
Get the data vector of the complete image, i.e., of all channels linked together. ...