| 1 |
/********************************************************************** |
| 2 |
Interface file for SWIG |
| 3 |
Author(s): [email protected] |
| 4 |
Copyright (c) 2016-2019 European Union (Joint Research Centre) |
| 5 |
|
| 6 |
This file is part of jiplib. |
| 7 |
|
| 8 |
jiplib is free software: you can redistribute it and/or modify |
| 9 |
it under the terms of the GNU General Public License as published by |
| 10 |
the Free Software Foundation, either version 3 of the License, or |
| 11 |
(at your option) any later version. |
| 12 |
|
| 13 |
jiplib is distributed in the hope that it will be useful, |
| 14 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 |
GNU General Public License for more details. |
| 17 |
|
| 18 |
You should have received a copy of the GNU General Public License |
| 19 |
along with jiplib. If not, see <https://www.gnu.org/licenses/>. |
| 20 |
***********************************************************************/ |
| 21 |
%include constraints.i |
| 22 |
|
| 23 |
%include carrays.i |
| 24 |
%array_functions(IMAGE *, imap) |
| 25 |
%array_functions(int , intp) // used for example for box array |
| 26 |
%array_functions(double , doublep) |
| 27 |
|
| 28 |
// 20170317 |
| 29 |
// integer box array with 2,4, or 6 size parameters (1-D, 2-D, or 3-D images respectively) |
| 30 |
%typemap(in) (int *box) { |
| 31 |
int i, dim; |
| 32 |
$1 = (int *) calloc(6, sizeof(int)); |
| 33 |
if (!PySequence_Check($input)) { |
| 34 |
PyErr_SetString(PyExc_ValueError,"Expected a sequence"); |
| 35 |
return NULL; |
| 36 |
} |
| 37 |
dim=PySequence_Length($input); |
| 38 |
if ((dim!=2) || (dim!=4) || (dim!=6)){ |
| 39 |
for (i = 0; i < dim; i++) { |
| 40 |
PyObject *o = PySequence_GetItem($input,i); |
| 41 |
// https://docs.python.org/3.5/c-api/long.html |
| •42 |
if (PyLong_Check(o)) { |
| 43 |
$1[i] = (int)PyLong_AsLong(o); |
| 44 |
} |
| 45 |
else { |
| 46 |
PyErr_SetString(PyExc_ValueError,"Sequence elements must be integers"); |
| 47 |
free($1); |
| 48 |
return NULL; |
| 49 |
} |
| 50 |
} |
| 51 |
} |
| 52 |
else { |
| 53 |
PyErr_SetString(PyExc_ValueError,"Sequence elements must be equal to 2, 4, or 6 for the size of the [left, right], [left, right, top, bottom], or [left, right, top, bottom, up, down] borders respectively."); |
| 54 |
return NULL; |
| 55 |
} |
| 56 |
} |
| 57 |
|
| 58 |
//allow for overloading (due to int iband) |
| 59 |
%typemap(typecheck) (int *box) = PyObject *; |
| 60 |
%typemap(typecheck) (int *box, double d_gval, int iband){ |
| 61 |
$1=PySequence_Check($input) ? 1 : 0; |
| 62 |
} |
| 63 |
%typemap(typecheck) (int *box, double d_gval){ |
| 64 |
$1=PySequence_Check($input) ? 1 : 0; |
| 65 |
} |
| 66 |
%typemap(typecheck) (int *box, int iband){ |
| 67 |
$1=PySequence_Check($input) ? 1 : 0; |
| 68 |
} |
| 69 |
%typemap(typecheck) (int *box){ |
| 70 |
$1=PySequence_Check($input) ? 1 : 0; |
| 71 |
} |
| 72 |
// Free the box array |
| 73 |
%typemap(freearg) (int *box) { |
| 74 |
free($1); |
| 75 |
} |
| 76 |
|
| 77 |
// make sure box parameters are non-negative |
| 78 |
%typemap(check) int *box { |
| 79 |
int i; |
| 80 |
for (i=0; i<6; i++){ |
| 81 |
if ($1[i] < 0) { |
| 82 |
SWIG_exception(SWIG_ValueError, "Expected non-negative value."); |
| 83 |
} |
| 84 |
} |
| 85 |
} |
| 86 |
|
| 87 |
// 20160922 |
| 88 |
// Allow for automatic garbage collection (no need to patch!) |
| 89 |
%extend IMAGE { // Attach these functions to struct IMAGE |
| 90 |
IMAGE(int type, long int nx, int ny, int nz) { |
| 91 |
return create_image(type, nx,ny,nz); |
| 92 |
} |
| 93 |
~IMAGE() { |
| 94 |
free_image($self); |
| 95 |
} |
| 96 |
void iminfoMethod() { |
| 97 |
iminfo($self); |
| 98 |
} |
| 99 |
}; |
| 100 |
|
| 101 |
%typemap(newfree) IMAGE * { |
| 102 |
delete $1; |
| 103 |
} |
| 104 |
|
| 105 |
// Addtional code for IMAGE<->NumPy array conversions [20160729] |
| 106 |
// adapted from gdal_array.i |
| 107 |
|
| 108 |
%clear IMAGE *; |