1
votes

Can someone explain the calculation being used to extract color components on the right side of the following statements using bit shift operators?

                uint alpha = (currentPixel & 0xff000000) >> 24; // alpha component

                uint red = (currentPixel & 0x00ff0000) >> 16; // red color component

                uint green = (currentPixel & 0x0000ff00) >> 8; // green color component

                uint blue = currentPixel & 0x000000ff; // blue color component
1

1 Answers

2
votes

Lumia Imaging SDK exposes the color value using the ARGB color format. It will use 8 bits to encode each color component, but for simplicity/efficiency it will store and expose all four of them in a single uint32.

That means that each color component is "laid out" in the int in the order you saw: 8 bits for alpha, 8 bits for Red, 8 bits for Green and 8 bits for blue: ARGB.

To extract the individual components you need to do some bitwise operations on the int. First you need to do an and operation to single out the bits you are interested in (using the & operator), then you do a bitwise right shift (the >> operator) to get the bits you want into the [0, 255] range.