Move the sources to trunk
[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         $1 = PyObject_to_CvArr($input, &freearg);
46 }
47 %typemap(freearg) (CvArr *) {
48         if($1!=NULL && freearg$argnum){
49                 cvReleaseData( $1 );
50                 cvFree(&($1));
51         }
52 }
53 %typecheck(SWIG_TYPECHECK_POINTER) CvArr * {
54         void *ptr;
55         if(PyList_Check($input) || PyTuple_Check($input)) {
56                 $1 = 1;
57         }
58         else if (SWIG_ConvertPtr($input, &ptr, 0, 0) == -1) {
59                 $1 = 0;
60                 PyErr_Clear();
61         }
62         else{
63                 $1 = 1;
64         }
65 }
66
67 // for cvReshape, cvGetRow, where header is passed, then filled in
68 %typemap(in, numinputs=0) CvMat * OUTPUT (CvMat * header) {
69         header = (CvMat *)cvAlloc(sizeof(CvMat));
70         $1 = header;
71 }
72 %newobject cvReshape;
73 %newobject cvGetRow;
74 %newobject cvGetRows;
75 %newobject cvGetCol;
76 %newobject cvGetCols;
77 %newobject cvGetSubRect;
78 %newobject cvGetDiag;
79
80 %apply CvMat *OUTPUT {CvMat * header};
81 %apply CvMat *OUTPUT {CvMat * submat};
82
83 /**
84  * In C, these functions assume input will always be around at least as long as header,
85  * presumably because the most common usage is to pass in a reference to a stack object.  
86  * i.e
87  * CvMat row;
88  * cvGetRow(A, &row, 0);
89  *
90  * As a result, the header is not refcounted (see the C source for cvGetRow, Reshape, in cxarray.cpp)
91  * However, in python, the header parameter is implicitly created so it is easier to create
92  * situations where the sub-array outlives the original header.  A simple example is:
93  * A = cvReshape(A, -1, A.rows*A.cols)
94  *
95  * since python doesn't have an assignment operator, the new header simply replaces the original,
96  * the refcount of the original goes to zero, and cvReleaseMat is called on the original, freeing both
97  * the header and data.  The new header is left pointing to invalid data.  To avoid this, need to add
98  * refcount field to the returned header.
99 */
100 %typemap(argout) (const CvArr* arr, CvMat* header) {
101         $2->hdr_refcount = ((CvMat *)$1)->hdr_refcount;
102         $2->refcount = ((CvMat *)$1)->refcount;
103         cvIncRefData($2);
104 }
105 %typemap(argout) (const CvArr* arr, CvMat* submat) {
106         $2->hdr_refcount = ((CvMat *)$1)->hdr_refcount;
107         $2->refcount = ((CvMat *)$1)->refcount;
108         cvIncRefData($2);
109 }
110
111 /* map scalar or sequence to CvScalar, CvPoint2D32f, CvPoint */
112 %typemap(in) (CvScalar) {
113         $1 = PyObject_to_CvScalar( $input );
114 }
115 %typemap(in) (CvPoint) {
116         $1 = PyObject_to_CvPoint($input);
117 }
118 %typemap(in) (CvPoint2D32f) {
119         $1 = PyObject_to_CvPoint2D32f($input);
120 }
121
122
123 /* typemap for cvGetDims */
124 %typemap(in) (const CvArr * arr, int * sizes = NULL) (void * myarr, int mysizes[CV_MAX_DIM]){
125         SWIG_Python_ConvertPtr($input, &myarr, 0, SWIG_POINTER_EXCEPTION);
126         $1=(CvArr *)myarr;
127         $2=mysizes;
128 }
129
130 %typemap(argout) (const CvArr * arr, int * sizes = NULL) {
131         int len = PyInt_AsLong( $result );
132         PyObject * obj = PyTuple_FromIntArray( $2, len );
133         Py_DECREF( $result );
134         $result = obj;
135 }
136                                 
137 /* map one list of integer to the two parameters dimension/sizes */
138 %typemap(in) (int dims, int* sizes) {
139     int i;
140
141     /* get the size of the dimention array */
142     $1 = PyList_Size ($input);
143
144     /* allocate the needed memory */
145     $2 = (int *)malloc ($1 * sizeof (int));
146
147     /* extract all the integer values from the list */
148     for (i = 0; i < $1; i++) {
149         PyObject *item = PyList_GetItem ($input, i);
150         $2 [i] = (int)PyInt_AsLong (item);
151     }
152 }
153
154 /* map one list of integer to the parameter idx of
155    cvGetND, cvSetND, cvClearND, cvGetRealND, cvSetRealND and cvClearRealND */
156 %typemap(in) (int* idx) {
157     int i;
158     int size;
159
160     /* get the size of the dimention array */
161     size = PyList_Size ($input);
162
163     /* allocate the needed memory */
164     $1 = (int *)malloc (size * sizeof (int));
165
166     /* extract all the integer values from the list */
167     for (i = 0; i < size; i++) {
168         PyObject *item = PyList_GetItem ($input, i);
169         $1 [i] = (int)PyInt_AsLong (item);
170     }
171 }
172
173 /* map a list of list of float to an matrix of floats*/
174 %typemap(in) float** ranges {
175     int i1;
176     int i2;
177     int size1;
178     int size2 = 0;
179
180     /* get the number of lines of the matrix */
181     size1 = PyList_Size ($input);
182
183     /* allocate the correct number of lines for the destination matrix */
184     $1 = (float **)malloc (size1 * sizeof (float *));
185
186     for (i1 = 0; i1 < size1; i1++) {
187
188         /* extract all the lines of the matrix */
189         PyObject *list = PyList_GetItem ($input, i1);
190
191         if (size2 == 0) {
192             /* size 2 wasn't computed before */
193             size2 = PyList_Size (list);
194         } else if (size2 != PyList_Size (list)) {
195             /* the current line as a different size than the previous one */
196             /* so, generate an exception */
197             SWIG_exception (SWIG_ValueError, "Lines must be the same size");
198         }
199
200         /* allocate the correct number of rows for the current line */
201         $1 [i1] = (float *)malloc (size2 * sizeof (float));
202
203         /* extract all the float values of this row */
204         for (i2 = 0; i2 < size2; i2++) {
205             PyObject *item = PyList_GetItem (list, i2);
206             $1 [i1][i2] = (float)PyFloat_AsDouble (item);
207         }
208     }
209 }
210
211 /**
212  * map the output parameter of the cvGetMinMaxHistValue()
213  * so, we can call cvGetMinMaxHistValue() in Python like:
214  * min_value, max_value = cvGetMinMaxHistValue (hist, None, None)
215  */
216 %apply int *OUTPUT {int *min_idx};                                                                   
217 %apply int *OUTPUT {int *max_idx}; 
218 %apply float *OUTPUT {float *min_value};
219 %apply float *OUTPUT {float *max_value};
220 /**
221  * map output parameters of cvMinMaxLoc
222  */
223 %apply double *OUTPUT {double* min_val, double* max_val};
224
225 /**
226  * the input argument of cvPolyLine "CvPoint** pts" is converted from 
227  * a "list of list" (aka. an array) of CvPoint().
228  * The next parameters "int* npts" and "int contours" are computed from
229  * the givne list.
230  */
231 %typemap(in) (CvPoint** pts, int* npts, int contours){
232     int i;
233     int j;
234     int size2 = -1;
235     CvPoint **points = NULL;
236     int *nb_vertex = NULL;
237
238         if(!PySequence_Check($input)){
239                 SWIG_exception(SWIG_TypeError, "Expected a list for argument $argnum\n");
240                 return NULL;
241         }
242
243     /* get the number of polylines input array */
244     int size1 = PySequence_Size ($input);
245     $3 = size1;
246
247         if(size1>0){
248             /* create the points array */
249             points = (CvPoint **)malloc (size1 * sizeof (CvPoint *));
250
251             /* point to the created array for passing info to the C function */
252             $1 = points;
253
254             /* create the array for memorizing the vertex */
255             nb_vertex = (int *)malloc (size1 * sizeof (int));
256             $2 = nb_vertex;
257         }
258     for (i = 0; i < size1; i++) {
259
260                 /* get the current item */
261                 PyObject *line = PySequence_GetItem ($input, i);
262
263                 if(!PySequence_Check(line)){
264                         SWIG_exception(SWIG_TypeError, "Expected a sequence of sequences of integers for argument $argnum\n");
265                         // TODO: cleanup here
266                 }
267
268                 /* get the size of the current polyline */
269                 size2 = PySequence_Size (line);
270
271
272                 if(size2>0){
273                         /* allocate the necessary memory to store the points */
274                         points [i] = (CvPoint *)malloc (size2 * sizeof (CvPoint));
275                 }
276
277                 /* memorize the size of the polyline in the vertex list */
278                 nb_vertex [i] = size2;
279
280                 for (j = 0; j < size2; j++) {
281                     /* get the current item */
282                     PyObject *item = PySequence_GetItem (line, j);
283                         points[i][j] = PyObject_to_CvPoint( item );
284         }
285         }
286 }
287 /** Free arguments allocated before the function call */
288 %typemap(freearg) (CvPoint **pts, int* npts, int contours){
289         int i;
290         for(i=0;i<$3;i++){
291                 free($1[i]);
292         }
293         free($1);
294         free($2);
295 }
296
297 /** Macro to define typemaps to convert a python list of CvPoints to a C array of CvPoints */
298 %define %typemap_CvPoint_CArr(points_arg, numpoints_arg)
299
300 %typemap(in, numinputs=1) (CvPoint * points_arg, int numpoints_arg){
301         int i;
302         if(!PySequence_Check($input)){
303                 SWIG_exception(SWIG_TypeError, "Expected a list for argument $argnum\n");
304                 return NULL;
305         }
306         int size = PySequence_Size($input);
307         CvPoint * points = (CvPoint *)malloc(size*sizeof(CvPoint));
308         for(i=0; i<size; i++){
309                 PyObject *item = PySequence_GetItem($input, i);
310                 points[i] = PyObject_to_CvPoint( item );
311         }
312         $1 = points;
313         $2 = size;
314 }
315 %typemap(freearg) (CvPoint *points_arg, int numpoints_arg){
316         free((char *)$1);
317 }
318 %enddef
319
320 /* apply to cvFillConvexPoly */
321 %typemap_CvPoint_CArr(pts, npts)
322
323 /**
324  * this is mainly an "output parameter"
325  * So, just allocate the memory as input
326  */
327 %typemap (in, numinputs=0) (CvSeq ** OUTPUT) (CvSeq * seq) {
328     $1 = &seq;
329 }
330
331 /**
332  * return the finded contours with all the others parametres
333  */
334 %typemap(argout) (CvSeq ** OUTPUT) {
335     PyObject *to_add;
336
337     /* extract the pointer we want to add to the returned tuple */
338         /* sequence is allocated in CvMemStorage, so python_ownership=0 */
339     to_add = SWIG_NewPointerObj (*$1, $descriptor(CvSeq*), 0); 
340
341         $result = SWIG_AppendResult($result, &to_add, 1);
342 }
343 %apply CvSeq **OUTPUT {CvSeq **first_contour};
344 %apply CvSeq **OUTPUT {CvSeq **comp};
345
346 /**
347  * CvArr ** image can be either one CvArr or one array of CvArr
348  * (for example like in cvCalcHist() )
349  * From Python, the array of CvArr can be a tuple.
350  */
351 %typemap(in) (CvArr ** INPUT) (
352     CvArr * one_image=NULL, 
353     bool free_one_arg=false, 
354     CvArr ** many_images=NULL, 
355     bool *free_many_args=NULL, 
356     int nimages=0 ) {
357
358     /* first, check if this is a tuple */
359     if PyTuple_Check ($input) {
360         /* This is a tuple, so we need to test each element and pass
361             them to the called function */
362
363         int i;
364
365         /* get the size of the tuple */
366         nimages = PyTuple_Size ($input);
367
368         /* allocate the necessary place */
369         many_images = (CvArr **)malloc (nimages * sizeof (CvArr *));
370         free_many_args = (bool *)malloc(nimages * sizeof(bool));
371
372         for (i = 0; i < nimages; i++) {
373
374             /* convert the current tuple element to a CvArr *, and
375                store to many_images [i] */
376             many_images[i] = PyObject_to_CvArr (PyTuple_GetItem ($input, i),
377                                                 free_many_args+i);
378
379             /* check that the current item is a correct type */
380             if(!many_images[i]) {
381                 /* incorrect ! */
382                 SWIG_fail;
383             }
384         }
385
386         /* what to give to the called function */
387         $1 = many_images;
388
389     } else if((one_image = PyObject_to_CvArr( $input, &free_one_arg ))){
390
391         /* this is just one CvArr *, so one_image will receive it */
392         $1 = &one_image;
393
394     } else {
395         /* not a CvArr *, not a tuple, this is wrong */
396         SWIG_fail;
397     }
398 }
399 %apply CvArr ** INPUT {CvArr ** img};
400 %apply CvArr ** INPUT {CvArr ** image};
401 %apply CvArr ** INPUT {CvArr ** arr};
402 %apply CvArr ** INPUT {CvArr ** vects};
403
404 %typemap(freearg) (CvArr ** FREEARG) {
405         if(free_one_arg$argnum){
406                 cvFree(&(one_image$argnum));
407         }
408         else if(free_many_args$argnum){
409                 int i;
410                 for (i=0; i<nimages$argnum; i++){
411                         if(free_many_args$argnum[i]){
412                                 cvReleaseData(many_images$argnum[i]);
413                                 cvFree(many_images$argnum+i);
414                         }
415                 }
416                 free(many_images$argnum);
417                 free(free_many_args$argnum);
418         }
419
420 }
421 %apply CvArr ** FREEARG {CvArr ** img};
422 %apply CvArr ** FREEARG {CvArr ** image};
423 %apply CvArr ** FREEARG {CvArr ** arr};
424 %apply CvArr ** FREEARG {CvArr ** vects};
425
426 /**
427  * Map the CvFont * parameter from the cvInitFont() as an output parameter
428  */
429 %typemap (in, numinputs=1) (CvFont* font, int font_face) {
430     $1 = (CvFont *)malloc (sizeof (CvFont));
431     $2 = (int)PyInt_AsLong ($input); 
432     if (SWIG_arg_fail($argnum)) SWIG_fail;
433 }
434 %typemap(argout) (CvFont* font, int font_face) {
435     PyObject *to_add;
436
437     /* extract the pointer we want to add to the returned tuple */
438     to_add = SWIG_NewPointerObj ($1, $descriptor(CvFont *), 0);
439
440         $result = SWIG_AppendResult($result, &to_add, 1);
441 }
442
443 /**
444  * these are output parameters for cvGetTextSize
445  */
446 %typemap (in, numinputs=0) (CvSize* text_size, int* baseline) {
447     CvSize *size = (CvSize *)malloc (sizeof (CvSize));
448     int *baseline = (int *)malloc (sizeof (int));
449     $1 = size;
450     $2 = baseline;
451 }
452
453 /**
454  * return the finded parameters for cvGetTextSize
455  */
456 %typemap(argout) (CvSize* text_size, int* baseline) {
457     PyObject * to_add[2];
458
459     /* extract the pointers we want to add to the returned tuple */
460     to_add [0] = SWIG_NewPointerObj ($1, $descriptor(CvSize *), 0);
461     to_add [1] = PyInt_FromLong (*$2);
462
463     $result = SWIG_AppendResult($result, to_add, 2);
464 }
465
466
467 /**
468  * curr_features is output parameter for cvCalcOpticalFlowPyrLK
469  */
470 %typemap (in, numinputs=1) (CvPoint2D32f* curr_features, int count)
471      (int tmpCount) {
472     /* as input, we only need the size of the wanted features */
473
474     /* memorize the size of the wanted features */
475     tmpCount = (int)PyInt_AsLong ($input);
476
477     /* create the array for the C call */
478     $1 = (CvPoint2D32f *) malloc(tmpCount * sizeof (CvPoint2D32f));
479
480     /* the size of the array for the C call */
481     $2 = tmpCount;
482 }
483
484 /**
485  * the features returned by cvCalcOpticalFlowPyrLK
486  */
487 %typemap(argout) (CvPoint2D32f* curr_features, int count) {
488     int i;
489     PyObject *to_add;
490     
491     /* create the list to return */
492     to_add = PyList_New (tmpCount$argnum);
493
494     /* extract all the points values of the result, and add it to the
495        final resulting list */
496     for (i = 0; i < tmpCount$argnum; i++) {
497         PyList_SetItem (to_add, i,
498                         SWIG_NewPointerObj (&($1 [i]),
499                                             $descriptor(CvPoint2D32f *), 0));
500     }
501
502         $result = SWIG_AppendResult($result, &to_add, 1);
503 }
504
505 /**
506  * status is an output parameters for cvCalcOpticalFlowPyrLK
507  */
508 %typemap (in, numinputs=1) (char *status) (int tmpCountStatus){
509     /* as input, we still need the size of the status array */
510
511     /* memorize the size of the status array */
512     tmpCountStatus = (int)PyInt_AsLong ($input);
513
514     /* create the status array for the C call */
515     $1 = (char *)malloc (tmpCountStatus * sizeof (char));
516 }
517
518 /**
519  * the status returned by cvCalcOpticalFlowPyrLK
520  */
521 %typemap(argout) (char *status) {
522     int i;
523     PyObject *to_add;
524
525     /* create the list to return */
526     to_add = PyList_New (tmpCountStatus$argnum);
527
528     /* extract all the integer values of the result, and add it to the
529        final resulting list */
530     for (i = 0; i < tmpCountStatus$argnum; i++) {
531                 PyList_SetItem (to_add, i, PyBool_FromLong ($1 [i]));
532     }
533
534         $result = SWIG_AppendResult($result, &to_add, 1); 
535 }
536
537 /* map one list of points to the two parameters dimenssion/sizes
538  for cvCalcOpticalFlowPyrLK */
539 %typemap(in) (CvPoint2D32f* prev_features) {
540     int i;
541     int size;
542
543     /* get the size of the input array */
544     size = PyList_Size ($input);
545
546     /* allocate the needed memory */
547     $1 = (CvPoint2D32f *)malloc (size * sizeof (CvPoint2D32f));
548
549     /* extract all the points values from the list */
550     for (i = 0; i < size; i++) {
551         PyObject *item = PyList_GetItem ($input, i);
552
553         void * vptr;
554         SWIG_Python_ConvertPtr (item, &vptr,
555                                 $descriptor(CvPoint2D32f*),
556                                 SWIG_POINTER_EXCEPTION);
557         CvPoint2D32f *p = (CvPoint2D32f *)vptr;
558         $1 [i].x = p->x;
559         $1 [i].y = p->y;
560     }
561 }
562
563 /**
564  * the corners returned by cvGoodFeaturesToTrack
565  */
566 %typemap (in, numinputs=1) (CvPoint2D32f* corners, int* corner_count)
567      (int tmpCount) {
568     /* as input, we still need the size of the corners array */
569
570     /* memorize the size of the status corners */
571     tmpCount = (int)PyInt_AsLong ($input);
572
573     /* create the corners array for the C call */
574     $1 = (CvPoint2D32f *)malloc (tmpCount * sizeof (CvPoint2D32f));
575
576     /* the size of the array for the C call */
577     $2 = &tmpCount;
578 }
579
580 /**
581  * the corners returned by cvGoodFeaturesToTrack
582  */
583 %typemap(argout) (CvPoint2D32f* corners, int* corner_count) {
584     int i;
585     PyObject *to_add;
586     
587     /* create the list to return */
588     to_add = PyList_New (tmpCount$argnum);
589
590     /* extract all the integer values of the result, and add it to the
591        final resulting list */
592     for (i = 0; i < tmpCount$argnum; i++) {
593         PyList_SetItem (to_add, i,
594                         SWIG_NewPointerObj (&($1 [i]),
595                                             $descriptor(CvPoint2D32f *), 0));
596     }
597
598     $result = SWIG_AppendResult($result, &to_add, 1);
599 }
600
601 /* map one list of points to the two parameters dimension/sizes
602    for cvFindCornerSubPix */
603 %typemap(in, numinputs=1) (CvPoint2D32f* corners, int count)
604      (int cornersCount, CvPoint2D32f* corners){
605     int i;
606
607         if(!PyList_Check($input)){
608                 PyErr_SetString(PyExc_TypeError, "Expected a list");
609                 return NULL;
610         }
611
612     /* get the size of the input array */
613     cornersCount = PyList_Size ($input);
614     $2 = cornersCount;
615
616     /* allocate the needed memory */
617     corners = (CvPoint2D32f *)malloc ($2 * sizeof (CvPoint2D32f));
618     $1 = corners;
619
620     /* the size of the array for the C call */
621
622     /* extract all the points values from the list */
623     for (i = 0; i < $2; i++) {
624         PyObject *item = PyList_GetItem ($input, i);
625
626         void *vptr;
627         SWIG_Python_ConvertPtr (item, &vptr,
628                                 $descriptor(CvPoint2D32f*),
629                                 SWIG_POINTER_EXCEPTION);
630         CvPoint2D32f *p = (CvPoint2D32f *) vptr;;
631         $1 [i].x = p->x;
632         $1 [i].y = p->y;
633     }
634
635 }
636
637 /**
638  * the corners returned by cvFindCornerSubPix
639  */
640 %typemap(argout) (CvPoint2D32f* corners, int count) {
641     int i;
642     PyObject *to_add;
643
644     /* create the list to return */
645     to_add = PyList_New (cornersCount$argnum);
646
647     /* extract all the corner values of the result, and add it to the
648        final resulting list */
649     for (i = 0; i < cornersCount$argnum; i++) {
650         PyList_SetItem (to_add, i,
651                         SWIG_NewPointerObj (&(corners$argnum [i]),
652                                             $descriptor(CvPoint2D32f *), 0));
653     }
654
655         $result = SWIG_AppendResult( $result, &to_add, 1);
656 }
657
658 /**
659  * return the corners for cvFindChessboardCorners
660  */
661 %typemap(in, numinputs=1) (CvSize pattern_size, CvPoint2D32f * corners, int * corner_count ) 
662      (CvSize * pattern_size, CvPoint2D32f * tmp_corners, int tmp_ncorners) {
663          void * vptr;
664         if( SWIG_ConvertPtr($input, &vptr, $descriptor( CvSize * ), SWIG_POINTER_EXCEPTION ) == -1){
665                 return NULL;
666         }
667         pattern_size=(CvSize *)vptr;
668         tmp_ncorners = pattern_size->width*pattern_size->height;
669
670         tmp_corners = (CvPoint2D32f *) malloc(sizeof(CvPoint2D32f)*tmp_ncorners);
671         $1 = *pattern_size;
672         $2 = tmp_corners;
673         $3 = &tmp_ncorners;
674 }
675
676 %typemap(argout) (CvSize pattern_size, CvPoint2D32f * corners, int * corner_count){
677     int i;
678     PyObject *to_add;
679
680     /* create the list to return */
681     to_add = PyList_New ( tmp_ncorners$argnum );
682
683     /* extract all the corner values of the result, and add it to the
684        final resulting list */
685     for (i = 0; i < tmp_ncorners$argnum; i++) {
686                 CvPoint2D32f * pt = new CvPoint2D32f;
687                 pt->x = tmp_corners$argnum[i].x;
688                 pt->y = tmp_corners$argnum[i].y;
689                 
690         PyList_SetItem (to_add, i,
691             SWIG_NewPointerObj( pt, $descriptor(CvPoint2D32f *), 0));
692     }
693
694         $result = SWIG_AppendResult( $result, &to_add, 1);
695     free(tmp_corners$argnum);
696 }
697
698 /**
699  * return the matrices for cvCameraCalibrate
700  */
701 %typemap(in, numinputs=0) (CvMat * intrinsic_matrix, CvMat * distortion_coeffs)
702 {
703         $1 = cvCreateMat(3,3,CV_32F);
704         $2 = cvCreateMat(4,1,CV_32F);
705 }
706
707 %typemap(argout) (CvMat * intrinsic_matrix, CvMat * distortion_coeffs)
708 {
709         PyObject * to_add[2] = {NULL, NULL};
710         to_add[0] = SWIG_NewPointerObj($1, $descriptor(CvMat *), 1);
711         to_add[1] = SWIG_NewPointerObj($2, $descriptor(CvMat *), 1);
712         $result = SWIG_AppendResult( $result, to_add, 2 );
713 }
714
715 /**
716  * Fix OpenCV inheritance for CvSeq, CvSet, CvGraph
717  * Otherwise, can't call CvSeq functions on CvSet or CvGraph
718 */
719 %typemap(in, numinputs=1) (CvSeq *) (void * ptr)
720 {
721         
722         if( SWIG_ConvertPtr($input, &ptr, $descriptor(CvSeq *), 0) == -1 &&
723             SWIG_ConvertPtr($input, &ptr, $descriptor(CvSet *), 0) == -1 &&
724             SWIG_ConvertPtr($input, &ptr, $descriptor(CvGraph *), 0) == -1 &&
725             SWIG_ConvertPtr($input, &ptr, $descriptor(CvSubdiv2D *), 0) == -1 &&
726             SWIG_ConvertPtr($input, &ptr, $descriptor(CvChain *), 0) == -1 &&
727             SWIG_ConvertPtr($input, &ptr, $descriptor(CvContour *), 0) == -1 &&
728             SWIG_ConvertPtr($input, &ptr, $descriptor(CvContourTree *), 0) == -1 )
729         {
730                 SWIG_exception (SWIG_TypeError, "could not convert to CvSeq");
731                 return NULL;
732         }
733         $1 = (CvSeq *) ptr;
734 }
735
736 %typemap(in, numinputs=1) (CvSet *) (void * ptr)
737 {
738         if( SWIG_ConvertPtr($input, &ptr, $descriptor(CvSet *), 0) == -1 &&
739             SWIG_ConvertPtr($input, &ptr, $descriptor(CvGraph *), 0) == -1 &&
740             SWIG_ConvertPtr($input, &ptr, $descriptor(CvSubdiv2D *), 0) == -1) 
741         {
742                 SWIG_exception (SWIG_TypeError, "could not convert to CvSet");
743                 return NULL;
744         }
745         $1 = (CvSet *)ptr;
746 }
747
748 %typemap(in, numinputs=1) (CvGraph *) (void * ptr)
749 {
750         if( SWIG_ConvertPtr($input, &ptr, $descriptor(CvGraph *), 0) == -1 &&
751             SWIG_ConvertPtr($input, &ptr, $descriptor(CvSubdiv2D *), 0) == -1) 
752         {
753                 SWIG_exception (SWIG_TypeError, "could not convert to CvGraph");
754                 return NULL;
755         }
756         $1 = (CvGraph *)ptr;
757 }
758
759 /**
760  * Remap output arguments to multiple return values for cvMinEnclosingCircle
761  */
762 %typemap(in, numinputs=0) (CvPoint2D32f * center, float * radius) (CvPoint2D32f * tmp_center, float tmp_radius) 
763 {
764         tmp_center = (CvPoint2D32f *) malloc(sizeof(CvPoint2D32f));
765         $1 = tmp_center;
766         $2 = &tmp_radius;
767 }
768 %typemap(argout) (CvPoint2D32f * center, float * radius)
769 {
770     PyObject * to_add[2] = {NULL, NULL};
771         to_add[0] = SWIG_NewPointerObj( tmp_center$argnum, $descriptor(CvPoint2D32f *), 1); 
772         to_add[1] = PyFloat_FromDouble( tmp_radius$argnum );
773
774     $result = SWIG_AppendResult($result, to_add, 2);
775 }
776
777 /** BoxPoints */
778 %typemap(in, numinputs=0) (CvPoint2D32f pt[4]) (CvPoint2D32f tmp_pts[4])
779 {
780         $1 = tmp_pts;
781 }
782 %typemap(argout) (CvPoint2D32f pt[4])
783 {
784         PyObject * to_add = PyList_New(4);
785         int i;
786         for(i=0; i<4; i++){
787                 CvPoint2D32f * p = new CvPoint2D32f;
788                 *p = tmp_pts$argnum[i];
789                 PyList_SetItem(to_add, i, SWIG_NewPointerObj( p, $descriptor(CvPoint2D32f *), 1 ) );
790         }
791         $result = SWIG_AppendResult($result, &to_add, 1);
792 }
793
794 /** Macro to wrap a built-in type that is used as an object like CvRNG and CvSubdiv2DEdge */
795 %define %wrap_builtin(type)
796 %inline %{
797 // Wrapper class
798 class type##_Wrapper {
799 private:
800         type m_val;
801 public:
802         type##_Wrapper( const type & val ) :
803                 m_val(val)
804         {
805         }
806         type * ptr() { return &m_val; }
807         type & ref() { return m_val; }
808         bool operator==(const type##_Wrapper & x){
809                 return m_val==x.m_val;
810         }
811         bool operator!=(const type##_Wrapper & x){
812                 return m_val!=x.m_val;
813         }
814 };
815 %}
816 %typemap(out) type
817 {
818         type##_Wrapper * wrapper = new type##_Wrapper( $1 );
819         $result = SWIG_NewPointerObj( wrapper, $descriptor( type##_Wrapper * ), 1 );
820 }
821 %typemap(out) type *
822 {
823         type##_Wrapper * wrapper = new type##_Wrapper( *($1) );
824         $result = SWIG_NewPointerObj( wrapper, $descriptor( type##_Wrapper * ), 1 );
825 }
826
827 %typemap(in) (type *) (void * vptr, type##_Wrapper * wrapper){
828         if(SWIG_ConvertPtr($input, &vptr, $descriptor(type##_Wrapper *), 0)==-1){
829                 SWIG_exception( SWIG_TypeError, "could not convert Python object to C value");
830                 return NULL;
831         }
832         wrapper = (type##_Wrapper *) vptr;
833         $1 = wrapper->ptr();
834 }
835 %typemap(in) (type) (void * vptr, type##_Wrapper * wrapper){
836         if(SWIG_ConvertPtr($input, &vptr, $descriptor(type##_Wrapper *), 0)==-1){
837                 SWIG_exception( SWIG_TypeError, "could not convert Python object to C value");
838                 return NULL;
839         }
840         wrapper = (type##_Wrapper *) vptr;
841         $1 = wrapper->ref();
842 }
843 %enddef 
844
845 /** Application of wrapper class to built-in types */
846 %wrap_builtin(CvRNG);
847 %wrap_builtin(CvSubdiv2DEdge);
848
849 /**
850  * Allow CvQuadEdge2D to be interpreted as CvSubdiv2DEdge
851  */
852 %typemap(in, numinputs=1) (CvSubdiv2DEdge) (CvSubdiv2DEdge_Wrapper * wrapper, CvQuadEdge2D * qedge, void *vptr)
853 {
854         if( SWIG_ConvertPtr($input, &vptr, $descriptor(CvSubdiv2DEdge_Wrapper *), 0) != -1 ){
855                 wrapper = (CvSubdiv2DEdge_Wrapper *) vptr;
856                 $1 = wrapper->ref();
857         }
858         else if( SWIG_ConvertPtr($input, &vptr, $descriptor(CvQuadEdge2D *), 0) != -1 ){
859                 qedge = (CvQuadEdge2D *) vptr;
860                 $1 = (CvSubdiv2DEdge)qedge;
861         }
862         else{
863                  SWIG_exception( SWIG_TypeError, "could not convert to CvSubdiv2DEdge");
864                  return NULL;
865         }
866 }
867
868 /**
869  * return the vertex and edge for cvSubdiv2DLocate
870  */
871 %typemap(in, numinputs=0) (CvSubdiv2DEdge * edge, CvSubdiv2DPoint ** vertex) 
872         (CvSubdiv2DEdge tmpEdge, CvSubdiv2DPoint * tmpVertex)
873 {
874         $1 = &tmpEdge;
875         $2 = &tmpVertex;
876 }
877 %typemap(argout) (CvSubdiv2DEdge * edge, CvSubdiv2DPoint ** vertex)
878 {
879         PyObject * to_add[2] = {NULL, NULL};
880         if(result==CV_PTLOC_INSIDE || result==CV_PTLOC_ON_EDGE){
881                 CvSubdiv2DEdge_Wrapper * wrapper = new CvSubdiv2DEdge_Wrapper( tmpEdge$argnum );
882                 to_add[0] = SWIG_NewPointerObj( wrapper, $descriptor(CvSubdiv2DEdge_Wrapper *), 0);
883                 to_add[1] = Py_None;
884         }
885         if(result==CV_PTLOC_VERTEX){
886                 to_add[0] = Py_None;
887                 to_add[1] = SWIG_NewPointerObj( tmpVertex$argnum, $descriptor(CvSubdiv2DPoint *), 0);
888         }
889         
890         $result = SWIG_AppendResult($result, to_add, 2);
891 }
892
893 /**
894  * int *value  in cvCreateTrackbar() is only used for input in the Python wrapper.
895  * for output, use the pos in the callback
896  * TODO: remove the memory leak introducted by the malloc () (if needed).
897  */
898 %typemap(in, numinputs=1) (int *value)
899 {
900     $1 = (int *)malloc (sizeof (int));
901     *$1 = PyInt_AsLong ($input);
902 }
903