Microsoft DirectX 9.0

Working with 16-bit RGB

Two formats are defined for 16-bit uncompressed RGB:

RGB 565

To extract the color components from an RGB 565 image, treat each pixel as a WORD type and use the following bit masks:

WORD red_mask = 0xF800;
WORD green_mask = 0x7E0;
WORD blue_mask = 0x1F;

Get the color components from a pixel as follows:

BYTE red_value = (pixel & red_mask) >> 11;
BYTE green_value = (pixel & green_mask) >> 5;
BYTE blue_value = (pixel & blue_mask);

Reverse this process to create an RGB 565 pixel.

WORD pixel565 = (red << 11) | (green << 5) | blue;

This code assumes the color values have already been truncated to the correct number of bits. The red, green, and blue values are packed into a single WORD value.

RGB 555

Working with RGB 555 is essentially the same as RGB 565, except the bit masks and bit shift operations are different. To get the color components from an RGB 555 pixel, do the following:

WORD red_mask = 0x7C00;
WORD green_mask = 0x3E0;
WORD blue_mask = 0x1F;

BYTE red_value = (pixel & red_mask) >> 10;
BYTE green_value = (pixel & green_mask) >> 5;
BYTE blue_value = (pixel & blue_mask);

To pack the red, green, and blue color values into an RGB 555 pixel, do the following:

WORD pixel565 = (red << 10) | (green << 5) | blue;

See Also