[cairo] Image Format / Pre-Multiplied Alpha Related Feature Request

Lawrence D'Oliveiro ldo at geek-central.gen.nz
Thu Jan 28 15:12:29 PST 2016

On Thu, 28 Jan 2016 17:18:04 -0500, William Kappler wrote:

> perhaps I could render color without alpha and then alpha on its own,
> and just blend the data together on my end.

Try oversampling.

Say you want to create an image that is m × n pixels. Create it at,
say, 2m x 2n pixels instead. Undo the premultiplied alpha, then add
each square grouping of 2 × 2 pixel values into a new, deeper pixel
value, creating an image at m × n pixels.

Each original pixel component value in CAIRO_FORMAT_ARGB32 format is 8
bits, with a range of 0..255. Adding together 4 of these gives a range
of 0..1020; replicate the top 2 bits into the bottom 2 to extend the
dynamic range to 0..1023. This gives you an effective bit depth of log₂
1021 ≅ 10 bits. Even if not all 8 bits were meaningful to begin with
(e.g. because of rounding caused by the original premultiplied alpha),
you still gain log₂ 1021 - 8 ≅ 2 extra bits regardless.

If that’s not enough, you can try starting with a 4m × 4n image, and
add together groupings of 4 × 4 pixels to end up with a maximum of log₂
4081 ≅ 12 bits per pixel component at m × n resolution. Here you
replicate the top 4 bits into the bottom 4 bits to extend the 0..4080
dynamic range to 0..4095. And again, even if not all the bits are
meaningful, you still gain log₂ 4081 - 8 ≅ 4 extra bits per pixel

That’s oversampling in a nutshell.

More information about the cairo mailing list