A few years ago, I posted on my personal web page a number of programming challenges for my friends. This week, I present one of the old challenges: computing luma from RGB triplets using integer arithmetic only.

You have to compute the function

unsigned char SlowBrightness(unsigned char r, unsigned char g, unsigned char b) { return 0.289f * r + 0.587f * g + 0.114f * b + 1e-5f; }

but the processor you’re using is not capable of floating point number arithmetic. In other words, it uses a all-software library to emulate them and it’s really slow.

Re-implement this function using only integer operations.

Efficiency is not the only goal, you must have an exact solution as well: your integer-only implementation must give the same answer as the float version for all RGB triplets.

Bonus. Use only +, *, >> and minimize the size of the integer constants involved.

Share this: Reddit

Twitter

More

Facebook

Email



Like this: Like Loading... Related