I’m writing a gauss filter for android.
According to the GaussFilter example of this tutorial: http://www.jhlabs.com/ip/blurring.html I’m editing my ALPHA value like this
alpha += f * ((pixelValue >> 24) & 0xff);
According to this Thread :
Using logical bitshift for RGB values
(pixelValue >>24 & 0xff);
is not equivalent to
Color.alpha(pixelValue);
As far as I know, the alpha channel is optional in RGB.
So it depends on the depth how I read and edit the alpha and rgb vlaues.
There are lots of combinations for different color depths, for example:
- 4 bit no alpha
- 4 bit + 4 bit alpha
- 24 bit including alpha (?)
- Macintosh offers 24 bit plus (or including?) 8 bit alpha
Since I don’t know which type of image the user wants to process, I have to catch all the combinations. I’d just use Color.Red(value), Color.Green(value) … but I’m not sure whether this will be too slow in the end? I could convert every input into a 24 bit Bitmap before processing, but this would take too long.
Any help is appreciated, thank you.
First of all, if you are worried about performance, you should implement it and measure.
If you want fastest possible implementation, you pretty much have to specialize it for each pixel format. But most likely you only need to handle only one, see below.
On the other hand, assuming you load your bitmap as Bitmap class instance, there are just 4 possible pixel formats, and
ARGB_8888is probably the only one you should care about (perhapsRGB_565too, but I don’t expect nice blur effect with such a small bits-per-pixel value). Actually, bitmaps are loaded withARGB_8888configuration by default, according to BitmapFactory.Options documentation. In other words – using standard Android’s bitmap handling API you do not need to do any conversion, it is done for you – just specify the format you need.In fact
(pixelValue >>24 & 0xff);andColor.alpha(pixelValue);are equivalent forARGB_8888pixel format, it can however be done better.Using logical shift operator (as opposed to
>>, arithmetic shift) you do not need& 0xFFoperation at all, as bits 31..8 are always 0. See howColor.alpha()is implemented: