This discussion is archived
12 Replies Latest reply: Jan 7, 2011 1:14 AM by PhHein RSS

Read large images fast?

800188 Newbie
Currently Being Moderated
Hello all,

I'm not sure if this is the appropriate place to post this but here goes. I am working on creating an image viewer application and so far its going great. But i've noticed one problem when reading large images typically over 1 megabyte in size. These images using <tt> ImageIO.read() </tt> takes anywhere from 1 to two seconds to finish loading. I've searched all over the web for faster methods of reading large images but I haven't had any luck. Here is an example of what code i'm using to read an image.
try {
     BufferedImage b = ImageIO.read(someLargeImageFile); // this is slow for images over 1MB in size.
} catch(Exception err) {

}
So my question is does anyone have any tips or tricks I could use to read large images faster. I see programs like iPhoto reading images up to 5MB in size in under one second. Just wondering what I could do to read these kind of images just as fast.

Thank you for your help :)
Nathan D.
  • 1. Re: Read large images fast?
    sabre150 Expert
    Currently Being Moderated
    How did you time the reading of the images? Are you sure that the problem is not the time taken to render the images?

    Edited by: sabre150 on 05-Jan-2011 03:00
    I have just done a check on my Fedora 10.10 system using JDK 1.6.0_22 and I can load (as ImageIcon) and render 17 images each over 1 MByte and totalling 37 MByte in 13 seconds.
  • 2. Re: Read large images fast?
    800188 Newbie
    Currently Being Moderated
    sabre150 wrote:
    How did you time the reading of the images? Are you sure that the problem is not the time taken to render the images?

    Edited by: sabre150 on 05-Jan-2011 03:00
    I have just done a check on my Fedora 10.10 system using JDK 1.6.0_22 and I can load (as ImageIcon) and render 17 images each over 1 MByte and totalling 37 MByte in 13 seconds.
    You are correct I am not sure if the time it needs is for the rendering or reading of the image. All I do know is the processing time occurs in this block of code. The image is never displayed or used.
    I also just noticed that images that are around 100 kilobytes but have dimensions of around 3200x2900 take around 1-2 seconds to load as well. Which suggests that you are correct about it being the rendering time, assuming that rendering happens in the line <tt>BufferedImage image = ImageIO.read(someLargeImageFile);</tt>. <b> So my question is how can I speed up the rendering or reading of an image with sizes over 1MB or with large dimensions?</b> I know there must be some tip or tick to reading them faster. I know I could theoretically shrink large images when the user imports them so later those images would take less time to display but then again I really want to leave images that a user imports exactly as they were. So i'm kind of stuck between a rock and a hard place. :) I'm just curious how the big boys might do it. Such as the application iPhoto or Windows preview both these applications seem to load large images very fast. Do they save some kind of data from the image the first time they read it so they can load it faster or do they alter the images to make them a smaller size maybe. Hopefully I can come up with something in Java to get the same results.

    Thank you for reading this. :)
    try {
         BufferedImage image = ImageIO.read(someLargeImageFile);
    } catch(Exception err) {
    
    }
    Edited by: neptune692 on Jan 5, 2011 10:54 AM
  • 3. Re: Read large images fast?
    800381 Explorer
    Currently Being Moderated
    What version of the JVM are you using? How much RAM is available? What OS are you running? What kind of storage are the images on? How many other users and how many other processes are fighting over that storage?

    And if the JVM is recent and has a lot of RAM available, and you have eliminated any storage bottlenecks, have you tried Java Advanced Imaging?

    http://java.sun.com/javase/technologies/desktop/media/jai/

    I've used it to process image files in the hundreds of MB size range, and while I don't have any benchmarks handy I'd be surprised if those files were processed much if any slower than the processing speed you're getting with your much smaller files.
  • 4. Re: Read large images fast?
    800188 Newbie
    Currently Being Moderated
    user5287726 wrote:
    What version of the JVM are you using? How much RAM is available? What OS are you running? What kind of storage are the images on? How many other users and how many other processes are fighting over that storage?

    And if the JVM is recent and has a lot of RAM available, and you have eliminated any storage bottlenecks, have you tried Java Advanced Imaging?

    http://java.sun.com/javase/technologies/desktop/media/jai/

    I've used it to process image files in the hundreds of MB size range, and while I don't have any benchmarks handy I'd be surprised if those files were processed much if any slower than the processing speed you're getting with your much smaller files.
    I'm using the most current JVM, lots of RAM is available the application i'm developing can use up to 1.5 GB of RAM on the current machine. I'm using Mac OS 10.6 but the problem occurs on all operating systems i've tested it on including, Windows XP, Windows Vista, Windows 7, Mac OS 10.4, 10.5, 10.6, and Linux. I develop the software on an iMac which I just recently reformatted to factory settings so there are little to no other processes fighting over that storage.

    I've heard of JAI and just really never gave it a second thought cause I never really needed any more features then ImageIO gave me. However does Java Advanced Imaging read images in a different way then ImageIO would? I'm beginning to think that it isn't so much the actual size of the file but the dimensions. Even small files in the 100kilobyte range load slow because there dimensions are large for example 3000x3000. Though i'm assuming those 100 megabyte images you processed with speed also had large dimensions?

    Now for a moment I was thinking it was something in the BufferedImage class that was taking a long time to process and not the ImageIO class. However the following proves that wrong.
    try {
         ImageIO.read(someLargeImageFile);
    } catch(Exception err) {
    
    }
    The above code doesn't even use a buffered image so I guess its down to something in the ImageIO class. I'll see if I have better luck with JAI but can someone explain why this might be faster then using ImageIO.read() or if there are any other tips or ticks to reading large images such as these faster?

    Thank you for your help :)

    Edited by: neptune692 on Jan 6, 2011 8:35 AM
  • 5. Re: Read large images fast?
    800188 Newbie
    Currently Being Moderated
    I've gone ahead and tried some of the Java Advanced Imaging resources. I have loaded an image by doing the following:
         PlanarImage image = JAI.create("fileload", "/SomeImage.jpg");
    This piece of code still takes around 1 - 2 seconds to complete when reading an image file with the following attributes:

    Format: JPEG Image
    Size: 4.1 Megabytes
    Dimensions: 3648 x 2736
    Color space: RGB
    Color profile: Camera RBG Profile
    Focal Length 14.303
    Alpha channel: No
    F number: 4.5

    Now I just started literally 10 minutes ago learning how to use JAI so maybe there is a faster way to load images like you mentioned but so far it seems it has no beneficial speed increase at least by the method i'm using. I'll keep experimenting with it but anymore help would be great.

    Thank you guys :)

    Edited by: neptune692 on Jan 6, 2011 12:31 PM
  • 6. Re: Read large images fast?
    793415 Pro
    Currently Being Moderated
    ..does anyone have any tips or tricks I could use to read large images faster.
    Is that a question?

    In any case. I recommend wrapping an image InputStream in a ProgressMonitorInputStream before handing it to ImageIO.

    It will be no faster, in fact - it will be slightly slower, but it will seem faster to the observer.
  • 7. Re: Read large images fast?
    800188 Newbie
    Currently Being Moderated
    Andrew Thompson wrote:
    ..does anyone have any tips or tricks I could use to read large images faster.
    Is that a question?
    Haha, thanks for pointing that out. I've never been the best in grammer... Yes its a question.
    Andrew Thompson wrote:
    In any case. I recommend wrapping an image InputStream in a ProgressMonitorInputStream before handing it to ImageIO.
    It will be no faster, in fact - it will be slightly slower, but it will seem faster to the observer.
    Ok i'll try that. Just curious though if it is physically slower how is it possible that it will appear to load faster to the user? Will the image appear on the screen faster and if its not too much trouble, i'm kind of new at what your suggesting, can you give me an example. Maybe just a line of code or something.

    EDIT:

    I understand now it will show to the user a dialog depicting the current progress if reading the image is taking long. I was more hoping to find some way of just reading the image faster so it could be displayed faster.

    Thank you.
  • 8. Re: Read large images fast?
    793415 Pro
    Currently Being Moderated
    neptune692 wrote:
    ..it will show to the user a dialog depicting the current progress if reading the image is taking long.
    Yep. That was what I was talking about.
    .. I was more hoping to find some way of just reading the image faster so it could be displayed faster.
    You'd be surprised at the 'power of suggestion' that progress is happening - on the user's overall perception of program performance. ;)
  • 9. Re: Read large images fast?
    800188 Newbie
    Currently Being Moderated
    Andrew Thompson wrote:
    neptune692 wrote:
    ..it will show to the user a dialog depicting the current progress if reading the image is taking long.
    Yep. That was what I was talking about.
    .. I was more hoping to find some way of just reading the image faster so it could be displayed faster.
    You'd be surprised at the 'power of suggestion' that progress is happening - on the user's overall perception of program performance. ;)
    :) Yeah thank you for your help i'll probably just go with that. But as a last resort I was thinking is it possible to load the image in parts. For a rough example say by having three threads running simultaneously each working on constructing a different part of the image and when all three threads are done the three parts of the image are pieced together to form the final image. If i'm correct that would load the image 3 times faster? Just wondering if that is possible. I thought I heard somewhere about tiled images but haven't' been able to find a good tutorial on it.

    Thanks again.

    Edited by: neptune692 on Jan 6, 2011 9:30 PM
  • 10. Re: Read large images fast?
    793415 Pro
    Currently Being Moderated
    neptune692 wrote:
    ..For a rough example say by having three threads running simultaneously each working on constructing a different part of the image and when all three threads are done the three parts of the image are pieced together to form the final image. If i'm correct that would load the image 3 times faster? ..
    Not sure. If the answer is to be found though, I almost expect it will be found on the JAI forum. Do you want the thread moved?
  • 11. Re: Read large images fast?
    800188 Newbie
    Currently Being Moderated
    Andrew Thompson wrote:
    neptune692 wrote:
    ..For a rough example say by having three threads running simultaneously each working on constructing a different part of the image and when all three threads are done the three parts of the image are pieced together to form the final image. If i'm correct that would load the image 3 times faster? ..
    Not sure. If the answer is to be found though, I almost expect it will be found on the JAI forum. Do you want the thread moved?
    Yeah that's it I heard that it was something too do with Java Advanced Imaging so if you could move the thread there that would be great. Or would it be better if I just post a new question there?

    Thank you for your help.
  • 12. Re: Read large images fast?
    PhHein Guru Moderator
    Currently Being Moderated
    Moderator action: thread moved to JAI

Legend

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