03-27-2009 08:22 AM
How can we greyscale a colored image? I tried to used LCDUI Graphics , in that we have setGrayScale(int value) method but its not working. Is there any way to achieve this?
Thanks in advance,
Solved! Go to Solution.
04-02-2009 08:56 AM
04-05-2009 11:23 PM - edited 04-05-2009 11:25 PM
Here is an effective but inefficient way to acheive what you're after:
Bitmap b = Bitmap.getBitmapResource( "myImage.png" );
int argb = new argb[ b.getWidth() * b.getHeight() ];
b.getARGB( argb, 0, b.getWidth(), x, y, b.getWidth(), b.getHeight() );
for ( int i = argb.length - 1; i >= 0; --i )
int alpha = argb[i] >> 24;
int red = (argb[i] >> 16) & 0xFF;
int green = (argb[i] >> 8) & 0xFF;
int blue = argb[i] & 0xFF
int grey = ( red + green + blue ) / 3;
int composite = (alpha << 24) | (grey << 16) | (grey << 8) | grey;
argb[i] = composite;
b.setARGB( argb, 0, b.getWidth(), x, y, b.getWidth(), b.getHeight() );
04-06-2009 02:16 AM
It worked just a small typo (int instead of argb while making new object) .
Is it not advisable to use this? Will this have any problem with performance? Actually as far as i have understood. We have to convert the image into raw byte, if we want to do some image editing stuff like providing some effects. If we dont have any other way then this should be fine right?
Please corrects my understanding.
04-06-2009 02:20 AM
This is like adding brightness, contrast and this don't need converting into raw bytes etc.. This will not cause any performance issues..
04-06-2009 01:41 PM - edited 04-06-2009 01:46 PM
There's no real problem with using this method, it's just that it's at the java layer so it has to go through the VM. You can also use a weighted sum. GIMP does it like this: composite = 0.3R + 0.59G + 0.11B.
Obviously the weighted sum method is a bit slower and requires fixed point arithmetic, but it looks better than a simple average.
*edit* Regarding animation, you can do fade to gray by using two bitmaps, one colour one gray scale and fading the grayscale one over top of the colour one. Cheers