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

;;

;; Live video + Movie video composited

;;

;; Turn your isight on and away you go :)

;;

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


(define *canvas* (gfx:make-canvas 640 400))

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

(define *lgth* (- (gfx:get-movie-duration *movie*) 1.0))

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

(define *transform* (objc:call "NSAffineTransform" "transform"))

(objc:call *transform* "scaleBy:" 2.1)

(define *ciat-params* (list (cons "inputTransform" *transform*)))


(gfx:start-live-video)


(define loop

   (lambda (time movie-time)

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

             (big-movie-image (gfx:filter-image movie-image "CIAffineTransform" *ciat-params*))

             (camera-image (gfx:get-live-frame))             

             (comp-params (list (cons "inputBackgroundImage" camera-image)))             

             (comp-image (gfx:filter-image big-movie-image "CIExclusionBlendMode" comp-params)))

         (gfx:draw-image time *canvas* comp-image 1.0 '(0 0 640 400) '(0 0 640 390))

         (callback (- time 2000) 'loop (+ time *frame-length*) (+ movie-time 0.1)))))


(callback (+ (now) (* *second* 2)) 'loop (now) 0.0)