Skip to content

Instantly share code, notes, and snippets.

@jbclements
Created March 30, 2013 00:32
Show Gist options
  • Save jbclements/5274674 to your computer and use it in GitHub Desktop.
Save jbclements/5274674 to your computer and use it in GitHub Desktop.
The 'blur' function for a rust-in-racket demo
;; the gaussian filter used in the racket blur.
;; boosted center value by 1/1000 to make sure that whites stay white.
(define filter '[[0.011 0.084 0.011]
[0.084 0.620 0.084]
[0.011 0.084 0.011]])
;; racket-blur: blur the image using the gaussian filter
;; number number list-of-bytes -> vector-of-bytes
(define (racket-blur width height data)
(define data-vec (list->vector data))
;; ij->offset : compute the offset of the pixel data within the buffer
(define (ij->offset i j)
(+ i (* j width)))
(define bytes-len (* width height))
(define new-bytes (make-vector bytes-len 0))
(define filter-x (length (car filter)))
(define filter-y (length filter))
(define offset-x (/ (sub1 filter-x) 2))
(define offset-y (/ (sub1 filter-y) 2))
;; compute the filtered byte array
(for* ([x width]
[y height])
(define new-val
(for*/fold ([sum 0.0])
([dx filter-x]
[dy filter-y])
(define sample-x (modulo (+ dx (- x offset-x)) width))
(define sample-y (modulo (+ dy (- y offset-y)) height))
(define sample-value (vector-ref data-vec (ij->offset sample-x sample-y)))
(define weight (list-ref (list-ref filter dy) dx))
(+ sum (* weight sample-value))))
(vector-set! new-bytes (ij->offset x y) new-val))
(vector->list new-bytes))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment