How RGB data is encoded into 16 bits?

Um… a while back I asked a question on using 16 bit surfaces. I am still having
trouble understanding how RGB data is stuffed into 16 bits.

I wrote some code to give me the binary equivalents of RGB values, unfortunately I
have been unable to find out how the RGB data is stored in 16 bits.

For example, here is (255, 0, 255)

Interger Value: 64575

11111100 00111111

Anyhow, I got the R only , G only , and B, only versions of this thingy and this is
what I got:

(255, 0, 0)

Integer Value: 63488

11111000 00000000

(0, 0, 0)

Value: 0

00000000 00000000

(0, 0, 255)

Value: 31

00000000 00011111

Last time I check 16 wasn’t divisible by 3.

Rloss: 3 Gloss: 2 Bloss: 3
Rshift: 11 Gshift: 5 Bshift: 0

What is Rloss? What is Gloss? Bloss? What roles do the Rshift, Gshift and Bshift
play?

I know the RGB data must be sifted out with bitwise / bitmask manipulations, but
HOW IS IT ENCODED IN? BITWISE!

Paul Lowe
xpaull at ultraviolet.org

Oh, anyone know a good quick efficient way of fading in/out in 16 bits?

David Vandewalle wrote:

Rloss: 3 Gloss: 2 Bloss: 3
Rshift: 11 Gshift: 5 Bshift: 0

What is Rloss? What is Gloss? Bloss? What roles do the Rshift, Gshift and
Bshift
play?

I assumed that this means that red is only packed in 5 bits (3 lost), green is
packed into 6 bits (2 lost) and Blue into 5 bits (again, 3 lost).

Um…If 3 bits are lost…does that affect the color accuracy? I don’t get it!
How can you accurately store a value of 0-255 with bits missing?

Ok ok! I admit it! I’m not really big on bitwise manipulations! (ah…that feels
better)
Can this be explained?

Possibly a bitwise diagram?

Gmasks, Rshifts, and Blosses?

(I’m trying to break away from using 8 bit surfaces for over a year and a half,
bear with me)

I’d rather understand this stuff that just copy-paste from SDL_blit.h…

Anyone?

Sorry,

Paul Lowe
xpaull at ultraviolet.org> On 14-Oct-98 Paul Lowe wrote:

/dev


// David E. Vandewalle |Weinberg’s Law: If builders built
// vandewal at skyblue.com | buildings the way programmers wrote
// david.e.vandewalle at lmco.com | programs, then thefirst woodpecker that
// | came along would destroy civilization.

Rloss: 3 Gloss: 2 Bloss: 3
Rshift: 11 Gshift: 5 Bshift: 0

What is Rloss? What is Gloss? Bloss? What roles do the Rshift, Gshift and
Bshift
play?

I assumed that this means that red is only packed in 5 bits (3 lost), green is
packed into 6 bits (2 lost) and Blue into 5 bits (again, 3 lost).

/devOn 14-Oct-98 Paul Lowe wrote:


// David E. Vandewalle |Weinberg’s Law: If builders built
// vandewal at skyblue.com | buildings the way programmers wrote
// david.e.vandewalle at lmco.com | programs, then thefirst woodpecker that
// | came along would destroy civilization.

Rloss: 3 Gloss: 2 Bloss: 3
Rshift: 11 Gshift: 5 Bshift: 0

I assumed that this means that red is only packed in 5 bits (3 lost), green is
packed into 6 bits (2 lost) and Blue into 5 bits (again, 3 lost).

That’s correct. For the answer to the 100 million dollar question, you
should look in SDL_blit.h:

/* Load pixel of the specified format from a buffer and get its R-G-B values */
#define RGB_FROM_PIXEL(pixel, fmt, r, g, b)
{
r = (((pixel&fmt->Rmask)>>fmt->Rshift)<Rloss);
g = (((pixel&fmt->Gmask)>>fmt->Gshift)<Gloss);
b = (((pixel&fmt->Bmask)>>fmt->Bshift)<Bloss);
}

/* Assemble R-G-B values into a specified pixel format and store them */
#define PIXEL_FROM_RGB(pixel, fmt, r, g, b)
{
pixel = ((r>>fmt->Rloss)<Rshift)|
((g>>fmt->Gloss)<Gshift)|
((b>>fmt->Bloss)<Bshift);
}

See ya!
-Sam Lantinga (slouken at devolution.com)–
Author of Simple DirectMedia Layer -
http://www.devolution.com/~slouken/SDL/

Um…If 3 bits are lost…does that affect the color accuracy? I don’t get it!
How can you accurately store a value of 0-255 with bits missing?

You can’t. You do lose some color accuracy, but you lose the least
significant bits. Imagine a color value 255, or 0xFF
Let’s say the mask is 0xF8, or 11111000 in binary, then the lost bits would
be the three lower bits, and the color value would be packed from 8 bits into
5 bits. Let’s apply the mask to the value: 0xFF & 0xF8 = 0xF8
Now we use the shift to pack the value from 8 bits to 5 bits:
0xF8 >> 3 = 0x1F

This process is applied to each color value, R, G, B, and then the final
product is packed via bitwise operators into a 16-bit value, as you see
in the cut-paste macros I posted earlier.

Does that make more sense?

Possibly a bitwise diagram?

Gmasks, Rshifts, and Blosses?

In our example above,
mask = 0xF8
loss = 3
shift depends on the position of the color in the final pixel.

(I’m trying to break away from using 8 bit surfaces for over a year and a half,
bear with me)

Think about it a bit. A good understanding of bitwise operators is very
helpful. … about 3 years ago, I swore I would never use bit-operators.
Heheh.

I’d rather understand this stuff that just copy-paste from SDL_blit.h…

Good for you. :slight_smile:

See ya!
-Sam Lantinga (slouken at devolution.com)–
Author of Simple DirectMedia Layer -
http://www.devolution.com/~slouken/SDL/

Paul Lowe wrote:

Um…If 3 bits are lost…does that affect the color accuracy? I don’t get
it! How can you accurately store a value of 0-255 with bits missing?

Actually, you do get it. You cannot accurately store a value ranging
from 0 to 255 in 5 bits. That’s why it doesn’t look as good as 24 bpp!

Some video cards have a 15 bpp mode that has 5 bits for everything, but
I’ve been told the green part is given the extra bit because the human
eye perceives differences in green better than the rest (or something to
that effect), so having the extra bit for the green does a big
difference (doubles the number of shades of green possible).

Simply, we trash the least significant bits to fit all three color
values in 16 bits.–
Pierre Phaneuf
Web: http://seventh.ml.org/~pp/
finger: pp at seventh.ml.org