ok I tried this translate-from-foreign method and it did work I have these defined in my structs.lisp file in my library which loads first before all my other dependencies
(cffi:defcstruct (cv-size :class cv-size-type)
(width :int)
(height :int))
(defmethod cffi:translate-from-foreign (p (type cv-size-type))
(let ((plist (call-next-method)))
(make-size :width (getf plist 'width)
:height (getf plist 'height))))
and my opencv wrappers for CvGetSize and cvCreateImage, get-size and create-image, are defined like this
;; CvSize cvGetSize(const CvArr* arr)
(cffi:defcfun ("cvGetSize" get-size) (:struct cv-size)
(arr cv-arr))
;; IplImage* cvCreateImage(CvSize size, int depth, int channels)
(cffi:defcfun ("cvCreateImage" %create-image) ipl-image
(size :int64)
(depth :int)
(channels :int))
(defun create-image (size depth channels)
"Create an image with dimensions given by SIZE, DEPTH bits per
channel, and CHANNELS number of channels."
(let ((nsize (size->int64 size)))
(%create-image nsize depth channels)))
here is the definition of size->int64
(DEFUN SIZE->INT64 (S) (+ (SIZE-WIDTH S) (ASH (SIZE-HEIGHT S) 32)))
it converts get-size output which is a structure here:
#S(SIZE :WIDTH 640 :HEIGHT 480)
into 64-bit integer, which CFFI can handle
but I love the idea of the translate-foreign defmethod's
so I was wondering if you can show my how to make the translate-into-foreign version of the below from method this would really make my library awesome
(defmethod cffi:translate-from-foreign (p (type cv-size-type))
(let ((plist (call-next-method)))
(make-size :width (getf plist 'width)
:height (getf plist 'height))))
I was going to try stuff and add it but for the get-size output structure, it isn't a plist so not really sure what to put there for the
(let ((plist (call-next-method)))
part, for the
(make-size :width (getf plist 'width)
:height (getf plist 'height))))
part, I was hoping to find another method other than the size->64 function because that was made 2 years ago when cl-opencv https://github.com/ryepup/cl-opencv first came out and I would like to make an even better wrapper than that... I've already taken cl-opencv added 100 new function 5000 lines of code samples and documentation and a new structs.lisp file so I would love if someone could help me with all the latest cffi tools so I could do something else than int64...plus the if I have a function to wrap where the int64 thing wouldn't work ill be ready
Thanks again to all the answerers on S.O. you all really have helped my library great.
Edit
Ok I think I defined everthing as you Mr Madeira as below (i show the repl session)
CL-OPENCV>
;; (cffi:foreign-type-size '(:struct cv-size)) = 8
(cffi:defcstruct (cv-size :class cv-size-type)
(width :int)
(height :int))
(defmethod cffi:translate-from-foreign (p (type cv-size-type))
(let ((plist (call-next-method)))
(make-size :width (getf plist 'width)
:height (getf plist 'height))))
(defmethod cffi:translate-to-foreign (value (type cv-size-type))
(let ((plist ()))
(setf (getf plist 'width) (size-width value)
(getf plist 'height) (size-height value))
(call-next-method plist type)))
;; CvSize cvGetSize(const CvArr* arr)
(cffi:defcfun ("cvGetSize" get-size) (:struct cv-size)
(arr (:pointer cv-arr)))
;; IplImage* cvCreateImage(CvSize size, int depth, int channels)
(cffi:defcfun ("cvCreateImage" create-image) (:pointer (:struct ipl-image))
(size (:struct cv-size))
(depth :int)
(channels :int))
STYLE-WARNING: redefining CL-OPENCV:GET-SIZE in DEFUN
STYLE-WARNING: redefining CL-OPENCV:CREATE-IMAGE in DEFUN
CREATE-IMAGE
CL-OPENCV> (defparameter capture (create-camera-capture 0))
(defparameter frame (query-frame capture))
(defparameter img-size (get-size frame))
(defparameter img (create-image img-size +ipl-depth-8u+ 3))
but I get error
There is no applicable method for the generic function
#<STANDARD-GENERIC-FUNCTION
CFFI:TRANSLATE-INTO-FOREIGN-MEMORY (5)>
when called with arguments
(#S(SIZE :WIDTH 640 :HEIGHT 480) #<CV-SIZE-TYPE CV-SIZE>
#.(SB-SYS:INT-SAP #X7FFFE5427FF0)).
[Condition of type SIMPLE-ERROR]
because the translate-from-foreign function I have is converting the output from cv-size into a structure
CL-OPENCV> img-size
#S(SIZE :WIDTH 640 :HEIGHT 480)
I appreciate the translate-into foreign function but with the old translate-from-foreign function it isn't woking, because of the make-size part...would you help me figure out what cvCreateImage needs to satisfy it ....here is the link 4 that:
http://docs.opencv.org/modules/core/doc/old_basic_structures.html?highlight=eimage#createimage
I can get this version below to run right(i show the repl session)
5
CL-OPENCV> ; TODO SIZE-WIDTH AND HEIGHT
;; CvSize cvGetSize(const CvArr* arr)
(cffi:defcfun ("cvGetSize" get-size) (:pointer (:struct cv-size))
(arr cv-arr))
;; 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))
STYLE-WARNING: redefining CL-OPENCV:GET-SIZE in DEFUN
STYLE-WARNING: redefining CL-OPENCV:CREATE-IMAGE in DEFUN
CREATE-IMAGE
CL-OPENCV> (defparameter capture (create-camera-capture 0))
(defparameter frame (query-frame capture))
(defparameter img-size (get-size frame))
(defparameter img (create-image img-size +ipl-depth-8u+ 3))
IMG
CL-OPENCV> (cffi:with-foreign-slots ((n-size id n-channels
alpha-channel depth color-model
channel-seq data-order origin
align width height roi
mask-roi image-id tile-info
image-size image-data width-step
border-mode border-const image-data-origin)
img(:struct ipl-image))
(format t "n-size = ~a~%id = ~a~%n-channels = ~a~%alpha-channel = ~a~%depth = ~a~%color-model = ~a~%channel-seq = ~a~%data-order = ~a~%origin = ~a~%align = ~a~%width = ~a~%height = ~a~%roi = ~a~%mask-roi = ~a~%image-id = ~a~%tile-info = ~a~%image-size = ~a~%image-data = ~a~%width-step = ~a~%border-mode = ~a~%border-const = ~a~%image-data-origin = ~a~%"
n-size id n-channels
alpha-channel depth color-model
channel-seq data-order origin
align width height roi
mask-rOI image-id tile-info
image-size image-data width-step
border-mode border-const image-data-origin))
n-size = 144
id = 0
n-channels = 3
alpha-channel = 0
depth = 8
color-model = 4343634
channel-seq = 5392194
data-order = 0
origin = 0
align = 4
width = 640
height = 480
roi = #.(SB-SYS:INT-SAP #X00000000)
mask-roi = #.(SB-SYS:INT-SAP #X00000000)
image-id = #.(SB-SYS:INT-SAP #X00000000)
tile-info = #.(SB-SYS:INT-SAP #X00000000)
image-size = 921600
image-data =
width-step = 1920
border-mode = #.(SB-SYS:INT-SAP #X00000000)
border-const = #.(SB-SYS:INT-SAP #X00000000)
image-data-origin = NIL
NIL
so I get data from the slots for ipl-image but this does'nt seem like correct way because id have to be able to derefrence the cv-size poiner output by get-size
here is documentation on cvGetSize the function im wrapping
http://docs.opencv.org/modules/core/doc/old_basic_structures.html?highlight=eimage#getsize
as u can see it is a pointer
CL-OPENCV> img-size
#.(SB-SYS:INT-SAP #X1E000000280)
so when I do :
(cffi:with-foreign-object (img-size '(:pointer (:struct cv-size)))
;; Initialize the slots
;; Return a list with the coordinates
(cffi:with-foreign-slots ((width height) img-size
(list width height)))
I get
There is no applicable method for the generic function
#<STANDARD-GENERIC-FUNCTION CFFI::SLOTS (1)>
when called with arguments
(#<CFFI::FOREIGN-POINTER-TYPE (:POINTER (:STRUCT CV-SIZE))>).
[Condition of type SIMPLE-ERROR]
and when I do
(cffi:with-foreign-object (img-size '(:struct cv-size))
;; Initialize the slots
;; Return a list with the coordinates
(cffi:with-foreign-slots ((width height) img-size (:struct cv-size))
(list width height)))
I get
(346539 0)
just nonsensical output
I try mem-refing and mem-arefing the pointer and get unhandled memory fault errors
if you can help me figure out how to write compatible
translate-from-foreign
and
translate-into-foreign functions I would be very grateful =).
but if I use make-size or size-width,height anywhere in them the create-image would have to have the size->int64 in it because they work only because that function.
cffi:translation-forms-for-class
defines a specialization oncffi:translate-from-foreign
like that of user2735131's and a specialization ofcffi:translate-into-foreign-memory
like your sample. The defaultcffi:translate-to-foreign
specialized tocffi:foreign-struct-type
delegates the serialization tocffi:translate-into-foreign-memory
aftercffi:foreign-alloc
. Good point on the:int64
vs(:struct cv-size)
and method specialization. – Merkel