;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;;

;;  CRAZY CIRCULAR AND CRYSTAL FILTER EXAMPLE

;;

;;  Download a medium sized quicktime trailer from apple

;;  Load it into quicktime-pro and export it at its current

;;  size but in TIFF format.  Use that file in gfx:load-movie.

;; 

;;  Impromptu does not currently support any format

;;  other than TIFF

;;

;;  A complete list of filters and filter parameters can

;;  be found in apples "Core Image Programming Guide".

;;

;;  NSNumbers are scheme real numbers

;;  CIColors are lists of (r g b)

;;  CIImage is an impromptu image

;;  CIVector is a scheme vector

;;

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;


(define canvas (gfx:make-canvas 600 600))

(define movie (gfx:load-movie "/tmp/myfilm.mov"))

(define circle-params '(("inputAngle" . 4.8)

                        ("inputCenter" . #(186 186))

                        ("inputRadius" . 50)))

(define crystal-params '(("inputCenter" . #(240 102))

                         ("inputRadius" . 14)))

(define bg-frame-length (* *second* 4.0))

(define fg-frame-length (* *second* 0.1))


(define bgloop

   (lambda (time)

      (let* ((image (gfx:get-movie-frame movie (* (random) 

                                             (gfx:get-movie-duration movie))

                                     *gfx:ciimage*))

             (bg (gfx:filter-image image "CICrystallize" crystal-params)))

         (gfx:draw-image time canvas bg 1 '(20 20 600 650))

         (set! time (+ bg-frame-length time))

         (callback (- time 1000) 'bgloop time))))


(define loop

   (lambda (time movie-time)

      (let* ((image (gfx:get-movie-frame movie movie-time *gfx:ciimage*))

             (filtered-image (gfx:filter-image image "CICircularWrap"

                                           circle-params)))

         (gfx:draw-image time canvas filtered-image 0.1 '(100 100))

         (set! time (+ time fg-frame-length))

         (if (< movie-time (- (gfx:get-movie-duration movie) 1.0))

             (callback (- time 4000) 'loop time (+ movie-time 0.1))

             (callback (- time 4000) 'loop time 0.0)))))


(bgloop (now) 0.0)

(loop (now) 0.0)