MCPcopy Create free account
hub / github.com/SimpleITK/SimpleITK / sitk_SetImageFromArray

Function sitk_SetImageFromArray

Wrapping/Python/sitkNumpyArrayConversion.cxx:39–114  ·  view source on GitHub ↗

An internal function that performs a deep copy of the image buffer * into a python byte array. The byte array can later be converted * into a numpy array with the frombuffer method. */

Source from the content-addressed store, hash-verified

37 * into a numpy array with the frombuffer method.
38 */
39 static PyObject *
40 sitk_SetImageFromArray(PyObject * SWIGUNUSEDPARM(self), PyObject * args)
41 {
42 PyObject * pyObj = NULL;
43 PyObject * pyImage = NULL;
44
45 Py_buffer pyBuffer;
46 memset(&pyBuffer, 0, sizeof(Py_buffer));
47
48 sitk::Image * sitkImage = NULL;
49 void * sitkBufferPtr = NULL;
50
51 size_t len = 1;
52
53 // We wish to support both the new PEP3118 buffer interface and the
54 // older. So we first try to parse the arguments with the new buffer
55 // protocol, then the old.
56 if (!PyArg_ParseTuple(args, "OO", &pyObj, &pyImage))
57 {
58 return NULL;
59 }
60 if (PyObject_GetBuffer(pyObj, &pyBuffer, PyBUF_FULL_RO) != 0)
61 {
62 return NULL;
63 }
64
65
66 /* Cast over to a sitk Image. */
67 {
68 void * voidImage;
69 int res = 0;
70 res = SWIG_ConvertPtr(pyImage, &voidImage, SWIGTYPE_p_itk__simple__Image, 0);
71 if (!SWIG_IsOK(res))
72 {
73 SWIG_exception_fail(SWIG_ArgError(res),
74 "in method 'SetImageFromArray', argument needs to be of type 'sitk::Image *'");
75 }
76 sitkImage = reinterpret_cast<sitk::Image *>(voidImage);
77 }
78
79 try
80 {
81 if (sitkImage->GetPixelIDValue() == sitk::sitkUnknown)
82 {
83 PyErr_SetString(PyExc_RuntimeError, "Unknown pixel type.");
84 SWIG_fail;
85 }
86
87 sitkBufferPtr = sitkImage->GetBufferAsVoid();
88 }
89 catch (const std::exception & e)
90 {
91 std::string msg = "Exception thrown in SimpleITK new Image: ";
92 msg += e.what();
93 PyErr_SetString(PyExc_RuntimeError, msg.c_str());
94 goto fail;
95 }
96

Callers

nothing calls this directly

Calls 6

GetBufferAsVoidMethod · 0.80
whatMethod · 0.80
GetNumberOfPixelsMethod · 0.80
GetPixelIDValueMethod · 0.45

Tested by

no test coverage detected