How do i write a comparable cffi:translate-into foreign defmethod for this cffi:translate-from-foreign?
Joeish W
joeish80829 at yahoo.com
Sat Oct 5 10:37:38 UTC 2013
How do i write a comparable cffi:translate-into foreign defmethod for this cffi:translate-from-foreign?
up vote 0 down vote favorite 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 :classcv-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
;;CvSizecvGetSize(constCvArr*arr)(cffi:defcfun ("cvGetSize"get-size)(:structcv-size)(arr cv-arr));;IplImage*cvCreateImage(CvSizesize,intdepth,intchannels)(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 isa structure here:#S(SIZE :WIDTH 640 :HEIGHT 480)into64-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 isnt 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 funtion to wrap where the int64
thing wouldnt work ill be ready
Thanks again to all the answerers cffi-devel. you all really have helped my library great.
Edit: I also tried it this way, Below is my repl history showing my steps
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)) 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
Thereisnoapplicable method forthe genericfunction#<STANDARD-GENERIC-FUNCTIONCFFI:TRANSLATE-INTO-FOREIGN-MEMORY (5)>whencalled witharguments (#S(SIZE :WIDTH 640:HEIGHT 480)#<CV-SIZE-TYPE CV-SIZE>#.(SB-SYS:INT-SAP #X7FFFE5427FF0)).[Conditionof 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 isnt 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)
5CL-OPENCV>;TODO SIZE-WIDTH AND HEIGHT ;;CvSizecvGetSize(constCvArr*arr)(cffi:defcfun ("cvGetSize"get-size)(:pointer (:structcv-size))(arr cv-arr));;IplImage*cvCreateImage(CvSizesize,intdepth,intchannels)(cffi:defcfun ("cvCreateImage"create-image)(:pointer (:structipl-image))(size (:pointer (:structcv-size)))(depth :int)(channels :int))STYLE-WARNING:redefining CL-OPENCV:GET-SIZE inDEFUN STYLE-WARNING:redefining CL-OPENCV:CREATE-IMAGE inDEFUN 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-constimage-data-origin)img(:structipl-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-constimage-data-origin))n-size =144id =0n-channels =3alpha-channel =0depth =8color-model =4343634channel-seq =5392194data-order =0origin =0align =4width =640height =480roi =#.(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 =921600image-data =width-step =1920border-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
Thereisnoapplicable method forthe genericfunction#<STANDARD-GENERIC-FUNCTION CFFI::SLOTS (1)>whencalled witharguments (#<CFFI::FOREIGN-POINTER-TYPE (:POINTER (:STRUCT CV-SIZE))>).[Conditionof 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.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mailman.common-lisp.net/pipermail/cffi-devel/attachments/20131005/f0e25440/attachment.html>
More information about the cffi-devel
mailing list