1
votes

I'm writing a simple J2ME game that uses PNG images with 8-bit alpha channel. Problem: not all hardware supports full alpha transparency rendering. However, since my game is pretty static in nature (at the beginning, "sprites" are layed out onto background image, based on current screen size, and that's about it), I thought it would be possible to prerender those transparent images directly onto background during game initialization and use that later in game. I can't prerender them in Photoshop as their positions are not known in advance.

But, it seems there is no way to read the original alpha channel on devices that do not support semi-transparency as it gets resampled during PNG loading. Is there some library that can help with that? Or is it a good idea to store alpha channels separately (e.g. as separate 8-bit PNG images) and manually apply them?

Thanks!

2

2 Answers

1
votes

PNG Images also have transparency support if you want to create transparent image then you have read RGB data along with alpha channels and process alpha

Image transPNG=Image.createImage("/trans.png");  //load the tranparent image
int rgbData[];
transPNG.getRGB(rgbData, 0,transPNG.getWidth(), 0, 0,transPNG.getWidth(), transPNG.getHeight());
Image tranparentImage=Image.createRGBImage(rgbData, width, height, true); //process alpha
transPNG=null;

Above code shows how to create the transparent image and use.

0
votes

I cant promise this will help, but you can try this way of reading the alpha channel using standard methods from Java util.

BufferedImage image = ImageIO.read(new File(name));

int[] alpha = new int[1]; //containg alpha-value for one pixel.
image.getAlphaRaster().getPixel(x, y, alpha);

System.out.println(alpha[0]); //gives the alpha value for x,y