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
cagdasgerede
Posts: 241
Registered: ‎11-20-2008
My Device: Not Specified
Accepted Solution

Bug in PNG Encoding: Bitmap to PNG with transparency

I first create a bitmap with some alpha values in each pixel. Then I create a png image from bitmap. Then I read the data back. From the png image, I get no alpha, no transparency, and the pixels are opaque.

See the code below. Try it yourself. Am I doing something wrong? Does PNG.encode support alpha values?

 

 

Bitmap source = new Bitmap(Bitmap.ROWWISE_16BIT_COLOR, width, height);
source.createAlpha(Bitmap.ALPHA_BITDEPTH_8BPP);      
int [] data = new int[width * height];       
for(int i=0; i<data.length;i++)        
  data[i] = 0x10000020;               
source.setARGB(data, 0, width, 0, 0, width, height);


PNGEncodedImage png = PNGEncodedImage(source);

png.hasAlpha(); //RETURNS FALSE

png.hasTransparency(); //RETURNS FALSE

 

//READ BACK

int data_read_back = new int[width * height];

png.getBitmap().getARGB(data_read_back,0,width,0,0,width,height);

data_read_back[0]; //RETURNS 0xFF000021 (opaque)

Please use plain text.
Developer
webmasterpdx
Posts: 558
Registered: ‎11-25-2008
My Device: Not Specified

Re: Bug in PNG Encoding: Bitmap to PNG with transparency

I don't have time to put too much time into this, but a couple of hints.

 

First, png is one of the few bitmap types to support alphablending. So anything set up before hand will be lost when you change to png.

 

You need to set the alpha values after it's png.

 

Secondly, only certain color formats support alpha. Typically it's 32-bit pixels with 8 bits for each of A, R, G and B.

 

I notice you set your color depth initially to 16 bits. I know that typically thats 565 mode, and there is no room for alpha, but I have seen different alpha modes at different depths. You'll need to look at the javadocs to see what ones are supported. That alone might be your problem. Have you tried using 32-bit pixels right from the start?

 

Hope that was of some help.

-Donald

==================

Donald Murray

CTO IPPUB

 

"If this helps you, then please click solved and Kudos on this post"

 

 

 

 

 

Please use plain text.
Developer
cagdasgerede
Posts: 241
Registered: ‎11-20-2008
My Device: Not Specified

Re: Bug in PNG Encoding: Bitmap to PNG with transparency

There is no support in BlackBerry for 32 bit pixels, and 16-bit for Bitmaps is the only way to get transparency (see javadocs).

 

I am sorry but your message was not very helpful, but I still thank you for your time.

Please use plain text.
Developer
webmasterpdx
Posts: 558
Registered: ‎11-25-2008
My Device: Not Specified

Re: Bug in PNG Encoding: Bitmap to PNG with transparency

That is not true. Of course the blackberry supports 32-bit color. I think you misunderstand what I'm saying. First, let me see if I understand your problem.

 

My understanding is you have a file that you want to read in. First, that file must be a png file if it is to contain alpha values. It must be saved in an alpha format. I thought that .png is one of the few file formats that supports alpha blending in the file. However, you must save it as such....save it as an ARGB 32-bit pixel.

 

Now, you must read it INTO the API as a 32-bit alpha'd pixel.

 

I don't use Bitmap for that. I use EncodedImage...like this.....

 

// Create an encoded image from provided name resource
EncodedImage eImage = EncodedImage.getEncodedImageResource( "img/new.png" )

 

Now, Once you have this EncodedImage, you can display it.

 

screen.add(new BitmapField(eImage.getBitmap()));

 

That should work......

 

Hope that helps.

 

I haven't played too much with bitmaps other than simple ones (24-bit) on the blackberry. I have used 32-bit colors as background colors for buttons though, and that worked perfectly, including the alpha values.

I have worked A LOT with different image formats in other systems, including windows, QNX, linux, etc.. and I understand these calls, and they are pretty standard.....nothing that looks unusual.

 

Good Luck.

-Donald

 

 

 

Please use plain text.
Developer
webmasterpdx
Posts: 558
Registered: ‎11-25-2008
My Device: Not Specified

Re: Bug in PNG Encoding: Bitmap to PNG with transparency

 

 I just noticed that your one set the bitmap by hand, so lets try this....

I looked at the Bitmap class, and I think if you leave out the type and just do it like this..... it should work...I haven't tried it though.

 

 

Bitmap source = new Bitmap(width, height);

int [] data = new int[width*height];

for(int j = 0; j < data.length; ++j)

    data[i] = 0x10000020;

source.setARGB(data, 0, width, 0, 0, width, height);

 

 Good Luck.

-Donald

 

 

Please use plain text.
Developer
webmasterpdx
Posts: 558
Registered: ‎11-25-2008
My Device: Not Specified

Re: Bug in PNG Encoding: Bitmap to PNG with transparency

Wait a minute.....what is your phone type. Mine is a Storm?

I just noticed you don't seem to say what type you have.....you should say that and what rev of JDE.

 

Thx

-D

Please use plain text.
Developer
Ted_Hopp
Posts: 1,305
Registered: ‎01-21-2009
My Device: Not Specified

Re: Bug in PNG Encoding: Bitmap to PNG with transparency

What is the line

 

PNGEncodedImage png = PNGEncodedImage(source);

supposed to be? It isn't going to get through the compiler as is. If it's supposed to be:

 

PNGEncodedImage png = PNGEncodedImage.encode(source);

(only available since device software 4.3) then I'm not sure, but you may need to call createAlpha() on source before trying to encode.

 

 




Solved? click "Accept as solution". Helpful? give kudos by clicking on the star.
Please use plain text.
Developer
cagdasgerede
Posts: 241
Registered: ‎11-20-2008
My Device: Not Specified

Re: Bug in PNG Encoding: Bitmap to PNG with transparency

[ Edited ]

To: webmasterpdx

 

BlackBerry only supports 16 bit RGB colors. It does not support 24 bit RGB. As a result, the following line is correct.

Bitmap source = new Bitmap(Bitmap.ROWWISE_16BIT_COLOR, width, height);

 

 

You misunderstand. I do not have a png file. I am creating a png in memory from a bitmap.

 

 

Unfortunately your code does not work. You have to create the alpha channel for the bitmap to have alpha values. If you test the code you write, you will see that when you read all the alpha values back with getARGB, you will get 255 for them. So the bitmap ignores the alpha values you send with setARGB.

 

 

 

By the way, my phone is BlackBerry Bold. I am targeting OS 4.3 and higher

 

 

 

Message Edited by cagdasgerede on 04-13-2009 12:38 PM
Please use plain text.
Developer
cagdasgerede
Posts: 241
Registered: ‎11-20-2008
My Device: Not Specified

Re: Bug in PNG Encoding: Bitmap to PNG with transparency

PNGEncodedImage png = PNGEncodedImage(source);

is a typo. In the actual code, it is

PNGEncodedImage png = PNGEncodedImage.encode(source);

Please use plain text.
Developer
Ted_Hopp
Posts: 1,305
Registered: ‎01-21-2009
My Device: Not Specified

Re: Bug in PNG Encoding: Bitmap to PNG with transparency

So did you try calling createAlpha() on source before trying to encode it?



Solved? click "Accept as solution". Helpful? give kudos by clicking on the star.
Please use plain text.