473,382 Members | 1,359 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,382 software developers and data experts.

C++ function that will convert a 24bit color pixel

109 100+
Hey guys,

so i was given this question by one of my dads friends. With no information or turtorial. Just a paper and a pencil. Needless to say, it did not go well, and now my brain want to know the answer. I will try and remember the question as best i can.

Write a C++ function that will convert a 24bit color pixel in the follow format

RGB24(8:8:8)

into a 16bit color pixel

RGB16(5:6:5)

ensure that all the proper variable checking is done.

i have never worked on anything like this, so i would love any feed back.

Thanks!
Apr 28 '08 #1
19 13161
weaknessforcats
9,208 Expert Mod 8TB
The 24-bit R is 8 bits and that can hold a value 0-255.
The 16-bit R is 5 bits and that can hold a value 0-31.

You might think about (24-bit R) % 31 to get the 16-bit R.
Apr 28 '08 #2
SpecialKay
109 100+
what does it mean, when you have

RGB24(8:8:8)

i mean, i can assume that this means 8red 8 green 8 blue.

but how would i grab the value of red? from RGB

RGB24[0] = RGB16[0]%31?
Apr 28 '08 #3
Savage
1,764 Expert 1GB
what does it mean, when you have

RGB24(8:8:8)

i mean, i can assume that this means 8red 8 green 8 blue.

but how would i grab the value of red? from RGB

RGB24[0] = RGB16[0]%31?
Simple.

24bit pixel is represented with 32 bit integer.

in this format 0x00RRGGBB (00 becomes AA if you are using RGBA format),so which logical operator are you going to use to extract only parts of it?
Apr 28 '08 #4
SpecialKay
109 100+
What is an RGBA format? and i would assume one would us the Modulas operatior to extract parts of it. This seems like its just a Slightly more complicated version on a "Change Calculator"
Apr 30 '08 #5
oler1s
671 Expert 512MB
What is an RGBA format?
Is it really that hard to Google?

i would assume one would us the Modulas operatior to extract parts of it
Modulus? Whoa, hold on there. This isn't a decimal number. It's in hexadecimal. The assumption here is that you understand the basic relationship between hexadecimal numbers and binary numbers. And that you are aware of bitwise operations. To be specific, you should understand NOT, AND, OR, XOR, and the left/right shifts. If you know these three things, you should know how to work with RGBA numbers.
Apr 30 '08 #6
SpecialKay
109 100+
Your a meanie :P

Wowie thats a lot of information. The orignal problem did not mention anything about fadeing. So that means i would be dealing with just a normal RGB?

I understand all of the Shitfs and Or,And,XOR. I have never used them in a program. But i understand the concept.

Im still not sure how to work with all of this...

and, if an int is 32bits, and we are workin with 24bitRGB pixel. where do the other bits go... i understand it with the RGBA, the remaining bits are used for transparicy (If i read right).

EDIT:
by basic relationship between HEX and DEC you mean what? how to convert them?
Apr 30 '08 #7
weaknessforcats
9,208 Expert Mod 8TB
Expand|Select|Wrap|Line Numbers
  1. typedef struct RGB
  2. {
  3.   unisgned char blue;
  4.   unsigned char green;
  5.   unsigned char red;
  6. } RGB;
  7.  
RGB is just a struct with three 8-bit members.

All you have to do is convert these members to the other struct.

You should not need to work at the bit level.
Apr 30 '08 #8
oler1s
671 Expert 512MB
So that means i would be dealing with just a normal RGB?
Yes.

and, if an int is 32bits, and we are workin with 24bitRGB pixel. where do the other bits go...
Nowhere. You have 32 bits, but you only use 24. Yes, they get wasted.

by basic relationship between HEX and DEC you mean what? how to convert them?
No, I'm talking about relationship between hexadecimal and binary. Right, because you have a number of binary operations. But you work with binary numbers in hexademical instead. If you're confused, let me give you an example.

I have 1111 1110. What bit operation could i do to get 1111?
Then, I have FE. What bit operation could I do to get F?

It isn't complicated, but you need to realize the relationship between hex and bin.

EDIT: If you are given a struct of the component colors individually, that's not really RGB conversion, then. The idea is that you have a packed value that you need to extract the RGB components with bitwise operations and pack into another format.
Apr 30 '08 #9
SpecialKay
109 100+
I have 1111 1110. What bit operation could i do to get 1111?
Then, I have FE. What bit operation could I do to get F?
1111 1110
0000 1110 xor

=
1111

maybe?

and since FE = 1111 1110 it would be the same operation?
Apr 30 '08 #10
SpecialKay
109 100+
Expand|Select|Wrap|Line Numbers
  1. typedef struct RGB
  2. {
  3.   unisgned char blue;
  4.   unsigned char green;
  5.   unsigned char red;
  6. } RGB;
  7.  
why are you using char?

EDIT:
im also not seeing the big picture really. How do i convert a 24bits, to 16bits?
Apr 30 '08 #11
Savage
1,764 Expert 1GB
1111 1110
0000 1110 xor

=
1111

maybe?

and since FE = 1111 1110 it would be the same operation?
But why set the whole mask,in this case you need to know bits of the rest instead of only knowing bits you need.

What if you had 1011 1110 and you only needed to extract the two highest bits?

You would need to use mask like:0011 1110,instead of having a chance to easily put this mask:

1100 0000 with binary AND operator.

Converting the example to HEX say you have 0x00FFAABB and you want to pull out FF and shift it so it comes down to [0-255] interval.How would you do that with XOR?
Apr 30 '08 #12
SpecialKay
109 100+
would you have to convert it to 111111111010101010111011.

