This discussion is archived
5 Replies Latest reply: Feb 27, 2013 3:14 PM by EJP RSS

Performance ImageIO read

803039 Newbie
Currently Being Moderated
hi,

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
                     FileInputStream("c:/temp/1.png"),8192));
          
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
    StanislavL Pro
    Currently Being Moderated
    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();
                System.out.println("time="+(end-start));
    
                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();
                System.out.println("time="+(end2-start2));
    
            } catch (IOException e) {
                e.printStackTrace();
            }
    
    
        }
    
    }
  • 2. Re: Performance ImageIO read
    EJP Guru
    Currently Being Moderated
    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
    StanislavL Pro
    Currently Being Moderated
    Yes. You are right. closer investigations show no real improvement.
  • 4. Re: Performance ImageIO read
    Maxideon Explorer
    Currently Being Moderated
    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
    EJP Guru
    Currently Being Moderated
    Yes. You are right. closer investigations show no real improvement.
    It will be slower, when you measure it properly.

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points