473,322 Members | 1,409 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

How to convert 16 - bit 565 rgb value to 32 bit

Hi,

I am trying to convert a 16 bit rgb value to 32 bit, however the new
color generated is different from the 16 bit rgb data. Please let me
know the formula to convert an 16 bit rgb data to 32 bit rgb data.
Thanks!!

Oct 20 '06 #1
7 27954
an***********@gmail.com wrote:
I am trying to convert a 16 bit rgb value to 32 bit, however the new
color generated is different from the 16 bit rgb data. Please let me
know the formula to convert an 16 bit rgb data to 32 bit rgb data.
this is off-topic to comp.lang.c you need to identify what platform
(OS, graphic package etc) you are using and post to a news group where
that is relevant.

--
Nick keighley

Oct 20 '06 #2
Ico
an***********@gmail.com wrote:
I am trying to convert a 16 bit rgb value to 32 bit, however the new
color generated is different from the 16 bit rgb data. Please let me
know the formula to convert an 16 bit rgb data to 32 bit rgb data.
This depends completely on the color format used, there are too many
possibilities here. Assuming you are not doing any colorspace conversion
(eg RGB<->YUV, etc), a few shifts and ors would be sufficient.

--
:wq
^X^Cy^K^X^C^C^C^C
Oct 20 '06 #3
an***********@gmail.com wrote:
Hi,

I am trying to convert a 16 bit rgb value to 32 bit, however the new
color generated is different from the 16 bit rgb data. Please let me
know the formula to convert an 16 bit rgb data to 32 bit rgb data.
Thanks!!
You need to rigidly specify the actual bitwise format for both the
16-bit and the 32-bit form.

Your subject said the 16-bit rgb value was 565, so I have assumed it's
the binary value rrrrrggggggbbbbb. Splitting this into nybbles helps for
reading off the masks required in hexadecimal.

You said nothing about the 32-bit format. Common formats include BGR0,
RGB0, 0RGB, 0BGR. In the code below I have assumed 0RGB. Changing this
is easy, just modify the shift amounts in the last line.

unsigned long rgb16_to_rgb32(unsigned short a)
{
/* 1. Extract the red, green and blue values */

/* from rrrr rggg gggb bbbb */
unsigned long r = (a & 0xF800) >11;
unsigned long g = (a & 0x07E0) >5;
unsigned long b = (a & 0x001F);

/* 2. Convert them to 0-255 range:
There is more than one way. You can just shift them left:
to 00000000 rrrrr000 gggggg00 bbbbb000
r <<= 3;
g <<= 2;
b <<= 3;
But that means your image will be slightly dark and
off-colour as white 0xFFFF will convert to F8,FC,F8
So instead you can scale by multiply and divide: */
r = r * 255 / 31;
g = g * 255 / 63;
b = b * 255 / 31;
/* This ensures 31/31 converts to 255/255 */

/* 3. Construct your 32-bit format (this is 0RGB): */
return (r << 16) | (g << 8) | b;

/* Or for BGR0:
return (r << 8) | (g << 16) | (b << 24);
*/
}
--
Simon.
Oct 20 '06 #4
an***********@gmail.com wrote:
Hi,

I am trying to convert a 16 bit rgb value to 32 bit, however the new
color generated is different from the 16 bit rgb data. Please let me
know the formula to convert an 16 bit rgb data to 32 bit rgb data.
Platform-specific questions are off-topic in comp.lang.c, but I'll try
to give a general answer anyway.

You wrote (in your subject) that you have a 16 bit value that is split
into 5, 6, and 5 bit fields respectively. Think about the values that
can be represented using 5 and 6 bits. With 5 bits you have 32 possible
values and with 6 bits you have 64 possible values. With a 32 bit value
split into four components (that's usually how it's done) you have 8
bits for each field, which gives you 256 possible values.

Now think of the representable values as a range going from minimum to
maximum. The maximum value that van be stored in a 5 bit unsigned
number is 0x1F. But the maximum value that can be stored in an 8 bit
unsigned number is 0xFF. If you just extend 0x1F with zeroes so that it
becomes an 8 bit number it will hardly represent the maximum possible
value. That's why just extending the values will not give the expected
result.

What you need to do is to interpolate the value that you have over the
range of values that you are extending to (or vice versa). In C it
would be something like:

