16-bit colour representation

Diez B. Roggisch deets at nospam.web.de
Wed Jul 25 09:04:55 EDT 2007


beertje 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
> """
> 
> 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
> 
> But encountered two problems: First, I don't know what the best way is
> to do this conversion, 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
> dark shade of grey.
> 
> I guess I'm on the wrong track completely?
> 
> I'm a bit unsure about how to treat what the guide calls 'UWORD'...

ANDing and SHIFTing are your friends here:

v = 0x0bcd
b = v & 0xf
v >>= 4
g = v & 0xf
v >>= 4
r = v & 0xf
print r, g, b


Diez



More information about the Python-list mailing list