Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
Developer
sandeepkumar03
Posts: 117
Registered: ‎02-12-2009
My Device: Not Specified
Accepted Solution

Grayscale Image

Hi All,

 

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,

Sandeep

Please use plain text.
Administrator
MSohm
Posts: 14,241
Registered: ‎07-09-2008
My Device: BlackBerry Z30, BlackBerry PlayBook
My Carrier: Bell

Re: Grayscale Image

There is no built in method to convert a colour image to greyscale.
Mark Sohm
BlackBerry Development Advisor

Please refrain from posting new questions in solved threads.
Problem solved? Click the Accept As Solution button.
Found a bug? Report it using Issue Tracker
Please use plain text.
Developer
pwerry
Posts: 177
Registered: ‎01-21-2009
My Device: 9900
My Carrier: Vodafone

Re: Grayscale Image

[ Edited ]

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() );

 

 

 

Message Edited by pwerry on 04-05-2009 11:25 PM
Message Edited by pwerry on 04-05-2009 11:25 PM
Please use plain text.
Developer
sandeepkumar03
Posts: 117
Registered: ‎02-12-2009
My Device: Not Specified

Re: Grayscale Image

Thanks pwerry,

 

It worked just a small typo (int instead of argb while making new object) :smileyhappy:.

 

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.

 

Thanks,

Sandeep

Please use plain text.
Developer
mantaker
Posts: 1,477
Registered: ‎12-30-2008
My Device: Torch 9810

Re: Grayscale Image

This is like adding brightness, contrast and this don't need converting into raw bytes etc.. This will not cause any performance issues..

Thanks! 

--
Manimaran Selvan
Co-Founder, Tech Lead,
Equity Markets Research Group
Please use plain text.
Developer
pwerry
Posts: 177
Registered: ‎01-21-2009
My Device: 9900
My Carrier: Vodafone

Re: Grayscale Image

[ Edited ]

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 :smileyhappy:

Message Edited by pwerry on 04-06-2009 01:44 PM
Message Edited by pwerry on 04-06-2009 01:46 PM
Please use plain text.