9 Replies Latest reply: Feb 23, 2010 3:04 PM by 843802 RSS

    Better TIFF Performance

    843802
      Hi,

      I would like to display TIFF images in a JPanel more quickly than I do now.

      Currently, I use this JAI code :
             PlanarImage pi = JAI.create("fileload", "C:\\image.tif");
             ColorModel colmodel = pi.getColorModel();
             Rectangle rect = new Rectangle(new Dimension(pi.getWidth(), pi.getHeight()));
             BufferedImage buf = pi.getAsBufferedImage(rect, colmodel);
      And here I have my BufferedImage that I can display in my JPanel.

      That's works, but that also does have very bad performances : it needs a few seconds to open a simple TIFF G4 (Bilevel, B&W) image.

      Does someone know a better way to load TIFF images with JAI ?

      Thanks.
      Hugo.
        • 1. Re: Better TIFF Performance
          843802
          Hi,

          I don't know if this will lead to a performance boost but try this
          load method:
          public BufferedImage loadImageAsStream(File f) throws IOException {
                  SeekableStream s = new FileSeekableStream(f);
                  // maybe params for your tiff-file
                  TIFFDecodeParam param = null;
          
                  ImageDecoder dec = ImageCodec.createImageDecoder("tiff", s, param);
                  
          
          
                  // Which of the multiple images in the TIFF file do we want to load
                  // 0 refers to the first, 1 to the second and so on.
                  int imageToLoad = 0;
          
                  // older version
                  /*
                  RenderedImage op =
                          new NullOpImage(dec.decodeAsRenderedImage(imageToLoad),
                          null,
                          null,
                          OpImage.OP_IO_BOUND);
                  */
                  
                  BufferedImage buf = null;
                  //System.out.println("Colormodel " + op.getColorModel().getColorSpace().getType());
                  // tiff with one subimage
                  if (dec.getNumPages() == 1) {
                      buf = PlanarImage.wrapRenderedImage(dec.decodeAsRenderedImage()).getAsBufferedImage();
                  }
                  else{
                      // do load all pictures of a tiff ...
                  }
                  return buf;
              }
          Olek
          • 2. Re: Better TIFF Performance
            843802
            Thanks Olek.

            I try this right now, do some performance tests, and post my results.

            Hugo.
            • 3. Re: Better TIFF Performance
              843802
              Well,

              your code seems to have really good performances compared to mine !

              Your method needs 0.2 seconds to load most of my Tiff images in a BufferedImage object, when mine needs more than 1 or 2 seconds.

              Here is the code I use to perform this test :
                        TiffViewer tv = new TiffViewer("Tiff Viewer"); // A simple class which extends JFrame
                        BufferedImage buf = null;
                        
                        long begin = System.currentTimeMillis(); // Start time
              
                              // In the following "TRY" block, the first line is your method, the second is mine...
                        try {
                              // buf = tv.loadImageAsStream(new File("D:\\e.tif"));
                              buf = tv.loadImageWithMyMethod("D:\\e.tif");
                        } catch (IOException e) {
                        }
              
                        long end = System.currentTimeMillis(); // End time
              
                        float time = ((float) (end-begin)) / 1000f;
                        System.out.println("Loading time : " + time);
              So thanks again Olek !

              Now I have to do something so that your method also opens images from an URL, and not only from a File.
              And I think I am also going to use the same kind of method for opening my JPEG, PNG, GIF and BMP files (and see if there is some performance here too).

              Hugo.
              • 4. Re: Better TIFF Performance
                843802
                hi,
                why do you construct BufferedImage? that's the time consuming task. you can draw PlanarImage on the JPanel.
                lev
                • 5. Re: Better TIFF Performance
                  843802
                  Hi hugo,

                  I use a BufferedImage cause it's buffered ;)
                  The Java2D-Rendering Engine can benefit from BufferedImage as
                  e.G. storing frequently used images in VRAM. And many other benefits.
                  But you can of corse use the PlanarImage. It's the JAI common Image-object.

                  Olek
                  • 6. Re: Better TIFF Performance
                    843802
                    Ok, thank you for the hint.

                    Actually, I have used BufferedImage objects to display my images for a long time, and I have never tried to know more about the PlanarImage object.
                    I'll take a look at the javadoc, and maybe change my code.

                    Hugo
                    • 7. Re: Better TIFF Performance
                      843802
                      So are there more questions on this topic?

                      If no so you may reward some of the dukes ;) ?

                      Olek
                      • 8. Re: Better TIFF Performance
                        843802
                        All right man, this is done !

                        Thank you again.
                        • 9. Re: Better TIFF Performance
                          843802
                          TiffViewer tv = new TiffViewer("Tiff Viewer"); // A simple class which extends JFrame
                                    BufferedImage buf = null;
                                    
                                    long begin = System.currentTimeMillis(); // Start time
                           
                                          // In the following "TRY" block, the first line is your method, the second is mine...
                                    try {
                                          // buf = tv.loadImageAsStream(new File("D:\\e.tif"));
                                          buf = tv.loadImageWithMyMethod("D:\\e.tif");
                                    } catch (IOException e) {
                                    }
                           
                                    long end = System.currentTimeMillis(); // End time
                           
                                    float time = ((float) (end-begin)) / 1000f;
                                    System.out.println("Loading time : " + time);
                          hi, someone know how to do this part ???

                          // do load all pictures of a tiff ...

                          Thanks...

                          Edited by: Joshua1983 on Feb 23, 2010 1:04 PM