A pixel to be dithered is sent to the routine provided in this example. The values of the variables
RedValue, GreenValue, and BlueValue are generated by an application. In this example, the color
values are assumed to be in the range 0..255.
The given routine receives the color values and the X and Y window address (X
p
and Y
p
) of the pixel.
The X and Y address is used to access the dither tables. The values from the dither tables are added to
the color values. After the dither addition, the resultant color values are quantized to three bits of red and
green and two bits of blue. The quantized results are packed into an 8-bit unsigned char and then stored
in the frame buffer. In the process of sending the contents of the frame buffer to the CRT, a special
section in the hardware then converts the frame buffer's 8-bit data into 24-bit TrueColor data for display.
Here is a routine that can be used to dither the 24-bit TrueColor data.
unsigned char dither_pixel_for_CR(RedValue,GreenValue,BlueValue,Xp,Yp)
int RedValue, GreenValue, BlueValue, Xp, Yp;
{
static short dither_red[2][16] = {
{-16, 4, -1, 11,-14, 6, -3, 9,-15, 5, -2, 10,-13, 7, -4, 8},
{ 15, -5, 0,-12, 13, -7, 2,-10, 14, -6, 1,-11, 12, -8, 3, -9}};
static short dither_green[2][16] = {
{ 11,-15, 7, -3, 8,-14, 4, -2, 10,-16, 6, -4, 9,-13, 5, -1},
{-12, 14, -8, 2, -9, 13, -5, 1,-11, 15, -7, 3,-10, 12, -6, 0}};
static short dither_blue[2][16] = {
{ -3, 9,-13, 7, -1, 11,-15, 5, -4, 8,-14, 6, -2, 10,-16, 4},
{ 2,-10, 12, -8, 0,-12, 14, -6, 3, -9, 13, -7, 1,-11, 15, -5} };
int red, green, blue;
int x_dither_table, y_dither_table;
unsigned char pixel;
/* Determine the dither table entries to use based on the pixel address */
x_dither_table = Xp % 16; /* X Pixel Address MOD 16 */
y_dither_table = Yp % 2; /* Y Pixel Address MOD 2 */
/* Start with the initial values as supplied by the calling routine */
red = RedValue;
green = GreenValue;
blue = BlueValue;
/* Generate the red dither value */
red += dither_red[y_dither_table][x_dither_table];
/* Check for overflow or underflow on red value */
if (red > 0xff) red = 0xff;
if (red < 0x00) red = 0x00;
/* Generate the green dither value */
green += dither_green[y_dither_table][x_dither_table];
Graphics Administration Guide for HP-UX 10.20
Page 53