color = (unsigned)(final_range * ((double)color) / initial_range);

Regards,
Bart.

Oct 20 '06 #5
Bart wrote:
an***********@gmail.com wrote:
>Hi,

I am trying to convert a 16 bit rgb value to 32 bit, however the new
color generated is different from the 16 bit rgb data. Please let me
know the formula to convert an 16 bit rgb data to 32 bit rgb data.

Platform-specific questions are off-topic in comp.lang.c, but I'll try
to give a general answer anyway.
It's not platform specific. These are just bit manipulations, and it's
completely reasonable to do them in portable, standard C.

As for the question of exactly which layout is required, that depends on
what the OP intends to do with the data after it is converted. If it's
to write a binary file, then the file type matters. For instance, BMP
and PPM file formats use different RGB orderings.

[snip good info]

--
Simon.
Oct 20 '06 #6
an***********@gmail.com wrote:
Hi,

I am trying to convert a 16 bit rgb value to 32 bit, however the new
color generated is different from the 16 bit rgb data. Please let me
know the formula to convert an 16 bit rgb data to 32 bit rgb data.
Thanks!!
You can't. The 565 rgb format is just one unholy mess. You can't even
represent grey values in 565 format. Whoever came up with that format
should be shot, hanged, and fed to crocodiles, but not necessarily in
that order.

Try using 555 format instead, possibly with an alpha bit.

Oct 21 '06 #7
christian wrote:
an***********@gmail.com wrote:
Hi,

I am trying to convert a 16 bit rgb value to 32 bit, however the new
color generated is different from the 16 bit rgb data. Please let me
know the formula to convert an 16 bit rgb data to 32 bit rgb data.
Thanks!!

You can't. The 565 rgb format is just one unholy mess. You can't even
represent grey values in 565 format. Whoever came up with that format
should be shot, hanged, and fed to crocodiles, but not necessarily in
that order.
You don't know what you're talking about. Grey values are simply r=x,
g=2x, b=x. The double granularity for green is due to the better green
perception in the human eye.
Try using 555 format instead, possibly with an alpha bit.
The 555 format typically just ignores the extra bit. Talk about coming
up with a good format!

Anyway, that's all off-topic in comp.lang.c.

Regards,
Bart.

Oct 21 '06 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Sorisio, Chris | last post by:
Ladies and gentlemen, I've imported some data from a MySQL database into a Python dictionary. I'm attempting to tidy up the date fields, but I'm receiving a 'mx.DateTime.Error: cannot convert...
13
by: Stumped and Confused | last post by:
Hello, I really, really, need some help here - I've spent hours trying to find a solution. In a nutshell, I'm trying to have a user input a value in form's textfield. The value should then be...
21
by: Michael Bierman | last post by:
Please forgive the simplicy of this question. I have the following code which attempts to determine the color of some text and set other text to match that color. It works fine in Firefox, but does...
7
by: Sashi | last post by:
Two questions: (1) I can pull the text of an XML element as a string just fine using code as such: strSomeString = myXmlDoc.SelectSingleNode("/Element1/Element2/Element3",...
7
by: whatluo | last post by:
Hi, all I'm now working on a program which will convert dec number to hex and oct and bin respectively, I've checked the clc but with no luck, so can anybody give me a hit how to make this done...
4
by: MilanB | last post by:
Hello How to convert value of WindowsIdentity.Token that is IntPtr type to string type? Thanks
1
by: David Shorthouse | last post by:
Hey folks, I am attempting to pass null as the input value from a series of textboxes if the user does not input a value prior to submit. To try and do this, I am using a vbscript function on...
4
by: Edwin Knoppert | last post by:
In my code i use the text from a textbox and convert it to a double value. I was using Convert.ToDouble() but i'm used to convert comma to dot. This way i can assure the text is correct. However...
6
by: Kate77 | last post by:
Hi, Im trying to build simple encryption, I use char d="a" int intASC = System.Convert.ToInt32(d); to convert it, and play with it, now I want to change it (d) back for the original ascii...
1
by: simply123 | last post by:
I doing doing C btw... i have been trying to convert array elements into their respective addresses but Im faced with many problems. eg. int x (array with 7 elements) im trying to set the...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.