How to convert 16 - bit 565 rgb value to 32 bit

Discussion in 'C Programming' started by anuragkhanna8@gmail.com, Oct 20, 2006.

  1. Guest

    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, 2006
    #1
    1. Advertising

  2. 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
     
    Nick Keighley, Oct 20, 2006
    #2
    1. Advertising

  3. Ico Guest

    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
     
    Ico, Oct 20, 2006
    #3
  4. Simon Biber Guest

    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.
     
    Simon Biber, Oct 20, 2006
    #4
  5. Bart Guest

    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.
     
    Bart, Oct 20, 2006
    #5
  6. Simon Biber Guest

    Bart wrote:
    > 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.
     
    Simon Biber, Oct 20, 2006
    #6
  7. christian Guest

    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.
     
    christian, Oct 21, 2006
    #7
  8. Bart Guest

    christian wrote:
    > 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.
     
    Bart, Oct 21, 2006
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Adam Teasdale Hartshorne

    Convert RGB as a float into unsigned chars

    Adam Teasdale Hartshorne, Oct 24, 2005, in forum: C++
    Replies:
    2
    Views:
    580
  2. manisha chawla
    Replies:
    1
    Views:
    1,052
    Oliver Wong
    Apr 3, 2007
  3. ZelluX
    Replies:
    3
    Views:
    1,927
    Peter Otten
    Dec 1, 2008
  4. Lamberti Fabrizio

    convert OleColor into RGB

    Lamberti Fabrizio, Apr 8, 2005, in forum: ASP General
    Replies:
    1
    Views:
    717
    Leonhardt Wille
    Apr 8, 2005
  5. Bruno Malvestuto

    Convert HEX to RGB

    Bruno Malvestuto, Aug 10, 2006, in forum: Ruby
    Replies:
    11
    Views:
    261
    Jeremy Hinegardner
    Dec 1, 2007
Loading...

Share This Page