Diff of /src/itkPyVnl.hxx [000000] .. [5d12a0]

Switch to unified view

a b/src/itkPyVnl.hxx
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
#ifndef itkPyVnl_hxx
19
#define itkPyVnl_hxx
20
21
#include "itkPyVnl.h"
22
#include <stdexcept>
23
24
namespace itk
25
{
26
27
template<class TElement>
28
PyObject *
29
PyVnl<TElement>
30
::_GetArrayViewFromVnlVector( VectorType * vector)
31
{
32
  PyObject *                  memoryView    = NULL;
33
  Py_buffer                   pyBuffer;
34
  memset(&pyBuffer, 0, sizeof(Py_buffer));
35
36
  size_t                      elementSize   = sizeof(DataType);
37
  int                         res           = 0;
38
39
  if( !vector )
40
    {
41
    throw std::runtime_error("Input vector is null");
42
    }
43
44
  DataType *buffer = vector->data_block();
45
46
  void * vectorBuffer = (void *)( buffer );
47
48
  // Computing the length of data
49
  Py_ssize_t len = vector->size();
50
  len *= elementSize;
51
52
  res = PyBuffer_FillInfo(&pyBuffer, NULL, (void*)vectorBuffer, len, 0, PyBUF_CONTIG);
53
  memoryView = PyMemoryView_FromBuffer(&pyBuffer);
54
55
  PyBuffer_Release(&pyBuffer);
56
57
  return memoryView;
58
}
59
60
template<class TElement>
61
const typename PyVnl<TElement>::VectorType
62
PyVnl<TElement>
63
::_GetVnlVectorViewFromArray( PyObject *arr, PyObject *shape)
64
{
65
  PyObject *                  obj           = NULL;
66
  PyObject *                  shapeseq      = NULL;
67
  PyObject *                  item          = NULL;
68
69
  Py_ssize_t                  bufferLength;
70
  Py_buffer                   pyBuffer;
71
  memset(&pyBuffer, 0, sizeof(Py_buffer));
72
73
  size_t numberOfElements = 1;
74
75
  const void *                buffer;
76
77
  unsigned int                dimension     = 0;
78
79
80
  size_t                      elementSize   = sizeof(DataType);
81
  size_t                      len           = 1;
82
83
  if(PyObject_GetBuffer(arr, &pyBuffer, PyBUF_CONTIG) == -1)
84
    {
85
    PyErr_SetString( PyExc_RuntimeError, "Cannot get an instance of NumPy array." );
86
    PyBuffer_Release(&pyBuffer);
87
    return VectorType();
88
    }
89
  else
90
    {
91
    bufferLength = pyBuffer.len;
92
    buffer = pyBuffer.buf;
93
    }
94
95
  obj        = shape;
96
  shapeseq   = PySequence_Fast(obj, "expected sequence");
97
  dimension  = PySequence_Size(obj);
98
99
  item = PySequence_Fast_GET_ITEM(shapeseq,0);// Only one dimension
100
  numberOfElements = (size_t)PyLong_AsLong(item);
101
102
  len = numberOfElements*elementSize;
103
  if ( bufferLength != len )
104
    {
105
    PyErr_SetString( PyExc_RuntimeError, "Size mismatch of vector and Buffer." );
106
    PyBuffer_Release(&pyBuffer);
107
    return VectorType();
108
    }
109
  DataType * data = (DataType *)buffer;
110
  VectorType output(data, numberOfElements);
111
  PyBuffer_Release(&pyBuffer);
112
113
  return output;
114
}
115
116
template<class TElement>
117
PyObject *
118
PyVnl<TElement>
119
::_GetArrayViewFromVnlMatrix( MatrixType * matrix)
120
{
121
  PyObject *                  memoryView    = NULL;
122
  Py_buffer                   pyBuffer;
123
  memset(&pyBuffer, 0, sizeof(Py_buffer));
124
125
  size_t                      elementSize   = sizeof(DataType);
126
  int                         res           = 0;
127
128
  if( !matrix )
129
    {
130
    throw std::runtime_error("Input matrix is null");
131
    }
132
133
  DataType *buffer =  matrix->data_block();
134
  //ComponentType *buffer =  const_cast < ComponentType *> (reinterpret_cast< const ComponentType* > ( image->GetBufferPointer() ) );
135
  void * matrixBuffer = (void *)( buffer );
136
137
  // Computing the length of data
138
  Py_ssize_t len = matrix->size();
139
  len *= elementSize;
140
141
  res = PyBuffer_FillInfo(&pyBuffer, NULL, (void*)matrixBuffer, len, 0, PyBUF_CONTIG);
142
  memoryView = PyMemoryView_FromBuffer(&pyBuffer);
143
144
  PyBuffer_Release(&pyBuffer);
145
146
  return memoryView;
147
}
148
149
template<class TElement>
150
const typename PyVnl<TElement>::MatrixType
151
PyVnl<TElement>
152
::_GetVnlMatrixViewFromArray( PyObject *arr, PyObject *shape)
153
{
154
  PyObject *                  obj           = NULL;
155
  PyObject *                  shapeseq      = NULL;
156
  PyObject *                  item          = NULL;
157
158
  Py_ssize_t                  bufferLength;
159
  Py_buffer                   pyBuffer;
160
  memset(&pyBuffer, 0, sizeof(Py_buffer));
161
162
  size_t numberOfElements = 1;
163
164
  const void *                buffer;
165
166
  unsigned int                dimension     = 0;
167
168
  size_t                      elementSize   = sizeof(DataType);
169
  size_t                      len           = 1;
170
  unsigned int                size[2];
171
172
  if(PyObject_GetBuffer(arr, &pyBuffer, PyBUF_CONTIG) == -1)
173
    {
174
    PyErr_SetString( PyExc_RuntimeError, "Cannot get an instance of NumPy array." );
175
    PyBuffer_Release(&pyBuffer);
176
    return MatrixType();
177
    }
178
  else
179
    {
180
    bufferLength = pyBuffer.len;
181
    buffer = pyBuffer.buf;
182
    }
183
184
  obj        = shape;
185
  shapeseq   = PySequence_Fast(obj, "expected sequence");
186
  dimension  = PySequence_Size(obj);
187
188
  for( unsigned int i = 0; i < 2; ++i )
189
    {
190
    item = PySequence_Fast_GET_ITEM(shapeseq,i);
191
    size[i] = (unsigned int)PyLong_AsLong(item);
192
    numberOfElements *= size[i];
193
    }
194
195
  len = numberOfElements*elementSize;
196
  if ( bufferLength != len )
197
    {
198
    PyErr_SetString( PyExc_RuntimeError, "Size mismatch of matrix and Buffer." );
199
    PyBuffer_Release(&pyBuffer);
200
    return MatrixType();
201
    }
202
203
  DataType * data = (DataType *)buffer;
204
  MatrixType output(data, size[0], size[1]);
205
  PyBuffer_Release(&pyBuffer);
206
207
  return output;
208
}
209
210
211
} // namespace itk
212
213
#endif