This section of the archives stores flipcode's complete Developer Toolbox collection,
featuring a variety of mini-articles and source code contributions from our readers.

Image Dither
Submitted by

This code snippet is code to convert any format of image to another format, and
dithers the image while converting using a very simplified error diffusion
algorithm inspired by Floyd Steinberg's.

I'm getting about 185ms times for conversion and dithering of 32bit 800x600
image to 16bit A1R5G5B5 images on my PII 450Mhz, not too great, but not too bad
either, feedback is welcome on ways you might think up to optimize the code.

I've attached two images showing the quality gain in 16bits. As you can see,
even though the algorithm is very simple, the quality increase of sprites and
textures can be pretty big nonetheless in 16-bits video modes.

for(int i = 0; i < Height; i++)
{
//Reset the error after every line because we don't want
//the error of the last pixel to be used on the first pixel
//of this line
RedError = GreenError = BlueError = AlphaError = 0;