471,354 Members | 1,721 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,354 software developers and data experts.

16-bit colour representation

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'...

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

Thanks alot.

Jul 25 '07 #1
5 1609
On Jul 25, 9:53 pm, beertje <bjorn...@gmail.comwrote:
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
dark shade of grey.
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-de...92026813&w=1#2
(16-bit PC cards)
Jul 25 '07 #2
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
Jul 25 '07 #3
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 -------------

Jul 25 '07 #4
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 -------------

Jul 25 '07 #5
>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
Darn. I knew I missed _something_...

Diez
Jul 25 '07 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Andreas Falck | last post: by
38 posts views Thread by Haines Brown | last post: by
12 posts views Thread by Olaf Baeyens | last post: by
23 posts views Thread by Steven T. Hatton | last post: by
1 post views Thread by David Bertoni | last post: by
7 posts views Thread by Jimmy Shaw | last post: by
15 posts views Thread by arnuld | last post: by
reply views Thread by Bill Wordsworth | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.