Android – How to convert grayscale bitmap into alpha mask

alphaandroidbitmapgraphicstransparency

I have a grayscale JPG picture, and I'd like to load it into Bitmap of format Bitmap.Config.ALPHA_8. Is that possible, how can I do that?

It's straightforward to load alpha channel from a PNG (which can have empty R,G,B channels), but I'd like to use JPG for compression.

This is a followup question to How to combine two opaque bitmaps into one with alpha channel?

Best Answer

ColorMatrix to rescue!

Quoting Android docs, ColorMatrix:

5x4 matrix for transforming the color+alpha components of a Bitmap. The matrix is stored in a single array, and its treated as follows: [ a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t ] When applied to a color [r, g, b, a], the resulting color is computed as (after clamping) R' = aR + bG + cB + dA + e; G' = fR + gG + hB + iA + j; B' = kR + lG + mB + nA + o; A' = pR + qG + rB + sA + t;

Set up color matrix that takes alpha value from red channel (or green, or blue, doesn't matter for grayscale...), then use it in Paint.setColorFilter(). Here's a more or less complete example:

final BitmapFactory.Options options = new BitmapFactory.Options();
options.inPreferredConfig = Bitmap.Config.ARGB_8888;
options.inScaled = false;

// Load source grayscale bitmap
Bitmap grayscale = BitmapFactory.decodeResource(getResources(), R.drawable.my_grayscale_mask, options);
// Place for  alpha mask. It's specifically ARGB_8888 not ALPHA_8, 
// ALPHA_8 for some reason didn't work out for me. 
Bitmap alpha = Bitmap.createBitmap(grayscale.getWidth(), grayscale.getHeight(),
        Bitmap.Config.ARGB_8888);
float[] matrix = new float[] {
        0, 0, 0, 0, 0,
        0, 0, 0, 0, 0,
        0, 0, 0, 0, 0,
        1, 0, 0, 0, 0};
Paint grayToAlpha = new Paint();
grayToAlpha.setColorFilter(new ColorMatrixColorFilter(new ColorMatrix(matrix)));
Canvas alphaCanvas = new Canvas(alpha);
// Make sure nothing gets scaled during drawing
alphaCanvas.setDensity(Bitmap.DENSITY_NONE);
// Draw grayscale bitmap on to alpha canvas, using color filter that
// takes alpha from red channel
alphaCanvas.drawBitmap(grayscale, 0, 0, grayToAlpha);
// Bitmap alpha now has usable alpha channel!