createCompatibleImage() Question

I was reading KGPJ (Killer Game Programming in Java) and the author talks about using createCompatibleImage() to create a BufferedImage that is optimized for the internals of the graphics device, and to mark the BufferedImage as a managed image. It also says that since J2SE 5 any image read in with ImageIO.read() is marked as a managed image. So my question is does anyone know if using createCompatibleImage() really optimizes the image for the graphics object, or if it doesn’t really have any affect since the BufferedImage would be marked as manged already.

Thanks
-ZMan3359

createCompatibleImage is to create a new image. Yes, it does create a managed image which is vitally important if you want good performance.

If you’re creating the image via ImageIO.read() then you should also have a managed image. You don’t need to involve createCompatibleImage as well unless I’m misunderstanding your use-case.

I know that ImageIO.read() also makes the image managed, but the book talks about createCompatibleImage() optimizing the image as well as making it managed. So I’m wondering if it does optimize the image and, and if it’s worth it. It sounds like it’s not. The book loads an image using ImageIO.read(). Then it creates a compatible image with createCompatibleImage(). Finally it copies the image read in by ImageIO.read() to the image created by createCompatibleImage(). The book is from 2005 which is why I’m wondering if it still applies. The book says this about it:

[quote]In J2SE 5.0 the JVM knows that any image read in by ImageIO’s read can become a managed image, so the call to createCompatibleImage() is no longer necessary for that reason. The call should still be made though since it optimizes the BufferedImage’s internals for the graphics device.
[/quote]

ImageIO will not convert the loaded image to be compatible with the graphics device.

private static BufferedImage toCompatibleImage(BufferedImage image)
      {
          // obtain the current system graphical settings
      	GraphicsConfiguration gfx_config = DisplayManager.getGraphicsConfiguration();
  
          /*
           * if image is already compatible and optimized for current system
           * settings, simply return it
           */
          if (image.getColorModel().equals(gfx_config.getColorModel()))
          {
              image.setAccelerationPriority(1.0f);
              return image;
          }
  
          // image is not optimized, so create a new image that is
          BufferedImage new_image = gfx_config.createCompatibleImage(image.getWidth(), image.getHeight(), image.getTransparency());
  
          // get the graphics context of the new image to draw the old image on
          Graphics2D g2d = (Graphics2D) new_image.getGraphics();
  
          // actually draw the image and dispose of context no longer needed
          g2d.drawImage(image, 0, 0, null);
          g2d.dispose();
  
          new_image.setAccelerationPriority(1.0f);
  
          // return the new optimized image
          return new_image;
      }

Note: DisplayManager.getGraphicsConfiguration(); is from my engine, you have to get your GraphicsConfiguration however else

So I have used this in the past; when loading an image from ImageIO I would pass it through this method.
I removed it and wrote this comment back then: “// Eats double RAM with no CPU performance improvement @ drawing whatsoever.”

Also takes longer to load obviously.

I didn’t test it thoroughly; but it didn’t show any improvement, so I haven’t used it ever in Java2D

Your images could already have been in a ‘compatible’ format in the file it was loaded from.

If they already are, you’ll find no performance improvement in creating a new image with exactly the same format.

Thanks Riven and Cero, for answering my question.