or is there a way to xor the hex value

EDIT:

111111111010101010111011
111111111101010101000100 xor =

010101010111011

not too sure about the shift part.
This is way more complicated then i thought
Apr 30 '08 #13
Savage
1,764 Expert 1GB
would you have to convert it to 111111111010101010111011.

or is there a way to xor the hex value

EDIT:

111111111010101010111011
111111111101010101000100 xor =

010101010111011

not too sure about the shift part.
This is way more complicated then i thought
There is a way to do bitwise operations under hex.You just write it in hex notation.

For example:

0x00FF99CC,if I were going to extract 99 from here I would use:

0x00FF99CC &0x0000FF00=0x00009900,shift it 8 places to the right >>

0x00000099(153)

Now you do it with XOR and tell me which one is easier..
Apr 30 '08 #14
oler1s
671 Expert 512MB
im also not seeing the big picture really. How do i convert a 24bits, to 16bits?
If I said, you have 2, 2, and want 4. What would you do with 2 and 2? You would say, just add them. It's dead obvious.

Now, converting 24bits to 16bits requires a number of mathematical operations as well. Obviously, it's a bit more involved than 2+2, but surprisingly not all that much. It's pretty straightforward, and we want you to be able to see how to put together the mathematical operations.

I mean, we could give you the answer. But then all someone has to do is give you RGBA to ARGB or something simplistic, with just a few things changed around. And you'll still be clueless.
May 1 '08 #15
SpecialKay
109 100+
There is a way to do bitwise operations under hex.You just write it in hex notation.

For example:

0x00FF99CC,if I were going to extract 99 from here I would use:

0x00FF99CC &0x0000FF00=0x00009900,shift it 8 places to the right >>

0x00000099(153)

Now you do it with XOR and tell me which one is easier..

0x00FF99CC ^
0x00FF00CC

111111111001100111001100
111111110000000011001100 ^

000000001001100100000000 << 8 = 10011001 = 99

Im going to be honest. I didnt see the hex at the start. I had to do the xor in bianary first. Now i get it tho.


For converting you would need to shift 3 and take 5, then shift 2 and take 6 then
shift 3 and take 5 more. I think?
24bit = 11111000 11111100 11111000

16bit = 11111 111111 11111
May 1 '08 #16
SpecialKay
109 100+
also what is a little hard to grasp, is that this is all taking place inside an int?

int a = 10;

a = 0x0000000A

a = 00000000000000000000000000001010
May 1 '08 #17
Savage
1,764 Expert 1GB
also what is a little hard to grasp, is that this is all taking place inside an int?

int a = 10;

a = 0x0000000A

a = 00000000000000000000000000001010
Yes,int is usually the same size as size of a register.So if processor is 32bit int is 32 bit.
May 1 '08 #18
Savage
1,764 Expert 1GB
0x00FF99CC ^
0x00FF00CC

111111111001100111001100
111111110000000011001100 ^

000000001001100100000000 << 8 = 10011001 = 99

Im going to be honest. I didnt see the hex at the start. I had to do the xor in bianary first. Now i get it tho.


For converting you would need to shift 3 and take 5, then shift 2 and take 6 then
shift 3 and take 5 more. I think?
24bit = 11111000 11111100 11111000

16bit = 11111 111111 11111

Ok,now if you have unknown RGB integer let's call it Color,how are you going to extract green component for example?

??(Color^WHAT)>>8=0xGG??

What I'm asking you,is how are you going to cancel all the other bits in this integer,so that only important bits remain(by using XOR)?
May 1 '08 #19
SpecialKay
109 100+
Ok,now if you have unknown RGB integer let's call it Color,how are you going to extract green component for example?

??(Color^WHAT)>>8=0xGG??

What I'm asking you,is how are you going to cancel all the other bits in this integer,so that only important bits remain(by using XOR)?
You are so Smart. I get it now. I dont have the time to do right now. But i will.
May 2 '08 #20

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Edwin Quijada | last post by:
There is a funciton to convert numbers into string?>? str:=string(3234); ?? _________________________________________________________________ Las mejores tiendas, los precios mas bajos,...
0
by: Ffelagund | last post by:
There are any Path or Directory function to convert an absolute path to a relative path using a choiced directory as base to calculate the origin of the relative path?
6
by: MrKrich | last post by:
I want to convert Hexadecimal or normal integer to Binary. Does VB.Net has function to do that? I only found Hex function that convert normal integer to Hexadecimal.
5
by: XML newbie: Urgent pls help! | last post by:
function to convert string to 1 dimensional array of long in VB.Net
4
by: perspolis | last post by:
Hi all I have some points in twips measurement.how can I convert them to pixel measurement ? thanks
1
by: Robert Dodier | last post by:
Hello, Sorry for asking what must be a FAQ, but I wasn't able to find the answer. I have an XML document fragment which I want to store as a text string. I want a function to convert any XML...
3
by: ashok | last post by:
Hi, I need a function that will divide text from mysql in 2 parts, so that I can display first half in one column and second half in second column. I can't find what function will do this job....
0
Akatz712
by: Akatz712 | last post by:
The following function converts a decimal number representing a color stored in the way that Microsoft windows stores colors (low byte is red), and converts it to a hex string which is needed for web...
3
by: manjuks | last post by:
Hi, Does C provides any library function to convert from ascii value to binary? Or we need to write our own function for it. Thanks, Manjunath
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...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

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.