Bayer Dithering

Written by Dominik Pantůček on 2026-12-03

graphics

Although we live in an age of "unlimited" number of colours on our screens, it turns out that some of them have rather limited colour-expression capabilities. Take e-paper devices for example. And that is when some rather old technologies like ordered dithering may come to play again.


Dithering is a process of placing pixels on the screen in a pattern that expresses larger colour gamut using only a smaller set of available colours. Depending on the intended usage there are two main classes of dithering algorithms available:

  1. Unordered
  2. Ordered

The unordered algorithms typically work by distributing the error coming from the quantization of the colour-space into a smaller set of distinct colours to the surrounding pixels. Of course that means that if something changes in one part of the image, it affects the representation of other parts of the image which are in the path of the error distribution. For static images this is not a problem, however once we take the possibility of animating the images, it quickly becomes clear that creating such noise is not a viable approach.

The other class of ordered dithering algorithms allows for animating parts of the image or even the whole image without producing such noise downstream. In this article we will introduce one such algorithm the Bayer Dithering threshold algorithm.

The idea is simple. We tile the screen using a fixed-size square matrices with coefficients setting given pixel on or off based on whether some higher-resolution colour value reaches given threshold value.

To start experimenting with this algorithm, we need to prepare the matrix first. Today we will implement an algorithm that produces a bayer matrix as a fxvector? in Racket, of course.

Our intermediate goal is to write a procedure that returns the threshold value at given coordinates of Bayer matrix of specified size:

(define (bayer-matrix-cell i j (n 2))
  (bit-reverse (bit-interleave i (bitwise-xor j i) n) (* 2 (log n 2))))

So apparently we only need to implement the bit-reverse and bit-interleave procedures. The former is rather straightforward:

(define (bit-reverse n (m 4))
  (let loop ((r 0)
             (n n)
             (m m))
    (if (zero? m)
        r
        (loop (bitwise-ior (arithmetic-shift r 1)
                           (bitwise-and n 1))
              (arithmetic-shift n -1)
              (sub1 m)))))

It reverses the bits in m-bit number n and returns the result.

How about the latter procedure? A bit more complex, however as we will only calculate this once and store it, we are more interested in correctness than speed and therefore it is again pretty straightforward:

(define (bits->list n)
  (string->list (number->string n 2)))

(define (pad-bit-list l n)
  (let loop ((l l))
    (if (= (length l) n)
        l
        (loop (cons 0 l)))))

(define (bit-interleave a b (n 4))
  (define al0 (bits->list a))
  (define bl0 (bits->list b))
  (define al (pad-bit-list al0 n))
  (define bl (pad-bit-list bl0 n))
  (define l (apply append (map list al bl)))
  (let loop ((r 0)
             (l l))
    (if (null? l)
        r
        (loop (bitwise-ior (arithmetic-shift r 1)
                           (if (eq? (car l) #\1) 1 0))
              (cdr l)))))

A bit of an overkill? Yes, but we do not need to worry about it anymore.

So what about gluing it together and producing the matrix itself? Again, it is not hard at all:

(define (make-bayer-matrix (n 4))
  (for*/fxvector #:length (* n n)
               ((j (in-range n))
                (i (in-range n)))
    (bayer-matrix-cell j i n)))

Just remember the side n should alwyas be a power of two. So let's print us some Bayer matrices using our favourite Unicode Tables package:

(require uni-table)
(define (printbm bm)
  (define s (sqrt (vector-length bm)))
  (print-uni-table
   #:col-align '((right))
   (for/list ((j (in-range s)))
     (for/list ((i (in-range s)))
       (format " ~a" (vector-ref bm (+ (* j s) i)))))))

And let's print them in REPL:

bayer.rkt> (printbm (make-bayer-matrix 2))
 0 2
 3 1
bayer.rkt> (printbm (make-bayer-matrix 4))
  0  8  2 10
 12  4 14  6
  3 11  1  9
 15  7 13  5
bayer.rkt> (printbm (make-bayer-matrix 8))
  0 32  8 40  2 34 10 42
 48 16 56 24 50 18 58 26
 12 44  4 36 14 46  6 38
 60 28 52 20 62 30 54 22
  3 35 11 43  1 33  9 41
 51 19 59 27 49 17 57 25
 15 47  7 39 13 45  5 37
 63 31 55 23 61 29 53 21
bayer.rkt> (printbm (make-bayer-matrix 16))
   0 128  32 160   8 136  40 168   2 130  34 162  10 138  42 170
 192  64 224  96 200  72 232 104 194  66 226  98 202  74 234 106
  48 176  16 144  56 184  24 152  50 178  18 146  58 186  26 154
 240 112 208  80 248 120 216  88 242 114 210  82 250 122 218  90
  12 140  44 172   4 132  36 164  14 142  46 174   6 134  38 166
 204  76 236 108 196  68 228 100 206  78 238 110 198  70 230 102
  60 188  28 156  52 180  20 148  62 190  30 158  54 182  22 150
 252 124 220  92 244 116 212  84 254 126 222  94 246 118 214  86
   3 131  35 163  11 139  43 171   1 129  33 161   9 137  41 169
 195  67 227  99 203  75 235 107 193  65 225  97 201  73 233 105
  51 179  19 147  59 187  27 155  49 177  17 145  57 185  25 153
 243 115 211  83 251 123 219  91 241 113 209  81 249 121 217  89
  15 143  47 175   7 135  39 167  13 141  45 173   5 133  37 165
 207  79 239 111 199  71 231 103 205  77 237 109 197  69 229 101
  63 191  31 159  55 183  23 151  61 189  29 157  53 181  21 149
 255 127 223  95 247 119 215  87 253 125 221  93 245 117 213  85
bayer.rkt>

Hope you liked this little introduction into ordered dithering matrices and see ya next time when we will use them for something interesting!