My First SWIG Experience

by JS

A day of reading the SWIG manual and grokking the examples resulted in the following interface file. What does it do? It adds this to Python.

%module blobs
 
%exception
    {
    try { $action }
    catch (...)
        {
        return NULL;
        }
    }
 
 
%include "exception.i"
 
%typemap(in) IplImage * (IplImage header){
	void * vptr;
	int res = SWIG_ConvertPtr($input, (&vptr), $descriptor( CvMat * ), 0);
	if ( res == -1 ){
		SWIG_exception( SWIG_TypeError, "%%typemap(in) IplImage * : could not convert to CvMat");
		SWIG_fail;
	}
	$1 = cvGetImage((CvMat *)vptr, &header);
}
 
%typemap(in) IplImage *mask{
	if ($input == Py_None){
	   $1 = NULL;
	}
	else {
		SWIG_exception( SWIG_TypeError, "%%typemap(in) IplImage *mask : masks not supported yet.");
		SWIG_fail;
	}
}
 
%typecheck(SWIG_TYPECHECK_POINTER) IplImage * {
  void *ptr;
  if (SWIG_ConvertPtr($input, (void **) &ptr, $descriptor( CvMat * ), 0) == -1) {
    $1 = 0;
    PyErr_Clear();
  } else {
    $1 = 1;
  }
}
 
%{
#include "BlobResult.h"
#include "Blob.h"
%}
%include "BlobResult.h"
%include "Blob.h"