# 16-bit colour representation

John Machin sjmachin at lexicon.net
Wed Jul 25 15:04:23 CEST 2007

```On Jul 25, 9:53 pm, beertje <bjorn... at gmail.com> wrote:
> This has me a bit stumped...
>
> I'm trying to extract pictures from a file. So far I'm successfully
> retrieved the header and what I think is the colour for each pixel.
> Here's the description:
>
> """
> 3) The picture data format:
> The color information is 15 bit data stored in 16 bit. This means the
> most
> significant bit is unused. The data is stored line for line in little
> endian Intel format starting with top left edge to bottom right edge.
> This
> is normally called chunky format.
>
> Bit  0.. 4      blue value
> Bit  5.. 9      green value
> Bit 10..14      red value
> """

Do yourself a favour -- read the next line in TFM. It says: "To get 8
bit RGB data, all these values must be shifted 3 bits to the left."

>
> So I've got a list of 16-bit numbers, but how to extract RGB info from
> those I'm a bit lost. I thought at first I should convert the decimal
> (say 23294) into a binary (say 0101101011111110) into something like
> this:
> blue: 01011
> green: 01011
> red: 11111

I think you've lost it somewhere;  23294 -> red 22, green 23, blue 30;
see below.

>
> But encountered two problems: First, I don't know what the best way is
> to do this conversion,

b = rgb & 31
g = (rgb >> 5) & 31
r = (rgb >> 10) & 31

IOW like you would in C; IOW isn't this whole question OT?

> but more importantly I don't see how every
> colour could possibly be represented like this. 65535 is presumably
> white, but converting this into chunks of 5 gives me a 31, 31, 31, a

It is a dark shade of grey in 8-bit RGB, but it's as white as the
driven snow in 5-bit RGB.

You need to scale it up. TFM indicates rgb8 = rgb5 << 3 -- i.e.
multiply by 8, but 31 * 8 is 248, not 255. You might want to try rgb8
= (rgb5 * 255 + 16) / 31 instead.

>
> I guess I'm on the wrong track completely?
>
> I'm a bit unsure about how to treat what the guide calls 'UWORD'...

TFM says "UWORD is unsigned 16 bit", so you treat it as an Unsigned
(16-bit) WORD, which is what you [should] have been doing.

>
> Here's the full guide:http://lists.kde.org/?l=kde-games-devel&m=105548792026813&w=1#2
> (16-bit PC cards)

```