Dithering

Dithering is the process of defining gradations of gray or color shades in a 2-D graphic by varying the gray or color shades in the neighboring pixels. Dithering creates a halftone effect, but gives less resolution as shown in Figure 99 on the right.

NOTE Dithering currently works only for 2-D images. It will be expanded to work on all 2-D graphics in the near future.


The classes available for dithering derive from TGrafAttribute as shown in Figure 100. To add instances of these classes to a TGrafBundle, use the TGrafBundle::AdoptPrimaryDitherHint function. Dither hints use a token (a text string) to represent the attribute name. You pass the token to the TGrafBundle::OrphanPrimaryDitherHint to orphan the attribute or to return a constant pointer to the attribute.

The Token passed to TGrafAttribute lets you use the TGrafBundle OrphanGrafAttribute function; however, this approach is not as easy as the Adopt and OrphanPrimaryDitherHint functions mentioned above.

The dither classes derived from TDitherHint provide dithering algorithms that you can adopt to a TGrafBundle; however, dithering actually occurs only when the graphic device is capable of dithering (that is why it is called a dithering hint). The dithering classes are described below. Refer to a graphics book for detailed information on the various dithering algorithms.


Suppress Dither: Adopt a TSuppressDither instance when you want to turn dithering off for any 2-D graphic.

Noise Dither: Adopt a TNoiseDither instance when you want to add a random amount of noise to the rendered graphic. The noise dither changes the pixel value by a random value.

Error Diffusion Dither: Adopt a TErrorDiffusionDither instance when you want to diffuse an error value. An error value is the difference between the exact pixel value and the approximate pixel value rendered. This value is added to the values of the adjacent pixels, which spreads the error over the four adjacent pixels in the rendered graphic.

Matrix Dither: Matrix dithering uses a default matrix to achieve ordered dithering or halftone dithering. You can create your own matrix by deriving from THalftoneDither or TOrderedDither and overriding the CreateDitherMatrix function.

Halftone dithering provides resolution at the expense of intensity or color resolution. If the original graphic uses pixels with intensity values of 0 to 4, halftoning can be accomplished on a bilevel display (two intensity levels) where 0 equals black and 1 equals white by associating the orignal pixel with a 2-by-2 matrix of bilevel pixels. The final color of the original pixel is viewed on a bilevel display as the effective average of the four intensity values as produced by the array.

The following code fragment dithers the ellipse shown in Figure 101. The outline of the ellipse appears jagged when compared to the same ellipse without dithering.


    1  TColorPaint* grayFill = new TColorPaint( TRGBColor( .5, .5, .5 ) );
    2  TGrafBundle* ellipseBundle = new TGrafBundle( grayFill, TAttributeState::kFill );
    3  TEllipse anEllipse( TGRect( 50, 50, 80, 70 ), ellipseBundle );
    4  
    5  TGrafBundle* imageBundle = new TGrafBundle();
    6  TOrderedDither* orderedDither = new TOrderedDither(TRUE);
    7  imageBundle->AdoptPrimaryDitherHint( orderedDither );
    8  
    9  TA8R8G8B8Image anRGBImage( TGPoint( 0,0 ), 200, 200 );
    10  anRGBImage.AdoptBundle( imageBundle );
    11  TGrafPort* imagePort = anRGBImage.GetGrafPort();
    12  
    13  TFillBundle whiteFillBundle( TRGBColor( 1, 1, 1 ) );
    14  imagePort->Draw( TGRect( 0, 0, 200, 200 ), whiteFillBundle );
    15  
    16  anEllipse.Draw( *imagePort );
    17  anRGBImage.Draw( thePort );
Lines 1 through 3: Create a bundle with a gray fill and use it to create an ellipse graphic.

Lines 5 through 7: Create an image bundle and define it to use an ordered dither.

Lines 9 through 11: Create an image graphic, adopt the image bundle, and get the image port.

Lines 13 and 14: Create a white fill background, and draw it into the image port.

Lines 16 and 17: Draw the ellipse into the image port, and draw the image.


[Contents] [Previous] [Next]
Click the icon to mail questions or corrections about this material to Taligent personnel.
Copyright©1995 Taligent,Inc. All rights reserved.

Generated with WebMaker