From: Marco M. <ok_...@ph...> - 2005-09-02 08:02:20
|
Maxim Shemanarev wrote: > It's a truncating division, while we need > rounding one. For unsigned integer numbers, a/b is replaced by (2*a+b) > / (2*b): > > unsigned f(unsigned alpha, unsigned cover) > { > return (alpha*cover*2 + 255) / (255 * 2); > } That's: byte f(byte alpha, byte cover) { int q=alpha*cover+128; return (q+(q>>8))>>8; } exact for 0< alpha,cover,f() < 256 and not terribly slower than (alpha*cover + 255) >> 8. Marco |