Results 1 to 16 of 16
  1. #1
    neptune692 is offline Member
    Join Date
    Jan 2011
    Posts
    20
    Rep Power
    0

    Question Read and display large images quickly

    Hello all, i'm not sure if i'm in the right forum but it seems appropriate to me so feel free to move this thread if you want.

    I have been working on an application that reads and displays images and so far its been going great but i've run into a problem where large images load slowly making the application appear sluggish. Now it seems that the file size of the image is not what is making the load time increase but instead the dimensions of the image. For example an image 3000x3000 pixels and a size of 100 kilobytes loads very slow. Whereas a small image (500x500) with a size of 3 megabytes loads very quick. So i'm really stumped on how to get around this slow loading. I've seen applications such as Windows Preview and iPhoto load these same large images very fast and i'm just wondering if that is possible to do in Java and if so how would I go about implementing this?

    Here is an example of how i'm currently loading images:

    Java Code:
    try {
         BufferedImage image = ImageIO.read(someLargeImageFile);
    } catch(Exception err) {
    
    }

    Any help on this would be much appreciated. I have asked this question on another forum but that was awhile ago and its really going nowhere so I thought I would give this forum a try before I give up on finding a solution. I also have researched on google a lot with no success.

    Thank you for your help :)

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

  3. #3
    neptune692 is offline Member
    Join Date
    Jan 2011
    Posts
    20
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    1) don't ignore exceptions
    Thank you, I know its not a good idea to ignore exceptions. I was just being sloppy and trying to get an example done fast.
    Last edited by neptune692; 01-11-2011 at 02:27 AM.

  4. #4
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    12,016
    Rep Power
    20

    Default

    Large images are large.
    As far as I'm aware the Image class (well, classes) are uncompressed versions of an image, so it is the actual dimensions of the image that matter. The file type is irrelevant. Create a BMP version of that 3000x3000 image and see how big it is...that's what Java will be working with (at least that general size).

    Not knowing what it is your system is doing I can't say how to fill the time while an image is loading.

  5. #5
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default

    Yeah, don't forget that once loaded into a buffered image, the original file size is irrelevant - a bitmap uses no compression for its array of pixels, so the bigger the array, the more memory it uses. Make sure that in between image loads you call System.gc(); to manually force garbage collection. This can help keep your app from bloating if you've assigned it a lot of ram to work with. You might also consider preloading images in the background.

    I wrote a number of image sorting applications, which take a second or two to load the first image, but subsequent images are buffered in the background 10 or so in advance. Keep in mind though, this is RAM intensive, and will probably require you to set the -Xmx and -Xms flags to something large on launch.

  6. #6
    neptune692 is offline Member
    Join Date
    Jan 2011
    Posts
    20
    Rep Power
    0

    Default

    Quote Originally Posted by quad64bit View Post
    Yeah, don't forget that once loaded into a buffered image, the original file size is irrelevant - a bitmap uses no compression for its array of pixels, so the bigger the array, the more memory it uses. Make sure that in between image loads you call System.gc(); to manually force garbage collection. This can help keep your app from bloating if you've assigned it a lot of ram to work with. You might also consider preloading images in the background.

    I wrote a number of image sorting applications, which take a second or two to load the first image, but subsequent images are buffered in the background 10 or so in advance. Keep in mind though, this is RAM intensive, and will probably require you to set the -Xmx and -Xms flags to something large on launch.
    Thank you all for your help. I am currently using -Xms flags to allocate more RAM for my application so I could very well pre load some images however I'm also not sure how I would implement that effectively if the user has say 100 available images to choose from i'm not sure how I would know which ones to pre load and pre loading all 100 images may use too much memory even with 1.5GB available but then again i'm not sure. I was just hoping there was a faster way to read images but its looking like there isn't.

    Quote Originally Posted by Tolls
    Large images are large.
    As far as I'm aware the Image class (well, classes) are uncompressed versions of an image, so it is the actual dimensions of the image that matter. The file type is irrelevant. Create a BMP version of that 3000x3000 image and see how big it is...that's what Java will be working with (at least that general size).
    Thank you for that info I didn't know that. That will make it easier to determine if pre loading all the images in the background would be a realistic idea or not.

    I guess if I can't figure out a way to load an image faster I will probably go towards pre loading some images in the background hopefully not using too much RAM. I'm just kind of stumped though, I know it should be possible to load these images faster seeing as applications such as Windows Preview and iPhoto seem to do just that at the images original size (Unless iPhoto is pre loading the images of course).

    Thank you guys again for all your help. :)
    Last edited by neptune692; 01-11-2011 at 05:12 PM. Reason: Grammer / Punctuation

  7. #7
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default

    "to load these images faster seeing as applications such as Windows Preview and iPhoto"

    These applications are both native code and in the case of iPhoto, accelerated by the GPU via Quartz Extreme. This makes things MUCH faster. Java2D is not the fastest thing in the world, even by pure software standards. A Java2D particle simulation I wrote ran at about .5FPS. I switched to rendering with OpenGL via JOGL, and the simulation jumped to 140FPS. The logic was the same, but hardware did the drawing. Makes all the difference. But also, native image libraries (like quickdraw back in the day on MacOS) were much faster than java2d.

    Careful memory management, background loading, and paying attention to how you draw your images (only render what can be seen on the screen, scale only when needed, not storing images in memory that aren't being used shortly, etc...) all affect performance a great deal. I also had HUGE slowdowns when I let java do its own garbage collection cycle with large amounts of memory - old images no longer used lingered in memory until java hit the cap - then garbage collection occurred. Very slow. Triggering it manually after each image disposal (when you're done with the image) helped a great deal. You can also attach a profiler to your app at runtime to monitor the realtime memory usage for fine tuning. You might also time some of your methods to find out where the biggest slow downs occur and try to optimize those parts. If its in the ImageIO.read() block, then there isn't much you can do about it.

  8. #8
    toadaly is offline Senior Member
    Join Date
    Jan 2009
    Posts
    671
    Rep Power
    6

    Default

    I'm just kind of stumped though, I know it should be possible to load these images faster seeing as applications such as Windows Preview and iPhoto seem to do just that at the images original size (Unless iPhoto is pre loading the images of course).
    Preview software does not load and decompress the entire image. Some image files have built-in preview images, and thats' what get's used for previews. Other types, like jpeg, have an encoding scheme that fascilitates decimated decompression so you can decompress, say, every tenth block of data to form a smaller version of the image from. For raster type images, the preview software will simply step through the file reading 1 pixel for every N, and forming a smaller image from those.

    If you're trying to form a preview image, you can do the same thing, but if you're trying to load and display the entire image, it's going to take time.

  9. #9
    neptune692 is offline Member
    Join Date
    Jan 2011
    Posts
    20
    Rep Power
    0

    Default

    Quote Originally Posted by quad64bit View Post
    "to load these images faster seeing as applications such as Windows Preview and iPhoto"

    These applications are both native code and in the case of iPhoto, accelerated by the GPU via Quartz Extreme. This makes things MUCH faster. Java2D is not the fastest thing in the world, even by pure software standards. A Java2D particle simulation I wrote ran at about .5FPS. I switched to rendering with OpenGL via JOGL, and the simulation jumped to 140FPS. The logic was the same, but hardware did the drawing. Makes all the difference. But also, native image libraries (like quickdraw back in the day on MacOS) were much faster than java2d.

    Careful memory management, background loading, and paying attention to how you draw your images (only render what can be seen on the screen, scale only when needed, not storing images in memory that aren't being used shortly, etc...) all affect performance a great deal. I also had HUGE slowdowns when I let java do its own garbage collection cycle with large amounts of memory - old images no longer used lingered in memory until java hit the cap - then garbage collection occurred. Very slow. Triggering it manually after each image disposal (when you're done with the image) helped a great deal. You can also attach a profiler to your app at runtime to monitor the realtime memory usage for fine tuning. You might also time some of your methods to find out where the biggest slow downs occur and try to optimize those parts. If its in the ImageIO.read() block, then there isn't much you can do about it.
    Thank you for the info in both your posts. Very helpful. I've decided to implement your idea on loading some images in advance which works quite well when the user is moving through images with the arrow keys since I can just preload 5 images behind the selected image and 5 infront so loading appears much faster. I've also added a fading animation when images change which eats up an extra 4 milliseconds just incase the application needs a little more load time it can do it while the animation is in progress. So I'm happy to say that I think you guys have answered my question. Thank you :)

    On a side note i've been having trouble rendering smooth crossfade animations at large screen sizes I think JOGL could help in this situation. Could you maybe point me in the direction of a simple tutorial on implementing JOGL for working with animations? I'd really appreciate it. Thank you.

  10. #10
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default

    I've since switched to the Light Weight Java Game Library - the jogl project is kind of a mess - infrequent updates and none of the tutorials match the API anymore. LWJGL is pretty powerful and also has OpenGL bindings - Minecraft was built on it! lwjgl.org - Home of the Lightweight Java Game Library. There is tons of great documentation on the site. Good luck!

  11. #11
    neptune692 is offline Member
    Join Date
    Jan 2011
    Posts
    20
    Rep Power
    0

    Default

    Thanks for the info. I have since figured out that I really don't need something as sophisticated as that but simply need what VolatileImage offers. On Windows it works great and is hardware accelerated. I see a significant improvement in fps with all smooth animations. However on Mac OS VolatileImage doesn't help at all with isAccelerated() always returning false. I'm not sure why that is but on Windows isAccelerated() returns true and the animations run smooth, I just can't get hardware acceleration to work on the Mac and i'm using a 2010 iMac with the latest JVM. I guess I should probably post another question I don't want to get too off topic. But if you have any thoughts on how to enable hardware acceleration on the mac i'd love to here it.

    Thanks again for all your help, i'm finally making some progress with my application. Feels good not to be stuck anymore :)

  12. #12
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default

    Sadly, I'm pretty sure that Volatile image is NOT accelerated in MacOS. Lets hope now that Oracle is taking over development of the MacOS JVM through the Open JVM that Java 1.7 will bring some of these optimizations over.

  13. #13
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default

    P.S. the Java media Framework (JMF) also doesn't support a lot of codecs outside of windows - i.e. mp3!

  14. #14
    neptune692 is offline Member
    Join Date
    Jan 2011
    Posts
    20
    Rep Power
    0

    Default

    Oh darn :( and it makes such a drastic improvement to my program. Any alternatives that could just help with something as simple as fading a large image?

    Oh and how do I mark this thread as solved? Or is there a way to give credit to the correct answer like on Oracle forums?

    Thanks again.

  15. #15
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default

    You know, there is a way to do all that, and I'm not sure what it is! :D I always answer questions, I rarely ask them:D Try clicking on some of the buttons on this page - I think the OP sees buttons commenters don't - you might also be able to click on a name to get some more options. There is a flag to mark a thread as solved in there somewhere!

  16. #16
    neptune692 is offline Member
    Join Date
    Jan 2011
    Posts
    20
    Rep Power
    0

    Default

    Alright thanks, got the thread solved part figured out :-D

Similar Threads

  1. How to display Images?
    By Rectal Exambot in forum New To Java
    Replies: 12
    Last Post: 10-11-2010, 04:36 AM
  2. Replies: 3
    Last Post: 11-10-2009, 10:24 PM
  3. Draw Large Images
    By Sreekanth.m in forum Java 2D
    Replies: 1
    Last Post: 09-08-2009, 04:53 PM
  4. Loading large images in JavaME
    By b12 in forum Advanced Java
    Replies: 1
    Last Post: 04-01-2009, 11:02 AM
  5. Displaying large images
    By pir8ped in forum AWT / Swing
    Replies: 2
    Last Post: 01-21-2009, 09:20 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •