File: | /tmp/pyrefcon/scipy/scipy/ndimage/src/nd_image.c |
Warning: | line 1184, column 9 PyObject ownership leak with reference count of 1 |
Press '?' to see keyboard shortcuts
Keyboard shortcuts:
1 | /* Copyright (C) 2003-2005 Peter J. Verveer | |||
2 | * | |||
3 | * Redistribution and use in source and binary forms, with or without | |||
4 | * modification, are permitted provided that the following conditions | |||
5 | * are met: | |||
6 | * | |||
7 | * 1. Redistributions of source code must retain the above copyright | |||
8 | * notice, this list of conditions and the following disclaimer. | |||
9 | * | |||
10 | * 2. Redistributions in binary form must reproduce the above | |||
11 | * copyright notice, this list of conditions and the following | |||
12 | * disclaimer in the documentation and/or other materials provided | |||
13 | * with the distribution. | |||
14 | * | |||
15 | * 3. The name of the author may not be used to endorse or promote | |||
16 | * products derived from this software without specific prior | |||
17 | * written permission. | |||
18 | * | |||
19 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS | |||
20 | * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED | |||
21 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |||
22 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY | |||
23 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |||
24 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE | |||
25 | * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | |||
26 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, | |||
27 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING | |||
28 | * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS | |||
29 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |||
30 | */ | |||
31 | ||||
32 | /* | |||
33 | * The order of these first two imports should not be changed, see the note | |||
34 | * in ni_support.h for details. | |||
35 | */ | |||
36 | #include "nd_image.h" | |||
37 | #include "ni_support.h" | |||
38 | ||||
39 | #include "ni_filters.h" | |||
40 | #include "ni_fourier.h" | |||
41 | #include "ni_morphology.h" | |||
42 | #include "ni_interpolation.h" | |||
43 | #include "ni_measure.h" | |||
44 | ||||
45 | #include "ccallback.h" | |||
46 | ||||
47 | #if NPY_API_VERSION0x0000000D >= 0x0000000c | |||
48 | #define HAVE_WRITEBACKIFCOPY | |||
49 | #endif | |||
50 | ||||
51 | typedef struct { | |||
52 | PyObject *extra_arguments; | |||
53 | PyObject *extra_keywords; | |||
54 | } NI_PythonCallbackData; | |||
55 | ||||
56 | /* Numarray Helper Functions */ | |||
57 | ||||
58 | /* | |||
59 | * Creates a new numpy array of the requested type and shape, and either | |||
60 | * copies into it the contents of buffer, or sets it to all zeros if | |||
61 | * buffer is NULL. | |||
62 | */ | |||
63 | static PyArrayObject * | |||
64 | NA_NewArray(void *buffer, enum NPY_TYPES type, int ndim, npy_intp *shape) | |||
65 | { | |||
66 | PyArrayObject *result; | |||
67 | ||||
68 | if (type == NPY_NOTYPE) { | |||
69 | type = NPY_DOUBLE; | |||
70 | } | |||
71 | ||||
72 | result = (PyArrayObject *)PyArray_SimpleNew(ndim, shape, type)(*(PyObject * (*)(PyTypeObject *, int, npy_intp const *, int, npy_intp const *, void *, int, int, PyObject *)) _scipy_ndimage_ARRAY_API [93])(&(*(PyTypeObject *)_scipy_ndimage_ARRAY_API[2]), ndim , shape, type, ((void*)0), ((void*)0), 0, 0, ((void*)0)); | |||
73 | if (result == NULL((void*)0)) { | |||
74 | return NULL((void*)0); | |||
75 | } | |||
76 | ||||
77 | if (buffer == NULL((void*)0)) { | |||
78 | memset(PyArray_DATA(result), 0, PyArray_NBYTES(result)(PyArray_ITEMSIZE(result) * (*(npy_intp (*)(npy_intp const *, int)) _scipy_ndimage_ARRAY_API[158])(PyArray_DIMS(result), PyArray_NDIM (result)))); | |||
79 | } | |||
80 | else { | |||
81 | memcpy(PyArray_DATA(result), buffer, PyArray_NBYTES(result)(PyArray_ITEMSIZE(result) * (*(npy_intp (*)(npy_intp const *, int)) _scipy_ndimage_ARRAY_API[158])(PyArray_DIMS(result), PyArray_NDIM (result)))); | |||
82 | } | |||
83 | ||||
84 | return result; | |||
85 | } | |||
86 | ||||
87 | /* Converts a Python array-like object into a behaved input array. */ | |||
88 | static int | |||
89 | NI_ObjectToInputArray(PyObject *object, PyArrayObject **array) | |||
90 | { | |||
91 | int flags = NPY_ARRAY_ALIGNED0x0100 | NPY_ARRAY_NOTSWAPPED0x0200; | |||
92 | *array = (PyArrayObject *)PyArray_CheckFromAny(*(PyObject * (*)(PyObject *, PyArray_Descr *, int, int, int, PyObject *)) _scipy_ndimage_ARRAY_API[108])(object, NULL((void*)0), 0, 0, flags, | |||
93 | NULL((void*)0)); | |||
94 | return *array != NULL((void*)0); | |||
95 | } | |||
96 | ||||
97 | /* Like NI_ObjectToInputArray, but with special handling for Py_None. */ | |||
98 | static int | |||
99 | NI_ObjectToOptionalInputArray(PyObject *object, PyArrayObject **array) | |||
100 | { | |||
101 | if (object == Py_None(&_Py_NoneStruct)) { | |||
102 | *array = NULL((void*)0); | |||
103 | return 1; | |||
104 | } | |||
105 | return NI_ObjectToInputArray(object, array); | |||
106 | } | |||
107 | ||||
108 | /* Converts a Python array-like object into a behaved output array. */ | |||
109 | static int | |||
110 | NI_ObjectToOutputArray(PyObject *object, PyArrayObject **array) | |||
111 | { | |||
112 | #ifdef HAVE_WRITEBACKIFCOPY | |||
113 | int flags = NPY_ARRAY_BEHAVED_NS(0x0100 | 0x0400 | 0x0200) | NPY_ARRAY_WRITEBACKIFCOPY0x2000; | |||
114 | #else | |||
115 | int flags = NPY_ARRAY_BEHAVED_NS(0x0100 | 0x0400 | 0x0200) | NPY_ARRAY_UPDATEIFCOPY0x1000; | |||
116 | #endif | |||
117 | /* | |||
118 | * This would also be caught by the PyArray_CheckFromAny call, but | |||
119 | * we check it explicitly here to provide a saner error message. | |||
120 | */ | |||
121 | if (PyArray_Check(object)((((PyObject*)(object))->ob_type) == (&(*(PyTypeObject *)_scipy_ndimage_ARRAY_API[2])) || PyType_IsSubtype((((PyObject *)(object))->ob_type), (&(*(PyTypeObject *)_scipy_ndimage_ARRAY_API [2])))) && | |||
122 | !PyArray_ISWRITEABLE((PyArrayObject *)object)PyArray_CHKFLAGS(((PyArrayObject *)object), 0x0400)) { | |||
123 | PyErr_SetString(PyExc_ValueError, "output array is read-only."); | |||
124 | return 0; | |||
125 | } | |||
126 | /* | |||
127 | * If the input array is not aligned or is byteswapped, this call | |||
128 | * will create a new aligned, native byte order array, and copy the | |||
129 | * contents of object into it. For an output array, the copy is | |||
130 | * unnecessary, so this could be optimized. It is very easy to not | |||
131 | * do NPY_ARRAY_UPDATEIFCOPY right, so we let NumPy do it for us | |||
132 | * and pay the performance price. | |||
133 | */ | |||
134 | *array = (PyArrayObject *)PyArray_CheckFromAny(*(PyObject * (*)(PyObject *, PyArray_Descr *, int, int, int, PyObject *)) _scipy_ndimage_ARRAY_API[108])(object, NULL((void*)0), 0, 0, flags, | |||
135 | NULL((void*)0)); | |||
136 | return *array != NULL((void*)0); | |||
137 | } | |||
138 | ||||
139 | /* Like NI_ObjectToOutputArray, but with special handling for Py_None. */ | |||
140 | static int | |||
141 | NI_ObjectToOptionalOutputArray(PyObject *object, PyArrayObject **array) | |||
142 | { | |||
143 | if (object == Py_None(&_Py_NoneStruct)) { | |||
144 | *array = NULL((void*)0); | |||
145 | return 1; | |||
146 | } | |||
147 | return NI_ObjectToOutputArray(object, array); | |||
148 | } | |||
149 | ||||
150 | /* Converts a Python array-like object into a behaved input/output array. */ | |||
151 | static int | |||
152 | NI_ObjectToInputOutputArray(PyObject *object, PyArrayObject **array) | |||
153 | { | |||
154 | /* | |||
155 | * This is also done in NI_ObjectToOutputArray, double checking here | |||
156 | * to provide a more specific error message. | |||
157 | */ | |||
158 | if (PyArray_Check(object)((((PyObject*)(object))->ob_type) == (&(*(PyTypeObject *)_scipy_ndimage_ARRAY_API[2])) || PyType_IsSubtype((((PyObject *)(object))->ob_type), (&(*(PyTypeObject *)_scipy_ndimage_ARRAY_API [2])))) && | |||
159 | !PyArray_ISWRITEABLE((PyArrayObject *)object)PyArray_CHKFLAGS(((PyArrayObject *)object), 0x0400)) { | |||
160 | PyErr_SetString(PyExc_ValueError, "input/output array is read-only."); | |||
161 | return 0; | |||
162 | } | |||
163 | return NI_ObjectToOutputArray(object, array); | |||
164 | } | |||
165 | ||||
166 | /* Checks that an origin value was received for each array dimension. */ | |||
167 | static int | |||
168 | _validate_origin(PyArrayObject *array, PyArray_Dims origin) | |||
169 | { | |||
170 | if (origin.len != PyArray_NDIM(array)) { | |||
171 | PyErr_Format(PyExc_ValueError, | |||
172 | "Invalid %d element 'origin' sequence for " | |||
173 | "%d-dimensional input array.", | |||
174 | origin.len, PyArray_NDIM(array)); | |||
175 | return 0; | |||
176 | } | |||
177 | return 1; | |||
178 | } | |||
179 | ||||
180 | /*********************************************************************/ | |||
181 | /* wrapper functions: */ | |||
182 | /*********************************************************************/ | |||
183 | ||||
184 | static PyObject *Py_Correlate1D(PyObject *obj, PyObject *args) | |||
185 | { | |||
186 | PyArrayObject *input = NULL((void*)0), *output = NULL((void*)0), *weights = NULL((void*)0); | |||
187 | int axis, mode; | |||
188 | double cval; | |||
189 | npy_intp origin; | |||
190 | ||||
191 | if (!PyArg_ParseTuple(args, "O&O&iO&idn" , | |||
192 | NI_ObjectToInputArray, &input, | |||
193 | NI_ObjectToInputArray, &weights, &axis, | |||
194 | NI_ObjectToOutputArray, &output, &mode, &cval, | |||
195 | &origin)) | |||
196 | goto exit; | |||
197 | ||||
198 | NI_Correlate1D(input, weights, axis, output, (NI_ExtendMode)mode, cval, | |||
199 | origin); | |||
200 | #ifdef HAVE_WRITEBACKIFCOPY | |||
201 | PyArray_ResolveWritebackIfCopy(*(int (*)(PyArrayObject *)) _scipy_ndimage_ARRAY_API[302])(output); | |||
202 | #endif | |||
203 | ||||
204 | exit: | |||
205 | Py_XDECREF(input)_Py_XDECREF(((PyObject*)(input))); | |||
206 | Py_XDECREF(weights)_Py_XDECREF(((PyObject*)(weights))); | |||
207 | Py_XDECREF(output)_Py_XDECREF(((PyObject*)(output))); | |||
208 | return PyErr_Occurred() ? NULL((void*)0) : Py_BuildValue(""); | |||
209 | } | |||
210 | ||||
211 | static PyObject *Py_Correlate(PyObject *obj, PyObject *args) | |||
212 | { | |||
213 | PyArrayObject *input = NULL((void*)0), *output = NULL((void*)0), *weights = NULL((void*)0); | |||
214 | PyArray_Dims origin = {NULL((void*)0), 0}; | |||
215 | int mode; | |||
216 | double cval; | |||
217 | ||||
218 | if (!PyArg_ParseTuple(args, "O&O&O&idO&", NI_ObjectToInputArray, &input, | |||
219 | NI_ObjectToInputArray, &weights, | |||
220 | NI_ObjectToOutputArray, &output, | |||
221 | &mode, &cval, | |||
222 | PyArray_IntpConverter(*(int (*)(PyObject *, PyArray_Dims *)) _scipy_ndimage_ARRAY_API [176]), &origin)) { | |||
223 | goto exit; | |||
224 | } | |||
225 | if (!_validate_origin(input, origin)) { | |||
226 | goto exit; | |||
227 | } | |||
228 | ||||
229 | NI_Correlate(input, weights, output, (NI_ExtendMode)mode, cval, | |||
230 | origin.ptr); | |||
231 | #ifdef HAVE_WRITEBACKIFCOPY | |||
232 | PyArray_ResolveWritebackIfCopy(*(int (*)(PyArrayObject *)) _scipy_ndimage_ARRAY_API[302])(output); | |||
233 | #endif | |||
234 | ||||
235 | exit: | |||
236 | Py_XDECREF(input)_Py_XDECREF(((PyObject*)(input))); | |||
237 | Py_XDECREF(weights)_Py_XDECREF(((PyObject*)(weights))); | |||
238 | Py_XDECREF(output)_Py_XDECREF(((PyObject*)(output))); | |||
239 | PyDimMem_FREE(origin.ptr)PyMem_RawFree(origin.ptr); | |||
240 | return PyErr_Occurred() ? NULL((void*)0) : Py_BuildValue(""); | |||
241 | } | |||
242 | ||||
243 | static PyObject *Py_UniformFilter1D(PyObject *obj, PyObject *args) | |||
244 | { | |||
245 | PyArrayObject *input = NULL((void*)0), *output = NULL((void*)0); | |||
246 | int axis, mode; | |||
247 | npy_intp filter_size, origin; | |||
248 | double cval; | |||
249 | ||||
250 | if (!PyArg_ParseTuple(args, "O&niO&idn", | |||
251 | NI_ObjectToInputArray, &input, | |||
252 | &filter_size, &axis, | |||
253 | NI_ObjectToOutputArray, &output, | |||
254 | &mode, &cval, &origin)) | |||
255 | goto exit; | |||
256 | ||||
257 | NI_UniformFilter1D(input, filter_size, axis, output, (NI_ExtendMode)mode, | |||
258 | cval, origin); | |||
259 | #ifdef HAVE_WRITEBACKIFCOPY | |||
260 | PyArray_ResolveWritebackIfCopy(*(int (*)(PyArrayObject *)) _scipy_ndimage_ARRAY_API[302])(output); | |||
261 | #endif | |||
262 | ||||
263 | exit: | |||
264 | Py_XDECREF(input)_Py_XDECREF(((PyObject*)(input))); | |||
265 | Py_XDECREF(output)_Py_XDECREF(((PyObject*)(output))); | |||
266 | return PyErr_Occurred() ? NULL((void*)0) : Py_BuildValue(""); | |||
267 | } | |||
268 | ||||
269 | static PyObject *Py_MinOrMaxFilter1D(PyObject *obj, PyObject *args) | |||
270 | { | |||
271 | PyArrayObject *input = NULL((void*)0), *output = NULL((void*)0); | |||
272 | int axis, mode, minimum; | |||
273 | npy_intp filter_size, origin; | |||
274 | double cval; | |||
275 | ||||
276 | if (!PyArg_ParseTuple(args, "O&niO&idni", | |||
277 | NI_ObjectToInputArray, &input, | |||
278 | &filter_size, &axis, | |||
279 | NI_ObjectToOutputArray, &output, | |||
280 | &mode, &cval, &origin, &minimum)) | |||
281 | goto exit; | |||
282 | ||||
283 | NI_MinOrMaxFilter1D(input, filter_size, axis, output, (NI_ExtendMode)mode, | |||
284 | cval, origin, minimum); | |||
285 | #ifdef HAVE_WRITEBACKIFCOPY | |||
286 | PyArray_ResolveWritebackIfCopy(*(int (*)(PyArrayObject *)) _scipy_ndimage_ARRAY_API[302])(output); | |||
287 | #endif | |||
288 | ||||
289 | exit: | |||
290 | Py_XDECREF(input)_Py_XDECREF(((PyObject*)(input))); | |||
291 | Py_XDECREF(output)_Py_XDECREF(((PyObject*)(output))); | |||
292 | return PyErr_Occurred() ? NULL((void*)0) : Py_BuildValue(""); | |||
293 | } | |||
294 | ||||
295 | static PyObject *Py_MinOrMaxFilter(PyObject *obj, PyObject *args) | |||
296 | { | |||
297 | PyArrayObject *input = NULL((void*)0), *output = NULL((void*)0), *footprint = NULL((void*)0); | |||
298 | PyArrayObject *structure = NULL((void*)0); | |||
299 | PyArray_Dims origin = {NULL((void*)0), 0}; | |||
300 | int mode, minimum; | |||
301 | double cval; | |||
302 | ||||
303 | if (!PyArg_ParseTuple(args, "O&O&O&O&idO&i", | |||
304 | NI_ObjectToInputArray, &input, | |||
305 | NI_ObjectToInputArray, &footprint, | |||
306 | NI_ObjectToOptionalInputArray, &structure, | |||
307 | NI_ObjectToOutputArray, &output, | |||
308 | &mode, &cval, | |||
309 | PyArray_IntpConverter(*(int (*)(PyObject *, PyArray_Dims *)) _scipy_ndimage_ARRAY_API [176]), &origin, | |||
310 | &minimum)) { | |||
311 | goto exit; | |||
312 | } | |||
313 | if (!_validate_origin(input, origin)) { | |||
314 | goto exit; | |||
315 | } | |||
316 | ||||
317 | NI_MinOrMaxFilter(input, footprint, structure, output, (NI_ExtendMode)mode, | |||
318 | cval, origin.ptr, minimum); | |||
319 | #ifdef HAVE_WRITEBACKIFCOPY | |||
320 | PyArray_ResolveWritebackIfCopy(*(int (*)(PyArrayObject *)) _scipy_ndimage_ARRAY_API[302])(output); | |||
321 | #endif | |||
322 | ||||
323 | exit: | |||
324 | Py_XDECREF(input)_Py_XDECREF(((PyObject*)(input))); | |||
325 | Py_XDECREF(footprint)_Py_XDECREF(((PyObject*)(footprint))); | |||
326 | Py_XDECREF(structure)_Py_XDECREF(((PyObject*)(structure))); | |||
327 | Py_XDECREF(output)_Py_XDECREF(((PyObject*)(output))); | |||
328 | PyDimMem_FREE(origin.ptr)PyMem_RawFree(origin.ptr); | |||
329 | return PyErr_Occurred() ? NULL((void*)0) : Py_BuildValue(""); | |||
330 | } | |||
331 | ||||
332 | static PyObject *Py_RankFilter(PyObject *obj, PyObject *args) | |||
333 | { | |||
334 | PyArrayObject *input = NULL((void*)0), *output = NULL((void*)0), *footprint = NULL((void*)0); | |||
335 | PyArray_Dims origin = {NULL((void*)0), 0}; | |||
336 | int mode, rank; | |||
337 | double cval; | |||
338 | ||||
339 | if (!PyArg_ParseTuple(args, "O&iO&O&idO&", | |||
340 | NI_ObjectToInputArray, &input, &rank, | |||
341 | NI_ObjectToInputArray, &footprint, | |||
342 | NI_ObjectToOutputArray, &output, | |||
343 | &mode, &cval, | |||
344 | PyArray_IntpConverter(*(int (*)(PyObject *, PyArray_Dims *)) _scipy_ndimage_ARRAY_API [176]), &origin)) { | |||
345 | goto exit; | |||
346 | } | |||
347 | if (!_validate_origin(input, origin)) { | |||
348 | goto exit; | |||
349 | } | |||
350 | ||||
351 | NI_RankFilter(input, rank, footprint, output, (NI_ExtendMode)mode, cval, | |||
352 | origin.ptr); | |||
353 | #ifdef HAVE_WRITEBACKIFCOPY | |||
354 | PyArray_ResolveWritebackIfCopy(*(int (*)(PyArrayObject *)) _scipy_ndimage_ARRAY_API[302])(output); | |||
355 | #endif | |||
356 | ||||
357 | exit: | |||
358 | Py_XDECREF(input)_Py_XDECREF(((PyObject*)(input))); | |||
359 | Py_XDECREF(footprint)_Py_XDECREF(((PyObject*)(footprint))); | |||
360 | Py_XDECREF(output)_Py_XDECREF(((PyObject*)(output))); | |||
361 | PyDimMem_FREE(origin.ptr)PyMem_RawFree(origin.ptr); | |||
362 | return PyErr_Occurred() ? NULL((void*)0) : Py_BuildValue(""); | |||
363 | } | |||
364 | ||||
365 | static int Py_Filter1DFunc(double *iline, npy_intp ilen, | |||
366 | double *oline, npy_intp olen, void *data) | |||
367 | { | |||
368 | PyArrayObject *py_ibuffer = NULL((void*)0), *py_obuffer = NULL((void*)0); | |||
369 | PyObject *rv = NULL((void*)0), *args = NULL((void*)0), *tmp = NULL((void*)0); | |||
370 | npy_intp ii; | |||
371 | double *po = NULL((void*)0); | |||
372 | ccallback_t *callback = (ccallback_t *)data; | |||
373 | NI_PythonCallbackData *cbdata = (NI_PythonCallbackData*)callback->info_p; | |||
374 | ||||
375 | py_ibuffer = NA_NewArray(iline, NPY_DOUBLE, 1, &ilen); | |||
376 | py_obuffer = NA_NewArray(NULL((void*)0), NPY_DOUBLE, 1, &olen); | |||
377 | if (!py_ibuffer || !py_obuffer) | |||
378 | goto exit; | |||
379 | tmp = Py_BuildValue("(OO)", py_ibuffer, py_obuffer); | |||
380 | if (!tmp) | |||
381 | goto exit; | |||
382 | args = PySequence_Concat(tmp, cbdata->extra_arguments); | |||
383 | if (!args) | |||
384 | goto exit; | |||
385 | rv = PyObject_Call(callback->py_function, args, cbdata->extra_keywords); | |||
386 | if (!rv) | |||
387 | goto exit; | |||
388 | po = (double*)PyArray_DATA(py_obuffer); | |||
389 | for(ii = 0; ii < olen; ii++) | |||
390 | oline[ii] = po[ii]; | |||
391 | exit: | |||
392 | Py_XDECREF(py_ibuffer)_Py_XDECREF(((PyObject*)(py_ibuffer))); | |||
393 | Py_XDECREF(py_obuffer)_Py_XDECREF(((PyObject*)(py_obuffer))); | |||
394 | Py_XDECREF(rv)_Py_XDECREF(((PyObject*)(rv))); | |||
395 | Py_XDECREF(args)_Py_XDECREF(((PyObject*)(args))); | |||
396 | Py_XDECREF(tmp)_Py_XDECREF(((PyObject*)(tmp))); | |||
397 | return PyErr_Occurred() ? 0 : 1; | |||
398 | } | |||
399 | ||||
400 | static PyObject *Py_GenericFilter1D(PyObject *obj, PyObject *args) | |||
401 | { | |||
402 | PyArrayObject *input = NULL((void*)0), *output = NULL((void*)0); | |||
403 | PyObject *fnc = NULL((void*)0), *extra_arguments = NULL((void*)0), *extra_keywords = NULL((void*)0); | |||
404 | void *func = NULL((void*)0), *data = NULL((void*)0); | |||
405 | NI_PythonCallbackData cbdata; | |||
406 | int axis, mode; | |||
407 | npy_intp origin, filter_size; | |||
408 | double cval; | |||
409 | ccallback_t callback; | |||
410 | static ccallback_signature_t callback_signatures[] = { | |||
411 | {"int (double *, intptr_t, double *, intptr_t, void *)"}, | |||
412 | {"int (double *, npy_intp, double *, npy_intp, void *)"}, | |||
413 | #if NPY_SIZEOF_INTP8 == NPY_SIZEOF_SHORT2 | |||
414 | {"int (double *, short, double *, short, void *)"}, | |||
415 | #endif | |||
416 | #if NPY_SIZEOF_INTP8 == NPY_SIZEOF_INT4 | |||
417 | {"int (double *, int, double *, int, void *)"}, | |||
418 | #endif | |||
419 | #if NPY_SIZEOF_INTP8 == NPY_SIZEOF_LONG8 | |||
420 | {"int (double *, long, double *, long, void *)"}, | |||
421 | #endif | |||
422 | #if NPY_SIZEOF_INTP8 == NPY_SIZEOF_LONGLONG8 | |||
423 | {"int (double *, long long, double *, long long, void *)"}, | |||
424 | #endif | |||
425 | {NULL((void*)0)} | |||
426 | }; | |||
427 | ||||
428 | callback.py_function = NULL((void*)0); | |||
429 | callback.c_function = NULL((void*)0); | |||
430 | ||||
431 | if (!PyArg_ParseTuple(args, "O&OniO&idnOO", | |||
432 | NI_ObjectToInputArray, &input, | |||
433 | &fnc, &filter_size, &axis, | |||
434 | NI_ObjectToOutputArray, &output, | |||
435 | &mode, &cval, &origin, | |||
436 | &extra_arguments, &extra_keywords)) | |||
437 | goto exit; | |||
438 | ||||
439 | if (!PyTuple_Check(extra_arguments)((((((PyObject*)(extra_arguments))->ob_type))->tp_flags & ((1UL << 26))) != 0)) { | |||
440 | PyErr_SetString(PyExc_RuntimeError, "extra_arguments must be a tuple"); | |||
441 | goto exit; | |||
442 | } | |||
443 | if (!PyDict_Check(extra_keywords)((((((PyObject*)(extra_keywords))->ob_type))->tp_flags & ((1UL << 29))) != 0)) { | |||
444 | PyErr_SetString(PyExc_RuntimeError, | |||
445 | "extra_keywords must be a dictionary"); | |||
446 | goto exit; | |||
447 | } | |||
448 | if (PyCapsule_CheckExact(fnc)((((PyObject*)(fnc))->ob_type) == &PyCapsule_Type) && PyCapsule_GetName(fnc) == NULL((void*)0)) { | |||
449 | /* 'Legacy' low-level callable */ | |||
450 | func = PyCapsule_GetPointer(fnc, NULL((void*)0)); | |||
451 | data = PyCapsule_GetContext(fnc); | |||
452 | } else { | |||
453 | int ret; | |||
454 | ||||
455 | ret = ccallback_prepare(&callback, callback_signatures, fnc, CCALLBACK_DEFAULTS0x0); | |||
456 | if (ret == -1) { | |||
457 | goto exit; | |||
458 | } | |||
459 | ||||
460 | if (callback.py_function != NULL((void*)0)) { | |||
461 | cbdata.extra_arguments = extra_arguments; | |||
462 | cbdata.extra_keywords = extra_keywords; | |||
463 | callback.info_p = (void*)&cbdata; | |||
464 | func = Py_Filter1DFunc; | |||
465 | data = (void*)&callback; | |||
466 | } | |||
467 | else { | |||
468 | func = callback.c_function; | |||
469 | data = callback.user_data; | |||
470 | } | |||
471 | } | |||
472 | ||||
473 | NI_GenericFilter1D(input, func, data, filter_size, axis, output, | |||
474 | (NI_ExtendMode)mode, cval, origin); | |||
475 | #ifdef HAVE_WRITEBACKIFCOPY | |||
476 | PyArray_ResolveWritebackIfCopy(*(int (*)(PyArrayObject *)) _scipy_ndimage_ARRAY_API[302])(output); | |||
477 | #endif | |||
478 | ||||
479 | exit: | |||
480 | if (callback.py_function != NULL((void*)0) || callback.c_function != NULL((void*)0)) { | |||
481 | ccallback_release(&callback); | |||
482 | } | |||
483 | Py_XDECREF(input)_Py_XDECREF(((PyObject*)(input))); | |||
484 | Py_XDECREF(output)_Py_XDECREF(((PyObject*)(output))); | |||
485 | return PyErr_Occurred() ? NULL((void*)0) : Py_BuildValue(""); | |||
486 | } | |||
487 | ||||
488 | static int Py_FilterFunc(double *buffer, npy_intp filter_size, | |||
489 | double *output, void *data) | |||
490 | { | |||
491 | PyArrayObject *py_buffer = NULL((void*)0); | |||
492 | PyObject *rv = NULL((void*)0), *args = NULL((void*)0), *tmp = NULL((void*)0); | |||
493 | ccallback_t *callback = (ccallback_t *)data; | |||
494 | NI_PythonCallbackData *cbdata = (NI_PythonCallbackData*)callback->info_p; | |||
495 | ||||
496 | py_buffer = NA_NewArray(buffer, NPY_DOUBLE, 1, &filter_size); | |||
497 | if (!py_buffer) | |||
498 | goto exit; | |||
499 | tmp = Py_BuildValue("(O)", py_buffer); | |||
500 | if (!tmp) | |||
501 | goto exit; | |||
502 | args = PySequence_Concat(tmp, cbdata->extra_arguments); | |||
503 | if (!args) | |||
504 | goto exit; | |||
505 | rv = PyObject_Call(callback->py_function, args, cbdata->extra_keywords); | |||
506 | if (!rv) | |||
507 | goto exit; | |||
508 | *output = PyFloat_AsDouble(rv); | |||
509 | exit: | |||
510 | Py_XDECREF(py_buffer)_Py_XDECREF(((PyObject*)(py_buffer))); | |||
511 | Py_XDECREF(rv)_Py_XDECREF(((PyObject*)(rv))); | |||
512 | Py_XDECREF(args)_Py_XDECREF(((PyObject*)(args))); | |||
513 | Py_XDECREF(tmp)_Py_XDECREF(((PyObject*)(tmp))); | |||
514 | return PyErr_Occurred() ? 0 : 1; | |||
515 | } | |||
516 | ||||
517 | static PyObject *Py_GenericFilter(PyObject *obj, PyObject *args) | |||
518 | { | |||
519 | PyArrayObject *input = NULL((void*)0), *output = NULL((void*)0), *footprint = NULL((void*)0); | |||
520 | PyObject *fnc = NULL((void*)0), *extra_arguments = NULL((void*)0), *extra_keywords = NULL((void*)0); | |||
521 | void *func = NULL((void*)0), *data = NULL((void*)0); | |||
522 | NI_PythonCallbackData cbdata; | |||
523 | int mode; | |||
524 | PyArray_Dims origin = {NULL((void*)0), 0}; | |||
525 | double cval; | |||
526 | ccallback_t callback; | |||
527 | static ccallback_signature_t callback_signatures[] = { | |||
528 | {"int (double *, intptr_t, double *, void *)"}, | |||
529 | {"int (double *, npy_intp, double *, void *)"}, | |||
530 | #if NPY_SIZEOF_INTP8 == NPY_SIZEOF_SHORT2 | |||
531 | {"int (double *, short, double *, void *)"}, | |||
532 | #endif | |||
533 | #if NPY_SIZEOF_INTP8 == NPY_SIZEOF_INT4 | |||
534 | {"int (double *, int, double *, void *)"}, | |||
535 | #endif | |||
536 | #if NPY_SIZEOF_INTP8 == NPY_SIZEOF_LONG8 | |||
537 | {"int (double *, long, double *, void *)"}, | |||
538 | #endif | |||
539 | #if NPY_SIZEOF_INTP8 == NPY_SIZEOF_LONGLONG8 | |||
540 | {"int (double *, long long, double *, void *)"}, | |||
541 | #endif | |||
542 | {NULL((void*)0)} | |||
543 | }; | |||
544 | ||||
545 | callback.py_function = NULL((void*)0); | |||
546 | callback.c_function = NULL((void*)0); | |||
547 | ||||
548 | if (!PyArg_ParseTuple(args, "O&OO&O&idO&OO", | |||
549 | NI_ObjectToInputArray, &input, | |||
550 | &fnc, | |||
551 | NI_ObjectToInputArray, &footprint, | |||
552 | NI_ObjectToOutputArray, &output, | |||
553 | &mode, &cval, | |||
554 | PyArray_IntpConverter(*(int (*)(PyObject *, PyArray_Dims *)) _scipy_ndimage_ARRAY_API [176]), &origin, | |||
555 | &extra_arguments, &extra_keywords)) { | |||
556 | goto exit; | |||
557 | } | |||
558 | if (!_validate_origin(input, origin)) { | |||
559 | goto exit; | |||
560 | } | |||
561 | if (!PyTuple_Check(extra_arguments)((((((PyObject*)(extra_arguments))->ob_type))->tp_flags & ((1UL << 26))) != 0)) { | |||
562 | PyErr_SetString(PyExc_RuntimeError, "extra_arguments must be a tuple"); | |||
563 | goto exit; | |||
564 | } | |||
565 | if (!PyDict_Check(extra_keywords)((((((PyObject*)(extra_keywords))->ob_type))->tp_flags & ((1UL << 29))) != 0)) { | |||
566 | PyErr_SetString(PyExc_RuntimeError, | |||
567 | "extra_keywords must be a dictionary"); | |||
568 | goto exit; | |||
569 | } | |||
570 | if (PyCapsule_CheckExact(fnc)((((PyObject*)(fnc))->ob_type) == &PyCapsule_Type) && PyCapsule_GetName(fnc) == NULL((void*)0)) { | |||
571 | func = PyCapsule_GetPointer(fnc, NULL((void*)0)); | |||
572 | data = PyCapsule_GetContext(fnc); | |||
573 | } else { | |||
574 | int ret; | |||
575 | ||||
576 | ret = ccallback_prepare(&callback, callback_signatures, fnc, CCALLBACK_DEFAULTS0x0); | |||
577 | if (ret == -1) { | |||
578 | goto exit; | |||
579 | } | |||
580 | ||||
581 | if (callback.py_function != NULL((void*)0)) { | |||
582 | cbdata.extra_arguments = extra_arguments; | |||
583 | cbdata.extra_keywords = extra_keywords; | |||
584 | callback.info_p = (void*)&cbdata; | |||
585 | func = Py_FilterFunc; | |||
586 | data = (void*)&callback; | |||
587 | } | |||
588 | else { | |||
589 | func = callback.c_function; | |||
590 | data = callback.user_data; | |||
591 | } | |||
592 | } | |||
593 | ||||
594 | NI_GenericFilter(input, func, data, footprint, output, (NI_ExtendMode)mode, | |||
595 | cval, origin.ptr); | |||
596 | #ifdef HAVE_WRITEBACKIFCOPY | |||
597 | PyArray_ResolveWritebackIfCopy(*(int (*)(PyArrayObject *)) _scipy_ndimage_ARRAY_API[302])(output); | |||
598 | #endif | |||
599 | ||||
600 | exit: | |||
601 | if (callback.py_function != NULL((void*)0) || callback.c_function != NULL((void*)0)) { | |||
602 | ccallback_release(&callback); | |||
603 | } | |||
604 | Py_XDECREF(input)_Py_XDECREF(((PyObject*)(input))); | |||
605 | Py_XDECREF(output)_Py_XDECREF(((PyObject*)(output))); | |||
606 | Py_XDECREF(footprint)_Py_XDECREF(((PyObject*)(footprint))); | |||
607 | PyDimMem_FREE(origin.ptr)PyMem_RawFree(origin.ptr); | |||
608 | return PyErr_Occurred() ? NULL((void*)0) : Py_BuildValue(""); | |||
609 | } | |||
610 | ||||
611 | static PyObject *Py_FourierFilter(PyObject *obj, PyObject *args) | |||
612 | { | |||
613 | PyArrayObject *input = NULL((void*)0), *output = NULL((void*)0), *parameters = NULL((void*)0); | |||
614 | int axis, filter_type; | |||
615 | npy_intp n; | |||
616 | ||||
617 | if (!PyArg_ParseTuple(args, "O&O&niO&i", | |||
618 | NI_ObjectToInputArray, &input, | |||
619 | NI_ObjectToInputArray, ¶meters, | |||
620 | &n, &axis, | |||
621 | NI_ObjectToOutputArray, &output, | |||
622 | &filter_type)) | |||
623 | goto exit; | |||
624 | ||||
625 | NI_FourierFilter(input, parameters, n, axis, output, filter_type); | |||
626 | #ifdef HAVE_WRITEBACKIFCOPY | |||
627 | PyArray_ResolveWritebackIfCopy(*(int (*)(PyArrayObject *)) _scipy_ndimage_ARRAY_API[302])(output); | |||
628 | #endif | |||
629 | ||||
630 | exit: | |||
631 | Py_XDECREF(input)_Py_XDECREF(((PyObject*)(input))); | |||
632 | Py_XDECREF(parameters)_Py_XDECREF(((PyObject*)(parameters))); | |||
633 | Py_XDECREF(output)_Py_XDECREF(((PyObject*)(output))); | |||
634 | return PyErr_Occurred() ? NULL((void*)0) : Py_BuildValue(""); | |||
635 | } | |||
636 | ||||
637 | static PyObject *Py_FourierShift(PyObject *obj, PyObject *args) | |||
638 | { | |||
639 | PyArrayObject *input = NULL((void*)0), *output = NULL((void*)0), *shifts = NULL((void*)0); | |||
640 | int axis; | |||
641 | npy_intp n; | |||
642 | ||||
643 | if (!PyArg_ParseTuple(args, "O&O&niO&", | |||
644 | NI_ObjectToInputArray, &input, | |||
645 | NI_ObjectToInputArray, &shifts, | |||
646 | &n, &axis, | |||
647 | NI_ObjectToOutputArray, &output)) | |||
648 | goto exit; | |||
649 | ||||
650 | NI_FourierShift(input, shifts, n, axis, output); | |||
651 | #ifdef HAVE_WRITEBACKIFCOPY | |||
652 | PyArray_ResolveWritebackIfCopy(*(int (*)(PyArrayObject *)) _scipy_ndimage_ARRAY_API[302])(output); | |||
653 | #endif | |||
654 | ||||
655 | exit: | |||
656 | Py_XDECREF(input)_Py_XDECREF(((PyObject*)(input))); | |||
657 | Py_XDECREF(shifts)_Py_XDECREF(((PyObject*)(shifts))); | |||
658 | Py_XDECREF(output)_Py_XDECREF(((PyObject*)(output))); | |||
659 | return PyErr_Occurred() ? NULL((void*)0) : Py_BuildValue(""); | |||
660 | } | |||
661 | ||||
662 | static PyObject *Py_SplineFilter1D(PyObject *obj, PyObject *args) | |||
663 | { | |||
664 | PyArrayObject *input = NULL((void*)0), *output = NULL((void*)0); | |||
665 | int axis, order, mode; | |||
666 | ||||
667 | if (!PyArg_ParseTuple(args, "O&iiO&i", | |||
668 | NI_ObjectToInputArray, &input, &order, &axis, | |||
669 | NI_ObjectToOutputArray, &output, &mode)) | |||
670 | goto exit; | |||
671 | ||||
672 | NI_SplineFilter1D(input, order, axis, mode, output); | |||
673 | #ifdef HAVE_WRITEBACKIFCOPY | |||
674 | PyArray_ResolveWritebackIfCopy(*(int (*)(PyArrayObject *)) _scipy_ndimage_ARRAY_API[302])(output); | |||
675 | #endif | |||
676 | ||||
677 | exit: | |||
678 | Py_XDECREF(input)_Py_XDECREF(((PyObject*)(input))); | |||
679 | Py_XDECREF(output)_Py_XDECREF(((PyObject*)(output))); | |||
680 | return PyErr_Occurred() ? NULL((void*)0) : Py_BuildValue(""); | |||
681 | } | |||
682 | ||||
683 | static int Py_Map(npy_intp *ocoor, double* icoor, int orank, int irank, | |||
684 | void *data) | |||
685 | { | |||
686 | PyObject *coors = NULL((void*)0), *rets = NULL((void*)0), *args = NULL((void*)0), *tmp = NULL((void*)0); | |||
687 | npy_intp ii; | |||
688 | ccallback_t *callback = (ccallback_t *)data; | |||
689 | NI_PythonCallbackData *cbdata = (NI_PythonCallbackData*)callback->info_p; | |||
690 | ||||
691 | coors = PyTuple_New(orank); | |||
692 | if (!coors) | |||
693 | goto exit; | |||
694 | for(ii = 0; ii < orank; ii++) { | |||
695 | PyTuple_SetItem(coors, ii, PyLong_FromSsize_t(ocoor[ii])); | |||
696 | if (PyErr_Occurred()) | |||
697 | goto exit; | |||
698 | } | |||
699 | tmp = Py_BuildValue("(O)", coors); | |||
700 | if (!tmp) | |||
701 | goto exit; | |||
702 | args = PySequence_Concat(tmp, cbdata->extra_arguments); | |||
703 | if (!args) | |||
704 | goto exit; | |||
705 | rets = PyObject_Call(callback->py_function, args, cbdata->extra_keywords); | |||
706 | if (!rets) | |||
707 | goto exit; | |||
708 | for(ii = 0; ii < irank; ii++) { | |||
709 | icoor[ii] = PyFloat_AsDouble(PyTuple_GetItem(rets, ii)); | |||
710 | if (PyErr_Occurred()) | |||
711 | goto exit; | |||
712 | } | |||
713 | exit: | |||
714 | Py_XDECREF(coors)_Py_XDECREF(((PyObject*)(coors))); | |||
715 | Py_XDECREF(tmp)_Py_XDECREF(((PyObject*)(tmp))); | |||
716 | Py_XDECREF(rets)_Py_XDECREF(((PyObject*)(rets))); | |||
717 | Py_XDECREF(args)_Py_XDECREF(((PyObject*)(args))); | |||
718 | return PyErr_Occurred() ? 0 : 1; | |||
719 | } | |||
720 | ||||
721 | ||||
722 | static PyObject *Py_GeometricTransform(PyObject *obj, PyObject *args) | |||
723 | { | |||
724 | PyArrayObject *input = NULL((void*)0), *output = NULL((void*)0); | |||
725 | PyArrayObject *coordinates = NULL((void*)0), *matrix = NULL((void*)0), *shift = NULL((void*)0); | |||
726 | PyObject *fnc = NULL((void*)0), *extra_arguments = NULL((void*)0), *extra_keywords = NULL((void*)0); | |||
727 | int mode, order, nprepad; | |||
728 | double cval; | |||
729 | void *func = NULL((void*)0), *data = NULL((void*)0); | |||
730 | NI_PythonCallbackData cbdata; | |||
731 | ccallback_t callback; | |||
732 | static ccallback_signature_t callback_signatures[] = { | |||
733 | {"int (intptr_t *, double *, int, int, void *)"}, | |||
734 | {"int (npy_intp *, double *, int, int, void *)"}, | |||
735 | #if NPY_SIZEOF_INTP8 == NPY_SIZEOF_SHORT2 | |||
736 | {"int (short *, double *, int, int, void *)"}, | |||
737 | #endif | |||
738 | #if NPY_SIZEOF_INTP8 == NPY_SIZEOF_INT4 | |||
739 | {"int (int *, double *, int, int, void *)"}, | |||
740 | #endif | |||
741 | #if NPY_SIZEOF_INTP8 == NPY_SIZEOF_LONG8 | |||
742 | {"int (long *, double *, int, int, void *)"}, | |||
743 | #endif | |||
744 | #if NPY_SIZEOF_INTP8 == NPY_SIZEOF_LONGLONG8 | |||
745 | {"int (long long *, double *, int, int, void *)"}, | |||
746 | #endif | |||
747 | {NULL((void*)0)} | |||
748 | }; | |||
749 | ||||
750 | callback.py_function = NULL((void*)0); | |||
751 | callback.c_function = NULL((void*)0); | |||
752 | ||||
753 | if (!PyArg_ParseTuple(args, "O&OO&O&O&O&iidiOO", | |||
754 | NI_ObjectToInputArray, &input, | |||
755 | &fnc, | |||
756 | NI_ObjectToOptionalInputArray, &coordinates, | |||
757 | NI_ObjectToOptionalInputArray, &matrix, | |||
758 | NI_ObjectToOptionalInputArray, &shift, | |||
759 | NI_ObjectToOutputArray, &output, | |||
760 | &order, &mode, &cval, &nprepad, | |||
761 | &extra_arguments, &extra_keywords)) | |||
762 | goto exit; | |||
763 | ||||
764 | if (fnc != Py_None(&_Py_NoneStruct)) { | |||
765 | if (!PyTuple_Check(extra_arguments)((((((PyObject*)(extra_arguments))->ob_type))->tp_flags & ((1UL << 26))) != 0)) { | |||
766 | PyErr_SetString(PyExc_RuntimeError, | |||
767 | "extra_arguments must be a tuple"); | |||
768 | goto exit; | |||
769 | } | |||
770 | if (!PyDict_Check(extra_keywords)((((((PyObject*)(extra_keywords))->ob_type))->tp_flags & ((1UL << 29))) != 0)) { | |||
771 | PyErr_SetString(PyExc_RuntimeError, | |||
772 | "extra_keywords must be a dictionary"); | |||
773 | goto exit; | |||
774 | } | |||
775 | if (PyCapsule_CheckExact(fnc)((((PyObject*)(fnc))->ob_type) == &PyCapsule_Type) && PyCapsule_GetName(fnc) == NULL((void*)0)) { | |||
776 | func = PyCapsule_GetPointer(fnc, NULL((void*)0)); | |||
777 | data = PyCapsule_GetContext(fnc); | |||
778 | } else { | |||
779 | int ret; | |||
780 | ||||
781 | ret = ccallback_prepare(&callback, callback_signatures, fnc, CCALLBACK_DEFAULTS0x0); | |||
782 | if (ret == -1) { | |||
783 | goto exit; | |||
784 | } | |||
785 | ||||
786 | if (callback.py_function != NULL((void*)0)) { | |||
787 | cbdata.extra_arguments = extra_arguments; | |||
788 | cbdata.extra_keywords = extra_keywords; | |||
789 | callback.info_p = (void*)&cbdata; | |||
790 | func = Py_Map; | |||
791 | data = (void*)&callback; | |||
792 | } | |||
793 | else { | |||
794 | func = callback.c_function; | |||
795 | data = callback.user_data; | |||
796 | } | |||
797 | } | |||
798 | } | |||
799 | ||||
800 | NI_GeometricTransform(input, func, data, matrix, shift, coordinates, | |||
801 | output, order, (NI_ExtendMode)mode, cval, nprepad); | |||
802 | #ifdef HAVE_WRITEBACKIFCOPY | |||
803 | PyArray_ResolveWritebackIfCopy(*(int (*)(PyArrayObject *)) _scipy_ndimage_ARRAY_API[302])(output); | |||
804 | #endif | |||
805 | ||||
806 | exit: | |||
807 | if (callback.py_function != NULL((void*)0) || callback.c_function != NULL((void*)0)) { | |||
808 | ccallback_release(&callback); | |||
809 | } | |||
810 | Py_XDECREF(input)_Py_XDECREF(((PyObject*)(input))); | |||
811 | Py_XDECREF(output)_Py_XDECREF(((PyObject*)(output))); | |||
812 | Py_XDECREF(coordinates)_Py_XDECREF(((PyObject*)(coordinates))); | |||
813 | Py_XDECREF(matrix)_Py_XDECREF(((PyObject*)(matrix))); | |||
814 | Py_XDECREF(shift)_Py_XDECREF(((PyObject*)(shift))); | |||
815 | return PyErr_Occurred() ? NULL((void*)0) : Py_BuildValue(""); | |||
816 | } | |||
817 | ||||
818 | static PyObject *Py_ZoomShift(PyObject *obj, PyObject *args) | |||
819 | { | |||
820 | PyArrayObject *input = NULL((void*)0), *output = NULL((void*)0), *shift = NULL((void*)0); | |||
821 | PyArrayObject *zoom = NULL((void*)0); | |||
822 | int mode, order, nprepad, grid_mode; | |||
823 | double cval; | |||
824 | ||||
825 | if (!PyArg_ParseTuple(args, "O&O&O&O&iidii", | |||
826 | NI_ObjectToInputArray, &input, | |||
827 | NI_ObjectToOptionalInputArray, &zoom, | |||
828 | NI_ObjectToOptionalInputArray, &shift, | |||
829 | NI_ObjectToOutputArray, &output, | |||
830 | &order, &mode, &cval, &nprepad, &grid_mode)) | |||
831 | goto exit; | |||
832 | ||||
833 | NI_ZoomShift(input, zoom, shift, output, order, (NI_ExtendMode)mode, cval, | |||
834 | nprepad, grid_mode); | |||
835 | #ifdef HAVE_WRITEBACKIFCOPY | |||
836 | PyArray_ResolveWritebackIfCopy(*(int (*)(PyArrayObject *)) _scipy_ndimage_ARRAY_API[302])(output); | |||
837 | #endif | |||
838 | ||||
839 | exit: | |||
840 | Py_XDECREF(input)_Py_XDECREF(((PyObject*)(input))); | |||
841 | Py_XDECREF(shift)_Py_XDECREF(((PyObject*)(shift))); | |||
842 | Py_XDECREF(zoom)_Py_XDECREF(((PyObject*)(zoom))); | |||
843 | Py_XDECREF(output)_Py_XDECREF(((PyObject*)(output))); | |||
844 | return PyErr_Occurred() ? NULL((void*)0) : Py_BuildValue(""); | |||
845 | } | |||
846 | ||||
847 | static PyObject *Py_FindObjects(PyObject *obj, PyObject *args) | |||
848 | { | |||
849 | PyArrayObject *input = NULL((void*)0); | |||
850 | PyObject *result = NULL((void*)0), *tuple = NULL((void*)0), *start = NULL((void*)0), *end = NULL((void*)0); | |||
851 | PyObject *slc = NULL((void*)0); | |||
852 | int jj; | |||
853 | npy_intp max_label; | |||
854 | npy_intp ii, *regions = NULL((void*)0); | |||
855 | ||||
856 | if (!PyArg_ParseTuple(args, "O&n", | |||
857 | NI_ObjectToInputArray, &input, &max_label)) | |||
858 | goto exit; | |||
859 | ||||
860 | if (max_label < 0) | |||
861 | max_label = 0; | |||
862 | if (max_label > 0) { | |||
863 | if (PyArray_NDIM(input) > 0) { | |||
864 | regions = (npy_intp*)malloc(2 * max_label * PyArray_NDIM(input) * | |||
865 | sizeof(npy_intp)); | |||
866 | } else { | |||
867 | regions = (npy_intp*)malloc(max_label * sizeof(npy_intp)); | |||
868 | } | |||
869 | if (!regions) { | |||
870 | PyErr_NoMemory(); | |||
871 | goto exit; | |||
872 | } | |||
873 | } | |||
874 | ||||
875 | if (!NI_FindObjects(input, max_label, regions)) | |||
876 | goto exit; | |||
877 | ||||
878 | result = PyList_New(max_label); | |||
879 | if (!result) { | |||
880 | PyErr_NoMemory(); | |||
881 | goto exit; | |||
882 | } | |||
883 | ||||
884 | for(ii = 0; ii < max_label; ii++) { | |||
885 | npy_intp idx = | |||
886 | PyArray_NDIM(input) > 0 ? 2 * PyArray_NDIM(input) * ii : ii; | |||
887 | if (regions[idx] >= 0) { | |||
888 | PyObject *tuple = PyTuple_New(PyArray_NDIM(input)); | |||
889 | if (!tuple) { | |||
890 | PyErr_NoMemory(); | |||
891 | goto exit; | |||
892 | } | |||
893 | for(jj = 0; jj < PyArray_NDIM(input); jj++) { | |||
894 | start = PyLong_FromSsize_t(regions[idx + jj]); | |||
895 | end = PyLong_FromSsize_t(regions[idx + jj + | |||
896 | PyArray_NDIM(input)]); | |||
897 | if (!start || !end) { | |||
898 | PyErr_NoMemory(); | |||
899 | goto exit; | |||
900 | } | |||
901 | slc = PySlice_New(start, end, NULL((void*)0)); | |||
902 | if (!slc) { | |||
903 | PyErr_NoMemory(); | |||
904 | goto exit; | |||
905 | } | |||
906 | Py_XDECREF(start)_Py_XDECREF(((PyObject*)(start))); | |||
907 | Py_XDECREF(end)_Py_XDECREF(((PyObject*)(end))); | |||
908 | start = end = NULL((void*)0); | |||
909 | PyTuple_SetItem(tuple, jj, slc); | |||
910 | slc = NULL((void*)0); | |||
911 | } | |||
912 | PyList_SetItem(result, ii, tuple); | |||
913 | tuple = NULL((void*)0); | |||
914 | } else { | |||
915 | Py_INCREF(Py_None)_Py_INCREF(((PyObject*)((&_Py_NoneStruct)))); | |||
916 | PyList_SetItem(result, ii, Py_None(&_Py_NoneStruct)); | |||
917 | } | |||
918 | } | |||
919 | ||||
920 | Py_INCREF(result)_Py_INCREF(((PyObject*)(result))); | |||
921 | ||||
922 | exit: | |||
923 | Py_XDECREF(input)_Py_XDECREF(((PyObject*)(input))); | |||
924 | Py_XDECREF(result)_Py_XDECREF(((PyObject*)(result))); | |||
925 | Py_XDECREF(tuple)_Py_XDECREF(((PyObject*)(tuple))); | |||
926 | Py_XDECREF(start)_Py_XDECREF(((PyObject*)(start))); | |||
927 | Py_XDECREF(end)_Py_XDECREF(((PyObject*)(end))); | |||
928 | Py_XDECREF(slc)_Py_XDECREF(((PyObject*)(slc))); | |||
929 | free(regions); | |||
930 | if (PyErr_Occurred()) { | |||
931 | Py_XDECREF(result)_Py_XDECREF(((PyObject*)(result))); | |||
932 | return NULL((void*)0); | |||
933 | } else { | |||
934 | return result; | |||
935 | } | |||
936 | } | |||
937 | ||||
938 | static PyObject *Py_WatershedIFT(PyObject *obj, PyObject *args) | |||
939 | { | |||
940 | PyArrayObject *input = NULL((void*)0), *output = NULL((void*)0), *markers = NULL((void*)0); | |||
941 | PyArrayObject *strct = NULL((void*)0); | |||
942 | ||||
943 | if (!PyArg_ParseTuple(args, "O&O&O&O&", NI_ObjectToInputArray, &input, | |||
944 | NI_ObjectToInputArray, &markers, NI_ObjectToInputArray, | |||
945 | &strct, NI_ObjectToOutputArray, &output)) | |||
946 | goto exit; | |||
947 | ||||
948 | NI_WatershedIFT(input, markers, strct, output); | |||
949 | #ifdef HAVE_WRITEBACKIFCOPY | |||
950 | PyArray_ResolveWritebackIfCopy(*(int (*)(PyArrayObject *)) _scipy_ndimage_ARRAY_API[302])(output); | |||
951 | #endif | |||
952 | ||||
953 | exit: | |||
954 | Py_XDECREF(input)_Py_XDECREF(((PyObject*)(input))); | |||
955 | Py_XDECREF(markers)_Py_XDECREF(((PyObject*)(markers))); | |||
956 | Py_XDECREF(strct)_Py_XDECREF(((PyObject*)(strct))); | |||
957 | Py_XDECREF(output)_Py_XDECREF(((PyObject*)(output))); | |||
958 | return PyErr_Occurred() ? NULL((void*)0) : Py_BuildValue(""); | |||
959 | } | |||
960 | ||||
961 | static PyObject *Py_DistanceTransformBruteForce(PyObject *obj, | |||
962 | PyObject *args) | |||
963 | { | |||
964 | PyArrayObject *input = NULL((void*)0), *output = NULL((void*)0), *features = NULL((void*)0); | |||
965 | PyArrayObject *sampling = NULL((void*)0); | |||
966 | int metric; | |||
967 | ||||
968 | if (!PyArg_ParseTuple(args, "O&iO&O&O&", | |||
969 | NI_ObjectToInputArray, &input, | |||
970 | &metric, | |||
971 | NI_ObjectToOptionalInputArray, &sampling, | |||
972 | NI_ObjectToOptionalOutputArray, &output, | |||
973 | NI_ObjectToOptionalOutputArray, &features)) | |||
974 | goto exit; | |||
975 | ||||
976 | NI_DistanceTransformBruteForce(input, metric, sampling, output, features); | |||
977 | #ifdef HAVE_WRITEBACKIFCOPY | |||
978 | PyArray_ResolveWritebackIfCopy(*(int (*)(PyArrayObject *)) _scipy_ndimage_ARRAY_API[302])(output); | |||
979 | #endif | |||
980 | ||||
981 | exit: | |||
982 | Py_XDECREF(input)_Py_XDECREF(((PyObject*)(input))); | |||
983 | Py_XDECREF(sampling)_Py_XDECREF(((PyObject*)(sampling))); | |||
984 | Py_XDECREF(output)_Py_XDECREF(((PyObject*)(output))); | |||
985 | Py_XDECREF(features)_Py_XDECREF(((PyObject*)(features))); | |||
986 | return PyErr_Occurred() ? NULL((void*)0) : Py_BuildValue(""); | |||
987 | } | |||
988 | ||||
989 | static PyObject *Py_DistanceTransformOnePass(PyObject *obj, PyObject *args) | |||
990 | { | |||
991 | PyArrayObject *strct = NULL((void*)0), *distances = NULL((void*)0), *features = NULL((void*)0); | |||
992 | ||||
993 | if (!PyArg_ParseTuple(args, "O&O&O&", | |||
994 | NI_ObjectToInputArray, &strct, | |||
995 | NI_ObjectToInputOutputArray, &distances, | |||
996 | NI_ObjectToOptionalOutputArray, &features)) | |||
997 | goto exit; | |||
998 | ||||
999 | NI_DistanceTransformOnePass(strct, distances, features); | |||
1000 | ||||
1001 | exit: | |||
1002 | Py_XDECREF(strct)_Py_XDECREF(((PyObject*)(strct))); | |||
1003 | Py_XDECREF(distances)_Py_XDECREF(((PyObject*)(distances))); | |||
1004 | Py_XDECREF(features)_Py_XDECREF(((PyObject*)(features))); | |||
1005 | return PyErr_Occurred() ? NULL((void*)0) : Py_BuildValue(""); | |||
1006 | } | |||
1007 | ||||
1008 | static PyObject *Py_EuclideanFeatureTransform(PyObject *obj, | |||
1009 | PyObject *args) | |||
1010 | { | |||
1011 | PyArrayObject *input = NULL((void*)0), *features = NULL((void*)0), *sampling = NULL((void*)0); | |||
1012 | ||||
1013 | if (!PyArg_ParseTuple(args, "O&O&O&", | |||
1014 | NI_ObjectToInputArray, &input, | |||
1015 | NI_ObjectToOptionalInputArray, &sampling, | |||
1016 | NI_ObjectToOutputArray, &features)) | |||
1017 | goto exit; | |||
1018 | ||||
1019 | NI_EuclideanFeatureTransform(input, sampling, features); | |||
1020 | ||||
1021 | exit: | |||
1022 | Py_XDECREF(input)_Py_XDECREF(((PyObject*)(input))); | |||
1023 | Py_XDECREF(sampling)_Py_XDECREF(((PyObject*)(sampling))); | |||
1024 | Py_XDECREF(features)_Py_XDECREF(((PyObject*)(features))); | |||
1025 | return PyErr_Occurred() ? NULL((void*)0) : Py_BuildValue(""); | |||
1026 | } | |||
1027 | ||||
1028 | static void _FreeCoordinateList(PyObject *obj) | |||
1029 | { | |||
1030 | NI_FreeCoordinateList((NI_CoordinateList*)PyCapsule_GetPointer(obj, NULL((void*)0))); | |||
1031 | } | |||
1032 | ||||
1033 | static PyObject *Py_BinaryErosion(PyObject *obj, PyObject *args) | |||
1034 | { | |||
1035 | PyArrayObject *input = NULL((void*)0), *output = NULL((void*)0), *strct = NULL((void*)0); | |||
1036 | PyArrayObject *mask = NULL((void*)0); | |||
1037 | PyObject *cobj = NULL((void*)0); | |||
1038 | int border_value, invert, center_is_true; | |||
1039 | int changed = 0, return_coordinates; | |||
1040 | NI_CoordinateList *coordinate_list = NULL((void*)0); | |||
1041 | PyArray_Dims origin = {NULL((void*)0), 0}; | |||
1042 | ||||
1043 | if (!PyArg_ParseTuple(args, "O&O&O&O&iO&iii", | |||
1044 | NI_ObjectToInputArray, &input, | |||
1045 | NI_ObjectToInputArray, &strct, | |||
1046 | NI_ObjectToOptionalInputArray, &mask, | |||
1047 | NI_ObjectToOutputArray, &output, | |||
1048 | &border_value, | |||
1049 | PyArray_IntpConverter(*(int (*)(PyObject *, PyArray_Dims *)) _scipy_ndimage_ARRAY_API [176]), &origin, | |||
1050 | &invert, ¢er_is_true, &return_coordinates)) { | |||
1051 | goto exit; | |||
1052 | } | |||
1053 | if (!_validate_origin(input, origin)) { | |||
1054 | goto exit; | |||
1055 | } | |||
1056 | if (!NI_BinaryErosion(input, strct, mask, output, border_value, | |||
1057 | origin.ptr, invert, center_is_true, &changed, | |||
1058 | return_coordinates ? &coordinate_list : NULL((void*)0))) { | |||
1059 | goto exit; | |||
1060 | } | |||
1061 | if (return_coordinates) { | |||
1062 | cobj = PyCapsule_New(coordinate_list, NULL((void*)0), _FreeCoordinateList); | |||
1063 | } | |||
1064 | #ifdef HAVE_WRITEBACKIFCOPY | |||
1065 | PyArray_ResolveWritebackIfCopy(*(int (*)(PyArrayObject *)) _scipy_ndimage_ARRAY_API[302])(output); | |||
1066 | #endif | |||
1067 | ||||
1068 | exit: | |||
1069 | Py_XDECREF(input)_Py_XDECREF(((PyObject*)(input))); | |||
1070 | Py_XDECREF(strct)_Py_XDECREF(((PyObject*)(strct))); | |||
1071 | Py_XDECREF(mask)_Py_XDECREF(((PyObject*)(mask))); | |||
1072 | Py_XDECREF(output)_Py_XDECREF(((PyObject*)(output))); | |||
1073 | PyDimMem_FREE(origin.ptr)PyMem_RawFree(origin.ptr); | |||
1074 | if (PyErr_Occurred()) { | |||
1075 | Py_XDECREF(cobj)_Py_XDECREF(((PyObject*)(cobj))); | |||
1076 | return NULL((void*)0); | |||
1077 | } else { | |||
1078 | if (return_coordinates) { | |||
1079 | return Py_BuildValue("iN", changed, cobj); | |||
1080 | } else { | |||
1081 | return Py_BuildValue("i", changed); | |||
1082 | } | |||
1083 | } | |||
1084 | } | |||
1085 | ||||
1086 | static PyObject *Py_BinaryErosion2(PyObject *obj, PyObject *args) | |||
1087 | { | |||
1088 | PyArrayObject *array = NULL((void*)0), *strct = NULL((void*)0), *mask = NULL((void*)0); | |||
1089 | PyObject *cobj = NULL((void*)0); | |||
1090 | int invert, niter; | |||
1091 | PyArray_Dims origin = {NULL((void*)0), 0}; | |||
1092 | ||||
1093 | if (!PyArg_ParseTuple(args, "O&O&O&iO&iO", | |||
1094 | NI_ObjectToInputOutputArray, &array, | |||
1095 | NI_ObjectToInputArray, &strct, | |||
1096 | NI_ObjectToOptionalInputArray, | |||
1097 | &mask, &niter, | |||
1098 | PyArray_IntpConverter(*(int (*)(PyObject *, PyArray_Dims *)) _scipy_ndimage_ARRAY_API [176]), &origin, | |||
1099 | &invert, &cobj)) { | |||
1100 | goto exit; | |||
1101 | } | |||
1102 | if (!_validate_origin(array, origin)) { | |||
1103 | goto exit; | |||
1104 | } | |||
1105 | if (PyCapsule_CheckExact(cobj)((((PyObject*)(cobj))->ob_type) == &PyCapsule_Type)) { | |||
1106 | NI_CoordinateList *cobj_data = PyCapsule_GetPointer(cobj, NULL((void*)0)); | |||
1107 | if (!NI_BinaryErosion2(array, strct, mask, niter, origin.ptr, invert, | |||
1108 | &cobj_data)) { | |||
1109 | goto exit; | |||
1110 | } | |||
1111 | } | |||
1112 | else { | |||
1113 | PyErr_SetString(PyExc_RuntimeError, "cannot convert CObject"); | |||
1114 | } | |||
1115 | exit: | |||
1116 | Py_XDECREF(array)_Py_XDECREF(((PyObject*)(array))); | |||
1117 | Py_XDECREF(strct)_Py_XDECREF(((PyObject*)(strct))); | |||
1118 | Py_XDECREF(mask)_Py_XDECREF(((PyObject*)(mask))); | |||
1119 | PyDimMem_FREE(origin.ptr)PyMem_RawFree(origin.ptr); | |||
1120 | return PyErr_Occurred() ? NULL((void*)0) : Py_BuildValue(""); | |||
1121 | } | |||
1122 | ||||
1123 | static PyMethodDef methods[] = { | |||
1124 | {"correlate1d", (PyCFunction)Py_Correlate1D, | |||
1125 | METH_VARARGS0x0001, NULL((void*)0)}, | |||
1126 | {"correlate", (PyCFunction)Py_Correlate, | |||
1127 | METH_VARARGS0x0001, NULL((void*)0)}, | |||
1128 | {"uniform_filter1d", (PyCFunction)Py_UniformFilter1D, | |||
1129 | METH_VARARGS0x0001, NULL((void*)0)}, | |||
1130 | {"min_or_max_filter1d", (PyCFunction)Py_MinOrMaxFilter1D, | |||
1131 | METH_VARARGS0x0001, NULL((void*)0)}, | |||
1132 | {"min_or_max_filter", (PyCFunction)Py_MinOrMaxFilter, | |||
1133 | METH_VARARGS0x0001, NULL((void*)0)}, | |||
1134 | {"rank_filter", (PyCFunction)Py_RankFilter, | |||
1135 | METH_VARARGS0x0001, NULL((void*)0)}, | |||
1136 | {"generic_filter", (PyCFunction)Py_GenericFilter, | |||
1137 | METH_VARARGS0x0001, NULL((void*)0)}, | |||
1138 | {"generic_filter1d", (PyCFunction)Py_GenericFilter1D, | |||
1139 | METH_VARARGS0x0001, NULL((void*)0)}, | |||
1140 | {"fourier_filter", (PyCFunction)Py_FourierFilter, | |||
1141 | METH_VARARGS0x0001, NULL((void*)0)}, | |||
1142 | {"fourier_shift", (PyCFunction)Py_FourierShift, | |||
1143 | METH_VARARGS0x0001, NULL((void*)0)}, | |||
1144 | {"spline_filter1d", (PyCFunction)Py_SplineFilter1D, | |||
1145 | METH_VARARGS0x0001, NULL((void*)0)}, | |||
1146 | {"geometric_transform", (PyCFunction)Py_GeometricTransform, | |||
1147 | METH_VARARGS0x0001, NULL((void*)0)}, | |||
1148 | {"zoom_shift", (PyCFunction)Py_ZoomShift, | |||
1149 | METH_VARARGS0x0001, NULL((void*)0)}, | |||
1150 | {"find_objects", (PyCFunction)Py_FindObjects, | |||
1151 | METH_VARARGS0x0001, NULL((void*)0)}, | |||
1152 | {"watershed_ift", (PyCFunction)Py_WatershedIFT, | |||
1153 | METH_VARARGS0x0001, NULL((void*)0)}, | |||
1154 | {"distance_transform_bf", (PyCFunction)Py_DistanceTransformBruteForce, | |||
1155 | METH_VARARGS0x0001, NULL((void*)0)}, | |||
1156 | {"distance_transform_op", (PyCFunction)Py_DistanceTransformOnePass, | |||
1157 | METH_VARARGS0x0001, NULL((void*)0)}, | |||
1158 | {"euclidean_feature_transform", | |||
1159 | (PyCFunction)Py_EuclideanFeatureTransform, | |||
1160 | METH_VARARGS0x0001, NULL((void*)0)}, | |||
1161 | {"binary_erosion", (PyCFunction)Py_BinaryErosion, | |||
1162 | METH_VARARGS0x0001, NULL((void*)0)}, | |||
1163 | {"binary_erosion2", (PyCFunction)Py_BinaryErosion2, | |||
1164 | METH_VARARGS0x0001, NULL((void*)0)}, | |||
1165 | {NULL((void*)0), NULL((void*)0), 0, NULL((void*)0)} | |||
1166 | }; | |||
1167 | ||||
1168 | static struct PyModuleDef moduledef = { | |||
1169 | PyModuleDef_HEAD_INIT{ { 1, ((void*)0) }, ((void*)0), 0, ((void*)0), }, | |||
1170 | "_nd_image", | |||
1171 | NULL((void*)0), | |||
1172 | -1, | |||
1173 | methods, | |||
1174 | NULL((void*)0), | |||
1175 | NULL((void*)0), | |||
1176 | NULL((void*)0), | |||
1177 | NULL((void*)0) | |||
1178 | }; | |||
1179 | ||||
1180 | PyObject *PyInit__nd_image(void) | |||
1181 | { | |||
1182 | PyObject *m; | |||
1183 | ||||
1184 | m = PyModule_Create(&moduledef)PyModule_Create2(&moduledef, 1013); | |||
| ||||
| ||||
1185 | import_array(){if (_import_array() < 0) {PyErr_Print(); PyErr_SetString( PyExc_ImportError, "numpy.core.multiarray failed to import"); return ((void*)0); } }; | |||
1186 | ||||
1187 | return m; | |||
1188 | } |
1 | #ifndef PyModule_Create2 |
2 | struct _object; |
3 | typedef struct _object PyObject; |
4 | PyObject* clang_analyzer_PyObject_New_Reference(); |
5 | PyObject* PyModule_Create2(PyModuleDef *def, int module_api_version) { |
6 | return clang_analyzer_PyObject_New_Reference(); |
7 | } |
8 | #else |
9 | #warning "API PyModule_Create2 is defined as a macro." |
10 | #endif |