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

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • SpecialKay
    New Member
    • Mar 2008
    • 109

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

    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!
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    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.

    Comment

    • SpecialKay
      New Member
      • Mar 2008
      • 109

      #3
      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?

      Comment

      • Savage
        Recognized Expert Top Contributor
        • Feb 2007
        • 1759

        #4
        Originally posted by SpecialKay
        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?

        Comment

        • SpecialKay
          New Member
          • Mar 2008
          • 109

          #5
          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"

          Comment

          • oler1s
            Recognized Expert Contributor
            • Aug 2007
            • 671

            #6
            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.

            Comment

            • SpecialKay
              New Member
              • Mar 2008
              • 109

              #7
              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?

              Comment

              • weaknessforcats
                Recognized Expert Expert
                • Mar 2007
                • 9214

                #8
                [code=c]
                typedef struct RGB
                {
                unisgned char blue;
                unsigned char green;
                unsigned char red;
                } RGB;
                [/code]

                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.

                Comment

                • oler1s
                  Recognized Expert Contributor
                  • Aug 2007
                  • 671

                  #9
                  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.

                  Comment

                  • SpecialKay
                    New Member
                    • Mar 2008
                    • 109

                    #10
                    Originally posted by oler1s
                    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?

                    Comment

                    • SpecialKay
                      New Member
                      • Mar 2008
                      • 109

                      #11
                      Originally posted by weaknessforcats
                      [code=c]
                      typedef struct RGB
                      {
                      unisgned char blue;
                      unsigned char green;
                      unsigned char red;
                      } RGB;
                      [/code]
                      why are you using char?

                      EDIT:
                      im also not seeing the big picture really. How do i convert a 24bits, to 16bits?

                      Comment

                      • Savage
                        Recognized Expert Top Contributor
                        • Feb 2007
                        • 1759

                        #12
                        Originally posted by SpecialKay
                        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?

                        Comment

                        • SpecialKay
                          New Member
                          • Mar 2008
                          • 109

                          #13
                          would you have to convert it to 111111111010101 010111011.

                          or is there a way to xor the hex value

                          EDIT:

                          111111111010101 010111011
                          111111111101010 101000100 xor =

                          010101010111011

                          not too sure about the shift part.
                          This is way more complicated then i thought

                          Comment

                          • Savage
                            Recognized Expert Top Contributor
                            • Feb 2007
                            • 1759

                            #14
                            Originally posted by SpecialKay
                            would you have to convert it to 111111111010101 010111011.

                            or is there a way to xor the hex value

                            EDIT:

                            111111111010101 010111011
                            111111111101010 101000100 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=0x0 0009900,shift it 8 places to the right >>

                            0x00000099(153)

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

                            Comment

                            • oler1s
                              Recognized Expert Contributor
                              • Aug 2007
                              • 671

                              #15
                              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.

                              Comment

                              Working...