Related topic (without solution yet): Access to raw data in ARGB_8888 Android Bitmap
In short: when using copyPixelFromBuffer and copyPixelsToBuffer, Android already applied the Alpha channel on the RGB channels.
I need to convert it into original ARGB value and vice-versa. I don’t know how does Android apply it. Can you please tell me the formula?
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:
where
To convert from premultiplied alpha to canonical form, divide the premultiplied components with the normalized alpha:
but be sure to watch out for normalizedAlpha = 0! You should note though that the data handled by
Bitmap.copyPixelsToBuffer()andBitmap.copyPixelsFromBuffer()in theory can be in any format. It is safer to useBitmap.getPixels()andBitmap.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.