- Source: Ordered dithering
Ordered dithering is any image dithering algorithm which uses a pre-set threshold map tiled across an image. It is commonly used to display a continuous image on a display of smaller color depth. For example, Microsoft Windows uses it in 16-color graphics modes. The algorithm is characterized by noticeable crosshatch patterns in the result.
Threshold map
The algorithm reduces the number of colors by applying a threshold map M to the pixels displayed, causing some pixels to change color, depending on the distance of the original color from the available color entries in the reduced palette.
The first threshold maps were designed by hand to minimise the perceptual difference between a grayscale image and its two-bit quantisation for up to a 4x4 matrix.
An optimal threshold matrix is one that for any possible quantisation of color has the minimum possible texture so that the greatest impression of the underlying feature comes from the image being quantised. It can be proven that for matrices whose side length is a power of two there is an optimal threshold matrix. The map may be rotated or mirrored without affecting the effectiveness of the algorithm.
This threshold map (for sides with length as power of two) is also known as a Bayer matrix or, when unscaled, an index matrix. For threshold maps whose dimensions are a power of two, the map can be generated recursively via:
M
2
n
=
1
(
2
n
)
2
[
(
2
n
)
2
M
n
(
2
n
)
2
M
n
+
2
J
n
(
2
n
)
2
M
n
+
3
J
n
(
2
n
)
2
M
n
+
J
n
]
=
J
2
⊗
M
n
+
1
n
2
M
2
⊗
J
n
,
{\displaystyle \mathbf {M} _{2n}={\frac {1}{(2n)^{2}}}{\begin{bmatrix}(2n)^{2}\mathbf {M} _{n}&(2n)^{2}\mathbf {M} _{n}+2\mathbf {J} _{n}\\(2n)^{2}\mathbf {M} _{n}+3\mathbf {J} _{n}&(2n)^{2}\mathbf {M} _{n}+\mathbf {J} _{n}\end{bmatrix}}=\mathbf {J} _{2}\otimes \mathbf {M} _{n}+{\frac {1}{n^{2}}}\mathbf {M} _{2}\otimes \mathbf {J} _{n},}
where
J
n
{\displaystyle \mathbf {J} _{n}}
are
n
×
n
{\displaystyle n\times n}
matrices of ones and
⊗
{\displaystyle \otimes }
is the Kronecker product.
While the metric for texture that Bayer proposed could be used find optimal matrices for sizes that are not a power of two, such matrices are uncommon as no simple formula for finding them exists, and relatively small matrix sizes frequently give excellent practical results (especially when combined with other modifications to the dithering algorithm).
This function can also be expressed using only bit arithmetic:
M(i, j) = bit_reverse(bit_interleave(bitwise_xor(i, j), i)) / n ^ 2
Pre-calculated threshold maps
Rather than storing the threshold map as a matrix of
n
{\displaystyle n}
×
n
{\displaystyle n}
integers from 0 to
n
2
{\displaystyle n^{2}}
, depending on the exact hardware used to perform the dithering, it may be beneficial to pre-calculate the thresholds of the map into a floating point format, rather than the traditional integer matrix format shown above.
For this, the following formula can be used:
Mpre(i,j) = Mint(i,j) / n^2
This generates a standard threshold matrix.
for the 2×2 map:
this creates the pre-calculated map:
Additionally, normalizing the values to average out their sum to 0 (as done in the dithering algorithm shown below) can be done during pre-processing as well by subtracting 1⁄2 of the largest value from every value:
Mpre(i,j) = Mint(i,j) / n^2 – 0.5 * maxValue
creating the pre-calculated map:
Algorithm
The ordered dithering algorithm renders the image normally, but for each pixel, it offsets its color value with a corresponding value from the threshold map according to its location, causing the pixel's value to be quantized to a different color if it exceeds the threshold.
For most dithering purposes, it is sufficient to simply add the threshold value to every pixel (without performing normalization by subtracting 1⁄2), or equivalently, to compare the pixel's value to the threshold: if the brightness value of a pixel is less than the number in the corresponding cell of the matrix, plot that pixel black, otherwise, plot it white. This lack of normalization slightly increases the average brightness of the image, and causes almost-white pixels to not be dithered. This is not a problem when using a gray scale palette (or any palette where the relative color distances are (nearly) constant), and it is often even desired, since the human eye perceives differences in darker colors more accurately than lighter ones, however, it produces incorrect results especially when using a small or arbitrary palette, so proper normalization should be preferred.
In other words, the algorithm performs the following transformation on each color c of every pixel:
c
′
=
n
e
a
r
e
s
t
_
p
a
l
e
t
t
e
_
c
o
l
o
r
(
c
+
r
×
(
M
(
x
mod
n
,
y
mod
n
)
−
1
/
2
)
)
{\displaystyle c'=\mathrm {nearest\_palette\_color} {\mathopen {}}\left(c+r\times \left(M(x{\bmod {n}},y{\bmod {n}})-1/2\right){\mathclose {}}\right)}
where M(i, j) is the threshold map on the i-th row and j-th column, c′ is the transformed color, and r is the amount of spread in color space. Assuming an RGB palette with 23N evenly distanced colors where each color (a triple of red, green and blue values) is represented by an octet from 0 to 255, one would typically choose
r
≈
255
N
{\textstyle r\approx {\frac {255}{N}}}
. (1⁄2 is again the normalizing term.)
Because the algorithm operates on single pixels and has no conditional statements, it is very fast and suitable for real-time transformations. Additionally, because the location of the dithering patterns always stays the same relative to the display frame, it is less prone to jitter than error-diffusion methods, making it suitable for animations. Because the patterns are more repetitive than error-diffusion method, an image with ordered dithering compresses better. Ordered dithering is more suitable for line-art graphics as it will result in straighter lines and fewer anomalies.
The values read from the threshold map should preferably scale into the same range as the minimal difference between distinct colors in the target palette. Equivalently, the size of the map selected should be equal to or larger than the ratio of source colors to target colors. For example, when quantizing a 24 bpp image to 15 bpp (256 colors per channel to 32 colors per channel), the smallest map one would choose would be 4×2, for the ratio of 8 (256:32). This allows expressing each distinct tone of the input with different dithering patterns.
= A variable palette: pattern dithering
=Non-Bayer approaches
The above thresholding matrix approach describes the Bayer family of ordered dithering algorithms. A number of other algorithms are also known; they generally involve changes in the threshold matrix, equivalent to the "noise" in general descriptions of dithering.
= Halftone
=Halftone dithering performs a form of clustered dithering, creating a look similar to halftone patterns, using a specially crafted matrix.
= Void and cluster
=The Void and cluster algorithm uses a pre-generated blue noise as the matrix for the dithering process. The blue noise matrix keeps the Bayer's good high frequency content, but with a more uniform coverage of all the frequencies involved shows a much lower amount of patterning.
The "voids-and-cluster" method gets its name from the matrix generation procedure, where a black image with randomly initialized white pixels is gaussian-blurred to find the brightest and darkest parts, corresponding to voids and clusters. After a few swaps have evenly distributed the bright and dark parts, the pixels are numbered by importance. It takes significant computational resources to generate the blue noise matrix: on a modern computer a 64×64 matrix requires a couple seconds using the original algorithm.
This algorithm can be extended to make animated dither masks which also consider the axis of time. This is done by running the algorithm in three dimensions and using a kernel which is a product of a two-dimensional gaussian kernel on the XY plane, and a one-dimensional Gaussian kernel on the Z axis.
References
Ordered Dithering (Graphics course project, Visgraf lab, Brazil)
Dithering algorithms (Lee Daniel Crocker, Paul Boulay and Mike Morra)
Further reading
External links
Matlab implementation of various dithering methods
anim8gdx, Java implementation of various (mostly ordered) dithering methods
Kata Kunci Pencarian:
- Ordered dithering
- Dither
- Bayer filter
- Error diffusion
- Halftone
- List of algorithms
- PaintShop Pro
- Box-drawing characters
- ZX Spectrum graphic modes
- Katie Britt