In the #define's, the shifts are just to scale the values up by 2^14in order to use integer values rather than floats. Note that cR and cG are cast back to int and cB uses integer arithmetic and does not need to be converted back.

In the final line, the value is shifted back (scaled down by 2^14) to get back to a normal integer grayscale value. Each of RGB is an 8-bit number, and the coefficients in the grayscale calculation add up to 1, so the grayscale value is also in the range 0-256 (8 bits). The '+ (1 << ((RGB_SCALE)-1))' part is just to round to what will be the LSB after the final right shift.

In short, this algorithmj just multiplies the original RGB values by a big number so integer math can be used to compute the grayscale value. Then after the calculation, the big number is divided back out. The '+ 0.5' and the '+ (2^13)' just round results.

The way this code is written, there is only one shift actually done in the executable code - the final right shift. cR, cG, cB, and (1 << ((RGB_SCALE)-1)) are computed by the preprocessor and put into the code as constants.

And you would expect a shift to be much faster than a divide.

0

Featured Post

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

In the final line, the value is shifted back (scaled down by 2^14) to get back to a normal integer grayscale value. Each of RGB is an 8-bit number, and the coefficients in the grayscale calculation add up to 1, so the grayscale value is also in the range 0-256 (8 bits). The '+ (1 << ((RGB_SCALE)-1))' part is just to round to what will be the LSB after the final right shift.

In short, this algorithmj just multiplies the original RGB values by a big number so integer math can be used to compute the grayscale value. Then after the calculation, the big number is divided back out. The '+ 0.5' and the '+ (2^13)' just round results.