Android stores Bitmap data in alpha premultiplied form. In other words, the alpha value is not applied when the data is copied, it has been applied all the time.
To convert into premultiplied form, multiply the color components with the normalized alpha value, like this:
redPremultiplied = red * normalizedAlpha
greenPremultiplied = green * normalizedAlpha
bluePremultiplied = blue * normalizedAlpha
alphaPremultiplied = alpha
where
red = <value between 0 and 255>
green = <value between 0 and 255>
blue = <value between 0 and 255>
alpha = <value between 0 and 255>
normalizedAlpha = alpha / 255
To convert from premultiplied alpha to canonical form, divide the premultiplied components with the normalized alpha:
red = redPremultiplied / normalizedAlpha
green = greenPremultiplied / normalizedAlpha
blue = bluePremultiplied / normalizedAlpha
alpha = alphaPremultiplied
but be sure to watch out for normalizedAlpha = 0! You should note though that the data handled by Bitmap.copyPixelsToBuffer()
and Bitmap.copyPixelsFromBuffer()
in theory can be in any format. It is safer to use Bitmap.getPixels()
and Bitmap.setPixels()
, because the API makes guarantees about the format of the data handled by those functions. In particular, you don't have to worry about premultiplying and un-premultiplying the alpha, because those functions handle that for you.