5 Replies Latest reply: Feb 27, 2013 5:14 PM by EJP RSS

    Performance ImageIO read


      for our system i have to read a lot of png images and scale them down to about 25%.

      The images are 2500x3500px.

      the are read by following code
       BufferedImage image= ImageIO.read(new BufferedInputStream(new
      it takes 800ms to load on image. i tried on several machines, linux, windows without a big difference . Hardware acceleration like d3d and opengl seems not to help.

      is there any way to speed this up?

      greetings sascha
        • 1. Re: Performance ImageIO read
          I tried a bit different approach. Instead of reading from file I load the file in memory and then read. Works ~ 20% faster for me
          public class ResourceTest {
              public static void main(String[] args){
                  try {
                      long start=System.currentTimeMillis();
                      BufferedImage image= ImageIO.read(new BufferedInputStream(new
                              FileInputStream("c:/temp/1.jpg"), 8192));
                      long end=System.currentTimeMillis();
                      long start2=System.currentTimeMillis();
                      File f=new File("c:/temp/1.jpg");
                      byte[] buffer=new byte[(int)f.length()];
                      new FileInputStream(f).read(buffer);
                      BufferedImage image2= ImageIO.read(new ByteArrayInputStream(buffer));
                      long end2=System.currentTimeMillis();
                  } catch (IOException e) {
          • 2. Re: Performance ImageIO read
            There's no reason why the second would be any faster. It just adds latency and wastes memory, among other issues. All you're seeing is the effect of cache priming via the first.
            • 3. Re: Performance ImageIO read
              Yes. You are right. closer investigations show no real improvement.
              • 4. Re: Performance ImageIO read
                Passing a file should be a faster read than an input stream. That is, reading from a FileImageInputStream (what the file is wrapped in) is generally faster than reading from a FileCacheImageInputStream (what the inputstream is wrapped in).

                Another way to improve speed is to read only each n'th pixel. This is effectively scaling an image down using nearest neighbor interpolation during read time. To do so, you use the #setSourceSubsampling method on an ImageReadParam that you pass to an ImageReader.

                The last way to improve speed is to use a faster image reader. Something called the JAI-ImageIO package contains all of the readers and writers officially made by Sun/Oracle. It contains all reader/writers that you already have from installing the JDK, plus a TIFF reader/writer and a 'natively accelerated' jpeg and png reader/writer. You'll want the natively accelerated png one. It's called CLibPNGImageReader.
                • 5. Re: Performance ImageIO read
                  Yes. You are right. closer investigations show no real improvement.
                  It will be slower, when you measure it properly.