Loading a rescaled version of a JPEG image

I’m using the following code to load an scaled version of a big JPEG image. I’d like to know if there are better (faster) ways to do it. I’m developing a thumbnail explorer-type app and I’d like to create the thumbnails the fastest way possible.

Here is the code. As you can see, I’m not loading the full image and then rescaling it. I rescale it while loading. Also, I’d like to know if the result BufferedImage is hardware accelerated:



private static BufferedImage readSubsampling(File file, int maxSize) throws IOException {
      Iterator readers = ImageIO.getImageReadersByFormatName("jpeg");
      ImageReader reader = (ImageReader)readers.next();
      ImageInputStream iis=null;
      iis = ImageIO.createImageInputStream(file);
      reader.setInput(iis, true, true);
      ImageReadParam param = reader.getDefaultReadParam();
      int size = Math.max(reader.getWidth(0), reader.getHeight(0));
      int subsampling = size/maxSize + (size%maxSize != 0 ? 1 : 0);
      param.setSourceProgressivePasses(0, 4);
      param.setSourceSubsampling(subsampling, subsampling, 0, 0);
      BufferedImage image = reader.read(0, param);
      reader.dispose();
                  
      return image;
}

Thanks in advance

Because you are using an ImageIO ImageReader this will not result in a “Managed Image” with Java 1.4.x. I’m not sure, if 1.5beta1 accelerates ImageIO images yet.

Since on 1.4 you would have to copy the image to a managed or accelerated image after loading, it might be faster to load the image full size and do the scaling when you draw the image into the accelerated image.

The resulting image would’ve been accelerated in 1.5b if it was not for the bug that no jpeg images loaded with imageio were being accelerated. This bug has been fixed in 1.5b2 (should be out soon).