5 int PySwigObject_Check(PyObject *op);
7 /* Py_ssize_t for old Pythons */
8 #if PY_VERSION_HEX < 0x02050000
9 typedef int Py_ssize_t;
12 PyObject * PyTuple_FromIntArray(int * arr, int len){
13 PyObject * obj = PyTuple_New(len);
14 for(int i=0; i<len; i++){
15 PyTuple_SetItem(obj, i, PyLong_FromLong( arr[i] ) );
20 PyObject * SWIG_SetResult(PyObject * result, PyObject * obj){
24 result = PyTuple_New(1);
25 PyTuple_SetItem(result, 0, obj);
29 PyObject * SWIG_AppendResult(PyObject * result, PyObject ** to_add, int num){
30 if ((!result) || (result == Py_None)) {
31 /* no other results, so just add our values */
33 /* if only one object, return that */
38 /* create a new tuple to put in our new pointer python objects */
39 result = PyTuple_New (num);
41 /* put in our new pointer python objects */
42 for(int i=0; i<num; i++){
43 PyTuple_SetItem (result, i, to_add[i]);
47 /* we have other results, so add it to the end */
49 if (!PyTuple_Check (result)) {
50 /* previous result is not a tuple, so create one and put
51 previous result and current pointer in it */
53 /* first, save previous result */
54 PyObject *obj_save = result;
56 /* then, create the tuple */
57 result = PyTuple_New (1);
59 /* finaly, put the saved value in the tuple */
60 PyTuple_SetItem (result, 0, obj_save);
63 /* create a new tuple to put in our new pointer python object */
64 PyObject *my_obj = PyTuple_New (num);
66 /* put in our new pointer python object */
67 for( int i=0; i<num ; i++ ){
68 PyTuple_SetItem (my_obj, i, to_add[i]);
71 /* save the previous result */
72 PyObject *obj_save = result;
74 /* concat previous and our new result */
75 result = PySequence_Concat (obj_save, my_obj);
77 /* decrement the usage of no more used objects */
85 std::ostream & cv_arr_write(std::ostream & out, T * data, int rows, int nch, int step){
87 char * cdata = (char *) data;
88 std::string chdelim1="", chdelim2="";
90 // only output channel parens if > 1
97 for(i=0; i<rows; i++){
102 out<<((T*)(cdata+i*step))[0];
103 for(k=1; k<nch; k++){
104 out<<", "<<((T*)(cdata+i*step))[k];
108 // remaining elements
109 for(j=nch*sizeof(T); j<step; j+=(nch*sizeof(T))){
111 out<<((T*)(cdata+i*step+j))[0];
112 for(k=1; k<nch; k++){
113 out<<", "<<((T*)(cdata+i*step+j))[k];
123 void cvArrPrint(CvArr * arr){
124 CV_FUNCNAME( "cvArrPrint" );
130 mat = cvGetMat(arr, &stub);
132 int cn = CV_MAT_CN(mat->type);
133 int depth = CV_MAT_DEPTH(mat->type);
134 int step = MAX(mat->step, cn*mat->cols*CV_ELEM_SIZE(depth));
135 std::ostringstream str;
139 cv_arr_write(str, (uchar *)mat->data.ptr, mat->rows, cn, step);
142 cv_arr_write(str, (char *)mat->data.ptr, mat->rows, cn, step);
145 cv_arr_write(str, (ushort *)mat->data.ptr, mat->rows, cn, step);
148 cv_arr_write(str, (short *)mat->data.ptr, mat->rows, cn, step);
151 cv_arr_write(str, (int *)mat->data.ptr, mat->rows, cn, step);
154 cv_arr_write(str, (float *)mat->data.ptr, mat->rows, cn, step);
157 cv_arr_write(str, (double *)mat->data.ptr, mat->rows, cn, step);
160 CV_ERROR( CV_StsError, "Unknown element type");
163 std::cout<<str.str()<<std::endl;
168 // deal with negative array indices
169 int PyLong_AsIndex( PyObject * idx_object, int len ){
170 int idx = PyLong_AsLong( idx_object );
171 if(idx<0) return len+idx;
175 CvRect PySlice_to_CvRect(CvArr * src, PyObject * idx_object){
176 CvSize sz = cvGetSize(src);
177 //printf("Size %dx%d\n", sz.height, sz.width);
178 int lower[2], upper[2];
179 Py_ssize_t len, start, stop, step, slicelength;
181 if(PyInt_Check(idx_object) || PyLong_Check(idx_object)){
182 // if array is a row vector, assume index into columns
184 lower[0] = PyLong_AsIndex( idx_object, sz.height );
185 upper[0] = lower[0] + 1;
191 upper[0] = sz.height;
192 lower[1] = PyLong_AsIndex( idx_object, sz.width );
193 upper[1] = lower[1]+1;
198 else if(PySlice_Check(idx_object)){
200 if(PySlice_GetIndicesEx( (PySliceObject*)idx_object, len, &start, &stop, &step, &slicelength )!=0){
201 printf("Error in PySlice_GetIndicesEx: returning NULL");
202 PyErr_SetString(PyExc_Exception, "Error");
203 return cvRect(0,0,0,0);
205 // if array is a row vector, assume index bounds are into columns
207 lower[0] = (int) start; // use c convention of start index = 0
208 upper[0] = (int) stop; // use c convention
213 lower[1] = (int) start; // use c convention of start index = 0
214 upper[1] = (int) stop; // use c convention
216 upper[0] = sz.height;
221 else if(PyTuple_Check(idx_object)){
222 //printf("PyTuple{\n");
223 if(PyObject_Length(idx_object)!=2){
224 //printf("Expected a sequence of length 2: returning NULL");
225 PyErr_SetString(PyExc_ValueError, "Expected a sequence with 2 elements");
226 return cvRect(0,0,0,0);
228 for(int i=0; i<2; i++){
229 PyObject *o = PyTuple_GetItem(idx_object, i);
231 // 2a. Slice -- same as above
232 if(PySlice_Check(o)){
233 //printf("PySlice\n");
234 len = (i==0 ? sz.height : sz.width);
235 if(PySlice_GetIndicesEx( (PySliceObject*)o, len, &start, &stop, &step, &slicelength )!=0){
236 PyErr_SetString(PyExc_Exception, "Error");
237 printf("Error in PySlice_GetIndicesEx: returning NULL");
238 return cvRect(0,0,0,0);
240 //printf("PySlice_GetIndecesEx(%d, %d, %d, %d, %d)\n", len, start, stop, step, slicelength);
247 else if(PyInt_Check(o) || PyLong_Check(o)){
249 lower[i] = PyLong_AsIndex(o, i==0 ? sz.height : sz.width);
250 upper[i] = lower[i]+1;
254 PyErr_SetString(PyExc_TypeError, "Expected a sequence of slices or integers");
255 printf("Expected a slice or int as sequence item: returning NULL");
256 return cvRect(0,0,0,0);
262 PyErr_SetString( PyExc_TypeError, "Expected a slice or sequence");
263 printf("Expected a slice or sequence: returning NULL");
264 return cvRect(0,0,0,0);
267 //lower[0] = MAX(0, lower[0]);
268 //lower[1] = MAX(0, lower[1]);
269 //upper[0] = MIN(sz.height, upper[0]);
270 //upper[1] = MIN(sz.width, upper[1]);
271 //printf("Slice=%d %d %d %d\n", lower[0], upper[0], lower[1], upper[1]);
272 return cvRect(lower[1],lower[0], upper[1]-lower[1], upper[0]-lower[0]);
275 double PyObject_AsDouble(PyObject * obj){
276 if(PyNumber_Check(obj)){
277 if(PyFloat_Check(obj)){
278 return PyFloat_AsDouble(obj);
280 else if(PyInt_Check(obj) || PyLong_Check(obj)){
281 return (double) PyLong_AsLong(obj);
284 PyErr_SetString( PyExc_TypeError, "Could not convert python object to Double");
288 long PyObject_AsLong(PyObject * obj){
289 if(PyNumber_Check(obj)){
290 if(PyFloat_Check(obj)){
291 return (long) PyFloat_AsDouble(obj);
293 else if(PyInt_Check(obj) || PyLong_Check(obj)){
294 return PyLong_AsLong(obj);
297 PyErr_SetString( PyExc_TypeError, "Could not convert python object to Long");