/examples/dynamic_linking.scm

http://github.com/digego/extempore · Scheme · 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. ;; load highgui dynamic library
  35. (define highguilib (sys:open-dylib "libhighgui.so.2.1"))
  36. ;; bind the symbols we need so that the
  37. ;; extempore compiler can find (and understand) them
  38. (bind-lib highguilib cvLoadImage [i8*,i8*,i32]*)
  39. (bind-lib highguilib cvCloneImage [i8*,i8*]*)
  40. (bind-lib highguilib cvCanny [void,i8*,i8*,double,double,i32]*)
  41. (bind-lib highguilib cvSaveImage [i32,i8*,i8*]*)
  42. ;; load image and apply canny edge detection
  43. (definec test-opencv
  44. (lambda (path-to-in-img path-to-out-img)
  45. (let* ((imgin (cvLoadImage path-to-in-img 0))
  46. (imgout (cvCloneImage imgin)))
  47. (cvCanny imgin imgout 400.0 100.0 3)
  48. (cvSaveImage path-to-out-img imgout))))
  49. ;; GO!
  50. (test-opencv "/tmp/my-image.jpg" "/tmp/my-new-image.jpg")
  51. ;; We're done - close dylib
  52. (sys:close-dylib highguilib)