Page 1 of 1

Using Lisp's CFFI defcstruct with OpenCV's cvSize and CvSize

Posted: Sun Sep 29, 2013 8:21 pm
by joeish80829
I have half successfully wrapped the OpenCV cvSize function

Code: Select all

CV_INLINE  CvSize  cvSize( int width, int height )
and I would like to acces the slots of the defctruct cv-size but could use help here are my steps:

i created a cl-opencv-glue.c file and adding this

Code: Select all

CvSize  cvSize_glue(int width, int height)
{
    return cvSize(width, height);
}
then created cl-opencv-glue.h file and adding this:

Code: Select all

  /* CvSize  cvSize(int width, int height) */
     CvSize  cvSize_glue(int width, int height);
then builded a

Code: Select all

/usr/local/lib/libcl-opencv-glue.so 
which builds successfully

then created a wrapper for the glue function like this:

Code: Select all

;; CvSize cvSize(int width, int height)
(cffi:defcfun ("cvSize_glue" size) (:pointer (:struct cv-size))
   "Constructs CvSize structure."
   (width :int)
   (height :int))
then i can run this program successfully:

Code: Select all

(defun init-image-header-example (&optional (width *default-width*)
                                            (height *default-height*))
  "Initializes an image header that was previously allocated."
  (let* ((img-size (size width height))
         (image (create-image img-size +ipl-depth-8u+ 3))
         (image-re-init (init-image-header image img-size 
                         +ipl-depth-8u+ 3 
                         +ipl-origin-tl+ 4))
          (window-name "INIT-IMAGE-HEADER Example"))      
            (named-window window-name +window-normal+)
            (move-window window-name 702 285)
            (create-data image-re-init)
            (show-image window-name image-re-init)
       (loop while (not (= (wait-key 0) 27)))
      (release-image image-re-init)
        (destroy-window window-name)))
the

Code: Select all

(img-size (size width height))
line successfully creates a size structure for the:

Code: Select all

(create-image img-size +ipl-depth-8u+ 3)
which is wrapped like this

Code: Select all

;; IplImage* cvCreateImage(CvSize size, int depth, int channels)
(cffi:defcfun ("cvCreateImage" create-image) (:pointer (:struct ipl-image))
  (size (:pointer (:struct cv-size)))
  (depth :int)
  (channels :int))
now for testing i create a size struct at the repl(w/ output):

Code: Select all

CL-OPENCV>       (defparameter size (size 1280 1024))
SIZE
CL-OPENCV> size
#.(SB-SYS:INT-SAP #X40000000500)
CL-OPENCV> 
now id like to access slots of the size pointer so i can get size.width and size.height so i can create a size-width and size-height function in lisp

so i attempt to get at the slots by running at the repl(w/ output)

Code: Select all

CL-OPENCV> (cffi:with-foreign-object (size '(:struct cv-size))
          ;; Initialize the slots

          ;; Return a list with the coordinates
          (cffi:with-foreign-slots ((width height) size (:struct cv-size))
            (list width height)))

 Output >  (754484 0)
and get this

Code: Select all

(754484 0)   
my defcstruct created by swig is

Code: Select all

(cffi:defcstruct cv-size
      (width :int)
      (height :int))
and the defined Opencv struct is this(along with the CV_INLINE for reference):

here is how they are defined in /home/w/Downloads/opencv-2.4.6.1/modules/core/include/opencv2/core/types_c.h:

Code: Select all

 typedef struct CvSize
 {
     int width;
     int height;
 }
 CvSize;

 CV_INLINE  CvSize  cvSize( int width, int height )
 {
     CvSize s;

     s.width = width;
     s.height = height;

     return s;
 }  
so i change my glue.c to(and rebuild .so)

Code: Select all

CvSize  cvSize_glue(int width, int height)
{
    cvSize(width, height);
}
i re-open emacs and run the (init-image-header-example) function above and it runs so i rerun the above repl test(shown below w/ output)

Code: Select all

(cffi:with-foreign-object (size '(:struct cv-size))
          ;; Initialize the slots

          ;; Return a list with the coordinates
          (cffi:with-foreign-slots ((width height) size (:struct cv-size))
            (list width height)))

Output >  (338509 0)
compared to above test of

Code: Select all

(754484 0)
it is similar but different the 754484 changes evertime regardless of whether return is there or not I tried every variation of the defcstruct cv-size parameter i/e replacing the cv-size in

Code: Select all

(cffi:defcstruct cv-size
      (width :int)
      (height :int))
with all the below:

Code: Select all

:struct cv-size, (:struct cv-size), (:pointer (:struct cv-size)), :struct cv-size
and get errors for all....(didnt include errors for sake of length of post(but will if neccesary)) i then try the below to access the slots (shown /w output)

Code: Select all

CL-OPENCV>       (defparameter size (size 1280 1024))
 SIZE
 CL-OPENCV> size
 #.(SB-SYS:INT-SAP #X40000000500)
 CL-OPENCV> (cffi:with-foreign-slots ((width height) 

                           size (:struct cv-size))
                           (format t "width = ~a~%height = ~a~%" 
                         width height ))
but get a

Code: Select all

[Condition of type SB-SYS:MEMORY-FAULT-ERROR]
after last entry

Any help on this would be much appreciated...