[cffi-devel] How do i cast variables

Martin Simmons martin at lispworks.com
Fri Oct 25 11:14:12 UTC 2013


[ please post back to the list, not private email ]

It looks like you are confused about the difference in CFFI between a pointer
to a struct and a pointer to a pointer to a struct.  Unfortunately, CFFI
pointers are untyped so you don't get errors by doing the wrong thing.

To call cvFindContours as in the example, you need a pointer to a pointer to a
struct (&contour in the C code).  Whenever you want to translate C code that
uses &variable, you need to allocate a pointer to hold the value and then
dereference it after calling the function.

E.g. something like this (untested):

(cffi:with-foreign-object (contour-ptr (:pointer (:struct cv-seq)))
  ;; contour-ptr has foreign type (:pointer (:pointer (:struct cv-seq)))
  (cv-find-contours ... contour-ptr ...)
  (mem-ref contour-ptr '(:pointer (:struct cv-seq))))

__Martin


>>>>> On Wed, 23 Oct 2013 17:04:07 -0700 (PDT), Joeish W said:
> 
> well I'm trying to learn about Sequences in opencv. So this was practice at making the code at this link 
> 
> http://opencv-srf.blogspot.com/2011/09/object-detection-tracking-using-contours.html
> 
> under the 
> 
> Shape Detection & Tracking using Contours 
> heading .  in that code there is this line CvSeq* contours;  since i dont know how to initialize a struct pointer in lisp as it is done there ive been using  CvSeq* contours = cvCreateSeq(0, sizeof(CvSeq),sizeof(CvPoint),storage); which also initializes a CvSeq* and works in that example. if you could advise me initializing structs I would be most grateful.....Basically I'm starting off by mastering the CvSeq parts of the code.
> .
> 
> 
> 
> On Wednesday, October 23, 2013 3:23 AM, Martin Simmons <martin at lispworks.com> wrote:
>  
>>>>> On Tue, 22 Oct 2013 15:48:50 -0700 (PDT), Joeish W said:
> >
> >> 
> >> take this for instance 
> >> 
> >> CvPoint points[2];
> >> 
> >> 
> >> id like to be able to cast  points[2] to the CvPoint struct  more info here http://docs.opencv.org/modules/core/doc/old_basic_structures.html?highlight=cvpoint#CvPoint
> >> 
> >> 
> >> here is my struct which works for other things
> >> 
> >> 
> >> 
> >> ;; (cffi:foreign-type-size '(:struct cv-point)) = 8 
> >> (cffi:defcstruct cv-point 
> >> (x :int) 
> >> (y :int)) 
> >> 
> >> 
> >> put in a defparameter you cant just go 
> >> 
> >> (defparameter points (cffi:foreign-alloc :int :count 2))
> >> 
> >> (defparameter a ((:struct cv-point) points))
> >> 
> >> 
> >> you get an illegal function call error...any help is appreciated
> >
> >Your example is inconsistent -- in C, your points variable is an array of two
> >CvPoints, but in Lisp it is an array of two ints.
> >
> >What are you really trying to do?
> >
> >I suggest you post the C code that you are trying to convert.  The opencv doc
> >for CvPoint doesn't use casting or arrays of ints.
> >
> >__Martin
> >
> >
> >
> >
> >
> 



More information about the cffi-devel mailing list