Update to 2.0.0 tree from current Fremantle build
[opencv] / interfaces / swig / python / pytypemaps.i
1 /*M///////////////////////////////////////////////////////////////////////////////////////
2 //
3 //  IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
4 //
5 //  By downloading, copying, installing or using the software you agree to this license.
6 //  If you do not agree to this license, do not download, install,
7 //  copy or use the software.
8 //
9 //
10 //                        Intel License Agreement
11 //                For Open Source Computer Vision Library
12 //
13 // Copyright (C) 2000, Intel Corporation, all rights reserved.
14 // Third party copyrights are property of their respective owners.
15 //
16 // Redistribution and use in source and binary forms, with or without modification,
17 // are permitted provided that the following conditions are met:
18 //
19 //   * Redistribution's of source code must retain the above copyright notice,
20 //     this list of conditions and the following disclaimer.
21 //
22 //   * Redistribution's in binary form must reproduce the above copyright notice,
23 //     this list of conditions and the following disclaimer in the documentation
24 //     and/or other materials provided with the distribution.
25 //
26 //   * The name of Intel Corporation may not be used to endorse or promote products
27 //     derived from this software without specific prior written permission.
28 //
29 // This software is provided by the copyright holders and contributors "as is" and
30 // any express or implied warranties, including, but not limited to, the implied
31 // warranties of merchantability and fitness for a particular purpose are disclaimed.
32 // In no event shall the Intel Corporation or contributors be liable for any direct,
33 // indirect, incidental, special, exemplary, or consequential damages
34 // (including, but not limited to, procurement of substitute goods or services;
35 // loss of use, data, or profits; or business interruption) however caused
36 // and on any theory of liability, whether in contract, strict liability,
37 // or tort (including negligence or otherwise) arising in any way out of
38 // the use of this software, even if advised of the possibility of such damage.
39 //
40 //M*/
41 %include "exception.i"
42 %include "./pyhelpers.i"
43
44 %typemap(in) (CvArr *) (bool freearg=false) 
45 {
46   $1 = PyObject_to_CvArr($input, &freearg);
47 }
48
49 %typemap(freearg) (CvArr *) 
50 {
51   if($1!=NULL && freearg$argnum)
52   {
53     cvReleaseData( $1 );
54     cvFree(&($1));
55   }
56 }
57
58 %typemap(in) CvMat* (bool freearg=false), const CvMat* (bool freearg=false)
59 {
60   $1 = (CvMat*)PyObject_to_CvArr($input, &freearg);
61 }
62
63 %typemap(freearg) CvMat*,const CvMat* {
64   if($1!=NULL && freearg$argnum){
65     cvReleaseData( $1 );
66     cvFree(&($1));
67   }
68 }
69
70 /* typecheck typemaps */
71 %typecheck(SWIG_TYPECHECK_POINTER) CvArr * {
72     $1 = CvArr_Check( $input );
73 }
74
75 %typecheck(SWIG_TYPECHECK_POINTER) CvScalar {
76     $1 = CvScalar_Check( $input );
77 }
78
79 /* copy built-in swig typemaps for these types */
80 %typemap(typecheck) CvPoint = SWIGTYPE;
81 %typemap(typecheck) CvPoint2D32f = SWIGTYPE;
82 %typemap(typecheck) CvPoint3D32f = SWIGTYPE;
83 %typemap(typecheck) CvPoint2D64f = SWIGTYPE;
84 %typemap(typecheck) CvPoint3D64f = SWIGTYPE;
85 %typemap(typecheck) CvRect = SWIGTYPE;
86 %typemap(typecheck) CvSize = SWIGTYPE;
87 %typemap(typecheck) CvSize2D32f = SWIGTYPE;
88 %typemap(typecheck) CvSlice = SWIGTYPE;
89 %typemap(typecheck) CvBox2D = SWIGTYPE;
90 %typemap(typecheck) CvTermCriteria = SWIGTYPE;
91
92
93 // for cvReshape, cvGetRow, where header is passed, then filled in
94 %typemap(in, numinputs=0) CvMat * OUTPUT (CvMat * header, bool freearg=false) {
95         header = (CvMat *)cvAlloc(sizeof(CvMat));
96         $1 = header;
97 }
98
99 %apply CvMat * OUTPUT {CvMat * header};
100 %apply CvMat * OUTPUT {CvMat * submat};
101
102 %newobject cvReshape;
103 %newobject cvGetRow;
104 %newobject cvGetRows;
105 %newobject cvGetCol;
106 %newobject cvGetCols;
107 %newobject cvGetSubRect;
108 %newobject cvGetDiag;
109
110 /**
111  * In C, these functions assume input will always be around at least as long as header,
112  * presumably because the most common usage is to pass in a reference to a stack object.  
113  * i.e
114  * CvMat row;
115  * cvGetRow(A, &row, 0);
116  *
117  * As a result, the header is not refcounted (see the C source for cvGetRow, Reshape, in cxarray.cpp)
118  * However, in python, the header parameter is implicitly created so it is easier to create
119  * situations where the sub-array outlives the original header.  A simple example is:
120  * A = cvReshape(A, -1, A.rows*A.cols)
121  *
122  * since python doesn't have an assignment operator, the new header simply replaces the original,
123  * the refcount of the original goes to zero, and cvReleaseMat is called on the original, freeing both
124  * the header and data.  The new header is left pointing to invalid data.  To avoid this, need to add
125  * refcount field to the returned header.
126 */
127 %typemap(argout) (const CvArr* arr, CvMat* header) 
128 {
129         $2->hdr_refcount = ((CvMat *)$1)->hdr_refcount;
130         $2->refcount = ((CvMat *)$1)->refcount;
131         cvIncRefData($2);
132 }
133
134 %typemap(argout) (const CvArr* arr, CvMat* submat) 
135 {
136         $2->hdr_refcount = ((CvMat *)$1)->hdr_refcount;
137         $2->refcount = ((CvMat *)$1)->refcount;
138         cvIncRefData($2);
139 }
140
141 /* map scalar or sequence to CvScalar, CvPoint2D32f, CvPoint */
142 %typemap(in) (CvScalar) 
143 {
144         $1 = PyObject_to_CvScalar( $input );
145 }
146
147 //%typemap(in) (CvPoint) {
148 //      $1 = PyObject_to_CvPoint($input);
149 //}
150 //%typemap(in) (CvPoint2D32f) {
151 //      $1 = PyObject_to_CvPoint2D32f($input);
152 //}
153
154
155 // ============================================================================================
156
157 %define TUPLE_OR_TYPE (item, destination, typename, number, description, ...)
158 {
159   if (PySequence_Check(item)  &&  PySequence_Length(item) == number) 
160   {
161     PyObject * as_tuple = PySequence_Tuple (item);
162     if (!PyArg_ParseTuple (as_tuple, __VA_ARGS__)) 
163     {
164       PyErr_SetString(PyExc_TypeError, "each entry must consist of " # number " values " # description);
165       Py_DECREF (as_tuple);
166       return NULL;
167     }
168     Py_DECREF (as_tuple);
169   } 
170   else
171   {
172     typename * ptr;
173     if (SWIG_ConvertPtr (item, (void **) & ptr, $descriptor(typename *), SWIG_POINTER_EXCEPTION) == -1)
174     {
175       PyErr_SetString (PyExc_TypeError, "expected a sequence of " # number " values " # description " or a " # typename);
176       Py_DECREF (item);
177       return NULL;
178     }
179     destination = *ptr;
180   }
181 }
182 %enddef
183
184 %define INPUT_ARRAY_OF_TUPLES_OR_TYPES (typename, number, description, ...) 
185 {
186         if(! PySequence_Check ($input))
187   {
188                 PyErr_SetString(PyExc_TypeError, "Expected a list or tuple");
189                 return NULL;
190         }
191   
192   // TODO: will this ever be freed?
193   int count = PySequence_Size ($input);
194   int array = (typename *) malloc (count * sizeof (typename));
195   
196   // extract all the points values from the list */
197   typename * element = array;
198   for (int i = 0; i < count; i++, element++) 
199   {
200     PyObject * item = PySequence_GetItem ($input, i);
201     
202     // use the macro we have to expand a single entry
203     TUPLE_OR_TYPE (item, *element, typename, number, description, __VA_ARGS__)
204     // *corner, "ff", & corner->x, & corner->y
205   }
206   
207   // these are the arguments passed to the OpenCV function
208   $1 = array;
209   $2 = count;
210 }
211 %enddef
212
213
214 // ============================================================================================
215 // Tiny typemaps for tiny types ...
216
217 %typemap(in) CvRect (CvRect temp) 
218 //TUPLE_OR_TYPE ($input, $1, CvRect, 4, "(x,y,w,h)", "iiii", & temp.x, & temp.y, & temp.width, & temp.height)
219 {
220   if (PyTuple_Check($input)) 
221   {
222     if (!PyArg_ParseTuple($input,"iiii", & temp.x, & temp.y, & temp.width, & temp.height)) 
223     {
224       PyErr_SetString(PyExc_TypeError,"tuple must consist of 4 integers (x, y, width, height)");
225       return NULL;
226     }
227     $1 = temp;
228   } 
229   else
230   {
231     CvRect * ptr;
232     if (SWIG_ConvertPtr ($input, (void **) & ptr, SWIGTYPE_p_CvRect, SWIG_POINTER_EXCEPTION) == -1)
233     {
234       PyErr_SetString (PyExc_TypeError,"expected a tuple or a CvRect");
235       return NULL;
236     }
237     $1 = *ptr;
238   }
239 }
240
241
242 %typemap(in) CvTermCriteria (CvTermCriteria temp) 
243 {
244   if (PyTuple_Check($input)) 
245   {
246     if (!PyArg_ParseTuple($input,"iid", & temp.type, & temp.max_iter, & temp.epsilon)) 
247     {
248       PyErr_SetString(PyExc_TypeError,"tuple must consist of 2 integers and a float (type, max_iter, epsilon)");
249       return NULL;
250     }
251     $1 = temp;
252   } 
253   else
254   {
255     CvTermCriteria * ptr;
256     if (SWIG_ConvertPtr ($input, (void **) & ptr, SWIGTYPE_p_CvTermCriteria, SWIG_POINTER_EXCEPTION) == -1)
257     {
258       PyErr_SetString (PyExc_TypeError,"expected a tuple or a CvTermCriteria");
259       return NULL;
260     }
261     $1 = *ptr;
262   }
263 }
264
265
266 %typemap(in) CvPoint (CvPoint temp) 
267 {
268   if (PyTuple_Check($input)) 
269   {
270     if (!PyArg_ParseTuple($input,"ii", & temp.x, & temp.y)) 
271     {
272       PyErr_SetString(PyExc_TypeError,"tuple must consist of 2 integers (x, y)");
273       return NULL;
274     }
275     $1 = temp;
276   } 
277   else
278   {
279     CvPoint * ptr;
280     if (SWIG_ConvertPtr ($input, (void **) & ptr, SWIGTYPE_p_CvPoint, SWIG_POINTER_EXCEPTION) == -1)
281     {
282       PyErr_SetString (PyExc_TypeError,"expected a tuple or a CvPoint");
283       return NULL;
284     }
285     $1 = *ptr;
286   }
287 }
288
289
290 %typemap(in) CvPoint2D32f (CvPoint2D32f temp) 
291 {
292   if (PyTuple_Check($input)) 
293   {
294     if (!PyArg_ParseTuple($input,"ff", & temp.x, & temp.y)) 
295     {
296       PyErr_SetString(PyExc_TypeError,"tuple must consist of 2 floats (x, y)");
297       return NULL;
298     }
299     $1 = temp;
300   } 
301   else
302   {
303     CvPoint2D32f * ptr;
304     if (SWIG_ConvertPtr ($input, (void **) & ptr, SWIGTYPE_p_CvPoint2D32f, SWIG_POINTER_EXCEPTION) == -1)
305     {
306       PyErr_SetString (PyExc_TypeError,"expected a tuple or a CvPoint2D32f");
307       return NULL;
308     }
309     $1 = *ptr;
310   }
311 }
312
313
314 %typemap(in) CvPoint3D32f (CvPoint3D32f temp) 
315 {
316   if (PyTuple_Check($input)) 
317   {
318     if (!PyArg_ParseTuple($input,"fff", & temp.x, & temp.y, &temp.z)) 
319     {
320       PyErr_SetString(PyExc_TypeError,"tuple must consist of 3 floats (x, y, z)");
321       return NULL;
322     }
323     $1 = temp;
324   } 
325   else
326   {
327     CvPoint3D32f * ptr;
328     if (SWIG_ConvertPtr ($input, (void **) & ptr, SWIGTYPE_p_CvPoint3D32f, SWIG_POINTER_EXCEPTION) == -1)
329     {
330       PyErr_SetString (PyExc_TypeError,"expected a tuple or a CvPoint3D32f");
331       return NULL;
332     }
333     $1 = *ptr;
334   }
335 }
336
337
338 %typemap(in) CvPoint2D64f (CvPoint2D64f temp) 
339 {
340   if (PyTuple_Check($input)) 
341   {
342     if (!PyArg_ParseTuple($input,"dd", & temp.x, & temp.y)) 
343     {
344       PyErr_SetString(PyExc_TypeError,"tuple must consist of 2 floats (x, y)");
345       return NULL;
346     }
347     $1 = temp;
348   } 
349   else
350   {
351     CvPoint2D64f * ptr;
352     if (SWIG_ConvertPtr ($input, (void **) & ptr, SWIGTYPE_p_CvPoint2D64f, SWIG_POINTER_EXCEPTION) == -1)
353     {
354       PyErr_SetString (PyExc_TypeError,"expected a tuple or a CvPoint2D64f");
355       return NULL;
356     }
357     $1 = *ptr;
358   }
359 }
360
361
362 %typemap(in) CvPoint3D64f (CvPoint3D64f temp) 
363 {
364   if (PyTuple_Check($input)) 
365   {
366     if (!PyArg_ParseTuple($input,"ddd", & temp.x, & temp.y, &temp.z)) 
367     {
368       PyErr_SetString(PyExc_TypeError,"tuple must consist of 3 floats (x, y, z)");
369       return NULL;
370     }
371     $1 = temp;
372   } 
373   else
374   {
375     CvPoint3D64f * ptr;
376     if (SWIG_ConvertPtr ($input, (void **) & ptr, SWIGTYPE_p_CvPoint3D64f, SWIG_POINTER_EXCEPTION) == -1)
377     {
378       PyErr_SetString (PyExc_TypeError,"expected a tuple or a CvPoint3D64f");
379       return NULL;
380     }
381     $1 = *ptr;
382   }
383 }
384
385
386 %typemap(in) CvSize (CvSize temp) 
387 {
388   if (PyTuple_Check($input)) 
389   {
390     if (!PyArg_ParseTuple($input,"ii", & temp.width, & temp.height)) 
391     {
392       PyErr_SetString(PyExc_TypeError,"tuple must consist of 2 integers (width, height)");
393       return NULL;
394     }
395     $1 = temp;
396   } 
397   else
398   {
399     CvSize * ptr;
400     if (SWIG_ConvertPtr ($input, (void **) & ptr, SWIGTYPE_p_CvSize, SWIG_POINTER_EXCEPTION) == -1)
401     {
402       PyErr_SetString (PyExc_TypeError,"expected a tuple or a CvSize");
403       return NULL;
404     }
405     $1 = *ptr;
406   }
407 }
408
409
410 %typemap(in) CvSize2D32f (CvSize2D32f temp) 
411 {
412   if (PyTuple_Check($input)) 
413   {
414     if (!PyArg_ParseTuple($input,"ff", & temp.width, & temp.height)) 
415     {
416       PyErr_SetString(PyExc_TypeError,"tuple must consist of 2 floats (width, height)");
417       return NULL;
418     }
419     $1 = temp;
420   } 
421   else
422   {
423     CvSize2D32f * ptr;
424     if (SWIG_ConvertPtr ($input, (void **) & ptr, SWIGTYPE_p_CvSize2D32f, SWIG_POINTER_EXCEPTION) == -1)
425     {
426       PyErr_SetString (PyExc_TypeError,"expected a tuple or a CvSize2D32f");
427       return NULL;
428     }
429     $1 = *ptr;
430   }
431 }
432
433
434 %typemap(in) CvBox2D (CvBox2D temp) 
435 {
436   if (PyTuple_Check($input)) 
437   {
438     if (!PyArg_ParseTuple($input,"fffff",  & temp.center.x, & temp.center.y, & temp.size.width, & temp.size.height, & temp.angle)) 
439     {
440       PyErr_SetString(PyExc_TypeError,"tuple must consist of 5 floats (center_x, center_y, width, height, angle)");
441       return NULL;
442     }
443     $1 = temp;
444   } 
445   else
446   {
447     CvBox2D * ptr;
448     if (SWIG_ConvertPtr ($input, (void **) & ptr, SWIGTYPE_p_CvBox2D, SWIG_POINTER_EXCEPTION) == -1)
449     {
450       PyErr_SetString (PyExc_TypeError,"expected a tuple or a CvBox2D");
451       return NULL;
452     }
453     $1 = *ptr;
454   }
455 }
456
457
458 %typemap(in) CvSlice (CvSlice temp) 
459 {
460   if (PyTuple_Check($input)) 
461   {
462     if (!PyArg_ParseTuple($input,"ii", & temp.start_index, & temp.end_index)) 
463     {
464       PyErr_SetString(PyExc_TypeError,"tuple must consist of 2 integers (start_index, end_index)");
465       return NULL;
466     }
467     $1 = temp;
468   } 
469   else
470   {
471     CvSlice * ptr;
472     if (SWIG_ConvertPtr ($input, (void **) & ptr, SWIGTYPE_p_CvSlice, SWIG_POINTER_EXCEPTION) == -1)
473     {
474       PyErr_SetString (PyExc_TypeError,"expected a tuple or a CvSlice");
475       return NULL;
476     }
477     $1 = *ptr;
478   }
479 }
480
481
482 /* typemap for cvGetDims */
483 %typemap(in) (const CvArr * arr, int * sizes = NULL) (void * myarr, int mysizes[CV_MAX_DIM]){
484         SWIG_Python_ConvertPtr($input, &myarr, 0, SWIG_POINTER_EXCEPTION);
485         $1=(CvArr *)myarr;
486         $2=mysizes;
487 }
488
489 %typemap(argout) (const CvArr * arr, int * sizes = NULL) {
490         int len = PyInt_AsLong( $result );
491         PyObject * obj = PyTuple_FromIntArray( $2, len );
492         Py_DECREF( $result );
493         $result = obj;
494 }
495                                 
496 /* map one list of integer to the two parameters dimension/sizes */
497 %typemap(in) (int dims, int* sizes) {
498     int i;
499
500     /* get the size of the dimention array */
501     $1 = PyList_Size ($input);
502
503     /* allocate the needed memory */
504     $2 = (int *)malloc ($1 * sizeof (int));
505
506     /* extract all the integer values from the list */
507     for (i = 0; i < $1; i++) {
508         PyObject *item = PyList_GetItem ($input, i);
509         $2 [i] = (int)PyInt_AsLong (item);
510     }
511 }
512
513 /* map one list of integer to the parameter idx of
514    cvGetND, cvSetND, cvClearND, cvGetRealND, cvSetRealND and cvClearRealND */
515 %typemap(in) (int* idx) {
516     int i;
517     int size;
518
519     /* get the size of the dimention array */
520     size = PyList_Size ($input);
521
522     /* allocate the needed memory */
523     $1 = (int *)malloc (size * sizeof (int));
524
525     /* extract all the integer values from the list */
526     for (i = 0; i < size; i++) {
527         PyObject *item = PyList_GetItem ($input, i);
528         $1 [i] = (int)PyInt_AsLong (item);
529     }
530 }
531
532 /* map a list of list of float to an matrix of floats*/
533 %typemap(in) float** ranges {
534     int i1;
535     int i2;
536     int size1;
537     int size2 = 0;
538
539     /* get the number of lines of the matrix */
540     size1 = PyList_Size ($input);
541
542     /* allocate the correct number of lines for the destination matrix */
543     $1 = (float **)malloc (size1 * sizeof (float *));
544
545     for (i1 = 0; i1 < size1; i1++) {
546
547         /* extract all the lines of the matrix */
548         PyObject *list = PyList_GetItem ($input, i1);
549
550         if (size2 == 0) {
551             /* size 2 wasn't computed before */
552             size2 = PyList_Size (list);
553         } else if (size2 != PyList_Size (list)) {
554             /* the current line as a different size than the previous one */
555             /* so, generate an exception */
556             SWIG_exception (SWIG_ValueError, "Lines must be the same size");
557         }
558
559         /* allocate the correct number of rows for the current line */
560         $1 [i1] = (float *)malloc (size2 * sizeof (float));
561
562         /* extract all the float values of this row */
563         for (i2 = 0; i2 < size2; i2++) {
564             PyObject *item = PyList_GetItem (list, i2);
565             $1 [i1][i2] = (float)PyFloat_AsDouble (item);
566         }
567     }
568 }
569
570 /**
571  * map the output parameter of the cvGetMinMaxHistValue()
572  * so, we can call cvGetMinMaxHistValue() in Python like:
573  * min_value, max_value = cvGetMinMaxHistValue (hist, None, None)
574  */
575 %apply int *OUTPUT {int *min_idx};
576 %apply int *OUTPUT {int *max_idx}; 
577 %apply float *OUTPUT {float *min_value};
578 %apply float *OUTPUT {float *max_value};
579
580 /**
581  * map output parameters of cvMinMaxLoc
582  */
583 %apply double *OUTPUT {double* min_val};
584 %apply double *OUTPUT {double* max_val};
585
586 %typemap(in, numinputs=0) CvPoint * OUTPUT {
587     $1 = (CvPoint *) malloc(sizeof(CvPoint));
588 }
589
590 %typemap(argout) CvPoint * OUTPUT { 
591         PyObject * to_add = SWIG_NewPointerObj ($1, $descriptor(CvPoint *), SWIG_POINTER_OWN);
592         $result = SWIG_AppendOutput( $result, to_add );
593 }
594
595 %apply CvPoint *OUTPUT {CvPoint *min_loc};
596 %apply CvPoint *OUTPUT {CvPoint *max_loc}; 
597
598 /**
599  * the input argument of cvPolyLine "CvPoint** pts" is converted from 
600  * a "list of list" (aka. an array) of CvPoint().
601  * The next parameters "int* npts" and "int contours" are computed from
602  * the givne list.
603  */
604 %typemap(in) (CvPoint** pts, int* npts, int contours){
605     int i;
606     int j;
607     int size2 = -1;
608     CvPoint **points = NULL;
609     int *nb_vertex = NULL;
610
611         if(!PySequence_Check($input)){
612                 SWIG_exception(SWIG_TypeError, "Expected a list for argument $argnum\n");
613                 return NULL;
614         }
615
616     /* get the number of polylines input array */
617     int size1 = PySequence_Size ($input);
618     $3 = size1;
619
620         if(size1>0){
621             /* create the points array */
622             points = (CvPoint **)malloc (size1 * sizeof (CvPoint *));
623
624             /* point to the created array for passing info to the C function */
625             $1 = points;
626
627             /* create the array for memorizing the vertex */
628             nb_vertex = (int *)malloc (size1 * sizeof (int));
629             $2 = nb_vertex;
630         }
631     for (i = 0; i < size1; i++) {
632
633                 /* get the current item */
634                 PyObject *line = PySequence_GetItem ($input, i);
635
636                 if(!PySequence_Check(line)){
637                         SWIG_exception(SWIG_TypeError, "Expected a sequence of sequences of integers for argument $argnum\n");
638                         // TODO: cleanup here
639                 }
640
641                 /* get the size of the current polyline */
642                 size2 = PySequence_Size (line);
643
644
645                 if(size2>0){
646                         /* allocate the necessary memory to store the points */
647                         points [i] = (CvPoint *)malloc (size2 * sizeof (CvPoint));
648                 }
649
650                 /* memorize the size of the polyline in the vertex list */
651                 nb_vertex [i] = size2;
652
653                 for (j = 0; j < size2; j++) {
654                     /* get the current item */
655                     PyObject *item = PySequence_GetItem (line, j);
656                         points[i][j] = PyObject_to_CvPoint( item );
657         }
658         }
659 }
660 /** Free arguments allocated before the function call */
661 %typemap(freearg) (CvPoint **pts, int* npts, int contours){
662         int i;
663         for(i=0;i<$3;i++){
664                 free($1[i]);
665         }
666         free($1);
667         free($2);
668 }
669
670
671 /** this typemap is meant to help cvCalcOpticalFlowPyrLK */
672 %typemap(in, numinputs = 0) (int count, char* status, float* track_error) {
673         $1 [count] = (char *)  malloc (count * sizeof (char));
674         $2 [count] = (float *) malloc (count * sizeof (float));
675 }
676
677 %typemap(argout) float *track_error { 
678         PyObject * to_add = SWIG_NewPointerObj ($1, $descriptor(float *), SWIG_POINTER_OWN);
679         $result = SWIG_AppendOutput( $result, to_add );
680 }
681
682 /** Macro to define typemaps to convert a python list of CvPoints to a C array of CvPoints */
683 %define %typemap_CvPoint_CArr(points_arg, numpoints_arg)
684
685 %typemap(in, numinputs=1) (CvPoint * points_arg, int numpoints_arg){
686         int i;
687
688         if(!PySequence_Check($input)){
689                 SWIG_exception(SWIG_TypeError, "Expected a list for argument $argnum\n");
690                 return NULL;
691         }
692         int size = PySequence_Size($input);
693         CvPoint * points = (CvPoint *)malloc(size*sizeof(CvPoint));
694         for(i=0; i<size; i++){
695                 PyObject *item = PySequence_GetItem($input, i);
696                 points[i] = PyObject_to_CvPoint( item );
697         }
698         $1 = points;
699         $2 = size;
700 }
701 %typemap(freearg) (CvPoint *points_arg, int numpoints_arg){
702         free((char *)$1);
703 }
704 %enddef
705
706 /* apply to cvFillConvexPoly */
707 %typemap_CvPoint_CArr(pts, npts)
708
709 /**
710  * this is mainly an "output parameter"
711  * So, just allocate the memory as input
712  */
713 %typemap (in, numinputs=0) (CvSeq ** OUTPUT) (CvSeq * seq) {
714     $1 = &seq;
715 }
716
717 /**
718  * return the contours with all the others parametres
719  */
720 %typemap(argout) (CvSeq ** OUTPUT) {
721     PyObject *to_add;
722
723     /* extract the pointer we want to add to the returned tuple */
724         /* sequence is allocated in CvMemStorage, so python_ownership=0 */
725     to_add = SWIG_NewPointerObj (*$1, $descriptor(CvSeq*), 0); 
726
727         $result = SWIG_AppendResult($result, &to_add, 1);
728 }
729 %apply CvSeq **OUTPUT {CvSeq **first_contour};
730 %apply CvSeq **OUTPUT {CvSeq **comp};
731
732 /**
733  * CvArr ** image can be either one CvArr or one array of CvArr
734  * (for example like in cvCalcHist() )
735  * From Python, the array of CvArr can be a tuple.
736  */
737 %typemap(in) (CvArr ** INPUT) (
738     CvArr * one_image=NULL, 
739     bool free_one_arg=false, 
740     CvArr ** many_images=NULL, 
741     bool *free_many_args=NULL, 
742     int nimages=0 ) {
743
744     /* first, check if this is a tuple */
745     if PyTuple_Check ($input) {
746         /* This is a tuple, so we need to test each element and pass
747             them to the called function */
748
749         int i;
750
751         /* get the size of the tuple */
752         nimages = PyTuple_Size ($input);
753
754         /* allocate the necessary place */
755         many_images = (CvArr **)malloc (nimages * sizeof (CvArr *));
756         free_many_args = (bool *)malloc(nimages * sizeof(bool));
757
758         for (i = 0; i < nimages; i++) {
759
760             /* convert the current tuple element to a CvArr *, and
761                store to many_images [i] */
762             many_images[i] = PyObject_to_CvArr (PyTuple_GetItem ($input, i),
763                                                 free_many_args+i);
764
765             /* check that the current item is a correct type */
766             if(!many_images[i]) {
767                 /* incorrect ! */
768                 SWIG_fail;
769             }
770         }
771
772         /* what to give to the called function */
773         $1 = many_images;
774
775     } else if((one_image = PyObject_to_CvArr( $input, &free_one_arg ))){
776
777         /* this is just one CvArr *, so one_image will receive it */
778         $1 = &one_image;
779
780     } else {
781         /* not a CvArr *, not a tuple, this is wrong */
782         SWIG_fail;
783     }
784 }
785 %apply CvArr ** INPUT {CvArr ** img};
786 %apply CvArr ** INPUT {CvArr ** image};
787 %apply CvArr ** INPUT {CvArr ** arr};
788 %apply CvArr ** INPUT {CvArr ** vects};
789
790 %typemap(freearg) (CvArr ** FREEARG) {
791         if(free_one_arg$argnum){
792                 cvFree(&(one_image$argnum));
793         }
794         else if(free_many_args$argnum){
795                 int i;
796                 for (i=0; i<nimages$argnum; i++){
797                         if(free_many_args$argnum[i]){
798                                 cvReleaseData(many_images$argnum[i]);
799                                 cvFree(many_images$argnum+i);
800                         }
801                 }
802                 free(many_images$argnum);
803                 free(free_many_args$argnum);
804         }
805
806 }
807 %apply CvArr ** FREEARG {CvArr ** img};
808 %apply CvArr ** FREEARG {CvArr ** image};
809 %apply CvArr ** FREEARG {CvArr ** arr};
810 %apply CvArr ** FREEARG {CvArr ** vects};
811
812 /**
813  * Map the CvFont * parameter from the cvInitFont() as an output parameter
814  */
815 %typemap (in, numinputs=1) (CvFont* font, int font_face) {
816     $1 = (CvFont *)malloc (sizeof (CvFont));
817     $2 = (int)PyInt_AsLong ($input); 
818     if (SWIG_arg_fail($argnum)) SWIG_fail;
819 }
820 %typemap(argout) (CvFont* font, int font_face) {
821     PyObject *to_add;
822
823     /* extract the pointer we want to add to the returned tuple */
824     to_add = SWIG_NewPointerObj ($1, $descriptor(CvFont *), 0);
825
826         $result = SWIG_AppendResult($result, &to_add, 1);
827 }
828
829 /**
830  * these are output parameters for cvGetTextSize
831  */
832 %typemap (in, numinputs=0) (CvSize* text_size, int* baseline) {
833     CvSize *size = (CvSize *)malloc (sizeof (CvSize));
834     int *baseline = (int *)malloc (sizeof (int));
835     $1 = size;
836     $2 = baseline;
837 }
838
839 /**
840  * return the finded parameters for cvGetTextSize
841  */
842 %typemap(argout) (CvSize* text_size, int* baseline) {
843     PyObject * to_add[2];
844
845     /* extract the pointers we want to add to the returned tuple */
846     to_add [0] = SWIG_NewPointerObj ($1, $descriptor(CvSize *), 0);
847     to_add [1] = PyInt_FromLong (*$2);
848
849     $result = SWIG_AppendResult($result, to_add, 2);
850 }
851
852
853 /**
854  * curr_features is output parameter for cvCalcOpticalFlowPyrLK
855  */
856 %typemap (in, numinputs=1) (CvPoint2D32f* curr_features, int count)
857      (int tmpCount) {
858     /* as input, we only need the size of the wanted features */
859
860     /* memorize the size of the wanted features */
861     tmpCount = (int)PyInt_AsLong ($input);
862
863     /* create the array for the C call */
864     $1 = (CvPoint2D32f *) malloc(tmpCount * sizeof (CvPoint2D32f));
865
866     /* the size of the array for the C call */
867     $2 = tmpCount;
868 }
869
870 /**
871  * the features returned by cvCalcOpticalFlowPyrLK
872  */
873 %typemap(argout) (CvPoint2D32f* curr_features, int count) {
874     int i;
875     PyObject *to_add;
876     
877     /* create the list to return */
878     to_add = PyList_New (tmpCount$argnum);
879
880     /* extract all the points values of the result, and add it to the
881        final resulting list */
882     for (i = 0; i < tmpCount$argnum; i++) {
883         PyList_SetItem (to_add, i,
884                         SWIG_NewPointerObj (&($1 [i]),
885                                             $descriptor(CvPoint2D32f *), 0));
886     }
887
888         $result = SWIG_AppendResult($result, &to_add, 1);
889 }
890
891 /**
892  * status is an output parameters for cvCalcOpticalFlowPyrLK
893  */
894 %typemap (in, numinputs=1) (char *status) (int tmpCountStatus){
895     /* as input, we still need the size of the status array */
896
897     /* memorize the size of the status array */
898     tmpCountStatus = (int)PyInt_AsLong ($input);
899
900     /* create the status array for the C call */
901     $1 = (char *)malloc (tmpCountStatus * sizeof (char));
902 }
903
904 /**
905  * the status returned by cvCalcOpticalFlowPyrLK
906  */
907 %typemap(argout) (char *status) {
908     int i;
909     PyObject *to_add;
910
911     /* create the list to return */
912     to_add = PyList_New (tmpCountStatus$argnum);
913
914     /* extract all the integer values of the result, and add it to the
915        final resulting list */
916     for (i = 0; i < tmpCountStatus$argnum; i++) {
917                 PyList_SetItem (to_add, i, PyBool_FromLong ($1 [i]));
918     }
919
920         $result = SWIG_AppendResult($result, &to_add, 1); 
921 }
922
923 /* map one list of points to the two parameters dimenssion/sizes
924  for cvCalcOpticalFlowPyrLK */
925 %typemap(in) (CvPoint2D32f* prev_features) 
926 {
927   int i;
928   int size;
929   
930   /* get the size of the input array */
931   size = PyList_Size ($input);
932   
933   /* allocate the needed memory */
934   CvPoint2D32f * features = (CvPoint2D32f *) malloc (size * sizeof (CvPoint2D32f));
935   
936   /* extract all the points values from the list */
937   for (i = 0; i < size; i++)
938   {
939     PyObject *item = PyList_GetItem ($input, i);
940     
941     void * vptr;
942     SWIG_Python_ConvertPtr (item, &vptr,
943                             $descriptor(CvPoint2D32f*),
944                             SWIG_POINTER_EXCEPTION);
945     CvPoint2D32f *p = (CvPoint2D32f *)vptr;
946     features[i].x = p->x;
947     features[i].y = p->y;
948   }
949   
950   // these are the arguments passed to the OpenCV function
951   $1 = features;
952 }
953
954 /**
955  * the corners returned by cvGoodFeaturesToTrack
956  */
957 %typemap (in, numinputs=1) (CvPoint2D32f* corners, int* corner_count) (int tmpCount) 
958 {
959   /* as input, we still need the size of the corners array */
960   
961   /* memorize the size of the status corners */
962   tmpCount = (int) PyInt_AsLong ($input);
963   
964   // these are the arguments passed to the OpenCV function
965   $1 = (CvPoint2D32f *) malloc (tmpCount * sizeof (CvPoint2D32f));
966   $2 = &tmpCount;
967 }
968
969 /**
970  * the corners returned by cvGoodFeaturesToTrack
971  */
972 %typemap(argout) (CvPoint2D32f* corners, int* corner_count) 
973 {
974   int i;
975   PyObject *to_add;
976   
977   /* create the list to return */
978   to_add = PyList_New (tmpCount$argnum);
979   
980   /* extract all the integer values of the result, and add it to the final resulting list */
981   for (i = 0; i < tmpCount$argnum; i++)
982     PyList_SetItem (to_add, i, SWIG_NewPointerObj (&($1 [i]), $descriptor(CvPoint2D32f *), 0));
983   
984   $result = SWIG_AppendResult($result, &to_add, 1);
985 }
986
987 /* map one list of points to the two parameters dimension/sizes for cvFindCornerSubPix */
988 %typemap(in, numinputs=1) (CvPoint2D32f* corners, int count) (int cornersCount, CvPoint2D32f* corners)
989 {
990         if(! PySequence_Check ($input))
991   {
992                 PyErr_SetString(PyExc_TypeError, "Expected a list or tuple");
993                 return NULL;
994         }
995   
996   // TODO: will this ever be freed?
997   cornersCount = PySequence_Size ($input);
998   corners = (CvPoint2D32f *) malloc (cornersCount * sizeof (CvPoint2D32f));
999   
1000   // extract all the points values from the list */
1001   CvPoint2D32f * corner = corners;
1002   for (int i = 0; i < cornersCount; i++, corner++) 
1003   {
1004     PyObject * item = PySequence_GetItem ($input, i);
1005         
1006     if (PySequence_Check(item)  &&  PySequence_Length(item) == 2) 
1007     {
1008       PyObject * tuple = PySequence_Tuple (item);
1009       if (!PyArg_ParseTuple (tuple, "ff", & corner->x, & corner->y)) 
1010       {
1011         PyErr_SetString(PyExc_TypeError,"each entry must consist of 2 floats (x, y)");
1012         Py_DECREF (tuple);
1013         Py_DECREF (item);
1014         return NULL;
1015       }
1016       Py_DECREF (tuple);
1017     } 
1018     else
1019     {
1020       CvPoint2D32f * ptr;
1021       if (SWIG_ConvertPtr (item, (void **) & ptr, $descriptor(CvPoint2D32f *), SWIG_POINTER_EXCEPTION) == -1)
1022       {
1023         PyErr_SetString (PyExc_TypeError,"expected a sequence of 2 floats (x, y) or a CvPoint2D32f");
1024         Py_DECREF (item);
1025         return NULL;
1026       }
1027       *corner = *ptr;
1028     }
1029     
1030     Py_DECREF (item);
1031   }
1032   
1033   // these are the arguments passed to the OpenCV function
1034   $1 = corners;
1035   $2 = cornersCount;
1036 }
1037
1038 /**
1039  * the corners returned by cvFindCornerSubPix
1040  */
1041 %typemap(argout) (CvPoint2D32f* corners, int count) 
1042 {
1043   int i;
1044   PyObject *to_add;
1045   
1046   /* create the list to return */
1047   to_add = PyList_New (cornersCount$argnum);
1048   
1049   /* extract all the corner values of the result, and add it to the
1050    final resulting list */
1051   for (i = 0; i < cornersCount$argnum; i++)
1052     PyList_SetItem (to_add, i, SWIG_NewPointerObj (&(corners$argnum [i]), $descriptor(CvPoint2D32f *), 0));
1053   
1054         $result = SWIG_AppendResult( $result, &to_add, 1);
1055 }
1056
1057 /**
1058  * return the corners for cvFindChessboardCorners
1059  */
1060 %typemap(in, numinputs=1) (CvSize pattern_size, CvPoint2D32f * corners, int * corner_count ) 
1061      (CvSize * pattern_size, CvPoint2D32f * tmp_corners, int tmp_ncorners) {
1062          void * vptr;
1063         if( SWIG_ConvertPtr($input, &vptr, $descriptor( CvSize * ), SWIG_POINTER_EXCEPTION ) == -1){
1064                 return NULL;
1065         }
1066         pattern_size=(CvSize *)vptr;
1067         tmp_ncorners = pattern_size->width*pattern_size->height;
1068
1069         tmp_corners = (CvPoint2D32f *) malloc(sizeof(CvPoint2D32f)*tmp_ncorners);
1070         $1 = *pattern_size;
1071         $2 = tmp_corners;
1072         $3 = &tmp_ncorners;
1073 }
1074
1075 %typemap(argout) (CvSize pattern_size, CvPoint2D32f * corners, int * corner_count){
1076     int i;
1077     PyObject *to_add;
1078
1079     /* create the list to return */
1080     to_add = PyList_New ( tmp_ncorners$argnum );
1081
1082     /* extract all the corner values of the result, and add it to the
1083        final resulting list */
1084     for (i = 0; i < tmp_ncorners$argnum; i++) {
1085                 CvPoint2D32f * pt = new CvPoint2D32f;
1086                 pt->x = tmp_corners$argnum[i].x;
1087                 pt->y = tmp_corners$argnum[i].y;
1088                 
1089         PyList_SetItem (to_add, i,
1090             SWIG_NewPointerObj( pt, $descriptor(CvPoint2D32f *), 0));
1091     }
1092
1093         $result = SWIG_AppendResult( $result, &to_add, 1);
1094     free(tmp_corners$argnum);
1095 }
1096
1097 /**
1098  * return the matrices for cvCameraCalibrate
1099  */
1100 %typemap(in, numinputs=0) (CvMat * intrinsic_matrix, CvMat * distortion_coeffs)
1101 {
1102         $1 = cvCreateMat(3,3,CV_32F);
1103         $2 = cvCreateMat(4,1,CV_32F);
1104 }
1105
1106 %typemap(argout) (CvMat * intrinsic_matrix, CvMat * distortion_coeffs)
1107 {
1108         PyObject * to_add[2] = {NULL, NULL};
1109         to_add[0] = SWIG_NewPointerObj($1, $descriptor(CvMat *), 1);
1110         to_add[1] = SWIG_NewPointerObj($2, $descriptor(CvMat *), 1);
1111         $result = SWIG_AppendResult( $result, to_add, 2 );
1112 }
1113
1114 /**
1115  * Fix OpenCV inheritance for CvSeq, CvSet, CvGraph
1116  * Otherwise, can't call CvSeq functions on CvSet or CvGraph
1117 */
1118 %typemap(in, numinputs=1) (CvSeq *) (void * ptr)
1119 {
1120         
1121         if( SWIG_ConvertPtr($input, &ptr, $descriptor(CvSeq *), 0) == -1 &&
1122             SWIG_ConvertPtr($input, &ptr, $descriptor(CvSet *), 0) == -1 &&
1123             SWIG_ConvertPtr($input, &ptr, $descriptor(CvGraph *), 0) == -1 &&
1124             SWIG_ConvertPtr($input, &ptr, $descriptor(CvSubdiv2D *), 0) == -1 &&
1125             SWIG_ConvertPtr($input, &ptr, $descriptor(CvChain *), 0) == -1 &&
1126             SWIG_ConvertPtr($input, &ptr, $descriptor(CvContour *), 0) == -1 &&
1127             SWIG_ConvertPtr($input, &ptr, $descriptor(CvContourTree *), 0) == -1 )
1128         {
1129                 SWIG_exception (SWIG_TypeError, "could not convert to CvSeq");
1130                 return NULL;
1131         }
1132         $1 = (CvSeq *) ptr;
1133 }
1134
1135 %typemap(in, numinputs=1) (CvSet *) (void * ptr)
1136 {
1137         if( SWIG_ConvertPtr($input, &ptr, $descriptor(CvSet *), 0) == -1 &&
1138             SWIG_ConvertPtr($input, &ptr, $descriptor(CvGraph *), 0) == -1 &&
1139             SWIG_ConvertPtr($input, &ptr, $descriptor(CvSubdiv2D *), 0) == -1) 
1140         {
1141                 SWIG_exception (SWIG_TypeError, "could not convert to CvSet");
1142                 return NULL;
1143         }
1144         $1 = (CvSet *)ptr;
1145 }
1146
1147 %typemap(in, numinputs=1) (CvGraph *) (void * ptr)
1148 {
1149         if( SWIG_ConvertPtr($input, &ptr, $descriptor(CvGraph *), 0) == -1 &&
1150             SWIG_ConvertPtr($input, &ptr, $descriptor(CvSubdiv2D *), 0) == -1) 
1151         {
1152                 SWIG_exception (SWIG_TypeError, "could not convert to CvGraph");
1153                 return NULL;
1154         }
1155         $1 = (CvGraph *)ptr;
1156 }
1157
1158 /**
1159  * Remap output arguments to multiple return values for cvMinEnclosingCircle
1160  */
1161 %typemap(in, numinputs=0) (CvPoint2D32f * center, float * radius) (CvPoint2D32f * tmp_center, float tmp_radius) 
1162 {
1163         tmp_center = (CvPoint2D32f *) malloc(sizeof(CvPoint2D32f));
1164         $1 = tmp_center;
1165         $2 = &tmp_radius;
1166 }
1167 %typemap(argout) (CvPoint2D32f * center, float * radius)
1168 {
1169     PyObject * to_add[2] = {NULL, NULL};
1170         to_add[0] = SWIG_NewPointerObj( tmp_center$argnum, $descriptor(CvPoint2D32f *), 1); 
1171         to_add[1] = PyFloat_FromDouble( tmp_radius$argnum );
1172
1173     $result = SWIG_AppendResult($result, to_add, 2);
1174 }
1175
1176 /** BoxPoints */
1177 %typemap(in, numinputs=0) (CvPoint2D32f pt[4]) (CvPoint2D32f tmp_pts[4])
1178 {
1179         $1 = tmp_pts;
1180 }
1181 %typemap(argout) (CvPoint2D32f pt[4])
1182 {
1183         PyObject * to_add = PyList_New(4);
1184         int i;
1185         for(i=0; i<4; i++){
1186                 CvPoint2D32f * p = new CvPoint2D32f;
1187                 *p = tmp_pts$argnum[i];
1188                 PyList_SetItem(to_add, i, SWIG_NewPointerObj( p, $descriptor(CvPoint2D32f *), 1 ) );
1189         }
1190         $result = SWIG_AppendResult($result, &to_add, 1);
1191 }
1192
1193 /** Macro to wrap a built-in type that is used as an object like CvRNG and CvSubdiv2DEdge */
1194 %define %wrap_builtin(type)
1195 %inline %{
1196 // Wrapper class
1197 class type##_Wrapper {
1198 private:
1199         type m_val;
1200 public:
1201         type##_Wrapper( const type & val ) :
1202                 m_val(val)
1203         {
1204         }
1205         type * ptr() { return &m_val; }
1206         type & ref() { return m_val; }
1207         bool operator==(const type##_Wrapper & x){
1208                 return m_val==x.m_val;
1209         }
1210         bool operator!=(const type##_Wrapper & x){
1211                 return m_val!=x.m_val;
1212         }
1213 };
1214 %}
1215 %typemap(out) type
1216 {
1217         type##_Wrapper * wrapper = new type##_Wrapper( $1 );
1218         $result = SWIG_NewPointerObj( wrapper, $descriptor( type##_Wrapper * ), 1 );
1219 }
1220 %typemap(out) type *
1221 {
1222         type##_Wrapper * wrapper = new type##_Wrapper( *($1) );
1223         $result = SWIG_NewPointerObj( wrapper, $descriptor( type##_Wrapper * ), 1 );
1224 }
1225
1226 %typemap(in) (type *) (void * vptr, type##_Wrapper * wrapper){
1227         if(SWIG_ConvertPtr($input, &vptr, $descriptor(type##_Wrapper *), 0)==-1){
1228                 SWIG_exception( SWIG_TypeError, "could not convert Python object to C value");
1229                 return NULL;
1230         }
1231         wrapper = (type##_Wrapper *) vptr;
1232         $1 = wrapper->ptr();
1233 }
1234 %typemap(in) (type) (void * vptr, type##_Wrapper * wrapper){
1235         if(SWIG_ConvertPtr($input, &vptr, $descriptor(type##_Wrapper *), 0)==-1){
1236                 SWIG_exception( SWIG_TypeError, "could not convert Python object to C value");
1237                 return NULL;
1238         }
1239         wrapper = (type##_Wrapper *) vptr;
1240         $1 = wrapper->ref();
1241 }
1242 %enddef 
1243
1244 /** Application of wrapper class to built-in types */
1245 %wrap_builtin(CvRNG);
1246 %wrap_builtin(CvSubdiv2DEdge);
1247
1248 /**
1249  * Allow CvQuadEdge2D to be interpreted as CvSubdiv2DEdge
1250  */
1251 %typemap(in, numinputs=1) (CvSubdiv2DEdge) (CvSubdiv2DEdge_Wrapper * wrapper, CvQuadEdge2D * qedge, void *vptr)
1252 {
1253         if( SWIG_ConvertPtr($input, &vptr, $descriptor(CvSubdiv2DEdge_Wrapper *), 0) != -1 ){
1254                 wrapper = (CvSubdiv2DEdge_Wrapper *) vptr;
1255                 $1 = wrapper->ref();
1256         }
1257         else if( SWIG_ConvertPtr($input, &vptr, $descriptor(CvQuadEdge2D *), 0) != -1 ){
1258                 qedge = (CvQuadEdge2D *) vptr;
1259                 $1 = (CvSubdiv2DEdge)qedge;
1260         }
1261         else{
1262                  SWIG_exception( SWIG_TypeError, "could not convert to CvSubdiv2DEdge");
1263                  return NULL;
1264         }
1265 }
1266
1267 /**
1268  * return the vertex and edge for cvSubdiv2DLocate
1269  */
1270 %typemap(in, numinputs=0) (CvSubdiv2DEdge * edge, CvSubdiv2DPoint ** vertex) 
1271         (CvSubdiv2DEdge tmpEdge, CvSubdiv2DPoint * tmpVertex)
1272 {
1273         $1 = &tmpEdge;
1274         $2 = &tmpVertex;
1275 }
1276 %typemap(argout) (CvSubdiv2DEdge * edge, CvSubdiv2DPoint ** vertex)
1277 {
1278         PyObject * to_add[2] = {NULL, NULL};
1279         if(result==CV_PTLOC_INSIDE || result==CV_PTLOC_ON_EDGE){
1280                 CvSubdiv2DEdge_Wrapper * wrapper = new CvSubdiv2DEdge_Wrapper( tmpEdge$argnum );
1281                 to_add[0] = SWIG_NewPointerObj( wrapper, $descriptor(CvSubdiv2DEdge_Wrapper *), 0);
1282                 to_add[1] = Py_None;
1283         }
1284         if(result==CV_PTLOC_VERTEX){
1285                 to_add[0] = Py_None;
1286                 to_add[1] = SWIG_NewPointerObj( tmpVertex$argnum, $descriptor(CvSubdiv2DPoint *), 0);
1287         }
1288         
1289         $result = SWIG_AppendResult($result, to_add, 2);
1290 }
1291
1292 /**
1293  * int *value  in cvCreateTrackbar() is only used for input in the Python wrapper.
1294  * for output, use the pos in the callback
1295  * TODO: remove the memory leak introducted by the malloc () (if needed).
1296  */
1297 %typemap(in, numinputs=1) (int *value)
1298 {
1299     $1 = (int *)malloc (sizeof (int));
1300     *$1 = PyInt_AsLong ($input);
1301 }
1302
1303
1304 /**
1305  * take (query_points,k) and return (indices,dist)
1306  * for cvLSHQuery, cvFindFeatures
1307  */
1308 %typemap(in, noblock=1) (const CvMat* query_points) (bool freearg=false, int num_query_points)
1309 {
1310   $1 = (CvMat*)PyObject_to_CvArr($input, &freearg);
1311   num_query_points = $1->rows;
1312 }
1313 %typemap(freearg) (const CvMat* query_points) {
1314   if($1!=NULL && freearg$argnum){
1315     cvReleaseData( $1 );
1316     cvFree(&($1));
1317   }
1318 }
1319 %typemap(in) (CvMat* indices, CvMat* dist, int k)
1320 {
1321   $3 = (int)PyInt_AsLong($input);
1322   $1 = cvCreateMat(num_query_points2, $3, CV_32SC1);
1323   $2 = cvCreateMat(num_query_points2, $3, CV_64FC1);
1324 }
1325 %typemap(argout) (CvMat* indices, CvMat* dist, int k)
1326 {
1327   $result = SWIG_AppendOutput( $result, SWIG_NewPointerObj($1, $descriptor(CvMat *), 1) );
1328   $result = SWIG_AppendOutput( $result, SWIG_NewPointerObj($2, $descriptor(CvMat *), 1) );
1329 }
1330
1331 /**
1332  * take (data) and return (indices)
1333  * for cvLSHAdd
1334  */
1335 %typemap(in) (const CvMat* data, CvMat* indices) (bool freearg=false)
1336 {
1337   $1 = (CvMat*)PyObject_to_CvArr($input, &freearg);
1338   CvMat* m = (CvMat*)$1;
1339   $2 = cvCreateMat(m->rows, 1, CV_32SC1 );
1340 }
1341 %typemap(argout) (const CvMat* data, CvMat* indices)
1342 {
1343   $result = SWIG_AppendOutput( $result, SWIG_NewPointerObj($2, $descriptor(CvMat *), 1) );
1344 }
1345 %typemap(freearg) (const CvMat* data, CvMat* indices) {
1346   if($1!=NULL && freearg$argnum){
1347     cvReleaseData( $1 );
1348     cvFree(&($1));
1349   }
1350 }
1351
1352 /**
1353  * take (max_out_indices) and return (indices)
1354  * for cvFindFeaturesBoxed
1355  */
1356 %typemap(in) (CvMat* out_indices) (bool freearg=false)
1357 {
1358   int max_out_indices = (int)PyInt_AsLong($input);
1359   $1 = cvCreateMat(max_out_indices, 1, CV_32SC1 );
1360 }
1361 %typemap(argout) (CvMat* out_indices)
1362 {
1363   $result = SWIG_AppendOutput( $result, SWIG_NewPointerObj($1, $descriptor(CvMat *), 1) );
1364 }
1365
1366 /**
1367  * suppress (CvSeq** keypoints, CvSeq** descriptors, CvMemStorage* storage) and return (keypoints, descriptors) 
1368  * for cvExtractSURF
1369  */
1370 %typemap(in,numinputs=0) (CvSeq** keypoints, CvSeq** descriptors, CvMemStorage* storage)
1371      (CvSeq* keypoints = 0, CvSeq* descriptors = 0,CvMemStorage* storage)
1372 {
1373   storage = cvCreateMemStorage();
1374   $1 = &keypoints;
1375   $2 = &descriptors;
1376   $3 = storage;
1377 }
1378 %typemap(argout) (CvSeq** keypoints, CvSeq** descriptors, CvMemStorage* storage)
1379 {
1380   const int n1 = 6;
1381   int n2 = (*$2)->elem_size / sizeof(float);
1382   assert((*$2)->elem_size == 64 * sizeof(float) ||
1383          (*$2)->elem_size == 128 * sizeof(float));
1384   assert((*$2)->total == (*$1)->total);
1385   CvMat* m1 = cvCreateMat((*$2)->total,n1,CV_32FC1);
1386   CvMat* m2 = cvCreateMat((*$2)->total,n2,CV_32FC1);
1387   CvSeqReader r1;
1388   cvStartReadSeq(*$1, &r1);
1389   float* m1p = m1->data.fl;
1390   for (int j=0;j<(*$2)->total;++j) {
1391     CvSURFPoint* sp = (CvSURFPoint*)r1.ptr;
1392     m1p[0] = sp->pt.x;
1393     m1p[1] = sp->pt.y;
1394     m1p[2] = sp->laplacian;
1395     m1p[3] = sp->size;
1396     m1p[4] = sp->dir;
1397     m1p[5] = sp->hessian;
1398     m1p += n1;
1399     CV_NEXT_SEQ_ELEM((*$1)->elem_size, r1);
1400   }
1401   CvSeqReader r2;
1402   cvStartReadSeq(*$2, &r2);
1403   float* m2p = m2->data.fl;
1404   for (int j=0;j<(*$2)->total;++j) {
1405     memcpy(m2p,r2.ptr,n2*sizeof(float));
1406     m2p += n2;
1407     CV_NEXT_SEQ_ELEM((*$2)->elem_size, r2);
1408   }
1409   $result = SWIG_AppendOutput( $result, SWIG_NewPointerObj(m1, $descriptor(CvMat *), 1) );
1410   $result = SWIG_AppendOutput( $result, SWIG_NewPointerObj(m2, $descriptor(CvMat *), 1) );
1411   cvReleaseMemStorage(&$3);
1412 }
1413
1414 /**
1415  * suppress (CvMat* homography) and return (homography)
1416  * for cvFindHomography
1417  */
1418 %typemap(in,numinputs=0) (CvMat* homography) (bool freearg=false)
1419 {
1420   $1 = cvCreateMat(3,3,CV_64FC1);
1421 }
1422 %typemap(argout) (CvMat* homography)
1423 {
1424   $result = SWIG_AppendOutput( $result, SWIG_NewPointerObj($1, $descriptor(CvMat *), 1) );
1425 }
1426
1427 /**
1428  * take (coeffs) for (const CvMat* coeffs, CvMat *roots) and return (roots)
1429  * for cvSolveCubic
1430  */
1431 %typemap(in) (const CvMat* coeffs, CvMat *roots) (bool freearg=false)
1432 {
1433   $1 = (CvMat*)PyObject_to_CvArr($input, &freearg);
1434   int m = $1->rows * $1->cols;
1435   if (m<2) {
1436     PyErr_SetString (PyExc_TypeError,"must give at least 2 coefficients");
1437     return NULL;
1438   }
1439   $2 = cvCreateMat(m-1, 1, CV_MAKETYPE(CV_MAT_DEPTH($1->type),1));
1440 }
1441 %typemap(argout) (const CvMat* coeffs, CvMat *roots)
1442 {
1443   $result = SWIG_AppendOutput( $result, SWIG_NewPointerObj($2, $descriptor(CvMat *), 1) );
1444 }
1445 %typemap(freearg) (const CvMat* coeffs, CvMat *roots)
1446 {
1447   if($1!=NULL && freearg$argnum){
1448     cvReleaseData( $1 );
1449     cvFree(&($1));
1450   }
1451 }
1452
1453 /**
1454  * take (coeffs) for (const CvMat* coeffs, CvMat *roots2) and return (roots2)
1455  * for cvSolvePoly
1456  */
1457 %typemap(in) (const CvMat* coeffs, CvMat *roots2) (bool freearg=false)
1458 {
1459   $1 = (CvMat*)PyObject_to_CvArr($input, &freearg);
1460   int m = $1->rows * $1->cols;
1461   if (m<2) {
1462     PyErr_SetString (PyExc_TypeError,"must give at least 2 coefficients");
1463     return NULL;
1464   }
1465   $2 = cvCreateMat(m-1, 1, CV_MAKETYPE(CV_MAT_DEPTH($1->type),2));
1466 }
1467 %typemap(argout) (const CvMat* coeffs, CvMat *roots2)
1468 {
1469   $result = SWIG_AppendOutput( $result, SWIG_NewPointerObj($2, $descriptor(CvMat *), 1) );
1470 }
1471 %typemap(freearg) (const CvMat* coeffs, CvMat *roots2)
1472 {
1473   if($1!=NULL && freearg$argnum){
1474     cvReleaseData( $1 );
1475     cvFree(&($1));
1476   }
1477 }
1478