Page 1 of 1

How do i write a cffi:translate-into foreign defmethod?

Posted: Wed Oct 02, 2013 2:32 am
by joeish80829
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

Code: Select all

(cffi:defcstruct (cv-size :class cv-size-type)
  (width :int)
  (height :int))

Code: Select all

(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

Code: Select all

;; CvSize cvGetSize(const CvArr* arr)
 (cffi:defcfun ("cvGetSize" get-size) (:struct cv-size)
   (arr cv-arr))

Code: Select all

 ;; IplImage* cvCreateImage(CvSize size, int depth, int channels)
 (cffi:defcfun ("cvCreateImage" %create-image) ipl-image
   (size :int64)
   (depth :int)
   (channels :int))

Code: Select all

 (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

Code: Select all

(DEFUN SIZE->INT64 (S) (+ (SIZE-WIDTH S) (ASH (SIZE-HEIGHT S) 32)))
it converts get-size output which is a structure here:

Code: Select all

#S(SIZE :WIDTH 640 :HEIGHT 480)
into 64-bit integer, which CFFI can handle (at least on some platforms)

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

Code: Select all

(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 isnt a plist so not really sure what to put there for the

Code: Select all

(let ((plist (call-next-method)))
part, for the

Code: Select all

 (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 funtion to wrap where the int64 thing wouldnt work ill be ready