473,473 Members | 1,857 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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 27990
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.