views:

73

answers:

1

I have a single-channel PNG file I'd like to use as an alpha mask for Porter-Duff drawing operations. If I load it without any options, the resulting Bitmap has an RGB_565 config, i.e. treated as grayscale. If I set the preferred config to ALPHA_8, it loads it as a grayscale ARGB_8888 instead.

How can I convince Android to treat this file as an alpha mask instead of a grayscale image?

mask1 = BitmapFactory.decodeStream(pngStream);
// mask1.getConfig() is now RGB_565

BitmapFactory.Options maskOpts = new BitmapFactory.Options();
maskOpts.inPreferredConfig = Bitmap.Config.ALPHA_8;
mask2 = BitmapFactory.decodeStream(pngStream, null, maskOpts);
// mask2.getConfig() is now ARGB_8888 (the alpha channel is fully opaque)
A: 

More of a workaround than a solution:

I'm now including the alpha channel in an RGBA PNG file with the RGB channels all zeroes. I can load this file with a preferred config of ARGB_8888 and then extract its alpha channel. This wastes a few KB in the mask file, and a lot of memory while decoding the image.

BitmapFactory.Options opts = new BitmapFactory.Options();
opts.inPreferredConfig = Bitmap.Config.ARGB_8888;
Bitmap source = BitmapFactory.decodeStream(pngStream, null, opts);
Bitmap mask = source.extractAlpha();
source.recycle();
// mask.getConfig() is now ALPHA_8
kvance