memory hog added by Corbin on Tue Oct 12 20:49:41 2021

(define (viewport width height)
  (let*
    ((aspect (/ width height))
     (iw (/ width))
     (ih (/ height))
     (dw (* 0.5 iw))
     (dh (* 0.5 ih)))
    (lambda (i)
      (let ((w (modulo i width)) (h (/ i width)))
        (cons (+ dw (* iw w)) (+ dh (* ih h)))))))

(define (finish-channel c)
  (inexact->exact (round (* 255 (max 0.0 (min 1.0 c))))))

(define ((draw-pixel program vp) i)
  (let*
    ((color (program (vp i)))
     (r (car color))
     (g (car (cdr color)))
     (b (cdr (cdr color))))
    (map finish-channel (list r g b))))

(define (count-to i) (if (eqv? 0 i) '() (cons (- i 1) (count-to (- i 1)))))

(define ((draw-png program width height))
  (let
    ((vp (viewport width height))
     (indices (reverse (count-to (* width height)))))
    (write-png (list->u8vector
                 (flatten (map (draw-pixel program vp) indices)))
               width height 3)))