473,836 Members | 1,487 Online
Bytes | Software Development & Data Engineering Community
+ 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 28109
an***********@g mail.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***********@g mail.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***********@g mail.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 rrrrrggggggbbbb b. 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***********@g mail.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)(fina l_range * ((double)color) / initial_range);

Regards,
Bart.

Oct 20 '06 #5
Bart wrote:
an***********@g mail.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***********@g mail.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***********@g mail.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
3252
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 value to a time value' error. It's related to glibc returning an error to a pre-1970 date, I think. My question: /how/ do I go through the Python direction I've created to remove the pre-1970 date objects? Ideally, I would be able to iterate...
13
9664
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 assigned to a variable and output using document.write. (Note, there is no submit button or other form elements. Basically
21
4012
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 nothing in IE. I'd be greatful for any assistance. Also, if I will have problems the code on Opera or Safari, I'd appreciate any pointers--I don't have a Mac to test Safari. THanks very much, Michael
7
3830
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", myXmlNSMgr).InnerText;
7
7129
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 without strtol or s/printf function. Thanks, whatluo.
4
8240
by: MilanB | last post by:
Hello How to convert value of WindowsIdentity.Token that is IntPtr type to string type? Thanks
1
11484
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 this asp as follows: CommentsAdd = IIf(Request.Form("Comments")="",NULL,Request.Form("Comments")) I was hoping this would convert those emptystring textboxes to null.
4
4528
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 it seems this convert is determined by the local settings and comma is indeed used as decimal separator. Is there another way to convert a dotted value to a double variable? Like 1234.5 and not 1234,5
6
15188
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 value ("a"), how do I do that ?
1
2312
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 3rd element value = address of 3rd element of array eg.
0
9666
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10544
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10585
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10250
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7788
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6977
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5645
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5821
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
4010
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.