I found out my own way to make accelerated images

I read the Java doc and ImageIO does not return an accelerated image under 1.5.

Regardless I wanted to do it myself and found a nice way of doing so.


BufferedImage img =  ImageIO.read(new File(path));
                  img = img.getSubimage(x, y, w, h);
                  
                  BufferedImage tmp = new BufferedImage(
                              img.getWidth(),
                              img.getHeight(),
                              img.getType()
                  );
                  
                  //get pixels from img and put into accelerated image called tmp
                  for(int yy=0; yy < tmp.getWidth(); yy++){
                        for(int xx=0; xx < tmp.getWidth(); xx++){
                              tmp.setRGB(xx, yy, img.getRGB(xx, yy));
                        }
                  }

EDIT:
Funny how I didn’t see these methods before.

“tmp.setRGB(xx, yy, img.getRGB(xx, yy));”
I was creating sub images like this under .NET’s GDI+.

[quote]I read the Java doc and ImageIO does not return an accelerated image under 1.5.
[/quote]
Where exactly did you read this? As of 5.0, ImageIO will always return managed (accelerated) images. If there’s a case in which it is returning an unmanaged image, then we’d like to know about it.

Are you sure that ImageIO is returning an unmanaged image? In some cases, getSubimage() will cause an image to become unmanaged, so you should see if this is what is happening in your case.

There are much more efficient approaches than using set/getRGB(). For example:


BufferedImage img = ...;
BufferedImage tmp = new BufferedImage(w, h, img.getType());
Graphics2D g = tmp.createGraphics();
g.drawImage(img, 0, 0, w, h, x, y, x+w, y+h, null);
g.dispose();

Chris

ImageIO doesn’t get a managed image.

My version of Java:
java version “1.5.0”
Java™ 2 Runtime Environment, Standard Edition (build 1.5.0-b64
Java HotSpot™ Client VM (build 1.5.0-b64, mixed mode, sharing)

http://members.optusnet.com.au/ksaho/normal/dist.zip

My code’s inside and so is the 32bit png image.


public class Main extends JFrame
{
      private BufferedImage img;
      private GraphicsDevice ge;
      
      
      public void paint(java.awt.Graphics graphics)
      {
            Graphics2D g = (Graphics2D)graphics;
            
            g.drawImage(img, null, 50, 0);
            
            boolean b = img.getCapabilities(ge.getDefaultConfiguration()).isAccelerated();
            g.drawString("Is image accelerated: " + b, getWidth() - 170, getHeight() - 50);
      }
      
      
      /** Creates a new instance of Main */
      public Main()
      {
            try{
                  img = ImageIO.read(
                              new File("ass.png")
                  );
            }catch(IOException e){System.out.println("no image");}
            
            
            this.setDefaultCloseOperation(EXIT_ON_CLOSE);
            this.setSize(320, 240);
            this.setVisible(true);
            
            ge = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice();
      }
      
      
      /**
       * @param args the command line arguments
       */
      public static void main(String[] args)
      {            
            SwingUtilities.invokeLater(
                        new Runnable(){
                        public final void run(){
                              Main main = new Main();
                        }
                  }
            );
      }
      
}

That’s one wierd way of doing things! What’s wrong with just using createCompatibleImage and blitting any ImageIO-returned image into it and using the new one?

Cas :slight_smile:

FYI, the WIKI even has a topic on the stanard way of making managed images out of those loaded with ImageIO:

http://wiki.java.net/bin/view/Games/LoadingSpritesWithImageIO

Doing a getRGB/setRGB is inventive, but bound to be extremely slow. :slight_smile:

Works instantly for me with over 25,000 images with transperencies and widths and heights over 64.

In my dreams… :lol:

[quote]FYI, the WIKI even has a topic on the stanard way of making managed images out of those loaded with ImageIO:

http://wiki.java.net/bin/view/Games/LoadingSpritesWithImageIO

Doing a getRGB/setRGB is inventive, but bound to be extremely slow. :slight_smile:
[/quote]