1-bit dither

1-bit dither

Overview

1-bit dithering, such as Atkinson Dither, can be used as an aesthetic.

The monolith janet function monolith/gfx-dither takes a region in the framebuffer and applies an Atkinson Dither filter using two arbitrary RGB colors.

The arguments are x, y, width, height, RGB color 1, and RGB color 2.

Example Snippet

The following janet function colorbar creates a 1-bit dither region of size 16x64 at position (32,0) with the colors blood red (102, 0, 0), and NCS red (192, 2, 51).

<<colorbar>>=
(defn colorbar []
  (monolith/gfx-dither
   32 0 16 64
   # Blood Red
   102 0 0
   # NCS red
   192 2 51))

A full example

The following example uses the previous colorbar function on a texture created using fractional brownian motion. The resulting output is a 128x4 picture zoomed 3x that looks like this:

<<dithertest.janet>>=
(defn colorlerp [c1 c2 a]
  (array
   (+ (* a (c2 0)) (* (- 1 a) (c1 0)))
   (+ (* a (c2 1)) (* (- 1 a) (c1 1)))
   (+ (* a (c2 2)) (* (- 1 a) (c1 2)))))

(def noctaves 5)
(def white @(255 255 255))
(def black @(0 0 0))

(defn color [x y c]
    (monolith/gfx-pixel-set
     x y
     (math/floor (c 0))
     (math/floor (c 1))
     (math/floor (c 2)) 255))

(defn fbm []
  (var height (monolith/gfx-height))
  (var width (monolith/gfx-width))
  (for ypos 0 height
    (for xpos 0 width
      (var x (/ xpos width))
      (var y (/ ypos height))
      (set x (* x (/ width height)))

      (set y (* y 4))
      (set x (* x 4))

      (var ut 0)

      (var qx (monolith/fbm (+ x ut) (+ y ut) noctaves))
      (var qy (monolith/fbm (+ x 1) (+ y 1) noctaves))

      (var rx
           (monolith/fbm
            (+ x qx 1.7 (* 0.15 ut))
            (+ y qy 9.2 (* 0.15 ut))
            noctaves))
      (var ry
           (monolith/fbm
            (+ x qx 8.3 (* 0.126 ut))
            (+ y qy 2.8 (* 0.126 ut))
            noctaves))

      (var amp (monolith/fbm (+ x rx) (+ y ry) noctaves))

      (if (> amp 1) (set amp 1))
      (if (< amp 0) (set amp 0))

      (color xpos ypos (colorlerp black white amp)))))

# colorbar function goes here

<<colorbar>>

(monolith/gfx-fb-init)
(monolith/gfx-setsize 128 64)
(monolith/gfx-zoom 3)
(fbm)
(colorbar)
(monolith/gfx-write-png "dithertest.png")

Algorithm and C implementation

More information on the implemtnation can be found at dither.c, with the main algorithm outlined at 4. The Dither Algorithm.