Sign in to follow this  

[java] Copying alpha from a Buffered Image to a Volatile Image?

This topic is 3842 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm currently writing a map editing tool in Java for a game project I'm working on. I'm trying to use accelerated (or at least image format compatible) images for speed purposes. I initially load my image into a BufferedImage with ImageIO.read(), and then blit that onto a VolatileImage object using drawImage. This works fine for all of the color channels, but the alpha channel isn't actually getting copied to the VolatileImage as far as I can tell. I have verified (by drawing the BufferedImage to my target surface) that the alpha channel is indeed being loaded correctly from the png file. I have also verified that the VolatileImage has been created with transparency support. So it appears that the alpha channel is only used to modify the blending of the color channels on the target surface, and the alpha values themselves are not updated. Here is the code that creates both of the images (sans the class stuff around it):

	BufferedImage image;
	VolatileImage acceleratedImage;

	public LoadImage(String filename)
	{
		try 
		{
			image = ImageIO.read(new File(filename));
			
			if(image.getColorModel().hasAlpha())
				System.out.println("Alpha");
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
	}

	public Image getAcceleratedImage(GraphicsConfiguration graphicsConf)
	{
		if(acceleratedImage == null)
		{
			acceleratedImage = 
				graphicsConf.createCompatibleVolatileImage(image.getWidth(), image.getHeight(), Transparency.TRANSLUCENT);
			acceleratedImage.createGraphics().drawImage(image, 0, 0, null);
			
			if(acceleratedImage.getTransparency() == Transparency.TRANSLUCENT)
				System.out.println("translucent");
		}
		
		if(acceleratedImage.contentsLost())
		{
			acceleratedImage.createGraphics().drawImage(image, 0, 0, null);
		}
			
		return acceleratedImage;
}

Any help would be fantastic!

Share this post


Link to post
Share on other sites
Just wanted to let everyone know that I found the solution to the problem. It turns out that VolatileImages were added almost exclusively for the benefit of hardware accelerated back buffers, and that a BufferedImage, if the surface format matches a hardware-based target surface, will automatically be moved into a video-accelerated buffer whenever possible.

So I tried it, and it works like a charm.

Share this post


Link to post
Share on other sites

This topic is 3842 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this