chicken-users
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Chicken-users] Basic FFI Principle in Chicken


From: Chris Mueller
Subject: Re: [Chicken-users] Basic FFI Principle in Chicken
Date: Mon, 09 Sep 2013 11:58:19 +0200
User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:17.0) Gecko/20130806 Thunderbird/17.0.8

On 09.09.2013 00:07, Kristian Lein-Mathisen wrote:
Ah, of course, John! I got them mixed up.

In that case Chris, I think your color struct might fit quite nicely
into a make-s32vector. I did not test the code below, but something like
it might work:


(define-foreign-type color* s32vector
   (lambda (a) (assert (= 4 (s32vector-length a))) a))

(define color s32vector)
(define (rgb-to-hsl color)
   (let ((return (make-s32vector 3)))
     ((foreign-lambda*
       void ((color input) (color output))
       "rgb_to_hsl((struct color*)input, &output[0], &output[1],
&output[2])") color return)
     return))

(rgb-to-hsl (color 0 255 0 0)) ;; <-- should in theory return a
                                ;; 3-element s32vector with your hue,
                                ;; sat and lum.


Note the cast from s32vector (int*) to struct color*.

Cheers,
K.

Thanks for this extensive example. I've managed it exactly this way :) with the usage of srfi-4.




On Sat, Sep 7, 2013 at 4:33 PM, John Cowan <address@hidden
<mailto:address@hidden>> wrote:

    Kristian Lein-Mathisen scripsit:

     > Because size of an "int" can generally be either 32 or 64-bit
    depending
     > on your architecture, the srfi-4 vectors are possible better suited
     > for floats and doubles where the sizes are all set. It's worth
    knowing
     > about them though.

    Actually, there are essentially no architectures on which "int" is
    anything but 32 bits: unless you have an ancient Cray running Unicos
    (modern ones run Linux), a (now-defunct) HAL Systems port of Solaris,
    or an IBM PC/AT, you don't have to worry about that.  What does vary is
    "long", which is 32 bits on 32-bit systems and Win64, and 64 bits on
    non-Windows 64-bit systems.

    --
    John Cowan http://ccil.org/~cowan address@hidden <mailto:address@hidden>
    SAXParserFactory [is] a hideous, evil monstrosity of a class that should
    be hung, shot, beheaded, drawn and quartered, burned at the stake,
    buried in unconsecrated ground, dug up, cremated, and the ashes tossed
    in the Tiber while the complete cast of Wicked sings "Ding dong, the
    witch is dead."  --Elliotte Rusty Harold on xml-dev






reply via email to

[Prev in Thread] Current Thread [Next in Thread]