# [cairo] rewriting libpixman

Bill Spitzak spitzak at d2.com
Fri Mar 30 03:22:41 PDT 2007

```David Turner wrote:
> Hello Guys,
>
> On Thu, 29 Mar 2007 13:33:42 -0400, "Jeff Muizelaar" <jeff at infidigm.net> said:
>>> The bit-copying "approximation" is exactly equivalent to multiplying by 255 and dividing by 31.  There's no point in implementing both -- stick with the bit copy, since it's more efficient.
>> (x << 3) | (x >> 2) is not equivalent to (255*x + 15)/31 for x = {3, 7,
>> 24, 28}
>>
>
> May I remind everyone that what we call "alpha" is a mere *approximation*, and that maybe this kind of
> numerical accuracy isn't so important as it may seem ?
>
> another common example is the "fast" implementation of y = x*alpha/255.0
>
> Cairo uses (t = x*alpha+128, y = (t + (t >> 8)) >> 8)
>
> while many other graphics libraries do something like:
>
>   alpha2 = alpha + (alpha >> 7)   // convert from 0..255 to 0..256 range
>   y      = x*alpha2 >> 8
>
> which turns out to be noticeably faster, especially when you're performing several multiplications
> with the same alpha in parallel. And I'm pretty certain that this doesn't amount to any perceptual
> difference in an 8-bit/channel world, unless you do a ton of alpha composition work on the same
> pixels (in which case things are likely to break *anyway* due to accumulated rounding errors)
>
> Frankly, I'd like to see alternative implementations of the "libpixman2" to compare their
> performance; that could be very interesting.
>
> Just my €.02
>
> - David

Indeed. I think in all cases where the fast and slow methods come out
different, the actual floating point value is within the range n+.25 and
n+.75. This means that the inaccuracy of either answer exceeds the
additional inaccuracy of picking the further-away answer. This is
pointless, the only way to get a more accurate result is to use error
diffusion, and Cairo is not going to attempt that.

Also people *rely* on the simple math. They do not expect edges of their
carefully photoshopped overlays to become visible when put atop the
background they painted it atop. This pretty much means that it has to
use the common simple implementations of these since that is what
Photoshop did. This also means that Cairo must have a "specify the color
as 8 bits" api, because otherwise people are going to guess or do
experiments and then rely on preciesly how Cairo turns floating point
into 8 bits, which means that algorithim can never be fixed or changed
after the first version.

```