# [cairo] On multiplying alpha

Jeff Muizelaar jeff at infidigm.net
Fri Feb 1 17:07:40 PST 2008

```On Fri, Feb 01, 2008 at 07:26:16PM -0500, Behdad Esfahbod wrote:
> I wrote some code about how to go from premultiplied alpha to multiplied
> alpha formats.  Thought people here may be interested to review:
>
>   http://bugzilla.gnome.org/show_bug.cgi?id=513812

Yeah, that code looks wrong. It should be:

t = a*c + 0x80;
r = ((t>>8) + t) >> 8;

If you're interested in the derivation it's in the article:
"Jim Blinn's Corner: Three wrongs make a right"

Furthermore, the standard way of multiplying a 32bit pixel by alpha is what FbByteMul does.
It should be faster than the method you propose.

#define FbByteMul(x, a) do {
uint32_t t = ((x & 0xff00ff) * a) + 0x800080;               \
t = (t + ((t >> 8) & 0xff00ff)) >> 8;                       \
t &= 0xff00ff;                                              \
\
x = (((x >> 8) & 0xff00ff) * a) + 0x800080;                 \
x = (x + ((x >> 8) & 0xff00ff));                            \
x &= 0xff00ff00;                                            \
x += t;

-Jeff
```