PageRenderTime 14ms CodeModel.GetById 10ms app.highlight 2ms RepoModel.GetById 1ms app.codeStats 0ms

/examples/dynamic_linking.scm

http://github.com/digego/extempore
Lisp | 58 lines | 13 code | 6 blank | 39 comment | 0 complexity | 254fc82233bbde2dca8c98fe8d4bba31 MD5 | raw file
 1;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
 2;;
 3;; A short demonstration of Extempore's ability to 
 4;; link with dynamic libraries at runtime!
 5;;
 6;; The importance of this example is to demonstrate
 7;; Extempore's ability to bind to arbitrary dynamic
 8;; libraries at runtime.  In this case OpenCV.
 9;;
10;; There are some limitations on this - for example
11;; at present only C libraries (not objc or C++)
12;; can be dynamically bound at runtime and understood
13;; by the extempore compiler.  There are also some
14;; limitations imposed by restrictions in the
15;; extempore compiler - particularly function 
16;; pointers (callbacks) - however, these are high on 
17;; the must-fix list.
18;; 
19;; Of course there are ways to bind C++ and ObjC 
20;; libraries for static use.  However, this method
21;; of dynamic binding at runtime presents a very easy 
22;; and flexible mechanism for language extension
23;; on-the-fly, and certainly provides a way to
24;; rapidly develop library extensions.  This nice
25;; thing is that this extension can happen within
26;; the Extempore runtime itself - rather than requiring
27;; static compilation at the project level.
28;; 
29;; This little bit of code packs a pretty big punch
30;; 1) Opens the OpenCV HighGUI dynamic library
31;; 2) Binds 4 function definitions that we will use
32;; 3) Compiles text-opencv using these 4 functions
33;; 4) Runs the code translating the supplied image
34
35
36;; load highgui dynamic library
37(define highguilib (sys:open-dylib "libhighgui.so.2.1"))
38                               
39;; bind the symbols we need so that the
40;; extempore compiler can find (and understand) them
41(bind-lib highguilib cvLoadImage [i8*,i8*,i32]*)
42(bind-lib highguilib cvCloneImage [i8*,i8*]*)
43(bind-lib highguilib cvCanny [void,i8*,i8*,double,double,i32]*)
44(bind-lib highguilib cvSaveImage [i32,i8*,i8*]*)
45
46;; load image and apply canny edge detection
47(definec test-opencv
48   (lambda (path-to-in-img path-to-out-img)
49     (let* ((imgin (cvLoadImage path-to-in-img 0))
50            (imgout (cvCloneImage imgin)))
51       (cvCanny imgin imgout 400.0 100.0 3)
52       (cvSaveImage path-to-out-img imgout))))
53
54;; GO!
55(test-opencv "/tmp/my-image.jpg" "/tmp/my-new-image.jpg")
56
57;; We're done - close dylib
58(sys:close-dylib highguilib)