16-bit colour representation

Steve Holden steve at holdenweb.com
Wed Jul 25 18:18:22 CEST 2007


Diez B. Roggisch wrote:
> 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
> 
> 
Alternatively, using 5-bit colors you would use

b = v & 0x1f
g = (v >> 5) & 0x1f
r = (v >> 10) & 0x1f

regards
  Steve

-- 
Steve Holden        +1 571 484 6266   +1 800 494 3119
Holden Web LLC/Ltd           http://www.holdenweb.com
Skype: holdenweb      http://del.icio.us/steve.holden
--------------- Asciimercial ------------------
Get on the web: Blog, lens and tag the Internet
Many services currently offer free registration
----------- Thank You for Reading -------------




More information about the Python-list mailing list