based on this paper: http://www.efg2.com/Lab/Library/ImageProcessing/DHALF.TXT
// Ordered dithering aka Bayer matrix dithering
uniform sampler2D bgl_RenderedTexture;
float Scale = 1.0;
float find_closest(int x, int y, float c0)
{
int dither[8][8] = {
{ 0, 32, 8, 40, 2, 34, 10, 42}, /* 8x8 Bayer ordered dithering */
{48, 16, 56, 24, 50, 18, 58, 26}, /* pattern. Each input pixel */
{12, 44, 4, 36, 14, 46, 6, 38}, /* is scaled to the 0..63 range */
{60, 28, 52, 20, 62, 30, 54, 22}, /* before looking in this table */
{ 3, 35, 11, 43, 1, 33, 9, 41}, /* to determine the action. */
{51, 19, 59, 27, 49, 17, 57, 25},
{15, 47, 7, 39, 13, 45, 5, 37},
{63, 31, 55, 23, 61, 29, 53, 21} };
float limit = 0.0;
if(x < 8)
{
limit = (dither[x][y]+1)/64.0;
}
if(c0 < limit)
return 0.0;
return 1.0;
}
void main(void)
{
vec4 lum = vec4(0.299, 0.587, 0.114, 0);
float grayscale = dot(texture2D(bgl_RenderedTexture, gl_TexCoord[0].xy), lum);
vec3 rgb = texture2D(bgl_RenderedTexture, gl_TexCoord[0].xy).rgb;
vec2 xy = gl_FragCoord.xy * Scale;
int x = int(mod(xy.x, 8));
int y = int(mod(xy.y, 8));
vec3 finalRGB;
finalRGB.r = find_closest(x, y, rgb.r);
finalRGB.g = find_closest(x, y, rgb.g);
finalRGB.b = find_closest(x, y, rgb.b);
float final = find_closest(x, y, grayscale);
gl_FragColor = vec4(finalRGB, 1.0);
}
Comparison screenshots
original gradient
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhws4Yi2w-kM7vI_eYHCewItB3zmLZBA-azO0_Ml3P9vKZQFbOpsxvjPXkRRb4QHzYRmamN5nVAykrtGrOBYd98Ku638Dd9qq4-INLm_p3-iE4ANsjh4Rbd5-t6NBIHws0FURg30lCvCJ89/s400/original.jpg)
8x8 dither
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgHI1NuYQX8amX0P6vU1c1mcUx5jH2bEC662gmbPFleh79irERVdxXMoJbM4uTIS_alto8A_g2GYMiD9Iw6hDJgdNKYTbxhyphenhyphenoRJtrBRqgIVTbSd7xO0l_SuG2G0ZOq6TCfKReD5TQfRTiY2/s400/8x8.jpg)
4x4 dither
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEghrFkZ5FimSFtExeOx20LeJbJwI43YNdmoext7c3obA9FCllcERVdMACy4K1MCrbYSpTsQFSdAH6kzM8LhVdxTamHz_HkvFZjZcgJDh0Y7q-LxqCPTbtyw8uTZhuDYRv1Dw0nrTDc8xM5S/s400/4x4.jpg)
2x2 dither
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgWlJDCEfadPKC6ldeKhfvwEeGtfY7Bxui3938Uww1F0gqmNmzG8dayIsQUiNmvG2WydpAXOOY4heHcpyDjwCX3kAAlmGYw9WS7Fw9a2IU6tSoTPhVozLi4-G3ulXEcprutSHJCOCiHFcRp/s400/low.jpg)
No comments:
Post a Comment