CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7

Thread: Spliting bytes

  1. #1
    Join Date
    Mar 2010
    Location
    Melbourne Australia
    Posts
    454

    Spliting bytes

    I am currently working with win32 API , for image processing, one of the windows function returns RGBA ( colors ) as unsigned int , I then split it into individual bytes by creating a union ,
    Code:
    union colour
    {
     unsigned int value;
     unsigned char RGBA[4];
    }
    to spit the int into individual bytes , my Question is there a better or my convenient way of doing this .

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: Spliting bytes

    There are some macros for splitting:
    • HIWORD
    • LOWORD
    • HIBYTE
    • LOBYTE

    and for packing:
    • MAKEWORD
    • MAKELONG
    • ...

    Read MSDN for details.
    Victor Nijegorodov

  3. #3
    Join Date
    Mar 2010
    Location
    Melbourne Australia
    Posts
    454

    Re: Spliting bytes

    thanks Victor , however I was more interested in how it was done in the first place , I meant better solution than union.

  4. #4
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    4,626

    Re: Spliting bytes

    A union is a pretty normal way to address individual bytes of a larger structure. Getting the individual bytes with the macro's victor posted is equally common.
    When done right, neither of the two has an appreciable performance penalty or benefit to the other.

    In this particular case, you could also have used an RGBQUAD structure (available in the windows API) instead of the array of 4 chars. (assuming the bytes are in the same order as in the RGBQUAD). Or you could even have added the RGBQUAD as a 3rd choice of accessing the union.

    Depending on how you access the data from your C/C++ code, either of the solutions may have benefits in either ease of coding or readability.

    Here, a union "makes more sense" than the macro's. But sometimes the macro's "make more sense" than a union would.

  5. #5
    Join Date
    Mar 2010
    Location
    Melbourne Australia
    Posts
    454

    Re: Spliting bytes

    Thank you OReubens

  6. #6
    Join Date
    Mar 2010
    Location
    Melbourne Australia
    Posts
    454

    Re: Spliting bytes

    The only difference with Windows API is the Alpha channel is at the very end RGBA , as oppose to QT or C++ Builder XE2 ( from Embarcadero )where is is ARGB

  7. #7
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    4,626

    Re: Spliting bytes

    you can make your own structure if you find it makes more sense (or easier) to access an individual color component as
    .red
    rather than
    .RGBA[1]

    the .red has an advantage in readability the array has an advantage in that you can make a loop. having both in the union means you can use whichever makes more sense at a particular moment in time.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured