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!
19 13161
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.
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?
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?
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"
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.
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?
-
typedef struct RGB
-
{
-
unisgned char blue;
-
unsigned char green;
-
unsigned char red;
-
} RGB;
-
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.
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.
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?
-
typedef struct RGB
-
{
-
unisgned char blue;
-
unsigned char green;
-
unsigned char red;
-
} RGB;
-
why are you using char?
EDIT:
im also not seeing the big picture really. How do i convert a 24bits, to 16bits?
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?
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
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..
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.
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
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
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.
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)?
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.
Sign in to post your reply or Sign up for a free account.
Similar topics
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,...
|
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?
|
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.
|
by: XML newbie: Urgent pls help! |
last post by:
function to convert string to 1 dimensional array of long in VB.Net
|
by: perspolis |
last post by:
Hi all
I have some points in twips measurement.how can I convert them to pixel
measurement ?
thanks
|
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...
|
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....
|
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...
|
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
|
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: 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...
|
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...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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$) {
}
...
|
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...
|
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
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
| |