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

;;

;; Paint using coreimage filters

;;

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


(define canvas (gfx:make-canvas 640 480))

(gfx:clear-canvas (now) canvas '(0 0 0 .1))


(define accum (objc:call "CIImageAccumulator" "imageAccumulatorWithExtent:format:" (list 0 0 640 480) 27)) 

(define filt (gfx:make-filter "CIRadialGradient"))

(define comp (gfx:make-filter "CISourceOverCompositing"))

(gfx:set-filter-param filt "inputCenter" (vector 320 240))

(gfx:set-filter-param filt "inputColor0" (list 1.0 0.0 0.0 0.0))

(gfx:set-filter-param filt "inputColor1" (list 0.0 0.0 0.0 0.05))

(gfx:set-filter-param filt "inputRadius0" 10)

(gfx:set-filter-param filt "inputRadius1" 30)

                            


(define crop (gfx:make-filter "CICrop"))

(gfx:set-filter-param crop "inputRectangle" (vector 0 0 640 480))

 

(io:register-mouse-events canvas)


(define paint

   (lambda (x y colour)

      (gfx:set-filter-param filt "inputColor0" colour)            

      (gfx:set-filter-param filt "inputCenter" (vector x y))      

      (define img (gfx:apply-filter filt))     

      (gfx:set-filter-param comp "inputBackgroundImage" (objc:call accum "image"))

      (define i2 (gfx:apply-filter comp img))

      (define i3 (gfx:apply-filter crop img))

      (objc:call accum "setImage:dirtyRect:" i2 (list 0 0 640 480))

      (gfx:draw-image (now) canvas (objc:call accum "image") 1)))


(define io:mouse-down

   (lambda (x y)

      (paint x y (list 1.0 0.0 0.0 0.5))))

                            

(define io:mouse-drag

   (lambda (x y)

      (paint x y (list 1.0 0.0 0.0 0.5))))


(print filt)