Skip to main content

How do you read a jpg image into a BufferedImage.TYPE_INT_ARGB_PRE?

1 reply [Last post]
Joined: 2008-03-14

How do you read a jpg image into a BufferedImage.TYPE_INT_ARGB_PRE?

I've been trying all day and as far as I can tell, reading a jpg image into a
BufferedImage.TYPE_INT_ARGB_PRE is impossible and that can't be right.

It should be less than 10 lines of code. I would really appreciate it if someone could post
a snippet showing how that's done.

The closest I can come to it is a BufferedImage.TYPE_INT_RGB and then I have to translate
it into an int array OR'ing it with 0xff000000. So for an instant, I use twice as much
memory and that can cause my applet to suffer an OutOfMemoryError on some systems.

If I could read the image directly into a premultiplied int array, that would be a big help
to the robustness of the applet.

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
Joined: 2006-10-24

Hi demonduck,

I don't know any way of forcing the image reading into a specific kind of BufferedImage.

However what I do when I need a specific type is:
1- read the image the standard way into an image (called origImg here)

2- create an empty BufferedImage of the desired image type and the same size as the original image:
BufferedImage newImg = new BufferedImage(origImg.getWidth(), origImg.getHeight(), BufferedImage.TYPE_INT_ARGB_PRE);
3- create the Graphics2D object of the empty image using BufferedImage.createGraphics() method:
Graphics2D g2d = newImg.createGraphics();

4- draw the original image into the empty image using the the Graphics2D.drawImage methode:
g2d.drawImg(origImg, null, 0, 0);

5- call to Graphics2D.dispose() to force release of resources used by the graphic context object:

This is the only way I found to do what you are looking for.

Alternatively if you already have a BufferedImage of type TYPE_INT_ARGB, you can use the [code]BufferedImage.coerceData(true)[/code] method to convert into pre-multiplied alpha.

Hope this helps.