It's often necessary to reduce the number of colours used in an image, usually in order to display it on a device which has colour limitations. For example, we may want to display a colour image on a black and white printer. If the printer can print shades of gray, we can simply convert the colour image to a grayscale image. If it can only print black or white, we have to use some method of halftoning where black pixels are scattered in a pattern which simulates shades of grey to the human eye. It's also possible to halftone to more than two shades of grey or to halftone each colour component separately in order to print colour images. Colour pictures in magazines are printed by halftone cyan, magenta, yellow and black separately.
The easiest method of halftoning is ordered dithering. In this method, pixels are turned on or off according to a pattern called a dither matrix. The dither matrix is a rectangular array of numbers, all different, which define the order in which pixels are turned on as the brightness of a pixel increases. The matrix is tiled over the image and the pixel at each point is turned on if its brightness is larger than the value in the matrix at that point, suitably scaled for the number of levels we're dithering to.
Here's the simplest 2x2 dither matrix:
0 2 3 1
And here's a 4x4 dither matrix:
0 14 3 13 11 5 8 6 12 2 15 1 7 9 4 10
This filter dithers an image to a specified number of colours. You supply the number of colors and the dither matrix. There are built-in matrices for common patterns such as 4x4 magic square and ordered dither etc.
- int matrix - The dither matrix
- int levels - The number of levels to dither to