Dither

Share your ZGE-development tips and techniques here!

Moderator: Moderators

Post Reply
User avatar
Kjell
Posts: 1876
Joined: Sat Feb 23, 2008 11:15 pm

Dither

Post by Kjell »

8)

A simple dithering approach for gradients.

Just to clarify - The reason for using floats instead of writing to this.Pixel directly is because a color uses only 8-bit ( opposed to a 32-bit float ) .. and since the whole point of dithering is to fake a higher bit depth, we don't want to lose any precision until we're done with our algorithm.

Code: Select all

float C, F, S, R, G, B;

C = 9/16-X/8; // Low contrast gradient ( less colors available then preferable )

R = C;
G = C;
B = C;

F = frac(C*255);
S = 1/255; // 1-bit offset ( on a 8-bit value )

if(frac(F) < 0.25)R += S;
if(frac(F) < 0.50)G += S;
if(frac(F) < 0.75)B += S;

this.Pixel.R = R;
this.Pixel.G = G;
this.Pixel.B = B;
You can shuffle the RGB thresholds around to determine your dominant hue.

Attached is a zip containing two 512x512 radial gradients, one without dithering, one using the described technique. Since the difference is subtle, also attached is a high contrast comparison of the two.

More complex algorithms to follow ..

K
Attachments
Dither.zip
(30.06 KiB) Downloaded 534 times
Compare.gif
Compare.gif (25.73 KiB) Viewed 4986 times
User avatar
Kjell
Posts: 1876
Joined: Sat Feb 23, 2008 11:15 pm

Post by Kjell »

:arrow:

For good measure, Floyd-Steinberg dithering. Bitmap represented as 3D Array ( 1=X, 2=Y, 3=RGB ) in the 0-255 range. Would have used the convolution component if it was more ambiguous ..

Code: Select all

for(int X=1; X<Array.SizeDim1-1; ++X)
{
  for(int Y=0; Y<Array.SizeDim2-1; ++Y)
  {
    for(int Z=0; Z<Array.SizeDim3; ++Z)
    {
      float O = Array[X,Y,0];
      float N = round(O);
      Array[X,Y,Z] = N;
      float E = O-N;
      Array[X+1,Y  ,Z] += 7/16*E;
      Array[X-1,Y+1,Z] += 3/16*E;
      Array[X  ,Y+1,Z] += 5/16*E;
      Array[X+1,Y+1,Z] += 1/16*E;
    }
  }
}
* Use with caution, this technique isn't very fast.
+ Remove the Z loop for monochrome images.

Attached is another high contrast comparison.

K
Attachments
Compare.gif
Compare.gif (19.26 KiB) Viewed 4979 times
Post Reply