Although I should really be working on adding the dithering algorithms into Gif Animator, I thought it would be useful to expand the repertoire of algorithms available for use with it and the other projects I'm working on.
I decided to re-factor the class I created for the Burkes algorithm to make it suitable for adding other error diffusion filters with a minimal amount of code.
First, I added a new abstract class,
The constructor of this class requires you to pass in the matrix
used to disperse the error to neighbouring pixels, the divisor,
and whether or not to use bit shifting. The reason for the last
parameter is the Floyd-Steinberg and Burkes algorithms
covered in my earlier posts had divisors that were powers of
two, and so could therefore be bit shifted for faster division.
Not all algorithms use a power of two divisor though and so we
need to be flexible.
The constructor then stores the matrix, and pre-calculates a
couple of other values to avoid repeating these each time the
Diffuse method is called.
The actual dithering implementation is unchanged from original matrix handling code, with the exception of supporting bit shifting or integer division, and not having to work out the current pixel in the matrix, width or height.
BurkesDithering class now looks like this
No code, just the matrix and the bit shifted divisor of 5, which will divide each result by 32. Nice!
As well as opening the door to allowing a user to define a custom dither matrix, it also makes it trivial to implement a number of other common error diffusion matrixes. The GitHub Repository now offers the following algorithms
- Jarvis, Judice & Ninke
- Two Row Sierra
- Sierra Light
Which is a fairly nice array.
There's a rather old (in internet terms anyway!) text file
floating around named DHALF.TXT (based in turn on an even
older document named
DITHER.TXT) that has a ton of useful
information on dithering, and with the exception of the
Altkinson algorithm (I took that from here is where I have
pulled all the error weights and divisors from.
One of the sections in this document dealt with random dithering. Although I didn't think I would ever use it myself, I thought I'd add an implementation of it anyway to see what it's like.
Unlike the error diffusion methods, random dithering affects only a single pixel at a time, and does not consider or modify its neighbours. You also have a modicum of control over it too, if you can control the initial seed of the random number generator.
DHALF.TXT text sums it up succinctly: For each dot in our
grayscale image, we generate a random number in the range 0 -
255: if the random number is greater than the image value at
that dot, the display device plots the dot white; otherwise, it
plots it black. That's it.
And here's our implementation (ignoring the fact that it isn't
error diffusion and all of a sudden our
interface is named wrong!)
(Although I reversed black and white from the original description as otherwise it looked completely wrong)
I was surprised to see it actually doesn't look that bad.
I've almost got a full house of useful dithering algorithms now. About the only thing left for me to do is to implement a ordered Bayer dithering as I really like the look of this type, and reminds me of games and computers of yesteryear. So there's still at least one more article to follow in this series!
The updated source code with all these algorithms is available from the GitHub repository.
- 2015-06-13 - First published
- 2020-11-21 - Updated formatting
- Even more algorithms for dithering images using C#
- Dithering an image using the Burkes algorithm in C#
- Dithering an image using the Floyd‑Steinberg algorithm in C#
- An introduction to dithering images