Sunday, 29 September 2013

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

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

I have half successfully wrapped the OpenCV cvSize function
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
CvSize cvSize_glue(int width, int height)
{
return cvSize(width, height);
}
then created cl-opencv-glue.h file and adding this:
/* CvSize cvSize(int width, int height) */
CvSize cvSize_glue(int width, int height);
then builded a
/usr/local/lib/libcl-opencv-glue.so
which builds successfully
then created a wrapper for the glue function like this:
;; 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:
(defun init-image-header-example (&optional (width 640)
(height 480))
"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
(img-size (size width height))
line successfully creates a size structure for the:
(create-image img-size +ipl-depth-8u+ 3)
which is wrapped like this
;; 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):
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)
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
(754484 0)
my defcstruct created by swig is
(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:
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)
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)
(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
(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
(cffi:defcstruct cv-size
(width :int)
(height :int))
with all the below:
: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)
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
[Condition of type SB-SYS:MEMORY-FAULT-ERROR]
after last entry
Any help on this would be much appreciated...

No comments:

Post a Comment